blob: 668efb7ffdcf0546dd94c5fff6ca6fec3fff61e7 [file] [log] [blame]
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +00001//===-- MipsSEInstrInfo.cpp - Mips32/64 Instruction Information -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Mips32/64 implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MipsSEInstrInfo.h"
15#include "MipsTargetMachine.h"
16#include "MipsMachineFunction.h"
17#include "InstPrinter/MipsInstPrinter.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/TargetRegistry.h"
22#include "llvm/ADT/STLExtras.h"
23
24using namespace llvm;
25
26MipsSEInstrInfo::MipsSEInstrInfo(MipsTargetMachine &tm)
27 : MipsInstrInfo(tm,
28 tm.getRelocationModel() == Reloc::PIC_ ? Mips::B : Mips::J),
29 IsN64(tm.getSubtarget<MipsSubtarget>().isABI_N64()) {}
30
31/// isLoadFromStackSlot - If the specified machine instruction is a direct
32/// load from a stack slot, return the virtual or physical register number of
33/// the destination along with the FrameIndex of the loaded stack slot. If
34/// not, return 0. This predicate must return 0 if the instruction has
35/// any side effects other than loading from the stack slot.
36unsigned MipsSEInstrInfo::
37isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
38{
39 unsigned Opc = MI->getOpcode();
40
41 if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) ||
42 (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) ||
43 (Opc == Mips::LDC1) || (Opc == Mips::LDC164) ||
44 (Opc == Mips::LDC164_P8)) {
45 if ((MI->getOperand(1).isFI()) && // is a stack slot
46 (MI->getOperand(2).isImm()) && // the imm is zero
47 (isZeroImm(MI->getOperand(2)))) {
48 FrameIndex = MI->getOperand(1).getIndex();
49 return MI->getOperand(0).getReg();
50 }
51 }
52
53 return 0;
54}
55
56/// isStoreToStackSlot - If the specified machine instruction is a direct
57/// store to a stack slot, return the virtual or physical register number of
58/// the source reg along with the FrameIndex of the loaded stack slot. If
59/// not, return 0. This predicate must return 0 if the instruction has
60/// any side effects other than storing to the stack slot.
61unsigned MipsSEInstrInfo::
62isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
63{
64 unsigned Opc = MI->getOpcode();
65
66 if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) ||
67 (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) ||
68 (Opc == Mips::SDC1) || (Opc == Mips::SDC164) ||
69 (Opc == Mips::SDC164_P8)) {
70 if ((MI->getOperand(1).isFI()) && // is a stack slot
71 (MI->getOperand(2).isImm()) && // the imm is zero
72 (isZeroImm(MI->getOperand(2)))) {
73 FrameIndex = MI->getOperand(1).getIndex();
74 return MI->getOperand(0).getReg();
75 }
76 }
77 return 0;
78}
79
80void MipsSEInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator I, DebugLoc DL,
82 unsigned DestReg, unsigned SrcReg,
83 bool KillSrc) const {
84 unsigned Opc = 0, ZeroReg = 0;
85
86 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
87 if (Mips::CPURegsRegClass.contains(SrcReg))
88 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
89 else if (Mips::CCRRegClass.contains(SrcReg))
90 Opc = Mips::CFC1;
91 else if (Mips::FGR32RegClass.contains(SrcReg))
92 Opc = Mips::MFC1;
93 else if (SrcReg == Mips::HI)
94 Opc = Mips::MFHI, SrcReg = 0;
95 else if (SrcReg == Mips::LO)
96 Opc = Mips::MFLO, SrcReg = 0;
97 }
98 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
99 if (Mips::CCRRegClass.contains(DestReg))
100 Opc = Mips::CTC1;
101 else if (Mips::FGR32RegClass.contains(DestReg))
102 Opc = Mips::MTC1;
103 else if (DestReg == Mips::HI)
104 Opc = Mips::MTHI, DestReg = 0;
105 else if (DestReg == Mips::LO)
106 Opc = Mips::MTLO, DestReg = 0;
107 }
108 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
109 Opc = Mips::FMOV_S;
110 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
111 Opc = Mips::FMOV_D32;
112 else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
113 Opc = Mips::FMOV_D64;
114 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
115 Opc = Mips::MOVCCRToCCR;
116 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
117 if (Mips::CPU64RegsRegClass.contains(SrcReg))
118 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
119 else if (SrcReg == Mips::HI64)
120 Opc = Mips::MFHI64, SrcReg = 0;
121 else if (SrcReg == Mips::LO64)
122 Opc = Mips::MFLO64, SrcReg = 0;
123 else if (Mips::FGR64RegClass.contains(SrcReg))
124 Opc = Mips::DMFC1;
125 }
126 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
127 if (DestReg == Mips::HI64)
128 Opc = Mips::MTHI64, DestReg = 0;
129 else if (DestReg == Mips::LO64)
130 Opc = Mips::MTLO64, DestReg = 0;
131 else if (Mips::FGR64RegClass.contains(DestReg))
132 Opc = Mips::DMTC1;
133 }
134
135 assert(Opc && "Cannot copy registers");
136
137 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
138
139 if (DestReg)
140 MIB.addReg(DestReg, RegState::Define);
141
142 if (ZeroReg)
143 MIB.addReg(ZeroReg);
144
145 if (SrcReg)
146 MIB.addReg(SrcReg, getKillRegState(KillSrc));
147}
148
149void MipsSEInstrInfo::
150storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
151 unsigned SrcReg, bool isKill, int FI,
152 const TargetRegisterClass *RC,
153 const TargetRegisterInfo *TRI) const {
154 DebugLoc DL;
155 if (I != MBB.end()) DL = I->getDebugLoc();
156 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
157
158 unsigned Opc = 0;
159
160 if (Mips::CPURegsRegClass.hasSubClassEq(RC))
161 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
162 else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC))
163 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
164 else if (Mips::FGR32RegClass.hasSubClassEq(RC))
165 Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1;
166 else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
167 Opc = Mips::SDC1;
168 else if (Mips::FGR64RegClass.hasSubClassEq(RC))
169 Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164;
170
171 assert(Opc && "Register class not handled!");
172 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
173 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
174}
175
176void MipsSEInstrInfo::
177loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
178 unsigned DestReg, int FI,
179 const TargetRegisterClass *RC,
180 const TargetRegisterInfo *TRI) const
181{
182 DebugLoc DL;
183 if (I != MBB.end()) DL = I->getDebugLoc();
184 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
185 unsigned Opc = 0;
186
187 if (Mips::CPURegsRegClass.hasSubClassEq(RC))
188 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
189 else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC))
190 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
191 else if (Mips::FGR32RegClass.hasSubClassEq(RC))
192 Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1;
193 else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
194 Opc = Mips::LDC1;
195 else if (Mips::FGR64RegClass.hasSubClassEq(RC))
196 Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164;
197
198 assert(Opc && "Register class not handled!");
199 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0)
200 .addMemOperand(MMO);
201}
202
203bool MipsSEInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
204 MachineBasicBlock &MBB = *MI->getParent();
205
206 switch(MI->getDesc().getOpcode()) {
207 default:
208 return false;
209 case Mips::RetRA:
210 ExpandRetRA(MBB, MI, Mips::RET);
211 break;
212 case Mips::BuildPairF64:
213 ExpandBuildPairF64(MBB, MI);
214 break;
215 case Mips::ExtractElementF64:
216 ExpandExtractElementF64(MBB, MI);
217 break;
218 }
219
220 MBB.erase(MI);
221 return true;
222}
223
224/// GetOppositeBranchOpc - Return the inverse of the specified
225/// opcode, e.g. turning BEQ to BNE.
226unsigned MipsSEInstrInfo::GetOppositeBranchOpc(unsigned Opc) const {
227 switch (Opc) {
228 default: llvm_unreachable("Illegal opcode!");
229 case Mips::BEQ: return Mips::BNE;
230 case Mips::BNE: return Mips::BEQ;
231 case Mips::BGTZ: return Mips::BLEZ;
232 case Mips::BGEZ: return Mips::BLTZ;
233 case Mips::BLTZ: return Mips::BGEZ;
234 case Mips::BLEZ: return Mips::BGTZ;
235 case Mips::BEQ64: return Mips::BNE64;
236 case Mips::BNE64: return Mips::BEQ64;
237 case Mips::BGTZ64: return Mips::BLEZ64;
238 case Mips::BGEZ64: return Mips::BLTZ64;
239 case Mips::BLTZ64: return Mips::BGEZ64;
240 case Mips::BLEZ64: return Mips::BGTZ64;
241 case Mips::BC1T: return Mips::BC1F;
242 case Mips::BC1F: return Mips::BC1T;
243 }
244}
245
246unsigned MipsSEInstrInfo::GetAnalyzableBrOpc(unsigned Opc) const {
247 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
248 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
249 Opc == Mips::BEQ64 || Opc == Mips::BNE64 || Opc == Mips::BGTZ64 ||
250 Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
251 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::B ||
252 Opc == Mips::J) ?
253 Opc : 0;
254}
255
256void MipsSEInstrInfo::ExpandRetRA(MachineBasicBlock &MBB,
257 MachineBasicBlock::iterator I,
258 unsigned Opc) const {
259 BuildMI(MBB, I, I->getDebugLoc(), get(Opc)).addReg(Mips::RA);
260}
261
262void MipsSEInstrInfo::ExpandExtractElementF64(MachineBasicBlock &MBB,
263 MachineBasicBlock::iterator I) const {
264 unsigned DstReg = I->getOperand(0).getReg();
265 unsigned SrcReg = I->getOperand(1).getReg();
266 unsigned N = I->getOperand(2).getImm();
267 const MCInstrDesc& Mfc1Tdd = get(Mips::MFC1);
268 DebugLoc dl = I->getDebugLoc();
269
270 assert(N < 2 && "Invalid immediate");
271 unsigned SubIdx = N ? Mips::sub_fpodd : Mips::sub_fpeven;
272 unsigned SubReg = getRegisterInfo().getSubReg(SrcReg, SubIdx);
273
274 BuildMI(MBB, I, dl, Mfc1Tdd, DstReg).addReg(SubReg);
275}
276
277void MipsSEInstrInfo::ExpandBuildPairF64(MachineBasicBlock &MBB,
278 MachineBasicBlock::iterator I) const {
279 unsigned DstReg = I->getOperand(0).getReg();
280 unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
281 const MCInstrDesc& Mtc1Tdd = get(Mips::MTC1);
282 DebugLoc dl = I->getDebugLoc();
283 const TargetRegisterInfo &TRI = getRegisterInfo();
284
285 // mtc1 Lo, $fp
286 // mtc1 Hi, $fp + 1
287 BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_fpeven))
288 .addReg(LoReg);
289 BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_fpodd))
290 .addReg(HiReg);
291}