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