From e1368aef23e688cbf8f8ff455ce2a3680e24a285 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 27 May 2004 17:30:27 +0000 Subject: [PATCH] Fix InstCombine/load.ll & PR347. This code hadn't been updated after the "structs with more than 256 elements" related changes to the GEP instruction. Also it was not handling the ConstantAggregateZero class. Now it does! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13834 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 6d39c50e3fb..532747b0336 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2852,19 +2852,31 @@ static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { // Loop over all of the operands, tracking down which value we are // addressing... - for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) - if (ConstantUInt *CU = dyn_cast(CE->getOperand(i))) { - ConstantStruct *CS = dyn_cast(C); - if (CS == 0) return 0; - if (CU->getValue() >= CS->getValues().size()) return 0; - C = cast(CS->getValues()[CU->getValue()]); - } else if (ConstantSInt *CS = dyn_cast(CE->getOperand(i))) { - ConstantArray *CA = dyn_cast(C); - if (CA == 0) return 0; - if ((uint64_t)CS->getValue() >= CA->getValues().size()) return 0; - C = cast(CA->getValues()[CS->getValue()]); - } else + gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); + for (++I; I != E; ++I) + if (const StructType *STy = dyn_cast(*I)) { + ConstantUInt *CU = cast(I.getOperand()); + assert(CU->getValue() < STy->getNumElements() && + "Struct index out of range!"); + if (ConstantStruct *CS = dyn_cast(C)) { + C = cast(CS->getValues()[CU->getValue()]); + } else if (isa(C)) { + C = Constant::getNullValue(STy->getElementType(CU->getValue())); + } else { + return 0; + } + } else if (ConstantInt *CI = dyn_cast(I.getOperand())) { + const ArrayType *ATy = cast(*I); + if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0; + if (ConstantArray *CA = dyn_cast(C)) + C = cast(CA->getValues()[CI->getRawValue()]); + else if (isa(C)) + C = Constant::getNullValue(ATy->getElementType()); + else + return 0; + } else { return 0; + } return C; } -- 2.34.1