Use range-based for loops in ASan, TSan and MSan
[oota-llvm.git] / lib / Transforms / Instrumentation / ThreadSanitizer.cpp
1 //===-- ThreadSanitizer.cpp - race detector -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer, a race detector.
11 //
12 // The tool is under development, for the details about previous versions see
13 // http://code.google.com/p/data-race-test
14 //
15 // The instrumentation phase is quite simple:
16 //   - Insert calls to run-time library before every memory access.
17 //      - Optimizations may apply to avoid instrumenting some of the accesses.
18 //   - Insert calls at function entry/exit.
19 // The rest is handled by the run-time library.
20 //===----------------------------------------------------------------------===//
21
22 #include "llvm/Transforms/Instrumentation.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/IRBuilder.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/IR/Metadata.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
42 #include "llvm/Transforms/Utils/ModuleUtils.h"
43 #include "llvm/Transforms/Utils/SpecialCaseList.h"
44
45 using namespace llvm;
46
47 #define DEBUG_TYPE "tsan"
48
49 static cl::opt<std::string>  ClBlacklistFile("tsan-blacklist",
50        cl::desc("Blacklist file"), cl::Hidden);
51 static cl::opt<bool>  ClInstrumentMemoryAccesses(
52     "tsan-instrument-memory-accesses", cl::init(true),
53     cl::desc("Instrument memory accesses"), cl::Hidden);
54 static cl::opt<bool>  ClInstrumentFuncEntryExit(
55     "tsan-instrument-func-entry-exit", cl::init(true),
56     cl::desc("Instrument function entry and exit"), cl::Hidden);
57 static cl::opt<bool>  ClInstrumentAtomics(
58     "tsan-instrument-atomics", cl::init(true),
59     cl::desc("Instrument atomics"), cl::Hidden);
60 static cl::opt<bool>  ClInstrumentMemIntrinsics(
61     "tsan-instrument-memintrinsics", cl::init(true),
62     cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden);
63
64 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
65 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
66 STATISTIC(NumOmittedReadsBeforeWrite,
67           "Number of reads ignored due to following writes");
68 STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
69 STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
70 STATISTIC(NumInstrumentedVtableReads, "Number of vtable ptr reads");
71 STATISTIC(NumOmittedReadsFromConstantGlobals,
72           "Number of reads from constant globals");
73 STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
74
75 namespace {
76
77 /// ThreadSanitizer: instrument the code in module to find races.
78 struct ThreadSanitizer : public FunctionPass {
79   ThreadSanitizer(StringRef BlacklistFile = StringRef())
80       : FunctionPass(ID),
81         DL(nullptr),
82         BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
83                                             : BlacklistFile) { }
84   const char *getPassName() const override;
85   bool runOnFunction(Function &F) override;
86   bool doInitialization(Module &M) override;
87   static char ID;  // Pass identification, replacement for typeid.
88
89  private:
90   void initializeCallbacks(Module &M);
91   bool instrumentLoadOrStore(Instruction *I);
92   bool instrumentAtomic(Instruction *I);
93   bool instrumentMemIntrinsic(Instruction *I);
94   void chooseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
95                                       SmallVectorImpl<Instruction*> &All);
96   bool addrPointsToConstantData(Value *Addr);
97   int getMemoryAccessFuncIndex(Value *Addr);
98
99   const DataLayout *DL;
100   Type *IntptrTy;
101   SmallString<64> BlacklistFile;
102   std::unique_ptr<SpecialCaseList> BL;
103   IntegerType *OrdTy;
104   // Callbacks to run-time library are computed in doInitialization.
105   Function *TsanFuncEntry;
106   Function *TsanFuncExit;
107   // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
108   static const size_t kNumberOfAccessSizes = 5;
109   Function *TsanRead[kNumberOfAccessSizes];
110   Function *TsanWrite[kNumberOfAccessSizes];
111   Function *TsanAtomicLoad[kNumberOfAccessSizes];
112   Function *TsanAtomicStore[kNumberOfAccessSizes];
113   Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes];
114   Function *TsanAtomicCAS[kNumberOfAccessSizes];
115   Function *TsanAtomicThreadFence;
116   Function *TsanAtomicSignalFence;
117   Function *TsanVptrUpdate;
118   Function *TsanVptrLoad;
119   Function *MemmoveFn, *MemcpyFn, *MemsetFn;
120 };
121 }  // namespace
122
123 char ThreadSanitizer::ID = 0;
124 INITIALIZE_PASS(ThreadSanitizer, "tsan",
125     "ThreadSanitizer: detects data races.",
126     false, false)
127
128 const char *ThreadSanitizer::getPassName() const {
129   return "ThreadSanitizer";
130 }
131
132 FunctionPass *llvm::createThreadSanitizerPass(StringRef BlacklistFile) {
133   return new ThreadSanitizer(BlacklistFile);
134 }
135
136 static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
137   if (Function *F = dyn_cast<Function>(FuncOrBitcast))
138      return F;
139   FuncOrBitcast->dump();
140   report_fatal_error("ThreadSanitizer interface function redefined");
141 }
142
143 void ThreadSanitizer::initializeCallbacks(Module &M) {
144   IRBuilder<> IRB(M.getContext());
145   // Initialize the callbacks.
146   TsanFuncEntry = checkInterfaceFunction(M.getOrInsertFunction(
147       "__tsan_func_entry", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
148   TsanFuncExit = checkInterfaceFunction(M.getOrInsertFunction(
149       "__tsan_func_exit", IRB.getVoidTy(), NULL));
150   OrdTy = IRB.getInt32Ty();
151   for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
152     const size_t ByteSize = 1 << i;
153     const size_t BitSize = ByteSize * 8;
154     SmallString<32> ReadName("__tsan_read" + itostr(ByteSize));
155     TsanRead[i] = checkInterfaceFunction(M.getOrInsertFunction(
156         ReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
157
158     SmallString<32> WriteName("__tsan_write" + itostr(ByteSize));
159     TsanWrite[i] = checkInterfaceFunction(M.getOrInsertFunction(
160         WriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
161
162     Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
163     Type *PtrTy = Ty->getPointerTo();
164     SmallString<32> AtomicLoadName("__tsan_atomic" + itostr(BitSize) +
165                                    "_load");
166     TsanAtomicLoad[i] = checkInterfaceFunction(M.getOrInsertFunction(
167         AtomicLoadName, Ty, PtrTy, OrdTy, NULL));
168
169     SmallString<32> AtomicStoreName("__tsan_atomic" + itostr(BitSize) +
170                                     "_store");
171     TsanAtomicStore[i] = checkInterfaceFunction(M.getOrInsertFunction(
172         AtomicStoreName, IRB.getVoidTy(), PtrTy, Ty, OrdTy,
173         NULL));
174
175     for (int op = AtomicRMWInst::FIRST_BINOP;
176         op <= AtomicRMWInst::LAST_BINOP; ++op) {
177       TsanAtomicRMW[op][i] = nullptr;
178       const char *NamePart = nullptr;
179       if (op == AtomicRMWInst::Xchg)
180         NamePart = "_exchange";
181       else if (op == AtomicRMWInst::Add)
182         NamePart = "_fetch_add";
183       else if (op == AtomicRMWInst::Sub)
184         NamePart = "_fetch_sub";
185       else if (op == AtomicRMWInst::And)
186         NamePart = "_fetch_and";
187       else if (op == AtomicRMWInst::Or)
188         NamePart = "_fetch_or";
189       else if (op == AtomicRMWInst::Xor)
190         NamePart = "_fetch_xor";
191       else if (op == AtomicRMWInst::Nand)
192         NamePart = "_fetch_nand";
193       else
194         continue;
195       SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
196       TsanAtomicRMW[op][i] = checkInterfaceFunction(M.getOrInsertFunction(
197           RMWName, Ty, PtrTy, Ty, OrdTy, NULL));
198     }
199
200     SmallString<32> AtomicCASName("__tsan_atomic" + itostr(BitSize) +
201                                   "_compare_exchange_val");
202     TsanAtomicCAS[i] = checkInterfaceFunction(M.getOrInsertFunction(
203         AtomicCASName, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, NULL));
204   }
205   TsanVptrUpdate = checkInterfaceFunction(M.getOrInsertFunction(
206       "__tsan_vptr_update", IRB.getVoidTy(), IRB.getInt8PtrTy(),
207       IRB.getInt8PtrTy(), NULL));
208   TsanVptrLoad = checkInterfaceFunction(M.getOrInsertFunction(
209       "__tsan_vptr_read", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
210   TsanAtomicThreadFence = checkInterfaceFunction(M.getOrInsertFunction(
211       "__tsan_atomic_thread_fence", IRB.getVoidTy(), OrdTy, NULL));
212   TsanAtomicSignalFence = checkInterfaceFunction(M.getOrInsertFunction(
213       "__tsan_atomic_signal_fence", IRB.getVoidTy(), OrdTy, NULL));
214
215   MemmoveFn = checkInterfaceFunction(M.getOrInsertFunction(
216     "memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
217     IRB.getInt8PtrTy(), IntptrTy, NULL));
218   MemcpyFn = checkInterfaceFunction(M.getOrInsertFunction(
219     "memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
220     IntptrTy, NULL));
221   MemsetFn = checkInterfaceFunction(M.getOrInsertFunction(
222     "memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
223     IntptrTy, NULL));
224 }
225
226 bool ThreadSanitizer::doInitialization(Module &M) {
227   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
228   if (!DLP)
229     report_fatal_error("data layout missing");
230   DL = &DLP->getDataLayout();
231   BL.reset(SpecialCaseList::createOrDie(BlacklistFile));
232
233   // Always insert a call to __tsan_init into the module's CTORs.
234   IRBuilder<> IRB(M.getContext());
235   IntptrTy = IRB.getIntPtrTy(DL);
236   Value *TsanInit = M.getOrInsertFunction("__tsan_init",
237                                           IRB.getVoidTy(), NULL);
238   appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
239
240   return true;
241 }
242
243 static bool isVtableAccess(Instruction *I) {
244   if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
245     return Tag->isTBAAVtableAccess();
246   return false;
247 }
248
249 bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
250   // If this is a GEP, just analyze its pointer operand.
251   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
252     Addr = GEP->getPointerOperand();
253
254   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
255     if (GV->isConstant()) {
256       // Reads from constant globals can not race with any writes.
257       NumOmittedReadsFromConstantGlobals++;
258       return true;
259     }
260   } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) {
261     if (isVtableAccess(L)) {
262       // Reads from a vtable pointer can not race with any writes.
263       NumOmittedReadsFromVtable++;
264       return true;
265     }
266   }
267   return false;
268 }
269
270 // Instrumenting some of the accesses may be proven redundant.
271 // Currently handled:
272 //  - read-before-write (within same BB, no calls between)
273 //
274 // We do not handle some of the patterns that should not survive
275 // after the classic compiler optimizations.
276 // E.g. two reads from the same temp should be eliminated by CSE,
277 // two writes should be eliminated by DSE, etc.
278 //
279 // 'Local' is a vector of insns within the same BB (no calls between).
280 // 'All' is a vector of insns that will be instrumented.
281 void ThreadSanitizer::chooseInstructionsToInstrument(
282     SmallVectorImpl<Instruction*> &Local,
283     SmallVectorImpl<Instruction*> &All) {
284   SmallSet<Value*, 8> WriteTargets;
285   // Iterate from the end.
286   for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
287        E = Local.rend(); It != E; ++It) {
288     Instruction *I = *It;
289     if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
290       WriteTargets.insert(Store->getPointerOperand());
291     } else {
292       LoadInst *Load = cast<LoadInst>(I);
293       Value *Addr = Load->getPointerOperand();
294       if (WriteTargets.count(Addr)) {
295         // We will write to this temp, so no reason to analyze the read.
296         NumOmittedReadsBeforeWrite++;
297         continue;
298       }
299       if (addrPointsToConstantData(Addr)) {
300         // Addr points to some constant data -- it can not race with any writes.
301         continue;
302       }
303     }
304     All.push_back(I);
305   }
306   Local.clear();
307 }
308
309 static bool isAtomic(Instruction *I) {
310   if (LoadInst *LI = dyn_cast<LoadInst>(I))
311     return LI->isAtomic() && LI->getSynchScope() == CrossThread;
312   if (StoreInst *SI = dyn_cast<StoreInst>(I))
313     return SI->isAtomic() && SI->getSynchScope() == CrossThread;
314   if (isa<AtomicRMWInst>(I))
315     return true;
316   if (isa<AtomicCmpXchgInst>(I))
317     return true;
318   if (isa<FenceInst>(I))
319     return true;
320   return false;
321 }
322
323 bool ThreadSanitizer::runOnFunction(Function &F) {
324   if (!DL) return false;
325   if (BL->isIn(F)) return false;
326   initializeCallbacks(*F.getParent());
327   SmallVector<Instruction*, 8> RetVec;
328   SmallVector<Instruction*, 8> AllLoadsAndStores;
329   SmallVector<Instruction*, 8> LocalLoadsAndStores;
330   SmallVector<Instruction*, 8> AtomicAccesses;
331   SmallVector<Instruction*, 8> MemIntrinCalls;
332   bool Res = false;
333   bool HasCalls = false;
334
335   // Traverse all instructions, collect loads/stores/returns, check for calls.
336   for (auto &BB : F) {
337     for (auto &Inst : BB) {
338       if (isAtomic(&Inst))
339         AtomicAccesses.push_back(&Inst);
340       else if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
341         LocalLoadsAndStores.push_back(&Inst);
342       else if (isa<ReturnInst>(Inst))
343         RetVec.push_back(&Inst);
344       else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
345         if (isa<MemIntrinsic>(Inst))
346           MemIntrinCalls.push_back(&Inst);
347         HasCalls = true;
348         chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
349       }
350     }
351     chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
352   }
353
354   // We have collected all loads and stores.
355   // FIXME: many of these accesses do not need to be checked for races
356   // (e.g. variables that do not escape, etc).
357
358   // Instrument memory accesses.
359   if (ClInstrumentMemoryAccesses && F.hasFnAttribute(Attribute::SanitizeThread))
360     for (auto Inst : AllLoadsAndStores) {
361       Res |= instrumentLoadOrStore(Inst);
362     }
363
364   // Instrument atomic memory accesses.
365   if (ClInstrumentAtomics)
366     for (auto Inst : AtomicAccesses) {
367       Res |= instrumentAtomic(Inst);
368     }
369
370   if (ClInstrumentMemIntrinsics)
371     for (auto Inst : MemIntrinCalls) {
372       Res |= instrumentMemIntrinsic(Inst);
373     }
374
375   // Instrument function entry/exit points if there were instrumented accesses.
376   if ((Res || HasCalls) && ClInstrumentFuncEntryExit) {
377     IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
378     Value *ReturnAddress = IRB.CreateCall(
379         Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
380         IRB.getInt32(0));
381     IRB.CreateCall(TsanFuncEntry, ReturnAddress);
382     for (auto RetInst : RetVec) {
383       IRBuilder<> IRBRet(RetInst);
384       IRBRet.CreateCall(TsanFuncExit);
385     }
386     Res = true;
387   }
388   return Res;
389 }
390
391 bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
392   IRBuilder<> IRB(I);
393   bool IsWrite = isa<StoreInst>(*I);
394   Value *Addr = IsWrite
395       ? cast<StoreInst>(I)->getPointerOperand()
396       : cast<LoadInst>(I)->getPointerOperand();
397   int Idx = getMemoryAccessFuncIndex(Addr);
398   if (Idx < 0)
399     return false;
400   if (IsWrite && isVtableAccess(I)) {
401     DEBUG(dbgs() << "  VPTR : " << *I << "\n");
402     Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
403     // StoredValue may be a vector type if we are storing several vptrs at once.
404     // In this case, just take the first element of the vector since this is
405     // enough to find vptr races.
406     if (isa<VectorType>(StoredValue->getType()))
407       StoredValue = IRB.CreateExtractElement(
408           StoredValue, ConstantInt::get(IRB.getInt32Ty(), 0));
409     if (StoredValue->getType()->isIntegerTy())
410       StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
411     // Call TsanVptrUpdate.
412     IRB.CreateCall2(TsanVptrUpdate,
413                     IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
414                     IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
415     NumInstrumentedVtableWrites++;
416     return true;
417   }
418   if (!IsWrite && isVtableAccess(I)) {
419     IRB.CreateCall(TsanVptrLoad,
420                    IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
421     NumInstrumentedVtableReads++;
422     return true;
423   }
424   Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
425   IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
426   if (IsWrite) NumInstrumentedWrites++;
427   else         NumInstrumentedReads++;
428   return true;
429 }
430
431 static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
432   uint32_t v = 0;
433   switch (ord) {
434     case NotAtomic:              assert(false);
435     case Unordered:              // Fall-through.
436     case Monotonic:              v = 0; break;
437     // case Consume:                v = 1; break;  // Not specified yet.
438     case Acquire:                v = 2; break;
439     case Release:                v = 3; break;
440     case AcquireRelease:         v = 4; break;
441     case SequentiallyConsistent: v = 5; break;
442   }
443   return IRB->getInt32(v);
444 }
445
446 // If a memset intrinsic gets inlined by the code gen, we will miss races on it.
447 // So, we either need to ensure the intrinsic is not inlined, or instrument it.
448 // We do not instrument memset/memmove/memcpy intrinsics (too complicated),
449 // instead we simply replace them with regular function calls, which are then
450 // intercepted by the run-time.
451 // Since tsan is running after everyone else, the calls should not be
452 // replaced back with intrinsics. If that becomes wrong at some point,
453 // we will need to call e.g. __tsan_memset to avoid the intrinsics.
454 bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) {
455   IRBuilder<> IRB(I);
456   if (MemSetInst *M = dyn_cast<MemSetInst>(I)) {
457     IRB.CreateCall3(MemsetFn,
458       IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
459       IRB.CreateIntCast(M->getArgOperand(1), IRB.getInt32Ty(), false),
460       IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false));
461     I->eraseFromParent();
462   } else if (MemTransferInst *M = dyn_cast<MemTransferInst>(I)) {
463     IRB.CreateCall3(isa<MemCpyInst>(M) ? MemcpyFn : MemmoveFn,
464       IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
465       IRB.CreatePointerCast(M->getArgOperand(1), IRB.getInt8PtrTy()),
466       IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false));
467     I->eraseFromParent();
468   }
469   return false;
470 }
471
472 // Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
473 // standards.  For background see C++11 standard.  A slightly older, publicly
474 // available draft of the standard (not entirely up-to-date, but close enough
475 // for casual browsing) is available here:
476 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
477 // The following page contains more background information:
478 // http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/
479
480 bool ThreadSanitizer::instrumentAtomic(Instruction *I) {
481   IRBuilder<> IRB(I);
482   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
483     Value *Addr = LI->getPointerOperand();
484     int Idx = getMemoryAccessFuncIndex(Addr);
485     if (Idx < 0)
486       return false;
487     const size_t ByteSize = 1 << Idx;
488     const size_t BitSize = ByteSize * 8;
489     Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
490     Type *PtrTy = Ty->getPointerTo();
491     Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
492                      createOrdering(&IRB, LI->getOrdering())};
493     CallInst *C = CallInst::Create(TsanAtomicLoad[Idx],
494                                    ArrayRef<Value*>(Args));
495     ReplaceInstWithInst(I, C);
496
497   } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
498     Value *Addr = SI->getPointerOperand();
499     int Idx = getMemoryAccessFuncIndex(Addr);
500     if (Idx < 0)
501       return false;
502     const size_t ByteSize = 1 << Idx;
503     const size_t BitSize = ByteSize * 8;
504     Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
505     Type *PtrTy = Ty->getPointerTo();
506     Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
507                      IRB.CreateIntCast(SI->getValueOperand(), Ty, false),
508                      createOrdering(&IRB, SI->getOrdering())};
509     CallInst *C = CallInst::Create(TsanAtomicStore[Idx],
510                                    ArrayRef<Value*>(Args));
511     ReplaceInstWithInst(I, C);
512   } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) {
513     Value *Addr = RMWI->getPointerOperand();
514     int Idx = getMemoryAccessFuncIndex(Addr);
515     if (Idx < 0)
516       return false;
517     Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
518     if (!F)
519       return false;
520     const size_t ByteSize = 1 << Idx;
521     const size_t BitSize = ByteSize * 8;
522     Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
523     Type *PtrTy = Ty->getPointerTo();
524     Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
525                      IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
526                      createOrdering(&IRB, RMWI->getOrdering())};
527     CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args));
528     ReplaceInstWithInst(I, C);
529   } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
530     Value *Addr = CASI->getPointerOperand();
531     int Idx = getMemoryAccessFuncIndex(Addr);
532     if (Idx < 0)
533       return false;
534     const size_t ByteSize = 1 << Idx;
535     const size_t BitSize = ByteSize * 8;
536     Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
537     Type *PtrTy = Ty->getPointerTo();
538     Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
539                      IRB.CreateIntCast(CASI->getCompareOperand(), Ty, false),
540                      IRB.CreateIntCast(CASI->getNewValOperand(), Ty, false),
541                      createOrdering(&IRB, CASI->getSuccessOrdering()),
542                      createOrdering(&IRB, CASI->getFailureOrdering())};
543     CallInst *C = CallInst::Create(TsanAtomicCAS[Idx], ArrayRef<Value*>(Args));
544     ReplaceInstWithInst(I, C);
545   } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
546     Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
547     Function *F = FI->getSynchScope() == SingleThread ?
548         TsanAtomicSignalFence : TsanAtomicThreadFence;
549     CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args));
550     ReplaceInstWithInst(I, C);
551   }
552   return true;
553 }
554
555 int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr) {
556   Type *OrigPtrTy = Addr->getType();
557   Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
558   assert(OrigTy->isSized());
559   uint32_t TypeSize = DL->getTypeStoreSizeInBits(OrigTy);
560   if (TypeSize != 8  && TypeSize != 16 &&
561       TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
562     NumAccessesWithBadSize++;
563     // Ignore all unusual sizes.
564     return -1;
565   }
566   size_t Idx = countTrailingZeros(TypeSize / 8);
567   assert(Idx < kNumberOfAccessSizes);
568   return Idx;
569 }