blob: d72589604ae19b50c80292db9b5544621d93172b [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- X86InstrInfo.h - X86 Instruction Information ------------*- C++ -*-===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// 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.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd92fb002002-10-25 22:55:53 +00009//
Chris Lattnerb4d58d72003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattnerd92fb002002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TARGET_X86_X86INSTRINFO_H
15#define LLVM_LIB_TARGET_X86_X86INSTRINFO_H
Chris Lattnerd92fb002002-10-25 22:55:53 +000016
Craig Topperc6d4efa2014-03-19 06:53:25 +000017#include "MCTargetDesc/X86BaseInfo.h"
Chris Lattnerd92fb002002-10-25 22:55:53 +000018#include "X86RegisterInfo.h"
Dan Gohman906152a2009-01-05 17:59:02 +000019#include "llvm/ADT/DenseMap.h"
Craig Topperb25fda92012-03-17 18:46:09 +000020#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerd92fb002002-10-25 22:55:53 +000021
Evan Cheng703a0fb2011-07-01 17:57:27 +000022#define GET_INSTRINFO_HEADER
23#include "X86GenInstrInfo.inc"
24
Brian Gaeke960707c2003-11-11 22:41:34 +000025namespace llvm {
Hans Wennborg4ae51192016-03-25 01:10:56 +000026 class MachineInstrBuilder;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000027 class X86RegisterInfo;
Eric Christopher6c786a12014-06-10 22:34:31 +000028 class X86Subtarget;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattnerc0fb5672006-10-20 17:42:20 +000030namespace X86 {
31 // X86 specific condition code. These correspond to X86_*_COND in
32 // X86InstrInfo.td. They must be kept in synch.
Cong Hou94710842016-03-23 21:45:37 +000033enum CondCode {
34 COND_A = 0,
35 COND_AE = 1,
36 COND_B = 2,
37 COND_BE = 3,
38 COND_E = 4,
39 COND_G = 5,
40 COND_GE = 6,
41 COND_L = 7,
42 COND_LE = 8,
43 COND_NE = 9,
44 COND_NO = 10,
45 COND_NP = 11,
46 COND_NS = 12,
47 COND_O = 13,
48 COND_P = 14,
49 COND_S = 15,
50 LAST_VALID_COND = COND_S,
Dan Gohman97d95d62008-10-21 03:29:32 +000051
Cong Hou94710842016-03-23 21:45:37 +000052 // Artificial condition codes. These are used by AnalyzeBranch
53 // to indicate a block terminated with two conditional branches that together
54 // form a compound condition. They occur in code using FCMP_OEQ or FCMP_UNE,
55 // which can't be represented on x86 with a single condition. These
56 // are never used in MachineInstrs and are inverses of one another.
57 COND_NE_OR_P,
58 COND_E_AND_NP,
Dan Gohman97d95d62008-10-21 03:29:32 +000059
Cong Hou94710842016-03-23 21:45:37 +000060 COND_INVALID
61};
Andrew Trick27c079e2011-03-05 06:31:54 +000062
Cong Hou94710842016-03-23 21:45:37 +000063// Turn condition code into conditional branch opcode.
64unsigned GetCondBranchFromCond(CondCode CC);
Andrew Trick27c079e2011-03-05 06:31:54 +000065
Cong Hou94710842016-03-23 21:45:37 +000066/// \brief Return a set opcode for the given condition and whether it has
67/// a memory operand.
68unsigned getSETFromCond(CondCode CC, bool HasMemoryOperand = false);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +000069
Cong Hou94710842016-03-23 21:45:37 +000070/// \brief Return a cmov opcode for the given condition, register size in
71/// bytes, and operand type.
72unsigned getCMovFromCond(CondCode CC, unsigned RegBytes,
73 bool HasMemoryOperand = false);
Juergen Ributzka6ef06f92014-06-23 21:55:36 +000074
Cong Hou94710842016-03-23 21:45:37 +000075// Turn CMov opcode into condition code.
76CondCode getCondFromCMovOpc(unsigned Opc);
Michael Liao32376622012-09-20 03:06:15 +000077
Cong Hou94710842016-03-23 21:45:37 +000078/// GetOppositeBranchCondition - Return the inverse of the specified cond,
79/// e.g. turning COND_E to COND_NE.
80CondCode GetOppositeBranchCondition(CondCode CC);
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081} // end namespace X86;
Chris Lattner3a897f32006-10-21 05:52:40 +000082
Chris Lattner377f1d52009-07-10 06:06:17 +000083
Chris Lattnerca9d7842009-07-10 06:29:59 +000084/// isGlobalStubReference - Return true if the specified TargetFlag operand is
Chris Lattner377f1d52009-07-10 06:06:17 +000085/// a reference to a stub for a global, not the global itself.
Chris Lattnerca9d7842009-07-10 06:29:59 +000086inline static bool isGlobalStubReference(unsigned char TargetFlag) {
87 switch (TargetFlag) {
Chris Lattner377f1d52009-07-10 06:06:17 +000088 case X86II::MO_DLLIMPORT: // dllimport stub.
89 case X86II::MO_GOTPCREL: // rip-relative GOT reference.
90 case X86II::MO_GOT: // normal GOT reference.
91 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: // Normal $non_lazy_ptr ref.
92 case X86II::MO_DARWIN_NONLAZY: // Normal $non_lazy_ptr ref.
93 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Hidden $non_lazy_ptr ref.
Chris Lattner377f1d52009-07-10 06:06:17 +000094 return true;
95 default:
96 return false;
97 }
98}
Chris Lattnerd3f32c72009-07-10 07:33:30 +000099
100/// isGlobalRelativeToPICBase - Return true if the specified global value
101/// reference is relative to a 32-bit PIC base (X86ISD::GlobalBaseReg). If this
102/// is true, the addressing mode has the PIC base register added in (e.g. EBX).
103inline static bool isGlobalRelativeToPICBase(unsigned char TargetFlag) {
104 switch (TargetFlag) {
105 case X86II::MO_GOTOFF: // isPICStyleGOT: local global.
106 case X86II::MO_GOT: // isPICStyleGOT: other global.
107 case X86II::MO_PIC_BASE_OFFSET: // Darwin local global.
108 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: // Darwin/32 external global.
109 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Darwin/32 hidden global.
Eric Christopherb0e1a452010-06-03 04:07:48 +0000110 case X86II::MO_TLVP: // ??? Pretty sure..
Chris Lattnerd3f32c72009-07-10 07:33:30 +0000111 return true;
112 default:
113 return false;
114 }
115}
Andrew Trick27c079e2011-03-05 06:31:54 +0000116
Anton Korobeynikov4e9dfe82008-06-28 11:07:54 +0000117inline static bool isScale(const MachineOperand &MO) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000118 return MO.isImm() &&
Anton Korobeynikov4e9dfe82008-06-28 11:07:54 +0000119 (MO.getImm() == 1 || MO.getImm() == 2 ||
120 MO.getImm() == 4 || MO.getImm() == 8);
121}
122
Rafael Espindola3b2df102009-04-08 21:14:34 +0000123inline static bool isLeaMem(const MachineInstr *MI, unsigned Op) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000124 if (MI->getOperand(Op).isFI()) return true;
Manuel Jacobdcb78db2014-03-18 16:14:11 +0000125 return Op+X86::AddrSegmentReg <= MI->getNumOperands() &&
126 MI->getOperand(Op+X86::AddrBaseReg).isReg() &&
127 isScale(MI->getOperand(Op+X86::AddrScaleAmt)) &&
128 MI->getOperand(Op+X86::AddrIndexReg).isReg() &&
129 (MI->getOperand(Op+X86::AddrDisp).isImm() ||
130 MI->getOperand(Op+X86::AddrDisp).isGlobal() ||
131 MI->getOperand(Op+X86::AddrDisp).isCPI() ||
132 MI->getOperand(Op+X86::AddrDisp).isJTI());
Anton Korobeynikov4e9dfe82008-06-28 11:07:54 +0000133}
134
Rafael Espindola3b2df102009-04-08 21:14:34 +0000135inline static bool isMem(const MachineInstr *MI, unsigned Op) {
136 if (MI->getOperand(Op).isFI()) return true;
Manuel Jacobdcb78db2014-03-18 16:14:11 +0000137 return Op+X86::AddrNumOperands <= MI->getNumOperands() &&
138 MI->getOperand(Op+X86::AddrSegmentReg).isReg() &&
Rafael Espindola3b2df102009-04-08 21:14:34 +0000139 isLeaMem(MI, Op);
140}
141
Craig Topperec828472014-03-31 06:53:13 +0000142class X86InstrInfo final : public X86GenInstrInfo {
Eric Christopher6c786a12014-06-10 22:34:31 +0000143 X86Subtarget &Subtarget;
Chris Lattnerd92fb002002-10-25 22:55:53 +0000144 const X86RegisterInfo RI;
Andrew Trick27c079e2011-03-05 06:31:54 +0000145
Craig Topper9eadcfd2012-06-01 05:34:01 +0000146 /// RegOp2MemOpTable3Addr, RegOp2MemOpTable0, RegOp2MemOpTable1,
147 /// RegOp2MemOpTable2, RegOp2MemOpTable3 - Load / store folding opcode maps.
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000148 ///
Bruno Cardoso Lopes23eb5262011-09-08 18:35:57 +0000149 typedef DenseMap<unsigned,
Craig Toppere012ede2016-04-30 17:59:49 +0000150 std::pair<uint16_t, uint16_t> > RegOp2MemOpTableType;
Bruno Cardoso Lopes23eb5262011-09-08 18:35:57 +0000151 RegOp2MemOpTableType RegOp2MemOpTable2Addr;
152 RegOp2MemOpTableType RegOp2MemOpTable0;
153 RegOp2MemOpTableType RegOp2MemOpTable1;
154 RegOp2MemOpTableType RegOp2MemOpTable2;
Elena Demikhovsky602f3a22012-05-31 09:20:20 +0000155 RegOp2MemOpTableType RegOp2MemOpTable3;
Robert Khasanov79fb7292014-12-18 12:28:22 +0000156 RegOp2MemOpTableType RegOp2MemOpTable4;
Andrew Trick27c079e2011-03-05 06:31:54 +0000157
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000158 /// MemOp2RegOpTable - Load / store unfolding opcode map.
159 ///
Bruno Cardoso Lopes23eb5262011-09-08 18:35:57 +0000160 typedef DenseMap<unsigned,
Craig Toppere012ede2016-04-30 17:59:49 +0000161 std::pair<uint16_t, uint16_t> > MemOp2RegOpTableType;
Bruno Cardoso Lopes23eb5262011-09-08 18:35:57 +0000162 MemOp2RegOpTableType MemOp2RegOpTable;
163
Craig Topperd9c7d0d2012-06-23 04:58:41 +0000164 static void AddTableEntry(RegOp2MemOpTableType &R2MTable,
165 MemOp2RegOpTableType &M2RTable,
Craig Toppere012ede2016-04-30 17:59:49 +0000166 uint16_t RegOp, uint16_t MemOp, uint16_t Flags);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000167
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000168 virtual void anchor();
169
Sanjoy Das6b34a462015-06-15 18:44:21 +0000170 bool AnalyzeBranchImpl(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
171 MachineBasicBlock *&FBB,
172 SmallVectorImpl<MachineOperand> &Cond,
173 SmallVectorImpl<MachineInstr *> &CondBranches,
174 bool AllowModify) const;
175
Chris Lattnerd92fb002002-10-25 22:55:53 +0000176public:
Eric Christopher6c786a12014-06-10 22:34:31 +0000177 explicit X86InstrInfo(X86Subtarget &STI);
Chris Lattnerd92fb002002-10-25 22:55:53 +0000178
Chris Lattnerb4d58d72003-01-14 22:00:31 +0000179 /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
Chris Lattnerd92fb002002-10-25 22:55:53 +0000180 /// such, whenever a client has an instance of instruction info, it should
181 /// always be able to get register info as well (through this method).
182 ///
Craig Topperf5e3b0b2014-03-09 07:58:15 +0000183 const X86RegisterInfo &getRegisterInfo() const { return RI; }
Chris Lattnerd92fb002002-10-25 22:55:53 +0000184
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000185 /// getSPAdjust - This returns the stack pointer adjustment made by
186 /// this instruction. For x86, we need to handle more complex call
187 /// sequences involving PUSHes.
188 int getSPAdjust(const MachineInstr *MI) const override;
189
Evan Cheng30bebff2010-01-13 00:30:23 +0000190 /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
191 /// extension instruction. That is, it's like a copy where it's legal for the
192 /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
193 /// true, then it's expected the pre-extension value is available as a subreg
194 /// of the result register. This also returns the sub-register index in
195 /// SubIdx.
Craig Topper2d9361e2014-03-09 07:44:38 +0000196 bool isCoalescableExtInstr(const MachineInstr &MI,
197 unsigned &SrcReg, unsigned &DstReg,
198 unsigned &SubIdx) const override;
Evan Cheng42166152010-01-12 00:09:37 +0000199
Craig Topper2d9361e2014-03-09 07:44:38 +0000200 unsigned isLoadFromStackSlot(const MachineInstr *MI,
201 int &FrameIndex) const override;
David Greene2f4c3742009-11-13 00:29:53 +0000202 /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
203 /// stack locations as well. This uses a heuristic so it isn't
204 /// reliable for correctness.
205 unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
Craig Topper2d9361e2014-03-09 07:44:38 +0000206 int &FrameIndex) const override;
David Greene70fdd572009-11-12 20:55:29 +0000207
Craig Topper2d9361e2014-03-09 07:44:38 +0000208 unsigned isStoreToStackSlot(const MachineInstr *MI,
209 int &FrameIndex) const override;
David Greene2f4c3742009-11-13 00:29:53 +0000210 /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
211 /// stack locations as well. This uses a heuristic so it isn't
212 /// reliable for correctness.
213 unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
Craig Topper2d9361e2014-03-09 07:44:38 +0000214 int &FrameIndex) const override;
Evan Chenged6e34f2008-03-31 20:40:39 +0000215
Dan Gohmane919de52009-10-10 00:34:18 +0000216 bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
Craig Topper2d9361e2014-03-09 07:44:38 +0000217 AliasAnalysis *AA) const override;
Evan Chenged6e34f2008-03-31 20:40:39 +0000218 void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Evan Cheng84517442009-07-16 09:20:10 +0000219 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +0000220 const MachineInstr *Orig,
Craig Topper2d9361e2014-03-09 07:44:38 +0000221 const TargetRegisterInfo &TRI) const override;
Evan Chenged6e34f2008-03-31 20:40:39 +0000222
Tim Northover6833e3f2013-06-10 20:43:49 +0000223 /// Given an operand within a MachineInstr, insert preceding code to put it
224 /// into the right format for a particular kind of LEA instruction. This may
225 /// involve using an appropriate super-register instead (with an implicit use
226 /// of the original) or creating a new virtual register and inserting COPY
227 /// instructions to get the data into the right class.
228 ///
229 /// Reference parameters are set to indicate how caller should add this
230 /// operand to the LEA instruction.
231 bool classifyLEAReg(MachineInstr *MI, const MachineOperand &Src,
232 unsigned LEAOpcode, bool AllowSP,
233 unsigned &NewSrc, bool &isKill,
234 bool &isUndef, MachineOperand &ImplicitOp) const;
235
Chris Lattnerb7782d72005-01-02 02:37:07 +0000236 /// convertToThreeAddress - This method must be implemented by targets that
237 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
238 /// may be able to convert a two-address instruction into a true
239 /// three-address instruction on demand. This allows the X86 target (for
240 /// example) to convert ADD and SHL instructions into LEA instructions if they
241 /// would require register copies due to two-addressness.
242 ///
243 /// This method returns a null pointer if the transformation cannot be
244 /// performed, otherwise it returns the new instruction.
245 ///
Craig Topper2d9361e2014-03-09 07:44:38 +0000246 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
247 MachineBasicBlock::iterator &MBBI,
248 LiveVariables *LV) const override;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000249
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000250 /// Returns true iff the routine could find two commutable operands in the
251 /// given machine instruction.
252 /// The 'SrcOpIdx1' and 'SrcOpIdx2' are INPUT and OUTPUT arguments. Their
253 /// input values can be re-defined in this method only if the input values
254 /// are not pre-defined, which is designated by the special value
255 /// 'CommuteAnyOperandIndex' assigned to it.
256 /// If both of indices are pre-defined and refer to some operands, then the
257 /// method simply returns true if the corresponding operands are commutable
258 /// and returns false otherwise.
Chris Lattner29478012005-01-19 07:11:01 +0000259 ///
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000260 /// For example, calling this method this way:
261 /// unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
262 /// findCommutedOpIndices(MI, Op1, Op2);
263 /// can be interpreted as a query asking to find an operand that would be
264 /// commutable with the operand#1.
Lang Hamesc59a2d02014-04-02 23:57:49 +0000265 bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
266 unsigned &SrcOpIdx2) const override;
267
Andrew Kaylor4731bea2015-11-06 19:47:25 +0000268 /// Returns true if the routine could find two commutable operands
269 /// in the given FMA instruction. Otherwise, returns false.
270 ///
271 /// \p SrcOpIdx1 and \p SrcOpIdx2 are INPUT and OUTPUT arguments.
272 /// The output indices of the commuted operands are returned in these
273 /// arguments. Also, the input values of these arguments may be preset either
274 /// to indices of operands that must be commuted or be equal to a special
275 /// value 'CommuteAnyOperandIndex' which means that the corresponding
276 /// operand index is not set and this method is free to pick any of
277 /// available commutable operands.
278 ///
279 /// For example, calling this method this way:
280 /// unsigned Idx1 = 1, Idx2 = CommuteAnyOperandIndex;
281 /// findFMA3CommutedOpIndices(MI, Idx1, Idx2);
282 /// can be interpreted as a query asking if the operand #1 can be swapped
283 /// with any other available operand (e.g. operand #2, operand #3, etc.).
284 ///
285 /// The returned FMA opcode may differ from the opcode in the given MI.
286 /// For example, commuting the operands #1 and #3 in the following FMA
287 /// FMA213 #1, #2, #3
288 /// results into instruction with adjusted opcode:
289 /// FMA231 #3, #2, #1
290 bool findFMA3CommutedOpIndices(MachineInstr *MI,
291 unsigned &SrcOpIdx1,
292 unsigned &SrcOpIdx2) const;
293
294 /// Returns an adjusted FMA opcode that must be used in FMA instruction that
295 /// performs the same computations as the given MI but which has the operands
296 /// \p SrcOpIdx1 and \p SrcOpIdx2 commuted.
297 /// It may return 0 if it is unsafe to commute the operands.
298 ///
299 /// The returned FMA opcode may differ from the opcode in the given \p MI.
300 /// For example, commuting the operands #1 and #3 in the following FMA
301 /// FMA213 #1, #2, #3
302 /// results into instruction with adjusted opcode:
303 /// FMA231 #3, #2, #1
304 unsigned getFMA3OpcodeToCommuteOperands(MachineInstr *MI,
305 unsigned SrcOpIdx1,
306 unsigned SrcOpIdx2) const;
307
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000308 // Branch analysis.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000309 bool isUnpredicatedTerminator(const MachineInstr &MI) const override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000310 bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
311 MachineBasicBlock *&FBB,
312 SmallVectorImpl<MachineOperand> &Cond,
313 bool AllowModify) const override;
Sanjoy Dasb666ea32015-06-15 18:44:14 +0000314
315 bool getMemOpBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
Chad Rosierc27a18f2016-03-09 16:00:35 +0000316 int64_t &Offset,
Sanjoy Dasb666ea32015-06-15 18:44:14 +0000317 const TargetRegisterInfo *TRI) const override;
Sanjoy Das6b34a462015-06-15 18:44:21 +0000318 bool AnalyzeBranchPredicate(MachineBasicBlock &MBB,
319 TargetInstrInfo::MachineBranchPredicate &MBP,
320 bool AllowModify = false) const override;
321
Craig Topper2d9361e2014-03-09 07:44:38 +0000322 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
323 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000324 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
Craig Topper2d9361e2014-03-09 07:44:38 +0000325 DebugLoc DL) const override;
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000326 bool canInsertSelect(const MachineBasicBlock&, ArrayRef<MachineOperand> Cond,
Craig Topper2d9361e2014-03-09 07:44:38 +0000327 unsigned, unsigned, int&, int&, int&) const override;
328 void insertSelect(MachineBasicBlock &MBB,
329 MachineBasicBlock::iterator MI, DebugLoc DL,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000330 unsigned DstReg, ArrayRef<MachineOperand> Cond,
Craig Topper2d9361e2014-03-09 07:44:38 +0000331 unsigned TrueReg, unsigned FalseReg) const override;
332 void copyPhysReg(MachineBasicBlock &MBB,
333 MachineBasicBlock::iterator MI, DebugLoc DL,
334 unsigned DestReg, unsigned SrcReg,
335 bool KillSrc) const override;
336 void storeRegToStackSlot(MachineBasicBlock &MBB,
337 MachineBasicBlock::iterator MI,
338 unsigned SrcReg, bool isKill, int FrameIndex,
339 const TargetRegisterClass *RC,
340 const TargetRegisterInfo *TRI) const override;
Owen Andersoneee14602008-01-01 21:11:32 +0000341
Craig Topperf5e3b0b2014-03-09 07:58:15 +0000342 void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
343 SmallVectorImpl<MachineOperand> &Addr,
344 const TargetRegisterClass *RC,
345 MachineInstr::mmo_iterator MMOBegin,
346 MachineInstr::mmo_iterator MMOEnd,
347 SmallVectorImpl<MachineInstr*> &NewMIs) const;
Owen Andersoneee14602008-01-01 21:11:32 +0000348
Craig Topper2d9361e2014-03-09 07:44:38 +0000349 void loadRegFromStackSlot(MachineBasicBlock &MBB,
350 MachineBasicBlock::iterator MI,
351 unsigned DestReg, int FrameIndex,
352 const TargetRegisterClass *RC,
353 const TargetRegisterInfo *TRI) const override;
Owen Andersoneee14602008-01-01 21:11:32 +0000354
Craig Topperf5e3b0b2014-03-09 07:58:15 +0000355 void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
356 SmallVectorImpl<MachineOperand> &Addr,
357 const TargetRegisterClass *RC,
358 MachineInstr::mmo_iterator MMOBegin,
359 MachineInstr::mmo_iterator MMOEnd,
360 SmallVectorImpl<MachineInstr*> &NewMIs) const;
Jakob Stoklund Olesendd1904e2011-09-29 05:10:54 +0000361
Craig Topper2d9361e2014-03-09 07:44:38 +0000362 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
Jakob Stoklund Olesendd1904e2011-09-29 05:10:54 +0000363
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000364 /// foldMemoryOperand - If this target supports it, fold a load or store of
365 /// the specified stack slot into the specified machine instruction for the
366 /// specified operand(s). If this is possible, the target should perform the
367 /// folding and return true, otherwise it should return false. If it folds
368 /// the instruction, it is likely that the MachineInstruction the iterator
369 /// references has been changed.
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000370 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
371 ArrayRef<unsigned> Ops,
Keno Fischere70b31f2015-06-08 20:09:58 +0000372 MachineBasicBlock::iterator InsertPt,
Craig Topper2d9361e2014-03-09 07:44:38 +0000373 int FrameIndex) const override;
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000374
375 /// foldMemoryOperand - Same as the previous version except it allows folding
376 /// of any load and store from / to any address, not just from a specific
377 /// stack slot.
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000378 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
379 ArrayRef<unsigned> Ops,
Keno Fischere70b31f2015-06-08 20:09:58 +0000380 MachineBasicBlock::iterator InsertPt,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000381 MachineInstr *LoadMI) const override;
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000382
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000383 /// unfoldMemoryOperand - Separate a single instruction which folded a load or
384 /// a store or a load and a store into two or more instruction. If this is
385 /// possible, returns true as well as the new instructions by reference.
Craig Topper2d9361e2014-03-09 07:44:38 +0000386 bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
387 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
388 SmallVectorImpl<MachineInstr*> &NewMIs) const override;
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000389
Craig Topper2d9361e2014-03-09 07:44:38 +0000390 bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
391 SmallVectorImpl<SDNode*> &NewNodes) const override;
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000392
393 /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
394 /// instruction after load / store are unfolded from an instruction of the
395 /// specified opcode. It returns zero if the specified unfolding is not
Dan Gohman49fa51d2009-10-30 22:18:41 +0000396 /// possible. If LoadRegIndex is non-null, it is filled in with the operand
397 /// index of the operand which will hold the register holding the loaded
398 /// value.
Craig Topper2d9361e2014-03-09 07:44:38 +0000399 unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
400 bool UnfoldLoad, bool UnfoldStore,
Craig Toppere73658d2014-04-28 04:05:08 +0000401 unsigned *LoadRegIndex = nullptr) const override;
Andrew Trick27c079e2011-03-05 06:31:54 +0000402
Evan Cheng4f026f32010-01-22 03:34:51 +0000403 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
404 /// to determine if two loads are loading from the same base address. It
405 /// should only return true if the base pointers are the same and the
406 /// only differences between the two addresses are the offset. It also returns
407 /// the offsets by reference.
Craig Topper2d9361e2014-03-09 07:44:38 +0000408 bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
409 int64_t &Offset2) const override;
Evan Cheng4f026f32010-01-22 03:34:51 +0000410
411 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000412 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Evan Cheng4f026f32010-01-22 03:34:51 +0000413 /// be scheduled togther. On some targets if two loads are loading from
414 /// addresses in the same cache line, it's better if they are scheduled
415 /// together. This function takes two integers that represent the load offsets
416 /// from the common base address. It returns true if it decides it's desirable
417 /// to schedule the two loads together. "NumLoads" is the number of loads that
418 /// have already been scheduled after Load1.
Craig Topper2d9361e2014-03-09 07:44:38 +0000419 bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
420 int64_t Offset1, int64_t Offset2,
421 unsigned NumLoads) const override;
Evan Cheng4f026f32010-01-22 03:34:51 +0000422
Craig Topper2d9361e2014-03-09 07:44:38 +0000423 bool shouldScheduleAdjacent(MachineInstr* First,
424 MachineInstr *Second) const override;
Andrew Trick47740de2013-06-23 09:00:28 +0000425
Craig Topper2d9361e2014-03-09 07:44:38 +0000426 void getNoopForMachoTarget(MCInst &NopInst) const override;
Chris Lattner6a5e7062010-04-26 23:37:21 +0000427
Craig Topper2d9361e2014-03-09 07:44:38 +0000428 bool
429 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
Chris Lattner29478012005-01-19 07:11:01 +0000430
Evan Chengb5f0ec32009-02-06 17:17:30 +0000431 /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
432 /// instruction that defines the specified register class.
Craig Topper2d9361e2014-03-09 07:44:38 +0000433 bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
Evan Chengf7137222008-10-27 07:14:50 +0000434
Alexey Volkov6226de62014-05-20 08:55:50 +0000435 /// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction tha
436 /// would clobber the EFLAGS condition register. Note the result may be
437 /// conservative. If it cannot definitely determine the safety after visiting
438 /// a few instructions in each direction it assumes it's not safe.
439 bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
440 MachineBasicBlock::iterator I) const;
441
Andrew Kayloraf083d42015-08-26 20:36:52 +0000442 /// True if MI has a condition code def, e.g. EFLAGS, that is
443 /// not marked dead.
444 bool hasLiveCondCodeDef(MachineInstr *MI) const;
445
Dan Gohman6ebe7342008-09-30 00:58:23 +0000446 /// getGlobalBaseReg - Return a virtual register initialized with the
447 /// the global base register value. Output instructions required to
448 /// initialize the register in the function entry block, if necessary.
Dan Gohman24300732008-09-23 18:22:58 +0000449 ///
Dan Gohman6ebe7342008-09-30 00:58:23 +0000450 unsigned getGlobalBaseReg(MachineFunction *MF) const;
Dan Gohman24300732008-09-23 18:22:58 +0000451
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000452 std::pair<uint16_t, uint16_t>
Craig Topper2d9361e2014-03-09 07:44:38 +0000453 getExecutionDomain(const MachineInstr *MI) const override;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000454
Craig Topper2d9361e2014-03-09 07:44:38 +0000455 void setExecutionDomain(MachineInstr *MI, unsigned Domain) const override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000456
Craig Topper2d9361e2014-03-09 07:44:38 +0000457 unsigned
458 getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
459 const TargetRegisterInfo *TRI) const override;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000460 unsigned getUndefRegClearance(const MachineInstr *MI, unsigned &OpNum,
Craig Topper2d9361e2014-03-09 07:44:38 +0000461 const TargetRegisterInfo *TRI) const override;
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000462 void breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
Craig Topper2d9361e2014-03-09 07:44:38 +0000463 const TargetRegisterInfo *TRI) const override;
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000464
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000465 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
Chris Lattnereeba0c72010-09-05 02:18:34 +0000466 unsigned OpNum,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000467 ArrayRef<MachineOperand> MOs,
Keno Fischere70b31f2015-06-08 20:09:58 +0000468 MachineBasicBlock::iterator InsertPt,
Simon Pilgrim2f9548a2014-10-20 22:14:22 +0000469 unsigned Size, unsigned Alignment,
470 bool AllowCommute) const;
Evan Cheng63c76082010-10-19 18:58:51 +0000471
Tom Roeder44cb65f2014-06-05 19:29:43 +0000472 void
473 getUnconditionalBranch(MCInst &Branch,
474 const MCSymbolRefExpr *BranchTarget) const override;
475
476 void getTrap(MCInst &MI) const override;
477
Tom Roedereb7a3032014-11-11 21:08:02 +0000478 unsigned getJumpInstrTableEntryBound() const override;
479
Craig Topper2d9361e2014-03-09 07:44:38 +0000480 bool isHighLatencyDef(int opc) const override;
Andrew Trick641e2d42011-03-05 08:00:22 +0000481
Matthias Braun88e21312015-06-13 03:42:11 +0000482 bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
Evan Cheng63c76082010-10-19 18:58:51 +0000483 const MachineRegisterInfo *MRI,
484 const MachineInstr *DefMI, unsigned DefIdx,
Craig Topper2d9361e2014-03-09 07:44:38 +0000485 const MachineInstr *UseMI,
486 unsigned UseIdx) const override;
Sanjay Patel08829ba2015-06-10 20:32:21 +0000487
488 bool useMachineCombiner() const override {
489 return true;
490 }
Chad Rosier03a47302015-09-21 15:09:11 +0000491
492 bool isAssociativeAndCommutative(const MachineInstr &Inst) const override;
493
494 bool hasReassociableOperands(const MachineInstr &Inst,
495 const MachineBasicBlock *MBB) const override;
496
497 void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
498 MachineInstr &NewMI1,
499 MachineInstr &NewMI2) const override;
Sanjay Patel08829ba2015-06-10 20:32:21 +0000500
Manman Renc9656732012-07-06 17:36:20 +0000501 /// analyzeCompare - For a comparison instruction, return the source registers
502 /// in SrcReg and SrcReg2 if having two register operands, and the value it
503 /// compares against in CmpValue. Return true if the comparison instruction
504 /// can be analyzed.
Craig Topper2d9361e2014-03-09 07:44:38 +0000505 bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
506 unsigned &SrcReg2, int &CmpMask,
507 int &CmpValue) const override;
Manman Renc9656732012-07-06 17:36:20 +0000508
509 /// optimizeCompareInstr - Check if there exists an earlier instruction that
510 /// operates on the same source operands and sets flags in the same way as
511 /// Compare; remove Compare if possible.
Craig Topper2d9361e2014-03-09 07:44:38 +0000512 bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
513 unsigned SrcReg2, int CmpMask, int CmpValue,
514 const MachineRegisterInfo *MRI) const override;
Manman Renc9656732012-07-06 17:36:20 +0000515
Manman Ren5759d012012-08-02 00:56:42 +0000516 /// optimizeLoadInstr - Try to remove the load by folding it to a register
517 /// operand at the use. We fold the load instructions if and only if the
Manman Renba8122c2012-08-02 19:37:32 +0000518 /// def and use are in the same BB. We only look at one load and see
519 /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
520 /// defined by the load we are trying to fold. DefMI returns the machine
521 /// instruction that defines FoldAsLoadDefReg, and the function returns
522 /// the machine instruction generated due to folding.
Craig Topper2d9361e2014-03-09 07:44:38 +0000523 MachineInstr* optimizeLoadInstr(MachineInstr *MI,
524 const MachineRegisterInfo *MRI,
525 unsigned &FoldAsLoadDefReg,
526 MachineInstr *&DefMI) const override;
Manman Ren5759d012012-08-02 00:56:42 +0000527
Alex Lorenz49873a82015-08-06 00:44:07 +0000528 std::pair<unsigned, unsigned>
529 decomposeMachineOperandsTargetFlags(unsigned TF) const override;
530
531 ArrayRef<std::pair<unsigned, const char *>>
532 getSerializableDirectMachineOperandTargetFlags() const override;
533
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000534protected:
535 /// Commutes the operands in the given instruction by changing the operands
536 /// order and/or changing the instruction's opcode and/or the immediate value
537 /// operand.
538 ///
539 /// The arguments 'CommuteOpIdx1' and 'CommuteOpIdx2' specify the operands
540 /// to be commuted.
541 ///
542 /// Do not call this method for a non-commutable instruction or
543 /// non-commutable operands.
544 /// Even though the instruction is commutable, the method may still
545 /// fail to commute the operands, null pointer is returned in such cases.
546 MachineInstr *commuteInstructionImpl(MachineInstr *MI, bool NewMI,
547 unsigned CommuteOpIdx1,
548 unsigned CommuteOpIdx2) const override;
549
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000550private:
Evan Cheng766a73f2009-12-11 06:01:48 +0000551 MachineInstr * convertToThreeAddressWithLEA(unsigned MIOpc,
552 MachineFunction::iterator &MFI,
553 MachineBasicBlock::iterator &MBBI,
554 LiveVariables *LV) const;
555
Simon Pilgrim7e6606f2015-11-04 20:48:09 +0000556 /// Handles memory folding for special case instructions, for instance those
557 /// requiring custom manipulation of the address.
558 MachineInstr *foldMemoryOperandCustom(MachineFunction &MF, MachineInstr *MI,
559 unsigned OpNum,
560 ArrayRef<MachineOperand> MOs,
561 MachineBasicBlock::iterator InsertPt,
562 unsigned Size, unsigned Align) const;
563
David Greene70fdd572009-11-12 20:55:29 +0000564 /// isFrameOperand - Return true and the FrameIndex if the specified
565 /// operand and follow operands form a reference to the stack frame.
566 bool isFrameOperand(const MachineInstr *MI, unsigned int Op,
567 int &FrameIndex) const;
Hans Wennborg4ae51192016-03-25 01:10:56 +0000568
569 /// Expand the MOVImmSExti8 pseudo-instructions.
570 bool ExpandMOVImmSExti8(MachineInstrBuilder &MIB) const;
Chris Lattnerd92fb002002-10-25 22:55:53 +0000571};
572
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000573} // End llvm namespace
Brian Gaeke960707c2003-11-11 22:41:34 +0000574
Chris Lattnerd92fb002002-10-25 22:55:53 +0000575#endif