LLVM 24.0.0git
RISCVZacasABIFix.cpp
Go to the documentation of this file.
1//===----- RISCVZacasABIFix.cpp -------------------------------------------===//
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 implements a fence insertion for an atomic cmpxchg in a case that
10// isn't easy to do with the current AtomicExpandPass hooks API.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RISCV.h"
15#include "RISCVTargetMachine.h"
16#include "llvm/ADT/Statistic.h"
19#include "llvm/IR/Dominators.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/InstVisitor.h"
22#include "llvm/IR/Intrinsics.h"
24#include "llvm/Pass.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "riscv-zacas-abi-fix"
29#define PASS_NAME "RISC-V Zacas ABI fix"
30
31namespace {
32class RISCVZacasABIFixImpl : public InstVisitor<RISCVZacasABIFixImpl, bool> {
33 const RISCVSubtarget *ST;
34
35public:
36 RISCVZacasABIFixImpl(const RISCVSubtarget *ST) : ST(ST) {}
37 bool run(Function &F);
38 bool visitInstruction(Instruction &I) { return false; }
39 bool visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
40};
41} // namespace
42
43namespace {
44class RISCVZacasABIFixLegacy : public FunctionPass {
45public:
46 static char ID;
47
48 RISCVZacasABIFixLegacy() : FunctionPass(ID) {}
49
50 bool runOnFunction(Function &F) override;
51
52 StringRef getPassName() const override { return PASS_NAME; }
53
54 void getAnalysisUsage(AnalysisUsage &AU) const override {
55 AU.setPreservesCFG();
56 AU.addRequired<TargetPassConfig>();
57 }
58};
59} // namespace
60
61// Insert a leading fence (needed for broadest atomics ABI compatibility)
62// only if the Zacas extension is enabled and the AtomicCmpXchgInst has a
63// SequentiallyConsistent failure ordering.
64bool RISCVZacasABIFixImpl::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
65 assert(ST->hasStdExtZacas() && "only necessary to run in presence of zacas");
66 IRBuilder<> Builder(&I);
67 if (I.getFailureOrdering() != AtomicOrdering::SequentiallyConsistent)
68 return false;
69
70 Builder.CreateFence(AtomicOrdering::SequentiallyConsistent);
71 return true;
72}
73
74bool RISCVZacasABIFixImpl::run(Function &F) {
75 if (!ST->hasStdExtZacas())
76 return false;
77
78 bool MadeChange = false;
79 for (auto &BB : F)
80 for (Instruction &I : llvm::make_early_inc_range(BB))
81 MadeChange |= visit(I);
82
83 return MadeChange;
84}
85
86bool RISCVZacasABIFixLegacy::runOnFunction(Function &F) {
87 auto &TPC = getAnalysis<TargetPassConfig>();
88 auto &TM = TPC.getTM<RISCVTargetMachine>();
89 auto *ST = &TM.getSubtarget<RISCVSubtarget>(F);
90
91 if (skipFunction(F))
92 return false;
93
94 return RISCVZacasABIFixImpl(ST).run(F);
95}
96
97INITIALIZE_PASS_BEGIN(RISCVZacasABIFixLegacy, DEBUG_TYPE, PASS_NAME, false,
98 false)
99INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
100INITIALIZE_PASS_END(RISCVZacasABIFixLegacy, DEBUG_TYPE, PASS_NAME, false, false)
101
102char RISCVZacasABIFixLegacy::ID = 0;
103
104FunctionPass *llvm::createRISCVZacasABIFixPass() {
105 return new RISCVZacasABIFixLegacy();
106}
107
110 auto *ST = &TM->getSubtarget<RISCVSubtarget>(F);
111
112 bool Changed = RISCVZacasABIFixImpl(ST).run(F);
113 if (!Changed)
114 return PreservedAnalyses::all();
115
117}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define PASS_NAME
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Target-Independent Code Generator Pass Configuration Options pass.
#define PASS_NAME
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
An instruction that atomically checks whether a specified value is in a memory location,...
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Base class for instruction visitors.
Definition InstVisitor.h:78
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
static PreservedAnalyses allInSet()
Construct a preserved analyses object with a single preserved set.
Definition Analysis.h:125
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Changed
This is an optimization pass for GlobalISel generic memory operations.
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
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
FunctionPass * createRISCVZacasABIFixPass()