[llvm] [InstCombine] Relax the conditons of fold of `ucmp`/`scmp` into phi by allowing the phi node to use the result of `ucmp`/`scmp` more than once (PR #109593)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 00:30:57 PDT 2024


================
@@ -1868,18 +1868,25 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
 
   // Clone the instruction that uses the phi node and move it into the incoming
   // BB because we know that the next iteration of InstCombine will simplify it.
+  SmallDenseMap<BasicBlock *, Instruction *> Clones;
   for (auto OpIndex : OpsToMoveUseToIncomingBB) {
     Value *Op = PN->getIncomingValue(OpIndex);
     BasicBlock *OpBB = PN->getIncomingBlock(OpIndex);
 
-    Instruction *Clone = I.clone();
-    for (Use &U : Clone->operands()) {
-      if (U == PN)
-        U = Op;
-      else
-        U = U->DoPHITranslation(PN->getParent(), OpBB);
+    auto CloneIt = Clones.find(OpBB);
+    if (CloneIt == Clones.end()) {
+      Instruction *Clone = I.clone();
+      for (Use &U : Clone->operands()) {
+        if (U == PN)
+          U = Op;
+        else
+          U = U->DoPHITranslation(PN->getParent(), OpBB);
+      }
+      Clone = InsertNewInstBefore(Clone, OpBB->getTerminator()->getIterator());
+      CloneIt = Clones.insert(std::make_pair(OpBB, Clone)).first;
----------------
nikic wrote:

```suggestion
      CloneIt = Clones.insert({OpBB, Clone}).first;
```

https://github.com/llvm/llvm-project/pull/109593


More information about the llvm-commits mailing list