diff mbox series

[5/8] cfg: adjust phi-nodes when merging BBs

Message ID 20201116222927.51939-6-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series cfg: early CFG simplification | expand

Commit Message

Luc Van Oostenryck Nov. 16, 2020, 10:29 p.m. UTC
When merging BBs, it's possible that the top BB feeds one or
several phi-nodes in the bottom BB. Since phi-nodes only make
sense for values incoming from the parent BBs, these phi-nodes
can and should be removed when merging the BBs.

So, when merging BBs, remove these related phi-nodes.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 flow.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
diff mbox series

Patch

diff --git a/flow.c b/flow.c
index e9c8f6b7355e..23886f9d84fa 100644
--- a/flow.c
+++ b/flow.c
@@ -760,6 +760,26 @@  static void adjust_phisrc(struct basic_block *top, struct instruction *insn)
 	} END_FOR_EACH_PTR(phi);
 }
 
+static void adjust_phi(struct basic_block *top, struct instruction *insn)
+{
+	pseudo_t phi;
+
+	FOR_EACH_PTR(insn->phi_list, phi) {
+		struct instruction *def;
+
+		if (phi == VOID)
+			continue;
+
+		def = phi->def;
+		if (def->bb != top)
+			continue;
+
+		convert_instruction_target(insn, def->src);
+		kill_instruction(def);
+		kill_instruction(insn);
+	} END_FOR_EACH_PTR(phi);
+}
+
 ///
 // merge two BBs
 // @top: the first BB to be merged
@@ -786,6 +806,9 @@  static int merge_bb(struct basic_block *top, struct basic_block *bot)
 			continue;
 		assert(insn->bb == bot);
 		switch (insn->opcode) {
+		case OP_PHI:
+			adjust_phi(top, insn);
+			continue;
 		case OP_PHISOURCE:
 			adjust_phisrc(top, insn);
 			break;