OSDN Git Service

[InstCombine] fix bug in canonicalization to fabs()
authorSanjay Patel <spatel@rotateright.com>
Mon, 10 Jun 2019 14:57:45 +0000 (14:57 +0000)
committerSanjay Patel <spatel@rotateright.com>
Mon, 10 Jun 2019 14:57:45 +0000 (14:57 +0000)
Forgot to translate the predicate clauses in rL362943.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362945 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineSelect.cpp
test/Transforms/InstCombine/fabs.ll

index ced2f10..808833d 100644 (file)
@@ -1878,14 +1878,16 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
   Instruction *FSub;
   if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
       match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
-      match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs()) {
+      match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
+      Pred == FCmpInst::FCMP_OLE) {
     Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub);
     return replaceInstUsesWith(SI, Fabs);
   }
   // (X >  +/-0.0) ? X : (0.0 - X) --> fabs(X)
   if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
       match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
-      match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs()) {
+      match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
+      Pred == FCmpInst::FCMP_OGT) {
     Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FSub);
     return replaceInstUsesWith(SI, Fabs);
   }
index 0f4f480..1ee8989 100644 (file)
@@ -275,6 +275,21 @@ define double @select_fcmp_nnan_ole_zero(double %x) {
   ret double %fabs
 }
 
+; Negative test - wrong predicate.
+
+define double @select_fcmp_nnan_olt_zero(double %x) {
+; CHECK-LABEL: @select_fcmp_nnan_olt_zero(
+; CHECK-NEXT:    [[LEZERO:%.*]] = fcmp olt double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT:    [[NEGX:%.*]] = fsub nnan double 0.000000e+00, [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
+; CHECK-NEXT:    ret double [[FABS]]
+;
+  %lezero = fcmp olt double %x, 0.0
+  %negx = fsub nnan double 0.0, %x
+  %fabs = select i1 %lezero, double %negx, double %x
+  ret double %fabs
+}
+
 ; X <= -0.0 ? (0.0 - X) : X --> fabs(X)
 
 define <2 x float> @select_fcmp_nnan_ole_negzero(<2 x float> %x) {
@@ -314,6 +329,21 @@ define half @select_fcmp_nnan_ogt_negzero(half %x) {
   ret half %fabs
 }
 
+; Negative test - wrong predicate.
+
+define half @select_fcmp_nnan_oge_negzero(half %x) {
+; CHECK-LABEL: @select_fcmp_nnan_oge_negzero(
+; CHECK-NEXT:    [[GTZERO:%.*]] = fcmp oge half [[X:%.*]], 0xH0000
+; CHECK-NEXT:    [[NEGX:%.*]] = fsub nnan half 0xH0000, [[X]]
+; CHECK-NEXT:    [[FABS:%.*]] = select i1 [[GTZERO]], half [[X]], half [[NEGX]]
+; CHECK-NEXT:    ret half [[FABS]]
+;
+  %gtzero = fcmp oge half %x, -0.0
+  %negx = fsub nnan half 0.0, %x
+  %fabs = select i1 %gtzero, half %x, half %negx
+  ret half %fabs
+}
+
 ; X < 0.0 ? -X : X --> fabs(X)
 
 define double @select_fcmp_nnan_nsz_olt_zero(double %x) {