1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 12:12:47 +01:00

llvm/ConstantFolding.cpp: Make ReadDataFromGlobal() and FoldReinterpretLoadFromConstPtr() Big-endian-aware.

llvm-svn: 167595
This commit is contained in:
NAKAMURA Takumi 2012-11-08 20:34:25 +00:00
parent 9647e19de7
commit cda12da6b9
5 changed files with 131 additions and 52 deletions

View File

@ -292,7 +292,10 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
unsigned IntBytes = unsigned(CI->getBitWidth()/8);
for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8));
int n = ByteOffset;
if (!TD.isLittleEndian())
n = IntBytes - n - 1;
CurPtr[i] = (unsigned char)(Val >> (n * 8));
++ByteOffset;
}
return true;
@ -442,10 +445,19 @@ static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
BytesLoaded, TD))
return 0;
APInt ResultVal = APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1]);
for (unsigned i = 1; i != BytesLoaded; ++i) {
ResultVal <<= 8;
ResultVal |= RawBytes[BytesLoaded-1-i];
APInt ResultVal = APInt(IntType->getBitWidth(), 0);
if (TD.isLittleEndian()) {
ResultVal = RawBytes[BytesLoaded - 1];
for (unsigned i = 1; i != BytesLoaded; ++i) {
ResultVal <<= 8;
ResultVal |= RawBytes[BytesLoaded-1-i];
}
} else {
ResultVal = RawBytes[0];
for (unsigned i = 1; i != BytesLoaded; ++i) {
ResultVal <<= 8;
ResultVal |= RawBytes[i];
}
}
return ConstantInt::get(IntType->getContext(), ResultVal);
@ -521,10 +533,8 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
}
}
// Try hard to fold loads from bitcasted strange and non-type-safe things. We
// currently don't do any of this for big endian systems. It can be
// generalized in the future if someone is interested.
if (TD && TD->isLittleEndian())
// Try hard to fold loads from bitcasted strange and non-type-safe things.
if (TD)
return FoldReinterpretLoadFromConstPtr(CE, *TD);
return 0;
}

View File

