llvm.org GIT mirror llvm / 83a6451
[IR] Introduce helpers to skip debug instructions (NFC) This patch introduces two helpers to make it easier to ignore debug intrinsics: - Instruction::getNextNonDebugInstruction() This is just like Instruction::getNextNode(), except that it skips debug info. - skipDebugInfo(BasicBlock::iterator) A free function which advances a BasicBlock iterator past any debug info. This is a no-op when the iterator already points to a non-debug instruction. Part of: llvm.org/PR37728 Related to: https://reviews.llvm.org/D47874 Differential Revision: https://reviews.llvm.org/D48305 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335083 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 1 year, 3 months ago
6 changed file(s) with 78 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
432432 // Create wrappers for C Binding types (see CBindingWrapping.h).
433433 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef)
434434
435 /// Advance \p It while it points to a debug instruction and return the result.
436 /// This assumes that \p It is not at the end of a block.
437 BasicBlock::iterator skipDebugInfo(BasicBlock::iterator It);
438
435439 } // end namespace llvm
436440
437441 #endif // LLVM_IR_BASICBLOCK_H
555555 }
556556 }
557557
558 /// Return a pointer to the next non-debug instruction in the same basic
559 /// block as 'this', or nullptr if no such instruction exists.
560 const Instruction *getNextNonDebugInstruction() const;
561 Instruction *getNextNonDebugInstruction() {
562 return const_cast(
563 static_cast(this)->getNextNonDebugInstruction());
564 }
565
558566 /// Create a copy of 'this' instruction that is identical in all ways except
559567 /// the following:
560568 /// * The instruction has no parent
478478 }
479479 return Optional();
480480 }
481
482 BasicBlock::iterator llvm::skipDebugInfo(BasicBlock::iterator It) {
483 while (isa(It))
484 ++It;
485 return It;
486 }
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/IntrinsicInst.h"
1415 #include "llvm/ADT/DenseSet.h"
1516 #include "llvm/IR/Constants.h"
1617 #include "llvm/IR/Instructions.h"
593594 !isa(this);
594595 }
595596
597 const Instruction *Instruction::getNextNonDebugInstruction() const {
598 for (const Instruction *I = getNextNode(); I; I = I->getNextNode())
599 if (!isa(I))
600 return I;
601 return nullptr;
602 }
603
596604 bool Instruction::isAssociative() const {
597605 unsigned Opcode = getOpcode();
598606 if (isAssociative(Opcode))
36133613 // happen when variable allocas are DCE'd.
36143614 if (IntrinsicInst *SS = dyn_cast(II->getArgOperand(0))) {
36153615 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
3616 // Skip over debug info instructions.
3617 // FIXME: This should be an utility in Instruction.h
3618 auto It = SS->getIterator();
3619 It++;
3620 while (isa(*It))
3621 It++;
3622 if (&*It == II) {
3616 // Skip over debug info.
3617 if (SS->getNextNonDebugInstruction() == II) {
36233618 return eraseInstFromFunction(CI);
36243619 }
36253620 }
38033798 // Fence instruction simplification
38043799 Instruction *InstCombiner::visitFenceInst(FenceInst &FI) {
38053800 // Remove identical consecutive fences.
3806 Instruction *Next = FI.getNextNode();
3807 while (Next != nullptr && isa(Next))
3808 Next = Next->getNextNode();
3809
3801 Instruction *Next = FI.getNextNonDebugInstruction();
38103802 if (auto *NFI = dyn_cast(Next))
38113803 if (FI.isIdenticalTo(NFI))
38123804 return eraseInstFromFunction(FI);
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "llvm/AsmParser/Parser.h"
910 #include "llvm/IR/Instructions.h"
1011 #include "llvm/ADT/STLExtras.h"
1112 #include "llvm/Analysis/ValueTracking.h"
2021 #include "llvm/IR/Module.h"
2122 #include "llvm/IR/NoFolder.h"
2223 #include "llvm/IR/Operator.h"
24 #include "llvm/Support/SourceMgr.h"
2325 #include "gmock/gmock-matchers.h"
2426 #include "gtest/gtest.h"
2527 #include
2628
2729 namespace llvm {
2830 namespace {
31
32 static std::unique_ptr parseIR(LLVMContext &C, const char *IR) {
33 SMDiagnostic Err;
34 std::unique_ptr Mod = parseAssemblyString(IR, Err, C);
35 if (!Mod)
36 Err.print("InstructionsTests", errs());
37 return Mod;
38 }
2939
3040 TEST(InstructionsTest, ReturnInst) {
3141 LLVMContext C;
828838 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3})));
829839 }
830840
841 TEST(InstructionsTest, SkipDebug) {
842 LLVMContext C;
843 std::unique_ptr M = parseIR(C,
844 R"(
845 declare void @llvm.dbg.value(metadata, metadata, metadata)
846
847 define void @f() {
848 entry:
849 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
850 ret void
851 }
852
853 !llvm.dbg.cu = !{!0}
854 !llvm.module.flags = !{!3, !4}
855 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
856 !1 = !DIFile(filename: "t2.c", directory: "foo")
857 !2 = !{}
858 !3 = !{i32 2, !"Dwarf Version", i32 4}
859 !4 = !{i32 2, !"Debug Info Version", i32 3}
860 !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)
861 !9 = !DISubroutineType(types: !10)
862 !10 = !{null}
863 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
864 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
865 !13 = !DILocation(line: 2, column: 7, scope: !8)
866 )");
867 ASSERT_TRUE(M);
868 Function *F = cast(M->getNamedValue("f"));
869 BasicBlock &BB = F->front();
870
871 // The first non-debug instruction is the terminator.
872 auto *Term = BB.getTerminator();
873 EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
874 EXPECT_EQ(Term->getIterator(), skipDebugInfo(BB.begin()));
875
876 // After the terminator, there are no non-debug instructions.
877 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
878 }
879
831880 } // end anonymous namespace
832881 } // end namespace llvm