blob: 7fef306985b76fd179c607d766c2f4c29c5b33a0 [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"
Chris Lattner1467f642002-04-28 00:47:11 +000027#include <algorithm>
Chris Lattner49525f82004-01-09 06:02:20 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattner79a42ac2006-12-19 21:40:18 +000030STATISTIC(NumInstRemoved, "Number of instructions removed");
31STATISTIC(NumLoadRemoved, "Number of loads removed");
32STATISTIC(NumCallRemoved, "Number of calls removed");
33STATISTIC(NumNonInsts , "Number of instructions removed due "
34 "to non-instruction values");
35STATISTIC(NumArgsRepl , "Number of function arguments replaced "
36 "with constant values");
Chris Lattner1467f642002-04-28 00:47:11 +000037namespace {
Chris Lattner69c49002004-04-10 21:11:11 +000038 struct GCSE : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000039 virtual bool runOnFunction(Function &F);
Chris Lattner1467f642002-04-28 00:47:11 +000040
Chris Lattner1467f642002-04-28 00:47:11 +000041 private:
Chris Lattner69c49002004-04-10 21:11:11 +000042 void ReplaceInstructionWith(Instruction *I, Value *V);
Chris Lattnerd38ddb12002-05-14 05:02:40 +000043
Chris Lattner1467f642002-04-28 00:47:11 +000044 // This transformation requires dominator and immediate dominator info
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000046 AU.setPreservesCFG();
Chris Lattnercb367102006-01-11 05:10:20 +000047 AU.addRequired<ETForest>();
Chris Lattner69c49002004-04-10 21:11:11 +000048 AU.addRequired<DominatorTree>();
Chris Lattnerb2a31092002-08-30 20:22:29 +000049 AU.addRequired<ValueNumbering>();
Chris Lattner1467f642002-04-28 00:47:11 +000050 }
51 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000052
Chris Lattnerc2d3d312006-08-27 22:42:52 +000053 RegisterPass<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattner1467f642002-04-28 00:47:11 +000054}
55
56// createGCSEPass - The public interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +000057FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattner1467f642002-04-28 00:47:11 +000058
Chris Lattner1467f642002-04-28 00:47:11 +000059// GCSE::runOnFunction - This is the main transformation entry point for a
60// function.
61//
Chris Lattner113f4f42002-06-25 16:13:24 +000062bool GCSE::runOnFunction(Function &F) {
Chris Lattner1467f642002-04-28 00:47:11 +000063 bool Changed = false;
64
Chris Lattner879cb972002-08-22 18:24:48 +000065 // Get pointers to the analysis results that we will be using...
Chris Lattnercb367102006-01-11 05:10:20 +000066 ETForest &EF = getAnalysis<ETForest>();
Chris Lattner69c49002004-04-10 21:11:11 +000067 ValueNumbering &VN = getAnalysis<ValueNumbering>();
68 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner1467f642002-04-28 00:47:11 +000069
Chris Lattner69c49002004-04-10 21:11:11 +000070 std::vector<Value*> EqualValues;
Chris Lattner1467f642002-04-28 00:47:11 +000071
Chris Lattnere67dbc22004-05-23 21:19:55 +000072 // Check for value numbers of arguments. If the value numbering
73 // implementation can prove that an incoming argument is a constant or global
74 // value address, substitute it, making the argument dead.
Chris Lattner531f9e92005-03-15 04:54:21 +000075 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI)
Chris Lattnere67dbc22004-05-23 21:19:55 +000076 if (!AI->use_empty()) {
77 VN.getEqualNumberNodes(AI, EqualValues);
78 if (!EqualValues.empty()) {
79 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
Reid Spenceref784f02004-07-18 00:32:14 +000080 if (isa<Constant>(EqualValues[i])) {
Chris Lattnere67dbc22004-05-23 21:19:55 +000081 AI->replaceAllUsesWith(EqualValues[i]);
82 ++NumArgsRepl;
83 Changed = true;
84 break;
85 }
86 EqualValues.clear();
87 }
88 }
89
Chris Lattner69c49002004-04-10 21:11:11 +000090 // Traverse the CFG of the function in dominator order, so that we see each
91 // instruction after we see its operands.
92 for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
93 E = df_end(DT.getRootNode()); DI != E; ++DI) {
94 BasicBlock *BB = DI->getBlock();
Chris Lattner1467f642002-04-28 00:47:11 +000095
Chris Lattner69c49002004-04-10 21:11:11 +000096 // Remember which instructions we've seen in this basic block as we scan.
97 std::set<Instruction*> BlockInsts;
Chris Lattnerb2a31092002-08-30 20:22:29 +000098
Chris Lattner69c49002004-04-10 21:11:11 +000099 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
100 Instruction *Inst = I++;
101
Chris Lattner88deefa2004-12-12 18:23:20 +0000102 if (Constant *C = ConstantFoldInstruction(Inst)) {
103 ReplaceInstructionWith(Inst, C);
104 } else if (Inst->getType() != Type::VoidTy) {
105 // If this instruction computes a value, try to fold together common
106 // instructions that compute it.
107 //
Chris Lattner69c49002004-04-10 21:11:11 +0000108 VN.getEqualNumberNodes(Inst, EqualValues);
109
110 // If this instruction computes a value that is already computed
111 // elsewhere, try to recycle the old value.
112 if (!EqualValues.empty()) {
113 if (Inst == &*BB->begin())
114 I = BB->end();
115 else {
116 I = Inst; --I;
117 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000118
Chris Lattner69c49002004-04-10 21:11:11 +0000119 // First check to see if we were able to value number this instruction
120 // to a non-instruction value. If so, prefer that value over other
121 // instructions which may compute the same thing.
122 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
123 if (!isa<Instruction>(EqualValues[i])) {
124 ++NumNonInsts; // Keep track of # of insts repl with values
125
126 // Change all users of Inst to use the replacement and remove it
127 // from the program.
128 ReplaceInstructionWith(Inst, EqualValues[i]);
129 Inst = 0;
130 EqualValues.clear(); // don't enter the next loop
131 break;
132 }
133
134 // If there were no non-instruction values that this instruction
135 // produces, find a dominating instruction that produces the same
136 // value. If we find one, use it's value instead of ours.
137 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
138 Instruction *OtherI = cast<Instruction>(EqualValues[i]);
139 bool Dominates = false;
140 if (OtherI->getParent() == BB)
141 Dominates = BlockInsts.count(OtherI);
142 else
Chris Lattnercb367102006-01-11 05:10:20 +0000143 Dominates = EF.dominates(OtherI->getParent(), BB);
Chris Lattner69c49002004-04-10 21:11:11 +0000144
145 if (Dominates) {
146 // Okay, we found an instruction with the same value as this one
147 // and that dominates this one. Replace this instruction with the
148 // specified one.
149 ReplaceInstructionWith(Inst, OtherI);
150 Inst = 0;
151 break;
152 }
153 }
154
155 EqualValues.clear();
156
157 if (Inst) {
158 I = Inst; ++I; // Deleted no instructions
159 } else if (I == BB->end()) { // Deleted first instruction
160 I = BB->begin();
161 } else { // Deleted inst in middle of block.
162 ++I;
163 }
164 }
165
166 if (Inst)
167 BlockInsts.insert(Inst);
168 }
Chris Lattnerb2a31092002-08-30 20:22:29 +0000169 }
Chris Lattner1467f642002-04-28 00:47:11 +0000170 }
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000171
Chris Lattner1467f642002-04-28 00:47:11 +0000172 // When the worklist is empty, return whether or not we changed anything...
173 return Changed;
174}
175
Chris Lattnerb2a31092002-08-30 20:22:29 +0000176
Chris Lattner69c49002004-04-10 21:11:11 +0000177void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
178 if (isa<LoadInst>(I))
179 ++NumLoadRemoved; // Keep track of loads eliminated
180 if (isa<CallInst>(I))
181 ++NumCallRemoved; // Keep track of calls eliminated
182 ++NumInstRemoved; // Keep track of number of insts eliminated
Chris Lattnerb2a31092002-08-30 20:22:29 +0000183
Chris Lattnerf16fe722004-04-10 22:33:34 +0000184 // Update value numbering
Chris Lattnere67dbc22004-05-23 21:19:55 +0000185 getAnalysis<ValueNumbering>().deleteValue(I);
Chris Lattnerf16fe722004-04-10 22:33:34 +0000186
Chris Lattner88deefa2004-12-12 18:23:20 +0000187 I->replaceAllUsesWith(V);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000188
Chris Lattner494a6852004-04-12 05:15:13 +0000189 if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
190 // Removing an invoke instruction requires adding a branch to the normal
191 // destination and removing PHI node entries in the exception destination.
192 new BranchInst(II->getNormalDest(), II);
193 II->getUnwindDest()->removePredecessor(II->getParent());
194 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000195
Chris Lattner69c49002004-04-10 21:11:11 +0000196 // Erase the instruction from the program.
197 I->getParent()->getInstList().erase(I);
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000198}