LLVM 20.0.0git
AArch64A53Fix835769.cpp
Go to the documentation of this file.
1//===-- AArch64A53Fix835769.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// This pass changes code to work around Cortex-A53 erratum 835769.
9// It works around it by inserting a nop instruction in code sequences that
10// in some circumstances may trigger the erratum.
11// It inserts a nop instruction between a sequence of the following 2 classes
12// of instructions:
13// instr 1: mem-instr (including loads, stores and prefetches).
14// instr 2: non-SIMD integer multiply-accumulate writing 64-bit X registers.
15//===----------------------------------------------------------------------===//
16
17#include "AArch64.h"
18#include "AArch64Subtarget.h"
19#include "llvm/ADT/Statistic.h"
25#include "llvm/Support/Debug.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "aarch64-fix-cortex-a53-835769"
31
32STATISTIC(NumNopsAdded, "Number of Nops added to work around erratum 835769");
33
34//===----------------------------------------------------------------------===//
35// Helper functions
36
37// Is the instruction a match for the instruction that comes first in the
38// sequence of instructions that can trigger the erratum?
40 // Must return true if this instruction is a load, a store or a prefetch.
41 switch (MI->getOpcode()) {
42 case AArch64::PRFMl:
43 case AArch64::PRFMroW:
44 case AArch64::PRFMroX:
45 case AArch64::PRFMui:
46 case AArch64::PRFUMi:
47 return true;
48 default:
49 return MI->mayLoadOrStore();
50 }
51}
52
53// Is the instruction a match for the instruction that comes second in the
54// sequence that can trigger the erratum?
56 // Must return true for non-SIMD integer multiply-accumulates, writing
57 // to a 64-bit register.
58 switch (MI->getOpcode()) {
59 // Erratum cannot be triggered when the destination register is 32 bits,
60 // therefore only include the following.
61 case AArch64::MSUBXrrr:
62 case AArch64::MADDXrrr:
63 case AArch64::SMADDLrrr:
64 case AArch64::SMSUBLrrr:
65 case AArch64::UMADDLrrr:
66 case AArch64::UMSUBLrrr:
67 // Erratum can only be triggered by multiply-adds, not by regular
68 // non-accumulating multiplies, i.e. when Ra=XZR='11111'
69 return MI->getOperand(3).getReg() != AArch64::XZR;
70 default:
71 return false;
72 }
73}
74
75
76//===----------------------------------------------------------------------===//
77
78namespace {
79class AArch64A53Fix835769 : public MachineFunctionPass {
80 const TargetInstrInfo *TII;
81
82public:
83 static char ID;
84 explicit AArch64A53Fix835769() : MachineFunctionPass(ID) {
86 }
87
89
92 MachineFunctionProperties::Property::NoVRegs);
93 }
94
95 StringRef getPassName() const override {
96 return "Workaround A53 erratum 835769 pass";
97 }
98
99 void getAnalysisUsage(AnalysisUsage &AU) const override {
100 AU.setPreservesCFG();
102 }
103
104private:
106};
107char AArch64A53Fix835769::ID = 0;
108
109} // end anonymous namespace
110
111INITIALIZE_PASS(AArch64A53Fix835769, "aarch64-fix-cortex-a53-835769-pass",
112 "AArch64 fix for A53 erratum 835769", false, false)
113
114//===----------------------------------------------------------------------===//
115
116bool
117AArch64A53Fix835769::runOnMachineFunction(MachineFunction &F) {
118 LLVM_DEBUG(dbgs() << "***** AArch64A53Fix835769 *****\n");
119 auto &STI = F.getSubtarget<AArch64Subtarget>();
120 // Fix not requested, skip pass.
121 if (!STI.fixCortexA53_835769())
122 return false;
123
124 bool Changed = false;
125 TII = STI.getInstrInfo();
126
127 for (auto &MBB : F) {
128 Changed |= runOnBasicBlock(MBB);
129 }
130 return Changed;
131}
132
133// Return the block that was fallen through to get to MBB, if any,
134// otherwise nullptr.
136 const TargetInstrInfo *TII) {
137 // Get the previous machine basic block in the function.
139
140 // Can't go off top of function.
141 if (MBBI == MBB->getParent()->begin())
142 return nullptr;
143
144 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
146
147 MachineBasicBlock *PrevBB = &*std::prev(MBBI);
149 if (S == PrevBB && !TII->analyzeBranch(*PrevBB, TBB, FBB, Cond) && !TBB &&
150 !FBB)
151 return S;
152
153 return nullptr;
154}
155
156// Iterate through fallen through blocks trying to find a previous non-pseudo if
157// there is one, otherwise return nullptr. Only look for instructions in
158// previous blocks, not the current block, since we only use this to look at
159// previous blocks.
161 const TargetInstrInfo *TII) {
162 MachineBasicBlock *FMBB = &MBB;
163
164 // If there is no non-pseudo in the current block, loop back around and try
165 // the previous block (if there is one).
166 while ((FMBB = getBBFallenThrough(FMBB, TII))) {
167 for (MachineInstr &I : llvm::reverse(*FMBB))
168 if (!I.isPseudo())
169 return &I;
170 }
171
172 // There was no previous non-pseudo in the fallen through blocks
173 return nullptr;
174}
175
177 const TargetInstrInfo *TII) {
178 // If we are the first instruction of the block, put the NOP at the end of
179 // the previous fallthrough block
180 if (MI == &MBB.front()) {
182 assert(I && "Expected instruction");
183 DebugLoc DL = I->getDebugLoc();
184 BuildMI(I->getParent(), DL, TII->get(AArch64::HINT)).addImm(0);
185 }
186 else {
187 DebugLoc DL = MI->getDebugLoc();
188 BuildMI(MBB, MI, DL, TII->get(AArch64::HINT)).addImm(0);
189 }
190
191 ++NumNopsAdded;
192}
193
194bool
195AArch64A53Fix835769::runOnBasicBlock(MachineBasicBlock &MBB) {
196 bool Changed = false;
197 LLVM_DEBUG(dbgs() << "Running on MBB: " << MBB
198 << " - scanning instructions...\n");
199
200 // First, scan the basic block, looking for a sequence of 2 instructions
201 // that match the conditions under which the erratum may trigger.
202
203 // List of terminating instructions in matching sequences
204 std::vector<MachineInstr*> Sequences;
205 unsigned Idx = 0;
206 MachineInstr *PrevInstr = nullptr;
207
208 // Try and find the last non-pseudo instruction in any fallen through blocks,
209 // if there isn't one, then we use nullptr to represent that.
210 PrevInstr = getLastNonPseudo(MBB, TII);
211
212 for (auto &MI : MBB) {
213 MachineInstr *CurrInstr = &MI;
214 LLVM_DEBUG(dbgs() << " Examining: " << MI);
215 if (PrevInstr) {
216 LLVM_DEBUG(dbgs() << " PrevInstr: " << *PrevInstr
217 << " CurrInstr: " << *CurrInstr
218 << " isFirstInstructionInSequence(PrevInstr): "
219 << isFirstInstructionInSequence(PrevInstr) << "\n"
220 << " isSecondInstructionInSequence(CurrInstr): "
221 << isSecondInstructionInSequence(CurrInstr) << "\n");
222 if (isFirstInstructionInSequence(PrevInstr) &&
224 LLVM_DEBUG(dbgs() << " ** pattern found at Idx " << Idx << "!\n");
225 (void) Idx;
226 Sequences.push_back(CurrInstr);
227 }
228 }
229 if (!CurrInstr->isPseudo())
230 PrevInstr = CurrInstr;
231 ++Idx;
232 }
233
234 LLVM_DEBUG(dbgs() << "Scan complete, " << Sequences.size()
235 << " occurrences of pattern found.\n");
236
237 // Then update the basic block, inserting nops between the detected sequences.
238 for (auto &MI : Sequences) {
239 Changed = true;
241 }
242
243 return Changed;
244}
245
246// Factory function used by AArch64TargetMachine to add the pass to
247// the passmanager.
249 return new AArch64A53Fix835769();
250}
static MachineBasicBlock * getBBFallenThrough(MachineBasicBlock *MBB, const TargetInstrInfo *TII)
static MachineInstr * getLastNonPseudo(MachineBasicBlock &MBB, const TargetInstrInfo *TII)
static void insertNopBeforeInstruction(MachineBasicBlock &MBB, MachineInstr *MI, const TargetInstrInfo *TII)
static bool isFirstInstructionInSequence(MachineInstr *MI)
static bool isSecondInstructionInSequence(MachineInstr *MI)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(...)
Definition: Debug.h:106
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static bool runOnBasicBlock(MachineBasicBlock *MBB, unsigned BasicBlockNum, VRegRenamer &Renamer)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< pred_iterator > predecessors()
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
bool isPseudo(QueryType Type=IgnoreBundle) const
Return true if this is a pseudo instruction that doesn't correspond to a real machine instruction.
Definition: MachineInstr.h:936
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
TargetInstrInfo - Interface to description of machine instruction set.
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
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeAArch64A53Fix835769Pass(PassRegistry &)
FunctionPass * createAArch64A53Fix835769()