Skip to content

Commit 1d3329c

Browse files
authored
[Thumb] Resolve FIXME: Transform "(and (shl x, c2), c1)" into "(shl (and x, c1>>c2), c2)" (llvm#82120)
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 f28085a commit 1d3329c

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
@@ -14381,9 +14381,17 @@ static SDValue CombineANDShift(SDNode *N,
1438114381
}
1438214382
}
1438314383

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

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)