Make sure constant subscript is truncated to ptr size if it may not fit.
authorEvan Cheng <evan.cheng@apple.com>
Mon, 9 Feb 2009 20:54:38 +0000 (20:54 +0000)
committerEvan Cheng <evan.cheng@apple.com>
Mon, 9 Feb 2009 20:54:38 +0000 (20:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64163 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
test/CodeGen/X86/negative-subscript.ll [new file with mode: 0644]

index 3c90cd767ce658f777be965159ed8cf70b79c17b..7b613d7f242e4c925dc8b5c9c4d027dca8daf55f 100644 (file)
@@ -2699,8 +2699,15 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) {
         if (CI->getZExtValue() == 0) continue;
         uint64_t Offs =
             TD->getTypePaddedSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
+        SDValue OffsVal = DAG.getConstant(Offs, MVT::i64);
+        unsigned PtrBits = TLI.getPointerTy().getSizeInBits();
+        if (PtrBits < 64)
+          OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), 
+                                TLI.getPointerTy(), OffsVal);
+        else
+          OffsVal = DAG.getIntPtrConstant(Offs);
         N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
-                        DAG.getIntPtrConstant(Offs));
+                        OffsVal);
         continue;
       }
 
diff --git a/test/CodeGen/X86/negative-subscript.ll b/test/CodeGen/X86/negative-subscript.ll
new file mode 100644 (file)
index 0000000..f2bd315
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86
+; rdar://6559995
+
+@a = external global [255 x i8*], align 32
+
+define i32 @main() nounwind {
+entry:
+       store i8* bitcast (i8** getelementptr ([255 x i8*]* @a, i32 0, i32 -2147483624) to i8*), i8** getelementptr ([255 x i8*]* @a, i32 0, i32 16), align 32
+       ret i32 0
+}