[Bug fortran/85599] invalid optimization: function not always evaluated in logical expression

janus at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed May 2 11:58:00 GMT 2018


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

--- Comment #6 from janus at gcc dot gnu.org ---
Another variant, this time with the (non-std) intrinsic function AND:


program lazy

   logical :: flag

   flag = .false.
   flag = AND(check(), flag)
   flag = AND(flag, check())

contains

   logical function check()
      integer, save :: i = 1
      print *, "check", i
      i = i + 1
      check = .true.
   end function

end


This calls 'check' twice, as I would expect, and -fdump-tree-original shows
that it is translated to this pseudo C code:

  flag = check () & flag;
  flag = check () & flag;

While the original test case translates to:

  flag = check () && flag;
  flag = flag && check ();

Changing the operand order (as done in the first case) does not matter here.
Obviously the crucial difference is the use of & vs &&.


More information about the Gcc-bugs mailing list