LLVM 24.0.0git
WebAssemblyFastISel.cpp
Go to the documentation of this file.
1//===-- WebAssemblyFastISel.cpp - WebAssembly FastISel implementation -----===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines the WebAssembly-specific support for the FastISel
11/// class. Some of the target-specific code is generated by tablegen in the file
12/// WebAssemblyGenFastISel.inc, which is #included here.
13///
14/// TODO: kill flags
15///
16//===----------------------------------------------------------------------===//
17
31#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/Function.h"
37#include "llvm/IR/IntrinsicsWebAssembly.h"
38#include "llvm/IR/Operator.h"
39
40using namespace llvm;
41
42#define DEBUG_TYPE "wasm-fastisel"
43
44namespace {
45
46class WebAssemblyFastISel final : public FastISel {
47 // All possible address modes.
48 class Address {
49 public:
50 enum BaseKind { RegBase, FrameIndexBase };
51
52 private:
53 BaseKind Kind = RegBase;
54 union {
55 unsigned Reg;
56 int FI;
57 } Base;
58
59 // Whether the base has been determined yet
60 bool IsBaseSet = false;
61
62 int64_t Offset = 0;
63
64 const GlobalValue *GV = nullptr;
65
66 public:
67 // Innocuous defaults for our address.
68 Address() { Base.Reg = 0; }
69 void setKind(BaseKind K) {
70 assert(!isSet() && "Can't change kind with non-zero base");
71 Kind = K;
72 }
73 BaseKind getKind() const { return Kind; }
74 bool isRegBase() const { return Kind == RegBase; }
75 bool isFIBase() const { return Kind == FrameIndexBase; }
76 void setReg(unsigned Reg) {
77 assert(isRegBase() && "Invalid base register access!");
78 assert(!IsBaseSet && "Base cannot be reset");
79 Base.Reg = Reg;
80 IsBaseSet = true;
81 }
82 unsigned getReg() const {
83 assert(isRegBase() && "Invalid base register access!");
84 return Base.Reg;
85 }
86 void setFI(unsigned FI) {
87 assert(isFIBase() && "Invalid base frame index access!");
88 assert(!IsBaseSet && "Base cannot be reset");
89 Base.FI = FI;
90 IsBaseSet = true;
91 }
92 unsigned getFI() const {
93 assert(isFIBase() && "Invalid base frame index access!");
94 return Base.FI;
95 }
96
97 void setOffset(int64_t NewOffset) {
98 assert(NewOffset >= 0 && "Offsets must be non-negative");
99 Offset = NewOffset;
100 }
101 int64_t getOffset() const { return Offset; }
102 void setGlobalValue(const GlobalValue *G) { GV = G; }
103 const GlobalValue *getGlobalValue() const { return GV; }
104 bool isSet() const { return IsBaseSet; }
105 };
106
107 /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
108 /// right decision when generating code for different targets.
110
111private:
112 // Utility helper routines
113 MVT::SimpleValueType getSimpleType(Type *Ty) {
114 EVT VT = TLI.getValueType(DL, Ty, /*AllowUnknown=*/true);
115 return VT.isSimple() ? VT.getSimpleVT().SimpleTy
117 }
119 switch (VT) {
120 case MVT::i1:
121 case MVT::i8:
122 case MVT::i16:
123 return MVT::i32;
124 case MVT::i32:
125 case MVT::i64:
126 case MVT::f32:
127 case MVT::f64:
128 return VT;
129 case MVT::funcref:
130 case MVT::externref:
131 if (Subtarget->hasReferenceTypes())
132 return VT;
133 break;
134 case MVT::exnref:
135 if (Subtarget->hasReferenceTypes() && Subtarget->hasExceptionHandling())
136 return VT;
137 break;
138 case MVT::f16:
139 return MVT::f32;
140 case MVT::v16i8:
141 case MVT::v8i16:
142 case MVT::v4i32:
143 case MVT::v4f32:
144 case MVT::v2i64:
145 case MVT::v2f64:
146 if (Subtarget->hasSIMD128())
147 return VT;
148 break;
149 default:
150 break;
151 }
153 }
154 bool computeAddress(const Value *Obj, Address &Addr);
155 void materializeLoadStoreOperands(Address &Addr);
156 void addLoadStoreOperands(const Address &Addr, const MachineInstrBuilder &MIB,
157 MachineMemOperand *MMO);
158 bool emitLoad(Register ResultReg, unsigned Opc, const LoadInst *LoadInst);
159 unsigned maskI1Value(unsigned Reg, const Value *V);
160 unsigned getRegForI1Value(const Value *V, const BasicBlock *BB, bool &Not);
161 unsigned zeroExtendToI32(unsigned Reg, const Value *V,
163 unsigned signExtendToI32(unsigned Reg, const Value *V,
165 unsigned zeroExtend(unsigned Reg, const Value *V, MVT::SimpleValueType From,
167 unsigned signExtend(unsigned Reg, const Value *V, MVT::SimpleValueType From,
169 unsigned getRegForUnsignedValue(const Value *V);
170 unsigned getRegForSignedValue(const Value *V);
171 unsigned getRegForPromotedValue(const Value *V, bool IsSigned);
172 unsigned notValue(unsigned Reg);
173 unsigned copyValue(unsigned Reg);
174
175 // Backend specific FastISel code.
176 Register fastMaterializeAlloca(const AllocaInst *AI) override;
177 Register fastMaterializeConstant(const Constant *C) override;
178 bool fastLowerArguments() override;
179
180 // Selection routines.
181 bool selectCall(const Instruction *I);
182 bool selectSelect(const Instruction *I);
183 bool selectTrunc(const Instruction *I);
184 bool selectZExt(const Instruction *I);
185 bool selectSExt(const Instruction *I);
186 bool selectICmp(const Instruction *I);
187 bool selectFCmp(const Instruction *I);
188 bool selectBitCast(const Instruction *I);
189 bool selectLoad(const Instruction *I);
190 bool selectStore(const Instruction *I);
191 bool selectCondBr(const Instruction *I);
192 bool selectRet(const Instruction *I);
193 bool selectUnreachable(const Instruction *I);
194
195public:
196 // Backend specific FastISel code.
197 WebAssemblyFastISel(FunctionLoweringInfo &FuncInfo,
198 const TargetLibraryInfo *LibInfo,
199 const LibcallLoweringInfo *LibcallLowering)
200 : FastISel(FuncInfo, LibInfo, LibcallLowering,
201 /*SkipTargetIndependentISel=*/true) {
203 }
204
205 bool fastSelectInstruction(const Instruction *I) override;
206 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
207 const LoadInst *LI) override;
208
209#include "WebAssemblyGenFastISel.inc"
210};
211
212} // end anonymous namespace
213
214bool WebAssemblyFastISel::computeAddress(const Value *Obj, Address &Addr) {
215 const User *U = nullptr;
216 unsigned Opcode = Instruction::UserOp1;
217 if (const auto *I = dyn_cast<Instruction>(Obj)) {
218 // Don't walk into other basic blocks unless the object is an alloca from
219 // another block, otherwise it may not have a virtual register assigned.
220 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
221 FuncInfo.getMBB(I->getParent()) == FuncInfo.MBB) {
222 Opcode = I->getOpcode();
223 U = I;
224 }
225 } else if (const auto *C = dyn_cast<ConstantExpr>(Obj)) {
226 Opcode = C->getOpcode();
227 U = C;
228 }
229
230 if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
231 if (Ty->getAddressSpace() > 255)
232 // Fast instruction selection doesn't support the special
233 // address spaces.
234 return false;
235
236 if (const auto *GV = dyn_cast<GlobalValue>(Obj)) {
238 return false;
239 if (Addr.getGlobalValue())
240 return false;
241 if (GV->isThreadLocal())
242 return false;
243 Addr.setGlobalValue(GV);
244 return true;
245 }
246
247 switch (Opcode) {
248 default:
249 break;
250 case Instruction::BitCast: {
251 // Look through bitcasts.
252 return computeAddress(U->getOperand(0), Addr);
253 }
254 case Instruction::IntToPtr: {
255 // Look past no-op inttoptrs.
256 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
258 return computeAddress(U->getOperand(0), Addr);
259 break;
260 }
261 case Instruction::PtrToInt: {
262 // Look past no-op ptrtoints.
263 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
264 return computeAddress(U->getOperand(0), Addr);
265 break;
266 }
267 case Instruction::GetElementPtr: {
268 Address SavedAddr = Addr;
269 uint64_t TmpOffset = Addr.getOffset();
270 // Non-inbounds geps can wrap; wasm's offsets can't.
271 if (!cast<GEPOperator>(U)->isInBounds())
272 goto unsupported_gep;
273 // Iterate through the GEP folding the constants into offsets where
274 // we can.
276 GTI != E; ++GTI) {
277 const Value *Op = GTI.getOperand();
278 if (StructType *STy = GTI.getStructTypeOrNull()) {
279 const StructLayout *SL = DL.getStructLayout(STy);
280 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
281 TmpOffset += SL->getElementOffset(Idx);
282 } else {
283 uint64_t S = GTI.getSequentialElementStride(DL);
284 for (;;) {
285 if (const auto *CI = dyn_cast<ConstantInt>(Op)) {
286 // Constant-offset addressing.
287 TmpOffset += CI->getSExtValue() * S;
288 break;
289 }
290 if (S == 1 && Addr.isRegBase() && Addr.getReg() == 0) {
291 // An unscaled add of a register. Set it as the new base.
292 Register Reg = getRegForValue(Op);
293 if (Reg == 0)
294 return false;
295 Addr.setReg(Reg);
296 break;
297 }
298 if (canFoldAddIntoGEP(U, Op)) {
299 // A compatible add with a constant operand. Fold the constant.
300 auto *CI = cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
301 TmpOffset += CI->getSExtValue() * S;
302 // Iterate on the other operand.
303 Op = cast<AddOperator>(Op)->getOperand(0);
304 continue;
305 }
306 // Unsupported
307 goto unsupported_gep;
308 }
309 }
310 }
311 // Don't fold in negative offsets.
312 if (int64_t(TmpOffset) >= 0) {
313 // Try to grab the base operand now.
314 Addr.setOffset(TmpOffset);
315 if (computeAddress(U->getOperand(0), Addr))
316 return true;
317 }
318 // We failed, restore everything and try the other options.
319 Addr = SavedAddr;
320 unsupported_gep:
321 break;
322 }
323 case Instruction::Alloca: {
324 const auto *AI = cast<AllocaInst>(Obj);
325 auto SI = FuncInfo.StaticAllocaMap.find(AI);
326 if (SI != FuncInfo.StaticAllocaMap.end()) {
327 if (Addr.isSet()) {
328 return false;
329 }
330 Addr.setKind(Address::FrameIndexBase);
331 Addr.setFI(SI->second);
332 return true;
333 }
334 break;
335 }
336 case Instruction::Add: {
337 // We should not fold operands into an offset when 'nuw' (no unsigned wrap)
338 // is not present, because the address calculation does not wrap.
339 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(U))
340 if (!OFBinOp->hasNoUnsignedWrap())
341 break;
342
343 // Adds of constants are common and easy enough.
344 const Value *LHS = U->getOperand(0);
345 const Value *RHS = U->getOperand(1);
346
348 std::swap(LHS, RHS);
349
350 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
351 uint64_t TmpOffset = Addr.getOffset() + CI->getSExtValue();
352 if (int64_t(TmpOffset) >= 0) {
353 Addr.setOffset(TmpOffset);
354 return computeAddress(LHS, Addr);
355 }
356 }
357
358 Address Backup = Addr;
359 if (computeAddress(LHS, Addr) && computeAddress(RHS, Addr))
360 return true;
361 Addr = Backup;
362
363 break;
364 }
365 case Instruction::Sub: {
366 // We should not fold operands into an offset when 'nuw' (no unsigned wrap)
367 // is not present, because the address calculation does not wrap.
368 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(U))
369 if (!OFBinOp->hasNoUnsignedWrap())
370 break;
371
372 // Subs of constants are common and easy enough.
373 const Value *LHS = U->getOperand(0);
374 const Value *RHS = U->getOperand(1);
375
376 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
377 int64_t TmpOffset = Addr.getOffset() - CI->getSExtValue();
378 if (TmpOffset >= 0) {
379 Addr.setOffset(TmpOffset);
380 return computeAddress(LHS, Addr);
381 }
382 }
383 break;
384 }
385 }
386 if (Addr.isSet()) {
387 return false;
388 }
389 Register Reg = getRegForValue(Obj);
390 if (Reg == 0)
391 return false;
392 Addr.setReg(Reg);
393 return Addr.getReg() != 0;
394}
395
396void WebAssemblyFastISel::materializeLoadStoreOperands(Address &Addr) {
397 if (Addr.isRegBase()) {
398 unsigned Reg = Addr.getReg();
399 if (Reg == 0) {
400 Reg = createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
401 : &WebAssembly::I32RegClass);
402 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
403 : WebAssembly::CONST_I32;
404 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), Reg)
405 .addImm(0);
406 Addr.setReg(Reg);
407 }
408 }
409}
410
411void WebAssemblyFastISel::addLoadStoreOperands(const Address &Addr,
412 const MachineInstrBuilder &MIB,
413 MachineMemOperand *MMO) {
414 // Set the alignment operand (this is rewritten in SetP2AlignOperands).
415 // TODO: Disable SetP2AlignOperands for FastISel and just do it here.
416 MIB.addImm(0);
417
418 if (const GlobalValue *GV = Addr.getGlobalValue())
419 MIB.addGlobalAddress(GV, Addr.getOffset());
420 else
421 MIB.addImm(Addr.getOffset());
422
423 if (Addr.isRegBase())
424 MIB.addReg(Addr.getReg());
425 else
426 MIB.addFrameIndex(Addr.getFI());
427
428 MIB.addMemOperand(MMO);
429}
430
431bool WebAssemblyFastISel::emitLoad(Register ResultReg, unsigned Opc,
432 const LoadInst *Load) {
433 Address Addr;
434 if (!computeAddress(Load->getPointerOperand(), Addr))
435 return false;
436
437 materializeLoadStoreOperands(Addr);
438 auto MIB =
439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg);
440 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Load));
441
442 return true;
443}
444
445unsigned WebAssemblyFastISel::maskI1Value(unsigned Reg, const Value *V) {
446 return zeroExtendToI32(Reg, V, MVT::i1);
447}
448
449unsigned WebAssemblyFastISel::getRegForI1Value(const Value *V,
450 const BasicBlock *BB,
451 bool &Not) {
452 if (const auto *ICmp = dyn_cast<ICmpInst>(V))
453 if (const ConstantInt *C = dyn_cast<ConstantInt>(ICmp->getOperand(1)))
454 if (ICmp->isEquality() && C->isZero() && C->getType()->isIntegerTy(32) &&
455 ICmp->getParent() == BB) {
456 Not = ICmp->isTrueWhenEqual();
457 return getRegForValue(ICmp->getOperand(0));
458 }
459
460 Not = false;
461 Register Reg = getRegForValue(V);
462 if (Reg == 0)
463 return 0;
464 return maskI1Value(Reg, V);
465}
466
467unsigned WebAssemblyFastISel::zeroExtendToI32(unsigned Reg, const Value *V,
469 if (Reg == 0)
470 return 0;
471
472 switch (From) {
473 case MVT::i1:
474 // If the value is naturally an i1, we don't need to mask it. We only know
475 // if a value is naturally an i1 if it is definitely lowered by FastISel,
476 // not a DAG ISel fallback.
477 if (V != nullptr && isa<Argument>(V) && cast<Argument>(V)->hasZExtAttr())
478 return copyValue(Reg);
479 break;
480 case MVT::i8:
481 case MVT::i16:
482 break;
483 case MVT::i32:
484 return copyValue(Reg);
485 default:
486 return 0;
487 }
488
489 Register Imm = createResultReg(&WebAssembly::I32RegClass);
490 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
491 TII.get(WebAssembly::CONST_I32), Imm)
492 .addImm(~(~uint64_t(0) << MVT(From).getSizeInBits()));
493
494 Register Result = createResultReg(&WebAssembly::I32RegClass);
495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(WebAssembly::AND_I32),
496 Result)
497 .addReg(Reg)
498 .addReg(Imm);
499
500 return Result;
501}
502
503unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
505 if (Reg == 0)
506 return 0;
507
508 switch (From) {
509 case MVT::i1:
510 case MVT::i8:
511 case MVT::i16:
512 break;
513 case MVT::i32:
514 return copyValue(Reg);
515 default:
516 return 0;
517 }
518
519 if (Subtarget->hasSignExt()) {
520 if (From == MVT::i8 || From == MVT::i16) {
521 Register Result = createResultReg(&WebAssembly::I32RegClass);
522 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
523 TII.get(From == MVT::i16 ? WebAssembly::I32_EXTEND16_S_I32
524 : WebAssembly::I32_EXTEND8_S_I32),
525 Result)
526 .addReg(Reg);
527 return Result;
528 }
529 }
530
531 Register Imm = createResultReg(&WebAssembly::I32RegClass);
532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
533 TII.get(WebAssembly::CONST_I32), Imm)
534 .addImm(32 - MVT(From).getSizeInBits());
535
536 Register Left = createResultReg(&WebAssembly::I32RegClass);
537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(WebAssembly::SHL_I32),
538 Left)
539 .addReg(Reg)
540 .addReg(Imm);
541
542 Register Right = createResultReg(&WebAssembly::I32RegClass);
543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
544 TII.get(WebAssembly::SHR_S_I32), Right)
545 .addReg(Left)
546 .addReg(Imm);
547
548 return Right;
549}
550
551unsigned WebAssemblyFastISel::zeroExtend(unsigned Reg, const Value *V,
554 if (To == MVT::i64) {
555 if (From == MVT::i64)
556 return copyValue(Reg);
557
558 Reg = zeroExtendToI32(Reg, V, From);
559
560 Register Result = createResultReg(&WebAssembly::I64RegClass);
561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
562 TII.get(WebAssembly::I64_EXTEND_U_I32), Result)
563 .addReg(Reg);
564 return Result;
565 }
566
567 if (To == MVT::i32)
568 return zeroExtendToI32(Reg, V, From);
569
570 return 0;
571}
572
573unsigned WebAssemblyFastISel::signExtend(unsigned Reg, const Value *V,
576 if (To == MVT::i64) {
577 if (From == MVT::i64)
578 return copyValue(Reg);
579
580 Register Result = createResultReg(&WebAssembly::I64RegClass);
581
582 if (Subtarget->hasSignExt()) {
583 switch (From) {
584 case MVT::i8:
585 case MVT::i16: {
586 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
587 TII.get(WebAssembly::I64_EXTEND_U_I32), Result)
588 .addReg(Reg);
589
590 Reg = Result;
591 Result = createResultReg(&WebAssembly::I64RegClass);
592
593 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
594 TII.get(From == MVT::i8 ? WebAssembly::I64_EXTEND8_S_I64
595 : WebAssembly::I64_EXTEND16_S_I64),
596 Result)
597 .addReg(Reg);
598 return Result;
599 }
600 case MVT::i32:
601 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
602 TII.get(WebAssembly::I64_EXTEND_S_I32), Result)
603 .addReg(Reg);
604 return Result;
605 default:
606 break;
607 }
608 }
609
610 Reg = signExtendToI32(Reg, V, From);
611 if (Reg == 0)
612 return 0;
613
614 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
615 TII.get(WebAssembly::I64_EXTEND_S_I32), Result)
616 .addReg(Reg);
617 return Result;
618 }
619
620 if (To == MVT::i32)
621 return signExtendToI32(Reg, V, From);
622
623 return 0;
624}
625
626unsigned WebAssemblyFastISel::getRegForUnsignedValue(const Value *V) {
627 MVT::SimpleValueType From = getSimpleType(V->getType());
628 MVT::SimpleValueType To = getLegalType(From);
629 Register VReg = getRegForValue(V);
630 if (VReg == 0)
631 return 0;
632 if (From == To)
633 return VReg;
634 return zeroExtend(VReg, V, From, To);
635}
636
637unsigned WebAssemblyFastISel::getRegForSignedValue(const Value *V) {
638 MVT::SimpleValueType From = getSimpleType(V->getType());
639 MVT::SimpleValueType To = getLegalType(From);
640 Register VReg = getRegForValue(V);
641 if (VReg == 0)
642 return 0;
643 if (From == To)
644 return VReg;
645 return signExtend(VReg, V, From, To);
646}
647
648unsigned WebAssemblyFastISel::getRegForPromotedValue(const Value *V,
649 bool IsSigned) {
650 return IsSigned ? getRegForSignedValue(V) : getRegForUnsignedValue(V);
651}
652
653unsigned WebAssemblyFastISel::notValue(unsigned Reg) {
654 assert(MRI.getRegClass(Reg) == &WebAssembly::I32RegClass);
655
656 Register NotReg = createResultReg(&WebAssembly::I32RegClass);
657 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(WebAssembly::EQZ_I32),
658 NotReg)
659 .addReg(Reg);
660 return NotReg;
661}
662
663unsigned WebAssemblyFastISel::copyValue(unsigned Reg) {
664 Register ResultReg = createResultReg(MRI.getRegClass(Reg));
665 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(WebAssembly::COPY),
666 ResultReg)
667 .addReg(Reg);
668 return ResultReg;
669}
670
671Register WebAssemblyFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
672 auto SI = FuncInfo.StaticAllocaMap.find(AI);
673
674 if (SI != FuncInfo.StaticAllocaMap.end()) {
675 Register ResultReg =
676 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
677 : &WebAssembly::I32RegClass);
678 unsigned Opc =
679 Subtarget->hasAddr64() ? WebAssembly::COPY_I64 : WebAssembly::COPY_I32;
680 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
681 .addFrameIndex(SI->second);
682 return ResultReg;
683 }
684
685 return Register();
686}
687
688Register WebAssemblyFastISel::fastMaterializeConstant(const Constant *C) {
689 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
691 return Register();
692 if (GV->isThreadLocal())
693 return Register();
694 Register ResultReg =
695 createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass
696 : &WebAssembly::I32RegClass);
697 unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64
698 : WebAssembly::CONST_I32;
699 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
700 .addGlobalAddress(GV);
701 return ResultReg;
702 }
703
704 // Let target-independent code handle it.
705 return Register();
706}
707
708bool WebAssemblyFastISel::fastLowerArguments() {
709 if (!FuncInfo.CanLowerReturn)
710 return false;
711
712 const Function *F = FuncInfo.Fn;
713 if (F->isVarArg())
714 return false;
715
716 if (FuncInfo.Fn->getCallingConv() == CallingConv::Swift ||
717 FuncInfo.Fn->getCallingConv() == CallingConv::SwiftTail)
718 return false;
719
720 unsigned I = 0;
721 for (auto const &Arg : F->args()) {
722 const AttributeList &Attrs = F->getAttributes();
723 if (Attrs.hasParamAttr(I, Attribute::ByVal) ||
724 Attrs.hasParamAttr(I, Attribute::SwiftSelf) ||
725 Attrs.hasParamAttr(I, Attribute::SwiftError) ||
726 Attrs.hasParamAttr(I, Attribute::SwiftAsync) ||
727 Attrs.hasParamAttr(I, Attribute::InAlloca) ||
728 Attrs.hasParamAttr(I, Attribute::Nest))
729 return false;
730
731 Type *ArgTy = Arg.getType();
732 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
733 return false;
734 if (!Subtarget->hasSIMD128() && ArgTy->isVectorTy())
735 return false;
736
737 unsigned Opc;
738 const TargetRegisterClass *RC;
739 switch (getSimpleType(ArgTy)) {
740 case MVT::i1:
741 case MVT::i8:
742 case MVT::i16:
743 case MVT::i32:
744 Opc = WebAssembly::ARGUMENT_i32;
745 RC = &WebAssembly::I32RegClass;
746 break;
747 case MVT::i64:
748 Opc = WebAssembly::ARGUMENT_i64;
749 RC = &WebAssembly::I64RegClass;
750 break;
751 case MVT::f32:
752 Opc = WebAssembly::ARGUMENT_f32;
753 RC = &WebAssembly::F32RegClass;
754 break;
755 case MVT::f64:
756 Opc = WebAssembly::ARGUMENT_f64;
757 RC = &WebAssembly::F64RegClass;
758 break;
759 case MVT::v16i8:
760 Opc = WebAssembly::ARGUMENT_v16i8;
761 RC = &WebAssembly::V128RegClass;
762 break;
763 case MVT::v8i16:
764 Opc = WebAssembly::ARGUMENT_v8i16;
765 RC = &WebAssembly::V128RegClass;
766 break;
767 case MVT::v4i32:
768 Opc = WebAssembly::ARGUMENT_v4i32;
769 RC = &WebAssembly::V128RegClass;
770 break;
771 case MVT::v2i64:
772 Opc = WebAssembly::ARGUMENT_v2i64;
773 RC = &WebAssembly::V128RegClass;
774 break;
775 case MVT::v4f32:
776 Opc = WebAssembly::ARGUMENT_v4f32;
777 RC = &WebAssembly::V128RegClass;
778 break;
779 case MVT::v2f64:
780 Opc = WebAssembly::ARGUMENT_v2f64;
781 RC = &WebAssembly::V128RegClass;
782 break;
783 case MVT::funcref:
784 Opc = WebAssembly::ARGUMENT_funcref;
785 RC = &WebAssembly::FUNCREFRegClass;
786 break;
787 case MVT::externref:
788 Opc = WebAssembly::ARGUMENT_externref;
789 RC = &WebAssembly::EXTERNREFRegClass;
790 break;
791 case MVT::exnref:
792 Opc = WebAssembly::ARGUMENT_exnref;
793 RC = &WebAssembly::EXNREFRegClass;
794 break;
795 default:
796 return false;
797 }
798 Register ResultReg = createResultReg(RC);
799 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
800 .addImm(I);
801 updateValueMap(&Arg, ResultReg);
802
803 ++I;
804 }
805
806 MRI.addLiveIn(WebAssembly::ARGUMENTS);
807
808 auto *MFI = MF->getInfo<WebAssemblyFunctionInfo>();
809 for (auto const &Arg : F->args()) {
810 MVT::SimpleValueType ArgTy = getLegalType(getSimpleType(Arg.getType()));
811 if (ArgTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
812 MFI->clearParamsAndResults();
813 return false;
814 }
815 MFI->addParam(ArgTy);
816 }
817
818 if (!F->getReturnType()->isVoidTy()) {
820 getLegalType(getSimpleType(F->getReturnType()));
821 if (RetTy == MVT::INVALID_SIMPLE_VALUE_TYPE) {
822 MFI->clearParamsAndResults();
823 return false;
824 }
825 MFI->addResult(RetTy);
826 }
827
828 return true;
829}
830
831bool WebAssemblyFastISel::selectCall(const Instruction *I) {
832 const auto *Call = cast<CallInst>(I);
833
834 // TODO: Support tail calls in FastISel
835 if (Call->isMustTailCall() || Call->isInlineAsm() ||
837 return false;
838
840 if (Func && Func->isIntrinsic())
841 return false;
842
843 if (Call->getCallingConv() == CallingConv::Swift ||
844 Call->getCallingConv() == CallingConv::SwiftTail)
845 return false;
846
847 bool IsDirect = Func != nullptr;
848 if (!IsDirect && isa<ConstantExpr>(Call->getCalledOperand()))
849 return false;
850
851 FunctionType *FuncTy = Call->getFunctionType();
852 unsigned Opc = IsDirect ? WebAssembly::CALL : WebAssembly::CALL_INDIRECT;
853 bool IsVoid = FuncTy->getReturnType()->isVoidTy();
854 unsigned ResultReg;
855 if (!IsVoid) {
856 if (!Subtarget->hasSIMD128() && Call->getType()->isVectorTy())
857 return false;
858
859 MVT::SimpleValueType RetTy = getSimpleType(Call->getType());
860 switch (RetTy) {
861 case MVT::i1:
862 case MVT::i8:
863 case MVT::i16:
864 case MVT::i32:
865 ResultReg = createResultReg(&WebAssembly::I32RegClass);
866 break;
867 case MVT::i64:
868 ResultReg = createResultReg(&WebAssembly::I64RegClass);
869 break;
870 case MVT::f32:
871 ResultReg = createResultReg(&WebAssembly::F32RegClass);
872 break;
873 case MVT::f64:
874 ResultReg = createResultReg(&WebAssembly::F64RegClass);
875 break;
876 case MVT::v16i8:
877 ResultReg = createResultReg(&WebAssembly::V128RegClass);
878 break;
879 case MVT::v8i16:
880 ResultReg = createResultReg(&WebAssembly::V128RegClass);
881 break;
882 case MVT::v4i32:
883 ResultReg = createResultReg(&WebAssembly::V128RegClass);
884 break;
885 case MVT::v2i64:
886 ResultReg = createResultReg(&WebAssembly::V128RegClass);
887 break;
888 case MVT::v4f32:
889 ResultReg = createResultReg(&WebAssembly::V128RegClass);
890 break;
891 case MVT::v2f64:
892 ResultReg = createResultReg(&WebAssembly::V128RegClass);
893 break;
894 case MVT::funcref:
895 ResultReg = createResultReg(&WebAssembly::FUNCREFRegClass);
896 break;
897 case MVT::externref:
898 ResultReg = createResultReg(&WebAssembly::EXTERNREFRegClass);
899 break;
900 case MVT::exnref:
901 ResultReg = createResultReg(&WebAssembly::EXNREFRegClass);
902 break;
903 default:
904 return false;
905 }
906 }
907
908 SmallVector<unsigned, 8> Args;
909 for (unsigned I = 0, E = Call->arg_size(); I < E; ++I) {
911 MVT::SimpleValueType ArgTy = getSimpleType(V->getType());
913 return false;
914
915 // The attribute of the callee counts too, see computeSignatureVTs.
916 const AttributeList &Attrs = Call->getAttributes();
917 if (Attrs.hasParamAttr(I, Attribute::ByVal) ||
918 Attrs.hasParamAttr(I, Attribute::SwiftSelf) ||
919 Attrs.hasParamAttr(I, Attribute::SwiftError) ||
920 Call->paramHasAttr(I, Attribute::SwiftAsync) ||
921 Attrs.hasParamAttr(I, Attribute::InAlloca) ||
922 Attrs.hasParamAttr(I, Attribute::Nest))
923 return false;
924
925 unsigned Reg;
926
927 if (Call->paramHasAttr(I, Attribute::SExt))
928 Reg = getRegForSignedValue(V);
929 else if (Call->paramHasAttr(I, Attribute::ZExt))
930 Reg = getRegForUnsignedValue(V);
931 else
932 Reg = getRegForValue(V);
933
934 if (Reg == 0)
935 return false;
936
937 Args.push_back(Reg);
938 }
939
940 unsigned CalleeReg = 0;
941 // A call through a funcref is expressed as a call through the pointer
942 // produced by llvm.wasm.funcref.to_ptr. Recover the funcref operand, place it
943 // into __funcref_call_table, and call it.
944 //
945 // TODO: Use call_ref if wasm-gc feature is available, would lead to simpler
946 // code here.
947 const Value *FuncrefArg = nullptr;
948 if (const auto *Conv = dyn_cast<CallInst>(Call->getCalledOperand()))
949 if (Conv->getIntrinsicID() == Intrinsic::wasm_funcref_to_ptr)
950 FuncrefArg = Conv->getArgOperand(0);
951
952 const bool IsFuncrefCall = FuncrefArg != nullptr;
953 MCSymbolWasm *Table = nullptr;
954
955 if (!IsDirect) {
956 if (!IsFuncrefCall) {
957 // Table is ___indirect_function_table
958 Table = WebAssembly::getOrCreateFunctionTableSymbol(MF->getContext(),
959 Subtarget);
960 CalleeReg = getRegForValue(Call->getCalledOperand());
961 if (!CalleeReg)
962 return false;
963 } else {
964 // Table is __funcref_call_table
965 Table = WebAssembly::getOrCreateFuncrefCallTableSymbol(MF->getContext(),
966 Subtarget);
967 CalleeReg = getRegForValue(FuncrefArg);
968 // Put the funcref in slot 0 of __funcref_call_table
969 unsigned ZeroReg = createResultReg(&WebAssembly::I32RegClass);
970 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
971 TII.get(WebAssembly::CONST_I32), ZeroReg)
972 .addImm(0);
973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
974 TII.get(WebAssembly::TABLE_SET_FUNCREF))
975 .addSym(Table)
976 .addReg(ZeroReg)
977 .addReg(CalleeReg);
978 // Set CalleeReg to an immediate 0
979 CalleeReg = createResultReg(&WebAssembly::I32RegClass);
980 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
981 TII.get(WebAssembly::CONST_I32), CalleeReg)
982 .addImm(0);
983 }
984 }
985
986 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc));
987
988 if (!IsVoid)
989 MIB.addReg(ResultReg, RegState::Define);
990
991 if (IsDirect) {
992 MIB.addGlobalAddress(Func);
993 } else {
994 // Placeholder for the type index.
995 MIB.addImm(0);
996 if (Subtarget->hasCallIndirectOverlong()) {
997 MIB.addSym(Table);
998 } else {
999 // Otherwise for the MVP there is at most one table whose number is 0, but
1000 // we can't write a table symbol or issue relocations. Instead we just
1001 // ensure the table is live.
1002 Table->setNoStrip();
1003 MIB.addImm(0);
1004 }
1005 }
1006
1007 for (unsigned ArgReg : Args)
1008 MIB.addReg(ArgReg);
1009
1010 if (!IsDirect)
1011 MIB.addReg(CalleeReg);
1012
1013 if (IsFuncrefCall) {
1014 // Clear slot 0 of the funcref call table after the call.
1015 unsigned ZeroReg = createResultReg(&WebAssembly::I32RegClass);
1016 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1017 TII.get(WebAssembly::CONST_I32), ZeroReg)
1018 .addImm(0);
1019 unsigned NullReg = createResultReg(&WebAssembly::FUNCREFRegClass);
1020 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1021 TII.get(WebAssembly::REF_NULL_FUNCREF), NullReg);
1022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1023 TII.get(WebAssembly::TABLE_SET_FUNCREF))
1024 .addSym(Table)
1025 .addReg(ZeroReg)
1026 .addReg(NullReg);
1027 }
1028
1029 if (!IsVoid)
1030 updateValueMap(Call, ResultReg);
1031
1033 return true;
1034}
1035
1036bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
1037 const auto *Select = cast<SelectInst>(I);
1038
1039 bool Not;
1040 unsigned CondReg =
1041 getRegForI1Value(Select->getCondition(), I->getParent(), Not);
1042 if (CondReg == 0)
1043 return false;
1044
1045 Register TrueReg = getRegForValue(Select->getTrueValue());
1046 if (TrueReg == 0)
1047 return false;
1048
1049 Register FalseReg = getRegForValue(Select->getFalseValue());
1050 if (FalseReg == 0)
1051 return false;
1052
1053 if (Not)
1054 std::swap(TrueReg, FalseReg);
1055
1056 unsigned Opc;
1057 const TargetRegisterClass *RC;
1058 switch (getSimpleType(Select->getType())) {
1059 case MVT::i1:
1060 case MVT::i8:
1061 case MVT::i16:
1062 case MVT::i32:
1063 Opc = WebAssembly::SELECT_I32;
1064 RC = &WebAssembly::I32RegClass;
1065 break;
1066 case MVT::i64:
1067 Opc = WebAssembly::SELECT_I64;
1068 RC = &WebAssembly::I64RegClass;
1069 break;
1070 case MVT::f32:
1071 Opc = WebAssembly::SELECT_F32;
1072 RC = &WebAssembly::F32RegClass;
1073 break;
1074 case MVT::f64:
1075 Opc = WebAssembly::SELECT_F64;
1076 RC = &WebAssembly::F64RegClass;
1077 break;
1078 case MVT::funcref:
1079 Opc = WebAssembly::SELECT_FUNCREF;
1080 RC = &WebAssembly::FUNCREFRegClass;
1081 break;
1082 case MVT::externref:
1083 Opc = WebAssembly::SELECT_EXTERNREF;
1084 RC = &WebAssembly::EXTERNREFRegClass;
1085 break;
1086 case MVT::exnref:
1087 Opc = WebAssembly::SELECT_EXNREF;
1088 RC = &WebAssembly::EXNREFRegClass;
1089 break;
1090 default:
1091 return false;
1092 }
1093
1094 Register ResultReg = createResultReg(RC);
1095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
1096 .addReg(TrueReg)
1097 .addReg(FalseReg)
1098 .addReg(CondReg);
1099
1100 updateValueMap(Select, ResultReg);
1101 return true;
1102}
1103
1104bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
1105 const auto *Trunc = cast<TruncInst>(I);
1106
1107 const Value *Op = Trunc->getOperand(0);
1108 MVT::SimpleValueType From = getSimpleType(Op->getType());
1109 MVT::SimpleValueType To = getLegalType(getSimpleType(Trunc->getType()));
1110 Register In = getRegForValue(Op);
1111 if (In == 0)
1112 return false;
1113
1114 auto Truncate = [&](Register Reg) -> unsigned {
1115 if (From == MVT::i64) {
1116 if (To == MVT::i64)
1117 return copyValue(Reg);
1118
1119 if (To == MVT::i1 || To == MVT::i8 || To == MVT::i16 || To == MVT::i32) {
1120 Register Result = createResultReg(&WebAssembly::I32RegClass);
1121 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1122 TII.get(WebAssembly::I32_WRAP_I64), Result)
1123 .addReg(Reg);
1124 return Result;
1125 }
1126 }
1127
1128 if (From == MVT::i32)
1129 return copyValue(Reg);
1130
1131 return 0;
1132 };
1133
1134 unsigned Reg = Truncate(In);
1135 if (Reg == 0)
1136 return false;
1137
1138 updateValueMap(Trunc, Reg);
1139 return true;
1140}
1141
1142bool WebAssemblyFastISel::selectZExt(const Instruction *I) {
1143 const auto *ZExt = cast<ZExtInst>(I);
1144
1145 const Value *Op = ZExt->getOperand(0);
1146 MVT::SimpleValueType From = getSimpleType(Op->getType());
1147 MVT::SimpleValueType To = getLegalType(getSimpleType(ZExt->getType()));
1148 Register In = getRegForValue(Op);
1149 if (In == 0)
1150 return false;
1151 unsigned Reg = zeroExtend(In, Op, From, To);
1152 if (Reg == 0)
1153 return false;
1154
1155 updateValueMap(ZExt, Reg);
1156 return true;
1157}
1158
1159bool WebAssemblyFastISel::selectSExt(const Instruction *I) {
1160 const auto *SExt = cast<SExtInst>(I);
1161
1162 const Value *Op = SExt->getOperand(0);
1163 MVT::SimpleValueType From = getSimpleType(Op->getType());
1164 MVT::SimpleValueType To = getLegalType(getSimpleType(SExt->getType()));
1165 Register In = getRegForValue(Op);
1166 if (In == 0)
1167 return false;
1168 unsigned Reg = signExtend(In, Op, From, To);
1169 if (Reg == 0)
1170 return false;
1171
1172 updateValueMap(SExt, Reg);
1173 return true;
1174}
1175
1176bool WebAssemblyFastISel::selectICmp(const Instruction *I) {
1177 const auto *ICmp = cast<ICmpInst>(I);
1178
1179 // The I32 test below classifies every non-i64 type as i32, so a vector
1180 // compare would emit a scalar compare over v128 registers and produce
1181 // an invalid module. The SelectionDAG lowers vector compares to SIMD
1182 // compares.
1183 if (ICmp->getOperand(0)->getType()->isVectorTy())
1184 return false;
1185
1186 bool I32 = getSimpleType(ICmp->getOperand(0)->getType()) != MVT::i64;
1187 unsigned Opc;
1188 bool IsSigned = false;
1189 switch (ICmp->getPredicate()) {
1190 case ICmpInst::ICMP_EQ:
1191 Opc = I32 ? WebAssembly::EQ_I32 : WebAssembly::EQ_I64;
1192 break;
1193 case ICmpInst::ICMP_NE:
1194 Opc = I32 ? WebAssembly::NE_I32 : WebAssembly::NE_I64;
1195 break;
1196 case ICmpInst::ICMP_UGT:
1197 Opc = I32 ? WebAssembly::GT_U_I32 : WebAssembly::GT_U_I64;
1198 break;
1199 case ICmpInst::ICMP_UGE:
1200 Opc = I32 ? WebAssembly::GE_U_I32 : WebAssembly::GE_U_I64;
1201 break;
1202 case ICmpInst::ICMP_ULT:
1203 Opc = I32 ? WebAssembly::LT_U_I32 : WebAssembly::LT_U_I64;
1204 break;
1205 case ICmpInst::ICMP_ULE:
1206 Opc = I32 ? WebAssembly::LE_U_I32 : WebAssembly::LE_U_I64;
1207 break;
1208 case ICmpInst::ICMP_SGT:
1209 Opc = I32 ? WebAssembly::GT_S_I32 : WebAssembly::GT_S_I64;
1210 IsSigned = true;
1211 break;
1212 case ICmpInst::ICMP_SGE:
1213 Opc = I32 ? WebAssembly::GE_S_I32 : WebAssembly::GE_S_I64;
1214 IsSigned = true;
1215 break;
1216 case ICmpInst::ICMP_SLT:
1217 Opc = I32 ? WebAssembly::LT_S_I32 : WebAssembly::LT_S_I64;
1218 IsSigned = true;
1219 break;
1220 case ICmpInst::ICMP_SLE:
1221 Opc = I32 ? WebAssembly::LE_S_I32 : WebAssembly::LE_S_I64;
1222 IsSigned = true;
1223 break;
1224 default:
1225 return false;
1226 }
1227
1228 unsigned LHS = getRegForPromotedValue(ICmp->getOperand(0), IsSigned);
1229 if (LHS == 0)
1230 return false;
1231
1232 unsigned RHS = getRegForPromotedValue(ICmp->getOperand(1), IsSigned);
1233 if (RHS == 0)
1234 return false;
1235
1236 Register ResultReg = createResultReg(&WebAssembly::I32RegClass);
1237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
1238 .addReg(LHS)
1239 .addReg(RHS);
1240 updateValueMap(ICmp, ResultReg);
1241 return true;
1242}
1243
1244bool WebAssemblyFastISel::selectFCmp(const Instruction *I) {
1245 const auto *FCmp = cast<FCmpInst>(I);
1246
1247 // The F32 test below classifies every non-f64 type as f32, so a vector
1248 // compare would emit a scalar compare over v128 registers and produce
1249 // an invalid module. The SelectionDAG lowers vector compares to SIMD
1250 // compares.
1251 if (FCmp->getOperand(0)->getType()->isVectorTy())
1252 return false;
1253
1254 Register LHS = getRegForValue(FCmp->getOperand(0));
1255 if (LHS == 0)
1256 return false;
1257
1258 Register RHS = getRegForValue(FCmp->getOperand(1));
1259 if (RHS == 0)
1260 return false;
1261
1262 bool F32 = getSimpleType(FCmp->getOperand(0)->getType()) != MVT::f64;
1263 unsigned Opc;
1264 bool Not = false;
1265 switch (FCmp->getPredicate()) {
1266 case FCmpInst::FCMP_OEQ:
1267 Opc = F32 ? WebAssembly::EQ_F32 : WebAssembly::EQ_F64;
1268 break;
1269 case FCmpInst::FCMP_UNE:
1270 Opc = F32 ? WebAssembly::NE_F32 : WebAssembly::NE_F64;
1271 break;
1272 case FCmpInst::FCMP_OGT:
1273 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1274 break;
1275 case FCmpInst::FCMP_OGE:
1276 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1277 break;
1278 case FCmpInst::FCMP_OLT:
1279 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1280 break;
1281 case FCmpInst::FCMP_OLE:
1282 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1283 break;
1284 case FCmpInst::FCMP_UGT:
1285 Opc = F32 ? WebAssembly::LE_F32 : WebAssembly::LE_F64;
1286 Not = true;
1287 break;
1288 case FCmpInst::FCMP_UGE:
1289 Opc = F32 ? WebAssembly::LT_F32 : WebAssembly::LT_F64;
1290 Not = true;
1291 break;
1292 case FCmpInst::FCMP_ULT:
1293 Opc = F32 ? WebAssembly::GE_F32 : WebAssembly::GE_F64;
1294 Not = true;
1295 break;
1296 case FCmpInst::FCMP_ULE:
1297 Opc = F32 ? WebAssembly::GT_F32 : WebAssembly::GT_F64;
1298 Not = true;
1299 break;
1300 default:
1301 return false;
1302 }
1303
1304 Register ResultReg = createResultReg(&WebAssembly::I32RegClass);
1305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
1306 .addReg(LHS)
1307 .addReg(RHS);
1308
1309 if (Not)
1310 ResultReg = notValue(ResultReg);
1311
1312 updateValueMap(FCmp, ResultReg);
1313 return true;
1314}
1315
1316bool WebAssemblyFastISel::selectBitCast(const Instruction *I) {
1317 // Target-independent code can handle this, except it doesn't set the dead
1318 // flag on the ARGUMENTS clobber, so we have to do that manually in order
1319 // to satisfy code that expects this of isBitcast() instructions.
1320 EVT VT = TLI.getValueType(DL, I->getOperand(0)->getType());
1321 EVT RetVT = TLI.getValueType(DL, I->getType());
1322 if (!VT.isSimple() || !RetVT.isSimple())
1323 return false;
1324
1325 Register In = getRegForValue(I->getOperand(0));
1326 if (In == 0)
1327 return false;
1328
1329 if (VT == RetVT) {
1330 // No-op bitcast.
1331 updateValueMap(I, In);
1332 return true;
1333 }
1334
1335 Register Reg =
1336 fastEmit_ISD_BITCAST_r(VT.getSimpleVT(), RetVT.getSimpleVT(), In);
1337 if (!Reg)
1338 return false;
1339 MachineBasicBlock::iterator Iter = FuncInfo.InsertPt;
1340 --Iter;
1341 assert(Iter->isBitcast());
1342 Iter->setImplicitPhysRegDefsDead();
1343 updateValueMap(I, Reg);
1344 return true;
1345}
1346
1347static unsigned getSExtLoadOpcode(unsigned LoadSize, bool I64Result, bool A64) {
1348 if (I64Result) {
1349 switch (LoadSize) {
1350 default:
1351 return WebAssembly::INSTRUCTION_LIST_END;
1352 case 8:
1353 return A64 ? WebAssembly::LOAD8_S_I64_A64 : WebAssembly::LOAD8_S_I64_A32;
1354 case 16:
1355 return A64 ? WebAssembly::LOAD16_S_I64_A64
1356 : WebAssembly::LOAD16_S_I64_A32;
1357 case 32:
1358 return A64 ? WebAssembly::LOAD32_S_I64_A64
1359 : WebAssembly::LOAD32_S_I64_A32;
1360 }
1361 }
1362
1363 switch (LoadSize) {
1364 default:
1365 return WebAssembly::INSTRUCTION_LIST_END;
1366 case 8:
1367 return A64 ? WebAssembly::LOAD8_S_I32_A64 : WebAssembly::LOAD8_S_I32_A32;
1368 case 16:
1369 return A64 ? WebAssembly::LOAD16_S_I32_A64 : WebAssembly::LOAD16_S_I32_A32;
1370 }
1371}
1372
1373static unsigned getZExtLoadOpcode(unsigned LoadSize, bool I64Result, bool A64) {
1374 if (I64Result) {
1375 switch (LoadSize) {
1376 default:
1377 return WebAssembly::INSTRUCTION_LIST_END;
1378 case 8:
1379 return A64 ? WebAssembly::LOAD8_U_I64_A64 : WebAssembly::LOAD8_U_I64_A32;
1380 case 16:
1381 return A64 ? WebAssembly::LOAD16_U_I64_A64
1382 : WebAssembly::LOAD16_U_I64_A32;
1383 case 32:
1384 return A64 ? WebAssembly::LOAD32_U_I64_A64
1385 : WebAssembly::LOAD32_U_I64_A32;
1386 }
1387 }
1388
1389 switch (LoadSize) {
1390 default:
1391 return WebAssembly::INSTRUCTION_LIST_END;
1392 case 8:
1393 return A64 ? WebAssembly::LOAD8_U_I32_A64 : WebAssembly::LOAD8_U_I32_A32;
1394 case 16:
1395 return A64 ? WebAssembly::LOAD16_U_I32_A64 : WebAssembly::LOAD16_U_I32_A32;
1396 }
1397}
1398
1399static bool isFoldableSExtOpcode(unsigned Opc) {
1400 switch (Opc) {
1401 default:
1402 return false;
1403 case WebAssembly::I32_EXTEND8_S_I32:
1404 case WebAssembly::I32_EXTEND16_S_I32:
1405 case WebAssembly::I64_EXTEND8_S_I64:
1406 case WebAssembly::I64_EXTEND16_S_I64:
1407 case WebAssembly::I64_EXTEND32_S_I64:
1408 case WebAssembly::I64_EXTEND_S_I32:
1409 return true;
1410 }
1411}
1412
1413static bool isI64SExtResult(unsigned Opc) {
1414 switch (Opc) {
1415 default:
1416 llvm_unreachable("unexpected opcode");
1417 case WebAssembly::I32_EXTEND8_S_I32:
1418 case WebAssembly::I32_EXTEND16_S_I32:
1419 return false;
1420 case WebAssembly::I64_EXTEND8_S_I64:
1421 case WebAssembly::I64_EXTEND16_S_I64:
1422 case WebAssembly::I64_EXTEND32_S_I64:
1423 case WebAssembly::I64_EXTEND_S_I32:
1424 return true;
1425 }
1426}
1427
1429 const LoadInst *LI, bool A64) {
1430 unsigned Opc = MI->getOpcode();
1431
1433 unsigned LoadSize = LI->getType()->getPrimitiveSizeInBits();
1434 return getSExtLoadOpcode(LoadSize, isI64SExtResult(Opc), A64);
1435 }
1436
1437 return WebAssembly::INSTRUCTION_LIST_END;
1438}
1439
1440static unsigned getFoldedI64LoadOpcode(Register DestReg, const LoadInst *LI,
1441 MachineRegisterInfo &MRI, bool A64,
1442 MachineInstr *&OuterUserMI,
1443 unsigned NarrowOpc) {
1444 if (!MRI.hasOneNonDBGUse(DestReg))
1445 return NarrowOpc;
1446
1447 MachineInstr *UserMI = &*MRI.use_instr_nodbg_begin(DestReg);
1448 unsigned LoadSize = LI->getType()->getPrimitiveSizeInBits();
1449 switch (UserMI->getOpcode()) {
1450 case WebAssembly::I64_EXTEND_U_I32:
1451 OuterUserMI = UserMI;
1452 return getZExtLoadOpcode(LoadSize, /*I64Result=*/true, A64);
1453 case WebAssembly::I64_EXTEND_S_I32:
1454 OuterUserMI = UserMI;
1455 return getSExtLoadOpcode(LoadSize, /*I64Result=*/true, A64);
1456 default:
1457 return NarrowOpc;
1458 }
1459}
1460
1461/// Matches a sign-extension pattern (shl + shr_s) to fold it into a signed
1462/// load. FastISel assumes that 'sext' from i8 or i16 will first be lowered to a
1463/// 32-bit zero-extending load (i32.load8_u / i32.load16_u) followed by 32-bit
1464/// shifts, even when extending to i64. Therefore, this function only matches
1465/// 32-bit shifts (SHL_I32 / SHR_S_I32) and specifically checks if both shift
1466/// amounts are identical, compile-time constants that match the exact extension
1467/// size (32 - LoadBitWidth).
1468static unsigned matchFoldableShift(MachineInstr *MI, const LoadInst *LI,
1469 MachineRegisterInfo &MRI, bool A64,
1470 MachineInstr *&UserMI,
1471 MachineInstr *&OuterUserMI) {
1472 unsigned Opc = MI->getOpcode();
1473 unsigned NewOpc = WebAssembly::INSTRUCTION_LIST_END;
1474 if (Opc != WebAssembly::SHL_I32)
1475 return NewOpc;
1476
1477 Register DestReg = MI->getOperand(0).getReg();
1478 if (!MRI.hasOneNonDBGUse(DestReg))
1479 return NewOpc;
1480
1481 UserMI = &*MRI.use_instr_nodbg_begin(DestReg);
1482 unsigned UserOpc = UserMI->getOpcode();
1483 if (UserOpc != WebAssembly::SHR_S_I32)
1484 return NewOpc;
1485
1486 Type *LoadTy = LI->getType();
1487 if (!LoadTy->isIntegerTy(8) && !LoadTy->isIntegerTy(16))
1488 return NewOpc;
1489
1490 int64_t ExpectedShiftAmt = 32 - LoadTy->getIntegerBitWidth();
1491 Register ShlAmtReg = MI->getOperand(2).getReg();
1492 Register ShrAmtReg = UserMI->getOperand(2).getReg();
1493 MachineInstr *ShlAmtDef = MRI.getUniqueVRegDef(ShlAmtReg);
1494 MachineInstr *ShrAmtDef = MRI.getUniqueVRegDef(ShrAmtReg);
1495 auto IsExpectedConst = [ExpectedShiftAmt](MachineInstr *MI) {
1496 return MI && MI->getOpcode() == WebAssembly::CONST_I32 &&
1497 MI->getOperand(1).getImm() == ExpectedShiftAmt;
1498 };
1499 if (!IsExpectedConst(ShlAmtDef) || !IsExpectedConst(ShrAmtDef))
1500 return NewOpc;
1501
1502 unsigned LoadSize = LoadTy->getIntegerBitWidth();
1503 unsigned NarrowOpc = getSExtLoadOpcode(LoadSize, /*I64Result=*/false, A64);
1504 if (NarrowOpc == WebAssembly::INSTRUCTION_LIST_END)
1505 return WebAssembly::INSTRUCTION_LIST_END;
1506
1507 return getFoldedI64LoadOpcode(UserMI->getOperand(0).getReg(), LI, MRI, A64,
1508 OuterUserMI, NarrowOpc);
1509}
1510
1512 const LoadInst *LI,
1514 bool A64,
1515 MachineInstr *&UserMI) {
1516 if (MI->getOpcode() != WebAssembly::I64_EXTEND_U_I32)
1517 return WebAssembly::INSTRUCTION_LIST_END;
1518
1519 unsigned LoadSize = LI->getType()->getPrimitiveSizeInBits();
1520 Register DestReg = MI->getOperand(0).getReg();
1521 if (!MRI.hasOneNonDBGUse(DestReg))
1522 return WebAssembly::INSTRUCTION_LIST_END;
1523
1524 UserMI = &*MRI.use_instr_nodbg_begin(DestReg);
1525 switch (UserMI->getOpcode()) {
1526 default:
1527 return WebAssembly::INSTRUCTION_LIST_END;
1528 case WebAssembly::I64_EXTEND8_S_I64:
1529 if (LoadSize != 8)
1530 return WebAssembly::INSTRUCTION_LIST_END;
1531 return getSExtLoadOpcode(LoadSize, true, A64);
1532 case WebAssembly::I64_EXTEND16_S_I64:
1533 if (LoadSize != 16)
1534 return WebAssembly::INSTRUCTION_LIST_END;
1535 return getSExtLoadOpcode(LoadSize, true, A64);
1536 }
1537}
1538
1540 MachineRegisterInfo &MRI, bool A64,
1541 MachineInstr *&OuterUserMI) {
1542 if (MI->getOpcode() != WebAssembly::COPY)
1543 return WebAssembly::INSTRUCTION_LIST_END;
1544
1545 unsigned LoadSize = LI->getType()->getPrimitiveSizeInBits();
1546 if (LoadSize != 32)
1547 return WebAssembly::INSTRUCTION_LIST_END;
1548
1549 Register CopyDst = MI->getOperand(0).getReg();
1550 if (!MRI.hasOneNonDBGUse(CopyDst))
1551 return WebAssembly::INSTRUCTION_LIST_END;
1552
1553 OuterUserMI = &*MRI.use_instr_nodbg_begin(CopyDst);
1554 switch (OuterUserMI->getOpcode()) {
1555 default:
1556 return WebAssembly::INSTRUCTION_LIST_END;
1557 case WebAssembly::I64_EXTEND_U_I32:
1558 return getZExtLoadOpcode(LoadSize, true, A64);
1559 case WebAssembly::I64_EXTEND_S_I32:
1560 return getSExtLoadOpcode(LoadSize, true, A64);
1561 }
1562}
1563
1564static unsigned matchFoldableAnd(MachineInstr *MI, const LoadInst *LI,
1565 MachineRegisterInfo &MRI, bool A64,
1566 MachineInstr *&OuterUserMI) {
1567 if (MI->getOpcode() != WebAssembly::AND_I32 &&
1568 MI->getOpcode() != WebAssembly::AND_I64)
1569 return WebAssembly::INSTRUCTION_LIST_END;
1570
1571 uint64_t Mask = 0;
1572 bool IsConstant = false;
1573 for (unsigned I = 1; I <= 2; ++I) {
1574 Register Reg = MI->getOperand(I).getReg();
1575 MachineInstr *DefMI = MRI.getUniqueVRegDef(Reg);
1576 if (DefMI && (DefMI->getOpcode() == WebAssembly::CONST_I32 ||
1577 DefMI->getOpcode() == WebAssembly::CONST_I64)) {
1578 Mask = DefMI->getOperand(1).getImm();
1579 IsConstant = true;
1580 break;
1581 }
1582 }
1583
1584 if (!IsConstant)
1585 return WebAssembly::INSTRUCTION_LIST_END;
1586
1587 unsigned LoadSize = LI->getType()->getPrimitiveSizeInBits();
1588 if (Mask != llvm::maskTrailingOnes<uint64_t>(LoadSize))
1589 return WebAssembly::INSTRUCTION_LIST_END;
1590
1591 if (MI->getOpcode() == WebAssembly::AND_I64)
1592 return getZExtLoadOpcode(LoadSize, /*I64Result=*/true, A64);
1593
1594 unsigned NarrowOpc = getZExtLoadOpcode(LoadSize, /*I64Result=*/false, A64);
1595 if (NarrowOpc == WebAssembly::INSTRUCTION_LIST_END)
1596 return WebAssembly::INSTRUCTION_LIST_END;
1597
1598 return getFoldedI64LoadOpcode(MI->getOperand(0).getReg(), LI, MRI, A64,
1599 OuterUserMI, NarrowOpc);
1600}
1601
1602bool WebAssemblyFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
1603 const LoadInst *LI) {
1604 bool A64 = Subtarget->hasAddr64();
1605 MachineRegisterInfo &MRI = FuncInfo.MF->getRegInfo();
1606 Register ResultReg;
1607 MachineInstr *UserMI = nullptr;
1608 MachineInstr *OuterUserMI = nullptr;
1609 unsigned NewOpc = WebAssembly::INSTRUCTION_LIST_END;
1610 if ((NewOpc = matchFoldableSExtFromPromotedI32(MI, LI, MRI, A64, UserMI)) !=
1611 WebAssembly::INSTRUCTION_LIST_END) {
1612 ResultReg = UserMI->getOperand(0).getReg();
1613 } else if ((NewOpc =
1614 matchFoldableCopyToI64Ext(MI, LI, MRI, A64, OuterUserMI)) !=
1615 WebAssembly::INSTRUCTION_LIST_END) {
1616 ResultReg = OuterUserMI->getOperand(0).getReg();
1617 } else if ((NewOpc = matchFoldableAnd(MI, LI, MRI, A64, OuterUserMI)) !=
1618 WebAssembly::INSTRUCTION_LIST_END) {
1619 ResultReg = OuterUserMI ? OuterUserMI->getOperand(0).getReg()
1620 : MI->getOperand(0).getReg();
1621 } else if ((NewOpc = getFoldedLoadOpcode(MI, MRI, LI, A64)) !=
1622 WebAssembly::INSTRUCTION_LIST_END) {
1623 ResultReg = MI->getOperand(0).getReg();
1624 } else if ((NewOpc =
1625 matchFoldableShift(MI, LI, MRI, A64, UserMI, OuterUserMI)) !=
1626 WebAssembly::INSTRUCTION_LIST_END) {
1627 ResultReg = OuterUserMI ? OuterUserMI->getOperand(0).getReg()
1628 : UserMI->getOperand(0).getReg();
1629 } else {
1630 return false;
1631 }
1632
1633 if (!emitLoad(ResultReg, NewOpc, LI))
1634 return false;
1635
1636 if (OuterUserMI) {
1637 MachineBasicBlock::iterator OuterIter(OuterUserMI);
1638 removeDeadCode(OuterIter, std::next(OuterIter));
1639 }
1640
1641 if (UserMI) {
1642 MachineBasicBlock::iterator UserIter(UserMI);
1643 removeDeadCode(UserIter, std::next(UserIter));
1644 }
1645
1647 removeDeadCode(Iter, std::next(Iter));
1648 return true;
1649}
1650
1651bool WebAssemblyFastISel::selectLoad(const Instruction *I) {
1652 const auto *Load = cast<LoadInst>(I);
1653 if (Load->isAtomic())
1654 return false;
1655 if (!WebAssembly::isDefaultAddressSpace(Load->getPointerAddressSpace()))
1656 return false;
1657 if (!Subtarget->hasSIMD128() && Load->getType()->isVectorTy())
1658 return false;
1659
1660 // TODO: Fold a following sign-/zero-extend into the load instruction.
1661
1662 unsigned Opc;
1663 const TargetRegisterClass *RC;
1664 bool A64 = Subtarget->hasAddr64();
1665 switch (getSimpleType(Load->getType())) {
1666 case MVT::i1:
1667 case MVT::i8:
1668 Opc = A64 ? WebAssembly::LOAD8_U_I32_A64 : WebAssembly::LOAD8_U_I32_A32;
1669 RC = &WebAssembly::I32RegClass;
1670 break;
1671 case MVT::i16:
1672 Opc = A64 ? WebAssembly::LOAD16_U_I32_A64 : WebAssembly::LOAD16_U_I32_A32;
1673 RC = &WebAssembly::I32RegClass;
1674 break;
1675 case MVT::i32:
1676 Opc = A64 ? WebAssembly::LOAD_I32_A64 : WebAssembly::LOAD_I32_A32;
1677 RC = &WebAssembly::I32RegClass;
1678 break;
1679 case MVT::i64:
1680 Opc = A64 ? WebAssembly::LOAD_I64_A64 : WebAssembly::LOAD_I64_A32;
1681 RC = &WebAssembly::I64RegClass;
1682 break;
1683 case MVT::f32:
1684 Opc = A64 ? WebAssembly::LOAD_F32_A64 : WebAssembly::LOAD_F32_A32;
1685 RC = &WebAssembly::F32RegClass;
1686 break;
1687 case MVT::f64:
1688 Opc = A64 ? WebAssembly::LOAD_F64_A64 : WebAssembly::LOAD_F64_A32;
1689 RC = &WebAssembly::F64RegClass;
1690 break;
1691 default:
1692 return false;
1693 }
1694
1695 Register ResultReg = createResultReg(RC);
1696 if (!emitLoad(ResultReg, Opc, Load))
1697 return false;
1698
1699 updateValueMap(Load, ResultReg);
1700 return true;
1701}
1702
1703bool WebAssemblyFastISel::selectStore(const Instruction *I) {
1704 const auto *Store = cast<StoreInst>(I);
1705 if (Store->isAtomic())
1706 return false;
1707 if (!WebAssembly::isDefaultAddressSpace(Store->getPointerAddressSpace()))
1708 return false;
1709 if (!Subtarget->hasSIMD128() &&
1710 Store->getValueOperand()->getType()->isVectorTy())
1711 return false;
1712
1713 Address Addr;
1714 if (!computeAddress(Store->getPointerOperand(), Addr))
1715 return false;
1716
1717 unsigned Opc;
1718 bool VTIsi1 = false;
1719 bool A64 = Subtarget->hasAddr64();
1720 switch (getSimpleType(Store->getValueOperand()->getType())) {
1721 case MVT::i1:
1722 VTIsi1 = true;
1723 [[fallthrough]];
1724 case MVT::i8:
1725 Opc = A64 ? WebAssembly::STORE8_I32_A64 : WebAssembly::STORE8_I32_A32;
1726 break;
1727 case MVT::i16:
1728 Opc = A64 ? WebAssembly::STORE16_I32_A64 : WebAssembly::STORE16_I32_A32;
1729 break;
1730 case MVT::i32:
1731 Opc = A64 ? WebAssembly::STORE_I32_A64 : WebAssembly::STORE_I32_A32;
1732 break;
1733 case MVT::i64:
1734 Opc = A64 ? WebAssembly::STORE_I64_A64 : WebAssembly::STORE_I64_A32;
1735 break;
1736 case MVT::f32:
1737 Opc = A64 ? WebAssembly::STORE_F32_A64 : WebAssembly::STORE_F32_A32;
1738 break;
1739 case MVT::f64:
1740 Opc = A64 ? WebAssembly::STORE_F64_A64 : WebAssembly::STORE_F64_A32;
1741 break;
1742 default:
1743 return false;
1744 }
1745
1746 materializeLoadStoreOperands(Addr);
1747
1748 Register ValueReg = getRegForValue(Store->getValueOperand());
1749 if (ValueReg == 0)
1750 return false;
1751 if (VTIsi1)
1752 ValueReg = maskI1Value(ValueReg, Store->getValueOperand());
1753
1754 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc));
1755
1756 addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(Store));
1757
1758 MIB.addReg(ValueReg);
1759 return true;
1760}
1761
1762bool WebAssemblyFastISel::selectCondBr(const Instruction *I) {
1763 const auto *Br = cast<CondBrInst>(I);
1764
1765 MachineBasicBlock *TBB = FuncInfo.getMBB(Br->getSuccessor(0));
1766 MachineBasicBlock *FBB = FuncInfo.getMBB(Br->getSuccessor(1));
1767
1768 bool Not;
1769 unsigned CondReg = getRegForI1Value(Br->getCondition(), Br->getParent(), Not);
1770 if (CondReg == 0)
1771 return false;
1772
1773 unsigned Opc = WebAssembly::BR_IF;
1774 if (Not)
1775 Opc = WebAssembly::BR_UNLESS;
1776
1777 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))
1778 .addMBB(TBB)
1779 .addReg(CondReg);
1780
1781 finishCondBranch(Br->getParent(), TBB, FBB);
1782 return true;
1783}
1784
1785bool WebAssemblyFastISel::selectRet(const Instruction *I) {
1786 if (!FuncInfo.CanLowerReturn)
1787 return false;
1788
1789 const auto *Ret = cast<ReturnInst>(I);
1790
1791 if (Ret->getNumOperands() == 0) {
1792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1793 TII.get(WebAssembly::RETURN));
1794 return true;
1795 }
1796
1797 // TODO: support multiple return in FastISel
1798 if (Ret->getNumOperands() > 1)
1799 return false;
1800
1801 Value *RV = Ret->getOperand(0);
1802 if (!Subtarget->hasSIMD128() && RV->getType()->isVectorTy())
1803 return false;
1804
1805 switch (getSimpleType(RV->getType())) {
1806 case MVT::i1:
1807 case MVT::i8:
1808 case MVT::i16:
1809 case MVT::i32:
1810 case MVT::i64:
1811 case MVT::f32:
1812 case MVT::f64:
1813 case MVT::v16i8:
1814 case MVT::v8i16:
1815 case MVT::v4i32:
1816 case MVT::v2i64:
1817 case MVT::v4f32:
1818 case MVT::v2f64:
1819 case MVT::funcref:
1820 case MVT::externref:
1821 case MVT::exnref:
1822 break;
1823 default:
1824 return false;
1825 }
1826
1827 unsigned Reg;
1828 if (FuncInfo.Fn->getAttributes().hasRetAttr(Attribute::SExt))
1829 Reg = getRegForSignedValue(RV);
1830 else if (FuncInfo.Fn->getAttributes().hasRetAttr(Attribute::ZExt))
1831 Reg = getRegForUnsignedValue(RV);
1832 else
1833 Reg = getRegForValue(RV);
1834
1835 if (Reg == 0)
1836 return false;
1837
1838 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(WebAssembly::RETURN))
1839 .addReg(Reg);
1840 return true;
1841}
1842
1843bool WebAssemblyFastISel::selectUnreachable(const Instruction *I) {
1844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1845 TII.get(WebAssembly::UNREACHABLE));
1846 return true;
1847}
1848
1849bool WebAssemblyFastISel::fastSelectInstruction(const Instruction *I) {
1850 switch (I->getOpcode()) {
1851 case Instruction::Call:
1852 if (selectCall(I))
1853 return true;
1854 break;
1855 case Instruction::Select:
1856 return selectSelect(I);
1857 case Instruction::Trunc:
1858 return selectTrunc(I);
1859 case Instruction::ZExt:
1860 return selectZExt(I);
1861 case Instruction::SExt:
1862 return selectSExt(I);
1863 case Instruction::ICmp:
1864 return selectICmp(I);
1865 case Instruction::FCmp:
1866 return selectFCmp(I);
1867 case Instruction::BitCast:
1868 return selectBitCast(I);
1869 case Instruction::Load:
1870 return selectLoad(I);
1871 case Instruction::Store:
1872 return selectStore(I);
1873 case Instruction::CondBr:
1874 return selectCondBr(I);
1875 case Instruction::Ret:
1876 return selectRet(I);
1877 case Instruction::Unreachable:
1878 return selectUnreachable(I);
1879 default:
1880 break;
1881 }
1882
1883 // Fall back to target-independent instruction selection.
1884 return selectOperator(I, I->getOpcode());
1885}
1886
1887FastISel *
1889 const TargetLibraryInfo *LibInfo,
1890 const LibcallLoweringInfo *LibcallLowering) {
1891 return new WebAssemblyFastISel(FuncInfo, LibInfo, LibcallLowering);
1892}
MachineInstrBuilder MachineInstrBuilder & DefMI
const AArch64Subtarget & Subtarget
const TargetLowering & TLI
static void emitLoad(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator Pos, const TargetInstrInfo &TII, unsigned Reg1, unsigned Reg2, int Offset, bool IsPostDec)
Emit a load-pair instruction for frame-destroy.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
constexpr LLT F32
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the FastISel class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
MachineRegisterInfo & MRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
static bool isFoldableSExtOpcode(unsigned Opc)
static unsigned getSExtLoadOpcode(unsigned LoadSize, bool I64Result, bool A64)
static bool isI64SExtResult(unsigned Opc)
static unsigned matchFoldableCopyToI64Ext(MachineInstr *MI, const LoadInst *LI, MachineRegisterInfo &MRI, bool A64, MachineInstr *&OuterUserMI)
static unsigned matchFoldableSExtFromPromotedI32(MachineInstr *MI, const LoadInst *LI, MachineRegisterInfo &MRI, bool A64, MachineInstr *&UserMI)
static unsigned getZExtLoadOpcode(unsigned LoadSize, bool I64Result, bool A64)
static unsigned matchFoldableShift(MachineInstr *MI, const LoadInst *LI, MachineRegisterInfo &MRI, bool A64, MachineInstr *&UserMI, MachineInstr *&OuterUserMI)
Matches a sign-extension pattern (shl + shr_s) to fold it into a signed load.
static unsigned getFoldedI64LoadOpcode(Register DestReg, const LoadInst *LI, MachineRegisterInfo &MRI, bool A64, MachineInstr *&OuterUserMI, unsigned NarrowOpc)
static unsigned getFoldedLoadOpcode(MachineInstr *MI, MachineRegisterInfo &MRI, const LoadInst *LI, bool A64)
static unsigned matchFoldableAnd(MachineInstr *MI, const LoadInst *LI, MachineRegisterInfo &MRI, bool A64, MachineInstr *&OuterUserMI)
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific utility functions.
Value * RHS
Value * LHS
an instruction to allocate memory on the stack
LLVM Basic Block Representation.
Definition BasicBlock.h:62
bool isInlineAsm() const
Check if this call is an inline asm statement.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
CallingConv::ID getCallingConv() const
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
bool isMustTailCall() const
This is an important base class in LLVM.
Definition Constant.h:44
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:67
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
bool isVarArg() const
Tracks which library functions to use for a particular subtarget or function.
An instruction for reading from memory.
@ INVALID_SIMPLE_VALUE_TYPE
SimpleValueType SimpleTy
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
void addLiveIn(MCRegister Reg, Register vreg=Register())
addLiveIn - Add the specified register as a live-in.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:777
Provides information about what library functions are available for the current target.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isPositionIndependent() const
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:274
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:271
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Not(const Pred &P) -> Not< Pred >
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo, const LibcallLoweringInfo *libcallLowering)
@ User
could "use" a pointer
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
gep_type_iterator gep_type_end(const User *GEP)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
generic_gep_type_iterator<> gep_type_iterator
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
gep_type_iterator gep_type_begin(const User *GEP)
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:78
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
Extended Value Type.
Definition ValueTypes.h:35
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:160
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354