LLVM 18.0.0git
SIAnnotateControlFlow.cpp
Go to the documentation of this file.
1//===- SIAnnotateControlFlow.cpp ------------------------------------------===//
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//
9/// \file
10/// Annotates the control flow with hardware specific intrinsics.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AMDGPU.h"
15#include "GCNSubtarget.h"
19#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/Dominators.h"
22#include "llvm/IR/IntrinsicsAMDGPU.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "si-annotate-control-flow"
31
32namespace {
33
34// Complex types used in this pass
35using StackEntry = std::pair<BasicBlock *, Value *>;
36using StackVector = SmallVector<StackEntry, 16>;
37
38class SIAnnotateControlFlow : public FunctionPass {
40
42 Type *Void;
43 Type *IntMask;
44 Type *ReturnStruct;
45
46 ConstantInt *BoolTrue;
47 ConstantInt *BoolFalse;
48 UndefValue *BoolUndef;
49 Constant *IntMaskZero;
50
51 Function *If;
52 Function *Else;
53 Function *IfBreak;
55 Function *EndCf;
56
57 DominatorTree *DT;
58 StackVector Stack;
59
60 LoopInfo *LI;
61
62 void initialize(Module &M, const GCNSubtarget &ST);
63
64 bool isUniform(BranchInst *T);
65
66 bool isTopOfStack(BasicBlock *BB);
67
68 Value *popSaved();
69
70 void push(BasicBlock *BB, Value *Saved);
71
72 bool isElse(PHINode *Phi);
73
74 bool hasKill(const BasicBlock *BB);
75
76 bool eraseIfUnused(PHINode *Phi);
77
78 bool openIf(BranchInst *Term);
79
80 bool insertElse(BranchInst *Term);
81
82 Value *
83 handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
84 BranchInst *Term);
85
86 bool handleLoop(BranchInst *Term);
87
88 bool closeControlFlow(BasicBlock *BB);
89
90public:
91 static char ID;
92
93 SIAnnotateControlFlow() : FunctionPass(ID) {}
94
95 bool runOnFunction(Function &F) override;
96
97 StringRef getPassName() const override { return "SI annotate control flow"; }
98
99 void getAnalysisUsage(AnalysisUsage &AU) const override {
107 }
108};
109
110} // end anonymous namespace
111
112INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
113 "Annotate SI Control Flow", false, false)
117INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
118 "Annotate SI Control Flow", false, false)
119
120char SIAnnotateControlFlow::ID = 0;
121
122/// Initialize all the types and constants used in the pass
123void SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) {
124 LLVMContext &Context = M.getContext();
125
128 IntMask = ST.isWave32() ? Type::getInt32Ty(Context)
130 ReturnStruct = StructType::get(Boolean, IntMask);
131
132 BoolTrue = ConstantInt::getTrue(Context);
133 BoolFalse = ConstantInt::getFalse(Context);
134 BoolUndef = PoisonValue::get(Boolean);
135 IntMaskZero = ConstantInt::get(IntMask, 0);
136
137 If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if, { IntMask });
138 Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else,
139 { IntMask, IntMask });
140 IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break,
141 { IntMask });
142 Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask });
143 EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask });
144}
145
146/// Is the branch condition uniform or did the StructurizeCFG pass
147/// consider it as such?
148bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
149 return UA->isUniform(T) ||
150 T->getMetadata("structurizecfg.uniform") != nullptr;
151}
152
153/// Is BB the last block saved on the stack ?
154bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
155 return !Stack.empty() && Stack.back().first == BB;
156}
157
158/// Pop the last saved value from the control flow stack
159Value *SIAnnotateControlFlow::popSaved() {
160 return Stack.pop_back_val().second;
161}
162
163/// Push a BB and saved value to the control flow stack
164void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
165 Stack.push_back(std::pair(BB, Saved));
166}
167
168/// Can the condition represented by this PHI node treated like
169/// an "Else" block?
170bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
171 BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
172 for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
173 if (Phi->getIncomingBlock(i) == IDom) {
174
175 if (Phi->getIncomingValue(i) != BoolTrue)
176 return false;
177
178 } else {
179 if (Phi->getIncomingValue(i) != BoolFalse)
180 return false;
181
182 }
183 }
184 return true;
185}
186
187bool SIAnnotateControlFlow::hasKill(const BasicBlock *BB) {
188 for (const Instruction &I : *BB) {
189 if (const CallInst *CI = dyn_cast<CallInst>(&I))
190 if (CI->getIntrinsicID() == Intrinsic::amdgcn_kill)
191 return true;
192 }
193 return false;
194}
195
196// Erase "Phi" if it is not used any more. Return true if any change was made.
197bool SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
198 bool Changed = RecursivelyDeleteDeadPHINode(Phi);
199 if (Changed)
200 LLVM_DEBUG(dbgs() << "Erased unused condition phi\n");
201 return Changed;
202}
203
204/// Open a new "If" block
205bool SIAnnotateControlFlow::openIf(BranchInst *Term) {
206 if (isUniform(Term))
207 return false;
208
209 Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
210 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
211 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
212 return true;
213}
214
215/// Close the last "If" block and open a new "Else" block
216bool SIAnnotateControlFlow::insertElse(BranchInst *Term) {
217 if (isUniform(Term)) {
218 return false;
219 }
220 Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
221 Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
222 push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
223 return true;
224}
225
226/// Recursively handle the condition leading to a loop
227Value *SIAnnotateControlFlow::handleLoopCondition(
228 Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) {
229 if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
230 BasicBlock *Parent = Inst->getParent();
232 if (L->contains(Inst)) {
233 Insert = Parent->getTerminator();
234 } else {
235 Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
236 }
237
238 Value *Args[] = { Cond, Broken };
239 return CallInst::Create(IfBreak, Args, "", Insert);
240 }
241
242 // Insert IfBreak in the loop header TERM for constant COND other than true.
243 if (isa<Constant>(Cond)) {
244 Instruction *Insert = Cond == BoolTrue ?
245 Term : L->getHeader()->getTerminator();
246
247 Value *Args[] = { Cond, Broken };
248 return CallInst::Create(IfBreak, Args, "", Insert);
249 }
250
251 if (isa<Argument>(Cond)) {
252 Instruction *Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
253 Value *Args[] = { Cond, Broken };
254 return CallInst::Create(IfBreak, Args, "", Insert);
255 }
256
257 llvm_unreachable("Unhandled loop condition!");
258}
259
260/// Handle a back edge (loop)
261bool SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
262 if (isUniform(Term))
263 return false;
264
265 BasicBlock *BB = Term->getParent();
266 llvm::Loop *L = LI->getLoopFor(BB);
267 if (!L)
268 return false;
269
270 BasicBlock *Target = Term->getSuccessor(1);
271 PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken");
272 Broken->insertBefore(Target->begin());
273
274 Value *Cond = Term->getCondition();
275 Term->setCondition(BoolTrue);
276 Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
277
278 for (BasicBlock *Pred : predecessors(Target)) {
279 Value *PHIValue = IntMaskZero;
280 if (Pred == BB) // Remember the value of the previous iteration.
281 PHIValue = Arg;
282 // If the backedge from Pred to Target could be executed before the exit
283 // of the loop at BB, it should not reset or change "Broken", which keeps
284 // track of the number of threads exited the loop at BB.
285 else if (L->contains(Pred) && DT->dominates(Pred, BB))
286 PHIValue = Broken;
287 Broken->addIncoming(PHIValue, Pred);
288 }
289
290 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
291
292 push(Term->getSuccessor(0), Arg);
293
294 return true;
295}
296
297/// Close the last opened control flow
298bool SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
299 llvm::Loop *L = LI->getLoopFor(BB);
300
301 assert(Stack.back().first == BB);
302
303 if (L && L->getHeader() == BB) {
304 // We can't insert an EndCF call into a loop header, because it will
305 // get executed on every iteration of the loop, when it should be
306 // executed only once before the loop.
307 SmallVector <BasicBlock *, 8> Latches;
308 L->getLoopLatches(Latches);
309
311 for (BasicBlock *Pred : predecessors(BB)) {
312 if (!is_contained(Latches, Pred))
313 Preds.push_back(Pred);
314 }
315
316 BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
317 false);
318 }
319
320 Value *Exec = popSaved();
321 Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
322 if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt)) {
323 Instruction *ExecDef = cast<Instruction>(Exec);
324 BasicBlock *DefBB = ExecDef->getParent();
325 if (!DT->dominates(DefBB, BB)) {
326 // Split edge to make Def dominate Use
327 FirstInsertionPt = &*SplitEdge(DefBB, BB, DT, LI)->getFirstInsertionPt();
328 }
329 CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
330 }
331
332 return true;
333}
334
335/// Annotate the control flow with intrinsics so the backend can
336/// recognize if/then/else and loops.
337bool SIAnnotateControlFlow::runOnFunction(Function &F) {
338 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
339 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
340 UA = &getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
341 TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
342 const TargetMachine &TM = TPC.getTM<TargetMachine>();
343
344 bool Changed = false;
345 initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
346 for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
347 E = df_end(&F.getEntryBlock()); I != E; ++I) {
348 BasicBlock *BB = *I;
349 BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());
350
351 if (!Term || Term->isUnconditional()) {
352 if (isTopOfStack(BB))
353 Changed |= closeControlFlow(BB);
354
355 continue;
356 }
357
358 if (I.nodeVisited(Term->getSuccessor(1))) {
359 if (isTopOfStack(BB))
360 Changed |= closeControlFlow(BB);
361
362 if (DT->dominates(Term->getSuccessor(1), BB))
363 Changed |= handleLoop(Term);
364 continue;
365 }
366
367 if (isTopOfStack(BB)) {
368 PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
369 if (Phi && Phi->getParent() == BB && isElse(Phi) && !hasKill(BB)) {
370 Changed |= insertElse(Term);
371 Changed |= eraseIfUnused(Phi);
372 continue;
373 }
374
375 Changed |= closeControlFlow(BB);
376 }
377
378 Changed |= openIf(Term);
379 }
380
381 if (!Stack.empty()) {
382 // CFG was probably not structured.
383 report_fatal_error("failed to annotate CFG");
384 }
385
386 return Changed;
387}
388
389/// Create the annotation pass
391 return new SIAnnotateControlFlow();
392}
aarch64 promote const
static void push(SmallVectorImpl< uint64_t > &R, StringRef Str)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
Fixup Statepoint Caller Saved
AMD GCN specific subclass of TargetSubtarget.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
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
const SmallVectorImpl< MachineOperand > & Cond
Annotate SI Control Flow
#define DEBUG_TYPE
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
Target-Independent Code Generator Pass Configuration Options pass.
LLVM IR instance of the generic uniformity analysis.
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.
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:257
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
Conditional or Unconditional Branch instruction.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
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.
Definition: Constants.cpp:888
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:840
This is an important base class in LLVM.
Definition: Constant.h:41
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:314
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
Definition: Instruction.cpp:89
const BasicBlock * getParent() const
Definition: Instruction.h:90
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:594
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:47
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
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...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:374
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
Target-Independent Code Generator Pass Configuration Options.
TMC & getTM() const
Get the right type of TargetMachine for this target.
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt1Ty(LLVMContext &C)
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
'undef' values are things that do not have specified contents.
Definition: Constants.h:1371
Legacy analysis pass which computes a CycleInfo.
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1422
NodeAddr< PhiNode * > Phi
Definition: RDFGraph.h:390
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
df_iterator< T > df_begin(const T &G)
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
unsigned char Boolean
Definition: ConvertUTF.h:131
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1884
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
Definition: Local.cpp:637
df_iterator< T > df_end(const T &G)
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
FunctionPass * createSIAnnotateControlFlowPass()
Create the annotation pass.