blob: 2dc8558246915385a2298a144b17217b07ef7be0 [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"
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +000024#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerd358e6f2003-10-23 16:52:27 +000025#include "llvm/Support/CallSite.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000026#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
Devang Patel7db30ba2008-03-11 22:24:29 +000028#include "llvm/ADT/SmallVector.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner86453c52006-12-19 22:09:18 +000031STATISTIC(NumArgumentsProped, "Number of args turned into constants");
32STATISTIC(NumReturnValProped, "Number of return values turned into constants");
Chris Lattnerd358e6f2003-10-23 16:52:27 +000033
Chris Lattner86453c52006-12-19 22:09:18 +000034namespace {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000035 /// IPCP - The interprocedural constant propagation pass
36 ///
Reid Spencer9133fe22007-02-05 23:32:05 +000037 struct VISIBILITY_HIDDEN IPCP : public ModulePass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000039 IPCP() : ModulePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000040
Chris Lattnerb12914b2004-09-20 04:48:05 +000041 bool runOnModule(Module &M);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000042 private:
Chris Lattnera990d942004-11-14 06:10:11 +000043 bool PropagateConstantsIntoArguments(Function &F);
44 bool PropagateConstantReturn(Function &F);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000045 };
Chris Lattnerd358e6f2003-10-23 16:52:27 +000046}
47
Dan Gohman844731a2008-05-13 00:00:25 +000048char IPCP::ID = 0;
49static RegisterPass<IPCP>
50X("ipconstprop", "Interprocedural constant propagation");
51
Chris Lattnerb12914b2004-09-20 04:48:05 +000052ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000053
Chris Lattnerb12914b2004-09-20 04:48:05 +000054bool IPCP::runOnModule(Module &M) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000055 bool Changed = false;
Chris Lattner2e8dfb82003-10-27 21:09:00 +000056 bool LocalChange = true;
57
58 // FIXME: instead of using smart algorithms, we just iterate until we stop
59 // making changes.
60 while (LocalChange) {
61 LocalChange = false;
62 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000063 if (!I->isDeclaration()) {
Chris Lattnera990d942004-11-14 06:10:11 +000064 // Delete any klingons.
65 I->removeDeadConstantUsers();
Rafael Espindolabb46f522009-01-15 20:18:42 +000066 if (I->hasLocalLinkage())
Chris Lattnera990d942004-11-14 06:10:11 +000067 LocalChange |= PropagateConstantsIntoArguments(*I);
68 Changed |= PropagateConstantReturn(*I);
69 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +000070 Changed |= LocalChange;
71 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000072 return Changed;
73}
74
Chris Lattnera990d942004-11-14 06:10:11 +000075/// PropagateConstantsIntoArguments - Look at all uses of the specified
76/// function. If all uses are direct call sites, and all pass a particular
77/// constant in for an argument, propagate that constant in as the argument.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000078///
Chris Lattnera990d942004-11-14 06:10:11 +000079bool IPCP::PropagateConstantsIntoArguments(Function &F) {
Chris Lattner7f8897f2006-08-27 22:42:52 +000080 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000081
Chris Lattner4af6ad32008-04-23 06:16:27 +000082 // For each argument, keep track of its constant value and whether it is a
83 // constant or not. The bool is driven to true when found to be non-constant.
84 SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants;
Chris Lattnere4d5c442005-03-15 04:54:21 +000085 ArgumentConstants.resize(F.arg_size());
Chris Lattnerd358e6f2003-10-23 16:52:27 +000086
87 unsigned NumNonconstant = 0;
Chris Lattner4af6ad32008-04-23 06:16:27 +000088 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
89 // Used by a non-instruction, or not the callee of a function, do not
90 // transform.
Gabor Greifedc4d692009-01-22 21:35:57 +000091 if (!isa<CallInst>(*UI) && !isa<InvokeInst>(*UI))
Chris Lattner4af6ad32008-04-23 06:16:27 +000092 return false;
93
94 CallSite CS = CallSite::get(cast<Instruction>(*UI));
Gabor Greifedc4d692009-01-22 21:35:57 +000095 if (!CS.isCallee(UI))
96 return false;
Chris Lattnerd358e6f2003-10-23 16:52:27 +000097
Chris Lattner4af6ad32008-04-23 06:16:27 +000098 // Check out all of the potentially constant arguments. Note that we don't
99 // inspect varargs here.
100 CallSite::arg_iterator AI = CS.arg_begin();
101 Function::arg_iterator Arg = F.arg_begin();
102 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
103 ++i, ++AI, ++Arg) {
104
105 // If this argument is known non-constant, ignore it.
106 if (ArgumentConstants[i].second)
107 continue;
108
109 Constant *C = dyn_cast<Constant>(*AI);
110 if (C && ArgumentConstants[i].first == 0) {
111 ArgumentConstants[i].first = C; // First constant seen.
112 } else if (C && ArgumentConstants[i].first == C) {
113 // Still the constant value we think it is.
114 } else if (*AI == &*Arg) {
115 // Ignore recursive calls passing argument down.
116 } else {
117 // Argument became non-constant. If all arguments are non-constant now,
118 // give up on this function.
119 if (++NumNonconstant == ArgumentConstants.size())
120 return false;
121 ArgumentConstants[i].second = true;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000122 }
123 }
Chris Lattner4af6ad32008-04-23 06:16:27 +0000124 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000125
126 // If we got to this point, there is a constant argument!
127 assert(NumNonconstant != ArgumentConstants.size());
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000128 bool MadeChange = false;
Chris Lattner4af6ad32008-04-23 06:16:27 +0000129 Function::arg_iterator AI = F.arg_begin();
130 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
131 // Do we have a constant argument?
132 if (ArgumentConstants[i].second || AI->use_empty())
133 continue;
134
135 Value *V = ArgumentConstants[i].first;
136 if (V == 0) V = UndefValue::get(AI->getType());
137 AI->replaceAllUsesWith(V);
138 ++NumArgumentsProped;
139 MadeChange = true;
140 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000141 return MadeChange;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000142}
Brian Gaeked0fde302003-11-11 22:41:34 +0000143
Chris Lattnera990d942004-11-14 06:10:11 +0000144
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000145// Check to see if this function returns one or more constants. If so, replace
146// all callers that use those return values with the constant value. This will
147// leave in the actual return values and instructions, but deadargelim will
148// clean that up.
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000149//
150// Additionally if a function always returns one of its arguments directly,
151// callers will be updated to use the value they pass in directly instead of
152// using the return value.
Chris Lattnera990d942004-11-14 06:10:11 +0000153bool IPCP::PropagateConstantReturn(Function &F) {
154 if (F.getReturnType() == Type::VoidTy)
155 return false; // No return value.
156
Chris Lattner18d73c22008-06-09 07:58:07 +0000157 // If this function could be overridden later in the link stage, we can't
158 // propagate information about its results into callers.
Nuno Lopes5ed3b892008-09-29 14:40:32 +0000159 if (F.mayBeOverridden())
Chris Lattner18d73c22008-06-09 07:58:07 +0000160 return false;
161
Chris Lattnera990d942004-11-14 06:10:11 +0000162 // Check to see if this function returns a constant.
Devang Patel7db30ba2008-03-11 22:24:29 +0000163 SmallVector<Value *,4> RetVals;
Devang Patel7db30ba2008-03-11 22:24:29 +0000164 const StructType *STy = dyn_cast<StructType>(F.getReturnType());
165 if (STy)
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000166 for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i)
167 RetVals.push_back(UndefValue::get(STy->getElementType(i)));
Devang Patel7db30ba2008-03-11 22:24:29 +0000168 else
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000169 RetVals.push_back(UndefValue::get(F.getReturnType()));
Devang Patel7db30ba2008-03-11 22:24:29 +0000170
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000171 unsigned NumNonConstant = 0;
Chris Lattnera990d942004-11-14 06:10:11 +0000172 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000173 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Chris Lattnere9625c62008-04-23 05:59:23 +0000174 for (unsigned i = 0, e = RetVals.size(); i != e; ++i) {
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000175 // Already found conflicting return values?
Chris Lattnere9625c62008-04-23 05:59:23 +0000176 Value *RV = RetVals[i];
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000177 if (!RV)
178 continue;
179
180 // Find the returned value
181 Value *V;
Dan Gohman792e1e92008-10-03 22:21:24 +0000182 if (!STy)
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000183 V = RI->getOperand(i);
184 else
185 V = FindInsertedValue(RI->getOperand(0), i);
186
187 if (V) {
188 // Ignore undefs, we can change them into anything
189 if (isa<UndefValue>(V))
190 continue;
191
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000192 // Try to see if all the rets return the same constant or argument.
193 if (isa<Constant>(V) || isa<Argument>(V)) {
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000194 if (isa<UndefValue>(RV)) {
195 // No value found yet? Try the current one.
196 RetVals[i] = V;
197 continue;
198 }
199 // Returning the same value? Good.
200 if (RV == V)
201 continue;
202 }
203 }
204 // Different or no known return value? Don't propagate this return
205 // value.
206 RetVals[i] = 0;
207 // All values non constant? Stop looking.
208 if (++NumNonConstant == RetVals.size())
209 return false;
Chris Lattnera990d942004-11-14 06:10:11 +0000210 }
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000211 }
Chris Lattnera990d942004-11-14 06:10:11 +0000212
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000213 // If we got here, the function returns at least one constant value. Loop
214 // over all users, replacing any uses of the return value with the returned
215 // constant.
Chris Lattnera990d942004-11-14 06:10:11 +0000216 bool MadeChange = false;
Chris Lattnere9625c62008-04-23 05:59:23 +0000217 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000218 CallSite CS = CallSite::get(*UI);
219 Instruction* Call = CS.getInstruction();
220
221 // Not a call instruction or a call instruction that's not calling F
222 // directly?
Gabor Greifedc4d692009-01-22 21:35:57 +0000223 if (!Call || !CS.isCallee(UI))
Chris Lattnere9625c62008-04-23 05:59:23 +0000224 continue;
Chris Lattnere9625c62008-04-23 05:59:23 +0000225
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000226 // Call result not used?
Chris Lattnere9625c62008-04-23 05:59:23 +0000227 if (Call->use_empty())
228 continue;
Chris Lattnera990d942004-11-14 06:10:11 +0000229
Chris Lattnere9625c62008-04-23 05:59:23 +0000230 MadeChange = true;
231
232 if (STy == 0) {
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000233 Value* New = RetVals[0];
234 if (Argument *A = dyn_cast<Argument>(New))
235 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000236 // the call instruction and use that.
237 New = CS.getArgument(A->getArgNo());
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000238 Call->replaceAllUsesWith(New);
Chris Lattnere9625c62008-04-23 05:59:23 +0000239 continue;
240 }
241
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000242 for (Value::use_iterator I = Call->use_begin(), E = Call->use_end();
243 I != E;) {
244 Instruction *Ins = dyn_cast<Instruction>(*I);
245
246 // Increment now, so we can remove the use
247 ++I;
248
249 // Not an instruction? Ignore
250 if (!Ins)
251 continue;
252
253 // Find the index of the retval to replace with
254 int index = -1;
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000255 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins))
Matthijs Kooijman8b0fcf32008-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 Kooijmanc2afe892008-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 Kooijmanf0da2032008-06-19 08:53:24 +0000266 // the call instruction and use that.
267 New = CS.getArgument(A->getArgNo());
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000268 Ins->replaceAllUsesWith(New);
269 Ins->eraseFromParent();
Chris Lattner2ffa47b2004-12-11 17:00:14 +0000270 }
Devang Patel488b6782008-03-20 18:30:32 +0000271 }
Devang Patel7db30ba2008-03-11 22:24:29 +0000272 }
Chris Lattnera990d942004-11-14 06:10:11 +0000273 }
274
275 if (MadeChange) ++NumReturnValProped;
Chris Lattnera990d942004-11-14 06:10:11 +0000276 return MadeChange;
277}