GVN.h 14 KB

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