Index: llvm/trunk/include/llvm/IR/BasicBlock.h =================================================================== --- llvm/trunk/include/llvm/IR/BasicBlock.h +++ llvm/trunk/include/llvm/IR/BasicBlock.h @@ -182,6 +182,18 @@ ->getFirstInsertionPt().getNonConst(); } + /// Return a const iterator range over the instructions in the block, skipping + /// any debug instructions. + iterator_range>> + instructionsWithoutDebug() const; + + /// Return an iterator range over the instructions in the block, skipping any + /// debug instructions. + iterator_range>> + instructionsWithoutDebug(); + /// Unlink 'this' from the containing function, but do not delete it. void removeFromParent(); Index: llvm/trunk/lib/IR/BasicBlock.cpp =================================================================== --- llvm/trunk/lib/IR/BasicBlock.cpp +++ llvm/trunk/lib/IR/BasicBlock.cpp @@ -90,6 +90,24 @@ InstList.setSymTabObject(&Parent, parent); } +iterator_range>> +BasicBlock::instructionsWithoutDebug() const { + std::function Fn = [](const Instruction &I) { + return !isa(I); + }; + return make_filter_range(*this, Fn); +} + +iterator_range>> +BasicBlock::instructionsWithoutDebug() { + std::function Fn = [](Instruction &I) { + return !isa(I); + }; + return make_filter_range(*this, Fn); +} + void BasicBlock::removeFromParent() { getParent()->getBasicBlockList().remove(getIterator()); } Index: llvm/trunk/unittests/IR/BasicBlockTest.cpp =================================================================== --- llvm/trunk/unittests/IR/BasicBlockTest.cpp +++ llvm/trunk/unittests/IR/BasicBlockTest.cpp @@ -77,5 +77,44 @@ } } +#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 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 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(Builder1.CreateAdd(V, V)); + Instruction *MulInst = cast(Builder1.CreateMul(AddInst, V)); + Builder1.CreateCall(DbgDeclare, Args); + Instruction *SubInst = cast(Builder1.CreateSub(MulInst, V)); + Builder1.CreateCall(DbgAddr, Args); + + SmallVector Exp = {Var, AddInst, MulInst, SubInst}; + CHECK_ITERATORS(BB1->instructionsWithoutDebug(), Exp); + CHECK_ITERATORS(BBConst->instructionsWithoutDebug(), Exp); +} + } // End anonymous namespace. } // End llvm namespace.