blob: bf84c937ace100d17ed49ac763ad1ea45080ba8d [file] [log] [blame]
Chris Lattnere67dbc22004-05-23 21:19:55 +00001//===-- GCSE.cpp - SSA-based Global Common Subexpression Elimination ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1467f642002-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
Chris Lattner1b09a9a2002-08-30 22:53:30 +000012// using an existing value numbering implementation to identify the common
13// subexpressions, eliminating them when possible.
Chris Lattner1467f642002-04-28 00:47:11 +000014//
Chris Lattner1467f642002-04-28 00:47:11 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner79a42ac2006-12-19 21:40:18 +000017#define DEBUG_TYPE "gcse"
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner69c49002004-04-10 21:11:11 +000019#include "llvm/Instructions.h"
Chris Lattner024f4ab2007-01-30 23:46:24 +000020#include "llvm/Function.h"
Chris Lattner1b09a9a2002-08-30 22:53:30 +000021#include "llvm/Type.h"
Chris Lattner024f4ab2007-01-30 23:46:24 +000022#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner1467f642002-04-28 00:47:11 +000023#include "llvm/Analysis/Dominators.h"
Chris Lattnerb2a31092002-08-30 20:22:29 +000024#include "llvm/Analysis/ValueNumbering.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/ADT/DepthFirstIterator.h"
26#include "llvm/ADT/Statistic.h"
Reid Spencer557ab152007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattner1467f642002-04-28 00:47:11 +000028#include <algorithm>
Chris Lattner49525f82004-01-09 06:02:20 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner79a42ac2006-12-19 21:40:18 +000031STATISTIC(NumInstRemoved, "Number of instructions removed");
32STATISTIC(NumLoadRemoved, "Number of loads removed");
33STATISTIC(NumCallRemoved, "Number of calls removed");
34STATISTIC(NumNonInsts , "Number of instructions removed due "
35 "to non-instruction values");
36STATISTIC(NumArgsRepl , "Number of function arguments replaced "
37 "with constant values");
Chris Lattner1467f642002-04-28 00:47:11 +000038namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000039 struct VISIBILITY_HIDDEN GCSE : public FunctionPass {
Devang Patel09f162c2007-05-01 21:15:47 +000040 static const int ID; // Pass identifcation, replacement for typeid
41 GCSE() : FunctionPass((intptr_t)&ID) {}
42
Chris Lattner113f4f42002-06-25 16:13:24 +000043 virtual bool runOnFunction(Function &F);
Chris Lattner1467f642002-04-28 00:47:11 +000044
Chris Lattner1467f642002-04-28 00:47:11 +000045 private:
Chris Lattner69c49002004-04-10 21:11:11 +000046 void ReplaceInstructionWith(Instruction *I, Value *V);
Chris Lattnerd38ddb12002-05-14 05:02:40 +000047
Chris Lattner1467f642002-04-28 00:47:11 +000048 // This transformation requires dominator and immediate dominator info
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000050 AU.setPreservesCFG();
Chris Lattnercb367102006-01-11 05:10:20 +000051 AU.addRequired<ETForest>();
Chris Lattner69c49002004-04-10 21:11:11 +000052 AU.addRequired<DominatorTree>();
Chris Lattnerb2a31092002-08-30 20:22:29 +000053 AU.addRequired<ValueNumbering>();
Chris Lattner1467f642002-04-28 00:47:11 +000054 }
55 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000056
Devang Patel09f162c2007-05-01 21:15:47 +000057 const int GCSE::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000058 RegisterPass<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattner1467f642002-04-28 00:47:11 +000059}
60
61// createGCSEPass - The public interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +000062FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattner1467f642002-04-28 00:47:11 +000063
Chris Lattner1467f642002-04-28 00:47:11 +000064// GCSE::runOnFunction - This is the main transformation entry point for a
65// function.
66//
Chris Lattner113f4f42002-06-25 16:13:24 +000067bool GCSE::runOnFunction(Function &F) {
Chris Lattner1467f642002-04-28 00:47:11 +000068 bool Changed = false;
69
Chris Lattner879cb972002-08-22 18:24:48 +000070 // Get pointers to the analysis results that we will be using...
Chris Lattnercb367102006-01-11 05:10:20 +000071 ETForest &EF = getAnalysis<ETForest>();
Chris Lattner69c49002004-04-10 21:11:11 +000072 ValueNumbering &VN = getAnalysis<ValueNumbering>();
73 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner1467f642002-04-28 00:47:11 +000074
Chris Lattner69c49002004-04-10 21:11:11 +000075 std::vector<Value*> EqualValues;
Chris Lattner1467f642002-04-28 00:47:11 +000076
Chris Lattnere67dbc22004-05-23 21:19:55 +000077 // Check for value numbers of arguments. If the value numbering
78 // implementation can prove that an incoming argument is a constant or global
79 // value address, substitute it, making the argument dead.
Chris Lattner28d921d2007-04-14 23:32:02 +000080 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattnere67dbc22004-05-23 21:19:55 +000081 if (!AI->use_empty()) {
82 VN.getEqualNumberNodes(AI, EqualValues);
83 if (!EqualValues.empty()) {
84 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
Reid Spenceref784f02004-07-18 00:32:14 +000085 if (isa<Constant>(EqualValues[i])) {
Chris Lattnere67dbc22004-05-23 21:19:55 +000086 AI->replaceAllUsesWith(EqualValues[i]);
87 ++NumArgsRepl;
88 Changed = true;
89 break;
90 }
91 EqualValues.clear();
92 }
93 }
94
Chris Lattner69c49002004-04-10 21:11:11 +000095 // Traverse the CFG of the function in dominator order, so that we see each
96 // instruction after we see its operands.
97 for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
98 E = df_end(DT.getRootNode()); DI != E; ++DI) {
99 BasicBlock *BB = DI->getBlock();
Chris Lattner1467f642002-04-28 00:47:11 +0000100
Chris Lattner69c49002004-04-10 21:11:11 +0000101 // Remember which instructions we've seen in this basic block as we scan.
102 std::set<Instruction*> BlockInsts;
Chris Lattnerb2a31092002-08-30 20:22:29 +0000103
Chris Lattner69c49002004-04-10 21:11:11 +0000104 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
105 Instruction *Inst = I++;
106
Chris Lattner88deefa2004-12-12 18:23:20 +0000107 if (Constant *C = ConstantFoldInstruction(Inst)) {
108 ReplaceInstructionWith(Inst, C);
109 } else if (Inst->getType() != Type::VoidTy) {
110 // If this instruction computes a value, try to fold together common
111 // instructions that compute it.
112 //
Chris Lattner69c49002004-04-10 21:11:11 +0000113 VN.getEqualNumberNodes(Inst, EqualValues);
114
115 // If this instruction computes a value that is already computed
116 // elsewhere, try to recycle the old value.
117 if (!EqualValues.empty()) {
118 if (Inst == &*BB->begin())
119 I = BB->end();
120 else {
121 I = Inst; --I;
122 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123
Chris Lattner69c49002004-04-10 21:11:11 +0000124 // First check to see if we were able to value number this instruction
125 // to a non-instruction value. If so, prefer that value over other
126 // instructions which may compute the same thing.
127 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
128 if (!isa<Instruction>(EqualValues[i])) {
129 ++NumNonInsts; // Keep track of # of insts repl with values
130
131 // Change all users of Inst to use the replacement and remove it
132 // from the program.
133 ReplaceInstructionWith(Inst, EqualValues[i]);
134 Inst = 0;
135 EqualValues.clear(); // don't enter the next loop
136 break;
137 }
138
139 // If there were no non-instruction values that this instruction
140 // produces, find a dominating instruction that produces the same
141 // value. If we find one, use it's value instead of ours.
142 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
143 Instruction *OtherI = cast<Instruction>(EqualValues[i]);
144 bool Dominates = false;
145 if (OtherI->getParent() == BB)
146 Dominates = BlockInsts.count(OtherI);
147 else
Chris Lattnercb367102006-01-11 05:10:20 +0000148 Dominates = EF.dominates(OtherI->getParent(), BB);
Chris Lattner69c49002004-04-10 21:11:11 +0000149
150 if (Dominates) {
151 // Okay, we found an instruction with the same value as this one
152 // and that dominates this one. Replace this instruction with the
153 // specified one.
154 ReplaceInstructionWith(Inst, OtherI);
155 Inst = 0;
156 break;
157 }
158 }
159
160 EqualValues.clear();
161
162 if (Inst) {
163 I = Inst; ++I; // Deleted no instructions
164 } else if (I == BB->end()) { // Deleted first instruction
165 I = BB->begin();
166 } else { // Deleted inst in middle of block.
167 ++I;
168 }
169 }
170
171 if (Inst)
172 BlockInsts.insert(Inst);
173 }
Chris Lattnerb2a31092002-08-30 20:22:29 +0000174 }
Chris Lattner1467f642002-04-28 00:47:11 +0000175 }
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000176
Chris Lattner1467f642002-04-28 00:47:11 +0000177 // When the worklist is empty, return whether or not we changed anything...
178 return Changed;
179}
180
Chris Lattnerb2a31092002-08-30 20:22:29 +0000181
Chris Lattner69c49002004-04-10 21:11:11 +0000182void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
183 if (isa<LoadInst>(I))
184 ++NumLoadRemoved; // Keep track of loads eliminated
185 if (isa<CallInst>(I))
186 ++NumCallRemoved; // Keep track of calls eliminated
187 ++NumInstRemoved; // Keep track of number of insts eliminated
Chris Lattnerb2a31092002-08-30 20:22:29 +0000188
Chris Lattnerf16fe722004-04-10 22:33:34 +0000189 // Update value numbering
Chris Lattnere67dbc22004-05-23 21:19:55 +0000190 getAnalysis<ValueNumbering>().deleteValue(I);
Chris Lattnerf16fe722004-04-10 22:33:34 +0000191
Chris Lattner88deefa2004-12-12 18:23:20 +0000192 I->replaceAllUsesWith(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000193
Chris Lattner494a6852004-04-12 05:15:13 +0000194 if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
195 // Removing an invoke instruction requires adding a branch to the normal
196 // destination and removing PHI node entries in the exception destination.
197 new BranchInst(II->getNormalDest(), II);
198 II->getUnwindDest()->removePredecessor(II->getParent());
199 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000200
Chris Lattner69c49002004-04-10 21:11:11 +0000201 // Erase the instruction from the program.
202 I->getParent()->getInstList().erase(I);
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000203}