Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 1 | //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements LoopPass and LPPassManager. All loop optimization |
| 11 | // and transformation passes are derived from LoopPass. LPPassManager is |
| 12 | // responsible for managing LoopPasses. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Analysis/LoopPass.h" |
Chandler Carruth | c68d25f | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopAnalysisManager.h" |
Igor Laevsky | 22eba5a | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 8a5351f | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IRPrintingPasses.h" |
Juergen Ributzka | 9bc1b73 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 21 | #include "llvm/IR/OptBisect.h" |
Justin Bogner | 32968f1 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 22 | #include "llvm/IR/PassManager.h" |
Fedor Sergeev | 9f6be22 | 2018-08-28 21:06:51 +0000 | [diff] [blame] | 23 | #include "llvm/IR/PassTimingInfo.h" |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Timer.h" |
Benjamin Kramer | df93f4b | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 283b399 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "loop-pass-manager" |
| 30 | |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | /// PrintLoopPass - Print a Function corresponding to a Loop. |
| 34 | /// |
Justin Bogner | 32968f1 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 35 | class PrintLoopPassWrapper : public LoopPass { |
Chandler Carruth | d27a39a | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 36 | raw_ostream &OS; |
| 37 | std::string Banner; |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
| 40 | static char ID; |
Chandler Carruth | d27a39a | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 41 | PrintLoopPassWrapper() : LoopPass(ID), OS(dbgs()) {} |
Justin Bogner | 32968f1 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 42 | PrintLoopPassWrapper(raw_ostream &OS, const std::string &Banner) |
Chandler Carruth | d27a39a | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 43 | : LoopPass(ID), OS(OS), Banner(Banner) {} |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 44 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 45 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 46 | AU.setPreservesAll(); |
| 47 | } |
| 48 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 49 | bool runOnLoop(Loop *L, LPPassManager &) override { |
Chandler Carruth | f5f6185 | 2017-11-17 19:58:36 +0000 | [diff] [blame] | 50 | auto BBI = llvm::find_if(L->blocks(), [](BasicBlock *BB) { return BB; }); |
Weiming Zhao | b85410e | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 51 | if (BBI != L->blocks().end() && |
Chandler Carruth | 04d0fe9 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 52 | isFunctionInPrintList((*BBI)->getParent()->getName())) { |
Chandler Carruth | d27a39a | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 53 | printLoop(*L, OS, Banner); |
Chandler Carruth | 04d0fe9 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 54 | } |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
Yaron Keren | ea56368 | 2017-03-10 07:09:20 +0000 | [diff] [blame] | 57 | |
| 58 | StringRef getPassName() const override { return "Print Loop IR"; } |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Justin Bogner | 32968f1 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 61 | char PrintLoopPassWrapper::ID = 0; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 62 | } |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 63 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // LPPassManager |
| 66 | // |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 67 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 68 | char LPPassManager::ID = 0; |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 69 | |
Andrew Trick | 0e122d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 70 | LPPassManager::LPPassManager() |
| 71 | : FunctionPass(ID), PMDataManager() { |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 72 | LI = nullptr; |
| 73 | CurrentLoop = nullptr; |
Devang Patel | d0e6e33 | 2007-02-22 23:30:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chandler Carruth | 9f6280a | 2017-05-25 03:01:31 +0000 | [diff] [blame] | 76 | // Insert loop into loop nest (LoopInfo) and loop queue (LQ). |
| 77 | void LPPassManager::addLoop(Loop &L) { |
| 78 | if (!L.getParentLoop()) { |
Justin Bogner | ca9d3aa | 2015-10-22 21:21:32 +0000 | [diff] [blame] | 79 | // This is the top level loop. |
Chandler Carruth | 9f6280a | 2017-05-25 03:01:31 +0000 | [diff] [blame] | 80 | LQ.push_front(&L); |
| 81 | return; |
Justin Bogner | ca9d3aa | 2015-10-22 21:21:32 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Justin Bogner | ca9d3aa | 2015-10-22 21:21:32 +0000 | [diff] [blame] | 84 | // Insert L into the loop queue after the parent loop. |
| 85 | for (auto I = LQ.begin(), E = LQ.end(); I != E; ++I) { |
Chandler Carruth | 9f6280a | 2017-05-25 03:01:31 +0000 | [diff] [blame] | 86 | if (*I == L.getParentLoop()) { |
Justin Bogner | ca9d3aa | 2015-10-22 21:21:32 +0000 | [diff] [blame] | 87 | // deque does not support insert after. |
| 88 | ++I; |
Chandler Carruth | 9f6280a | 2017-05-25 03:01:31 +0000 | [diff] [blame] | 89 | LQ.insert(I, 1, &L); |
| 90 | return; |
Devang Patel | a885c06 | 2007-03-06 19:00:02 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 95 | /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for |
| 96 | /// all loop passes. |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 97 | void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From, |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 98 | BasicBlock *To, Loop *L) { |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 99 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Dan Gohman | bd4d66d | 2010-08-11 20:34:43 +0000 | [diff] [blame] | 100 | LoopPass *LP = getContainedPass(Index); |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 101 | LP->cloneBasicBlockAnalysis(From, To, L); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes. |
| 106 | void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) { |
Devang Patel | 575ec80 | 2009-03-25 23:57:48 +0000 | [diff] [blame] | 107 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Benjamin Kramer | 8d0d2b6 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 108 | for (Instruction &I : *BB) { |
Devang Patel | 575ec80 | 2009-03-25 23:57:48 +0000 | [diff] [blame] | 109 | deleteSimpleAnalysisValue(&I, L); |
| 110 | } |
| 111 | } |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 112 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Dan Gohman | bd4d66d | 2010-08-11 20:34:43 +0000 | [diff] [blame] | 113 | LoopPass *LP = getContainedPass(Index); |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 114 | LP->deleteAnalysisValue(V, L); |
| 115 | } |
| 116 | } |
| 117 | |
David Peixotto | cfc4296 | 2014-09-24 16:48:31 +0000 | [diff] [blame] | 118 | /// Invoke deleteAnalysisLoop hook for all passes. |
| 119 | void LPPassManager::deleteSimpleAnalysisLoop(Loop *L) { |
| 120 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 121 | LoopPass *LP = getContainedPass(Index); |
| 122 | LP->deleteAnalysisLoop(L); |
| 123 | } |
| 124 | } |
| 125 | |
Devang Patel | c7e49c0 | 2007-07-31 08:00:57 +0000 | [diff] [blame] | 126 | |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 127 | // Recurse through all subloops and all loops into LQ. |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame] | 128 | static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) { |
Devang Patel | 622adea | 2007-03-06 19:50:49 +0000 | [diff] [blame] | 129 | LQ.push_back(L); |
David Majnemer | 55bd48f | 2016-07-19 17:50:24 +0000 | [diff] [blame] | 130 | for (Loop *I : reverse(*L)) |
| 131 | addLoopIntoQueue(I, LQ); |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 134 | /// Pass Manager itself does not invalidate any analysis info. |
| 135 | void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const { |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 136 | // LPPassManager needs LoopInfo. In the long term LoopInfo class will |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 137 | // become part of LPPassManager. |
Chandler Carruth | de5df29 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 138 | Info.addRequired<LoopInfoWrapperPass>(); |
Igor Laevsky | 22eba5a | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 139 | Info.addRequired<DominatorTreeWrapperPass>(); |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 140 | Info.setPreservesAll(); |
| 141 | } |
| 142 | |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 143 | void LPPassManager::markLoopAsDeleted(Loop &L) { |
| 144 | assert((&L == CurrentLoop || CurrentLoop->contains(&L)) && |
| 145 | "Must not delete loop outside the current loop tree!"); |
Chandler Carruth | 762b38d | 2018-06-22 02:43:41 +0000 | [diff] [blame] | 146 | // If this loop appears elsewhere within the queue, we also need to remove it |
| 147 | // there. However, we have to be careful to not remove the back of the queue |
| 148 | // as that is assumed to match the current loop. |
| 149 | assert(LQ.back() == CurrentLoop && "Loop queue back isn't the current loop!"); |
| 150 | LQ.erase(std::remove(LQ.begin(), LQ.end(), &L), LQ.end()); |
| 151 | |
| 152 | if (&L == CurrentLoop) { |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 153 | CurrentLoopDeleted = true; |
Chandler Carruth | 762b38d | 2018-06-22 02:43:41 +0000 | [diff] [blame] | 154 | // Add this loop back onto the back of the queue to preserve our invariants. |
| 155 | LQ.push_back(&L); |
| 156 | } |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 159 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 160 | /// whether any of the passes modifies the function, and if so, return true. |
| 161 | bool LPPassManager::runOnFunction(Function &F) { |
Chandler Carruth | de5df29 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 162 | auto &LIWP = getAnalysis<LoopInfoWrapperPass>(); |
| 163 | LI = &LIWP.getLoopInfo(); |
Jessica Paquette | 849da55 | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 164 | Module &M = *F.getParent(); |
Michael Zolotukhin | 9cac4d0 | 2018-02-07 04:24:44 +0000 | [diff] [blame] | 165 | #if 0 |
Igor Laevsky | 22eba5a | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 166 | DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Michael Zolotukhin | 811a2d3 | 2018-02-07 00:13:08 +0000 | [diff] [blame] | 167 | #endif |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 168 | bool Changed = false; |
| 169 | |
Devang Patel | 70c09c5 | 2008-07-03 07:02:30 +0000 | [diff] [blame] | 170 | // Collect inherited analysis from Module level pass manager. |
| 171 | populateInheritedAnalysis(TPM->activeStack); |
| 172 | |
Andrew Trick | c9b1e25 | 2012-06-26 04:11:38 +0000 | [diff] [blame] | 173 | // Populate the loop queue in reverse program order. There is no clear need to |
| 174 | // process sibling loops in either forward or reverse order. There may be some |
| 175 | // advantage in deleting uses in a later loop before optimizing the |
| 176 | // definitions in an earlier loop. If we find a clear reason to process in |
| 177 | // forward order, then a forward variant of LoopPassManager should be created. |
Andrew Trick | 360fef5 | 2013-07-20 23:10:31 +0000 | [diff] [blame] | 178 | // |
| 179 | // Note that LoopInfo::iterator visits loops in reverse program |
| 180 | // order. Here, reverse_iterator gives us a forward order, and the LoopQueue |
| 181 | // reverses the order a third time by popping from the back. |
David Majnemer | 55bd48f | 2016-07-19 17:50:24 +0000 | [diff] [blame] | 182 | for (Loop *L : reverse(*LI)) |
| 183 | addLoopIntoQueue(L, LQ); |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 184 | |
Torok Edwin | 1970a89 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 185 | if (LQ.empty()) // No loops, skip calling finalizers |
| 186 | return false; |
| 187 | |
Devang Patel | a5057d0 | 2007-03-06 16:59:03 +0000 | [diff] [blame] | 188 | // Initialization |
David Majnemer | 55bd48f | 2016-07-19 17:50:24 +0000 | [diff] [blame] | 189 | for (Loop *L : LQ) { |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 190 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Dan Gohman | bd4d66d | 2010-08-11 20:34:43 +0000 | [diff] [blame] | 191 | LoopPass *P = getContainedPass(Index); |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 192 | Changed |= P->doInitialization(L, *this); |
Devang Patel | a5057d0 | 2007-03-06 16:59:03 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 196 | // Walk Loops |
Jessica Paquette | 6a73860 | 2018-08-31 20:20:54 +0000 | [diff] [blame] | 197 | unsigned InstrCount, FunctionSize = 0; |
Jessica Paquette | 9e21280 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 198 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | a747d61 | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 199 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Jessica Paquette | 6a73860 | 2018-08-31 20:20:54 +0000 | [diff] [blame] | 200 | // Collect the initial size of the module and the function we're looking at. |
| 201 | if (EmitICRemark) { |
Jessica Paquette | 9e21280 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 202 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Jessica Paquette | 6a73860 | 2018-08-31 20:20:54 +0000 | [diff] [blame] | 203 | FunctionSize = F.getInstructionCount(); |
| 204 | } |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame] | 205 | while (!LQ.empty()) { |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 206 | CurrentLoopDeleted = false; |
Justin Bogner | 6da3c1a | 2015-12-16 00:01:02 +0000 | [diff] [blame] | 207 | CurrentLoop = LQ.back(); |
Justin Bogner | fbbc16f | 2016-01-08 19:08:53 +0000 | [diff] [blame] | 208 | |
Dan Gohman | 1edaef6 | 2009-09-27 23:52:07 +0000 | [diff] [blame] | 209 | // Run all passes on the current Loop. |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 210 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Dan Gohman | bd4d66d | 2010-08-11 20:34:43 +0000 | [diff] [blame] | 211 | LoopPass *P = getContainedPass(Index); |
Eric Christopher | 9e7e609 | 2012-03-23 03:54:05 +0000 | [diff] [blame] | 212 | |
Dan Gohman | 6d594a9 | 2009-09-28 15:07:18 +0000 | [diff] [blame] | 213 | dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, |
Benjamin Kramer | 41d43eb | 2010-03-31 16:06:22 +0000 | [diff] [blame] | 214 | CurrentLoop->getHeader()->getName()); |
Chris Lattner | 0dabb7e | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 215 | dumpRequiredSet(P); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 216 | |
| 217 | initializeAnalysisImpl(P); |
Eric Christopher | 9e7e609 | 2012-03-23 03:54:05 +0000 | [diff] [blame] | 218 | |
Fedor Sergeev | b7c75ac | 2018-11-19 15:10:59 +0000 | [diff] [blame] | 219 | bool LocalChanged = false; |
Chris Lattner | d6f1658 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 220 | { |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 221 | PassManagerPrettyStackEntry X(P, *CurrentLoop->getHeader()); |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 222 | TimeRegion PassTimer(getPassTimer(P)); |
Fedor Sergeev | b7c75ac | 2018-11-19 15:10:59 +0000 | [diff] [blame] | 223 | LocalChanged = P->runOnLoop(CurrentLoop, *this); |
| 224 | Changed |= LocalChanged; |
Jessica Paquette | 6a73860 | 2018-08-31 20:20:54 +0000 | [diff] [blame] | 225 | if (EmitICRemark) { |
| 226 | unsigned NewSize = F.getInstructionCount(); |
| 227 | // Update the size of the function, emit a remark, and update the |
| 228 | // size of the module. |
| 229 | if (NewSize != FunctionSize) { |
Jessica Paquette | 6a73860 | 2018-08-31 20:20:54 +0000 | [diff] [blame] | 230 | int64_t Delta = static_cast<int64_t>(NewSize) - |
| 231 | static_cast<int64_t>(FunctionSize); |
Jessica Paquette | 9e21280 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 232 | emitInstrCountChangedRemark(P, M, Delta, InstrCount, |
| 233 | FunctionToInstrCount, &F); |
Jessica Paquette | 6a73860 | 2018-08-31 20:20:54 +0000 | [diff] [blame] | 234 | InstrCount = static_cast<int64_t>(InstrCount) + Delta; |
| 235 | FunctionSize = NewSize; |
| 236 | } |
| 237 | } |
Chris Lattner | d6f1658 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 238 | } |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 239 | |
Fedor Sergeev | b7c75ac | 2018-11-19 15:10:59 +0000 | [diff] [blame] | 240 | if (LocalChanged) |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 241 | dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, |
| 242 | CurrentLoopDeleted ? "<deleted loop>" |
| 243 | : CurrentLoop->getName()); |
Chris Lattner | 0dabb7e | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 244 | dumpPreservedSet(P); |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 245 | |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 246 | if (CurrentLoopDeleted) { |
Justin Bogner | 6da3c1a | 2015-12-16 00:01:02 +0000 | [diff] [blame] | 247 | // Notify passes that the loop is being deleted. |
| 248 | deleteSimpleAnalysisLoop(CurrentLoop); |
| 249 | } else { |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 250 | // Manually check that this loop is still healthy. This is done |
| 251 | // instead of relying on LoopInfo::verifyLoop since LoopInfo |
| 252 | // is a function pass and it's really expensive to verify every |
| 253 | // loop in the function every time. That level of checking can be |
| 254 | // enabled with the -verify-loop-info option. |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 255 | { |
Chandler Carruth | de5df29 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 256 | TimeRegion PassTimer(getPassTimer(&LIWP)); |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 257 | CurrentLoop->verifyLoop(); |
| 258 | } |
Igor Laevsky | 22eba5a | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 259 | // Here we apply same reasoning as in the above case. Only difference |
| 260 | // is that LPPassManager might run passes which do not require LCSSA |
| 261 | // form (LoopPassPrinter for example). We should skip verification for |
| 262 | // such passes. |
Michael Zolotukhin | 9cac4d0 | 2018-02-07 04:24:44 +0000 | [diff] [blame] | 263 | // FIXME: Loop-sink currently break LCSSA. Fix it and reenable the |
| 264 | // verification! |
| 265 | #if 0 |
Igor Laevsky | 22eba5a | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 266 | if (mustPreserveAnalysisID(LCSSAVerificationPass::ID)) |
Michael Zolotukhin | 811a2d3 | 2018-02-07 00:13:08 +0000 | [diff] [blame] | 267 | assert(CurrentLoop->isRecursivelyLCSSAForm(*DT, *LI)); |
Michael Zolotukhin | 9cac4d0 | 2018-02-07 04:24:44 +0000 | [diff] [blame] | 268 | #endif |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 269 | |
| 270 | // Then call the regular verifyAnalysis functions. |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 271 | verifyPreservedAnalysis(P); |
Juergen Ributzka | 9bc1b73 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 272 | |
| 273 | F.getContext().yield(); |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 274 | } |
Dan Gohman | dd12de6 | 2009-09-03 15:09:24 +0000 | [diff] [blame] | 275 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 276 | removeNotPreservedAnalysis(P); |
| 277 | recordAvailableAnalysis(P); |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 278 | removeDeadPasses(P, |
| 279 | CurrentLoopDeleted ? "<deleted>" |
| 280 | : CurrentLoop->getHeader()->getName(), |
Dan Gohman | 6d594a9 | 2009-09-28 15:07:18 +0000 | [diff] [blame] | 281 | ON_LOOP_MSG); |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 282 | |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 283 | if (CurrentLoopDeleted) |
Devang Patel | 5afdc7d | 2007-02-23 00:10:16 +0000 | [diff] [blame] | 284 | // Do not run other passes on this loop. |
| 285 | break; |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 286 | } |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 287 | |
Dan Gohman | 9702901 | 2009-09-27 23:43:07 +0000 | [diff] [blame] | 288 | // If the loop was deleted, release all the loop passes. This frees up |
| 289 | // some memory, and avoids trouble with the pass manager trying to call |
| 290 | // verifyAnalysis on them. |
Sanjoy Das | e87cf87 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 291 | if (CurrentLoopDeleted) { |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 292 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Dan Gohman | 9702901 | 2009-09-27 23:43:07 +0000 | [diff] [blame] | 293 | Pass *P = getContainedPass(Index); |
Dan Gohman | 6d594a9 | 2009-09-28 15:07:18 +0000 | [diff] [blame] | 294 | freePass(P, "<deleted>", ON_LOOP_MSG); |
Dan Gohman | 9702901 | 2009-09-27 23:43:07 +0000 | [diff] [blame] | 295 | } |
Justin Bogner | 6da3c1a | 2015-12-16 00:01:02 +0000 | [diff] [blame] | 296 | } |
Dan Gohman | 9702901 | 2009-09-27 23:43:07 +0000 | [diff] [blame] | 297 | |
Devang Patel | 643a79b | 2007-02-22 23:45:15 +0000 | [diff] [blame] | 298 | // Pop the loop from queue after running all passes. |
Devang Patel | 3015972 | 2007-03-06 02:30:46 +0000 | [diff] [blame] | 299 | LQ.pop_back(); |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 300 | } |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 301 | |
Devang Patel | a5057d0 | 2007-03-06 16:59:03 +0000 | [diff] [blame] | 302 | // Finalization |
| 303 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Dan Gohman | bd4d66d | 2010-08-11 20:34:43 +0000 | [diff] [blame] | 304 | LoopPass *P = getContainedPass(Index); |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 305 | Changed |= P->doFinalization(); |
Devang Patel | a5057d0 | 2007-03-06 16:59:03 +0000 | [diff] [blame] | 306 | } |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 307 | |
| 308 | return Changed; |
| 309 | } |
| 310 | |
Dan Gohman | 189c635 | 2009-02-17 19:41:26 +0000 | [diff] [blame] | 311 | /// Print passes managed by this manager |
| 312 | void LPPassManager::dumpPassStructure(unsigned Offset) { |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 313 | errs().indent(Offset*2) << "Loop Pass Manager\n"; |
Dan Gohman | 189c635 | 2009-02-17 19:41:26 +0000 | [diff] [blame] | 314 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 315 | Pass *P = getContainedPass(Index); |
Dan Gohman | 8a757ae | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 316 | P->dumpPassStructure(Offset + 1); |
Dan Gohman | 189c635 | 2009-02-17 19:41:26 +0000 | [diff] [blame] | 317 | dumpLastUses(P, Offset+1); |
| 318 | } |
| 319 | } |
| 320 | |
Devang Patel | 16a31c4 | 2007-02-22 08:56:17 +0000 | [diff] [blame] | 321 | |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 322 | //===----------------------------------------------------------------------===// |
| 323 | // LoopPass |
| 324 | |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 325 | Pass *LoopPass::createPrinterPass(raw_ostream &O, |
| 326 | const std::string &Banner) const { |
Justin Bogner | 32968f1 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 327 | return new PrintLoopPassWrapper(O, Banner); |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Devang Patel | 22033be | 2007-03-06 17:59:37 +0000 | [diff] [blame] | 330 | // Check if this pass is suitable for the current LPPassManager, if |
| 331 | // available. This pass P is not suitable for a LPPassManager if P |
| 332 | // is not preserving higher level analysis info used by other |
| 333 | // LPPassManager passes. In such case, pop LPPassManager from the |
| 334 | // stack. This will force assignPassManager() to create new |
| 335 | // LPPassManger as expected. |
| 336 | void LoopPass::preparePassManager(PMStack &PMS) { |
| 337 | |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 338 | // Find LPPassManager |
Duncan Sands | 20d824b | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 339 | while (!PMS.empty() && |
| 340 | PMS.top()->getPassManagerType() > PMT_LoopPassManager) |
| 341 | PMS.pop(); |
Devang Patel | 22033be | 2007-03-06 17:59:37 +0000 | [diff] [blame] | 342 | |
Devang Patel | 22033be | 2007-03-06 17:59:37 +0000 | [diff] [blame] | 343 | // If this pass is destroying high level information that is used |
| 344 | // by other passes that are managed by LPM then do not insert |
| 345 | // this pass in current LPM. Use new LPPassManager. |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 346 | if (PMS.top()->getPassManagerType() == PMT_LoopPassManager && |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 347 | !PMS.top()->preserveHigherLevelAnalysis(this)) |
Devang Patel | 22033be | 2007-03-06 17:59:37 +0000 | [diff] [blame] | 348 | PMS.pop(); |
| 349 | } |
| 350 | |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 351 | /// Assign pass manager to manage this pass. |
| 352 | void LoopPass::assignPassManager(PMStack &PMS, |
| 353 | PassManagerType PreferredType) { |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 354 | // Find LPPassManager |
Duncan Sands | 20d824b | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 355 | while (!PMS.empty() && |
| 356 | PMS.top()->getPassManagerType() > PMT_LoopPassManager) |
| 357 | PMS.pop(); |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 359 | LPPassManager *LPPM; |
| 360 | if (PMS.top()->getPassManagerType() == PMT_LoopPassManager) |
| 361 | LPPM = (LPPassManager*)PMS.top(); |
| 362 | else { |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 363 | // Create new Loop Pass Manager if it does not exist. |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 364 | assert (!PMS.empty() && "Unable to create Loop Pass Manager"); |
| 365 | PMDataManager *PMD = PMS.top(); |
| 366 | |
Andrew Trick | 0e122d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 367 | // [1] Create new Loop Pass Manager |
| 368 | LPPM = new LPPassManager(); |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 369 | LPPM->populateInheritedAnalysis(PMS); |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 370 | |
| 371 | // [2] Set up new manager's top level manager |
| 372 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 373 | TPM->addIndirectPassManager(LPPM); |
| 374 | |
| 375 | // [3] Assign manager to manage this new manager. This may create |
| 376 | // and push new managers into PMS |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 377 | Pass *P = LPPM->getAsPass(); |
Devang Patel | c37177e | 2007-03-06 19:11:25 +0000 | [diff] [blame] | 378 | TPM->schedulePass(P); |
Devang Patel | bfd5905 | 2007-02-23 00:36:57 +0000 | [diff] [blame] | 379 | |
| 380 | // [4] Push new manager into PMS |
| 381 | PMS.push(LPPM); |
| 382 | } |
| 383 | |
| 384 | LPPM->add(this); |
| 385 | } |
Paul Robinson | 2684ddd | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 386 | |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 387 | bool LoopPass::skipLoop(const Loop *L) const { |
Paul Robinson | cf84b5b | 2014-02-26 01:23:26 +0000 | [diff] [blame] | 388 | const Function *F = L->getHeader()->getParent(); |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 389 | if (!F) |
| 390 | return false; |
| 391 | // Check the opt bisect limit. |
| 392 | LLVMContext &Context = F->getContext(); |
Fedor Sergeev | 558f76f | 2018-03-27 16:57:20 +0000 | [diff] [blame] | 393 | if (!Context.getOptPassGate().shouldRunPass(this, *L)) |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 394 | return true; |
| 395 | // Check for the OptimizeNone attribute. |
| 396 | if (F->hasFnAttribute(Attribute::OptimizeNone)) { |
Paul Robinson | 2684ddd | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 397 | // FIXME: Report this to dbgs() only once per function. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 398 | LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' in function " |
| 399 | << F->getName() << "\n"); |
Paul Robinson | 2684ddd | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 400 | // FIXME: Delete loop from pass manager's queue? |
| 401 | return true; |
| 402 | } |
| 403 | return false; |
| 404 | } |
Igor Laevsky | 22eba5a | 2016-10-28 12:57:20 +0000 | [diff] [blame] | 405 | |
| 406 | char LCSSAVerificationPass::ID = 0; |
| 407 | INITIALIZE_PASS(LCSSAVerificationPass, "lcssa-verification", "LCSSA Verifier", |
| 408 | false, false) |