[PATCH] D142293: [InstCombine] Add PHINode operands to worklist on instruction erasure

Luke Lau via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 21 17:11:07 PST 2023


luke created this revision.
luke added reviewers: asb, craig.topper, spatel, nikic.
Herald added subscribers: pmatos, StephenFan, hiraditya.
Herald added a project: All.
luke requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

If a phi node has one use, and that user has only one use itself, and
that one use is in turn the phi node, then the phi node is erased.
Simultaneously, whenever an instruction is erased, its operands are
added to the worklist as their use counts have changed.

But it's possible that the operand now only has one use and that use is
a phi node: So add the phi node to worklist so the phi node elimination
can occur.

This prevents the need to do lots of cascading iterations over a
function when eliminating phi nodes unlocks more potential phi node
eliminations.

Fixes #50564


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142293

Files:
  llvm/lib/Transforms/InstCombine/InstCombineInternal.h
  llvm/test/Transforms/InstCombine/phi-elimination-iteration.ll


Index: llvm/test/Transforms/InstCombine/phi-elimination-iteration.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/phi-elimination-iteration.ll
@@ -0,0 +1,21 @@
+; RUN: opt < %s -passes=instcombine -S -debug 2>&1 | FileCheck %s
+define void @f() {
+entry:
+	br label %a
+a:
+	%phi.a = phi i32 [ 0, %entry ], [ %x, %a ]
+	%x = add i32 %phi.a, 1
+	br i1 false, label %a, label %b
+b:
+	%phi.b = phi i32 [ %x, %a ], [ %y, %b ]
+	%y = add i32 %phi.b, 1
+	br label %b
+}
+
+; CHECK: INSTCOMBINE ITERATION #1
+; CHECK: INSTCOMBINE ITERATION #2
+; CHECK-NOT: INSTCOMBINE ITERATION #3
+; CHECK-LABEL: a:
+; CHECK-NEXT:    br i1 false, label %a, label %b
+; CHECK-LABEL: b:
+; CHECK-NEXT:    br label %b
Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstVisitor.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/IR/Value.h"
 #include "llvm/Support/Debug.h"
@@ -412,8 +413,14 @@
     // Make sure that we reprocess all operands now that we reduced their
     // use counts.
     for (Use &Operand : I.operands())
-      if (auto *Inst = dyn_cast<Instruction>(Operand))
+      if (auto *Inst = dyn_cast<Instruction>(Operand)) {
         Worklist.add(Inst);
+        // If this instruction uses a phi node then we need to process the phi
+        // node, as it may potentially be able to delete it
+        for (Use &OpOp : Inst->operands())
+          if (auto *PN = dyn_cast<PHINode>(OpOp))
+            Worklist.add(PN);
+      }
 
     Worklist.remove(&I);
     I.eraseFromParent();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142293.491110.patch
Type: text/x-patch
Size: 1898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230122/65829843/attachment.bin>


More information about the llvm-commits mailing list