Index: llvm/trunk/include/llvm/IR/BasicBlock.h =================================================================== --- llvm/trunk/include/llvm/IR/BasicBlock.h +++ llvm/trunk/include/llvm/IR/BasicBlock.h @@ -433,6 +433,10 @@ // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) +/// Advance \p It while it points to a debug instruction and return the result. +/// This assumes that \p It is not at the end of a block. +BasicBlock::iterator skipDebugInfo(BasicBlock::iterator It); + } // end namespace llvm #endif // LLVM_IR_BASICBLOCK_H Index: llvm/trunk/include/llvm/IR/Instruction.h =================================================================== --- llvm/trunk/include/llvm/IR/Instruction.h +++ llvm/trunk/include/llvm/IR/Instruction.h @@ -556,6 +556,14 @@ } } + /// Return a pointer to the next non-debug instruction in the same basic + /// block as 'this', or nullptr if no such instruction exists. + const Instruction *getNextNonDebugInstruction() const; + Instruction *getNextNonDebugInstruction() { + return const_cast( + static_cast(this)->getNextNonDebugInstruction()); + } + /// Create a copy of 'this' instruction that is identical in all ways except /// the following: /// * The instruction has no parent Index: llvm/trunk/lib/IR/BasicBlock.cpp =================================================================== --- llvm/trunk/lib/IR/BasicBlock.cpp +++ llvm/trunk/lib/IR/BasicBlock.cpp @@ -479,3 +479,9 @@ } return Optional(); } + +BasicBlock::iterator llvm::skipDebugInfo(BasicBlock::iterator It) { + while (isa(It)) + ++It; + return It; +} Index: llvm/trunk/lib/IR/Instruction.cpp =================================================================== --- llvm/trunk/lib/IR/Instruction.cpp +++ llvm/trunk/lib/IR/Instruction.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/IR/Instruction.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/ADT/DenseSet.h" #include "llvm/IR/Constants.h" #include "llvm/IR/Instructions.h" @@ -594,6 +595,13 @@ !isa(this); } +const Instruction *Instruction::getNextNonDebugInstruction() const { + for (const Instruction *I = getNextNode(); I; I = I->getNextNode()) + if (!isa(I)) + return I; + return nullptr; +} + bool Instruction::isAssociative() const { unsigned Opcode = getOpcode(); if (isAssociative(Opcode)) Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -3614,13 +3614,8 @@ // happen when variable allocas are DCE'd. if (IntrinsicInst *SS = dyn_cast(II->getArgOperand(0))) { if (SS->getIntrinsicID() == Intrinsic::stacksave) { - // Skip over debug info instructions. - // FIXME: This should be an utility in Instruction.h - auto It = SS->getIterator(); - It++; - while (isa(*It)) - It++; - if (&*It == II) { + // Skip over debug info. + if (SS->getNextNonDebugInstruction() == II) { return eraseInstFromFunction(CI); } } @@ -3804,10 +3799,7 @@ // Fence instruction simplification Instruction *InstCombiner::visitFenceInst(FenceInst &FI) { // Remove identical consecutive fences. - Instruction *Next = FI.getNextNode(); - while (Next != nullptr && isa(Next)) - Next = Next->getNextNode(); - + Instruction *Next = FI.getNextNonDebugInstruction(); if (auto *NFI = dyn_cast(Next)) if (FI.isIdenticalTo(NFI)) return eraseInstFromFunction(FI); Index: llvm/trunk/unittests/IR/InstructionsTest.cpp =================================================================== --- llvm/trunk/unittests/IR/InstructionsTest.cpp +++ llvm/trunk/unittests/IR/InstructionsTest.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/AsmParser/Parser.h" #include "llvm/IR/Instructions.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Analysis/ValueTracking.h" @@ -21,6 +22,7 @@ #include "llvm/IR/Module.h" #include "llvm/IR/NoFolder.h" #include "llvm/IR/Operator.h" +#include "llvm/Support/SourceMgr.h" #include "gmock/gmock-matchers.h" #include "gtest/gtest.h" #include @@ -28,6 +30,14 @@ namespace llvm { namespace { +static std::unique_ptr parseIR(LLVMContext &C, const char *IR) { + SMDiagnostic Err; + std::unique_ptr Mod = parseAssemblyString(IR, Err, C); + if (!Mod) + Err.print("InstructionsTests", errs()); + return Mod; +} + TEST(InstructionsTest, ReturnInst) { LLVMContext C; @@ -829,5 +839,44 @@ EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3}))); } +TEST(InstructionsTest, SkipDebug) { + LLVMContext C; + std::unique_ptr M = parseIR(C, + R"( + declare void @llvm.dbg.value(metadata, metadata, metadata) + + define void @f() { + entry: + call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13 + ret void + } + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!3, !4} + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) + !1 = !DIFile(filename: "t2.c", directory: "foo") + !2 = !{} + !3 = !{i32 2, !"Dwarf Version", i32 4} + !4 = !{i32 2, !"Debug Info Version", i32 3} + !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2) + !9 = !DISubroutineType(types: !10) + !10 = !{null} + !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) + !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !13 = !DILocation(line: 2, column: 7, scope: !8) + )"); + ASSERT_TRUE(M); + Function *F = cast(M->getNamedValue("f")); + BasicBlock &BB = F->front(); + + // The first non-debug instruction is the terminator. + auto *Term = BB.getTerminator(); + EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction()); + EXPECT_EQ(Term->getIterator(), skipDebugInfo(BB.begin())); + + // After the terminator, there are no non-debug instructions. + EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction()); +} + } // end anonymous namespace } // end namespace llvm