diff options
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/IR/BasicBlock.h | 12 | ||||
| -rw-r--r-- | llvm/lib/IR/BasicBlock.cpp | 18 | ||||
| -rw-r--r-- | llvm/unittests/IR/BasicBlockTest.cpp | 39 |
3 files changed, 69 insertions, 0 deletions
diff --git a/llvm/include/llvm/IR/BasicBlock.h b/llvm/include/llvm/IR/BasicBlock.h index 29bbefb7cbf..3dee6b451c1 100644 --- a/llvm/include/llvm/IR/BasicBlock.h +++ b/llvm/include/llvm/IR/BasicBlock.h @@ -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(); diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp index 938c40182b9..c103de4fa34 100644 --- a/llvm/lib/IR/BasicBlock.cpp +++ b/llvm/lib/IR/BasicBlock.cpp @@ -90,6 +90,24 @@ void BasicBlock::setParent(Function *parent) { 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()); } diff --git a/llvm/unittests/IR/BasicBlockTest.cpp b/llvm/unittests/IR/BasicBlockTest.cpp index 08a41ff3693..cf950501aec 100644 --- a/llvm/unittests/IR/BasicBlockTest.cpp +++ b/llvm/unittests/IR/BasicBlockTest.cpp @@ -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. |

