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