blob: 39a1b257258073a123d797f30df865593bae909d [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 };
Chris Lattnerf6293092002-07-23 18:06:35 +000055
Devang Patel19974732007-05-03 01:11:54 +000056 char GCSE::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000057 RegisterPass<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattnerd80e9732002-04-28 00:47:11 +000058}
59
60// createGCSEPass - The public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000061FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattnerd80e9732002-04-28 00:47:11 +000062
Chris Lattnerd80e9732002-04-28 00:47:11 +000063// GCSE::runOnFunction - This is the main transformation entry point for a
64// function.
65//
Chris Lattner7e708292002-06-25 16:13:24 +000066bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000067 bool Changed = false;
68
Chris Lattnerd456ec92002-08-22 18:24:48 +000069 // Get pointers to the analysis results that we will be using...
Chris Lattner38e66bd2004-04-10 21:11:11 +000070 DominatorTree &DT = getAnalysis<DominatorTree>();
Devang Patel387b8cb2007-06-07 18:40:55 +000071 ValueNumbering &VN = getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000072
Chris Lattner38e66bd2004-04-10 21:11:11 +000073 std::vector<Value*> EqualValues;
Chris Lattnerd80e9732002-04-28 00:47:11 +000074
Chris Lattner057f78a2004-05-23 21:19:55 +000075 // Check for value numbers of arguments. If the value numbering
76 // implementation can prove that an incoming argument is a constant or global
77 // value address, substitute it, making the argument dead.
Chris Lattnere34e9a22007-04-14 23:32:02 +000078 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
Chris Lattner057f78a2004-05-23 21:19:55 +000079 if (!AI->use_empty()) {
80 VN.getEqualNumberNodes(AI, EqualValues);
81 if (!EqualValues.empty()) {
82 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
Reid Spencer460f16c2004-07-18 00:32:14 +000083 if (isa<Constant>(EqualValues[i])) {
Chris Lattner057f78a2004-05-23 21:19:55 +000084 AI->replaceAllUsesWith(EqualValues[i]);
85 ++NumArgsRepl;
86 Changed = true;
87 break;
88 }
89 EqualValues.clear();
90 }
91 }
92
Chris Lattner38e66bd2004-04-10 21:11:11 +000093 // Traverse the CFG of the function in dominator order, so that we see each
94 // instruction after we see its operands.
Devang Patel26042422007-06-04 00:32:22 +000095 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
Chris Lattner38e66bd2004-04-10 21:11:11 +000096 E = df_end(DT.getRootNode()); DI != E; ++DI) {
97 BasicBlock *BB = DI->getBlock();
Chris Lattnerd80e9732002-04-28 00:47:11 +000098
Chris Lattner38e66bd2004-04-10 21:11:11 +000099 // Remember which instructions we've seen in this basic block as we scan.
100 std::set<Instruction*> BlockInsts;
Chris Lattner14987f12002-08-30 20:22:29 +0000101
Chris Lattner38e66bd2004-04-10 21:11:11 +0000102 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
103 Instruction *Inst = I++;
104
Chris Lattnerfb851ab2004-12-12 18:23:20 +0000105 if (Constant *C = ConstantFoldInstruction(Inst)) {
106 ReplaceInstructionWith(Inst, C);
107 } else if (Inst->getType() != Type::VoidTy) {
108 // If this instruction computes a value, try to fold together common
109 // instructions that compute it.
110 //
Chris Lattner38e66bd2004-04-10 21:11:11 +0000111 VN.getEqualNumberNodes(Inst, EqualValues);
112
113 // If this instruction computes a value that is already computed
114 // elsewhere, try to recycle the old value.
115 if (!EqualValues.empty()) {
116 if (Inst == &*BB->begin())
117 I = BB->end();
118 else {
119 I = Inst; --I;
120 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000121
Chris Lattner38e66bd2004-04-10 21:11:11 +0000122 // First check to see if we were able to value number this instruction
123 // to a non-instruction value. If so, prefer that value over other
124 // instructions which may compute the same thing.
125 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
126 if (!isa<Instruction>(EqualValues[i])) {
127 ++NumNonInsts; // Keep track of # of insts repl with values
128
129 // Change all users of Inst to use the replacement and remove it
130 // from the program.
131 ReplaceInstructionWith(Inst, EqualValues[i]);
132 Inst = 0;
133 EqualValues.clear(); // don't enter the next loop
134 break;
135 }
136
137 // If there were no non-instruction values that this instruction
138 // produces, find a dominating instruction that produces the same
139 // value. If we find one, use it's value instead of ours.
140 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
141 Instruction *OtherI = cast<Instruction>(EqualValues[i]);
142 bool Dominates = false;
143 if (OtherI->getParent() == BB)
144 Dominates = BlockInsts.count(OtherI);
145 else
Devang Patela97e0402007-06-07 18:45:06 +0000146 Dominates = DT.dominates(OtherI->getParent(), BB);
Chris Lattner38e66bd2004-04-10 21:11:11 +0000147
148 if (Dominates) {
149 // Okay, we found an instruction with the same value as this one
150 // and that dominates this one. Replace this instruction with the
151 // specified one.
152 ReplaceInstructionWith(Inst, OtherI);
153 Inst = 0;
154 break;
155 }
156 }
157
158 EqualValues.clear();
159
160 if (Inst) {
161 I = Inst; ++I; // Deleted no instructions
162 } else if (I == BB->end()) { // Deleted first instruction
163 I = BB->begin();
164 } else { // Deleted inst in middle of block.
165 ++I;
166 }
167 }
168
169 if (Inst)
170 BlockInsts.insert(Inst);
171 }
Chris Lattner14987f12002-08-30 20:22:29 +0000172 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000173 }
Chris Lattner18fb2a62002-05-14 05:02:40 +0000174
Chris Lattnerd80e9732002-04-28 00:47:11 +0000175 // When the worklist is empty, return whether or not we changed anything...
176 return Changed;
177}
178
Chris Lattner14987f12002-08-30 20:22:29 +0000179
Chris Lattner38e66bd2004-04-10 21:11:11 +0000180void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
181 if (isa<LoadInst>(I))
182 ++NumLoadRemoved; // Keep track of loads eliminated
183 if (isa<CallInst>(I))
184 ++NumCallRemoved; // Keep track of calls eliminated
185 ++NumInstRemoved; // Keep track of number of insts eliminated
Chris Lattner14987f12002-08-30 20:22:29 +0000186
Chris Lattneradb7c0d2004-04-10 22:33:34 +0000187 // Update value numbering
Chris Lattner057f78a2004-05-23 21:19:55 +0000188 getAnalysis<ValueNumbering>().deleteValue(I);
Chris Lattneradb7c0d2004-04-10 22:33:34 +0000189
Chris Lattnerfb851ab2004-12-12 18:23:20 +0000190 I->replaceAllUsesWith(V);
Misha Brukmanfd939082005-04-21 23:48:37 +0000191
Chris Lattner1708d122004-04-12 05:15:13 +0000192 if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
193 // Removing an invoke instruction requires adding a branch to the normal
194 // destination and removing PHI node entries in the exception destination.
Gabor Greif051a9502008-04-06 20:25:17 +0000195 BranchInst::Create(II->getNormalDest(), II);
Chris Lattner1708d122004-04-12 05:15:13 +0000196 II->getUnwindDest()->removePredecessor(II->getParent());
197 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000198
Chris Lattner38e66bd2004-04-10 21:11:11 +0000199 // Erase the instruction from the program.
200 I->getParent()->getInstList().erase(I);
Chris Lattner18fb2a62002-05-14 05:02:40 +0000201}