blob: 629cb87d7a91690370dbad6b4b41d0733516bfe3 [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
Dehao Chendcafd5e2016-07-15 16:42:11 +000014#include "llvm/Transforms/Scalar/LoopInstSimplify.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"
Chandler Carruth66b31302015-01-04 12:03:27 +000017#include "llvm/Analysis/AssumptionCache.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000018#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwariche9249692011-01-04 00:12:46 +000019#include "llvm/Analysis/LoopInfo.h"
Cameron Zwarich4c51d122011-01-05 05:15:53 +000020#include "llvm/Analysis/LoopPass.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000021#include "llvm/Analysis/LoopPassManager.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000022#include "llvm/Analysis/ScalarEvolution.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000023#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000025#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/Debug.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000028#include "llvm/Transforms/Scalar.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000029#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000030#include "llvm/Transforms/Utils/LoopUtils.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000031using namespace llvm;
32
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "loop-instsimplify"
34
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000035STATISTIC(NumSimplified, "Number of redundant instructions simplified");
36
Dehao Chendcafd5e2016-07-15 16:42:11 +000037static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI,
38 AssumptionCache *AC,
39 const TargetLibraryInfo *TLI) {
40 SmallVector<BasicBlock *, 8> ExitBlocks;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000041 L->getUniqueExitBlocks(ExitBlocks);
42 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
43
Dehao Chendcafd5e2016-07-15 16:42:11 +000044 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000045
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000046 // The bit we are stealing from the pointer represents whether this basic
47 // block is the header of a subloop, in which case we only process its phis.
Dehao Chendcafd5e2016-07-15 16:42:11 +000048 typedef PointerIntPair<BasicBlock *, 1> WorklistItem;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000049 SmallVector<WorklistItem, 16> VisitStack;
Dehao Chendcafd5e2016-07-15 16:42:11 +000050 SmallPtrSet<BasicBlock *, 32> Visited;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000051
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000052 bool Changed = false;
53 bool LocalChanged;
54 do {
55 LocalChanged = false;
56
Cameron Zwarich4c51d122011-01-05 05:15:53 +000057 VisitStack.clear();
58 Visited.clear();
59
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000060 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +000061
62 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000063 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000064 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000065 bool IsSubloopHeader = Item.getInt();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000066 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Cameron Zwarich4c51d122011-01-05 05:15:53 +000067
68 // Simplify instructions in the current basic block.
69 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000070 Instruction *I = &*BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000071
72 // The first time through the loop ToSimplify is empty and we try to
73 // simplify all instructions. On later iterations ToSimplify is not
74 // empty and we only bother simplifying instructions that are in it.
75 if (!ToSimplify->empty() && !ToSimplify->count(I))
76 continue;
77
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000078 // Don't bother simplifying unused instructions.
79 if (!I->use_empty()) {
Dehao Chendcafd5e2016-07-15 16:42:11 +000080 Value *V = SimplifyInstruction(I, DL, TLI, DT, AC);
Cameron Zwariche9249692011-01-04 00:12:46 +000081 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000082 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000083 for (User *U : I->users())
84 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000085
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000086 I->replaceAllUsesWith(V);
87 LocalChanged = true;
88 ++NumSimplified;
89 }
90 }
Chad Rosier074ce832016-04-06 13:27:13 +000091 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
92 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
93 // instruction, so simply incrementing the iterator does not work.
94 // When instructions get deleted re-iterate instead.
Dehao Chendcafd5e2016-07-15 16:42:11 +000095 BI = BB->begin();
96 BE = BB->end();
Chad Rosier074ce832016-04-06 13:27:13 +000097 LocalChanged = true;
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +000098 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000099
100 if (IsSubloopHeader && !isa<PHINode>(I))
101 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000102 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000103
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000104 // Add all successors to the worklist, except for loop exit blocks and the
Dehao Chendcafd5e2016-07-15 16:42:11 +0000105 // bodies of subloops. We visit the headers of loops so that we can
106 // process
107 // their phis, but we contract the rest of the subloop body and only
108 // follow
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000109 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000110 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
111 ++SI) {
112 BasicBlock *SuccBB = *SI;
David Blaikie70573dc2014-11-19 07:49:26 +0000113 if (!Visited.insert(SuccBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000114 continue;
115
116 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
Dehao Chendcafd5e2016-07-15 16:42:11 +0000117 if (SuccLoop && SuccLoop->getHeader() == SuccBB &&
118 L->contains(SuccLoop)) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000119 VisitStack.push_back(WorklistItem(SuccBB, true));
120
Dehao Chendcafd5e2016-07-15 16:42:11 +0000121 SmallVector<BasicBlock *, 8> SubLoopExitBlocks;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000122 SuccLoop->getExitBlocks(SubLoopExitBlocks);
123
124 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
125 BasicBlock *ExitBB = SubLoopExitBlocks[i];
David Blaikie70573dc2014-11-19 07:49:26 +0000126 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000127 VisitStack.push_back(WorklistItem(ExitBB, false));
128 }
129
130 continue;
131 }
132
Dehao Chendcafd5e2016-07-15 16:42:11 +0000133 bool IsExitBlock =
134 std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB);
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000135 if (IsExitBlock)
136 continue;
137
138 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000139 }
140 }
141
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000142 // Place the list of instructions to simplify on the next loop iteration
143 // into ToSimplify.
144 std::swap(ToSimplify, Next);
145 Next->clear();
146
Cameron Zwariche9249692011-01-04 00:12:46 +0000147 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000148 } while (LocalChanged);
149
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000150 return Changed;
151}
Dehao Chendcafd5e2016-07-15 16:42:11 +0000152
153namespace {
154class LoopInstSimplifyLegacyPass : public LoopPass {
155public:
156 static char ID; // Pass ID, replacement for typeid
157 LoopInstSimplifyLegacyPass() : LoopPass(ID) {
158 initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
159 }
160
161 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
162 if (skipLoop(L))
163 return false;
164 DominatorTreeWrapperPass *DTWP =
165 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
166 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
167 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
168 AssumptionCache *AC =
169 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
170 *L->getHeader()->getParent());
171 const TargetLibraryInfo *TLI =
172 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
173
174 return SimplifyLoopInst(L, DT, LI, AC, TLI);
175 }
176
177 void getAnalysisUsage(AnalysisUsage &AU) const override {
178 AU.addRequired<AssumptionCacheTracker>();
179 AU.addRequired<TargetLibraryInfoWrapperPass>();
180 AU.setPreservesCFG();
181 getLoopAnalysisUsage(AU);
182 }
183};
184}
185
186PreservedAnalyses LoopInstSimplifyPass::run(Loop &L,
187 AnalysisManager<Loop> &AM) {
188 const auto &FAM =
189 AM.getResult<FunctionAnalysisManagerLoopProxy>(L).getManager();
190 Function *F = L.getHeader()->getParent();
191
192 // Use getCachedResult because Loop pass cannot trigger a function analysis.
193 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(*F);
194 auto *LI = FAM.getCachedResult<LoopAnalysis>(*F);
195 auto *AC = FAM.getCachedResult<AssumptionAnalysis>(*F);
196 const auto *TLI = FAM.getCachedResult<TargetLibraryAnalysis>(*F);
197 assert((LI && AC && TLI) && "Analyses for Loop Inst Simplify not available");
198
199 if (!SimplifyLoopInst(&L, DT, LI, AC, TLI))
200 return PreservedAnalyses::all();
201
202 return getLoopPassPreservedAnalyses();
203}
204
205char LoopInstSimplifyLegacyPass::ID = 0;
206INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
207 "Simplify instructions in loops", false, false)
208INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
209INITIALIZE_PASS_DEPENDENCY(LoopPass)
210INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
211INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
212 "Simplify instructions in loops", false, false)
213
214Pass *llvm::createLoopInstSimplifyPass() {
215 return new LoopInstSimplifyLegacyPass();
216}