GVNHoist.cpp 47 KB

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