GVNHoist.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. //===- GVNHoist.cpp - Hoist scalar and load expressions -------------------===//
  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. // This pass hoists expressions from branches to a common dominator. It uses
  10. // GVN (global value numbering) to discover expressions computing the same
  11. // values. The primary goals of code-hoisting are:
  12. // 1. To reduce the code size.
  13. // 2. In some cases reduce critical path (by exposing more ILP).
  14. //
  15. // The algorithm factors out the reachability of values such that multiple
  16. // queries to find reachability of values are fast. This is based on finding the
  17. // ANTIC points in the CFG which do not change during hoisting. The ANTIC points
  18. // are basically the dominance-frontiers in the inverse graph. So we introduce a
  19. // data structure (CHI nodes) to keep track of values flowing out of a basic
  20. // block. We only do this for values with multiple occurrences in the function
  21. // as they are the potential hoistable candidates. This approach allows us to
  22. // hoist instructions to a basic block with more than two successors, as well as
  23. // deal with infinite loops in a trivial way.
  24. //
  25. // Limitations: This pass does not hoist fully redundant expressions because
  26. // they are already handled by GVN-PRE. It is advisable to run gvn-hoist before
  27. // and after gvn-pre because gvn-pre creates opportunities for more instructions
  28. // to be hoisted.
  29. //
  30. // Hoisting may affect the performance in some cases. To mitigate that, hoisting
  31. // is disabled in the following cases.
  32. // 1. Scalars across calls.
  33. // 2. geps when corresponding load/store cannot be hoisted.
  34. //===----------------------------------------------------------------------===//
  35. #include "llvm/ADT/DenseMap.h"
  36. #include "llvm/ADT/DenseSet.h"
  37. #include "llvm/ADT/STLExtras.h"
  38. #include "llvm/ADT/SmallPtrSet.h"
  39. #include "llvm/ADT/SmallVector.h"
  40. #include "llvm/ADT/Statistic.h"
  41. #include "llvm/ADT/iterator_range.h"
  42. #include "llvm/Analysis/AliasAnalysis.h"
  43. #include "llvm/Analysis/GlobalsModRef.h"
  44. #include "llvm/Analysis/IteratedDominanceFrontier.h"
  45. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  46. #include "llvm/Analysis/MemorySSA.h"
  47. #include "llvm/Analysis/MemorySSAUpdater.h"
  48. #include "llvm/Analysis/PostDominators.h"
  49. #include "llvm/Analysis/ValueTracking.h"
  50. #include "llvm/IR/Argument.h"
  51. #include "llvm/IR/BasicBlock.h"
  52. #include "llvm/IR/CFG.h"
  53. #include "llvm/IR/Constants.h"
  54. #include "llvm/IR/Dominators.h"
  55. #include "llvm/IR/Function.h"
  56. #include "llvm/IR/InstrTypes.h"
  57. #include "llvm/IR/Instruction.h"
  58. #include "llvm/IR/Instructions.h"
  59. #include "llvm/IR/IntrinsicInst.h"
  60. #include "llvm/IR/Intrinsics.h"
  61. #include "llvm/IR/LLVMContext.h"
  62. #include "llvm/IR/PassManager.h"
  63. #include "llvm/IR/Use.h"
  64. #include "llvm/IR/User.h"
  65. #include "llvm/IR/Value.h"
  66. #include "llvm/InitializePasses.h"
  67. #include "llvm/Pass.h"
  68. #include "llvm/Support/Casting.h"
  69. #include "llvm/Support/CommandLine.h"
  70. #include "llvm/Support/Debug.h"
  71. #include "llvm/Support/raw_ostream.h"
  72. #include "llvm/Transforms/Scalar.h"
  73. #include "llvm/Transforms/Scalar/GVN.h"
  74. #include "llvm/Transforms/Utils/Local.h"
  75. #include <algorithm>
  76. #include <cassert>
  77. #include <iterator>
  78. #include <memory>
  79. #include <utility>
  80. #include <vector>
  81. using namespace llvm;
  82. #define DEBUG_TYPE "gvn-hoist"
  83. STATISTIC(NumHoisted, "Number of instructions hoisted");
  84. STATISTIC(NumRemoved, "Number of instructions removed");
  85. STATISTIC(NumLoadsHoisted, "Number of loads hoisted");
  86. STATISTIC(NumLoadsRemoved, "Number of loads removed");
  87. STATISTIC(NumStoresHoisted, "Number of stores hoisted");
  88. STATISTIC(NumStoresRemoved, "Number of stores removed");
  89. STATISTIC(NumCallsHoisted, "Number of calls hoisted");
  90. STATISTIC(NumCallsRemoved, "Number of calls removed");
  91. static cl::opt<int>
  92. MaxHoistedThreshold("gvn-max-hoisted", cl::Hidden, cl::init(-1),
  93. cl::desc("Max number of instructions to hoist "
  94. "(default unlimited = -1)"));
  95. static cl::opt<int> MaxNumberOfBBSInPath(
  96. "gvn-hoist-max-bbs", cl::Hidden, cl::init(4),
  97. cl::desc("Max number of basic blocks on the path between "
  98. "hoisting locations (default = 4, unlimited = -1)"));
  99. static cl::opt<int> MaxDepthInBB(
  100. "gvn-hoist-max-depth", cl::Hidden, cl::init(100),
  101. cl::desc("Hoist instructions from the beginning of the BB up to the "
  102. "maximum specified depth (default = 100, unlimited = -1)"));
  103. static cl::opt<int>
  104. MaxChainLength("gvn-hoist-max-chain-length", cl::Hidden, cl::init(10),
  105. cl::desc("Maximum length of dependent chains to hoist "
  106. "(default = 10, unlimited = -1)"));
  107. namespace llvm {
  108. using BBSideEffectsSet = DenseMap<const BasicBlock *, bool>;
  109. using SmallVecInsn = SmallVector<Instruction *, 4>;
  110. using SmallVecImplInsn = SmallVectorImpl<Instruction *>;
  111. // Each element of a hoisting list contains the basic block where to hoist and
  112. // a list of instructions to be hoisted.
  113. using HoistingPointInfo = std::pair<BasicBlock *, SmallVecInsn>;
  114. using HoistingPointList = SmallVector<HoistingPointInfo, 4>;
  115. // A map from a pair of VNs to all the instructions with those VNs.
  116. using VNType = std::pair<unsigned, unsigned>;
  117. using VNtoInsns = DenseMap<VNType, SmallVector<Instruction *, 4>>;
  118. // CHI keeps information about values flowing out of a basic block. It is
  119. // similar to PHI but in the inverse graph, and used for outgoing values on each
  120. // edge. For conciseness, it is computed only for instructions with multiple
  121. // occurrences in the CFG because they are the only hoistable candidates.
  122. // A (CHI[{V, B, I1}, {V, C, I2}]
  123. // / \
  124. // / \
  125. // B(I1) C (I2)
  126. // The Value number for both I1 and I2 is V, the CHI node will save the
  127. // instruction as well as the edge where the value is flowing to.
  128. struct CHIArg {
  129. VNType VN;
  130. // Edge destination (shows the direction of flow), may not be where the I is.
  131. BasicBlock *Dest;
  132. // The instruction (VN) which uses the values flowing out of CHI.
  133. Instruction *I;
  134. bool operator==(const CHIArg &A) const { return VN == A.VN; }
  135. bool operator!=(const CHIArg &A) const { return !(*this == A); }
  136. };
  137. using CHIIt = SmallVectorImpl<CHIArg>::iterator;
  138. using CHIArgs = iterator_range<CHIIt>;
  139. using OutValuesType = DenseMap<BasicBlock *, SmallVector<CHIArg, 2>>;
  140. using InValuesType =
  141. DenseMap<BasicBlock *, SmallVector<std::pair<VNType, Instruction *>, 2>>;
  142. // An invalid value number Used when inserting a single value number into
  143. // VNtoInsns.
  144. enum : unsigned { InvalidVN = ~2U };
  145. // Records all scalar instructions candidate for code hoisting.
  146. class InsnInfo {
  147. VNtoInsns VNtoScalars;
  148. public:
  149. // Inserts I and its value number in VNtoScalars.
  150. void insert(Instruction *I, GVNPass::ValueTable &VN) {
  151. // Scalar instruction.
  152. unsigned V = VN.lookupOrAdd(I);
  153. VNtoScalars[{V, InvalidVN}].push_back(I);
  154. }
  155. const VNtoInsns &getVNTable() const { return VNtoScalars; }
  156. };
  157. // Records all load instructions candidate for code hoisting.
  158. class LoadInfo {
  159. VNtoInsns VNtoLoads;
  160. public:
  161. // Insert Load and the value number of its memory address in VNtoLoads.
  162. void insert(LoadInst *Load, GVNPass::ValueTable &VN) {
  163. if (Load->isSimple()) {
  164. unsigned V = VN.lookupOrAdd(Load->getPointerOperand());
  165. VNtoLoads[{V, InvalidVN}].push_back(Load);
  166. }
  167. }
  168. const VNtoInsns &getVNTable() const { return VNtoLoads; }
  169. };
  170. // Records all store instructions candidate for code hoisting.
  171. class StoreInfo {
  172. VNtoInsns VNtoStores;
  173. public:
  174. // Insert the Store and a hash number of the store address and the stored
  175. // value in VNtoStores.
  176. void insert(StoreInst *Store, GVNPass::ValueTable &VN) {
  177. if (!Store->isSimple())
  178. return;
  179. // Hash the store address and the stored value.
  180. Value *Ptr = Store->getPointerOperand();
  181. Value *Val = Store->getValueOperand();
  182. VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store);
  183. }
  184. const VNtoInsns &getVNTable() const { return VNtoStores; }
  185. };
  186. // Records all call instructions candidate for code hoisting.
  187. class CallInfo {
  188. VNtoInsns VNtoCallsScalars;
  189. VNtoInsns VNtoCallsLoads;
  190. VNtoInsns VNtoCallsStores;
  191. public:
  192. // Insert Call and its value numbering in one of the VNtoCalls* containers.
  193. void insert(CallInst *Call, GVNPass::ValueTable &VN) {
  194. // A call that doesNotAccessMemory is handled as a Scalar,
  195. // onlyReadsMemory will be handled as a Load instruction,
  196. // all other calls will be handled as stores.
  197. unsigned V = VN.lookupOrAdd(Call);
  198. auto Entry = std::make_pair(V, InvalidVN);
  199. if (Call->doesNotAccessMemory())
  200. VNtoCallsScalars[Entry].push_back(Call);
  201. else if (Call->onlyReadsMemory())
  202. VNtoCallsLoads[Entry].push_back(Call);
  203. else
  204. VNtoCallsStores[Entry].push_back(Call);
  205. }
  206. const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; }
  207. const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; }
  208. const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; }
  209. };
  210. static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
  211. static const unsigned KnownIDs[] = {LLVMContext::MD_tbaa,
  212. LLVMContext::MD_alias_scope,
  213. LLVMContext::MD_noalias,
  214. LLVMContext::MD_range,
  215. LLVMContext::MD_fpmath,
  216. LLVMContext::MD_invariant_load,
  217. LLVMContext::MD_invariant_group,
  218. LLVMContext::MD_access_group};
  219. combineMetadata(ReplInst, I, KnownIDs, true);
  220. }
  221. // This pass hoists common computations across branches sharing common
  222. // dominator. The primary goal is to reduce the code size, and in some
  223. // cases reduce critical path (by exposing more ILP).
  224. class GVNHoist {
  225. public:
  226. GVNHoist(DominatorTree *DT, PostDominatorTree *PDT, AliasAnalysis *AA,
  227. MemoryDependenceResults *MD, MemorySSA *MSSA)
  228. : DT(DT), PDT(PDT), AA(AA), MD(MD), MSSA(MSSA),
  229. MSSAUpdater(std::make_unique<MemorySSAUpdater>(MSSA)) {}
  230. bool run(Function &F);
  231. // Copied from NewGVN.cpp
  232. // This function provides global ranking of operations so that we can place
  233. // them in a canonical order. Note that rank alone is not necessarily enough
  234. // for a complete ordering, as constants all have the same rank. However,
  235. // generally, we will simplify an operation with all constants so that it
  236. // doesn't matter what order they appear in.
  237. unsigned int rank(const Value *V) const;
  238. private:
  239. GVNPass::ValueTable VN;
  240. DominatorTree *DT;
  241. PostDominatorTree *PDT;
  242. AliasAnalysis *AA;
  243. MemoryDependenceResults *MD;
  244. MemorySSA *MSSA;
  245. std::unique_ptr<MemorySSAUpdater> MSSAUpdater;
  246. DenseMap<const Value *, unsigned> DFSNumber;
  247. BBSideEffectsSet BBSideEffects;
  248. DenseSet<const BasicBlock *> HoistBarrier;
  249. SmallVector<BasicBlock *, 32> IDFBlocks;
  250. unsigned NumFuncArgs;
  251. const bool HoistingGeps = false;
  252. enum InsKind { Unknown, Scalar, Load, Store };
  253. // Return true when there are exception handling in BB.
  254. bool hasEH(const BasicBlock *BB);
  255. // Return true when I1 appears before I2 in the instructions of BB.
  256. bool firstInBB(const Instruction *I1, const Instruction *I2) {
  257. assert(I1->getParent() == I2->getParent());
  258. unsigned I1DFS = DFSNumber.lookup(I1);
  259. unsigned I2DFS = DFSNumber.lookup(I2);
  260. assert(I1DFS && I2DFS);
  261. return I1DFS < I2DFS;
  262. }
  263. // Return true when there are memory uses of Def in BB.
  264. bool hasMemoryUse(const Instruction *NewPt, MemoryDef *Def,
  265. const BasicBlock *BB);
  266. bool hasEHhelper(const BasicBlock *BB, const BasicBlock *SrcBB,
  267. int &NBBsOnAllPaths);
  268. // Return true when there are exception handling or loads of memory Def
  269. // between Def and NewPt. This function is only called for stores: Def is
  270. // the MemoryDef of the store to be hoisted.
  271. // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
  272. // return true when the counter NBBsOnAllPaths reaces 0, except when it is
  273. // initialized to -1 which is unlimited.
  274. bool hasEHOrLoadsOnPath(const Instruction *NewPt, MemoryDef *Def,
  275. int &NBBsOnAllPaths);
  276. // Return true when there are exception handling between HoistPt and BB.
  277. // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
  278. // return true when the counter NBBsOnAllPaths reaches 0, except when it is
  279. // initialized to -1 which is unlimited.
  280. bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *SrcBB,
  281. int &NBBsOnAllPaths);
  282. // Return true when it is safe to hoist a memory load or store U from OldPt
  283. // to NewPt.
  284. bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
  285. MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths);
  286. // Return true when it is safe to hoist scalar instructions from all blocks in
  287. // WL to HoistBB.
  288. bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB,
  289. int &NBBsOnAllPaths) {
  290. return !hasEHOnPath(HoistBB, BB, NBBsOnAllPaths);
  291. }
  292. // In the inverse CFG, the dominance frontier of basic block (BB) is the
  293. // point where ANTIC needs to be computed for instructions which are going
  294. // to be hoisted. Since this point does not change during gvn-hoist,
  295. // we compute it only once (on demand).
  296. // The ides is inspired from:
  297. // "Partial Redundancy Elimination in SSA Form"
  298. // ROBERT KENNEDY, SUN CHAN, SHIN-MING LIU, RAYMOND LO, PENG TU and FRED CHOW
  299. // They use similar idea in the forward graph to find fully redundant and
  300. // partially redundant expressions, here it is used in the inverse graph to
  301. // find fully anticipable instructions at merge point (post-dominator in
  302. // the inverse CFG).
  303. // Returns the edge via which an instruction in BB will get the values from.
  304. // Returns true when the values are flowing out to each edge.
  305. bool valueAnticipable(CHIArgs C, Instruction *TI) const;
  306. // Check if it is safe to hoist values tracked by CHI in the range
  307. // [Begin, End) and accumulate them in Safe.
  308. void checkSafety(CHIArgs C, BasicBlock *BB, InsKind K,
  309. SmallVectorImpl<CHIArg> &Safe);
  310. using RenameStackType = DenseMap<VNType, SmallVector<Instruction *, 2>>;
  311. // Push all the VNs corresponding to BB into RenameStack.
  312. void fillRenameStack(BasicBlock *BB, InValuesType &ValueBBs,
  313. RenameStackType &RenameStack);
  314. void fillChiArgs(BasicBlock *BB, OutValuesType &CHIBBs,
  315. RenameStackType &RenameStack);
  316. // Walk the post-dominator tree top-down and use a stack for each value to
  317. // store the last value you see. When you hit a CHI from a given edge, the
  318. // value to use as the argument is at the top of the stack, add the value to
  319. // CHI and pop.
  320. void insertCHI(InValuesType &ValueBBs, OutValuesType &CHIBBs) {
  321. auto Root = PDT->getNode(nullptr);
  322. if (!Root)
  323. return;
  324. // Depth first walk on PDom tree to fill the CHIargs at each PDF.
  325. for (auto Node : depth_first(Root)) {
  326. BasicBlock *BB = Node->getBlock();
  327. if (!BB)
  328. continue;
  329. RenameStackType RenameStack;
  330. // Collect all values in BB and push to stack.
  331. fillRenameStack(BB, ValueBBs, RenameStack);
  332. // Fill outgoing values in each CHI corresponding to BB.
  333. fillChiArgs(BB, CHIBBs, RenameStack);
  334. }
  335. }
  336. // Walk all the CHI-nodes to find ones which have a empty-entry and remove
  337. // them Then collect all the instructions which are safe to hoist and see if
  338. // they form a list of anticipable values. OutValues contains CHIs
  339. // corresponding to each basic block.
  340. void findHoistableCandidates(OutValuesType &CHIBBs, InsKind K,
  341. HoistingPointList &HPL);
  342. // Compute insertion points for each values which can be fully anticipated at
  343. // a dominator. HPL contains all such values.
  344. void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
  345. InsKind K) {
  346. // Sort VNs based on their rankings
  347. std::vector<VNType> Ranks;
  348. for (const auto &Entry : Map) {
  349. Ranks.push_back(Entry.first);
  350. }
  351. // TODO: Remove fully-redundant expressions.
  352. // Get instruction from the Map, assume that all the Instructions
  353. // with same VNs have same rank (this is an approximation).
  354. llvm::sort(Ranks, [this, &Map](const VNType &r1, const VNType &r2) {
  355. return (rank(*Map.lookup(r1).begin()) < rank(*Map.lookup(r2).begin()));
  356. });
  357. // - Sort VNs according to their rank, and start with lowest ranked VN
  358. // - Take a VN and for each instruction with same VN
  359. // - Find the dominance frontier in the inverse graph (PDF)
  360. // - Insert the chi-node at PDF
  361. // - Remove the chi-nodes with missing entries
  362. // - Remove values from CHI-nodes which do not truly flow out, e.g.,
  363. // modified along the path.
  364. // - Collect the remaining values that are still anticipable
  365. SmallVector<BasicBlock *, 2> IDFBlocks;
  366. ReverseIDFCalculator IDFs(*PDT);
  367. OutValuesType OutValue;
  368. InValuesType InValue;
  369. for (const auto &R : Ranks) {
  370. const SmallVecInsn &V = Map.lookup(R);
  371. if (V.size() < 2)
  372. continue;
  373. const VNType &VN = R;
  374. SmallPtrSet<BasicBlock *, 2> VNBlocks;
  375. for (auto &I : V) {
  376. BasicBlock *BBI = I->getParent();
  377. if (!hasEH(BBI))
  378. VNBlocks.insert(BBI);
  379. }
  380. // Compute the Post Dominance Frontiers of each basic block
  381. // The dominance frontier of a live block X in the reverse
  382. // control graph is the set of blocks upon which X is control
  383. // dependent. The following sequence computes the set of blocks
  384. // which currently have dead terminators that are control
  385. // dependence sources of a block which is in NewLiveBlocks.
  386. IDFs.setDefiningBlocks(VNBlocks);
  387. IDFBlocks.clear();
  388. IDFs.calculate(IDFBlocks);
  389. // Make a map of BB vs instructions to be hoisted.
  390. for (unsigned i = 0; i < V.size(); ++i) {
  391. InValue[V[i]->getParent()].push_back(std::make_pair(VN, V[i]));
  392. }
  393. // Insert empty CHI node for this VN. This is used to factor out
  394. // basic blocks where the ANTIC can potentially change.
  395. CHIArg EmptyChi = {VN, nullptr, nullptr};
  396. for (auto *IDFBB : IDFBlocks) {
  397. for (unsigned i = 0; i < V.size(); ++i) {
  398. // Ignore spurious PDFs.
  399. if (DT->properlyDominates(IDFBB, V[i]->getParent())) {
  400. OutValue[IDFBB].push_back(EmptyChi);
  401. LLVM_DEBUG(dbgs() << "\nInserting a CHI for BB: "
  402. << IDFBB->getName() << ", for Insn: " << *V[i]);
  403. }
  404. }
  405. }
  406. }
  407. // Insert CHI args at each PDF to iterate on factored graph of
  408. // control dependence.
  409. insertCHI(InValue, OutValue);
  410. // Using the CHI args inserted at each PDF, find fully anticipable values.
  411. findHoistableCandidates(OutValue, K, HPL);
  412. }
  413. // Return true when all operands of Instr are available at insertion point
  414. // HoistPt. When limiting the number of hoisted expressions, one could hoist
  415. // a load without hoisting its access function. So before hoisting any
  416. // expression, make sure that all its operands are available at insert point.
  417. bool allOperandsAvailable(const Instruction *I,
  418. const BasicBlock *HoistPt) const;
  419. // Same as allOperandsAvailable with recursive check for GEP operands.
  420. bool allGepOperandsAvailable(const Instruction *I,
  421. const BasicBlock *HoistPt) const;
  422. // Make all operands of the GEP available.
  423. void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
  424. const SmallVecInsn &InstructionsToHoist,
  425. Instruction *Gep) const;
  426. void updateAlignment(Instruction *I, Instruction *Repl);
  427. // Remove all the instructions in Candidates and replace their usage with
  428. // Repl. Returns the number of instructions removed.
  429. unsigned rauw(const SmallVecInsn &Candidates, Instruction *Repl,
  430. MemoryUseOrDef *NewMemAcc);
  431. // Replace all Memory PHI usage with NewMemAcc.
  432. void raMPHIuw(MemoryUseOrDef *NewMemAcc);
  433. // Remove all other instructions and replace them with Repl.
  434. unsigned removeAndReplace(const SmallVecInsn &Candidates, Instruction *Repl,
  435. BasicBlock *DestBB, bool MoveAccess);
  436. // In the case Repl is a load or a store, we make all their GEPs
  437. // available: GEPs are not hoisted by default to avoid the address
  438. // computations to be hoisted without the associated load or store.
  439. bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
  440. const SmallVecInsn &InstructionsToHoist) const;
  441. std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL);
  442. // Hoist all expressions. Returns Number of scalars hoisted
  443. // and number of non-scalars hoisted.
  444. std::pair<unsigned, unsigned> hoistExpressions(Function &F);
  445. };
  446. class GVNHoistLegacyPass : public FunctionPass {
  447. public:
  448. static char ID;
  449. GVNHoistLegacyPass() : FunctionPass(ID) {
  450. initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
  451. }
  452. bool runOnFunction(Function &F) override {
  453. if (skipFunction(F))
  454. return false;
  455. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  456. auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
  457. auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
  458. auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
  459. auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
  460. GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA);
  461. return G.run(F);
  462. }
  463. void getAnalysisUsage(AnalysisUsage &AU) const override {
  464. AU.addRequired<DominatorTreeWrapperPass>();
  465. AU.addRequired<PostDominatorTreeWrapperPass>();
  466. AU.addRequired<AAResultsWrapperPass>();
  467. AU.addRequired<MemoryDependenceWrapperPass>();
  468. AU.addRequired<MemorySSAWrapperPass>();
  469. AU.addPreserved<DominatorTreeWrapperPass>();
  470. AU.addPreserved<MemorySSAWrapperPass>();
  471. AU.addPreserved<GlobalsAAWrapperPass>();
  472. }
  473. };
  474. bool GVNHoist::run(Function &F) {
  475. NumFuncArgs = F.arg_size();
  476. VN.setDomTree(DT);
  477. VN.setAliasAnalysis(AA);
  478. VN.setMemDep(MD);
  479. bool Res = false;
  480. // Perform DFS Numbering of instructions.
  481. unsigned BBI = 0;
  482. for (const BasicBlock *BB : depth_first(&F.getEntryBlock())) {
  483. DFSNumber[BB] = ++BBI;
  484. unsigned I = 0;
  485. for (auto &Inst : *BB)
  486. DFSNumber[&Inst] = ++I;
  487. }
  488. int ChainLength = 0;
  489. // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
  490. while (true) {
  491. if (MaxChainLength != -1 && ++ChainLength >= MaxChainLength)
  492. return Res;
  493. auto HoistStat = hoistExpressions(F);
  494. if (HoistStat.first + HoistStat.second == 0)
  495. return Res;
  496. if (HoistStat.second > 0)
  497. // To address a limitation of the current GVN, we need to rerun the
  498. // hoisting after we hoisted loads or stores in order to be able to
  499. // hoist all scalars dependent on the hoisted ld/st.
  500. VN.clear();
  501. Res = true;
  502. }
  503. return Res;
  504. }
  505. unsigned int GVNHoist::rank(const Value *V) const {
  506. // Prefer constants to undef to anything else
  507. // Undef is a constant, have to check it first.
  508. // Prefer smaller constants to constantexprs
  509. if (isa<ConstantExpr>(V))
  510. return 2;
  511. if (isa<UndefValue>(V))
  512. return 1;
  513. if (isa<Constant>(V))
  514. return 0;
  515. else if (auto *A = dyn_cast<Argument>(V))
  516. return 3 + A->getArgNo();
  517. // Need to shift the instruction DFS by number of arguments + 3 to account
  518. // for the constant and argument ranking above.
  519. auto Result = DFSNumber.lookup(V);
  520. if (Result > 0)
  521. return 4 + NumFuncArgs + Result;
  522. // Unreachable or something else, just return a really large number.
  523. return ~0;
  524. }
  525. bool GVNHoist::hasEH(const BasicBlock *BB) {
  526. auto It = BBSideEffects.find(BB);
  527. if (It != BBSideEffects.end())
  528. return It->second;
  529. if (BB->isEHPad() || BB->hasAddressTaken()) {
  530. BBSideEffects[BB] = true;
  531. return true;
  532. }
  533. if (BB->getTerminator()->mayThrow()) {
  534. BBSideEffects[BB] = true;
  535. return true;
  536. }
  537. BBSideEffects[BB] = false;
  538. return false;
  539. }
  540. bool GVNHoist::hasMemoryUse(const Instruction *NewPt, MemoryDef *Def,
  541. const BasicBlock *BB) {
  542. const MemorySSA::AccessList *Acc = MSSA->getBlockAccesses(BB);
  543. if (!Acc)
  544. return false;
  545. Instruction *OldPt = Def->getMemoryInst();
  546. const BasicBlock *OldBB = OldPt->getParent();
  547. const BasicBlock *NewBB = NewPt->getParent();
  548. bool ReachedNewPt = false;
  549. for (const MemoryAccess &MA : *Acc)
  550. if (const MemoryUse *MU = dyn_cast<MemoryUse>(&MA)) {
  551. Instruction *Insn = MU->getMemoryInst();
  552. // Do not check whether MU aliases Def when MU occurs after OldPt.
  553. if (BB == OldBB && firstInBB(OldPt, Insn))
  554. break;
  555. // Do not check whether MU aliases Def when MU occurs before NewPt.
  556. if (BB == NewBB) {
  557. if (!ReachedNewPt) {
  558. if (firstInBB(Insn, NewPt))
  559. continue;
  560. ReachedNewPt = true;
  561. }
  562. }
  563. if (MemorySSAUtil::defClobbersUseOrDef(Def, MU, *AA))
  564. return true;
  565. }
  566. return false;
  567. }
  568. bool GVNHoist::hasEHhelper(const BasicBlock *BB, const BasicBlock *SrcBB,
  569. int &NBBsOnAllPaths) {
  570. // Stop walk once the limit is reached.
  571. if (NBBsOnAllPaths == 0)
  572. return true;
  573. // Impossible to hoist with exceptions on the path.
  574. if (hasEH(BB))
  575. return true;
  576. // No such instruction after HoistBarrier in a basic block was
  577. // selected for hoisting so instructions selected within basic block with
  578. // a hoist barrier can be hoisted.
  579. if ((BB != SrcBB) && HoistBarrier.count(BB))
  580. return true;
  581. return false;
  582. }
  583. bool GVNHoist::hasEHOrLoadsOnPath(const Instruction *NewPt, MemoryDef *Def,
  584. int &NBBsOnAllPaths) {
  585. const BasicBlock *NewBB = NewPt->getParent();
  586. const BasicBlock *OldBB = Def->getBlock();
  587. assert(DT->dominates(NewBB, OldBB) && "invalid path");
  588. assert(DT->dominates(Def->getDefiningAccess()->getBlock(), NewBB) &&
  589. "def does not dominate new hoisting point");
  590. // Walk all basic blocks reachable in depth-first iteration on the inverse
  591. // CFG from OldBB to NewBB. These blocks are all the blocks that may be
  592. // executed between the execution of NewBB and OldBB. Hoisting an expression
  593. // from OldBB into NewBB has to be safe on all execution paths.
  594. for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
  595. const BasicBlock *BB = *I;
  596. if (BB == NewBB) {
  597. // Stop traversal when reaching HoistPt.
  598. I.skipChildren();
  599. continue;
  600. }
  601. if (hasEHhelper(BB, OldBB, NBBsOnAllPaths))
  602. return true;
  603. // Check that we do not move a store past loads.
  604. if (hasMemoryUse(NewPt, Def, BB))
  605. return true;
  606. // -1 is unlimited number of blocks on all paths.
  607. if (NBBsOnAllPaths != -1)
  608. --NBBsOnAllPaths;
  609. ++I;
  610. }
  611. return false;
  612. }
  613. bool GVNHoist::hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *SrcBB,
  614. int &NBBsOnAllPaths) {
  615. assert(DT->dominates(HoistPt, SrcBB) && "Invalid path");
  616. // Walk all basic blocks reachable in depth-first iteration on
  617. // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
  618. // blocks that may be executed between the execution of NewHoistPt and
  619. // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
  620. // on all execution paths.
  621. for (auto I = idf_begin(SrcBB), E = idf_end(SrcBB); I != E;) {
  622. const BasicBlock *BB = *I;
  623. if (BB == HoistPt) {
  624. // Stop traversal when reaching NewHoistPt.
  625. I.skipChildren();
  626. continue;
  627. }
  628. if (hasEHhelper(BB, SrcBB, NBBsOnAllPaths))
  629. return true;
  630. // -1 is unlimited number of blocks on all paths.
  631. if (NBBsOnAllPaths != -1)
  632. --NBBsOnAllPaths;
  633. ++I;
  634. }
  635. return false;
  636. }
  637. bool GVNHoist::safeToHoistLdSt(const Instruction *NewPt,
  638. const Instruction *OldPt, MemoryUseOrDef *U,
  639. GVNHoist::InsKind K, int &NBBsOnAllPaths) {
  640. // In place hoisting is safe.
  641. if (NewPt == OldPt)
  642. return true;
  643. const BasicBlock *NewBB = NewPt->getParent();
  644. const BasicBlock *OldBB = OldPt->getParent();
  645. const BasicBlock *UBB = U->getBlock();
  646. // Check for dependences on the Memory SSA.
  647. MemoryAccess *D = U->getDefiningAccess();
  648. BasicBlock *DBB = D->getBlock();
  649. if (DT->properlyDominates(NewBB, DBB))
  650. // Cannot move the load or store to NewBB above its definition in DBB.
  651. return false;
  652. if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
  653. if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
  654. if (!firstInBB(UD->getMemoryInst(), NewPt))
  655. // Cannot move the load or store to NewPt above its definition in D.
  656. return false;
  657. // Check for unsafe hoistings due to side effects.
  658. if (K == InsKind::Store) {
  659. if (hasEHOrLoadsOnPath(NewPt, cast<MemoryDef>(U), NBBsOnAllPaths))
  660. return false;
  661. } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
  662. return false;
  663. if (UBB == NewBB) {
  664. if (DT->properlyDominates(DBB, NewBB))
  665. return true;
  666. assert(UBB == DBB);
  667. assert(MSSA->locallyDominates(D, U));
  668. }
  669. // No side effects: it is safe to hoist.
  670. return true;
  671. }
  672. bool GVNHoist::valueAnticipable(CHIArgs C, Instruction *TI) const {
  673. if (TI->getNumSuccessors() > (unsigned)size(C))
  674. return false; // Not enough args in this CHI.
  675. for (auto CHI : C) {
  676. // Find if all the edges have values flowing out of BB.
  677. if (!llvm::is_contained(successors(TI), CHI.Dest))
  678. return false;
  679. }
  680. return true;
  681. }
  682. void GVNHoist::checkSafety(CHIArgs C, BasicBlock *BB, GVNHoist::InsKind K,
  683. SmallVectorImpl<CHIArg> &Safe) {
  684. int NumBBsOnAllPaths = MaxNumberOfBBSInPath;
  685. for (auto CHI : C) {
  686. Instruction *Insn = CHI.I;
  687. if (!Insn) // No instruction was inserted in this CHI.
  688. continue;
  689. if (K == InsKind::Scalar) {
  690. if (safeToHoistScalar(BB, Insn->getParent(), NumBBsOnAllPaths))
  691. Safe.push_back(CHI);
  692. } else {
  693. auto *T = BB->getTerminator();
  694. if (MemoryUseOrDef *UD = MSSA->getMemoryAccess(Insn))
  695. if (safeToHoistLdSt(T, Insn, UD, K, NumBBsOnAllPaths))
  696. Safe.push_back(CHI);
  697. }
  698. }
  699. }
  700. void GVNHoist::fillRenameStack(BasicBlock *BB, InValuesType &ValueBBs,
  701. GVNHoist::RenameStackType &RenameStack) {
  702. auto it1 = ValueBBs.find(BB);
  703. if (it1 != ValueBBs.end()) {
  704. // Iterate in reverse order to keep lower ranked values on the top.
  705. LLVM_DEBUG(dbgs() << "\nVisiting: " << BB->getName()
  706. << " for pushing instructions on stack";);
  707. for (std::pair<VNType, Instruction *> &VI : reverse(it1->second)) {
  708. // Get the value of instruction I
  709. LLVM_DEBUG(dbgs() << "\nPushing on stack: " << *VI.second);
  710. RenameStack[VI.first].push_back(VI.second);
  711. }
  712. }
  713. }
  714. void GVNHoist::fillChiArgs(BasicBlock *BB, OutValuesType &CHIBBs,
  715. GVNHoist::RenameStackType &RenameStack) {
  716. // For each *predecessor* (because Post-DOM) of BB check if it has a CHI
  717. for (auto Pred : predecessors(BB)) {
  718. auto P = CHIBBs.find(Pred);
  719. if (P == CHIBBs.end()) {
  720. continue;
  721. }
  722. LLVM_DEBUG(dbgs() << "\nLooking at CHIs in: " << Pred->getName(););
  723. // A CHI is found (BB -> Pred is an edge in the CFG)
  724. // Pop the stack until Top(V) = Ve.
  725. auto &VCHI = P->second;
  726. for (auto It = VCHI.begin(), E = VCHI.end(); It != E;) {
  727. CHIArg &C = *It;
  728. if (!C.Dest) {
  729. auto si = RenameStack.find(C.VN);
  730. // The Basic Block where CHI is must dominate the value we want to
  731. // track in a CHI. In the PDom walk, there can be values in the
  732. // stack which are not control dependent e.g., nested loop.
  733. if (si != RenameStack.end() && si->second.size() &&
  734. DT->properlyDominates(Pred, si->second.back()->getParent())) {
  735. C.Dest = BB; // Assign the edge
  736. C.I = si->second.pop_back_val(); // Assign the argument
  737. LLVM_DEBUG(dbgs()
  738. << "\nCHI Inserted in BB: " << C.Dest->getName() << *C.I
  739. << ", VN: " << C.VN.first << ", " << C.VN.second);
  740. }
  741. // Move to next CHI of a different value
  742. It = std::find_if(It, VCHI.end(), [It](CHIArg &A) { return A != *It; });
  743. } else
  744. ++It;
  745. }
  746. }
  747. }
  748. void GVNHoist::findHoistableCandidates(OutValuesType &CHIBBs,
  749. GVNHoist::InsKind K,
  750. HoistingPointList &HPL) {
  751. auto cmpVN = [](const CHIArg &A, const CHIArg &B) { return A.VN < B.VN; };
  752. // CHIArgs now have the outgoing values, so check for anticipability and
  753. // accumulate hoistable candidates in HPL.
  754. for (std::pair<BasicBlock *, SmallVector<CHIArg, 2>> &A : CHIBBs) {
  755. BasicBlock *BB = A.first;
  756. SmallVectorImpl<CHIArg> &CHIs = A.second;
  757. // Vector of PHIs contains PHIs for different instructions.
  758. // Sort the args according to their VNs, such that identical
  759. // instructions are together.
  760. llvm::stable_sort(CHIs, cmpVN);
  761. auto TI = BB->getTerminator();
  762. auto B = CHIs.begin();
  763. // [PreIt, PHIIt) form a range of CHIs which have identical VNs.
  764. auto PHIIt = llvm::find_if(CHIs, [B](CHIArg &A) { return A != *B; });
  765. auto PrevIt = CHIs.begin();
  766. while (PrevIt != PHIIt) {
  767. // Collect values which satisfy safety checks.
  768. SmallVector<CHIArg, 2> Safe;
  769. // We check for safety first because there might be multiple values in
  770. // the same path, some of which are not safe to be hoisted, but overall
  771. // each edge has at least one value which can be hoisted, making the
  772. // value anticipable along that path.
  773. checkSafety(make_range(PrevIt, PHIIt), BB, K, Safe);
  774. // List of safe values should be anticipable at TI.
  775. if (valueAnticipable(make_range(Safe.begin(), Safe.end()), TI)) {
  776. HPL.push_back({BB, SmallVecInsn()});
  777. SmallVecInsn &V = HPL.back().second;
  778. for (auto B : Safe)
  779. V.push_back(B.I);
  780. }
  781. // Check other VNs
  782. PrevIt = PHIIt;
  783. PHIIt = std::find_if(PrevIt, CHIs.end(),
  784. [PrevIt](CHIArg &A) { return A != *PrevIt; });
  785. }
  786. }
  787. }
  788. bool GVNHoist::allOperandsAvailable(const Instruction *I,
  789. const BasicBlock *HoistPt) const {
  790. for (const Use &Op : I->operands())
  791. if (const auto *Inst = dyn_cast<Instruction>(&Op))
  792. if (!DT->dominates(Inst->getParent(), HoistPt))
  793. return false;
  794. return true;
  795. }
  796. bool GVNHoist::allGepOperandsAvailable(const Instruction *I,
  797. const BasicBlock *HoistPt) const {
  798. for (const Use &Op : I->operands())
  799. if (const auto *Inst = dyn_cast<Instruction>(&Op))
  800. if (!DT->dominates(Inst->getParent(), HoistPt)) {
  801. if (const GetElementPtrInst *GepOp =
  802. dyn_cast<GetElementPtrInst>(Inst)) {
  803. if (!allGepOperandsAvailable(GepOp, HoistPt))
  804. return false;
  805. // Gep is available if all operands of GepOp are available.
  806. } else {
  807. // Gep is not available if it has operands other than GEPs that are
  808. // defined in blocks not dominating HoistPt.
  809. return false;
  810. }
  811. }
  812. return true;
  813. }
  814. void GVNHoist::makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
  815. const SmallVecInsn &InstructionsToHoist,
  816. Instruction *Gep) const {
  817. assert(allGepOperandsAvailable(Gep, HoistPt) && "GEP operands not available");
  818. Instruction *ClonedGep = Gep->clone();
  819. for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i)
  820. if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) {
  821. // Check whether the operand is already available.
  822. if (DT->dominates(Op->getParent(), HoistPt))
  823. continue;
  824. // As a GEP can refer to other GEPs, recursively make all the operands
  825. // of this GEP available at HoistPt.
  826. if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op))
  827. makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp);
  828. }
  829. // Copy Gep and replace its uses in Repl with ClonedGep.
  830. ClonedGep->insertBefore(HoistPt->getTerminator());
  831. // Conservatively discard any optimization hints, they may differ on the
  832. // other paths.
  833. ClonedGep->dropUnknownNonDebugMetadata();
  834. // If we have optimization hints which agree with each other along different
  835. // paths, preserve them.
  836. for (const Instruction *OtherInst : InstructionsToHoist) {
  837. const GetElementPtrInst *OtherGep;
  838. if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
  839. OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
  840. else
  841. OtherGep = cast<GetElementPtrInst>(
  842. cast<StoreInst>(OtherInst)->getPointerOperand());
  843. ClonedGep->andIRFlags(OtherGep);
  844. }
  845. // Replace uses of Gep with ClonedGep in Repl.
  846. Repl->replaceUsesOfWith(Gep, ClonedGep);
  847. }
  848. void GVNHoist::updateAlignment(Instruction *I, Instruction *Repl) {
  849. if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
  850. ReplacementLoad->setAlignment(
  851. std::min(ReplacementLoad->getAlign(), cast<LoadInst>(I)->getAlign()));
  852. ++NumLoadsRemoved;
  853. } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
  854. ReplacementStore->setAlignment(
  855. std::min(ReplacementStore->getAlign(), cast<StoreInst>(I)->getAlign()));
  856. ++NumStoresRemoved;
  857. } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
  858. ReplacementAlloca->setAlignment(std::max(ReplacementAlloca->getAlign(),
  859. cast<AllocaInst>(I)->getAlign()));
  860. } else if (isa<CallInst>(Repl)) {
  861. ++NumCallsRemoved;
  862. }
  863. }
  864. unsigned GVNHoist::rauw(const SmallVecInsn &Candidates, Instruction *Repl,
  865. MemoryUseOrDef *NewMemAcc) {
  866. unsigned NR = 0;
  867. for (Instruction *I : Candidates) {
  868. if (I != Repl) {
  869. ++NR;
  870. updateAlignment(I, Repl);
  871. if (NewMemAcc) {
  872. // Update the uses of the old MSSA access with NewMemAcc.
  873. MemoryAccess *OldMA = MSSA->getMemoryAccess(I);
  874. OldMA->replaceAllUsesWith(NewMemAcc);
  875. MSSAUpdater->removeMemoryAccess(OldMA);
  876. }
  877. Repl->andIRFlags(I);
  878. combineKnownMetadata(Repl, I);
  879. I->replaceAllUsesWith(Repl);
  880. // Also invalidate the Alias Analysis cache.
  881. MD->removeInstruction(I);
  882. I->eraseFromParent();
  883. }
  884. }
  885. return NR;
  886. }
  887. void GVNHoist::raMPHIuw(MemoryUseOrDef *NewMemAcc) {
  888. SmallPtrSet<MemoryPhi *, 4> UsePhis;
  889. for (User *U : NewMemAcc->users())
  890. if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(U))
  891. UsePhis.insert(Phi);
  892. for (MemoryPhi *Phi : UsePhis) {
  893. auto In = Phi->incoming_values();
  894. if (llvm::all_of(In, [&](Use &U) { return U == NewMemAcc; })) {
  895. Phi->replaceAllUsesWith(NewMemAcc);
  896. MSSAUpdater->removeMemoryAccess(Phi);
  897. }
  898. }
  899. }
  900. unsigned GVNHoist::removeAndReplace(const SmallVecInsn &Candidates,
  901. Instruction *Repl, BasicBlock *DestBB,
  902. bool MoveAccess) {
  903. MemoryUseOrDef *NewMemAcc = MSSA->getMemoryAccess(Repl);
  904. if (MoveAccess && NewMemAcc) {
  905. // The definition of this ld/st will not change: ld/st hoisting is
  906. // legal when the ld/st is not moved past its current definition.
  907. MSSAUpdater->moveToPlace(NewMemAcc, DestBB, MemorySSA::BeforeTerminator);
  908. }
  909. // Replace all other instructions with Repl with memory access NewMemAcc.
  910. unsigned NR = rauw(Candidates, Repl, NewMemAcc);
  911. // Remove MemorySSA phi nodes with the same arguments.
  912. if (NewMemAcc)
  913. raMPHIuw(NewMemAcc);
  914. return NR;
  915. }
  916. bool GVNHoist::makeGepOperandsAvailable(
  917. Instruction *Repl, BasicBlock *HoistPt,
  918. const SmallVecInsn &InstructionsToHoist) const {
  919. // Check whether the GEP of a ld/st can be synthesized at HoistPt.
  920. GetElementPtrInst *Gep = nullptr;
  921. Instruction *Val = nullptr;
  922. if (auto *Ld = dyn_cast<LoadInst>(Repl)) {
  923. Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
  924. } else if (auto *St = dyn_cast<StoreInst>(Repl)) {
  925. Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
  926. Val = dyn_cast<Instruction>(St->getValueOperand());
  927. // Check that the stored value is available.
  928. if (Val) {
  929. if (isa<GetElementPtrInst>(Val)) {
  930. // Check whether we can compute the GEP at HoistPt.
  931. if (!allGepOperandsAvailable(Val, HoistPt))
  932. return false;
  933. } else if (!DT->dominates(Val->getParent(), HoistPt))
  934. return false;
  935. }
  936. }
  937. // Check whether we can compute the Gep at HoistPt.
  938. if (!Gep || !allGepOperandsAvailable(Gep, HoistPt))
  939. return false;
  940. makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep);
  941. if (Val && isa<GetElementPtrInst>(Val))
  942. makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val);
  943. return true;
  944. }
  945. std::pair<unsigned, unsigned> GVNHoist::hoist(HoistingPointList &HPL) {
  946. unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
  947. for (const HoistingPointInfo &HP : HPL) {
  948. // Find out whether we already have one of the instructions in HoistPt,
  949. // in which case we do not have to move it.
  950. BasicBlock *DestBB = HP.first;
  951. const SmallVecInsn &InstructionsToHoist = HP.second;
  952. Instruction *Repl = nullptr;
  953. for (Instruction *I : InstructionsToHoist)
  954. if (I->getParent() == DestBB)
  955. // If there are two instructions in HoistPt to be hoisted in place:
  956. // update Repl to be the first one, such that we can rename the uses
  957. // of the second based on the first.
  958. if (!Repl || firstInBB(I, Repl))
  959. Repl = I;
  960. // Keep track of whether we moved the instruction so we know whether we
  961. // should move the MemoryAccess.
  962. bool MoveAccess = true;
  963. if (Repl) {
  964. // Repl is already in HoistPt: it remains in place.
  965. assert(allOperandsAvailable(Repl, DestBB) &&
  966. "instruction depends on operands that are not available");
  967. MoveAccess = false;
  968. } else {
  969. // When we do not find Repl in HoistPt, select the first in the list
  970. // and move it to HoistPt.
  971. Repl = InstructionsToHoist.front();
  972. // We can move Repl in HoistPt only when all operands are available.
  973. // The order in which hoistings are done may influence the availability
  974. // of operands.
  975. if (!allOperandsAvailable(Repl, DestBB)) {
  976. // When HoistingGeps there is nothing more we can do to make the
  977. // operands available: just continue.
  978. if (HoistingGeps)
  979. continue;
  980. // When not HoistingGeps we need to copy the GEPs.
  981. if (!makeGepOperandsAvailable(Repl, DestBB, InstructionsToHoist))
  982. continue;
  983. }
  984. // Move the instruction at the end of HoistPt.
  985. Instruction *Last = DestBB->getTerminator();
  986. MD->removeInstruction(Repl);
  987. Repl->moveBefore(Last);
  988. DFSNumber[Repl] = DFSNumber[Last]++;
  989. }
  990. NR += removeAndReplace(InstructionsToHoist, Repl, DestBB, MoveAccess);
  991. if (isa<LoadInst>(Repl))
  992. ++NL;
  993. else if (isa<StoreInst>(Repl))
  994. ++NS;
  995. else if (isa<CallInst>(Repl))
  996. ++NC;
  997. else // Scalar
  998. ++NI;
  999. }
  1000. if (MSSA && VerifyMemorySSA)
  1001. MSSA->verifyMemorySSA();
  1002. NumHoisted += NL + NS + NC + NI;
  1003. NumRemoved += NR;
  1004. NumLoadsHoisted += NL;
  1005. NumStoresHoisted += NS;
  1006. NumCallsHoisted += NC;
  1007. return {NI, NL + NC + NS};
  1008. }
  1009. std::pair<unsigned, unsigned> GVNHoist::hoistExpressions(Function &F) {
  1010. InsnInfo II;
  1011. LoadInfo LI;
  1012. StoreInfo SI;
  1013. CallInfo CI;
  1014. for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
  1015. int InstructionNb = 0;
  1016. for (Instruction &I1 : *BB) {
  1017. // If I1 cannot guarantee progress, subsequent instructions
  1018. // in BB cannot be hoisted anyways.
  1019. if (!isGuaranteedToTransferExecutionToSuccessor(&I1)) {
  1020. HoistBarrier.insert(BB);
  1021. break;
  1022. }
  1023. // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting
  1024. // deeper may increase the register pressure and compilation time.
  1025. if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB)
  1026. break;
  1027. // Do not value number terminator instructions.
  1028. if (I1.isTerminator())
  1029. break;
  1030. if (auto *Load = dyn_cast<LoadInst>(&I1))
  1031. LI.insert(Load, VN);
  1032. else if (auto *Store = dyn_cast<StoreInst>(&I1))
  1033. SI.insert(Store, VN);
  1034. else if (auto *Call = dyn_cast<CallInst>(&I1)) {
  1035. if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
  1036. if (isa<DbgInfoIntrinsic>(Intr) ||
  1037. Intr->getIntrinsicID() == Intrinsic::assume ||
  1038. Intr->getIntrinsicID() == Intrinsic::sideeffect)
  1039. continue;
  1040. }
  1041. if (Call->mayHaveSideEffects())
  1042. break;
  1043. if (Call->isConvergent())
  1044. break;
  1045. CI.insert(Call, VN);
  1046. } else if (HoistingGeps || !isa<GetElementPtrInst>(&I1))
  1047. // Do not hoist scalars past calls that may write to memory because
  1048. // that could result in spills later. geps are handled separately.
  1049. // TODO: We can relax this for targets like AArch64 as they have more
  1050. // registers than X86.
  1051. II.insert(&I1, VN);
  1052. }
  1053. }
  1054. HoistingPointList HPL;
  1055. computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
  1056. computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
  1057. computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
  1058. computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
  1059. computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
  1060. computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
  1061. return hoist(HPL);
  1062. }
  1063. } // end namespace llvm
  1064. PreservedAnalyses GVNHoistPass::run(Function &F, FunctionAnalysisManager &AM) {
  1065. DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
  1066. PostDominatorTree &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
  1067. AliasAnalysis &AA = AM.getResult<AAManager>(F);
  1068. MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
  1069. MemorySSA &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
  1070. GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA);
  1071. if (!G.run(F))
  1072. return PreservedAnalyses::all();
  1073. PreservedAnalyses PA;
  1074. PA.preserve<DominatorTreeAnalysis>();
  1075. PA.preserve<MemorySSAAnalysis>();
  1076. return PA;
  1077. }
  1078. char GVNHoistLegacyPass::ID = 0;
  1079. INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
  1080. "Early GVN Hoisting of Expressions", false, false)
  1081. INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
  1082. INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
  1083. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  1084. INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
  1085. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  1086. INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
  1087. "Early GVN Hoisting of Expressions", false, false)
  1088. FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }