linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 7/7] move up instructions blocking if-conversion
Date: Fri, 27 Nov 2020 23:25:16 +0100	[thread overview]
Message-ID: <20201127222516.44915-8-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com>

---
 linearize.h |  6 +++++
 simplify.c  | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/linearize.h b/linearize.h
index 2c548d43526f..71a693960c9e 100644
--- a/linearize.h
+++ b/linearize.h
@@ -300,6 +300,12 @@ static inline void replace_bb_in_list(struct basic_block_list **list,
 	replace_ptr_list_entry((struct ptr_list **)list, old, new, count);
 }
 
+static inline void replace_insn(struct instruction *old, struct instruction *new)
+{
+	replace_ptr_list_entry((struct ptr_list **)&old->bb->insns, old, new, 1);
+}
+
+
 struct entrypoint {
 	struct symbol *name;
 	struct symbol_list *syms;
diff --git a/simplify.c b/simplify.c
index fc64e5b77adf..14a5562c84a4 100644
--- a/simplify.c
+++ b/simplify.c
@@ -61,6 +61,63 @@ static inline bool is_pow2(pseudo_t src)
 	return is_power_of_2(src->value);
 }
 
+///
+// check if a pseudo is defined in a BB
+//
+// :note: this could also use the liveness information if available.
+//
+static inline bool is_defined_at(pseudo_t src, struct basic_block *bb)
+{
+	if (src->type != PSEUDO_REG)
+		return true;
+	return domtree_dominates(src->def->bb, bb);
+}
+
+///
+// move an instruction at the end of the given BB
+static void move_insn(struct instruction *insn, struct basic_block *bb)
+{
+	struct instruction *last = delete_last_instruction(&bb->insns);
+	insn->bb = bb;
+	add_instruction(&bb->insns, insn);
+	add_instruction(&bb->insns, last);
+}
+
+///
+// try to move an instruction into a BB if all its arguments are defined there
+static bool try_to_move_insn(struct instruction *insn, struct basic_block *bb)
+{
+	static struct instruction NOP;	// will later be ignore because !ep
+
+	if (!bb || bb == insn->bb)
+		return 0;
+
+	switch (insn->opcode) {
+	case OP_SEL:
+		if (!is_defined_at(insn->src3, bb))
+			return 0;
+	case OP_ADD: case OP_SUB:
+	case OP_ASR: case OP_LSR: case OP_SHL:
+	case OP_AND: case OP_XOR: case OP_OR:
+	case OP_BINCMP ... OP_BINCMP_END:
+	case OP_FPCMP ... OP_FPCMP_END:
+	case OP_FADD: case OP_FSUB:
+		// basically, all the binops but multiply and divide, just because
+		if (!is_defined_at(insn->src2, bb))
+			return 0;
+	case OP_UNOP ... OP_UNOP_END:
+		if (!is_defined_at(insn->src1, bb))
+			return 0;
+		break;
+	default:
+		return 0;
+	}
+	replace_insn(insn, &NOP);
+	move_insn(insn, bb);
+	repeat_phase |= REPEAT_CSE;
+	return 1;
+}
+
 ///
 // find the trivial parent for a phi-source
 static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo)
@@ -68,8 +125,11 @@ static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseud
 	/* Can't go upwards if the pseudo is defined in the bb it came from.. */
 	if (pseudo->type == PSEUDO_REG) {
 		struct instruction *def = pseudo->def;
-		if (def->bb == source)
-			return source;
+		if (def->bb == source) {
+			// unless we can move the defining instruction up
+			if (!try_to_move_insn(def, source->idom))
+				return source;
+		}
 	}
 	if (bb_list_size(source->children) != 1 || bb_list_size(source->parents) != 1)
 		return source;
-- 
2.29.2


  parent reply	other threads:[~2020-11-27 22:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-22 15:27 [PATCH 0/7] simplify logical negation Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 1/7] not: add testcases for canonicalization & simplification of negations Luc Van Oostenryck
2020-11-22 19:00   ` Linus Torvalds
2020-11-22 19:57     ` Luc Van Oostenryck
2020-11-22 20:13       ` Linus Torvalds
2020-11-22 20:38         ` Luc Van Oostenryck
2020-11-22 22:26         ` Luc Van Oostenryck
2020-11-27 22:25         ` [PATCH 0/6] 'bits translation' simplification Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 1/6] add testscases for 'bits translation' optimization Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 2/6] factorize SEL(x, OP(y,z), y) into OP(SEL(x, z, 0), y) Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 3/6] add helper is_power_of_2() Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 4/6] add helper is_pow2() Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 5/6] add log base 2 function: log2_exact() Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 6/6] convert SEL(x & BIT1, BIT2, 0) into SHIFT(x & BIT1, S) Luc Van Oostenryck
2020-11-27 22:25           ` Luc Van Oostenryck [this message]
2020-11-27 22:44           ` [PATCH 0/6] 'bits translation' simplification Linus Torvalds
2020-11-22 15:27 ` [PATCH 2/7] canon: put PSEUDO_ARGs in canonical order too Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 3/7] canon: put PSEUDO_REGs " Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 4/7] canon: simplify calculation of canonical order Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 5/7] opcode: add helpers opcode_negate() & opcode_swap() Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 6/7] not: simplify (~x {&,|,^} x) --> {0,~0,~0} Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 7/7] not: simplify ((x cmp y) {&,|,^} (x !cmp y)) --> {0,1,1} Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201127222516.44915-8-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).