]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Target/Mips/MipsInstrInfo.cpp
Vendor import of llvm trunk r305575:
[FreeBSD/FreeBSD.git] / lib / Target / Mips / MipsInstrInfo.cpp
1 //===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Mips implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsInstrInfo.h"
15 #include "InstPrinter/MipsInstPrinter.h"
16 #include "MipsMachineFunction.h"
17 #include "MipsSubtarget.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/TargetRegistry.h"
23
24 using namespace llvm;
25
26 #define GET_INSTRINFO_CTOR_DTOR
27 #include "MipsGenInstrInfo.inc"
28
29 // Pin the vtable to this file.
30 void MipsInstrInfo::anchor() {}
31
32 MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr)
33     : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
34       Subtarget(STI), UncondBrOpc(UncondBr) {}
35
36 const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
37   if (STI.inMips16Mode())
38     return llvm::createMips16InstrInfo(STI);
39
40   return llvm::createMipsSEInstrInfo(STI);
41 }
42
43 bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
44   return op.isImm() && op.getImm() == 0;
45 }
46
47 /// insertNoop - If data hazard condition is found insert the target nop
48 /// instruction.
49 // FIXME: This appears to be dead code.
50 void MipsInstrInfo::
51 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
52 {
53   DebugLoc DL;
54   BuildMI(MBB, MI, DL, get(Mips::NOP));
55 }
56
57 MachineMemOperand *
58 MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
59                              MachineMemOperand::Flags Flags) const {
60   MachineFunction &MF = *MBB.getParent();
61   MachineFrameInfo &MFI = MF.getFrameInfo();
62   unsigned Align = MFI.getObjectAlignment(FI);
63
64   return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
65                                  Flags, MFI.getObjectSize(FI), Align);
66 }
67
68 //===----------------------------------------------------------------------===//
69 // Branch Analysis
70 //===----------------------------------------------------------------------===//
71
72 void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
73                                   MachineBasicBlock *&BB,
74                                   SmallVectorImpl<MachineOperand> &Cond) const {
75   assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
76   int NumOp = Inst->getNumExplicitOperands();
77
78   // for both int and fp branches, the last explicit operand is the
79   // MBB.
80   BB = Inst->getOperand(NumOp-1).getMBB();
81   Cond.push_back(MachineOperand::CreateImm(Opc));
82
83   for (int i=0; i<NumOp-1; i++)
84     Cond.push_back(Inst->getOperand(i));
85 }
86
87 bool MipsInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
88                                   MachineBasicBlock *&TBB,
89                                   MachineBasicBlock *&FBB,
90                                   SmallVectorImpl<MachineOperand> &Cond,
91                                   bool AllowModify) const {
92   SmallVector<MachineInstr*, 2> BranchInstrs;
93   BranchType BT = analyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
94
95   return (BT == BT_None) || (BT == BT_Indirect);
96 }
97
98 void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
99                                 const DebugLoc &DL,
100                                 ArrayRef<MachineOperand> Cond) const {
101   unsigned Opc = Cond[0].getImm();
102   const MCInstrDesc &MCID = get(Opc);
103   MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
104
105   for (unsigned i = 1; i < Cond.size(); ++i) {
106     assert((Cond[i].isImm() || Cond[i].isReg()) &&
107            "Cannot copy operand for conditional branch!");
108     MIB.add(Cond[i]);
109   }
110   MIB.addMBB(TBB);
111 }
112
113 unsigned MipsInstrInfo::insertBranch(MachineBasicBlock &MBB,
114                                      MachineBasicBlock *TBB,
115                                      MachineBasicBlock *FBB,
116                                      ArrayRef<MachineOperand> Cond,
117                                      const DebugLoc &DL,
118                                      int *BytesAdded) const {
119   // Shouldn't be a fall through.
120   assert(TBB && "insertBranch must not be told to insert a fallthrough");
121   assert(!BytesAdded && "code size not handled");
122
123   // # of condition operands:
124   //  Unconditional branches: 0
125   //  Floating point branches: 1 (opc)
126   //  Int BranchZero: 2 (opc, reg)
127   //  Int Branch: 3 (opc, reg0, reg1)
128   assert((Cond.size() <= 3) &&
129          "# of Mips branch conditions must be <= 3!");
130
131   // Two-way Conditional branch.
132   if (FBB) {
133     BuildCondBr(MBB, TBB, DL, Cond);
134     BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
135     return 2;
136   }
137
138   // One way branch.
139   // Unconditional branch.
140   if (Cond.empty())
141     BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
142   else // Conditional branch.
143     BuildCondBr(MBB, TBB, DL, Cond);
144   return 1;
145 }
146
147 unsigned MipsInstrInfo::removeBranch(MachineBasicBlock &MBB,
148                                      int *BytesRemoved) const {
149   assert(!BytesRemoved && "code size not handled");
150
151   MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
152   unsigned removed;
153
154   // Skip all the debug instructions.
155   while (I != REnd && I->isDebugValue())
156     ++I;
157
158   if (I == REnd)
159     return 0;
160
161   MachineBasicBlock::iterator FirstBr = ++I.getReverse();
162
163   // Up to 2 branches are removed.
164   // Note that indirect branches are not removed.
165   for (removed = 0; I != REnd && removed < 2; ++I, ++removed)
166     if (!getAnalyzableBrOpc(I->getOpcode()))
167       break;
168
169   MBB.erase((--I).getReverse(), FirstBr);
170
171   return removed;
172 }
173
174 /// reverseBranchCondition - Return the inverse opcode of the
175 /// specified Branch instruction.
176 bool MipsInstrInfo::reverseBranchCondition(
177     SmallVectorImpl<MachineOperand> &Cond) const {
178   assert( (Cond.size() && Cond.size() <= 3) &&
179           "Invalid Mips branch condition!");
180   Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
181   return false;
182 }
183
184 MipsInstrInfo::BranchType MipsInstrInfo::analyzeBranch(
185     MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
186     SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
187     SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
188
189   MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
190
191   // Skip all the debug instructions.
192   while (I != REnd && I->isDebugValue())
193     ++I;
194
195   if (I == REnd || !isUnpredicatedTerminator(*I)) {
196     // This block ends with no branches (it just falls through to its succ).
197     // Leave TBB/FBB null.
198     TBB = FBB = nullptr;
199     return BT_NoBranch;
200   }
201
202   MachineInstr *LastInst = &*I;
203   unsigned LastOpc = LastInst->getOpcode();
204   BranchInstrs.push_back(LastInst);
205
206   // Not an analyzable branch (e.g., indirect jump).
207   if (!getAnalyzableBrOpc(LastOpc))
208     return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
209
210   // Get the second to last instruction in the block.
211   unsigned SecondLastOpc = 0;
212   MachineInstr *SecondLastInst = nullptr;
213
214   if (++I != REnd) {
215     SecondLastInst = &*I;
216     SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
217
218     // Not an analyzable branch (must be an indirect jump).
219     if (isUnpredicatedTerminator(*SecondLastInst) && !SecondLastOpc)
220       return BT_None;
221   }
222
223   // If there is only one terminator instruction, process it.
224   if (!SecondLastOpc) {
225     // Unconditional branch.
226     if (LastInst->isUnconditionalBranch()) {
227       TBB = LastInst->getOperand(0).getMBB();
228       return BT_Uncond;
229     }
230
231     // Conditional branch
232     AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
233     return BT_Cond;
234   }
235
236   // If we reached here, there are two branches.
237   // If there are three terminators, we don't know what sort of block this is.
238   if (++I != REnd && isUnpredicatedTerminator(*I))
239     return BT_None;
240
241   BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
242
243   // If second to last instruction is an unconditional branch,
244   // analyze it and remove the last instruction.
245   if (SecondLastInst->isUnconditionalBranch()) {
246     // Return if the last instruction cannot be removed.
247     if (!AllowModify)
248       return BT_None;
249
250     TBB = SecondLastInst->getOperand(0).getMBB();
251     LastInst->eraseFromParent();
252     BranchInstrs.pop_back();
253     return BT_Uncond;
254   }
255
256   // Conditional branch followed by an unconditional branch.
257   // The last one must be unconditional.
258   if (!LastInst->isUnconditionalBranch())
259     return BT_None;
260
261   AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
262   FBB = LastInst->getOperand(0).getMBB();
263
264   return BT_CondUncond;
265 }
266
267 /// Return the corresponding compact (no delay slot) form of a branch.
268 unsigned MipsInstrInfo::getEquivalentCompactForm(
269     const MachineBasicBlock::iterator I) const {
270   unsigned Opcode = I->getOpcode();
271   bool canUseShortMicroMipsCTI = false;
272
273   if (Subtarget.inMicroMipsMode()) {
274     switch (Opcode) {
275     case Mips::BNE:
276     case Mips::BNE_MM:
277     case Mips::BEQ:
278     case Mips::BEQ_MM:
279     // microMIPS has NE,EQ branches that do not have delay slots provided one
280     // of the operands is zero.
281       if (I->getOperand(1).getReg() == Subtarget.getABI().GetZeroReg())
282         canUseShortMicroMipsCTI = true;
283       break;
284     // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
285     // expanded to JR_MM, so they can be replaced with JRC16_MM.
286     case Mips::JR:
287     case Mips::PseudoReturn:
288     case Mips::PseudoIndirectBranch:
289     case Mips::TAILCALLREG:
290       canUseShortMicroMipsCTI = true;
291       break;
292     }
293   }
294
295   // MIPSR6 forbids both operands being the zero register.
296   if (Subtarget.hasMips32r6() && (I->getNumOperands() > 1) &&
297       (I->getOperand(0).isReg() &&
298        (I->getOperand(0).getReg() == Mips::ZERO ||
299         I->getOperand(0).getReg() == Mips::ZERO_64)) &&
300       (I->getOperand(1).isReg() &&
301        (I->getOperand(1).getReg() == Mips::ZERO ||
302         I->getOperand(1).getReg() == Mips::ZERO_64)))
303     return 0;
304
305   if (Subtarget.hasMips32r6() || canUseShortMicroMipsCTI) {
306     switch (Opcode) {
307     case Mips::B:
308       return Mips::BC;
309     case Mips::BAL:
310       return Mips::BALC;
311     case Mips::BEQ:
312     case Mips::BEQ_MM:
313       if (canUseShortMicroMipsCTI)
314         return Mips::BEQZC_MM;
315       else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
316         return 0;
317       return Mips::BEQC;
318     case Mips::BNE:
319     case Mips::BNE_MM:
320       if (canUseShortMicroMipsCTI)
321         return Mips::BNEZC_MM;
322       else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
323         return 0;
324       return Mips::BNEC;
325     case Mips::BGE:
326       if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
327         return 0;
328       return Mips::BGEC;
329     case Mips::BGEU:
330       if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
331         return 0;
332       return Mips::BGEUC;
333     case Mips::BGEZ:
334       return Mips::BGEZC;
335     case Mips::BGTZ:
336       return Mips::BGTZC;
337     case Mips::BLEZ:
338       return Mips::BLEZC;
339     case Mips::BLT:
340       if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
341         return 0;
342       return Mips::BLTC;
343     case Mips::BLTU:
344       if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
345         return 0;
346       return Mips::BLTUC;
347     case Mips::BLTZ:
348       return Mips::BLTZC;
349     case Mips::BEQ64:
350       if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
351         return 0;
352       return Mips::BEQC64;
353     case Mips::BNE64:
354       if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
355         return 0;
356       return Mips::BNEC64;
357     case Mips::BGTZ64:
358       return Mips::BGTZC64;
359     case Mips::BGEZ64:
360       return Mips::BGEZC64;
361     case Mips::BLTZ64:
362       return Mips::BLTZC64;
363     case Mips::BLEZ64:
364       return Mips::BLEZC64;
365     // For MIPSR6, the instruction 'jic' can be used for these cases. Some
366     // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
367     case Mips::JR:
368     case Mips::PseudoReturn:
369     case Mips::PseudoIndirectBranch:
370     case Mips::TAILCALLREG:
371       if (canUseShortMicroMipsCTI)
372         return Mips::JRC16_MM;
373       return Mips::JIC;
374     case Mips::JALRPseudo:
375       return Mips::JIALC;
376     case Mips::JR64:
377     case Mips::PseudoReturn64:
378     case Mips::PseudoIndirectBranch64:
379     case Mips::TAILCALLREG64:
380       return Mips::JIC64;
381     case Mips::JALR64Pseudo:
382       return Mips::JIALC64;
383     default:
384       return 0;
385     }
386   }
387
388   return 0;
389 }
390
391 /// Predicate for distingushing between control transfer instructions and all
392 /// other instructions for handling forbidden slots. Consider inline assembly
393 /// as unsafe as well.
394 bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr &MI) const {
395   if (MI.isInlineAsm())
396     return false;
397
398   return (MI.getDesc().TSFlags & MipsII::IsCTI) == 0;
399
400 }
401
402 /// Predicate for distingushing instructions that have forbidden slots.
403 bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr &MI) const {
404   return (MI.getDesc().TSFlags & MipsII::HasForbiddenSlot) != 0;
405 }
406
407 /// Return the number of bytes of code the specified instruction may be.
408 unsigned MipsInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
409   switch (MI.getOpcode()) {
410   default:
411     return MI.getDesc().getSize();
412   case  TargetOpcode::INLINEASM: {       // Inline Asm: Variable size.
413     const MachineFunction *MF = MI.getParent()->getParent();
414     const char *AsmStr = MI.getOperand(0).getSymbolName();
415     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
416   }
417   case Mips::CONSTPOOL_ENTRY:
418     // If this machine instr is a constant pool entry, its size is recorded as
419     // operand #2.
420     return MI.getOperand(2).getImm();
421   }
422 }
423
424 MachineInstrBuilder
425 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
426                                   MachineBasicBlock::iterator I) const {
427   MachineInstrBuilder MIB;
428
429   // Certain branches have two forms: e.g beq $1, $zero, dest vs beqz $1, dest
430   // Pick the zero form of the branch for readable assembly and for greater
431   // branch distance in non-microMIPS mode.
432   // Additional MIPSR6 does not permit the use of register $zero for compact
433   // branches.
434   // FIXME: Certain atomic sequences on mips64 generate 32bit references to
435   // Mips::ZERO, which is incorrect. This test should be updated to use
436   // Subtarget.getABI().GetZeroReg() when those atomic sequences and others
437   // are fixed.
438   int ZeroOperandPosition = -1;
439   bool BranchWithZeroOperand = false;
440   if (I->isBranch() && !I->isPseudo()) {
441     auto TRI = I->getParent()->getParent()->getSubtarget().getRegisterInfo();
442     ZeroOperandPosition = I->findRegisterUseOperandIdx(Mips::ZERO, false, TRI);
443     BranchWithZeroOperand = ZeroOperandPosition != -1;
444   }
445
446   if (BranchWithZeroOperand) {
447     switch (NewOpc) {
448     case Mips::BEQC:
449       NewOpc = Mips::BEQZC;
450       break;
451     case Mips::BNEC:
452       NewOpc = Mips::BNEZC;
453       break;
454     case Mips::BGEC:
455       NewOpc = Mips::BGEZC;
456       break;
457     case Mips::BLTC:
458       NewOpc = Mips::BLTZC;
459       break;
460     case Mips::BEQC64:
461       NewOpc = Mips::BEQZC64;
462       break;
463     case Mips::BNEC64:
464       NewOpc = Mips::BNEZC64;
465       break;
466     }
467   }
468
469   MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
470
471   // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an
472   // immediate 0 as an operand and requires the removal of it's %RA<imp-def>
473   // implicit operand as copying the implicit operations of the instructio we're
474   // looking at will give us the correct flags.
475   if (NewOpc == Mips::JIC || NewOpc == Mips::JIALC || NewOpc == Mips::JIC64 ||
476       NewOpc == Mips::JIALC64) {
477
478     if (NewOpc == Mips::JIALC || NewOpc == Mips::JIALC64)
479       MIB->RemoveOperand(0);
480
481     for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
482       MIB.add(I->getOperand(J));
483     }
484
485     MIB.addImm(0);
486
487   } else {
488     for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
489       if (BranchWithZeroOperand && (unsigned)ZeroOperandPosition == J)
490         continue;
491
492       MIB.add(I->getOperand(J));
493     }
494   }
495
496   MIB.copyImplicitOps(*I);
497
498   MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
499   return MIB;
500 }
501
502 bool MipsInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
503                                           unsigned &SrcOpIdx2) const {
504   assert(!MI.isBundle() &&
505          "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
506
507   const MCInstrDesc &MCID = MI.getDesc();
508   if (!MCID.isCommutable())
509     return false;
510
511   switch (MI.getOpcode()) {
512   case Mips::DPADD_U_H:
513   case Mips::DPADD_U_W:
514   case Mips::DPADD_U_D:
515   case Mips::DPADD_S_H:
516   case Mips::DPADD_S_W:
517   case Mips::DPADD_S_D: {
518     // The first operand is both input and output, so it should not commute
519     if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3))
520       return false;
521
522     if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
523       return false;
524     return true;
525   }
526   }
527   return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
528 }