blob: 40d468a084d498859cf17e9deb804d0b58278f52 [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"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000015#include "llvm/ADT/PointerIntPair.h"
Jakub Staszakf23980a2013-02-09 01:04:28 +000016#include "llvm/ADT/STLExtras.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000017#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallVector.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/ADT/Statistic.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000020#include "llvm/Analysis/AssumptionCache.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000021#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwariche9249692011-01-04 00:12:46 +000022#include "llvm/Analysis/LoopInfo.h"
Cameron Zwarich4c51d122011-01-05 05:15:53 +000023#include "llvm/Analysis/LoopPass.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000025#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000028#include "llvm/IR/Dominators.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000029#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Instructions.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000031#include "llvm/IR/Module.h"
32#include "llvm/IR/PassManager.h"
33#include "llvm/IR/User.h"
34#include "llvm/Pass.h"
35#include "llvm/Support/Casting.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +000036#include "llvm/Transforms/Scalar.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000037#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000038#include "llvm/Transforms/Utils/LoopUtils.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000039#include <algorithm>
40#include <utility>
41
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000042using namespace llvm;
43
Chandler Carruth964daaa2014-04-22 02:55:47 +000044#define DEBUG_TYPE "loop-instsimplify"
45
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000046STATISTIC(NumSimplified, "Number of redundant instructions simplified");
47
Dehao Chendcafd5e2016-07-15 16:42:11 +000048static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI,
Daniel Jasperaec2fa32016-12-19 08:22:17 +000049 AssumptionCache *AC,
Dehao Chendcafd5e2016-07-15 16:42:11 +000050 const TargetLibraryInfo *TLI) {
51 SmallVector<BasicBlock *, 8> ExitBlocks;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000052 L->getUniqueExitBlocks(ExitBlocks);
53 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
54
Dehao Chendcafd5e2016-07-15 16:42:11 +000055 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000056
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000057 // The bit we are stealing from the pointer represents whether this basic
58 // block is the header of a subloop, in which case we only process its phis.
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000059 using WorklistItem = PointerIntPair<BasicBlock *, 1>;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000060 SmallVector<WorklistItem, 16> VisitStack;
Dehao Chendcafd5e2016-07-15 16:42:11 +000061 SmallPtrSet<BasicBlock *, 32> Visited;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000062
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000063 bool Changed = false;
64 bool LocalChanged;
65 do {
66 LocalChanged = false;
67
Cameron Zwarich4c51d122011-01-05 05:15:53 +000068 VisitStack.clear();
69 Visited.clear();
70
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000071 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +000072
73 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000074 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000075 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000076 bool IsSubloopHeader = Item.getInt();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000077 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Cameron Zwarich4c51d122011-01-05 05:15:53 +000078
79 // Simplify instructions in the current basic block.
80 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000081 Instruction *I = &*BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000082
83 // The first time through the loop ToSimplify is empty and we try to
84 // simplify all instructions. On later iterations ToSimplify is not
85 // empty and we only bother simplifying instructions that are in it.
86 if (!ToSimplify->empty() && !ToSimplify->count(I))
87 continue;
88
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000089 // Don't bother simplifying unused instructions.
90 if (!I->use_empty()) {
Daniel Berlin4d0fe642017-04-28 19:55:38 +000091 Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC});
Cameron Zwariche9249692011-01-04 00:12:46 +000092 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000093 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000094 for (User *U : I->users())
95 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000096
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000097 I->replaceAllUsesWith(V);
98 LocalChanged = true;
99 ++NumSimplified;
100 }
101 }
Chad Rosier074ce832016-04-06 13:27:13 +0000102 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
103 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
104 // instruction, so simply incrementing the iterator does not work.
105 // When instructions get deleted re-iterate instead.
Dehao Chendcafd5e2016-07-15 16:42:11 +0000106 BI = BB->begin();
107 BE = BB->end();
Chad Rosier074ce832016-04-06 13:27:13 +0000108 LocalChanged = true;
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +0000109 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000110
111 if (IsSubloopHeader && !isa<PHINode>(I))
112 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000113 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000114
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000115 // Add all successors to the worklist, except for loop exit blocks and the
Dehao Chendcafd5e2016-07-15 16:42:11 +0000116 // bodies of subloops. We visit the headers of loops so that we can
117 // process
118 // their phis, but we contract the rest of the subloop body and only
119 // follow
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000120 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000121 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
122 ++SI) {
123 BasicBlock *SuccBB = *SI;
David Blaikie70573dc2014-11-19 07:49:26 +0000124 if (!Visited.insert(SuccBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000125 continue;
126
127 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
Dehao Chendcafd5e2016-07-15 16:42:11 +0000128 if (SuccLoop && SuccLoop->getHeader() == SuccBB &&
129 L->contains(SuccLoop)) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000130 VisitStack.push_back(WorklistItem(SuccBB, true));
131
Dehao Chendcafd5e2016-07-15 16:42:11 +0000132 SmallVector<BasicBlock *, 8> SubLoopExitBlocks;
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000133 SuccLoop->getExitBlocks(SubLoopExitBlocks);
134
135 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
136 BasicBlock *ExitBB = SubLoopExitBlocks[i];
David Blaikie70573dc2014-11-19 07:49:26 +0000137 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000138 VisitStack.push_back(WorklistItem(ExitBB, false));
139 }
140
141 continue;
142 }
143
Dehao Chendcafd5e2016-07-15 16:42:11 +0000144 bool IsExitBlock =
145 std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB);
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000146 if (IsExitBlock)
147 continue;
148
149 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000150 }
151 }
152
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000153 // Place the list of instructions to simplify on the next loop iteration
154 // into ToSimplify.
155 std::swap(ToSimplify, Next);
156 Next->clear();
157
Cameron Zwariche9249692011-01-04 00:12:46 +0000158 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000159 } while (LocalChanged);
160
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000161 return Changed;
162}
Dehao Chendcafd5e2016-07-15 16:42:11 +0000163
164namespace {
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000165
Dehao Chendcafd5e2016-07-15 16:42:11 +0000166class LoopInstSimplifyLegacyPass : public LoopPass {
167public:
168 static char ID; // Pass ID, replacement for typeid
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000169
Dehao Chendcafd5e2016-07-15 16:42:11 +0000170 LoopInstSimplifyLegacyPass() : LoopPass(ID) {
171 initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
172 }
173
174 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
175 if (skipLoop(L))
176 return false;
177 DominatorTreeWrapperPass *DTWP =
178 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
179 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
180 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000181 AssumptionCache *AC =
182 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
183 *L->getHeader()->getParent());
Dehao Chendcafd5e2016-07-15 16:42:11 +0000184 const TargetLibraryInfo *TLI =
185 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
186
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000187 return SimplifyLoopInst(L, DT, LI, AC, TLI);
Dehao Chendcafd5e2016-07-15 16:42:11 +0000188 }
189
190 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000191 AU.addRequired<AssumptionCacheTracker>();
Dehao Chendcafd5e2016-07-15 16:42:11 +0000192 AU.addRequired<TargetLibraryInfoWrapperPass>();
193 AU.setPreservesCFG();
194 getLoopAnalysisUsage(AU);
195 }
196};
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000197
198} // end anonymous namespace
Dehao Chendcafd5e2016-07-15 16:42:11 +0000199
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000200PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
201 LoopStandardAnalysisResults &AR,
202 LPMUpdater &) {
203 if (!SimplifyLoopInst(&L, &AR.DT, &AR.LI, &AR.AC, &AR.TLI))
Dehao Chendcafd5e2016-07-15 16:42:11 +0000204 return PreservedAnalyses::all();
205
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000206 auto PA = getLoopPassPreservedAnalyses();
207 PA.preserveSet<CFGAnalyses>();
208 return PA;
Dehao Chendcafd5e2016-07-15 16:42:11 +0000209}
210
211char LoopInstSimplifyLegacyPass::ID = 0;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000212
Dehao Chendcafd5e2016-07-15 16:42:11 +0000213INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
214 "Simplify instructions in loops", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000215INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Dehao Chendcafd5e2016-07-15 16:42:11 +0000216INITIALIZE_PASS_DEPENDENCY(LoopPass)
217INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
218INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
219 "Simplify instructions in loops", false, false)
220
221Pass *llvm::createLoopInstSimplifyPass() {
222 return new LoopInstSimplifyLegacyPass();
223}