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;
89  }
90  Builder.CreateStore(Res, Ptr);
91  RMWI->replaceAllUsesWith(Orig);
92  RMWI->eraseFromParent();
93  return true;
94 }
95 
96 static bool LowerFenceInst(FenceInst *FI) {
97  FI->eraseFromParent();
98  return true;
99 }
100 
101 static bool LowerLoadInst(LoadInst *LI) {
103  return true;
104 }
105 
106 static bool LowerStoreInst(StoreInst *SI) {
108  return true;
109 }
110 
111 static bool runOnBasicBlock(BasicBlock &BB) {
112  bool Changed = false;
113  for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE;) {
114  Instruction *Inst = &*DI++;
115  if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
116  Changed |= LowerFenceInst(FI);
117  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Inst))
118  Changed |= LowerAtomicCmpXchgInst(CXI);
119  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Inst))
120  Changed |= LowerAtomicRMWInst(RMWI);
121  else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
122  if (LI->isAtomic())
123  LowerLoadInst(LI);
124  } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
125  if (SI->isAtomic())
127  }
128  }
129  return Changed;
130 }
131 
132 static bool lowerAtomics(Function &F) {
133  bool Changed = false;
134  for (BasicBlock &BB : F) {
135  Changed |= runOnBasicBlock(BB);
136  }
137  return Changed;
138 }
139 
141  if (lowerAtomics(F))
142  return PreservedAnalyses::none();
143  return PreservedAnalyses::all();
144 }
145 
146 namespace {
147 class LowerAtomicLegacyPass : public FunctionPass {
148 public:
149  static char ID;
150 
151  LowerAtomicLegacyPass() : FunctionPass(ID) {
153  }
154 
155  bool runOnFunction(Function &F) override {
156  // Don't skip optnone functions; atomics still need to be lowered.
157  FunctionAnalysisManager DummyFAM;
158  auto PA = Impl.run(F, DummyFAM);
159  return !PA.areAllPreserved();
160  }
161 
162 private:
163  LowerAtomicPass Impl;
164  };
165 }
166 
168 INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
169  "Lower atomic intrinsics to non-atomic form", false, false)
170 
171 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:721
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1890
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:1392
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:528
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1906
static bool runOnBasicBlock(BasicBlock &BB)
*p = old <unsigned v ? old : v
Definition: Instructions.h:725
static bool lowerAtomics(Function &F)
static bool LowerLoadInst(LoadInst *LI)
*p = old >unsigned v ? old : v
Definition: Instructions.h:723
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:691
static bool LowerFenceInst(FenceInst *FI)
Definition: LowerAtomic.cpp:96
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:719
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:268
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1369
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:750
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:1421
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:2057
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:1874
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:815
Iterator for intrusive lists based on ilist_node.
iterator end()
Definition: BasicBlock.h:270
Pass * createLowerAtomicPass()
Value * getPointerOperand()
Definition: Instructions.h:811
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:2127
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Definition: LowerAtomic.cpp:23