APInt-ify the gep scaling code, so that it correctly handles the case where

the scale overflows pointer-sized arithmetic. This fixes PR5281.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84954 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-10-23 17:57:43 +00:00
parent 934af9cfe0
commit 7abbd04e90
2 changed files with 17 additions and 4 deletions

View File

@ -2666,7 +2666,8 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) {
} }
// N = N + Idx * ElementSize; // N = N + Idx * ElementSize;
uint64_t ElementSize = TD->getTypeAllocSize(Ty); APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
TD->getTypeAllocSize(Ty));
SDValue IdxN = getValue(Idx); SDValue IdxN = getValue(Idx);
// If the index is smaller or larger than intptr_t, truncate or extend // If the index is smaller or larger than intptr_t, truncate or extend
@ -2676,13 +2677,13 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) {
// If this is a multiply by a power of two, turn it into a shl // If this is a multiply by a power of two, turn it into a shl
// immediately. This is a very common case. // immediately. This is a very common case.
if (ElementSize != 1) { if (ElementSize != 1) {
if (isPowerOf2_64(ElementSize)) { if (ElementSize.isPowerOf2()) {
unsigned Amt = Log2_64(ElementSize); unsigned Amt = ElementSize.logBase2();
IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
N.getValueType(), IdxN, N.getValueType(), IdxN,
DAG.getConstant(Amt, TLI.getPointerTy())); DAG.getConstant(Amt, TLI.getPointerTy()));
} else { } else {
SDValue Scale = DAG.getIntPtrConstant(ElementSize); SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
N.getValueType(), IdxN, Scale); N.getValueType(), IdxN, Scale);
} }

View File

@ -0,0 +1,12 @@
; RUN: llc < %s -march=x86 | FileCheck %s
; PR5281
; After scaling, this type doesn't fit in memory. Codegen should generate
; correct addressing still.
; CHECK: shll $2, %edx
define fastcc i32* @_ada_smkr([2147483647 x i32]* %u, i32 %t) nounwind {
%x = getelementptr [2147483647 x i32]* %u, i32 %t, i32 0
ret i32* %x
}