LLVM  9.0.0svn
LowerAtomic.cpp
Go to the documentation of this file.
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 
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 
24  IRBuilder<> Builder(CXI);
25  Value *Ptr = CXI->getPointerOperand();
26  Value *Cmp = CXI->getCompareOperand();
27  Value *Val = CXI->getNewValOperand();
28 
29  LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
30  Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
31  Value *Res = Builder.CreateSelect(Equal, Val, Orig);
32  Builder.CreateStore(Res, Ptr);
33 
34  Res = Builder.CreateInsertValue(UndefValue::get(CXI->getType()), Orig, 0);
35  Res = Builder.CreateInsertValue(Res, Equal, 1);
36 
37  CXI->replaceAllUsesWith(Res);
38  CXI->eraseFromParent();
39  return true;
40 }
41 
42 static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI) {
43  IRBuilder<> Builder(RMWI);
44  Value *Ptr = RMWI->getPointerOperand();
45  Value *Val = RMWI->getValOperand();
46 
47  LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
48  Value *Res = nullptr;
49 
50  switch (RMWI->getOperation()) {
51  default: llvm_unreachable("Unexpected RMW operation");
53  Res = Val;
54  break;
55  case AtomicRMWInst::Add:
56  Res = Builder.CreateAdd(Orig, Val);
57  break;
58  case AtomicRMWInst::Sub:
59  Res = Builder.CreateSub(Orig, Val);
60  break;
61  case AtomicRMWInst::And:
62  Res = Builder.CreateAnd(Orig, Val);
63  break;
65  Res = Builder.CreateNot(Builder.CreateAnd(Orig, Val));
66  break;
67  case AtomicRMWInst::Or:
68  Res = Builder.CreateOr(Orig, Val);
69  break;
70  case AtomicRMWInst::Xor:
71  Res = Builder.CreateXor(Orig, Val);
72  break;
73  case AtomicRMWInst::Max:
74  Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
75  Val, Orig);
76  break;
77  case AtomicRMWInst::Min:
78  Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
79  Orig, Val);
80  break;
82  Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
83  Val, Orig);
84  break;
86  Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
87  Orig, Val);
88  break;
90  Res = Builder.CreateFAdd(Orig, Val);
91  break;
93  Res = Builder.CreateFSub(Orig, Val);
94  break;
95  }
96  Builder.CreateStore(Res, Ptr);
97  RMWI->replaceAllUsesWith(Orig);
98  RMWI->eraseFromParent();
99  return true;
100 }
101 
102 static bool LowerFenceInst(FenceInst *FI) {
103  FI->eraseFromParent();
104  return true;
105 }
106 
107 static bool LowerLoadInst(LoadInst *LI) {
109  return true;
110 }
111 
112 static bool LowerStoreInst(StoreInst *SI) {
114  return true;
115 }
116 
117 static bool runOnBasicBlock(BasicBlock &BB) {
118  bool Changed = false;
119  for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE;) {
120  Instruction *Inst = &*DI++;
121  if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
122  Changed |= LowerFenceInst(FI);
123  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Inst))
124  Changed |= LowerAtomicCmpXchgInst(CXI);
125  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Inst))
126  Changed |= LowerAtomicRMWInst(RMWI);
127  else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
128  if (LI->isAtomic())
129  LowerLoadInst(LI);
130  } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
131  if (SI->isAtomic())
133  }
134  }
135  return Changed;
136 }
137 
138 static bool lowerAtomics(Function &F) {
139  bool Changed = false;
140  for (BasicBlock &BB : F) {
141  Changed |= runOnBasicBlock(BB);
142  }
143  return Changed;
144 }
145 
147  if (lowerAtomics(F))
148  return PreservedAnalyses::none();
149  return PreservedAnalyses::all();
150 }
151 
152 namespace {
153 class LowerAtomicLegacyPass : public FunctionPass {
154 public:
155  static char ID;
156 
157  LowerAtomicLegacyPass() : FunctionPass(ID) {
159  }
160 
161  bool runOnFunction(Function &F) override {
162  // Don't skip optnone functions; atomics still need to be lowered.
163  FunctionAnalysisManager DummyFAM;
164  auto PA = Impl.run(F, DummyFAM);
165  return !PA.areAllPreserved();
166  }
167 
168 private:
169  LowerAtomicPass Impl;
170  };
171 }
172 
174 INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
175  "Lower atomic intrinsics to non-atomic form", false, false)
176 
177 Pass *llvm::createLowerAtomicPass() { return new LowerAtomicLegacyPass(); }
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
*p = old <signed v ? old : v
Definition: Instructions.h:723
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1932
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ty, Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool...
Definition: IRBuilder.h:1434
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1235
An instruction for ordering other memory operations.
Definition: Instructions.h:454
An instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:530
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1948
static bool runOnBasicBlock(BasicBlock &BB)
*p = old <unsigned v ? old : v
Definition: Instructions.h:727
static bool lowerAtomics(Function &F)
static bool LowerLoadInst(LoadInst *LI)
*p = old >unsigned v ? old : v
Definition: Instructions.h:725
F(f)
An instruction for reading from memory.
Definition: Instructions.h:167
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:693
static bool LowerFenceInst(FenceInst *FI)
A pass that lowers atomic intrinsic into non-atomic intrinsics.
Definition: LowerAtomic.h:22
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this store instruction.
Definition: Instructions.h:395
*p = old >signed v ? old : v
Definition: Instructions.h:721
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:268
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1382
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this load instruction.
Definition: Instructions.h:270
INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic", "Lower atomic intrinsics to non-atomic form", false, false) Pass *llvm
BinOp getOperation() const
Definition: Instructions.h:752
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1049
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1463
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
static bool LowerStoreInst(StoreInst *SI)
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1066
An instruction for storing to memory.
Definition: Instructions.h:320
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1217
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:2099
static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI)
Definition: LowerAtomic.cpp:42
void initializeLowerAtomicLegacyPassPass(PassRegistry &)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1916
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1424
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * getValOperand()
Definition: Instructions.h:817
Iterator for intrusive lists based on ilist_node.
iterator end()
Definition: BasicBlock.h:270
Pass * createLowerAtomicPass()
Value * getPointerOperand()
Definition: Instructions.h:813
Value * CreateFSub(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1265
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1248
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1199
LLVM Value Representation.
Definition: Value.h:72
A container for analyses that lazily runs them and caches their results.
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2171
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Definition: LowerAtomic.cpp:23