Skip to content

Commit daa1ae6

Browse files
committedJun 17, 2019
[EarlyCSE] Fix hashing of self-compares
Summary: Update compare normalization in SimpleValue hashing to break ties (when the same value is being compared to itself) by switching to the swapped predicate if it has a lower numerical value. This brings the hashing in line with isEqual, which already recognizes the self-compares with swapped predicates as equal. Fixes PR 42280. Reviewers: spatel, efriedma, nikic, fhahn, uabelho Reviewed By: nikic Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63349 llvm-svn: 363598
1 parent 7a0098a commit daa1ae6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

Diff for: ‎llvm/lib/Transforms/Scalar/EarlyCSE.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,17 @@ static unsigned getHashValueImpl(SimpleValue Val) {
174174
}
175175

176176
if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
177+
// Compares can be commuted by swapping the comparands and
178+
// updating the predicate. Choose the form that has the
179+
// comparands in sorted order, or in the case of a tie, the
180+
// one with the lower predicate.
177181
Value *LHS = CI->getOperand(0);
178182
Value *RHS = CI->getOperand(1);
179183
CmpInst::Predicate Pred = CI->getPredicate();
180-
if (Inst->getOperand(0) > Inst->getOperand(1)) {
184+
CmpInst::Predicate SwappedPred = CI->getSwappedPredicate();
185+
if (std::tie(LHS, Pred) > std::tie(RHS, SwappedPred)) {
181186
std::swap(LHS, RHS);
182-
Pred = CI->getSwappedPredicate();
187+
Pred = SwappedPred;
183188
}
184189
return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
185190
}

Diff for: ‎llvm/test/Transforms/EarlyCSE/commute.ll

+16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ define void @test5(i32 %A, i32 %B, i1* %PA, i1* %PB) {
7272
ret void
7373
}
7474

75+
; Test degenerate case of commuted compare of identical comparands.
76+
77+
define void @test6(float %f, i1* %p1, i1* %p2) {
78+
; CHECK-LABEL: @test6(
79+
; CHECK-NEXT: [[C1:%.*]] = fcmp ult float [[F:%.*]], [[F]]
80+
; CHECK-NEXT: store i1 [[C1]], i1* [[P1:%.*]]
81+
; CHECK-NEXT: store i1 [[C1]], i1* [[P2:%.*]]
82+
; CHECK-NEXT: ret void
83+
;
84+
%c1 = fcmp ult float %f, %f
85+
%c2 = fcmp ugt float %f, %f
86+
store i1 %c1, i1* %p1
87+
store i1 %c2, i1* %p2
88+
ret void
89+
}
90+
7591
; Min/max operands may be commuted in the compare and select.
7692

7793
define i8 @smin_commute(i8 %a, i8 %b) {

0 commit comments

Comments
 (0)
Please sign in to comment.