LLVM  16.0.0git
MipsMulMulBugPass.cpp
Go to the documentation of this file.
1 //===- MipsMulMulBugPass.cpp - Mips VR4300 mulmul bugfix pass -------------===//
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 // Early revisions of the VR4300 have a hardware bug where two consecutive
10 // multiplications can produce an incorrect result in the second multiply.
11 //
12 // This pass scans for mul instructions in each basic block and inserts
13 // a nop whenever the following conditions are met:
14 //
15 // - The current instruction is a single or double-precision floating-point
16 // mul instruction.
17 // - The next instruction is either a mul instruction (any kind)
18 // or a branch instruction.
19 //===----------------------------------------------------------------------===//
20 
21 #include "Mips.h"
22 #include "MipsInstrInfo.h"
23 #include "MipsSubtarget.h"
28 #include "llvm/Support/Debug.h"
30 
31 #define DEBUG_TYPE "mips-vr4300-mulmul-fix"
32 
33 using namespace llvm;
34 
35 namespace {
36 
37 class MipsMulMulBugFix : public MachineFunctionPass {
38 public:
39  MipsMulMulBugFix() : MachineFunctionPass(ID) {
41  }
42 
43  StringRef getPassName() const override { return "Mips VR4300 mulmul bugfix"; }
44 
45  MachineFunctionProperties getRequiredProperties() const override {
48  }
49 
50  bool runOnMachineFunction(MachineFunction &MF) override;
51 
52  static char ID;
53 
54 private:
55  bool fixMulMulBB(MachineBasicBlock &MBB, const MipsInstrInfo &MipsII);
56 };
57 
58 } // namespace
59 
60 INITIALIZE_PASS(MipsMulMulBugFix, "mips-vr4300-mulmul-fix",
61  "Mips VR4300 mulmul bugfix", false, false)
62 
63 char MipsMulMulBugFix::ID = 0;
64 
65 bool MipsMulMulBugFix::runOnMachineFunction(MachineFunction &MF) {
66  const MipsInstrInfo &MipsII =
67  *static_cast<const MipsInstrInfo *>(MF.getSubtarget().getInstrInfo());
68 
69  bool Modified = false;
70 
71  for (auto &MBB : MF)
72  Modified |= fixMulMulBB(MBB, MipsII);
73 
74  return Modified;
75 }
76 
77 static bool isFirstMul(const MachineInstr &MI) {
78  switch (MI.getOpcode()) {
79  case Mips::FMUL_S:
80  case Mips::FMUL_D:
81  case Mips::FMUL_D32:
82  case Mips::FMUL_D64:
83  return true;
84  default:
85  return false;
86  }
87 }
88 
89 static bool isSecondMulOrBranch(const MachineInstr &MI) {
90  if (MI.isBranch() || MI.isIndirectBranch() || MI.isCall())
91  return true;
92 
93  switch (MI.getOpcode()) {
94  case Mips::MUL:
95  case Mips::FMUL_S:
96  case Mips::FMUL_D:
97  case Mips::FMUL_D32:
98  case Mips::FMUL_D64:
99  case Mips::MULT:
100  case Mips::MULTu:
101  case Mips::DMULT:
102  case Mips::DMULTu:
103  return true;
104  default:
105  return false;
106  }
107 }
108 
109 bool MipsMulMulBugFix::fixMulMulBB(MachineBasicBlock &MBB,
110  const MipsInstrInfo &MipsII) {
111  bool Modified = false;
112 
114 
115  // Iterate through the instructions in the basic block
117  E = MBB.instr_end();
118  MII != E; MII = NextMII) {
119 
120  NextMII = next_nodbg(MII, E);
121 
122  // Trigger when the current instruction is a mul and the next instruction
123  // is either a mul or a branch in case the branch target start with a mul
124  if (NextMII != E && isFirstMul(*MII) && isSecondMulOrBranch(*NextMII)) {
125  LLVM_DEBUG(dbgs() << "Found mulmul!\n");
126 
127  const MCInstrDesc &NewMCID = MipsII.get(Mips::NOP);
128  BuildMI(MBB, NextMII, DebugLoc(), NewMCID);
129  Modified = true;
130  }
131  }
132 
133  return Modified;
134 }
135 
136 FunctionPass *llvm::createMipsMulMulBugPass() { return new MipsMulMulBugFix(); }
llvm::next_nodbg
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
Definition: MachineBasicBlock.h:1289
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:108
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
MachineBasicBlock.h
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition: MachineFunction.h:127
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
isFirstMul
static bool isFirstMul(const MachineInstr &MI)
Definition: MipsMulMulBugPass.cpp:77
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
TargetMachine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
llvm::MipsISD::MULT
@ MULT
Definition: MipsISelLowering.h:197
llvm::initializeMipsMulMulBugFixPass
void initializeMipsMulMulBugFixPass(PassRegistry &)
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
Mips.h
llvm::MCInstrDesc
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:197
llvm::MipsInstrInfo
Definition: MipsInstrInfo.h:41
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition: MachineFunction.h:196
LoopDeletionResult::Modified
@ Modified
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::MachineFunctionProperties::Property::NoVRegs
@ NoVRegs
isSecondMulOrBranch
static bool isSecondMulOrBranch(const MachineInstr &MI)
Definition: MipsMulMulBugPass.cpp:89
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
MachineFunctionPass.h
llvm::MachineBasicBlock::instr_begin
instr_iterator instr_begin()
Definition: MachineBasicBlock.h:289
llvm::MachineBasicBlock::instr_end
instr_iterator instr_end()
Definition: MachineBasicBlock.h:291
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::ilist_iterator
Iterator for intrusive lists based on ilist_node.
Definition: ilist_iterator.h:57
MipsInstrInfo.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:357
llvm::ISD::MUL
@ MUL
Definition: ISDOpcodes.h:241
MipsSubtarget.h
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
MachineFunction.h
Debug.h
llvm::createMipsMulMulBugPass
FunctionPass * createMipsMulMulBugPass()
Definition: MipsMulMulBugPass.cpp:136