Skip to contentSkip to conversation

teach memory simplification about ASM instructions

PATCH New Luc Van Oostenryck 1 participant 6 messages Feb 21, 2021 Sparse checker

lore
Share & tools
lore
#0 lore raw

cover teach memory simplification about ASM instructions

This series fixes simplify_memops() which didn't took in account the fact that ASM instructions can also modify the content of the memory:
either because it contains an output memory operand, or because it explicetly clobbers memory in some other way.

Luc Van Oostenryck (5):
reorg dominates()
asm-mem: add testcase for missing reload after asm memops
asm-mem: does it clobber memory?
asm-mem: does it output to memory?
asm-mem: teach dominates() about OP_ASM

 flow.c                           | 17 +++++++++++++----
 linearize.c                      |  9 ++++++++-
 linearize.h                      |  2 ++
 validation/mem2reg/asm-reload0.c | 14 ++++++++++++++
 4 files changed, 37 insertions(+), 5 deletions(-)
 create mode 100644 validation/mem2reg/asm-reload0.c

base-commit: 2494587e823700458923052b17b0b981be92d776

Signature · 1 line
2.30.0
#1 lore raw

PATCH 1/5 reorg dominates()

To prepare the handling of OP_ASM instructions, reorganize the opcode tests to use a switch.

---
 flow.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
flow.c +7 -4
index bda277aa551b..5751ce756518 100644
@@ -490,12 +490,15 @@ static inline int distinct_symbols(pseudo_t a, pseudo_t b)
  */
 int dominates(pseudo_t pseudo, struct instruction *insn, struct instruction *dom, int local)
 {
-	int opcode = dom->opcode;
-
-	if (opcode == OP_CALL || opcode == OP_ENTRY)
+	switch (dom->opcode) {
+	case OP_CALL: case OP_ENTRY:
 		return local ? 0 : -1;
-	if (opcode != OP_LOAD && opcode != OP_STORE)
+	case OP_LOAD: case OP_STORE:
+		break;
+	default:
 		return 0;
+	}
+
 	if (dom->src != pseudo) {
 		if (local)
 			return 0;
Signature · 1 line
2.30.0
#2 lore raw

PATCH 2/5 asm-mem: add testcase for missing reload after asm memops

In reply to Luc Van Oostenryck

Memory simplification is done with the help of the function dominates() which determine when memory instructions interfere.

This function handles OP_CALLs, OP_LOADs and OP_STOREs but memory can also be changed via OP_ASMs.

Add a testcase showing this.

---
 validation/mem2reg/asm-reload0.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 validation/mem2reg/asm-reload0.c
validation/mem2reg/asm-reload0.c new file +15 -0
new file mode 100644
index 000000000000..c9e297dde428
@@ -0,0 +1,15 @@
+static int asm_reload(void)
+{
+	int mem = 0;
+	asm volatile ("[%1] <= 1" : "=m" (mem));
+	return mem;
+}
+
+/*
+ * check-name: asm-reload0
+ * check-command: test-linearize $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ * check-output-contains: load\\.
+ */
Signature · 1 line
2.30.0
#3 lore raw

PATCH 3/5 asm-mem: does it clobber memory?

In reply to Luc Van Oostenryck

An asm statement can specify that it clobbers memory. Add this info directly in the corresponding instruction, avoiding the need to scan the clobber list each time.

---
 linearize.c | 8 +++++++-
 linearize.h | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
linearize.c +7 -1
index 33d641b40de6..4140b60caebd 100644
@@ -2174,7 +2174,7 @@ static void add_asm_output(struct entrypoint *ep, struct instruction *insn, stru
 static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement *stmt)
 {
 	struct instruction *insn;
-	struct expression *expr;
+	struct expression *expr, *clob;
 	struct asm_rules *rules;
 	struct asm_operand *op;
 
@@ -2206,6 +2206,12 @@ static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement
 		add_asm_output(ep, insn, op);
 	} END_FOR_EACH_PTR(op);
 
+	/* and finally, look if it clobbers memory */
+	FOR_EACH_PTR(stmt->asm_clobbers, clob) {
+		if (!strcmp(clob->string->data, "memory"))
+			insn->clobber_memory = 1;
+	} END_FOR_EACH_PTR(clob);
+
 	return VOID;
 }
 
linearize.h +1 -0
index a77e4b3e5f6f..fb51327684bb 100644
@@ -150,6 +150,7 @@ struct instruction {
 		struct /* asm */ {
 			const char *string;
 			struct asm_rules *asm_rules;
+			int clobber_memory:1;
 		};
 	};
 };
Signature · 1 line
2.30.0
#4 lore raw

PATCH 4/5 asm-mem: does it output to memory?

In reply to Luc Van Oostenryck

If an asm statement have a memory output operand, it modifies memory.

Since this information is needed during memops simplification, add this info directly in the corresponding instruction, avoiding the need to scan the output operands list each time.

---
 linearize.c | 1 +
 linearize.h | 1 +
 2 files changed, 2 insertions(+)
linearize.c +1 -0
index 4140b60caebd..0c9b0e59cc4b 100644
@@ -2153,6 +2153,7 @@ static void add_asm_output_address(struct entrypoint *ep, struct instruction *in
 
 	pseudo = linearize_expression(ep, op->expr);
 	add_asm_rule(insn, &insn->asm_rules->outputs, op, pseudo);
+	insn->output_memory = 1;
 }
 
 static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
linearize.h +1 -0
index fb51327684bb..cf0cf066a8e5 100644
@@ -151,6 +151,7 @@ struct instruction {
 			const char *string;
 			struct asm_rules *asm_rules;
 			int clobber_memory:1;
+			int output_memory:1;
 		};
 	};
 };
Signature · 1 line
2.30.0
#5 lore raw

PATCH 5/5 asm-mem: teach dominates() about OP_ASM

In reply to Luc Van Oostenryck

The function dominates() needs to know if an OP_ASM instruction may modify.

Use the information now available in the instruction to return the answer.

---
 flow.c                           | 6 ++++++
 validation/mem2reg/asm-reload0.c | 1 -
 2 files changed, 6 insertions(+), 1 deletion(-)
flow.c +6 -0
index 5751ce756518..5d63018798d6 100644
@@ -495,6 +495,12 @@ int dominates(pseudo_t pseudo, struct instruction *insn, struct instruction *dom
 		return local ? 0 : -1;
 	case OP_LOAD: case OP_STORE:
 		break;
+	case OP_ASM:
+		if (dom->clobber_memory)
+			return -1;
+		if (dom->output_memory)
+			return -1;
+		return 0;
 	default:
 		return 0;
 	}
validation/mem2reg/asm-reload0.c +0 -1
index c9e297dde428..ce1829e02724 100644
@@ -8,7 +8,6 @@ static int asm_reload(void)
 /*
  * check-name: asm-reload0
  * check-command: test-linearize $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: load\\.
Signature · 1 line
2.30.0