From cff611711ba54d6bd7b0ab02426419ceab4f7f07 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Sun, 5 Nov 2023 21:03:06 +0100 Subject: [PATCH] Optimize multiplication by zero --- src/cc65/codegen.c | 14 +++++++++++++- test/val/mult1.c | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index 480cc32ead..f923ac8c9e 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -2726,7 +2726,12 @@ void g_mul (unsigned flags, unsigned long val) if (flags & CF_FORCECHAR) { /* Handle some special cases */ switch (val) { - + case 0: + AddCodeLine ("lda #$00"); + return; + case 1: + /* Nothing to do */ + return; case 3: AddCodeLine ("sta tmp1"); AddCodeLine ("asl a"); @@ -2764,6 +2769,13 @@ void g_mul (unsigned flags, unsigned long val) case CF_INT: switch (val) { + case 0: + AddCodeLine ("lda #$00"); + AddCodeLine ("tax"); + return; + case 1: + /* Nothing to do */ + return; case 3: AddCodeLine ("jsr mulax3"); return; diff --git a/test/val/mult1.c b/test/val/mult1.c index 95141d76d0..b1a074e41c 100644 --- a/test/val/mult1.c +++ b/test/val/mult1.c @@ -55,6 +55,7 @@ void m3(unsigned char uc) /* testing literal multiply with same source and destination */ vuc = uc; uc2 = 0; + uc1 = vuc; uc1 = uc1*0; if( uc1 != 0 ) failures++; uc1 = vuc; uc1 = uc1*1; if( uc1 != (uc2+=TESTLIT) ) failures++; uc1 = vuc; uc1 = uc1*2; if( uc1 != (uc2+=TESTLIT) ) failures++; uc1 = vuc; uc1 = uc1*3; if( uc1 != (uc2+=TESTLIT) ) failures++;