blob: bca3355df68e8c3898e53f3ee526fe11c1a0c498 [file] [log] [blame]
Dan Gohman7b634842015-08-24 18:44:37 +00001//===-- WebAssemblyFastISel.cpp - WebAssembly FastISel implementation -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman7b634842015-08-24 18:44:37 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file defines the WebAssembly-specific support for the FastISel
Dan Gohman7b634842015-08-24 18:44:37 +000011/// class. Some of the target-specific code is generated by tablegen in the file
12/// WebAssemblyGenFastISel.inc, which is #included here.
13///
Dan Gohman33e694a2016-05-12 04:19:09 +000014/// TODO: kill flags
15///
Dan Gohman7b634842015-08-24 18:44:37 +000016//===----------------------------------------------------------------------===//
17
Dan Gohman7b634842015-08-24 18:44:37 +000018#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "WebAssembly.h"
Dan Gohman33e694a2016-05-12 04:19:09 +000020#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman7b634842015-08-24 18:44:37 +000021#include "WebAssemblySubtarget.h"
22#include "WebAssemblyTargetMachine.h"
23#include "llvm/Analysis/BranchProbabilityInfo.h"
24#include "llvm/CodeGen/FastISel.h"
25#include "llvm/CodeGen/FunctionLoweringInfo.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/DerivedTypes.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/GetElementPtrTypeIterator.h"
34#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/Operator.h"
Sanjay Patel47a52a02018-10-23 16:05:09 +000039#include "llvm/IR/PatternMatch.h"
40
Dan Gohman7b634842015-08-24 18:44:37 +000041using namespace llvm;
Sanjay Patel47a52a02018-10-23 16:05:09 +000042using namespace PatternMatch;
Dan Gohman7b634842015-08-24 18:44:37 +000043
44#define DEBUG_TYPE "wasm-fastisel"
45
46namespace {
47
48class WebAssemblyFastISel final : public FastISel {
Dan Gohman2e644382016-05-10 17:39:48 +000049 // All possible address modes.
50 class Address {
51 public:
52 typedef enum { RegBase, FrameIndexBase } BaseKind;
53
54 private:
55 BaseKind Kind;
56 union {
57 unsigned Reg;
58 int FI;
59 } Base;
60
61 int64_t Offset;
62
63 const GlobalValue *GV;
64
65 public:
66 // Innocuous defaults for our address.
67 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
Jacob Gravellea31ec612017-06-22 21:26:08 +000068 void setKind(BaseKind K) {
69 assert(!isSet() && "Can't change kind with non-zero base");
70 Kind = K;
71 }
Dan Gohman2e644382016-05-10 17:39:48 +000072 BaseKind getKind() const { return Kind; }
73 bool isRegBase() const { return Kind == RegBase; }
74 bool isFIBase() const { return Kind == FrameIndexBase; }
75 void setReg(unsigned Reg) {
76 assert(isRegBase() && "Invalid base register access!");
Jacob Gravellea31ec612017-06-22 21:26:08 +000077 assert(Base.Reg == 0 && "Overwriting non-zero register");
Dan Gohman2e644382016-05-10 17:39:48 +000078 Base.Reg = Reg;
79 }
80 unsigned getReg() const {
81 assert(isRegBase() && "Invalid base register access!");
82 return Base.Reg;
83 }
84 void setFI(unsigned FI) {
85 assert(isFIBase() && "Invalid base frame index access!");
Jacob Gravellea31ec612017-06-22 21:26:08 +000086 assert(Base.FI == 0 && "Overwriting non-zero frame index");
Dan Gohman2e644382016-05-10 17:39:48 +000087 Base.FI = FI;
88 }
89 unsigned getFI() const {
90 assert(isFIBase() && "Invalid base frame index access!");
91 return Base.FI;
92 }
93
Dan Gohman728926a2016-12-22 15:15:10 +000094 void setOffset(int64_t Offset_) {
95 assert(Offset_ >= 0 && "Offsets must be non-negative");
96 Offset = Offset_;
97 }
Dan Gohman2e644382016-05-10 17:39:48 +000098 int64_t getOffset() const { return Offset; }
99 void setGlobalValue(const GlobalValue *G) { GV = G; }
100 const GlobalValue *getGlobalValue() const { return GV; }
Jacob Gravellea31ec612017-06-22 21:26:08 +0000101 bool isSet() const {
102 if (isRegBase()) {
103 return Base.Reg != 0;
104 } else {
105 return Base.FI != 0;
106 }
107 }
Dan Gohman2e644382016-05-10 17:39:48 +0000108 };
109
Dan Gohman7b634842015-08-24 18:44:37 +0000110 /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
111 /// right decision when generating code for different targets.
112 const WebAssemblySubtarget *Subtarget;
113 LLVMContext *Context;
114
Dan Gohman7b634842015-08-24 18:44:37 +0000115private:
Dan Gohman2e644382016-05-10 17:39:48 +0000116 // Utility helper routines
Dan Gohman3a5ce732016-05-11 16:32:42 +0000117 MVT::SimpleValueType getSimpleType(Type *Ty) {
118 EVT VT = TLI.getValueType(DL, Ty, /*HandleUnknown=*/true);
Heejin Ahnf208f632018-09-05 01:27:38 +0000119 return VT.isSimple() ? VT.getSimpleVT().SimpleTy
120 : MVT::INVALID_SIMPLE_VALUE_TYPE;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000121 }
122 MVT::SimpleValueType getLegalType(MVT::SimpleValueType VT) {
123 switch (VT) {
124 case MVT::i1:
125 case MVT::i8:
126 case MVT::i16:
Dan Gohman3a5ce732016-05-11 16:32:42 +0000127 return MVT::i32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000128 case MVT::i32:
Dan Gohman3a5ce732016-05-11 16:32:42 +0000129 case MVT::i64:
Dan Gohman33e694a2016-05-12 04:19:09 +0000130 case MVT::f32:
131 case MVT::f64:
Heejin Ahn0de58722018-03-08 04:05:37 +0000132 case MVT::ExceptRef:
Dan Gohman33e694a2016-05-12 04:19:09 +0000133 return VT;
Dan Gohman6999c4f2017-02-24 21:05:35 +0000134 case MVT::f16:
135 return MVT::f32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000136 case MVT::v16i8:
137 case MVT::v8i16:
138 case MVT::v4i32:
139 case MVT::v4f32:
140 if (Subtarget->hasSIMD128())
141 return VT;
142 break;
Thomas Livelyb6dac892018-12-21 06:58:15 +0000143 case MVT::v2i64:
144 case MVT::v2f64:
Thomas Lively64a39a12019-01-10 22:32:11 +0000145 if (Subtarget->hasUnimplementedSIMD128())
Thomas Livelyb6dac892018-12-21 06:58:15 +0000146 return VT;
147 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000148 default:
149 break;
150 }
151 return MVT::INVALID_SIMPLE_VALUE_TYPE;
152 }
Dan Gohman2e644382016-05-10 17:39:48 +0000153 bool computeAddress(const Value *Obj, Address &Addr);
154 void materializeLoadStoreOperands(Address &Addr);
155 void addLoadStoreOperands(const Address &Addr, const MachineInstrBuilder &MIB,
156 MachineMemOperand *MMO);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000157 unsigned maskI1Value(unsigned Reg, const Value *V);
Dan Gohman33e694a2016-05-12 04:19:09 +0000158 unsigned getRegForI1Value(const Value *V, bool &Not);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000159 unsigned zeroExtendToI32(unsigned Reg, const Value *V,
160 MVT::SimpleValueType From);
161 unsigned signExtendToI32(unsigned Reg, const Value *V,
162 MVT::SimpleValueType From);
Heejin Ahnf208f632018-09-05 01:27:38 +0000163 unsigned zeroExtend(unsigned Reg, const Value *V, MVT::SimpleValueType From,
Dan Gohman3a5ce732016-05-11 16:32:42 +0000164 MVT::SimpleValueType To);
Heejin Ahnf208f632018-09-05 01:27:38 +0000165 unsigned signExtend(unsigned Reg, const Value *V, MVT::SimpleValueType From,
Dan Gohman3a5ce732016-05-11 16:32:42 +0000166 MVT::SimpleValueType To);
167 unsigned getRegForUnsignedValue(const Value *V);
168 unsigned getRegForSignedValue(const Value *V);
169 unsigned getRegForPromotedValue(const Value *V, bool IsSigned);
170 unsigned notValue(unsigned Reg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000171 unsigned copyValue(unsigned Reg);
Dan Gohman2e644382016-05-10 17:39:48 +0000172
173 // Backend specific FastISel code.
174 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
175 unsigned fastMaterializeConstant(const Constant *C) override;
Dan Gohman33e694a2016-05-12 04:19:09 +0000176 bool fastLowerArguments() override;
Dan Gohman2e644382016-05-10 17:39:48 +0000177
178 // Selection routines.
Dan Gohman33e694a2016-05-12 04:19:09 +0000179 bool selectCall(const Instruction *I);
180 bool selectSelect(const Instruction *I);
181 bool selectTrunc(const Instruction *I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000182 bool selectZExt(const Instruction *I);
183 bool selectSExt(const Instruction *I);
184 bool selectICmp(const Instruction *I);
185 bool selectFCmp(const Instruction *I);
Dan Gohman2e644382016-05-10 17:39:48 +0000186 bool selectBitCast(const Instruction *I);
187 bool selectLoad(const Instruction *I);
188 bool selectStore(const Instruction *I);
189 bool selectBr(const Instruction *I);
190 bool selectRet(const Instruction *I);
191 bool selectUnreachable(const Instruction *I);
192
Dan Gohman7b634842015-08-24 18:44:37 +0000193public:
194 // Backend specific FastISel code.
195 WebAssemblyFastISel(FunctionLoweringInfo &FuncInfo,
196 const TargetLibraryInfo *LibInfo)
197 : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
198 Subtarget = &FuncInfo.MF->getSubtarget<WebAssemblySubtarget>();
199 Context = &FuncInfo.Fn->getContext();
200 }
201
202 bool fastSelectInstruction(const Instruction *I) override;
203
204#include "WebAssemblyGenFastISel.inc"
205};
206
207} // end anonymous namespace
208
Dan Gohman2e644382016-05-10 17:39:48 +0000209bool WebAssemblyFastISel::computeAddress(const Value *Obj, Address &Addr) {
210
211 const User *U = nullptr;
212 unsigned Opcode = Instruction::UserOp1;
213 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
214 // Don't walk into other basic blocks unless the object is an alloca from
215 // another block, otherwise it may not have a virtual register assigned.
216 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
217 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
218 Opcode = I->getOpcode();
219 U = I;
220 }
221 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
222 Opcode = C->getOpcode();
223 U = C;
224 }
225
226 if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
227 if (Ty->getAddressSpace() > 255)
228 // Fast instruction selection doesn't support the special
229 // address spaces.
230 return false;
231
232 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
233 if (Addr.getGlobalValue())
234 return false;
235 Addr.setGlobalValue(GV);
236 return true;
237 }
238
239 switch (Opcode) {
Dan Gohman7b634842015-08-24 18:44:37 +0000240 default:
241 break;
Dan Gohman2e644382016-05-10 17:39:48 +0000242 case Instruction::BitCast: {
243 // Look through bitcasts.
244 return computeAddress(U->getOperand(0), Addr);
245 }
246 case Instruction::IntToPtr: {
247 // Look past no-op inttoptrs.
248 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
249 TLI.getPointerTy(DL))
250 return computeAddress(U->getOperand(0), Addr);
251 break;
252 }
253 case Instruction::PtrToInt: {
254 // Look past no-op ptrtoints.
255 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
256 return computeAddress(U->getOperand(0), Addr);
257 break;
258 }
259 case Instruction::GetElementPtr: {
260 Address SavedAddr = Addr;
261 uint64_t TmpOffset = Addr.getOffset();
Dan Gohman728926a2016-12-22 15:15:10 +0000262 // Non-inbounds geps can wrap; wasm's offsets can't.
263 if (!cast<GEPOperator>(U)->isInBounds())
264 goto unsupported_gep;
Dan Gohman2e644382016-05-10 17:39:48 +0000265 // Iterate through the GEP folding the constants into offsets where
266 // we can.
Dan Gohman33e694a2016-05-12 04:19:09 +0000267 for (gep_type_iterator GTI = gep_type_begin(U), E = gep_type_end(U);
Dan Gohman2e644382016-05-10 17:39:48 +0000268 GTI != E; ++GTI) {
269 const Value *Op = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000270 if (StructType *STy = GTI.getStructTypeOrNull()) {
Dan Gohman2e644382016-05-10 17:39:48 +0000271 const StructLayout *SL = DL.getStructLayout(STy);
272 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
273 TmpOffset += SL->getElementOffset(Idx);
274 } else {
275 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
276 for (;;) {
277 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
278 // Constant-offset addressing.
279 TmpOffset += CI->getSExtValue() * S;
280 break;
281 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000282 if (S == 1 && Addr.isRegBase() && Addr.getReg() == 0) {
283 // An unscaled add of a register. Set it as the new base.
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000284 unsigned Reg = getRegForValue(Op);
285 if (Reg == 0)
286 return false;
287 Addr.setReg(Reg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000288 break;
289 }
Dan Gohman2e644382016-05-10 17:39:48 +0000290 if (canFoldAddIntoGEP(U, Op)) {
291 // A compatible add with a constant operand. Fold the constant.
292 ConstantInt *CI =
293 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
294 TmpOffset += CI->getSExtValue() * S;
295 // Iterate on the other operand.
296 Op = cast<AddOperator>(Op)->getOperand(0);
297 continue;
298 }
299 // Unsupported
300 goto unsupported_gep;
301 }
302 }
303 }
Dan Gohman728926a2016-12-22 15:15:10 +0000304 // Don't fold in negative offsets.
305 if (int64_t(TmpOffset) >= 0) {
306 // Try to grab the base operand now.
307 Addr.setOffset(TmpOffset);
308 if (computeAddress(U->getOperand(0), Addr))
309 return true;
310 }
Dan Gohman2e644382016-05-10 17:39:48 +0000311 // We failed, restore everything and try the other options.
312 Addr = SavedAddr;
313 unsupported_gep:
314 break;
315 }
316 case Instruction::Alloca: {
317 const AllocaInst *AI = cast<AllocaInst>(Obj);
318 DenseMap<const AllocaInst *, int>::iterator SI =
319 FuncInfo.StaticAllocaMap.find(AI);
320 if (SI != FuncInfo.StaticAllocaMap.end()) {
Jacob Gravellea31ec612017-06-22 21:26:08 +0000321 if (Addr.isSet()) {
322 return false;
323 }
Dan Gohman2e644382016-05-10 17:39:48 +0000324 Addr.setKind(Address::FrameIndexBase);
325 Addr.setFI(SI->second);
326 return true;
327 }
328 break;
329 }
330 case Instruction::Add: {
331 // Adds of constants are common and easy enough.
332 const Value *LHS = U->getOperand(0);
333 const Value *RHS = U->getOperand(1);
334
335 if (isa<ConstantInt>(LHS))
336 std::swap(LHS, RHS);
337
338 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
Dan Gohman728926a2016-12-22 15:15:10 +0000339 uint64_t TmpOffset = Addr.getOffset() + CI->getSExtValue();
340 if (int64_t(TmpOffset) >= 0) {
341 Addr.setOffset(TmpOffset);
342 return computeAddress(LHS, Addr);
343 }
Dan Gohman2e644382016-05-10 17:39:48 +0000344 }
345
346 Address Backup = Addr;
347 if (computeAddress(LHS, Addr) && computeAddress(RHS, Addr))
348 return true;
349 Addr = Backup;
350
351 break;
352 }
353 case Instruction::Sub: {
354 // Subs of constants are common and easy enough.
355 const Value *LHS = U->getOperand(0);
356 const Value *RHS = U->getOperand(1);
357
358 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
Dan Gohman728926a2016-12-22 15:15:10 +0000359 int64_t TmpOffset = Addr.getOffset() - CI->getSExtValue();
360 if (TmpOffset >= 0) {
361 Addr.setOffset(TmpOffset);
362 return computeAddress(LHS, Addr);
363 }
Dan Gohman2e644382016-05-10 17:39:48 +0000364 }
365 break;
366 }
367 }
Jacob Gravellea31ec612017-06-22 21:26:08 +0000368 if (Addr.isSet()) {
369 return false;
370 }
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000371 unsigned Reg = getRegForValue(Obj);
372 if (Reg == 0)
373 return false;
374 Addr.setReg(Reg);
Dan Gohman2e644382016-05-10 17:39:48 +0000375 return Addr.getReg() != 0;
376}
377
378void WebAssemblyFastISel::materializeLoadStoreOperands(Address &Addr) {
379 if (Addr.isRegBase()) {
380 unsigned Reg = Addr.getReg();
381 if (Reg == 0) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000382 Reg = createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
383 : &WebAssembly::I32RegClass);
384 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
385 : WebAssembly::CONST_I32;
Dan Gohman2e644382016-05-10 17:39:48 +0000386 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), Reg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000387 .addImm(0);
Dan Gohman2e644382016-05-10 17:39:48 +0000388 Addr.setReg(Reg);
389 }
390 }
391}
392
393void WebAssemblyFastISel::addLoadStoreOperands(const Address &Addr,
394 const MachineInstrBuilder &MIB,
395 MachineMemOperand *MMO) {
Dan Gohman48abaa92016-10-25 00:17:11 +0000396 // Set the alignment operand (this is rewritten in SetP2AlignOperands).
397 // TODO: Disable SetP2AlignOperands for FastISel and just do it here.
398 MIB.addImm(0);
399
Dan Gohman2e644382016-05-10 17:39:48 +0000400 if (const GlobalValue *GV = Addr.getGlobalValue())
401 MIB.addGlobalAddress(GV, Addr.getOffset());
402 else
403 MIB.addImm(Addr.getOffset());
404
405 if (Addr.isRegBase())
406 MIB.addReg(Addr.getReg());
407 else
408 MIB.addFrameIndex(Addr.getFI());
409
Dan Gohman2e644382016-05-10 17:39:48 +0000410 MIB.addMemOperand(MMO);
411}
412
Dan Gohman3a5ce732016-05-11 16:32:42 +0000413unsigned WebAssemblyFastISel::maskI1Value(unsigned Reg, const Value *V) {
414 return zeroExtendToI32(Reg, V, MVT::i1);
Dan Gohman2e644382016-05-10 17:39:48 +0000415}
416
Dan Gohman33e694a2016-05-12 04:19:09 +0000417unsigned WebAssemblyFastISel::getRegForI1Value(const Value *V, bool &Not) {
418 if (const ICmpInst *ICmp = dyn_cast<ICmpInst>(V))
419 if (const ConstantInt *C = dyn_cast<ConstantInt>(ICmp->getOperand(1)))
420 if (ICmp->isEquality() && C->isZero() && C->getType()->isIntegerTy(32)) {
421 Not = ICmp->isTrueWhenEqual();
422 return getRegForValue(ICmp->getOperand(0));
423 }
424
Sanjay Patel47a52a02018-10-23 16:05:09 +0000425 Value *NotV;
426 if (match(V, m_Not(m_Value(NotV))) && V->getType()->isIntegerTy(32)) {
Dan Gohman33e694a2016-05-12 04:19:09 +0000427 Not = true;
Sanjay Patel47a52a02018-10-23 16:05:09 +0000428 return getRegForValue(NotV);
Dan Gohman33e694a2016-05-12 04:19:09 +0000429 }
430
431 Not = false;
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000432 unsigned Reg = getRegForValue(V);
433 if (Reg == 0)
434 return 0;
435 return maskI1Value(Reg, V);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000436}
437
438unsigned WebAssemblyFastISel::zeroExtendToI32(unsigned Reg, const Value *V,
439 MVT::SimpleValueType From) {
Derek Schuff732636d2016-08-04 18:01:52 +0000440 if (Reg == 0)
441 return 0;
442
Dan Gohman3a5ce732016-05-11 16:32:42 +0000443 switch (From) {
444 case MVT::i1:
Thomas Lively2a47e032019-01-14 22:03:43 +0000445 // If the value is naturally an i1, we don't need to mask it. We only know
446 // if a value is naturally an i1 if it is definitely lowered by FastISel,
447 // not a DAG ISel fallback.
448 if (V != nullptr && isa<Argument>(V) && cast<Argument>(V)->hasZExtAttr())
449 return copyValue(Reg);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000450 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000451 case MVT::i8:
452 case MVT::i16:
Dan Gohman3a5ce732016-05-11 16:32:42 +0000453 break;
454 case MVT::i32:
Dan Gohman33e694a2016-05-12 04:19:09 +0000455 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000456 default:
457 return 0;
458 }
459
460 unsigned Imm = createResultReg(&WebAssembly::I32RegClass);
461 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
462 TII.get(WebAssembly::CONST_I32), Imm)
Heejin Ahnf208f632018-09-05 01:27:38 +0000463 .addImm(~(~uint64_t(0) << MVT(From).getSizeInBits()));
Dan Gohman3a5ce732016-05-11 16:32:42 +0000464
465 unsigned Result = createResultReg(&WebAssembly::I32RegClass);
466 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
467 TII.get(WebAssembly::AND_I32), Result)
Heejin Ahnf208f632018-09-05 01:27:38 +0000468 .addReg(Reg)
469 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000470
471 return Result;
472}
473
474unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
475 MVT::SimpleValueType From) {
Derek Schuff732636d2016-08-04 18:01:52 +0000476 if (Reg == 0)
477 return 0;
478
Dan Gohman3a5ce732016-05-11 16:32:42 +0000479 switch (From) {
480 case MVT::i1:
481 case MVT::i8:
482 case MVT::i16:
483 break;
484 case MVT::i32:
Dan Gohman33e694a2016-05-12 04:19:09 +0000485 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000486 default:
Dan Gohman33e694a2016-05-12 04:19:09 +0000487 return 0;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000488 }
489
490 unsigned Imm = createResultReg(&WebAssembly::I32RegClass);
491 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
492 TII.get(WebAssembly::CONST_I32), Imm)
Heejin Ahnf208f632018-09-05 01:27:38 +0000493 .addImm(32 - MVT(From).getSizeInBits());
Dan Gohman3a5ce732016-05-11 16:32:42 +0000494
495 unsigned Left = createResultReg(&WebAssembly::I32RegClass);
496 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
497 TII.get(WebAssembly::SHL_I32), Left)
Heejin Ahnf208f632018-09-05 01:27:38 +0000498 .addReg(Reg)
499 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000500
501 unsigned Right = createResultReg(&WebAssembly::I32RegClass);
502 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
503 TII.get(WebAssembly::SHR_S_I32), Right)
Heejin Ahnf208f632018-09-05 01:27:38 +0000504 .addReg(Left)
505 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000506
507 return Right;
508}
509
510unsigned WebAssemblyFastISel::zeroExtend(unsigned Reg, const Value *V,
511 MVT::SimpleValueType From,
512 MVT::SimpleValueType To) {
513 if (To == MVT::i64) {
514 if (From == MVT::i64)
Dan Gohman33e694a2016-05-12 04:19:09 +0000515 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000516
517 Reg = zeroExtendToI32(Reg, V, From);
518
519 unsigned Result = createResultReg(&WebAssembly::I64RegClass);
520 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
521 TII.get(WebAssembly::I64_EXTEND_U_I32), Result)
522 .addReg(Reg);
523 return Result;
524 }
525
Dan Gohman3a5ce732016-05-11 16:32:42 +0000526 return zeroExtendToI32(Reg, V, From);
527}
528
529unsigned WebAssemblyFastISel::signExtend(unsigned Reg, const Value *V,
530 MVT::SimpleValueType From,
531 MVT::SimpleValueType To) {
532 if (To == MVT::i64) {
533 if (From == MVT::i64)
Dan Gohman33e694a2016-05-12 04:19:09 +0000534 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000535
536 Reg = signExtendToI32(Reg, V, From);
537
538 unsigned Result = createResultReg(&WebAssembly::I64RegClass);
539 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
540 TII.get(WebAssembly::I64_EXTEND_S_I32), Result)
541 .addReg(Reg);
542 return Result;
543 }
544
Dan Gohman3a5ce732016-05-11 16:32:42 +0000545 return signExtendToI32(Reg, V, From);
546}
547
548unsigned WebAssemblyFastISel::getRegForUnsignedValue(const Value *V) {
549 MVT::SimpleValueType From = getSimpleType(V->getType());
550 MVT::SimpleValueType To = getLegalType(From);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000551 unsigned VReg = getRegForValue(V);
552 if (VReg == 0)
553 return 0;
554 return zeroExtend(VReg, V, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000555}
556
557unsigned WebAssemblyFastISel::getRegForSignedValue(const Value *V) {
558 MVT::SimpleValueType From = getSimpleType(V->getType());
559 MVT::SimpleValueType To = getLegalType(From);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000560 unsigned VReg = getRegForValue(V);
561 if (VReg == 0)
562 return 0;
563 return signExtend(VReg, V, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000564}
565
566unsigned WebAssemblyFastISel::getRegForPromotedValue(const Value *V,
567 bool IsSigned) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000568 return IsSigned ? getRegForSignedValue(V) : getRegForUnsignedValue(V);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000569}
570
571unsigned WebAssemblyFastISel::notValue(unsigned Reg) {
Dan Gohman33e694a2016-05-12 04:19:09 +0000572 assert(MRI.getRegClass(Reg) == &WebAssembly::I32RegClass);
573
Dan Gohman3a5ce732016-05-11 16:32:42 +0000574 unsigned NotReg = createResultReg(&WebAssembly::I32RegClass);
575 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
576 TII.get(WebAssembly::EQZ_I32), NotReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000577 .addReg(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000578 return NotReg;
Dan Gohman2e644382016-05-10 17:39:48 +0000579}
580
Dan Gohman33e694a2016-05-12 04:19:09 +0000581unsigned WebAssemblyFastISel::copyValue(unsigned Reg) {
582 unsigned ResultReg = createResultReg(MRI.getRegClass(Reg));
Heejin Ahnf208f632018-09-05 01:27:38 +0000583 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(WebAssembly::COPY),
584 ResultReg)
585 .addReg(Reg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000586 return ResultReg;
587}
588
Dan Gohman2e644382016-05-10 17:39:48 +0000589unsigned WebAssemblyFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
590 DenseMap<const AllocaInst *, int>::iterator SI =
591 FuncInfo.StaticAllocaMap.find(AI);
592
593 if (SI != FuncInfo.StaticAllocaMap.end()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000594 unsigned ResultReg =
595 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
596 : &WebAssembly::I32RegClass);
597 unsigned Opc =
598 Subtarget->hasAddr64() ? WebAssembly::COPY_I64 : WebAssembly::COPY_I32;
Dan Gohman2e644382016-05-10 17:39:48 +0000599 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
600 .addFrameIndex(SI->second);
601 return ResultReg;
602 }
603
604 return 0;
605}
606
607unsigned WebAssemblyFastISel::fastMaterializeConstant(const Constant *C) {
608 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000609 unsigned ResultReg =
610 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
611 : &WebAssembly::I32RegClass);
612 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
613 : WebAssembly::CONST_I32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000614 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000615 .addGlobalAddress(GV);
Dan Gohman33e694a2016-05-12 04:19:09 +0000616 return ResultReg;
Dan Gohman2e644382016-05-10 17:39:48 +0000617 }
618
619 // Let target-independent code handle it.
620 return 0;
621}
622
Dan Gohman33e694a2016-05-12 04:19:09 +0000623bool WebAssemblyFastISel::fastLowerArguments() {
624 if (!FuncInfo.CanLowerReturn)
625 return false;
626
627 const Function *F = FuncInfo.Fn;
628 if (F->isVarArg())
629 return false;
630
631 unsigned i = 0;
632 for (auto const &Arg : F->args()) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000633 const AttributeList &Attrs = F->getAttributes();
Reid Klecknerf021fab2017-04-13 23:12:13 +0000634 if (Attrs.hasParamAttribute(i, Attribute::ByVal) ||
635 Attrs.hasParamAttribute(i, Attribute::SwiftSelf) ||
636 Attrs.hasParamAttribute(i, Attribute::SwiftError) ||
637 Attrs.hasParamAttribute(i, Attribute::InAlloca) ||
638 Attrs.hasParamAttribute(i, Attribute::Nest))
Dan Gohman33e694a2016-05-12 04:19:09 +0000639 return false;
640
641 Type *ArgTy = Arg.getType();
Derek Schuff39bf39f2016-08-02 23:16:09 +0000642 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
643 return false;
644 if (!Subtarget->hasSIMD128() && ArgTy->isVectorTy())
Dan Gohman33e694a2016-05-12 04:19:09 +0000645 return false;
646
647 unsigned Opc;
648 const TargetRegisterClass *RC;
649 switch (getSimpleType(ArgTy)) {
650 case MVT::i1:
651 case MVT::i8:
652 case MVT::i16:
653 case MVT::i32:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000654 Opc = WebAssembly::ARGUMENT_i32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000655 RC = &WebAssembly::I32RegClass;
656 break;
657 case MVT::i64:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000658 Opc = WebAssembly::ARGUMENT_i64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000659 RC = &WebAssembly::I64RegClass;
660 break;
661 case MVT::f32:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000662 Opc = WebAssembly::ARGUMENT_f32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000663 RC = &WebAssembly::F32RegClass;
664 break;
665 case MVT::f64:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000666 Opc = WebAssembly::ARGUMENT_f64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000667 RC = &WebAssembly::F64RegClass;
668 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000669 case MVT::v16i8:
670 Opc = WebAssembly::ARGUMENT_v16i8;
671 RC = &WebAssembly::V128RegClass;
672 break;
673 case MVT::v8i16:
674 Opc = WebAssembly::ARGUMENT_v8i16;
675 RC = &WebAssembly::V128RegClass;
676 break;
677 case MVT::v4i32:
678 Opc = WebAssembly::ARGUMENT_v4i32;
679 RC = &WebAssembly::V128RegClass;
680 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000681 case MVT::v2i64:
682 Opc = WebAssembly::ARGUMENT_v2i64;
683 RC = &WebAssembly::V128RegClass;
684 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000685 case MVT::v4f32:
686 Opc = WebAssembly::ARGUMENT_v4f32;
687 RC = &WebAssembly::V128RegClass;
688 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000689 case MVT::v2f64:
690 Opc = WebAssembly::ARGUMENT_v2f64;
691 RC = &WebAssembly::V128RegClass;
692 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000693 case MVT::ExceptRef:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000694 Opc = WebAssembly::ARGUMENT_ExceptRef;
Heejin Ahn0de58722018-03-08 04:05:37 +0000695 RC = &WebAssembly::EXCEPT_REFRegClass;
696 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000697 default:
698 return false;
699 }
700 unsigned ResultReg = createResultReg(RC);
701 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000702 .addImm(i);
Dan Gohman33e694a2016-05-12 04:19:09 +0000703 updateValueMap(&Arg, ResultReg);
704
705 ++i;
706 }
707
708 MRI.addLiveIn(WebAssembly::ARGUMENTS);
709
710 auto *MFI = MF->getInfo<WebAssemblyFunctionInfo>();
Dan Gohmanb8184822018-05-22 04:58:36 +0000711 for (auto const &Arg : F->args()) {
712 MVT::SimpleValueType ArgTy = getLegalType(getSimpleType(Arg.getType()));
713 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
714 MFI->clearParamsAndResults();
715 return false;
716 }
717 MFI->addParam(ArgTy);
718 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000719
Dan Gohman4576dc02018-04-17 20:46:42 +0000720 if (!F->getReturnType()->isVoidTy()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000721 MVT::SimpleValueType RetTy =
722 getLegalType(getSimpleType(F->getReturnType()));
Dan Gohmanb8184822018-05-22 04:58:36 +0000723 if (RetTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
724 MFI->clearParamsAndResults();
Dan Gohman4576dc02018-04-17 20:46:42 +0000725 return false;
Dan Gohmanb8184822018-05-22 04:58:36 +0000726 }
727 MFI->addResult(RetTy);
Dan Gohman4576dc02018-04-17 20:46:42 +0000728 }
Dan Gohman6055fba2017-01-09 23:09:38 +0000729
Dan Gohman33e694a2016-05-12 04:19:09 +0000730 return true;
731}
732
733bool WebAssemblyFastISel::selectCall(const Instruction *I) {
734 const CallInst *Call = cast<CallInst>(I);
735
736 if (Call->isMustTailCall() || Call->isInlineAsm() ||
737 Call->getFunctionType()->isVarArg())
738 return false;
739
740 Function *Func = Call->getCalledFunction();
741 if (Func && Func->isIntrinsic())
742 return false;
743
Jacob Gravelle690b76e2017-08-24 19:53:44 +0000744 bool IsDirect = Func != nullptr;
745 if (!IsDirect && isa<ConstantExpr>(Call->getCalledValue()))
746 return false;
747
Dan Gohman33e694a2016-05-12 04:19:09 +0000748 FunctionType *FuncTy = Call->getFunctionType();
749 unsigned Opc;
Dan Gohman33e694a2016-05-12 04:19:09 +0000750 bool IsVoid = FuncTy->getReturnType()->isVoidTy();
751 unsigned ResultReg;
752 if (IsVoid) {
Derek Schuff6f697832016-10-21 16:38:07 +0000753 Opc = IsDirect ? WebAssembly::CALL_VOID : WebAssembly::PCALL_INDIRECT_VOID;
Dan Gohman33e694a2016-05-12 04:19:09 +0000754 } else {
Derek Schuff39bf39f2016-08-02 23:16:09 +0000755 if (!Subtarget->hasSIMD128() && Call->getType()->isVectorTy())
756 return false;
757
Dan Gohman33e694a2016-05-12 04:19:09 +0000758 MVT::SimpleValueType RetTy = getSimpleType(Call->getType());
759 switch (RetTy) {
760 case MVT::i1:
761 case MVT::i8:
762 case MVT::i16:
763 case MVT::i32:
Derek Schuff6f697832016-10-21 16:38:07 +0000764 Opc = IsDirect ? WebAssembly::CALL_I32 : WebAssembly::PCALL_INDIRECT_I32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000765 ResultReg = createResultReg(&WebAssembly::I32RegClass);
766 break;
767 case MVT::i64:
Derek Schuff6f697832016-10-21 16:38:07 +0000768 Opc = IsDirect ? WebAssembly::CALL_I64 : WebAssembly::PCALL_INDIRECT_I64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000769 ResultReg = createResultReg(&WebAssembly::I64RegClass);
770 break;
771 case MVT::f32:
Derek Schuff6f697832016-10-21 16:38:07 +0000772 Opc = IsDirect ? WebAssembly::CALL_F32 : WebAssembly::PCALL_INDIRECT_F32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000773 ResultReg = createResultReg(&WebAssembly::F32RegClass);
774 break;
775 case MVT::f64:
Derek Schuff6f697832016-10-21 16:38:07 +0000776 Opc = IsDirect ? WebAssembly::CALL_F64 : WebAssembly::PCALL_INDIRECT_F64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000777 ResultReg = createResultReg(&WebAssembly::F64RegClass);
778 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000779 case MVT::v16i8:
Heejin Ahnf208f632018-09-05 01:27:38 +0000780 Opc = IsDirect ? WebAssembly::CALL_v16i8
781 : WebAssembly::PCALL_INDIRECT_v16i8;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000782 ResultReg = createResultReg(&WebAssembly::V128RegClass);
783 break;
784 case MVT::v8i16:
Heejin Ahnf208f632018-09-05 01:27:38 +0000785 Opc = IsDirect ? WebAssembly::CALL_v8i16
786 : WebAssembly::PCALL_INDIRECT_v8i16;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000787 ResultReg = createResultReg(&WebAssembly::V128RegClass);
788 break;
789 case MVT::v4i32:
Heejin Ahnf208f632018-09-05 01:27:38 +0000790 Opc = IsDirect ? WebAssembly::CALL_v4i32
791 : WebAssembly::PCALL_INDIRECT_v4i32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000792 ResultReg = createResultReg(&WebAssembly::V128RegClass);
793 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000794 case MVT::v2i64:
Heejin Ahnf208f632018-09-05 01:27:38 +0000795 Opc = IsDirect ? WebAssembly::CALL_v2i64
796 : WebAssembly::PCALL_INDIRECT_v2i64;
Derek Schuff51ed1312018-08-07 21:24:01 +0000797 ResultReg = createResultReg(&WebAssembly::V128RegClass);
798 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000799 case MVT::v4f32:
Heejin Ahnf208f632018-09-05 01:27:38 +0000800 Opc = IsDirect ? WebAssembly::CALL_v4f32
801 : WebAssembly::PCALL_INDIRECT_v4f32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000802 ResultReg = createResultReg(&WebAssembly::V128RegClass);
803 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000804 case MVT::v2f64:
Heejin Ahnf208f632018-09-05 01:27:38 +0000805 Opc = IsDirect ? WebAssembly::CALL_v2f64
806 : WebAssembly::PCALL_INDIRECT_v2f64;
Derek Schuff51ed1312018-08-07 21:24:01 +0000807 ResultReg = createResultReg(&WebAssembly::V128RegClass);
808 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000809 case MVT::ExceptRef:
810 Opc = IsDirect ? WebAssembly::CALL_EXCEPT_REF
811 : WebAssembly::PCALL_INDIRECT_EXCEPT_REF;
812 ResultReg = createResultReg(&WebAssembly::EXCEPT_REFRegClass);
813 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000814 default:
815 return false;
816 }
817 }
818
819 SmallVector<unsigned, 8> Args;
820 for (unsigned i = 0, e = Call->getNumArgOperands(); i < e; ++i) {
821 Value *V = Call->getArgOperand(i);
822 MVT::SimpleValueType ArgTy = getSimpleType(V->getType());
823 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
824 return false;
825
Reid Klecknerb5180542017-03-21 16:57:19 +0000826 const AttributeList &Attrs = Call->getAttributes();
Reid Klecknerf021fab2017-04-13 23:12:13 +0000827 if (Attrs.hasParamAttribute(i, Attribute::ByVal) ||
828 Attrs.hasParamAttribute(i, Attribute::SwiftSelf) ||
829 Attrs.hasParamAttribute(i, Attribute::SwiftError) ||
830 Attrs.hasParamAttribute(i, Attribute::InAlloca) ||
831 Attrs.hasParamAttribute(i, Attribute::Nest))
Dan Gohman33e694a2016-05-12 04:19:09 +0000832 return false;
833
834 unsigned Reg;
835
Reid Klecknerf021fab2017-04-13 23:12:13 +0000836 if (Attrs.hasParamAttribute(i, Attribute::SExt))
Dan Gohman33e694a2016-05-12 04:19:09 +0000837 Reg = getRegForSignedValue(V);
Reid Klecknerf021fab2017-04-13 23:12:13 +0000838 else if (Attrs.hasParamAttribute(i, Attribute::ZExt))
Dan Gohman33e694a2016-05-12 04:19:09 +0000839 Reg = getRegForUnsignedValue(V);
840 else
841 Reg = getRegForValue(V);
842
843 if (Reg == 0)
844 return false;
845
846 Args.push_back(Reg);
847 }
848
849 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
850
851 if (!IsVoid)
852 MIB.addReg(ResultReg, RegState::Define);
853
854 if (IsDirect)
855 MIB.addGlobalAddress(Func);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000856 else {
857 unsigned Reg = getRegForValue(Call->getCalledValue());
858 if (Reg == 0)
859 return false;
860 MIB.addReg(Reg);
861 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000862
863 for (unsigned ArgReg : Args)
864 MIB.addReg(ArgReg);
865
866 if (!IsVoid)
867 updateValueMap(Call, ResultReg);
868 return true;
869}
870
871bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
872 const SelectInst *Select = cast<SelectInst>(I);
873
874 bool Not;
Heejin Ahnf208f632018-09-05 01:27:38 +0000875 unsigned CondReg = getRegForI1Value(Select->getCondition(), Not);
Dan Gohman33e694a2016-05-12 04:19:09 +0000876 if (CondReg == 0)
877 return false;
878
Heejin Ahnf208f632018-09-05 01:27:38 +0000879 unsigned TrueReg = getRegForValue(Select->getTrueValue());
Dan Gohman33e694a2016-05-12 04:19:09 +0000880 if (TrueReg == 0)
881 return false;
882
883 unsigned FalseReg = getRegForValue(Select->getFalseValue());
884 if (FalseReg == 0)
885 return false;
886
887 if (Not)
888 std::swap(TrueReg, FalseReg);
889
890 unsigned Opc;
891 const TargetRegisterClass *RC;
892 switch (getSimpleType(Select->getType())) {
893 case MVT::i1:
894 case MVT::i8:
895 case MVT::i16:
896 case MVT::i32:
897 Opc = WebAssembly::SELECT_I32;
898 RC = &WebAssembly::I32RegClass;
899 break;
900 case MVT::i64:
901 Opc = WebAssembly::SELECT_I64;
902 RC = &WebAssembly::I64RegClass;
903 break;
904 case MVT::f32:
905 Opc = WebAssembly::SELECT_F32;
906 RC = &WebAssembly::F32RegClass;
907 break;
908 case MVT::f64:
909 Opc = WebAssembly::SELECT_F64;
910 RC = &WebAssembly::F64RegClass;
911 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000912 case MVT::ExceptRef:
913 Opc = WebAssembly::SELECT_EXCEPT_REF;
914 RC = &WebAssembly::EXCEPT_REFRegClass;
915 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000916 default:
917 return false;
918 }
919
920 unsigned ResultReg = createResultReg(RC);
921 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000922 .addReg(TrueReg)
923 .addReg(FalseReg)
924 .addReg(CondReg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000925
926 updateValueMap(Select, ResultReg);
927 return true;
928}
929
930bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
931 const TruncInst *Trunc = cast<TruncInst>(I);
932
933 unsigned Reg = getRegForValue(Trunc->getOperand(0));
934 if (Reg == 0)
935 return false;
936
937 if (Trunc->getOperand(0)->getType()->isIntegerTy(64)) {
938 unsigned Result = createResultReg(&WebAssembly::I32RegClass);
939 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
940 TII.get(WebAssembly::I32_WRAP_I64), Result)
941 .addReg(Reg);
942 Reg = Result;
943 }
944
945 updateValueMap(Trunc, Reg);
946 return true;
947}
948
Dan Gohman3a5ce732016-05-11 16:32:42 +0000949bool WebAssemblyFastISel::selectZExt(const Instruction *I) {
950 const ZExtInst *ZExt = cast<ZExtInst>(I);
951
Dan Gohman33e694a2016-05-12 04:19:09 +0000952 const Value *Op = ZExt->getOperand(0);
953 MVT::SimpleValueType From = getSimpleType(Op->getType());
954 MVT::SimpleValueType To = getLegalType(getSimpleType(ZExt->getType()));
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000955 unsigned In = getRegForValue(Op);
956 if (In == 0)
957 return false;
958 unsigned Reg = zeroExtend(In, Op, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000959 if (Reg == 0)
960 return false;
961
962 updateValueMap(ZExt, Reg);
963 return true;
964}
965
966bool WebAssemblyFastISel::selectSExt(const Instruction *I) {
967 const SExtInst *SExt = cast<SExtInst>(I);
968
Dan Gohman33e694a2016-05-12 04:19:09 +0000969 const Value *Op = SExt->getOperand(0);
970 MVT::SimpleValueType From = getSimpleType(Op->getType());
971 MVT::SimpleValueType To = getLegalType(getSimpleType(SExt->getType()));
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000972 unsigned In = getRegForValue(Op);
973 if (In == 0)
974 return false;
975 unsigned Reg = signExtend(In, Op, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000976 if (Reg == 0)
977 return false;
978
979 updateValueMap(SExt, Reg);
980 return true;
981}
982
983bool WebAssemblyFastISel::selectICmp(const Instruction *I) {
984 const ICmpInst *ICmp = cast<ICmpInst>(I);
985
986 bool I32 = getSimpleType(ICmp->getOperand(0)->getType()) != MVT::i64;
987 unsigned Opc;
988 bool isSigned = false;
989 switch (ICmp->getPredicate()) {
990 case ICmpInst::ICMP_EQ:
991 Opc = I32 ? WebAssembly::EQ_I32 : WebAssembly::EQ_I64;
992 break;
993 case ICmpInst::ICMP_NE:
994 Opc = I32 ? WebAssembly::NE_I32 : WebAssembly::NE_I64;
995 break;
996 case ICmpInst::ICMP_UGT:
997 Opc = I32 ? WebAssembly::GT_U_I32 : WebAssembly::GT_U_I64;
998 break;
999 case ICmpInst::ICMP_UGE:
1000 Opc = I32 ? WebAssembly::GE_U_I32 : WebAssembly::GE_U_I64;
1001 break;
1002 case ICmpInst::ICMP_ULT:
1003 Opc = I32 ? WebAssembly::LT_U_I32 : WebAssembly::LT_U_I64;
1004 break;
1005 case ICmpInst::ICMP_ULE:
1006 Opc = I32 ? WebAssembly::LE_U_I32 : WebAssembly::LE_U_I64;
1007 break;
1008 case ICmpInst::ICMP_SGT:
1009 Opc = I32 ? WebAssembly::GT_S_I32 : WebAssembly::GT_S_I64;
1010 isSigned = true;
1011 break;
1012 case ICmpInst::ICMP_SGE:
1013 Opc = I32 ? WebAssembly::GE_S_I32 : WebAssembly::GE_S_I64;
1014 isSigned = true;
1015 break;
1016 case ICmpInst::ICMP_SLT:
1017 Opc = I32 ? WebAssembly::LT_S_I32 : WebAssembly::LT_S_I64;
1018 isSigned = true;
1019 break;
1020 case ICmpInst::ICMP_SLE:
1021 Opc = I32 ? WebAssembly::LE_S_I32 : WebAssembly::LE_S_I64;
1022 isSigned = true;
1023 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001024 default:
1025 return false;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001026 }
1027
1028 unsigned LHS = getRegForPromotedValue(ICmp->getOperand(0), isSigned);
1029 if (LHS == 0)
1030 return false;
1031
1032 unsigned RHS = getRegForPromotedValue(ICmp->getOperand(1), isSigned);
1033 if (RHS == 0)
1034 return false;
1035
1036 unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass);
1037 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1038 .addReg(LHS)
1039 .addReg(RHS);
1040 updateValueMap(ICmp, ResultReg);
1041 return true;
1042}
1043
1044bool WebAssemblyFastISel::selectFCmp(const Instruction *I) {
1045 const FCmpInst *FCmp = cast<FCmpInst>(I);
1046
1047 unsigned LHS = getRegForValue(FCmp->getOperand(0));
1048 if (LHS == 0)
1049 return false;
1050
1051 unsigned RHS = getRegForValue(FCmp->getOperand(1));
1052 if (RHS == 0)
1053 return false;
1054
1055 bool F32 = getSimpleType(FCmp->getOperand(0)->getType()) != MVT::f64;
1056 unsigned Opc;
1057 bool Not = false;
1058 switch (FCmp->getPredicate()) {
1059 case FCmpInst::FCMP_OEQ:
1060 Opc = F32 ? WebAssembly::EQ_F32 : WebAssembly::EQ_F64;
1061 break;
1062 case FCmpInst::FCMP_UNE:
1063 Opc = F32 ? WebAssembly::NE_F32 : WebAssembly::NE_F64;
1064 break;
1065 case FCmpInst::FCMP_OGT:
1066 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1067 break;
1068 case FCmpInst::FCMP_OGE:
1069 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1070 break;
1071 case FCmpInst::FCMP_OLT:
1072 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1073 break;
1074 case FCmpInst::FCMP_OLE:
1075 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1076 break;
1077 case FCmpInst::FCMP_UGT:
1078 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1079 Not = true;
1080 break;
1081 case FCmpInst::FCMP_UGE:
1082 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1083 Not = true;
1084 break;
1085 case FCmpInst::FCMP_ULT:
1086 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1087 Not = true;
1088 break;
1089 case FCmpInst::FCMP_ULE:
1090 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1091 Not = true;
1092 break;
1093 default:
1094 return false;
1095 }
1096
1097 unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass);
1098 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1099 .addReg(LHS)
1100 .addReg(RHS);
1101
1102 if (Not)
1103 ResultReg = notValue(ResultReg);
1104
1105 updateValueMap(FCmp, ResultReg);
1106 return true;
1107}
1108
Dan Gohman2e644382016-05-10 17:39:48 +00001109bool WebAssemblyFastISel::selectBitCast(const Instruction *I) {
1110 // Target-independent code can handle this, except it doesn't set the dead
1111 // flag on the ARGUMENTS clobber, so we have to do that manually in order
1112 // to satisfy code that expects this of isBitcast() instructions.
1113 EVT VT = TLI.getValueType(DL, I->getOperand(0)->getType());
1114 EVT RetVT = TLI.getValueType(DL, I->getType());
1115 if (!VT.isSimple() || !RetVT.isSimple())
1116 return false;
Dan Gohman33e694a2016-05-12 04:19:09 +00001117
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001118 unsigned In = getRegForValue(I->getOperand(0));
1119 if (In == 0)
1120 return false;
1121
Dan Gohman33e694a2016-05-12 04:19:09 +00001122 if (VT == RetVT) {
1123 // No-op bitcast.
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001124 updateValueMap(I, In);
Dan Gohman33e694a2016-05-12 04:19:09 +00001125 return true;
1126 }
1127
Dan Gohman2e644382016-05-10 17:39:48 +00001128 unsigned Reg = fastEmit_ISD_BITCAST_r(VT.getSimpleVT(), RetVT.getSimpleVT(),
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001129 In, I->getOperand(0)->hasOneUse());
Dan Gohman2e644382016-05-10 17:39:48 +00001130 if (!Reg)
1131 return false;
1132 MachineBasicBlock::iterator Iter = FuncInfo.InsertPt;
1133 --Iter;
1134 assert(Iter->isBitcast());
1135 Iter->setPhysRegsDeadExcept(ArrayRef<unsigned>(), TRI);
1136 updateValueMap(I, Reg);
1137 return true;
1138}
1139
1140bool WebAssemblyFastISel::selectLoad(const Instruction *I) {
1141 const LoadInst *Load = cast<LoadInst>(I);
1142 if (Load->isAtomic())
1143 return false;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001144 if (!Subtarget->hasSIMD128() && Load->getType()->isVectorTy())
1145 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001146
1147 Address Addr;
1148 if (!computeAddress(Load->getPointerOperand(), Addr))
1149 return false;
1150
1151 // TODO: Fold a following sign-/zero-extend into the load instruction.
1152
1153 unsigned Opc;
1154 const TargetRegisterClass *RC;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001155 switch (getSimpleType(Load->getType())) {
1156 case MVT::i1:
1157 case MVT::i8:
1158 Opc = WebAssembly::LOAD8_U_I32;
1159 RC = &WebAssembly::I32RegClass;
Dan Gohman2e644382016-05-10 17:39:48 +00001160 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001161 case MVT::i16:
1162 Opc = WebAssembly::LOAD16_U_I32;
1163 RC = &WebAssembly::I32RegClass;
1164 break;
1165 case MVT::i32:
1166 Opc = WebAssembly::LOAD_I32;
1167 RC = &WebAssembly::I32RegClass;
1168 break;
1169 case MVT::i64:
1170 Opc = WebAssembly::LOAD_I64;
1171 RC = &WebAssembly::I64RegClass;
1172 break;
1173 case MVT::f32:
Dan Gohman2e644382016-05-10 17:39:48 +00001174 Opc = WebAssembly::LOAD_F32;
1175 RC = &WebAssembly::F32RegClass;
1176 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001177 case MVT::f64:
Dan Gohman2e644382016-05-10 17:39:48 +00001178 Opc = WebAssembly::LOAD_F64;
1179 RC = &WebAssembly::F64RegClass;
1180 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001181 default:
1182 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001183 }
1184
1185 materializeLoadStoreOperands(Addr);
1186
1187 unsigned ResultReg = createResultReg(RC);
1188 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1189 ResultReg);
1190
1191 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Load));
1192
1193 updateValueMap(Load, ResultReg);
1194 return true;
1195}
1196
1197bool WebAssemblyFastISel::selectStore(const Instruction *I) {
1198 const StoreInst *Store = cast<StoreInst>(I);
1199 if (Store->isAtomic())
1200 return false;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001201 if (!Subtarget->hasSIMD128() &&
1202 Store->getValueOperand()->getType()->isVectorTy())
1203 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001204
1205 Address Addr;
1206 if (!computeAddress(Store->getPointerOperand(), Addr))
1207 return false;
1208
1209 unsigned Opc;
Dan Gohman2e644382016-05-10 17:39:48 +00001210 bool VTIsi1 = false;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001211 switch (getSimpleType(Store->getValueOperand()->getType())) {
1212 case MVT::i1:
1213 VTIsi1 = true;
Dan Gohman861bec22018-02-09 22:59:01 +00001214 LLVM_FALLTHROUGH;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001215 case MVT::i8:
1216 Opc = WebAssembly::STORE8_I32;
Dan Gohman2e644382016-05-10 17:39:48 +00001217 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001218 case MVT::i16:
1219 Opc = WebAssembly::STORE16_I32;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001220 break;
1221 case MVT::i32:
1222 Opc = WebAssembly::STORE_I32;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001223 break;
1224 case MVT::i64:
1225 Opc = WebAssembly::STORE_I64;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001226 break;
1227 case MVT::f32:
Dan Gohman2e644382016-05-10 17:39:48 +00001228 Opc = WebAssembly::STORE_F32;
Dan Gohman2e644382016-05-10 17:39:48 +00001229 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001230 case MVT::f64:
Dan Gohman2e644382016-05-10 17:39:48 +00001231 Opc = WebAssembly::STORE_F64;
Dan Gohman2e644382016-05-10 17:39:48 +00001232 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001233 default:
1234 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001235 }
1236
1237 materializeLoadStoreOperands(Addr);
1238
1239 unsigned ValueReg = getRegForValue(Store->getValueOperand());
Derek Schuff732636d2016-08-04 18:01:52 +00001240 if (ValueReg == 0)
1241 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001242 if (VTIsi1)
Dan Gohman3a5ce732016-05-11 16:32:42 +00001243 ValueReg = maskI1Value(ValueReg, Store->getValueOperand());
Dan Gohman2e644382016-05-10 17:39:48 +00001244
Dan Gohman7f1bdb22016-10-06 22:08:28 +00001245 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
Dan Gohman2e644382016-05-10 17:39:48 +00001246
1247 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Store));
1248
1249 MIB.addReg(ValueReg);
1250 return true;
1251}
1252
1253bool WebAssemblyFastISel::selectBr(const Instruction *I) {
1254 const BranchInst *Br = cast<BranchInst>(I);
1255 if (Br->isUnconditional()) {
1256 MachineBasicBlock *MSucc = FuncInfo.MBBMap[Br->getSuccessor(0)];
1257 fastEmitBranch(MSucc, Br->getDebugLoc());
1258 return true;
1259 }
1260
1261 MachineBasicBlock *TBB = FuncInfo.MBBMap[Br->getSuccessor(0)];
1262 MachineBasicBlock *FBB = FuncInfo.MBBMap[Br->getSuccessor(1)];
1263
Dan Gohman33e694a2016-05-12 04:19:09 +00001264 bool Not;
1265 unsigned CondReg = getRegForI1Value(Br->getCondition(), Not);
Derek Schuff732636d2016-08-04 18:01:52 +00001266 if (CondReg == 0)
1267 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001268
Dan Gohman33e694a2016-05-12 04:19:09 +00001269 unsigned Opc = WebAssembly::BR_IF;
1270 if (Not)
1271 Opc = WebAssembly::BR_UNLESS;
1272
Dan Gohman2e644382016-05-10 17:39:48 +00001273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1274 .addMBB(TBB)
1275 .addReg(CondReg);
Derek Schuff39bf39f2016-08-02 23:16:09 +00001276
Dan Gohman2e644382016-05-10 17:39:48 +00001277 finishCondBranch(Br->getParent(), TBB, FBB);
1278 return true;
1279}
1280
1281bool WebAssemblyFastISel::selectRet(const Instruction *I) {
1282 if (!FuncInfo.CanLowerReturn)
1283 return false;
1284
1285 const ReturnInst *Ret = cast<ReturnInst>(I);
1286
1287 if (Ret->getNumOperands() == 0) {
1288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1289 TII.get(WebAssembly::RETURN_VOID));
1290 return true;
1291 }
1292
1293 Value *RV = Ret->getOperand(0);
Derek Schuff39bf39f2016-08-02 23:16:09 +00001294 if (!Subtarget->hasSIMD128() && RV->getType()->isVectorTy())
1295 return false;
1296
Dan Gohman2e644382016-05-10 17:39:48 +00001297 unsigned Opc;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001298 switch (getSimpleType(RV->getType())) {
Heejin Ahnf208f632018-09-05 01:27:38 +00001299 case MVT::i1:
1300 case MVT::i8:
1301 case MVT::i16:
1302 case MVT::i32:
Dan Gohman3a5ce732016-05-11 16:32:42 +00001303 Opc = WebAssembly::RETURN_I32;
Dan Gohman2e644382016-05-10 17:39:48 +00001304 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001305 case MVT::i64:
1306 Opc = WebAssembly::RETURN_I64;
1307 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001308 case MVT::f32:
1309 Opc = WebAssembly::RETURN_F32;
1310 break;
1311 case MVT::f64:
1312 Opc = WebAssembly::RETURN_F64;
1313 break;
1314 case MVT::v16i8:
1315 Opc = WebAssembly::RETURN_v16i8;
1316 break;
1317 case MVT::v8i16:
1318 Opc = WebAssembly::RETURN_v8i16;
1319 break;
1320 case MVT::v4i32:
1321 Opc = WebAssembly::RETURN_v4i32;
1322 break;
Derek Schuff51ed1312018-08-07 21:24:01 +00001323 case MVT::v2i64:
1324 Opc = WebAssembly::RETURN_v2i64;
1325 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001326 case MVT::v4f32:
1327 Opc = WebAssembly::RETURN_v4f32;
1328 break;
Derek Schuff51ed1312018-08-07 21:24:01 +00001329 case MVT::v2f64:
1330 Opc = WebAssembly::RETURN_v2f64;
1331 break;
Heejin Ahn0de58722018-03-08 04:05:37 +00001332 case MVT::ExceptRef:
1333 Opc = WebAssembly::RETURN_EXCEPT_REF;
1334 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001335 default:
1336 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001337 }
1338
Dan Gohman33e694a2016-05-12 04:19:09 +00001339 unsigned Reg;
1340 if (FuncInfo.Fn->getAttributes().hasAttribute(0, Attribute::SExt))
1341 Reg = getRegForSignedValue(RV);
1342 else if (FuncInfo.Fn->getAttributes().hasAttribute(0, Attribute::ZExt))
1343 Reg = getRegForUnsignedValue(RV);
1344 else
1345 Reg = getRegForValue(RV);
1346
Derek Schuff732636d2016-08-04 18:01:52 +00001347 if (Reg == 0)
1348 return false;
1349
Dan Gohman2e644382016-05-10 17:39:48 +00001350 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)).addReg(Reg);
1351 return true;
1352}
1353
1354bool WebAssemblyFastISel::selectUnreachable(const Instruction *I) {
1355 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1356 TII.get(WebAssembly::UNREACHABLE));
1357 return true;
1358}
1359
1360bool WebAssemblyFastISel::fastSelectInstruction(const Instruction *I) {
1361 switch (I->getOpcode()) {
Dan Gohman33e694a2016-05-12 04:19:09 +00001362 case Instruction::Call:
1363 if (selectCall(I))
1364 return true;
1365 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001366 case Instruction::Select:
1367 return selectSelect(I);
1368 case Instruction::Trunc:
1369 return selectTrunc(I);
1370 case Instruction::ZExt:
1371 return selectZExt(I);
1372 case Instruction::SExt:
1373 return selectSExt(I);
1374 case Instruction::ICmp:
1375 return selectICmp(I);
1376 case Instruction::FCmp:
1377 return selectFCmp(I);
1378 case Instruction::BitCast:
1379 return selectBitCast(I);
1380 case Instruction::Load:
1381 return selectLoad(I);
1382 case Instruction::Store:
1383 return selectStore(I);
1384 case Instruction::Br:
1385 return selectBr(I);
1386 case Instruction::Ret:
1387 return selectRet(I);
1388 case Instruction::Unreachable:
1389 return selectUnreachable(I);
1390 default:
1391 break;
Dan Gohman7b634842015-08-24 18:44:37 +00001392 }
1393
1394 // Fall back to target-independent instruction selection.
1395 return selectOperator(I, I->getOpcode());
1396}
1397
1398FastISel *WebAssembly::createFastISel(FunctionLoweringInfo &FuncInfo,
1399 const TargetLibraryInfo *LibInfo) {
1400 return new WebAssemblyFastISel(FuncInfo, LibInfo);
1401}