https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90436

            Bug ID: 90436
           Summary: Redundant size checking in vector
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org
  Target Milestone: ---

(split from bug 87544)
#include <vector>
void f(std::vector<int>&v){v.push_back(42);}

Compiled with -O3 -fdump-tree-optimized, one can see for _M_realloc_insert
(slightly edited, _40 is the size)

  if (_40 == 2305843009213693951)
    goto <bb 3>; [0.04%]
  else
    goto <bb 4>; [99.96%]

  <bb 4> [local count: 1073312328]:
  if (_40 == 0)
    goto <bb 15>; [34.00%]
  else
    goto <bb 5>; [66.00%]

  <bb 5> [local count: 708386133]:
  __len_45 = _40 * 2;
  if (_40 > __len_45)
    goto <bb 15>; [53.03%]
  else
    goto <bb 6>; [46.97%]

  <bb 6> [local count: 438358647]:
  if (__len_45 != 0)
    goto <bb 14>; [0.00%]
  else
    goto <bb 7>; [100.00%]

  <bb 14> [local count: 0]:
  _46 = MIN_EXPR <__len_45, 2305843009213693951>;
  _10 = _46 * 4;


That is, we first check if size is exactly max_size, then if it is 0, then if
newsize=2*size overflows, then if newsize is 0, and finally we take the min of
newsize and max_size. That's more than twice as many checks as needed.

Reply via email to