2017-04-08 51 views
1

我想從一個IR文件中獲得所有前驅基本塊,而不是在傳遞過程中。我可以用如何將'llvm :: BasicBlock'轉換爲'pred_iterator'?

for (auto iter1 = m->getFunctionList().begin(); 
    iter1 != m->getFunctionList().end(); 
    iter1++) 
{ 
    for (auto iter2 = f.getBasicBlockList().begin(); 
     iter2 != f.getBasicBlockList().end(); 
     iter2++) 
    { 
    BasicBlock &bb = *iter2; 

    std::cout << " BasicBlock: " << bb.getName().str() << std::endl; 
    } 
} 

然而在所有基本塊迭代,當我從this linkfor (BasicBlock *Pred : pred_iterator(bb)) {},我得到

for (auto iter1 = m->getFunctionList().begin(); 
    iter1 != m->getFunctionList().end(); 
    iter1++) 
{ 
    for (auto iter2 = f.getBasicBlockList().begin(); 
     iter2 != f.getBasicBlockList().end(); 
     iter2++) 
    { 
    BasicBlock &bb = *iter2; 

    std::cout << " BasicBlock: " << bb.getName().str() << std::endl; 
    for (BasicBlock *Pred : pred_iterator(bb)) {} 
    } 
} 

的錯誤顯示如下

test2.cpp:63:37: error: no matching conversion for functional-style cast from 'llvm::BasicBlock' to 'pred_iterator' (aka 'PredIterator<llvm::BasicBlock, user_iterator_impl<llvm::User> >') 
     for (BasicBlock *Pred : pred_iterator(bb)) { 
           ^~~~~~~~~~~~~~~~ 

這裏是一個錯誤一些附加信息:

/usr/local/include/llvm/IR/CFG.h:48:19: note: candidate constructor not viable: no known conversion from 'llvm::BasicBlock' to 'llvm::BasicBlock *' for 1st argument; take the address of the 
     argument with & 
    explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) { 
       ^
Additional Informatino is below. 
/usr/local/include/llvm/IR/CFG.h:30:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'llvm::BasicBlock' to 'const 
     llvm::PredIterator<llvm::BasicBlock, llvm::Value::user_iterator_impl<llvm::User> >' for 1st argument 
class PredIterator : public std::iterator<std::forward_iterator_tag, 
    ^
/usr/local/include/llvm/IR/CFG.h:30:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'llvm::BasicBlock' to 
     'llvm::PredIterator<llvm::BasicBlock, llvm::Value::user_iterator_impl<llvm::User> >' for 1st argument 
/usr/local/include/llvm/IR/CFG.h:47:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided 
    PredIterator() {} 
^
/usr/local/include/llvm/IR/CFG.h:51:10: note: candidate constructor not viable: requires 2 arguments, but 1 was provided 
    inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {} 

如何從'llvm :: BasicBlock'轉換爲'pred_iterator'?

回答

1

如您發佈的錯誤消息所示,pred_iterator構造函數需要BasicBlock*,而不是BasicBlock&

你的例子的另一個問題是,爲了迭代一個基於範圍的循環,你需要一個範圍,而不是迭代器。你應該使用一個稱爲llvm::predecessors的幫手。

修正這些錯誤,我們得出以下幾點:

for (BasicBlock *Pred : predecessors(&bb)) {}