LLVM 22.0.0git
IRTranslator.cpp
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator ---*- C++ -*-==//
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/// \file
9/// This file implements the IRTranslator class.
10//===----------------------------------------------------------------------===//
11
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/ScopeExit.h"
20#include "llvm/Analysis/Loads.h"
50#include "llvm/IR/BasicBlock.h"
51#include "llvm/IR/CFG.h"
52#include "llvm/IR/Constant.h"
53#include "llvm/IR/Constants.h"
54#include "llvm/IR/DataLayout.h"
57#include "llvm/IR/Function.h"
59#include "llvm/IR/InlineAsm.h"
60#include "llvm/IR/InstrTypes.h"
63#include "llvm/IR/Intrinsics.h"
64#include "llvm/IR/IntrinsicsAMDGPU.h"
65#include "llvm/IR/LLVMContext.h"
66#include "llvm/IR/Metadata.h"
68#include "llvm/IR/Statepoint.h"
69#include "llvm/IR/Type.h"
70#include "llvm/IR/User.h"
71#include "llvm/IR/Value.h"
73#include "llvm/MC/MCContext.h"
74#include "llvm/Pass.h"
77#include "llvm/Support/Debug.h"
84#include <algorithm>
85#include <cassert>
86#include <cstdint>
87#include <iterator>
88#include <optional>
89#include <string>
90#include <utility>
91#include <vector>
92
93#define DEBUG_TYPE "irtranslator"
94
95using namespace llvm;
96
97static cl::opt<bool>
98 EnableCSEInIRTranslator("enable-cse-in-irtranslator",
99 cl::desc("Should enable CSE in irtranslator"),
100 cl::Optional, cl::init(false));
101char IRTranslator::ID = 0;
102
103INITIALIZE_PASS_BEGIN(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
104 false, false)
110INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
112
117 MF.getProperties().setFailedISel();
118
119 // Print the function name explicitly if we don't have a debug location (which
120 // makes the diagnostic less useful) or if we're going to emit a raw error.
121 if (!R.getLocation().isValid() || TPC.isGlobalISelAbortEnabled())
122 R << (" (in function: " + MF.getName() + ")").str();
123
124 if (TPC.isGlobalISelAbortEnabled())
125 report_fatal_error(Twine(R.getMsg()));
126 else
127 ORE.emit(R);
128}
129
131 : MachineFunctionPass(ID), OptLevel(optlevel) {}
132
133#ifndef NDEBUG
134namespace {
135/// Verify that every instruction created has the same DILocation as the
136/// instruction being translated.
137class DILocationVerifier : public GISelChangeObserver {
138 const Instruction *CurrInst = nullptr;
139
140public:
141 DILocationVerifier() = default;
142 ~DILocationVerifier() override = default;
143
144 const Instruction *getCurrentInst() const { return CurrInst; }
145 void setCurrentInst(const Instruction *Inst) { CurrInst = Inst; }
146
147 void erasingInstr(MachineInstr &MI) override {}
148 void changingInstr(MachineInstr &MI) override {}
149 void changedInstr(MachineInstr &MI) override {}
150
151 void createdInstr(MachineInstr &MI) override {
152 assert(getCurrentInst() && "Inserted instruction without a current MI");
153
154 // Only print the check message if we're actually checking it.
155#ifndef NDEBUG
156 LLVM_DEBUG(dbgs() << "Checking DILocation from " << *CurrInst
157 << " was copied to " << MI);
158#endif
159 // We allow insts in the entry block to have no debug loc because
160 // they could have originated from constants, and we don't want a jumpy
161 // debug experience.
162 assert((CurrInst->getDebugLoc() == MI.getDebugLoc() ||
163 (MI.getParent()->isEntryBlock() && !MI.getDebugLoc()) ||
164 (MI.isDebugInstr())) &&
165 "Line info was not transferred to all instructions");
166 }
167};
168} // namespace
169#endif // ifndef NDEBUG
170
171
186
187IRTranslator::ValueToVRegInfo::VRegListT &
188IRTranslator::allocateVRegs(const Value &Val) {
189 auto VRegsIt = VMap.findVRegs(Val);
190 if (VRegsIt != VMap.vregs_end())
191 return *VRegsIt->second;
192 auto *Regs = VMap.getVRegs(Val);
193 auto *Offsets = VMap.getOffsets(Val);
194 SmallVector<LLT, 4> SplitTys;
195 computeValueLLTs(*DL, *Val.getType(), SplitTys,
196 Offsets->empty() ? Offsets : nullptr);
197 for (unsigned i = 0; i < SplitTys.size(); ++i)
198 Regs->push_back(0);
199 return *Regs;
200}
201
202ArrayRef<Register> IRTranslator::getOrCreateVRegs(const Value &Val) {
203 auto VRegsIt = VMap.findVRegs(Val);
204 if (VRegsIt != VMap.vregs_end())
205 return *VRegsIt->second;
206
207 if (Val.getType()->isVoidTy())
208 return *VMap.getVRegs(Val);
209
210 // Create entry for this type.
211 auto *VRegs = VMap.getVRegs(Val);
212 auto *Offsets = VMap.getOffsets(Val);
213
214 if (!Val.getType()->isTokenTy())
215 assert(Val.getType()->isSized() &&
216 "Don't know how to create an empty vreg");
217
218 SmallVector<LLT, 4> SplitTys;
219 computeValueLLTs(*DL, *Val.getType(), SplitTys,
220 Offsets->empty() ? Offsets : nullptr);
221
222 if (!isa<Constant>(Val)) {
223 for (auto Ty : SplitTys)
224 VRegs->push_back(MRI->createGenericVirtualRegister(Ty));
225 return *VRegs;
226 }
227
228 if (Val.getType()->isAggregateType()) {
229 // UndefValue, ConstantAggregateZero
230 auto &C = cast<Constant>(Val);
231 unsigned Idx = 0;
232 while (auto Elt = C.getAggregateElement(Idx++)) {
233 auto EltRegs = getOrCreateVRegs(*Elt);
234 llvm::append_range(*VRegs, EltRegs);
235 }
236 } else {
237 assert(SplitTys.size() == 1 && "unexpectedly split LLT");
238 VRegs->push_back(MRI->createGenericVirtualRegister(SplitTys[0]));
239 bool Success = translate(cast<Constant>(Val), VRegs->front());
240 if (!Success) {
241 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
242 MF->getFunction().getSubprogram(),
243 &MF->getFunction().getEntryBlock());
244 R << "unable to translate constant: " << ore::NV("Type", Val.getType());
245 reportTranslationError(*MF, *TPC, *ORE, R);
246 return *VRegs;
247 }
248 }
249
250 return *VRegs;
251}
252
253int IRTranslator::getOrCreateFrameIndex(const AllocaInst &AI) {
254 auto [MapEntry, Inserted] = FrameIndices.try_emplace(&AI);
255 if (!Inserted)
256 return MapEntry->second;
257
258 uint64_t ElementSize = DL->getTypeAllocSize(AI.getAllocatedType());
259 uint64_t Size =
260 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
261
262 // Always allocate at least one byte.
263 Size = std::max<uint64_t>(Size, 1u);
264
265 int &FI = MapEntry->second;
266 FI = MF->getFrameInfo().CreateStackObject(Size, AI.getAlign(), false, &AI);
267 return FI;
268}
269
270Align IRTranslator::getMemOpAlign(const Instruction &I) {
271 if (const StoreInst *SI = dyn_cast<StoreInst>(&I))
272 return SI->getAlign();
273 if (const LoadInst *LI = dyn_cast<LoadInst>(&I))
274 return LI->getAlign();
275 if (const AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(&I))
276 return AI->getAlign();
277 if (const AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(&I))
278 return AI->getAlign();
279
280 OptimizationRemarkMissed R("gisel-irtranslator", "", &I);
281 R << "unable to translate memop: " << ore::NV("Opcode", &I);
282 reportTranslationError(*MF, *TPC, *ORE, R);
283 return Align(1);
284}
285
286MachineBasicBlock &IRTranslator::getMBB(const BasicBlock &BB) {
287 MachineBasicBlock *MBB = FuncInfo.getMBB(&BB);
288 assert(MBB && "BasicBlock was not encountered before");
289 return *MBB;
290}
291
292void IRTranslator::addMachineCFGPred(CFGEdge Edge, MachineBasicBlock *NewPred) {
293 assert(NewPred && "new predecessor must be a real MachineBasicBlock");
294 MachinePreds[Edge].push_back(NewPred);
295}
296
298 return MF->getTarget().getTargetTriple().isSPIRV();
299}
300
301static bool containsBF16Type(const User &U) {
302 // BF16 cannot currently be represented by LLT, to avoid miscompiles we
303 // prevent any instructions using them. FIXME: This can be removed once LLT
304 // supports bfloat.
305 return U.getType()->getScalarType()->isBFloatTy() ||
306 any_of(U.operands(), [](Value *V) {
307 return V->getType()->getScalarType()->isBFloatTy();
308 });
309}
310
311bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U,
312 MachineIRBuilder &MIRBuilder) {
314 return false;
315
316 // Get or create a virtual register for each value.
317 // Unless the value is a Constant => loadimm cst?
318 // or inline constant each time?
319 // Creation of a virtual register needs to have a size.
320 Register Op0 = getOrCreateVReg(*U.getOperand(0));
321 Register Op1 = getOrCreateVReg(*U.getOperand(1));
322 Register Res = getOrCreateVReg(U);
323 uint32_t Flags = 0;
324 if (isa<Instruction>(U)) {
325 const Instruction &I = cast<Instruction>(U);
327 }
328
329 MIRBuilder.buildInstr(Opcode, {Res}, {Op0, Op1}, Flags);
330 return true;
331}
332
333bool IRTranslator::translateUnaryOp(unsigned Opcode, const User &U,
334 MachineIRBuilder &MIRBuilder) {
336 return false;
337
338 Register Op0 = getOrCreateVReg(*U.getOperand(0));
339 Register Res = getOrCreateVReg(U);
340 uint32_t Flags = 0;
341 if (isa<Instruction>(U)) {
342 const Instruction &I = cast<Instruction>(U);
344 }
345 MIRBuilder.buildInstr(Opcode, {Res}, {Op0}, Flags);
346 return true;
347}
348
349bool IRTranslator::translateFNeg(const User &U, MachineIRBuilder &MIRBuilder) {
350 return translateUnaryOp(TargetOpcode::G_FNEG, U, MIRBuilder);
351}
352
353bool IRTranslator::translateCompare(const User &U,
354 MachineIRBuilder &MIRBuilder) {
356 return false;
357
358 auto *CI = cast<CmpInst>(&U);
359 Register Op0 = getOrCreateVReg(*U.getOperand(0));
360 Register Op1 = getOrCreateVReg(*U.getOperand(1));
361 Register Res = getOrCreateVReg(U);
362 CmpInst::Predicate Pred = CI->getPredicate();
364 if (CmpInst::isIntPredicate(Pred))
365 MIRBuilder.buildICmp(Pred, Res, Op0, Op1, Flags);
366 else if (Pred == CmpInst::FCMP_FALSE)
367 MIRBuilder.buildCopy(
368 Res, getOrCreateVReg(*Constant::getNullValue(U.getType())));
369 else if (Pred == CmpInst::FCMP_TRUE)
370 MIRBuilder.buildCopy(
371 Res, getOrCreateVReg(*Constant::getAllOnesValue(U.getType())));
372 else
373 MIRBuilder.buildFCmp(Pred, Res, Op0, Op1, Flags);
374
375 return true;
376}
377
378bool IRTranslator::translateRet(const User &U, MachineIRBuilder &MIRBuilder) {
379 const ReturnInst &RI = cast<ReturnInst>(U);
380 const Value *Ret = RI.getReturnValue();
381 if (Ret && DL->getTypeStoreSize(Ret->getType()).isZero())
382 Ret = nullptr;
383
384 ArrayRef<Register> VRegs;
385 if (Ret)
386 VRegs = getOrCreateVRegs(*Ret);
387
388 Register SwiftErrorVReg = 0;
389 if (CLI->supportSwiftError() && SwiftError.getFunctionArg()) {
390 SwiftErrorVReg = SwiftError.getOrCreateVRegUseAt(
391 &RI, &MIRBuilder.getMBB(), SwiftError.getFunctionArg());
392 }
393
394 // The target may mess up with the insertion point, but
395 // this is not important as a return is the last instruction
396 // of the block anyway.
397 return CLI->lowerReturn(MIRBuilder, Ret, VRegs, FuncInfo, SwiftErrorVReg);
398}
399
400void IRTranslator::emitBranchForMergedCondition(
402 MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB,
403 BranchProbability TProb, BranchProbability FProb, bool InvertCond) {
404 // If the leaf of the tree is a comparison, merge the condition into
405 // the caseblock.
406 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
407 CmpInst::Predicate Condition;
408 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
409 Condition = InvertCond ? IC->getInversePredicate() : IC->getPredicate();
410 } else {
411 const FCmpInst *FC = cast<FCmpInst>(Cond);
412 Condition = InvertCond ? FC->getInversePredicate() : FC->getPredicate();
413 }
414
415 SwitchCG::CaseBlock CB(Condition, false, BOp->getOperand(0),
416 BOp->getOperand(1), nullptr, TBB, FBB, CurBB,
417 CurBuilder->getDebugLoc(), TProb, FProb);
418 SL->SwitchCases.push_back(CB);
419 return;
420 }
421
422 // Create a CaseBlock record representing this branch.
424 SwitchCG::CaseBlock CB(
425 Pred, false, Cond, ConstantInt::getTrue(MF->getFunction().getContext()),
426 nullptr, TBB, FBB, CurBB, CurBuilder->getDebugLoc(), TProb, FProb);
427 SL->SwitchCases.push_back(CB);
428}
429
430static bool isValInBlock(const Value *V, const BasicBlock *BB) {
431 if (const Instruction *I = dyn_cast<Instruction>(V))
432 return I->getParent() == BB;
433 return true;
434}
435
436void IRTranslator::findMergedConditions(
438 MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB,
440 BranchProbability FProb, bool InvertCond) {
441 using namespace PatternMatch;
442 assert((Opc == Instruction::And || Opc == Instruction::Or) &&
443 "Expected Opc to be AND/OR");
444 // Skip over not part of the tree and remember to invert op and operands at
445 // next level.
446 Value *NotCond;
447 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
448 isValInBlock(NotCond, CurBB->getBasicBlock())) {
449 findMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
450 !InvertCond);
451 return;
452 }
453
455 const Value *BOpOp0, *BOpOp1;
456 // Compute the effective opcode for Cond, taking into account whether it needs
457 // to be inverted, e.g.
458 // and (not (or A, B)), C
459 // gets lowered as
460 // and (and (not A, not B), C)
462 if (BOp) {
463 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
464 ? Instruction::And
465 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
466 ? Instruction::Or
468 if (InvertCond) {
469 if (BOpc == Instruction::And)
470 BOpc = Instruction::Or;
471 else if (BOpc == Instruction::Or)
472 BOpc = Instruction::And;
473 }
474 }
475
476 // If this node is not part of the or/and tree, emit it as a branch.
477 // Note that all nodes in the tree should have same opcode.
478 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
479 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
480 !isValInBlock(BOpOp0, CurBB->getBasicBlock()) ||
481 !isValInBlock(BOpOp1, CurBB->getBasicBlock())) {
482 emitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB, TProb, FProb,
483 InvertCond);
484 return;
485 }
486
487 // Create TmpBB after CurBB.
488 MachineFunction::iterator BBI(CurBB);
489 MachineBasicBlock *TmpBB =
490 MF->CreateMachineBasicBlock(CurBB->getBasicBlock());
491 CurBB->getParent()->insert(++BBI, TmpBB);
492
493 if (Opc == Instruction::Or) {
494 // Codegen X | Y as:
495 // BB1:
496 // jmp_if_X TBB
497 // jmp TmpBB
498 // TmpBB:
499 // jmp_if_Y TBB
500 // jmp FBB
501 //
502
503 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
504 // The requirement is that
505 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
506 // = TrueProb for original BB.
507 // Assuming the original probabilities are A and B, one choice is to set
508 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
509 // A/(1+B) and 2B/(1+B). This choice assumes that
510 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
511 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
512 // TmpBB, but the math is more complicated.
513
514 auto NewTrueProb = TProb / 2;
515 auto NewFalseProb = TProb / 2 + FProb;
516 // Emit the LHS condition.
517 findMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
518 NewFalseProb, InvertCond);
519
520 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
521 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
523 // Emit the RHS condition into TmpBB.
524 findMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
525 Probs[1], InvertCond);
526 } else {
527 assert(Opc == Instruction::And && "Unknown merge op!");
528 // Codegen X & Y as:
529 // BB1:
530 // jmp_if_X TmpBB
531 // jmp FBB
532 // TmpBB:
533 // jmp_if_Y TBB
534 // jmp FBB
535 //
536 // This requires creation of TmpBB after CurBB.
537
538 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
539 // The requirement is that
540 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
541 // = FalseProb for original BB.
542 // Assuming the original probabilities are A and B, one choice is to set
543 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
544 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
545 // TrueProb for BB1 * FalseProb for TmpBB.
546
547 auto NewTrueProb = TProb + FProb / 2;
548 auto NewFalseProb = FProb / 2;
549 // Emit the LHS condition.
550 findMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
551 NewFalseProb, InvertCond);
552
553 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
554 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
556 // Emit the RHS condition into TmpBB.
557 findMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
558 Probs[1], InvertCond);
559 }
560}
561
562bool IRTranslator::shouldEmitAsBranches(
563 const std::vector<SwitchCG::CaseBlock> &Cases) {
564 // For multiple cases, it's better to emit as branches.
565 if (Cases.size() != 2)
566 return true;
567
568 // If this is two comparisons of the same values or'd or and'd together, they
569 // will get folded into a single comparison, so don't emit two blocks.
570 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
571 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
572 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
573 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
574 return false;
575 }
576
577 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
578 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
579 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
580 Cases[0].PredInfo.Pred == Cases[1].PredInfo.Pred &&
581 isa<Constant>(Cases[0].CmpRHS) &&
582 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
583 if (Cases[0].PredInfo.Pred == CmpInst::ICMP_EQ &&
584 Cases[0].TrueBB == Cases[1].ThisBB)
585 return false;
586 if (Cases[0].PredInfo.Pred == CmpInst::ICMP_NE &&
587 Cases[0].FalseBB == Cases[1].ThisBB)
588 return false;
589 }
590
591 return true;
592}
593
594bool IRTranslator::translateBr(const User &U, MachineIRBuilder &MIRBuilder) {
595 const BranchInst &BrInst = cast<BranchInst>(U);
596 auto &CurMBB = MIRBuilder.getMBB();
597 auto *Succ0MBB = &getMBB(*BrInst.getSuccessor(0));
598
599 if (BrInst.isUnconditional()) {
600 // If the unconditional target is the layout successor, fallthrough.
601 if (OptLevel == CodeGenOptLevel::None ||
602 !CurMBB.isLayoutSuccessor(Succ0MBB))
603 MIRBuilder.buildBr(*Succ0MBB);
604
605 // Link successors.
606 for (const BasicBlock *Succ : successors(&BrInst))
607 CurMBB.addSuccessor(&getMBB(*Succ));
608 return true;
609 }
610
611 // If this condition is one of the special cases we handle, do special stuff
612 // now.
613 const Value *CondVal = BrInst.getCondition();
614 MachineBasicBlock *Succ1MBB = &getMBB(*BrInst.getSuccessor(1));
615
616 // If this is a series of conditions that are or'd or and'd together, emit
617 // this as a sequence of branches instead of setcc's with and/or operations.
618 // As long as jumps are not expensive (exceptions for multi-use logic ops,
619 // unpredictable branches, and vector extracts because those jumps are likely
620 // expensive for any target), this should improve performance.
621 // For example, instead of something like:
622 // cmp A, B
623 // C = seteq
624 // cmp D, E
625 // F = setle
626 // or C, F
627 // jnz foo
628 // Emit:
629 // cmp A, B
630 // je foo
631 // cmp D, E
632 // jle foo
633 using namespace PatternMatch;
634 const Instruction *CondI = dyn_cast<Instruction>(CondVal);
635 if (!TLI->isJumpExpensive() && CondI && CondI->hasOneUse() &&
636 !BrInst.hasMetadata(LLVMContext::MD_unpredictable)) {
638 Value *Vec;
639 const Value *BOp0, *BOp1;
640 if (match(CondI, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
641 Opcode = Instruction::And;
642 else if (match(CondI, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
643 Opcode = Instruction::Or;
644
645 if (Opcode && !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
646 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value())))) {
647 findMergedConditions(CondI, Succ0MBB, Succ1MBB, &CurMBB, &CurMBB, Opcode,
648 getEdgeProbability(&CurMBB, Succ0MBB),
649 getEdgeProbability(&CurMBB, Succ1MBB),
650 /*InvertCond=*/false);
651 assert(SL->SwitchCases[0].ThisBB == &CurMBB && "Unexpected lowering!");
652
653 // Allow some cases to be rejected.
654 if (shouldEmitAsBranches(SL->SwitchCases)) {
655 // Emit the branch for this block.
656 emitSwitchCase(SL->SwitchCases[0], &CurMBB, *CurBuilder);
657 SL->SwitchCases.erase(SL->SwitchCases.begin());
658 return true;
659 }
660
661 // Okay, we decided not to do this, remove any inserted MBB's and clear
662 // SwitchCases.
663 for (unsigned I = 1, E = SL->SwitchCases.size(); I != E; ++I)
664 MF->erase(SL->SwitchCases[I].ThisBB);
665
666 SL->SwitchCases.clear();
667 }
668 }
669
670 // Create a CaseBlock record representing this branch.
671 SwitchCG::CaseBlock CB(CmpInst::ICMP_EQ, false, CondVal,
672 ConstantInt::getTrue(MF->getFunction().getContext()),
673 nullptr, Succ0MBB, Succ1MBB, &CurMBB,
674 CurBuilder->getDebugLoc());
675
676 // Use emitSwitchCase to actually insert the fast branch sequence for this
677 // cond branch.
678 emitSwitchCase(CB, &CurMBB, *CurBuilder);
679 return true;
680}
681
682void IRTranslator::addSuccessorWithProb(MachineBasicBlock *Src,
684 BranchProbability Prob) {
685 if (!FuncInfo.BPI) {
686 Src->addSuccessorWithoutProb(Dst);
687 return;
688 }
689 if (Prob.isUnknown())
690 Prob = getEdgeProbability(Src, Dst);
691 Src->addSuccessor(Dst, Prob);
692}
693
695IRTranslator::getEdgeProbability(const MachineBasicBlock *Src,
696 const MachineBasicBlock *Dst) const {
697 const BasicBlock *SrcBB = Src->getBasicBlock();
698 const BasicBlock *DstBB = Dst->getBasicBlock();
699 if (!FuncInfo.BPI) {
700 // If BPI is not available, set the default probability as 1 / N, where N is
701 // the number of successors.
702 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
703 return BranchProbability(1, SuccSize);
704 }
705 return FuncInfo.BPI->getEdgeProbability(SrcBB, DstBB);
706}
707
708bool IRTranslator::translateSwitch(const User &U, MachineIRBuilder &MIB) {
709 using namespace SwitchCG;
710 // Extract cases from the switch.
711 const SwitchInst &SI = cast<SwitchInst>(U);
712 BranchProbabilityInfo *BPI = FuncInfo.BPI;
713 CaseClusterVector Clusters;
714 Clusters.reserve(SI.getNumCases());
715 for (const auto &I : SI.cases()) {
716 MachineBasicBlock *Succ = &getMBB(*I.getCaseSuccessor());
717 assert(Succ && "Could not find successor mbb in mapping");
718 const ConstantInt *CaseVal = I.getCaseValue();
719 BranchProbability Prob =
720 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
721 : BranchProbability(1, SI.getNumCases() + 1);
722 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
723 }
724
725 MachineBasicBlock *DefaultMBB = &getMBB(*SI.getDefaultDest());
726
727 // Cluster adjacent cases with the same destination. We do this at all
728 // optimization levels because it's cheap to do and will make codegen faster
729 // if there are many clusters.
730 sortAndRangeify(Clusters);
731
732 MachineBasicBlock *SwitchMBB = &getMBB(*SI.getParent());
733
734 // If there is only the default destination, jump there directly.
735 if (Clusters.empty()) {
736 SwitchMBB->addSuccessor(DefaultMBB);
737 if (DefaultMBB != SwitchMBB->getNextNode())
738 MIB.buildBr(*DefaultMBB);
739 return true;
740 }
741
742 SL->findJumpTables(Clusters, &SI, std::nullopt, DefaultMBB, nullptr, nullptr);
743 SL->findBitTestClusters(Clusters, &SI);
744
745 LLVM_DEBUG({
746 dbgs() << "Case clusters: ";
747 for (const CaseCluster &C : Clusters) {
748 if (C.Kind == CC_JumpTable)
749 dbgs() << "JT:";
750 if (C.Kind == CC_BitTests)
751 dbgs() << "BT:";
752
753 C.Low->getValue().print(dbgs(), true);
754 if (C.Low != C.High) {
755 dbgs() << '-';
756 C.High->getValue().print(dbgs(), true);
757 }
758 dbgs() << ' ';
759 }
760 dbgs() << '\n';
761 });
762
763 assert(!Clusters.empty());
764 SwitchWorkList WorkList;
765 CaseClusterIt First = Clusters.begin();
766 CaseClusterIt Last = Clusters.end() - 1;
767 auto DefaultProb = getEdgeProbability(SwitchMBB, DefaultMBB);
768 WorkList.push_back({SwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
769
770 while (!WorkList.empty()) {
771 SwitchWorkListItem W = WorkList.pop_back_val();
772
773 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
774 // For optimized builds, lower large range as a balanced binary tree.
775 if (NumClusters > 3 &&
776 MF->getTarget().getOptLevel() != CodeGenOptLevel::None &&
777 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
778 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB, MIB);
779 continue;
780 }
781
782 if (!lowerSwitchWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB, MIB))
783 return false;
784 }
785 return true;
786}
787
788void IRTranslator::splitWorkItem(SwitchCG::SwitchWorkList &WorkList,
790 Value *Cond, MachineBasicBlock *SwitchMBB,
791 MachineIRBuilder &MIB) {
792 using namespace SwitchCG;
793 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
794 "Clusters not sorted?");
795 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
796
797 auto [LastLeft, FirstRight, LeftProb, RightProb] =
798 SL->computeSplitWorkItemInfo(W);
799
800 // Use the first element on the right as pivot since we will make less-than
801 // comparisons against it.
802 CaseClusterIt PivotCluster = FirstRight;
803 assert(PivotCluster > W.FirstCluster);
804 assert(PivotCluster <= W.LastCluster);
805
806 CaseClusterIt FirstLeft = W.FirstCluster;
807 CaseClusterIt LastRight = W.LastCluster;
808
809 const ConstantInt *Pivot = PivotCluster->Low;
810
811 // New blocks will be inserted immediately after the current one.
813 ++BBI;
814
815 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
816 // we can branch to its destination directly if it's squeezed exactly in
817 // between the known lower bound and Pivot - 1.
818 MachineBasicBlock *LeftMBB;
819 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
820 FirstLeft->Low == W.GE &&
821 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
822 LeftMBB = FirstLeft->MBB;
823 } else {
824 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
825 FuncInfo.MF->insert(BBI, LeftMBB);
826 WorkList.push_back(
827 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
828 }
829
830 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
831 // single cluster, RHS.Low == Pivot, and we can branch to its destination
832 // directly if RHS.High equals the current upper bound.
833 MachineBasicBlock *RightMBB;
834 if (FirstRight == LastRight && FirstRight->Kind == CC_Range && W.LT &&
835 (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
836 RightMBB = FirstRight->MBB;
837 } else {
838 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
839 FuncInfo.MF->insert(BBI, RightMBB);
840 WorkList.push_back(
841 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
842 }
843
844 // Create the CaseBlock record that will be used to lower the branch.
845 CaseBlock CB(ICmpInst::Predicate::ICMP_SLT, false, Cond, Pivot, nullptr,
846 LeftMBB, RightMBB, W.MBB, MIB.getDebugLoc(), LeftProb,
847 RightProb);
848
849 if (W.MBB == SwitchMBB)
850 emitSwitchCase(CB, SwitchMBB, MIB);
851 else
852 SL->SwitchCases.push_back(CB);
853}
854
855void IRTranslator::emitJumpTable(SwitchCG::JumpTable &JT,
857 // Emit the code for the jump table
858 assert(JT.Reg && "Should lower JT Header first!");
859 MachineIRBuilder MIB(*MBB->getParent());
860 MIB.setMBB(*MBB);
861 MIB.setDebugLoc(CurBuilder->getDebugLoc());
862
863 Type *PtrIRTy = PointerType::getUnqual(MF->getFunction().getContext());
864 const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
865
866 auto Table = MIB.buildJumpTable(PtrTy, JT.JTI);
867 MIB.buildBrJT(Table.getReg(0), JT.JTI, JT.Reg);
868}
869
870bool IRTranslator::emitJumpTableHeader(SwitchCG::JumpTable &JT,
872 MachineBasicBlock *HeaderBB) {
873 MachineIRBuilder MIB(*HeaderBB->getParent());
874 MIB.setMBB(*HeaderBB);
875 MIB.setDebugLoc(CurBuilder->getDebugLoc());
876
877 const Value &SValue = *JTH.SValue;
878 // Subtract the lowest switch case value from the value being switched on.
879 const LLT SwitchTy = getLLTForType(*SValue.getType(), *DL);
880 Register SwitchOpReg = getOrCreateVReg(SValue);
881 auto FirstCst = MIB.buildConstant(SwitchTy, JTH.First);
882 auto Sub = MIB.buildSub({SwitchTy}, SwitchOpReg, FirstCst);
883
884 // This value may be smaller or larger than the target's pointer type, and
885 // therefore require extension or truncating.
886 auto *PtrIRTy = PointerType::getUnqual(SValue.getContext());
887 const LLT PtrScalarTy = LLT::scalar(DL->getTypeSizeInBits(PtrIRTy));
888 Sub = MIB.buildZExtOrTrunc(PtrScalarTy, Sub);
889
890 JT.Reg = Sub.getReg(0);
891
892 if (JTH.FallthroughUnreachable) {
893 if (JT.MBB != HeaderBB->getNextNode())
894 MIB.buildBr(*JT.MBB);
895 return true;
896 }
897
898 // Emit the range check for the jump table, and branch to the default block
899 // for the switch statement if the value being switched on exceeds the
900 // largest case in the switch.
901 auto Cst = getOrCreateVReg(
902 *ConstantInt::get(SValue.getType(), JTH.Last - JTH.First));
903 Cst = MIB.buildZExtOrTrunc(PtrScalarTy, Cst).getReg(0);
904 auto Cmp = MIB.buildICmp(CmpInst::ICMP_UGT, LLT::scalar(1), Sub, Cst);
905
906 auto BrCond = MIB.buildBrCond(Cmp.getReg(0), *JT.Default);
907
908 // Avoid emitting unnecessary branches to the next block.
909 if (JT.MBB != HeaderBB->getNextNode())
910 BrCond = MIB.buildBr(*JT.MBB);
911 return true;
912}
913
914void IRTranslator::emitSwitchCase(SwitchCG::CaseBlock &CB,
915 MachineBasicBlock *SwitchBB,
916 MachineIRBuilder &MIB) {
917 Register CondLHS = getOrCreateVReg(*CB.CmpLHS);
919 DebugLoc OldDbgLoc = MIB.getDebugLoc();
920 MIB.setDebugLoc(CB.DbgLoc);
921 MIB.setMBB(*CB.ThisBB);
922
923 if (CB.PredInfo.NoCmp) {
924 // Branch or fall through to TrueBB.
925 addSuccessorWithProb(CB.ThisBB, CB.TrueBB, CB.TrueProb);
926 addMachineCFGPred({SwitchBB->getBasicBlock(), CB.TrueBB->getBasicBlock()},
927 CB.ThisBB);
929 if (CB.TrueBB != CB.ThisBB->getNextNode())
930 MIB.buildBr(*CB.TrueBB);
931 MIB.setDebugLoc(OldDbgLoc);
932 return;
933 }
934
935 const LLT i1Ty = LLT::scalar(1);
936 // Build the compare.
937 if (!CB.CmpMHS) {
938 const auto *CI = dyn_cast<ConstantInt>(CB.CmpRHS);
939 // For conditional branch lowering, we might try to do something silly like
940 // emit an G_ICMP to compare an existing G_ICMP i1 result with true. If so,
941 // just re-use the existing condition vreg.
942 if (MRI->getType(CondLHS).getSizeInBits() == 1 && CI && CI->isOne() &&
944 Cond = CondLHS;
945 } else {
946 Register CondRHS = getOrCreateVReg(*CB.CmpRHS);
948 Cond =
949 MIB.buildFCmp(CB.PredInfo.Pred, i1Ty, CondLHS, CondRHS).getReg(0);
950 else
951 Cond =
952 MIB.buildICmp(CB.PredInfo.Pred, i1Ty, CondLHS, CondRHS).getReg(0);
953 }
954 } else {
956 "Can only handle SLE ranges");
957
958 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
959 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
960
961 Register CmpOpReg = getOrCreateVReg(*CB.CmpMHS);
962 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
963 Register CondRHS = getOrCreateVReg(*CB.CmpRHS);
964 Cond =
965 MIB.buildICmp(CmpInst::ICMP_SLE, i1Ty, CmpOpReg, CondRHS).getReg(0);
966 } else {
967 const LLT CmpTy = MRI->getType(CmpOpReg);
968 auto Sub = MIB.buildSub({CmpTy}, CmpOpReg, CondLHS);
969 auto Diff = MIB.buildConstant(CmpTy, High - Low);
970 Cond = MIB.buildICmp(CmpInst::ICMP_ULE, i1Ty, Sub, Diff).getReg(0);
971 }
972 }
973
974 // Update successor info
975 addSuccessorWithProb(CB.ThisBB, CB.TrueBB, CB.TrueProb);
976
977 addMachineCFGPred({SwitchBB->getBasicBlock(), CB.TrueBB->getBasicBlock()},
978 CB.ThisBB);
979
980 // TrueBB and FalseBB are always different unless the incoming IR is
981 // degenerate. This only happens when running llc on weird IR.
982 if (CB.TrueBB != CB.FalseBB)
983 addSuccessorWithProb(CB.ThisBB, CB.FalseBB, CB.FalseProb);
985
986 addMachineCFGPred({SwitchBB->getBasicBlock(), CB.FalseBB->getBasicBlock()},
987 CB.ThisBB);
988
989 MIB.buildBrCond(Cond, *CB.TrueBB);
990 MIB.buildBr(*CB.FalseBB);
991 MIB.setDebugLoc(OldDbgLoc);
992}
993
994bool IRTranslator::lowerJumpTableWorkItem(SwitchCG::SwitchWorkListItem W,
995 MachineBasicBlock *SwitchMBB,
996 MachineBasicBlock *CurMBB,
997 MachineBasicBlock *DefaultMBB,
998 MachineIRBuilder &MIB,
1000 BranchProbability UnhandledProbs,
1002 MachineBasicBlock *Fallthrough,
1003 bool FallthroughUnreachable) {
1004 using namespace SwitchCG;
1005 MachineFunction *CurMF = SwitchMBB->getParent();
1006 // FIXME: Optimize away range check based on pivot comparisons.
1007 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
1008 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
1009 BranchProbability DefaultProb = W.DefaultProb;
1010
1011 // The jump block hasn't been inserted yet; insert it here.
1012 MachineBasicBlock *JumpMBB = JT->MBB;
1013 CurMF->insert(BBI, JumpMBB);
1014
1015 // Since the jump table block is separate from the switch block, we need
1016 // to keep track of it as a machine predecessor to the default block,
1017 // otherwise we lose the phi edges.
1018 addMachineCFGPred({SwitchMBB->getBasicBlock(), DefaultMBB->getBasicBlock()},
1019 CurMBB);
1020 addMachineCFGPred({SwitchMBB->getBasicBlock(), DefaultMBB->getBasicBlock()},
1021 JumpMBB);
1022
1023 auto JumpProb = I->Prob;
1024 auto FallthroughProb = UnhandledProbs;
1025
1026 // If the default statement is a target of the jump table, we evenly
1027 // distribute the default probability to successors of CurMBB. Also
1028 // update the probability on the edge from JumpMBB to Fallthrough.
1029 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
1030 SE = JumpMBB->succ_end();
1031 SI != SE; ++SI) {
1032 if (*SI == DefaultMBB) {
1033 JumpProb += DefaultProb / 2;
1034 FallthroughProb -= DefaultProb / 2;
1035 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
1036 JumpMBB->normalizeSuccProbs();
1037 } else {
1038 // Also record edges from the jump table block to it's successors.
1039 addMachineCFGPred({SwitchMBB->getBasicBlock(), (*SI)->getBasicBlock()},
1040 JumpMBB);
1041 }
1042 }
1043
1044 if (FallthroughUnreachable)
1045 JTH->FallthroughUnreachable = true;
1046
1047 if (!JTH->FallthroughUnreachable)
1048 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
1049 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
1050 CurMBB->normalizeSuccProbs();
1051
1052 // The jump table header will be inserted in our current block, do the
1053 // range check, and fall through to our fallthrough block.
1054 JTH->HeaderBB = CurMBB;
1055 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
1056
1057 // If we're in the right place, emit the jump table header right now.
1058 if (CurMBB == SwitchMBB) {
1059 if (!emitJumpTableHeader(*JT, *JTH, CurMBB))
1060 return false;
1061 JTH->Emitted = true;
1062 }
1063 return true;
1064}
1065bool IRTranslator::lowerSwitchRangeWorkItem(SwitchCG::CaseClusterIt I,
1066 Value *Cond,
1067 MachineBasicBlock *Fallthrough,
1068 bool FallthroughUnreachable,
1069 BranchProbability UnhandledProbs,
1070 MachineBasicBlock *CurMBB,
1071 MachineIRBuilder &MIB,
1072 MachineBasicBlock *SwitchMBB) {
1073 using namespace SwitchCG;
1074 const Value *RHS, *LHS, *MHS;
1075 CmpInst::Predicate Pred;
1076 if (I->Low == I->High) {
1077 // Check Cond == I->Low.
1078 Pred = CmpInst::ICMP_EQ;
1079 LHS = Cond;
1080 RHS = I->Low;
1081 MHS = nullptr;
1082 } else {
1083 // Check I->Low <= Cond <= I->High.
1084 Pred = CmpInst::ICMP_SLE;
1085 LHS = I->Low;
1086 MHS = Cond;
1087 RHS = I->High;
1088 }
1089
1090 // If Fallthrough is unreachable, fold away the comparison.
1091 // The false probability is the sum of all unhandled cases.
1092 CaseBlock CB(Pred, FallthroughUnreachable, LHS, RHS, MHS, I->MBB, Fallthrough,
1093 CurMBB, MIB.getDebugLoc(), I->Prob, UnhandledProbs);
1094
1095 emitSwitchCase(CB, SwitchMBB, MIB);
1096 return true;
1097}
1098
1099void IRTranslator::emitBitTestHeader(SwitchCG::BitTestBlock &B,
1100 MachineBasicBlock *SwitchBB) {
1101 MachineIRBuilder &MIB = *CurBuilder;
1102 MIB.setMBB(*SwitchBB);
1103
1104 // Subtract the minimum value.
1105 Register SwitchOpReg = getOrCreateVReg(*B.SValue);
1106
1107 LLT SwitchOpTy = MRI->getType(SwitchOpReg);
1108 Register MinValReg = MIB.buildConstant(SwitchOpTy, B.First).getReg(0);
1109 auto RangeSub = MIB.buildSub(SwitchOpTy, SwitchOpReg, MinValReg);
1110
1111 Type *PtrIRTy = PointerType::getUnqual(MF->getFunction().getContext());
1112 const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
1113
1114 LLT MaskTy = SwitchOpTy;
1115 if (MaskTy.getSizeInBits() > PtrTy.getSizeInBits() ||
1117 MaskTy = LLT::scalar(PtrTy.getSizeInBits());
1118 else {
1119 // Ensure that the type will fit the mask value.
1120 for (const SwitchCG::BitTestCase &Case : B.Cases) {
1121 if (!isUIntN(SwitchOpTy.getSizeInBits(), Case.Mask)) {
1122 // Switch table case range are encoded into series of masks.
1123 // Just use pointer type, it's guaranteed to fit.
1124 MaskTy = LLT::scalar(PtrTy.getSizeInBits());
1125 break;
1126 }
1127 }
1128 }
1129 Register SubReg = RangeSub.getReg(0);
1130 if (SwitchOpTy != MaskTy)
1131 SubReg = MIB.buildZExtOrTrunc(MaskTy, SubReg).getReg(0);
1132
1133 B.RegVT = getMVTForLLT(MaskTy);
1134 B.Reg = SubReg;
1135
1136 MachineBasicBlock *MBB = B.Cases[0].ThisBB;
1137
1138 if (!B.FallthroughUnreachable)
1139 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
1140 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
1141
1142 SwitchBB->normalizeSuccProbs();
1143
1144 if (!B.FallthroughUnreachable) {
1145 // Conditional branch to the default block.
1146 auto RangeCst = MIB.buildConstant(SwitchOpTy, B.Range);
1147 auto RangeCmp = MIB.buildICmp(CmpInst::Predicate::ICMP_UGT, LLT::scalar(1),
1148 RangeSub, RangeCst);
1149 MIB.buildBrCond(RangeCmp, *B.Default);
1150 }
1151
1152 // Avoid emitting unnecessary branches to the next block.
1153 if (MBB != SwitchBB->getNextNode())
1154 MIB.buildBr(*MBB);
1155}
1156
1157void IRTranslator::emitBitTestCase(SwitchCG::BitTestBlock &BB,
1158 MachineBasicBlock *NextMBB,
1159 BranchProbability BranchProbToNext,
1161 MachineBasicBlock *SwitchBB) {
1162 MachineIRBuilder &MIB = *CurBuilder;
1163 MIB.setMBB(*SwitchBB);
1164
1165 LLT SwitchTy = getLLTForMVT(BB.RegVT);
1166 Register Cmp;
1167 unsigned PopCount = llvm::popcount(B.Mask);
1168 if (PopCount == 1) {
1169 // Testing for a single bit; just compare the shift count with what it
1170 // would need to be to shift a 1 bit in that position.
1171 auto MaskTrailingZeros =
1172 MIB.buildConstant(SwitchTy, llvm::countr_zero(B.Mask));
1173 Cmp =
1174 MIB.buildICmp(ICmpInst::ICMP_EQ, LLT::scalar(1), Reg, MaskTrailingZeros)
1175 .getReg(0);
1176 } else if (PopCount == BB.Range) {
1177 // There is only one zero bit in the range, test for it directly.
1178 auto MaskTrailingOnes =
1179 MIB.buildConstant(SwitchTy, llvm::countr_one(B.Mask));
1180 Cmp = MIB.buildICmp(CmpInst::ICMP_NE, LLT::scalar(1), Reg, MaskTrailingOnes)
1181 .getReg(0);
1182 } else {
1183 // Make desired shift.
1184 auto CstOne = MIB.buildConstant(SwitchTy, 1);
1185 auto SwitchVal = MIB.buildShl(SwitchTy, CstOne, Reg);
1186
1187 // Emit bit tests and jumps.
1188 auto CstMask = MIB.buildConstant(SwitchTy, B.Mask);
1189 auto AndOp = MIB.buildAnd(SwitchTy, SwitchVal, CstMask);
1190 auto CstZero = MIB.buildConstant(SwitchTy, 0);
1191 Cmp = MIB.buildICmp(CmpInst::ICMP_NE, LLT::scalar(1), AndOp, CstZero)
1192 .getReg(0);
1193 }
1194
1195 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
1196 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
1197 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
1198 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
1199 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
1200 // one as they are relative probabilities (and thus work more like weights),
1201 // and hence we need to normalize them to let the sum of them become one.
1202 SwitchBB->normalizeSuccProbs();
1203
1204 // Record the fact that the IR edge from the header to the bit test target
1205 // will go through our new block. Neeeded for PHIs to have nodes added.
1206 addMachineCFGPred({BB.Parent->getBasicBlock(), B.TargetBB->getBasicBlock()},
1207 SwitchBB);
1208
1209 MIB.buildBrCond(Cmp, *B.TargetBB);
1210
1211 // Avoid emitting unnecessary branches to the next block.
1212 if (NextMBB != SwitchBB->getNextNode())
1213 MIB.buildBr(*NextMBB);
1214}
1215
1216bool IRTranslator::lowerBitTestWorkItem(
1218 MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
1220 BranchProbability DefaultProb, BranchProbability UnhandledProbs,
1222 bool FallthroughUnreachable) {
1223 using namespace SwitchCG;
1224 MachineFunction *CurMF = SwitchMBB->getParent();
1225 // FIXME: Optimize away range check based on pivot comparisons.
1226 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
1227 // The bit test blocks haven't been inserted yet; insert them here.
1228 for (BitTestCase &BTC : BTB->Cases)
1229 CurMF->insert(BBI, BTC.ThisBB);
1230
1231 // Fill in fields of the BitTestBlock.
1232 BTB->Parent = CurMBB;
1233 BTB->Default = Fallthrough;
1234
1235 BTB->DefaultProb = UnhandledProbs;
1236 // If the cases in bit test don't form a contiguous range, we evenly
1237 // distribute the probability on the edge to Fallthrough to two
1238 // successors of CurMBB.
1239 if (!BTB->ContiguousRange) {
1240 BTB->Prob += DefaultProb / 2;
1241 BTB->DefaultProb -= DefaultProb / 2;
1242 }
1243
1244 if (FallthroughUnreachable)
1245 BTB->FallthroughUnreachable = true;
1246
1247 // If we're in the right place, emit the bit test header right now.
1248 if (CurMBB == SwitchMBB) {
1249 emitBitTestHeader(*BTB, SwitchMBB);
1250 BTB->Emitted = true;
1251 }
1252 return true;
1253}
1254
1255bool IRTranslator::lowerSwitchWorkItem(SwitchCG::SwitchWorkListItem W,
1256 Value *Cond,
1257 MachineBasicBlock *SwitchMBB,
1258 MachineBasicBlock *DefaultMBB,
1259 MachineIRBuilder &MIB) {
1260 using namespace SwitchCG;
1261 MachineFunction *CurMF = FuncInfo.MF;
1262 MachineBasicBlock *NextMBB = nullptr;
1264 if (++BBI != FuncInfo.MF->end())
1265 NextMBB = &*BBI;
1266
1267 if (EnableOpts) {
1268 // Here, we order cases by probability so the most likely case will be
1269 // checked first. However, two clusters can have the same probability in
1270 // which case their relative ordering is non-deterministic. So we use Low
1271 // as a tie-breaker as clusters are guaranteed to never overlap.
1272 llvm::sort(W.FirstCluster, W.LastCluster + 1,
1273 [](const CaseCluster &a, const CaseCluster &b) {
1274 return a.Prob != b.Prob
1275 ? a.Prob > b.Prob
1276 : a.Low->getValue().slt(b.Low->getValue());
1277 });
1278
1279 // Rearrange the case blocks so that the last one falls through if possible
1280 // without changing the order of probabilities.
1281 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster;) {
1282 --I;
1283 if (I->Prob > W.LastCluster->Prob)
1284 break;
1285 if (I->Kind == CC_Range && I->MBB == NextMBB) {
1286 std::swap(*I, *W.LastCluster);
1287 break;
1288 }
1289 }
1290 }
1291
1292 // Compute total probability.
1293 BranchProbability DefaultProb = W.DefaultProb;
1294 BranchProbability UnhandledProbs = DefaultProb;
1295 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
1296 UnhandledProbs += I->Prob;
1297
1298 MachineBasicBlock *CurMBB = W.MBB;
1299 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
1300 bool FallthroughUnreachable = false;
1301 MachineBasicBlock *Fallthrough;
1302 if (I == W.LastCluster) {
1303 // For the last cluster, fall through to the default destination.
1304 Fallthrough = DefaultMBB;
1305 FallthroughUnreachable = isa<UnreachableInst>(
1306 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
1307 } else {
1308 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
1309 CurMF->insert(BBI, Fallthrough);
1310 }
1311 UnhandledProbs -= I->Prob;
1312
1313 switch (I->Kind) {
1314 case CC_BitTests: {
1315 if (!lowerBitTestWorkItem(W, SwitchMBB, CurMBB, DefaultMBB, MIB, BBI,
1316 DefaultProb, UnhandledProbs, I, Fallthrough,
1317 FallthroughUnreachable)) {
1318 LLVM_DEBUG(dbgs() << "Failed to lower bit test for switch");
1319 return false;
1320 }
1321 break;
1322 }
1323
1324 case CC_JumpTable: {
1325 if (!lowerJumpTableWorkItem(W, SwitchMBB, CurMBB, DefaultMBB, MIB, BBI,
1326 UnhandledProbs, I, Fallthrough,
1327 FallthroughUnreachable)) {
1328 LLVM_DEBUG(dbgs() << "Failed to lower jump table");
1329 return false;
1330 }
1331 break;
1332 }
1333 case CC_Range: {
1334 if (!lowerSwitchRangeWorkItem(I, Cond, Fallthrough,
1335 FallthroughUnreachable, UnhandledProbs,
1336 CurMBB, MIB, SwitchMBB)) {
1337 LLVM_DEBUG(dbgs() << "Failed to lower switch range");
1338 return false;
1339 }
1340 break;
1341 }
1342 }
1343 CurMBB = Fallthrough;
1344 }
1345
1346 return true;
1347}
1348
1349bool IRTranslator::translateIndirectBr(const User &U,
1350 MachineIRBuilder &MIRBuilder) {
1351 const IndirectBrInst &BrInst = cast<IndirectBrInst>(U);
1352
1353 const Register Tgt = getOrCreateVReg(*BrInst.getAddress());
1354 MIRBuilder.buildBrIndirect(Tgt);
1355
1356 // Link successors.
1357 SmallPtrSet<const BasicBlock *, 32> AddedSuccessors;
1358 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
1359 for (const BasicBlock *Succ : successors(&BrInst)) {
1360 // It's legal for indirectbr instructions to have duplicate blocks in the
1361 // destination list. We don't allow this in MIR. Skip anything that's
1362 // already a successor.
1363 if (!AddedSuccessors.insert(Succ).second)
1364 continue;
1365 CurBB.addSuccessor(&getMBB(*Succ));
1366 }
1367
1368 return true;
1369}
1370
1371static bool isSwiftError(const Value *V) {
1372 if (auto Arg = dyn_cast<Argument>(V))
1373 return Arg->hasSwiftErrorAttr();
1374 if (auto AI = dyn_cast<AllocaInst>(V))
1375 return AI->isSwiftError();
1376 return false;
1377}
1378
1379bool IRTranslator::translateLoad(const User &U, MachineIRBuilder &MIRBuilder) {
1380 const LoadInst &LI = cast<LoadInst>(U);
1381 TypeSize StoreSize = DL->getTypeStoreSize(LI.getType());
1382 if (StoreSize.isZero())
1383 return true;
1384
1385 ArrayRef<Register> Regs = getOrCreateVRegs(LI);
1386 ArrayRef<uint64_t> Offsets = *VMap.getOffsets(LI);
1387 Register Base = getOrCreateVReg(*LI.getPointerOperand());
1388 AAMDNodes AAInfo = LI.getAAMetadata();
1389
1390 const Value *Ptr = LI.getPointerOperand();
1391 Type *OffsetIRTy = DL->getIndexType(Ptr->getType());
1392 LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1393
1394 if (CLI->supportSwiftError() && isSwiftError(Ptr)) {
1395 assert(Regs.size() == 1 && "swifterror should be single pointer");
1396 Register VReg =
1397 SwiftError.getOrCreateVRegUseAt(&LI, &MIRBuilder.getMBB(), Ptr);
1398 MIRBuilder.buildCopy(Regs[0], VReg);
1399 return true;
1400 }
1401
1403 TLI->getLoadMemOperandFlags(LI, *DL, AC, LibInfo);
1404 if (AA && !(Flags & MachineMemOperand::MOInvariant)) {
1405 if (AA->pointsToConstantMemory(
1406 MemoryLocation(Ptr, LocationSize::precise(StoreSize), AAInfo))) {
1408 }
1409 }
1410
1411 const MDNode *Ranges =
1412 Regs.size() == 1 ? LI.getMetadata(LLVMContext::MD_range) : nullptr;
1413 for (unsigned i = 0; i < Regs.size(); ++i) {
1414 Register Addr;
1415 MIRBuilder.materializeObjectPtrOffset(Addr, Base, OffsetTy, Offsets[i] / 8);
1416
1417 MachinePointerInfo Ptr(LI.getPointerOperand(), Offsets[i] / 8);
1418 Align BaseAlign = getMemOpAlign(LI);
1419 auto MMO = MF->getMachineMemOperand(
1420 Ptr, Flags, MRI->getType(Regs[i]),
1421 commonAlignment(BaseAlign, Offsets[i] / 8), AAInfo, Ranges,
1422 LI.getSyncScopeID(), LI.getOrdering());
1423 MIRBuilder.buildLoad(Regs[i], Addr, *MMO);
1424 }
1425
1426 return true;
1427}
1428
1429bool IRTranslator::translateStore(const User &U, MachineIRBuilder &MIRBuilder) {
1430 const StoreInst &SI = cast<StoreInst>(U);
1431 if (DL->getTypeStoreSize(SI.getValueOperand()->getType()).isZero())
1432 return true;
1433
1434 ArrayRef<Register> Vals = getOrCreateVRegs(*SI.getValueOperand());
1435 ArrayRef<uint64_t> Offsets = *VMap.getOffsets(*SI.getValueOperand());
1436 Register Base = getOrCreateVReg(*SI.getPointerOperand());
1437
1438 Type *OffsetIRTy = DL->getIndexType(SI.getPointerOperandType());
1439 LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1440
1441 if (CLI->supportSwiftError() && isSwiftError(SI.getPointerOperand())) {
1442 assert(Vals.size() == 1 && "swifterror should be single pointer");
1443
1444 Register VReg = SwiftError.getOrCreateVRegDefAt(&SI, &MIRBuilder.getMBB(),
1445 SI.getPointerOperand());
1446 MIRBuilder.buildCopy(VReg, Vals[0]);
1447 return true;
1448 }
1449
1450 MachineMemOperand::Flags Flags = TLI->getStoreMemOperandFlags(SI, *DL);
1451
1452 for (unsigned i = 0; i < Vals.size(); ++i) {
1453 Register Addr;
1454 MIRBuilder.materializeObjectPtrOffset(Addr, Base, OffsetTy, Offsets[i] / 8);
1455
1456 MachinePointerInfo Ptr(SI.getPointerOperand(), Offsets[i] / 8);
1457 Align BaseAlign = getMemOpAlign(SI);
1458 auto MMO = MF->getMachineMemOperand(
1459 Ptr, Flags, MRI->getType(Vals[i]),
1460 commonAlignment(BaseAlign, Offsets[i] / 8), SI.getAAMetadata(), nullptr,
1461 SI.getSyncScopeID(), SI.getOrdering());
1462 MIRBuilder.buildStore(Vals[i], Addr, *MMO);
1463 }
1464 return true;
1465}
1466
1468 const Value *Src = U.getOperand(0);
1469 Type *Int32Ty = Type::getInt32Ty(U.getContext());
1470
1471 // getIndexedOffsetInType is designed for GEPs, so the first index is the
1472 // usual array element rather than looking into the actual aggregate.
1474 Indices.push_back(ConstantInt::get(Int32Ty, 0));
1475
1476 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
1477 for (auto Idx : EVI->indices())
1478 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
1479 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
1480 for (auto Idx : IVI->indices())
1481 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
1482 } else {
1483 llvm::append_range(Indices, drop_begin(U.operands()));
1484 }
1485
1486 return 8 * static_cast<uint64_t>(
1487 DL.getIndexedOffsetInType(Src->getType(), Indices));
1488}
1489
1490bool IRTranslator::translateExtractValue(const User &U,
1491 MachineIRBuilder &MIRBuilder) {
1492 const Value *Src = U.getOperand(0);
1493 uint64_t Offset = getOffsetFromIndices(U, *DL);
1494 ArrayRef<Register> SrcRegs = getOrCreateVRegs(*Src);
1495 ArrayRef<uint64_t> Offsets = *VMap.getOffsets(*Src);
1496 unsigned Idx = llvm::lower_bound(Offsets, Offset) - Offsets.begin();
1497 auto &DstRegs = allocateVRegs(U);
1498
1499 for (unsigned i = 0; i < DstRegs.size(); ++i)
1500 DstRegs[i] = SrcRegs[Idx++];
1501
1502 return true;
1503}
1504
1505bool IRTranslator::translateInsertValue(const User &U,
1506 MachineIRBuilder &MIRBuilder) {
1507 const Value *Src = U.getOperand(0);
1508 uint64_t Offset = getOffsetFromIndices(U, *DL);
1509 auto &DstRegs = allocateVRegs(U);
1510 ArrayRef<uint64_t> DstOffsets = *VMap.getOffsets(U);
1511 ArrayRef<Register> SrcRegs = getOrCreateVRegs(*Src);
1512 ArrayRef<Register> InsertedRegs = getOrCreateVRegs(*U.getOperand(1));
1513 auto *InsertedIt = InsertedRegs.begin();
1514
1515 for (unsigned i = 0; i < DstRegs.size(); ++i) {
1516 if (DstOffsets[i] >= Offset && InsertedIt != InsertedRegs.end())
1517 DstRegs[i] = *InsertedIt++;
1518 else
1519 DstRegs[i] = SrcRegs[i];
1520 }
1521
1522 return true;
1523}
1524
1525bool IRTranslator::translateSelect(const User &U,
1526 MachineIRBuilder &MIRBuilder) {
1527 Register Tst = getOrCreateVReg(*U.getOperand(0));
1528 ArrayRef<Register> ResRegs = getOrCreateVRegs(U);
1529 ArrayRef<Register> Op0Regs = getOrCreateVRegs(*U.getOperand(1));
1530 ArrayRef<Register> Op1Regs = getOrCreateVRegs(*U.getOperand(2));
1531
1532 uint32_t Flags = 0;
1533 if (const SelectInst *SI = dyn_cast<SelectInst>(&U))
1535
1536 for (unsigned i = 0; i < ResRegs.size(); ++i) {
1537 MIRBuilder.buildSelect(ResRegs[i], Tst, Op0Regs[i], Op1Regs[i], Flags);
1538 }
1539
1540 return true;
1541}
1542
1543bool IRTranslator::translateCopy(const User &U, const Value &V,
1544 MachineIRBuilder &MIRBuilder) {
1545 Register Src = getOrCreateVReg(V);
1546 auto &Regs = *VMap.getVRegs(U);
1547 if (Regs.empty()) {
1548 Regs.push_back(Src);
1549 VMap.getOffsets(U)->push_back(0);
1550 } else {
1551 // If we already assigned a vreg for this instruction, we can't change that.
1552 // Emit a copy to satisfy the users we already emitted.
1553 MIRBuilder.buildCopy(Regs[0], Src);
1554 }
1555 return true;
1556}
1557
1558bool IRTranslator::translateBitCast(const User &U,
1559 MachineIRBuilder &MIRBuilder) {
1560 // If we're bitcasting to the source type, we can reuse the source vreg.
1561 if (getLLTForType(*U.getOperand(0)->getType(), *DL) ==
1562 getLLTForType(*U.getType(), *DL)) {
1563 // If the source is a ConstantInt then it was probably created by
1564 // ConstantHoisting and we should leave it alone.
1565 if (isa<ConstantInt>(U.getOperand(0)))
1566 return translateCast(TargetOpcode::G_CONSTANT_FOLD_BARRIER, U,
1567 MIRBuilder);
1568 return translateCopy(U, *U.getOperand(0), MIRBuilder);
1569 }
1570
1571 return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder);
1572}
1573
1574bool IRTranslator::translateCast(unsigned Opcode, const User &U,
1575 MachineIRBuilder &MIRBuilder) {
1577 return false;
1578
1579 uint32_t Flags = 0;
1580 if (const Instruction *I = dyn_cast<Instruction>(&U))
1582
1583 Register Op = getOrCreateVReg(*U.getOperand(0));
1584 Register Res = getOrCreateVReg(U);
1585 MIRBuilder.buildInstr(Opcode, {Res}, {Op}, Flags);
1586 return true;
1587}
1588
1589bool IRTranslator::translateGetElementPtr(const User &U,
1590 MachineIRBuilder &MIRBuilder) {
1591 Value &Op0 = *U.getOperand(0);
1592 Register BaseReg = getOrCreateVReg(Op0);
1593 Type *PtrIRTy = Op0.getType();
1594 LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
1595 Type *OffsetIRTy = DL->getIndexType(PtrIRTy);
1596 LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1597
1598 uint32_t PtrAddFlags = 0;
1599 // Each PtrAdd generated to implement the GEP inherits its nuw, nusw, inbounds
1600 // flags.
1601 if (const Instruction *I = dyn_cast<Instruction>(&U))
1603
1604 auto PtrAddFlagsWithConst = [&](int64_t Offset) {
1605 // For nusw/inbounds GEP with an offset that is nonnegative when interpreted
1606 // as signed, assume there is no unsigned overflow.
1607 if (Offset >= 0 && (PtrAddFlags & MachineInstr::MIFlag::NoUSWrap))
1608 return PtrAddFlags | MachineInstr::MIFlag::NoUWrap;
1609 return PtrAddFlags;
1610 };
1611
1612 // Normalize Vector GEP - all scalar operands should be converted to the
1613 // splat vector.
1614 unsigned VectorWidth = 0;
1615
1616 // True if we should use a splat vector; using VectorWidth alone is not
1617 // sufficient.
1618 bool WantSplatVector = false;
1619 if (auto *VT = dyn_cast<VectorType>(U.getType())) {
1620 VectorWidth = cast<FixedVectorType>(VT)->getNumElements();
1621 // We don't produce 1 x N vectors; those are treated as scalars.
1622 WantSplatVector = VectorWidth > 1;
1623 }
1624
1625 // We might need to splat the base pointer into a vector if the offsets
1626 // are vectors.
1627 if (WantSplatVector && !PtrTy.isVector()) {
1628 BaseReg = MIRBuilder
1629 .buildSplatBuildVector(LLT::fixed_vector(VectorWidth, PtrTy),
1630 BaseReg)
1631 .getReg(0);
1632 PtrIRTy = FixedVectorType::get(PtrIRTy, VectorWidth);
1633 PtrTy = getLLTForType(*PtrIRTy, *DL);
1634 OffsetIRTy = DL->getIndexType(PtrIRTy);
1635 OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1636 }
1637
1638 int64_t Offset = 0;
1639 for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
1640 GTI != E; ++GTI) {
1641 const Value *Idx = GTI.getOperand();
1642 if (StructType *StTy = GTI.getStructTypeOrNull()) {
1643 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
1644 Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
1645 continue;
1646 } else {
1647 uint64_t ElementSize = GTI.getSequentialElementStride(*DL);
1648
1649 // If this is a scalar constant or a splat vector of constants,
1650 // handle it quickly.
1651 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
1652 if (std::optional<int64_t> Val = CI->getValue().trySExtValue()) {
1653 Offset += ElementSize * *Val;
1654 continue;
1655 }
1656 }
1657
1658 if (Offset != 0) {
1659 auto OffsetMIB = MIRBuilder.buildConstant({OffsetTy}, Offset);
1660 BaseReg = MIRBuilder
1661 .buildPtrAdd(PtrTy, BaseReg, OffsetMIB.getReg(0),
1662 PtrAddFlagsWithConst(Offset))
1663 .getReg(0);
1664 Offset = 0;
1665 }
1666
1667 Register IdxReg = getOrCreateVReg(*Idx);
1668 LLT IdxTy = MRI->getType(IdxReg);
1669 if (IdxTy != OffsetTy) {
1670 if (!IdxTy.isVector() && WantSplatVector) {
1671 IdxReg = MIRBuilder
1673 IdxReg)
1674 .getReg(0);
1675 }
1676
1677 IdxReg = MIRBuilder.buildSExtOrTrunc(OffsetTy, IdxReg).getReg(0);
1678 }
1679
1680 // N = N + Idx * ElementSize;
1681 // Avoid doing it for ElementSize of 1.
1682 Register GepOffsetReg;
1683 if (ElementSize != 1) {
1684 auto ElementSizeMIB = MIRBuilder.buildConstant(
1685 getLLTForType(*OffsetIRTy, *DL), ElementSize);
1686
1687 // The multiplication is NUW if the GEP is NUW and NSW if the GEP is
1688 // NUSW.
1689 uint32_t ScaleFlags = PtrAddFlags & MachineInstr::MIFlag::NoUWrap;
1690 if (PtrAddFlags & MachineInstr::MIFlag::NoUSWrap)
1691 ScaleFlags |= MachineInstr::MIFlag::NoSWrap;
1692
1693 GepOffsetReg =
1694 MIRBuilder.buildMul(OffsetTy, IdxReg, ElementSizeMIB, ScaleFlags)
1695 .getReg(0);
1696 } else {
1697 GepOffsetReg = IdxReg;
1698 }
1699
1700 BaseReg =
1701 MIRBuilder.buildPtrAdd(PtrTy, BaseReg, GepOffsetReg, PtrAddFlags)
1702 .getReg(0);
1703 }
1704 }
1705
1706 if (Offset != 0) {
1707 auto OffsetMIB =
1708 MIRBuilder.buildConstant(OffsetTy, Offset);
1709
1710 MIRBuilder.buildPtrAdd(getOrCreateVReg(U), BaseReg, OffsetMIB.getReg(0),
1711 PtrAddFlagsWithConst(Offset));
1712 return true;
1713 }
1714
1715 MIRBuilder.buildCopy(getOrCreateVReg(U), BaseReg);
1716 return true;
1717}
1718
1719bool IRTranslator::translateMemFunc(const CallInst &CI,
1720 MachineIRBuilder &MIRBuilder,
1721 unsigned Opcode) {
1722 const Value *SrcPtr = CI.getArgOperand(1);
1723 // If the source is undef, then just emit a nop.
1724 if (isa<UndefValue>(SrcPtr))
1725 return true;
1726
1728
1729 unsigned MinPtrSize = UINT_MAX;
1730 for (auto AI = CI.arg_begin(), AE = CI.arg_end(); std::next(AI) != AE; ++AI) {
1731 Register SrcReg = getOrCreateVReg(**AI);
1732 LLT SrcTy = MRI->getType(SrcReg);
1733 if (SrcTy.isPointer())
1734 MinPtrSize = std::min<unsigned>(SrcTy.getSizeInBits(), MinPtrSize);
1735 SrcRegs.push_back(SrcReg);
1736 }
1737
1738 LLT SizeTy = LLT::scalar(MinPtrSize);
1739
1740 // The size operand should be the minimum of the pointer sizes.
1741 Register &SizeOpReg = SrcRegs[SrcRegs.size() - 1];
1742 if (MRI->getType(SizeOpReg) != SizeTy)
1743 SizeOpReg = MIRBuilder.buildZExtOrTrunc(SizeTy, SizeOpReg).getReg(0);
1744
1745 auto ICall = MIRBuilder.buildInstr(Opcode);
1746 for (Register SrcReg : SrcRegs)
1747 ICall.addUse(SrcReg);
1748
1749 Align DstAlign;
1750 Align SrcAlign;
1751 unsigned IsVol =
1752 cast<ConstantInt>(CI.getArgOperand(CI.arg_size() - 1))->getZExtValue();
1753
1754 ConstantInt *CopySize = nullptr;
1755
1756 if (auto *MCI = dyn_cast<MemCpyInst>(&CI)) {
1757 DstAlign = MCI->getDestAlign().valueOrOne();
1758 SrcAlign = MCI->getSourceAlign().valueOrOne();
1759 CopySize = dyn_cast<ConstantInt>(MCI->getArgOperand(2));
1760 } else if (auto *MMI = dyn_cast<MemMoveInst>(&CI)) {
1761 DstAlign = MMI->getDestAlign().valueOrOne();
1762 SrcAlign = MMI->getSourceAlign().valueOrOne();
1763 CopySize = dyn_cast<ConstantInt>(MMI->getArgOperand(2));
1764 } else {
1765 auto *MSI = cast<MemSetInst>(&CI);
1766 DstAlign = MSI->getDestAlign().valueOrOne();
1767 }
1768
1769 if (Opcode != TargetOpcode::G_MEMCPY_INLINE) {
1770 // We need to propagate the tail call flag from the IR inst as an argument.
1771 // Otherwise, we have to pessimize and assume later that we cannot tail call
1772 // any memory intrinsics.
1773 ICall.addImm(CI.isTailCall() ? 1 : 0);
1774 }
1775
1776 // Create mem operands to store the alignment and volatile info.
1779 if (IsVol) {
1780 LoadFlags |= MachineMemOperand::MOVolatile;
1781 StoreFlags |= MachineMemOperand::MOVolatile;
1782 }
1783
1784 AAMDNodes AAInfo = CI.getAAMetadata();
1785 if (AA && CopySize &&
1786 AA->pointsToConstantMemory(MemoryLocation(
1787 SrcPtr, LocationSize::precise(CopySize->getZExtValue()), AAInfo))) {
1788 LoadFlags |= MachineMemOperand::MOInvariant;
1789
1790 // FIXME: pointsToConstantMemory probably does not imply dereferenceable,
1791 // but the previous usage implied it did. Probably should check
1792 // isDereferenceableAndAlignedPointer.
1794 }
1795
1796 ICall.addMemOperand(
1797 MF->getMachineMemOperand(MachinePointerInfo(CI.getArgOperand(0)),
1798 StoreFlags, 1, DstAlign, AAInfo));
1799 if (Opcode != TargetOpcode::G_MEMSET)
1800 ICall.addMemOperand(MF->getMachineMemOperand(
1801 MachinePointerInfo(SrcPtr), LoadFlags, 1, SrcAlign, AAInfo));
1802
1803 return true;
1804}
1805
1806bool IRTranslator::translateTrap(const CallInst &CI,
1807 MachineIRBuilder &MIRBuilder,
1808 unsigned Opcode) {
1809 StringRef TrapFuncName =
1810 CI.getAttributes().getFnAttr("trap-func-name").getValueAsString();
1811 if (TrapFuncName.empty()) {
1812 if (Opcode == TargetOpcode::G_UBSANTRAP) {
1813 uint64_t Code = cast<ConstantInt>(CI.getOperand(0))->getZExtValue();
1814 MIRBuilder.buildInstr(Opcode, {}, ArrayRef<llvm::SrcOp>{Code});
1815 } else {
1816 MIRBuilder.buildInstr(Opcode);
1817 }
1818 return true;
1819 }
1820
1821 CallLowering::CallLoweringInfo Info;
1822 if (Opcode == TargetOpcode::G_UBSANTRAP)
1823 Info.OrigArgs.push_back({getOrCreateVRegs(*CI.getArgOperand(0)),
1824 CI.getArgOperand(0)->getType(), 0});
1825
1826 Info.Callee = MachineOperand::CreateES(TrapFuncName.data());
1827 Info.CB = &CI;
1828 Info.OrigRet = {Register(), Type::getVoidTy(CI.getContext()), 0};
1829 return CLI->lowerCall(MIRBuilder, Info);
1830}
1831
1832bool IRTranslator::translateVectorInterleave2Intrinsic(
1833 const CallInst &CI, MachineIRBuilder &MIRBuilder) {
1834 assert(CI.getIntrinsicID() == Intrinsic::vector_interleave2 &&
1835 "This function can only be called on the interleave2 intrinsic!");
1836 // Canonicalize interleave2 to G_SHUFFLE_VECTOR (similar to SelectionDAG).
1837 Register Op0 = getOrCreateVReg(*CI.getOperand(0));
1838 Register Op1 = getOrCreateVReg(*CI.getOperand(1));
1839 Register Res = getOrCreateVReg(CI);
1840
1841 LLT OpTy = MRI->getType(Op0);
1842 MIRBuilder.buildShuffleVector(Res, Op0, Op1,
1844
1845 return true;
1846}
1847
1848bool IRTranslator::translateVectorDeinterleave2Intrinsic(
1849 const CallInst &CI, MachineIRBuilder &MIRBuilder) {
1850 assert(CI.getIntrinsicID() == Intrinsic::vector_deinterleave2 &&
1851 "This function can only be called on the deinterleave2 intrinsic!");
1852 // Canonicalize deinterleave2 to shuffles that extract sub-vectors (similar to
1853 // SelectionDAG).
1854 Register Op = getOrCreateVReg(*CI.getOperand(0));
1855 auto Undef = MIRBuilder.buildUndef(MRI->getType(Op));
1856 ArrayRef<Register> Res = getOrCreateVRegs(CI);
1857
1858 LLT ResTy = MRI->getType(Res[0]);
1859 MIRBuilder.buildShuffleVector(Res[0], Op, Undef,
1860 createStrideMask(0, 2, ResTy.getNumElements()));
1861 MIRBuilder.buildShuffleVector(Res[1], Op, Undef,
1862 createStrideMask(1, 2, ResTy.getNumElements()));
1863
1864 return true;
1865}
1866
1867void IRTranslator::getStackGuard(Register DstReg,
1868 MachineIRBuilder &MIRBuilder) {
1869 Value *Global = TLI->getSDagStackGuard(*MF->getFunction().getParent());
1870 if (!Global) {
1871 LLVMContext &Ctx = MIRBuilder.getContext();
1872 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
1873 MIRBuilder.buildUndef(DstReg);
1874 return;
1875 }
1876
1877 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1878 MRI->setRegClass(DstReg, TRI->getPointerRegClass());
1879 auto MIB =
1880 MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD, {DstReg}, {});
1881
1882 unsigned AddrSpace = Global->getType()->getPointerAddressSpace();
1883 LLT PtrTy = LLT::pointer(AddrSpace, DL->getPointerSizeInBits(AddrSpace));
1884
1885 MachinePointerInfo MPInfo(Global);
1888 MachineMemOperand *MemRef = MF->getMachineMemOperand(
1889 MPInfo, Flags, PtrTy, DL->getPointerABIAlignment(AddrSpace));
1890 MIB.setMemRefs({MemRef});
1891}
1892
1893bool IRTranslator::translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
1894 MachineIRBuilder &MIRBuilder) {
1895 ArrayRef<Register> ResRegs = getOrCreateVRegs(CI);
1896 MIRBuilder.buildInstr(
1897 Op, {ResRegs[0], ResRegs[1]},
1898 {getOrCreateVReg(*CI.getOperand(0)), getOrCreateVReg(*CI.getOperand(1))});
1899
1900 return true;
1901}
1902
1903bool IRTranslator::translateFixedPointIntrinsic(unsigned Op, const CallInst &CI,
1904 MachineIRBuilder &MIRBuilder) {
1905 Register Dst = getOrCreateVReg(CI);
1906 Register Src0 = getOrCreateVReg(*CI.getOperand(0));
1907 Register Src1 = getOrCreateVReg(*CI.getOperand(1));
1908 uint64_t Scale = cast<ConstantInt>(CI.getOperand(2))->getZExtValue();
1909 MIRBuilder.buildInstr(Op, {Dst}, { Src0, Src1, Scale });
1910 return true;
1911}
1912
1913unsigned IRTranslator::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
1914 switch (ID) {
1915 default:
1916 break;
1917 case Intrinsic::acos:
1918 return TargetOpcode::G_FACOS;
1919 case Intrinsic::asin:
1920 return TargetOpcode::G_FASIN;
1921 case Intrinsic::atan:
1922 return TargetOpcode::G_FATAN;
1923 case Intrinsic::atan2:
1924 return TargetOpcode::G_FATAN2;
1925 case Intrinsic::bswap:
1926 return TargetOpcode::G_BSWAP;
1927 case Intrinsic::bitreverse:
1928 return TargetOpcode::G_BITREVERSE;
1929 case Intrinsic::fshl:
1930 return TargetOpcode::G_FSHL;
1931 case Intrinsic::fshr:
1932 return TargetOpcode::G_FSHR;
1933 case Intrinsic::ceil:
1934 return TargetOpcode::G_FCEIL;
1935 case Intrinsic::cos:
1936 return TargetOpcode::G_FCOS;
1937 case Intrinsic::cosh:
1938 return TargetOpcode::G_FCOSH;
1939 case Intrinsic::ctpop:
1940 return TargetOpcode::G_CTPOP;
1941 case Intrinsic::exp:
1942 return TargetOpcode::G_FEXP;
1943 case Intrinsic::exp2:
1944 return TargetOpcode::G_FEXP2;
1945 case Intrinsic::exp10:
1946 return TargetOpcode::G_FEXP10;
1947 case Intrinsic::fabs:
1948 return TargetOpcode::G_FABS;
1949 case Intrinsic::copysign:
1950 return TargetOpcode::G_FCOPYSIGN;
1951 case Intrinsic::minnum:
1952 return TargetOpcode::G_FMINNUM;
1953 case Intrinsic::maxnum:
1954 return TargetOpcode::G_FMAXNUM;
1955 case Intrinsic::minimum:
1956 return TargetOpcode::G_FMINIMUM;
1957 case Intrinsic::maximum:
1958 return TargetOpcode::G_FMAXIMUM;
1959 case Intrinsic::minimumnum:
1960 return TargetOpcode::G_FMINIMUMNUM;
1961 case Intrinsic::maximumnum:
1962 return TargetOpcode::G_FMAXIMUMNUM;
1963 case Intrinsic::canonicalize:
1964 return TargetOpcode::G_FCANONICALIZE;
1965 case Intrinsic::floor:
1966 return TargetOpcode::G_FFLOOR;
1967 case Intrinsic::fma:
1968 return TargetOpcode::G_FMA;
1969 case Intrinsic::log:
1970 return TargetOpcode::G_FLOG;
1971 case Intrinsic::log2:
1972 return TargetOpcode::G_FLOG2;
1973 case Intrinsic::log10:
1974 return TargetOpcode::G_FLOG10;
1975 case Intrinsic::ldexp:
1976 return TargetOpcode::G_FLDEXP;
1977 case Intrinsic::nearbyint:
1978 return TargetOpcode::G_FNEARBYINT;
1979 case Intrinsic::pow:
1980 return TargetOpcode::G_FPOW;
1981 case Intrinsic::powi:
1982 return TargetOpcode::G_FPOWI;
1983 case Intrinsic::rint:
1984 return TargetOpcode::G_FRINT;
1985 case Intrinsic::round:
1986 return TargetOpcode::G_INTRINSIC_ROUND;
1987 case Intrinsic::roundeven:
1988 return TargetOpcode::G_INTRINSIC_ROUNDEVEN;
1989 case Intrinsic::sin:
1990 return TargetOpcode::G_FSIN;
1991 case Intrinsic::sinh:
1992 return TargetOpcode::G_FSINH;
1993 case Intrinsic::sqrt:
1994 return TargetOpcode::G_FSQRT;
1995 case Intrinsic::tan:
1996 return TargetOpcode::G_FTAN;
1997 case Intrinsic::tanh:
1998 return TargetOpcode::G_FTANH;
1999 case Intrinsic::trunc:
2000 return TargetOpcode::G_INTRINSIC_TRUNC;
2001 case Intrinsic::readcyclecounter:
2002 return TargetOpcode::G_READCYCLECOUNTER;
2003 case Intrinsic::readsteadycounter:
2004 return TargetOpcode::G_READSTEADYCOUNTER;
2005 case Intrinsic::ptrmask:
2006 return TargetOpcode::G_PTRMASK;
2007 case Intrinsic::lrint:
2008 return TargetOpcode::G_INTRINSIC_LRINT;
2009 case Intrinsic::llrint:
2010 return TargetOpcode::G_INTRINSIC_LLRINT;
2011 // FADD/FMUL require checking the FMF, so are handled elsewhere.
2012 case Intrinsic::vector_reduce_fmin:
2013 return TargetOpcode::G_VECREDUCE_FMIN;
2014 case Intrinsic::vector_reduce_fmax:
2015 return TargetOpcode::G_VECREDUCE_FMAX;
2016 case Intrinsic::vector_reduce_fminimum:
2017 return TargetOpcode::G_VECREDUCE_FMINIMUM;
2018 case Intrinsic::vector_reduce_fmaximum:
2019 return TargetOpcode::G_VECREDUCE_FMAXIMUM;
2020 case Intrinsic::vector_reduce_add:
2021 return TargetOpcode::G_VECREDUCE_ADD;
2022 case Intrinsic::vector_reduce_mul:
2023 return TargetOpcode::G_VECREDUCE_MUL;
2024 case Intrinsic::vector_reduce_and:
2025 return TargetOpcode::G_VECREDUCE_AND;
2026 case Intrinsic::vector_reduce_or:
2027 return TargetOpcode::G_VECREDUCE_OR;
2028 case Intrinsic::vector_reduce_xor:
2029 return TargetOpcode::G_VECREDUCE_XOR;
2030 case Intrinsic::vector_reduce_smax:
2031 return TargetOpcode::G_VECREDUCE_SMAX;
2032 case Intrinsic::vector_reduce_smin:
2033 return TargetOpcode::G_VECREDUCE_SMIN;
2034 case Intrinsic::vector_reduce_umax:
2035 return TargetOpcode::G_VECREDUCE_UMAX;
2036 case Intrinsic::vector_reduce_umin:
2037 return TargetOpcode::G_VECREDUCE_UMIN;
2038 case Intrinsic::experimental_vector_compress:
2039 return TargetOpcode::G_VECTOR_COMPRESS;
2040 case Intrinsic::lround:
2041 return TargetOpcode::G_LROUND;
2042 case Intrinsic::llround:
2043 return TargetOpcode::G_LLROUND;
2044 case Intrinsic::get_fpenv:
2045 return TargetOpcode::G_GET_FPENV;
2046 case Intrinsic::get_fpmode:
2047 return TargetOpcode::G_GET_FPMODE;
2048 }
2050}
2051
2052bool IRTranslator::translateSimpleIntrinsic(const CallInst &CI,
2054 MachineIRBuilder &MIRBuilder) {
2055
2056 unsigned Op = getSimpleIntrinsicOpcode(ID);
2057
2058 // Is this a simple intrinsic?
2060 return false;
2061
2062 // Yes. Let's translate it.
2064 for (const auto &Arg : CI.args())
2065 VRegs.push_back(getOrCreateVReg(*Arg));
2066
2067 MIRBuilder.buildInstr(Op, {getOrCreateVReg(CI)}, VRegs,
2069 return true;
2070}
2071
2072// TODO: Include ConstainedOps.def when all strict instructions are defined.
2074 switch (ID) {
2075 case Intrinsic::experimental_constrained_fadd:
2076 return TargetOpcode::G_STRICT_FADD;
2077 case Intrinsic::experimental_constrained_fsub:
2078 return TargetOpcode::G_STRICT_FSUB;
2079 case Intrinsic::experimental_constrained_fmul:
2080 return TargetOpcode::G_STRICT_FMUL;
2081 case Intrinsic::experimental_constrained_fdiv:
2082 return TargetOpcode::G_STRICT_FDIV;
2083 case Intrinsic::experimental_constrained_frem:
2084 return TargetOpcode::G_STRICT_FREM;
2085 case Intrinsic::experimental_constrained_fma:
2086 return TargetOpcode::G_STRICT_FMA;
2087 case Intrinsic::experimental_constrained_sqrt:
2088 return TargetOpcode::G_STRICT_FSQRT;
2089 case Intrinsic::experimental_constrained_ldexp:
2090 return TargetOpcode::G_STRICT_FLDEXP;
2091 default:
2092 return 0;
2093 }
2094}
2095
2096bool IRTranslator::translateConstrainedFPIntrinsic(
2097 const ConstrainedFPIntrinsic &FPI, MachineIRBuilder &MIRBuilder) {
2099
2100 unsigned Opcode = getConstrainedOpcode(FPI.getIntrinsicID());
2101 if (!Opcode)
2102 return false;
2103
2107
2109 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
2110 VRegs.push_back(getOrCreateVReg(*FPI.getArgOperand(I)));
2111
2112 MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(FPI)}, VRegs, Flags);
2113 return true;
2114}
2115
2116std::optional<MCRegister> IRTranslator::getArgPhysReg(Argument &Arg) {
2117 auto VRegs = getOrCreateVRegs(Arg);
2118 if (VRegs.size() != 1)
2119 return std::nullopt;
2120
2121 // Arguments are lowered as a copy of a livein physical register.
2122 auto *VRegDef = MF->getRegInfo().getVRegDef(VRegs[0]);
2123 if (!VRegDef || !VRegDef->isCopy())
2124 return std::nullopt;
2125 return VRegDef->getOperand(1).getReg().asMCReg();
2126}
2127
2128bool IRTranslator::translateIfEntryValueArgument(bool isDeclare, Value *Val,
2129 const DILocalVariable *Var,
2130 const DIExpression *Expr,
2131 const DebugLoc &DL,
2132 MachineIRBuilder &MIRBuilder) {
2133 auto *Arg = dyn_cast<Argument>(Val);
2134 if (!Arg)
2135 return false;
2136
2137 if (!Expr->isEntryValue())
2138 return false;
2139
2140 std::optional<MCRegister> PhysReg = getArgPhysReg(*Arg);
2141 if (!PhysReg) {
2142 LLVM_DEBUG(dbgs() << "Dropping dbg." << (isDeclare ? "declare" : "value")
2143 << ": expression is entry_value but "
2144 << "couldn't find a physical register\n");
2145 LLVM_DEBUG(dbgs() << *Var << "\n");
2146 return true;
2147 }
2148
2149 if (isDeclare) {
2150 // Append an op deref to account for the fact that this is a dbg_declare.
2151 Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
2152 MF->setVariableDbgInfo(Var, Expr, *PhysReg, DL);
2153 } else {
2154 MIRBuilder.buildDirectDbgValue(*PhysReg, Var, Expr);
2155 }
2156
2157 return true;
2158}
2159
2161 switch (ID) {
2162 default:
2163 llvm_unreachable("Unexpected intrinsic");
2164 case Intrinsic::experimental_convergence_anchor:
2165 return TargetOpcode::CONVERGENCECTRL_ANCHOR;
2166 case Intrinsic::experimental_convergence_entry:
2167 return TargetOpcode::CONVERGENCECTRL_ENTRY;
2168 case Intrinsic::experimental_convergence_loop:
2169 return TargetOpcode::CONVERGENCECTRL_LOOP;
2170 }
2171}
2172
2173bool IRTranslator::translateConvergenceControlIntrinsic(
2174 const CallInst &CI, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder) {
2175 MachineInstrBuilder MIB = MIRBuilder.buildInstr(getConvOpcode(ID));
2176 Register OutputReg = getOrCreateConvergenceTokenVReg(CI);
2177 MIB.addDef(OutputReg);
2178
2179 if (ID == Intrinsic::experimental_convergence_loop) {
2181 assert(Bundle && "Expected a convergence control token.");
2182 Register InputReg =
2183 getOrCreateConvergenceTokenVReg(*Bundle->Inputs[0].get());
2184 MIB.addUse(InputReg);
2185 }
2186
2187 return true;
2188}
2189
2190bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
2191 MachineIRBuilder &MIRBuilder) {
2192 if (auto *MI = dyn_cast<AnyMemIntrinsic>(&CI)) {
2193 if (ORE->enabled()) {
2194 if (MemoryOpRemark::canHandle(MI, *LibInfo)) {
2195 MemoryOpRemark R(*ORE, "gisel-irtranslator-memsize", *DL, *LibInfo);
2196 R.visit(MI);
2197 }
2198 }
2199 }
2200
2201 // If this is a simple intrinsic (that is, we just need to add a def of
2202 // a vreg, and uses for each arg operand, then translate it.
2203 if (translateSimpleIntrinsic(CI, ID, MIRBuilder))
2204 return true;
2205
2206 switch (ID) {
2207 default:
2208 break;
2209 case Intrinsic::lifetime_start:
2210 case Intrinsic::lifetime_end: {
2211 // No stack colouring in O0, discard region information.
2212 if (MF->getTarget().getOptLevel() == CodeGenOptLevel::None ||
2213 MF->getFunction().hasOptNone())
2214 return true;
2215
2216 unsigned Op = ID == Intrinsic::lifetime_start ? TargetOpcode::LIFETIME_START
2217 : TargetOpcode::LIFETIME_END;
2218
2219 const AllocaInst *AI = dyn_cast<AllocaInst>(CI.getArgOperand(0));
2220 if (!AI || !AI->isStaticAlloca())
2221 return true;
2222
2223 MIRBuilder.buildInstr(Op).addFrameIndex(getOrCreateFrameIndex(*AI));
2224 return true;
2225 }
2226 case Intrinsic::fake_use: {
2228 for (const auto &Arg : CI.args())
2229 llvm::append_range(VRegs, getOrCreateVRegs(*Arg));
2230 MIRBuilder.buildInstr(TargetOpcode::FAKE_USE, {}, VRegs);
2231 MF->setHasFakeUses(true);
2232 return true;
2233 }
2234 case Intrinsic::dbg_declare: {
2235 const DbgDeclareInst &DI = cast<DbgDeclareInst>(CI);
2236 assert(DI.getVariable() && "Missing variable");
2237 translateDbgDeclareRecord(DI.getAddress(), DI.hasArgList(), DI.getVariable(),
2238 DI.getExpression(), DI.getDebugLoc(), MIRBuilder);
2239 return true;
2240 }
2241 case Intrinsic::dbg_label: {
2242 const DbgLabelInst &DI = cast<DbgLabelInst>(CI);
2243 assert(DI.getLabel() && "Missing label");
2244
2246 MIRBuilder.getDebugLoc()) &&
2247 "Expected inlined-at fields to agree");
2248
2249 MIRBuilder.buildDbgLabel(DI.getLabel());
2250 return true;
2251 }
2252 case Intrinsic::vaend:
2253 // No target I know of cares about va_end. Certainly no in-tree target
2254 // does. Simplest intrinsic ever!
2255 return true;
2256 case Intrinsic::vastart: {
2257 Value *Ptr = CI.getArgOperand(0);
2258 unsigned ListSize = TLI->getVaListSizeInBits(*DL) / 8;
2259 Align Alignment = getKnownAlignment(Ptr, *DL);
2260
2261 MIRBuilder.buildInstr(TargetOpcode::G_VASTART, {}, {getOrCreateVReg(*Ptr)})
2262 .addMemOperand(MF->getMachineMemOperand(MachinePointerInfo(Ptr),
2264 ListSize, Alignment));
2265 return true;
2266 }
2267 case Intrinsic::dbg_assign:
2268 // A dbg.assign is a dbg.value with more information about stack locations,
2269 // typically produced during optimisation of variables with leaked
2270 // addresses. We can treat it like a normal dbg_value intrinsic here; to
2271 // benefit from the full analysis of stack/SSA locations, GlobalISel would
2272 // need to register for and use the AssignmentTrackingAnalysis pass.
2273 [[fallthrough]];
2274 case Intrinsic::dbg_value: {
2275 // This form of DBG_VALUE is target-independent.
2276 const DbgValueInst &DI = cast<DbgValueInst>(CI);
2277 translateDbgValueRecord(DI.getValue(), DI.hasArgList(), DI.getVariable(),
2278 DI.getExpression(), DI.getDebugLoc(), MIRBuilder);
2279 return true;
2280 }
2281 case Intrinsic::uadd_with_overflow:
2282 return translateOverflowIntrinsic(CI, TargetOpcode::G_UADDO, MIRBuilder);
2283 case Intrinsic::sadd_with_overflow:
2284 return translateOverflowIntrinsic(CI, TargetOpcode::G_SADDO, MIRBuilder);
2285 case Intrinsic::usub_with_overflow:
2286 return translateOverflowIntrinsic(CI, TargetOpcode::G_USUBO, MIRBuilder);
2287 case Intrinsic::ssub_with_overflow:
2288 return translateOverflowIntrinsic(CI, TargetOpcode::G_SSUBO, MIRBuilder);
2289 case Intrinsic::umul_with_overflow:
2290 return translateOverflowIntrinsic(CI, TargetOpcode::G_UMULO, MIRBuilder);
2291 case Intrinsic::smul_with_overflow:
2292 return translateOverflowIntrinsic(CI, TargetOpcode::G_SMULO, MIRBuilder);
2293 case Intrinsic::uadd_sat:
2294 return translateBinaryOp(TargetOpcode::G_UADDSAT, CI, MIRBuilder);
2295 case Intrinsic::sadd_sat:
2296 return translateBinaryOp(TargetOpcode::G_SADDSAT, CI, MIRBuilder);
2297 case Intrinsic::usub_sat:
2298 return translateBinaryOp(TargetOpcode::G_USUBSAT, CI, MIRBuilder);
2299 case Intrinsic::ssub_sat:
2300 return translateBinaryOp(TargetOpcode::G_SSUBSAT, CI, MIRBuilder);
2301 case Intrinsic::ushl_sat:
2302 return translateBinaryOp(TargetOpcode::G_USHLSAT, CI, MIRBuilder);
2303 case Intrinsic::sshl_sat:
2304 return translateBinaryOp(TargetOpcode::G_SSHLSAT, CI, MIRBuilder);
2305 case Intrinsic::umin:
2306 return translateBinaryOp(TargetOpcode::G_UMIN, CI, MIRBuilder);
2307 case Intrinsic::umax:
2308 return translateBinaryOp(TargetOpcode::G_UMAX, CI, MIRBuilder);
2309 case Intrinsic::smin:
2310 return translateBinaryOp(TargetOpcode::G_SMIN, CI, MIRBuilder);
2311 case Intrinsic::smax:
2312 return translateBinaryOp(TargetOpcode::G_SMAX, CI, MIRBuilder);
2313 case Intrinsic::abs:
2314 // TODO: Preserve "int min is poison" arg in GMIR?
2315 return translateUnaryOp(TargetOpcode::G_ABS, CI, MIRBuilder);
2316 case Intrinsic::smul_fix:
2317 return translateFixedPointIntrinsic(TargetOpcode::G_SMULFIX, CI, MIRBuilder);
2318 case Intrinsic::umul_fix:
2319 return translateFixedPointIntrinsic(TargetOpcode::G_UMULFIX, CI, MIRBuilder);
2320 case Intrinsic::smul_fix_sat:
2321 return translateFixedPointIntrinsic(TargetOpcode::G_SMULFIXSAT, CI, MIRBuilder);
2322 case Intrinsic::umul_fix_sat:
2323 return translateFixedPointIntrinsic(TargetOpcode::G_UMULFIXSAT, CI, MIRBuilder);
2324 case Intrinsic::sdiv_fix:
2325 return translateFixedPointIntrinsic(TargetOpcode::G_SDIVFIX, CI, MIRBuilder);
2326 case Intrinsic::udiv_fix:
2327 return translateFixedPointIntrinsic(TargetOpcode::G_UDIVFIX, CI, MIRBuilder);
2328 case Intrinsic::sdiv_fix_sat:
2329 return translateFixedPointIntrinsic(TargetOpcode::G_SDIVFIXSAT, CI, MIRBuilder);
2330 case Intrinsic::udiv_fix_sat:
2331 return translateFixedPointIntrinsic(TargetOpcode::G_UDIVFIXSAT, CI, MIRBuilder);
2332 case Intrinsic::fmuladd: {
2333 const TargetMachine &TM = MF->getTarget();
2334 Register Dst = getOrCreateVReg(CI);
2335 Register Op0 = getOrCreateVReg(*CI.getArgOperand(0));
2336 Register Op1 = getOrCreateVReg(*CI.getArgOperand(1));
2337 Register Op2 = getOrCreateVReg(*CI.getArgOperand(2));
2338 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
2339 TLI->isFMAFasterThanFMulAndFAdd(*MF,
2340 TLI->getValueType(*DL, CI.getType()))) {
2341 // TODO: Revisit this to see if we should move this part of the
2342 // lowering to the combiner.
2343 MIRBuilder.buildFMA(Dst, Op0, Op1, Op2,
2345 } else {
2346 LLT Ty = getLLTForType(*CI.getType(), *DL);
2347 auto FMul = MIRBuilder.buildFMul(
2348 Ty, Op0, Op1, MachineInstr::copyFlagsFromInstruction(CI));
2349 MIRBuilder.buildFAdd(Dst, FMul, Op2,
2351 }
2352 return true;
2353 }
2354 case Intrinsic::convert_from_fp16:
2355 // FIXME: This intrinsic should probably be removed from the IR.
2356 MIRBuilder.buildFPExt(getOrCreateVReg(CI),
2357 getOrCreateVReg(*CI.getArgOperand(0)),
2359 return true;
2360 case Intrinsic::convert_to_fp16:
2361 // FIXME: This intrinsic should probably be removed from the IR.
2362 MIRBuilder.buildFPTrunc(getOrCreateVReg(CI),
2363 getOrCreateVReg(*CI.getArgOperand(0)),
2365 return true;
2366 case Intrinsic::frexp: {
2367 ArrayRef<Register> VRegs = getOrCreateVRegs(CI);
2368 MIRBuilder.buildFFrexp(VRegs[0], VRegs[1],
2369 getOrCreateVReg(*CI.getArgOperand(0)),
2371 return true;
2372 }
2373 case Intrinsic::modf: {
2374 ArrayRef<Register> VRegs = getOrCreateVRegs(CI);
2375 MIRBuilder.buildModf(VRegs[0], VRegs[1],
2376 getOrCreateVReg(*CI.getArgOperand(0)),
2378 return true;
2379 }
2380 case Intrinsic::sincos: {
2381 ArrayRef<Register> VRegs = getOrCreateVRegs(CI);
2382 MIRBuilder.buildFSincos(VRegs[0], VRegs[1],
2383 getOrCreateVReg(*CI.getArgOperand(0)),
2385 return true;
2386 }
2387 case Intrinsic::fptosi_sat:
2388 MIRBuilder.buildFPTOSI_SAT(getOrCreateVReg(CI),
2389 getOrCreateVReg(*CI.getArgOperand(0)));
2390 return true;
2391 case Intrinsic::fptoui_sat:
2392 MIRBuilder.buildFPTOUI_SAT(getOrCreateVReg(CI),
2393 getOrCreateVReg(*CI.getArgOperand(0)));
2394 return true;
2395 case Intrinsic::memcpy_inline:
2396 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMCPY_INLINE);
2397 case Intrinsic::memcpy:
2398 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMCPY);
2399 case Intrinsic::memmove:
2400 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMMOVE);
2401 case Intrinsic::memset:
2402 return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMSET);
2403 case Intrinsic::eh_typeid_for: {
2404 GlobalValue *GV = ExtractTypeInfo(CI.getArgOperand(0));
2405 Register Reg = getOrCreateVReg(CI);
2406 unsigned TypeID = MF->getTypeIDFor(GV);
2407 MIRBuilder.buildConstant(Reg, TypeID);
2408 return true;
2409 }
2410 case Intrinsic::objectsize:
2411 llvm_unreachable("llvm.objectsize.* should have been lowered already");
2412
2413 case Intrinsic::is_constant:
2414 llvm_unreachable("llvm.is.constant.* should have been lowered already");
2415
2416 case Intrinsic::stackguard:
2417 getStackGuard(getOrCreateVReg(CI), MIRBuilder);
2418 return true;
2419 case Intrinsic::stackprotector: {
2420 LLT PtrTy = getLLTForType(*CI.getArgOperand(0)->getType(), *DL);
2421 Register GuardVal;
2422 if (TLI->useLoadStackGuardNode(*CI.getModule())) {
2423 GuardVal = MRI->createGenericVirtualRegister(PtrTy);
2424 getStackGuard(GuardVal, MIRBuilder);
2425 } else
2426 GuardVal = getOrCreateVReg(*CI.getArgOperand(0)); // The guard's value.
2427
2428 AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
2429 int FI = getOrCreateFrameIndex(*Slot);
2430 MF->getFrameInfo().setStackProtectorIndex(FI);
2431
2432 MIRBuilder.buildStore(
2433 GuardVal, getOrCreateVReg(*Slot),
2434 *MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
2437 PtrTy, Align(8)));
2438 return true;
2439 }
2440 case Intrinsic::stacksave: {
2441 MIRBuilder.buildInstr(TargetOpcode::G_STACKSAVE, {getOrCreateVReg(CI)}, {});
2442 return true;
2443 }
2444 case Intrinsic::stackrestore: {
2445 MIRBuilder.buildInstr(TargetOpcode::G_STACKRESTORE, {},
2446 {getOrCreateVReg(*CI.getArgOperand(0))});
2447 return true;
2448 }
2449 case Intrinsic::cttz:
2450 case Intrinsic::ctlz: {
2451 ConstantInt *Cst = cast<ConstantInt>(CI.getArgOperand(1));
2452 bool isTrailing = ID == Intrinsic::cttz;
2453 unsigned Opcode = isTrailing
2454 ? Cst->isZero() ? TargetOpcode::G_CTTZ
2455 : TargetOpcode::G_CTTZ_ZERO_UNDEF
2456 : Cst->isZero() ? TargetOpcode::G_CTLZ
2457 : TargetOpcode::G_CTLZ_ZERO_UNDEF;
2458 MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(CI)},
2459 {getOrCreateVReg(*CI.getArgOperand(0))});
2460 return true;
2461 }
2462 case Intrinsic::invariant_start: {
2463 MIRBuilder.buildUndef(getOrCreateVReg(CI));
2464 return true;
2465 }
2466 case Intrinsic::invariant_end:
2467 return true;
2468 case Intrinsic::expect:
2469 case Intrinsic::expect_with_probability:
2470 case Intrinsic::annotation:
2471 case Intrinsic::ptr_annotation:
2472 case Intrinsic::launder_invariant_group:
2473 case Intrinsic::strip_invariant_group: {
2474 // Drop the intrinsic, but forward the value.
2475 MIRBuilder.buildCopy(getOrCreateVReg(CI),
2476 getOrCreateVReg(*CI.getArgOperand(0)));
2477 return true;
2478 }
2479 case Intrinsic::assume:
2480 case Intrinsic::experimental_noalias_scope_decl:
2481 case Intrinsic::var_annotation:
2482 case Intrinsic::sideeffect:
2483 // Discard annotate attributes, assumptions, and artificial side-effects.
2484 return true;
2485 case Intrinsic::read_volatile_register:
2486 case Intrinsic::read_register: {
2487 Value *Arg = CI.getArgOperand(0);
2488 MIRBuilder
2489 .buildInstr(TargetOpcode::G_READ_REGISTER, {getOrCreateVReg(CI)}, {})
2490 .addMetadata(cast<MDNode>(cast<MetadataAsValue>(Arg)->getMetadata()));
2491 return true;
2492 }
2493 case Intrinsic::write_register: {
2494 Value *Arg = CI.getArgOperand(0);
2495 MIRBuilder.buildInstr(TargetOpcode::G_WRITE_REGISTER)
2496 .addMetadata(cast<MDNode>(cast<MetadataAsValue>(Arg)->getMetadata()))
2497 .addUse(getOrCreateVReg(*CI.getArgOperand(1)));
2498 return true;
2499 }
2500 case Intrinsic::localescape: {
2501 MachineBasicBlock &EntryMBB = MF->front();
2502 StringRef EscapedName = GlobalValue::dropLLVMManglingEscape(MF->getName());
2503
2504 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
2505 // is the same on all targets.
2506 for (unsigned Idx = 0, E = CI.arg_size(); Idx < E; ++Idx) {
2507 Value *Arg = CI.getArgOperand(Idx)->stripPointerCasts();
2508 if (isa<ConstantPointerNull>(Arg))
2509 continue; // Skip null pointers. They represent a hole in index space.
2510
2511 int FI = getOrCreateFrameIndex(*cast<AllocaInst>(Arg));
2512 MCSymbol *FrameAllocSym =
2513 MF->getContext().getOrCreateFrameAllocSymbol(EscapedName, Idx);
2514
2515 // This should be inserted at the start of the entry block.
2516 auto LocalEscape =
2517 MIRBuilder.buildInstrNoInsert(TargetOpcode::LOCAL_ESCAPE)
2518 .addSym(FrameAllocSym)
2519 .addFrameIndex(FI);
2520
2521 EntryMBB.insert(EntryMBB.begin(), LocalEscape);
2522 }
2523
2524 return true;
2525 }
2526 case Intrinsic::vector_reduce_fadd:
2527 case Intrinsic::vector_reduce_fmul: {
2528 // Need to check for the reassoc flag to decide whether we want a
2529 // sequential reduction opcode or not.
2530 Register Dst = getOrCreateVReg(CI);
2531 Register ScalarSrc = getOrCreateVReg(*CI.getArgOperand(0));
2532 Register VecSrc = getOrCreateVReg(*CI.getArgOperand(1));
2533 unsigned Opc = 0;
2534 if (!CI.hasAllowReassoc()) {
2535 // The sequential ordering case.
2536 Opc = ID == Intrinsic::vector_reduce_fadd
2537 ? TargetOpcode::G_VECREDUCE_SEQ_FADD
2538 : TargetOpcode::G_VECREDUCE_SEQ_FMUL;
2539 if (!MRI->getType(VecSrc).isVector())
2540 Opc = ID == Intrinsic::vector_reduce_fadd ? TargetOpcode::G_FADD
2541 : TargetOpcode::G_FMUL;
2542 MIRBuilder.buildInstr(Opc, {Dst}, {ScalarSrc, VecSrc},
2544 return true;
2545 }
2546 // We split the operation into a separate G_FADD/G_FMUL + the reduce,
2547 // since the associativity doesn't matter.
2548 unsigned ScalarOpc;
2549 if (ID == Intrinsic::vector_reduce_fadd) {
2550 Opc = TargetOpcode::G_VECREDUCE_FADD;
2551 ScalarOpc = TargetOpcode::G_FADD;
2552 } else {
2553 Opc = TargetOpcode::G_VECREDUCE_FMUL;
2554 ScalarOpc = TargetOpcode::G_FMUL;
2555 }
2556 LLT DstTy = MRI->getType(Dst);
2557 auto Rdx = MIRBuilder.buildInstr(
2558 Opc, {DstTy}, {VecSrc}, MachineInstr::copyFlagsFromInstruction(CI));
2559 MIRBuilder.buildInstr(ScalarOpc, {Dst}, {ScalarSrc, Rdx},
2561
2562 return true;
2563 }
2564 case Intrinsic::trap:
2565 return translateTrap(CI, MIRBuilder, TargetOpcode::G_TRAP);
2566 case Intrinsic::debugtrap:
2567 return translateTrap(CI, MIRBuilder, TargetOpcode::G_DEBUGTRAP);
2568 case Intrinsic::ubsantrap:
2569 return translateTrap(CI, MIRBuilder, TargetOpcode::G_UBSANTRAP);
2570 case Intrinsic::allow_runtime_check:
2571 case Intrinsic::allow_ubsan_check:
2572 MIRBuilder.buildCopy(getOrCreateVReg(CI),
2573 getOrCreateVReg(*ConstantInt::getTrue(CI.getType())));
2574 return true;
2575 case Intrinsic::amdgcn_cs_chain:
2576 case Intrinsic::amdgcn_call_whole_wave:
2577 return translateCallBase(CI, MIRBuilder);
2578 case Intrinsic::fptrunc_round: {
2580
2581 // Convert the metadata argument to a constant integer
2582 Metadata *MD = cast<MetadataAsValue>(CI.getArgOperand(1))->getMetadata();
2583 std::optional<RoundingMode> RoundMode =
2584 convertStrToRoundingMode(cast<MDString>(MD)->getString());
2585
2586 // Add the Rounding mode as an integer
2587 MIRBuilder
2588 .buildInstr(TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND,
2589 {getOrCreateVReg(CI)},
2590 {getOrCreateVReg(*CI.getArgOperand(0))}, Flags)
2591 .addImm((int)*RoundMode);
2592
2593 return true;
2594 }
2595 case Intrinsic::is_fpclass: {
2596 Value *FpValue = CI.getOperand(0);
2597 ConstantInt *TestMaskValue = cast<ConstantInt>(CI.getOperand(1));
2598
2599 MIRBuilder
2600 .buildInstr(TargetOpcode::G_IS_FPCLASS, {getOrCreateVReg(CI)},
2601 {getOrCreateVReg(*FpValue)})
2602 .addImm(TestMaskValue->getZExtValue());
2603
2604 return true;
2605 }
2606 case Intrinsic::set_fpenv: {
2607 Value *FPEnv = CI.getOperand(0);
2608 MIRBuilder.buildSetFPEnv(getOrCreateVReg(*FPEnv));
2609 return true;
2610 }
2611 case Intrinsic::reset_fpenv:
2612 MIRBuilder.buildResetFPEnv();
2613 return true;
2614 case Intrinsic::set_fpmode: {
2615 Value *FPState = CI.getOperand(0);
2616 MIRBuilder.buildSetFPMode(getOrCreateVReg(*FPState));
2617 return true;
2618 }
2619 case Intrinsic::reset_fpmode:
2620 MIRBuilder.buildResetFPMode();
2621 return true;
2622 case Intrinsic::get_rounding:
2623 MIRBuilder.buildGetRounding(getOrCreateVReg(CI));
2624 return true;
2625 case Intrinsic::set_rounding:
2626 MIRBuilder.buildSetRounding(getOrCreateVReg(*CI.getOperand(0)));
2627 return true;
2628 case Intrinsic::vscale: {
2629 MIRBuilder.buildVScale(getOrCreateVReg(CI), 1);
2630 return true;
2631 }
2632 case Intrinsic::scmp:
2633 MIRBuilder.buildSCmp(getOrCreateVReg(CI),
2634 getOrCreateVReg(*CI.getOperand(0)),
2635 getOrCreateVReg(*CI.getOperand(1)));
2636 return true;
2637 case Intrinsic::ucmp:
2638 MIRBuilder.buildUCmp(getOrCreateVReg(CI),
2639 getOrCreateVReg(*CI.getOperand(0)),
2640 getOrCreateVReg(*CI.getOperand(1)));
2641 return true;
2642 case Intrinsic::vector_extract:
2643 return translateExtractVector(CI, MIRBuilder);
2644 case Intrinsic::vector_insert:
2645 return translateInsertVector(CI, MIRBuilder);
2646 case Intrinsic::stepvector: {
2647 MIRBuilder.buildStepVector(getOrCreateVReg(CI), 1);
2648 return true;
2649 }
2650 case Intrinsic::prefetch: {
2651 Value *Addr = CI.getOperand(0);
2652 unsigned RW = cast<ConstantInt>(CI.getOperand(1))->getZExtValue();
2653 unsigned Locality = cast<ConstantInt>(CI.getOperand(2))->getZExtValue();
2654 unsigned CacheType = cast<ConstantInt>(CI.getOperand(3))->getZExtValue();
2655
2657 auto &MMO = *MF->getMachineMemOperand(MachinePointerInfo(Addr), Flags,
2658 LLT(), Align());
2659
2660 MIRBuilder.buildPrefetch(getOrCreateVReg(*Addr), RW, Locality, CacheType,
2661 MMO);
2662
2663 return true;
2664 }
2665
2666 case Intrinsic::vector_interleave2:
2667 case Intrinsic::vector_deinterleave2: {
2668 // Both intrinsics have at least one operand.
2669 Value *Op0 = CI.getOperand(0);
2670 LLT ResTy = getLLTForType(*Op0->getType(), MIRBuilder.getDataLayout());
2671 if (!ResTy.isFixedVector())
2672 return false;
2673
2674 if (CI.getIntrinsicID() == Intrinsic::vector_interleave2)
2675 return translateVectorInterleave2Intrinsic(CI, MIRBuilder);
2676
2677 return translateVectorDeinterleave2Intrinsic(CI, MIRBuilder);
2678 }
2679
2680#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
2681 case Intrinsic::INTRINSIC:
2682#include "llvm/IR/ConstrainedOps.def"
2683 return translateConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(CI),
2684 MIRBuilder);
2685 case Intrinsic::experimental_convergence_anchor:
2686 case Intrinsic::experimental_convergence_entry:
2687 case Intrinsic::experimental_convergence_loop:
2688 return translateConvergenceControlIntrinsic(CI, ID, MIRBuilder);
2689 case Intrinsic::reloc_none: {
2690 Metadata *MD = cast<MetadataAsValue>(CI.getArgOperand(0))->getMetadata();
2691 StringRef SymbolName = cast<MDString>(MD)->getString();
2692 MIRBuilder.buildInstr(TargetOpcode::RELOC_NONE)
2694 return true;
2695 }
2696 }
2697 return false;
2698}
2699
2700bool IRTranslator::translateInlineAsm(const CallBase &CB,
2701 MachineIRBuilder &MIRBuilder) {
2703 return false;
2704
2705 const InlineAsmLowering *ALI = MF->getSubtarget().getInlineAsmLowering();
2706
2707 if (!ALI) {
2708 LLVM_DEBUG(
2709 dbgs() << "Inline asm lowering is not supported for this target yet\n");
2710 return false;
2711 }
2712
2713 return ALI->lowerInlineAsm(
2714 MIRBuilder, CB, [&](const Value &Val) { return getOrCreateVRegs(Val); });
2715}
2716
2717bool IRTranslator::translateCallBase(const CallBase &CB,
2718 MachineIRBuilder &MIRBuilder) {
2719 ArrayRef<Register> Res = getOrCreateVRegs(CB);
2720
2722 Register SwiftInVReg = 0;
2723 Register SwiftErrorVReg = 0;
2724 for (const auto &Arg : CB.args()) {
2725 if (CLI->supportSwiftError() && isSwiftError(Arg)) {
2726 assert(SwiftInVReg == 0 && "Expected only one swift error argument");
2727 LLT Ty = getLLTForType(*Arg->getType(), *DL);
2728 SwiftInVReg = MRI->createGenericVirtualRegister(Ty);
2729 MIRBuilder.buildCopy(SwiftInVReg, SwiftError.getOrCreateVRegUseAt(
2730 &CB, &MIRBuilder.getMBB(), Arg));
2731 Args.emplace_back(ArrayRef(SwiftInVReg));
2732 SwiftErrorVReg =
2733 SwiftError.getOrCreateVRegDefAt(&CB, &MIRBuilder.getMBB(), Arg);
2734 continue;
2735 }
2736 Args.push_back(getOrCreateVRegs(*Arg));
2737 }
2738
2739 if (auto *CI = dyn_cast<CallInst>(&CB)) {
2740 if (ORE->enabled()) {
2741 if (MemoryOpRemark::canHandle(CI, *LibInfo)) {
2742 MemoryOpRemark R(*ORE, "gisel-irtranslator-memsize", *DL, *LibInfo);
2743 R.visit(CI);
2744 }
2745 }
2746 }
2747
2748 std::optional<CallLowering::PtrAuthInfo> PAI;
2749 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_ptrauth)) {
2750 // Functions should never be ptrauth-called directly.
2751 assert(!CB.getCalledFunction() && "invalid direct ptrauth call");
2752
2753 const Value *Key = Bundle->Inputs[0];
2754 const Value *Discriminator = Bundle->Inputs[1];
2755
2756 // Look through ptrauth constants to try to eliminate the matching bundle
2757 // and turn this into a direct call with no ptrauth.
2758 // CallLowering will use the raw pointer if it doesn't find the PAI.
2759 const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CB.getCalledOperand());
2760 if (!CalleeCPA || !isa<Function>(CalleeCPA->getPointer()) ||
2761 !CalleeCPA->isKnownCompatibleWith(Key, Discriminator, *DL)) {
2762 // If we can't make it direct, package the bundle into PAI.
2763 Register DiscReg = getOrCreateVReg(*Discriminator);
2764 PAI = CallLowering::PtrAuthInfo{cast<ConstantInt>(Key)->getZExtValue(),
2765 DiscReg};
2766 }
2767 }
2768
2769 Register ConvergenceCtrlToken = 0;
2770 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
2771 const auto &Token = *Bundle->Inputs[0].get();
2772 ConvergenceCtrlToken = getOrCreateConvergenceTokenVReg(Token);
2773 }
2774
2775 // We don't set HasCalls on MFI here yet because call lowering may decide to
2776 // optimize into tail calls. Instead, we defer that to selection where a final
2777 // scan is done to check if any instructions are calls.
2778 bool Success = CLI->lowerCall(
2779 MIRBuilder, CB, Res, Args, SwiftErrorVReg, PAI, ConvergenceCtrlToken,
2780 [&]() { return getOrCreateVReg(*CB.getCalledOperand()); });
2781
2782 // Check if we just inserted a tail call.
2783 if (Success) {
2784 assert(!HasTailCall && "Can't tail call return twice from block?");
2785 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
2786 HasTailCall = TII->isTailCall(*std::prev(MIRBuilder.getInsertPt()));
2787 }
2788
2789 return Success;
2790}
2791
2792bool IRTranslator::translateCall(const User &U, MachineIRBuilder &MIRBuilder) {
2794 return false;
2795
2796 const CallInst &CI = cast<CallInst>(U);
2797 const Function *F = CI.getCalledFunction();
2798
2799 // FIXME: support Windows dllimport function calls and calls through
2800 // weak symbols.
2801 if (F && (F->hasDLLImportStorageClass() ||
2802 (MF->getTarget().getTargetTriple().isOSWindows() &&
2803 F->hasExternalWeakLinkage())))
2804 return false;
2805
2806 // FIXME: support control flow guard targets.
2808 return false;
2809
2810 // FIXME: support statepoints and related.
2812 return false;
2813
2814 if (CI.isInlineAsm())
2815 return translateInlineAsm(CI, MIRBuilder);
2816
2817 Intrinsic::ID ID = F ? F->getIntrinsicID() : Intrinsic::not_intrinsic;
2818 if (!F || ID == Intrinsic::not_intrinsic) {
2819 if (translateCallBase(CI, MIRBuilder)) {
2820 diagnoseDontCall(CI);
2821 return true;
2822 }
2823 return false;
2824 }
2825
2826 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
2827
2828 if (translateKnownIntrinsic(CI, ID, MIRBuilder))
2829 return true;
2830
2831 TargetLowering::IntrinsicInfo Info;
2832 bool IsTgtMemIntrinsic = TLI->getTgtMemIntrinsic(Info, CI, *MF, ID);
2833
2834 return translateIntrinsic(CI, ID, MIRBuilder,
2835 IsTgtMemIntrinsic ? &Info : nullptr);
2836}
2837
2838/// Translate a call to an intrinsic.
2839/// Depending on whether TLI->getTgtMemIntrinsic() is true, TgtMemIntrinsicInfo
2840/// is a pointer to the correspondingly populated IntrinsicInfo object.
2841/// Otherwise, this pointer is null.
2842bool IRTranslator::translateIntrinsic(
2843 const CallBase &CB, Intrinsic::ID ID, MachineIRBuilder &MIRBuilder,
2844 const TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
2845 ArrayRef<Register> ResultRegs;
2846 if (!CB.getType()->isVoidTy())
2847 ResultRegs = getOrCreateVRegs(CB);
2848
2849 // Ignore the callsite attributes. Backend code is most likely not expecting
2850 // an intrinsic to sometimes have side effects and sometimes not.
2851 MachineInstrBuilder MIB = MIRBuilder.buildIntrinsic(ID, ResultRegs);
2852 if (isa<FPMathOperator>(CB))
2853 MIB->copyIRFlags(CB);
2854
2855 for (const auto &Arg : enumerate(CB.args())) {
2856 // If this is required to be an immediate, don't materialize it in a
2857 // register.
2858 if (CB.paramHasAttr(Arg.index(), Attribute::ImmArg)) {
2859 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg.value())) {
2860 // imm arguments are more convenient than cimm (and realistically
2861 // probably sufficient), so use them.
2862 assert(CI->getBitWidth() <= 64 &&
2863 "large intrinsic immediates not handled");
2864 MIB.addImm(CI->getSExtValue());
2865 } else {
2866 MIB.addFPImm(cast<ConstantFP>(Arg.value()));
2867 }
2868 } else if (auto *MDVal = dyn_cast<MetadataAsValue>(Arg.value())) {
2869 auto *MD = MDVal->getMetadata();
2870 auto *MDN = dyn_cast<MDNode>(MD);
2871 if (!MDN) {
2872 if (auto *ConstMD = dyn_cast<ConstantAsMetadata>(MD))
2873 MDN = MDNode::get(MF->getFunction().getContext(), ConstMD);
2874 else // This was probably an MDString.
2875 return false;
2876 }
2877 MIB.addMetadata(MDN);
2878 } else {
2879 ArrayRef<Register> VRegs = getOrCreateVRegs(*Arg.value());
2880 if (VRegs.size() > 1)
2881 return false;
2882 MIB.addUse(VRegs[0]);
2883 }
2884 }
2885
2886 // Add a MachineMemOperand if it is a target mem intrinsic.
2887 if (TgtMemIntrinsicInfo) {
2888 const Function *F = CB.getCalledFunction();
2889
2890 Align Alignment = TgtMemIntrinsicInfo->align.value_or(DL->getABITypeAlign(
2891 TgtMemIntrinsicInfo->memVT.getTypeForEVT(F->getContext())));
2892 LLT MemTy =
2893 TgtMemIntrinsicInfo->memVT.isSimple()
2894 ? getLLTForMVT(TgtMemIntrinsicInfo->memVT.getSimpleVT())
2895 : LLT::scalar(TgtMemIntrinsicInfo->memVT.getStoreSizeInBits());
2896
2897 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
2898 // didn't yield anything useful.
2899 MachinePointerInfo MPI;
2900 if (TgtMemIntrinsicInfo->ptrVal) {
2901 MPI = MachinePointerInfo(TgtMemIntrinsicInfo->ptrVal,
2902 TgtMemIntrinsicInfo->offset);
2903 } else if (TgtMemIntrinsicInfo->fallbackAddressSpace) {
2904 MPI = MachinePointerInfo(*TgtMemIntrinsicInfo->fallbackAddressSpace);
2905 }
2906 MIB.addMemOperand(MF->getMachineMemOperand(
2907 MPI, TgtMemIntrinsicInfo->flags, MemTy, Alignment, CB.getAAMetadata(),
2908 /*Ranges=*/nullptr, TgtMemIntrinsicInfo->ssid,
2909 TgtMemIntrinsicInfo->order, TgtMemIntrinsicInfo->failureOrder));
2910 }
2911
2912 if (CB.isConvergent()) {
2913 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
2914 auto *Token = Bundle->Inputs[0].get();
2915 Register TokenReg = getOrCreateVReg(*Token);
2916 MIB.addUse(TokenReg, RegState::Implicit);
2917 }
2918 }
2919
2920 return true;
2921}
2922
2923bool IRTranslator::findUnwindDestinations(
2924 const BasicBlock *EHPadBB,
2925 BranchProbability Prob,
2926 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2927 &UnwindDests) {
2929 EHPadBB->getParent()->getFunction().getPersonalityFn());
2930 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2931 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2932 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2933 bool IsSEH = isAsynchronousEHPersonality(Personality);
2934
2935 if (IsWasmCXX) {
2936 // Ignore this for now.
2937 return false;
2938 }
2939
2940 while (EHPadBB) {
2942 BasicBlock *NewEHPadBB = nullptr;
2943 if (isa<LandingPadInst>(Pad)) {
2944 // Stop on landingpads. They are not funclets.
2945 UnwindDests.emplace_back(&getMBB(*EHPadBB), Prob);
2946 break;
2947 }
2948 if (isa<CleanupPadInst>(Pad)) {
2949 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2950 // personalities.
2951 UnwindDests.emplace_back(&getMBB(*EHPadBB), Prob);
2952 UnwindDests.back().first->setIsEHScopeEntry();
2953 UnwindDests.back().first->setIsEHFuncletEntry();
2954 break;
2955 }
2956 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2957 // Add the catchpad handlers to the possible destinations.
2958 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2959 UnwindDests.emplace_back(&getMBB(*CatchPadBB), Prob);
2960 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2961 if (IsMSVCCXX || IsCoreCLR)
2962 UnwindDests.back().first->setIsEHFuncletEntry();
2963 if (!IsSEH)
2964 UnwindDests.back().first->setIsEHScopeEntry();
2965 }
2966 NewEHPadBB = CatchSwitch->getUnwindDest();
2967 } else {
2968 continue;
2969 }
2970
2971 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2972 if (BPI && NewEHPadBB)
2973 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2974 EHPadBB = NewEHPadBB;
2975 }
2976 return true;
2977}
2978
2979bool IRTranslator::translateInvoke(const User &U,
2980 MachineIRBuilder &MIRBuilder) {
2981 const InvokeInst &I = cast<InvokeInst>(U);
2982 MCContext &Context = MF->getContext();
2983
2984 const BasicBlock *ReturnBB = I.getSuccessor(0);
2985 const BasicBlock *EHPadBB = I.getSuccessor(1);
2986
2987 const Function *Fn = I.getCalledFunction();
2988
2989 // FIXME: support invoking patchpoint and statepoint intrinsics.
2990 if (Fn && Fn->isIntrinsic())
2991 return false;
2992
2993 // FIXME: support whatever these are.
2994 if (I.hasDeoptState())
2995 return false;
2996
2997 // FIXME: support control flow guard targets.
2998 if (I.countOperandBundlesOfType(LLVMContext::OB_cfguardtarget))
2999 return false;
3000
3001 // FIXME: support Windows exception handling.
3002 if (!isa<LandingPadInst>(EHPadBB->getFirstNonPHIIt()))
3003 return false;
3004
3005 // FIXME: support Windows dllimport function calls and calls through
3006 // weak symbols.
3007 if (Fn && (Fn->hasDLLImportStorageClass() ||
3008 (MF->getTarget().getTargetTriple().isOSWindows() &&
3009 Fn->hasExternalWeakLinkage())))
3010 return false;
3011
3012 bool LowerInlineAsm = I.isInlineAsm();
3013 bool NeedEHLabel = true;
3014
3015 // Emit the actual call, bracketed by EH_LABELs so that the MF knows about
3016 // the region covered by the try.
3017 MCSymbol *BeginSymbol = nullptr;
3018 if (NeedEHLabel) {
3019 MIRBuilder.buildInstr(TargetOpcode::G_INVOKE_REGION_START);
3020 BeginSymbol = Context.createTempSymbol();
3021 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
3022 }
3023
3024 if (LowerInlineAsm) {
3025 if (!translateInlineAsm(I, MIRBuilder))
3026 return false;
3027 } else if (!translateCallBase(I, MIRBuilder))
3028 return false;
3029
3030 MCSymbol *EndSymbol = nullptr;
3031 if (NeedEHLabel) {
3032 EndSymbol = Context.createTempSymbol();
3033 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
3034 }
3035
3037 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3038 MachineBasicBlock *InvokeMBB = &MIRBuilder.getMBB();
3039 BranchProbability EHPadBBProb =
3040 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3042
3043 if (!findUnwindDestinations(EHPadBB, EHPadBBProb, UnwindDests))
3044 return false;
3045
3046 MachineBasicBlock &EHPadMBB = getMBB(*EHPadBB),
3047 &ReturnMBB = getMBB(*ReturnBB);
3048 // Update successor info.
3049 addSuccessorWithProb(InvokeMBB, &ReturnMBB);
3050 for (auto &UnwindDest : UnwindDests) {
3051 UnwindDest.first->setIsEHPad();
3052 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3053 }
3054 InvokeMBB->normalizeSuccProbs();
3055
3056 if (NeedEHLabel) {
3057 assert(BeginSymbol && "Expected a begin symbol!");
3058 assert(EndSymbol && "Expected an end symbol!");
3059 MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
3060 }
3061
3062 MIRBuilder.buildBr(ReturnMBB);
3063 return true;
3064}
3065
3066bool IRTranslator::translateCallBr(const User &U,
3067 MachineIRBuilder &MIRBuilder) {
3068 // FIXME: Implement this.
3069 return false;
3070}
3071
3072bool IRTranslator::translateLandingPad(const User &U,
3073 MachineIRBuilder &MIRBuilder) {
3074 const LandingPadInst &LP = cast<LandingPadInst>(U);
3075
3076 MachineBasicBlock &MBB = MIRBuilder.getMBB();
3077
3078 MBB.setIsEHPad();
3079
3080 // If there aren't registers to copy the values into (e.g., during SjLj
3081 // exceptions), then don't bother.
3082 const Constant *PersonalityFn = MF->getFunction().getPersonalityFn();
3083 if (TLI->getExceptionPointerRegister(PersonalityFn) == 0 &&
3084 TLI->getExceptionSelectorRegister(PersonalityFn) == 0)
3085 return true;
3086
3087 // If landingpad's return type is token type, we don't create DAG nodes
3088 // for its exception pointer and selector value. The extraction of exception
3089 // pointer or selector value from token type landingpads is not currently
3090 // supported.
3091 if (LP.getType()->isTokenTy())
3092 return true;
3093
3094 // Add a label to mark the beginning of the landing pad. Deletion of the
3095 // landing pad can thus be detected via the MachineModuleInfo.
3096 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL)
3097 .addSym(MF->addLandingPad(&MBB));
3098
3099 // If the unwinder does not preserve all registers, ensure that the
3100 // function marks the clobbered registers as used.
3101 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
3102 if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF))
3103 MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
3104
3105 LLT Ty = getLLTForType(*LP.getType(), *DL);
3106 Register Undef = MRI->createGenericVirtualRegister(Ty);
3107 MIRBuilder.buildUndef(Undef);
3108
3110 for (Type *Ty : cast<StructType>(LP.getType())->elements())
3111 Tys.push_back(getLLTForType(*Ty, *DL));
3112 assert(Tys.size() == 2 && "Only two-valued landingpads are supported");
3113
3114 // Mark exception register as live in.
3115 Register ExceptionReg = TLI->getExceptionPointerRegister(PersonalityFn);
3116 if (!ExceptionReg)
3117 return false;
3118
3119 MBB.addLiveIn(ExceptionReg);
3120 ArrayRef<Register> ResRegs = getOrCreateVRegs(LP);
3121 MIRBuilder.buildCopy(ResRegs[0], ExceptionReg);
3122
3123 Register SelectorReg = TLI->getExceptionSelectorRegister(PersonalityFn);
3124 if (!SelectorReg)
3125 return false;
3126
3127 MBB.addLiveIn(SelectorReg);
3128 Register PtrVReg = MRI->createGenericVirtualRegister(Tys[0]);
3129 MIRBuilder.buildCopy(PtrVReg, SelectorReg);
3130 MIRBuilder.buildCast(ResRegs[1], PtrVReg);
3131
3132 return true;
3133}
3134
3135bool IRTranslator::translateAlloca(const User &U,
3136 MachineIRBuilder &MIRBuilder) {
3137 auto &AI = cast<AllocaInst>(U);
3138
3139 if (AI.isSwiftError())
3140 return true;
3141
3142 if (AI.isStaticAlloca()) {
3143 Register Res = getOrCreateVReg(AI);
3144 int FI = getOrCreateFrameIndex(AI);
3145 MIRBuilder.buildFrameIndex(Res, FI);
3146 return true;
3147 }
3148
3149 // FIXME: support stack probing for Windows.
3150 if (MF->getTarget().getTargetTriple().isOSWindows())
3151 return false;
3152
3153 // Now we're in the harder dynamic case.
3154 Register NumElts = getOrCreateVReg(*AI.getArraySize());
3155 Type *IntPtrIRTy = DL->getIntPtrType(AI.getType());
3156 LLT IntPtrTy = getLLTForType(*IntPtrIRTy, *DL);
3157 if (MRI->getType(NumElts) != IntPtrTy) {
3158 Register ExtElts = MRI->createGenericVirtualRegister(IntPtrTy);
3159 MIRBuilder.buildZExtOrTrunc(ExtElts, NumElts);
3160 NumElts = ExtElts;
3161 }
3162
3163 Type *Ty = AI.getAllocatedType();
3164
3165 Register AllocSize = MRI->createGenericVirtualRegister(IntPtrTy);
3166 Register TySize =
3167 getOrCreateVReg(*ConstantInt::get(IntPtrIRTy, DL->getTypeAllocSize(Ty)));
3168 MIRBuilder.buildMul(AllocSize, NumElts, TySize);
3169
3170 // Round the size of the allocation up to the stack alignment size
3171 // by add SA-1 to the size. This doesn't overflow because we're computing
3172 // an address inside an alloca.
3173 Align StackAlign = MF->getSubtarget().getFrameLowering()->getStackAlign();
3174 auto SAMinusOne = MIRBuilder.buildConstant(IntPtrTy, StackAlign.value() - 1);
3175 auto AllocAdd = MIRBuilder.buildAdd(IntPtrTy, AllocSize, SAMinusOne,
3177 auto AlignCst =
3178 MIRBuilder.buildConstant(IntPtrTy, ~(uint64_t)(StackAlign.value() - 1));
3179 auto AlignedAlloc = MIRBuilder.buildAnd(IntPtrTy, AllocAdd, AlignCst);
3180
3181 Align Alignment = std::max(AI.getAlign(), DL->getPrefTypeAlign(Ty));
3182 if (Alignment <= StackAlign)
3183 Alignment = Align(1);
3184 MIRBuilder.buildDynStackAlloc(getOrCreateVReg(AI), AlignedAlloc, Alignment);
3185
3186 MF->getFrameInfo().CreateVariableSizedObject(Alignment, &AI);
3187 assert(MF->getFrameInfo().hasVarSizedObjects());
3188 return true;
3189}
3190
3191bool IRTranslator::translateVAArg(const User &U, MachineIRBuilder &MIRBuilder) {
3192 // FIXME: We may need more info about the type. Because of how LLT works,
3193 // we're completely discarding the i64/double distinction here (amongst
3194 // others). Fortunately the ABIs I know of where that matters don't use va_arg
3195 // anyway but that's not guaranteed.
3196 MIRBuilder.buildInstr(TargetOpcode::G_VAARG, {getOrCreateVReg(U)},
3197 {getOrCreateVReg(*U.getOperand(0)),
3198 DL->getABITypeAlign(U.getType()).value()});
3199 return true;
3200}
3201
3202bool IRTranslator::translateUnreachable(const User &U,
3203 MachineIRBuilder &MIRBuilder) {
3204 auto &UI = cast<UnreachableInst>(U);
3205 if (!UI.shouldLowerToTrap(MF->getTarget().Options.TrapUnreachable,
3206 MF->getTarget().Options.NoTrapAfterNoreturn))
3207 return true;
3208
3209 MIRBuilder.buildTrap();
3210 return true;
3211}
3212
3213bool IRTranslator::translateInsertElement(const User &U,
3214 MachineIRBuilder &MIRBuilder) {
3215 // If it is a <1 x Ty> vector, use the scalar as it is
3216 // not a legal vector type in LLT.
3217 if (auto *FVT = dyn_cast<FixedVectorType>(U.getType());
3218 FVT && FVT->getNumElements() == 1)
3219 return translateCopy(U, *U.getOperand(1), MIRBuilder);
3220
3221 Register Res = getOrCreateVReg(U);
3222 Register Val = getOrCreateVReg(*U.getOperand(0));
3223 Register Elt = getOrCreateVReg(*U.getOperand(1));
3224 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
3225 Register Idx;
3226 if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(2))) {
3227 if (CI->getBitWidth() != PreferredVecIdxWidth) {
3228 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
3229 auto *NewIdxCI = ConstantInt::get(CI->getContext(), NewIdx);
3230 Idx = getOrCreateVReg(*NewIdxCI);
3231 }
3232 }
3233 if (!Idx)
3234 Idx = getOrCreateVReg(*U.getOperand(2));
3235 if (MRI->getType(Idx).getSizeInBits() != PreferredVecIdxWidth) {
3236 const LLT VecIdxTy = LLT::scalar(PreferredVecIdxWidth);
3237 Idx = MIRBuilder.buildZExtOrTrunc(VecIdxTy, Idx).getReg(0);
3238 }
3239 MIRBuilder.buildInsertVectorElement(Res, Val, Elt, Idx);
3240 return true;
3241}
3242
3243bool IRTranslator::translateInsertVector(const User &U,
3244 MachineIRBuilder &MIRBuilder) {
3245 Register Dst = getOrCreateVReg(U);
3246 Register Vec = getOrCreateVReg(*U.getOperand(0));
3247 Register Elt = getOrCreateVReg(*U.getOperand(1));
3248
3249 ConstantInt *CI = cast<ConstantInt>(U.getOperand(2));
3250 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
3251
3252 // Resize Index to preferred index width.
3253 if (CI->getBitWidth() != PreferredVecIdxWidth) {
3254 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
3255 CI = ConstantInt::get(CI->getContext(), NewIdx);
3256 }
3257
3258 // If it is a <1 x Ty> vector, we have to use other means.
3259 if (auto *ResultType = dyn_cast<FixedVectorType>(U.getOperand(1)->getType());
3260 ResultType && ResultType->getNumElements() == 1) {
3261 if (auto *InputType = dyn_cast<FixedVectorType>(U.getOperand(0)->getType());
3262 InputType && InputType->getNumElements() == 1) {
3263 // We are inserting an illegal fixed vector into an illegal
3264 // fixed vector, use the scalar as it is not a legal vector type
3265 // in LLT.
3266 return translateCopy(U, *U.getOperand(0), MIRBuilder);
3267 }
3268 if (isa<FixedVectorType>(U.getOperand(0)->getType())) {
3269 // We are inserting an illegal fixed vector into a legal fixed
3270 // vector, use the scalar as it is not a legal vector type in
3271 // LLT.
3272 Register Idx = getOrCreateVReg(*CI);
3273 MIRBuilder.buildInsertVectorElement(Dst, Vec, Elt, Idx);
3274 return true;
3275 }
3276 if (isa<ScalableVectorType>(U.getOperand(0)->getType())) {
3277 // We are inserting an illegal fixed vector into a scalable
3278 // vector, use a scalar element insert.
3279 LLT VecIdxTy = LLT::scalar(PreferredVecIdxWidth);
3280 Register Idx = getOrCreateVReg(*CI);
3281 auto ScaledIndex = MIRBuilder.buildMul(
3282 VecIdxTy, MIRBuilder.buildVScale(VecIdxTy, 1), Idx);
3283 MIRBuilder.buildInsertVectorElement(Dst, Vec, Elt, ScaledIndex);
3284 return true;
3285 }
3286 }
3287
3288 MIRBuilder.buildInsertSubvector(
3289 getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
3290 getOrCreateVReg(*U.getOperand(1)), CI->getZExtValue());
3291 return true;
3292}
3293
3294bool IRTranslator::translateExtractElement(const User &U,
3295 MachineIRBuilder &MIRBuilder) {
3296 // If it is a <1 x Ty> vector, use the scalar as it is
3297 // not a legal vector type in LLT.
3298 if (const FixedVectorType *FVT =
3299 dyn_cast<FixedVectorType>(U.getOperand(0)->getType()))
3300 if (FVT->getNumElements() == 1)
3301 return translateCopy(U, *U.getOperand(0), MIRBuilder);
3302
3303 Register Res = getOrCreateVReg(U);
3304 Register Val = getOrCreateVReg(*U.getOperand(0));
3305 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
3306 Register Idx;
3307 if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(1))) {
3308 if (CI->getBitWidth() != PreferredVecIdxWidth) {
3309 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
3310 auto *NewIdxCI = ConstantInt::get(CI->getContext(), NewIdx);
3311 Idx = getOrCreateVReg(*NewIdxCI);
3312 }
3313 }
3314 if (!Idx)
3315 Idx = getOrCreateVReg(*U.getOperand(1));
3316 if (MRI->getType(Idx).getSizeInBits() != PreferredVecIdxWidth) {
3317 const LLT VecIdxTy = LLT::scalar(PreferredVecIdxWidth);
3318 Idx = MIRBuilder.buildZExtOrTrunc(VecIdxTy, Idx).getReg(0);
3319 }
3320 MIRBuilder.buildExtractVectorElement(Res, Val, Idx);
3321 return true;
3322}
3323
3324bool IRTranslator::translateExtractVector(const User &U,
3325 MachineIRBuilder &MIRBuilder) {
3326 Register Res = getOrCreateVReg(U);
3327 Register Vec = getOrCreateVReg(*U.getOperand(0));
3328 ConstantInt *CI = cast<ConstantInt>(U.getOperand(1));
3329 unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
3330
3331 // Resize Index to preferred index width.
3332 if (CI->getBitWidth() != PreferredVecIdxWidth) {
3333 APInt NewIdx = CI->getValue().zextOrTrunc(PreferredVecIdxWidth);
3334 CI = ConstantInt::get(CI->getContext(), NewIdx);
3335 }
3336
3337 // If it is a <1 x Ty> vector, we have to use other means.
3338 if (auto *ResultType = dyn_cast<FixedVectorType>(U.getType());
3339 ResultType && ResultType->getNumElements() == 1) {
3340 if (auto *InputType = dyn_cast<FixedVectorType>(U.getOperand(0)->getType());
3341 InputType && InputType->getNumElements() == 1) {
3342 // We are extracting an illegal fixed vector from an illegal fixed vector,
3343 // use the scalar as it is not a legal vector type in LLT.
3344 return translateCopy(U, *U.getOperand(0), MIRBuilder);
3345 }
3346 if (isa<FixedVectorType>(U.getOperand(0)->getType())) {
3347 // We are extracting an illegal fixed vector from a legal fixed
3348 // vector, use the scalar as it is not a legal vector type in
3349 // LLT.
3350 Register Idx = getOrCreateVReg(*CI);
3351 MIRBuilder.buildExtractVectorElement(Res, Vec, Idx);
3352 return true;
3353 }
3354 if (isa<ScalableVectorType>(U.getOperand(0)->getType())) {
3355 // We are extracting an illegal fixed vector from a scalable
3356 // vector, use a scalar element extract.
3357 LLT VecIdxTy = LLT::scalar(PreferredVecIdxWidth);
3358 Register Idx = getOrCreateVReg(*CI);
3359 auto ScaledIndex = MIRBuilder.buildMul(
3360 VecIdxTy, MIRBuilder.buildVScale(VecIdxTy, 1), Idx);
3361 MIRBuilder.buildExtractVectorElement(Res, Vec, ScaledIndex);
3362 return true;
3363 }
3364 }
3365
3366 MIRBuilder.buildExtractSubvector(getOrCreateVReg(U),
3367 getOrCreateVReg(*U.getOperand(0)),
3368 CI->getZExtValue());
3369 return true;
3370}
3371
3372bool IRTranslator::translateShuffleVector(const User &U,
3373 MachineIRBuilder &MIRBuilder) {
3374 // A ShuffleVector that operates on scalable vectors is a splat vector where
3375 // the value of the splat vector is the 0th element of the first operand,
3376 // since the index mask operand is the zeroinitializer (undef and
3377 // poison are treated as zeroinitializer here).
3378 if (U.getOperand(0)->getType()->isScalableTy()) {
3379 Register Val = getOrCreateVReg(*U.getOperand(0));
3380 auto SplatVal = MIRBuilder.buildExtractVectorElementConstant(
3381 MRI->getType(Val).getElementType(), Val, 0);
3382 MIRBuilder.buildSplatVector(getOrCreateVReg(U), SplatVal);
3383 return true;
3384 }
3385
3386 ArrayRef<int> Mask;
3387 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&U))
3388 Mask = SVI->getShuffleMask();
3389 else
3390 Mask = cast<ConstantExpr>(U).getShuffleMask();
3391
3392 // As GISel does not represent <1 x > vectors as a separate type from scalars,
3393 // we transform shuffle_vector with a scalar output to an
3394 // ExtractVectorElement. If the input type is also scalar it becomes a Copy.
3395 unsigned DstElts = cast<FixedVectorType>(U.getType())->getNumElements();
3396 unsigned SrcElts =
3397 cast<FixedVectorType>(U.getOperand(0)->getType())->getNumElements();
3398 if (DstElts == 1) {
3399 unsigned M = Mask[0];
3400 if (SrcElts == 1) {
3401 if (M == 0 || M == 1)
3402 return translateCopy(U, *U.getOperand(M), MIRBuilder);
3403 MIRBuilder.buildUndef(getOrCreateVReg(U));
3404 } else {
3405 Register Dst = getOrCreateVReg(U);
3406 if (M < SrcElts) {
3408 Dst, getOrCreateVReg(*U.getOperand(0)), M);
3409 } else if (M < SrcElts * 2) {
3411 Dst, getOrCreateVReg(*U.getOperand(1)), M - SrcElts);
3412 } else {
3413 MIRBuilder.buildUndef(Dst);
3414 }
3415 }
3416 return true;
3417 }
3418
3419 // A single element src is transformed to a build_vector.
3420 if (SrcElts == 1) {
3423 for (int M : Mask) {
3424 LLT SrcTy = getLLTForType(*U.getOperand(0)->getType(), *DL);
3425 if (M == 0 || M == 1) {
3426 Ops.push_back(getOrCreateVReg(*U.getOperand(M)));
3427 } else {
3428 if (!Undef.isValid()) {
3429 Undef = MRI->createGenericVirtualRegister(SrcTy);
3430 MIRBuilder.buildUndef(Undef);
3431 }
3432 Ops.push_back(Undef);
3433 }
3434 }
3435 MIRBuilder.buildBuildVector(getOrCreateVReg(U), Ops);
3436 return true;
3437 }
3438
3439 ArrayRef<int> MaskAlloc = MF->allocateShuffleMask(Mask);
3440 MIRBuilder
3441 .buildInstr(TargetOpcode::G_SHUFFLE_VECTOR, {getOrCreateVReg(U)},
3442 {getOrCreateVReg(*U.getOperand(0)),
3443 getOrCreateVReg(*U.getOperand(1))})
3444 .addShuffleMask(MaskAlloc);
3445 return true;
3446}
3447
3448bool IRTranslator::translatePHI(const User &U, MachineIRBuilder &MIRBuilder) {
3449 const PHINode &PI = cast<PHINode>(U);
3450
3451 SmallVector<MachineInstr *, 4> Insts;
3452 for (auto Reg : getOrCreateVRegs(PI)) {
3453 auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_PHI, {Reg}, {});
3454 Insts.push_back(MIB.getInstr());
3455 }
3456
3457 PendingPHIs.emplace_back(&PI, std::move(Insts));
3458 return true;
3459}
3460
3461bool IRTranslator::translateAtomicCmpXchg(const User &U,
3462 MachineIRBuilder &MIRBuilder) {
3463 const AtomicCmpXchgInst &I = cast<AtomicCmpXchgInst>(U);
3464
3465 auto Flags = TLI->getAtomicMemOperandFlags(I, *DL);
3466
3467 auto Res = getOrCreateVRegs(I);
3468 Register OldValRes = Res[0];
3469 Register SuccessRes = Res[1];
3470 Register Addr = getOrCreateVReg(*I.getPointerOperand());
3471 Register Cmp = getOrCreateVReg(*I.getCompareOperand());
3472 Register NewVal = getOrCreateVReg(*I.getNewValOperand());
3473
3475 OldValRes, SuccessRes, Addr, Cmp, NewVal,
3476 *MF->getMachineMemOperand(
3477 MachinePointerInfo(I.getPointerOperand()), Flags, MRI->getType(Cmp),
3478 getMemOpAlign(I), I.getAAMetadata(), nullptr, I.getSyncScopeID(),
3479 I.getSuccessOrdering(), I.getFailureOrdering()));
3480 return true;
3481}
3482
3483bool IRTranslator::translateAtomicRMW(const User &U,
3484 MachineIRBuilder &MIRBuilder) {
3485 if (containsBF16Type(U))
3486 return false;
3487
3488 const AtomicRMWInst &I = cast<AtomicRMWInst>(U);
3489 auto Flags = TLI->getAtomicMemOperandFlags(I, *DL);
3490
3491 Register Res = getOrCreateVReg(I);
3492 Register Addr = getOrCreateVReg(*I.getPointerOperand());
3493 Register Val = getOrCreateVReg(*I.getValOperand());
3494
3495 unsigned Opcode = 0;
3496 switch (I.getOperation()) {
3497 default:
3498 return false;
3500 Opcode = TargetOpcode::G_ATOMICRMW_XCHG;
3501 break;
3502 case AtomicRMWInst::Add:
3503 Opcode = TargetOpcode::G_ATOMICRMW_ADD;
3504 break;
3505 case AtomicRMWInst::Sub:
3506 Opcode = TargetOpcode::G_ATOMICRMW_SUB;
3507 break;
3508 case AtomicRMWInst::And:
3509 Opcode = TargetOpcode::G_ATOMICRMW_AND;
3510 break;
3512 Opcode = TargetOpcode::G_ATOMICRMW_NAND;
3513 break;
3514 case AtomicRMWInst::Or:
3515 Opcode = TargetOpcode::G_ATOMICRMW_OR;
3516 break;
3517 case AtomicRMWInst::Xor:
3518 Opcode = TargetOpcode::G_ATOMICRMW_XOR;
3519 break;
3520 case AtomicRMWInst::Max:
3521 Opcode = TargetOpcode::G_ATOMICRMW_MAX;
3522 break;
3523 case AtomicRMWInst::Min:
3524 Opcode = TargetOpcode::G_ATOMICRMW_MIN;
3525 break;
3527 Opcode = TargetOpcode::G_ATOMICRMW_UMAX;
3528 break;
3530 Opcode = TargetOpcode::G_ATOMICRMW_UMIN;
3531 break;
3533 Opcode = TargetOpcode::G_ATOMICRMW_FADD;
3534 break;
3536 Opcode = TargetOpcode::G_ATOMICRMW_FSUB;
3537 break;
3539 Opcode = TargetOpcode::G_ATOMICRMW_FMAX;
3540 break;
3542 Opcode = TargetOpcode::G_ATOMICRMW_FMIN;
3543 break;
3545 Opcode = TargetOpcode::G_ATOMICRMW_FMAXIMUM;
3546 break;
3548 Opcode = TargetOpcode::G_ATOMICRMW_FMINIMUM;
3549 break;
3551 Opcode = TargetOpcode::G_ATOMICRMW_UINC_WRAP;
3552 break;
3554 Opcode = TargetOpcode::G_ATOMICRMW_UDEC_WRAP;
3555 break;
3557 Opcode = TargetOpcode::G_ATOMICRMW_USUB_COND;
3558 break;
3560 Opcode = TargetOpcode::G_ATOMICRMW_USUB_SAT;
3561 break;
3562 }
3563
3564 MIRBuilder.buildAtomicRMW(
3565 Opcode, Res, Addr, Val,
3566 *MF->getMachineMemOperand(MachinePointerInfo(I.getPointerOperand()),
3567 Flags, MRI->getType(Val), getMemOpAlign(I),
3568 I.getAAMetadata(), nullptr, I.getSyncScopeID(),
3569 I.getOrdering()));
3570 return true;
3571}
3572
3573bool IRTranslator::translateFence(const User &U,
3574 MachineIRBuilder &MIRBuilder) {
3575 const FenceInst &Fence = cast<FenceInst>(U);
3576 MIRBuilder.buildFence(static_cast<unsigned>(Fence.getOrdering()),
3577 Fence.getSyncScopeID());
3578 return true;
3579}
3580
3581bool IRTranslator::translateFreeze(const User &U,
3582 MachineIRBuilder &MIRBuilder) {
3583 const ArrayRef<Register> DstRegs = getOrCreateVRegs(U);
3584 const ArrayRef<Register> SrcRegs = getOrCreateVRegs(*U.getOperand(0));
3585
3586 assert(DstRegs.size() == SrcRegs.size() &&
3587 "Freeze with different source and destination type?");
3588
3589 for (unsigned I = 0; I < DstRegs.size(); ++I) {
3590 MIRBuilder.buildFreeze(DstRegs[I], SrcRegs[I]);
3591 }
3592
3593 return true;
3594}
3595
3596void IRTranslator::finishPendingPhis() {
3597#ifndef NDEBUG
3598 DILocationVerifier Verifier;
3599 GISelObserverWrapper WrapperObserver(&Verifier);
3600 RAIIMFObsDelInstaller ObsInstall(*MF, WrapperObserver);
3601#endif // ifndef NDEBUG
3602 for (auto &Phi : PendingPHIs) {
3603 const PHINode *PI = Phi.first;
3604 if (PI->getType()->isEmptyTy())
3605 continue;
3606 ArrayRef<MachineInstr *> ComponentPHIs = Phi.second;
3607 MachineBasicBlock *PhiMBB = ComponentPHIs[0]->getParent();
3608 EntryBuilder->setDebugLoc(PI->getDebugLoc());
3609#ifndef NDEBUG
3610 Verifier.setCurrentInst(PI);
3611#endif // ifndef NDEBUG
3612
3613 SmallPtrSet<const MachineBasicBlock *, 16> SeenPreds;
3614 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
3615 auto IRPred = PI->getIncomingBlock(i);
3616 ArrayRef<Register> ValRegs = getOrCreateVRegs(*PI->getIncomingValue(i));
3617 for (auto *Pred : getMachinePredBBs({IRPred, PI->getParent()})) {
3618 if (SeenPreds.count(Pred) || !PhiMBB->isPredecessor(Pred))
3619 continue;
3620 SeenPreds.insert(Pred);
3621 for (unsigned j = 0; j < ValRegs.size(); ++j) {
3622 MachineInstrBuilder MIB(*MF, ComponentPHIs[j]);
3623 MIB.addUse(ValRegs[j]);
3624 MIB.addMBB(Pred);
3625 }
3626 }
3627 }
3628 }
3629}
3630
3631void IRTranslator::translateDbgValueRecord(Value *V, bool HasArgList,
3632 const DILocalVariable *Variable,
3633 const DIExpression *Expression,
3634 const DebugLoc &DL,
3635 MachineIRBuilder &MIRBuilder) {
3636 assert(Variable->isValidLocationForIntrinsic(DL) &&
3637 "Expected inlined-at fields to agree");
3638 // Act as if we're handling a debug intrinsic.
3639 MIRBuilder.setDebugLoc(DL);
3640
3641 if (!V || HasArgList) {
3642 // DI cannot produce a valid DBG_VALUE, so produce an undef DBG_VALUE to
3643 // terminate any prior location.
3644 MIRBuilder.buildIndirectDbgValue(0, Variable, Expression);
3645 return;
3646 }
3647
3648 if (const auto *CI = dyn_cast<Constant>(V)) {
3649 MIRBuilder.buildConstDbgValue(*CI, Variable, Expression);
3650 return;
3651 }
3652
3653 if (auto *AI = dyn_cast<AllocaInst>(V);
3654 AI && AI->isStaticAlloca() && Expression->startsWithDeref()) {
3655 // If the value is an alloca and the expression starts with a
3656 // dereference, track a stack slot instead of a register, as registers
3657 // may be clobbered.
3658 auto ExprOperands = Expression->getElements();
3659 auto *ExprDerefRemoved =
3660 DIExpression::get(AI->getContext(), ExprOperands.drop_front());
3661 MIRBuilder.buildFIDbgValue(getOrCreateFrameIndex(*AI), Variable,
3662 ExprDerefRemoved);
3663 return;
3664 }
3665 if (translateIfEntryValueArgument(false, V, Variable, Expression, DL,
3666 MIRBuilder))
3667 return;
3668 for (Register Reg : getOrCreateVRegs(*V)) {
3669 // FIXME: This does not handle register-indirect values at offset 0. The
3670 // direct/indirect thing shouldn't really be handled by something as
3671 // implicit as reg+noreg vs reg+imm in the first place, but it seems
3672 // pretty baked in right now.
3673 MIRBuilder.buildDirectDbgValue(Reg, Variable, Expression);
3674 }
3675}
3676
3677void IRTranslator::translateDbgDeclareRecord(Value *Address, bool HasArgList,
3678 const DILocalVariable *Variable,
3679 const DIExpression *Expression,
3680 const DebugLoc &DL,
3681 MachineIRBuilder &MIRBuilder) {
3682 if (!Address || isa<UndefValue>(Address)) {
3683 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *Variable << "\n");
3684 return;
3685 }
3686
3687 assert(Variable->isValidLocationForIntrinsic(DL) &&
3688 "Expected inlined-at fields to agree");
3689 auto AI = dyn_cast<AllocaInst>(Address);
3690 if (AI && AI->isStaticAlloca()) {
3691 // Static allocas are tracked at the MF level, no need for DBG_VALUE
3692 // instructions (in fact, they get ignored if they *do* exist).
3693 MF->setVariableDbgInfo(Variable, Expression,
3694 getOrCreateFrameIndex(*AI), DL);
3695 return;
3696 }
3697
3698 if (translateIfEntryValueArgument(true, Address, Variable,
3699 Expression, DL,
3700 MIRBuilder))
3701 return;
3702
3703 // A dbg.declare describes the address of a source variable, so lower it
3704 // into an indirect DBG_VALUE.
3705 MIRBuilder.setDebugLoc(DL);
3706 MIRBuilder.buildIndirectDbgValue(getOrCreateVReg(*Address), Variable,
3707 Expression);
3708}
3709
3710void IRTranslator::translateDbgInfo(const Instruction &Inst,
3711 MachineIRBuilder &MIRBuilder) {
3712 for (DbgRecord &DR : Inst.getDbgRecordRange()) {
3713 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
3714 MIRBuilder.setDebugLoc(DLR->getDebugLoc());
3715 assert(DLR->getLabel() && "Missing label");
3716 assert(DLR->getLabel()->isValidLocationForIntrinsic(
3717 MIRBuilder.getDebugLoc()) &&
3718 "Expected inlined-at fields to agree");
3719 MIRBuilder.buildDbgLabel(DLR->getLabel());
3720 continue;
3721 }
3722 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
3723 const DILocalVariable *Variable = DVR.getVariable();
3724 const DIExpression *Expression = DVR.getExpression();
3725 Value *V = DVR.getVariableLocationOp(0);
3726 if (DVR.isDbgDeclare())
3727 translateDbgDeclareRecord(V, DVR.hasArgList(), Variable, Expression,
3728 DVR.getDebugLoc(), MIRBuilder);
3729 else
3730 translateDbgValueRecord(V, DVR.hasArgList(), Variable, Expression,
3731 DVR.getDebugLoc(), MIRBuilder);
3732 }
3733}
3734
3735bool IRTranslator::translate(const Instruction &Inst) {
3736 CurBuilder->setDebugLoc(Inst.getDebugLoc());
3737 CurBuilder->setPCSections(Inst.getMetadata(LLVMContext::MD_pcsections));
3738 CurBuilder->setMMRAMetadata(Inst.getMetadata(LLVMContext::MD_mmra));
3739
3740 if (TLI->fallBackToDAGISel(Inst))
3741 return false;
3742
3743 switch (Inst.getOpcode()) {
3744#define HANDLE_INST(NUM, OPCODE, CLASS) \
3745 case Instruction::OPCODE: \
3746 return translate##OPCODE(Inst, *CurBuilder.get());
3747#include "llvm/IR/Instruction.def"
3748 default:
3749 return false;
3750 }
3751}
3752
3753bool IRTranslator::translate(const Constant &C, Register Reg) {
3754 // We only emit constants into the entry block from here. To prevent jumpy
3755 // debug behaviour remove debug line.
3756 if (auto CurrInstDL = CurBuilder->getDL())
3757 EntryBuilder->setDebugLoc(DebugLoc());
3758
3759 if (auto CI = dyn_cast<ConstantInt>(&C)) {
3760 // buildConstant expects a to-be-splatted scalar ConstantInt.
3761 if (isa<VectorType>(CI->getType()))
3762 CI = ConstantInt::get(CI->getContext(), CI->getValue());
3763 EntryBuilder->buildConstant(Reg, *CI);
3764 } else if (auto CF = dyn_cast<ConstantFP>(&C)) {
3765 // buildFConstant expects a to-be-splatted scalar ConstantFP.
3766 if (isa<VectorType>(CF->getType()))
3767 CF = ConstantFP::get(CF->getContext(), CF->getValue());
3768 EntryBuilder->buildFConstant(Reg, *CF);
3769 } else if (isa<UndefValue>(C))
3770 EntryBuilder->buildUndef(Reg);
3771 else if (isa<ConstantPointerNull>(C))
3772 EntryBuilder->buildConstant(Reg, 0);
3773 else if (auto GV = dyn_cast<GlobalValue>(&C))
3774 EntryBuilder->buildGlobalValue(Reg, GV);
3775 else if (auto CPA = dyn_cast<ConstantPtrAuth>(&C)) {
3776 Register Addr = getOrCreateVReg(*CPA->getPointer());
3777 Register AddrDisc = getOrCreateVReg(*CPA->getAddrDiscriminator());
3778 EntryBuilder->buildConstantPtrAuth(Reg, CPA, Addr, AddrDisc);
3779 } else if (auto CAZ = dyn_cast<ConstantAggregateZero>(&C)) {
3780 Constant &Elt = *CAZ->getElementValue(0u);
3781 if (isa<ScalableVectorType>(CAZ->getType())) {
3782 EntryBuilder->buildSplatVector(Reg, getOrCreateVReg(Elt));
3783 return true;
3784 }
3785 // Return the scalar if it is a <1 x Ty> vector.
3786 unsigned NumElts = CAZ->getElementCount().getFixedValue();
3787 if (NumElts == 1)
3788 return translateCopy(C, Elt, *EntryBuilder);
3789 // All elements are zero so we can just use the first one.
3790 EntryBuilder->buildSplatBuildVector(Reg, getOrCreateVReg(Elt));
3791 } else if (auto CV = dyn_cast<ConstantDataVector>(&C)) {
3792 // Return the scalar if it is a <1 x Ty> vector.
3793 if (CV->getNumElements() == 1)
3794 return translateCopy(C, *CV->getElementAsConstant(0), *EntryBuilder);
3796 for (unsigned i = 0; i < CV->getNumElements(); ++i) {
3797 Constant &Elt = *CV->getElementAsConstant(i);
3798 Ops.push_back(getOrCreateVReg(Elt));
3799 }
3800 EntryBuilder->buildBuildVector(Reg, Ops);
3801 } else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
3802 switch(CE->getOpcode()) {
3803#define HANDLE_INST(NUM, OPCODE, CLASS) \
3804 case Instruction::OPCODE: \
3805 return translate##OPCODE(*CE, *EntryBuilder.get());
3806#include "llvm/IR/Instruction.def"
3807 default:
3808 return false;
3809 }
3810 } else if (auto CV = dyn_cast<ConstantVector>(&C)) {
3811 if (CV->getNumOperands() == 1)
3812 return translateCopy(C, *CV->getOperand(0), *EntryBuilder);
3814 for (unsigned i = 0; i < CV->getNumOperands(); ++i) {
3815 Ops.push_back(getOrCreateVReg(*CV->getOperand(i)));
3816 }
3817 EntryBuilder->buildBuildVector(Reg, Ops);
3818 } else if (auto *BA = dyn_cast<BlockAddress>(&C)) {
3819 EntryBuilder->buildBlockAddress(Reg, BA);
3820 } else
3821 return false;
3822
3823 return true;
3824}
3825
3826bool IRTranslator::finalizeBasicBlock(const BasicBlock &BB,
3828 for (auto &BTB : SL->BitTestCases) {
3829 // Emit header first, if it wasn't already emitted.
3830 if (!BTB.Emitted)
3831 emitBitTestHeader(BTB, BTB.Parent);
3832
3833 BranchProbability UnhandledProb = BTB.Prob;
3834 for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) {
3835 UnhandledProb -= BTB.Cases[j].ExtraProb;
3836 // Set the current basic block to the mbb we wish to insert the code into
3837 MachineBasicBlock *MBB = BTB.Cases[j].ThisBB;
3838 // If all cases cover a contiguous range, it is not necessary to jump to
3839 // the default block after the last bit test fails. This is because the
3840 // range check during bit test header creation has guaranteed that every
3841 // case here doesn't go outside the range. In this case, there is no need
3842 // to perform the last bit test, as it will always be true. Instead, make
3843 // the second-to-last bit-test fall through to the target of the last bit
3844 // test, and delete the last bit test.
3845
3846 MachineBasicBlock *NextMBB;
3847 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
3848 // Second-to-last bit-test with contiguous range: fall through to the
3849 // target of the final bit test.
3850 NextMBB = BTB.Cases[j + 1].TargetBB;
3851 } else if (j + 1 == ej) {
3852 // For the last bit test, fall through to Default.
3853 NextMBB = BTB.Default;
3854 } else {
3855 // Otherwise, fall through to the next bit test.
3856 NextMBB = BTB.Cases[j + 1].ThisBB;
3857 }
3858
3859 emitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j], MBB);
3860
3861 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
3862 // We need to record the replacement phi edge here that normally
3863 // happens in emitBitTestCase before we delete the case, otherwise the
3864 // phi edge will be lost.
3865 addMachineCFGPred({BTB.Parent->getBasicBlock(),
3866 BTB.Cases[ej - 1].TargetBB->getBasicBlock()},
3867 MBB);
3868 // Since we're not going to use the final bit test, remove it.
3869 BTB.Cases.pop_back();
3870 break;
3871 }
3872 }
3873 // This is "default" BB. We have two jumps to it. From "header" BB and from
3874 // last "case" BB, unless the latter was skipped.
3875 CFGEdge HeaderToDefaultEdge = {BTB.Parent->getBasicBlock(),
3876 BTB.Default->getBasicBlock()};
3877 addMachineCFGPred(HeaderToDefaultEdge, BTB.Parent);
3878 if (!BTB.ContiguousRange) {
3879 addMachineCFGPred(HeaderToDefaultEdge, BTB.Cases.back().ThisBB);
3880 }
3881 }
3882 SL->BitTestCases.clear();
3883
3884 for (auto &JTCase : SL->JTCases) {
3885 // Emit header first, if it wasn't already emitted.
3886 if (!JTCase.first.Emitted)
3887 emitJumpTableHeader(JTCase.second, JTCase.first, JTCase.first.HeaderBB);
3888
3889 emitJumpTable(JTCase.second, JTCase.second.MBB);
3890 }
3891 SL->JTCases.clear();
3892
3893 for (auto &SwCase : SL->SwitchCases)
3894 emitSwitchCase(SwCase, &CurBuilder->getMBB(), *CurBuilder);
3895 SL->SwitchCases.clear();
3896
3897 // Check if we need to generate stack-protector guard checks.
3898 StackProtector &SP = getAnalysis<StackProtector>();
3899 if (SP.shouldEmitSDCheck(BB)) {
3900 bool FunctionBasedInstrumentation =
3901 TLI->getSSPStackGuardCheck(*MF->getFunction().getParent());
3902 SPDescriptor.initialize(&BB, &MBB, FunctionBasedInstrumentation);
3903 }
3904 // Handle stack protector.
3905 if (SPDescriptor.shouldEmitFunctionBasedCheckStackProtector()) {
3906 LLVM_DEBUG(dbgs() << "Unimplemented stack protector case\n");
3907 return false;
3908 } else if (SPDescriptor.shouldEmitStackProtector()) {
3909 MachineBasicBlock *ParentMBB = SPDescriptor.getParentMBB();
3910 MachineBasicBlock *SuccessMBB = SPDescriptor.getSuccessMBB();
3911
3912 // Find the split point to split the parent mbb. At the same time copy all
3913 // physical registers used in the tail of parent mbb into virtual registers
3914 // before the split point and back into physical registers after the split
3915 // point. This prevents us needing to deal with Live-ins and many other
3916 // register allocation issues caused by us splitting the parent mbb. The
3917 // register allocator will clean up said virtual copies later on.
3919 ParentMBB, *MF->getSubtarget().getInstrInfo());
3920
3921 // Splice the terminator of ParentMBB into SuccessMBB.
3922 SuccessMBB->splice(SuccessMBB->end(), ParentMBB, SplitPoint,
3923 ParentMBB->end());
3924
3925 // Add compare/jump on neq/jump to the parent BB.
3926 if (!emitSPDescriptorParent(SPDescriptor, ParentMBB))
3927 return false;
3928
3929 // CodeGen Failure MBB if we have not codegened it yet.
3930 MachineBasicBlock *FailureMBB = SPDescriptor.getFailureMBB();
3931 if (FailureMBB->empty()) {
3932 if (!emitSPDescriptorFailure(SPDescriptor, FailureMBB))
3933 return false;
3934 }
3935
3936 // Clear the Per-BB State.
3937 SPDescriptor.resetPerBBState();
3938 }
3939 return true;
3940}
3941
3942bool IRTranslator::emitSPDescriptorParent(StackProtectorDescriptor &SPD,
3943 MachineBasicBlock *ParentBB) {
3944 CurBuilder->setInsertPt(*ParentBB, ParentBB->end());
3945 // First create the loads to the guard/stack slot for the comparison.
3946 Type *PtrIRTy = PointerType::getUnqual(MF->getFunction().getContext());
3947 const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
3948 LLT PtrMemTy = getLLTForMVT(TLI->getPointerMemTy(*DL));
3949
3950 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3951 int FI = MFI.getStackProtectorIndex();
3952
3953 Register Guard;
3954 Register StackSlotPtr = CurBuilder->buildFrameIndex(PtrTy, FI).getReg(0);
3955 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3956 Align Align = DL->getPrefTypeAlign(PointerType::getUnqual(M.getContext()));
3957
3958 // Generate code to load the content of the guard slot.
3959 Register GuardVal =
3960 CurBuilder
3961 ->buildLoad(PtrMemTy, StackSlotPtr,
3962 MachinePointerInfo::getFixedStack(*MF, FI), Align,
3964 .getReg(0);
3965
3966 if (TLI->useStackGuardXorFP()) {
3967 LLVM_DEBUG(dbgs() << "Stack protector xor'ing with FP not yet implemented");
3968 return false;
3969 }
3970
3971 // Retrieve guard check function, nullptr if instrumentation is inlined.
3972 if (const Function *GuardCheckFn = TLI->getSSPStackGuardCheck(M)) {
3973 // This path is currently untestable on GlobalISel, since the only platform
3974 // that needs this seems to be Windows, and we fall back on that currently.
3975 // The code still lives here in case that changes.
3976 // Silence warning about unused variable until the code below that uses
3977 // 'GuardCheckFn' is enabled.
3978 (void)GuardCheckFn;
3979 return false;
3980#if 0
3981 // The target provides a guard check function to validate the guard value.
3982 // Generate a call to that function with the content of the guard slot as
3983 // argument.
3984 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3985 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3986 ISD::ArgFlagsTy Flags;
3987 if (GuardCheckFn->hasAttribute(1, Attribute::AttrKind::InReg))
3988 Flags.setInReg();
3989 CallLowering::ArgInfo GuardArgInfo(
3990 {GuardVal, FnTy->getParamType(0), {Flags}});
3991
3992 CallLowering::CallLoweringInfo Info;
3993 Info.OrigArgs.push_back(GuardArgInfo);
3994 Info.CallConv = GuardCheckFn->getCallingConv();
3995 Info.Callee = MachineOperand::CreateGA(GuardCheckFn, 0);
3996 Info.OrigRet = {Register(), FnTy->getReturnType()};
3997 if (!CLI->lowerCall(MIRBuilder, Info)) {
3998 LLVM_DEBUG(dbgs() << "Failed to lower call to stack protector check\n");
3999 return false;
4000 }
4001 return true;
4002#endif
4003 }
4004
4005 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
4006 // Otherwise, emit a volatile load to retrieve the stack guard value.
4007 if (TLI->useLoadStackGuardNode(*ParentBB->getBasicBlock()->getModule())) {
4008 Guard =
4009 MRI->createGenericVirtualRegister(LLT::scalar(PtrTy.getSizeInBits()));
4010 getStackGuard(Guard, *CurBuilder);
4011 } else {
4012 // TODO: test using android subtarget when we support @llvm.thread.pointer.
4013 const Value *IRGuard = TLI->getSDagStackGuard(M);
4014 Register GuardPtr = getOrCreateVReg(*IRGuard);
4015
4016 Guard = CurBuilder
4017 ->buildLoad(PtrMemTy, GuardPtr,
4018 MachinePointerInfo::getFixedStack(*MF, FI), Align,
4021 .getReg(0);
4022 }
4023
4024 // Perform the comparison.
4025 auto Cmp =
4026 CurBuilder->buildICmp(CmpInst::ICMP_NE, LLT::scalar(1), Guard, GuardVal);
4027 // If the guard/stackslot do not equal, branch to failure MBB.
4028 CurBuilder->buildBrCond(Cmp, *SPD.getFailureMBB());
4029 // Otherwise branch to success MBB.
4030 CurBuilder->buildBr(*SPD.getSuccessMBB());
4031 return true;
4032}
4033
4034bool IRTranslator::emitSPDescriptorFailure(StackProtectorDescriptor &SPD,
4035 MachineBasicBlock *FailureBB) {
4036 CurBuilder->setInsertPt(*FailureBB, FailureBB->end());
4037
4038 const RTLIB::Libcall Libcall = RTLIB::STACKPROTECTOR_CHECK_FAIL;
4039 const char *Name = TLI->getLibcallName(Libcall);
4040
4041 CallLowering::CallLoweringInfo Info;
4042 Info.CallConv = TLI->getLibcallCallingConv(Libcall);
4043 Info.Callee = MachineOperand::CreateES(Name);
4044 Info.OrigRet = {Register(), Type::getVoidTy(MF->getFunction().getContext()),
4045 0};
4046 if (!CLI->lowerCall(*CurBuilder, Info)) {
4047 LLVM_DEBUG(dbgs() << "Failed to lower call to stack protector fail\n");
4048 return false;
4049 }
4050
4051 // Emit a trap instruction if we are required to do so.
4052 const TargetOptions &TargetOpts = TLI->getTargetMachine().Options;
4053 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
4054 CurBuilder->buildInstr(TargetOpcode::G_TRAP);
4055
4056 return true;
4057}
4058
4059void IRTranslator::finalizeFunction() {
4060 // Release the memory used by the different maps we
4061 // needed during the translation.
4062 PendingPHIs.clear();
4063 VMap.reset();
4064 FrameIndices.clear();
4065 MachinePreds.clear();
4066 // MachineIRBuilder::DebugLoc can outlive the DILocation it holds. Clear it
4067 // to avoid accessing free’d memory (in runOnMachineFunction) and to avoid
4068 // destroying it twice (in ~IRTranslator() and ~LLVMContext())
4069 EntryBuilder.reset();
4070 CurBuilder.reset();
4071 FuncInfo.clear();
4072 SPDescriptor.resetPerFunctionState();
4073}
4074
4075/// Returns true if a BasicBlock \p BB within a variadic function contains a
4076/// variadic musttail call.
4077static bool checkForMustTailInVarArgFn(bool IsVarArg, const BasicBlock &BB) {
4078 if (!IsVarArg)
4079 return false;
4080
4081 // Walk the block backwards, because tail calls usually only appear at the end
4082 // of a block.
4083 return llvm::any_of(llvm::reverse(BB), [](const Instruction &I) {
4084 const auto *CI = dyn_cast<CallInst>(&I);
4085 return CI && CI->isMustTailCall();
4086 });
4087}
4088
4090 MF = &CurMF;
4091 const Function &F = MF->getFunction();
4094 // Set the CSEConfig and run the analysis.
4095 GISelCSEInfo *CSEInfo = nullptr;
4097 bool EnableCSE = EnableCSEInIRTranslator.getNumOccurrences()
4099 : TPC->isGISelCSEEnabled();
4100 TLI = MF->getSubtarget().getTargetLowering();
4101
4102 if (EnableCSE) {
4103 EntryBuilder = std::make_unique<CSEMIRBuilder>(CurMF);
4104 CSEInfo = &Wrapper.get(TPC->getCSEConfig());
4105 EntryBuilder->setCSEInfo(CSEInfo);
4106 CurBuilder = std::make_unique<CSEMIRBuilder>(CurMF);
4107 CurBuilder->setCSEInfo(CSEInfo);
4108 } else {
4109 EntryBuilder = std::make_unique<MachineIRBuilder>();
4110 CurBuilder = std::make_unique<MachineIRBuilder>();
4111 }
4112 CLI = MF->getSubtarget().getCallLowering();
4113 CurBuilder->setMF(*MF);
4114 EntryBuilder->setMF(*MF);
4115 MRI = &MF->getRegInfo();
4116 DL = &F.getDataLayout();
4117 ORE = std::make_unique<OptimizationRemarkEmitter>(&F);
4118 const TargetMachine &TM = MF->getTarget();
4119 TM.resetTargetOptions(F);
4120 EnableOpts = OptLevel != CodeGenOptLevel::None && !skipFunction(F);
4121 FuncInfo.MF = MF;
4122 if (EnableOpts) {
4123 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
4124 FuncInfo.BPI = &getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
4125 } else {
4126 AA = nullptr;
4127 FuncInfo.BPI = nullptr;
4128 }
4129
4130 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
4131 MF->getFunction());
4132 LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
4133 FuncInfo.CanLowerReturn = CLI->checkReturnTypeForCallConv(*MF);
4134
4135 SL = std::make_unique<GISelSwitchLowering>(this, FuncInfo);
4136 SL->init(*TLI, TM, *DL);
4137
4138 assert(PendingPHIs.empty() && "stale PHIs");
4139
4140 // Targets which want to use big endian can enable it using
4141 // enableBigEndian()
4142 if (!DL->isLittleEndian() && !CLI->enableBigEndian()) {
4143 // Currently we don't properly handle big endian code.
4144 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
4145 F.getSubprogram(), &F.getEntryBlock());
4146 R << "unable to translate in big endian mode";
4147 reportTranslationError(*MF, *TPC, *ORE, R);
4148 return false;
4149 }
4150
4151 // Release the per-function state when we return, whether we succeeded or not.
4152 auto FinalizeOnReturn = make_scope_exit([this]() { finalizeFunction(); });
4153
4154 // Setup a separate basic-block for the arguments and constants
4155 MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
4156 MF->push_back(EntryBB);
4157 EntryBuilder->setMBB(*EntryBB);
4158
4159 DebugLoc DbgLoc = F.getEntryBlock().getFirstNonPHIIt()->getDebugLoc();
4160 SwiftError.setFunction(CurMF);
4161 SwiftError.createEntriesInEntryBlock(DbgLoc);
4162
4163 bool IsVarArg = F.isVarArg();
4164 bool HasMustTailInVarArgFn = false;
4165
4166 // Create all blocks, in IR order, to preserve the layout.
4167 FuncInfo.MBBMap.resize(F.getMaxBlockNumber());
4168 for (const BasicBlock &BB: F) {
4169 auto *&MBB = FuncInfo.MBBMap[BB.getNumber()];
4170
4171 MBB = MF->CreateMachineBasicBlock(&BB);
4172 MF->push_back(MBB);
4173
4174 if (BB.hasAddressTaken())
4175 MBB->setAddressTakenIRBlock(const_cast<BasicBlock *>(&BB));
4176
4177 if (!HasMustTailInVarArgFn)
4178 HasMustTailInVarArgFn = checkForMustTailInVarArgFn(IsVarArg, BB);
4179 }
4180
4181 MF->getFrameInfo().setHasMustTailInVarArgFunc(HasMustTailInVarArgFn);
4182
4183 // Make our arguments/constants entry block fallthrough to the IR entry block.
4184 EntryBB->addSuccessor(&getMBB(F.front()));
4185
4186 if (CLI->fallBackToDAGISel(*MF)) {
4187 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
4188 F.getSubprogram(), &F.getEntryBlock());
4189 R << "unable to lower function: "
4190 << ore::NV("Prototype", F.getFunctionType());
4191 reportTranslationError(*MF, *TPC, *ORE, R);
4192 return false;
4193 }
4194
4195 // Lower the actual args into this basic block.
4196 SmallVector<ArrayRef<Register>, 8> VRegArgs;
4197 for (const Argument &Arg: F.args()) {
4198 if (DL->getTypeStoreSize(Arg.getType()).isZero())
4199 continue; // Don't handle zero sized types.
4200 ArrayRef<Register> VRegs = getOrCreateVRegs(Arg);
4201 VRegArgs.push_back(VRegs);
4202
4203 if (Arg.hasSwiftErrorAttr()) {
4204 assert(VRegs.size() == 1 && "Too many vregs for Swift error");
4205 SwiftError.setCurrentVReg(EntryBB, SwiftError.getFunctionArg(), VRegs[0]);
4206 }
4207 }
4208
4209 if (!CLI->lowerFormalArguments(*EntryBuilder, F, VRegArgs, FuncInfo)) {
4210 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
4211 F.getSubprogram(), &F.getEntryBlock());
4212 R << "unable to lower arguments: "
4213 << ore::NV("Prototype", F.getFunctionType());
4214 reportTranslationError(*MF, *TPC, *ORE, R);
4215 return false;
4216 }
4217
4218 // Need to visit defs before uses when translating instructions.
4219 GISelObserverWrapper WrapperObserver;
4220 if (EnableCSE && CSEInfo)
4221 WrapperObserver.addObserver(CSEInfo);
4222 {
4224#ifndef NDEBUG
4225 DILocationVerifier Verifier;
4226 WrapperObserver.addObserver(&Verifier);
4227#endif // ifndef NDEBUG
4228 RAIIMFObsDelInstaller ObsInstall(*MF, WrapperObserver);
4229 for (const BasicBlock *BB : RPOT) {
4230 MachineBasicBlock &MBB = getMBB(*BB);
4231 // Set the insertion point of all the following translations to
4232 // the end of this basic block.
4233 CurBuilder->setMBB(MBB);
4234 HasTailCall = false;
4235 for (const Instruction &Inst : *BB) {
4236 // If we translated a tail call in the last step, then we know
4237 // everything after the call is either a return, or something that is
4238 // handled by the call itself. (E.g. a lifetime marker or assume
4239 // intrinsic.) In this case, we should stop translating the block and
4240 // move on.
4241 if (HasTailCall)
4242 break;
4243#ifndef NDEBUG
4244 Verifier.setCurrentInst(&Inst);
4245#endif // ifndef NDEBUG
4246
4247 // Translate any debug-info attached to the instruction.
4248 translateDbgInfo(Inst, *CurBuilder);
4249
4250 if (translate(Inst))
4251 continue;
4252
4253 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
4254 Inst.getDebugLoc(), BB);
4255 R << "unable to translate instruction: " << ore::NV("Opcode", &Inst);
4256
4257 if (ORE->allowExtraAnalysis("gisel-irtranslator")) {
4258 std::string InstStrStorage;
4259 raw_string_ostream InstStr(InstStrStorage);
4260 InstStr << Inst;
4261
4262 R << ": '" << InstStrStorage << "'";
4263 }
4264
4265 reportTranslationError(*MF, *TPC, *ORE, R);
4266 return false;
4267 }
4268
4269 if (!finalizeBasicBlock(*BB, MBB)) {
4270 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
4271 BB->getTerminator()->getDebugLoc(), BB);
4272 R << "unable to translate basic block";
4273 reportTranslationError(*MF, *TPC, *ORE, R);
4274 return false;
4275 }
4276 }
4277#ifndef NDEBUG
4278 WrapperObserver.removeObserver(&Verifier);
4279#endif
4280 }
4281
4282 finishPendingPhis();
4283
4284 SwiftError.propagateVRegs();
4285
4286 // Merge the argument lowering and constants block with its single
4287 // successor, the LLVM-IR entry block. We want the basic block to
4288 // be maximal.
4289 assert(EntryBB->succ_size() == 1 &&
4290 "Custom BB used for lowering should have only one successor");
4291 // Get the successor of the current entry block.
4292 MachineBasicBlock &NewEntryBB = **EntryBB->succ_begin();
4293 assert(NewEntryBB.pred_size() == 1 &&
4294 "LLVM-IR entry block has a predecessor!?");
4295 // Move all the instruction from the current entry block to the
4296 // new entry block.
4297 NewEntryBB.splice(NewEntryBB.begin(), EntryBB, EntryBB->begin(),
4298 EntryBB->end());
4299
4300 // Update the live-in information for the new entry block.
4301 for (const MachineBasicBlock::RegisterMaskPair &LiveIn : EntryBB->liveins())
4302 NewEntryBB.addLiveIn(LiveIn);
4303 NewEntryBB.sortUniqueLiveIns();
4304
4305 // Get rid of the now empty basic block.
4306 EntryBB->removeSuccessor(&NewEntryBB);
4307 MF->remove(EntryBB);
4308 MF->deleteMachineBasicBlock(EntryBB);
4309
4310 assert(&MF->front() == &NewEntryBB &&
4311 "New entry wasn't next in the list of basic block!");
4312
4313 // Initialize stack protector information.
4315 SP.copyToMachineFrameInfo(MF->getFrameInfo());
4316
4317 return false;
4318}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
Provides analysis for continuously CSEing during GISel passes.
This file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
This file describes how to lower LLVM calls to machine code calls.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
This contains common code to allow clients to notify changes to machine instr.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR static false void reportTranslationError(MachineFunction &MF, const TargetPassConfig &TPC, OptimizationRemarkEmitter &ORE, OptimizationRemarkMissed &R)
static bool checkForMustTailInVarArgFn(bool IsVarArg, const BasicBlock &BB)
Returns true if a BasicBlock BB within a variadic function contains a variadic musttail call.
static bool targetSupportsBF16Type(const MachineFunction *MF)
static bool containsBF16Type(const User &U)
static unsigned getConvOpcode(Intrinsic::ID ID)
static uint64_t getOffsetFromIndices(const User &U, const DataLayout &DL)
static unsigned getConstrainedOpcode(Intrinsic::ID ID)
IRTranslator LLVM IR MI
static cl::opt< bool > EnableCSEInIRTranslator("enable-cse-in-irtranslator", cl::desc("Should enable CSE in irtranslator"), cl::Optional, cl::init(false))
static bool isValInBlock(const Value *V, const BasicBlock *BB)
static bool isSwiftError(const Value *V)
This file declares the IRTranslator pass.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This file describes how to lower LLVM inline asm to machine code INLINEASM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Implement a low-level type suitable for MachineInstr level instruction selection.
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
This file declares the MachineIRBuilder class.
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
uint64_t High
OptimizedStructLayoutField Field
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
verify safepoint Safepoint IR Verifier
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file describes how to lower LLVM code to machine code.
Target-Independent Code Generator Pass Configuration Options pass.
Value * RHS
Value * LHS
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1033
an instruction to allocate memory on the stack
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
LLVM_ABI bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
PointerType * getType() const
Overload to return most specific pointer type.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
const Value * getArraySize() const
Get the number of elements allocated.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:132
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
iterator begin() const
Definition ArrayRef.h:131
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:138
An immutable pass that tracks lazily created AssumptionCache objects.
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
unsigned getNumber() const
Definition BasicBlock.h:95
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:690
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Legacy analysis pass which computes BlockFrequencyInfo.
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Legacy analysis pass which computes BranchProbabilityInfo.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
bool isInlineAsm() const
Check if this call is an inline asm statement.
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
bool isFPPredicate() const
Definition InstrTypes.h:782
bool isIntPredicate() const
Definition InstrTypes.h:783
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:157
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static LLVM_ABI DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)
Append the opcodes Ops to DIExpr.
LLVM_ABI bool startsWithDeref() const
Return whether the first element a DW_OP_deref.
ArrayRef< uint64_t > getElements() const
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this label.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Value * getAddress() const
DILabel * getLabel() const
DebugLoc getDebugLoc() const
Value * getValue(unsigned OpIdx=0) const
DILocalVariable * getVariable() const
DIExpression * getExpression() const
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
A debug info location.
Definition DebugLoc.h:124
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this fence instruction.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this fence instruction.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:803
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:188
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
Constant * getPersonalityFn() const
Get the personality function associated with this function.
const Function & getFunction() const
Definition Function.h:164
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:249
The actual analysis pass wrapper.
Definition CSEInfo.h:229
Simple wrapper that does the following.
Definition CSEInfo.h:211
The CSE Analysis object.
Definition CSEInfo.h:71
Abstract class that contains various methods for clients to notify about changes.
Simple wrapper observer that takes several observers, and calls each one for each event.
void removeObserver(GISelChangeObserver *O)
void addObserver(GISelChangeObserver *O)
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasExternalWeakLinkage() const
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
bool isTailCall(const MachineInstr &MI) const override
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
IRTranslator(CodeGenOptLevel OptLevel=CodeGenOptLevel::None)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool lowerInlineAsm(MachineIRBuilder &MIRBuilder, const CallBase &CB, std::function< ArrayRef< Register >(const Value &Val)> GetOrCreateVRegs) const
Lower the given inline asm call instruction GetOrCreateVRegs is a callback to materialize a register ...
This instruction inserts a struct field of array element value into an aggregate value.
iterator_range< simple_ilist< DbgRecord >::iterator > getDbgRecordRange() const
Return a range over the DbgRecords attached to this instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI bool hasAllowReassoc() const LLVM_READONLY
Determine whether the allow-reassociation flag is set.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
constexpr LLT changeElementType(LLT NewEltTy) const
If this type is a vector, return a vector with the same number of elements but the new element type.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr bool isPointer() const
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Value * getPointerOperand()
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
static LocationSize precise(uint64_t Value)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
void push_back(MachineInstr *MI)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
LLVM_ABI void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
LLVM_ABI bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHPad(bool V=true)
Indicates the block is a landing pad.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Helper class to build MachineInstr.
MachineInstrBuilder buildFPTOUI_SAT(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_FPTOUI_SAT Src0.
MachineInstrBuilder buildFMul(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildFreeze(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_FREEZE Src.
MachineInstrBuilder buildBr(MachineBasicBlock &Dest)
Build and insert G_BR Dest.
MachineInstrBuilder buildModf(const DstOp &Fract, const DstOp &Int, const SrcOp &Src, std::optional< unsigned > Flags=std::nullopt)
Build and insert Fract, Int = G_FMODF Src.
LLVMContext & getContext() const
MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ADD Op0, Op1.
MachineInstrBuilder buildUndef(const DstOp &Res)
Build and insert Res = IMPLICIT_DEF.
MachineInstrBuilder buildResetFPMode()
Build and insert G_RESET_FPMODE.
MachineInstrBuilder buildFPExt(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FPEXT Op.
MachineInstrBuilder buildFPTOSI_SAT(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_FPTOSI_SAT Src0.
MachineInstrBuilder buildUCmp(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1)
Build and insert a Res = G_UCMP Op0, Op1.
MachineInstrBuilder buildJumpTable(const LLT PtrTy, unsigned JTI)
Build and insert Res = G_JUMP_TABLE JTI.
MachineInstrBuilder buildGetRounding(const DstOp &Dst)
Build and insert Dst = G_GET_ROUNDING.
MachineInstrBuilder buildSCmp(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1)
Build and insert a Res = G_SCMP Op0, Op1.
MachineInstrBuilder buildFence(unsigned Ordering, unsigned Scope)
Build and insert G_FENCE Ordering, Scope.
MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_SELECT Tst, Op0, Op1.
MachineInstrBuilder buildFMA(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, const SrcOp &Src2, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FMA Op0, Op1, Op2.
MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_MUL Op0, Op1.
MachineInstrBuilder buildInsertSubvector(const DstOp &Res, const SrcOp &Src0, const SrcOp &Src1, unsigned Index)
Build and insert Res = G_INSERT_SUBVECTOR Src0, Src1, Idx.
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
MachineInstrBuilder buildCast(const DstOp &Dst, const SrcOp &Src)
Build and insert an appropriate cast between two registers of equal size.
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder buildSExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_SEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildAtomicRMW(unsigned Opcode, const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val, MachineMemOperand &MMO)
Build and insert OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO.
MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_SUB Op0, Op1.
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef< Register > Res, bool HasSideEffects, bool isConvergent)
Build and insert a G_INTRINSIC instruction.
MachineInstrBuilder buildVScale(const DstOp &Res, unsigned MinElts)
Build and insert Res = G_VSCALE MinElts.
MachineInstrBuilder buildSplatBuildVector(const DstOp &Res, const SrcOp &Src)
Build and insert Res = G_BUILD_VECTOR with Src replicated to fill the number of elements.
MachineInstrBuilder buildSetFPMode(const SrcOp &Src)
Build and insert G_SET_FPMODE Src.
MachineInstrBuilder buildIndirectDbgValue(Register Reg, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in me...
MachineInstrBuilder buildBuildVector(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_BUILD_VECTOR Op0, ...
MachineInstrBuilder buildConstDbgValue(const Constant &C, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instructions specifying that Variable is given by C (suitably modified b...
MachineInstrBuilder buildBrCond(const SrcOp &Tst, MachineBasicBlock &Dest)
Build and insert G_BRCOND Tst, Dest.
std::optional< MachineInstrBuilder > materializeObjectPtrOffset(Register &Res, Register Op0, const LLT ValueTy, uint64_t Value)
Materialize and insert an instruction with appropriate flags for addressing some offset of an object,...
MachineInstrBuilder buildSetRounding(const SrcOp &Src)
Build and insert G_SET_ROUNDING.
MachineInstrBuilder buildExtractVectorElement(const DstOp &Res, const SrcOp &Val, const SrcOp &Idx)
Build and insert Res = G_EXTRACT_VECTOR_ELT Val, Idx.
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_PTR_ADD Op0, Op1.
MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ZEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res, const SrcOp &Val, const int Idx)
Build and insert Res = G_EXTRACT_VECTOR_ELT Val, Idx.
MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
MachineInstrBuilder buildDirectDbgValue(Register Reg, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in Re...
MachineInstrBuilder buildDbgLabel(const MDNode *Label)
Build and insert a DBG_LABEL instructions specifying that Label is given.
MachineInstrBuilder buildBrJT(Register TablePtr, unsigned JTI, Register IndexReg)
Build and insert G_BRJT TablePtr, JTI, IndexReg.
MachineInstrBuilder buildDynStackAlloc(const DstOp &Res, const SrcOp &Size, Align Alignment)
Build and insert Res = G_DYN_STACKALLOC Size, Align.
MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in th...
MachineInstrBuilder buildResetFPEnv()
Build and insert G_RESET_FPENV.
void setDebugLoc(const DebugLoc &DL)
Set the debug location to DL for all the next build instructions.
const MachineBasicBlock & getMBB() const
Getter for the basic block we currently build.
MachineInstrBuilder buildInsertVectorElement(const DstOp &Res, const SrcOp &Val, const SrcOp &Elt, const SrcOp &Idx)
Build and insert Res = G_INSERT_VECTOR_ELT Val, Elt, Idx.
MachineInstrBuilder buildAtomicCmpXchgWithSuccess(const DstOp &OldValRes, const DstOp &SuccessRes, const SrcOp &Addr, const SrcOp &CmpVal, const SrcOp &NewVal, MachineMemOperand &MMO)
Build and insert OldValRes<def>, SuccessRes<def> = / G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr,...
void setMBB(MachineBasicBlock &MBB)
Set the insertion point to the end of MBB.
const DebugLoc & getDebugLoc()
Get the current instruction's debug location.
MachineInstrBuilder buildTrap(bool Debug=false)
Build and insert G_TRAP or G_DEBUGTRAP.
MachineInstrBuilder buildFFrexp(const DstOp &Fract, const DstOp &Exp, const SrcOp &Src, std::optional< unsigned > Flags=std::nullopt)
Build and insert Fract, Exp = G_FFREXP Src.
MachineInstrBuilder buildFPTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FPTRUNC Op.
MachineInstrBuilder buildFSincos(const DstOp &Sin, const DstOp &Cos, const SrcOp &Src, std::optional< unsigned > Flags=std::nullopt)
Build and insert Sin, Cos = G_FSINCOS Src.
MachineInstrBuilder buildShuffleVector(const DstOp &Res, const SrcOp &Src1, const SrcOp &Src2, ArrayRef< int > Mask)
Build and insert Res = G_SHUFFLE_VECTOR Src1, Src2, Mask.
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don't insert <empty> = Opcode <empty>.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
MachineInstrBuilder buildPrefetch(const SrcOp &Addr, unsigned RW, unsigned Locality, unsigned CacheType, MachineMemOperand &MMO)
Build and insert G_PREFETCH Addr, RW, Locality, CacheType.
MachineInstrBuilder buildExtractSubvector(const DstOp &Res, const SrcOp &Src, unsigned Index)
Build and insert Res = G_EXTRACT_SUBVECTOR Src, Idx0.
const DataLayout & getDataLayout() const
MachineInstrBuilder buildBrIndirect(Register Tgt)
Build and insert G_BRINDIRECT Tgt.
MachineInstrBuilder buildSplatVector(const DstOp &Res, const SrcOp &Val)
Build and insert Res = G_SPLAT_VECTOR Val.
MachineInstrBuilder buildStepVector(const DstOp &Res, unsigned Step)
Build and insert Res = G_STEP_VECTOR Step.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_FCMP PredOp0, Op1.
MachineInstrBuilder buildFAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FADD Op0, Op1.
MachineInstrBuilder buildSetFPEnv(const SrcOp &Src)
Build and insert G_SET_FPENV Src.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addFPImm(const ConstantFP *Val) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
LLVM_ABI void copyIRFlags(const Instruction &I)
Copy all flags to MachineInst MIFlags.
static LLVM_ABI uint32_t copyFlagsFromInstruction(const Instruction &I)
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateES(const char *SymName, unsigned TargetFlags=0)
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
The optimization diagnostic interface.
Diagnostic information for missed-optimization remarks.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Class to install both of the above.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Primary interface to the complete machine description for the target machine.
const Triple & getTargetTriple() const
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
Target-Independent Code Generator Pass Configuration Options.
bool isSPIRV() const
Tests whether the target is SPIR-V (32/64-bit/Logical).
Definition Triple.h:894
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:181
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:304
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
constexpr bool isZero() const
Definition TypeSize.h:154
const ParentTy * getParent() const
Definition ilist_node.h:34
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ Libcall
The operation should be implemented as a call to some kind of runtime support library.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Undef
Value of the register doesn't matter.
Offsets
Offsets in bytes from the start of the input buffer.
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:48
initializer< Ty > init(const Ty &Val)
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< CodeNode * > Code
Definition RDFGraph.h:388
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2472
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
LLVM_ABI MVT getMVTForLLT(LLT Ty)
Get a rough equivalent of an MVT for a given LLT.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
gep_type_iterator gep_type_end(const User *GEP)
MachineBasicBlock::iterator findSplitPointForStackProtector(MachineBasicBlock *BB, const TargetInstrInfo &TII)
Find the split point at which to splice the end of BB into its success stack protector check machine ...
LLVM_ABI LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
Align getKnownAlignment(Value *V, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to infer an alignment for the specified pointer.
Definition Local.h:252
constexpr bool has_single_bit(T Value) noexcept
Definition bit.h:147
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
void computeValueLLTs(const DataLayout &DL, Type &Ty, SmallVectorImpl< LLT > &ValueTys, SmallVectorImpl< uint64_t > *Offsets=nullptr, uint64_t StartingOffset=0)
computeValueLLTs - Given an LLVM IR type, compute a sequence of LLTs that represent all the individua...
Definition Analysis.cpp:149
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
generic_gep_type_iterator<> gep_type_iterator
auto succ_size(const MachineBasicBlock *BB)
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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
@ Success
The lock was released successfully.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Global
Append to llvm.global_dtors.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1184
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:1994
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ FMul
Product of floats.
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:185
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Pair of physical register and lane mask.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
static bool canHandle(const Instruction *I, const TargetLibraryInfo &TLI)
This structure is used to communicate between SelectionDAGBuilder and SDISel for the code generation ...
std::optional< unsigned > fallbackAddressSpace
PointerUnion< const Value *, const PseudoSourceValue * > ptrVal