LLVM  10.0.0
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 "AMDGPUSubtarget.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/Debug.h"
43 #include <cassert>
44 #include <utility>
45 
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "si-annotate-control-flow"
49 
50 namespace {
51 
52 // Complex types used in this pass
53 using StackEntry = std::pair<BasicBlock *, Value *>;
54 using StackVector = SmallVector<StackEntry, 16>;
55 
56 class SIAnnotateControlFlow : public FunctionPass {
58 
59  Type *Boolean;
60  Type *Void;
61  Type *IntMask;
62  Type *ReturnStruct;
63 
64  ConstantInt *BoolTrue;
65  ConstantInt *BoolFalse;
66  UndefValue *BoolUndef;
67  Constant *IntMaskZero;
68 
69  Function *If;
70  Function *Else;
71  Function *IfBreak;
72  Function *Loop;
73  Function *EndCf;
74 
75  DominatorTree *DT;
76  StackVector Stack;
77 
78  LoopInfo *LI;
79 
80  void initialize(Module &M, const GCNSubtarget &ST);
81 
82  bool isUniform(BranchInst *T);
83 
84  bool isTopOfStack(BasicBlock *BB);
85 
86  Value *popSaved();
87 
88  void push(BasicBlock *BB, Value *Saved);
89 
90  bool isElse(PHINode *Phi);
91 
92  void eraseIfUnused(PHINode *Phi);
93 
94  void openIf(BranchInst *Term);
95 
96  void insertElse(BranchInst *Term);
97 
98  Value *
99  handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
100  BranchInst *Term);
101 
102  void handleLoop(BranchInst *Term);
103 
104  void closeControlFlow(BasicBlock *BB);
105 
106 public:
107  static char ID;
108 
109  SIAnnotateControlFlow() : FunctionPass(ID) {}
110 
111  bool runOnFunction(Function &F) override;
112 
113  StringRef getPassName() const override { return "SI annotate control flow"; }
114 
115  void getAnalysisUsage(AnalysisUsage &AU) const override {
122  }
123 };
124 
125 } // end anonymous namespace
126 
127 INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
128  "Annotate SI Control Flow", false, false)
132 INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
133  "Annotate SI Control Flow", false, false)
134 
135 char SIAnnotateControlFlow::ID = 0;
136 
137 /// Initialize all the types and constants used in the pass
138 void SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) {
139  LLVMContext &Context = M.getContext();
140 
141  Void = Type::getVoidTy(Context);
142  Boolean = Type::getInt1Ty(Context);
143  IntMask = ST.isWave32() ? Type::getInt32Ty(Context)
144  : Type::getInt64Ty(Context);
145  ReturnStruct = StructType::get(Boolean, IntMask);
146 
147  BoolTrue = ConstantInt::getTrue(Context);
148  BoolFalse = ConstantInt::getFalse(Context);
149  BoolUndef = UndefValue::get(Boolean);
150  IntMaskZero = ConstantInt::get(IntMask, 0);
151 
152  If = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if, { IntMask });
153  Else = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_else,
154  { IntMask, IntMask });
155  IfBreak = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_if_break,
156  { IntMask, IntMask });
157  Loop = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_loop, { IntMask });
158  EndCf = Intrinsic::getDeclaration(&M, Intrinsic::amdgcn_end_cf, { IntMask });
159 }
160 
161 /// Is the branch condition uniform or did the StructurizeCFG pass
162 /// consider it as such?
163 bool SIAnnotateControlFlow::isUniform(BranchInst *T) {
164  return DA->isUniform(T) ||
165  T->getMetadata("structurizecfg.uniform") != nullptr;
166 }
167 
168 /// Is BB the last block saved on the stack ?
169 bool SIAnnotateControlFlow::isTopOfStack(BasicBlock *BB) {
170  return !Stack.empty() && Stack.back().first == BB;
171 }
172 
173 /// Pop the last saved value from the control flow stack
174 Value *SIAnnotateControlFlow::popSaved() {
175  return Stack.pop_back_val().second;
176 }
177 
178 /// Push a BB and saved value to the control flow stack
179 void SIAnnotateControlFlow::push(BasicBlock *BB, Value *Saved) {
180  Stack.push_back(std::make_pair(BB, Saved));
181 }
182 
183 /// Can the condition represented by this PHI node treated like
184 /// an "Else" block?
185 bool SIAnnotateControlFlow::isElse(PHINode *Phi) {
186  BasicBlock *IDom = DT->getNode(Phi->getParent())->getIDom()->getBlock();
187  for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) {
188  if (Phi->getIncomingBlock(i) == IDom) {
189 
190  if (Phi->getIncomingValue(i) != BoolTrue)
191  return false;
192 
193  } else {
194  if (Phi->getIncomingValue(i) != BoolFalse)
195  return false;
196 
197  }
198  }
199  return true;
200 }
201 
202 // Erase "Phi" if it is not used any more
203 void SIAnnotateControlFlow::eraseIfUnused(PHINode *Phi) {
204  if (RecursivelyDeleteDeadPHINode(Phi)) {
205  LLVM_DEBUG(dbgs() << "Erased unused condition phi\n");
206  }
207 }
208 
209 /// Open a new "If" block
210 void SIAnnotateControlFlow::openIf(BranchInst *Term) {
211  if (isUniform(Term))
212  return;
213 
214  Value *Ret = CallInst::Create(If, Term->getCondition(), "", Term);
215  Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
216  push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
217 }
218 
219 /// Close the last "If" block and open a new "Else" block
220 void SIAnnotateControlFlow::insertElse(BranchInst *Term) {
221  if (isUniform(Term)) {
222  return;
223  }
224  Value *Ret = CallInst::Create(Else, popSaved(), "", Term);
225  Term->setCondition(ExtractValueInst::Create(Ret, 0, "", Term));
226  push(Term->getSuccessor(1), ExtractValueInst::Create(Ret, 1, "", Term));
227 }
228 
229 /// Recursively handle the condition leading to a loop
230 Value *SIAnnotateControlFlow::handleLoopCondition(
231  Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term) {
232  if (Instruction *Inst = dyn_cast<Instruction>(Cond)) {
233  BasicBlock *Parent = Inst->getParent();
234  Instruction *Insert;
235  if (L->contains(Inst)) {
236  Insert = Parent->getTerminator();
237  } else {
238  Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
239  }
240 
241  Value *Args[] = { Cond, Broken };
242  return CallInst::Create(IfBreak, Args, "", Insert);
243  }
244 
245  // Insert IfBreak in the loop header TERM for constant COND other than true.
246  if (isa<Constant>(Cond)) {
247  Instruction *Insert = Cond == BoolTrue ?
248  Term : L->getHeader()->getTerminator();
249 
250  Value *Args[] = { Cond, Broken };
251  return CallInst::Create(IfBreak, Args, "", Insert);
252  }
253 
254  llvm_unreachable("Unhandled loop condition!");
255 }
256 
257 /// Handle a back edge (loop)
258 void SIAnnotateControlFlow::handleLoop(BranchInst *Term) {
259  if (isUniform(Term))
260  return;
261 
262  BasicBlock *BB = Term->getParent();
263  llvm::Loop *L = LI->getLoopFor(BB);
264  if (!L)
265  return;
266 
267  BasicBlock *Target = Term->getSuccessor(1);
268  PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken", &Target->front());
269 
270  Value *Cond = Term->getCondition();
271  Term->setCondition(BoolTrue);
272  Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
273 
274  for (BasicBlock *Pred : predecessors(Target)) {
275  Value *PHIValue = IntMaskZero;
276  if (Pred == BB) // Remember the value of the previous iteration.
277  PHIValue = Arg;
278  // If the backedge from Pred to Target could be executed before the exit
279  // of the loop at BB, it should not reset or change "Broken", which keeps
280  // track of the number of threads exited the loop at BB.
281  else if (L->contains(Pred) && DT->dominates(Pred, BB))
282  PHIValue = Broken;
283  Broken->addIncoming(PHIValue, Pred);
284  }
285 
286  Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
287 
288  push(Term->getSuccessor(0), Arg);
289 }
290 
291 /// Close the last opened control flow
292 void SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
293  llvm::Loop *L = LI->getLoopFor(BB);
294 
295  assert(Stack.back().first == BB);
296 
297  if (L && L->getHeader() == BB) {
298  // We can't insert an EndCF call into a loop header, because it will
299  // get executed on every iteration of the loop, when it should be
300  // executed only once before the loop.
302  L->getLoopLatches(Latches);
303 
305  for (BasicBlock *Pred : predecessors(BB)) {
306  if (!is_contained(Latches, Pred))
307  Preds.push_back(Pred);
308  }
309 
310  BB = SplitBlockPredecessors(BB, Preds, "endcf.split", DT, LI, nullptr,
311  false);
312  }
313 
314  Value *Exec = popSaved();
315  Instruction *FirstInsertionPt = &*BB->getFirstInsertionPt();
316  if (!isa<UndefValue>(Exec) && !isa<UnreachableInst>(FirstInsertionPt))
317  CallInst::Create(EndCf, Exec, "", FirstInsertionPt);
318 }
319 
320 /// Annotate the control flow with intrinsics so the backend can
321 /// recognize if/then/else and loops.
323  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
324  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
325  DA = &getAnalysis<LegacyDivergenceAnalysis>();
326  TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
327  const TargetMachine &TM = TPC.getTM<TargetMachine>();
328 
329  initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
330 
332  E = df_end(&F.getEntryBlock()); I != E; ++I) {
333  BasicBlock *BB = *I;
335 
336  if (!Term || Term->isUnconditional()) {
337  if (isTopOfStack(BB))
338  closeControlFlow(BB);
339 
340  continue;
341  }
342 
343  if (I.nodeVisited(Term->getSuccessor(1))) {
344  if (isTopOfStack(BB))
345  closeControlFlow(BB);
346 
347  handleLoop(Term);
348  continue;
349  }
350 
351  if (isTopOfStack(BB)) {
352  PHINode *Phi = dyn_cast<PHINode>(Term->getCondition());
353  if (Phi && Phi->getParent() == BB && isElse(Phi)) {
354  insertElse(Term);
355  eraseIfUnused(Phi);
356  continue;
357  }
358 
359  closeControlFlow(BB);
360  }
361 
362  openIf(Term);
363  }
364 
365  if (!Stack.empty()) {
366  // CFG was probably not structured.
367  report_fatal_error("failed to annotate CFG");
368  }
369 
370  return true;
371 }
372 
373 /// Create the annotation pass
375  return new SIAnnotateControlFlow();
376 }
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:667
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:177
FunctionPass * createSIAnnotateControlFlowPass()
Create the annotation pass.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
LLVMContext & Context
AMDGPU specific subclass of TargetSubtarget.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE, "Annotate SI Control Flow", false, false) INITIALIZE_PASS_END(SIAnnotateControlFlow
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:181
Value * getCondition() const
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.cpp:144
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
&#39;undef&#39; values are things that do not have specified contents.
Definition: Constants.h:1286
void getLoopLatches(SmallVectorImpl< BlockT *> &LoopLatches) const
Return all loop latch blocks of this loop.
Definition: LoopInfo.h:314
Target-Independent Code Generator Pass Configuration Options.
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:346
BlockT * getHeader() const
Definition: LoopInfo.h:105
const Instruction * getFirstNonPHIOrDbgOrLifetime() const
Returns a pointer to the first instruction in this block that is not a PHINode, a debug intrinsic...
Definition: BasicBlock.cpp:210
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:96
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:244
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1112
const BasicBlock & getEntryBlock() const
Definition: Function.h:675
static bool runOnFunction(Function &F, bool PostInlining)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock *> Preds, const char *Suffix, DominatorTree *DT=nullptr, 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...
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:223
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
static void push(SmallVectorImpl< uint64_t > &R, StringRef Str)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
Conditional or Unconditional Branch instruction.
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
df_iterator< T > df_end(const T &G)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
Definition: BasicBlock.h:285
unsigned char Boolean
Definition: ConvertUTF.h:112
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:165
Annotate SI Control Flow
constexpr double e
Definition: MathExtras.h:57
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1514
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:115
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
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...
Definition: Local.cpp:523
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:704
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)
Definition: CFG.h:124
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:660
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
df_iterator< T > df_begin(const T &G)
Target - Wrapper for Target specific information.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:180
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
bool isUnconditional() const
void setCondition(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:74
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:1219
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
#define LLVM_DEBUG(X)
Definition: Debug.h:122
#define DEBUG_TYPE
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
const BasicBlock * getParent() const
Definition: Instruction.h:66
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1241