https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78332
Bug ID: 78332 Summary: [ARM] Negative costs of ivopts groups Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: ka6ash at gmail dot com CC: amker.cheng at gmail dot com Target Milestone: --- For the following code GCC generates six induction variables for each use arr[{0-1}][{0-2}][k]. I think the problem is negative costs. The negative costs is appeared in the following part of the function determine_group_iv_cost_address: sum_cost = cost; if (!sum_cost.infinite_cost_p () && cand->ainc_use == use) { if (can_autoinc) sum_cost -= cand->cost_step; /* If we generated the candidate solely for exploiting autoincrement opportunities, and it turns out it can't be used, set the cost to infinity to make sure we ignore it. */ else if (cand->pos == IP_AFTER_USE || cand->pos == IP_BEFORE_USE) sum_cost = infinite_cost; } If we have a zero cost and autoinc is possible, we obtain the negative cost. This situation is a little bit confusing because we also can have candidates with a zero cost of casting and they are less preferable than candidates with autoinc. It seems that there is a preference to use autoinc candidates. But the testcase shows the preference could increase register pressure without any reason. I hotfixed it by zeroing negative costs and GCC started to use two iv. So, are negative costs a part of the cost model and if it is, is it something wrong with six ivs? gcc btc.c -O2 -S -march=armv7-a -mcpu=cortex-a9 void foo(int amount, double arr[2][3][amount]) { int k; for (k = 1; k < amount; k++) { arr[0][0][k] = 1.0; arr[0][1][k] = 1.0; arr[0][2][k] = 1.0; arr[1][0][k] = 1.0; arr[1][1][k] = 1.0; arr[1][2][k] = 1.0; } } <Group-candidate Costs>: Group 0: cand cost compl. inv.ex. depends on 0 10 2 1 ... 5 0 1 6 -4 0 7 -4 0 ... Group 1: cand cost compl. inv.ex. depends on 0 10 2 8 ... 9 0 1 10 -4 0 11 -4 0 ...