llvm.org GIT mirror llvm / 33ec780
[InstCombine] look through bitcasts to find selects There was concern that creating bitcasts for the simpler potential select pattern: define <2 x i64> @vecBitcastOp1(<4 x i1> %cmp, <2 x i64> %a) { %a2 = add <2 x i64> %a, %a %sext = sext <4 x i1> %cmp to <4 x i32> %bc = bitcast <4 x i32> %sext to <2 x i64> %and = and <2 x i64> %a2, %bc ret <2 x i64> %and } might lead to worse code for some targets, so this patch is matching the larger patterns seen in the test cases. The motivating example for this patch is this IR produced via SSE intrinsics in C: define <2 x i64> @gibson(<2 x i64> %a, <2 x i64> %b) { %t0 = bitcast <2 x i64> %a to <4 x i32> %t1 = bitcast <2 x i64> %b to <4 x i32> %cmp = icmp sgt <4 x i32> %t0, %t1 %sext = sext <4 x i1> %cmp to <4 x i32> %t2 = bitcast <4 x i32> %sext to <2 x i64> %and = and <2 x i64> %t2, %a %neg = xor <4 x i32> %sext, <i32 -1, i32 -1, i32 -1, i32 -1> %neg2 = bitcast <4 x i32> %neg to <2 x i64> %and2 = and <2 x i64> %neg2, %b %or = or <2 x i64> %and, %and2 ret <2 x i64> %or } For an AVX target, this is currently: vpcmpgtd %xmm1, %xmm0, %xmm2 vpand %xmm0, %xmm2, %xmm0 vpandn %xmm1, %xmm2, %xmm1 vpor %xmm1, %xmm0, %xmm0 retq With this patch, it becomes: vpmaxsd %xmm1, %xmm0, %xmm0 Differential Revision: http://reviews.llvm.org/D20774 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271676 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 4 years ago
2 changed file(s) with 68 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
16401640 /// We have an expression of the form (A&C)|(B&D). Check if A is (cond?-1:0)
16411641 /// and either B or D is ~(cond?-1,0) or (cond?0,-1), then we can simplify this
16421642 /// expression to "cond ? C : D or B".
1643 static Instruction *matchSelectFromAndOr(Value *A, Value *B,
1644 Value *C, Value *D) {
1643 static Instruction *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D,
1644 InstCombiner::BuilderTy &Builder) {
16451645 // If A is not a select of -1/0, this cannot match.
16461646 Value *Cond = nullptr;
1647 if (!match(A, m_SExt(m_Value(Cond))) || !Cond->getType()->isIntegerTy(1))
1648 return nullptr;
1649
1650 // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
1651 if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
1652 return SelectInst::Create(Cond, C, B);
1653 if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
1654 return SelectInst::Create(Cond, C, B);
1655
1656 // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
1657 if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
1658 return SelectInst::Create(Cond, C, D);
1659 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
1660 return SelectInst::Create(Cond, C, D);
1647 if (match(A, m_SExt(m_Value(Cond))) &&
1648 Cond->getType()->getScalarType()->isIntegerTy(1)) {
1649
1650 // ((cond ? -1:0) & C) | (B & (cond ? 0:-1)) -> cond ? C : B.
1651 if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
1652 return SelectInst::Create(Cond, C, B);
1653 if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
1654 return SelectInst::Create(Cond, C, B);
1655
1656 // ((cond ? -1:0) & C) | ((cond ? 0:-1) & D) -> cond ? C : D.
1657 if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
1658 return SelectInst::Create(Cond, C, D);
1659 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
1660 return SelectInst::Create(Cond, C, D);
1661 }
1662
1663 // TODO: Refactor the pattern matching above and below so there's less code.
1664
1665 // The sign-extended boolean condition may be hiding behind a bitcast. In that
1666 // case, look for the same patterns as above. However, we need to bitcast the
1667 // input operands to the select and bitcast the output of the select to match
1668 // the expected types.
1669 if (match(A, m_BitCast(m_SExt(m_Value(Cond)))) &&
1670 Cond->getType()->getScalarType()->isIntegerTy(1)) {
1671
1672 Type *SrcType = cast(A)->getSrcTy();
1673
1674 // ((bc Cond) & C) | (B & (bc ~Cond)) --> bc (select Cond, (bc C), (bc B))
1675 if (match(D, m_CombineOr(m_BitCast(m_Not(m_SExt(m_Specific(Cond)))),
1676 m_BitCast(m_SExt(m_Not(m_Specific(Cond))))))) {
1677 Value *BitcastC = Builder.CreateBitCast(C, SrcType);
1678 Value *BitcastB = Builder.CreateBitCast(B, SrcType);
1679 Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastB);
1680 return CastInst::Create(Instruction::BitCast, Select, A->getType());
1681 }
1682
1683 // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
1684 if (match(B, m_CombineOr(m_BitCast(m_Not(m_SExt(m_Specific(Cond)))),
1685 m_BitCast(m_SExt(m_Not(m_Specific(Cond))))))) {
1686 Value *BitcastC = Builder.CreateBitCast(C, SrcType);
1687 Value *BitcastD = Builder.CreateBitCast(D, SrcType);
1688 Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
1689 return CastInst::Create(Instruction::BitCast, Select, A->getType());
1690 }
1691 }
16611692
16621693 return nullptr;
16631694 }
22552286 }
22562287
22572288 // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants.
2258 if (Instruction *Match = matchSelectFromAndOr(A, B, C, D))
2289 if (Instruction *Match = matchSelectFromAndOr(A, B, C, D, *Builder))
22592290 return Match;
2260 if (Instruction *Match = matchSelectFromAndOr(B, A, D, C))
2291 if (Instruction *Match = matchSelectFromAndOr(B, A, D, C, *Builder))
22612292 return Match;
2262 if (Instruction *Match = matchSelectFromAndOr(C, B, A, D))
2293 if (Instruction *Match = matchSelectFromAndOr(C, B, A, D, *Builder))
22632294 return Match;
2264 if (Instruction *Match = matchSelectFromAndOr(D, A, B, C))
2295 if (Instruction *Match = matchSelectFromAndOr(D, A, B, C, *Builder))
22652296 return Match;
22662297
22672298 // ((A&~B)|(~A&B)) -> A^B
7676 ret i32 %t3
7777 }
7878
79 ; FIXME: In the following tests, verify that a bitcast doesn't get in the way
79 ; In the following tests, verify that a bitcast doesn't get in the way
8080 ; of a select transform. These bitcasts are common in SSE/AVX and possibly
8181 ; other vector code because of canonicalization to i64 elements for vectors.
8282
8383 define <2 x i64> @bitcast_select(<4 x i1> %cmp, <2 x i64> %a, <2 x i64> %b) {
8484 ; CHECK-LABEL: @bitcast_select(
85 ; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> %cmp to <4 x i32>
86 ; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i32> [[SEXT]] to <2 x i64>
87 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[T2]], %a
88 ; CHECK-NEXT: [[NEG:%.*]] = xor <4 x i32> [[SEXT]],
89 ; CHECK-NEXT: [[NEG2:%.*]] = bitcast <4 x i32> [[NEG]] to <2 x i64>
90 ; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[NEG2]], %b
91 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[AND]], [[AND2]]
85 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> %a to <4 x i32>
86 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> %b to <4 x i32>
87 ; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> %cmp, <4 x i32> [[TMP1]], <4 x i32> [[TMP2]]
88 ; CHECK-NEXT: [[OR:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64>
9289 ; CHECK-NEXT: ret <2 x i64> [[OR]]
9390 ;
9491 %sext = sext <4 x i1> %cmp to <4 x i32>
103100
104101 define <2 x i64> @bitcast_select_swap_or_ops(<4 x i1> %cmp, <2 x i64> %a, <2 x i64> %b) {
105102 ; CHECK-LABEL: @bitcast_select_swap_or_ops(
106 ; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> %cmp to <4 x i32>
107 ; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i32> [[SEXT]] to <2 x i64>
108 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[T2]], %a
109 ; CHECK-NEXT: [[NEG:%.*]] = xor <4 x i32> [[SEXT]],
110 ; CHECK-NEXT: [[NEG2:%.*]] = bitcast <4 x i32> [[NEG]] to <2 x i64>
111 ; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[NEG2]], %b
112 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[AND2]], [[AND]]
103 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> %a to <4 x i32>
104 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> %b to <4 x i32>
105 ; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> %cmp, <4 x i32> [[TMP1]], <4 x i32> [[TMP2]]
106 ; CHECK-NEXT: [[OR:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64>
113107 ; CHECK-NEXT: ret <2 x i64> [[OR]]
114108 ;
115109 %sext = sext <4 x i1> %cmp to <4 x i32>
124118
125119 define <2 x i64> @bitcast_select_swap_and_ops(<4 x i1> %cmp, <2 x i64> %a, <2 x i64> %b) {
126120 ; CHECK-LABEL: @bitcast_select_swap_and_ops(
127 ; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> %cmp to <4 x i32>
128 ; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i32> [[SEXT]] to <2 x i64>
129 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[T2]], %a
130 ; CHECK-NEXT: [[NEG:%.*]] = xor <4 x i32> [[SEXT]],
131 ; CHECK-NEXT: [[NEG2:%.*]] = bitcast <4 x i32> [[NEG]] to <2 x i64>
132 ; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[NEG2]], %b
133 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[AND]], [[AND2]]
121 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> %a to <4 x i32>
122 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> %b to <4 x i32>
123 ; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> %cmp, <4 x i32> [[TMP1]], <4 x i32> [[TMP2]]
124 ; CHECK-NEXT: [[OR:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64>
134125 ; CHECK-NEXT: ret <2 x i64> [[OR]]
135126 ;
136127 %sext = sext <4 x i1> %cmp to <4 x i32>
145136
146137 define <2 x i64> @bitcast_select_swap_and_ops2(<4 x i1> %cmp, <2 x i64> %a, <2 x i64> %b) {
147138 ; CHECK-LABEL: @bitcast_select_swap_and_ops2(
148 ; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> %cmp to <4 x i32>
149 ; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i32> [[SEXT]] to <2 x i64>
150 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[T2]], %a
151 ; CHECK-NEXT: [[NEG:%.*]] = xor <4 x i32> [[SEXT]],
152 ; CHECK-NEXT: [[NEG2:%.*]] = bitcast <4 x i32> [[NEG]] to <2 x i64>
153 ; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[NEG2]], %b
154 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[AND]], [[AND2]]
139 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> %a to <4 x i32>
140 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> %b to <4 x i32>
141 ; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> %cmp, <4 x i32> [[TMP1]], <4 x i32> [[TMP2]]
142 ; CHECK-NEXT: [[OR:%.*]] = bitcast <4 x i32> [[TMP3]] to <2 x i64>
155143 ; CHECK-NEXT: ret <2 x i64> [[OR]]
156144 ;
157145 %sext = sext <4 x i1> %cmp to <4 x i32>