[llvm-commits] [llvm] r79639 - in /llvm/trunk: lib/Analysis/ConstantFolding.cpp test/Transforms/InstCombine/constant-fold-gep-overindex.ll

Dan Gohman gohman at apple.com
Fri Aug 21 09:52:54 PDT 2009


Author: djg
Date: Fri Aug 21 11:52:54 2009
New Revision: 79639

URL: http://llvm.org/viewvc/llvm-project?rev=79639&view=rev
Log:
Change getelementptr folding to use APInt instead of uint64_t for
offset computations. This fixes a truncation bug on targets that
don't have 64-bit pointers.

Modified:
    llvm/trunk/lib/Analysis/ConstantFolding.cpp
    llvm/trunk/test/Transforms/InstCombine/constant-fold-gep-overindex.ll

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=79639&r1=79638&r2=79639&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Fri Aug 21 11:52:54 2009
@@ -129,8 +129,9 @@
   Constant *Ptr = Ops[0];
   if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized())
     return 0;
-  
-  uint64_t BasePtr = 0;
+
+  unsigned BitWidth = TD->getTypeSizeInBits(TD->getIntPtrType(Context));
+  APInt BasePtr(BitWidth, 0);
   bool BaseIsInt = true;
   if (!Ptr->isNullValue()) {
     // If this is a inttoptr from a constant int, we can fold this as the base,
@@ -138,7 +139,7 @@
     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
       if (CE->getOpcode() == Instruction::IntToPtr)
         if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
-          BasePtr = Base->getZExtValue();
+          BasePtr = Base->getValue();
     
     if (BasePtr == 0)
       BaseIsInt = false;
@@ -150,12 +151,13 @@
     if (!isa<ConstantInt>(Ops[i]))
       return 0;
   
-  uint64_t Offset = TD->getIndexedOffset(Ptr->getType(),
-                                         (Value**)Ops+1, NumOps-1);
+  APInt Offset = APInt(BitWidth,
+                       TD->getIndexedOffset(Ptr->getType(),
+                                            (Value**)Ops+1, NumOps-1));
   // 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) {
-    Constant *C = ConstantInt::get(TD->getIntPtrType(Context), Offset+BasePtr);
+    Constant *C = ConstantInt::get(Context, Offset+BasePtr);
     return ConstantExpr::getIntToPtr(C, ResultTy);
   }
 
@@ -171,19 +173,21 @@
       if (isa<PointerType>(ATy) && ATy != Ptr->getType())
         break;
       // Determine which element of the array the offset points into.
-      uint64_t ElemSize = TD->getTypeAllocSize(ATy->getElementType());
+      APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
       if (ElemSize == 0)
         return 0;
-      uint64_t NewIdx = Offset / ElemSize;
+      APInt NewIdx = Offset.udiv(ElemSize);
       Offset -= NewIdx * ElemSize;
       NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Context), NewIdx));
       Ty = ATy->getElementType();
     } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
-      // Determine which field of the struct the offset points into.
+      // Determine which field of the struct the offset points into. The
+      // getZExtValue is at least as safe as the StructLayout API because we
+      // know the offset is within the struct at this point.
       const StructLayout &SL = *TD->getStructLayout(STy);
-      unsigned ElIdx = SL.getElementContainingOffset(Offset);
+      unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
       NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), ElIdx));
-      Offset -= SL.getElementOffset(ElIdx);
+      Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
       Ty = STy->getTypeAtIndex(ElIdx);
     } else {
       // We've reached some non-indexable type.

Modified: llvm/trunk/test/Transforms/InstCombine/constant-fold-gep-overindex.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/constant-fold-gep-overindex.ll?rev=79639&r1=79638&r2=79639&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/constant-fold-gep-overindex.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/constant-fold-gep-overindex.ll Fri Aug 21 11:52:54 2009
@@ -1,17 +1,33 @@
-; RUN: llvm-as < %s | opt -instcombine
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | FileCheck %s
+
 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
 target triple = "i386-apple-darwin10.0"
 
 %0 = type { i8*, [19 x i8] }
 %1 = type { i8*, [0 x i8] }
 
+ at array = external global [11 x i8]
+
 @s = external global %0                           ; <%0*> [#uses=1]
 @"\01LC8" = external constant [17 x i8]           ; <[17 x i8]*> [#uses=1]
 
+; Instcombine should be able to fold this getelementptr.
+
 define i32 @main() nounwind {
-entry:
-  %0 = call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0), i8* undef, i8* getelementptr (%1* bitcast (%0* @s to %1*), i32 0, i32 1, i32 0)) nounwind ; <i32> [#uses=0]
+; CHECK: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @"\01LC8", i32 0, i32 0), i8* undef, i8* bitcast (i8** getelementptr (%1* bitcast (%0* @s to %1*), i32 1, i32 0) to i8*)) nounwind
+
+  call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0), i8* undef, i8* getelementptr (%1* bitcast (%0* @s to %1*), i32 0, i32 1, i32 0)) nounwind ; <i32> [#uses=0]
   ret i32 0
 }
 
+; Instcombine should constant-fold the GEP so that indices that have
+; static array extents are within bounds of those array extents.
+; In the below, -1 is not in the range [0,11). After the transformation,
+; the same address is computed, but 3 is in the range of [0,11).
+
+define i8* @foo() nounwind {
+; CHECK: ret i8* getelementptr ([11 x i8]* @array, i32 390451572, i32 3)
+  ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
+}
+
 declare i32 @printf(i8*, ...) nounwind





More information about the llvm-commits mailing list