LLVM 20.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 {
32
33class RISCVZacasABIFix : public FunctionPass,
34 public InstVisitor<RISCVZacasABIFix, bool> {
35 const RISCVSubtarget *ST;
36
37public:
38 static char ID;
39
40 RISCVZacasABIFix() : FunctionPass(ID) {}
41
42 bool runOnFunction(Function &F) override;
43
44 StringRef getPassName() const override { return PASS_NAME; }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.setPreservesCFG();
49 }
50
51 bool visitInstruction(Instruction &I) { return false; }
53};
54
55} // end anonymous namespace
56
57// Insert a leading fence (needed for broadest atomics ABI compatibility)
58// only if the Zacas extension is enabled and the AtomicCmpXchgInst has a
59// SequentiallyConsistent failure ordering.
60bool RISCVZacasABIFix::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
61 assert(ST->hasStdExtZacas() && "only necessary to run in presence of zacas");
62 IRBuilder<> Builder(&I);
63 if (I.getFailureOrdering() != AtomicOrdering::SequentiallyConsistent)
64 return false;
65
66 Builder.CreateFence(AtomicOrdering::SequentiallyConsistent);
67 return true;
68}
69
70bool RISCVZacasABIFix::runOnFunction(Function &F) {
71 auto &TPC = getAnalysis<TargetPassConfig>();
72 auto &TM = TPC.getTM<RISCVTargetMachine>();
73 ST = &TM.getSubtarget<RISCVSubtarget>(F);
74
75 if (skipFunction(F) || !ST->hasStdExtZacas())
76 return false;
77
78 bool MadeChange = false;
79 for (auto &BB : F)
81 MadeChange |= visit(I);
82
83 return MadeChange;
84}
85
86INITIALIZE_PASS_BEGIN(RISCVZacasABIFix, DEBUG_TYPE, PASS_NAME, false, false)
89
90char RISCVZacasABIFix::ID = 0;
91
93 return new RISCVZacasABIFix();
94}
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
#define PASS_NAME
#define DEBUG_TYPE
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> 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
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:501
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
Base class for instruction visitors.
Definition: InstVisitor.h:78
RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I)
Definition: InstVisitor.h:171
void visitInstruction(Instruction &I)
Definition: InstVisitor.h:283
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target-Independent Code Generator Pass Configuration Options.
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
FunctionPass * createRISCVZacasABIFixPass()