blob: 6779ef2f0fa0b2547ae3e590b7db1904bb5f1206 [file] [log] [blame]
Chris Lattnerd358e6f2003-10-23 16:52:27 +00001//===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnerd358e6f2003-10-23 16:52:27 +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//
Chris Lattnerd358e6f2003-10-23 16:52:27 +00008//===----------------------------------------------------------------------===//
9//
10// This pass implements an _extremely_ simple interprocedural constant
11// propagation pass. It could certainly be improved in many different ways,
12// like using a worklist. This pass makes arguments dead, but does not remove
13// them. The existing dead argument elimination pass should be run after this
14// to clean up the mess.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner86453c52006-12-19 22:09:18 +000018#define DEBUG_TYPE "ipconstprop"
Chris Lattnerd358e6f2003-10-23 16:52:27 +000019#include "llvm/Transforms/IPO.h"
Chris Lattnera990d942004-11-14 06:10:11 +000020#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
Chris Lattnerd358e6f2003-10-23 16:52:27 +000022#include "llvm/Module.h"
23#include "llvm/Pass.h"
Chris Lattnerd358e6f2003-10-23 16:52:27 +000024#include "llvm/Support/CallSite.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000025#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Devang Patel7db30ba2008-03-11 22:24:29 +000027#include "llvm/ADT/SmallVector.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner86453c52006-12-19 22:09:18 +000030STATISTIC(NumArgumentsProped, "Number of args turned into constants");
31STATISTIC(NumReturnValProped, "Number of return values turned into constants");
Chris Lattnerd358e6f2003-10-23 16:52:27 +000032
Chris Lattner86453c52006-12-19 22:09:18 +000033namespace {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000034 /// IPCP - The interprocedural constant propagation pass
35 ///
Reid Spencer9133fe22007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN IPCP : public ModulePass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000038 IPCP() : ModulePass((intptr_t)&ID) {}
39
Chris Lattnerb12914b2004-09-20 04:48:05 +000040 bool runOnModule(Module &M);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000041 private:
Chris Lattnera990d942004-11-14 06:10:11 +000042 bool PropagateConstantsIntoArguments(Function &F);
43 bool PropagateConstantReturn(Function &F);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000044 };
Devang Patel19974732007-05-03 01:11:54 +000045 char IPCP::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000046 RegisterPass<IPCP> X("ipconstprop", "Interprocedural constant propagation");
Chris Lattnerd358e6f2003-10-23 16:52:27 +000047}
48
Chris Lattnerb12914b2004-09-20 04:48:05 +000049ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000050
Chris Lattnerb12914b2004-09-20 04:48:05 +000051bool IPCP::runOnModule(Module &M) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000052 bool Changed = false;
Chris Lattner2e8dfb82003-10-27 21:09:00 +000053 bool LocalChange = true;
54
55 // FIXME: instead of using smart algorithms, we just iterate until we stop
56 // making changes.
57 while (LocalChange) {
58 LocalChange = false;
59 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000060 if (!I->isDeclaration()) {
Chris Lattnera990d942004-11-14 06:10:11 +000061 // Delete any klingons.
62 I->removeDeadConstantUsers();
63 if (I->hasInternalLinkage())
64 LocalChange |= PropagateConstantsIntoArguments(*I);
65 Changed |= PropagateConstantReturn(*I);
66 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +000067 Changed |= LocalChange;
68 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000069 return Changed;
70}
71
Chris Lattnera990d942004-11-14 06:10:11 +000072/// PropagateConstantsIntoArguments - Look at all uses of the specified
73/// function. If all uses are direct call sites, and all pass a particular
74/// constant in for an argument, propagate that constant in as the argument.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000075///
Chris Lattnera990d942004-11-14 06:10:11 +000076bool IPCP::PropagateConstantsIntoArguments(Function &F) {
Chris Lattner7f8897f2006-08-27 22:42:52 +000077 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000078
79 std::vector<std::pair<Constant*, bool> > ArgumentConstants;
Chris Lattnere4d5c442005-03-15 04:54:21 +000080 ArgumentConstants.resize(F.arg_size());
Chris Lattnerd358e6f2003-10-23 16:52:27 +000081
82 unsigned NumNonconstant = 0;
83
84 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
85 if (!isa<Instruction>(*I))
86 return false; // Used by a non-instruction, do not transform
87 else {
88 CallSite CS = CallSite::get(cast<Instruction>(*I));
Misha Brukmanfd939082005-04-21 23:48:37 +000089 if (CS.getInstruction() == 0 ||
Chris Lattnerd358e6f2003-10-23 16:52:27 +000090 CS.getCalledFunction() != &F)
91 return false; // Not a direct call site?
Misha Brukmanfd939082005-04-21 23:48:37 +000092
Chris Lattnerd358e6f2003-10-23 16:52:27 +000093 // Check out all of the potentially constant arguments
94 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattnere4d5c442005-03-15 04:54:21 +000095 Function::arg_iterator Arg = F.arg_begin();
Chris Lattnerff1529b2004-11-10 19:43:59 +000096 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
97 ++i, ++AI, ++Arg) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000098 if (*AI == &F) return false; // Passes the function into itself
99
100 if (!ArgumentConstants[i].second) {
Reid Spencer3188cab2004-07-18 08:31:18 +0000101 if (Constant *C = dyn_cast<Constant>(*AI)) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000102 if (!ArgumentConstants[i].first)
103 ArgumentConstants[i].first = C;
104 else if (ArgumentConstants[i].first != C) {
105 // Became non-constant
106 ArgumentConstants[i].second = true;
107 ++NumNonconstant;
108 if (NumNonconstant == ArgumentConstants.size()) return false;
109 }
Chris Lattnerff1529b2004-11-10 19:43:59 +0000110 } else if (*AI != &*Arg) { // Ignore recursive calls with same arg
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000111 // This is not a constant argument. Mark the argument as
112 // non-constant.
113 ArgumentConstants[i].second = true;
114 ++NumNonconstant;
115 if (NumNonconstant == ArgumentConstants.size()) return false;
116 }
117 }
118 }
119 }
120
121 // If we got to this point, there is a constant argument!
122 assert(NumNonconstant != ArgumentConstants.size());
Chris Lattnere4d5c442005-03-15 04:54:21 +0000123 Function::arg_iterator AI = F.arg_begin();
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000124 bool MadeChange = false;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000125 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
126 // Do we have a constant argument!?
Chris Lattnerd79c7b42004-11-11 07:47:54 +0000127 if (!ArgumentConstants[i].second && !AI->use_empty()) {
Chris Lattner2e56dd82003-10-23 18:49:23 +0000128 Value *V = ArgumentConstants[i].first;
Chris Lattner3e062ea2004-11-11 07:46:29 +0000129 if (V == 0) V = UndefValue::get(AI->getType());
Chris Lattner2e56dd82003-10-23 18:49:23 +0000130 AI->replaceAllUsesWith(V);
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000131 ++NumArgumentsProped;
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000132 MadeChange = true;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000133 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000134 return MadeChange;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000135}
Brian Gaeked0fde302003-11-11 22:41:34 +0000136
Chris Lattnera990d942004-11-14 06:10:11 +0000137
138// Check to see if this function returns a constant. If so, replace all callers
139// that user the return value with the returned valued. If we can replace ALL
140// callers,
141bool IPCP::PropagateConstantReturn(Function &F) {
142 if (F.getReturnType() == Type::VoidTy)
143 return false; // No return value.
144
145 // Check to see if this function returns a constant.
Devang Patel7db30ba2008-03-11 22:24:29 +0000146 SmallVector<Value *,4> RetVals;
147 unsigned N = 0;
148 const StructType *STy = dyn_cast<StructType>(F.getReturnType());
149 if (STy)
150 N = STy->getNumElements();
151 else
152 N = 1;
153 for (unsigned i = 0; i < N; ++i)
154 RetVals.push_back(0);
155
Chris Lattnera990d942004-11-14 06:10:11 +0000156 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000157 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Devang Patel7db30ba2008-03-11 22:24:29 +0000158 assert (N == RI->getNumOperands() && "Invalid ReturnInst operands!");
159 for (unsigned i = 0; i < N; ++i) {
160 if (isa<UndefValue>(RI->getOperand(i))) {
161 // Ignore
162 } else if (Constant *C = dyn_cast<Constant>(RI->getOperand(i))) {
163 Value *RV = RetVals[i];
164 if (RV == 0)
165 RetVals[i] = C;
166 else if (RV != C)
167 return false; // Does not return the same constant.
168 } else {
169 return false; // Does not return a constant.
170 }
Chris Lattnera990d942004-11-14 06:10:11 +0000171 }
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000172 }
Chris Lattnera990d942004-11-14 06:10:11 +0000173
Devang Patel7db30ba2008-03-11 22:24:29 +0000174 if (N == 1) {
175 if (RetVals[0] == 0)
176 RetVals[0] = UndefValue::get(F.getReturnType());
177 } else {
178 for (unsigned i = 0; i < N; ++i) {
179 Value *RetVal = RetVals[i];
180 if (RetVal == 0)
181 RetVals[i] = UndefValue::get(STy->getElementType(i));
182 }
183 }
Chris Lattnera990d942004-11-14 06:10:11 +0000184
185 // If we got here, the function returns a constant value. Loop over all
186 // users, replacing any uses of the return value with the returned constant.
187 bool ReplacedAllUsers = true;
188 bool MadeChange = false;
189 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
190 if (!isa<Instruction>(*I))
191 ReplacedAllUsers = false;
192 else {
193 CallSite CS = CallSite::get(cast<Instruction>(*I));
Misha Brukmanfd939082005-04-21 23:48:37 +0000194 if (CS.getInstruction() == 0 ||
Chris Lattnera990d942004-11-14 06:10:11 +0000195 CS.getCalledFunction() != &F) {
196 ReplacedAllUsers = false;
197 } else {
Devang Patel7db30ba2008-03-11 22:24:29 +0000198 Instruction *Call = CS.getInstruction();
199 if (!Call->use_empty()) {
200 if (N == 1)
201 Call->replaceAllUsesWith(RetVals[0]);
202 else {
203 for(Value::use_iterator CUI = Call->use_begin(), CUE = Call->use_end();
204 CUI != CUE; ++CUI) {
205 GetResultInst *GR = cast<GetResultInst>(CUI);
206 GR->replaceAllUsesWith(RetVals[GR->getIndex()]);
207 GR->eraseFromParent();
208 }
209 }
Chris Lattnera990d942004-11-14 06:10:11 +0000210 MadeChange = true;
211 }
212 }
213 }
214
215 // If we replace all users with the returned constant, and there can be no
216 // other callers of the function, replace the constant being returned in the
217 // function with an undef value.
Devang Patel7db30ba2008-03-11 22:24:29 +0000218 if (ReplacedAllUsers && F.hasInternalLinkage()) {
219 for (unsigned i = 0; i < N; ++i) {
220 Value *RetVal = RetVals[i];
221 if (isa<UndefValue>(RetVal))
222 continue;
223 Value *RV = UndefValue::get(RetVal->getType());
224 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
225 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
226 if (RI->getOperand(i) != RV) {
227 RI->setOperand(i, RV);
228 MadeChange = true;
229 }
Chris Lattner2ffa47b2004-12-11 17:00:14 +0000230 }
Devang Patel7db30ba2008-03-11 22:24:29 +0000231 }
Chris Lattnera990d942004-11-14 06:10:11 +0000232 }
233
234 if (MadeChange) ++NumReturnValProped;
Chris Lattnera990d942004-11-14 06:10:11 +0000235 return MadeChange;
236}