blob: af64c9f7411236c6619c370dd39045c8fd16cce4 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#include "MipsInstrInfo.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000015#include "MipsTargetMachine.h"
Dan Gohman99114052009-06-03 20:30:14 +000016#include "MipsMachineFunction.h"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "MipsGenInstrInfo.inc"
22
23using namespace llvm;
24
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000026 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000027 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028
29static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000030 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031}
32
33/// Return true if the instruction is a register to register move and
34/// leave the source and dest operands in the passed parameters.
35bool MipsInstrInfo::
Evan Cheng04ee5a12009-01-20 19:12:24 +000036isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
37 unsigned &SrcSubIdx, unsigned &DstSubIdx) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038{
Evan Cheng04ee5a12009-01-20 19:12:24 +000039 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
40
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000041 // addu $dst, $src, $zero || addu $dst, $zero, $src
42 // or $dst, $src, $zero || or $dst, $zero, $src
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000043 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000044 if (MI.getOperand(1).getReg() == Mips::ZERO) {
45 DstReg = MI.getOperand(0).getReg();
46 SrcReg = MI.getOperand(2).getReg();
47 return true;
48 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
49 DstReg = MI.getOperand(0).getReg();
50 SrcReg = MI.getOperand(1).getReg();
51 return true;
52 }
53 }
54
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000055 // mov $fpDst, $fpSrc
56 // mfc $gpDst, $fpSrc
57 // mtc $fpDst, $gpSrc
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000058 if (MI.getOpcode() == Mips::FMOV_S32 ||
59 MI.getOpcode() == Mips::FMOV_D32 ||
60 MI.getOpcode() == Mips::MFC1 ||
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000061 MI.getOpcode() == Mips::MTC1 ||
62 MI.getOpcode() == Mips::MOVCCRToCCR) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000063 DstReg = MI.getOperand(0).getReg();
64 SrcReg = MI.getOperand(1).getReg();
65 return true;
66 }
67
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000068 // addiu $dst, $src, 0
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000069 if (MI.getOpcode() == Mips::ADDiu) {
Dan Gohmand735b802008-10-03 15:45:36 +000070 if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071 DstReg = MI.getOperand(0).getReg();
72 SrcReg = MI.getOperand(1).getReg();
73 return true;
74 }
75 }
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000076
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000077 return false;
78}
79
80/// isLoadFromStackSlot - If the specified machine instruction is a direct
81/// load from a stack slot, return the virtual or physical register number of
82/// the destination along with the FrameIndex of the loaded stack slot. If
83/// not, return 0. This predicate must return 0 if the instruction has
84/// any side effects other than loading from the stack slot.
85unsigned MipsInstrInfo::
Dan Gohmancbad42c2008-11-18 19:49:32 +000086isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000087{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000088 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000089 (MI->getOpcode() == Mips::LDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000090 if ((MI->getOperand(2).isFI()) && // is a stack slot
91 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000092 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000093 FrameIndex = MI->getOperand(2).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000094 return MI->getOperand(0).getReg();
95 }
96 }
97
98 return 0;
99}
100
101/// isStoreToStackSlot - If the specified machine instruction is a direct
102/// store to a stack slot, return the virtual or physical register number of
103/// the source reg along with the FrameIndex of the loaded stack slot. If
104/// not, return 0. This predicate must return 0 if the instruction has
105/// any side effects other than storing to the stack slot.
106unsigned MipsInstrInfo::
Dan Gohmancbad42c2008-11-18 19:49:32 +0000107isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000108{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000109 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000110 (MI->getOpcode() == Mips::SDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +0000111 if ((MI->getOperand(2).isFI()) && // is a stack slot
112 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000113 (isZeroImm(MI->getOperand(1)))) {
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000114 FrameIndex = MI->getOperand(2).getIndex();
115 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000116 }
117 }
118 return 0;
119}
120
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000121/// insertNoop - If data hazard condition is found insert the target nop
122/// instruction.
123void MipsInstrInfo::
124insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
125{
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000126 DebugLoc DL = DebugLoc::getUnknownLoc();
127 if (MI != MBB.end()) DL = MI->getDebugLoc();
128 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000129}
130
Owen Anderson940f83e2008-08-26 18:03:31 +0000131bool MipsInstrInfo::
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000132copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
133 unsigned DestReg, unsigned SrcReg,
134 const TargetRegisterClass *DestRC,
135 const TargetRegisterClass *SrcRC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000136 DebugLoc DL = DebugLoc::getUnknownLoc();
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000137 const MachineFunction *MF = MBB.getParent();
138 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
139
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000140 if (I != MBB.end()) DL = I->getDebugLoc();
141
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000142 if (DestRC != SrcRC) {
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000143
144 // Copy to/from FCR31 condition register
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000145 if ((DestRC == Mips::CPURegsRegisterClass) &&
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000146 (SrcRC == Mips::CCRRegisterClass))
147 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg).addReg(SrcReg);
148 else if ((DestRC == Mips::CCRRegisterClass) &&
149 (SrcRC == Mips::CPURegsRegisterClass))
150 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg).addReg(SrcReg);
151
152 // Moves between coprocessors and cpu
153 else if ((DestRC == Mips::CPURegsRegisterClass) &&
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000154 (SrcRC == Mips::FGR32RegisterClass))
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000155 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000156 else if ((DestRC == Mips::FGR32RegisterClass) &&
157 (SrcRC == Mips::CPURegsRegisterClass))
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000158 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg).addReg(SrcReg);
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000159 else if ((DestRC == Mips::AFGR64RegisterClass) &&
160 (SrcRC == Mips::CPURegsRegisterClass) &&
161 (SrcReg == Mips::ZERO)) {
162 const unsigned *AliasSet = TRI->getAliasSet(DestReg);
163 BuildMI(MBB, I, DL, get(Mips::MTC1), AliasSet[0]).addReg(SrcReg);
164 BuildMI(MBB, I, DL, get(Mips::MTC1), AliasSet[1]).addReg(SrcReg);
165 }
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000166
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000167 // Move from/to Hi/Lo registers
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000168 else if ((DestRC == Mips::HILORegisterClass) &&
169 (SrcRC == Mips::CPURegsRegisterClass)) {
170 unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000171 BuildMI(MBB, I, DL, get(Opc), DestReg);
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000172 } else if ((SrcRC == Mips::HILORegisterClass) &&
173 (DestRC == Mips::CPURegsRegisterClass)) {
174 unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000175 BuildMI(MBB, I, DL, get(Opc), DestReg);
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000176 } else
177 // Can't copy this register
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000178 return false;
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000179
Owen Anderson940f83e2008-08-26 18:03:31 +0000180 return true;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000181 }
182
183 if (DestRC == Mips::CPURegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000184 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000185 .addReg(SrcReg);
186 else if (DestRC == Mips::FGR32RegisterClass)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000187 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000188 else if (DestRC == Mips::AFGR64RegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000189 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000190 else if (DestRC == Mips::CCRRegisterClass)
191 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000192 else
Owen Anderson940f83e2008-08-26 18:03:31 +0000193 // Can't copy this register
194 return false;
195
196 return true;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000197}
198
199void MipsInstrInfo::
200storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000201 unsigned SrcReg, bool isKill, int FI,
Chris Lattnere3a85832009-03-26 05:28:26 +0000202 const TargetRegisterClass *RC) const {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000203 unsigned Opc;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000204
205 DebugLoc DL = DebugLoc::getUnknownLoc();
206 if (I != MBB.end()) DL = I->getDebugLoc();
207
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000208 if (RC == Mips::CPURegsRegisterClass)
209 Opc = Mips::SW;
210 else if (RC == Mips::FGR32RegisterClass)
211 Opc = Mips::SWC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000212 else {
213 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000214 Opc = Mips::SDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000215 }
216
Bill Wendling587daed2009-05-13 21:33:08 +0000217 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000218 .addImm(0).addFrameIndex(FI);
219}
220
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000221void MipsInstrInfo::
222loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
223 unsigned DestReg, int FI,
224 const TargetRegisterClass *RC) const
225{
226 unsigned Opc;
227 if (RC == Mips::CPURegsRegisterClass)
228 Opc = Mips::LW;
229 else if (RC == Mips::FGR32RegisterClass)
230 Opc = Mips::LWC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000231 else {
232 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000233 Opc = Mips::LDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000234 }
235
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000236 DebugLoc DL = DebugLoc::getUnknownLoc();
237 if (I != MBB.end()) DL = I->getDebugLoc();
238 BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000239}
240
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000241MachineInstr *MipsInstrInfo::
Dan Gohmanc54baa22008-12-03 18:43:12 +0000242foldMemoryOperandImpl(MachineFunction &MF,
243 MachineInstr* MI,
244 const SmallVectorImpl<unsigned> &Ops, int FI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000245{
246 if (Ops.size() != 1) return NULL;
247
248 MachineInstr *NewMI = NULL;
249
250 switch (MI->getOpcode()) {
251 case Mips::ADDu:
Dan Gohmand735b802008-10-03 15:45:36 +0000252 if ((MI->getOperand(0).isReg()) &&
253 (MI->getOperand(1).isReg()) &&
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000254 (MI->getOperand(1).getReg() == Mips::ZERO) &&
Dan Gohmand735b802008-10-03 15:45:36 +0000255 (MI->getOperand(2).isReg())) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000256 if (Ops[0] == 0) { // COPY -> STORE
257 unsigned SrcReg = MI->getOperand(2).getReg();
258 bool isKill = MI->getOperand(2).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000259 bool isUndef = MI->getOperand(2).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000260 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
Evan Cheng2578ba22009-07-01 01:59:31 +0000261 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000262 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000263 } else { // COPY -> LOAD
264 unsigned DstReg = MI->getOperand(0).getReg();
265 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000266 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000267 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
Evan Cheng2578ba22009-07-01 01:59:31 +0000268 .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
269 getUndefRegState(isUndef))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000270 .addImm(0).addFrameIndex(FI);
271 }
272 }
273 break;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000274 case Mips::FMOV_S32:
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000275 case Mips::FMOV_D32:
Dan Gohmand735b802008-10-03 15:45:36 +0000276 if ((MI->getOperand(0).isReg()) &&
277 (MI->getOperand(1).isReg())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000278 const TargetRegisterClass
279 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000280 unsigned StoreOpc, LoadOpc;
281
282 if (RC == Mips::FGR32RegisterClass) {
283 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000284 } else {
285 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000286 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000287 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000288
289 if (Ops[0] == 0) { // COPY -> STORE
290 unsigned SrcReg = MI->getOperand(1).getReg();
291 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000292 bool isUndef = MI->getOperand(2).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000293 NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
Evan Cheng2578ba22009-07-01 01:59:31 +0000294 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000295 .addImm(0).addFrameIndex(FI) ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000296 } else { // COPY -> LOAD
297 unsigned DstReg = MI->getOperand(0).getReg();
298 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000299 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000300 NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
Evan Cheng2578ba22009-07-01 01:59:31 +0000301 .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
302 getUndefRegState(isUndef))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000303 .addImm(0).addFrameIndex(FI);
304 }
305 }
306 break;
307 }
308
309 return NewMI;
310}
311
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000312//===----------------------------------------------------------------------===//
313// Branch Analysis
314//===----------------------------------------------------------------------===//
315
316/// GetCondFromBranchOpc - Return the Mips CC that matches
317/// the correspondent Branch instruction opcode.
318static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
319{
320 switch (BrOpc) {
321 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000322 case Mips::BEQ : return Mips::COND_E;
323 case Mips::BNE : return Mips::COND_NE;
324 case Mips::BGTZ : return Mips::COND_GZ;
325 case Mips::BGEZ : return Mips::COND_GEZ;
326 case Mips::BLTZ : return Mips::COND_LZ;
327 case Mips::BLEZ : return Mips::COND_LEZ;
328
329 // We dont do fp branch analysis yet!
330 case Mips::BC1T :
331 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000332 }
333}
334
335/// GetCondBranchFromCond - Return the Branch instruction
336/// opcode that matches the cc.
337unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
338{
339 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000340 default: llvm_unreachable("Illegal condition code!");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000341 case Mips::COND_E : return Mips::BEQ;
342 case Mips::COND_NE : return Mips::BNE;
343 case Mips::COND_GZ : return Mips::BGTZ;
344 case Mips::COND_GEZ : return Mips::BGEZ;
345 case Mips::COND_LZ : return Mips::BLTZ;
346 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000347
348 case Mips::FCOND_F:
349 case Mips::FCOND_UN:
350 case Mips::FCOND_EQ:
351 case Mips::FCOND_UEQ:
352 case Mips::FCOND_OLT:
353 case Mips::FCOND_ULT:
354 case Mips::FCOND_OLE:
355 case Mips::FCOND_ULE:
356 case Mips::FCOND_SF:
357 case Mips::FCOND_NGLE:
358 case Mips::FCOND_SEQ:
359 case Mips::FCOND_NGL:
360 case Mips::FCOND_LT:
361 case Mips::FCOND_NGE:
362 case Mips::FCOND_LE:
363 case Mips::FCOND_NGT: return Mips::BC1T;
364
365 case Mips::FCOND_T:
366 case Mips::FCOND_OR:
367 case Mips::FCOND_NEQ:
368 case Mips::FCOND_OGL:
369 case Mips::FCOND_UGE:
370 case Mips::FCOND_OGE:
371 case Mips::FCOND_UGT:
372 case Mips::FCOND_OGT:
373 case Mips::FCOND_ST:
374 case Mips::FCOND_GLE:
375 case Mips::FCOND_SNE:
376 case Mips::FCOND_GL:
377 case Mips::FCOND_NLT:
378 case Mips::FCOND_GE:
379 case Mips::FCOND_NLE:
380 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000381 }
382}
383
384/// GetOppositeBranchCondition - Return the inverse of the specified
385/// condition, e.g. turning COND_E to COND_NE.
386Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
387{
388 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000389 default: llvm_unreachable("Illegal condition code!");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000390 case Mips::COND_E : return Mips::COND_NE;
391 case Mips::COND_NE : return Mips::COND_E;
392 case Mips::COND_GZ : return Mips::COND_LEZ;
393 case Mips::COND_GEZ : return Mips::COND_LZ;
394 case Mips::COND_LZ : return Mips::COND_GEZ;
395 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000396 case Mips::FCOND_F : return Mips::FCOND_T;
397 case Mips::FCOND_UN : return Mips::FCOND_OR;
398 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
399 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
400 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
401 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
402 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
403 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
404 case Mips::FCOND_SF: return Mips::FCOND_ST;
405 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
406 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
407 case Mips::FCOND_NGL: return Mips::FCOND_GL;
408 case Mips::FCOND_LT: return Mips::FCOND_NLT;
409 case Mips::FCOND_NGE: return Mips::FCOND_GE;
410 case Mips::FCOND_LE: return Mips::FCOND_NLE;
411 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000412 }
413}
414
415bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
416 MachineBasicBlock *&TBB,
417 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000418 SmallVectorImpl<MachineOperand> &Cond,
419 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000420{
421 // If the block has no terminators, it just falls into the block after it.
422 MachineBasicBlock::iterator I = MBB.end();
423 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
424 return false;
425
426 // Get the last instruction in the block.
427 MachineInstr *LastInst = I;
428
429 // If there is only one terminator instruction, process it.
430 unsigned LastOpc = LastInst->getOpcode();
431 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000432 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000433 return true;
434
435 // Unconditional branch
436 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000437 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000438 return false;
439 }
440
441 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
442 if (BranchCode == Mips::COND_INVALID)
443 return true; // Can't handle indirect branch.
444
445 // Conditional branch
446 // Block ends with fall-through condbranch.
447 if (LastOpc != Mips::COND_INVALID) {
448 int LastNumOp = LastInst->getNumOperands();
449
Chris Lattner8aa797a2007-12-30 23:10:15 +0000450 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000451 Cond.push_back(MachineOperand::CreateImm(BranchCode));
452
453 for (int i=0; i<LastNumOp-1; i++) {
454 Cond.push_back(LastInst->getOperand(i));
455 }
456
457 return false;
458 }
459 }
460
461 // Get the instruction before it if it is a terminator.
462 MachineInstr *SecondLastInst = I;
463
464 // If there are three terminators, we don't know what sort of block this is.
465 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
466 return true;
467
468 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
469 unsigned SecondLastOpc = SecondLastInst->getOpcode();
470 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
471
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000472 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000473 int SecondNumOp = SecondLastInst->getNumOperands();
474
Chris Lattner8aa797a2007-12-30 23:10:15 +0000475 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000476 Cond.push_back(MachineOperand::CreateImm(BranchCode));
477
478 for (int i=0; i<SecondNumOp-1; i++) {
479 Cond.push_back(SecondLastInst->getOperand(i));
480 }
481
Chris Lattner8aa797a2007-12-30 23:10:15 +0000482 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000483 return false;
484 }
485
486 // If the block ends with two unconditional branches, handle it. The last
487 // one is not executed, so remove it.
488 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000489 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000490 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000491 if (AllowModify)
492 I->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000493 return false;
494 }
495
496 // Otherwise, can't handle this.
497 return true;
498}
499
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000500unsigned MipsInstrInfo::
501InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000502 MachineBasicBlock *FBB,
503 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen94817572009-02-13 02:34:39 +0000504 // FIXME this should probably have a DebugLoc argument
505 DebugLoc dl = DebugLoc::getUnknownLoc();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000506 // Shouldn't be a fall through.
507 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
508 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
509 "Mips branch conditions can have two|three components!");
510
511 if (FBB == 0) { // One way branch.
512 if (Cond.empty()) {
513 // Unconditional branch?
Dale Johannesen94817572009-02-13 02:34:39 +0000514 BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000515 } else {
516 // Conditional branch.
517 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000518 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000519
Chris Lattner349c4952008-01-07 03:13:06 +0000520 if (TID.getNumOperands() == 3)
Dale Johannesen94817572009-02-13 02:34:39 +0000521 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000522 .addReg(Cond[2].getReg())
523 .addMBB(TBB);
524 else
Dale Johannesen94817572009-02-13 02:34:39 +0000525 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000526 .addMBB(TBB);
527
528 }
529 return 1;
530 }
531
532 // Two-way Conditional branch.
533 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000534 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000535
Chris Lattner349c4952008-01-07 03:13:06 +0000536 if (TID.getNumOperands() == 3)
Dale Johannesen94817572009-02-13 02:34:39 +0000537 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000538 .addMBB(TBB);
539 else
Dale Johannesen94817572009-02-13 02:34:39 +0000540 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000541
Dale Johannesen94817572009-02-13 02:34:39 +0000542 BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000543 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000544}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000545
546unsigned MipsInstrInfo::
547RemoveBranch(MachineBasicBlock &MBB) const
548{
549 MachineBasicBlock::iterator I = MBB.end();
550 if (I == MBB.begin()) return 0;
551 --I;
552 if (I->getOpcode() != Mips::J &&
553 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
554 return 0;
555
556 // Remove the branch.
557 I->eraseFromParent();
558
559 I = MBB.end();
560
561 if (I == MBB.begin()) return 1;
562 --I;
563 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
564 return 1;
565
566 // Remove the branch.
567 I->eraseFromParent();
568 return 2;
569}
570
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000571/// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000572/// fall-through into its successor block.
573bool MipsInstrInfo::
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000574BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000575{
576 if (MBB.empty()) return false;
577
578 switch (MBB.back().getOpcode()) {
579 case Mips::RET: // Return.
580 case Mips::JR: // Indirect branch.
581 case Mips::J: // Uncond branch.
582 return true;
583 default: return false;
584 }
585}
586
587/// ReverseBranchCondition - Return the inverse opcode of the
588/// specified Branch instruction.
589bool MipsInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000590ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000591{
592 assert( (Cond.size() == 3 || Cond.size() == 2) &&
593 "Invalid Mips branch condition!");
594 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
595 return false;
596}
Dan Gohman99114052009-06-03 20:30:14 +0000597
598/// getGlobalBaseReg - Return a virtual register initialized with the
599/// the global base register value. Output instructions required to
600/// initialize the register in the function entry block, if necessary.
601///
602unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
603 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
604 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
605 if (GlobalBaseReg != 0)
606 return GlobalBaseReg;
607
608 // Insert the set of GlobalBaseReg into the first MBB of the function
609 MachineBasicBlock &FirstMBB = MF->front();
610 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
611 MachineRegisterInfo &RegInfo = MF->getRegInfo();
612 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
613
614 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
615 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Mips::GP,
616 Mips::CPURegsRegisterClass,
617 Mips::CPURegsRegisterClass);
618 assert(Ok && "Couldn't assign to global base register!");
Duncan Sands43050692009-07-03 16:11:59 +0000619 Ok = Ok; // Silence warning when assertions are turned off.
Dan Gohman99114052009-06-03 20:30:14 +0000620 RegInfo.addLiveIn(Mips::GP);
621
622 MipsFI->setGlobalBaseReg(GlobalBaseReg);
623 return GlobalBaseReg;
624}