Quentin Mayo
Quentin Mayo

Reputation: 410

How do I convert 'llvm::BasicBlock' to 'pred_iterator'?

I am trying to get all the predecessor basic blocks from an IR file not during a pass. I can iterate over all the basic blocks using

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;
  }
}

However, when I add for (BasicBlock *Pred : pred_iterator(bb)) {} from this link, I get an error

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)) {}
  }
}

The errors are shown below

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)) {
                                ^~~~~~~~~~~~~~~~

Here is some additional Information:

/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()) {}

How do I convert from 'llvm::BasicBlock' to a 'pred_iterator'?

Upvotes: 1

Views: 1061

Answers (1)

eush77
eush77

Reputation: 4088

As shown in the error message you posted, pred_iterator constructor takes BasicBlock*, not BasicBlock&.

The other problem with your example is that in order to iterate with a range-based loop, you need a range, not an iterator. You should use a helper called llvm::predecessors for that.

Fixing these errors, we arrive at the following:

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

Upvotes: 2

Related Questions