簡體   English   中英

如何查找LLVM IR級別中兩個特定基本塊之間出現的所有基本塊?

[英]How to find all basic blocks appearing between two specific basic blocks in LLVM IR level?

我在LLVM做了一些分析,由於某些原因,我想找到可以在兩個特定基本塊之間執行的所有基本塊。

例如,假設我們有兩個名為AB基本塊。 現在我想知道在執行基本塊A和基本塊B之前可以出現哪些基本塊。

一種可能的解決方案是使用控制輝光圖的可達性分析。 例如,如果基本塊C可以從A到達並且基本塊B可以從C到達,那么我們可以說C可以在A之后和B之前執行。

我在LLVM中唯一能找到的是llvm/analysis/CFG.h這個函數:

/// Determine whether instruction 'To' is reachable from 'From', without passing
/// through any blocks in ExclusionSet, returning true if uncertain.
///
/// Determine whether there is a path from From to To within a single function.
/// Returns false only if we can prove that once 'From' has been executed then
/// 'To' can not be executed. Conservatively returns true.
///
/// This function is linear with respect to the number of blocks in the CFG,
/// walking down successors from From to reach To, with a fixed threshold.
/// Using DT or LI allows us to answer more quickly. LI reduces the cost of
/// an entire loop of any number of blocks to be the same as the cost of a
/// single block. DT reduces the cost by allowing the search to terminate when
/// we find a block that dominates the block containing 'To'. DT is most useful
/// on branchy code but not loops, and LI is most useful on code with loops but
/// does not help on branchy code outside loops.
bool isPotentiallyReachable(
     const Instruction *From, const Instruction *To,
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
     const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);

但問題是這個功能是如此保守,答案並不確定。 我想知道某些答案。

你正在尋找的概念被稱為統治。 你想要由A支配並由B支配的塊。為此,你制作或獲得DominatorTreePostDominatorTree ,並查看A和B下面的樹的部分。你可以從傳遞管理器中獲得一個,如果你正在寫一個通行證。

但請注意,LLVM的代碼是保守的,因為這種事情很快就會變得復雜。 那么在A之后執行的塊(即由A支配)但是返回或拋出異常而不是分支到B? 你想要那些嗎? 如果有無限循環怎么辦? 那些可能是故意的。 如果拋出異常並且處理程序沒有被B進行后置處理怎么辦? 這種事情充滿了你必須考慮的案例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM