Skip to content

Commit 518caa6

Browse files
committed
[X86] Improve transform for add-like nodes to add
We previously did this only in tablegen, after we would have already dropped `disjoint` flag from `or`.
1 parent 1f613bc commit 518caa6

File tree

115 files changed

+1241
-1137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+1241
-1137
lines changed

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5294,11 +5294,26 @@ void X86DAGToDAGISel::Select(SDNode *Node) {
52945294
return;
52955295
if (tryVPTERNLOG(Node))
52965296
return;
5297-
52985297
[[fallthrough]];
52995298
case ISD::ADD:
53005299
if (Opcode == ISD::ADD && matchBitExtract(Node))
53015300
return;
5301+
5302+
// Convert addlike to add before final selection. Do this before we drop
5303+
// flags like `disjoint`.
5304+
// NB: Conversion to add is preferable so we use `lea` in codegen.
5305+
if (Opcode != ISD::ADD && NVT.isScalarInteger() &&
5306+
(Opcode == ISD::OR ||
5307+
(NVT == MVT::i8 || NVT == MVT::i16 || NVT == MVT::i32)) &&
5308+
CurDAG->isADDLike(SDValue(Node, 0))
5309+
) {
5310+
SDValue AsAdd = CurDAG->getNode(ISD::ADD, SDLoc(Node), NVT,
5311+
Node->getOperand(0), Node->getOperand(1));
5312+
ReplaceUses(SDValue(Node, 0), AsAdd);
5313+
CurDAG->RemoveDeadNode(Node);
5314+
Node = AsAdd.getNode();
5315+
Opcode = ISD::ADD;
5316+
}
53025317
[[fallthrough]];
53035318
case ISD::SUB: {
53045319
// Try to avoid folding immediates with multiple uses for optsize.

llvm/lib/Target/X86/X86InstrCompiler.td

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,24 @@ def : Pat<(or (and GR64:$dst, -65536),
15741574

15751575
def : Pat<(or (and GR32:$dst, -65536),
15761576
(i32 (zextloadi16 addr:$src))),
1577-
(INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm i16mem:$src), sub_16bit)>;
1577+
(INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm i16mem:$src), sub_16bit)>;
1578+
1579+
// We convert or -> add when the or is disjoint so need to handle for add as well.
1580+
def : Pat<(add (and GR64:$dst, -256),
1581+
(i64 (zextloadi8 addr:$src))),
1582+
(INSERT_SUBREG (i64 (COPY $dst)), (MOV8rm i8mem:$src), sub_8bit)>;
1583+
1584+
def : Pat<(add (and GR32:$dst, -256),
1585+
(i32 (zextloadi8 addr:$src))),
1586+
(INSERT_SUBREG (i32 (COPY $dst)), (MOV8rm i8mem:$src), sub_8bit)>;
1587+
1588+
def : Pat<(add (and GR64:$dst, -65536),
1589+
(i64 (zextloadi16 addr:$src))),
1590+
(INSERT_SUBREG (i64 (COPY $dst)), (MOV16rm i16mem:$src), sub_16bit)>;
1591+
1592+
def : Pat<(add (and GR32:$dst, -65536),
1593+
(i32 (zextloadi16 addr:$src))),
1594+
(INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm i16mem:$src), sub_16bit)>;
15781595

15791596
// To avoid needing to materialize an immediate in a register, use a 32-bit and
15801597
// with implicit zero-extension instead of a 64-bit and if the immediate has at

llvm/test/CodeGen/X86/2009-05-23-dagcombine-shifts.ll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
12
; RUN: llc < %s | FileCheck %s
23

34
; Check that the shr(shl X, 56), 48) is not mistakenly turned into
@@ -16,11 +17,13 @@ target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3
1617
target triple = "x86_64-unknown-linux-gnu"
1718

