32#define DEBUG_TYPE "loongarch-memory-barrier-opt"
33#define LOONGARCH_MEMORY_BARRIER_OPT_NAME \
34 "LoongArch Memory Barrier Optimisation pass"
37 "loongarch-require-no-path-bypass",
38 cl::desc(
"Optimize only when no paths bypass either memory barrier"),
42 "loongarch-merge-amo-with-dbar",
43 cl::desc(
"Merge AMOs with DBARs into AMO_DB during optimization"),
47 "loongarch-replace-eliminated-dbar-to-nop",
48 cl::desc(
"Replace eliminated DBARs with NOPs to preserve code layout"),
82 return MI.getOpcode() == LoongArch::DBAR;
86 switch (
MI.getOpcode()) {
96 switch (
MI.getOpcode()) {
107#define CASE(Name, Suffix) \
108 case LoongArch::Name##_##Suffix: \
109 if (!MergeAMOWithMB) \
110 return std::nullopt; \
112 case LoongArch::Name##__DB_##Suffix: \
113 return LoongArch::Name##__DB_##Suffix;
114 switch (
MI.getOpcode()) {
123 if (
MI.mayLoadOrStore())
125 if (
MI.isCall() ||
MI.isReturn())
127 if (
MI.isInlineAsm())
129 if (
MI.hasUnmodeledSideEffects())
135 BarrierHint(
unsigned Hint) : Hint(Hint) {}
137 bool subsumes(
const BarrierHint &O)
const {
return (Hint &
O.Hint) == Hint; }
139 BarrierHint
merge(
const BarrierHint &O)
const {
140 return BarrierHint(Hint &
O.Hint);
143 static inline bool isValid(
unsigned Hint) {
return (Hint & ~0x1f) == 0; }
149 InstBarrier(MachineInstr &MI)
150 : MI(&MI), Pre(0), Post(0), OpcAMDB(0), IsMB(
false), IsAM(
false) {
152 unsigned Hint = MI.getOperand(0).getImm();
153 if (!BarrierHint::isValid(Hint))
156 Pre = Post = BarrierHint(Hint);
157 }
else if (isLL(MI)) {
159 Pre = BarrierHint(0b10000);
160 Post = BarrierHint(0b11111);
161 }
else if (isSC(MI)) {
163 Pre = BarrierHint(0b11111);
164 Post = BarrierHint(0b10000);
165 }
else if (
auto R = isAM(MI)) {
168 Pre = Post = BarrierHint(0b10000);
186 LoongArchMemoryBarrierOptLegacy() : MachineFunctionPass(ID) {}
188 StringRef getPassName()
const override {
192 void getAnalysisUsage(AnalysisUsage &AU)
const override {
195 AU.
addRequired<MachinePostDominatorTreeWrapperPass>();
203 CandidateA = 1u << 0,
204 CandidateB = 1u << 1,
220 while (!Worklist.
empty()) {
228 if (!isSafeToSkip(
MI))
235 if (Visited.
insert(Succ).second)
259 for (
auto It = std::next(
A->getIterator()); It != MBBA->
end(); ++It) {
260 if (It ==
B->getIterator())
261 return CandidateA | CandidateB;
262 if (!isSafeToSkip(*It))
270 bool BPostDomA = MPDT->
dominates(MBBB, MBBA);
272 if (!ADomB && !BPostDomA)
276 for (
auto It = std::next(
A->getIterator()); It != MBBA->
end(); ++It)
277 if (!isSafeToSkip(*It))
280 for (
auto It = MBBB->
begin(); It !=
B->getIterator(); ++It)
281 if (!isSafeToSkip(*It))
299 assert(
I.IsMB &&
"Unexpected!");
300 I.Pre =
I.Post = Hint;
301 I.MI->getOperand(0).setImm(Hint.Hint);
310 I.MI->setDesc(ST.getInstrInfo()->get(
I.OpcAMDB));
333 BuildMI(*
MI->getParent(),
MI->getIterator(),
MI->getDebugLoc(),
334 ST.getInstrInfo()->get(LoongArch::ANDI), LoongArch::R0)
338 MI->eraseFromParent();
346 if ((Mask & CandidateA) && IA.IsMB) {
347 if (!IB.Pre.subsumes(IA.Post)) {
350 updateMB(IB, IB.Pre.merge(IA.Post));
353 eraseOrReplaceWithNop(
A);
363 if ((Mask & CandidateB) && IB.IsMB) {
364 if (!IA.Post.subsumes(IB.Pre)) {
367 updateMB(IA, IA.Post.merge(IB.Pre));
370 eraseOrReplaceWithNop(
B);
387 if (IB.IsMB || IB.IsAM)
391 for (
size_t a = 0; a < Sites.
size(); ++a) {
392 for (
size_t b = a + 1; b < Sites.
size(); ++b) {
393 InstBarrier &IA = Sites[a];
394 InstBarrier &IB = Sites[b];
403bool LoongArchMemoryBarrierOptLegacy::runOnMachineFunction(
408 MF, &getAnalysis<MachineDominatorTreeWrapperPass>().
getDomTree(),
409 &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree());
412char LoongArchMemoryBarrierOptLegacy::ID = 0;
421 return new LoongArchMemoryBarrierOptLegacy();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void replaceAM(InstBarrier &I, MachineFunction &MF)
static bool eliminateRedundantBarrier(InstBarrier &IA, InstBarrier &IB, MachineFunction &MF, const MachineDominatorTree *MDT, const MachinePostDominatorTree *MPDT)
static cl::opt< bool > RequireNoPathBypass("loongarch-require-no-path-bypass", cl::desc("Optimize only when no paths bypass either memory barrier"), cl::init(true), cl::Hidden)
static unsigned resolveBarrierRedundancy(const MachineInstr *A, const MachineInstr *B, const MachineDominatorTree *MDT, const MachinePostDominatorTree *MPDT)
static bool checkAllPathSafe(const MachineBasicBlock *MBBA, const MachineBasicBlock *MBBB, bool IsAToB)
static bool runMemoryBarrierOpt(MachineFunction &MF, const MachineDominatorTree *MDT, const MachinePostDominatorTree *MPDT)
#define LOONGARCH_MEMORY_BARRIER_OPT_NAME
static cl::opt< bool > ReplaceEliminatedMBToNop("loongarch-replace-eliminated-dbar-to-nop", cl::desc("Replace eliminated DBARs with NOPs to preserve code layout"), cl::init(false), cl::Hidden)
static void updateMB(InstBarrier &I, BarrierHint Hint)
static cl::opt< bool > MergeAMOWithMB("loongarch-merge-amo-with-dbar", cl::desc("Merge AMOs with DBARs into AMO_DB during optimization"), cl::init(true), cl::Hidden)
static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
static DominatorTree getDomTree(Function &F)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
Implements a dense probed hash-table based set.
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
FunctionPass class - This class is used to implement most global optimizations.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::pair< iterator, bool > insert(const ValueT &V)
Pass manager infrastructure for declaring and invalidating analyses.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createLoongArchMemoryBarrierOptLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
@ End
Run the machine verifier once, at the end of the pipeline.