blob: 4cbb30da8b57ad6489b8a6906706037d1dcddd2f [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- 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_TARGET_SystemZ_ISELLOWERING_H
16#define LLVM_TARGET_SystemZ_ISELLOWERING_H
17
18#include "SystemZ.h"
Richard Sandiford0fb90ab2013-05-28 10:41:11 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000020#include "llvm/CodeGen/SelectionDAG.h"
21#include "llvm/Target/TargetLowering.h"
22
23namespace llvm {
24namespace 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,
Richard Sandiford709bda62013-08-19 12:42:31 +000035 SIBCALL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000036
37 // Wraps a TargetGlobalAddress that should be loaded using PC-relative
38 // accesses (LARL). Operand 0 is the address.
39 PCREL_WRAPPER,
40
Richard Sandiford54b36912013-09-27 15:14:04 +000041 // Used in cases where an offset is applied to a TargetGlobalAddress.
42 // Operand 0 is the full TargetGlobalAddress and operand 1 is a
43 // PCREL_WRAPPER for an anchor point. This is used so that we can
44 // cheaply refer to either the full address or the anchor point
45 // as a register base.
46 PCREL_OFFSET,
47
Richard Sandiford5bc670b2013-09-06 11:51:39 +000048 // Integer comparisons. There are three operands: the two values
49 // to compare, and an integer of type SystemZICMP.
50 ICMP,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000051
Richard Sandiford5bc670b2013-09-06 11:51:39 +000052 // Floating-point comparisons. The two operands are the values to compare.
53 FCMP,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000054
Richard Sandiford35b9be22013-08-28 10:31:43 +000055 // Test under mask. The first operand is ANDed with the second operand
Richard Sandiforda9eb9972013-09-10 10:20:32 +000056 // and the condition codes are set on the result. The third operand is
57 // a boolean that is true if the condition codes need to distinguish
58 // between CCMASK_TM_MIXED_MSB_0 and CCMASK_TM_MIXED_MSB_1 (which the
59 // register forms do but the memory forms don't).
Richard Sandiford35b9be22013-08-28 10:31:43 +000060 TM,
61
Ulrich Weigand5f613df2013-05-06 16:15:19 +000062 // Branches if a condition is true. Operand 0 is the chain operand;
63 // operand 1 is the 4-bit condition-code mask, with bit N in
64 // big-endian order meaning "branch if CC=N"; operand 2 is the
65 // target block and operand 3 is the flag operand.
66 BR_CCMASK,
67
68 // Selects between operand 0 and operand 1. Operand 2 is the
69 // mask of condition-code values for which operand 0 should be
70 // chosen over operand 1; it has the same form as BR_CCMASK.
71 // Operand 3 is the flag operand.
72 SELECT_CCMASK,
73
74 // Evaluates to the gap between the stack pointer and the
75 // base of the dynamically-allocatable area.
76 ADJDYNALLOC,
77
78 // Extracts the value of a 32-bit access register. Operand 0 is
79 // the number of the register.
80 EXTRACT_ACCESS,
81
82 // Wrappers around the ISD opcodes of the same name. The output and
83 // first input operands are GR128s. The trailing numbers are the
84 // widths of the second operand in bits.
85 UMUL_LOHI64,
Richard Sandiforde6e78852013-07-02 15:40:22 +000086 SDIVREM32,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000087 SDIVREM64,
88 UDIVREM32,
89 UDIVREM64,
90
Richard Sandiford5e318f02013-08-27 09:54:29 +000091 // Use a series of MVCs to copy bytes from one memory location to another.
92 // The operands are:
93 // - the target address
94 // - the source address
95 // - the constant length
96 //
Richard Sandifordd131ff82013-07-08 09:35:23 +000097 // This isn't a memory opcode because we'd need to attach two
98 // MachineMemOperands rather than one.
99 MVC,
100
Richard Sandiford5e318f02013-08-27 09:54:29 +0000101 // Like MVC, but implemented as a loop that handles X*256 bytes
102 // followed by straight-line code to handle the rest (if any).
103 // The value of X is passed as an additional operand.
104 MVC_LOOP,
105
Richard Sandiford178273a2013-09-05 10:36:45 +0000106 // Similar to MVC and MVC_LOOP, but for logic operations (AND, OR, XOR).
107 NC,
108 NC_LOOP,
109 OC,
110 OC_LOOP,
111 XC,
112 XC_LOOP,
113
Richard Sandiford761703a2013-08-12 10:17:33 +0000114 // Use CLC to compare two blocks of memory, with the same comments
Richard Sandiford5e318f02013-08-27 09:54:29 +0000115 // as for MVC and MVC_LOOP.
Richard Sandiford761703a2013-08-12 10:17:33 +0000116 CLC,
Richard Sandiford5e318f02013-08-27 09:54:29 +0000117 CLC_LOOP,
Richard Sandiford761703a2013-08-12 10:17:33 +0000118
Richard Sandifordbb83a502013-08-16 11:29:37 +0000119 // Use an MVST-based sequence to implement stpcpy().
120 STPCPY,
121
Richard Sandifordca232712013-08-16 11:21:54 +0000122 // Use a CLST-based sequence to implement strcmp(). The two input operands
123 // are the addresses of the strings to compare.
124 STRCMP,
125
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000126 // Use an SRST-based sequence to search a block of memory. The first
127 // operand is the end address, the second is the start, and the third
128 // is the character to search for. CC is set to 1 on success and 2
129 // on failure.
130 SEARCH_STRING,
131
Richard Sandiford564681c2013-08-12 10:28:10 +0000132 // Store the CC value in bits 29 and 28 of an integer.
133 IPM,
134
Richard Sandiford9afe6132013-12-10 10:36:34 +0000135 // Perform a serialization operation. (BCR 15,0 or BCR 14,0.)
136 SERIALIZE,
137
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000138 // Wrappers around the inner loop of an 8- or 16-bit ATOMIC_SWAP or
139 // ATOMIC_LOAD_<op>.
140 //
141 // Operand 0: the address of the containing 32-bit-aligned field
142 // Operand 1: the second operand of <op>, in the high bits of an i32
143 // for everything except ATOMIC_SWAPW
144 // Operand 2: how many bits to rotate the i32 left to bring the first
145 // operand into the high bits
146 // Operand 3: the negative of operand 2, for rotating the other way
147 // Operand 4: the width of the field in bits (8 or 16)
148 ATOMIC_SWAPW = ISD::FIRST_TARGET_MEMORY_OPCODE,
149 ATOMIC_LOADW_ADD,
150 ATOMIC_LOADW_SUB,
151 ATOMIC_LOADW_AND,
152 ATOMIC_LOADW_OR,
153 ATOMIC_LOADW_XOR,
154 ATOMIC_LOADW_NAND,
155 ATOMIC_LOADW_MIN,
156 ATOMIC_LOADW_MAX,
157 ATOMIC_LOADW_UMIN,
158 ATOMIC_LOADW_UMAX,
159
160 // A wrapper around the inner loop of an ATOMIC_CMP_SWAP.
161 //
162 // Operand 0: the address of the containing 32-bit-aligned field
163 // Operand 1: the compare value, in the low bits of an i32
164 // Operand 2: the swap value, in the low bits of an i32
165 // Operand 3: how many bits to rotate the i32 left to bring the first
166 // operand into the high bits
167 // Operand 4: the negative of operand 2, for rotating the other way
168 // Operand 5: the width of the field in bits (8 or 16)
Richard Sandiford03481332013-08-23 11:36:42 +0000169 ATOMIC_CMP_SWAPW,
170
171 // Prefetch from the second operand using the 4-bit control code in
172 // the first operand. The code is 1 for a load prefetch and 2 for
173 // a store prefetch.
174 PREFETCH
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175 };
Richard Sandiford54b36912013-09-27 15:14:04 +0000176
177 // Return true if OPCODE is some kind of PC-relative address.
178 inline bool isPCREL(unsigned Opcode) {
179 return Opcode == PCREL_WRAPPER || Opcode == PCREL_OFFSET;
180 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181}
182
Richard Sandiford5bc670b2013-09-06 11:51:39 +0000183namespace SystemZICMP {
184 // Describes whether an integer comparison needs to be signed or unsigned,
185 // or whether either type is OK.
186 enum {
187 Any,
188 UnsignedOnly,
189 SignedOnly
190 };
191}
192
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193class SystemZSubtarget;
194class SystemZTargetMachine;
195
196class SystemZTargetLowering : public TargetLowering {
197public:
198 explicit SystemZTargetLowering(SystemZTargetMachine &TM);
199
200 // Override TargetLowering.
201 virtual MVT getScalarShiftAmountTy(EVT LHSTy) const LLVM_OVERRIDE {
202 return MVT::i32;
203 }
Richard Sandifordabc010b2013-11-06 12:16:02 +0000204 virtual EVT getSetCCResultType(LLVMContext &, EVT) const LLVM_OVERRIDE;
Stephen Lin73de7bf2013-07-09 18:16:56 +0000205 virtual bool isFMAFasterThanFMulAndFAdd(EVT VT) const LLVM_OVERRIDE;
Richard Sandiford791bea42013-07-31 12:58:26 +0000206 virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const LLVM_OVERRIDE;
207 virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const
208 LLVM_OVERRIDE;
209 virtual bool allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const
210 LLVM_OVERRIDE;
Richard Sandiford709bda62013-08-19 12:42:31 +0000211 virtual bool isTruncateFree(Type *, Type *) const LLVM_OVERRIDE;
212 virtual bool isTruncateFree(EVT, EVT) const LLVM_OVERRIDE;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213 virtual const char *getTargetNodeName(unsigned Opcode) const LLVM_OVERRIDE;
214 virtual std::pair<unsigned, const TargetRegisterClass *>
215 getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +0000216 MVT VT) const LLVM_OVERRIDE;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000217 virtual TargetLowering::ConstraintType
218 getConstraintType(const std::string &Constraint) const LLVM_OVERRIDE;
219 virtual TargetLowering::ConstraintWeight
220 getSingleConstraintMatchWeight(AsmOperandInfo &info,
221 const char *constraint) const LLVM_OVERRIDE;
222 virtual void
223 LowerAsmOperandForConstraint(SDValue Op,
224 std::string &Constraint,
225 std::vector<SDValue> &Ops,
226 SelectionDAG &DAG) const LLVM_OVERRIDE;
227 virtual MachineBasicBlock *
228 EmitInstrWithCustomInserter(MachineInstr *MI,
229 MachineBasicBlock *BB) const LLVM_OVERRIDE;
230 virtual SDValue LowerOperation(SDValue Op,
231 SelectionDAG &DAG) const LLVM_OVERRIDE;
Richard Sandiford709bda62013-08-19 12:42:31 +0000232 virtual bool allowTruncateForTailCall(Type *, Type *) const LLVM_OVERRIDE;
233 virtual bool mayBeEmittedAsTailCall(CallInst *CI) const LLVM_OVERRIDE;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000234 virtual SDValue
235 LowerFormalArguments(SDValue Chain,
236 CallingConv::ID CallConv, bool isVarArg,
237 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000238 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 SmallVectorImpl<SDValue> &InVals) const LLVM_OVERRIDE;
240 virtual SDValue
241 LowerCall(CallLoweringInfo &CLI,
242 SmallVectorImpl<SDValue> &InVals) const LLVM_OVERRIDE;
243
244 virtual SDValue
245 LowerReturn(SDValue Chain,
246 CallingConv::ID CallConv, bool IsVarArg,
247 const SmallVectorImpl<ISD::OutputArg> &Outs,
248 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000249 SDLoc DL, SelectionDAG &DAG) const LLVM_OVERRIDE;
Richard Sandiford9afe6132013-12-10 10:36:34 +0000250 virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL,
251 SelectionDAG &DAG) const
252 LLVM_OVERRIDE;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000253
254private:
255 const SystemZSubtarget &Subtarget;
256 const SystemZTargetMachine &TM;
257
258 // Implement LowerOperation for individual opcodes.
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000259 SDValue lowerSETCC(SDValue Op, SelectionDAG &DAG) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000260 SDValue lowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
261 SDValue lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
262 SDValue lowerGlobalAddress(GlobalAddressSDNode *Node,
263 SelectionDAG &DAG) const;
264 SDValue lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
265 SelectionDAG &DAG) const;
266 SDValue lowerBlockAddress(BlockAddressSDNode *Node,
267 SelectionDAG &DAG) const;
268 SDValue lowerJumpTable(JumpTableSDNode *JT, SelectionDAG &DAG) const;
269 SDValue lowerConstantPool(ConstantPoolSDNode *CP, SelectionDAG &DAG) const;
270 SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
271 SDValue lowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
272 SDValue lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
Richard Sandiford7d86e472013-08-21 09:34:56 +0000273 SDValue lowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000274 SDValue lowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
275 SDValue lowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
276 SDValue lowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
277 SDValue lowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
278 SDValue lowerOR(SDValue Op, SelectionDAG &DAG) const;
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000279 SDValue lowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG) const;
280 SDValue lowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) const;
281 SDValue lowerATOMIC_LOAD_OP(SDValue Op, SelectionDAG &DAG,
282 unsigned Opcode) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000283 SDValue lowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const;
Richard Sandiford9afe6132013-12-10 10:36:34 +0000284 SDValue lowerLOAD_SEQUENCE_POINT(SDValue Op, SelectionDAG &DAG) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000285 SDValue lowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const;
286 SDValue lowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
Richard Sandiford03481332013-08-23 11:36:42 +0000287 SDValue lowerPREFETCH(SDValue Op, SelectionDAG &DAG) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000288
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000289 // If the last instruction before MBBI in MBB was some form of COMPARE,
290 // try to replace it with a COMPARE AND BRANCH just before MBBI.
291 // CCMask and Target are the BRC-like operands for the branch.
292 // Return true if the change was made.
293 bool convertPrevCompareToBranch(MachineBasicBlock *MBB,
294 MachineBasicBlock::iterator MBBI,
295 unsigned CCMask,
296 MachineBasicBlock *Target) const;
297
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000298 // Implement EmitInstrWithCustomInserter for individual operation types.
299 MachineBasicBlock *emitSelect(MachineInstr *MI,
300 MachineBasicBlock *BB) const;
Richard Sandifordb86a8342013-06-27 09:27:40 +0000301 MachineBasicBlock *emitCondStore(MachineInstr *MI,
302 MachineBasicBlock *BB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +0000303 unsigned StoreOpcode, unsigned STOCOpcode,
304 bool Invert) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305 MachineBasicBlock *emitExt128(MachineInstr *MI,
306 MachineBasicBlock *MBB,
307 bool ClearEven, unsigned SubReg) const;
308 MachineBasicBlock *emitAtomicLoadBinary(MachineInstr *MI,
309 MachineBasicBlock *BB,
310 unsigned BinOpcode, unsigned BitSize,
311 bool Invert = false) const;
312 MachineBasicBlock *emitAtomicLoadMinMax(MachineInstr *MI,
313 MachineBasicBlock *MBB,
314 unsigned CompareOpcode,
315 unsigned KeepOldMask,
316 unsigned BitSize) const;
317 MachineBasicBlock *emitAtomicCmpSwapW(MachineInstr *MI,
318 MachineBasicBlock *BB) const;
Richard Sandiford564681c2013-08-12 10:28:10 +0000319 MachineBasicBlock *emitMemMemWrapper(MachineInstr *MI,
320 MachineBasicBlock *BB,
321 unsigned Opcode) const;
Richard Sandifordca232712013-08-16 11:21:54 +0000322 MachineBasicBlock *emitStringWrapper(MachineInstr *MI,
323 MachineBasicBlock *BB,
324 unsigned Opcode) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000325};
326} // end namespace llvm
327
328#endif // LLVM_TARGET_SystemZ_ISELLOWERING_H