llvm.org GIT mirror llvm / 5cbbcbf
[EarlyCSE] recognize swapped variants of abs/nabs as equivalent Extends https://reviews.llvm.org/rL320640 Differential Revision: https://reviews.llvm.org/D41136 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320653 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 1 year, 9 months ago
2 changed file(s) with 14 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
142142 return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
143143 }
144144
145 // Hash min/max (cmp + select) to allow for commuted operands, non-canonical
146 // compare predicate (eg, the compare for smin may use 'sgt' rather than
147 // 'slt'), and non-canonical operands in the compare.
145 // Hash min/max/abs (cmp + select) to allow for commuted operands.
146 // Min/max may also have non-canonical compare predicate (eg, the compare for
147 // smin may use 'sgt' rather than 'slt'), and non-canonical operands in the
148 // compare.
148149 Value *A, *B;
149150 SelectPatternFlavor SPF = matchSelectPattern(Inst, A, B).Flavor;
150 // TODO: We should also detect abs and FP min/max.
151 // TODO: We should also detect FP min/max.
151152 if (SPF == SPF_SMIN || SPF == SPF_SMAX ||
152 SPF == SPF_UMIN || SPF == SPF_UMAX) {
153 SPF == SPF_UMIN || SPF == SPF_UMAX ||
154 SPF == SPF_ABS || SPF == SPF_NABS) {
153155 if (A > B)
154156 std::swap(A, B);
155157 return hash_combine(Inst->getOpcode(), SPF, A, B);
213215 LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
214216 }
215217
216 // Min/max can occur with commuted operands, non-canonical predicates, and/or
217 // non-canonical operands.
218 // Min/max/abs can occur with commuted operands, non-canonical predicates,
219 // and/or non-canonical operands.
218220 Value *LHSA, *LHSB;
219221 SelectPatternFlavor LSPF = matchSelectPattern(LHSI, LHSA, LHSB).Flavor;
220 // TODO: We should also detect abs and FP min/max.
222 // TODO: We should also detect FP min/max.
221223 if (LSPF == SPF_SMIN || LSPF == SPF_SMAX ||
222 LSPF == SPF_UMIN || LSPF == SPF_UMAX) {
224 LSPF == SPF_UMIN || LSPF == SPF_UMAX ||
225 LSPF == SPF_ABS || LSPF == SPF_NABS) {
223226 Value *RHSA, *RHSB;
224227 SelectPatternFlavor RSPF = matchSelectPattern(RHSI, RHSA, RHSB).Flavor;
225228 return (LSPF == RSPF && ((LHSA == RHSA && LHSB == RHSB) ||
223223 ; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 %a, 0
224224 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 %a, 0
225225 ; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 %a, i8 [[NEG]]
226 ; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 %a
227 ; CHECK-NEXT: [[R:%.*]] = or i8 [[M2]], [[M1]]
228 ; CHECK-NEXT: ret i8 [[R]]
226 ; CHECK-NEXT: ret i8 [[M1]]
229227 ;
230228 %neg = sub i8 0, %a
231229 %cmp1 = icmp sgt i8 %a, 0
242240 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 %a, 0
243241 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 %a, 0
244242 ; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 %a, i8 [[NEG]]
245 ; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 %a
246 ; CHECK-NEXT: [[R:%.*]] = xor i8 [[M2]], [[M1]]
247 ; CHECK-NEXT: ret i8 [[R]]
243 ; CHECK-NEXT: ret i8 0
248244 ;
249245 %neg = sub i8 0, %a
250246 %cmp1 = icmp slt i8 %a, 0