Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 1 | //===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===// |
| 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 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the Mips implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 14 | //#include "Mips.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 15 | #include "MipsInstrInfo.h" |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 16 | #include "MipsTargetMachine.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" |
| 19 | #include "MipsGenInstrInfo.inc" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 23 | MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm) |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 24 | : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)), |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 25 | TM(tm), RI(*TM.getSubtargetImpl(), *this) {} |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 26 | |
| 27 | static bool isZeroImm(const MachineOperand &op) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 28 | return op.isImmediate() && op.getImm() == 0; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /// Return true if the instruction is a register to register move and |
| 32 | /// leave the source and dest operands in the passed parameters. |
| 33 | bool MipsInstrInfo:: |
| 34 | isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg) const |
| 35 | { |
| 36 | // addu $dst, $src, $zero || addu $dst, $zero, $src |
| 37 | // or $dst, $src, $zero || or $dst, $zero, $src |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 38 | if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 39 | if (MI.getOperand(1).getReg() == Mips::ZERO) { |
| 40 | DstReg = MI.getOperand(0).getReg(); |
| 41 | SrcReg = MI.getOperand(2).getReg(); |
| 42 | return true; |
| 43 | } else if (MI.getOperand(2).getReg() == Mips::ZERO) { |
| 44 | DstReg = MI.getOperand(0).getReg(); |
| 45 | SrcReg = MI.getOperand(1).getReg(); |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 50 | // mov $fpDst, $fpSrc |
| 51 | // mfc $gpDst, $fpSrc |
| 52 | // mtc $fpDst, $gpSrc |
| 53 | if (MI.getOpcode() == Mips::FMOV_SO32 || MI.getOpcode() == Mips::FMOV_AS32 || |
| 54 | MI.getOpcode() == Mips::FMOV_D32 || MI.getOpcode() == Mips::MFC1A || |
| 55 | MI.getOpcode() == Mips::MFC1 || MI.getOpcode() == Mips::MTC1A || |
| 56 | MI.getOpcode() == Mips::MTC1 ) { |
| 57 | DstReg = MI.getOperand(0).getReg(); |
| 58 | SrcReg = MI.getOperand(1).getReg(); |
| 59 | return true; |
| 60 | } |
| 61 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 62 | // addiu $dst, $src, 0 |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 63 | if (MI.getOpcode() == Mips::ADDiu) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 64 | if ((MI.getOperand(1).isRegister()) && (isZeroImm(MI.getOperand(2)))) { |
| 65 | DstReg = MI.getOperand(0).getReg(); |
| 66 | SrcReg = MI.getOperand(1).getReg(); |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | /// isLoadFromStackSlot - If the specified machine instruction is a direct |
| 74 | /// load from a stack slot, return the virtual or physical register number of |
| 75 | /// the destination along with the FrameIndex of the loaded stack slot. If |
| 76 | /// not, return 0. This predicate must return 0 if the instruction has |
| 77 | /// any side effects other than loading from the stack slot. |
| 78 | unsigned MipsInstrInfo:: |
| 79 | isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const |
| 80 | { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 81 | if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) || |
| 82 | (MI->getOpcode() == Mips::LWC1A) || (MI->getOpcode() == Mips::LDC1)) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 83 | if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot |
| 84 | (MI->getOperand(1).isImmediate()) && // the imm is zero |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 85 | (isZeroImm(MI->getOperand(1)))) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 86 | FrameIndex = MI->getOperand(2).getIndex(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 87 | return MI->getOperand(0).getReg(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /// isStoreToStackSlot - If the specified machine instruction is a direct |
| 95 | /// store to a stack slot, return the virtual or physical register number of |
| 96 | /// the source reg along with the FrameIndex of the loaded stack slot. If |
| 97 | /// not, return 0. This predicate must return 0 if the instruction has |
| 98 | /// any side effects other than storing to the stack slot. |
| 99 | unsigned MipsInstrInfo:: |
| 100 | isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const |
| 101 | { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 102 | if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) || |
| 103 | (MI->getOpcode() == Mips::SWC1A) || (MI->getOpcode() == Mips::SDC1)) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 104 | if ((MI->getOperand(0).isFrameIndex()) && // is a stack slot |
| 105 | (MI->getOperand(1).isImmediate()) && // the imm is zero |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 106 | (isZeroImm(MI->getOperand(1)))) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 107 | FrameIndex = MI->getOperand(0).getIndex(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 108 | return MI->getOperand(2).getReg(); |
| 109 | } |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 114 | /// insertNoop - If data hazard condition is found insert the target nop |
| 115 | /// instruction. |
| 116 | void MipsInstrInfo:: |
| 117 | insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const |
| 118 | { |
| 119 | BuildMI(MBB, MI, get(Mips::NOP)); |
| 120 | } |
| 121 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 122 | void MipsInstrInfo:: |
| 123 | copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 124 | unsigned DestReg, unsigned SrcReg, |
| 125 | const TargetRegisterClass *DestRC, |
| 126 | const TargetRegisterClass *SrcRC) const { |
| 127 | if (DestRC != SrcRC) { |
| 128 | if ((DestRC == Mips::CPURegsRegisterClass) && |
| 129 | (SrcRC == Mips::FGR32RegisterClass)) |
| 130 | BuildMI(MBB, I, get(Mips::MFC1), DestReg).addReg(SrcReg); |
| 131 | else if ((DestRC == Mips::CPURegsRegisterClass) && |
| 132 | (SrcRC == Mips::AFGR32RegisterClass)) |
| 133 | BuildMI(MBB, I, get(Mips::MFC1A), DestReg).addReg(SrcReg); |
| 134 | else if ((DestRC == Mips::FGR32RegisterClass) && |
| 135 | (SrcRC == Mips::CPURegsRegisterClass)) |
| 136 | BuildMI(MBB, I, get(Mips::MTC1), DestReg).addReg(SrcReg); |
| 137 | else if ((DestRC == Mips::AFGR32RegisterClass) && |
| 138 | (SrcRC == Mips::CPURegsRegisterClass)) |
| 139 | BuildMI(MBB, I, get(Mips::MTC1A), DestReg).addReg(SrcReg); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame^] | 140 | else if ((SrcRC == Mips::CCRRegisterClass) && |
| 141 | (SrcReg == Mips::FCR31)) |
| 142 | return; // This register is used implicitly, no copy needed. |
| 143 | else if ((DestRC == Mips::CCRRegisterClass) && |
| 144 | (DestReg == Mips::FCR31)) |
| 145 | return; // This register is used implicitly, no copy needed. |
| 146 | else |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 147 | assert (0 && "DestRC != SrcRC, Can't copy this register"); |
| 148 | } |
| 149 | |
| 150 | if (DestRC == Mips::CPURegsRegisterClass) |
| 151 | BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO) |
| 152 | .addReg(SrcReg); |
| 153 | else if (DestRC == Mips::FGR32RegisterClass) |
| 154 | BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg); |
| 155 | else if (DestRC == Mips::AFGR32RegisterClass) |
| 156 | BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg); |
| 157 | else if (DestRC == Mips::AFGR64RegisterClass) |
| 158 | BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg); |
| 159 | else |
| 160 | assert (0 && "Can't copy this register"); |
| 161 | } |
| 162 | |
| 163 | void MipsInstrInfo:: |
| 164 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 165 | unsigned SrcReg, bool isKill, int FI, |
| 166 | const TargetRegisterClass *RC) const |
| 167 | { |
| 168 | unsigned Opc; |
| 169 | if (RC == Mips::CPURegsRegisterClass) |
| 170 | Opc = Mips::SW; |
| 171 | else if (RC == Mips::FGR32RegisterClass) |
| 172 | Opc = Mips::SWC1; |
| 173 | else if (RC == Mips::AFGR32RegisterClass) |
| 174 | Opc = Mips::SWC1A; |
| 175 | else if (RC == Mips::AFGR64RegisterClass) |
| 176 | Opc = Mips::SDC1; |
| 177 | else |
| 178 | assert(0 && "Can't store this register to stack slot"); |
| 179 | |
| 180 | BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill) |
| 181 | .addImm(0).addFrameIndex(FI); |
| 182 | } |
| 183 | |
| 184 | void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg, |
| 185 | bool isKill, SmallVectorImpl<MachineOperand> &Addr, |
| 186 | const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const |
| 187 | { |
| 188 | unsigned Opc; |
| 189 | if (RC == Mips::CPURegsRegisterClass) |
| 190 | Opc = Mips::SW; |
| 191 | else if (RC == Mips::FGR32RegisterClass) |
| 192 | Opc = Mips::SWC1; |
| 193 | else if (RC == Mips::AFGR32RegisterClass) |
| 194 | Opc = Mips::SWC1A; |
| 195 | else if (RC == Mips::AFGR64RegisterClass) |
| 196 | Opc = Mips::SDC1; |
| 197 | else |
| 198 | assert(0 && "Can't store this register"); |
| 199 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 200 | MachineInstrBuilder MIB = BuildMI(MF, get(Opc)) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 201 | .addReg(SrcReg, false, false, isKill); |
| 202 | for (unsigned i = 0, e = Addr.size(); i != e; ++i) { |
| 203 | MachineOperand &MO = Addr[i]; |
| 204 | if (MO.isRegister()) |
| 205 | MIB.addReg(MO.getReg()); |
| 206 | else if (MO.isImmediate()) |
| 207 | MIB.addImm(MO.getImm()); |
| 208 | else |
| 209 | MIB.addFrameIndex(MO.getIndex()); |
| 210 | } |
| 211 | NewMIs.push_back(MIB); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | void MipsInstrInfo:: |
| 216 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 217 | unsigned DestReg, int FI, |
| 218 | const TargetRegisterClass *RC) const |
| 219 | { |
| 220 | unsigned Opc; |
| 221 | if (RC == Mips::CPURegsRegisterClass) |
| 222 | Opc = Mips::LW; |
| 223 | else if (RC == Mips::FGR32RegisterClass) |
| 224 | Opc = Mips::LWC1; |
| 225 | else if (RC == Mips::AFGR32RegisterClass) |
| 226 | Opc = Mips::LWC1A; |
| 227 | else if (RC == Mips::AFGR64RegisterClass) |
| 228 | Opc = Mips::LDC1; |
| 229 | else |
| 230 | assert(0 && "Can't load this register from stack slot"); |
| 231 | |
| 232 | BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI); |
| 233 | } |
| 234 | |
| 235 | void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg, |
| 236 | SmallVectorImpl<MachineOperand> &Addr, |
| 237 | const TargetRegisterClass *RC, |
| 238 | SmallVectorImpl<MachineInstr*> &NewMIs) const { |
| 239 | unsigned Opc; |
| 240 | if (RC == Mips::CPURegsRegisterClass) |
| 241 | Opc = Mips::LW; |
| 242 | else if (RC == Mips::FGR32RegisterClass) |
| 243 | Opc = Mips::LWC1; |
| 244 | else if (RC == Mips::AFGR32RegisterClass) |
| 245 | Opc = Mips::LWC1A; |
| 246 | else if (RC == Mips::AFGR64RegisterClass) |
| 247 | Opc = Mips::LDC1; |
| 248 | else |
| 249 | assert(0 && "Can't load this register"); |
| 250 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 251 | MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 252 | for (unsigned i = 0, e = Addr.size(); i != e; ++i) { |
| 253 | MachineOperand &MO = Addr[i]; |
| 254 | if (MO.isRegister()) |
| 255 | MIB.addReg(MO.getReg()); |
| 256 | else if (MO.isImmediate()) |
| 257 | MIB.addImm(MO.getImm()); |
| 258 | else |
| 259 | MIB.addFrameIndex(MO.getIndex()); |
| 260 | } |
| 261 | NewMIs.push_back(MIB); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | MachineInstr *MipsInstrInfo:: |
| 266 | foldMemoryOperand(MachineFunction &MF, |
| 267 | MachineInstr* MI, |
| 268 | SmallVectorImpl<unsigned> &Ops, int FI) const |
| 269 | { |
| 270 | if (Ops.size() != 1) return NULL; |
| 271 | |
| 272 | MachineInstr *NewMI = NULL; |
| 273 | |
| 274 | switch (MI->getOpcode()) { |
| 275 | case Mips::ADDu: |
| 276 | if ((MI->getOperand(0).isRegister()) && |
| 277 | (MI->getOperand(1).isRegister()) && |
| 278 | (MI->getOperand(1).getReg() == Mips::ZERO) && |
| 279 | (MI->getOperand(2).isRegister())) { |
| 280 | if (Ops[0] == 0) { // COPY -> STORE |
| 281 | unsigned SrcReg = MI->getOperand(2).getReg(); |
| 282 | bool isKill = MI->getOperand(2).isKill(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 283 | NewMI = BuildMI(MF, get(Mips::SW)).addFrameIndex(FI) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 284 | .addImm(0).addReg(SrcReg, false, false, isKill); |
| 285 | } else { // COPY -> LOAD |
| 286 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 287 | bool isDead = MI->getOperand(0).isDead(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 288 | NewMI = BuildMI(MF, get(Mips::LW)) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 289 | .addReg(DstReg, true, false, false, isDead) |
| 290 | .addImm(0).addFrameIndex(FI); |
| 291 | } |
| 292 | } |
| 293 | break; |
| 294 | case Mips::FMOV_SO32: |
| 295 | case Mips::FMOV_AS32: |
| 296 | case Mips::FMOV_D32: |
| 297 | if ((MI->getOperand(0).isRegister()) && |
| 298 | (MI->getOperand(1).isRegister())) { |
Bruno Cardoso Lopes | 7b76da1 | 2008-07-09 04:45:36 +0000 | [diff] [blame] | 299 | const TargetRegisterClass |
| 300 | *RC = RI.getRegClass(MI->getOperand(0).getReg()); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 301 | unsigned StoreOpc, LoadOpc; |
| 302 | |
| 303 | if (RC == Mips::FGR32RegisterClass) { |
| 304 | LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1; |
| 305 | } else if (RC == Mips::AFGR32RegisterClass) { |
| 306 | LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A; |
| 307 | } else if (RC == Mips::AFGR64RegisterClass) { |
| 308 | LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1; |
| 309 | } else |
| 310 | assert(0 && "foldMemoryOperand register unknown"); |
| 311 | |
| 312 | if (Ops[0] == 0) { // COPY -> STORE |
| 313 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 314 | bool isKill = MI->getOperand(1).isKill(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 315 | NewMI = BuildMI(MF, get(StoreOpc)).addFrameIndex(FI) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 316 | .addImm(0).addReg(SrcReg, false, false, isKill); |
| 317 | } else { // COPY -> LOAD |
| 318 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 319 | bool isDead = MI->getOperand(0).isDead(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 320 | NewMI = BuildMI(MF, get(LoadOpc)) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 321 | .addReg(DstReg, true, false, false, isDead) |
| 322 | .addImm(0).addFrameIndex(FI); |
| 323 | } |
| 324 | } |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | return NewMI; |
| 329 | } |
| 330 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 331 | //===----------------------------------------------------------------------===// |
| 332 | // Branch Analysis |
| 333 | //===----------------------------------------------------------------------===// |
| 334 | |
| 335 | /// GetCondFromBranchOpc - Return the Mips CC that matches |
| 336 | /// the correspondent Branch instruction opcode. |
| 337 | static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc) |
| 338 | { |
| 339 | switch (BrOpc) { |
| 340 | default: return Mips::COND_INVALID; |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame^] | 341 | case Mips::BEQ : return Mips::COND_E; |
| 342 | case Mips::BNE : return Mips::COND_NE; |
| 343 | case Mips::BGTZ : return Mips::COND_GZ; |
| 344 | case Mips::BGEZ : return Mips::COND_GEZ; |
| 345 | case Mips::BLTZ : return Mips::COND_LZ; |
| 346 | case Mips::BLEZ : return Mips::COND_LEZ; |
| 347 | |
| 348 | // We dont do fp branch analysis yet! |
| 349 | case Mips::BC1T : |
| 350 | case Mips::BC1F : return Mips::COND_INVALID; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | /// GetCondBranchFromCond - Return the Branch instruction |
| 355 | /// opcode that matches the cc. |
| 356 | unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC) |
| 357 | { |
| 358 | switch (CC) { |
| 359 | default: assert(0 && "Illegal condition code!"); |
| 360 | case Mips::COND_E : return Mips::BEQ; |
| 361 | case Mips::COND_NE : return Mips::BNE; |
| 362 | case Mips::COND_GZ : return Mips::BGTZ; |
| 363 | case Mips::COND_GEZ : return Mips::BGEZ; |
| 364 | case Mips::COND_LZ : return Mips::BLTZ; |
| 365 | case Mips::COND_LEZ : return Mips::BLEZ; |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame^] | 366 | |
| 367 | case Mips::FCOND_F: |
| 368 | case Mips::FCOND_UN: |
| 369 | case Mips::FCOND_EQ: |
| 370 | case Mips::FCOND_UEQ: |
| 371 | case Mips::FCOND_OLT: |
| 372 | case Mips::FCOND_ULT: |
| 373 | case Mips::FCOND_OLE: |
| 374 | case Mips::FCOND_ULE: |
| 375 | case Mips::FCOND_SF: |
| 376 | case Mips::FCOND_NGLE: |
| 377 | case Mips::FCOND_SEQ: |
| 378 | case Mips::FCOND_NGL: |
| 379 | case Mips::FCOND_LT: |
| 380 | case Mips::FCOND_NGE: |
| 381 | case Mips::FCOND_LE: |
| 382 | case Mips::FCOND_NGT: return Mips::BC1T; |
| 383 | |
| 384 | case Mips::FCOND_T: |
| 385 | case Mips::FCOND_OR: |
| 386 | case Mips::FCOND_NEQ: |
| 387 | case Mips::FCOND_OGL: |
| 388 | case Mips::FCOND_UGE: |
| 389 | case Mips::FCOND_OGE: |
| 390 | case Mips::FCOND_UGT: |
| 391 | case Mips::FCOND_OGT: |
| 392 | case Mips::FCOND_ST: |
| 393 | case Mips::FCOND_GLE: |
| 394 | case Mips::FCOND_SNE: |
| 395 | case Mips::FCOND_GL: |
| 396 | case Mips::FCOND_NLT: |
| 397 | case Mips::FCOND_GE: |
| 398 | case Mips::FCOND_NLE: |
| 399 | case Mips::FCOND_GT: return Mips::BC1F; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | /// GetOppositeBranchCondition - Return the inverse of the specified |
| 404 | /// condition, e.g. turning COND_E to COND_NE. |
| 405 | Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC) |
| 406 | { |
| 407 | switch (CC) { |
| 408 | default: assert(0 && "Illegal condition code!"); |
| 409 | case Mips::COND_E : return Mips::COND_NE; |
| 410 | case Mips::COND_NE : return Mips::COND_E; |
| 411 | case Mips::COND_GZ : return Mips::COND_LEZ; |
| 412 | case Mips::COND_GEZ : return Mips::COND_LZ; |
| 413 | case Mips::COND_LZ : return Mips::COND_GEZ; |
| 414 | case Mips::COND_LEZ : return Mips::COND_GZ; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 415 | case Mips::FCOND_F : return Mips::FCOND_T; |
| 416 | case Mips::FCOND_UN : return Mips::FCOND_OR; |
| 417 | case Mips::FCOND_EQ : return Mips::FCOND_NEQ; |
| 418 | case Mips::FCOND_UEQ: return Mips::FCOND_OGL; |
| 419 | case Mips::FCOND_OLT: return Mips::FCOND_UGE; |
| 420 | case Mips::FCOND_ULT: return Mips::FCOND_OGE; |
| 421 | case Mips::FCOND_OLE: return Mips::FCOND_UGT; |
| 422 | case Mips::FCOND_ULE: return Mips::FCOND_OGT; |
| 423 | case Mips::FCOND_SF: return Mips::FCOND_ST; |
| 424 | case Mips::FCOND_NGLE:return Mips::FCOND_GLE; |
| 425 | case Mips::FCOND_SEQ: return Mips::FCOND_SNE; |
| 426 | case Mips::FCOND_NGL: return Mips::FCOND_GL; |
| 427 | case Mips::FCOND_LT: return Mips::FCOND_NLT; |
| 428 | case Mips::FCOND_NGE: return Mips::FCOND_GE; |
| 429 | case Mips::FCOND_LE: return Mips::FCOND_NLE; |
| 430 | case Mips::FCOND_NGT: return Mips::FCOND_GT; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 435 | MachineBasicBlock *&TBB, |
| 436 | MachineBasicBlock *&FBB, |
| 437 | std::vector<MachineOperand> &Cond) const |
| 438 | { |
| 439 | // If the block has no terminators, it just falls into the block after it. |
| 440 | MachineBasicBlock::iterator I = MBB.end(); |
| 441 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) |
| 442 | return false; |
| 443 | |
| 444 | // Get the last instruction in the block. |
| 445 | MachineInstr *LastInst = I; |
| 446 | |
| 447 | // If there is only one terminator instruction, process it. |
| 448 | unsigned LastOpc = LastInst->getOpcode(); |
| 449 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 450 | if (!LastInst->getDesc().isBranch()) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 451 | return true; |
| 452 | |
| 453 | // Unconditional branch |
| 454 | if (LastOpc == Mips::J) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 455 | TBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
| 459 | Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode()); |
| 460 | if (BranchCode == Mips::COND_INVALID) |
| 461 | return true; // Can't handle indirect branch. |
| 462 | |
| 463 | // Conditional branch |
| 464 | // Block ends with fall-through condbranch. |
| 465 | if (LastOpc != Mips::COND_INVALID) { |
| 466 | int LastNumOp = LastInst->getNumOperands(); |
| 467 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 468 | TBB = LastInst->getOperand(LastNumOp-1).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 469 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 470 | |
| 471 | for (int i=0; i<LastNumOp-1; i++) { |
| 472 | Cond.push_back(LastInst->getOperand(i)); |
| 473 | } |
| 474 | |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Get the instruction before it if it is a terminator. |
| 480 | MachineInstr *SecondLastInst = I; |
| 481 | |
| 482 | // If there are three terminators, we don't know what sort of block this is. |
| 483 | if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I)) |
| 484 | return true; |
| 485 | |
| 486 | // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it. |
| 487 | unsigned SecondLastOpc = SecondLastInst->getOpcode(); |
| 488 | Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc); |
| 489 | |
| 490 | if (SecondLastOpc != Mips::COND_INVALID && LastOpc == Mips::J) { |
| 491 | int SecondNumOp = SecondLastInst->getNumOperands(); |
| 492 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 493 | TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 494 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 495 | |
| 496 | for (int i=0; i<SecondNumOp-1; i++) { |
| 497 | Cond.push_back(SecondLastInst->getOperand(i)); |
| 498 | } |
| 499 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 500 | FBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 501 | return false; |
| 502 | } |
| 503 | |
| 504 | // If the block ends with two unconditional branches, handle it. The last |
| 505 | // one is not executed, so remove it. |
| 506 | if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 507 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 508 | I = LastInst; |
| 509 | I->eraseFromParent(); |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | // Otherwise, can't handle this. |
| 514 | return true; |
| 515 | } |
| 516 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 517 | unsigned MipsInstrInfo:: |
| 518 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 519 | MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond) |
| 520 | const |
| 521 | { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 522 | // Shouldn't be a fall through. |
| 523 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 524 | assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) && |
| 525 | "Mips branch conditions can have two|three components!"); |
| 526 | |
| 527 | if (FBB == 0) { // One way branch. |
| 528 | if (Cond.empty()) { |
| 529 | // Unconditional branch? |
| 530 | BuildMI(&MBB, get(Mips::J)).addMBB(TBB); |
| 531 | } else { |
| 532 | // Conditional branch. |
| 533 | unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm()); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 534 | const TargetInstrDesc &TID = get(Opc); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 536 | if (TID.getNumOperands() == 3) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 537 | BuildMI(&MBB, TID).addReg(Cond[1].getReg()) |
| 538 | .addReg(Cond[2].getReg()) |
| 539 | .addMBB(TBB); |
| 540 | else |
| 541 | BuildMI(&MBB, TID).addReg(Cond[1].getReg()) |
| 542 | .addMBB(TBB); |
| 543 | |
| 544 | } |
| 545 | return 1; |
| 546 | } |
| 547 | |
| 548 | // Two-way Conditional branch. |
| 549 | unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm()); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 550 | const TargetInstrDesc &TID = get(Opc); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 552 | if (TID.getNumOperands() == 3) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 553 | BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg()) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 554 | .addMBB(TBB); |
| 555 | else |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 556 | BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 557 | |
| 558 | BuildMI(&MBB, get(Mips::J)).addMBB(FBB); |
| 559 | return 2; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 560 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 561 | |
| 562 | unsigned MipsInstrInfo:: |
| 563 | RemoveBranch(MachineBasicBlock &MBB) const |
| 564 | { |
| 565 | MachineBasicBlock::iterator I = MBB.end(); |
| 566 | if (I == MBB.begin()) return 0; |
| 567 | --I; |
| 568 | if (I->getOpcode() != Mips::J && |
| 569 | GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID) |
| 570 | return 0; |
| 571 | |
| 572 | // Remove the branch. |
| 573 | I->eraseFromParent(); |
| 574 | |
| 575 | I = MBB.end(); |
| 576 | |
| 577 | if (I == MBB.begin()) return 1; |
| 578 | --I; |
| 579 | if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID) |
| 580 | return 1; |
| 581 | |
| 582 | // Remove the branch. |
| 583 | I->eraseFromParent(); |
| 584 | return 2; |
| 585 | } |
| 586 | |
| 587 | /// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not |
| 588 | /// fall-through into its successor block. |
| 589 | bool MipsInstrInfo:: |
| 590 | BlockHasNoFallThrough(MachineBasicBlock &MBB) const |
| 591 | { |
| 592 | if (MBB.empty()) return false; |
| 593 | |
| 594 | switch (MBB.back().getOpcode()) { |
| 595 | case Mips::RET: // Return. |
| 596 | case Mips::JR: // Indirect branch. |
| 597 | case Mips::J: // Uncond branch. |
| 598 | return true; |
| 599 | default: return false; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /// ReverseBranchCondition - Return the inverse opcode of the |
| 604 | /// specified Branch instruction. |
| 605 | bool MipsInstrInfo:: |
| 606 | ReverseBranchCondition(std::vector<MachineOperand> &Cond) const |
| 607 | { |
| 608 | assert( (Cond.size() == 3 || Cond.size() == 2) && |
| 609 | "Invalid Mips branch condition!"); |
| 610 | Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm())); |
| 611 | return false; |
| 612 | } |