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