[llvm] r330316 - [BasicBlock] Add instructionsWithoutDebug methods to skip debug insts.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 19 02:48:07 PDT 2018


Author: fhahn
Date: Thu Apr 19 02:48:07 2018
New Revision: 330316

URL: http://llvm.org/viewvc/llvm-project?rev=330316&view=rev
Log:
[BasicBlock] Add instructionsWithoutDebug methods to skip debug insts.

Reviewers: aprantl, vsk, mattd, chandlerc

Reviewed By: aprantl, vsk

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

Modified:
    llvm/trunk/include/llvm/IR/BasicBlock.h
    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=330316&r1=330315&r2=330316&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/BasicBlock.h (original)
+++ llvm/trunk/include/llvm/IR/BasicBlock.h Thu Apr 19 02:48:07 2018
@@ -182,6 +182,18 @@ public:
                                           ->getFirstInsertionPt().getNonConst();
   }
 
+  /// Return a const iterator range over the instructions in the block, skipping
+  /// any debug instructions.
+  iterator_range<filter_iterator<BasicBlock::const_iterator,
+                                 std::function<bool(const Instruction &)>>>
+  instructionsWithoutDebug() const;
+
+  /// Return an iterator range over the instructions in the block, skipping any
+  /// debug instructions.
+  iterator_range<filter_iterator<BasicBlock::iterator,
+                                 std::function<bool(Instruction &)>>>
+  instructionsWithoutDebug();
+
   /// Unlink 'this' from the containing function, but do not delete it.
   void removeFromParent();
 

Modified: llvm/trunk/lib/IR/BasicBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/BasicBlock.cpp?rev=330316&r1=330315&r2=330316&view=diff
==============================================================================
--- llvm/trunk/lib/IR/BasicBlock.cpp (original)
+++ llvm/trunk/lib/IR/BasicBlock.cpp Thu Apr 19 02:48:07 2018
@@ -90,6 +90,24 @@ void BasicBlock::setParent(Function *par
   InstList.setSymTabObject(&Parent, parent);
 }
 
+iterator_range<filter_iterator<BasicBlock::const_iterator,
+                               std::function<bool(const Instruction &)>>>
+BasicBlock::instructionsWithoutDebug() const {
+  std::function<bool(const Instruction &)> Fn = [](const Instruction &I) {
+    return !isa<DbgInfoIntrinsic>(I);
+  };
+  return make_filter_range(*this, Fn);
+}
+
+iterator_range<filter_iterator<BasicBlock::iterator,
+                               std::function<bool(Instruction &)>>>
+BasicBlock::instructionsWithoutDebug() {
+  std::function<bool(Instruction &)> Fn = [](Instruction &I) {
+    return !isa<DbgInfoIntrinsic>(I);
+  };
+  return make_filter_range(*this, Fn);
+}
+
 void BasicBlock::removeFromParent() {
   getParent()->getBasicBlockList().remove(getIterator());
 }

Modified: llvm/trunk/unittests/IR/BasicBlockTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/BasicBlockTest.cpp?rev=330316&r1=330315&r2=330316&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/BasicBlockTest.cpp (original)
+++ llvm/trunk/unittests/IR/BasicBlockTest.cpp Thu Apr 19 02:48:07 2018
@@ -77,5 +77,44 @@ TEST(BasicBlockTest, PhiRange) {
   }
 }
 
+#define CHECK_ITERATORS(Range1, Range2)                                        \
+  EXPECT_EQ(std::distance(Range1.begin(), Range1.end()),                       \
+            std::distance(Range2.begin(), Range2.end()));                      \
+  for (auto Pair : zip(Range1, Range2))                                        \
+    EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
+
+TEST(BasicBlockTest, TestSkipInsts) {
+  LLVMContext Ctx;
+
+  std::unique_ptr<Module> M(new Module("MyModule", Ctx));
+  Type *ArgTy1[] = {Type::getInt32PtrTy(Ctx)};
+  FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), ArgTy1, false);
+  auto *V = new Argument(Type::getInt32Ty(Ctx));
+  Function *F = Function::Create(FT, Function::ExternalLinkage, "", M.get());
+
+  Value *DbgAddr = Intrinsic::getDeclaration(M.get(), Intrinsic::dbg_addr);
+  Value *DbgDeclare =
+      Intrinsic::getDeclaration(M.get(), Intrinsic::dbg_declare);
+  Value *DbgValue = Intrinsic::getDeclaration(M.get(), Intrinsic::dbg_value);
+  Value *DIV = MetadataAsValue::get(Ctx, (Metadata *)nullptr);
+  SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
+
+  BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
+  const BasicBlock *BBConst = BB1;
+  IRBuilder<> Builder1(BB1);
+
+  AllocaInst *Var = Builder1.CreateAlloca(Builder1.getInt8Ty());
+  Builder1.CreateCall(DbgValue, Args);
+  Instruction *AddInst = cast<Instruction>(Builder1.CreateAdd(V, V));
+  Instruction *MulInst = cast<Instruction>(Builder1.CreateMul(AddInst, V));
+  Builder1.CreateCall(DbgDeclare, Args);
+  Instruction *SubInst = cast<Instruction>(Builder1.CreateSub(MulInst, V));
+  Builder1.CreateCall(DbgAddr, Args);
+
+  SmallVector<Instruction *, 4> Exp = {Var, AddInst, MulInst, SubInst};
+  CHECK_ITERATORS(BB1->instructionsWithoutDebug(), Exp);
+  CHECK_ITERATORS(BBConst->instructionsWithoutDebug(), Exp);
+}
+
 } // End anonymous namespace.
 } // End llvm namespace.




More information about the llvm-commits mailing list