public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/42505] New: loop canonicalization causes a lot of unnecessary temporary variables
@ 2009-12-25 18:45 sliao at google dot com
2010-04-30 8:56 ` [Bug middle-end/42505] [4.4/4.5/4.6 Regression] " jakub at gcc dot gnu dot org
` (9 more replies)
0 siblings, 10 replies; 14+ messages in thread
From: sliao at google dot com @ 2009-12-25 18:45 UTC (permalink / raw)
To: gcc-bugs
This regression was caused by loop canonicalization.
The following example:
struct A {
int f1;
int f2;
};
int func(int c);
int test(struct A* src, struct A* dst, int count)
{
while (count--) {
if (!func(src->f2)) {
return 0;
}
*dst++ = *src++;
}
return 1;
}
gcc 4.2.1 compiles this to 40 bytes, gcc 4.4.0 to 48 bytes:
gcc 4.2.1 output:
test:
push {r4, r5, r6, lr}
mov r4, r0
mov r5, r1
mov r6, r2
b .L2
.L3:
ldr r0, [r4, #4]
bl func
cmp r0, #0
beq .L6
mov r3, r5
mov r2, r4
ldmia r2!, {r0, r1}
stmia r3!, {r0, r1}
mov r5, r3
mov r4, r2
.L2:
sub r6, r6, #1
bcs .L3
mov r0, #1
.L6:
@ sp needed for prologue
pop {r4, r5, r6, pc}
gcc 4.4.0 output:
push {r4, r5, r6, r7, lr} // note r7 is cloberred
sub sp, sp, #12 // why need to store smth on the stack?
mov r7, r0
str r1, [sp, #4] // why store r1 onto stack?
mov r6, r2
mov r5, #0
b .L2
.L5:
add r4, r7, r5
ldr r0, [r4, #4]
bl func
sub r6, r6, #1
cmp r0, #0
beq .L4
ldr r1, [sp, #4] // load from stack
add r3, r1, r5
add r5, r5, #8
ldmia r4!, {r1, r2}
stmia r3!, {r1, r2}
.L2:
cmp r6, #0
bne .L5
mov r0, #1
.L4:
add sp, sp, #12
@ sp needed for prologue
pop {r4, r5, r6, r7, pc}
This is caused by loop canonicalization pass (pass_iv_optimize) that was added
in gcc 4.4.
Final GIMPLE form in gcc 4.2.1 compiler:
test (src, dst, count)
{
int a;
int D.1545;
<bb 2>:
goto <bb 6> (<L3>);
<L0>:;
a = func (MEM[base: src, offset: 4]);
if (a == 0) goto <L8>; else goto <L2>;
<L8>:;
D.1545 = 0;
goto <bb 8> (<L5>);
<L2>:;
MEM[base: dst] = MEM[base: src];
dst = dst + 8B;
src = src + 8B;
<L3>:;
count = count - 1;
if (count != -1) goto <L0>; else goto <L9>;
<L9>:;
D.1545 = 1;
<L5>:;
return D.1545;
}
The final GIMPLE in gcc 4.4:
test (struct A * src, struct A * dst, int count)
{
unsigned int ivtmp.22; // induction variables introduced by pass_iv_optimize
unsigned int ivtmp.19;
int a;
int D.1274;
<bb 2>:
ivtmp.22 = (unsigned int) count; // copy of count, count itself is not used
anymore
ivtmp.19 = 0;
goto <bb 6>;
<bb 3>:
a = func (MEM[base: src + ivtmp.19, offset: 4]);
ivtmp.22 = ivtmp.22 - 1;
if (a == 0)
goto <bb 4>;
else
goto <bb 5>;
<bb 4>:
D.1274 = 0;
goto <bb 8>;
<bb 5>:
MEM[base: dst, index: ivtmp.19] = MEM[base: src, index: ivtmp.19];
ivtmp.19 = ivtmp.19 + 8;
<bb 6>:
if (ivtmp.22 != 0)
goto <bb 3>;
else
goto <bb 7>;
<bb 7>:
D.1274 = 1;
<bb 8>:
return D.1274;
}
The following RTL passes could not optimize these temporary induction variables
and they are spilled on the stack, which causes a lot of other inefficiencies.
The main question: there are three way to fix this:
1) turn off loop canonicalization for -Os
2) optimize the extra variable in the GIMPLE passes
3) optimize the extra variable in the RTL passes
--
Summary: loop canonicalization causes a lot of unnecessary
temporary variables
Product: gcc
Version: 4.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: sliao at google dot com
GCC build triplet: i686-linux
GCC host triplet: i686-linux
GCC target triplet: arm-eabi
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
@ 2010-04-30 8:56 ` jakub at gcc dot gnu dot org
2010-06-04 0:09 ` sandra at codesourcery dot com
` (8 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: jakub at gcc dot gnu dot org @ 2010-04-30 8:56 UTC (permalink / raw)
To: gcc-bugs
--
jakub at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Target Milestone|4.4.4 |4.4.5
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
2010-04-30 8:56 ` [Bug middle-end/42505] [4.4/4.5/4.6 Regression] " jakub at gcc dot gnu dot org
@ 2010-06-04 0:09 ` sandra at codesourcery dot com
2010-06-04 7:45 ` steven at gcc dot gnu dot org
` (7 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: sandra at codesourcery dot com @ 2010-06-04 0:09 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from sandra at codesourcery dot com 2010-06-04 00:09 -------
I've been looking at this problem today. Here's the stupid part coming out of
ivopts:
<bb 5>:
# ivtmp.7_21 = PHI <0(2), ivtmp.7_20(4)>
# ivtmp.10_22 = PHI <ivtmp.10_24(2), ivtmp.10_23(4)>
count_25 = (int) ivtmp.10_22;
if (count_25 != 0)
goto <bb 3>;
else
goto <bb 6>;
No subsequent pass is recognizing that the unsigned-to-signed conversion is
useless and "count" is otherwise dead.
If I change the parameter "count" to have type "unsigned int", then ivopts does
the obvious replacement itself:
<bb 5>:
# ivtmp.7_21 = PHI <0(2), ivtmp.7_20(4)>
# ivtmp.10_22 = PHI <count_7(D)(2), ivtmp.10_23(4)>
if (ivtmp.10_22 != 0)
goto <bb 3>;
else
goto <bb 6>;
Then "count" is completely gone from the loop after ivopts and the resulting
code looks good.
So, fix this somewhere inside ivopts to make the signed case produce the same
code as the unsigned one? Or tell it not to replace count at all if it has to
do a type conversion? I'm still trying to find my way around the code for this
pass to figure out where things happen, so if this is obvious to someone else
I'd appreciate a pointer. :-)
--
sandra at codesourcery dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |sandra at codesourcery dot
| |com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
2010-04-30 8:56 ` [Bug middle-end/42505] [4.4/4.5/4.6 Regression] " jakub at gcc dot gnu dot org
2010-06-04 0:09 ` sandra at codesourcery dot com
@ 2010-06-04 7:45 ` steven at gcc dot gnu dot org
2010-06-04 9:09 ` rguenth at gcc dot gnu dot org
` (6 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: steven at gcc dot gnu dot org @ 2010-06-04 7:45 UTC (permalink / raw)
To: gcc-bugs
------- Comment #5 from steven at gcc dot gnu dot org 2010-06-04 07:45 -------
AFAIU, you can't randomly change signed to unsigned, due to different overflow
semantics, which is why IVOPTS doesn't make this change itself. Imagine you
enter the loop with count = 0, and with a second counter hidden in func. You
will not get the same number of iterations if you change the type of count from
"int" to "unsigned int".
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (2 preceding siblings ...)
2010-06-04 7:45 ` steven at gcc dot gnu dot org
@ 2010-06-04 9:09 ` rguenth at gcc dot gnu dot org
2010-06-05 20:42 ` sandra at codesourcery dot com
` (5 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-06-04 9:09 UTC (permalink / raw)
To: gcc-bugs
------- Comment #6 from rguenth at gcc dot gnu dot org 2010-06-04 09:08 -------
If the result of the conversion is only used in an exit equality test against a
constant it can be dropped. This could also happen in a following
forwprop run which is our single tree-combiner (though that currently will
combine into comparisons only if the result will be a constant, it doesn't
treat defs with a single use specially which it could, if the combined
constant is in gimple form).
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (3 preceding siblings ...)
2010-06-04 9:09 ` rguenth at gcc dot gnu dot org
@ 2010-06-05 20:42 ` sandra at codesourcery dot com
2010-06-10 13:01 ` sandra at codesourcery dot com
` (4 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: sandra at codesourcery dot com @ 2010-06-05 20:42 UTC (permalink / raw)
To: gcc-bugs
------- Comment #7 from sandra at codesourcery dot com 2010-06-05 20:41 -------
OK, I'm testing a hack to rewrite_use_compare to make it know that it doesn't
have to introduce a temporary just to compare against constant zero. I'm also
doing a little tuning of the costs model for -Os, using CSiBE.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (4 preceding siblings ...)
2010-06-05 20:42 ` sandra at codesourcery dot com
@ 2010-06-10 13:01 ` sandra at codesourcery dot com
2010-06-12 7:42 ` sandra at codesourcery dot com
` (3 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: sandra at codesourcery dot com @ 2010-06-10 13:01 UTC (permalink / raw)
To: gcc-bugs
------- Comment #8 from sandra at codesourcery dot com 2010-06-10 13:01 -------
I was barking up the wrong tree with my last idea -- the signed/unsigned
conversion business was a red herring. Here's what I now believe is the
problem: the costs computation is underestimating the register pressure costs
so that we are in fact spilling when the cost computation thinks it still has
"free" registers.
A hack to make get_computation_cost_at add target_reg_cost to the result when
it must use a scratch register seemed to have positive overall effects on code
size (as well as fixing the test case). But, I don't think that's the real
solution, as I can't come up with a good logical justification for putting such
a cost there. :-) estimate_reg_pressure_cost already reserves 3 "free"
registers for such things. Anyway, I am continuing to poke at this in hopes of
figuring out where the register costs model is really going wrong.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (5 preceding siblings ...)
2010-06-10 13:01 ` sandra at codesourcery dot com
@ 2010-06-12 7:42 ` sandra at codesourcery dot com
2010-06-19 12:57 ` sandra at codesourcery dot com
` (2 subsequent siblings)
9 siblings, 0 replies; 14+ messages in thread
From: sandra at codesourcery dot com @ 2010-06-12 7:42 UTC (permalink / raw)
To: gcc-bugs
------- Comment #9 from sandra at codesourcery dot com 2010-06-12 07:42 -------
I now have a specific theory of what is going on here. There are two problems:
(1) estimate_reg_pressure_cost is not accounting for the function call in the
loop body. In this case it ought to use call_used_regs instead of fixed_regs
to determine how many registers are available for loop invariants. Here the
target is Thumb-1 and there are only 4 non-call-clobbered registers available
rather than 9, so we are much more constrained than ivopts thinks we are. This
is pretty straightforward to fix.
(2) For the test case filed with the issue, there are 4 registers needed for
the two candidates and two invariants ivopts is selecting, so even with the fix
for (1) ivopts thinks it has enough registers available. But, there are two
uses of the form (src + offset) in the ivopts output, although they appear
differently in the gimple code. RTL optimizations are combining these and
allocating a temporary. Since the two uses span the function call in the loop
body, the temporary needs to be assigned to a non-call-clobbered register.
This is why there is a spill of the other loop invariant. Perhaps we could
make the RA smarter about recomputing the src + offset value rather than resort
to spilling something, but since I am dumb about the RA ;-) I'm planning to
keep poking at the ivopts cost model instead.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (6 preceding siblings ...)
2010-06-12 7:42 ` sandra at codesourcery dot com
@ 2010-06-19 12:57 ` sandra at codesourcery dot com
2010-07-05 17:41 ` sandra at gcc dot gnu dot org
2010-07-10 18:43 ` sandra at gcc dot gnu dot org
9 siblings, 0 replies; 14+ messages in thread
From: sandra at codesourcery dot com @ 2010-06-19 12:57 UTC (permalink / raw)
To: gcc-bugs
------- Comment #10 from sandra at codesourcery dot com 2010-06-19 12:56 -------
Patch posted here:
http://gcc.gnu.org/ml/gcc-patches/2010-06/msg01920.html
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (7 preceding siblings ...)
2010-06-19 12:57 ` sandra at codesourcery dot com
@ 2010-07-05 17:41 ` sandra at gcc dot gnu dot org
2010-07-10 18:43 ` sandra at gcc dot gnu dot org
9 siblings, 0 replies; 14+ messages in thread
From: sandra at gcc dot gnu dot org @ 2010-07-05 17:41 UTC (permalink / raw)
To: gcc-bugs
------- Comment #11 from sandra at gcc dot gnu dot org 2010-07-05 17:41 -------
Subject: Bug 42505
Author: sandra
Date: Mon Jul 5 17:40:57 2010
New Revision: 161844
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=161844
Log:
2010-07-05 Sandra Loosemore <sandra@codesourcery.com>
PR middle-end/42505
gcc/
* tree-ssa-loop-ivopts.c (determine_set_costs): Delete obsolete
comments about cost model.
(try_add_cand_for): Add second strategy for choosing initial set
based on original IVs, controlled by ORIGINALP argument.
(get_initial_solution): Add ORIGINALP argument.
(find_optimal_iv_set_1): New function, split from find_optimal_iv_set.
(find_optimal_iv_set): Try two different strategies for choosing
the IV set, and return the one with lower cost.
gcc/testsuite/
* gcc.target/arm/pr42505.c: New test case.
Added:
trunk/gcc/testsuite/gcc.target/arm/pr42505.c
Modified:
trunk/gcc/ChangeLog
trunk/gcc/testsuite/ChangeLog
trunk/gcc/tree-ssa-loop-ivopts.c
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
` (8 preceding siblings ...)
2010-07-05 17:41 ` sandra at gcc dot gnu dot org
@ 2010-07-10 18:43 ` sandra at gcc dot gnu dot org
9 siblings, 0 replies; 14+ messages in thread
From: sandra at gcc dot gnu dot org @ 2010-07-10 18:43 UTC (permalink / raw)
To: gcc-bugs
------- Comment #12 from sandra at gcc dot gnu dot org 2010-07-10 18:43 -------
Subject: Bug 42505
Author: sandra
Date: Sat Jul 10 18:43:29 2010
New Revision: 162043
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=162043
Log:
2010-07-10 Sandra Loosemore <sandra@codesourcery.com>
PR middle-end/42505
gcc/
* tree-inline.c (estimate_num_insns): Refactor builtin complexity
lookup code into....
* builtins.c (is_simple_builtin, is_inexpensive_builtin): ...these
new functions.
* tree.h (is_simple_builtin, is_inexpensive_builtin): Declare.
* cfgloopanal.c (target_clobbered_regs): Define.
(init_set_costs): Initialize target_clobbered_regs.
(estimate_reg_pressure_cost): Add call_p argument. When true,
adjust the number of available registers to exclude the
call-clobbered registers.
* cfgloop.h (target_clobbered_regs): Declare.
(estimate_reg_pressure_cost): Adjust declaration.
* tree-ssa-loop-ivopts.c (struct ivopts_data): Add body_includes_call.
(ivopts_global_cost_for_size): Pass it to estimate_reg_pressure_cost.
(determine_set_costs): Dump target_clobbered_regs.
(loop_body_includes_call): New function.
(tree_ssa_iv_optimize_loop): Use it to initialize new field.
* loop-invariant.c (gain_for_invariant): Adjust arguments to pass
call_p flag through.
(best_gain_for_invariant): Likewise.
(find_invariants_to_move): Likewise.
(move_single_loop_invariants): Likewise, using already-computed
has_call field.
Modified:
trunk/gcc/ChangeLog
trunk/gcc/builtins.c
trunk/gcc/cfgloop.h
trunk/gcc/cfgloopanal.c
trunk/gcc/loop-invariant.c
trunk/gcc/tree-inline.c
trunk/gcc/tree-ssa-loop-ivopts.c
trunk/gcc/tree.h
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42505
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2011-01-13 16:48 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <bug-42505-4@http.gcc.gnu.org/bugzilla/>
2010-10-01 12:00 ` [Bug middle-end/42505] [4.4/4.5/4.6 Regression] loop canonicalization causes a lot of unnecessary temporary variables jakub at gcc dot gnu.org
2010-10-01 15:01 ` sandra at codesourcery dot com
2011-01-13 16:24 ` law at redhat dot com
2011-01-13 16:55 ` rguenth at gcc dot gnu.org
2009-12-25 18:45 [Bug middle-end/42505] New: " sliao at google dot com
2010-04-30 8:56 ` [Bug middle-end/42505] [4.4/4.5/4.6 Regression] " jakub at gcc dot gnu dot org
2010-06-04 0:09 ` sandra at codesourcery dot com
2010-06-04 7:45 ` steven at gcc dot gnu dot org
2010-06-04 9:09 ` rguenth at gcc dot gnu dot org
2010-06-05 20:42 ` sandra at codesourcery dot com
2010-06-10 13:01 ` sandra at codesourcery dot com
2010-06-12 7:42 ` sandra at codesourcery dot com
2010-06-19 12:57 ` sandra at codesourcery dot com
2010-07-05 17:41 ` sandra at gcc dot gnu dot org
2010-07-10 18:43 ` sandra at gcc dot gnu dot org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).