Skip to contentSkip to conversation

add and use new helper: replace_with_value()

PATCH New Luc Van Oostenryck 1 participant 4 messages Sep 5, 2020 Sparse checker

lore
Share & tools
lore
#0 lore raw

cover add and use new helper: replace_with_value()

This makes things slightly more readable and is a preparatory step for some others series.

Luc Van Oostenryck (3):
add helper replace_with_value()
use replace_with_value()
replace_with_{pseudo,value}() can be tail-calls

 simplify.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/3 add helper replace_with_value()

During simplification, it's relatively common to have to replace an instruction with pseudo corresponding to a known value. The pseudo can be created with value_pseudo() and the replacement can be made via replace_with_pseudo() but the combination of the two is a bit long.

So, create an helper doing both sat once: replace_with_value().

---
 simplify.c | 5 +++++
 1 file changed, 5 insertions(+)
simplify.c +5 -0
index f6b79685f439..0c75b3fadcab 100644
@@ -465,6 +465,11 @@ static int replace_with_pseudo(struct instruction *insn, pseudo_t pseudo)
 	return REPEAT_CSE;
 }
 
+static inline int replace_with_value(struct instruction *insn, long long val)
+{
+	return replace_with_pseudo(insn, value_pseudo(val));
+}
+
 static inline int def_opcode(pseudo_t p)
 {
 	if (p->type != PSEUDO_REG)
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/3 use replace_with_value()

In reply to Luc Van Oostenryck

Replace existing 'replace_with_pseudo(insn, value_pseudo($N))' by 'replace_with_value($N)'.

---
 simplify.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
simplify.c +8 -8
index 0c75b3fadcab..3c4b972ce607 100644
@@ -838,7 +838,7 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
 			omask = def->src2->value;
 			nmask = omask & mask;
 			if (nmask == 0)
-				return replace_with_pseudo(insn, value_pseudo(0));
+				return replace_with_value(insn, 0);
 			if (nmask == mask)
 				return replace_pseudo(insn, &insn->src1, def->src1);
 			if (nbr_users(pseudo) > 1)
@@ -874,7 +874,7 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
 			omask = def->src2->value;
 			nmask = omask & mask;
 			if (nmask == 0)
-				return replace_with_pseudo(insn, value_pseudo(0));
+				return replace_with_value(insn, 0);
 			if (nmask == mask)
 				return replace_pseudo(insn, &insn->src1, def->src1);
 			// do not simplify into ((A << S) & (M << S))
@@ -912,7 +912,7 @@ new_value:
 		return replace_pseudo(insn, &insn->src1, pseudo->def->src1);
 	}
 zero:
-	return replace_with_pseudo(insn, value_pseudo(0));
+	return replace_with_value(insn, 0);
 replace_mask:
 	insn->opcode = OP_AND;
 	insn->src2 = value_pseudo(mask);
@@ -1104,7 +1104,7 @@ static int simplify_constant_rightside(struct instruction *insn)
 
 	case OP_MODU: case OP_MODS:
 		if (value == 1)
-			return replace_with_pseudo(insn, value_pseudo(0));
+			return replace_with_value(insn, 0);
 		return 0;
 
 	case OP_DIVU: case OP_DIVS:
@@ -1167,14 +1167,14 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg)
 			warning(insn->pos, "self-comparison always evaluates to false");
 	case OP_SUB:
 	case OP_XOR:
-		return replace_with_pseudo(insn, value_pseudo(0));
+		return replace_with_value(insn, 0);
 
 	case OP_SET_EQ:
 	case OP_SET_LE: case OP_SET_GE:
 	case OP_SET_BE: case OP_SET_AE:
 		if (Wtautological_compare)
 			warning(insn->pos, "self-comparison always evaluates to true");
-		return replace_with_pseudo(insn, value_pseudo(1));
+		return replace_with_value(insn, 1);
 
 	case OP_AND:
 	case OP_OR:
@@ -1298,7 +1298,7 @@ static int simplify_constant_unop(struct instruction *insn)
 	mask = 1ULL << (insn->size-1);
 	res &= mask | (mask-1);
 	
-	replace_with_pseudo(insn, value_pseudo(res));
+	replace_with_value(insn, res);
 	return REPEAT_CSE;
 }
 
@@ -1577,7 +1577,7 @@ static int simplify_select(struct instruction *insn)
 	if (cond == src2 && is_zero(src1)) {
 		kill_use(&insn->src1);
 		kill_use(&insn->src3);
-		replace_with_pseudo(insn, value_pseudo(0));
+		replace_with_value(insn, 0);
 		return REPEAT_CSE;
 	}
 	return 0;
Signature · 1 line
2.28.0
#3 lore raw

PATCH 3/3 replace_with_{pseudo,value}() can be tail-calls

In reply to Luc Van Oostenryck

This avoids the need to have a separate 'return REPEAT_CSE' and thus make the code slightly more compact and fast to read.

---
 simplify.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
simplify.c +4 -8
index 3c4b972ce607..76c0558855a2 100644
@@ -1153,8 +1153,7 @@ static int simplify_constant_binop(struct instruction *insn)
 	if (!res)
 		return 0;
 
-	replace_with_pseudo(insn, res);
-	return REPEAT_CSE;
+	return replace_with_pseudo(insn, res);
 }
 
 static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg)
@@ -1298,8 +1297,7 @@ static int simplify_constant_unop(struct instruction *insn)
 	mask = 1ULL << (insn->size-1);
 	res &= mask | (mask-1);
 	
-	replace_with_value(insn, res);
-	return REPEAT_CSE;
+	return replace_with_value(insn, res);
 }
 
 static int simplify_unop(struct instruction *insn)
@@ -1554,8 +1552,7 @@ static int simplify_select(struct instruction *insn)
 		take = cond->value ? src1 : src2;
 		kill = cond->value ? &insn->src3 : &insn->src2;
 		kill_use(kill);
-		replace_with_pseudo(insn, take);
-		return REPEAT_CSE;
+		return replace_with_pseudo(insn, take);
 	}
 	if (constant(src1) && constant(src2)) {
 		long long val1 = src1->value;
@@ -1577,8 +1574,7 @@ static int simplify_select(struct instruction *insn)
 	if (cond == src2 && is_zero(src1)) {
 		kill_use(&insn->src1);
 		kill_use(&insn->src3);
-		replace_with_value(insn, 0);
-		return REPEAT_CSE;
+		return replace_with_value(insn, 0);
 	}
 	return 0;
 }
Signature · 1 line
2.28.0