lrmforever
lrmforever

Reputation: 41

how to get basic block ID of a statement inside visit* methods

How can we get basic block id (blockID) of a statement when we override visit* methods?

e.g. for a basic block given below, when VisitIfStmt() is visited, how to get blockID inside this visit method?
[B4]
1: x == 0
T: if [B4.1]
Preds (1): B6
Succs (2): B3 B2

Upvotes: 1

Views: 515

Answers (2)

neuroo
neuroo

Reputation: 51

Old question, but I had to answer the same lately. You can use the CFGStmtMap to query for the BBL of a statement:

const FunctionDecl* FD = ...;
const CFG* cfg = ...;
std::unique_ptr<ParentMap> PM = llvm::make_unique<ParentMap>(FD->getBody());
std::unique_ptr<CFGStmtMap> CM = llvm::make_unique<CFGStmtMap>(cfg, PM.get());
// do your traversal and for a given Stmt `stmt` you can get
// its containing block:
CFGBlock* stmt_block = CM->getBlock(stmt);
const unsigned int block_id = stmt_block->getBlockID();

Upvotes: 1

Alex
Alex

Reputation: 360

You can try to use llvm::PHINode::getBasicBlockIndex ( const BasicBlock *BB).

Upvotes: 0

Related Questions