blob: e9d3aaeb9cfe23bcfed8e83eee53788540cbaa9e [file] [log] [blame]
Chris Dewhurst4f7cac32016-05-23 10:56:36 +00001//===------ LeonPasses.cpp - Define passes specific to LEON ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Dewhurst4f7cac32016-05-23 10:56:36 +00006//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
12#include "LeonPasses.h"
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000013#include "llvm/CodeGen/ISDOpcodes.h"
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000014#include "llvm/CodeGen/MachineFunction.h"
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000016#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Dewhurst2c3cdd62016-10-19 14:01:06 +000018#include "llvm/IR/DiagnosticInfo.h"
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000019#include "llvm/IR/LLVMContext.h"
20#include "llvm/Support/raw_ostream.h"
Benjamin Kramer797fb962016-05-27 10:06:27 +000021using namespace llvm;
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000022
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000023LEONMachineFunctionPass::LEONMachineFunctionPass(char &ID)
24 : MachineFunctionPass(ID) {}
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000025
26//*****************************************************************************
27//**** InsertNOPLoad pass
28//*****************************************************************************
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000029// This pass fixes the incorrectly working Load instructions that exists for
30// some earlier versions of the LEON processor line. NOP instructions must
31// be inserted after the load instruction to ensure that the Load instruction
32// behaves as expected for these processors.
33//
34// This pass inserts a NOP after any LD or LDF instruction.
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000035//
36char InsertNOPLoad::ID = 0;
37
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +000038InsertNOPLoad::InsertNOPLoad() : LEONMachineFunctionPass(ID) {}
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000039
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000040bool InsertNOPLoad::runOnMachineFunction(MachineFunction &MF) {
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000041 Subtarget = &MF.getSubtarget<SparcSubtarget>();
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000042 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000043 DebugLoc DL = DebugLoc();
44
45 bool Modified = false;
46 for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) {
47 MachineBasicBlock &MBB = *MFI;
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000048 for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000049 MachineInstr &MI = *MBBI;
50 unsigned Opcode = MI.getOpcode();
Chris Dewhurst2bad85c2016-06-27 14:19:19 +000051 if (Opcode >= SP::LDDArr && Opcode <= SP::LDrr) {
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000052 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
53 BuildMI(MBB, NMBBI, DL, TII.get(SP::NOP));
54 Modified = true;
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000055 }
56 }
57 }
58
59 return Modified;
60}
Chris Dewhurst0c1e0022016-06-19 11:03:28 +000061
Chris Dewhurst0c1e0022016-06-19 11:03:28 +000062
Chris Dewhurst2c3cdd62016-10-19 14:01:06 +000063
64//*****************************************************************************
65//**** DetectRoundChange pass
66//*****************************************************************************
67// To prevent any explicit change of the default rounding mode, this pass
68// detects any call of the fesetround function.
69// A warning is generated to ensure the user knows this has happened.
70//
71// Detects an erratum in UT699 LEON 3 processor
72
73char DetectRoundChange::ID = 0;
74
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +000075DetectRoundChange::DetectRoundChange() : LEONMachineFunctionPass(ID) {}
Chris Dewhurst2c3cdd62016-10-19 14:01:06 +000076
77bool DetectRoundChange::runOnMachineFunction(MachineFunction &MF) {
78 Subtarget = &MF.getSubtarget<SparcSubtarget>();
79
80 bool Modified = false;
81 for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) {
82 MachineBasicBlock &MBB = *MFI;
83 for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
84 MachineInstr &MI = *MBBI;
85 unsigned Opcode = MI.getOpcode();
86 if (Opcode == SP::CALL && MI.getNumOperands() > 0) {
87 MachineOperand &MO = MI.getOperand(0);
88
89 if (MO.isGlobal()) {
90 StringRef FuncName = MO.getGlobal()->getName();
91 if (FuncName.compare_lower("fesetround") == 0) {
92 errs() << "Error: You are using the detectroundchange "
93 "option to detect rounding changes that will "
94 "cause LEON errata. The only way to fix this "
95 "is to remove the call to fesetround from "
96 "the source code.\n";
97 }
98 }
99 }
100 }
101 }
102
103 return Modified;
104}
105
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000106//*****************************************************************************
107//**** FixAllFDIVSQRT pass
108//*****************************************************************************
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000109// This pass fixes the incorrectly working FDIVx and FSQRTx instructions that
110// exist for some earlier versions of the LEON processor line. Five NOP
111// instructions need to be inserted after these instructions to ensure the
112// correct result is placed in the destination registers before they are used.
113//
114// This pass implements two fixes:
115// 1) fixing the FSQRTS and FSQRTD instructions.
116// 2) fixing the FDIVS and FDIVD instructions.
117//
118// FSQRTS and FDIVS are converted to FDIVD and FSQRTD respectively earlier in
119// the pipeline when this option is enabled, so this pass needs only to deal
120// with the changes that still need implementing for the "double" versions
121// of these instructions.
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000122//
123char FixAllFDIVSQRT::ID = 0;
124
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000125FixAllFDIVSQRT::FixAllFDIVSQRT() : LEONMachineFunctionPass(ID) {}
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000126
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000127bool FixAllFDIVSQRT::runOnMachineFunction(MachineFunction &MF) {
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000128 Subtarget = &MF.getSubtarget<SparcSubtarget>();
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000129 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000130 DebugLoc DL = DebugLoc();
131
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000132 bool Modified = false;
133 for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) {
134 MachineBasicBlock &MBB = *MFI;
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000135 for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000136 MachineInstr &MI = *MBBI;
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000137 unsigned Opcode = MI.getOpcode();
138
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000139 // Note: FDIVS and FSQRTS cannot be generated when this erratum fix is
140 // switched on so we don't need to check for them here. They will
141 // already have been converted to FSQRTD or FDIVD earlier in the
142 // pipeline.
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000143 if (Opcode == SP::FSQRTD || Opcode == SP::FDIVD) {
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000144 for (int InsertedCount = 0; InsertedCount < 5; InsertedCount++)
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000145 BuildMI(MBB, MBBI, DL, TII.get(SP::NOP));
146
147 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
Chris Dewhurst2bad85c2016-06-27 14:19:19 +0000148 for (int InsertedCount = 0; InsertedCount < 28; InsertedCount++)
Chris Dewhurst0c1e0022016-06-19 11:03:28 +0000149 BuildMI(MBB, NMBBI, DL, TII.get(SP::NOP));
150
151 Modified = true;
152 }
153 }
154 }
155
156 return Modified;
157}