Skip to content

Commit

Permalink
Merge pull request #1497 from WalterBright/b11
Browse files Browse the repository at this point in the history
fixed bt bugs; added another pattern
  • Loading branch information
WalterBright committed Jan 17, 2013
2 parents a3635f6 + ca7e196 commit f4d6b72
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/backend/cgelem.c
Expand Up @@ -3606,6 +3606,8 @@ STATIC elem * elbool(elem *e)

if (OPTIMIZER)
{
int shift;

// Replace bool(x,1) with (x,1),1
elem *e1 = elscancommas(e->E1);
if (cnst(e1) || e1->Eoper == OPrelconst)
Expand All @@ -3614,11 +3616,14 @@ STATIC elem * elbool(elem *e)
e->Eoper = OPcomma;
e->E2 = el_int(e->Ety,i);
e = optelem(e,TRUE);
return e;
}

// Replace bool(e & 1) with (unsigned char)(e & 1)
else if (e->E1->Eoper == OPand && e->E1->E2->Eoper == OPconst && el_tolong(e->E1->E2) == 1)
{ unsigned sz = tysize(e->E1->Ety);
{
L1:
unsigned sz = tysize(e->E1->Ety);
tym_t ty = e->Ety;
switch (sz)
{
Expand Down Expand Up @@ -3676,7 +3681,7 @@ STATIC elem * elbool(elem *e)
e = optelem(e,TRUE);
}

// replace bool((1<<c)&b) with -(b btst c)
// Replace bool((1<<c)&b) with -(b btst c)
else if ((I32 || I64) &&
e->E1->Eoper == OPand &&
e->E1->E1->Eoper == OPshl &&
Expand All @@ -3695,6 +3700,17 @@ STATIC elem * elbool(elem *e)
e = ex;
return optelem(e,TRUE);
}

// Replace bool(a & c) when c is a power of 2 with ((a >> shift) & 1)
else if (e->E1->Eoper == OPand &&
e->E1->E2->Eoper == OPconst &&
(shift = ispow2(el_tolong(e->E1->E2))) != -1
)
{
e->E1->E1 = el_bin(OPshr,e->E1->E1->Ety,e->E1->E1,el_long(TYint, shift));
e->E1->E2->EV.Vullong = 1;
goto L1;
}
}
return e;
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/cod3.c
Expand Up @@ -1595,7 +1595,7 @@ int jmpopcode(elem *e)
{
return XP|JNE;
}
return (op >= OPbt && op <= OPbts) ? JC : JNE;
return ((op >= OPbt && op <= OPbts) || op == OPbtst) ? JC : JNE;
}

if (e->E2->Eoper == OPconst)
Expand Down
4 changes: 3 additions & 1 deletion src/backend/cod4.c
Expand Up @@ -3273,6 +3273,8 @@ code *cdbtst(elem *e, regm_t *pretregs)
int op;
int mode;

//printf("cdbtst(e = %p, *pretregs = %s\n", e, regm_str(*pretregs));

op = 0xA3; // BT EA,value
mode = 4;

Expand All @@ -3293,7 +3295,7 @@ code *cdbtst(elem *e, regm_t *pretregs)
}
else
{
retregs = allregs;
retregs = tysize[tybasic(e1->Ety)] == 1 ? BYTEREGS : allregs;
c = codelem(e1, &retregs, FALSE);
reg = findreg(retregs);
cs.Irm = modregrm(3,0,reg & 7);
Expand Down
30 changes: 30 additions & 0 deletions test/runnable/test42.d
Expand Up @@ -5569,6 +5569,34 @@ void test248()

/***************************************************/

int foo249(int a, int b)
{
return a + ((b & 0x80) != 0);
}

long bar249(long a, int b)
{
return a + ((b & 0x80) != 0);
}

void test249()
{
{
auto i = foo249(3, 6);
assert(i == 3);
i = foo249(3, 0x88);
assert(i == 4);
}
{
auto i = bar249(3, 6);
assert(i == 3);
i = bar249(3, 0x88);
assert(i == 4);
}
}

/***************************************************/

int main()
{
test1();
Expand Down Expand Up @@ -5844,6 +5872,8 @@ int main()
testdbl_to_ulong();
testdbl_to_uint();
testreal_to_ulong();
test248();
test249();

writefln("Success");
return 0;
Expand Down

0 comments on commit f4d6b72

Please sign in to comment.