[llvm] [IR] Don't skip `catchswitch` instructions when calling `getFirstNonPHIOrDbgOrAlloca()` (PR #136056)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 16 16:44:47 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Tyler Lanphear (tylanphear)
<details>
<summary>Changes</summary>
`BasicBlock::getFirstNonPHIOrDbgOrAlloca()` is supposed to yield the first instruction in the block which is not a PHI or dbg or alloca instruction. To that effect, it also skips an EHPad instruction which might be present after any PHIs. Normally, the latter is fine, but it has an unexpected interaction with `catchswitch` blocks:
```
bb:
; optional PHIs
%0 = catchswitch within none [label %catch] unwind to caller
```
If we skip over the `catchswitch` instruction in the previous block, we'll return the end iterator, despite `catchswitch` not being a PHI or dbg or alloca instruction.
This patch amends the logic to not skip an EHPad instruction which is also a terminator. This means that calling
`getFirstNonPHIOrDbgOrAlloca()` on a non-empty block with a terminator (such as the above) will always yield a valid instruction iterator. This appears to be the expected behavior anyways, as most usages of this method do not check that the returned iterator is `end()`.
Closes #<!-- -->136048.
---
Full diff: https://github.com/llvm/llvm-project/pull/136056.diff
1 Files Affected:
- (modified) llvm/lib/IR/BasicBlock.cpp (+1-1)
``````````diff
diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index c632b1b2dc2ab..f870fb53307ea 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -455,7 +455,7 @@ BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
if (InsertPt == end())
return end();
- if (InsertPt->isEHPad())
+ if (InsertPt->isEHPad() && !InsertPt->isTerminator())
++InsertPt;
if (isEntryBlock()) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/136056
More information about the llvm-commits
mailing list