blob: 8684796b4e7835b729c89301650ca2af680c7824 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/ValueTracking.h"
Stephen Hines36b56882014-04-23 16:57:46 -070023#include "llvm/IR/CallSite.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Module.h"
Chris Lattnerd358e6f2003-10-23 16:52:27 +000027#include "llvm/Pass.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 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +000036 struct IPCP : public ModulePass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000038 IPCP() : ModulePass(ID) {
39 initializeIPCPPass(*PassRegistry::getPassRegistry());
40 }
Devang Patel794fd752007-05-01 21:15:47 +000041
Stephen Hines36b56882014-04-23 16:57:46 -070042 bool runOnModule(Module &M) override;
Chris Lattnerd358e6f2003-10-23 16:52:27 +000043 private:
Chris Lattnera990d942004-11-14 06:10:11 +000044 bool PropagateConstantsIntoArguments(Function &F);
45 bool PropagateConstantReturn(Function &F);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000046 };
Chris Lattnerd358e6f2003-10-23 16:52:27 +000047}
48
Dan Gohman844731a2008-05-13 00:00:25 +000049char IPCP::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000050INITIALIZE_PASS(IPCP, "ipconstprop",
Owen Andersonce665bd2010-10-07 22:25:06 +000051 "Interprocedural constant propagation", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000052
Chris Lattnerb12914b2004-09-20 04:48:05 +000053ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000054
Chris Lattnerb12914b2004-09-20 04:48:05 +000055bool IPCP::runOnModule(Module &M) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000056 bool Changed = false;
Chris Lattner2e8dfb82003-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 Spencer5cbf9852007-01-30 20:08:39 +000064 if (!I->isDeclaration()) {
Chris Lattnera990d942004-11-14 06:10:11 +000065 // Delete any klingons.
66 I->removeDeadConstantUsers();
Rafael Espindolabb46f522009-01-15 20:18:42 +000067 if (I->hasLocalLinkage())
Chris Lattnera990d942004-11-14 06:10:11 +000068 LocalChange |= PropagateConstantsIntoArguments(*I);
69 Changed |= PropagateConstantReturn(*I);
70 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +000071 Changed |= LocalChange;
72 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000073 return Changed;
74}
75
Chris Lattnera990d942004-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 Lattnerd358e6f2003-10-23 16:52:27 +000079///
Chris Lattnera990d942004-11-14 06:10:11 +000080bool IPCP::PropagateConstantsIntoArguments(Function &F) {
Chris Lattner7f8897f2006-08-27 22:42:52 +000081 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000082
Chris Lattner4af6ad32008-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 Lattnere4d5c442005-03-15 04:54:21 +000086 ArgumentConstants.resize(F.arg_size());
Chris Lattnerd358e6f2003-10-23 16:52:27 +000087
88 unsigned NumNonconstant = 0;
Stephen Hines36b56882014-04-23 16:57:46 -070089 for (Use &U : F.uses()) {
90 User *UR = U.getUser();
Chris Lattnerb2710042009-11-01 06:11:53 +000091 // Ignore blockaddress uses.
Stephen Hines36b56882014-04-23 16:57:46 -070092 if (isa<BlockAddress>(UR)) continue;
Chris Lattnerb2710042009-11-01 06:11:53 +000093
Chris Lattner4af6ad32008-04-23 06:16:27 +000094 // Used by a non-instruction, or not the callee of a function, do not
95 // transform.
Stephen Hines36b56882014-04-23 16:57:46 -070096 if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR))
Chris Lattner4af6ad32008-04-23 06:16:27 +000097 return false;
98
Stephen Hines36b56882014-04-23 16:57:46 -070099 CallSite CS(cast<Instruction>(UR));
100 if (!CS.isCallee(&U))
Gabor Greifedc4d692009-01-22 21:35:57 +0000101 return false;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000102
Chris Lattner4af6ad32008-04-23 06:16:27 +0000103 // Check out all of the potentially constant arguments. Note that we don't
104 // inspect varargs here.
105 CallSite::arg_iterator AI = CS.arg_begin();
106 Function::arg_iterator Arg = F.arg_begin();
107 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
108 ++i, ++AI, ++Arg) {
109
110 // If this argument is known non-constant, ignore it.
111 if (ArgumentConstants[i].second)
112 continue;
113
114 Constant *C = dyn_cast<Constant>(*AI);
115 if (C && ArgumentConstants[i].first == 0) {
116 ArgumentConstants[i].first = C; // First constant seen.
117 } else if (C && ArgumentConstants[i].first == C) {
118 // Still the constant value we think it is.
119 } else if (*AI == &*Arg) {
120 // Ignore recursive calls passing argument down.
121 } else {
122 // Argument became non-constant. If all arguments are non-constant now,
123 // give up on this function.
124 if (++NumNonconstant == ArgumentConstants.size())
125 return false;
126 ArgumentConstants[i].second = true;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000127 }
128 }
Chris Lattner4af6ad32008-04-23 06:16:27 +0000129 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000130
131 // If we got to this point, there is a constant argument!
132 assert(NumNonconstant != ArgumentConstants.size());
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000133 bool MadeChange = false;
Chris Lattner4af6ad32008-04-23 06:16:27 +0000134 Function::arg_iterator AI = F.arg_begin();
135 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
136 // Do we have a constant argument?
Torok Edwinc3384992009-09-24 18:33:42 +0000137 if (ArgumentConstants[i].second || AI->use_empty() ||
Stephen Hines36b56882014-04-23 16:57:46 -0700138 AI->hasInAllocaAttr() || (AI->hasByValAttr() && !F.onlyReadsMemory()))
Chris Lattner4af6ad32008-04-23 06:16:27 +0000139 continue;
140
141 Value *V = ArgumentConstants[i].first;
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000142 if (V == 0) V = UndefValue::get(AI->getType());
Chris Lattner4af6ad32008-04-23 06:16:27 +0000143 AI->replaceAllUsesWith(V);
144 ++NumArgumentsProped;
145 MadeChange = true;
146 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000147 return MadeChange;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000148}
Brian Gaeked0fde302003-11-11 22:41:34 +0000149
Chris Lattnera990d942004-11-14 06:10:11 +0000150
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000151// Check to see if this function returns one or more constants. If so, replace
152// all callers that use those return values with the constant value. This will
153// leave in the actual return values and instructions, but deadargelim will
154// clean that up.
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000155//
156// Additionally if a function always returns one of its arguments directly,
157// callers will be updated to use the value they pass in directly instead of
158// using the return value.
Chris Lattnera990d942004-11-14 06:10:11 +0000159bool IPCP::PropagateConstantReturn(Function &F) {
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000160 if (F.getReturnType()->isVoidTy())
Chris Lattnera990d942004-11-14 06:10:11 +0000161 return false; // No return value.
162
Chris Lattner18d73c22008-06-09 07:58:07 +0000163 // If this function could be overridden later in the link stage, we can't
164 // propagate information about its results into callers.
Nuno Lopes5ed3b892008-09-29 14:40:32 +0000165 if (F.mayBeOverridden())
Chris Lattner18d73c22008-06-09 07:58:07 +0000166 return false;
Owen Andersone922c022009-07-22 00:24:57 +0000167
Chris Lattnera990d942004-11-14 06:10:11 +0000168 // Check to see if this function returns a constant.
Devang Patel7db30ba2008-03-11 22:24:29 +0000169 SmallVector<Value *,4> RetVals;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000170 StructType *STy = dyn_cast<StructType>(F.getReturnType());
Devang Patel7db30ba2008-03-11 22:24:29 +0000171 if (STy)
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000172 for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000173 RetVals.push_back(UndefValue::get(STy->getElementType(i)));
Devang Patel7db30ba2008-03-11 22:24:29 +0000174 else
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000175 RetVals.push_back(UndefValue::get(F.getReturnType()));
Devang Patel7db30ba2008-03-11 22:24:29 +0000176
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000177 unsigned NumNonConstant = 0;
Chris Lattnera990d942004-11-14 06:10:11 +0000178 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000179 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Chris Lattnere9625c62008-04-23 05:59:23 +0000180 for (unsigned i = 0, e = RetVals.size(); i != e; ++i) {
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000181 // Already found conflicting return values?
Chris Lattnere9625c62008-04-23 05:59:23 +0000182 Value *RV = RetVals[i];
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000183 if (!RV)
184 continue;
185
186 // Find the returned value
187 Value *V;
Dan Gohman792e1e92008-10-03 22:21:24 +0000188 if (!STy)
Jay Foad3e2f74e2011-04-04 07:44:02 +0000189 V = RI->getOperand(0);
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000190 else
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000191 V = FindInsertedValue(RI->getOperand(0), i);
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000192
193 if (V) {
194 // Ignore undefs, we can change them into anything
195 if (isa<UndefValue>(V))
196 continue;
197
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000198 // Try to see if all the rets return the same constant or argument.
199 if (isa<Constant>(V) || isa<Argument>(V)) {
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000200 if (isa<UndefValue>(RV)) {
201 // No value found yet? Try the current one.
202 RetVals[i] = V;
203 continue;
204 }
205 // Returning the same value? Good.
206 if (RV == V)
207 continue;
208 }
209 }
210 // Different or no known return value? Don't propagate this return
211 // value.
212 RetVals[i] = 0;
Stephen Hines36b56882014-04-23 16:57:46 -0700213 // All values non-constant? Stop looking.
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000214 if (++NumNonConstant == RetVals.size())
215 return false;
Chris Lattnera990d942004-11-14 06:10:11 +0000216 }
Anton Korobeynikov07e6e562008-02-20 11:26:25 +0000217 }
Chris Lattnera990d942004-11-14 06:10:11 +0000218
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000219 // If we got here, the function returns at least one constant value. Loop
220 // over all users, replacing any uses of the return value with the returned
221 // constant.
Chris Lattnera990d942004-11-14 06:10:11 +0000222 bool MadeChange = false;
Stephen Hines36b56882014-04-23 16:57:46 -0700223 for (Use &U : F.uses()) {
224 CallSite CS(U.getUser());
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000225 Instruction* Call = CS.getInstruction();
226
227 // Not a call instruction or a call instruction that's not calling F
228 // directly?
Stephen Hines36b56882014-04-23 16:57:46 -0700229 if (!Call || !CS.isCallee(&U))
Chris Lattnere9625c62008-04-23 05:59:23 +0000230 continue;
Chris Lattnere9625c62008-04-23 05:59:23 +0000231
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000232 // Call result not used?
Chris Lattnere9625c62008-04-23 05:59:23 +0000233 if (Call->use_empty())
234 continue;
Chris Lattnera990d942004-11-14 06:10:11 +0000235
Chris Lattnere9625c62008-04-23 05:59:23 +0000236 MadeChange = true;
237
238 if (STy == 0) {
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000239 Value* New = RetVals[0];
240 if (Argument *A = dyn_cast<Argument>(New))
241 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000242 // the call instruction and use that.
243 New = CS.getArgument(A->getArgNo());
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000244 Call->replaceAllUsesWith(New);
Chris Lattnere9625c62008-04-23 05:59:23 +0000245 continue;
246 }
Stephen Hines36b56882014-04-23 16:57:46 -0700247
248 for (auto I = Call->user_begin(), E = Call->user_end(); I != E;) {
Jay Foad0906b1b2009-06-06 17:49:35 +0000249 Instruction *Ins = cast<Instruction>(*I);
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000250
251 // Increment now, so we can remove the use
252 ++I;
253
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000254 // Find the index of the retval to replace with
255 int index = -1;
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000256 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins))
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000257 if (EV->hasIndices())
258 index = *EV->idx_begin();
259
260 // If this use uses a specific return value, and we have a replacement,
261 // replace it.
262 if (index != -1) {
263 Value *New = RetVals[index];
264 if (New) {
Matthijs Kooijmanc2afe892008-06-18 08:30:37 +0000265 if (Argument *A = dyn_cast<Argument>(New))
266 // Was an argument returned? Then find the corresponding argument in
Matthijs Kooijmanf0da2032008-06-19 08:53:24 +0000267 // the call instruction and use that.
268 New = CS.getArgument(A->getArgNo());
Matthijs Kooijman8b0fcf32008-06-18 08:09:27 +0000269 Ins->replaceAllUsesWith(New);
270 Ins->eraseFromParent();
Chris Lattner2ffa47b2004-12-11 17:00:14 +0000271 }
Devang Patel488b6782008-03-20 18:30:32 +0000272 }
Devang Patel7db30ba2008-03-11 22:24:29 +0000273 }
Chris Lattnera990d942004-11-14 06:10:11 +0000274 }
275
276 if (MadeChange) ++NumReturnValProped;
Chris Lattnera990d942004-11-14 06:10:11 +0000277 return MadeChange;
278}