30f4c7dde446a12b86df24000b454b4a3e6013dd
[oota-llvm.git] / lib / Target / SystemZ / SystemZInstrInfo.h
1 //===-- 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
14 #ifndef LLVM_TARGET_SYSTEMZINSTRINFO_H
15 #define LLVM_TARGET_SYSTEMZINSTRINFO_H
16
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
24 namespace llvm {
25
26 class SystemZTargetMachine;
27
28 namespace SystemZII {
29   enum {
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   };
46   static inline unsigned getAccessSize(unsigned int Flags) {
47     return (Flags & AccessSizeMask) >> AccessSizeShift;
48   }
49   static inline unsigned getCCValues(unsigned int Flags) {
50     return (Flags & CCValuesMask) >> CCValuesShift;
51   }
52   static inline unsigned getCompareZeroCCMask(unsigned int Flags) {
53     return (Flags & CompareZeroCCMaskMask) >> CompareZeroCCMaskShift;
54   }
55
56   // SystemZ MachineOperand target flags.
57   enum {
58     // Masks out the bits for the access model.
59     MO_SYMBOL_MODIFIER = (1 << 0),
60
61     // @GOT (aka @GOTENT)
62     MO_GOT = (1 << 0)
63   };
64   // Classifies a branch.
65   enum BranchType {
66     // An instruction that branches on the current value of CC.
67     BranchNormal,
68
69     // An instruction that peforms a 32-bit signed comparison and branches
70     // on the result.
71     BranchC,
72
73     // An instruction that peforms a 32-bit unsigned comparison and branches
74     // on the result.
75     BranchCL,
76
77     // An instruction that peforms a 64-bit signed comparison and branches
78     // on the result.
79     BranchCG,
80
81     // An instruction that peforms a 64-bit unsigned comparison and branches
82     // on the result.
83     BranchCLG,
84
85     // An instruction that decrements a 32-bit register and branches if
86     // the result is nonzero.
87     BranchCT,
88
89     // An instruction that decrements a 64-bit register and branches if
90     // the result is nonzero.
91     BranchCTG
92   };
93   // Information about a branch instruction.
94   struct Branch {
95     // The type of the branch.
96     BranchType Type;
97
98     // CCMASK_<N> is set if CC might be equal to N.
99     unsigned CCValid;
100
101     // CCMASK_<N> is set if the branch should be taken when CC == N.
102     unsigned CCMask;
103
104     // The target of the branch.
105     const MachineOperand *Target;
106
107     Branch(BranchType type, unsigned ccValid, unsigned ccMask,
108            const MachineOperand *target)
109       : Type(type), CCValid(ccValid), CCMask(ccMask), Target(target) {}
110   };
111 }
112
113 class SystemZInstrInfo : public SystemZGenInstrInfo {
114   const SystemZRegisterInfo RI;
115   SystemZTargetMachine &TM;
116
117   void splitMove(MachineBasicBlock::iterator MI, unsigned NewOpcode) const;
118   void splitAdjDynAlloc(MachineBasicBlock::iterator MI) const;
119   void expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
120                        unsigned HighOpcode) const;
121   void expandZExtPseudo(MachineInstr *MI, unsigned LowOpcode,
122                         unsigned Size) const;
123   void emitGRX32Move(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
124                      DebugLoc DL, unsigned DestReg, unsigned SrcReg,
125                      unsigned LowLowOpcode, unsigned Size, bool KillSrc) const;
126   
127 public:
128   explicit SystemZInstrInfo(SystemZTargetMachine &TM);
129
130   // Override TargetInstrInfo.
131   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
132                                        int &FrameIndex) const LLVM_OVERRIDE;
133   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
134                                       int &FrameIndex) const LLVM_OVERRIDE;
135   virtual bool isStackSlotCopy(const MachineInstr *MI, int &DestFrameIndex,
136                                int &SrcFrameIndex) const LLVM_OVERRIDE;
137   virtual bool AnalyzeBranch(MachineBasicBlock &MBB,
138                              MachineBasicBlock *&TBB,
139                              MachineBasicBlock *&FBB,
140                              SmallVectorImpl<MachineOperand> &Cond,
141                              bool AllowModify) const LLVM_OVERRIDE;
142   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const LLVM_OVERRIDE;
143   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
144                                 MachineBasicBlock *FBB,
145                                 const SmallVectorImpl<MachineOperand> &Cond,
146                                 DebugLoc DL) const LLVM_OVERRIDE;
147   bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
148                       unsigned &SrcReg2, int &Mask, int &Value) const
149     LLVM_OVERRIDE;
150   bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
151                             unsigned SrcReg2, int Mask, int Value,
152                             const MachineRegisterInfo *MRI) const LLVM_OVERRIDE;
153   virtual bool isPredicable(MachineInstr *MI) const LLVM_OVERRIDE;
154   virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
155                                    unsigned ExtraPredCycles,
156                                    const BranchProbability &Probability) const
157     LLVM_OVERRIDE;
158   virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
159                                    unsigned NumCyclesT,
160                                    unsigned ExtraPredCyclesT,
161                                    MachineBasicBlock &FMBB,
162                                    unsigned NumCyclesF,
163                                    unsigned ExtraPredCyclesF,
164                                    const BranchProbability &Probability) const
165     LLVM_OVERRIDE;
166   virtual bool
167     PredicateInstruction(MachineInstr *MI,
168                          const SmallVectorImpl<MachineOperand> &Pred) const
169     LLVM_OVERRIDE;
170   virtual void copyPhysReg(MachineBasicBlock &MBB,
171                            MachineBasicBlock::iterator MBBI, DebugLoc DL,
172                            unsigned DestReg, unsigned SrcReg,
173                            bool KillSrc) const LLVM_OVERRIDE;
174   virtual void
175     storeRegToStackSlot(MachineBasicBlock &MBB,
176                         MachineBasicBlock::iterator MBBI,
177                         unsigned SrcReg, bool isKill, int FrameIndex,
178                         const TargetRegisterClass *RC,
179                         const TargetRegisterInfo *TRI) const LLVM_OVERRIDE;
180   virtual void
181     loadRegFromStackSlot(MachineBasicBlock &MBB,
182                          MachineBasicBlock::iterator MBBI,
183                          unsigned DestReg, int FrameIdx,
184                          const TargetRegisterClass *RC,
185                          const TargetRegisterInfo *TRI) const LLVM_OVERRIDE;
186   virtual MachineInstr *
187     convertToThreeAddress(MachineFunction::iterator &MFI,
188                           MachineBasicBlock::iterator &MBBI,
189                           LiveVariables *LV) const;
190   virtual MachineInstr *
191     foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
192                           const SmallVectorImpl<unsigned> &Ops,
193                           int FrameIndex) const;
194   virtual MachineInstr *
195     foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
196                           const SmallVectorImpl<unsigned> &Ops,
197                           MachineInstr* LoadMI) const;
198   virtual bool
199     expandPostRAPseudo(MachineBasicBlock::iterator MBBI) const LLVM_OVERRIDE;
200   virtual bool
201     ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
202     LLVM_OVERRIDE;
203
204   // Return the SystemZRegisterInfo, which this class owns.
205   const SystemZRegisterInfo &getRegisterInfo() const { return RI; }
206
207   // Return the size in bytes of MI.
208   uint64_t getInstSizeInBytes(const MachineInstr *MI) const;
209
210   // Return true if MI is a conditional or unconditional branch.
211   // When returning true, set Cond to the mask of condition-code
212   // values on which the instruction will branch, and set Target
213   // to the operand that contains the branch target.  This target
214   // can be a register or a basic block.
215   SystemZII::Branch getBranchInfo(const MachineInstr *MI) const;
216
217   // Get the load and store opcodes for a given register class.
218   void getLoadStoreOpcodes(const TargetRegisterClass *RC,
219                            unsigned &LoadOpcode, unsigned &StoreOpcode) const;
220
221   // Opcode is the opcode of an instruction that has an address operand,
222   // and the caller wants to perform that instruction's operation on an
223   // address that has displacement Offset.  Return the opcode of a suitable
224   // instruction (which might be Opcode itself) or 0 if no such instruction
225   // exists.
226   unsigned getOpcodeForOffset(unsigned Opcode, int64_t Offset) const;
227
228   // If Opcode is a load instruction that has a LOAD AND TEST form,
229   // return the opcode for the testing form, otherwise return 0.
230   unsigned getLoadAndTest(unsigned Opcode) const;
231
232   // Return true if ROTATE AND ... SELECTED BITS can be used to select bits
233   // Mask of the R2 operand, given that only the low BitSize bits of Mask are
234   // significant.  Set Start and End to the I3 and I4 operands if so.
235   bool isRxSBGMask(uint64_t Mask, unsigned BitSize,
236                    unsigned &Start, unsigned &End) const;
237
238   // If Opcode is a COMPARE opcode for which an associated COMPARE AND
239   // BRANCH exists, return the opcode for the latter, otherwise return 0.
240   // MI, if nonnull, is the compare instruction.
241   unsigned getCompareAndBranch(unsigned Opcode,
242                                const MachineInstr *MI = 0) const;
243
244   // Emit code before MBBI in MI to move immediate value Value into
245   // physical register Reg.
246   void loadImmediate(MachineBasicBlock &MBB,
247                      MachineBasicBlock::iterator MBBI,
248                      unsigned Reg, uint64_t Value) const;
249 };
250 } // end namespace llvm
251
252 #endif