blob: 794b193a501e62772dc27bd043fd95cd8f997db2 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZInstrInfo.h - SystemZ instruction information ----*- C++ -*-===//
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 SystemZ implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZINSTRINFO_H
15#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZINSTRINFO_H
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016
17#include "SystemZ.h"
18#include "SystemZRegisterInfo.h"
19#include "llvm/Target/TargetInstrInfo.h"
20
21#define GET_INSTRINFO_HEADER
22#include "SystemZGenInstrInfo.inc"
23
24namespace llvm {
25
26class SystemZTargetMachine;
27
28namespace SystemZII {
Richard Sandifordc2312692014-03-06 10:38:30 +000029enum {
30 // See comments in SystemZInstrFormats.td.
31 SimpleBDXLoad = (1 << 0),
32 SimpleBDXStore = (1 << 1),
33 Has20BitOffset = (1 << 2),
34 HasIndex = (1 << 3),
35 Is128Bit = (1 << 4),
36 AccessSizeMask = (31 << 5),
37 AccessSizeShift = 5,
38 CCValuesMask = (15 << 10),
39 CCValuesShift = 10,
40 CompareZeroCCMaskMask = (15 << 14),
41 CompareZeroCCMaskShift = 14,
42 CCMaskFirst = (1 << 18),
43 CCMaskLast = (1 << 19),
44 IsLogical = (1 << 20)
45};
46static inline unsigned getAccessSize(unsigned int Flags) {
47 return (Flags & AccessSizeMask) >> AccessSizeShift;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000048}
Richard Sandifordc2312692014-03-06 10:38:30 +000049static inline unsigned getCCValues(unsigned int Flags) {
50 return (Flags & CCValuesMask) >> CCValuesShift;
51}
52static inline unsigned getCompareZeroCCMask(unsigned int Flags) {
53 return (Flags & CompareZeroCCMaskMask) >> CompareZeroCCMaskShift;
54}
55
56// SystemZ MachineOperand target flags.
57enum {
58 // Masks out the bits for the access model.
Ulrich Weigand7db69182015-02-18 09:13:27 +000059 MO_SYMBOL_MODIFIER = (3 << 0),
Richard Sandifordc2312692014-03-06 10:38:30 +000060
61 // @GOT (aka @GOTENT)
Ulrich Weigand7db69182015-02-18 09:13:27 +000062 MO_GOT = (1 << 0),
63
64 // @INDNTPOFF
65 MO_INDNTPOFF = (2 << 0)
Richard Sandifordc2312692014-03-06 10:38:30 +000066};
67// Classifies a branch.
68enum BranchType {
69 // An instruction that branches on the current value of CC.
70 BranchNormal,
71
72 // An instruction that peforms a 32-bit signed comparison and branches
73 // on the result.
74 BranchC,
75
76 // An instruction that peforms a 32-bit unsigned comparison and branches
77 // on the result.
78 BranchCL,
79
80 // An instruction that peforms a 64-bit signed comparison and branches
81 // on the result.
82 BranchCG,
83
84 // An instruction that peforms a 64-bit unsigned comparison and branches
85 // on the result.
86 BranchCLG,
87
88 // An instruction that decrements a 32-bit register and branches if
89 // the result is nonzero.
90 BranchCT,
91
92 // An instruction that decrements a 64-bit register and branches if
93 // the result is nonzero.
94 BranchCTG
95};
96// Information about a branch instruction.
97struct Branch {
98 // The type of the branch.
99 BranchType Type;
100
101 // CCMASK_<N> is set if CC might be equal to N.
102 unsigned CCValid;
103
104 // CCMASK_<N> is set if the branch should be taken when CC == N.
105 unsigned CCMask;
106
107 // The target of the branch.
108 const MachineOperand *Target;
109
110 Branch(BranchType type, unsigned ccValid, unsigned ccMask,
111 const MachineOperand *target)
112 : Type(type), CCValid(ccValid), CCMask(ccMask), Target(target) {}
113};
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000114// Kinds of fused compares in compare-and-* instructions. Together with type
115// of the converted compare, this identifies the compare-and-*
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000116// instruction.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000117enum FusedCompareType {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000118 // Relative branch - CRJ etc.
119 CompareAndBranch,
120
121 // Indirect branch, used for return - CRBReturn etc.
Ulrich Weigand848a5132016-04-11 12:12:32 +0000122 CompareAndReturn,
123
124 // Indirect branch, used for sibcall - CRBCall etc.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000125 CompareAndSibcall,
126
127 // Trap
128 CompareAndTrap
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000129};
Richard Sandifordc2312692014-03-06 10:38:30 +0000130} // end namespace SystemZII
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000131
Eric Christopher673b3af2014-06-27 07:01:17 +0000132class SystemZSubtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133class SystemZInstrInfo : public SystemZGenInstrInfo {
134 const SystemZRegisterInfo RI;
Eric Christopher673b3af2014-06-27 07:01:17 +0000135 SystemZSubtarget &STI;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136
137 void splitMove(MachineBasicBlock::iterator MI, unsigned NewOpcode) const;
138 void splitAdjDynAlloc(MachineBasicBlock::iterator MI) const;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000139 void expandRIPseudo(MachineInstr &MI, unsigned LowOpcode, unsigned HighOpcode,
140 bool ConvertHigh) const;
141 void expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000142 unsigned LowOpcodeK, unsigned HighOpcode) const;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000143 void expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000144 unsigned HighOpcode) const;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000145 void expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
146 unsigned HighOpcode) const;
147 void expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode,
148 unsigned HighOpcode) const;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000149 void expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000150 unsigned Size) const;
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000151 void expandLoadStackGuard(MachineInstr *MI) const;
Richard Sandiford0755c932013-10-01 11:26:28 +0000152 void emitGRX32Move(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000153 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
Richard Sandiford0755c932013-10-01 11:26:28 +0000154 unsigned LowLowOpcode, unsigned Size, bool KillSrc) const;
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000155 virtual void anchor();
Ulrich Weigand524f2762016-11-28 13:34:08 +0000156
157protected:
158 /// Commutes the operands in the given instruction by changing the operands
159 /// order and/or changing the instruction's opcode and/or the immediate value
160 /// operand.
161 ///
162 /// The arguments 'CommuteOpIdx1' and 'CommuteOpIdx2' specify the operands
163 /// to be commuted.
164 ///
165 /// Do not call this method for a non-commutable instruction or
166 /// non-commutable operands.
167 /// Even though the instruction is commutable, the method may still
168 /// fail to commute the operands, null pointer is returned in such cases.
169 MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
170 unsigned CommuteOpIdx1,
171 unsigned CommuteOpIdx2) const override;
172
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000173public:
Eric Christopher673b3af2014-06-27 07:01:17 +0000174 explicit SystemZInstrInfo(SystemZSubtarget &STI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175
176 // Override TargetInstrInfo.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000177 unsigned isLoadFromStackSlot(const MachineInstr &MI,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000178 int &FrameIndex) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000179 unsigned isStoreToStackSlot(const MachineInstr &MI,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000180 int &FrameIndex) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000181 bool isStackSlotCopy(const MachineInstr &MI, int &DestFrameIndex,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000182 int &SrcFrameIndex) const override;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000183 bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000184 MachineBasicBlock *&FBB,
185 SmallVectorImpl<MachineOperand> &Cond,
186 bool AllowModify) const override;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000187 unsigned removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000188 int *BytesRemoved = nullptr) const override;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000189 unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000190 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000191 const DebugLoc &DL,
192 int *BytesAdded = nullptr) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000193 bool analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
Craig Topper73156022014-03-02 09:09:27 +0000194 unsigned &SrcReg2, int &Mask, int &Value) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000195 bool optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
Richard Sandiford564681c2013-08-12 10:28:10 +0000196 unsigned SrcReg2, int Mask, int Value,
Craig Topper73156022014-03-02 09:09:27 +0000197 const MachineRegisterInfo *MRI) const override;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000198 bool canInsertSelect(const MachineBasicBlock&, ArrayRef<MachineOperand> Cond,
199 unsigned, unsigned, int&, int&, int&) const override;
200 void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
201 const DebugLoc &DL, unsigned DstReg,
202 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
203 unsigned FalseReg) const override;
204 bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, unsigned Reg,
205 MachineRegisterInfo *MRI) const override;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000206 bool isPredicable(MachineInstr &MI) const override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000207 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
208 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000209 BranchProbability Probability) const override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000210 bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
211 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
212 MachineBasicBlock &FMBB,
213 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000214 BranchProbability Probability) const override;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000215 bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
216 BranchProbability Probability) const override;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000217 bool PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000218 ArrayRef<MachineOperand> Pred) const override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000219 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000220 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000221 bool KillSrc) const override;
222 void storeRegToStackSlot(MachineBasicBlock &MBB,
223 MachineBasicBlock::iterator MBBI,
224 unsigned SrcReg, bool isKill, int FrameIndex,
225 const TargetRegisterClass *RC,
226 const TargetRegisterInfo *TRI) const override;
227 void loadRegFromStackSlot(MachineBasicBlock &MBB,
228 MachineBasicBlock::iterator MBBI,
229 unsigned DestReg, int FrameIdx,
230 const TargetRegisterClass *RC,
231 const TargetRegisterInfo *TRI) const override;
232 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000233 MachineInstr &MI,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000234 LiveVariables *LV) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000235 MachineInstr *
236 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
237 ArrayRef<unsigned> Ops,
238 MachineBasicBlock::iterator InsertPt, int FrameIndex,
239 LiveIntervals *LIS = nullptr) const override;
240 MachineInstr *foldMemoryOperandImpl(
241 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
242 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
243 LiveIntervals *LIS = nullptr) const override;
244 bool expandPostRAPseudo(MachineInstr &MBBI) const override;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000245 bool reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000246 override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247
248 // Return the SystemZRegisterInfo, which this class owns.
249 const SystemZRegisterInfo &getRegisterInfo() const { return RI; }
250
Richard Sandiford312425f2013-05-20 14:23:08 +0000251 // Return the size in bytes of MI.
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +0000252 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
Richard Sandiford312425f2013-05-20 14:23:08 +0000253
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000254 // Return true if MI is a conditional or unconditional branch.
255 // When returning true, set Cond to the mask of condition-code
256 // values on which the instruction will branch, and set Target
257 // to the operand that contains the branch target. This target
258 // can be a register or a basic block.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000259 SystemZII::Branch getBranchInfo(const MachineInstr &MI) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000260
261 // Get the load and store opcodes for a given register class.
262 void getLoadStoreOpcodes(const TargetRegisterClass *RC,
263 unsigned &LoadOpcode, unsigned &StoreOpcode) const;
264
265 // Opcode is the opcode of an instruction that has an address operand,
266 // and the caller wants to perform that instruction's operation on an
267 // address that has displacement Offset. Return the opcode of a suitable
268 // instruction (which might be Opcode itself) or 0 if no such instruction
269 // exists.
270 unsigned getOpcodeForOffset(unsigned Opcode, int64_t Offset) const;
271
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000272 // If Opcode is a load instruction that has a LOAD AND TEST form,
273 // return the opcode for the testing form, otherwise return 0.
274 unsigned getLoadAndTest(unsigned Opcode) const;
275
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000276 // Return true if ROTATE AND ... SELECTED BITS can be used to select bits
277 // Mask of the R2 operand, given that only the low BitSize bits of Mask are
278 // significant. Set Start and End to the I3 and I4 operands if so.
279 bool isRxSBGMask(uint64_t Mask, unsigned BitSize,
280 unsigned &Start, unsigned &End) const;
281
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000282 // If Opcode is a COMPARE opcode for which an associated fused COMPARE AND *
283 // operation exists, return the opcode for the latter, otherwise return 0.
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000284 // MI, if nonnull, is the compare instruction.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000285 unsigned getFusedCompare(unsigned Opcode,
286 SystemZII::FusedCompareType Type,
287 const MachineInstr *MI = nullptr) const;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000288
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000289 // If Opcode is a LOAD opcode for with an associated LOAD AND TRAP
290 // operation exists, returh the opcode for the latter, otherwise return 0.
291 unsigned getLoadAndTrap(unsigned Opcode) const;
292
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000293 // Emit code before MBBI in MI to move immediate value Value into
294 // physical register Reg.
295 void loadImmediate(MachineBasicBlock &MBB,
296 MachineBasicBlock::iterator MBBI,
297 unsigned Reg, uint64_t Value) const;
Jonas Paulsson8010b632016-10-20 08:27:16 +0000298
299 // Sometimes, it is possible for the target to tell, even without
300 // aliasing information, that two MIs access different memory
301 // addresses. This function returns true if two MIs access different
302 // memory addresses and false otherwise.
303 bool
304 areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
305 AliasAnalysis *AA = nullptr) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000306};
307} // end namespace llvm
308
309#endif