[systemz] Distinguish the 'Q', 'R', 'S', and 'T' inline assembly memory constraints.
[oota-llvm.git] / lib / Target / SystemZ / SystemZISelLowering.h
1 //===-- SystemZISelLowering.h - SystemZ DAG lowering interface --*- 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 defines the interfaces that SystemZ uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZISELLOWERING_H
16 #define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZISELLOWERING_H
17
18 #include "SystemZ.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/Target/TargetLowering.h"
22
23 namespace llvm {
24 namespace SystemZISD {
25 enum {
26   FIRST_NUMBER = ISD::BUILTIN_OP_END,
27
28   // Return with a flag operand.  Operand 0 is the chain operand.
29   RET_FLAG,
30
31   // Calls a function.  Operand 0 is the chain operand and operand 1
32   // is the target address.  The arguments start at operand 2.
33   // There is an optional glue operand at the end.
34   CALL,
35   SIBCALL,
36
37   // TLS calls.  Like regular calls, except operand 1 is the TLS symbol.
38   // (The call target is implicitly __tls_get_offset.)
39   TLS_GDCALL,
40   TLS_LDCALL,
41
42   // Wraps a TargetGlobalAddress that should be loaded using PC-relative
43   // accesses (LARL).  Operand 0 is the address.
44   PCREL_WRAPPER,
45
46   // Used in cases where an offset is applied to a TargetGlobalAddress.
47   // Operand 0 is the full TargetGlobalAddress and operand 1 is a
48   // PCREL_WRAPPER for an anchor point.  This is used so that we can
49   // cheaply refer to either the full address or the anchor point
50   // as a register base.
51   PCREL_OFFSET,
52
53   // Integer absolute.
54   IABS,
55
56   // Integer comparisons.  There are three operands: the two values
57   // to compare, and an integer of type SystemZICMP.
58   ICMP,
59
60   // Floating-point comparisons.  The two operands are the values to compare.
61   FCMP,
62
63   // Test under mask.  The first operand is ANDed with the second operand
64   // and the condition codes are set on the result.  The third operand is
65   // a boolean that is true if the condition codes need to distinguish
66   // between CCMASK_TM_MIXED_MSB_0 and CCMASK_TM_MIXED_MSB_1 (which the
67   // register forms do but the memory forms don't).
68   TM,
69
70   // Branches if a condition is true.  Operand 0 is the chain operand;
71   // operand 1 is the 4-bit condition-code mask, with bit N in
72   // big-endian order meaning "branch if CC=N"; operand 2 is the
73   // target block and operand 3 is the flag operand.
74   BR_CCMASK,
75
76   // Selects between operand 0 and operand 1.  Operand 2 is the
77   // mask of condition-code values for which operand 0 should be
78   // chosen over operand 1; it has the same form as BR_CCMASK.
79   // Operand 3 is the flag operand.
80   SELECT_CCMASK,
81
82   // Evaluates to the gap between the stack pointer and the
83   // base of the dynamically-allocatable area.
84   ADJDYNALLOC,
85
86   // Extracts the value of a 32-bit access register.  Operand 0 is
87   // the number of the register.
88   EXTRACT_ACCESS,
89
90   // Wrappers around the ISD opcodes of the same name.  The output and
91   // first input operands are GR128s.  The trailing numbers are the
92   // widths of the second operand in bits.
93   UMUL_LOHI64,
94   SDIVREM32,
95   SDIVREM64,
96   UDIVREM32,
97   UDIVREM64,
98
99   // Use a series of MVCs to copy bytes from one memory location to another.
100   // The operands are:
101   // - the target address
102   // - the source address
103   // - the constant length
104   //
105   // This isn't a memory opcode because we'd need to attach two
106   // MachineMemOperands rather than one.
107   MVC,
108
109   // Like MVC, but implemented as a loop that handles X*256 bytes
110   // followed by straight-line code to handle the rest (if any).
111   // The value of X is passed as an additional operand.
112   MVC_LOOP,
113
114   // Similar to MVC and MVC_LOOP, but for logic operations (AND, OR, XOR).
115   NC,
116   NC_LOOP,
117   OC,
118   OC_LOOP,
119   XC,
120   XC_LOOP,
121
122   // Use CLC to compare two blocks of memory, with the same comments
123   // as for MVC and MVC_LOOP.
124   CLC,
125   CLC_LOOP,
126
127   // Use an MVST-based sequence to implement stpcpy().
128   STPCPY,
129
130   // Use a CLST-based sequence to implement strcmp().  The two input operands
131   // are the addresses of the strings to compare.
132   STRCMP,
133
134   // Use an SRST-based sequence to search a block of memory.  The first
135   // operand is the end address, the second is the start, and the third
136   // is the character to search for.  CC is set to 1 on success and 2
137   // on failure.
138   SEARCH_STRING,
139
140   // Store the CC value in bits 29 and 28 of an integer.
141   IPM,
142
143   // Perform a serialization operation.  (BCR 15,0 or BCR 14,0.)
144   SERIALIZE,
145
146   // Wrappers around the inner loop of an 8- or 16-bit ATOMIC_SWAP or
147   // ATOMIC_LOAD_<op>.
148   //
149   // Operand 0: the address of the containing 32-bit-aligned field
150   // Operand 1: the second operand of <op>, in the high bits of an i32
151   //            for everything except ATOMIC_SWAPW
152   // Operand 2: how many bits to rotate the i32 left to bring the first
153   //            operand into the high bits
154   // Operand 3: the negative of operand 2, for rotating the other way
155   // Operand 4: the width of the field in bits (8 or 16)
156   ATOMIC_SWAPW = ISD::FIRST_TARGET_MEMORY_OPCODE,
157   ATOMIC_LOADW_ADD,
158   ATOMIC_LOADW_SUB,
159   ATOMIC_LOADW_AND,
160   ATOMIC_LOADW_OR,
161   ATOMIC_LOADW_XOR,
162   ATOMIC_LOADW_NAND,
163   ATOMIC_LOADW_MIN,
164   ATOMIC_LOADW_MAX,
165   ATOMIC_LOADW_UMIN,
166   ATOMIC_LOADW_UMAX,
167
168   // A wrapper around the inner loop of an ATOMIC_CMP_SWAP.
169   //
170   // Operand 0: the address of the containing 32-bit-aligned field
171   // Operand 1: the compare value, in the low bits of an i32
172   // Operand 2: the swap value, in the low bits of an i32
173   // Operand 3: how many bits to rotate the i32 left to bring the first
174   //            operand into the high bits
175   // Operand 4: the negative of operand 2, for rotating the other way
176   // Operand 5: the width of the field in bits (8 or 16)
177   ATOMIC_CMP_SWAPW,
178
179   // Prefetch from the second operand using the 4-bit control code in
180   // the first operand.  The code is 1 for a load prefetch and 2 for
181   // a store prefetch.
182   PREFETCH
183 };
184
185 // Return true if OPCODE is some kind of PC-relative address.
186 inline bool isPCREL(unsigned Opcode) {
187   return Opcode == PCREL_WRAPPER || Opcode == PCREL_OFFSET;
188 }
189 } // end namespace SystemZISD
190
191 namespace SystemZICMP {
192 // Describes whether an integer comparison needs to be signed or unsigned,
193 // or whether either type is OK.
194 enum {
195   Any,
196   UnsignedOnly,
197   SignedOnly
198 };
199 } // end namespace SystemZICMP
200
201 class SystemZSubtarget;
202 class SystemZTargetMachine;
203
204 class SystemZTargetLowering : public TargetLowering {
205 public:
206   explicit SystemZTargetLowering(const TargetMachine &TM,
207                                  const SystemZSubtarget &STI);
208
209   // Override TargetLowering.
210   MVT getScalarShiftAmountTy(EVT LHSTy) const override {
211     return MVT::i32;
212   }
213   EVT getSetCCResultType(LLVMContext &, EVT) const override;
214   bool isFMAFasterThanFMulAndFAdd(EVT VT) const override;
215   bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
216   bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const override;
217   bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AS,
218                                       unsigned Align,
219                                       bool *Fast) const override;
220   bool isTruncateFree(Type *, Type *) const override;
221   bool isTruncateFree(EVT, EVT) const override;
222   const char *getTargetNodeName(unsigned Opcode) const override;
223   std::pair<unsigned, const TargetRegisterClass *>
224   getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
225                                const std::string &Constraint,
226                                MVT VT) const override;
227   TargetLowering::ConstraintType
228     getConstraintType(const std::string &Constraint) const override;
229   TargetLowering::ConstraintWeight
230     getSingleConstraintMatchWeight(AsmOperandInfo &info,
231                                    const char *constraint) const override;
232   void LowerAsmOperandForConstraint(SDValue Op,
233                                     std::string &Constraint,
234                                     std::vector<SDValue> &Ops,
235                                     SelectionDAG &DAG) const override;
236
237   unsigned getInlineAsmMemConstraint(
238       const std::string &ConstraintCode) const override {
239     if (ConstraintCode.size() == 1) {
240       switch(ConstraintCode[0]) {
241       default:
242         break;
243       case 'Q':
244         return InlineAsm::Constraint_Q;
245       case 'R':
246         return InlineAsm::Constraint_R;
247       case 'S':
248         return InlineAsm::Constraint_S;
249       case 'T':
250         return InlineAsm::Constraint_T;
251       }
252     }
253     return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
254   }
255
256   MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
257                                                  MachineBasicBlock *BB) const
258     override;
259   SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
260   bool allowTruncateForTailCall(Type *, Type *) const override;
261   bool mayBeEmittedAsTailCall(CallInst *CI) const override;
262   SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
263                                bool isVarArg,
264                                const SmallVectorImpl<ISD::InputArg> &Ins,
265                                SDLoc DL, SelectionDAG &DAG,
266                                SmallVectorImpl<SDValue> &InVals) const override;
267   SDValue LowerCall(CallLoweringInfo &CLI,
268                     SmallVectorImpl<SDValue> &InVals) const override;
269
270   SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
271                       const SmallVectorImpl<ISD::OutputArg> &Outs,
272                       const SmallVectorImpl<SDValue> &OutVals,
273                       SDLoc DL, SelectionDAG &DAG) const override;
274   SDValue prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL,
275                                       SelectionDAG &DAG) const override;
276   SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
277
278 private:
279   const SystemZSubtarget &Subtarget;
280
281   // Implement LowerOperation for individual opcodes.
282   SDValue lowerSETCC(SDValue Op, SelectionDAG &DAG) const;
283   SDValue lowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
284   SDValue lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
285   SDValue lowerGlobalAddress(GlobalAddressSDNode *Node,
286                              SelectionDAG &DAG) const;
287   SDValue lowerTLSGetOffset(GlobalAddressSDNode *Node,
288                             SelectionDAG &DAG, unsigned Opcode,
289                             SDValue GOTOffset) const;
290   SDValue lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
291                                 SelectionDAG &DAG) const;
292   SDValue lowerBlockAddress(BlockAddressSDNode *Node,
293                             SelectionDAG &DAG) const;
294   SDValue lowerJumpTable(JumpTableSDNode *JT, SelectionDAG &DAG) const;
295   SDValue lowerConstantPool(ConstantPoolSDNode *CP, SelectionDAG &DAG) const;
296   SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
297   SDValue lowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
298   SDValue lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
299   SDValue lowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
300   SDValue lowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
301   SDValue lowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
302   SDValue lowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
303   SDValue lowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
304   SDValue lowerOR(SDValue Op, SelectionDAG &DAG) const;
305   SDValue lowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG) const;
306   SDValue lowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) const;
307   SDValue lowerATOMIC_LOAD_OP(SDValue Op, SelectionDAG &DAG,
308                               unsigned Opcode) const;
309   SDValue lowerATOMIC_LOAD_SUB(SDValue Op, SelectionDAG &DAG) const;
310   SDValue lowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const;
311   SDValue lowerLOAD_SEQUENCE_POINT(SDValue Op, SelectionDAG &DAG) const;
312   SDValue lowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const;
313   SDValue lowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
314   SDValue lowerPREFETCH(SDValue Op, SelectionDAG &DAG) const;
315
316   // If the last instruction before MBBI in MBB was some form of COMPARE,
317   // try to replace it with a COMPARE AND BRANCH just before MBBI.
318   // CCMask and Target are the BRC-like operands for the branch.
319   // Return true if the change was made.
320   bool convertPrevCompareToBranch(MachineBasicBlock *MBB,
321                                   MachineBasicBlock::iterator MBBI,
322                                   unsigned CCMask,
323                                   MachineBasicBlock *Target) const;
324
325   // Implement EmitInstrWithCustomInserter for individual operation types.
326   MachineBasicBlock *emitSelect(MachineInstr *MI,
327                                 MachineBasicBlock *BB) const;
328   MachineBasicBlock *emitCondStore(MachineInstr *MI,
329                                    MachineBasicBlock *BB,
330                                    unsigned StoreOpcode, unsigned STOCOpcode,
331                                    bool Invert) const;
332   MachineBasicBlock *emitExt128(MachineInstr *MI,
333                                 MachineBasicBlock *MBB,
334                                 bool ClearEven, unsigned SubReg) const;
335   MachineBasicBlock *emitAtomicLoadBinary(MachineInstr *MI,
336                                           MachineBasicBlock *BB,
337                                           unsigned BinOpcode, unsigned BitSize,
338                                           bool Invert = false) const;
339   MachineBasicBlock *emitAtomicLoadMinMax(MachineInstr *MI,
340                                           MachineBasicBlock *MBB,
341                                           unsigned CompareOpcode,
342                                           unsigned KeepOldMask,
343                                           unsigned BitSize) const;
344   MachineBasicBlock *emitAtomicCmpSwapW(MachineInstr *MI,
345                                         MachineBasicBlock *BB) const;
346   MachineBasicBlock *emitMemMemWrapper(MachineInstr *MI,
347                                        MachineBasicBlock *BB,
348                                        unsigned Opcode) const;
349   MachineBasicBlock *emitStringWrapper(MachineInstr *MI,
350                                        MachineBasicBlock *BB,
351                                        unsigned Opcode) const;
352 };
353 } // end namespace llvm
354
355 #endif