Skip to content

Commit

Permalink
[EarlyCSE] recognize swapped variants of abs/nabs as equivalent
Browse files Browse the repository at this point in the history
Extends https://reviews.llvm.org/rL320640

Differential Revision: https://reviews.llvm.org/D41136

llvm-svn: 320653
  • Loading branch information
rotateright committed Dec 13, 2017
1 parent d44a81c commit 558a465
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
21 changes: 12 additions & 9 deletions llvm/lib/Transforms/Scalar/EarlyCSE.cpp
Expand Up @@ -143,14 +143,16 @@ unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
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);
Expand Down Expand Up @@ -214,13 +216,14 @@ bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
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) ||
Expand Down
8 changes: 2 additions & 6 deletions llvm/test/Transforms/EarlyCSE/commute.ll
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 558a465

Please sign in to comment.