Skip to content

EarlyCSE: fix CmpPredicate duplicate-hashing #119902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions llvm/include/llvm/IR/CmpPredicate.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ class CmpPredicate {

/// Get the swapped predicate of a CmpInst.
static CmpPredicate getSwapped(const CmpInst *Cmp);

/// Provided to facilitate storing a CmpPredicate in data structures that
/// require hashing.
friend hash_code hash_value(const CmpPredicate &Arg); // NOLINT
};

[[nodiscard]] hash_code hash_value(const CmpPredicate &Arg);
} // namespace llvm

#endif
4 changes: 0 additions & 4 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3946,10 +3946,6 @@ CmpPredicate CmpPredicate::getSwapped(const CmpInst *Cmp) {
return getSwapped(get(Cmp));
}

hash_code llvm::hash_value(const CmpPredicate &Arg) { // NOLINT
return hash_combine(Arg.Pred, Arg.HasSameSign);
}

//===----------------------------------------------------------------------===//
// SwitchInst Implementation
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/EarlyCSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ static unsigned getHashValueImpl(SimpleValue Val) {
Pred = CmpInst::getInversePredicate(Pred);
std::swap(A, B);
}
return hash_combine(Inst->getOpcode(), Pred, X, Y, A, B);
return hash_combine(Inst->getOpcode(),
static_cast<CmpInst::Predicate>(Pred), X, Y, A, B);
}

if (CastInst *CI = dyn_cast<CastInst>(Inst))
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/Transforms/EarlyCSE/pr119893.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes=early-cse -S %s | FileCheck %s

define i32 @samesign_hash_bug(i16 %v) {
; CHECK-LABEL: define i32 @samesign_hash_bug(
; CHECK-SAME: i16 [[V:%.*]]) {
; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[V]] to i32
; CHECK-NEXT: [[ICMP_SAMESIGN:%.*]] = icmp ugt i32 [[ZEXT]], 31
; CHECK-NEXT: [[SELECT_ICMP_SAMESIGN:%.*]] = select i1 [[ICMP_SAMESIGN]], i32 0, i32 1
; CHECK-NEXT: [[SELECT_ICMP:%.*]] = select i1 [[ICMP_SAMESIGN]], i32 1, i32 0
; CHECK-NEXT: ret i32 [[SELECT_ICMP]]
;
%zext = zext i16 %v to i32
%icmp.samesign = icmp samesign ugt i32 %zext, 31
%select.icmp.samesign = select i1 %icmp.samesign, i32 0, i32 1
%ashr = ashr i32 0, %select.icmp.samesign
%icmp = icmp ugt i32 %zext, 31
%select.icmp = select i1 %icmp, i32 1, i32 0
%ret = add i32 %ashr, %select.icmp
ret i32 %ret
}
Loading