blob: 5d7e431221ee40077e5f72449d7e1dce15f7a88e [file] [log] [blame]
Cameron Zwarich832f6112011-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
14#define DEBUG_TYPE "loop-instsimplify"
Cameron Zwarich8368ac32011-01-08 15:52:22 +000015#include "llvm/Instructions.h"
Cameron Zwarich832f6112011-01-03 00:25:16 +000016#include "llvm/Analysis/Dominators.h"
17#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000018#include "llvm/Analysis/LoopInfo.h"
Cameron Zwariche389ab12011-01-05 05:15:53 +000019#include "llvm/Analysis/LoopPass.h"
20#include "llvm/Support/Debug.h"
Cameron Zwarich832f6112011-01-03 00:25:16 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Transforms/Scalar.h"
23#include "llvm/Transforms/Utils/Local.h"
24#include "llvm/ADT/Statistic.h"
25using namespace llvm;
26
27STATISTIC(NumSimplified, "Number of redundant instructions simplified");
28
29namespace {
Cameron Zwariche389ab12011-01-05 05:15:53 +000030 class LoopInstSimplify : public LoopPass {
Cameron Zwarich832f6112011-01-03 00:25:16 +000031 public:
32 static char ID; // Pass ID, replacement for typeid
Cameron Zwariche389ab12011-01-05 05:15:53 +000033 LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarich832f6112011-01-03 00:25:16 +000034 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
35 }
36
Cameron Zwariche389ab12011-01-05 05:15:53 +000037 bool runOnLoop(Loop*, LPPassManager&);
Cameron Zwarich832f6112011-01-03 00:25:16 +000038
Cameron Zwarich64573ae2011-01-04 18:19:19 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Cameron Zwarich832f6112011-01-03 00:25:16 +000040 AU.setPreservesCFG();
Cameron Zwarich832f6112011-01-03 00:25:16 +000041 AU.addRequired<LoopInfo>();
Cameron Zwariche389ab12011-01-05 05:15:53 +000042 AU.addRequiredID(LoopSimplifyID);
Cameron Zwariche7d78652011-01-09 12:35:16 +000043 AU.addPreservedID(LoopSimplifyID);
Cameron Zwarich832f6112011-01-03 00:25:16 +000044 AU.addPreservedID(LCSSAID);
45 }
46 };
47}
48
49char LoopInstSimplify::ID = 0;
50INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
51 "Simplify instructions in loops", false, false)
52INITIALIZE_PASS_DEPENDENCY(DominatorTree)
53INITIALIZE_PASS_DEPENDENCY(LoopInfo)
54INITIALIZE_PASS_DEPENDENCY(LCSSA)
55INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
56 "Simplify instructions in loops", false, false)
57
Cameron Zwarichb434acb2011-01-08 17:07:11 +000058Pass *llvm::createLoopInstSimplifyPass() {
Cameron Zwarich832f6112011-01-03 00:25:16 +000059 return new LoopInstSimplify();
60}
61
Cameron Zwariche389ab12011-01-05 05:15:53 +000062bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Cameron Zwarich64573ae2011-01-04 18:19:19 +000063 DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
64 LoopInfo *LI = &getAnalysis<LoopInfo>();
65 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Cameron Zwarich832f6112011-01-03 00:25:16 +000066
Cameron Zwariche389ab12011-01-05 05:15:53 +000067 SmallVector<BasicBlock*, 8> ExitBlocks;
68 L->getUniqueExitBlocks(ExitBlocks);
69 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
70
Cameron Zwarich08602ab2011-01-05 05:47:47 +000071 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
72
Cameron Zwarich8368ac32011-01-08 15:52:22 +000073 // The bit we are stealing from the pointer represents whether this basic
74 // block is the header of a subloop, in which case we only process its phis.
75 typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
76 SmallVector<WorklistItem, 16> VisitStack;
Cameron Zwariche389ab12011-01-05 05:15:53 +000077 SmallPtrSet<BasicBlock*, 32> Visited;
78
Cameron Zwarich832f6112011-01-03 00:25:16 +000079 bool Changed = false;
80 bool LocalChanged;
81 do {
82 LocalChanged = false;
83
Cameron Zwariche389ab12011-01-05 05:15:53 +000084 VisitStack.clear();
85 Visited.clear();
86
Cameron Zwarich8368ac32011-01-08 15:52:22 +000087 VisitStack.push_back(WorklistItem(L->getHeader(), false));
Cameron Zwariche389ab12011-01-05 05:15:53 +000088
89 while (!VisitStack.empty()) {
Cameron Zwarich8368ac32011-01-08 15:52:22 +000090 WorklistItem Item = VisitStack.pop_back_val();
Cameron Zwarichb434acb2011-01-08 17:07:11 +000091 BasicBlock *BB = Item.getPointer();
Cameron Zwarich8368ac32011-01-08 15:52:22 +000092 bool IsSubloopHeader = Item.getInt();
Cameron Zwariche389ab12011-01-05 05:15:53 +000093
94 // Simplify instructions in the current basic block.
95 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Cameron Zwarich64573ae2011-01-04 18:19:19 +000096 Instruction *I = BI++;
Cameron Zwarich08602ab2011-01-05 05:47:47 +000097
98 // The first time through the loop ToSimplify is empty and we try to
99 // simplify all instructions. On later iterations ToSimplify is not
100 // empty and we only bother simplifying instructions that are in it.
101 if (!ToSimplify->empty() && !ToSimplify->count(I))
102 continue;
103
Cameron Zwarich832f6112011-01-03 00:25:16 +0000104 // Don't bother simplifying unused instructions.
105 if (!I->use_empty()) {
Cameron Zwarich64573ae2011-01-04 18:19:19 +0000106 Value *V = SimplifyInstruction(I, TD, DT);
Cameron Zwaricha1cb5852011-01-04 00:12:46 +0000107 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich08602ab2011-01-05 05:47:47 +0000108 // Mark all uses for resimplification next time round the loop.
109 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
110 UI != UE; ++UI)
111 Next->insert(cast<Instruction>(*UI));
112
Cameron Zwarich832f6112011-01-03 00:25:16 +0000113 I->replaceAllUsesWith(V);
114 LocalChanged = true;
115 ++NumSimplified;
116 }
117 }
118 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
Cameron Zwarich8368ac32011-01-08 15:52:22 +0000119
120 if (IsSubloopHeader && !isa<PHINode>(I))
121 break;
Cameron Zwarich832f6112011-01-03 00:25:16 +0000122 }
Cameron Zwarich832f6112011-01-03 00:25:16 +0000123
Cameron Zwarich8368ac32011-01-08 15:52:22 +0000124 // Add all successors to the worklist, except for loop exit blocks and the
125 // bodies of subloops. We visit the headers of loops so that we can process
126 // their phis, but we contract the rest of the subloop body and only follow
127 // edges leading back to the original loop.
Cameron Zwariche389ab12011-01-05 05:15:53 +0000128 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
129 ++SI) {
130 BasicBlock *SuccBB = *SI;
Cameron Zwarich8368ac32011-01-08 15:52:22 +0000131 if (!Visited.insert(SuccBB))
132 continue;
133
134 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
135 if (SuccLoop && SuccLoop->getHeader() == SuccBB
136 && L->contains(SuccLoop)) {
137 VisitStack.push_back(WorklistItem(SuccBB, true));
138
139 SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
140 SuccLoop->getExitBlocks(SubLoopExitBlocks);
141
142 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
143 BasicBlock *ExitBB = SubLoopExitBlocks[i];
144 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB))
145 VisitStack.push_back(WorklistItem(ExitBB, false));
146 }
147
148 continue;
149 }
150
Cameron Zwariche389ab12011-01-05 05:15:53 +0000151 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
Cameron Zwarich8368ac32011-01-08 15:52:22 +0000152 ExitBlocks.end(), SuccBB);
153 if (IsExitBlock)
154 continue;
155
156 VisitStack.push_back(WorklistItem(SuccBB, false));
Cameron Zwariche389ab12011-01-05 05:15:53 +0000157 }
158 }
159
Cameron Zwarich08602ab2011-01-05 05:47:47 +0000160 // Place the list of instructions to simplify on the next loop iteration
161 // into ToSimplify.
162 std::swap(ToSimplify, Next);
163 Next->clear();
164
Cameron Zwaricha1cb5852011-01-04 00:12:46 +0000165 Changed |= LocalChanged;
Cameron Zwarich832f6112011-01-03 00:25:16 +0000166 } while (LocalChanged);
167
Cameron Zwarich832f6112011-01-03 00:25:16 +0000168 return Changed;
169}