blob: 76644c1584e766ec712dff3623b0536ae0bdaf09 [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
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#include "MipsInstrInfo.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Chandler Carruthd04a8d42012-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 Lopes972f5892007-06-06 07:42:06 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.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 Hatanakae6ac7d62012-09-13 17:12:37 +000098
Akira Hatanaka20ada982011-04-01 17:39:08 +000099 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000100
Akira Hatanaka20ada982011-04-01 17:39:08 +0000101 // Skip all the debug instructions.
102 while (I != REnd && I->isDebugValue())
103 ++I;
104
105 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
106 // If this block ends with no branches (it just falls through to its succ)
107 // just return false, leaving TBB/FBB null.
108 TBB = FBB = NULL;
109 return false;
110 }
111
112 MachineInstr *LastInst = &*I;
113 unsigned LastOpc = LastInst->getOpcode();
114
115 // Not an analyzable branch (must be an indirect jump).
116 if (!GetAnalyzableBrOpc(LastOpc))
117 return true;
118
119 // Get the second to last instruction in the block.
120 unsigned SecondLastOpc = 0;
121 MachineInstr *SecondLastInst = NULL;
122
123 if (++I != REnd) {
124 SecondLastInst = &*I;
125 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
126
127 // Not an analyzable branch (must be an indirect jump).
128 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
129 return true;
130 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000131
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000132 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000133 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000134 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000135 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000136 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000137 return false;
138 }
139
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000140 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000141 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
142 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000143 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000144
Akira Hatanaka20ada982011-04-01 17:39:08 +0000145 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000146 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000147 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000148 return true;
149
Akira Hatanaka20ada982011-04-01 17:39:08 +0000150 // If second to last instruction is an unconditional branch,
151 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000152 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000153 // Return if the last instruction cannot be removed.
154 if (!AllowModify)
155 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000156
Chris Lattner8aa797a2007-12-30 23:10:15 +0000157 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000158 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000159 return false;
160 }
161
Akira Hatanaka20ada982011-04-01 17:39:08 +0000162 // Conditional branch followed by an unconditional branch.
163 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000164 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000165 return true;
166
167 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
168 FBB = LastInst->getOperand(0).getMBB();
169
170 return false;
Jia Liubb481f82012-02-28 07:46:26 +0000171}
172
Akira Hatanaka20ada982011-04-01 17:39:08 +0000173void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
174 MachineBasicBlock *TBB, DebugLoc DL,
175 const SmallVectorImpl<MachineOperand>& Cond)
176 const {
177 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000178 const MCInstrDesc &MCID = get(Opc);
179 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000180
Akira Hatanakae6ac7d62012-09-13 17:12:37 +0000181 for (unsigned i = 1; i < Cond.size(); ++i) {
182 if (Cond[i].isReg())
183 MIB.addReg(Cond[i].getReg());
184 else if (Cond[i].isImm())
185 MIB.addImm(Cond[i].getImm());
186 else
187 assert(true && "Cannot copy operand");
188 }
Akira Hatanaka20ada982011-04-01 17:39:08 +0000189 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000190}
191
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000192unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000193InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000194 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000195 const SmallVectorImpl<MachineOperand> &Cond,
196 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000197 // Shouldn't be a fall through.
198 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000199
Akira Hatanaka20ada982011-04-01 17:39:08 +0000200 // # of condition operands:
201 // Unconditional branches: 0
202 // Floating point branches: 1 (opc)
203 // Int BranchZero: 2 (opc, reg)
204 // Int Branch: 3 (opc, reg0, reg1)
205 assert((Cond.size() <= 3) &&
206 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000207
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000208 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000209 if (FBB) {
210 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000211 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000212 return 2;
213 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000214
Akira Hatanaka20ada982011-04-01 17:39:08 +0000215 // One way branch.
216 // Unconditional branch.
217 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000218 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000219 else // Conditional branch.
220 BuildCondBr(MBB, TBB, DL, Cond);
221 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000222}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000223
224unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000225RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000226{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000227 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
228 MachineBasicBlock::reverse_iterator FirstBr;
229 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000230
Akira Hatanaka20ada982011-04-01 17:39:08 +0000231 // Skip all the debug instructions.
232 while (I != REnd && I->isDebugValue())
233 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000234
Akira Hatanaka20ada982011-04-01 17:39:08 +0000235 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000236
Akira Hatanaka20ada982011-04-01 17:39:08 +0000237 // Up to 2 branches are removed.
238 // Note that indirect branches are not removed.
239 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
240 if (!GetAnalyzableBrOpc(I->getOpcode()))
241 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000242
Akira Hatanaka20ada982011-04-01 17:39:08 +0000243 MBB.erase(I.base(), FirstBr.base());
244
245 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000246}
247
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000248/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000249/// specified Branch instruction.
250bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000251ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000252{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000253 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000254 "Invalid Mips branch condition!");
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +0000255 Cond[0].setImm(GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000256 return false;
257}
Dan Gohman99114052009-06-03 20:30:14 +0000258
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000259/// Return the number of bytes of code the specified instruction may be.
260unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
261 switch (MI->getOpcode()) {
262 default:
263 return MI->getDesc().getSize();
264 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
265 const MachineFunction *MF = MI->getParent()->getParent();
266 const char *AsmStr = MI->getOperand(0).getSymbolName();
267 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
268 }
269 }
270}