Skip to content

Commit 0a39c88

Browse files
authored
[InstCombine] Fold select Cond, not X, X into Cond ^ X (#93591)
See the following example: ``` define i1 @src(i64 %x, i1 %y) { %1526 = icmp ne i64 %x, 0 %1527 = icmp eq i64 %x, 0 %sel = select i1 %y, i1 %1526, i1 %1527 ret i1 %sel } define i1 @tgt(i64 %x, i1 %y) { %1527 = icmp eq i64 %x, 0 %sel = xor i1 %y, %1527 ret i1 %sel } ``` I find that this pattern is common in C/C++/Rust code base. This patch folds `select Cond, Y, X` into `Cond ^ X` iff: 1. X has the same type as Cond 2. X is poison -> Y is poison 3. X == !Y Alive2: https://alive2.llvm.org/ce/z/hSmkHS
1 parent 6cd86d0 commit 0a39c88

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,33 @@ static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
35053505
return false;
35063506
}
35073507

3508+
/// Return true iff:
3509+
/// 1. X is poison implies Y is poison.
3510+
/// 2. X is true implies Y is false.
3511+
/// 3. X is false implies Y is true.
3512+
/// Otherwise, return false.
3513+
static bool isKnownInversion(Value *X, Value *Y) {
3514+
// Handle X = icmp pred A, B, Y = icmp pred A, C.
3515+
Value *A, *B, *C;
3516+
ICmpInst::Predicate Pred1, Pred2;
3517+
if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
3518+
!match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
3519+
return false;
3520+
3521+
if (B == C)
3522+
return Pred1 == ICmpInst::getInversePredicate(Pred2);
3523+
3524+
// Try to infer the relationship from constant ranges.
3525+
const APInt *RHSC1, *RHSC2;
3526+
if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
3527+
return false;
3528+
3529+
const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
3530+
const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
3531+
3532+
return CR1.inverse() == CR2;
3533+
}
3534+
35083535
Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
35093536
Value *CondVal = SI.getCondition();
35103537
Value *TrueVal = SI.getTrueValue();
@@ -3999,5 +4026,9 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
39994026
}
40004027
}
40014028

4029+
// select Cond, !X, X -> xor Cond, X
4030+
if (CondVal->getType() == SI.getType() && isKnownInversion(FalseVal, TrueVal))
4031+
return BinaryOperator::CreateXor(CondVal, FalseVal);
4032+
40024033
return nullptr;
40034034
}

llvm/test/Transforms/InstCombine/select-cmp.ll

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,139 @@ define i1 @icmp_no_common(i1 %c, i8 %x, i8 %y, i8 %z) {
345345
ret i1 %r
346346
}
347347

