如何检查迭代器的相等性?

时间:2012-02-24 13:35:12

标签: c++ llvm intermediate-code

在我的llvm代码中,我尝试使用以下代码检查迭代器 InsertPos 是否指向基本块的最后一条指令。

  BasicBlock::iterator InsertPos = BB->begin();
  LLVMContext &Context = BB->getContext();

  while ( !( isa<CallInst>(InsertPos) || 
      ( InsertPos == BB->getTerminator() ) ) ) // <-- Error here
    ++InsertPos;

但是,clang给出了以下错误。

Basic2.cpp:82:54: error: use of overloaded operator '==' is ambiguous (with operand types 'BasicBlock::iterator' (aka 'ilist_iterator<llvm::Instruction>')
      and 'llvm::TerminatorInst *')
  while ( !( isa<CallInst>(InsertPos) || ( InsertPos == BB->getTerminator() ) ) ) 
                                           ~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~
/home/hmushtaq/llvm/include/llvm/ADT/ilist.h:204:8: note: candidate function
  bool operator==(const ilist_iterator &RHS) const {
       ^
Basic2.cpp:82:54: note: built-in candidate operator==(class llvm::Instruction *, class llvm::Instruction *)
  while ( !( isa<CallInst>(InsertPos) || ( InsertPos == BB->getTerminator() ) ) ) 
                                                     ^
    Basic2.cpp:82:54: note: built-in candidate operator==(restrict class llvm::Instruction    *, restrict class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const restrict class llvm::Instruction *, const restrict class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(volatile class llvm::Instruction *, volatile class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const volatile class llvm::Instruction *, const volatile class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(volatile restrict class llvm::Instruction *, volatile restrict class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const class llvm::Instruction *, const class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const volatile restrict class llvm::Instruction *, const volatile restrict class llvm::Instruction *)

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

BB->getTerminator()返回指针(TerminatorInst *),而InsertPos是迭代器(BasicBlock::iterator)。类型不匹配。

也许你可以试试

&*InsertPos == BB->getTerminator()

您还可以检查教学是否是the isTerminator() method的终结者。

InsertPos->isTerminator()

答案 1 :(得分:1)

我这样解决了。

BasicBlock::iterator InsertPos = BB->begin();
BasicBlock::iterator TermPos = BB->getTerminator(); // <-- Added this variable

while ( !( isa<CallInst>(InsertPos) || ( InsertPos == TermPos ) ) ) 
  ++InsertPos;