GCC optimizes fixed range-based for loop as if it had longer, variable length

后端 未结 1 1036
野性不改
野性不改 2021-01-31 17:56

I have an array of POD structs and am trying to sum across one field. Here\'s a minimal example:

struct Item
{
    int x = 0;
    int y = 0;
};

typedef Item It         


        
相关标签:
1条回答
  • 2021-01-31 18:29

    As described by Richard Biener in my bug report, the problem seems to be that GCC prior to version 8 failed to understand that a field of a class or struct was subject to the same optimizations (e.g. constant loop count) as a regular variable. So it would emit all sorts of fancy code to optimally loop an unknown number of times, even when it was known at compile time, in the case where the container was a member variable.

    The way I understand it, this bug probably affects quite a bit of code in the wild--e.g. anywhere a member small array is the subject of a C++11 range-based for loop.

    Thanks to Richard Biener for the prompt resolution (targeted for GCC 8).

    0 讨论(0)
提交回复
热议问题