From 8598a84bf5ca91ddd26f3bf6f944b0235cfa06b4 Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Tue, 11 Feb 2025 21:00:06 -0800 Subject: [PATCH] loop-invariant: Treat inline-asm conditional trapping [PR102150] So inline-asm is known not to trap BUT it can have undefined behavior if made executed speculatively. This fixes the loop invariant pass to treat it similarly as trapping cases. If the inline-asm could be executed always, then it will be pulled out of the loop; otherwise it will be kept inside the loop. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: PR rtl-optimization/102150 * loop-invariant.cc (find_invariant_insn): Treat inline-asm similar to trapping instruction and only move them if always executed. Signed-off-by: Andrew Pinski --- gcc/loop-invariant.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gcc/loop-invariant.cc b/gcc/loop-invariant.cc index bcb52bb9c76..236d5ee70ee 100644 --- a/gcc/loop-invariant.cc +++ b/gcc/loop-invariant.cc @@ -1123,6 +1123,11 @@ find_invariant_insn (rtx_insn *insn, bool always_reached, bool always_executed) if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached) return; + /* inline-asm that is not always executed cannot be moved + as it might conditionally trap. */ + if (!always_reached && asm_noperands (PATTERN (insn)) >= 0) + return; + depends_on = BITMAP_ALLOC (NULL); if (!check_dependencies (insn, depends_on)) {