From 488b678a4915eae8e878f5776549a19493edecf7 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Thu, 20 Mar 2008 18:30:32 +0000 Subject: [PATCH] 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 --- lib/Transforms/IPO/IPConstantPropagation.cpp | 48 ++++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/Transforms/IPO/IPConstantPropagation.cpp b/lib/Transforms/IPO/IPConstantPropagation.cpp index 6779ef2f0fa..8632cd1f25a 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 RetVals; - unsigned N = 0; const StructType *STy = dyn_cast(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(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(RI->getOperand(i))) { // Ignore } else if (Constant *C = dyn_cast(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(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(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(BB->getTerminator())) { + for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { + if (ReturnInst *RI = dyn_cast(BB->getTerminator())) { + for (unsigned i = 0, e = RetVals.size(); i < e; ++i) { + Value *RetVal = RetVals[i]; + if (isa(RetVal)) + continue; + Value *RV = UndefValue::get(RetVal->getType()); if (RI->getOperand(i) != RV) { RI->setOperand(i, RV); MadeChange = true; } } + } } }