Hello all;<div><br></div><div>So my goal is to insert some (self-written) function calls in the LLVM IR. I can achieve it with a ModulePass and using the getOrInsertFunction() call.  For a ModulePass I follow the steps-</div>
<div>1.  Compile the code (in which call instructions are to be inserted) to LLVM IR</div><div>2.  Compile the file (which contains the *called* external function ) to LLVM IR</div><div>3.  Link them together and run the transformation pass to insert function calls.</div>
<div><br></div><div>But I have to use LoopInfo pass in my transformation pass as well and for that I have to have a FunctionPass (LoopInfo fails in a ModulePass).  So I am trying to follow the above steps for a FunctionPass class (with relevant modifications, as I can't use getOrInsertFunction() now (or can I)). And when I run my optimization pass now (step 3) to instrument, I am getting - </div>
<div><br></div><div><div>Referencing function in another module!</div><div>  %1 = call i32 @print(i32 %0)</div><div>Broken module found, compilation aborted!</div><div><br></div><div>My Code:</div><div><div><br></div><div>
#include "llvm/Pass.h"</div><div>#include "llvm/Module.h"</div><div>#include "llvm/Function.h"</div><div>#include "llvm/Support/raw_ostream.h"</div><div>#include "llvm/Type.h"</div>
<div>#include "llvm/Instructions.h"</div><div>#include "llvm/Instruction.h"</div><div>#include "llvm/ADT/StringRef.h"</div><div>#include "llvm/IRBuilder.h"</div><div><br></div><div>
using namespace llvm;</div><div><br></div><div>Module * M;</div><div>LLVMContext Context;</div><div>Twine * name = new Twine("print");</div><div>FunctionType *STy=FunctionType::get(Type::getInt32Ty(Context),Type::getInt32Ty(Context), false);</div>
<div>Function *check = Function::Create(STy, Function::ExternalLinkage, *name ,M);</div><div>AllocaInst* count; <span class="Apple-tab-span" style="white-space:pre">       </span></div><div>namespace{</div><div>    struct bishe_insert : public FunctionPass{</div>
<div>        static char ID;   </div><div>        bishe_insert() : FunctionPass(ID) {}</div><div><br></div><div>        virtual bool runOnFunction(Function &func)</div><div>        {</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>count = new AllocaInst(IntegerType::getInt32Ty(Context), 0, "count");</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  <span class="Apple-tab-span" style="white-space:pre">  </span>count->setAlignment(4);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">  </span>        for(Function::iterator F = func.begin(), E = func.end(); F!= E; ++F)</div>
<div>        <span class="Apple-tab-span" style="white-space:pre">      </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>    if(F == func.begin())</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>    {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>bishe_insert::insertOnFirstBlock(F);</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>    }</div><div>                    bishe_insert::runOnBasicBlock(F);</div>
<div>                }</div><div><br></div><div>            return false;</div><div>        }</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>/*insert alloca instruction in the start of the first basic block*/</div>
<div>        virtual bool insertOnFirstBlock(Function::iterator &BB)</div><div>        {</div><div>            for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI)           </div><div>            {</div>
<div>                    if(BI == BB->begin())</div><div>                    {</div><div><span class="Apple-tab-span" style="white-space:pre">                       </span>BB->getInstList().insert((Instruction*)BI, count);</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>break;</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>    }//end of if</div><div>                   </div><div>            }</div><div>            return true;</div><div>        }</div><div>        virtual bool runOnBasicBlock(Function::iterator &BB)</div>
<div>        {</div><div>            for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI)           </div><div>            {</div><div>                    if(isa<LoadInst>(&(*BI)))</div>
<div>                    {</div><div><span class="Apple-tab-span" style="white-space:pre">                          </span>LoadInst *CI = dyn_cast<LoadInst>(BI);</div><div><span class="Apple-tab-span" style="white-space:pre">                         </span>/*load count*/</div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>LoadInst* load_count = new LoadInst(count, "", false);</div><div><span class="Apple-tab-span" style="white-space:pre">                             </span>load_count->setAlignment(4);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>/*call print(count)*/</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>CallInst* call_print = CallInst::Create(check, load_count, "");</div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>/*insert the instructions*/</div><div><span class="Apple-tab-span" style="white-space:pre">                          </span>BB->getInstList().insert((Instruction*)CI, load_count);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>BB->getInstList().insert((Instruction*)CI, call_print);</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>     }//end of if</div>
<div>                   </div><div>            }</div><div>            return true;</div><div>        }</div><div>    };</div><div>}</div><div>char bishe_insert::ID = 0;</div><div>static RegisterPass<bishe_insert> X("bishe_insert", "insert print calls");</div>
</div><div><br></div><div>Thanks a lot;</div>-- <br>Arnamoy Bhattacharyya<br>Athabasca Hall 143<br>Department of Computing Science - University of Alberta<br>Edmonton, Alberta, Canada, T6G 2E8<br>587-710-7073<br>
</div>