blob: 45d700d8afd64b79beab54e7b25c9b375a638983 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsInstrInfo.h - Mips Instruction Information ----------*- C++ -*-===//
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//
Sasa Stankovic7b061a42014-04-30 15:06:25 +000012// FIXME: We need to override TargetInstrInfo::getInlineAsmLength method in
13// order for MipsLongBranch pass to work correctly when the code has inline
14// assembly. The returned value doesn't have to be the asm instruction's exact
15// size in bytes; MipsLongBranch only expects it to be the correct upper bound.
Akira Hatanakae2489122011-04-15 21:51:11 +000016//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000017
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000018#ifndef LLVM_LIB_TARGET_MIPS_MIPSINSTRINFO_H
19#define LLVM_LIB_TARGET_MIPS_MIPSINSTRINFO_H
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000020
21#include "Mips.h"
Craig Topperb25fda92012-03-17 18:46:09 +000022#include "MipsRegisterInfo.h"
Akira Hatanaka310e26a2013-05-13 17:57:42 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Torok Edwin56d06592009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000025#include "llvm/Target/TargetInstrInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026
Evan Cheng703a0fb2011-07-01 17:57:27 +000027#define GET_INSTRINFO_HEADER
28#include "MipsGenInstrInfo.inc"
29
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030namespace llvm {
Eric Christophera20c3cf2015-03-12 05:43:57 +000031class MipsSubtarget;
Evan Cheng703a0fb2011-07-01 17:57:27 +000032class MipsInstrInfo : public MipsGenInstrInfo {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000033 virtual void anchor();
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000034protected:
Eric Christopher675cb4d2014-07-18 23:25:00 +000035 const MipsSubtarget &Subtarget;
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +000036 unsigned UncondBrOpc;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000037
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000038public:
Akira Hatanaka7320b232013-03-01 01:10:17 +000039 enum BranchType {
40 BT_None, // Couldn't analyze branch.
41 BT_NoBranch, // No branches found.
42 BT_Uncond, // One unconditional branch.
43 BT_Cond, // One conditional branch.
44 BT_CondUncond, // A conditional branch followed by an unconditional branch.
45 BT_Indirect // One indirct branch.
46 };
47
Eric Christopher675cb4d2014-07-18 23:25:00 +000048 explicit MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBrOpc);
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000049
Eric Christopher675cb4d2014-07-18 23:25:00 +000050 static const MipsInstrInfo *create(MipsSubtarget &STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000051
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000052 /// Branch Analysis
Jacques Pienaar71c30a12016-07-15 14:41:04 +000053 bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Craig Topper56c590a2014-04-29 07:58:02 +000054 MachineBasicBlock *&FBB,
55 SmallVectorImpl<MachineOperand> &Cond,
56 bool AllowModify) const override;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000057
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +000058 unsigned removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +000059 int *BytesRemoved = nullptr) const override;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000060
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +000061 unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +000062 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +000063 const DebugLoc &DL,
64 int *BytesAdded = nullptr) const override;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000065
Craig Topper56c590a2014-04-29 07:58:02 +000066 bool
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +000067 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000068
Jacques Pienaar71c30a12016-07-15 14:41:04 +000069 BranchType analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Akira Hatanaka7320b232013-03-01 01:10:17 +000070 MachineBasicBlock *&FBB,
71 SmallVectorImpl<MachineOperand> &Cond,
72 bool AllowModify,
Jacques Pienaar71c30a12016-07-15 14:41:04 +000073 SmallVectorImpl<MachineInstr *> &BranchInstrs) const;
Akira Hatanaka7320b232013-03-01 01:10:17 +000074
Daniel Sanderse8efff32016-03-14 16:24:05 +000075 /// Determine the opcode of a non-delay slot form for a branch if one exists.
76 unsigned getEquivalentCompactForm(const MachineBasicBlock::iterator I) const;
77
78 /// Predicate to determine if an instruction can go in a forbidden slot.
79 bool SafeInForbiddenSlot(const MachineInstr &MI) const;
80
81 /// Predicate to determine if an instruction has a forbidden slot.
82 bool HasForbiddenSlot(const MachineInstr &MI) const;
83
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000084 /// Insert nop instruction when hazard condition is found
Craig Topper56c590a2014-04-29 07:58:02 +000085 void insertNoop(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator MI) const override;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000087
88 /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
89 /// such, whenever a client has an instance of instruction info, it should
90 /// always be able to get register info as well (through this method).
91 ///
Akira Hatanakacb37e132012-07-31 23:41:32 +000092 virtual const MipsRegisterInfo &getRegisterInfo() const = 0;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000093
Akira Hatanaka067d8152013-05-13 17:43:19 +000094 virtual unsigned getOppositeBranchOpc(unsigned Opc) const = 0;
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +000095
96 /// Return the number of bytes of code the specified instruction may be.
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +000097 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000098
Craig Topper56c590a2014-04-29 07:58:02 +000099 void storeRegToStackSlot(MachineBasicBlock &MBB,
100 MachineBasicBlock::iterator MBBI,
101 unsigned SrcReg, bool isKill, int FrameIndex,
102 const TargetRegisterClass *RC,
103 const TargetRegisterInfo *TRI) const override {
Akira Hatanaka465facca2013-03-29 02:14:12 +0000104 storeRegToStack(MBB, MBBI, SrcReg, isKill, FrameIndex, RC, TRI, 0);
105 }
106
Craig Topper56c590a2014-04-29 07:58:02 +0000107 void loadRegFromStackSlot(MachineBasicBlock &MBB,
108 MachineBasicBlock::iterator MBBI,
109 unsigned DestReg, int FrameIndex,
110 const TargetRegisterClass *RC,
111 const TargetRegisterInfo *TRI) const override {
Akira Hatanaka465facca2013-03-29 02:14:12 +0000112 loadRegFromStack(MBB, MBBI, DestReg, FrameIndex, RC, TRI, 0);
113 }
114
115 virtual void storeRegToStack(MachineBasicBlock &MBB,
116 MachineBasicBlock::iterator MI,
117 unsigned SrcReg, bool isKill, int FrameIndex,
118 const TargetRegisterClass *RC,
119 const TargetRegisterInfo *TRI,
120 int64_t Offset) const = 0;
121
122 virtual void loadRegFromStack(MachineBasicBlock &MBB,
123 MachineBasicBlock::iterator MI,
124 unsigned DestReg, int FrameIndex,
125 const TargetRegisterClass *RC,
126 const TargetRegisterInfo *TRI,
127 int64_t Offset) const = 0;
128
Vasileios Kalintiris6d687782015-04-02 10:42:44 +0000129 virtual void adjustStackPtr(unsigned SP, int64_t Amount,
130 MachineBasicBlock &MBB,
131 MachineBasicBlock::iterator I) const = 0;
132
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000133 /// Create an instruction which has the same operands and memory operands
134 /// as MI but has a new opcode.
135 MachineInstrBuilder genInstrWithNewOpc(unsigned NewOpc,
136 MachineBasicBlock::iterator I) const;
137
Petar Jovanovic9bff3b72017-03-31 14:31:55 +0000138 bool findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
139 unsigned &SrcOpIdx2) const override;
140
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000141protected:
142 bool isZeroImm(const MachineOperand &op) const;
143
144 MachineMemOperand *GetMemOperand(MachineBasicBlock &MBB, int FI,
Justin Lebar0af80cd2016-07-15 18:26:59 +0000145 MachineMemOperand::Flags Flags) const;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000146
147private:
Akira Hatanaka067d8152013-05-13 17:43:19 +0000148 virtual unsigned getAnalyzableBrOpc(unsigned Opc) const = 0;
Akira Hatanakab7fa3c92012-07-31 21:49:49 +0000149
150 void AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
151 MachineBasicBlock *&BB,
152 SmallVectorImpl<MachineOperand> &Cond) const;
153
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000154 void BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
155 const DebugLoc &DL, ArrayRef<MachineOperand> Cond) const;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000156};
157
Akira Hatanakafab89292012-08-02 18:21:47 +0000158/// Create MipsInstrInfo objects.
Eric Christopher675cb4d2014-07-18 23:25:00 +0000159const MipsInstrInfo *createMips16InstrInfo(const MipsSubtarget &STI);
160const MipsInstrInfo *createMipsSEInstrInfo(const MipsSubtarget &STI);
Akira Hatanakafab89292012-08-02 18:21:47 +0000161
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000162}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000163
164#endif