refactor codes in CDSPass.cpp and also add support for function-like atomic operation...
[c11llvm.git] / initializeCallbacks.hpp
1 void CDSPass::initializeCallbacks(Module &M) {
2         LLVMContext &Ctx = M.getContext();
3
4         Type * Int1Ty = Type::getInt1Ty(Ctx);
5         Int8Ty  = Type::getInt8Ty(Ctx);
6         Int16Ty = Type::getInt16Ty(Ctx);
7         Int32Ty = Type::getInt32Ty(Ctx);
8         Int64Ty = Type::getInt64Ty(Ctx);
9         OrdTy = Type::getInt32Ty(Ctx);
10
11         Int8PtrTy  = Type::getInt8PtrTy(Ctx);
12         Int16PtrTy = Type::getInt16PtrTy(Ctx);
13         Int32PtrTy = Type::getInt32PtrTy(Ctx);
14         Int64PtrTy = Type::getInt64PtrTy(Ctx);
15
16         VoidTy = Type::getVoidTy(Ctx);
17   
18         // Get the function to call from our untime library.
19         for (unsigned i = 0; i < FUNCARRAYSIZE; i++) {
20                 const unsigned ByteSize = 1U << i;
21                 const unsigned BitSize = ByteSize * 8;
22
23                 std::string ByteSizeStr = utostr(ByteSize);
24                 std::string BitSizeStr = utostr(BitSize);
25
26                 Type *Ty = Type::getIntNTy(Ctx, BitSize);
27                 Type *PtrTy = Ty->getPointerTo();
28
29                 // uint8_t cds_atomic_load8 (void * obj, int atomic_index)
30                 // void cds_atomic_store8 (void * obj, int atomic_index, uint8_t val)
31                 SmallString<32> LoadName("cds_load" + BitSizeStr);
32                 SmallString<32> StoreName("cds_store" + BitSizeStr);
33                 SmallString<32> AtomicInitName("cds_atomic_init" + BitSizeStr);
34                 SmallString<32> AtomicLoadName("cds_atomic_load" + BitSizeStr);
35                 SmallString<32> AtomicStoreName("cds_atomic_store" + BitSizeStr);
36
37                 CDSLoad[i]  = M.getOrInsertFunction(LoadName, VoidTy, PtrTy);
38                 CDSStore[i] = M.getOrInsertFunction(StoreName, VoidTy, PtrTy);
39                 CDSAtomicInit[i] = M.getOrInsertFunction(AtomicInitName, VoidTy, PtrTy, Ty);
40                 CDSAtomicLoad[i]  = M.getOrInsertFunction(AtomicLoadName, Ty, PtrTy, OrdTy);
41                 CDSAtomicStore[i] = M.getOrInsertFunction(AtomicStoreName, VoidTy, PtrTy, Ty, OrdTy);
42
43                 for (int op = AtomicRMWInst::FIRST_BINOP; 
44                         op <= AtomicRMWInst::LAST_BINOP; ++op) {
45                         CDSAtomicRMW[op][i] = nullptr;
46                         std::string NamePart;
47
48                         if (op == AtomicRMWInst::Xchg)
49                                 NamePart = "_exchange";
50                         else if (op == AtomicRMWInst::Add) 
51                                 NamePart = "_fetch_add";
52                         else if (op == AtomicRMWInst::Sub)
53                                 NamePart = "_fetch_sub";
54                         else if (op == AtomicRMWInst::And)
55                                 NamePart = "_fetch_and";
56                         else if (op == AtomicRMWInst::Or)
57                                 NamePart = "_fetch_or";
58                         else if (op == AtomicRMWInst::Xor)
59                                 NamePart = "_fetch_xor";
60                         else
61                                 continue;
62
63                         SmallString<32> AtomicRMWName("cds_atomic" + NamePart + BitSizeStr);
64                         CDSAtomicRMW[op][i] = M.getOrInsertFunction(AtomicRMWName, Ty, PtrTy, Ty, OrdTy);
65                 }
66
67                 // only supportes strong version
68                 SmallString<32> AtomicCASName_V1("cds_atomic_compare_exchange" + BitSizeStr + "_v1");
69                 SmallString<32> AtomicCASName_V2("cds_atomic_compare_exchange" + BitSizeStr + "_v2");
70                 CDSAtomicCAS_V1[i] = M.getOrInsertFunction(AtomicCASName_V1, 
71                                                                 Ty, PtrTy, Ty, Ty, OrdTy, OrdTy);
72                 CDSAtomicCAS_V2[i] = M.getOrInsertFunction(AtomicCASName_V2, 
73                                                                 Int1Ty, PtrTy, PtrTy, Ty, OrdTy, OrdTy);
74         }
75
76         CDSAtomicThreadFence = M.getOrInsertFunction("cds_atomic_thread_fence", VoidTy, OrdTy);
77 }