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