blob: f9bbd820fca059b1cd18b40d7f3b662985da6f95 [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"
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"
Dehao Chendcafd5e2016-07-15 16:42:11 +000020#include "llvm/Analysis/LoopPassManager.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000021#include "llvm/Analysis/ScalarEvolution.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000022#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Instructions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Debug.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000027#include "llvm/Transforms/Scalar.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000028#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000029#include "llvm/Transforms/Utils/LoopUtils.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000030using namespace llvm;
31
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "loop-instsimplify"
33
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000034STATISTIC(NumSimplified, "Number of redundant instructions simplified");
35
Dehao Chendcafd5e2016-07-15 16:42:11 +000036static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI,
Dehao Chendcafd5e2016-07-15 16:42:11 +000037 const TargetLibraryInfo *TLI) {
38 SmallVector<BasicBlock *, 8> ExitBlocks;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000039 L->getUniqueExitBlocks(ExitBlocks);
40 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
41
Dehao Chendcafd5e2016-07-15 16:42:11 +000042 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000043
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000044 // The bit we are stealing from the pointer represents whether this basic
45 // block is the header of a subloop, in which case we only process its phis.
Dehao Chendcafd5e2016-07-15 16:42:11 +000046 typedef PointerIntPair<BasicBlock *, 1> WorklistItem;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000047 SmallVector<WorklistItem, 16> VisitStack;
Dehao Chendcafd5e2016-07-15 16:42:11 +000048 SmallPtrSet<BasicBlock *, 32> Visited;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000049
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000050 bool Changed = false;
51 bool LocalChanged;
52 do {
53 LocalChanged = false;
54
Cameron Zwarich4c51d122011-01-05 05:15:53 +000055 VisitStack.clear();
56 Visited.clear();
57
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000058 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +000059
60 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000061 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000062 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000063 bool IsSubloopHeader = Item.getInt();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000064 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Cameron Zwarich4c51d122011-01-05 05:15:53 +000065
66 // Simplify instructions in the current basic block.
67 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000068 Instruction *I = &*BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000069
70 // The first time through the loop ToSimplify is empty and we try to
71 // simplify all instructions. On later iterations ToSimplify is not
72 // empty and we only bother simplifying instructions that are in it.
73 if (!ToSimplify->empty() && !ToSimplify->count(I))
74 continue;
75
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000076 // Don't bother simplifying unused instructions.
77 if (!I->use_empty()) {
Hal Finkel3ca4a6b2016-12-15 03:02:15 +000078 Value *V = SimplifyInstruction(I, DL, TLI, DT);
Cameron Zwariche9249692011-01-04 00:12:46 +000079 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000080 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000081 for (User *U : I->users())
82 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000083
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000084 I->replaceAllUsesWith(V);
85 LocalChanged = true;
86 ++NumSimplified;
87 }
88 }
Chad Rosier074ce832016-04-06 13:27:13 +000089 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
90 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
91 // instruction, so simply incrementing the iterator does not work.
92 // When instructions get deleted re-iterate instead.
Dehao Chendcafd5e2016-07-15 16:42:11 +000093 BI = BB->begin();
94 BE = BB->end();
Chad Rosier074ce832016-04-06 13:27:13 +000095 LocalChanged = true;
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +000096 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000097
98 if (IsSubloopHeader && !isa<PHINode>(I))
99 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000100 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000101
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000102 // Add all successors to the worklist, except for loop exit blocks and the
Dehao Chendcafd5e2016-07-15 16:42:11 +0000103 // bodies of subloops. We visit the headers of loops so that we can
104 // process
105 // their phis, but we contract the rest of the subloop body and only
106 // follow
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000107 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000108 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
109 ++SI) {
110 BasicBlock *SuccBB = *SI;
David Blaikie70573dc2014-11-19 07:49:26 +0000111 if (!Visited.insert(SuccBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000112 continue;
113
114 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
Dehao Chendcafd5e2016-07-15 16:42:11 +0000115 if (SuccLoop && SuccLoop->getHeader() == SuccBB &&
116 L->contains(SuccLoop)) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000117 VisitStack.push_back(WorklistItem(SuccBB, true));
118
Dehao Chendcafd5e2016-07-15 16:42:11 +0000119 SmallVector<BasicBlock *, 8> SubLoopExitBlocks;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000120 SuccLoop->getExitBlocks(SubLoopExitBlocks);
121
122 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
123 BasicBlock *ExitBB = SubLoopExitBlocks[i];
David Blaikie70573dc2014-11-19 07:49:26 +0000124 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000125 VisitStack.push_back(WorklistItem(ExitBB, false));
126 }
127
128 continue;
129 }
130
Dehao Chendcafd5e2016-07-15 16:42:11 +0000131 bool IsExitBlock =
132 std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB);
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000133 if (IsExitBlock)
134 continue;
135
136 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000137 }
138 }
139
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000140 // Place the list of instructions to simplify on the next loop iteration
141 // into ToSimplify.
142 std::swap(ToSimplify, Next);
143 Next->clear();
144
Cameron Zwariche9249692011-01-04 00:12:46 +0000145 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000146 } while (LocalChanged);
147
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000148 return Changed;
149}
Dehao Chendcafd5e2016-07-15 16:42:11 +0000150
151namespace {
152class LoopInstSimplifyLegacyPass : public LoopPass {
153public:
154 static char ID; // Pass ID, replacement for typeid
155 LoopInstSimplifyLegacyPass() : LoopPass(ID) {
156 initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
157 }
158
159 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
160 if (skipLoop(L))
161 return false;
162 DominatorTreeWrapperPass *DTWP =
163 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
164 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
165 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Dehao Chendcafd5e2016-07-15 16:42:11 +0000166 const TargetLibraryInfo *TLI =
167 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
168
Hal Finkel3ca4a6b2016-12-15 03:02:15 +0000169 return SimplifyLoopInst(L, DT, LI, TLI);
Dehao Chendcafd5e2016-07-15 16:42:11 +0000170 }
171
172 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dehao Chendcafd5e2016-07-15 16:42:11 +0000173 AU.addRequired<TargetLibraryInfoWrapperPass>();
174 AU.setPreservesCFG();
175 getLoopAnalysisUsage(AU);
176 }
177};
178}
179
180PreservedAnalyses LoopInstSimplifyPass::run(Loop &L,
Sean Silva0746f3b2016-08-09 00:28:52 +0000181 LoopAnalysisManager &AM) {
Dehao Chendcafd5e2016-07-15 16:42:11 +0000182 const auto &FAM =
183 AM.getResult<FunctionAnalysisManagerLoopProxy>(L).getManager();
184 Function *F = L.getHeader()->getParent();
185
186 // Use getCachedResult because Loop pass cannot trigger a function analysis.
187 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(*F);
188 auto *LI = FAM.getCachedResult<LoopAnalysis>(*F);
Dehao Chendcafd5e2016-07-15 16:42:11 +0000189 const auto *TLI = FAM.getCachedResult<TargetLibraryAnalysis>(*F);
Hal Finkel3ca4a6b2016-12-15 03:02:15 +0000190 assert((LI && TLI) && "Analyses for Loop Inst Simplify not available");
Dehao Chendcafd5e2016-07-15 16:42:11 +0000191
Hal Finkel3ca4a6b2016-12-15 03:02:15 +0000192 if (!SimplifyLoopInst(&L, DT, LI, TLI))
Dehao Chendcafd5e2016-07-15 16:42:11 +0000193 return PreservedAnalyses::all();
194
195 return getLoopPassPreservedAnalyses();
196}
197
198char LoopInstSimplifyLegacyPass::ID = 0;
199INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
200 "Simplify instructions in loops", false, false)
Dehao Chendcafd5e2016-07-15 16:42:11 +0000201INITIALIZE_PASS_DEPENDENCY(LoopPass)
202INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
203INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
204 "Simplify instructions in loops", false, false)
205
206Pass *llvm::createLoopInstSimplifyPass() {
207 return new LoopInstSimplifyLegacyPass();
208}