[FastISel][AArch64] Add target-dependent instruction selection for Add/Sub.
[oota-llvm.git] / lib / Target / R600 / AMDGPUInstrInfo.h
1 //===-- AMDGPUInstrInfo.h - AMDGPU 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 /// \file
11 /// \brief Contains the definition of a TargetInstrInfo class that is common
12 /// to all AMD GPUs.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_LIB_TARGET_R600_AMDGPUINSTRINFO_H
17 #define LLVM_LIB_TARGET_R600_AMDGPUINSTRINFO_H
18
19 #include "AMDGPURegisterInfo.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include <map>
22
23 #define GET_INSTRINFO_HEADER
24 #define GET_INSTRINFO_ENUM
25 #define GET_INSTRINFO_OPERAND_ENUM
26 #include "AMDGPUGenInstrInfo.inc"
27
28 #define OPCODE_IS_ZERO_INT AMDGPU::PRED_SETE_INT
29 #define OPCODE_IS_NOT_ZERO_INT AMDGPU::PRED_SETNE_INT
30 #define OPCODE_IS_ZERO AMDGPU::PRED_SETE
31 #define OPCODE_IS_NOT_ZERO AMDGPU::PRED_SETNE
32
33 namespace llvm {
34
35 class AMDGPUSubtarget;
36 class MachineFunction;
37 class MachineInstr;
38 class MachineInstrBuilder;
39
40 class AMDGPUInstrInfo : public AMDGPUGenInstrInfo {
41 private:
42   const AMDGPURegisterInfo RI;
43   bool getNextBranchInstr(MachineBasicBlock::iterator &iter,
44                           MachineBasicBlock &MBB) const;
45   virtual void anchor();
46 protected:
47   const AMDGPUSubtarget &ST;
48 public:
49   explicit AMDGPUInstrInfo(const AMDGPUSubtarget &st);
50
51   virtual const AMDGPURegisterInfo &getRegisterInfo() const = 0;
52
53   bool isCoalescableExtInstr(const MachineInstr &MI, unsigned &SrcReg,
54                              unsigned &DstReg, unsigned &SubIdx) const override;
55
56   unsigned isLoadFromStackSlot(const MachineInstr *MI,
57                                int &FrameIndex) const override;
58   unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
59                                      int &FrameIndex) const override;
60   bool hasLoadFromStackSlot(const MachineInstr *MI,
61                             const MachineMemOperand *&MMO,
62                             int &FrameIndex) const override;
63   unsigned isStoreFromStackSlot(const MachineInstr *MI, int &FrameIndex) const;
64   unsigned isStoreFromStackSlotPostFE(const MachineInstr *MI,
65                                       int &FrameIndex) const;
66   bool hasStoreFromStackSlot(const MachineInstr *MI,
67                              const MachineMemOperand *&MMO,
68                              int &FrameIndex) const;
69
70   MachineInstr *
71   convertToThreeAddress(MachineFunction::iterator &MFI,
72                         MachineBasicBlock::iterator &MBBI,
73                         LiveVariables *LV) const override;
74
75
76   virtual void copyPhysReg(MachineBasicBlock &MBB,
77                            MachineBasicBlock::iterator MI, DebugLoc DL,
78                            unsigned DestReg, unsigned SrcReg,
79                            bool KillSrc) const = 0;
80
81   bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
82
83   void storeRegToStackSlot(MachineBasicBlock &MBB,
84                            MachineBasicBlock::iterator MI,
85                            unsigned SrcReg, bool isKill, int FrameIndex,
86                            const TargetRegisterClass *RC,
87                            const TargetRegisterInfo *TRI) const override;
88   void loadRegFromStackSlot(MachineBasicBlock &MBB,
89                             MachineBasicBlock::iterator MI,
90                             unsigned DestReg, int FrameIndex,
91                             const TargetRegisterClass *RC,
92                             const TargetRegisterInfo *TRI) const override;
93
94 protected:
95   MachineInstr *foldMemoryOperandImpl(MachineFunction &MF,
96                                       MachineInstr *MI,
97                                       const SmallVectorImpl<unsigned> &Ops,
98                                       int FrameIndex) const override;
99   MachineInstr *foldMemoryOperandImpl(MachineFunction &MF,
100                                       MachineInstr *MI,
101                                       const SmallVectorImpl<unsigned> &Ops,
102                                       MachineInstr *LoadMI) const override;
103   /// \returns the smallest register index that will be accessed by an indirect
104   /// read or write or -1 if indirect addressing is not used by this program.
105   int getIndirectIndexBegin(const MachineFunction &MF) const;
106
107   /// \returns the largest register index that will be accessed by an indirect
108   /// read or write or -1 if indirect addressing is not used by this program.
109   int getIndirectIndexEnd(const MachineFunction &MF) const;
110
111 public:
112   bool canFoldMemoryOperand(const MachineInstr *MI,
113                            const SmallVectorImpl<unsigned> &Ops) const override;
114   bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
115                         unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
116                         SmallVectorImpl<MachineInstr *> &NewMIs) const override;
117   bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
118                            SmallVectorImpl<SDNode *> &NewNodes) const override;
119   unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
120                                bool UnfoldLoad, bool UnfoldStore,
121                                unsigned *LoadRegIndex = nullptr) const override;
122
123   bool enableClusterLoads() const override;
124
125   bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
126                                int64_t Offset1, int64_t Offset2,
127                                unsigned NumLoads) const override;
128
129   bool
130   ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
131   void insertNoop(MachineBasicBlock &MBB,
132                   MachineBasicBlock::iterator MI) const override;
133   bool isPredicated(const MachineInstr *MI) const override;
134   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
135                    const SmallVectorImpl<MachineOperand> &Pred2) const override;
136   bool DefinesPredicate(MachineInstr *MI,
137                         std::vector<MachineOperand> &Pred) const override;
138   bool isPredicable(MachineInstr *MI) const override;
139   bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
140
141   // Helper functions that check the opcode for status information
142   bool isRegisterStore(const MachineInstr &MI) const;
143   bool isRegisterLoad(const MachineInstr &MI) const;
144
145 //===---------------------------------------------------------------------===//
146 // Pure virtual funtions to be implemented by sub-classes.
147 //===---------------------------------------------------------------------===//
148
149   virtual bool isMov(unsigned opcode) const = 0;
150
151   /// \brief Calculate the "Indirect Address" for the given \p RegIndex and
152   ///        \p Channel
153   ///
154   /// We model indirect addressing using a virtual address space that can be
155   /// accesed with loads and stores.  The "Indirect Address" is the memory
156   /// address in this virtual address space that maps to the given \p RegIndex
157   /// and \p Channel.
158   virtual unsigned calculateIndirectAddress(unsigned RegIndex,
159                                             unsigned Channel) const = 0;
160
161   /// \returns The register class to be used for loading and storing values
162   /// from an "Indirect Address" .
163   virtual const TargetRegisterClass *getIndirectAddrRegClass() const = 0;
164
165   /// \brief Build instruction(s) for an indirect register write.
166   ///
167   /// \returns The instruction that performs the indirect register write
168   virtual MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
169                                     MachineBasicBlock::iterator I,
170                                     unsigned ValueReg, unsigned Address,
171                                     unsigned OffsetReg) const = 0;
172
173   /// \brief Build instruction(s) for an indirect register read.
174   ///
175   /// \returns The instruction that performs the indirect register read
176   virtual MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
177                                     MachineBasicBlock::iterator I,
178                                     unsigned ValueReg, unsigned Address,
179                                     unsigned OffsetReg) const = 0;
180
181   /// \brief Build a MOV instruction.
182   virtual MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
183                                       MachineBasicBlock::iterator I,
184                                       unsigned DstReg, unsigned SrcReg) const = 0;
185
186   /// \brief Given a MIMG \p Opcode that writes all 4 channels, return the
187   /// equivalent opcode that writes \p Channels Channels.
188   int getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const;
189
190 };
191
192 namespace AMDGPU {
193   int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex);
194 }  // End namespace AMDGPU
195
196 } // End llvm namespace
197
198 #define AMDGPU_FLAG_REGISTER_LOAD  (UINT64_C(1) << 63)
199 #define AMDGPU_FLAG_REGISTER_STORE (UINT64_C(1) << 62)
200
201 #endif