summaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/LowerAtomic.cpp
blob: 11f52570e18da97d98e8bd73c9966c3030bd7be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass lowers atomic intrinsics to non-atomic form for use in a known
// non-preemptible environment.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "loweratomic"
#include "llvm/Transforms/Scalar.h"
#include "llvm/BasicBlock.h"
#include "llvm/Function.h"
#include "llvm/Instruction.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/Pass.h"
#include "llvm/Support/IRBuilder.h"

using namespace llvm;

namespace {

bool LowerAtomicIntrinsic(CallInst *CI) {
  IRBuilder<> Builder(CI->getParent(), CI);

  Function *Callee = CI->getCalledFunction();
  if (!Callee)
    return false;

  unsigned IID = Callee->getIntrinsicID();
  switch (IID) {
  case Intrinsic::memory_barrier:
    break;

  case Intrinsic::atomic_load_add:
  case Intrinsic::atomic_load_sub:
  case Intrinsic::atomic_load_and:
  case Intrinsic::atomic_load_nand:
  case Intrinsic::atomic_load_or:
  case Intrinsic::atomic_load_xor:
  case Intrinsic::atomic_load_max:
  case Intrinsic::atomic_load_min:
  case Intrinsic::atomic_load_umax:
  case Intrinsic::atomic_load_umin: {
    Value *Ptr = CI->getArgOperand(0);
    Value *Delta = CI->getArgOperand(1);

    LoadInst *Orig = Builder.CreateLoad(Ptr);
    Value *Res;
    switch (IID) {
      case Intrinsic::atomic_load_add:
        Res = Builder.CreateAdd(Orig, Delta);
        break;
      case Intrinsic::atomic_load_sub:
        Res = Builder.CreateSub(Orig, Delta);
        break;
      case Intrinsic::atomic_load_and:
        Res = Builder.CreateAnd(Orig, Delta);
        break;
      case Intrinsic::atomic_load_nand:
        Res = Builder.CreateNot(Builder.CreateAnd(Orig, Delta));
        break;
      case Intrinsic::atomic_load_or:
        Res = Builder.CreateOr(Orig, Delta);
        break;
      case Intrinsic::atomic_load_xor:
        Res = Builder.CreateXor(Orig, Delta);
        break;
      case Intrinsic::atomic_load_max:
        Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Delta),
                                   Delta,
                                   Orig);
        break;
      case Intrinsic::atomic_load_min:
        Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Delta),
                                   Orig,
                                   Delta);
        break;
      case Intrinsic::atomic_load_umax:
        Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Delta),
                                   Delta,
                                   Orig);
        break;
      case Intrinsic::atomic_load_umin:
        Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Delta),
                                   Orig,
                                   Delta);
        break;
      default: assert(0 && "Unrecognized atomic modify operation");
    }
    Builder.CreateStore(Res, Ptr);

    CI->replaceAllUsesWith(Orig);
    break;
  }

  case Intrinsic::atomic_swap: {
    Value *Ptr = CI->getArgOperand(0);
    Value *Val = CI->getArgOperand(1);

    LoadInst *Orig = Builder.CreateLoad(Ptr);
    Builder.CreateStore(Val, Ptr);

    CI->replaceAllUsesWith(Orig);
    break;
  }

  case Intrinsic::atomic_cmp_swap: {
    Value *Ptr = CI->getArgOperand(0);
    Value *Cmp = CI->getArgOperand(1);
    Value *Val = CI->getArgOperand(2);

    LoadInst *Orig = Builder.CreateLoad(Ptr);
    Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
    Value *Res = Builder.CreateSelect(Equal, Val, Orig);
    Builder.CreateStore(Res, Ptr);

    CI->replaceAllUsesWith(Orig);
    break;
  }

  default:
    return false;
  }

  assert(CI->use_empty() &&
         "Lowering should have eliminated any uses of the intrinsic call!");
  CI->eraseFromParent();

  return true;
}

struct LowerAtomic : public BasicBlockPass {
  static char ID;
  LowerAtomic() : BasicBlockPass(&ID) {}
  bool runOnBasicBlock(BasicBlock &BB) {
    bool Changed = false;
    for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE; ) {
      Instruction *Inst = DI++;
      if (CallInst *CI = dyn_cast<CallInst>(Inst))
        Changed |= LowerAtomicIntrinsic(CI);
    }
    return Changed;
  }

};

}

char LowerAtomic::ID = 0;
static RegisterPass<LowerAtomic>
X("loweratomic", "Lower atomic intrinsics to non-atomic form");

Pass *llvm::createLowerAtomicPass() { return new LowerAtomic(); }