GVN.h 14 KB

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