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