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