InstList insert depreciated?

Hi llvm-devel,

I have migrated my codebase from llvm-3.6 to llvm 3.8.1-stable.
Although I was able to resolve most of the problems, I am facing
issues resolving the following:

To insert an instruction immediately after the first instruction
within a basic block, I first get all instructions in my basic block
in an instruction container list. Once that is done, I insert my new
instruction in the instruction container list using InstList.insert().

// code

void FSliceModulePass::allocaVSetArray(void) {
  auto &B = F->getEntryBlock();
  auto &IList = B.getInstList();
  auto &FirstI = *IList.begin();
  auto TaintVar = new AllocaInst(IntPtrTy, FirstI);
     IList.insert(FirstI, TaintVar); // ERROR

After migrating to 3.8.1, It gives me the following error:

ERROR:

/home/shehbaz/project/plugin/FSlice.cpp: In member function ‘void
FSliceModulePass::allocaVSetArray()’:
/home/shehbaz/project/plugin/FSlice.cpp:284:34: error: no matching
function for call to
‘llvm::SymbolTableList<llvm::Instruction>::insert(llvm::I
nstruction&, llvm::AllocaInst*&)’
     IList.insert(FirstI, TaintVar);
                                  ^
In file included from
/home/shehbaz/project/llvm/include/llvm/IR/BasicBlock.h:18:0,
                 from /home/shehbaz/project/llvm/include/llvm/IR/Function.h:25,
                 from /home/shehbaz/project/plugin/FSlice.cpp:7:
/home/shehbaz/project/llvm/include/llvm/ADT/ilist.h:461:12: note:
candidate: llvm::iplist<NodeTy, Traits>::iterator llvm::iplist<NodeTy,

:

:insert(llvm::iplist<NodeTy, Traits>::iterator, NodeTy*) [with NodeTy
= llvm::Instruction; Traits =
llvm::SymbolTableListTraits<llvm::Instruction

; llvm::iplist<NodeTy, Traits>::iterator = llvm::ilist_iterator<llvm::Instruction>]

   iterator insert(iterator where, NodeTy *New) {
            ^
/home/shehbaz/project/llvm/include/llvm/ADT/ilist.h:461:12: note: no
known conversion for argument 1 from ‘llvm::Instruction’ to
‘llvm::iplist<
llvm::Instruction, llvm::SymbolTableListTraits<llvm::Instruction>

::iterator {aka llvm::ilist_iterator<llvm::Instruction>}’

/home/shehbaz/project/llvm/include/llvm/ADT/ilist.h:620:29: note:
candidate: template<class InIt> void llvm::iplist<NodeTy,

::insert(llvm:

:iplist<NodeTy, Traits>::iterator, InIt, InIt) [with InIt = InIt;
NodeTy = llvm::Instruction; Traits =
llvm::SymbolTableListTraits<llvm::Instruct

]

   template<class InIt> void insert(iterator where, InIt first, InIt last) {
                             ^
/home/shehbaz/project/llvm/include/llvm/ADT/ilist.h:620:29: note:
template argument deduction/substitution failed:
/home/shehbaz/project/plugin/FSlice.cpp:284:34: note: candidate
expects 3 arguments, 2 provided
     IList.insert(FirstI, TaintVar);

I tried an alternative way of adding instruction by first getting the
first instruction of the basic block, and then calling insertAfter()
on it as follows:

  auto &B = F->getEntryBlock();
  auto &IList = B.getInstList();
  auto &FirstI = *IList.begin();
    auto TaintVar = new AllocaInst(IntPtrTy);
    // IList.insert(FirstI, TaintVar); // OLD
     FirstI.insertAfter(TaintVar); // NEW

This compiles, however, while running the code, I get the following Stack Dump:

#0 0x0000000001466cb8 llvm::sys::PrintStackTrace(llvm::raw_ostream&)
(/home/shehbaz/project/llvm/build/bin/opt+0x1466cb8)
#1 0x0000000001467337 SignalHandler(int)
(/home/shehbaz/project/llvm/build/bin/opt+0x1467337)
#2 0x00007f8e846fa3d0 __restore_rt
(/lib/x86_64-linux-gnu/libpthread.so.0+0x113d0)
#3 0x0000000001107c2b
llvm::Instruction::insertAfter(llvm::Instruction*)
(/home/shehbaz/project/llvm/build/bin/opt+0x1107c2b)
#4 0x00007f8e83642f20 FSliceModulePass::allocaVSetArray()
/home/shehbaz/project/plugin/FSlice.cpp:287:0
#5 0x00007f8e83642666 FSliceModulePass::runOnFunction(char const*)
/home/shehbaz/project/plugin/FSlice.cpp:172:0
#6 0x00007f8e836425dd FSliceModulePass::runOnModule(llvm::Module&)
/home/shehbaz/project/plugin/FSlice.cpp:139:0
#7 0x000000000112852c
llvm::legacy::PassManagerImpl::run(llvm::Module&)
(/home/shehbaz/project/llvm/build/bin/opt+0x112852c)
#8 0x000000000064cf88 main (/home/shehbaz/project/llvm/build/bin/opt+0x64cf88)
#9 0x00007f8e83885830 __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x20830)
#10 0x0000000000641809 _start
(/home/shehbaz/project/llvm/build/bin/opt+0x641809)

Any suggestions or pointers to how I can go about debugging this issue
will be of great help.

Thanks in Advance,

You want:

    TaintVar->insertAfter(FirstI);

Jon

Jon,

You want:
   TaintVar->insertAfter(FirstI);

This worked! Thank you.

More specifically,

- FirstI->insertAfter(TaintVar);
+ auto t = dyn_cast<Instruction>(TaintVar);
+ t->insertBefore(&FirstI);

worked for me.

since TaintVar is of type AllocaInst, it does not have an insertAfter
insertBefore defined, so I had to dynamically cast it to Instruction.

Also, making t->insertAfter(&FirstI) causes some errors of type
"llvm-instruction-does-not-dominate-all-uses", since I want alloca to
appear before FirstI, I had to make insertAfter => insertBefore.

More specifically,

- FirstI->insertAfter(TaintVar);
+ auto t = dyn_cast<Instruction>(TaintVar);
+ t->insertBefore(&FirstI);

worked for me.

since TaintVar is of type AllocaInst, it does not have an insertAfter
insertBefore defined, so I had to dynamically cast it to Instruction.

Instruction is AllocaInst's public base class... you shouldn't need to dyn_cast<> here.

Also, dyn_cast<> followed by dereferencing its result without checking for null is an anti-pattern. You should either use cast<>, or add a check for it.

Also, making t->insertAfter(&FirstI) causes some errors of type
"llvm-instruction-does-not-dominate-all-uses", since I want alloca to
appear before FirstI, I had to make insertAfter => insertBefore.

Yeah, sticking alloca's at the top of the block is a good idea. Even better if it's the entry block.

Jon