[PATCH] D50714: [InstCombine] Fold Select with binary op - FP opcodes

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 14 08:57:48 PDT 2018


xbolva00 created this revision.
xbolva00 added a reviewer: spatel.
Herald added a subscriber: llvm-commits.

Follow up for https://reviews.llvm.org/rL339520 and https://reviews.llvm.org/rL338300


Repository:
  rL LLVM

https://reviews.llvm.org/D50714

Files:
  lib/Transforms/InstCombine/InstCombineSelect.cpp
  test/Transforms/InstCombine/select-binop-cmp.ll


Index: test/Transforms/InstCombine/select-binop-cmp.ll
===================================================================
--- test/Transforms/InstCombine/select-binop-cmp.ll
+++ test/Transforms/InstCombine/select-binop-cmp.ll
@@ -140,12 +140,10 @@
   ret i32 %C
 }
 
-; TODO: FP opcodes support
 define float @select_fadd_fcmp(float %x, float %y, float %z) {
 ; CHECK-LABEL: @select_fadd_fcmp(
 ; CHECK-NEXT:    [[A:%.*]] = fcmp oeq float [[X:%.*]], -0.000000e+00
-; CHECK-NEXT:    [[B:%.*]] = fadd float [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[C]]
 ;
   %A = fcmp oeq float %x, -0.0
@@ -157,8 +155,7 @@
 define float @select_fadd_fcmp_2(float %x, float %y, float %z) {
 ; CHECK-LABEL: @select_fadd_fcmp_2(
 ; CHECK-NEXT:    [[A:%.*]] = fcmp ueq float [[X:%.*]], -0.000000e+00
-; CHECK-NEXT:    [[B:%.*]] = fadd float [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[C]]
 ;
   %A = fcmp ueq float %x, -0.0
@@ -170,8 +167,7 @@
 define float @select_fadd_fcmp_3(float %x, float %y, float %z) {
 ; CHECK-LABEL: @select_fadd_fcmp_3(
 ; CHECK-NEXT:    [[A:%.*]] = fcmp one float [[X:%.*]], -0.000000e+00
-; CHECK-NEXT:    [[B:%.*]] = fadd float [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[C]]
 ;
   %A = fcmp one float %x, -0.0
@@ -183,8 +179,7 @@
 define float @select_fadd_fcmp_4(float %x, float %y, float %z) {
 ; CHECK-LABEL: @select_fadd_fcmp_4(
 ; CHECK-NEXT:    [[A:%.*]] = fcmp une float [[X:%.*]], -0.000000e+00
-; CHECK-NEXT:    [[B:%.*]] = fadd float [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[C]]
 ;
   %A = fcmp une float %x, -0.0
@@ -196,8 +191,7 @@
 define float @select_fmul_fcmp(float %x, float %y, float %z) {
 ; CHECK-LABEL: @select_fmul_fcmp(
 ; CHECK-NEXT:    [[A:%.*]] = fcmp oeq float [[X:%.*]], 1.000000e+00
-; CHECK-NEXT:    [[B:%.*]] = fmul float [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[C]]
 ;
   %A = fcmp oeq float %x, 1.0
Index: lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -58,17 +58,22 @@
 /// constant of a binop.
 static Instruction *foldSelectBinOpIdentity(SelectInst &Sel) {
   // The select condition must be an equality compare with a constant operand.
-  // TODO: Support FP compares.
   Value *X;
   Constant *C;
   CmpInst::Predicate Pred;
-  if (!match(Sel.getCondition(), m_ICmp(Pred, m_Value(X), m_Constant(C))) ||
-      !ICmpInst::isEquality(Pred))
+  if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C))))
+    return nullptr;
+
+  bool IsEq = false;
+  if (ICmpInst::isEquality(Pred))
+    IsEq = Pred == ICmpInst::ICMP_EQ;
+  else if (FCmpInst::isEquality(Pred))
+    IsEq = Pred == FCmpInst::FCMP_OEQ || FCmpInst::FCMP_UEQ;
+  else
     return nullptr;
 
   // A select operand must be a binop, and the compare constant must be the
   // identity constant for that binop.
-  bool IsEq = Pred == ICmpInst::ICMP_EQ;
   BinaryOperator *BO;
   if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)) ||
       ConstantExpr::getBinOpIdentity(BO->getOpcode(), BO->getType(), true) != C)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50714.160580.patch
Type: text/x-patch
Size: 3921 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180814/bb9a02f5/attachment.bin>


More information about the llvm-commits mailing list