[PATCH] D26088: Don't leave unused divs/rems sitting around in BypassSlowDivision.

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 28 10:52:54 PDT 2016


jlebar created this revision.
jlebar added a reviewer: tra.
jlebar added a subscriber: llvm-commits.

This "pass" eagerly creates div and rem instructions even when only one
is needed -- it relies on a later pass (machine DCE?) to clean them up.

This is problematic not just from a cleanliness perspective (this pass
is running during CodeGenPrepare, so should leave the IR in a better
state), but it also creates a problem for instruction selection.  If we
always have a div+rem, isel will always select a divrem instruction (if
possible), even when a single div or rem would do.

Specifically, in NVPTX, we want to compute rem from the output of div,
if available.  But if a div is not available, we want to leave the rem
alone.  This transformation is overeager if div is always available.

Because this code runs as part of CodeGenPrepare, it's nontrivial to
write a test for this change.  But this will effectively be tested by
a later patch which adds the aforementioned change to NVPTX isel.


https://reviews.llvm.org/D26088

Files:
  llvm/lib/Transforms/Utils/BypassSlowDivision.cpp


Index: llvm/lib/Transforms/Utils/BypassSlowDivision.cpp
===================================================================
--- llvm/lib/Transforms/Utils/BypassSlowDivision.cpp
+++ llvm/lib/Transforms/Utils/BypassSlowDivision.cpp
@@ -247,5 +247,22 @@
     MadeChange |= reuseOrInsertFastDiv(I, BT, UseDivOp, UseSignedOp, DivCache);
   }
 
+  // Above we eagerly create divs and rems, as pairs, so that we can efficiently
+  // create divrem machine instructions.  Now erase unused any divs / rems so we
+  // don't leave extra instructions sitting around.
+  SmallVector<Instruction *, 4> ToErase;
+  for (auto &KV : DivCache)
+    for (Instruction *Phi : {KV.second.Quotient, KV.second.Remainder}) {
+      if (!Phi->use_empty())
+        continue;
+      ToErase.push_back(Phi);
+      for (Value *Operand : Phi->operand_values())
+        if (Instruction *I = dyn_cast<Instruction>(Operand))
+          ToErase.push_back(I);
+    }
+
+  for (Instruction *I : ToErase)
+    I->eraseFromParent();
+
   return MadeChange;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26088.76217.patch
Type: text/x-patch
Size: 1025 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161028/a45b22fa/attachment.bin>


More information about the llvm-commits mailing list