Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit bc7935f

Browse files
author
Sjoerd Meijer
committed
[Thumb] Teach ISel how to lower compares of AND bitmasks efficiently
This is essentially a recommit of r285893, but with a correctness fix. The problem of the original commit was that this: bic r5, r7, #31 cbz r5, .LBB2_10 got rewritten into: lsrs r5, r7, #5 beq .LBB2_10 The result in destination register r5 is not the same and this is incorrect when r5 is not dead. So this fix includes checking the uses of the AND destination register. And also, compared to the original commit, some regression tests didn't need changing anymore because of this extra check. For completeness, this was the original commit message: For the common pattern (CMPZ (AND x, #bitmask), #0), we can do some more efficient instruction selection if the bitmask is one consecutive sequence of set bits (32 - clz(bm) - ctz(bm) == popcount(bm)). 1) If the bitmask touches the LSB, then we can remove all the upper bits and set the flags by doing one LSLS. 2) If the bitmask touches the MSB, then we can remove all the lower bits and set the flags with one LSRS. 3) If the bitmask has popcount == 1 (only one set bit), we can shift that bit into the sign bit with one LSLS and change the condition query from NE/EQ to MI/PL (we could also implement this by shifting into the carry bit and branching on BCC/BCS). 4) Otherwise, we can emit a sequence of LSLS+LSRS to remove the upper and lower zero bits of the mask. 1-3 require only one 16-bit instruction and can elide the CMP. 4 requires two 16-bit instructions but can elide the CMP and doesn't require materializing a complex immediate, so is also a win. Differential Revision: https://reviews.llvm.org/D27761 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289794 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7a6365a commit bc7935f

File tree

9 files changed

+232
-23
lines changed

9 files changed

+232
-23
lines changed

lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,11 @@ bool ARMBaseInstrInfo::optimizeCompareInstr(
25442544
case ARM::EORrr:
25452545
case ARM::EORri:
25462546
case ARM::t2EORrr:
2547-
case ARM::t2EORri: {
2547+
case ARM::t2EORri:
2548+
case ARM::t2LSRri:
2549+
case ARM::t2LSRrr:
2550+
case ARM::t2LSLri:
2551+
case ARM::t2LSLrr: {
25482552
// Scan forward for the use of CPSR
25492553
// When checking against MI: if it's a conditional code that requires
25502554
// checking of the V bit or C bit, then this is not safe to do.

lib/Target/ARM/ARMISelDAGToDAG.cpp

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ class ARMDAGToDAGISel : public SelectionDAGISel {
245245
bool tryInlineAsm(SDNode *N);
246246

247247
void SelectConcatVector(SDNode *N);
248+
void SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI);
248249

249250
bool trySMLAWSMULW(SDNode *N);
250251

@@ -2730,6 +2731,87 @@ void ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
27302731
ReplaceNode(N, createDRegPairNode(VT, N->getOperand(0), N->getOperand(1)));
27312732
}
27322733

2734+
static Optional<std::pair<unsigned, unsigned>>
2735+
getContiguousRangeOfSetBits(const APInt &A) {
2736+
unsigned FirstOne = A.getBitWidth() - A.countLeadingZeros() - 1;
2737+
unsigned LastOne = A.countTrailingZeros();
2738+
if (A.countPopulation() != (FirstOne - LastOne + 1))
2739+
return Optional<std::pair<unsigned,unsigned>>();
2740+
return std::make_pair(FirstOne, LastOne);
2741+
}
2742+
2743+
void ARMDAGToDAGISel::SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI) {
2744+
assert(N->getOpcode() == ARMISD::CMPZ);
2745+
SwitchEQNEToPLMI = false;
2746+
2747+
if (!Subtarget->isThumb())
2748+
// FIXME: Work out whether it is profitable to do this in A32 mode - LSL and
2749+
// LSR don't exist as standalone instructions - they need the barrel shifter.
2750+
return;
2751+
2752+
// select (cmpz (and X, C), #0) -> (LSLS X) or (LSRS X) or (LSRS (LSLS X))
2753+
SDValue And = N->getOperand(0);
2754+
if (!And->hasOneUse())
2755+
return;
2756+
2757+
SDValue Zero = N->getOperand(1);
2758+
if (!isa<ConstantSDNode>(Zero) || !cast<ConstantSDNode>(Zero)->isNullValue() ||
2759+
And->getOpcode() != ISD::AND)
2760+
return;
2761+
SDValue X = And.getOperand(0);
2762+
auto C = dyn_cast<ConstantSDNode>(And.getOperand(1));
2763+
2764+
if (!C || !X->hasOneUse())
2765+
return;
2766+
auto Range = getContiguousRangeOfSetBits(C->getAPIntValue());
2767+
if (!Range)
2768+
return;
2769+
2770+
// There are several ways to lower this:
2771+
SDNode *NewN;
2772+
SDLoc dl(N);
2773+
2774+
auto EmitShift = [&](unsigned Opc, SDValue Src, unsigned Imm) -> SDNode* {
2775+
if (Subtarget->isThumb2()) {
2776+
Opc = (Opc == ARM::tLSLri) ? ARM::t2LSLri : ARM::t2LSRri;
2777+
SDValue Ops[] = { Src, CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2778+
getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2779+
CurDAG->getRegister(0, MVT::i32) };
2780+
return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2781+
} else {
2782+
SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
2783+
CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2784+
getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
2785+
return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2786+
}
2787+
};
2788+
2789+
if (Range->second == 0) {
2790+
// 1. Mask includes the LSB -> Simply shift the top N bits off
2791+
NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2792+
ReplaceNode(And.getNode(), NewN);
2793+
} else if (Range->first == 31) {
2794+
// 2. Mask includes the MSB -> Simply shift the bottom N bits off
2795+
NewN = EmitShift(ARM::tLSRri, X, Range->second);
2796+
ReplaceNode(And.getNode(), NewN);
2797+
} else if (Range->first == Range->second) {
2798+
// 3. Only one bit is set. We can shift this into the sign bit and use a
2799+
// PL/MI comparison.
2800+
NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2801+
ReplaceNode(And.getNode(), NewN);
2802+
2803+
SwitchEQNEToPLMI = true;
2804+
} else if (!Subtarget->hasV6T2Ops()) {
2805+
// 4. Do a double shift to clear bottom and top bits, but only in
2806+
// thumb-1 mode as in thumb-2 we can use UBFX.
2807+
NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2808+
NewN = EmitShift(ARM::tLSRri, SDValue(NewN, 0),
2809+
Range->second + (31 - Range->first));
2810+
ReplaceNode(And.getNode(), NewN);
2811+
}
2812+
2813+
}
2814+
27332815
void ARMDAGToDAGISel::Select(SDNode *N) {
27342816
SDLoc dl(N);
27352817

@@ -2957,6 +3039,7 @@ void ARMDAGToDAGISel::Select(SDNode *N) {
29573039
return;
29583040
}
29593041
}
3042+
29603043
break;
29613044
}
29623045
case ARMISD::VMOVRRD:
@@ -3148,9 +3231,27 @@ void ARMDAGToDAGISel::Select(SDNode *N) {
31483231
assert(N2.getOpcode() == ISD::Constant);
31493232
assert(N3.getOpcode() == ISD::Register);
31503233

3151-
SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
3152-
cast<ConstantSDNode>(N2)->getZExtValue()), dl,
3153-
MVT::i32);
3234+
unsigned CC = (unsigned) cast<ConstantSDNode>(N2)->getZExtValue();
3235+
3236+
if (InFlag.getOpcode() == ARMISD::CMPZ) {
3237+
bool SwitchEQNEToPLMI;
3238+
SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
3239+
InFlag = N->getOperand(4);
3240+
3241+
if (SwitchEQNEToPLMI) {
3242+
switch ((ARMCC::CondCodes)CC) {
3243+
default: llvm_unreachable("CMPZ must be either NE or EQ!");
3244+
case ARMCC::NE:
3245+
CC = (unsigned)ARMCC::MI;
3246+
break;
3247+
case ARMCC::EQ:
3248+
CC = (unsigned)ARMCC::PL;
3249+
break;
3250+
}
3251+
}
3252+
}
3253+
3254+
SDValue Tmp2 = CurDAG->getTargetConstant(CC, dl, MVT::i32);
31543255
SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
31553256
SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
31563257
MVT::Glue, Ops);
@@ -3205,6 +3306,38 @@ void ARMDAGToDAGISel::Select(SDNode *N) {
32053306
// Other cases are autogenerated.
32063307
break;
32073308
}
3309+
3310+
case ARMISD::CMOV: {
3311+
SDValue InFlag = N->getOperand(4);
3312+
3313+
if (InFlag.getOpcode() == ARMISD::CMPZ) {
3314+
bool SwitchEQNEToPLMI;
3315+
SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
3316+
3317+
if (SwitchEQNEToPLMI) {
3318+
SDValue ARMcc = N->getOperand(2);
3319+
ARMCC::CondCodes CC =
3320+
(ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
3321+
3322+
switch (CC) {
3323+
default: llvm_unreachable("CMPZ must be either NE or EQ!");
3324+
case ARMCC::NE:
3325+
CC = ARMCC::MI;
3326+
break;
3327+
case ARMCC::EQ:
3328+
CC = ARMCC::PL;
3329+
break;
3330+
}
3331+
SDValue NewARMcc = CurDAG->getConstant((unsigned)CC, dl, MVT::i32);
3332+
SDValue Ops[] = {N->getOperand(0), N->getOperand(1), NewARMcc,
3333+
N->getOperand(3), N->getOperand(4)};
3334+
CurDAG->MorphNodeTo(N, ARMISD::CMOV, N->getVTList(), Ops);
3335+
}
3336+
3337+
}
3338+
// Other cases are autogenerated.
3339+
break;
3340+
}
32083341

32093342
case ARMISD::VZIP: {
32103343
unsigned Opc = 0;

test/CodeGen/ARM/and-cmpz.ll

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; RUN: llc -mtriple=thumbv7m-linux-gnu < %s | FileCheck %s --check-prefix=CHECK --check-prefix=T2
2+
; RUN: llc -mtriple=thumbv6m-linux-gnu < %s | FileCheck %s --check-prefix=CHECK --check-prefix=T1
3+
4+
; CHECK-LABEL: single_bit:
5+
; CHECK: lsls r0, r0, #23
6+
; T2-NEXT: mov
7+
; T2-NEXT: it
8+
; T1-NEXT: bmi
9+
define i32 @single_bit(i32 %p) {
10+
%a = and i32 %p, 256
11+
%b = icmp eq i32 %a, 0
12+
br i1 %b, label %true, label %false
13+
14+
true:
15+
ret i32 1
16+
17+
false:
18+
ret i32 2
19+
}
20+
21+
; CHECK-LABEL: multi_bit_lsb_ubfx:
22+
; CHECK: lsls r0, r0, #24
23+
; T2-NEXT: mov
24+
; T2-NEXT: it
25+
; T1-NEXT: beq
26+
define i32 @multi_bit_lsb_ubfx(i32 %p) {
27+
%a = and i32 %p, 255
28+
%b = icmp eq i32 %a, 0
29+
br i1 %b, label %true, label %false
30+
31+
true:
32+
ret i32 1
33+
34+
false:
35+
ret i32 2
36+
}
37+
38+
; CHECK-LABEL: multi_bit_msb:
39+
; CHECK: lsrs r0, r0, #24
40+
; T2-NEXT: mov
41+
; T2-NEXT: it
42+
; T1-NEXT: beq
43+
define i32 @multi_bit_msb(i32 %p) {
44+
%a = and i32 %p, 4278190080 ; 0xff000000
45+
%b = icmp eq i32 %a, 0
46+
br i1 %b, label %true, label %false
47+
48+
true:
49+
ret i32 1
50+
51+
false:
52+
ret i32 2
53+
}
54+
55+
; CHECK-LABEL: multi_bit_nosb:
56+
; T1: lsls r0, r0, #8
57+
; T1-NEXT: lsrs r0, r0, #24
58+
; T2: tst.w
59+
; T2-NEXT: it
60+
; T1-NEXT: beq
61+
define i32 @multi_bit_nosb(i32 %p) {
62+
%a = and i32 %p, 16711680 ; 0x00ff0000
63+
%b = icmp eq i32 %a, 0
64+
br i1 %b, label %true, label %false
65+
66+
true:
67+
ret i32 1
68+
69+
false:
70+
ret i32 2
71+
}

test/CodeGen/ARM/arm-and-tst-peephole.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ entry:
9393
%1 = load i8, i8* %0, align 1
9494
%2 = zext i8 %1 to i32
9595
; ARM: ands
96-
; THUMB: ands
96+
; THUMB: ands
9797
; T2: ands
9898
; V8: ands
9999
; V8-NEXT: beq
@@ -150,10 +150,9 @@ define i32 @test_tst_assessment(i1 %lhs, i1 %rhs) {
150150
%rhs32 = zext i1 %rhs to i32
151151
%diff = sub nsw i32 %lhs32, %rhs32
152152
; ARM: tst r1, #1
153-
; THUMB: movs [[RTMP:r[0-9]+]], #1
154-
; THUMB: tst r1, [[RTMP]]
155-
; T2: tst.w r1, #1
156-
; V8: tst.w r1, #1
153+
; THUMB: lsls r1, r1, #31
154+
; T2: lsls r1, r1, #31
155+
; V8: lsls r1, r1, #31
157156
ret i32 %diff
158157
}
159158

test/CodeGen/ARM/arm-shrink-wrapping.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,14 +638,13 @@ declare double @llvm.pow.f64(double, double)
638638
; during PEI with shrink-wrapping enable.
639639
; CHECK-LABEL: debug_info:
640640
;
641-
; ENABLE: tst{{(\.w)?}} r2, #1
641+
; ENABLE: {{tst r2, #1|lsls r1, r2, #31}}
642642
; ENABLE-NEXT: beq [[BB13:LBB[0-9_]+]]
643643
;
644644
; CHECK: push
645645
;
646-
; DISABLE: tst{{(\.w)?}} r2, #1
647-
; DISABLE-NEXT: vst1.64
648-
; DISABLE-NEXT: beq [[BB13:LBB[0-9_]+]]
646+
; DISABLE: {{tst r2, #1|lsls r1, r2, #31}}
647+
; DISABLE: beq [[BB13:LBB[0-9_]+]]
649648
;
650649
; CHECK: bl{{x?}} _pow
651650
;

test/CodeGen/ARM/call-tc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ if.end: ; preds = %entry
120120
br i1 %tobool2, label %if.end5, label %if.then3
121121

122122
if.then3: ; preds = %if.end
123-
; CHECKT2D: bne.w _b
123+
; CHECKT2D: bmi.w _b
124124
%call4 = tail call i32 @b(i32 %x) nounwind
125125
br label %return
126126

test/CodeGen/ARM/debug-info-branch-folding.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:32-
33
target triple = "thumbv7-apple-macosx10.6.7"
44

55
;CHECK: vadd.f32 q4, q8, q8
6-
;CHECK: LBB0_1:
7-
;CHECK-NOT: beq LBB0_1
6+
;CHECK-NEXT: LBB0_1
87

98
;CHECK: @DEBUG_VALUE: x <- %Q4{{$}}
109
;CHECK-NEXT: @DEBUG_VALUE: y <- %Q4{{$}}

test/CodeGen/Thumb/thumb-shrink-wrapping.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,14 @@ define i1 @beq_to_bx(i32* %y, i32 %head) {
650650

651651
; CHECK: tst r3, r4
652652
; ENABLE-NEXT: pop {r4}
653-
; ENABLE-NEXT: pop {r3}
654-
; ENABLE-NEXT: mov lr, r3
653+
; ENABLE-NEXT: mov r12, r{{.*}}
654+
; ENABLE-NEXT: pop {r0}
655+
; ENABLE-NEXT: mov lr, r0
656+
; ENABLE-NEXT: mov r0, r12
655657
; CHECK-NEXT: beq [[EXIT_LABEL]]
656658

657659
; CHECK: str r1, [r2]
660+
; CHECK: str r3, [r2]
658661
; CHECK-NEXT: movs r0, #0
659662
; CHECK-NEXT: [[EXIT_LABEL]]: @ %cleanup
660663
; ENABLE-NEXT: bx lr
@@ -675,6 +678,7 @@ if.end:
675678

676679
if.end4:
677680
store i32 %head, i32* %y, align 4
681+
store volatile i32 %z, i32* %y, align 4
678682
br label %cleanup
679683

680684
cleanup:

test/CodeGen/Thumb2/float-ops.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ define i64 @bitcast_d_to_i(double %a) {
259259

260260
define float @select_f(float %a, float %b, i1 %c) {
261261
; CHECK-LABEL: select_f:
262-
; NONE: tst.w r2, #1
262+
; NONE: lsls r2, r2, #31
263263
; NONE: moveq r0, r1
264-
; HARD: tst.w r0, #1
264+
; HARD: lsls r0, r0, #31
265265
; VFP4-ALL: vmovne.f32 s1, s0
266266
; VFP4-ALL: vmov.f32 s0, s1
267267
; FP-ARMv8: vseleq.f32 s0, s1, s0
@@ -271,8 +271,8 @@ define float @select_f(float %a, float %b, i1 %c) {
271271

272272
define double @select_d(double %a, double %b, i1 %c) {
273273
; CHECK-LABEL: select_d:
274-
; NONE: ldr.w [[REG:r[0-9]+]], [sp]
275-
; NONE: ands [[REG]], [[REG]], #1
274+
; NONE: ldr{{(.w)?}} [[REG:r[0-9]+]], [sp]
275+
; NONE ands [[REG]], [[REG]], #1
276276
; NONE: moveq r0, r2
277277
; NONE: moveq r1, r3
278278
; SP: ands r0, r0, #1
@@ -282,7 +282,7 @@ define double @select_d(double %a, double %b, i1 %c) {
282282
; SP-DAG: movne [[BLO]], [[ALO]]
283283
; SP-DAG: movne [[BHI]], [[AHI]]
284284
; SP: vmov d0, [[BLO]], [[BHI]]
285-
; DP: tst.w r0, #1
285+
; DP: lsls r0, r0, #31
286286
; VFP4-DP: vmovne.f64 d1, d0
287287
; VFP4-DP: vmov.f64 d0, d1
288288
; FP-ARMV8: vseleq.f64 d0, d1, d0

0 commit comments

Comments
 (0)