LLVM  8.0.1
LowerAtomic.cpp
Go to the documentation of this file.
1 //===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass lowers atomic intrinsics to non-atomic form for use in a known
11 // non-preemptible environment.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/IRBuilder.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(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(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;
90  }
91  Builder.CreateStore(Res, Ptr);
92  RMWI->replaceAllUsesWith(Orig);
93  RMWI->eraseFromParent();
94  return true;
95 }
96 
97 static bool LowerFenceInst(FenceInst *FI) {
98  FI->eraseFromParent();
99  return true;
100 }
101 
102 static bool LowerLoadInst(LoadInst *LI) {
104  return true;
105 }
106 
107 static bool LowerStoreInst(StoreInst *SI) {
109  return true;
110 }
111 
112 static bool runOnBasicBlock(BasicBlock &BB) {
113  bool Changed = false;
114  for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE;) {
115  Instruction *Inst = &*DI++;
116  if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
117  Changed |= LowerFenceInst(FI);
118  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Inst))
119  Changed |= LowerAtomicCmpXchgInst(CXI);
120  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Inst))
121  Changed |= LowerAtomicRMWInst(RMWI);
122  else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
123  if (LI->isAtomic())
124  LowerLoadInst(LI);
125  } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
126  if (SI->isAtomic())
128  }
129  }
130  return Changed;
131 }
132 
133 static bool lowerAtomics(Function &F) {
134  bool Changed = false;
135  for (BasicBlock &BB : F) {
136  Changed |= runOnBasicBlock(BB);
137  }
138  return Changed;
139 }
140 
142  if (lowerAtomics(F))
143  return PreservedAnalyses::none();
144  return PreservedAnalyses::all();
145 }
146 
147 namespace {
148 class LowerAtomicLegacyPass : public FunctionPass {
149 public:
150  static char ID;
151 
152  LowerAtomicLegacyPass() : FunctionPass(ID) {
154  }
155 
156  bool runOnFunction(Function &F) override {
157  // Don't skip optnone functions; atomics still need to be lowered.
158  FunctionAnalysisManager DummyFAM;
159  auto PA = Impl.run(F, DummyFAM);
160  return !PA.areAllPreserved();
161  }
162 
163 private:
164  LowerAtomicPass Impl;
165  };
166 }
167 
169 INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
170  "Lower atomic intrinsics to non-atomic form", false, false)
171 
172 Pass *llvm::createLowerAtomicPass() { return new LowerAtomicLegacyPass(); }
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
*p = old <signed v ? old : v
Definition: Instructions.h:722
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1855
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:1357
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1200
An instruction for ordering other memory operations.
Definition: Instructions.h:455
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:529
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1871
static bool runOnBasicBlock(BasicBlock &BB)
*p = old <unsigned v ? old : v
Definition: Instructions.h:726
static bool lowerAtomics(Function &F)
static bool LowerLoadInst(LoadInst *LI)
*p = old >unsigned v ? old : v
Definition: Instructions.h:724
F(f)
An instruction for reading from memory.
Definition: Instructions.h:168
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:692
static bool LowerFenceInst(FenceInst *FI)
Definition: LowerAtomic.cpp:97
A pass that lowers atomic intrinsic into non-atomic intrinsics.
Definition: LowerAtomic.h:23
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:396
*p = old >signed v ? old : v
Definition: Instructions.h:720
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:269
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1334
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:271
INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic", "Lower atomic intrinsics to non-atomic form", false, false) Pass *llvm
BinOp getOperation() const
Definition: Instructions.h:745
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1014
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1386
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
static bool LowerStoreInst(StoreInst *SI)
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1031
An instruction for storing to memory.
Definition: Instructions.h:321
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:157
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1182
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
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:2021
static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI)
Definition: LowerAtomic.cpp:43
void initializeLowerAtomicLegacyPassPass(PassRegistry &)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1839
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1415
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:800
Iterator for intrusive lists based on ilist_node.
iterator end()
Definition: BasicBlock.h:271
Pass * createLowerAtomicPass()
Value * getPointerOperand()
Definition: Instructions.h:796
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1164
LLVM Value Representation.
Definition: Value.h:73
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:2091
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Definition: LowerAtomic.cpp:24