@ -1,17 +1,24 @@
; RUN: opt < %s -instcombine -S | FileCheck %s
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
; RUN: opt < %s -default-data-layout="e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64" -instcombine -S | FileCheck %s --check-prefix=LE
; RUN: opt < %s -default-data-layout="E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64" -instcombine -S | FileCheck %s --check-prefix=BE
; {{ 0xDEADBEEF, 0xBA }, 0xCAFEBABE}
@g1 = constant {{i32,i8},i32} {{i32,i8} { i32 -559038737, i8 186 }, i32 -889275714 }
@g2 = constant double 1.0
; { 0x7B, 0x06B1BFF8 }
@g3 = constant {i64, i64} { i64 123, i64 112312312 }
; Simple load
define i32 @test1() {
%r = load i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0)
ret i32 %r
; CHECK: @test1
; CHECK: ret i32 -559038737
; 0xDEADBEEF
; LE: @test1
; LE: ret i32 -559038737
; 0xDEADBEEF
; BE: @test1
; BE: ret i32 -559038737
}
; PR3152
@ -20,8 +27,13 @@ define i16 @test2() {
%r = load i16* bitcast(i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*)
ret i16 %r
; CHECK: @test2
; CHECK: ret i16 -16657
; 0xBEEF
; LE: @test2
; LE: ret i16 -16657
; 0xDEAD
; BE: @test2
; BE: ret i16 -8531
}
; Load of second 16 bits of 32-bit value.
@ -29,16 +41,27 @@ define i16 @test3() {
%r = load i16* getelementptr(i16* bitcast(i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*), i32 1)
ret i16 %r
; CHECK: @test3
; CHECK: ret i16 -8531
; 0xDEAD
; LE: @test3
; LE: ret i16 -8531
; 0xBEEF
; BE: @test3
; BE: ret i16 -16657
}
; Load of 8 bit field + tail padding.
define i16 @test4() {
%r = load i16* getelementptr(i16* bitcast(i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*), i32 2)
ret i16 %r
; CHECK: @test4
; CHECK: ret i16 186
; 0x00BA
; LE: @test4
; LE: ret i16 186
; 0xBA00
; BE: @test4
; BE: ret i16 -17920
}
; Load of double bits.
@ -46,8 +69,13 @@ define i64 @test6() {
%r = load i64* bitcast(double* @g2 to i64*)
ret i64 %r
; CHECK: @test6
; CHECK: ret i64 4607182418800017408
; 0x3FF_0000000000000
; LE: @test6
; LE: ret i64 4607182418800017408
; 0x3FF_0000000000000
; BE: @test6
; BE: ret i64 4607182418800017408
}
; Load of double bits.
@ -55,8 +83,13 @@ define i16 @test7() {
%r = load i16* bitcast(double* @g2 to i16*)
ret i16 %r
; CHECK: @test7
; CHECK: ret i16 0
; 0x0000
; LE: @test7
; LE: ret i16 0
; 0x3FF0
; BE: @test7
; BE: ret i16 16368
}
; Double load.
@ -64,8 +97,11 @@ define double @test8() {
%r = load double* bitcast({{i32,i8},i32}* @g1 to double*)
ret double %r
; CHECK: @test8
; CHECK: ret double 0xBADEADBEEF
; LE: @test8
; LE: ret double 0xBADEADBEEF
; BE: @test8
; BE: ret double 0xDEADBEEFBA000000
}
@ -74,8 +110,13 @@ define i128 @test9() {
%r = load i128* bitcast({i64, i64}* @g3 to i128*)
ret i128 %r
; CHECK: @test9
; CHECK: ret i128 2071796475790618158476296315
; 0x00000000_06B1BFF8_00000000_0000007B
; LE: @test9
; LE: ret i128 2071796475790618158476296315
; 0x00000000_0000007B_00000000_06B1BFF8
; BE: @test9
; BE: ret i128 2268949521066387161080
}
; vector load.
@ -83,21 +124,30 @@ define <2 x i64> @test10() {
%r = load <2 x i64>* bitcast({i64, i64}* @g3 to <2 x i64>*)
ret <2 x i64> %r
; CHECK: @test10
; CHECK: ret <2 x i64> <i64 123, i64 112312312>
; LE: @test10
; LE: ret <2 x i64> <i64 123, i64 112312312>
; BE: @test10
; BE: ret <2 x i64> <i64 123, i64 112312312>
}
; PR5287
; { 0xA1, 0x08 }
@g4 = internal constant { i8, i8 } { i8 -95, i8 8 }
define i16 @test11() nounwind {
entry:
%a = load i16* bitcast ({ i8, i8 }* @g4 to i16*)
ret i16 %a
; CHECK: @test11
; CHECK: ret i16 2209
; 0x08A1
; LE: @test11
; LE: ret i16 2209
; 0xA108
; BE: @test11
; BE: ret i16 -24312
}
@ -107,8 +157,14 @@ entry:
define i16 @test12() {
%a = load i16* getelementptr inbounds ([3 x i16]* bitcast ([6 x i8]* @test12g to [3 x i16]*), i32 0, i64 1)
ret i16 %a
; CHECK: @test12
; CHECK: ret i16 98
; 0x0062
; LE: @test12
; LE: ret i16 98
; 0x6200
; BE: @test12
; BE: ret i16 25088
}
@ -117,8 +173,12 @@ define i16 @test12() {
define i1 @test13() {
%A = load i1* bitcast (i8* @g5 to i1*)
ret i1 %A
; CHECK: @test13
; CHECK: ret i1 false
; LE: @test13
; LE: ret i1 false
; BE: @test13
; BE: ret i1 false
}
@g6 = constant [2 x i8*] [i8* inttoptr (i64 1 to i8*), i8* inttoptr (i64 2 to i8*)]
@ -126,14 +186,22 @@ define i64 @test14() nounwind {
entry:
%tmp = load i64* bitcast ([2 x i8*]* @g6 to i64*)
ret i64 %tmp
; CHECK: @test14
; CHECK: ret i64 1
; LE: @test14
; LE: ret i64 1
; BE: @test14
; BE: ret i64 1
}
define i64 @test15() nounwind {
entry:
%tmp = load i64* bitcast (i8** getelementptr inbounds ([2 x i8*]* @g6, i32 0, i64 1) to i64*)
ret i64 %tmp
; CHECK: @test15
; CHECK: ret i64 2
; LE: @test15
; LE: ret i64 2
; BE: @test15
; BE: ret i64 2
}

View File

@ -1,7 +1,5 @@
; RUN: opt < %s -basicaa -gvn -S -die | FileCheck %s
; 32-bit little endian target.
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-n8:16:32"
; RUN: opt < %s -default-data-layout="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-n8:16:32" -basicaa -gvn -S -die | FileCheck %s
; RUN: opt < %s -default-data-layout="E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-n32" -basicaa -gvn -S -die | FileCheck %s
;; Trivial RLE test.
define i32 @test0(i32 %V, i32* %P) {
@ -318,7 +316,7 @@ define i8 @coerce_offset_nonlocal0(i32* %P, i1 %cond) {
%P4 = getelementptr i8* %P3, i32 2
br i1 %cond, label %T, label %F
T:
store i32 42, i32* %P
store i32 57005, i32* %P
br label %Cont
F:

View File

@ -1,12 +1,14 @@
; RUN: opt < %s -instcombine -S | FileCheck %s
; RUN: opt < %s -default-data-layout="e-p:32:32:32" -instcombine -S | FileCheck %s --check-prefix=LE
; RUN: opt < %s -default-data-layout="E-p:32:32:32" -instcombine -S | FileCheck %s --check-prefix=BE
; PR13442
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:32:32-n8:16:32-S128"
@test = constant [4 x i32] [i32 1, i32 2, i32 3, i32 4]
define i64 @foo() {
%ret = load i64* bitcast (i8* getelementptr (i8* bitcast ([4 x i32]* @test to i8*), i64 2) to i64*), align 1
ret i64 %ret
; CHECK: ret i64 844424930263040
; 0x00030000_00020000 in [01 00/00 00 02 00 00 00 03 00/00 00 04 00 00 00]
; LE: ret i64 844424930263040
; 0x00000200_00000300 in [00 00/00 01 00 00 00 02 00 00/00 03 00 00 00 04]
; BE: ret i64 281474976841728
}

View File

@ -1,8 +1,9 @@
; This test makes sure that these instructions are properly constant propagated.
target datalayout = "e-p:32:32"
; RUN: opt < %s -default-data-layout="e-p:32:32" -sccp -S | FileCheck %s
; RUN: opt < %s -default-data-layout="E-p:32:32" -sccp -S | FileCheck %s
; RUN: opt < %s -sccp -S | not grep load
; CHECK-NOT: load
@X = constant i32 42 ; <i32*> [#uses=1]