1819
define i64 @foo(i64 %b) nounwind readnone {
19-
entry:
2020
; CHECK-LABEL: foo:
21-
; CHECK: movsbq %dil, %rax
22-
; CHECK: shlq $8, %rax
23-
; CHECK: orq $1, %rax
21+
; CHECK: # %bb.0: # %entry
22+
; CHECK-NEXT: movsbq %dil, %rax
23+
; CHECK-NEXT: shlq $8, %rax
24+
; CHECK-NEXT: incq %rax
25+
; CHECK-NEXT: retq
26+
entry:
2427
%shl = shl i64 %b, 56 ; <i64> [#uses=1]
2528
%shr = ashr i64 %shl, 48 ; <i64> [#uses=1]
2629
%add5 = or i64 %shr, 1 ; <i64> [#uses=1]

llvm/test/CodeGen/X86/3addr-or.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ define i64 @test2(i8 %A, i8 %B) nounwind {
2424
; CHECK-NEXT: andl $48, %edi
2525
; CHECK-NEXT: movzbl %sil, %eax
2626
; CHECK-NEXT: shrl $4, %eax
27-
; CHECK-NEXT: orl %edi, %eax
27+
; CHECK-NEXT: addl %edi, %eax
2828
; CHECK-NEXT: retq
2929
%C = zext i8 %A to i64
3030
%D = shl i64 %C, 4
@@ -42,7 +42,7 @@ define void @test3(i32 %x, ptr %P) nounwind readnone ssp {
4242
; CHECK-LABEL: test3:
4343
; CHECK: # %bb.0:
4444
; CHECK-NEXT: shll $5, %edi
45-
; CHECK-NEXT: orl $3, %edi
45+
; CHECK-NEXT: addl $3, %edi
4646
; CHECK-NEXT: movl %edi, (%rsi)
4747
; CHECK-NEXT: retq
4848
%t0 = shl i32 %x, 5
@@ -71,7 +71,7 @@ define void @test5(i32 %a, i32 %b, ptr nocapture %P) nounwind ssp {
7171
; CHECK: # %bb.0:
7272
; CHECK-NEXT: andl $6, %edi
7373
; CHECK-NEXT: andl $16, %esi
74-
; CHECK-NEXT: orl %edi, %esi
74+
; CHECK-NEXT: addl %edi, %esi
7575
; CHECK-NEXT: movl %esi, (%rdx)
7676
; CHECK-NEXT: retq
7777
%and = and i32 %a, 6

llvm/test/CodeGen/X86/addcarry2.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ define void @adc_load_store_32_127(ptr inreg %x, ptr inreg %x2, i32 inreg %y) no
138138
; X64-NEXT: movl (%rdi), %eax # encoding: [0x8b,0x07]
139139
; X64-NEXT: shlq $32, %rax # encoding: [0x48,0xc1,0xe0,0x20]
140140
; X64-NEXT: movl %edx, %ecx # encoding: [0x89,0xd1]
141-
; X64-NEXT: orq %rax, %rcx # encoding: [0x48,0x09,0xc1]
141+
; X64-NEXT: addq %rax, %rcx # encoding: [0x48,0x01,0xc1]
142142
; X64-NEXT: movabsq $545460846593, %rax # encoding: [0x48,0xb8,0x01,0x00,0x00,0x00,0x7f,0x00,0x00,0x00]
143143
; X64-NEXT: # imm = 0x7F00000001
144144
; X64-NEXT: xorl %edx, %edx # encoding: [0x31,0xd2]
@@ -178,7 +178,7 @@ define void @adc_load_store_32_128(ptr inreg %x, ptr inreg %x2, i32 inreg %y) no
178178
; X64-NEXT: movl (%rdi), %eax # encoding: [0x8b,0x07]
179179
; X64-NEXT: shlq $32, %rax # encoding: [0x48,0xc1,0xe0,0x20]
180180
; X64-NEXT: movl %edx, %ecx # encoding: [0x89,0xd1]
181-
; X64-NEXT: orq %rax, %rcx # encoding: [0x48,0x09,0xc1]
181+
; X64-NEXT: addq %rax, %rcx # encoding: [0x48,0x01,0xc1]
182182
; X64-NEXT: movabsq $549755813889, %rax # encoding: [0x48,0xb8,0x01,0x00,0x00,0x00,0x80,0x00,0x00,0x00]
183183
; X64-NEXT: # imm = 0x8000000001
184184
; X64-NEXT: xorl %edx, %edx # encoding: [0x31,0xd2]

llvm/test/CodeGen/X86/and-or-fold.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ define i32 @test1(i32 %x, i16 %y) {
4545
; DARWIN-NEXT: movzwl {{[0-9]+}}(%esp), %ecx
4646
; DARWIN-NEXT: movl {{[0-9]+}}(%esp), %eax
4747
; DARWIN-NEXT: shll $16, %eax
48-
; DARWIN-NEXT: orl %ecx, %eax
48+
; DARWIN-NEXT: addl %ecx, %eax
4949
; DARWIN-NEXT: andl $16711807, %eax ## imm = 0xFF007F
5050
; DARWIN-NEXT: retl
5151
;
@@ -54,7 +54,7 @@ define i32 @test1(i32 %x, i16 %y) {
5454
; DARWIN-OPT-NEXT: andl $127, %esi
5555
; DARWIN-OPT-NEXT: movzbl %dil, %eax
5656
; DARWIN-OPT-NEXT: shll $16, %eax
57-
; DARWIN-OPT-NEXT: orl %esi, %eax
57+
; DARWIN-OPT-NEXT: addl %esi, %eax
5858
; DARWIN-OPT-NEXT: retq
5959
%tmp1 = zext i16 %y to i32
6060
%tmp2 = and i32 %tmp1, 127

llvm/test/CodeGen/X86/andimm8.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ define void @foo(i64 %zed, ptr %x) nounwind {
2929
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx # encoding: [0x8b,0x4c,0x24,0x04]
3030
; X86-NEXT: movl {{[0-9]+}}(%esp), %edx # encoding: [0x8b,0x54,0x24,0x08]
3131
; X86-NEXT: andl $-4, %ecx # encoding: [0x83,0xe1,0xfc]
32-
; X86-NEXT: orl $2, %ecx # encoding: [0x83,0xc9,0x02]
32+
; X86-NEXT: addl $2, %ecx # encoding: [0x83,0xc1,0x02]
3333
; X86-NEXT: movl %edx, 4(%eax) # encoding: [0x89,0x50,0x04]
3434
; X86-NEXT: movl %ecx, (%eax) # encoding: [0x89,0x08]
3535
; X86-NEXT: retl # encoding: [0xc3]
3636
;
3737
; X64-LABEL: foo:
3838
; X64: # %bb.0:
3939
; X64-NEXT: andq $-4, %rdi # encoding: [0x48,0x83,0xe7,0xfc]
40-
; X64-NEXT: orq $2, %rdi # encoding: [0x48,0x83,0xcf,0x02]
40+
; X64-NEXT: addq $2, %rdi # encoding: [0x48,0x83,0xc7,0x02]
4141
; X64-NEXT: movq %rdi, (%rsi) # encoding: [0x48,0x89,0x3e]
4242
; X64-NEXT: retq # encoding: [0xc3]
4343
%t1 = and i64 %zed, -4

llvm/test/CodeGen/X86/atomic-unordered.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,7 @@ define i16 @load_combine(ptr %p) {
23592359
; CHECK-O3-NEXT: movzbl (%rdi), %ecx
23602360
; CHECK-O3-NEXT: movzbl 1(%rdi), %eax
23612361
; CHECK-O3-NEXT: shll $8, %eax
2362-
; CHECK-O3-NEXT: orl %ecx, %eax
2362+
; CHECK-O3-NEXT: addl %ecx, %eax
23632363
; CHECK-O3-NEXT: # kill: def $ax killed $ax killed $eax
23642364
; CHECK-O3-NEXT: retq
23652365
%v1 = load atomic i8, ptr %p unordered, align 2

0 commit comments

Comments
 (0)