blob: 15df3d4ba0653d225e5a77f44cc2f442bbf2bcd4 [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);
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;
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
377void WebAssemblyFastISel::materializeLoadStoreOperands(Address &Addr) {
378 if (Addr.isRegBase()) {
379 unsigned Reg = Addr.getReg();
380 if (Reg == 0) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000381 Reg = createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
382 : &WebAssembly::I32RegClass);
383 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
384 : WebAssembly::CONST_I32;
Dan Gohman2e644382016-05-10 17:39:48 +0000385 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), Reg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000386 .addImm(0);
Dan Gohman2e644382016-05-10 17:39:48 +0000387 Addr.setReg(Reg);
388 }
389 }
390}
391
392void WebAssemblyFastISel::addLoadStoreOperands(const Address &Addr,
393 const MachineInstrBuilder &MIB,
394 MachineMemOperand *MMO) {
Dan Gohman48abaa92016-10-25 00:17:11 +0000395 // Set the alignment operand (this is rewritten in SetP2AlignOperands).
396 // TODO: Disable SetP2AlignOperands for FastISel and just do it here.
397 MIB.addImm(0);
398
Dan Gohman2e644382016-05-10 17:39:48 +0000399 if (const GlobalValue *GV = Addr.getGlobalValue())
400 MIB.addGlobalAddress(GV, Addr.getOffset());
401 else
402 MIB.addImm(Addr.getOffset());
403
404 if (Addr.isRegBase())
405 MIB.addReg(Addr.getReg());
406 else
407 MIB.addFrameIndex(Addr.getFI());
408
Dan Gohman2e644382016-05-10 17:39:48 +0000409 MIB.addMemOperand(MMO);
410}
411
Dan Gohman3a5ce732016-05-11 16:32:42 +0000412unsigned WebAssemblyFastISel::maskI1Value(unsigned Reg, const Value *V) {
413 return zeroExtendToI32(Reg, V, MVT::i1);
Dan Gohman2e644382016-05-10 17:39:48 +0000414}
415
Dan Gohman33e694a2016-05-12 04:19:09 +0000416unsigned WebAssemblyFastISel::getRegForI1Value(const Value *V, bool &Not) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000417 if (const auto *ICmp = dyn_cast<ICmpInst>(V))
Dan Gohman33e694a2016-05-12 04:19:09 +0000418 if (const ConstantInt *C = dyn_cast<ConstantInt>(ICmp->getOperand(1)))
419 if (ICmp->isEquality() && C->isZero() && C->getType()->isIntegerTy(32)) {
420 Not = ICmp->isTrueWhenEqual();
421 return getRegForValue(ICmp->getOperand(0));
422 }
423
Sanjay Patel47a52a02018-10-23 16:05:09 +0000424 Value *NotV;
425 if (match(V, m_Not(m_Value(NotV))) && V->getType()->isIntegerTy(32)) {
Dan Gohman33e694a2016-05-12 04:19:09 +0000426 Not = true;
Sanjay Patel47a52a02018-10-23 16:05:09 +0000427 return getRegForValue(NotV);
Dan Gohman33e694a2016-05-12 04:19:09 +0000428 }
429
430 Not = false;
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000431 unsigned Reg = getRegForValue(V);
432 if (Reg == 0)
433 return 0;
434 return maskI1Value(Reg, V);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000435}
436
437unsigned WebAssemblyFastISel::zeroExtendToI32(unsigned Reg, const Value *V,
438 MVT::SimpleValueType From) {
Derek Schuff732636d2016-08-04 18:01:52 +0000439 if (Reg == 0)
440 return 0;
441
Dan Gohman3a5ce732016-05-11 16:32:42 +0000442 switch (From) {
443 case MVT::i1:
Thomas Lively2a47e032019-01-14 22:03:43 +0000444 // If the value is naturally an i1, we don't need to mask it. We only know
445 // if a value is naturally an i1 if it is definitely lowered by FastISel,
446 // not a DAG ISel fallback.
447 if (V != nullptr && isa<Argument>(V) && cast<Argument>(V)->hasZExtAttr())
448 return copyValue(Reg);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000449 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000450 case MVT::i8:
451 case MVT::i16:
Dan Gohman3a5ce732016-05-11 16:32:42 +0000452 break;
453 case MVT::i32:
Dan Gohman33e694a2016-05-12 04:19:09 +0000454 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000455 default:
456 return 0;
457 }
458
459 unsigned Imm = createResultReg(&WebAssembly::I32RegClass);
460 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
461 TII.get(WebAssembly::CONST_I32), Imm)
Heejin Ahnf208f632018-09-05 01:27:38 +0000462 .addImm(~(~uint64_t(0) << MVT(From).getSizeInBits()));
Dan Gohman3a5ce732016-05-11 16:32:42 +0000463
464 unsigned Result = createResultReg(&WebAssembly::I32RegClass);
465 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
466 TII.get(WebAssembly::AND_I32), Result)
Heejin Ahnf208f632018-09-05 01:27:38 +0000467 .addReg(Reg)
468 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000469
470 return Result;
471}
472
473unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
474 MVT::SimpleValueType From) {
Derek Schuff732636d2016-08-04 18:01:52 +0000475 if (Reg == 0)
476 return 0;
477
Dan Gohman3a5ce732016-05-11 16:32:42 +0000478 switch (From) {
479 case MVT::i1:
480 case MVT::i8:
481 case MVT::i16:
482 break;
483 case MVT::i32:
Dan Gohman33e694a2016-05-12 04:19:09 +0000484 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000485 default:
Dan Gohman33e694a2016-05-12 04:19:09 +0000486 return 0;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000487 }
488
489 unsigned Imm = createResultReg(&WebAssembly::I32RegClass);
490 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
491 TII.get(WebAssembly::CONST_I32), Imm)
Heejin Ahnf208f632018-09-05 01:27:38 +0000492 .addImm(32 - MVT(From).getSizeInBits());
Dan Gohman3a5ce732016-05-11 16:32:42 +0000493
494 unsigned Left = createResultReg(&WebAssembly::I32RegClass);
495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
496 TII.get(WebAssembly::SHL_I32), Left)
Heejin Ahnf208f632018-09-05 01:27:38 +0000497 .addReg(Reg)
498 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000499
500 unsigned Right = createResultReg(&WebAssembly::I32RegClass);
501 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
502 TII.get(WebAssembly::SHR_S_I32), Right)
Heejin Ahnf208f632018-09-05 01:27:38 +0000503 .addReg(Left)
504 .addReg(Imm);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000505
506 return Right;
507}
508
509unsigned WebAssemblyFastISel::zeroExtend(unsigned Reg, const Value *V,
510 MVT::SimpleValueType From,
511 MVT::SimpleValueType To) {
512 if (To == MVT::i64) {
513 if (From == MVT::i64)
Dan Gohman33e694a2016-05-12 04:19:09 +0000514 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000515
516 Reg = zeroExtendToI32(Reg, V, From);
517
518 unsigned Result = createResultReg(&WebAssembly::I64RegClass);
519 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
520 TII.get(WebAssembly::I64_EXTEND_U_I32), Result)
521 .addReg(Reg);
522 return Result;
523 }
524
Dan Gohman3a5ce732016-05-11 16:32:42 +0000525 return zeroExtendToI32(Reg, V, From);
526}
527
528unsigned WebAssemblyFastISel::signExtend(unsigned Reg, const Value *V,
529 MVT::SimpleValueType From,
530 MVT::SimpleValueType To) {
531 if (To == MVT::i64) {
532 if (From == MVT::i64)
Dan Gohman33e694a2016-05-12 04:19:09 +0000533 return copyValue(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000534
535 Reg = signExtendToI32(Reg, V, From);
536
537 unsigned Result = createResultReg(&WebAssembly::I64RegClass);
538 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
539 TII.get(WebAssembly::I64_EXTEND_S_I32), Result)
540 .addReg(Reg);
541 return Result;
542 }
543
Dan Gohman3a5ce732016-05-11 16:32:42 +0000544 return signExtendToI32(Reg, V, From);
545}
546
547unsigned WebAssemblyFastISel::getRegForUnsignedValue(const Value *V) {
548 MVT::SimpleValueType From = getSimpleType(V->getType());
549 MVT::SimpleValueType To = getLegalType(From);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000550 unsigned VReg = getRegForValue(V);
551 if (VReg == 0)
552 return 0;
553 return zeroExtend(VReg, V, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000554}
555
556unsigned WebAssemblyFastISel::getRegForSignedValue(const Value *V) {
557 MVT::SimpleValueType From = getSimpleType(V->getType());
558 MVT::SimpleValueType To = getLegalType(From);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000559 unsigned VReg = getRegForValue(V);
560 if (VReg == 0)
561 return 0;
562 return signExtend(VReg, V, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000563}
564
565unsigned WebAssemblyFastISel::getRegForPromotedValue(const Value *V,
566 bool IsSigned) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000567 return IsSigned ? getRegForSignedValue(V) : getRegForUnsignedValue(V);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000568}
569
570unsigned WebAssemblyFastISel::notValue(unsigned Reg) {
Dan Gohman33e694a2016-05-12 04:19:09 +0000571 assert(MRI.getRegClass(Reg) == &WebAssembly::I32RegClass);
572
Dan Gohman3a5ce732016-05-11 16:32:42 +0000573 unsigned NotReg = createResultReg(&WebAssembly::I32RegClass);
574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
575 TII.get(WebAssembly::EQZ_I32), NotReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000576 .addReg(Reg);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000577 return NotReg;
Dan Gohman2e644382016-05-10 17:39:48 +0000578}
579
Dan Gohman33e694a2016-05-12 04:19:09 +0000580unsigned WebAssemblyFastISel::copyValue(unsigned Reg) {
581 unsigned ResultReg = createResultReg(MRI.getRegClass(Reg));
Heejin Ahnf208f632018-09-05 01:27:38 +0000582 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(WebAssembly::COPY),
583 ResultReg)
584 .addReg(Reg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000585 return ResultReg;
586}
587
Dan Gohman2e644382016-05-10 17:39:48 +0000588unsigned WebAssemblyFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
589 DenseMap<const AllocaInst *, int>::iterator SI =
590 FuncInfo.StaticAllocaMap.find(AI);
591
592 if (SI != FuncInfo.StaticAllocaMap.end()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000593 unsigned ResultReg =
594 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
595 : &WebAssembly::I32RegClass);
596 unsigned Opc =
597 Subtarget->hasAddr64() ? WebAssembly::COPY_I64 : WebAssembly::COPY_I32;
Dan Gohman2e644382016-05-10 17:39:48 +0000598 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
599 .addFrameIndex(SI->second);
600 return ResultReg;
601 }
602
603 return 0;
604}
605
606unsigned WebAssemblyFastISel::fastMaterializeConstant(const Constant *C) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000607 if (const auto *GV = dyn_cast<GlobalValue>(C)) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000608 unsigned ResultReg =
609 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
610 : &WebAssembly::I32RegClass);
611 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
612 : WebAssembly::CONST_I32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000613 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000614 .addGlobalAddress(GV);
Dan Gohman33e694a2016-05-12 04:19:09 +0000615 return ResultReg;
Dan Gohman2e644382016-05-10 17:39:48 +0000616 }
617
618 // Let target-independent code handle it.
619 return 0;
620}
621
Dan Gohman33e694a2016-05-12 04:19:09 +0000622bool WebAssemblyFastISel::fastLowerArguments() {
623 if (!FuncInfo.CanLowerReturn)
624 return false;
625
626 const Function *F = FuncInfo.Fn;
627 if (F->isVarArg())
628 return false;
629
Heejin Ahn18c56a02019-02-04 19:13:39 +0000630 unsigned I = 0;
Dan Gohman33e694a2016-05-12 04:19:09 +0000631 for (auto const &Arg : F->args()) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000632 const AttributeList &Attrs = F->getAttributes();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000633 if (Attrs.hasParamAttribute(I, Attribute::ByVal) ||
634 Attrs.hasParamAttribute(I, Attribute::SwiftSelf) ||
635 Attrs.hasParamAttribute(I, Attribute::SwiftError) ||
636 Attrs.hasParamAttribute(I, Attribute::InAlloca) ||
637 Attrs.hasParamAttribute(I, Attribute::Nest))
Dan Gohman33e694a2016-05-12 04:19:09 +0000638 return false;
639
640 Type *ArgTy = Arg.getType();
Derek Schuff39bf39f2016-08-02 23:16:09 +0000641 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
642 return false;
643 if (!Subtarget->hasSIMD128() && ArgTy->isVectorTy())
Dan Gohman33e694a2016-05-12 04:19:09 +0000644 return false;
645
646 unsigned Opc;
647 const TargetRegisterClass *RC;
648 switch (getSimpleType(ArgTy)) {
649 case MVT::i1:
650 case MVT::i8:
651 case MVT::i16:
652 case MVT::i32:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000653 Opc = WebAssembly::ARGUMENT_i32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000654 RC = &WebAssembly::I32RegClass;
655 break;
656 case MVT::i64:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000657 Opc = WebAssembly::ARGUMENT_i64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000658 RC = &WebAssembly::I64RegClass;
659 break;
660 case MVT::f32:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000661 Opc = WebAssembly::ARGUMENT_f32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000662 RC = &WebAssembly::F32RegClass;
663 break;
664 case MVT::f64:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000665 Opc = WebAssembly::ARGUMENT_f64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000666 RC = &WebAssembly::F64RegClass;
667 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000668 case MVT::v16i8:
669 Opc = WebAssembly::ARGUMENT_v16i8;
670 RC = &WebAssembly::V128RegClass;
671 break;
672 case MVT::v8i16:
673 Opc = WebAssembly::ARGUMENT_v8i16;
674 RC = &WebAssembly::V128RegClass;
675 break;
676 case MVT::v4i32:
677 Opc = WebAssembly::ARGUMENT_v4i32;
678 RC = &WebAssembly::V128RegClass;
679 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000680 case MVT::v2i64:
681 Opc = WebAssembly::ARGUMENT_v2i64;
682 RC = &WebAssembly::V128RegClass;
683 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000684 case MVT::v4f32:
685 Opc = WebAssembly::ARGUMENT_v4f32;
686 RC = &WebAssembly::V128RegClass;
687 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000688 case MVT::v2f64:
689 Opc = WebAssembly::ARGUMENT_v2f64;
690 RC = &WebAssembly::V128RegClass;
691 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000692 case MVT::ExceptRef:
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000693 Opc = WebAssembly::ARGUMENT_ExceptRef;
Heejin Ahn0de58722018-03-08 04:05:37 +0000694 RC = &WebAssembly::EXCEPT_REFRegClass;
695 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000696 default:
697 return false;
698 }
699 unsigned ResultReg = createResultReg(RC);
700 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahn18c56a02019-02-04 19:13:39 +0000701 .addImm(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000702 updateValueMap(&Arg, ResultReg);
703
Heejin Ahn18c56a02019-02-04 19:13:39 +0000704 ++I;
Dan Gohman33e694a2016-05-12 04:19:09 +0000705 }
706
707 MRI.addLiveIn(WebAssembly::ARGUMENTS);
708
709 auto *MFI = MF->getInfo<WebAssemblyFunctionInfo>();
Dan Gohmanb8184822018-05-22 04:58:36 +0000710 for (auto const &Arg : F->args()) {
711 MVT::SimpleValueType ArgTy = getLegalType(getSimpleType(Arg.getType()));
712 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
713 MFI->clearParamsAndResults();
714 return false;
715 }
716 MFI->addParam(ArgTy);
717 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000718
Dan Gohman4576dc02018-04-17 20:46:42 +0000719 if (!F->getReturnType()->isVoidTy()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000720 MVT::SimpleValueType RetTy =
721 getLegalType(getSimpleType(F->getReturnType()));
Dan Gohmanb8184822018-05-22 04:58:36 +0000722 if (RetTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
723 MFI->clearParamsAndResults();
Dan Gohman4576dc02018-04-17 20:46:42 +0000724 return false;
Dan Gohmanb8184822018-05-22 04:58:36 +0000725 }
726 MFI->addResult(RetTy);
Dan Gohman4576dc02018-04-17 20:46:42 +0000727 }
Dan Gohman6055fba2017-01-09 23:09:38 +0000728
Dan Gohman33e694a2016-05-12 04:19:09 +0000729 return true;
730}
731
732bool WebAssemblyFastISel::selectCall(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000733 const auto *Call = cast<CallInst>(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000734
735 if (Call->isMustTailCall() || Call->isInlineAsm() ||
736 Call->getFunctionType()->isVarArg())
737 return false;
738
739 Function *Func = Call->getCalledFunction();
740 if (Func && Func->isIntrinsic())
741 return false;
742
Jacob Gravelle690b76e2017-08-24 19:53:44 +0000743 bool IsDirect = Func != nullptr;
744 if (!IsDirect && isa<ConstantExpr>(Call->getCalledValue()))
745 return false;
746
Dan Gohman33e694a2016-05-12 04:19:09 +0000747 FunctionType *FuncTy = Call->getFunctionType();
748 unsigned Opc;
Dan Gohman33e694a2016-05-12 04:19:09 +0000749 bool IsVoid = FuncTy->getReturnType()->isVoidTy();
750 unsigned ResultReg;
751 if (IsVoid) {
Derek Schuff6f697832016-10-21 16:38:07 +0000752 Opc = IsDirect ? WebAssembly::CALL_VOID : WebAssembly::PCALL_INDIRECT_VOID;
Dan Gohman33e694a2016-05-12 04:19:09 +0000753 } else {
Derek Schuff39bf39f2016-08-02 23:16:09 +0000754 if (!Subtarget->hasSIMD128() && Call->getType()->isVectorTy())
755 return false;
756
Dan Gohman33e694a2016-05-12 04:19:09 +0000757 MVT::SimpleValueType RetTy = getSimpleType(Call->getType());
758 switch (RetTy) {
759 case MVT::i1:
760 case MVT::i8:
761 case MVT::i16:
762 case MVT::i32:
Derek Schuff6f697832016-10-21 16:38:07 +0000763 Opc = IsDirect ? WebAssembly::CALL_I32 : WebAssembly::PCALL_INDIRECT_I32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000764 ResultReg = createResultReg(&WebAssembly::I32RegClass);
765 break;
766 case MVT::i64:
Derek Schuff6f697832016-10-21 16:38:07 +0000767 Opc = IsDirect ? WebAssembly::CALL_I64 : WebAssembly::PCALL_INDIRECT_I64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000768 ResultReg = createResultReg(&WebAssembly::I64RegClass);
769 break;
770 case MVT::f32:
Derek Schuff6f697832016-10-21 16:38:07 +0000771 Opc = IsDirect ? WebAssembly::CALL_F32 : WebAssembly::PCALL_INDIRECT_F32;
Dan Gohman33e694a2016-05-12 04:19:09 +0000772 ResultReg = createResultReg(&WebAssembly::F32RegClass);
773 break;
774 case MVT::f64:
Derek Schuff6f697832016-10-21 16:38:07 +0000775 Opc = IsDirect ? WebAssembly::CALL_F64 : WebAssembly::PCALL_INDIRECT_F64;
Dan Gohman33e694a2016-05-12 04:19:09 +0000776 ResultReg = createResultReg(&WebAssembly::F64RegClass);
777 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000778 case MVT::v16i8:
Heejin Ahnf208f632018-09-05 01:27:38 +0000779 Opc = IsDirect ? WebAssembly::CALL_v16i8
780 : WebAssembly::PCALL_INDIRECT_v16i8;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000781 ResultReg = createResultReg(&WebAssembly::V128RegClass);
782 break;
783 case MVT::v8i16:
Heejin Ahnf208f632018-09-05 01:27:38 +0000784 Opc = IsDirect ? WebAssembly::CALL_v8i16
785 : WebAssembly::PCALL_INDIRECT_v8i16;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000786 ResultReg = createResultReg(&WebAssembly::V128RegClass);
787 break;
788 case MVT::v4i32:
Heejin Ahnf208f632018-09-05 01:27:38 +0000789 Opc = IsDirect ? WebAssembly::CALL_v4i32
790 : WebAssembly::PCALL_INDIRECT_v4i32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000791 ResultReg = createResultReg(&WebAssembly::V128RegClass);
792 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000793 case MVT::v2i64:
Heejin Ahnf208f632018-09-05 01:27:38 +0000794 Opc = IsDirect ? WebAssembly::CALL_v2i64
795 : WebAssembly::PCALL_INDIRECT_v2i64;
Derek Schuff51ed1312018-08-07 21:24:01 +0000796 ResultReg = createResultReg(&WebAssembly::V128RegClass);
797 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000798 case MVT::v4f32:
Heejin Ahnf208f632018-09-05 01:27:38 +0000799 Opc = IsDirect ? WebAssembly::CALL_v4f32
800 : WebAssembly::PCALL_INDIRECT_v4f32;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000801 ResultReg = createResultReg(&WebAssembly::V128RegClass);
802 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000803 case MVT::v2f64:
Heejin Ahnf208f632018-09-05 01:27:38 +0000804 Opc = IsDirect ? WebAssembly::CALL_v2f64
805 : WebAssembly::PCALL_INDIRECT_v2f64;
Derek Schuff51ed1312018-08-07 21:24:01 +0000806 ResultReg = createResultReg(&WebAssembly::V128RegClass);
807 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000808 case MVT::ExceptRef:
809 Opc = IsDirect ? WebAssembly::CALL_EXCEPT_REF
810 : WebAssembly::PCALL_INDIRECT_EXCEPT_REF;
811 ResultReg = createResultReg(&WebAssembly::EXCEPT_REFRegClass);
812 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000813 default:
814 return false;
815 }
816 }
817
818 SmallVector<unsigned, 8> Args;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000819 for (unsigned I = 0, E = Call->getNumArgOperands(); I < E; ++I) {
820 Value *V = Call->getArgOperand(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000821 MVT::SimpleValueType ArgTy = getSimpleType(V->getType());
822 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
823 return false;
824
Reid Klecknerb5180542017-03-21 16:57:19 +0000825 const AttributeList &Attrs = Call->getAttributes();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000826 if (Attrs.hasParamAttribute(I, Attribute::ByVal) ||
827 Attrs.hasParamAttribute(I, Attribute::SwiftSelf) ||
828 Attrs.hasParamAttribute(I, Attribute::SwiftError) ||
829 Attrs.hasParamAttribute(I, Attribute::InAlloca) ||
830 Attrs.hasParamAttribute(I, Attribute::Nest))
Dan Gohman33e694a2016-05-12 04:19:09 +0000831 return false;
832
833 unsigned Reg;
834
Heejin Ahn18c56a02019-02-04 19:13:39 +0000835 if (Attrs.hasParamAttribute(I, Attribute::SExt))
Dan Gohman33e694a2016-05-12 04:19:09 +0000836 Reg = getRegForSignedValue(V);
Heejin Ahn18c56a02019-02-04 19:13:39 +0000837 else if (Attrs.hasParamAttribute(I, Attribute::ZExt))
Dan Gohman33e694a2016-05-12 04:19:09 +0000838 Reg = getRegForUnsignedValue(V);
839 else
840 Reg = getRegForValue(V);
841
842 if (Reg == 0)
843 return false;
844
845 Args.push_back(Reg);
846 }
847
848 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
849
850 if (!IsVoid)
851 MIB.addReg(ResultReg, RegState::Define);
852
853 if (IsDirect)
854 MIB.addGlobalAddress(Func);
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000855 else {
856 unsigned Reg = getRegForValue(Call->getCalledValue());
857 if (Reg == 0)
858 return false;
859 MIB.addReg(Reg);
860 }
Dan Gohman33e694a2016-05-12 04:19:09 +0000861
862 for (unsigned ArgReg : Args)
863 MIB.addReg(ArgReg);
864
865 if (!IsVoid)
866 updateValueMap(Call, ResultReg);
867 return true;
868}
869
870bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000871 const auto *Select = cast<SelectInst>(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000872
873 bool Not;
Heejin Ahnf208f632018-09-05 01:27:38 +0000874 unsigned CondReg = getRegForI1Value(Select->getCondition(), Not);
Dan Gohman33e694a2016-05-12 04:19:09 +0000875 if (CondReg == 0)
876 return false;
877
Heejin Ahnf208f632018-09-05 01:27:38 +0000878 unsigned TrueReg = getRegForValue(Select->getTrueValue());
Dan Gohman33e694a2016-05-12 04:19:09 +0000879 if (TrueReg == 0)
880 return false;
881
882 unsigned FalseReg = getRegForValue(Select->getFalseValue());
883 if (FalseReg == 0)
884 return false;
885
886 if (Not)
887 std::swap(TrueReg, FalseReg);
888
889 unsigned Opc;
890 const TargetRegisterClass *RC;
891 switch (getSimpleType(Select->getType())) {
892 case MVT::i1:
893 case MVT::i8:
894 case MVT::i16:
895 case MVT::i32:
896 Opc = WebAssembly::SELECT_I32;
897 RC = &WebAssembly::I32RegClass;
898 break;
899 case MVT::i64:
900 Opc = WebAssembly::SELECT_I64;
901 RC = &WebAssembly::I64RegClass;
902 break;
903 case MVT::f32:
904 Opc = WebAssembly::SELECT_F32;
905 RC = &WebAssembly::F32RegClass;
906 break;
907 case MVT::f64:
908 Opc = WebAssembly::SELECT_F64;
909 RC = &WebAssembly::F64RegClass;
910 break;
Heejin Ahn0de58722018-03-08 04:05:37 +0000911 case MVT::ExceptRef:
912 Opc = WebAssembly::SELECT_EXCEPT_REF;
913 RC = &WebAssembly::EXCEPT_REFRegClass;
914 break;
Dan Gohman33e694a2016-05-12 04:19:09 +0000915 default:
916 return false;
917 }
918
919 unsigned ResultReg = createResultReg(RC);
920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Heejin Ahnf208f632018-09-05 01:27:38 +0000921 .addReg(TrueReg)
922 .addReg(FalseReg)
923 .addReg(CondReg);
Dan Gohman33e694a2016-05-12 04:19:09 +0000924
925 updateValueMap(Select, ResultReg);
926 return true;
927}
928
929bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000930 const auto *Trunc = cast<TruncInst>(I);
Dan Gohman33e694a2016-05-12 04:19:09 +0000931
932 unsigned Reg = getRegForValue(Trunc->getOperand(0));
933 if (Reg == 0)
934 return false;
935
936 if (Trunc->getOperand(0)->getType()->isIntegerTy(64)) {
937 unsigned Result = createResultReg(&WebAssembly::I32RegClass);
938 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
939 TII.get(WebAssembly::I32_WRAP_I64), Result)
940 .addReg(Reg);
941 Reg = Result;
942 }
943
944 updateValueMap(Trunc, Reg);
945 return true;
946}
947
Dan Gohman3a5ce732016-05-11 16:32:42 +0000948bool WebAssemblyFastISel::selectZExt(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000949 const auto *ZExt = cast<ZExtInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000950
Dan Gohman33e694a2016-05-12 04:19:09 +0000951 const Value *Op = ZExt->getOperand(0);
952 MVT::SimpleValueType From = getSimpleType(Op->getType());
953 MVT::SimpleValueType To = getLegalType(getSimpleType(ZExt->getType()));
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000954 unsigned In = getRegForValue(Op);
955 if (In == 0)
956 return false;
957 unsigned Reg = zeroExtend(In, Op, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000958 if (Reg == 0)
959 return false;
960
961 updateValueMap(ZExt, Reg);
962 return true;
963}
964
965bool WebAssemblyFastISel::selectSExt(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000966 const auto *SExt = cast<SExtInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000967
Dan Gohman33e694a2016-05-12 04:19:09 +0000968 const Value *Op = SExt->getOperand(0);
969 MVT::SimpleValueType From = getSimpleType(Op->getType());
970 MVT::SimpleValueType To = getLegalType(getSimpleType(SExt->getType()));
Dan Gohman3ff73cf2017-11-28 05:36:42 +0000971 unsigned In = getRegForValue(Op);
972 if (In == 0)
973 return false;
974 unsigned Reg = signExtend(In, Op, From, To);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000975 if (Reg == 0)
976 return false;
977
978 updateValueMap(SExt, Reg);
979 return true;
980}
981
982bool WebAssemblyFastISel::selectICmp(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000983 const auto *ICmp = cast<ICmpInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +0000984
985 bool I32 = getSimpleType(ICmp->getOperand(0)->getType()) != MVT::i64;
986 unsigned Opc;
Heejin Ahn18c56a02019-02-04 19:13:39 +0000987 bool IsSigned = false;
Dan Gohman3a5ce732016-05-11 16:32:42 +0000988 switch (ICmp->getPredicate()) {
989 case ICmpInst::ICMP_EQ:
990 Opc = I32 ? WebAssembly::EQ_I32 : WebAssembly::EQ_I64;
991 break;
992 case ICmpInst::ICMP_NE:
993 Opc = I32 ? WebAssembly::NE_I32 : WebAssembly::NE_I64;
994 break;
995 case ICmpInst::ICMP_UGT:
996 Opc = I32 ? WebAssembly::GT_U_I32 : WebAssembly::GT_U_I64;
997 break;
998 case ICmpInst::ICMP_UGE:
999 Opc = I32 ? WebAssembly::GE_U_I32 : WebAssembly::GE_U_I64;
1000 break;
1001 case ICmpInst::ICMP_ULT:
1002 Opc = I32 ? WebAssembly::LT_U_I32 : WebAssembly::LT_U_I64;
1003 break;
1004 case ICmpInst::ICMP_ULE:
1005 Opc = I32 ? WebAssembly::LE_U_I32 : WebAssembly::LE_U_I64;
1006 break;
1007 case ICmpInst::ICMP_SGT:
1008 Opc = I32 ? WebAssembly::GT_S_I32 : WebAssembly::GT_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001009 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001010 break;
1011 case ICmpInst::ICMP_SGE:
1012 Opc = I32 ? WebAssembly::GE_S_I32 : WebAssembly::GE_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001013 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001014 break;
1015 case ICmpInst::ICMP_SLT:
1016 Opc = I32 ? WebAssembly::LT_S_I32 : WebAssembly::LT_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001017 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001018 break;
1019 case ICmpInst::ICMP_SLE:
1020 Opc = I32 ? WebAssembly::LE_S_I32 : WebAssembly::LE_S_I64;
Heejin Ahn18c56a02019-02-04 19:13:39 +00001021 IsSigned = true;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001022 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001023 default:
1024 return false;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001025 }
1026
Heejin Ahn18c56a02019-02-04 19:13:39 +00001027 unsigned LHS = getRegForPromotedValue(ICmp->getOperand(0), IsSigned);
Dan Gohman3a5ce732016-05-11 16:32:42 +00001028 if (LHS == 0)
1029 return false;
1030
Heejin Ahn18c56a02019-02-04 19:13:39 +00001031 unsigned RHS = getRegForPromotedValue(ICmp->getOperand(1), IsSigned);
Dan Gohman3a5ce732016-05-11 16:32:42 +00001032 if (RHS == 0)
1033 return false;
1034
1035 unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass);
1036 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1037 .addReg(LHS)
1038 .addReg(RHS);
1039 updateValueMap(ICmp, ResultReg);
1040 return true;
1041}
1042
1043bool WebAssemblyFastISel::selectFCmp(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001044 const auto *FCmp = cast<FCmpInst>(I);
Dan Gohman3a5ce732016-05-11 16:32:42 +00001045
1046 unsigned LHS = getRegForValue(FCmp->getOperand(0));
1047 if (LHS == 0)
1048 return false;
1049
1050 unsigned RHS = getRegForValue(FCmp->getOperand(1));
1051 if (RHS == 0)
1052 return false;
1053
1054 bool F32 = getSimpleType(FCmp->getOperand(0)->getType()) != MVT::f64;
1055 unsigned Opc;
1056 bool Not = false;
1057 switch (FCmp->getPredicate()) {
1058 case FCmpInst::FCMP_OEQ:
1059 Opc = F32 ? WebAssembly::EQ_F32 : WebAssembly::EQ_F64;
1060 break;
1061 case FCmpInst::FCMP_UNE:
1062 Opc = F32 ? WebAssembly::NE_F32 : WebAssembly::NE_F64;
1063 break;
1064 case FCmpInst::FCMP_OGT:
1065 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1066 break;
1067 case FCmpInst::FCMP_OGE:
1068 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1069 break;
1070 case FCmpInst::FCMP_OLT:
1071 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1072 break;
1073 case FCmpInst::FCMP_OLE:
1074 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1075 break;
1076 case FCmpInst::FCMP_UGT:
1077 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1078 Not = true;
1079 break;
1080 case FCmpInst::FCMP_UGE:
1081 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1082 Not = true;
1083 break;
1084 case FCmpInst::FCMP_ULT:
1085 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1086 Not = true;
1087 break;
1088 case FCmpInst::FCMP_ULE:
1089 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1090 Not = true;
1091 break;
1092 default:
1093 return false;
1094 }
1095
1096 unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass);
1097 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1098 .addReg(LHS)
1099 .addReg(RHS);
1100
1101 if (Not)
1102 ResultReg = notValue(ResultReg);
1103
1104 updateValueMap(FCmp, ResultReg);
1105 return true;
1106}
1107
Dan Gohman2e644382016-05-10 17:39:48 +00001108bool WebAssemblyFastISel::selectBitCast(const Instruction *I) {
1109 // Target-independent code can handle this, except it doesn't set the dead
1110 // flag on the ARGUMENTS clobber, so we have to do that manually in order
1111 // to satisfy code that expects this of isBitcast() instructions.
1112 EVT VT = TLI.getValueType(DL, I->getOperand(0)->getType());
1113 EVT RetVT = TLI.getValueType(DL, I->getType());
1114 if (!VT.isSimple() || !RetVT.isSimple())
1115 return false;
Dan Gohman33e694a2016-05-12 04:19:09 +00001116
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001117 unsigned In = getRegForValue(I->getOperand(0));
1118 if (In == 0)
1119 return false;
1120
Dan Gohman33e694a2016-05-12 04:19:09 +00001121 if (VT == RetVT) {
1122 // No-op bitcast.
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001123 updateValueMap(I, In);
Dan Gohman33e694a2016-05-12 04:19:09 +00001124 return true;
1125 }
1126
Dan Gohman2e644382016-05-10 17:39:48 +00001127 unsigned Reg = fastEmit_ISD_BITCAST_r(VT.getSimpleVT(), RetVT.getSimpleVT(),
Dan Gohman3ff73cf2017-11-28 05:36:42 +00001128 In, I->getOperand(0)->hasOneUse());
Dan Gohman2e644382016-05-10 17:39:48 +00001129 if (!Reg)
1130 return false;
1131 MachineBasicBlock::iterator Iter = FuncInfo.InsertPt;
1132 --Iter;
1133 assert(Iter->isBitcast());
1134 Iter->setPhysRegsDeadExcept(ArrayRef<unsigned>(), TRI);
1135 updateValueMap(I, Reg);
1136 return true;
1137}
1138
1139bool WebAssemblyFastISel::selectLoad(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001140 const auto *Load = cast<LoadInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001141 if (Load->isAtomic())
1142 return false;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001143 if (!Subtarget->hasSIMD128() && Load->getType()->isVectorTy())
1144 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001145
1146 Address Addr;
1147 if (!computeAddress(Load->getPointerOperand(), Addr))
1148 return false;
1149
1150 // TODO: Fold a following sign-/zero-extend into the load instruction.
1151
1152 unsigned Opc;
1153 const TargetRegisterClass *RC;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001154 switch (getSimpleType(Load->getType())) {
1155 case MVT::i1:
1156 case MVT::i8:
1157 Opc = WebAssembly::LOAD8_U_I32;
1158 RC = &WebAssembly::I32RegClass;
Dan Gohman2e644382016-05-10 17:39:48 +00001159 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001160 case MVT::i16:
1161 Opc = WebAssembly::LOAD16_U_I32;
1162 RC = &WebAssembly::I32RegClass;
1163 break;
1164 case MVT::i32:
1165 Opc = WebAssembly::LOAD_I32;
1166 RC = &WebAssembly::I32RegClass;
1167 break;
1168 case MVT::i64:
1169 Opc = WebAssembly::LOAD_I64;
1170 RC = &WebAssembly::I64RegClass;
1171 break;
1172 case MVT::f32:
Dan Gohman2e644382016-05-10 17:39:48 +00001173 Opc = WebAssembly::LOAD_F32;
1174 RC = &WebAssembly::F32RegClass;
1175 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001176 case MVT::f64:
Dan Gohman2e644382016-05-10 17:39:48 +00001177 Opc = WebAssembly::LOAD_F64;
1178 RC = &WebAssembly::F64RegClass;
1179 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001180 default:
1181 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001182 }
1183
1184 materializeLoadStoreOperands(Addr);
1185
1186 unsigned ResultReg = createResultReg(RC);
1187 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1188 ResultReg);
1189
1190 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Load));
1191
1192 updateValueMap(Load, ResultReg);
1193 return true;
1194}
1195
1196bool WebAssemblyFastISel::selectStore(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001197 const auto *Store = cast<StoreInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001198 if (Store->isAtomic())
1199 return false;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001200 if (!Subtarget->hasSIMD128() &&
1201 Store->getValueOperand()->getType()->isVectorTy())
1202 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001203
1204 Address Addr;
1205 if (!computeAddress(Store->getPointerOperand(), Addr))
1206 return false;
1207
1208 unsigned Opc;
Dan Gohman2e644382016-05-10 17:39:48 +00001209 bool VTIsi1 = false;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001210 switch (getSimpleType(Store->getValueOperand()->getType())) {
1211 case MVT::i1:
1212 VTIsi1 = true;
Dan Gohman861bec22018-02-09 22:59:01 +00001213 LLVM_FALLTHROUGH;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001214 case MVT::i8:
1215 Opc = WebAssembly::STORE8_I32;
Dan Gohman2e644382016-05-10 17:39:48 +00001216 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001217 case MVT::i16:
1218 Opc = WebAssembly::STORE16_I32;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001219 break;
1220 case MVT::i32:
1221 Opc = WebAssembly::STORE_I32;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001222 break;
1223 case MVT::i64:
1224 Opc = WebAssembly::STORE_I64;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001225 break;
1226 case MVT::f32:
Dan Gohman2e644382016-05-10 17:39:48 +00001227 Opc = WebAssembly::STORE_F32;
Dan Gohman2e644382016-05-10 17:39:48 +00001228 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001229 case MVT::f64:
Dan Gohman2e644382016-05-10 17:39:48 +00001230 Opc = WebAssembly::STORE_F64;
Dan Gohman2e644382016-05-10 17:39:48 +00001231 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001232 default:
1233 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001234 }
1235
1236 materializeLoadStoreOperands(Addr);
1237
1238 unsigned ValueReg = getRegForValue(Store->getValueOperand());
Derek Schuff732636d2016-08-04 18:01:52 +00001239 if (ValueReg == 0)
1240 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001241 if (VTIsi1)
Dan Gohman3a5ce732016-05-11 16:32:42 +00001242 ValueReg = maskI1Value(ValueReg, Store->getValueOperand());
Dan Gohman2e644382016-05-10 17:39:48 +00001243
Dan Gohman7f1bdb22016-10-06 22:08:28 +00001244 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
Dan Gohman2e644382016-05-10 17:39:48 +00001245
1246 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Store));
1247
1248 MIB.addReg(ValueReg);
1249 return true;
1250}
1251
1252bool WebAssemblyFastISel::selectBr(const Instruction *I) {
Heejin Ahn18c56a02019-02-04 19:13:39 +00001253 const auto *Br = cast<BranchInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001254 if (Br->isUnconditional()) {
1255 MachineBasicBlock *MSucc = FuncInfo.MBBMap[Br->getSuccessor(0)];
1256 fastEmitBranch(MSucc, Br->getDebugLoc());
1257 return true;
1258 }
1259
1260 MachineBasicBlock *TBB = FuncInfo.MBBMap[Br->getSuccessor(0)];
1261 MachineBasicBlock *FBB = FuncInfo.MBBMap[Br->getSuccessor(1)];
1262
Dan Gohman33e694a2016-05-12 04:19:09 +00001263 bool Not;
1264 unsigned CondReg = getRegForI1Value(Br->getCondition(), Not);
Derek Schuff732636d2016-08-04 18:01:52 +00001265 if (CondReg == 0)
1266 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001267
Dan Gohman33e694a2016-05-12 04:19:09 +00001268 unsigned Opc = WebAssembly::BR_IF;
1269 if (Not)
1270 Opc = WebAssembly::BR_UNLESS;
1271
Dan Gohman2e644382016-05-10 17:39:48 +00001272 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1273 .addMBB(TBB)
1274 .addReg(CondReg);
Derek Schuff39bf39f2016-08-02 23:16:09 +00001275
Dan Gohman2e644382016-05-10 17:39:48 +00001276 finishCondBranch(Br->getParent(), TBB, FBB);
1277 return true;
1278}
1279
1280bool WebAssemblyFastISel::selectRet(const Instruction *I) {
1281 if (!FuncInfo.CanLowerReturn)
1282 return false;
1283
Heejin Ahn18c56a02019-02-04 19:13:39 +00001284 const auto *Ret = cast<ReturnInst>(I);
Dan Gohman2e644382016-05-10 17:39:48 +00001285
1286 if (Ret->getNumOperands() == 0) {
1287 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1288 TII.get(WebAssembly::RETURN_VOID));
1289 return true;
1290 }
1291
1292 Value *RV = Ret->getOperand(0);
Derek Schuff39bf39f2016-08-02 23:16:09 +00001293 if (!Subtarget->hasSIMD128() && RV->getType()->isVectorTy())
1294 return false;
1295
Dan Gohman2e644382016-05-10 17:39:48 +00001296 unsigned Opc;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001297 switch (getSimpleType(RV->getType())) {
Heejin Ahnf208f632018-09-05 01:27:38 +00001298 case MVT::i1:
1299 case MVT::i8:
1300 case MVT::i16:
1301 case MVT::i32:
Dan Gohman3a5ce732016-05-11 16:32:42 +00001302 Opc = WebAssembly::RETURN_I32;
Dan Gohman2e644382016-05-10 17:39:48 +00001303 break;
Dan Gohman3a5ce732016-05-11 16:32:42 +00001304 case MVT::i64:
1305 Opc = WebAssembly::RETURN_I64;
1306 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001307 case MVT::f32:
1308 Opc = WebAssembly::RETURN_F32;
1309 break;
1310 case MVT::f64:
1311 Opc = WebAssembly::RETURN_F64;
1312 break;
1313 case MVT::v16i8:
1314 Opc = WebAssembly::RETURN_v16i8;
1315 break;
1316 case MVT::v8i16:
1317 Opc = WebAssembly::RETURN_v8i16;
1318 break;
1319 case MVT::v4i32:
1320 Opc = WebAssembly::RETURN_v4i32;
1321 break;
Derek Schuff51ed1312018-08-07 21:24:01 +00001322 case MVT::v2i64:
1323 Opc = WebAssembly::RETURN_v2i64;
1324 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +00001325 case MVT::v4f32:
1326 Opc = WebAssembly::RETURN_v4f32;
1327 break;
Derek Schuff51ed1312018-08-07 21:24:01 +00001328 case MVT::v2f64:
1329 Opc = WebAssembly::RETURN_v2f64;
1330 break;
Heejin Ahn0de58722018-03-08 04:05:37 +00001331 case MVT::ExceptRef:
1332 Opc = WebAssembly::RETURN_EXCEPT_REF;
1333 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001334 default:
1335 return false;
Dan Gohman2e644382016-05-10 17:39:48 +00001336 }
1337
Dan Gohman33e694a2016-05-12 04:19:09 +00001338 unsigned Reg;
1339 if (FuncInfo.Fn->getAttributes().hasAttribute(0, Attribute::SExt))
1340 Reg = getRegForSignedValue(RV);
1341 else if (FuncInfo.Fn->getAttributes().hasAttribute(0, Attribute::ZExt))
1342 Reg = getRegForUnsignedValue(RV);
1343 else
1344 Reg = getRegForValue(RV);
1345
Derek Schuff732636d2016-08-04 18:01:52 +00001346 if (Reg == 0)
1347 return false;
1348
Dan Gohman2e644382016-05-10 17:39:48 +00001349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)).addReg(Reg);
1350 return true;
1351}
1352
1353bool WebAssemblyFastISel::selectUnreachable(const Instruction *I) {
1354 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1355 TII.get(WebAssembly::UNREACHABLE));
1356 return true;
1357}
1358
1359bool WebAssemblyFastISel::fastSelectInstruction(const Instruction *I) {
1360 switch (I->getOpcode()) {
Dan Gohman33e694a2016-05-12 04:19:09 +00001361 case Instruction::Call:
1362 if (selectCall(I))
1363 return true;
1364 break;
Heejin Ahnf208f632018-09-05 01:27:38 +00001365 case Instruction::Select:
1366 return selectSelect(I);
1367 case Instruction::Trunc:
1368 return selectTrunc(I);
1369 case Instruction::ZExt:
1370 return selectZExt(I);
1371 case Instruction::SExt:
1372 return selectSExt(I);
1373 case Instruction::ICmp:
1374 return selectICmp(I);
1375 case Instruction::FCmp:
1376 return selectFCmp(I);
1377 case Instruction::BitCast:
1378 return selectBitCast(I);
1379 case Instruction::Load:
1380 return selectLoad(I);
1381 case Instruction::Store:
1382 return selectStore(I);
1383 case Instruction::Br:
1384 return selectBr(I);
1385 case Instruction::Ret:
1386 return selectRet(I);
1387 case Instruction::Unreachable:
1388 return selectUnreachable(I);
1389 default:
1390 break;
Dan Gohman7b634842015-08-24 18:44:37 +00001391 }
1392
1393 // Fall back to target-independent instruction selection.
1394 return selectOperator(I, I->getOpcode());
1395}
1396
1397FastISel *WebAssembly::createFastISel(FunctionLoweringInfo &FuncInfo,
1398 const TargetLibraryInfo *LibInfo) {
1399 return new WebAssemblyFastISel(FuncInfo, LibInfo);
1400}