1*0b57cec5SDimitry Andric //===-- WebAssemblyFastISel.cpp - WebAssembly FastISel implementation -----===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric ///
9*0b57cec5SDimitry Andric /// \file
10*0b57cec5SDimitry Andric /// This file defines the WebAssembly-specific support for the FastISel
11*0b57cec5SDimitry Andric /// class. Some of the target-specific code is generated by tablegen in the file
12*0b57cec5SDimitry Andric /// WebAssemblyGenFastISel.inc, which is #included here.
13*0b57cec5SDimitry Andric ///
14*0b57cec5SDimitry Andric /// TODO: kill flags
15*0b57cec5SDimitry Andric ///
16*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
191fd87a68SDimitry Andric #include "Utils/WebAssemblyTypeUtilities.h"
20fe6060f1SDimitry Andric #include "Utils/WebAssemblyUtilities.h"
21*0b57cec5SDimitry Andric #include "WebAssembly.h"
22*0b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
23*0b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
24*0b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h"
25*0b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
26*0b57cec5SDimitry Andric #include "llvm/CodeGen/FastISel.h"
27*0b57cec5SDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h"
28*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
29*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
30*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
31e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
32*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
33*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
34*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
35*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
36*0b57cec5SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
37*0b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h"
38*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
39*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
40*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
41*0b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
42*0b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric using namespace llvm;
45*0b57cec5SDimitry Andric using namespace PatternMatch;
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-fastisel"
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric namespace {
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric class WebAssemblyFastISel final : public FastISel {
52*0b57cec5SDimitry Andric   // All possible address modes.
53*0b57cec5SDimitry Andric   class Address {
54*0b57cec5SDimitry Andric   public:
55*0b57cec5SDimitry Andric     using BaseKind = enum { RegBase, FrameIndexBase };
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric   private:
58*0b57cec5SDimitry Andric     BaseKind Kind = RegBase;
59*0b57cec5SDimitry Andric     union {
60*0b57cec5SDimitry Andric       unsigned Reg;
61*0b57cec5SDimitry Andric       int FI;
62*0b57cec5SDimitry Andric     } Base;
63*0b57cec5SDimitry Andric 
64eaeb601bSDimitry Andric     // Whether the base has been determined yet
65eaeb601bSDimitry Andric     bool IsBaseSet = false;
66eaeb601bSDimitry Andric 
67*0b57cec5SDimitry Andric     int64_t Offset = 0;
68*0b57cec5SDimitry Andric 
69*0b57cec5SDimitry Andric     const GlobalValue *GV = nullptr;
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   public:
72*0b57cec5SDimitry Andric     // Innocuous defaults for our address.
73*0b57cec5SDimitry Andric     Address() { Base.Reg = 0; }
74*0b57cec5SDimitry Andric     void setKind(BaseKind K) {
75*0b57cec5SDimitry Andric       assert(!isSet() && "Can't change kind with non-zero base");
76*0b57cec5SDimitry Andric       Kind = K;
77*0b57cec5SDimitry Andric     }
78*0b57cec5SDimitry Andric     BaseKind getKind() const { return Kind; }
79*0b57cec5SDimitry Andric     bool isRegBase() const { return Kind == RegBase; }
80*0b57cec5SDimitry Andric     bool isFIBase() const { return Kind == FrameIndexBase; }
81*0b57cec5SDimitry Andric     void setReg(unsigned Reg) {
82*0b57cec5SDimitry Andric       assert(isRegBase() && "Invalid base register access!");
83eaeb601bSDimitry Andric       assert(!IsBaseSet && "Base cannot be reset");
84*0b57cec5SDimitry Andric       Base.Reg = Reg;
85eaeb601bSDimitry Andric       IsBaseSet = true;
86*0b57cec5SDimitry Andric     }
87*0b57cec5SDimitry Andric     unsigned getReg() const {
88*0b57cec5SDimitry Andric       assert(isRegBase() && "Invalid base register access!");
89*0b57cec5SDimitry Andric       return Base.Reg;
90*0b57cec5SDimitry Andric     }
91*0b57cec5SDimitry Andric     void setFI(unsigned FI) {
92*0b57cec5SDimitry Andric       assert(isFIBase() && "Invalid base frame index access!");
93eaeb601bSDimitry Andric       assert(!IsBaseSet && "Base cannot be reset");
94*0b57cec5SDimitry Andric       Base.FI = FI;
95eaeb601bSDimitry Andric       IsBaseSet = true;
96*0b57cec5SDimitry Andric     }
97*0b57cec5SDimitry Andric     unsigned getFI() const {
98*0b57cec5SDimitry Andric       assert(isFIBase() && "Invalid base frame index access!");
99*0b57cec5SDimitry Andric       return Base.FI;
100*0b57cec5SDimitry Andric     }
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric     void setOffset(int64_t NewOffset) {
103*0b57cec5SDimitry Andric       assert(NewOffset >= 0 && "Offsets must be non-negative");
104*0b57cec5SDimitry Andric       Offset = NewOffset;
105*0b57cec5SDimitry Andric     }
106*0b57cec5SDimitry Andric     int64_t getOffset() const { return Offset; }
107*0b57cec5SDimitry Andric     void setGlobalValue(const GlobalValue *G) { GV = G; }
108*0b57cec5SDimitry Andric     const GlobalValue *getGlobalValue() const { return GV; }
109eaeb601bSDimitry Andric     bool isSet() const { return IsBaseSet; }
110*0b57cec5SDimitry Andric   };
111*0b57cec5SDimitry Andric 
112*0b57cec5SDimitry Andric   /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
113*0b57cec5SDimitry Andric   /// right decision when generating code for different targets.
114*0b57cec5SDimitry Andric   const WebAssemblySubtarget *Subtarget;
115*0b57cec5SDimitry Andric   LLVMContext *Context;
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric private:
118*0b57cec5SDimitry Andric   // Utility helper routines
119*0b57cec5SDimitry Andric   MVT::SimpleValueType getSimpleType(Type *Ty) {
120*0b57cec5SDimitry Andric     EVT VT = TLI.getValueType(DL, Ty, /*AllowUnknown=*/true);
121*0b57cec5SDimitry Andric     return VT.isSimple() ? VT.getSimpleVT().SimpleTy
122*0b57cec5SDimitry Andric                          : MVT::INVALID_SIMPLE_VALUE_TYPE;
123*0b57cec5SDimitry Andric   }
124*0b57cec5SDimitry Andric   MVT::SimpleValueType getLegalType(MVT::SimpleValueType VT) {
125*0b57cec5SDimitry Andric     switch (VT) {
126*0b57cec5SDimitry Andric     case MVT::i1:
127*0b57cec5SDimitry Andric     case MVT::i8:
128*0b57cec5SDimitry Andric     case MVT::i16:
129*0b57cec5SDimitry Andric       return MVT::i32;
130*0b57cec5SDimitry Andric     case MVT::i32:
131*0b57cec5SDimitry Andric     case MVT::i64:
132*0b57cec5SDimitry Andric     case MVT::f32:
133*0b57cec5SDimitry Andric     case MVT::f64:
134fe6060f1SDimitry Andric       return VT;
135e8d8bef9SDimitry Andric     case MVT::funcref:
136e8d8bef9SDimitry Andric     case MVT::externref:
137fe6060f1SDimitry Andric       if (Subtarget->hasReferenceTypes())
138*0b57cec5SDimitry Andric         return VT;
139fe6060f1SDimitry Andric       break;
140*0b57cec5SDimitry Andric     case MVT::f16:
141*0b57cec5SDimitry Andric       return MVT::f32;
142*0b57cec5SDimitry Andric     case MVT::v16i8:
143*0b57cec5SDimitry Andric     case MVT::v8i16:
144*0b57cec5SDimitry Andric     case MVT::v4i32:
145*0b57cec5SDimitry Andric     case MVT::v4f32:
146*0b57cec5SDimitry Andric     case MVT::v2i64:
147*0b57cec5SDimitry Andric     case MVT::v2f64:
148fe6060f1SDimitry Andric       if (Subtarget->hasSIMD128())
149*0b57cec5SDimitry Andric         return VT;
150*0b57cec5SDimitry Andric       break;
151*0b57cec5SDimitry Andric     default:
152*0b57cec5SDimitry Andric       break;
153*0b57cec5SDimitry Andric     }
154*0b57cec5SDimitry Andric     return MVT::INVALID_SIMPLE_VALUE_TYPE;
155*0b57cec5SDimitry Andric   }
156*0b57cec5SDimitry Andric   bool computeAddress(const Value *Obj, Address &Addr);
157*0b57cec5SDimitry Andric   void materializeLoadStoreOperands(Address &Addr);
158*0b57cec5SDimitry Andric   void addLoadStoreOperands(const Address &Addr, const MachineInstrBuilder &MIB,
159*0b57cec5SDimitry Andric                             MachineMemOperand *MMO);
160*0b57cec5SDimitry Andric   unsigned maskI1Value(unsigned Reg, const Value *V);
16169ade1e0SDimitry Andric   unsigned getRegForI1Value(const Value *V, const BasicBlock *BB, bool &Not);
162*0b57cec5SDimitry Andric   unsigned zeroExtendToI32(unsigned Reg, const Value *V,
163*0b57cec5SDimitry Andric                            MVT::SimpleValueType From);
164*0b57cec5SDimitry Andric   unsigned signExtendToI32(unsigned Reg, const Value *V,
165*0b57cec5SDimitry Andric                            MVT::SimpleValueType From);
166*0b57cec5SDimitry Andric   unsigned zeroExtend(unsigned Reg, const Value *V, MVT::SimpleValueType From,
167*0b57cec5SDimitry Andric                       MVT::SimpleValueType To);
168*0b57cec5SDimitry Andric   unsigned signExtend(unsigned Reg, const Value *V, MVT::SimpleValueType From,
169*0b57cec5SDimitry Andric                       MVT::SimpleValueType To);
170*0b57cec5SDimitry Andric   unsigned getRegForUnsignedValue(const Value *V);
171*0b57cec5SDimitry Andric   unsigned getRegForSignedValue(const Value *V);
172*0b57cec5SDimitry Andric   unsigned getRegForPromotedValue(const Value *V, bool IsSigned);
173*0b57cec5SDimitry Andric   unsigned notValue(unsigned Reg);
174*0b57cec5SDimitry Andric   unsigned copyValue(unsigned Reg);
175*0b57cec5SDimitry Andric 
176*0b57cec5SDimitry Andric   // Backend specific FastISel code.
177*0b57cec5SDimitry Andric   unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
178*0b57cec5SDimitry Andric   unsigned fastMaterializeConstant(const Constant *C) override;
179*0b57cec5SDimitry Andric   bool fastLowerArguments() override;
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric   // Selection routines.
182*0b57cec5SDimitry Andric   bool selectCall(const Instruction *I);
183*0b57cec5SDimitry Andric   bool selectSelect(const Instruction *I);
184*0b57cec5SDimitry Andric   bool selectTrunc(const Instruction *I);
185*0b57cec5SDimitry Andric   bool selectZExt(const Instruction *I);
186*0b57cec5SDimitry Andric   bool selectSExt(const Instruction *I);
187*0b57cec5SDimitry Andric   bool selectICmp(const Instruction *I);
188*0b57cec5SDimitry Andric   bool selectFCmp(const Instruction *I);
189*0b57cec5SDimitry Andric   bool selectBitCast(const Instruction *I);
190*0b57cec5SDimitry Andric   bool selectLoad(const Instruction *I);
191*0b57cec5SDimitry Andric   bool selectStore(const Instruction *I);
192*0b57cec5SDimitry Andric   bool selectBr(const Instruction *I);
193*0b57cec5SDimitry Andric   bool selectRet(const Instruction *I);
194*0b57cec5SDimitry Andric   bool selectUnreachable(const Instruction *I);
195*0b57cec5SDimitry Andric 
196*0b57cec5SDimitry Andric public:
197*0b57cec5SDimitry Andric   // Backend specific FastISel code.
198*0b57cec5SDimitry Andric   WebAssemblyFastISel(FunctionLoweringInfo &FuncInfo,
199*0b57cec5SDimitry Andric                       const TargetLibraryInfo *LibInfo)
200*0b57cec5SDimitry Andric       : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
201*0b57cec5SDimitry Andric     Subtarget = &FuncInfo.MF->getSubtarget<WebAssemblySubtarget>();
202*0b57cec5SDimitry Andric     Context = &FuncInfo.Fn->getContext();
203*0b57cec5SDimitry Andric   }
204*0b57cec5SDimitry Andric 
205*0b57cec5SDimitry Andric   bool fastSelectInstruction(const Instruction *I) override;
206*0b57cec5SDimitry Andric 
207*0b57cec5SDimitry Andric #include "WebAssemblyGenFastISel.inc"
208*0b57cec5SDimitry Andric };
209*0b57cec5SDimitry Andric 
210*0b57cec5SDimitry Andric } // end anonymous namespace
211*0b57cec5SDimitry Andric 
212*0b57cec5SDimitry Andric bool WebAssemblyFastISel::computeAddress(const Value *Obj, Address &Addr) {
213*0b57cec5SDimitry Andric   const User *U = nullptr;
214*0b57cec5SDimitry Andric   unsigned Opcode = Instruction::UserOp1;
215*0b57cec5SDimitry Andric   if (const auto *I = dyn_cast<Instruction>(Obj)) {
216*0b57cec5SDimitry Andric     // Don't walk into other basic blocks unless the object is an alloca from
217*0b57cec5SDimitry Andric     // another block, otherwise it may not have a virtual register assigned.
218*0b57cec5SDimitry Andric     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
219*0b57cec5SDimitry Andric         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
220*0b57cec5SDimitry Andric       Opcode = I->getOpcode();
221*0b57cec5SDimitry Andric       U = I;
222*0b57cec5SDimitry Andric     }
223*0b57cec5SDimitry Andric   } else if (const auto *C = dyn_cast<ConstantExpr>(Obj)) {
224*0b57cec5SDimitry Andric     Opcode = C->getOpcode();
225*0b57cec5SDimitry Andric     U = C;
226*0b57cec5SDimitry Andric   }
227*0b57cec5SDimitry Andric 
228*0b57cec5SDimitry Andric   if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
229*0b57cec5SDimitry Andric     if (Ty->getAddressSpace() > 255)
230*0b57cec5SDimitry Andric       // Fast instruction selection doesn't support the special
231*0b57cec5SDimitry Andric       // address spaces.
232*0b57cec5SDimitry Andric       return false;
233*0b57cec5SDimitry Andric 
234*0b57cec5SDimitry Andric   if (const auto *GV = dyn_cast<GlobalValue>(Obj)) {
235*0b57cec5SDimitry Andric     if (TLI.isPositionIndependent())
236*0b57cec5SDimitry Andric       return false;
237*0b57cec5SDimitry Andric     if (Addr.getGlobalValue())
238*0b57cec5SDimitry Andric       return false;
239*0b57cec5SDimitry Andric     if (GV->isThreadLocal())
240*0b57cec5SDimitry Andric       return false;
241*0b57cec5SDimitry Andric     Addr.setGlobalValue(GV);
242*0b57cec5SDimitry Andric     return true;
243*0b57cec5SDimitry Andric   }
244*0b57cec5SDimitry Andric 
245*0b57cec5SDimitry Andric   switch (Opcode) {
246*0b57cec5SDimitry Andric   default:
247*0b57cec5SDimitry Andric     break;
248*0b57cec5SDimitry Andric   case Instruction::BitCast: {
249*0b57cec5SDimitry Andric     // Look through bitcasts.
250*0b57cec5SDimitry Andric     return computeAddress(U->getOperand(0), Addr);
251*0b57cec5SDimitry Andric   }
252*0b57cec5SDimitry Andric   case Instruction::IntToPtr: {
253*0b57cec5SDimitry Andric     // Look past no-op inttoptrs.
254*0b57cec5SDimitry Andric     if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
255*0b57cec5SDimitry Andric         TLI.getPointerTy(DL))
256*0b57cec5SDimitry Andric       return computeAddress(U->getOperand(0), Addr);
257*0b57cec5SDimitry Andric     break;
258*0b57cec5SDimitry Andric   }
259*0b57cec5SDimitry Andric   case Instruction::PtrToInt: {
260*0b57cec5SDimitry Andric     // Look past no-op ptrtoints.
261*0b57cec5SDimitry Andric     if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
262*0b57cec5SDimitry Andric       return computeAddress(U->getOperand(0), Addr);
263*0b57cec5SDimitry Andric     break;
264*0b57cec5SDimitry Andric   }
265*0b57cec5SDimitry Andric   case Instruction::GetElementPtr: {
266*0b57cec5SDimitry Andric     Address SavedAddr = Addr;
267*0b57cec5SDimitry Andric     uint64_t TmpOffset = Addr.getOffset();
268*0b57cec5SDimitry Andric     // Non-inbounds geps can wrap; wasm's offsets can't.
269*0b57cec5SDimitry Andric     if (!cast<GEPOperator>(U)->isInBounds())
270*0b57cec5SDimitry Andric       goto unsupported_gep;
271*0b57cec5SDimitry Andric     // Iterate through the GEP folding the constants into offsets where
272*0b57cec5SDimitry Andric     // we can.
273*0b57cec5SDimitry Andric     for (gep_type_iterator GTI = gep_type_begin(U), E = gep_type_end(U);
274*0b57cec5SDimitry Andric          GTI != E; ++GTI) {
275*0b57cec5SDimitry Andric       const Value *Op = GTI.getOperand();
276*0b57cec5SDimitry Andric       if (StructType *STy = GTI.getStructTypeOrNull()) {
277*0b57cec5SDimitry Andric         const StructLayout *SL = DL.getStructLayout(STy);
278*0b57cec5SDimitry Andric         unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
279*0b57cec5SDimitry Andric         TmpOffset += SL->getElementOffset(Idx);
280*0b57cec5SDimitry Andric       } else {
281*0b57cec5SDimitry Andric         uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
282*0b57cec5SDimitry Andric         for (;;) {
283*0b57cec5SDimitry Andric           if (const auto *CI = dyn_cast<ConstantInt>(Op)) {
284*0b57cec5SDimitry Andric             // Constant-offset addressing.
285*0b57cec5SDimitry Andric             TmpOffset += CI->getSExtValue() * S;
286*0b57cec5SDimitry Andric             break;
287*0b57cec5SDimitry Andric           }
288*0b57cec5SDimitry Andric           if (S == 1 && Addr.isRegBase() && Addr.getReg() == 0) {
289*0b57cec5SDimitry Andric             // An unscaled add of a register. Set it as the new base.
29004eeddc0SDimitry Andric             Register Reg = getRegForValue(Op);
291*0b57cec5SDimitry Andric             if (Reg == 0)
292*0b57cec5SDimitry Andric               return false;
293*0b57cec5SDimitry Andric             Addr.setReg(Reg);
294*0b57cec5SDimitry Andric             break;
295*0b57cec5SDimitry Andric           }
296*0b57cec5SDimitry Andric           if (canFoldAddIntoGEP(U, Op)) {
297*0b57cec5SDimitry Andric             // A compatible add with a constant operand. Fold the constant.
298*0b57cec5SDimitry Andric             auto *CI = cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
299*0b57cec5SDimitry Andric             TmpOffset += CI->getSExtValue() * S;
300*0b57cec5SDimitry Andric             // Iterate on the other operand.
301*0b57cec5SDimitry Andric             Op = cast<AddOperator>(Op)->getOperand(0);
302*0b57cec5SDimitry Andric             continue;
303*0b57cec5SDimitry Andric           }
304*0b57cec5SDimitry Andric           // Unsupported
305*0b57cec5SDimitry Andric           goto unsupported_gep;
306*0b57cec5SDimitry Andric         }
307*0b57cec5SDimitry Andric       }
308*0b57cec5SDimitry Andric     }
309*0b57cec5SDimitry Andric     // Don't fold in negative offsets.
310*0b57cec5SDimitry Andric     if (int64_t(TmpOffset) >= 0) {
311*0b57cec5SDimitry Andric       // Try to grab the base operand now.
312*0b57cec5SDimitry Andric       Addr.setOffset(TmpOffset);
313*0b57cec5SDimitry Andric       if (computeAddress(U->getOperand(0), Addr))
314*0b57cec5SDimitry Andric         return true;
315*0b57cec5SDimitry Andric     }
316*0b57cec5SDimitry Andric     // We failed, restore everything and try the other options.
317*0b57cec5SDimitry Andric     Addr = SavedAddr;
318*0b57cec5SDimitry Andric   unsupported_gep:
319*0b57cec5SDimitry Andric     break;
320*0b57cec5SDimitry Andric   }
321*0b57cec5SDimitry Andric   case Instruction::Alloca: {
322*0b57cec5SDimitry Andric     const auto *AI = cast<AllocaInst>(Obj);
323*0b57cec5SDimitry Andric     DenseMap<const AllocaInst *, int>::iterator SI =
324*0b57cec5SDimitry Andric         FuncInfo.StaticAllocaMap.find(AI);
325*0b57cec5SDimitry Andric     if (SI != FuncInfo.StaticAllocaMap.end()) {
326*0b57cec5SDimitry Andric       if (Addr.isSet()) {
327*0b57cec5SDimitry Andric         return false;
328*0b57cec5SDimitry Andric       }
329*0b57cec5SDimitry Andric       Addr.setKind(Address::FrameIndexBase);
330*0b57cec5SDimitry Andric       Addr.setFI(SI->second);
331*0b57cec5SDimitry Andric       return true;
332*0b57cec5SDimitry Andric     }
333*0b57cec5SDimitry Andric     break;
334*0b57cec5SDimitry Andric   }
335*0b57cec5SDimitry Andric   case Instruction::Add: {
336*0b57cec5SDimitry Andric     // Adds of constants are common and easy enough.
337*0b57cec5SDimitry Andric     const Value *LHS = U->getOperand(0);
338*0b57cec5SDimitry Andric     const Value *RHS = U->getOperand(1);
339*0b57cec5SDimitry Andric 
340*0b57cec5SDimitry Andric     if (isa<ConstantInt>(LHS))
341*0b57cec5SDimitry Andric       std::swap(LHS, RHS);
342*0b57cec5SDimitry Andric 
343*0b57cec5SDimitry Andric     if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
344*0b57cec5SDimitry Andric       uint64_t TmpOffset = Addr.getOffset() + CI->getSExtValue();
345*0b57cec5SDimitry Andric       if (int64_t(TmpOffset) >= 0) {
346*0b57cec5SDimitry Andric         Addr.setOffset(TmpOffset);
347*0b57cec5SDimitry Andric         return computeAddress(LHS, Addr);
348*0b57cec5SDimitry Andric       }
349*0b57cec5SDimitry Andric     }
350*0b57cec5SDimitry Andric 
351*0b57cec5SDimitry Andric     Address Backup = Addr;
352*0b57cec5SDimitry Andric     if (computeAddress(LHS, Addr) && computeAddress(RHS, Addr))
353*0b57cec5SDimitry Andric       return true;
354*0b57cec5SDimitry Andric     Addr = Backup;
355*0b57cec5SDimitry Andric 
356*0b57cec5SDimitry Andric     break;
357*0b57cec5SDimitry Andric   }
358*0b57cec5SDimitry Andric   case Instruction::Sub: {
359*0b57cec5SDimitry Andric     // Subs of constants are common and easy enough.
360*0b57cec5SDimitry Andric     const Value *LHS = U->getOperand(0);
361*0b57cec5SDimitry Andric     const Value *RHS = U->getOperand(1);
362*0b57cec5SDimitry Andric 
363*0b57cec5SDimitry Andric     if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
364*0b57cec5SDimitry Andric       int64_t TmpOffset = Addr.getOffset() - CI->getSExtValue();
365*0b57cec5SDimitry Andric       if (TmpOffset >= 0) {
366*0b57cec5SDimitry Andric         Addr.setOffset(TmpOffset);
367*0b57cec5SDimitry Andric         return computeAddress(LHS, Addr);
368*0b57cec5SDimitry Andric       }
369*0b57cec5SDimitry Andric     }
370*0b57cec5SDimitry Andric     break;
371*0b57cec5SDimitry Andric   }
372*0b57cec5SDimitry Andric   }
373*0b57cec5SDimitry Andric   if (Addr.isSet()) {
374*0b57cec5SDimitry Andric     return false;
375*0b57cec5SDimitry Andric   }
37604eeddc0SDimitry Andric   Register Reg = getRegForValue(Obj);
377*0b57cec5SDimitry Andric   if (Reg == 0)
378*0b57cec5SDimitry Andric     return false;
379*0b57cec5SDimitry Andric   Addr.setReg(Reg);
380*0b57cec5SDimitry Andric   return Addr.getReg() != 0;
381*0b57cec5SDimitry Andric }
382*0b57cec5SDimitry Andric 
383*0b57cec5SDimitry Andric void WebAssemblyFastISel::materializeLoadStoreOperands(Address &Addr) {
384*0b57cec5SDimitry Andric   if (Addr.isRegBase()) {
385*0b57cec5SDimitry Andric     unsigned Reg = Addr.getReg();
386*0b57cec5SDimitry Andric     if (Reg == 0) {
387*0b57cec5SDimitry Andric       Reg = createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
388*0b57cec5SDimitry Andric                                                    : &WebAssembly::I32RegClass);
389*0b57cec5SDimitry Andric       unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
390*0b57cec5SDimitry Andric                                             : WebAssembly::CONST_I32;
391bdd1243dSDimitry Andric       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), Reg)
392*0b57cec5SDimitry Andric           .addImm(0);
393*0b57cec5SDimitry Andric       Addr.setReg(Reg);
394*0b57cec5SDimitry Andric     }
395*0b57cec5SDimitry Andric   }
396*0b57cec5SDimitry Andric }
397*0b57cec5SDimitry Andric 
398*0b57cec5SDimitry Andric void WebAssemblyFastISel::addLoadStoreOperands(const Address &Addr,
399*0b57cec5SDimitry Andric                                                const MachineInstrBuilder &MIB,
400*0b57cec5SDimitry Andric                                                MachineMemOperand *MMO) {
401*0b57cec5SDimitry Andric   // Set the alignment operand (this is rewritten in SetP2AlignOperands).
402*0b57cec5SDimitry Andric   // TODO: Disable SetP2AlignOperands for FastISel and just do it here.
403*0b57cec5SDimitry Andric   MIB.addImm(0);
404*0b57cec5SDimitry Andric 
405*0b57cec5SDimitry Andric   if (const GlobalValue *GV = Addr.getGlobalValue())
406*0b57cec5SDimitry Andric     MIB.addGlobalAddress(GV, Addr.getOffset());
407*0b57cec5SDimitry Andric   else
408*0b57cec5SDimitry Andric     MIB.addImm(Addr.getOffset());
409*0b57cec5SDimitry Andric 
410*0b57cec5SDimitry Andric   if (Addr.isRegBase())
411*0b57cec5SDimitry Andric     MIB.addReg(Addr.getReg());
412*0b57cec5SDimitry Andric   else
413*0b57cec5SDimitry Andric     MIB.addFrameIndex(Addr.getFI());
414*0b57cec5SDimitry Andric 
415*0b57cec5SDimitry Andric   MIB.addMemOperand(MMO);
416*0b57cec5SDimitry Andric }
417*0b57cec5SDimitry Andric 
418*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::maskI1Value(unsigned Reg, const Value *V) {
419*0b57cec5SDimitry Andric   return zeroExtendToI32(Reg, V, MVT::i1);
420*0b57cec5SDimitry Andric }
421*0b57cec5SDimitry Andric 
42269ade1e0SDimitry Andric unsigned WebAssemblyFastISel::getRegForI1Value(const Value *V,
42369ade1e0SDimitry Andric                                                const BasicBlock *BB,
42469ade1e0SDimitry Andric                                                bool &Not) {
425*0b57cec5SDimitry Andric   if (const auto *ICmp = dyn_cast<ICmpInst>(V))
426*0b57cec5SDimitry Andric     if (const ConstantInt *C = dyn_cast<ConstantInt>(ICmp->getOperand(1)))
42769ade1e0SDimitry Andric       if (ICmp->isEquality() && C->isZero() && C->getType()->isIntegerTy(32) &&
42869ade1e0SDimitry Andric           ICmp->getParent() == BB) {
429*0b57cec5SDimitry Andric         Not = ICmp->isTrueWhenEqual();
430*0b57cec5SDimitry Andric         return getRegForValue(ICmp->getOperand(0));
431*0b57cec5SDimitry Andric       }
432*0b57cec5SDimitry Andric 
433*0b57cec5SDimitry Andric   Not = false;
43404eeddc0SDimitry Andric   Register Reg = getRegForValue(V);
435*0b57cec5SDimitry Andric   if (Reg == 0)
436*0b57cec5SDimitry Andric     return 0;
437*0b57cec5SDimitry Andric   return maskI1Value(Reg, V);
438*0b57cec5SDimitry Andric }
439*0b57cec5SDimitry Andric 
440*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::zeroExtendToI32(unsigned Reg, const Value *V,
441*0b57cec5SDimitry Andric                                               MVT::SimpleValueType From) {
442*0b57cec5SDimitry Andric   if (Reg == 0)
443*0b57cec5SDimitry Andric     return 0;
444*0b57cec5SDimitry Andric 
445*0b57cec5SDimitry Andric   switch (From) {
446*0b57cec5SDimitry Andric   case MVT::i1:
447*0b57cec5SDimitry Andric     // If the value is naturally an i1, we don't need to mask it. We only know
448*0b57cec5SDimitry Andric     // if a value is naturally an i1 if it is definitely lowered by FastISel,
449*0b57cec5SDimitry Andric     // not a DAG ISel fallback.
450*0b57cec5SDimitry Andric     if (V != nullptr && isa<Argument>(V) && cast<Argument>(V)->hasZExtAttr())
451*0b57cec5SDimitry Andric       return copyValue(Reg);
452*0b57cec5SDimitry Andric     break;
453*0b57cec5SDimitry Andric   case MVT::i8:
454*0b57cec5SDimitry Andric   case MVT::i16:
455*0b57cec5SDimitry Andric     break;
456*0b57cec5SDimitry Andric   case MVT::i32:
457*0b57cec5SDimitry Andric     return copyValue(Reg);
458*0b57cec5SDimitry Andric   default:
459*0b57cec5SDimitry Andric     return 0;
460*0b57cec5SDimitry Andric   }
461*0b57cec5SDimitry Andric 
46204eeddc0SDimitry Andric   Register Imm = createResultReg(&WebAssembly::I32RegClass);
463bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
464*0b57cec5SDimitry Andric           TII.get(WebAssembly::CONST_I32), Imm)
465*0b57cec5SDimitry Andric       .addImm(~(~uint64_t(0) << MVT(From).getSizeInBits()));
466*0b57cec5SDimitry Andric 
46704eeddc0SDimitry Andric   Register Result = createResultReg(&WebAssembly::I32RegClass);
468bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
469*0b57cec5SDimitry Andric           TII.get(WebAssembly::AND_I32), Result)
470*0b57cec5SDimitry Andric       .addReg(Reg)
471*0b57cec5SDimitry Andric       .addReg(Imm);
472*0b57cec5SDimitry Andric 
473*0b57cec5SDimitry Andric   return Result;
474*0b57cec5SDimitry Andric }
475*0b57cec5SDimitry Andric 
476*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
477*0b57cec5SDimitry Andric                                               MVT::SimpleValueType From) {
478*0b57cec5SDimitry Andric   if (Reg == 0)
479*0b57cec5SDimitry Andric     return 0;
480*0b57cec5SDimitry Andric 
481*0b57cec5SDimitry Andric   switch (From) {
482*0b57cec5SDimitry Andric   case MVT::i1:
483*0b57cec5SDimitry Andric   case MVT::i8:
484*0b57cec5SDimitry Andric   case MVT::i16:
485*0b57cec5SDimitry Andric     break;
486*0b57cec5SDimitry Andric   case MVT::i32:
487*0b57cec5SDimitry Andric     return copyValue(Reg);
488*0b57cec5SDimitry Andric   default:
489*0b57cec5SDimitry Andric     return 0;
490*0b57cec5SDimitry Andric   }
491*0b57cec5SDimitry Andric 
49204eeddc0SDimitry Andric   Register Imm = createResultReg(&WebAssembly::I32RegClass);
493bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
494*0b57cec5SDimitry Andric           TII.get(WebAssembly::CONST_I32), Imm)
495*0b57cec5SDimitry Andric       .addImm(32 - MVT(From).getSizeInBits());
496*0b57cec5SDimitry Andric 
49704eeddc0SDimitry Andric   Register Left = createResultReg(&WebAssembly::I32RegClass);
498bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
499*0b57cec5SDimitry Andric           TII.get(WebAssembly::SHL_I32), Left)
500*0b57cec5SDimitry Andric       .addReg(Reg)
501*0b57cec5SDimitry Andric       .addReg(Imm);
502*0b57cec5SDimitry Andric 
50304eeddc0SDimitry Andric   Register Right = createResultReg(&WebAssembly::I32RegClass);
504bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
505*0b57cec5SDimitry Andric           TII.get(WebAssembly::SHR_S_I32), Right)
506*0b57cec5SDimitry Andric       .addReg(Left)
507*0b57cec5SDimitry Andric       .addReg(Imm);
508*0b57cec5SDimitry Andric 
509*0b57cec5SDimitry Andric   return Right;
510*0b57cec5SDimitry Andric }
511*0b57cec5SDimitry Andric 
512*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::zeroExtend(unsigned Reg, const Value *V,
513*0b57cec5SDimitry Andric                                          MVT::SimpleValueType From,
514*0b57cec5SDimitry Andric                                          MVT::SimpleValueType To) {
515*0b57cec5SDimitry Andric   if (To == MVT::i64) {
516*0b57cec5SDimitry Andric     if (From == MVT::i64)
517*0b57cec5SDimitry Andric       return copyValue(Reg);
518*0b57cec5SDimitry Andric 
519*0b57cec5SDimitry Andric     Reg = zeroExtendToI32(Reg, V, From);
520*0b57cec5SDimitry Andric 
52104eeddc0SDimitry Andric     Register Result = createResultReg(&WebAssembly::I64RegClass);
522bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
523*0b57cec5SDimitry Andric             TII.get(WebAssembly::I64_EXTEND_U_I32), Result)
524*0b57cec5SDimitry Andric         .addReg(Reg);
525*0b57cec5SDimitry Andric     return Result;
526*0b57cec5SDimitry Andric   }
527*0b57cec5SDimitry Andric 
528*0b57cec5SDimitry Andric   if (To == MVT::i32)
529*0b57cec5SDimitry Andric     return zeroExtendToI32(Reg, V, From);
530*0b57cec5SDimitry Andric 
531*0b57cec5SDimitry Andric   return 0;
532*0b57cec5SDimitry Andric }
533*0b57cec5SDimitry Andric 
534*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::signExtend(unsigned Reg, const Value *V,
535*0b57cec5SDimitry Andric                                          MVT::SimpleValueType From,
536*0b57cec5SDimitry Andric                                          MVT::SimpleValueType To) {
537*0b57cec5SDimitry Andric   if (To == MVT::i64) {
538*0b57cec5SDimitry Andric     if (From == MVT::i64)
539*0b57cec5SDimitry Andric       return copyValue(Reg);
540*0b57cec5SDimitry Andric 
541*0b57cec5SDimitry Andric     Reg = signExtendToI32(Reg, V, From);
542*0b57cec5SDimitry Andric 
54304eeddc0SDimitry Andric     Register Result = createResultReg(&WebAssembly::I64RegClass);
544bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
545*0b57cec5SDimitry Andric             TII.get(WebAssembly::I64_EXTEND_S_I32), Result)
546*0b57cec5SDimitry Andric         .addReg(Reg);
547*0b57cec5SDimitry Andric     return Result;
548*0b57cec5SDimitry Andric   }
549*0b57cec5SDimitry Andric 
550*0b57cec5SDimitry Andric   if (To == MVT::i32)
551*0b57cec5SDimitry Andric     return signExtendToI32(Reg, V, From);
552*0b57cec5SDimitry Andric 
553*0b57cec5SDimitry Andric   return 0;
554*0b57cec5SDimitry Andric }
555*0b57cec5SDimitry Andric 
556*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::getRegForUnsignedValue(const Value *V) {
557*0b57cec5SDimitry Andric   MVT::SimpleValueType From = getSimpleType(V->getType());
558*0b57cec5SDimitry Andric   MVT::SimpleValueType To = getLegalType(From);
55904eeddc0SDimitry Andric   Register VReg = getRegForValue(V);
560*0b57cec5SDimitry Andric   if (VReg == 0)
561*0b57cec5SDimitry Andric     return 0;
562*0b57cec5SDimitry Andric   return zeroExtend(VReg, V, From, To);
563*0b57cec5SDimitry Andric }
564*0b57cec5SDimitry Andric 
565*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::getRegForSignedValue(const Value *V) {
566*0b57cec5SDimitry Andric   MVT::SimpleValueType From = getSimpleType(V->getType());
567*0b57cec5SDimitry Andric   MVT::SimpleValueType To = getLegalType(From);
56804eeddc0SDimitry Andric   Register VReg = getRegForValue(V);
569*0b57cec5SDimitry Andric   if (VReg == 0)
570*0b57cec5SDimitry Andric     return 0;
571*0b57cec5SDimitry Andric   return signExtend(VReg, V, From, To);
572*0b57cec5SDimitry Andric }
573*0b57cec5SDimitry Andric 
574*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::getRegForPromotedValue(const Value *V,
575*0b57cec5SDimitry Andric                                                      bool IsSigned) {
576*0b57cec5SDimitry Andric   return IsSigned ? getRegForSignedValue(V) : getRegForUnsignedValue(V);
577*0b57cec5SDimitry Andric }
578*0b57cec5SDimitry Andric 
579*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::notValue(unsigned Reg) {
580*0b57cec5SDimitry Andric   assert(MRI.getRegClass(Reg) == &WebAssembly::I32RegClass);
581*0b57cec5SDimitry Andric 
58204eeddc0SDimitry Andric   Register NotReg = createResultReg(&WebAssembly::I32RegClass);
583bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
584*0b57cec5SDimitry Andric           TII.get(WebAssembly::EQZ_I32), NotReg)
585*0b57cec5SDimitry Andric       .addReg(Reg);
586*0b57cec5SDimitry Andric   return NotReg;
587*0b57cec5SDimitry Andric }
588*0b57cec5SDimitry Andric 
589*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::copyValue(unsigned Reg) {
59004eeddc0SDimitry Andric   Register ResultReg = createResultReg(MRI.getRegClass(Reg));
591bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(WebAssembly::COPY),
592*0b57cec5SDimitry Andric           ResultReg)
593*0b57cec5SDimitry Andric       .addReg(Reg);
594*0b57cec5SDimitry Andric   return ResultReg;
595*0b57cec5SDimitry Andric }
596*0b57cec5SDimitry Andric 
597*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
598*0b57cec5SDimitry Andric   DenseMap<const AllocaInst *, int>::iterator SI =
599*0b57cec5SDimitry Andric       FuncInfo.StaticAllocaMap.find(AI);
600*0b57cec5SDimitry Andric 
601*0b57cec5SDimitry Andric   if (SI != FuncInfo.StaticAllocaMap.end()) {
60204eeddc0SDimitry Andric     Register ResultReg =
603*0b57cec5SDimitry Andric         createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
604*0b57cec5SDimitry Andric                                                : &WebAssembly::I32RegClass);
605*0b57cec5SDimitry Andric     unsigned Opc =
606*0b57cec5SDimitry Andric         Subtarget->hasAddr64() ? WebAssembly::COPY_I64 : WebAssembly::COPY_I32;
607bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
608*0b57cec5SDimitry Andric         .addFrameIndex(SI->second);
609*0b57cec5SDimitry Andric     return ResultReg;
610*0b57cec5SDimitry Andric   }
611*0b57cec5SDimitry Andric 
612*0b57cec5SDimitry Andric   return 0;
613*0b57cec5SDimitry Andric }
614*0b57cec5SDimitry Andric 
615*0b57cec5SDimitry Andric unsigned WebAssemblyFastISel::fastMaterializeConstant(const Constant *C) {
616*0b57cec5SDimitry Andric   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
617*0b57cec5SDimitry Andric     if (TLI.isPositionIndependent())
618*0b57cec5SDimitry Andric       return 0;
619*0b57cec5SDimitry Andric     if (GV->isThreadLocal())
620*0b57cec5SDimitry Andric       return 0;
62104eeddc0SDimitry Andric     Register ResultReg =
622*0b57cec5SDimitry Andric         createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
623*0b57cec5SDimitry Andric                                                : &WebAssembly::I32RegClass);
624*0b57cec5SDimitry Andric     unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
625*0b57cec5SDimitry Andric                                           : WebAssembly::CONST_I32;
626bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
627*0b57cec5SDimitry Andric         .addGlobalAddress(GV);
628*0b57cec5SDimitry Andric     return ResultReg;
629*0b57cec5SDimitry Andric   }
630*0b57cec5SDimitry Andric 
631*0b57cec5SDimitry Andric   // Let target-independent code handle it.
632*0b57cec5SDimitry Andric   return 0;
633*0b57cec5SDimitry Andric }
634*0b57cec5SDimitry Andric 
635*0b57cec5SDimitry Andric bool WebAssemblyFastISel::fastLowerArguments() {
636*0b57cec5SDimitry Andric   if (!FuncInfo.CanLowerReturn)
637*0b57cec5SDimitry Andric     return false;
638*0b57cec5SDimitry Andric 
639*0b57cec5SDimitry Andric   const Function *F = FuncInfo.Fn;
640*0b57cec5SDimitry Andric   if (F->isVarArg())
641*0b57cec5SDimitry Andric     return false;
642*0b57cec5SDimitry Andric 
6435ffd83dbSDimitry Andric   if (FuncInfo.Fn->getCallingConv() == CallingConv::Swift)
6445ffd83dbSDimitry Andric     return false;
6455ffd83dbSDimitry Andric 
646*0b57cec5SDimitry Andric   unsigned I = 0;
647*0b57cec5SDimitry Andric   for (auto const &Arg : F->args()) {
648*0b57cec5SDimitry Andric     const AttributeList &Attrs = F->getAttributes();
649349cc55cSDimitry Andric     if (Attrs.hasParamAttr(I, Attribute::ByVal) ||
650349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::SwiftSelf) ||
651349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::SwiftError) ||
652349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::InAlloca) ||
653349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::Nest))
654*0b57cec5SDimitry Andric       return false;
655*0b57cec5SDimitry Andric 
656*0b57cec5SDimitry Andric     Type *ArgTy = Arg.getType();
657*0b57cec5SDimitry Andric     if (ArgTy->isStructTy() || ArgTy->isArrayTy())
658*0b57cec5SDimitry Andric       return false;
659*0b57cec5SDimitry Andric     if (!Subtarget->hasSIMD128() && ArgTy->isVectorTy())
660*0b57cec5SDimitry Andric       return false;
661*0b57cec5SDimitry Andric 
662*0b57cec5SDimitry Andric     unsigned Opc;
663*0b57cec5SDimitry Andric     const TargetRegisterClass *RC;
664*0b57cec5SDimitry Andric     switch (getSimpleType(ArgTy)) {
665*0b57cec5SDimitry Andric     case MVT::i1:
666*0b57cec5SDimitry Andric     case MVT::i8:
667*0b57cec5SDimitry Andric     case MVT::i16:
668*0b57cec5SDimitry Andric     case MVT::i32:
669*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_i32;
670*0b57cec5SDimitry Andric       RC = &WebAssembly::I32RegClass;
671*0b57cec5SDimitry Andric       break;
672*0b57cec5SDimitry Andric     case MVT::i64:
673*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_i64;
674*0b57cec5SDimitry Andric       RC = &WebAssembly::I64RegClass;
675*0b57cec5SDimitry Andric       break;
676*0b57cec5SDimitry Andric     case MVT::f32:
677*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_f32;
678*0b57cec5SDimitry Andric       RC = &WebAssembly::F32RegClass;
679*0b57cec5SDimitry Andric       break;
680*0b57cec5SDimitry Andric     case MVT::f64:
681*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_f64;
682*0b57cec5SDimitry Andric       RC = &WebAssembly::F64RegClass;
683*0b57cec5SDimitry Andric       break;
684*0b57cec5SDimitry Andric     case MVT::v16i8:
685*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_v16i8;
686*0b57cec5SDimitry Andric       RC = &WebAssembly::V128RegClass;
687*0b57cec5SDimitry Andric       break;
688*0b57cec5SDimitry Andric     case MVT::v8i16:
689*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_v8i16;
690*0b57cec5SDimitry Andric       RC = &WebAssembly::V128RegClass;
691*0b57cec5SDimitry Andric       break;
692*0b57cec5SDimitry Andric     case MVT::v4i32:
693*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_v4i32;
694*0b57cec5SDimitry Andric       RC = &WebAssembly::V128RegClass;
695*0b57cec5SDimitry Andric       break;
696*0b57cec5SDimitry Andric     case MVT::v2i64:
697*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_v2i64;
698*0b57cec5SDimitry Andric       RC = &WebAssembly::V128RegClass;
699*0b57cec5SDimitry Andric       break;
700*0b57cec5SDimitry Andric     case MVT::v4f32:
701*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_v4f32;
702*0b57cec5SDimitry Andric       RC = &WebAssembly::V128RegClass;
703*0b57cec5SDimitry Andric       break;
704*0b57cec5SDimitry Andric     case MVT::v2f64:
705*0b57cec5SDimitry Andric       Opc = WebAssembly::ARGUMENT_v2f64;
706*0b57cec5SDimitry Andric       RC = &WebAssembly::V128RegClass;
707*0b57cec5SDimitry Andric       break;
708e8d8bef9SDimitry Andric     case MVT::funcref:
709e8d8bef9SDimitry Andric       Opc = WebAssembly::ARGUMENT_funcref;
710e8d8bef9SDimitry Andric       RC = &WebAssembly::FUNCREFRegClass;
711e8d8bef9SDimitry Andric       break;
712e8d8bef9SDimitry Andric     case MVT::externref:
713e8d8bef9SDimitry Andric       Opc = WebAssembly::ARGUMENT_externref;
714e8d8bef9SDimitry Andric       RC = &WebAssembly::EXTERNREFRegClass;
715*0b57cec5SDimitry Andric       break;
716*0b57cec5SDimitry Andric     default:
717*0b57cec5SDimitry Andric       return false;
718*0b57cec5SDimitry Andric     }
71904eeddc0SDimitry Andric     Register ResultReg = createResultReg(RC);
720bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
721*0b57cec5SDimitry Andric         .addImm(I);
722*0b57cec5SDimitry Andric     updateValueMap(&Arg, ResultReg);
723*0b57cec5SDimitry Andric 
724*0b57cec5SDimitry Andric     ++I;
725*0b57cec5SDimitry Andric   }
726*0b57cec5SDimitry Andric 
727*0b57cec5SDimitry Andric   MRI.addLiveIn(WebAssembly::ARGUMENTS);
728*0b57cec5SDimitry Andric 
729*0b57cec5SDimitry Andric   auto *MFI = MF->getInfo<WebAssemblyFunctionInfo>();
730*0b57cec5SDimitry Andric   for (auto const &Arg : F->args()) {
731*0b57cec5SDimitry Andric     MVT::SimpleValueType ArgTy = getLegalType(getSimpleType(Arg.getType()));
732*0b57cec5SDimitry Andric     if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
733*0b57cec5SDimitry Andric       MFI->clearParamsAndResults();
734*0b57cec5SDimitry Andric       return false;
735*0b57cec5SDimitry Andric     }
736*0b57cec5SDimitry Andric     MFI->addParam(ArgTy);
737*0b57cec5SDimitry Andric   }
738*0b57cec5SDimitry Andric 
739*0b57cec5SDimitry Andric   if (!F->getReturnType()->isVoidTy()) {
740*0b57cec5SDimitry Andric     MVT::SimpleValueType RetTy =
741*0b57cec5SDimitry Andric         getLegalType(getSimpleType(F->getReturnType()));
742*0b57cec5SDimitry Andric     if (RetTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
743*0b57cec5SDimitry Andric       MFI->clearParamsAndResults();
744*0b57cec5SDimitry Andric       return false;
745*0b57cec5SDimitry Andric     }
746*0b57cec5SDimitry Andric     MFI->addResult(RetTy);
747*0b57cec5SDimitry Andric   }
748*0b57cec5SDimitry Andric 
749*0b57cec5SDimitry Andric   return true;
750*0b57cec5SDimitry Andric }
751*0b57cec5SDimitry Andric 
752*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectCall(const Instruction *I) {
753*0b57cec5SDimitry Andric   const auto *Call = cast<CallInst>(I);
754*0b57cec5SDimitry Andric 
755*0b57cec5SDimitry Andric   // TODO: Support tail calls in FastISel
756*0b57cec5SDimitry Andric   if (Call->isMustTailCall() || Call->isInlineAsm() ||
757*0b57cec5SDimitry Andric       Call->getFunctionType()->isVarArg())
758*0b57cec5SDimitry Andric     return false;
759*0b57cec5SDimitry Andric 
760*0b57cec5SDimitry Andric   Function *Func = Call->getCalledFunction();
761*0b57cec5SDimitry Andric   if (Func && Func->isIntrinsic())
762*0b57cec5SDimitry Andric     return false;
763*0b57cec5SDimitry Andric 
7645ffd83dbSDimitry Andric   if (Call->getCallingConv() == CallingConv::Swift)
7655ffd83dbSDimitry Andric     return false;
7665ffd83dbSDimitry Andric 
767*0b57cec5SDimitry Andric   bool IsDirect = Func != nullptr;
7685ffd83dbSDimitry Andric   if (!IsDirect && isa<ConstantExpr>(Call->getCalledOperand()))
769*0b57cec5SDimitry Andric     return false;
770*0b57cec5SDimitry Andric 
771*0b57cec5SDimitry Andric   FunctionType *FuncTy = Call->getFunctionType();
7725ffd83dbSDimitry Andric   unsigned Opc = IsDirect ? WebAssembly::CALL : WebAssembly::CALL_INDIRECT;
773*0b57cec5SDimitry Andric   bool IsVoid = FuncTy->getReturnType()->isVoidTy();
774*0b57cec5SDimitry Andric   unsigned ResultReg;
7755ffd83dbSDimitry Andric   if (!IsVoid) {
776*0b57cec5SDimitry Andric     if (!Subtarget->hasSIMD128() && Call->getType()->isVectorTy())
777*0b57cec5SDimitry Andric       return false;
778*0b57cec5SDimitry Andric 
779*0b57cec5SDimitry Andric     MVT::SimpleValueType RetTy = getSimpleType(Call->getType());
780*0b57cec5SDimitry Andric     switch (RetTy) {
781*0b57cec5SDimitry Andric     case MVT::i1:
782*0b57cec5SDimitry Andric     case MVT::i8:
783*0b57cec5SDimitry Andric     case MVT::i16:
784*0b57cec5SDimitry Andric     case MVT::i32:
785*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::I32RegClass);
786*0b57cec5SDimitry Andric       break;
787*0b57cec5SDimitry Andric     case MVT::i64:
788*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::I64RegClass);
789*0b57cec5SDimitry Andric       break;
790*0b57cec5SDimitry Andric     case MVT::f32:
791*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::F32RegClass);
792*0b57cec5SDimitry Andric       break;
793*0b57cec5SDimitry Andric     case MVT::f64:
794*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::F64RegClass);
795*0b57cec5SDimitry Andric       break;
796*0b57cec5SDimitry Andric     case MVT::v16i8:
797*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::V128RegClass);
798*0b57cec5SDimitry Andric       break;
799*0b57cec5SDimitry Andric     case MVT::v8i16:
800*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::V128RegClass);
801*0b57cec5SDimitry Andric       break;
802*0b57cec5SDimitry Andric     case MVT::v4i32:
803*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::V128RegClass);
804*0b57cec5SDimitry Andric       break;
805*0b57cec5SDimitry Andric     case MVT::v2i64:
806*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::V128RegClass);
807*0b57cec5SDimitry Andric       break;
808*0b57cec5SDimitry Andric     case MVT::v4f32:
809*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::V128RegClass);
810*0b57cec5SDimitry Andric       break;
811*0b57cec5SDimitry Andric     case MVT::v2f64:
812*0b57cec5SDimitry Andric       ResultReg = createResultReg(&WebAssembly::V128RegClass);
813*0b57cec5SDimitry Andric       break;
814e8d8bef9SDimitry Andric     case MVT::funcref:
815e8d8bef9SDimitry Andric       ResultReg = createResultReg(&WebAssembly::FUNCREFRegClass);
816e8d8bef9SDimitry Andric       break;
817e8d8bef9SDimitry Andric     case MVT::externref:
818e8d8bef9SDimitry Andric       ResultReg = createResultReg(&WebAssembly::EXTERNREFRegClass);
819*0b57cec5SDimitry Andric       break;
820*0b57cec5SDimitry Andric     default:
821*0b57cec5SDimitry Andric       return false;
822*0b57cec5SDimitry Andric     }
823*0b57cec5SDimitry Andric   }
824*0b57cec5SDimitry Andric 
825*0b57cec5SDimitry Andric   SmallVector<unsigned, 8> Args;
826349cc55cSDimitry Andric   for (unsigned I = 0, E = Call->arg_size(); I < E; ++I) {
827*0b57cec5SDimitry Andric     Value *V = Call->getArgOperand(I);
828*0b57cec5SDimitry Andric     MVT::SimpleValueType ArgTy = getSimpleType(V->getType());
829*0b57cec5SDimitry Andric     if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
830*0b57cec5SDimitry Andric       return false;
831*0b57cec5SDimitry Andric 
832*0b57cec5SDimitry Andric     const AttributeList &Attrs = Call->getAttributes();
833349cc55cSDimitry Andric     if (Attrs.hasParamAttr(I, Attribute::ByVal) ||
834349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::SwiftSelf) ||
835349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::SwiftError) ||
836349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::InAlloca) ||
837349cc55cSDimitry Andric         Attrs.hasParamAttr(I, Attribute::Nest))
838*0b57cec5SDimitry Andric       return false;
839*0b57cec5SDimitry Andric 
840*0b57cec5SDimitry Andric     unsigned Reg;
841*0b57cec5SDimitry Andric 
842349cc55cSDimitry Andric     if (Attrs.hasParamAttr(I, Attribute::SExt))
843*0b57cec5SDimitry Andric       Reg = getRegForSignedValue(V);
844349cc55cSDimitry Andric     else if (Attrs.hasParamAttr(I, Attribute::ZExt))
845*0b57cec5SDimitry Andric       Reg = getRegForUnsignedValue(V);
846*0b57cec5SDimitry Andric     else
847*0b57cec5SDimitry Andric       Reg = getRegForValue(V);
848*0b57cec5SDimitry Andric 
849*0b57cec5SDimitry Andric     if (Reg == 0)
850*0b57cec5SDimitry Andric       return false;
851*0b57cec5SDimitry Andric 
852*0b57cec5SDimitry Andric     Args.push_back(Reg);
853*0b57cec5SDimitry Andric   }
854*0b57cec5SDimitry Andric 
855*0b57cec5SDimitry Andric   unsigned CalleeReg = 0;
856*0b57cec5SDimitry Andric   if (!IsDirect) {
8575ffd83dbSDimitry Andric     CalleeReg = getRegForValue(Call->getCalledOperand());
858*0b57cec5SDimitry Andric     if (!CalleeReg)
859*0b57cec5SDimitry Andric       return false;
860*0b57cec5SDimitry Andric   }
861*0b57cec5SDimitry Andric 
862bdd1243dSDimitry Andric   auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc));
863*0b57cec5SDimitry Andric 
864*0b57cec5SDimitry Andric   if (!IsVoid)
865*0b57cec5SDimitry Andric     MIB.addReg(ResultReg, RegState::Define);
866*0b57cec5SDimitry Andric 
8675ffd83dbSDimitry Andric   if (IsDirect) {
868*0b57cec5SDimitry Andric     MIB.addGlobalAddress(Func);
8695ffd83dbSDimitry Andric   } else {
870fe6060f1SDimitry Andric     // Placeholder for the type index.
8715ffd83dbSDimitry Andric     MIB.addImm(0);
872fe6060f1SDimitry Andric     // The table into which this call_indirect indexes.
873fe6060f1SDimitry Andric     MCSymbolWasm *Table = WebAssembly::getOrCreateFunctionTableSymbol(
874fe6060f1SDimitry Andric         MF->getMMI().getContext(), Subtarget);
875fe6060f1SDimitry Andric     if (Subtarget->hasReferenceTypes()) {
876fe6060f1SDimitry Andric       MIB.addSym(Table);
877fe6060f1SDimitry Andric     } else {
878fe6060f1SDimitry Andric       // Otherwise for the MVP there is at most one table whose number is 0, but
879fe6060f1SDimitry Andric       // we can't write a table symbol or issue relocations.  Instead we just
880fe6060f1SDimitry Andric       // ensure the table is live.
881fe6060f1SDimitry Andric       Table->setNoStrip();
8825ffd83dbSDimitry Andric       MIB.addImm(0);
883fe6060f1SDimitry Andric     }
884fe6060f1SDimitry Andric     // See if we must truncate the function pointer.
885fe6060f1SDimitry Andric     // CALL_INDIRECT takes an i32, but in wasm64 we represent function pointers
886fe6060f1SDimitry Andric     // as 64-bit for uniformity with other pointer types.
887fe6060f1SDimitry Andric     // See also: WebAssemblyISelLowering.cpp: LowerCallResults
888fe6060f1SDimitry Andric     if (Subtarget->hasAddr64()) {
889bdd1243dSDimitry Andric       auto Wrap = BuildMI(*FuncInfo.MBB, std::prev(FuncInfo.InsertPt), MIMD,
890fe6060f1SDimitry Andric                           TII.get(WebAssembly::I32_WRAP_I64));
89104eeddc0SDimitry Andric       Register Reg32 = createResultReg(&WebAssembly::I32RegClass);
892fe6060f1SDimitry Andric       Wrap.addReg(Reg32, RegState::Define);
893fe6060f1SDimitry Andric       Wrap.addReg(CalleeReg);
894fe6060f1SDimitry Andric       CalleeReg = Reg32;
895fe6060f1SDimitry Andric     }
8965ffd83dbSDimitry Andric   }
897*0b57cec5SDimitry Andric 
898*0b57cec5SDimitry Andric   for (unsigned ArgReg : Args)
899*0b57cec5SDimitry Andric     MIB.addReg(ArgReg);
900*0b57cec5SDimitry Andric 
9015ffd83dbSDimitry Andric   if (!IsDirect)
9025ffd83dbSDimitry Andric     MIB.addReg(CalleeReg);
9035ffd83dbSDimitry Andric 
904*0b57cec5SDimitry Andric   if (!IsVoid)
905*0b57cec5SDimitry Andric     updateValueMap(Call, ResultReg);
906*0b57cec5SDimitry Andric   return true;
907*0b57cec5SDimitry Andric }
908*0b57cec5SDimitry Andric 
909*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
910*0b57cec5SDimitry Andric   const auto *Select = cast<SelectInst>(I);
911*0b57cec5SDimitry Andric 
912*0b57cec5SDimitry Andric   bool Not;
91369ade1e0SDimitry Andric   unsigned CondReg =
91469ade1e0SDimitry Andric       getRegForI1Value(Select->getCondition(), I->getParent(), Not);
915*0b57cec5SDimitry Andric   if (CondReg == 0)
916*0b57cec5SDimitry Andric     return false;
917*0b57cec5SDimitry Andric 
91804eeddc0SDimitry Andric   Register TrueReg = getRegForValue(Select->getTrueValue());
919*0b57cec5SDimitry Andric   if (TrueReg == 0)
920*0b57cec5SDimitry Andric     return false;
921*0b57cec5SDimitry Andric 
92204eeddc0SDimitry Andric   Register FalseReg = getRegForValue(Select->getFalseValue());
923*0b57cec5SDimitry Andric   if (FalseReg == 0)
924*0b57cec5SDimitry Andric     return false;
925*0b57cec5SDimitry Andric 
926*0b57cec5SDimitry Andric   if (Not)
927*0b57cec5SDimitry Andric     std::swap(TrueReg, FalseReg);
928*0b57cec5SDimitry Andric 
929*0b57cec5SDimitry Andric   unsigned Opc;
930*0b57cec5SDimitry Andric   const TargetRegisterClass *RC;
931*0b57cec5SDimitry Andric   switch (getSimpleType(Select->getType())) {
932*0b57cec5SDimitry Andric   case MVT::i1:
933*0b57cec5SDimitry Andric   case MVT::i8:
934*0b57cec5SDimitry Andric   case MVT::i16:
935*0b57cec5SDimitry Andric   case MVT::i32:
936*0b57cec5SDimitry Andric     Opc = WebAssembly::SELECT_I32;
937*0b57cec5SDimitry Andric     RC = &WebAssembly::I32RegClass;
938*0b57cec5SDimitry Andric     break;
939*0b57cec5SDimitry Andric   case MVT::i64:
940*0b57cec5SDimitry Andric     Opc = WebAssembly::SELECT_I64;
941*0b57cec5SDimitry Andric     RC = &WebAssembly::I64RegClass;
942*0b57cec5SDimitry Andric     break;
943*0b57cec5SDimitry Andric   case MVT::f32:
944*0b57cec5SDimitry Andric     Opc = WebAssembly::SELECT_F32;
945*0b57cec5SDimitry Andric     RC = &WebAssembly::F32RegClass;
946*0b57cec5SDimitry Andric     break;
947*0b57cec5SDimitry Andric   case MVT::f64:
948*0b57cec5SDimitry Andric     Opc = WebAssembly::SELECT_F64;
949*0b57cec5SDimitry Andric     RC = &WebAssembly::F64RegClass;
950*0b57cec5SDimitry Andric     break;
951e8d8bef9SDimitry Andric   case MVT::funcref:
952e8d8bef9SDimitry Andric     Opc = WebAssembly::SELECT_FUNCREF;
953e8d8bef9SDimitry Andric     RC = &WebAssembly::FUNCREFRegClass;
954e8d8bef9SDimitry Andric     break;
955e8d8bef9SDimitry Andric   case MVT::externref:
956e8d8bef9SDimitry Andric     Opc = WebAssembly::SELECT_EXTERNREF;
957e8d8bef9SDimitry Andric     RC = &WebAssembly::EXTERNREFRegClass;
958*0b57cec5SDimitry Andric     break;
959*0b57cec5SDimitry Andric   default:
960*0b57cec5SDimitry Andric     return false;
961*0b57cec5SDimitry Andric   }
962*0b57cec5SDimitry Andric 
96304eeddc0SDimitry Andric   Register ResultReg = createResultReg(RC);
964bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
965*0b57cec5SDimitry Andric       .addReg(TrueReg)
966*0b57cec5SDimitry Andric       .addReg(FalseReg)
967*0b57cec5SDimitry Andric       .addReg(CondReg);
968*0b57cec5SDimitry Andric 
969*0b57cec5SDimitry Andric   updateValueMap(Select, ResultReg);
970*0b57cec5SDimitry Andric   return true;
971*0b57cec5SDimitry Andric }
972*0b57cec5SDimitry Andric 
973*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
974*0b57cec5SDimitry Andric   const auto *Trunc = cast<TruncInst>(I);
975*0b57cec5SDimitry Andric 
97604eeddc0SDimitry Andric   Register Reg = getRegForValue(Trunc->getOperand(0));
977*0b57cec5SDimitry Andric   if (Reg == 0)
978*0b57cec5SDimitry Andric     return false;
979*0b57cec5SDimitry Andric 
980*0b57cec5SDimitry Andric   if (Trunc->getOperand(0)->getType()->isIntegerTy(64)) {
98104eeddc0SDimitry Andric     Register Result = createResultReg(&WebAssembly::I32RegClass);
982bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
983*0b57cec5SDimitry Andric             TII.get(WebAssembly::I32_WRAP_I64), Result)
984*0b57cec5SDimitry Andric         .addReg(Reg);
985*0b57cec5SDimitry Andric     Reg = Result;
986*0b57cec5SDimitry Andric   }
987*0b57cec5SDimitry Andric 
988*0b57cec5SDimitry Andric   updateValueMap(Trunc, Reg);
989*0b57cec5SDimitry Andric   return true;
990*0b57cec5SDimitry Andric }
991*0b57cec5SDimitry Andric 
992*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectZExt(const Instruction *I) {
993*0b57cec5SDimitry Andric   const auto *ZExt = cast<ZExtInst>(I);
994*0b57cec5SDimitry Andric 
995*0b57cec5SDimitry Andric   const Value *Op = ZExt->getOperand(0);
996*0b57cec5SDimitry Andric   MVT::SimpleValueType From = getSimpleType(Op->getType());
997*0b57cec5SDimitry Andric   MVT::SimpleValueType To = getLegalType(getSimpleType(ZExt->getType()));
99804eeddc0SDimitry Andric   Register In = getRegForValue(Op);
999*0b57cec5SDimitry Andric   if (In == 0)
1000*0b57cec5SDimitry Andric     return false;
1001*0b57cec5SDimitry Andric   unsigned Reg = zeroExtend(In, Op, From, To);
1002*0b57cec5SDimitry Andric   if (Reg == 0)
1003*0b57cec5SDimitry Andric     return false;
1004*0b57cec5SDimitry Andric 
1005*0b57cec5SDimitry Andric   updateValueMap(ZExt, Reg);
1006*0b57cec5SDimitry Andric   return true;
1007*0b57cec5SDimitry Andric }
1008*0b57cec5SDimitry Andric 
1009*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectSExt(const Instruction *I) {
1010*0b57cec5SDimitry Andric   const auto *SExt = cast<SExtInst>(I);
1011*0b57cec5SDimitry Andric 
1012*0b57cec5SDimitry Andric   const Value *Op = SExt->getOperand(0);
1013*0b57cec5SDimitry Andric   MVT::SimpleValueType From = getSimpleType(Op->getType());
1014*0b57cec5SDimitry Andric   MVT::SimpleValueType To = getLegalType(getSimpleType(SExt->getType()));
101504eeddc0SDimitry Andric   Register In = getRegForValue(Op);
1016*0b57cec5SDimitry Andric   if (In == 0)
1017*0b57cec5SDimitry Andric     return false;
1018*0b57cec5SDimitry Andric   unsigned Reg = signExtend(In, Op, From, To);
1019*0b57cec5SDimitry Andric   if (Reg == 0)
1020*0b57cec5SDimitry Andric     return false;
1021*0b57cec5SDimitry Andric 
1022*0b57cec5SDimitry Andric   updateValueMap(SExt, Reg);
1023*0b57cec5SDimitry Andric   return true;
1024*0b57cec5SDimitry Andric }
1025*0b57cec5SDimitry Andric 
1026*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectICmp(const Instruction *I) {
1027*0b57cec5SDimitry Andric   const auto *ICmp = cast<ICmpInst>(I);
1028*0b57cec5SDimitry Andric 
1029*0b57cec5SDimitry Andric   bool I32 = getSimpleType(ICmp->getOperand(0)->getType()) != MVT::i64;
1030*0b57cec5SDimitry Andric   unsigned Opc;
1031*0b57cec5SDimitry Andric   bool IsSigned = false;
1032*0b57cec5SDimitry Andric   switch (ICmp->getPredicate()) {
1033*0b57cec5SDimitry Andric   case ICmpInst::ICMP_EQ:
1034*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::EQ_I32 : WebAssembly::EQ_I64;
1035*0b57cec5SDimitry Andric     break;
1036*0b57cec5SDimitry Andric   case ICmpInst::ICMP_NE:
1037*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::NE_I32 : WebAssembly::NE_I64;
1038*0b57cec5SDimitry Andric     break;
1039*0b57cec5SDimitry Andric   case ICmpInst::ICMP_UGT:
1040*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::GT_U_I32 : WebAssembly::GT_U_I64;
1041*0b57cec5SDimitry Andric     break;
1042*0b57cec5SDimitry Andric   case ICmpInst::ICMP_UGE:
1043*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::GE_U_I32 : WebAssembly::GE_U_I64;
1044*0b57cec5SDimitry Andric     break;
1045*0b57cec5SDimitry Andric   case ICmpInst::ICMP_ULT:
1046*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::LT_U_I32 : WebAssembly::LT_U_I64;
1047*0b57cec5SDimitry Andric     break;
1048*0b57cec5SDimitry Andric   case ICmpInst::ICMP_ULE:
1049*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::LE_U_I32 : WebAssembly::LE_U_I64;
1050*0b57cec5SDimitry Andric     break;
1051*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SGT:
1052*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::GT_S_I32 : WebAssembly::GT_S_I64;
1053*0b57cec5SDimitry Andric     IsSigned = true;
1054*0b57cec5SDimitry Andric     break;
1055*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SGE:
1056*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::GE_S_I32 : WebAssembly::GE_S_I64;
1057*0b57cec5SDimitry Andric     IsSigned = true;
1058*0b57cec5SDimitry Andric     break;
1059*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SLT:
1060*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::LT_S_I32 : WebAssembly::LT_S_I64;
1061*0b57cec5SDimitry Andric     IsSigned = true;
1062*0b57cec5SDimitry Andric     break;
1063*0b57cec5SDimitry Andric   case ICmpInst::ICMP_SLE:
1064*0b57cec5SDimitry Andric     Opc = I32 ? WebAssembly::LE_S_I32 : WebAssembly::LE_S_I64;
1065*0b57cec5SDimitry Andric     IsSigned = true;
1066*0b57cec5SDimitry Andric     break;
1067*0b57cec5SDimitry Andric   default:
1068*0b57cec5SDimitry Andric     return false;
1069*0b57cec5SDimitry Andric   }
1070*0b57cec5SDimitry Andric 
1071*0b57cec5SDimitry Andric   unsigned LHS = getRegForPromotedValue(ICmp->getOperand(0), IsSigned);
1072*0b57cec5SDimitry Andric   if (LHS == 0)
1073*0b57cec5SDimitry Andric     return false;
1074*0b57cec5SDimitry Andric 
1075*0b57cec5SDimitry Andric   unsigned RHS = getRegForPromotedValue(ICmp->getOperand(1), IsSigned);
1076*0b57cec5SDimitry Andric   if (RHS == 0)
1077*0b57cec5SDimitry Andric     return false;
1078*0b57cec5SDimitry Andric 
107904eeddc0SDimitry Andric   Register ResultReg = createResultReg(&WebAssembly::I32RegClass);
1080bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
1081*0b57cec5SDimitry Andric       .addReg(LHS)
1082*0b57cec5SDimitry Andric       .addReg(RHS);
1083*0b57cec5SDimitry Andric   updateValueMap(ICmp, ResultReg);
1084*0b57cec5SDimitry Andric   return true;
1085*0b57cec5SDimitry Andric }
1086*0b57cec5SDimitry Andric 
1087*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectFCmp(const Instruction *I) {
1088*0b57cec5SDimitry Andric   const auto *FCmp = cast<FCmpInst>(I);
1089*0b57cec5SDimitry Andric 
109004eeddc0SDimitry Andric   Register LHS = getRegForValue(FCmp->getOperand(0));
1091*0b57cec5SDimitry Andric   if (LHS == 0)
1092*0b57cec5SDimitry Andric     return false;
1093*0b57cec5SDimitry Andric 
109404eeddc0SDimitry Andric   Register RHS = getRegForValue(FCmp->getOperand(1));
1095*0b57cec5SDimitry Andric   if (RHS == 0)
1096*0b57cec5SDimitry Andric     return false;
1097*0b57cec5SDimitry Andric 
1098*0b57cec5SDimitry Andric   bool F32 = getSimpleType(FCmp->getOperand(0)->getType()) != MVT::f64;
1099*0b57cec5SDimitry Andric   unsigned Opc;
1100*0b57cec5SDimitry Andric   bool Not = false;
1101*0b57cec5SDimitry Andric   switch (FCmp->getPredicate()) {
1102*0b57cec5SDimitry Andric   case FCmpInst::FCMP_OEQ:
1103*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::EQ_F32 : WebAssembly::EQ_F64;
1104*0b57cec5SDimitry Andric     break;
1105*0b57cec5SDimitry Andric   case FCmpInst::FCMP_UNE:
1106*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::NE_F32 : WebAssembly::NE_F64;
1107*0b57cec5SDimitry Andric     break;
1108*0b57cec5SDimitry Andric   case FCmpInst::FCMP_OGT:
1109*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1110*0b57cec5SDimitry Andric     break;
1111*0b57cec5SDimitry Andric   case FCmpInst::FCMP_OGE:
1112*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1113*0b57cec5SDimitry Andric     break;
1114*0b57cec5SDimitry Andric   case FCmpInst::FCMP_OLT:
1115*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1116*0b57cec5SDimitry Andric     break;
1117*0b57cec5SDimitry Andric   case FCmpInst::FCMP_OLE:
1118*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1119*0b57cec5SDimitry Andric     break;
1120*0b57cec5SDimitry Andric   case FCmpInst::FCMP_UGT:
1121*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1122*0b57cec5SDimitry Andric     Not = true;
1123*0b57cec5SDimitry Andric     break;
1124*0b57cec5SDimitry Andric   case FCmpInst::FCMP_UGE:
1125*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1126*0b57cec5SDimitry Andric     Not = true;
1127*0b57cec5SDimitry Andric     break;
1128*0b57cec5SDimitry Andric   case FCmpInst::FCMP_ULT:
1129*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1130*0b57cec5SDimitry Andric     Not = true;
1131*0b57cec5SDimitry Andric     break;
1132*0b57cec5SDimitry Andric   case FCmpInst::FCMP_ULE:
1133*0b57cec5SDimitry Andric     Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1134*0b57cec5SDimitry Andric     Not = true;
1135*0b57cec5SDimitry Andric     break;
1136*0b57cec5SDimitry Andric   default:
1137*0b57cec5SDimitry Andric     return false;
1138*0b57cec5SDimitry Andric   }
1139*0b57cec5SDimitry Andric 
114004eeddc0SDimitry Andric   Register ResultReg = createResultReg(&WebAssembly::I32RegClass);
1141bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
1142*0b57cec5SDimitry Andric       .addReg(LHS)
1143*0b57cec5SDimitry Andric       .addReg(RHS);
1144*0b57cec5SDimitry Andric 
1145*0b57cec5SDimitry Andric   if (Not)
1146*0b57cec5SDimitry Andric     ResultReg = notValue(ResultReg);
1147*0b57cec5SDimitry Andric 
1148*0b57cec5SDimitry Andric   updateValueMap(FCmp, ResultReg);
1149*0b57cec5SDimitry Andric   return true;
1150*0b57cec5SDimitry Andric }
1151*0b57cec5SDimitry Andric 
1152*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectBitCast(const Instruction *I) {
1153*0b57cec5SDimitry Andric   // Target-independent code can handle this, except it doesn't set the dead
1154*0b57cec5SDimitry Andric   // flag on the ARGUMENTS clobber, so we have to do that manually in order
1155*0b57cec5SDimitry Andric   // to satisfy code that expects this of isBitcast() instructions.
1156*0b57cec5SDimitry Andric   EVT VT = TLI.getValueType(DL, I->getOperand(0)->getType());
1157*0b57cec5SDimitry Andric   EVT RetVT = TLI.getValueType(DL, I->getType());
1158*0b57cec5SDimitry Andric   if (!VT.isSimple() || !RetVT.isSimple())
1159*0b57cec5SDimitry Andric     return false;
1160*0b57cec5SDimitry Andric 
116104eeddc0SDimitry Andric   Register In = getRegForValue(I->getOperand(0));
1162*0b57cec5SDimitry Andric   if (In == 0)
1163*0b57cec5SDimitry Andric     return false;
1164*0b57cec5SDimitry Andric 
1165*0b57cec5SDimitry Andric   if (VT == RetVT) {
1166*0b57cec5SDimitry Andric     // No-op bitcast.
1167*0b57cec5SDimitry Andric     updateValueMap(I, In);
1168*0b57cec5SDimitry Andric     return true;
1169*0b57cec5SDimitry Andric   }
1170*0b57cec5SDimitry Andric 
11718bcb0991SDimitry Andric   Register Reg = fastEmit_ISD_BITCAST_r(VT.getSimpleVT(), RetVT.getSimpleVT(),
1172fe6060f1SDimitry Andric                                         In);
1173*0b57cec5SDimitry Andric   if (!Reg)
1174*0b57cec5SDimitry Andric     return false;
1175*0b57cec5SDimitry Andric   MachineBasicBlock::iterator Iter = FuncInfo.InsertPt;
1176*0b57cec5SDimitry Andric   --Iter;
1177*0b57cec5SDimitry Andric   assert(Iter->isBitcast());
11788bcb0991SDimitry Andric   Iter->setPhysRegsDeadExcept(ArrayRef<Register>(), TRI);
1179*0b57cec5SDimitry Andric   updateValueMap(I, Reg);
1180*0b57cec5SDimitry Andric   return true;
1181*0b57cec5SDimitry Andric }
1182*0b57cec5SDimitry Andric 
1183*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectLoad(const Instruction *I) {
1184*0b57cec5SDimitry Andric   const auto *Load = cast<LoadInst>(I);
1185*0b57cec5SDimitry Andric   if (Load->isAtomic())
1186*0b57cec5SDimitry Andric     return false;
1187fe6060f1SDimitry Andric   if (!WebAssembly::isDefaultAddressSpace(Load->getPointerAddressSpace()))
1188fe6060f1SDimitry Andric     return false;
1189*0b57cec5SDimitry Andric   if (!Subtarget->hasSIMD128() && Load->getType()->isVectorTy())
1190*0b57cec5SDimitry Andric     return false;
1191*0b57cec5SDimitry Andric 
1192*0b57cec5SDimitry Andric   Address Addr;
1193*0b57cec5SDimitry Andric   if (!computeAddress(Load->getPointerOperand(), Addr))
1194*0b57cec5SDimitry Andric     return false;
1195*0b57cec5SDimitry Andric 
1196*0b57cec5SDimitry Andric   // TODO: Fold a following sign-/zero-extend into the load instruction.
1197*0b57cec5SDimitry Andric 
1198*0b57cec5SDimitry Andric   unsigned Opc;
1199*0b57cec5SDimitry Andric   const TargetRegisterClass *RC;
12005ffd83dbSDimitry Andric   bool A64 = Subtarget->hasAddr64();
1201*0b57cec5SDimitry Andric   switch (getSimpleType(Load->getType())) {
1202*0b57cec5SDimitry Andric   case MVT::i1:
1203*0b57cec5SDimitry Andric   case MVT::i8:
12045ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::LOAD8_U_I32_A64 : WebAssembly::LOAD8_U_I32_A32;
1205*0b57cec5SDimitry Andric     RC = &WebAssembly::I32RegClass;
1206*0b57cec5SDimitry Andric     break;
1207*0b57cec5SDimitry Andric   case MVT::i16:
12085ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::LOAD16_U_I32_A64 : WebAssembly::LOAD16_U_I32_A32;
1209*0b57cec5SDimitry Andric     RC = &WebAssembly::I32RegClass;
1210*0b57cec5SDimitry Andric     break;
1211*0b57cec5SDimitry Andric   case MVT::i32:
12125ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::LOAD_I32_A64 : WebAssembly::LOAD_I32_A32;
1213*0b57cec5SDimitry Andric     RC = &WebAssembly::I32RegClass;
1214*0b57cec5SDimitry Andric     break;
1215*0b57cec5SDimitry Andric   case MVT::i64:
12165ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::LOAD_I64_A64 : WebAssembly::LOAD_I64_A32;
1217*0b57cec5SDimitry Andric     RC = &WebAssembly::I64RegClass;
1218*0b57cec5SDimitry Andric     break;
1219*0b57cec5SDimitry Andric   case MVT::f32:
12205ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::LOAD_F32_A64 : WebAssembly::LOAD_F32_A32;
1221*0b57cec5SDimitry Andric     RC = &WebAssembly::F32RegClass;
1222*0b57cec5SDimitry Andric     break;
1223*0b57cec5SDimitry Andric   case MVT::f64:
12245ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::LOAD_F64_A64 : WebAssembly::LOAD_F64_A32;
1225*0b57cec5SDimitry Andric     RC = &WebAssembly::F64RegClass;
1226*0b57cec5SDimitry Andric     break;
1227*0b57cec5SDimitry Andric   default:
1228*0b57cec5SDimitry Andric     return false;
1229*0b57cec5SDimitry Andric   }
1230*0b57cec5SDimitry Andric 
1231*0b57cec5SDimitry Andric   materializeLoadStoreOperands(Addr);
1232*0b57cec5SDimitry Andric 
123304eeddc0SDimitry Andric   Register ResultReg = createResultReg(RC);
1234bdd1243dSDimitry Andric   auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc),
1235*0b57cec5SDimitry Andric                      ResultReg);
1236*0b57cec5SDimitry Andric 
1237*0b57cec5SDimitry Andric   addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Load));
1238*0b57cec5SDimitry Andric 
1239*0b57cec5SDimitry Andric   updateValueMap(Load, ResultReg);
1240*0b57cec5SDimitry Andric   return true;
1241*0b57cec5SDimitry Andric }
1242*0b57cec5SDimitry Andric 
1243*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectStore(const Instruction *I) {
1244*0b57cec5SDimitry Andric   const auto *Store = cast<StoreInst>(I);
1245*0b57cec5SDimitry Andric   if (Store->isAtomic())
1246*0b57cec5SDimitry Andric     return false;
1247fe6060f1SDimitry Andric   if (!WebAssembly::isDefaultAddressSpace(Store->getPointerAddressSpace()))
1248fe6060f1SDimitry Andric     return false;
1249*0b57cec5SDimitry Andric   if (!Subtarget->hasSIMD128() &&
1250*0b57cec5SDimitry Andric       Store->getValueOperand()->getType()->isVectorTy())
1251*0b57cec5SDimitry Andric     return false;
1252*0b57cec5SDimitry Andric 
1253*0b57cec5SDimitry Andric   Address Addr;
1254*0b57cec5SDimitry Andric   if (!computeAddress(Store->getPointerOperand(), Addr))
1255*0b57cec5SDimitry Andric     return false;
1256*0b57cec5SDimitry Andric 
1257*0b57cec5SDimitry Andric   unsigned Opc;
1258*0b57cec5SDimitry Andric   bool VTIsi1 = false;
12595ffd83dbSDimitry Andric   bool A64 = Subtarget->hasAddr64();
1260*0b57cec5SDimitry Andric   switch (getSimpleType(Store->getValueOperand()->getType())) {
1261*0b57cec5SDimitry Andric   case MVT::i1:
1262*0b57cec5SDimitry Andric     VTIsi1 = true;
1263bdd1243dSDimitry Andric     [[fallthrough]];
1264*0b57cec5SDimitry Andric   case MVT::i8:
12655ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::STORE8_I32_A64 : WebAssembly::STORE8_I32_A32;
1266*0b57cec5SDimitry Andric     break;
1267*0b57cec5SDimitry Andric   case MVT::i16:
12685ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::STORE16_I32_A64 : WebAssembly::STORE16_I32_A32;
1269*0b57cec5SDimitry Andric     break;
1270*0b57cec5SDimitry Andric   case MVT::i32:
12715ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::STORE_I32_A64 : WebAssembly::STORE_I32_A32;
1272*0b57cec5SDimitry Andric     break;
1273*0b57cec5SDimitry Andric   case MVT::i64:
12745ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::STORE_I64_A64 : WebAssembly::STORE_I64_A32;
1275*0b57cec5SDimitry Andric     break;
1276*0b57cec5SDimitry Andric   case MVT::f32:
12775ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::STORE_F32_A64 : WebAssembly::STORE_F32_A32;
1278*0b57cec5SDimitry Andric     break;
1279*0b57cec5SDimitry Andric   case MVT::f64:
12805ffd83dbSDimitry Andric     Opc = A64 ? WebAssembly::STORE_F64_A64 : WebAssembly::STORE_F64_A32;
1281*0b57cec5SDimitry Andric     break;
1282*0b57cec5SDimitry Andric   default:
1283*0b57cec5SDimitry Andric     return false;
1284*0b57cec5SDimitry Andric   }
1285*0b57cec5SDimitry Andric 
1286*0b57cec5SDimitry Andric   materializeLoadStoreOperands(Addr);
1287*0b57cec5SDimitry Andric 
128804eeddc0SDimitry Andric   Register ValueReg = getRegForValue(Store->getValueOperand());
1289*0b57cec5SDimitry Andric   if (ValueReg == 0)
1290*0b57cec5SDimitry Andric     return false;
1291*0b57cec5SDimitry Andric   if (VTIsi1)
1292*0b57cec5SDimitry Andric     ValueReg = maskI1Value(ValueReg, Store->getValueOperand());
1293*0b57cec5SDimitry Andric 
1294bdd1243dSDimitry Andric   auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc));
1295*0b57cec5SDimitry Andric 
1296*0b57cec5SDimitry Andric   addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Store));
1297*0b57cec5SDimitry Andric 
1298*0b57cec5SDimitry Andric   MIB.addReg(ValueReg);
1299*0b57cec5SDimitry Andric   return true;
1300*0b57cec5SDimitry Andric }
1301*0b57cec5SDimitry Andric 
1302*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectBr(const Instruction *I) {
1303*0b57cec5SDimitry Andric   const auto *Br = cast<BranchInst>(I);
1304*0b57cec5SDimitry Andric   if (Br->isUnconditional()) {
1305*0b57cec5SDimitry Andric     MachineBasicBlock *MSucc = FuncInfo.MBBMap[Br->getSuccessor(0)];
1306*0b57cec5SDimitry Andric     fastEmitBranch(MSucc, Br->getDebugLoc());
1307*0b57cec5SDimitry Andric     return true;
1308*0b57cec5SDimitry Andric   }
1309*0b57cec5SDimitry Andric 
1310*0b57cec5SDimitry Andric   MachineBasicBlock *TBB = FuncInfo.MBBMap[Br->getSuccessor(0)];
1311*0b57cec5SDimitry Andric   MachineBasicBlock *FBB = FuncInfo.MBBMap[Br->getSuccessor(1)];
1312*0b57cec5SDimitry Andric 
1313*0b57cec5SDimitry Andric   bool Not;
131469ade1e0SDimitry Andric   unsigned CondReg = getRegForI1Value(Br->getCondition(), Br->getParent(), Not);
1315*0b57cec5SDimitry Andric   if (CondReg == 0)
1316*0b57cec5SDimitry Andric     return false;
1317*0b57cec5SDimitry Andric 
1318*0b57cec5SDimitry Andric   unsigned Opc = WebAssembly::BR_IF;
1319*0b57cec5SDimitry Andric   if (Not)
1320*0b57cec5SDimitry Andric     Opc = WebAssembly::BR_UNLESS;
1321*0b57cec5SDimitry Andric 
1322bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))
1323*0b57cec5SDimitry Andric       .addMBB(TBB)
1324*0b57cec5SDimitry Andric       .addReg(CondReg);
1325*0b57cec5SDimitry Andric 
1326*0b57cec5SDimitry Andric   finishCondBranch(Br->getParent(), TBB, FBB);
1327*0b57cec5SDimitry Andric   return true;
1328*0b57cec5SDimitry Andric }
1329*0b57cec5SDimitry Andric 
1330*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectRet(const Instruction *I) {
1331*0b57cec5SDimitry Andric   if (!FuncInfo.CanLowerReturn)
1332*0b57cec5SDimitry Andric     return false;
1333*0b57cec5SDimitry Andric 
1334*0b57cec5SDimitry Andric   const auto *Ret = cast<ReturnInst>(I);
1335*0b57cec5SDimitry Andric 
1336*0b57cec5SDimitry Andric   if (Ret->getNumOperands() == 0) {
1337bdd1243dSDimitry Andric     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
13388bcb0991SDimitry Andric             TII.get(WebAssembly::RETURN));
1339*0b57cec5SDimitry Andric     return true;
1340*0b57cec5SDimitry Andric   }
1341*0b57cec5SDimitry Andric 
13428bcb0991SDimitry Andric   // TODO: support multiple return in FastISel
13438bcb0991SDimitry Andric   if (Ret->getNumOperands() > 1)
13448bcb0991SDimitry Andric     return false;
13458bcb0991SDimitry Andric 
1346*0b57cec5SDimitry Andric   Value *RV = Ret->getOperand(0);
1347*0b57cec5SDimitry Andric   if (!Subtarget->hasSIMD128() && RV->getType()->isVectorTy())
1348*0b57cec5SDimitry Andric     return false;
1349*0b57cec5SDimitry Andric 
1350*0b57cec5SDimitry Andric   switch (getSimpleType(RV->getType())) {
1351*0b57cec5SDimitry Andric   case MVT::i1:
1352*0b57cec5SDimitry Andric   case MVT::i8:
1353*0b57cec5SDimitry Andric   case MVT::i16:
1354*0b57cec5SDimitry Andric   case MVT::i32:
1355*0b57cec5SDimitry Andric   case MVT::i64:
1356*0b57cec5SDimitry Andric   case MVT::f32:
1357*0b57cec5SDimitry Andric   case MVT::f64:
1358*0b57cec5SDimitry Andric   case MVT::v16i8:
1359*0b57cec5SDimitry Andric   case MVT::v8i16:
1360*0b57cec5SDimitry Andric   case MVT::v4i32:
1361*0b57cec5SDimitry Andric   case MVT::v2i64:
1362*0b57cec5SDimitry Andric   case MVT::v4f32:
1363*0b57cec5SDimitry Andric   case MVT::v2f64:
1364e8d8bef9SDimitry Andric   case MVT::funcref:
1365e8d8bef9SDimitry Andric   case MVT::externref:
1366*0b57cec5SDimitry Andric     break;
1367*0b57cec5SDimitry Andric   default:
1368*0b57cec5SDimitry Andric     return false;
1369*0b57cec5SDimitry Andric   }
1370*0b57cec5SDimitry Andric 
1371*0b57cec5SDimitry Andric   unsigned Reg;
1372349cc55cSDimitry Andric   if (FuncInfo.Fn->getAttributes().hasRetAttr(Attribute::SExt))
1373*0b57cec5SDimitry Andric     Reg = getRegForSignedValue(RV);
1374349cc55cSDimitry Andric   else if (FuncInfo.Fn->getAttributes().hasRetAttr(Attribute::ZExt))
1375*0b57cec5SDimitry Andric     Reg = getRegForUnsignedValue(RV);
1376*0b57cec5SDimitry Andric   else
1377*0b57cec5SDimitry Andric     Reg = getRegForValue(RV);
1378*0b57cec5SDimitry Andric 
1379*0b57cec5SDimitry Andric   if (Reg == 0)
1380*0b57cec5SDimitry Andric     return false;
1381*0b57cec5SDimitry Andric 
1382bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
13838bcb0991SDimitry Andric           TII.get(WebAssembly::RETURN))
13848bcb0991SDimitry Andric       .addReg(Reg);
1385*0b57cec5SDimitry Andric   return true;
1386*0b57cec5SDimitry Andric }
1387*0b57cec5SDimitry Andric 
1388*0b57cec5SDimitry Andric bool WebAssemblyFastISel::selectUnreachable(const Instruction *I) {
1389bdd1243dSDimitry Andric   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1390*0b57cec5SDimitry Andric           TII.get(WebAssembly::UNREACHABLE));
1391*0b57cec5SDimitry Andric   return true;
1392*0b57cec5SDimitry Andric }
1393*0b57cec5SDimitry Andric 
1394*0b57cec5SDimitry Andric bool WebAssemblyFastISel::fastSelectInstruction(const Instruction *I) {
1395*0b57cec5SDimitry Andric   switch (I->getOpcode()) {
1396*0b57cec5SDimitry Andric   case Instruction::Call:
1397*0b57cec5SDimitry Andric     if (selectCall(I))
1398*0b57cec5SDimitry Andric       return true;
1399*0b57cec5SDimitry Andric     break;
1400*0b57cec5SDimitry Andric   case Instruction::Select:
1401*0b57cec5SDimitry Andric     return selectSelect(I);
1402*0b57cec5SDimitry Andric   case Instruction::Trunc:
1403*0b57cec5SDimitry Andric     return selectTrunc(I);
1404*0b57cec5SDimitry Andric   case Instruction::ZExt:
1405*0b57cec5SDimitry Andric     return selectZExt(I);
1406*0b57cec5SDimitry Andric   case Instruction::SExt:
1407*0b57cec5SDimitry Andric     return selectSExt(I);
1408*0b57cec5SDimitry Andric   case Instruction::ICmp:
1409*0b57cec5SDimitry Andric     return selectICmp(I);
1410*0b57cec5SDimitry Andric   case Instruction::FCmp:
1411*0b57cec5SDimitry Andric     return selectFCmp(I);
1412*0b57cec5SDimitry Andric   case Instruction::BitCast:
1413*0b57cec5SDimitry Andric     return selectBitCast(I);
1414*0b57cec5SDimitry Andric   case Instruction::Load:
1415*0b57cec5SDimitry Andric     return selectLoad(I);
1416*0b57cec5SDimitry Andric   case Instruction::Store:
1417*0b57cec5SDimitry Andric     return selectStore(I);
1418*0b57cec5SDimitry Andric   case Instruction::Br:
1419*0b57cec5SDimitry Andric     return selectBr(I);
1420*0b57cec5SDimitry Andric   case Instruction::Ret:
1421*0b57cec5SDimitry Andric     return selectRet(I);
1422*0b57cec5SDimitry Andric   case Instruction::Unreachable:
1423*0b57cec5SDimitry Andric     return selectUnreachable(I);
1424*0b57cec5SDimitry Andric   default:
1425*0b57cec5SDimitry Andric     break;
1426*0b57cec5SDimitry Andric   }
1427*0b57cec5SDimitry Andric 
1428*0b57cec5SDimitry Andric   // Fall back to target-independent instruction selection.
1429*0b57cec5SDimitry Andric   return selectOperator(I, I->getOpcode());
1430*0b57cec5SDimitry Andric }
1431*0b57cec5SDimitry Andric 
1432*0b57cec5SDimitry Andric FastISel *WebAssembly::createFastISel(FunctionLoweringInfo &FuncInfo,
1433*0b57cec5SDimitry Andric                                       const TargetLibraryInfo *LibInfo) {
1434*0b57cec5SDimitry Andric   return new WebAssemblyFastISel(FuncInfo, LibInfo);
1435*0b57cec5SDimitry Andric }
1436