forked from OSchip/llvm-project
Incorporate feedback.
- Fix loop nest. - Use RetVals.size() - Check for null return value. llvm-svn: 48605
This commit is contained in:
parent
b81777a354
commit
5ca2ea6479
|
|
@ -144,19 +144,17 @@ bool IPCP::PropagateConstantReturn(Function &F) {
|
||||||
|
|
||||||
// Check to see if this function returns a constant.
|
// Check to see if this function returns a constant.
|
||||||
SmallVector<Value *,4> RetVals;
|
SmallVector<Value *,4> RetVals;
|
||||||
unsigned N = 0;
|
|
||||||
const StructType *STy = dyn_cast<StructType>(F.getReturnType());
|
const StructType *STy = dyn_cast<StructType>(F.getReturnType());
|
||||||
if (STy)
|
if (STy)
|
||||||
N = STy->getNumElements();
|
RetVals.assign(STy->getNumElements(), 0);
|
||||||
else
|
else
|
||||||
N = 1;
|
|
||||||
for (unsigned i = 0; i < N; ++i)
|
|
||||||
RetVals.push_back(0);
|
RetVals.push_back(0);
|
||||||
|
|
||||||
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
||||||
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
||||||
assert (N == RI->getNumOperands() && "Invalid ReturnInst operands!");
|
unsigned RetValsSize = RetVals.size();
|
||||||
for (unsigned i = 0; i < N; ++i) {
|
assert (RetValsSize == RI->getNumOperands() && "Invalid ReturnInst operands!");
|
||||||
|
for (unsigned i = 0; i < RetValsSize; ++i) {
|
||||||
if (isa<UndefValue>(RI->getOperand(i))) {
|
if (isa<UndefValue>(RI->getOperand(i))) {
|
||||||
// Ignore
|
// Ignore
|
||||||
} else if (Constant *C = dyn_cast<Constant>(RI->getOperand(i))) {
|
} else if (Constant *C = dyn_cast<Constant>(RI->getOperand(i))) {
|
||||||
|
|
@ -171,15 +169,14 @@ bool IPCP::PropagateConstantReturn(Function &F) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (N == 1) {
|
if (STy) {
|
||||||
if (RetVals[0] == 0)
|
for (unsigned i = 0, e = RetVals.size(); i < e; ++i)
|
||||||
RetVals[0] = UndefValue::get(F.getReturnType());
|
if (RetVals[i] == 0)
|
||||||
} else {
|
|
||||||
for (unsigned i = 0; i < N; ++i) {
|
|
||||||
Value *RetVal = RetVals[i];
|
|
||||||
if (RetVal == 0)
|
|
||||||
RetVals[i] = UndefValue::get(STy->getElementType(i));
|
RetVals[i] = UndefValue::get(STy->getElementType(i));
|
||||||
}
|
} else {
|
||||||
|
if (RetVals.size() == 1)
|
||||||
|
if (RetVals[0] == 0)
|
||||||
|
RetVals[0] = UndefValue::get(F.getReturnType());
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we got here, the function returns a constant value. Loop over all
|
// If we got here, the function returns a constant value. Loop over all
|
||||||
|
|
@ -197,14 +194,16 @@ bool IPCP::PropagateConstantReturn(Function &F) {
|
||||||
} else {
|
} else {
|
||||||
Instruction *Call = CS.getInstruction();
|
Instruction *Call = CS.getInstruction();
|
||||||
if (!Call->use_empty()) {
|
if (!Call->use_empty()) {
|
||||||
if (N == 1)
|
if (RetVals.size() == 1)
|
||||||
Call->replaceAllUsesWith(RetVals[0]);
|
Call->replaceAllUsesWith(RetVals[0]);
|
||||||
else {
|
else {
|
||||||
for(Value::use_iterator CUI = Call->use_begin(), CUE = Call->use_end();
|
for(Value::use_iterator CUI = Call->use_begin(), CUE = Call->use_end();
|
||||||
CUI != CUE; ++CUI) {
|
CUI != CUE; ++CUI) {
|
||||||
GetResultInst *GR = cast<GetResultInst>(CUI);
|
GetResultInst *GR = cast<GetResultInst>(CUI);
|
||||||
GR->replaceAllUsesWith(RetVals[GR->getIndex()]);
|
if (RetVals[GR->getIndex()]) {
|
||||||
GR->eraseFromParent();
|
GR->replaceAllUsesWith(RetVals[GR->getIndex()]);
|
||||||
|
GR->eraseFromParent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MadeChange = true;
|
MadeChange = true;
|
||||||
|
|
@ -216,18 +215,19 @@ bool IPCP::PropagateConstantReturn(Function &F) {
|
||||||
// other callers of the function, replace the constant being returned in the
|
// other callers of the function, replace the constant being returned in the
|
||||||
// function with an undef value.
|
// function with an undef value.
|
||||||
if (ReplacedAllUsers && F.hasInternalLinkage()) {
|
if (ReplacedAllUsers && F.hasInternalLinkage()) {
|
||||||
for (unsigned i = 0; i < N; ++i) {
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
|
||||||
Value *RetVal = RetVals[i];
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
||||||
if (isa<UndefValue>(RetVal))
|
for (unsigned i = 0, e = RetVals.size(); i < e; ++i) {
|
||||||
continue;
|
Value *RetVal = RetVals[i];
|
||||||
Value *RV = UndefValue::get(RetVal->getType());
|
if (isa<UndefValue>(RetVal))
|
||||||
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
continue;
|
||||||
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
Value *RV = UndefValue::get(RetVal->getType());
|
||||||
if (RI->getOperand(i) != RV) {
|
if (RI->getOperand(i) != RV) {
|
||||||
RI->setOperand(i, RV);
|
RI->setOperand(i, RV);
|
||||||
MadeChange = true;
|
MadeChange = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue