From: Evan Cheng Date: Mon, 9 Feb 2009 20:54:38 +0000 (+0000) Subject: Make sure constant subscript is truncated to ptr size if it may not fit. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=b1032a8a2f322e031f7c0947c3fb60fe9e8a10e6;p=oota-llvm.git Make sure constant subscript is truncated to ptr size if it may not fit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64163 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp index 3c90cd767ce..7b613d7f242 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp @@ -2699,8 +2699,15 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) { if (CI->getZExtValue() == 0) continue; uint64_t Offs = TD->getTypePaddedSize(Ty)*cast(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 index 00000000000..f2bd315bd86 --- /dev/null +++ b/test/CodeGen/X86/negative-subscript.ll @@ -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 +}