Reputation: 301
I have a filtered set of instructions with in a llvm pass. I want to check if each instruction is a part of any loop ( or sub loops). I know that I can get the parent basic block from an instruction
BasicBlock* bb = aInst->getParent();
But how to check if the instruction / the basic block is part of a loop or not ?
Upvotes: 0
Views: 2041
Reputation: 1671
You can use LoopInfo to find loops in an llvm::Function
.
As explained here, you can get the function a given instruction belongs to, compute a dominator tree for that function, and analyze the result with an llvm::LoopInfo::BaseT
object.
llvm::DominatorTree DT = llvm::DominatorTree();
DT.recalculate(instr->getFunction());
auto loopInfo = new llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>();
loopInfo->releaseMemory();
loopInfo->analyze(DT);
Then you check if instr
's surrounding function contains a loop that contains instr
by passing instr
and loopInfo
to a routine like
bool instrIsInaLoop(llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop> &loopInfo,
llvm::Instruction *instr) {
for (auto loop : loopInfo) if (instrIsInLoop(loop, instr)) return true;
return false;
}
with subroutine
bool instrIsInLoop(llvm::Loop *loop, llvm::Instruction *instr) {
if (loop->contains(instr))
return true;
else {
for (auto subLoop : loop->getSubLoops()) {
if (instrIsInLoop(loop, instr)) return true;
}
return false;
}
}
Upvotes: 2