Merge /home/git/c11llvm
[c11llvm.git] / CDSPass.cpp
index c0bd2446930365fe8a40d0ca8d35489876e0967f..108d4deadd54bceb020085519ea88db0f9ed4a2f 100644 (file)
@@ -115,10 +115,12 @@ namespace {
                static char ID;
                CDSPass() : FunctionPass(ID) {}
                bool runOnFunction(Function &F) override; 
+               StringRef getPassName() const override;
 
        private:
                void initializeCallbacks(Module &M);
                bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
+               bool instrumentVolatile(Instruction *I, const DataLayout &DL);
                bool isAtomicCall(Instruction *I);
                bool instrumentAtomic(Instruction *I, const DataLayout &DL);
                bool instrumentAtomicCall(CallInst *CI, const DataLayout &DL);
@@ -134,6 +136,8 @@ namespace {
 
                Constant * CDSLoad[kNumberOfAccessSizes];
                Constant * CDSStore[kNumberOfAccessSizes];
+               Constant * CDSVolatileLoad[kNumberOfAccessSizes];
+               Constant * CDSVolatileStore[kNumberOfAccessSizes];
                Constant * CDSAtomicInit[kNumberOfAccessSizes];
                Constant * CDSAtomicLoad[kNumberOfAccessSizes];
                Constant * CDSAtomicStore[kNumberOfAccessSizes];
@@ -147,6 +151,10 @@ namespace {
        };
 }
 
+StringRef CDSPass::getPassName() const {
+       return "CDSPass";
+}
+
 static bool isVtableAccess(Instruction *I) {
        if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
                return Tag->isTBAAVtableAccess();
@@ -186,12 +194,18 @@ void CDSPass::initializeCallbacks(Module &M) {
                // void cds_atomic_store8 (void * obj, int atomic_index, uint8_t val)
                SmallString<32> LoadName("cds_load" + BitSizeStr);
                SmallString<32> StoreName("cds_store" + BitSizeStr);
+               SmallString<32> VolatileLoadName("cds_volatile_load" + BitSizeStr);
+               SmallString<32> VolatileStoreName("cds_volatile_store" + BitSizeStr);
                SmallString<32> AtomicInitName("cds_atomic_init" + BitSizeStr);
                SmallString<32> AtomicLoadName("cds_atomic_load" + BitSizeStr);
                SmallString<32> AtomicStoreName("cds_atomic_store" + BitSizeStr);
 
                CDSLoad[i]  = M.getOrInsertFunction(LoadName, VoidTy, PtrTy);
                CDSStore[i] = M.getOrInsertFunction(StoreName, VoidTy, PtrTy);
+               CDSVolatileLoad[i]  = M.getOrInsertFunction(VolatileLoadName,
+                                                                       Ty, PtrTy, Int8PtrTy);
+               CDSVolatileStore[i] = M.getOrInsertFunction(VolatileStoreName, 
+                                                                       VoidTy, PtrTy, Ty, Int8PtrTy);
                CDSAtomicInit[i] = M.getOrInsertFunction(AtomicInitName, 
                                                                VoidTy, PtrTy, Ty, Int8PtrTy);
                CDSAtomicLoad[i]  = M.getOrInsertFunction(AtomicLoadName, 
@@ -311,12 +325,14 @@ bool CDSPass::runOnFunction(Function &F) {
 
                SmallVector<Instruction*, 8> AllLoadsAndStores;
                SmallVector<Instruction*, 8> LocalLoadsAndStores;
+               SmallVector<Instruction*, 8> VolatileLoadsAndStores;
                SmallVector<Instruction*, 8> AtomicAccesses;
 
                std::vector<Instruction *> worklist;
 
                bool Res = false;
                bool HasAtomic = false;
+               bool HasVolatile = false;
                const DataLayout &DL = F.getParent()->getDataLayout();
 
                // errs() << "--- " << F.getName() << "---\n";
@@ -327,7 +343,15 @@ bool CDSPass::runOnFunction(Function &F) {
                                        AtomicAccesses.push_back(&I);
                                        HasAtomic = true;
                                } else if (isa<LoadInst>(I) || isa<StoreInst>(I)) {
-                                       LocalLoadsAndStores.push_back(&I);
+                                       LoadInst *LI = dyn_cast<LoadInst>(&I);
+                                       StoreInst *SI = dyn_cast<StoreInst>(&I);
+                                       bool isVolatile = ( LI ? LI->isVolatile() : SI->isVolatile() );
+
+                                       if (isVolatile) {
+                                               VolatileLoadsAndStores.push_back(&I);
+                                               HasVolatile = true;
+                                       } else
+                                               LocalLoadsAndStores.push_back(&I);
                                } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
                                        // not implemented yet
                                }
@@ -340,12 +364,16 @@ bool CDSPass::runOnFunction(Function &F) {
                        Res |= instrumentLoadOrStore(Inst, DL);
                }
 
+               for (auto Inst : VolatileLoadsAndStores) {
+                       Res |= instrumentVolatile(Inst, DL);
+               }
+
                for (auto Inst : AtomicAccesses) {
                        Res |= instrumentAtomic(Inst, DL);
                }
 
                // only instrument functions that contain atomics
-               if (Res && HasAtomic) {
+               if (Res && ( HasAtomic || HasVolatile) ) {
                        IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
                        /* Unused for now
                        Value *ReturnAddress = IRB.CreateCall(
@@ -473,10 +501,40 @@ bool CDSPass::instrumentLoadOrStore(Instruction *I,
        return true;
 }
 
-bool CDSPass::instrumentAtomic(Instruction * I, const DataLayout &DL) {
+bool CDSPass::instrumentVolatile(Instruction * I, const DataLayout &DL) {
        IRBuilder<> IRB(I);
+       Value *position = getPosition(I, IRB);
+
+       if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
+               assert( LI->isVolatile() );
+               Value *Addr = LI->getPointerOperand();
+               int Idx=getMemoryAccessFuncIndex(Addr, DL);
+               if (Idx < 0)
+                       return false;
 
-       // errs() << "instrumenting: " << *I << "\n";
+               Value *args[] = {Addr, position};
+               Instruction* funcInst=CallInst::Create(CDSVolatileLoad[Idx], args);
+               ReplaceInstWithInst(LI, funcInst);
+       } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
+               assert( SI->isVolatile() );
+               Value *Addr = SI->getPointerOperand();
+               int Idx=getMemoryAccessFuncIndex(Addr, DL);
+               if (Idx < 0)
+                       return false;
+
+               Value *val = SI->getValueOperand();
+               Value *args[] = {Addr, val, position};
+               Instruction* funcInst=CallInst::Create(CDSVolatileStore[Idx], args);
+               ReplaceInstWithInst(SI, funcInst);
+       } else {
+               return false;
+       }
+
+       return true;
+}
+
+bool CDSPass::instrumentAtomic(Instruction * I, const DataLayout &DL) {
+       IRBuilder<> IRB(I);
 
        if (auto *CI = dyn_cast<CallInst>(I)) {
                return instrumentAtomicCall(CI, DL);
@@ -626,8 +684,15 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
 
        // atomic_init; args = {obj, order}
        if (funName.contains("atomic_init")) {
+               Value *OrigVal = parameters[1];
+
                Value *ptr = IRB.CreatePointerCast(OrigPtr, PtrTy);
-               Value *val = IRB.CreateBitOrPointerCast(parameters[1], Ty);
+               Value *val;
+               if (OrigVal->getType()->isPtrOrPtrVectorTy())
+                       val = IRB.CreatePointerCast(OrigVal, Ty);
+               else
+                       val = IRB.CreateIntCast(OrigVal, Ty, true);
+
                Value *args[] = {ptr, val, position};
 
                Instruction* funcInst = CallInst::Create(CDSAtomicInit[Idx], args);
@@ -693,12 +758,17 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
 
                return true;
        } else if (funName.contains("atomic") && 
-                                       funName.contains("EEEE5store") ) {
+                                       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 *val;
+               if (OrigVal->getType()->isPtrOrPtrVectorTy())
+                       val = IRB.CreatePointerCast(OrigVal, Ty);
+               else
+                       val = IRB.CreateIntCast(OrigVal, Ty, true);
+
                Value *order = IRB.CreateBitOrPointerCast(parameters[2], OrdTy);
                Value *args[] = {ptr, val, order, position};
 
@@ -710,7 +780,12 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
 
        // atomic_fetch_*; args = {obj, val, order}
        if (funName.contains("atomic_fetch_") || 
-                       funName.contains("atomic_exchange") ) {
+               funName.contains("atomic_exchange")) {
+
+               /* TODO: implement stricter function name checking */
+               if (funName.contains("non"))
+                       return false;
+
                bool isExplicit = funName.contains("_explicit");
                Value *OrigVal = parameters[1];
 
@@ -733,7 +808,12 @@ bool CDSPass::instrumentAtomicCall(CallInst *CI, const DataLayout &DL) {
                }
 
                Value *ptr = IRB.CreatePointerCast(OrigPtr, PtrTy);
-               Value *val = IRB.CreatePointerCast(OrigVal, Ty);
+               Value *val;
+               if (OrigVal->getType()->isPtrOrPtrVectorTy())
+                       val = IRB.CreatePointerCast(OrigVal, Ty);
+               else
+                       val = IRB.CreateIntCast(OrigVal, Ty, true);
+
                Value *order;
                if (isExplicit)
                        order = IRB.CreateBitOrPointerCast(parameters[2], OrdTy);