Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MUL+I / MUL+U simplification fixes. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/simp.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,14 @@ Tree simplify(int op, Type ty, Tree l, Tree r) {
break;

case MUL+U:
foldcnst(U,u,*);
commute(l,r);
if (l->op == CNST+U && (n = ispow2(l->u.v.u)) != 0)
return simplify(LSH, ty, r, cnsttree(inttype, (long)n));
foldcnst(U,u,*);
identity(r,l,U,u,1);
/* 0 * x => 0 */
if (l->op == CNST+U && l->u.v.u == 0)
return cnsttree(ty, 0L);
identity(l,r,U,u,1);
break;
case NE+I:
cfoldcnst(I,i,!=);
Expand Down Expand Up @@ -496,8 +499,8 @@ Tree simplify(int op, Type ty, Tree l, Tree r) {
commute(l,r);
break;
case MUL+I:
commute(l,r);
xfoldcnst(I,i,*,muli);
commute(l,r);
if (l->op == CNST+I && r->op == ADD+I && r->kids[1]->op == CNST+I)
/* c1*(x + c2) => c1*x + c1*c2 */
return simplify(ADD, ty, simplify(MUL, ty, l, r->kids[0]),
Expand All @@ -509,7 +512,12 @@ Tree simplify(int op, Type ty, Tree l, Tree r) {
if (l->op == CNST+I && l->u.v.i > 0 && (n = ispow2(l->u.v.i)) != 0)
/* 2^n * r => r<<n */
return simplify(LSH, ty, r, cnsttree(inttype, (long)n));
identity(r,l,I,i,1);

/* 0 * x => 0 */
if (l->op == CNST+I && l->u.v.i == 0)
return cnsttree(ty, 0L);

identity(l,r,I,i,1);
break;
case NE+F:
cfoldcnst(F,d,!=);
Expand Down