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