LLVM 24.0.0git
SystemZISelLowering.cpp
Go to the documentation of this file.
1//===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SystemZTargetLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SystemZISelLowering.h"
14#include "SystemZCallingConv.h"
17#include "llvm/ADT/SmallSet.h"
22#include "llvm/IR/GlobalAlias.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/IntrinsicsS390.h"
26#include "llvm/IR/Module.h"
32#include <cctype>
33#include <optional>
34
35using namespace llvm;
36
37#define DEBUG_TYPE "systemz-lower"
38
39// Temporarily let this be disabled by default until all known problems
40// related to argument extensions are fixed.
42 "argext-abi-check", cl::init(false),
43 cl::desc("Verify that narrow int args are properly extended per the "
44 "SystemZ ABI."));
45
46namespace {
47// Represents information about a comparison.
48struct Comparison {
49 Comparison(SDValue Op0In, SDValue Op1In, SDValue ChainIn)
50 : Op0(Op0In), Op1(Op1In), Chain(ChainIn),
51 Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
52
53 // The operands to the comparison.
54 SDValue Op0, Op1;
55
56 // Chain if this is a strict floating-point comparison.
57 SDValue Chain;
58
59 // The opcode that should be used to compare Op0 and Op1.
60 unsigned Opcode;
61
62 // A SystemZICMP value. Only used for integer comparisons.
63 unsigned ICmpType;
64
65 // The mask of CC values that Opcode can produce.
66 unsigned CCValid;
67
68 // The mask of CC values for which the original condition is true.
69 unsigned CCMask;
70};
71} // end anonymous namespace
72
73// Classify VT as either 32 or 64 bit.
74static bool is32Bit(EVT VT) {
75 switch (VT.getSimpleVT().SimpleTy) {
76 case MVT::i32:
77 return true;
78 case MVT::i64:
79 return false;
80 default:
81 llvm_unreachable("Unsupported type");
82 }
83}
84
85// Return a version of MachineOperand that can be safely used before the
86// final use.
88 if (Op.isReg())
89 Op.setIsKill(false);
90 return Op;
91}
92
94 const SystemZSubtarget &STI)
95 : TargetLowering(TM, STI), Subtarget(STI) {
96 MVT PtrVT = MVT::getIntegerVT(TM.getPointerSizeInBits(0));
97
98 auto *Regs = STI.getSpecialRegisters();
99
100 // Set up the register classes.
101 if (Subtarget.hasHighWord())
102 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
103 else
104 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
105 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
106 if (!useSoftFloat()) {
107 if (Subtarget.hasVector()) {
108 addRegisterClass(MVT::f16, &SystemZ::VR16BitRegClass);
109 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
110 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
111 } else {
112 addRegisterClass(MVT::f16, &SystemZ::FP16BitRegClass);
113 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
114 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
115 }
116 if (Subtarget.hasVectorEnhancements1())
117 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
118 else
119 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
120
121 if (Subtarget.hasVector()) {
122 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
123 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
124 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
125 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
126 addRegisterClass(MVT::v8f16, &SystemZ::VR128BitRegClass);
127 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
128 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
129 }
130
131 if (Subtarget.hasVector())
132 addRegisterClass(MVT::i128, &SystemZ::VR128BitRegClass);
133 }
134
135 // Compute derived properties from the register classes
136 computeRegisterProperties(Subtarget.getRegisterInfo());
137
138 // Set up special registers.
139 setStackPointerRegisterToSaveRestore(Regs->getStackPointerRegister());
140
141 // TODO: It may be better to default to latency-oriented scheduling, however
142 // LLVM's current latency-oriented scheduler can't handle physreg definitions
143 // such as SystemZ has with CC, so set this to the register-pressure
144 // scheduler, because it can.
146
149
151
152 // Instructions are strings of 2-byte aligned 2-byte values.
154 // For performance reasons we prefer 16-byte alignment.
156
157 // Handle operations that are handled in a similar way for all types.
158 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
159 I <= MVT::LAST_FP_VALUETYPE;
160 ++I) {
162 if (isTypeLegal(VT)) {
163 // Lower SET_CC into an IPM-based sequence.
167
168 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
170
171 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
174 }
175 }
176
177 // Expand jump table branches as address arithmetic followed by an
178 // indirect jump.
180
181 // Expand BRCOND into a BR_CC (see above).
183
184 // Handle integer types except i128.
185 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
186 I <= MVT::LAST_INTEGER_VALUETYPE;
187 ++I) {
189 if (isTypeLegal(VT) && VT != MVT::i128) {
191
192 // Expand individual DIV and REMs into DIVREMs.
199
200 // Support addition/subtraction with overflow.
203
204 // Support addition/subtraction with carry.
207
208 // Support carry in as value rather than glue.
211
212 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
213 // available, or if the operand is constant.
215
216 // Use POPCNT on z196 and above.
217 if (Subtarget.hasPopulationCount())
219 else
221
222 // No special instructions for these.
225
226 // Use *MUL_LOHI where possible instead of MULH*.
231
232 // The fp<=>i32/i64 conversions are all Legal except for f16 and for
233 // unsigned on z10 (only z196 and above have native support for
234 // unsigned conversions).
241 // Handle unsigned 32-bit input types as signed 64-bit types on z10.
242 auto OpAction =
243 (!Subtarget.hasFPExtension() && VT == MVT::i32) ? Promote : Custom;
244 setOperationAction(Op, VT, OpAction);
245 }
246 }
247 }
248
249 // Handle i128 if legal.
250 if (isTypeLegal(MVT::i128)) {
251 // No special instructions for these.
258
259 // We may be able to use VSLDB/VSLD/VSRD for these.
262
263 // No special instructions for these before z17.
264 if (!Subtarget.hasVectorEnhancements3()) {
274 } else {
275 // Even if we do have a legal 128-bit multiply, we do not
276 // want 64-bit multiply-high operations to use it.
279 }
280
281 // Support addition/subtraction with carry.
286
287 // Use VPOPCT and add up partial results.
289
290 // Additional instructions available with z17.
291 if (Subtarget.hasVectorEnhancements3()) {
292 setOperationAction(ISD::ABS, MVT::i128, Legal);
293
295 MVT::i128, Legal);
296 }
297 }
298
299 // These need custom handling in order to handle the f16 conversions.
308
309 // Type legalization will convert 8- and 16-bit atomic operations into
310 // forms that operate on i32s (but still keeping the original memory VT).
311 // Lower them into full i32 operations.
323
324 // Whether or not i128 is not a legal type, we need to custom lower
325 // the atomic operations in order to exploit SystemZ instructions.
330
331 // Mark sign/zero extending atomic loads as legal, which will make
332 // DAGCombiner fold extensions into atomic loads if possible.
334 {MVT::i8, MVT::i16, MVT::i32}, Legal);
336 {MVT::i8, MVT::i16}, Legal);
338 MVT::i8, Legal);
339
340 // We can use the CC result of compare-and-swap to implement
341 // the "success" result of ATOMIC_CMP_SWAP_WITH_SUCCESS.
345
347
348 // Traps are legal, as we will convert them to "j .+2".
349 setOperationAction(ISD::TRAP, MVT::Other, Legal);
350
351 // We have native support for a 64-bit CTLZ, via FLOGR.
355
356 // On z17 we have native support for a 64-bit CTTZ.
357 if (Subtarget.hasMiscellaneousExtensions4()) {
361 }
362
363 // On z15 we have native support for a 64-bit CTPOP.
364 if (Subtarget.hasMiscellaneousExtensions3()) {
367 }
368
369 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
371
372 // Expand 128 bit shifts without using a libcall.
376
377 // Also expand 256 bit shifts if i128 is a legal type.
378 if (isTypeLegal(MVT::i128)) {
382 }
383
384 // Handle bitcast from fp128 to i128.
385 if (!isTypeLegal(MVT::i128))
387
388 // We have native instructions for i8, i16 and i32 extensions, but not i1.
390 for (MVT VT : MVT::integer_valuetypes()) {
394 }
395
396 // Handle the various types of symbolic address.
402
403 // We need to handle dynamic allocations specially because of the
404 // 160-byte area at the bottom of the stack.
407
410
411 // Handle prefetches with PFD or PFDRL.
413
414 // Handle readcyclecounter with STCKF.
416
418 // Assume by default that all vector operations need to be expanded.
419 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
420 if (getOperationAction(Opcode, VT) == Legal)
421 setOperationAction(Opcode, VT, Expand);
422
423 // Likewise all truncating stores and extending loads.
424 for (MVT InnerVT : MVT::fixedlen_vector_valuetypes()) {
425 setTruncStoreAction(VT, InnerVT, Expand);
428 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
429 }
430
431 if (isTypeLegal(VT)) {
432 // These operations are legal for anything that can be stored in a
433 // vector register, even if there is no native support for the format
434 // as such. In particular, we can do these for v4f32 even though there
435 // are no specific instructions for that format.
441
442 // Likewise, except that we need to replace the nodes with something
443 // more specific.
446 }
447 }
448
449 // Handle integer vector types.
451 if (isTypeLegal(VT)) {
452 // These operations have direct equivalents.
457 if (VT != MVT::v2i64 || Subtarget.hasVectorEnhancements3()) {
461 }
462 if (Subtarget.hasVectorEnhancements3() &&
463 VT != MVT::v16i8 && VT != MVT::v8i16) {
468 }
473 if (Subtarget.hasVectorEnhancements1())
475 else
479
480 // Convert a GPR scalar to a vector by inserting it into element 0.
482
483 // Use a series of unpacks for extensions.
486
487 // Detect shifts/rotates by a scalar amount and convert them into
488 // V*_BY_SCALAR.
493
494 // Add ISD::VECREDUCE_ADD as custom in order to implement
495 // it with VZERO+VSUM
497
498 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
499 // and inverting the result as necessary.
501
503 Legal);
504 }
505 }
506
507 if (Subtarget.hasVector()) {
508 // There should be no need to check for float types other than v2f64
509 // since <2 x f32> isn't a legal type.
518
527 }
528
529 if (Subtarget.hasVectorEnhancements2()) {
538
547 }
548
549 // Handle floating-point types.
550 if (!useSoftFloat()) {
551 // Promote all f16 operations to float, with some exceptions below.
552 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
553 setOperationAction(Opc, MVT::f16, Promote);
555 for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
556 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
557 setTruncStoreAction(VT, MVT::f16, Expand);
558 }
560 setOperationAction(Op, MVT::f16, Subtarget.hasVector() ? Legal : Custom);
564
565 for (auto Op : {ISD::FNEG, ISD::FABS, ISD::FCOPYSIGN})
566 setOperationAction(Op, MVT::f16, Legal);
567 }
568
569 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
570 I <= MVT::LAST_FP_VALUETYPE;
571 ++I) {
573 if (isTypeLegal(VT) && VT != MVT::f16) {
574 // We can use FI for FRINT.
576
577 // We can use the extended form of FI for other rounding operations.
578 if (Subtarget.hasFPExtension()) {
585 }
586
587 // No special instructions for these.
593
594 // Special treatment.
596
597 // Handle constrained floating-point operations.
606 if (Subtarget.hasFPExtension()) {
613 }
614
615 // Extension from f16 needs libcall.
618 }
619 }
620
621 // Handle floating-point vector types.
622 if (Subtarget.hasVector()) {
623 // Scalar-to-vector conversion is just a subreg.
627
628 // Some insertions and extractions can be done directly but others
629 // need to go via integers.
636
637 // These operations have direct equivalents.
638 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
639 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
640 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
641 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
642 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
643 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
644 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
645 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
646 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
649 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
653
654 // Handle constrained floating-point operations.
668
673 if (Subtarget.hasVectorEnhancements1()) {
676 }
677 }
678
679 // The vector enhancements facility 1 has instructions for these.
680 if (Subtarget.hasVectorEnhancements1()) {
681 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
682 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
683 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
684 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
685 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
686 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
687 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
688 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
689 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
692 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
696
697 for (MVT Type : {MVT::f64, MVT::v2f64, MVT::f32, MVT::v4f32, MVT::f128}) {
706 }
707
708 // Handle constrained floating-point operations.
722 for (auto VT : { MVT::f32, MVT::f64, MVT::f128,
723 MVT::v4f32, MVT::v2f64 }) {
730 }
731 }
732
733 // We only have fused f128 multiply-addition on vector registers.
734 if (!Subtarget.hasVectorEnhancements1()) {
737 }
738
739 // We don't have a copysign instruction on vector registers.
740 if (Subtarget.hasVectorEnhancements1())
742
743 // Needed so that we don't try to implement f128 constant loads using
744 // a load-and-extend of a f80 constant (in cases where the constant
745 // would fit in an f80).
746 for (MVT VT : MVT::fp_valuetypes())
747 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
748
749 // We don't have extending load instruction on vector registers.
750 if (Subtarget.hasVectorEnhancements1()) {
751 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
752 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
753 }
754
755 // Floating-point truncation and stores need to be done separately.
756 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
757 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
758 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
759
760 // We have 64-bit FPR<->GPR moves, but need special handling for
761 // 32-bit forms.
762 if (!Subtarget.hasVector()) {
765 }
766
767 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
768 // structure, but VAEND is a no-op.
772
773 if (Subtarget.isTargetzOS()) {
774 // Handle address space casts between mixed sized pointers.
777 }
778
780
781 // Codes for which we want to perform some z-specific combinations.
785 ISD::LOAD,
798 ISD::SRL,
799 ISD::SRA,
800 ISD::MUL,
801 ISD::SDIV,
802 ISD::UDIV,
803 ISD::SREM,
804 ISD::UREM,
807
808 // Handle intrinsics.
811
812 // We're not using SJLJ for exception handling, but they're implemented
813 // solely to support use of __builtin_setjmp / __builtin_longjmp.
816
817 // We want to use MVC in preference to even a single load/store pair.
818 MaxStoresPerMemcpy = Subtarget.hasVector() ? 2 : 0;
820
821 // Same with memmove.
822 MaxStoresPerMemmove = Subtarget.hasVector() ? 2 : 0;
824
825 // The main memset sequence is a byte store followed by an MVC.
826 // Two STC or MV..I stores win over that, but the kind of fused stores
827 // generated by target-independent code don't when the byte value is
828 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
829 // than "STC;MVC". Handle the choice in target-specific code instead.
830 MaxStoresPerMemset = Subtarget.hasVector() ? 2 : 0;
832
833 // Default to having -disable-strictnode-mutation on
834 IsStrictFPEnabled = true;
835}
836
838 return Subtarget.hasSoftFloat();
839}
840
842 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT,
843 unsigned &NumIntermediates, MVT &RegisterVT) const {
844 // Pass fp16 vectors in VR(s).
845 if (Subtarget.hasVector() && VT.isVectorOf(MVT::f16)) {
846 IntermediateVT = RegisterVT = MVT::v8f16;
847 return NumIntermediates =
849 }
851 Context, CC, VT, IntermediateVT, NumIntermediates, RegisterVT);
852}
853
856 EVT VT) const {
857 // 128-bit single-element vector types are passed like other vectors,
858 // not like their element type.
859 if (Subtarget.hasVector() && VT.isVector() && VT.getSizeInBits() == 128 &&
860 VT.getVectorNumElements() == 1)
861 return MVT::v16i8;
862 // Pass fp16 vectors in VR(s).
863 if (Subtarget.hasVector() && VT.isVectorOf(MVT::f16))
864 return MVT::v8f16;
865 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
866}
867
869 LLVMContext &Context, CallingConv::ID CC, EVT VT) const {
870 // Pass fp16 vectors in VR(s).
871 if (Subtarget.hasVector() && VT.isVectorOf(MVT::f16))
873 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
874}
875
877 LLVMContext &, EVT VT) const {
878 if (!VT.isVector())
879 return MVT::i32;
881}
882
884 const MachineFunction &MF, EVT VT) const {
885 if (useSoftFloat())
886 return false;
887
888 VT = VT.getScalarType();
889
890 if (!VT.isSimple())
891 return false;
892
893 switch (VT.getSimpleVT().SimpleTy) {
894 case MVT::f32:
895 case MVT::f64:
896 return true;
897 case MVT::f128:
898 return Subtarget.hasVectorEnhancements1();
899 default:
900 break;
901 }
902
903 return false;
904}
905
906// Return true if the constant can be generated with a vector instruction,
907// such as VGM, VGMB or VREPI.
909 const SystemZSubtarget &Subtarget) {
910 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
911 if (!Subtarget.hasVector() ||
912 (isFP128 && !Subtarget.hasVectorEnhancements1()))
913 return false;
914
915 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
916 // preferred way of creating all-zero and all-one vectors so give it
917 // priority over other methods below.
918 unsigned Mask = 0;
919 unsigned I = 0;
920 for (; I < SystemZ::VectorBytes; ++I) {
921 uint64_t Byte = IntBits.lshr(I * 8).trunc(8).getZExtValue();
922 if (Byte == 0xff)
923 Mask |= 1ULL << I;
924 else if (Byte != 0)
925 break;
926 }
927 if (I == SystemZ::VectorBytes) {
928 Opcode = SystemZISD::BYTE_MASK;
929 OpVals.push_back(Mask);
931 return true;
932 }
933
934 if (SplatBitSize > 64)
935 return false;
936
937 auto TryValue = [&](uint64_t Value) -> bool {
938 // Try VECTOR REPLICATE IMMEDIATE
939 int64_t SignedValue = SignExtend64(Value, SplatBitSize);
940 if (isInt<16>(SignedValue)) {
941 OpVals.push_back(((unsigned) SignedValue));
942 Opcode = SystemZISD::REPLICATE;
944 SystemZ::VectorBits / SplatBitSize);
945 return true;
946 }
947 // Try VECTOR GENERATE MASK
948 unsigned Start, End;
949 if (TII->isRxSBGMask(Value, SplatBitSize, Start, End)) {
950 // isRxSBGMask returns the bit numbers for a full 64-bit value, with 0
951 // denoting 1 << 63 and 63 denoting 1. Convert them to bit numbers for
952 // an SplatBitSize value, so that 0 denotes 1 << (SplatBitSize-1).
953 OpVals.push_back(Start - (64 - SplatBitSize));
954 OpVals.push_back(End - (64 - SplatBitSize));
955 Opcode = SystemZISD::ROTATE_MASK;
957 SystemZ::VectorBits / SplatBitSize);
958 return true;
959 }
960 return false;
961 };
962
963 // First try assuming that any undefined bits above the highest set bit
964 // and below the lowest set bit are 1s. This increases the likelihood of
965 // being able to use a sign-extended element value in VECTOR REPLICATE
966 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
967 uint64_t SplatBitsZ = SplatBits.getZExtValue();
968 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
969 unsigned LowerBits = llvm::countr_zero(SplatBitsZ);
970 unsigned UpperBits = llvm::countl_zero(SplatBitsZ);
971 uint64_t Lower = SplatUndefZ & maskTrailingOnes<uint64_t>(LowerBits);
972 uint64_t Upper = SplatUndefZ & maskLeadingOnes<uint64_t>(UpperBits);
973 if (TryValue(SplatBitsZ | Upper | Lower))
974 return true;
975
976 // Now try assuming that any undefined bits between the first and
977 // last defined set bits are set. This increases the chances of
978 // using a non-wraparound mask.
979 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
980 return TryValue(SplatBitsZ | Middle);
981}
982
984 if (IntImm.isSingleWord()) {
985 IntBits = APInt(128, IntImm.getZExtValue());
986 IntBits <<= (SystemZ::VectorBits - IntImm.getBitWidth());
987 } else
988 IntBits = IntImm;
989 assert(IntBits.getBitWidth() == 128 && "Unsupported APInt.");
990
991 // Find the smallest splat.
992 SplatBits = IntImm;
993 unsigned Width = SplatBits.getBitWidth();
994 while (Width > 8) {
995 unsigned HalfSize = Width / 2;
996 APInt HighValue = SplatBits.lshr(HalfSize).trunc(HalfSize);
997 APInt LowValue = SplatBits.trunc(HalfSize);
998
999 // If the two halves do not match, stop here.
1000 if (HighValue != LowValue || 8 > HalfSize)
1001 break;
1002
1003 SplatBits = HighValue;
1004 Width = HalfSize;
1005 }
1006 SplatUndef = 0;
1007 SplatBitSize = Width;
1008}
1009
1011 assert(BVN->isConstant() && "Expected a constant BUILD_VECTOR");
1012 bool HasAnyUndefs;
1013
1014 // Get IntBits by finding the 128 bit splat.
1015 BVN->isConstantSplat(IntBits, SplatUndef, SplatBitSize, HasAnyUndefs, 128,
1016 true);
1017
1018 // Get SplatBits by finding the 8 bit or greater splat.
1019 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 8,
1020 true);
1021}
1022
1024 bool ForCodeSize) const {
1025 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
1026 if (Imm.isZero() || Imm.isNegZero())
1027 return true;
1028
1029 return SystemZVectorConstantInfo(Imm).isVectorConstantLegal(Subtarget);
1030}
1031
1034 MachineBasicBlock *MBB) const {
1035 DebugLoc DL = MI.getDebugLoc();
1036 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1037 const SystemZRegisterInfo *TRI = Subtarget.getRegisterInfo();
1038
1039 MachineFunction *MF = MBB->getParent();
1040 MachineRegisterInfo &MRI = MF->getRegInfo();
1041
1042 const BasicBlock *BB = MBB->getBasicBlock();
1043 MachineFunction::iterator I = ++MBB->getIterator();
1044
1045 Register DstReg = MI.getOperand(0).getReg();
1046 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
1047 assert(TRI->isTypeLegalForClass(*RC, MVT::i32) && "Invalid destination!");
1048 (void)TRI;
1049 Register MainDstReg = MRI.createVirtualRegister(RC);
1050 Register RestoreDstReg = MRI.createVirtualRegister(RC);
1051
1052 MVT PVT = getPointerTy(MF->getDataLayout());
1053 assert((PVT == MVT::i64 || PVT == MVT::i32) && "Invalid Pointer Size!");
1054 // For v = setjmp(buf), we generate.
1055 // Algorithm:
1056 //
1057 // ---------
1058 // | thisMBB |
1059 // ---------
1060 // |
1061 // ------------------------
1062 // | |
1063 // ---------- ---------------
1064 // | mainMBB | | restoreMBB |
1065 // | v = 0 | | v = 1 |
1066 // ---------- ---------------
1067 // | |
1068 // -------------------------
1069 // |
1070 // -----------------------------
1071 // | sinkMBB |
1072 // | phi(v_mainMBB,v_restoreMBB) |
1073 // -----------------------------
1074 // thisMBB:
1075 // buf[FPOffset] = Frame Pointer if hasFP.
1076 // buf[LabelOffset] = restoreMBB <-- takes address of restoreMBB.
1077 // buf[BCOffset] = Backchain value if building with -mbackchain.
1078 // buf[SPOffset] = Stack Pointer.
1079 // buf[LPOffset] = We never write this slot with R13, gcc stores R13 always.
1080 // SjLjSetup restoreMBB
1081 // mainMBB:
1082 // v_main = 0
1083 // sinkMBB:
1084 // v = phi(v_main, v_restore)
1085 // restoreMBB:
1086 // v_restore = 1
1087
1088 MachineBasicBlock *ThisMBB = MBB;
1089 MachineBasicBlock *MainMBB = MF->CreateMachineBasicBlock(BB);
1090 MachineBasicBlock *SinkMBB = MF->CreateMachineBasicBlock(BB);
1091 MachineBasicBlock *RestoreMBB = MF->CreateMachineBasicBlock(BB);
1092
1093 MF->insert(I, MainMBB);
1094 MF->insert(I, SinkMBB);
1095 MF->push_back(RestoreMBB);
1096 RestoreMBB->setMachineBlockAddressTaken();
1097
1099
1100 // Transfer the remainder of BB and its successor edges to sinkMBB.
1101 SinkMBB->splice(SinkMBB->begin(), MBB,
1102 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
1104
1105 // thisMBB:
1106 const int64_t FPOffset = 0; // Slot 1.
1107 const int64_t LabelOffset = 1 * PVT.getStoreSize(); // Slot 2.
1108 const int64_t BCOffset = 2 * PVT.getStoreSize(); // Slot 3.
1109 const int64_t SPOffset = 3 * PVT.getStoreSize(); // Slot 4.
1110
1111 // Buf address.
1112 Register BufReg = MI.getOperand(1).getReg();
1113
1114 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
1115 Register LabelReg = MRI.createVirtualRegister(PtrRC);
1116
1117 // Prepare IP for longjmp.
1118 BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::LARL), LabelReg)
1119 .addMBB(RestoreMBB);
1120 // Store IP for return from jmp, slot 2, offset = 1.
1121 BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::STG))
1122 .addReg(LabelReg)
1123 .addReg(BufReg)
1124 .addImm(LabelOffset)
1125 .addReg(0);
1126
1127 auto *SpecialRegs = Subtarget.getSpecialRegisters();
1128 bool HasFP = Subtarget.getFrameLowering()->hasFP(*MF);
1129 if (HasFP) {
1130 BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::STG))
1131 .addReg(SpecialRegs->getFramePointerRegister())
1132 .addReg(BufReg)
1133 .addImm(FPOffset)
1134 .addReg(0);
1135 }
1136
1137 // Store SP.
1138 BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::STG))
1139 .addReg(SpecialRegs->getStackPointerRegister())
1140 .addReg(BufReg)
1141 .addImm(SPOffset)
1142 .addReg(0);
1143
1144 // Slot 3(Offset = 2) Backchain value (if building with -mbackchain).
1145 bool BackChain = MF->getSubtarget<SystemZSubtarget>().hasBackChain();
1146 if (BackChain) {
1147 Register BCReg = MRI.createVirtualRegister(PtrRC);
1148 auto *TFL = Subtarget.getFrameLowering<SystemZFrameLowering>();
1149 MIB = BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::LG), BCReg)
1150 .addReg(SpecialRegs->getStackPointerRegister())
1151 .addImm(TFL->getBackchainOffset(*MF))
1152 .addReg(0);
1153
1154 BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::STG))
1155 .addReg(BCReg)
1156 .addReg(BufReg)
1157 .addImm(BCOffset)
1158 .addReg(0);
1159 }
1160
1161 // Setup.
1162 MIB = BuildMI(*ThisMBB, MI, DL, TII->get(SystemZ::EH_SjLj_Setup))
1163 .addMBB(RestoreMBB);
1164
1165 const SystemZRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1166 MIB.addRegMask(RegInfo->getNoPreservedMask());
1167
1168 ThisMBB->addSuccessor(MainMBB);
1169 ThisMBB->addSuccessor(RestoreMBB);
1170
1171 // mainMBB:
1172 BuildMI(MainMBB, DL, TII->get(SystemZ::LHI), MainDstReg).addImm(0);
1173 MainMBB->addSuccessor(SinkMBB);
1174
1175 // sinkMBB:
1176 BuildMI(*SinkMBB, SinkMBB->begin(), DL, TII->get(SystemZ::PHI), DstReg)
1177 .addReg(MainDstReg)
1178 .addMBB(MainMBB)
1179 .addReg(RestoreDstReg)
1180 .addMBB(RestoreMBB);
1181
1182 // restoreMBB.
1183 BuildMI(RestoreMBB, DL, TII->get(SystemZ::LHI), RestoreDstReg).addImm(1);
1184 BuildMI(RestoreMBB, DL, TII->get(SystemZ::J)).addMBB(SinkMBB);
1185 RestoreMBB->addSuccessor(SinkMBB);
1186
1187 MI.eraseFromParent();
1188
1189 return SinkMBB;
1190}
1191
1194 MachineBasicBlock *MBB) const {
1195
1196 DebugLoc DL = MI.getDebugLoc();
1197 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1198
1199 MachineFunction *MF = MBB->getParent();
1200 MachineRegisterInfo &MRI = MF->getRegInfo();
1201
1202 MVT PVT = getPointerTy(MF->getDataLayout());
1203 assert((PVT == MVT::i64 || PVT == MVT::i32) && "Invalid Pointer Size!");
1204 Register BufReg = MI.getOperand(0).getReg();
1205 const TargetRegisterClass *RC = MRI.getRegClass(BufReg);
1206 auto *SpecialRegs = Subtarget.getSpecialRegisters();
1207
1208 Register Tmp = MRI.createVirtualRegister(RC);
1209 Register BCReg = MRI.createVirtualRegister(RC);
1210
1212
1213 const int64_t FPOffset = 0;
1214 const int64_t LabelOffset = 1 * PVT.getStoreSize();
1215 const int64_t BCOffset = 2 * PVT.getStoreSize();
1216 const int64_t SPOffset = 3 * PVT.getStoreSize();
1217 const int64_t LPOffset = 4 * PVT.getStoreSize();
1218
1219 MIB = BuildMI(*MBB, MI, DL, TII->get(SystemZ::LG), Tmp)
1220 .addReg(BufReg)
1221 .addImm(LabelOffset)
1222 .addReg(0);
1223
1224 MIB = BuildMI(*MBB, MI, DL, TII->get(SystemZ::LG),
1225 SpecialRegs->getFramePointerRegister())
1226 .addReg(BufReg)
1227 .addImm(FPOffset)
1228 .addReg(0);
1229
1230 // We are restoring R13 even though we never stored in setjmp from llvm,
1231 // as gcc always stores R13 in builtin_setjmp. We could have mixed code
1232 // gcc setjmp and llvm longjmp.
1233 MIB = BuildMI(*MBB, MI, DL, TII->get(SystemZ::LG), SystemZ::R13D)
1234 .addReg(BufReg)
1235 .addImm(LPOffset)
1236 .addReg(0);
1237
1238 bool BackChain = MF->getSubtarget<SystemZSubtarget>().hasBackChain();
1239 if (BackChain) {
1240 MIB = BuildMI(*MBB, MI, DL, TII->get(SystemZ::LG), BCReg)
1241 .addReg(BufReg)
1242 .addImm(BCOffset)
1243 .addReg(0);
1244 }
1245
1246 MIB = BuildMI(*MBB, MI, DL, TII->get(SystemZ::LG),
1247 SpecialRegs->getStackPointerRegister())
1248 .addReg(BufReg)
1249 .addImm(SPOffset)
1250 .addReg(0);
1251
1252 if (BackChain) {
1253 auto *TFL = Subtarget.getFrameLowering<SystemZFrameLowering>();
1254 BuildMI(*MBB, MI, DL, TII->get(SystemZ::STG))
1255 .addReg(BCReg)
1256 .addReg(SpecialRegs->getStackPointerRegister())
1257 .addImm(TFL->getBackchainOffset(*MF))
1258 .addReg(0);
1259 }
1260
1261 MIB = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BR)).addReg(Tmp);
1262
1263 MI.eraseFromParent();
1264 return MBB;
1265}
1266
1267/// Returns true if stack probing through inline assembly is requested.
1269 // If the function specifically requests inline stack probes, emit them.
1270 if (MF.getFunction().hasFnAttribute("probe-stack"))
1271 return MF.getFunction().getFnAttribute("probe-stack").getValueAsString() ==
1272 "inline-asm";
1273 return false;
1274}
1275
1280
1285
1288 const AtomicRMWInst *RMW) const {
1289 // Don't expand subword operations as they require special treatment.
1290 if (RMW->getType()->isIntegerTy(8) || RMW->getType()->isIntegerTy(16))
1292
1293 // Don't expand if there is a target instruction available.
1294 if (Subtarget.hasInterlockedAccess1() &&
1295 (RMW->getType()->isIntegerTy(32) || RMW->getType()->isIntegerTy(64)) &&
1302
1304}
1305
1307 // We can use CGFI or CLGFI.
1308 return isInt<32>(Imm) || isUInt<32>(Imm);
1309}
1310
1312 // We can use ALGFI or SLGFI.
1313 return isUInt<32>(Imm) || isUInt<32>(-Imm);
1314}
1315
1317 EVT VT, unsigned, Align, MachineMemOperand::Flags, unsigned *Fast) const {
1318 // Unaligned accesses should never be slower than the expanded version.
1319 // We check specifically for aligned accesses in the few cases where
1320 // they are required.
1321 if (Fast)
1322 *Fast = 1;
1323 return true;
1324}
1325
1327 EVT VT = Y.getValueType();
1328
1329 // We can use NC(G)RK for types in GPRs ...
1330 if (VT == MVT::i32 || VT == MVT::i64)
1331 return Subtarget.hasMiscellaneousExtensions3();
1332
1333 // ... or VNC for types in VRs.
1334 if (VT.isVector() || VT == MVT::i128)
1335 return Subtarget.hasVector();
1336
1337 return false;
1338}
1339
1340// Information about the addressing mode for a memory access.
1342 // True if a long displacement is supported.
1344
1345 // True if use of index register is supported.
1347
1348 AddressingMode(bool LongDispl, bool IdxReg) :
1349 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
1350};
1351
1352// Return the desired addressing mode for a Load which has only one use (in
1353// the same block) which is a Store.
1355 Type *Ty) {
1356 // With vector support a Load->Store combination may be combined to either
1357 // an MVC or vector operations and it seems to work best to allow the
1358 // vector addressing mode.
1359 if (HasVector)
1360 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
1361
1362 // Otherwise only the MVC case is special.
1363 bool MVC = Ty->isIntegerTy(8);
1364 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
1365}
1366
1367// Return the addressing mode which seems most desirable given an LLVM
1368// Instruction pointer.
1369static AddressingMode
1372 switch (II->getIntrinsicID()) {
1373 default: break;
1374 case Intrinsic::memset:
1375 case Intrinsic::memmove:
1376 case Intrinsic::memcpy:
1377 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
1378 }
1379 }
1380
1381 if (isa<LoadInst>(I) && I->hasOneUse()) {
1382 auto *SingleUser = cast<Instruction>(*I->user_begin());
1383 if (SingleUser->getParent() == I->getParent()) {
1384 if (isa<ICmpInst>(SingleUser)) {
1385 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
1386 if (C->getBitWidth() <= 64 &&
1387 (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue())))
1388 // Comparison of memory with 16 bit signed / unsigned immediate
1389 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
1390 } else if (isa<StoreInst>(SingleUser))
1391 // Load->Store
1392 return getLoadStoreAddrMode(HasVector, I->getType());
1393 }
1394 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
1395 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
1396 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
1397 // Load->Store
1398 return getLoadStoreAddrMode(HasVector, LoadI->getType());
1399 }
1400
1401 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
1402
1403 // * Use LDE instead of LE/LEY for z13 to avoid partial register
1404 // dependencies (LDE only supports small offsets).
1405 // * Utilize the vector registers to hold floating point
1406 // values (vector load / store instructions only support small
1407 // offsets).
1408
1409 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
1410 I->getOperand(0)->getType());
1411 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
1412 bool IsVectorAccess = MemAccessTy->isVectorTy();
1413
1414 // A store of an extracted vector element will be combined into a VSTE type
1415 // instruction.
1416 if (!IsVectorAccess && isa<StoreInst>(I)) {
1417 Value *DataOp = I->getOperand(0);
1418 if (isa<ExtractElementInst>(DataOp))
1419 IsVectorAccess = true;
1420 }
1421
1422 // A load which gets inserted into a vector element will be combined into a
1423 // VLE type instruction.
1424 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
1425 User *LoadUser = *I->user_begin();
1426 if (isa<InsertElementInst>(LoadUser))
1427 IsVectorAccess = true;
1428 }
1429
1430 if (IsFPAccess || IsVectorAccess)
1431 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
1432 }
1433
1434 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
1435}
1436
1438 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
1439 // Punt on globals for now, although they can be used in limited
1440 // RELATIVE LONG cases.
1441 if (AM.BaseGV)
1442 return false;
1443
1444 // Require a 20-bit signed offset.
1445 if (!isInt<20>(AM.BaseOffs))
1446 return false;
1447
1448 bool RequireD12 =
1449 Subtarget.hasVector() && (Ty->isVectorTy() || Ty->isIntegerTy(128));
1450 AddressingMode SupportedAM(!RequireD12, true);
1451 if (I != nullptr)
1452 SupportedAM = supportedAddressingMode(I, Subtarget.hasVector());
1453
1454 if (!SupportedAM.LongDisplacement && !isUInt<12>(AM.BaseOffs))
1455 return false;
1456
1457 if (!SupportedAM.IndexReg)
1458 // No indexing allowed.
1459 return AM.Scale == 0;
1460 else
1461 // Indexing is OK but no scale factor can be applied.
1462 return AM.Scale == 0 || AM.Scale == 1;
1463}
1464
1466 LLVMContext &Context, std::vector<EVT> &MemOps, unsigned Limit,
1467 const MemOp &Op, unsigned DstAS, unsigned SrcAS,
1468 const AttributeList &FuncAttributes, EVT *LargestVT) const {
1469
1470 assert(Limit != ~0U &&
1471 "Expected EmitTargetCodeForMemXXX() to handle AlwaysInline cases.");
1472
1473 if (Op.isZeroMemset())
1474 return false; // Memset zero: Use XC.
1475
1476 const int MVCFastLen = 16;
1477 // Use MVC up to 16 bytes for memcpy. Small memset uses STC/MVI for first
1478 // byte.
1479 if (Op.isMemcpy() && Op.size() <= MVCFastLen)
1480 return false;
1481 if (Op.isMemset() && Op.size() - 1 <= MVCFastLen)
1482 return false;
1483
1484 // Avoid unaligned VL/VST:s.
1485 if ((Op.size() >= 16 && !Op.isAligned(Align(8))) ||
1486 (Op.size() >= 25 && Op.size() <= 31))
1487 return false;
1488
1490 Context, MemOps, Limit, Op, DstAS, SrcAS, FuncAttributes, LargestVT);
1491}
1492
1494 LLVMContext &Context, const MemOp &Op,
1495 const AttributeList &FuncAttributes) const {
1496 return Subtarget.hasVector() ? MVT::v2i64 : MVT::Other;
1497}
1498
1499bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
1500 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
1501 return false;
1502 unsigned FromBits = FromType->getPrimitiveSizeInBits().getFixedValue();
1503 unsigned ToBits = ToType->getPrimitiveSizeInBits().getFixedValue();
1504 return FromBits > ToBits;
1505}
1506
1508 if (!FromVT.isInteger() || !ToVT.isInteger())
1509 return false;
1510 unsigned FromBits = FromVT.getFixedSizeInBits();
1511 unsigned ToBits = ToVT.getFixedSizeInBits();
1512 return FromBits > ToBits;
1513}
1514
1515//===----------------------------------------------------------------------===//
1516// Inline asm support
1517//===----------------------------------------------------------------------===//
1518
1521 if (Constraint.size() == 1) {
1522 switch (Constraint[0]) {
1523 case 'a': // Address register
1524 case 'd': // Data register (equivalent to 'r')
1525 case 'f': // Floating-point register
1526 case 'h': // High-part register
1527 case 'r': // General-purpose register
1528 case 'v': // Vector register
1529 return C_RegisterClass;
1530
1531 case 'Q': // Memory with base and unsigned 12-bit displacement
1532 case 'R': // Likewise, plus an index
1533 case 'S': // Memory with base and signed 20-bit displacement
1534 case 'T': // Likewise, plus an index
1535 case 'm': // Equivalent to 'T'.
1536 return C_Memory;
1537
1538 case 'I': // Unsigned 8-bit constant
1539 case 'J': // Unsigned 12-bit constant
1540 case 'K': // Signed 16-bit constant
1541 case 'L': // Signed 20-bit displacement (on all targets we support)
1542 case 'M': // 0x7fffffff
1543 return C_Immediate;
1544
1545 default:
1546 break;
1547 }
1548 } else if (Constraint.size() == 2 && Constraint[0] == 'Z') {
1549 switch (Constraint[1]) {
1550 case 'Q': // Address with base and unsigned 12-bit displacement
1551 case 'R': // Likewise, plus an index
1552 case 'S': // Address with base and signed 20-bit displacement
1553 case 'T': // Likewise, plus an index
1554 return C_Address;
1555
1556 default:
1557 break;
1558 }
1559 } else if (Constraint.size() == 5 && Constraint.starts_with("{")) {
1560 if (StringRef("{@cc}").compare(Constraint) == 0)
1561 return C_Other;
1562 }
1563 return TargetLowering::getConstraintType(Constraint);
1564}
1565
1568 AsmOperandInfo &Info, const char *Constraint) const {
1570 Value *CallOperandVal = Info.CallOperandVal;
1571 // If we don't have a value, we can't do a match,
1572 // but allow it at the lowest weight.
1573 if (!CallOperandVal)
1574 return CW_Default;
1575 Type *type = CallOperandVal->getType();
1576 // Look at the constraint type.
1577 switch (*Constraint) {
1578 default:
1579 Weight = TargetLowering::getSingleConstraintMatchWeight(Info, Constraint);
1580 break;
1581
1582 case 'a': // Address register
1583 case 'd': // Data register (equivalent to 'r')
1584 case 'h': // High-part register
1585 case 'r': // General-purpose register
1586 Weight =
1587 CallOperandVal->getType()->isIntegerTy() ? CW_Register : CW_Default;
1588 break;
1589
1590 case 'f': // Floating-point register
1591 if (!useSoftFloat())
1592 Weight = type->isFloatingPointTy() ? CW_Register : CW_Default;
1593 break;
1594
1595 case 'v': // Vector register
1596 if (Subtarget.hasVector())
1597 Weight = (type->isVectorTy() || type->isFloatingPointTy()) ? CW_Register
1598 : CW_Default;
1599 break;
1600
1601 case 'I': // Unsigned 8-bit constant
1602 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
1603 if (isUInt<8>(C->getZExtValue()))
1604 Weight = CW_Constant;
1605 break;
1606
1607 case 'J': // Unsigned 12-bit constant
1608 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
1609 if (isUInt<12>(C->getZExtValue()))
1610 Weight = CW_Constant;
1611 break;
1612
1613 case 'K': // Signed 16-bit constant
1614 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
1615 if (isInt<16>(C->getSExtValue()))
1616 Weight = CW_Constant;
1617 break;
1618
1619 case 'L': // Signed 20-bit displacement (on all targets we support)
1620 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
1621 if (isInt<20>(C->getSExtValue()))
1622 Weight = CW_Constant;
1623 break;
1624
1625 case 'M': // 0x7fffffff
1626 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
1627 if (C->getZExtValue() == 0x7fffffff)
1628 Weight = CW_Constant;
1629 break;
1630 }
1631 return Weight;
1632}
1633
1634// Parse a "{tNNN}" register constraint for which the register type "t"
1635// has already been verified. MC is the class associated with "t" and
1636// Map maps 0-based register numbers to LLVM register numbers.
1637static std::pair<unsigned, const TargetRegisterClass *>
1639 const unsigned *Map, unsigned Size) {
1640 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
1641 if (isdigit(Constraint[2])) {
1642 unsigned Index;
1643 bool Failed =
1644 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
1645 if (!Failed && Index < Size && Map[Index])
1646 return std::make_pair(Map[Index], RC);
1647 }
1648 return std::make_pair(0U, nullptr);
1649}
1650
1651std::pair<unsigned, const TargetRegisterClass *>
1653 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
1654 if (Constraint.size() == 1) {
1655 // GCC Constraint Letters
1656 switch (Constraint[0]) {
1657 default: break;
1658 case 'd': // Data register (equivalent to 'r')
1659 case 'r': // General-purpose register
1660 if (VT.getSizeInBits() == 64)
1661 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
1662 else if (VT.getSizeInBits() == 128)
1663 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
1664 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
1665
1666 case 'a': // Address register
1667 if (VT == MVT::i64)
1668 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
1669 else if (VT == MVT::i128)
1670 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
1671 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
1672
1673 case 'h': // High-part register (an LLVM extension)
1674 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
1675
1676 case 'f': // Floating-point register
1677 if (!useSoftFloat()) {
1678 if (VT.getSizeInBits() == 16)
1679 return std::make_pair(0U, &SystemZ::FP16BitRegClass);
1680 else if (VT.getSizeInBits() == 64)
1681 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
1682 else if (VT.getSizeInBits() == 128)
1683 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
1684 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
1685 }
1686 break;
1687
1688 case 'v': // Vector register
1689 if (Subtarget.hasVector()) {
1690 if (VT.getSizeInBits() == 16)
1691 return std::make_pair(0U, &SystemZ::VR16BitRegClass);
1692 if (VT.getSizeInBits() == 32)
1693 return std::make_pair(0U, &SystemZ::VR32BitRegClass);
1694 if (VT.getSizeInBits() == 64)
1695 return std::make_pair(0U, &SystemZ::VR64BitRegClass);
1696 return std::make_pair(0U, &SystemZ::VR128BitRegClass);
1697 }
1698 break;
1699 }
1700 }
1701 if (Constraint.starts_with("{")) {
1702
1703 // A clobber constraint (e.g. ~{f0}) will have MVT::Other which is illegal
1704 // to check the size on.
1705 auto getVTSizeInBits = [&VT]() {
1706 return VT == MVT::Other ? 0 : VT.getSizeInBits();
1707 };
1708
1709 // We need to override the default register parsing for GPRs and FPRs
1710 // because the interpretation depends on VT. The internal names of
1711 // the registers are also different from the external names
1712 // (F0D and F0S instead of F0, etc.).
1713 if (Constraint[1] == 'r') {
1714 if (getVTSizeInBits() == 32)
1715 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
1717 if (getVTSizeInBits() == 128)
1718 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
1720 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
1722 }
1723 if (Constraint[1] == 'f') {
1724 if (useSoftFloat())
1725 return std::make_pair(
1726 0u, static_cast<const TargetRegisterClass *>(nullptr));
1727 if (getVTSizeInBits() == 16)
1728 return parseRegisterNumber(Constraint, &SystemZ::FP16BitRegClass,
1730 if (getVTSizeInBits() == 32)
1731 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
1733 if (getVTSizeInBits() == 128)
1734 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
1736 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
1738 }
1739 if (Constraint[1] == 'v') {
1740 if (!Subtarget.hasVector())
1741 return std::make_pair(
1742 0u, static_cast<const TargetRegisterClass *>(nullptr));
1743 if (getVTSizeInBits() == 16)
1744 return parseRegisterNumber(Constraint, &SystemZ::VR16BitRegClass,
1746 if (getVTSizeInBits() == 32)
1747 return parseRegisterNumber(Constraint, &SystemZ::VR32BitRegClass,
1749 if (getVTSizeInBits() == 64)
1750 return parseRegisterNumber(Constraint, &SystemZ::VR64BitRegClass,
1752 return parseRegisterNumber(Constraint, &SystemZ::VR128BitRegClass,
1754 }
1755 if (Constraint[1] == '@') {
1756 if (StringRef("{@cc}").compare(Constraint) == 0)
1757 return std::make_pair(SystemZ::CC, &SystemZ::CCRRegClass);
1758 }
1759 }
1760 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1761}
1762
1763// FIXME? Maybe this could be a TableGen attribute on some registers and
1764// this table could be generated automatically from RegInfo.
1767 const MachineFunction &MF) const {
1768 Register Reg =
1770 .Case("r4", Subtarget.isTargetXPLINK64() ? SystemZ::R4D
1771 : SystemZ::NoRegister)
1772 .Case("r15",
1773 Subtarget.isTargetELF() ? SystemZ::R15D : SystemZ::NoRegister)
1774 .Default(Register());
1775
1776 return Reg;
1777}
1778
1780 const Constant *PersonalityFn) const {
1781 return Subtarget.isTargetXPLINK64() ? SystemZ::R1D : SystemZ::R6D;
1782}
1783
1785 const Constant *PersonalityFn) const {
1786 return Subtarget.isTargetXPLINK64() ? SystemZ::R2D : SystemZ::R7D;
1787}
1788
1789// Convert condition code in CCReg to an i32 value.
1791 SDLoc DL(CCReg);
1792 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
1793 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
1794 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
1795}
1796
1797// Lower @cc targets via setcc.
1799 SDValue &Chain, SDValue &Glue, const SDLoc &DL,
1800 const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const {
1801 if (StringRef("{@cc}").compare(OpInfo.ConstraintCode) != 0)
1802 return SDValue();
1803
1804 // Check that return type is valid.
1805 if (OpInfo.ConstraintVT.isVector() || !OpInfo.ConstraintVT.isInteger() ||
1806 OpInfo.ConstraintVT.getSizeInBits() < 8)
1807 report_fatal_error("Glue output operand is of invalid type");
1808
1809 if (Glue.getNode()) {
1810 Glue = DAG.getCopyFromReg(Chain, DL, SystemZ::CC, MVT::i32, Glue);
1811 Chain = Glue.getValue(1);
1812 } else
1813 Glue = DAG.getCopyFromReg(Chain, DL, SystemZ::CC, MVT::i32);
1814 return getCCResult(DAG, Glue);
1815}
1816
1818 SDValue Op, StringRef Constraint, std::vector<SDValue> &Ops,
1819 SelectionDAG &DAG) const {
1820 // Only support length 1 constraints for now.
1821 if (Constraint.size() == 1) {
1822 switch (Constraint[0]) {
1823 case 'I': // Unsigned 8-bit constant
1824 if (auto *C = dyn_cast<ConstantSDNode>(Op))
1825 if (isUInt<8>(C->getZExtValue()))
1826 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
1827 Op.getValueType()));
1828 return;
1829
1830 case 'J': // Unsigned 12-bit constant
1831 if (auto *C = dyn_cast<ConstantSDNode>(Op))
1832 if (isUInt<12>(C->getZExtValue()))
1833 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
1834 Op.getValueType()));
1835 return;
1836
1837 case 'K': // Signed 16-bit constant
1838 if (auto *C = dyn_cast<ConstantSDNode>(Op))
1839 if (isInt<16>(C->getSExtValue()))
1840 Ops.push_back(DAG.getSignedTargetConstant(
1841 C->getSExtValue(), SDLoc(Op), Op.getValueType()));
1842 return;
1843
1844 case 'L': // Signed 20-bit displacement (on all targets we support)
1845 if (auto *C = dyn_cast<ConstantSDNode>(Op))
1846 if (isInt<20>(C->getSExtValue()))
1847 Ops.push_back(DAG.getSignedTargetConstant(
1848 C->getSExtValue(), SDLoc(Op), Op.getValueType()));
1849 return;
1850
1851 case 'M': // 0x7fffffff
1852 if (auto *C = dyn_cast<ConstantSDNode>(Op))
1853 if (C->getZExtValue() == 0x7fffffff)
1854 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
1855 Op.getValueType()));
1856 return;
1857 }
1858 }
1860}
1861
1862//===----------------------------------------------------------------------===//
1863// Calling conventions
1864//===----------------------------------------------------------------------===//
1865
1866#define GET_CALLING_CONV_IMPL
1867#include "SystemZGenCallingConv.inc"
1868
1870 CallingConv::ID) const {
1871 static const MCPhysReg ScratchRegs[] = { SystemZ::R0D, SystemZ::R1D,
1872 SystemZ::R14D, 0 };
1873 return ScratchRegs;
1874}
1875
1877 Type *ToType) const {
1878 return isTruncateFree(FromType, ToType);
1879}
1880
1882 return CI->isTailCall();
1883}
1884
1885// Value is a value that has been passed to us in the location described by VA
1886// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
1887// any loads onto Chain.
1889 CCValAssign &VA, SDValue Chain,
1890 SDValue Value) {
1891 // If the argument has been promoted from a smaller type, insert an
1892 // assertion to capture this.
1893 if (VA.getLocInfo() == CCValAssign::SExt)
1895 DAG.getValueType(VA.getValVT()));
1896 else if (VA.getLocInfo() == CCValAssign::ZExt)
1898 DAG.getValueType(VA.getValVT()));
1899
1900 if (VA.isExtInLoc())
1901 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
1902 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1903 // If this is a short vector argument loaded from the stack,
1904 // extend from i64 to full vector size and then bitcast.
1905 assert(VA.getLocVT() == MVT::i64);
1906 assert(VA.getValVT().isVector());
1907 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
1908 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1909 } else
1910 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1911 return Value;
1912}
1913
1914// Value is a value of type VA.getValVT() that we need to copy into
1915// the location described by VA. Return a copy of Value converted to
1916// VA.getValVT(). The caller is responsible for handling indirect values.
1918 CCValAssign &VA, SDValue Value) {
1919 switch (VA.getLocInfo()) {
1920 case CCValAssign::SExt:
1921 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1922 case CCValAssign::ZExt:
1923 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1924 case CCValAssign::AExt:
1925 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
1926 case CCValAssign::BCvt: {
1927 assert(VA.getLocVT() == MVT::i64 || VA.getLocVT() == MVT::i128);
1928 assert(VA.getValVT().isVector() || VA.getValVT() == MVT::f32 ||
1929 VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::f128);
1930 // For an f32 vararg we need to first promote it to an f64 and then
1931 // bitcast it to an i64.
1932 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i64)
1933 Value = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f64, Value);
1934 MVT BitCastToType = VA.getValVT().isVector() && VA.getLocVT() == MVT::i64
1935 ? MVT::v2i64
1936 : VA.getLocVT();
1937 Value = DAG.getNode(ISD::BITCAST, DL, BitCastToType, Value);
1938 // For ELF, this is a short vector argument to be stored to the stack,
1939 // bitcast to v2i64 and then extract first element.
1940 if (BitCastToType == MVT::v2i64)
1941 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1942 DAG.getConstant(0, DL, MVT::i32));
1943 return Value;
1944 }
1945 case CCValAssign::Full:
1946 return Value;
1947 default:
1948 llvm_unreachable("Unhandled getLocInfo()");
1949 }
1950}
1951
1953 SDLoc DL(In);
1954 SDValue Lo, Hi;
1955 if (DAG.getTargetLoweringInfo().isTypeLegal(MVT::i128)) {
1956 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i64, In);
1957 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i64,
1958 DAG.getNode(ISD::SRL, DL, MVT::i128, In,
1959 DAG.getConstant(64, DL, MVT::i32)));
1960 } else {
1961 std::tie(Lo, Hi) = DAG.SplitScalar(In, DL, MVT::i64, MVT::i64);
1962 }
1963
1964 // FIXME: If v2i64 were a legal type, we could use it instead of
1965 // Untyped here. This might enable improved folding.
1966 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
1967 MVT::Untyped, Hi, Lo);
1968 return SDValue(Pair, 0);
1969}
1970
1972 SDLoc DL(In);
1973 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
1974 DL, MVT::i64, In);
1975 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
1976 DL, MVT::i64, In);
1977
1978 if (DAG.getTargetLoweringInfo().isTypeLegal(MVT::i128)) {
1979 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i128, Lo);
1980 Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i128, Hi);
1981 Hi = DAG.getNode(ISD::SHL, DL, MVT::i128, Hi,
1982 DAG.getConstant(64, DL, MVT::i32));
1983 return DAG.getNode(ISD::OR, DL, MVT::i128, Lo, Hi);
1984 } else {
1985 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
1986 }
1987}
1988
1990 SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts,
1991 unsigned NumParts, MVT PartVT, std::optional<CallingConv::ID> CC) const {
1992 EVT ValueVT = Val.getValueType();
1993 if (ValueVT.getSizeInBits() == 128 && NumParts == 1 && PartVT == MVT::Untyped) {
1994 // Inline assembly operand.
1995 Parts[0] = lowerI128ToGR128(DAG, DAG.getBitcast(MVT::i128, Val));
1996 return true;
1997 }
1998
1999 return false;
2000}
2001
2003 SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts,
2004 MVT PartVT, EVT ValueVT, std::optional<CallingConv::ID> CC) const {
2005 if (ValueVT.getSizeInBits() == 128 && NumParts == 1 && PartVT == MVT::Untyped) {
2006 // Inline assembly operand.
2007 SDValue Res = lowerGR128ToI128(DAG, Parts[0]);
2008 return DAG.getBitcast(ValueVT, Res);
2009 }
2010
2011 return SDValue();
2012}
2013
2014// The first part of a split stack argument is at index I in Args (and
2015// ArgLocs). Return the type of a part and the number of them by reference.
2016template <class ArgTy>
2018 SmallVector<CCValAssign, 16> &ArgLocs, unsigned I,
2019 MVT &PartVT, unsigned &NumParts) {
2020 if (!Args[I].Flags.isSplit())
2021 return false;
2022 assert(I < ArgLocs.size() && ArgLocs.size() == Args.size() &&
2023 "ArgLocs havoc.");
2024 PartVT = ArgLocs[I].getValVT();
2025 NumParts = 1;
2026 for (unsigned PartIdx = I + 1;; ++PartIdx) {
2027 assert(PartIdx != ArgLocs.size() && "SplitEnd not found.");
2028 assert(ArgLocs[PartIdx].getValVT() == PartVT && "Unsupported split.");
2029 ++NumParts;
2030 if (Args[PartIdx].Flags.isSplitEnd())
2031 break;
2032 }
2033 return true;
2034}
2035
2037 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
2038 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2039 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
2041 MachineFrameInfo &MFI = MF.getFrameInfo();
2042 MachineRegisterInfo &MRI = MF.getRegInfo();
2043 SystemZMachineFunctionInfo *FuncInfo =
2045 auto *TFL = Subtarget.getFrameLowering<SystemZELFFrameLowering>();
2046 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2047
2048 // Assign locations to all of the incoming arguments.
2050 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
2051 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
2052 FuncInfo->setSizeOfFnParams(CCInfo.getStackSize());
2053
2054 unsigned NumFixedGPRs = 0;
2055 unsigned NumFixedFPRs = 0;
2056 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
2057 SDValue ArgValue;
2058 CCValAssign &VA = ArgLocs[I];
2059 EVT LocVT = VA.getLocVT();
2060 if (VA.isRegLoc()) {
2061 // Arguments passed in registers
2062 const TargetRegisterClass *RC;
2063 switch (LocVT.getSimpleVT().SimpleTy) {
2064 default:
2065 // Integers smaller than i64 should be promoted to i64.
2066 llvm_unreachable("Unexpected argument type");
2067 case MVT::i32:
2068 NumFixedGPRs += 1;
2069 RC = &SystemZ::GR32BitRegClass;
2070 break;
2071 case MVT::i64:
2072 NumFixedGPRs += 1;
2073 RC = &SystemZ::GR64BitRegClass;
2074 break;
2075 case MVT::f16:
2076 NumFixedFPRs += 1;
2077 RC = &SystemZ::FP16BitRegClass;
2078 break;
2079 case MVT::f32:
2080 NumFixedFPRs += 1;
2081 RC = &SystemZ::FP32BitRegClass;
2082 break;
2083 case MVT::f64:
2084 NumFixedFPRs += 1;
2085 RC = &SystemZ::FP64BitRegClass;
2086 break;
2087 case MVT::f128:
2088 NumFixedFPRs += 2;
2089 RC = &SystemZ::FP128BitRegClass;
2090 break;
2091 case MVT::v16i8:
2092 case MVT::v8i16:
2093 case MVT::v4i32:
2094 case MVT::v2i64:
2095 case MVT::v8f16:
2096 case MVT::v4f32:
2097 case MVT::v2f64:
2098 RC = &SystemZ::VR128BitRegClass;
2099 break;
2100 }
2101
2102 Register VReg = MRI.createVirtualRegister(RC);
2103 MRI.addLiveIn(VA.getLocReg(), VReg);
2104 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
2105 } else {
2106 assert(VA.isMemLoc() && "Argument not register or memory");
2107
2108 // Create the frame index object for this incoming parameter.
2109 // FIXME: Pre-include call frame size in the offset, should not
2110 // need to manually add it here.
2111 int64_t ArgSPOffset = VA.getLocMemOffset();
2112 if (Subtarget.isTargetXPLINK64()) {
2113 auto &XPRegs =
2114 Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
2115 ArgSPOffset += XPRegs.getCallFrameSize();
2116 }
2117 int FI =
2118 MFI.CreateFixedObject(LocVT.getSizeInBits() / 8, ArgSPOffset, true);
2119
2120 // Create the SelectionDAG nodes corresponding to a load
2121 // from this parameter. Unpromoted ints and floats are
2122 // passed as right-justified 8-byte values.
2123 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2124 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32 ||
2125 VA.getLocVT() == MVT::f16) {
2126 unsigned SlotOffs = VA.getLocVT() == MVT::f16 ? 6 : 4;
2127 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
2128 DAG.getIntPtrConstant(SlotOffs, DL));
2129 }
2130 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
2132 }
2133
2134 // Convert the value of the argument register into the value that's
2135 // being passed.
2136 if (VA.getLocInfo() == CCValAssign::Indirect) {
2137 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
2139 // If the original argument was split (e.g. i128), we need
2140 // to load all parts of it here (using the same address).
2141 MVT PartVT;
2142 unsigned NumParts;
2143 if (analyzeArgSplit(Ins, ArgLocs, I, PartVT, NumParts)) {
2144 for (unsigned PartIdx = 1; PartIdx < NumParts; ++PartIdx) {
2145 ++I;
2146 CCValAssign &PartVA = ArgLocs[I];
2147 unsigned PartOffset = Ins[I].PartOffset;
2148 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
2149 DAG.getIntPtrConstant(PartOffset, DL));
2150 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
2152 assert(PartOffset && "Offset should be non-zero.");
2153 }
2154 }
2155 } else
2156 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
2157 }
2158
2159 if (IsVarArg && Subtarget.isTargetXPLINK64()) {
2160 // Save the number of non-varargs registers for later use by va_start, etc.
2161 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
2162 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
2163
2164 auto *Regs = static_cast<SystemZXPLINK64Registers *>(
2165 Subtarget.getSpecialRegisters());
2166
2167 // Likewise the address (in the form of a frame index) of where the
2168 // first stack vararg would be. The 1-byte size here is arbitrary.
2169 // FIXME: Pre-include call frame size in the offset, should not
2170 // need to manually add it here.
2171 int64_t VarArgOffset = CCInfo.getStackSize() + Regs->getCallFrameSize();
2172 int FI = MFI.CreateFixedObject(1, VarArgOffset, true);
2173 FuncInfo->setVarArgsFrameIndex(FI);
2174 }
2175
2176 if (IsVarArg && Subtarget.isTargetELF()) {
2177 // Save the number of non-varargs registers for later use by va_start, etc.
2178 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
2179 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
2180
2181 // Likewise the address (in the form of a frame index) of where the
2182 // first stack vararg would be. The 1-byte size here is arbitrary.
2183 int64_t VarArgsOffset = CCInfo.getStackSize();
2184 FuncInfo->setVarArgsFrameIndex(
2185 MFI.CreateFixedObject(1, VarArgsOffset, true));
2186
2187 // ...and a similar frame index for the caller-allocated save area
2188 // that will be used to store the incoming registers.
2189 int64_t RegSaveOffset =
2190 -SystemZMC::ELFCallFrameSize + TFL->getRegSpillOffset(MF, SystemZ::R2D) - 16;
2191 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
2192 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
2193
2194 // Store the FPR varargs in the reserved frame slots. (We store the
2195 // GPRs as part of the prologue.)
2196 if (NumFixedFPRs < SystemZ::ELFNumArgFPRs && !useSoftFloat()) {
2198 for (unsigned I = NumFixedFPRs; I < SystemZ::ELFNumArgFPRs; ++I) {
2199 unsigned Offset = TFL->getRegSpillOffset(MF, SystemZ::ELFArgFPRs[I]);
2200 int FI =
2202 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2204 &SystemZ::FP64BitRegClass);
2205 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
2206 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
2208 }
2209 // Join the stores, which are independent of one another.
2210 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
2211 ArrayRef(&MemOps[NumFixedFPRs],
2212 SystemZ::ELFNumArgFPRs - NumFixedFPRs));
2213 }
2214 }
2215
2216 if (Subtarget.isTargetXPLINK64()) {
2217 // Create virual register for handling incoming "ADA" special register (R5)
2218 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
2219 Register ADAvReg = MRI.createVirtualRegister(RC);
2220 auto *Regs = static_cast<SystemZXPLINK64Registers *>(
2221 Subtarget.getSpecialRegisters());
2222 MRI.addLiveIn(Regs->getADARegister(), ADAvReg);
2223 FuncInfo->setADAVirtualRegister(ADAvReg);
2224 }
2225 return Chain;
2226}
2227
2228static bool canUseSiblingCall(const CCState &ArgCCInfo,
2231 // Punt if there are any indirect or stack arguments, or if the call
2232 // needs the callee-saved argument register R6, or if the call uses
2233 // the callee-saved register arguments SwiftSelf and SwiftError.
2234 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
2235 CCValAssign &VA = ArgLocs[I];
2237 return false;
2238 if (!VA.isRegLoc())
2239 return false;
2240 Register Reg = VA.getLocReg();
2241 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
2242 return false;
2243 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
2244 return false;
2245 }
2246 return true;
2247}
2248
2250 unsigned Offset, bool LoadAdr = false) {
2253 Register ADAvReg = MFI->getADAVirtualRegister();
2255
2256 SDValue Reg = DAG.getRegister(ADAvReg, PtrVT);
2257 SDValue Ofs = DAG.getTargetConstant(Offset, DL, PtrVT);
2258
2259 SDValue Result = DAG.getNode(SystemZISD::ADA_ENTRY, DL, PtrVT, Val, Reg, Ofs);
2260 if (!LoadAdr)
2261 Result = DAG.getLoad(
2262 PtrVT, DL, DAG.getEntryNode(), Result, MachinePointerInfo(), Align(8),
2264
2265 return Result;
2266}
2267
2268// ADA access using Global value
2269// Note: for functions, address of descriptor is returned
2271 EVT PtrVT) {
2272 unsigned ADAtype;
2273 bool LoadAddr = false;
2274 const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV);
2275 bool IsFunction =
2276 (isa<Function>(GV)) || (GA && isa<Function>(GA->getAliaseeObject()));
2277 bool IsInternal = (GV->hasInternalLinkage() || GV->hasPrivateLinkage());
2278
2279 if (IsFunction) {
2280 if (IsInternal) {
2282 LoadAddr = true;
2283 } else
2285 } else {
2287 }
2288 SDValue Val = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, ADAtype);
2289
2290 return getADAEntry(DAG, Val, DL, 0, LoadAddr);
2291}
2292
2293static bool getzOSCalleeAndADA(SelectionDAG &DAG, SDValue &Callee, SDValue &ADA,
2294 SDLoc &DL, SDValue &Chain) {
2295 unsigned ADADelta = 0; // ADA offset in desc.
2296 unsigned EPADelta = 8; // EPA offset in desc.
2299
2300 // XPLink calling convention.
2301 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2302 bool IsInternal = (G->getGlobal()->hasInternalLinkage() ||
2303 G->getGlobal()->hasPrivateLinkage());
2304 if (IsInternal) {
2307 Register ADAvReg = MFI->getADAVirtualRegister();
2308 ADA = DAG.getCopyFromReg(Chain, DL, ADAvReg, PtrVT);
2309 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
2310 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
2311 return true;
2312 } else {
2314 G->getGlobal(), DL, PtrVT, 0, SystemZII::MO_ADA_DIRECT_FUNC_DESC);
2315 ADA = getADAEntry(DAG, GA, DL, ADADelta);
2316 Callee = getADAEntry(DAG, GA, DL, EPADelta);
2317 }
2318 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2320 E->getSymbol(), PtrVT, SystemZII::MO_ADA_DIRECT_FUNC_DESC);
2321 ADA = getADAEntry(DAG, ES, DL, ADADelta);
2322 Callee = getADAEntry(DAG, ES, DL, EPADelta);
2323 } else {
2324 // Function pointer case
2325 ADA = DAG.getNode(ISD::ADD, DL, PtrVT, Callee,
2326 DAG.getConstant(ADADelta, DL, PtrVT));
2327 ADA = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), ADA,
2329 Callee = DAG.getNode(ISD::ADD, DL, PtrVT, Callee,
2330 DAG.getConstant(EPADelta, DL, PtrVT));
2331 Callee = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Callee,
2333 }
2334 return false;
2335}
2336
2337SDValue
2339 SmallVectorImpl<SDValue> &InVals) const {
2340 SelectionDAG &DAG = CLI.DAG;
2341 SDLoc &DL = CLI.DL;
2343 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2345 SDValue Chain = CLI.Chain;
2346 SDValue Callee = CLI.Callee;
2347 bool &IsTailCall = CLI.IsTailCall;
2348 CallingConv::ID CallConv = CLI.CallConv;
2349 bool IsVarArg = CLI.IsVarArg;
2351 EVT PtrVT = getPointerTy(MF.getDataLayout());
2352 LLVMContext &Ctx = *DAG.getContext();
2353 SystemZCallingConventionRegisters *Regs = Subtarget.getSpecialRegisters();
2354
2355 // FIXME: z/OS support to be added in later.
2356 if (Subtarget.isTargetXPLINK64())
2357 IsTailCall = false;
2358
2359 // Integer args <=32 bits should have an extension attribute.
2360 verifyNarrowIntegerArgs_Call(Outs, &MF.getFunction(), Callee);
2361
2362 // Analyze the operands of the call, assigning locations to each operand.
2364 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, Ctx);
2365 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
2366
2367 // We don't support GuaranteedTailCallOpt, only automatically-detected
2368 // sibling calls.
2369 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
2370 IsTailCall = false;
2371
2372 // Get a count of how many bytes are to be pushed on the stack.
2373 unsigned NumBytes = ArgCCInfo.getStackSize();
2374
2375 // Mark the start of the call.
2376 if (!IsTailCall)
2377 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
2378
2379 // Copy argument values to their designated locations.
2381 SmallVector<SDValue, 8> MemOpChains;
2382 SDValue StackPtr;
2383 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
2384 CCValAssign &VA = ArgLocs[I];
2385 SDValue ArgValue = OutVals[I];
2386
2387 if (VA.getLocInfo() == CCValAssign::Indirect) {
2388 // Store the argument in a stack slot and pass its address.
2389 EVT SlotVT;
2390 MVT PartVT;
2391 unsigned NumParts = 1;
2392 if (analyzeArgSplit(Outs, ArgLocs, I, PartVT, NumParts))
2393 SlotVT = EVT::getIntegerVT(Ctx, PartVT.getSizeInBits() * NumParts);
2394 else
2395 SlotVT = Outs[I].VT;
2396 SDValue SpillSlot = DAG.CreateStackTemporary(SlotVT);
2397 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
2398
2399 MachinePointerInfo StackPtrInfo =
2401 MemOpChains.push_back(
2402 DAG.getStore(Chain, DL, ArgValue, SpillSlot, StackPtrInfo));
2403 // If the original argument was split (e.g. i128), we need
2404 // to store all parts of it here (and pass just one address).
2405 assert(Outs[I].PartOffset == 0);
2406 for (unsigned PartIdx = 1; PartIdx < NumParts; ++PartIdx) {
2407 ++I;
2408 SDValue PartValue = OutVals[I];
2409 unsigned PartOffset = Outs[I].PartOffset;
2410 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
2411 DAG.getIntPtrConstant(PartOffset, DL));
2412 MemOpChains.push_back(
2413 DAG.getStore(Chain, DL, PartValue, Address,
2414 StackPtrInfo.getWithOffset(PartOffset)));
2415 assert(PartOffset && "Offset should be non-zero.");
2416 assert((PartOffset + PartValue.getValueType().getStoreSize() <=
2417 SlotVT.getStoreSize()) && "Not enough space for argument part!");
2418 }
2419 ArgValue = SpillSlot;
2420 } else
2421 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
2422
2423 if (VA.isRegLoc()) {
2424 // In XPLINK64, for the 128-bit vararg case, ArgValue is bitcasted to a
2425 // MVT::i128 type. We decompose the 128-bit type to a pair of its high
2426 // and low values.
2427 if (VA.getLocVT() == MVT::i128)
2428 ArgValue = lowerI128ToGR128(DAG, ArgValue);
2429 // Queue up the argument copies and emit them at the end.
2430 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
2431 } else {
2432 assert(VA.isMemLoc() && "Argument not register or memory");
2433
2434 // Work out the address of the stack slot. Unpromoted ints and
2435 // floats are passed as right-justified 8-byte values.
2436 if (!StackPtr.getNode())
2437 StackPtr = DAG.getCopyFromReg(Chain, DL,
2438 Regs->getStackPointerRegister(), PtrVT);
2439 unsigned Offset = Regs->getStackPointerBias() + Regs->getCallFrameSize() +
2440 VA.getLocMemOffset();
2441 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
2442 Offset += 4;
2443 else if (VA.getLocVT() == MVT::f16)
2444 Offset += 6;
2445 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
2447
2448 // Emit the store.
2449 MemOpChains.push_back(
2450 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
2451
2452 // Although long doubles or vectors are passed through the stack when
2453 // they are vararg (non-fixed arguments), if a long double or vector
2454 // occupies the third and fourth slot of the argument list GPR3 should
2455 // still shadow the third slot of the argument list.
2456 if (Subtarget.isTargetXPLINK64() && VA.needsCustom()) {
2457 SDValue ShadowArgValue =
2458 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, ArgValue,
2459 DAG.getIntPtrConstant(1, DL));
2460 RegsToPass.push_back(std::make_pair(SystemZ::R3D, ShadowArgValue));
2461 }
2462 }
2463 }
2464
2465 // Join the stores, which are independent of one another.
2466 if (!MemOpChains.empty())
2467 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
2468
2469 // Accept direct calls by converting symbolic call addresses to the
2470 // associated Target* opcodes. Force %r1 to be used for indirect
2471 // tail calls.
2472 SDValue Glue;
2473
2474 if (Subtarget.isTargetXPLINK64()) {
2475 SDValue ADA;
2476 bool IsBRASL = getzOSCalleeAndADA(DAG, Callee, ADA, DL, Chain);
2477 if (!IsBRASL) {
2478 unsigned CalleeReg = static_cast<SystemZXPLINK64Registers *>(Regs)
2479 ->getAddressOfCalleeRegister();
2480 Chain = DAG.getCopyToReg(Chain, DL, CalleeReg, Callee, Glue);
2481 Glue = Chain.getValue(1);
2482 Callee = DAG.getRegister(CalleeReg, Callee.getValueType());
2483 }
2484 RegsToPass.push_back(std::make_pair(
2485 static_cast<SystemZXPLINK64Registers *>(Regs)->getADARegister(), ADA));
2486 } else {
2487 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2488 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
2489 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
2490 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2491 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
2492 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
2493 } else if (IsTailCall) {
2494 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
2495 Glue = Chain.getValue(1);
2496 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
2497 }
2498 }
2499
2500 // Build a sequence of copy-to-reg nodes, chained and glued together.
2501 for (const auto &[Reg, N] : RegsToPass) {
2502 Chain = DAG.getCopyToReg(Chain, DL, Reg, N, Glue);
2503 Glue = Chain.getValue(1);
2504 }
2505
2506 // The first call operand is the chain and the second is the target address.
2508 Ops.push_back(Chain);
2509 Ops.push_back(Callee);
2510
2511 // Add argument registers to the end of the list so that they are
2512 // known live into the call.
2513 for (const auto &[Reg, N] : RegsToPass)
2514 Ops.push_back(DAG.getRegister(Reg, N.getValueType()));
2515
2516 // Add a register mask operand representing the call-preserved registers.
2517 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
2518 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
2519 assert(Mask && "Missing call preserved mask for calling convention");
2520 Ops.push_back(DAG.getRegisterMask(Mask));
2521
2522 // Glue the call to the argument copies, if any.
2523 if (Glue.getNode())
2524 Ops.push_back(Glue);
2525
2526 // Emit the call.
2527 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2528 if (IsTailCall) {
2529 SDValue Ret = DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
2530 DAG.addNoMergeSiteInfo(Ret.getNode(), CLI.NoMerge);
2531 return Ret;
2532 }
2533 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
2534 DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
2535 Glue = Chain.getValue(1);
2536
2537 // Mark the end of the call, which is glued to the call itself.
2538 Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, Glue, DL);
2539 Glue = Chain.getValue(1);
2540
2541 // Assign locations to each value returned by this call.
2543 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, Ctx);
2544 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
2545
2546 // Copy all of the result registers out of their specified physreg.
2547 for (CCValAssign &VA : RetLocs) {
2548 // Copy the value out, gluing the copy to the end of the call sequence.
2549 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
2550 VA.getLocVT(), Glue);
2551 Chain = RetValue.getValue(1);
2552 Glue = RetValue.getValue(2);
2553
2554 // Convert the value of the return register into the value that's
2555 // being returned.
2556 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
2557 }
2558
2559 return Chain;
2560}
2561
2562// Generate a call taking the given operands as arguments and returning a
2563// result of type RetVT.
2565 SDValue Chain, SelectionDAG &DAG, const char *CalleeName, EVT RetVT,
2566 ArrayRef<SDValue> Ops, CallingConv::ID CallConv, bool IsSigned, SDLoc DL,
2567 bool DoesNotReturn, bool IsReturnValueUsed) const {
2569 Args.reserve(Ops.size());
2570
2571 for (SDValue Op : Ops) {
2573 Op, Op.getValueType().getTypeForEVT(*DAG.getContext()));
2574 Entry.IsSExt = shouldSignExtendTypeInLibCall(Entry.Ty, IsSigned);
2575 Entry.IsZExt = !Entry.IsSExt;
2576 Args.push_back(Entry);
2577 }
2578
2579 SDValue Callee =
2580 DAG.getExternalSymbol(CalleeName, getPointerTy(DAG.getDataLayout()));
2581
2582 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2584 bool SignExtend = shouldSignExtendTypeInLibCall(RetTy, IsSigned);
2585 CLI.setDebugLoc(DL)
2586 .setChain(Chain)
2587 .setCallee(CallConv, RetTy, Callee, std::move(Args))
2588 .setNoReturn(DoesNotReturn)
2589 .setDiscardResult(!IsReturnValueUsed)
2590 .setSExtResult(SignExtend)
2591 .setZExtResult(!SignExtend);
2592 return LowerCallTo(CLI);
2593}
2594
2596 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
2597 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
2598 const Type *RetTy) const {
2599 // Special case that we cannot easily detect in RetCC_SystemZ since
2600 // i128 may not be a legal type.
2601 for (auto &Out : Outs)
2602 if (Out.ArgVT.isScalarInteger() && Out.ArgVT.getSizeInBits() > 64)
2603 return false;
2604
2606 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, Context);
2607 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
2608}
2609
2610SDValue
2612 bool IsVarArg,
2614 const SmallVectorImpl<SDValue> &OutVals,
2615 const SDLoc &DL, SelectionDAG &DAG) const {
2617
2618 // Integer args <=32 bits should have an extension attribute.
2619 verifyNarrowIntegerArgs_Ret(Outs, &MF.getFunction());
2620
2621 // Assign locations to each returned value.
2623 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
2624 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
2625
2626 // Quick exit for void returns
2627 if (RetLocs.empty())
2628 return DAG.getNode(SystemZISD::RET_GLUE, DL, MVT::Other, Chain);
2629
2630 if (CallConv == CallingConv::GHC)
2631 report_fatal_error("GHC functions return void only");
2632
2633 // Copy the result values into the output registers.
2634 SDValue Glue;
2636 RetOps.push_back(Chain);
2637 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
2638 CCValAssign &VA = RetLocs[I];
2639 SDValue RetValue = OutVals[I];
2640
2641 // Make the return register live on exit.
2642 assert(VA.isRegLoc() && "Can only return in registers!");
2643
2644 // Promote the value as required.
2645 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
2646
2647 // Chain and glue the copies together.
2648 Register Reg = VA.getLocReg();
2649 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
2650 Glue = Chain.getValue(1);
2651 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
2652 }
2653
2654 // Update chain and glue.
2655 RetOps[0] = Chain;
2656 if (Glue.getNode())
2657 RetOps.push_back(Glue);
2658
2659 return DAG.getNode(SystemZISD::RET_GLUE, DL, MVT::Other, RetOps);
2660}
2661
2662// Return true if Op is an intrinsic node with chain that returns the CC value
2663// as its only (other) argument. Provide the associated SystemZISD opcode and
2664// the mask of valid CC values if so.
2665static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
2666 unsigned &CCValid) {
2667 unsigned Id = Op.getConstantOperandVal(1);
2668 switch (Id) {
2669 case Intrinsic::s390_tbegin:
2670 Opcode = SystemZISD::TBEGIN;
2671 CCValid = SystemZ::CCMASK_TBEGIN;
2672 return true;
2673
2674 case Intrinsic::s390_tbegin_nofloat:
2675 Opcode = SystemZISD::TBEGIN_NOFLOAT;
2676 CCValid = SystemZ::CCMASK_TBEGIN;
2677 return true;
2678
2679 case Intrinsic::s390_tend:
2680 Opcode = SystemZISD::TEND;
2681 CCValid = SystemZ::CCMASK_TEND;
2682 return true;
2683
2684 default:
2685 return false;
2686 }
2687}
2688
2689// Return true if Op is an intrinsic node without chain that returns the
2690// CC value as its final argument. Provide the associated SystemZISD
2691// opcode and the mask of valid CC values if so.
2692static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
2693 unsigned Id = Op.getConstantOperandVal(0);
2694 switch (Id) {
2695 case Intrinsic::s390_vpkshs:
2696 case Intrinsic::s390_vpksfs:
2697 case Intrinsic::s390_vpksgs:
2698 Opcode = SystemZISD::PACKS_CC;
2699 CCValid = SystemZ::CCMASK_VCMP;
2700 return true;
2701
2702 case Intrinsic::s390_vpklshs:
2703 case Intrinsic::s390_vpklsfs:
2704 case Intrinsic::s390_vpklsgs:
2705 Opcode = SystemZISD::PACKLS_CC;
2706 CCValid = SystemZ::CCMASK_VCMP;
2707 return true;
2708
2709 case Intrinsic::s390_vceqbs:
2710 case Intrinsic::s390_vceqhs:
2711 case Intrinsic::s390_vceqfs:
2712 case Intrinsic::s390_vceqgs:
2713 case Intrinsic::s390_vceqqs:
2714 Opcode = SystemZISD::VICMPES;
2715 CCValid = SystemZ::CCMASK_VCMP;
2716 return true;
2717
2718 case Intrinsic::s390_vchbs:
2719 case Intrinsic::s390_vchhs:
2720 case Intrinsic::s390_vchfs:
2721 case Intrinsic::s390_vchgs:
2722 case Intrinsic::s390_vchqs:
2723 Opcode = SystemZISD::VICMPHS;
2724 CCValid = SystemZ::CCMASK_VCMP;
2725 return true;
2726
2727 case Intrinsic::s390_vchlbs:
2728 case Intrinsic::s390_vchlhs:
2729 case Intrinsic::s390_vchlfs:
2730 case Intrinsic::s390_vchlgs:
2731 case Intrinsic::s390_vchlqs:
2732 Opcode = SystemZISD::VICMPHLS;
2733 CCValid = SystemZ::CCMASK_VCMP;
2734 return true;
2735
2736 case Intrinsic::s390_vtm:
2737 Opcode = SystemZISD::VTM;
2738 CCValid = SystemZ::CCMASK_VCMP;
2739 return true;
2740
2741 case Intrinsic::s390_vfaebs:
2742 case Intrinsic::s390_vfaehs:
2743 case Intrinsic::s390_vfaefs:
2744 Opcode = SystemZISD::VFAE_CC;
2745 CCValid = SystemZ::CCMASK_ANY;
2746 return true;
2747
2748 case Intrinsic::s390_vfaezbs:
2749 case Intrinsic::s390_vfaezhs:
2750 case Intrinsic::s390_vfaezfs:
2751 Opcode = SystemZISD::VFAEZ_CC;
2752 CCValid = SystemZ::CCMASK_ANY;
2753 return true;
2754
2755 case Intrinsic::s390_vfeebs:
2756 case Intrinsic::s390_vfeehs:
2757 case Intrinsic::s390_vfeefs:
2758 Opcode = SystemZISD::VFEE_CC;
2759 CCValid = SystemZ::CCMASK_ANY;
2760 return true;
2761
2762 case Intrinsic::s390_vfeezbs:
2763 case Intrinsic::s390_vfeezhs:
2764 case Intrinsic::s390_vfeezfs:
2765 Opcode = SystemZISD::VFEEZ_CC;
2766 CCValid = SystemZ::CCMASK_ANY;
2767 return true;
2768
2769 case Intrinsic::s390_vfenebs:
2770 case Intrinsic::s390_vfenehs:
2771 case Intrinsic::s390_vfenefs:
2772 Opcode = SystemZISD::VFENE_CC;
2773 CCValid = SystemZ::CCMASK_ANY;
2774 return true;
2775
2776 case Intrinsic::s390_vfenezbs:
2777 case Intrinsic::s390_vfenezhs:
2778 case Intrinsic::s390_vfenezfs:
2779 Opcode = SystemZISD::VFENEZ_CC;
2780 CCValid = SystemZ::CCMASK_ANY;
2781 return true;
2782
2783 case Intrinsic::s390_vistrbs:
2784 case Intrinsic::s390_vistrhs:
2785 case Intrinsic::s390_vistrfs:
2786 Opcode = SystemZISD::VISTR_CC;
2788 return true;
2789
2790 case Intrinsic::s390_vstrcbs:
2791 case Intrinsic::s390_vstrchs:
2792 case Intrinsic::s390_vstrcfs:
2793 Opcode = SystemZISD::VSTRC_CC;
2794 CCValid = SystemZ::CCMASK_ANY;
2795 return true;
2796
2797 case Intrinsic::s390_vstrczbs:
2798 case Intrinsic::s390_vstrczhs:
2799 case Intrinsic::s390_vstrczfs:
2800 Opcode = SystemZISD::VSTRCZ_CC;
2801 CCValid = SystemZ::CCMASK_ANY;
2802 return true;
2803
2804 case Intrinsic::s390_vstrsb:
2805 case Intrinsic::s390_vstrsh:
2806 case Intrinsic::s390_vstrsf:
2807 Opcode = SystemZISD::VSTRS_CC;
2808 CCValid = SystemZ::CCMASK_ANY;
2809 return true;
2810
2811 case Intrinsic::s390_vstrszb:
2812 case Intrinsic::s390_vstrszh:
2813 case Intrinsic::s390_vstrszf:
2814 Opcode = SystemZISD::VSTRSZ_CC;
2815 CCValid = SystemZ::CCMASK_ANY;
2816 return true;
2817
2818 case Intrinsic::s390_vfcedbs:
2819 case Intrinsic::s390_vfcesbs:
2820 Opcode = SystemZISD::VFCMPES;
2821 CCValid = SystemZ::CCMASK_VCMP;
2822 return true;
2823
2824 case Intrinsic::s390_vfchdbs:
2825 case Intrinsic::s390_vfchsbs:
2826 Opcode = SystemZISD::VFCMPHS;
2827 CCValid = SystemZ::CCMASK_VCMP;
2828 return true;
2829
2830 case Intrinsic::s390_vfchedbs:
2831 case Intrinsic::s390_vfchesbs:
2832 Opcode = SystemZISD::VFCMPHES;
2833 CCValid = SystemZ::CCMASK_VCMP;
2834 return true;
2835
2836 case Intrinsic::s390_vftcidb:
2837 case Intrinsic::s390_vftcisb:
2838 Opcode = SystemZISD::VFTCI;
2839 CCValid = SystemZ::CCMASK_VCMP;
2840 return true;
2841
2842 case Intrinsic::s390_tdc:
2843 Opcode = SystemZISD::TDC;
2844 CCValid = SystemZ::CCMASK_TDC;
2845 return true;
2846
2847 default:
2848 return false;
2849 }
2850}
2851
2852// Emit an intrinsic with chain and an explicit CC register result.
2854 unsigned Opcode) {
2855 // Copy all operands except the intrinsic ID.
2856 unsigned NumOps = Op.getNumOperands();
2858 Ops.reserve(NumOps - 1);
2859 Ops.push_back(Op.getOperand(0));
2860 for (unsigned I = 2; I < NumOps; ++I)
2861 Ops.push_back(Op.getOperand(I));
2862
2863 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
2864 SDVTList RawVTs = DAG.getVTList(MVT::i32, MVT::Other);
2865 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
2866 SDValue OldChain = SDValue(Op.getNode(), 1);
2867 SDValue NewChain = SDValue(Intr.getNode(), 1);
2868 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
2869 return Intr.getNode();
2870}
2871
2872// Emit an intrinsic with an explicit CC register result.
2874 unsigned Opcode) {
2875 // Copy all operands except the intrinsic ID.
2876 SDLoc DL(Op);
2877 unsigned NumOps = Op.getNumOperands();
2879 Ops.reserve(NumOps - 1);
2880 for (unsigned I = 1; I < NumOps; ++I) {
2881 SDValue CurrOper = Op.getOperand(I);
2882 if (CurrOper.getValueType() == MVT::f16) {
2883 assert((Op.getConstantOperandVal(0) == Intrinsic::s390_tdc && I == 1) &&
2884 "Unhandled intrinsic with f16 operand.");
2885 CurrOper = DAG.getFPExtendOrRound(CurrOper, DL, MVT::f32);
2886 }
2887 Ops.push_back(CurrOper);
2888 }
2889
2890 SDValue Intr = DAG.getNode(Opcode, DL, Op->getVTList(), Ops);
2891 return Intr.getNode();
2892}
2893
2894// CC is a comparison that will be implemented using an integer or
2895// floating-point comparison. Return the condition code mask for
2896// a branch on true. In the integer case, CCMASK_CMP_UO is set for
2897// unsigned comparisons and clear for signed ones. In the floating-point
2898// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
2900#define CONV(X) \
2901 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
2902 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
2903 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
2904
2905 switch (CC) {
2906 default:
2907 llvm_unreachable("Invalid integer condition!");
2908
2909 CONV(EQ);
2910 CONV(NE);
2911 CONV(GT);
2912 CONV(GE);
2913 CONV(LT);
2914 CONV(LE);
2915
2916 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
2918 }
2919#undef CONV
2920}
2921
2922// If C can be converted to a comparison against zero, adjust the operands
2923// as necessary.
2924static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
2925 if (C.ICmpType == SystemZICMP::UnsignedOnly)
2926 return;
2927
2928 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
2929 if (!ConstOp1 || ConstOp1->getValueSizeInBits(0) > 64)
2930 return;
2931
2932 int64_t Value = ConstOp1->getSExtValue();
2933 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
2934 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
2935 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
2936 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
2937 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2938 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
2939 }
2940}
2941
2942// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
2943// adjust the operands as necessary.
2944static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
2945 Comparison &C) {
2946 // For us to make any changes, it must a comparison between a single-use
2947 // load and a constant.
2948 if (!C.Op0.hasOneUse() ||
2949 C.Op0.getOpcode() != ISD::LOAD ||
2950 C.Op1.getOpcode() != ISD::Constant)
2951 return;
2952
2953 // We must have an 8- or 16-bit load.
2954 auto *Load = cast<LoadSDNode>(C.Op0);
2955 unsigned NumBits = Load->getMemoryVT().getSizeInBits();
2956 if ((NumBits != 8 && NumBits != 16) ||
2957 NumBits != Load->getMemoryVT().getStoreSizeInBits())
2958 return;
2959
2960 // The load must be an extending one and the constant must be within the
2961 // range of the unextended value.
2962 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
2963 if (!ConstOp1 || ConstOp1->getValueSizeInBits(0) > 64)
2964 return;
2965 uint64_t Value = ConstOp1->getZExtValue();
2966 uint64_t Mask = (1 << NumBits) - 1;
2967 if (Load->getExtensionType() == ISD::SEXTLOAD) {
2968 // Make sure that ConstOp1 is in range of C.Op0.
2969 int64_t SignedValue = ConstOp1->getSExtValue();
2970 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
2971 return;
2972 if (C.ICmpType != SystemZICMP::SignedOnly) {
2973 // Unsigned comparison between two sign-extended values is equivalent
2974 // to unsigned comparison between two zero-extended values.
2975 Value &= Mask;
2976 } else if (NumBits == 8) {
2977 // Try to treat the comparison as unsigned, so that we can use CLI.
2978 // Adjust CCMask and Value as necessary.
2979 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
2980 // Test whether the high bit of the byte is set.
2981 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
2982 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
2983 // Test whether the high bit of the byte is clear.
2984 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
2985 else
2986 // No instruction exists for this combination.
2987 return;
2988 C.ICmpType = SystemZICMP::UnsignedOnly;
2989 }
2990 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
2991 if (Value > Mask)
2992 return;
2993 // If the constant is in range, we can use any comparison.
2994 C.ICmpType = SystemZICMP::Any;
2995 } else
2996 return;
2997
2998 // Make sure that the first operand is an i32 of the right extension type.
2999 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
3002 if (C.Op0.getValueType() != MVT::i32 ||
3003 Load->getExtensionType() != ExtType) {
3004 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
3005 Load->getBasePtr(), Load->getPointerInfo(),
3006 Load->getMemoryVT(), Load->getAlign(),
3007 Load->getMemOperand()->getFlags());
3008 // Update the chain uses.
3009 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), C.Op0.getValue(1));
3010 }
3011
3012 // Make sure that the second operand is an i32 with the right value.
3013 if (C.Op1.getValueType() != MVT::i32 ||
3014 Value != ConstOp1->getZExtValue())
3015 C.Op1 = DAG.getConstant((uint32_t)Value, DL, MVT::i32);
3016}
3017
3018// Return true if Op is either an unextended load, or a load suitable
3019// for integer register-memory comparisons of type ICmpType.
3020static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
3021 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
3022 if (Load) {
3023 // There are no instructions to compare a register with a memory byte.
3024 if (Load->getMemoryVT() == MVT::i8)
3025 return false;
3026 // Otherwise decide on extension type.
3027 switch (Load->getExtensionType()) {
3028 case ISD::NON_EXTLOAD:
3029 return true;
3030 case ISD::SEXTLOAD:
3031 return ICmpType != SystemZICMP::UnsignedOnly;
3032 case ISD::ZEXTLOAD:
3033 return ICmpType != SystemZICMP::SignedOnly;
3034 default:
3035 break;
3036 }
3037 }
3038 return false;
3039}
3040
3041// Return true if it is better to swap the operands of C.
3042static bool shouldSwapCmpOperands(const Comparison &C) {
3043 // If one side of the compare is a load of the stackguard reference value,
3044 // then that load should be Op1.
3045 if (C.Op0.isMachineOpcode() &&
3046 (C.Op0.getMachineOpcode() == SystemZ::LOAD_STACK_GUARD))
3047 return true;
3048
3049 // Leave i128 and f128 comparisons alone, since they have no memory forms.
3050 if (C.Op0.getValueType() == MVT::i128)
3051 return false;
3052 if (C.Op0.getValueType() == MVT::f128)
3053 return false;
3054
3055 // Always keep a floating-point constant second, since comparisons with
3056 // zero can use LOAD TEST and comparisons with other constants make a
3057 // natural memory operand.
3058 if (isa<ConstantFPSDNode>(C.Op1))
3059 return false;
3060
3061 // Never swap comparisons with zero since there are many ways to optimize
3062 // those later.
3063 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
3064 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
3065 return false;
3066
3067 // Also keep natural memory operands second if the loaded value is
3068 // only used here. Several comparisons have memory forms.
3069 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
3070 return false;
3071
3072 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
3073 // In that case we generally prefer the memory to be second.
3074 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
3075 // The only exceptions are when the second operand is a constant and
3076 // we can use things like CHHSI.
3077 if (!ConstOp1)
3078 return true;
3079 // The unsigned memory-immediate instructions can handle 16-bit
3080 // unsigned integers.
3081 if (C.ICmpType != SystemZICMP::SignedOnly &&
3082 isUInt<16>(ConstOp1->getZExtValue()))
3083 return false;
3084 // The signed memory-immediate instructions can handle 16-bit
3085 // signed integers.
3086 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
3087 isInt<16>(ConstOp1->getSExtValue()))
3088 return false;
3089 return true;
3090 }
3091
3092 // Try to promote the use of CGFR and CLGFR.
3093 unsigned Opcode0 = C.Op0.getOpcode();
3094 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
3095 return true;
3096 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
3097 return true;
3098 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::AND &&
3099 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
3100 C.Op0.getConstantOperandVal(1) == 0xffffffff)
3101 return true;
3102
3103 return false;
3104}
3105
3106// Check whether C tests for equality between X and Y and whether X - Y
3107// or Y - X is also computed. In that case it's better to compare the
3108// result of the subtraction against zero.
3110 Comparison &C) {
3111 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
3112 C.CCMask == SystemZ::CCMASK_CMP_NE) {
3113 for (SDNode *N : C.Op0->users()) {
3114 if (N->getOpcode() == ISD::SUB &&
3115 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
3116 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
3117 // Disable the nsw and nuw flags: the backend needs to handle
3118 // overflow as well during comparison elimination.
3119 N->dropFlags(SDNodeFlags::NoWrap);
3120 C.Op0 = SDValue(N, 0);
3121 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
3122 return;
3123 }
3124 }
3125 }
3126}
3127
3128// Check whether C compares a floating-point value with zero and if that
3129// floating-point value is also negated. In this case we can use the
3130// negation to set CC, so avoiding separate LOAD AND TEST and
3131// LOAD (NEGATIVE/COMPLEMENT) instructions.
3132static void adjustForFNeg(Comparison &C) {
3133 // This optimization is invalid for strict comparisons, since FNEG
3134 // does not raise any exceptions.
3135 if (C.Chain)
3136 return;
3137 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
3138 if (C1 && C1->isZero()) {
3139 for (SDNode *N : C.Op0->users()) {
3140 if (N->getOpcode() == ISD::FNEG) {
3141 C.Op0 = SDValue(N, 0);
3142 C.CCMask = SystemZ::reverseCCMask(C.CCMask);
3143 return;
3144 }
3145 }
3146 }
3147}
3148
3149// Check whether C compares (shl X, 32) with 0 and whether X is
3150// also sign-extended. In that case it is better to test the result
3151// of the sign extension using LTGFR.
3152//
3153// This case is important because InstCombine transforms a comparison
3154// with (sext (trunc X)) into a comparison with (shl X, 32).
3155static void adjustForLTGFR(Comparison &C) {
3156 // Check for a comparison between (shl X, 32) and 0.
3157 if (C.Op0.getOpcode() == ISD::SHL && C.Op0.getValueType() == MVT::i64 &&
3158 C.Op1.getOpcode() == ISD::Constant && C.Op1->getAsZExtVal() == 0) {
3159 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
3160 if (C1 && C1->getZExtValue() == 32) {
3161 SDValue ShlOp0 = C.Op0.getOperand(0);
3162 // See whether X has any SIGN_EXTEND_INREG uses.
3163 for (SDNode *N : ShlOp0->users()) {
3164 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
3165 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
3166 C.Op0 = SDValue(N, 0);
3167 return;
3168 }
3169 }
3170 }
3171 }
3172}
3173
3174// If C compares the truncation of an extending load, try to compare
3175// the untruncated value instead. This exposes more opportunities to
3176// reuse CC.
3177static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
3178 Comparison &C) {
3179 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
3180 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
3181 C.Op1.getOpcode() == ISD::Constant &&
3182 cast<ConstantSDNode>(C.Op1)->getValueSizeInBits(0) <= 64 &&
3183 C.Op1->getAsZExtVal() == 0) {
3184 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
3185 if (L->getMemoryVT().getStoreSizeInBits().getFixedValue() <=
3186 C.Op0.getValueSizeInBits().getFixedValue()) {
3187 unsigned Type = L->getExtensionType();
3188 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
3189 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
3190 C.Op0 = C.Op0.getOperand(0);
3191 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
3192 }
3193 }
3194 }
3195}
3196
3197// Adjust if a given Compare is a check of the stack guard against a stack
3198// guard instance on the stack. Specifically, this checks if:
3199// - The operands are a load of the stack guard, and a load from a stack slot
3200// - The original opcode is ICMP
3201// - ICMPType is compatible with unsigned comparison.
3203 Comparison &C) {
3204
3205 // Opcode must be ICMP.
3206 if (C.Opcode != SystemZISD::ICMP)
3207 return;
3208 // ICmpType must be Unsigned or Any.
3209 if (C.ICmpType == SystemZICMP::SignedOnly)
3210 return;
3211 // Op0 must be FrameIndex Load.
3212 if (!(ISD::isNormalLoad(C.Op0.getNode()) &&
3213 dyn_cast<FrameIndexSDNode>(C.Op0.getOperand(1))))
3214 return;
3215 // Op1 must be LOAD_STACK_GUARD.
3216 if (!C.Op1.isMachineOpcode() ||
3217 C.Op1.getMachineOpcode() != SystemZ::LOAD_STACK_GUARD)
3218 return;
3219
3220 // At this point we are sure that this is a proper CMP_STACKGUARD
3221 // case, update the opcode to reflect this.
3222 C.Opcode = SystemZISD::CMP_STACKGUARD;
3223 C.Op1 = SDValue();
3224}
3225
3226// Return true if shift operation N has an in-range constant shift value.
3227// Store it in ShiftVal if so.
3228static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
3229 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
3230 if (!Shift)
3231 return false;
3232
3233 uint64_t Amount = Shift->getZExtValue();
3234 if (Amount >= N.getValueSizeInBits())
3235 return false;
3236
3237 ShiftVal = Amount;
3238 return true;
3239}
3240
3241// Check whether an AND with Mask is suitable for a TEST UNDER MASK
3242// instruction and whether the CC value is descriptive enough to handle
3243// a comparison of type Opcode between the AND result and CmpVal.
3244// CCMask says which comparison result is being tested and BitSize is
3245// the number of bits in the operands. If TEST UNDER MASK can be used,
3246// return the corresponding CC mask, otherwise return 0.
3247static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
3248 uint64_t Mask, uint64_t CmpVal,
3249 unsigned ICmpType) {
3250 assert(Mask != 0 && "ANDs with zero should have been removed by now");
3251
3252 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
3253 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
3254 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
3255 return 0;
3256
3257 // Work out the masks for the lowest and highest bits.
3259 uint64_t Low = uint64_t(1) << llvm::countr_zero(Mask);
3260
3261 // Signed ordered comparisons are effectively unsigned if the sign
3262 // bit is dropped.
3263 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
3264
3265 // Check for equality comparisons with 0, or the equivalent.
3266 if (CmpVal == 0) {
3267 if (CCMask == SystemZ::CCMASK_CMP_EQ)
3269 if (CCMask == SystemZ::CCMASK_CMP_NE)
3271 }
3272 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
3273 if (CCMask == SystemZ::CCMASK_CMP_LT)
3275 if (CCMask == SystemZ::CCMASK_CMP_GE)
3277 }
3278 if (EffectivelyUnsigned && CmpVal < Low) {
3279 if (CCMask == SystemZ::CCMASK_CMP_LE)
3281 if (CCMask == SystemZ::CCMASK_CMP_GT)
3283 }
3284
3285 // Check for equality comparisons with the mask, or the equivalent.
3286 if (CmpVal == Mask) {
3287 if (CCMask == SystemZ::CCMASK_CMP_EQ)
3289 if (CCMask == SystemZ::CCMASK_CMP_NE)
3291 }
3292 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
3293 if (CCMask == SystemZ::CCMASK_CMP_GT)
3295 if (CCMask == SystemZ::CCMASK_CMP_LE)
3297 }
3298 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
3299 if (CCMask == SystemZ::CCMASK_CMP_GE)
3301 if (CCMask == SystemZ::CCMASK_CMP_LT)
3303 }
3304
3305 // Check for ordered comparisons with the top bit.
3306 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
3307 if (CCMask == SystemZ::CCMASK_CMP_LE)
3309 if (CCMask == SystemZ::CCMASK_CMP_GT)
3311 }
3312 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
3313 if (CCMask == SystemZ::CCMASK_CMP_LT)
3315 if (CCMask == SystemZ::CCMASK_CMP_GE)
3317 }
3318
3319 // If there are just two bits, we can do equality checks for Low and High
3320 // as well.
3321 if (Mask == Low + High) {
3322 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
3324 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
3326 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
3328 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
3330 }
3331
3332 // Looks like we've exhausted our options.
3333 return 0;
3334}
3335
3336// See whether C can be implemented as a TEST UNDER MASK instruction.
3337// Update the arguments with the TM version if so.
3339 Comparison &C) {
3340 // Use VECTOR TEST UNDER MASK for i128 operations.
3341 if (C.Op0.getValueType() == MVT::i128) {
3342 // We can use VTM for EQ/NE comparisons of x & y against 0.
3343 if (C.Op0.getOpcode() == ISD::AND &&
3344 (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
3345 C.CCMask == SystemZ::CCMASK_CMP_NE)) {
3346 auto *Mask = dyn_cast<ConstantSDNode>(C.Op1);
3347 if (Mask && Mask->getAPIntValue() == 0) {
3348 C.Opcode = SystemZISD::VTM;
3349 C.Op1 = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, C.Op0.getOperand(1));
3350 C.Op0 = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, C.Op0.getOperand(0));
3351 C.CCValid = SystemZ::CCMASK_VCMP;
3352 if (C.CCMask == SystemZ::CCMASK_CMP_EQ)
3353 C.CCMask = SystemZ::CCMASK_VCMP_ALL;
3354 else
3355 C.CCMask = SystemZ::CCMASK_VCMP_ALL ^ C.CCValid;
3356 }
3357 }
3358 return;
3359 }
3360
3361 // Check that we have a comparison with a constant.
3362 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
3363 if (!ConstOp1)
3364 return;
3365 uint64_t CmpVal = ConstOp1->getZExtValue();
3366
3367 // Check whether the nonconstant input is an AND with a constant mask.
3368 Comparison NewC(C);
3369 uint64_t MaskVal;
3370 ConstantSDNode *Mask = nullptr;
3371 if (C.Op0.getOpcode() == ISD::AND) {
3372 NewC.Op0 = C.Op0.getOperand(0);
3373 NewC.Op1 = C.Op0.getOperand(1);
3374 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
3375 if (!Mask)
3376 return;
3377 MaskVal = Mask->getZExtValue();
3378 } else {
3379 // There is no instruction to compare with a 64-bit immediate
3380 // so use TMHH instead if possible. We need an unsigned ordered
3381 // comparison with an i64 immediate.
3382 if (NewC.Op0.getValueType() != MVT::i64 ||
3383 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
3384 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
3385 NewC.ICmpType == SystemZICMP::SignedOnly)
3386 return;
3387 // Convert LE and GT comparisons into LT and GE.
3388 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
3389 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
3390 if (CmpVal == uint64_t(-1))
3391 return;
3392 CmpVal += 1;
3393 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
3394 }
3395 // If the low N bits of Op1 are zero than the low N bits of Op0 can
3396 // be masked off without changing the result.
3397 MaskVal = -(CmpVal & -CmpVal);
3398 NewC.ICmpType = SystemZICMP::UnsignedOnly;
3399 }
3400 if (!MaskVal)
3401 return;
3402
3403 // Check whether the combination of mask, comparison value and comparison
3404 // type are suitable.
3405 unsigned BitSize = NewC.Op0.getValueSizeInBits();
3406 unsigned NewCCMask, ShiftVal;
3407 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
3408 NewC.Op0.getOpcode() == ISD::SHL &&
3409 isSimpleShift(NewC.Op0, ShiftVal) &&
3410 (MaskVal >> ShiftVal != 0) &&
3411 ((CmpVal >> ShiftVal) << ShiftVal) == CmpVal &&
3412 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
3413 MaskVal >> ShiftVal,
3414 CmpVal >> ShiftVal,
3415 SystemZICMP::Any))) {
3416 NewC.Op0 = NewC.Op0.getOperand(0);
3417 MaskVal >>= ShiftVal;
3418 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
3419 NewC.Op0.getOpcode() == ISD::SRL &&
3420 isSimpleShift(NewC.Op0, ShiftVal) &&
3421 (MaskVal << ShiftVal != 0) &&
3422 ((CmpVal << ShiftVal) >> ShiftVal) == CmpVal &&
3423 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
3424 MaskVal << ShiftVal,
3425 CmpVal << ShiftVal,
3427 NewC.Op0 = NewC.Op0.getOperand(0);
3428 MaskVal <<= ShiftVal;
3429 } else {
3430 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
3431 NewC.ICmpType);
3432 if (!NewCCMask)
3433 return;
3434 }
3435
3436 // Go ahead and make the change.
3437 C.Opcode = SystemZISD::TM;
3438 C.Op0 = NewC.Op0;
3439 if (Mask && Mask->getZExtValue() == MaskVal)
3440 C.Op1 = SDValue(Mask, 0);
3441 else
3442 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
3443 C.CCValid = SystemZ::CCMASK_TM;
3444 C.CCMask = NewCCMask;
3445}
3446
3447// Implement i128 comparison in vector registers.
3448static void adjustICmp128(SelectionDAG &DAG, const SDLoc &DL,
3449 Comparison &C) {
3450 if (C.Opcode != SystemZISD::ICMP)
3451 return;
3452 if (C.Op0.getValueType() != MVT::i128)
3453 return;
3454
3455 // Recognize vector comparison reductions.
3456 if ((C.CCMask == SystemZ::CCMASK_CMP_EQ ||
3457 C.CCMask == SystemZ::CCMASK_CMP_NE) &&
3458 (isNullConstant(C.Op1) || isAllOnesConstant(C.Op1))) {
3459 bool CmpEq = C.CCMask == SystemZ::CCMASK_CMP_EQ;
3460 bool CmpNull = isNullConstant(C.Op1);
3461 SDValue Src = peekThroughBitcasts(C.Op0);
3462 if (Src.hasOneUse() && isBitwiseNot(Src)) {
3463 Src = Src.getOperand(0);
3464 CmpNull = !CmpNull;
3465 }
3466 unsigned Opcode = 0;
3467 if (Src.hasOneUse()) {
3468 switch (Src.getOpcode()) {
3469 case SystemZISD::VICMPE: Opcode = SystemZISD::VICMPES; break;
3470 case SystemZISD::VICMPH: Opcode = SystemZISD::VICMPHS; break;
3471 case SystemZISD::VICMPHL: Opcode = SystemZISD::VICMPHLS; break;
3472 case SystemZISD::VFCMPE: Opcode = SystemZISD::VFCMPES; break;
3473 case SystemZISD::VFCMPH: Opcode = SystemZISD::VFCMPHS; break;
3474 case SystemZISD::VFCMPHE: Opcode = SystemZISD::VFCMPHES; break;
3475 default: break;
3476 }
3477 }
3478 if (Opcode) {
3479 C.Opcode = Opcode;
3480 C.Op0 = Src->getOperand(0);
3481 C.Op1 = Src->getOperand(1);
3482 C.CCValid = SystemZ::CCMASK_VCMP;
3484 if (!CmpEq)
3485 C.CCMask ^= C.CCValid;
3486 return;
3487 }
3488 }
3489
3490 // Everything below here is not useful if we have native i128 compares.
3491 if (DAG.getSubtarget<SystemZSubtarget>().hasVectorEnhancements3())
3492 return;
3493
3494 // (In-)Equality comparisons can be implemented via VCEQGS.
3495 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
3496 C.CCMask == SystemZ::CCMASK_CMP_NE) {
3497 C.Opcode = SystemZISD::VICMPES;
3498 C.Op0 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, C.Op0);
3499 C.Op1 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, C.Op1);
3500 C.CCValid = SystemZ::CCMASK_VCMP;
3501 if (C.CCMask == SystemZ::CCMASK_CMP_EQ)
3502 C.CCMask = SystemZ::CCMASK_VCMP_ALL;
3503 else
3504 C.CCMask = SystemZ::CCMASK_VCMP_ALL ^ C.CCValid;
3505 return;
3506 }
3507
3508 // Normalize other comparisons to GT.
3509 bool Swap = false, Invert = false;
3510 switch (C.CCMask) {
3511 case SystemZ::CCMASK_CMP_GT: break;
3512 case SystemZ::CCMASK_CMP_LT: Swap = true; break;
3513 case SystemZ::CCMASK_CMP_LE: Invert = true; break;
3514 case SystemZ::CCMASK_CMP_GE: Swap = Invert = true; break;
3515 default: llvm_unreachable("Invalid integer condition!");
3516 }
3517 if (Swap)
3518 std::swap(C.Op0, C.Op1);
3519
3520 if (C.ICmpType == SystemZICMP::UnsignedOnly)
3521 C.Opcode = SystemZISD::UCMP128HI;
3522 else
3523 C.Opcode = SystemZISD::SCMP128HI;
3524 C.CCValid = SystemZ::CCMASK_ANY;
3525 C.CCMask = SystemZ::CCMASK_1;
3526
3527 if (Invert)
3528 C.CCMask ^= C.CCValid;
3529}
3530
3531// See whether the comparison argument contains a redundant AND
3532// and remove it if so. This sometimes happens due to the generic
3533// BRCOND expansion.
3535 Comparison &C) {
3536 if (C.Op0.getOpcode() != ISD::AND)
3537 return;
3538 auto *Mask = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
3539 if (!Mask || Mask->getValueSizeInBits(0) > 64)
3540 return;
3541 KnownBits Known = DAG.computeKnownBits(C.Op0.getOperand(0));
3542 if ((~Known.Zero).getZExtValue() & ~Mask->getZExtValue())
3543 return;
3544
3545 C.Op0 = C.Op0.getOperand(0);
3546}
3547
3548// Return a Comparison that tests the condition-code result of intrinsic
3549// node Call against constant integer CC using comparison code Cond.
3550// Opcode is the opcode of the SystemZISD operation for the intrinsic
3551// and CCValid is the set of possible condition-code results.
3552static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
3553 SDValue Call, unsigned CCValid, uint64_t CC,
3555 Comparison C(Call, SDValue(), SDValue());
3556 C.Opcode = Opcode;
3557 C.CCValid = CCValid;
3558 if (Cond == ISD::SETEQ)
3559 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
3560 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
3561 else if (Cond == ISD::SETNE)
3562 // ...and the inverse of that.
3563 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
3564 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
3565 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
3566 // always true for CC>3.
3567 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
3568 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
3569 // ...and the inverse of that.
3570 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
3571 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
3572 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
3573 // always true for CC>3.
3574 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
3575 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
3576 // ...and the inverse of that.
3577 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
3578 else
3579 llvm_unreachable("Unexpected integer comparison type");
3580 C.CCMask &= CCValid;
3581 return C;
3582}
3583
3584// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
3585static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
3586 ISD::CondCode Cond, const SDLoc &DL,
3587 SDValue Chain = SDValue(),
3588 bool IsSignaling = false) {
3589 if (CmpOp1.getOpcode() == ISD::Constant) {
3590 assert(!Chain);
3591 unsigned Opcode, CCValid;
3592 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
3593 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
3594 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
3595 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid,
3596 CmpOp1->getAsZExtVal(), Cond);
3597 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
3598 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
3599 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
3600 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid,
3601 CmpOp1->getAsZExtVal(), Cond);
3602 }
3603 Comparison C(CmpOp0, CmpOp1, Chain);
3604 C.CCMask = CCMaskForCondCode(Cond);
3605 if (C.Op0.getValueType().isFloatingPoint()) {
3606 C.CCValid = SystemZ::CCMASK_FCMP;
3607 if (!C.Chain)
3608 C.Opcode = SystemZISD::FCMP;
3609 else if (!IsSignaling)
3610 C.Opcode = SystemZISD::STRICT_FCMP;
3611 else
3612 C.Opcode = SystemZISD::STRICT_FCMPS;
3614 } else {
3615 assert(!C.Chain);
3616 C.CCValid = SystemZ::CCMASK_ICMP;
3617 C.Opcode = SystemZISD::ICMP;
3618 // Choose the type of comparison. Equality and inequality tests can
3619 // use either signed or unsigned comparisons. The choice also doesn't
3620 // matter if both sign bits are known to be clear. In those cases we
3621 // want to give the main isel code the freedom to choose whichever
3622 // form fits best.
3623 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
3624 C.CCMask == SystemZ::CCMASK_CMP_NE ||
3625 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
3626 C.ICmpType = SystemZICMP::Any;
3627 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
3628 C.ICmpType = SystemZICMP::UnsignedOnly;
3629 else
3630 C.ICmpType = SystemZICMP::SignedOnly;
3631 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
3632 adjustForRedundantAnd(DAG, DL, C);
3633 adjustZeroCmp(DAG, DL, C);
3634 adjustSubwordCmp(DAG, DL, C);
3635 adjustForSubtraction(DAG, DL, C);
3637 adjustICmpTruncate(DAG, DL, C);
3638 }
3639
3640 if (shouldSwapCmpOperands(C)) {
3641 std::swap(C.Op0, C.Op1);
3642 C.CCMask = SystemZ::reverseCCMask(C.CCMask);
3643 }
3644
3646 adjustICmp128(DAG, DL, C);
3648 return C;
3649}
3650
3651// Emit the comparison instruction described by C.
3652static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
3653 if (!C.Op1.getNode()) {
3654 if (C.Opcode == SystemZISD::CMP_STACKGUARD)
3655 return DAG.getNode(SystemZISD::CMP_STACKGUARD, DL, MVT::i32, C.Op0);
3656 SDNode *Node;
3657 switch (C.Op0.getOpcode()) {
3659 Node = emitIntrinsicWithCCAndChain(DAG, C.Op0, C.Opcode);
3660 return SDValue(Node, 0);
3662 Node = emitIntrinsicWithCC(DAG, C.Op0, C.Opcode);
3663 return SDValue(Node, Node->getNumValues() - 1);
3664 default:
3665 llvm_unreachable("Invalid comparison operands");
3666 }
3667 }
3668 if (C.Opcode == SystemZISD::ICMP)
3669 return DAG.getNode(SystemZISD::ICMP, DL, MVT::i32, C.Op0, C.Op1,
3670 DAG.getTargetConstant(C.ICmpType, DL, MVT::i32));
3671 if (C.Opcode == SystemZISD::TM) {
3672 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
3674 return DAG.getNode(SystemZISD::TM, DL, MVT::i32, C.Op0, C.Op1,
3675 DAG.getTargetConstant(RegisterOnly, DL, MVT::i32));
3676 }
3677 if (C.Opcode == SystemZISD::VICMPES ||
3678 C.Opcode == SystemZISD::VICMPHS ||
3679 C.Opcode == SystemZISD::VICMPHLS ||
3680 C.Opcode == SystemZISD::VFCMPES ||
3681 C.Opcode == SystemZISD::VFCMPHS ||
3682 C.Opcode == SystemZISD::VFCMPHES) {
3683 EVT IntVT = C.Op0.getValueType().changeVectorElementTypeToInteger();
3684 SDVTList VTs = DAG.getVTList(IntVT, MVT::i32);
3685 SDValue Val = DAG.getNode(C.Opcode, DL, VTs, C.Op0, C.Op1);
3686 return SDValue(Val.getNode(), 1);
3687 }
3688 if (C.Chain) {
3689 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
3690 return DAG.getNode(C.Opcode, DL, VTs, C.Chain, C.Op0, C.Op1);
3691 }
3692 return DAG.getNode(C.Opcode, DL, MVT::i32, C.Op0, C.Op1);
3693}
3694
3695// Implement a 32-bit *MUL_LOHI operation by extending both operands to
3696// 64 bits. Extend is the extension type to use. Store the high part
3697// in Hi and the low part in Lo.
3698static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
3699 SDValue Op0, SDValue Op1, SDValue &Hi,
3700 SDValue &Lo) {
3701 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
3702 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
3703 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
3704 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
3705 DAG.getConstant(32, DL, MVT::i64));
3706 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
3707 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
3708}
3709
3710// Lower a binary operation that produces two VT results, one in each
3711// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
3712// and Opcode performs the GR128 operation. Store the even register result
3713// in Even and the odd register result in Odd.
3714static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
3715 unsigned Opcode, SDValue Op0, SDValue Op1,
3716 SDValue &Even, SDValue &Odd) {
3717 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
3718 bool Is32Bit = is32Bit(VT);
3719 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
3720 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
3721}
3722
3723// Return an i32 value that is 1 if the CC value produced by CCReg is
3724// in the mask CCMask and 0 otherwise. CC is known to have a value
3725// in CCValid, so other values can be ignored.
3726static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue CCReg,
3727 unsigned CCValid, unsigned CCMask) {
3728 SDValue Ops[] = {DAG.getConstant(1, DL, MVT::i32),
3729 DAG.getConstant(0, DL, MVT::i32),
3730 DAG.getTargetConstant(CCValid, DL, MVT::i32),
3731 DAG.getTargetConstant(CCMask, DL, MVT::i32), CCReg};
3732 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, MVT::i32, Ops);
3733}
3734
3735// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
3736// be done directly. Mode is CmpMode::Int for integer comparisons, CmpMode::FP
3737// for regular floating-point comparisons, CmpMode::StrictFP for strict (quiet)
3738// floating-point comparisons, and CmpMode::SignalingFP for strict signaling
3739// floating-point comparisons.
3742 switch (CC) {
3743 case ISD::SETOEQ:
3744 case ISD::SETEQ:
3745 switch (Mode) {
3746 case CmpMode::Int: return SystemZISD::VICMPE;
3747 case CmpMode::FP: return SystemZISD::VFCMPE;
3748 case CmpMode::StrictFP: return SystemZISD::STRICT_VFCMPE;
3749 case CmpMode::SignalingFP: return SystemZISD::STRICT_VFCMPES;
3750 }
3751 llvm_unreachable("Bad mode");
3752
3753 case ISD::SETOGE:
3754 case ISD::SETGE:
3755 switch (Mode) {
3756 case CmpMode::Int: return 0;
3757 case CmpMode::FP: return SystemZISD::VFCMPHE;
3758 case CmpMode::StrictFP: return SystemZISD::STRICT_VFCMPHE;
3759 case CmpMode::SignalingFP: return SystemZISD::STRICT_VFCMPHES;
3760 }
3761 llvm_unreachable("Bad mode");
3762
3763 case ISD::SETOGT:
3764 case ISD::SETGT:
3765 switch (Mode) {
3766 case CmpMode::Int: return SystemZISD::VICMPH;
3767 case CmpMode::FP: return SystemZISD::VFCMPH;
3768 case CmpMode::StrictFP: return SystemZISD::STRICT_VFCMPH;
3769 case CmpMode::SignalingFP: return SystemZISD::STRICT_VFCMPHS;
3770 }
3771 llvm_unreachable("Bad mode");
3772
3773 case ISD::SETUGT:
3774 switch (Mode) {
3775 case CmpMode::Int: return SystemZISD::VICMPHL;
3776 case CmpMode::FP: return 0;
3777 case CmpMode::StrictFP: return 0;
3778 case CmpMode::SignalingFP: return 0;
3779 }
3780 llvm_unreachable("Bad mode");
3781
3782 default:
3783 return 0;
3784 }
3785}
3786
3787// Return the SystemZISD vector comparison operation for CC or its inverse,
3788// or 0 if neither can be done directly. Indicate in Invert whether the
3789// result is for the inverse of CC. Mode is as above.
3791 bool &Invert) {
3792 if (unsigned Opcode = getVectorComparison(CC, Mode)) {
3793 Invert = false;
3794 return Opcode;
3795 }
3796
3797 CC = ISD::getSetCCInverse(CC, Mode == CmpMode::Int ? MVT::i32 : MVT::f32);
3798 if (unsigned Opcode = getVectorComparison(CC, Mode)) {
3799 Invert = true;
3800 return Opcode;
3801 }
3802
3803 return 0;
3804}
3805
3806// Return a v2f64 that contains the extended form of elements Start and Start+1
3807// of v4f32 value Op. If Chain is nonnull, return the strict form.
3808static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
3809 SDValue Op, SDValue Chain) {
3810 int Mask[] = { Start, -1, Start + 1, -1 };
3811 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
3812 if (Chain) {
3813 SDVTList VTs = DAG.getVTList(MVT::v2f64, MVT::Other);
3814 return DAG.getNode(SystemZISD::STRICT_VEXTEND, DL, VTs, Chain, Op);
3815 }
3816 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
3817}
3818
3819// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
3820// producing a result of type VT. If Chain is nonnull, return the strict form.
3821SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
3822 const SDLoc &DL, EVT VT,
3823 SDValue CmpOp0,
3824 SDValue CmpOp1,
3825 SDValue Chain) const {
3826 // There is no hardware support for v4f32 (unless we have the vector
3827 // enhancements facility 1), so extend the vector into two v2f64s
3828 // and compare those.
3829 if (CmpOp0.getValueType() == MVT::v4f32 &&
3830 !Subtarget.hasVectorEnhancements1()) {
3831 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0, Chain);
3832 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0, Chain);
3833 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1, Chain);
3834 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1, Chain);
3835 if (Chain) {
3836 SDVTList VTs = DAG.getVTList(MVT::v2i64, MVT::Other);
3837 SDValue HRes = DAG.getNode(Opcode, DL, VTs, Chain, H0, H1);
3838 SDValue LRes = DAG.getNode(Opcode, DL, VTs, Chain, L0, L1);
3839 SDValue Res = DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
3840 SDValue Chains[6] = { H0.getValue(1), L0.getValue(1),
3841 H1.getValue(1), L1.getValue(1),
3842 HRes.getValue(1), LRes.getValue(1) };
3843 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
3844 SDValue Ops[2] = { Res, NewChain };
3845 return DAG.getMergeValues(Ops, DL);
3846 }
3847 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
3848 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
3849 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
3850 }
3851 if (Chain) {
3852 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
3853 return DAG.getNode(Opcode, DL, VTs, Chain, CmpOp0, CmpOp1);
3854 }
3855 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
3856}
3857
3858// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
3859// an integer mask of type VT. If Chain is nonnull, we have a strict
3860// floating-point comparison. If in addition IsSignaling is true, we have
3861// a strict signaling floating-point comparison.
3862SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
3863 const SDLoc &DL, EVT VT,
3864 ISD::CondCode CC,
3865 SDValue CmpOp0,
3866 SDValue CmpOp1,
3867 SDValue Chain,
3868 bool IsSignaling) const {
3869 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
3870 assert (!Chain || IsFP);
3871 assert (!IsSignaling || Chain);
3872 CmpMode Mode = IsSignaling ? CmpMode::SignalingFP :
3873 Chain ? CmpMode::StrictFP : IsFP ? CmpMode::FP : CmpMode::Int;
3874 bool Invert = false;
3875 SDValue Cmp;
3876 switch (CC) {
3877 // Handle tests for order using (or (ogt y x) (oge x y)).
3878 case ISD::SETUO:
3879 Invert = true;
3880 [[fallthrough]];
3881 case ISD::SETO: {
3882 assert(IsFP && "Unexpected integer comparison");
3883 SDValue LT = getVectorCmp(DAG, getVectorComparison(ISD::SETOGT, Mode),
3884 DL, VT, CmpOp1, CmpOp0, Chain);
3885 SDValue GE = getVectorCmp(DAG, getVectorComparison(ISD::SETOGE, Mode),
3886 DL, VT, CmpOp0, CmpOp1, Chain);
3887 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
3888 if (Chain)
3889 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
3890 LT.getValue(1), GE.getValue(1));
3891 break;
3892 }
3893
3894 // Handle <> tests using (or (ogt y x) (ogt x y)).
3895 case ISD::SETUEQ:
3896 Invert = true;
3897 [[fallthrough]];
3898 case ISD::SETONE: {
3899 assert(IsFP && "Unexpected integer comparison");
3900 SDValue LT = getVectorCmp(DAG, getVectorComparison(ISD::SETOGT, Mode),
3901 DL, VT, CmpOp1, CmpOp0, Chain);
3902 SDValue GT = getVectorCmp(DAG, getVectorComparison(ISD::SETOGT, Mode),
3903 DL, VT, CmpOp0, CmpOp1, Chain);
3904 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
3905 if (Chain)
3906 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
3907 LT.getValue(1), GT.getValue(1));
3908 break;
3909 }
3910
3911 // Otherwise a single comparison is enough. It doesn't really
3912 // matter whether we try the inversion or the swap first, since
3913 // there are no cases where both work.
3914 default:
3915 // Optimize sign-bit comparisons to signed compares.
3916 if (Mode == CmpMode::Int && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
3918 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3919 APInt Mask;
3920 if (CmpOp0.getOpcode() == ISD::AND
3921 && ISD::isConstantSplatVector(CmpOp0.getOperand(1).getNode(), Mask)
3922 && Mask == APInt::getSignMask(EltSize)) {
3923 CC = CC == ISD::SETEQ ? ISD::SETGE : ISD::SETLT;
3924 CmpOp0 = CmpOp0.getOperand(0);
3925 }
3926 }
3927 if (unsigned Opcode = getVectorComparisonOrInvert(CC, Mode, Invert))
3928 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1, Chain);
3929 else {
3931 if (unsigned Opcode = getVectorComparisonOrInvert(CC, Mode, Invert))
3932 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0, Chain);
3933 else
3934 llvm_unreachable("Unhandled comparison");
3935 }
3936 if (Chain)
3937 Chain = Cmp.getValue(1);
3938 break;
3939 }
3940 if (Invert) {
3941 SDValue Mask =
3942 DAG.getSplatBuildVector(VT, DL, DAG.getAllOnesConstant(DL, MVT::i64));
3943 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
3944 }
3945 if (Chain && Chain.getNode() != Cmp.getNode()) {
3946 SDValue Ops[2] = { Cmp, Chain };
3947 Cmp = DAG.getMergeValues(Ops, DL);
3948 }
3949 return Cmp;
3950}
3951
3952SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
3953 SelectionDAG &DAG) const {
3954 SDValue CmpOp0 = Op.getOperand(0);
3955 SDValue CmpOp1 = Op.getOperand(1);
3956 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
3957 SDLoc DL(Op);
3958 EVT VT = Op.getValueType();
3959 if (VT.isVector())
3960 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
3961
3962 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
3963 SDValue CCReg = emitCmp(DAG, DL, C);
3964 return emitSETCC(DAG, DL, CCReg, C.CCValid, C.CCMask);
3965}
3966
3967SDValue SystemZTargetLowering::lowerSTRICT_FSETCC(SDValue Op,
3968 SelectionDAG &DAG,
3969 bool IsSignaling) const {
3970 SDValue Chain = Op.getOperand(0);
3971 SDValue CmpOp0 = Op.getOperand(1);
3972 SDValue CmpOp1 = Op.getOperand(2);
3973 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(3))->get();
3974 SDLoc DL(Op);
3975 EVT VT = Op.getNode()->getValueType(0);
3976 if (VT.isVector()) {
3977 SDValue Res = lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1,
3978 Chain, IsSignaling);
3979 return Res.getValue(Op.getResNo());
3980 }
3981
3982 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL, Chain, IsSignaling));
3983 SDValue CCReg = emitCmp(DAG, DL, C);
3984 CCReg->setFlags(Op->getFlags());
3985 SDValue Result = emitSETCC(DAG, DL, CCReg, C.CCValid, C.CCMask);
3986 SDValue Ops[2] = { Result, CCReg.getValue(1) };
3987 return DAG.getMergeValues(Ops, DL);
3988}
3989
3990SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3991 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3992 SDValue CmpOp0 = Op.getOperand(2);
3993 SDValue CmpOp1 = Op.getOperand(3);
3994 SDValue Dest = Op.getOperand(4);
3995 SDLoc DL(Op);
3996
3997 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
3998 SDValue CCReg = emitCmp(DAG, DL, C);
3999 return DAG.getNode(
4000 SystemZISD::BR_CCMASK, DL, Op.getValueType(), Op.getOperand(0),
4001 DAG.getTargetConstant(C.CCValid, DL, MVT::i32),
4002 DAG.getTargetConstant(C.CCMask, DL, MVT::i32), Dest, CCReg);
4003}
4004
4005// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
4006// allowing Pos and Neg to be wider than CmpOp.
4007static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
4008 return (Neg.getOpcode() == ISD::SUB &&
4009 Neg.getOperand(0).getOpcode() == ISD::Constant &&
4010 Neg.getConstantOperandVal(0) == 0 && Neg.getOperand(1) == Pos &&
4011 (Pos == CmpOp || (Pos.getOpcode() == ISD::SIGN_EXTEND &&
4012 Pos.getOperand(0) == CmpOp)));
4013}
4014
4015// Return the absolute or negative absolute of Op; IsNegative decides which.
4017 bool IsNegative) {
4018 Op = DAG.getNode(ISD::ABS, DL, Op.getValueType(), Op);
4019 if (IsNegative)
4020 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
4021 DAG.getConstant(0, DL, Op.getValueType()), Op);
4022 return Op;
4023}
4024
4026 Comparison C, SDValue TrueOp, SDValue FalseOp) {
4027 EVT VT = MVT::i128;
4028 unsigned Op;
4029
4030 if (C.CCMask == SystemZ::CCMASK_CMP_NE ||
4031 C.CCMask == SystemZ::CCMASK_CMP_GE ||
4032 C.CCMask == SystemZ::CCMASK_CMP_LE) {
4033 std::swap(TrueOp, FalseOp);
4034 C.CCMask ^= C.CCValid;
4035 }
4036 if (C.CCMask == SystemZ::CCMASK_CMP_LT) {
4037 std::swap(C.Op0, C.Op1);
4038 C.CCMask = SystemZ::CCMASK_CMP_GT;
4039 }
4040 switch (C.CCMask) {
4042 Op = SystemZISD::VICMPE;
4043 break;
4045 if (C.ICmpType == SystemZICMP::UnsignedOnly)
4046 Op = SystemZISD::VICMPHL;
4047 else
4048 Op = SystemZISD::VICMPH;
4049 break;
4050 default:
4051 llvm_unreachable("Unhandled comparison");
4052 break;
4053 }
4054
4055 SDValue Mask = DAG.getNode(Op, DL, VT, C.Op0, C.Op1);
4056 TrueOp = DAG.getNode(ISD::AND, DL, VT, TrueOp, Mask);
4057 FalseOp = DAG.getNode(ISD::AND, DL, VT, FalseOp, DAG.getNOT(DL, Mask, VT));
4058 return DAG.getNode(ISD::OR, DL, VT, TrueOp, FalseOp);
4059}
4060
4061SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
4062 SelectionDAG &DAG) const {
4063 SDValue CmpOp0 = Op.getOperand(0);
4064 SDValue CmpOp1 = Op.getOperand(1);
4065 SDValue TrueOp = Op.getOperand(2);
4066 SDValue FalseOp = Op.getOperand(3);
4067 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4068 SDLoc DL(Op);
4069
4070 // SELECT_CC involving f16 will not have the cmp-ops promoted by the
4071 // legalizer, as it will be handled according to the type of the resulting
4072 // value. Extend them here if needed.
4073 if (CmpOp0.getSimpleValueType() == MVT::f16) {
4074 CmpOp0 = DAG.getFPExtendOrRound(CmpOp0, SDLoc(CmpOp0), MVT::f32);
4075 CmpOp1 = DAG.getFPExtendOrRound(CmpOp1, SDLoc(CmpOp1), MVT::f32);
4076 }
4077
4078 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
4079
4080 // Check for absolute and negative-absolute selections, including those
4081 // where the comparison value is sign-extended (for LPGFR and LNGFR).
4082 // This check supplements the one in DAGCombiner.
4083 if (C.Opcode == SystemZISD::ICMP && C.CCMask != SystemZ::CCMASK_CMP_EQ &&
4084 C.CCMask != SystemZ::CCMASK_CMP_NE &&
4085 C.Op1.getOpcode() == ISD::Constant &&
4086 cast<ConstantSDNode>(C.Op1)->getValueSizeInBits(0) <= 64 &&
4087 C.Op1->getAsZExtVal() == 0) {
4088 if (isAbsolute(C.Op0, TrueOp, FalseOp))
4089 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
4090 if (isAbsolute(C.Op0, FalseOp, TrueOp))
4091 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
4092 }
4093
4094 if (Subtarget.hasVectorEnhancements3() &&
4095 C.Opcode == SystemZISD::ICMP &&
4096 C.Op0.getValueType() == MVT::i128 &&
4097 TrueOp.getValueType() == MVT::i128) {
4098 return getI128Select(DAG, DL, C, TrueOp, FalseOp);
4099 }
4100
4101 SDValue CCReg = emitCmp(DAG, DL, C);
4102 SDValue Ops[] = {TrueOp, FalseOp,
4103 DAG.getTargetConstant(C.CCValid, DL, MVT::i32),
4104 DAG.getTargetConstant(C.CCMask, DL, MVT::i32), CCReg};
4105
4106 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, Op.getValueType(), Ops);
4107}
4108
4109SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
4110 SelectionDAG &DAG) const {
4111 SDLoc DL(Node);
4112 const GlobalValue *GV = Node->getGlobal();
4113 int64_t Offset = Node->getOffset();
4114 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4116
4118 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
4119 if (isInt<32>(Offset)) {
4120 // Assign anchors at 1<<12 byte boundaries.
4121 uint64_t Anchor = Offset & ~uint64_t(0xfff);
4122 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
4123 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
4124
4125 // The offset can be folded into the address if it is aligned to a
4126 // halfword.
4127 Offset -= Anchor;
4128 if (Offset != 0 && (Offset & 1) == 0) {
4129 SDValue Full =
4130 DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
4131 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
4132 Offset = 0;
4133 }
4134 } else {
4135 // Conservatively load a constant offset greater than 32 bits into a
4136 // register below.
4137 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT);
4138 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
4139 }
4140 } else if (Subtarget.isTargetELF()) {
4141 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
4142 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
4143 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
4145 } else if (Subtarget.isTargetzOS()) {
4146 Result = getADAEntry(DAG, GV, DL, PtrVT);
4147 } else
4148 llvm_unreachable("Unexpected Subtarget");
4149
4150 // If there was a non-zero offset that we didn't fold, create an explicit
4151 // addition for it.
4152 if (Offset != 0)
4153 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
4154 DAG.getSignedConstant(Offset, DL, PtrVT));
4155
4156 return Result;
4157}
4158
4159SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
4160 SelectionDAG &DAG,
4161 unsigned Opcode,
4162 SDValue GOTOffset) const {
4163 SDLoc DL(Node);
4164 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4165 SDValue Chain = DAG.getEntryNode();
4166 SDValue Glue;
4167
4170 report_fatal_error("In GHC calling convention TLS is not supported");
4171
4172 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
4173 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
4174 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
4175 Glue = Chain.getValue(1);
4176 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
4177 Glue = Chain.getValue(1);
4178
4179 // The first call operand is the chain and the second is the TLS symbol.
4181 Ops.push_back(Chain);
4182 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
4183 Node->getValueType(0),
4184 0, 0));
4185
4186 // Add argument registers to the end of the list so that they are
4187 // known live into the call.
4188 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
4189 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
4190
4191 // Add a register mask operand representing the call-preserved registers.
4192 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
4193 const uint32_t *Mask =
4194 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
4195 assert(Mask && "Missing call preserved mask for calling convention");
4196 Ops.push_back(DAG.getRegisterMask(Mask));
4197
4198 // Glue the call to the argument copies.
4199 Ops.push_back(Glue);
4200
4201 // Emit the call.
4202 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
4203 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
4204 Glue = Chain.getValue(1);
4205
4206 // Copy the return value from %r2.
4207 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
4208}
4209
4210SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
4211 SelectionDAG &DAG) const {
4212 SDValue Chain = DAG.getEntryNode();
4213 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4214
4215 // The high part of the thread pointer is in access register 0.
4216 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
4217 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
4218
4219 // The low part of the thread pointer is in access register 1.
4220 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
4221 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
4222
4223 // Merge them into a single 64-bit address.
4224 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
4225 DAG.getConstant(32, DL, PtrVT));
4226 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
4227}
4228
4229SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
4230 SelectionDAG &DAG) const {
4231 if (DAG.getTarget().useEmulatedTLS())
4232 return LowerToTLSEmulatedModel(Node, DAG);
4233 SDLoc DL(Node);
4234 const GlobalValue *GV = Node->getGlobal();
4235 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4236 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
4237
4240 report_fatal_error("In GHC calling convention TLS is not supported");
4241
4242 SDValue TP = lowerThreadPointer(DL, DAG);
4243
4244 // Get the offset of GA from the thread pointer, based on the TLS model.
4246 switch (model) {
4248 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
4249 SystemZConstantPoolValue *CPV =
4251
4252 Offset = DAG.getConstantPool(CPV, PtrVT, Align(8));
4253 Offset = DAG.getLoad(
4254 PtrVT, DL, DAG.getEntryNode(), Offset,
4256
4257 // Call __tls_get_offset to retrieve the offset.
4258 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
4259 break;
4260 }
4261
4263 // Load the GOT offset of the module ID.
4264 SystemZConstantPoolValue *CPV =
4266
4267 Offset = DAG.getConstantPool(CPV, PtrVT, Align(8));
4268 Offset = DAG.getLoad(
4269 PtrVT, DL, DAG.getEntryNode(), Offset,
4271
4272 // Call __tls_get_offset to retrieve the module base offset.
4273 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
4274
4275 // Note: The SystemZLDCleanupPass will remove redundant computations
4276 // of the module base offset. Count total number of local-dynamic
4277 // accesses to trigger execution of that pass.
4278 SystemZMachineFunctionInfo* MFI =
4279 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
4281
4282 // Add the per-symbol offset.
4284
4285 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, Align(8));
4286 DTPOffset = DAG.getLoad(
4287 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
4289
4290 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
4291 break;
4292 }
4293
4294 case TLSModel::InitialExec: {
4295 // Load the offset from the GOT.
4296 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
4298 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
4299 Offset =
4300 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
4302 break;
4303 }
4304
4305 case TLSModel::LocalExec: {
4306 // Force the offset into the constant pool and load it from there.
4307 SystemZConstantPoolValue *CPV =
4309
4310 Offset = DAG.getConstantPool(CPV, PtrVT, Align(8));
4311 Offset = DAG.getLoad(
4312 PtrVT, DL, DAG.getEntryNode(), Offset,
4314 break;
4315 }
4316 }
4317
4318 // Add the base and offset together.
4319 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
4320}
4321
4322SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
4323 SelectionDAG &DAG) const {
4324 SDLoc DL(Node);
4325 const BlockAddress *BA = Node->getBlockAddress();
4326 int64_t Offset = Node->getOffset();
4327 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4328
4329 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
4330 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
4331 return Result;
4332}
4333
4334SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
4335 SelectionDAG &DAG) const {
4336 SDLoc DL(JT);
4337 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4338 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
4339
4340 // Use LARL to load the address of the table.
4341 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
4342}
4343
4344SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
4345 SelectionDAG &DAG) const {
4346 SDLoc DL(CP);
4347 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4348
4351 Result =
4352 DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, CP->getAlign());
4353 else
4354 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlign(),
4355 CP->getOffset());
4356
4357 // Use LARL to load the address of the constant pool entry.
4358 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
4359}
4360
4361SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
4362 SelectionDAG &DAG) const {
4363 auto *TFL = Subtarget.getFrameLowering<SystemZFrameLowering>();
4364 MachineFunction &MF = DAG.getMachineFunction();
4365 MachineFrameInfo &MFI = MF.getFrameInfo();
4366 MFI.setFrameAddressIsTaken(true);
4367
4368 SDLoc DL(Op);
4369 unsigned Depth = Op.getConstantOperandVal(0);
4370 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4371
4372 // By definition, the frame address is the address of the back chain. (In
4373 // the case of packed stack without backchain, return the address where the
4374 // backchain would have been stored. This will either be an unused space or
4375 // contain a saved register).
4376 int BackChainIdx = TFL->getOrCreateFramePointerSaveIndex(MF);
4377 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
4378
4379 if (Depth > 0) {
4380 // FIXME The frontend should detect this case.
4381 if (!MF.getSubtarget<SystemZSubtarget>().hasBackChain())
4382 report_fatal_error("Unsupported stack frame traversal count");
4383
4384 SDValue Offset = DAG.getConstant(TFL->getBackchainOffset(MF), DL, PtrVT);
4385 while (Depth--) {
4386 BackChain = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), BackChain,
4387 MachinePointerInfo());
4388 BackChain = DAG.getNode(ISD::ADD, DL, PtrVT, BackChain, Offset);
4389 }
4390 }
4391
4392 return BackChain;
4393}
4394
4395SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
4396 SelectionDAG &DAG) const {
4397 MachineFunction &MF = DAG.getMachineFunction();
4398 MachineFrameInfo &MFI = MF.getFrameInfo();
4399 MFI.setReturnAddressIsTaken(true);
4400
4401 SDLoc DL(Op);
4402 unsigned Depth = Op.getConstantOperandVal(0);
4403 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4404
4405 if (Depth > 0) {
4406 // FIXME The frontend should detect this case.
4407 if (!MF.getSubtarget<SystemZSubtarget>().hasBackChain())
4408 report_fatal_error("Unsupported stack frame traversal count");
4409
4410 SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
4411 const auto *TFL = Subtarget.getFrameLowering<SystemZFrameLowering>();
4412 int Offset = TFL->getReturnAddressOffset(MF);
4413 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, FrameAddr,
4414 DAG.getSignedConstant(Offset, DL, PtrVT));
4415 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr,
4416 MachinePointerInfo());
4417 }
4418
4419 // Return R14D (Elf) / R7D (XPLINK), which has the return address. Mark it an
4420 // implicit live-in.
4421 SystemZCallingConventionRegisters *CCR = Subtarget.getSpecialRegisters();
4423 &SystemZ::GR64BitRegClass);
4424 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
4425}
4426
4427SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
4428 SelectionDAG &DAG) const {
4429 SDLoc DL(Op);
4430 SDValue In = Op.getOperand(0);
4431 EVT InVT = In.getValueType();
4432 EVT ResVT = Op.getValueType();
4433
4434 // Convert loads directly. This is normally done by DAGCombiner,
4435 // but we need this case for bitcasts that are created during lowering
4436 // and which are then lowered themselves.
4437 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
4438 if (ISD::isNormalLoad(LoadN)) {
4439 SDValue NewLoad = DAG.getLoad(ResVT, DL, LoadN->getChain(),
4440 LoadN->getBasePtr(), LoadN->getMemOperand());
4441 // Update the chain uses.
4442 DAG.ReplaceAllUsesOfValueWith(SDValue(LoadN, 1), NewLoad.getValue(1));
4443 return NewLoad;
4444 }
4445
4446 if (InVT == MVT::i32 && ResVT == MVT::f32) {
4447 SDValue In64;
4448 if (Subtarget.hasHighWord()) {
4449 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
4450 MVT::i64);
4451 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
4452 MVT::i64, SDValue(U64, 0), In);
4453 } else {
4454 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
4455 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
4456 DAG.getConstant(32, DL, MVT::i64));
4457 }
4458 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
4459 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
4460 DL, MVT::f32, Out64);
4461 }
4462 if (InVT == MVT::f32 && ResVT == MVT::i32) {
4463 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
4464 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
4465 MVT::f64, SDValue(U64, 0), In);
4466 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
4467 if (Subtarget.hasHighWord())
4468 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
4469 MVT::i32, Out64);
4470 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
4471 DAG.getConstant(32, DL, MVT::i64));
4472 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
4473 }
4474 llvm_unreachable("Unexpected bitcast combination");
4475}
4476
4477SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
4478 SelectionDAG &DAG) const {
4479
4480 if (Subtarget.isTargetXPLINK64())
4481 return lowerVASTART_XPLINK(Op, DAG);
4482 else
4483 return lowerVASTART_ELF(Op, DAG);
4484}
4485
4486SDValue SystemZTargetLowering::lowerVASTART_XPLINK(SDValue Op,
4487 SelectionDAG &DAG) const {
4488 MachineFunction &MF = DAG.getMachineFunction();
4489 SystemZMachineFunctionInfo *FuncInfo =
4490 MF.getInfo<SystemZMachineFunctionInfo>();
4491
4492 SDLoc DL(Op);
4493
4494 // vastart just stores the address of the VarArgsFrameIndex slot into the
4495 // memory location argument.
4496 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4497 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
4498 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4499 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
4500 MachinePointerInfo(SV));
4501}
4502
4503SDValue SystemZTargetLowering::lowerVASTART_ELF(SDValue Op,
4504 SelectionDAG &DAG) const {
4505 MachineFunction &MF = DAG.getMachineFunction();
4506 SystemZMachineFunctionInfo *FuncInfo =
4507 MF.getInfo<SystemZMachineFunctionInfo>();
4508 EVT PtrVT = getPointerTy(DAG.getDataLayout());
4509
4510 SDValue Chain = Op.getOperand(0);
4511 SDValue Addr = Op.getOperand(1);
4512 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4513 SDLoc DL(Op);
4514
4515 // The initial values of each field.
4516 const unsigned NumFields = 4;
4517 SDValue Fields[NumFields] = {
4518 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
4519 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
4520 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
4521 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
4522 };
4523
4524 // Store each field into its respective slot.
4525 SDValue MemOps[NumFields];
4526 unsigned Offset = 0;
4527 for (unsigned I = 0; I < NumFields; ++I) {
4528 SDValue FieldAddr = Addr;
4529 if (Offset != 0)
4530 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
4532 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
4533 MachinePointerInfo(SV, Offset));
4534 Offset += 8;
4535 }
4536 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
4537}
4538
4539SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
4540 SelectionDAG &DAG) const {
4541 SDValue Chain = Op.getOperand(0);
4542 SDValue DstPtr = Op.getOperand(1);
4543 SDValue SrcPtr = Op.getOperand(2);
4544 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4545 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
4546 SDLoc DL(Op);
4547
4548 uint32_t Sz =
4549 Subtarget.isTargetXPLINK64() ? getTargetMachine().getPointerSize(0) : 32;
4550 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(Sz, DL),
4551 Align(8), Align(8), /*isVolatile*/ false,
4552 /*AlwaysInline*/ false,
4553 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(DstSV),
4554 MachinePointerInfo(SrcSV));
4555}
4556
4557SDValue
4558SystemZTargetLowering::lowerDYNAMIC_STACKALLOC(SDValue Op,
4559 SelectionDAG &DAG) const {
4560 if (Subtarget.isTargetXPLINK64())
4561 return lowerDYNAMIC_STACKALLOC_XPLINK(Op, DAG);
4562 else
4563 return lowerDYNAMIC_STACKALLOC_ELF(Op, DAG);
4564}
4565
4566SDValue
4567SystemZTargetLowering::lowerDYNAMIC_STACKALLOC_XPLINK(SDValue Op,
4568 SelectionDAG &DAG) const {
4569 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
4570 MachineFunction &MF = DAG.getMachineFunction();
4571 bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
4572 SDValue Chain = Op.getOperand(0);
4573 SDValue Size = Op.getOperand(1);
4574 SDValue Align = Op.getOperand(2);
4575 SDLoc DL(Op);
4576
4577 // If user has set the no alignment function attribute, ignore
4578 // alloca alignments.
4579 uint64_t AlignVal = (RealignOpt ? Align->getAsZExtVal() : 0);
4580
4581 uint64_t StackAlign = TFI->getStackAlignment();
4582 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
4583 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
4584
4585 SDValue NeededSpace = Size;
4586
4587 // Add extra space for alignment if needed.
4588 EVT PtrVT = getPointerTy(MF.getDataLayout());
4589 if (ExtraAlignSpace)
4590 NeededSpace = DAG.getNode(ISD::ADD, DL, PtrVT, NeededSpace,
4591 DAG.getConstant(ExtraAlignSpace, DL, PtrVT));
4592
4593 bool IsSigned = false;
4594 bool DoesNotReturn = false;
4595 bool IsReturnValueUsed = false;
4596 EVT VT = Op.getValueType();
4597 SDValue AllocaCall =
4598 makeExternalCall(Chain, DAG, "@@ALCAXP", VT, ArrayRef(NeededSpace),
4599 CallingConv::C, IsSigned, DL, DoesNotReturn,
4600 IsReturnValueUsed)
4601 .first;
4602
4603 // Perform a CopyFromReg from %GPR4 (stack pointer register). Chain and Glue
4604 // to end of call in order to ensure it isn't broken up from the call
4605 // sequence.
4606 auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
4607 Register SPReg = Regs.getStackPointerRegister();
4608 Chain = AllocaCall.getValue(1);
4609 SDValue Glue = AllocaCall.getValue(2);
4610 SDValue NewSPRegNode = DAG.getCopyFromReg(Chain, DL, SPReg, PtrVT, Glue);
4611 Chain = NewSPRegNode.getValue(1);
4612
4613 MVT PtrMVT = getPointerMemTy(MF.getDataLayout());
4614 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, PtrMVT);
4615 SDValue Result = DAG.getNode(ISD::ADD, DL, PtrMVT, NewSPRegNode, ArgAdjust);
4616
4617 // Dynamically realign if needed.
4618 if (ExtraAlignSpace) {
4619 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
4620 DAG.getConstant(ExtraAlignSpace, DL, PtrVT));
4621 Result = DAG.getNode(ISD::AND, DL, PtrVT, Result,
4622 DAG.getConstant(~(RequiredAlign - 1), DL, PtrVT));
4623 }
4624
4625 SDValue Ops[2] = {Result, Chain};
4626 return DAG.getMergeValues(Ops, DL);
4627}
4628
4629SDValue
4630SystemZTargetLowering::lowerDYNAMIC_STACKALLOC_ELF(SDValue Op,
4631 SelectionDAG &DAG) const {
4632 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
4633 MachineFunction &MF = DAG.getMachineFunction();
4634 bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
4635 bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
4636
4637 SDValue Chain = Op.getOperand(0);
4638 SDValue Size = Op.getOperand(1);
4639 SDValue Align = Op.getOperand(2);
4640 SDLoc DL(Op);
4641
4642 // If user has set the no alignment function attribute, ignore
4643 // alloca alignments.
4644 uint64_t AlignVal = (RealignOpt ? Align->getAsZExtVal() : 0);
4645
4646 uint64_t StackAlign = TFI->getStackAlignment();
4647 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
4648 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
4649
4651 SDValue NeededSpace = Size;
4652
4653 // Get a reference to the stack pointer.
4654 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
4655
4656 // If we need a backchain, save it now.
4657 SDValue Backchain;
4658 if (StoreBackchain)
4659 Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG),
4660 MachinePointerInfo());
4661
4662 // Add extra space for alignment if needed.
4663 if (ExtraAlignSpace)
4664 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
4665 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
4666
4667 // Get the new stack pointer value.
4668 SDValue NewSP;
4669 if (hasInlineStackProbe(MF)) {
4670 NewSP = DAG.getNode(SystemZISD::PROBED_ALLOCA, DL,
4671 DAG.getVTList(MVT::i64, MVT::Other), Chain, OldSP, NeededSpace);
4672 Chain = NewSP.getValue(1);
4673 }
4674 else {
4675 NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
4676 // Copy the new stack pointer back.
4677 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
4678 }
4679
4680 // The allocated data lives above the 160 bytes allocated for the standard
4681 // frame, plus any outgoing stack arguments. We don't know how much that
4682 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
4683 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
4684 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
4685
4686 // Dynamically realign if needed.
4687 if (RequiredAlign > StackAlign) {
4688 Result =
4689 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
4690 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
4691 Result =
4692 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
4693 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
4694 }
4695
4696 if (StoreBackchain)
4697 Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG),
4698 MachinePointerInfo());
4699
4700 SDValue Ops[2] = { Result, Chain };
4701 return DAG.getMergeValues(Ops, DL);
4702}
4703
4704SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
4705 SDValue Op, SelectionDAG &DAG) const {
4706 SDLoc DL(Op);
4707
4708 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
4709}
4710
4711SDValue SystemZTargetLowering::lowerMULH(SDValue Op,
4712 SelectionDAG &DAG,
4713 unsigned Opcode) const {
4714 EVT VT = Op.getValueType();
4715 SDLoc DL(Op);
4716 SDValue Even, Odd;
4717
4718 // This custom expander is only used on z17 and later for 64-bit types.
4719 assert(!is32Bit(VT));
4720 assert(Subtarget.hasMiscellaneousExtensions2());
4721
4722 // SystemZISD::xMUL_LOHI returns the low result in the odd register and
4723 // the high result in the even register. Return the latter.
4724 lowerGR128Binary(DAG, DL, VT, Opcode,
4725 Op.getOperand(0), Op.getOperand(1), Even, Odd);
4726 return Even;
4727}
4728
4729SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
4730 SelectionDAG &DAG) const {
4731 EVT VT = Op.getValueType();
4732 SDLoc DL(Op);
4733 SDValue Ops[2];
4734 if (is32Bit(VT))
4735 // Just do a normal 64-bit multiplication and extract the results.
4736 // We define this so that it can be used for constant division.
4737 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
4738 Op.getOperand(1), Ops[1], Ops[0]);
4739 else if (Subtarget.hasMiscellaneousExtensions2())
4740 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
4741 // the high result in the even register. ISD::SMUL_LOHI is defined to
4742 // return the low half first, so the results are in reverse order.
4743 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
4744 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
4745 else {
4746 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
4747 //
4748 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
4749 //
4750 // but using the fact that the upper halves are either all zeros
4751 // or all ones:
4752 //
4753 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
4754 //
4755 // and grouping the right terms together since they are quicker than the
4756 // multiplication:
4757 //
4758 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
4759 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
4760 SDValue LL = Op.getOperand(0);
4761 SDValue RL = Op.getOperand(1);
4762 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
4763 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
4764 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
4765 // the high result in the even register. ISD::SMUL_LOHI is defined to
4766 // return the low half first, so the results are in reverse order.
4767 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
4768 LL, RL, Ops[1], Ops[0]);
4769 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
4770 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
4771 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
4772 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
4773 }
4774 return DAG.getMergeValues(Ops, DL);
4775}
4776
4777SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
4778 SelectionDAG &DAG) const {
4779 EVT VT = Op.getValueType();
4780 SDLoc DL(Op);
4781 SDValue Ops[2];
4782 if (is32Bit(VT))
4783 // Just do a normal 64-bit multiplication and extract the results.
4784 // We define this so that it can be used for constant division.
4785 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
4786 Op.getOperand(1), Ops[1], Ops[0]);
4787 else
4788 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
4789 // the high result in the even register. ISD::UMUL_LOHI is defined to
4790 // return the low half first, so the results are in reverse order.
4791 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
4792 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
4793 return DAG.getMergeValues(Ops, DL);
4794}
4795
4796SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
4797 SelectionDAG &DAG) const {
4798 SDValue Op0 = Op.getOperand(0);
4799 SDValue Op1 = Op.getOperand(1);
4800 EVT VT = Op.getValueType();
4801 SDLoc DL(Op);
4802
4803 // We use DSGF for 32-bit division. This means the first operand must
4804 // always be 64-bit, and the second operand should be 32-bit whenever
4805 // that is possible, to improve performance.
4806 if (is32Bit(VT))
4807 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
4808 else if (DAG.ComputeNumSignBits(Op1) > 32)
4809 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
4810
4811 // DSG(F) returns the remainder in the even register and the
4812 // quotient in the odd register.
4813 SDValue Ops[2];
4814 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
4815 return DAG.getMergeValues(Ops, DL);
4816}
4817
4818SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
4819 SelectionDAG &DAG) const {
4820 EVT VT = Op.getValueType();
4821 SDLoc DL(Op);
4822
4823 // DL(G) returns the remainder in the even register and the
4824 // quotient in the odd register.
4825 SDValue Ops[2];
4826 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
4827 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
4828 return DAG.getMergeValues(Ops, DL);
4829}
4830
4831SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
4832 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
4833
4834 // Get the known-zero masks for each operand.
4835 SDValue Ops[] = {Op.getOperand(0), Op.getOperand(1)};
4836 KnownBits Known[2] = {DAG.computeKnownBits(Ops[0]),
4837 DAG.computeKnownBits(Ops[1])};
4838
4839 // See if the upper 32 bits of one operand and the lower 32 bits of the
4840 // other are known zero. They are the low and high operands respectively.
4841 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
4842 Known[1].Zero.getZExtValue() };
4843 unsigned High, Low;
4844 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
4845 High = 1, Low = 0;
4846 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
4847 High = 0, Low = 1;
4848 else
4849 return Op;
4850
4851 SDValue LowOp = Ops[Low];
4852 SDValue HighOp = Ops[High];
4853
4854 // If the high part is a constant, we're better off using IILH.
4855 if (HighOp.getOpcode() == ISD::Constant)
4856 return Op;
4857
4858 // If the low part is a constant that is outside the range of LHI,
4859 // then we're better off using IILF.
4860 if (LowOp.getOpcode() == ISD::Constant) {
4861 int64_t Value = int32_t(LowOp->getAsZExtVal());
4862 if (!isInt<16>(Value))
4863 return Op;
4864 }
4865
4866 // Check whether the high part is an AND that doesn't change the
4867 // high 32 bits and just masks out low bits. We can skip it if so.
4868 if (HighOp.getOpcode() == ISD::AND &&
4869 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
4870 SDValue HighOp0 = HighOp.getOperand(0);
4871 uint64_t Mask = HighOp.getConstantOperandVal(1);
4872 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
4873 HighOp = HighOp0;
4874 }
4875
4876 // Take advantage of the fact that all GR32 operations only change the
4877 // low 32 bits by truncating Low to an i32 and inserting it directly
4878 // using a subreg. The interesting cases are those where the truncation
4879 // can be folded.
4880 SDLoc DL(Op);
4881 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
4882 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
4883 MVT::i64, HighOp, Low32);
4884}
4885
4886// Lower SADDO/SSUBO/UADDO/USUBO nodes.
4887SDValue SystemZTargetLowering::lowerXALUO(SDValue Op,
4888 SelectionDAG &DAG) const {
4889 SDNode *N = Op.getNode();
4890 SDValue LHS = N->getOperand(0);
4891 SDValue RHS = N->getOperand(1);
4892 SDLoc DL(N);
4893
4894 if (N->getValueType(0) == MVT::i128) {
4895 unsigned BaseOp = 0;
4896 unsigned FlagOp = 0;
4897 bool IsBorrow = false;
4898 switch (Op.getOpcode()) {
4899 default: llvm_unreachable("Unknown instruction!");
4900 case ISD::UADDO:
4901 BaseOp = ISD::ADD;
4902 FlagOp = SystemZISD::VACC;
4903 break;
4904 case ISD::USUBO:
4905 BaseOp = ISD::SUB;
4906 FlagOp = SystemZISD::VSCBI;
4907 IsBorrow = true;
4908 break;
4909 }
4910 SDValue Result = DAG.getNode(BaseOp, DL, MVT::i128, LHS, RHS);
4911 SDValue Flag = DAG.getNode(FlagOp, DL, MVT::i128, LHS, RHS);
4912 Flag = DAG.getNode(ISD::AssertZext, DL, MVT::i128, Flag,
4913 DAG.getValueType(MVT::i1));
4914 Flag = DAG.getZExtOrTrunc(Flag, DL, N->getValueType(1));
4915 if (IsBorrow)
4916 Flag = DAG.getNode(ISD::XOR, DL, Flag.getValueType(),
4917 Flag, DAG.getConstant(1, DL, Flag.getValueType()));
4918 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Flag);
4919 }
4920
4921 unsigned BaseOp = 0;
4922 unsigned CCValid = 0;
4923 unsigned CCMask = 0;
4924
4925 switch (Op.getOpcode()) {
4926 default: llvm_unreachable("Unknown instruction!");
4927 case ISD::SADDO:
4928 BaseOp = SystemZISD::SADDO;
4929 CCValid = SystemZ::CCMASK_ARITH;
4931 break;
4932 case ISD::SSUBO:
4933 BaseOp = SystemZISD::SSUBO;
4934 CCValid = SystemZ::CCMASK_ARITH;
4936 break;
4937 case ISD::UADDO:
4938 BaseOp = SystemZISD::UADDO;
4939 CCValid = SystemZ::CCMASK_LOGICAL;
4941 break;
4942 case ISD::USUBO:
4943 BaseOp = SystemZISD::USUBO;
4944 CCValid = SystemZ::CCMASK_LOGICAL;
4946 break;
4947 }
4948
4949 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
4950 SDValue Result = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
4951
4952 SDValue SetCC = emitSETCC(DAG, DL, Result.getValue(1), CCValid, CCMask);
4953 if (N->getValueType(1) == MVT::i1)
4954 SetCC = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC);
4955
4956 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, SetCC);
4957}
4958
4959static bool isAddCarryChain(SDValue Carry) {
4960 while (Carry.getOpcode() == ISD::UADDO_CARRY &&
4961 Carry->getValueType(0) != MVT::i128)
4962 Carry = Carry.getOperand(2);
4963 return Carry.getOpcode() == ISD::UADDO &&
4964 Carry->getValueType(0) != MVT::i128;
4965}
4966
4967static bool isSubBorrowChain(SDValue Carry) {
4968 while (Carry.getOpcode() == ISD::USUBO_CARRY &&
4969 Carry->getValueType(0) != MVT::i128)
4970 Carry = Carry.getOperand(2);
4971 return Carry.getOpcode() == ISD::USUBO &&
4972 Carry->getValueType(0) != MVT::i128;
4973}
4974
4975// Lower UADDO_CARRY/USUBO_CARRY nodes.
4976SDValue SystemZTargetLowering::lowerUADDSUBO_CARRY(SDValue Op,
4977 SelectionDAG &DAG) const {
4978
4979 SDNode *N = Op.getNode();
4980 MVT VT = N->getSimpleValueType(0);
4981
4982 // Let legalize expand this if it isn't a legal type yet.
4983 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
4984 return SDValue();
4985
4986 SDValue LHS = N->getOperand(0);
4987 SDValue RHS = N->getOperand(1);
4988 SDValue Carry = Op.getOperand(2);
4989 SDLoc DL(N);
4990
4991 if (VT == MVT::i128) {
4992 unsigned BaseOp = 0;
4993 unsigned FlagOp = 0;
4994 bool IsBorrow = false;
4995 switch (Op.getOpcode()) {
4996 default: llvm_unreachable("Unknown instruction!");
4997 case ISD::UADDO_CARRY:
4998 BaseOp = SystemZISD::VAC;
4999 FlagOp = SystemZISD::VACCC;
5000 break;
5001 case ISD::USUBO_CARRY:
5002 BaseOp = SystemZISD::VSBI;
5003 FlagOp = SystemZISD::VSBCBI;
5004 IsBorrow = true;
5005 break;
5006 }
5007 if (IsBorrow)
5008 Carry = DAG.getNode(ISD::XOR, DL, Carry.getValueType(),
5009 Carry, DAG.getConstant(1, DL, Carry.getValueType()));
5010 Carry = DAG.getZExtOrTrunc(Carry, DL, MVT::i128);
5011 SDValue Result = DAG.getNode(BaseOp, DL, MVT::i128, LHS, RHS, Carry);
5012 SDValue Flag = DAG.getNode(FlagOp, DL, MVT::i128, LHS, RHS, Carry);
5013 Flag = DAG.getNode(ISD::AssertZext, DL, MVT::i128, Flag,
5014 DAG.getValueType(MVT::i1));
5015 Flag = DAG.getZExtOrTrunc(Flag, DL, N->getValueType(1));
5016 if (IsBorrow)
5017 Flag = DAG.getNode(ISD::XOR, DL, Flag.getValueType(),
5018 Flag, DAG.getConstant(1, DL, Flag.getValueType()));
5019 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Flag);
5020 }
5021
5022 unsigned BaseOp = 0;
5023 unsigned CCValid = 0;
5024 unsigned CCMask = 0;
5025
5026 switch (Op.getOpcode()) {
5027 default: llvm_unreachable("Unknown instruction!");
5028 case ISD::UADDO_CARRY:
5029 if (!isAddCarryChain(Carry))
5030 return SDValue();
5031
5032 BaseOp = SystemZISD::ADDCARRY;
5033 CCValid = SystemZ::CCMASK_LOGICAL;
5035 break;
5036 case ISD::USUBO_CARRY:
5037 if (!isSubBorrowChain(Carry))
5038 return SDValue();
5039
5040 BaseOp = SystemZISD::SUBCARRY;
5041 CCValid = SystemZ::CCMASK_LOGICAL;
5043 break;
5044 }
5045
5046 // Set the condition code from the carry flag.
5047 Carry = DAG.getNode(SystemZISD::GET_CCMASK, DL, MVT::i32, Carry,
5048 DAG.getConstant(CCValid, DL, MVT::i32),
5049 DAG.getConstant(CCMask, DL, MVT::i32));
5050
5051 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5052 SDValue Result = DAG.getNode(BaseOp, DL, VTs, LHS, RHS, Carry);
5053
5054 SDValue SetCC = emitSETCC(DAG, DL, Result.getValue(1), CCValid, CCMask);
5055 if (N->getValueType(1) == MVT::i1)
5056 SetCC = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC);
5057
5058 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, SetCC);
5059}
5060
5061SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
5062 SelectionDAG &DAG) const {
5063 EVT VT = Op.getValueType();
5064 SDLoc DL(Op);
5065 Op = Op.getOperand(0);
5066
5067 if (VT.getScalarSizeInBits() == 128) {
5068 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op);
5069 Op = DAG.getNode(ISD::CTPOP, DL, MVT::v2i64, Op);
5070 SDValue Tmp = DAG.getSplatBuildVector(MVT::v2i64, DL,
5071 DAG.getConstant(0, DL, MVT::i64));
5072 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
5073 return Op;
5074 }
5075
5076 // Handle vector types via VPOPCT.
5077 if (VT.isVector()) {
5078 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
5079 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
5080 switch (VT.getScalarSizeInBits()) {
5081 case 8:
5082 break;
5083 case 16: {
5084 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
5085 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
5086 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
5087 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
5088 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
5089 break;
5090 }
5091 case 32: {
5092 SDValue Tmp = DAG.getSplatBuildVector(MVT::v16i8, DL,
5093 DAG.getConstant(0, DL, MVT::i32));
5094 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
5095 break;
5096 }
5097 case 64: {
5098 SDValue Tmp = DAG.getSplatBuildVector(MVT::v16i8, DL,
5099 DAG.getConstant(0, DL, MVT::i32));
5100 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
5101 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
5102 break;
5103 }
5104 default:
5105 llvm_unreachable("Unexpected type");
5106 }
5107 return Op;
5108 }
5109
5110 // Get the known-zero mask for the operand.
5111 KnownBits Known = DAG.computeKnownBits(Op);
5112 unsigned NumSignificantBits = Known.getMaxValue().getActiveBits();
5113 if (NumSignificantBits == 0)
5114 return DAG.getConstant(0, DL, VT);
5115
5116 // Skip known-zero high parts of the operand.
5117 int64_t OrigBitSize = VT.getSizeInBits();
5118 int64_t BitSize = llvm::bit_ceil(NumSignificantBits);
5119 BitSize = std::min(BitSize, OrigBitSize);
5120
5121 // The POPCNT instruction counts the number of bits in each byte.
5122 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
5123 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
5124 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
5125
5126 // Add up per-byte counts in a binary tree. All bits of Op at
5127 // position larger than BitSize remain zero throughout.
5128 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
5129 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
5130 if (BitSize != OrigBitSize)
5131 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
5132 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
5133 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
5134 }
5135
5136 // Extract overall result from high byte.
5137 if (BitSize > 8)
5138 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
5139 DAG.getConstant(BitSize - 8, DL, VT));
5140
5141 return Op;
5142}
5143
5144SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
5145 SelectionDAG &DAG) const {
5146 SDLoc DL(Op);
5147 AtomicOrdering FenceOrdering =
5148 static_cast<AtomicOrdering>(Op.getConstantOperandVal(1));
5149 SyncScope::ID FenceSSID =
5150 static_cast<SyncScope::ID>(Op.getConstantOperandVal(2));
5151
5152 // The only fence that needs an instruction is a sequentially-consistent
5153 // cross-thread fence.
5154 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
5155 FenceSSID == SyncScope::System) {
5156 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
5157 Op.getOperand(0)),
5158 0);
5159 }
5160
5161 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
5162 return DAG.getNode(ISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
5163}
5164
5165SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
5166 SelectionDAG &DAG) const {
5167 EVT RegVT = Op.getValueType();
5168 if (RegVT.getSizeInBits() == 128)
5169 return lowerATOMIC_LDST_I128(Op, DAG);
5170 return lowerLoadF16(Op, DAG);
5171}
5172
5173SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
5174 SelectionDAG &DAG) const {
5175 auto *Node = cast<AtomicSDNode>(Op.getNode());
5176 if (Node->getMemoryVT().getSizeInBits() == 128)
5177 return lowerATOMIC_LDST_I128(Op, DAG);
5178 return lowerStoreF16(Op, DAG);
5179}
5180
5181SDValue SystemZTargetLowering::lowerATOMIC_LDST_I128(SDValue Op,
5182 SelectionDAG &DAG) const {
5183 auto *Node = cast<AtomicSDNode>(Op.getNode());
5184 assert(
5185 (Node->getMemoryVT() == MVT::i128 || Node->getMemoryVT() == MVT::f128) &&
5186 "Only custom lowering i128 or f128.");
5187 // Use same code to handle both legal and non-legal i128 types.
5189 LowerOperationWrapper(Node, Results, DAG);
5190 return DAG.getMergeValues(Results, SDLoc(Op));
5191}
5192
5193// Prepare for a Compare And Swap for a subword operation. This needs to be
5194// done in memory with 4 bytes at natural alignment.
5196 SDValue &AlignedAddr, SDValue &BitShift,
5197 SDValue &NegBitShift) {
5198 EVT PtrVT = Addr.getValueType();
5199 EVT WideVT = MVT::i32;
5200
5201 // Get the address of the containing word.
5202 AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
5203 DAG.getSignedConstant(-4, DL, PtrVT));
5204
5205 // Get the number of bits that the word must be rotated left in order
5206 // to bring the field to the top bits of a GR32.
5207 BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
5208 DAG.getConstant(3, DL, PtrVT));
5209 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
5210
5211 // Get the complementing shift amount, for rotating a field in the top
5212 // bits back to its proper position.
5213 NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
5214 DAG.getConstant(0, DL, WideVT), BitShift);
5215
5216}
5217
5218// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
5219// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
5220SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
5221 SelectionDAG &DAG,
5222 unsigned Opcode) const {
5223 auto *Node = cast<AtomicSDNode>(Op.getNode());
5224
5225 // 32-bit operations need no special handling.
5226 EVT NarrowVT = Node->getMemoryVT();
5227 EVT WideVT = MVT::i32;
5228 if (NarrowVT == WideVT)
5229 return Op;
5230
5231 int64_t BitSize = NarrowVT.getSizeInBits();
5232 SDValue ChainIn = Node->getChain();
5233 SDValue Addr = Node->getBasePtr();
5234 SDValue Src2 = Node->getVal();
5235 MachineMemOperand *MMO = Node->getMemOperand();
5236 SDLoc DL(Node);
5237
5238 // Convert atomic subtracts of constants into additions.
5239 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
5240 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
5241 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
5242 Src2 = DAG.getSignedConstant(-Const->getSExtValue(), DL,
5243 Src2.getValueType());
5244 }
5245
5246 SDValue AlignedAddr, BitShift, NegBitShift;
5247 getCSAddressAndShifts(Addr, DAG, DL, AlignedAddr, BitShift, NegBitShift);
5248
5249 // Extend the source operand to 32 bits and prepare it for the inner loop.
5250 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
5251 // operations require the source to be shifted in advance. (This shift
5252 // can be folded if the source is constant.) For AND and NAND, the lower
5253 // bits must be set, while for other opcodes they should be left clear.
5254 if (Opcode != SystemZISD::ATOMIC_SWAPW)
5255 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
5256 DAG.getConstant(32 - BitSize, DL, WideVT));
5257 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
5258 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
5259 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
5260 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
5261
5262 // Construct the ATOMIC_LOADW_* node.
5263 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
5264 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
5265 DAG.getConstant(BitSize, DL, WideVT) };
5266 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
5267 NarrowVT, MMO);
5268
5269 // Rotate the result of the final CS so that the field is in the lower
5270 // bits of a GR32, then truncate it.
5271 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
5272 DAG.getConstant(BitSize, DL, WideVT));
5273 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
5274
5275 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
5276 return DAG.getMergeValues(RetOps, DL);
5277}
5278
5279// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations into
5280// ATOMIC_LOADW_SUBs and convert 32- and 64-bit operations into additions.
5281SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
5282 SelectionDAG &DAG) const {
5283 auto *Node = cast<AtomicSDNode>(Op.getNode());
5284 EVT MemVT = Node->getMemoryVT();
5285 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
5286 // A full-width operation: negate and use LAA(G).
5287 assert(Op.getValueType() == MemVT && "Mismatched VTs");
5288 assert(Subtarget.hasInterlockedAccess1() &&
5289 "Should have been expanded by AtomicExpand pass.");
5290 SDValue Src2 = Node->getVal();
5291 SDLoc DL(Src2);
5292 SDValue NegSrc2 =
5293 DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT), Src2);
5294 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
5295 Node->getChain(), Node->getBasePtr(), NegSrc2,
5296 Node->getMemOperand());
5297 }
5298
5299 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
5300}
5301
5302// Lower 8/16/32/64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS node.
5303SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
5304 SelectionDAG &DAG) const {
5305 auto *Node = cast<AtomicSDNode>(Op.getNode());
5306 SDValue ChainIn = Node->getOperand(0);
5307 SDValue Addr = Node->getOperand(1);
5308 SDValue CmpVal = Node->getOperand(2);
5309 SDValue SwapVal = Node->getOperand(3);
5310 MachineMemOperand *MMO = Node->getMemOperand();
5311 SDLoc DL(Node);
5312
5313 if (Node->getMemoryVT() == MVT::i128) {
5314 // Use same code to handle both legal and non-legal i128 types.
5316 LowerOperationWrapper(Node, Results, DAG);
5317 return DAG.getMergeValues(Results, DL);
5318 }
5319
5320 // We have native support for 32-bit and 64-bit compare and swap, but we
5321 // still need to expand extracting the "success" result from the CC.
5322 EVT NarrowVT = Node->getMemoryVT();
5323 EVT WideVT = NarrowVT == MVT::i64 ? MVT::i64 : MVT::i32;
5324 if (NarrowVT == WideVT) {
5325 SDVTList Tys = DAG.getVTList(WideVT, MVT::i32, MVT::Other);
5326 SDValue Ops[] = { ChainIn, Addr, CmpVal, SwapVal };
5327 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP,
5328 DL, Tys, Ops, NarrowVT, MMO);
5329 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(1),
5331
5332 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
5333 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
5334 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2));
5335 return SDValue();
5336 }
5337
5338 // Convert 8-bit and 16-bit compare and swap to a loop, implemented
5339 // via a fullword ATOMIC_CMP_SWAPW operation.
5340 int64_t BitSize = NarrowVT.getSizeInBits();
5341
5342 SDValue AlignedAddr, BitShift, NegBitShift;
5343 getCSAddressAndShifts(Addr, DAG, DL, AlignedAddr, BitShift, NegBitShift);
5344
5345 // Construct the ATOMIC_CMP_SWAPW node.
5346 SDVTList VTList = DAG.getVTList(WideVT, MVT::i32, MVT::Other);
5347 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
5348 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
5349 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
5350 VTList, Ops, NarrowVT, MMO);
5351 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(1),
5353
5354 // emitAtomicCmpSwapW() will zero extend the result (original value).
5355 SDValue OrigVal = DAG.getNode(ISD::AssertZext, DL, WideVT, AtomicOp.getValue(0),
5356 DAG.getValueType(NarrowVT));
5357 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), OrigVal);
5358 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
5359 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2));
5360 return SDValue();
5361}
5362
5364SystemZTargetLowering::getTargetMMOFlags(const Instruction &I) const {
5365 // Because of how we convert atomic_load and atomic_store to normal loads and
5366 // stores in the DAG, we need to ensure that the MMOs are marked volatile
5367 // since DAGCombine hasn't been updated to account for atomic, but non
5368 // volatile loads. (See D57601)
5369 if (auto *SI = dyn_cast<StoreInst>(&I))
5370 if (SI->isAtomic())
5372 if (auto *LI = dyn_cast<LoadInst>(&I))
5373 if (LI->isAtomic())
5375 if (auto *AI = dyn_cast<AtomicRMWInst>(&I))
5376 if (AI->isAtomic())
5378 if (auto *AI = dyn_cast<AtomicCmpXchgInst>(&I))
5379 if (AI->isAtomic())
5382}
5383
5384SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
5385 SelectionDAG &DAG) const {
5386 MachineFunction &MF = DAG.getMachineFunction();
5387 auto *Regs = Subtarget.getSpecialRegisters();
5389 report_fatal_error("Variable-sized stack allocations are not supported "
5390 "in GHC calling convention");
5391 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
5392 Regs->getStackPointerRegister(), Op.getValueType());
5393}
5394
5395SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
5396 SelectionDAG &DAG) const {
5397 MachineFunction &MF = DAG.getMachineFunction();
5398 auto *Regs = Subtarget.getSpecialRegisters();
5399 bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
5400
5402 report_fatal_error("Variable-sized stack allocations are not supported "
5403 "in GHC calling convention");
5404
5405 SDValue Chain = Op.getOperand(0);
5406 SDValue NewSP = Op.getOperand(1);
5407 SDValue Backchain;
5408 SDLoc DL(Op);
5409
5410 if (StoreBackchain) {
5411 SDValue OldSP = DAG.getCopyFromReg(
5412 Chain, DL, Regs->getStackPointerRegister(), MVT::i64);
5413 Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG),
5414 MachinePointerInfo());
5415 }
5416
5417 Chain = DAG.getCopyToReg(Chain, DL, Regs->getStackPointerRegister(), NewSP);
5418
5419 if (StoreBackchain)
5420 Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG),
5421 MachinePointerInfo());
5422
5423 return Chain;
5424}
5425
5426SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
5427 SelectionDAG &DAG) const {
5428 bool IsData = Op.getConstantOperandVal(4);
5429 if (!IsData)
5430 // Just preserve the chain.
5431 return Op.getOperand(0);
5432
5433 SDLoc DL(Op);
5434 bool IsWrite = Op.getConstantOperandVal(2);
5435 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
5436 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
5437 SDValue Ops[] = {Op.getOperand(0), DAG.getTargetConstant(Code, DL, MVT::i32),
5438 Op.getOperand(1)};
5439 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
5440 Node->getVTList(), Ops,
5441 Node->getMemoryVT(), Node->getMemOperand());
5442}
5443
5444SDValue
5445SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
5446 SelectionDAG &DAG) const {
5447 unsigned Opcode, CCValid;
5448 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
5449 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
5450 SDNode *Node = emitIntrinsicWithCCAndChain(DAG, Op, Opcode);
5451 SDValue CC = getCCResult(DAG, SDValue(Node, 0));
5452 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
5453 return SDValue();
5454 }
5455
5456 return SDValue();
5457}
5458
5459SDValue
5460SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
5461 SelectionDAG &DAG) const {
5462 unsigned Opcode, CCValid;
5463 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
5464 SDNode *Node = emitIntrinsicWithCC(DAG, Op, Opcode);
5465 if (Op->getNumValues() == 1)
5466 return getCCResult(DAG, SDValue(Node, 0));
5467 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
5468 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(),
5469 SDValue(Node, 0), getCCResult(DAG, SDValue(Node, 1)));
5470 }
5471
5472 unsigned Id = Op.getConstantOperandVal(0);
5473 switch (Id) {
5474 case Intrinsic::thread_pointer:
5475 return lowerThreadPointer(SDLoc(Op), DAG);
5476
5477 case Intrinsic::s390_vpdi:
5478 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
5479 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5480
5481 case Intrinsic::s390_vperm:
5482 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
5483 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5484
5485 case Intrinsic::s390_vuphb:
5486 case Intrinsic::s390_vuphh:
5487 case Intrinsic::s390_vuphf:
5488 case Intrinsic::s390_vuphg:
5489 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
5490 Op.getOperand(1));
5491
5492 case Intrinsic::s390_vuplhb:
5493 case Intrinsic::s390_vuplhh:
5494 case Intrinsic::s390_vuplhf:
5495 case Intrinsic::s390_vuplhg:
5496 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
5497 Op.getOperand(1));
5498
5499 case Intrinsic::s390_vuplb:
5500 case Intrinsic::s390_vuplhw:
5501 case Intrinsic::s390_vuplf:
5502 case Intrinsic::s390_vuplg:
5503 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
5504 Op.getOperand(1));
5505
5506 case Intrinsic::s390_vupllb:
5507 case Intrinsic::s390_vupllh:
5508 case Intrinsic::s390_vupllf:
5509 case Intrinsic::s390_vupllg:
5510 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
5511 Op.getOperand(1));
5512
5513 case Intrinsic::s390_vsumb:
5514 case Intrinsic::s390_vsumh:
5515 case Intrinsic::s390_vsumgh:
5516 case Intrinsic::s390_vsumgf:
5517 case Intrinsic::s390_vsumqf:
5518 case Intrinsic::s390_vsumqg:
5519 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
5520 Op.getOperand(1), Op.getOperand(2));
5521
5522 case Intrinsic::s390_vaq:
5523 return DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(),
5524 Op.getOperand(1), Op.getOperand(2));
5525 case Intrinsic::s390_vaccb:
5526 case Intrinsic::s390_vacch:
5527 case Intrinsic::s390_vaccf:
5528 case Intrinsic::s390_vaccg:
5529 case Intrinsic::s390_vaccq:
5530 return DAG.getNode(SystemZISD::VACC, SDLoc(Op), Op.getValueType(),
5531 Op.getOperand(1), Op.getOperand(2));
5532 case Intrinsic::s390_vacq:
5533 return DAG.getNode(SystemZISD::VAC, SDLoc(Op), Op.getValueType(),
5534 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5535 case Intrinsic::s390_vacccq:
5536 return DAG.getNode(SystemZISD::VACCC, SDLoc(Op), Op.getValueType(),
5537 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5538
5539 case Intrinsic::s390_vsq:
5540 return DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(),
5541 Op.getOperand(1), Op.getOperand(2));
5542 case Intrinsic::s390_vscbib:
5543 case Intrinsic::s390_vscbih:
5544 case Intrinsic::s390_vscbif:
5545 case Intrinsic::s390_vscbig:
5546 case Intrinsic::s390_vscbiq:
5547 return DAG.getNode(SystemZISD::VSCBI, SDLoc(Op), Op.getValueType(),
5548 Op.getOperand(1), Op.getOperand(2));
5549 case Intrinsic::s390_vsbiq:
5550 return DAG.getNode(SystemZISD::VSBI, SDLoc(Op), Op.getValueType(),
5551 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5552 case Intrinsic::s390_vsbcbiq:
5553 return DAG.getNode(SystemZISD::VSBCBI, SDLoc(Op), Op.getValueType(),
5554 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5555
5556 case Intrinsic::s390_vmhb:
5557 case Intrinsic::s390_vmhh:
5558 case Intrinsic::s390_vmhf:
5559 case Intrinsic::s390_vmhg:
5560 case Intrinsic::s390_vmhq:
5561 return DAG.getNode(ISD::MULHS, SDLoc(Op), Op.getValueType(),
5562 Op.getOperand(1), Op.getOperand(2));
5563 case Intrinsic::s390_vmlhb:
5564 case Intrinsic::s390_vmlhh:
5565 case Intrinsic::s390_vmlhf:
5566 case Intrinsic::s390_vmlhg:
5567 case Intrinsic::s390_vmlhq:
5568 return DAG.getNode(ISD::MULHU, SDLoc(Op), Op.getValueType(),
5569 Op.getOperand(1), Op.getOperand(2));
5570
5571 case Intrinsic::s390_vmahb:
5572 case Intrinsic::s390_vmahh:
5573 case Intrinsic::s390_vmahf:
5574 case Intrinsic::s390_vmahg:
5575 case Intrinsic::s390_vmahq:
5576 return DAG.getNode(SystemZISD::VMAH, SDLoc(Op), Op.getValueType(),
5577 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5578 case Intrinsic::s390_vmalhb:
5579 case Intrinsic::s390_vmalhh:
5580 case Intrinsic::s390_vmalhf:
5581 case Intrinsic::s390_vmalhg:
5582 case Intrinsic::s390_vmalhq:
5583 return DAG.getNode(SystemZISD::VMALH, SDLoc(Op), Op.getValueType(),
5584 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
5585
5586 case Intrinsic::s390_vmeb:
5587 case Intrinsic::s390_vmeh:
5588 case Intrinsic::s390_vmef:
5589 case Intrinsic::s390_vmeg:
5590 return DAG.getNode(SystemZISD::VME, SDLoc(Op), Op.getValueType(),
5591 Op.getOperand(1), Op.getOperand(2));
5592 case Intrinsic::s390_vmleb:
5593 case Intrinsic::s390_vmleh:
5594 case Intrinsic::s390_vmlef:
5595 case Intrinsic::s390_vmleg:
5596 return DAG.getNode(SystemZISD::VMLE, SDLoc(Op), Op.getValueType(),
5597 Op.getOperand(1), Op.getOperand(2));
5598 case Intrinsic::s390_vmob:
5599 case Intrinsic::s390_vmoh:
5600 case Intrinsic::s390_vmof:
5601 case Intrinsic::s390_vmog:
5602 return DAG.getNode(SystemZISD::VMO, SDLoc(Op), Op.getValueType(),
5603 Op.getOperand(1), Op.getOperand(2));
5604 case Intrinsic::s390_vmlob:
5605 case Intrinsic::s390_vmloh:
5606 case Intrinsic::s390_vmlof:
5607 case Intrinsic::s390_vmlog:
5608 return DAG.getNode(SystemZISD::VMLO, SDLoc(Op), Op.getValueType(),
5609 Op.getOperand(1), Op.getOperand(2));
5610
5611 case Intrinsic::s390_vmaeb:
5612 case Intrinsic::s390_vmaeh:
5613 case Intrinsic::s390_vmaef:
5614 case Intrinsic::s390_vmaeg:
5615 return DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(),
5616 DAG.getNode(SystemZISD::VME, SDLoc(Op), Op.getValueType(),
5617 Op.getOperand(1), Op.getOperand(2)),
5618 Op.getOperand(3));
5619 case Intrinsic::s390_vmaleb:
5620 case Intrinsic::s390_vmaleh:
5621 case Intrinsic::s390_vmalef:
5622 case Intrinsic::s390_vmaleg:
5623 return DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(),
5624 DAG.getNode(SystemZISD::VMLE, SDLoc(Op), Op.getValueType(),
5625 Op.getOperand(1), Op.getOperand(2)),
5626 Op.getOperand(3));
5627 case Intrinsic::s390_vmaob:
5628 case Intrinsic::s390_vmaoh:
5629 case Intrinsic::s390_vmaof:
5630 case Intrinsic::s390_vmaog:
5631 return DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(),
5632 DAG.getNode(SystemZISD::VMO, SDLoc(Op), Op.getValueType(),
5633 Op.getOperand(1), Op.getOperand(2)),
5634 Op.getOperand(3));
5635 case Intrinsic::s390_vmalob:
5636 case Intrinsic::s390_vmaloh:
5637 case Intrinsic::s390_vmalof:
5638 case Intrinsic::s390_vmalog:
5639 return DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(),
5640 DAG.getNode(SystemZISD::VMLO, SDLoc(Op), Op.getValueType(),
5641 Op.getOperand(1), Op.getOperand(2)),
5642 Op.getOperand(3));
5643 }
5644
5645 return SDValue();
5646}
5647
5648namespace {
5649// Says that SystemZISD operation Opcode can be used to perform the equivalent
5650// of a VPERM with permute vector Bytes. If Opcode takes three operands,
5651// Operand is the constant third operand, otherwise it is the number of
5652// bytes in each element of the result.
5653struct Permute {
5654 unsigned Opcode;
5655 unsigned Operand;
5656 unsigned char Bytes[SystemZ::VectorBytes];
5657};
5658}
5659
5660static const Permute PermuteForms[] = {
5661 // VMRHG
5662 { SystemZISD::MERGE_HIGH, 8,
5663 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
5664 // VMRHF
5665 { SystemZISD::MERGE_HIGH, 4,
5666 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
5667 // VMRHH
5668 { SystemZISD::MERGE_HIGH, 2,
5669 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
5670 // VMRHB
5671 { SystemZISD::MERGE_HIGH, 1,
5672 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
5673 // VMRLG
5674 { SystemZISD::MERGE_LOW, 8,
5675 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
5676 // VMRLF
5677 { SystemZISD::MERGE_LOW, 4,
5678 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
5679 // VMRLH
5680 { SystemZISD::MERGE_LOW, 2,
5681 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
5682 // VMRLB
5683 { SystemZISD::MERGE_LOW, 1,
5684 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
5685 // VPKG
5686 { SystemZISD::PACK, 4,
5687 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
5688 // VPKF
5689 { SystemZISD::PACK, 2,
5690 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
5691 // VPKH
5692 { SystemZISD::PACK, 1,
5693 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
5694 // VPDI V1, V2, 4 (low half of V1, high half of V2)
5695 { SystemZISD::PERMUTE_DWORDS, 4,
5696 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
5697 // VPDI V1, V2, 1 (high half of V1, low half of V2)
5698 { SystemZISD::PERMUTE_DWORDS, 1,
5699 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
5700};
5701
5702// Called after matching a vector shuffle against a particular pattern.
5703// Both the original shuffle and the pattern have two vector operands.
5704// OpNos[0] is the operand of the original shuffle that should be used for
5705// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
5706// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
5707// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
5708// for operands 0 and 1 of the pattern.
5709static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
5710 if (OpNos[0] < 0) {
5711 if (OpNos[1] < 0)
5712 return false;
5713 OpNo0 = OpNo1 = OpNos[1];
5714 } else if (OpNos[1] < 0) {
5715 OpNo0 = OpNo1 = OpNos[0];
5716 } else {
5717 OpNo0 = OpNos[0];
5718 OpNo1 = OpNos[1];
5719 }
5720 return true;
5721}
5722
5723// Bytes is a VPERM-like permute vector, except that -1 is used for
5724// undefined bytes. Return true if the VPERM can be implemented using P.
5725// When returning true set OpNo0 to the VPERM operand that should be
5726// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
5727//
5728// For example, if swapping the VPERM operands allows P to match, OpNo0
5729// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
5730// operand, but rewriting it to use two duplicated operands allows it to
5731// match P, then OpNo0 and OpNo1 will be the same.
5732static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
5733 unsigned &OpNo0, unsigned &OpNo1) {
5734 int OpNos[] = { -1, -1 };
5735 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
5736 int Elt = Bytes[I];
5737 if (Elt >= 0) {
5738 // Make sure that the two permute vectors use the same suboperand
5739 // byte number. Only the operand numbers (the high bits) are
5740 // allowed to differ.
5741 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
5742 return false;
5743 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
5744 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
5745 // Make sure that the operand mappings are consistent with previous
5746 // elements.
5747 if (OpNos[ModelOpNo] == 1 - RealOpNo)
5748 return false;
5749 OpNos[ModelOpNo] = RealOpNo;
5750 }
5751 }
5752 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
5753}
5754
5755// As above, but search for a matching permute.
5756static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
5757 unsigned &OpNo0, unsigned &OpNo1) {
5758 for (auto &P : PermuteForms)
5759 if (matchPermute(Bytes, P, OpNo0, OpNo1))
5760 return &P;
5761 return nullptr;
5762}
5763
5764// Bytes is a VPERM-like permute vector, except that -1 is used for
5765// undefined bytes. This permute is an operand of an outer permute.
5766// See whether redistributing the -1 bytes gives a shuffle that can be
5767// implemented using P. If so, set Transform to a VPERM-like permute vector
5768// that, when applied to the result of P, gives the original permute in Bytes.
5770 const Permute &P,
5771 SmallVectorImpl<int> &Transform) {
5772 unsigned To = 0;
5773 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
5774 int Elt = Bytes[From];
5775 if (Elt < 0)
5776 // Byte number From of the result is undefined.
5777 Transform[From] = -1;
5778 else {
5779 while (P.Bytes[To] != Elt) {
5780 To += 1;
5781 if (To == SystemZ::VectorBytes)
5782 return false;
5783 }
5784 Transform[From] = To;
5785 }
5786 }
5787 return true;
5788}
5789
5790// As above, but search for a matching permute.
5791static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
5792 SmallVectorImpl<int> &Transform) {
5793 for (auto &P : PermuteForms)
5794 if (matchDoublePermute(Bytes, P, Transform))
5795 return &P;
5796 return nullptr;
5797}
5798
5799// Convert the mask of the given shuffle op into a byte-level mask,
5800// as if it had type vNi8.
5801static bool getVPermMask(SDValue ShuffleOp,
5802 SmallVectorImpl<int> &Bytes) {
5803 EVT VT = ShuffleOp.getValueType();
5804 unsigned NumElements = VT.getVectorNumElements();
5805 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
5806
5807 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(ShuffleOp)) {
5808 Bytes.resize(NumElements * BytesPerElement, -1);
5809 for (unsigned I = 0; I < NumElements; ++I) {
5810 int Index = VSN->getMaskElt(I);
5811 if (Index >= 0)
5812 for (unsigned J = 0; J < BytesPerElement; ++J)
5813 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
5814 }
5815 return true;
5816 }
5817 if (SystemZISD::SPLAT == ShuffleOp.getOpcode() &&
5818 isa<ConstantSDNode>(ShuffleOp.getOperand(1))) {
5819 unsigned Index = ShuffleOp.getConstantOperandVal(1);
5820 Bytes.resize(NumElements * BytesPerElement, -1);
5821 for (unsigned I = 0; I < NumElements; ++I)
5822 for (unsigned J = 0; J < BytesPerElement; ++J)
5823 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
5824 return true;
5825 }
5826 return false;
5827}
5828
5829// Bytes is a VPERM-like permute vector, except that -1 is used for
5830// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
5831// the result come from a contiguous sequence of bytes from one input.
5832// Set Base to the selector for the first byte if so.
5833static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
5834 unsigned BytesPerElement, int &Base) {
5835 Base = -1;
5836 for (unsigned I = 0; I < BytesPerElement; ++I) {
5837 if (Bytes[Start + I] >= 0) {
5838 unsigned Elem = Bytes[Start + I];
5839 if (Base < 0) {
5840 Base = Elem - I;
5841 // Make sure the bytes would come from one input operand.
5842 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
5843 return false;
5844 } else if (unsigned(Base) != Elem - I)
5845 return false;
5846 }
5847 }
5848 return true;
5849}
5850
5851// Bytes is a VPERM-like permute vector, except that -1 is used for
5852// undefined bytes. Return true if it can be performed using VSLDB.
5853// When returning true, set StartIndex to the shift amount and OpNo0
5854// and OpNo1 to the VPERM operands that should be used as the first
5855// and second shift operand respectively.
5857 unsigned &StartIndex, unsigned &OpNo0,
5858 unsigned &OpNo1) {
5859 int OpNos[] = { -1, -1 };
5860 int Shift = -1;
5861 for (unsigned I = 0; I < 16; ++I) {
5862 int Index = Bytes[I];
5863 if (Index >= 0) {
5864 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
5865 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
5866 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
5867 if (Shift < 0)
5868 Shift = ExpectedShift;
5869 else if (Shift != ExpectedShift)
5870 return false;
5871 // Make sure that the operand mappings are consistent with previous
5872 // elements.
5873 if (OpNos[ModelOpNo] == 1 - RealOpNo)
5874 return false;
5875 OpNos[ModelOpNo] = RealOpNo;
5876 }
5877 }
5878 StartIndex = Shift;
5879 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
5880}
5881
5882// Create a node that performs P on operands Op0 and Op1, casting the
5883// operands to the appropriate type. The type of the result is determined by P.
5885 const Permute &P, SDValue Op0, SDValue Op1) {
5886 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
5887 // elements of a PACK are twice as wide as the outputs.
5888 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
5889 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
5890 P.Operand);
5891 // Cast both operands to the appropriate type.
5892 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
5893 SystemZ::VectorBytes / InBytes);
5894 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
5895 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
5896 SDValue Op;
5897 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
5898 SDValue Op2 = DAG.getTargetConstant(P.Operand, DL, MVT::i32);
5899 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
5900 } else if (P.Opcode == SystemZISD::PACK) {
5901 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
5902 SystemZ::VectorBytes / P.Operand);
5903 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
5904 } else {
5905 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
5906 }
5907 return Op;
5908}
5909
5910static bool isZeroVector(SDValue N) {
5911 if (N->getOpcode() == ISD::BITCAST)
5912 N = N->getOperand(0);
5913 if (N->getOpcode() == ISD::SPLAT_VECTOR)
5914 if (auto *Op = dyn_cast<ConstantSDNode>(N->getOperand(0)))
5915 return Op->getZExtValue() == 0;
5916 return ISD::isBuildVectorAllZeros(N.getNode());
5917}
5918
5919// Return the index of the zero/undef vector, or UINT32_MAX if not found.
5920static uint32_t findZeroVectorIdx(SDValue *Ops, unsigned Num) {
5921 for (unsigned I = 0; I < Num ; I++)
5922 if (isZeroVector(Ops[I]))
5923 return I;
5924 return UINT32_MAX;
5925}
5926
5927// Bytes is a VPERM-like permute vector, except that -1 is used for
5928// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
5929// VSLDB or VPERM.
5931 SDValue *Ops,
5932 const SmallVectorImpl<int> &Bytes) {
5933 for (unsigned I = 0; I < 2; ++I)
5934 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
5935
5936 // First see whether VSLDB can be used.
5937 unsigned StartIndex, OpNo0, OpNo1;
5938 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
5939 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
5940 Ops[OpNo1],
5941 DAG.getTargetConstant(StartIndex, DL, MVT::i32));
5942
5943 // Fall back on VPERM. Construct an SDNode for the permute vector. Try to
5944 // eliminate a zero vector by reusing any zero index in the permute vector.
5945 unsigned ZeroVecIdx = findZeroVectorIdx(&Ops[0], 2);
5946 if (ZeroVecIdx != UINT32_MAX) {
5947 bool MaskFirst = true;
5948 int ZeroIdx = -1;
5949 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
5950 unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes;
5951 unsigned Byte = unsigned(Bytes[I]) % SystemZ::VectorBytes;
5952 if (OpNo == ZeroVecIdx && I == 0) {
5953 // If the first byte is zero, use mask as first operand.
5954 ZeroIdx = 0;
5955 break;
5956 }
5957 if (OpNo != ZeroVecIdx && Byte == 0) {
5958 // If mask contains a zero, use it by placing that vector first.
5959 ZeroIdx = I + SystemZ::VectorBytes;
5960 MaskFirst = false;
5961 break;
5962 }
5963 }
5964 if (ZeroIdx != -1) {
5965 SDValue IndexNodes[SystemZ::VectorBytes];
5966 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
5967 if (Bytes[I] >= 0) {
5968 unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes;
5969 unsigned Byte = unsigned(Bytes[I]) % SystemZ::VectorBytes;
5970 if (OpNo == ZeroVecIdx)
5971 IndexNodes[I] = DAG.getConstant(ZeroIdx, DL, MVT::i32);
5972 else {
5973 unsigned BIdx = MaskFirst ? Byte + SystemZ::VectorBytes : Byte;
5974 IndexNodes[I] = DAG.getConstant(BIdx, DL, MVT::i32);
5975 }
5976 } else
5977 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
5978 }
5979 SDValue Mask = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
5980 SDValue Src = ZeroVecIdx == 0 ? Ops[1] : Ops[0];
5981 if (MaskFirst)
5982 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Mask, Src,
5983 Mask);
5984 else
5985 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Src, Mask,
5986 Mask);
5987 }
5988 }
5989
5990 SDValue IndexNodes[SystemZ::VectorBytes];
5991 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
5992 if (Bytes[I] >= 0)
5993 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
5994 else
5995 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
5996 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
5997 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0],
5998 (!Ops[1].isUndef() ? Ops[1] : Ops[0]), Op2);
5999}
6000
6001namespace {
6002// Describes a general N-operand vector shuffle.
6003struct GeneralShuffle {
6004 GeneralShuffle(EVT vt)
6005 : VT(vt), UnpackFromEltSize(UINT_MAX), UnpackLow(false) {}
6006 void addUndef();
6007 bool add(SDValue, unsigned);
6008 SDValue getNode(SelectionDAG &, const SDLoc &);
6009 void tryPrepareForUnpack();
6010 bool unpackWasPrepared() { return UnpackFromEltSize <= 4; }
6011 SDValue insertUnpackIfPrepared(SelectionDAG &DAG, const SDLoc &DL, SDValue Op);
6012
6013 // The operands of the shuffle.
6015
6016 // Index I is -1 if byte I of the result is undefined. Otherwise the
6017 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
6018 // Bytes[I] / SystemZ::VectorBytes.
6020
6021 // The type of the shuffle result.
6022 EVT VT;
6023
6024 // Holds a value of 1, 2 or 4 if a final unpack has been prepared for.
6025 unsigned UnpackFromEltSize;
6026 // True if the final unpack uses the low half.
6027 bool UnpackLow;
6028};
6029} // namespace
6030
6031// Add an extra undefined element to the shuffle.
6032void GeneralShuffle::addUndef() {
6033 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
6034 for (unsigned I = 0; I < BytesPerElement; ++I)
6035 Bytes.push_back(-1);
6036}
6037
6038// Add an extra element to the shuffle, taking it from element Elem of Op.
6039// A null Op indicates a vector input whose value will be calculated later;
6040// there is at most one such input per shuffle and it always has the same
6041// type as the result. Aborts and returns false if the source vector elements
6042// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
6043// LLVM they become implicitly extended, but this is rare and not optimized.
6044bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
6045 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
6046
6047 // The source vector can have wider elements than the result,
6048 // either through an explicit TRUNCATE or because of type legalization.
6049 // We want the least significant part.
6050 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
6051 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
6052
6053 // Return false if the source elements are smaller than their destination
6054 // elements.
6055 if (FromBytesPerElement < BytesPerElement)
6056 return false;
6057
6058 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
6059 (FromBytesPerElement - BytesPerElement));
6060
6061 // Look through things like shuffles and bitcasts.
6062 while (Op.getNode()) {
6063 if (Op.getOpcode() == ISD::BITCAST)
6064 Op = Op.getOperand(0);
6065 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
6066 // See whether the bytes we need come from a contiguous part of one
6067 // operand.
6069 if (!getVPermMask(Op, OpBytes))
6070 break;
6071 int NewByte;
6072 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
6073 break;
6074 if (NewByte < 0) {
6075 addUndef();
6076 return true;
6077 }
6078 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
6079 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
6080 } else if (Op.isUndef()) {
6081 addUndef();
6082 return true;
6083 } else
6084 break;
6085 }
6086
6087 // Make sure that the source of the extraction is in Ops.
6088 unsigned OpNo = 0;
6089 for (; OpNo < Ops.size(); ++OpNo)
6090 if (Ops[OpNo] == Op)
6091 break;
6092 if (OpNo == Ops.size())
6093 Ops.push_back(Op);
6094
6095 // Add the element to Bytes.
6096 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
6097 for (unsigned I = 0; I < BytesPerElement; ++I)
6098 Bytes.push_back(Base + I);
6099
6100 return true;
6101}
6102
6103// Return SDNodes for the completed shuffle.
6104SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
6105 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
6106
6107 if (Ops.size() == 0)
6108 return DAG.getUNDEF(VT);
6109
6110 // Use a single unpack if possible as the last operation.
6111 tryPrepareForUnpack();
6112
6113 // Make sure that there are at least two shuffle operands.
6114 if (Ops.size() == 1)
6115 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
6116
6117 // Create a tree of shuffles, deferring root node until after the loop.
6118 // Try to redistribute the undefined elements of non-root nodes so that
6119 // the non-root shuffles match something like a pack or merge, then adjust
6120 // the parent node's permute vector to compensate for the new order.
6121 // Among other things, this copes with vectors like <2 x i16> that were
6122 // padded with undefined elements during type legalization.
6123 //
6124 // In the best case this redistribution will lead to the whole tree
6125 // using packs and merges. It should rarely be a loss in other cases.
6126 unsigned Stride = 1;
6127 for (; Stride * 2 < Ops.size(); Stride *= 2) {
6128 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
6129 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
6130
6131 // Create a mask for just these two operands.
6133 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
6134 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
6135 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
6136 if (OpNo == I)
6137 NewBytes[J] = Byte;
6138 else if (OpNo == I + Stride)
6139 NewBytes[J] = SystemZ::VectorBytes + Byte;
6140 else
6141 NewBytes[J] = -1;
6142 }
6143 // See if it would be better to reorganize NewMask to avoid using VPERM.
6145 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
6146 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
6147 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
6148 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
6149 if (NewBytes[J] >= 0) {
6150 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
6151 "Invalid double permute");
6152 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
6153 } else
6154 assert(NewBytesMap[J] < 0 && "Invalid double permute");
6155 }
6156 } else {
6157 // Just use NewBytes on the operands.
6158 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
6159 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
6160 if (NewBytes[J] >= 0)
6161 Bytes[J] = I * SystemZ::VectorBytes + J;
6162 }
6163 }
6164 }
6165
6166 // Now we just have 2 inputs. Put the second operand in Ops[1].
6167 if (Stride > 1) {
6168 Ops[1] = Ops[Stride];
6169 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
6170 if (Bytes[I] >= int(SystemZ::VectorBytes))
6171 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
6172 }
6173
6174 // Look for an instruction that can do the permute without resorting
6175 // to VPERM.
6176 unsigned OpNo0, OpNo1;
6177 SDValue Op;
6178 if (unpackWasPrepared() && Ops[1].isUndef())
6179 Op = Ops[0];
6180 else if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
6181 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
6182 else
6183 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
6184
6185 Op = insertUnpackIfPrepared(DAG, DL, Op);
6186
6187 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
6188}
6189
6190#ifndef NDEBUG
6191static void dumpBytes(const SmallVectorImpl<int> &Bytes, std::string Msg) {
6192 dbgs() << Msg.c_str() << " { ";
6193 for (unsigned I = 0; I < Bytes.size(); I++)
6194 dbgs() << Bytes[I] << " ";
6195 dbgs() << "}\n";
6196}
6197#endif
6198
6199// If the Bytes vector matches an unpack operation, prepare to do the unpack
6200// after all else by removing the zero vector and the effect of the unpack on
6201// Bytes.
6202void GeneralShuffle::tryPrepareForUnpack() {
6203 uint32_t ZeroVecOpNo = findZeroVectorIdx(&Ops[0], Ops.size());
6204 if (ZeroVecOpNo == UINT32_MAX || Ops.size() == 1)
6205 return;
6206
6207 // Only do this if removing the zero vector reduces the depth, otherwise
6208 // the critical path will increase with the final unpack.
6209 if (Ops.size() > 2 &&
6210 Log2_32_Ceil(Ops.size()) == Log2_32_Ceil(Ops.size() - 1))
6211 return;
6212
6213 // Find an unpack that would allow removing the zero vector from Ops.
6214 UnpackFromEltSize = 1;
6215 for (; UnpackFromEltSize <= 4; UnpackFromEltSize *= 2) {
6216 bool MatchUnpack = true;
6218 for (unsigned Elt = 0; Elt < SystemZ::VectorBytes; Elt++) {
6219 unsigned ToEltSize = UnpackFromEltSize * 2;
6220 bool IsZextByte = (Elt % ToEltSize) < UnpackFromEltSize;
6221 if (!IsZextByte)
6222 SrcBytes.push_back(Bytes[Elt]);
6223 if (Bytes[Elt] != -1) {
6224 unsigned OpNo = unsigned(Bytes[Elt]) / SystemZ::VectorBytes;
6225 if (IsZextByte != (OpNo == ZeroVecOpNo)) {
6226 MatchUnpack = false;
6227 break;
6228 }
6229 }
6230 }
6231 if (MatchUnpack) {
6232 if (Ops.size() == 2) {
6233 // Don't use unpack if a single source operand needs rearrangement.
6234 bool CanUseUnpackLow = true, CanUseUnpackHigh = true;
6235 for (unsigned i = 0; i < SystemZ::VectorBytes / 2; i++) {
6236 if (SrcBytes[i] == -1)
6237 continue;
6238 if (SrcBytes[i] % 16 != int(i))
6239 CanUseUnpackHigh = false;
6240 if (SrcBytes[i] % 16 != int(i + SystemZ::VectorBytes / 2))
6241 CanUseUnpackLow = false;
6242 if (!CanUseUnpackLow && !CanUseUnpackHigh) {
6243 UnpackFromEltSize = UINT_MAX;
6244 return;
6245 }
6246 }
6247 if (!CanUseUnpackHigh)
6248 UnpackLow = true;
6249 }
6250 break;
6251 }
6252 }
6253 if (UnpackFromEltSize > 4)
6254 return;
6255
6256 LLVM_DEBUG(dbgs() << "Preparing for final unpack of element size "
6257 << UnpackFromEltSize << ". Zero vector is Op#" << ZeroVecOpNo
6258 << ".\n";
6259 dumpBytes(Bytes, "Original Bytes vector:"););
6260
6261 // Apply the unpack in reverse to the Bytes array.
6262 unsigned B = 0;
6263 if (UnpackLow) {
6264 while (B < SystemZ::VectorBytes / 2)
6265 Bytes[B++] = -1;
6266 }
6267 for (unsigned Elt = 0; Elt < SystemZ::VectorBytes;) {
6268 Elt += UnpackFromEltSize;
6269 for (unsigned i = 0; i < UnpackFromEltSize; i++, Elt++, B++)
6270 Bytes[B] = Bytes[Elt];
6271 }
6272 if (!UnpackLow) {
6273 while (B < SystemZ::VectorBytes)
6274 Bytes[B++] = -1;
6275 }
6276
6277 // Remove the zero vector from Ops
6278 Ops.erase(&Ops[ZeroVecOpNo]);
6279 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
6280 if (Bytes[I] >= 0) {
6281 unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes;
6282 if (OpNo > ZeroVecOpNo)
6283 Bytes[I] -= SystemZ::VectorBytes;
6284 }
6285
6286 LLVM_DEBUG(dumpBytes(Bytes, "Resulting Bytes vector, zero vector removed:");
6287 dbgs() << "\n";);
6288}
6289
6290SDValue GeneralShuffle::insertUnpackIfPrepared(SelectionDAG &DAG,
6291 const SDLoc &DL,
6292 SDValue Op) {
6293 if (!unpackWasPrepared())
6294 return Op;
6295 unsigned InBits = UnpackFromEltSize * 8;
6296 EVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBits),
6297 SystemZ::VectorBits / InBits);
6298 SDValue PackedOp = DAG.getNode(ISD::BITCAST, DL, InVT, Op);
6299 unsigned OutBits = InBits * 2;
6300 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(OutBits),
6301 SystemZ::VectorBits / OutBits);
6302 return DAG.getNode(UnpackLow ? SystemZISD::UNPACKL_LOW
6303 : SystemZISD::UNPACKL_HIGH,
6304 DL, OutVT, PackedOp);
6305}
6306
6307// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
6309 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
6310 if (!Op.getOperand(I).isUndef())
6311 return false;
6312 return true;
6313}
6314
6315// Return a vector of type VT that contains Value in the first element.
6316// The other elements don't matter.
6318 SDValue Value) {
6319 // If we have a constant, replicate it to all elements and let the
6320 // BUILD_VECTOR lowering take care of it.
6321 if (Value.getOpcode() == ISD::Constant ||
6322 Value.getOpcode() == ISD::ConstantFP) {
6324 return DAG.getBuildVector(VT, DL, Ops);
6325 }
6326 if (Value.isUndef())
6327 return DAG.getUNDEF(VT);
6328 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
6329}
6330
6331// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
6332// element 1. Used for cases in which replication is cheap.
6334 SDValue Op0, SDValue Op1) {
6335 if (Op0.isUndef()) {
6336 if (Op1.isUndef())
6337 return DAG.getUNDEF(VT);
6338 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
6339 }
6340 if (Op1.isUndef())
6341 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
6342 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
6343 buildScalarToVector(DAG, DL, VT, Op0),
6344 buildScalarToVector(DAG, DL, VT, Op1));
6345}
6346
6347// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
6348// vector for them.
6350 SDValue Op1) {
6351 if (Op0.isUndef() && Op1.isUndef())
6352 return DAG.getUNDEF(MVT::v2i64);
6353 // If one of the two inputs is undefined then replicate the other one,
6354 // in order to avoid using another register unnecessarily.
6355 if (Op0.isUndef())
6356 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
6357 else if (Op1.isUndef())
6358 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
6359 else {
6360 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
6361 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
6362 }
6363 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
6364}
6365
6366// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
6367// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
6368// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
6369// would benefit from this representation and return it if so.
6371 BuildVectorSDNode *BVN) {
6372 EVT VT = BVN->getValueType(0);
6373 unsigned NumElements = VT.getVectorNumElements();
6374
6375 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
6376 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
6377 // need a BUILD_VECTOR, add an additional placeholder operand for that
6378 // BUILD_VECTOR and store its operands in ResidueOps.
6379 GeneralShuffle GS(VT);
6381 bool FoundOne = false;
6382 for (unsigned I = 0; I < NumElements; ++I) {
6383 SDValue Op = BVN->getOperand(I);
6384 if (Op.getOpcode() == ISD::TRUNCATE)
6385 Op = Op.getOperand(0);
6386 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6387 Op.getOperand(1).getOpcode() == ISD::Constant) {
6388 unsigned Elem = Op.getConstantOperandVal(1);
6389 if (!GS.add(Op.getOperand(0), Elem))
6390 return SDValue();
6391 FoundOne = true;
6392 } else if (Op.isUndef()) {
6393 GS.addUndef();
6394 } else {
6395 if (!GS.add(SDValue(), ResidueOps.size()))
6396 return SDValue();
6397 ResidueOps.push_back(BVN->getOperand(I));
6398 }
6399 }
6400
6401 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
6402 if (!FoundOne)
6403 return SDValue();
6404
6405 // Create the BUILD_VECTOR for the remaining elements, if any.
6406 if (!ResidueOps.empty()) {
6407 while (ResidueOps.size() < NumElements)
6408 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
6409 for (auto &Op : GS.Ops) {
6410 if (!Op.getNode()) {
6411 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
6412 break;
6413 }
6414 }
6415 }
6416 return GS.getNode(DAG, SDLoc(BVN));
6417}
6418
6419bool SystemZTargetLowering::isVectorElementLoad(SDValue Op) const {
6420 if (Op.getOpcode() == ISD::LOAD && cast<LoadSDNode>(Op)->isUnindexed())
6421 return true;
6422 if (auto *AL = dyn_cast<AtomicSDNode>(Op))
6423 if (AL->getOpcode() == ISD::ATOMIC_LOAD)
6424 return true;
6425 if (Subtarget.hasVectorEnhancements2() && Op.getOpcode() == SystemZISD::LRV)
6426 return true;
6427 return false;
6428}
6429
6431 unsigned MergedBits, EVT VT, SDValue Op0,
6432 SDValue Op1) {
6433 MVT IntVecVT = MVT::getVectorVT(MVT::getIntegerVT(MergedBits),
6434 SystemZ::VectorBits / MergedBits);
6435 assert(VT.getSizeInBits() == 128 && IntVecVT.getSizeInBits() == 128 &&
6436 "Handling full vectors only.");
6437 Op0 = DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0);
6438 Op1 = DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op1);
6439 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, DL, IntVecVT, Op0, Op1);
6440 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
6441}
6442
6444 EVT VT, SmallVectorImpl<SDValue> &Elems,
6445 unsigned Pos) {
6446 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[Pos + 0], Elems[Pos + 1]);
6447 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[Pos + 2], Elems[Pos + 3]);
6448 // Avoid unnecessary undefs by reusing the other operand.
6449 if (Op01.isUndef()) {
6450 if (Op23.isUndef())
6451 return Op01;
6452 Op01 = Op23;
6453 } else if (Op23.isUndef())
6454 Op23 = Op01;
6455 // Merging identical replications is a no-op.
6456 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
6457 return Op01;
6458 unsigned MergedBits = VT.getSimpleVT().getScalarSizeInBits() * 2;
6459 return mergeHighParts(DAG, DL, MergedBits, VT, Op01, Op23);
6460}
6461
6462// Combine GPR scalar values Elems into a vector of type VT.
6463SDValue
6464SystemZTargetLowering::buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
6465 SmallVectorImpl<SDValue> &Elems) const {
6466 // See whether there is a single replicated value.
6468 unsigned int NumElements = Elems.size();
6469 unsigned int Count = 0;
6470 for (auto Elem : Elems) {
6471 if (!Elem.isUndef()) {
6472 if (!Single.getNode())
6473 Single = Elem;
6474 else if (Elem != Single) {
6475 Single = SDValue();
6476 break;
6477 }
6478 Count += 1;
6479 }
6480 }
6481 // There are three cases here:
6482 //
6483 // - if the only defined element is a loaded one, the best sequence
6484 // is a replicating load.
6485 //
6486 // - otherwise, if the only defined element is an i64 value, we will
6487 // end up with the same VLVGP sequence regardless of whether we short-cut
6488 // for replication or fall through to the later code.
6489 //
6490 // - otherwise, if the only defined element is an i32 or smaller value,
6491 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
6492 // This is only a win if the single defined element is used more than once.
6493 // In other cases we're better off using a single VLVGx.
6494 if (Single.getNode() && (Count > 1 || isVectorElementLoad(Single)))
6495 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
6496
6497 // If all elements are loads, use VLREP/VLEs (below).
6498 bool AllLoads = true;
6499 for (auto Elem : Elems)
6500 if (!isVectorElementLoad(Elem)) {
6501 AllLoads = false;
6502 break;
6503 }
6504
6505 // The best way of building a v2i64 from two i64s is to use VLVGP.
6506 if (VT == MVT::v2i64 && !AllLoads)
6507 return joinDwords(DAG, DL, Elems[0], Elems[1]);
6508
6509 // Use a 64-bit merge high to combine two doubles.
6510 if (VT == MVT::v2f64 && !AllLoads)
6511 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
6512
6513 // Build v4f32 values directly from the FPRs:
6514 //
6515 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
6516 // V V VMRHF
6517 // <ABxx> <CDxx>
6518 // V VMRHG
6519 // <ABCD>
6520 if (VT == MVT::v4f32 && !AllLoads)
6521 return buildFPVecFromScalars4(DAG, DL, VT, Elems, 0);
6522
6523 // Same for v8f16.
6524 if (VT == MVT::v8f16 && !AllLoads) {
6525 SDValue Op0123 = buildFPVecFromScalars4(DAG, DL, VT, Elems, 0);
6526 SDValue Op4567 = buildFPVecFromScalars4(DAG, DL, VT, Elems, 4);
6527 // Avoid unnecessary undefs by reusing the other operand.
6528 if (Op0123.isUndef())
6529 Op0123 = Op4567;
6530 else if (Op4567.isUndef())
6531 Op4567 = Op0123;
6532 // Merging identical replications is a no-op.
6533 if (Op0123.getOpcode() == SystemZISD::REPLICATE && Op0123 == Op4567)
6534 return Op0123;
6535 return mergeHighParts(DAG, DL, 64, VT, Op0123, Op4567);
6536 }
6537
6538 // Collect the constant terms.
6541
6542 unsigned NumConstants = 0;
6543 for (unsigned I = 0; I < NumElements; ++I) {
6544 SDValue Elem = Elems[I];
6545 if (Elem.getOpcode() == ISD::Constant ||
6546 Elem.getOpcode() == ISD::ConstantFP) {
6547 NumConstants += 1;
6548 Constants[I] = Elem;
6549 Done[I] = true;
6550 }
6551 }
6552 // If there was at least one constant, fill in the other elements of
6553 // Constants with undefs to get a full vector constant and use that
6554 // as the starting point.
6556 SDValue ReplicatedVal;
6557 if (NumConstants > 0) {
6558 for (unsigned I = 0; I < NumElements; ++I)
6559 if (!Constants[I].getNode())
6560 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
6561 Result = DAG.getBuildVector(VT, DL, Constants);
6562 } else {
6563 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
6564 // avoid a false dependency on any previous contents of the vector
6565 // register.
6566
6567 // Use a VLREP if at least one element is a load. Make sure to replicate
6568 // the load with the most elements having its value.
6569 std::map<const SDNode*, unsigned> UseCounts;
6570 SDNode *LoadMaxUses = nullptr;
6571 for (unsigned I = 0; I < NumElements; ++I)
6572 if (isVectorElementLoad(Elems[I])) {
6573 SDNode *Ld = Elems[I].getNode();
6574 unsigned Count = ++UseCounts[Ld];
6575 if (LoadMaxUses == nullptr || UseCounts[LoadMaxUses] < Count)
6576 LoadMaxUses = Ld;
6577 }
6578 if (LoadMaxUses != nullptr) {
6579 ReplicatedVal = SDValue(LoadMaxUses, 0);
6580 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, ReplicatedVal);
6581 } else {
6582 // Try to use VLVGP.
6583 unsigned I1 = NumElements / 2 - 1;
6584 unsigned I2 = NumElements - 1;
6585 bool Def1 = !Elems[I1].isUndef();
6586 bool Def2 = !Elems[I2].isUndef();
6587 if (Def1 || Def2) {
6588 SDValue Elem1 = Elems[Def1 ? I1 : I2];
6589 SDValue Elem2 = Elems[Def2 ? I2 : I1];
6590 Result = DAG.getNode(ISD::BITCAST, DL, VT,
6591 joinDwords(DAG, DL, Elem1, Elem2));
6592 Done[I1] = true;
6593 Done[I2] = true;
6594 } else
6595 Result = DAG.getUNDEF(VT);
6596 }
6597 }
6598
6599 // Use VLVGx to insert the other elements.
6600 for (unsigned I = 0; I < NumElements; ++I)
6601 if (!Done[I] && !Elems[I].isUndef() && Elems[I] != ReplicatedVal)
6602 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
6603 DAG.getConstant(I, DL, MVT::i32));
6604 return Result;
6605}
6606
6607SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
6608 SelectionDAG &DAG) const {
6609 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
6610 SDLoc DL(Op);
6611 EVT VT = Op.getValueType();
6612
6613 if (BVN->isConstant()) {
6614 if (SystemZVectorConstantInfo(BVN).isVectorConstantLegal(Subtarget))
6615 return Op;
6616
6617 // Fall back to loading it from memory.
6618 return SDValue();
6619 }
6620
6621 // See if we should use shuffles to construct the vector from other vectors.
6622 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
6623 return Res;
6624
6625 // Detect SCALAR_TO_VECTOR conversions.
6627 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
6628
6629 // Otherwise use buildVector to build the vector up from GPRs.
6630 unsigned NumElements = Op.getNumOperands();
6632 for (unsigned I = 0; I < NumElements; ++I)
6633 Ops[I] = Op.getOperand(I);
6634 return buildVector(DAG, DL, VT, Ops);
6635}
6636
6637SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
6638 SelectionDAG &DAG) const {
6639 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
6640 SDLoc DL(Op);
6641 EVT VT = Op.getValueType();
6642 unsigned NumElements = VT.getVectorNumElements();
6643
6644 if (VSN->isSplat()) {
6645 SDValue Op0 = Op.getOperand(0);
6646 unsigned Index = VSN->getSplatIndex();
6647 assert(Index < VT.getVectorNumElements() &&
6648 "Splat index should be defined and in first operand");
6649 // See whether the value we're splatting is directly available as a scalar.
6650 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
6652 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
6653 // Otherwise keep it as a vector-to-vector operation.
6654 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
6655 DAG.getTargetConstant(Index, DL, MVT::i32));
6656 }
6657
6658 GeneralShuffle GS(VT);
6659 for (unsigned I = 0; I < NumElements; ++I) {
6660 int Elt = VSN->getMaskElt(I);
6661 if (Elt < 0)
6662 GS.addUndef();
6663 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
6664 unsigned(Elt) % NumElements))
6665 return SDValue();
6666 }
6667 return GS.getNode(DAG, SDLoc(VSN));
6668}
6669
6670SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
6671 SelectionDAG &DAG) const {
6672 SDLoc DL(Op);
6673 // Just insert the scalar into element 0 of an undefined vector.
6674 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
6675 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
6676 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
6677}
6678
6679// Shift the lower 2 bytes of Op to the left in order to insert into the
6680// upper 2 bytes of the FP register.
6682 assert(Op.getSimpleValueType() == MVT::i64 &&
6683 "Expexted to convert i64 to f16.");
6684 SDLoc DL(Op);
6685 SDValue Shft = DAG.getNode(ISD::SHL, DL, MVT::i64, Op,
6686 DAG.getConstant(48, DL, MVT::i64));
6687 SDValue BCast = DAG.getNode(ISD::BITCAST, DL, MVT::f64, Shft);
6688 SDValue F16Val =
6689 DAG.getTargetExtractSubreg(SystemZ::subreg_h16, DL, MVT::f16, BCast);
6690 return F16Val;
6691}
6692
6693// Extract Op into GPR and shift the 2 f16 bytes to the right.
6695 assert(Op.getSimpleValueType() == MVT::f16 &&
6696 "Expected to convert f16 to i64.");
6697 SDNode *U32 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
6698 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h16, DL, MVT::f64,
6699 SDValue(U32, 0), Op);
6700 SDValue BCast = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
6701 SDValue Shft = DAG.getNode(ISD::SRL, DL, MVT::i64, BCast,
6702 DAG.getConstant(48, DL, MVT::i32));
6703 return Shft;
6704}
6705
6706SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
6707 SelectionDAG &DAG) const {
6708 // Handle insertions of floating-point values.
6709 SDLoc DL(Op);
6710 SDValue Op0 = Op.getOperand(0);
6711 SDValue Op1 = Op.getOperand(1);
6712 SDValue Op2 = Op.getOperand(2);
6713 EVT VT = Op.getValueType();
6714
6715 // Insertions into constant indices of a v2f64 can be done using VPDI.
6716 // However, if the inserted value is a bitcast or a constant then it's
6717 // better to use GPRs, as below.
6718 if (VT == MVT::v2f64 &&
6719 Op1.getOpcode() != ISD::BITCAST &&
6720 Op1.getOpcode() != ISD::ConstantFP &&
6721 Op2.getOpcode() == ISD::Constant) {
6722 uint64_t Index = Op2->getAsZExtVal();
6723 unsigned Mask = VT.getVectorNumElements() - 1;
6724 if (Index <= Mask)
6725 return Op;
6726 }
6727
6728 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
6729 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
6730 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
6731 SDValue IntOp1 =
6732 VT == MVT::v8f16
6733 ? DAG.getZExtOrTrunc(convertFromF16(Op1, DL, DAG), DL, MVT::i32)
6734 : DAG.getNode(ISD::BITCAST, DL, IntVT, Op1);
6735 SDValue Res =
6736 DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
6737 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), IntOp1, Op2);
6738 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
6739}
6740
6741SDValue
6742SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
6743 SelectionDAG &DAG) const {
6744 // Handle extractions of floating-point values.
6745 SDLoc DL(Op);
6746 SDValue Op0 = Op.getOperand(0);
6747 SDValue Op1 = Op.getOperand(1);
6748 EVT VT = Op.getValueType();
6749 EVT VecVT = Op0.getValueType();
6750
6751 // Extractions of constant indices can be done directly.
6752 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
6753 uint64_t Index = CIndexN->getZExtValue();
6754 unsigned Mask = VecVT.getVectorNumElements() - 1;
6755 if (Index <= Mask)
6756 return Op;
6757 }
6758
6759 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
6760 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
6761 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
6762 MVT ExtrVT = IntVT == MVT::i16 ? MVT::i32 : IntVT;
6763 SDValue Extr = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ExtrVT,
6764 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
6765 if (VT == MVT::f16)
6766 return convertToF16(DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Extr), DAG);
6767 return DAG.getNode(ISD::BITCAST, DL, VT, Extr);
6768}
6769
6770SDValue SystemZTargetLowering::
6771lowerSIGN_EXTEND_VECTOR_INREG(SDValue Op, SelectionDAG &DAG) const {
6772 SDValue PackedOp = Op.getOperand(0);
6773 EVT OutVT = Op.getValueType();
6774 EVT InVT = PackedOp.getValueType();
6775 unsigned ToBits = OutVT.getScalarSizeInBits();
6776 unsigned FromBits = InVT.getScalarSizeInBits();
6777 unsigned StartOffset = 0;
6778
6779 // If the input is a VECTOR_SHUFFLE, there are a number of important
6780 // cases where we can directly implement the sign-extension of the
6781 // original input lanes of the shuffle.
6782 if (PackedOp.getOpcode() == ISD::VECTOR_SHUFFLE) {
6783 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(PackedOp.getNode());
6784 ArrayRef<int> ShuffleMask = SVN->getMask();
6785 int OutNumElts = OutVT.getVectorNumElements();
6786
6787 // Recognize the special case where the sign-extension can be done
6788 // by the VSEG instruction. Handled via the default expander.
6789 if (ToBits == 64 && OutNumElts == 2) {
6790 int NumElem = ToBits / FromBits;
6791 if (ShuffleMask[0] == NumElem - 1 && ShuffleMask[1] == 2 * NumElem - 1)
6792 return SDValue();
6793 }
6794
6795 // Recognize the special case where we can fold the shuffle by
6796 // replacing some of the UNPACK_HIGH with UNPACK_LOW.
6797 int StartOffsetCandidate = -1;
6798 for (int Elt = 0; Elt < OutNumElts; Elt++) {
6799 if (ShuffleMask[Elt] == -1)
6800 continue;
6801 if (ShuffleMask[Elt] % OutNumElts == Elt) {
6802 if (StartOffsetCandidate == -1)
6803 StartOffsetCandidate = ShuffleMask[Elt] - Elt;
6804 if (StartOffsetCandidate == ShuffleMask[Elt] - Elt)
6805 continue;
6806 }
6807 StartOffsetCandidate = -1;
6808 break;
6809 }
6810 if (StartOffsetCandidate != -1) {
6811 StartOffset = StartOffsetCandidate;
6812 PackedOp = PackedOp.getOperand(0);
6813 }
6814 }
6815
6816 do {
6817 FromBits *= 2;
6818 unsigned OutNumElts = SystemZ::VectorBits / FromBits;
6819 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits), OutNumElts);
6820 unsigned Opcode = SystemZISD::UNPACK_HIGH;
6821 if (StartOffset >= OutNumElts) {
6822 Opcode = SystemZISD::UNPACK_LOW;
6823 StartOffset -= OutNumElts;
6824 }
6825 PackedOp = DAG.getNode(Opcode, SDLoc(PackedOp), OutVT, PackedOp);
6826 } while (FromBits != ToBits);
6827 return PackedOp;
6828}
6829
6830// Lower a ZERO_EXTEND_VECTOR_INREG to a vector shuffle with a zero vector.
6831SDValue SystemZTargetLowering::
6832lowerZERO_EXTEND_VECTOR_INREG(SDValue Op, SelectionDAG &DAG) const {
6833 SDValue PackedOp = Op.getOperand(0);
6834 SDLoc DL(Op);
6835 EVT OutVT = Op.getValueType();
6836 EVT InVT = PackedOp.getValueType();
6837 unsigned InNumElts = InVT.getVectorNumElements();
6838 unsigned OutNumElts = OutVT.getVectorNumElements();
6839 unsigned NumInPerOut = InNumElts / OutNumElts;
6840
6841 SDValue ZeroVec =
6842 DAG.getSplatVector(InVT, DL, DAG.getConstant(0, DL, InVT.getScalarType()));
6843
6844 SmallVector<int, 16> Mask(InNumElts);
6845 unsigned ZeroVecElt = InNumElts;
6846 for (unsigned PackedElt = 0; PackedElt < OutNumElts; PackedElt++) {
6847 unsigned MaskElt = PackedElt * NumInPerOut;
6848 unsigned End = MaskElt + NumInPerOut - 1;
6849 for (; MaskElt < End; MaskElt++)
6850 Mask[MaskElt] = ZeroVecElt++;
6851 Mask[MaskElt] = PackedElt;
6852 }
6853 SDValue Shuf = DAG.getVectorShuffle(InVT, DL, PackedOp, ZeroVec, Mask);
6854 return DAG.getNode(ISD::BITCAST, DL, OutVT, Shuf);
6855}
6856
6857SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
6858 unsigned ByScalar) const {
6859 // Look for cases where a vector shift can use the *_BY_SCALAR form.
6860 SDValue Op0 = Op.getOperand(0);
6861 SDValue Op1 = Op.getOperand(1);
6862 SDLoc DL(Op);
6863 EVT VT = Op.getValueType();
6864 unsigned ElemBitSize = VT.getScalarSizeInBits();
6865
6866 // See whether the shift vector is a splat represented as BUILD_VECTOR.
6867 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
6868 APInt SplatBits, SplatUndef;
6869 unsigned SplatBitSize;
6870 bool HasAnyUndefs;
6871 // Check for constant splats. Use ElemBitSize as the minimum element
6872 // width and reject splats that need wider elements.
6873 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
6874 ElemBitSize, true) &&
6875 SplatBitSize == ElemBitSize) {
6876 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
6877 DL, MVT::i32);
6878 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
6879 }
6880 // Check for variable splats.
6881 BitVector UndefElements;
6882 SDValue Splat = BVN->getSplatValue(&UndefElements);
6883 if (Splat) {
6884 // Since i32 is the smallest legal type, we either need a no-op
6885 // or a truncation.
6886 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
6887 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
6888 }
6889 }
6890
6891 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
6892 // and the shift amount is directly available in a GPR.
6893 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
6894 if (VSN->isSplat()) {
6895 SDValue VSNOp0 = VSN->getOperand(0);
6896 unsigned Index = VSN->getSplatIndex();
6897 assert(Index < VT.getVectorNumElements() &&
6898 "Splat index should be defined and in first operand");
6899 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
6900 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
6901 // Since i32 is the smallest legal type, we either need a no-op
6902 // or a truncation.
6903 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
6904 VSNOp0.getOperand(Index));
6905 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
6906 }
6907 }
6908 }
6909
6910 // Otherwise just treat the current form as legal.
6911 return Op;
6912}
6913
6914SDValue SystemZTargetLowering::lowerFSHL(SDValue Op, SelectionDAG &DAG) const {
6915 SDLoc DL(Op);
6916
6917 // i128 FSHL with a constant amount that is a multiple of 8 can be
6918 // implemented via VECTOR_SHUFFLE. If we have the vector-enhancements-2
6919 // facility, FSHL with a constant amount less than 8 can be implemented
6920 // via SHL_DOUBLE_BIT, and FSHL with other constant amounts by a
6921 // combination of the two.
6922 if (auto *ShiftAmtNode = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
6923 uint64_t ShiftAmt = ShiftAmtNode->getZExtValue() & 127;
6924 if ((ShiftAmt & 7) == 0 || Subtarget.hasVectorEnhancements2()) {
6925 SDValue Op0 = DAG.getBitcast(MVT::v16i8, Op.getOperand(0));
6926 SDValue Op1 = DAG.getBitcast(MVT::v16i8, Op.getOperand(1));
6927 if (ShiftAmt > 120) {
6928 // For N in 121..128, fshl N == fshr (128 - N), and for 1 <= N < 8
6929 // SHR_DOUBLE_BIT emits fewer instructions.
6930 SDValue Val =
6931 DAG.getNode(SystemZISD::SHR_DOUBLE_BIT, DL, MVT::v16i8, Op0, Op1,
6932 DAG.getTargetConstant(128 - ShiftAmt, DL, MVT::i32));
6933 return DAG.getBitcast(MVT::i128, Val);
6934 }
6935 SmallVector<int, 16> Mask(16);
6936 for (unsigned Elt = 0; Elt < 16; Elt++)
6937 Mask[Elt] = (ShiftAmt >> 3) + Elt;
6938 SDValue Shuf1 = DAG.getVectorShuffle(MVT::v16i8, DL, Op0, Op1, Mask);
6939 if ((ShiftAmt & 7) == 0)
6940 return DAG.getBitcast(MVT::i128, Shuf1);
6941 SDValue Shuf2 = DAG.getVectorShuffle(MVT::v16i8, DL, Op1, Op1, Mask);
6942 SDValue Val =
6943 DAG.getNode(SystemZISD::SHL_DOUBLE_BIT, DL, MVT::v16i8, Shuf1, Shuf2,
6944 DAG.getTargetConstant(ShiftAmt & 7, DL, MVT::i32));
6945 return DAG.getBitcast(MVT::i128, Val);
6946 }
6947 }
6948
6949 return SDValue();
6950}
6951
6952SDValue SystemZTargetLowering::lowerFSHR(SDValue Op, SelectionDAG &DAG) const {
6953 SDLoc DL(Op);
6954
6955 // i128 FSHR with a constant amount that is a multiple of 8 can be
6956 // implemented via VECTOR_SHUFFLE. If we have the vector-enhancements-2
6957 // facility, FSHR with a constant amount less than 8 can be implemented
6958 // via SHR_DOUBLE_BIT, and FSHR with other constant amounts by a
6959 // combination of the two.
6960 if (auto *ShiftAmtNode = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
6961 uint64_t ShiftAmt = ShiftAmtNode->getZExtValue() & 127;
6962 if ((ShiftAmt & 7) == 0 || Subtarget.hasVectorEnhancements2()) {
6963 SDValue Op0 = DAG.getBitcast(MVT::v16i8, Op.getOperand(0));
6964 SDValue Op1 = DAG.getBitcast(MVT::v16i8, Op.getOperand(1));
6965 if (ShiftAmt > 120) {
6966 // For N in 121..128, fshr N == fshl (128 - N), and for 1 <= N < 8
6967 // SHL_DOUBLE_BIT emits fewer instructions.
6968 SDValue Val =
6969 DAG.getNode(SystemZISD::SHL_DOUBLE_BIT, DL, MVT::v16i8, Op0, Op1,
6970 DAG.getTargetConstant(128 - ShiftAmt, DL, MVT::i32));
6971 return DAG.getBitcast(MVT::i128, Val);
6972 }
6973 SmallVector<int, 16> Mask(16);
6974 for (unsigned Elt = 0; Elt < 16; Elt++)
6975 Mask[Elt] = 16 - (ShiftAmt >> 3) + Elt;
6976 SDValue Shuf1 = DAG.getVectorShuffle(MVT::v16i8, DL, Op0, Op1, Mask);
6977 if ((ShiftAmt & 7) == 0)
6978 return DAG.getBitcast(MVT::i128, Shuf1);
6979 SDValue Shuf2 = DAG.getVectorShuffle(MVT::v16i8, DL, Op0, Op0, Mask);
6980 SDValue Val =
6981 DAG.getNode(SystemZISD::SHR_DOUBLE_BIT, DL, MVT::v16i8, Shuf2, Shuf1,
6982 DAG.getTargetConstant(ShiftAmt & 7, DL, MVT::i32));
6983 return DAG.getBitcast(MVT::i128, Val);
6984 }
6985 }
6986
6987 return SDValue();
6988}
6989
6991 SDLoc DL(Op);
6992 SDValue Src = Op.getOperand(0);
6993 MVT DstVT = Op.getSimpleValueType();
6994
6996 unsigned SrcAS = N->getSrcAddressSpace();
6997
6998 assert(SrcAS != N->getDestAddressSpace() &&
6999 "addrspacecast must be between different address spaces");
7000
7001 // addrspacecast [0 <- 1] : Assinging a ptr32 value to a 64-bit pointer.
7002 // addrspacecast [1 <- 0] : Assigining a 64-bit pointer to a ptr32 value.
7003 if (SrcAS == SYSTEMZAS::PTR32 && DstVT == MVT::i64) {
7004 Op = DAG.getNode(ISD::AND, DL, MVT::i32, Src,
7005 DAG.getConstant(0x7fffffff, DL, MVT::i32));
7006 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, DstVT, Op);
7007 } else if (DstVT == MVT::i32) {
7008 Op = DAG.getNode(ISD::TRUNCATE, DL, DstVT, Src);
7009 Op = DAG.getNode(ISD::AND, DL, MVT::i32, Op,
7010 DAG.getConstant(0x7fffffff, DL, MVT::i32));
7011 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, DstVT, Op);
7012 } else {
7013 report_fatal_error("Bad address space in addrspacecast");
7014 }
7015 return Op;
7016}
7017
7018SDValue SystemZTargetLowering::lowerFP_EXTEND(SDValue Op,
7019 SelectionDAG &DAG) const {
7020 SDValue In = Op.getOperand(Op->isStrictFPOpcode() ? 1 : 0);
7021 if (In.getSimpleValueType() != MVT::f16)
7022 return Op; // Legal
7023 return SDValue(); // Let legalizer emit the libcall.
7024}
7025
7027 MVT VT, SDValue Arg, SDLoc DL,
7028 SDValue Chain, bool IsStrict) const {
7029 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected request for libcall!");
7030 MakeLibCallOptions CallOptions;
7031 SDValue Result;
7032 std::tie(Result, Chain) =
7033 makeLibCall(DAG, LC, VT, Arg, CallOptions, DL, Chain);
7034 return IsStrict ? DAG.getMergeValues({Result, Chain}, DL) : Result;
7035}
7036
7037SDValue SystemZTargetLowering::lower_FP_TO_INT(SDValue Op,
7038 SelectionDAG &DAG) const {
7039 bool IsSigned = (Op->getOpcode() == ISD::FP_TO_SINT ||
7040 Op->getOpcode() == ISD::STRICT_FP_TO_SINT);
7041 bool IsStrict = Op->isStrictFPOpcode();
7042 SDLoc DL(Op);
7043 MVT VT = Op.getSimpleValueType();
7044 SDValue InOp = Op.getOperand(IsStrict ? 1 : 0);
7045 SDValue Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode();
7046 EVT InVT = InOp.getValueType();
7047
7048 // FP to unsigned is not directly supported on z10. Promoting an i32
7049 // result to (signed) i64 doesn't generate an inexact condition (fp
7050 // exception) for values that are outside the i32 range but in the i64
7051 // range, so use the default expansion.
7052 if (!Subtarget.hasFPExtension() && !IsSigned)
7053 // Expand i32/i64. F16 values will be recognized to fit and extended.
7054 return SDValue();
7055
7056 // Conversion from f16 is done via f32.
7057 if (InOp.getSimpleValueType() == MVT::f16) {
7059 LowerOperationWrapper(Op.getNode(), Results, DAG);
7060 return DAG.getMergeValues(Results, DL);
7061 }
7062
7063 if (VT == MVT::i128) {
7064 RTLIB::Libcall LC =
7065 IsSigned ? RTLIB::getFPTOSINT(InVT, VT) : RTLIB::getFPTOUINT(InVT, VT);
7066 return useLibCall(DAG, LC, VT, InOp, DL, Chain, IsStrict);
7067 }
7068
7069 return Op; // Legal
7070}
7071
7072SDValue SystemZTargetLowering::lower_INT_TO_FP(SDValue Op,
7073 SelectionDAG &DAG) const {
7074 bool IsSigned = (Op->getOpcode() == ISD::SINT_TO_FP ||
7075 Op->getOpcode() == ISD::STRICT_SINT_TO_FP);
7076 bool IsStrict = Op->isStrictFPOpcode();
7077 SDLoc DL(Op);
7078 MVT VT = Op.getSimpleValueType();
7079 SDValue InOp = Op.getOperand(IsStrict ? 1 : 0);
7080 SDValue Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode();
7081 EVT InVT = InOp.getValueType();
7082
7083 // Conversion to f16 is done via f32.
7084 if (VT == MVT::f16) {
7086 LowerOperationWrapper(Op.getNode(), Results, DAG);
7087 return DAG.getMergeValues(Results, DL);
7088 }
7089
7090 // Unsigned to fp is not directly supported on z10.
7091 if (!Subtarget.hasFPExtension() && !IsSigned)
7092 return SDValue(); // Expand i64.
7093
7094 if (InVT == MVT::i128) {
7095 RTLIB::Libcall LC =
7096 IsSigned ? RTLIB::getSINTTOFP(InVT, VT) : RTLIB::getUINTTOFP(InVT, VT);
7097 return useLibCall(DAG, LC, VT, InOp, DL, Chain, IsStrict);
7098 }
7099
7100 return Op; // Legal
7101}
7102
7103// Lower an f16 LOAD in case of no vector support.
7104SDValue SystemZTargetLowering::lowerLoadF16(SDValue Op,
7105 SelectionDAG &DAG) const {
7106 EVT RegVT = Op.getValueType();
7107 assert(RegVT == MVT::f16 && "Expected to lower an f16 load.");
7108 (void)RegVT;
7109
7110 // Load as integer.
7111 SDLoc DL(Op);
7112 SDValue NewLd;
7113 if (auto *AtomicLd = dyn_cast<AtomicSDNode>(Op.getNode())) {
7114 assert(EVT(RegVT) == AtomicLd->getMemoryVT() && "Unhandled f16 load");
7115 NewLd = DAG.getAtomicLoad(ISD::EXTLOAD, DL, MVT::i16, MVT::i64,
7116 AtomicLd->getChain(), AtomicLd->getBasePtr(),
7117 AtomicLd->getMemOperand());
7118 } else {
7119 LoadSDNode *Ld = cast<LoadSDNode>(Op.getNode());
7120 assert(EVT(RegVT) == Ld->getMemoryVT() && "Unhandled f16 load");
7121 NewLd = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i64, Ld->getChain(),
7122 Ld->getBasePtr(), Ld->getPointerInfo(), MVT::i16,
7123 Ld->getBaseAlign(), Ld->getMemOperand()->getFlags());
7124 }
7125 SDValue F16Val = convertToF16(NewLd, DAG);
7126 return DAG.getMergeValues({F16Val, NewLd.getValue(1)}, DL);
7127}
7128
7129// Lower an f16 STORE in case of no vector support.
7130SDValue SystemZTargetLowering::lowerStoreF16(SDValue Op,
7131 SelectionDAG &DAG) const {
7132 SDLoc DL(Op);
7133 SDValue Shft = convertFromF16(Op->getOperand(1), DL, DAG);
7134
7135 if (auto *AtomicSt = dyn_cast<AtomicSDNode>(Op.getNode()))
7136 return DAG.getAtomic(ISD::ATOMIC_STORE, DL, MVT::i16, AtomicSt->getChain(),
7137 Shft, AtomicSt->getBasePtr(),
7138 AtomicSt->getMemOperand());
7139
7140 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
7141 return DAG.getTruncStore(St->getChain(), DL, Shft, St->getBasePtr(), MVT::i16,
7142 St->getMemOperand());
7143}
7144
7145SDValue SystemZTargetLowering::lowerIS_FPCLASS(SDValue Op,
7146 SelectionDAG &DAG) const {
7147 SDLoc DL(Op);
7148 MVT ResultVT = Op.getSimpleValueType();
7149 SDValue Arg = Op.getOperand(0);
7150 unsigned Check = Op.getConstantOperandVal(1);
7151
7152 unsigned TDCMask = 0;
7153 if (Check & fcSNan)
7155 if (Check & fcQNan)
7157 if (Check & fcPosInf)
7159 if (Check & fcNegInf)
7161 if (Check & fcPosNormal)
7163 if (Check & fcNegNormal)
7165 if (Check & fcPosSubnormal)
7167 if (Check & fcNegSubnormal)
7169 if (Check & fcPosZero)
7170 TDCMask |= SystemZ::TDCMASK_ZERO_PLUS;
7171 if (Check & fcNegZero)
7172 TDCMask |= SystemZ::TDCMASK_ZERO_MINUS;
7173 SDValue TDCMaskV = DAG.getConstant(TDCMask, DL, MVT::i64);
7174
7175 SDValue Intr = DAG.getNode(SystemZISD::TDC, DL, ResultVT, Arg, TDCMaskV);
7176 return getCCResult(DAG, Intr);
7177}
7178
7179SDValue SystemZTargetLowering::lowerREADCYCLECOUNTER(SDValue Op,
7180 SelectionDAG &DAG) const {
7181 SDLoc DL(Op);
7182 SDValue Chain = Op.getOperand(0);
7183
7184 // STCKF only supports a memory operand, so we have to use a temporary.
7185 SDValue StackPtr = DAG.CreateStackTemporary(MVT::i64);
7186 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
7187 MachinePointerInfo MPI =
7189
7190 // Use STCFK to store the TOD clock into the temporary.
7191 SDValue StoreOps[] = {Chain, StackPtr};
7192 Chain = DAG.getMemIntrinsicNode(
7193 SystemZISD::STCKF, DL, DAG.getVTList(MVT::Other), StoreOps, MVT::i64,
7194 MPI, MaybeAlign(), MachineMemOperand::MOStore);
7195
7196 // And read it back from there.
7197 return DAG.getLoad(MVT::i64, DL, Chain, StackPtr, MPI);
7198}
7199
7201 SelectionDAG &DAG) const {
7202 switch (Op.getOpcode()) {
7203 case ISD::FRAMEADDR:
7204 return lowerFRAMEADDR(Op, DAG);
7205 case ISD::RETURNADDR:
7206 return lowerRETURNADDR(Op, DAG);
7207 case ISD::BR_CC:
7208 return lowerBR_CC(Op, DAG);
7209 case ISD::SELECT_CC:
7210 return lowerSELECT_CC(Op, DAG);
7211 case ISD::SETCC:
7212 return lowerSETCC(Op, DAG);
7213 case ISD::STRICT_FSETCC:
7214 return lowerSTRICT_FSETCC(Op, DAG, false);
7216 return lowerSTRICT_FSETCC(Op, DAG, true);
7217 case ISD::GlobalAddress:
7218 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
7220 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
7221 case ISD::BlockAddress:
7222 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
7223 case ISD::JumpTable:
7224 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
7225 case ISD::ConstantPool:
7226 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
7227 case ISD::BITCAST:
7228 return lowerBITCAST(Op, DAG);
7229 case ISD::VASTART:
7230 return lowerVASTART(Op, DAG);
7231 case ISD::VACOPY:
7232 return lowerVACOPY(Op, DAG);
7234 return lowerDYNAMIC_STACKALLOC(Op, DAG);
7236 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
7237 case ISD::MULHS:
7238 return lowerMULH(Op, DAG, SystemZISD::SMUL_LOHI);
7239 case ISD::MULHU:
7240 return lowerMULH(Op, DAG, SystemZISD::UMUL_LOHI);
7241 case ISD::SMUL_LOHI:
7242 return lowerSMUL_LOHI(Op, DAG);
7243 case ISD::UMUL_LOHI:
7244 return lowerUMUL_LOHI(Op, DAG);
7245 case ISD::SDIVREM:
7246 return lowerSDIVREM(Op, DAG);
7247 case ISD::UDIVREM:
7248 return lowerUDIVREM(Op, DAG);
7249 case ISD::SADDO:
7250 case ISD::SSUBO:
7251 case ISD::UADDO:
7252 case ISD::USUBO:
7253 return lowerXALUO(Op, DAG);
7254 case ISD::UADDO_CARRY:
7255 case ISD::USUBO_CARRY:
7256 return lowerUADDSUBO_CARRY(Op, DAG);
7257 case ISD::OR:
7258 return lowerOR(Op, DAG);
7259 case ISD::CTPOP:
7260 return lowerCTPOP(Op, DAG);
7261 case ISD::VECREDUCE_ADD:
7262 return lowerVECREDUCE_ADD(Op, DAG);
7263 case ISD::ATOMIC_FENCE:
7264 return lowerATOMIC_FENCE(Op, DAG);
7265 case ISD::ATOMIC_SWAP:
7266 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
7267 case ISD::ATOMIC_STORE:
7268 return lowerATOMIC_STORE(Op, DAG);
7269 case ISD::ATOMIC_LOAD:
7270 return lowerATOMIC_LOAD(Op, DAG);
7272 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
7274 return lowerATOMIC_LOAD_SUB(Op, DAG);
7276 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
7278 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
7280 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
7282 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
7284 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
7286 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
7288 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
7290 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
7292 return lowerATOMIC_CMP_SWAP(Op, DAG);
7293 case ISD::STACKSAVE:
7294 return lowerSTACKSAVE(Op, DAG);
7295 case ISD::STACKRESTORE:
7296 return lowerSTACKRESTORE(Op, DAG);
7297 case ISD::PREFETCH:
7298 return lowerPREFETCH(Op, DAG);
7300 return lowerINTRINSIC_W_CHAIN(Op, DAG);
7302 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
7303 case ISD::BUILD_VECTOR:
7304 return lowerBUILD_VECTOR(Op, DAG);
7306 return lowerVECTOR_SHUFFLE(Op, DAG);
7308 return lowerSCALAR_TO_VECTOR(Op, DAG);
7310 return lowerINSERT_VECTOR_ELT(Op, DAG);
7312 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
7314 return lowerSIGN_EXTEND_VECTOR_INREG(Op, DAG);
7316 return lowerZERO_EXTEND_VECTOR_INREG(Op, DAG);
7317 case ISD::SHL:
7318 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
7319 case ISD::SRL:
7320 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
7321 case ISD::SRA:
7322 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
7323 case ISD::ADDRSPACECAST:
7324 return lowerAddrSpaceCast(Op, DAG);
7325 case ISD::ROTL:
7326 return lowerShift(Op, DAG, SystemZISD::VROTL_BY_SCALAR);
7327 case ISD::FSHL:
7328 return lowerFSHL(Op, DAG);
7329 case ISD::FSHR:
7330 return lowerFSHR(Op, DAG);
7331 case ISD::FP_EXTEND:
7333 return lowerFP_EXTEND(Op, DAG);
7334 case ISD::FP_TO_UINT:
7335 case ISD::FP_TO_SINT:
7338 return lower_FP_TO_INT(Op, DAG);
7339 case ISD::UINT_TO_FP:
7340 case ISD::SINT_TO_FP:
7343 return lower_INT_TO_FP(Op, DAG);
7344 case ISD::LOAD:
7345 return lowerLoadF16(Op, DAG);
7346 case ISD::STORE:
7347 return lowerStoreF16(Op, DAG);
7348 case ISD::IS_FPCLASS:
7349 return lowerIS_FPCLASS(Op, DAG);
7350 case ISD::GET_ROUNDING:
7351 return lowerGET_ROUNDING(Op, DAG);
7353 return lowerREADCYCLECOUNTER(Op, DAG);
7356 // These operations are legal on our platform, but we cannot actually
7357 // set the operation action to Legal as common code would treat this
7358 // as equivalent to Expand. Instead, we keep the operation action to
7359 // Custom and just leave them unchanged here.
7360 return Op;
7361
7362 default:
7363 llvm_unreachable("Unexpected node to lower");
7364 }
7365}
7366
7368 const SDLoc &SL) {
7369 // If i128 is legal, just use a normal bitcast.
7370 if (DAG.getTargetLoweringInfo().isTypeLegal(MVT::i128))
7371 return DAG.getBitcast(MVT::f128, Src);
7372
7373 // Otherwise, f128 must live in FP128, so do a partwise move.
7375 &SystemZ::FP128BitRegClass);
7376
7377 SDValue Hi, Lo;
7378 std::tie(Lo, Hi) = DAG.SplitScalar(Src, SL, MVT::i64, MVT::i64);
7379
7380 Hi = DAG.getBitcast(MVT::f64, Hi);
7381 Lo = DAG.getBitcast(MVT::f64, Lo);
7382
7383 SDNode *Pair = DAG.getMachineNode(
7384 SystemZ::REG_SEQUENCE, SL, MVT::f128,
7385 {DAG.getTargetConstant(SystemZ::FP128BitRegClassID, SL, MVT::i32), Lo,
7386 DAG.getTargetConstant(SystemZ::subreg_l64, SL, MVT::i32), Hi,
7387 DAG.getTargetConstant(SystemZ::subreg_h64, SL, MVT::i32)});
7388 return SDValue(Pair, 0);
7389}
7390
7392 const SDLoc &SL) {
7393 // If i128 is legal, just use a normal bitcast.
7394 if (DAG.getTargetLoweringInfo().isTypeLegal(MVT::i128))
7395 return DAG.getBitcast(MVT::i128, Src);
7396
7397 // Otherwise, f128 must live in FP128, so do a partwise move.
7399 &SystemZ::FP128BitRegClass);
7400
7401 SDValue LoFP =
7402 DAG.getTargetExtractSubreg(SystemZ::subreg_l64, SL, MVT::f64, Src);
7403 SDValue HiFP =
7404 DAG.getTargetExtractSubreg(SystemZ::subreg_h64, SL, MVT::f64, Src);
7405 SDValue Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i64, LoFP);
7406 SDValue Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i64, HiFP);
7407
7408 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i128, Lo, Hi);
7409}
7410
7411// Lower operations with invalid operand or result types.
7412void
7415 SelectionDAG &DAG) const {
7416 switch (N->getOpcode()) {
7417 case ISD::ATOMIC_LOAD: {
7418 SDLoc DL(N);
7419 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
7420 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
7421 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
7422 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
7423 DL, Tys, Ops, MVT::i128, MMO);
7424
7425 SDValue Lowered = lowerGR128ToI128(DAG, Res);
7426 if (N->getValueType(0) == MVT::f128)
7427 Lowered = expandBitCastI128ToF128(DAG, Lowered, DL);
7428 Results.push_back(Lowered);
7429 Results.push_back(Res.getValue(1));
7430 break;
7431 }
7432 case ISD::ATOMIC_STORE: {
7433 SDLoc DL(N);
7434 SDVTList Tys = DAG.getVTList(MVT::Other);
7435 SDValue Val = N->getOperand(1);
7436 if (Val.getValueType() == MVT::f128)
7437 Val = expandBitCastF128ToI128(DAG, Val, DL);
7438 Val = lowerI128ToGR128(DAG, Val);
7439
7440 SDValue Ops[] = {N->getOperand(0), Val, N->getOperand(2)};
7441 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
7442 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
7443 DL, Tys, Ops, MVT::i128, MMO);
7444 // We have to enforce sequential consistency by performing a
7445 // serialization operation after the store.
7446 if (cast<AtomicSDNode>(N)->getSuccessOrdering() ==
7448 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
7449 MVT::Other, Res), 0);
7450 Results.push_back(Res);
7451 break;
7452 }
7454 SDLoc DL(N);
7455 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other);
7456 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
7457 lowerI128ToGR128(DAG, N->getOperand(2)),
7458 lowerI128ToGR128(DAG, N->getOperand(3)) };
7459 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
7460 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
7461 DL, Tys, Ops, MVT::i128, MMO);
7462 SDValue Success = emitSETCC(DAG, DL, Res.getValue(1),
7464 Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1));
7465 Results.push_back(lowerGR128ToI128(DAG, Res));
7466 Results.push_back(Success);
7467 Results.push_back(Res.getValue(2));
7468 break;
7469 }
7470 case ISD::BITCAST: {
7471 if (useSoftFloat())
7472 return;
7473 SDLoc DL(N);
7474 SDValue Src = N->getOperand(0);
7475 EVT SrcVT = Src.getValueType();
7476 EVT ResVT = N->getValueType(0);
7477 if (ResVT == MVT::i128 && SrcVT == MVT::f128)
7478 Results.push_back(expandBitCastF128ToI128(DAG, Src, DL));
7479 else if (SrcVT == MVT::i16 && ResVT == MVT::f16) {
7480 if (Subtarget.hasVector()) {
7481 SDValue In32 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Src);
7482 Results.push_back(SDValue(
7483 DAG.getMachineNode(SystemZ::LEFR_16, DL, MVT::f16, In32), 0));
7484 } else {
7485 SDValue In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Src);
7486 Results.push_back(convertToF16(In64, DAG));
7487 }
7488 } else if (SrcVT == MVT::f16 && ResVT == MVT::i16) {
7489 SDValue ExtractedVal =
7490 Subtarget.hasVector()
7491 ? SDValue(DAG.getMachineNode(SystemZ::LFER_16, DL, MVT::i32, Src),
7492 0)
7493 : convertFromF16(Src, DL, DAG);
7494 Results.push_back(DAG.getZExtOrTrunc(ExtractedVal, DL, ResVT));
7495 }
7496 break;
7497 }
7498 case ISD::UINT_TO_FP:
7499 case ISD::SINT_TO_FP:
7502 if (useSoftFloat())
7503 return;
7504 bool IsStrict = N->isStrictFPOpcode();
7505 SDLoc DL(N);
7506 SDValue InOp = N->getOperand(IsStrict ? 1 : 0);
7507 EVT ResVT = N->getValueType(0);
7508 SDValue Chain = IsStrict ? N->getOperand(0) : DAG.getEntryNode();
7509 if (ResVT == MVT::f16) {
7510 if (!IsStrict) {
7511 SDValue OpF32 = DAG.getNode(N->getOpcode(), DL, MVT::f32, InOp);
7512 Results.push_back(DAG.getFPExtendOrRound(OpF32, DL, MVT::f16));
7513 } else {
7514 SDValue OpF32 =
7515 DAG.getNode(N->getOpcode(), DL, DAG.getVTList(MVT::f32, MVT::Other),
7516 {Chain, InOp});
7517 SDValue F16Res;
7518 std::tie(F16Res, Chain) = DAG.getStrictFPExtendOrRound(
7519 OpF32, OpF32.getValue(1), DL, MVT::f16);
7520 Results.push_back(F16Res);
7521 Results.push_back(Chain);
7522 }
7523 }
7524 break;
7525 }
7526 case ISD::FP_TO_UINT:
7527 case ISD::FP_TO_SINT:
7530 if (useSoftFloat())
7531 return;
7532 bool IsStrict = N->isStrictFPOpcode();
7533 SDLoc DL(N);
7534 EVT ResVT = N->getValueType(0);
7535 SDValue InOp = N->getOperand(IsStrict ? 1 : 0);
7536 EVT InVT = InOp->getValueType(0);
7537 SDValue Chain = IsStrict ? N->getOperand(0) : DAG.getEntryNode();
7538 if (InVT == MVT::f16) {
7539 if (!IsStrict) {
7540 SDValue InF32 = DAG.getFPExtendOrRound(InOp, DL, MVT::f32);
7541 Results.push_back(DAG.getNode(N->getOpcode(), DL, ResVT, InF32));
7542 } else {
7543 SDValue InF32;
7544 std::tie(InF32, Chain) =
7545 DAG.getStrictFPExtendOrRound(InOp, Chain, DL, MVT::f32);
7546 SDValue OpF32 =
7547 DAG.getNode(N->getOpcode(), DL, DAG.getVTList(ResVT, MVT::Other),
7548 {Chain, InF32});
7549 Results.push_back(OpF32);
7550 Results.push_back(OpF32.getValue(1));
7551 }
7552 }
7553 break;
7554 }
7555 default:
7556 llvm_unreachable("Unexpected node to lower");
7557 }
7558}
7559
7560void
7566
7567// Return true if VT is a vector whose elements are a whole number of bytes
7568// in width. Also check for presence of vector support.
7569bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
7570 if (!Subtarget.hasVector())
7571 return false;
7572
7573 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
7574}
7575
7576// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
7577// producing a result of type ResVT. Op is a possibly bitcast version
7578// of the input vector and Index is the index (based on type VecVT) that
7579// should be extracted. Return the new extraction if a simplification
7580// was possible or if Force is true.
7581SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
7582 EVT VecVT, SDValue Op,
7583 unsigned Index,
7584 DAGCombinerInfo &DCI,
7585 bool Force) const {
7586 SelectionDAG &DAG = DCI.DAG;
7587
7588 // The number of bytes being extracted.
7589 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
7590
7591 for (;;) {
7592 unsigned Opcode = Op.getOpcode();
7593 if (Opcode == ISD::BITCAST)
7594 // Look through bitcasts.
7595 Op = Op.getOperand(0);
7596 else if ((Opcode == ISD::VECTOR_SHUFFLE || Opcode == SystemZISD::SPLAT) &&
7597 canTreatAsByteVector(Op.getValueType())) {
7598 // Get a VPERM-like permute mask and see whether the bytes covered
7599 // by the extracted element are a contiguous sequence from one
7600 // source operand.
7602 if (!getVPermMask(Op, Bytes))
7603 break;
7604 int First;
7605 if (!getShuffleInput(Bytes, Index * BytesPerElement,
7606 BytesPerElement, First))
7607 break;
7608 if (First < 0)
7609 return DAG.getUNDEF(ResVT);
7610 // Make sure the contiguous sequence starts at a multiple of the
7611 // original element size.
7612 unsigned Byte = unsigned(First) % Bytes.size();
7613 if (Byte % BytesPerElement != 0)
7614 break;
7615 // We can get the extracted value directly from an input.
7616 Index = Byte / BytesPerElement;
7617 Op = Op.getOperand(unsigned(First) / Bytes.size());
7618 Force = true;
7619 } else if (Opcode == ISD::BUILD_VECTOR &&
7620 canTreatAsByteVector(Op.getValueType())) {
7621 // We can only optimize this case if the BUILD_VECTOR elements are
7622 // at least as wide as the extracted value.
7623 EVT OpVT = Op.getValueType();
7624 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
7625 if (OpBytesPerElement < BytesPerElement)
7626 break;
7627 // Make sure that the least-significant bit of the extracted value
7628 // is the least significant bit of an input.
7629 unsigned End = (Index + 1) * BytesPerElement;
7630 if (End % OpBytesPerElement != 0)
7631 break;
7632 // We're extracting the low part of one operand of the BUILD_VECTOR.
7633 Op = Op.getOperand(End / OpBytesPerElement - 1);
7634 if (!Op.getValueType().isInteger()) {
7635 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
7636 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
7637 DCI.AddToWorklist(Op.getNode());
7638 }
7639 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
7640 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
7641 if (VT != ResVT) {
7642 DCI.AddToWorklist(Op.getNode());
7643 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
7644 }
7645 return Op;
7646 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
7648 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
7649 canTreatAsByteVector(Op.getValueType()) &&
7650 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
7651 // Make sure that only the unextended bits are significant.
7652 EVT ExtVT = Op.getValueType();
7653 EVT OpVT = Op.getOperand(0).getValueType();
7654 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
7655 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
7656 unsigned Byte = Index * BytesPerElement;
7657 unsigned SubByte = Byte % ExtBytesPerElement;
7658 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
7659 if (SubByte < MinSubByte ||
7660 SubByte + BytesPerElement > ExtBytesPerElement)
7661 break;
7662 // Get the byte offset of the unextended element
7663 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
7664 // ...then add the byte offset relative to that element.
7665 Byte += SubByte - MinSubByte;
7666 if (Byte % BytesPerElement != 0)
7667 break;
7668 Op = Op.getOperand(0);
7669 Index = Byte / BytesPerElement;
7670 Force = true;
7671 } else
7672 break;
7673 }
7674 if (Force) {
7675 if (Op.getValueType() != VecVT) {
7676 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
7677 DCI.AddToWorklist(Op.getNode());
7678 }
7679 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
7680 DAG.getConstant(Index, DL, MVT::i32));
7681 }
7682 return SDValue();
7683}
7684
7685// Optimize vector operations in scalar value Op on the basis that Op
7686// is truncated to TruncVT.
7687SDValue SystemZTargetLowering::combineTruncateExtract(
7688 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
7689 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
7690 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
7691 // of type TruncVT.
7692 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
7693 TruncVT.getSizeInBits() % 8 == 0) {
7694 SDValue Vec = Op.getOperand(0);
7695 EVT VecVT = Vec.getValueType();
7696 if (canTreatAsByteVector(VecVT)) {
7697 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
7698 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
7699 unsigned TruncBytes = TruncVT.getStoreSize();
7700 if (BytesPerElement % TruncBytes == 0) {
7701 // Calculate the value of Y' in the above description. We are
7702 // splitting the original elements into Scale equal-sized pieces
7703 // and for truncation purposes want the last (least-significant)
7704 // of these pieces for IndexN. This is easiest to do by calculating
7705 // the start index of the following element and then subtracting 1.
7706 unsigned Scale = BytesPerElement / TruncBytes;
7707 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
7708
7709 // Defer the creation of the bitcast from X to combineExtract,
7710 // which might be able to optimize the extraction.
7711 VecVT = EVT::getVectorVT(*DCI.DAG.getContext(),
7712 MVT::getIntegerVT(TruncBytes * 8),
7713 VecVT.getStoreSize() / TruncBytes);
7714 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
7715 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
7716 }
7717 }
7718 }
7719 }
7720 return SDValue();
7721}
7722
7723SDValue SystemZTargetLowering::combineZERO_EXTEND(
7724 SDNode *N, DAGCombinerInfo &DCI) const {
7725 // Convert (zext (select_ccmask C1, C2)) into (select_ccmask C1', C2')
7726 SelectionDAG &DAG = DCI.DAG;
7727 SDValue N0 = N->getOperand(0);
7728 EVT VT = N->getValueType(0);
7729 if (N0.getOpcode() == SystemZISD::SELECT_CCMASK) {
7730 auto *TrueOp = dyn_cast<ConstantSDNode>(N0.getOperand(0));
7731 auto *FalseOp = dyn_cast<ConstantSDNode>(N0.getOperand(1));
7732 if (TrueOp && FalseOp) {
7733 SDLoc DL(N0);
7734 SDValue Ops[] = { DAG.getConstant(TrueOp->getZExtValue(), DL, VT),
7735 DAG.getConstant(FalseOp->getZExtValue(), DL, VT),
7736 N0.getOperand(2), N0.getOperand(3), N0.getOperand(4) };
7737 SDValue NewSelect = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VT, Ops);
7738 // If N0 has multiple uses, change other uses as well.
7739 if (!N0.hasOneUse()) {
7740 SDValue TruncSelect =
7741 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), NewSelect);
7742 DCI.CombineTo(N0.getNode(), TruncSelect);
7743 }
7744 return NewSelect;
7745 }
7746 }
7747 // Convert (zext (xor (trunc X), C)) into (xor (trunc X), C') if the size
7748 // of the result is smaller than the size of X and all the truncated bits
7749 // of X are already zero.
7750 if (N0.getOpcode() == ISD::XOR &&
7751 N0.hasOneUse() && N0.getOperand(0).hasOneUse() &&
7752 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
7753 N0.getOperand(1).getOpcode() == ISD::Constant) {
7754 SDValue X = N0.getOperand(0).getOperand(0);
7755 if (VT.isScalarInteger() && VT.getSizeInBits() < X.getValueSizeInBits()) {
7756 KnownBits Known = DAG.computeKnownBits(X);
7757 APInt TruncatedBits = APInt::getBitsSet(X.getValueSizeInBits(),
7758 N0.getValueSizeInBits(),
7759 VT.getSizeInBits());
7760 if (TruncatedBits.isSubsetOf(Known.Zero)) {
7761 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
7762 APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
7763 return DAG.getNode(ISD::XOR, SDLoc(N0), VT,
7764 X, DAG.getConstant(Mask, SDLoc(N0), VT));
7765 }
7766 }
7767 }
7768 // Recognize patterns for VECTOR SUBTRACT COMPUTE BORROW INDICATION
7769 // and VECTOR ADD COMPUTE CARRY for i128:
7770 // (zext (setcc_uge X Y)) --> (VSCBI X Y)
7771 // (zext (setcc_ule Y X)) --> (VSCBI X Y)
7772 // (zext (setcc_ult (add X Y) X/Y) -> (VACC X Y)
7773 // (zext (setcc_ugt X/Y (add X Y)) -> (VACC X Y)
7774 // For vector types, these patterns are recognized in the .td file.
7775 if (N0.getOpcode() == ISD::SETCC && isTypeLegal(VT) && VT == MVT::i128 &&
7776 N0.getOperand(0).getValueType() == VT) {
7777 SDValue Op0 = N0.getOperand(0);
7778 SDValue Op1 = N0.getOperand(1);
7779 const ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
7780 switch (CC) {
7781 case ISD::SETULE:
7782 std::swap(Op0, Op1);
7783 [[fallthrough]];
7784 case ISD::SETUGE:
7785 return DAG.getNode(SystemZISD::VSCBI, SDLoc(N0), VT, Op0, Op1);
7786 case ISD::SETUGT:
7787 std::swap(Op0, Op1);
7788 [[fallthrough]];
7789 case ISD::SETULT:
7790 if (Op0->hasOneUse() && Op0->getOpcode() == ISD::ADD &&
7791 (Op0->getOperand(0) == Op1 || Op0->getOperand(1) == Op1))
7792 return DAG.getNode(SystemZISD::VACC, SDLoc(N0), VT, Op0->getOperand(0),
7793 Op0->getOperand(1));
7794 break;
7795 default:
7796 break;
7797 }
7798 }
7799
7800 return SDValue();
7801}
7802
7803SDValue SystemZTargetLowering::combineSIGN_EXTEND_INREG(
7804 SDNode *N, DAGCombinerInfo &DCI) const {
7805 // Convert (sext_in_reg (setcc LHS, RHS, COND), i1)
7806 // and (sext_in_reg (any_extend (setcc LHS, RHS, COND)), i1)
7807 // into (select_cc LHS, RHS, -1, 0, COND)
7808 SelectionDAG &DAG = DCI.DAG;
7809 SDValue N0 = N->getOperand(0);
7810 EVT VT = N->getValueType(0);
7811 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
7812 if (N0.hasOneUse() && N0.getOpcode() == ISD::ANY_EXTEND)
7813 N0 = N0.getOperand(0);
7814 if (EVT == MVT::i1 && N0.hasOneUse() && N0.getOpcode() == ISD::SETCC) {
7815 SDLoc DL(N0);
7816 SDValue Ops[] = { N0.getOperand(0), N0.getOperand(1),
7817 DAG.getAllOnesConstant(DL, VT),
7818 DAG.getConstant(0, DL, VT), N0.getOperand(2) };
7819 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops);
7820 }
7821 return SDValue();
7822}
7823
7824SDValue SystemZTargetLowering::combineSIGN_EXTEND(
7825 SDNode *N, DAGCombinerInfo &DCI) const {
7826 // Convert (sext (ashr (shl X, C1), C2)) to
7827 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
7828 // cheap as narrower ones.
7829 SelectionDAG &DAG = DCI.DAG;
7830 SDValue N0 = N->getOperand(0);
7831 EVT VT = N->getValueType(0);
7832 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
7833 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
7834 SDValue Inner = N0.getOperand(0);
7835 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
7836 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
7837 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
7838 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
7839 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
7840 EVT ShiftVT = N0.getOperand(1).getValueType();
7841 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
7842 Inner.getOperand(0));
7843 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
7844 DAG.getConstant(NewShlAmt, SDLoc(Inner),
7845 ShiftVT));
7846 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
7847 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
7848 }
7849 }
7850 }
7851
7852 return SDValue();
7853}
7854
7855SDValue SystemZTargetLowering::combineMERGE(
7856 SDNode *N, DAGCombinerInfo &DCI) const {
7857 SelectionDAG &DAG = DCI.DAG;
7858 unsigned Opcode = N->getOpcode();
7859 SDValue Op0 = N->getOperand(0);
7860 SDValue Op1 = N->getOperand(1);
7861 if (Op0.getOpcode() == ISD::BITCAST)
7862 Op0 = Op0.getOperand(0);
7864 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
7865 // for v4f32.
7866 if (Op1 == N->getOperand(0))
7867 return Op1;
7868 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
7869 EVT VT = Op1.getValueType();
7870 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
7871 if (ElemBytes <= 4) {
7872 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
7873 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
7874 EVT InVT = VT.changeVectorElementTypeToInteger();
7875 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
7876 SystemZ::VectorBytes / ElemBytes / 2);
7877 if (VT != InVT) {
7878 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
7879 DCI.AddToWorklist(Op1.getNode());
7880 }
7881 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
7882 DCI.AddToWorklist(Op.getNode());
7883 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
7884 }
7885 }
7886 return SDValue();
7887}
7888
7889static bool isI128MovedToParts(LoadSDNode *LD, SDNode *&LoPart,
7890 SDNode *&HiPart) {
7891 LoPart = HiPart = nullptr;
7892
7893 // Scan through all users.
7894 for (SDUse &Use : LD->uses()) {
7895 // Skip the uses of the chain.
7896 if (Use.getResNo() != 0)
7897 continue;
7898
7899 // Verify every user is a TRUNCATE to i64 of the low or high half.
7900 SDNode *User = Use.getUser();
7901 bool IsLoPart = true;
7902 if (User->getOpcode() == ISD::SRL &&
7903 User->getOperand(1).getOpcode() == ISD::Constant &&
7904 User->getConstantOperandVal(1) == 64 && User->hasOneUse()) {
7905 User = *User->user_begin();
7906 IsLoPart = false;
7907 }
7908 if (User->getOpcode() != ISD::TRUNCATE || User->getValueType(0) != MVT::i64)
7909 return false;
7910
7911 if (IsLoPart) {
7912 if (LoPart)
7913 return false;
7914 LoPart = User;
7915 } else {
7916 if (HiPart)
7917 return false;
7918 HiPart = User;
7919 }
7920 }
7921 return true;
7922}
7923
7924static bool isF128MovedToParts(LoadSDNode *LD, SDNode *&LoPart,
7925 SDNode *&HiPart) {
7926 LoPart = HiPart = nullptr;
7927
7928 // Scan through all users.
7929 for (SDUse &Use : LD->uses()) {
7930 // Skip the uses of the chain.
7931 if (Use.getResNo() != 0)
7932 continue;
7933
7934 // Verify every user is an EXTRACT_SUBREG of the low or high half.
7935 SDNode *User = Use.getUser();
7936 if (!User->hasOneUse() || !User->isMachineOpcode() ||
7937 User->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
7938 return false;
7939
7940 switch (User->getConstantOperandVal(1)) {
7941 case SystemZ::subreg_l64:
7942 if (LoPart)
7943 return false;
7944 LoPart = User;
7945 break;
7946 case SystemZ::subreg_h64:
7947 if (HiPart)
7948 return false;
7949 HiPart = User;
7950 break;
7951 default:
7952 return false;
7953 }
7954 }
7955 return true;
7956}
7957
7958SDValue SystemZTargetLowering::combineLOAD(
7959 SDNode *N, DAGCombinerInfo &DCI) const {
7960 SelectionDAG &DAG = DCI.DAG;
7961 EVT LdVT = N->getValueType(0);
7962 if (auto *LN = dyn_cast<LoadSDNode>(N)) {
7963 if (LN->getAddressSpace() == SYSTEMZAS::PTR32) {
7964 MVT PtrVT = getPointerTy(DAG.getDataLayout());
7965 MVT LoadNodeVT = LN->getBasePtr().getSimpleValueType();
7966 if (PtrVT != LoadNodeVT) {
7967 SDLoc DL(LN);
7968 SDValue AddrSpaceCast = DAG.getAddrSpaceCast(
7969 DL, PtrVT, LN->getBasePtr(), SYSTEMZAS::PTR32, 0);
7970 return DAG.getExtLoad(LN->getExtensionType(), DL, LN->getValueType(0),
7971 LN->getChain(), AddrSpaceCast, LN->getMemoryVT(),
7972 LN->getMemOperand());
7973 }
7974 }
7975 }
7976 SDLoc DL(N);
7977
7978 // Replace a 128-bit load that is used solely to move its value into GPRs
7979 // by separate loads of both halves.
7980 LoadSDNode *LD = cast<LoadSDNode>(N);
7981 if (LD->isSimple() && ISD::isNormalLoad(LD)) {
7982 SDNode *LoPart, *HiPart;
7983 if ((LdVT == MVT::i128 && isI128MovedToParts(LD, LoPart, HiPart)) ||
7984 (LdVT == MVT::f128 && isF128MovedToParts(LD, LoPart, HiPart))) {
7985 // Rewrite each extraction as an independent load.
7986 SmallVector<SDValue, 2> ArgChains;
7987 if (HiPart) {
7988 SDValue EltLoad = DAG.getLoad(
7989 HiPart->getValueType(0), DL, LD->getChain(), LD->getBasePtr(),
7990 LD->getPointerInfo(), LD->getBaseAlign(),
7991 LD->getMemOperand()->getFlags(), LD->getAAInfo());
7992
7993 DCI.CombineTo(HiPart, EltLoad, true);
7994 ArgChains.push_back(EltLoad.getValue(1));
7995 }
7996 if (LoPart) {
7997 SDValue EltLoad = DAG.getLoad(
7998 LoPart->getValueType(0), DL, LD->getChain(),
7999 DAG.getObjectPtrOffset(DL, LD->getBasePtr(), TypeSize::getFixed(8)),
8000 LD->getPointerInfo().getWithOffset(8), LD->getBaseAlign(),
8001 LD->getMemOperand()->getFlags(), LD->getAAInfo());
8002
8003 DCI.CombineTo(LoPart, EltLoad, true);
8004 ArgChains.push_back(EltLoad.getValue(1));
8005 }
8006
8007 // Collect all chains via TokenFactor.
8008 SDValue Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, ArgChains);
8009 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8010 DCI.AddToWorklist(Chain.getNode());
8011 return SDValue(N, 0);
8012 }
8013 }
8014
8015 if (LdVT.isVector() || LdVT.isInteger())
8016 return SDValue();
8017 // Transform a scalar load that is REPLICATEd as well as having other
8018 // use(s) to the form where the other use(s) use the first element of the
8019 // REPLICATE instead of the load. Otherwise instruction selection will not
8020 // produce a VLREP. Avoid extracting to a GPR, so only do this for floating
8021 // point loads.
8022
8023 SDValue Replicate;
8024 SmallVector<SDNode*, 8> OtherUses;
8025 for (SDUse &Use : N->uses()) {
8026 if (Use.getUser()->getOpcode() == SystemZISD::REPLICATE) {
8027 if (Replicate)
8028 return SDValue(); // Should never happen
8029 Replicate = SDValue(Use.getUser(), 0);
8030 } else if (Use.getResNo() == 0)
8031 OtherUses.push_back(Use.getUser());
8032 }
8033 if (!Replicate || OtherUses.empty())
8034 return SDValue();
8035
8036 SDValue Extract0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, LdVT,
8037 Replicate, DAG.getConstant(0, DL, MVT::i32));
8038 // Update uses of the loaded Value while preserving old chains.
8039 for (SDNode *U : OtherUses) {
8041 for (SDValue Op : U->ops())
8042 Ops.push_back((Op.getNode() == N && Op.getResNo() == 0) ? Extract0 : Op);
8043 DAG.UpdateNodeOperands(U, Ops);
8044 }
8045 return SDValue(N, 0);
8046}
8047
8048bool SystemZTargetLowering::canLoadStoreByteSwapped(EVT VT) const {
8049 if (VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64)
8050 return true;
8051 if (Subtarget.hasVectorEnhancements2())
8052 if (VT == MVT::v8i16 || VT == MVT::v4i32 || VT == MVT::v2i64 || VT == MVT::i128)
8053 return true;
8054 return false;
8055}
8056
8058 if (!VT.isVector() || !VT.isSimple() ||
8059 VT.getSizeInBits() != 128 ||
8060 VT.getScalarSizeInBits() % 8 != 0)
8061 return false;
8062
8063 unsigned NumElts = VT.getVectorNumElements();
8064 for (unsigned i = 0; i < NumElts; ++i) {
8065 if (M[i] < 0) continue; // ignore UNDEF indices
8066 if ((unsigned) M[i] != NumElts - 1 - i)
8067 return false;
8068 }
8069
8070 return true;
8071}
8072
8073static bool isOnlyUsedByStores(SDValue StoredVal, SelectionDAG &DAG) {
8074 for (auto *U : StoredVal->users()) {
8075 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(U)) {
8076 EVT CurrMemVT = ST->getMemoryVT().getScalarType();
8077 if (CurrMemVT.isRound() && CurrMemVT.getStoreSize() <= 16)
8078 continue;
8079 } else if (isa<BuildVectorSDNode>(U)) {
8080 SDValue BuildVector = SDValue(U, 0);
8081 if (DAG.isSplatValue(BuildVector, true/*AllowUndefs*/) &&
8082 isOnlyUsedByStores(BuildVector, DAG))
8083 continue;
8084 }
8085 return false;
8086 }
8087 return true;
8088}
8089
8090static bool isI128MovedFromParts(SDValue Val, SDValue &LoPart,
8091 SDValue &HiPart) {
8092 if (Val.getOpcode() != ISD::OR || !Val.getNode()->hasOneUse())
8093 return false;
8094
8095 SDValue Op0 = Val.getOperand(0);
8096 SDValue Op1 = Val.getOperand(1);
8097
8098 if (Op0.getOpcode() == ISD::SHL)
8099 std::swap(Op0, Op1);
8100 if (Op1.getOpcode() != ISD::SHL || !Op1.getNode()->hasOneUse() ||
8101 Op1.getOperand(1).getOpcode() != ISD::Constant ||
8102 Op1.getConstantOperandVal(1) != 64)
8103 return false;
8104 Op1 = Op1.getOperand(0);
8105
8106 if (Op0.getOpcode() != ISD::ZERO_EXTEND || !Op0.getNode()->hasOneUse() ||
8107 Op0.getOperand(0).getValueType() != MVT::i64)
8108 return false;
8109 if (Op1.getOpcode() != ISD::ANY_EXTEND || !Op1.getNode()->hasOneUse() ||
8110 Op1.getOperand(0).getValueType() != MVT::i64)
8111 return false;
8112
8113 LoPart = Op0.getOperand(0);
8114 HiPart = Op1.getOperand(0);
8115 return true;
8116}
8117
8118static bool isF128MovedFromParts(SDValue Val, SDValue &LoPart,
8119 SDValue &HiPart) {
8120 if (!Val.getNode()->hasOneUse() || !Val.isMachineOpcode() ||
8121 Val.getMachineOpcode() != TargetOpcode::REG_SEQUENCE)
8122 return false;
8123
8124 if (Val->getNumOperands() != 5 ||
8125 Val->getOperand(0)->getAsZExtVal() != SystemZ::FP128BitRegClassID ||
8126 Val->getOperand(2)->getAsZExtVal() != SystemZ::subreg_l64 ||
8127 Val->getOperand(4)->getAsZExtVal() != SystemZ::subreg_h64)
8128 return false;
8129
8130 LoPart = Val->getOperand(1);
8131 HiPart = Val->getOperand(3);
8132 return true;
8133}
8134
8135SDValue SystemZTargetLowering::combineSTORE(
8136 SDNode *N, DAGCombinerInfo &DCI) const {
8137 SelectionDAG &DAG = DCI.DAG;
8138 auto *SN = cast<StoreSDNode>(N);
8139 auto &Op1 = N->getOperand(1);
8140 EVT MemVT = SN->getMemoryVT();
8141
8142 if (SN->getAddressSpace() == SYSTEMZAS::PTR32) {
8143 MVT PtrVT = getPointerTy(DAG.getDataLayout());
8144 MVT StoreNodeVT = SN->getBasePtr().getSimpleValueType();
8145 if (PtrVT != StoreNodeVT) {
8146 SDLoc DL(SN);
8147 SDValue AddrSpaceCast = DAG.getAddrSpaceCast(DL, PtrVT, SN->getBasePtr(),
8148 SYSTEMZAS::PTR32, 0);
8149 return DAG.getStore(SN->getChain(), DL, SN->getValue(), AddrSpaceCast,
8150 SN->getPointerInfo(), SN->getBaseAlign(),
8151 SN->getMemOperand()->getFlags(), SN->getAAInfo());
8152 }
8153 }
8154
8155 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
8156 // for the extraction to be done on a vMiN value, so that we can use VSTE.
8157 // If X has wider elements then convert it to:
8158 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
8159 if (MemVT.isInteger() && SN->isTruncatingStore()) {
8160 if (SDValue Value =
8161 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
8162 DCI.AddToWorklist(Value.getNode());
8163
8164 // Rewrite the store with the new form of stored value.
8165 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
8166 SN->getBasePtr(), SN->getMemoryVT(),
8167 SN->getMemOperand());
8168 }
8169 }
8170
8171 // combine STORE (LOAD_STACK_GUARD) into MOV_STACKGUARD_DAG
8172 if (Op1->isMachineOpcode() &&
8173 (Op1->getMachineOpcode() == SystemZ::LOAD_STACK_GUARD)) {
8174 // Obtain the frame index the store was targeting.
8175 int FI = cast<FrameIndexSDNode>(SN->getOperand(2))->getIndex();
8176 // Prepare operands of the MOV_STACKGUARD ISD Node - Chain and FrameIndex.
8177 SDValue Ops[] = {SN->getChain(), DAG.getTargetFrameIndex(FI, MVT::i64)};
8178 return DAG.getNode(SystemZISD::MOV_STACKGUARD, SDLoc(SN), MVT::Other, Ops);
8179 }
8180
8181 // Combine STORE (BSWAP) into STRVH/STRV/STRVG/VSTBR
8182 if (!SN->isTruncatingStore() &&
8183 Op1.getOpcode() == ISD::BSWAP &&
8184 Op1.getNode()->hasOneUse() &&
8185 canLoadStoreByteSwapped(Op1.getValueType())) {
8186
8187 SDValue BSwapOp = Op1.getOperand(0);
8188
8189 if (BSwapOp.getValueType() == MVT::i16)
8190 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
8191
8192 SDValue Ops[] = {
8193 N->getOperand(0), BSwapOp, N->getOperand(2)
8194 };
8195
8196 return
8197 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
8198 Ops, MemVT, SN->getMemOperand());
8199 }
8200 // Combine STORE (element-swap) into VSTER
8201 if (!SN->isTruncatingStore() &&
8202 Op1.getOpcode() == ISD::VECTOR_SHUFFLE &&
8203 Op1.getNode()->hasOneUse() &&
8204 Subtarget.hasVectorEnhancements2()) {
8205 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op1.getNode());
8206 ArrayRef<int> ShuffleMask = SVN->getMask();
8207 if (isVectorElementSwap(ShuffleMask, Op1.getValueType())) {
8208 SDValue Ops[] = {
8209 N->getOperand(0), Op1.getOperand(0), N->getOperand(2)
8210 };
8211
8212 return DAG.getMemIntrinsicNode(SystemZISD::VSTER, SDLoc(N),
8213 DAG.getVTList(MVT::Other),
8214 Ops, MemVT, SN->getMemOperand());
8215 }
8216 }
8217
8218 // Combine STORE (READCYCLECOUNTER) into STCKF.
8219 if (!SN->isTruncatingStore() &&
8221 Op1.hasOneUse() &&
8222 N->getOperand(0).reachesChainWithoutSideEffects(SDValue(Op1.getNode(), 1))) {
8223 SDValue Ops[] = { Op1.getOperand(0), N->getOperand(2) };
8224 return DAG.getMemIntrinsicNode(SystemZISD::STCKF, SDLoc(N),
8225 DAG.getVTList(MVT::Other),
8226 Ops, MemVT, SN->getMemOperand());
8227 }
8228
8229 // Transform a store of a 128-bit value moved from parts into two stores.
8230 if (SN->isSimple() && ISD::isNormalStore(SN)) {
8231 SDValue LoPart, HiPart;
8232 if ((MemVT == MVT::i128 && isI128MovedFromParts(Op1, LoPart, HiPart)) ||
8233 (MemVT == MVT::f128 && isF128MovedFromParts(Op1, LoPart, HiPart))) {
8234 SDLoc DL(SN);
8235 SDValue Chain0 = DAG.getStore(
8236 SN->getChain(), DL, HiPart, SN->getBasePtr(), SN->getPointerInfo(),
8237 SN->getBaseAlign(), SN->getMemOperand()->getFlags(), SN->getAAInfo());
8238 SDValue Chain1 = DAG.getStore(
8239 SN->getChain(), DL, LoPart,
8240 DAG.getObjectPtrOffset(DL, SN->getBasePtr(), TypeSize::getFixed(8)),
8241 SN->getPointerInfo().getWithOffset(8), SN->getBaseAlign(),
8242 SN->getMemOperand()->getFlags(), SN->getAAInfo());
8243
8244 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain0, Chain1);
8245 }
8246 }
8247
8248 // Replicate a reg or immediate with VREP instead of scalar multiply or
8249 // immediate load. It seems best to do this during the first DAGCombine as
8250 // it is straight-forward to handle the zero-extend node in the initial
8251 // DAG, and also not worry about the keeping the new MemVT legal (e.g. when
8252 // extracting an i16 element from a v16i8 vector).
8253 if (Subtarget.hasVector() && DCI.Level == BeforeLegalizeTypes &&
8254 isOnlyUsedByStores(Op1, DAG)) {
8255 SDValue Word = SDValue();
8256 EVT WordVT;
8257
8258 // Find a replicated immediate and return it if found in Word and its
8259 // type in WordVT.
8260 auto FindReplicatedImm = [&](ConstantSDNode *C, unsigned TotBytes) {
8261 // Some constants are better handled with a scalar store.
8262 if (C->getAPIntValue().getBitWidth() > 64 || C->isAllOnes() ||
8263 isInt<16>(C->getSExtValue()) || MemVT.getStoreSize() <= 2)
8264 return;
8265
8266 APInt Val = C->getAPIntValue();
8267 // Truncate Val in case of a truncating store.
8268 if (!llvm::isUIntN(TotBytes * 8, Val.getZExtValue())) {
8269 assert(SN->isTruncatingStore() &&
8270 "Non-truncating store and immediate value does not fit?");
8271 Val = Val.trunc(TotBytes * 8);
8272 }
8273
8274 SystemZVectorConstantInfo VCI(APInt(TotBytes * 8, Val.getZExtValue()));
8275 if (VCI.isVectorConstantLegal(Subtarget) &&
8276 VCI.Opcode == SystemZISD::REPLICATE) {
8277 Word = DAG.getConstant(VCI.OpVals[0], SDLoc(SN), MVT::i32);
8278 WordVT = VCI.VecVT.getScalarType();
8279 }
8280 };
8281
8282 // Find a replicated register and return it if found in Word and its type
8283 // in WordVT.
8284 auto FindReplicatedReg = [&](SDValue MulOp) {
8285 EVT MulVT = MulOp.getValueType();
8286 if (MulOp->getOpcode() == ISD::MUL &&
8287 (MulVT == MVT::i16 || MulVT == MVT::i32 || MulVT == MVT::i64)) {
8288 // Find a zero extended value and its type.
8289 SDValue LHS = MulOp->getOperand(0);
8290 if (LHS->getOpcode() == ISD::ZERO_EXTEND)
8291 WordVT = LHS->getOperand(0).getValueType();
8292 else if (LHS->getOpcode() == ISD::AssertZext)
8293 WordVT = cast<VTSDNode>(LHS->getOperand(1))->getVT();
8294 else
8295 return;
8296 // Find a replicating constant, e.g. 0x00010001.
8297 if (auto *C = dyn_cast<ConstantSDNode>(MulOp->getOperand(1))) {
8298 SystemZVectorConstantInfo VCI(
8299 APInt(MulVT.getSizeInBits(), C->getZExtValue()));
8300 if (VCI.isVectorConstantLegal(Subtarget) &&
8301 VCI.Opcode == SystemZISD::REPLICATE && VCI.OpVals[0] == 1 &&
8302 WordVT == VCI.VecVT.getScalarType())
8303 Word = DAG.getZExtOrTrunc(LHS->getOperand(0), SDLoc(SN), WordVT);
8304 }
8305 }
8306 };
8307
8308 if (isa<BuildVectorSDNode>(Op1) &&
8309 DAG.isSplatValue(Op1, true/*AllowUndefs*/)) {
8310 SDValue SplatVal = Op1->getOperand(0);
8311 if (auto *C = dyn_cast<ConstantSDNode>(SplatVal))
8312 FindReplicatedImm(C, SplatVal.getValueType().getStoreSize());
8313 else
8314 FindReplicatedReg(SplatVal);
8315 } else {
8316 if (auto *C = dyn_cast<ConstantSDNode>(Op1))
8317 FindReplicatedImm(C, MemVT.getStoreSize());
8318 else
8319 FindReplicatedReg(Op1);
8320 }
8321
8322 if (Word != SDValue()) {
8323 assert(MemVT.getSizeInBits() % WordVT.getSizeInBits() == 0 &&
8324 "Bad type handling");
8325 unsigned NumElts = MemVT.getSizeInBits() / WordVT.getSizeInBits();
8326 EVT SplatVT = EVT::getVectorVT(*DAG.getContext(), WordVT, NumElts);
8327 SDValue SplatVal = DAG.getSplatVector(SplatVT, SDLoc(SN), Word);
8328 return DAG.getStore(SN->getChain(), SDLoc(SN), SplatVal,
8329 SN->getBasePtr(), SN->getMemOperand());
8330 }
8331 }
8332
8333 return SDValue();
8334}
8335
8336SDValue SystemZTargetLowering::combineVECTOR_SHUFFLE(
8337 SDNode *N, DAGCombinerInfo &DCI) const {
8338 SelectionDAG &DAG = DCI.DAG;
8339 // Combine element-swap (LOAD) into VLER
8340 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
8341 N->getOperand(0).hasOneUse() &&
8342 Subtarget.hasVectorEnhancements2()) {
8343 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8344 ArrayRef<int> ShuffleMask = SVN->getMask();
8345 if (isVectorElementSwap(ShuffleMask, N->getValueType(0))) {
8346 SDValue Load = N->getOperand(0);
8347 LoadSDNode *LD = cast<LoadSDNode>(Load);
8348
8349 // Create the element-swapping load.
8350 SDValue Ops[] = {
8351 LD->getChain(), // Chain
8352 LD->getBasePtr() // Ptr
8353 };
8354 SDValue ESLoad =
8355 DAG.getMemIntrinsicNode(SystemZISD::VLER, SDLoc(N),
8356 DAG.getVTList(LD->getValueType(0), MVT::Other),
8357 Ops, LD->getMemoryVT(), LD->getMemOperand());
8358
8359 // First, combine the VECTOR_SHUFFLE away. This makes the value produced
8360 // by the load dead.
8361 DCI.CombineTo(N, ESLoad);
8362
8363 // Next, combine the load away, we give it a bogus result value but a real
8364 // chain result. The result value is dead because the shuffle is dead.
8365 DCI.CombineTo(Load.getNode(), ESLoad, ESLoad.getValue(1));
8366
8367 // Return N so it doesn't get rechecked!
8368 return SDValue(N, 0);
8369 }
8370 }
8371
8372 return SDValue();
8373}
8374
8375SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
8376 SDNode *N, DAGCombinerInfo &DCI) const {
8377 SelectionDAG &DAG = DCI.DAG;
8378
8379 if (!Subtarget.hasVector())
8380 return SDValue();
8381
8382 // Look through bitcasts that retain the number of vector elements.
8383 SDValue Op = N->getOperand(0);
8384 if (Op.getOpcode() == ISD::BITCAST &&
8385 Op.getValueType().isVector() &&
8386 Op.getOperand(0).getValueType().isVector() &&
8387 Op.getValueType().getVectorNumElements() ==
8388 Op.getOperand(0).getValueType().getVectorNumElements())
8389 Op = Op.getOperand(0);
8390
8391 // Pull BSWAP out of a vector extraction.
8392 if (Op.getOpcode() == ISD::BSWAP && Op.hasOneUse()) {
8393 EVT VecVT = Op.getValueType();
8394 EVT EltVT = VecVT.getVectorElementType();
8395 Op = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), EltVT,
8396 Op.getOperand(0), N->getOperand(1));
8397 DCI.AddToWorklist(Op.getNode());
8398 Op = DAG.getNode(ISD::BSWAP, SDLoc(N), EltVT, Op);
8399 if (EltVT != N->getValueType(0)) {
8400 DCI.AddToWorklist(Op.getNode());
8401 Op = DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0), Op);
8402 }
8403 return Op;
8404 }
8405
8406 // Try to simplify a vector extraction.
8407 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8408 SDValue Op0 = N->getOperand(0);
8409 EVT VecVT = Op0.getValueType();
8410 if (canTreatAsByteVector(VecVT))
8411 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
8412 IndexN->getZExtValue(), DCI, false);
8413 }
8414 return SDValue();
8415}
8416
8417SDValue SystemZTargetLowering::combineJOIN_DWORDS(
8418 SDNode *N, DAGCombinerInfo &DCI) const {
8419 SelectionDAG &DAG = DCI.DAG;
8420 // (join_dwords X, X) == (replicate X)
8421 if (N->getOperand(0) == N->getOperand(1))
8422 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
8423 N->getOperand(0));
8424 return SDValue();
8425}
8426
8428 SDValue Chain1 = N1->getOperand(0);
8429 SDValue Chain2 = N2->getOperand(0);
8430
8431 // Trivial case: both nodes take the same chain.
8432 if (Chain1 == Chain2)
8433 return Chain1;
8434
8435 // FIXME - we could handle more complex cases via TokenFactor,
8436 // assuming we can verify that this would not create a cycle.
8437 return SDValue();
8438}
8439
8440SDValue SystemZTargetLowering::combineFP_ROUND(
8441 SDNode *N, DAGCombinerInfo &DCI) const {
8442
8443 if (!Subtarget.hasVector())
8444 return SDValue();
8445
8446 // (fpround (extract_vector_elt X 0))
8447 // (fpround (extract_vector_elt X 1)) ->
8448 // (extract_vector_elt (VROUND X) 0)
8449 // (extract_vector_elt (VROUND X) 2)
8450 //
8451 // This is a special case since the target doesn't really support v2f32s.
8452 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
8453 SelectionDAG &DAG = DCI.DAG;
8454 SDValue Op0 = N->getOperand(OpNo);
8455 if (N->getValueType(0) == MVT::f32 && Op0.hasOneUse() &&
8457 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
8458 Op0.getOperand(1).getOpcode() == ISD::Constant &&
8459 Op0.getConstantOperandVal(1) == 0) {
8460 SDValue Vec = Op0.getOperand(0);
8461 for (auto *U : Vec->users()) {
8462 if (U != Op0.getNode() && U->hasOneUse() &&
8463 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
8464 U->getOperand(0) == Vec &&
8465 U->getOperand(1).getOpcode() == ISD::Constant &&
8466 U->getConstantOperandVal(1) == 1) {
8467 SDValue OtherRound = SDValue(*U->user_begin(), 0);
8468 if (OtherRound.getOpcode() == N->getOpcode() &&
8469 OtherRound.getOperand(OpNo) == SDValue(U, 0) &&
8470 OtherRound.getValueType() == MVT::f32) {
8471 SDValue VRound, Chain;
8472 if (N->isStrictFPOpcode()) {
8473 Chain = MergeInputChains(N, OtherRound.getNode());
8474 if (!Chain)
8475 continue;
8476 VRound = DAG.getNode(SystemZISD::STRICT_VROUND, SDLoc(N),
8477 {MVT::v4f32, MVT::Other}, {Chain, Vec});
8478 Chain = VRound.getValue(1);
8479 } else
8480 VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
8481 MVT::v4f32, Vec);
8482 DCI.AddToWorklist(VRound.getNode());
8483 SDValue Extract1 =
8484 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
8485 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
8486 DCI.AddToWorklist(Extract1.getNode());
8487 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
8488 if (Chain)
8489 DAG.ReplaceAllUsesOfValueWith(OtherRound.getValue(1), Chain);
8490 SDValue Extract0 =
8491 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
8492 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
8493 if (Chain)
8494 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op0),
8495 N->getVTList(), Extract0, Chain);
8496 return Extract0;
8497 }
8498 }
8499 }
8500 }
8501 return SDValue();
8502}
8503
8504SDValue SystemZTargetLowering::combineFP_EXTEND(
8505 SDNode *N, DAGCombinerInfo &DCI) const {
8506
8507 if (!Subtarget.hasVector())
8508 return SDValue();
8509
8510 // (fpextend (extract_vector_elt X 0))
8511 // (fpextend (extract_vector_elt X 2)) ->
8512 // (extract_vector_elt (VEXTEND X) 0)
8513 // (extract_vector_elt (VEXTEND X) 1)
8514 //
8515 // This is a special case since the target doesn't really support v2f32s.
8516 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
8517 SelectionDAG &DAG = DCI.DAG;
8518 SDValue Op0 = N->getOperand(OpNo);
8519 if (N->getValueType(0) == MVT::f64 && Op0.hasOneUse() &&
8521 Op0.getOperand(0).getValueType() == MVT::v4f32 &&
8522 Op0.getOperand(1).getOpcode() == ISD::Constant &&
8523 Op0.getConstantOperandVal(1) == 0) {
8524 SDValue Vec = Op0.getOperand(0);
8525 for (auto *U : Vec->users()) {
8526 if (U != Op0.getNode() && U->hasOneUse() &&
8527 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
8528 U->getOperand(0) == Vec &&
8529 U->getOperand(1).getOpcode() == ISD::Constant &&
8530 U->getConstantOperandVal(1) == 2) {
8531 SDValue OtherExtend = SDValue(*U->user_begin(), 0);
8532 if (OtherExtend.getOpcode() == N->getOpcode() &&
8533 OtherExtend.getOperand(OpNo) == SDValue(U, 0) &&
8534 OtherExtend.getValueType() == MVT::f64) {
8535 SDValue VExtend, Chain;
8536 if (N->isStrictFPOpcode()) {
8537 Chain = MergeInputChains(N, OtherExtend.getNode());
8538 if (!Chain)
8539 continue;
8540 VExtend = DAG.getNode(SystemZISD::STRICT_VEXTEND, SDLoc(N),
8541 {MVT::v2f64, MVT::Other}, {Chain, Vec});
8542 Chain = VExtend.getValue(1);
8543 } else
8544 VExtend = DAG.getNode(SystemZISD::VEXTEND, SDLoc(N),
8545 MVT::v2f64, Vec);
8546 DCI.AddToWorklist(VExtend.getNode());
8547 SDValue Extract1 =
8548 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f64,
8549 VExtend, DAG.getConstant(1, SDLoc(U), MVT::i32));
8550 DCI.AddToWorklist(Extract1.getNode());
8551 DAG.ReplaceAllUsesOfValueWith(OtherExtend, Extract1);
8552 if (Chain)
8553 DAG.ReplaceAllUsesOfValueWith(OtherExtend.getValue(1), Chain);
8554 SDValue Extract0 =
8555 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f64,
8556 VExtend, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
8557 if (Chain)
8558 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op0),
8559 N->getVTList(), Extract0, Chain);
8560 return Extract0;
8561 }
8562 }
8563 }
8564 }
8565 return SDValue();
8566}
8567
8568SDValue SystemZTargetLowering::combineINT_TO_FP(
8569 SDNode *N, DAGCombinerInfo &DCI) const {
8570 if (DCI.Level != BeforeLegalizeTypes)
8571 return SDValue();
8572 SelectionDAG &DAG = DCI.DAG;
8573 LLVMContext &Ctx = *DAG.getContext();
8574 unsigned Opcode = N->getOpcode();
8575 EVT OutVT = N->getValueType(0);
8576 Type *OutLLVMTy = OutVT.getTypeForEVT(Ctx);
8577 SDValue Op = N->getOperand(0);
8578 unsigned OutScalarBits = OutLLVMTy->getScalarSizeInBits();
8579 unsigned InScalarBits = Op->getValueType(0).getScalarSizeInBits();
8580
8581 // Insert an extension before type-legalization to avoid scalarization, e.g.:
8582 // v2f64 = uint_to_fp v2i16
8583 // =>
8584 // v2f64 = uint_to_fp (v2i64 zero_extend v2i16)
8585 if (OutLLVMTy->isVectorTy() && OutScalarBits > InScalarBits &&
8586 OutScalarBits <= 64) {
8587 unsigned NumElts = cast<FixedVectorType>(OutLLVMTy)->getNumElements();
8588 EVT ExtVT = EVT::getVectorVT(
8589 Ctx, EVT::getIntegerVT(Ctx, OutLLVMTy->getScalarSizeInBits()), NumElts);
8590 unsigned ExtOpcode =
8592 SDValue ExtOp = DAG.getNode(ExtOpcode, SDLoc(N), ExtVT, Op);
8593 return DAG.getNode(Opcode, SDLoc(N), OutVT, ExtOp);
8594 }
8595 return SDValue();
8596}
8597
8598SDValue SystemZTargetLowering::combineFCOPYSIGN(
8599 SDNode *N, DAGCombinerInfo &DCI) const {
8600 SelectionDAG &DAG = DCI.DAG;
8601 EVT VT = N->getValueType(0);
8602 SDValue ValOp = N->getOperand(0);
8603 SDValue SignOp = N->getOperand(1);
8604
8605 // Remove the rounding which is not needed.
8606 if (SignOp.getOpcode() == ISD::FP_ROUND) {
8607 SDValue WideOp = SignOp.getOperand(0);
8608 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, ValOp, WideOp);
8609 }
8610
8611 return SDValue();
8612}
8613
8614SDValue SystemZTargetLowering::combineBSWAP(
8615 SDNode *N, DAGCombinerInfo &DCI) const {
8616 SelectionDAG &DAG = DCI.DAG;
8617 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG/VLBR
8618 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
8619 N->getOperand(0).hasOneUse() &&
8620 canLoadStoreByteSwapped(N->getValueType(0))) {
8621 SDValue Load = N->getOperand(0);
8622 LoadSDNode *LD = cast<LoadSDNode>(Load);
8623
8624 // Create the byte-swapping load.
8625 SDValue Ops[] = {
8626 LD->getChain(), // Chain
8627 LD->getBasePtr() // Ptr
8628 };
8629 EVT LoadVT = N->getValueType(0);
8630 if (LoadVT == MVT::i16)
8631 LoadVT = MVT::i32;
8632 SDValue BSLoad =
8633 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
8634 DAG.getVTList(LoadVT, MVT::Other),
8635 Ops, LD->getMemoryVT(), LD->getMemOperand());
8636
8637 // If this is an i16 load, insert the truncate.
8638 SDValue ResVal = BSLoad;
8639 if (N->getValueType(0) == MVT::i16)
8640 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
8641
8642 // First, combine the bswap away. This makes the value produced by the
8643 // load dead.
8644 DCI.CombineTo(N, ResVal);
8645
8646 // Next, combine the load away, we give it a bogus result value but a real
8647 // chain result. The result value is dead because the bswap is dead.
8648 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
8649
8650 // Return N so it doesn't get rechecked!
8651 return SDValue(N, 0);
8652 }
8653
8654 // Look through bitcasts that retain the number of vector elements.
8655 SDValue Op = N->getOperand(0);
8656 if (Op.getOpcode() == ISD::BITCAST &&
8657 Op.getValueType().isVector() &&
8658 Op.getOperand(0).getValueType().isVector() &&
8659 Op.getValueType().getVectorNumElements() ==
8660 Op.getOperand(0).getValueType().getVectorNumElements())
8661 Op = Op.getOperand(0);
8662
8663 // Push BSWAP into a vector insertion if at least one side then simplifies.
8664 if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT && Op.hasOneUse()) {
8665 SDValue Vec = Op.getOperand(0);
8666 SDValue Elt = Op.getOperand(1);
8667 SDValue Idx = Op.getOperand(2);
8668
8670 Vec.getOpcode() == ISD::BSWAP || Vec.isUndef() ||
8672 Elt.getOpcode() == ISD::BSWAP || Elt.isUndef() ||
8673 (canLoadStoreByteSwapped(N->getValueType(0)) &&
8674 ISD::isNON_EXTLoad(Elt.getNode()) && Elt.hasOneUse())) {
8675 EVT VecVT = N->getValueType(0);
8676 EVT EltVT = N->getValueType(0).getVectorElementType();
8677 if (VecVT != Vec.getValueType()) {
8678 Vec = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Vec);
8679 DCI.AddToWorklist(Vec.getNode());
8680 }
8681 if (EltVT != Elt.getValueType()) {
8682 Elt = DAG.getNode(ISD::BITCAST, SDLoc(N), EltVT, Elt);
8683 DCI.AddToWorklist(Elt.getNode());
8684 }
8685 Vec = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Vec);
8686 DCI.AddToWorklist(Vec.getNode());
8687 Elt = DAG.getNode(ISD::BSWAP, SDLoc(N), EltVT, Elt);
8688 DCI.AddToWorklist(Elt.getNode());
8689 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), VecVT,
8690 Vec, Elt, Idx);
8691 }
8692 }
8693
8694 // Push BSWAP into a vector shuffle if at least one side then simplifies.
8695 ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(Op);
8696 if (SV && Op.hasOneUse()) {
8697 SDValue Op0 = Op.getOperand(0);
8698 SDValue Op1 = Op.getOperand(1);
8699
8701 Op0.getOpcode() == ISD::BSWAP || Op0.isUndef() ||
8703 Op1.getOpcode() == ISD::BSWAP || Op1.isUndef()) {
8704 EVT VecVT = N->getValueType(0);
8705 if (VecVT != Op0.getValueType()) {
8706 Op0 = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Op0);
8707 DCI.AddToWorklist(Op0.getNode());
8708 }
8709 if (VecVT != Op1.getValueType()) {
8710 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Op1);
8711 DCI.AddToWorklist(Op1.getNode());
8712 }
8713 Op0 = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Op0);
8714 DCI.AddToWorklist(Op0.getNode());
8715 Op1 = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Op1);
8716 DCI.AddToWorklist(Op1.getNode());
8717 return DAG.getVectorShuffle(VecVT, SDLoc(N), Op0, Op1, SV->getMask());
8718 }
8719 }
8720
8721 return SDValue();
8722}
8723
8724SDValue SystemZTargetLowering::combineSETCC(
8725 SDNode *N, DAGCombinerInfo &DCI) const {
8726 SelectionDAG &DAG = DCI.DAG;
8727 const ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
8728 const SDValue LHS = N->getOperand(0);
8729 const SDValue RHS = N->getOperand(1);
8730 bool CmpNull = isNullConstant(RHS);
8731 bool CmpAllOnes = isAllOnesConstant(RHS);
8732 EVT VT = N->getValueType(0);
8733 SDLoc DL(N);
8734
8735 // Match icmp_eq/ne(bitcast(icmp(X,Y)),0/-1) reduction patterns, and
8736 // change the outer compare to a i128 compare. This will normally
8737 // allow the reduction to be recognized in adjustICmp128, and even if
8738 // not, the i128 compare will still generate better code.
8739 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && (CmpNull || CmpAllOnes)) {
8741 if (Src.getOpcode() == ISD::SETCC &&
8742 Src.getValueType().isFixedLengthVector() &&
8743 Src.getValueType().getScalarType() == MVT::i1) {
8744 EVT CmpVT = Src.getOperand(0).getValueType();
8745 if (CmpVT.getSizeInBits() == 128) {
8746 EVT IntVT = CmpVT.changeVectorElementTypeToInteger();
8747 SDValue LHS =
8748 DAG.getBitcast(MVT::i128, DAG.getSExtOrTrunc(Src, DL, IntVT));
8749 SDValue RHS = CmpNull ? DAG.getConstant(0, DL, MVT::i128)
8750 : DAG.getAllOnesConstant(DL, MVT::i128);
8751 return DAG.getNode(ISD::SETCC, DL, VT, LHS, RHS, N->getOperand(2),
8752 N->getFlags());
8753 }
8754 }
8755 }
8756
8757 return SDValue();
8758}
8759
8760static std::pair<SDValue, int> findCCUse(const SDValue &Val,
8761 unsigned Depth = 0) {
8762 // Limit depth of potentially exponential walk.
8763 if (Depth > 5)
8764 return std::make_pair(SDValue(), SystemZ::CCMASK_NONE);
8765
8766 switch (Val.getOpcode()) {
8767 default:
8768 return std::make_pair(SDValue(), SystemZ::CCMASK_NONE);
8769 case SystemZISD::IPM:
8770 if (Val.getOperand(0).getOpcode() == SystemZISD::CLC ||
8771 Val.getOperand(0).getOpcode() == SystemZISD::STRCMP)
8772 return std::make_pair(Val.getOperand(0), SystemZ::CCMASK_ICMP);
8773 return std::make_pair(Val.getOperand(0), SystemZ::CCMASK_ANY);
8774 case SystemZISD::SELECT_CCMASK: {
8775 SDValue Op4CCReg = Val.getOperand(4);
8776 if (Op4CCReg.getOpcode() == SystemZISD::ICMP ||
8777 Op4CCReg.getOpcode() == SystemZISD::TM) {
8778 auto [OpCC, OpCCValid] = findCCUse(Op4CCReg.getOperand(0), Depth + 1);
8779 if (OpCC != SDValue())
8780 return std::make_pair(OpCC, OpCCValid);
8781 }
8782 auto *CCValid = dyn_cast<ConstantSDNode>(Val.getOperand(2));
8783 if (!CCValid)
8784 return std::make_pair(SDValue(), SystemZ::CCMASK_NONE);
8785 int CCValidVal = CCValid->getZExtValue();
8786 return std::make_pair(Op4CCReg, CCValidVal);
8787 }
8788 case ISD::ADD:
8789 case ISD::AND:
8790 case ISD::OR:
8791 case ISD::XOR:
8792 case ISD::SHL:
8793 case ISD::SRA:
8794 case ISD::SRL:
8795 auto [Op0CC, Op0CCValid] = findCCUse(Val.getOperand(0), Depth + 1);
8796 if (Op0CC != SDValue())
8797 return std::make_pair(Op0CC, Op0CCValid);
8798 return findCCUse(Val.getOperand(1), Depth + 1);
8799 }
8800}
8801
8802static bool combineCCMask(SDValue &CCReg, int &CCValid, int &CCMask,
8803 SelectionDAG &DAG);
8804
8806 SelectionDAG &DAG) {
8807 SDLoc DL(Val);
8808 auto Opcode = Val.getOpcode();
8809 switch (Opcode) {
8810 default:
8811 return {};
8812 case ISD::Constant:
8813 return {Val, Val, Val, Val};
8814 case SystemZISD::IPM: {
8815 SDValue IPMOp0 = Val.getOperand(0);
8816 if (IPMOp0 != CC)
8817 return {};
8818 SmallVector<SDValue, 4> ShiftedCCVals;
8819 for (auto CC : {0, 1, 2, 3})
8820 ShiftedCCVals.emplace_back(
8821 DAG.getConstant((CC << SystemZ::IPM_CC), DL, MVT::i32));
8822 return ShiftedCCVals;
8823 }
8824 case SystemZISD::SELECT_CCMASK: {
8825 SDValue TrueVal = Val.getOperand(0), FalseVal = Val.getOperand(1);
8826 auto *CCValid = dyn_cast<ConstantSDNode>(Val.getOperand(2));
8827 auto *CCMask = dyn_cast<ConstantSDNode>(Val.getOperand(3));
8828 if (!CCValid || !CCMask)
8829 return {};
8830
8831 int CCValidVal = CCValid->getZExtValue();
8832 int CCMaskVal = CCMask->getZExtValue();
8833 // Pruning search tree early - Moving CC test and combineCCMask ahead of
8834 // recursive call to simplifyAssumingCCVal.
8835 SDValue Op4CCReg = Val.getOperand(4);
8836 if (Op4CCReg != CC)
8837 combineCCMask(Op4CCReg, CCValidVal, CCMaskVal, DAG);
8838 if (Op4CCReg != CC)
8839 return {};
8840 const auto &&TrueSDVals = simplifyAssumingCCVal(TrueVal, CC, DAG);
8841 const auto &&FalseSDVals = simplifyAssumingCCVal(FalseVal, CC, DAG);
8842 if (TrueSDVals.empty() || FalseSDVals.empty())
8843 return {};
8844 SmallVector<SDValue, 4> MergedSDVals;
8845 for (auto &CCVal : {0, 1, 2, 3})
8846 MergedSDVals.emplace_back(((CCMaskVal & (1 << (3 - CCVal))) != 0)
8847 ? TrueSDVals[CCVal]
8848 : FalseSDVals[CCVal]);
8849 return MergedSDVals;
8850 }
8851 case ISD::ADD:
8852 case ISD::AND:
8853 case ISD::OR:
8854 case ISD::XOR:
8855 case ISD::SRA:
8856 // Avoid introducing CC spills (because ADD/AND/OR/XOR/SRA
8857 // would clobber CC).
8858 if (!Val.hasOneUse())
8859 return {};
8860 [[fallthrough]];
8861 case ISD::SHL:
8862 case ISD::SRL:
8863 SDValue Op0 = Val.getOperand(0), Op1 = Val.getOperand(1);
8864 const auto &&Op0SDVals = simplifyAssumingCCVal(Op0, CC, DAG);
8865 const auto &&Op1SDVals = simplifyAssumingCCVal(Op1, CC, DAG);
8866 if (Op0SDVals.empty() || Op1SDVals.empty())
8867 return {};
8868 SmallVector<SDValue, 4> BinaryOpSDVals;
8869 for (auto CCVal : {0, 1, 2, 3})
8870 BinaryOpSDVals.emplace_back(DAG.getNode(
8871 Opcode, DL, Val.getValueType(), Op0SDVals[CCVal], Op1SDVals[CCVal]));
8872 return BinaryOpSDVals;
8873 }
8874}
8875
8876static bool combineCCMask(SDValue &CCReg, int &CCValid, int &CCMask,
8877 SelectionDAG &DAG) {
8878 // We have a SELECT_CCMASK or BR_CCMASK comparing the condition code
8879 // set by the CCReg instruction using the CCValid / CCMask masks,
8880 // If the CCReg instruction is itself a ICMP / TM testing the condition
8881 // code set by some other instruction, see whether we can directly
8882 // use that condition code.
8883 auto *CCNode = CCReg.getNode();
8884 if (!CCNode)
8885 return false;
8886
8887 if (CCNode->getOpcode() == SystemZISD::TM) {
8888 if (CCValid != SystemZ::CCMASK_TM)
8889 return false;
8890 auto emulateTMCCMask = [](const SDValue &Op0Val, const SDValue &Op1Val) {
8891 auto *Op0Node = dyn_cast<ConstantSDNode>(Op0Val.getNode());
8892 auto *Op1Node = dyn_cast<ConstantSDNode>(Op1Val.getNode());
8893 if (!Op0Node || !Op1Node)
8894 return -1;
8895 auto Op0APVal = Op0Node->getAPIntValue();
8896 auto Op1APVal = Op1Node->getAPIntValue();
8897 auto Result = Op0APVal & Op1APVal;
8898 bool AllOnes = Result == Op1APVal;
8899 bool AllZeros = Result == 0;
8900 bool IsLeftMostBitSet = Result[Op1APVal.getActiveBits() - 1] != 0;
8901 return AllZeros ? 0 : AllOnes ? 3 : IsLeftMostBitSet ? 2 : 1;
8902 };
8903 SDValue Op0 = CCNode->getOperand(0);
8904 SDValue Op1 = CCNode->getOperand(1);
8905 auto [Op0CC, Op0CCValid] = findCCUse(Op0);
8906 if (Op0CC == SDValue())
8907 return false;
8908 const auto &&Op0SDVals = simplifyAssumingCCVal(Op0, Op0CC, DAG);
8909 const auto &&Op1SDVals = simplifyAssumingCCVal(Op1, Op0CC, DAG);
8910 if (Op0SDVals.empty() || Op1SDVals.empty())
8911 return false;
8912 int NewCCMask = 0;
8913 for (auto CC : {0, 1, 2, 3}) {
8914 auto CCVal = emulateTMCCMask(Op0SDVals[CC], Op1SDVals[CC]);
8915 if (CCVal < 0)
8916 return false;
8917 NewCCMask <<= 1;
8918 NewCCMask |= (CCMask & (1 << (3 - CCVal))) != 0;
8919 }
8920 NewCCMask &= Op0CCValid;
8921 CCReg = Op0CC;
8922 CCMask = NewCCMask;
8923 CCValid = Op0CCValid;
8924 return true;
8925 }
8926 if (CCNode->getOpcode() != SystemZISD::ICMP ||
8927 CCValid != SystemZ::CCMASK_ICMP)
8928 return false;
8929
8930 SDValue CmpOp0 = CCNode->getOperand(0);
8931 SDValue CmpOp1 = CCNode->getOperand(1);
8932 SDValue CmpOp2 = CCNode->getOperand(2);
8933 auto [Op0CC, Op0CCValid] = findCCUse(CmpOp0);
8934 if (Op0CC != SDValue()) {
8935 const auto &&Op0SDVals = simplifyAssumingCCVal(CmpOp0, Op0CC, DAG);
8936 const auto &&Op1SDVals = simplifyAssumingCCVal(CmpOp1, Op0CC, DAG);
8937 if (Op0SDVals.empty() || Op1SDVals.empty())
8938 return false;
8939
8940 auto *CmpType = dyn_cast<ConstantSDNode>(CmpOp2);
8941 auto CmpTypeVal = CmpType->getZExtValue();
8942 const auto compareCCSigned = [&CmpTypeVal](const SDValue &Op0Val,
8943 const SDValue &Op1Val) {
8944 auto *Op0Node = dyn_cast<ConstantSDNode>(Op0Val.getNode());
8945 auto *Op1Node = dyn_cast<ConstantSDNode>(Op1Val.getNode());
8946 if (!Op0Node || !Op1Node)
8947 return -1;
8948 auto Op0APVal = Op0Node->getAPIntValue();
8949 auto Op1APVal = Op1Node->getAPIntValue();
8950 if (CmpTypeVal == SystemZICMP::SignedOnly)
8951 return Op0APVal == Op1APVal ? 0 : Op0APVal.slt(Op1APVal) ? 1 : 2;
8952 return Op0APVal == Op1APVal ? 0 : Op0APVal.ult(Op1APVal) ? 1 : 2;
8953 };
8954 int NewCCMask = 0;
8955 for (auto CC : {0, 1, 2, 3}) {
8956 auto CCVal = compareCCSigned(Op0SDVals[CC], Op1SDVals[CC]);
8957 if (CCVal < 0)
8958 return false;
8959 NewCCMask <<= 1;
8960 NewCCMask |= (CCMask & (1 << (3 - CCVal))) != 0;
8961 }
8962 NewCCMask &= Op0CCValid;
8963 CCMask = NewCCMask;
8964 CCReg = Op0CC;
8965 CCValid = Op0CCValid;
8966 return true;
8967 }
8968
8969 return false;
8970}
8971
8972// Merging versus split in multiple branches cost.
8975 const Value *Lhs,
8976 const Value *Rhs,
8977 const Function *) const {
8978 const auto isFlagOutOpCC = [](const Value *V) {
8979 using namespace llvm::PatternMatch;
8980 const Value *RHSVal;
8981 const APInt *RHSC;
8982 if (const auto *I = dyn_cast<Instruction>(V)) {
8983 // PatternMatch.h provides concise tree-based pattern match of llvm IR.
8984 if (match(I->getOperand(0), m_And(m_Value(RHSVal), m_APInt(RHSC))) ||
8985 match(I, m_Cmp(m_Value(RHSVal), m_APInt(RHSC)))) {
8986 if (const auto *CB = dyn_cast<CallBase>(RHSVal)) {
8987 if (CB->isInlineAsm()) {
8988 const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());
8989 return IA && IA->getConstraintString().contains("{@cc}");
8990 }
8991 }
8992 }
8993 }
8994 return false;
8995 };
8996 // Pattern (ICmp %asm) or (ICmp (And %asm)).
8997 // Cost of longest dependency chain (ICmp, And) is 2. CostThreshold or
8998 // BaseCost can be set >=2. If cost of instruction <= CostThreshold
8999 // conditionals will be merged or else conditionals will be split.
9000 if (isFlagOutOpCC(Lhs) && isFlagOutOpCC(Rhs))
9001 return {3, 0, -1};
9002 // Default.
9003 return {-1, -1, -1};
9004}
9005
9006SDValue SystemZTargetLowering::combineBR_CCMASK(SDNode *N,
9007 DAGCombinerInfo &DCI) const {
9008 SelectionDAG &DAG = DCI.DAG;
9009
9010 // Combine BR_CCMASK (ICMP (SELECT_CCMASK)) into a single BR_CCMASK.
9011 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1));
9012 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2));
9013 if (!CCValid || !CCMask)
9014 return SDValue();
9015
9016 int CCValidVal = CCValid->getZExtValue();
9017 int CCMaskVal = CCMask->getZExtValue();
9018 SDValue Chain = N->getOperand(0);
9019 SDValue CCReg = N->getOperand(4);
9020 // If combineCMask was able to merge or simplify ccvalid or ccmask, re-emit
9021 // the modified BR_CCMASK with the new values.
9022 // In order to avoid conditional branches with full or empty cc masks, do not
9023 // do this if ccmask is 0 or equal to ccvalid.
9024 if (combineCCMask(CCReg, CCValidVal, CCMaskVal, DAG) && CCMaskVal != 0 &&
9025 CCMaskVal != CCValidVal)
9026 return DAG.getNode(SystemZISD::BR_CCMASK, SDLoc(N), N->getValueType(0),
9027 Chain,
9028 DAG.getTargetConstant(CCValidVal, SDLoc(N), MVT::i32),
9029 DAG.getTargetConstant(CCMaskVal, SDLoc(N), MVT::i32),
9030 N->getOperand(3), CCReg);
9031 return SDValue();
9032}
9033
9034SDValue SystemZTargetLowering::combineSELECT_CCMASK(
9035 SDNode *N, DAGCombinerInfo &DCI) const {
9036 SelectionDAG &DAG = DCI.DAG;
9037
9038 // Combine SELECT_CCMASK (ICMP (SELECT_CCMASK)) into a single SELECT_CCMASK.
9039 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(2));
9040 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(3));
9041 if (!CCValid || !CCMask)
9042 return SDValue();
9043
9044 int CCValidVal = CCValid->getZExtValue();
9045 int CCMaskVal = CCMask->getZExtValue();
9046 SDValue CCReg = N->getOperand(4);
9047
9048 bool IsCombinedCCReg = combineCCMask(CCReg, CCValidVal, CCMaskVal, DAG);
9049
9050 // Populate SDVals vector for each condition code ccval for given Val, which
9051 // can again be another nested select_ccmask with the same CC.
9052 const auto constructCCSDValsFromSELECT = [&CCReg](SDValue &Val) {
9053 if (Val.getOpcode() == SystemZISD::SELECT_CCMASK) {
9055 if (Val.getOperand(4) != CCReg)
9056 return SmallVector<SDValue, 4>{};
9057 SDValue TrueVal = Val.getOperand(0), FalseVal = Val.getOperand(1);
9058 auto *CCMask = dyn_cast<ConstantSDNode>(Val.getOperand(3));
9059 if (!CCMask)
9060 return SmallVector<SDValue, 4>{};
9061
9062 int CCMaskVal = CCMask->getZExtValue();
9063 for (auto &CC : {0, 1, 2, 3})
9064 Res.emplace_back(((CCMaskVal & (1 << (3 - CC))) != 0) ? TrueVal
9065 : FalseVal);
9066 return Res;
9067 }
9068 return SmallVector<SDValue, 4>{Val, Val, Val, Val};
9069 };
9070 // Attempting to optimize TrueVal/FalseVal in outermost select_ccmask either
9071 // with CCReg found by combineCCMask or original CCReg.
9072 SDValue TrueVal = N->getOperand(0);
9073 SDValue FalseVal = N->getOperand(1);
9074 auto &&TrueSDVals = simplifyAssumingCCVal(TrueVal, CCReg, DAG);
9075 auto &&FalseSDVals = simplifyAssumingCCVal(FalseVal, CCReg, DAG);
9076 // TrueSDVals/FalseSDVals might be empty in case of non-constant
9077 // TrueVal/FalseVal for select_ccmask, which can not be optimized further.
9078 if (TrueSDVals.empty())
9079 TrueSDVals = constructCCSDValsFromSELECT(TrueVal);
9080 if (FalseSDVals.empty())
9081 FalseSDVals = constructCCSDValsFromSELECT(FalseVal);
9082 if (!TrueSDVals.empty() && !FalseSDVals.empty()) {
9083 SmallSet<SDValue, 4> MergedSDValsSet;
9084 // Ignoring CC values outside CCValiid.
9085 for (auto CC : {0, 1, 2, 3}) {
9086 if ((CCValidVal & ((1 << (3 - CC)))) != 0)
9087 MergedSDValsSet.insert(((CCMaskVal & (1 << (3 - CC))) != 0)
9088 ? TrueSDVals[CC]
9089 : FalseSDVals[CC]);
9090 }
9091 if (MergedSDValsSet.size() == 1)
9092 return *MergedSDValsSet.begin();
9093 if (MergedSDValsSet.size() == 2) {
9094 auto BeginIt = MergedSDValsSet.begin();
9095 SDValue NewTrueVal = *BeginIt, NewFalseVal = *next(BeginIt);
9096 if (NewTrueVal == FalseVal || NewFalseVal == TrueVal)
9097 std::swap(NewTrueVal, NewFalseVal);
9098 int NewCCMask = 0;
9099 for (auto CC : {0, 1, 2, 3}) {
9100 NewCCMask <<= 1;
9101 NewCCMask |= ((CCMaskVal & (1 << (3 - CC))) != 0)
9102 ? (TrueSDVals[CC] == NewTrueVal)
9103 : (FalseSDVals[CC] == NewTrueVal);
9104 }
9105 CCMaskVal = NewCCMask;
9106 CCMaskVal &= CCValidVal;
9107 TrueVal = NewTrueVal;
9108 FalseVal = NewFalseVal;
9109 IsCombinedCCReg = true;
9110 }
9111 }
9112 // If the condition is trivially false or trivially true after
9113 // combineCCMask, just collapse this SELECT_CCMASK to the indicated value
9114 // (possibly modified by constructCCSDValsFromSELECT).
9115 if (CCMaskVal == 0)
9116 return FalseVal;
9117 if (CCMaskVal == CCValidVal)
9118 return TrueVal;
9119
9120 if (IsCombinedCCReg)
9121 return DAG.getNode(
9122 SystemZISD::SELECT_CCMASK, SDLoc(N), N->getValueType(0), TrueVal,
9123 FalseVal, DAG.getTargetConstant(CCValidVal, SDLoc(N), MVT::i32),
9124 DAG.getTargetConstant(CCMaskVal, SDLoc(N), MVT::i32), CCReg);
9125
9126 return SDValue();
9127}
9128
9129SDValue SystemZTargetLowering::combineGET_CCMASK(
9130 SDNode *N, DAGCombinerInfo &DCI) const {
9131
9132 // Optimize away GET_CCMASK (SELECT_CCMASK) if the CC masks are compatible
9133 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1));
9134 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2));
9135 if (!CCValid || !CCMask)
9136 return SDValue();
9137 int CCValidVal = CCValid->getZExtValue();
9138 int CCMaskVal = CCMask->getZExtValue();
9139
9140 SDValue Select = N->getOperand(0);
9141 if (Select->getOpcode() == ISD::TRUNCATE)
9142 Select = Select->getOperand(0);
9143 if (Select->getOpcode() != SystemZISD::SELECT_CCMASK)
9144 return SDValue();
9145
9146 auto *SelectCCValid = dyn_cast<ConstantSDNode>(Select->getOperand(2));
9147 auto *SelectCCMask = dyn_cast<ConstantSDNode>(Select->getOperand(3));
9148 if (!SelectCCValid || !SelectCCMask)
9149 return SDValue();
9150 int SelectCCValidVal = SelectCCValid->getZExtValue();
9151 int SelectCCMaskVal = SelectCCMask->getZExtValue();
9152
9153 auto *TrueVal = dyn_cast<ConstantSDNode>(Select->getOperand(0));
9154 auto *FalseVal = dyn_cast<ConstantSDNode>(Select->getOperand(1));
9155 if (!TrueVal || !FalseVal)
9156 return SDValue();
9157 if (TrueVal->getZExtValue() == 1 && FalseVal->getZExtValue() == 0)
9158 ;
9159 else if (TrueVal->getZExtValue() == 0 && FalseVal->getZExtValue() == 1)
9160 SelectCCMaskVal ^= SelectCCValidVal;
9161 else
9162 return SDValue();
9163
9164 if (SelectCCValidVal & ~CCValidVal)
9165 return SDValue();
9166 if (SelectCCMaskVal != (CCMaskVal & SelectCCValidVal))
9167 return SDValue();
9168
9169 return Select->getOperand(4);
9170}
9171
9172SDValue SystemZTargetLowering::combineIntDIVREM(
9173 SDNode *N, DAGCombinerInfo &DCI) const {
9174 SelectionDAG &DAG = DCI.DAG;
9175 EVT VT = N->getValueType(0);
9176 // In the case where the divisor is a vector of constants a cheaper
9177 // sequence of instructions can replace the divide. BuildSDIV is called to
9178 // do this during DAG combining, but it only succeeds when it can build a
9179 // multiplication node. The only option for SystemZ is ISD::SMUL_LOHI, and
9180 // since it is not Legal but Custom it can only happen before
9181 // legalization. Therefore we must scalarize this early before Combine
9182 // 1. For widened vectors, this is already the result of type legalization.
9183 if (DCI.Level == BeforeLegalizeTypes && VT.isVector() && isTypeLegal(VT) &&
9184 DAG.isConstantIntBuildVectorOrConstantInt(N->getOperand(1)))
9185 return DAG.UnrollVectorOp(N);
9186 return SDValue();
9187}
9188
9189
9190// Transform a right shift of a multiply-and-add into a multiply-and-add-high.
9191// This is closely modeled after the common-code combineShiftToMULH.
9192SDValue SystemZTargetLowering::combineShiftToMulAddHigh(
9193 SDNode *N, DAGCombinerInfo &DCI) const {
9194 SelectionDAG &DAG = DCI.DAG;
9195 SDLoc DL(N);
9196
9197 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
9198 "SRL or SRA node is required here!");
9199
9200 if (!Subtarget.hasVector())
9201 return SDValue();
9202
9203 // Check the shift amount. Proceed with the transformation if the shift
9204 // amount is constant.
9205 ConstantSDNode *ShiftAmtSrc = isConstOrConstSplat(N->getOperand(1));
9206 if (!ShiftAmtSrc)
9207 return SDValue();
9208
9209 // The operation feeding into the shift must be an add.
9210 SDValue ShiftOperand = N->getOperand(0);
9211 if (ShiftOperand.getOpcode() != ISD::ADD)
9212 return SDValue();
9213
9214 // One operand of the add must be a multiply.
9215 SDValue MulOp = ShiftOperand.getOperand(0);
9216 SDValue AddOp = ShiftOperand.getOperand(1);
9217 if (MulOp.getOpcode() != ISD::MUL) {
9218 if (AddOp.getOpcode() != ISD::MUL)
9219 return SDValue();
9220 std::swap(MulOp, AddOp);
9221 }
9222
9223 // All operands must be equivalent extend nodes.
9224 SDValue LeftOp = MulOp.getOperand(0);
9225 SDValue RightOp = MulOp.getOperand(1);
9226
9227 bool IsSignExt = LeftOp.getOpcode() == ISD::SIGN_EXTEND;
9228 bool IsZeroExt = LeftOp.getOpcode() == ISD::ZERO_EXTEND;
9229
9230 if (!IsSignExt && !IsZeroExt)
9231 return SDValue();
9232
9233 EVT NarrowVT = LeftOp.getOperand(0).getValueType();
9234 unsigned NarrowVTSize = NarrowVT.getScalarSizeInBits();
9235
9236 SDValue MulhRightOp;
9237 if (ConstantSDNode *Constant = isConstOrConstSplat(RightOp)) {
9238 unsigned ActiveBits = IsSignExt
9239 ? Constant->getAPIntValue().getSignificantBits()
9240 : Constant->getAPIntValue().getActiveBits();
9241 if (ActiveBits > NarrowVTSize)
9242 return SDValue();
9243 MulhRightOp = DAG.getConstant(
9244 Constant->getAPIntValue().trunc(NarrowVT.getScalarSizeInBits()), DL,
9245 NarrowVT);
9246 } else {
9247 if (LeftOp.getOpcode() != RightOp.getOpcode())
9248 return SDValue();
9249 // Check that the two extend nodes are the same type.
9250 if (NarrowVT != RightOp.getOperand(0).getValueType())
9251 return SDValue();
9252 MulhRightOp = RightOp.getOperand(0);
9253 }
9254
9255 SDValue MulhAddOp;
9256 if (ConstantSDNode *Constant = isConstOrConstSplat(AddOp)) {
9257 unsigned ActiveBits = IsSignExt
9258 ? Constant->getAPIntValue().getSignificantBits()
9259 : Constant->getAPIntValue().getActiveBits();
9260 if (ActiveBits > NarrowVTSize)
9261 return SDValue();
9262 MulhAddOp = DAG.getConstant(
9263 Constant->getAPIntValue().trunc(NarrowVT.getScalarSizeInBits()), DL,
9264 NarrowVT);
9265 } else {
9266 if (LeftOp.getOpcode() != AddOp.getOpcode())
9267 return SDValue();
9268 // Check that the two extend nodes are the same type.
9269 if (NarrowVT != AddOp.getOperand(0).getValueType())
9270 return SDValue();
9271 MulhAddOp = AddOp.getOperand(0);
9272 }
9273
9274 EVT WideVT = LeftOp.getValueType();
9275 // Proceed with the transformation if the wide types match.
9276 assert((WideVT == RightOp.getValueType()) &&
9277 "Cannot have a multiply node with two different operand types.");
9278 assert((WideVT == AddOp.getValueType()) &&
9279 "Cannot have an add node with two different operand types.");
9280
9281 // Proceed with the transformation if the wide type is twice as large
9282 // as the narrow type.
9283 if (WideVT.getScalarSizeInBits() != 2 * NarrowVTSize)
9284 return SDValue();
9285
9286 // Check the shift amount with the narrow type size.
9287 // Proceed with the transformation if the shift amount is the width
9288 // of the narrow type.
9289 unsigned ShiftAmt = ShiftAmtSrc->getZExtValue();
9290 if (ShiftAmt != NarrowVTSize)
9291 return SDValue();
9292
9293 // Proceed if we support the multiply-and-add-high operation.
9294 if (!(NarrowVT == MVT::v16i8 || NarrowVT == MVT::v8i16 ||
9295 NarrowVT == MVT::v4i32 ||
9296 (Subtarget.hasVectorEnhancements3() &&
9297 (NarrowVT == MVT::v2i64 || NarrowVT == MVT::i128))))
9298 return SDValue();
9299
9300 // Emit the VMAH (signed) or VMALH (unsigned) operation.
9301 SDValue Result = DAG.getNode(IsSignExt ? SystemZISD::VMAH : SystemZISD::VMALH,
9302 DL, NarrowVT, LeftOp.getOperand(0),
9303 MulhRightOp, MulhAddOp);
9304 bool IsSigned = N->getOpcode() == ISD::SRA;
9305 return DAG.getExtOrTrunc(IsSigned, Result, DL, WideVT);
9306}
9307
9308// Op is an operand of a multiplication. Check whether this can be folded
9309// into an even/odd widening operation; if so, return the opcode to be used
9310// and update Op to the appropriate sub-operand. Note that the caller must
9311// verify that *both* operands of the multiplication support the operation.
9313 const SystemZSubtarget &Subtarget,
9314 SDValue &Op) {
9315 EVT VT = Op.getValueType();
9316
9317 // Check for (sign/zero_extend_vector_inreg (vector_shuffle)) corresponding
9318 // to selecting the even or odd vector elements.
9319 if (VT.isVector() && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
9320 (Op.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG ||
9321 Op.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG)) {
9322 bool IsSigned = Op.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG;
9323 unsigned NumElts = VT.getVectorNumElements();
9324 Op = Op.getOperand(0);
9325 if (Op.getValueType().getVectorNumElements() == 2 * NumElts &&
9326 Op.getOpcode() == ISD::VECTOR_SHUFFLE) {
9328 ArrayRef<int> ShuffleMask = SVN->getMask();
9329 bool CanUseEven = true, CanUseOdd = true;
9330 for (unsigned Elt = 0; Elt < NumElts; Elt++) {
9331 if (ShuffleMask[Elt] == -1)
9332 continue;
9333 if (unsigned(ShuffleMask[Elt]) != 2 * Elt)
9334 CanUseEven = false;
9335 if (unsigned(ShuffleMask[Elt]) != 2 * Elt + 1)
9336 CanUseOdd = false;
9337 }
9338 Op = Op.getOperand(0);
9339 if (CanUseEven)
9340 return IsSigned ? SystemZISD::VME : SystemZISD::VMLE;
9341 if (CanUseOdd)
9342 return IsSigned ? SystemZISD::VMO : SystemZISD::VMLO;
9343 }
9344 }
9345
9346 // For z17, we can also support the v2i64->i128 case, which looks like
9347 // (sign/zero_extend (extract_vector_elt X 0/1))
9348 if (VT == MVT::i128 && Subtarget.hasVectorEnhancements3() &&
9349 (Op.getOpcode() == ISD::SIGN_EXTEND ||
9350 Op.getOpcode() == ISD::ZERO_EXTEND)) {
9351 bool IsSigned = Op.getOpcode() == ISD::SIGN_EXTEND;
9352 Op = Op.getOperand(0);
9353 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
9354 Op.getOperand(0).getValueType() == MVT::v2i64 &&
9355 Op.getOperand(1).getOpcode() == ISD::Constant) {
9356 unsigned Elem = Op.getConstantOperandVal(1);
9357 Op = Op.getOperand(0);
9358 if (Elem == 0)
9359 return IsSigned ? SystemZISD::VME : SystemZISD::VMLE;
9360 if (Elem == 1)
9361 return IsSigned ? SystemZISD::VMO : SystemZISD::VMLO;
9362 }
9363 }
9364
9365 return 0;
9366}
9367
9368SDValue SystemZTargetLowering::combineMUL(
9369 SDNode *N, DAGCombinerInfo &DCI) const {
9370 SelectionDAG &DAG = DCI.DAG;
9371
9372 // Detect even/odd widening multiplication.
9373 SDValue Op0 = N->getOperand(0);
9374 SDValue Op1 = N->getOperand(1);
9375 unsigned OpcodeCand0 = detectEvenOddMultiplyOperand(DAG, Subtarget, Op0);
9376 unsigned OpcodeCand1 = detectEvenOddMultiplyOperand(DAG, Subtarget, Op1);
9377 if (OpcodeCand0 && OpcodeCand0 == OpcodeCand1)
9378 return DAG.getNode(OpcodeCand0, SDLoc(N), N->getValueType(0), Op0, Op1);
9379
9380 return SDValue();
9381}
9382
9383SDValue SystemZTargetLowering::combineINTRINSIC(
9384 SDNode *N, DAGCombinerInfo &DCI) const {
9385 SelectionDAG &DAG = DCI.DAG;
9386
9387 unsigned Id = N->getConstantOperandVal(1);
9388 switch (Id) {
9389 // VECTOR LOAD (RIGHTMOST) WITH LENGTH with a length operand of 15
9390 // or larger is simply a vector load.
9391 case Intrinsic::s390_vll:
9392 case Intrinsic::s390_vlrl:
9393 if (auto *C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
9394 if (C->getZExtValue() >= 15)
9395 return DAG.getLoad(N->getValueType(0), SDLoc(N), N->getOperand(0),
9396 N->getOperand(3), MachinePointerInfo());
9397 break;
9398 // Likewise for VECTOR STORE (RIGHTMOST) WITH LENGTH.
9399 case Intrinsic::s390_vstl:
9400 case Intrinsic::s390_vstrl:
9401 if (auto *C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
9402 if (C->getZExtValue() >= 15)
9403 return DAG.getStore(N->getOperand(0), SDLoc(N), N->getOperand(2),
9404 N->getOperand(4), MachinePointerInfo());
9405 break;
9406 }
9407
9408 return SDValue();
9409}
9410
9411SDValue SystemZTargetLowering::unwrapAddress(SDValue N) const {
9412 if (N->getOpcode() == SystemZISD::PCREL_WRAPPER)
9413 return N->getOperand(0);
9414 return N;
9415}
9416
9418 DAGCombinerInfo &DCI) const {
9419 switch(N->getOpcode()) {
9420 default: break;
9421 case ISD::ZERO_EXTEND: return combineZERO_EXTEND(N, DCI);
9422 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
9423 case ISD::SIGN_EXTEND_INREG: return combineSIGN_EXTEND_INREG(N, DCI);
9424 case SystemZISD::MERGE_HIGH:
9425 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
9426 case ISD::LOAD: return combineLOAD(N, DCI);
9427 case ISD::STORE: return combineSTORE(N, DCI);
9428 case ISD::VECTOR_SHUFFLE: return combineVECTOR_SHUFFLE(N, DCI);
9429 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
9430 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
9432 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
9434 case ISD::FP_EXTEND: return combineFP_EXTEND(N, DCI);
9435 case ISD::SINT_TO_FP:
9436 case ISD::UINT_TO_FP: return combineINT_TO_FP(N, DCI);
9437 case ISD::FCOPYSIGN: return combineFCOPYSIGN(N, DCI);
9438 case ISD::BSWAP: return combineBSWAP(N, DCI);
9439 case ISD::SETCC: return combineSETCC(N, DCI);
9440 case SystemZISD::BR_CCMASK: return combineBR_CCMASK(N, DCI);
9441 case SystemZISD::SELECT_CCMASK: return combineSELECT_CCMASK(N, DCI);
9442 case SystemZISD::GET_CCMASK: return combineGET_CCMASK(N, DCI);
9443 case ISD::SRL:
9444 case ISD::SRA: return combineShiftToMulAddHigh(N, DCI);
9445 case ISD::MUL: return combineMUL(N, DCI);
9446 case ISD::SDIV:
9447 case ISD::UDIV:
9448 case ISD::SREM:
9449 case ISD::UREM: return combineIntDIVREM(N, DCI);
9451 case ISD::INTRINSIC_VOID: return combineINTRINSIC(N, DCI);
9452 }
9453
9454 return SDValue();
9455}
9456
9457// Return the demanded elements for the OpNo source operand of Op. DemandedElts
9458// are for Op.
9459static APInt getDemandedSrcElements(SDValue Op, const APInt &DemandedElts,
9460 unsigned OpNo) {
9461 EVT VT = Op.getValueType();
9462 unsigned NumElts = (VT.isVector() ? VT.getVectorNumElements() : 1);
9463 APInt SrcDemE;
9464 unsigned Opcode = Op.getOpcode();
9465 if (Opcode == ISD::INTRINSIC_WO_CHAIN) {
9466 unsigned Id = Op.getConstantOperandVal(0);
9467 switch (Id) {
9468 case Intrinsic::s390_vpksh: // PACKS
9469 case Intrinsic::s390_vpksf:
9470 case Intrinsic::s390_vpksg:
9471 case Intrinsic::s390_vpkshs: // PACKS_CC
9472 case Intrinsic::s390_vpksfs:
9473 case Intrinsic::s390_vpksgs:
9474 case Intrinsic::s390_vpklsh: // PACKLS
9475 case Intrinsic::s390_vpklsf:
9476 case Intrinsic::s390_vpklsg:
9477 case Intrinsic::s390_vpklshs: // PACKLS_CC
9478 case Intrinsic::s390_vpklsfs:
9479 case Intrinsic::s390_vpklsgs:
9480 // VECTOR PACK truncates the elements of two source vectors into one.
9481 SrcDemE = DemandedElts;
9482 if (OpNo == 2)
9483 SrcDemE.lshrInPlace(NumElts / 2);
9484 SrcDemE = SrcDemE.trunc(NumElts / 2);
9485 break;
9486 // VECTOR UNPACK extends half the elements of the source vector.
9487 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH
9488 case Intrinsic::s390_vuphh:
9489 case Intrinsic::s390_vuphf:
9490 case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH
9491 case Intrinsic::s390_vuplhh:
9492 case Intrinsic::s390_vuplhf:
9493 SrcDemE = APInt(NumElts * 2, 0);
9494 SrcDemE.insertBits(DemandedElts, 0);
9495 break;
9496 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW
9497 case Intrinsic::s390_vuplhw:
9498 case Intrinsic::s390_vuplf:
9499 case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW
9500 case Intrinsic::s390_vupllh:
9501 case Intrinsic::s390_vupllf:
9502 SrcDemE = APInt(NumElts * 2, 0);
9503 SrcDemE.insertBits(DemandedElts, NumElts);
9504 break;
9505 case Intrinsic::s390_vpdi: {
9506 // VECTOR PERMUTE DWORD IMMEDIATE selects one element from each source.
9507 SrcDemE = APInt(NumElts, 0);
9508 if (!DemandedElts[OpNo - 1])
9509 break;
9510 unsigned Mask = Op.getConstantOperandVal(3);
9511 unsigned MaskBit = ((OpNo - 1) ? 1 : 4);
9512 // Demand input element 0 or 1, given by the mask bit value.
9513 SrcDemE.setBit((Mask & MaskBit)? 1 : 0);
9514 break;
9515 }
9516 case Intrinsic::s390_vsldb: {
9517 // VECTOR SHIFT LEFT DOUBLE BY BYTE
9518 assert(VT == MVT::v16i8 && "Unexpected type.");
9519 unsigned FirstIdx = Op.getConstantOperandVal(3);
9520 assert (FirstIdx > 0 && FirstIdx < 16 && "Unused operand.");
9521 unsigned NumSrc0Els = 16 - FirstIdx;
9522 SrcDemE = APInt(NumElts, 0);
9523 if (OpNo == 1) {
9524 APInt DemEls = DemandedElts.trunc(NumSrc0Els);
9525 SrcDemE.insertBits(DemEls, FirstIdx);
9526 } else {
9527 APInt DemEls = DemandedElts.lshr(NumSrc0Els);
9528 SrcDemE.insertBits(DemEls, 0);
9529 }
9530 break;
9531 }
9532 case Intrinsic::s390_vperm:
9533 SrcDemE = APInt::getAllOnes(NumElts);
9534 break;
9535 default:
9536 llvm_unreachable("Unhandled intrinsic.");
9537 break;
9538 }
9539 } else {
9540 switch (Opcode) {
9541 case SystemZISD::JOIN_DWORDS:
9542 // Scalar operand.
9543 SrcDemE = APInt(1, 1);
9544 break;
9545 case SystemZISD::SELECT_CCMASK:
9546 SrcDemE = DemandedElts;
9547 break;
9548 default:
9549 llvm_unreachable("Unhandled opcode.");
9550 break;
9551 }
9552 }
9553 return SrcDemE;
9554}
9555
9557 const APInt &DemandedElts,
9558 const SelectionDAG &DAG, unsigned Depth,
9559 unsigned OpNo) {
9560 APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo);
9561 APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1);
9562 KnownBits LHSKnown =
9563 DAG.computeKnownBits(Op.getOperand(OpNo), Src0DemE, Depth + 1);
9564 KnownBits RHSKnown =
9565 DAG.computeKnownBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1);
9566 Known = LHSKnown.intersectWith(RHSKnown);
9567}
9568
9569void
9572 const APInt &DemandedElts,
9573 const SelectionDAG &DAG,
9574 unsigned Depth) const {
9575 Known.resetAll();
9576
9577 // Intrinsic CC result is returned in the two low bits.
9578 unsigned Tmp0, Tmp1; // not used
9579 if (Op.getResNo() == 1 && isIntrinsicWithCC(Op, Tmp0, Tmp1)) {
9580 Known.Zero.setBitsFrom(2);
9581 return;
9582 }
9583 EVT VT = Op.getValueType();
9584 if (Op.getResNo() != 0 || VT == MVT::Untyped)
9585 return;
9586 assert (Known.getBitWidth() == VT.getScalarSizeInBits() &&
9587 "KnownBits does not match VT in bitwidth");
9588 assert ((!VT.isVector() ||
9589 (DemandedElts.getBitWidth() == VT.getVectorNumElements())) &&
9590 "DemandedElts does not match VT number of elements");
9591 unsigned BitWidth = Known.getBitWidth();
9592 unsigned Opcode = Op.getOpcode();
9593 if (Opcode == ISD::INTRINSIC_WO_CHAIN) {
9594 bool IsLogical = false;
9595 unsigned Id = Op.getConstantOperandVal(0);
9596 switch (Id) {
9597 case Intrinsic::s390_vpksh: // PACKS
9598 case Intrinsic::s390_vpksf:
9599 case Intrinsic::s390_vpksg:
9600 case Intrinsic::s390_vpkshs: // PACKS_CC
9601 case Intrinsic::s390_vpksfs:
9602 case Intrinsic::s390_vpksgs:
9603 case Intrinsic::s390_vpklsh: // PACKLS
9604 case Intrinsic::s390_vpklsf:
9605 case Intrinsic::s390_vpklsg:
9606 case Intrinsic::s390_vpklshs: // PACKLS_CC
9607 case Intrinsic::s390_vpklsfs:
9608 case Intrinsic::s390_vpklsgs:
9609 case Intrinsic::s390_vpdi:
9610 case Intrinsic::s390_vsldb:
9611 case Intrinsic::s390_vperm:
9612 computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 1);
9613 break;
9614 case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH
9615 case Intrinsic::s390_vuplhh:
9616 case Intrinsic::s390_vuplhf:
9617 case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW
9618 case Intrinsic::s390_vupllh:
9619 case Intrinsic::s390_vupllf:
9620 IsLogical = true;
9621 [[fallthrough]];
9622 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH
9623 case Intrinsic::s390_vuphh:
9624 case Intrinsic::s390_vuphf:
9625 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW
9626 case Intrinsic::s390_vuplhw:
9627 case Intrinsic::s390_vuplf: {
9628 SDValue SrcOp = Op.getOperand(1);
9629 APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 0);
9630 Known = DAG.computeKnownBits(SrcOp, SrcDemE, Depth + 1);
9631 if (IsLogical) {
9632 Known = Known.zext(BitWidth);
9633 } else
9634 Known = Known.sext(BitWidth);
9635 break;
9636 }
9637 default:
9638 break;
9639 }
9640 } else {
9641 switch (Opcode) {
9642 case SystemZISD::JOIN_DWORDS:
9643 case SystemZISD::SELECT_CCMASK:
9644 computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 0);
9645 break;
9646 case SystemZISD::REPLICATE: {
9647 SDValue SrcOp = Op.getOperand(0);
9648 Known = DAG.computeKnownBits(SrcOp, Depth + 1);
9649 if (Known.getBitWidth() < BitWidth && isa<ConstantSDNode>(SrcOp))
9650 Known = Known.sext(BitWidth); // VREPI sign extends the immedate.
9651 break;
9652 }
9653 default:
9654 break;
9655 }
9656 }
9657
9658 // Known has the width of the source operand(s). Adjust if needed to match
9659 // the passed bitwidth.
9660 if (Known.getBitWidth() != BitWidth)
9661 Known = Known.anyextOrTrunc(BitWidth);
9662}
9663
9664static unsigned computeNumSignBitsBinOp(SDValue Op, const APInt &DemandedElts,
9665 const SelectionDAG &DAG, unsigned Depth,
9666 unsigned OpNo) {
9667 APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo);
9668 unsigned LHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo), Src0DemE, Depth + 1);
9669 if (LHS == 1) return 1; // Early out.
9670 APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1);
9671 unsigned RHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1);
9672 if (RHS == 1) return 1; // Early out.
9673 unsigned Common = std::min(LHS, RHS);
9674 unsigned SrcBitWidth = Op.getOperand(OpNo).getScalarValueSizeInBits();
9675 EVT VT = Op.getValueType();
9676 unsigned VTBits = VT.getScalarSizeInBits();
9677 if (SrcBitWidth > VTBits) { // PACK
9678 unsigned SrcExtraBits = SrcBitWidth - VTBits;
9679 if (Common > SrcExtraBits)
9680 return (Common - SrcExtraBits);
9681 return 1;
9682 }
9683 assert (SrcBitWidth == VTBits && "Expected operands of same bitwidth.");
9684 return Common;
9685}
9686
9687unsigned
9689 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
9690 unsigned Depth) const {
9691 if (Op.getResNo() != 0)
9692 return 1;
9693 unsigned Opcode = Op.getOpcode();
9694 if (Opcode == ISD::INTRINSIC_WO_CHAIN) {
9695 unsigned Id = Op.getConstantOperandVal(0);
9696 switch (Id) {
9697 case Intrinsic::s390_vpksh: // PACKS
9698 case Intrinsic::s390_vpksf:
9699 case Intrinsic::s390_vpksg:
9700 case Intrinsic::s390_vpkshs: // PACKS_CC
9701 case Intrinsic::s390_vpksfs:
9702 case Intrinsic::s390_vpksgs:
9703 case Intrinsic::s390_vpklsh: // PACKLS
9704 case Intrinsic::s390_vpklsf:
9705 case Intrinsic::s390_vpklsg:
9706 case Intrinsic::s390_vpklshs: // PACKLS_CC
9707 case Intrinsic::s390_vpklsfs:
9708 case Intrinsic::s390_vpklsgs:
9709 case Intrinsic::s390_vpdi:
9710 case Intrinsic::s390_vsldb:
9711 case Intrinsic::s390_vperm:
9712 return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 1);
9713 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH
9714 case Intrinsic::s390_vuphh:
9715 case Intrinsic::s390_vuphf:
9716 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW
9717 case Intrinsic::s390_vuplhw:
9718 case Intrinsic::s390_vuplf: {
9719 SDValue PackedOp = Op.getOperand(1);
9720 APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 1);
9721 unsigned Tmp = DAG.ComputeNumSignBits(PackedOp, SrcDemE, Depth + 1);
9722 EVT VT = Op.getValueType();
9723 unsigned VTBits = VT.getScalarSizeInBits();
9724 Tmp += VTBits - PackedOp.getScalarValueSizeInBits();
9725 return Tmp;
9726 }
9727 default:
9728 break;
9729 }
9730 } else {
9731 switch (Opcode) {
9732 case SystemZISD::SELECT_CCMASK:
9733 return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 0);
9734 default:
9735 break;
9736 }
9737 }
9738
9739 return 1;
9740}
9741
9743 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
9744 UndefPoisonKind Kind, unsigned Depth) const {
9745 switch (Op->getOpcode()) {
9746 case SystemZISD::PCREL_WRAPPER:
9747 case SystemZISD::PCREL_OFFSET:
9748 return true;
9749 }
9750 return false;
9751}
9752
9753unsigned
9755 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
9756 unsigned StackAlign = TFI->getStackAlignment();
9757 assert(StackAlign >=1 && isPowerOf2_32(StackAlign) &&
9758 "Unexpected stack alignment");
9759 // The default stack probe size is 4096 if the function has no
9760 // stack-probe-size attribute.
9761 unsigned StackProbeSize =
9762 MF.getFunction().getFnAttributeAsParsedInteger("stack-probe-size", 4096);
9763 // Round down to the stack alignment.
9764 StackProbeSize &= ~(StackAlign - 1);
9765 return StackProbeSize ? StackProbeSize : StackAlign;
9766}
9767
9768//===----------------------------------------------------------------------===//
9769// Custom insertion
9770//===----------------------------------------------------------------------===//
9771
9772// Force base value Base into a register before MI. Return the register.
9774 const SystemZInstrInfo *TII) {
9775 MachineBasicBlock *MBB = MI.getParent();
9776 MachineFunction &MF = *MBB->getParent();
9777 MachineRegisterInfo &MRI = MF.getRegInfo();
9778
9779 if (Base.isReg()) {
9780 // Copy Base into a new virtual register to help register coalescing in
9781 // cases with multiple uses.
9782 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
9783 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), Reg)
9784 .add(Base);
9785 return Reg;
9786 }
9787
9788 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
9789 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
9790 .add(Base)
9791 .addImm(0)
9792 .addReg(0);
9793 return Reg;
9794}
9795
9796// The CC operand of MI might be missing a kill marker because there
9797// were multiple uses of CC, and ISel didn't know which to mark.
9798// Figure out whether MI should have had a kill marker.
9800 // Scan forward through BB for a use/def of CC.
9802 for (MachineBasicBlock::iterator miE = MBB->end(); miI != miE; ++miI) {
9803 const MachineInstr &MI = *miI;
9804 if (MI.readsRegister(SystemZ::CC, /*TRI=*/nullptr))
9805 return false;
9806 if (MI.definesRegister(SystemZ::CC, /*TRI=*/nullptr))
9807 break; // Should have kill-flag - update below.
9808 }
9809
9810 // If we hit the end of the block, check whether CC is live into a
9811 // successor.
9812 if (miI == MBB->end()) {
9813 for (const MachineBasicBlock *Succ : MBB->successors())
9814 if (Succ->isLiveIn(SystemZ::CC))
9815 return false;
9816 }
9817
9818 return true;
9819}
9820
9821// Return true if it is OK for this Select pseudo-opcode to be cascaded
9822// together with other Select pseudo-opcodes into a single basic-block with
9823// a conditional jump around it.
9825 switch (MI.getOpcode()) {
9826 case SystemZ::Select32:
9827 case SystemZ::Select64:
9828 case SystemZ::Select128:
9829 case SystemZ::SelectF32:
9830 case SystemZ::SelectF64:
9831 case SystemZ::SelectF128:
9832 case SystemZ::SelectVR32:
9833 case SystemZ::SelectVR64:
9834 case SystemZ::SelectVR128:
9835 return true;
9836
9837 default:
9838 return false;
9839 }
9840}
9841
9842// Helper function, which inserts PHI functions into SinkMBB:
9843// %Result(i) = phi [ %FalseValue(i), FalseMBB ], [ %TrueValue(i), TrueMBB ],
9844// where %FalseValue(i) and %TrueValue(i) are taken from Selects.
9846 MachineBasicBlock *TrueMBB,
9847 MachineBasicBlock *FalseMBB,
9848 MachineBasicBlock *SinkMBB) {
9849 MachineFunction *MF = TrueMBB->getParent();
9851
9852 MachineInstr *FirstMI = Selects.front();
9853 unsigned CCValid = FirstMI->getOperand(3).getImm();
9854 unsigned CCMask = FirstMI->getOperand(4).getImm();
9855
9856 MachineBasicBlock::iterator SinkInsertionPoint = SinkMBB->begin();
9857
9858 // As we are creating the PHIs, we have to be careful if there is more than
9859 // one. Later Selects may reference the results of earlier Selects, but later
9860 // PHIs have to reference the individual true/false inputs from earlier PHIs.
9861 // That also means that PHI construction must work forward from earlier to
9862 // later, and that the code must maintain a mapping from earlier PHI's
9863 // destination registers, and the registers that went into the PHI.
9865
9866 for (auto *MI : Selects) {
9867 Register DestReg = MI->getOperand(0).getReg();
9868 Register TrueReg = MI->getOperand(1).getReg();
9869 Register FalseReg = MI->getOperand(2).getReg();
9870
9871 // If this Select we are generating is the opposite condition from
9872 // the jump we generated, then we have to swap the operands for the
9873 // PHI that is going to be generated.
9874 if (MI->getOperand(4).getImm() == (CCValid ^ CCMask))
9875 std::swap(TrueReg, FalseReg);
9876
9877 if (auto It = RegRewriteTable.find(TrueReg); It != RegRewriteTable.end())
9878 TrueReg = It->second.first;
9879
9880 if (auto It = RegRewriteTable.find(FalseReg); It != RegRewriteTable.end())
9881 FalseReg = It->second.second;
9882
9883 DebugLoc DL = MI->getDebugLoc();
9884 BuildMI(*SinkMBB, SinkInsertionPoint, DL, TII->get(SystemZ::PHI), DestReg)
9885 .addReg(TrueReg).addMBB(TrueMBB)
9886 .addReg(FalseReg).addMBB(FalseMBB);
9887
9888 // Add this PHI to the rewrite table.
9889 RegRewriteTable[DestReg] = std::make_pair(TrueReg, FalseReg);
9890 }
9891
9892 MF->getProperties().resetNoPHIs();
9893}
9894
9896SystemZTargetLowering::emitAdjCallStack(MachineInstr &MI,
9897 MachineBasicBlock *BB) const {
9898 MachineFunction &MF = *BB->getParent();
9899 MachineFrameInfo &MFI = MF.getFrameInfo();
9900 auto *TFL = Subtarget.getFrameLowering<SystemZFrameLowering>();
9901 assert(TFL->hasReservedCallFrame(MF) &&
9902 "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
9903 (void)TFL;
9904 // Get the MaxCallFrameSize value and erase MI since it serves no further
9905 // purpose as the call frame is statically reserved in the prolog. Set
9906 // AdjustsStack as MI is *not* mapped as a frame instruction.
9907 uint32_t NumBytes = MI.getOperand(0).getImm();
9908 if (NumBytes > MFI.getMaxCallFrameSize())
9909 MFI.setMaxCallFrameSize(NumBytes);
9910 MFI.setAdjustsStack(true);
9911
9912 MI.eraseFromParent();
9913 return BB;
9914}
9915
9916// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
9918SystemZTargetLowering::emitSelect(MachineInstr &MI,
9919 MachineBasicBlock *MBB) const {
9920 assert(isSelectPseudo(MI) && "Bad call to emitSelect()");
9921 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
9922
9923 unsigned CCValid = MI.getOperand(3).getImm();
9924 unsigned CCMask = MI.getOperand(4).getImm();
9925
9926 // If we have a sequence of Select* pseudo instructions using the
9927 // same condition code value, we want to expand all of them into
9928 // a single pair of basic blocks using the same condition.
9929 SmallVector<MachineInstr*, 8> Selects;
9930 SmallVector<MachineInstr*, 8> DbgValues;
9931 Selects.push_back(&MI);
9932 unsigned Count = 0;
9933 for (MachineInstr &NextMI : llvm::make_range(
9934 std::next(MachineBasicBlock::iterator(MI)), MBB->end())) {
9935 if (isSelectPseudo(NextMI)) {
9936 assert(NextMI.getOperand(3).getImm() == CCValid &&
9937 "Bad CCValid operands since CC was not redefined.");
9938 if (NextMI.getOperand(4).getImm() == CCMask ||
9939 NextMI.getOperand(4).getImm() == (CCValid ^ CCMask)) {
9940 Selects.push_back(&NextMI);
9941 continue;
9942 }
9943 break;
9944 }
9945 if (NextMI.definesRegister(SystemZ::CC, /*TRI=*/nullptr) ||
9946 NextMI.usesCustomInsertionHook())
9947 break;
9948 bool User = false;
9949 for (auto *SelMI : Selects)
9950 if (NextMI.readsVirtualRegister(SelMI->getOperand(0).getReg())) {
9951 User = true;
9952 break;
9953 }
9954 if (NextMI.isDebugInstr()) {
9955 if (User) {
9956 assert(NextMI.isDebugValue() && "Unhandled debug opcode.");
9957 DbgValues.push_back(&NextMI);
9958 }
9959 } else if (User || ++Count > 20)
9960 break;
9961 }
9962
9963 MachineInstr *LastMI = Selects.back();
9964 bool CCKilled = (LastMI->killsRegister(SystemZ::CC, /*TRI=*/nullptr) ||
9965 checkCCKill(*LastMI, MBB));
9966 MachineBasicBlock *StartMBB = MBB;
9967 MachineBasicBlock *JoinMBB = SystemZ::splitBlockAfter(LastMI, MBB);
9968 MachineBasicBlock *FalseMBB = SystemZ::emitBlockAfter(StartMBB);
9969
9970 // Unless CC was killed in the last Select instruction, mark it as
9971 // live-in to both FalseMBB and JoinMBB.
9972 if (!CCKilled) {
9973 FalseMBB->addLiveIn(SystemZ::CC);
9974 JoinMBB->addLiveIn(SystemZ::CC);
9975 }
9976
9977 // StartMBB:
9978 // BRC CCMask, JoinMBB
9979 // # fallthrough to FalseMBB
9980 MBB = StartMBB;
9981 BuildMI(MBB, MI.getDebugLoc(), TII->get(SystemZ::BRC))
9982 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
9983 MBB->addSuccessor(JoinMBB);
9984 MBB->addSuccessor(FalseMBB);
9985
9986 // FalseMBB:
9987 // # fallthrough to JoinMBB
9988 MBB = FalseMBB;
9989 MBB->addSuccessor(JoinMBB);
9990
9991 // JoinMBB:
9992 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
9993 // ...
9994 MBB = JoinMBB;
9995 createPHIsForSelects(Selects, StartMBB, FalseMBB, MBB);
9996 for (auto *SelMI : Selects)
9997 SelMI->eraseFromParent();
9998
10000 for (auto *DbgMI : DbgValues)
10001 MBB->splice(InsertPos, StartMBB, DbgMI);
10002
10003 return JoinMBB;
10004}
10005
10006// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
10007// StoreOpcode is the store to use and Invert says whether the store should
10008// happen when the condition is false rather than true. If a STORE ON
10009// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
10010MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
10012 unsigned StoreOpcode,
10013 unsigned STOCOpcode,
10014 bool Invert) const {
10015 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10016
10017 Register SrcReg = MI.getOperand(0).getReg();
10018 MachineOperand Base = MI.getOperand(1);
10019 int64_t Disp = MI.getOperand(2).getImm();
10020 Register IndexReg = MI.getOperand(3).getReg();
10021 unsigned CCValid = MI.getOperand(4).getImm();
10022 unsigned CCMask = MI.getOperand(5).getImm();
10023 DebugLoc DL = MI.getDebugLoc();
10024
10025 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
10026
10027 // ISel pattern matching also adds a load memory operand of the same
10028 // address, so take special care to find the storing memory operand.
10029 MachineMemOperand *MMO = nullptr;
10030 for (auto *I : MI.memoperands())
10031 if (I->isStore()) {
10032 MMO = I;
10033 break;
10034 }
10035
10036 // Use STOCOpcode if possible. We could use different store patterns in
10037 // order to avoid matching the index register, but the performance trade-offs
10038 // might be more complicated in that case.
10039 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
10040 if (Invert)
10041 CCMask ^= CCValid;
10042
10043 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
10044 .addReg(SrcReg)
10045 .add(Base)
10046 .addImm(Disp)
10047 .addImm(CCValid)
10048 .addImm(CCMask)
10049 .addMemOperand(MMO);
10050
10051 MI.eraseFromParent();
10052 return MBB;
10053 }
10054
10055 // Get the condition needed to branch around the store.
10056 if (!Invert)
10057 CCMask ^= CCValid;
10058
10059 MachineBasicBlock *StartMBB = MBB;
10060 MachineBasicBlock *JoinMBB = SystemZ::splitBlockBefore(MI, MBB);
10061 MachineBasicBlock *FalseMBB = SystemZ::emitBlockAfter(StartMBB);
10062
10063 // Unless CC was killed in the CondStore instruction, mark it as
10064 // live-in to both FalseMBB and JoinMBB.
10065 if (!MI.killsRegister(SystemZ::CC, /*TRI=*/nullptr) &&
10066 !checkCCKill(MI, JoinMBB)) {
10067 FalseMBB->addLiveIn(SystemZ::CC);
10068 JoinMBB->addLiveIn(SystemZ::CC);
10069 }
10070
10071 // StartMBB:
10072 // BRC CCMask, JoinMBB
10073 // # fallthrough to FalseMBB
10074 MBB = StartMBB;
10075 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10076 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
10077 MBB->addSuccessor(JoinMBB);
10078 MBB->addSuccessor(FalseMBB);
10079
10080 // FalseMBB:
10081 // store %SrcReg, %Disp(%Index,%Base)
10082 // # fallthrough to JoinMBB
10083 MBB = FalseMBB;
10084 BuildMI(MBB, DL, TII->get(StoreOpcode))
10085 .addReg(SrcReg)
10086 .add(Base)
10087 .addImm(Disp)
10088 .addReg(IndexReg)
10089 .addMemOperand(MMO);
10090 MBB->addSuccessor(JoinMBB);
10091
10092 MI.eraseFromParent();
10093 return JoinMBB;
10094}
10095
10096// Implement EmitInstrWithCustomInserter for pseudo [SU]Cmp128Hi instruction MI.
10098SystemZTargetLowering::emitICmp128Hi(MachineInstr &MI,
10100 bool Unsigned) const {
10101 MachineFunction &MF = *MBB->getParent();
10102 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10103 MachineRegisterInfo &MRI = MF.getRegInfo();
10104
10105 // Synthetic instruction to compare 128-bit values.
10106 // Sets CC 1 if Op0 > Op1, sets a different CC otherwise.
10107 Register Op0 = MI.getOperand(0).getReg();
10108 Register Op1 = MI.getOperand(1).getReg();
10109
10110 MachineBasicBlock *StartMBB = MBB;
10111 MachineBasicBlock *JoinMBB = SystemZ::splitBlockAfter(MI, MBB);
10112 MachineBasicBlock *HiEqMBB = SystemZ::emitBlockAfter(StartMBB);
10113
10114 // StartMBB:
10115 //
10116 // Use VECTOR ELEMENT COMPARE [LOGICAL] to compare the high parts.
10117 // Swap the inputs to get:
10118 // CC 1 if high(Op0) > high(Op1)
10119 // CC 2 if high(Op0) < high(Op1)
10120 // CC 0 if high(Op0) == high(Op1)
10121 //
10122 // If CC != 0, we'd done, so jump over the next instruction.
10123 //
10124 // VEC[L]G Op1, Op0
10125 // JNE JoinMBB
10126 // # fallthrough to HiEqMBB
10127 MBB = StartMBB;
10128 int HiOpcode = Unsigned? SystemZ::VECLG : SystemZ::VECG;
10129 BuildMI(MBB, MI.getDebugLoc(), TII->get(HiOpcode))
10130 .addReg(Op1).addReg(Op0);
10131 BuildMI(MBB, MI.getDebugLoc(), TII->get(SystemZ::BRC))
10133 MBB->addSuccessor(JoinMBB);
10134 MBB->addSuccessor(HiEqMBB);
10135
10136 // HiEqMBB:
10137 //
10138 // Otherwise, use VECTOR COMPARE HIGH LOGICAL.
10139 // Since we already know the high parts are equal, the CC
10140 // result will only depend on the low parts:
10141 // CC 1 if low(Op0) > low(Op1)
10142 // CC 3 if low(Op0) <= low(Op1)
10143 //
10144 // VCHLGS Tmp, Op0, Op1
10145 // # fallthrough to JoinMBB
10146 MBB = HiEqMBB;
10147 Register Temp = MRI.createVirtualRegister(&SystemZ::VR128BitRegClass);
10148 BuildMI(MBB, MI.getDebugLoc(), TII->get(SystemZ::VCHLGS), Temp)
10149 .addReg(Op0).addReg(Op1);
10150 MBB->addSuccessor(JoinMBB);
10151
10152 // Mark CC as live-in to JoinMBB.
10153 JoinMBB->addLiveIn(SystemZ::CC);
10154
10155 MI.eraseFromParent();
10156 return JoinMBB;
10157}
10158
10159// Implement EmitInstrWithCustomInserter for subword pseudo ATOMIC_LOADW_* or
10160// ATOMIC_SWAPW instruction MI. BinOpcode is the instruction that performs
10161// the binary operation elided by "*", or 0 for ATOMIC_SWAPW. Invert says
10162// whether the field should be inverted after performing BinOpcode (e.g. for
10163// NAND).
10164MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
10165 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
10166 bool Invert) const {
10167 MachineFunction &MF = *MBB->getParent();
10168 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10169 MachineRegisterInfo &MRI = MF.getRegInfo();
10170
10171 // Extract the operands. Base can be a register or a frame index.
10172 // Src2 can be a register or immediate.
10173 Register Dest = MI.getOperand(0).getReg();
10174 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
10175 int64_t Disp = MI.getOperand(2).getImm();
10176 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
10177 Register BitShift = MI.getOperand(4).getReg();
10178 Register NegBitShift = MI.getOperand(5).getReg();
10179 unsigned BitSize = MI.getOperand(6).getImm();
10180 DebugLoc DL = MI.getDebugLoc();
10181
10182 // Get the right opcodes for the displacement.
10183 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
10184 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
10185 assert(LOpcode && CSOpcode && "Displacement out of range");
10186
10187 // Create virtual registers for temporary results.
10188 Register OrigVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10189 Register OldVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10190 Register NewVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10191 Register RotatedOldVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10192 Register RotatedNewVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10193
10194 // Insert a basic block for the main loop.
10195 MachineBasicBlock *StartMBB = MBB;
10196 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
10197 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB);
10198
10199 // StartMBB:
10200 // ...
10201 // %OrigVal = L Disp(%Base)
10202 // # fall through to LoopMBB
10203 MBB = StartMBB;
10204 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
10205 MBB->addSuccessor(LoopMBB);
10206
10207 // LoopMBB:
10208 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
10209 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
10210 // %RotatedNewVal = OP %RotatedOldVal, %Src2
10211 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
10212 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
10213 // JNE LoopMBB
10214 // # fall through to DoneMBB
10215 MBB = LoopMBB;
10216 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
10217 .addReg(OrigVal).addMBB(StartMBB)
10218 .addReg(Dest).addMBB(LoopMBB);
10219 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
10220 .addReg(OldVal).addReg(BitShift).addImm(0);
10221 if (Invert) {
10222 // Perform the operation normally and then invert every bit of the field.
10223 Register Tmp = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10224 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
10225 // XILF with the upper BitSize bits set.
10226 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
10227 .addReg(Tmp).addImm(-1U << (32 - BitSize));
10228 } else if (BinOpcode)
10229 // A simply binary operation.
10230 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
10231 .addReg(RotatedOldVal)
10232 .add(Src2);
10233 else
10234 // Use RISBG to rotate Src2 into position and use it to replace the
10235 // field in RotatedOldVal.
10236 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
10237 .addReg(RotatedOldVal).addReg(Src2.getReg())
10238 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
10239 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
10240 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
10241 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
10242 .addReg(OldVal)
10243 .addReg(NewVal)
10244 .add(Base)
10245 .addImm(Disp);
10246 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10248 MBB->addSuccessor(LoopMBB);
10249 MBB->addSuccessor(DoneMBB);
10250
10251 MI.eraseFromParent();
10252 return DoneMBB;
10253}
10254
10255// Implement EmitInstrWithCustomInserter for subword pseudo
10256// ATOMIC_LOADW_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
10257// instruction that should be used to compare the current field with the
10258// minimum or maximum value. KeepOldMask is the BRC condition-code mask
10259// for when the current field should be kept.
10260MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
10261 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
10262 unsigned KeepOldMask) const {
10263 MachineFunction &MF = *MBB->getParent();
10264 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10265 MachineRegisterInfo &MRI = MF.getRegInfo();
10266
10267 // Extract the operands. Base can be a register or a frame index.
10268 Register Dest = MI.getOperand(0).getReg();
10269 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
10270 int64_t Disp = MI.getOperand(2).getImm();
10271 Register Src2 = MI.getOperand(3).getReg();
10272 Register BitShift = MI.getOperand(4).getReg();
10273 Register NegBitShift = MI.getOperand(5).getReg();
10274 unsigned BitSize = MI.getOperand(6).getImm();
10275 DebugLoc DL = MI.getDebugLoc();
10276
10277 // Get the right opcodes for the displacement.
10278 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
10279 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
10280 assert(LOpcode && CSOpcode && "Displacement out of range");
10281
10282 // Create virtual registers for temporary results.
10283 Register OrigVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10284 Register OldVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10285 Register NewVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10286 Register RotatedOldVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10287 Register RotatedAltVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10288 Register RotatedNewVal = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
10289
10290 // Insert 3 basic blocks for the loop.
10291 MachineBasicBlock *StartMBB = MBB;
10292 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
10293 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB);
10294 MachineBasicBlock *UseAltMBB = SystemZ::emitBlockAfter(LoopMBB);
10295 MachineBasicBlock *UpdateMBB = SystemZ::emitBlockAfter(UseAltMBB);
10296
10297 // StartMBB:
10298 // ...
10299 // %OrigVal = L Disp(%Base)
10300 // # fall through to LoopMBB
10301 MBB = StartMBB;
10302 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
10303 MBB->addSuccessor(LoopMBB);
10304
10305 // LoopMBB:
10306 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
10307 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
10308 // CompareOpcode %RotatedOldVal, %Src2
10309 // BRC KeepOldMask, UpdateMBB
10310 MBB = LoopMBB;
10311 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
10312 .addReg(OrigVal).addMBB(StartMBB)
10313 .addReg(Dest).addMBB(UpdateMBB);
10314 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
10315 .addReg(OldVal).addReg(BitShift).addImm(0);
10316 BuildMI(MBB, DL, TII->get(CompareOpcode))
10317 .addReg(RotatedOldVal).addReg(Src2);
10318 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10319 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
10320 MBB->addSuccessor(UpdateMBB);
10321 MBB->addSuccessor(UseAltMBB);
10322
10323 // UseAltMBB:
10324 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
10325 // # fall through to UpdateMBB
10326 MBB = UseAltMBB;
10327 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
10328 .addReg(RotatedOldVal).addReg(Src2)
10329 .addImm(32).addImm(31 + BitSize).addImm(0);
10330 MBB->addSuccessor(UpdateMBB);
10331
10332 // UpdateMBB:
10333 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
10334 // [ %RotatedAltVal, UseAltMBB ]
10335 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
10336 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
10337 // JNE LoopMBB
10338 // # fall through to DoneMBB
10339 MBB = UpdateMBB;
10340 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
10341 .addReg(RotatedOldVal).addMBB(LoopMBB)
10342 .addReg(RotatedAltVal).addMBB(UseAltMBB);
10343 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
10344 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
10345 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
10346 .addReg(OldVal)
10347 .addReg(NewVal)
10348 .add(Base)
10349 .addImm(Disp);
10350 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10352 MBB->addSuccessor(LoopMBB);
10353 MBB->addSuccessor(DoneMBB);
10354
10355 MI.eraseFromParent();
10356 return DoneMBB;
10357}
10358
10359// Implement EmitInstrWithCustomInserter for subword pseudo ATOMIC_CMP_SWAPW
10360// instruction MI.
10362SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
10363 MachineBasicBlock *MBB) const {
10364 MachineFunction &MF = *MBB->getParent();
10365 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10366 MachineRegisterInfo &MRI = MF.getRegInfo();
10367
10368 // Extract the operands. Base can be a register or a frame index.
10369 Register Dest = MI.getOperand(0).getReg();
10370 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
10371 int64_t Disp = MI.getOperand(2).getImm();
10372 Register CmpVal = MI.getOperand(3).getReg();
10373 Register OrigSwapVal = MI.getOperand(4).getReg();
10374 Register BitShift = MI.getOperand(5).getReg();
10375 Register NegBitShift = MI.getOperand(6).getReg();
10376 int64_t BitSize = MI.getOperand(7).getImm();
10377 DebugLoc DL = MI.getDebugLoc();
10378
10379 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
10380
10381 // Get the right opcodes for the displacement and zero-extension.
10382 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
10383 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
10384 unsigned ZExtOpcode = BitSize == 8 ? SystemZ::LLCR : SystemZ::LLHR;
10385 assert(LOpcode && CSOpcode && "Displacement out of range");
10386
10387 // Create virtual registers for temporary results.
10388 Register OrigOldVal = MRI.createVirtualRegister(RC);
10389 Register OldVal = MRI.createVirtualRegister(RC);
10390 Register SwapVal = MRI.createVirtualRegister(RC);
10391 Register StoreVal = MRI.createVirtualRegister(RC);
10392 Register OldValRot = MRI.createVirtualRegister(RC);
10393 Register RetryOldVal = MRI.createVirtualRegister(RC);
10394 Register RetrySwapVal = MRI.createVirtualRegister(RC);
10395
10396 // Insert 2 basic blocks for the loop.
10397 MachineBasicBlock *StartMBB = MBB;
10398 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
10399 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB);
10400 MachineBasicBlock *SetMBB = SystemZ::emitBlockAfter(LoopMBB);
10401
10402 // StartMBB:
10403 // ...
10404 // %OrigOldVal = L Disp(%Base)
10405 // # fall through to LoopMBB
10406 MBB = StartMBB;
10407 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
10408 .add(Base)
10409 .addImm(Disp)
10410 .addReg(0);
10411 MBB->addSuccessor(LoopMBB);
10412
10413 // LoopMBB:
10414 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
10415 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
10416 // %OldValRot = RLL %OldVal, BitSize(%BitShift)
10417 // ^^ The low BitSize bits contain the field
10418 // of interest.
10419 // %RetrySwapVal = RISBG32 %SwapVal, %OldValRot, 32, 63-BitSize, 0
10420 // ^^ Replace the upper 32-BitSize bits of the
10421 // swap value with those that we loaded and rotated.
10422 // %Dest = LL[CH] %OldValRot
10423 // CR %Dest, %CmpVal
10424 // JNE DoneMBB
10425 // # Fall through to SetMBB
10426 MBB = LoopMBB;
10427 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
10428 .addReg(OrigOldVal).addMBB(StartMBB)
10429 .addReg(RetryOldVal).addMBB(SetMBB);
10430 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
10431 .addReg(OrigSwapVal).addMBB(StartMBB)
10432 .addReg(RetrySwapVal).addMBB(SetMBB);
10433 BuildMI(MBB, DL, TII->get(SystemZ::RLL), OldValRot)
10434 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
10435 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
10436 .addReg(SwapVal).addReg(OldValRot).addImm(32).addImm(63 - BitSize).addImm(0);
10437 BuildMI(MBB, DL, TII->get(ZExtOpcode), Dest)
10438 .addReg(OldValRot);
10439 BuildMI(MBB, DL, TII->get(SystemZ::CR))
10440 .addReg(Dest).addReg(CmpVal);
10441 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10444 MBB->addSuccessor(DoneMBB);
10445 MBB->addSuccessor(SetMBB);
10446
10447 // SetMBB:
10448 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
10449 // ^^ Rotate the new field to its proper position.
10450 // %RetryOldVal = CS %OldVal, %StoreVal, Disp(%Base)
10451 // JNE LoopMBB
10452 // # fall through to ExitMBB
10453 MBB = SetMBB;
10454 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
10455 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
10456 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
10457 .addReg(OldVal)
10458 .addReg(StoreVal)
10459 .add(Base)
10460 .addImm(Disp);
10461 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10463 MBB->addSuccessor(LoopMBB);
10464 MBB->addSuccessor(DoneMBB);
10465
10466 // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in
10467 // to the block after the loop. At this point, CC may have been defined
10468 // either by the CR in LoopMBB or by the CS in SetMBB.
10469 if (!MI.registerDefIsDead(SystemZ::CC, /*TRI=*/nullptr))
10470 DoneMBB->addLiveIn(SystemZ::CC);
10471
10472 MI.eraseFromParent();
10473 return DoneMBB;
10474}
10475
10476// Emit a move from two GR64s to a GR128.
10478SystemZTargetLowering::emitPair128(MachineInstr &MI,
10479 MachineBasicBlock *MBB) const {
10480 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10481 const DebugLoc &DL = MI.getDebugLoc();
10482
10483 Register Dest = MI.getOperand(0).getReg();
10484 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest)
10485 .add(MI.getOperand(1))
10486 .addImm(SystemZ::subreg_h64)
10487 .add(MI.getOperand(2))
10488 .addImm(SystemZ::subreg_l64);
10489 MI.eraseFromParent();
10490 return MBB;
10491}
10492
10493// Emit an extension from a GR64 to a GR128. ClearEven is true
10494// if the high register of the GR128 value must be cleared or false if
10495// it's "don't care".
10496MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
10498 bool ClearEven) const {
10499 MachineFunction &MF = *MBB->getParent();
10500 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10501 MachineRegisterInfo &MRI = MF.getRegInfo();
10502 DebugLoc DL = MI.getDebugLoc();
10503
10504 Register Dest = MI.getOperand(0).getReg();
10505 Register Src = MI.getOperand(1).getReg();
10506 Register In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
10507
10508 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
10509 if (ClearEven) {
10510 Register NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
10511 Register Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
10512
10513 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
10514 .addImm(0);
10515 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
10516 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
10517 In128 = NewIn128;
10518 }
10519 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
10520 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
10521
10522 MI.eraseFromParent();
10523 return MBB;
10524}
10525
10527SystemZTargetLowering::emitMemMemWrapper(MachineInstr &MI,
10529 unsigned Opcode, bool IsMemset) const {
10530 MachineFunction &MF = *MBB->getParent();
10531 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10532 MachineRegisterInfo &MRI = MF.getRegInfo();
10533 DebugLoc DL = MI.getDebugLoc();
10534
10535 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
10536 uint64_t DestDisp = MI.getOperand(1).getImm();
10537 MachineOperand SrcBase = MachineOperand::CreateReg(0U, false);
10538 uint64_t SrcDisp;
10539
10540 // Fold the displacement Disp if it is out of range.
10541 auto foldDisplIfNeeded = [&](MachineOperand &Base, uint64_t &Disp) -> void {
10542 if (!isUInt<12>(Disp)) {
10543 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
10544 unsigned Opcode = TII->getOpcodeForOffset(SystemZ::LA, Disp);
10545 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode), Reg)
10546 .add(Base).addImm(Disp).addReg(0);
10548 Disp = 0;
10549 }
10550 };
10551
10552 if (!IsMemset) {
10553 SrcBase = earlyUseOperand(MI.getOperand(2));
10554 SrcDisp = MI.getOperand(3).getImm();
10555 } else {
10556 SrcBase = DestBase;
10557 SrcDisp = DestDisp++;
10558 foldDisplIfNeeded(DestBase, DestDisp);
10559 }
10560
10561 MachineOperand &LengthMO = MI.getOperand(IsMemset ? 2 : 4);
10562 bool IsImmForm = LengthMO.isImm();
10563 bool IsRegForm = !IsImmForm;
10564
10565 // Build and insert one Opcode of Length, with special treatment for memset.
10566 auto insertMemMemOp = [&](MachineBasicBlock *InsMBB,
10568 MachineOperand DBase, uint64_t DDisp,
10569 MachineOperand SBase, uint64_t SDisp,
10570 unsigned Length) -> void {
10571 assert(Length > 0 && Length <= 256 && "Building memory op with bad length.");
10572 if (IsMemset) {
10573 MachineOperand ByteMO = earlyUseOperand(MI.getOperand(3));
10574 if (ByteMO.isImm())
10575 BuildMI(*InsMBB, InsPos, DL, TII->get(SystemZ::MVI))
10576 .add(SBase).addImm(SDisp).add(ByteMO);
10577 else
10578 BuildMI(*InsMBB, InsPos, DL, TII->get(SystemZ::STC))
10579 .add(ByteMO).add(SBase).addImm(SDisp).addReg(0);
10580 if (--Length == 0)
10581 return;
10582 }
10583 BuildMI(*MBB, InsPos, DL, TII->get(Opcode))
10584 .add(DBase).addImm(DDisp).addImm(Length)
10585 .add(SBase).addImm(SDisp)
10586 .setMemRefs(MI.memoperands());
10587 };
10588
10589 bool NeedsLoop = false;
10590 uint64_t ImmLength = 0;
10591 Register LenAdjReg = SystemZ::NoRegister;
10592 if (IsImmForm) {
10593 ImmLength = LengthMO.getImm();
10594 ImmLength += IsMemset ? 2 : 1; // Add back the subtracted adjustment.
10595 if (ImmLength == 0) {
10596 MI.eraseFromParent();
10597 return MBB;
10598 }
10599 if (Opcode == SystemZ::CLC) {
10600 if (ImmLength > 3 * 256)
10601 // A two-CLC sequence is a clear win over a loop, not least because
10602 // it needs only one branch. A three-CLC sequence needs the same
10603 // number of branches as a loop (i.e. 2), but is shorter. That
10604 // brings us to lengths greater than 768 bytes. It seems relatively
10605 // likely that a difference will be found within the first 768 bytes,
10606 // so we just optimize for the smallest number of branch
10607 // instructions, in order to avoid polluting the prediction buffer
10608 // too much.
10609 NeedsLoop = true;
10610 } else if (ImmLength > 6 * 256)
10611 // The heuristic we use is to prefer loops for anything that would
10612 // require 7 or more MVCs. With these kinds of sizes there isn't much
10613 // to choose between straight-line code and looping code, since the
10614 // time will be dominated by the MVCs themselves.
10615 NeedsLoop = true;
10616 } else {
10617 NeedsLoop = true;
10618 LenAdjReg = LengthMO.getReg();
10619 }
10620
10621 // When generating more than one CLC, all but the last will need to
10622 // branch to the end when a difference is found.
10623 MachineBasicBlock *EndMBB =
10624 (Opcode == SystemZ::CLC && (ImmLength > 256 || NeedsLoop)
10626 : nullptr);
10627
10628 if (NeedsLoop) {
10629 Register StartCountReg =
10630 MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
10631 if (IsImmForm) {
10632 TII->loadImmediate(*MBB, MI, StartCountReg, ImmLength / 256);
10633 ImmLength &= 255;
10634 } else {
10635 BuildMI(*MBB, MI, DL, TII->get(SystemZ::SRLG), StartCountReg)
10636 .addReg(LenAdjReg)
10637 .addReg(0)
10638 .addImm(8);
10639 }
10640
10641 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
10642 auto loadZeroAddress = [&]() -> MachineOperand {
10643 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
10644 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LGHI), Reg).addImm(0);
10645 return MachineOperand::CreateReg(Reg, false);
10646 };
10647 if (DestBase.isReg() && DestBase.getReg() == SystemZ::NoRegister)
10648 DestBase = loadZeroAddress();
10649 if (SrcBase.isReg() && SrcBase.getReg() == SystemZ::NoRegister)
10650 SrcBase = HaveSingleBase ? DestBase : loadZeroAddress();
10651
10652 MachineBasicBlock *StartMBB = nullptr;
10653 MachineBasicBlock *LoopMBB = nullptr;
10654 MachineBasicBlock *NextMBB = nullptr;
10655 MachineBasicBlock *DoneMBB = nullptr;
10656 MachineBasicBlock *AllDoneMBB = nullptr;
10657
10658 Register StartSrcReg = forceReg(MI, SrcBase, TII);
10659 Register StartDestReg =
10660 (HaveSingleBase ? StartSrcReg : forceReg(MI, DestBase, TII));
10661
10662 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
10663 Register ThisSrcReg = MRI.createVirtualRegister(RC);
10664 Register ThisDestReg =
10665 (HaveSingleBase ? ThisSrcReg : MRI.createVirtualRegister(RC));
10666 Register NextSrcReg = MRI.createVirtualRegister(RC);
10667 Register NextDestReg =
10668 (HaveSingleBase ? NextSrcReg : MRI.createVirtualRegister(RC));
10669 RC = &SystemZ::GR64BitRegClass;
10670 Register ThisCountReg = MRI.createVirtualRegister(RC);
10671 Register NextCountReg = MRI.createVirtualRegister(RC);
10672
10673 if (IsRegForm) {
10674 AllDoneMBB = SystemZ::splitBlockBefore(MI, MBB);
10675 StartMBB = SystemZ::emitBlockAfter(MBB);
10676 LoopMBB = SystemZ::emitBlockAfter(StartMBB);
10677 NextMBB = (EndMBB ? SystemZ::emitBlockAfter(LoopMBB) : LoopMBB);
10678 DoneMBB = SystemZ::emitBlockAfter(NextMBB);
10679
10680 // MBB:
10681 // # Jump to AllDoneMBB if LenAdjReg means 0, or fall thru to StartMBB.
10682 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
10683 .addReg(LenAdjReg).addImm(IsMemset ? -2 : -1);
10684 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10686 .addMBB(AllDoneMBB);
10687 MBB->addSuccessor(AllDoneMBB);
10688 if (!IsMemset)
10689 MBB->addSuccessor(StartMBB);
10690 else {
10691 // MemsetOneCheckMBB:
10692 // # Jump to MemsetOneMBB for a memset of length 1, or
10693 // # fall thru to StartMBB.
10694 MachineBasicBlock *MemsetOneCheckMBB = SystemZ::emitBlockAfter(MBB);
10695 MachineBasicBlock *MemsetOneMBB = SystemZ::emitBlockAfter(&*MF.rbegin());
10696 MBB->addSuccessor(MemsetOneCheckMBB);
10697 MBB = MemsetOneCheckMBB;
10698 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
10699 .addReg(LenAdjReg).addImm(-1);
10700 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10702 .addMBB(MemsetOneMBB);
10703 MBB->addSuccessor(MemsetOneMBB, {10, 100});
10704 MBB->addSuccessor(StartMBB, {90, 100});
10705
10706 // MemsetOneMBB:
10707 // # Jump back to AllDoneMBB after a single MVI or STC.
10708 MBB = MemsetOneMBB;
10709 insertMemMemOp(MBB, MBB->end(),
10710 MachineOperand::CreateReg(StartDestReg, false), DestDisp,
10711 MachineOperand::CreateReg(StartSrcReg, false), SrcDisp,
10712 1);
10713 BuildMI(MBB, DL, TII->get(SystemZ::J)).addMBB(AllDoneMBB);
10714 MBB->addSuccessor(AllDoneMBB);
10715 }
10716
10717 // StartMBB:
10718 // # Jump to DoneMBB if %StartCountReg is zero, or fall through to LoopMBB.
10719 MBB = StartMBB;
10720 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
10721 .addReg(StartCountReg).addImm(0);
10722 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10724 .addMBB(DoneMBB);
10725 MBB->addSuccessor(DoneMBB);
10726 MBB->addSuccessor(LoopMBB);
10727 }
10728 else {
10729 StartMBB = MBB;
10730 DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
10731 LoopMBB = SystemZ::emitBlockAfter(StartMBB);
10732 NextMBB = (EndMBB ? SystemZ::emitBlockAfter(LoopMBB) : LoopMBB);
10733
10734 // StartMBB:
10735 // # fall through to LoopMBB
10736 MBB->addSuccessor(LoopMBB);
10737
10738 DestBase = MachineOperand::CreateReg(NextDestReg, false);
10739 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
10740 if (EndMBB && !ImmLength)
10741 // If the loop handled the whole CLC range, DoneMBB will be empty with
10742 // CC live-through into EndMBB, so add it as live-in.
10743 DoneMBB->addLiveIn(SystemZ::CC);
10744 }
10745
10746 // LoopMBB:
10747 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
10748 // [ %NextDestReg, NextMBB ]
10749 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
10750 // [ %NextSrcReg, NextMBB ]
10751 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
10752 // [ %NextCountReg, NextMBB ]
10753 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
10754 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
10755 // ( JLH EndMBB )
10756 //
10757 // The prefetch is used only for MVC. The JLH is used only for CLC.
10758 MBB = LoopMBB;
10759 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
10760 .addReg(StartDestReg).addMBB(StartMBB)
10761 .addReg(NextDestReg).addMBB(NextMBB);
10762 if (!HaveSingleBase)
10763 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
10764 .addReg(StartSrcReg).addMBB(StartMBB)
10765 .addReg(NextSrcReg).addMBB(NextMBB);
10766 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
10767 .addReg(StartCountReg).addMBB(StartMBB)
10768 .addReg(NextCountReg).addMBB(NextMBB);
10769 if (Opcode == SystemZ::MVC)
10770 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
10772 .addReg(ThisDestReg).addImm(DestDisp - IsMemset + 768).addReg(0);
10773 insertMemMemOp(MBB, MBB->end(),
10774 MachineOperand::CreateReg(ThisDestReg, false), DestDisp,
10775 MachineOperand::CreateReg(ThisSrcReg, false), SrcDisp, 256);
10776 if (EndMBB) {
10777 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10779 .addMBB(EndMBB);
10780 MBB->addSuccessor(EndMBB);
10781 MBB->addSuccessor(NextMBB);
10782 }
10783
10784 // NextMBB:
10785 // %NextDestReg = LA 256(%ThisDestReg)
10786 // %NextSrcReg = LA 256(%ThisSrcReg)
10787 // %NextCountReg = AGHI %ThisCountReg, -1
10788 // CGHI %NextCountReg, 0
10789 // JLH LoopMBB
10790 // # fall through to DoneMBB
10791 //
10792 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
10793 MBB = NextMBB;
10794 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
10795 .addReg(ThisDestReg).addImm(256).addReg(0);
10796 if (!HaveSingleBase)
10797 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
10798 .addReg(ThisSrcReg).addImm(256).addReg(0);
10799 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
10800 .addReg(ThisCountReg).addImm(-1);
10801 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
10802 .addReg(NextCountReg).addImm(0);
10803 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10805 .addMBB(LoopMBB);
10806 MBB->addSuccessor(LoopMBB);
10807 MBB->addSuccessor(DoneMBB);
10808
10809 MBB = DoneMBB;
10810 if (IsRegForm) {
10811 // DoneMBB:
10812 // # Make PHIs for RemDestReg/RemSrcReg as the loop may or may not run.
10813 // # Use EXecute Relative Long for the remainder of the bytes. The target
10814 // instruction of the EXRL will have a length field of 1 since 0 is an
10815 // illegal value. The number of bytes processed becomes (%LenAdjReg &
10816 // 0xff) + 1.
10817 // # Fall through to AllDoneMBB.
10818 Register RemSrcReg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
10819 Register RemDestReg = HaveSingleBase ? RemSrcReg
10820 : MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
10821 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RemDestReg)
10822 .addReg(StartDestReg).addMBB(StartMBB)
10823 .addReg(NextDestReg).addMBB(NextMBB);
10824 if (!HaveSingleBase)
10825 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RemSrcReg)
10826 .addReg(StartSrcReg).addMBB(StartMBB)
10827 .addReg(NextSrcReg).addMBB(NextMBB);
10828 if (IsMemset)
10829 insertMemMemOp(MBB, MBB->end(),
10830 MachineOperand::CreateReg(RemDestReg, false), DestDisp,
10831 MachineOperand::CreateReg(RemSrcReg, false), SrcDisp, 1);
10832 MachineInstrBuilder EXRL_MIB =
10833 BuildMI(MBB, DL, TII->get(SystemZ::EXRL_Pseudo))
10834 .addImm(Opcode)
10835 .addReg(LenAdjReg)
10836 .addReg(RemDestReg).addImm(DestDisp)
10837 .addReg(RemSrcReg).addImm(SrcDisp);
10838 MBB->addSuccessor(AllDoneMBB);
10839 MBB = AllDoneMBB;
10840 if (Opcode != SystemZ::MVC) {
10841 EXRL_MIB.addReg(SystemZ::CC, RegState::ImplicitDefine);
10842 if (EndMBB)
10843 MBB->addLiveIn(SystemZ::CC);
10844 }
10845 }
10846 MF.getProperties().resetNoPHIs();
10847 }
10848
10849 // Handle any remaining bytes with straight-line code.
10850 while (ImmLength > 0) {
10851 uint64_t ThisLength = std::min(ImmLength, uint64_t(256));
10852 // The previous iteration might have created out-of-range displacements.
10853 // Apply them using LA/LAY if so.
10854 foldDisplIfNeeded(DestBase, DestDisp);
10855 foldDisplIfNeeded(SrcBase, SrcDisp);
10856 insertMemMemOp(MBB, MI, DestBase, DestDisp, SrcBase, SrcDisp, ThisLength);
10857 DestDisp += ThisLength;
10858 SrcDisp += ThisLength;
10859 ImmLength -= ThisLength;
10860 // If there's another CLC to go, branch to the end if a difference
10861 // was found.
10862 if (EndMBB && ImmLength > 0) {
10863 MachineBasicBlock *NextMBB = SystemZ::splitBlockBefore(MI, MBB);
10864 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10866 .addMBB(EndMBB);
10867 MBB->addSuccessor(EndMBB);
10868 MBB->addSuccessor(NextMBB);
10869 MBB = NextMBB;
10870 }
10871 }
10872 if (EndMBB) {
10873 MBB->addSuccessor(EndMBB);
10874 MBB = EndMBB;
10875 MBB->addLiveIn(SystemZ::CC);
10876 }
10877
10878 MI.eraseFromParent();
10879 return MBB;
10880}
10881
10883SystemZTargetLowering::emitMemmoveImm(MachineInstr &MI,
10884 MachineBasicBlock *MBB) const {
10885 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10886
10887 DebugLoc DL = MI.getDebugLoc();
10888 MachineOperand DstAddr = earlyUseOperand(MI.getOperand(0));
10889 MachineOperand SrcAddr = earlyUseOperand(MI.getOperand(1));
10890 uint64_t Len = MI.getOperand(2).getImm();
10891 assert(Len > 0 && Len <= 256 && "Memmove of of unsupported constant length.");
10892
10893 // Use MVC or MVCRL after comparing the addresses.
10894 MachineBasicBlock *DoneMBB = SystemZ::splitBlockAfter(MI, MBB);
10895 MachineBasicBlock *MvcMBB = SystemZ::emitBlockAfter(MBB);
10896 MachineBasicBlock *MvcrlMBB = SystemZ::emitBlockAfter(MvcMBB);
10897 MBB->addSuccessor(MvcMBB);
10898 MBB->addSuccessor(MvcrlMBB);
10899 MvcMBB->addSuccessor(DoneMBB);
10900 MvcrlMBB->addSuccessor(DoneMBB);
10901
10902 BuildMI(MBB, DL, TII->get(SystemZ::CLGR)).add(SrcAddr).add(DstAddr);
10903 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10905 .addMBB(MvcrlMBB);
10906
10907 BuildMI(MvcMBB, DL, TII->get(SystemZ::MVC))
10908 .add(DstAddr).addImm(0)
10909 .addImm(Len)
10910 .add(SrcAddr).addImm(0)
10911 .setMemRefs(MI.memoperands());
10912 BuildMI(MvcMBB, DL, TII->get(SystemZ::J)).addMBB(DoneMBB);
10913
10914 BuildMI(MvcrlMBB, DL, TII->get(SystemZ::LHI), SystemZ::R0L).addImm(Len - 1);
10915 BuildMI(MvcrlMBB, DL, TII->get(SystemZ::MVCRL))
10916 .add(DstAddr).addImm(0)
10917 .add(SrcAddr).addImm(0)
10918 .setMemRefs(MI.memoperands());
10919
10920 MI.eraseFromParent();
10921 return DoneMBB;
10922}
10923
10924// Decompose string pseudo-instruction MI into a loop that continually performs
10925// Opcode until CC != 3.
10926MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
10927 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
10928 MachineFunction &MF = *MBB->getParent();
10929 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10930 MachineRegisterInfo &MRI = MF.getRegInfo();
10931 DebugLoc DL = MI.getDebugLoc();
10932
10933 uint64_t End1Reg = MI.getOperand(0).getReg();
10934 uint64_t Start1Reg = MI.getOperand(1).getReg();
10935 uint64_t Start2Reg = MI.getOperand(2).getReg();
10936 uint64_t CharReg = MI.getOperand(3).getReg();
10937
10938 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
10939 uint64_t This1Reg = MRI.createVirtualRegister(RC);
10940 uint64_t This2Reg = MRI.createVirtualRegister(RC);
10941 uint64_t End2Reg = MRI.createVirtualRegister(RC);
10942
10943 MachineBasicBlock *StartMBB = MBB;
10944 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
10945 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB);
10946
10947 // StartMBB:
10948 // # fall through to LoopMBB
10949 MBB->addSuccessor(LoopMBB);
10950
10951 // LoopMBB:
10952 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
10953 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
10954 // R0L = %CharReg
10955 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
10956 // JO LoopMBB
10957 // # fall through to DoneMBB
10958 //
10959 // The load of R0L can be hoisted by post-RA LICM.
10960 MBB = LoopMBB;
10961
10962 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
10963 .addReg(Start1Reg).addMBB(StartMBB)
10964 .addReg(End1Reg).addMBB(LoopMBB);
10965 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
10966 .addReg(Start2Reg).addMBB(StartMBB)
10967 .addReg(End2Reg).addMBB(LoopMBB);
10968 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
10969 BuildMI(MBB, DL, TII->get(Opcode))
10970 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
10971 .addReg(This1Reg).addReg(This2Reg);
10972 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
10974 MBB->addSuccessor(LoopMBB);
10975 MBB->addSuccessor(DoneMBB);
10976
10977 DoneMBB->addLiveIn(SystemZ::CC);
10978
10979 MI.eraseFromParent();
10980 return DoneMBB;
10981}
10982
10983// Update TBEGIN instruction with final opcode and register clobbers.
10984MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
10985 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
10986 bool NoFloat) const {
10987 MachineFunction &MF = *MBB->getParent();
10988 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
10989 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
10990
10991 // Update opcode.
10992 MI.setDesc(TII->get(Opcode));
10993
10994 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
10995 // Make sure to add the corresponding GRSM bits if they are missing.
10996 uint64_t Control = MI.getOperand(2).getImm();
10997 static const unsigned GPRControlBit[16] = {
10998 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
10999 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
11000 };
11001 Control |= GPRControlBit[15];
11002 if (TFI->hasFP(MF))
11003 Control |= GPRControlBit[11];
11004 MI.getOperand(2).setImm(Control);
11005
11006 // Add GPR clobbers.
11007 for (int I = 0; I < 16; I++) {
11008 if ((Control & GPRControlBit[I]) == 0) {
11009 unsigned Reg = SystemZMC::GR64Regs[I];
11010 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
11011 }
11012 }
11013
11014 // Add FPR/VR clobbers.
11015 if (!NoFloat && (Control & 4) != 0) {
11016 if (Subtarget.hasVector()) {
11017 for (unsigned Reg : SystemZMC::VR128Regs) {
11018 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
11019 }
11020 } else {
11021 for (unsigned Reg : SystemZMC::FP64Regs) {
11022 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
11023 }
11024 }
11025 }
11026
11027 return MBB;
11028}
11029
11030MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
11031 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
11032 MachineFunction &MF = *MBB->getParent();
11033 MachineRegisterInfo *MRI = &MF.getRegInfo();
11034 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
11035 DebugLoc DL = MI.getDebugLoc();
11036
11037 Register SrcReg = MI.getOperand(0).getReg();
11038
11039 // Create new virtual register of the same class as source.
11040 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
11041 Register DstReg = MRI->createVirtualRegister(RC);
11042
11043 // Replace pseudo with a normal load-and-test that models the def as
11044 // well.
11045 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
11046 .addReg(SrcReg)
11047 .setMIFlags(MI.getFlags());
11048 MI.eraseFromParent();
11049
11050 return MBB;
11051}
11052
11053MachineBasicBlock *SystemZTargetLowering::emitProbedAlloca(
11055 MachineFunction &MF = *MBB->getParent();
11056 MachineRegisterInfo *MRI = &MF.getRegInfo();
11057 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
11058 DebugLoc DL = MI.getDebugLoc();
11059 const unsigned ProbeSize = getStackProbeSize(MF);
11060 Register DstReg = MI.getOperand(0).getReg();
11061 Register SizeReg = MI.getOperand(2).getReg();
11062
11063 MachineBasicBlock *StartMBB = MBB;
11064 MachineBasicBlock *DoneMBB = SystemZ::splitBlockAfter(MI, MBB);
11065 MachineBasicBlock *LoopTestMBB = SystemZ::emitBlockAfter(StartMBB);
11066 MachineBasicBlock *LoopBodyMBB = SystemZ::emitBlockAfter(LoopTestMBB);
11067 MachineBasicBlock *TailTestMBB = SystemZ::emitBlockAfter(LoopBodyMBB);
11068 MachineBasicBlock *TailMBB = SystemZ::emitBlockAfter(TailTestMBB);
11069
11070 MachineMemOperand *VolLdMMO = MF.getMachineMemOperand(MachinePointerInfo(),
11072
11073 Register PHIReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass);
11074 Register IncReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass);
11075
11076 // LoopTestMBB
11077 // BRC TailTestMBB
11078 // # fallthrough to LoopBodyMBB
11079 StartMBB->addSuccessor(LoopTestMBB);
11080 MBB = LoopTestMBB;
11081 BuildMI(MBB, DL, TII->get(SystemZ::PHI), PHIReg)
11082 .addReg(SizeReg)
11083 .addMBB(StartMBB)
11084 .addReg(IncReg)
11085 .addMBB(LoopBodyMBB);
11086 BuildMI(MBB, DL, TII->get(SystemZ::CLGFI))
11087 .addReg(PHIReg)
11088 .addImm(ProbeSize);
11089 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
11091 .addMBB(TailTestMBB);
11092 MBB->addSuccessor(LoopBodyMBB);
11093 MBB->addSuccessor(TailTestMBB);
11094
11095 // LoopBodyMBB: Allocate and probe by means of a volatile compare.
11096 // J LoopTestMBB
11097 MBB = LoopBodyMBB;
11098 BuildMI(MBB, DL, TII->get(SystemZ::SLGFI), IncReg)
11099 .addReg(PHIReg)
11100 .addImm(ProbeSize);
11101 BuildMI(MBB, DL, TII->get(SystemZ::SLGFI), SystemZ::R15D)
11102 .addReg(SystemZ::R15D)
11103 .addImm(ProbeSize);
11104 BuildMI(MBB, DL, TII->get(SystemZ::CG)).addReg(SystemZ::R15D)
11105 .addReg(SystemZ::R15D).addImm(ProbeSize - 8).addReg(0)
11106 .setMemRefs(VolLdMMO);
11107 BuildMI(MBB, DL, TII->get(SystemZ::J)).addMBB(LoopTestMBB);
11108 MBB->addSuccessor(LoopTestMBB);
11109
11110 // TailTestMBB
11111 // BRC DoneMBB
11112 // # fallthrough to TailMBB
11113 MBB = TailTestMBB;
11114 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
11115 .addReg(PHIReg)
11116 .addImm(0);
11117 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
11119 .addMBB(DoneMBB);
11120 MBB->addSuccessor(TailMBB);
11121 MBB->addSuccessor(DoneMBB);
11122
11123 // TailMBB
11124 // # fallthrough to DoneMBB
11125 MBB = TailMBB;
11126 BuildMI(MBB, DL, TII->get(SystemZ::SLGR), SystemZ::R15D)
11127 .addReg(SystemZ::R15D)
11128 .addReg(PHIReg);
11129 BuildMI(MBB, DL, TII->get(SystemZ::CG)).addReg(SystemZ::R15D)
11130 .addReg(SystemZ::R15D).addImm(-8).addReg(PHIReg)
11131 .setMemRefs(VolLdMMO);
11132 MBB->addSuccessor(DoneMBB);
11133
11134 // DoneMBB
11135 MBB = DoneMBB;
11136 BuildMI(*MBB, MBB->begin(), DL, TII->get(TargetOpcode::COPY), DstReg)
11137 .addReg(SystemZ::R15D);
11138
11139 MI.eraseFromParent();
11140 return DoneMBB;
11141}
11142
11143SDValue SystemZTargetLowering::
11144getBackchainAddress(SDValue SP, SelectionDAG &DAG) const {
11145 MachineFunction &MF = DAG.getMachineFunction();
11146 auto *TFL = Subtarget.getFrameLowering<SystemZELFFrameLowering>();
11147 SDLoc DL(SP);
11148 return DAG.getNode(ISD::ADD, DL, MVT::i64, SP,
11149 DAG.getIntPtrConstant(TFL->getBackchainOffset(MF), DL));
11150}
11151
11152// Replace a _STACKGUARD_DAG pseudo with a _STACKGUARD pseudo, adding
11153// a dead early-clobber def reg that will be used as a scratch register
11154// when the pseudo is expanded.
11155MachineBasicBlock *SystemZTargetLowering::emitStackGuardPseudo(
11156 MachineInstr &MI, MachineBasicBlock *MBB, unsigned PseudoOp) const {
11157 MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo();
11158 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
11159 DebugLoc DL = MI.getDebugLoc();
11160 Register AddrReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass);
11161 BuildMI(*MBB, MI, DL, TII->get(PseudoOp), AddrReg)
11162 .addFrameIndex(MI.getOperand(0).getIndex())
11163 .addImm(MI.getOperand(1).getImm());
11164 MI.eraseFromParent();
11165 return MBB;
11166}
11167
11170 switch (MI.getOpcode()) {
11171 case SystemZ::ADJCALLSTACKDOWN:
11172 case SystemZ::ADJCALLSTACKUP:
11173 return emitAdjCallStack(MI, MBB);
11174
11175 case SystemZ::Select32:
11176 case SystemZ::Select64:
11177 case SystemZ::Select128:
11178 case SystemZ::SelectF32:
11179 case SystemZ::SelectF64:
11180 case SystemZ::SelectF128:
11181 case SystemZ::SelectVR32:
11182 case SystemZ::SelectVR64:
11183 case SystemZ::SelectVR128:
11184 return emitSelect(MI, MBB);
11185
11186 case SystemZ::CondStore8Mux:
11187 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
11188 case SystemZ::CondStore8MuxInv:
11189 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
11190 case SystemZ::CondStore16Mux:
11191 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
11192 case SystemZ::CondStore16MuxInv:
11193 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
11194 case SystemZ::CondStore32Mux:
11195 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
11196 case SystemZ::CondStore32MuxInv:
11197 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
11198 case SystemZ::CondStore8:
11199 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
11200 case SystemZ::CondStore8Inv:
11201 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
11202 case SystemZ::CondStore16:
11203 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
11204 case SystemZ::CondStore16Inv:
11205 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
11206 case SystemZ::CondStore32:
11207 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
11208 case SystemZ::CondStore32Inv:
11209 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
11210 case SystemZ::CondStore64:
11211 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
11212 case SystemZ::CondStore64Inv:
11213 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
11214 case SystemZ::CondStoreF32:
11215 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
11216 case SystemZ::CondStoreF32Inv:
11217 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
11218 case SystemZ::CondStoreF64:
11219 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
11220 case SystemZ::CondStoreF64Inv:
11221 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
11222
11223 case SystemZ::SCmp128Hi:
11224 return emitICmp128Hi(MI, MBB, false);
11225 case SystemZ::UCmp128Hi:
11226 return emitICmp128Hi(MI, MBB, true);
11227
11228 case SystemZ::PAIR128:
11229 return emitPair128(MI, MBB);
11230 case SystemZ::AEXT128:
11231 return emitExt128(MI, MBB, false);
11232 case SystemZ::ZEXT128:
11233 return emitExt128(MI, MBB, true);
11234
11235 case SystemZ::ATOMIC_SWAPW:
11236 return emitAtomicLoadBinary(MI, MBB, 0);
11237
11238 case SystemZ::ATOMIC_LOADW_AR:
11239 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR);
11240 case SystemZ::ATOMIC_LOADW_AFI:
11241 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI);
11242
11243 case SystemZ::ATOMIC_LOADW_SR:
11244 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR);
11245
11246 case SystemZ::ATOMIC_LOADW_NR:
11247 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR);
11248 case SystemZ::ATOMIC_LOADW_NILH:
11249 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH);
11250
11251 case SystemZ::ATOMIC_LOADW_OR:
11252 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR);
11253 case SystemZ::ATOMIC_LOADW_OILH:
11254 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH);
11255
11256 case SystemZ::ATOMIC_LOADW_XR:
11257 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR);
11258 case SystemZ::ATOMIC_LOADW_XILF:
11259 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF);
11260
11261 case SystemZ::ATOMIC_LOADW_NRi:
11262 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, true);
11263 case SystemZ::ATOMIC_LOADW_NILHi:
11264 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, true);
11265
11266 case SystemZ::ATOMIC_LOADW_MIN:
11267 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, SystemZ::CCMASK_CMP_LE);
11268 case SystemZ::ATOMIC_LOADW_MAX:
11269 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, SystemZ::CCMASK_CMP_GE);
11270 case SystemZ::ATOMIC_LOADW_UMIN:
11271 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, SystemZ::CCMASK_CMP_LE);
11272 case SystemZ::ATOMIC_LOADW_UMAX:
11273 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, SystemZ::CCMASK_CMP_GE);
11274
11275 case SystemZ::ATOMIC_CMP_SWAPW:
11276 return emitAtomicCmpSwapW(MI, MBB);
11277 case SystemZ::MVCImm:
11278 case SystemZ::MVCReg:
11279 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
11280 case SystemZ::NCImm:
11281 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
11282 case SystemZ::OCImm:
11283 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
11284 case SystemZ::XCImm:
11285 case SystemZ::XCReg:
11286 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
11287 case SystemZ::CLCImm:
11288 case SystemZ::CLCReg:
11289 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
11290 case SystemZ::MemsetImmImm:
11291 case SystemZ::MemsetImmReg:
11292 case SystemZ::MemsetRegImm:
11293 case SystemZ::MemsetRegReg:
11294 return emitMemMemWrapper(MI, MBB, SystemZ::MVC, true/*IsMemset*/);
11295 case SystemZ::MemmoveImm:
11296 return emitMemmoveImm(MI, MBB);
11297 case SystemZ::CLSTLoop:
11298 return emitStringWrapper(MI, MBB, SystemZ::CLST);
11299 case SystemZ::MVSTLoop:
11300 return emitStringWrapper(MI, MBB, SystemZ::MVST);
11301 case SystemZ::SRSTLoop:
11302 return emitStringWrapper(MI, MBB, SystemZ::SRST);
11303 case SystemZ::TBEGIN:
11304 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
11305 case SystemZ::TBEGIN_nofloat:
11306 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
11307 case SystemZ::TBEGINC:
11308 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
11309 case SystemZ::LTEBRCompare_Pseudo:
11310 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
11311 case SystemZ::LTDBRCompare_Pseudo:
11312 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
11313 case SystemZ::LTXBRCompare_Pseudo:
11314 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
11315
11316 case SystemZ::PROBED_ALLOCA:
11317 return emitProbedAlloca(MI, MBB);
11318 case SystemZ::EH_SjLj_SetJmp:
11319 return emitEHSjLjSetJmp(MI, MBB);
11320 case SystemZ::EH_SjLj_LongJmp:
11321 return emitEHSjLjLongJmp(MI, MBB);
11322
11323 case TargetOpcode::STACKMAP:
11324 case TargetOpcode::PATCHPOINT:
11325 return emitPatchPoint(MI, MBB);
11326
11327 case SystemZ::MOV_STACKGUARD_DAG:
11328 return emitStackGuardPseudo(MI, MBB, SystemZ::MOV_STACKGUARD);
11329
11330 case SystemZ::CMP_STACKGUARD_DAG:
11331 return emitStackGuardPseudo(MI, MBB, SystemZ::CMP_STACKGUARD);
11332
11333 default:
11334 llvm_unreachable("Unexpected instr type to insert");
11335 }
11336}
11337
11338// This is only used by the isel schedulers, and is needed only to prevent
11339// compiler from crashing when list-ilp is used.
11340const TargetRegisterClass *
11341SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
11342 if (VT == MVT::Untyped)
11343 return &SystemZ::ADDR128BitRegClass;
11345}
11346
11347SDValue SystemZTargetLowering::lowerGET_ROUNDING(SDValue Op,
11348 SelectionDAG &DAG) const {
11349 SDLoc dl(Op);
11350 /*
11351 The rounding method is in FPC Byte 3 bits 6-7, and has the following
11352 settings:
11353 00 Round to nearest
11354 01 Round to 0
11355 10 Round to +inf
11356 11 Round to -inf
11357
11358 FLT_ROUNDS, on the other hand, expects the following:
11359 -1 Undefined
11360 0 Round to 0
11361 1 Round to nearest
11362 2 Round to +inf
11363 3 Round to -inf
11364 */
11365
11366 // Save FPC to register.
11367 SDValue Chain = Op.getOperand(0);
11368 SDValue EFPC(
11369 DAG.getMachineNode(SystemZ::EFPC, dl, {MVT::i32, MVT::Other}, Chain), 0);
11370 Chain = EFPC.getValue(1);
11371
11372 // Transform as necessary
11373 SDValue CWD1 = DAG.getNode(ISD::AND, dl, MVT::i32, EFPC,
11374 DAG.getConstant(3, dl, MVT::i32));
11375 // RetVal = (CWD1 ^ (CWD1 >> 1)) ^ 1
11376 SDValue CWD2 = DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1,
11377 DAG.getNode(ISD::SRL, dl, MVT::i32, CWD1,
11378 DAG.getConstant(1, dl, MVT::i32)));
11379
11380 SDValue RetVal = DAG.getNode(ISD::XOR, dl, MVT::i32, CWD2,
11381 DAG.getConstant(1, dl, MVT::i32));
11382 RetVal = DAG.getZExtOrTrunc(RetVal, dl, Op.getValueType());
11383
11384 return DAG.getMergeValues({RetVal, Chain}, dl);
11385}
11386
11387SDValue SystemZTargetLowering::lowerVECREDUCE_ADD(SDValue Op,
11388 SelectionDAG &DAG) const {
11389 EVT VT = Op.getValueType();
11390 Op = Op.getOperand(0);
11391 EVT OpVT = Op.getValueType();
11392
11393 assert(OpVT.isVector() && "Operand type for VECREDUCE_ADD is not a vector.");
11394
11395 SDLoc DL(Op);
11396
11397 // load a 0 vector for the third operand of VSUM.
11398 SDValue Zero = DAG.getSplatBuildVector(OpVT, DL, DAG.getConstant(0, DL, VT));
11399
11400 // execute VSUM.
11401 switch (OpVT.getScalarSizeInBits()) {
11402 case 8:
11403 case 16:
11404 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Zero);
11405 [[fallthrough]];
11406 case 32:
11407 case 64:
11408 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::i128, Op,
11409 DAG.getBitcast(Op.getValueType(), Zero));
11410 break;
11411 case 128:
11412 break; // VSUM over v1i128 should not happen and would be a noop
11413 default:
11414 llvm_unreachable("Unexpected scalar size.");
11415 }
11416 // Cast to original vector type, retrieve last element.
11417 return DAG.getNode(
11418 ISD::EXTRACT_VECTOR_ELT, DL, VT, DAG.getBitcast(OpVT, Op),
11419 DAG.getConstant(OpVT.getVectorNumElements() - 1, DL, MVT::i32));
11420}
11421
11423 FunctionType *FT = F->getFunctionType();
11424 const AttributeList &Attrs = F->getAttributes();
11425 if (Attrs.hasRetAttrs())
11426 OS << Attrs.getAsString(AttributeList::ReturnIndex) << " ";
11427 OS << *F->getReturnType() << " @" << F->getName() << "(";
11428 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
11429 if (I)
11430 OS << ", ";
11431 OS << *FT->getParamType(I);
11432 AttributeSet ArgAttrs = Attrs.getParamAttrs(I);
11433 for (auto A : {Attribute::SExt, Attribute::ZExt, Attribute::NoExt})
11434 if (ArgAttrs.hasAttribute(A))
11435 OS << " " << Attribute::getNameFromAttrKind(A);
11436 }
11437 OS << ")\n";
11438}
11439
11440bool SystemZTargetLowering::isInternal(const Function *Fn) const {
11441 std::map<const Function *, bool>::iterator Itr = IsInternalCache.find(Fn);
11442 if (Itr == IsInternalCache.end())
11443 Itr = IsInternalCache
11444 .insert(std::pair<const Function *, bool>(
11445 Fn, (Fn->hasLocalLinkage() && !Fn->hasAddressTaken())))
11446 .first;
11447 return Itr->second;
11448}
11449
11450void SystemZTargetLowering::
11451verifyNarrowIntegerArgs_Call(const SmallVectorImpl<ISD::OutputArg> &Outs,
11452 const Function *F, SDValue Callee) const {
11453 // Temporarily only do the check when explicitly requested, until it can be
11454 // enabled by default.
11456 return;
11457
11458 bool IsInternal = false;
11459 const Function *CalleeFn = nullptr;
11460 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee))
11461 if ((CalleeFn = dyn_cast<Function>(G->getGlobal())))
11462 IsInternal = isInternal(CalleeFn);
11463 if (!IsInternal && !verifyNarrowIntegerArgs(Outs)) {
11464 errs() << "ERROR: Missing extension attribute of passed "
11465 << "value in call to function:\n" << "Callee: ";
11466 if (CalleeFn != nullptr)
11467 printFunctionArgExts(CalleeFn, errs());
11468 else
11469 errs() << "-\n";
11470 errs() << "Caller: ";
11472 llvm_unreachable("");
11473 }
11474}
11475
11476void SystemZTargetLowering::
11477verifyNarrowIntegerArgs_Ret(const SmallVectorImpl<ISD::OutputArg> &Outs,
11478 const Function *F) const {
11479 // Temporarily only do the check when explicitly requested, until it can be
11480 // enabled by default.
11482 return;
11483
11484 if (!isInternal(F) && !verifyNarrowIntegerArgs(Outs)) {
11485 errs() << "ERROR: Missing extension attribute of returned "
11486 << "value from function:\n";
11488 llvm_unreachable("");
11489 }
11490}
11491
11492// Verify that narrow integer arguments are extended as required by the ABI.
11493// Return false if an error is found.
11494bool SystemZTargetLowering::verifyNarrowIntegerArgs(
11495 const SmallVectorImpl<ISD::OutputArg> &Outs) const {
11496 if (!Subtarget.isTargetELF())
11497 return true;
11498
11501 return true;
11502 } else if (!getTargetMachine().Options.VerifyArgABICompliance)
11503 return true;
11504
11505 for (unsigned i = 0; i < Outs.size(); ++i) {
11506 MVT VT = Outs[i].VT;
11507 ISD::ArgFlagsTy Flags = Outs[i].Flags;
11508 if (VT.isInteger()) {
11509 assert((VT == MVT::i32 || VT.getSizeInBits() >= 64) &&
11510 "Unexpected integer argument VT.");
11511 if (VT == MVT::i32 &&
11512 !Flags.isSExt() && !Flags.isZExt() && !Flags.isNoExt())
11513 return false;
11514 }
11515 }
11516
11517 return true;
11518}
11519
11521 Module &M, const LibcallLoweringInfo &Libcalls) const {
11522 StringRef GuardMode = M.getStackProtectorGuard();
11523
11524 // In the TLS case, no symbol needs to be inserted.
11525 if (GuardMode == "tls" || GuardMode.empty())
11526 return;
11527
11528 // Otherwise (in the global case), insert the appropriate global variable.
11530}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
AMDGPU Register Bank Select
static bool isZeroVector(SDValue N)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
Function Alias Analysis Results
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val, const CCValAssign &VA, const SDLoc &DL)
static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val, const CCValAssign &VA, const SDLoc &DL)
#define Check(C,...)
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
static LVOptions Options
Definition LVOptions.cpp:25
static bool isSelectPseudo(MachineInstr &MI)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
uint64_t High
uint64_t IntrinsicInst * II
#define P(N)
static constexpr MCPhysReg SPReg
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
const char * Msg
This file defines the SmallSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SDValue getI128Select(SelectionDAG &DAG, const SDLoc &DL, Comparison C, SDValue TrueOp, SDValue FalseOp)
static SmallVector< SDValue, 4 > simplifyAssumingCCVal(SDValue &Val, SDValue &CC, SelectionDAG &DAG)
static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static void printFunctionArgExts(const Function *F, raw_fd_ostream &OS)
static void adjustForLTGFR(Comparison &C)
static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0, SDValue Op1)
#define CONV(X)
static cl::opt< bool > EnableIntArgExtCheck("argext-abi-check", cl::init(false), cl::desc("Verify that narrow int args are properly extended per the " "SystemZ ABI."))
static bool isOnlyUsedByStores(SDValue StoredVal, SelectionDAG &DAG)
static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT, unsigned Opcode, SDValue Op0, SDValue Op1, SDValue &Even, SDValue &Odd)
static void adjustForRedundantAnd(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static SDValue lowerAddrSpaceCast(SDValue Op, SelectionDAG &DAG)
static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, SDValue Value)
static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In)
static bool isSimpleShift(SDValue N, unsigned &ShiftVal)
static SDValue mergeHighParts(SelectionDAG &DAG, const SDLoc &DL, unsigned MergedBits, EVT VT, SDValue Op0, SDValue Op1)
static bool isI128MovedToParts(LoadSDNode *LD, SDNode *&LoPart, SDNode *&HiPart)
static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1)
static uint32_t findZeroVectorIdx(SDValue *Ops, unsigned Num)
static bool isVectorElementSwap(ArrayRef< int > M, EVT VT)
static void getCSAddressAndShifts(SDValue Addr, SelectionDAG &DAG, SDLoc DL, SDValue &AlignedAddr, SDValue &BitShift, SDValue &NegBitShift)
static bool isShlDoublePermute(const SmallVectorImpl< int > &Bytes, unsigned &StartIndex, unsigned &OpNo0, unsigned &OpNo1)
static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL, const Permute &P, SDValue Op0, SDValue Op1)
static SDNode * emitIntrinsicWithCCAndChain(SelectionDAG &DAG, SDValue Op, unsigned Opcode)
static SDValue getCCResult(SelectionDAG &DAG, SDValue CCReg)
static void adjustForStackGuardCompare(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode, unsigned &CCValid)
static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend, SDValue Op0, SDValue Op1, SDValue &Hi, SDValue &Lo)
static bool isF128MovedToParts(LoadSDNode *LD, SDNode *&LoPart, SDNode *&HiPart)
static void createPHIsForSelects(SmallVector< MachineInstr *, 8 > &Selects, MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB, MachineBasicBlock *SinkMBB)
static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL, SDValue *Ops, const SmallVectorImpl< int > &Bytes)
static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, CmpMode Mode, bool &Invert)
static unsigned CCMaskForCondCode(ISD::CondCode CC)
static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static void adjustForFNeg(Comparison &C)
static bool isScalarToVector(SDValue Op)
static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue CCReg, unsigned CCValid, unsigned CCMask)
static bool matchPermute(const SmallVectorImpl< int > &Bytes, const Permute &P, unsigned &OpNo0, unsigned &OpNo1)
static bool isAddCarryChain(SDValue Carry)
static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static MachineOperand earlyUseOperand(MachineOperand Op)
static bool canUseSiblingCall(const CCState &ArgCCInfo, SmallVectorImpl< CCValAssign > &ArgLocs, SmallVectorImpl< ISD::OutputArg > &Outs)
static bool getzOSCalleeAndADA(SelectionDAG &DAG, SDValue &Callee, SDValue &ADA, SDLoc &DL, SDValue &Chain)
static SDValue convertToF16(SDValue Op, SelectionDAG &DAG)
static bool combineCCMask(SDValue &CCReg, int &CCValid, int &CCMask, SelectionDAG &DAG)
static bool shouldSwapCmpOperands(const Comparison &C)
static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType)
static SDValue getADAEntry(SelectionDAG &DAG, SDValue Val, SDLoc DL, unsigned Offset, bool LoadAdr=false)
static SDNode * emitIntrinsicWithCC(SelectionDAG &DAG, SDValue Op, unsigned Opcode)
static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static bool getVPermMask(SDValue ShuffleOp, SmallVectorImpl< int > &Bytes)
static const Permute PermuteForms[]
static bool isI128MovedFromParts(SDValue Val, SDValue &LoPart, SDValue &HiPart)
static std::pair< SDValue, int > findCCUse(const SDValue &Val, unsigned Depth=0)
static bool isSubBorrowChain(SDValue Carry)
static void adjustICmp128(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static bool analyzeArgSplit(const SmallVectorImpl< ArgTy > &Args, SmallVector< CCValAssign, 16 > &ArgLocs, unsigned I, MVT &PartVT, unsigned &NumParts)
static APInt getDemandedSrcElements(SDValue Op, const APInt &DemandedElts, unsigned OpNo)
static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op, bool IsNegative)
static unsigned computeNumSignBitsBinOp(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth, unsigned OpNo)
static SDValue expandBitCastI128ToF128(SelectionDAG &DAG, SDValue Src, const SDLoc &SL)
static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, BuildVectorSDNode *BVN)
static SDValue convertFromF16(SDValue Op, SDLoc DL, SelectionDAG &DAG)
static unsigned getVectorComparison(ISD::CondCode CC, CmpMode Mode)
static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In)
static SDValue MergeInputChains(SDNode *N1, SDNode *N2)
static SDValue expandBitCastF128ToI128(SelectionDAG &DAG, SDValue Src, const SDLoc &SL)
static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask, uint64_t Mask, uint64_t CmpVal, unsigned ICmpType)
static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid)
static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL, SDValue Op, SDValue Chain)
static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1, ISD::CondCode Cond, const SDLoc &DL, SDValue Chain=SDValue(), bool IsSignaling=false)
static bool checkCCKill(MachineInstr &MI, MachineBasicBlock *MBB)
static Register forceReg(MachineInstr &MI, MachineOperand &Base, const SystemZInstrInfo *TII)
static bool is32Bit(EVT VT)
static std::pair< unsigned, const TargetRegisterClass * > parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC, const unsigned *Map, unsigned Size)
static unsigned detectEvenOddMultiplyOperand(const SelectionDAG &DAG, const SystemZSubtarget &Subtarget, SDValue &Op)
static bool matchDoublePermute(const SmallVectorImpl< int > &Bytes, const Permute &P, SmallVectorImpl< int > &Transform)
static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode, SDValue Call, unsigned CCValid, uint64_t CC, ISD::CondCode Cond)
static SDValue buildFPVecFromScalars4(SelectionDAG &DAG, const SDLoc &DL, EVT VT, SmallVectorImpl< SDValue > &Elems, unsigned Pos)
static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg)
static AddressingMode getLoadStoreAddrMode(bool HasVector, Type *Ty)
static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT, SDValue Op0, SDValue Op1)
static void computeKnownBitsBinOp(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth, unsigned OpNo)
static bool getShuffleInput(const SmallVectorImpl< int > &Bytes, unsigned Start, unsigned BytesPerElement, int &Base)
static AddressingMode supportedAddressingMode(Instruction *I, bool HasVector)
static bool isF128MovedFromParts(SDValue Val, SDValue &LoPart, SDValue &HiPart)
static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Value * RHS
Value * LHS
BinaryOperator * Mul
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1055
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1565
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1537
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:968
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1355
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1513
bool isSingleWord() const
Determine if this APInt just has one word to store value.
Definition APInt.h:323
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:398
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1266
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:865
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
BinOp getOperation() const
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:407
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
static LLVM_ABI StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI bool isConstant() const
CCState - This class holds information needed while lowering arguments and return values.
LLVM_ABI void AnalyzeCallResult(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeCallResult - Analyze the return values of a call, incorporating info about the passed values i...
LLVM_ABI bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
LLVM_ABI void AnalyzeReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeReturn - Analyze the returned values of a return, incorporating info about the result values i...
LLVM_ABI void AnalyzeCallOperands(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeCallOperands - Analyze the outgoing arguments to a call, incorporating info about the passed v...
uint64_t getStackSize() const
Returns the size of the currently allocated portion of the stack.
LLVM_ABI void AnalyzeFormalArguments(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeFormalArguments - Analyze an array of argument values, incorporating info about the formals in...
CCValAssign - Represent assignment of one arg/retval to a location.
Register getLocReg() const
LocInfo getLocInfo() const
bool needsCustom() const
bool isExtInLoc() const
int64_t getLocMemOffset() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
uint64_t getZExtValue() const
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
iterator end()
Definition DenseMap.h:141
bool hasAddressTaken(const User **=nullptr, bool IgnoreCallbackUses=false, bool IgnoreAssumeLikeCalls=true, bool IngoreLLVMUsed=false, bool IgnoreARCAttachedCall=false, bool IgnoreCastedDirectCall=false) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
Definition Function.cpp:937
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:762
uint64_t getFnAttributeAsParsedInteger(StringRef Kind, uint64_t Default=0) const
For a string attribute Kind, parse attribute as an integer.
Definition Function.cpp:774
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:730
bool hasLocalLinkage() const
bool hasPrivateLinkage() const
bool hasInternalLinkage() const
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Tracks which library functions to use for a particular subtarget.
An instruction for reading from memory.
This class is used to represent ISD::LOAD nodes.
const SDValue & getBasePtr() const
Machine Value Type.
static auto integer_fixedlen_vector_valuetypes()
SimpleValueType SimpleTy
uint64_t getScalarSizeInBits() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
static auto integer_valuetypes()
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
static auto fixedlen_vector_valuetypes()
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
static MVT getVectorVT(MVT VT, unsigned NumElements)
static MVT getIntegerVT(unsigned BitWidth)
static auto fp_valuetypes()
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setMaxCallFrameSize(uint64_t S)
LLVM_ABI int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
void setFrameAddressIsTaken(bool T)
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
void setReturnAddressIsTaken(bool s)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
void push_back(MachineBasicBlock *MBB)
reverse_iterator rbegin()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineFunctionProperties & getProperties() const
Get the function properties.
Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
bool killsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr kills the specified register.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
Flags getFlags() const
Return the raw flags of the source value,.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void addLiveIn(MCRegister Reg, Register vreg=Register())
addLiveIn - Add the specified register as a live-in.
Align getBaseAlign() const
Returns alignment and volatility of the memory access.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
const SDValue & getChain() const
EVT getMemoryVT() const
Return the type of the in-memory value.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool hasOneUse() const
Return true if there is exactly one use of this node.
SDNodeFlags getFlags() const
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
const SDValue & getOperand(unsigned Num) const
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
iterator_range< user_iterator > users()
void setFlags(SDNodeFlags NewFlags)
Represents a use of a SDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node, in exactly one operand.
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
bool isMachineOpcode() const
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getMachineOpcode() const
unsigned getOpcode() const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
SDValue getGLOBAL_OFFSET_TABLE(EVT VT)
Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
void addNoMergeSiteInfo(const SDNode *Node, bool NoMerge)
Set NoMergeSiteInfo to be associated with Node if NoMerge is true.
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align DstAlign, Align SrcAlign, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
const TargetLowering & getTargetLoweringInfo() const
SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags=0)
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, SDValue InGlue, const SDLoc &DL)
Return a new CALLSEQ_END node, which always must have a glue result (to ensure it's not CSE'd).
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getSignedTargetConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, const SDLoc &DL)
Return a new CALLSEQ_START node, that starts new call frame, in which InSize bytes are set up inside ...
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, unsigned TargetFlags=0)
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
SDValue getTargetConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offset=0, unsigned TargetFlags=0)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
ArrayRef< int > getMask() const
const_iterator begin() const
Definition SmallSet.h:216
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
size_type size() const
Definition SmallSet.h:171
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
This class is used to represent ISD::STORE nodes.
const SDValue & getBasePtr() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:490
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition StringRef.h:720
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
iterator end() const
Definition StringRef.h:116
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
A SystemZ-specific class detailing special use registers particular for calling conventions.
static SystemZConstantPoolValue * Create(const GlobalValue *GV, SystemZCP::SystemZCPModifier Modifier)
const SystemZInstrInfo * getInstrInfo() const override
SystemZCallingConventionRegisters * getSpecialRegisters() const
AtomicExpansionKind shouldExpandAtomicRMWInIR(const AtomicRMWInst *RMW) const override
Returns how the IR-level AtomicExpand pass should expand the given AtomicRMW, if at all.
Register getExceptionSelectorRegister(const Constant *PersonalityFn) const override
If a physical register, this returns the register that receives the exception typeid on entry to a la...
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
This callback is invoked for operations that are unsupported by the target, which are registered to u...
EVT getOptimalMemOpType(LLVMContext &Context, const MemOp &Op, const AttributeList &FuncAttributes) const override
Returns the target specific optimal type for load and store operations as a result of memset,...
bool hasInlineStackProbe(const MachineFunction &MF) const override
Returns true if stack probing through inline assembly is requested.
MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *BB) const override
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
MachineBasicBlock * emitEHSjLjSetJmp(MachineInstr &MI, MachineBasicBlock *MBB) const
AtomicExpansionKind shouldCastAtomicLoadInIR(LoadInst *LI) const override
Returns how the given (atomic) load should be cast by the IR-level AtomicExpand pass.
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &, EVT) const override
Return the ValueType of the result of SETCC operations.
bool allowTruncateForTailCall(Type *, Type *) const override
Return true if a truncation from FromTy to ToTy is permitted when deciding whether a call is in tail ...
SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Flag, const SDLoc &DL, const AsmOperandInfo &Constraint, SelectionDAG &DAG) const override
SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SDLoc &DL, SelectionDAG &DAG) const override
This hook must be implemented to lower outgoing return values, described by the Outs array,...
MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
MachineBasicBlock * emitEHSjLjLongJmp(MachineInstr &MI, MachineBasicBlock *MBB) const
bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, LLVMContext &Context, const Type *RetTy) const override
This hook should be implemented to check whether the return values described by the Outs array can fi...
std::pair< SDValue, SDValue > makeExternalCall(SDValue Chain, SelectionDAG &DAG, const char *CalleeName, EVT RetVT, ArrayRef< SDValue > Ops, CallingConv::ID CallConv, bool IsSigned, SDLoc DL, bool DoesNotReturn, bool IsReturnValueUsed) const
void insertSSPDeclarations(Module &M, const LibcallLoweringInfo &Libcalls) const override
Insert SSP declaration if global stack protector is used.
bool mayBeEmittedAsTailCall(const CallInst *CI) const override
Return true if the target may be able emit the call instruction as a tail call.
bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const override
Target-specific splitting of values into parts that fit a register storing a legal type.
bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I=nullptr) const override
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Certain targets require unusual breakdowns of certain types.
bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, UndefPoisonKind Kind, unsigned Depth) const override
Return true if this function can prove that Op is never poison and, Kind can be used to track poison ...
SystemZTargetLowering(const TargetMachine &TM, const SystemZSubtarget &STI)
bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT VT) const override
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
bool isLegalICmpImmediate(int64_t Imm) const override
Return true if the specified immediate is legal icmp immediate, that is the target has icmp instructi...
std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const override
Given a physical register constraint (e.g.
TargetLowering::ConstraintWeight getSingleConstraintMatchWeight(AsmOperandInfo &info, const char *constraint) const override
Examine constraint string and operand type and determine a weight value.
bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AS, Align Alignment, MachineMemOperand::Flags Flags, unsigned *Fast) const override
Determine if the target supports unaligned memory accesses.
const MCPhysReg * getScratchRegisters(CallingConv::ID CC) const override
Returns a 0 terminated array of registers that can be safely used as scratch registers.
TargetLowering::ConstraintType getConstraintType(StringRef Constraint) const override
Given a constraint, return the type of constraint it is for this target.
bool isFPImmLegal(const APFloat &Imm, EVT VT, bool ForCodeSize) const override
Returns true if the target can instruction select the specified FP immediate natively.
Register getExceptionPointerRegister(const Constant *PersonalityFn) const override
If a physical register, this returns the register that receives the exception address on entry to an ...
SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const override
Target-specific combining of register parts into its original value.
bool isTruncateFree(Type *, Type *) const override
Return true if it's free to truncate a value of type FromTy to type ToTy.
SDValue useLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, MVT VT, SDValue Arg, SDLoc DL, SDValue Chain, bool IsStrict) const
unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const override
Determine the number of bits in the operation that are sign bits.
void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override
This method will be invoked for all target nodes and for any target-independent nodes that the target...
SDValue LowerCall(CallLoweringInfo &CLI, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower calls into the specified DAG.
bool isLegalAddImmediate(int64_t Imm) const override
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
CondMergingParams getJumpConditionMergingParams(Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, const Function *F) const override
bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes, EVT *LargestVT=nullptr) const override
Determines the optimal series of memory ops to replace the memset / memcpy.
void ReplaceNodeResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
This callback is invoked when a node result type is illegal for the target, and the operation was reg...
void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const override
Lower the specified operand into the Ops vector.
unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const override
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
AtomicExpansionKind shouldCastAtomicStoreInIR(StoreInst *SI) const override
Returns how the given (atomic) store should be cast by the IR-level AtomicExpand pass into.
Register getRegisterByName(const char *RegName, LLT VT, const MachineFunction &MF) const override
Return the register ID of the name passed in.
bool hasAndNot(SDValue Y) const override
Return true if the target has a bitwise and-not operation: X = ~A & B This can be used to simplify se...
SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::InputArg > &Ins, const SDLoc &DL, SelectionDAG &DAG, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower the incoming (formal) arguments, described by the Ins array,...
void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const override
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
unsigned getStackProbeSize(const MachineFunction &MF) const
XPLINK64 calling convention specific use registers Particular to z/OS when in 64 bit mode.
Information about stack frame layout on the target.
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
TargetInstrInfo - Interface to description of machine instruction set.
void setBooleanVectorContents(BooleanContent Ty)
Specify how the target extends the result of a vector boolean value from a vector of i1 to a wider ty...
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
unsigned MaxStoresPerMemcpyOptSize
Likewise for functions with the OptSize attribute.
MachineBasicBlock * emitPatchPoint(MachineInstr &MI, MachineBasicBlock *MBB) const
Replace/modify any TargetFrameIndex operands with a targte-dependent sequence of memory operands that...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
const TargetMachine & getTargetMachine() const
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual void insertSSPDeclarations(Module &M, const LibcallLoweringInfo &Libcalls) const
Inserts necessary declarations for SSP (stack protection) purpose.
void setMaxAtomicSizeInBitsSupported(unsigned SizeInBits)
Set the maximum atomic operation size supported by the backend.
void setAtomicLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, LegalizeAction Action)
Let target indicate that an extending atomic load of the specified type is legal.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
void setMinFunctionAlignment(Align Alignment)
Set the target's minimum function alignment.
unsigned MaxStoresPerMemsetOptSize
Likewise for functions with the OptSize attribute.
void setBooleanContents(BooleanContent Ty)
Specify how the target extends the result of integer and floating point boolean values from i1 to a w...
unsigned MaxStoresPerMemmove
Specify maximum number of store instructions per memmove call.
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose.
unsigned MaxStoresPerMemmoveOptSize
Likewise for functions with the OptSize attribute.
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
void setPrefFunctionAlignment(Align Alignment)
Set the target's preferred function alignment.
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
unsigned MaxStoresPerMemset
Specify maximum number of store instructions per memset call.
void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified truncating store does not work with the specified type and indicate what ...
virtual const TargetRegisterClass * getRepRegClassFor(MVT VT) const
Return the 'representative' register class for the specified value type.
void setStackPointerRegisterToSaveRestore(Register R)
If set to a physical register, this specifies the register that llvm.savestack/llvm....
AtomicExpansionKind
Enum that specifies what an atomic load/AtomicRMWInst is expanded to, if at all.
void setTargetDAGCombine(ArrayRef< ISD::NodeType > NTs)
Targets should invoke this method for each target independent node that they want to provide a custom...
void setLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified load with extension does not work with the specified type and indicate wh...
virtual bool shouldSignExtendTypeInLibCall(Type *Ty, bool IsSigned) const
Returns true if arguments should be sign-extended in lib calls.
std::vector< ArgListEntry > ArgListTy
unsigned MaxStoresPerMemcpy
Specify maximum number of store instructions per memcpy call.
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
void setSchedulingPreference(Sched::Preference Pref)
Specify the target scheduling preference.
LegalizeAction getOperationAction(unsigned Op, EVT VT) const
Return how this operation should be treated: either it is legal, needs to be promoted to a larger siz...
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes, EVT *LargestVT=nullptr) const
Determines the optimal series of memory ops to replace the memset / memcpy.
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, SelectionDAG &DAG) const
Lower TLS global address SDNode for target independent emulated TLS model.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual ConstraintWeight getSingleConstraintMatchWeight(AsmOperandInfo &info, const char *constraint) const
Examine constraint string and operand type and determine a weight value.
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
TargetLowering(const TargetLowering &)=delete
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::LibcallImpl LibcallImpl, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
Primary interface to the complete machine description for the target machine.
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
bool useEmulatedTLS() const
Returns true if this target uses emulated TLS.
unsigned getPointerSize(unsigned AS) const
Get the pointer size for this target.
CodeModel::Model getCodeModel() const
Returns the code model.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
user_iterator user_begin()
Definition Value.h:402
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
int getNumOccurrences() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
A raw_ostream that writes to a file descriptor.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:829
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:513
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:168
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:789
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:863
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:520
@ PSEUDO_FMIN
PSEUDO_FMIN is strictly equivalent to op0 olt op1 ?
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ STRICT_FMINIMUM
Definition ISDOpcodes.h:473
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:890
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:749
@ MEMBARRIER
MEMBARRIER - Compiler barrier only; generate a no-op.
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:920
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:280
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ STRICT_PSEUDO_FMAX
Definition ISDOpcodes.h:462
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ STRICT_FSQRT
Constrained versions of libm-equivalent floating point intrinsics.
Definition ISDOpcodes.h:438
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:798
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:854
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:487
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:667
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ STRICT_PSEUDO_FMIN
Definition ISDOpcodes.h:461
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ BR_JT
BR_JT - Jumptable branch.
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:550
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:806
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:674
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:980
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:706
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:771
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:651
@ STRICT_FMAXIMUM
Definition ISDOpcodes.h:472
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:578
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:860
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:821
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:909
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:898
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:729
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:988
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:815
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:486
@ STRICT_FROUNDEVEN
Definition ISDOpcodes.h:466
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:480
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:502
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:479
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:936
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:507
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:741
@ TRAP
TRAP - Trapping instruction.
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:427
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:567
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:797
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:969
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:931
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
@ STRICT_FNEARBYINT
Definition ISDOpcodes.h:458
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:162
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:866
@ BRCOND
BRCOND - Conditional branch.
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:843
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:536
@ GET_DYNAMIC_AREA_OFFSET
GET_DYNAMIC_AREA_OFFSET - get offset from native SP to the address of the most recent dynamic alloca.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:558
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
bool isNormalLoad(const SDNode *N)
Returns true if the specified node is a non-extending and unindexed load.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
auto m_Cmp()
Matches any compare instruction and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
auto m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getSINTTOFP(EVT OpVT, EVT RetVT)
getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getUINTTOFP(EVT OpVT, EVT RetVT)
getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getFPTOSINT(EVT OpVT, EVT RetVT)
getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:58
const unsigned GR64Regs[16]
const unsigned VR128Regs[32]
const unsigned VR16Regs[32]
const unsigned GR128Regs[16]
const unsigned FP32Regs[16]
const unsigned FP16Regs[16]
const unsigned GR32Regs[16]
const unsigned FP64Regs[16]
const int64_t ELFCallFrameSize
const unsigned VR64Regs[32]
const unsigned FP128Regs[16]
const unsigned VR32Regs[32]
unsigned odd128(bool Is32bit)
const unsigned CCMASK_CMP_GE
Definition SystemZ.h:41
static bool isImmHH(uint64_t Val)
Definition SystemZ.h:177
const unsigned CCMASK_TEND
Definition SystemZ.h:98
const unsigned CCMASK_CS_EQ
Definition SystemZ.h:68
const unsigned CCMASK_TBEGIN
Definition SystemZ.h:93
const unsigned CCMASK_0
Definition SystemZ.h:28
const MCPhysReg ELFArgFPRs[ELFNumArgFPRs]
MachineBasicBlock * splitBlockBefore(MachineBasicBlock::iterator MI, MachineBasicBlock *MBB)
const unsigned CCMASK_TM_SOME_1
Definition SystemZ.h:83
const unsigned CCMASK_LOGICAL_CARRY
Definition SystemZ.h:61
const unsigned TDCMASK_NORMAL_MINUS
Definition SystemZ.h:123
const unsigned CCMASK_TDC
Definition SystemZ.h:110
const unsigned CCMASK_FCMP
Definition SystemZ.h:49
const unsigned CCMASK_TM_SOME_0
Definition SystemZ.h:82
static bool isImmHL(uint64_t Val)
Definition SystemZ.h:172
const unsigned TDCMASK_SUBNORMAL_MINUS
Definition SystemZ.h:125
const unsigned PFD_READ
Definition SystemZ.h:116
const unsigned CCMASK_1
Definition SystemZ.h:29
const unsigned TDCMASK_NORMAL_PLUS
Definition SystemZ.h:122
const unsigned PFD_WRITE
Definition SystemZ.h:117
const unsigned CCMASK_CMP_GT
Definition SystemZ.h:38
const unsigned TDCMASK_QNAN_MINUS
Definition SystemZ.h:129
const unsigned CCMASK_CS
Definition SystemZ.h:70
const unsigned CCMASK_ANY
Definition SystemZ.h:32
const unsigned CCMASK_ARITH
Definition SystemZ.h:56
const unsigned CCMASK_TM_MIXED_MSB_0
Definition SystemZ.h:79
const unsigned TDCMASK_SUBNORMAL_PLUS
Definition SystemZ.h:124
static bool isImmLL(uint64_t Val)
Definition SystemZ.h:162
const unsigned VectorBits
Definition SystemZ.h:155
static bool isImmLH(uint64_t Val)
Definition SystemZ.h:167
MachineBasicBlock * emitBlockAfter(MachineBasicBlock *MBB)
const unsigned TDCMASK_INFINITY_PLUS
Definition SystemZ.h:126
unsigned reverseCCMask(unsigned CCMask)
const unsigned CCMASK_TM_ALL_0
Definition SystemZ.h:78
const unsigned IPM_CC
Definition SystemZ.h:113
const unsigned CCMASK_CMP_LE
Definition SystemZ.h:40
const unsigned CCMASK_CMP_O
Definition SystemZ.h:45
const unsigned CCMASK_CMP_EQ
Definition SystemZ.h:36
const unsigned VectorBytes
Definition SystemZ.h:159
const unsigned TDCMASK_INFINITY_MINUS
Definition SystemZ.h:127
const unsigned CCMASK_ICMP
Definition SystemZ.h:48
const unsigned CCMASK_VCMP_ALL
Definition SystemZ.h:102
const unsigned CCMASK_VCMP_NONE
Definition SystemZ.h:104
MachineBasicBlock * splitBlockAfter(MachineBasicBlock::iterator MI, MachineBasicBlock *MBB)
const unsigned CCMASK_VCMP
Definition SystemZ.h:105
const unsigned CCMASK_TM_MIXED_MSB_1
Definition SystemZ.h:80
const unsigned CCMASK_TM_MSB_0
Definition SystemZ.h:84
const unsigned CCMASK_ARITH_OVERFLOW
Definition SystemZ.h:55
const unsigned CCMASK_CS_NE
Definition SystemZ.h:69
const unsigned TDCMASK_SNAN_PLUS
Definition SystemZ.h:130
const unsigned CCMASK_TM
Definition SystemZ.h:86
const unsigned CCMASK_3
Definition SystemZ.h:31
const unsigned CCMASK_NONE
Definition SystemZ.h:27
const unsigned CCMASK_CMP_LT
Definition SystemZ.h:37
const unsigned CCMASK_CMP_NE
Definition SystemZ.h:39
const unsigned TDCMASK_ZERO_PLUS
Definition SystemZ.h:120
const unsigned TDCMASK_QNAN_PLUS
Definition SystemZ.h:128
const unsigned TDCMASK_ZERO_MINUS
Definition SystemZ.h:121
unsigned even128(bool Is32bit)
const unsigned CCMASK_TM_ALL_1
Definition SystemZ.h:81
const unsigned CCMASK_LOGICAL_BORROW
Definition SystemZ.h:63
const unsigned ELFNumArgFPRs
const unsigned CCMASK_CMP_UO
Definition SystemZ.h:44
const unsigned CCMASK_LOGICAL
Definition SystemZ.h:65
const unsigned CCMASK_TM_MSB_1
Definition SystemZ.h:85
const unsigned TDCMASK_SNAN_MINUS
Definition SystemZ.h:131
initializer< Ty > init(const Ty &Val)
support::ulittle32_t Word
Definition IRSymtab.h:53
@ User
could "use" a pointer
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< CodeNode * > Code
Definition RDFGraph.h:388
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:345
@ Offset
Definition DWP.cpp:578
@ Length
Definition DWP.cpp:578
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
@ Known
Known to have no common set bits.
@ Define
Register definition.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
@ Load
The value being inserted comes from a load (InsertElement only).
testing::Matcher< const detail::ErrorHolder & > Failed()
Definition Error.h:198
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:89
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
LLVM_ABI void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert ‘Bytes’ to a hex string and output to ‘OS’.
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
Definition bit.h:362
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Success
The lock was released successfully.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
AtomicOrdering
Atomic ordering for LLVM's memory model.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:395
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
@ BeforeLegalizeTypes
Definition DAGCombine.h:16
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:573
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:78
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:347
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
#define EQ(a, b)
Definition regexec.c:65
AddressingMode(bool LongDispl, bool IdxReg)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition ValueTypes.h:90
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:418
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:404
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isRound() const
Return true if the size is a power-of-two number of bytes.
Definition ValueTypes.h:271
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
bool isVectorOf(EVT EltVT) const
Return true if this is a vector with matching element type.
Definition ValueTypes.h:181
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:359
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
SmallVector< unsigned, 2 > OpVals
bool isVectorConstantLegal(const SystemZSubtarget &Subtarget)
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg + ScalableOffset*...
This contains information for each constraint that we are lowering.
This structure contains all information that is necessary for lowering calls.
SmallVector< ISD::InputArg, 32 > Ins
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setZExtResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setSExtResult(bool Value=true)
CallLoweringInfo & setNoReturn(bool Value=true)
SmallVector< ISD::OutputArg, 32 > Outs
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.