LLVM  12.0.0git
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/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Transforms/Scalar.h"
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "loweratomic"
23 
25  IRBuilder<> Builder(CXI);
26  Value *Ptr = CXI->getPointerOperand();
27  Value *Cmp = CXI->getCompareOperand();
28  Value *Val = CXI->getNewValOperand();
29 
30  LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
31  Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
32  Value *Res = Builder.CreateSelect(Equal, Val, Orig);
33  Builder.CreateStore(Res, Ptr);
34 
35  Res = Builder.CreateInsertValue(UndefValue::get(CXI->getType()), Orig, 0);
36  Res = Builder.CreateInsertValue(Res, Equal, 1);
37 
38  CXI->replaceAllUsesWith(Res);
39  CXI->eraseFromParent();
40  return true;
41 }
42 
43 static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI) {
44  IRBuilder<> Builder(RMWI);
45  Value *Ptr = RMWI->getPointerOperand();
46  Value *Val = RMWI->getValOperand();
47 
48  LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
49  Value *Res = nullptr;
50 
51  switch (RMWI->getOperation()) {
52  default: llvm_unreachable("Unexpected RMW operation");
54  Res = Val;
55  break;
56  case AtomicRMWInst::Add:
57  Res = Builder.CreateAdd(Orig, Val);
58  break;
59  case AtomicRMWInst::Sub:
60  Res = Builder.CreateSub(Orig, Val);
61  break;
62  case AtomicRMWInst::And:
63  Res = Builder.CreateAnd(Orig, Val);
64  break;
66  Res = Builder.CreateNot(Builder.CreateAnd(Orig, Val));
67  break;
68  case AtomicRMWInst::Or:
69  Res = Builder.CreateOr(Orig, Val);
70  break;
71  case AtomicRMWInst::Xor:
72  Res = Builder.CreateXor(Orig, Val);
73  break;
74  case AtomicRMWInst::Max:
75  Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
76  Val, Orig);
77  break;
78  case AtomicRMWInst::Min:
79  Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
80  Orig, Val);
81  break;
83  Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
84  Val, Orig);
85  break;
87  Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
88  Orig, Val);
89  break;
91  Res = Builder.CreateFAdd(Orig, Val);
92  break;
94  Res = Builder.CreateFSub(Orig, Val);
95  break;
96  }
97  Builder.CreateStore(Res, Ptr);
98  RMWI->replaceAllUsesWith(Orig);
99  RMWI->eraseFromParent();
100  return true;
101 }
102 
103 static bool LowerFenceInst(FenceInst *FI) {
104  FI->eraseFromParent();
105  return true;
106 }
107 
108 static bool LowerLoadInst(LoadInst *LI) {
110  return true;
111 }
112 
113 static bool LowerStoreInst(StoreInst *SI) {
115  return true;
116 }
117 
118 static bool runOnBasicBlock(BasicBlock &BB) {
119  bool Changed = false;
120  for (Instruction &Inst : make_early_inc_range(BB)) {
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:77
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:80
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2164
An instruction for ordering other memory operations.
Definition: Instructions.h:443
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:521
static bool runOnBasicBlock(BasicBlock &BB)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:584
static bool lowerAtomics(Function &F)
static bool LowerLoadInst(LoadInst *LI)
F(f)
An instruction for reading from memory.
Definition: Instructions.h:173
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:701
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:383
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1320
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:251
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1635
INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic", "Lower atomic intrinsics to non-atomic form", false, false) Pass *llvm
BinOp getOperation() const
Definition: Instructions.h:780
Value * CreateFSub(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1410
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
*p = old <unsigned v ? old : v
Definition: Instructions.h:735
*p = old <signed v ? old : v
Definition: Instructions.h:731
static bool LowerStoreInst(StoreInst *SI)
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2435
An instruction for storing to memory.
Definition: Instructions.h:302
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:486
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1187
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1563
static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI)
Definition: LowerAtomic.cpp:43
*p = old >unsigned v ? old : v
Definition: Instructions.h:733
void initializeLowerAtomicLegacyPassPass(PassRegistry &)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1170
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2180
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1372
assume Assume Builder
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1385
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:1606
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1665
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * getValOperand()
Definition: Instructions.h:845
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:920
Pass * createLowerAtomicPass()
*p = old >signed v ? old : v
Definition: Instructions.h:729
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1346
Value * getPointerOperand()
Definition: Instructions.h:841
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2196
LLVM Value Representation.
Definition: Value.h:74
A container for analyses that lazily runs them and caches their results.
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Definition: LowerAtomic.cpp:24