AArch64A53Fix835769.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include "AArch64.h"
  17. #include "AArch64Subtarget.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineInstrBuilder.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/TargetInstrInfo.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "aarch64-fix-cortex-a53-835769"
  29. STATISTIC(NumNopsAdded, "Number of Nops added to work around erratum 835769");
  30. //===----------------------------------------------------------------------===//
  31. // Helper functions
  32. // Is the instruction a match for the instruction that comes first in the
  33. // sequence of instructions that can trigger the erratum?
  34. static bool isFirstInstructionInSequence(MachineInstr *MI) {
  35. // Must return true if this instruction is a load, a store or a prefetch.
  36. switch (MI->getOpcode()) {
  37. case AArch64::PRFMl:
  38. case AArch64::PRFMroW:
  39. case AArch64::PRFMroX:
  40. case AArch64::PRFMui:
  41. case AArch64::PRFUMi:
  42. return true;
  43. default:
  44. return MI->mayLoadOrStore();
  45. }
  46. }
  47. // Is the instruction a match for the instruction that comes second in the
  48. // sequence that can trigger the erratum?
  49. static bool isSecondInstructionInSequence(MachineInstr *MI) {
  50. // Must return true for non-SIMD integer multiply-accumulates, writing
  51. // to a 64-bit register.
  52. switch (MI->getOpcode()) {
  53. // Erratum cannot be triggered when the destination register is 32 bits,
  54. // therefore only include the following.
  55. case AArch64::MSUBXrrr:
  56. case AArch64::MADDXrrr:
  57. case AArch64::SMADDLrrr:
  58. case AArch64::SMSUBLrrr:
  59. case AArch64::UMADDLrrr:
  60. case AArch64::UMSUBLrrr:
  61. // Erratum can only be triggered by multiply-adds, not by regular
  62. // non-accumulating multiplies, i.e. when Ra=XZR='11111'
  63. return MI->getOperand(3).getReg() != AArch64::XZR;
  64. default:
  65. return false;
  66. }
  67. }
  68. //===----------------------------------------------------------------------===//
  69. namespace {
  70. class AArch64A53Fix835769 : public MachineFunctionPass {
  71. const TargetInstrInfo *TII;
  72. public:
  73. static char ID;
  74. explicit AArch64A53Fix835769() : MachineFunctionPass(ID) {
  75. initializeAArch64A53Fix835769Pass(*PassRegistry::getPassRegistry());
  76. }
  77. bool runOnMachineFunction(MachineFunction &F) override;
  78. MachineFunctionProperties getRequiredProperties() const override {
  79. return MachineFunctionProperties().set(
  80. MachineFunctionProperties::Property::NoVRegs);
  81. }
  82. StringRef getPassName() const override {
  83. return "Workaround A53 erratum 835769 pass";
  84. }
  85. void getAnalysisUsage(AnalysisUsage &AU) const override {
  86. AU.setPreservesCFG();
  87. MachineFunctionPass::getAnalysisUsage(AU);
  88. }
  89. private:
  90. bool runOnBasicBlock(MachineBasicBlock &MBB);
  91. };
  92. char AArch64A53Fix835769::ID = 0;
  93. } // end anonymous namespace
  94. INITIALIZE_PASS(AArch64A53Fix835769, "aarch64-fix-cortex-a53-835769-pass",
  95. "AArch64 fix for A53 erratum 835769", false, false)
  96. //===----------------------------------------------------------------------===//
  97. bool
  98. AArch64A53Fix835769::runOnMachineFunction(MachineFunction &F) {
  99. LLVM_DEBUG(dbgs() << "***** AArch64A53Fix835769 *****\n");
  100. auto &STI = F.getSubtarget<AArch64Subtarget>();
  101. // Fix not requested, skip pass.
  102. if (!STI.fixCortexA53_835769())
  103. return false;
  104. bool Changed = false;
  105. TII = STI.getInstrInfo();
  106. for (auto &MBB : F) {
  107. Changed |= runOnBasicBlock(MBB);
  108. }
  109. return Changed;
  110. }
  111. // Return the block that was fallen through to get to MBB, if any,
  112. // otherwise nullptr.
  113. static MachineBasicBlock *getBBFallenThrough(MachineBasicBlock *MBB,
  114. const TargetInstrInfo *TII) {
  115. // Get the previous machine basic block in the function.
  116. MachineFunction::iterator MBBI(MBB);
  117. // Can't go off top of function.
  118. if (MBBI == MBB->getParent()->begin())
  119. return nullptr;
  120. MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
  121. SmallVector<MachineOperand, 2> Cond;
  122. MachineBasicBlock *PrevBB = &*std::prev(MBBI);
  123. for (MachineBasicBlock *S : MBB->predecessors())
  124. if (S == PrevBB && !TII->analyzeBranch(*PrevBB, TBB, FBB, Cond) && !TBB &&
  125. !FBB)
  126. return S;
  127. return nullptr;
  128. }
  129. // Iterate through fallen through blocks trying to find a previous non-pseudo if
  130. // there is one, otherwise return nullptr. Only look for instructions in
  131. // previous blocks, not the current block, since we only use this to look at
  132. // previous blocks.
  133. static MachineInstr *getLastNonPseudo(MachineBasicBlock &MBB,
  134. const TargetInstrInfo *TII) {
  135. MachineBasicBlock *FMBB = &MBB;
  136. // If there is no non-pseudo in the current block, loop back around and try
  137. // the previous block (if there is one).
  138. while ((FMBB = getBBFallenThrough(FMBB, TII))) {
  139. for (MachineInstr &I : llvm::reverse(*FMBB))
  140. if (!I.isPseudo())
  141. return &I;
  142. }
  143. // There was no previous non-pseudo in the fallen through blocks
  144. return nullptr;
  145. }
  146. static void insertNopBeforeInstruction(MachineBasicBlock &MBB, MachineInstr* MI,
  147. const TargetInstrInfo *TII) {
  148. // If we are the first instruction of the block, put the NOP at the end of
  149. // the previous fallthrough block
  150. if (MI == &MBB.front()) {
  151. MachineInstr *I = getLastNonPseudo(MBB, TII);
  152. assert(I && "Expected instruction");
  153. DebugLoc DL = I->getDebugLoc();
  154. BuildMI(I->getParent(), DL, TII->get(AArch64::HINT)).addImm(0);
  155. }
  156. else {
  157. DebugLoc DL = MI->getDebugLoc();
  158. BuildMI(MBB, MI, DL, TII->get(AArch64::HINT)).addImm(0);
  159. }
  160. ++NumNopsAdded;
  161. }
  162. bool
  163. AArch64A53Fix835769::runOnBasicBlock(MachineBasicBlock &MBB) {
  164. bool Changed = false;
  165. LLVM_DEBUG(dbgs() << "Running on MBB: " << MBB
  166. << " - scanning instructions...\n");
  167. // First, scan the basic block, looking for a sequence of 2 instructions
  168. // that match the conditions under which the erratum may trigger.
  169. // List of terminating instructions in matching sequences
  170. std::vector<MachineInstr*> Sequences;
  171. unsigned Idx = 0;
  172. MachineInstr *PrevInstr = nullptr;
  173. // Try and find the last non-pseudo instruction in any fallen through blocks,
  174. // if there isn't one, then we use nullptr to represent that.
  175. PrevInstr = getLastNonPseudo(MBB, TII);
  176. for (auto &MI : MBB) {
  177. MachineInstr *CurrInstr = &MI;
  178. LLVM_DEBUG(dbgs() << " Examining: " << MI);
  179. if (PrevInstr) {
  180. LLVM_DEBUG(dbgs() << " PrevInstr: " << *PrevInstr
  181. << " CurrInstr: " << *CurrInstr
  182. << " isFirstInstructionInSequence(PrevInstr): "
  183. << isFirstInstructionInSequence(PrevInstr) << "\n"
  184. << " isSecondInstructionInSequence(CurrInstr): "
  185. << isSecondInstructionInSequence(CurrInstr) << "\n");
  186. if (isFirstInstructionInSequence(PrevInstr) &&
  187. isSecondInstructionInSequence(CurrInstr)) {
  188. LLVM_DEBUG(dbgs() << " ** pattern found at Idx " << Idx << "!\n");
  189. (void) Idx;
  190. Sequences.push_back(CurrInstr);
  191. }
  192. }
  193. if (!CurrInstr->isPseudo())
  194. PrevInstr = CurrInstr;
  195. ++Idx;
  196. }
  197. LLVM_DEBUG(dbgs() << "Scan complete, " << Sequences.size()
  198. << " occurrences of pattern found.\n");
  199. // Then update the basic block, inserting nops between the detected sequences.
  200. for (auto &MI : Sequences) {
  201. Changed = true;
  202. insertNopBeforeInstruction(MBB, MI, TII);
  203. }
  204. return Changed;
  205. }
  206. // Factory function used by AArch64TargetMachine to add the pass to
  207. // the passmanager.
  208. FunctionPass *llvm::createAArch64A53Fix835769() {
  209. return new AArch64A53Fix835769();
  210. }