IVUsers.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //===- IVUsers.cpp - Induction Variable Users -------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements bookkeeping for "interesting" users of expressions
  11. // computed from induction variables.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/Analysis/AssumptionCache.h"
  16. #include "llvm/Analysis/CodeMetrics.h"
  17. #include "llvm/Analysis/IVUsers.h"
  18. #include "llvm/Analysis/LoopPass.h"
  19. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  20. #include "llvm/Analysis/ValueTracking.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/DerivedTypes.h"
  24. #include "llvm/IR/Dominators.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/Type.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include <algorithm>
  31. using namespace llvm;
  32. #define DEBUG_TYPE "iv-users"
  33. char IVUsers::ID = 0;
  34. INITIALIZE_PASS_BEGIN(IVUsers, "iv-users",
  35. "Induction Variable Users", false, true)
  36. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  37. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  38. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  39. INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
  40. INITIALIZE_PASS_END(IVUsers, "iv-users",
  41. "Induction Variable Users", false, true)
  42. Pass *llvm::createIVUsersPass() {
  43. return new IVUsers();
  44. }
  45. /// isInteresting - Test whether the given expression is "interesting" when
  46. /// used by the given expression, within the context of analyzing the
  47. /// given loop.
  48. static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
  49. ScalarEvolution *SE, LoopInfo *LI) {
  50. // An addrec is interesting if it's affine or if it has an interesting start.
  51. if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
  52. // Keep things simple. Don't touch loop-variant strides unless they're
  53. // only used outside the loop and we can simplify them.
  54. if (AR->getLoop() == L)
  55. return AR->isAffine() ||
  56. (!L->contains(I) &&
  57. SE->getSCEVAtScope(AR, LI->getLoopFor(I->getParent())) != AR);
  58. // Otherwise recurse to see if the start value is interesting, and that
  59. // the step value is not interesting, since we don't yet know how to
  60. // do effective SCEV expansions for addrecs with interesting steps.
  61. return isInteresting(AR->getStart(), I, L, SE, LI) &&
  62. !isInteresting(AR->getStepRecurrence(*SE), I, L, SE, LI);
  63. }
  64. // An add is interesting if exactly one of its operands is interesting.
  65. if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
  66. bool AnyInterestingYet = false;
  67. for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
  68. OI != OE; ++OI)
  69. if (isInteresting(*OI, I, L, SE, LI)) {
  70. if (AnyInterestingYet)
  71. return false;
  72. AnyInterestingYet = true;
  73. }
  74. return AnyInterestingYet;
  75. }
  76. // Nothing else is interesting here.
  77. return false;
  78. }
  79. /// Return true if all loop headers that dominate this block are in simplified
  80. /// form.
  81. static bool isSimplifiedLoopNest(BasicBlock *BB, const DominatorTree *DT,
  82. const LoopInfo *LI,
  83. SmallPtrSetImpl<Loop*> &SimpleLoopNests) {
  84. Loop *NearestLoop = nullptr;
  85. for (DomTreeNode *Rung = DT->getNode(BB);
  86. Rung; Rung = Rung->getIDom()) {
  87. BasicBlock *DomBB = Rung->getBlock();
  88. Loop *DomLoop = LI->getLoopFor(DomBB);
  89. if (DomLoop && DomLoop->getHeader() == DomBB) {
  90. // If the domtree walk reaches a loop with no preheader, return false.
  91. if (!DomLoop->isLoopSimplifyForm())
  92. return false;
  93. // If we have already checked this loop nest, stop checking.
  94. if (SimpleLoopNests.count(DomLoop))
  95. break;
  96. // If we have not already checked this loop nest, remember the loop
  97. // header nearest to BB. The nearest loop may not contain BB.
  98. if (!NearestLoop)
  99. NearestLoop = DomLoop;
  100. }
  101. }
  102. if (NearestLoop)
  103. SimpleLoopNests.insert(NearestLoop);
  104. return true;
  105. }
  106. /// AddUsersImpl - Inspect the specified instruction. If it is a
  107. /// reducible SCEV, recursively add its users to the IVUsesByStride set and
  108. /// return true. Otherwise, return false.
  109. bool IVUsers::AddUsersImpl(Instruction *I,
  110. SmallPtrSetImpl<Loop*> &SimpleLoopNests) {
  111. const DataLayout &DL = I->getModule()->getDataLayout();
  112. // Add this IV user to the Processed set before returning false to ensure that
  113. // all IV users are members of the set. See IVUsers::isIVUserOrOperand.
  114. if (!Processed.insert(I).second)
  115. return true; // Instruction already handled.
  116. if (!SE->isSCEVable(I->getType()))
  117. return false; // Void and FP expressions cannot be reduced.
  118. // IVUsers is used by LSR which assumes that all SCEV expressions are safe to
  119. // pass to SCEVExpander. Expressions are not safe to expand if they represent
  120. // operations that are not safe to speculate, namely integer division.
  121. if (!isa<PHINode>(I) && !isSafeToSpeculativelyExecute(I))
  122. return false;
  123. // LSR is not APInt clean, do not touch integers bigger than 64-bits.
  124. // Also avoid creating IVs of non-native types. For example, we don't want a
  125. // 64-bit IV in 32-bit code just because the loop has one 64-bit cast.
  126. uint64_t Width = SE->getTypeSizeInBits(I->getType());
  127. if (Width > 64 || !DL.isLegalInteger(Width))
  128. return false;
  129. // Don't attempt to promote ephemeral values to indvars. They will be removed
  130. // later anyway.
  131. if (EphValues.count(I))
  132. return false;
  133. // Get the symbolic expression for this instruction.
  134. const SCEV *ISE = SE->getSCEV(I);
  135. // If we've come to an uninteresting expression, stop the traversal and
  136. // call this a user.
  137. if (!isInteresting(ISE, I, L, SE, LI))
  138. return false;
  139. SmallPtrSet<Instruction *, 4> UniqueUsers;
  140. for (Use &U : I->uses()) {
  141. Instruction *User = cast<Instruction>(U.getUser());
  142. if (!UniqueUsers.insert(User).second)
  143. continue;
  144. // Do not infinitely recurse on PHI nodes.
  145. if (isa<PHINode>(User) && Processed.count(User))
  146. continue;
  147. // Only consider IVUsers that are dominated by simplified loop
  148. // headers. Otherwise, SCEVExpander will crash.
  149. BasicBlock *UseBB = User->getParent();
  150. // A phi's use is live out of its predecessor block.
  151. if (PHINode *PHI = dyn_cast<PHINode>(User)) {
  152. unsigned OperandNo = U.getOperandNo();
  153. unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
  154. UseBB = PHI->getIncomingBlock(ValNo);
  155. }
  156. if (!isSimplifiedLoopNest(UseBB, DT, LI, SimpleLoopNests))
  157. return false;
  158. // Descend recursively, but not into PHI nodes outside the current loop.
  159. // It's important to see the entire expression outside the loop to get
  160. // choices that depend on addressing mode use right, although we won't
  161. // consider references outside the loop in all cases.
  162. // If User is already in Processed, we don't want to recurse into it again,
  163. // but do want to record a second reference in the same instruction.
  164. bool AddUserToIVUsers = false;
  165. if (LI->getLoopFor(User->getParent()) != L) {
  166. if (isa<PHINode>(User) || Processed.count(User) ||
  167. !AddUsersImpl(User, SimpleLoopNests)) {
  168. DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
  169. << " OF SCEV: " << *ISE << '\n');
  170. AddUserToIVUsers = true;
  171. }
  172. } else if (Processed.count(User) || !AddUsersImpl(User, SimpleLoopNests)) {
  173. DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
  174. << " OF SCEV: " << *ISE << '\n');
  175. AddUserToIVUsers = true;
  176. }
  177. if (AddUserToIVUsers) {
  178. // Okay, we found a user that we cannot reduce.
  179. IVStrideUse &NewUse = AddUser(User, I);
  180. // Autodetect the post-inc loop set, populating NewUse.PostIncLoops.
  181. // The regular return value here is discarded; instead of recording
  182. // it, we just recompute it when we need it.
  183. const SCEV *OriginalISE = ISE;
  184. ISE = TransformForPostIncUse(NormalizeAutodetect,
  185. ISE, User, I,
  186. NewUse.PostIncLoops,
  187. *SE, *DT);
  188. // PostIncNormalization effectively simplifies the expression under
  189. // pre-increment assumptions. Those assumptions (no wrapping) might not
  190. // hold for the post-inc value. Catch such cases by making sure the
  191. // transformation is invertible.
  192. if (OriginalISE != ISE) {
  193. const SCEV *DenormalizedISE =
  194. TransformForPostIncUse(Denormalize, ISE, User, I,
  195. NewUse.PostIncLoops, *SE, *DT);
  196. // If we normalized the expression, but denormalization doesn't give the
  197. // original one, discard this user.
  198. if (OriginalISE != DenormalizedISE) {
  199. DEBUG(dbgs() << " DISCARDING (NORMALIZATION ISN'T INVERTIBLE): "
  200. << *ISE << '\n');
  201. IVUses.pop_back();
  202. return false;
  203. }
  204. }
  205. DEBUG(if (SE->getSCEV(I) != ISE)
  206. dbgs() << " NORMALIZED TO: " << *ISE << '\n');
  207. }
  208. }
  209. return true;
  210. }
  211. bool IVUsers::AddUsersIfInteresting(Instruction *I) {
  212. // SCEVExpander can only handle users that are dominated by simplified loop
  213. // entries. Keep track of all loops that are only dominated by other simple
  214. // loops so we don't traverse the domtree for each user.
  215. SmallPtrSet<Loop*,16> SimpleLoopNests;
  216. return AddUsersImpl(I, SimpleLoopNests);
  217. }
  218. IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
  219. IVUses.push_back(new IVStrideUse(this, User, Operand));
  220. return IVUses.back();
  221. }
  222. IVUsers::IVUsers()
  223. : LoopPass(ID) {
  224. initializeIVUsersPass(*PassRegistry::getPassRegistry());
  225. }
  226. void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
  227. AU.addRequired<AssumptionCacheTracker>();
  228. AU.addRequired<LoopInfoWrapperPass>();
  229. AU.addRequired<DominatorTreeWrapperPass>();
  230. AU.addRequired<ScalarEvolution>();
  231. AU.setPreservesAll();
  232. }
  233. bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
  234. L = l;
  235. AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
  236. *L->getHeader()->getParent());
  237. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  238. DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  239. SE = &getAnalysis<ScalarEvolution>();
  240. // Collect ephemeral values so that AddUsersIfInteresting skips them.
  241. EphValues.clear();
  242. CodeMetrics::collectEphemeralValues(L, AC, EphValues);
  243. // Find all uses of induction variables in this loop, and categorize
  244. // them by stride. Start by finding all of the PHI nodes in the header for
  245. // this loop. If they are induction variables, inspect their uses.
  246. for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
  247. (void)AddUsersIfInteresting(I);
  248. return false;
  249. }
  250. void IVUsers::print(raw_ostream &OS, const Module *M) const {
  251. OS << "IV Users for loop ";
  252. L->getHeader()->printAsOperand(OS, false);
  253. if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
  254. OS << " with backedge-taken count "
  255. << *SE->getBackedgeTakenCount(L);
  256. }
  257. OS << ":\n";
  258. for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(),
  259. E = IVUses.end(); UI != E; ++UI) {
  260. OS << " ";
  261. UI->getOperandValToReplace()->printAsOperand(OS, false);
  262. OS << " = " << *getReplacementExpr(*UI);
  263. for (PostIncLoopSet::const_iterator
  264. I = UI->PostIncLoops.begin(),
  265. E = UI->PostIncLoops.end(); I != E; ++I) {
  266. OS << " (post-inc with loop ";
  267. (*I)->getHeader()->printAsOperand(OS, false);
  268. OS << ")";
  269. }
  270. OS << " in ";
  271. if (UI->getUser())
  272. UI->getUser()->print(OS);
  273. else
  274. OS << "Printing <null> User";
  275. OS << '\n';
  276. }
  277. }
  278. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  279. void IVUsers::dump() const {
  280. print(dbgs());
  281. }
  282. #endif
  283. void IVUsers::releaseMemory() {
  284. Processed.clear();
  285. IVUses.clear();
  286. }
  287. /// getReplacementExpr - Return a SCEV expression which computes the
  288. /// value of the OperandValToReplace.
  289. const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {
  290. return SE->getSCEV(IU.getOperandValToReplace());
  291. }
  292. /// getExpr - Return the expression for the use.
  293. const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
  294. return
  295. TransformForPostIncUse(Normalize, getReplacementExpr(IU),
  296. IU.getUser(), IU.getOperandValToReplace(),
  297. const_cast<PostIncLoopSet &>(IU.getPostIncLoops()),
  298. *SE, *DT);
  299. }
  300. static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
  301. if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
  302. if (AR->getLoop() == L)
  303. return AR;
  304. return findAddRecForLoop(AR->getStart(), L);
  305. }
  306. if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
  307. for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
  308. I != E; ++I)
  309. if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L))
  310. return AR;
  311. return nullptr;
  312. }
  313. return nullptr;
  314. }
  315. const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const {
  316. if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(IU), L))
  317. return AR->getStepRecurrence(*SE);
  318. return nullptr;
  319. }
  320. void IVStrideUse::transformToPostInc(const Loop *L) {
  321. PostIncLoops.insert(L);
  322. }
  323. void IVStrideUse::deleted() {
  324. // Remove this user from the list.
  325. Parent->Processed.erase(this->getUser());
  326. Parent->IVUses.erase(this);
  327. // this now dangles!
  328. }