xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/Utils/PredicateInfo.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- PredicateInfo.h - Build PredicateInfo ----------------------*-C++-*-===//
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 ///  This file implements the PredicateInfo analysis, which creates an Extended
11 /// SSA form for operations used in branch comparisons and llvm.assume
12 /// comparisons.
13 ///
14 /// Copies of these operations are inserted into the true/false edge (and after
15 /// assumes), and information attached to the copies.  All uses of the original
16 /// operation in blocks dominated by the true/false edge (and assume), are
17 /// replaced with uses of the copies.  This enables passes to easily and sparsely
18 /// propagate condition based info into the operations that may be affected.
19 ///
20 /// Example:
21 /// %cmp = icmp eq i32 %x, 50
22 /// br i1 %cmp, label %true, label %false
23 /// true:
24 /// ret i32 %x
25 /// false:
26 /// ret i32 1
27 ///
28 /// will become
29 ///
30 /// %cmp = icmp eq i32, %x, 50
31 /// br i1 %cmp, label %true, label %false
32 /// true:
33 /// %x.0 = call \@llvm.ssa_copy.i32(i32 %x)
34 /// ret i32 %x.0
35 /// false:
36 /// ret i32 1
37 ///
38 /// Using getPredicateInfoFor on x.0 will give you the comparison it is
39 /// dominated by (the icmp), and that you are located in the true edge of that
40 /// comparison, which tells you x.0 is 50.
41 ///
42 /// In order to reduce the number of copies inserted, predicateinfo is only
43 /// inserted where it would actually be live.  This means if there are no uses of
44 /// an operation dominated by the branch edges, or by an assume, the associated
45 /// predicate info is never inserted.
46 ///
47 ///
48 //===----------------------------------------------------------------------===//
49 
50 #ifndef LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
51 #define LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
52 
53 #include "llvm/ADT/DenseMap.h"
54 #include "llvm/ADT/SmallSet.h"
55 #include "llvm/IR/Instructions.h"
56 #include "llvm/IR/PassManager.h"
57 #include "llvm/IR/ValueHandle.h"
58 #include "llvm/Support/Allocator.h"
59 #include "llvm/Support/Compiler.h"
60 
61 namespace llvm {
62 
63 class AssumptionCache;
64 class DominatorTree;
65 class Function;
66 class Value;
67 class IntrinsicInst;
68 class raw_ostream;
69 
70 enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
71 
72 /// Constraint for a predicate of the form "cmp Pred Op, OtherOp", where Op
73 /// is the value the constraint applies to (the ssa.copy result).
74 struct PredicateConstraint {
75   CmpInst::Predicate Predicate;
76   Value *OtherOp;
77 };
78 
79 // Base class for all predicate information we provide.
80 // All of our predicate information has at least a comparison.
81 class PredicateBase {
82 public:
83   PredicateType Type;
84   // The original operand before we renamed it.
85   // This can be use by passes, when destroying predicateinfo, to know
86   // whether they can just drop the intrinsic, or have to merge metadata.
87   Value *OriginalOp;
88   // The renamed operand in the condition used for this predicate. For nested
89   // predicates, this is different to OriginalOp which refers to the initial
90   // operand.
91   Value *RenamedOp;
92   // The condition associated with this predicate.
93   Value *Condition;
94 
95   PredicateBase(const PredicateBase &) = delete;
96   PredicateBase &operator=(const PredicateBase &) = delete;
97   PredicateBase() = delete;
classof(const PredicateBase * PB)98   static bool classof(const PredicateBase *PB) {
99     return PB->Type == PT_Assume || PB->Type == PT_Branch ||
100            PB->Type == PT_Switch;
101   }
102 
103   /// Fetch condition in the form of PredicateConstraint, if possible.
104   LLVM_ABI std::optional<PredicateConstraint> getConstraint() const;
105 
106 protected:
PredicateBase(PredicateType PT,Value * Op,Value * Condition)107   PredicateBase(PredicateType PT, Value *Op, Value *Condition)
108       : Type(PT), OriginalOp(Op), Condition(Condition) {}
109 };
110 
111 // Provides predicate information for assumes.  Since assumes are always true,
112 // we simply provide the assume instruction, so you can tell your relative
113 // position to it.
114 class PredicateAssume : public PredicateBase {
115 public:
116   IntrinsicInst *AssumeInst;
PredicateAssume(Value * Op,IntrinsicInst * AssumeInst,Value * Condition)117   PredicateAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
118       : PredicateBase(PT_Assume, Op, Condition), AssumeInst(AssumeInst) {}
119   PredicateAssume() = delete;
classof(const PredicateBase * PB)120   static bool classof(const PredicateBase *PB) {
121     return PB->Type == PT_Assume;
122   }
123 };
124 
125 // Mixin class for edge predicates.  The FROM block is the block where the
126 // predicate originates, and the TO block is the block where the predicate is
127 // valid.
128 class PredicateWithEdge : public PredicateBase {
129 public:
130   BasicBlock *From;
131   BasicBlock *To;
132   PredicateWithEdge() = delete;
classof(const PredicateBase * PB)133   static bool classof(const PredicateBase *PB) {
134     return PB->Type == PT_Branch || PB->Type == PT_Switch;
135   }
136 
137 protected:
PredicateWithEdge(PredicateType PType,Value * Op,BasicBlock * From,BasicBlock * To,Value * Cond)138   PredicateWithEdge(PredicateType PType, Value *Op, BasicBlock *From,
139                     BasicBlock *To, Value *Cond)
140       : PredicateBase(PType, Op, Cond), From(From), To(To) {}
141 };
142 
143 // Provides predicate information for branches.
144 class PredicateBranch : public PredicateWithEdge {
145 public:
146   // If true, SplitBB is the true successor, otherwise it's the false successor.
147   bool TrueEdge;
PredicateBranch(Value * Op,BasicBlock * BranchBB,BasicBlock * SplitBB,Value * Condition,bool TakenEdge)148   PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB,
149                   Value *Condition, bool TakenEdge)
150       : PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition),
151         TrueEdge(TakenEdge) {}
152   PredicateBranch() = delete;
classof(const PredicateBase * PB)153   static bool classof(const PredicateBase *PB) {
154     return PB->Type == PT_Branch;
155   }
156 };
157 
158 class PredicateSwitch : public PredicateWithEdge {
159 public:
160   Value *CaseValue;
161   // This is the switch instruction.
162   SwitchInst *Switch;
PredicateSwitch(Value * Op,BasicBlock * SwitchBB,BasicBlock * TargetBB,Value * CaseValue,SwitchInst * SI)163   PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB,
164                   Value *CaseValue, SwitchInst *SI)
165       : PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB,
166                           SI->getCondition()),
167         CaseValue(CaseValue), Switch(SI) {}
168   PredicateSwitch() = delete;
classof(const PredicateBase * PB)169   static bool classof(const PredicateBase *PB) {
170     return PB->Type == PT_Switch;
171   }
172 };
173 
174 /// Encapsulates PredicateInfo, including all data associated with memory
175 /// accesses.
176 class PredicateInfo {
177 public:
178   LLVM_ABI PredicateInfo(Function &, DominatorTree &, AssumptionCache &,
179                          BumpPtrAllocator &);
180   LLVM_ABI ~PredicateInfo();
181 
182   LLVM_ABI void verifyPredicateInfo() const;
183 
184   LLVM_ABI void dump() const;
185   LLVM_ABI void print(raw_ostream &) const;
186 
getPredicateInfoFor(const Value * V)187   const PredicateBase *getPredicateInfoFor(const Value *V) const {
188     return PredicateMap.lookup(V);
189   }
190 
191 protected:
192   // Used by PredicateInfo annotater, dumpers, and wrapper pass.
193   friend class PredicateInfoAnnotatedWriter;
194   friend class PredicateInfoBuilder;
195 
196 private:
197   Function &F;
198 
199   // This maps from copy operands to Predicate Info. Note that it does not own
200   // the Predicate Info, they belong to the ValueInfo structs in the ValueInfos
201   // vector.
202   DenseMap<const Value *, const PredicateBase *> PredicateMap;
203   // The set of ssa_copy declarations we created with our custom mangling.
204   SmallSet<AssertingVH<Function>, 20> CreatedDeclarations;
205   // Cache of ssa.copy declaration for a given type.
206   SmallDenseMap<Type *, Function *> DeclarationCache;
207 };
208 
209 /// Printer pass for \c PredicateInfo.
210 class PredicateInfoPrinterPass
211     : public PassInfoMixin<PredicateInfoPrinterPass> {
212   raw_ostream &OS;
213 
214 public:
PredicateInfoPrinterPass(raw_ostream & OS)215   explicit PredicateInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
216   LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
isRequired()217   static bool isRequired() { return true; }
218 };
219 
220 /// Verifier pass for \c PredicateInfo.
221 struct PredicateInfoVerifierPass : PassInfoMixin<PredicateInfoVerifierPass> {
222   LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
isRequiredPredicateInfoVerifierPass223   static bool isRequired() { return true; }
224 };
225 
226 } // end namespace llvm
227 
228 #endif // LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
229