Skip to content

Commit 9128705

Browse files
committed
[X86] convertToThreeAddress, make sure second operand of SUB32ri is really an immediate before calling getImm().
It might be a symbol instead. We can't fold those since we can't negate them. Similar for other SUB with immediates. Fixes PR43529. llvm-svn: 373397
1 parent f7133b7 commit 9128705

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
11221122
return nullptr;
11231123
case X86::SUB32ri8:
11241124
case X86::SUB32ri: {
1125+
if (!MI.getOperand(2).isImm())
1126+
return nullptr;
11251127
int64_t Imm = MI.getOperand(2).getImm();
11261128
if (!isInt<32>(-Imm))
11271129
return nullptr;
@@ -1148,6 +1150,8 @@ X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
11481150

11491151
case X86::SUB64ri8:
11501152
case X86::SUB64ri32: {
1153+
if (!MI.getOperand(2).isImm())
1154+
return nullptr;
11511155
int64_t Imm = MI.getOperand(2).getImm();
11521156
if (!isInt<32>(-Imm))
11531157
return nullptr;

llvm/test/CodeGen/X86/pr43529.ll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=i386-unknown-linux-gnu | FileCheck %s
3+
4+
define i32 @a() nounwind {
5+
; CHECK-LABEL: a:
6+
; CHECK: # %bb.0: # %entry
7+
; CHECK-NEXT: pushl %esi
8+
; CHECK-NEXT: subl $8, %esp
9+
; CHECK-NEXT: leal {{[0-9]+}}(%esp), %esi
10+
; CHECK-NEXT: movl %esi, %eax
11+
; CHECK-NEXT: subl $a, %eax
12+
; CHECK-NEXT: calll d
13+
; CHECK-NEXT: cmpl $a, %esi
14+
; CHECK-NEXT: jbe .LBB0_2
15+
; CHECK-NEXT: .p2align 4, 0x90
16+
; CHECK-NEXT: .LBB0_1: # %for.cond
17+
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
18+
; CHECK-NEXT: jmp .LBB0_1
19+
; CHECK-NEXT: .LBB0_2: # %for.end.split
20+
; CHECK-NEXT: addl $8, %esp
21+
; CHECK-NEXT: popl %esi
22+
; CHECK-NEXT: retl
23+
entry:
24+
%b = alloca i32, align 4
25+
%0 = bitcast i32* %b to i8*
26+
%1 = ptrtoint i32* %b to i32
27+
%sub = sub nsw i32 %1, ptrtoint (i32 ()* @a to i32)
28+
%call = call i32 bitcast (i32 (...)* @d to i32 (i32)*)(i32 inreg %sub)
29+
%cmp = icmp ugt i32* %b, bitcast (i32 ()* @a to i32*)
30+
br i1 %cmp, label %for.cond, label %for.end.split
31+
32+
for.cond: ; preds = %entry, %for.cond
33+
br label %for.cond
34+
35+
for.end.split: ; preds = %entry
36+
ret i32 undef
37+
}
38+
39+
declare i32 @d(...)

0 commit comments

Comments
 (0)