Skip to content

Commit 79d4afd

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 3f0ac46 commit 79d4afd

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

llvm/lib/Target/X86/X86RegisterInfo.cpp

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

967967
if (MI.getOperand(FIOperandNum+3).isImm()) {
968-
// Offset is a 32-bit integer.
969-
int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
970-
int Offset = FIOffset + Imm;
971-
assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
972-
"Requesting 64-bit offset in 32-bit immediate!");
968+
int64_t Imm = MI.getOperand(FIOperandNum + 3).getImm();
969+
int Offset = FIOffset + (int)Imm;
970+
if (!Is64Bit && !isInt<32>((int64_t)FIOffset + Imm))
971+
MI.emitGenericError("requesting 64-bit offset in 32-bit immediate");
973972
if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
974973
MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
975974
} else {
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 #113856 - incorrect FastISel assert
6+
7+
define dso_local i32 @main() #0 {
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
attributes #0 = { optnone noinline }

0 commit comments

Comments
 (0)