[llvm] [CodeGen] Do not emit TRAP for `unreachable` after `@llvm.trap` (PR #94570)

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 15:42:17 PDT 2024


================
@@ -3076,17 +3076,17 @@ bool IRTranslator::translateUnreachable(const User &U, MachineIRBuilder &MIRBuil
     return true;
 
   auto &UI = cast<UnreachableInst>(U);
+
   // We may be able to ignore unreachable behind a noreturn call.
-  if (MF->getTarget().Options.NoTrapAfterNoreturn) {
-    const BasicBlock &BB = *UI.getParent();
-    if (&UI != &BB.front()) {
-      BasicBlock::const_iterator PredI =
-        std::prev(BasicBlock::const_iterator(UI));
-      if (const CallInst *Call = dyn_cast<CallInst>(&*PredI)) {
-        if (Call->doesNotReturn())
-          return true;
-      }
-    }
+  if (const CallInst *Call = dyn_cast_or_null<CallInst>(UI.getPrevNode());
+      Call && Call->doesNotReturn()) {
+    if (MF->getTarget().Options.NoTrapAfterNoreturn)
+      return true;
+    // Do not emit an additional trap instruction.
+    if (Function *F = Call->getCalledFunction();
+        F && F->getIntrinsicID() == Intrinsic::trap &&
----------------
igorkudrin wrote:

> What about the other types of traps, like ubsantrap?

Thanks, updated.

> Maybe CallInst should have a predicate for "this is a non-continuable trap" instead of repeating the logic in two places.

Thanks. I added a new method `CallInst::isNonContinuableTrap()` and replaced the condition.

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


More information about the llvm-commits mailing list