Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 1 | //===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 7 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains the Mips implementation of the TargetInstrInfo class. |
| 11 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 14 | #include "MipsInstrInfo.h" |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 15 | #include "MipsTargetMachine.h" |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 16 | #include "MipsMachineFunction.h" |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 17 | #include "InstPrinter/MipsInstPrinter.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TargetRegistry.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Evan Cheng | 22fee2d | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 23 | |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 24 | #define GET_INSTRINFO_CTOR |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 25 | #include "MipsGenInstrInfo.inc" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 29 | MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm) |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 30 | : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP), |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 31 | TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()), |
| 32 | RI(*TM.getSubtargetImpl(), *this) {} |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 33 | |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 34 | |
| 35 | const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const { |
| 36 | return RI; |
| 37 | } |
| 38 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 39 | static bool isZeroImm(const MachineOperand &op) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 40 | return op.isImm() && op.getImm() == 0; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 43 | /// isLoadFromStackSlot - If the specified machine instruction is a direct |
| 44 | /// load from a stack slot, return the virtual or physical register number of |
| 45 | /// the destination along with the FrameIndex of the loaded stack slot. If |
| 46 | /// not, return 0. This predicate must return 0 if the instruction has |
| 47 | /// any side effects other than loading from the stack slot. |
| 48 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 49 | isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 50 | { |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame^] | 51 | unsigned Opc = MI->getOpcode(); |
| 52 | |
| 53 | if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) || |
| 54 | (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) || |
| 55 | (Opc == Mips::LDC1) || (Opc == Mips::LDC164) || |
| 56 | (Opc == Mips::LDC164_P8)) { |
Akira Hatanaka | d3ac47f | 2011-07-07 18:57:00 +0000 | [diff] [blame] | 57 | if ((MI->getOperand(1).isFI()) && // is a stack slot |
| 58 | (MI->getOperand(2).isImm()) && // the imm is zero |
| 59 | (isZeroImm(MI->getOperand(2)))) { |
| 60 | FrameIndex = MI->getOperand(1).getIndex(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 61 | return MI->getOperand(0).getReg(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /// isStoreToStackSlot - If the specified machine instruction is a direct |
| 69 | /// store to a stack slot, return the virtual or physical register number of |
| 70 | /// the source reg along with the FrameIndex of the loaded stack slot. If |
| 71 | /// not, return 0. This predicate must return 0 if the instruction has |
| 72 | /// any side effects other than storing to the stack slot. |
| 73 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 74 | isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 75 | { |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame^] | 76 | unsigned Opc = MI->getOpcode(); |
| 77 | |
| 78 | if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) || |
| 79 | (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) || |
| 80 | (Opc == Mips::SDC1) || (Opc == Mips::SDC164) || |
| 81 | (Opc == Mips::SDC164_P8)) { |
Akira Hatanaka | d3ac47f | 2011-07-07 18:57:00 +0000 | [diff] [blame] | 82 | if ((MI->getOperand(1).isFI()) && // is a stack slot |
| 83 | (MI->getOperand(2).isImm()) && // the imm is zero |
| 84 | (isZeroImm(MI->getOperand(2)))) { |
| 85 | FrameIndex = MI->getOperand(1).getIndex(); |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 86 | return MI->getOperand(0).getReg(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 92 | /// insertNoop - If data hazard condition is found insert the target nop |
| 93 | /// instruction. |
| 94 | void MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 95 | insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 96 | { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 97 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 98 | BuildMI(MBB, MI, DL, get(Mips::NOP)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 101 | void MipsInstrInfo:: |
| 102 | copyPhysReg(MachineBasicBlock &MBB, |
| 103 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 104 | unsigned DestReg, unsigned SrcReg, |
| 105 | bool KillSrc) const { |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 106 | unsigned Opc = 0, ZeroReg = 0; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 107 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 108 | if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg. |
| 109 | if (Mips::CPURegsRegClass.contains(SrcReg)) |
| 110 | Opc = Mips::ADDu, ZeroReg = Mips::ZERO; |
| 111 | else if (Mips::CCRRegClass.contains(SrcReg)) |
| 112 | Opc = Mips::CFC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 113 | else if (Mips::FGR32RegClass.contains(SrcReg)) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 114 | Opc = Mips::MFC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 115 | else if (SrcReg == Mips::HI) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 116 | Opc = Mips::MFHI, SrcReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 117 | else if (SrcReg == Mips::LO) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 118 | Opc = Mips::MFLO, SrcReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 119 | } |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 120 | else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg. |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 121 | if (Mips::CCRRegClass.contains(DestReg)) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 122 | Opc = Mips::CTC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 123 | else if (Mips::FGR32RegClass.contains(DestReg)) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 124 | Opc = Mips::MTC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 125 | else if (DestReg == Mips::HI) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 126 | Opc = Mips::MTHI, DestReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 127 | else if (DestReg == Mips::LO) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 128 | Opc = Mips::MTLO, DestReg = 0; |
| 129 | } |
| 130 | else if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) |
Akira Hatanaka | 4391bb7 | 2011-10-08 03:50:18 +0000 | [diff] [blame] | 131 | Opc = Mips::FMOV_S; |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 132 | else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) |
| 133 | Opc = Mips::FMOV_D32; |
| 134 | else if (Mips::CCRRegClass.contains(DestReg, SrcReg)) |
| 135 | Opc = Mips::MOVCCRToCCR; |
| 136 | else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg. |
| 137 | if (Mips::CPU64RegsRegClass.contains(SrcReg)) |
| 138 | Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64; |
| 139 | else if (SrcReg == Mips::HI64) |
| 140 | Opc = Mips::MFHI64, SrcReg = 0; |
| 141 | else if (SrcReg == Mips::LO64) |
| 142 | Opc = Mips::MFLO64, SrcReg = 0; |
| 143 | } |
| 144 | else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg. |
| 145 | if (DestReg == Mips::HI64) |
| 146 | Opc = Mips::MTHI64, DestReg = 0; |
| 147 | else if (DestReg == Mips::LO64) |
| 148 | Opc = Mips::MTLO64, DestReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 151 | assert(Opc && "Cannot copy registers"); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 152 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 153 | MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc)); |
| 154 | |
| 155 | if (DestReg) |
| 156 | MIB.addReg(DestReg, RegState::Define); |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 157 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 158 | if (ZeroReg) |
| 159 | MIB.addReg(ZeroReg); |
| 160 | |
| 161 | if (SrcReg) |
| 162 | MIB.addReg(SrcReg, getKillRegState(KillSrc)); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void MipsInstrInfo:: |
| 166 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 167 | unsigned SrcReg, bool isKill, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 168 | const TargetRegisterClass *RC, |
| 169 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 170 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 171 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 172 | unsigned Opc = 0; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 173 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 174 | if (RC == Mips::CPURegsRegisterClass) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 175 | Opc = IsN64 ? Mips::SW_P8 : Mips::SW; |
| 176 | else if (RC == Mips::CPU64RegsRegisterClass) |
| 177 | Opc = IsN64 ? Mips::SD_P8 : Mips::SD; |
Bruno Cardoso Lopes | 302525b | 2009-11-25 00:36:00 +0000 | [diff] [blame] | 178 | else if (RC == Mips::FGR32RegisterClass) |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame^] | 179 | Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1; |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 180 | else if (RC == Mips::AFGR64RegisterClass) |
| 181 | Opc = Mips::SDC1; |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame^] | 182 | else if (RC == Mips::FGR64RegisterClass) |
| 183 | Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164; |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 184 | |
| 185 | assert(Opc && "Register class not handled!"); |
| 186 | BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)) |
| 187 | .addFrameIndex(FI).addImm(0); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 190 | void MipsInstrInfo:: |
| 191 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 192 | unsigned DestReg, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 193 | const TargetRegisterClass *RC, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 194 | const TargetRegisterInfo *TRI) const |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 195 | { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 196 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 197 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 198 | unsigned Opc = 0; |
Bruno Cardoso Lopes | 302525b | 2009-11-25 00:36:00 +0000 | [diff] [blame] | 199 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 200 | if (RC == Mips::CPURegsRegisterClass) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 201 | Opc = IsN64 ? Mips::LW_P8 : Mips::LW; |
| 202 | else if (RC == Mips::CPU64RegsRegisterClass) |
| 203 | Opc = IsN64 ? Mips::LD_P8 : Mips::LD; |
Bruno Cardoso Lopes | 302525b | 2009-11-25 00:36:00 +0000 | [diff] [blame] | 204 | else if (RC == Mips::FGR32RegisterClass) |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame^] | 205 | Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1; |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 206 | else if (RC == Mips::AFGR64RegisterClass) |
| 207 | Opc = Mips::LDC1; |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame^] | 208 | else if (RC == Mips::FGR64RegisterClass) |
| 209 | Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164; |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 210 | |
| 211 | assert(Opc && "Register class not handled!"); |
| 212 | BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Akira Hatanaka | c4f24eb | 2011-07-01 01:04:43 +0000 | [diff] [blame] | 215 | MachineInstr* |
| 216 | MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx, |
| 217 | uint64_t Offset, const MDNode *MDPtr, |
| 218 | DebugLoc DL) const { |
| 219 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE)) |
| 220 | .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr); |
| 221 | return &*MIB; |
| 222 | } |
| 223 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 224 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 225 | // Branch Analysis |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 227 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 228 | static unsigned GetAnalyzableBrOpc(unsigned Opc) { |
| 229 | return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ || |
| 230 | Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ || |
| 231 | Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0; |
| 232 | } |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 233 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 234 | /// GetOppositeBranchOpc - Return the inverse of the specified |
| 235 | /// opcode, e.g. turning BEQ to BNE. |
| 236 | unsigned Mips::GetOppositeBranchOpc(unsigned Opc) |
| 237 | { |
| 238 | switch (Opc) { |
| 239 | default: llvm_unreachable("Illegal opcode!"); |
| 240 | case Mips::BEQ : return Mips::BNE; |
| 241 | case Mips::BNE : return Mips::BEQ; |
| 242 | case Mips::BGTZ : return Mips::BLEZ; |
| 243 | case Mips::BGEZ : return Mips::BLTZ; |
| 244 | case Mips::BLTZ : return Mips::BGEZ; |
| 245 | case Mips::BLEZ : return Mips::BGTZ; |
| 246 | case Mips::BC1T : return Mips::BC1F; |
| 247 | case Mips::BC1F : return Mips::BC1T; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 251 | static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc, |
| 252 | MachineBasicBlock *&BB, |
| 253 | SmallVectorImpl<MachineOperand>& Cond) { |
| 254 | assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch"); |
| 255 | int NumOp = Inst->getNumExplicitOperands(); |
| 256 | |
| 257 | // for both int and fp branches, the last explicit operand is the |
| 258 | // MBB. |
| 259 | BB = Inst->getOperand(NumOp-1).getMBB(); |
| 260 | Cond.push_back(MachineOperand::CreateImm(Opc)); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 261 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 262 | for (int i=0; i<NumOp-1; i++) |
| 263 | Cond.push_back(Inst->getOperand(i)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 266 | bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 267 | MachineBasicBlock *&TBB, |
| 268 | MachineBasicBlock *&FBB, |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 269 | SmallVectorImpl<MachineOperand> &Cond, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 270 | bool AllowModify) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 271 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 272 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 273 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 274 | // Skip all the debug instructions. |
| 275 | while (I != REnd && I->isDebugValue()) |
| 276 | ++I; |
| 277 | |
| 278 | if (I == REnd || !isUnpredicatedTerminator(&*I)) { |
| 279 | // If this block ends with no branches (it just falls through to its succ) |
| 280 | // just return false, leaving TBB/FBB null. |
| 281 | TBB = FBB = NULL; |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | MachineInstr *LastInst = &*I; |
| 286 | unsigned LastOpc = LastInst->getOpcode(); |
| 287 | |
| 288 | // Not an analyzable branch (must be an indirect jump). |
| 289 | if (!GetAnalyzableBrOpc(LastOpc)) |
| 290 | return true; |
| 291 | |
| 292 | // Get the second to last instruction in the block. |
| 293 | unsigned SecondLastOpc = 0; |
| 294 | MachineInstr *SecondLastInst = NULL; |
| 295 | |
| 296 | if (++I != REnd) { |
| 297 | SecondLastInst = &*I; |
| 298 | SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode()); |
| 299 | |
| 300 | // Not an analyzable branch (must be an indirect jump). |
| 301 | if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc) |
| 302 | return true; |
| 303 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 304 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 305 | // If there is only one terminator instruction, process it. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 306 | if (!SecondLastOpc) { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 307 | // Unconditional branch |
| 308 | if (LastOpc == Mips::J) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 309 | TBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 313 | // Conditional branch |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 314 | AnalyzeCondBr(LastInst, LastOpc, TBB, Cond); |
| 315 | return false; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 316 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 317 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 318 | // If we reached here, there are two branches. |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 319 | // If there are three terminators, we don't know what sort of block this is. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 320 | if (++I != REnd && isUnpredicatedTerminator(&*I)) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 321 | return true; |
| 322 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 323 | // If second to last instruction is an unconditional branch, |
| 324 | // analyze it and remove the last instruction. |
| 325 | if (SecondLastOpc == Mips::J) { |
| 326 | // Return if the last instruction cannot be removed. |
| 327 | if (!AllowModify) |
| 328 | return true; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 330 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 331 | LastInst->eraseFromParent(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 332 | return false; |
| 333 | } |
| 334 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 335 | // Conditional branch followed by an unconditional branch. |
| 336 | // The last one must be unconditional. |
| 337 | if (LastOpc != Mips::J) |
| 338 | return true; |
| 339 | |
| 340 | AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond); |
| 341 | FBB = LastInst->getOperand(0).getMBB(); |
| 342 | |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, |
| 347 | MachineBasicBlock *TBB, DebugLoc DL, |
| 348 | const SmallVectorImpl<MachineOperand>& Cond) |
| 349 | const { |
| 350 | unsigned Opc = Cond[0].getImm(); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 351 | const MCInstrDesc &MCID = get(Opc); |
| 352 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 353 | |
| 354 | for (unsigned i = 1; i < Cond.size(); ++i) |
| 355 | MIB.addReg(Cond[i].getReg()); |
| 356 | |
| 357 | MIB.addMBB(TBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 360 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 361 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 362 | MachineBasicBlock *FBB, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 363 | const SmallVectorImpl<MachineOperand> &Cond, |
| 364 | DebugLoc DL) const { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 365 | // Shouldn't be a fall through. |
| 366 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 367 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 368 | // # of condition operands: |
| 369 | // Unconditional branches: 0 |
| 370 | // Floating point branches: 1 (opc) |
| 371 | // Int BranchZero: 2 (opc, reg) |
| 372 | // Int Branch: 3 (opc, reg0, reg1) |
| 373 | assert((Cond.size() <= 3) && |
| 374 | "# of Mips branch conditions must be <= 3!"); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 375 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 376 | // Two-way Conditional branch. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 377 | if (FBB) { |
| 378 | BuildCondBr(MBB, TBB, DL, Cond); |
| 379 | BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB); |
| 380 | return 2; |
| 381 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 382 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 383 | // One way branch. |
| 384 | // Unconditional branch. |
| 385 | if (Cond.empty()) |
| 386 | BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB); |
| 387 | else // Conditional branch. |
| 388 | BuildCondBr(MBB, TBB, DL, Cond); |
| 389 | return 1; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 390 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 391 | |
| 392 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 393 | RemoveBranch(MachineBasicBlock &MBB) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 394 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 395 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
| 396 | MachineBasicBlock::reverse_iterator FirstBr; |
| 397 | unsigned removed; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 398 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 399 | // Skip all the debug instructions. |
| 400 | while (I != REnd && I->isDebugValue()) |
| 401 | ++I; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 402 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 403 | FirstBr = I; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 404 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 405 | // Up to 2 branches are removed. |
| 406 | // Note that indirect branches are not removed. |
| 407 | for(removed = 0; I != REnd && removed < 2; ++I, ++removed) |
| 408 | if (!GetAnalyzableBrOpc(I->getOpcode())) |
| 409 | break; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 410 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 411 | MBB.erase(I.base(), FirstBr.base()); |
| 412 | |
| 413 | return removed; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 416 | /// ReverseBranchCondition - Return the inverse opcode of the |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 417 | /// specified Branch instruction. |
| 418 | bool MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 419 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 420 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 421 | assert( (Cond.size() && Cond.size() <= 3) && |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 422 | "Invalid Mips branch condition!"); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 423 | Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm())); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 424 | return false; |
| 425 | } |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 426 | |
| 427 | /// getGlobalBaseReg - Return a virtual register initialized with the |
| 428 | /// the global base register value. Output instructions required to |
| 429 | /// initialize the register in the function entry block, if necessary. |
| 430 | /// |
| 431 | unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const { |
| 432 | MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>(); |
| 433 | unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg(); |
| 434 | if (GlobalBaseReg != 0) |
| 435 | return GlobalBaseReg; |
| 436 | |
| 437 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 438 | MachineBasicBlock &FirstMBB = MF->front(); |
| 439 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 440 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
| 441 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
| 442 | |
| 443 | GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass); |
Jakob Stoklund Olesen | 3ecf1f0 | 2010-07-10 22:43:03 +0000 | [diff] [blame] | 444 | BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), |
| 445 | GlobalBaseReg).addReg(Mips::GP); |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 446 | RegInfo.addLiveIn(Mips::GP); |
| 447 | |
| 448 | MipsFI->setGlobalBaseReg(GlobalBaseReg); |
| 449 | return GlobalBaseReg; |
| 450 | } |