Skip to content

Commit d2471d0

Browse files
committed
[ARM] Resolve FIXME: Transform "(and (shl x, c2) c1)" into "(shl (and x, c1>>c2), c2)"
Transform "(and (shl x, c2) c1)" into "(shl (and x, c1>>c2), c2)" if "c1 >> c2" is a cheaper immediate than "c1" using HasLowerConstantMaterializationCost.
1 parent 8d8bb35 commit d2471d0

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14388,9 +14388,17 @@ static SDValue CombineANDShift(SDNode *N,
1438814388
}
1438914389
}
1439014390

14391-
// FIXME: Transform "(and (shl x, c2) c1)" ->
14392-
// "(shl (and x, c1>>c2), c2)" if "c1 >> c2" is a cheaper immediate than
14393-
// c1.
14391+
// Transform "(and (shl x, c2) c1)" into "(shl (and x, c1>>c2), c2)"
14392+
// if "c1 >> c2" is a cheaper immediate than "c1"
14393+
if (LeftShift &&
14394+
HasLowerConstantMaterializationCost(C1 >> C2, C1, Subtarget)) {
14395+
14396+
SDValue And = DAG.getNode(ISD::AND, DL, MVT::i32, N0->getOperand(0),
14397+
DAG.getConstant(C1 >> C2, DL, MVT::i32));
14398+
return DAG.getNode(ISD::SHL, DL, MVT::i32, And,
14399+
DAG.getConstant(C2, DL, MVT::i32));
14400+
}
14401+
1439414402
return SDValue();
1439514403
}
1439614404

llvm/test/CodeGen/Thumb/shift-and.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ define i32 @test6(i32 %x) {
7070
; CHECK-LABEL: test6:
7171
; CHECK: @ %bb.0: @ %entry
7272
; CHECK-NEXT: movs r1, #5
73-
; CHECK-NEXT: lsls r1, r1, #29
74-
; CHECK-NEXT: lsls r0, r0, #29
75-
; CHECK-NEXT: ands r0, r1
73+
; CHECK-NEXT: ands r1, r0
74+
; CHECK-NEXT: lsls r0, r1, #29
7675
; CHECK-NEXT: bx lr
7776
entry:
7877
%0 = shl i32 %x, 29

0 commit comments

Comments
 (0)