[PATCH] D101727: Fix PR47960 - Incorrect transformation of fabs with nnan flag

Krishna Kariya via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 2 13:25:07 PDT 2021


Krishnakariya created this revision.
Krishnakariya added reviewers: nlopes, aqjune, nikic, sebastian-ne.
Herald added a subscriber: hiraditya.
Krishnakariya requested review of this revision.
Herald added a project: LLVM.

Bug Fix for PR: https://bugs.llvm.org/show_bug.cgi?id=47960

This patch makes sure that the fast math flag used in the 'select' instruction is the same as the 'fabs' instruction after the transformation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101727

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/fabs.ll


Index: llvm/test/Transforms/InstCombine/fabs.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fabs.ll
+++ llvm/test/Transforms/InstCombine/fabs.ll
@@ -269,7 +269,7 @@
 
 define double @select_fcmp_nnan_ole_zero(double %x) {
 ; CHECK-LABEL: @select_fcmp_nnan_ole_zero(
-; CHECK-NEXT:    [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
 ; CHECK-NEXT:    ret double [[TMP1]]
 ;
   %lezero = fcmp ole double %x, 0.0
@@ -278,11 +278,24 @@
   ret double %fabs
 }
 
+; An Example with fast math flag in select instruction.  
+
+define double @select_nnan_fcmp_nnan_ole_zero(double %x) {
+; CHECK-LABEL: @select_nnan_fcmp_nnan_ole_zero(
+; CHECK-NEXT:    [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT:    ret double [[TMP1]]
+;
+  %lezero = fcmp ole double %x, 0.0
+  %negx = fsub nnan double 0.0, %x
+  %fabs = select nnan i1 %lezero, double %negx, double %x
+  ret double %fabs
+}
+
 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
 
 define double @select_fcmp_nnan_ule_zero(double %x) {
 ; CHECK-LABEL: @select_fcmp_nnan_ule_zero(
-; CHECK-NEXT:    [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
 ; CHECK-NEXT:    ret double [[TMP1]]
 ;
   %lezero = fcmp ule double %x, 0.0
@@ -310,7 +323,7 @@
 
 define <2 x float> @select_fcmp_nnan_ole_negzero(<2 x float> %x) {
 ; CHECK-LABEL: @select_fcmp_nnan_ole_negzero(
-; CHECK-NEXT:    [[TMP1:%.*]] = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
 ; CHECK-NEXT:    ret <2 x float> [[TMP1]]
 ;
   %lezero = fcmp ole <2 x float> %x, <float -0.0, float -0.0>
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2809,7 +2809,7 @@
       match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
       match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
       (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
-    Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub);
+    Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
     return replaceInstUsesWith(SI, Fabs);
   }
   // (X >  +/-0.0) ? X : (0.0 - X) --> fabs(X)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101727.342260.patch
Type: text/x-patch
Size: 2659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210502/801709e4/attachment.bin>


More information about the llvm-commits mailing list