Constant-fold GEP-of-GEP into a single GEP.
authorDan Gohman <gohman@apple.com>
Wed, 10 Mar 2010 19:31:51 +0000 (19:31 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 10 Mar 2010 19:31:51 +0000 (19:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98178 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ConstantFolding.cpp
test/Other/constant-fold-gep.ll

index 114db2d37050aa194692a2f1f9aa462cc1281085..62020afb723cb6cbccc892cc11208486d651c22d 100644 (file)
@@ -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<GEPOperator>(Ptr)) {
+    SmallVector<Value *, 4> NestedOps(GEP->op_begin()+1, GEP->op_end());
+    Ptr = cast<Constant>(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) {
index 650b4af139e23c4c10c84c1e1e51e881f122cd56..ecef9c48492e8067a52577d4a6e02026248f8496 100644 (file)
 ; PLAIN: %1 = type { double, float, double, double }
 ; PLAIN: %2 = type { i1, i1* }
 ; PLAIN: %3 = type { i64, i64 }
+; PLAIN: %4 = type { i32, i32 }
 ; OPT: %0 = type { i1, double }
 ; OPT: %1 = type { double, float, double, double }
 ; OPT: %2 = type { i1, i1* }
 ; OPT: %3 = type { i64, i64 }
+; OPT: %4 = type { i32, i32 }
 
 ; The automatic constant folder in opt does not have targetdata access, so
 ; it can't fold gep arithmetic, in general. However, the constant folder run
 @N = constant i64* getelementptr ({ i64, i64 }* null, i32 0, i32 1)
 @O = constant i64* getelementptr ([2 x i64]* null, i32 0, i32 1)
 
+; Fold GEP of a GEP. Theoretically some of these cases could be folded
+; without using targetdata, however that's not implemented yet.
+
+; PLAIN: @Z = global i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x %4]* @ext, i64 0, i64 1, i32 0), i64 1)
+; OPT: @Z = global i32* getelementptr (i32* getelementptr inbounds ([3 x %4]* @ext, i64 0, i64 1, i32 0), i64 1)
+; TO: @Z = global i32* getelementptr inbounds ([3 x %0]* @ext, i64 0, i64 1, i32 1)
+
+@ext = external global [3 x { i32, i32 }]
+@Z = global i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 0), i64 1)
+
 ; Duplicate all of the above as function return values rather than
 ; global initializers.
 
@@ -468,3 +480,22 @@ define i64* @fO() nounwind {
   %t = bitcast i64* getelementptr ([2 x i64]* null, i32 0, i32 1) to i64*
   ret i64* %t
 }
+
+; PLAIN: define i32* @fZ() nounwind {
+; PLAIN:   %t = bitcast i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x %4]* @ext, i64 0, i64 1, i32 0), i64 1) to i32*
+; PLAIN:   ret i32* %t
+; PLAIN: }
+; OPT: define i32* @fZ() nounwind {
+; OPT:   ret i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x %4]* @ext, i64 0, i64 1, i32 0), i64 1)
+; OPT: }
+; TO: define i32* @fZ() nounwind {
+; TO:   ret i32* getelementptr inbounds ([3 x %0]* @ext, i64 0, i64 1, i32 1)
+; TO: }
+; SCEV: Classifying expressions for: @fZ
+; SCEV:   %t = bitcast i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x %4]* @ext, i64 0, i64 1, i32 0), i64 1) to i32*
+; SCEV:   -->  ((3 * sizeof(i32)) + @ext)
+
+define i32* @fZ() nounwind {
+  %t = bitcast i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 0), i64 1) to i32*
+  ret i32* %t
+}