Skip to content

Commit b68f7bd

Browse files
committed
[X86] Fix the issue of creating index reg negations
The 8 and 16 bit LEA instruction support was added by PR llvm#122102, and we have to update creating index register negations accordingly. The issue is exposed with APX NDD instructions.
1 parent 637f352 commit b68f7bd

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,23 @@ namespace {
275275
#define GET_ND_IF_ENABLED(OPC) (Subtarget->hasNDD() ? OPC##_ND : OPC)
276276
// Negate the index if needed.
277277
if (AM.NegateIndex) {
278-
unsigned NegOpc = VT == MVT::i64 ? GET_ND_IF_ENABLED(X86::NEG64r)
279-
: GET_ND_IF_ENABLED(X86::NEG32r);
278+
unsigned NegOpc;
279+
switch (VT.SimpleTy) {
280+
default:
281+
llvm_unreachable("Unsupported VT!");
282+
case MVT::i64:
283+
NegOpc = GET_ND_IF_ENABLED(X86::NEG64r);
284+
break;
285+
case MVT::i32:
286+
NegOpc = GET_ND_IF_ENABLED(X86::NEG32r);
287+
break;
288+
case MVT::i16:
289+
NegOpc = GET_ND_IF_ENABLED(X86::NEG16r);
290+
break;
291+
case MVT::i8:
292+
NegOpc = GET_ND_IF_ENABLED(X86::NEG8r);
293+
break;
294+
}
280295
SDValue Neg = SDValue(CurDAG->getMachineNode(NegOpc, DL, VT, MVT::i32,
281296
AM.IndexReg), 0);
282297
AM.IndexReg = Neg;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc %s -mtriple=x86_64-unknown -mattr=+ndd -verify-machineinstrs --show-mc-encoding -o - | FileCheck %s --check-prefix=NDD
3+
4+
5+
define void @neg_8bit_1(i1 %cmp) {
6+
; NDD-LABEL: neg_8bit_1:
7+
; NDD: # %bb.0: # %entry
8+
; NDD-NEXT: andb $1, %dil, %al # encoding: [0x62,0xf4,0x7c,0x18,0x80,0xe7,0x01]
9+
; NDD-NEXT: movzbl 0, %ecx # encoding: [0x0f,0xb6,0x0c,0x25,0x00,0x00,0x00,0x00]
10+
; NDD-NEXT: negb %al, %al # encoding: [0x62,0xf4,0x7c,0x18,0xf6,0xd8]
11+
; NDD-NEXT: leaw 2(%rcx,%rax), %al # encoding: [0x66,0x8d,0x44,0x01,0x02]
12+
; NDD-NEXT: movb %al, 0 # encoding: [0x88,0x04,0x25,0x00,0x00,0x00,0x00]
13+
; NDD-NEXT: retq # encoding: [0xc3]
14+
entry:
15+
%cond = select i1 %cmp, i8 1, i8 2
16+
%0 = load i8, ptr null, align 4
17+
%add = add i8 %cond, %0
18+
store i8 %add, ptr null, align 4
19+
ret void
20+
}
21+
22+
define void @neg_8bit_2(i8 %int8) {
23+
; NDD-LABEL: neg_8bit_2:
24+
; NDD: # %bb.0: # %entry
25+
; NDD-NEXT: # kill: def $edi killed $edi def $rdi
26+
; NDD-NEXT: addb %dil, %dil, %al # encoding: [0x62,0xf4,0x7c,0x18,0x00,0xff]
27+
; NDD-NEXT: negb %al, %al # encoding: [0x62,0xf4,0x7c,0x18,0xf6,0xd8]
28+
; NDD-NEXT: leaw 1(%rdi,%rax), %al # encoding: [0x66,0x8d,0x44,0x07,0x01]
29+
; NDD-NEXT: mulb %dil # encoding: [0x40,0xf6,0xe7]
30+
; NDD-NEXT: testb %al, %al # encoding: [0x84,0xc0]
31+
; NDD-NEXT: retq # encoding: [0xc3]
32+
entry:
33+
%0 = shl i8 %int8, 1
34+
%sub = sub i8 %int8, %0
35+
%add = add i8 %sub, 1
36+
%div = mul i8 %add, %int8
37+
%cmp = icmp slt i8 %div, 0
38+
br i1 %cmp, label %label2, label %label1
39+
40+
label1: ; preds = %entry
41+
ret void
42+
43+
label2: ; preds = %entry
44+
ret void
45+
}
46+
47+
define i32 @neg_16bit(i16 %0) {
48+
; NDD-LABEL: neg_16bit:
49+
; NDD: # %bb.0: # %entry
50+
; NDD-NEXT: # kill: def $edi killed $edi def $rdi
51+
; NDD-NEXT: incw %di, %ax # encoding: [0x62,0xf4,0x7d,0x18,0xff,0xc7]
52+
; NDD-NEXT: addw $256, %di, %cx # encoding: [0x62,0xf4,0x75,0x18,0x81,0xc7,0x00,0x01]
53+
; NDD-NEXT: # imm = 0x100
54+
; NDD-NEXT: testw %ax, %ax # encoding: [0x66,0x85,0xc0]
55+
; NDD-NEXT: cmovsl %ecx, %eax # EVEX TO LEGACY Compression encoding: [0x0f,0x48,0xc1]
56+
; NDD-NEXT: andw $-256, %ax # EVEX TO LEGACY Compression encoding: [0x66,0x25,0x00,0xff]
57+
; NDD-NEXT: negw %ax, %ax # encoding: [0x62,0xf4,0x7d,0x18,0xf7,0xd8]
58+
; NDD-NEXT: leaw 1(%rdi,%rax), %ax # encoding: [0x66,0x8d,0x44,0x07,0x01]
59+
; NDD-NEXT: movzwl %ax, %eax # encoding: [0x0f,0xb7,0xc0]
60+
; NDD-NEXT: movq %rax, 0 # encoding: [0x48,0x89,0x04,0x25,0x00,0x00,0x00,0x00]
61+
; NDD-NEXT: xorl %eax, %eax # encoding: [0x31,0xc0]
62+
; NDD-NEXT: retq # encoding: [0xc3]
63+
entry:
64+
%add = add i16 %0, 1
65+
%rem = srem i16 %add, 256
66+
%1 = zext i16 %rem to i19
67+
%2 = sext i19 %1 to i64
68+
%3 = getelementptr i8, ptr null, i64 %2
69+
store ptr %3, ptr null, align 4
70+
ret i32 0
71+
}

0 commit comments

Comments
 (0)