title: How to Insert a LLVM Instruction?
body: <p>I've been searching for hours and I can't find anything that could help me. I'm working on a project that involves a FunctionPass. I've implemented a runOnFunction(Function &amp;f) method and that's working fine. Basically it needs to:</p>
  
  <p>1) Detect a store instruction</p>
  
  <p>2) Convert the memory address of the store instruction to an Integer</p>
  
  <p>3) Alter the integer using a bitwise AND operation (0000FFFF)</p>
  
  <p>4) Convert the integer back into the pointer</p>
  
  <p>So far I've got the following:</p>
  
  <pre><code> virtual bool runOnFunction(Function &amp;F) {
    for (Function::iterator bb = F.begin(), bbe = F.end(); bb != bbe; ++bb) {
      BasicBlock&amp; b = *bb;
      for (BasicBlock::iterator i = b.begin(), ie = b.end(); i != ie; ++i) {
        if(StoreInst *si = dyn_cast&lt;StoreInst&gt;(&amp;*i)) {
          PtrToIntInst* ptrToInt = new PtrToIntInst(si-&gt;getPointerOperand(), IntegerType::get(si-&gt;getContext(), 32), "", si);
        }
      }
    }
    return true;
  }
  </code></pre>
  
  <p>I can't for the life of me figure out how to actually insert the instruction, or even find a way to create an AND instruction. If anyone could point me in the right direction, that would be great.</p>
  
  <p>Thanks in advance.</p>