[llvm-branch-commits] [llvm] d73eb82 - [llvm][IfConversion] update successor list when merging INLINEASM_BR

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Feb 9 05:00:59 PST 2023


Author: Nick Desaulniers
Date: 2023-02-09T13:55:14+01:00
New Revision: d73eb820244b7b02e95ee3e47238f6f6527180c5

URL: https://github.com/llvm/llvm-project/commit/d73eb820244b7b02e95ee3e47238f6f6527180c5
DIFF: https://github.com/llvm/llvm-project/commit/d73eb820244b7b02e95ee3e47238f6f6527180c5.diff

LOG: [llvm][IfConversion] update successor list when merging INLINEASM_BR

If this successor list is not correct, then branch-folding may
incorrectly think that the indirect target is dead and remove it. This
results in a dangling reference to the removed block as an operand to
the INLINEASM_BR, which later will get AsmPrinted into code that doesn't
assemble.

This was made more obvious by, but is not a regression of
https://reviews.llvm.org/D130316.

Fixes: https://github.com/llvm/llvm-project/issues/60346

Reviewed By: efriedma, void

Differential Revision: https://reviews.llvm.org/D142924

(cherry picked from commit 07c7784d7bf69b9944b92ecc283cac823bcfce16)

Added: 
    

Modified: 
    llvm/lib/CodeGen/IfConversion.cpp
    llvm/test/CodeGen/ARM/inlineasmbr-if-cvt.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/IfConversion.cpp b/llvm/lib/CodeGen/IfConversion.cpp
index 105ab908d3fab..936de316e5754 100644
--- a/llvm/lib/CodeGen/IfConversion.cpp
+++ b/llvm/lib/CodeGen/IfConversion.cpp
@@ -2244,6 +2244,15 @@ void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
   assert(!FromMBB.hasAddressTaken() &&
          "Removing a BB whose address is taken!");
 
+  // If we're about to splice an INLINEASM_BR from FromBBI, we need to update
+  // ToBBI's successor list accordingly.
+  if (FromMBB.mayHaveInlineAsmBr())
+    for (MachineInstr &MI : FromMBB)
+      if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
+        for (MachineOperand &MO : MI.operands())
+          if (MO.isMBB() && !ToBBI.BB->isSuccessor(MO.getMBB()))
+            ToBBI.BB->addSuccessor(MO.getMBB(), BranchProbability::getZero());
+
   // In case FromMBB contains terminators (e.g. return instruction),
   // first move the non-terminator instructions, then the terminators.
   MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator();

diff  --git a/llvm/test/CodeGen/ARM/inlineasmbr-if-cvt.mir b/llvm/test/CodeGen/ARM/inlineasmbr-if-cvt.mir
index 7b41aa41b823e..ad45edc9835f5 100644
--- a/llvm/test/CodeGen/ARM/inlineasmbr-if-cvt.mir
+++ b/llvm/test/CodeGen/ARM/inlineasmbr-if-cvt.mir
@@ -1,6 +1,6 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
 # RUN: llc -start-before=if-converter -stop-after=if-converter -o - %s \
-# RUN:   | FileCheck %s
+# RUN:   -verify-machineinstrs | FileCheck %s
 --- |
   ; ModuleID = 'reduced.ll'
   source_filename = "reduced.ll"
@@ -80,14 +80,23 @@ machineFunctionInfo: {}
 body:             |
   ; CHECK-LABEL: name: tusb1210_chg_det_work
   ; CHECK: bb.0.entry:
-  ; CHECK-NEXT:   successors:
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000), %bb.2(0x80000000)
   ; CHECK-NEXT:   liveins: $r0
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   TSTri killed renamable $r0, 1, 14 /* CC::al */, $noreg, implicit-def $cpsr
   ; CHECK-NEXT:   renamable $r0 = MOVi 0, 1 /* CC::ne */, $cpsr, $noreg
-  ; CHECK-NEXT:   dead renamable $r0 = MOVi 1, 0 /* CC::eq */, killed $cpsr, $noreg, implicit killed $r0
-  ; FIXME: %bb.-1 is a dangling reference!!!
-  ; CHECK-NEXT:   INLINEASM_BR &".word b, ${1:l}, ${0:c}\0A\09", 9 /* sideeffect mayload attdialect */, 13 /* imm */, 0, 13 /* imm */, %bb.-1
+  ; CHECK-NEXT:   renamable $r0 = MOVi 1, 0 /* CC::eq */, killed $cpsr, $noreg, implicit killed $r0
+  ; CHECK-NEXT:   INLINEASM_BR &".word b, ${1:l}, ${0:c}\0A\09", 9 /* sideeffect mayload attdialect */, 13 /* imm */, 0, 13 /* imm */, %bb.1
+  ; CHECK-NEXT:   B %bb.2
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1.if.end.sink.split (machine-block-address-taken, inlineasm-br-indirect-target):
+  ; CHECK-NEXT:   successors: %bb.2(0x80000000)
+  ; CHECK-NEXT:   liveins: $r0
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $r1 = MOVi 0, 14 /* CC::al */, $noreg, $noreg
+  ; CHECK-NEXT:   STRi12 killed renamable $r0, killed renamable $r1, 0, 14 /* CC::al */, $noreg :: (store (s32) into `ptr null`)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2.if.end:
   ; CHECK-NEXT:   BX_RET 14 /* CC::al */, $noreg
   bb.0.entry:
     successors: %bb.4(0x40000000), %bb.1(0x40000000)


        


More information about the llvm-branch-commits mailing list