[llvm] r372389 - [FastISel] Fix insertion of unconditional branches during FastISel

David Tellenbach via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 20 06:22:59 PDT 2019


Author: tellenbach
Date: Fri Sep 20 06:22:59 2019
New Revision: 372389

URL: http://llvm.org/viewvc/llvm-project?rev=372389&view=rev
Log:
[FastISel] Fix insertion of unconditional branches during FastISel

The insertion of an unconditional branch during FastISel can differ depending on
building with or without debug information. This happens because FastISel::fastEmitBranch
emits an unconditional branch depending on the size of the current basic block
without distinguishing between debug and non-debug instructions.

This patch fixes this issue by ignoring debug instructions when getting the size
of the basic block.

Reviewers: aprantl

Reviewed By: aprantl

Subscribers: ormris, aprantl, hiraditya, llvm-commits

Tags: #llvm

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

Added:
    llvm/trunk/test/CodeGen/AArch64/fast-isel-branch-uncond-debug.ll
Modified:
    llvm/trunk/include/llvm/IR/BasicBlock.h
    llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp
    llvm/trunk/lib/IR/BasicBlock.cpp
    llvm/trunk/unittests/IR/BasicBlockTest.cpp

Modified: llvm/trunk/include/llvm/IR/BasicBlock.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/BasicBlock.h?rev=372389&r1=372388&r2=372389&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/BasicBlock.h (original)
+++ llvm/trunk/include/llvm/IR/BasicBlock.h Fri Sep 20 06:22:59 2019
@@ -192,6 +192,11 @@ public:
                                  std::function<bool(Instruction &)>>>
   instructionsWithoutDebug();
 
+  /// Return the size of the basic block ignoring debug instructions
+  filter_iterator<BasicBlock::const_iterator,
+                  std::function<bool(const Instruction &)>>::difference_type
+  sizeWithoutDebug() const;
+
   /// Unlink 'this' from the containing function, but do not delete it.
   void removeFromParent();
 

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp?rev=372389&r1=372388&r2=372389&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/FastISel.cpp Fri Sep 20 06:22:59 2019
@@ -1677,11 +1677,11 @@ bool FastISel::selectInstruction(const I
 /// (fall-through) successor, and update the CFG.
 void FastISel::fastEmitBranch(MachineBasicBlock *MSucc,
                               const DebugLoc &DbgLoc) {
-  if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
+  if (FuncInfo.MBB->getBasicBlock()->sizeWithoutDebug() > 1 &&
       FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
-    // For more accurate line information if this is the only instruction
-    // in the block then emit it, otherwise we have the unconditional
-    // fall-through case, which needs no instructions.
+    // For more accurate line information if this is the only non-debug
+    // instruction in the block then emit it, otherwise we have the
+    // unconditional fall-through case, which needs no instructions.
   } else {
     // The unconditional branch case.
     TII.insertBranch(*FuncInfo.MBB, MSucc, nullptr,

Modified: llvm/trunk/lib/IR/BasicBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/BasicBlock.cpp?rev=372389&r1=372388&r2=372389&view=diff
==============================================================================
--- llvm/trunk/lib/IR/BasicBlock.cpp (original)
+++ llvm/trunk/lib/IR/BasicBlock.cpp Fri Sep 20 06:22:59 2019
@@ -107,6 +107,13 @@ BasicBlock::instructionsWithoutDebug() {
   return make_filter_range(*this, Fn);
 }
 
+filter_iterator<BasicBlock::const_iterator,
+                std::function<bool(const Instruction &)>>::difference_type
+BasicBlock::sizeWithoutDebug() const {
+  return std::distance(instructionsWithoutDebug().begin(),
+                       instructionsWithoutDebug().end());
+}
+
 void BasicBlock::removeFromParent() {
   getParent()->getBasicBlockList().remove(getIterator());
 }

Added: llvm/trunk/test/CodeGen/AArch64/fast-isel-branch-uncond-debug.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/fast-isel-branch-uncond-debug.ll?rev=372389&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/AArch64/fast-isel-branch-uncond-debug.ll (added)
+++ llvm/trunk/test/CodeGen/AArch64/fast-isel-branch-uncond-debug.ll Fri Sep 20 06:22:59 2019
@@ -0,0 +1,44 @@
+; RUN: llc -mtriple=aarch64-arm-none-eabi -O1 -opt-bisect-limit=2 -o - %s  2> /dev/null | FileCheck %s
+
+define dso_local i32 @a() #0 !dbg !7 {
+entry:
+; CHECK:    b       .LBB0_1
+; CHECK:  .LBB0_1:
+  call void @llvm.dbg.value(metadata i32 0, metadata !12, metadata !DIExpression()), !dbg !13
+  br label %for.cond, !dbg !14
+
+; CHECK:    b       .LBB0_1
+; CHECK:  .Lfunc_end0:
+for.cond:
+  br label %for.cond, !dbg !15, !llvm.loop !18
+}
+declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1
+
+declare void @llvm.dbg.value(metadata, metadata, metadata) #2
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4, !5}
+!llvm.ident = !{!6}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
+!1 = !DIFile(filename: "fast-isel-branch-uncond-debug.ll", directory: "/tmp")
+!2 = !{}
+!3 = !{i32 2, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !{i32 1, !"wchar_size", i32 4}
+!6 = !{!""}
+!7 = distinct !DISubprogram(name: "a", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !{!12}
+!12 = !DILocalVariable(name: "b", scope: !7, file: !1, line: 2, type: !10)
+!13 = !DILocation(line: 0, scope: !7)
+!14 = !DILocation(line: 3, column: 3, scope: !7)
+!15 = !DILocation(line: 3, column: 3, scope: !16)
+!16 = distinct !DILexicalBlock(scope: !17, file: !1, line: 3, column: 3)
+!17 = distinct !DILexicalBlock(scope: !7, file: !1, line: 3, column: 3)
+!18 = distinct !{!18, !19, !20}
+!19 = !DILocation(line: 3, column: 3, scope: !17)
+!20 = !DILocation(line: 4, column: 5, scope: !17)
+ 
\ No newline at end of file

Modified: llvm/trunk/unittests/IR/BasicBlockTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/BasicBlockTest.cpp?rev=372389&r1=372388&r2=372389&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/BasicBlockTest.cpp (original)
+++ llvm/trunk/unittests/IR/BasicBlockTest.cpp Fri Sep 20 06:22:59 2019
@@ -122,6 +122,9 @@ TEST(BasicBlockTest, TestInstructionsWit
   CHECK_ITERATORS(BB1->instructionsWithoutDebug(), Exp);
   CHECK_ITERATORS(BBConst->instructionsWithoutDebug(), Exp);
 
+  EXPECT_EQ(static_cast<size_t>(BB1->sizeWithoutDebug()), Exp.size());
+  EXPECT_EQ(static_cast<size_t>(BBConst->sizeWithoutDebug()), Exp.size());
+
   delete M;
   delete V;
 }




More information about the llvm-commits mailing list