LLVM  6.0.0svn
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/IR/IntrinsicInst.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Transforms/Scalar.h"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "loweratomic"
24 
26  IRBuilder<> Builder(CXI);
27  Value *Ptr = CXI->getPointerOperand();
28  Value *Cmp = CXI->getCompareOperand();
29  Value *Val = CXI->getNewValOperand();
30 
31  LoadInst *Orig = Builder.CreateLoad(Ptr);
32  Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
33  Value *Res = Builder.CreateSelect(Equal, Val, Orig);
34  Builder.CreateStore(Res, Ptr);
35 
36  Res = Builder.CreateInsertValue(UndefValue::get(CXI->getType()), Orig, 0);
37  Res = Builder.CreateInsertValue(Res, Equal, 1);
38 
39  CXI->replaceAllUsesWith(Res);
40  CXI->eraseFromParent();
41  return true;
42 }
43 
44 static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI) {
45  IRBuilder<> Builder(RMWI);
46  Value *Ptr = RMWI->getPointerOperand();
47  Value *Val = RMWI->getValOperand();
48 
49  LoadInst *Orig = Builder.CreateLoad(Ptr);
50  Value *Res = nullptr;
51 
52  switch (RMWI->getOperation()) {
53  default: llvm_unreachable("Unexpected RMW operation");
55  Res = Val;
56  break;
57  case AtomicRMWInst::Add:
58  Res = Builder.CreateAdd(Orig, Val);
59  break;
60  case AtomicRMWInst::Sub:
61  Res = Builder.CreateSub(Orig, Val);
62  break;
63  case AtomicRMWInst::And:
64  Res = Builder.CreateAnd(Orig, Val);
65  break;
67  Res = Builder.CreateNot(Builder.CreateAnd(Orig, Val));
68  break;
69  case AtomicRMWInst::Or:
70  Res = Builder.CreateOr(Orig, Val);
71  break;
72  case AtomicRMWInst::Xor:
73  Res = Builder.CreateXor(Orig, Val);
74  break;
75  case AtomicRMWInst::Max:
76  Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
77  Val, Orig);
78  break;
79  case AtomicRMWInst::Min:
80  Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
81  Orig, Val);
82  break;
84  Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
85  Val, Orig);
86  break;
88  Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
89  Orig, Val);
90  break;
91  }
92  Builder.CreateStore(Res, Ptr);
93  RMWI->replaceAllUsesWith(Orig);
94  RMWI->eraseFromParent();
95  return true;
96 }
97 
98 static bool LowerFenceInst(FenceInst *FI) {
99  FI->eraseFromParent();
100  return true;
101 }
102 
103 static bool LowerLoadInst(LoadInst *LI) {
105  return true;
106 }
107 
108 static bool LowerStoreInst(StoreInst *SI) {
110  return true;
111 }
112 
113 static bool runOnBasicBlock(BasicBlock &BB) {
114  bool Changed = false;
115  for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE;) {
116  Instruction *Inst = &*DI++;
117  if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
118  Changed |= LowerFenceInst(FI);
119  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Inst))
120  Changed |= LowerAtomicCmpXchgInst(CXI);
121  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Inst))
122  Changed |= LowerAtomicRMWInst(RMWI);
123  else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
124  if (LI->isAtomic())
125  LowerLoadInst(LI);
126  } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
127  if (SI->isAtomic())
129  }
130  }
131  return Changed;
132 }
133 
134 static bool lowerAtomics(Function &F) {
135  bool Changed = false;
136  for (BasicBlock &BB : F) {
137  Changed |= runOnBasicBlock(BB);
138  }
139  return Changed;
140 }
141 
143  if (lowerAtomics(F))
144  return PreservedAnalyses::none();
145  return PreservedAnalyses::all();
146 }
147 
148 namespace {
149 class LowerAtomicLegacyPass : public FunctionPass {
150 public:
151  static char ID;
152 
153  LowerAtomicLegacyPass() : FunctionPass(ID) {
155  }
156 
157  bool runOnFunction(Function &F) override {
158  // Don't skip optnone functions; atomics still need to be lowered.
159  FunctionAnalysisManager DummyFAM;
160  auto PA = Impl.run(F, DummyFAM);
161  return !PA.areAllPreserved();
162  }
163 
164 private:
165  LowerAtomicPass Impl;
166  };
167 }
168 
170 INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
171  "Lower atomic intrinsics to non-atomic form", false, false)
172 
173 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:69
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
*p = old <signed v ? old : v
Definition: Instructions.h:707
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1558
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1095
An instruction for ordering other memory operations.
Definition: Instructions.h:440
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:514
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1570
static bool runOnBasicBlock(BasicBlock &BB)
*p = old <unsigned v ? old : v
Definition: Instructions.h:711
static bool lowerAtomics(Function &F)
static bool LowerLoadInst(LoadInst *LI)
*p = old >unsigned v ? old : v
Definition: Instructions.h:709
F(f)
An instruction for reading from memory.
Definition: Instructions.h:164
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:677
static bool LowerFenceInst(FenceInst *FI)
Definition: LowerAtomic.cpp:98
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:381
*p = old >signed v ? old : v
Definition: Instructions.h:705
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:252
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1142
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:256
INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic", "Lower atomic intrinsics to non-atomic form", false, false) Pass *llvm
BinOp getOperation() const
Definition: Instructions.h:730
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:664
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:889
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1176
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:911
An instruction for storing to memory.
Definition: Instructions.h:306
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:428
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:1079
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Definition: IRBuilder.h:1164
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:59
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:1689
static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI)
Definition: LowerAtomic.cpp:44
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:1546
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1320
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:783
Iterator for intrusive lists based on ilist_node.
iterator end()
Definition: BasicBlock.h:254
Pass * createLowerAtomicPass()
Value * getPointerOperand()
Definition: Instructions.h:779
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1063
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:1759
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Definition: LowerAtomic.cpp:25