simplify code for propagation of constant arguments into

callees.

llvm-svn: 50142
This commit is contained in:
Chris Lattner 2008-04-23 06:16:27 +00:00
parent 5f1802cfdf
commit a82d691caa
1 changed files with 48 additions and 45 deletions

View File

@ -76,61 +76,64 @@ bool IPCP::runOnModule(Module &M) {
bool IPCP::PropagateConstantsIntoArguments(Function &F) { bool IPCP::PropagateConstantsIntoArguments(Function &F) {
if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit. if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
std::vector<std::pair<Constant*, bool> > ArgumentConstants; // For each argument, keep track of its constant value and whether it is a
// constant or not. The bool is driven to true when found to be non-constant.
SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants;
ArgumentConstants.resize(F.arg_size()); ArgumentConstants.resize(F.arg_size());
unsigned NumNonconstant = 0; unsigned NumNonconstant = 0;
for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) {
// Used by a non-instruction, or not the callee of a function, do not
// transform.
if (UI.getOperandNo() != 0 ||
(!isa<CallInst>(*UI) && !isa<InvokeInst>(*UI)))
return false;
CallSite CS = CallSite::get(cast<Instruction>(*UI));
for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) // Check out all of the potentially constant arguments. Note that we don't
if (!isa<Instruction>(*I)) // inspect varargs here.
return false; // Used by a non-instruction, do not transform CallSite::arg_iterator AI = CS.arg_begin();
else { Function::arg_iterator Arg = F.arg_begin();
CallSite CS = CallSite::get(cast<Instruction>(*I)); for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
if (CS.getInstruction() == 0 || ++i, ++AI, ++Arg) {
CS.getCalledFunction() != &F)
return false; // Not a direct call site? // If this argument is known non-constant, ignore it.
if (ArgumentConstants[i].second)
// Check out all of the potentially constant arguments continue;
CallSite::arg_iterator AI = CS.arg_begin();
Function::arg_iterator Arg = F.arg_begin(); Constant *C = dyn_cast<Constant>(*AI);
for (unsigned i = 0, e = ArgumentConstants.size(); i != e; if (C && ArgumentConstants[i].first == 0) {
++i, ++AI, ++Arg) { ArgumentConstants[i].first = C; // First constant seen.
if (*AI == &F) return false; // Passes the function into itself } else if (C && ArgumentConstants[i].first == C) {
// Still the constant value we think it is.
if (!ArgumentConstants[i].second) { } else if (*AI == &*Arg) {
if (Constant *C = dyn_cast<Constant>(*AI)) { // Ignore recursive calls passing argument down.
if (!ArgumentConstants[i].first) } else {
ArgumentConstants[i].first = C; // Argument became non-constant. If all arguments are non-constant now,
else if (ArgumentConstants[i].first != C) { // give up on this function.
// Became non-constant if (++NumNonconstant == ArgumentConstants.size())
ArgumentConstants[i].second = true; return false;
++NumNonconstant; ArgumentConstants[i].second = true;
if (NumNonconstant == ArgumentConstants.size()) return false;
}
} else if (*AI != &*Arg) { // Ignore recursive calls with same arg
// 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! // If we got to this point, there is a constant argument!
assert(NumNonconstant != ArgumentConstants.size()); assert(NumNonconstant != ArgumentConstants.size());
Function::arg_iterator AI = F.arg_begin();
bool MadeChange = false; bool MadeChange = false;
for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) Function::arg_iterator AI = F.arg_begin();
// Do we have a constant argument!? for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
if (!ArgumentConstants[i].second && !AI->use_empty()) { // Do we have a constant argument?
Value *V = ArgumentConstants[i].first; if (ArgumentConstants[i].second || AI->use_empty())
if (V == 0) V = UndefValue::get(AI->getType()); continue;
AI->replaceAllUsesWith(V);
++NumArgumentsProped; Value *V = ArgumentConstants[i].first;
MadeChange = true; if (V == 0) V = UndefValue::get(AI->getType());
} AI->replaceAllUsesWith(V);
++NumArgumentsProped;
MadeChange = true;
}
return MadeChange; return MadeChange;
} }