blob: 94cdcb1400f1bc34c83ce4b3b72e6a6a9b9a104a [file] [log] [blame]
Chris Lattner057f78a2004-05-23 21:19:55 +00001//===-- GCSE.cpp - SSA-based Global Common Subexpression Elimination ------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd80e9732002-04-28 00:47:11 +00009//
10// This pass is designed to be a very quick global transformation that
11// eliminates global common subexpressions from a function. It does this by
Matthijs Kooijman845f5242008-06-05 07:55:49 +000012// using an existing value numbering analysis pass to identify the common
Chris Lattner2964f362002-08-30 22:53:30 +000013// subexpressions, eliminating them when possible.
Chris Lattnerd80e9732002-04-28 00:47:11 +000014//
Matthijs Kooijman845f5242008-06-05 07:55:49 +000015// This pass is deprecated by the Global Value Numbering pass (which does a
16// better job with its own value numbering).
17//
Chris Lattnerd80e9732002-04-28 00:47:11 +000018//===----------------------------------------------------------------------===//
19
Chris Lattner0e5f4992006-12-19 21:40:18 +000020#define DEBUG_TYPE "gcse"
Chris Lattner022103b2002-05-07 20:03:00 +000021#include "llvm/Transforms/Scalar.h"
Chris Lattner38e66bd2004-04-10 21:11:11 +000022#include "llvm/Instructions.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000023#include "llvm/Function.h"
Chris Lattner2964f362002-08-30 22:53:30 +000024#include "llvm/Type.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000025#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000026#include "llvm/Analysis/Dominators.h"
Chris Lattner14987f12002-08-30 20:22:29 +000027#include "llvm/Analysis/ValueNumbering.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/DepthFirstIterator.h"
29#include "llvm/ADT/Statistic.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000030#include "llvm/Support/Compiler.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000031#include <algorithm>
Chris Lattnerd7456022004-01-09 06:02:20 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner0e5f4992006-12-19 21:40:18 +000034STATISTIC(NumInstRemoved, "Number of instructions removed");
35STATISTIC(NumLoadRemoved, "Number of loads removed");
36STATISTIC(NumCallRemoved, "Number of calls removed");
37STATISTIC(NumNonInsts , "Number of instructions removed due "
38 "to non-instruction values");
39STATISTIC(NumArgsRepl , "Number of function arguments replaced "
40 "with constant values");
Chris Lattnerd80e9732002-04-28 00:47:11 +000041namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000042 struct VISIBILITY_HIDDEN GCSE : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000043 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000044 GCSE() : FunctionPass((intptr_t)&ID) {}
45
Chris Lattner7e708292002-06-25 16:13:24 +000046 virtual bool runOnFunction(Function &F);
Chris Lattnerd80e9732002-04-28 00:47:11 +000047
Chris Lattnerd80e9732002-04-28 00:47:11 +000048 private:
Chris Lattner38e66bd2004-04-10 21:11:11 +000049 void ReplaceInstructionWith(Instruction *I, Value *V);
Chris Lattner18fb2a62002-05-14 05:02:40 +000050
Chris Lattnerd80e9732002-04-28 00:47:11 +000051 // This transformation requires dominator and immediate dominator info
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000053 AU.setPreservesCFG();
Chris Lattner38e66bd2004-04-10 21:11:11 +000054 AU.addRequired<DominatorTree>();
Chris Lattner14987f12002-08-30 20:22:29 +000055 AU.addRequired<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000056 }
57 };
58}
59
Dan Gohman844731a2008-05-13 00:00:25 +000060char GCSE::ID = 0;
61static RegisterPass<GCSE>
62X("gcse", "Global Common Subexpression Elimination");
63
Chris Lattnerd80e9732002-04-28 00:47:11 +000064// createGCSEPass - The public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000065FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattnerd80e9732002-04-28 00:47:11 +000066
Chris Lattnerd80e9732002-04-28 00:47:11 +000067// GCSE::runOnFunction - This is the main transformation entry point for a
68// function.
69//
Chris Lattner7e708292002-06-25 16:13:24 +000070bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000071 bool Changed = false;
72
Chris Lattnerd456ec92002-08-22 18:24:48 +000073 // Get pointers to the analysis results that we will be using...
Chris Lattner38e66bd2004-04-10 21:11:11 +000074 DominatorTree &DT = getAnalysis<DominatorTree>();
Devang Patel387b8cb2007-06-07 18:40:55 +000075 ValueNumbering &VN = getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000076
Chris Lattner38e66bd2004-04-10 21:11:11 +000077 std::vector<Value*> EqualValues;
Chris Lattnerd80e9732002-04-28 00:47:11 +000078
Chris Lattner057f78a2004-05-23 21:19:55 +000079 // Check for value numbers of arguments. If the value numbering
80 // implementation can prove that an incoming argument is a constant or global
81 // value address, substitute it, making the argument dead.
Chris Lattnere34e9a22007-04-14 23:32:02 +000082 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattner057f78a2004-05-23 21:19:55 +000083 if (!AI->use_empty()) {
84 VN.getEqualNumberNodes(AI, EqualValues);
85 if (!EqualValues.empty()) {
86 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
Reid Spencer460f16c2004-07-18 00:32:14 +000087 if (isa<Constant>(EqualValues[i])) {
Chris Lattner057f78a2004-05-23 21:19:55 +000088 AI->replaceAllUsesWith(EqualValues[i]);
89 ++NumArgsRepl;
90 Changed = true;
91 break;
92 }
93 EqualValues.clear();
94 }
95 }
96
Chris Lattner38e66bd2004-04-10 21:11:11 +000097 // Traverse the CFG of the function in dominator order, so that we see each
98 // instruction after we see its operands.
Devang Patel26042422007-06-04 00:32:22 +000099 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
Chris Lattner38e66bd2004-04-10 21:11:11 +0000100 E = df_end(DT.getRootNode()); DI != E; ++DI) {
101 BasicBlock *BB = DI->getBlock();
Chris Lattnerd80e9732002-04-28 00:47:11 +0000102
Chris Lattner38e66bd2004-04-10 21:11:11 +0000103 // Remember which instructions we've seen in this basic block as we scan.
104 std::set<Instruction*> BlockInsts;
Chris Lattner14987f12002-08-30 20:22:29 +0000105
Chris Lattner38e66bd2004-04-10 21:11:11 +0000106 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
107 Instruction *Inst = I++;
108
Chris Lattnerfb851ab2004-12-12 18:23:20 +0000109 if (Constant *C = ConstantFoldInstruction(Inst)) {
110 ReplaceInstructionWith(Inst, C);
111 } else if (Inst->getType() != Type::VoidTy) {
112 // If this instruction computes a value, try to fold together common
113 // instructions that compute it.
114 //
Chris Lattner38e66bd2004-04-10 21:11:11 +0000115 VN.getEqualNumberNodes(Inst, EqualValues);
116
117 // If this instruction computes a value that is already computed
118 // elsewhere, try to recycle the old value.
119 if (!EqualValues.empty()) {
120 if (Inst == &*BB->begin())
121 I = BB->end();
122 else {
123 I = Inst; --I;
124 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000125
Chris Lattner38e66bd2004-04-10 21:11:11 +0000126 // First check to see if we were able to value number this instruction
127 // to a non-instruction value. If so, prefer that value over other
128 // instructions which may compute the same thing.
129 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
130 if (!isa<Instruction>(EqualValues[i])) {
131 ++NumNonInsts; // Keep track of # of insts repl with values
132
133 // Change all users of Inst to use the replacement and remove it
134 // from the program.
135 ReplaceInstructionWith(Inst, EqualValues[i]);
136 Inst = 0;
137 EqualValues.clear(); // don't enter the next loop
138 break;
139 }
140
141 // If there were no non-instruction values that this instruction
142 // produces, find a dominating instruction that produces the same
143 // value. If we find one, use it's value instead of ours.
144 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
145 Instruction *OtherI = cast<Instruction>(EqualValues[i]);
146 bool Dominates = false;
147 if (OtherI->getParent() == BB)
148 Dominates = BlockInsts.count(OtherI);
149 else
Devang Patela97e0402007-06-07 18:45:06 +0000150 Dominates = DT.dominates(OtherI->getParent(), BB);
Chris Lattner38e66bd2004-04-10 21:11:11 +0000151
152 if (Dominates) {
153 // Okay, we found an instruction with the same value as this one
154 // and that dominates this one. Replace this instruction with the
155 // specified one.
156 ReplaceInstructionWith(Inst, OtherI);
157 Inst = 0;
158 break;
159 }
160 }
161
162 EqualValues.clear();
163
164 if (Inst) {
165 I = Inst; ++I; // Deleted no instructions
166 } else if (I == BB->end()) { // Deleted first instruction
167 I = BB->begin();
168 } else { // Deleted inst in middle of block.
169 ++I;
170 }
171 }
172
173 if (Inst)
174 BlockInsts.insert(Inst);
175 }
Chris Lattner14987f12002-08-30 20:22:29 +0000176 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000177 }
Chris Lattner18fb2a62002-05-14 05:02:40 +0000178
Chris Lattnerd80e9732002-04-28 00:47:11 +0000179 // When the worklist is empty, return whether or not we changed anything...
180 return Changed;
181}
182
Chris Lattner14987f12002-08-30 20:22:29 +0000183
Chris Lattner38e66bd2004-04-10 21:11:11 +0000184void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
185 if (isa<LoadInst>(I))
186 ++NumLoadRemoved; // Keep track of loads eliminated
187 if (isa<CallInst>(I))
188 ++NumCallRemoved; // Keep track of calls eliminated
189 ++NumInstRemoved; // Keep track of number of insts eliminated
Chris Lattner14987f12002-08-30 20:22:29 +0000190
Chris Lattneradb7c0d2004-04-10 22:33:34 +0000191 // Update value numbering
Chris Lattner057f78a2004-05-23 21:19:55 +0000192 getAnalysis<ValueNumbering>().deleteValue(I);
Chris Lattneradb7c0d2004-04-10 22:33:34 +0000193
Chris Lattnerfb851ab2004-12-12 18:23:20 +0000194 I->replaceAllUsesWith(V);
Misha Brukmanfd939082005-04-21 23:48:37 +0000195
Chris Lattner1708d122004-04-12 05:15:13 +0000196 if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
197 // Removing an invoke instruction requires adding a branch to the normal
198 // destination and removing PHI node entries in the exception destination.
Gabor Greif051a9502008-04-06 20:25:17 +0000199 BranchInst::Create(II->getNormalDest(), II);
Chris Lattner1708d122004-04-12 05:15:13 +0000200 II->getUnwindDest()->removePredecessor(II->getParent());
201 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000202
Chris Lattner38e66bd2004-04-10 21:11:11 +0000203 // Erase the instruction from the program.
204 I->getParent()->getInstList().erase(I);
Chris Lattner18fb2a62002-05-14 05:02:40 +0000205}