LLVM 24.0.0git
LeonPasses.cpp
Go to the documentation of this file.
1//===------ LeonPasses.cpp - Define passes specific to LEON ---------------===//
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//
10//===----------------------------------------------------------------------===//
11
12#include "LeonPasses.h"
13#include "Sparc.h"
14#include "SparcSubtarget.h"
23#include "llvm/IR/Analysis.h"
24
25using namespace llvm;
26
28
33
35 "Errata workaround pass", false, false)
36
37// Move iterator to the next instruction in the function, ignoring
38// meta instructions and inline assembly. Returns false when reaching
39// the end of the function.
40static bool moveNext(MachineBasicBlock::iterator &I) {
41
42 MachineBasicBlock *MBB = I->getParent();
43
44 do {
45 I++;
46
47 while (I == MBB->end()) {
48 if (MBB->getFallThrough() == nullptr)
49 return false;
50 MBB = MBB->getFallThrough();
51 I = MBB->begin();
52 }
53 } while (I->isMetaInstruction() || I->isInlineAsm());
54
55 return true;
56}
57
59 const TargetInstrInfo *TII) {
60 BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(SP::NOP));
61}
62
64 if (I->getNumOperands() == 0)
65 return false;
66
67 if (!I->getOperand(0).isReg())
68 return false;
69
70 unsigned reg = I->getOperand(0).getReg();
71
72 if (!SP::FPRegsRegClass.contains(reg) && !SP::DFPRegsRegClass.contains(reg))
73 return false;
74
75 return true;
76}
77
79 switch (I->getOpcode()) {
80 case SP::FDIVS:
81 case SP::FDIVD:
82 case SP::FSQRTS:
83 case SP::FSQRTD:
84 return true;
85 }
86 return false;
87}
88
90 switch (I->getOpcode()) {
91 case SP::SWAPrr:
92 case SP::SWAPri:
93 case SP::CASArr:
94 case SP::LDSTUBrr:
95 case SP::LDSTUBri:
96 case SP::LDSTUBArr:
97 return true;
98 }
99 return false;
100}
101
102// Prevents the following code sequence from being generated:
103// (stb/sth/st/stf) -> (single non-store/load instruction) -> (any store)
104// If the sequence is detected a NOP instruction is inserted after
105// the first store instruction.
107 const TargetInstrInfo *TII) {
108 switch (I->getOpcode()) {
109 case SP::STrr:
110 case SP::STri:
111 case SP::STBrr:
112 case SP::STBri:
113 case SP::STHrr:
114 case SP::STHri:
115 case SP::STFrr:
116 case SP::STFri:
117 break;
118 default:
119 return false;
120 }
121
123 if (!moveNext(MI))
124 return false;
125
126 if (MI->mayStore() || MI->mayLoad())
127 return false;
128
130
131 if (!moveNext(MI))
132 return false;
133
134 if (!MI->mayStore())
135 return false;
136
137 insertNop(PatchHere, TII);
138 return true;
139}
140
141// Prevents the following code sequence from being generated:
142// (std/stdf) -> (any store)
143// If the sequence is detected a NOP instruction is inserted after
144// the first store instruction.
146 const TargetInstrInfo *TII) {
147
148 switch (I->getOpcode()) {
149 case SP::STDrr:
150 case SP::STDri:
151 case SP::STDFrr:
152 case SP::STDFri:
153 break;
154 default:
155 return false;
156 }
157
159
160 if (!moveNext(MI))
161 return false;
162
163 if (!MI->mayStore())
164 return false;
165
166 insertNop(MI, TII);
167 return true;
168}
169
170// Insert a NOP at branch target if load in delay slot and atomic
171// instruction at branch target. Also insert a NOP between load
172// instruction and atomic instruction (swap or casa).
174 const TargetInstrInfo *TII) {
175
176 // Check for load instruction or branch bundled with load instruction
177 if (!I->mayLoad())
178 return false;
179
180 // Check for branch to atomic instruction with load in delay slot
181 if (I->isBranch() && I->getOperand(0).isMBB()) {
182 MachineBasicBlock *TargetMBB = I->getOperand(0).getMBB();
184 skipMetaInstructionsForward(TargetMBB->begin(), TargetMBB->end());
185
186 if (MI == TargetMBB->end())
187 return false;
188
189 if (isAtomic(MI))
190 insertNop(MI, TII);
191 }
192
193 // Check for load followed by atomic instruction
195 if (!moveNext(MI))
196 return false;
197
198 if (!isAtomic(MI))
199 return false;
200 insertNop(MI, TII);
201 return true;
202}
203
204// Do not allow functions to begin with an atomic instruction
206 const TargetInstrInfo *TII) {
208 skipMetaInstructionsForward(MBB.begin(), MBB.end());
209 if (I == MBB.end())
210 return false;
211 if (!isAtomic(I))
212 return false;
213 insertNop(I, TII);
214 return true;
215}
216
217// Inserts a NOP instruction at the target of an integer branch if the
218// target is a floating-point instruction or floating-point branch.
220 const TargetInstrInfo *TII) {
221
222 if (I->getOpcode() != SP::BCOND && I->getOpcode() != SP::BCONDA)
223 return false;
224
225 MachineBasicBlock *TargetMBB = I->getOperand(0).getMBB();
227 skipMetaInstructionsForward(TargetMBB->begin(), TargetMBB->end());
228
229 if (MI == TargetMBB->end())
230 return false;
231
232 if (!isFloat(MI) && MI->getOpcode() != SP::FBCOND)
233 return false;
234
235 insertNop(MI, TII);
236 return true;
237}
238
239// Prevents the following code sequence from being generated:
240// (div/sqrt) -> (2 to 3 floating-point operations or loads) -> (div/sqrt)
241// If the sequence is detected one or two NOP instruction are inserted after
242// the first div/sqrt instruction. No NOPs are inserted if one of the floating-
243// point instructions in the middle of the sequence is a (div/sqrt), or if
244// they have dependency on the destination register of the first (div/sqrt).
245//
246// The function also prevents the following code sequence from being generated,
247// (div/sqrt) -> (branch), by inserting a NOP instruction after the (div/sqrt).
249 const TargetInstrInfo *TII,
250 const TargetRegisterInfo *TRI) {
251
252 if (!isDivSqrt(I))
253 return false;
254
255 unsigned dstReg = I->getOperand(0).getReg();
256
258 if (!moveNext(MI))
259 return false;
260
261 if (MI->isBranch()) {
262 insertNop(MI, TII);
263 return true;
264 }
265
267
268 unsigned fpFound = 0;
269 for (unsigned i = 0; i < 4; i++) {
270
271 if (!isFloat(MI)) {
272 if (!moveNext(MI))
273 return false;
274 continue;
275 }
276
277 if (MI->readsRegister(dstReg, TRI))
278 return false;
279
280 if (isDivSqrt(MI)) {
281 if (i < 2)
282 return false;
283 if (fpFound < 2)
284 return false;
285
286 insertNop(PatchHere, TII);
287 if (i == 2)
288 insertNop(PatchHere, TII);
289 return true;
290 }
291
292 fpFound++;
293 if (!moveNext(MI))
294 return false;
295 }
296
297 return false;
298}
299
300// On the AT697E and AT697F, a single-word load into an odd-numbered FP register
301// followed by a double-precision operation on the enclosing register pair can
302// give a wrong result (AT697E erratum 13, AT697F erratum 1). On the UT699, a
303// single-cycle load followed by another load or store can fail.
304//
305// Inserts a NOP after any LD or LDF instruction.
307 bool Modified = false;
308 for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
309 unsigned Opcode = MBBI->getOpcode();
310 if (Opcode >= SP::LDDArr && Opcode <= SP::LDrr) {
311 BuildMI(MBB, std::next(MBBI), DebugLoc(), TII->get(SP::NOP));
312 Modified = true;
313 }
314 }
315 return Modified;
316}
317
319 bool Changed = false;
320 const SparcSubtarget *ST = &MF.getSubtarget<SparcSubtarget>();
321
322 if (!(ST->insertNOPLoad() || ST->fixTN0009() || ST->fixTN0010() ||
323 ST->fixTN0012() || ST->fixTN0013()))
324 return false;
325
326 const TargetInstrInfo *TII = ST->getInstrInfo();
327 const TargetRegisterInfo *TRI = ST->getRegisterInfo();
328
329 if (ST->insertNOPLoad())
330 for (auto &MBB : MF)
332
333 if (ST->fixTN0010())
335
336 for (auto &MBB : MF) {
337 for (auto &I : MBB) {
338 if (ST->fixTN0009()) {
341 }
342 if (ST->fixTN0010())
344 if (ST->fixTN0012())
346 if (ST->fixTN0013())
348 }
349 }
350 return Changed;
351}
352
356
const AArch64InstrInfo & TII
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
IRTranslator LLVM IR MI
static bool isDivSqrt(MachineBasicBlock::iterator I)
static bool isFloat(MachineBasicBlock::iterator I)
static bool checkSeqTN0010First(MachineBasicBlock &MBB, const TargetInstrInfo *TII)
static bool checkSeqTN0010(MachineBasicBlock::iterator I, const TargetInstrInfo *TII)
static bool insertNOPLoad(MachineBasicBlock &MBB, const TargetInstrInfo *TII)
static bool isAtomic(MachineBasicBlock::iterator I)
static bool runErrataWorkaroundOnMachineFunction(MachineFunction &MF)
static bool checkSeqTN0009A(MachineBasicBlock::iterator I, const TargetInstrInfo *TII)
static bool checkSeqTN0013(MachineBasicBlock::iterator I, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI)
static void insertNop(MachineBasicBlock::iterator I, const TargetInstrInfo *TII)
static bool checkSeqTN0012(MachineBasicBlock::iterator I, const TargetInstrInfo *TII)
static bool checkSeqTN0009B(MachineBasicBlock::iterator I, const TargetInstrInfo *TII)
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineBasicBlock & front() const
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Pass manager infrastructure for declaring and invalidating analyses.
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
IterT skipMetaInstructionsForward(IterT It, IterT End)
Increment It until it points to a non-meta instruction or to End and return the resulting iterator.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void initializeSparcErrataWorkaroundLegacyPass(PassRegistry &)