LLVM  13.0.0git
LeonPasses.cpp
Go to the documentation of this file.
1 //===------ LeonPasses.cpp - Define passes specific to LEON ---------------===//
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 //
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "LeonPasses.h"
13 #include "SparcSubtarget.h"
19 
20 using namespace llvm;
21 
24 
25 //*****************************************************************************
26 //**** InsertNOPLoad pass
27 //*****************************************************************************
28 // This pass fixes the incorrectly working Load instructions that exists for
29 // some earlier versions of the LEON processor line. NOP instructions must
30 // be inserted after the load instruction to ensure that the Load instruction
31 // behaves as expected for these processors.
32 //
33 // This pass inserts a NOP after any LD or LDF instruction.
34 //
35 char InsertNOPLoad::ID = 0;
36 
38 
42  DebugLoc DL = DebugLoc();
43 
44  bool Modified = false;
45  for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) {
46  MachineBasicBlock &MBB = *MFI;
47  for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
48  MachineInstr &MI = *MBBI;
49  unsigned Opcode = MI.getOpcode();
50  if (Opcode >= SP::LDDArr && Opcode <= SP::LDrr) {
51  MachineBasicBlock::iterator NMBBI = std::next(MBBI);
52  BuildMI(MBB, NMBBI, DL, TII.get(SP::NOP));
53  Modified = true;
54  }
55  }
56  }
57 
58  return Modified;
59 }
60 
61 
62 
63 //*****************************************************************************
64 //**** DetectRoundChange pass
65 //*****************************************************************************
66 // To prevent any explicit change of the default rounding mode, this pass
67 // detects any call of the fesetround function.
68 // A warning is generated to ensure the user knows this has happened.
69 //
70 // Detects an erratum in UT699 LEON 3 processor
71 
72 char DetectRoundChange::ID = 0;
73 
75 
78 
79  bool Modified = false;
80  for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) {
81  MachineBasicBlock &MBB = *MFI;
82  for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
83  MachineInstr &MI = *MBBI;
84  unsigned Opcode = MI.getOpcode();
85  if (Opcode == SP::CALL && MI.getNumOperands() > 0) {
86  MachineOperand &MO = MI.getOperand(0);
87 
88  if (MO.isGlobal()) {
89  StringRef FuncName = MO.getGlobal()->getName();
90  if (FuncName.compare_lower("fesetround") == 0) {
91  errs() << "Error: You are using the detectroundchange "
92  "option to detect rounding changes that will "
93  "cause LEON errata. The only way to fix this "
94  "is to remove the call to fesetround from "
95  "the source code.\n";
96  }
97  }
98  }
99  }
100  }
101 
102  return Modified;
103 }
104 
105 //*****************************************************************************
106 //**** FixAllFDIVSQRT pass
107 //*****************************************************************************
108 // This pass fixes the incorrectly working FDIVx and FSQRTx instructions that
109 // exist for some earlier versions of the LEON processor line. Five NOP
110 // instructions need to be inserted after these instructions to ensure the
111 // correct result is placed in the destination registers before they are used.
112 //
113 // This pass implements two fixes:
114 // 1) fixing the FSQRTS and FSQRTD instructions.
115 // 2) fixing the FDIVS and FDIVD instructions.
116 //
117 // FSQRTS and FDIVS are converted to FDIVD and FSQRTD respectively earlier in
118 // the pipeline when this option is enabled, so this pass needs only to deal
119 // with the changes that still need implementing for the "double" versions
120 // of these instructions.
121 //
122 char FixAllFDIVSQRT::ID = 0;
123 
125 
129  DebugLoc DL = DebugLoc();
130 
131  bool Modified = false;
132  for (auto MFI = MF.begin(), E = MF.end(); MFI != E; ++MFI) {
133  MachineBasicBlock &MBB = *MFI;
134  for (auto MBBI = MBB.begin(), E = MBB.end(); MBBI != E; ++MBBI) {
135  MachineInstr &MI = *MBBI;
136  unsigned Opcode = MI.getOpcode();
137 
138  // Note: FDIVS and FSQRTS cannot be generated when this erratum fix is
139  // switched on so we don't need to check for them here. They will
140  // already have been converted to FSQRTD or FDIVD earlier in the
141  // pipeline.
142  if (Opcode == SP::FSQRTD || Opcode == SP::FDIVD) {
143  for (int InsertedCount = 0; InsertedCount < 5; InsertedCount++)
144  BuildMI(MBB, MBBI, DL, TII.get(SP::NOP));
145 
146  MachineBasicBlock::iterator NMBBI = std::next(MBBI);
147  for (int InsertedCount = 0; InsertedCount < 28; InsertedCount++)
148  BuildMI(MBB, NMBBI, DL, TII.get(SP::NOP));
149 
150  Modified = true;
151  }
152  }
153  }
154 
155  return Modified;
156 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:102
MachineInstr.h
llvm
Definition: AllocatorList.h:23
llvm::LEONMachineFunctionPass::LEONMachineFunctionPass
LEONMachineFunctionPass(char &ID)
Definition: LeonPasses.cpp:22
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:563
llvm::DetectRoundChange::DetectRoundChange
DetectRoundChange()
Definition: LeonPasses.cpp:74
llvm::MachineFunction::end
iterator end()
Definition: MachineFunction.h:760
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::InsertNOPLoad::InsertNOPLoad
InsertNOPLoad()
Definition: LeonPasses.cpp:37
MachineBasicBlock.h
llvm::AArch64ISD::CALL
@ CALL
Definition: AArch64ISelLowering.h:52
llvm::DetectRoundChange::ID
static char ID
Definition: LeonPasses.h:57
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::InsertNOPLoad::ID
static char ID
Definition: LeonPasses.h:42
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:97
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::FixAllFDIVSQRT::FixAllFDIVSQRT
FixAllFDIVSQRT()
Definition: LeonPasses.cpp:124
llvm::StringRef::compare_lower
LLVM_NODISCARD int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:38
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
LoopDeletionResult::Modified
@ Modified
llvm::MachineFunction::begin
iterator begin()
Definition: MachineFunction.h:758
llvm::SparcSubtarget
Definition: SparcSubtarget.h:31
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
SparcSubtarget.h
llvm::DetectRoundChange::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: LeonPasses.cpp:76
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:576
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::FixAllFDIVSQRT::ID
static char ID
Definition: LeonPasses.h:70
llvm::SparcSubtarget::getInstrInfo
const SparcInstrInfo * getInstrInfo() const override
Definition: SparcSubtarget.h:64
llvm::LEONMachineFunctionPass::Subtarget
const SparcSubtarget * Subtarget
Definition: LeonPasses.h:23
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:294
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::InsertNOPLoad::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: LeonPasses.cpp:39
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::FixAllFDIVSQRT::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: LeonPasses.cpp:126
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
LeonPasses.h
raw_ostream.h
MachineFunction.h
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270
llvm::MachineOperand::isGlobal
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Definition: MachineOperand.h:339
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
llvm::LEONMachineFunctionPass
Definition: LeonPasses.h:20