aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2020-05-24 20:07:49 -0700
committerMichael Forney <mforney@mforney.org>2020-05-24 20:10:02 -0700
commit3d91966b0dcfe0bd0fc3a779a63d49546e46aea8 (patch)
tree82b972d68b11c32e1f26e05dd9e4bbfc7e7b802e
parent5ed26f909dd923dcd811784bc88efa46ad57bb30 (diff)
qbe: Omit unnecessary add to help QBE's memopt pass
-rw-r--r--qbe.c8
-rw-r--r--test/builtin-alloca.qbe9
-rw-r--r--test/compound-assignment.qbe20
-rw-r--r--test/conditional-compound-literal.qbe27
-rw-r--r--test/do-loop.qbe38
-rw-r--r--test/local-init.qbe25
-rw-r--r--test/struct-copy.qbe21
-rw-r--r--test/struct-return-1.qbe3
-rw-r--r--test/while-condition.qbe25
9 files changed, 85 insertions, 91 deletions
diff --git a/qbe.c b/qbe.c
index a1afbd5..799d56b 100644
--- a/qbe.c
+++ b/qbe.c
@@ -971,7 +971,13 @@ funcinit(struct func *func, struct decl *d, struct init *init)
} else {
if (offset < init->end && (dst.bits.before || dst.bits.after))
zero(func, d->value, d->type->align, offset, init->end);
- dst.addr = funcinst(func, IADD, &iptr, d->value, mkintconst(&iptr, init->start));
+ dst.addr = d->value;
+ /*
+ QBE's memopt does not eliminate the store for ptr + 0,
+ so only emit the add if the offset is non-zero
+ */
+ if (init->start > 0)
+ dst.addr = funcinst(func, IADD, &iptr, dst.addr, mkintconst(&iptr, init->start));
src = funcexpr(func, init->expr);
funcstore(func, init->expr->type, QUALNONE, dst, src);
offset = init->end;
diff --git a/test/builtin-alloca.qbe b/test/builtin-alloca.qbe
index 75a1fe1..877ed22 100644
--- a/test/builtin-alloca.qbe
+++ b/test/builtin-alloca.qbe
@@ -3,10 +3,9 @@ function $f() {
@start.1
%.1 =l alloc8 8
@body.2
- %.2 =l add %.1, 0
- %.3 =l extsw 32
- %.4 =l alloc16 %.3
- %.5 =l copy %.4
- storel %.5, %.2
+ %.2 =l extsw 32
+ %.3 =l alloc16 %.2
+ %.4 =l copy %.3
+ storel %.4, %.1
ret
}
diff --git a/test/compound-assignment.qbe b/test/compound-assignment.qbe
index 35e71a5..dcc7aa7 100644
--- a/test/compound-assignment.qbe
+++ b/test/compound-assignment.qbe
@@ -2,17 +2,15 @@ export
function $f() {
@start.1
%.1 =l alloc4 4
- %.3 =l alloc8 8
+ %.2 =l alloc8 8
@body.2
- %.2 =l add %.1, 0
- storew 0, %.2
- %.4 =l add %.3, 0
- storel %.1, %.4
- %.5 =l loadl %.3
- %.6 =l add %.5, 4
- storel %.6, %.3
- %.7 =w loadsw %.5
- %.8 =w add %.7, 1
- storew %.8, %.5
+ storew 0, %.1
+ storel %.1, %.2
+ %.3 =l loadl %.2
+ %.4 =l add %.3, 4
+ storel %.4, %.2
+ %.5 =w loadsw %.3
+ %.6 =w add %.5, 1
+ storew %.6, %.3
ret
}
diff --git a/test/conditional-compound-literal.qbe b/test/conditional-compound-literal.qbe
index 9e9c5f7..013d179 100644
--- a/test/conditional-compound-literal.qbe
+++ b/test/conditional-compound-literal.qbe
@@ -2,24 +2,21 @@ export
function w $main() {
@start.1
%.1 =l alloc4 4
- %.3 =l alloc8 8
- %.6 =l alloc4 4
+ %.2 =l alloc8 8
+ %.4 =l alloc4 4
@body.2
- %.2 =l add %.1, 0
- storew 0, %.2
- %.4 =l add %.3, 0
- %.5 =w cnew 0, 0
- jnz %.5, @cond_true.3, @cond_false.4
+ storew 0, %.1
+ %.3 =w cnew 0, 0
+ jnz %.3, @cond_true.3, @cond_false.4
@cond_true.3
jmp @cond_join.5
@cond_false.4
- %.7 =l add %.6, 0
- %.8 =w loadsw %.1
- storew %.8, %.7
+ %.5 =w loadsw %.1
+ storew %.5, %.4
@cond_join.5
- %.9 =l phi @cond_true.3 0, @cond_false.4 %.6
- storel %.9, %.4
- %.10 =l loadl %.3
- %.11 =w loadsw %.10
- ret %.11
+ %.6 =l phi @cond_true.3 0, @cond_false.4 %.4
+ storel %.6, %.2
+ %.7 =l loadl %.2
+ %.8 =w loadsw %.7
+ ret %.8
}
diff --git a/test/do-loop.qbe b/test/do-loop.qbe
index d31c80c..2f0e12a 100644
--- a/test/do-loop.qbe
+++ b/test/do-loop.qbe
@@ -2,31 +2,29 @@ export
function w $main() {
@start.1
%.1 =l alloc4 4
- %.3 =l alloc4 4
+ %.2 =l alloc4 4
@body.2
- %.2 =l add %.1, 0
- storew 2, %.2
- %.4 =l add %.3, 0
- storew 0, %.4
+ storew 2, %.1
+ storew 0, %.2
@do_body.3
- %.5 =w loadsw %.1
- %.6 =w ceqw %.5, 1
- %.7 =w cnew %.6, 0
- jnz %.7, @if_true.6, @if_false.7
+ %.3 =w loadsw %.1
+ %.4 =w ceqw %.3, 1
+ %.5 =w cnew %.4, 0
+ jnz %.5, @if_true.6, @if_false.7
@if_true.6
jmp @do_cond.4
@if_false.7
- %.8 =w loadsw %.3
- %.9 =w add %.8, 1
- storew %.9, %.3
+ %.6 =w loadsw %.2
+ %.7 =w add %.6, 1
+ storew %.7, %.2
@do_cond.4
- %.10 =w loadsw %.1
- %.11 =w sub %.10, 1
- storew %.11, %.1
- %.12 =w cnew %.10, 0
- jnz %.12, @do_body.3, @do_join.5
+ %.8 =w loadsw %.1
+ %.9 =w sub %.8, 1
+ storew %.9, %.1
+ %.10 =w cnew %.8, 0
+ jnz %.10, @do_body.3, @do_join.5
@do_join.5
- %.13 =w loadsw %.3
- %.14 =w cnew %.13, 2
- ret %.14
+ %.11 =w loadsw %.2
+ %.12 =w cnew %.11, 2
+ ret %.12
}
diff --git a/test/local-init.qbe b/test/local-init.qbe
index 8d7346a..a8b919e 100644
--- a/test/local-init.qbe
+++ b/test/local-init.qbe
@@ -3,20 +3,19 @@ function $f() {
@start.1
%.1 =l alloc8 32
@body.2
- %.2 =l add %.1, 0
- %.3 =w copy 97
- storeb %.3, %.2
- %.4 =l add %.1, 1
- storeb 0, %.4
- %.5 =l add %.1, 2
- storeh 0, %.5
- %.6 =l add %.1, 4
- storew 0, %.6
- %.7 =l add %.1, 8
+ %.2 =w copy 97
+ storeb %.2, %.1
+ %.3 =l add %.1, 1
+ storeb 0, %.3
+ %.4 =l add %.1, 2
+ storeh 0, %.4
+ %.5 =l add %.1, 4
+ storew 0, %.5
+ %.6 =l add %.1, 8
+ storel 0, %.6
+ %.7 =l add %.1, 16
storel 0, %.7
- %.8 =l add %.1, 16
+ %.8 =l add %.1, 24
storel 0, %.8
- %.9 =l add %.1, 24
- storel 0, %.9
ret
}
diff --git a/test/struct-copy.qbe b/test/struct-copy.qbe
index 4778921..eb2c955 100644
--- a/test/struct-copy.qbe
+++ b/test/struct-copy.qbe
@@ -3,19 +3,18 @@ function $f() {
@start.1
%.1 =l alloc4 12
@body.2
- %.2 =l add %.1, 0
- %.3 =l loaduw $x
- storew %.3, %.2
- %.4 =l add $x, 4
- %.5 =l add %.2, 4
- %.6 =l loaduw %.4
- storew %.6, %.5
+ %.2 =l loaduw $x
+ storew %.2, %.1
+ %.3 =l add $x, 4
+ %.4 =l add %.1, 4
+ %.5 =l loaduw %.3
+ storew %.5, %.4
+ %.6 =l add %.3, 4
%.7 =l add %.4, 4
- %.8 =l add %.5, 4
- %.9 =l loaduw %.7
- storew %.9, %.8
+ %.8 =l loaduw %.6
+ storew %.8, %.7
+ %.9 =l add %.6, 4
%.10 =l add %.7, 4
- %.11 =l add %.8, 4
ret
}
export data $x = align 4 { z 12 }
diff --git a/test/struct-return-1.qbe b/test/struct-return-1.qbe
index fcdf699..76bb98d 100644
--- a/test/struct-return-1.qbe
+++ b/test/struct-return-1.qbe
@@ -4,7 +4,6 @@ function :s.1 $f() {
@start.1
%.1 =l alloc4 4
@body.2
- %.2 =l add %.1, 0
- storew 2, %.2
+ storew 2, %.1
ret %.1
}
diff --git a/test/while-condition.qbe b/test/while-condition.qbe
index 23b3b15..1b8eece 100644
--- a/test/while-condition.qbe
+++ b/test/while-condition.qbe
@@ -3,21 +3,20 @@ function w $main() {
@start.1
%.1 =l alloc8 8
@body.2
- %.2 =l add %.1, 0
- %.3 =d swtof 1
- stored %.3, %.2
+ %.2 =d swtof 1
+ stored %.2, %.1
@while_cond.3
- %.4 =d loadd %.1
- %.5 =w cned %.4, d_0
- jnz %.5, @while_body.4, @while_join.5
+ %.3 =d loadd %.1
+ %.4 =w cned %.3, d_0
+ jnz %.4, @while_body.4, @while_join.5
@while_body.4
- %.6 =d loadd %.1
- %.7 =d swtof 2
- %.8 =d div %.6, %.7
- stored %.8, %.1
+ %.5 =d loadd %.1
+ %.6 =d swtof 2
+ %.7 =d div %.5, %.6
+ stored %.7, %.1
jmp @while_cond.3
@while_join.5
- %.9 =d loadd %.1
- %.10 =w dtosi %.9
- ret %.10
+ %.8 =d loadd %.1
+ %.9 =w dtosi %.8
+ ret %.9
}