[BasicBlock] Add instructionsWithoutDebug methods to skip debug insts.

Reviewers: aprantl, vsk, mattd, chandlerc

Reviewed By: aprantl, vsk

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

llvm-svn: 330316
This commit is contained in:
Florian Hahn 2018-04-19 09:48:07 +00:00
parent 18caced54a
commit a58926d51c
3 changed files with 69 additions and 0 deletions

View File

@ -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();

View File

@ -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());
}

View File

@ -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.