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