[DTrace-devel] [PATCH 2/4] clean up dt_cg_typecast(): suppress shifts if shift amount is 0

eugene.loh at oracle.com eugene.loh at oracle.com
Tue Aug 11 15:11:10 PDT 2020


From: Eugene Loh <eugene.loh at oracle.com>

The typecast consists of shifting left and then right.  It is possible
that the shift amount is 0:  the src and dst values can both be 64 bits
while one or else the other is signed.  It is easy to add the test
n!=0 to the condition for performing the typecast.  This is a small
but easy optimization.

Signed-off-by: Eugene Loh <eugene.loh at oracle.com>
---
 libdtrace/dt_cg.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index 4f657967..6ba779d1 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -1132,16 +1132,16 @@ dt_cg_typecast(const dt_node_t *src, const dt_node_t *dst,
 	srcsize = dt_node_type_size(src);
 	dstsize = dt_node_type_size(dst);
 
-	if (dt_node_is_scalar(dst) && (dstsize < srcsize ||
+	if (dstsize < srcsize)
+		n = sizeof (uint64_t) * NBBY - dstsize * NBBY;
+	else
+		n = sizeof (uint64_t) * NBBY - srcsize * NBBY;
+
+	if (dt_node_is_scalar(dst) && n != 0 && (dstsize < srcsize ||
 	    (src->dn_flags & DT_NF_SIGNED) ^ (dst->dn_flags & DT_NF_SIGNED))) {
 		if ((reg = dt_regset_alloc(drp)) == -1)
 			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
 
-		if (dstsize < srcsize)
-			n = sizeof (uint64_t) * NBBY - dstsize * NBBY;
-		else
-			n = sizeof (uint64_t) * NBBY - srcsize * NBBY;
-
 		dt_cg_setx(dlp, reg, n);
 
 		instr = BPF_ALU64_REG(BPF_LSH, dst->dn_reg, reg);
-- 
2.18.4




More information about the DTrace-devel mailing list