48 #define DEBUG_TYPE "indirectbr-expand" 70 "Expand indirectbr instructions",
false,
false)
73 return new IndirectBrExpandPass();
78 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
83 auto &STI = *
TM.getSubtargetImpl(F);
84 if (!STI.enableIndirectBrExpand())
86 TLI = STI.getTargetLowering();
95 if (
auto *IBr = dyn_cast<IndirectBrInst>(BB.getTerminator())) {
98 if (IBr->getNumSuccessors() == 0) {
106 IndirectBrSuccs.
insert(SuccBB);
109 if (IndirectBrs.
empty())
121 if (!IndirectBrSuccs.
count(&BB))
124 auto IsBlockAddressUse = [&](
const Use &U) {
125 return isa<BlockAddress>(U.getUser());
127 auto BlockAddressUseIt =
llvm::find_if(BB.uses(), IsBlockAddressUse);
128 if (BlockAddressUseIt == BB.use_end())
132 IsBlockAddressUse) == BB.use_end() &&
133 "There should only ever be a single blockaddress use because it is " 134 "a constant and should be uniqued.");
136 auto *BA = cast<BlockAddress>(BlockAddressUseIt->getUser());
140 if (!BA->isConstantUsed())
145 int BBIndex = BBs.
size() + 1;
148 auto *ITy = cast<IntegerType>(DL.getIntPtrType(BA->getType()));
163 for (
auto *IBr : IndirectBrs) {
175 for (
auto *IBr : IndirectBrs) {
177 cast<IntegerType>(DL.getIntPtrType(IBr->getAddress()->getType()));
184 IBr->getAddress(), CommonITy,
185 Twine(IBr->getAddress()->getName()) +
".switch_cast", IBr);
188 if (IndirectBrs.size() == 1) {
192 SwitchValue = GetSwitchValue(IndirectBrs[0]);
193 IndirectBrs[0]->eraseFromParent();
200 "switch_value_phi", SwitchBB);
201 SwitchValue = SwitchPN;
205 for (
auto *IBr : IndirectBrs) {
206 SwitchPN->addIncoming(GetSwitchValue(IBr), IBr->getParent());
208 IBr->eraseFromParent();
217 for (
int i : llvm::seq<int>(1, BBs.
size()))
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
This routine provides some synthesis utilities to produce sequences of values.
void initializeIndirectBrExpandPassPass(PassRegistry &)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
void push_back(const T &Elt)
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
A Use represents the edge between a Value definition and its users.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static bool runOnFunction(Function &F, bool PostInlining)
LLVM Basic Block Representation.
This function has undefined behavior.
Indirect Branch Instruction.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
FunctionPass class - This class is used to implement most global optimizations.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Class to represent integer types.
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
FunctionPass * createIndirectBrExpandPass()
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, Instruction *InsertBefore=nullptr)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
INITIALIZE_PASS(IndirectBrExpandPass, DEBUG_TYPE, "Expand indirectbr instructions", false, false) FunctionPass *llvm
LLVM_NODISCARD bool empty() const
const Function * getParent() const
Return the enclosing method, or null if none.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
Primary interface to the complete machine description for the target machine.