blob: 3fb7880cfeee08934ea7648559755135524b5399 [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"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFunction.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000022#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000023#include <cstdint>
Ulrich Weigand5f613df2013-05-06 16:15:19 +000024
25#define GET_INSTRINFO_HEADER
26#include "SystemZGenInstrInfo.inc"
27
28namespace llvm {
29
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000030class SystemZSubtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000031
32namespace SystemZII {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000033
Richard Sandifordc2312692014-03-06 10:38:30 +000034enum {
35 // See comments in SystemZInstrFormats.td.
36 SimpleBDXLoad = (1 << 0),
37 SimpleBDXStore = (1 << 1),
38 Has20BitOffset = (1 << 2),
39 HasIndex = (1 << 3),
40 Is128Bit = (1 << 4),
41 AccessSizeMask = (31 << 5),
42 AccessSizeShift = 5,
43 CCValuesMask = (15 << 10),
44 CCValuesShift = 10,
45 CompareZeroCCMaskMask = (15 << 14),
46 CompareZeroCCMaskShift = 14,
47 CCMaskFirst = (1 << 18),
48 CCMaskLast = (1 << 19),
49 IsLogical = (1 << 20)
50};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000051
Richard Sandifordc2312692014-03-06 10:38:30 +000052static inline unsigned getAccessSize(unsigned int Flags) {
53 return (Flags & AccessSizeMask) >> AccessSizeShift;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000054}
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000055
Richard Sandifordc2312692014-03-06 10:38:30 +000056static inline unsigned getCCValues(unsigned int Flags) {
57 return (Flags & CCValuesMask) >> CCValuesShift;
58}
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000059
Richard Sandifordc2312692014-03-06 10:38:30 +000060static inline unsigned getCompareZeroCCMask(unsigned int Flags) {
61 return (Flags & CompareZeroCCMaskMask) >> CompareZeroCCMaskShift;
62}
63
64// SystemZ MachineOperand target flags.
65enum {
66 // Masks out the bits for the access model.
Ulrich Weigand7db69182015-02-18 09:13:27 +000067 MO_SYMBOL_MODIFIER = (3 << 0),
Richard Sandifordc2312692014-03-06 10:38:30 +000068
69 // @GOT (aka @GOTENT)
Ulrich Weigand7db69182015-02-18 09:13:27 +000070 MO_GOT = (1 << 0),
71
72 // @INDNTPOFF
73 MO_INDNTPOFF = (2 << 0)
Richard Sandifordc2312692014-03-06 10:38:30 +000074};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000075
Richard Sandifordc2312692014-03-06 10:38:30 +000076// Classifies a branch.
77enum BranchType {
78 // An instruction that branches on the current value of CC.
79 BranchNormal,
80
81 // An instruction that peforms a 32-bit signed comparison and branches
82 // on the result.
83 BranchC,
84
85 // An instruction that peforms a 32-bit unsigned comparison and branches
86 // on the result.
87 BranchCL,
88
89 // An instruction that peforms a 64-bit signed comparison and branches
90 // on the result.
91 BranchCG,
92
93 // An instruction that peforms a 64-bit unsigned comparison and branches
94 // on the result.
95 BranchCLG,
96
97 // An instruction that decrements a 32-bit register and branches if
98 // the result is nonzero.
99 BranchCT,
100
101 // An instruction that decrements a 64-bit register and branches if
102 // the result is nonzero.
103 BranchCTG
104};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000105
Richard Sandifordc2312692014-03-06 10:38:30 +0000106// Information about a branch instruction.
107struct Branch {
108 // The type of the branch.
109 BranchType Type;
110
111 // CCMASK_<N> is set if CC might be equal to N.
112 unsigned CCValid;
113
114 // CCMASK_<N> is set if the branch should be taken when CC == N.
115 unsigned CCMask;
116
117 // The target of the branch.
118 const MachineOperand *Target;
119
120 Branch(BranchType type, unsigned ccValid, unsigned ccMask,
121 const MachineOperand *target)
122 : Type(type), CCValid(ccValid), CCMask(ccMask), Target(target) {}
123};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000124
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000125// Kinds of fused compares in compare-and-* instructions. Together with type
126// of the converted compare, this identifies the compare-and-*
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000127// instruction.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000128enum FusedCompareType {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000129 // Relative branch - CRJ etc.
130 CompareAndBranch,
131
132 // Indirect branch, used for return - CRBReturn etc.
Ulrich Weigand848a5132016-04-11 12:12:32 +0000133 CompareAndReturn,
134
135 // Indirect branch, used for sibcall - CRBCall etc.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000136 CompareAndSibcall,
137
138 // Trap
139 CompareAndTrap
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000140};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000141
Richard Sandifordc2312692014-03-06 10:38:30 +0000142} // end namespace SystemZII
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000143
144class SystemZInstrInfo : public SystemZGenInstrInfo {
145 const SystemZRegisterInfo RI;
Eric Christopher673b3af2014-06-27 07:01:17 +0000146 SystemZSubtarget &STI;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000147
148 void splitMove(MachineBasicBlock::iterator MI, unsigned NewOpcode) const;
149 void splitAdjDynAlloc(MachineBasicBlock::iterator MI) const;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 void expandRIPseudo(MachineInstr &MI, unsigned LowOpcode, unsigned HighOpcode,
151 bool ConvertHigh) const;
152 void expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000153 unsigned LowOpcodeK, unsigned HighOpcode) const;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 void expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000155 unsigned HighOpcode) const;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000156 void expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
157 unsigned HighOpcode) const;
158 void expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode,
159 unsigned HighOpcode) const;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000160 void expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000161 unsigned Size) const;
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000162 void expandLoadStackGuard(MachineInstr *MI) const;
Richard Sandiford0755c932013-10-01 11:26:28 +0000163 void emitGRX32Move(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000164 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000165 unsigned LowLowOpcode, unsigned Size, bool KillSrc,
166 bool UndefSrc) const;
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000167 virtual void anchor();
Ulrich Weigand524f2762016-11-28 13:34:08 +0000168
169protected:
170 /// Commutes the operands in the given instruction by changing the operands
171 /// order and/or changing the instruction's opcode and/or the immediate value
172 /// operand.
173 ///
174 /// The arguments 'CommuteOpIdx1' and 'CommuteOpIdx2' specify the operands
175 /// to be commuted.
176 ///
177 /// Do not call this method for a non-commutable instruction or
178 /// non-commutable operands.
179 /// Even though the instruction is commutable, the method may still
180 /// fail to commute the operands, null pointer is returned in such cases.
181 MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
182 unsigned CommuteOpIdx1,
183 unsigned CommuteOpIdx2) const override;
184
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185public:
Eric Christopher673b3af2014-06-27 07:01:17 +0000186 explicit SystemZInstrInfo(SystemZSubtarget &STI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000187
188 // Override TargetInstrInfo.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000189 unsigned isLoadFromStackSlot(const MachineInstr &MI,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000190 int &FrameIndex) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000191 unsigned isStoreToStackSlot(const MachineInstr &MI,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000192 int &FrameIndex) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000193 bool isStackSlotCopy(const MachineInstr &MI, int &DestFrameIndex,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000194 int &SrcFrameIndex) const override;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000195 bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000196 MachineBasicBlock *&FBB,
197 SmallVectorImpl<MachineOperand> &Cond,
198 bool AllowModify) const override;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000199 unsigned removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000200 int *BytesRemoved = nullptr) const override;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000201 unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000202 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000203 const DebugLoc &DL,
204 int *BytesAdded = nullptr) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000205 bool analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
Craig Topper73156022014-03-02 09:09:27 +0000206 unsigned &SrcReg2, int &Mask, int &Value) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000207 bool optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
Richard Sandiford564681c2013-08-12 10:28:10 +0000208 unsigned SrcReg2, int Mask, int Value,
Craig Topper73156022014-03-02 09:09:27 +0000209 const MachineRegisterInfo *MRI) const override;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000210 bool canInsertSelect(const MachineBasicBlock&, ArrayRef<MachineOperand> Cond,
211 unsigned, unsigned, int&, int&, int&) const override;
212 void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
213 const DebugLoc &DL, unsigned DstReg,
214 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
215 unsigned FalseReg) const override;
216 bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, unsigned Reg,
217 MachineRegisterInfo *MRI) const override;
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000218 bool isPredicable(const MachineInstr &MI) const override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000219 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
220 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000221 BranchProbability Probability) const override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000222 bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
223 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
224 MachineBasicBlock &FMBB,
225 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000226 BranchProbability Probability) const override;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000227 bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
228 BranchProbability Probability) const override;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000229 bool PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000230 ArrayRef<MachineOperand> Pred) const override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000231 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000232 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000233 bool KillSrc) const override;
234 void storeRegToStackSlot(MachineBasicBlock &MBB,
235 MachineBasicBlock::iterator MBBI,
236 unsigned SrcReg, bool isKill, int FrameIndex,
237 const TargetRegisterClass *RC,
238 const TargetRegisterInfo *TRI) const override;
239 void loadRegFromStackSlot(MachineBasicBlock &MBB,
240 MachineBasicBlock::iterator MBBI,
241 unsigned DestReg, int FrameIdx,
242 const TargetRegisterClass *RC,
243 const TargetRegisterInfo *TRI) const override;
244 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000245 MachineInstr &MI,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000246 LiveVariables *LV) const override;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000247 MachineInstr *
248 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
249 ArrayRef<unsigned> Ops,
250 MachineBasicBlock::iterator InsertPt, int FrameIndex,
251 LiveIntervals *LIS = nullptr) const override;
252 MachineInstr *foldMemoryOperandImpl(
253 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
254 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
255 LiveIntervals *LIS = nullptr) const override;
256 bool expandPostRAPseudo(MachineInstr &MBBI) const override;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000257 bool reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000258 override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000259
260 // Return the SystemZRegisterInfo, which this class owns.
261 const SystemZRegisterInfo &getRegisterInfo() const { return RI; }
262
Richard Sandiford312425f2013-05-20 14:23:08 +0000263 // Return the size in bytes of MI.
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +0000264 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
Richard Sandiford312425f2013-05-20 14:23:08 +0000265
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000266 // Return true if MI is a conditional or unconditional branch.
267 // When returning true, set Cond to the mask of condition-code
268 // values on which the instruction will branch, and set Target
269 // to the operand that contains the branch target. This target
270 // can be a register or a basic block.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000271 SystemZII::Branch getBranchInfo(const MachineInstr &MI) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000272
273 // Get the load and store opcodes for a given register class.
274 void getLoadStoreOpcodes(const TargetRegisterClass *RC,
275 unsigned &LoadOpcode, unsigned &StoreOpcode) const;
276
277 // Opcode is the opcode of an instruction that has an address operand,
278 // and the caller wants to perform that instruction's operation on an
279 // address that has displacement Offset. Return the opcode of a suitable
280 // instruction (which might be Opcode itself) or 0 if no such instruction
281 // exists.
282 unsigned getOpcodeForOffset(unsigned Opcode, int64_t Offset) const;
283
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000284 // If Opcode is a load instruction that has a LOAD AND TEST form,
285 // return the opcode for the testing form, otherwise return 0.
286 unsigned getLoadAndTest(unsigned Opcode) const;
287
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000288 // Return true if ROTATE AND ... SELECTED BITS can be used to select bits
289 // Mask of the R2 operand, given that only the low BitSize bits of Mask are
290 // significant. Set Start and End to the I3 and I4 operands if so.
291 bool isRxSBGMask(uint64_t Mask, unsigned BitSize,
292 unsigned &Start, unsigned &End) const;
293
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000294 // If Opcode is a COMPARE opcode for which an associated fused COMPARE AND *
295 // operation exists, return the opcode for the latter, otherwise return 0.
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000296 // MI, if nonnull, is the compare instruction.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000297 unsigned getFusedCompare(unsigned Opcode,
298 SystemZII::FusedCompareType Type,
299 const MachineInstr *MI = nullptr) const;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000300
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000301 // If Opcode is a LOAD opcode for with an associated LOAD AND TRAP
302 // operation exists, returh the opcode for the latter, otherwise return 0.
303 unsigned getLoadAndTrap(unsigned Opcode) const;
304
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305 // Emit code before MBBI in MI to move immediate value Value into
306 // physical register Reg.
307 void loadImmediate(MachineBasicBlock &MBB,
308 MachineBasicBlock::iterator MBBI,
309 unsigned Reg, uint64_t Value) const;
Jonas Paulsson8010b632016-10-20 08:27:16 +0000310
311 // Sometimes, it is possible for the target to tell, even without
312 // aliasing information, that two MIs access different memory
313 // addresses. This function returns true if two MIs access different
314 // memory addresses and false otherwise.
315 bool
316 areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
317 AliasAnalysis *AA = nullptr) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000318};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000319
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000320} // end namespace llvm
321
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000322#endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZINSTRINFO_H