blob: 8fd7c8fbaaa5c3ea95016d5e34a0b3ad3db46d6d [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"
Hal Finkel60db0582014-09-07 18:57:58 +000017#include "llvm/Analysis/AssumptionTracker.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 Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/Debug.h"
Chad Rosierc24b86f2011-12-01 03:08:23 +000025#include "llvm/Target/TargetLibraryInfo.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000026#include "llvm/Transforms/Utils/Local.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000027using namespace llvm;
28
Chandler Carruth964daaa2014-04-22 02:55:47 +000029#define DEBUG_TYPE "loop-instsimplify"
30
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000031STATISTIC(NumSimplified, "Number of redundant instructions simplified");
32
33namespace {
Cameron Zwarich4c51d122011-01-05 05:15:53 +000034 class LoopInstSimplify : public LoopPass {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000035 public:
36 static char ID; // Pass ID, replacement for typeid
Cameron Zwarich4c51d122011-01-05 05:15:53 +000037 LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000038 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
39 }
40
Craig Topper3e4c6972014-03-05 09:10:37 +000041 bool runOnLoop(Loop*, LPPassManager&) override;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000042
Craig Topper3e4c6972014-03-05 09:10:37 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000044 AU.setPreservesCFG();
Hal Finkel60db0582014-09-07 18:57:58 +000045 AU.addRequired<AssumptionTracker>();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000046 AU.addRequired<LoopInfo>();
Cameron Zwarich4c51d122011-01-05 05:15:53 +000047 AU.addRequiredID(LoopSimplifyID);
Cameron Zwaricha42e5912011-01-09 12:35:16 +000048 AU.addPreservedID(LoopSimplifyID);
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000049 AU.addPreservedID(LCSSAID);
Cameron Zwarich25cb63c2011-02-11 06:08:25 +000050 AU.addPreserved("scalar-evolution");
Chad Rosierc24b86f2011-12-01 03:08:23 +000051 AU.addRequired<TargetLibraryInfo>();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000052 }
53 };
54}
Nadav Rotem465834c2012-07-24 10:51:42 +000055
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000056char LoopInstSimplify::ID = 0;
57INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
58 "Simplify instructions in loops", false, false)
Hal Finkel60db0582014-09-07 18:57:58 +000059INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
Chad Rosierc24b86f2011-12-01 03:08:23 +000060INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Chandler Carruth73523022014-01-13 13:07:17 +000061INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000062INITIALIZE_PASS_DEPENDENCY(LoopInfo)
63INITIALIZE_PASS_DEPENDENCY(LCSSA)
64INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
65 "Simplify instructions in loops", false, false)
66
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000067Pass *llvm::createLoopInstSimplifyPass() {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000068 return new LoopInstSimplify();
69}
70
Cameron Zwarich4c51d122011-01-05 05:15:53 +000071bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000072 if (skipOptnoneFunction(L))
73 return false;
74
Chandler Carruth73523022014-01-13 13:07:17 +000075 DominatorTreeWrapperPass *DTWP =
76 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000077 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Cameron Zwarichb2a41e92011-01-04 18:19:19 +000078 LoopInfo *LI = &getAnalysis<LoopInfo>();
Rafael Espindola93512512014-02-25 17:30:31 +000079 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000080 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Chad Rosierc24b86f2011-12-01 03:08:23 +000081 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Hal Finkel60db0582014-09-07 18:57:58 +000082 AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000083
Cameron Zwarich4c51d122011-01-05 05:15:53 +000084 SmallVector<BasicBlock*, 8> ExitBlocks;
85 L->getUniqueExitBlocks(ExitBlocks);
86 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
87
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000088 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
89
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000090 // The bit we are stealing from the pointer represents whether this basic
91 // block is the header of a subloop, in which case we only process its phis.
92 typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
93 SmallVector<WorklistItem, 16> VisitStack;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000094 SmallPtrSet<BasicBlock*, 32> Visited;
95
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000096 bool Changed = false;
97 bool LocalChanged;
98 do {
99 LocalChanged = false;
100
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000101 VisitStack.clear();
102 Visited.clear();
103
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000104 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000105
106 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000107 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +0000108 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000109 bool IsSubloopHeader = Item.getInt();
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000110
111 // Simplify instructions in the current basic block.
112 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Cameron Zwarichb2a41e92011-01-04 18:19:19 +0000113 Instruction *I = BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000114
115 // The first time through the loop ToSimplify is empty and we try to
116 // simplify all instructions. On later iterations ToSimplify is not
117 // empty and we only bother simplifying instructions that are in it.
118 if (!ToSimplify->empty() && !ToSimplify->count(I))
119 continue;
120
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000121 // Don't bother simplifying unused instructions.
122 if (!I->use_empty()) {
Hal Finkel60db0582014-09-07 18:57:58 +0000123 Value *V = SimplifyInstruction(I, DL, TLI, DT, AT);
Cameron Zwariche9249692011-01-04 00:12:46 +0000124 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000125 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000126 for (User *U : I->users())
127 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000128
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000129 I->replaceAllUsesWith(V);
130 LocalChanged = true;
131 ++NumSimplified;
132 }
133 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +0000134 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
135 if (res) {
136 // RecursivelyDeleteTriviallyDeadInstruction can remove
137 // more than one instruction, so simply incrementing the
138 // iterator does not work. When instructions get deleted
139 // re-iterate instead.
140 BI = BB->begin(); BE = BB->end();
141 LocalChanged |= res;
142 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000143
144 if (IsSubloopHeader && !isa<PHINode>(I))
145 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000146 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000147
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000148 // Add all successors to the worklist, except for loop exit blocks and the
149 // bodies of subloops. We visit the headers of loops so that we can process
150 // their phis, but we contract the rest of the subloop body and only follow
151 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000152 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
153 ++SI) {
154 BasicBlock *SuccBB = *SI;
David Blaikie70573dc2014-11-19 07:49:26 +0000155 if (!Visited.insert(SuccBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000156 continue;
157
158 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
159 if (SuccLoop && SuccLoop->getHeader() == SuccBB
160 && L->contains(SuccLoop)) {
161 VisitStack.push_back(WorklistItem(SuccBB, true));
162
163 SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
164 SuccLoop->getExitBlocks(SubLoopExitBlocks);
165
166 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
167 BasicBlock *ExitBB = SubLoopExitBlocks[i];
David Blaikie70573dc2014-11-19 07:49:26 +0000168 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000169 VisitStack.push_back(WorklistItem(ExitBB, false));
170 }
171
172 continue;
173 }
174
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000175 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000176 ExitBlocks.end(), SuccBB);
177 if (IsExitBlock)
178 continue;
179
180 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000181 }
182 }
183
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000184 // Place the list of instructions to simplify on the next loop iteration
185 // into ToSimplify.
186 std::swap(ToSimplify, Next);
187 Next->clear();
188
Cameron Zwariche9249692011-01-04 00:12:46 +0000189 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000190 } while (LocalChanged);
191
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000192 return Changed;
193}