Index: llvm/lib/CodeGen/BranchFolding.cpp =================================================================== --- llvm/lib/CodeGen/BranchFolding.cpp +++ llvm/lib/CodeGen/BranchFolding.cpp @@ -396,11 +396,12 @@ // the CFI instruction. Later on, this leads to BB2 being 'hacked off' at the // wrong place (in ReplaceTailWithBranchTo()) which results in losing this CFI // instruction. - while (I1 != MBB1->end() && I1->isCFIInstruction()) { + // Skip CFI_INSTRUCTION and debugging instruction; necessary to avoid changing the code. + while (I1 != MBB1->end() && !countsAsInstruction(*I1)) { ++I1; } - while (I2 != MBB2->end() && I2->isCFIInstruction()) { + while (I2 != MBB2->end() && !countsAsInstruction(*I2)) { ++I2; } Index: llvm/test/CodeGen/MIR/X86/branch-folder-with-debug.mir =================================================================== --- /dev/null +++ llvm/test/CodeGen/MIR/X86/branch-folder-with-debug.mir @@ -0,0 +1,101 @@ +# Generated with +# +# clang++ -g -w -O1 -S -emit-llvm test.cc +# llc -stop-before=branch-folder test.ll +# +# template class e; +# class allocator { +# public: +# ~allocator(); +# }; +# template class e { +# public: +# e(char *, allocator = allocator()); +# }; +# template bool operator==(e, b); +# class f { +# public: +# f(int *, int *, int *, int, int, int, int); +# e g(); +# void j(); +# }; +# int h, i; +# class k { +# void l(); +# bool m_fn4(); +# int m; +# int n; +# int q; +# int fmap; +# }; +# void k::l() { +# e o = ""; +# for (;;) { +# int p = 0; +# for (;;) { +# if (m_fn4()) +# break; +# f a(&q, &fmap, &m, n, h, i, 0); +# if (a.g() == "") +# a.j(); +# } +# } +# } + +# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=branch-folder | FileCheck %s +--- | + + define dso_local void @l() { + ret void + } + +... +--- +name: l +body: | + bb.0: + ; CHECK: successors: %bb.1(0x40000000), %bb.7(0x40000000) + successors: %bb.1, %bb.3 + + bb.1: + $rdi = MOV64rr $rsp + + bb.2: + ; CHECK: successors: %bb.3(0x80000000) + successors: %bb.2, %bb.4 + DBG_VALUE + CFI_INSTRUCTION def_cfa_offset 8 + ; CHECK: bb.3: + ; CHECK: successors: %bb.2(0x40000000), %bb.4(0x40000000) + TEST8rr killed renamable $al, renamable $al, implicit-def $eflags + JCC_1 %bb.2, 5, implicit killed $eflags + JMP_1 %bb.4 + + ; CHECK: bb.4: + ; CHECK: successors: %bb.5(0x40000000), %bb.6(0x40000000) + bb.3 (landing-pad): + successors: + + bb.4: + successors: %bb.5, %bb.6 + ; CHECK: JCC_1 %bb.6, 4, implicit $eflags + JCC_1 %bb.6, 4, implicit killed $eflags + JMP_1 %bb.5 + + ; CHECK: bb.5: + ; CHECK: successors: %bb.6(0x80000000) + bb.5: + $rdi = COPY renamable $r12 + + ; CHECK: bb.6: + ; CHECK: successors: %bb.3(0x80000000) + bb.6: + successors: %bb.2, %bb.4 + ; CHECK: JMP_1 %bb.3 + ; CHECK: bb.7 (landing-pad): + $rdi = COPY renamable $rbx + TEST8rr killed renamable $al, renamable $al, implicit-def $eflags + JCC_1 %bb.2, 5, implicit killed $eflags + JMP_1 %bb.4 + +...