[llvm] r320653 - [EarlyCSE] recognize swapped variants of abs/nabs as equivalent
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 13 14:57:35 PST 2017
Author: spatel
Date: Wed Dec 13 14:57:35 2017
New Revision: 320653
URL: http://llvm.org/viewvc/llvm-project?rev=320653&view=rev
Log:
[EarlyCSE] recognize swapped variants of abs/nabs as equivalent
Extends https://reviews.llvm.org/rL320640
Differential Revision: https://reviews.llvm.org/D41136
Modified:
llvm/trunk/lib/Transforms/Scalar/EarlyCSE.cpp
llvm/trunk/test/Transforms/EarlyCSE/commute.ll
Modified: llvm/trunk/lib/Transforms/Scalar/EarlyCSE.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/EarlyCSE.cpp?rev=320653&r1=320652&r2=320653&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/EarlyCSE.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/EarlyCSE.cpp Wed Dec 13 14:57:35 2017
@@ -143,14 +143,16 @@ unsigned DenseMapInfo<SimpleValue>::getH
return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
}
- // Hash min/max (cmp + select) to allow for commuted operands, non-canonical
- // compare predicate (eg, the compare for smin may use 'sgt' rather than
- // 'slt'), and non-canonical operands in the compare.
+ // Hash min/max/abs (cmp + select) to allow for commuted operands.
+ // Min/max may also have non-canonical compare predicate (eg, the compare for
+ // smin may use 'sgt' rather than 'slt'), and non-canonical operands in the
+ // compare.
Value *A, *B;
SelectPatternFlavor SPF = matchSelectPattern(Inst, A, B).Flavor;
- // TODO: We should also detect abs and FP min/max.
+ // TODO: We should also detect FP min/max.
if (SPF == SPF_SMIN || SPF == SPF_SMAX ||
- SPF == SPF_UMIN || SPF == SPF_UMAX) {
+ SPF == SPF_UMIN || SPF == SPF_UMAX ||
+ SPF == SPF_ABS || SPF == SPF_NABS) {
if (A > B)
std::swap(A, B);
return hash_combine(Inst->getOpcode(), SPF, A, B);
@@ -214,13 +216,14 @@ bool DenseMapInfo<SimpleValue>::isEqual(
LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
}
- // Min/max can occur with commuted operands, non-canonical predicates, and/or
- // non-canonical operands.
+ // Min/max/abs can occur with commuted operands, non-canonical predicates,
+ // and/or non-canonical operands.
Value *LHSA, *LHSB;
SelectPatternFlavor LSPF = matchSelectPattern(LHSI, LHSA, LHSB).Flavor;
- // TODO: We should also detect abs and FP min/max.
+ // TODO: We should also detect FP min/max.
if (LSPF == SPF_SMIN || LSPF == SPF_SMAX ||
- LSPF == SPF_UMIN || LSPF == SPF_UMAX) {
+ LSPF == SPF_UMIN || LSPF == SPF_UMAX ||
+ LSPF == SPF_ABS || LSPF == SPF_NABS) {
Value *RHSA, *RHSB;
SelectPatternFlavor RSPF = matchSelectPattern(RHSI, RHSA, RHSB).Flavor;
return (LSPF == RSPF && ((LHSA == RHSA && LHSB == RHSB) ||
Modified: llvm/trunk/test/Transforms/EarlyCSE/commute.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/EarlyCSE/commute.ll?rev=320653&r1=320652&r2=320653&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/EarlyCSE/commute.ll (original)
+++ llvm/trunk/test/Transforms/EarlyCSE/commute.ll Wed Dec 13 14:57:35 2017
@@ -224,9 +224,7 @@ define i8 @abs_swapped(i8 %a) {
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 %a, 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 %a, 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 %a, i8 [[NEG]]
-; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 %a
-; CHECK-NEXT: [[R:%.*]] = or i8 [[M2]], [[M1]]
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[M1]]
;
%neg = sub i8 0, %a
%cmp1 = icmp sgt i8 %a, 0
@@ -243,9 +241,7 @@ define i8 @nabs_swapped(i8 %a) {
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 %a, 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 %a, 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 %a, i8 [[NEG]]
-; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 %a
-; CHECK-NEXT: [[R:%.*]] = xor i8 [[M2]], [[M1]]
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 0
;
%neg = sub i8 0, %a
%cmp1 = icmp slt i8 %a, 0
More information about the llvm-commits
mailing list