Skip to content

Commit

Permalink
Fix Issue 14436 - Optimizer fails to remove comparison loop when comp…
Browse files Browse the repository at this point in the history
…aring array against null
  • Loading branch information
yebblies committed Apr 12, 2015
1 parent 7f8f27c commit 7660158
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/e2ir.c
Expand Up @@ -2167,8 +2167,8 @@ elem *toElem(Expression *e, IRState *irs)
// Optimize comparisons of arrays of basic types
// For arrays of integers/characters, and void[],
// replace druntime call with:
// For a==b: a.length==b.length && memcmp(a.ptr, b.ptr, size)==0
// For a!=b: a.length!=b.length || memcmp(a.ptr, b.ptr, size)!=0
// For a==b: a.length==b.length && (a.length == 0 || memcmp(a.ptr, b.ptr, size)==0)
// For a!=b: a.length!=b.length || (a.length != 0 || memcmp(a.ptr, b.ptr, size)!=0)
// size is a.length*sizeof(a[0]) for dynamic arrays, or sizeof(a) for static arrays.

elem *earr1 = toElem(ee->e1, irs);
Expand Down Expand Up @@ -2212,6 +2212,10 @@ elem *toElem(Expression *e, IRState *irs)
e = el_bin(OPmemcmp, TYint, e, esize);
e = el_bin(eop, TYint, e, el_long(TYint, 0));

elem *elen = t2->ty == Tsarray ? elen2 : elen1;
elem *esizecheck = el_bin(eop, TYint, el_same(&elen), el_long(TYsize_t, 0));
e = el_bin(ee->op == TOKequal ? OPoror : OPandand, TYint, esizecheck, e);

if (t1->ty == Tsarray && t2->ty == Tsarray)
assert(t1->size() == t2->size());
else
Expand Down
11 changes: 11 additions & 0 deletions test/runnable/mars1.d
Expand Up @@ -1280,6 +1280,16 @@ int test13969(const S13969* f) {

////////////////////////////////////////////////////////////////////////

int[] arr14436;
void test14436()
{
assert(arr14436 == null);
arr14436 = [1, 2, 3];
assert(arr14436 != null);
}

////////////////////////////////////////////////////////////////////////

void test14220()
{
auto a = toString(14);
Expand Down Expand Up @@ -1347,6 +1357,7 @@ int main()
test13383();
test13190();
test13485();
test14436();
test10639();
test10715();
test10678();
Expand Down

0 comments on commit 7660158

Please sign in to comment.