2003-10-23 16:52:27 +00:00
|
|
|
//===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-23 16:52:27 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-23 16:52:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements an _extremely_ simple interprocedural constant
|
|
|
|
// propagation pass. It could certainly be improved in many different ways,
|
|
|
|
// like using a worklist. This pass makes arguments dead, but does not remove
|
|
|
|
// them. The existing dead argument elimination pass should be run after this
|
|
|
|
// to clean up the mess.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2004-11-14 06:10:11 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2003-10-23 16:52:27 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CallSite.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-11-21 21:54:22 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-23 16:52:27 +00:00
|
|
|
namespace {
|
|
|
|
Statistic<> NumArgumentsProped("ipconstprop",
|
|
|
|
"Number of args turned into constants");
|
2004-11-14 06:10:11 +00:00
|
|
|
Statistic<> NumReturnValProped("ipconstprop",
|
|
|
|
"Number of return values turned into constants");
|
2003-10-23 16:52:27 +00:00
|
|
|
|
|
|
|
/// IPCP - The interprocedural constant propagation pass
|
|
|
|
///
|
2004-09-20 04:48:05 +00:00
|
|
|
struct IPCP : public ModulePass {
|
|
|
|
bool runOnModule(Module &M);
|
2003-10-23 16:52:27 +00:00
|
|
|
private:
|
2004-11-14 06:10:11 +00:00
|
|
|
bool PropagateConstantsIntoArguments(Function &F);
|
|
|
|
bool PropagateConstantReturn(Function &F);
|
2003-10-23 16:52:27 +00:00
|
|
|
};
|
|
|
|
RegisterOpt<IPCP> X("ipconstprop", "Interprocedural constant propagation");
|
|
|
|
}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
|
2003-10-23 16:52:27 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool IPCP::runOnModule(Module &M) {
|
2003-10-23 16:52:27 +00:00
|
|
|
bool Changed = false;
|
2003-10-27 21:09:00 +00:00
|
|
|
bool LocalChange = true;
|
|
|
|
|
|
|
|
// FIXME: instead of using smart algorithms, we just iterate until we stop
|
|
|
|
// making changes.
|
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2004-11-14 06:10:11 +00:00
|
|
|
if (!I->isExternal()) {
|
|
|
|
// Delete any klingons.
|
|
|
|
I->removeDeadConstantUsers();
|
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
LocalChange |= PropagateConstantsIntoArguments(*I);
|
|
|
|
Changed |= PropagateConstantReturn(*I);
|
|
|
|
}
|
2003-10-27 21:09:00 +00:00
|
|
|
Changed |= LocalChange;
|
|
|
|
}
|
2003-10-23 16:52:27 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2004-11-14 06:10:11 +00:00
|
|
|
/// PropagateConstantsIntoArguments - Look at all uses of the specified
|
|
|
|
/// function. If all uses are direct call sites, and all pass a particular
|
|
|
|
/// constant in for an argument, propagate that constant in as the argument.
|
2003-10-23 16:52:27 +00:00
|
|
|
///
|
2004-11-14 06:10:11 +00:00
|
|
|
bool IPCP::PropagateConstantsIntoArguments(Function &F) {
|
2005-03-15 04:54:21 +00:00
|
|
|
if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
|
2003-10-23 16:52:27 +00:00
|
|
|
|
|
|
|
std::vector<std::pair<Constant*, bool> > ArgumentConstants;
|
2005-03-15 04:54:21 +00:00
|
|
|
ArgumentConstants.resize(F.arg_size());
|
2003-10-23 16:52:27 +00:00
|
|
|
|
|
|
|
unsigned NumNonconstant = 0;
|
|
|
|
|
|
|
|
for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
|
|
|
|
if (!isa<Instruction>(*I))
|
|
|
|
return false; // Used by a non-instruction, do not transform
|
|
|
|
else {
|
|
|
|
CallSite CS = CallSite::get(cast<Instruction>(*I));
|
2005-04-21 23:48:37 +00:00
|
|
|
if (CS.getInstruction() == 0 ||
|
2003-10-23 16:52:27 +00:00
|
|
|
CS.getCalledFunction() != &F)
|
|
|
|
return false; // Not a direct call site?
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-10-23 16:52:27 +00:00
|
|
|
// Check out all of the potentially constant arguments
|
|
|
|
CallSite::arg_iterator AI = CS.arg_begin();
|
2005-03-15 04:54:21 +00:00
|
|
|
Function::arg_iterator Arg = F.arg_begin();
|
2004-11-10 19:43:59 +00:00
|
|
|
for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
|
|
|
|
++i, ++AI, ++Arg) {
|
2003-10-23 16:52:27 +00:00
|
|
|
if (*AI == &F) return false; // Passes the function into itself
|
|
|
|
|
|
|
|
if (!ArgumentConstants[i].second) {
|
2004-07-18 08:31:18 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(*AI)) {
|
2003-10-23 16:52:27 +00:00
|
|
|
if (!ArgumentConstants[i].first)
|
|
|
|
ArgumentConstants[i].first = C;
|
|
|
|
else if (ArgumentConstants[i].first != C) {
|
|
|
|
// Became non-constant
|
|
|
|
ArgumentConstants[i].second = true;
|
|
|
|
++NumNonconstant;
|
|
|
|
if (NumNonconstant == ArgumentConstants.size()) return false;
|
|
|
|
}
|
2004-11-10 19:43:59 +00:00
|
|
|
} else if (*AI != &*Arg) { // Ignore recursive calls with same arg
|
2003-10-23 16:52:27 +00:00
|
|
|
// This is not a constant argument. Mark the argument as
|
|
|
|
// non-constant.
|
|
|
|
ArgumentConstants[i].second = true;
|
|
|
|
++NumNonconstant;
|
|
|
|
if (NumNonconstant == ArgumentConstants.size()) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got to this point, there is a constant argument!
|
|
|
|
assert(NumNonconstant != ArgumentConstants.size());
|
2005-03-15 04:54:21 +00:00
|
|
|
Function::arg_iterator AI = F.arg_begin();
|
2003-10-27 21:09:00 +00:00
|
|
|
bool MadeChange = false;
|
2003-10-23 16:52:27 +00:00
|
|
|
for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI)
|
|
|
|
// Do we have a constant argument!?
|
2004-11-11 07:47:54 +00:00
|
|
|
if (!ArgumentConstants[i].second && !AI->use_empty()) {
|
2003-10-23 18:49:23 +00:00
|
|
|
Value *V = ArgumentConstants[i].first;
|
2004-11-11 07:46:29 +00:00
|
|
|
if (V == 0) V = UndefValue::get(AI->getType());
|
2003-10-23 18:49:23 +00:00
|
|
|
AI->replaceAllUsesWith(V);
|
2003-10-23 16:52:27 +00:00
|
|
|
++NumArgumentsProped;
|
2003-10-27 21:09:00 +00:00
|
|
|
MadeChange = true;
|
2003-10-23 16:52:27 +00:00
|
|
|
}
|
2003-10-27 21:09:00 +00:00
|
|
|
return MadeChange;
|
2003-10-23 16:52:27 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-11-14 06:10:11 +00:00
|
|
|
|
|
|
|
// Check to see if this function returns a constant. If so, replace all callers
|
|
|
|
// that user the return value with the returned valued. If we can replace ALL
|
|
|
|
// callers,
|
|
|
|
bool IPCP::PropagateConstantReturn(Function &F) {
|
|
|
|
if (F.getReturnType() == Type::VoidTy)
|
|
|
|
return false; // No return value.
|
|
|
|
|
|
|
|
// Check to see if this function returns a constant.
|
|
|
|
Value *RetVal = 0;
|
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
|
|
|
|
if (isa<UndefValue>(RI->getOperand(0))) {
|
|
|
|
// Ignore.
|
|
|
|
} else if (Constant *C = dyn_cast<Constant>(RI->getOperand(0))) {
|
|
|
|
if (RetVal == 0)
|
|
|
|
RetVal = C;
|
|
|
|
else if (RetVal != C)
|
|
|
|
return false; // Does not return the same constant.
|
|
|
|
} else {
|
|
|
|
return false; // Does not return a constant.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RetVal == 0) RetVal = UndefValue::get(F.getReturnType());
|
|
|
|
|
|
|
|
// If we got here, the function returns a constant value. Loop over all
|
|
|
|
// users, replacing any uses of the return value with the returned constant.
|
|
|
|
bool ReplacedAllUsers = true;
|
|
|
|
bool MadeChange = false;
|
|
|
|
for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I)
|
|
|
|
if (!isa<Instruction>(*I))
|
|
|
|
ReplacedAllUsers = false;
|
|
|
|
else {
|
|
|
|
CallSite CS = CallSite::get(cast<Instruction>(*I));
|
2005-04-21 23:48:37 +00:00
|
|
|
if (CS.getInstruction() == 0 ||
|
2004-11-14 06:10:11 +00:00
|
|
|
CS.getCalledFunction() != &F) {
|
|
|
|
ReplacedAllUsers = false;
|
|
|
|
} else {
|
|
|
|
if (!CS.getInstruction()->use_empty()) {
|
|
|
|
CS.getInstruction()->replaceAllUsesWith(RetVal);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we replace all users with the returned constant, and there can be no
|
|
|
|
// other callers of the function, replace the constant being returned in the
|
|
|
|
// function with an undef value.
|
|
|
|
if (ReplacedAllUsers && F.hasInternalLinkage() && !isa<UndefValue>(RetVal)) {
|
|
|
|
Value *RV = UndefValue::get(RetVal->getType());
|
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
2004-12-11 17:00:14 +00:00
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
|
if (RI->getOperand(0) != RV) {
|
|
|
|
RI->setOperand(0, RV);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
2004-11-14 06:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MadeChange) ++NumReturnValProped;
|
|
|
|
return MadeChange;
|
|
|
|
}
|