Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 1 | //===------ LeonPasses.cpp - Define passes specific to LEON ---------------===// |
| 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 | // |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "LeonPasses.h" |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/ISDOpcodes.h" |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Dewhurst | 2c3cdd6 | 2016-10-19 14:01:06 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticInfo.h" |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 797fb96 | 2016-05-27 10:06:27 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 23 | |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 24 | LEONMachineFunctionPass::LEONMachineFunctionPass(char &ID) |
| 25 | : MachineFunctionPass(ID) {} |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 26 | |
| 27 | //***************************************************************************** |
| 28 | //**** InsertNOPLoad pass |
| 29 | //***************************************************************************** |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 30 | // This pass fixes the incorrectly working Load instructions that exists for |
| 31 | // some earlier versions of the LEON processor line. NOP instructions must |
| 32 | // be inserted after the load instruction to ensure that the Load instruction |
| 33 | // behaves as expected for these processors. |
| 34 | // |
| 35 | // This pass inserts a NOP after any LD or LDF instruction. |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 36 | // |
| 37 | char InsertNOPLoad::ID = 0; |
| 38 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 39 | InsertNOPLoad::InsertNOPLoad() : LEONMachineFunctionPass(ID) {} |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 40 | |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 41 | bool InsertNOPLoad::runOnMachineFunction(MachineFunction &MF) { |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 42 | Subtarget = &MF.getSubtarget<SparcSubtarget>(); |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 43 | const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 44 | DebugLoc DL = DebugLoc(); |
| 45 | |
| 46 | bool Modified = false; |
| 47 | for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) { |
| 48 | MachineBasicBlock &MBB = *MFI; |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 49 | for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) { |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 50 | MachineInstr &MI = *MBBI; |
| 51 | unsigned Opcode = MI.getOpcode(); |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 52 | if (Opcode >= SP::LDDArr && Opcode <= SP::LDrr) { |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 53 | MachineBasicBlock::iterator NMBBI = std::next(MBBI); |
| 54 | BuildMI(MBB, NMBBI, DL, TII.get(SP::NOP)); |
| 55 | Modified = true; |
Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return Modified; |
| 61 | } |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 62 | |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 63 | |
Chris Dewhurst | 2c3cdd6 | 2016-10-19 14:01:06 +0000 | [diff] [blame] | 64 | |
| 65 | //***************************************************************************** |
| 66 | //**** DetectRoundChange pass |
| 67 | //***************************************************************************** |
| 68 | // To prevent any explicit change of the default rounding mode, this pass |
| 69 | // detects any call of the fesetround function. |
| 70 | // A warning is generated to ensure the user knows this has happened. |
| 71 | // |
| 72 | // Detects an erratum in UT699 LEON 3 processor |
| 73 | |
| 74 | char DetectRoundChange::ID = 0; |
| 75 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 76 | DetectRoundChange::DetectRoundChange() : LEONMachineFunctionPass(ID) {} |
Chris Dewhurst | 2c3cdd6 | 2016-10-19 14:01:06 +0000 | [diff] [blame] | 77 | |
| 78 | bool DetectRoundChange::runOnMachineFunction(MachineFunction &MF) { |
| 79 | Subtarget = &MF.getSubtarget<SparcSubtarget>(); |
| 80 | |
| 81 | bool Modified = false; |
| 82 | for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) { |
| 83 | MachineBasicBlock &MBB = *MFI; |
| 84 | for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) { |
| 85 | MachineInstr &MI = *MBBI; |
| 86 | unsigned Opcode = MI.getOpcode(); |
| 87 | if (Opcode == SP::CALL && MI.getNumOperands() > 0) { |
| 88 | MachineOperand &MO = MI.getOperand(0); |
| 89 | |
| 90 | if (MO.isGlobal()) { |
| 91 | StringRef FuncName = MO.getGlobal()->getName(); |
| 92 | if (FuncName.compare_lower("fesetround") == 0) { |
| 93 | errs() << "Error: You are using the detectroundchange " |
| 94 | "option to detect rounding changes that will " |
| 95 | "cause LEON errata. The only way to fix this " |
| 96 | "is to remove the call to fesetround from " |
| 97 | "the source code.\n"; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return Modified; |
| 105 | } |
| 106 | |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 107 | //***************************************************************************** |
| 108 | //**** FixAllFDIVSQRT pass |
| 109 | //***************************************************************************** |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 110 | // This pass fixes the incorrectly working FDIVx and FSQRTx instructions that |
| 111 | // exist for some earlier versions of the LEON processor line. Five NOP |
| 112 | // instructions need to be inserted after these instructions to ensure the |
| 113 | // correct result is placed in the destination registers before they are used. |
| 114 | // |
| 115 | // This pass implements two fixes: |
| 116 | // 1) fixing the FSQRTS and FSQRTD instructions. |
| 117 | // 2) fixing the FDIVS and FDIVD instructions. |
| 118 | // |
| 119 | // FSQRTS and FDIVS are converted to FDIVD and FSQRTD respectively earlier in |
| 120 | // the pipeline when this option is enabled, so this pass needs only to deal |
| 121 | // with the changes that still need implementing for the "double" versions |
| 122 | // of these instructions. |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 123 | // |
| 124 | char FixAllFDIVSQRT::ID = 0; |
| 125 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 126 | FixAllFDIVSQRT::FixAllFDIVSQRT() : LEONMachineFunctionPass(ID) {} |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 127 | |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 128 | bool FixAllFDIVSQRT::runOnMachineFunction(MachineFunction &MF) { |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 129 | Subtarget = &MF.getSubtarget<SparcSubtarget>(); |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 130 | const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 131 | DebugLoc DL = DebugLoc(); |
| 132 | |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 133 | bool Modified = false; |
| 134 | for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) { |
| 135 | MachineBasicBlock &MBB = *MFI; |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 136 | for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) { |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 137 | MachineInstr &MI = *MBBI; |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 138 | unsigned Opcode = MI.getOpcode(); |
| 139 | |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 140 | // Note: FDIVS and FSQRTS cannot be generated when this erratum fix is |
| 141 | // switched on so we don't need to check for them here. They will |
| 142 | // already have been converted to FSQRTD or FDIVD earlier in the |
| 143 | // pipeline. |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 144 | if (Opcode == SP::FSQRTD || Opcode == SP::FDIVD) { |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 145 | for (int InsertedCount = 0; InsertedCount < 5; InsertedCount++) |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 146 | BuildMI(MBB, MBBI, DL, TII.get(SP::NOP)); |
| 147 | |
| 148 | MachineBasicBlock::iterator NMBBI = std::next(MBBI); |
Chris Dewhurst | 2bad85c | 2016-06-27 14:19:19 +0000 | [diff] [blame] | 149 | for (int InsertedCount = 0; InsertedCount < 28; InsertedCount++) |
Chris Dewhurst | 0c1e002 | 2016-06-19 11:03:28 +0000 | [diff] [blame] | 150 | BuildMI(MBB, NMBBI, DL, TII.get(SP::NOP)); |
| 151 | |
| 152 | Modified = true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return Modified; |
| 158 | } |