1 //===- PredicateInfo.h - Build PredicateInfo ----------------------*-C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements the PredicateInfo analysis, which creates an Extended
12 /// SSA form for operations used in branch comparisons and llvm.assume
15 /// Copies of these operations are inserted into the true/false edge (and after
16 /// assumes), and information attached to the copies. All uses of the original
17 /// operation in blocks dominated by the true/false edge (and assume), are
18 /// replaced with uses of the copies. This enables passes to easily and sparsely
19 /// propagate condition based info into the operations that may be affected.
22 /// %cmp = icmp eq i32 %x, 50
23 /// br i1 %cmp, label %true, label %false
31 /// %cmp = icmp eq i32, %x, 50
32 /// br i1 %cmp, label %true, label %false
34 /// %x.0 = call \@llvm.ssa_copy.i32(i32 %x)
39 /// Using getPredicateInfoFor on x.0 will give you the comparison it is
40 /// dominated by (the icmp), and that you are located in the true edge of that
41 /// comparison, which tells you x.0 is 50.
43 /// In order to reduce the number of copies inserted, predicateinfo is only
44 /// inserted where it would actually be live. This means if there are no uses of
45 /// an operation dominated by the branch edges, or by an assume, the associated
46 /// predicate info is never inserted.
49 //===----------------------------------------------------------------------===//
51 #ifndef LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
52 #define LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
54 #include "llvm/ADT/DenseMap.h"
55 #include "llvm/ADT/DenseSet.h"
56 #include "llvm/ADT/SmallPtrSet.h"
57 #include "llvm/ADT/SmallSet.h"
58 #include "llvm/ADT/SmallVector.h"
59 #include "llvm/ADT/ilist.h"
60 #include "llvm/ADT/ilist_node.h"
61 #include "llvm/ADT/iterator.h"
62 #include "llvm/Analysis/AssumptionCache.h"
63 #include "llvm/IR/BasicBlock.h"
64 #include "llvm/IR/Dominators.h"
65 #include "llvm/IR/Instructions.h"
66 #include "llvm/IR/IntrinsicInst.h"
67 #include "llvm/IR/Module.h"
68 #include "llvm/IR/OperandTraits.h"
69 #include "llvm/IR/Type.h"
70 #include "llvm/IR/Use.h"
71 #include "llvm/IR/User.h"
72 #include "llvm/IR/Value.h"
73 #include "llvm/IR/ValueHandle.h"
74 #include "llvm/Pass.h"
75 #include "llvm/PassAnalysisSupport.h"
76 #include "llvm/Support/Casting.h"
77 #include "llvm/Support/Compiler.h"
78 #include "llvm/Support/ErrorHandling.h"
79 #include "llvm/Transforms/Utils/OrderedInstructions.h"
96 enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
98 // Base class for all predicate information we provide.
99 // All of our predicate information has at least a comparison.
100 class PredicateBase : public ilist_node<PredicateBase> {
103 // The original operand before we renamed it.
104 // This can be use by passes, when destroying predicateinfo, to know
105 // whether they can just drop the intrinsic, or have to merge metadata.
107 PredicateBase(const PredicateBase &) = delete;
108 PredicateBase &operator=(const PredicateBase &) = delete;
109 PredicateBase() = delete;
110 virtual ~PredicateBase() = default;
113 PredicateBase(PredicateType PT, Value *Op) : Type(PT), OriginalOp(Op) {}
116 class PredicateWithCondition : public PredicateBase {
119 static bool classof(const PredicateBase *PB) {
120 return PB->Type == PT_Assume || PB->Type == PT_Branch ||
121 PB->Type == PT_Switch;
125 PredicateWithCondition(PredicateType PT, Value *Op, Value *Condition)
126 : PredicateBase(PT, Op), Condition(Condition) {}
129 // Provides predicate information for assumes. Since assumes are always true,
130 // we simply provide the assume instruction, so you can tell your relative
132 class PredicateAssume : public PredicateWithCondition {
134 IntrinsicInst *AssumeInst;
135 PredicateAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
136 : PredicateWithCondition(PT_Assume, Op, Condition),
137 AssumeInst(AssumeInst) {}
138 PredicateAssume() = delete;
139 static bool classof(const PredicateBase *PB) {
140 return PB->Type == PT_Assume;
144 // Mixin class for edge predicates. The FROM block is the block where the
145 // predicate originates, and the TO block is the block where the predicate is
147 class PredicateWithEdge : public PredicateWithCondition {
151 PredicateWithEdge() = delete;
152 static bool classof(const PredicateBase *PB) {
153 return PB->Type == PT_Branch || PB->Type == PT_Switch;
157 PredicateWithEdge(PredicateType PType, Value *Op, BasicBlock *From,
158 BasicBlock *To, Value *Cond)
159 : PredicateWithCondition(PType, Op, Cond), From(From), To(To) {}
162 // Provides predicate information for branches.
163 class PredicateBranch : public PredicateWithEdge {
165 // If true, SplitBB is the true successor, otherwise it's the false successor.
167 PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB,
168 Value *Condition, bool TakenEdge)
169 : PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition),
170 TrueEdge(TakenEdge) {}
171 PredicateBranch() = delete;
172 static bool classof(const PredicateBase *PB) {
173 return PB->Type == PT_Branch;
177 class PredicateSwitch : public PredicateWithEdge {
180 // This is the switch instruction.
182 PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB,
183 Value *CaseValue, SwitchInst *SI)
184 : PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB,
186 CaseValue(CaseValue), Switch(SI) {}
187 PredicateSwitch() = delete;
188 static bool classof(const PredicateBase *PB) {
189 return PB->Type == PT_Switch;
193 // This name is used in a few places, so kick it into their own namespace
194 namespace PredicateInfoClasses {
198 /// Encapsulates PredicateInfo, including all data associated with memory
200 class PredicateInfo {
202 // Used to store information about each value we might rename.
204 // Information about each possible copy. During processing, this is each
205 // inserted info. After processing, we move the uninserted ones to the
206 // uninserted vector.
207 SmallVector<PredicateBase *, 4> Infos;
208 SmallVector<PredicateBase *, 4> UninsertedInfos;
210 // This owns the all the predicate infos in the function, placed or not.
211 iplist<PredicateBase> AllInfos;
214 PredicateInfo(Function &, DominatorTree &, AssumptionCache &);
217 void verifyPredicateInfo() const;
220 void print(raw_ostream &) const;
222 const PredicateBase *getPredicateInfoFor(const Value *V) const {
223 return PredicateMap.lookup(V);
227 // Used by PredicateInfo annotater, dumpers, and wrapper pass.
228 friend class PredicateInfoAnnotatedWriter;
229 friend class PredicateInfoPrinterLegacyPass;
232 void buildPredicateInfo();
233 void processAssume(IntrinsicInst *, BasicBlock *, SmallPtrSetImpl<Value *> &);
234 void processBranch(BranchInst *, BasicBlock *, SmallPtrSetImpl<Value *> &);
235 void processSwitch(SwitchInst *, BasicBlock *, SmallPtrSetImpl<Value *> &);
236 void renameUses(SmallPtrSetImpl<Value *> &);
237 using ValueDFS = PredicateInfoClasses::ValueDFS;
238 typedef SmallVectorImpl<ValueDFS> ValueDFSStack;
239 void convertUsesToDFSOrdered(Value *, SmallVectorImpl<ValueDFS> &);
240 Value *materializeStack(unsigned int &, ValueDFSStack &, Value *);
241 bool stackIsInScope(const ValueDFSStack &, const ValueDFS &) const;
242 void popStackUntilDFSScope(ValueDFSStack &, const ValueDFS &);
243 ValueInfo &getOrCreateValueInfo(Value *);
244 void addInfoFor(SmallPtrSetImpl<Value *> &OpsToRename, Value *Op,
246 const ValueInfo &getValueInfo(Value *) const;
250 OrderedInstructions OI;
251 // This maps from copy operands to Predicate Info. Note that it does not own
252 // the Predicate Info, they belong to the ValueInfo structs in the ValueInfos
254 DenseMap<const Value *, const PredicateBase *> PredicateMap;
255 // This stores info about each operand or comparison result we make copies
256 // of. The real ValueInfos start at index 1, index 0 is unused so that we can
257 // more easily detect invalid indexing.
258 SmallVector<ValueInfo, 32> ValueInfos;
259 // This gives the index into the ValueInfos array for a given Value. Because
260 // 0 is not a valid Value Info index, you can use DenseMap::lookup and tell
261 // whether it returned a valid result.
262 DenseMap<Value *, unsigned int> ValueInfoNums;
263 // The set of edges along which we can only handle phi uses, due to critical
265 DenseSet<std::pair<BasicBlock *, BasicBlock *>> EdgeUsesOnly;
266 // The set of ssa_copy declarations we created with our custom mangling.
267 SmallSet<AssertingVH<Function>, 20> CreatedDeclarations;
270 // This pass does eager building and then printing of PredicateInfo. It is used
272 // the tests to be able to build, dump, and verify PredicateInfo.
273 class PredicateInfoPrinterLegacyPass : public FunctionPass {
275 PredicateInfoPrinterLegacyPass();
278 bool runOnFunction(Function &) override;
279 void getAnalysisUsage(AnalysisUsage &AU) const override;
282 /// Printer pass for \c PredicateInfo.
283 class PredicateInfoPrinterPass
284 : public PassInfoMixin<PredicateInfoPrinterPass> {
288 explicit PredicateInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
289 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
292 /// Verifier pass for \c PredicateInfo.
293 struct PredicateInfoVerifierPass : PassInfoMixin<PredicateInfoVerifierPass> {
294 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
297 } // end namespace llvm
299 #endif // LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H