Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 1 | //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===// |
| 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 pass performs lightweight instruction simplification on loop bodies. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "loop-instsimplify" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
Jakub Staszak | 4fa5793 | 2013-02-09 01:04:28 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/InstructionSimplify.h" |
Cameron Zwarich | a1cb585 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopPass.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetLibraryInfo.h" |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Local.h" |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(NumSimplified, "Number of redundant instructions simplified"); |
| 30 | |
| 31 | namespace { |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 32 | class LoopInstSimplify : public LoopPass { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 33 | public: |
| 34 | static char ID; // Pass ID, replacement for typeid |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 35 | LoopInstSimplify() : LoopPass(ID) { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 36 | initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry()); |
| 37 | } |
| 38 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 39 | bool runOnLoop(Loop*, LPPassManager&) override; |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 40 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 41 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 42 | AU.setPreservesCFG(); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 43 | AU.addRequired<LoopInfo>(); |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 44 | AU.addRequiredID(LoopSimplifyID); |
Cameron Zwarich | e7d7865 | 2011-01-09 12:35:16 +0000 | [diff] [blame] | 45 | AU.addPreservedID(LoopSimplifyID); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 46 | AU.addPreservedID(LCSSAID); |
Cameron Zwarich | fae0abe | 2011-02-11 06:08:25 +0000 | [diff] [blame] | 47 | AU.addPreserved("scalar-evolution"); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 48 | AU.addRequired<TargetLibraryInfo>(); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 52 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 53 | char LoopInstSimplify::ID = 0; |
| 54 | INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify", |
| 55 | "Simplify instructions in loops", false, false) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 56 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 57 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 58 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 59 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 60 | INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify", |
| 61 | "Simplify instructions in loops", false, false) |
| 62 | |
Cameron Zwarich | b434acb | 2011-01-08 17:07:11 +0000 | [diff] [blame] | 63 | Pass *llvm::createLoopInstSimplifyPass() { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 64 | return new LoopInstSimplify(); |
| 65 | } |
| 66 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 67 | bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 68 | if (skipOptnoneFunction(L)) |
| 69 | return false; |
| 70 | |
| 71 | DominatorTreeWrapperPass *DTWP = |
| 72 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 73 | DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0; |
Cameron Zwarich | 64573ae | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 74 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 75 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 76 | const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 77 | const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>(); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 78 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 79 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 80 | L->getUniqueExitBlocks(ExitBlocks); |
| 81 | array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 82 | |
Cameron Zwarich | 08602ab | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 83 | SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
| 84 | |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 85 | // The bit we are stealing from the pointer represents whether this basic |
| 86 | // block is the header of a subloop, in which case we only process its phis. |
| 87 | typedef PointerIntPair<BasicBlock*, 1> WorklistItem; |
| 88 | SmallVector<WorklistItem, 16> VisitStack; |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 89 | SmallPtrSet<BasicBlock*, 32> Visited; |
| 90 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 91 | bool Changed = false; |
| 92 | bool LocalChanged; |
| 93 | do { |
| 94 | LocalChanged = false; |
| 95 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 96 | VisitStack.clear(); |
| 97 | Visited.clear(); |
| 98 | |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 99 | VisitStack.push_back(WorklistItem(L->getHeader(), false)); |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 100 | |
| 101 | while (!VisitStack.empty()) { |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 102 | WorklistItem Item = VisitStack.pop_back_val(); |
Cameron Zwarich | b434acb | 2011-01-08 17:07:11 +0000 | [diff] [blame] | 103 | BasicBlock *BB = Item.getPointer(); |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 104 | bool IsSubloopHeader = Item.getInt(); |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 105 | |
| 106 | // Simplify instructions in the current basic block. |
| 107 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Cameron Zwarich | 64573ae | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 108 | Instruction *I = BI++; |
Cameron Zwarich | 08602ab | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 109 | |
| 110 | // The first time through the loop ToSimplify is empty and we try to |
| 111 | // simplify all instructions. On later iterations ToSimplify is not |
| 112 | // empty and we only bother simplifying instructions that are in it. |
| 113 | if (!ToSimplify->empty() && !ToSimplify->count(I)) |
| 114 | continue; |
| 115 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 116 | // Don't bother simplifying unused instructions. |
| 117 | if (!I->use_empty()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 118 | Value *V = SimplifyInstruction(I, DL, TLI, DT); |
Cameron Zwarich | a1cb585 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 119 | if (V && LI->replacementPreservesLCSSAForm(I, V)) { |
Cameron Zwarich | 08602ab | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 120 | // Mark all uses for resimplification next time round the loop. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 121 | for (User *U : I->users()) |
| 122 | Next->insert(cast<Instruction>(U)); |
Cameron Zwarich | 08602ab | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 123 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 124 | I->replaceAllUsesWith(V); |
| 125 | LocalChanged = true; |
| 126 | ++NumSimplified; |
| 127 | } |
| 128 | } |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 129 | LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I, TLI); |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 130 | |
| 131 | if (IsSubloopHeader && !isa<PHINode>(I)) |
| 132 | break; |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 133 | } |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 134 | |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 135 | // Add all successors to the worklist, except for loop exit blocks and the |
| 136 | // bodies of subloops. We visit the headers of loops so that we can process |
| 137 | // their phis, but we contract the rest of the subloop body and only follow |
| 138 | // edges leading back to the original loop. |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 139 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; |
| 140 | ++SI) { |
| 141 | BasicBlock *SuccBB = *SI; |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 142 | if (!Visited.insert(SuccBB)) |
| 143 | continue; |
| 144 | |
| 145 | const Loop *SuccLoop = LI->getLoopFor(SuccBB); |
| 146 | if (SuccLoop && SuccLoop->getHeader() == SuccBB |
| 147 | && L->contains(SuccLoop)) { |
| 148 | VisitStack.push_back(WorklistItem(SuccBB, true)); |
| 149 | |
| 150 | SmallVector<BasicBlock*, 8> SubLoopExitBlocks; |
| 151 | SuccLoop->getExitBlocks(SubLoopExitBlocks); |
| 152 | |
| 153 | for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) { |
| 154 | BasicBlock *ExitBB = SubLoopExitBlocks[i]; |
| 155 | if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB)) |
| 156 | VisitStack.push_back(WorklistItem(ExitBB, false)); |
| 157 | } |
| 158 | |
| 159 | continue; |
| 160 | } |
| 161 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 162 | bool IsExitBlock = std::binary_search(ExitBlocks.begin(), |
Cameron Zwarich | 8368ac3 | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 163 | ExitBlocks.end(), SuccBB); |
| 164 | if (IsExitBlock) |
| 165 | continue; |
| 166 | |
| 167 | VisitStack.push_back(WorklistItem(SuccBB, false)); |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Cameron Zwarich | 08602ab | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 171 | // Place the list of instructions to simplify on the next loop iteration |
| 172 | // into ToSimplify. |
| 173 | std::swap(ToSimplify, Next); |
| 174 | Next->clear(); |
| 175 | |
Cameron Zwarich | a1cb585 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 176 | Changed |= LocalChanged; |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 177 | } while (LocalChanged); |
| 178 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 179 | return Changed; |
| 180 | } |