commit 59e3faab23c5f9d281394c3e88172736d5ec847c parent 0935ae49a658bedc97d1bf9082049498e148f6df Author: Roberto E. Vargas Caballero <k0ga@shike2.net> Date: Mon, 23 Mar 2026 11:54:51 +0100 cc1: Reduce unsigned constant mult to shift Diffstat:
| M | src/cmd/scc-cc/cc1/fold.c | | | 5 | +++++ |
| A | tests/cc/execute/0251-mul.c | | | 14 | ++++++++++++++ |
| M | tests/cc/execute/scc-tests.lst | | | 1 | + |
3 files changed, 20 insertions(+), 0 deletions(-)
diff --git a/src/cmd/scc-cc/cc1/fold.c b/src/cmd/scc-cc/cc1/fold.c @@ -803,6 +803,11 @@ reduce(Node *np) return; switch (op) { + case OMUL: + /* i * 2^n => i << n */ + np->op = OSHL; + rp->sym->u.u = log; + break; case ODIV: /* i / 2^n => i >> n */ np->op = OSHR; diff --git a/tests/cc/execute/0251-mul.c b/tests/cc/execute/0251-mul.c @@ -0,0 +1,14 @@ +int +main(void) +{ + int a = -2; + unsigned b = 2; + + if (!(a * 2 == -4)) + return 1; + if (!(b * 2 == 4)) + return 2; + if (!(b * 8 == 16)) + return 3; + return 0; +} diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst @@ -241,3 +241,4 @@ 0248-enum.c 0249-mod.c 0250-div.c +0251-mul.c