48#define DEBUG_TYPE "indirectbr-expand"
72 auto *STI = TM->getSubtargetImpl(
F);
73 if (!STI->enableIndirectBrExpand())
76 auto *TLI = STI->getTargetLowering();
88char IndirectBrExpandLegacyPass::ID = 0;
91 "Expand indirectbr instructions",
false,
false)
97 return new IndirectBrExpandLegacyPass();
101 auto &
DL =
F.getDataLayout();
113 if (IBr->getNumSuccessors() == 0) {
123 if (IndirectBrs.
empty())
135 if (!IndirectBrSuccs.
count(&BB))
138 auto IsBlockAddressUse = [&](
const Use &U) {
141 auto BlockAddressUseIt =
llvm::find_if(BB.uses(), IsBlockAddressUse);
142 if (BlockAddressUseIt == BB.use_end())
145 assert(std::none_of(std::next(BlockAddressUseIt), BB.use_end(),
146 IsBlockAddressUse) &&
147 "There should only ever be a single blockaddress use because it is "
148 "a constant and should be uniqued.");
154 if (!BA->isConstantUsed())
159 int BBIndex = BBs.
size() + 1;
163 ConstantInt *BBIndexC = ConstantInt::get(ITy, BBIndex);
178 for (
auto *IBr : IndirectBrs) {
188 "Got unexpected update count.");
199 for (
auto *IBr : IndirectBrs) {
208 Twine(IBr->getAddress()->getName()) +
215 if (IndirectBrs.
size() == 1) {
220 SwitchValue = GetSwitchValue(IBr);
226 "Got unexpected update count.");
235 "switch_value_phi", SwitchBB);
236 SwitchValue = SwitchPN;
242 for (
auto *IBr : IndirectBrs) {
243 SwitchPN->addIncoming(GetSwitchValue(IBr), IBr->getParent());
250 IBr->eraseFromParent();
260 SI->addCase(ConstantInt::get(CommonITy, i + 1), BBs[i]);
268 if (UniqueSuccessors.
insert(BB).second)
277bool IndirectBrExpandLegacyPass::runOnFunction(
Function &
F) {
278 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
284 if (!STI.enableIndirectBrExpand())
286 auto *TLI = STI.getTargetLowering();
288 std::optional<DomTreeUpdater> DTU;
289 if (
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
290 DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
292 return runImpl(
F, TLI, DTU ? &*DTU :
nullptr);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
static bool runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
static bool runImpl(Function &F, const TargetLowering *TLI, DomTreeUpdater *DTU)
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Provides some synthesis utilities to produce sequences of values.
This file defines the SmallVector class.
Target-Independent Code Generator Pass Configuration Options pass.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
static LLVM_ABI CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
This is the shared class of boolean and integer constants.
Analysis pass which computes a DominatorTree.
static constexpr UpdateKind Delete
static constexpr UpdateKind Insert
Legacy analysis pass which computes a DominatorTree.
FunctionPass class - This class is used to implement most global optimizations.
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Indirect Branch Instruction.
iterator_range< succ_op_iterator > successors()
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Class to represent integer types.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, InsertPosition InsertBefore=nullptr)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Primary interface to the complete machine description for the target machine.
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const ParentTy * getParent() const
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI FunctionPass * createIndirectBrExpandPass()
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.