PredicateInfo.h 8.1 KB

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