Skip to content

Commit ff24fe7

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 f4c1e87 commit ff24fe7

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
@@ -14384,9 +14384,17 @@ static SDValue CombineANDShift(SDNode *N,
1438414384
}
1438514385
}
1438614386

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

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)