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