blob: 8321b22bf04f0888b3a30c4940e3583770ce2673 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattner1e2385b2003-11-21 21:54:22 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner86453c52006-12-19 22:09:18 +000029STATISTIC(NumArgumentsProped, "Number of args turned into constants");
30STATISTIC(NumReturnValProped, "Number of return values turned into constants");
Chris Lattnerd358e6f2003-10-23 16:52:27 +000031
Chris Lattner86453c52006-12-19 22:09:18 +000032namespace {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000033 /// IPCP - The interprocedural constant propagation pass
34 ///
Reid Spencer9133fe22007-02-05 23:32:05 +000035 struct VISIBILITY_HIDDEN IPCP : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000037 IPCP() : ModulePass((intptr_t)&ID) {}
38
Chris Lattnerb12914b2004-09-20 04:48:05 +000039 bool runOnModule(Module &M);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000040 private:
Chris Lattnera990d942004-11-14 06:10:11 +000041 bool PropagateConstantsIntoArguments(Function &F);
42 bool PropagateConstantReturn(Function &F);
Chris Lattnerd358e6f2003-10-23 16:52:27 +000043 };
Devang Patel19974732007-05-03 01:11:54 +000044 char IPCP::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000045 RegisterPass<IPCP> X("ipconstprop", "Interprocedural constant propagation");
Chris Lattnerd358e6f2003-10-23 16:52:27 +000046}
47
Chris Lattnerb12914b2004-09-20 04:48:05 +000048ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000049
Chris Lattnerb12914b2004-09-20 04:48:05 +000050bool IPCP::runOnModule(Module &M) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000051 bool Changed = false;
Chris Lattner2e8dfb82003-10-27 21:09:00 +000052 bool LocalChange = true;
53
54 // FIXME: instead of using smart algorithms, we just iterate until we stop
55 // making changes.
56 while (LocalChange) {
57 LocalChange = false;
58 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000059 if (!I->isDeclaration()) {
Chris Lattnera990d942004-11-14 06:10:11 +000060 // Delete any klingons.
61 I->removeDeadConstantUsers();
62 if (I->hasInternalLinkage())
63 LocalChange |= PropagateConstantsIntoArguments(*I);
64 Changed |= PropagateConstantReturn(*I);
65 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +000066 Changed |= LocalChange;
67 }
Chris Lattnerd358e6f2003-10-23 16:52:27 +000068 return Changed;
69}
70
Chris Lattnera990d942004-11-14 06:10:11 +000071/// PropagateConstantsIntoArguments - Look at all uses of the specified
72/// function. If all uses are direct call sites, and all pass a particular
73/// constant in for an argument, propagate that constant in as the argument.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000074///
Chris Lattnera990d942004-11-14 06:10:11 +000075bool IPCP::PropagateConstantsIntoArguments(Function &F) {
Chris Lattner7f8897f2006-08-27 22:42:52 +000076 if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
Chris Lattnerd358e6f2003-10-23 16:52:27 +000077
78 std::vector<std::pair<Constant*, bool> > ArgumentConstants;
Chris Lattnere4d5c442005-03-15 04:54:21 +000079 ArgumentConstants.resize(F.arg_size());
Chris Lattnerd358e6f2003-10-23 16:52:27 +000080
81 unsigned NumNonconstant = 0;
82
83 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
84 if (!isa<Instruction>(*I))
85 return false; // Used by a non-instruction, do not transform
86 else {
87 CallSite CS = CallSite::get(cast<Instruction>(*I));
Misha Brukmanfd939082005-04-21 23:48:37 +000088 if (CS.getInstruction() == 0 ||
Chris Lattnerd358e6f2003-10-23 16:52:27 +000089 CS.getCalledFunction() != &F)
90 return false; // Not a direct call site?
Misha Brukmanfd939082005-04-21 23:48:37 +000091
Chris Lattnerd358e6f2003-10-23 16:52:27 +000092 // Check out all of the potentially constant arguments
93 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattnere4d5c442005-03-15 04:54:21 +000094 Function::arg_iterator Arg = F.arg_begin();
Chris Lattnerff1529b2004-11-10 19:43:59 +000095 for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
96 ++i, ++AI, ++Arg) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +000097 if (*AI == &F) return false; // Passes the function into itself
98
99 if (!ArgumentConstants[i].second) {
Reid Spencer3188cab2004-07-18 08:31:18 +0000100 if (Constant *C = dyn_cast<Constant>(*AI)) {
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000101 if (!ArgumentConstants[i].first)
102 ArgumentConstants[i].first = C;
103 else if (ArgumentConstants[i].first != C) {
104 // Became non-constant
105 ArgumentConstants[i].second = true;
106 ++NumNonconstant;
107 if (NumNonconstant == ArgumentConstants.size()) return false;
108 }
Chris Lattnerff1529b2004-11-10 19:43:59 +0000109 } else if (*AI != &*Arg) { // Ignore recursive calls with same arg
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000110 // This is not a constant argument. Mark the argument as
111 // non-constant.
112 ArgumentConstants[i].second = true;
113 ++NumNonconstant;
114 if (NumNonconstant == ArgumentConstants.size()) return false;
115 }
116 }
117 }
118 }
119
120 // If we got to this point, there is a constant argument!
121 assert(NumNonconstant != ArgumentConstants.size());
Chris Lattnere4d5c442005-03-15 04:54:21 +0000122 Function::arg_iterator AI = F.arg_begin();
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000123 bool MadeChange = false;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000124 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
125 // Do we have a constant argument!?
Chris Lattnerd79c7b42004-11-11 07:47:54 +0000126 if (!ArgumentConstants[i].second && !AI->use_empty()) {
Chris Lattner2e56dd82003-10-23 18:49:23 +0000127 Value *V = ArgumentConstants[i].first;
Chris Lattner3e062ea2004-11-11 07:46:29 +0000128 if (V == 0) V = UndefValue::get(AI->getType());
Chris Lattner2e56dd82003-10-23 18:49:23 +0000129 AI->replaceAllUsesWith(V);
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000130 ++NumArgumentsProped;
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000131 MadeChange = true;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000132 }
Chris Lattner2e8dfb82003-10-27 21:09:00 +0000133 return MadeChange;
Chris Lattnerd358e6f2003-10-23 16:52:27 +0000134}
Brian Gaeked0fde302003-11-11 22:41:34 +0000135
Chris Lattnera990d942004-11-14 06:10:11 +0000136
137// Check to see if this function returns a constant. If so, replace all callers
138// that user the return value with the returned valued. If we can replace ALL
139// callers,
140bool IPCP::PropagateConstantReturn(Function &F) {
141 if (F.getReturnType() == Type::VoidTy)
142 return false; // No return value.
143
144 // Check to see if this function returns a constant.
145 Value *RetVal = 0;
146 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
147 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
148 if (isa<UndefValue>(RI->getOperand(0))) {
149 // Ignore.
150 } else if (Constant *C = dyn_cast<Constant>(RI->getOperand(0))) {
151 if (RetVal == 0)
152 RetVal = C;
153 else if (RetVal != C)
154 return false; // Does not return the same constant.
155 } else {
156 return false; // Does not return a constant.
157 }
158
159 if (RetVal == 0) RetVal = UndefValue::get(F.getReturnType());
160
161 // If we got here, the function returns a constant value. Loop over all
162 // users, replacing any uses of the return value with the returned constant.
163 bool ReplacedAllUsers = true;
164 bool MadeChange = false;
165 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
166 if (!isa<Instruction>(*I))
167 ReplacedAllUsers = false;
168 else {
169 CallSite CS = CallSite::get(cast<Instruction>(*I));
Misha Brukmanfd939082005-04-21 23:48:37 +0000170 if (CS.getInstruction() == 0 ||
Chris Lattnera990d942004-11-14 06:10:11 +0000171 CS.getCalledFunction() != &F) {
172 ReplacedAllUsers = false;
173 } else {
174 if (!CS.getInstruction()->use_empty()) {
175 CS.getInstruction()->replaceAllUsesWith(RetVal);
176 MadeChange = true;
177 }
178 }
179 }
180
181 // If we replace all users with the returned constant, and there can be no
182 // other callers of the function, replace the constant being returned in the
183 // function with an undef value.
184 if (ReplacedAllUsers && F.hasInternalLinkage() && !isa<UndefValue>(RetVal)) {
185 Value *RV = UndefValue::get(RetVal->getType());
186 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner2ffa47b2004-12-11 17:00:14 +0000187 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
188 if (RI->getOperand(0) != RV) {
189 RI->setOperand(0, RV);
190 MadeChange = true;
191 }
192 }
Chris Lattnera990d942004-11-14 06:10:11 +0000193 }
194
195 if (MadeChange) ++NumReturnValProped;
Chris Lattnera990d942004-11-14 06:10:11 +0000196 return MadeChange;
197}