blob: 6dc600e6614edf14df0c517202c925db836287d2 [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"
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"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000021#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000023#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/Debug.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000026#include "llvm/Analysis/TargetLibraryInfo.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000027#include "llvm/Transforms/Utils/Local.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000028using namespace llvm;
29
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "loop-instsimplify"
31
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000032STATISTIC(NumSimplified, "Number of redundant instructions simplified");
33
34namespace {
Cameron Zwarich4c51d122011-01-05 05:15:53 +000035 class LoopInstSimplify : public LoopPass {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000036 public:
37 static char ID; // Pass ID, replacement for typeid
Cameron Zwarich4c51d122011-01-05 05:15:53 +000038 LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000039 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
40 }
41
Craig Topper3e4c6972014-03-05 09:10:37 +000042 bool runOnLoop(Loop*, LPPassManager&) override;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000043
Craig Topper3e4c6972014-03-05 09:10:37 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000045 AU.setPreservesCFG();
Chandler Carruth66b31302015-01-04 12:03:27 +000046 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000047 AU.addRequired<LoopInfoWrapperPass>();
Cameron Zwarich4c51d122011-01-05 05:15:53 +000048 AU.addRequiredID(LoopSimplifyID);
Cameron Zwaricha42e5912011-01-09 12:35:16 +000049 AU.addPreservedID(LoopSimplifyID);
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000050 AU.addPreservedID(LCSSAID);
Chandler Carruthb81dfa62015-01-28 04:57:56 +000051 AU.addPreserved<ScalarEvolution>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000052 AU.addRequired<TargetLibraryInfoWrapperPass>();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000053 }
54 };
55}
Nadav Rotem465834c2012-07-24 10:51:42 +000056
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000057char LoopInstSimplify::ID = 0;
58INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
59 "Simplify instructions in loops", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +000060INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000061INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chandler Carruth73523022014-01-13 13:07:17 +000062INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth4f8f3072015-01-17 14:16:18 +000063INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000064INITIALIZE_PASS_DEPENDENCY(LCSSA)
65INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
66 "Simplify instructions in loops", false, false)
67
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000068Pass *llvm::createLoopInstSimplifyPass() {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000069 return new LoopInstSimplify();
70}
71
Cameron Zwarich4c51d122011-01-05 05:15:53 +000072bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000073 if (skipOptnoneFunction(L))
74 return false;
75
Chandler Carruth73523022014-01-13 13:07:17 +000076 DominatorTreeWrapperPass *DTWP =
77 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000078 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Chandler Carruth4f8f3072015-01-17 14:16:18 +000079 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Rafael Espindola93512512014-02-25 17:30:31 +000080 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000081 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000082 const TargetLibraryInfo *TLI =
83 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +000084 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
85 *L->getHeader()->getParent());
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000086
Cameron Zwarich4c51d122011-01-05 05:15:53 +000087 SmallVector<BasicBlock*, 8> ExitBlocks;
88 L->getUniqueExitBlocks(ExitBlocks);
89 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
90
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000091 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
92
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000093 // The bit we are stealing from the pointer represents whether this basic
94 // block is the header of a subloop, in which case we only process its phis.
95 typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
96 SmallVector<WorklistItem, 16> VisitStack;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000097 SmallPtrSet<BasicBlock*, 32> Visited;
98
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000099 bool Changed = false;
100 bool LocalChanged;
101 do {
102 LocalChanged = false;
103
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000104 VisitStack.clear();
105 Visited.clear();
106
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000107 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000108
109 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000110 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +0000111 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000112 bool IsSubloopHeader = Item.getInt();
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000113
114 // Simplify instructions in the current basic block.
115 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Cameron Zwarichb2a41e92011-01-04 18:19:19 +0000116 Instruction *I = BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000117
118 // The first time through the loop ToSimplify is empty and we try to
119 // simplify all instructions. On later iterations ToSimplify is not
120 // empty and we only bother simplifying instructions that are in it.
121 if (!ToSimplify->empty() && !ToSimplify->count(I))
122 continue;
123
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000124 // Don't bother simplifying unused instructions.
125 if (!I->use_empty()) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000126 Value *V = SimplifyInstruction(I, DL, TLI, DT, &AC);
Cameron Zwariche9249692011-01-04 00:12:46 +0000127 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000128 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000129 for (User *U : I->users())
130 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000131
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000132 I->replaceAllUsesWith(V);
133 LocalChanged = true;
134 ++NumSimplified;
135 }
136 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +0000137 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
138 if (res) {
139 // RecursivelyDeleteTriviallyDeadInstruction can remove
140 // more than one instruction, so simply incrementing the
141 // iterator does not work. When instructions get deleted
142 // re-iterate instead.
143 BI = BB->begin(); BE = BB->end();
144 LocalChanged |= res;
145 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000146
147 if (IsSubloopHeader && !isa<PHINode>(I))
148 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000149 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000150
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000151 // Add all successors to the worklist, except for loop exit blocks and the
152 // bodies of subloops. We visit the headers of loops so that we can process
153 // their phis, but we contract the rest of the subloop body and only follow
154 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000155 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
156 ++SI) {
157 BasicBlock *SuccBB = *SI;
David Blaikie70573dc2014-11-19 07:49:26 +0000158 if (!Visited.insert(SuccBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000159 continue;
160
161 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
162 if (SuccLoop && SuccLoop->getHeader() == SuccBB
163 && L->contains(SuccLoop)) {
164 VisitStack.push_back(WorklistItem(SuccBB, true));
165
166 SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
167 SuccLoop->getExitBlocks(SubLoopExitBlocks);
168
169 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
170 BasicBlock *ExitBB = SubLoopExitBlocks[i];
David Blaikie70573dc2014-11-19 07:49:26 +0000171 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000172 VisitStack.push_back(WorklistItem(ExitBB, false));
173 }
174
175 continue;
176 }
177
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000178 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000179 ExitBlocks.end(), SuccBB);
180 if (IsExitBlock)
181 continue;
182
183 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000184 }
185 }
186
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000187 // Place the list of instructions to simplify on the next loop iteration
188 // into ToSimplify.
189 std::swap(ToSimplify, Next);
190 Next->clear();
191
Cameron Zwariche9249692011-01-04 00:12:46 +0000192 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000193 } while (LocalChanged);
194
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000195 return Changed;
196}