Skip to content

Commit a8cef6b

Browse files
committed
[X86] promoteExtBeforeAdd - add support for or/xor 'addlike' patterns
Fold zext(addlike(x, C)) --> add(zext(x), C_zext) if its likely to help us create LEA instructions Addresses some regressions exposed by D155472
1 parent 79941c3 commit a8cef6b

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52131,11 +52131,12 @@ static SDValue combineSignExtendInReg(SDNode *N, SelectionDAG &DAG,
5213152131
return SDValue();
5213252132
}
5213352133

52134-
/// sext(add_nsw(x, C)) --> add(sext(x), C_sext)
52135-
/// zext(add_nuw(x, C)) --> add(zext(x), C_zext)
52136-
/// Promoting a sign/zero extension ahead of a no overflow 'add' exposes
52137-
/// opportunities to combine math ops, use an LEA, or use a complex addressing
52138-
/// mode. This can eliminate extend, add, and shift instructions.
52134+
/// sext(add_nsw(x, C)) --> add_nsw(sext(x), C_sext)
52135+
/// zext(add_nuw(x, C)) --> add_nuw(zext(x), C_zext)
52136+
/// zext(addlike(x, C)) --> add(zext(x), C_zext)
52137+
/// Promoting a sign/zero extension ahead of a no overflow 'add' or 'addlike'
52138+
/// exposes opportunities to combine math ops, use an LEA, or use a complex
52139+
/// addressing mode. This can eliminate extend, add, and shift instructions.
5213952140
static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
5214052141
const X86Subtarget &Subtarget) {
5214152142
if (Ext->getOpcode() != ISD::SIGN_EXTEND &&
@@ -52147,17 +52148,19 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
5214752148
if (VT != MVT::i64)
5214852149
return SDValue();
5214952150

52150-
SDValue Add = Ext->getOperand(0);
52151-
if (Add.getOpcode() != ISD::ADD)
52152-
return SDValue();
52153-
52151+
bool NSW = false, NUW = false;
5215452152
bool Sext = Ext->getOpcode() == ISD::SIGN_EXTEND;
52155-
bool NSW = Add->getFlags().hasNoSignedWrap();
52156-
bool NUW = Add->getFlags().hasNoUnsignedWrap();
5215752153

52158-
// We need an 'add nsw' feeding into the 'sext' or 'add nuw' feeding
52159-
// into the 'zext'
52160-
if ((Sext && !NSW) || (!Sext && !NUW))
52154+
SDValue Add = Ext->getOperand(0);
52155+
unsigned AddOpc = Add->getOpcode();
52156+
if (AddOpc == ISD::ADD) {
52157+
NSW = Add->getFlags().hasNoSignedWrap();
52158+
NUW = Add->getFlags().hasNoUnsignedWrap();
52159+
// We need an 'add nsw' feeding into the 'sext' or 'add nuw' feeding
52160+
// into the 'zext'
52161+
if ((Sext && !NSW) || (!Sext && !NUW))
52162+
return SDValue();
52163+
} else if (!(!Sext && DAG.isADDLike(Add)))
5216152164
return SDValue();
5216252165

5216352166
// Having a constant operand to the 'add' ensures that we are not increasing
@@ -52193,7 +52196,7 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
5219352196
SDNodeFlags Flags;
5219452197
Flags.setNoSignedWrap(NSW);
5219552198
Flags.setNoUnsignedWrap(NUW);
52196-
return DAG.getNode(ISD::ADD, SDLoc(Add), VT, NewExt, NewConstant, Flags);
52199+
return DAG.getNode(AddOpc, SDLoc(Add), VT, NewExt, NewConstant, Flags);
5219752200
}
5219852201

5219952202
// If we face {ANY,SIGN,ZERO}_EXTEND that is applied to a CMOV with constant

llvm/test/CodeGen/X86/lea-2.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ define i32 @test1(i32 %A, i32 %B) {
2626
ret i32 %t4
2727
}
2828

29-
; TODO: The addlike OR instruction should fold into the LEA.
29+
; The addlike OR instruction should fold into the LEA.
3030

3131
define i64 @test2(i32 %a0, i64 %a1) {
3232
; X32-LABEL: test2:
@@ -44,8 +44,7 @@ define i64 @test2(i32 %a0, i64 %a1) {
4444
; X64: # %bb.0:
4545
; X64-NEXT: # kill: def $edi killed $edi def $rdi
4646
; X64-NEXT: andl $-8, %edi
47-
; X64-NEXT: orl $2, %edi
48-
; X64-NEXT: leaq (%rsi,%rdi,2), %rax
47+
; X64-NEXT: leaq 4(%rsi,%rdi,2), %rax
4948
; X64-NEXT: retq
5049
%x1 = and i32 %a0, -8
5150
%x2 = or i32 %x1, 2

llvm/test/CodeGen/X86/select_const.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,8 @@ define i64 @select_pow2_diff_neg_invert(i1 zeroext %cond) {
626626
;
627627
; X64-LABEL: select_pow2_diff_neg_invert:
628628
; X64: # %bb.0:
629-
; X64-NEXT: xorb $1, %dil
630-
; X64-NEXT: movzbl %dil, %eax
629+
; X64-NEXT: movl %edi, %eax
630+
; X64-NEXT: xorq $1, %rax
631631
; X64-NEXT: shlq $7, %rax
632632
; X64-NEXT: addq $-99, %rax
633633
; X64-NEXT: retq

0 commit comments

Comments
 (0)