blob: df2456f9f2b7e59c981f3913f60883c9e4470e48 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
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
18#define DEBUG_TYPE "ipconstprop"
19#include "llvm/Transforms/IPO.h"
20#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
22#include "llvm/Module.h"
23#include "llvm/Pass.h"
Matthijs Kooijman054407f2008-06-18 08:09:27 +000024#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/CallSite.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/ADT/Statistic.h"
Devang Patel13f24372008-03-11 22:24:29 +000027#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29
30STATISTIC(NumArgumentsProped, "Number of args turned into constants");
31STATISTIC(NumReturnValProped, "Number of return values turned into constants");
32
33namespace {
34 /// IPCP - The interprocedural constant propagation pass
35 ///
Nick Lewycky492d06e2009-10-25 06:33:48 +000036 struct IPCP : public ModulePass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000038 IPCP() : ModulePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40 bool runOnModule(Module &M);
41 private:
42 bool PropagateConstantsIntoArguments(Function &F);
43 bool PropagateConstantReturn(Function &F);
44 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045}
46
Dan Gohman089efff2008-05-13 00:00:25 +000047char IPCP::ID = 0;
48static RegisterPass<IPCP>
49X("ipconstprop", "Interprocedural constant propagation");
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
52
53bool IPCP::runOnModule(Module &M) {
54 bool Changed = false;
55 bool LocalChange = true;
56
57 // FIXME: instead of using smart algorithms, we just iterate until we stop
58 // making changes.
59 while (LocalChange) {
60 LocalChange = false;
61 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
62 if (!I->isDeclaration()) {
63 // Delete any klingons.
64 I->removeDeadConstantUsers();
Rafael Espindolaa168fc92009-01-15 20:18:42 +000065 if (I->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 LocalChange |= PropagateConstantsIntoArguments(*I);
67 Changed |= PropagateConstantReturn(*I);
68 }
69 Changed |= LocalChange;
70 }
71 return Changed;
72}
73
74/// PropagateConstantsIntoArguments - Look at all uses of the specified
75/// function. If all uses are direct call sites, and all pass a particular
76/// constant in for an argument, propagate that constant in as the argument.
77///
78bool IPCP::PropagateConstantsIntoArguments(Function &F) {
79 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
80
Chris Lattner92edf502008-04-23 06:16:27 +000081 // For each argument, keep track of its constant value and whether it is a
82 // constant or not. The bool is driven to true when found to be non-constant.
83 SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 ArgumentConstants.resize(F.arg_size());
85
86 unsigned NumNonconstant = 0;
Chris Lattner92edf502008-04-23 06:16:27 +000087 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
Chris Lattner2f487502009-11-01 06:11:53 +000088 // Ignore blockaddress uses.
89 if (isa<BlockAddress>(*UI)) continue;
90
Chris Lattner92edf502008-04-23 06:16:27 +000091 // Used by a non-instruction, or not the callee of a function, do not
92 // transform.
Gabor Greif6be9a1b2009-01-22 21:35:57 +000093 if (!isa<CallInst>(*UI) && !isa<InvokeInst>(*UI))
Chris Lattner92edf502008-04-23 06:16:27 +000094 return false;
95
96 CallSite CS = CallSite::get(cast<Instruction>(*UI));
Gabor Greif6be9a1b2009-01-22 21:35:57 +000097 if (!CS.isCallee(UI))
98 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
Chris Lattner92edf502008-04-23 06:16:27 +0000100 // Check out all of the potentially constant arguments. Note that we don't
101 // inspect varargs here.
102 CallSite::arg_iterator AI = CS.arg_begin();
103 Function::arg_iterator Arg = F.arg_begin();
104 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
105 ++i, ++AI, ++Arg) {
106
107 // If this argument is known non-constant, ignore it.
108 if (ArgumentConstants[i].second)
109 continue;
110
111 Constant *C = dyn_cast<Constant>(*AI);
112 if (C && ArgumentConstants[i].first == 0) {
113 ArgumentConstants[i].first = C; // First constant seen.
114 } else if (C && ArgumentConstants[i].first == C) {
115 // Still the constant value we think it is.
116 } else if (*AI == &*Arg) {
117 // Ignore recursive calls passing argument down.
118 } else {
119 // Argument became non-constant. If all arguments are non-constant now,
120 // give up on this function.
121 if (++NumNonconstant == ArgumentConstants.size())
122 return false;
123 ArgumentConstants[i].second = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 }
125 }
Chris Lattner92edf502008-04-23 06:16:27 +0000126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127
128 // If we got to this point, there is a constant argument!
129 assert(NumNonconstant != ArgumentConstants.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 bool MadeChange = false;
Chris Lattner92edf502008-04-23 06:16:27 +0000131 Function::arg_iterator AI = F.arg_begin();
132 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
133 // Do we have a constant argument?
Edwin Török129b2d12009-09-24 18:33:42 +0000134 if (ArgumentConstants[i].second || AI->use_empty() ||
135 (AI->hasByValAttr() && !F.onlyReadsMemory()))
Chris Lattner92edf502008-04-23 06:16:27 +0000136 continue;
137
138 Value *V = ArgumentConstants[i].first;
Owen Andersonb99ecca2009-07-30 23:03:37 +0000139 if (V == 0) V = UndefValue::get(AI->getType());
Chris Lattner92edf502008-04-23 06:16:27 +0000140 AI->replaceAllUsesWith(V);
141 ++NumArgumentsProped;
142 MadeChange = true;
143 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return MadeChange;
145}
146
147
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000148// Check to see if this function returns one or more constants. If so, replace
149// all callers that use those return values with the constant value. This will
150// leave in the actual return values and instructions, but deadargelim will
151// clean that up.
Matthijs Kooijman675d7b02008-06-18 08:30:37 +0000152//
153// Additionally if a function always returns one of its arguments directly,
154// callers will be updated to use the value they pass in directly instead of
155// using the return value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156bool IPCP::PropagateConstantReturn(Function &F) {
Nick Lewycky6e8bc9c2009-11-23 03:29:18 +0000157 if (F.getReturnType()->isVoidTy())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 return false; // No return value.
159
Chris Lattnera77a73e2008-06-09 07:58:07 +0000160 // If this function could be overridden later in the link stage, we can't
161 // propagate information about its results into callers.
Nuno Lopesd7469cb2008-09-29 14:40:32 +0000162 if (F.mayBeOverridden())
Chris Lattnera77a73e2008-06-09 07:58:07 +0000163 return false;
Owen Anderson175b6542009-07-22 00:24:57 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 // Check to see if this function returns a constant.
Devang Patel13f24372008-03-11 22:24:29 +0000166 SmallVector<Value *,4> RetVals;
Devang Patel13f24372008-03-11 22:24:29 +0000167 const StructType *STy = dyn_cast<StructType>(F.getReturnType());
168 if (STy)
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000169 for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i)
Owen Andersonb99ecca2009-07-30 23:03:37 +0000170 RetVals.push_back(UndefValue::get(STy->getElementType(i)));
Devang Patel13f24372008-03-11 22:24:29 +0000171 else
Owen Andersonb99ecca2009-07-30 23:03:37 +0000172 RetVals.push_back(UndefValue::get(F.getReturnType()));
Devang Patel13f24372008-03-11 22:24:29 +0000173
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000174 unsigned NumNonConstant = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Anton Korobeynikov8522e1c2008-02-20 11:26:25 +0000176 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Chris Lattner7f295ea2008-04-23 05:59:23 +0000177 for (unsigned i = 0, e = RetVals.size(); i != e; ++i) {
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000178 // Already found conflicting return values?
Chris Lattner7f295ea2008-04-23 05:59:23 +0000179 Value *RV = RetVals[i];
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000180 if (!RV)
181 continue;
182
183 // Find the returned value
184 Value *V;
Dan Gohmanef81dbc2008-10-03 22:21:24 +0000185 if (!STy)
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000186 V = RI->getOperand(i);
187 else
Nick Lewycky6e8bc9c2009-11-23 03:29:18 +0000188 V = FindInsertedValue(RI->getOperand(0), i);
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000189
190 if (V) {
191 // Ignore undefs, we can change them into anything
192 if (isa<UndefValue>(V))
193 continue;
194
Matthijs Kooijman675d7b02008-06-18 08:30:37 +0000195 // Try to see if all the rets return the same constant or argument.
196 if (isa<Constant>(V) || isa<Argument>(V)) {
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000197 if (isa<UndefValue>(RV)) {
198 // No value found yet? Try the current one.
199 RetVals[i] = V;
200 continue;
201 }
202 // Returning the same value? Good.
203 if (RV == V)
204 continue;
205 }
206 }
207 // Different or no known return value? Don't propagate this return
208 // value.
209 RetVals[i] = 0;
210 // All values non constant? Stop looking.
211 if (++NumNonConstant == RetVals.size())
212 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 }
Anton Korobeynikov8522e1c2008-02-20 11:26:25 +0000214 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000216 // If we got here, the function returns at least one constant value. Loop
217 // over all users, replacing any uses of the return value with the returned
218 // constant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 bool MadeChange = false;
Chris Lattner7f295ea2008-04-23 05:59:23 +0000220 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
Matthijs Kooijman43fa9f62008-06-19 08:53:24 +0000221 CallSite CS = CallSite::get(*UI);
222 Instruction* Call = CS.getInstruction();
223
224 // Not a call instruction or a call instruction that's not calling F
225 // directly?
Gabor Greif6be9a1b2009-01-22 21:35:57 +0000226 if (!Call || !CS.isCallee(UI))
Chris Lattner7f295ea2008-04-23 05:59:23 +0000227 continue;
Chris Lattner7f295ea2008-04-23 05:59:23 +0000228
Matthijs Kooijman43fa9f62008-06-19 08:53:24 +0000229 // Call result not used?
Chris Lattner7f295ea2008-04-23 05:59:23 +0000230 if (Call->use_empty())
231 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Chris Lattner7f295ea2008-04-23 05:59:23 +0000233 MadeChange = true;
234
235 if (STy == 0) {
Matthijs Kooijman675d7b02008-06-18 08:30:37 +0000236 Value* New = RetVals[0];
237 if (Argument *A = dyn_cast<Argument>(New))
238 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijman43fa9f62008-06-19 08:53:24 +0000239 // the call instruction and use that.
240 New = CS.getArgument(A->getArgNo());
Matthijs Kooijman675d7b02008-06-18 08:30:37 +0000241 Call->replaceAllUsesWith(New);
Chris Lattner7f295ea2008-04-23 05:59:23 +0000242 continue;
243 }
244
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000245 for (Value::use_iterator I = Call->use_begin(), E = Call->use_end();
246 I != E;) {
Jay Foadd1d6a142009-06-06 17:49:35 +0000247 Instruction *Ins = cast<Instruction>(*I);
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000248
249 // Increment now, so we can remove the use
250 ++I;
251
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000252 // Find the index of the retval to replace with
253 int index = -1;
Dan Gohman29474e92008-07-23 00:34:11 +0000254 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins))
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000255 if (EV->hasIndices())
256 index = *EV->idx_begin();
257
258 // If this use uses a specific return value, and we have a replacement,
259 // replace it.
260 if (index != -1) {
261 Value *New = RetVals[index];
262 if (New) {
Matthijs Kooijman675d7b02008-06-18 08:30:37 +0000263 if (Argument *A = dyn_cast<Argument>(New))
264 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijman43fa9f62008-06-19 08:53:24 +0000265 // the call instruction and use that.
266 New = CS.getArgument(A->getArgNo());
Matthijs Kooijman054407f2008-06-18 08:09:27 +0000267 Ins->replaceAllUsesWith(New);
268 Ins->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 }
Devang Patelf922e852008-03-20 18:30:32 +0000270 }
Devang Patel13f24372008-03-11 22:24:29 +0000271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 }
273
274 if (MadeChange) ++NumReturnValProped;
275 return MadeChange;
276}