Skip to content

Commit a5cc23b

Browse files
committed
[X86] Fix 32-bit immediate assertion and convert into backend error
The assertion previously did not work correctly because the operand was being truncated to an `int` prior to comparison.
1 parent deba201 commit a5cc23b

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed

llvm/lib/Target/X86/X86RegisterInfo.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,10 @@ X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
958958
}
959959

960960
if (MI.getOperand(FIOperandNum+3).isImm()) {
961-
// Offset is a 32-bit integer.
962-
int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
963-
int Offset = FIOffset + Imm;
964-
assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
965-
"Requesting 64-bit offset in 32-bit immediate!");
961+
int64_t Imm = MI.getOperand(FIOperandNum + 3).getImm();
962+
int Offset = FIOffset + (int)Imm;
963+
if (!Is64Bit && !isInt<32>((int64_t)FIOffset + Imm))
964+
MI.emitGenericError("requesting 64-bit offset in 32-bit immediate");
966965
if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
967966
MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
968967
} else {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --no_x86_scrub_sp --version 4
2+
; RUN: llc -O0 -mtriple=x86_64 -mattr=+avx512f -verify-machineinstrs < %s | FileCheck %s --check-prefix=CHECK
3+
define void @f(i16 %LGV2, i1 %LGV3) {
4+
; CHECK-LABEL: f:
5+
; CHECK: # %bb.0: # %BB
6+
; CHECK-NEXT: subq $2147483520, %rsp # imm = 0x7FFFFF80
7+
; CHECK-NEXT: .cfi_def_cfa_offset 2147483528
8+
; CHECK-NEXT: movb %sil, %cl
9+
; CHECK-NEXT: movw %di, %ax
10+
; CHECK-NEXT: movswq %ax, %rax
11+
; CHECK-NEXT: andb $1, %cl
12+
; CHECK-NEXT: movb %cl, 2147483520(%rsp,%rax)
13+
; CHECK-NEXT: addq $2147483520, %rsp # imm = 0x7FFFFF80
14+
; CHECK-NEXT: .cfi_def_cfa_offset 8
15+
; CHECK-NEXT: retq
16+
BB:
17+
%A = alloca i1, i33 2147483648, align 1
18+
%G = getelementptr i1, ptr %A, i16 %LGV2
19+
%G4 = getelementptr i1, ptr %G, i32 -2147483648
20+
store i1 %LGV3, ptr %G4, align 1
21+
ret void
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -mtriple=x86_64 -O=0 | FileCheck %s
3+
@G = global i8 0
4+
5+
; Regression test for PR113856 - incorrect FastISel assert
6+
7+
define i32 @main() {
8+
; CHECK-LABEL: main:
9+
; CHECK: # %bb.0:
10+
; CHECK-NEXT: movl $0, {{[0-9]+}}(%rsp)
11+
; CHECK-NEXT: xorl %eax, %eax
12+
; CHECK-NEXT: retq
13+
%1 = alloca i32, align 4
14+
%G = getelementptr i8, ptr %1, i32 -2147483648
15+
store i32 0, ptr %G, align 4
16+
ret i32 0
17+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
; RUN: not llc < %s -mtriple=i686 -filetype=null 2>&1 | FileCheck %s -check-prefix=ERR-i686
2+
; RUN: llc < %s -mtriple=x86_64 | FileCheck %s -check-prefix=x86_64
3+
4+
; Regression test for #121932, #113856, #106352, #69365, #25051 which are caused by
5+
; an incorrectly written assertion for 64-bit offsets when compiling for 32-bit X86.
6+
7+
define i32 @main() #0 {
8+
; ERR-i686: error: <unknown>:0:0: requesting 64-bit offset in 32-bit immediate
9+
;
10+
; x86_64-LABEL: main:
11+
; x86_64: # %bb.0: # %entry
12+
; x86_64-NEXT: movl $4294967176, %eax # imm = 0xFFFFFF88
13+
; x86_64-NEXT: subq %rax, %rsp
14+
; x86_64-NEXT: .cfi_def_cfa_offset 4294967184
15+
; x86_64-NEXT: movb $32, -1073741994(%rsp)
16+
; x86_64-NEXT: movb $33, 2147483478(%rsp)
17+
; x86_64-NEXT: movb $34, 1073741654(%rsp)
18+
; x86_64-NEXT: movb $35, -170(%rsp)
19+
; x86_64-NEXT: xorl %eax, %eax
20+
; x86_64-NEXT: movl $4294967176, %ecx # imm = 0xFFFFFF88
21+
; x86_64-NEXT: addq %rcx, %rsp
22+
; x86_64-NEXT: .cfi_def_cfa_offset 8
23+
; x86_64-NEXT: retq
24+
entry:
25+
%a = alloca [1073741824 x i8], align 16
26+
%b = alloca [1073741824 x i8], align 16
27+
%c = alloca [1073741824 x i8], align 16
28+
%d = alloca [1073741824 x i8], align 16
29+
30+
%arrayida = getelementptr inbounds [1073741824 x i8], ptr %a, i64 0, i64 -42
31+
%arrayidb = getelementptr inbounds [1073741824 x i8], ptr %b, i64 0, i64 -42
32+
%arrayidc = getelementptr inbounds [1073741824 x i8], ptr %c, i64 0, i64 -42
33+
%arrayidd = getelementptr inbounds [1073741824 x i8], ptr %d, i64 0, i64 -42
34+
35+
store i8 32, ptr %arrayida, align 2
36+
store i8 33, ptr %arrayidb, align 2
37+
store i8 34, ptr %arrayidc, align 2
38+
store i8 35, ptr %arrayidd, align 2
39+
40+
ret i32 0
41+
}
42+
43+
define i32 @0() #0 {
44+
; ERR-i686: error: <unknown>:0:0: requesting 64-bit offset in 32-bit immediate
45+
;
46+
; x86_64-LABEL: __unnamed_1:
47+
; x86_64: # %bb.0: # %entry
48+
; x86_64-NEXT: movl $4294967176, %eax # imm = 0xFFFFFF88
49+
; x86_64-NEXT: subq %rax, %rsp
50+
; x86_64-NEXT: .cfi_def_cfa_offset 4294967184
51+
; x86_64-NEXT: movb $32, -1073741994(%rsp)
52+
; x86_64-NEXT: movb $33, 2147483478(%rsp)
53+
; x86_64-NEXT: movb $34, 1073741654(%rsp)
54+
; x86_64-NEXT: movb $35, -170(%rsp)
55+
; x86_64-NEXT: xorl %eax, %eax
56+
; x86_64-NEXT: movl $4294967176, %ecx # imm = 0xFFFFFF88
57+
; x86_64-NEXT: addq %rcx, %rsp
58+
; x86_64-NEXT: .cfi_def_cfa_offset 8
59+
; x86_64-NEXT: retq
60+
entry:
61+
%a = alloca [1073741824 x i8], align 16
62+
%b = alloca [1073741824 x i8], align 16
63+
%c = alloca [1073741824 x i8], align 16
64+
%d = alloca [1073741824 x i8], align 16
65+
66+
%arrayida = getelementptr inbounds [1073741824 x i8], ptr %a, i64 0, i64 -42
67+
%arrayidb = getelementptr inbounds [1073741824 x i8], ptr %b, i64 0, i64 -42
68+
%arrayidc = getelementptr inbounds [1073741824 x i8], ptr %c, i64 0, i64 -42
69+
%arrayidd = getelementptr inbounds [1073741824 x i8], ptr %d, i64 0, i64 -42
70+
71+
store i8 32, ptr %arrayida, align 2
72+
store i8 33, ptr %arrayidb, align 2
73+
store i8 34, ptr %arrayidc, align 2
74+
store i8 35, ptr %arrayidd, align 2
75+
76+
ret i32 0
77+
}
78+
79+
attributes #0 = { optnone noinline }

0 commit comments

Comments
 (0)