Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/LowerAtomic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This pass lowers atomic intrinsics to non-atomic form for use in a known
10
// non-preemptible environment.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Transforms/Scalar/LowerAtomic.h"
15
#include "llvm/IR/Function.h"
16
#include "llvm/IR/IRBuilder.h"
17
#include "llvm/Pass.h"
18
#include "llvm/Transforms/Scalar.h"
19
using namespace llvm;
20
21
#define DEBUG_TYPE "loweratomic"
22
23
59
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI) {
24
59
  IRBuilder<> Builder(CXI);
25
59
  Value *Ptr = CXI->getPointerOperand();
26
59
  Value *Cmp = CXI->getCompareOperand();
27
59
  Value *Val = CXI->getNewValOperand();
28
59
29
59
  LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
30
59
  Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
31
59
  Value *Res = Builder.CreateSelect(Equal, Val, Orig);
32
59
  Builder.CreateStore(Res, Ptr);
33
59
34
59
  Res = Builder.CreateInsertValue(UndefValue::get(CXI->getType()), Orig, 0);
35
59
  Res = Builder.CreateInsertValue(Res, Equal, 1);
36
59
37
59
  CXI->replaceAllUsesWith(Res);
38
59
  CXI->eraseFromParent();
39
59
  return true;
40
59
}
41
42
168
static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI) {
43
168
  IRBuilder<> Builder(RMWI);
44
168
  Value *Ptr = RMWI->getPointerOperand();
45
168
  Value *Val = RMWI->getValOperand();
46
168
47
168
  LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
48
168
  Value *Res = nullptr;
49
168
50
168
  switch (RMWI->getOperation()) {
51
168
  
default: 0
llvm_unreachable0
("Unexpected RMW operation");
52
168
  case AtomicRMWInst::Xchg:
53
14
    Res = Val;
54
14
    break;
55
168
  case AtomicRMWInst::Add:
56
55
    Res = Builder.CreateAdd(Orig, Val);
57
55
    break;
58
168
  case AtomicRMWInst::Sub:
59
14
    Res = Builder.CreateSub(Orig, Val);
60
14
    break;
61
168
  case AtomicRMWInst::And:
62
13
    Res = Builder.CreateAnd(Orig, Val);
63
13
    break;
64
168
  case AtomicRMWInst::Nand:
65
14
    Res = Builder.CreateNot(Builder.CreateAnd(Orig, Val));
66
14
    break;
67
168
  case AtomicRMWInst::Or:
68
13
    Res = Builder.CreateOr(Orig, Val);
69
13
    break;
70
168
  case AtomicRMWInst::Xor:
71
13
    Res = Builder.CreateXor(Orig, Val);
72
13
    break;
73
168
  case AtomicRMWInst::Max:
74
4
    Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
75
4
                               Val, Orig);
76
4
    break;
77
168
  case AtomicRMWInst::Min:
78
8
    Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
79
8
                               Orig, Val);
80
8
    break;
81
168
  case AtomicRMWInst::UMax:
82
8
    Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
83
8
                               Val, Orig);
84
8
    break;
85
168
  case AtomicRMWInst::UMin:
86
8
    Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
87
8
                               Orig, Val);
88
8
    break;
89
168
  case AtomicRMWInst::FAdd:
90
2
    Res = Builder.CreateFAdd(Orig, Val);
91
2
    break;
92
168
  case AtomicRMWInst::FSub:
93
2
    Res = Builder.CreateFSub(Orig, Val);
94
2
    break;
95
168
  }
96
168
  Builder.CreateStore(Res, Ptr);
97
168
  RMWI->replaceAllUsesWith(Orig);
98
168
  RMWI->eraseFromParent();
99
168
  return true;
100
168
}
101
102
12
static bool LowerFenceInst(FenceInst *FI) {
103
12
  FI->eraseFromParent();
104
12
  return true;
105
12
}
106
107
41
static bool LowerLoadInst(LoadInst *LI) {
108
41
  LI->setAtomic(AtomicOrdering::NotAtomic);
109
41
  return true;
110
41
}
111
112
28
static bool LowerStoreInst(StoreInst *SI) {
113
28
  SI->setAtomic(AtomicOrdering::NotAtomic);
114
28
  return true;
115
28
}
116
117
302
static bool runOnBasicBlock(BasicBlock &BB) {
118
302
  bool Changed = false;
119
1.57k
  for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE;) {
120
1.27k
    Instruction *Inst = &*DI++;
121
1.27k
    if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
122
12
      Changed |= LowerFenceInst(FI);
123
1.26k
    else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Inst))
124
59
      Changed |= LowerAtomicCmpXchgInst(CXI);
125
1.20k
    else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Inst))
126
168
      Changed |= LowerAtomicRMWInst(RMWI);
127
1.03k
    else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
128
42
      if (LI->isAtomic())
129
41
        LowerLoadInst(LI);
130
996
    } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
131
63
      if (SI->isAtomic())
132
28
        LowerStoreInst(SI);
133
63
    }
134
1.27k
  }
135
302
  return Changed;
136
302
}
137
138
305
static bool lowerAtomics(Function &F) {
139
305
  bool Changed = false;
140
305
  for (BasicBlock &BB : F) {
141
302
    Changed |= runOnBasicBlock(BB);
142
302
  }
143
305
  return Changed;
144
305
}
145
146
305
PreservedAnalyses LowerAtomicPass::run(Function &F, FunctionAnalysisManager &) {
147
305
  if (lowerAtomics(F))
148
214
    return PreservedAnalyses::none();
149
91
  return PreservedAnalyses::all();
150
91
}
151
152
namespace {
153
class LowerAtomicLegacyPass : public FunctionPass {
154
public:
155
  static char ID;
156
157
4
  LowerAtomicLegacyPass() : FunctionPass(ID) {
158
4
    initializeLowerAtomicLegacyPassPass(*PassRegistry::getPassRegistry());
159
4
  }
160
161
18
  bool runOnFunction(Function &F) override {
162
18
    // Don't skip optnone functions; atomics still need to be lowered.
163
18
    FunctionAnalysisManager DummyFAM;
164
18
    auto PA = Impl.run(F, DummyFAM);
165
18
    return !PA.areAllPreserved();
166
18
  }
167
168
private:
169
  LowerAtomicPass Impl;
170
  };
171
}
172
173
char LowerAtomicLegacyPass::ID = 0;
174
INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
175
                "Lower atomic intrinsics to non-atomic form", false, false)
176
177
1
Pass *llvm::createLowerAtomicPass() { return new LowerAtomicLegacyPass(); }