add support for libcds atomic function calls
[c11llvm.git] / instrumentAtomicCall.hpp
index 0c96df82472e4182e7f6f6fa046d221de706c476..4ee6d8de0d765ff1e88ba6d51e321d6cf1e8e4f3 100644 (file)
@@ -1,6 +1,4 @@
-bool containsStr() {
-
-}
+// todo: come up with better rules for function name checking
 
 bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
        IRBuilder<> IRB(CI);
@@ -15,6 +13,9 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
                parameters.push_back(param);
        }
 
+       // obtain source line number of the CallInst
+       Value *position = getPosition(CI, IRB);
+
        // the pointer to the address is always the first argument
        Value *OrigPtr = parameters[0];
        int Idx = getMemoryAccessFuncIndex(OrigPtr, DL);
@@ -30,9 +31,9 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
        if (funName.contains("atomic_init")) {
                Value *ptr = IRB.CreatePointerCast(OrigPtr, PtrTy);
                Value *val = IRB.CreateBitOrPointerCast(parameters[1], Ty);
-               Value *args[] = {ptr, val};
+               Value *args[] = {ptr, val, position};
 
-               Instruction* funcInst=CallInst::Create(CDSAtomicInit[Idx], args,"");
+               Instruction* funcInst=CallInst::Create(CDSAtomicInit[Idx], args);
                ReplaceInstWithInst(CI, funcInst);
 
                return true;
@@ -49,11 +50,26 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
                else 
                        order = ConstantInt::get(OrdTy, 
                                                        (int) AtomicOrderingCABI::seq_cst);
-               Value *args[] = {ptr, order};
+               Value *args[] = {ptr, order, position};
                
-               Instruction* funcInst=CallInst::Create(CDSAtomicLoad[Idx], args,"");
+               Instruction* funcInst=CallInst::Create(CDSAtomicLoad[Idx], args);
                ReplaceInstWithInst(CI, funcInst);
 
+               return true;
+       } else if (funName.contains("atomic") && 
+                               funName.contains("load")) {
+               // does this version of call always have an atomic order as an argument?
+               Value *ptr = IRB.CreatePointerCast(OrigPtr, PtrTy);
+               Value *order = IRB.CreateBitOrPointerCast(parameters[1], OrdTy);
+               Value *args[] = {ptr, order, position};
+
+               //Instruction* funcInst=CallInst::Create(CDSAtomicLoad[Idx], args);
+               CallInst *funcInst = IRB.CreateCall(CDSAtomicLoad[Idx], args);
+               Value *RetVal = IRB.CreateIntToPtr(funcInst, CI->getType());
+
+               CI->replaceAllUsesWith(RetVal);
+               CI->eraseFromParent();
+               
                return true;
        }
 
@@ -70,11 +86,25 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
                else 
                        order = ConstantInt::get(OrdTy, 
                                                        (int) AtomicOrderingCABI::seq_cst);
-               Value *args[] = {ptr, val, order};
+               Value *args[] = {ptr, val, order, position};
                
-               Instruction* funcInst=CallInst::Create(CDSAtomicStore[Idx], args,"");
+               Instruction* funcInst = CallInst::Create(CDSAtomicStore[Idx], args);
                ReplaceInstWithInst(CI, funcInst);
 
+               return true;
+       } else if (funName.contains("atomic") && 
+                                       funName.contains("store")) {
+               // does this version of call always have an atomic order as an argument?
+               Value *OrigVal = parameters[1];
+
+               Value *ptr = IRB.CreatePointerCast(OrigPtr, PtrTy);
+               Value *val = IRB.CreatePointerCast(OrigVal, Ty);
+               Value *order = IRB.CreateBitOrPointerCast(parameters[1], OrdTy);
+               Value *args[] = {ptr, val, order, position};
+
+               Instruction* funcInst = CallInst::Create(CDSAtomicStore[Idx], args);
+               ReplaceInstWithInst(CI, funcInst);
+               
                return true;
        }
 
@@ -110,13 +140,22 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
                else 
                        order = ConstantInt::get(OrdTy, 
                                                        (int) AtomicOrderingCABI::seq_cst);
-               Value *args[] = {ptr, val, order};
+               Value *args[] = {ptr, val, order, position};
                
-               Instruction* funcInst=CallInst::Create(CDSAtomicRMW[op][Idx], args,"");
+               Instruction* funcInst=CallInst::Create(CDSAtomicRMW[op][Idx], args);
                ReplaceInstWithInst(CI, funcInst);
 
                return true;
-       }
+       } else if (funName.contains("fetch")) {
+               errs() << "atomic exchange captured. Not implemented yet. "
+               errs() << "See source file :";
+               getPositionPrint(CI, IRB);
+       } else if (funName.contains("exchange") &&
+                               !funName.contains("compare_exchange") ) {
+               errs() << "atomic exchange captured. Not implemented yet. "
+               errs() << "See source file :";
+               getPositionPrint(CI, IRB);
+       } 
 
        /* atomic_compare_exchange_*; 
           args = {obj, expected, new value, order1, order2}
@@ -140,9 +179,26 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
                }
 
                Value *args[] = {Addr, CmpOperand, NewOperand, 
-                                                                       order_succ, order_fail};
+                                                       order_succ, order_fail, position};
                
-               Instruction* funcInst=CallInst::Create(CDSAtomicCAS_V2[Idx], args,"");
+               Instruction* funcInst=CallInst::Create(CDSAtomicCAS_V2[Idx], args);
+               ReplaceInstWithInst(CI, funcInst);
+
+               return true;
+       } else if ( funName.contains("compare_exchange_strong") || 
+                               funName.contains("compare_exchange_wesk") ) {
+
+               Value *Addr = IRB.CreatePointerCast(OrigPtr, PtrTy);
+               Value *CmpOperand = IRB.CreatePointerCast(parameters[1], PtrTy);
+               Value *NewOperand = IRB.CreateBitOrPointerCast(parameters[2], Ty);
+
+               Value *order_succ, *order_fail;
+               order_succ = IRB.CreateBitOrPointerCast(parameters[3], OrdTy);
+               order_fail = IRB.CreateBitOrPointerCast(parameters[4], OrdTy);
+
+               Value *args[] = {Addr, CmpOperand, NewOperand, 
+                                                       order_succ, order_fail, position};
+               Instruction* funcInst=CallInst::Create(CDSAtomicCAS_V2[Idx], args);
                ReplaceInstWithInst(CI, funcInst);
 
                return true;