348+
define i1 @test_select_inverse_eq(i64 %x, i1 %y) {
349+
; CHECK-LABEL: @test_select_inverse_eq(
350+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i64 [[X:%.*]], 0
351+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[Y:%.*]]
352+
; CHECK-NEXT: ret i1 [[SEL]]
353+
;
354+
%cmp1 = icmp ne i64 %x, 0
355+
%cmp2 = icmp eq i64 %x, 0
356+
%sel = select i1 %y, i1 %cmp1, i1 %cmp2
357+
ret i1 %sel
358+
}
359+
360+
define i1 @test_select_inverse_signed(i64 %x, i1 %y) {
361+
; CHECK-LABEL: @test_select_inverse_signed(
362+
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i64 [[X:%.*]], 0
363+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[Y:%.*]]
364+
; CHECK-NEXT: ret i1 [[SEL]]
365+
;
366+
%cmp1 = icmp sgt i64 %x, -1
367+
%cmp2 = icmp slt i64 %x, 0
368+
%sel = select i1 %y, i1 %cmp1, i1 %cmp2
369+
ret i1 %sel
370+
}
371+
372+
define i1 @test_select_inverse_unsigned(i64 %x, i1 %y) {
373+
; CHECK-LABEL: @test_select_inverse_unsigned(
374+
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i64 [[X:%.*]], 10
375+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[Y:%.*]]
376+
; CHECK-NEXT: ret i1 [[SEL]]
377+
;
378+
%cmp1 = icmp ult i64 %x, 11
379+
%cmp2 = icmp ugt i64 %x, 10
380+
%sel = select i1 %y, i1 %cmp1, i1 %cmp2
381+
ret i1 %sel
382+
}
383+
384+
define i1 @test_select_inverse_eq_ptr(ptr %x, i1 %y) {
385+
; CHECK-LABEL: @test_select_inverse_eq_ptr(
386+
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne ptr [[X:%.*]], null
387+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[Y:%.*]]
388+
; CHECK-NEXT: ret i1 [[SEL]]
389+
;
390+
%cmp1 = icmp eq ptr %x, null
391+
%cmp2 = icmp ne ptr %x, null
392+
%sel = select i1 %y, i1 %cmp1, i1 %cmp2
393+
ret i1 %sel
394+
}
395+
396+
define i1 @test_select_inverse_fail(i64 %x, i1 %y) {
397+
; CHECK-LABEL: @test_select_inverse_fail(
398+
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i64 [[X:%.*]], 0
399+
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i64 [[X]], 0
400+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[Y:%.*]], i1 [[CMP1]], i1 [[CMP2]]
401+
; CHECK-NEXT: ret i1 [[SEL]]
402+
;
403+
%cmp1 = icmp sgt i64 %x, 0
404+
%cmp2 = icmp slt i64 %x, 0
405+
%sel = select i1 %y, i1 %cmp1, i1 %cmp2
406+
ret i1 %sel
407+
}
408+
409+
define <2 x i1> @test_select_inverse_vec(<2 x i64> %x, <2 x i1> %y) {
410+
; CHECK-LABEL: @test_select_inverse_vec(
411+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq <2 x i64> [[X:%.*]], zeroinitializer
412+
; CHECK-NEXT: [[SEL:%.*]] = xor <2 x i1> [[CMP2]], [[Y:%.*]]
413+
; CHECK-NEXT: ret <2 x i1> [[SEL]]
414+
;
415+
%cmp1 = icmp ne <2 x i64> %x, zeroinitializer
416+
%cmp2 = icmp eq <2 x i64> %x, zeroinitializer
417+
%sel = select <2 x i1> %y, <2 x i1> %cmp1, <2 x i1> %cmp2
418+
ret <2 x i1> %sel
419+
}
420+
421+
define <2 x i1> @test_select_inverse_vec_fail(<2 x i64> %x, i1 %y) {
422+
; CHECK-LABEL: @test_select_inverse_vec_fail(
423+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne <2 x i64> [[X:%.*]], zeroinitializer
424+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq <2 x i64> [[X]], zeroinitializer
425+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[Y:%.*]], <2 x i1> [[CMP1]], <2 x i1> [[CMP2]]
426+
; CHECK-NEXT: ret <2 x i1> [[SEL]]
427+
;
428+
%cmp1 = icmp ne <2 x i64> %x, zeroinitializer
429+
%cmp2 = icmp eq <2 x i64> %x, zeroinitializer
430+
%sel = select i1 %y, <2 x i1> %cmp1, <2 x i1> %cmp2
431+
ret <2 x i1> %sel
432+
}
433+
434+
define i1 @test_select_inverse_nonconst1(i64 %x, i64 %y, i1 %cond) {
435+
; CHECK-LABEL: @test_select_inverse_nonconst1(
436+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
437+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[COND:%.*]]
438+
; CHECK-NEXT: ret i1 [[SEL]]
439+
;
440+
%cmp1 = icmp ne i64 %x, %y
441+
%cmp2 = icmp eq i64 %x, %y
442+
%sel = select i1 %cond, i1 %cmp1, i1 %cmp2
443+
ret i1 %sel
444+
}
445+
446+
define i1 @test_select_inverse_nonconst2(i64 %x, i64 %y, i1 %cond) {
447+
; CHECK-LABEL: @test_select_inverse_nonconst2(
448+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i64 [[Y:%.*]], [[X:%.*]]
449+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[COND:%.*]]
450+
; CHECK-NEXT: ret i1 [[SEL]]
451+
;
452+
%cmp1 = icmp ne i64 %x, %y
453+
%cmp2 = icmp eq i64 %y, %x
454+
%sel = select i1 %cond, i1 %cmp1, i1 %cmp2
455+
ret i1 %sel
456+
}
457+
458+
define i1 @test_select_inverse_nonconst3(i64 %x, i64 %y, i1 %cond) {
459+
; CHECK-LABEL: @test_select_inverse_nonconst3(
460+
; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i64 [[X:%.*]], [[Y:%.*]]
461+
; CHECK-NEXT: [[SEL:%.*]] = xor i1 [[CMP2]], [[COND:%.*]]
462+
; CHECK-NEXT: ret i1 [[SEL]]
463+
;
464+
%cmp1 = icmp ult i64 %x, %y
465+
%cmp2 = icmp uge i64 %x, %y
466+
%sel = select i1 %cond, i1 %cmp1, i1 %cmp2
467+
ret i1 %sel
468+
}
469+
470+
define i1 @test_select_inverse_nonconst4(i64 %x, i64 %y, i64 %z, i1 %cond) {
471+
; CHECK-LABEL: @test_select_inverse_nonconst4(
472+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i64 [[X:%.*]], [[Y:%.*]]
473+
; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i64 [[Z:%.*]], [[Y]]
474+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMP1]], i1 [[CMP2]]
475+
; CHECK-NEXT: ret i1 [[SEL]]
476+
;
477+
%cmp1 = icmp ult i64 %x, %y
478+
%cmp2 = icmp uge i64 %z, %y
479+
%sel = select i1 %cond, i1 %cmp1, i1 %cmp2
480+
ret i1 %sel
481+
}
482+
348483
declare void @use(i1)

0 commit comments

Comments
 (0)