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