blob: 6ef95b39c1d49ea02020a47dd95703d50c50982a [file] [log] [blame]
Chris Lattnerf516c692003-10-23 16:52:27 +00001//===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnerf516c692003-10-23 16:52:27 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnerf516c692003-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 Lattner1631bcb2006-12-19 22:09:18 +000018#define DEBUG_TYPE "ipconstprop"
Chris Lattnerf516c692003-10-23 16:52:27 +000019#include "llvm/Transforms/IPO.h"
Chris Lattneraf555ad2004-11-14 06:10:11 +000020#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
Owen Anderson605a8c72009-07-06 01:34:54 +000022#include "llvm/LLVMContext.h"
Chris Lattnerf516c692003-10-23 16:52:27 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
Matthijs Kooijman97034592008-06-18 08:09:27 +000025#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerf516c692003-10-23 16:52:27 +000026#include "llvm/Support/CallSite.h"
Reid Spencer557ab152007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Devang Patel73581652008-03-11 22:24:29 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattner1631bcb2006-12-19 22:09:18 +000032STATISTIC(NumArgumentsProped, "Number of args turned into constants");
33STATISTIC(NumReturnValProped, "Number of return values turned into constants");
Chris Lattnerf516c692003-10-23 16:52:27 +000034
Chris Lattner1631bcb2006-12-19 22:09:18 +000035namespace {
Chris Lattnerf516c692003-10-23 16:52:27 +000036 /// IPCP - The interprocedural constant propagation pass
37 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000038 struct IPCP : public ModulePass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +000040 IPCP() : ModulePass(&ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000041
Chris Lattner4f2cf032004-09-20 04:48:05 +000042 bool runOnModule(Module &M);
Chris Lattnerf516c692003-10-23 16:52:27 +000043 private:
Chris Lattneraf555ad2004-11-14 06:10:11 +000044 bool PropagateConstantsIntoArguments(Function &F);
45 bool PropagateConstantReturn(Function &F);
Chris Lattnerf516c692003-10-23 16:52:27 +000046 };
Chris Lattnerf516c692003-10-23 16:52:27 +000047}
48
Dan Gohmand78c4002008-05-13 00:00:25 +000049char IPCP::ID = 0;
50static RegisterPass<IPCP>
51X("ipconstprop", "Interprocedural constant propagation");
52
Chris Lattner4f2cf032004-09-20 04:48:05 +000053ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerf516c692003-10-23 16:52:27 +000054
Chris Lattner4f2cf032004-09-20 04:48:05 +000055bool IPCP::runOnModule(Module &M) {
Chris Lattnerf516c692003-10-23 16:52:27 +000056 bool Changed = false;
Chris Lattner5e004e82003-10-27 21:09:00 +000057 bool LocalChange = true;
58
59 // FIXME: instead of using smart algorithms, we just iterate until we stop
60 // making changes.
61 while (LocalChange) {
62 LocalChange = false;
63 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000064 if (!I->isDeclaration()) {
Chris Lattneraf555ad2004-11-14 06:10:11 +000065 // Delete any klingons.
66 I->removeDeadConstantUsers();
Rafael Espindola6de96a12009-01-15 20:18:42 +000067 if (I->hasLocalLinkage())
Chris Lattneraf555ad2004-11-14 06:10:11 +000068 LocalChange |= PropagateConstantsIntoArguments(*I);
69 Changed |= PropagateConstantReturn(*I);
70 }
Chris Lattner5e004e82003-10-27 21:09:00 +000071 Changed |= LocalChange;
72 }
Chris Lattnerf516c692003-10-23 16:52:27 +000073 return Changed;
74}
75
Chris Lattneraf555ad2004-11-14 06:10:11 +000076/// PropagateConstantsIntoArguments - Look at all uses of the specified
77/// function. If all uses are direct call sites, and all pass a particular
78/// constant in for an argument, propagate that constant in as the argument.
Chris Lattnerf516c692003-10-23 16:52:27 +000079///
Chris Lattneraf555ad2004-11-14 06:10:11 +000080bool IPCP::PropagateConstantsIntoArguments(Function &F) {
Chris Lattnerc2d3d312006-08-27 22:42:52 +000081 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
Chris Lattnerf516c692003-10-23 16:52:27 +000082
Chris Lattnera82d6912008-04-23 06:16:27 +000083 // For each argument, keep track of its constant value and whether it is a
84 // constant or not. The bool is driven to true when found to be non-constant.
85 SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants;
Chris Lattner531f9e92005-03-15 04:54:21 +000086 ArgumentConstants.resize(F.arg_size());
Chris Lattnerf516c692003-10-23 16:52:27 +000087
88 unsigned NumNonconstant = 0;
Chris Lattnera82d6912008-04-23 06:16:27 +000089 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
90 // Used by a non-instruction, or not the callee of a function, do not
91 // transform.
Gabor Greiff4013372009-01-22 21:35:57 +000092 if (!isa<CallInst>(*UI) && !isa<InvokeInst>(*UI))
Chris Lattnera82d6912008-04-23 06:16:27 +000093 return false;
94
95 CallSite CS = CallSite::get(cast<Instruction>(*UI));
Gabor Greiff4013372009-01-22 21:35:57 +000096 if (!CS.isCallee(UI))
97 return false;
Chris Lattnerf516c692003-10-23 16:52:27 +000098
Chris Lattnera82d6912008-04-23 06:16:27 +000099 // Check out all of the potentially constant arguments. Note that we don't
100 // inspect varargs here.
101 CallSite::arg_iterator AI = CS.arg_begin();
102 Function::arg_iterator Arg = F.arg_begin();
103 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
104 ++i, ++AI, ++Arg) {
105
106 // If this argument is known non-constant, ignore it.
107 if (ArgumentConstants[i].second)
108 continue;
109
110 Constant *C = dyn_cast<Constant>(*AI);
111 if (C && ArgumentConstants[i].first == 0) {
112 ArgumentConstants[i].first = C; // First constant seen.
113 } else if (C && ArgumentConstants[i].first == C) {
114 // Still the constant value we think it is.
115 } else if (*AI == &*Arg) {
116 // Ignore recursive calls passing argument down.
117 } else {
118 // Argument became non-constant. If all arguments are non-constant now,
119 // give up on this function.
120 if (++NumNonconstant == ArgumentConstants.size())
121 return false;
122 ArgumentConstants[i].second = true;
Chris Lattnerf516c692003-10-23 16:52:27 +0000123 }
124 }
Chris Lattnera82d6912008-04-23 06:16:27 +0000125 }
Chris Lattnerf516c692003-10-23 16:52:27 +0000126
127 // If we got to this point, there is a constant argument!
128 assert(NumNonconstant != ArgumentConstants.size());
Chris Lattner5e004e82003-10-27 21:09:00 +0000129 bool MadeChange = false;
Chris Lattnera82d6912008-04-23 06:16:27 +0000130 Function::arg_iterator AI = F.arg_begin();
131 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
132 // Do we have a constant argument?
Torok Edwin21bd8c92009-09-24 18:33:42 +0000133 if (ArgumentConstants[i].second || AI->use_empty() ||
134 (AI->hasByValAttr() && !F.onlyReadsMemory()))
Chris Lattnera82d6912008-04-23 06:16:27 +0000135 continue;
136
137 Value *V = ArgumentConstants[i].first;
Owen Andersonb292b8c2009-07-30 23:03:37 +0000138 if (V == 0) V = UndefValue::get(AI->getType());
Chris Lattnera82d6912008-04-23 06:16:27 +0000139 AI->replaceAllUsesWith(V);
140 ++NumArgumentsProped;
141 MadeChange = true;
142 }
Chris Lattner5e004e82003-10-27 21:09:00 +0000143 return MadeChange;
Chris Lattnerf516c692003-10-23 16:52:27 +0000144}
Brian Gaeke960707c2003-11-11 22:41:34 +0000145
Chris Lattneraf555ad2004-11-14 06:10:11 +0000146
Matthijs Kooijman97034592008-06-18 08:09:27 +0000147// Check to see if this function returns one or more constants. If so, replace
148// all callers that use those return values with the constant value. This will
149// leave in the actual return values and instructions, but deadargelim will
150// clean that up.
Matthijs Kooijmanfd173572008-06-18 08:30:37 +0000151//
152// Additionally if a function always returns one of its arguments directly,
153// callers will be updated to use the value they pass in directly instead of
154// using the return value.
Chris Lattneraf555ad2004-11-14 06:10:11 +0000155bool IPCP::PropagateConstantReturn(Function &F) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000156 if (F.getReturnType() == Type::getVoidTy(F.getContext()))
Chris Lattneraf555ad2004-11-14 06:10:11 +0000157 return false; // No return value.
158
Chris Lattnerdbd595f2008-06-09 07:58:07 +0000159 // If this function could be overridden later in the link stage, we can't
160 // propagate information about its results into callers.
Nuno Lopesffc9da62008-09-29 14:40:32 +0000161 if (F.mayBeOverridden())
Chris Lattnerdbd595f2008-06-09 07:58:07 +0000162 return false;
Owen Anderson47db9412009-07-22 00:24:57 +0000163
164 LLVMContext &Context = F.getContext();
Chris Lattnerdbd595f2008-06-09 07:58:07 +0000165
Chris Lattneraf555ad2004-11-14 06:10:11 +0000166 // Check to see if this function returns a constant.
Devang Patel73581652008-03-11 22:24:29 +0000167 SmallVector<Value *,4> RetVals;
Devang Patel73581652008-03-11 22:24:29 +0000168 const StructType *STy = dyn_cast<StructType>(F.getReturnType());
169 if (STy)
Matthijs Kooijman97034592008-06-18 08:09:27 +0000170 for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000171 RetVals.push_back(UndefValue::get(STy->getElementType(i)));
Devang Patel73581652008-03-11 22:24:29 +0000172 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000173 RetVals.push_back(UndefValue::get(F.getReturnType()));
Devang Patel73581652008-03-11 22:24:29 +0000174
Matthijs Kooijman97034592008-06-18 08:09:27 +0000175 unsigned NumNonConstant = 0;
Chris Lattneraf555ad2004-11-14 06:10:11 +0000176 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +0000177 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Chris Lattner5f1802c2008-04-23 05:59:23 +0000178 for (unsigned i = 0, e = RetVals.size(); i != e; ++i) {
Matthijs Kooijman97034592008-06-18 08:09:27 +0000179 // Already found conflicting return values?
Chris Lattner5f1802c2008-04-23 05:59:23 +0000180 Value *RV = RetVals[i];
Matthijs Kooijman97034592008-06-18 08:09:27 +0000181 if (!RV)
182 continue;
183
184 // Find the returned value
185 Value *V;
Dan Gohmane2190392008-10-03 22:21:24 +0000186 if (!STy)
Matthijs Kooijman97034592008-06-18 08:09:27 +0000187 V = RI->getOperand(i);
188 else
Owen Andersonf1f17432009-07-06 22:37:39 +0000189 V = FindInsertedValue(RI->getOperand(0), i, Context);
Matthijs Kooijman97034592008-06-18 08:09:27 +0000190
191 if (V) {
192 // Ignore undefs, we can change them into anything
193 if (isa<UndefValue>(V))
194 continue;
195
Matthijs Kooijmanfd173572008-06-18 08:30:37 +0000196 // Try to see if all the rets return the same constant or argument.
197 if (isa<Constant>(V) || isa<Argument>(V)) {
Matthijs Kooijman97034592008-06-18 08:09:27 +0000198 if (isa<UndefValue>(RV)) {
199 // No value found yet? Try the current one.
200 RetVals[i] = V;
201 continue;
202 }
203 // Returning the same value? Good.
204 if (RV == V)
205 continue;
206 }
207 }
208 // Different or no known return value? Don't propagate this return
209 // value.
210 RetVals[i] = 0;
211 // All values non constant? Stop looking.
212 if (++NumNonConstant == RetVals.size())
213 return false;
Chris Lattneraf555ad2004-11-14 06:10:11 +0000214 }
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +0000215 }
Chris Lattneraf555ad2004-11-14 06:10:11 +0000216
Matthijs Kooijman97034592008-06-18 08:09:27 +0000217 // If we got here, the function returns at least one constant value. Loop
218 // over all users, replacing any uses of the return value with the returned
219 // constant.
Chris Lattneraf555ad2004-11-14 06:10:11 +0000220 bool MadeChange = false;
Chris Lattner5f1802c2008-04-23 05:59:23 +0000221 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
Matthijs Kooijman0c717322008-06-19 08:53:24 +0000222 CallSite CS = CallSite::get(*UI);
223 Instruction* Call = CS.getInstruction();
224
225 // Not a call instruction or a call instruction that's not calling F
226 // directly?
Gabor Greiff4013372009-01-22 21:35:57 +0000227 if (!Call || !CS.isCallee(UI))
Chris Lattner5f1802c2008-04-23 05:59:23 +0000228 continue;
Chris Lattner5f1802c2008-04-23 05:59:23 +0000229
Matthijs Kooijman0c717322008-06-19 08:53:24 +0000230 // Call result not used?
Chris Lattner5f1802c2008-04-23 05:59:23 +0000231 if (Call->use_empty())
232 continue;
Chris Lattneraf555ad2004-11-14 06:10:11 +0000233
Chris Lattner5f1802c2008-04-23 05:59:23 +0000234 MadeChange = true;
235
236 if (STy == 0) {
Matthijs Kooijmanfd173572008-06-18 08:30:37 +0000237 Value* New = RetVals[0];
238 if (Argument *A = dyn_cast<Argument>(New))
239 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijman0c717322008-06-19 08:53:24 +0000240 // the call instruction and use that.
241 New = CS.getArgument(A->getArgNo());
Matthijs Kooijmanfd173572008-06-18 08:30:37 +0000242 Call->replaceAllUsesWith(New);
Chris Lattner5f1802c2008-04-23 05:59:23 +0000243 continue;
244 }
245
Matthijs Kooijman97034592008-06-18 08:09:27 +0000246 for (Value::use_iterator I = Call->use_begin(), E = Call->use_end();
247 I != E;) {
Jay Foade57ba2e2009-06-06 17:49:35 +0000248 Instruction *Ins = cast<Instruction>(*I);
Matthijs Kooijman97034592008-06-18 08:09:27 +0000249
250 // Increment now, so we can remove the use
251 ++I;
252
Matthijs Kooijman97034592008-06-18 08:09:27 +0000253 // Find the index of the retval to replace with
254 int index = -1;
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000255 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins))
Matthijs Kooijman97034592008-06-18 08:09:27 +0000256 if (EV->hasIndices())
257 index = *EV->idx_begin();
258
259 // If this use uses a specific return value, and we have a replacement,
260 // replace it.
261 if (index != -1) {
262 Value *New = RetVals[index];
263 if (New) {
Matthijs Kooijmanfd173572008-06-18 08:30:37 +0000264 if (Argument *A = dyn_cast<Argument>(New))
265 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijman0c717322008-06-19 08:53:24 +0000266 // the call instruction and use that.
267 New = CS.getArgument(A->getArgNo());
Matthijs Kooijman97034592008-06-18 08:09:27 +0000268 Ins->replaceAllUsesWith(New);
269 Ins->eraseFromParent();
Chris Lattner263b0a12004-12-11 17:00:14 +0000270 }
Devang Patel5ca2ea62008-03-20 18:30:32 +0000271 }
Devang Patel73581652008-03-11 22:24:29 +0000272 }
Chris Lattneraf555ad2004-11-14 06:10:11 +0000273 }
274
275 if (MadeChange) ++NumReturnValProped;
Chris Lattneraf555ad2004-11-14 06:10:11 +0000276 return MadeChange;
277}