LLVM 20.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"
17#include "llvm/Pass.h"
20using namespace llvm;
21
22#define DEBUG_TYPE "lower-atomic"
23
24static bool LowerFenceInst(FenceInst *FI) {
25 FI->eraseFromParent();
26 return true;
27}
28
29static bool LowerLoadInst(LoadInst *LI) {
30 LI->setAtomic(AtomicOrdering::NotAtomic);
31 return true;
32}
33
34static bool LowerStoreInst(StoreInst *SI) {
35 SI->setAtomic(AtomicOrdering::NotAtomic);
36 return true;
37}
38
39static bool runOnBasicBlock(BasicBlock &BB) {
40 bool Changed = false;
41 for (Instruction &Inst : make_early_inc_range(BB)) {
42 if (FenceInst *FI = dyn_cast<FenceInst>(&Inst))
43 Changed |= LowerFenceInst(FI);
44 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&Inst))
45 Changed |= lowerAtomicCmpXchgInst(CXI);
46 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&Inst))
47 Changed |= lowerAtomicRMWInst(RMWI);
48 else if (LoadInst *LI = dyn_cast<LoadInst>(&Inst)) {
49 if (LI->isAtomic())
50 LowerLoadInst(LI);
51 } else if (StoreInst *SI = dyn_cast<StoreInst>(&Inst)) {
52 if (SI->isAtomic())
54 }
55 }
56 return Changed;
57}
58
59static bool lowerAtomics(Function &F) {
60 bool Changed = false;
61 for (BasicBlock &BB : F) {
62 Changed |= runOnBasicBlock(BB);
63 }
64 return Changed;
65}
66
68 if (lowerAtomics(F))
71}
72
73namespace {
74class LowerAtomicLegacyPass : public FunctionPass {
75public:
76 static char ID;
77
78 LowerAtomicLegacyPass() : FunctionPass(ID) {
80 }
81
82 bool runOnFunction(Function &F) override {
83 // Don't skip optnone functions; atomics still need to be lowered.
85 auto PA = Impl.run(F, DummyFAM);
86 return !PA.areAllPreserved();
87 }
88
89private:
90 LowerAtomicPass Impl;
91 };
92}
93
94char LowerAtomicLegacyPass::ID = 0;
95INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
96 "Lower atomic intrinsics to non-atomic form", false, false)
97
98Pass *llvm::createLowerAtomicPass() { return new LowerAtomicLegacyPass(); }
static bool runOnFunction(Function &F, bool PostInlining)
static bool LowerStoreInst(StoreInst *SI)
static bool LowerLoadInst(LoadInst *LI)
static bool LowerFenceInst(FenceInst *FI)
static bool runOnBasicBlock(BasicBlock &BB)
static bool lowerAtomics(Function &F)
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:501
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:704
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
An instruction for ordering other memory operations.
Definition: Instructions.h:424
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
An instruction for reading from memory.
Definition: Instructions.h:176
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:241
A pass that lowers atomic intrinsic into non-atomic intrinsics.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
An instruction for storing to memory.
Definition: Instructions.h:292
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:657
Pass * createLowerAtomicPass()
bool lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Convert the given Cmpxchg into primitive load and compare.
Definition: LowerAtomic.cpp:22
bool lowerAtomicRMWInst(AtomicRMWInst *RMWI)
Convert the given RMWI into primitive load and stores, assuming that doing so is legal.
void initializeLowerAtomicLegacyPassPass(PassRegistry &)