Skip to content

Commit f8b04eb

Browse files
committed
[X86] matchIndexRecursively - add zext(add/addlike(x,c)) -> index: zext(x), disp + zext(c) pattern handling
More restricted alternative to a8cef6b
1 parent 6c0b9e3 commit f8b04eb

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,35 @@ SDValue X86DAGToDAGISel::matchIndexRecursively(SDValue N,
22802280
}
22812281
}
22822282

2283+
// index: zext(add_nuw(x,c)) -> index: zext(x), disp + zext(c)
2284+
// index: zext(addlike(x,c)) -> index: zext(x), disp + zext(c)
2285+
// TODO: call matchIndexRecursively(AddSrc) if we won't corrupt sext?
2286+
if (Opc == ISD::ZERO_EXTEND && !VT.isVector() && N.hasOneUse()) {
2287+
SDValue Src = N.getOperand(0);
2288+
unsigned SrcOpc = Src.getOpcode();
2289+
if (((SrcOpc == ISD::ADD && Src->getFlags().hasNoUnsignedWrap()) ||
2290+
CurDAG->isADDLike(Src)) &&
2291+
Src.hasOneUse()) {
2292+
if (CurDAG->isBaseWithConstantOffset(Src)) {
2293+
SDValue AddSrc = Src.getOperand(0);
2294+
auto *AddVal = cast<ConstantSDNode>(Src.getOperand(1));
2295+
uint64_t Offset = (uint64_t)AddVal->getZExtValue();
2296+
if (!foldOffsetIntoAddress(Offset * AM.Scale, AM)) {
2297+
SDLoc DL(N);
2298+
SDValue ExtSrc = CurDAG->getNode(Opc, DL, VT, AddSrc);
2299+
SDValue ExtVal = CurDAG->getConstant(Offset, DL, VT);
2300+
SDValue ExtAdd = CurDAG->getNode(SrcOpc, DL, VT, ExtSrc, ExtVal);
2301+
insertDAGNode(*CurDAG, N, ExtSrc);
2302+
insertDAGNode(*CurDAG, N, ExtVal);
2303+
insertDAGNode(*CurDAG, N, ExtAdd);
2304+
CurDAG->ReplaceAllUsesWith(N, ExtAdd);
2305+
CurDAG->RemoveDeadNode(N.getNode());
2306+
return ExtSrc;
2307+
}
2308+
}
2309+
}
2310+
}
2311+
22832312
// TODO: Handle extensions, shifted masks etc.
22842313
return N;
22852314
}

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

0 commit comments

Comments
 (0)