1 //===-- SystemZInstrInfo.h - SystemZ instruction information ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the SystemZ implementation of the TargetInstrInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZINSTRINFO_H
14 #define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZINSTRINFO_H
15 
16 #include "SystemZ.h"
17 #include "SystemZRegisterInfo.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include <cstdint>
24 
25 #define GET_INSTRINFO_HEADER
26 #include "SystemZGenInstrInfo.inc"
27 
28 namespace llvm {
29 
30 class SystemZSubtarget;
31 
32 namespace SystemZII {
33 
34 enum {
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   CCIfNoSignedWrap       = (1 << 21)
51 };
52 
53 static inline unsigned getAccessSize(unsigned int Flags) {
54   return (Flags & AccessSizeMask) >> AccessSizeShift;
55 }
56 
57 static inline unsigned getCCValues(unsigned int Flags) {
58   return (Flags & CCValuesMask) >> CCValuesShift;
59 }
60 
61 static inline unsigned getCompareZeroCCMask(unsigned int Flags) {
62   return (Flags & CompareZeroCCMaskMask) >> CompareZeroCCMaskShift;
63 }
64 
65 // SystemZ MachineOperand target flags.
66 enum {
67   // Masks out the bits for the access model.
68   MO_SYMBOL_MODIFIER = (3 << 0),
69 
70   // @GOT (aka @GOTENT)
71   MO_GOT = (1 << 0),
72 
73   // @INDNTPOFF
74   MO_INDNTPOFF = (2 << 0)
75 };
76 
77 // Classifies a branch.
78 enum BranchType {
79   // An instruction that branches on the current value of CC.
80   BranchNormal,
81 
82   // An instruction that peforms a 32-bit signed comparison and branches
83   // on the result.
84   BranchC,
85 
86   // An instruction that peforms a 32-bit unsigned comparison and branches
87   // on the result.
88   BranchCL,
89 
90   // An instruction that peforms a 64-bit signed comparison and branches
91   // on the result.
92   BranchCG,
93 
94   // An instruction that peforms a 64-bit unsigned comparison and branches
95   // on the result.
96   BranchCLG,
97 
98   // An instruction that decrements a 32-bit register and branches if
99   // the result is nonzero.
100   BranchCT,
101 
102   // An instruction that decrements a 64-bit register and branches if
103   // the result is nonzero.
104   BranchCTG,
105 
106   // An instruction representing an asm goto statement.
107   AsmGoto
108 };
109 
110 // Information about a branch instruction.
111 class Branch {
112   // The target of the branch. In case of INLINEASM_BR, this is nullptr.
113   const MachineOperand *Target;
114 
115 public:
116   // The type of the branch.
117   BranchType Type;
118 
119   // CCMASK_<N> is set if CC might be equal to N.
120   unsigned CCValid;
121 
122   // CCMASK_<N> is set if the branch should be taken when CC == N.
123   unsigned CCMask;
124 
125   Branch(BranchType type, unsigned ccValid, unsigned ccMask,
126          const MachineOperand *target)
127     : Target(target), Type(type), CCValid(ccValid), CCMask(ccMask) {}
128 
129   bool isIndirect() { return Target != nullptr && Target->isReg(); }
130   bool hasMBBTarget() { return Target != nullptr && Target->isMBB(); }
131   MachineBasicBlock *getMBBTarget() {
132     return hasMBBTarget() ? Target->getMBB() : nullptr;
133   }
134 };
135 
136 // Kinds of fused compares in compare-and-* instructions.  Together with type
137 // of the converted compare, this identifies the compare-and-*
138 // instruction.
139 enum FusedCompareType {
140   // Relative branch - CRJ etc.
141   CompareAndBranch,
142 
143   // Indirect branch, used for return - CRBReturn etc.
144   CompareAndReturn,
145 
146   // Indirect branch, used for sibcall - CRBCall etc.
147   CompareAndSibcall,
148 
149   // Trap
150   CompareAndTrap
151 };
152 
153 } // end namespace SystemZII
154 
155 namespace SystemZ {
156 int getTwoOperandOpcode(uint16_t Opcode);
157 int getTargetMemOpcode(uint16_t Opcode);
158 
159 // Return a version of comparison CC mask CCMask in which the LT and GT
160 // actions are swapped.
161 unsigned reverseCCMask(unsigned CCMask);
162 }
163 
164 class SystemZInstrInfo : public SystemZGenInstrInfo {
165   const SystemZRegisterInfo RI;
166   SystemZSubtarget &STI;
167 
168   void splitMove(MachineBasicBlock::iterator MI, unsigned NewOpcode) const;
169   void splitAdjDynAlloc(MachineBasicBlock::iterator MI) const;
170   void expandRIPseudo(MachineInstr &MI, unsigned LowOpcode, unsigned HighOpcode,
171                       bool ConvertHigh) const;
172   void expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
173                        unsigned LowOpcodeK, unsigned HighOpcode) const;
174   void expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
175                        unsigned HighOpcode) const;
176   void expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
177                        unsigned HighOpcode) const;
178   void expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
179                         unsigned Size) const;
180   void expandLoadStackGuard(MachineInstr *MI) const;
181 
182   MachineInstrBuilder
183   emitGRX32Move(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
184                 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
185                 unsigned LowLowOpcode, unsigned Size, bool KillSrc,
186                 bool UndefSrc) const;
187 
188   virtual void anchor();
189 
190 protected:
191   /// Commutes the operands in the given instruction by changing the operands
192   /// order and/or changing the instruction's opcode and/or the immediate value
193   /// operand.
194   ///
195   /// The arguments 'CommuteOpIdx1' and 'CommuteOpIdx2' specify the operands
196   /// to be commuted.
197   ///
198   /// Do not call this method for a non-commutable instruction or
199   /// non-commutable operands.
200   /// Even though the instruction is commutable, the method may still
201   /// fail to commute the operands, null pointer is returned in such cases.
202   MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
203                                        unsigned CommuteOpIdx1,
204                                        unsigned CommuteOpIdx2) const override;
205 
206 public:
207   explicit SystemZInstrInfo(SystemZSubtarget &STI);
208 
209   // Override TargetInstrInfo.
210   unsigned isLoadFromStackSlot(const MachineInstr &MI,
211                                int &FrameIndex) const override;
212   unsigned isStoreToStackSlot(const MachineInstr &MI,
213                               int &FrameIndex) const override;
214   bool isStackSlotCopy(const MachineInstr &MI, int &DestFrameIndex,
215                        int &SrcFrameIndex) const override;
216   bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
217                      MachineBasicBlock *&FBB,
218                      SmallVectorImpl<MachineOperand> &Cond,
219                      bool AllowModify) const override;
220   unsigned removeBranch(MachineBasicBlock &MBB,
221                         int *BytesRemoved = nullptr) const override;
222   unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
223                         MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
224                         const DebugLoc &DL,
225                         int *BytesAdded = nullptr) const override;
226   bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
227                       Register &SrcReg2, int &Mask, int &Value) const override;
228   bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
229                        Register, Register, Register, int &, int &,
230                        int &) const override;
231   void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
232                     const DebugLoc &DL, Register DstReg,
233                     ArrayRef<MachineOperand> Cond, Register TrueReg,
234                     Register FalseReg) const override;
235   bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg,
236                      MachineRegisterInfo *MRI) const override;
237   bool isPredicable(const MachineInstr &MI) const override;
238   bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
239                            unsigned ExtraPredCycles,
240                            BranchProbability Probability) const override;
241   bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
242                            unsigned NumCyclesT, unsigned ExtraPredCyclesT,
243                            MachineBasicBlock &FMBB,
244                            unsigned NumCyclesF, unsigned ExtraPredCyclesF,
245                            BranchProbability Probability) const override;
246   bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
247                             BranchProbability Probability) const override;
248   bool PredicateInstruction(MachineInstr &MI,
249                             ArrayRef<MachineOperand> Pred) const override;
250   void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
251                    const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg,
252                    bool KillSrc) const override;
253   void storeRegToStackSlot(MachineBasicBlock &MBB,
254                            MachineBasicBlock::iterator MBBI,
255                            Register SrcReg, bool isKill, int FrameIndex,
256                            const TargetRegisterClass *RC,
257                            const TargetRegisterInfo *TRI) const override;
258   void loadRegFromStackSlot(MachineBasicBlock &MBB,
259                             MachineBasicBlock::iterator MBBI,
260                             Register DestReg, int FrameIdx,
261                             const TargetRegisterClass *RC,
262                             const TargetRegisterInfo *TRI) const override;
263   MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
264                                       MachineInstr &MI,
265                                       LiveVariables *LV) const override;
266   MachineInstr *
267   foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
268                         ArrayRef<unsigned> Ops,
269                         MachineBasicBlock::iterator InsertPt, int FrameIndex,
270                         LiveIntervals *LIS = nullptr,
271                         VirtRegMap *VRM = nullptr) const override;
272   MachineInstr *foldMemoryOperandImpl(
273       MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
274       MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
275       LiveIntervals *LIS = nullptr) const override;
276   bool expandPostRAPseudo(MachineInstr &MBBI) const override;
277   bool reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
278     override;
279 
280   // Return the SystemZRegisterInfo, which this class owns.
281   const SystemZRegisterInfo &getRegisterInfo() const { return RI; }
282 
283   // Return the size in bytes of MI.
284   unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
285 
286   // Return true if MI is a conditional or unconditional branch.
287   // When returning true, set Cond to the mask of condition-code
288   // values on which the instruction will branch, and set Target
289   // to the operand that contains the branch target.  This target
290   // can be a register or a basic block.
291   SystemZII::Branch getBranchInfo(const MachineInstr &MI) const;
292 
293   // Get the load and store opcodes for a given register class.
294   void getLoadStoreOpcodes(const TargetRegisterClass *RC,
295                            unsigned &LoadOpcode, unsigned &StoreOpcode) const;
296 
297   // Opcode is the opcode of an instruction that has an address operand,
298   // and the caller wants to perform that instruction's operation on an
299   // address that has displacement Offset.  Return the opcode of a suitable
300   // instruction (which might be Opcode itself) or 0 if no such instruction
301   // exists.
302   unsigned getOpcodeForOffset(unsigned Opcode, int64_t Offset) const;
303 
304   // If Opcode is a load instruction that has a LOAD AND TEST form,
305   // return the opcode for the testing form, otherwise return 0.
306   unsigned getLoadAndTest(unsigned Opcode) const;
307 
308   // Return true if ROTATE AND ... SELECTED BITS can be used to select bits
309   // Mask of the R2 operand, given that only the low BitSize bits of Mask are
310   // significant.  Set Start and End to the I3 and I4 operands if so.
311   bool isRxSBGMask(uint64_t Mask, unsigned BitSize,
312                    unsigned &Start, unsigned &End) const;
313 
314   // If Opcode is a COMPARE opcode for which an associated fused COMPARE AND *
315   // operation exists, return the opcode for the latter, otherwise return 0.
316   // MI, if nonnull, is the compare instruction.
317   unsigned getFusedCompare(unsigned Opcode,
318                            SystemZII::FusedCompareType Type,
319                            const MachineInstr *MI = nullptr) const;
320 
321   // Try to find all CC users of the compare instruction (MBBI) and update
322   // all of them to maintain equivalent behavior after swapping the compare
323   // operands. Return false if not all users can be conclusively found and
324   // handled. The compare instruction is *not* changed.
325   bool prepareCompareSwapOperands(MachineBasicBlock::iterator MBBI) const;
326 
327   // If Opcode is a LOAD opcode for with an associated LOAD AND TRAP
328   // operation exists, returh the opcode for the latter, otherwise return 0.
329   unsigned getLoadAndTrap(unsigned Opcode) const;
330 
331   // Emit code before MBBI in MI to move immediate value Value into
332   // physical register Reg.
333   void loadImmediate(MachineBasicBlock &MBB,
334                      MachineBasicBlock::iterator MBBI,
335                      unsigned Reg, uint64_t Value) const;
336 
337   // Perform target specific instruction verification.
338   bool verifyInstruction(const MachineInstr &MI,
339                          StringRef &ErrInfo) const override;
340 
341   // Sometimes, it is possible for the target to tell, even without
342   // aliasing information, that two MIs access different memory
343   // addresses. This function returns true if two MIs access different
344   // memory addresses and false otherwise.
345   bool
346   areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
347                                   const MachineInstr &MIb) const override;
348 };
349 
350 } // end namespace llvm
351 
352 #endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZINSTRINFO_H
353