48#define DEBUG_TYPE "indirectbr-expand"
75 if (!STI->enableIndirectBrExpand())
78 auto *TLI = STI->getTargetLowering();
82 bool Changed =
runImpl(
F, TLI, DT ? &DTU :
nullptr);
90char IndirectBrExpandLegacyPass::ID = 0;
93 "Expand indirectbr instructions",
false,
false)
99 return new IndirectBrExpandLegacyPass();
103 auto &
DL =
F.getDataLayout();
112 if (
auto *IBr = dyn_cast<IndirectBrInst>(BB.getTerminator())) {
115 if (IBr->getNumSuccessors() == 0) {
123 IndirectBrSuccs.
insert(SuccBB);
126 if (IndirectBrs.
empty())
138 if (!IndirectBrSuccs.
count(&BB))
141 auto IsBlockAddressUse = [&](
const Use &U) {
142 return isa<BlockAddress>(U.getUser());
144 auto BlockAddressUseIt =
llvm::find_if(BB.uses(), IsBlockAddressUse);
145 if (BlockAddressUseIt == BB.use_end())
148 assert(std::find_if(std::next(BlockAddressUseIt), BB.use_end(),
149 IsBlockAddressUse) == BB.use_end() &&
150 "There should only ever be a single blockaddress use because it is "
151 "a constant and should be uniqued.");
153 auto *BA = cast<BlockAddress>(BlockAddressUseIt->getUser());
157 if (!BA->isConstantUsed())
162 int BBIndex = BBs.
size() + 1;
165 auto *ITy = cast<IntegerType>(
DL.getIntPtrType(BA->getType()));
166 ConstantInt *BBIndexC = ConstantInt::get(ITy, BBIndex);
181 for (
auto *IBr : IndirectBrs) {
191 "Got unexpected update count.");
202 for (
auto *IBr : IndirectBrs) {
204 cast<IntegerType>(
DL.getIntPtrType(IBr->getAddress()->getType()));
211 Twine(IBr->getAddress()->getName()) +
218 if (IndirectBrs.
size() == 1) {
223 SwitchValue = GetSwitchValue(IBr);
229 "Got unexpected update count.");
238 "switch_value_phi", SwitchBB);
239 SwitchValue = SwitchPN;
245 for (
auto *IBr : IndirectBrs) {
246 SwitchPN->addIncoming(GetSwitchValue(IBr), IBr->getParent());
253 IBr->eraseFromParent();
262 for (
int i : llvm::seq<int>(1, BBs.
size()))
263 SI->addCase(ConstantInt::get(CommonITy, i + 1), BBs[i]);
271 if (UniqueSuccessors.
insert(BB).second)
280bool IndirectBrExpandLegacyPass::runOnFunction(
Function &
F) {
281 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
286 auto &STI = *
TM.getSubtargetImpl(
F);
287 if (!STI.enableIndirectBrExpand())
289 auto *TLI = STI.getTargetLowering();
291 std::optional<DomTreeUpdater> DTU;
292 if (
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
293 DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
295 return runImpl(
F, TLI, DTU ? &*DTU :
nullptr);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runImpl(Function &F, const TargetLowering &TLI)
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Provides some synthesis utilities to produce sequences of values.
This file defines the SmallVector class.
Target-Independent Code Generator Pass Configuration Options pass.
A container for analyses that lazily runs them and caches their results.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
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 CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
static 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.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void applyUpdates(ArrayRef< typename DomTreeT::UpdateType > Updates)
Submit updates to all available trees.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Indirect Branch Instruction.
iterator_range< succ_op_iterator > successors()
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...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
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.
void 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.
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.
FunctionPass * createIndirectBrExpandPass()
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
void initializeIndirectBrExpandLegacyPassPass(PassRegistry &)