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