blob: 431ac475ac6b293bbf63939311985ce2e151cb61 [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"
Chandler Carruth31088a92016-02-19 10:45:18 +000028#include "llvm/Transforms/Utils/LoopUtils.h"
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000029using namespace llvm;
30
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "loop-instsimplify"
32
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000033STATISTIC(NumSimplified, "Number of redundant instructions simplified");
34
35namespace {
Cameron Zwarich4c51d122011-01-05 05:15:53 +000036 class LoopInstSimplify : public LoopPass {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000037 public:
38 static char ID; // Pass ID, replacement for typeid
Cameron Zwarich4c51d122011-01-05 05:15:53 +000039 LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000040 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
41 }
42
Craig Topper3e4c6972014-03-05 09:10:37 +000043 bool runOnLoop(Loop*, LPPassManager&) override;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000044
Craig Topper3e4c6972014-03-05 09:10:37 +000045 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth66b31302015-01-04 12:03:27 +000046 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000047 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth31088a92016-02-19 10:45:18 +000048 AU.setPreservesCFG();
49 getLoopAnalysisUsage(AU);
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000050 }
51 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000052}
Nadav Rotem465834c2012-07-24 10:51:42 +000053
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000054char LoopInstSimplify::ID = 0;
55INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
56 "Simplify instructions in loops", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +000057INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth31088a92016-02-19 10:45:18 +000058INITIALIZE_PASS_DEPENDENCY(LoopPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000059INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000060INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
61 "Simplify instructions in loops", false, false)
62
Cameron Zwarichb4ab2572011-01-08 17:07:11 +000063Pass *llvm::createLoopInstSimplifyPass() {
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000064 return new LoopInstSimplify();
65}
66
Cameron Zwarich4c51d122011-01-05 05:15:53 +000067bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Andrew Kayloraa641a52016-04-22 22:06:11 +000068 if (skipLoop(L))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000069 return false;
70
Chandler Carruth73523022014-01-13 13:07:17 +000071 DominatorTreeWrapperPass *DTWP =
72 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000073 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Chandler Carruth4f8f3072015-01-17 14:16:18 +000074 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000075 const TargetLibraryInfo *TLI =
76 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +000077 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
78 *L->getHeader()->getParent());
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000079
Cameron Zwarich4c51d122011-01-05 05:15:53 +000080 SmallVector<BasicBlock*, 8> ExitBlocks;
81 L->getUniqueExitBlocks(ExitBlocks);
82 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
83
Cameron Zwarich5a2bb992011-01-05 05:47:47 +000084 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
85
Cameron Zwarich80bd9af2011-01-08 15:52:22 +000086 // The bit we are stealing from the pointer represents whether this basic
87 // block is the header of a subloop, in which case we only process its phis.
88 typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
89 SmallVector<WorklistItem, 16> VisitStack;
Cameron Zwarich4c51d122011-01-05 05:15:53 +000090 SmallPtrSet<BasicBlock*, 32> Visited;
91
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +000092 bool Changed = false;
93 bool LocalChanged;
94 do {
95 LocalChanged = false;
96
Cameron Zwarich4c51d122011-01-05 05:15:53 +000097 VisitStack.clear();
98 Visited.clear();
99
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000100 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000101
102 while (!VisitStack.empty()) {
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000103 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb4ab2572011-01-08 17:07:11 +0000104 BasicBlock *BB = Item.getPointer();
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000105 bool IsSubloopHeader = Item.getInt();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000106 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000107
108 // Simplify instructions in the current basic block.
109 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000110 Instruction *I = &*BI++;
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000111
112 // The first time through the loop ToSimplify is empty and we try to
113 // simplify all instructions. On later iterations ToSimplify is not
114 // empty and we only bother simplifying instructions that are in it.
115 if (!ToSimplify->empty() && !ToSimplify->count(I))
116 continue;
117
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000118 // Don't bother simplifying unused instructions.
119 if (!I->use_empty()) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000120 Value *V = SimplifyInstruction(I, DL, TLI, DT, &AC);
Cameron Zwariche9249692011-01-04 00:12:46 +0000121 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000122 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000123 for (User *U : I->users())
124 Next->insert(cast<Instruction>(U));
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000125
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000126 I->replaceAllUsesWith(V);
127 LocalChanged = true;
128 ++NumSimplified;
129 }
130 }
Chad Rosier074ce832016-04-06 13:27:13 +0000131 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
132 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
133 // instruction, so simply incrementing the iterator does not work.
134 // When instructions get deleted re-iterate instead.
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +0000135 BI = BB->begin(); BE = BB->end();
Chad Rosier074ce832016-04-06 13:27:13 +0000136 LocalChanged = true;
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +0000137 }
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000138
139 if (IsSubloopHeader && !isa<PHINode>(I))
140 break;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000141 }
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000142
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000143 // Add all successors to the worklist, except for loop exit blocks and the
144 // bodies of subloops. We visit the headers of loops so that we can process
145 // their phis, but we contract the rest of the subloop body and only follow
146 // edges leading back to the original loop.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000147 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
148 ++SI) {
149 BasicBlock *SuccBB = *SI;
David Blaikie70573dc2014-11-19 07:49:26 +0000150 if (!Visited.insert(SuccBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000151 continue;
152
153 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
154 if (SuccLoop && SuccLoop->getHeader() == SuccBB
155 && L->contains(SuccLoop)) {
156 VisitStack.push_back(WorklistItem(SuccBB, true));
157
158 SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
159 SuccLoop->getExitBlocks(SubLoopExitBlocks);
160
161 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
162 BasicBlock *ExitBB = SubLoopExitBlocks[i];
David Blaikie70573dc2014-11-19 07:49:26 +0000163 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000164 VisitStack.push_back(WorklistItem(ExitBB, false));
165 }
166
167 continue;
168 }
169
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000170 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
Cameron Zwarich80bd9af2011-01-08 15:52:22 +0000171 ExitBlocks.end(), SuccBB);
172 if (IsExitBlock)
173 continue;
174
175 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwarich4c51d122011-01-05 05:15:53 +0000176 }
177 }
178
Cameron Zwarich5a2bb992011-01-05 05:47:47 +0000179 // Place the list of instructions to simplify on the next loop iteration
180 // into ToSimplify.
181 std::swap(ToSimplify, Next);
182 Next->clear();
183
Cameron Zwariche9249692011-01-04 00:12:46 +0000184 Changed |= LocalChanged;
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000185 } while (LocalChanged);
186
Cameron Zwarichcab9a0a2011-01-03 00:25:16 +0000187 return Changed;
188}