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