https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105053
Bug ID: 105053 Summary: Wrong loop count for scalar code from vectorizer Product: gcc Version: 12.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Target Milestone: --- #include <vector> #include <tuple> #include <algorithm> #include <random> #include <iostream> int main(){ const long n = 100000000; std::vector<std::tuple<int,int,double>> vec; vec.reserve(n); std::random_device rd; std::default_random_engine re(rd()); std::uniform_int_distribution<int> rand_int; std::uniform_real_distribution<double> rand_dbl; for(int i=0;i<n;++i) vec.emplace_back(rand_int(re), rand_int(re), rand_dbl(re)); { int sup = 0; for(int i=0;i<n;++i) sup=std::max(sup,std::max(std::get<0>(vec[i]),std::get<1>(vec[i]))); std::cout << sup << '\n'; } { int sup = 0; for(int i=0;i<n;++i) sup=std::max(std::max(sup,std::get<0>(vec[i])),std::get<1>(vec[i])); std::cout << sup << '\n'; } } Can output for instance 2147483645 2147483637 compiled with -O3, whereas the 2 numbers should be the same. If I compare what I get from the first loop with -O3 -fno-tree-loop-vectorize to the second loop with just -O3, the code is almost identical, except that the (scalar) code only iterates on 1/4 of the array, as if it was using a bound meant for a vector. -fno-tree-loop-vectorize seems to be ok.