Skip to content

Commit

Permalink
[InstCombine] fold abs of select with negated op (PR39474)
Browse files Browse the repository at this point in the history
Similar to the existing transform - peek through a select
to match a value and its negation.

https://alive2.llvm.org/ce/z/MXi5KG

  define i8 @src(i1 %b, i8 %x) {
  %0:
    %neg = sub i8 0, %x
    %sel = select i1 %b, i8 %x, i8 %neg
    %abs = abs i8 %sel, 1
    ret i8 %abs
  }
  =>
  define i8 @tgt(i1 %b, i8 %x) {
  %0:
    %abs = abs i8 %x, 1
    ret i8 %abs
  }
  Transformation seems to be correct!
  • Loading branch information
rotateright committed Aug 24, 2020
1 parent 52df6ca commit 6a44edb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,10 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
Value *X;
if (match(IIOperand, m_Neg(m_Value(X))))
return replaceOperand(*II, 0, X);
if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
return replaceOperand(*II, 0, X);
if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
return replaceOperand(*II, 0, X);

break;
}
Expand Down
8 changes: 2 additions & 6 deletions llvm/test/Transforms/InstCombine/abs-intrinsic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ define <4 x i32> @abs_of_neg_vec(<4 x i32> %x) {

define i32 @abs_of_select_neg_true_val(i1 %b, i32 %x) {
; CHECK-LABEL: @abs_of_select_neg_true_val(
; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i32 [[NEG]], i32 [[X]]
; CHECK-NEXT: [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[SEL]], i1 true)
; CHECK-NEXT: [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT: ret i32 [[ABS]]
;
%neg = sub i32 0, %x
Expand All @@ -120,9 +118,7 @@ define i32 @abs_of_select_neg_true_val(i1 %b, i32 %x) {

define <4 x i32> @abs_of_select_neg_false_val(<4 x i1> %b, <4 x i32> %x) {
; CHECK-LABEL: @abs_of_select_neg_false_val(
; CHECK-NEXT: [[NEG:%.*]] = sub <4 x i32> zeroinitializer, [[X:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[B:%.*]], <4 x i32> [[X]], <4 x i32> [[NEG]]
; CHECK-NEXT: [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[SEL]], i1 false)
; CHECK-NEXT: [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[X:%.*]], i1 false)
; CHECK-NEXT: ret <4 x i32> [[ABS]]
;
%neg = sub <4 x i32> zeroinitializer, %x
Expand Down

0 comments on commit 6a44edb

Please sign in to comment.