https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64910
Bug ID: 64910 Summary: tree reassociation results in poor code Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: law at redhat dot com Tree reassociation seems to be taking expressions of the form (X & Y) & C Where X & Y are objects and C is a constant. And turns them into (Y & C) & X The problem with generating that kind of code is it makes it much harder for the backends to (for example) create bitfield test instructions. Here's a concrete example: int v; int b2b (unsigned char u, unsigned char w) { if ((u & w) & 0x20) v = 1; } Generates the following .reassoc dump fragment <bb 2>: _8 = w_3(D) & 32; _7 = _8 & u_2(D); if (_7 != 0) To turn that into a bitfield test something is going to have to look at all three statements to determine that _8 can only have a single bit set and thus _7 can only have a single bet set. Contrast to the following after hacking up reassoc a bit: _4 = u_2(D) & w_3(D); _7 = _4 & 32; if (_7 != 0) Which looks like a canonical bit test instruction. The ranking of the operands in the array looks correct (X, Y, CONST). But when we go to rebuild the expression using the simple 3 operand recursive rewrite_expr_tree, we pair up the last two operands into an operation (Y & CONST), then create X & (Y & CONST) thus generating the poor code.