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