Jia Liu | c570711 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 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 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains the Mips implementation of the TargetInstrInfo class. |
| 11 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
Akira Hatanaka | d4b48b2 | 2012-06-14 01:16:45 +0000 | [diff] [blame^] | 14 | #include "MipsAnalyzeImmediate.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 15 | #include "MipsInstrInfo.h" |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 16 | #include "MipsTargetMachine.h" |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 17 | #include "MipsMachineFunction.h" |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 18 | #include "InstPrinter/MipsInstPrinter.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Evan Cheng | 22fee2d | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 24 | |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 25 | #define GET_INSTRINFO_CTOR |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 26 | #include "MipsGenInstrInfo.inc" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 30 | MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm) |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 31 | : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP), |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 32 | TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()), |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 33 | RI(*TM.getSubtargetImpl(), *this), |
| 34 | UncondBrOpc(TM.getRelocationModel() == Reloc::PIC_ ? Mips::B : Mips::J) {} |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 35 | |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 36 | const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const { |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 37 | return RI; |
| 38 | } |
| 39 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 40 | static bool isZeroImm(const MachineOperand &op) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 41 | return op.isImm() && op.getImm() == 0; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 44 | /// isLoadFromStackSlot - If the specified machine instruction is a direct |
| 45 | /// load from a stack slot, return the virtual or physical register number of |
| 46 | /// the destination along with the FrameIndex of the loaded stack slot. If |
| 47 | /// not, return 0. This predicate must return 0 if the instruction has |
| 48 | /// any side effects other than loading from the stack slot. |
| 49 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 50 | isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 51 | { |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame] | 52 | unsigned Opc = MI->getOpcode(); |
| 53 | |
| 54 | if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) || |
| 55 | (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) || |
| 56 | (Opc == Mips::LDC1) || (Opc == Mips::LDC164) || |
| 57 | (Opc == Mips::LDC164_P8)) { |
Akira Hatanaka | d3ac47f | 2011-07-07 18:57:00 +0000 | [diff] [blame] | 58 | if ((MI->getOperand(1).isFI()) && // is a stack slot |
| 59 | (MI->getOperand(2).isImm()) && // the imm is zero |
| 60 | (isZeroImm(MI->getOperand(2)))) { |
| 61 | FrameIndex = MI->getOperand(1).getIndex(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 62 | return MI->getOperand(0).getReg(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /// isStoreToStackSlot - If the specified machine instruction is a direct |
| 70 | /// store to a stack slot, return the virtual or physical register number of |
| 71 | /// the source reg along with the FrameIndex of the loaded stack slot. If |
| 72 | /// not, return 0. This predicate must return 0 if the instruction has |
| 73 | /// any side effects other than storing to the stack slot. |
| 74 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 75 | isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 76 | { |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame] | 77 | unsigned Opc = MI->getOpcode(); |
| 78 | |
| 79 | if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) || |
| 80 | (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) || |
| 81 | (Opc == Mips::SDC1) || (Opc == Mips::SDC164) || |
| 82 | (Opc == Mips::SDC164_P8)) { |
Akira Hatanaka | d3ac47f | 2011-07-07 18:57:00 +0000 | [diff] [blame] | 83 | if ((MI->getOperand(1).isFI()) && // is a stack slot |
| 84 | (MI->getOperand(2).isImm()) && // the imm is zero |
| 85 | (isZeroImm(MI->getOperand(2)))) { |
| 86 | FrameIndex = MI->getOperand(1).getIndex(); |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame] | 87 | return MI->getOperand(0).getReg(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 93 | /// insertNoop - If data hazard condition is found insert the target nop |
| 94 | /// instruction. |
| 95 | void MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 96 | insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 97 | { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 98 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 99 | BuildMI(MBB, MI, DL, get(Mips::NOP)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 102 | void MipsInstrInfo:: |
| 103 | copyPhysReg(MachineBasicBlock &MBB, |
| 104 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 105 | unsigned DestReg, unsigned SrcReg, |
| 106 | bool KillSrc) const { |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 107 | unsigned Opc = 0, ZeroReg = 0; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 108 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 109 | if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg. |
| 110 | if (Mips::CPURegsRegClass.contains(SrcReg)) |
| 111 | Opc = Mips::ADDu, ZeroReg = Mips::ZERO; |
| 112 | else if (Mips::CCRRegClass.contains(SrcReg)) |
| 113 | Opc = Mips::CFC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 114 | else if (Mips::FGR32RegClass.contains(SrcReg)) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 115 | Opc = Mips::MFC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 116 | else if (SrcReg == Mips::HI) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 117 | Opc = Mips::MFHI, SrcReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 118 | else if (SrcReg == Mips::LO) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 119 | Opc = Mips::MFLO, SrcReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 120 | } |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 121 | else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg. |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 122 | if (Mips::CCRRegClass.contains(DestReg)) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 123 | Opc = Mips::CTC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 124 | else if (Mips::FGR32RegClass.contains(DestReg)) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 125 | Opc = Mips::MTC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 126 | else if (DestReg == Mips::HI) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 127 | Opc = Mips::MTHI, DestReg = 0; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 128 | else if (DestReg == Mips::LO) |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 129 | Opc = Mips::MTLO, DestReg = 0; |
| 130 | } |
| 131 | else if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) |
Akira Hatanaka | 4391bb7 | 2011-10-08 03:50:18 +0000 | [diff] [blame] | 132 | Opc = Mips::FMOV_S; |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 133 | else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) |
| 134 | Opc = Mips::FMOV_D32; |
Akira Hatanaka | 29d525a | 2011-11-07 21:35:45 +0000 | [diff] [blame] | 135 | else if (Mips::FGR64RegClass.contains(DestReg, SrcReg)) |
| 136 | Opc = Mips::FMOV_D64; |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 137 | else if (Mips::CCRRegClass.contains(DestReg, SrcReg)) |
| 138 | Opc = Mips::MOVCCRToCCR; |
| 139 | else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg. |
| 140 | if (Mips::CPU64RegsRegClass.contains(SrcReg)) |
| 141 | Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64; |
| 142 | else if (SrcReg == Mips::HI64) |
| 143 | Opc = Mips::MFHI64, SrcReg = 0; |
| 144 | else if (SrcReg == Mips::LO64) |
| 145 | Opc = Mips::MFLO64, SrcReg = 0; |
Akira Hatanaka | 29d525a | 2011-11-07 21:35:45 +0000 | [diff] [blame] | 146 | else if (Mips::FGR64RegClass.contains(SrcReg)) |
| 147 | Opc = Mips::DMFC1; |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 148 | } |
| 149 | else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg. |
| 150 | if (DestReg == Mips::HI64) |
| 151 | Opc = Mips::MTHI64, DestReg = 0; |
| 152 | else if (DestReg == Mips::LO64) |
| 153 | Opc = Mips::MTLO64, DestReg = 0; |
Akira Hatanaka | 29d525a | 2011-11-07 21:35:45 +0000 | [diff] [blame] | 154 | else if (Mips::FGR64RegClass.contains(DestReg)) |
| 155 | Opc = Mips::DMTC1; |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 158 | assert(Opc && "Cannot copy registers"); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 159 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 160 | MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc)); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 161 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 162 | if (DestReg) |
| 163 | MIB.addReg(DestReg, RegState::Define); |
Jakob Stoklund Olesen | 273c14f | 2010-07-11 01:08:31 +0000 | [diff] [blame] | 164 | |
Akira Hatanaka | 2ad7668 | 2011-10-03 20:38:08 +0000 | [diff] [blame] | 165 | if (ZeroReg) |
| 166 | MIB.addReg(ZeroReg); |
| 167 | |
| 168 | if (SrcReg) |
| 169 | MIB.addReg(SrcReg, getKillRegState(KillSrc)); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 172 | static MachineMemOperand* GetMemOperand(MachineBasicBlock &MBB, int FI, |
| 173 | unsigned Flag) { |
| 174 | MachineFunction &MF = *MBB.getParent(); |
| 175 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 176 | unsigned Align = MFI.getObjectAlignment(FI); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 177 | |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 178 | return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag, |
| 179 | MFI.getObjectSize(FI), Align); |
| 180 | } |
| 181 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 182 | void MipsInstrInfo:: |
| 183 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 184 | unsigned SrcReg, bool isKill, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 185 | const TargetRegisterClass *RC, |
| 186 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 187 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 188 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 189 | MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore); |
| 190 | |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 191 | unsigned Opc = 0; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 192 | |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 193 | if (Mips::CPURegsRegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 194 | Opc = IsN64 ? Mips::SW_P8 : Mips::SW; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 195 | else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 196 | Opc = IsN64 ? Mips::SD_P8 : Mips::SD; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 197 | else if (Mips::FGR32RegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame] | 198 | Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 199 | else if (Mips::AFGR64RegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 200 | Opc = Mips::SDC1; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 201 | else if (Mips::FGR64RegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame] | 202 | Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164; |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 203 | |
| 204 | assert(Opc && "Register class not handled!"); |
| 205 | BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)) |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 206 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 209 | void MipsInstrInfo:: |
| 210 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 211 | unsigned DestReg, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 212 | const TargetRegisterClass *RC, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 213 | const TargetRegisterInfo *TRI) const |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 214 | { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 215 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 216 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 217 | MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad); |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 218 | unsigned Opc = 0; |
Bruno Cardoso Lopes | 302525b | 2009-11-25 00:36:00 +0000 | [diff] [blame] | 219 | |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 220 | if (Mips::CPURegsRegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 221 | Opc = IsN64 ? Mips::LW_P8 : Mips::LW; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 222 | else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 223 | Opc = IsN64 ? Mips::LD_P8 : Mips::LD; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 224 | else if (Mips::FGR32RegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame] | 225 | Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 226 | else if (Mips::AFGR64RegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 227 | Opc = Mips::LDC1; |
Akira Hatanaka | 66e19c3 | 2012-05-16 22:19:56 +0000 | [diff] [blame] | 228 | else if (Mips::FGR64RegClass.hasSubClassEq(RC)) |
Akira Hatanaka | 1acb7df | 2011-10-11 01:12:52 +0000 | [diff] [blame] | 229 | Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164; |
Akira Hatanaka | 43aed32 | 2011-10-11 00:37:28 +0000 | [diff] [blame] | 230 | |
| 231 | assert(Opc && "Register class not handled!"); |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 232 | BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0) |
| 233 | .addMemOperand(MMO); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Akira Hatanaka | 564f690 | 2012-05-25 20:52:52 +0000 | [diff] [blame] | 236 | void MipsInstrInfo::ExpandExtractElementF64(MachineBasicBlock &MBB, |
| 237 | MachineBasicBlock::iterator I) const { |
| 238 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 239 | unsigned DstReg = I->getOperand(0).getReg(); |
| 240 | unsigned SrcReg = I->getOperand(1).getReg(); |
| 241 | unsigned N = I->getOperand(2).getImm(); |
| 242 | const MCInstrDesc& Mfc1Tdd = TII->get(Mips::MFC1); |
| 243 | DebugLoc dl = I->getDebugLoc(); |
Akira Hatanaka | 564f690 | 2012-05-25 20:52:52 +0000 | [diff] [blame] | 244 | |
Jakob Stoklund Olesen | 6c82382 | 2012-05-30 18:40:49 +0000 | [diff] [blame] | 245 | assert(N < 2 && "Invalid immediate"); |
| 246 | unsigned SubIdx = N ? Mips::sub_fpodd : Mips::sub_fpeven; |
| 247 | unsigned SubReg = TM.getRegisterInfo()->getSubReg(SrcReg, SubIdx); |
| 248 | |
| 249 | BuildMI(MBB, I, dl, Mfc1Tdd, DstReg).addReg(SubReg); |
Akira Hatanaka | 564f690 | 2012-05-25 20:52:52 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void MipsInstrInfo::ExpandBuildPairF64(MachineBasicBlock &MBB, |
| 253 | MachineBasicBlock::iterator I) const { |
| 254 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 255 | unsigned DstReg = I->getOperand(0).getReg(); |
| 256 | unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg(); |
| 257 | const MCInstrDesc& Mtc1Tdd = TII->get(Mips::MTC1); |
| 258 | DebugLoc dl = I->getDebugLoc(); |
Jakob Stoklund Olesen | 6c82382 | 2012-05-30 18:40:49 +0000 | [diff] [blame] | 259 | const TargetRegisterInfo *TRI = TM.getRegisterInfo(); |
Akira Hatanaka | 564f690 | 2012-05-25 20:52:52 +0000 | [diff] [blame] | 260 | |
| 261 | // mtc1 Lo, $fp |
| 262 | // mtc1 Hi, $fp + 1 |
Jakob Stoklund Olesen | 6c82382 | 2012-05-30 18:40:49 +0000 | [diff] [blame] | 263 | BuildMI(MBB, I, dl, Mtc1Tdd, TRI->getSubReg(DstReg, Mips::sub_fpeven)) |
| 264 | .addReg(LoReg); |
| 265 | BuildMI(MBB, I, dl, Mtc1Tdd, TRI->getSubReg(DstReg, Mips::sub_fpodd)) |
| 266 | .addReg(HiReg); |
Akira Hatanaka | 564f690 | 2012-05-25 20:52:52 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | bool MipsInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const { |
| 270 | MachineBasicBlock &MBB = *MI->getParent(); |
| 271 | |
| 272 | switch(MI->getDesc().getOpcode()) { |
| 273 | default: |
| 274 | return false; |
| 275 | case Mips::BuildPairF64: |
| 276 | ExpandBuildPairF64(MBB, MI); |
| 277 | break; |
| 278 | case Mips::ExtractElementF64: |
| 279 | ExpandExtractElementF64(MBB, MI); |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | MBB.erase(MI); |
| 284 | return true; |
| 285 | } |
| 286 | |
Akira Hatanaka | c4f24eb | 2011-07-01 01:04:43 +0000 | [diff] [blame] | 287 | MachineInstr* |
| 288 | MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx, |
| 289 | uint64_t Offset, const MDNode *MDPtr, |
| 290 | DebugLoc DL) const { |
| 291 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE)) |
| 292 | .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr); |
| 293 | return &*MIB; |
| 294 | } |
| 295 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 296 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 297 | // Branch Analysis |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 298 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 299 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 300 | static unsigned GetAnalyzableBrOpc(unsigned Opc) { |
Akira Hatanaka | 3e3427a | 2011-10-11 18:49:17 +0000 | [diff] [blame] | 301 | return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ || |
| 302 | Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ || |
| 303 | Opc == Mips::BEQ64 || Opc == Mips::BNE64 || Opc == Mips::BGTZ64 || |
| 304 | Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 || |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 305 | Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::B || |
| 306 | Opc == Mips::J) ? |
Akira Hatanaka | 3e3427a | 2011-10-11 18:49:17 +0000 | [diff] [blame] | 307 | Opc : 0; |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 308 | } |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 309 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 310 | /// GetOppositeBranchOpc - Return the inverse of the specified |
| 311 | /// opcode, e.g. turning BEQ to BNE. |
| 312 | unsigned Mips::GetOppositeBranchOpc(unsigned Opc) |
| 313 | { |
| 314 | switch (Opc) { |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 315 | default: llvm_unreachable("Illegal opcode!"); |
| 316 | case Mips::BEQ: return Mips::BNE; |
| 317 | case Mips::BNE: return Mips::BEQ; |
| 318 | case Mips::BGTZ: return Mips::BLEZ; |
| 319 | case Mips::BGEZ: return Mips::BLTZ; |
| 320 | case Mips::BLTZ: return Mips::BGEZ; |
| 321 | case Mips::BLEZ: return Mips::BGTZ; |
| 322 | case Mips::BEQ64: return Mips::BNE64; |
| 323 | case Mips::BNE64: return Mips::BEQ64; |
| 324 | case Mips::BGTZ64: return Mips::BLEZ64; |
| 325 | case Mips::BGEZ64: return Mips::BLTZ64; |
| 326 | case Mips::BLTZ64: return Mips::BGEZ64; |
| 327 | case Mips::BLEZ64: return Mips::BGTZ64; |
| 328 | case Mips::BC1T: return Mips::BC1F; |
| 329 | case Mips::BC1F: return Mips::BC1T; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 333 | static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc, |
| 334 | MachineBasicBlock *&BB, |
| 335 | SmallVectorImpl<MachineOperand>& Cond) { |
| 336 | assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch"); |
| 337 | int NumOp = Inst->getNumExplicitOperands(); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 338 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 339 | // for both int and fp branches, the last explicit operand is the |
| 340 | // MBB. |
| 341 | BB = Inst->getOperand(NumOp-1).getMBB(); |
| 342 | Cond.push_back(MachineOperand::CreateImm(Opc)); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 343 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 344 | for (int i=0; i<NumOp-1; i++) |
| 345 | Cond.push_back(Inst->getOperand(i)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 348 | bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 349 | MachineBasicBlock *&TBB, |
| 350 | MachineBasicBlock *&FBB, |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 351 | SmallVectorImpl<MachineOperand> &Cond, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 352 | bool AllowModify) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 353 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 354 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 355 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 356 | // Skip all the debug instructions. |
| 357 | while (I != REnd && I->isDebugValue()) |
| 358 | ++I; |
| 359 | |
| 360 | if (I == REnd || !isUnpredicatedTerminator(&*I)) { |
| 361 | // If this block ends with no branches (it just falls through to its succ) |
| 362 | // just return false, leaving TBB/FBB null. |
| 363 | TBB = FBB = NULL; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | MachineInstr *LastInst = &*I; |
| 368 | unsigned LastOpc = LastInst->getOpcode(); |
| 369 | |
| 370 | // Not an analyzable branch (must be an indirect jump). |
| 371 | if (!GetAnalyzableBrOpc(LastOpc)) |
| 372 | return true; |
| 373 | |
| 374 | // Get the second to last instruction in the block. |
| 375 | unsigned SecondLastOpc = 0; |
| 376 | MachineInstr *SecondLastInst = NULL; |
| 377 | |
| 378 | if (++I != REnd) { |
| 379 | SecondLastInst = &*I; |
| 380 | SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode()); |
| 381 | |
| 382 | // Not an analyzable branch (must be an indirect jump). |
| 383 | if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc) |
| 384 | return true; |
| 385 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 386 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 387 | // If there is only one terminator instruction, process it. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 388 | if (!SecondLastOpc) { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 389 | // Unconditional branch |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 390 | if (LastOpc == UncondBrOpc) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 391 | TBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 395 | // Conditional branch |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 396 | AnalyzeCondBr(LastInst, LastOpc, TBB, Cond); |
| 397 | return false; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 398 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 399 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 400 | // If we reached here, there are two branches. |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 401 | // If there are three terminators, we don't know what sort of block this is. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 402 | if (++I != REnd && isUnpredicatedTerminator(&*I)) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 403 | return true; |
| 404 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 405 | // If second to last instruction is an unconditional branch, |
| 406 | // analyze it and remove the last instruction. |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 407 | if (SecondLastOpc == UncondBrOpc) { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 408 | // Return if the last instruction cannot be removed. |
| 409 | if (!AllowModify) |
| 410 | return true; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 412 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 413 | LastInst->eraseFromParent(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 417 | // Conditional branch followed by an unconditional branch. |
| 418 | // The last one must be unconditional. |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 419 | if (LastOpc != UncondBrOpc) |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 420 | return true; |
| 421 | |
| 422 | AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond); |
| 423 | FBB = LastInst->getOperand(0).getMBB(); |
| 424 | |
| 425 | return false; |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 428 | void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, |
| 429 | MachineBasicBlock *TBB, DebugLoc DL, |
| 430 | const SmallVectorImpl<MachineOperand>& Cond) |
| 431 | const { |
| 432 | unsigned Opc = Cond[0].getImm(); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 433 | const MCInstrDesc &MCID = get(Opc); |
| 434 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 435 | |
| 436 | for (unsigned i = 1; i < Cond.size(); ++i) |
| 437 | MIB.addReg(Cond[i].getReg()); |
| 438 | |
| 439 | MIB.addMBB(TBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 442 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 443 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 444 | MachineBasicBlock *FBB, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 445 | const SmallVectorImpl<MachineOperand> &Cond, |
| 446 | DebugLoc DL) const { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 447 | // Shouldn't be a fall through. |
| 448 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 449 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 450 | // # of condition operands: |
| 451 | // Unconditional branches: 0 |
| 452 | // Floating point branches: 1 (opc) |
| 453 | // Int BranchZero: 2 (opc, reg) |
| 454 | // Int Branch: 3 (opc, reg0, reg1) |
| 455 | assert((Cond.size() <= 3) && |
| 456 | "# of Mips branch conditions must be <= 3!"); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 457 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 458 | // Two-way Conditional branch. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 459 | if (FBB) { |
| 460 | BuildCondBr(MBB, TBB, DL, Cond); |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 461 | BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 462 | return 2; |
| 463 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 464 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 465 | // One way branch. |
| 466 | // Unconditional branch. |
| 467 | if (Cond.empty()) |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 468 | BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 469 | else // Conditional branch. |
| 470 | BuildCondBr(MBB, TBB, DL, Cond); |
| 471 | return 1; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 472 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 473 | |
| 474 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 475 | RemoveBranch(MachineBasicBlock &MBB) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 476 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 477 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
| 478 | MachineBasicBlock::reverse_iterator FirstBr; |
| 479 | unsigned removed; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 480 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 481 | // Skip all the debug instructions. |
| 482 | while (I != REnd && I->isDebugValue()) |
| 483 | ++I; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 484 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 485 | FirstBr = I; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 486 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 487 | // Up to 2 branches are removed. |
| 488 | // Note that indirect branches are not removed. |
| 489 | for(removed = 0; I != REnd && removed < 2; ++I, ++removed) |
| 490 | if (!GetAnalyzableBrOpc(I->getOpcode())) |
| 491 | break; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 492 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 493 | MBB.erase(I.base(), FirstBr.base()); |
| 494 | |
| 495 | return removed; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 498 | /// ReverseBranchCondition - Return the inverse opcode of the |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 499 | /// specified Branch instruction. |
| 500 | bool MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 501 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 502 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 503 | assert( (Cond.size() && Cond.size() <= 3) && |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 504 | "Invalid Mips branch condition!"); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 505 | Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm())); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 508 | |
Akira Hatanaka | d4b48b2 | 2012-06-14 01:16:45 +0000 | [diff] [blame^] | 509 | /// Return the number of bytes of code the specified instruction may be. |
| 510 | unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
| 511 | switch (MI->getOpcode()) { |
| 512 | default: |
| 513 | return MI->getDesc().getSize(); |
| 514 | case TargetOpcode::INLINEASM: { // Inline Asm: Variable size. |
| 515 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 516 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
| 517 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | unsigned |
| 523 | llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII, |
| 524 | MachineBasicBlock& MBB, |
| 525 | MachineBasicBlock::iterator II, DebugLoc DL, |
| 526 | bool LastInstrIsADDiu, |
| 527 | MipsAnalyzeImmediate::Inst *LastInst) { |
| 528 | MipsAnalyzeImmediate AnalyzeImm; |
| 529 | unsigned Size = IsN64 ? 64 : 32; |
| 530 | unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi; |
| 531 | unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO; |
| 532 | unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT; |
| 533 | |
| 534 | const MipsAnalyzeImmediate::InstSeq &Seq = |
| 535 | AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu); |
| 536 | MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin(); |
| 537 | |
| 538 | if (LastInst && (Seq.size() == 1)) { |
| 539 | *LastInst = *Inst; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | // The first instruction can be a LUi, which is different from other |
| 544 | // instructions (ADDiu, ORI and SLL) in that it does not have a register |
| 545 | // operand. |
| 546 | if (Inst->Opc == LUi) |
| 547 | BuildMI(MBB, II, DL, TII.get(LUi), ATReg) |
| 548 | .addImm(SignExtend64<16>(Inst->ImmOpnd)); |
| 549 | else |
| 550 | BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg) |
| 551 | .addImm(SignExtend64<16>(Inst->ImmOpnd)); |
| 552 | |
| 553 | // Build the remaining instructions in Seq. Skip the last instruction if |
| 554 | // LastInst is not 0. |
| 555 | for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst) |
| 556 | BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg) |
| 557 | .addImm(SignExtend64<16>(Inst->ImmOpnd)); |
| 558 | |
| 559 | if (LastInst) |
| 560 | *LastInst = *Inst; |
| 561 | |
| 562 | return Seq.size() - !!LastInst; |
| 563 | } |