diff options
| author | Russ Cox <rsc@golang.org> | 2010-04-11 15:24:44 -0700 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2010-04-11 15:24:44 -0700 |
| commit | ab23c9da33793e7aae9285c583b1e5acee1d5f61 (patch) | |
| tree | ba667aef243beab01a86951ce18a175b04cffa4c /src | |
| parent | c6596f656eb56d8e98114fdbd21faa913733e9e4 (diff) | |
| download | golang-ab23c9da33793e7aae9285c583b1e5acee1d5f61.tar.gz | |
gc: compile s == "" as len(s) == 0
R=ken2
CC=golang-dev
http://codereview.appspot.com/840043
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/gc/walk.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/cmd/gc/walk.c b/src/cmd/gc/walk.c index ced798e6b..34ac32436 100644 --- a/src/cmd/gc/walk.c +++ b/src/cmd/gc/walk.c @@ -1053,6 +1053,30 @@ walkexpr(Node **np, NodeList **init) goto ret; case OCMPSTR: + // If one argument to the comparison is an empty string, + // comparing the lengths instead will yield the same result + // without the function call. + if((isconst(n->left, CTSTR) && n->left->val.u.sval->len == 0) || + (isconst(n->right, CTSTR) && n->right->val.u.sval->len == 0)) { + r = nod(n->etype, nod(OLEN, n->left, N), nod(OLEN, n->right, N)); + typecheck(&r, Erv); + walkexpr(&r, init); + n = r; + goto ret; + } + + // s + "badgerbadgerbadger" == "badgerbadgerbadger" + if((n->etype == OEQ || n->etype == ONE) && + isconst(n->right, CTSTR) && + n->left->op == OADDSTR && isconst(n->left->right, CTSTR) && + cmpslit(n->right, n->left->right) == 0) { + r = nod(n->etype, nod(OLEN, n->left->left, N), nodintconst(0)); + typecheck(&r, Erv); + walkexpr(&r, init); + n = r; + goto ret; + } + // sys_cmpstring(s1, s2) :: 0 r = mkcall("cmpstring", types[TINT], init, conv(n->left, types[TSTRING]), |
