<div dir="ltr">Hello,<div><br></div><div>I'm trying to use an LLVM Pass to trace calls to memcpy via instrumentation, but finding it quite difficult without having much knowledge of sound IR structure. Here's my FunctionPass thus far:</div><div><br></div><div>// at the beginning of runOnFunction<br>FunctionCallee hookFunc = M->getOrInsertFunction("_Z12trace_memcpyPvS_m", Type::getVoidTy(M->getContext()));<br><br>// further in the same function<br>for (BasicBlock &BB : F) {<br>  for (Instruction &I : BB) {<br>    if (isa<CallInst>(&I)) {<br>      CallInst *CI = dyn_cast<CallInst>(&I);<br>      Function* FC = CI->getCalledFunction();<br>      StringRef SR = FC->getName();<br>      if (SR == "llvm.memcpy.p0i8.p0i8.i64") {<br>        CallInst* newInstr = dyn_cast<CallInst>(CI->clone());<br>        newInstr->setCalledFunction(hookFunc);<br>        newInstr->insertBefore(CI);<br>      }<br>    }<br>  }<br>}<br></div><div><br></div><div>The issue arises when using `setCalledFunction`. If I remove that, it runs successfully and I end up with duplicate calls to memcpy - as expected.</div><div><br></div><div>But when I leave it in, and run the pass with `opt`, I get the following error:</div><div><br></div><div>LLVM ERROR: Broken function found, compilation aborted!<br></div><div><br></div><div>Looking for any clues on how to fix... and would also appreciate  pointers in general about how I might learn the general concepts at play here.</div><div><br></div><div>Thanks all,.<br></div></div>