[flang] Fix invalid branch optimization
authorV Donaldson <vdonaldson@nvidia.com>
Sat, 10 Sep 2022 04:23:50 +0000 (21:23 -0700)
committerV Donaldson <vdonaldson@nvidia.com>
Mon, 12 Sep 2022 02:37:32 +0000 (19:37 -0700)
commita3c05df16bc66d8ac84c1b10a74f59b12eb25a94
treea49ab437e9b13f2531d90712502bb78b236327d0
parent180e9f60c1f48c11e79fa516e2358d0f0be76ca6
[flang] Fix invalid branch optimization

Branch optimization in function rewriteIfGotos attempts to rewrite code
such as

    <<IfConstruct>>
      1 If[Then]Stmt: if(cond) goto L
      2 GotoStmt: goto L
      3 EndIfStmt
    <<End IfConstruct>>
    4 Statement: ...
    5 Statement: ...
    6 Statement: L ...

to eliminate a branch and a trivial basic block to get:

    <<IfConstruct>>
      1 If[Then]Stmt [negate]: if(cond) goto L
      4 Statement: ...
      5 Statement: ...
      3 EndIfStmt
    <<End IfConstruct>>
    6 Statement: L ...

Among other requirements, this is invalid if any statement between the
GOTO and its target is an intermediate construct statement such as a
CASE or ELSE IF statement, like the CASE DEFAULT statement in:

  select case(i)
  case (:2)
    n = i * 10
  case (5:)
    n = i * 1000
    if (i <= 6) goto 9 ! exit over 'case default'; may not be rewritten
    n = i * 10000
  case default
    n = i * 100
9 end select
flang/lib/Lower/PFTBuilder.cpp
flang/test/Lower/select-case-statement.f90