Skip to content

Commit 9cd83d6

Browse files
authored
[InstCombine] Drop samesign in foldLogOpOfMaskedICmps (#125829)
Alive2: https://alive2.llvm.org/ce/z/6zLAYp Note: We can also apply this fix to the logic below (`if (Mask & AMask_NotAllOnes)`), but it seems unreachable.
1 parent 50cdf6c commit 9cd83d6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,13 @@ static Value *foldLogOpOfMaskedICmps(Value *LHS, Value *RHS, bool IsAnd,
610610
APInt NewMask = *ConstB & *ConstD;
611611
if (NewMask == *ConstB)
612612
return LHS;
613-
if (NewMask == *ConstD)
613+
if (NewMask == *ConstD) {
614+
if (IsLogical) {
615+
if (auto *RHSI = dyn_cast<Instruction>(RHS))
616+
RHSI->dropPoisonGeneratingFlags();
617+
}
614618
return RHS;
619+
}
615620
}
616621

617622
if (Mask & AMask_NotAllOnes) {

llvm/test/Transforms/InstCombine/sign-test-and-or.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,29 @@ define i1 @test9_logical(i32 %a) {
349349
ret i1 %or.cond
350350
}
351351

352+
define i1 @test9_logical_samesign(i32 %a) {
353+
; CHECK-LABEL: @test9_logical_samesign(
354+
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[A:%.*]], -1
355+
; CHECK-NEXT: ret i1 [[CMP2]]
356+
;
357+
%masked = and i32 %a, -1073741825
358+
%cmp1 = icmp eq i32 %masked, 0
359+
%cmp2 = icmp samesign sgt i32 %a, -1
360+
%or.cond = select i1 %cmp1, i1 true, i1 %cmp2
361+
ret i1 %or.cond
362+
}
363+
364+
define i1 @test_logical_or_icmp_icmp_samesign(i32 %a) {
365+
; CHECK-LABEL: @test_logical_or_icmp_icmp_samesign(
366+
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[A:%.*]], -1
367+
; CHECK-NEXT: ret i1 [[CMP2]]
368+
;
369+
%cmp1 = icmp eq i32 %a, 0
370+
%cmp2 = icmp samesign sgt i32 %a, -1
371+
%or = select i1 %cmp1, i1 true, i1 %cmp2
372+
ret i1 %or
373+
}
374+
352375
define i1 @test10(i32 %a) {
353376
; CHECK-LABEL: @test10(
354377
; CHECK-NEXT: [[OR_COND:%.*]] = icmp ult i32 [[A:%.*]], 2

0 commit comments

Comments
 (0)