llvm.org GIT mirror llvm / 9114c43
Merge 64314 from mainline. Make a transformation added in 63266 a bit less aggressive. It was transforming (x&y)==y to (x&y)!=0 in the case where y is variable and known to have at most one bit set (e.g. z&1). This is not correct; the expressions are not equivalent when y==0. I believe this patch salvages what can be salvaged, including all the cases in bt.ll. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_25@64782 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 11 years ago
2 changed file(s) with 79 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
13451345 return 1;
13461346 }
13471347
1348 static bool ValueHasAtMostOneBitSet(SDValue Val, const SelectionDAG &DAG) {
1348 static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
13491349 // Logical shift right or left won't ever introduce new set bits.
13501350 // We check for this case because we don't care which bits are
13511351 // set, but ComputeMaskedBits won't know anything unless it can
13521352 // determine which specific bits may be set.
13531353 if (Val.getOpcode() == ISD::SHL || Val.getOpcode() == ISD::SRL)
1354 return ValueHasAtMostOneBitSet(Val.getOperand(0), DAG);
1354 return ValueHasExactlyOneBitSet(Val.getOperand(0), DAG);
13551355
13561356 MVT OpVT = Val.getValueType();
13571357 unsigned BitWidth = OpVT.getSizeInBits();
13581358 APInt Mask = APInt::getAllOnesValue(BitWidth);
13591359 APInt KnownZero, KnownOne;
13601360 DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
1361 return KnownZero.countPopulation() == BitWidth - 1;
1361 return (KnownZero.countPopulation() == BitWidth - 1) &&
1362 (KnownOne.countPopulation() == 1);
13621363 }
13631364
13641365 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
18311832 }
18321833
18331834 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
1835 // Note that where y is variable and is known to have at most
1836 // one bit set (for example, if it is z&1) we cannot do this;
1837 // the expressions are not equivalent when y==0.
18341838 if (N0.getOpcode() == ISD::AND)
18351839 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
1836 if (ValueHasAtMostOneBitSet(N1, DAG)) {
1840 if (ValueHasExactlyOneBitSet(N1, DAG)) {
18371841 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
18381842 SDValue Zero = DAG.getConstant(0, N1.getValueType());
18391843 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
18411845 }
18421846 if (N1.getOpcode() == ISD::AND)
18431847 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
1844 if (ValueHasAtMostOneBitSet(N0, DAG)) {
1848 if (ValueHasExactlyOneBitSet(N0, DAG)) {
18451849 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
18461850 SDValue Zero = DAG.getConstant(0, N0.getValueType());
18471851 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
0 ; RUN: llvm-as < %s | llc -march=x86 | not grep btl
1
2 ; This tests some cases where BT must not be generated. See also bt.ll.
3 ; Fixes 20040709-[12].c in gcc testsuite.
4
5 define void @test2(i32 %x, i32 %n) nounwind {
6 entry:
7 %tmp1 = and i32 %x, 1
8 %tmp2 = urem i32 %tmp1, 15
9 %tmp3 = and i32 %tmp2, 1 ; [#uses=1]
10 %tmp4 = icmp eq i32 %tmp3, %tmp2 ; [#uses=1]
11 br i1 %tmp4, label %bb, label %UnifiedReturnBlock
12
13 bb: ; preds = %entry
14 call void @foo()
15 ret void
16
17 UnifiedReturnBlock: ; preds = %entry
18 ret void
19 }
20
21 define void @test3(i32 %x, i32 %n) nounwind {
22 entry:
23 %tmp1 = and i32 %x, 1
24 %tmp2 = urem i32 %tmp1, 15
25 %tmp3 = and i32 %tmp2, 1 ; [#uses=1]
26 %tmp4 = icmp eq i32 %tmp2, %tmp3 ; [#uses=1]
27 br i1 %tmp4, label %bb, label %UnifiedReturnBlock
28
29 bb: ; preds = %entry
30 call void @foo()
31 ret void
32
33 UnifiedReturnBlock: ; preds = %entry
34 ret void
35 }
36
37 define void @test4(i32 %x, i32 %n) nounwind {
38 entry:
39 %tmp1 = and i32 %x, 1
40 %tmp2 = urem i32 %tmp1, 15
41 %tmp3 = and i32 %tmp2, 1 ; [#uses=1]
42 %tmp4 = icmp ne i32 %tmp2, %tmp3 ; [#uses=1]
43 br i1 %tmp4, label %bb, label %UnifiedReturnBlock
44
45 bb: ; preds = %entry
46 call void @foo()
47 ret void
48
49 UnifiedReturnBlock: ; preds = %entry
50 ret void
51 }
52
53 define void @test5(i32 %x, i32 %n) nounwind {
54 entry:
55 %tmp1 = and i32 %x, 1
56 %tmp2 = urem i32 %tmp1, 15
57 %tmp3 = and i32 %tmp2, 1 ; [#uses=1]
58 %tmp4 = icmp ne i32 %tmp2, %tmp3 ; [#uses=1]
59 br i1 %tmp4, label %bb, label %UnifiedReturnBlock
60
61 bb: ; preds = %entry
62 call void @foo()
63 ret void
64
65 UnifiedReturnBlock: ; preds = %entry
66 ret void
67 }
68
69 declare void @foo()