This is an archive of the discontinued LLVM Phabricator instance.

[flang] Fix invalid branch optimization
ClosedPublic

Authored by vdonaldson on Sep 9 2022, 10:03 PM.

Details

Summary

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

Diff Detail

Event Timeline

vdonaldson created this revision.Sep 9 2022, 10:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 10:03 PM
vdonaldson requested review of this revision.Sep 9 2022, 10:03 PM
PeteSteinfeld accepted this revision.Sep 10 2022, 7:58 AM

All builds, tests, and looks good.

This revision is now accepted and ready to land.Sep 10 2022, 7:58 AM