From cc00cc7e0427ab96a049703dd7b941eccdd4e7bd Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Sun, 3 Feb 2013 13:17:11 +0000 Subject: use GEP::accumulateConstantOffset() to replace custom written code to compute GEP offset git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@174279 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ConstantFolding.cpp | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) (limited to 'lib/Analysis/ConstantFolding.cpp') diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 4b4fa5cd4e..400ce72ce6 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -235,38 +235,13 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) - if (CE->getOpcode() == Instruction::GetElementPtr) { - // Cannot compute this if the element type of the pointer is missing size - // info. - if (!cast(CE->getOperand(0)->getType()) - ->getElementType()->isSized()) - return false; - + if (GEPOperator *GEP = dyn_cast(CE)) { // If the base isn't a global+constant, we aren't either. if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD)) return false; // Otherwise, add any offset that our operands provide. - gep_type_iterator GTI = gep_type_begin(CE); - for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end(); - i != e; ++i, ++GTI) { - ConstantInt *CI = dyn_cast(*i); - if (!CI) return false; // Index isn't a simple constant? - if (CI->isZero()) continue; // Not adding anything. - - if (StructType *ST = dyn_cast(*GTI)) { - // N = N + Offset - Offset += - APInt(Offset.getBitWidth(), - TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue())); - } else { - SequentialType *SQT = cast(*GTI); - Offset += APInt(Offset.getBitWidth(), - TD.getTypeAllocSize(SQT->getElementType()) * - CI->getSExtValue()); - } - } - return true; + return GEP->accumulateConstantOffset(TD, Offset); } return false; -- cgit v1.2.3