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 | bdfbb74 | 2009-03-21 00:05:07 +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 |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 55 | if (MI.getOpcode() == Mips::FMOV_S32 || |
| 56 | MI.getOpcode() == Mips::FMOV_D32 || |
| 57 | MI.getOpcode() == Mips::MFC1 || |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 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 | bdfbb74 | 2009-03-21 00:05:07 +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) || |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 84 | (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) || |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 105 | (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 | { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 121 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 122 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 123 | BuildMI(MBB, MI, DL, get(Mips::NOP)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame] | 126 | bool MipsInstrInfo:: |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 127 | copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 128 | unsigned DestReg, unsigned SrcReg, |
| 129 | const TargetRegisterClass *DestRC, |
| 130 | const TargetRegisterClass *SrcRC) const { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 131 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 132 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 133 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 134 | if (DestRC != SrcRC) { |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 135 | // Moves between coprocessors and cpu |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 136 | if ((DestRC == Mips::CPURegsRegisterClass) && |
| 137 | (SrcRC == Mips::FGR32RegisterClass)) |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 138 | BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg).addReg(SrcReg); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 139 | else if ((DestRC == Mips::FGR32RegisterClass) && |
| 140 | (SrcRC == Mips::CPURegsRegisterClass)) |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 141 | BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg).addReg(SrcReg); |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 142 | |
| 143 | // Condition registers |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 144 | else if ((SrcRC == Mips::CCRRegisterClass) && |
| 145 | (SrcReg == Mips::FCR31)) |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame] | 146 | return true; // This register is used implicitly, no copy needed. |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 147 | else if ((DestRC == Mips::CCRRegisterClass) && |
| 148 | (DestReg == Mips::FCR31)) |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame] | 149 | return true; // This register is used implicitly, no copy needed. |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 150 | |
| 151 | // Move from/to Hi/Lo registers |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 152 | else if ((DestRC == Mips::HILORegisterClass) && |
| 153 | (SrcRC == Mips::CPURegsRegisterClass)) { |
| 154 | unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 155 | BuildMI(MBB, I, DL, get(Opc), DestReg); |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 156 | } else if ((SrcRC == Mips::HILORegisterClass) && |
| 157 | (DestRC == Mips::CPURegsRegisterClass)) { |
| 158 | unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 159 | BuildMI(MBB, I, DL, get(Opc), DestReg); |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 160 | |
| 161 | // Can't copy this register |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 162 | } else |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 163 | return false; |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 164 | |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame] | 165 | return true; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (DestRC == Mips::CPURegsRegisterClass) |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 169 | BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 170 | .addReg(SrcReg); |
| 171 | else if (DestRC == Mips::FGR32RegisterClass) |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 172 | BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg).addReg(SrcReg); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 173 | else if (DestRC == Mips::AFGR64RegisterClass) |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 174 | BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg).addReg(SrcReg); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 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, |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 184 | unsigned SrcReg, bool isKill, int FI, |
| 185 | const TargetRegisterClass *RC) const |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 186 | { |
| 187 | unsigned Opc; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 188 | |
| 189 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 190 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 191 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 192 | if (RC == Mips::CPURegsRegisterClass) |
| 193 | Opc = Mips::SW; |
| 194 | else if (RC == Mips::FGR32RegisterClass) |
| 195 | Opc = Mips::SWC1; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 196 | else if (RC == Mips::AFGR64RegisterClass) |
| 197 | Opc = Mips::SDC1; |
| 198 | else |
| 199 | assert(0 && "Can't store this register to stack slot"); |
| 200 | |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 201 | BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, false, false, isKill) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 202 | .addImm(0).addFrameIndex(FI); |
| 203 | } |
| 204 | |
| 205 | void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg, |
| 206 | bool isKill, SmallVectorImpl<MachineOperand> &Addr, |
| 207 | const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const |
| 208 | { |
| 209 | unsigned Opc; |
| 210 | if (RC == Mips::CPURegsRegisterClass) |
| 211 | Opc = Mips::SW; |
| 212 | else if (RC == Mips::FGR32RegisterClass) |
| 213 | Opc = Mips::SWC1; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 214 | else if (RC == Mips::AFGR64RegisterClass) |
| 215 | Opc = Mips::SDC1; |
| 216 | else |
| 217 | assert(0 && "Can't store this register"); |
| 218 | |
Dale Johannesen | 21b5541 | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 219 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 220 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc)) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 221 | .addReg(SrcReg, false, false, isKill); |
Dan Gohman | 9735761 | 2009-02-18 05:45:50 +0000 | [diff] [blame] | 222 | for (unsigned i = 0, e = Addr.size(); i != e; ++i) |
| 223 | MIB.addOperand(Addr[i]); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 224 | NewMIs.push_back(MIB); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | void MipsInstrInfo:: |
| 229 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 230 | unsigned DestReg, int FI, |
| 231 | const TargetRegisterClass *RC) const |
| 232 | { |
| 233 | unsigned Opc; |
| 234 | if (RC == Mips::CPURegsRegisterClass) |
| 235 | Opc = Mips::LW; |
| 236 | else if (RC == Mips::FGR32RegisterClass) |
| 237 | Opc = Mips::LWC1; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 238 | else if (RC == Mips::AFGR64RegisterClass) |
| 239 | Opc = Mips::LDC1; |
| 240 | else |
| 241 | assert(0 && "Can't load this register from stack slot"); |
| 242 | |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 243 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 244 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 245 | BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0).addFrameIndex(FI); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg, |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 249 | SmallVectorImpl<MachineOperand> &Addr, |
| 250 | const TargetRegisterClass *RC, |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 251 | SmallVectorImpl<MachineInstr*> &NewMIs) const { |
| 252 | unsigned Opc; |
| 253 | if (RC == Mips::CPURegsRegisterClass) |
| 254 | Opc = Mips::LW; |
| 255 | else if (RC == Mips::FGR32RegisterClass) |
| 256 | Opc = Mips::LWC1; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 257 | else if (RC == Mips::AFGR64RegisterClass) |
| 258 | Opc = Mips::LDC1; |
| 259 | else |
| 260 | assert(0 && "Can't load this register"); |
| 261 | |
Dale Johannesen | 21b5541 | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 262 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 263 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg); |
Dan Gohman | 9735761 | 2009-02-18 05:45:50 +0000 | [diff] [blame] | 264 | for (unsigned i = 0, e = Addr.size(); i != e; ++i) |
| 265 | MIB.addOperand(Addr[i]); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 266 | NewMIs.push_back(MIB); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | MachineInstr *MipsInstrInfo:: |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 271 | foldMemoryOperandImpl(MachineFunction &MF, |
| 272 | MachineInstr* MI, |
| 273 | const SmallVectorImpl<unsigned> &Ops, int FI) const |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 274 | { |
| 275 | if (Ops.size() != 1) return NULL; |
| 276 | |
| 277 | MachineInstr *NewMI = NULL; |
| 278 | |
| 279 | switch (MI->getOpcode()) { |
| 280 | case Mips::ADDu: |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 281 | if ((MI->getOperand(0).isReg()) && |
| 282 | (MI->getOperand(1).isReg()) && |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 283 | (MI->getOperand(1).getReg() == Mips::ZERO) && |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 284 | (MI->getOperand(2).isReg())) { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 285 | if (Ops[0] == 0) { // COPY -> STORE |
| 286 | unsigned SrcReg = MI->getOperand(2).getReg(); |
| 287 | bool isKill = MI->getOperand(2).isKill(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 288 | NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW)) |
| 289 | .addReg(SrcReg, false, false, isKill) |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 290 | .addImm(0).addFrameIndex(FI); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 291 | } else { // COPY -> LOAD |
| 292 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 293 | bool isDead = MI->getOperand(0).isDead(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 294 | NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW)) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 295 | .addReg(DstReg, true, false, false, isDead) |
| 296 | .addImm(0).addFrameIndex(FI); |
| 297 | } |
| 298 | } |
| 299 | break; |
Bruno Cardoso Lopes | bdfbb74 | 2009-03-21 00:05:07 +0000 | [diff] [blame] | 300 | case Mips::FMOV_S32: |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 301 | case Mips::FMOV_D32: |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 302 | if ((MI->getOperand(0).isReg()) && |
| 303 | (MI->getOperand(1).isReg())) { |
Bruno Cardoso Lopes | 7b76da1 | 2008-07-09 04:45:36 +0000 | [diff] [blame] | 304 | const TargetRegisterClass |
| 305 | *RC = RI.getRegClass(MI->getOperand(0).getReg()); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 306 | unsigned StoreOpc, LoadOpc; |
| 307 | |
| 308 | if (RC == Mips::FGR32RegisterClass) { |
| 309 | LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 310 | } else if (RC == Mips::AFGR64RegisterClass) { |
| 311 | LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1; |
| 312 | } else |
Dan Gohman | c54baa2 | 2008-12-03 18:43:12 +0000 | [diff] [blame] | 313 | assert(0 && "foldMemoryOperandImpl register unknown"); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 314 | |
| 315 | if (Ops[0] == 0) { // COPY -> STORE |
| 316 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 317 | bool isKill = MI->getOperand(1).isKill(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 318 | NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc)) |
| 319 | .addReg(SrcReg, false, false, isKill) |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 320 | .addImm(0).addFrameIndex(FI) ; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 321 | } else { // COPY -> LOAD |
| 322 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 323 | bool isDead = MI->getOperand(0).isDead(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 324 | NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc)) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 325 | .addReg(DstReg, true, false, false, isDead) |
| 326 | .addImm(0).addFrameIndex(FI); |
| 327 | } |
| 328 | } |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | return NewMI; |
| 333 | } |
| 334 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 335 | //===----------------------------------------------------------------------===// |
| 336 | // Branch Analysis |
| 337 | //===----------------------------------------------------------------------===// |
| 338 | |
| 339 | /// GetCondFromBranchOpc - Return the Mips CC that matches |
| 340 | /// the correspondent Branch instruction opcode. |
| 341 | static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc) |
| 342 | { |
| 343 | switch (BrOpc) { |
| 344 | default: return Mips::COND_INVALID; |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 345 | case Mips::BEQ : return Mips::COND_E; |
| 346 | case Mips::BNE : return Mips::COND_NE; |
| 347 | case Mips::BGTZ : return Mips::COND_GZ; |
| 348 | case Mips::BGEZ : return Mips::COND_GEZ; |
| 349 | case Mips::BLTZ : return Mips::COND_LZ; |
| 350 | case Mips::BLEZ : return Mips::COND_LEZ; |
| 351 | |
| 352 | // We dont do fp branch analysis yet! |
| 353 | case Mips::BC1T : |
| 354 | case Mips::BC1F : return Mips::COND_INVALID; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | /// GetCondBranchFromCond - Return the Branch instruction |
| 359 | /// opcode that matches the cc. |
| 360 | unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC) |
| 361 | { |
| 362 | switch (CC) { |
| 363 | default: assert(0 && "Illegal condition code!"); |
| 364 | case Mips::COND_E : return Mips::BEQ; |
| 365 | case Mips::COND_NE : return Mips::BNE; |
| 366 | case Mips::COND_GZ : return Mips::BGTZ; |
| 367 | case Mips::COND_GEZ : return Mips::BGEZ; |
| 368 | case Mips::COND_LZ : return Mips::BLTZ; |
| 369 | case Mips::COND_LEZ : return Mips::BLEZ; |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 370 | |
| 371 | case Mips::FCOND_F: |
| 372 | case Mips::FCOND_UN: |
| 373 | case Mips::FCOND_EQ: |
| 374 | case Mips::FCOND_UEQ: |
| 375 | case Mips::FCOND_OLT: |
| 376 | case Mips::FCOND_ULT: |
| 377 | case Mips::FCOND_OLE: |
| 378 | case Mips::FCOND_ULE: |
| 379 | case Mips::FCOND_SF: |
| 380 | case Mips::FCOND_NGLE: |
| 381 | case Mips::FCOND_SEQ: |
| 382 | case Mips::FCOND_NGL: |
| 383 | case Mips::FCOND_LT: |
| 384 | case Mips::FCOND_NGE: |
| 385 | case Mips::FCOND_LE: |
| 386 | case Mips::FCOND_NGT: return Mips::BC1T; |
| 387 | |
| 388 | case Mips::FCOND_T: |
| 389 | case Mips::FCOND_OR: |
| 390 | case Mips::FCOND_NEQ: |
| 391 | case Mips::FCOND_OGL: |
| 392 | case Mips::FCOND_UGE: |
| 393 | case Mips::FCOND_OGE: |
| 394 | case Mips::FCOND_UGT: |
| 395 | case Mips::FCOND_OGT: |
| 396 | case Mips::FCOND_ST: |
| 397 | case Mips::FCOND_GLE: |
| 398 | case Mips::FCOND_SNE: |
| 399 | case Mips::FCOND_GL: |
| 400 | case Mips::FCOND_NLT: |
| 401 | case Mips::FCOND_GE: |
| 402 | case Mips::FCOND_NLE: |
| 403 | case Mips::FCOND_GT: return Mips::BC1F; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | /// GetOppositeBranchCondition - Return the inverse of the specified |
| 408 | /// condition, e.g. turning COND_E to COND_NE. |
| 409 | Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC) |
| 410 | { |
| 411 | switch (CC) { |
| 412 | default: assert(0 && "Illegal condition code!"); |
| 413 | case Mips::COND_E : return Mips::COND_NE; |
| 414 | case Mips::COND_NE : return Mips::COND_E; |
| 415 | case Mips::COND_GZ : return Mips::COND_LEZ; |
| 416 | case Mips::COND_GEZ : return Mips::COND_LZ; |
| 417 | case Mips::COND_LZ : return Mips::COND_GEZ; |
| 418 | case Mips::COND_LEZ : return Mips::COND_GZ; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 419 | case Mips::FCOND_F : return Mips::FCOND_T; |
| 420 | case Mips::FCOND_UN : return Mips::FCOND_OR; |
| 421 | case Mips::FCOND_EQ : return Mips::FCOND_NEQ; |
| 422 | case Mips::FCOND_UEQ: return Mips::FCOND_OGL; |
| 423 | case Mips::FCOND_OLT: return Mips::FCOND_UGE; |
| 424 | case Mips::FCOND_ULT: return Mips::FCOND_OGE; |
| 425 | case Mips::FCOND_OLE: return Mips::FCOND_UGT; |
| 426 | case Mips::FCOND_ULE: return Mips::FCOND_OGT; |
| 427 | case Mips::FCOND_SF: return Mips::FCOND_ST; |
| 428 | case Mips::FCOND_NGLE:return Mips::FCOND_GLE; |
| 429 | case Mips::FCOND_SEQ: return Mips::FCOND_SNE; |
| 430 | case Mips::FCOND_NGL: return Mips::FCOND_GL; |
| 431 | case Mips::FCOND_LT: return Mips::FCOND_NLT; |
| 432 | case Mips::FCOND_NGE: return Mips::FCOND_GE; |
| 433 | case Mips::FCOND_LE: return Mips::FCOND_NLE; |
| 434 | case Mips::FCOND_NGT: return Mips::FCOND_GT; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 439 | MachineBasicBlock *&TBB, |
| 440 | MachineBasicBlock *&FBB, |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 441 | SmallVectorImpl<MachineOperand> &Cond, |
| 442 | bool AllowModify) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 443 | { |
| 444 | // If the block has no terminators, it just falls into the block after it. |
| 445 | MachineBasicBlock::iterator I = MBB.end(); |
| 446 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) |
| 447 | return false; |
| 448 | |
| 449 | // Get the last instruction in the block. |
| 450 | MachineInstr *LastInst = I; |
| 451 | |
| 452 | // If there is only one terminator instruction, process it. |
| 453 | unsigned LastOpc = LastInst->getOpcode(); |
| 454 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 455 | if (!LastInst->getDesc().isBranch()) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 456 | return true; |
| 457 | |
| 458 | // Unconditional branch |
| 459 | if (LastOpc == Mips::J) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 460 | TBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
| 464 | Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode()); |
| 465 | if (BranchCode == Mips::COND_INVALID) |
| 466 | return true; // Can't handle indirect branch. |
| 467 | |
| 468 | // Conditional branch |
| 469 | // Block ends with fall-through condbranch. |
| 470 | if (LastOpc != Mips::COND_INVALID) { |
| 471 | int LastNumOp = LastInst->getNumOperands(); |
| 472 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 473 | TBB = LastInst->getOperand(LastNumOp-1).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 474 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 475 | |
| 476 | for (int i=0; i<LastNumOp-1; i++) { |
| 477 | Cond.push_back(LastInst->getOperand(i)); |
| 478 | } |
| 479 | |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Get the instruction before it if it is a terminator. |
| 485 | MachineInstr *SecondLastInst = I; |
| 486 | |
| 487 | // If there are three terminators, we don't know what sort of block this is. |
| 488 | if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I)) |
| 489 | return true; |
| 490 | |
| 491 | // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it. |
| 492 | unsigned SecondLastOpc = SecondLastInst->getOpcode(); |
| 493 | Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc); |
| 494 | |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 495 | if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 496 | int SecondNumOp = SecondLastInst->getNumOperands(); |
| 497 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 498 | TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 499 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 500 | |
| 501 | for (int i=0; i<SecondNumOp-1; i++) { |
| 502 | Cond.push_back(SecondLastInst->getOperand(i)); |
| 503 | } |
| 504 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 505 | FBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | |
| 509 | // If the block ends with two unconditional branches, handle it. The last |
| 510 | // one is not executed, so remove it. |
| 511 | if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 512 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 513 | I = LastInst; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 514 | if (AllowModify) |
| 515 | I->eraseFromParent(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // Otherwise, can't handle this. |
| 520 | return true; |
| 521 | } |
| 522 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 523 | unsigned MipsInstrInfo:: |
| 524 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 525 | MachineBasicBlock *FBB, |
| 526 | const SmallVectorImpl<MachineOperand> &Cond) const { |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 527 | // FIXME this should probably have a DebugLoc argument |
| 528 | DebugLoc dl = DebugLoc::getUnknownLoc(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 529 | // Shouldn't be a fall through. |
| 530 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 531 | assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) && |
| 532 | "Mips branch conditions can have two|three components!"); |
| 533 | |
| 534 | if (FBB == 0) { // One way branch. |
| 535 | if (Cond.empty()) { |
| 536 | // Unconditional branch? |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 537 | BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 538 | } else { |
| 539 | // Conditional branch. |
| 540 | unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm()); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 541 | const TargetInstrDesc &TID = get(Opc); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 542 | |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 543 | if (TID.getNumOperands() == 3) |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 544 | BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 545 | .addReg(Cond[2].getReg()) |
| 546 | .addMBB(TBB); |
| 547 | else |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 548 | BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 549 | .addMBB(TBB); |
| 550 | |
| 551 | } |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | // Two-way Conditional branch. |
| 556 | unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm()); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 557 | const TargetInstrDesc &TID = get(Opc); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 559 | if (TID.getNumOperands() == 3) |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 560 | BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg()) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 561 | .addMBB(TBB); |
| 562 | else |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 563 | BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 564 | |
Dale Johannesen | 9481757 | 2009-02-13 02:34:39 +0000 | [diff] [blame] | 565 | BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 566 | return 2; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 567 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 568 | |
| 569 | unsigned MipsInstrInfo:: |
| 570 | RemoveBranch(MachineBasicBlock &MBB) const |
| 571 | { |
| 572 | MachineBasicBlock::iterator I = MBB.end(); |
| 573 | if (I == MBB.begin()) return 0; |
| 574 | --I; |
| 575 | if (I->getOpcode() != Mips::J && |
| 576 | GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID) |
| 577 | return 0; |
| 578 | |
| 579 | // Remove the branch. |
| 580 | I->eraseFromParent(); |
| 581 | |
| 582 | I = MBB.end(); |
| 583 | |
| 584 | if (I == MBB.begin()) return 1; |
| 585 | --I; |
| 586 | if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID) |
| 587 | return 1; |
| 588 | |
| 589 | // Remove the branch. |
| 590 | I->eraseFromParent(); |
| 591 | return 2; |
| 592 | } |
| 593 | |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 594 | /// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 595 | /// fall-through into its successor block. |
| 596 | bool MipsInstrInfo:: |
Dan Gohman | 8e8b8a2 | 2008-10-16 01:49:15 +0000 | [diff] [blame] | 597 | BlockHasNoFallThrough(const MachineBasicBlock &MBB) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 598 | { |
| 599 | if (MBB.empty()) return false; |
| 600 | |
| 601 | switch (MBB.back().getOpcode()) { |
| 602 | case Mips::RET: // Return. |
| 603 | case Mips::JR: // Indirect branch. |
| 604 | case Mips::J: // Uncond branch. |
| 605 | return true; |
| 606 | default: return false; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /// ReverseBranchCondition - Return the inverse opcode of the |
| 611 | /// specified Branch instruction. |
| 612 | bool MipsInstrInfo:: |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 613 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 614 | { |
| 615 | assert( (Cond.size() == 3 || Cond.size() == 2) && |
| 616 | "Invalid Mips branch condition!"); |
| 617 | Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm())); |
| 618 | return false; |
| 619 | } |