[PATCH] D75859: [CodeGenPrepare] Fold br(freeze(icmp x, const)) to br(icmp(freeze x, const))

Juneyoung Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 9 09:41:22 PDT 2020


aqjune created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

This patch helps CodeGenPrepare move freeze into the icmp when it is used by branch.
It reenables generation of efficient conditional jumps.

This is only done when at least one of icmp's operands is constant to prevent the transformation from increasing # of freeze instructions.

Performance degradation of MultiSource/Benchmarks/Ptrdist/yacr2/yacr2.test is resolved with this patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75859

Files:
  llvm/lib/CodeGen/CodeGenPrepare.cpp


Index: llvm/lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7191,6 +7191,29 @@
     return false;
   }
 
+  if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) {
+    // br(freeze(icmp a, const)) -> br(icmp (freeze a), const)
+    // This helps generate efficient conditional jumps.
+    bool AllUsesAreBr = llvm::all_of(FI->users(), [](const User *U) {
+      return isa<BranchInst>(U);
+    });
+    auto II = dyn_cast<ICmpInst>(FI->getOperand(0));
+    if (AllUsesAreBr && II && II->hasOneUse()) {
+      auto Op0 = II->getOperand(0), Op1 = II->getOperand(1);
+      bool Const0 = isa<ConstantInt>(Op0), Const1 = isa<ConstantInt>(Op1);
+      if (Const0 || Const1) {
+        FI->replaceAllUsesWith(II);
+        FI->eraseFromParent();
+        if (!Const0 || !Const1) {
+          Instruction *F = new FreezeInst(Const0 ? Op1 : Op0, "fr", II);
+          II->setOperand(Const0 ? 1 : 0, F);
+        }
+        return true;
+      }
+    }
+    return false;
+  }
+
   if (tryToSinkFreeOperands(I))
     return true;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75859.249134.patch
Type: text/x-patch
Size: 1151 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200309/1ec20fa8/attachment.bin>


More information about the llvm-commits mailing list