blob: 22ec48dde3f5e6955dcfff05047a6bb30c7ef990 [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 Zwarich832f6112011-01-03 00:25:16 +000015#include "llvm/Analysis/Dominators.h"
16#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000017#include "llvm/Analysis/LoopInfo.h"
Cameron Zwariche389ab12011-01-05 05:15:53 +000018#include "llvm/Analysis/LoopPass.h"
19#include "llvm/Support/Debug.h"
Cameron Zwarich832f6112011-01-03 00:25:16 +000020#include "llvm/Target/TargetData.h"
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Transforms/Utils/Local.h"
23#include "llvm/ADT/Statistic.h"
24using namespace llvm;
25
26STATISTIC(NumSimplified, "Number of redundant instructions simplified");
27
28namespace {
Cameron Zwariche389ab12011-01-05 05:15:53 +000029 class LoopInstSimplify : public LoopPass {
Cameron Zwarich832f6112011-01-03 00:25:16 +000030 public:
31 static char ID; // Pass ID, replacement for typeid
Cameron Zwariche389ab12011-01-05 05:15:53 +000032 LoopInstSimplify() : LoopPass(ID) {
Cameron Zwarich832f6112011-01-03 00:25:16 +000033 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
34 }
35
Cameron Zwariche389ab12011-01-05 05:15:53 +000036 bool runOnLoop(Loop*, LPPassManager&);
Cameron Zwarich832f6112011-01-03 00:25:16 +000037
Cameron Zwarich64573ae2011-01-04 18:19:19 +000038 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Cameron Zwarich832f6112011-01-03 00:25:16 +000039 AU.setPreservesCFG();
Cameron Zwarich832f6112011-01-03 00:25:16 +000040 AU.addRequired<LoopInfo>();
Cameron Zwariche389ab12011-01-05 05:15:53 +000041 AU.addRequiredID(LoopSimplifyID);
Cameron Zwarich832f6112011-01-03 00:25:16 +000042 AU.addPreservedID(LCSSAID);
43 }
44 };
45}
46
47char LoopInstSimplify::ID = 0;
48INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
49 "Simplify instructions in loops", false, false)
50INITIALIZE_PASS_DEPENDENCY(DominatorTree)
51INITIALIZE_PASS_DEPENDENCY(LoopInfo)
52INITIALIZE_PASS_DEPENDENCY(LCSSA)
53INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
54 "Simplify instructions in loops", false, false)
55
56Pass* llvm::createLoopInstSimplifyPass() {
57 return new LoopInstSimplify();
58}
59
Cameron Zwariche389ab12011-01-05 05:15:53 +000060bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Cameron Zwarich64573ae2011-01-04 18:19:19 +000061 DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
62 LoopInfo *LI = &getAnalysis<LoopInfo>();
63 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Cameron Zwarich832f6112011-01-03 00:25:16 +000064
Cameron Zwariche389ab12011-01-05 05:15:53 +000065 SmallVector<BasicBlock*, 8> ExitBlocks;
66 L->getUniqueExitBlocks(ExitBlocks);
67 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
68
Cameron Zwarich08602ab2011-01-05 05:47:47 +000069 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
70
Cameron Zwariche389ab12011-01-05 05:15:53 +000071 SmallVector<BasicBlock*, 16> VisitStack;
72 SmallPtrSet<BasicBlock*, 32> Visited;
73
Cameron Zwarich832f6112011-01-03 00:25:16 +000074 bool Changed = false;
75 bool LocalChanged;
76 do {
77 LocalChanged = false;
78
Cameron Zwariche389ab12011-01-05 05:15:53 +000079 VisitStack.clear();
80 Visited.clear();
81
82 VisitStack.push_back(L->getHeader());
83
84 while (!VisitStack.empty()) {
85 BasicBlock *BB = VisitStack.back();
86 VisitStack.pop_back();
87
88 // Simplify instructions in the current basic block.
89 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Cameron Zwarich64573ae2011-01-04 18:19:19 +000090 Instruction *I = BI++;
Cameron Zwarich08602ab2011-01-05 05:47:47 +000091
92 // The first time through the loop ToSimplify is empty and we try to
93 // simplify all instructions. On later iterations ToSimplify is not
94 // empty and we only bother simplifying instructions that are in it.
95 if (!ToSimplify->empty() && !ToSimplify->count(I))
96 continue;
97
Cameron Zwarich832f6112011-01-03 00:25:16 +000098 // Don't bother simplifying unused instructions.
99 if (!I->use_empty()) {
Cameron Zwarich64573ae2011-01-04 18:19:19 +0000100 Value *V = SimplifyInstruction(I, TD, DT);
Cameron Zwaricha1cb5852011-01-04 00:12:46 +0000101 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich08602ab2011-01-05 05:47:47 +0000102 // Mark all uses for resimplification next time round the loop.
103 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
104 UI != UE; ++UI)
105 Next->insert(cast<Instruction>(*UI));
106
Cameron Zwarich832f6112011-01-03 00:25:16 +0000107 I->replaceAllUsesWith(V);
108 LocalChanged = true;
109 ++NumSimplified;
110 }
111 }
112 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
113 }
Cameron Zwarich832f6112011-01-03 00:25:16 +0000114
Cameron Zwariche389ab12011-01-05 05:15:53 +0000115 // Add all successors to the worklist, except for loop exit blocks.
116 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
117 ++SI) {
118 BasicBlock *SuccBB = *SI;
119 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
120 ExitBlocks.end(), SuccBB);
121 if (!IsExitBlock && Visited.insert(SuccBB))
122 VisitStack.push_back(SuccBB);
123 }
124 }
125
Cameron Zwarich08602ab2011-01-05 05:47:47 +0000126 // Place the list of instructions to simplify on the next loop iteration
127 // into ToSimplify.
128 std::swap(ToSimplify, Next);
129 Next->clear();
130
Cameron Zwaricha1cb5852011-01-04 00:12:46 +0000131 Changed |= LocalChanged;
Cameron Zwarich832f6112011-01-03 00:25:16 +0000132 } while (LocalChanged);
133
Cameron Zwarich832f6112011-01-03 00:25:16 +0000134 return Changed;
135}