1 //===-- SystemZISelLowering.h - SystemZ DAG lowering interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that SystemZ uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZISELLOWERING_H
16 #define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZISELLOWERING_H
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/Target/TargetLowering.h"
24 namespace SystemZISD {
25 enum NodeType : unsigned {
26 FIRST_NUMBER = ISD::BUILTIN_OP_END,
28 // Return with a flag operand. Operand 0 is the chain operand.
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.
37 // TLS calls. Like regular calls, except operand 1 is the TLS symbol.
38 // (The call target is implicitly __tls_get_offset.)
42 // Wraps a TargetGlobalAddress that should be loaded using PC-relative
43 // accesses (LARL). Operand 0 is the address.
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.
56 // Integer comparisons. There are three operands: the two values
57 // to compare, and an integer of type SystemZICMP.
60 // Floating-point comparisons. The two operands are the values to compare.
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).
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.
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.
82 // Evaluates to the gap between the stack pointer and the
83 // base of the dynamically-allocatable area.
86 // Count number of bits set in operand 0 per byte.
89 // Wrappers around the ISD opcodes of the same name. The output and
90 // first input operands are GR128s. The trailing numbers are the
91 // widths of the second operand in bits.
98 // Use a series of MVCs to copy bytes from one memory location to another.
100 // - the target address
101 // - the source address
102 // - the constant length
104 // This isn't a memory opcode because we'd need to attach two
105 // MachineMemOperands rather than one.
108 // Like MVC, but implemented as a loop that handles X*256 bytes
109 // followed by straight-line code to handle the rest (if any).
110 // The value of X is passed as an additional operand.
113 // Similar to MVC and MVC_LOOP, but for logic operations (AND, OR, XOR).
121 // Use CLC to compare two blocks of memory, with the same comments
122 // as for MVC and MVC_LOOP.
126 // Use an MVST-based sequence to implement stpcpy().
129 // Use a CLST-based sequence to implement strcmp(). The two input operands
130 // are the addresses of the strings to compare.
133 // Use an SRST-based sequence to search a block of memory. The first
134 // operand is the end address, the second is the start, and the third
135 // is the character to search for. CC is set to 1 on success and 2
139 // Store the CC value in bits 29 and 28 of an integer.
142 // Compiler barrier only; generate a no-op.
145 // Transaction begin. The first operand is the chain, the second
146 // the TDB pointer, and the third the immediate control field.
147 // Returns chain and glue.
151 // Transaction end. Just the chain operand. Returns chain and glue.
154 // Create a vector constant by filling byte N of the result with bit
155 // 15-N of the single operand.
158 // Create a vector constant by replicating an element-sized RISBG-style mask.
159 // The first operand specifies the starting set bit and the second operand
160 // specifies the ending set bit. Both operands count from the MSB of the
164 // Replicate a GPR scalar value into all elements of a vector.
167 // Create a vector from two i64 GPRs.
170 // Replicate one element of a vector into all elements. The first operand
171 // is the vector and the second is the index of the element to replicate.
174 // Interleave elements from the high half of operand 0 and the high half
178 // Likewise for the low halves.
181 // Concatenate the vectors in the first two operands, shift them left
182 // by the third operand, and take the first half of the result.
185 // Take one element of the first v2i64 operand and the one element of
186 // the second v2i64 operand and concatenate them to form a v2i64 result.
187 // The third operand is a 4-bit value of the form 0A0B, where A and B
188 // are the element selectors for the first operand and second operands
192 // Perform a general vector permute on vector operands 0 and 1.
193 // Each byte of operand 2 controls the corresponding byte of the result,
194 // in the same way as a byte-level VECTOR_SHUFFLE mask.
197 // Pack vector operands 0 and 1 into a single vector with half-sized elements.
200 // Likewise, but saturate the result and set CC. PACKS_CC does signed
201 // saturation and PACKLS_CC does unsigned saturation.
205 // Unpack the first half of vector operand 0 into double-sized elements.
206 // UNPACK_HIGH sign-extends and UNPACKL_HIGH zero-extends.
210 // Likewise for the second half.
214 // Shift each element of vector operand 0 by the number of bits specified
215 // by scalar operand 1.
220 // For each element of the output type, sum across all sub-elements of
221 // operand 0 belonging to the corresponding element, and add in the
222 // rightmost sub-element of the corresponding element of operand 1.
225 // Compare integer vector operands 0 and 1 to produce the usual 0/-1
226 // vector result. VICMPE is for equality, VICMPH for "signed greater than"
227 // and VICMPHL for "unsigned greater than".
232 // Likewise, but also set the condition codes on the result.
237 // Compare floating-point vector operands 0 and 1 to preoduce the usual 0/-1
238 // vector result. VFCMPE is for "ordered and equal", VFCMPH for "ordered and
239 // greater than" and VFCMPHE for "ordered and greater than or equal to".
244 // Likewise, but also set the condition codes on the result.
249 // Test floating-point data class for vectors.
252 // Extend the even f32 elements of vector operand 0 to produce a vector
256 // Round the f64 elements of vector operand 0 to f32s and store them in the
257 // even elements of the result.
260 // AND the two vector operands together and set CC based on the result.
263 // String operations that set CC as a side-effect.
276 // Operand 0: the value to test
277 // Operand 1: the bit mask
280 // Wrappers around the inner loop of an 8- or 16-bit ATOMIC_SWAP or
283 // Operand 0: the address of the containing 32-bit-aligned field
284 // Operand 1: the second operand of <op>, in the high bits of an i32
285 // for everything except ATOMIC_SWAPW
286 // Operand 2: how many bits to rotate the i32 left to bring the first
287 // operand into the high bits
288 // Operand 3: the negative of operand 2, for rotating the other way
289 // Operand 4: the width of the field in bits (8 or 16)
290 ATOMIC_SWAPW = ISD::FIRST_TARGET_MEMORY_OPCODE,
302 // A wrapper around the inner loop of an ATOMIC_CMP_SWAP.
304 // Operand 0: the address of the containing 32-bit-aligned field
305 // Operand 1: the compare value, in the low bits of an i32
306 // Operand 2: the swap value, in the low bits of an i32
307 // Operand 3: how many bits to rotate the i32 left to bring the first
308 // operand into the high bits
309 // Operand 4: the negative of operand 2, for rotating the other way
310 // Operand 5: the width of the field in bits (8 or 16)
313 // Byte swapping load.
315 // Operand 0: the address to load from
316 // Operand 1: the type of load (i16, i32, i64)
319 // Byte swapping store.
321 // Operand 0: the value to store
322 // Operand 1: the address to store to
323 // Operand 2: the type of store (i16, i32, i64)
326 // Prefetch from the second operand using the 4-bit control code in
327 // the first operand. The code is 1 for a load prefetch and 2 for
332 // Return true if OPCODE is some kind of PC-relative address.
333 inline bool isPCREL(unsigned Opcode) {
334 return Opcode == PCREL_WRAPPER || Opcode == PCREL_OFFSET;
336 } // end namespace SystemZISD
338 namespace SystemZICMP {
339 // Describes whether an integer comparison needs to be signed or unsigned,
340 // or whether either type is OK.
346 } // end namespace SystemZICMP
348 class SystemZSubtarget;
349 class SystemZTargetMachine;
351 class SystemZTargetLowering : public TargetLowering {
353 explicit SystemZTargetLowering(const TargetMachine &TM,
354 const SystemZSubtarget &STI);
356 // Override TargetLowering.
357 MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override {
360 MVT getVectorIdxTy(const DataLayout &DL) const override {
361 // Only the lower 12 bits of an element index are used, so we don't
362 // want to clobber the upper 32 bits of a GPR unnecessarily.
365 TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(EVT VT)
367 // Widen subvectors to the full width rather than promoting integer
368 // elements. This is better because:
370 // (a) it means that we can handle the ABI for passing and returning
371 // sub-128 vectors without having to handle them as legal types.
373 // (b) we don't have instructions to extend on load and truncate on store,
374 // so promoting the integers is less efficient.
376 // (c) there are no multiplication instructions for the widest integer
378 if (VT.getScalarSizeInBits() % 8 == 0)
379 return TypeWidenVector;
380 return TargetLoweringBase::getPreferredVectorAction(VT);
382 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &,
384 bool isFMAFasterThanFMulAndFAdd(EVT VT) const override;
385 bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
386 bool isLegalICmpImmediate(int64_t Imm) const override;
387 bool isLegalAddImmediate(int64_t Imm) const override;
388 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
389 unsigned AS) const override;
390 bool isFoldableMemAccessOffset(Instruction *I, int64_t Offset) const override;
391 bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AS,
393 bool *Fast) const override;
394 bool isTruncateFree(Type *, Type *) const override;
395 bool isTruncateFree(EVT, EVT) const override;
396 const char *getTargetNodeName(unsigned Opcode) const override;
397 std::pair<unsigned, const TargetRegisterClass *>
398 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
399 StringRef Constraint, MVT VT) const override;
400 TargetLowering::ConstraintType
401 getConstraintType(StringRef Constraint) const override;
402 TargetLowering::ConstraintWeight
403 getSingleConstraintMatchWeight(AsmOperandInfo &info,
404 const char *constraint) const override;
405 void LowerAsmOperandForConstraint(SDValue Op,
406 std::string &Constraint,
407 std::vector<SDValue> &Ops,
408 SelectionDAG &DAG) const override;
410 unsigned getInlineAsmMemConstraint(StringRef ConstraintCode) const override {
411 if (ConstraintCode.size() == 1) {
412 switch(ConstraintCode[0]) {
416 return InlineAsm::Constraint_Q;
418 return InlineAsm::Constraint_R;
420 return InlineAsm::Constraint_S;
422 return InlineAsm::Constraint_T;
425 return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
428 /// If a physical register, this returns the register that receives the
429 /// exception address on entry to an EH pad.
431 getExceptionPointerRegister(const Constant *PersonalityFn) const override {
435 /// If a physical register, this returns the register that receives the
436 /// exception typeid on entry to a landing pad.
438 getExceptionSelectorRegister(const Constant *PersonalityFn) const override {
442 /// Override to support customized stack guard loading.
443 bool useLoadStackGuardNode() const override {
446 void insertSSPDeclarations(Module &M) const override {
450 EmitInstrWithCustomInserter(MachineInstr &MI,
451 MachineBasicBlock *BB) const override;
452 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
453 bool allowTruncateForTailCall(Type *, Type *) const override;
454 bool mayBeEmittedAsTailCall(const CallInst *CI) const override;
455 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
457 const SmallVectorImpl<ISD::InputArg> &Ins,
458 const SDLoc &DL, SelectionDAG &DAG,
459 SmallVectorImpl<SDValue> &InVals) const override;
460 SDValue LowerCall(CallLoweringInfo &CLI,
461 SmallVectorImpl<SDValue> &InVals) const override;
463 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
465 const SmallVectorImpl<ISD::OutputArg> &Outs,
466 LLVMContext &Context) const override;
467 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
468 const SmallVectorImpl<ISD::OutputArg> &Outs,
469 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
470 SelectionDAG &DAG) const override;
471 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
473 ISD::NodeType getExtendForAtomicOps() const override {
474 return ISD::ANY_EXTEND;
477 bool supportSwiftError() const override {
482 const SystemZSubtarget &Subtarget;
484 // Implement LowerOperation for individual opcodes.
485 SDValue lowerSETCC(SDValue Op, SelectionDAG &DAG) const;
486 SDValue lowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
487 SDValue lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
488 SDValue lowerGlobalAddress(GlobalAddressSDNode *Node,
489 SelectionDAG &DAG) const;
490 SDValue lowerTLSGetOffset(GlobalAddressSDNode *Node,
491 SelectionDAG &DAG, unsigned Opcode,
492 SDValue GOTOffset) const;
493 SDValue lowerThreadPointer(const SDLoc &DL, SelectionDAG &DAG) const;
494 SDValue lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
495 SelectionDAG &DAG) const;
496 SDValue lowerBlockAddress(BlockAddressSDNode *Node,
497 SelectionDAG &DAG) const;
498 SDValue lowerJumpTable(JumpTableSDNode *JT, SelectionDAG &DAG) const;
499 SDValue lowerConstantPool(ConstantPoolSDNode *CP, SelectionDAG &DAG) const;
500 SDValue lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
501 SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
502 SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
503 SDValue lowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
504 SDValue lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
505 SDValue lowerGET_DYNAMIC_AREA_OFFSET(SDValue Op, SelectionDAG &DAG) const;
506 SDValue lowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
507 SDValue lowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
508 SDValue lowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
509 SDValue lowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
510 SDValue lowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
511 SDValue lowerOR(SDValue Op, SelectionDAG &DAG) const;
512 SDValue lowerCTPOP(SDValue Op, SelectionDAG &DAG) const;
513 SDValue lowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG) const;
514 SDValue lowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG) const;
515 SDValue lowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) const;
516 SDValue lowerATOMIC_LOAD_OP(SDValue Op, SelectionDAG &DAG,
517 unsigned Opcode) const;
518 SDValue lowerATOMIC_LOAD_SUB(SDValue Op, SelectionDAG &DAG) const;
519 SDValue lowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const;
520 SDValue lowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const;
521 SDValue lowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
522 SDValue lowerPREFETCH(SDValue Op, SelectionDAG &DAG) const;
523 SDValue lowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const;
524 SDValue lowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
525 SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
526 SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
527 SDValue lowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const;
528 SDValue lowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
529 SDValue lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
530 SDValue lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
531 unsigned UnpackHigh) const;
532 SDValue lowerShift(SDValue Op, SelectionDAG &DAG, unsigned ByScalar) const;
534 bool canTreatAsByteVector(EVT VT) const;
535 SDValue combineExtract(const SDLoc &DL, EVT ElemVT, EVT VecVT, SDValue OrigOp,
536 unsigned Index, DAGCombinerInfo &DCI,
538 SDValue combineTruncateExtract(const SDLoc &DL, EVT TruncVT, SDValue Op,
539 DAGCombinerInfo &DCI) const;
540 SDValue combineSIGN_EXTEND(SDNode *N, DAGCombinerInfo &DCI) const;
541 SDValue combineMERGE(SDNode *N, DAGCombinerInfo &DCI) const;
542 SDValue combineSTORE(SDNode *N, DAGCombinerInfo &DCI) const;
543 SDValue combineEXTRACT_VECTOR_ELT(SDNode *N, DAGCombinerInfo &DCI) const;
544 SDValue combineJOIN_DWORDS(SDNode *N, DAGCombinerInfo &DCI) const;
545 SDValue combineFP_ROUND(SDNode *N, DAGCombinerInfo &DCI) const;
546 SDValue combineBSWAP(SDNode *N, DAGCombinerInfo &DCI) const;
547 SDValue combineSHIFTROT(SDNode *N, DAGCombinerInfo &DCI) const;
549 // If the last instruction before MBBI in MBB was some form of COMPARE,
550 // try to replace it with a COMPARE AND BRANCH just before MBBI.
551 // CCMask and Target are the BRC-like operands for the branch.
552 // Return true if the change was made.
553 bool convertPrevCompareToBranch(MachineBasicBlock *MBB,
554 MachineBasicBlock::iterator MBBI,
556 MachineBasicBlock *Target) const;
558 // Implement EmitInstrWithCustomInserter for individual operation types.
559 MachineBasicBlock *emitSelect(MachineInstr &MI, MachineBasicBlock *BB,
560 unsigned LOCROpcode) const;
561 MachineBasicBlock *emitCondStore(MachineInstr &MI, MachineBasicBlock *BB,
562 unsigned StoreOpcode, unsigned STOCOpcode,
564 MachineBasicBlock *emitExt128(MachineInstr &MI, MachineBasicBlock *MBB,
565 bool ClearEven, unsigned SubReg) const;
566 MachineBasicBlock *emitAtomicLoadBinary(MachineInstr &MI,
567 MachineBasicBlock *BB,
568 unsigned BinOpcode, unsigned BitSize,
569 bool Invert = false) const;
570 MachineBasicBlock *emitAtomicLoadMinMax(MachineInstr &MI,
571 MachineBasicBlock *MBB,
572 unsigned CompareOpcode,
573 unsigned KeepOldMask,
574 unsigned BitSize) const;
575 MachineBasicBlock *emitAtomicCmpSwapW(MachineInstr &MI,
576 MachineBasicBlock *BB) const;
577 MachineBasicBlock *emitMemMemWrapper(MachineInstr &MI, MachineBasicBlock *BB,
578 unsigned Opcode) const;
579 MachineBasicBlock *emitStringWrapper(MachineInstr &MI, MachineBasicBlock *BB,
580 unsigned Opcode) const;
581 MachineBasicBlock *emitTransactionBegin(MachineInstr &MI,
582 MachineBasicBlock *MBB,
583 unsigned Opcode, bool NoFloat) const;
584 MachineBasicBlock *emitLoadAndTestCmp0(MachineInstr &MI,
585 MachineBasicBlock *MBB,
586 unsigned Opcode) const;
588 const TargetRegisterClass *getRepRegClassFor(MVT VT) const override;
590 } // end namespace llvm