[llvm] [StructurizeCFG] Order IF Else block using Heuristics (PR #139605)

Vigneshwar Jayakumar via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 10 14:59:23 PDT 2025


VigneshwarJ wrote:

@ruiling Thanks for the suggestion. 
I tried your suggestion of hoisting the zero-cost instructions from the if or else block to the parent block. But there are still extra copies due to interference.
Consider this code,
```llvm
if:
  %load_then = load %pair, ptr %ptr
  br i1 %cond, label %then, label %else

then:
  %a_then = extractvalue %pair %load_then, 0
  br label %merge

else:
  %a_else = extractvalue %pair %load_then, 0
  %sum_else = add i32 %a_else, 1
  br label %merge

merge:
  %phi = phi i32  [ %a_then, %then ], [ %sum_else, %else ]
  store i32 %phi, ptr  %ptr
  ret void
```
structurizeCFG in trunk - [https://godbolt.org/z/Gc6Wbocef](url)

ISA with structurizeCFG with hoisting zero-cost instruction change - [https://godbolt.org/z/cEzs31dYq](url). 
ISA with structurizeCFG with reorder change - [https://godbolt.org/z/WMcYhG3Wv](url).

There is an extra copy even with hoisting change because, in the default SCC order, 'else' block comes first arbitrarily. Though the 'then' block doesn't contain any code now, 'copy' can't be coalesced due to interference (else->flow->then path) like I mentioned in Matt's comment.


https://github.com/llvm/llvm-project/pull/139605


More information about the llvm-commits mailing list