linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fix casts when linearizing compound assignments
@ 2012-06-08 10:47 Xi Wang
  2012-06-08 15:45 ` Xi Wang
  0 siblings, 1 reply; 12+ messages in thread
From: Xi Wang @ 2012-06-08 10:47 UTC (permalink / raw)
  To: linux-sparse; +Cc: Xi Wang

linearize_assignment() emits confusing casts for compound assignments.
Consider p += 1, where p is a pointer.

	oldvalue = cast_pseudo(ep, oldvalue, src->ctype, expr->ctype);
	opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], src->ctype);
	dst = add_binary_op(ep, src->ctype, opcode, oldvalue, value);
	value = cast_pseudo(ep, dst, expr->ctype, src->ctype);

It would make more sense to swap src->ctype and expr->ctype in both
cast_pseudo(ep, pseudo, from, to) calls:

In the first call, `oldvalue' is a pointer (expr->ctype), but it is
converted from an integer (src->ctype) to a pointer (expr->ctype);

In the second call, `dst' is an integer (src->ctype), but it is
converted from a pointer (expr->ctype) to an integer (src->ctype).

This patch instead converts `value' from an integer (src->ctype) to a
pointer (expr->ctype) so as to emit fewer casts.

Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
 linearize.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/linearize.c b/linearize.c
index 1d15cfd..d186387 100644
--- a/linearize.c
+++ b/linearize.c
@@ -1161,7 +1161,6 @@ static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *e
 		return value;
 	if (expr->op != '=') {
 		pseudo_t oldvalue = linearize_load_gen(ep, &ad);
-		pseudo_t dst;
 		static const int op_trans[] = {
 			[SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = OP_ADD,
 			[SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = OP_SUB,
@@ -1179,10 +1178,9 @@ static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *e
 		if (!src)
 			return VOID;
 
-		oldvalue = cast_pseudo(ep, oldvalue, src->ctype, expr->ctype);
-		opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], src->ctype);
-		dst = add_binary_op(ep, src->ctype, opcode, oldvalue, value);
-		value = cast_pseudo(ep, dst, expr->ctype, src->ctype);
+		opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], expr->ctype);
+		value = cast_pseudo(ep, value, src->ctype, expr->ctype);
+		value = add_binary_op(ep, expr->ctype, opcode, oldvalue, value);
 	}
 	value = linearize_store_gen(ep, value, &ad);
 	finish_address_gen(ep, &ad);
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-06-16  6:10 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-08 10:47 [PATCH] fix casts when linearizing compound assignments Xi Wang
2012-06-08 15:45 ` Xi Wang
2012-06-08 19:10   ` Pekka Enberg
2012-06-08 19:18     ` Linus Torvalds
2012-06-08 19:25       ` Xi Wang
2012-06-08 19:28         ` Linus Torvalds
2012-06-08 19:47           ` Pekka Enberg
2012-06-08 19:57             ` Xi Wang
2012-06-08 20:09           ` Jeff Garzik
2012-06-08 20:08     ` Jeff Garzik
2012-06-08 20:16       ` Linus Torvalds
2012-06-16  6:10         ` Xi Wang

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).