blob: 792fa980a60b5ff29d767cf9bc5032cedb0faa4a [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 };
Chris Lattnerd358e6f2003-10-23 16:52:27 +000045}
46
Dan Gohman844731a2008-05-13 00:00:25 +000047char IPCP::ID = 0;
48static RegisterPass<IPCP>
49X("ipconstprop", "Interprocedural constant propagation");
50
Chris Lattnerb12914b2004-09-20 04:48:05 +000051ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000052
Chris Lattnerb12914b2004-09-20 04:48:05 +000053bool IPCP::runOnModule(Module &M) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000054 bool Changed = false;
Chris Lattner2e8dfb82003-10-27 21:09:00 +000055 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)
Reid Spencer5cbf9852007-01-30 20:08:39 +000062 if (!I->isDeclaration()) {
Chris Lattnera990d942004-11-14 06:10:11 +000063 // Delete any klingons.
64 I->removeDeadConstantUsers();
65 if (I->hasInternalLinkage())
66 LocalChange |= PropagateConstantsIntoArguments(*I);
67 Changed |= PropagateConstantReturn(*I);
68 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +000069 Changed |= LocalChange;
70 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000071 return Changed;
72}
73
Chris Lattnera990d942004-11-14 06:10:11 +000074/// 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.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000077///
Chris Lattnera990d942004-11-14 06:10:11 +000078bool IPCP::PropagateConstantsIntoArguments(Function &F) {
Chris Lattner7f8897f2006-08-27 22:42:52 +000079 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000080
Chris Lattner4af6ad32008-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;
Chris Lattnere4d5c442005-03-15 04:54:21 +000084 ArgumentConstants.resize(F.arg_size());
Chris Lattnerd358e6f2003-10-23 16:52:27 +000085
86 unsigned NumNonconstant = 0;
Chris Lattner4af6ad32008-04-23 06:16:27 +000087 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
88 // Used by a non-instruction, or not the callee of a function, do not
89 // transform.
90 if (UI.getOperandNo() != 0 ||
91 (!isa<CallInst>(*UI) && !isa<InvokeInst>(*UI)))
92 return false;
93
94 CallSite CS = CallSite::get(cast<Instruction>(*UI));
Chris Lattnerd358e6f2003-10-23 16:52:27 +000095
Chris Lattner4af6ad32008-04-23 06:16:27 +000096 // Check out all of the potentially constant arguments. Note that we don't
97 // inspect varargs here.
98 CallSite::arg_iterator AI = CS.arg_begin();
99 Function::arg_iterator Arg = F.arg_begin();
100 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
101 ++i, ++AI, ++Arg) {
102
103 // If this argument is known non-constant, ignore it.
104 if (ArgumentConstants[i].second)
105 continue;
106
107 Constant *C = dyn_cast<Constant>(*AI);
108 if (C && ArgumentConstants[i].first == 0) {
109 ArgumentConstants[i].first = C; // First constant seen.
110 } else if (C && ArgumentConstants[i].first == C) {
111 // Still the constant value we think it is.
112 } else if (*AI == &*Arg) {
113 // Ignore recursive calls passing argument down.
114 } else {
115 // Argument became non-constant. If all arguments are non-constant now,
116 // give up on this function.
117 if (++NumNonconstant == ArgumentConstants.size())
118 return false;
119 ArgumentConstants[i].second = true;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000120 }
121 }
Chris Lattner4af6ad32008-04-23 06:16:27 +0000122 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000123
124 // If we got to this point, there is a constant argument!
125 assert(NumNonconstant != ArgumentConstants.size());
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000126 bool MadeChange = false;
Chris Lattner4af6ad32008-04-23 06:16:27 +0000127 Function::arg_iterator AI = F.arg_begin();
128 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
129 // Do we have a constant argument?
130 if (ArgumentConstants[i].second || AI->use_empty())
131 continue;
132
133 Value *V = ArgumentConstants[i].first;
134 if (V == 0) V = UndefValue::get(AI->getType());
135 AI->replaceAllUsesWith(V);
136 ++NumArgumentsProped;
137 MadeChange = true;
138 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000139 return MadeChange;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000140}
Brian Gaeked0fde302003-11-11 22:41:34 +0000141
Chris Lattnera990d942004-11-14 06:10:11 +0000142
143// Check to see if this function returns a constant. If so, replace all callers
144// that user the return value with the returned valued. If we can replace ALL
145// callers,
146bool IPCP::PropagateConstantReturn(Function &F) {
147 if (F.getReturnType() == Type::VoidTy)
148 return false; // No return value.
149
150 // Check to see if this function returns a constant.
Devang Patel7db30ba2008-03-11 22:24:29 +0000151 SmallVector<Value *,4> RetVals;
Devang Patel7db30ba2008-03-11 22:24:29 +0000152 const StructType *STy = dyn_cast<StructType>(F.getReturnType());
153 if (STy)
Devang Patel488b6782008-03-20 18:30:32 +0000154 RetVals.assign(STy->getNumElements(), 0);
Devang Patel7db30ba2008-03-11 22:24:29 +0000155 else
Devang Patel7db30ba2008-03-11 22:24:29 +0000156 RetVals.push_back(0);
157
Chris Lattnera990d942004-11-14 06:10:11 +0000158 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000159 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Chris Lattnere9625c62008-04-23 05:59:23 +0000160 assert(RetVals.size() == RI->getNumOperands() &&
161 "Invalid ReturnInst operands!");
162 for (unsigned i = 0, e = RetVals.size(); i != e; ++i) {
163 if (isa<UndefValue>(RI->getOperand(i)))
164 continue; // Ignore
165 Constant *C = dyn_cast<Constant>(RI->getOperand(i));
166 if (C == 0)
Devang Patel7db30ba2008-03-11 22:24:29 +0000167 return false; // Does not return a constant.
Chris Lattnere9625c62008-04-23 05:59:23 +0000168
169 Value *RV = RetVals[i];
170 if (RV == 0)
171 RetVals[i] = C;
172 else if (RV != C)
173 return false; // Does not return the same constant.
Chris Lattnera990d942004-11-14 06:10:11 +0000174 }
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000175 }
Chris Lattnera990d942004-11-14 06:10:11 +0000176
Devang Patel488b6782008-03-20 18:30:32 +0000177 if (STy) {
178 for (unsigned i = 0, e = RetVals.size(); i < e; ++i)
179 if (RetVals[i] == 0)
Devang Patel7db30ba2008-03-11 22:24:29 +0000180 RetVals[i] = UndefValue::get(STy->getElementType(i));
Devang Patel488b6782008-03-20 18:30:32 +0000181 } else {
Chris Lattnere9625c62008-04-23 05:59:23 +0000182 assert(RetVals.size() == 1);
183 if (RetVals[0] == 0)
184 RetVals[0] = UndefValue::get(F.getReturnType());
Devang Patel7db30ba2008-03-11 22:24:29 +0000185 }
Chris Lattnera990d942004-11-14 06:10:11 +0000186
187 // If we got here, the function returns a constant value. Loop over all
188 // users, replacing any uses of the return value with the returned constant.
189 bool ReplacedAllUsers = true;
190 bool MadeChange = false;
Chris Lattnere9625c62008-04-23 05:59:23 +0000191 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
192 // Make sure this is an invoke or call and that the use is for the callee.
193 if (!(isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) ||
194 UI.getOperandNo() != 0) {
Chris Lattnera990d942004-11-14 06:10:11 +0000195 ReplacedAllUsers = false;
Chris Lattnere9625c62008-04-23 05:59:23 +0000196 continue;
Chris Lattnera990d942004-11-14 06:10:11 +0000197 }
Chris Lattnere9625c62008-04-23 05:59:23 +0000198
199 Instruction *Call = cast<Instruction>(*UI);
200 if (Call->use_empty())
201 continue;
Chris Lattnera990d942004-11-14 06:10:11 +0000202
Chris Lattnere9625c62008-04-23 05:59:23 +0000203 MadeChange = true;
204
205 if (STy == 0) {
206 Call->replaceAllUsesWith(RetVals[0]);
207 continue;
208 }
209
210 while (!Call->use_empty()) {
211 GetResultInst *GR = cast<GetResultInst>(Call->use_back());
212 GR->replaceAllUsesWith(RetVals[GR->getIndex()]);
213 GR->eraseFromParent();
214 }
215 }
216
Chris Lattnera990d942004-11-14 06:10:11 +0000217 // If we replace all users with the returned constant, and there can be no
218 // other callers of the function, replace the constant being returned in the
219 // function with an undef value.
Devang Patel7db30ba2008-03-11 22:24:29 +0000220 if (ReplacedAllUsers && F.hasInternalLinkage()) {
Devang Patel488b6782008-03-20 18:30:32 +0000221 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
222 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
223 for (unsigned i = 0, e = RetVals.size(); i < e; ++i) {
224 Value *RetVal = RetVals[i];
225 if (isa<UndefValue>(RetVal))
226 continue;
227 Value *RV = UndefValue::get(RetVal->getType());
Devang Patel7db30ba2008-03-11 22:24:29 +0000228 if (RI->getOperand(i) != RV) {
229 RI->setOperand(i, RV);
230 MadeChange = true;
231 }
Chris Lattner2ffa47b2004-12-11 17:00:14 +0000232 }
Devang Patel488b6782008-03-20 18:30:32 +0000233 }
Devang Patel7db30ba2008-03-11 22:24:29 +0000234 }
Chris Lattnera990d942004-11-14 06:10:11 +0000235 }
236
237 if (MadeChange) ++NumReturnValProped;
Chris Lattnera990d942004-11-14 06:10:11 +0000238 return MadeChange;
239}