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