llvm / llvm/llvm-project

[LSROA] Handle PHI & Select instructions

Open
#193,749 0 comments 0 reactions 1 assignee Claimed by @bogner View on GitHub
enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

For now, the LSROA bails out when the SGEP is used in a phi node or a select instruction.
We should improve the pass to look through the user and still allow splitting (as long as pointer is not escaping).

### Acceptance Criteria

At the basic level we need to handle looking through phi nodes to `structured.gep` into `structured.alloc`. Consider:
```llvm
define i32 @test_partial_use_phi_node(i1 %cond) {
entry:
%tmp = call elementtype({ i32, i32 }) ptr @llvm.structured.alloca.p0()
%ptr0 = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype({ i32, i32 }) %tmp, i32 0)
%ptr1 = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype({ i32, i32 }) %tmp, i32 1)
br i1 %cond, label %l1, label %l2

l1:
call void @llvm.lifetime.start.p0(ptr %tmp)
store i32 0, ptr %ptr0
br label %l3

l2:
call void @llvm.lifetime.start.p0(ptr %tmp)
store i32 1, ptr %ptr1
br label %l3

l3:
%ptr = phi ptr [ %ptr0, %l1 ], [ %ptr1, %l2 ]
%a = load i32, ptr %ptr
call void @llvm.lifetime.end.p0(ptr %tmp)
br label %exit

exit:
ret i32 %a
}
```

We should be able to elminate the `structured.gep`s:
```llvm
define i32 @test_partial_use_phi_node(i1 %cond) {
entry:
%tmp0_i32 = call elementtype(i32) ptr @llvm.structured.alloca.p0()
%tmp1_i32 = call elementtype(i32) ptr @llvm.structured.alloca.p0()
br i1 %cond, label %l1, label %l2

l1:
call void @llvm.lifetime.start.p0(ptr %tmp0_i32)
store i32 0, ptr %tmp0_i32
br label %l3

l2:
call void @llvm.lifetime.start.p0(ptr %tmp1_i32)
store i32 1, ptr %tmp1_i32
br label %l3

l3:
%ptr = phi ptr [ %tmp0_i32, %l1 ], [ %tmp1_i32, %l2 ]
%a = load i32, ptr %ptr
call void @llvm.lifetime.end.p0(ptr %tmp0_i32)
call void @llvm.lifetime.end.p0(ptr %tmp1_i32)
br label %exit

exit:
ret i32 %a
}
```

We should handle `select` in a similar way.

We will need to update the existing [LSROA/phi.ll] and [LSROA/select.ll] test cases to show that this works, and add a few more complicated cases and cases where we demonstrate that we leave the sgeps unmodified.

[LSROA/phi.ll]: https://github.com/llvm/llvm-project/blob/03b5c52/llvm/test/Transforms/LSROA/phi.ll
[LSROA/select.ll]: https://github.com/llvm/llvm-project/blob/03b5c52/llvm/test/Transforms/LSROA/select.ll

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.