blob: 7b04a9a375983820cc6e76562c49b6a136975e3c [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000013
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsInstrInfo.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "MipsAnalyzeImmediate.h"
17#include "MipsMachineFunction.h"
18#include "MipsTargetMachine.h"
19#include "llvm/ADT/STLExtras.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmand5ca70642009-06-03 20:30:14 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin56d06592009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000024
Evan Cheng703a0fb2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026#include "MipsGenInstrInfo.inc"
27
28using namespace llvm;
29
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000030MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm, unsigned UncondBr)
Evan Cheng703a0fb2011-07-01 17:57:27 +000031 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Akira Hatanakacb37e132012-07-31 23:41:32 +000032 TM(tm), UncondBrOpc(UncondBr) {}
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000033
Akira Hatanakafab89292012-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 Hatanakab7fa3c92012-07-31 21:49:49 +000041bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohman0d1e9a82008-10-03 15:45:36 +000042 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000043}
44
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000045/// insertNoop - If data hazard condition is found insert the target nop
46/// instruction.
47void MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000048insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000049{
Chris Lattner6f306d72010-04-02 20:16:16 +000050 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +000051 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000052}
53
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000054MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
55 unsigned Flag) const {
Akira Hatanaka1cf75762011-12-24 03:11:18 +000056 MachineFunction &MF = *MBB.getParent();
57 MachineFrameInfo &MFI = *MF.getFrameInfo();
58 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liuf54f60f2012-02-28 07:46:26 +000059
Akira Hatanaka1cf75762011-12-24 03:11:18 +000060 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
61 MFI.getObjectSize(FI), Align);
62}
63
Akira Hatanakae2489122011-04-15 21:51:11 +000064//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000065// Branch Analysis
Akira Hatanakae2489122011-04-15 21:51:11 +000066//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000067
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000068void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
69 MachineBasicBlock *&BB,
70 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka067d8152013-05-13 17:43:19 +000071 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
Akira Hatanaka93f898f2011-04-01 17:39:08 +000072 int NumOp = Inst->getNumExplicitOperands();
Jia Liuf54f60f2012-02-28 07:46:26 +000073
Akira Hatanaka93f898f2011-04-01 17:39:08 +000074 // for both int and fp branches, the last explicit operand is the
75 // MBB.
76 BB = Inst->getOperand(NumOp-1).getMBB();
77 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +000078
Akira Hatanaka93f898f2011-04-01 17:39:08 +000079 for (int i=0; i<NumOp-1; i++)
80 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000081}
82
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000083bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000084 MachineBasicBlock *&TBB,
85 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +000086 SmallVectorImpl<MachineOperand> &Cond,
Akira Hatanaka7320b232013-03-01 01:10:17 +000087 bool AllowModify) const {
88 SmallVector<MachineInstr*, 2> BranchInstrs;
89 BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
Akira Hatanakafcdd9b12012-09-13 17:12:37 +000090
Akira Hatanaka7320b232013-03-01 01:10:17 +000091 return (BT == BT_None) || (BT == BT_Indirect);
Jia Liuf54f60f2012-02-28 07:46:26 +000092}
93
Akira Hatanaka93f898f2011-04-01 17:39:08 +000094void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
95 MachineBasicBlock *TBB, DebugLoc DL,
96 const SmallVectorImpl<MachineOperand>& Cond)
97 const {
98 unsigned Opc = Cond[0].getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +000099 const MCInstrDesc &MCID = get(Opc);
100 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000101
Akira Hatanakafcdd9b12012-09-13 17:12:37 +0000102 for (unsigned i = 1; i < Cond.size(); ++i) {
103 if (Cond[i].isReg())
104 MIB.addReg(Cond[i].getReg());
105 else if (Cond[i].isImm())
106 MIB.addImm(Cond[i].getImm());
107 else
108 assert(true && "Cannot copy operand");
109 }
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000110 MIB.addMBB(TBB);
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000111}
112
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000113unsigned MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000114InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson4f6bf042008-08-14 22:49:33 +0000115 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000116 const SmallVectorImpl<MachineOperand> &Cond,
117 DebugLoc DL) const {
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000118 // Shouldn't be a fall through.
119 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000120
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000121 // # of condition operands:
122 // Unconditional branches: 0
123 // Floating point branches: 1 (opc)
124 // Int BranchZero: 2 (opc, reg)
125 // Int Branch: 3 (opc, reg0, reg1)
126 assert((Cond.size() <= 3) &&
127 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000128
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000129 // Two-way Conditional branch.
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000130 if (FBB) {
131 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000132 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000133 return 2;
134 }
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000135
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000136 // One way branch.
137 // Unconditional branch.
138 if (Cond.empty())
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000139 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000140 else // Conditional branch.
141 BuildCondBr(MBB, TBB, DL, Cond);
142 return 1;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000143}
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000144
145unsigned MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000146RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000147{
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000148 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
149 MachineBasicBlock::reverse_iterator FirstBr;
150 unsigned removed;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000151
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000152 // Skip all the debug instructions.
153 while (I != REnd && I->isDebugValue())
154 ++I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000155
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000156 FirstBr = I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000157
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000158 // Up to 2 branches are removed.
159 // Note that indirect branches are not removed.
160 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
Akira Hatanaka067d8152013-05-13 17:43:19 +0000161 if (!getAnalyzableBrOpc(I->getOpcode()))
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000162 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000163
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000164 MBB.erase(I.base(), FirstBr.base());
165
166 return removed;
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000167}
168
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000169/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000170/// specified Branch instruction.
171bool MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000172ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000173{
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000174 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000175 "Invalid Mips branch condition!");
Akira Hatanaka067d8152013-05-13 17:43:19 +0000176 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000177 return false;
178}
Dan Gohmand5ca70642009-06-03 20:30:14 +0000179
Akira Hatanaka7320b232013-03-01 01:10:17 +0000180MipsInstrInfo::BranchType MipsInstrInfo::
181AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
182 MachineBasicBlock *&FBB, SmallVectorImpl<MachineOperand> &Cond,
183 bool AllowModify,
184 SmallVectorImpl<MachineInstr*> &BranchInstrs) const {
185
186 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
187
188 // Skip all the debug instructions.
189 while (I != REnd && I->isDebugValue())
190 ++I;
191
192 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
193 // This block ends with no branches (it just falls through to its succ).
194 // Leave TBB/FBB null.
195 TBB = FBB = NULL;
196 return BT_NoBranch;
197 }
198
199 MachineInstr *LastInst = &*I;
200 unsigned LastOpc = LastInst->getOpcode();
201 BranchInstrs.push_back(LastInst);
202
203 // Not an analyzable branch (e.g., indirect jump).
Akira Hatanaka067d8152013-05-13 17:43:19 +0000204 if (!getAnalyzableBrOpc(LastOpc))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000205 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
206
207 // Get the second to last instruction in the block.
208 unsigned SecondLastOpc = 0;
209 MachineInstr *SecondLastInst = NULL;
210
211 if (++I != REnd) {
212 SecondLastInst = &*I;
Akira Hatanaka067d8152013-05-13 17:43:19 +0000213 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
Akira Hatanaka7320b232013-03-01 01:10:17 +0000214
215 // Not an analyzable branch (must be an indirect jump).
216 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
217 return BT_None;
218 }
219
Akira Hatanaka7320b232013-03-01 01:10:17 +0000220 // If there is only one terminator instruction, process it.
221 if (!SecondLastOpc) {
Matheus Almeida6de62d32013-10-01 12:53:00 +0000222 // Unconditional branch.
Akira Hatanaka7320b232013-03-01 01:10:17 +0000223 if (LastOpc == UncondBrOpc) {
224 TBB = LastInst->getOperand(0).getMBB();
225 return BT_Uncond;
226 }
227
228 // Conditional branch
229 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
230 return BT_Cond;
231 }
232
233 // If we reached here, there are two branches.
234 // If there are three terminators, we don't know what sort of block this is.
235 if (++I != REnd && isUnpredicatedTerminator(&*I))
236 return BT_None;
237
Akira Hatanaka28dc83c2013-03-01 01:22:26 +0000238 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
239
Akira Hatanaka7320b232013-03-01 01:10:17 +0000240 // If second to last instruction is an unconditional branch,
241 // analyze it and remove the last instruction.
242 if (SecondLastOpc == UncondBrOpc) {
243 // Return if the last instruction cannot be removed.
244 if (!AllowModify)
245 return BT_None;
246
247 TBB = SecondLastInst->getOperand(0).getMBB();
248 LastInst->eraseFromParent();
249 BranchInstrs.pop_back();
250 return BT_Uncond;
251 }
252
253 // Conditional branch followed by an unconditional branch.
254 // The last one must be unconditional.
255 if (LastOpc != UncondBrOpc)
256 return BT_None;
257
258 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
259 FBB = LastInst->getOperand(0).getMBB();
260
261 return BT_CondUncond;
262}
263
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000264/// Return the number of bytes of code the specified instruction may be.
265unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
266 switch (MI->getOpcode()) {
267 default:
268 return MI->getDesc().getSize();
269 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
270 const MachineFunction *MF = MI->getParent()->getParent();
271 const char *AsmStr = MI->getOperand(0).getSymbolName();
272 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
273 }
Reed Kotler91ae9822013-10-27 21:57:36 +0000274 case Mips::CONSTPOOL_ENTRY:
275 // If this machine instr is a constant pool entry, its size is recorded as
276 // operand #2.
277 return MI->getOperand(2).getImm();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000278 }
279}
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000280
281MachineInstrBuilder
282MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
283 MachineBasicBlock::iterator I) const {
284 MachineInstrBuilder MIB;
285 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
286
287 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
288 MIB.addOperand(I->getOperand(J));
289
290 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
291 return MIB;
292}