blob: a67d1e4b18d2922f773302d200fc3df167d82bcf [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
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//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
Akira Hatanakad4b48b22012-06-14 01:16:45 +000014#include "MipsAnalyzeImmediate.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsInstrInfo.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000016#include "MipsTargetMachine.h"
Dan Gohman99114052009-06-03 20:30:14 +000017#include "MipsMachineFunction.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000018#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000023#include "llvm/ADT/STLExtras.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000024
Evan Cheng4db3cff2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "MipsGenInstrInfo.inc"
27
28using namespace llvm;
29
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000030MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm, unsigned UncondBr)
Evan Cheng4db3cff2011-07-01 17:57:27 +000031 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000032 TM(tm), RI(*TM.getSubtargetImpl(), *this), UncondBrOpc(UncondBr) {}
Akira Hatanaka794bf172011-07-07 23:56:50 +000033
Jia Liubb481f82012-02-28 07:46:26 +000034const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
Akira Hatanaka794bf172011-07-07 23:56:50 +000035 return RI;
36}
37
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000038bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohmand735b802008-10-03 15:45:36 +000039 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040}
41
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000042/// insertNoop - If data hazard condition is found insert the target nop
43/// instruction.
44void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000045insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000046{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000047 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000048 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000049}
50
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000051MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
52 unsigned Flag) const {
Akira Hatanakafd1d9252011-12-24 03:11:18 +000053 MachineFunction &MF = *MBB.getParent();
54 MachineFrameInfo &MFI = *MF.getFrameInfo();
55 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liubb481f82012-02-28 07:46:26 +000056
Akira Hatanakafd1d9252011-12-24 03:11:18 +000057 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
58 MFI.getObjectSize(FI), Align);
59}
60
Akira Hatanakac4f24eb2011-07-01 01:04:43 +000061MachineInstr*
62MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
63 uint64_t Offset, const MDNode *MDPtr,
64 DebugLoc DL) const {
65 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
66 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
67 return &*MIB;
68}
69
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000070//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000071// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000072//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000073
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000074void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
75 MachineBasicBlock *&BB,
76 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka20ada982011-04-01 17:39:08 +000077 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
78 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +000079
Akira Hatanaka20ada982011-04-01 17:39:08 +000080 // for both int and fp branches, the last explicit operand is the
81 // MBB.
82 BB = Inst->getOperand(NumOp-1).getMBB();
83 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +000084
Akira Hatanaka20ada982011-04-01 17:39:08 +000085 for (int i=0; i<NumOp-1; i++)
86 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000087}
88
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000089bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000090 MachineBasicBlock *&TBB,
91 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +000092 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000093 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000094{
Akira Hatanaka20ada982011-04-01 17:39:08 +000095 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000096
Akira Hatanaka20ada982011-04-01 17:39:08 +000097 // Skip all the debug instructions.
98 while (I != REnd && I->isDebugValue())
99 ++I;
100
101 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
102 // If this block ends with no branches (it just falls through to its succ)
103 // just return false, leaving TBB/FBB null.
104 TBB = FBB = NULL;
105 return false;
106 }
107
108 MachineInstr *LastInst = &*I;
109 unsigned LastOpc = LastInst->getOpcode();
110
111 // Not an analyzable branch (must be an indirect jump).
112 if (!GetAnalyzableBrOpc(LastOpc))
113 return true;
114
115 // Get the second to last instruction in the block.
116 unsigned SecondLastOpc = 0;
117 MachineInstr *SecondLastInst = NULL;
118
119 if (++I != REnd) {
120 SecondLastInst = &*I;
121 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
122
123 // Not an analyzable branch (must be an indirect jump).
124 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
125 return true;
126 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000127
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000128 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000129 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000130 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000131 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000132 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000133 return false;
134 }
135
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000136 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000137 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
138 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000139 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000140
Akira Hatanaka20ada982011-04-01 17:39:08 +0000141 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000142 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000143 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000144 return true;
145
Akira Hatanaka20ada982011-04-01 17:39:08 +0000146 // If second to last instruction is an unconditional branch,
147 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000148 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000149 // Return if the last instruction cannot be removed.
150 if (!AllowModify)
151 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000152
Chris Lattner8aa797a2007-12-30 23:10:15 +0000153 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000154 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000155 return false;
156 }
157
Akira Hatanaka20ada982011-04-01 17:39:08 +0000158 // Conditional branch followed by an unconditional branch.
159 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000160 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000161 return true;
162
163 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
164 FBB = LastInst->getOperand(0).getMBB();
165
166 return false;
Jia Liubb481f82012-02-28 07:46:26 +0000167}
168
Akira Hatanaka20ada982011-04-01 17:39:08 +0000169void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
170 MachineBasicBlock *TBB, DebugLoc DL,
171 const SmallVectorImpl<MachineOperand>& Cond)
172 const {
173 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000174 const MCInstrDesc &MCID = get(Opc);
175 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000176
177 for (unsigned i = 1; i < Cond.size(); ++i)
178 MIB.addReg(Cond[i].getReg());
179
180 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000181}
182
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000183unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000184InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000185 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000186 const SmallVectorImpl<MachineOperand> &Cond,
187 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000188 // Shouldn't be a fall through.
189 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000190
Akira Hatanaka20ada982011-04-01 17:39:08 +0000191 // # of condition operands:
192 // Unconditional branches: 0
193 // Floating point branches: 1 (opc)
194 // Int BranchZero: 2 (opc, reg)
195 // Int Branch: 3 (opc, reg0, reg1)
196 assert((Cond.size() <= 3) &&
197 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000198
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000199 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000200 if (FBB) {
201 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000202 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000203 return 2;
204 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000205
Akira Hatanaka20ada982011-04-01 17:39:08 +0000206 // One way branch.
207 // Unconditional branch.
208 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000209 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000210 else // Conditional branch.
211 BuildCondBr(MBB, TBB, DL, Cond);
212 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000213}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000214
215unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000216RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000217{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000218 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
219 MachineBasicBlock::reverse_iterator FirstBr;
220 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000221
Akira Hatanaka20ada982011-04-01 17:39:08 +0000222 // Skip all the debug instructions.
223 while (I != REnd && I->isDebugValue())
224 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000225
Akira Hatanaka20ada982011-04-01 17:39:08 +0000226 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000227
Akira Hatanaka20ada982011-04-01 17:39:08 +0000228 // Up to 2 branches are removed.
229 // Note that indirect branches are not removed.
230 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
231 if (!GetAnalyzableBrOpc(I->getOpcode()))
232 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000233
Akira Hatanaka20ada982011-04-01 17:39:08 +0000234 MBB.erase(I.base(), FirstBr.base());
235
236 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000237}
238
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000239/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000240/// specified Branch instruction.
241bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000242ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000243{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000244 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000245 "Invalid Mips branch condition!");
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +0000246 Cond[0].setImm(GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000247 return false;
248}
Dan Gohman99114052009-06-03 20:30:14 +0000249
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000250/// Return the number of bytes of code the specified instruction may be.
251unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
252 switch (MI->getOpcode()) {
253 default:
254 return MI->getDesc().getSize();
255 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
256 const MachineFunction *MF = MI->getParent()->getParent();
257 const char *AsmStr = MI->getOperand(0).getSymbolName();
258 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
259 }
260 }
261}
262
263unsigned
264llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII,
265 MachineBasicBlock& MBB,
266 MachineBasicBlock::iterator II, DebugLoc DL,
267 bool LastInstrIsADDiu,
268 MipsAnalyzeImmediate::Inst *LastInst) {
269 MipsAnalyzeImmediate AnalyzeImm;
270 unsigned Size = IsN64 ? 64 : 32;
271 unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi;
272 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
273 unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT;
274
275 const MipsAnalyzeImmediate::InstSeq &Seq =
276 AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
277 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
278
279 if (LastInst && (Seq.size() == 1)) {
280 *LastInst = *Inst;
281 return 0;
282 }
283
284 // The first instruction can be a LUi, which is different from other
285 // instructions (ADDiu, ORI and SLL) in that it does not have a register
286 // operand.
287 if (Inst->Opc == LUi)
288 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
289 .addImm(SignExtend64<16>(Inst->ImmOpnd));
290 else
291 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
292 .addImm(SignExtend64<16>(Inst->ImmOpnd));
293
294 // Build the remaining instructions in Seq. Skip the last instruction if
295 // LastInst is not 0.
296 for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst)
297 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
298 .addImm(SignExtend64<16>(Inst->ImmOpnd));
299
300 if (LastInst)
301 *LastInst = *Inst;
302
303 return Seq.size() - !!LastInst;
304}