2

In my llvm code, I try to check if the iterator InsertPos is pointing to the last instruction of a basic block, with the following code.

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

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

However, clang gives me the following error.

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 *)

What is the right way of doing this?

Lundin
  • 195,001
  • 40
  • 254
  • 396
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

2 Answers2

3

BB->getTerminator() returns a pointer (TerminatorInst *), while InsertPos is an iterator (BasicBlock::iterator). The types do no match.

Maybe you could try

&*InsertPos == BB->getTerminator()

You could also check if an Instruction is a terminator with the isTerminator() method.

InsertPos->isTerminator()
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

I solved it like this.

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

while ( !( isa<CallInst>(InsertPos) || ( InsertPos == TermPos ) ) ) 
  ++InsertPos;
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356