Skip to content

Commit 3488894

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 0c90e88 commit 3488894

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
@@ -14386,9 +14386,17 @@ static SDValue CombineANDShift(SDNode *N,
1438614386
}
1438714387
}
1438814388

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

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)