blob: 1705748673788caff15b9f3e80093af08d0b0252 [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:
Heejin Ahn18c56a02019-02-04 19:13:39 +000052 using BaseKind = enum { RegBase, FrameIndexBase };
Dan Gohman2e644382016-05-10 17:39:48 +000053
54 private:
Heejin Ahn18c56a02019-02-04 19:13:39 +000055 BaseKind Kind = RegBase;
Dan Gohman2e644382016-05-10 17:39:48 +000056 union {
57 unsigned Reg;
58 int FI;
59 } Base;
60
Heejin Ahn18c56a02019-02-04 19:13:39 +000061 int64_t Offset = 0;
Dan Gohman2e644382016-05-10 17:39:48 +000062
Heejin Ahn18c56a02019-02-04 19:13:39 +000063 const GlobalValue *GV = nullptr;
Dan Gohman2e644382016-05-10 17:39:48 +000064
65 public:
66 // Innocuous defaults for our address.
Heejin Ahn18c56a02019-02-04 19:13:39 +000067 Address() { 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
Heejin Ahn18c56a02019-02-04 19:13:39 +000094 void setOffset(int64_t NewOffset) {
95 assert(NewOffset >= 0 && "Offsets must be non-negative");
96 Offset = NewOffset;
Dan Gohman728926a2016-12-22 15:15:10 +000097 }
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);
Sam Clegg492f7522019-03-26 19:46:15 +0000154 bool materializeLoadStoreOperands(Address &Addr);
Dan Gohman2e644382016-05-10 17:39:48 +0000155 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;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000213 if (const auto *I = dyn_cast<Instruction>(Obj)) {
Dan Gohman2e644382016-05-10 17:39:48 +0000214 // 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 }
Heejin Ahn18c56a02019-02-04 19:13:39 +0000221 } else if (const auto *C = dyn_cast<ConstantExpr>(Obj)) {
Dan Gohman2e644382016-05-10 17:39:48 +0000222 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
Heejin Ahn18c56a02019-02-04 19:13:39 +0000232 if (const auto *GV = dyn_cast<GlobalValue>(Obj)) {
Dan Gohman2e644382016-05-10 17:39:48 +0000233 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 (;;) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000277 if (const auto *CI = dyn_cast<ConstantInt>(Op)) {
Dan Gohman2e644382016-05-10 17:39:48 +0000278 // 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.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000292 auto *CI = cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Dan Gohman2e644382016-05-10 17:39:48 +0000293 TmpOffset += CI->getSExtValue() * S;
294 // Iterate on the other operand.
295 Op = cast<AddOperator>(Op)->getOperand(0);
296 continue;
297 }
298 // Unsupported
299 goto unsupported_gep;
300 }
301 }
302 }
Dan Gohman728926a2016-12-22 15:15:10 +0000303 // Don't fold in negative offsets.
304 if (int64_t(TmpOffset) >= 0) {
305 // Try to grab the base operand now.
306 Addr.setOffset(TmpOffset);
307 if (computeAddress(U->getOperand(0), Addr))
308 return true;
309 }
Dan Gohman2e644382016-05-10 17:39:48 +0000310 // We failed, restore everything and try the other options.
311 Addr = SavedAddr;
312 unsupported_gep:
313 break;
314 }
315 case Instruction::Alloca: {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000316 const auto *AI = cast<AllocaInst>(Obj);
Dan Gohman2e644382016-05-10 17:39:48 +0000317 DenseMap<const AllocaInst *, int>::iterator SI =
318 FuncInfo.StaticAllocaMap.find(AI);
319 if (SI != FuncInfo.StaticAllocaMap.end()) {
Jacob Gravellea31ec612017-06-22 21:26:08 +0000320 if (Addr.isSet()) {
321 return false;
322 }
Dan Gohman2e644382016-05-10 17:39:48 +0000323 Addr.setKind(Address::FrameIndexBase);
324 Addr.setFI(SI->second);
325 return true;
326 }
327 break;
328 }
329 case Instruction::Add: {
330 // Adds of constants are common and easy enough.
331 const Value *LHS = U->getOperand(0);
332 const Value *RHS = U->getOperand(1);
333
334 if (isa<ConstantInt>(LHS))
335 std::swap(LHS, RHS);
336
Heejin Ahn18c56a02019-02-04 19:13:39 +0000337 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
Dan Gohman728926a2016-12-22 15:15:10 +0000338 uint64_t TmpOffset = Addr.getOffset() + CI->getSExtValue();
339 if (int64_t(TmpOffset) >= 0) {
340 Addr.setOffset(TmpOffset);
341 return computeAddress(LHS, Addr);
342 }
Dan Gohman2e644382016-05-10 17:39:48 +0000343 }
344
345 Address Backup = Addr;
346 if (computeAddress(LHS, Addr) && computeAddress(RHS, Addr))
347 return true;
348 Addr = Backup;
349
350 break;
351 }
352 case Instruction::Sub: {
353 // Subs of constants are common and easy enough.
354 const Value *LHS = U->getOperand(0);
355 const Value *RHS = U->getOperand(1);
356
Heejin Ahn18c56a02019-02-04 19:13:39 +0000357 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
Dan Gohman728926a2016-12-22 15:15:10 +0000358 int64_t TmpOffset = Addr.getOffset() - CI->getSExtValue();
359 if (TmpOffset >= 0) {
360 Addr.setOffset(TmpOffset);
361 return computeAddress(LHS, Addr);
362 }
Dan Gohman2e644382016-05-10 17:39:48 +0000363 }
364 break;
365 }
366 }
Jacob Gravellea31ec612017-06-22 21:26:08 +0000367 if (Addr.isSet()) {
368 return false;
369 }
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000370 unsigned Reg = getRegForValue(Obj);
371 if (Reg == 0)
372 return false;
373 Addr.setReg(Reg);
Dan Gohman2e644382016-05-10 17:39:48 +0000374 return Addr.getReg() != 0;
375}
376
Sam Clegg492f7522019-03-26 19:46:15 +0000377bool WebAssemblyFastISel::materializeLoadStoreOperands(Address &Addr) {
Dan Gohman2e644382016-05-10 17:39:48 +0000378 if (Addr.isRegBase()) {
379 unsigned Reg = Addr.getReg();
380 if (Reg == 0) {
Sam Clegg492f7522019-03-26 19:46:15 +0000381 const GlobalValue *GV = Addr.getGlobalValue();
382 if (GV && TLI.isPositionIndependent())
383 return false;
Heejin Ahnf208f632018-09-05 01:27:38 +0000384 Reg = createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
385 : &WebAssembly::I32RegClass);
386 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
387 : WebAssembly::CONST_I32;
Dan Gohman2e644382016-05-10 17:39:48 +0000388 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), Reg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000389 .addImm(0);
Dan Gohman2e644382016-05-10 17:39:48 +0000390 Addr.setReg(Reg);
391 }
392 }
Sam Clegg492f7522019-03-26 19:46:15 +0000393 return true;
Dan Gohman2e644382016-05-10 17:39:48 +0000394}
395
396void WebAssemblyFastISel::addLoadStoreOperands(const Address &Addr,
397 const MachineInstrBuilder &MIB,
398 MachineMemOperand *MMO) {
Dan Gohman48abaa92016-10-25 00:17:11 +0000399 // Set the alignment operand (this is rewritten in SetP2AlignOperands).
400 // TODO: Disable SetP2AlignOperands for FastISel and just do it here.
401 MIB.addImm(0);
402
Dan Gohman2e644382016-05-10 17:39:48 +0000403 if (const GlobalValue *GV = Addr.getGlobalValue())
404 MIB.addGlobalAddress(GV, Addr.getOffset());
405 else
406 MIB.addImm(Addr.getOffset());
407
408 if (Addr.isRegBase())
409 MIB.addReg(Addr.getReg());
410 else
411 MIB.addFrameIndex(Addr.getFI());
412
Dan Gohman2e644382016-05-10 17:39:48 +0000413 MIB.addMemOperand(MMO);
414}
415
Dan Gohman3a5ce732016-05-11 16:32:42 +0000416unsigned WebAssemblyFastISel::maskI1Value(unsigned Reg, const Value *V) {
417 return zeroExtendToI32(Reg, V, MVT::i1);
Dan Gohman2e644382016-05-10 17:39:48 +0000418}
419
Dan Gohman33e694a2016-05-12 04:19:09 +0000420unsigned WebAssemblyFastISel::getRegForI1Value(const Value *V, bool &Not) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000421 if (const auto *ICmp = dyn_cast<ICmpInst>(V))
Dan Gohman33e694a2016-05-12 04:19:09 +0000422 if (const ConstantInt *C = dyn_cast<ConstantInt>(ICmp->getOperand(1)))
423 if (ICmp->isEquality() && C->isZero() && C->getType()->isIntegerTy(32)) {
424 Not = ICmp->isTrueWhenEqual();
425 return getRegForValue(ICmp->getOperand(0));
426 }
427
Sanjay Patel47a52a02018-10-23 16:05:09 +0000428 Value *NotV;
429 if (match(V, m_Not(m_Value(NotV))) && V->getType()->isIntegerTy(32)) {
Dan Gohman33e694a2016-05-12 04:19:09 +0000430 Not = true;
Sanjay Patel47a52a02018-10-23 16:05:09 +0000431 return getRegForValue(NotV);
Dan Gohman33e694a2016-05-12 04:19:09 +0000432 }
433
434 Not = false;
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000435 unsigned Reg = getRegForValue(V);
436 if (Reg == 0)
437 return 0;
438 return maskI1Value(Reg, V);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000439}
440
441unsigned WebAssemblyFastISel::zeroExtendToI32(unsigned Reg, const Value *V,
442 MVT::SimpleValueType From) {
Derek Schuff732636d2016-08-04 18:01:52 +0000443 if (Reg == 0)
444 return 0;
445
Dan Gohman3a5ce732016-05-11 16:32:42 +0000446 switch (From) {
447 case MVT::i1:
Thomas Lively2a47e032019-01-14 22:03:43 +0000448 // If the value is naturally an i1, we don't need to mask it. We only know
449 // if a value is naturally an i1 if it is definitely lowered by FastISel,
450 // not a DAG ISel fallback.
451 if (V != nullptr && isa<Argument>(V) && cast<Argument>(V)->hasZExtAttr())
452 return copyValue(Reg);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000453 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000454 case MVT::i8:
455 case MVT::i16:
Dan Gohman3a5ce732016-05-11 16:32:42 +0000456 break;
457 case MVT::i32:
Dan Gohman33e694a2016-05-12 04:19:09 +0000458 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000459 default:
460 return 0;
461 }
462
463 unsigned Imm = createResultReg(&WebAssembly::I32RegClass);
464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
465 TII.get(WebAssembly::CONST_I32), Imm)
Heejin Ahnf208f632018-09-05 01:27:38 +0000466 .addImm(~(~uint64_t(0) << MVT(From).getSizeInBits()));
Dan Gohman3a5ce732016-05-11 16:32:42 +0000467
468 unsigned Result = createResultReg(&WebAssembly::I32RegClass);
469 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
470 TII.get(WebAssembly::AND_I32), Result)
Heejin Ahnf208f632018-09-05 01:27:38 +0000471 .addReg(Reg)
472 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000473
474 return Result;
475}
476
477unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
478 MVT::SimpleValueType From) {
Derek Schuff732636d2016-08-04 18:01:52 +0000479 if (Reg == 0)
480 return 0;
481
Dan Gohman3a5ce732016-05-11 16:32:42 +0000482 switch (From) {
483 case MVT::i1:
484 case MVT::i8:
485 case MVT::i16:
486 break;
487 case MVT::i32:
Dan Gohman33e694a2016-05-12 04:19:09 +0000488 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000489 default:
Dan Gohman33e694a2016-05-12 04:19:09 +0000490 return 0;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000491 }
492
493 unsigned Imm = createResultReg(&WebAssembly::I32RegClass);
494 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
495 TII.get(WebAssembly::CONST_I32), Imm)
Heejin Ahnf208f632018-09-05 01:27:38 +0000496 .addImm(32 - MVT(From).getSizeInBits());
Dan Gohman3a5ce732016-05-11 16:32:42 +0000497
498 unsigned Left = createResultReg(&WebAssembly::I32RegClass);
499 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
500 TII.get(WebAssembly::SHL_I32), Left)
Heejin Ahnf208f632018-09-05 01:27:38 +0000501 .addReg(Reg)
502 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000503
504 unsigned Right = createResultReg(&WebAssembly::I32RegClass);
505 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
506 TII.get(WebAssembly::SHR_S_I32), Right)
Heejin Ahnf208f632018-09-05 01:27:38 +0000507 .addReg(Left)
508 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000509
510 return Right;
511}
512
513unsigned WebAssemblyFastISel::zeroExtend(unsigned Reg, const Value *V,
514 MVT::SimpleValueType From,
515 MVT::SimpleValueType To) {
516 if (To == MVT::i64) {
517 if (From == MVT::i64)
Dan Gohman33e694a2016-05-12 04:19:09 +0000518 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000519
520 Reg = zeroExtendToI32(Reg, V, From);
521
522 unsigned Result = createResultReg(&WebAssembly::I64RegClass);
523 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
524 TII.get(WebAssembly::I64_EXTEND_U_I32), Result)
525 .addReg(Reg);
526 return Result;
527 }
528
Dan Gohman3a5ce732016-05-11 16:32:42 +0000529 return zeroExtendToI32(Reg, V, From);
530}
531
532unsigned WebAssemblyFastISel::signExtend(unsigned Reg, const Value *V,
533 MVT::SimpleValueType From,
534 MVT::SimpleValueType To) {
535 if (To == MVT::i64) {
536 if (From == MVT::i64)
Dan Gohman33e694a2016-05-12 04:19:09 +0000537 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000538
539 Reg = signExtendToI32(Reg, V, From);
540
541 unsigned Result = createResultReg(&WebAssembly::I64RegClass);
542 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
543 TII.get(WebAssembly::I64_EXTEND_S_I32), Result)
544 .addReg(Reg);
545 return Result;
546 }
547
Dan Gohman3a5ce732016-05-11 16:32:42 +0000548 return signExtendToI32(Reg, V, From);
549}
550
551unsigned WebAssemblyFastISel::getRegForUnsignedValue(const Value *V) {
552 MVT::SimpleValueType From = getSimpleType(V->getType());
553 MVT::SimpleValueType To = getLegalType(From);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000554 unsigned VReg = getRegForValue(V);
555 if (VReg == 0)
556 return 0;
557 return zeroExtend(VReg, V, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000558}
559
560unsigned WebAssemblyFastISel::getRegForSignedValue(const Value *V) {
561 MVT::SimpleValueType From = getSimpleType(V->getType());
562 MVT::SimpleValueType To = getLegalType(From);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000563 unsigned VReg = getRegForValue(V);
564 if (VReg == 0)
565 return 0;
566 return signExtend(VReg, V, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000567}
568
569unsigned WebAssemblyFastISel::getRegForPromotedValue(const Value *V,
570 bool IsSigned) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000571 return IsSigned ? getRegForSignedValue(V) : getRegForUnsignedValue(V);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000572}
573
574unsigned WebAssemblyFastISel::notValue(unsigned Reg) {
Dan Gohman33e694a2016-05-12 04:19:09 +0000575 assert(MRI.getRegClass(Reg) == &WebAssembly::I32RegClass);
576
Dan Gohman3a5ce732016-05-11 16:32:42 +0000577 unsigned NotReg = createResultReg(&WebAssembly::I32RegClass);
578 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
579 TII.get(WebAssembly::EQZ_I32), NotReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000580 .addReg(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000581 return NotReg;
Dan Gohman2e644382016-05-10 17:39:48 +0000582}
583
Dan Gohman33e694a2016-05-12 04:19:09 +0000584unsigned WebAssemblyFastISel::copyValue(unsigned Reg) {
585 unsigned ResultReg = createResultReg(MRI.getRegClass(Reg));
Heejin Ahnf208f632018-09-05 01:27:38 +0000586 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(WebAssembly::COPY),
587 ResultReg)
588 .addReg(Reg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000589 return ResultReg;
590}
591
Dan Gohman2e644382016-05-10 17:39:48 +0000592unsigned WebAssemblyFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
593 DenseMap<const AllocaInst *, int>::iterator SI =
594 FuncInfo.StaticAllocaMap.find(AI);
595
596 if (SI != FuncInfo.StaticAllocaMap.end()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000597 unsigned ResultReg =
598 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
599 : &WebAssembly::I32RegClass);
600 unsigned Opc =
601 Subtarget->hasAddr64() ? WebAssembly::COPY_I64 : WebAssembly::COPY_I32;
Dan Gohman2e644382016-05-10 17:39:48 +0000602 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
603 .addFrameIndex(SI->second);
604 return ResultReg;
605 }
606
607 return 0;
608}
609
610unsigned WebAssemblyFastISel::fastMaterializeConstant(const Constant *C) {
Sam Clegg492f7522019-03-26 19:46:15 +0000611 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
612 if (TLI.isPositionIndependent())
613 return 0;
Heejin Ahnf208f632018-09-05 01:27:38 +0000614 unsigned ResultReg =
615 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
616 : &WebAssembly::I32RegClass);
617 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
618 : WebAssembly::CONST_I32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000619 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000620 .addGlobalAddress(GV);
Dan Gohman33e694a2016-05-12 04:19:09 +0000621 return ResultReg;
Dan Gohman2e644382016-05-10 17:39:48 +0000622 }
623
624 // Let target-independent code handle it.
625 return 0;
626}
627
Dan Gohman33e694a2016-05-12 04:19:09 +0000628bool WebAssemblyFastISel::fastLowerArguments() {
629 if (!FuncInfo.CanLowerReturn)
630 return false;
631
632 const Function *F = FuncInfo.Fn;
633 if (F->isVarArg())
634 return false;
635
Heejin Ahn18c56a02019-02-04 19:13:39 +0000636 unsigned I = 0;
Dan Gohman33e694a2016-05-12 04:19:09 +0000637 for (auto const &Arg : F->args()) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000638 const AttributeList &Attrs = F->getAttributes();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000639 if (Attrs.hasParamAttribute(I, Attribute::ByVal) ||
640 Attrs.hasParamAttribute(I, Attribute::SwiftSelf) ||
641 Attrs.hasParamAttribute(I, Attribute::SwiftError) ||
642 Attrs.hasParamAttribute(I, Attribute::InAlloca) ||
643 Attrs.hasParamAttribute(I, Attribute::Nest))
Dan Gohman33e694a2016-05-12 04:19:09 +0000644 return false;
645
646 Type *ArgTy = Arg.getType();
Derek Schuff39bf39f2016-08-02 23:16:09 +0000647 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
648 return false;
649 if (!Subtarget->hasSIMD128() && ArgTy->isVectorTy())
Dan Gohman33e694a2016-05-12 04:19:09 +0000650 return false;
651
652 unsigned Opc;
653 const TargetRegisterClass *RC;
654 switch (getSimpleType(ArgTy)) {
655 case MVT::i1:
656 case MVT::i8:
657 case MVT::i16:
658 case MVT::i32:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000659 Opc = WebAssembly::ARGUMENT_i32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000660 RC = &WebAssembly::I32RegClass;
661 break;
662 case MVT::i64:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000663 Opc = WebAssembly::ARGUMENT_i64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000664 RC = &WebAssembly::I64RegClass;
665 break;
666 case MVT::f32:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000667 Opc = WebAssembly::ARGUMENT_f32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000668 RC = &WebAssembly::F32RegClass;
669 break;
670 case MVT::f64:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000671 Opc = WebAssembly::ARGUMENT_f64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000672 RC = &WebAssembly::F64RegClass;
673 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000674 case MVT::v16i8:
675 Opc = WebAssembly::ARGUMENT_v16i8;
676 RC = &WebAssembly::V128RegClass;
677 break;
678 case MVT::v8i16:
679 Opc = WebAssembly::ARGUMENT_v8i16;
680 RC = &WebAssembly::V128RegClass;
681 break;
682 case MVT::v4i32:
683 Opc = WebAssembly::ARGUMENT_v4i32;
684 RC = &WebAssembly::V128RegClass;
685 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000686 case MVT::v2i64:
687 Opc = WebAssembly::ARGUMENT_v2i64;
688 RC = &WebAssembly::V128RegClass;
689 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000690 case MVT::v4f32:
691 Opc = WebAssembly::ARGUMENT_v4f32;
692 RC = &WebAssembly::V128RegClass;
693 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000694 case MVT::v2f64:
695 Opc = WebAssembly::ARGUMENT_v2f64;
696 RC = &WebAssembly::V128RegClass;
697 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000698 case MVT::ExceptRef:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000699 Opc = WebAssembly::ARGUMENT_ExceptRef;
Heejin Ahn0de58722018-03-08 04:05:37 +0000700 RC = &WebAssembly::EXCEPT_REFRegClass;
701 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000702 default:
703 return false;
704 }
705 unsigned ResultReg = createResultReg(RC);
706 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahn18c56a02019-02-04 19:13:39 +0000707 .addImm(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000708 updateValueMap(&Arg, ResultReg);
709
Heejin Ahn18c56a02019-02-04 19:13:39 +0000710 ++I;
Dan Gohman33e694a2016-05-12 04:19:09 +0000711 }
712
713 MRI.addLiveIn(WebAssembly::ARGUMENTS);
714
715 auto *MFI = MF->getInfo<WebAssemblyFunctionInfo>();
Dan Gohmanb8184822018-05-22 04:58:36 +0000716 for (auto const &Arg : F->args()) {
717 MVT::SimpleValueType ArgTy = getLegalType(getSimpleType(Arg.getType()));
718 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
719 MFI->clearParamsAndResults();
720 return false;
721 }
722 MFI->addParam(ArgTy);
723 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000724
Dan Gohman4576dc02018-04-17 20:46:42 +0000725 if (!F->getReturnType()->isVoidTy()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000726 MVT::SimpleValueType RetTy =
727 getLegalType(getSimpleType(F->getReturnType()));
Dan Gohmanb8184822018-05-22 04:58:36 +0000728 if (RetTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
729 MFI->clearParamsAndResults();
Dan Gohman4576dc02018-04-17 20:46:42 +0000730 return false;
Dan Gohmanb8184822018-05-22 04:58:36 +0000731 }
732 MFI->addResult(RetTy);
Dan Gohman4576dc02018-04-17 20:46:42 +0000733 }
Dan Gohman6055fba2017-01-09 23:09:38 +0000734
Dan Gohman33e694a2016-05-12 04:19:09 +0000735 return true;
736}
737
738bool WebAssemblyFastISel::selectCall(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000739 const auto *Call = cast<CallInst>(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000740
741 if (Call->isMustTailCall() || Call->isInlineAsm() ||
742 Call->getFunctionType()->isVarArg())
743 return false;
744
745 Function *Func = Call->getCalledFunction();
746 if (Func && Func->isIntrinsic())
747 return false;
748
Jacob Gravelle690b76e2017-08-24 19:53:44 +0000749 bool IsDirect = Func != nullptr;
750 if (!IsDirect && isa<ConstantExpr>(Call->getCalledValue()))
751 return false;
752
Dan Gohman33e694a2016-05-12 04:19:09 +0000753 FunctionType *FuncTy = Call->getFunctionType();
754 unsigned Opc;
Dan Gohman33e694a2016-05-12 04:19:09 +0000755 bool IsVoid = FuncTy->getReturnType()->isVoidTy();
756 unsigned ResultReg;
757 if (IsVoid) {
Derek Schuff6f697832016-10-21 16:38:07 +0000758 Opc = IsDirect ? WebAssembly::CALL_VOID : WebAssembly::PCALL_INDIRECT_VOID;
Dan Gohman33e694a2016-05-12 04:19:09 +0000759 } else {
Derek Schuff39bf39f2016-08-02 23:16:09 +0000760 if (!Subtarget->hasSIMD128() && Call->getType()->isVectorTy())
761 return false;
762
Dan Gohman33e694a2016-05-12 04:19:09 +0000763 MVT::SimpleValueType RetTy = getSimpleType(Call->getType());
764 switch (RetTy) {
765 case MVT::i1:
766 case MVT::i8:
767 case MVT::i16:
768 case MVT::i32:
Derek Schuff6f697832016-10-21 16:38:07 +0000769 Opc = IsDirect ? WebAssembly::CALL_I32 : WebAssembly::PCALL_INDIRECT_I32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000770 ResultReg = createResultReg(&WebAssembly::I32RegClass);
771 break;
772 case MVT::i64:
Derek Schuff6f697832016-10-21 16:38:07 +0000773 Opc = IsDirect ? WebAssembly::CALL_I64 : WebAssembly::PCALL_INDIRECT_I64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000774 ResultReg = createResultReg(&WebAssembly::I64RegClass);
775 break;
776 case MVT::f32:
Derek Schuff6f697832016-10-21 16:38:07 +0000777 Opc = IsDirect ? WebAssembly::CALL_F32 : WebAssembly::PCALL_INDIRECT_F32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000778 ResultReg = createResultReg(&WebAssembly::F32RegClass);
779 break;
780 case MVT::f64:
Derek Schuff6f697832016-10-21 16:38:07 +0000781 Opc = IsDirect ? WebAssembly::CALL_F64 : WebAssembly::PCALL_INDIRECT_F64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000782 ResultReg = createResultReg(&WebAssembly::F64RegClass);
783 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000784 case MVT::v16i8:
Heejin Ahnf208f632018-09-05 01:27:38 +0000785 Opc = IsDirect ? WebAssembly::CALL_v16i8
786 : WebAssembly::PCALL_INDIRECT_v16i8;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000787 ResultReg = createResultReg(&WebAssembly::V128RegClass);
788 break;
789 case MVT::v8i16:
Heejin Ahnf208f632018-09-05 01:27:38 +0000790 Opc = IsDirect ? WebAssembly::CALL_v8i16
791 : WebAssembly::PCALL_INDIRECT_v8i16;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000792 ResultReg = createResultReg(&WebAssembly::V128RegClass);
793 break;
794 case MVT::v4i32:
Heejin Ahnf208f632018-09-05 01:27:38 +0000795 Opc = IsDirect ? WebAssembly::CALL_v4i32
796 : WebAssembly::PCALL_INDIRECT_v4i32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000797 ResultReg = createResultReg(&WebAssembly::V128RegClass);
798 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000799 case MVT::v2i64:
Heejin Ahnf208f632018-09-05 01:27:38 +0000800 Opc = IsDirect ? WebAssembly::CALL_v2i64
801 : WebAssembly::PCALL_INDIRECT_v2i64;
Derek Schuff51ed1312018-08-07 21:24:01 +0000802 ResultReg = createResultReg(&WebAssembly::V128RegClass);
803 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000804 case MVT::v4f32:
Heejin Ahnf208f632018-09-05 01:27:38 +0000805 Opc = IsDirect ? WebAssembly::CALL_v4f32
806 : WebAssembly::PCALL_INDIRECT_v4f32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000807 ResultReg = createResultReg(&WebAssembly::V128RegClass);
808 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000809 case MVT::v2f64:
Heejin Ahnf208f632018-09-05 01:27:38 +0000810 Opc = IsDirect ? WebAssembly::CALL_v2f64
811 : WebAssembly::PCALL_INDIRECT_v2f64;
Derek Schuff51ed1312018-08-07 21:24:01 +0000812 ResultReg = createResultReg(&WebAssembly::V128RegClass);
813 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000814 case MVT::ExceptRef:
815 Opc = IsDirect ? WebAssembly::CALL_EXCEPT_REF
816 : WebAssembly::PCALL_INDIRECT_EXCEPT_REF;
817 ResultReg = createResultReg(&WebAssembly::EXCEPT_REFRegClass);
818 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000819 default:
820 return false;
821 }
822 }
823
824 SmallVector<unsigned, 8> Args;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000825 for (unsigned I = 0, E = Call->getNumArgOperands(); I < E; ++I) {
826 Value *V = Call->getArgOperand(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000827 MVT::SimpleValueType ArgTy = getSimpleType(V->getType());
828 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
829 return false;
830
Reid Klecknerb5180542017-03-21 16:57:19 +0000831 const AttributeList &Attrs = Call->getAttributes();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000832 if (Attrs.hasParamAttribute(I, Attribute::ByVal) ||
833 Attrs.hasParamAttribute(I, Attribute::SwiftSelf) ||
834 Attrs.hasParamAttribute(I, Attribute::SwiftError) ||
835 Attrs.hasParamAttribute(I, Attribute::InAlloca) ||
836 Attrs.hasParamAttribute(I, Attribute::Nest))
Dan Gohman33e694a2016-05-12 04:19:09 +0000837 return false;
838
839 unsigned Reg;
840
Heejin Ahn18c56a02019-02-04 19:13:39 +0000841 if (Attrs.hasParamAttribute(I, Attribute::SExt))
Dan Gohman33e694a2016-05-12 04:19:09 +0000842 Reg = getRegForSignedValue(V);
Heejin Ahn18c56a02019-02-04 19:13:39 +0000843 else if (Attrs.hasParamAttribute(I, Attribute::ZExt))
Dan Gohman33e694a2016-05-12 04:19:09 +0000844 Reg = getRegForUnsignedValue(V);
845 else
846 Reg = getRegForValue(V);
847
848 if (Reg == 0)
849 return false;
850
851 Args.push_back(Reg);
852 }
853
854 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
855
856 if (!IsVoid)
857 MIB.addReg(ResultReg, RegState::Define);
858
859 if (IsDirect)
860 MIB.addGlobalAddress(Func);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000861 else {
862 unsigned Reg = getRegForValue(Call->getCalledValue());
863 if (Reg == 0)
864 return false;
865 MIB.addReg(Reg);
866 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000867
868 for (unsigned ArgReg : Args)
869 MIB.addReg(ArgReg);
870
871 if (!IsVoid)
872 updateValueMap(Call, ResultReg);
873 return true;
874}
875
876bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000877 const auto *Select = cast<SelectInst>(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000878
879 bool Not;
Heejin Ahnf208f632018-09-05 01:27:38 +0000880 unsigned CondReg = getRegForI1Value(Select->getCondition(), Not);
Dan Gohman33e694a2016-05-12 04:19:09 +0000881 if (CondReg == 0)
882 return false;
883
Heejin Ahnf208f632018-09-05 01:27:38 +0000884 unsigned TrueReg = getRegForValue(Select->getTrueValue());
Dan Gohman33e694a2016-05-12 04:19:09 +0000885 if (TrueReg == 0)
886 return false;
887
888 unsigned FalseReg = getRegForValue(Select->getFalseValue());
889 if (FalseReg == 0)
890 return false;
891
892 if (Not)
893 std::swap(TrueReg, FalseReg);
894
895 unsigned Opc;
896 const TargetRegisterClass *RC;
897 switch (getSimpleType(Select->getType())) {
898 case MVT::i1:
899 case MVT::i8:
900 case MVT::i16:
901 case MVT::i32:
902 Opc = WebAssembly::SELECT_I32;
903 RC = &WebAssembly::I32RegClass;
904 break;
905 case MVT::i64:
906 Opc = WebAssembly::SELECT_I64;
907 RC = &WebAssembly::I64RegClass;
908 break;
909 case MVT::f32:
910 Opc = WebAssembly::SELECT_F32;
911 RC = &WebAssembly::F32RegClass;
912 break;
913 case MVT::f64:
914 Opc = WebAssembly::SELECT_F64;
915 RC = &WebAssembly::F64RegClass;
916 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000917 case MVT::ExceptRef:
918 Opc = WebAssembly::SELECT_EXCEPT_REF;
919 RC = &WebAssembly::EXCEPT_REFRegClass;
920 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000921 default:
922 return false;
923 }
924
925 unsigned ResultReg = createResultReg(RC);
926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000927 .addReg(TrueReg)
928 .addReg(FalseReg)
929 .addReg(CondReg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000930
931 updateValueMap(Select, ResultReg);
932 return true;
933}
934
935bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000936 const auto *Trunc = cast<TruncInst>(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000937
938 unsigned Reg = getRegForValue(Trunc->getOperand(0));
939 if (Reg == 0)
940 return false;
941
942 if (Trunc->getOperand(0)->getType()->isIntegerTy(64)) {
943 unsigned Result = createResultReg(&WebAssembly::I32RegClass);
944 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
945 TII.get(WebAssembly::I32_WRAP_I64), Result)
946 .addReg(Reg);
947 Reg = Result;
948 }
949
950 updateValueMap(Trunc, Reg);
951 return true;
952}
953
Dan Gohman3a5ce732016-05-11 16:32:42 +0000954bool WebAssemblyFastISel::selectZExt(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000955 const auto *ZExt = cast<ZExtInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000956
Dan Gohman33e694a2016-05-12 04:19:09 +0000957 const Value *Op = ZExt->getOperand(0);
958 MVT::SimpleValueType From = getSimpleType(Op->getType());
959 MVT::SimpleValueType To = getLegalType(getSimpleType(ZExt->getType()));
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000960 unsigned In = getRegForValue(Op);
961 if (In == 0)
962 return false;
963 unsigned Reg = zeroExtend(In, Op, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000964 if (Reg == 0)
965 return false;
966
967 updateValueMap(ZExt, Reg);
968 return true;
969}
970
971bool WebAssemblyFastISel::selectSExt(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000972 const auto *SExt = cast<SExtInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000973
Dan Gohman33e694a2016-05-12 04:19:09 +0000974 const Value *Op = SExt->getOperand(0);
975 MVT::SimpleValueType From = getSimpleType(Op->getType());
976 MVT::SimpleValueType To = getLegalType(getSimpleType(SExt->getType()));
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000977 unsigned In = getRegForValue(Op);
978 if (In == 0)
979 return false;
980 unsigned Reg = signExtend(In, Op, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000981 if (Reg == 0)
982 return false;
983
984 updateValueMap(SExt, Reg);
985 return true;
986}
987
988bool WebAssemblyFastISel::selectICmp(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000989 const auto *ICmp = cast<ICmpInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000990
991 bool I32 = getSimpleType(ICmp->getOperand(0)->getType()) != MVT::i64;
992 unsigned Opc;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000993 bool IsSigned = false;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000994 switch (ICmp->getPredicate()) {
995 case ICmpInst::ICMP_EQ:
996 Opc = I32 ? WebAssembly::EQ_I32 : WebAssembly::EQ_I64;
997 break;
998 case ICmpInst::ICMP_NE:
999 Opc = I32 ? WebAssembly::NE_I32 : WebAssembly::NE_I64;
1000 break;
1001 case ICmpInst::ICMP_UGT:
1002 Opc = I32 ? WebAssembly::GT_U_I32 : WebAssembly::GT_U_I64;
1003 break;
1004 case ICmpInst::ICMP_UGE:
1005 Opc = I32 ? WebAssembly::GE_U_I32 : WebAssembly::GE_U_I64;
1006 break;
1007 case ICmpInst::ICMP_ULT:
1008 Opc = I32 ? WebAssembly::LT_U_I32 : WebAssembly::LT_U_I64;
1009 break;
1010 case ICmpInst::ICMP_ULE:
1011 Opc = I32 ? WebAssembly::LE_U_I32 : WebAssembly::LE_U_I64;
1012 break;
1013 case ICmpInst::ICMP_SGT:
1014 Opc = I32 ? WebAssembly::GT_S_I32 : WebAssembly::GT_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001015 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001016 break;
1017 case ICmpInst::ICMP_SGE:
1018 Opc = I32 ? WebAssembly::GE_S_I32 : WebAssembly::GE_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001019 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001020 break;
1021 case ICmpInst::ICMP_SLT:
1022 Opc = I32 ? WebAssembly::LT_S_I32 : WebAssembly::LT_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001023 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001024 break;
1025 case ICmpInst::ICMP_SLE:
1026 Opc = I32 ? WebAssembly::LE_S_I32 : WebAssembly::LE_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001027 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001028 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001029 default:
1030 return false;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001031 }
1032
Heejin Ahn18c56a02019-02-04 19:13:39 +00001033 unsigned LHS = getRegForPromotedValue(ICmp->getOperand(0), IsSigned);
Dan Gohman3a5ce732016-05-11 16:32:42 +00001034 if (LHS == 0)
1035 return false;
1036
Heejin Ahn18c56a02019-02-04 19:13:39 +00001037 unsigned RHS = getRegForPromotedValue(ICmp->getOperand(1), IsSigned);
Dan Gohman3a5ce732016-05-11 16:32:42 +00001038 if (RHS == 0)
1039 return false;
1040
1041 unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass);
1042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1043 .addReg(LHS)
1044 .addReg(RHS);
1045 updateValueMap(ICmp, ResultReg);
1046 return true;
1047}
1048
1049bool WebAssemblyFastISel::selectFCmp(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001050 const auto *FCmp = cast<FCmpInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +00001051
1052 unsigned LHS = getRegForValue(FCmp->getOperand(0));
1053 if (LHS == 0)
1054 return false;
1055
1056 unsigned RHS = getRegForValue(FCmp->getOperand(1));
1057 if (RHS == 0)
1058 return false;
1059
1060 bool F32 = getSimpleType(FCmp->getOperand(0)->getType()) != MVT::f64;
1061 unsigned Opc;
1062 bool Not = false;
1063 switch (FCmp->getPredicate()) {
1064 case FCmpInst::FCMP_OEQ:
1065 Opc = F32 ? WebAssembly::EQ_F32 : WebAssembly::EQ_F64;
1066 break;
1067 case FCmpInst::FCMP_UNE:
1068 Opc = F32 ? WebAssembly::NE_F32 : WebAssembly::NE_F64;
1069 break;
1070 case FCmpInst::FCMP_OGT:
1071 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1072 break;
1073 case FCmpInst::FCMP_OGE:
1074 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1075 break;
1076 case FCmpInst::FCMP_OLT:
1077 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1078 break;
1079 case FCmpInst::FCMP_OLE:
1080 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1081 break;
1082 case FCmpInst::FCMP_UGT:
1083 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1084 Not = true;
1085 break;
1086 case FCmpInst::FCMP_UGE:
1087 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1088 Not = true;
1089 break;
1090 case FCmpInst::FCMP_ULT:
1091 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1092 Not = true;
1093 break;
1094 case FCmpInst::FCMP_ULE:
1095 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1096 Not = true;
1097 break;
1098 default:
1099 return false;
1100 }
1101
1102 unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass);
1103 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1104 .addReg(LHS)
1105 .addReg(RHS);
1106
1107 if (Not)
1108 ResultReg = notValue(ResultReg);
1109
1110 updateValueMap(FCmp, ResultReg);
1111 return true;
1112}
1113
Dan Gohman2e644382016-05-10 17:39:48 +00001114bool WebAssemblyFastISel::selectBitCast(const Instruction *I) {
1115 // Target-independent code can handle this, except it doesn't set the dead
1116 // flag on the ARGUMENTS clobber, so we have to do that manually in order
1117 // to satisfy code that expects this of isBitcast() instructions.
1118 EVT VT = TLI.getValueType(DL, I->getOperand(0)->getType());
1119 EVT RetVT = TLI.getValueType(DL, I->getType());
1120 if (!VT.isSimple() || !RetVT.isSimple())
1121 return false;
Dan Gohman33e694a2016-05-12 04:19:09 +00001122
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001123 unsigned In = getRegForValue(I->getOperand(0));
1124 if (In == 0)
1125 return false;
1126
Dan Gohman33e694a2016-05-12 04:19:09 +00001127 if (VT == RetVT) {
1128 // No-op bitcast.
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001129 updateValueMap(I, In);
Dan Gohman33e694a2016-05-12 04:19:09 +00001130 return true;
1131 }
1132
Dan Gohman2e644382016-05-10 17:39:48 +00001133 unsigned Reg = fastEmit_ISD_BITCAST_r(VT.getSimpleVT(), RetVT.getSimpleVT(),
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001134 In, I->getOperand(0)->hasOneUse());
Dan Gohman2e644382016-05-10 17:39:48 +00001135 if (!Reg)
1136 return false;
1137 MachineBasicBlock::iterator Iter = FuncInfo.InsertPt;
1138 --Iter;
1139 assert(Iter->isBitcast());
1140 Iter->setPhysRegsDeadExcept(ArrayRef<unsigned>(), TRI);
1141 updateValueMap(I, Reg);
1142 return true;
1143}
1144
1145bool WebAssemblyFastISel::selectLoad(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001146 const auto *Load = cast<LoadInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001147 if (Load->isAtomic())
1148 return false;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001149 if (!Subtarget->hasSIMD128() && Load->getType()->isVectorTy())
1150 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001151
1152 Address Addr;
1153 if (!computeAddress(Load->getPointerOperand(), Addr))
1154 return false;
1155
1156 // TODO: Fold a following sign-/zero-extend into the load instruction.
1157
1158 unsigned Opc;
1159 const TargetRegisterClass *RC;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001160 switch (getSimpleType(Load->getType())) {
1161 case MVT::i1:
1162 case MVT::i8:
1163 Opc = WebAssembly::LOAD8_U_I32;
1164 RC = &WebAssembly::I32RegClass;
Dan Gohman2e644382016-05-10 17:39:48 +00001165 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001166 case MVT::i16:
1167 Opc = WebAssembly::LOAD16_U_I32;
1168 RC = &WebAssembly::I32RegClass;
1169 break;
1170 case MVT::i32:
1171 Opc = WebAssembly::LOAD_I32;
1172 RC = &WebAssembly::I32RegClass;
1173 break;
1174 case MVT::i64:
1175 Opc = WebAssembly::LOAD_I64;
1176 RC = &WebAssembly::I64RegClass;
1177 break;
1178 case MVT::f32:
Dan Gohman2e644382016-05-10 17:39:48 +00001179 Opc = WebAssembly::LOAD_F32;
1180 RC = &WebAssembly::F32RegClass;
1181 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001182 case MVT::f64:
Dan Gohman2e644382016-05-10 17:39:48 +00001183 Opc = WebAssembly::LOAD_F64;
1184 RC = &WebAssembly::F64RegClass;
1185 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001186 default:
1187 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001188 }
1189
Sam Clegg492f7522019-03-26 19:46:15 +00001190 if (!materializeLoadStoreOperands(Addr))
1191 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001192
1193 unsigned ResultReg = createResultReg(RC);
1194 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1195 ResultReg);
1196
1197 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Load));
1198
1199 updateValueMap(Load, ResultReg);
1200 return true;
1201}
1202
1203bool WebAssemblyFastISel::selectStore(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001204 const auto *Store = cast<StoreInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001205 if (Store->isAtomic())
1206 return false;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001207 if (!Subtarget->hasSIMD128() &&
1208 Store->getValueOperand()->getType()->isVectorTy())
1209 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001210
1211 Address Addr;
1212 if (!computeAddress(Store->getPointerOperand(), Addr))
1213 return false;
1214
1215 unsigned Opc;
Dan Gohman2e644382016-05-10 17:39:48 +00001216 bool VTIsi1 = false;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001217 switch (getSimpleType(Store->getValueOperand()->getType())) {
1218 case MVT::i1:
1219 VTIsi1 = true;
Dan Gohman861bec22018-02-09 22:59:01 +00001220 LLVM_FALLTHROUGH;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001221 case MVT::i8:
1222 Opc = WebAssembly::STORE8_I32;
Dan Gohman2e644382016-05-10 17:39:48 +00001223 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001224 case MVT::i16:
1225 Opc = WebAssembly::STORE16_I32;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001226 break;
1227 case MVT::i32:
1228 Opc = WebAssembly::STORE_I32;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001229 break;
1230 case MVT::i64:
1231 Opc = WebAssembly::STORE_I64;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001232 break;
1233 case MVT::f32:
Dan Gohman2e644382016-05-10 17:39:48 +00001234 Opc = WebAssembly::STORE_F32;
Dan Gohman2e644382016-05-10 17:39:48 +00001235 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001236 case MVT::f64:
Dan Gohman2e644382016-05-10 17:39:48 +00001237 Opc = WebAssembly::STORE_F64;
Dan Gohman2e644382016-05-10 17:39:48 +00001238 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001239 default:
1240 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001241 }
1242
Sam Clegg492f7522019-03-26 19:46:15 +00001243 if (!materializeLoadStoreOperands(Addr))
1244 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001245
1246 unsigned ValueReg = getRegForValue(Store->getValueOperand());
Derek Schuff732636d2016-08-04 18:01:52 +00001247 if (ValueReg == 0)
1248 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001249 if (VTIsi1)
Dan Gohman3a5ce732016-05-11 16:32:42 +00001250 ValueReg = maskI1Value(ValueReg, Store->getValueOperand());
Dan Gohman2e644382016-05-10 17:39:48 +00001251
Dan Gohman7f1bdb22016-10-06 22:08:28 +00001252 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
Dan Gohman2e644382016-05-10 17:39:48 +00001253
1254 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Store));
1255
1256 MIB.addReg(ValueReg);
1257 return true;
1258}
1259
1260bool WebAssemblyFastISel::selectBr(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001261 const auto *Br = cast<BranchInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001262 if (Br->isUnconditional()) {
1263 MachineBasicBlock *MSucc = FuncInfo.MBBMap[Br->getSuccessor(0)];
1264 fastEmitBranch(MSucc, Br->getDebugLoc());
1265 return true;
1266 }
1267
1268 MachineBasicBlock *TBB = FuncInfo.MBBMap[Br->getSuccessor(0)];
1269 MachineBasicBlock *FBB = FuncInfo.MBBMap[Br->getSuccessor(1)];
1270
Dan Gohman33e694a2016-05-12 04:19:09 +00001271 bool Not;
1272 unsigned CondReg = getRegForI1Value(Br->getCondition(), Not);
Derek Schuff732636d2016-08-04 18:01:52 +00001273 if (CondReg == 0)
1274 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001275
Dan Gohman33e694a2016-05-12 04:19:09 +00001276 unsigned Opc = WebAssembly::BR_IF;
1277 if (Not)
1278 Opc = WebAssembly::BR_UNLESS;
1279
Dan Gohman2e644382016-05-10 17:39:48 +00001280 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1281 .addMBB(TBB)
1282 .addReg(CondReg);
Derek Schuff39bf39f2016-08-02 23:16:09 +00001283
Dan Gohman2e644382016-05-10 17:39:48 +00001284 finishCondBranch(Br->getParent(), TBB, FBB);
1285 return true;
1286}
1287
1288bool WebAssemblyFastISel::selectRet(const Instruction *I) {
1289 if (!FuncInfo.CanLowerReturn)
1290 return false;
1291
Heejin Ahn18c56a02019-02-04 19:13:39 +00001292 const auto *Ret = cast<ReturnInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001293
1294 if (Ret->getNumOperands() == 0) {
1295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1296 TII.get(WebAssembly::RETURN_VOID));
1297 return true;
1298 }
1299
1300 Value *RV = Ret->getOperand(0);
Derek Schuff39bf39f2016-08-02 23:16:09 +00001301 if (!Subtarget->hasSIMD128() && RV->getType()->isVectorTy())
1302 return false;
1303
Dan Gohman2e644382016-05-10 17:39:48 +00001304 unsigned Opc;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001305 switch (getSimpleType(RV->getType())) {
Heejin Ahnf208f632018-09-05 01:27:38 +00001306 case MVT::i1:
1307 case MVT::i8:
1308 case MVT::i16:
1309 case MVT::i32:
Dan Gohman3a5ce732016-05-11 16:32:42 +00001310 Opc = WebAssembly::RETURN_I32;
Dan Gohman2e644382016-05-10 17:39:48 +00001311 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001312 case MVT::i64:
1313 Opc = WebAssembly::RETURN_I64;
1314 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001315 case MVT::f32:
1316 Opc = WebAssembly::RETURN_F32;
1317 break;
1318 case MVT::f64:
1319 Opc = WebAssembly::RETURN_F64;
1320 break;
1321 case MVT::v16i8:
1322 Opc = WebAssembly::RETURN_v16i8;
1323 break;
1324 case MVT::v8i16:
1325 Opc = WebAssembly::RETURN_v8i16;
1326 break;
1327 case MVT::v4i32:
1328 Opc = WebAssembly::RETURN_v4i32;
1329 break;
Derek Schuff51ed1312018-08-07 21:24:01 +00001330 case MVT::v2i64:
1331 Opc = WebAssembly::RETURN_v2i64;
1332 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001333 case MVT::v4f32:
1334 Opc = WebAssembly::RETURN_v4f32;
1335 break;
Derek Schuff51ed1312018-08-07 21:24:01 +00001336 case MVT::v2f64:
1337 Opc = WebAssembly::RETURN_v2f64;
1338 break;
Heejin Ahn0de58722018-03-08 04:05:37 +00001339 case MVT::ExceptRef:
1340 Opc = WebAssembly::RETURN_EXCEPT_REF;
1341 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001342 default:
1343 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001344 }
1345
Dan Gohman33e694a2016-05-12 04:19:09 +00001346 unsigned Reg;
1347 if (FuncInfo.Fn->getAttributes().hasAttribute(0, Attribute::SExt))
1348 Reg = getRegForSignedValue(RV);
1349 else if (FuncInfo.Fn->getAttributes().hasAttribute(0, Attribute::ZExt))
1350 Reg = getRegForUnsignedValue(RV);
1351 else
1352 Reg = getRegForValue(RV);
1353
Derek Schuff732636d2016-08-04 18:01:52 +00001354 if (Reg == 0)
1355 return false;
1356
Dan Gohman2e644382016-05-10 17:39:48 +00001357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)).addReg(Reg);
1358 return true;
1359}
1360
1361bool WebAssemblyFastISel::selectUnreachable(const Instruction *I) {
1362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1363 TII.get(WebAssembly::UNREACHABLE));
1364 return true;
1365}
1366
1367bool WebAssemblyFastISel::fastSelectInstruction(const Instruction *I) {
1368 switch (I->getOpcode()) {
Dan Gohman33e694a2016-05-12 04:19:09 +00001369 case Instruction::Call:
1370 if (selectCall(I))
1371 return true;
1372 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001373 case Instruction::Select:
1374 return selectSelect(I);
1375 case Instruction::Trunc:
1376 return selectTrunc(I);
1377 case Instruction::ZExt:
1378 return selectZExt(I);
1379 case Instruction::SExt:
1380 return selectSExt(I);
1381 case Instruction::ICmp:
1382 return selectICmp(I);
1383 case Instruction::FCmp:
1384 return selectFCmp(I);
1385 case Instruction::BitCast:
1386 return selectBitCast(I);
1387 case Instruction::Load:
1388 return selectLoad(I);
1389 case Instruction::Store:
1390 return selectStore(I);
1391 case Instruction::Br:
1392 return selectBr(I);
1393 case Instruction::Ret:
1394 return selectRet(I);
1395 case Instruction::Unreachable:
1396 return selectUnreachable(I);
1397 default:
1398 break;
Dan Gohman7b634842015-08-24 18:44:37 +00001399 }
1400
1401 // Fall back to target-independent instruction selection.
1402 return selectOperator(I, I->getOpcode());
1403}
1404
1405FastISel *WebAssembly::createFastISel(FunctionLoweringInfo &FuncInfo,
1406 const TargetLibraryInfo *LibInfo) {
1407 return new WebAssemblyFastISel(FuncInfo, LibInfo);
1408}