From d10376bee5138d30a290f56ad84a0b26a9752bcc Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 27 May 2003 16:09:27 +0000 Subject: [PATCH] * Actually USE the statistic that we made * Implement SRoA for arrays git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6349 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/ScalarReplAggregates.cpp | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index 0ca7e69d19c..c540bba2703 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -52,12 +52,15 @@ bool SROA::runOnFunction(Function &F) { WorkList.pop_back(); // We cannot transform the allocation instruction if it is an array - // allocation, and an allocation of a scalar value cannot be decomposed + // allocation (allocations OF arrays are ok though), and an allocation of a + // scalar value cannot be decomposed at all. + // if (AI->isArrayAllocation() || - (!isa(AI->getAllocatedType()) /*&& - !isa(AI->getAllocatedType())*/ - )) continue; - + (!isa(AI->getAllocatedType()) && + !isa(AI->getAllocatedType()))) continue; + + const ArrayType *AT = dyn_cast(AI->getAllocatedType()); + // Loop over the use list of the alloca. We can only transform it if there // are only getelementptr instructions (with a zero first index) and free // instructions. @@ -77,6 +80,18 @@ bool SROA::runOnFunction(Function &F) { CannotTransform = true; break; } + + // If this is an array access, check to make sure that index falls + // within the array. If not, something funny is going on, so we won't + // do the optimization. + if (AT && cast(GEPI->getOperand(2))->getValue() >= + AT->getNumElements()) { + DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: " + << User); + CannotTransform = true; + break; + } + } else { DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: " << User); @@ -100,7 +115,6 @@ bool SROA::runOnFunction(Function &F) { WorkList.push_back(NA); // Add to worklist for recursive processing } } else { - const ArrayType *AT = cast(AI->getAllocatedType()); ElementAllocas.reserve(AT->getNumElements()); const Type *ElTy = AT->getElementType(); for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { @@ -158,6 +172,7 @@ bool SROA::runOnFunction(Function &F) { // Finally, delete the Alloca instruction AI->getParent()->getInstList().erase(AI); + NumReplaced++; } return Changed; -- 2.40.0