From 474e488c06fb59b53f597515c01aee140707451f Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 10 Mar 2010 19:31:51 +0000 Subject: Constant-fold GEP-of-GEP into a single GEP. llvm-svn: 98178 --- llvm/lib/Analysis/ConstantFolding.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'llvm/lib/Analysis/ConstantFolding.cpp') diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 114db2d3705..62020afb723 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -589,6 +589,17 @@ static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps, APInt Offset = APInt(BitWidth, TD->getIndexedOffset(Ptr->getType(), (Value**)Ops+1, NumOps-1)); + + // If this is a GEP of a GEP, fold it all into a single GEP. + while (GEPOperator *GEP = dyn_cast(Ptr)) { + SmallVector NestedOps(GEP->op_begin()+1, GEP->op_end()); + Ptr = cast(GEP->getOperand(0)); + Offset += APInt(BitWidth, + TD->getIndexedOffset(Ptr->getType(), + (Value**)NestedOps.data(), + NestedOps.size())); + } + // If the base value for this address is a literal integer value, fold the // getelementptr to the resulting integer value casted to the pointer type. if (BaseIsInt) { -- cgit v1.2.3