@@ -157,6 +157,8 @@ class WebAssemblyFastISel final : public FastISel {
157157 MachineMemOperand *MMO);
158158 unsigned maskI1Value (unsigned Reg, const Value *V);
159159 unsigned getRegForI1Value (const Value *V, const BasicBlock *BB, bool &Not);
160+ unsigned truncate (unsigned Reg, const Value *V, MVT::SimpleValueType From,
161+ MVT::SimpleValueType To);
160162 unsigned zeroExtendToI32 (unsigned Reg, const Value *V,
161163 MVT::SimpleValueType From);
162164 unsigned signExtendToI32 (unsigned Reg, const Value *V,
@@ -519,6 +521,28 @@ unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
519521 return Right;
520522}
521523
524+ unsigned WebAssemblyFastISel::truncate (unsigned Reg, const Value *V,
525+ MVT::SimpleValueType From,
526+ MVT::SimpleValueType To) {
527+ if (From == MVT::i64 ) {
528+ if (To == MVT::i64 )
529+ return copyValue (Reg);
530+
531+ if (To == MVT::i1 || To == MVT::i8 || To == MVT::i16 || To == MVT::i32 ) {
532+ Register Result = createResultReg (&WebAssembly::I32RegClass);
533+ BuildMI (*FuncInfo.MBB , FuncInfo.InsertPt , MIMD,
534+ TII.get (WebAssembly::I32_WRAP_I64), Result)
535+ .addReg (Reg);
536+ return Result;
537+ }
538+ }
539+
540+ if (From == MVT::i32 )
541+ return copyValue (Reg);
542+
543+ return 0 ;
544+ }
545+
522546unsigned WebAssemblyFastISel::zeroExtend (unsigned Reg, const Value *V,
523547 MVT::SimpleValueType From,
524548 MVT::SimpleValueType To) {
@@ -988,23 +1012,17 @@ bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
9881012bool WebAssemblyFastISel::selectTrunc (const Instruction *I) {
9891013 const auto *Trunc = cast<TruncInst>(I);
9901014
991- Register Reg = getRegForValue (Trunc->getOperand (0 ));
1015+ const Value *Op = Trunc->getOperand (0 );
1016+ MVT::SimpleValueType From = getSimpleType (Op->getType ());
1017+ MVT::SimpleValueType To = getLegalType (getSimpleType (Trunc->getType ()));
1018+ Register In = getRegForValue (Op);
1019+ if (In == 0 )
1020+ return false ;
1021+ unsigned Reg = truncate (In, Op, From, To);
9921022 if (Reg == 0 )
9931023 return false ;
9941024
995- unsigned FromBitWidth = Trunc->getOperand (0 )->getType ()->getIntegerBitWidth ();
996- unsigned ToBitWidth = Trunc->getType ()->getIntegerBitWidth ();
997-
998- if (ToBitWidth <= 32 && (32 < FromBitWidth && FromBitWidth <= 64 )) {
999- Register Result = createResultReg (&WebAssembly::I32RegClass);
1000- BuildMI (*FuncInfo.MBB , FuncInfo.InsertPt , MIMD,
1001- TII.get (WebAssembly::I32_WRAP_I64), Result)
1002- .addReg (Reg);
1003- Reg = Result;
1004- }
1005-
1006- updateValueMap (Trunc, Reg);
1007- return true ;
1025+ updateValueMap (Trunc, Reg); return true ;
10081026}
10091027
10101028bool WebAssemblyFastISel::selectZExt (const Instruction *I) {
0 commit comments