blob: ab1a9393c526c7c05377945ea4364405544a42ea [file] [log] [blame]
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +00001//===- 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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Transforms/Scalar.h"
Jakub Staszakf23980a2013-02-09 01:04:28 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/Statistic.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000017#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwariche9249692011-01-04 00:12:46 +000018#include "llvm/Analysis/LoopInfo.h"
Cameron Zwarich4c51d122011-01-05 05:15:53 +000019#include "llvm/Analysis/LoopPass.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000021#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/Debug.h"
Chad Rosierc24b86f2011-12-01 03:08:23 +000024#include "llvm/Target/TargetLibraryInfo.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000025#include "llvm/Transforms/Utils/Local.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000026using namespace llvm;
27
Chandler Carruth964daaa2014-04-22 02:55:47 +000028#define DEBUG_TYPE "loop-instsimplify"
29
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000030STATISTIC(NumSimplified, "Number of redundant instructions simplified");
31
32namespace {
Cameron Zwarich4c51d122011-01-05 05:15:53 +000033 class LoopInstSimplify : public LoopPass {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000034 public:
35 static char ID; // Pass ID, replacement for typeid
Cameron Zwarich4c51d122011-01-05 05:15:53 +000036 LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000037 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
38 }
39
Craig Topper3e4c6972014-03-05 09:10:37 +000040 bool runOnLoop(Loop*, LPPassManager&) override;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000041
Craig Topper3e4c6972014-03-05 09:10:37 +000042 void getAnalysisUsage(AnalysisUsage &AU) const override {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000043 AU.setPreservesCFG();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000044 AU.addRequired<LoopInfo>();
Cameron Zwarich4c51d122011-01-05 05:15:53 +000045 AU.addRequiredID(LoopSimplifyID);
Cameron Zwaricha42e5912011-01-09 12:35:16 +000046 AU.addPreservedID(LoopSimplifyID);
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000047 AU.addPreservedID(LCSSAID);
Cameron Zwarich25cb63c2011-02-11 06:08:25 +000048 AU.addPreserved("scalar-evolution");
Chad Rosierc24b86f2011-12-01 03:08:23 +000049 AU.addRequired<TargetLibraryInfo>();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000050 }
51 };
52}
Nadav Rotem465834c2012-07-24 10:51:42 +000053
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000054char LoopInstSimplify::ID = 0;
55INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
56 "Simplify instructions in loops", false, false)
Chad Rosierc24b86f2011-12-01 03:08:23 +000057INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Chandler Carruth73523022014-01-13 13:07:17 +000058INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000059INITIALIZE_PASS_DEPENDENCY(LoopInfo)
60INITIALIZE_PASS_DEPENDENCY(LCSSA)
61INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
62 "Simplify instructions in loops", false, false)
63
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000064Pass *llvm::createLoopInstSimplifyPass() {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000065 return new LoopInstSimplify();
66}
67
Cameron Zwarich4c51d122011-01-05 05:15:53 +000068bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000069 if (skipOptnoneFunction(L))
70 return false;
71
Chandler Carruth73523022014-01-13 13:07:17 +000072 DominatorTreeWrapperPass *DTWP =
73 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000074 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Cameron Zwarichb2a41e92011-01-04 18:19:19 +000075 LoopInfo *LI = &getAnalysis<LoopInfo>();
Rafael Espindola93512512014-02-25 17:30:31 +000076 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000077 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Chad Rosierc24b86f2011-12-01 03:08:23 +000078 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000079
Cameron Zwarich4c51d122011-01-05 05:15:53 +000080 SmallVector<BasicBlock*, 8> ExitBlocks;
81 L->getUniqueExitBlocks(ExitBlocks);
82 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
83
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000084 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
85
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000086 // The bit we are stealing from the pointer represents whether this basic
87 // block is the header of a subloop, in which case we only process its phis.
88 typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
89 SmallVector<WorklistItem, 16> VisitStack;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000090 SmallPtrSet<BasicBlock*, 32> Visited;
91
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000092 bool Changed = false;
93 bool LocalChanged;
94 do {
95 LocalChanged = false;
96
Cameron Zwarich4c51d122011-01-05 05:15:53 +000097 VisitStack.clear();
98 Visited.clear();
99
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000100 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000101
102 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000103 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +0000104 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000105 bool IsSubloopHeader = Item.getInt();
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000106
107 // Simplify instructions in the current basic block.
108 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Cameron Zwarichb2a41e92011-01-04 18:19:19 +0000109 Instruction *I = BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000110
111 // The first time through the loop ToSimplify is empty and we try to
112 // simplify all instructions. On later iterations ToSimplify is not
113 // empty and we only bother simplifying instructions that are in it.
114 if (!ToSimplify->empty() && !ToSimplify->count(I))
115 continue;
116
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000117 // Don't bother simplifying unused instructions.
118 if (!I->use_empty()) {
Rafael Espindola612886f2014-02-21 01:53:35 +0000119 Value *V = SimplifyInstruction(I, DL, TLI, DT);
Cameron Zwariche9249692011-01-04 00:12:46 +0000120 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000121 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000122 for (User *U : I->users())
123 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000124
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000125 I->replaceAllUsesWith(V);
126 LocalChanged = true;
127 ++NumSimplified;
128 }
129 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +0000130 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
131 if (res) {
132 // RecursivelyDeleteTriviallyDeadInstruction can remove
133 // more than one instruction, so simply incrementing the
134 // iterator does not work. When instructions get deleted
135 // re-iterate instead.
136 BI = BB->begin(); BE = BB->end();
137 LocalChanged |= res;
138 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000139
140 if (IsSubloopHeader && !isa<PHINode>(I))
141 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000142 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000143
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000144 // Add all successors to the worklist, except for loop exit blocks and the
145 // bodies of subloops. We visit the headers of loops so that we can process
146 // their phis, but we contract the rest of the subloop body and only follow
147 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000148 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
149 ++SI) {
150 BasicBlock *SuccBB = *SI;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000151 if (!Visited.insert(SuccBB))
152 continue;
153
154 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
155 if (SuccLoop && SuccLoop->getHeader() == SuccBB
156 && L->contains(SuccLoop)) {
157 VisitStack.push_back(WorklistItem(SuccBB, true));
158
159 SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
160 SuccLoop->getExitBlocks(SubLoopExitBlocks);
161
162 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
163 BasicBlock *ExitBB = SubLoopExitBlocks[i];
164 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB))
165 VisitStack.push_back(WorklistItem(ExitBB, false));
166 }
167
168 continue;
169 }
170
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000171 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000172 ExitBlocks.end(), SuccBB);
173 if (IsExitBlock)
174 continue;
175
176 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000177 }
178 }
179
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000180 // Place the list of instructions to simplify on the next loop iteration
181 // into ToSimplify.
182 std::swap(ToSimplify, Next);
183 Next->clear();
184
Cameron Zwariche9249692011-01-04 00:12:46 +0000185 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000186 } while (LocalChanged);
187
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000188 return Changed;
189}