GVN.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- GVN.h - Eliminate redundant values and loads -------------*- 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. /// \file
  14. /// This file provides the interface for LLVM's Global Value Numbering pass
  15. /// which eliminates fully redundant instructions. It also does somewhat Ad-Hoc
  16. /// PRE and dead load elimination.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_TRANSFORMS_SCALAR_GVN_H
  20. #define LLVM_TRANSFORMS_SCALAR_GVN_H
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/MapVector.h"
  23. #include "llvm/ADT/PostOrderIterator.h"
  24. #include "llvm/ADT/SetVector.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/Analysis/InstructionPrecedenceTracking.h"
  27. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  28. #include "llvm/IR/Dominators.h"
  29. #include "llvm/IR/InstrTypes.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include "llvm/IR/ValueHandle.h"
  32. #include "llvm/Support/Allocator.h"
  33. #include "llvm/Support/Compiler.h"
  34. #include <cstdint>
  35. #include <utility>
  36. #include <vector>
  37. namespace llvm {
  38. class AAResults;
  39. class AssumptionCache;
  40. class BasicBlock;
  41. class BranchInst;
  42. class CallInst;
  43. class Constant;
  44. class ExtractValueInst;
  45. class Function;
  46. class FunctionPass;
  47. class IntrinsicInst;
  48. class LoadInst;
  49. class LoopInfo;
  50. class MemorySSA;
  51. class MemorySSAUpdater;
  52. class OptimizationRemarkEmitter;
  53. class PHINode;
  54. class TargetLibraryInfo;
  55. class Value;
  56. /// A private "module" namespace for types and utilities used by GVN. These
  57. /// are implementation details and should not be used by clients.
  58. namespace gvn LLVM_LIBRARY_VISIBILITY {
  59. struct AvailableValue;
  60. struct AvailableValueInBlock;
  61. class GVNLegacyPass;
  62. } // end namespace gvn
  63. /// A set of parameters to control various transforms performed by GVN pass.
  64. // Each of the optional boolean parameters can be set to:
  65. /// true - enabling the transformation.
  66. /// false - disabling the transformation.
  67. /// None - relying on a global default.
  68. /// Intended use is to create a default object, modify parameters with
  69. /// additional setters and then pass it to GVN.
  70. struct GVNOptions {
  71. Optional<bool> AllowPRE = None;
  72. Optional<bool> AllowLoadPRE = None;
  73. Optional<bool> AllowLoadInLoopPRE = None;
  74. Optional<bool> AllowLoadPRESplitBackedge = None;
  75. Optional<bool> AllowMemDep = None;
  76. GVNOptions() = default;
  77. /// Enables or disables PRE in GVN.
  78. GVNOptions &setPRE(bool PRE) {
  79. AllowPRE = PRE;
  80. return *this;
  81. }
  82. /// Enables or disables PRE of loads in GVN.
  83. GVNOptions &setLoadPRE(bool LoadPRE) {
  84. AllowLoadPRE = LoadPRE;
  85. return *this;
  86. }
  87. GVNOptions &setLoadInLoopPRE(bool LoadInLoopPRE) {
  88. AllowLoadInLoopPRE = LoadInLoopPRE;
  89. return *this;
  90. }
  91. /// Enables or disables PRE of loads in GVN.
  92. GVNOptions &setLoadPRESplitBackedge(bool LoadPRESplitBackedge) {
  93. AllowLoadPRESplitBackedge = LoadPRESplitBackedge;
  94. return *this;
  95. }
  96. /// Enables or disables use of MemDepAnalysis.
  97. GVNOptions &setMemDep(bool MemDep) {
  98. AllowMemDep = MemDep;
  99. return *this;
  100. }
  101. };
  102. /// The core GVN pass object.
  103. ///
  104. /// FIXME: We should have a good summary of the GVN algorithm implemented by
  105. /// this particular pass here.
  106. class GVN : public PassInfoMixin<GVN> {
  107. GVNOptions Options;
  108. public:
  109. struct Expression;
  110. GVN(GVNOptions Options = {}) : Options(Options) {}
  111. /// Run the pass over the function.
  112. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  113. /// This removes the specified instruction from
  114. /// our various maps and marks it for deletion.
  115. void markInstructionForDeletion(Instruction *I) {
  116. VN.erase(I);
  117. InstrsToErase.push_back(I);
  118. }
  119. DominatorTree &getDominatorTree() const { return *DT; }
  120. AAResults *getAliasAnalysis() const { return VN.getAliasAnalysis(); }
  121. MemoryDependenceResults &getMemDep() const { return *MD; }
  122. bool isPREEnabled() const;
  123. bool isLoadPREEnabled() const;
  124. bool isLoadInLoopPREEnabled() const;
  125. bool isLoadPRESplitBackedgeEnabled() const;
  126. bool isMemDepEnabled() const;
  127. /// This class holds the mapping between values and value numbers. It is used
  128. /// as an efficient mechanism to determine the expression-wise equivalence of
  129. /// two values.
  130. class ValueTable {
  131. DenseMap<Value *, uint32_t> valueNumbering;
  132. DenseMap<Expression, uint32_t> expressionNumbering;
  133. // Expressions is the vector of Expression. ExprIdx is the mapping from
  134. // value number to the index of Expression in Expressions. We use it
  135. // instead of a DenseMap because filling such mapping is faster than
  136. // filling a DenseMap and the compile time is a little better.
  137. uint32_t nextExprNumber = 0;
  138. std::vector<Expression> Expressions;
  139. std::vector<uint32_t> ExprIdx;
  140. // Value number to PHINode mapping. Used for phi-translate in scalarpre.
  141. DenseMap<uint32_t, PHINode *> NumberingPhi;
  142. // Cache for phi-translate in scalarpre.
  143. using PhiTranslateMap =
  144. DenseMap<std::pair<uint32_t, const BasicBlock *>, uint32_t>;
  145. PhiTranslateMap PhiTranslateTable;
  146. AAResults *AA = nullptr;
  147. MemoryDependenceResults *MD = nullptr;
  148. DominatorTree *DT = nullptr;
  149. uint32_t nextValueNumber = 1;
  150. Expression createExpr(Instruction *I);
  151. Expression createCmpExpr(unsigned Opcode, CmpInst::Predicate Predicate,
  152. Value *LHS, Value *RHS);
  153. Expression createExtractvalueExpr(ExtractValueInst *EI);
  154. uint32_t lookupOrAddCall(CallInst *C);
  155. uint32_t phiTranslateImpl(const BasicBlock *BB, const BasicBlock *PhiBlock,
  156. uint32_t Num, GVN &Gvn);
  157. bool areCallValsEqual(uint32_t Num, uint32_t NewNum, const BasicBlock *Pred,
  158. const BasicBlock *PhiBlock, GVN &Gvn);
  159. std::pair<uint32_t, bool> assignExpNewValueNum(Expression &exp);
  160. bool areAllValsInBB(uint32_t num, const BasicBlock *BB, GVN &Gvn);
  161. public:
  162. ValueTable();
  163. ValueTable(const ValueTable &Arg);
  164. ValueTable(ValueTable &&Arg);
  165. ~ValueTable();
  166. ValueTable &operator=(const ValueTable &Arg);
  167. uint32_t lookupOrAdd(Value *V);
  168. uint32_t lookup(Value *V, bool Verify = true) const;
  169. uint32_t lookupOrAddCmp(unsigned Opcode, CmpInst::Predicate Pred,
  170. Value *LHS, Value *RHS);
  171. uint32_t phiTranslate(const BasicBlock *BB, const BasicBlock *PhiBlock,
  172. uint32_t Num, GVN &Gvn);
  173. void eraseTranslateCacheEntry(uint32_t Num, const BasicBlock &CurrBlock);
  174. bool exists(Value *V) const;
  175. void add(Value *V, uint32_t num);
  176. void clear();
  177. void erase(Value *v);
  178. void setAliasAnalysis(AAResults *A) { AA = A; }
  179. AAResults *getAliasAnalysis() const { return AA; }
  180. void setMemDep(MemoryDependenceResults *M) { MD = M; }
  181. void setDomTree(DominatorTree *D) { DT = D; }
  182. uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
  183. void verifyRemoved(const Value *) const;
  184. };
  185. private:
  186. friend class gvn::GVNLegacyPass;
  187. friend struct DenseMapInfo<Expression>;
  188. MemoryDependenceResults *MD = nullptr;
  189. DominatorTree *DT = nullptr;
  190. const TargetLibraryInfo *TLI = nullptr;
  191. AssumptionCache *AC = nullptr;
  192. SetVector<BasicBlock *> DeadBlocks;
  193. OptimizationRemarkEmitter *ORE = nullptr;
  194. ImplicitControlFlowTracking *ICF = nullptr;
  195. LoopInfo *LI = nullptr;
  196. MemorySSAUpdater *MSSAU = nullptr;
  197. ValueTable VN;
  198. /// A mapping from value numbers to lists of Value*'s that
  199. /// have that value number. Use findLeader to query it.
  200. struct LeaderTableEntry {
  201. Value *Val;
  202. const BasicBlock *BB;
  203. LeaderTableEntry *Next;
  204. };
  205. DenseMap<uint32_t, LeaderTableEntry> LeaderTable;
  206. BumpPtrAllocator TableAllocator;
  207. // Block-local map of equivalent values to their leader, does not
  208. // propagate to any successors. Entries added mid-block are applied
  209. // to the remaining instructions in the block.
  210. SmallMapVector<Value *, Value *, 4> ReplaceOperandsWithMap;
  211. SmallVector<Instruction *, 8> InstrsToErase;
  212. // Map the block to reversed postorder traversal number. It is used to
  213. // find back edge easily.
  214. DenseMap<AssertingVH<BasicBlock>, uint32_t> BlockRPONumber;
  215. // This is set 'true' initially and also when new blocks have been added to
  216. // the function being analyzed. This boolean is used to control the updating
  217. // of BlockRPONumber prior to accessing the contents of BlockRPONumber.
  218. bool InvalidBlockRPONumbers = true;
  219. using LoadDepVect = SmallVector<NonLocalDepResult, 64>;
  220. using AvailValInBlkVect = SmallVector<gvn::AvailableValueInBlock, 64>;
  221. using UnavailBlkVect = SmallVector<BasicBlock *, 64>;
  222. bool runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
  223. const TargetLibraryInfo &RunTLI, AAResults &RunAA,
  224. MemoryDependenceResults *RunMD, LoopInfo *LI,
  225. OptimizationRemarkEmitter *ORE, MemorySSA *MSSA = nullptr);
  226. /// Push a new Value to the LeaderTable onto the list for its value number.
  227. void addToLeaderTable(uint32_t N, Value *V, const BasicBlock *BB) {
  228. LeaderTableEntry &Curr = LeaderTable[N];
  229. if (!Curr.Val) {
  230. Curr.Val = V;
  231. Curr.BB = BB;
  232. return;
  233. }
  234. LeaderTableEntry *Node = TableAllocator.Allocate<LeaderTableEntry>();
  235. Node->Val = V;
  236. Node->BB = BB;
  237. Node->Next = Curr.Next;
  238. Curr.Next = Node;
  239. }
  240. /// Scan the list of values corresponding to a given
  241. /// value number, and remove the given instruction if encountered.
  242. void removeFromLeaderTable(uint32_t N, Instruction *I, BasicBlock *BB) {
  243. LeaderTableEntry *Prev = nullptr;
  244. LeaderTableEntry *Curr = &LeaderTable[N];
  245. while (Curr && (Curr->Val != I || Curr->BB != BB)) {
  246. Prev = Curr;
  247. Curr = Curr->Next;
  248. }
  249. if (!Curr)
  250. return;
  251. if (Prev) {
  252. Prev->Next = Curr->Next;
  253. } else {
  254. if (!Curr->Next) {
  255. Curr->Val = nullptr;
  256. Curr->BB = nullptr;
  257. } else {
  258. LeaderTableEntry *Next = Curr->Next;
  259. Curr->Val = Next->Val;
  260. Curr->BB = Next->BB;
  261. Curr->Next = Next->Next;
  262. }
  263. }
  264. }
  265. // List of critical edges to be split between iterations.
  266. SmallVector<std::pair<Instruction *, unsigned>, 4> toSplit;
  267. // Helper functions of redundant load elimination
  268. bool processLoad(LoadInst *L);
  269. bool processNonLocalLoad(LoadInst *L);
  270. bool processAssumeIntrinsic(IntrinsicInst *II);
  271. /// Given a local dependency (Def or Clobber) determine if a value is
  272. /// available for the load. Returns true if an value is known to be
  273. /// available and populates Res. Returns false otherwise.
  274. bool AnalyzeLoadAvailability(LoadInst *LI, MemDepResult DepInfo,
  275. Value *Address, gvn::AvailableValue &Res);
  276. /// Given a list of non-local dependencies, determine if a value is
  277. /// available for the load in each specified block. If it is, add it to
  278. /// ValuesPerBlock. If not, add it to UnavailableBlocks.
  279. void AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
  280. AvailValInBlkVect &ValuesPerBlock,
  281. UnavailBlkVect &UnavailableBlocks);
  282. bool PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
  283. UnavailBlkVect &UnavailableBlocks);
  284. // Other helper routines
  285. bool processInstruction(Instruction *I);
  286. bool processBlock(BasicBlock *BB);
  287. void dump(DenseMap<uint32_t, Value *> &d) const;
  288. bool iterateOnFunction(Function &F);
  289. bool performPRE(Function &F);
  290. bool performScalarPRE(Instruction *I);
  291. bool performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
  292. BasicBlock *Curr, unsigned int ValNo);
  293. Value *findLeader(const BasicBlock *BB, uint32_t num);
  294. void cleanupGlobalSets();
  295. void verifyRemoved(const Instruction *I) const;
  296. bool splitCriticalEdges();
  297. BasicBlock *splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ);
  298. bool replaceOperandsForInBlockEquality(Instruction *I) const;
  299. bool propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root,
  300. bool DominatesByEdge);
  301. bool processFoldableCondBr(BranchInst *BI);
  302. void addDeadBlock(BasicBlock *BB);
  303. void assignValNumForDeadCode();
  304. void assignBlockRPONumber(Function &F);
  305. };
  306. /// Create a legacy GVN pass. This also allows parameterizing whether or not
  307. /// MemDep is enabled.
  308. FunctionPass *createGVNPass(bool NoMemDepAnalysis = false);
  309. /// A simple and fast domtree-based GVN pass to hoist common expressions
  310. /// from sibling branches.
  311. struct GVNHoistPass : PassInfoMixin<GVNHoistPass> {
  312. /// Run the pass over the function.
  313. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  314. };
  315. /// Uses an "inverted" value numbering to decide the similarity of
  316. /// expressions and sinks similar expressions into successors.
  317. struct GVNSinkPass : PassInfoMixin<GVNSinkPass> {
  318. /// Run the pass over the function.
  319. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  320. };
  321. } // end namespace llvm
  322. #endif // LLVM_TRANSFORMS_SCALAR_GVN_H
  323. #ifdef __GNUC__
  324. #pragma GCC diagnostic pop
  325. #endif