[llvm] c39cb1c - [CodeGenPrepare] Expand freeze conversion to support fcmp and icmp with null

Juneyoung Lee via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 13 01:21:40 PDT 2020


Author: Juneyoung Lee
Date: 2020-03-13T17:21:33+09:00
New Revision: c39cb1c0ddcc04c79d22b0fd42f25ea835840cbb

URL: https://github.com/llvm/llvm-project/commit/c39cb1c0ddcc04c79d22b0fd42f25ea835840cbb
DIFF: https://github.com/llvm/llvm-project/commit/c39cb1c0ddcc04c79d22b0fd42f25ea835840cbb.diff

LOG: [CodeGenPrepare] Expand freeze conversion to support fcmp and icmp with null

Summary:
This is a simple patch that expands https://reviews.llvm.org/D75859 to pointer comparison and fcmp

Checked with Alive2

Reviewers: reames, jdoerfert

Reviewed By: jdoerfert

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D76048

Added: 
    

Modified: 
    llvm/lib/CodeGen/CodeGenPrepare.cpp
    llvm/test/Transforms/CodeGenPrepare/X86/freeze-cmp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index d2e31e492db5..373064da7bb8 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7198,16 +7198,25 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, bool &ModifiedDT) {
   if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) {
     // br(freeze(icmp a, const)) -> br(icmp (freeze a), const)
     // This helps generate efficient conditional jumps.
-    if (ICmpInst *II = dyn_cast<ICmpInst>(FI->getOperand(0))) {
-      auto Op0 = II->getOperand(0), Op1 = II->getOperand(1);
-      bool Const0 = isa<ConstantInt>(Op0), Const1 = isa<ConstantInt>(Op1);
-      if (II->hasOneUse() && (Const0 || Const1)) {
+    Instruction *CmpI = nullptr;
+    if (ICmpInst *II = dyn_cast<ICmpInst>(FI->getOperand(0)))
+      CmpI = II;
+    else if (FCmpInst *F = dyn_cast<FCmpInst>(FI->getOperand(0)))
+      CmpI = F->getFastMathFlags().none() ? F : nullptr;
+
+    if (CmpI && CmpI->hasOneUse()) {
+      auto Op0 = CmpI->getOperand(0), Op1 = CmpI->getOperand(1);
+      bool Const0 = isa<ConstantInt>(Op0) || isa<ConstantFP>(Op0) ||
+                    isa<ConstantPointerNull>(Op0);
+      bool Const1 = isa<ConstantInt>(Op1) || isa<ConstantFP>(Op1) ||
+                    isa<ConstantPointerNull>(Op1);
+      if (Const0 || Const1) {
         if (!Const0 || !Const1) {
-          auto *F = new FreezeInst(Const0 ? Op1 : Op0, "", II);
+          auto *F = new FreezeInst(Const0 ? Op1 : Op0, "", CmpI);
           F->takeName(FI);
-          II->setOperand(Const0 ? 1 : 0, F);
+          CmpI->setOperand(Const0 ? 1 : 0, F);
         }
-        FI->replaceAllUsesWith(II);
+        FI->replaceAllUsesWith(CmpI);
         FI->eraseFromParent();
         return true;
       }

diff  --git a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-cmp.ll b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-cmp.ll
index 419491cde8d6..d0d40bc91a93 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/freeze-cmp.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/freeze-cmp.ll
@@ -73,9 +73,9 @@ B:
 
 define i1 @ptrcmp(i8* %p) {
 ; CHECK-LABEL: @ptrcmp(
-; CHECK-NEXT:    [[C:%.*]] = icmp eq i8* [[P:%.*]], null
-; CHECK-NEXT:    [[FR:%.*]] = freeze i1 [[C]]
-; CHECK-NEXT:    ret i1 [[FR]]
+; CHECK-NEXT:    [[FR:%.*]] = freeze i8* [[P:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = icmp eq i8* [[FR]], null
+; CHECK-NEXT:    ret i1 [[C]]
 ;
   %c = icmp eq i8* %p, null
   %fr = freeze i1 %c
@@ -85,9 +85,9 @@ define i1 @ptrcmp(i8* %p) {
 
 define i1 @fcmp(float %a) {
 ; CHECK-LABEL: @fcmp(
-; CHECK-NEXT:    [[C:%.*]] = fcmp oeq float [[A:%.*]], 0.000000e+00
-; CHECK-NEXT:    [[FR:%.*]] = freeze i1 [[C]]
-; CHECK-NEXT:    ret i1 [[FR]]
+; CHECK-NEXT:    [[FR:%.*]] = freeze float [[A:%.*]]
+; CHECK-NEXT:    [[C:%.*]] = fcmp oeq float [[FR]], 0.000000e+00
+; CHECK-NEXT:    ret i1 [[C]]
 ;
   %c = fcmp oeq float %a, 0.0
   %fr = freeze i1 %c
@@ -104,5 +104,6 @@ define i1 @fcmp_nan(float %a) {
   %fr = freeze i1 %c
   ret i1 %fr
 }
+
 declare void @g1()
 declare void @g2()


        


More information about the llvm-commits mailing list