summaryrefslogtreecommitdiff
path: root/lib/Transforms/IPO/IPConstantPropagation.cpp
diff options
context:
space:
mode:
authorDevang Patel <dpatel@apple.com>2008-03-20 18:30:32 +0000
committerDevang Patel <dpatel@apple.com>2008-03-20 18:30:32 +0000
commit488b678a4915eae8e878f5776549a19493edecf7 (patch)
tree6c21b58e80bea495ef528e77bdb9d9b197443234 /lib/Transforms/IPO/IPConstantPropagation.cpp
parent69cf031ee33fc564c3888694cb1cd8ab5dae9fd2 (diff)
downloadllvm-488b678a4915eae8e878f5776549a19493edecf7.tar.gz
llvm-488b678a4915eae8e878f5776549a19493edecf7.tar.bz2
llvm-488b678a4915eae8e878f5776549a19493edecf7.tar.xz
Incorporate feedback.
- Fix loop nest. - Use RetVals.size() - Check for null return value. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48605 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/IPO/IPConstantPropagation.cpp')
-rw-r--r--lib/Transforms/IPO/IPConstantPropagation.cpp48
1 files changed, 24 insertions, 24 deletions
diff --git a/lib/Transforms/IPO/IPConstantPropagation.cpp b/lib/Transforms/IPO/IPConstantPropagation.cpp
index 6779ef2f0f..8632cd1f25 100644
--- a/lib/Transforms/IPO/IPConstantPropagation.cpp
+++ b/lib/Transforms/IPO/IPConstantPropagation.cpp
@@ -144,19 +144,17 @@ bool IPCP::PropagateConstantReturn(Function &F) {
// Check to see if this function returns a constant.
SmallVector<Value *,4> RetVals;
- unsigned N = 0;
const StructType *STy = dyn_cast<StructType>(F.getReturnType());
if (STy)
- N = STy->getNumElements();
+ RetVals.assign(STy->getNumElements(), 0);
else
- N = 1;
- for (unsigned i = 0; i < N; ++i)
RetVals.push_back(0);
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
- assert (N == RI->getNumOperands() && "Invalid ReturnInst operands!");
- for (unsigned i = 0; i < N; ++i) {
+ unsigned RetValsSize = RetVals.size();
+ assert (RetValsSize == RI->getNumOperands() && "Invalid ReturnInst operands!");
+ for (unsigned i = 0; i < RetValsSize; ++i) {
if (isa<UndefValue>(RI->getOperand(i))) {
// Ignore
} else if (Constant *C = dyn_cast<Constant>(RI->getOperand(i))) {
@@ -171,15 +169,14 @@ bool IPCP::PropagateConstantReturn(Function &F) {
}
}
- if (N == 1) {
- if (RetVals[0] == 0)
- RetVals[0] = UndefValue::get(F.getReturnType());
- } else {
- for (unsigned i = 0; i < N; ++i) {
- Value *RetVal = RetVals[i];
- if (RetVal == 0)
+ if (STy) {
+ for (unsigned i = 0, e = RetVals.size(); i < e; ++i)
+ if (RetVals[i] == 0)
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
@@ -197,14 +194,16 @@ bool IPCP::PropagateConstantReturn(Function &F) {
} else {
Instruction *Call = CS.getInstruction();
if (!Call->use_empty()) {
- if (N == 1)
+ if (RetVals.size() == 1)
Call->replaceAllUsesWith(RetVals[0]);
else {
for(Value::use_iterator CUI = Call->use_begin(), CUE = Call->use_end();
CUI != CUE; ++CUI) {
GetResultInst *GR = cast<GetResultInst>(CUI);
- GR->replaceAllUsesWith(RetVals[GR->getIndex()]);
- GR->eraseFromParent();
+ if (RetVals[GR->getIndex()]) {
+ GR->replaceAllUsesWith(RetVals[GR->getIndex()]);
+ GR->eraseFromParent();
+ }
}
}
MadeChange = true;
@@ -216,18 +215,19 @@ bool IPCP::PropagateConstantReturn(Function &F) {
// other callers of the function, replace the constant being returned in the
// function with an undef value.
if (ReplacedAllUsers && F.hasInternalLinkage()) {
- for (unsigned i = 0; i < N; ++i) {
- Value *RetVal = RetVals[i];
- if (isa<UndefValue>(RetVal))
- continue;
- Value *RV = UndefValue::get(RetVal->getType());
- for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
- if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
+ for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
+ if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
+ for (unsigned i = 0, e = RetVals.size(); i < e; ++i) {
+ Value *RetVal = RetVals[i];
+ if (isa<UndefValue>(RetVal))
+ continue;
+ Value *RV = UndefValue::get(RetVal->getType());
if (RI->getOperand(i) != RV) {
RI->setOperand(i, RV);
MadeChange = true;
}
}
+ }
}
}