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