blob: 50e3eb534e88cc24e305107c0f49d96046e6eb98 [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 Hatanaka85890102012-07-31 23:41:32 +000032 TM(tm), UncondBrOpc(UncondBr) {}
Akira Hatanaka794bf172011-07-07 23:56:50 +000033
Akira Hatanakaaf266262012-08-02 18:21:47 +000034const MipsInstrInfo *MipsInstrInfo::create(MipsTargetMachine &TM) {
35 if (TM.getSubtargetImpl()->inMips16Mode())
36 return llvm::createMips16InstrInfo(TM);
37
38 return llvm::createMipsSEInstrInfo(TM);
39}
40
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000041bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohmand735b802008-10-03 15:45:36 +000042 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043}
44
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000045/// insertNoop - If data hazard condition is found insert the target nop
46/// instruction.
47void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000048insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000049{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000050 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000051 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000052}
53
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000054MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
55 unsigned Flag) const {
Akira Hatanakafd1d9252011-12-24 03:11:18 +000056 MachineFunction &MF = *MBB.getParent();
57 MachineFrameInfo &MFI = *MF.getFrameInfo();
58 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liubb481f82012-02-28 07:46:26 +000059
Akira Hatanakafd1d9252011-12-24 03:11:18 +000060 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
61 MFI.getObjectSize(FI), Align);
62}
63
Akira Hatanakac4f24eb2011-07-01 01:04:43 +000064MachineInstr*
65MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
66 uint64_t Offset, const MDNode *MDPtr,
67 DebugLoc DL) const {
68 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
69 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
70 return &*MIB;
71}
72
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000073//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000074// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000075//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000076
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000077void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
78 MachineBasicBlock *&BB,
79 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka20ada982011-04-01 17:39:08 +000080 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
81 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +000082
Akira Hatanaka20ada982011-04-01 17:39:08 +000083 // for both int and fp branches, the last explicit operand is the
84 // MBB.
85 BB = Inst->getOperand(NumOp-1).getMBB();
86 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +000087
Akira Hatanaka20ada982011-04-01 17:39:08 +000088 for (int i=0; i<NumOp-1; i++)
89 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000090}
91
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000092bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000093 MachineBasicBlock *&TBB,
94 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +000095 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000096 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000097{
Akira Hatanaka20ada982011-04-01 17:39:08 +000098 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000099
Akira Hatanaka20ada982011-04-01 17:39:08 +0000100 // Skip all the debug instructions.
101 while (I != REnd && I->isDebugValue())
102 ++I;
103
104 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
105 // If this block ends with no branches (it just falls through to its succ)
106 // just return false, leaving TBB/FBB null.
107 TBB = FBB = NULL;
108 return false;
109 }
110
111 MachineInstr *LastInst = &*I;
112 unsigned LastOpc = LastInst->getOpcode();
113
114 // Not an analyzable branch (must be an indirect jump).
115 if (!GetAnalyzableBrOpc(LastOpc))
116 return true;
117
118 // Get the second to last instruction in the block.
119 unsigned SecondLastOpc = 0;
120 MachineInstr *SecondLastInst = NULL;
121
122 if (++I != REnd) {
123 SecondLastInst = &*I;
124 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
125
126 // Not an analyzable branch (must be an indirect jump).
127 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
128 return true;
129 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000130
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000131 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000132 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000133 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000134 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000135 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000136 return false;
137 }
138
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000139 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000140 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
141 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000142 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000143
Akira Hatanaka20ada982011-04-01 17:39:08 +0000144 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000145 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000146 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000147 return true;
148
Akira Hatanaka20ada982011-04-01 17:39:08 +0000149 // If second to last instruction is an unconditional branch,
150 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000151 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000152 // Return if the last instruction cannot be removed.
153 if (!AllowModify)
154 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000155
Chris Lattner8aa797a2007-12-30 23:10:15 +0000156 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000157 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000158 return false;
159 }
160
Akira Hatanaka20ada982011-04-01 17:39:08 +0000161 // Conditional branch followed by an unconditional branch.
162 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000163 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000164 return true;
165
166 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
167 FBB = LastInst->getOperand(0).getMBB();
168
169 return false;
Jia Liubb481f82012-02-28 07:46:26 +0000170}
171
Akira Hatanaka20ada982011-04-01 17:39:08 +0000172void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
173 MachineBasicBlock *TBB, DebugLoc DL,
174 const SmallVectorImpl<MachineOperand>& Cond)
175 const {
176 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000177 const MCInstrDesc &MCID = get(Opc);
178 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000179
180 for (unsigned i = 1; i < Cond.size(); ++i)
181 MIB.addReg(Cond[i].getReg());
182
183 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000184}
185
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000186unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000187InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000188 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000189 const SmallVectorImpl<MachineOperand> &Cond,
190 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000191 // Shouldn't be a fall through.
192 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000193
Akira Hatanaka20ada982011-04-01 17:39:08 +0000194 // # of condition operands:
195 // Unconditional branches: 0
196 // Floating point branches: 1 (opc)
197 // Int BranchZero: 2 (opc, reg)
198 // Int Branch: 3 (opc, reg0, reg1)
199 assert((Cond.size() <= 3) &&
200 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000201
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000202 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000203 if (FBB) {
204 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000205 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000206 return 2;
207 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000208
Akira Hatanaka20ada982011-04-01 17:39:08 +0000209 // One way branch.
210 // Unconditional branch.
211 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000212 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000213 else // Conditional branch.
214 BuildCondBr(MBB, TBB, DL, Cond);
215 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000216}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000217
218unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000219RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000220{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000221 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
222 MachineBasicBlock::reverse_iterator FirstBr;
223 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000224
Akira Hatanaka20ada982011-04-01 17:39:08 +0000225 // Skip all the debug instructions.
226 while (I != REnd && I->isDebugValue())
227 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000228
Akira Hatanaka20ada982011-04-01 17:39:08 +0000229 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000230
Akira Hatanaka20ada982011-04-01 17:39:08 +0000231 // Up to 2 branches are removed.
232 // Note that indirect branches are not removed.
233 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
234 if (!GetAnalyzableBrOpc(I->getOpcode()))
235 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000236
Akira Hatanaka20ada982011-04-01 17:39:08 +0000237 MBB.erase(I.base(), FirstBr.base());
238
239 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000240}
241
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000242/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000243/// specified Branch instruction.
244bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000245ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000246{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000247 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000248 "Invalid Mips branch condition!");
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +0000249 Cond[0].setImm(GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000250 return false;
251}
Dan Gohman99114052009-06-03 20:30:14 +0000252
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000253/// Return the number of bytes of code the specified instruction may be.
254unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
255 switch (MI->getOpcode()) {
256 default:
257 return MI->getDesc().getSize();
258 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
259 const MachineFunction *MF = MI->getParent()->getParent();
260 const char *AsmStr = MI->getOperand(0).getSymbolName();
261 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
262 }
263 }
264}
265
266unsigned
267llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII,
268 MachineBasicBlock& MBB,
269 MachineBasicBlock::iterator II, DebugLoc DL,
270 bool LastInstrIsADDiu,
271 MipsAnalyzeImmediate::Inst *LastInst) {
272 MipsAnalyzeImmediate AnalyzeImm;
273 unsigned Size = IsN64 ? 64 : 32;
274 unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi;
275 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
276 unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT;
277
278 const MipsAnalyzeImmediate::InstSeq &Seq =
279 AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
280 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
281
282 if (LastInst && (Seq.size() == 1)) {
283 *LastInst = *Inst;
284 return 0;
285 }
286
287 // The first instruction can be a LUi, which is different from other
288 // instructions (ADDiu, ORI and SLL) in that it does not have a register
289 // operand.
290 if (Inst->Opc == LUi)
291 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
292 .addImm(SignExtend64<16>(Inst->ImmOpnd));
293 else
294 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
295 .addImm(SignExtend64<16>(Inst->ImmOpnd));
296
297 // Build the remaining instructions in Seq. Skip the last instruction if
298 // LastInst is not 0.
299 for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst)
300 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
301 .addImm(SignExtend64<16>(Inst->ImmOpnd));
302
303 if (LastInst)
304 *LastInst = *Inst;
305
306 return Seq.size() - !!LastInst;
307}