From f488b358263adee8ad47c1b03fbfbb53a71d4996 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 24 Jan 2012 05:43:50 +0000 Subject: [PATCH] Split the interesting bits of ConstantFoldLoadThroughGEPConstantExpr out into a new ConstantFoldLoadThroughGEPIndices (more useful) function and rewrite it to be simpler, more efficient, and to handle the new ConstantDataSequential type. Enhance ConstantFoldLoadFromConstPtr to handle ConstantDataSequential. llvm-svn: 148786 --- llvm/include/llvm/Analysis/ConstantFolding.h | 9 +- llvm/lib/Analysis/ConstantFolding.cpp | 99 +++++++++++--------- 2 files changed, 64 insertions(+), 44 deletions(-) diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h index 67bc2b32ac69..2fdef5f0836e 100644 --- a/llvm/include/llvm/Analysis/ConstantFolding.h +++ b/llvm/include/llvm/Analysis/ConstantFolding.h @@ -81,7 +81,14 @@ Constant *ConstantFoldLoadFromConstPtr(Constant *C, const TargetData *TD = 0); /// getelementptr constantexpr, return the constant value being addressed by the /// constant expression, or null if something is funny and we can't decide. Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE); - + +/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr +/// indices (with an *implied* zero pointer index that is not in the list), +/// return the constant value being addressed by a virtual load, or null if +/// something is funny and we can't decide. +Constant *ConstantFoldLoadThroughGEPIndices(Constant *C, + ArrayRef Indices); + /// canConstantFoldCallTo - Return true if its even possible to fold a call to /// the specified function. bool canConstantFoldCallTo(const Function *F); diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index dba6de35df84..7ee716fdf629 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -273,7 +273,7 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, } return false; } - + if (ConstantStruct *CS = dyn_cast(C)) { const StructLayout *SL = TD.getStructLayout(CS->getType()); unsigned Index = SL->getElementContainingOffset(ByteOffset); @@ -347,6 +347,24 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, return true; } + if (ConstantDataSequential *CDS = dyn_cast(C)) { + uint64_t EltSize = CDS->getElementByteSize(); + uint64_t Index = ByteOffset / EltSize; + uint64_t Offset = ByteOffset - Index * EltSize; + for (; Index != CDS->getType()->getNumElements(); ++Index) { + if (!ReadDataFromGlobal(CDS->getElementAsConstant(Index), Offset, CurPtr, + BytesLeft, TD)) + return false; + if (EltSize >= BytesLeft) + return true; + + Offset = 0; + BytesLeft -= EltSize; + CurPtr += EltSize; + } + return true; + } + if (ConstantExpr *CE = dyn_cast(C)) { if (CE->getOpcode() == Instruction::IntToPtr && CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext())) @@ -990,56 +1008,51 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, /// constant expression, or null if something is funny and we can't decide. Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE) { - if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) + if (!CE->getOperand(1)->isNullValue()) return 0; // Do not allow stepping over the value! + SmallVector Indices(CE->getNumOperands()-2); + for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) + Indices[i-2] = CE->getOperand(i); + return ConstantFoldLoadThroughGEPIndices(C, Indices); +} + +/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr +/// indices (with an *implied* zero pointer index that is not in the list), +/// return the constant value being addressed by a virtual load, or null if +/// something is funny and we can't decide. +Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C, + ArrayRef Indices) { // Loop over all of the operands, tracking down which value we are - // addressing... - gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); - for (++I; I != E; ++I) - if (StructType *STy = dyn_cast(*I)) { - ConstantInt *CU = cast(I.getOperand()); - assert(CU->getZExtValue() < STy->getNumElements() && - "Struct index out of range!"); - unsigned El = (unsigned)CU->getZExtValue(); - if (ConstantStruct *CS = dyn_cast(C)) { - C = CS->getOperand(El); - } else if (isa(C)) { - C = Constant::getNullValue(STy->getElementType(El)); - } else if (isa(C)) { - C = UndefValue::get(STy->getElementType(El)); - } else { + // addressing. + for (unsigned i = 0, e = Indices.size(); i != e; ++i) { + ConstantInt *Idx = dyn_cast(Indices[i]); + if (Idx == 0) return 0; + + uint64_t IdxVal = Idx->getZExtValue(); + + if (ConstantStruct *CS = dyn_cast(C)) { + C = CS->getOperand(IdxVal); + } else if (ConstantAggregateZero *CAZ = dyn_cast(C)){ + C = CAZ->getElementValue(Idx); + } else if (UndefValue *UV = dyn_cast(C)) { + C = UV->getElementValue(Idx); + } else if (ConstantArray *CA = dyn_cast(C)) { + if (IdxVal >= CA->getType()->getNumElements()) return 0; - } - } else if (ConstantInt *CI = dyn_cast(I.getOperand())) { - if (ArrayType *ATy = dyn_cast(*I)) { - if (CI->getZExtValue() >= ATy->getNumElements()) - return 0; - if (ConstantArray *CA = dyn_cast(C)) - C = CA->getOperand(CI->getZExtValue()); - else if (isa(C)) - C = Constant::getNullValue(ATy->getElementType()); - else if (isa(C)) - C = UndefValue::get(ATy->getElementType()); - else - return 0; - } else if (VectorType *VTy = dyn_cast(*I)) { - if (CI->getZExtValue() >= VTy->getNumElements()) - return 0; - if (ConstantVector *CP = dyn_cast(C)) - C = CP->getOperand(CI->getZExtValue()); - else if (isa(C)) - C = Constant::getNullValue(VTy->getElementType()); - else if (isa(C)) - C = UndefValue::get(VTy->getElementType()); - else - return 0; - } else { + C = CA->getOperand(IdxVal); + } else if (ConstantDataSequential *CDS=dyn_cast(C)){ + if (IdxVal >= CDS->getType()->getNumElements()) return 0; - } + C = CDS->getElementAsConstant(IdxVal); + } else if (ConstantVector *CV = dyn_cast(C)) { + if (IdxVal >= CV->getType()->getNumElements()) + return 0; + C = CV->getOperand(IdxVal); } else { return 0; } + } return C; }