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