blob: 35752bb67967d6e6b3f01076826abb3f943fee58 [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
Chris Lattner2964f362002-08-30 22:53:30 +000012// using an existing value numbering implementation to identify the common
13// subexpressions, eliminating them when possible.
Chris Lattnerd80e9732002-04-28 00:47:11 +000014//
Chris Lattnerd80e9732002-04-28 00:47:11 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner0e5f4992006-12-19 21:40:18 +000017#define DEBUG_TYPE "gcse"
Chris Lattner022103b2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner38e66bd2004-04-10 21:11:11 +000019#include "llvm/Instructions.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000020#include "llvm/Function.h"
Chris Lattner2964f362002-08-30 22:53:30 +000021#include "llvm/Type.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000022#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000023#include "llvm/Analysis/Dominators.h"
Chris Lattner14987f12002-08-30 20:22:29 +000024#include "llvm/Analysis/ValueNumbering.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/DepthFirstIterator.h"
26#include "llvm/ADT/Statistic.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000028#include <algorithm>
Chris Lattnerd7456022004-01-09 06:02:20 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner0e5f4992006-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 Lattnerd80e9732002-04-28 00:47:11 +000038namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000039 struct VISIBILITY_HIDDEN GCSE : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000041 GCSE() : FunctionPass((intptr_t)&ID) {}
42
Chris Lattner7e708292002-06-25 16:13:24 +000043 virtual bool runOnFunction(Function &F);
Chris Lattnerd80e9732002-04-28 00:47:11 +000044
Chris Lattnerd80e9732002-04-28 00:47:11 +000045 private:
Chris Lattner38e66bd2004-04-10 21:11:11 +000046 void ReplaceInstructionWith(Instruction *I, Value *V);
Chris Lattner18fb2a62002-05-14 05:02:40 +000047
Chris Lattnerd80e9732002-04-28 00:47:11 +000048 // This transformation requires dominator and immediate dominator info
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000050 AU.setPreservesCFG();
Chris Lattner38e66bd2004-04-10 21:11:11 +000051 AU.addRequired<DominatorTree>();
Chris Lattner14987f12002-08-30 20:22:29 +000052 AU.addRequired<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000053 }
54 };
55}
56
Dan Gohman844731a2008-05-13 00:00:25 +000057char GCSE::ID = 0;
58static RegisterPass<GCSE>
59X("gcse", "Global Common Subexpression Elimination");
60
Chris Lattnerd80e9732002-04-28 00:47:11 +000061// createGCSEPass - The public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000062FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattnerd80e9732002-04-28 00:47:11 +000063
Chris Lattnerd80e9732002-04-28 00:47:11 +000064// GCSE::runOnFunction - This is the main transformation entry point for a
65// function.
66//
Chris Lattner7e708292002-06-25 16:13:24 +000067bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000068 bool Changed = false;
69
Chris Lattnerd456ec92002-08-22 18:24:48 +000070 // Get pointers to the analysis results that we will be using...
Chris Lattner38e66bd2004-04-10 21:11:11 +000071 DominatorTree &DT = getAnalysis<DominatorTree>();
Devang Patel387b8cb2007-06-07 18:40:55 +000072 ValueNumbering &VN = getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000073
Chris Lattner38e66bd2004-04-10 21:11:11 +000074 std::vector<Value*> EqualValues;
Chris Lattnerd80e9732002-04-28 00:47:11 +000075
Chris Lattner057f78a2004-05-23 21:19:55 +000076 // Check for value numbers of arguments. If the value numbering
77 // implementation can prove that an incoming argument is a constant or global
78 // value address, substitute it, making the argument dead.
Chris Lattnere34e9a22007-04-14 23:32:02 +000079 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattner057f78a2004-05-23 21:19:55 +000080 if (!AI->use_empty()) {
81 VN.getEqualNumberNodes(AI, EqualValues);
82 if (!EqualValues.empty()) {
83 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
Reid Spencer460f16c2004-07-18 00:32:14 +000084 if (isa<Constant>(EqualValues[i])) {
Chris Lattner057f78a2004-05-23 21:19:55 +000085 AI->replaceAllUsesWith(EqualValues[i]);
86 ++NumArgsRepl;
87 Changed = true;
88 break;
89 }
90 EqualValues.clear();
91 }
92 }
93
Chris Lattner38e66bd2004-04-10 21:11:11 +000094 // Traverse the CFG of the function in dominator order, so that we see each
95 // instruction after we see its operands.
Devang Patel26042422007-06-04 00:32:22 +000096 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
Chris Lattner38e66bd2004-04-10 21:11:11 +000097 E = df_end(DT.getRootNode()); DI != E; ++DI) {
98 BasicBlock *BB = DI->getBlock();
Chris Lattnerd80e9732002-04-28 00:47:11 +000099
Chris Lattner38e66bd2004-04-10 21:11:11 +0000100 // Remember which instructions we've seen in this basic block as we scan.
101 std::set<Instruction*> BlockInsts;
Chris Lattner14987f12002-08-30 20:22:29 +0000102
Chris Lattner38e66bd2004-04-10 21:11:11 +0000103 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
104 Instruction *Inst = I++;
105
Chris Lattnerfb851ab2004-12-12 18:23:20 +0000106 if (Constant *C = ConstantFoldInstruction(Inst)) {
107 ReplaceInstructionWith(Inst, C);
108 } else if (Inst->getType() != Type::VoidTy) {
109 // If this instruction computes a value, try to fold together common
110 // instructions that compute it.
111 //
Chris Lattner38e66bd2004-04-10 21:11:11 +0000112 VN.getEqualNumberNodes(Inst, EqualValues);
113
114 // If this instruction computes a value that is already computed
115 // elsewhere, try to recycle the old value.
116 if (!EqualValues.empty()) {
117 if (Inst == &*BB->begin())
118 I = BB->end();
119 else {
120 I = Inst; --I;
121 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000122
Chris Lattner38e66bd2004-04-10 21:11:11 +0000123 // First check to see if we were able to value number this instruction
124 // to a non-instruction value. If so, prefer that value over other
125 // instructions which may compute the same thing.
126 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
127 if (!isa<Instruction>(EqualValues[i])) {
128 ++NumNonInsts; // Keep track of # of insts repl with values
129
130 // Change all users of Inst to use the replacement and remove it
131 // from the program.
132 ReplaceInstructionWith(Inst, EqualValues[i]);
133 Inst = 0;
134 EqualValues.clear(); // don't enter the next loop
135 break;
136 }
137
138 // If there were no non-instruction values that this instruction
139 // produces, find a dominating instruction that produces the same
140 // value. If we find one, use it's value instead of ours.
141 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
142 Instruction *OtherI = cast<Instruction>(EqualValues[i]);
143 bool Dominates = false;
144 if (OtherI->getParent() == BB)
145 Dominates = BlockInsts.count(OtherI);
146 else
Devang Patela97e0402007-06-07 18:45:06 +0000147 Dominates = DT.dominates(OtherI->getParent(), BB);
Chris Lattner38e66bd2004-04-10 21:11:11 +0000148
149 if (Dominates) {
150 // Okay, we found an instruction with the same value as this one
151 // and that dominates this one. Replace this instruction with the
152 // specified one.
153 ReplaceInstructionWith(Inst, OtherI);
154 Inst = 0;
155 break;
156 }
157 }
158
159 EqualValues.clear();
160
161 if (Inst) {
162 I = Inst; ++I; // Deleted no instructions
163 } else if (I == BB->end()) { // Deleted first instruction
164 I = BB->begin();
165 } else { // Deleted inst in middle of block.
166 ++I;
167 }
168 }
169
170 if (Inst)
171 BlockInsts.insert(Inst);
172 }
Chris Lattner14987f12002-08-30 20:22:29 +0000173 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000174 }
Chris Lattner18fb2a62002-05-14 05:02:40 +0000175
Chris Lattnerd80e9732002-04-28 00:47:11 +0000176 // When the worklist is empty, return whether or not we changed anything...
177 return Changed;
178}
179
Chris Lattner14987f12002-08-30 20:22:29 +0000180
Chris Lattner38e66bd2004-04-10 21:11:11 +0000181void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
182 if (isa<LoadInst>(I))
183 ++NumLoadRemoved; // Keep track of loads eliminated
184 if (isa<CallInst>(I))
185 ++NumCallRemoved; // Keep track of calls eliminated
186 ++NumInstRemoved; // Keep track of number of insts eliminated
Chris Lattner14987f12002-08-30 20:22:29 +0000187
Chris Lattneradb7c0d2004-04-10 22:33:34 +0000188 // Update value numbering
Chris Lattner057f78a2004-05-23 21:19:55 +0000189 getAnalysis<ValueNumbering>().deleteValue(I);
Chris Lattneradb7c0d2004-04-10 22:33:34 +0000190
Chris Lattnerfb851ab2004-12-12 18:23:20 +0000191 I->replaceAllUsesWith(V);
Misha Brukmanfd939082005-04-21 23:48:37 +0000192
Chris Lattner1708d122004-04-12 05:15:13 +0000193 if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
194 // Removing an invoke instruction requires adding a branch to the normal
195 // destination and removing PHI node entries in the exception destination.
Gabor Greif051a9502008-04-06 20:25:17 +0000196 BranchInst::Create(II->getNormalDest(), II);
Chris Lattner1708d122004-04-12 05:15:13 +0000197 II->getUnwindDest()->removePredecessor(II->getParent());
198 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000199
Chris Lattner38e66bd2004-04-10 21:11:11 +0000200 // Erase the instruction from the program.
201 I->getParent()->getInstList().erase(I);
Chris Lattner18fb2a62002-05-14 05:02:40 +0000202}