30 #define DEBUG_TYPE "si-annotate-control-flow"
35 typedef std::pair<BasicBlock *, Value *> StackEntry;
74 void eraseIfUnused(
PHINode *Phi);
92 SIAnnotateControlFlow():
95 bool doInitialization(
Module &M)
override;
99 StringRef getPassName()
const override {
return "SI annotate control flow"; }
114 "Annotate SI Control Flow",
false,
false)
118 "Annotate SI Control
Flow",
false, false)
120 char SIAnnotateControlFlow::
ID = 0;
123 bool SIAnnotateControlFlow::doInitialization(
Module &M) {
148 bool SIAnnotateControlFlow::isUniform(
BranchInst *
T) {
150 T->
getMetadata(
"structurizecfg.uniform") !=
nullptr;
154 bool SIAnnotateControlFlow::isTopOfStack(
BasicBlock *BB) {
155 return !
Stack.empty() &&
Stack.back().first == BB;
159 Value *SIAnnotateControlFlow::popSaved() {
160 return Stack.pop_back_val().second;
165 Stack.push_back(std::make_pair(BB, Saved));
170 bool SIAnnotateControlFlow::isElse(
PHINode *Phi) {
188 void SIAnnotateControlFlow::eraseIfUnused(
PHINode *Phi) {
190 DEBUG(
dbgs() <<
"Erased unused condition phi\n");
195 void SIAnnotateControlFlow::openIf(
BranchInst *Term) {
205 void SIAnnotateControlFlow::insertElse(
BranchInst *Term) {
206 if (isUniform(Term)) {
215 Value *SIAnnotateControlFlow::handleLoopCondition(
225 if ((Phi = dyn_cast<PHINode>(Cond)) && L->
contains(Phi)) {
235 if (isa<ConstantInt>(Incoming)) {
241 Value *PhiArg = handleLoopCondition(Incoming, Broken, L,
242 Term, LoopPhiConditions);
246 BasicBlock *IDom = DT->getNode(Parent)->getIDom()->getBlock();
250 if (Incoming != BoolTrue)
271 OldEnd = dyn_cast<CallInst>(OldEnd->
getNextNode());
288 if (
Instruction *Inst = dyn_cast<Instruction>(Cond)) {
294 Insert = L->
getHeader()->getFirstNonPHIOrDbgOrLifetime();
302 if (isa<Constant>(Cond)) {
314 void SIAnnotateControlFlow::handleLoop(
BranchInst *Term) {
329 Value *Arg = handleLoopCondition(Cond, Broken, L, Term, LoopPhiConditions);
332 Broken->
addIncoming(Pred == BB ? Arg : Int64Zero, Pred);
337 if (
PHINode *Cond = cast_or_null<PHINode>(Val))
345 void SIAnnotateControlFlow::closeControlFlow(
BasicBlock *BB) {
366 Value *Exec = popSaved();
368 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
374 bool SIAnnotateControlFlow::runOnFunction(
Function &
F) {
375 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
376 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
377 DA = &getAnalysis<DivergenceAnalysis>();
385 if (isTopOfStack(BB))
386 closeControlFlow(BB);
392 if (isTopOfStack(BB))
393 closeControlFlow(BB);
399 if (isTopOfStack(BB)) {
401 if (Phi && Phi->
getParent() == BB && isElse(Phi)) {
407 closeControlFlow(BB);
419 return new SIAnnotateControlFlow();
void push_back(const T &Elt)
static ConstantInt * getFalse(LLVMContext &Context)
Annotate SI Control false
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
FunctionPass * createSIAnnotateControlFlowPass()
Create the annotation pass.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
void getLoopLatches(SmallVectorImpl< BlockT * > &LoopLatches) const
Return all loop latch blocks of this loop.
A Module instance is used to store all the information related to an LLVM module. ...
Maximum length of the test input If
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
This class represents a function call, abstracting a target machine's calling convention.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
static IntegerType * getInt64Ty(LLVMContext &C)
BlockT * getHeader() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isUnconditional() const
'undef' values are things that do not have specified contents.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Value handle that is nullable, but tries to track the Value.
BasicBlock * getSuccessor(unsigned i) const
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Subclasses of this class are all able to terminate a basic block.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
Conditional or Unconditional Branch instruction.
df_iterator< T > df_end(const T &G)
This is an important base class in LLVM.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
This is the shared class of boolean and integer constants.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
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.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
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...
pred_range predecessors(BasicBlock *BB)
const BasicBlock & getEntryBlock() const
static ConstantInt * getTrue(LLVMContext &Context)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
df_iterator< T > df_begin(const T &G)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Target - Wrapper for Target specific information.
llvm_unreachable("Unknown RegBank!")
Value * getCondition() const
Represents a single loop in the control flow graph.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
void setCondition(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
Legacy analysis pass which computes a DominatorTree.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
void setIncomingValue(unsigned i, Value *V)
INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,"Annotate SI Control Flow", false, false) INITIALIZE_PASS_END(SIAnnotateControlFlow
const BasicBlock * getParent() const
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.