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