LLVM 24.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) {
31 LI->setElementwise(false);
32 return true;
33}
34
36 SI->setAtomic(AtomicOrdering::NotAtomic);
37 return true;
38}
39
40static 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))
47 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&Inst))
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
60static bool lowerAtomics(Function &F) {
61 bool Changed = false;
62 for (BasicBlock &BB : F) {
64 }
65 return Changed;
66}
67
73
74namespace {
75class LowerAtomicLegacyPass : public FunctionPass {
76public:
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.
86 auto PA = Impl.run(F, DummyFAM);
87 return !PA.areAllPreserved();
88 }
89
90private:
91 LowerAtomicPass Impl;
92 };
93}
94
95char LowerAtomicLegacyPass::ID = 0;
96INITIALIZE_PASS(LowerAtomicLegacyPass, "loweratomic",
97 "Lower atomic intrinsics to non-atomic form", false, false)
98
99Pass *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:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
LLVM Basic Block Representation.
Definition BasicBlock.h:62
An instruction for ordering other memory operations.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
An instruction for reading from memory.
void setElementwise(bool V)
Specify whether this is an elementwise atomic load or not.
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this load instruction.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
An instruction for storing to memory.
Changed
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void initializeLowerAtomicLegacyPassPass(PassRegistry &)
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:633
LLVM_ABI Pass * createLowerAtomicPass()
LLVM_ABI bool lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Convert the given Cmpxchg into primitive load and compare.
LLVM_ABI bool lowerAtomicRMWInst(AtomicRMWInst *RMWI)
Convert the given RMWI into primitive load and stores, assuming that doing so is legal.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.