writing llvm pass to instrument read and write

Hello All,

I am trying to write llvm pass to instrument function in external library. I am using clang to automatically load my pass. I have test.cpp which calls functions in library testlib.so. I have written llvm pass to instrument all reads and writes in test.cpp and call function(recordRW) in testlib.so.
this is how my pass looks like -

for (auto &BB : F) {
for (auto &I : BB) {
// Is this a load or store? Get the address.
Value *Ptr = nullptr;
Value *op_l = nullptr;
Value *op_s = nullptr;
if (auto *LI = dyn_cast(&I)) {
Ptr = LI->getPointerOperand();

Constant* read = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
instrument_access(Ptr, &(*LI), read, M);
modified = true;
} else if (auto *SI = dyn_cast(&I)) {
Ptr = SI->getPointerOperand();

Constant* write = ConstantInt::get(Type::getInt32Ty(M.getContext()), 1);
instrument_access(Ptr, &(*SI),write, M);
modified = true;



void instrument_access(Value* op, Instruction* inst, Constant* rdWr, Module& module) {
SmallVector<Value*, 8> args;
Instruction* get_tid = CallInst::Create(getThreadId, args, “”, inst);
BitCastInst* bitcast = new BitCastInst(op,
“”, inst);


Instruction* recordRWCall = CallInst::Create(recordRW, args, “”, inst);

I have run my pass with opt and I could see that recordRWCall has been created but this function is not called when I run test.cpp. How can I integrate all this together? What I want to do is when I run test.cpp, and if there is any read or write instruction, recordRW defined in testlib.so should be called. I have read a lot on Internet but I could find a way to do something like this. Any help would be much appreciated.