LLVM  16.0.0git
LowerAtomicPass.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"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "loweratomic"
24 
25 static bool LowerFenceInst(FenceInst *FI) {
26  FI->eraseFromParent();
27  return true;
28 }
29 
30 static bool LowerLoadInst(LoadInst *LI) {
32  return true;
33 }
34 
35 static bool LowerStoreInst(StoreInst *SI) {
36  SI->setAtomic(AtomicOrdering::NotAtomic);
37  return true;
38 }
39 
40 static bool runOnBasicBlock(BasicBlock &BB) {
41  bool Changed = false;
42  for (Instruction &Inst : make_early_inc_range(BB)) {
43  if (FenceInst *FI = dyn_cast<FenceInst>(&Inst))
44  Changed |= LowerFenceInst(FI);
45  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&Inst))
46  Changed |= lowerAtomicCmpXchgInst(CXI);
47  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&Inst))
48  Changed |= lowerAtomicRMWInst(RMWI);
49  else if (LoadInst *LI = dyn_cast<LoadInst>(&Inst)) {
50  if (LI->isAtomic())
51  LowerLoadInst(LI);
52  } else if (StoreInst *SI = dyn_cast<StoreInst>(&Inst)) {
53  if (SI->isAtomic())
55  }
56  }
57  return Changed;
58 }
59 
60 static bool lowerAtomics(Function &F) {
61  bool Changed = false;
62  for (BasicBlock &BB : F) {
63  Changed |= runOnBasicBlock(BB);
64  }
65  return Changed;
66 }
67 
69  if (lowerAtomics(F))
70  return PreservedAnalyses::none();
71  return PreservedAnalyses::all();
72 }
73 
74 namespace {
75 class LowerAtomicLegacyPass : public FunctionPass {
76 public:
77  static char ID;
78 
79  LowerAtomicLegacyPass() : FunctionPass(ID) {
81  }
82 
83  bool runOnFunction(Function &F) override {
84  // Don't skip optnone functions; atomics still need to be lowered.
85  FunctionAnalysisManager DummyFAM;
86  auto PA = Impl.run(F, DummyFAM);
87  return !PA.areAllPreserved();
88  }
89 
90 private:
91  LowerAtomicPass Impl;
92  };
93 }
94 
96 INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
97  "Lower atomic intrinsics to non-atomic form", false, false)
98 
99 Pass *llvm::createLowerAtomicPass() { return new LowerAtomicLegacyPass(); }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
llvm::LowerAtomicPass
A pass that lowers atomic intrinsic into non-atomic intrinsics.
Definition: LowerAtomicPass.h:22
Scalar.h
llvm::Function
Definition: Function.h:60
llvm::initializeLowerAtomicLegacyPassPass
void initializeLowerAtomicLegacyPassPass(PassRegistry &)
Pass.h
LowerAtomic.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
llvm::FenceInst
An instruction for ordering other memory operations.
Definition: Instructions.h:432
LowerFenceInst
static bool LowerFenceInst(FenceInst *FI)
Definition: LowerAtomicPass.cpp:25
LowerStoreInst
static bool LowerStoreInst(StoreInst *SI)
Definition: LowerAtomicPass.cpp:35
lowerAtomics
static bool lowerAtomics(Function &F)
Definition: LowerAtomicPass.cpp:60
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
SI
@ SI
Definition: SIInstrInfo.cpp:7882
llvm::Instruction
Definition: Instruction.h:42
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:297
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:81
llvm::createLowerAtomicPass
Pass * createLowerAtomicPass()
llvm::make_early_inc_range
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:716
llvm::lowerAtomicRMWInst
bool lowerAtomicRMWInst(AtomicRMWInst *RMWI)
Convert the given RMWI into primitive load and stores, assuming that doing so is legal.
Definition: LowerAtomic.cpp:86
IRBuilder.h
llvm::lowerAtomicCmpXchgInst
bool lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Convert the given Cmpxchg into primitive load and compare.
Definition: LowerAtomic.cpp:23
LowerAtomicPass.h
runOnBasicBlock
static bool runOnBasicBlock(BasicBlock &BB)
Definition: LowerAtomicPass.cpp:40
llvm::LoadInst::setAtomic
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:246
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:173
llvm::AtomicRMWInst
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:714
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:85
llvm::LowerAtomicPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Definition: LowerAtomicPass.cpp:68
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
Function.h
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
INITIALIZE_PASS
INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic", "Lower atomic intrinsics to non-atomic form", false, false) Pass *llvm
Definition: LowerAtomicPass.cpp:96
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
InitializePasses.h
llvm::AtomicCmpXchgInst
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:509
LowerLoadInst
static bool LowerLoadInst(LoadInst *LI)
Definition: LowerAtomicPass.cpp:30
llvm::AtomicOrdering::NotAtomic
@ NotAtomic