blob: 5b0d4eb6f0a8f0a81b67529317b19b07b13f61fe [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 Hatanaka0bc1adb2012-07-31 21:49:49 +000034bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohmand735b802008-10-03 15:45:36 +000035 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036}
37
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000038/// insertNoop - If data hazard condition is found insert the target nop
39/// instruction.
40void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000041insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000042{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000043 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000044 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000045}
46
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000047MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
48 unsigned Flag) const {
Akira Hatanakafd1d9252011-12-24 03:11:18 +000049 MachineFunction &MF = *MBB.getParent();
50 MachineFrameInfo &MFI = *MF.getFrameInfo();
51 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liubb481f82012-02-28 07:46:26 +000052
Akira Hatanakafd1d9252011-12-24 03:11:18 +000053 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
54 MFI.getObjectSize(FI), Align);
55}
56
Akira Hatanakac4f24eb2011-07-01 01:04:43 +000057MachineInstr*
58MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
59 uint64_t Offset, const MDNode *MDPtr,
60 DebugLoc DL) const {
61 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
62 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
63 return &*MIB;
64}
65
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000066//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000067// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000068//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000069
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000070void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
71 MachineBasicBlock *&BB,
72 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka20ada982011-04-01 17:39:08 +000073 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
74 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +000075
Akira Hatanaka20ada982011-04-01 17:39:08 +000076 // for both int and fp branches, the last explicit operand is the
77 // MBB.
78 BB = Inst->getOperand(NumOp-1).getMBB();
79 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +000080
Akira Hatanaka20ada982011-04-01 17:39:08 +000081 for (int i=0; i<NumOp-1; i++)
82 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000083}
84
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000085bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000086 MachineBasicBlock *&TBB,
87 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +000088 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000089 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000090{
Akira Hatanaka20ada982011-04-01 17:39:08 +000091 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000092
Akira Hatanaka20ada982011-04-01 17:39:08 +000093 // Skip all the debug instructions.
94 while (I != REnd && I->isDebugValue())
95 ++I;
96
97 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
98 // If this block ends with no branches (it just falls through to its succ)
99 // just return false, leaving TBB/FBB null.
100 TBB = FBB = NULL;
101 return false;
102 }
103
104 MachineInstr *LastInst = &*I;
105 unsigned LastOpc = LastInst->getOpcode();
106
107 // Not an analyzable branch (must be an indirect jump).
108 if (!GetAnalyzableBrOpc(LastOpc))
109 return true;
110
111 // Get the second to last instruction in the block.
112 unsigned SecondLastOpc = 0;
113 MachineInstr *SecondLastInst = NULL;
114
115 if (++I != REnd) {
116 SecondLastInst = &*I;
117 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
118
119 // Not an analyzable branch (must be an indirect jump).
120 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
121 return true;
122 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000123
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000124 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000125 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000126 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000127 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000128 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000129 return false;
130 }
131
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000132 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000133 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
134 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000135 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000136
Akira Hatanaka20ada982011-04-01 17:39:08 +0000137 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000138 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000139 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000140 return true;
141
Akira Hatanaka20ada982011-04-01 17:39:08 +0000142 // If second to last instruction is an unconditional branch,
143 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000144 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000145 // Return if the last instruction cannot be removed.
146 if (!AllowModify)
147 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000148
Chris Lattner8aa797a2007-12-30 23:10:15 +0000149 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000150 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000151 return false;
152 }
153
Akira Hatanaka20ada982011-04-01 17:39:08 +0000154 // Conditional branch followed by an unconditional branch.
155 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000156 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000157 return true;
158
159 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
160 FBB = LastInst->getOperand(0).getMBB();
161
162 return false;
Jia Liubb481f82012-02-28 07:46:26 +0000163}
164
Akira Hatanaka20ada982011-04-01 17:39:08 +0000165void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
166 MachineBasicBlock *TBB, DebugLoc DL,
167 const SmallVectorImpl<MachineOperand>& Cond)
168 const {
169 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000170 const MCInstrDesc &MCID = get(Opc);
171 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000172
173 for (unsigned i = 1; i < Cond.size(); ++i)
174 MIB.addReg(Cond[i].getReg());
175
176 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000177}
178
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000179unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000180InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000181 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000182 const SmallVectorImpl<MachineOperand> &Cond,
183 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000184 // Shouldn't be a fall through.
185 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000186
Akira Hatanaka20ada982011-04-01 17:39:08 +0000187 // # of condition operands:
188 // Unconditional branches: 0
189 // Floating point branches: 1 (opc)
190 // Int BranchZero: 2 (opc, reg)
191 // Int Branch: 3 (opc, reg0, reg1)
192 assert((Cond.size() <= 3) &&
193 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000194
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000195 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000196 if (FBB) {
197 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000198 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000199 return 2;
200 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000201
Akira Hatanaka20ada982011-04-01 17:39:08 +0000202 // One way branch.
203 // Unconditional branch.
204 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000205 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000206 else // Conditional branch.
207 BuildCondBr(MBB, TBB, DL, Cond);
208 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000209}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000210
211unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000212RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000213{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000214 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
215 MachineBasicBlock::reverse_iterator FirstBr;
216 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000217
Akira Hatanaka20ada982011-04-01 17:39:08 +0000218 // Skip all the debug instructions.
219 while (I != REnd && I->isDebugValue())
220 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000221
Akira Hatanaka20ada982011-04-01 17:39:08 +0000222 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000223
Akira Hatanaka20ada982011-04-01 17:39:08 +0000224 // Up to 2 branches are removed.
225 // Note that indirect branches are not removed.
226 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
227 if (!GetAnalyzableBrOpc(I->getOpcode()))
228 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000229
Akira Hatanaka20ada982011-04-01 17:39:08 +0000230 MBB.erase(I.base(), FirstBr.base());
231
232 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000233}
234
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000235/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000236/// specified Branch instruction.
237bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000238ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000239{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000240 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000241 "Invalid Mips branch condition!");
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +0000242 Cond[0].setImm(GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000243 return false;
244}
Dan Gohman99114052009-06-03 20:30:14 +0000245
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000246/// Return the number of bytes of code the specified instruction may be.
247unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
248 switch (MI->getOpcode()) {
249 default:
250 return MI->getDesc().getSize();
251 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
252 const MachineFunction *MF = MI->getParent()->getParent();
253 const char *AsmStr = MI->getOperand(0).getSymbolName();
254 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
255 }
256 }
257}
258
259unsigned
260llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII,
261 MachineBasicBlock& MBB,
262 MachineBasicBlock::iterator II, DebugLoc DL,
263 bool LastInstrIsADDiu,
264 MipsAnalyzeImmediate::Inst *LastInst) {
265 MipsAnalyzeImmediate AnalyzeImm;
266 unsigned Size = IsN64 ? 64 : 32;
267 unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi;
268 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
269 unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT;
270
271 const MipsAnalyzeImmediate::InstSeq &Seq =
272 AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
273 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
274
275 if (LastInst && (Seq.size() == 1)) {
276 *LastInst = *Inst;
277 return 0;
278 }
279
280 // The first instruction can be a LUi, which is different from other
281 // instructions (ADDiu, ORI and SLL) in that it does not have a register
282 // operand.
283 if (Inst->Opc == LUi)
284 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
285 .addImm(SignExtend64<16>(Inst->ImmOpnd));
286 else
287 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
288 .addImm(SignExtend64<16>(Inst->ImmOpnd));
289
290 // Build the remaining instructions in Seq. Skip the last instruction if
291 // LastInst is not 0.
292 for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst)
293 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
294 .addImm(SignExtend64<16>(Inst->ImmOpnd));
295
296 if (LastInst)
297 *LastInst = *Inst;
298
299 return Seq.size() - !!LastInst;
300}