Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 1 | //===-- AArch64A53Fix835769.cpp -------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // This pass changes code to work around Cortex-A53 erratum 835769. |
| 10 | // It works around it by inserting a nop instruction in code sequences that |
| 11 | // in some circumstances may trigger the erratum. |
| 12 | // It inserts a nop instruction between a sequence of the following 2 classes |
| 13 | // of instructions: |
| 14 | // instr 1: mem-instr (including loads, stores and prefetches). |
| 15 | // instr 2: non-SIMD integer multiply-accumulate writing 64-bit X registers. |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "AArch64.h" |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineInstr.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetInstrInfo.h" |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | #define DEBUG_TYPE "aarch64-fix-cortex-a53-835769" |
| 32 | |
| 33 | STATISTIC(NumNopsAdded, "Number of Nops added to work around erratum 835769"); |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Helper functions |
| 37 | |
| 38 | // Is the instruction a match for the instruction that comes first in the |
| 39 | // sequence of instructions that can trigger the erratum? |
| 40 | static bool isFirstInstructionInSequence(MachineInstr *MI) { |
| 41 | // Must return true if this instruction is a load, a store or a prefetch. |
| 42 | switch (MI->getOpcode()) { |
| 43 | case AArch64::PRFMl: |
| 44 | case AArch64::PRFMroW: |
| 45 | case AArch64::PRFMroX: |
| 46 | case AArch64::PRFMui: |
| 47 | case AArch64::PRFUMi: |
| 48 | return true; |
| 49 | default: |
Chad Rosier | a73b359 | 2015-05-21 21:59:57 +0000 | [diff] [blame] | 50 | return MI->mayLoadOrStore(); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | // Is the instruction a match for the instruction that comes second in the |
| 55 | // sequence that can trigger the erratum? |
| 56 | static bool isSecondInstructionInSequence(MachineInstr *MI) { |
| 57 | // Must return true for non-SIMD integer multiply-accumulates, writing |
| 58 | // to a 64-bit register. |
| 59 | switch (MI->getOpcode()) { |
| 60 | // Erratum cannot be triggered when the destination register is 32 bits, |
| 61 | // therefore only include the following. |
| 62 | case AArch64::MSUBXrrr: |
| 63 | case AArch64::MADDXrrr: |
| 64 | case AArch64::SMADDLrrr: |
| 65 | case AArch64::SMSUBLrrr: |
| 66 | case AArch64::UMADDLrrr: |
| 67 | case AArch64::UMSUBLrrr: |
| 68 | // Erratum can only be triggered by multiply-adds, not by regular |
| 69 | // non-accumulating multiplies, i.e. when Ra=XZR='11111' |
| 70 | return MI->getOperand(3).getReg() != AArch64::XZR; |
| 71 | default: |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | |
| 79 | namespace { |
| 80 | class AArch64A53Fix835769 : public MachineFunctionPass { |
Eric Christopher | 125898a | 2015-01-30 01:10:24 +0000 | [diff] [blame] | 81 | const TargetInstrInfo *TII; |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 82 | |
| 83 | public: |
| 84 | static char ID; |
Diana Picus | 850043b | 2016-08-01 05:56:57 +0000 | [diff] [blame] | 85 | explicit AArch64A53Fix835769() : MachineFunctionPass(ID) { |
| 86 | initializeAArch64A53Fix835769Pass(*PassRegistry::getPassRegistry()); |
| 87 | } |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 88 | |
| 89 | bool runOnMachineFunction(MachineFunction &F) override; |
| 90 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 91 | MachineFunctionProperties getRequiredProperties() const override { |
| 92 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 93 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 96 | StringRef getPassName() const override { |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 97 | return "Workaround A53 erratum 835769 pass"; |
| 98 | } |
| 99 | |
| 100 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 101 | AU.setPreservesCFG(); |
| 102 | MachineFunctionPass::getAnalysisUsage(AU); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | bool runOnBasicBlock(MachineBasicBlock &MBB); |
| 107 | }; |
| 108 | char AArch64A53Fix835769::ID = 0; |
| 109 | |
| 110 | } // end anonymous namespace |
| 111 | |
Diana Picus | 850043b | 2016-08-01 05:56:57 +0000 | [diff] [blame] | 112 | INITIALIZE_PASS(AArch64A53Fix835769, "aarch64-fix-cortex-a53-835769-pass", |
| 113 | "AArch64 fix for A53 erratum 835769", false, false) |
| 114 | |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
| 116 | |
| 117 | bool |
| 118 | AArch64A53Fix835769::runOnMachineFunction(MachineFunction &F) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 119 | LLVM_DEBUG(dbgs() << "***** AArch64A53Fix835769 *****\n"); |
Eric Christopher | 125898a | 2015-01-30 01:10:24 +0000 | [diff] [blame] | 120 | bool Changed = false; |
| 121 | TII = F.getSubtarget().getInstrInfo(); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 122 | |
| 123 | for (auto &MBB : F) { |
| 124 | Changed |= runOnBasicBlock(MBB); |
| 125 | } |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 126 | return Changed; |
| 127 | } |
| 128 | |
| 129 | // Return the block that was fallen through to get to MBB, if any, |
| 130 | // otherwise nullptr. |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 131 | static MachineBasicBlock *getBBFallenThrough(MachineBasicBlock *MBB, |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 132 | const TargetInstrInfo *TII) { |
| 133 | // Get the previous machine basic block in the function. |
Duncan P. N. Exon Smith | d3b9df0 | 2015-10-13 20:02:15 +0000 | [diff] [blame] | 134 | MachineFunction::iterator MBBI(MBB); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 135 | |
| 136 | // Can't go off top of function. |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 137 | if (MBBI == MBB->getParent()->begin()) |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 138 | return nullptr; |
| 139 | |
| 140 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
| 141 | SmallVector<MachineOperand, 2> Cond; |
| 142 | |
Duncan P. N. Exon Smith | d3b9df0 | 2015-10-13 20:02:15 +0000 | [diff] [blame] | 143 | MachineBasicBlock *PrevBB = &*std::prev(MBBI); |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 144 | for (MachineBasicBlock *S : MBB->predecessors()) |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 145 | if (S == PrevBB && !TII->analyzeBranch(*PrevBB, TBB, FBB, Cond) && !TBB && |
| 146 | !FBB) |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 147 | return S; |
| 148 | |
| 149 | return nullptr; |
| 150 | } |
| 151 | |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 152 | // Iterate through fallen through blocks trying to find a previous non-pseudo if |
| 153 | // there is one, otherwise return nullptr. Only look for instructions in |
| 154 | // previous blocks, not the current block, since we only use this to look at |
| 155 | // previous blocks. |
| 156 | static MachineInstr *getLastNonPseudo(MachineBasicBlock &MBB, |
| 157 | const TargetInstrInfo *TII) { |
| 158 | MachineBasicBlock *FMBB = &MBB; |
| 159 | |
| 160 | // If there is no non-pseudo in the current block, loop back around and try |
| 161 | // the previous block (if there is one). |
| 162 | while ((FMBB = getBBFallenThrough(FMBB, TII))) { |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 163 | for (MachineInstr &I : make_range(FMBB->rbegin(), FMBB->rend())) |
| 164 | if (!I.isPseudo()) |
| 165 | return &I; |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 168 | // There was no previous non-pseudo in the fallen through blocks |
| 169 | return nullptr; |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void insertNopBeforeInstruction(MachineBasicBlock &MBB, MachineInstr* MI, |
| 173 | const TargetInstrInfo *TII) { |
| 174 | // If we are the first instruction of the block, put the NOP at the end of |
| 175 | // the previous fallthrough block |
| 176 | if (MI == &MBB.front()) { |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 177 | MachineInstr *I = getLastNonPseudo(MBB, TII); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 178 | assert(I && "Expected instruction"); |
| 179 | DebugLoc DL = I->getDebugLoc(); |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 180 | BuildMI(I->getParent(), DL, TII->get(AArch64::HINT)).addImm(0); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 181 | } |
| 182 | else { |
| 183 | DebugLoc DL = MI->getDebugLoc(); |
| 184 | BuildMI(MBB, MI, DL, TII->get(AArch64::HINT)).addImm(0); |
| 185 | } |
| 186 | |
| 187 | ++NumNopsAdded; |
| 188 | } |
| 189 | |
| 190 | bool |
| 191 | AArch64A53Fix835769::runOnBasicBlock(MachineBasicBlock &MBB) { |
| 192 | bool Changed = false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 193 | LLVM_DEBUG(dbgs() << "Running on MBB: " << MBB |
| 194 | << " - scanning instructions...\n"); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 195 | |
| 196 | // First, scan the basic block, looking for a sequence of 2 instructions |
| 197 | // that match the conditions under which the erratum may trigger. |
| 198 | |
| 199 | // List of terminating instructions in matching sequences |
| 200 | std::vector<MachineInstr*> Sequences; |
| 201 | unsigned Idx = 0; |
| 202 | MachineInstr *PrevInstr = nullptr; |
| 203 | |
Bradley Smith | 698e08f | 2014-10-14 14:02:41 +0000 | [diff] [blame] | 204 | // Try and find the last non-pseudo instruction in any fallen through blocks, |
| 205 | // if there isn't one, then we use nullptr to represent that. |
| 206 | PrevInstr = getLastNonPseudo(MBB, TII); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 207 | |
| 208 | for (auto &MI : MBB) { |
| 209 | MachineInstr *CurrInstr = &MI; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 210 | LLVM_DEBUG(dbgs() << " Examining: " << MI); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 211 | if (PrevInstr) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 212 | LLVM_DEBUG(dbgs() << " PrevInstr: " << *PrevInstr |
| 213 | << " CurrInstr: " << *CurrInstr |
| 214 | << " isFirstInstructionInSequence(PrevInstr): " |
| 215 | << isFirstInstructionInSequence(PrevInstr) << "\n" |
| 216 | << " isSecondInstructionInSequence(CurrInstr): " |
| 217 | << isSecondInstructionInSequence(CurrInstr) << "\n"); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 218 | if (isFirstInstructionInSequence(PrevInstr) && |
| 219 | isSecondInstructionInSequence(CurrInstr)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 220 | LLVM_DEBUG(dbgs() << " ** pattern found at Idx " << Idx << "!\n"); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 221 | Sequences.push_back(CurrInstr); |
| 222 | } |
| 223 | } |
| 224 | if (!CurrInstr->isPseudo()) |
| 225 | PrevInstr = CurrInstr; |
| 226 | ++Idx; |
| 227 | } |
| 228 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 229 | LLVM_DEBUG(dbgs() << "Scan complete, " << Sequences.size() |
| 230 | << " occurrences of pattern found.\n"); |
Bradley Smith | f2a801d | 2014-10-13 10:12:35 +0000 | [diff] [blame] | 231 | |
| 232 | // Then update the basic block, inserting nops between the detected sequences. |
| 233 | for (auto &MI : Sequences) { |
| 234 | Changed = true; |
| 235 | insertNopBeforeInstruction(MBB, MI, TII); |
| 236 | } |
| 237 | |
| 238 | return Changed; |
| 239 | } |
| 240 | |
| 241 | // Factory function used by AArch64TargetMachine to add the pass to |
| 242 | // the passmanager. |
| 243 | FunctionPass *llvm::createAArch64A53Fix835769() { |
| 244 | return new AArch64A53Fix835769(); |
| 245 | } |