LLVM  7.0.0
AArch64ISelLowering.cpp
Go to the documentation of this file.
1 //===-- AArch64ISelLowering.cpp - AArch64 DAG Lowering Implementation ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AArch64TargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AArch64ISelLowering.h"
17 #include "AArch64PerfectShuffle.h"
18 #include "AArch64RegisterInfo.h"
19 #include "AArch64Subtarget.h"
21 #include "Utils/AArch64BaseInfo.h"
22 #include "llvm/ADT/APFloat.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/ADT/Twine.h"
47 #include "llvm/IR/Attributes.h"
48 #include "llvm/IR/Constants.h"
49 #include "llvm/IR/DataLayout.h"
50 #include "llvm/IR/DebugLoc.h"
51 #include "llvm/IR/DerivedTypes.h"
52 #include "llvm/IR/Function.h"
54 #include "llvm/IR/GlobalValue.h"
55 #include "llvm/IR/IRBuilder.h"
56 #include "llvm/IR/Instruction.h"
57 #include "llvm/IR/Instructions.h"
58 #include "llvm/IR/Intrinsics.h"
59 #include "llvm/IR/Module.h"
60 #include "llvm/IR/OperandTraits.h"
61 #include "llvm/IR/Type.h"
62 #include "llvm/IR/Use.h"
63 #include "llvm/IR/Value.h"
64 #include "llvm/MC/MCRegisterInfo.h"
65 #include "llvm/Support/Casting.h"
66 #include "llvm/Support/CodeGen.h"
68 #include "llvm/Support/Compiler.h"
69 #include "llvm/Support/Debug.h"
71 #include "llvm/Support/KnownBits.h"
77 #include <algorithm>
78 #include <bitset>
79 #include <cassert>
80 #include <cctype>
81 #include <cstdint>
82 #include <cstdlib>
83 #include <iterator>
84 #include <limits>
85 #include <tuple>
86 #include <utility>
87 #include <vector>
88 
89 using namespace llvm;
90 
91 #define DEBUG_TYPE "aarch64-lower"
92 
93 STATISTIC(NumTailCalls, "Number of tail calls");
94 STATISTIC(NumShiftInserts, "Number of vector shift inserts");
95 STATISTIC(NumOptimizedImms, "Number of times immediates were optimized");
96 
97 static cl::opt<bool>
98 EnableAArch64SlrGeneration("aarch64-shift-insert-generation", cl::Hidden,
99  cl::desc("Allow AArch64 SLI/SRI formation"),
100  cl::init(false));
101 
102 // FIXME: The necessary dtprel relocations don't seem to be supported
103 // well in the GNU bfd and gold linkers at the moment. Therefore, by
104 // default, for now, fall back to GeneralDynamic code generation.
106  "aarch64-elf-ldtls-generation", cl::Hidden,
107  cl::desc("Allow AArch64 Local Dynamic TLS code generation"),
108  cl::init(false));
109 
110 static cl::opt<bool>
111 EnableOptimizeLogicalImm("aarch64-enable-logical-imm", cl::Hidden,
112  cl::desc("Enable AArch64 logical imm instruction "
113  "optimization"),
114  cl::init(true));
115 
116 /// Value type used for condition codes.
117 static const MVT MVT_CC = MVT::i32;
118 
120  const AArch64Subtarget &STI)
121  : TargetLowering(TM), Subtarget(&STI) {
122  // AArch64 doesn't have comparisons which set GPRs or setcc instructions, so
123  // we have to make something up. Arbitrarily, choose ZeroOrOne.
125  // When comparing vectors the result sets the different elements in the
126  // vector to all-one or all-zero.
128 
129  // Set up the register classes.
130  addRegisterClass(MVT::i32, &AArch64::GPR32allRegClass);
131  addRegisterClass(MVT::i64, &AArch64::GPR64allRegClass);
132 
133  if (Subtarget->hasFPARMv8()) {
134  addRegisterClass(MVT::f16, &AArch64::FPR16RegClass);
135  addRegisterClass(MVT::f32, &AArch64::FPR32RegClass);
136  addRegisterClass(MVT::f64, &AArch64::FPR64RegClass);
137  addRegisterClass(MVT::f128, &AArch64::FPR128RegClass);
138  }
139 
140  if (Subtarget->hasNEON()) {
141  addRegisterClass(MVT::v16i8, &AArch64::FPR8RegClass);
142  addRegisterClass(MVT::v8i16, &AArch64::FPR16RegClass);
143  // Someone set us up the NEON.
144  addDRTypeForNEON(MVT::v2f32);
145  addDRTypeForNEON(MVT::v8i8);
146  addDRTypeForNEON(MVT::v4i16);
147  addDRTypeForNEON(MVT::v2i32);
148  addDRTypeForNEON(MVT::v1i64);
149  addDRTypeForNEON(MVT::v1f64);
150  addDRTypeForNEON(MVT::v4f16);
151 
152  addQRTypeForNEON(MVT::v4f32);
153  addQRTypeForNEON(MVT::v2f64);
154  addQRTypeForNEON(MVT::v16i8);
155  addQRTypeForNEON(MVT::v8i16);
156  addQRTypeForNEON(MVT::v4i32);
157  addQRTypeForNEON(MVT::v2i64);
158  addQRTypeForNEON(MVT::v8f16);
159  }
160 
161  // Compute derived properties from the register classes
163 
164  // Provide all sorts of operation actions
192 
196 
200 
202 
203  // Custom lowering hooks are needed for XOR
204  // to fold it into CSINC/CSINV.
207 
208  // Virtually no operation on f128 is legal, but LLVM can't expand them when
209  // there's a valid register class, so we need custom operations in most cases.
231 
232  // Lowering for many of the conversions is actually specified by the non-f128
233  // type. The LowerXXX function will be trivial when f128 isn't involved.
248 
249  // Variable arguments.
254 
255  // Variable-sized objects.
258 
259  if (Subtarget->isTargetWindows())
261  else
263 
264  // Constant pool entries
266 
267  // BlockAddress
269 
270  // Add/Sub overflow ops with MVT::Glues are lowered to NZCV dependences.
279 
280  // AArch64 lacks both left-rotate and popcount instructions.
283  for (MVT VT : MVT::vector_valuetypes()) {
286  }
287 
288  // AArch64 doesn't have {U|S}MUL_LOHI.
291 
294 
297  for (MVT VT : MVT::vector_valuetypes()) {
300  }
307 
308  // Custom lower Add/Sub/Mul with overflow.
321 
330  if (Subtarget->hasFullFP16())
332  else
334 
366 
367  if (!Subtarget->hasFullFP16()) {
390 
391  // promote v4f16 to v4f32 when that is known to be safe.
404 
420 
441  }
442 
443  // AArch64 has implementations of a lot of rounding-like FP operations.
444  for (MVT Ty : {MVT::f32, MVT::f64}) {
455  }
456 
457  if (Subtarget->hasFullFP16()) {
468  }
469 
471 
473 
479 
480  // Lower READCYCLECOUNTER using an mrs from PMCCNTR_EL0.
481  // This requires the Performance Monitors extension.
482  if (Subtarget->hasPerfMon())
484 
485  if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr &&
486  getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) {
487  // Issue __sincos_stret if available.
490  } else {
493  }
494 
495  // Make floating-point constants legal for the large code model, so they don't
496  // become loads from the constant pool.
497  if (Subtarget->isTargetMachO() && TM.getCodeModel() == CodeModel::Large) {
500  }
501 
502  // AArch64 does not have floating-point extending loads, i1 sign-extending
503  // load, floating-point truncating stores, or v2i32->v2i16 truncating store.
504  for (MVT VT : MVT::fp_valuetypes()) {
509  }
510  for (MVT VT : MVT::integer_valuetypes())
512 
520 
523 
524  // Indexed loads and stores are supported.
525  for (unsigned im = (unsigned)ISD::PRE_INC;
541  }
542 
543  // Trap.
545 
546  // We combine OR nodes for bitfield operations.
548 
549  // Vector add and sub nodes may conceal a high-half opportunity.
550  // Also, try to fold ADD into CSINC/CSINV..
557 
561 
563 
570  if (Subtarget->supportsAddressTopByteIgnored())
572 
574 
577 
581 
583 
584  // In case of strict alignment, avoid an excessive number of byte wide stores.
588 
593 
595 
597 
599 
600  EnableExtLdPromotion = true;
601 
602  // Set required alignment.
604  // Set preferred alignments.
607 
608  // Only change the limit for entries in a jump table if specified by
609  // the subtarget, but not at the command line.
610  unsigned MaxJT = STI.getMaximumJumpTableSize();
611  if (MaxJT && getMaximumJumpTableSize() == 0)
613 
614  setHasExtractBitsInsn(true);
615 
617 
618  if (Subtarget->hasNEON()) {
619  // FIXME: v1f64 shouldn't be legal if we can avoid it, because it leads to
620  // silliness like this:
646 
652 
654 
655  // AArch64 doesn't have a direct vector ->f32 conversion instructions for
656  // elements smaller than i32, so promote the input to i32 first.
661  // i8 and i16 vector elements also need promotion to i32 for v8i8 or v8i16
662  // -> v8f16 conversions.
667  // Similarly, there is no direct i32 -> f64 vector conversion instruction.
672  // Or, direct i32 -> f16 vector conversion. Set it so custom, so the
673  // conversion happens in two steps: v4i32 -> v4f32 -> v4f16
676 
679 
688 
689  // AArch64 doesn't have MUL.2d:
691  // Custom handling for some quad-vector types to detect MULL.
695 
696  // Vector reductions
697  for (MVT VT : MVT::integer_valuetypes()) {
703  }
704  for (MVT VT : MVT::fp_valuetypes()) {
707  }
708 
711  // Likewise, narrowing and extending vector loads/stores aren't handled
712  // directly.
713  for (MVT VT : MVT::vector_valuetypes()) {
715 
716  if (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32) {
719  } else {
722  }
725 
727 
728  for (MVT InnerVT : MVT::vector_valuetypes()) {
729  setTruncStoreAction(VT, InnerVT, Expand);
730  setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
731  setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
732  setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
733  }
734  }
735 
736  // AArch64 has implementations of a lot of rounding-like FP operations.
737  for (MVT Ty : {MVT::v2f32, MVT::v4f32, MVT::v2f64}) {
744  }
745 
747  }
748 
750 }
751 
752 void AArch64TargetLowering::addTypeForNEON(MVT VT, MVT PromotedBitwiseVT) {
753  assert(VT.isVector() && "VT should be a vector type");
754 
755  if (VT.isFloatingPoint()) {
757  setOperationPromotedToType(ISD::LOAD, VT, PromoteTo);
758  setOperationPromotedToType(ISD::STORE, VT, PromoteTo);
759  }
760 
761  // Mark vector float intrinsics as expand.
762  if (VT == MVT::v2f32 || VT == MVT::v4f32 || VT == MVT::v2f64) {
771 
772  // But we do support custom-lowering for FCOPYSIGN.
774  }
775 
788 
792  for (MVT InnerVT : MVT::all_valuetypes())
793  setLoadExtAction(ISD::EXTLOAD, InnerVT, VT, Expand);
794 
795  // CNT supports only B element sizes.
796  if (VT != MVT::v8i8 && VT != MVT::v16i8)
798 
804 
807 
808  if (!VT.isFloatingPoint())
810 
811  // [SU][MIN|MAX] are available for all NEON types apart from i64.
812  if (!VT.isFloatingPoint() && VT != MVT::v2i64 && VT != MVT::v1i64)
813  for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
814  setOperationAction(Opcode, VT, Legal);
815 
816  // F[MIN|MAX][NUM|NAN] are available for all FP NEON types.
817  if (VT.isFloatingPoint() &&
818  (VT.getVectorElementType() != MVT::f16 || Subtarget->hasFullFP16()))
819  for (unsigned Opcode : {ISD::FMINNAN, ISD::FMAXNAN,
821  setOperationAction(Opcode, VT, Legal);
822 
823  if (Subtarget->isLittleEndian()) {
824  for (unsigned im = (unsigned)ISD::PRE_INC;
828  }
829  }
830 }
831 
832 void AArch64TargetLowering::addDRTypeForNEON(MVT VT) {
833  addRegisterClass(VT, &AArch64::FPR64RegClass);
834  addTypeForNEON(VT, MVT::v2i32);
835 }
836 
837 void AArch64TargetLowering::addQRTypeForNEON(MVT VT) {
838  addRegisterClass(VT, &AArch64::FPR128RegClass);
839  addTypeForNEON(VT, MVT::v4i32);
840 }
841 
843  EVT VT) const {
844  if (!VT.isVector())
845  return MVT::i32;
847 }
848 
849 static bool optimizeLogicalImm(SDValue Op, unsigned Size, uint64_t Imm,
850  const APInt &Demanded,
852  unsigned NewOpc) {
853  uint64_t OldImm = Imm, NewImm, Enc;
854  uint64_t Mask = ((uint64_t)(-1LL) >> (64 - Size)), OrigMask = Mask;
855 
856  // Return if the immediate is already all zeros, all ones, a bimm32 or a
857  // bimm64.
858  if (Imm == 0 || Imm == Mask ||
859  AArch64_AM::isLogicalImmediate(Imm & Mask, Size))
860  return false;
861 
862  unsigned EltSize = Size;
863  uint64_t DemandedBits = Demanded.getZExtValue();
864 
865  // Clear bits that are not demanded.
866  Imm &= DemandedBits;
867 
868  while (true) {
869  // The goal here is to set the non-demanded bits in a way that minimizes
870  // the number of switching between 0 and 1. In order to achieve this goal,
871  // we set the non-demanded bits to the value of the preceding demanded bits.
872  // For example, if we have an immediate 0bx10xx0x1 ('x' indicates a
873  // non-demanded bit), we copy bit0 (1) to the least significant 'x',
874  // bit2 (0) to 'xx', and bit6 (1) to the most significant 'x'.
875  // The final result is 0b11000011.
876  uint64_t NonDemandedBits = ~DemandedBits;
877  uint64_t InvertedImm = ~Imm & DemandedBits;
878  uint64_t RotatedImm =
879  ((InvertedImm << 1) | (InvertedImm >> (EltSize - 1) & 1)) &
880  NonDemandedBits;
881  uint64_t Sum = RotatedImm + NonDemandedBits;
882  bool Carry = NonDemandedBits & ~Sum & (1ULL << (EltSize - 1));
883  uint64_t Ones = (Sum + Carry) & NonDemandedBits;
884  NewImm = (Imm | Ones) & Mask;
885 
886  // If NewImm or its bitwise NOT is a shifted mask, it is a bitmask immediate
887  // or all-ones or all-zeros, in which case we can stop searching. Otherwise,
888  // we halve the element size and continue the search.
889  if (isShiftedMask_64(NewImm) || isShiftedMask_64(~(NewImm | ~Mask)))
890  break;
891 
892  // We cannot shrink the element size any further if it is 2-bits.
893  if (EltSize == 2)
894  return false;
895 
896  EltSize /= 2;
897  Mask >>= EltSize;
898  uint64_t Hi = Imm >> EltSize, DemandedBitsHi = DemandedBits >> EltSize;
899 
900  // Return if there is mismatch in any of the demanded bits of Imm and Hi.
901  if (((Imm ^ Hi) & (DemandedBits & DemandedBitsHi) & Mask) != 0)
902  return false;
903 
904  // Merge the upper and lower halves of Imm and DemandedBits.
905  Imm |= Hi;
906  DemandedBits |= DemandedBitsHi;
907  }
908 
909  ++NumOptimizedImms;
910 
911  // Replicate the element across the register width.
912  while (EltSize < Size) {
913  NewImm |= NewImm << EltSize;
914  EltSize *= 2;
915  }
916 
917  (void)OldImm;
918  assert(((OldImm ^ NewImm) & Demanded.getZExtValue()) == 0 &&
919  "demanded bits should never be altered");
920  assert(OldImm != NewImm && "the new imm shouldn't be equal to the old imm");
921 
922  // Create the new constant immediate node.
923  EVT VT = Op.getValueType();
924  SDLoc DL(Op);
925  SDValue New;
926 
927  // If the new constant immediate is all-zeros or all-ones, let the target
928  // independent DAG combine optimize this node.
929  if (NewImm == 0 || NewImm == OrigMask) {
930  New = TLO.DAG.getNode(Op.getOpcode(), DL, VT, Op.getOperand(0),
931  TLO.DAG.getConstant(NewImm, DL, VT));
932  // Otherwise, create a machine node so that target independent DAG combine
933  // doesn't undo this optimization.
934  } else {
935  Enc = AArch64_AM::encodeLogicalImmediate(NewImm, Size);
936  SDValue EncConst = TLO.DAG.getTargetConstant(Enc, DL, VT);
937  New = SDValue(
938  TLO.DAG.getMachineNode(NewOpc, DL, VT, Op.getOperand(0), EncConst), 0);
939  }
940 
941  return TLO.CombineTo(Op, New);
942 }
943 
945  SDValue Op, const APInt &Demanded, TargetLoweringOpt &TLO) const {
946  // Delay this optimization to as late as possible.
947  if (!TLO.LegalOps)
948  return false;
949 
951  return false;
952 
953  EVT VT = Op.getValueType();
954  if (VT.isVector())
955  return false;
956 
957  unsigned Size = VT.getSizeInBits();
958  assert((Size == 32 || Size == 64) &&
959  "i32 or i64 is expected after legalization.");
960 
961  // Exit early if we demand all bits.
962  if (Demanded.countPopulation() == Size)
963  return false;
964 
965  unsigned NewOpc;
966  switch (Op.getOpcode()) {
967  default:
968  return false;
969  case ISD::AND:
970  NewOpc = Size == 32 ? AArch64::ANDWri : AArch64::ANDXri;
971  break;
972  case ISD::OR:
973  NewOpc = Size == 32 ? AArch64::ORRWri : AArch64::ORRXri;
974  break;
975  case ISD::XOR:
976  NewOpc = Size == 32 ? AArch64::EORWri : AArch64::EORXri;
977  break;
978  }
980  if (!C)
981  return false;
982  uint64_t Imm = C->getZExtValue();
983  return optimizeLogicalImm(Op, Size, Imm, Demanded, TLO, NewOpc);
984 }
985 
986 /// computeKnownBitsForTargetNode - Determine which of the bits specified in
987 /// Mask are known to be either zero or one and return them Known.
989  const SDValue Op, KnownBits &Known,
990  const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const {
991  switch (Op.getOpcode()) {
992  default:
993  break;
994  case AArch64ISD::CSEL: {
995  KnownBits Known2;
996  DAG.computeKnownBits(Op->getOperand(0), Known, Depth + 1);
997  DAG.computeKnownBits(Op->getOperand(1), Known2, Depth + 1);
998  Known.Zero &= Known2.Zero;
999  Known.One &= Known2.One;
1000  break;
1001  }
1002  case ISD::INTRINSIC_W_CHAIN: {
1003  ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
1004  Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
1005  switch (IntID) {
1006  default: return;
1007  case Intrinsic::aarch64_ldaxr:
1008  case Intrinsic::aarch64_ldxr: {
1009  unsigned BitWidth = Known.getBitWidth();
1010  EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
1011  unsigned MemBits = VT.getScalarSizeInBits();
1012  Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
1013  return;
1014  }
1015  }
1016  break;
1017  }
1019  case ISD::INTRINSIC_VOID: {
1020  unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1021  switch (IntNo) {
1022  default:
1023  break;
1024  case Intrinsic::aarch64_neon_umaxv:
1025  case Intrinsic::aarch64_neon_uminv: {
1026  // Figure out the datatype of the vector operand. The UMINV instruction
1027  // will zero extend the result, so we can mark as known zero all the
1028  // bits larger than the element datatype. 32-bit or larget doesn't need
1029  // this as those are legal types and will be handled by isel directly.
1030  MVT VT = Op.getOperand(1).getValueType().getSimpleVT();
1031  unsigned BitWidth = Known.getBitWidth();
1032  if (VT == MVT::v8i8 || VT == MVT::v16i8) {
1033  assert(BitWidth >= 8 && "Unexpected width!");
1034  APInt Mask = APInt::getHighBitsSet(BitWidth, BitWidth - 8);
1035  Known.Zero |= Mask;
1036  } else if (VT == MVT::v4i16 || VT == MVT::v8i16) {
1037  assert(BitWidth >= 16 && "Unexpected width!");
1038  APInt Mask = APInt::getHighBitsSet(BitWidth, BitWidth - 16);
1039  Known.Zero |= Mask;
1040  }
1041  break;
1042  } break;
1043  }
1044  }
1045  }
1046 }
1047 
1049  EVT) const {
1050  return MVT::i64;
1051 }
1052 
1054  unsigned AddrSpace,
1055  unsigned Align,
1056  bool *Fast) const {
1057  if (Subtarget->requiresStrictAlign())
1058  return false;
1059 
1060  if (Fast) {
1061  // Some CPUs are fine with unaligned stores except for 128-bit ones.
1062  *Fast = !Subtarget->isMisaligned128StoreSlow() || VT.getStoreSize() != 16 ||
1063  // See comments in performSTORECombine() for more details about
1064  // these conditions.
1065 
1066  // Code that uses clang vector extensions can mark that it
1067  // wants unaligned accesses to be treated as fast by
1068  // underspecifying alignment to be 1 or 2.
1069  Align <= 2 ||
1070 
1071  // Disregard v2i64. Memcpy lowering produces those and splitting
1072  // them regresses performance on micro-benchmarks and olden/bh.
1073  VT == MVT::v2i64;
1074  }
1075  return true;
1076 }
1077 
1078 FastISel *
1080  const TargetLibraryInfo *libInfo) const {
1081  return AArch64::createFastISel(funcInfo, libInfo);
1082 }
1083 
1084 const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const {
1085  switch ((AArch64ISD::NodeType)Opcode) {
1086  case AArch64ISD::FIRST_NUMBER: break;
1087  case AArch64ISD::CALL: return "AArch64ISD::CALL";
1088  case AArch64ISD::ADRP: return "AArch64ISD::ADRP";
1089  case AArch64ISD::ADDlow: return "AArch64ISD::ADDlow";
1090  case AArch64ISD::LOADgot: return "AArch64ISD::LOADgot";
1091  case AArch64ISD::RET_FLAG: return "AArch64ISD::RET_FLAG";
1092  case AArch64ISD::BRCOND: return "AArch64ISD::BRCOND";
1093  case AArch64ISD::CSEL: return "AArch64ISD::CSEL";
1094  case AArch64ISD::FCSEL: return "AArch64ISD::FCSEL";
1095  case AArch64ISD::CSINV: return "AArch64ISD::CSINV";
1096  case AArch64ISD::CSNEG: return "AArch64ISD::CSNEG";
1097  case AArch64ISD::CSINC: return "AArch64ISD::CSINC";
1098  case AArch64ISD::THREAD_POINTER: return "AArch64ISD::THREAD_POINTER";
1099  case AArch64ISD::TLSDESC_CALLSEQ: return "AArch64ISD::TLSDESC_CALLSEQ";
1100  case AArch64ISD::ADC: return "AArch64ISD::ADC";
1101  case AArch64ISD::SBC: return "AArch64ISD::SBC";
1102  case AArch64ISD::ADDS: return "AArch64ISD::ADDS";
1103  case AArch64ISD::SUBS: return "AArch64ISD::SUBS";
1104  case AArch64ISD::ADCS: return "AArch64ISD::ADCS";
1105  case AArch64ISD::SBCS: return "AArch64ISD::SBCS";
1106  case AArch64ISD::ANDS: return "AArch64ISD::ANDS";
1107  case AArch64ISD::CCMP: return "AArch64ISD::CCMP";
1108  case AArch64ISD::CCMN: return "AArch64ISD::CCMN";
1109  case AArch64ISD::FCCMP: return "AArch64ISD::FCCMP";
1110  case AArch64ISD::FCMP: return "AArch64ISD::FCMP";
1111  case AArch64ISD::DUP: return "AArch64ISD::DUP";
1112  case AArch64ISD::DUPLANE8: return "AArch64ISD::DUPLANE8";
1113  case AArch64ISD::DUPLANE16: return "AArch64ISD::DUPLANE16";
1114  case AArch64ISD::DUPLANE32: return "AArch64ISD::DUPLANE32";
1115  case AArch64ISD::DUPLANE64: return "AArch64ISD::DUPLANE64";
1116  case AArch64ISD::MOVI: return "AArch64ISD::MOVI";
1117  case AArch64ISD::MOVIshift: return "AArch64ISD::MOVIshift";
1118  case AArch64ISD::MOVIedit: return "AArch64ISD::MOVIedit";
1119  case AArch64ISD::MOVImsl: return "AArch64ISD::MOVImsl";
1120  case AArch64ISD::FMOV: return "AArch64ISD::FMOV";
1121  case AArch64ISD::MVNIshift: return "AArch64ISD::MVNIshift";
1122  case AArch64ISD::MVNImsl: return "AArch64ISD::MVNImsl";
1123  case AArch64ISD::BICi: return "AArch64ISD::BICi";
1124  case AArch64ISD::ORRi: return "AArch64ISD::ORRi";
1125  case AArch64ISD::BSL: return "AArch64ISD::BSL";
1126  case AArch64ISD::NEG: return "AArch64ISD::NEG";
1127  case AArch64ISD::EXTR: return "AArch64ISD::EXTR";
1128  case AArch64ISD::ZIP1: return "AArch64ISD::ZIP1";
1129  case AArch64ISD::ZIP2: return "AArch64ISD::ZIP2";
1130  case AArch64ISD::UZP1: return "AArch64ISD::UZP1";
1131  case AArch64ISD::UZP2: return "AArch64ISD::UZP2";
1132  case AArch64ISD::TRN1: return "AArch64ISD::TRN1";
1133  case AArch64ISD::TRN2: return "AArch64ISD::TRN2";
1134  case AArch64ISD::REV16: return "AArch64ISD::REV16";
1135  case AArch64ISD::REV32: return "AArch64ISD::REV32";
1136  case AArch64ISD::REV64: return "AArch64ISD::REV64";
1137  case AArch64ISD::EXT: return "AArch64ISD::EXT";
1138  case AArch64ISD::VSHL: return "AArch64ISD::VSHL";
1139  case AArch64ISD::VLSHR: return "AArch64ISD::VLSHR";
1140  case AArch64ISD::VASHR: return "AArch64ISD::VASHR";
1141  case AArch64ISD::CMEQ: return "AArch64ISD::CMEQ";
1142  case AArch64ISD::CMGE: return "AArch64ISD::CMGE";
1143  case AArch64ISD::CMGT: return "AArch64ISD::CMGT";
1144  case AArch64ISD::CMHI: return "AArch64ISD::CMHI";
1145  case AArch64ISD::CMHS: return "AArch64ISD::CMHS";
1146  case AArch64ISD::FCMEQ: return "AArch64ISD::FCMEQ";
1147  case AArch64ISD::FCMGE: return "AArch64ISD::FCMGE";
1148  case AArch64ISD::FCMGT: return "AArch64ISD::FCMGT";
1149  case AArch64ISD::CMEQz: return "AArch64ISD::CMEQz";
1150  case AArch64ISD::CMGEz: return "AArch64ISD::CMGEz";
1151  case AArch64ISD::CMGTz: return "AArch64ISD::CMGTz";
1152  case AArch64ISD::CMLEz: return "AArch64ISD::CMLEz";
1153  case AArch64ISD::CMLTz: return "AArch64ISD::CMLTz";
1154  case AArch64ISD::FCMEQz: return "AArch64ISD::FCMEQz";
1155  case AArch64ISD::FCMGEz: return "AArch64ISD::FCMGEz";
1156  case AArch64ISD::FCMGTz: return "AArch64ISD::FCMGTz";
1157  case AArch64ISD::FCMLEz: return "AArch64ISD::FCMLEz";
1158  case AArch64ISD::FCMLTz: return "AArch64ISD::FCMLTz";
1159  case AArch64ISD::SADDV: return "AArch64ISD::SADDV";
1160  case AArch64ISD::UADDV: return "AArch64ISD::UADDV";
1161  case AArch64ISD::SMINV: return "AArch64ISD::SMINV";
1162  case AArch64ISD::UMINV: return "AArch64ISD::UMINV";
1163  case AArch64ISD::SMAXV: return "AArch64ISD::SMAXV";
1164  case AArch64ISD::UMAXV: return "AArch64ISD::UMAXV";
1165  case AArch64ISD::NOT: return "AArch64ISD::NOT";
1166  case AArch64ISD::BIT: return "AArch64ISD::BIT";
1167  case AArch64ISD::CBZ: return "AArch64ISD::CBZ";
1168  case AArch64ISD::CBNZ: return "AArch64ISD::CBNZ";
1169  case AArch64ISD::TBZ: return "AArch64ISD::TBZ";
1170  case AArch64ISD::TBNZ: return "AArch64ISD::TBNZ";
1171  case AArch64ISD::TC_RETURN: return "AArch64ISD::TC_RETURN";
1172  case AArch64ISD::PREFETCH: return "AArch64ISD::PREFETCH";
1173  case AArch64ISD::SITOF: return "AArch64ISD::SITOF";
1174  case AArch64ISD::UITOF: return "AArch64ISD::UITOF";
1175  case AArch64ISD::NVCAST: return "AArch64ISD::NVCAST";
1176  case AArch64ISD::SQSHL_I: return "AArch64ISD::SQSHL_I";
1177  case AArch64ISD::UQSHL_I: return "AArch64ISD::UQSHL_I";
1178  case AArch64ISD::SRSHR_I: return "AArch64ISD::SRSHR_I";
1179  case AArch64ISD::URSHR_I: return "AArch64ISD::URSHR_I";
1180  case AArch64ISD::SQSHLU_I: return "AArch64ISD::SQSHLU_I";
1181  case AArch64ISD::WrapperLarge: return "AArch64ISD::WrapperLarge";
1182  case AArch64ISD::LD2post: return "AArch64ISD::LD2post";
1183  case AArch64ISD::LD3post: return "AArch64ISD::LD3post";
1184  case AArch64ISD::LD4post: return "AArch64ISD::LD4post";
1185  case AArch64ISD::ST2post: return "AArch64ISD::ST2post";
1186  case AArch64ISD::ST3post: return "AArch64ISD::ST3post";
1187  case AArch64ISD::ST4post: return "AArch64ISD::ST4post";
1188  case AArch64ISD::LD1x2post: return "AArch64ISD::LD1x2post";
1189  case AArch64ISD::LD1x3post: return "AArch64ISD::LD1x3post";
1190  case AArch64ISD::LD1x4post: return "AArch64ISD::LD1x4post";
1191  case AArch64ISD::ST1x2post: return "AArch64ISD::ST1x2post";
1192  case AArch64ISD::ST1x3post: return "AArch64ISD::ST1x3post";
1193  case AArch64ISD::ST1x4post: return "AArch64ISD::ST1x4post";
1194  case AArch64ISD::LD1DUPpost: return "AArch64ISD::LD1DUPpost";
1195  case AArch64ISD::LD2DUPpost: return "AArch64ISD::LD2DUPpost";
1196  case AArch64ISD::LD3DUPpost: return "AArch64ISD::LD3DUPpost";
1197  case AArch64ISD::LD4DUPpost: return "AArch64ISD::LD4DUPpost";
1198  case AArch64ISD::LD1LANEpost: return "AArch64ISD::LD1LANEpost";
1199  case AArch64ISD::LD2LANEpost: return "AArch64ISD::LD2LANEpost";
1200  case AArch64ISD::LD3LANEpost: return "AArch64ISD::LD3LANEpost";
1201  case AArch64ISD::LD4LANEpost: return "AArch64ISD::LD4LANEpost";
1202  case AArch64ISD::ST2LANEpost: return "AArch64ISD::ST2LANEpost";
1203  case AArch64ISD::ST3LANEpost: return "AArch64ISD::ST3LANEpost";
1204  case AArch64ISD::ST4LANEpost: return "AArch64ISD::ST4LANEpost";
1205  case AArch64ISD::SMULL: return "AArch64ISD::SMULL";
1206  case AArch64ISD::UMULL: return "AArch64ISD::UMULL";
1207  case AArch64ISD::FRECPE: return "AArch64ISD::FRECPE";
1208  case AArch64ISD::FRECPS: return "AArch64ISD::FRECPS";
1209  case AArch64ISD::FRSQRTE: return "AArch64ISD::FRSQRTE";
1210  case AArch64ISD::FRSQRTS: return "AArch64ISD::FRSQRTS";
1211  }
1212  return nullptr;
1213 }
1214 
1217  MachineBasicBlock *MBB) const {
1218  // We materialise the F128CSEL pseudo-instruction as some control flow and a
1219  // phi node:
1220 
1221  // OrigBB:
1222  // [... previous instrs leading to comparison ...]
1223  // b.ne TrueBB
1224  // b EndBB
1225  // TrueBB:
1226  // ; Fallthrough
1227  // EndBB:
1228  // Dest = PHI [IfTrue, TrueBB], [IfFalse, OrigBB]
1229 
1230  MachineFunction *MF = MBB->getParent();
1231  const TargetInstrInfo *TII = Subtarget->getInstrInfo();
1232  const BasicBlock *LLVM_BB = MBB->getBasicBlock();
1233  DebugLoc DL = MI.getDebugLoc();
1234  MachineFunction::iterator It = ++MBB->getIterator();
1235 
1236  unsigned DestReg = MI.getOperand(0).getReg();
1237  unsigned IfTrueReg = MI.getOperand(1).getReg();
1238  unsigned IfFalseReg = MI.getOperand(2).getReg();
1239  unsigned CondCode = MI.getOperand(3).getImm();
1240  bool NZCVKilled = MI.getOperand(4).isKill();
1241 
1242  MachineBasicBlock *TrueBB = MF->CreateMachineBasicBlock(LLVM_BB);
1243  MachineBasicBlock *EndBB = MF->CreateMachineBasicBlock(LLVM_BB);
1244  MF->insert(It, TrueBB);
1245  MF->insert(It, EndBB);
1246 
1247  // Transfer rest of current basic-block to EndBB
1248  EndBB->splice(EndBB->begin(), MBB, std::next(MachineBasicBlock::iterator(MI)),
1249  MBB->end());
1250  EndBB->transferSuccessorsAndUpdatePHIs(MBB);
1251 
1252  BuildMI(MBB, DL, TII->get(AArch64::Bcc)).addImm(CondCode).addMBB(TrueBB);
1253  BuildMI(MBB, DL, TII->get(AArch64::B)).addMBB(EndBB);
1254  MBB->addSuccessor(TrueBB);
1255  MBB->addSuccessor(EndBB);
1256 
1257  // TrueBB falls through to the end.
1258  TrueBB->addSuccessor(EndBB);
1259 
1260  if (!NZCVKilled) {
1261  TrueBB->addLiveIn(AArch64::NZCV);
1262  EndBB->addLiveIn(AArch64::NZCV);
1263  }
1264 
1265  BuildMI(*EndBB, EndBB->begin(), DL, TII->get(AArch64::PHI), DestReg)
1266  .addReg(IfTrueReg)
1267  .addMBB(TrueBB)
1268  .addReg(IfFalseReg)
1269  .addMBB(MBB);
1270 
1271  MI.eraseFromParent();
1272  return EndBB;
1273 }
1274 
1276  MachineInstr &MI, MachineBasicBlock *BB) const {
1277  switch (MI.getOpcode()) {
1278  default:
1279 #ifndef NDEBUG
1280  MI.dump();
1281 #endif
1282  llvm_unreachable("Unexpected instruction for custom inserter!");
1283 
1284  case AArch64::F128CSEL:
1285  return EmitF128CSEL(MI, BB);
1286 
1287  case TargetOpcode::STACKMAP:
1288  case TargetOpcode::PATCHPOINT:
1289  return emitPatchPoint(MI, BB);
1290  }
1291 }
1292 
1293 //===----------------------------------------------------------------------===//
1294 // AArch64 Lowering private implementation.
1295 //===----------------------------------------------------------------------===//
1296 
1297 //===----------------------------------------------------------------------===//
1298 // Lowering Code
1299 //===----------------------------------------------------------------------===//
1300 
1301 /// changeIntCCToAArch64CC - Convert a DAG integer condition code to an AArch64
1302 /// CC
1304  switch (CC) {
1305  default:
1306  llvm_unreachable("Unknown condition code!");
1307  case ISD::SETNE:
1308  return AArch64CC::NE;
1309  case ISD::SETEQ:
1310  return AArch64CC::EQ;
1311  case ISD::SETGT:
1312  return AArch64CC::GT;
1313  case ISD::SETGE:
1314  return AArch64CC::GE;
1315  case ISD::SETLT:
1316  return AArch64CC::LT;
1317  case ISD::SETLE:
1318  return AArch64CC::LE;
1319  case ISD::SETUGT:
1320  return AArch64CC::HI;
1321  case ISD::SETUGE:
1322  return AArch64CC::HS;
1323  case ISD::SETULT:
1324  return AArch64CC::LO;
1325  case ISD::SETULE:
1326  return AArch64CC::LS;
1327  }
1328 }
1329 
1330 /// changeFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64 CC.
1333  AArch64CC::CondCode &CondCode2) {
1334  CondCode2 = AArch64CC::AL;
1335  switch (CC) {
1336  default:
1337  llvm_unreachable("Unknown FP condition!");
1338  case ISD::SETEQ:
1339  case ISD::SETOEQ:
1340  CondCode = AArch64CC::EQ;
1341  break;
1342  case ISD::SETGT:
1343  case ISD::SETOGT:
1344  CondCode = AArch64CC::GT;
1345  break;
1346  case ISD::SETGE:
1347  case ISD::SETOGE:
1348  CondCode = AArch64CC::GE;
1349  break;
1350  case ISD::SETOLT:
1351  CondCode = AArch64CC::MI;
1352  break;
1353  case ISD::SETOLE:
1354  CondCode = AArch64CC::LS;
1355  break;
1356  case ISD::SETONE:
1357  CondCode = AArch64CC::MI;
1358  CondCode2 = AArch64CC::GT;
1359  break;
1360  case ISD::SETO:
1361  CondCode = AArch64CC::VC;
1362  break;
1363  case ISD::SETUO:
1364  CondCode = AArch64CC::VS;
1365  break;
1366  case ISD::SETUEQ:
1367  CondCode = AArch64CC::EQ;
1368  CondCode2 = AArch64CC::VS;
1369  break;
1370  case ISD::SETUGT:
1371  CondCode = AArch64CC::HI;
1372  break;
1373  case ISD::SETUGE:
1374  CondCode = AArch64CC::PL;
1375  break;
1376  case ISD::SETLT:
1377  case ISD::SETULT:
1378  CondCode = AArch64CC::LT;
1379  break;
1380  case ISD::SETLE:
1381  case ISD::SETULE:
1382  CondCode = AArch64CC::LE;
1383  break;
1384  case ISD::SETNE:
1385  case ISD::SETUNE:
1386  CondCode = AArch64CC::NE;
1387  break;
1388  }
1389 }
1390 
1391 /// Convert a DAG fp condition code to an AArch64 CC.
1392 /// This differs from changeFPCCToAArch64CC in that it returns cond codes that
1393 /// should be AND'ed instead of OR'ed.
1396  AArch64CC::CondCode &CondCode2) {
1397  CondCode2 = AArch64CC::AL;
1398  switch (CC) {
1399  default:
1400  changeFPCCToAArch64CC(CC, CondCode, CondCode2);
1401  assert(CondCode2 == AArch64CC::AL);
1402  break;
1403  case ISD::SETONE:
1404  // (a one b)
1405  // == ((a olt b) || (a ogt b))
1406  // == ((a ord b) && (a une b))
1407  CondCode = AArch64CC::VC;
1408  CondCode2 = AArch64CC::NE;
1409  break;
1410  case ISD::SETUEQ:
1411  // (a ueq b)
1412  // == ((a uno b) || (a oeq b))
1413  // == ((a ule b) && (a uge b))
1414  CondCode = AArch64CC::PL;
1415  CondCode2 = AArch64CC::LE;
1416  break;
1417  }
1418 }
1419 
1420 /// changeVectorFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64
1421 /// CC usable with the vector instructions. Fewer operations are available
1422 /// without a real NZCV register, so we have to use less efficient combinations
1423 /// to get the same effect.
1426  AArch64CC::CondCode &CondCode2,
1427  bool &Invert) {
1428  Invert = false;
1429  switch (CC) {
1430  default:
1431  // Mostly the scalar mappings work fine.
1432  changeFPCCToAArch64CC(CC, CondCode, CondCode2);
1433  break;
1434  case ISD::SETUO:
1435  Invert = true;
1437  case ISD::SETO:
1438  CondCode = AArch64CC::MI;
1439  CondCode2 = AArch64CC::GE;
1440  break;
1441  case ISD::SETUEQ:
1442  case ISD::SETULT:
1443  case ISD::SETULE:
1444  case ISD::SETUGT:
1445  case ISD::SETUGE:
1446  // All of the compare-mask comparisons are ordered, but we can switch
1447  // between the two by a double inversion. E.g. ULE == !OGT.
1448  Invert = true;
1449  changeFPCCToAArch64CC(getSetCCInverse(CC, false), CondCode, CondCode2);
1450  break;
1451  }
1452 }
1453 
1454 static bool isLegalArithImmed(uint64_t C) {
1455  // Matches AArch64DAGToDAGISel::SelectArithImmed().
1456  bool IsLegal = (C >> 12 == 0) || ((C & 0xFFFULL) == 0 && C >> 24 == 0);
1457  LLVM_DEBUG(dbgs() << "Is imm " << C
1458  << " legal: " << (IsLegal ? "yes\n" : "no\n"));
1459  return IsLegal;
1460 }
1461 
1463  const SDLoc &dl, SelectionDAG &DAG) {
1464  EVT VT = LHS.getValueType();
1465  const bool FullFP16 =
1466  static_cast<const AArch64Subtarget &>(DAG.getSubtarget()).hasFullFP16();
1467 
1468  if (VT.isFloatingPoint()) {
1469  assert(VT != MVT::f128);
1470  if (VT == MVT::f16 && !FullFP16) {
1471  LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, LHS);
1472  RHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, RHS);
1473  VT = MVT::f32;
1474  }
1475  return DAG.getNode(AArch64ISD::FCMP, dl, VT, LHS, RHS);
1476  }
1477 
1478  // The CMP instruction is just an alias for SUBS, and representing it as
1479  // SUBS means that it's possible to get CSE with subtract operations.
1480  // A later phase can perform the optimization of setting the destination
1481  // register to WZR/XZR if it ends up being unused.
1482  unsigned Opcode = AArch64ISD::SUBS;
1483 
1484  if (RHS.getOpcode() == ISD::SUB && isNullConstant(RHS.getOperand(0)) &&
1485  (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1486  // We'd like to combine a (CMP op1, (sub 0, op2) into a CMN instruction on
1487  // the grounds that "op1 - (-op2) == op1 + op2". However, the C and V flags
1488  // can be set differently by this operation. It comes down to whether
1489  // "SInt(~op2)+1 == SInt(~op2+1)" (and the same for UInt). If they are then
1490  // everything is fine. If not then the optimization is wrong. Thus general
1491  // comparisons are only valid if op2 != 0.
1492 
1493  // So, finally, the only LLVM-native comparisons that don't mention C and V
1494  // are SETEQ and SETNE. They're the only ones we can safely use CMN for in
1495  // the absence of information about op2.
1496  Opcode = AArch64ISD::ADDS;
1497  RHS = RHS.getOperand(1);
1498  } else if (LHS.getOpcode() == ISD::AND && isNullConstant(RHS) &&
1499  !isUnsignedIntSetCC(CC)) {
1500  // Similarly, (CMP (and X, Y), 0) can be implemented with a TST
1501  // (a.k.a. ANDS) except that the flags are only guaranteed to work for one
1502  // of the signed comparisons.
1503  Opcode = AArch64ISD::ANDS;
1504  RHS = LHS.getOperand(1);
1505  LHS = LHS.getOperand(0);
1506  }
1507 
1508  return DAG.getNode(Opcode, dl, DAG.getVTList(VT, MVT_CC), LHS, RHS)
1509  .getValue(1);
1510 }
1511 
1512 /// \defgroup AArch64CCMP CMP;CCMP matching
1513 ///
1514 /// These functions deal with the formation of CMP;CCMP;... sequences.
1515 /// The CCMP/CCMN/FCCMP/FCCMPE instructions allow the conditional execution of
1516 /// a comparison. They set the NZCV flags to a predefined value if their
1517 /// predicate is false. This allows to express arbitrary conjunctions, for
1518 /// example "cmp 0 (and (setCA (cmp A)) (setCB (cmp B))))"
1519 /// expressed as:
1520 /// cmp A
1521 /// ccmp B, inv(CB), CA
1522 /// check for CB flags
1523 ///
1524 /// In general we can create code for arbitrary "... (and (and A B) C)"
1525 /// sequences. We can also implement some "or" expressions, because "(or A B)"
1526 /// is equivalent to "not (and (not A) (not B))" and we can implement some
1527 /// negation operations:
1528 /// We can negate the results of a single comparison by inverting the flags
1529 /// used when the predicate fails and inverting the flags tested in the next
1530 /// instruction; We can also negate the results of the whole previous
1531 /// conditional compare sequence by inverting the flags tested in the next
1532 /// instruction. However there is no way to negate the result of a partial
1533 /// sequence.
1534 ///
1535 /// Therefore on encountering an "or" expression we can negate the subtree on
1536 /// one side and have to be able to push the negate to the leafs of the subtree
1537 /// on the other side (see also the comments in code). As complete example:
1538 /// "or (or (setCA (cmp A)) (setCB (cmp B)))
1539 /// (and (setCC (cmp C)) (setCD (cmp D)))"
1540 /// is transformed to
1541 /// "not (and (not (and (setCC (cmp C)) (setCC (cmp D))))
1542 /// (and (not (setCA (cmp A)) (not (setCB (cmp B))))))"
1543 /// and implemented as:
1544 /// cmp C
1545 /// ccmp D, inv(CD), CC
1546 /// ccmp A, CA, inv(CD)
1547 /// ccmp B, CB, inv(CA)
1548 /// check for CB flags
1549 /// A counterexample is "or (and A B) (and C D)" which cannot be implemented
1550 /// by conditional compare sequences.
1551 /// @{
1552 
1553 /// Create a conditional comparison; Use CCMP, CCMN or FCCMP as appropriate.
1555  ISD::CondCode CC, SDValue CCOp,
1557  AArch64CC::CondCode OutCC,
1558  const SDLoc &DL, SelectionDAG &DAG) {
1559  unsigned Opcode = 0;
1560  const bool FullFP16 =
1561  static_cast<const AArch64Subtarget &>(DAG.getSubtarget()).hasFullFP16();
1562 
1563  if (LHS.getValueType().isFloatingPoint()) {
1564  assert(LHS.getValueType() != MVT::f128);
1565  if (LHS.getValueType() == MVT::f16 && !FullFP16) {
1566  LHS = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, LHS);
1567  RHS = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, RHS);
1568  }
1569  Opcode = AArch64ISD::FCCMP;
1570  } else if (RHS.getOpcode() == ISD::SUB) {
1571  SDValue SubOp0 = RHS.getOperand(0);
1572  if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1573  // See emitComparison() on why we can only do this for SETEQ and SETNE.
1574  Opcode = AArch64ISD::CCMN;
1575  RHS = RHS.getOperand(1);
1576  }
1577  }
1578  if (Opcode == 0)
1579  Opcode = AArch64ISD::CCMP;
1580 
1581  SDValue Condition = DAG.getConstant(Predicate, DL, MVT_CC);
1583  unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(InvOutCC);
1584  SDValue NZCVOp = DAG.getConstant(NZCV, DL, MVT::i32);
1585  return DAG.getNode(Opcode, DL, MVT_CC, LHS, RHS, NZCVOp, Condition, CCOp);
1586 }
1587 
1588 /// Returns true if @p Val is a tree of AND/OR/SETCC operations.
1589 /// CanPushNegate is set to true if we can push a negate operation through
1590 /// the tree in a was that we are left with AND operations and negate operations
1591 /// at the leafs only. i.e. "not (or (or x y) z)" can be changed to
1592 /// "and (and (not x) (not y)) (not z)"; "not (or (and x y) z)" cannot be
1593 /// brought into such a form.
1594 static bool isConjunctionDisjunctionTree(const SDValue Val, bool &CanNegate,
1595  unsigned Depth = 0) {
1596  if (!Val.hasOneUse())
1597  return false;
1598  unsigned Opcode = Val->getOpcode();
1599  if (Opcode == ISD::SETCC) {
1600  if (Val->getOperand(0).getValueType() == MVT::f128)
1601  return false;
1602  CanNegate = true;
1603  return true;
1604  }
1605  // Protect against exponential runtime and stack overflow.
1606  if (Depth > 6)
1607  return false;
1608  if (Opcode == ISD::AND || Opcode == ISD::OR) {
1609  SDValue O0 = Val->getOperand(0);
1610  SDValue O1 = Val->getOperand(1);
1611  bool CanNegateL;
1612  if (!isConjunctionDisjunctionTree(O0, CanNegateL, Depth+1))
1613  return false;
1614  bool CanNegateR;
1615  if (!isConjunctionDisjunctionTree(O1, CanNegateR, Depth+1))
1616  return false;
1617 
1618  if (Opcode == ISD::OR) {
1619  // For an OR expression we need to be able to negate at least one side or
1620  // we cannot do the transformation at all.
1621  if (!CanNegateL && !CanNegateR)
1622  return false;
1623  // We can however change a (not (or x y)) to (and (not x) (not y)) if we
1624  // can negate the x and y subtrees.
1625  CanNegate = CanNegateL && CanNegateR;
1626  } else {
1627  // If the operands are OR expressions then we finally need to negate their
1628  // outputs, we can only do that for the operand with emitted last by
1629  // negating OutCC, not for both operands.
1630  bool NeedsNegOutL = O0->getOpcode() == ISD::OR;
1631  bool NeedsNegOutR = O1->getOpcode() == ISD::OR;
1632  if (NeedsNegOutL && NeedsNegOutR)
1633  return false;
1634  // We cannot negate an AND operation (it would become an OR),
1635  CanNegate = false;
1636  }
1637  return true;
1638  }
1639  return false;
1640 }
1641 
1642 /// Emit conjunction or disjunction tree with the CMP/FCMP followed by a chain
1643 /// of CCMP/CFCMP ops. See @ref AArch64CCMP.
1644 /// Tries to transform the given i1 producing node @p Val to a series compare
1645 /// and conditional compare operations. @returns an NZCV flags producing node
1646 /// and sets @p OutCC to the flags that should be tested or returns SDValue() if
1647 /// transformation was not possible.
1648 /// On recursive invocations @p PushNegate may be set to true to have negation
1649 /// effects pushed to the tree leafs; @p Predicate is an NZCV flag predicate
1650 /// for the comparisons in the current subtree; @p Depth limits the search
1651 /// depth to avoid stack overflow.
1653  AArch64CC::CondCode &OutCC, bool Negate, SDValue CCOp,
1655  // We're at a tree leaf, produce a conditional comparison operation.
1656  unsigned Opcode = Val->getOpcode();
1657  if (Opcode == ISD::SETCC) {
1658  SDValue LHS = Val->getOperand(0);
1659  SDValue RHS = Val->getOperand(1);
1660  ISD::CondCode CC = cast<CondCodeSDNode>(Val->getOperand(2))->get();
1661  bool isInteger = LHS.getValueType().isInteger();
1662  if (Negate)
1663  CC = getSetCCInverse(CC, isInteger);
1664  SDLoc DL(Val);
1665  // Determine OutCC and handle FP special case.
1666  if (isInteger) {
1667  OutCC = changeIntCCToAArch64CC(CC);
1668  } else {
1670  AArch64CC::CondCode ExtraCC;
1671  changeFPCCToANDAArch64CC(CC, OutCC, ExtraCC);
1672  // Some floating point conditions can't be tested with a single condition
1673  // code. Construct an additional comparison in this case.
1674  if (ExtraCC != AArch64CC::AL) {
1675  SDValue ExtraCmp;
1676  if (!CCOp.getNode())
1677  ExtraCmp = emitComparison(LHS, RHS, CC, DL, DAG);
1678  else
1679  ExtraCmp = emitConditionalComparison(LHS, RHS, CC, CCOp, Predicate,
1680  ExtraCC, DL, DAG);
1681  CCOp = ExtraCmp;
1682  Predicate = ExtraCC;
1683  }
1684  }
1685 
1686  // Produce a normal comparison if we are first in the chain
1687  if (!CCOp)
1688  return emitComparison(LHS, RHS, CC, DL, DAG);
1689  // Otherwise produce a ccmp.
1690  return emitConditionalComparison(LHS, RHS, CC, CCOp, Predicate, OutCC, DL,
1691  DAG);
1692  }
1693  assert((Opcode == ISD::AND || (Opcode == ISD::OR && Val->hasOneUse())) &&
1694  "Valid conjunction/disjunction tree");
1695 
1696  // Check if both sides can be transformed.
1697  SDValue LHS = Val->getOperand(0);
1698  SDValue RHS = Val->getOperand(1);
1699 
1700  // In case of an OR we need to negate our operands and the result.
1701  // (A v B) <=> not(not(A) ^ not(B))
1702  bool NegateOpsAndResult = Opcode == ISD::OR;
1703  // We can negate the results of all previous operations by inverting the
1704  // predicate flags giving us a free negation for one side. The other side
1705  // must be negatable by itself.
1706  if (NegateOpsAndResult) {
1707  // See which side we can negate.
1708  bool CanNegateL;
1709  bool isValidL = isConjunctionDisjunctionTree(LHS, CanNegateL);
1710  assert(isValidL && "Valid conjunction/disjunction tree");
1711  (void)isValidL;
1712 
1713 #ifndef NDEBUG
1714  bool CanNegateR;
1715  bool isValidR = isConjunctionDisjunctionTree(RHS, CanNegateR);
1716  assert(isValidR && "Valid conjunction/disjunction tree");
1717  assert((CanNegateL || CanNegateR) && "Valid conjunction/disjunction tree");
1718 #endif
1719 
1720  // Order the side which we cannot negate to RHS so we can emit it first.
1721  if (!CanNegateL)
1722  std::swap(LHS, RHS);
1723  } else {
1724  bool NeedsNegOutL = LHS->getOpcode() == ISD::OR;
1725  assert((!NeedsNegOutL || RHS->getOpcode() != ISD::OR) &&
1726  "Valid conjunction/disjunction tree");
1727  // Order the side where we need to negate the output flags to RHS so it
1728  // gets emitted first.
1729  if (NeedsNegOutL)
1730  std::swap(LHS, RHS);
1731  }
1732 
1733  // Emit RHS. If we want to negate the tree we only need to push a negate
1734  // through if we are already in a PushNegate case, otherwise we can negate
1735  // the "flags to test" afterwards.
1736  AArch64CC::CondCode RHSCC;
1737  SDValue CmpR = emitConjunctionDisjunctionTreeRec(DAG, RHS, RHSCC, Negate,
1738  CCOp, Predicate);
1739  if (NegateOpsAndResult && !Negate)
1740  RHSCC = AArch64CC::getInvertedCondCode(RHSCC);
1741  // Emit LHS. We may need to negate it.
1742  SDValue CmpL = emitConjunctionDisjunctionTreeRec(DAG, LHS, OutCC,
1743  NegateOpsAndResult, CmpR,
1744  RHSCC);
1745  // If we transformed an OR to and AND then we have to negate the result
1746  // (or absorb the Negate parameter).
1747  if (NegateOpsAndResult && !Negate)
1748  OutCC = AArch64CC::getInvertedCondCode(OutCC);
1749  return CmpL;
1750 }
1751 
1752 /// Emit conjunction or disjunction tree with the CMP/FCMP followed by a chain
1753 /// of CCMP/CFCMP ops. See @ref AArch64CCMP.
1754 /// \see emitConjunctionDisjunctionTreeRec().
1756  AArch64CC::CondCode &OutCC) {
1757  bool CanNegate;
1758  if (!isConjunctionDisjunctionTree(Val, CanNegate))
1759  return SDValue();
1760 
1761  return emitConjunctionDisjunctionTreeRec(DAG, Val, OutCC, false, SDValue(),
1762  AArch64CC::AL);
1763 }
1764 
1765 /// @}
1766 
1768  SDValue &AArch64cc, SelectionDAG &DAG,
1769  const SDLoc &dl) {
1770  if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1771  EVT VT = RHS.getValueType();
1772  uint64_t C = RHSC->getZExtValue();
1773  if (!isLegalArithImmed(C)) {
1774  // Constant does not fit, try adjusting it by one?
1775  switch (CC) {
1776  default:
1777  break;
1778  case ISD::SETLT:
1779  case ISD::SETGE:
1780  if ((VT == MVT::i32 && C != 0x80000000 &&
1781  isLegalArithImmed((uint32_t)(C - 1))) ||
1782  (VT == MVT::i64 && C != 0x80000000ULL &&
1783  isLegalArithImmed(C - 1ULL))) {
1784  CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1785  C = (VT == MVT::i32) ? (uint32_t)(C - 1) : C - 1;
1786  RHS = DAG.getConstant(C, dl, VT);
1787  }
1788  break;
1789  case ISD::SETULT:
1790  case ISD::SETUGE:
1791  if ((VT == MVT::i32 && C != 0 &&
1792  isLegalArithImmed((uint32_t)(C - 1))) ||
1793  (VT == MVT::i64 && C != 0ULL && isLegalArithImmed(C - 1ULL))) {
1794  CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1795  C = (VT == MVT::i32) ? (uint32_t)(C - 1) : C - 1;
1796  RHS = DAG.getConstant(C, dl, VT);
1797  }
1798  break;
1799  case ISD::SETLE:
1800  case ISD::SETGT:
1801  if ((VT == MVT::i32 && C != INT32_MAX &&
1802  isLegalArithImmed((uint32_t)(C + 1))) ||
1803  (VT == MVT::i64 && C != INT64_MAX &&
1804  isLegalArithImmed(C + 1ULL))) {
1805  CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1806  C = (VT == MVT::i32) ? (uint32_t)(C + 1) : C + 1;
1807  RHS = DAG.getConstant(C, dl, VT);
1808  }
1809  break;
1810  case ISD::SETULE:
1811  case ISD::SETUGT:
1812  if ((VT == MVT::i32 && C != UINT32_MAX &&
1813  isLegalArithImmed((uint32_t)(C + 1))) ||
1814  (VT == MVT::i64 && C != UINT64_MAX &&
1815  isLegalArithImmed(C + 1ULL))) {
1816  CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1817  C = (VT == MVT::i32) ? (uint32_t)(C + 1) : C + 1;
1818  RHS = DAG.getConstant(C, dl, VT);
1819  }
1820  break;
1821  }
1822  }
1823  }
1824  SDValue Cmp;
1825  AArch64CC::CondCode AArch64CC;
1826  if ((CC == ISD::SETEQ || CC == ISD::SETNE) && isa<ConstantSDNode>(RHS)) {
1827  const ConstantSDNode *RHSC = cast<ConstantSDNode>(RHS);
1828 
1829  // The imm operand of ADDS is an unsigned immediate, in the range 0 to 4095.
1830  // For the i8 operand, the largest immediate is 255, so this can be easily
1831  // encoded in the compare instruction. For the i16 operand, however, the
1832  // largest immediate cannot be encoded in the compare.
1833  // Therefore, use a sign extending load and cmn to avoid materializing the
1834  // -1 constant. For example,
1835  // movz w1, #65535
1836  // ldrh w0, [x0, #0]
1837  // cmp w0, w1
1838  // >
1839  // ldrsh w0, [x0, #0]
1840  // cmn w0, #1
1841  // Fundamental, we're relying on the property that (zext LHS) == (zext RHS)
1842  // if and only if (sext LHS) == (sext RHS). The checks are in place to
1843  // ensure both the LHS and RHS are truly zero extended and to make sure the
1844  // transformation is profitable.
1845  if ((RHSC->getZExtValue() >> 16 == 0) && isa<LoadSDNode>(LHS) &&
1846  cast<LoadSDNode>(LHS)->getExtensionType() == ISD::ZEXTLOAD &&
1847  cast<LoadSDNode>(LHS)->getMemoryVT() == MVT::i16 &&
1848  LHS.getNode()->hasNUsesOfValue(1, 0)) {
1849  int16_t ValueofRHS = cast<ConstantSDNode>(RHS)->getZExtValue();
1850  if (ValueofRHS < 0 && isLegalArithImmed(-ValueofRHS)) {
1851  SDValue SExt =
1852  DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, LHS.getValueType(), LHS,
1853  DAG.getValueType(MVT::i16));
1854  Cmp = emitComparison(SExt, DAG.getConstant(ValueofRHS, dl,
1855  RHS.getValueType()),
1856  CC, dl, DAG);
1857  AArch64CC = changeIntCCToAArch64CC(CC);
1858  }
1859  }
1860 
1861  if (!Cmp && (RHSC->isNullValue() || RHSC->isOne())) {
1862  if ((Cmp = emitConjunctionDisjunctionTree(DAG, LHS, AArch64CC))) {
1863  if ((CC == ISD::SETNE) ^ RHSC->isNullValue())
1864  AArch64CC = AArch64CC::getInvertedCondCode(AArch64CC);
1865  }
1866  }
1867  }
1868 
1869  if (!Cmp) {
1870  Cmp = emitComparison(LHS, RHS, CC, dl, DAG);
1871  AArch64CC = changeIntCCToAArch64CC(CC);
1872  }
1873  AArch64cc = DAG.getConstant(AArch64CC, dl, MVT_CC);
1874  return Cmp;
1875 }
1876 
1877 static std::pair<SDValue, SDValue>
1879  assert((Op.getValueType() == MVT::i32 || Op.getValueType() == MVT::i64) &&
1880  "Unsupported value type");
1881  SDValue Value, Overflow;
1882  SDLoc DL(Op);
1883  SDValue LHS = Op.getOperand(0);
1884  SDValue RHS = Op.getOperand(1);
1885  unsigned Opc = 0;
1886  switch (Op.getOpcode()) {
1887  default:
1888  llvm_unreachable("Unknown overflow instruction!");
1889  case ISD::SADDO:
1890  Opc = AArch64ISD::ADDS;
1891  CC = AArch64CC::VS;
1892  break;
1893  case ISD::UADDO:
1894  Opc = AArch64ISD::ADDS;
1895  CC = AArch64CC::HS;
1896  break;
1897  case ISD::SSUBO:
1898  Opc = AArch64ISD::SUBS;
1899  CC = AArch64CC::VS;
1900  break;
1901  case ISD::USUBO:
1902  Opc = AArch64ISD::SUBS;
1903  CC = AArch64CC::LO;
1904  break;
1905  // Multiply needs a little bit extra work.
1906  case ISD::SMULO:
1907  case ISD::UMULO: {
1908  CC = AArch64CC::NE;
1909  bool IsSigned = Op.getOpcode() == ISD::SMULO;
1910  if (Op.getValueType() == MVT::i32) {
1911  unsigned ExtendOpc = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1912  // For a 32 bit multiply with overflow check we want the instruction
1913  // selector to generate a widening multiply (SMADDL/UMADDL). For that we
1914  // need to generate the following pattern:
1915  // (i64 add 0, (i64 mul (i64 sext|zext i32 %a), (i64 sext|zext i32 %b))
1916  LHS = DAG.getNode(ExtendOpc, DL, MVT::i64, LHS);
1917  RHS = DAG.getNode(ExtendOpc, DL, MVT::i64, RHS);
1918  SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, LHS, RHS);
1919  SDValue Add = DAG.getNode(ISD::ADD, DL, MVT::i64, Mul,
1920  DAG.getConstant(0, DL, MVT::i64));
1921  // On AArch64 the upper 32 bits are always zero extended for a 32 bit
1922  // operation. We need to clear out the upper 32 bits, because we used a
1923  // widening multiply that wrote all 64 bits. In the end this should be a
1924  // noop.
1925  Value = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Add);
1926  if (IsSigned) {
1927  // The signed overflow check requires more than just a simple check for
1928  // any bit set in the upper 32 bits of the result. These bits could be
1929  // just the sign bits of a negative number. To perform the overflow
1930  // check we have to arithmetic shift right the 32nd bit of the result by
1931  // 31 bits. Then we compare the result to the upper 32 bits.
1932  SDValue UpperBits = DAG.getNode(ISD::SRL, DL, MVT::i64, Add,
1933  DAG.getConstant(32, DL, MVT::i64));
1934  UpperBits = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, UpperBits);
1935  SDValue LowerBits = DAG.getNode(ISD::SRA, DL, MVT::i32, Value,
1936  DAG.getConstant(31, DL, MVT::i64));
1937  // It is important that LowerBits is last, otherwise the arithmetic
1938  // shift will not be folded into the compare (SUBS).
1939  SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32);
1940  Overflow = DAG.getNode(AArch64ISD::SUBS, DL, VTs, UpperBits, LowerBits)
1941  .getValue(1);
1942  } else {
1943  // The overflow check for unsigned multiply is easy. We only need to
1944  // check if any of the upper 32 bits are set. This can be done with a
1945  // CMP (shifted register). For that we need to generate the following
1946  // pattern:
1947  // (i64 AArch64ISD::SUBS i64 0, (i64 srl i64 %Mul, i64 32)
1948  SDValue UpperBits = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
1949  DAG.getConstant(32, DL, MVT::i64));
1950  SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32);
1951  Overflow =
1952  DAG.getNode(AArch64ISD::SUBS, DL, VTs,
1953  DAG.getConstant(0, DL, MVT::i64),
1954  UpperBits).getValue(1);
1955  }
1956  break;
1957  }
1958  assert(Op.getValueType() == MVT::i64 && "Expected an i64 value type");
1959  // For the 64 bit multiply
1960  Value = DAG.getNode(ISD::MUL, DL, MVT::i64, LHS, RHS);
1961  if (IsSigned) {
1962  SDValue UpperBits = DAG.getNode(ISD::MULHS, DL, MVT::i64, LHS, RHS);
1963  SDValue LowerBits = DAG.getNode(ISD::SRA, DL, MVT::i64, Value,
1964  DAG.getConstant(63, DL, MVT::i64));
1965  // It is important that LowerBits is last, otherwise the arithmetic
1966  // shift will not be folded into the compare (SUBS).
1967  SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32);
1968  Overflow = DAG.getNode(AArch64ISD::SUBS, DL, VTs, UpperBits, LowerBits)
1969  .getValue(1);
1970  } else {
1971  SDValue UpperBits = DAG.getNode(ISD::MULHU, DL, MVT::i64, LHS, RHS);
1972  SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32);
1973  Overflow =
1974  DAG.getNode(AArch64ISD::SUBS, DL, VTs,
1975  DAG.getConstant(0, DL, MVT::i64),
1976  UpperBits).getValue(1);
1977  }
1978  break;
1979  }
1980  } // switch (...)
1981 
1982  if (Opc) {
1983  SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::i32);
1984 
1985  // Emit the AArch64 operation with overflow check.
1986  Value = DAG.getNode(Opc, DL, VTs, LHS, RHS);
1987  Overflow = Value.getValue(1);
1988  }
1989  return std::make_pair(Value, Overflow);
1990 }
1991 
1992 SDValue AArch64TargetLowering::LowerF128Call(SDValue Op, SelectionDAG &DAG,
1993  RTLIB::Libcall Call) const {
1994  SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end());
1995  return makeLibCall(DAG, Call, MVT::f128, Ops, false, SDLoc(Op)).first;
1996 }
1997 
1998 // Returns true if the given Op is the overflow flag result of an overflow
1999 // intrinsic operation.
2000 static bool isOverflowIntrOpRes(SDValue Op) {
2001  unsigned Opc = Op.getOpcode();
2002  return (Op.getResNo() == 1 &&
2003  (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
2004  Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO));
2005 }
2006 
2008  SDValue Sel = Op.getOperand(0);
2009  SDValue Other = Op.getOperand(1);
2010  SDLoc dl(Sel);
2011 
2012  // If the operand is an overflow checking operation, invert the condition
2013  // code and kill the Not operation. I.e., transform:
2014  // (xor (overflow_op_bool, 1))
2015  // -->
2016  // (csel 1, 0, invert(cc), overflow_op_bool)
2017  // ... which later gets transformed to just a cset instruction with an
2018  // inverted condition code, rather than a cset + eor sequence.
2019  if (isOneConstant(Other) && isOverflowIntrOpRes(Sel)) {
2020  // Only lower legal XALUO ops.
2021  if (!DAG.getTargetLoweringInfo().isTypeLegal(Sel->getValueType(0)))
2022  return SDValue();
2023 
2024  SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
2025  SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
2027  SDValue Value, Overflow;
2028  std::tie(Value, Overflow) = getAArch64XALUOOp(CC, Sel.getValue(0), DAG);
2029  SDValue CCVal = DAG.getConstant(getInvertedCondCode(CC), dl, MVT::i32);
2030  return DAG.getNode(AArch64ISD::CSEL, dl, Op.getValueType(), TVal, FVal,
2031  CCVal, Overflow);
2032  }
2033  // If neither operand is a SELECT_CC, give up.
2034  if (Sel.getOpcode() != ISD::SELECT_CC)
2035  std::swap(Sel, Other);
2036  if (Sel.getOpcode() != ISD::SELECT_CC)
2037  return Op;
2038 
2039  // The folding we want to perform is:
2040  // (xor x, (select_cc a, b, cc, 0, -1) )
2041  // -->
2042  // (csel x, (xor x, -1), cc ...)
2043  //
2044  // The latter will get matched to a CSINV instruction.
2045 
2046  ISD::CondCode CC = cast<CondCodeSDNode>(Sel.getOperand(4))->get();
2047  SDValue LHS = Sel.getOperand(0);
2048  SDValue RHS = Sel.getOperand(1);
2049  SDValue TVal = Sel.getOperand(2);
2050  SDValue FVal = Sel.getOperand(3);
2051 
2052  // FIXME: This could be generalized to non-integer comparisons.
2053  if (LHS.getValueType() != MVT::i32 && LHS.getValueType() != MVT::i64)
2054  return Op;
2055 
2056  ConstantSDNode *CFVal = dyn_cast<ConstantSDNode>(FVal);
2057  ConstantSDNode *CTVal = dyn_cast<ConstantSDNode>(TVal);
2058 
2059  // The values aren't constants, this isn't the pattern we're looking for.
2060  if (!CFVal || !CTVal)
2061  return Op;
2062 
2063  // We can commute the SELECT_CC by inverting the condition. This
2064  // might be needed to make this fit into a CSINV pattern.
2065  if (CTVal->isAllOnesValue() && CFVal->isNullValue()) {
2066  std::swap(TVal, FVal);
2067  std::swap(CTVal, CFVal);
2068  CC = ISD::getSetCCInverse(CC, true);
2069  }
2070 
2071  // If the constants line up, perform the transform!
2072  if (CTVal->isNullValue() && CFVal->isAllOnesValue()) {
2073  SDValue CCVal;
2074  SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl);
2075 
2076  FVal = Other;
2077  TVal = DAG.getNode(ISD::XOR, dl, Other.getValueType(), Other,
2078  DAG.getConstant(-1ULL, dl, Other.getValueType()));
2079 
2080  return DAG.getNode(AArch64ISD::CSEL, dl, Sel.getValueType(), FVal, TVal,
2081  CCVal, Cmp);
2082  }
2083 
2084  return Op;
2085 }
2086 
2088  EVT VT = Op.getValueType();
2089 
2090  // Let legalize expand this if it isn't a legal type yet.
2091  if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
2092  return SDValue();
2093 
2094  SDVTList VTs = DAG.getVTList(VT, MVT::i32);
2095 
2096  unsigned Opc;
2097  bool ExtraOp = false;
2098  switch (Op.getOpcode()) {
2099  default:
2100  llvm_unreachable("Invalid code");
2101  case ISD::ADDC:
2102  Opc = AArch64ISD::ADDS;
2103  break;
2104  case ISD::SUBC:
2105  Opc = AArch64ISD::SUBS;
2106  break;
2107  case ISD::ADDE:
2108  Opc = AArch64ISD::ADCS;
2109  ExtraOp = true;
2110  break;
2111  case ISD::SUBE:
2112  Opc = AArch64ISD::SBCS;
2113  ExtraOp = true;
2114  break;
2115  }
2116 
2117  if (!ExtraOp)
2118  return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1));
2119  return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1),
2120  Op.getOperand(2));
2121 }
2122 
2124  // Let legalize expand this if it isn't a legal type yet.
2126  return SDValue();
2127 
2128  SDLoc dl(Op);
2130  // The actual operation that sets the overflow or carry flag.
2131  SDValue Value, Overflow;
2132  std::tie(Value, Overflow) = getAArch64XALUOOp(CC, Op, DAG);
2133 
2134  // We use 0 and 1 as false and true values.
2135  SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
2136  SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
2137 
2138  // We use an inverted condition, because the conditional select is inverted
2139  // too. This will allow it to be selected to a single instruction:
2140  // CSINC Wd, WZR, WZR, invert(cond).
2141  SDValue CCVal = DAG.getConstant(getInvertedCondCode(CC), dl, MVT::i32);
2142  Overflow = DAG.getNode(AArch64ISD::CSEL, dl, MVT::i32, FVal, TVal,
2143  CCVal, Overflow);
2144 
2145  SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
2146  return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
2147 }
2148 
2149 // Prefetch operands are:
2150 // 1: Address to prefetch
2151 // 2: bool isWrite
2152 // 3: int locality (0 = no locality ... 3 = extreme locality)
2153 // 4: bool isDataCache
2155  SDLoc DL(Op);
2156  unsigned IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2157  unsigned Locality = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
2158  unsigned IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2159 
2160  bool IsStream = !Locality;
2161  // When the locality number is set
2162  if (Locality) {
2163  // The front-end should have filtered out the out-of-range values
2164  assert(Locality <= 3 && "Prefetch locality out-of-range");
2165  // The locality degree is the opposite of the cache speed.
2166  // Put the number the other way around.
2167  // The encoding starts at 0 for level 1
2168  Locality = 3 - Locality;
2169  }
2170 
2171  // built the mask value encoding the expected behavior.
2172  unsigned PrfOp = (IsWrite << 4) | // Load/Store bit
2173  (!IsData << 3) | // IsDataCache bit
2174  (Locality << 1) | // Cache level bits
2175  (unsigned)IsStream; // Stream bit
2176  return DAG.getNode(AArch64ISD::PREFETCH, DL, MVT::Other, Op.getOperand(0),
2177  DAG.getConstant(PrfOp, DL, MVT::i32), Op.getOperand(1));
2178 }
2179 
2180 SDValue AArch64TargetLowering::LowerFP_EXTEND(SDValue Op,
2181  SelectionDAG &DAG) const {
2182  assert(Op.getValueType() == MVT::f128 && "Unexpected lowering");
2183 
2184  RTLIB::Libcall LC;
2186 
2187  return LowerF128Call(Op, DAG, LC);
2188 }
2189 
2190 SDValue AArch64TargetLowering::LowerFP_ROUND(SDValue Op,
2191  SelectionDAG &DAG) const {
2192  if (Op.getOperand(0).getValueType() != MVT::f128) {
2193  // It's legal except when f128 is involved
2194  return Op;
2195  }
2196 
2197  RTLIB::Libcall LC;
2199 
2200  // FP_ROUND node has a second operand indicating whether it is known to be
2201  // precise. That doesn't take part in the LibCall so we can't directly use
2202  // LowerF128Call.
2203  SDValue SrcVal = Op.getOperand(0);
2204  return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
2205  SDLoc(Op)).first;
2206 }
2207 
2209  // Warning: We maintain cost tables in AArch64TargetTransformInfo.cpp.
2210  // Any additional optimization in this function should be recorded
2211  // in the cost tables.
2212  EVT InVT = Op.getOperand(0).getValueType();
2213  EVT VT = Op.getValueType();
2214  unsigned NumElts = InVT.getVectorNumElements();
2215 
2216  // f16 vectors are promoted to f32 before a conversion.
2217  if (InVT.getVectorElementType() == MVT::f16) {
2218  MVT NewVT = MVT::getVectorVT(MVT::f32, NumElts);
2219  SDLoc dl(Op);
2220  return DAG.getNode(
2221  Op.getOpcode(), dl, Op.getValueType(),
2222  DAG.getNode(ISD::FP_EXTEND, dl, NewVT, Op.getOperand(0)));
2223  }
2224 
2225  if (VT.getSizeInBits() < InVT.getSizeInBits()) {
2226  SDLoc dl(Op);
2227  SDValue Cv =
2228  DAG.getNode(Op.getOpcode(), dl, InVT.changeVectorElementTypeToInteger(),
2229  Op.getOperand(0));
2230  return DAG.getNode(ISD::TRUNCATE, dl, VT, Cv);
2231  }
2232 
2233  if (VT.getSizeInBits() > InVT.getSizeInBits()) {
2234  SDLoc dl(Op);
2235  MVT ExtVT =
2237  VT.getVectorNumElements());
2238  SDValue Ext = DAG.getNode(ISD::FP_EXTEND, dl, ExtVT, Op.getOperand(0));
2239  return DAG.getNode(Op.getOpcode(), dl, VT, Ext);
2240  }
2241 
2242  // Type changing conversions are illegal.
2243  return Op;
2244 }
2245 
2246 SDValue AArch64TargetLowering::LowerFP_TO_INT(SDValue Op,
2247  SelectionDAG &DAG) const {
2248  if (Op.getOperand(0).getValueType().isVector())
2249  return LowerVectorFP_TO_INT(Op, DAG);
2250 
2251  // f16 conversions are promoted to f32 when full fp16 is not supported.
2252  if (Op.getOperand(0).getValueType() == MVT::f16 &&
2253  !Subtarget->hasFullFP16()) {
2254  SDLoc dl(Op);
2255  return DAG.getNode(
2256  Op.getOpcode(), dl, Op.getValueType(),
2257  DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, Op.getOperand(0)));
2258  }
2259 
2260  if (Op.getOperand(0).getValueType() != MVT::f128) {
2261  // It's legal except when f128 is involved
2262  return Op;
2263  }
2264 
2265  RTLIB::Libcall LC;
2266  if (Op.getOpcode() == ISD::FP_TO_SINT)
2268  else
2270 
2271  SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end());
2272  return makeLibCall(DAG, LC, Op.getValueType(), Ops, false, SDLoc(Op)).first;
2273 }
2274 
2276  // Warning: We maintain cost tables in AArch64TargetTransformInfo.cpp.
2277  // Any additional optimization in this function should be recorded
2278  // in the cost tables.
2279  EVT VT = Op.getValueType();
2280  SDLoc dl(Op);
2281  SDValue In = Op.getOperand(0);
2282  EVT InVT = In.getValueType();
2283 
2284  if (VT.getSizeInBits() < InVT.getSizeInBits()) {
2285  MVT CastVT =
2287  InVT.getVectorNumElements());
2288  In = DAG.getNode(Op.getOpcode(), dl, CastVT, In);
2289  return DAG.getNode(ISD::FP_ROUND, dl, VT, In, DAG.getIntPtrConstant(0, dl));
2290  }
2291 
2292  if (VT.getSizeInBits() > InVT.getSizeInBits()) {
2293  unsigned CastOpc =
2295  EVT CastVT = VT.changeVectorElementTypeToInteger();
2296  In = DAG.getNode(CastOpc, dl, CastVT, In);
2297  return DAG.getNode(Op.getOpcode(), dl, VT, In);
2298  }
2299 
2300  return Op;
2301 }
2302 
2303 SDValue AArch64TargetLowering::LowerINT_TO_FP(SDValue Op,
2304  SelectionDAG &DAG) const {
2305  if (Op.getValueType().isVector())
2306  return LowerVectorINT_TO_FP(Op, DAG);
2307 
2308  // f16 conversions are promoted to f32 when full fp16 is not supported.
2309  if (Op.getValueType() == MVT::f16 &&
2310  !Subtarget->hasFullFP16()) {
2311  SDLoc dl(Op);
2312  return DAG.getNode(
2313  ISD::FP_ROUND, dl, MVT::f16,
2314  DAG.getNode(Op.getOpcode(), dl, MVT::f32, Op.getOperand(0)),
2315  DAG.getIntPtrConstant(0, dl));
2316  }
2317 
2318  // i128 conversions are libcalls.
2319  if (Op.getOperand(0).getValueType() == MVT::i128)
2320  return SDValue();
2321 
2322  // Other conversions are legal, unless it's to the completely software-based
2323  // fp128.
2324  if (Op.getValueType() != MVT::f128)
2325  return Op;
2326 
2327  RTLIB::Libcall LC;
2328  if (Op.getOpcode() == ISD::SINT_TO_FP)
2330  else
2332 
2333  return LowerF128Call(Op, DAG, LC);
2334 }
2335 
2336 SDValue AArch64TargetLowering::LowerFSINCOS(SDValue Op,
2337  SelectionDAG &DAG) const {
2338  // For iOS, we want to call an alternative entry point: __sincos_stret,
2339  // which returns the values in two S / D registers.
2340  SDLoc dl(Op);
2341  SDValue Arg = Op.getOperand(0);
2342  EVT ArgVT = Arg.getValueType();
2343  Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2344 
2345  ArgListTy Args;
2346  ArgListEntry Entry;
2347 
2348  Entry.Node = Arg;
2349  Entry.Ty = ArgTy;
2350  Entry.IsSExt = false;
2351  Entry.IsZExt = false;
2352  Args.push_back(Entry);
2353 
2354  RTLIB::Libcall LC = ArgVT == MVT::f64 ? RTLIB::SINCOS_STRET_F64
2355  : RTLIB::SINCOS_STRET_F32;
2356  const char *LibcallName = getLibcallName(LC);
2357  SDValue Callee =
2358  DAG.getExternalSymbol(LibcallName, getPointerTy(DAG.getDataLayout()));
2359 
2360  StructType *RetTy = StructType::get(ArgTy, ArgTy);
2362  CLI.setDebugLoc(dl)
2363  .setChain(DAG.getEntryNode())
2364  .setLibCallee(CallingConv::Fast, RetTy, Callee, std::move(Args));
2365 
2366  std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2367  return CallResult.first;
2368 }
2369 
2371  if (Op.getValueType() != MVT::f16)
2372  return SDValue();
2373 
2374  assert(Op.getOperand(0).getValueType() == MVT::i16);
2375  SDLoc DL(Op);
2376 
2377  Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Op.getOperand(0));
2378  Op = DAG.getNode(ISD::BITCAST, DL, MVT::f32, Op);
2379  return SDValue(
2380  DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, MVT::f16, Op,
2381  DAG.getTargetConstant(AArch64::hsub, DL, MVT::i32)),
2382  0);
2383 }
2384 
2385 static EVT getExtensionTo64Bits(const EVT &OrigVT) {
2386  if (OrigVT.getSizeInBits() >= 64)
2387  return OrigVT;
2388 
2389  assert(OrigVT.isSimple() && "Expecting a simple value type");
2390 
2391  MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
2392  switch (OrigSimpleTy) {
2393  default: llvm_unreachable("Unexpected Vector Type");
2394  case MVT::v2i8:
2395  case MVT::v2i16:
2396  return MVT::v2i32;
2397  case MVT::v4i8:
2398  return MVT::v4i16;
2399  }
2400 }
2401 
2403  const EVT &OrigTy,
2404  const EVT &ExtTy,
2405  unsigned ExtOpcode) {
2406  // The vector originally had a size of OrigTy. It was then extended to ExtTy.
2407  // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
2408  // 64-bits we need to insert a new extension so that it will be 64-bits.
2409  assert(ExtTy.is128BitVector() && "Unexpected extension size");
2410  if (OrigTy.getSizeInBits() >= 64)
2411  return N;
2412 
2413  // Must extend size to at least 64 bits to be used as an operand for VMULL.
2414  EVT NewVT = getExtensionTo64Bits(OrigTy);
2415 
2416  return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
2417 }
2418 
2420  bool isSigned) {
2421  EVT VT = N->getValueType(0);
2422 
2423  if (N->getOpcode() != ISD::BUILD_VECTOR)
2424  return false;
2425 
2426  for (const SDValue &Elt : N->op_values()) {
2427  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
2428  unsigned EltSize = VT.getScalarSizeInBits();
2429  unsigned HalfSize = EltSize / 2;
2430  if (isSigned) {
2431  if (!isIntN(HalfSize, C->getSExtValue()))
2432  return false;
2433  } else {
2434  if (!isUIntN(HalfSize, C->getZExtValue()))
2435  return false;
2436  }
2437  continue;
2438  }
2439  return false;
2440  }
2441 
2442  return true;
2443 }
2444 
2446  if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
2448  N->getOperand(0)->getValueType(0),
2449  N->getValueType(0),
2450  N->getOpcode());
2451 
2452  assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
2453  EVT VT = N->getValueType(0);
2454  SDLoc dl(N);
2455  unsigned EltSize = VT.getScalarSizeInBits() / 2;
2456  unsigned NumElts = VT.getVectorNumElements();
2457  MVT TruncVT = MVT::getIntegerVT(EltSize);
2459  for (unsigned i = 0; i != NumElts; ++i) {
2460  ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
2461  const APInt &CInt = C->getAPIntValue();
2462  // Element types smaller than 32 bits are not legal, so use i32 elements.
2463  // The values are implicitly truncated so sext vs. zext doesn't matter.
2464  Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
2465  }
2466  return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
2467 }
2468 
2469 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
2470  return N->getOpcode() == ISD::SIGN_EXTEND ||
2471  isExtendedBUILD_VECTOR(N, DAG, true);
2472 }
2473 
2474 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
2475  return N->getOpcode() == ISD::ZERO_EXTEND ||
2476  isExtendedBUILD_VECTOR(N, DAG, false);
2477 }
2478 
2479 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
2480  unsigned Opcode = N->getOpcode();
2481  if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
2482  SDNode *N0 = N->getOperand(0).getNode();
2483  SDNode *N1 = N->getOperand(1).getNode();
2484  return N0->hasOneUse() && N1->hasOneUse() &&
2485  isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
2486  }
2487  return false;
2488 }
2489 
2490 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
2491  unsigned Opcode = N->getOpcode();
2492  if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
2493  SDNode *N0 = N->getOperand(0).getNode();
2494  SDNode *N1 = N->getOperand(1).getNode();
2495  return N0->hasOneUse() && N1->hasOneUse() &&
2496  isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
2497  }
2498  return false;
2499 }
2500 
2501 SDValue AArch64TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
2502  SelectionDAG &DAG) const {
2503  // The rounding mode is in bits 23:22 of the FPSCR.
2504  // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2505  // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2506  // so that the shift + and get folded into a bitfield extract.
2507  SDLoc dl(Op);
2508 
2509  SDValue FPCR_64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i64,
2510  DAG.getConstant(Intrinsic::aarch64_get_fpcr, dl,
2511  MVT::i64));
2512  SDValue FPCR_32 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, FPCR_64);
2513  SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPCR_32,
2514  DAG.getConstant(1U << 22, dl, MVT::i32));
2515  SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2516  DAG.getConstant(22, dl, MVT::i32));
2517  return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
2518  DAG.getConstant(3, dl, MVT::i32));
2519 }
2520 
2522  // Multiplications are only custom-lowered for 128-bit vectors so that
2523  // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
2524  EVT VT = Op.getValueType();
2525  assert(VT.is128BitVector() && VT.isInteger() &&
2526  "unexpected type for custom-lowering ISD::MUL");
2527  SDNode *N0 = Op.getOperand(0).getNode();
2528  SDNode *N1 = Op.getOperand(1).getNode();
2529  unsigned NewOpc = 0;
2530  bool isMLA = false;
2531  bool isN0SExt = isSignExtended(N0, DAG);
2532  bool isN1SExt = isSignExtended(N1, DAG);
2533  if (isN0SExt && isN1SExt)
2534  NewOpc = AArch64ISD::SMULL;
2535  else {
2536  bool isN0ZExt = isZeroExtended(N0, DAG);
2537  bool isN1ZExt = isZeroExtended(N1, DAG);
2538  if (isN0ZExt && isN1ZExt)
2539  NewOpc = AArch64ISD::UMULL;
2540  else if (isN1SExt || isN1ZExt) {
2541  // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
2542  // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
2543  if (isN1SExt && isAddSubSExt(N0, DAG)) {
2544  NewOpc = AArch64ISD::SMULL;
2545  isMLA = true;
2546  } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
2547  NewOpc = AArch64ISD::UMULL;
2548  isMLA = true;
2549  } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
2550  std::swap(N0, N1);
2551  NewOpc = AArch64ISD::UMULL;
2552  isMLA = true;
2553  }
2554  }
2555 
2556  if (!NewOpc) {
2557  if (VT == MVT::v2i64)
2558  // Fall through to expand this. It is not legal.
2559  return SDValue();
2560  else
2561  // Other vector multiplications are legal.
2562  return Op;
2563  }
2564  }
2565 
2566  // Legalize to a S/UMULL instruction
2567  SDLoc DL(Op);
2568  SDValue Op0;
2569  SDValue Op1 = skipExtensionForVectorMULL(N1, DAG);
2570  if (!isMLA) {
2571  Op0 = skipExtensionForVectorMULL(N0, DAG);
2572  assert(Op0.getValueType().is64BitVector() &&
2573  Op1.getValueType().is64BitVector() &&
2574  "unexpected types for extended operands to VMULL");
2575  return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
2576  }
2577  // Optimizing (zext A + zext B) * C, to (S/UMULL A, C) + (S/UMULL B, C) during
2578  // isel lowering to take advantage of no-stall back to back s/umul + s/umla.
2579  // This is true for CPUs with accumulate forwarding such as Cortex-A53/A57
2580  SDValue N00 = skipExtensionForVectorMULL(N0->getOperand(0).getNode(), DAG);
2581  SDValue N01 = skipExtensionForVectorMULL(N0->getOperand(1).getNode(), DAG);
2582  EVT Op1VT = Op1.getValueType();
2583  return DAG.getNode(N0->getOpcode(), DL, VT,
2584  DAG.getNode(NewOpc, DL, VT,
2585  DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
2586  DAG.getNode(NewOpc, DL, VT,
2587  DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
2588 }
2589 
2590 // Lower vector multiply high (ISD::MULHS and ISD::MULHU).
2592  // Multiplications are only custom-lowered for 128-bit vectors so that
2593  // {S,U}MULL{2} can be detected. Otherwise v2i64 multiplications are not
2594  // legal.
2595  EVT VT = Op.getValueType();
2596  assert(VT.is128BitVector() && VT.isInteger() &&
2597  "unexpected type for custom-lowering ISD::MULH{U,S}");
2598 
2599  SDValue V0 = Op.getOperand(0);
2600  SDValue V1 = Op.getOperand(1);
2601 
2602  SDLoc DL(Op);
2603 
2604  EVT ExtractVT = VT.getHalfNumVectorElementsVT(*DAG.getContext());
2605 
2606  // We turn (V0 mulhs/mulhu V1) to:
2607  //
2608  // (uzp2 (smull (extract_subvector (ExtractVT V128:V0, (i64 0)),
2609  // (extract_subvector (ExtractVT V128:V1, (i64 0))))),
2610  // (smull (extract_subvector (ExtractVT V128:V0, (i64 VMull2Idx)),
2611  // (extract_subvector (ExtractVT V128:V2, (i64 VMull2Idx))))))
2612  //
2613  // Where ExtractVT is a subvector with half number of elements, and
2614  // VMullIdx2 is the index of the middle element (the high part).
2615  //
2616  // The vector hight part extract and multiply will be matched against
2617  // {S,U}MULL{v16i8_v8i16,v8i16_v4i32,v4i32_v2i64} which in turn will
2618  // issue a {s}mull2 instruction.
2619  //
2620  // This basically multiply the lower subvector with '{s,u}mull', the high
2621  // subvector with '{s,u}mull2', and shuffle both results high part in
2622  // resulting vector.
2623  unsigned Mull2VectorIdx = VT.getVectorNumElements () / 2;
2624  SDValue VMullIdx = DAG.getConstant(0, DL, MVT::i64);
2625  SDValue VMull2Idx = DAG.getConstant(Mull2VectorIdx, DL, MVT::i64);
2626 
2627  SDValue VMullV0 =
2628  DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtractVT, V0, VMullIdx);
2629  SDValue VMullV1 =
2630  DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtractVT, V1, VMullIdx);
2631 
2632  SDValue VMull2V0 =
2633  DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtractVT, V0, VMull2Idx);
2634  SDValue VMull2V1 =
2635  DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtractVT, V1, VMull2Idx);
2636 
2637  unsigned MullOpc = Op.getOpcode() == ISD::MULHS ? AArch64ISD::SMULL
2639 
2640  EVT MullVT = ExtractVT.widenIntegerVectorElementType(*DAG.getContext());
2641  SDValue Mull = DAG.getNode(MullOpc, DL, MullVT, VMullV0, VMullV1);
2642  SDValue Mull2 = DAG.getNode(MullOpc, DL, MullVT, VMull2V0, VMull2V1);
2643 
2644  Mull = DAG.getNode(ISD::BITCAST, DL, VT, Mull);
2645  Mull2 = DAG.getNode(ISD::BITCAST, DL, VT, Mull2);
2646 
2647  return DAG.getNode(AArch64ISD::UZP2, DL, VT, Mull, Mull2);
2648 }
2649 
2650 SDValue AArch64TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2651  SelectionDAG &DAG) const {
2652  unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2653  SDLoc dl(Op);
2654  switch (IntNo) {
2655  default: return SDValue(); // Don't custom lower most intrinsics.
2656  case Intrinsic::thread_pointer: {
2657  EVT PtrVT = getPointerTy(DAG.getDataLayout());
2658  return DAG.getNode(AArch64ISD::THREAD_POINTER, dl, PtrVT);
2659  }
2660  case Intrinsic::aarch64_neon_abs:
2661  return DAG.getNode(ISD::ABS, dl, Op.getValueType(),
2662  Op.getOperand(1));
2663  case Intrinsic::aarch64_neon_smax:
2664  return DAG.getNode(ISD::SMAX, dl, Op.getValueType(),
2665  Op.getOperand(1), Op.getOperand(2));
2666  case Intrinsic::aarch64_neon_umax:
2667  return DAG.getNode(ISD::UMAX, dl, Op.getValueType(),
2668  Op.getOperand(1), Op.getOperand(2));
2669  case Intrinsic::aarch64_neon_smin:
2670  return DAG.getNode(ISD::SMIN, dl, Op.getValueType(),
2671  Op.getOperand(1), Op.getOperand(2));
2672  case Intrinsic::aarch64_neon_umin:
2673  return DAG.getNode(ISD::UMIN, dl, Op.getValueType(),
2674  Op.getOperand(1), Op.getOperand(2));
2675  }
2676 }
2677 
2678 // Custom lower trunc store for v4i8 vectors, since it is promoted to v4i16.
2680  EVT VT, EVT MemVT,
2681  SelectionDAG &DAG) {
2682  assert(VT.isVector() && "VT should be a vector type");
2683  assert(MemVT == MVT::v4i8 && VT == MVT::v4i16);
2684 
2685  SDValue Value = ST->getValue();
2686 
2687  // It first extend the promoted v4i16 to v8i16, truncate to v8i8, and extract
2688  // the word lane which represent the v4i8 subvector. It optimizes the store
2689  // to:
2690  //
2691  // xtn v0.8b, v0.8h
2692  // str s0, [x0]
2693 
2694  SDValue Undef = DAG.getUNDEF(MVT::i16);
2695  SDValue UndefVec = DAG.getBuildVector(MVT::v4i16, DL,
2696  {Undef, Undef, Undef, Undef});
2697 
2698  SDValue TruncExt = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i16,
2699  Value, UndefVec);
2700  SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::v8i8, TruncExt);
2701 
2702  Trunc = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Trunc);
2703  SDValue ExtractTrunc = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
2704  Trunc, DAG.getConstant(0, DL, MVT::i64));
2705 
2706  return DAG.getStore(ST->getChain(), DL, ExtractTrunc,
2707  ST->getBasePtr(), ST->getMemOperand());
2708 }
2709 
2710 // Custom lowering for any store, vector or scalar and/or default or with
2711 // a truncate operations. Currently only custom lower truncate operation
2712 // from vector v4i16 to v4i8.
2713 SDValue AArch64TargetLowering::LowerSTORE(SDValue Op,
2714  SelectionDAG &DAG) const {
2715  SDLoc Dl(Op);
2716  StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
2717  assert (StoreNode && "Can only custom lower store nodes");
2718 
2719  SDValue Value = StoreNode->getValue();
2720 
2721  EVT VT = Value.getValueType();
2722  EVT MemVT = StoreNode->getMemoryVT();
2723 
2724  assert (VT.isVector() && "Can only custom lower vector store types");
2725 
2726  unsigned AS = StoreNode->getAddressSpace();
2727  unsigned Align = StoreNode->getAlignment();
2728  if (Align < MemVT.getStoreSize() &&
2729  !allowsMisalignedMemoryAccesses(MemVT, AS, Align, nullptr)) {
2730  return scalarizeVectorStore(StoreNode, DAG);
2731  }
2732 
2733  if (StoreNode->isTruncatingStore()) {
2734  return LowerTruncateVectorStore(Dl, StoreNode, VT, MemVT, DAG);
2735  }
2736 
2737  return SDValue();
2738 }
2739 
2741  SelectionDAG &DAG) const {
2742  LLVM_DEBUG(dbgs() << "Custom lowering: ");
2743  LLVM_DEBUG(Op.dump());
2744 
2745  switch (Op.getOpcode()) {
2746  default:
2747  llvm_unreachable("unimplemented operand");
2748  return SDValue();
2749  case ISD::BITCAST:
2750  return LowerBITCAST(Op, DAG);
2751  case ISD::GlobalAddress:
2752  return LowerGlobalAddress(Op, DAG);
2753  case ISD::GlobalTLSAddress:
2754  return LowerGlobalTLSAddress(Op, DAG);
2755  case ISD::SETCC:
2756  return LowerSETCC(Op, DAG);
2757  case ISD::BR_CC:
2758  return LowerBR_CC(Op, DAG);
2759  case ISD::SELECT:
2760  return LowerSELECT(Op, DAG);
2761  case ISD::SELECT_CC:
2762  return LowerSELECT_CC(Op, DAG);
2763  case ISD::JumpTable:
2764  return LowerJumpTable(Op, DAG);
2765  case ISD::ConstantPool:
2766  return LowerConstantPool(Op, DAG);
2767  case ISD::BlockAddress:
2768  return LowerBlockAddress(Op, DAG);
2769  case ISD::VASTART:
2770  return LowerVASTART(Op, DAG);
2771  case ISD::VACOPY:
2772  return LowerVACOPY(Op, DAG);
2773  case ISD::VAARG:
2774  return LowerVAARG(Op, DAG);
2775  case ISD::ADDC:
2776  case ISD::ADDE:
2777  case ISD::SUBC:
2778  case ISD::SUBE:
2779  return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
2780  case ISD::SADDO:
2781  case ISD::UADDO:
2782  case ISD::SSUBO:
2783  case ISD::USUBO:
2784  case ISD::SMULO:
2785  case ISD::UMULO:
2786  return LowerXALUO(Op, DAG);
2787  case ISD::FADD:
2788  return LowerF128Call(Op, DAG, RTLIB::ADD_F128);
2789  case ISD::FSUB:
2790  return LowerF128Call(Op, DAG, RTLIB::SUB_F128);
2791  case ISD::FMUL:
2792  return LowerF128Call(Op, DAG, RTLIB::MUL_F128);
2793  case ISD::FDIV:
2794  return LowerF128Call(Op, DAG, RTLIB::DIV_F128);
2795  case ISD::FP_ROUND:
2796  return LowerFP_ROUND(Op, DAG);
2797  case ISD::FP_EXTEND:
2798  return LowerFP_EXTEND(Op, DAG);
2799  case ISD::FRAMEADDR:
2800  return LowerFRAMEADDR(Op, DAG);
2801  case ISD::RETURNADDR:
2802  return LowerRETURNADDR(Op, DAG);
2804  return LowerINSERT_VECTOR_ELT(Op, DAG);
2806  return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2807  case ISD::BUILD_VECTOR:
2808  return LowerBUILD_VECTOR(Op, DAG);
2809  case ISD::VECTOR_SHUFFLE:
2810  return LowerVECTOR_SHUFFLE(Op, DAG);
2812  return LowerEXTRACT_SUBVECTOR(Op, DAG);
2813  case ISD::SRA:
2814  case ISD::SRL:
2815  case ISD::SHL:
2816  return LowerVectorSRA_SRL_SHL(Op, DAG);
2817  case ISD::SHL_PARTS:
2818  return LowerShiftLeftParts(Op, DAG);
2819  case ISD::SRL_PARTS:
2820  case ISD::SRA_PARTS:
2821  return LowerShiftRightParts(Op, DAG);
2822  case ISD::CTPOP:
2823  return LowerCTPOP(Op, DAG);
2824  case ISD::FCOPYSIGN:
2825  return LowerFCOPYSIGN(Op, DAG);
2826  case ISD::AND:
2827  return LowerVectorAND(Op, DAG);
2828  case ISD::OR:
2829  return LowerVectorOR(Op, DAG);
2830  case ISD::XOR:
2831  return LowerXOR(Op, DAG);
2832  case ISD::PREFETCH:
2833  return LowerPREFETCH(Op, DAG);
2834  case ISD::SINT_TO_FP:
2835  case ISD::UINT_TO_FP:
2836  return LowerINT_TO_FP(Op, DAG);
2837  case ISD::FP_TO_SINT:
2838  case ISD::FP_TO_UINT:
2839  return LowerFP_TO_INT(Op, DAG);
2840  case ISD::FSINCOS:
2841  return LowerFSINCOS(Op, DAG);
2842  case ISD::FLT_ROUNDS_:
2843  return LowerFLT_ROUNDS_(Op, DAG);
2844  case ISD::MUL:
2845  return LowerMUL(Op, DAG);
2846  case ISD::MULHS:
2847  case ISD::MULHU:
2848  return LowerMULH(Op, DAG);
2850  return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2851  case ISD::STORE:
2852  return LowerSTORE(Op, DAG);
2853  case ISD::VECREDUCE_ADD:
2854  case ISD::VECREDUCE_SMAX:
2855  case ISD::VECREDUCE_SMIN:
2856  case ISD::VECREDUCE_UMAX:
2857  case ISD::VECREDUCE_UMIN:
2858  case ISD::VECREDUCE_FMAX:
2859  case ISD::VECREDUCE_FMIN:
2860  return LowerVECREDUCE(Op, DAG);
2861  case ISD::ATOMIC_LOAD_SUB:
2862  return LowerATOMIC_LOAD_SUB(Op, DAG);
2863  case ISD::ATOMIC_LOAD_AND:
2864  return LowerATOMIC_LOAD_AND(Op, DAG);
2866  return LowerDYNAMIC_STACKALLOC(Op, DAG);
2867  }
2868 }
2869 
2870 //===----------------------------------------------------------------------===//
2871 // Calling Convention Implementation
2872 //===----------------------------------------------------------------------===//
2873 
2874 #include "AArch64GenCallingConv.inc"
2875 
2876 /// Selects the correct CCAssignFn for a given CallingConvention value.
2878  bool IsVarArg) const {
2879  switch (CC) {
2880  default:
2881  report_fatal_error("Unsupported calling convention.");
2883  return CC_AArch64_WebKit_JS;
2884  case CallingConv::GHC:
2885  return CC_AArch64_GHC;
2886  case CallingConv::C:
2887  case CallingConv::Fast:
2890  case CallingConv::Swift:
2891  if (Subtarget->isTargetWindows() && IsVarArg)
2892  return CC_AArch64_Win64_VarArg;
2893  if (!Subtarget->isTargetDarwin())
2894  return CC_AArch64_AAPCS;
2895  return IsVarArg ? CC_AArch64_DarwinPCS_VarArg : CC_AArch64_DarwinPCS;
2896  case CallingConv::Win64:
2897  return IsVarArg ? CC_AArch64_Win64_VarArg : CC_AArch64_AAPCS;
2898  }
2899 }
2900 
2901 CCAssignFn *
2903  return CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
2904  : RetCC_AArch64_AAPCS;
2905 }
2906 
2907 SDValue AArch64TargetLowering::LowerFormalArguments(
2908  SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2909  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2910  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
2911  MachineFunction &MF = DAG.getMachineFunction();
2912  MachineFrameInfo &MFI = MF.getFrameInfo();
2913  bool IsWin64 = Subtarget->isCallingConvWin64(MF.getFunction().getCallingConv());
2914 
2915  // Assign locations to all of the incoming arguments.
2917  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2918  *DAG.getContext());
2919 
2920  // At this point, Ins[].VT may already be promoted to i32. To correctly
2921  // handle passing i8 as i8 instead of i32 on stack, we pass in both i32 and
2922  // i8 to CC_AArch64_AAPCS with i32 being ValVT and i8 being LocVT.
2923  // Since AnalyzeFormalArguments uses Ins[].VT for both ValVT and LocVT, here
2924  // we use a special version of AnalyzeFormalArguments to pass in ValVT and
2925  // LocVT.
2926  unsigned NumArgs = Ins.size();
2928  unsigned CurArgIdx = 0;
2929  for (unsigned i = 0; i != NumArgs; ++i) {
2930  MVT ValVT = Ins[i].VT;
2931  if (Ins[i].isOrigArg()) {
2932  std::advance(CurOrigArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2933  CurArgIdx = Ins[i].getOrigArgIndex();
2934 
2935  // Get type of the original argument.
2936  EVT ActualVT = getValueType(DAG.getDataLayout(), CurOrigArg->getType(),
2937  /*AllowUnknown*/ true);
2938  MVT ActualMVT = ActualVT.isSimple() ? ActualVT.getSimpleVT() : MVT::Other;
2939  // If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16.
2940  if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8)
2941  ValVT = MVT::i8;
2942  else if (ActualMVT == MVT::i16)
2943  ValVT = MVT::i16;
2944  }
2945  CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
2946  bool Res =
2947  AssignFn(i, ValVT, ValVT, CCValAssign::Full, Ins[i].Flags, CCInfo);
2948  assert(!Res && "Call operand has unhandled type");
2949  (void)Res;
2950  }
2951  assert(ArgLocs.size() == Ins.size());
2952  SmallVector<SDValue, 16> ArgValues;
2953  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2954  CCValAssign &VA = ArgLocs[i];
2955 
2956  if (Ins[i].Flags.isByVal()) {
2957  // Byval is used for HFAs in the PCS, but the system should work in a
2958  // non-compliant manner for larger structs.
2959  EVT PtrVT = getPointerTy(DAG.getDataLayout());
2960  int Size = Ins[i].Flags.getByValSize();
2961  unsigned NumRegs = (Size + 7) / 8;
2962 
2963  // FIXME: This works on big-endian for composite byvals, which are the common
2964  // case. It should also work for fundamental types too.
2965  unsigned FrameIdx =
2966  MFI.CreateFixedObject(8 * NumRegs, VA.getLocMemOffset(), false);
2967  SDValue FrameIdxN = DAG.getFrameIndex(FrameIdx, PtrVT);
2968  InVals.push_back(FrameIdxN);
2969 
2970  continue;
2971  }
2972 
2973  if (VA.isRegLoc()) {
2974  // Arguments stored in registers.
2975  EVT RegVT = VA.getLocVT();
2976 
2977  SDValue ArgValue;
2978  const TargetRegisterClass *RC;
2979 
2980  if (RegVT == MVT::i32)
2981  RC = &AArch64::GPR32RegClass;
2982  else if (RegVT == MVT::i64)
2983  RC = &AArch64::GPR64RegClass;
2984  else if (RegVT == MVT::f16)
2985  RC = &AArch64::FPR16RegClass;
2986  else if (RegVT == MVT::f32)
2987  RC = &AArch64::FPR32RegClass;
2988  else if (RegVT == MVT::f64 || RegVT.is64BitVector())
2989  RC = &AArch64::FPR64RegClass;
2990  else if (RegVT == MVT::f128 || RegVT.is128BitVector())
2991  RC = &AArch64::FPR128RegClass;
2992  else
2993  llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
2994 
2995  // Transform the arguments in physical registers into virtual ones.
2996  unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
2997  ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
2998 
2999  // If this is an 8, 16 or 32-bit value, it is really passed promoted
3000  // to 64 bits. Insert an assert[sz]ext to capture this, then
3001  // truncate to the right size.
3002  switch (VA.getLocInfo()) {
3003  default:
3004  llvm_unreachable("Unknown loc info!");
3005  case CCValAssign::Full:
3006  break;
3007  case CCValAssign::BCvt:
3008  ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), ArgValue);
3009  break;
3010  case CCValAssign::AExt:
3011  case CCValAssign::SExt:
3012  case CCValAssign::ZExt:
3013  // SelectionDAGBuilder will insert appropriate AssertZExt & AssertSExt
3014  // nodes after our lowering.
3015  assert(RegVT == Ins[i].VT && "incorrect register location selected");
3016  break;
3017  }
3018 
3019  InVals.push_back(ArgValue);
3020 
3021  } else { // VA.isRegLoc()
3022  assert(VA.isMemLoc() && "CCValAssign is neither reg nor mem");
3023  unsigned ArgOffset = VA.getLocMemOffset();
3024  unsigned ArgSize = VA.getValVT().getSizeInBits() / 8;
3025 
3026  uint32_t BEAlign = 0;
3027  if (!Subtarget->isLittleEndian() && ArgSize < 8 &&
3028  !Ins[i].Flags.isInConsecutiveRegs())
3029  BEAlign = 8 - ArgSize;
3030 
3031  int FI = MFI.CreateFixedObject(ArgSize, ArgOffset + BEAlign, true);
3032 
3033  // Create load nodes to retrieve arguments from the stack.
3034  SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3035  SDValue ArgValue;
3036 
3037  // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT)
3039  MVT MemVT = VA.getValVT();
3040 
3041  switch (VA.getLocInfo()) {
3042  default:
3043  break;
3044  case CCValAssign::BCvt:
3045  MemVT = VA.getLocVT();
3046  break;
3047  case CCValAssign::SExt:
3048  ExtType = ISD::SEXTLOAD;
3049  break;
3050  case CCValAssign::ZExt:
3051  ExtType = ISD::ZEXTLOAD;
3052  break;
3053  case CCValAssign::AExt:
3054  ExtType = ISD::EXTLOAD;
3055  break;
3056  }
3057 
3058  ArgValue = DAG.getExtLoad(
3059  ExtType, DL, VA.getLocVT(), Chain, FIN,
3061  MemVT);
3062 
3063  InVals.push_back(ArgValue);
3064  }
3065  }
3066 
3067  // varargs
3069  if (isVarArg) {
3070  if (!Subtarget->isTargetDarwin() || IsWin64) {
3071  // The AAPCS variadic function ABI is identical to the non-variadic
3072  // one. As a result there may be more arguments in registers and we should
3073  // save them for future reference.
3074  // Win64 variadic functions also pass arguments in registers, but all float
3075  // arguments are passed in integer registers.
3076  saveVarArgRegisters(CCInfo, DAG, DL, Chain);
3077  }
3078 
3079  // This will point to the next argument passed via stack.
3080  unsigned StackOffset = CCInfo.getNextStackOffset();
3081  // We currently pass all varargs at 8-byte alignment.
3082  StackOffset = ((StackOffset + 7) & ~7);
3083  FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackOffset, true));
3084  }
3085 
3086  unsigned StackArgSize = CCInfo.getNextStackOffset();
3087  bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
3088  if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) {
3089  // This is a non-standard ABI so by fiat I say we're allowed to make full
3090  // use of the stack area to be popped, which must be aligned to 16 bytes in
3091  // any case:
3092  StackArgSize = alignTo(StackArgSize, 16);
3093 
3094  // If we're expected to restore the stack (e.g. fastcc) then we'll be adding
3095  // a multiple of 16.
3096  FuncInfo->setArgumentStackToRestore(StackArgSize);
3097 
3098  // This realignment carries over to the available bytes below. Our own
3099  // callers will guarantee the space is free by giving an aligned value to
3100  // CALLSEQ_START.
3101  }
3102  // Even if we're not expected to free up the space, it's useful to know how
3103  // much is there while considering tail calls (because we can reuse it).
3104  FuncInfo->setBytesInStackArgArea(StackArgSize);
3105 
3106  return Chain;
3107 }
3108 
3109 void AArch64TargetLowering::saveVarArgRegisters(CCState &CCInfo,
3110  SelectionDAG &DAG,
3111  const SDLoc &DL,
3112  SDValue &Chain) const {
3113  MachineFunction &MF = DAG.getMachineFunction();
3114  MachineFrameInfo &MFI = MF.getFrameInfo();
3116  auto PtrVT = getPointerTy(DAG.getDataLayout());
3117  bool IsWin64 = Subtarget->isCallingConvWin64(MF.getFunction().getCallingConv());
3118 
3119  SmallVector<SDValue, 8> MemOps;
3120 
3121  static const MCPhysReg GPRArgRegs[] = { AArch64::X0, AArch64::X1, AArch64::X2,
3122  AArch64::X3, AArch64::X4, AArch64::X5,
3123  AArch64::X6, AArch64::X7 };
3124  static const unsigned NumGPRArgRegs = array_lengthof(GPRArgRegs);
3125  unsigned FirstVariadicGPR = CCInfo.getFirstUnallocated(GPRArgRegs);
3126 
3127  unsigned GPRSaveSize = 8 * (NumGPRArgRegs - FirstVariadicGPR);
3128  int GPRIdx = 0;
3129  if (GPRSaveSize != 0) {
3130  if (IsWin64) {
3131  GPRIdx = MFI.CreateFixedObject(GPRSaveSize, -(int)GPRSaveSize, false);
3132  if (GPRSaveSize & 15)
3133  // The extra size here, if triggered, will always be 8.
3134  MFI.CreateFixedObject(16 - (GPRSaveSize & 15), -(int)alignTo(GPRSaveSize, 16), false);
3135  } else
3136  GPRIdx = MFI.CreateStackObject(GPRSaveSize, 8, false);
3137 
3138  SDValue FIN = DAG.getFrameIndex(GPRIdx, PtrVT);
3139 
3140  for (unsigned i = FirstVariadicGPR; i < NumGPRArgRegs; ++i) {
3141  unsigned VReg = MF.addLiveIn(GPRArgRegs[i], &AArch64::GPR64RegClass);
3142  SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
3143  SDValue Store = DAG.getStore(
3144  Val.getValue(1), DL, Val, FIN,
3145  IsWin64
3147  GPRIdx,
3148  (i - FirstVariadicGPR) * 8)
3150  MemOps.push_back(Store);
3151  FIN =
3152  DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getConstant(8, DL, PtrVT));
3153  }
3154  }
3155  FuncInfo->setVarArgsGPRIndex(GPRIdx);
3156  FuncInfo->setVarArgsGPRSize(GPRSaveSize);
3157 
3158  if (Subtarget->hasFPARMv8() && !IsWin64) {
3159  static const MCPhysReg FPRArgRegs[] = {
3160  AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3,
3161  AArch64::Q4, AArch64::Q5, AArch64::Q6, AArch64::Q7};
3162  static const unsigned NumFPRArgRegs = array_lengthof(FPRArgRegs);
3163  unsigned FirstVariadicFPR = CCInfo.getFirstUnallocated(FPRArgRegs);
3164 
3165  unsigned FPRSaveSize = 16 * (NumFPRArgRegs - FirstVariadicFPR);
3166  int FPRIdx = 0;
3167  if (FPRSaveSize != 0) {
3168  FPRIdx = MFI.CreateStackObject(FPRSaveSize, 16, false);
3169 
3170  SDValue FIN = DAG.getFrameIndex(FPRIdx, PtrVT);
3171 
3172  for (unsigned i = FirstVariadicFPR; i < NumFPRArgRegs; ++i) {
3173  unsigned VReg = MF.addLiveIn(FPRArgRegs[i], &AArch64::FPR128RegClass);
3174  SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f128);
3175 
3176  SDValue Store = DAG.getStore(
3177  Val.getValue(1), DL, Val, FIN,
3179  MemOps.push_back(Store);
3180  FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
3181  DAG.getConstant(16, DL, PtrVT));
3182  }
3183  }
3184  FuncInfo->setVarArgsFPRIndex(FPRIdx);
3185  FuncInfo->setVarArgsFPRSize(FPRSaveSize);
3186  }
3187 
3188  if (!MemOps.empty()) {
3189  Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
3190  }
3191 }
3192 
3193 /// LowerCallResult - Lower the result values of a call into the
3194 /// appropriate copies out of appropriate physical registers.
3195 SDValue AArch64TargetLowering::LowerCallResult(
3196  SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
3197  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3198  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
3199  SDValue ThisVal) const {
3200  CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS
3201  ? RetCC_AArch64_WebKit_JS
3202  : RetCC_AArch64_AAPCS;
3203  // Assign locations to each value returned by this call.
3205  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3206  *DAG.getContext());
3207  CCInfo.AnalyzeCallResult(Ins, RetCC);
3208 
3209  // Copy all of the result registers out of their specified physreg.
3210  for (unsigned i = 0; i != RVLocs.size(); ++i) {
3211  CCValAssign VA = RVLocs[i];
3212 
3213  // Pass 'this' value directly from the argument to return value, to avoid
3214  // reg unit interference
3215  if (i == 0 && isThisReturn) {
3216  assert(!VA.needsCustom() && VA.getLocVT() == MVT::i64 &&
3217  "unexpected return calling convention register assignment");
3218  InVals.push_back(ThisVal);
3219  continue;
3220  }
3221 
3222  SDValue Val =
3223  DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InFlag);
3224  Chain = Val.getValue(1);
3225  InFlag = Val.getValue(2);
3226 
3227  switch (VA.getLocInfo()) {
3228  default:
3229  llvm_unreachable("Unknown loc info!");
3230  case CCValAssign::Full:
3231  break;
3232  case CCValAssign::BCvt:
3233  Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3234  break;
3235  }
3236 
3237  InVals.push_back(Val);
3238  }
3239 
3240  return Chain;
3241 }
3242 
3243 /// Return true if the calling convention is one that we can guarantee TCO for.
3245  return CC == CallingConv::Fast;
3246 }
3247 
3248 /// Return true if we might ever do TCO for calls with this calling convention.
3250  switch (CC) {
3251  case CallingConv::C:
3253  case CallingConv::Swift:
3254  return true;
3255  default:
3256  return canGuaranteeTCO(CC);
3257  }
3258 }
3259 
3260 bool AArch64TargetLowering::isEligibleForTailCallOptimization(
3261  SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
3262  const SmallVectorImpl<ISD::OutputArg> &Outs,
3263  const SmallVectorImpl<SDValue> &OutVals,
3264  const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
3265  if (!mayTailCallThisCC(CalleeCC))
3266  return false;
3267 
3268  MachineFunction &MF = DAG.getMachineFunction();
3269  const Function &CallerF = MF.getFunction();
3270  CallingConv::ID CallerCC = CallerF.getCallingConv();
3271  bool CCMatch = CallerCC == CalleeCC;
3272 
3273  // Byval parameters hand the function a pointer directly into the stack area
3274  // we want to reuse during a tail call. Working around this *is* possible (see
3275  // X86) but less efficient and uglier in LowerCall.
3276  for (Function::const_arg_iterator i = CallerF.arg_begin(),
3277  e = CallerF.arg_end();
3278  i != e; ++i)
3279  if (i->hasByValAttr())
3280  return false;
3281 
3283  return canGuaranteeTCO(CalleeCC) && CCMatch;
3284 
3285  // Externally-defined functions with weak linkage should not be
3286  // tail-called on AArch64 when the OS does not support dynamic
3287  // pre-emption of symbols, as the AAELF spec requires normal calls
3288  // to undefined weak functions to be replaced with a NOP or jump to the
3289  // next instruction. The behaviour of branch instructions in this
3290  // situation (as used for tail calls) is implementation-defined, so we
3291  // cannot rely on the linker replacing the tail call with a return.
3292  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3293  const GlobalValue *GV = G->getGlobal();
3294  const Triple &TT = getTargetMachine().getTargetTriple();
3295  if (GV->hasExternalWeakLinkage() &&
3296  (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
3297  return false;
3298  }
3299 
3300  // Now we search for cases where we can use a tail call without changing the
3301  // ABI. Sibcall is used in some places (particularly gcc) to refer to this
3302  // concept.
3303 
3304  // I want anyone implementing a new calling convention to think long and hard
3305  // about this assert.
3306  assert((!isVarArg || CalleeCC == CallingConv::C) &&
3307  "Unexpected variadic calling convention");
3308 
3309  LLVMContext &C = *DAG.getContext();
3310  if (isVarArg && !Outs.empty()) {
3311  // At least two cases here: if caller is fastcc then we can't have any
3312  // memory arguments (we'd be expected to clean up the stack afterwards). If
3313  // caller is C then we could potentially use its argument area.
3314 
3315  // FIXME: for now we take the most conservative of these in both cases:
3316  // disallow all variadic memory operands.
3318  CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
3319 
3320  CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, true));
3321  for (const CCValAssign &ArgLoc : ArgLocs)
3322  if (!ArgLoc.isRegLoc())
3323  return false;
3324  }
3325 
3326  // Check that the call results are passed in the same way.
3327  if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
3328  CCAssignFnForCall(CalleeCC, isVarArg),
3329  CCAssignFnForCall(CallerCC, isVarArg)))
3330  return false;
3331  // The callee has to preserve all registers the caller needs to preserve.
3332  const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
3333  const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
3334  if (!CCMatch) {
3335  const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
3336  if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
3337  return false;
3338  }
3339 
3340  // Nothing more to check if the callee is taking no arguments
3341  if (Outs.empty())
3342  return true;
3343 
3345  CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
3346 
3347  CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg));
3348 
3349  const AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
3350 
3351  // If the stack arguments for this call do not fit into our own save area then
3352  // the call cannot be made tail.
3353  if (CCInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea())
3354  return false;
3355 
3356  const MachineRegisterInfo &MRI = MF.getRegInfo();
3357  if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
3358  return false;
3359 
3360  return true;
3361 }
3362 
3363 SDValue AArch64TargetLowering::addTokenForArgument(SDValue Chain,
3364  SelectionDAG &DAG,
3365  MachineFrameInfo &MFI,
3366  int ClobberedFI) const {
3367  SmallVector<SDValue, 8> ArgChains;
3368  int64_t FirstByte = MFI.getObjectOffset(ClobberedFI);
3369  int64_t LastByte = FirstByte + MFI.getObjectSize(ClobberedFI) - 1;
3370 
3371  // Include the original chain at the beginning of the list. When this is
3372  // used by target LowerCall hooks, this helps legalize find the
3373  // CALLSEQ_BEGIN node.
3374  ArgChains.push_back(Chain);
3375 
3376  // Add a chain value for each stack argument corresponding
3377  for (SDNode::use_iterator U = DAG.getEntryNode().getNode()->use_begin(),
3378  UE = DAG.getEntryNode().getNode()->use_end();
3379  U != UE; ++U)
3380  if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3381  if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3382  if (FI->getIndex() < 0) {
3383  int64_t InFirstByte = MFI.getObjectOffset(FI->getIndex());
3384  int64_t InLastByte = InFirstByte;
3385  InLastByte += MFI.getObjectSize(FI->getIndex()) - 1;
3386 
3387  if ((InFirstByte <= FirstByte && FirstByte <= InLastByte) ||
3388  (FirstByte <= InFirstByte && InFirstByte <= LastByte))
3389  ArgChains.push_back(SDValue(L, 1));
3390  }
3391 
3392  // Build a tokenfactor for all the chains.
3393  return DAG.getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
3394 }
3395 
3396 bool AArch64TargetLowering::DoesCalleeRestoreStack(CallingConv::ID CallCC,
3397  bool TailCallOpt) const {
3398  return CallCC == CallingConv::Fast && TailCallOpt;
3399 }
3400 
3401 /// LowerCall - Lower a call to a callseq_start + CALL + callseq_end chain,
3402 /// and add input and output parameter nodes.
3403 SDValue
3404 AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
3405  SmallVectorImpl<SDValue> &InVals) const {
3406  SelectionDAG &DAG = CLI.DAG;
3407  SDLoc &DL = CLI.DL;
3408  SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3409  SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3410  SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3411  SDValue Chain = CLI.Chain;
3412  SDValue Callee = CLI.Callee;
3413  bool &IsTailCall = CLI.IsTailCall;
3414  CallingConv::ID CallConv = CLI.CallConv;
3415  bool IsVarArg = CLI.IsVarArg;
3416 
3417  MachineFunction &MF = DAG.getMachineFunction();
3418  bool IsThisReturn = false;
3419 
3421  bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
3422  bool IsSibCall = false;
3423 
3424  if (IsTailCall) {
3425  // Check if it's really possible to do a tail call.
3426  IsTailCall = isEligibleForTailCallOptimization(
3427  Callee, CallConv, IsVarArg, Outs, OutVals, Ins, DAG);
3428  if (!IsTailCall && CLI.CS && CLI.CS.isMustTailCall())
3429  report_fatal_error("failed to perform tail call elimination on a call "
3430  "site marked musttail");
3431 
3432  // A sibling call is one where we're under the usual C ABI and not planning
3433  // to change that but can still do a tail call:
3434  if (!TailCallOpt && IsTailCall)
3435  IsSibCall = true;
3436 
3437  if (IsTailCall)
3438  ++NumTailCalls;
3439  }
3440 
3441  // Analyze operands of the call, assigning locations to each operand.
3443  CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3444  *DAG.getContext());
3445 
3446  if (IsVarArg) {
3447  // Handle fixed and variable vector arguments differently.
3448  // Variable vector arguments always go into memory.
3449  unsigned NumArgs = Outs.size();
3450 
3451  for (unsigned i = 0; i != NumArgs; ++i) {
3452  MVT ArgVT = Outs[i].VT;
3453  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
3454  CCAssignFn *AssignFn = CCAssignFnForCall(CallConv,
3455  /*IsVarArg=*/ !Outs[i].IsFixed);
3456  bool Res = AssignFn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3457  assert(!Res && "Call operand has unhandled type");
3458  (void)Res;
3459  }
3460  } else {
3461  // At this point, Outs[].VT may already be promoted to i32. To correctly
3462  // handle passing i8 as i8 instead of i32 on stack, we pass in both i32 and
3463  // i8 to CC_AArch64_AAPCS with i32 being ValVT and i8 being LocVT.
3464  // Since AnalyzeCallOperands uses Ins[].VT for both ValVT and LocVT, here
3465  // we use a special version of AnalyzeCallOperands to pass in ValVT and
3466  // LocVT.
3467  unsigned NumArgs = Outs.size();
3468  for (unsigned i = 0; i != NumArgs; ++i) {
3469  MVT ValVT = Outs[i].VT;
3470  // Get type of the original argument.
3471  EVT ActualVT = getValueType(DAG.getDataLayout(),
3472  CLI.getArgs()[Outs[i].OrigArgIndex].Ty,
3473  /*AllowUnknown*/ true);
3474  MVT ActualMVT = ActualVT.isSimple() ? ActualVT.getSimpleVT() : ValVT;
3475  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
3476  // If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16.
3477  if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8)
3478  ValVT = MVT::i8;
3479  else if (ActualMVT == MVT::i16)
3480  ValVT = MVT::i16;
3481 
3482  CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
3483  bool Res = AssignFn(i, ValVT, ValVT, CCValAssign::Full, ArgFlags, CCInfo);
3484  assert(!Res && "Call operand has unhandled type");
3485  (void)Res;
3486  }
3487  }
3488 
3489  // Get a count of how many bytes are to be pushed on the stack.
3490  unsigned NumBytes = CCInfo.getNextStackOffset();
3491 
3492  if (IsSibCall) {
3493  // Since we're not changing the ABI to make this a tail call, the memory
3494  // operands are already available in the caller's incoming argument space.
3495  NumBytes = 0;
3496  }
3497 
3498  // FPDiff is the byte offset of the call's argument area from the callee's.
3499  // Stores to callee stack arguments will be placed in FixedStackSlots offset
3500  // by this amount for a tail call. In a sibling call it must be 0 because the
3501  // caller will deallocate the entire stack and the callee still expects its
3502  // arguments to begin at SP+0. Completely unused for non-tail calls.
3503  int FPDiff = 0;
3504 
3505  if (IsTailCall && !IsSibCall) {
3506  unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
3507 
3508  // Since callee will pop argument stack as a tail call, we must keep the
3509  // popped size 16-byte aligned.
3510  NumBytes = alignTo(NumBytes, 16);
3511 
3512  // FPDiff will be negative if this tail call requires more space than we
3513  // would automatically have in our incoming argument space. Positive if we
3514  // can actually shrink the stack.
3515  FPDiff = NumReusableBytes - NumBytes;
3516 
3517  // The stack pointer must be 16-byte aligned at all times it's used for a
3518  // memory operation, which in practice means at *all* times and in
3519  // particular across call boundaries. Therefore our own arguments started at
3520  // a 16-byte aligned SP and the delta applied for the tail call should
3521  // satisfy the same constraint.
3522  assert(FPDiff % 16 == 0 && "unaligned stack on tail call");
3523  }
3524 
3525  // Adjust the stack pointer for the new arguments...
3526  // These operations are automatically eliminated by the prolog/epilog pass
3527  if (!IsSibCall)
3528  Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
3529 
3530  SDValue StackPtr = DAG.getCopyFromReg(Chain, DL, AArch64::SP,
3531  getPointerTy(DAG.getDataLayout()));
3532 
3534  SmallVector<SDValue, 8> MemOpChains;
3535  auto PtrVT = getPointerTy(DAG.getDataLayout());
3536 
3537  // Walk the register/memloc assignments, inserting copies/loads.
3538  for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); i != e;
3539  ++i, ++realArgIdx) {
3540  CCValAssign &VA = ArgLocs[i];
3541  SDValue Arg = OutVals[realArgIdx];
3542  ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
3543 
3544  // Promote the value if needed.
3545  switch (VA.getLocInfo()) {
3546  default:
3547  llvm_unreachable("Unknown loc info!");
3548  case CCValAssign::Full:
3549  break;
3550  case CCValAssign::SExt:
3551  Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
3552  break;
3553  case CCValAssign::ZExt:
3554  Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
3555  break;
3556  case CCValAssign::AExt:
3557  if (Outs[realArgIdx].ArgVT == MVT::i1) {
3558  // AAPCS requires i1 to be zero-extended to 8-bits by the caller.
3559  Arg = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Arg);
3560  Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i8, Arg);
3561  }
3562  Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
3563  break;
3564  case CCValAssign::BCvt:
3565  Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
3566  break;
3567  case CCValAssign::FPExt:
3568  Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg);
3569  break;
3570  }
3571 
3572  if (VA.isRegLoc()) {
3573  if (realArgIdx == 0 && Flags.isReturned() && !Flags.isSwiftSelf() &&
3574  Outs[0].VT == MVT::i64) {
3575  assert(VA.getLocVT() == MVT::i64 &&
3576  "unexpected calling convention register assignment");
3577  assert(!Ins.empty() && Ins[0].VT == MVT::i64 &&
3578  "unexpected use of 'returned'");
3579  IsThisReturn = true;
3580  }
3581  RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3582  } else {
3583  assert(VA.isMemLoc());
3584 
3585  SDValue DstAddr;
3586  MachinePointerInfo DstInfo;
3587 
3588  // FIXME: This works on big-endian for composite byvals, which are the
3589  // common case. It should also work for fundamental types too.
3590  uint32_t BEAlign = 0;
3591  unsigned OpSize = Flags.isByVal() ? Flags.getByValSize() * 8
3592  : VA.getValVT().getSizeInBits();
3593  OpSize = (OpSize + 7) / 8;
3594  if (!Subtarget->isLittleEndian() && !Flags.isByVal() &&
3595  !Flags.isInConsecutiveRegs()) {
3596  if (OpSize < 8)
3597  BEAlign = 8 - OpSize;
3598  }
3599  unsigned LocMemOffset = VA.getLocMemOffset();
3600  int32_t Offset = LocMemOffset + BEAlign;
3601  SDValue PtrOff = DAG.getIntPtrConstant(Offset, DL);
3602  PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
3603 
3604  if (IsTailCall) {
3605  Offset = Offset + FPDiff;
3606  int FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true);
3607 
3608  DstAddr = DAG.getFrameIndex(FI, PtrVT);
3609  DstInfo =
3611 
3612  // Make sure any stack arguments overlapping with where we're storing
3613  // are loaded before this eventual operation. Otherwise they'll be
3614  // clobbered.
3615  Chain = addTokenForArgument(Chain, DAG, MF.getFrameInfo(), FI);
3616  } else {
3617  SDValue PtrOff = DAG.getIntPtrConstant(Offset, DL);
3618 
3619  DstAddr = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
3621  LocMemOffset);
3622  }
3623 
3624  if (Outs[i].Flags.isByVal()) {
3625  SDValue SizeNode =
3626  DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i64);
3627  SDValue Cpy = DAG.getMemcpy(
3628  Chain, DL, DstAddr, Arg, SizeNode, Outs[i].Flags.getByValAlign(),
3629  /*isVol = */ false, /*AlwaysInline = */ false,
3630  /*isTailCall = */ false,
3631  DstInfo, MachinePointerInfo());
3632 
3633  MemOpChains.push_back(Cpy);
3634  } else {
3635  // Since we pass i1/i8/i16 as i1/i8/i16 on stack and Arg is already
3636  // promoted to a legal register type i32, we should truncate Arg back to
3637  // i1/i8/i16.
3638  if (VA.getValVT() == MVT::i1 || VA.getValVT() == MVT::i8 ||
3639  VA.getValVT() == MVT::i16)
3640  Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
3641 
3642  SDValue Store = DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo);
3643  MemOpChains.push_back(Store);
3644  }
3645  }
3646  }
3647 
3648  if (!MemOpChains.empty())
3649  Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3650 
3651  // Build a sequence of copy-to-reg nodes chained together with token chain
3652  // and flag operands which copy the outgoing args into the appropriate regs.
3653  SDValue InFlag;
3654  for (auto &RegToPass : RegsToPass) {
3655  Chain = DAG.getCopyToReg(Chain, DL, RegToPass.first,
3656  RegToPass.second, InFlag);
3657  InFlag = Chain.getValue(1);
3658  }
3659 
3660  // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3661  // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3662  // node so that legalize doesn't hack it.
3663  if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3664  auto GV = G->getGlobal();
3665  if (Subtarget->classifyGlobalFunctionReference(GV, getTargetMachine()) ==
3667  Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_GOT);
3668  Callee = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Callee);
3669  } else if (Subtarget->isTargetCOFF() && GV->hasDLLImportStorageClass()) {
3670  assert(Subtarget->isTargetWindows() &&
3671  "Windows is the only supported COFF target");
3672  Callee = getGOT(G, DAG, AArch64II::MO_DLLIMPORT);
3673  } else {
3674  const GlobalValue *GV = G->getGlobal();
3675  Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 0);
3676  }
3677  } else if (auto *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3678  if (getTargetMachine().getCodeModel() == CodeModel::Large &&
3679  Subtarget->isTargetMachO()) {
3680  const char *Sym = S->getSymbol();
3681  Callee = DAG.getTargetExternalSymbol(Sym, PtrVT, AArch64II::MO_GOT);
3682  Callee = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Callee);
3683  } else {
3684  const char *Sym = S->getSymbol();
3685  Callee = DAG.getTargetExternalSymbol(Sym, PtrVT, 0);
3686  }
3687  }
3688 
3689  // We don't usually want to end the call-sequence here because we would tidy
3690  // the frame up *after* the call, however in the ABI-changing tail-call case
3691  // we've carefully laid out the parameters so that when sp is reset they'll be
3692  // in the correct location.
3693  if (IsTailCall && !IsSibCall) {
3694  Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true),
3695  DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
3696  InFlag = Chain.getValue(1);
3697  }
3698 
3699  std::vector<SDValue> Ops;
3700  Ops.push_back(Chain);
3701  Ops.push_back(Callee);
3702 
3703  if (IsTailCall) {
3704  // Each tail call may have to adjust the stack by a different amount, so
3705  // this information must travel along with the operation for eventual
3706  // consumption by emitEpilogue.
3707  Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32));
3708  }
3709 
3710  // Add argument registers to the end of the list so that they are known live
3711  // into the call.
3712  for (auto &RegToPass : RegsToPass)
3713  Ops.push_back(DAG.getRegister(RegToPass.first,
3714  RegToPass.second.getValueType()));
3715 
3716  // Add a register mask operand representing the call-preserved registers.
3717  const uint32_t *Mask;
3718  const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
3719  if (IsThisReturn) {
3720  // For 'this' returns, use the X0-preserving mask if applicable
3721  Mask = TRI->getThisReturnPreservedMask(MF, CallConv);
3722  if (!Mask) {
3723  IsThisReturn = false;
3724  Mask = TRI->getCallPreservedMask(MF, CallConv);
3725  }
3726  } else
3727  Mask = TRI->getCallPreservedMask(MF, CallConv);
3728 
3729  assert(Mask && "Missing call preserved mask for calling convention");
3730  Ops.push_back(DAG.getRegisterMask(Mask));
3731 
3732  if (InFlag.getNode())
3733  Ops.push_back(InFlag);
3734 
3735  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3736 
3737  // If we're doing a tall call, use a TC_RETURN here rather than an
3738  // actual call instruction.
3739  if (IsTailCall) {
3741  return DAG.getNode(AArch64ISD::TC_RETURN, DL, NodeTys, Ops);
3742  }
3743 
3744  // Returns a chain and a flag for retval copy to use.
3745  Chain = DAG.getNode(AArch64ISD::CALL, DL, NodeTys, Ops);
3746  InFlag = Chain.getValue(1);
3747 
3748  uint64_t CalleePopBytes =
3749  DoesCalleeRestoreStack(CallConv, TailCallOpt) ? alignTo(NumBytes, 16) : 0;
3750 
3751  Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true),
3752  DAG.getIntPtrConstant(CalleePopBytes, DL, true),
3753  InFlag, DL);
3754  if (!Ins.empty())
3755  InFlag = Chain.getValue(1);
3756 
3757  // Handle result values, copying them out of physregs into vregs that we
3758  // return.
3759  return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
3760  InVals, IsThisReturn,
3761  IsThisReturn ? OutVals[0] : SDValue());
3762 }
3763 
3764 bool AArch64TargetLowering::CanLowerReturn(
3765  CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
3766  const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
3767  CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS
3768  ? RetCC_AArch64_WebKit_JS
3769  : RetCC_AArch64_AAPCS;
3771  CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
3772  return CCInfo.CheckReturn(Outs, RetCC);
3773 }
3774 
3775 SDValue
3776 AArch64TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3777  bool isVarArg,
3778  const SmallVectorImpl<ISD::OutputArg> &Outs,
3779  const SmallVectorImpl<SDValue> &OutVals,
3780  const SDLoc &DL, SelectionDAG &DAG) const {
3781  CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS
3782  ? RetCC_AArch64_WebKit_JS
3783  : RetCC_AArch64_AAPCS;
3785  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3786  *DAG.getContext());
3787  CCInfo.AnalyzeReturn(Outs, RetCC);
3788 
3789  // Copy the result values into the output registers.
3790  SDValue Flag;
3791  SmallVector<SDValue, 4> RetOps(1, Chain);
3792  for (unsigned i = 0, realRVLocIdx = 0; i != RVLocs.size();
3793  ++i, ++realRVLocIdx) {
3794  CCValAssign &VA = RVLocs[i];
3795  assert(VA.isRegLoc() && "Can only return in registers!");
3796  SDValue Arg = OutVals[realRVLocIdx];
3797 
3798  switch (VA.getLocInfo()) {
3799  default:
3800  llvm_unreachable("Unknown loc info!");
3801  case CCValAssign::Full:
3802  if (Outs[i].ArgVT == MVT::i1) {
3803  // AAPCS requires i1 to be zero-extended to i8 by the producer of the
3804  // value. This is strictly redundant on Darwin (which uses "zeroext
3805  // i1"), but will be optimised out before ISel.
3806  Arg = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Arg);
3807  Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
3808  }
3809  break;
3810  case CCValAssign::BCvt:
3811  Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
3812  break;
3813  }
3814 
3815  Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
3816  Flag = Chain.getValue(1);
3817  RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3818  }
3819  const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
3820  const MCPhysReg *I =
3822  if (I) {
3823  for (; *I; ++I) {
3824  if (AArch64::GPR64RegClass.contains(*I))
3825  RetOps.push_back(DAG.getRegister(*I, MVT::i64));
3826  else if (AArch64::FPR64RegClass.contains(*I))
3827  RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
3828  else
3829  llvm_unreachable("Unexpected register class in CSRsViaCopy!");
3830  }
3831  }
3832 
3833  RetOps[0] = Chain; // Update chain.
3834 
3835  // Add the flag if we have it.
3836  if (Flag.getNode())
3837  RetOps.push_back(Flag);
3838 
3839  return DAG.getNode(AArch64ISD::RET_FLAG, DL, MVT::Other, RetOps);
3840 }
3841 
3842 //===----------------------------------------------------------------------===//
3843 // Other Lowering Code
3844 //===----------------------------------------------------------------------===//
3845 
3846 SDValue AArch64TargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
3847  SelectionDAG &DAG,
3848  unsigned Flag) const {
3849  return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty,
3850  N->getOffset(), Flag);
3851 }
3852 
3853 SDValue AArch64TargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
3854  SelectionDAG &DAG,
3855  unsigned Flag) const {
3856  return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
3857 }
3858 
3859 SDValue AArch64TargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
3860  SelectionDAG &DAG,
3861  unsigned Flag) const {
3862  return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
3863  N->getOffset(), Flag);
3864 }
3865 
3866 SDValue AArch64TargetLowering::getTargetNode(BlockAddressSDNode* N, EVT Ty,
3867  SelectionDAG &DAG,
3868  unsigned Flag) const {
3869  return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
3870 }
3871 
3872 // (loadGOT sym)
3873 template <class NodeTy>
3874 SDValue AArch64TargetLowering::getGOT(NodeTy *N, SelectionDAG &DAG,
3875  unsigned Flags) const {
3876  LLVM_DEBUG(dbgs() << "AArch64TargetLowering::getGOT\n");
3877  SDLoc DL(N);
3878  EVT Ty = getPointerTy(DAG.getDataLayout());
3879  SDValue GotAddr = getTargetNode(N, Ty, DAG, AArch64II::MO_GOT | Flags);
3880  // FIXME: Once remat is capable of dealing with instructions with register
3881  // operands, expand this into two nodes instead of using a wrapper node.
3882  return DAG.getNode(AArch64ISD::LOADgot, DL, Ty, GotAddr);
3883 }
3884 
3885 // (wrapper %highest(sym), %higher(sym), %hi(sym), %lo(sym))
3886 template <class NodeTy>
3887 SDValue AArch64TargetLowering::getAddrLarge(NodeTy *N, SelectionDAG &DAG,
3888  unsigned Flags) const {
3889  LLVM_DEBUG(dbgs() << "AArch64TargetLowering::getAddrLarge\n");
3890  SDLoc DL(N);
3891  EVT Ty = getPointerTy(DAG.getDataLayout());
3892  const unsigned char MO_NC = AArch64II::MO_NC;
3893  return DAG.getNode(
3894  AArch64ISD::WrapperLarge, DL, Ty,
3895  getTargetNode(N, Ty, DAG, AArch64II::MO_G3 | Flags),
3896  getTargetNode(N, Ty, DAG, AArch64II::MO_G2 | MO_NC | Flags),
3897  getTargetNode(N, Ty, DAG, AArch64II::MO_G1 | MO_NC | Flags),
3898  getTargetNode(N, Ty, DAG, AArch64II::MO_G0 | MO_NC | Flags));
3899 }
3900 
3901 // (addlow (adrp %hi(sym)) %lo(sym))
3902 template <class NodeTy>
3903 SDValue AArch64TargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
3904  unsigned Flags) const {
3905  LLVM_DEBUG(dbgs() << "AArch64TargetLowering::getAddr\n");
3906  SDLoc DL(N);
3907  EVT Ty = getPointerTy(DAG.getDataLayout());
3908  SDValue Hi = getTargetNode(N, Ty, DAG, AArch64II::MO_PAGE | Flags);
3909  SDValue Lo = getTargetNode(N, Ty, DAG,
3911  SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, Ty, Hi);
3912  return DAG.getNode(AArch64ISD::ADDlow, DL, Ty, ADRP, Lo);
3913 }
3914 
3915 SDValue AArch64TargetLowering::LowerGlobalAddress(SDValue Op,
3916  SelectionDAG &DAG) const {
3917  GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
3918  const GlobalValue *GV = GN->getGlobal();
3919  const AArch64II::TOF TargetFlags =
3920  (GV->hasDLLImportStorageClass() ? AArch64II::MO_DLLIMPORT
3922  unsigned char OpFlags =
3923  Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
3924 
3925  if (OpFlags != AArch64II::MO_NO_FLAG)
3926  assert(cast<GlobalAddressSDNode>(Op)->getOffset() == 0 &&
3927  "unexpected offset in global node");
3928 
3929  // This also catches the large code model case for Darwin.
3930  if ((OpFlags & AArch64II::MO_GOT) != 0) {
3931  return getGOT(GN, DAG, TargetFlags);
3932  }
3933 
3934  SDValue Result;
3935  if (getTargetMachine().getCodeModel() == CodeModel::Large) {
3936  Result = getAddrLarge(GN, DAG, TargetFlags);
3937  } else {
3938  Result = getAddr(GN, DAG, TargetFlags);
3939  }
3940  EVT PtrVT = getPointerTy(DAG.getDataLayout());
3941  SDLoc DL(GN);
3942  if (GV->hasDLLImportStorageClass())
3943  Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
3945  return Result;
3946 }
3947 
3948 /// Convert a TLS address reference into the correct sequence of loads
3949 /// and calls to compute the variable's address (for Darwin, currently) and
3950 /// return an SDValue containing the final node.
3951 
3952 /// Darwin only has one TLS scheme which must be capable of dealing with the
3953 /// fully general situation, in the worst case. This means:
3954 /// + "extern __thread" declaration.
3955 /// + Defined in a possibly unknown dynamic library.
3956 ///
3957 /// The general system is that each __thread variable has a [3 x i64] descriptor
3958 /// which contains information used by the runtime to calculate the address. The
3959 /// only part of this the compiler needs to know about is the first xword, which
3960 /// contains a function pointer that must be called with the address of the
3961 /// entire descriptor in "x0".
3962 ///
3963 /// Since this descriptor may be in a different unit, in general even the
3964 /// descriptor must be accessed via an indirect load. The "ideal" code sequence
3965 /// is:
3966 /// adrp x0, _var@TLVPPAGE
3967 /// ldr x0, [x0, _var@TLVPPAGEOFF] ; x0 now contains address of descriptor
3968 /// ldr x1, [x0] ; x1 contains 1st entry of descriptor,
3969 /// ; the function pointer
3970 /// blr x1 ; Uses descriptor address in x0
3971 /// ; Address of _var is now in x0.
3972 ///
3973 /// If the address of _var's descriptor *is* known to the linker, then it can
3974 /// change the first "ldr" instruction to an appropriate "add x0, x0, #imm" for
3975 /// a slight efficiency gain.
3976 SDValue
3977 AArch64TargetLowering::LowerDarwinGlobalTLSAddress(SDValue Op,
3978  SelectionDAG &DAG) const {
3979  assert(Subtarget->isTargetDarwin() &&
3980  "This function expects a Darwin target");
3981 
3982  SDLoc DL(Op);
3983  MVT PtrVT = getPointerTy(DAG.getDataLayout());
3984  const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3985 
3986  SDValue TLVPAddr =
3987  DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_TLS);
3988  SDValue DescAddr = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, TLVPAddr);
3989 
3990  // The first entry in the descriptor is a function pointer that we must call
3991  // to obtain the address of the variable.
3992  SDValue Chain = DAG.getEntryNode();
3993  SDValue FuncTLVGet = DAG.getLoad(
3994  MVT::i64, DL, Chain, DescAddr,
3996  /* Alignment = */ 8,
3999  Chain = FuncTLVGet.getValue(1);
4000 
4002  MFI.setAdjustsStack(true);
4003 
4004  // TLS calls preserve all registers except those that absolutely must be
4005  // trashed: X0 (it takes an argument), LR (it's a call) and NZCV (let's not be
4006  // silly).
4007  const uint32_t *Mask =
4008  Subtarget->getRegisterInfo()->getTLSCallPreservedMask();
4009 
4010  // Finally, we can make the call. This is just a degenerate version of a
4011  // normal AArch64 call node: x0 takes the address of the descriptor, and
4012  // returns the address of the variable in this thread.
4013  Chain = DAG.getCopyToReg(Chain, DL, AArch64::X0, DescAddr, SDValue());
4014  Chain =
4016  Chain, FuncTLVGet, DAG.getRegister(AArch64::X0, MVT::i64),
4017  DAG.getRegisterMask(Mask), Chain.getValue(1));
4018  return DAG.getCopyFromReg(Chain, DL, AArch64::X0, PtrVT, Chain.getValue(1));
4019 }
4020 
4021 /// When accessing thread-local variables under either the general-dynamic or
4022 /// local-dynamic system, we make a "TLS-descriptor" call. The variable will
4023 /// have a descriptor, accessible via a PC-relative ADRP, and whose first entry
4024 /// is a function pointer to carry out the resolution.
4025 ///
4026 /// The sequence is:
4027 /// adrp x0, :tlsdesc:var
4028 /// ldr x1, [x0, #:tlsdesc_lo12:var]
4029 /// add x0, x0, #:tlsdesc_lo12:var
4030 /// .tlsdesccall var
4031 /// blr x1
4032 /// (TPIDR_EL0 offset now in x0)
4033 ///
4034 /// The above sequence must be produced unscheduled, to enable the linker to
4035 /// optimize/relax this sequence.
4036 /// Therefore, a pseudo-instruction (TLSDESC_CALLSEQ) is used to represent the
4037 /// above sequence, and expanded really late in the compilation flow, to ensure
4038 /// the sequence is produced as per above.
4039 SDValue AArch64TargetLowering::LowerELFTLSDescCallSeq(SDValue SymAddr,
4040  const SDLoc &DL,
4041  SelectionDAG &DAG) const {
4042  EVT PtrVT = getPointerTy(DAG.getDataLayout());
4043 
4044  SDValue Chain = DAG.getEntryNode();
4045  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
4046 
4047  Chain =
4048  DAG.getNode(AArch64ISD::TLSDESC_CALLSEQ, DL, NodeTys, {Chain, SymAddr});
4049  SDValue Glue = Chain.getValue(1);
4050 
4051  return DAG.getCopyFromReg(Chain, DL, AArch64::X0, PtrVT, Glue);
4052 }
4053 
4054 SDValue
4055 AArch64TargetLowering::LowerELFGlobalTLSAddress(SDValue Op,
4056  SelectionDAG &DAG) const {
4057  assert(Subtarget->isTargetELF() && "This function expects an ELF target");
4058  assert(Subtarget->useSmallAddressing() &&
4059  "ELF TLS only supported in small memory model");
4060  // Different choices can be made for the maximum size of the TLS area for a
4061  // module. For the small address model, the default TLS size is 16MiB and the
4062  // maximum TLS size is 4GiB.
4063  // FIXME: add -mtls-size command line option and make it control the 16MiB
4064  // vs. 4GiB code sequence generation.
4065  const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4066 
4067  TLSModel::Model Model = getTargetMachine().getTLSModel(GA->getGlobal());
4068 
4070  if (Model == TLSModel::LocalDynamic)
4071  Model = TLSModel::GeneralDynamic;
4072  }
4073 
4074  SDValue TPOff;
4075  EVT PtrVT = getPointerTy(DAG.getDataLayout());
4076  SDLoc DL(Op);
4077  const GlobalValue *GV = GA->getGlobal();
4078 
4079  SDValue ThreadBase = DAG.getNode(AArch64ISD::THREAD_POINTER, DL, PtrVT);
4080 
4081  if (Model == TLSModel::LocalExec) {
4082  SDValue HiVar = DAG.getTargetGlobalAddress(
4083  GV, DL, PtrVT, 0, AArch64II::MO_TLS | AArch64II::MO_HI12);
4084  SDValue LoVar = DAG.getTargetGlobalAddress(
4085  GV, DL, PtrVT, 0,
4087 
4088  SDValue TPWithOff_lo =
4089  SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, ThreadBase,
4090  HiVar,
4091  DAG.getTargetConstant(0, DL, MVT::i32)),
4092  0);
4093  SDValue TPWithOff =
4094  SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TPWithOff_lo,
4095  LoVar,
4096  DAG.getTargetConstant(0, DL, MVT::i32)),
4097  0);
4098  return TPWithOff;
4099  } else if (Model == TLSModel::InitialExec) {
4100  TPOff = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_TLS);
4101  TPOff = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, TPOff);
4102  } else if (Model == TLSModel::LocalDynamic) {
4103  // Local-dynamic accesses proceed in two phases. A general-dynamic TLS
4104  // descriptor call against the special symbol _TLS_MODULE_BASE_ to calculate
4105  // the beginning of the module's TLS region, followed by a DTPREL offset
4106  // calculation.
4107 
4108  // These accesses will need deduplicating if there's more than one.
4109  AArch64FunctionInfo *MFI =
4112 
4113  // The call needs a relocation too for linker relaxation. It doesn't make
4114  // sense to call it MO_PAGE or MO_PAGEOFF though so we need another copy of
4115  // the address.
4116  SDValue SymAddr = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT,
4118 
4119  // Now we can calculate the offset from TPIDR_EL0 to this module's
4120  // thread-local area.
4121  TPOff = LowerELFTLSDescCallSeq(SymAddr, DL, DAG);
4122 
4123  // Now use :dtprel_whatever: operations to calculate this variable's offset
4124  // in its thread-storage area.
4125  SDValue HiVar = DAG.getTargetGlobalAddress(
4127  SDValue LoVar = DAG.getTargetGlobalAddress(
4128  GV, DL, MVT::i64, 0,
4130 
4131  TPOff = SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TPOff, HiVar,
4132  DAG.getTargetConstant(0, DL, MVT::i32)),
4133  0);
4134  TPOff = SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TPOff, LoVar,
4135  DAG.getTargetConstant(0, DL, MVT::i32)),
4136  0);
4137  } else if (Model == TLSModel::GeneralDynamic) {
4138  // The call needs a relocation too for linker relaxation. It doesn't make
4139  // sense to call it MO_PAGE or MO_PAGEOFF though so we need another copy of
4140  // the address.
4141  SDValue SymAddr =
4142  DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_TLS);
4143 
4144  // Finally we can make a call to calculate the offset from tpidr_el0.
4145  TPOff = LowerELFTLSDescCallSeq(SymAddr, DL, DAG);
4146  } else
4147  llvm_unreachable("Unsupported ELF TLS access model");
4148 
4149  return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadBase, TPOff);
4150 }
4151 
4152 SDValue
4153 AArch64TargetLowering::LowerWindowsGlobalTLSAddress(SDValue Op,
4154  SelectionDAG &DAG) const {
4155  assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
4156 
4157  SDValue Chain = DAG.getEntryNode();
4158  EVT PtrVT = getPointerTy(DAG.getDataLayout());
4159  SDLoc DL(Op);
4160 
4161  SDValue TEB = DAG.getRegister(AArch64::X18, MVT::i64);
4162 
4163  // Load the ThreadLocalStoragePointer from the TEB
4164  // A pointer to the TLS array is located at offset 0x58 from the TEB.
4165  SDValue TLSArray =
4166  DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x58, DL));
4167  TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo());
4168  Chain = TLSArray.getValue(1);
4169 
4170  // Load the TLS index from the C runtime;
4171  // This does the same as getAddr(), but without having a GlobalAddressSDNode.
4172  // This also does the same as LOADgot, but using a generic i32 load,
4173  // while LOADgot only loads i64.
4174  SDValue TLSIndexHi =
4175  DAG.getTargetExternalSymbol("_tls_index", PtrVT, AArch64II::MO_PAGE);
4176  SDValue TLSIndexLo = DAG.getTargetExternalSymbol(
4177  "_tls_index", PtrVT, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
4178  SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, TLSIndexHi);
4179  SDValue TLSIndex =
4180  DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, TLSIndexLo);
4181  TLSIndex = DAG.getLoad(MVT::i32, DL, Chain, TLSIndex, MachinePointerInfo());
4182  Chain = TLSIndex.getValue(1);
4183 
4184  // The pointer to the thread's TLS data area is at the TLS Index scaled by 8
4185  // offset into the TLSArray.
4186  TLSIndex = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TLSIndex);
4187  SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
4188  DAG.getConstant(3, DL, PtrVT));
4189  SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
4190  DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
4191  MachinePointerInfo());
4192  Chain = TLS.getValue(1);
4193 
4194  const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4195  const GlobalValue *GV = GA->getGlobal();
4196  SDValue TGAHi = DAG.getTargetGlobalAddress(
4197  GV, DL, PtrVT, 0, AArch64II::MO_TLS | AArch64II::MO_HI12);
4198  SDValue TGALo = DAG.getTargetGlobalAddress(
4199  GV, DL, PtrVT, 0,
4201 
4202  // Add the offset from the start of the .tls section (section base).
4203  SDValue Addr =
4204  SDValue(DAG.getMachineNode(AArch64::ADDXri, DL, PtrVT, TLS, TGAHi,
4205  DAG.getTargetConstant(0, DL, MVT::i32)),
4206  0);
4207  Addr = DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, Addr, TGALo);
4208  return Addr;
4209 }
4210 
4211 SDValue AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op,
4212  SelectionDAG &DAG) const {
4213  const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4214  if (DAG.getTarget().useEmulatedTLS())
4215  return LowerToTLSEmulatedModel(GA, DAG);
4216 
4217  if (Subtarget->isTargetDarwin())
4218  return LowerDarwinGlobalTLSAddress(Op, DAG);
4219  if (Subtarget->isTargetELF())
4220  return LowerELFGlobalTLSAddress(Op, DAG);
4221  if (Subtarget->isTargetWindows())
4222  return LowerWindowsGlobalTLSAddress(Op, DAG);
4223 
4224  llvm_unreachable("Unexpected platform trying to use TLS");
4225 }
4226 
4227 SDValue AArch64TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
4228  SDValue Chain = Op.getOperand(0);
4229  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
4230  SDValue LHS = Op.getOperand(2);
4231  SDValue RHS = Op.getOperand(3);
4232  SDValue Dest = Op.getOperand(4);
4233  SDLoc dl(Op);
4234 
4235  // Handle f128 first, since lowering it will result in comparing the return
4236  // value of a libcall against zero, which is just what the rest of LowerBR_CC
4237  // is expecting to deal with.
4238  if (LHS.getValueType() == MVT::f128) {
4239  softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl);
4240 
4241  // If softenSetCCOperands returned a scalar, we need to compare the result
4242  // against zero to select between true and false values.
4243  if (!RHS.getNode()) {
4244  RHS = DAG.getConstant(0, dl, LHS.getValueType());
4245  CC = ISD::SETNE;
4246  }
4247  }
4248 
4249  // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch
4250  // instruction.
4251  if (isOverflowIntrOpRes(LHS) && isOneConstant(RHS) &&
4252  (CC == ISD::SETEQ || CC == ISD::SETNE)) {
4253  // Only lower legal XALUO ops.
4254  if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0)))
4255  return SDValue();
4256 
4257  // The actual operation with overflow check.
4258  AArch64CC::CondCode OFCC;
4259  SDValue Value, Overflow;
4260  std::tie(Value, Overflow) = getAArch64XALUOOp(OFCC, LHS.getValue(0), DAG);
4261 
4262  if (CC == ISD::SETNE)
4263  OFCC = getInvertedCondCode(OFCC);
4264  SDValue CCVal = DAG.getConstant(OFCC, dl, MVT::i32);
4265 
4266  return DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal,
4267  Overflow);
4268  }
4269 
4270  if (LHS.getValueType().isInteger()) {
4271  assert((LHS.getValueType() == RHS.getValueType()) &&
4272  (LHS.getValueType() == MVT::i32 || LHS.getValueType() == MVT::i64));
4273 
4274  // If the RHS of the comparison is zero, we can potentially fold this
4275  // to a specialized branch.
4276  const ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS);
4277  if (RHSC && RHSC->getZExtValue() == 0) {
4278  if (CC == ISD::SETEQ) {
4279  // See if we can use a TBZ to fold in an AND as well.
4280  // TBZ has a smaller branch displacement than CBZ. If the offset is
4281  // out of bounds, a late MI-layer pass rewrites branches.
4282  // 403.gcc is an example that hits this case.
4283  if (LHS.getOpcode() == ISD::AND &&
4284  isa<ConstantSDNode>(LHS.getOperand(1)) &&
4286  SDValue Test = LHS.getOperand(0);
4287  uint64_t Mask = LHS.getConstantOperandVal(1);
4288  return DAG.getNode(AArch64ISD::TBZ, dl, MVT::Other, Chain, Test,
4289  DAG.getConstant(Log2_64(Mask), dl, MVT::i64),
4290  Dest);
4291  }
4292 
4293  return DAG.getNode(AArch64ISD::CBZ, dl, MVT::Other, Chain, LHS, Dest);
4294  } else if (CC == ISD::SETNE) {
4295  // See if we can use a TBZ to fold in an AND as well.
4296  // TBZ has a smaller branch displacement than CBZ. If the offset is
4297  // out of bounds, a late MI-layer pass rewrites branches.
4298  // 403.gcc is an example that hits this case.
4299  if (LHS.getOpcode() == ISD::AND &&
4300  isa<ConstantSDNode>(LHS.getOperand(1)) &&
4302  SDValue Test = LHS.getOperand(0);
4303  uint64_t Mask = LHS.getConstantOperandVal(1);
4304  return DAG.getNode(AArch64ISD::TBNZ, dl, MVT::Other, Chain, Test,
4305  DAG.getConstant(Log2_64(Mask), dl, MVT::i64),
4306  Dest);
4307  }
4308 
4309  return DAG.getNode(AArch64ISD::CBNZ, dl, MVT::Other, Chain, LHS, Dest);
4310  } else if (CC == ISD::SETLT && LHS.getOpcode() != ISD::AND) {
4311  // Don't combine AND since emitComparison converts the AND to an ANDS
4312  // (a.k.a. TST) and the test in the test bit and branch instruction
4313  // becomes redundant. This would also increase register pressure.
4314  uint64_t Mask = LHS.getValueSizeInBits() - 1;
4315  return DAG.getNode(AArch64ISD::TBNZ, dl, MVT::Other, Chain, LHS,
4316  DAG.getConstant(Mask, dl, MVT::i64), Dest);
4317  }
4318  }
4319  if (RHSC && RHSC->getSExtValue() == -1 && CC == ISD::SETGT &&
4320  LHS.getOpcode() != ISD::AND) {
4321  // Don't combine AND since emitComparison converts the AND to an ANDS
4322  // (a.k.a. TST) and the test in the test bit and branch instruction
4323  // becomes redundant. This would also increase register pressure.
4324  uint64_t Mask = LHS.getValueSizeInBits() - 1;
4325  return DAG.getNode(AArch64ISD::TBZ, dl, MVT::Other, Chain, LHS,
4326  DAG.getConstant(Mask, dl, MVT::i64), Dest);
4327  }
4328 
4329  SDValue CCVal;
4330  SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl);
4331  return DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal,
4332  Cmp);
4333  }
4334 
4335  assert(LHS.getValueType() == MVT::f16 || LHS.getValueType() == MVT::f32 ||
4336  LHS.getValueType() == MVT::f64);
4337 
4338  // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't totally
4339  // clean. Some of them require two branches to implement.
4340  SDValue Cmp = emitComparison(LHS, RHS, CC, dl, DAG);
4341  AArch64CC::CondCode CC1, CC2;
4342  changeFPCCToAArch64CC(CC, CC1, CC2);
4343  SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32);
4344  SDValue BR1 =
4345  DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, Chain, Dest, CC1Val, Cmp);
4346  if (CC2 != AArch64CC::AL) {
4347  SDValue CC2Val = DAG.getConstant(CC2, dl, MVT::i32);
4348  return DAG.getNode(AArch64ISD::BRCOND, dl, MVT::Other, BR1, Dest, CC2Val,
4349  Cmp);
4350  }
4351 
4352  return BR1;
4353 }
4354 
4355 SDValue AArch64TargetLowering::LowerFCOPYSIGN(SDValue Op,
4356  SelectionDAG &DAG) const {
4357  EVT VT = Op.getValueType();
4358  SDLoc DL(Op);
4359 
4360  SDValue In1 = Op.getOperand(0);
4361  SDValue In2 = Op.getOperand(1);
4362  EVT SrcVT = In2.getValueType();
4363 
4364  if (SrcVT.bitsLT(VT))
4365  In2 = DAG.getNode(ISD::FP_EXTEND, DL, VT, In2);
4366  else if (SrcVT.bitsGT(VT))
4367  In2 = DAG.getNode(ISD::FP_ROUND, DL, VT, In2, DAG.getIntPtrConstant(0, DL));
4368 
4369  EVT VecVT;
4370  uint64_t EltMask;
4371  SDValue VecVal1, VecVal2;
4372 
4373  auto setVecVal = [&] (int Idx) {
4374  if (!VT.isVector()) {
4375  VecVal1 = DAG.getTargetInsertSubreg(Idx, DL, VecVT,
4376  DAG.getUNDEF(VecVT), In1);
4377  VecVal2 = DAG.getTargetInsertSubreg(Idx, DL, VecVT,
4378  DAG.getUNDEF(VecVT), In2);
4379  } else {
4380  VecVal1 = DAG.getNode(ISD::BITCAST, DL, VecVT, In1);
4381  VecVal2 = DAG.getNode(ISD::BITCAST, DL, VecVT, In2);
4382  }
4383  };
4384 
4385  if (VT == MVT::f32 || VT == MVT::v2f32 || VT == MVT::v4f32) {
4386  VecVT = (VT == MVT::v2f32 ? MVT::v2i32 : MVT::v4i32);
4387  EltMask = 0x80000000ULL;
4388  setVecVal(AArch64::ssub);
4389  } else if (VT == MVT::f64 || VT == MVT::v2f64) {
4390  VecVT = MVT::v2i64;
4391 
4392  // We want to materialize a mask with the high bit set, but the AdvSIMD
4393  // immediate moves cannot materialize that in a single instruction for
4394  // 64-bit elements. Instead, materialize zero and then negate it.
4395  EltMask = 0;
4396 
4397  setVecVal(AArch64::dsub);
4398  } else if (VT == MVT::f16 || VT == MVT::v4f16 || VT == MVT::v8f16) {
4399  VecVT = (VT == MVT::v4f16 ? MVT::v4i16 : MVT::v8i16);
4400  EltMask = 0x8000ULL;
4401  setVecVal(AArch64::hsub);
4402  } else {
4403  llvm_unreachable("Invalid type for copysign!");
4404  }
4405 
4406  SDValue BuildVec = DAG.getConstant(EltMask, DL, VecVT);
4407 
4408  // If we couldn't materialize the mask above, then the mask vector will be
4409  // the zero vector, and we need to negate it here.
4410  if (VT == MVT::f64 || VT == MVT::v2f64) {
4411  BuildVec = DAG.getNode(ISD::BITCAST, DL, MVT::v2f64, BuildVec);
4412  BuildVec = DAG.getNode(ISD::FNEG, DL, MVT::v2f64, BuildVec);
4413  BuildVec = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, BuildVec);
4414  }
4415 
4416  SDValue Sel =
4417  DAG.getNode(AArch64ISD::BIT, DL, VecVT, VecVal1, VecVal2, BuildVec);
4418 
4419  if (VT == MVT::f16)
4420  return DAG.getTargetExtractSubreg(AArch64::hsub, DL, VT, Sel);
4421  if (VT == MVT::f32)
4422  return DAG.getTargetExtractSubreg(AArch64::ssub, DL, VT, Sel);
4423  else if (VT == MVT::f64)
4424  return DAG.getTargetExtractSubreg(AArch64::dsub, DL, VT, Sel);
4425  else
4426  return DAG.getNode(ISD::BITCAST, DL, VT, Sel);
4427 }
4428 
4429 SDValue AArch64TargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
4431  Attribute::NoImplicitFloat))
4432  return SDValue();
4433 
4434  if (!Subtarget->hasNEON())
4435  return SDValue();
4436 
4437  // While there is no integer popcount instruction, it can
4438  // be more efficiently lowered to the following sequence that uses
4439  // AdvSIMD registers/instructions as long as the copies to/from
4440  // the AdvSIMD registers are cheap.
4441  // FMOV D0, X0 // copy 64-bit int to vector, high bits zero'd
4442  // CNT V0.8B, V0.8B // 8xbyte pop-counts
4443  // ADDV B0, V0.8B // sum 8xbyte pop-counts
4444  // UMOV X0, V0.B[0] // copy byte result back to integer reg
4445  SDValue Val = Op.getOperand(0);
4446  SDLoc DL(Op);
4447  EVT VT = Op.getValueType();
4448 
4449  if (VT == MVT::i32)
4450  Val = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Val);
4451  Val = DAG.getNode(ISD::BITCAST, DL, MVT::v8i8, Val);
4452 
4453  SDValue CtPop = DAG.getNode(ISD::CTPOP, DL, MVT::v8i8, Val);
4454  SDValue UaddLV = DAG.getNode(
4456  DAG.getConstant(Intrinsic::aarch64_neon_uaddlv, DL, MVT::i32), CtPop);
4457 
4458  if (VT == MVT::i64)
4459  UaddLV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, UaddLV);
4460  return UaddLV;
4461 }
4462 
4463 SDValue AArch64TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
4464 
4465  if (Op.getValueType().isVector())
4466  return LowerVSETCC(Op, DAG);
4467 
4468  SDValue LHS = Op.getOperand(0);
4469  SDValue RHS = Op.getOperand(1);
4470  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
4471  SDLoc dl(Op);
4472 
4473  // We chose ZeroOrOneBooleanContents, so use zero and one.
4474  EVT VT = Op.getValueType();
4475  SDValue TVal = DAG.getConstant(1, dl, VT);
4476  SDValue FVal = DAG.getConstant(0, dl, VT);
4477 
4478  // Handle f128 first, since one possible outcome is a normal integer
4479  // comparison which gets picked up by the next if statement.
4480  if (LHS.getValueType() == MVT::f128) {
4481  softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl);
4482 
4483  // If softenSetCCOperands returned a scalar, use it.
4484  if (!RHS.getNode()) {
4485  assert(LHS.getValueType() == Op.getValueType() &&
4486  "Unexpected setcc expansion!");
4487  return LHS;
4488  }
4489  }
4490 
4491  if (LHS.getValueType().isInteger()) {
4492  SDValue CCVal;
4493  SDValue Cmp =
4494  getAArch64Cmp(LHS, RHS, ISD::getSetCCInverse(CC, true), CCVal, DAG, dl);
4495 
4496  // Note that we inverted the condition above, so we reverse the order of
4497  // the true and false operands here. This will allow the setcc to be
4498  // matched to a single CSINC instruction.
4499  return DAG.getNode(AArch64ISD::CSEL, dl, VT, FVal, TVal, CCVal, Cmp);
4500  }
4501 
4502  // Now we know we're dealing with FP values.
4503  assert(LHS.getValueType() == MVT::f16 || LHS.getValueType() == MVT::f32 ||
4504  LHS.getValueType() == MVT::f64);
4505 
4506  // If that fails, we'll need to perform an FCMP + CSEL sequence. Go ahead
4507  // and do the comparison.
4508  SDValue Cmp = emitComparison(LHS, RHS, CC, dl, DAG);
4509 
4510  AArch64CC::CondCode CC1, CC2;
4511  changeFPCCToAArch64CC(CC, CC1, CC2);
4512  if (CC2 == AArch64CC::AL) {
4513  changeFPCCToAArch64CC(ISD::getSetCCInverse(CC, false), CC1, CC2);
4514  SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32);
4515 
4516  // Note that we inverted the condition above, so we reverse the order of
4517  // the true and false operands here. This will allow the setcc to be
4518  // matched to a single CSINC instruction.
4519  return DAG.getNode(AArch64ISD::CSEL, dl, VT, FVal, TVal, CC1Val, Cmp);
4520  } else {
4521  // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't
4522  // totally clean. Some of them require two CSELs to implement. As is in
4523  // this case, we emit the first CSEL and then emit a second using the output
4524  // of the first as the RHS. We're effectively OR'ing the two CC's together.
4525 
4526  // FIXME: It would be nice if we could match the two CSELs to two CSINCs.
4527  SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32);
4528  SDValue CS1 =
4529  DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, FVal, CC1Val, Cmp);
4530 
4531  SDValue CC2Val = DAG.getConstant(CC2, dl, MVT::i32);
4532  return DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, CS1, CC2Val, Cmp);
4533  }
4534 }
4535 
4536 SDValue AArch64TargetLowering::LowerSELECT_CC(ISD::CondCode CC, SDValue LHS,
4537  SDValue RHS, SDValue TVal,
4538  SDValue FVal, const SDLoc &dl,
4539  SelectionDAG &DAG) const {
4540  // Handle f128 first, because it will result in a comparison of some RTLIB
4541  // call result against zero.
4542  if (LHS.getValueType() == MVT::f128) {
4543  softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl);
4544 
4545  // If softenSetCCOperands returned a scalar, we need to compare the result
4546  // against zero to select between true and false values.
4547  if (!RHS.getNode()) {
4548  RHS = DAG.getConstant(0, dl, LHS.getValueType());
4549  CC = ISD::SETNE;
4550  }
4551  }
4552 
4553  // Also handle f16, for which we need to do a f32 comparison.
4554  if (LHS.getValueType() == MVT::f16 && !Subtarget->hasFullFP16()) {
4555  LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, LHS);
4556  RHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, RHS);
4557  }
4558 
4559  // Next, handle integers.
4560  if (LHS.getValueType().isInteger()) {
4561  assert((LHS.getValueType() == RHS.getValueType()) &&
4562  (LHS.getValueType() == MVT::i32 || LHS.getValueType() == MVT::i64));
4563 
4564  unsigned Opcode = AArch64ISD::CSEL;
4565 
4566  // If both the TVal and the FVal are constants, see if we can swap them in
4567  // order to for a CSINV or CSINC out of them.
4568  ConstantSDNode *CFVal = dyn_cast<ConstantSDNode>(FVal);
4569  ConstantSDNode *CTVal = dyn_cast<ConstantSDNode>(TVal);
4570 
4571  if (CTVal && CFVal && CTVal->isAllOnesValue() && CFVal->isNullValue()) {
4572  std::swap(TVal, FVal);
4573  std::swap(CTVal, CFVal);
4574  CC = ISD::getSetCCInverse(CC, true);
4575  } else if (CTVal && CFVal && CTVal->isOne() && CFVal->isNullValue()) {
4576  std::swap(TVal, FVal);
4577  std::swap(CTVal, CFVal);
4578  CC = ISD::getSetCCInverse(CC, true);
4579  } else if (TVal.getOpcode() == ISD::XOR) {
4580  // If TVal is a NOT we want to swap TVal and FVal so that we can match
4581  // with a CSINV rather than a CSEL.
4582  if (isAllOnesConstant(TVal.getOperand(1))) {
4583  std::swap(TVal, FVal);
4584  std::swap(CTVal, CFVal);
4585  CC = ISD::getSetCCInverse(CC, true);
4586  }
4587  } else if (TVal.getOpcode() == ISD::SUB) {
4588  // If TVal is a negation (SUB from 0) we want to swap TVal and FVal so
4589  // that we can match with a CSNEG rather than a CSEL.
4590  if (isNullConstant(TVal.getOperand(0))) {
4591  std::swap(TVal, FVal);
4592  std::swap(CTVal, CFVal);
4593  CC = ISD::getSetCCInverse(CC, true);
4594  }
4595  } else if (CTVal && CFVal) {
4596  const int64_t TrueVal = CTVal->getSExtValue();
4597  const int64_t FalseVal = CFVal->getSExtValue();
4598  bool Swap = false;
4599 
4600  // If both TVal and FVal are constants, see if FVal is the
4601  // inverse/negation/increment of TVal and generate a CSINV/CSNEG/CSINC
4602  // instead of a CSEL in that case.
4603  if (TrueVal == ~FalseVal) {
4604  Opcode = AArch64ISD::CSINV;
4605  } else if (TrueVal == -FalseVal) {
4606  Opcode = AArch64ISD::CSNEG;
4607  } else if (TVal.getValueType() == MVT::i32) {
4608  // If our operands are only 32-bit wide, make sure we use 32-bit
4609  // arithmetic for the check whether we can use CSINC. This ensures that
4610  // the addition in the check will wrap around properly in case there is
4611  // an overflow (which would not be the case if we do the check with
4612  // 64-bit arithmetic).
4613  const uint32_t TrueVal32 = CTVal->getZExtValue();
4614  const uint32_t FalseVal32 = CFVal->getZExtValue();
4615 
4616  if ((TrueVal32 == FalseVal32 + 1) || (TrueVal32 + 1 == FalseVal32)) {
4617  Opcode = AArch64ISD::CSINC;
4618 
4619  if (TrueVal32 > FalseVal32) {
4620  Swap = true;
4621  }
4622  }
4623  // 64-bit check whether we can use CSINC.
4624  } else if ((TrueVal == FalseVal + 1) || (TrueVal + 1 == FalseVal)) {
4625  Opcode = AArch64ISD::CSINC;
4626 
4627  if (TrueVal > FalseVal) {
4628  Swap = true;
4629  }
4630  }
4631 
4632  // Swap TVal and FVal if necessary.
4633  if (Swap) {
4634  std::swap(TVal, FVal);
4635  std::swap(CTVal, CFVal);
4636  CC = ISD::getSetCCInverse(CC, true);
4637  }
4638 
4639  if (Opcode != AArch64ISD::CSEL) {
4640  // Drop FVal since we can get its value by simply inverting/negating
4641  // TVal.
4642  FVal = TVal;
4643  }
4644  }
4645 
4646  // Avoid materializing a constant when possible by reusing a known value in
4647  // a register. However, don't perform this optimization if the known value
4648  // is one, zero or negative one in the case of a CSEL. We can always
4649  // materialize these values using CSINC, CSEL and CSINV with wzr/xzr as the
4650  // FVal, respectively.
4651  ConstantSDNode *RHSVal = dyn_cast<ConstantSDNode>(RHS);
4652  if (Opcode == AArch64ISD::CSEL && RHSVal && !RHSVal->isOne() &&
4653  !RHSVal->isNullValue() && !RHSVal->isAllOnesValue()) {
4655  // Transform "a == C ? C : x" to "a == C ? a : x" and "a != C ? x : C" to
4656  // "a != C ? x : a" to avoid materializing C.
4657  if (CTVal && CTVal == RHSVal && AArch64CC == AArch64CC::EQ)
4658  TVal = LHS;
4659  else if (CFVal && CFVal == RHSVal && AArch64CC == AArch64CC::NE)
4660  FVal = LHS;
4661  } else if (Opcode == AArch64ISD::CSNEG && RHSVal && RHSVal->isOne()) {
4662  assert (CTVal && CFVal && "Expected constant operands for CSNEG.");
4663  // Use a CSINV to transform "a == C ? 1 : -1" to "a == C ? a : -1" to
4664  // avoid materializing C.
4666  if (CTVal == RHSVal && AArch64CC == AArch64CC::EQ) {
4667  Opcode = AArch64ISD::CSINV;
4668  TVal = LHS;
4669  FVal = DAG.getConstant(0, dl, FVal.getValueType());
4670  }
4671  }
4672 
4673  SDValue CCVal;
4674  SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl);
4675  EVT VT = TVal.getValueType();
4676  return DAG.getNode(Opcode, dl, VT, TVal, FVal, CCVal, Cmp);
4677  }
4678 
4679  // Now we know we're dealing with FP values.
4680  assert(LHS.getValueType() == MVT::f16 || LHS.getValueType() == MVT::f32 ||
4681  LHS.getValueType() == MVT::f64);
4682  assert(LHS.getValueType() == RHS.getValueType());
4683  EVT VT = TVal.getValueType();
4684  SDValue Cmp = emitComparison(LHS, RHS, CC, dl, DAG);
4685 
4686  // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't totally
4687  // clean. Some of them require two CSELs to implement.
4688  AArch64CC::CondCode CC1, CC2;
4689  changeFPCCToAArch64CC(CC, CC1, CC2);
4690 
4691  if (DAG.getTarget().Options.UnsafeFPMath) {
4692  // Transform "a == 0.0 ? 0.0 : x" to "a == 0.0 ? a : x" and
4693  // "a != 0.0 ? x : 0.0" to "a != 0.0 ? x : a" to avoid materializing 0.0.
4694  ConstantFPSDNode *RHSVal = dyn_cast<ConstantFPSDNode>(RHS);
4695  if (RHSVal && RHSVal->isZero()) {
4696  ConstantFPSDNode *CFVal = dyn_cast<ConstantFPSDNode>(FVal);
4697  ConstantFPSDNode *CTVal = dyn_cast<ConstantFPSDNode>(TVal);
4698 
4699  if ((CC == ISD::SETEQ || CC == ISD::SETOEQ || CC == ISD::SETUEQ) &&
4700  CTVal && CTVal->isZero() && TVal.getValueType() == LHS.getValueType())
4701  TVal = LHS;
4702  else if ((CC == ISD::SETNE || CC == ISD::SETONE || CC == ISD::SETUNE) &&
4703  CFVal && CFVal->isZero() &&
4704  FVal.getValueType() == LHS.getValueType())
4705  FVal = LHS;
4706  }
4707  }
4708 
4709  // Emit first, and possibly only, CSEL.
4710  SDValue CC1Val = DAG.getConstant(CC1, dl, MVT::i32);
4711  SDValue CS1 = DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, FVal, CC1Val, Cmp);
4712 
4713  // If we need a second CSEL, emit it, using the output of the first as the
4714  // RHS. We're effectively OR'ing the two CC's together.
4715  if (CC2 != AArch64CC::AL) {
4716  SDValue CC2Val = DAG.getConstant(CC2, dl, MVT::i32);
4717  return DAG.getNode(AArch64ISD::CSEL, dl, VT, TVal, CS1, CC2Val, Cmp);
4718  }
4719 
4720  // Otherwise, return the output of the first CSEL.
4721  return CS1;
4722 }
4723 
4724 SDValue AArch64TargetLowering::LowerSELECT_CC(SDValue Op,
4725  SelectionDAG &DAG) const {
4726  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4727  SDValue LHS = Op.getOperand(0);
4728  SDValue RHS = Op.getOperand(1);
4729  SDValue TVal = Op.getOperand(2);
4730  SDValue FVal = Op.getOperand(3);
4731  SDLoc DL(Op);
4732  return LowerSELECT_CC(CC, LHS, RHS, TVal, FVal, DL, DAG);
4733 }
4734 
4735 SDValue AArch64TargetLowering::LowerSELECT(SDValue Op,
4736  SelectionDAG &DAG) const {
4737  SDValue CCVal = Op->getOperand(0);
4738  SDValue TVal = Op->getOperand(1);
4739  SDValue FVal = Op->getOperand(2);
4740  SDLoc DL(Op);
4741 
4742  // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a select
4743  // instruction.
4744  if (isOverflowIntrOpRes(CCVal)) {
4745  // Only lower legal XALUO ops.
4746  if (!DAG.getTargetLoweringInfo().isTypeLegal(CCVal->getValueType(0)))
4747  return SDValue();
4748 
4749  AArch64CC::CondCode OFCC;
4750  SDValue Value, Overflow;
4751  std::tie(Value, Overflow) = getAArch64XALUOOp(OFCC, CCVal.getValue(0), DAG);
4752  SDValue CCVal = DAG.getConstant(OFCC, DL, MVT::i32);
4753 
4754  return DAG.getNode(AArch64ISD::CSEL, DL, Op.getValueType(), TVal, FVal,
4755  CCVal, Overflow);
4756  }
4757 
4758  // Lower it the same way as we would lower a SELECT_CC node.
4759  ISD::CondCode CC;
4760  SDValue LHS, RHS;
4761  if (CCVal.getOpcode() == ISD::SETCC) {
4762  LHS = CCVal.getOperand(0);
4763  RHS = CCVal.getOperand(1);
4764  CC = cast<CondCodeSDNode>(CCVal->getOperand(2))->get();
4765  } else {
4766  LHS = CCVal;
4767  RHS = DAG.getConstant(0, DL, CCVal.getValueType());
4768  CC = ISD::SETNE;
4769  }
4770  return LowerSELECT_CC(CC, LHS, RHS, TVal, FVal, DL, DAG);
4771 }
4772 
4773 SDValue AArch64TargetLowering::LowerJumpTable(SDValue Op,
4774  SelectionDAG &DAG) const {
4775  // Jump table entries as PC relative offsets. No additional tweaking
4776  // is necessary here. Just get the address of the jump table.
4777  JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4778 
4780  !Subtarget->isTargetMachO()) {
4781  return getAddrLarge(JT, DAG);
4782  }
4783  return getAddr(JT, DAG);
4784 }
4785 
4786 SDValue AArch64TargetLowering::LowerConstantPool(SDValue Op,
4787  SelectionDAG &DAG) const {
4788  ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4789 
4791  // Use the GOT for the large code model on iOS.
4792  if (Subtarget->isTargetMachO()) {
4793  return getGOT(CP, DAG);
4794  }
4795  return getAddrLarge(CP, DAG);
4796  } else {
4797  return getAddr(CP, DAG);
4798  }
4799 }
4800 
4801 SDValue AArch64TargetLowering::LowerBlockAddress(SDValue Op,
4802  SelectionDAG &DAG) const {
4803  BlockAddressSDNode *BA = cast<BlockAddressSDNode>(Op);
4805  !Subtarget->isTargetMachO()) {
4806  return getAddrLarge(BA, DAG);
4807  } else {
4808  return getAddr(BA, DAG);
4809  }
4810 }
4811 
4812 SDValue AArch64TargetLowering::LowerDarwin_VASTART(SDValue Op,
4813  SelectionDAG &DAG) const {
4814  AArch64FunctionInfo *FuncInfo =
4816 
4817  SDLoc DL(Op);
4818  SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsStackIndex(),
4819  getPointerTy(DAG.getDataLayout()));
4820  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4821  return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
4822  MachinePointerInfo(SV));
4823 }
4824 
4825 SDValue AArch64TargetLowering::LowerWin64_VASTART(SDValue Op,
4826  SelectionDAG &DAG) const {
4827  AArch64FunctionInfo *FuncInfo =
4829 
4830  SDLoc DL(Op);
4831  SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsGPRSize() > 0
4832  ? FuncInfo->getVarArgsGPRIndex()
4833  : FuncInfo->getVarArgsStackIndex(),
4834  getPointerTy(DAG.getDataLayout()));
4835  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4836  return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
4837  MachinePointerInfo(SV));
4838 }
4839 
4840 SDValue AArch64TargetLowering::LowerAAPCS_VASTART(SDValue Op,
4841  SelectionDAG &DAG) const {
4842  // The layout of the va_list struct is specified in the AArch64 Procedure Call
4843  // Standard, section B.3.
4844  MachineFunction &MF = DAG.getMachineFunction();
4846  auto PtrVT = getPointerTy(DAG.getDataLayout());
4847  SDLoc DL(Op);
4848 
4849  SDValue Chain = Op.getOperand(0);
4850  SDValue VAList = Op.getOperand(1);
4851  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4852  SmallVector<SDValue, 4> MemOps;
4853 
4854  // void *__stack at offset 0
4855  SDValue Stack = DAG.getFrameIndex(FuncInfo->getVarArgsStackIndex(), PtrVT);
4856  MemOps.push_back(DAG.getStore(Chain, DL, Stack, VAList,
4857  MachinePointerInfo(SV), /* Alignment = */ 8));
4858 
4859  // void *__gr_top at offset 8
4860  int GPRSize = FuncInfo->getVarArgsGPRSize();
4861  if (GPRSize > 0) {
4862  SDValue GRTop, GRTopAddr;
4863 
4864  GRTopAddr =
4865  DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getConstant(8, DL, PtrVT));
4866 
4867  GRTop = DAG.getFrameIndex(FuncInfo->getVarArgsGPRIndex(), PtrVT);
4868  GRTop = DAG.getNode(ISD::ADD, DL, PtrVT, GRTop,
4869  DAG.getConstant(GPRSize, DL, PtrVT));
4870 
4871  MemOps.push_back(DAG.getStore(Chain, DL, GRTop, GRTopAddr,
4872  MachinePointerInfo(SV, 8),
4873  /* Alignment = */ 8));
4874  }
4875 
4876  // void *__vr_top at offset 16
4877  int FPRSize = FuncInfo->getVarArgsFPRSize();
4878  if (FPRSize > 0) {
4879  SDValue VRTop, VRTopAddr;
4880  VRTopAddr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
4881  DAG.getConstant(16, DL, PtrVT));
4882 
4883  VRTop = DAG.getFrameIndex(FuncInfo->getVarArgsFPRIndex(), PtrVT);
4884  VRTop = DAG.getNode(ISD::ADD, DL, PtrVT, VRTop,
4885  DAG.getConstant(FPRSize, DL, PtrVT));
4886 
4887  MemOps.push_back(DAG.getStore(Chain, DL, VRTop, VRTopAddr,
4888  MachinePointerInfo(SV, 16),
4889  /* Alignment = */ 8));
4890  }
4891 
4892  // int __gr_offs at offset 24
4893  SDValue GROffsAddr =
4894  DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getConstant(24, DL, PtrVT));
4895  MemOps.push_back(DAG.getStore(
4896  Chain, DL, DAG.getConstant(-GPRSize, DL, MVT::i32), GROffsAddr,
4897  MachinePointerInfo(SV, 24), /* Alignment = */ 4));
4898 
4899  // int __vr_offs at offset 28
4900  SDValue VROffsAddr =
4901  DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getConstant(28, DL, PtrVT));
4902  MemOps.push_back(DAG.getStore(
4903  Chain, DL, DAG.getConstant(-FPRSize, DL, MVT::i32), VROffsAddr,
4904  MachinePointerInfo(SV, 28), /* Alignment = */ 4));
4905 
4906  return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
4907 }
4908 
4909 SDValue AArch64TargetLowering::LowerVASTART(SDValue Op,
4910  SelectionDAG &DAG) const {
4911  MachineFunction &MF = DAG.getMachineFunction();
4912 
4913  if (Subtarget->isCallingConvWin64(MF.getFunction().getCallingConv()))
4914  return LowerWin64_VASTART(Op, DAG);
4915  else if (Subtarget->isTargetDarwin())
4916  return LowerDarwin_VASTART(Op, DAG);
4917  else
4918  return LowerAAPCS_VASTART(Op, DAG);
4919 }
4920 
4921 SDValue AArch64TargetLowering::LowerVACOPY(SDValue Op,
4922  SelectionDAG &DAG) const {
4923  // AAPCS has three pointers and two ints (= 32 bytes), Darwin has single
4924  // pointer.
4925  SDLoc DL(Op);
4926  unsigned VaListSize =
4927  Subtarget->isTargetDarwin() || Subtarget->isTargetWindows() ? 8 : 32;
4928  const Value *DestSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4929  const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
4930 
4931  return DAG.getMemcpy(Op.getOperand(0), DL, Op.getOperand(1),
4932  Op.getOperand(2),
4933  DAG.getConstant(VaListSize, DL, MVT::i32),
4934  8, false, false, false, MachinePointerInfo(DestSV),
4935  MachinePointerInfo(SrcSV));
4936 }
4937 
4938 SDValue AArch64TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
4939  assert(Subtarget->isTargetDarwin() &&
4940  "automatic va_arg instruction only works on Darwin");
4941 
4942  const Value *V = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4943  EVT VT = Op.getValueType();
4944  SDLoc DL(Op);
4945  SDValue Chain = Op.getOperand(0);
4946  SDValue Addr = Op.getOperand(1);
4947  unsigned Align = Op.getConstantOperandVal(3);
4948  auto PtrVT = getPointerTy(DAG.getDataLayout());
4949 
4950  SDValue VAList = DAG.getLoad(PtrVT, DL, Chain, Addr, MachinePointerInfo(V));
4951  Chain = VAList.getValue(1);
4952 
4953  if (Align > 8) {
4954  assert(((Align & (Align - 1)) == 0) && "Expected Align to be a power of 2");
4955  VAList = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
4956  DAG.getConstant(Align - 1, DL, PtrVT));
4957  VAList = DAG.getNode(ISD::AND, DL, PtrVT, VAList,
4958  DAG.getConstant(-(int64_t)Align, DL, PtrVT));
4959  }
4960 
4961  Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
4962  uint64_t ArgSize = DAG.getDataLayout().getTypeAllocSize(ArgTy);
4963 
4964  // Scalar integer and FP values smaller than 64 bits are implicitly extended
4965  // up to 64 bits. At the very least, we have to increase the striding of the
4966  // vaargs list to match this, and for FP values we need to introduce
4967  // FP_ROUND nodes as well.
4968  if (VT.isInteger() && !VT.isVector())
4969  ArgSize = 8;
4970  bool NeedFPTrunc = false;
4971  if (VT.isFloatingPoint() && !VT.isVector() && VT != MVT::f64) {
4972  ArgSize = 8;
4973  NeedFPTrunc = true;
4974  }
4975 
4976  // Increment the pointer, VAList, to the next vaarg
4977  SDValue VANext = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
4978  DAG.getConstant(ArgSize, DL, PtrVT));
4979  // Store the incremented VAList to the legalized pointer
4980  SDValue APStore =
4981  DAG.getStore(Chain, DL, VANext, Addr, MachinePointerInfo(V));
4982 
4983  // Load the actual argument out of the pointer VAList
4984  if (NeedFPTrunc) {
4985  // Load the value as an f64.
4986  SDValue WideFP =
4987  DAG.getLoad(MVT::f64, DL, APStore, VAList, MachinePointerInfo());
4988  // Round the value down to an f32.
4989  SDValue NarrowFP = DAG.getNode(ISD::FP_ROUND, DL, VT, WideFP.getValue(0),
4990  DAG.getIntPtrConstant(1, DL));
4991  SDValue Ops[] = { NarrowFP, WideFP.getValue(1) };
4992  // Merge the rounded value with the chain output of the load.
4993  return DAG.getMergeValues(Ops, DL);
4994  }
4995 
4996  return DAG.getLoad(VT, DL, APStore, VAList, MachinePointerInfo());
4997 }
4998 
4999 SDValue AArch64TargetLowering::LowerFRAMEADDR(SDValue Op,
5000  SelectionDAG &DAG) const {
5002  MFI.setFrameAddressIsTaken(true);
5003 
5004  EVT VT = Op.getValueType();
5005  SDLoc DL(Op);
5006  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5007  SDValue FrameAddr =
5008  DAG.getCopyFromReg(DAG.getEntryNode(), DL, AArch64::FP, VT);
5009  while (Depth--)
5010  FrameAddr = DAG.getLoad(VT, DL, DAG.getEntryNode(), FrameAddr,
5011  MachinePointerInfo());
5012  return FrameAddr;
5013 }
5014 
5015 // FIXME? Maybe this could be a TableGen attribute on some registers and
5016 // this table could be generated automatically from RegInfo.
5017 unsigned AArch64TargetLowering::getRegisterByName(const char* RegName, EVT VT,
5018  SelectionDAG &DAG) const {
5019  unsigned Reg = StringSwitch<unsigned>(RegName)
5020  .Case("sp", AArch64::SP)
5021  .Case("x18", AArch64::X18)
5022  .Case("w18", AArch64::W18)
5023  .Case("x20", AArch64::X20)
5024  .Case("w20", AArch64::W20)
5025  .Default(0);
5026  if (((Reg == AArch64::X18 || Reg == AArch64::W18) &&
5027  !Subtarget->isX18Reserved()) ||
5028  ((Reg == AArch64::X20 || Reg == AArch64::W20) &&
5029  !Subtarget->isX20Reserved()))
5030  Reg = 0;
5031  if (Reg)
5032  return Reg;
5033  report_fatal_error(Twine("Invalid register name \""
5034  + StringRef(RegName) + "\"."));
5035 }
5036 
5037 SDValue AArch64TargetLowering::LowerRETURNADDR(SDValue Op,
5038  SelectionDAG &DAG) const {
5039  MachineFunction &MF = DAG.getMachineFunction();
5040  MachineFrameInfo &MFI = MF.getFrameInfo();
5041  MFI.setReturnAddressIsTaken(true);
5042 
5043  EVT VT = Op.getValueType();
5044  SDLoc DL(Op);
5045  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5046  if (Depth) {
5047  SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
5048  SDValue Offset = DAG.getConstant(8, DL, getPointerTy(DAG.getDataLayout()));
5049  return DAG.getLoad(VT, DL, DAG.getEntryNode(),
5050  DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
5051  MachinePointerInfo());
5052  }
5053 
5054  // Return LR, which contains the return address. Mark it an implicit live-in.
5055  unsigned Reg = MF.addLiveIn(AArch64::LR, &AArch64::GPR64RegClass);
5056  return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
5057 }
5058 
5059 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two
5060 /// i64 values and take a 2 x i64 value to shift plus a shift amount.
5061 SDValue AArch64TargetLowering::LowerShiftRightParts(SDValue Op,
5062  SelectionDAG &DAG) const {
5063  assert(Op.getNumOperands() == 3 && "Not a double-shift!");
5064  EVT VT = Op.getValueType();
5065  unsigned VTBits = VT.getSizeInBits();
5066  SDLoc dl(Op);
5067  SDValue ShOpLo = Op.getOperand(0);
5068  SDValue ShOpHi = Op.getOperand(1);
5069  SDValue ShAmt = Op.getOperand(2);
5070  unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
5071 
5073 
5074  SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64,
5075  DAG.getConstant(VTBits, dl, MVT::i64), ShAmt);
5076  SDValue HiBitsForLo = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
5077 
5078  // Unfortunately, if ShAmt == 0, we just calculated "(SHL ShOpHi, 64)" which
5079  // is "undef". We wanted 0, so CSEL it directly.
5080  SDValue Cmp = emitComparison(ShAmt, DAG.getConstant(0, dl, MVT::i64),
5081  ISD::SETEQ, dl, DAG);
5082  SDValue CCVal = DAG.getConstant(AArch64CC::EQ, dl, MVT::i32);
5083  HiBitsForLo =
5084  DAG.getNode(AArch64ISD::CSEL, dl, VT, DAG.getConstant(0, dl, MVT::i64),
5085  HiBitsForLo, CCVal, Cmp);
5086 
5087  SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64, ShAmt,
5088  DAG.getConstant(VTBits, dl, MVT::i64));
5089 
5090  SDValue LoBitsForLo = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
5091  SDValue LoForNormalShift =
5092  DAG.getNode(ISD::OR, dl, VT, LoBitsForLo, HiBitsForLo);
5093 
5094  Cmp = emitComparison(ExtraShAmt, DAG.getConstant(0, dl, MVT::i64), ISD::SETGE,
5095  dl, DAG);
5096  CCVal = DAG.getConstant(AArch64CC::GE, dl, MVT::i32);
5097  SDValue LoForBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
5098  SDValue Lo = DAG.getNode(AArch64ISD::CSEL, dl, VT, LoForBigShift,
5099  LoForNormalShift, CCVal, Cmp);
5100 
5101  // AArch64 shifts larger than the register width are wrapped rather than
5102  // clamped, so we can't just emit "hi >> x".
5103  SDValue HiForNormalShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
5104  SDValue HiForBigShift =
5105  Opc == ISD::SRA
5106  ? DAG.getNode(Opc, dl, VT, ShOpHi,
5107  DAG.getConstant(VTBits - 1, dl, MVT::i64))
5108  : DAG.getConstant(0, dl, VT);
5109  SDValue Hi = DAG.getNode(AArch64ISD::CSEL, dl, VT, HiForBigShift,
5110  HiForNormalShift, CCVal, Cmp);
5111 
5112  SDValue Ops[2] = { Lo, Hi };
5113  return DAG.getMergeValues(Ops, dl);
5114 }
5115 
5116 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
5117 /// i64 values and take a 2 x i64 value to shift plus a shift amount.
5118 SDValue AArch64TargetLowering::LowerShiftLeftParts(SDValue Op,
5119  SelectionDAG &DAG) const {
5120  assert(Op.getNumOperands() == 3 && "Not a double-shift!");
5121  EVT VT = Op.getValueType();
5122  unsigned VTBits = VT.getSizeInBits();
5123  SDLoc dl(Op);
5124  SDValue ShOpLo = Op.getOperand(0);
5125  SDValue ShOpHi = Op.getOperand(1);
5126  SDValue ShAmt = Op.getOperand(2);
5127 
5128  assert(Op.getOpcode() == ISD::SHL_PARTS);
5129  SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64,
5130  DAG.getConstant(VTBits, dl, MVT::i64), ShAmt);
5131  SDValue LoBitsForHi = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
5132 
5133  // Unfortunately, if ShAmt == 0, we just calculated "(SRL ShOpLo, 64)" which
5134  // is "undef". We wanted 0, so CSEL it directly.
5135  SDValue Cmp = emitComparison(ShAmt, DAG.getConstant(0, dl, MVT::i64),
5136  ISD::SETEQ, dl, DAG);
5137  SDValue CCVal = DAG.getConstant(AArch64CC::EQ, dl, MVT::i32);
5138  LoBitsForHi =
5139  DAG.getNode(AArch64ISD::CSEL, dl, VT, DAG.getConstant(0, dl, MVT::i64),
5140  LoBitsForHi, CCVal, Cmp);
5141 
5142  SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i64, ShAmt,
5143  DAG.getConstant(VTBits, dl, MVT::i64));
5144  SDValue HiBitsForHi = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
5145  SDValue HiForNormalShift =
5146  DAG.getNode(ISD::OR, dl, VT, LoBitsForHi, HiBitsForHi);
5147 
5148  SDValue HiForBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
5149 
5150  Cmp = emitComparison(ExtraShAmt, DAG.getConstant(0, dl, MVT::i64), ISD::SETGE,
5151  dl, DAG);
5152  CCVal = DAG.getConstant(AArch64CC::GE, dl, MVT::i32);
5153  SDValue Hi = DAG.getNode(AArch64ISD::CSEL, dl, VT, HiForBigShift,
5154  HiForNormalShift, CCVal, Cmp);
5155 
5156  // AArch64 shifts of larger than register sizes are wrapped rather than
5157  // clamped, so we can't just emit "lo << a" if a is too big.
5158  SDValue LoForBigShift = DAG.getConstant(0, dl, VT);
5159  SDValue LoForNormalShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
5160  SDValue Lo = DAG.getNode(AArch64ISD::CSEL, dl, VT, LoForBigShift,
5161  LoForNormalShift, CCVal, Cmp);
5162 
5163  SDValue Ops[2] = { Lo, Hi };
5164  return DAG.getMergeValues(Ops, dl);
5165 }
5166 
5168  const GlobalAddressSDNode *GA) const {
5169  // Offsets are folded in the DAG combine rather than here so that we can
5170  // intelligently choose an offset based on the uses.
5171  return false;
5172 }
5173 
5175  // We can materialize #0.0 as fmov $Rd, XZR for 64-bit and 32-bit cases.
5176  // FIXME: We should be able to handle f128 as well with a clever lowering.
5177  if (Imm.isPosZero() && (VT == MVT::f64 || VT == MVT::f32 ||
5178  (VT == MVT::f16 && Subtarget->hasFullFP16()))) {
5179  LLVM_DEBUG(
5180  dbgs() << "Legal fp imm: materialize 0 using the zero register\n");
5181  return true;
5182  }
5183 
5184  StringRef FPType;
5185  bool IsLegal = false;
5186  SmallString<128> ImmStrVal;
5187  Imm.toString(ImmStrVal);
5188 
5189  if (VT == MVT::f64) {
5190  FPType = "f64";
5191  IsLegal = AArch64_AM::getFP64Imm(Imm) != -1;
5192  } else if (VT == MVT::f32) {
5193  FPType = "f32";
5194  IsLegal = AArch64_AM::getFP32Imm(Imm) != -1;
5195  } else if (VT == MVT::f16 && Subtarget->hasFullFP16()) {
5196  FPType = "f16";
5197  IsLegal = AArch64_AM::getFP16Imm(Imm) != -1;
5198  }
5199 
5200  if (IsLegal) {
5201  LLVM_DEBUG(dbgs() << "Legal " << FPType << " imm value: " << ImmStrVal
5202  << "\n");
5203  return true;
5204  }
5205 
5206  if (!FPType.empty())
5207  LLVM_DEBUG(dbgs() << "Illegal " << FPType << " imm value: " << ImmStrVal
5208  << "\n");
5209  else
5210  LLVM_DEBUG(dbgs() << "Illegal fp imm " << ImmStrVal
5211  << ": unsupported fp type\n");
5212 
5213  return false;
5214 }
5215 
5216 //===----------------------------------------------------------------------===//
5217 // AArch64 Optimization Hooks
5218 //===----------------------------------------------------------------------===//
5219 
5220 static SDValue getEstimate(const AArch64Subtarget *ST, unsigned Opcode,
5221  SDValue Operand, SelectionDAG &DAG,
5222  int &ExtraSteps) {
5223  EVT VT = Operand.getValueType();
5224  if (ST->hasNEON() &&
5225  (VT == MVT::f64 || VT == MVT::v1f64 || VT == MVT::v2f64 ||
5226  VT == MVT::f32 || VT == MVT::v1f32 ||
5227  VT == MVT::v2f32 || VT == MVT::v4f32)) {
5228  if (ExtraSteps == TargetLoweringBase::ReciprocalEstimate::Unspecified)
5229  // For the reciprocal estimates, convergence is quadratic, so the number
5230  // of digits is doubled after each iteration. In ARMv8, the accuracy of
5231  // the initial estimate is 2^-8. Thus the number of extra steps to refine
5232  // the result for float (23 mantissa bits) is 2 and for double (52
5233  // mantissa bits) is 3.
5234  ExtraSteps = VT.getScalarType() == MVT::f64 ? 3 : 2;
5235 
5236  return DAG.getNode(Opcode, SDLoc(Operand), VT, Operand);
5237  }
5238 
5239  return SDValue();
5240 }
5241 
5242 SDValue AArch64TargetLowering::getSqrtEstimate(SDValue Operand,
5243  SelectionDAG &DAG, int Enabled,
5244  int &ExtraSteps,
5245  bool &UseOneConst,
5246  bool Reciprocal) const {
5247  if (Enabled == ReciprocalEstimate::Enabled ||
5248  (Enabled == ReciprocalEstimate::Unspecified && Subtarget->useRSqrt()))
5249  if (SDValue Estimate = getEstimate(Subtarget, AArch64ISD::FRSQRTE, Operand,
5250  DAG, ExtraSteps)) {
5251  SDLoc DL(Operand);
5252  EVT VT = Operand.getValueType();
5253 
5254  SDNodeFlags Flags;
5255  Flags.setAllowReassociation(true);
5256 
5257  // Newton reciprocal square root iteration: E * 0.5 * (3 - X * E^2)
5258  // AArch64 reciprocal square root iteration instruction: 0.5 * (3 - M * N)
5259  for (int i = ExtraSteps; i > 0; --i) {
5260  SDValue Step = DAG.getNode(ISD::FMUL, DL, VT, Estimate, Estimate,
5261  Flags);
5262  Step = DAG.getNode(AArch64ISD::FRSQRTS, DL, VT, Operand, Step, Flags);
5263  Estimate = DAG.getNode(ISD::FMUL, DL, VT, Estimate, Step, Flags);
5264  }
5265  if (!Reciprocal) {
5266  EVT CCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
5267  VT);
5268  SDValue FPZero = DAG.getConstantFP(0.0, DL, VT);
5269  SDValue Eq = DAG.getSetCC(DL, CCVT, Operand, FPZero, ISD::SETEQ);
5270 
5271  Estimate = DAG.getNode(ISD::FMUL, DL, VT, Operand, Estimate, Flags);
5272  // Correct the result if the operand is 0.0.
5273  Estimate = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, DL,
5274  VT, Eq, Operand, Estimate);
5275  }
5276 
5277  ExtraSteps = 0;
5278  return Estimate;
5279  }
5280 
5281  return SDValue();
5282 }
5283 
5284 SDValue AArch64TargetLowering::getRecipEstimate(SDValue Operand,
5285  SelectionDAG &DAG, int Enabled,
5286  int &ExtraSteps) const {
5287  if (Enabled == ReciprocalEstimate::Enabled)
5288  if (SDValue Estimate = getEstimate(Subtarget, AArch64ISD::FRECPE, Operand,
5289  DAG, ExtraSteps)) {
5290  SDLoc DL(Operand);
5291  EVT VT = Operand.getValueType();
5292 
5293  SDNodeFlags Flags;
5294  Flags.setAllowReassociation(true);
5295 
5296  // Newton reciprocal iteration: E * (2 - X * E)
5297  // AArch64 reciprocal iteration instruction: (2 - M * N)
5298  for (int i = ExtraSteps; i > 0; --i) {
5299  SDValue Step = DAG.getNode(AArch64ISD::FRECPS, DL, VT, Operand,
5300  Estimate, Flags);
5301  Estimate = DAG.getNode(ISD::FMUL, DL, VT, Estimate, Step, Flags);
5302  }
5303 
5304  ExtraSteps = 0;
5305  return Estimate;
5306  }
5307 
5308  return SDValue();
5309 }
5310 
5311 //===----------------------------------------------------------------------===//
5312 // AArch64 Inline Assembly Support
5313 //===----------------------------------------------------------------------===//
5314 
5315 // Table of Constraints
5316 // TODO: This is the current set of constraints supported by ARM for the
5317 // compiler, not all of them may make sense.
5318 //
5319 // r - A general register
5320 // w - An FP/SIMD register of some size in the range v0-v31
5321 // x - An FP/SIMD register of some size in the range v0-v15
5322 // I - Constant that can be used with an ADD instruction
5323 // J - Constant that can be used with a SUB instruction
5324 // K - Constant that can be used with a 32-bit logical instruction
5325 // L - Constant that can be used with a 64-bit logical instruction
5326 // M - Constant that can be used as a 32-bit MOV immediate
5327 // N - Constant that can be used as a 64-bit MOV immediate
5328 // Q - A memory reference with base register and no offset
5329 // S - A symbolic address
5330 // Y - Floating point constant zero
5331 // Z - Integer constant zero
5332 //
5333 // Note that general register operands will be output using their 64-bit x
5334 // register name, whatever the size of the variable, unless the asm operand
5335 // is prefixed by the %w modifier. Floating-point and SIMD register operands
5336 // will be output with the v prefix unless prefixed by the %b, %h, %s, %d or
5337 // %q modifier.
5338 const char *AArch64TargetLowering::LowerXConstraint(EVT ConstraintVT) const {
5339  // At this point, we have to lower this constraint to something else, so we
5340  // lower it to an "r" or "w". However, by doing this we will force the result
5341  // to be in register, while the X constraint is much more permissive.
5342  //
5343  // Although we are correct (we are free to emit anything, without
5344  // constraints), we might break use cases that would expect us to be more
5345  // efficient and emit something else.
5346  if (!Subtarget->hasFPARMv8())
5347  return "r";
5348 
5349  if (ConstraintVT.isFloatingPoint())
5350  return "w";
5351 
5352  if (ConstraintVT.isVector() &&
5353  (ConstraintVT.getSizeInBits() == 64 ||
5354  ConstraintVT.getSizeInBits() == 128))
5355  return "w";
5356 
5357  return "r";
5358 }
5359 
5360 /// getConstraintType - Given a constraint letter, return the type of
5361 /// constraint it is for this target.
5363 AArch64TargetLowering::getConstraintType(StringRef Constraint) const {
5364  if (Constraint.size() == 1) {
5365  switch (Constraint[0]) {
5366  default:
5367  break;
5368  case 'z':
5369  return C_Other;
5370  case 'x':
5371  case 'w':
5372  return C_RegisterClass;
5373  // An address with a single base register. Due to the way we
5374  // currently handle addresses it is the same as 'r'.
5375  case 'Q':
5376  return C_Memory;
5377  case 'S': // A symbolic address
5378  return C_Other;
5379  }
5380  }
5381  return TargetLowering::getConstraintType(Constraint);
5382 }
5383 
5384 /// Examine constraint type and operand type and determine a weight value.
5385 /// This object must already have been set up with the operand type
5386 /// and the current alternative constraint selected.
5388 AArch64TargetLowering::getSingleConstraintMatchWeight(
5389  AsmOperandInfo &info, const char *constraint) const {
5390  ConstraintWeight weight = CW_Invalid;
5391  Value *CallOperandVal = info.CallOperandVal;
5392  // If we don't have a value, we can't do a match,
5393  // but allow it at the lowest weight.
5394  if (!CallOperandVal)
5395  return CW_Default;
5396  Type *type = CallOperandVal->getType();
5397  // Look at the constraint type.
5398  switch (*constraint) {
5399  default:
5400  weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
5401  break;
5402  case 'x':
5403  case 'w':
5404  if (type->isFloatingPointTy() || type->isVectorTy())
5405  weight = CW_Register;
5406  break;
5407  case 'z':
5408  weight = CW_Constant;
5409  break;
5410  }
5411  return weight;
5412 }
5413 
5414 std::pair<unsigned, const TargetRegisterClass *>
5415 AArch64TargetLowering::getRegForInlineAsmConstraint(
5416  const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
5417  if (Constraint.size() == 1) {
5418  switch (Constraint[0]) {
5419  case 'r':
5420  if (VT.getSizeInBits() == 64)
5421  return std::make_pair(0U, &AArch64::GPR64commonRegClass);
5422  return std::make_pair(0U, &AArch64::GPR32commonRegClass);
5423  case 'w':
5424  if (VT.getSizeInBits() == 16)
5425  return std::make_pair(0U, &AArch64::FPR16RegClass);
5426  if (VT.getSizeInBits() == 32)
5427  return std::make_pair(0U, &AArch64::FPR32RegClass);
5428  if (VT.getSizeInBits() == 64)
5429  return std::make_pair(0U, &AArch64::FPR64RegClass);
5430  if (VT.getSizeInBits() == 128)
5431  return std::make_pair(0U, &AArch64::FPR128RegClass);
5432  break;
5433  // The instructions that this constraint is designed for can
5434  // only take 128-bit registers so just use that regclass.
5435  case 'x':
5436  if (VT.getSizeInBits() == 128)
5437  return std::make_pair(0U, &AArch64::FPR128_loRegClass);
5438  break;
5439  }
5440  }
5441  if (StringRef("{cc}").equals_lower(Constraint))
5442  return std::make_pair(unsigned(AArch64::NZCV), &AArch64::CCRRegClass);
5443 
5444  // Use the default implementation in TargetLowering to convert the register
5445  // constraint into a member of a register class.
5446  std::pair<unsigned, const TargetRegisterClass *> Res;
5447  Res = TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
5448 
5449  // Not found as a standard register?
5450  if (!Res.second) {
5451  unsigned Size = Constraint.size();
5452  if ((Size == 4 || Size == 5) && Constraint[0] == '{' &&
5453  tolower(Constraint[1]) == 'v' && Constraint[Size - 1] == '}') {
5454  int RegNo;
5455  bool Failed = Constraint.slice(2, Size - 1).getAsInteger(10, RegNo);
5456  if (!Failed && RegNo >= 0 && RegNo <= 31) {
5457  // v0 - v31 are aliases of q0 - q31 or d0 - d31 depending on size.
5458  // By default we'll emit v0-v31 for this unless there's a modifier where
5459  // we'll emit the correct register as well.
5460  if (VT != MVT::Other && VT.getSizeInBits() == 64) {
5461  Res.first = AArch64::FPR64RegClass.getRegister(RegNo);
5462  Res.second = &AArch64::FPR64RegClass;
5463  } else {
5464  Res.first = AArch64::FPR128RegClass.getRegister(RegNo);
5465  Res.second = &AArch64::FPR128RegClass;
5466  }
5467  }
5468  }
5469  }
5470 
5471  return Res;
5472 }
5473 
5474 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5475 /// vector. If it is invalid, don't add anything to Ops.
5476 void AArch64TargetLowering::LowerAsmOperandForConstraint(
5477  SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
5478  SelectionDAG &DAG) const {
5479  SDValue Result;
5480 
5481  // Currently only support length 1 constraints.
5482  if (Constraint.length() != 1)
5483  return;
5484 
5485  char ConstraintLetter = Constraint[0];
5486  switch (ConstraintLetter) {
5487  default:
5488  break;
5489 
5490  // This set of constraints deal with valid constants for various instructions.
5491  // Validate and return a target constant for them if we can.
5492  case 'z': {
5493  // 'z' maps to xzr or wzr so it needs an input of 0.
5494  if (!isNullConstant(Op))
5495  return;
5496 
5497  if (Op.getValueType() == MVT::i64)
5498  Result = DAG.getRegister(AArch64::XZR, MVT::i64);
5499  else
5500  Result = DAG.getRegister(AArch64::WZR, MVT::i32);
5501  break;
5502  }
5503  case 'S': {
5504  // An absolute symbolic address or label reference.
5505  if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
5506  Result = DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(Op),
5507  GA->getValueType(0));
5508  } else if (const BlockAddressSDNode *BA =
5509  dyn_cast<BlockAddressSDNode>(Op)) {
5510  Result =
5511  DAG.getTargetBlockAddress(BA->getBlockAddress(), BA->getValueType(0));
5512  } else if (const ExternalSymbolSDNode *ES =
5513  dyn_cast<ExternalSymbolSDNode>(Op)) {
5514  Result =
5515  DAG.getTargetExternalSymbol(ES->getSymbol(), ES->getValueType(0));
5516  } else
5517  return;
5518  break;
5519  }
5520 
5521  case 'I':
5522  case 'J':
5523  case 'K':
5524  case 'L':
5525  case 'M':
5526  case 'N':
5528  if (!C)
5529  return;
5530 
5531  // Grab the value and do some validation.
5532  uint64_t CVal = C->getZExtValue();
5533  switch (ConstraintLetter) {
5534  // The I constraint applies only to simple ADD or SUB immediate operands:
5535  // i.e. 0 to 4095 with optional shift by 12
5536  // The J constraint applies only to ADD or SUB immediates that would be
5537  // valid when negated, i.e. if [an add pattern] were to be output as a SUB
5538  // instruction [or vice versa], in other words -1 to -4095 with optional
5539  // left shift by 12.
5540  case 'I':
5541  if (isUInt<12>(CVal) || isShiftedUInt<12, 12>(CVal))
5542  break;
5543  return;
5544  case 'J': {
5545  uint64_t NVal = -C->getSExtValue();
5546  if (isUInt<12>(NVal) || isShiftedUInt<12, 12>(NVal)) {
5547  CVal = C->getSExtValue();
5548  break;
5549  }
5550  return;
5551  }
5552  // The K and L constraints apply *only* to logical immediates, including
5553  // what used to be the MOVI alias for ORR (though the MOVI alias has now
5554  // been removed and MOV should be used). So these constraints have to
5555  // distinguish between bit patterns that are valid 32-bit or 64-bit
5556  // "bitmask immediates": for example 0xaaaaaaaa is a valid bimm32 (K), but
5557  // not a valid bimm64 (L) where 0xaaaaaaaaaaaaaaaa would be valid, and vice
5558  // versa.
5559  case 'K':
5560  if (AArch64_AM::isLogicalImmediate(CVal, 32))
5561  break;
5562  return;
5563  case 'L':
5564  if (AArch64_AM::isLogicalImmediate(CVal, 64))
5565  break;
5566  return;
5567  // The M and N constraints are a superset of K and L respectively, for use
5568  // with the MOV (immediate) alias. As well as the logical immediates they
5569  // also match 32 or 64-bit immediates that can be loaded either using a
5570  // *single* MOVZ or MOVN , such as 32-bit 0x12340000, 0x00001234, 0xffffedca
5571  // (M) or 64-bit 0x1234000000000000 (N) etc.
5572  // As a note some of this code is liberally stolen from the asm parser.
5573  case 'M': {
5574  if (!isUInt<32>(CVal))
5575  return;
5576  if (AArch64_AM::isLogicalImmediate(CVal, 32))
5577  break;
5578  if ((CVal & 0xFFFF) == CVal)
5579  break;
5580  if ((CVal & 0xFFFF0000ULL) == CVal)
5581  break;
5582  uint64_t NCVal = ~(uint32_t)CVal;
5583  if ((NCVal & 0xFFFFULL) == NCVal)
5584  break;
5585  if ((NCVal & 0xFFFF0000ULL) == NCVal)
5586  break;
5587  return;
5588  }
5589  case 'N': {
5590  if (AArch64_AM::isLogicalImmediate(CVal, 64))
5591  break;
5592  if ((CVal & 0xFFFFULL) == CVal)
5593  break;
5594  if ((CVal & 0xFFFF0000ULL) == CVal)
5595  break;
5596  if ((CVal & 0xFFFF00000000ULL) == CVal)
5597  break;
5598  if ((CVal & 0xFFFF000000000000ULL) == CVal)
5599  break;
5600  uint64_t NCVal = ~CVal;
5601  if ((NCVal & 0xFFFFULL) == NCVal)
5602  break;
5603  if ((NCVal & 0xFFFF0000ULL) == NCVal)
5604  break;
5605  if ((NCVal & 0xFFFF00000000ULL) == NCVal)
5606  break;
5607  if ((NCVal & 0xFFFF000000000000ULL) == NCVal)
5608  break;
5609  return;
5610  }
5611  default:
5612  return;
5613  }
5614 
5615  // All assembler immediates are 64-bit integers.
5616  Result = DAG.getTargetConstant(CVal, SDLoc(Op), MVT::i64);
5617  break;
5618  }
5619 
5620  if (Result.getNode()) {
5621  Ops.push_back(Result);
5622  return;
5623  }
5624 
5625  return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
5626 }
5627 
5628 //===----------------------------------------------------------------------===//
5629 // AArch64 Advanced SIMD Support
5630 //===----------------------------------------------------------------------===//
5631 
5632 /// WidenVector - Given a value in the V64 register class, produce the
5633 /// equivalent value in the V128 register class.
5634 static SDValue WidenVector(SDValue V64Reg, SelectionDAG &DAG) {
5635  EVT VT = V64Reg.getValueType();
5636  unsigned NarrowSize = VT.getVectorNumElements();
5637  MVT EltTy = VT.getVectorElementType().getSimpleVT();
5638  MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
5639  SDLoc DL(V64Reg);
5640 
5641  return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideTy, DAG.getUNDEF(WideTy),
5642  V64Reg, DAG.getConstant(0, DL, MVT::i32));
5643 }
5644 
5645 /// getExtFactor - Determine the adjustment factor for the position when
5646 /// generating an "extract from vector registers" instruction.
5647 static unsigned getExtFactor(SDValue &V) {
5648  EVT EltType = V.getValueType().getVectorElementType();
5649  return EltType.getSizeInBits() / 8;
5650 }
5651 
5652 /// NarrowVector - Given a value in the V128 register class, produce the
5653 /// equivalent value in the V64 register class.
5654 static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
5655  EVT VT = V128Reg.getValueType();
5656  unsigned WideSize = VT.getVectorNumElements();
5657  MVT EltTy = VT.getVectorElementType().getSimpleVT();
5658  MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
5659  SDLoc DL(V128Reg);
5660 
5661  return DAG.getTargetExtractSubreg(AArch64::dsub, DL, NarrowTy, V128Reg);
5662 }
5663 
5664 // Gather data to see if the operation can be modelled as a
5665 // shuffle in combination with VEXTs.
5667  SelectionDAG &DAG) const {
5668  assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
5669  LLVM_DEBUG(dbgs() << "AArch64TargetLowering::ReconstructShuffle\n");
5670  SDLoc dl(Op);
5671  EVT VT = Op.getValueType();
5672  unsigned NumElts = VT.getVectorNumElements();
5673 
5674  struct ShuffleSourceInfo {
5675  SDValue Vec;
5676  unsigned MinElt;
5677  unsigned MaxElt;
5678 
5679  // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5680  // be compatible with the shuffle we intend to construct. As a result
5681  // ShuffleVec will be some sliding window into the original Vec.
5682  SDValue ShuffleVec;
5683 
5684  // Code should guarantee that element i in Vec starts at element "WindowBase
5685  // + i * WindowScale in ShuffleVec".
5686  int WindowBase;
5687  int WindowScale;
5688 
5689  ShuffleSourceInfo(SDValue Vec)
5690  : Vec(Vec), MinElt(std::numeric_limits<unsigned>::max()), MaxElt(0),
5691  ShuffleVec(Vec), WindowBase(0), WindowScale(1) {}
5692 
5693  bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5694  };
5695 
5696  // First gather all vectors used as an immediate source for this BUILD_VECTOR
5697  // node.
5699  for (unsigned i = 0; i < NumElts; ++i) {
5700  SDValue V = Op.getOperand(i);
5701  if (V.isUndef())
5702  continue;
5703  else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5704  !isa<ConstantSDNode>(V.getOperand(1))) {
5705  LLVM_DEBUG(
5706  dbgs() << "Reshuffle failed: "
5707  "a shuffle can only come from building a vector from "
5708  "various elements of other vectors, provided their "
5709  "indices are constant\n");
5710  return SDValue();
5711  }
5712 
5713  // Add this element source to the list if it's not already there.
5714  SDValue SourceVec = V.getOperand(0);
5715  auto Source = find(Sources, SourceVec);
5716  if (Source == Sources.end())
5717  Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
5718 
5719  // Update the minimum and maximum lane number seen.
5720  unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5721  Source->MinElt = std::min(Source->MinElt, EltNo);
5722  Source->MaxElt = std::max(Source->MaxElt, EltNo);
5723  }
5724 
5725  if (Sources.size() > 2) {
5726  LLVM_DEBUG(
5727  dbgs() << "Reshuffle failed: currently only do something sane when at "
5728  "most two source vectors are involved\n");
5729  return SDValue();
5730  }
5731 
5732  // Find out the smallest element size among result and two sources, and use
5733  // it as element size to build the shuffle_vector.
5734  EVT SmallestEltTy = VT.getVectorElementType();
5735  for (auto &Source : Sources) {
5736  EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5737  if (SrcEltTy.bitsLT(SmallestEltTy)) {
5738  SmallestEltTy = SrcEltTy;
5739  }
5740  }
5741  unsigned ResMultiplier =
5742  VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits();
5743  NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5744  EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
5745 
5746  // If the source vector is too wide or too narrow, we may nevertheless be able
5747  // to construct a compatible shuffle either by concatenating it with UNDEF or
5748  // extracting a suitable range of elements.
5749  for (auto &Src : Sources) {
5750  EVT SrcVT = Src.ShuffleVec.getValueType();
5751 
5752  if (SrcVT.getSizeInBits() == VT.getSizeInBits())
5753  continue;
5754 
5755  // This stage of the search produces a source with the same element type as
5756  // the original, but with a total width matching the BUILD_VECTOR output.
5757  EVT EltVT = SrcVT.getVectorElementType();
5758  unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5759  EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5760 
5761  if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5762  assert(2 * SrcVT.getSizeInBits() == VT.getSizeInBits());
5763  // We can pad out the smaller vector for free, so if it's part of a
5764  // shuffle...
5765  Src.ShuffleVec =
5766  DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5767  DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5768  continue;
5769  }
5770 
5771  assert(SrcVT.getSizeInBits() == 2 * VT.getSizeInBits());
5772 
5773  if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
5774  LLVM_DEBUG(
5775  dbgs() << "Reshuffle failed: span too large for a VEXT to cope\n");
5776  return SDValue();
5777  }
5778 
5779  if (Src.MinElt >= NumSrcElts) {
5780  // The extraction can just take the second half
5781  Src.ShuffleVec =
5782  DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5783  DAG.getConstant(NumSrcElts, dl, MVT::i64));
5784  Src.WindowBase = -NumSrcElts;
5785  } else if (Src.MaxElt < NumSrcElts) {
5786  // The extraction can just take the first half
5787  Src.ShuffleVec =
5788  DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5789  DAG.getConstant(0, dl, MVT::i64));
5790  } else {
5791  // An actual VEXT is needed
5792  SDValue VEXTSrc1 =
5793  DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5794  DAG.getConstant(0, dl, MVT::i64));
5795  SDValue VEXTSrc2 =
5796  DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5797  DAG.getConstant(NumSrcElts, dl, MVT::i64));
5798  unsigned Imm = Src.MinElt * getExtFactor(VEXTSrc1);
5799 
5800  Src.ShuffleVec = DAG.getNode(AArch64ISD::EXT, dl, DestVT, VEXTSrc1,
5801  VEXTSrc2,
5802  DAG.getConstant(Imm, dl, MVT::i32));
5803  Src.WindowBase = -Src.MinElt;
5804  }
5805  }
5806 
5807  // Another possible incompatibility occurs from the vector element types. We
5808  // can fix this by bitcasting the source vectors to the same type we intend
5809  // for the shuffle.
5810  for (auto &Src : Sources) {
5811  EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5812  if (SrcEltTy == SmallestEltTy)
5813  continue;
5814  assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5815  Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5816  Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5817  Src.WindowBase *= Src.WindowScale;
5818  }
5819 
5820  // Final sanity check before we try to actually produce a shuffle.
5821  LLVM_DEBUG(for (auto Src
5822  : Sources)
5823  assert(Src.ShuffleVec.getValueType() == ShuffleVT););
5824 
5825  // The stars all align, our next step is to produce the mask for the shuffle.
5827  int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits();
5828  for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5829  SDValue Entry = Op.getOperand(i);
5830  if (Entry.isUndef())
5831  continue;
5832 
5833  auto Src = find(Sources, Entry.getOperand(0));
5834  int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5835 
5836  // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5837  // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5838  // segment.
5839  EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5840  int BitsDefined =
5841  std::min(OrigEltTy.getSizeInBits(), VT.getScalarSizeInBits());
5842  int LanesDefined = BitsDefined / BitsPerShuffleLane;
5843 
5844  // This source is expected to fill ResMultiplier lanes of the final shuffle,
5845  // starting at the appropriate offset.
5846  int *LaneMask = &Mask[i * ResMultiplier];
5847 
5848  int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5849  ExtractBase += NumElts * (Src - Sources.begin());
5850  for (int j = 0; j < LanesDefined; ++j)
5851  LaneMask[j] = ExtractBase + j;
5852  }
5853 
5854  // Final check before we try to produce nonsense...
5855  if (!isShuffleMaskLegal(Mask, ShuffleVT)) {
5856  LLVM_DEBUG(dbgs() << "Reshuffle failed: illegal shuffle mask\n");
5857  return SDValue();
5858  }
5859 
5860  SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5861  for (unsigned i = 0; i < Sources.size(); ++i)
5862  ShuffleOps[i] = Sources[i].ShuffleVec;
5863 
5864  SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5865  ShuffleOps[1], Mask);
5866  SDValue V = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
5867 
5868  LLVM_DEBUG(dbgs() << "Reshuffle, creating node: "; Shuffle.dump();
5869  dbgs() << "Reshuffle, creating node: "; V.dump(););
5870 
5871  return V;
5872 }
5873 
5874 // check if an EXT instruction can handle the shuffle mask when the
5875 // vector sources of the shuffle are the same.
5876 static bool isSingletonEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5877  unsigned NumElts = VT.getVectorNumElements();
5878 
5879  // Assume that the first shuffle index is not UNDEF. Fail if it is.
5880  if (M[0] < 0)
5881  return false;
5882 
5883  Imm = M[0];
5884 
5885  // If this is a VEXT shuffle, the immediate value is the index of the first
5886  // element. The other shuffle indices must be the successive elements after
5887  // the first one.
5888  unsigned ExpectedElt = Imm;
5889  for (unsigned i = 1; i < NumElts; ++i) {
5890  // Increment the expected index. If it wraps around, just follow it
5891  // back to index zero and keep going.
5892  ++ExpectedElt;
5893  if (ExpectedElt == NumElts)
5894  ExpectedElt = 0;
5895 
5896  if (M[i] < 0)
5897  continue; // ignore UNDEF indices
5898  if (ExpectedElt != static_cast<unsigned>(M[i]))
5899  return false;
5900  }
5901 
5902  return true;
5903 }
5904 
5905 // check if an EXT instruction can handle the shuffle mask when the
5906 // vector sources of the shuffle are different.
5907 static bool isEXTMask(ArrayRef<int> M, EVT VT, bool &ReverseEXT,
5908  unsigned &Imm) {
5909  // Look for the first non-undef element.
5910  const int *FirstRealElt = find_if(M, [](int Elt) { return Elt >= 0; });
5911 
5912  // Benefit form APInt to handle overflow when calculating expected element.
5913  unsigned NumElts = VT.getVectorNumElements();
5914  unsigned MaskBits = APInt(32, NumElts * 2).logBase2();
5915  APInt ExpectedElt = APInt(MaskBits, *FirstRealElt + 1);
5916  // The following shuffle indices must be the successive elements after the
5917  // first real element.
5918  const int *FirstWrongElt = std::find_if(FirstRealElt + 1, M.end(),
5919  [&](int Elt) {return Elt != ExpectedElt++ && Elt != -1;});
5920  if (FirstWrongElt != M.end())
5921  return false;
5922 
5923  // The index of an EXT is the first element if it is not UNDEF.
5924  // Watch out for the beginning UNDEFs. The EXT index should be the expected
5925  // value of the first element. E.g.
5926  // <-1, -1, 3, ...> is treated as <1, 2, 3, ...>.
5927  // <-1, -1, 0, 1, ...> is treated as <2*NumElts-2, 2*NumElts-1, 0, 1, ...>.
5928  // ExpectedElt is the last mask index plus 1.
5929  Imm = ExpectedElt.getZExtValue();
5930 
5931  // There are two difference cases requiring to reverse input vectors.
5932  // For example, for vector <4 x i32> we have the following cases,
5933  // Case 1: shufflevector(<4 x i32>,<4 x i32>,<-1, -1, -1, 0>)
5934  // Case 2: shufflevector(<4 x i32>,<4 x i32>,<-1, -1, 7, 0>)
5935  // For both cases, we finally use mask <5, 6, 7, 0>, which requires
5936  // to reverse two input vectors.
5937  if (Imm < NumElts)
5938  ReverseEXT = true;
5939  else
5940  Imm -= NumElts;
5941 
5942  return true;
5943 }
5944 
5945 /// isREVMask - Check if a vector shuffle corresponds to a REV
5946 /// instruction with the specified blocksize. (The order of the elements
5947 /// within each block of the vector is reversed.)
5948 static bool isREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
5949  assert((BlockSize == 16 || BlockSize == 32 || BlockSize == 64) &&
5950  "Only possible block sizes for REV are: 16, 32, 64");
5951 
5952  unsigned EltSz = VT.getScalarSizeInBits();
5953  if (EltSz == 64)
5954  return false;
5955 
5956  unsigned NumElts = VT.getVectorNumElements();
5957  unsigned BlockElts = M[0] + 1;
5958  // If the first shuffle index is UNDEF, be optimistic.
5959  if (M[0] < 0)
5960  BlockElts = BlockSize / EltSz;
5961 
5962  if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5963  return false;
5964 
5965  for (unsigned i = 0; i < NumElts; ++i) {
5966  if (M[i] < 0)
5967  continue; // ignore UNDEF indices
5968  if ((unsigned)M[i] != (i - i % BlockElts) + (BlockElts - 1 - i % BlockElts))
5969  return false;
5970  }
5971 
5972  return true;
5973 }
5974 
5975 static bool isZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
5976  unsigned NumElts = VT.getVectorNumElements();
5977  WhichResult = (M[0] == 0 ? 0 : 1);
5978  unsigned Idx = WhichResult * NumElts / 2;
5979  for (unsigned i = 0; i != NumElts; i += 2) {
5980  if ((M[i] >= 0 && (unsigned)M[i] != Idx) ||
5981  (M[i + 1] >= 0 && (unsigned)M[i + 1] != Idx + NumElts))
5982  return false;
5983  Idx += 1;
5984  }
5985 
5986  return true;
5987 }
5988 
5989 static bool isUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
5990  unsigned NumElts = VT.getVectorNumElements();
5991  WhichResult = (M[0] == 0 ? 0 : 1);
5992  for (unsigned i = 0; i != NumElts; ++i) {
5993  if (M[i] < 0)
5994  continue; // ignore UNDEF indices
5995  if ((unsigned)M[i] != 2 * i + WhichResult)
5996  return false;
5997  }
5998 
5999  return true;
6000 }
6001 
6002 static bool isTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6003  unsigned NumElts = VT.getVectorNumElements();
6004  WhichResult = (M[0] == 0 ? 0 : 1);
6005  for (unsigned i = 0; i < NumElts; i += 2) {
6006  if ((M[i] >= 0 && (unsigned)M[i] != i + WhichResult) ||
6007  (M[i + 1] >= 0 && (unsigned)M[i + 1] != i + NumElts + WhichResult))
6008  return false;
6009  }
6010  return true;
6011 }
6012 
6013 /// isZIP_v_undef_Mask - Special case of isZIPMask for canonical form of
6014 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
6015 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
6016 static bool isZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6017  unsigned NumElts = VT.getVectorNumElements();
6018  WhichResult = (M[0] == 0 ? 0 : 1);
6019  unsigned Idx = WhichResult * NumElts / 2;
6020  for (unsigned i = 0; i != NumElts; i += 2) {
6021  if ((M[i] >= 0 && (unsigned)M[i] != Idx) ||
6022  (M[i + 1] >= 0 && (unsigned)M[i + 1] != Idx))
6023  return false;
6024  Idx += 1;
6025  }
6026 
6027  return true;
6028 }
6029 
6030 /// isUZP_v_undef_Mask - Special case of isUZPMask for canonical form of
6031 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
6032 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
6033 static bool isUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6034  unsigned Half = VT.getVectorNumElements() / 2;
6035  WhichResult = (M[0] == 0 ? 0 : 1);
6036  for (unsigned j = 0; j != 2; ++j) {
6037  unsigned Idx = WhichResult;
6038  for (unsigned i = 0; i != Half; ++i) {
6039  int MIdx = M[i + j * Half];
6040  if (MIdx >= 0 && (unsigned)MIdx != Idx)
6041  return false;
6042  Idx += 2;
6043  }
6044  }
6045 
6046  return true;
6047 }
6048 
6049 /// isTRN_v_undef_Mask - Special case of isTRNMask for canonical form of
6050 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
6051 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
6052 static bool isTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6053  unsigned NumElts = VT.getVectorNumElements();
6054  WhichResult = (M[0] == 0 ? 0 : 1);
6055  for (unsigned i = 0; i < NumElts; i += 2) {
6056  if ((M[i] >= 0 && (unsigned)M[i] != i + WhichResult) ||
6057  (M[i + 1] >= 0 && (unsigned)M[i + 1] != i + WhichResult))
6058  return false;
6059  }
6060  return true;
6061 }
6062 
6063 static bool isINSMask(ArrayRef<int> M, int NumInputElements,
6064  bool &DstIsLeft, int &Anomaly) {
6065  if (M.size() != static_cast<size_t>(NumInputElements))
6066  return false;
6067 
6068  int NumLHSMatch = 0, NumRHSMatch = 0;
6069  int LastLHSMismatch = -1, LastRHSMismatch = -1;
6070 
6071  for (int i = 0; i < NumInputElements; ++i) {
6072  if (M[i] == -1) {
6073  ++NumLHSMatch;
6074  ++NumRHSMatch;
6075  continue;
6076  }
6077 
6078  if (M[i] == i)
6079  ++NumLHSMatch;
6080  else
6081  LastLHSMismatch = i;
6082 
6083  if (M[i] == i + NumInputElements)
6084  ++NumRHSMatch;
6085  else
6086  LastRHSMismatch = i;
6087  }
6088 
6089  if (NumLHSMatch == NumInputElements - 1) {
6090  DstIsLeft = true;
6091  Anomaly = LastLHSMismatch;
6092  return true;
6093  } else if (NumRHSMatch == NumInputElements - 1) {
6094  DstIsLeft = false;
6095  Anomaly = LastRHSMismatch;
6096  return true;
6097  }
6098 
6099  return false;
6100 }
6101 
6102 static bool isConcatMask(ArrayRef<int> Mask, EVT VT, bool SplitLHS) {
6103  if (VT.getSizeInBits() != 128)
6104  return false;
6105 
6106  unsigned NumElts = VT.getVectorNumElements();
6107 
6108  for (int I = 0, E = NumElts / 2; I != E; I++) {
6109  if (Mask[I] != I)
6110  return false;
6111  }
6112 
6113  int Offset = NumElts / 2;
6114  for (int I = NumElts / 2, E = NumElts; I != E; I++) {
6115  if (Mask[I] != I + SplitLHS * Offset)
6116  return false;
6117  }
6118 
6119  return true;
6120 }
6121 
6123  SDLoc DL(Op);
6124  EVT VT = Op.getValueType();
6125  SDValue V0 = Op.getOperand(0);
6126  SDValue V1 = Op.getOperand(1);
6127  ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op)->getMask();
6128 
6131  return SDValue();
6132 
6133  bool SplitV0 = V0.getValueSizeInBits() == 128;
6134 
6135  if (!isConcatMask(Mask, VT, SplitV0))
6136  return SDValue();
6137 
6138  EVT CastVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
6139  VT.getVectorNumElements() / 2);
6140  if (SplitV0) {
6141  V0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, CastVT, V0,
6142  DAG.getConstant(0, DL, MVT::i64));
6143  }
6144  if (V1.getValueSizeInBits() == 128) {
6145  V1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, CastVT, V1,
6146  DAG.getConstant(0, DL, MVT::i64));
6147  }
6148  return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, V0, V1);
6149 }
6150 
6151 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6152 /// the specified operations to build the shuffle.
6153 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
6154  SDValue RHS, SelectionDAG &DAG,
6155  const SDLoc &dl) {
6156  unsigned OpNum = (PFEntry >> 26) & 0x0F;
6157  unsigned LHSID = (PFEntry >> 13) & ((1 << 13) - 1);
6158  unsigned RHSID = (PFEntry >> 0) & ((1 << 13) - 1);
6159 
6160  enum {
6161  OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
6162  OP_VREV,
6163  OP_VDUP0,
6164  OP_VDUP1,
6165  OP_VDUP2,
6166  OP_VDUP3,
6167  OP_VEXT1,
6168  OP_VEXT2,
6169  OP_VEXT3,
6170  OP_VUZPL, // VUZP, left result
6171  OP_VUZPR, // VUZP, right result
6172  OP_VZIPL, // VZIP, left result
6173  OP_VZIPR, // VZIP, right result
6174  OP_VTRNL, // VTRN, left result
6175  OP_VTRNR // VTRN, right result
6176  };
6177 
6178  if (OpNum == OP_COPY) {
6179  if (LHSID == (1 * 9 + 2) * 9 + 3)
6180  return LHS;
6181  assert(LHSID == ((4 * 9 + 5) * 9 + 6) * 9 + 7 && "Illegal OP_COPY!");
6182  return RHS;
6183  }
6184 
6185  SDValue OpLHS, OpRHS;
6186  OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6187  OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6188  EVT VT = OpLHS.getValueType();
6189 
6190  switch (OpNum) {
6191  default:
6192  llvm_unreachable("Unknown shuffle opcode!");
6193  case OP_VREV:
6194  // VREV divides the vector in half and swaps within the half.
6195  if (VT.getVectorElementType() == MVT::i32 ||
6197  return DAG.getNode(AArch64ISD::REV64, dl, VT, OpLHS);
6198  // vrev <4 x i16> -> REV32
6199  if (VT.getVectorElementType() == MVT::i16 ||
6201  return DAG.getNode(AArch64ISD::REV32, dl, VT, OpLHS);
6202  // vrev <4 x i8> -> REV16
6204  return DAG.getNode(AArch64ISD::REV16, dl, VT, OpLHS);
6205  case OP_VDUP0:
6206  case OP_VDUP1:
6207  case OP_VDUP2:
6208  case OP_VDUP3: {
6209  EVT EltTy = VT.getVectorElementType();
6210  unsigned Opcode;
6211  if (EltTy == MVT::i8)
6212  Opcode = AArch64ISD::DUPLANE8;
6213  else if (EltTy == MVT::i16 || EltTy == MVT::f16)
6214  Opcode = AArch64ISD::DUPLANE16;
6215  else if (EltTy == MVT::i32 || EltTy == MVT::f32)
6216  Opcode = AArch64ISD::DUPLANE32;
6217  else if (EltTy == MVT::i64 || EltTy == MVT::f64)
6218  Opcode = AArch64ISD::DUPLANE64;
6219  else
6220  llvm_unreachable("Invalid vector element type?");
6221 
6222  if (VT.getSizeInBits() == 64)
6223  OpLHS = WidenVector(OpLHS, DAG);
6224  SDValue Lane = DAG.getConstant(OpNum - OP_VDUP0, dl, MVT::i64);
6225  return DAG.getNode(Opcode, dl, VT, OpLHS, Lane);
6226  }
6227  case OP_VEXT1:
6228  case OP_VEXT2:
6229  case OP_VEXT3: {
6230  unsigned Imm = (OpNum - OP_VEXT1 + 1) * getExtFactor(OpLHS);
6231  return DAG.getNode(AArch64ISD::EXT, dl, VT, OpLHS, OpRHS,
6232  DAG.getConstant(Imm, dl, MVT::i32));
6233  }
6234  case OP_VUZPL:
6235  return DAG.getNode(AArch64ISD::UZP1, dl, DAG.getVTList(VT, VT), OpLHS,
6236  OpRHS);
6237  case OP_VUZPR:
6238  return DAG.getNode(AArch64ISD::UZP2, dl, DAG.getVTList(VT, VT), OpLHS,
6239  OpRHS);
6240  case OP_VZIPL:
6241  return DAG.getNode(AArch64ISD::ZIP1, dl, DAG.getVTList(VT, VT), OpLHS,
6242  OpRHS);
6243  case OP_VZIPR:
6244  return DAG.getNode(AArch64ISD::ZIP2, dl, DAG.getVTList(VT, VT), OpLHS,
6245  OpRHS);
6246  case OP_VTRNL:
6247  return DAG.getNode(AArch64ISD::TRN1, dl, DAG.getVTList(VT, VT), OpLHS,
6248  OpRHS);
6249  case OP_VTRNR:
6250  return DAG.getNode(AArch64ISD::TRN2, dl, DAG.getVTList(VT, VT), OpLHS,
6251  OpRHS);
6252  }
6253 }
6254 
6255 static SDValue GenerateTBL(SDValue Op, ArrayRef<int> ShuffleMask,
6256  SelectionDAG &DAG) {
6257  // Check to see if we can use the TBL instruction.
6258  SDValue V1 = Op.getOperand(0);
6259  SDValue V2 = Op.getOperand(1);
6260  SDLoc DL(Op);
6261 
6262  EVT EltVT = Op.getValueType().getVectorElementType();
6263  unsigned BytesPerElt = EltVT.getSizeInBits() / 8;
6264 
6265  SmallVector<SDValue, 8> TBLMask;
6266  for (int Val : ShuffleMask) {
6267  for (unsigned Byte = 0; Byte < BytesPerElt; ++Byte) {
6268  unsigned Offset = Byte + Val * BytesPerElt;
6269  TBLMask.push_back(DAG.getConstant(Offset, DL, MVT::i32));
6270  }
6271  }
6272 
6273  MVT IndexVT = MVT::v8i8;
6274  unsigned IndexLen = 8;
6275  if (Op.getValueSizeInBits() == 128) {
6276  IndexVT = MVT::v16i8;
6277  IndexLen = 16;
6278  }
6279 
6280  SDValue V1Cst = DAG.getNode(ISD::BITCAST, DL, IndexVT, V1);
6281  SDValue V2Cst = DAG.getNode(ISD::BITCAST, DL, IndexVT, V2);
6282 
6283  SDValue Shuffle;
6284  if (V2.getNode()->isUndef()) {
6285  if (IndexLen == 8)
6286  V1Cst = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v16i8, V1Cst, V1Cst);
6287  Shuffle = DAG.getNode(
6288  ISD::INTRINSIC_WO_CHAIN, DL, IndexVT,
6289  DAG.getConstant(Intrinsic::aarch64_neon_tbl1, DL, MVT::i32), V1Cst,
6290  DAG.getBuildVector(IndexVT, DL,
6291  makeArrayRef(TBLMask.data(), IndexLen)));
6292  } else {
6293  if (IndexLen == 8) {
6294  V1Cst = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v16i8, V1Cst, V2Cst);
6295  Shuffle = DAG.getNode(
6296  ISD::INTRINSIC_WO_CHAIN, DL, IndexVT,
6297  DAG.getConstant(Intrinsic::aarch64_neon_tbl1, DL, MVT::i32), V1Cst,
6298  DAG.getBuildVector(IndexVT, DL,
6299  makeArrayRef(TBLMask.data(), IndexLen)));
6300  } else {
6301  // FIXME: We cannot, for the moment, emit a TBL2 instruction because we
6302  // cannot currently represent the register constraints on the input
6303  // table registers.
6304  // Shuffle = DAG.getNode(AArch64ISD::TBL2, DL, IndexVT, V1Cst, V2Cst,
6305  // DAG.getBuildVector(IndexVT, DL, &TBLMask[0],
6306  // IndexLen));
6307  Shuffle = DAG.getNode(
6308  ISD::INTRINSIC_WO_CHAIN, DL, IndexVT,
6309  DAG.getConstant(Intrinsic::aarch64_neon_tbl2, DL, MVT::i32), V1Cst,
6310  V2Cst, DAG.getBuildVector(IndexVT, DL,
6311  makeArrayRef(TBLMask.data(), IndexLen)));
6312  }
6313  }
6314  return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Shuffle);
6315 }
6316 
6317 static unsigned getDUPLANEOp(EVT EltType) {
6318  if (EltType == MVT::i8)
6319  return AArch64ISD::DUPLANE8;
6320  if (EltType == MVT::i16 || EltType == MVT::f16)
6321  return AArch64ISD::DUPLANE16;
6322  if (EltType == MVT::i32 || EltType == MVT::f32)
6323  return AArch64ISD::DUPLANE32;
6324  if (EltType == MVT::i64 || EltType == MVT::f64)
6325  return AArch64ISD::DUPLANE64;
6326 
6327  llvm_unreachable("Invalid vector element type?");
6328 }
6329 
6330 SDValue AArch64TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
6331  SelectionDAG &DAG) const {
6332  SDLoc dl(Op);
6333  EVT VT = Op.getValueType();
6334 
6335  ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
6336 
6337  // Convert shuffles that are directly supported on NEON to target-specific
6338  // DAG nodes, instead of keeping them as shuffles and matching them again
6339  // during code selection. This is more efficient and avoids the possibility
6340  // of inconsistencies between legalization and selection.
6341  ArrayRef<int> ShuffleMask = SVN->getMask();
6342 
6343  SDValue V1 = Op.getOperand(0);
6344  SDValue V2 = Op.getOperand(1);
6345 
6346  if (SVN->isSplat()) {
6347  int Lane = SVN->getSplatIndex();
6348  // If this is undef splat, generate it via "just" vdup, if possible.
6349  if (Lane == -1)
6350  Lane = 0;
6351 
6352  if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
6353  return DAG.getNode(AArch64ISD::DUP, dl, V1.getValueType(),
6354  V1.getOperand(0));
6355  // Test if V1 is a BUILD_VECTOR and the lane being referenced is a non-
6356  // constant. If so, we can just reference the lane's definition directly.
6357  if (V1.getOpcode() == ISD::BUILD_VECTOR &&
6358  !isa<ConstantSDNode>(V1.getOperand(Lane)))
6359  return DAG.getNode(AArch64ISD::DUP, dl, VT, V1.getOperand(Lane));
6360 
6361  // Otherwise, duplicate from the lane of the input vector.
6362  unsigned Opcode = getDUPLANEOp(V1.getValueType().getVectorElementType());
6363 
6364  // SelectionDAGBuilder may have "helpfully" already extracted or conatenated
6365  // to make a vector of the same size as this SHUFFLE. We can ignore the
6366  // extract entirely, and canonicalise the concat using WidenVector.
6367  if (V1.getOpcode() == ISD::EXTRACT_SUBVECTOR) {
6368  Lane += cast<ConstantSDNode>(V1.getOperand(1))->getZExtValue();
6369  V1 = V1.getOperand(0);
6370  } else if (V1.getOpcode() == ISD::CONCAT_VECTORS) {
6371  unsigned Idx = Lane >= (int)VT.getVectorNumElements() / 2;
6372  Lane -= Idx * VT.getVectorNumElements() / 2;
6373  V1 = WidenVector(V1.getOperand(Idx), DAG);
6374  } else if (VT.getSizeInBits() == 64)
6375  V1 = WidenVector(V1, DAG);
6376 
6377  return DAG.getNode(Opcode, dl, VT, V1, DAG.getConstant(Lane, dl, MVT::i64));
6378  }
6379 
6380  if (isREVMask(ShuffleMask, VT, 64))
6381  return DAG.getNode(AArch64ISD::REV64, dl, V1.getValueType(), V1, V2);
6382  if (isREVMask(ShuffleMask, VT, 32))
6383  return DAG.getNode(AArch64ISD::REV32, dl, V1.getValueType(), V1, V2);
6384  if (isREVMask(ShuffleMask, VT, 16))
6385  return DAG.getNode(AArch64ISD::REV16, dl, V1.getValueType(), V1, V2);
6386 
6387  bool ReverseEXT = false;
6388  unsigned Imm;
6389  if (isEXTMask(ShuffleMask, VT, ReverseEXT, Imm)) {
6390  if (ReverseEXT)
6391  std::swap(V1, V2);
6392  Imm *= getExtFactor(V1);
6393  return DAG.getNode(AArch64ISD::EXT, dl, V1.getValueType(), V1, V2,
6394  DAG.getConstant(Imm, dl, MVT::i32));
6395  } else if (V2->isUndef() && isSingletonEXTMask(ShuffleMask, VT, Imm)) {
6396  Imm *= getExtFactor(V1);
6397  return DAG.getNode(AArch64ISD::EXT, dl, V1.getValueType(), V1, V1,
6398  DAG.getConstant(Imm, dl, MVT::i32));
6399  }
6400 
6401  unsigned WhichResult;
6402  if (isZIPMask(ShuffleMask, VT, WhichResult)) {
6403  unsigned Opc = (WhichResult == 0) ? AArch64ISD::ZIP1 : AArch64ISD::ZIP2;
6404  return DAG.getNode(Opc, dl, V1.getValueType(), V1, V2);
6405  }
6406  if (isUZPMask(ShuffleMask, VT, WhichResult)) {
6407  unsigned Opc = (WhichResult == 0) ? AArch64ISD::UZP1 : AArch64ISD::UZP2;
6408  return DAG.getNode(Opc, dl, V1.getValueType(), V1, V2);
6409  }
6410  if (isTRNMask(ShuffleMask, VT, WhichResult)) {
6411  unsigned Opc = (WhichResult == 0) ? AArch64ISD::TRN1 : AArch64ISD::TRN2;
6412  return DAG.getNode(Opc, dl, V1.getValueType(), V1, V2);
6413  }
6414 
6415  if (isZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) {
6416  unsigned Opc = (WhichResult == 0) ? AArch64ISD::ZIP1 : AArch64ISD::ZIP2;
6417  return DAG.getNode(Opc, dl, V1.getValueType(), V1, V1);
6418  }
6419  if (isUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) {
6420  unsigned Opc = (WhichResult == 0) ? AArch64ISD::UZP1 : AArch64ISD::UZP2;
6421  return DAG.getNode(Opc, dl, V1.getValueType(), V1, V1);
6422  }
6423  if (isTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) {
6424  unsigned Opc = (WhichResult == 0) ? AArch64ISD::TRN1 : AArch64ISD::TRN2;
6425  return DAG.getNode(Opc, dl, V1.getValueType(), V1, V1);
6426  }
6427 
6428  if (SDValue Concat = tryFormConcatFromShuffle(Op, DAG))
6429  return Concat;
6430 
6431  bool DstIsLeft;
6432  int Anomaly;
6433  int NumInputElements = V1.getValueType().getVectorNumElements();
6434  if (isINSMask(ShuffleMask, NumInputElements, DstIsLeft, Anomaly)) {
6435  SDValue DstVec = DstIsLeft ? V1 : V2;
6436  SDValue DstLaneV = DAG.getConstant(Anomaly, dl, MVT::i64);
6437 
6438  SDValue SrcVec = V1;
6439  int SrcLane = ShuffleMask[Anomaly];
6440  if (SrcLane >= NumInputElements) {
6441  SrcVec = V2;
6442  SrcLane -= VT.getVectorNumElements();
6443  }
6444  SDValue SrcLaneV = DAG.getConstant(SrcLane, dl, MVT::i64);
6445 
6446  EVT ScalarVT = VT.getVectorElementType();
6447 
6448  if (ScalarVT.getSizeInBits() < 32 && ScalarVT.isInteger())
6449  ScalarVT = MVT::i32;
6450 
6451  return DAG.getNode(
6452  ISD::INSERT_VECTOR_ELT, dl, VT, DstVec,
6453  DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, SrcVec, SrcLaneV),
6454  DstLaneV);
6455  }
6456 
6457  // If the shuffle is not directly supported and it has 4 elements, use
6458  // the PerfectShuffle-generated table to synthesize it from other shuffles.
6459  unsigned NumElts = VT.getVectorNumElements();
6460  if (NumElts == 4) {
6461  unsigned PFIndexes[4];
6462  for (unsigned i = 0; i != 4; ++i) {
6463  if (ShuffleMask[i] < 0)
6464  PFIndexes[i] = 8;
6465  else
6466  PFIndexes[i] = ShuffleMask[i];
6467  }
6468 
6469  // Compute the index in the perfect shuffle table.
6470  unsigned PFTableIndex = PFIndexes[0] * 9 * 9 * 9 + PFIndexes[1] * 9 * 9 +
6471  PFIndexes[2] * 9 + PFIndexes[3];
6472  unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6473  unsigned Cost = (PFEntry >> 30);
6474 
6475  if (Cost <= 4)
6476  return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6477  }
6478 
6479  return GenerateTBL(Op, ShuffleMask, DAG);
6480 }
6481 
6482 static bool resolveBuildVector(BuildVectorSDNode *BVN, APInt &CnstBits,
6483  APInt &UndefBits) {
6484  EVT VT = BVN->getValueType(0);
6485  APInt SplatBits, SplatUndef;
6486  unsigned SplatBitSize;
6487  bool HasAnyUndefs;
6488  if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6489  unsigned NumSplats = VT.getSizeInBits() / SplatBitSize;
6490 
6491  for (unsigned i = 0; i < NumSplats; ++i) {
6492  CnstBits <<= SplatBitSize;
6493  UndefBits <<= SplatBitSize;
6494  CnstBits |= SplatBits.zextOrTrunc(VT.getSizeInBits());
6495  UndefBits |= (SplatBits ^ SplatUndef).zextOrTrunc(VT.getSizeInBits());
6496  }
6497 
6498  return true;
6499  }
6500 
6501  return false;
6502 }
6503 
6504 // Try 64-bit splatted SIMD immediate.
6505 static SDValue tryAdvSIMDModImm64(unsigned NewOp, SDValue Op, SelectionDAG &DAG,
6506  const APInt &Bits) {
6507  if (Bits.getHiBits(64) == Bits.getLoBits(64)) {
6508  uint64_t Value = Bits.zextOrTrunc(64).getZExtValue();
6509  EVT VT = Op.getValueType();
6510  MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v2i64 : MVT::f64;
6511 
6512  if (AArch64_AM::isAdvSIMDModImmType10(Value)) {
6514 
6515  SDLoc dl(Op);
6516  SDValue Mov = DAG.getNode(NewOp, dl, MovTy,
6517  DAG.getConstant(Value, dl, MVT::i32));
6518  return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov);
6519  }
6520  }
6521 
6522  return SDValue();
6523 }
6524 
6525 // Try 32-bit splatted SIMD immediate.
6526 static SDValue tryAdvSIMDModImm32(unsigned NewOp, SDValue Op, SelectionDAG &DAG,
6527  const APInt &Bits,
6528  const SDValue *LHS = nullptr) {
6529  if (Bits.getHiBits(64) == Bits.getLoBits(64)) {
6530  uint64_t Value = Bits.zextOrTrunc(64).getZExtValue();
6531  EVT VT = Op.getValueType();
6532  MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32;
6533  bool isAdvSIMDModImm = false;
6534  uint64_t Shift;
6535 
6536  if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType1(Value))) {
6537  Value = AArch64_AM::encodeAdvSIMDModImmType1(Value);
6538  Shift = 0;
6539  }
6540  else if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType2(Value))) {
6541  Value = AArch64_AM::encodeAdvSIMDModImmType2(Value);
6542  Shift = 8;
6543  }
6544  else if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType3(Value))) {
6545  Value = AArch64_AM::encodeAdvSIMDModImmType3(Value);
6546  Shift = 16;
6547  }
6548  else if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType4(Value))) {
6549  Value = AArch64_AM::encodeAdvSIMDModImmType4(Value);
6550  Shift = 24;
6551  }
6552 
6553  if (isAdvSIMDModImm) {
6554  SDLoc dl(Op);
6555  SDValue Mov;
6556 
6557  if (LHS)
6558  Mov = DAG.getNode(NewOp, dl, MovTy, *LHS,
6559  DAG.getConstant(Value, dl, MVT::i32),
6560  DAG.getConstant(Shift, dl, MVT::i32));
6561  else
6562  Mov = DAG.getNode(NewOp, dl, MovTy,
6563  DAG.getConstant(Value, dl, MVT::i32),
6564  DAG.getConstant(Shift, dl, MVT::i32));
6565 
6566  return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov);
6567  }
6568  }
6569 
6570  return SDValue();
6571 }
6572 
6573 // Try 16-bit splatted SIMD immediate.
6574 static SDValue tryAdvSIMDModImm16(unsigned NewOp, SDValue Op, SelectionDAG &DAG,
6575  const APInt &Bits,
6576  const SDValue *LHS = nullptr) {
6577  if (Bits.getHiBits(64) == Bits.getLoBits(64)) {
6578  uint64_t Value = Bits.zextOrTrunc(64).getZExtValue();
6579  EVT VT = Op.getValueType();
6580  MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v8i16 : MVT::v4i16;
6581  bool isAdvSIMDModImm = false;
6582  uint64_t Shift;
6583 
6584  if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType5(Value))) {
6585  Value = AArch64_AM::encodeAdvSIMDModImmType5(Value);
6586  Shift = 0;
6587  }
6588  else if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType6(Value))) {
6589  Value = AArch64_AM::encodeAdvSIMDModImmType6(Value);
6590  Shift = 8;
6591  }
6592 
6593  if (isAdvSIMDModImm) {
6594  SDLoc dl(Op);
6595  SDValue Mov;
6596 
6597  if (LHS)
6598  Mov = DAG.getNode(NewOp, dl, MovTy, *LHS,
6599  DAG.getConstant(Value, dl, MVT::i32),
6600  DAG.getConstant(Shift, dl, MVT::i32));
6601  else
6602  Mov = DAG.getNode(NewOp, dl, MovTy,
6603  DAG.getConstant(Value, dl, MVT::i32),
6604  DAG.getConstant(Shift, dl, MVT::i32));
6605 
6606  return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov);
6607  }
6608  }
6609 
6610  return SDValue();
6611 }
6612 
6613 // Try 32-bit splatted SIMD immediate with shifted ones.
6614 static SDValue tryAdvSIMDModImm321s(unsigned NewOp, SDValue Op,
6615  SelectionDAG &DAG, const APInt &Bits) {
6616  if (Bits.getHiBits(64) == Bits.getLoBits(64)) {
6617  uint64_t Value = Bits.zextOrTrunc(64).getZExtValue();
6618  EVT VT = Op.getValueType();
6619  MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v4i32 : MVT::v2i32;
6620  bool isAdvSIMDModImm = false;
6621  uint64_t Shift;
6622 
6623  if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType7(Value))) {
6624  Value = AArch64_AM::encodeAdvSIMDModImmType7(Value);
6625  Shift = 264;
6626  }
6627  else if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType8(Value))) {
6628  Value = AArch64_AM::encodeAdvSIMDModImmType8(Value);
6629  Shift = 272;
6630  }
6631 
6632  if (isAdvSIMDModImm) {
6633  SDLoc dl(Op);
6634  SDValue Mov = DAG.getNode(NewOp, dl, MovTy,
6635  DAG.getConstant(Value, dl, MVT::i32),
6636  DAG.getConstant(Shift, dl, MVT::i32));
6637  return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov);
6638  }
6639  }
6640 
6641  return SDValue();
6642 }
6643 
6644 // Try 8-bit splatted SIMD immediate.
6645 static SDValue tryAdvSIMDModImm8(unsigned NewOp, SDValue Op, SelectionDAG &DAG,
6646  const APInt &Bits) {
6647  if (Bits.getHiBits(64) == Bits.getLoBits(64)) {
6648  uint64_t Value = Bits.zextOrTrunc(64).getZExtValue();
6649  EVT VT = Op.getValueType();
6650  MVT MovTy = (VT.getSizeInBits() == 128) ? MVT::v16i8 : MVT::v8i8;
6651 
6652  if (AArch64_AM::isAdvSIMDModImmType9(Value)) {
6653  Value = AArch64_AM::encodeAdvSIMDModImmType9(Value);
6654 
6655  SDLoc dl(Op);
6656  SDValue Mov = DAG.getNode(NewOp, dl, MovTy,
6657  DAG.getConstant(Value, dl, MVT::i32));
6658  return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov);
6659  }
6660  }
6661 
6662  return SDValue();
6663 }
6664 
6665 // Try FP splatted SIMD immediate.
6666 static SDValue tryAdvSIMDModImmFP(unsigned NewOp, SDValue Op, SelectionDAG &DAG,
6667  const APInt &Bits) {
6668  if (Bits.getHiBits(64) == Bits.getLoBits(64)) {
6669  uint64_t Value = Bits.zextOrTrunc(64).getZExtValue();
6670  EVT VT = Op.getValueType();
6671  bool isWide = (VT.getSizeInBits() == 128);
6672  MVT MovTy;
6673  bool isAdvSIMDModImm = false;
6674 
6675  if ((isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType11(Value))) {
6677  MovTy = isWide ? MVT::v4f32 : MVT::v2f32;
6678  }
6679  else if (isWide &&
6680  (isAdvSIMDModImm = AArch64_AM::isAdvSIMDModImmType12(Value))) {
6682  MovTy = MVT::v2f64;
6683  }
6684 
6685  if (isAdvSIMDModImm) {
6686  SDLoc dl(Op);
6687  SDValue Mov = DAG.getNode(NewOp, dl, MovTy,
6688  DAG.getConstant(Value, dl, MVT::i32));
6689  return DAG.getNode(AArch64ISD::NVCAST, dl, VT, Mov);
6690  }
6691  }
6692 
6693  return SDValue();
6694 }
6695 
6696 SDValue AArch64TargetLowering::LowerVectorAND(SDValue Op,
6697  SelectionDAG &DAG) const {
6698  SDValue LHS = Op.getOperand(0);
6699  EVT VT = Op.getValueType();
6700 
6701  BuildVectorSDNode *BVN =
6703  if (!BVN) {
6704  // AND commutes, so try swapping the operands.
6705  LHS = Op.getOperand(1);
6706  BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(0).getNode());
6707  }
6708  if (!BVN)
6709  return Op;
6710 
6711  APInt DefBits(VT.getSizeInBits(), 0);
6712  APInt UndefBits(VT.getSizeInBits(), 0);
6713  if (resolveBuildVector(BVN, DefBits, UndefBits)) {
6714  SDValue NewOp;
6715 
6716  // We only have BIC vector immediate instruction, which is and-not.
6717  DefBits = ~DefBits;
6718  if ((NewOp = tryAdvSIMDModImm32(AArch64ISD::BICi, Op, DAG,
6719  DefBits, &LHS)) ||
6720  (NewOp = tryAdvSIMDModImm16(AArch64ISD::BICi, Op, DAG,
6721  DefBits, &LHS)))
6722  return NewOp;
6723 
6724  UndefBits = ~UndefBits;
6725  if ((NewOp = tryAdvSIMDModImm32(AArch64ISD::BICi, Op, DAG,
6726  UndefBits, &LHS)) ||
6727  (NewOp = tryAdvSIMDModImm16(AArch64ISD::BICi, Op, DAG,
6728  UndefBits, &LHS)))
6729  return NewOp;
6730  }
6731 
6732  // We can always fall back to a non-immediate AND.
6733  return Op;
6734 }
6735 
6736 // Specialized code to quickly find if PotentialBVec is a BuildVector that
6737 // consists of only the same constant int value, returned in reference arg
6738 // ConstVal
6739 static bool isAllConstantBuildVector(const SDValue &PotentialBVec,
6740  uint64_t &ConstVal) {
6741  BuildVectorSDNode *Bvec = dyn_cast<BuildVectorSDNode>(PotentialBVec);
6742  if (!Bvec)
6743  return false;
6744  ConstantSDNode *FirstElt = dyn_cast<ConstantSDNode>(Bvec->getOperand(0));
6745  if (!FirstElt)
6746  return false;
6747  EVT VT = Bvec->getValueType(0);
6748  unsigned NumElts = VT.getVectorNumElements();
6749  for (unsigned i = 1; i < NumElts; ++i)
6750  if (dyn_cast<ConstantSDNode>(Bvec->getOperand(i)) != FirstElt)
6751  return false;
6752  ConstVal = FirstElt->getZExtValue();
6753  return true;
6754 }
6755 
6756 static unsigned getIntrinsicID(const SDNode *N) {
6757  unsigned Opcode = N->getOpcode();
6758  switch (Opcode) {
6759  default:
6760  return Intrinsic::not_intrinsic;
6761  case ISD::INTRINSIC_WO_CHAIN: {
6762  unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6763  if (IID < Intrinsic::num_intrinsics)
6764  return IID;
6765  return Intrinsic::not_intrinsic;
6766  }
6767  }
6768 }
6769 
6770 // Attempt to form a vector S[LR]I from (or (and X, BvecC1), (lsl Y, C2)),
6771 // to (SLI X, Y, C2), where X and Y have matching vector types, BvecC1 is a
6772 // BUILD_VECTORs with constant element C1, C2 is a constant, and C1 == ~C2.
6773 // Also, logical shift right -> sri, with the same structure.
6775  EVT VT = N->getValueType(0);
6776 
6777  if (!VT.isVector())
6778  return SDValue();
6779 
6780  SDLoc DL(N);
6781 
6782  // Is the first op an AND?
6783  const SDValue And = N->getOperand(0);
6784  if (And.getOpcode() != ISD::AND)
6785  return SDValue();
6786 
6787  // Is the second op an shl or lshr?
6788  SDValue Shift = N->getOperand(1);
6789  // This will have been turned into: AArch64ISD::VSHL vector, #shift
6790  // or AArch64ISD::VLSHR vector, #shift
6791  unsigned ShiftOpc = Shift.getOpcode();
6792  if ((ShiftOpc != AArch64ISD::VSHL && ShiftOpc != AArch64ISD::VLSHR))
6793  return SDValue();
6794  bool IsShiftRight = ShiftOpc == AArch64ISD::VLSHR;
6795 
6796  // Is the shift amount constant?
6797  ConstantSDNode *C2node = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
6798  if (!C2node)
6799  return SDValue();
6800 
6801  // Is the and mask vector all constant?
6802  uint64_t C1;
6803  if (!isAllConstantBuildVector(And.getOperand(1), C1))
6804  return SDValue();
6805 
6806  // Is C1 == ~C2, taking into account how much one can shift elements of a
6807  // particular size?
6808  uint64_t C2 = C2node->getZExtValue();
6809  unsigned ElemSizeInBits = VT.getScalarSizeInBits();
6810  if (C2 > ElemSizeInBits)
6811  return SDValue();
6812  unsigned ElemMask = (1 << ElemSizeInBits) - 1;
6813  if ((C1 & ElemMask) != (~C2 & ElemMask))
6814  return SDValue();
6815 
6816  SDValue X = And.getOperand(0);
6817  SDValue Y = Shift.getOperand(0);
6818 
6819  unsigned Intrin =
6820  IsShiftRight ? Intrinsic::aarch64_neon_vsri : Intrinsic::aarch64_neon_vsli;
6821  SDValue ResultSLI =
6822  DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
6823  DAG.getConstant(Intrin, DL, MVT::i32), X, Y,
6824  Shift.getOperand(1));
6825 
6826  LLVM_DEBUG(dbgs() << "aarch64-lower: transformed: \n");
6827  LLVM_DEBUG(N->dump(&DAG));
6828  LLVM_DEBUG(dbgs() << "into: \n");
6829  LLVM_DEBUG(ResultSLI->dump(&DAG));
6830 
6831  ++NumShiftInserts;
6832  return ResultSLI;
6833 }
6834 
6835 SDValue AArch64TargetLowering::LowerVectorOR(SDValue Op,
6836  SelectionDAG &DAG) const {
6837  // Attempt to form a vector S[LR]I from (or (and X, C1), (lsl Y, C2))
6839  if (SDValue Res = tryLowerToSLI(Op.getNode(), DAG))
6840  return Res;
6841  }
6842 
6843  EVT VT = Op.getValueType();
6844 
6845  SDValue LHS = Op.getOperand(0);
6846  BuildVectorSDNode *BVN =
6848  if (!BVN) {
6849  // OR commutes, so try swapping the operands.
6850  LHS = Op.getOperand(1);
6851  BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(0).getNode());
6852  }
6853  if (!BVN)
6854  return Op;
6855 
6856  APInt DefBits(VT.getSizeInBits(), 0);
6857  APInt UndefBits(VT.getSizeInBits(), 0);
6858  if (resolveBuildVector(BVN, DefBits, UndefBits)) {
6859  SDValue NewOp;
6860 
6861  if ((NewOp = tryAdvSIMDModImm32(AArch64ISD::ORRi, Op, DAG,
6862  DefBits, &LHS)) ||
6863  (NewOp = tryAdvSIMDModImm16(AArch64ISD::ORRi, Op, DAG,
6864  DefBits, &LHS)))
6865  return NewOp;
6866 
6867  if ((NewOp = tryAdvSIMDModImm32(AArch64ISD::ORRi, Op, DAG,
6868  UndefBits, &LHS)) ||
6869  (NewOp = tryAdvSIMDModImm16(AArch64ISD::ORRi, Op, DAG,
6870  UndefBits, &LHS)))
6871  return NewOp;
6872  }
6873 
6874  // We can always fall back to a non-immediate OR.
6875  return Op;
6876 }
6877 
6878 // Normalize the operands of BUILD_VECTOR. The value of constant operands will
6879 // be truncated to fit element width.
6881  SelectionDAG &DAG) {
6882  assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
6883  SDLoc dl(Op);
6884  EVT VT = Op.getValueType();
6885  EVT EltTy= VT.getVectorElementType();
6886 
6887  if (EltTy.isFloatingPoint() || EltTy.getSizeInBits() > 16)
6888  return Op;
6889 
6891  for (SDValue Lane : Op->ops()) {
6892  if (auto *CstLane = dyn_cast<ConstantSDNode>(Lane)) {
6893  APInt LowBits(EltTy.getSizeInBits(),
6894  CstLane->getZExtValue());
6895  Lane = DAG.getConstant(LowBits.getZExtValue(), dl, MVT::i32);
6896  }
6897  Ops.push_back(Lane);
6898  }
6899  return DAG.getBuildVector(VT, dl, Ops);
6900 }
6901 
6903  EVT VT = Op.getValueType();
6904 
6905  APInt DefBits(VT.getSizeInBits(), 0);
6906  APInt UndefBits(VT.getSizeInBits(), 0);
6907  BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
6908  if (resolveBuildVector(BVN, DefBits, UndefBits)) {
6909  SDValue NewOp;
6910  if ((NewOp = tryAdvSIMDModImm64(AArch64ISD::MOVIedit, Op, DAG, DefBits)) ||
6911  (NewOp = tryAdvSIMDModImm32(AArch64ISD::MOVIshift, Op, DAG, DefBits)) ||
6912  (NewOp = tryAdvSIMDModImm321s(AArch64ISD::MOVImsl, Op, DAG, DefBits)) ||
6913  (NewOp = tryAdvSIMDModImm16(AArch64ISD::MOVIshift, Op, DAG, DefBits)) ||
6914  (NewOp = tryAdvSIMDModImm8(AArch64ISD::MOVI, Op, DAG, DefBits)) ||
6915  (NewOp = tryAdvSIMDModImmFP(AArch64ISD::FMOV, Op, DAG, DefBits)))
6916  return NewOp;
6917 
6918  DefBits = ~DefBits;
6919  if ((NewOp = tryAdvSIMDModImm32(AArch64ISD::MVNIshift, Op, DAG, DefBits)) ||
6920  (NewOp = tryAdvSIMDModImm321s(AArch64ISD::MVNImsl, Op, DAG, DefBits)) ||
6921  (NewOp = tryAdvSIMDModImm16(AArch64ISD::MVNIshift, Op, DAG, DefBits)))
6922  return NewOp;
6923 
6924  DefBits = UndefBits;
6925  if ((NewOp = tryAdvSIMDModImm64(AArch64ISD::MOVIedit, Op, DAG, DefBits)) ||
6926  (NewOp = tryAdvSIMDModImm32(AArch64ISD::MOVIshift, Op, DAG, DefBits)) ||
6927  (NewOp = tryAdvSIMDModImm321s(AArch64ISD::MOVImsl, Op, DAG, DefBits)) ||
6928  (NewOp = tryAdvSIMDModImm16(AArch64ISD::MOVIshift, Op, DAG, DefBits)) ||
6929  (NewOp = tryAdvSIMDModImm8(AArch64ISD::MOVI, Op, DAG, DefBits)) ||
6930  (NewOp = tryAdvSIMDModImmFP(AArch64ISD::FMOV, Op, DAG, DefBits)))
6931  return NewOp;
6932 
6933  DefBits = ~UndefBits;
6934  if ((NewOp = tryAdvSIMDModImm32(AArch64ISD::MVNIshift, Op, DAG, DefBits)) ||
6935  (NewOp = tryAdvSIMDModImm321s(AArch64ISD::MVNImsl, Op, DAG, DefBits)) ||
6936  (NewOp = tryAdvSIMDModImm16(AArch64ISD::MVNIshift, Op, DAG, DefBits)))
6937  return NewOp;
6938  }
6939 
6940  return SDValue();
6941 }
6942 
6943 SDValue AArch64TargetLowering::LowerBUILD_VECTOR(SDValue Op,
6944  SelectionDAG &DAG) const {
6945  EVT VT = Op.getValueType();
6946 
6947  // Try to build a simple constant vector.
6948  Op = NormalizeBuildVector(Op, DAG);
6949  if (VT.isInteger()) {
6950  // Certain vector constants, used to express things like logical NOT and
6951  // arithmetic NEG, are passed through unmodified. This allows special
6952  // patterns for these operations to match, which will lower these constants
6953  // to whatever is proven necessary.
6954  BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
6955  if (BVN->isConstant())
6956  if (ConstantSDNode *Const = BVN->getConstantSplatNode()) {
6957  unsigned BitSize = VT.getVectorElementType().getSizeInBits();
6958  APInt Val(BitSize,
6959  Const->getAPIntValue().zextOrTrunc(BitSize).getZExtValue());
6960  if (Val.isNullValue() || Val.isAllOnesValue())
6961  return Op;
6962  }
6963  }
6964 
6965  if (SDValue V = ConstantBuildVector(Op, DAG))
6966  return V;
6967 
6968  // Scan through the operands to find some interesting properties we can
6969  // exploit:
6970  // 1) If only one value is used, we can use a DUP, or
6971  // 2) if only the low element is not undef, we can just insert that, or
6972  // 3) if only one constant value is used (w/ some non-constant lanes),
6973  // we can splat the constant value into the whole vector then fill
6974  // in the non-constant lanes.
6975  // 4) FIXME: If different constant values are used, but we can intelligently
6976  // select the values we'll be overwriting for the non-constant
6977  // lanes such that we can directly materialize the vector
6978  // some other way (MOVI, e.g.), we can be sneaky.
6979  // 5) if all operands are EXTRACT_VECTOR_ELT, check for VUZP.
6980  SDLoc dl(Op);
6981  unsigned NumElts = VT.getVectorNumElements();
6982  bool isOnlyLowElement = true;
6983  bool usesOnlyOneValue = true;
6984  bool usesOnlyOneConstantValue = true;
6985  bool isConstant = true;
6986  bool AllLanesExtractElt = true;
6987  unsigned NumConstantLanes = 0;
6988  SDValue Value;
6989  SDValue ConstantValue;
6990  for (unsigned i = 0; i < NumElts; ++i) {
6991  SDValue V = Op.getOperand(i);
6993  AllLanesExtractElt = false;
6994  if (V.isUndef())
6995  continue;
6996  if (i > 0)
6997  isOnlyLowElement = false;
6998  if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
6999  isConstant = false;
7000 
7001  if (isa<ConstantSDNode>(V) || isa<ConstantFPSDNode>(V)) {
7002  ++NumConstantLanes;
7003  if (!ConstantValue.getNode())
7004  ConstantValue = V;
7005  else if (ConstantValue != V)
7006  usesOnlyOneConstantValue = false;
7007  }
7008 
7009  if (!Value.getNode())
7010  Value = V;
7011  else if (V != Value)
7012  usesOnlyOneValue = false;
7013  }
7014 
7015  if (!Value.getNode()) {
7016  LLVM_DEBUG(
7017  dbgs() << "LowerBUILD_VECTOR: value undefined, creating undef node\n");
7018  return DAG.getUNDEF(VT);
7019  }
7020 
7021  if (isOnlyLowElement) {
7022  LLVM_DEBUG(dbgs() << "LowerBUILD_VECTOR: only low element used, creating 1 "
7023  "SCALAR_TO_VECTOR node\n");
7024  return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
7025  }
7026 
7027  if (AllLanesExtractElt) {
7028  SDNode *Vector = nullptr;
7029  bool Even = false;
7030  bool Odd = false;
7031  // Check whether the extract elements match the Even pattern <0,2,4,...> or
7032  // the Odd pattern <1,3,5,...>.
7033  for (unsigned i = 0; i < NumElts; ++i) {
7034  SDValue V = Op.getOperand(i);
7035  const SDNode *N = V.getNode();
7036  if (!isa<ConstantSDNode>(N->getOperand(1)))
7037  break;
7038  SDValue N0 = N->getOperand(0);
7039 
7040  // All elements are extracted from the same vector.
7041  if (!Vector) {
7042  Vector = N0.getNode();
7043  // Check that the type of EXTRACT_VECTOR_ELT matches the type of
7044  // BUILD_VECTOR.
7045  if (VT.getVectorElementType() !=
7047  break;
7048  } else if (Vector != N0.getNode()) {
7049  Odd = false;
7050  Even = false;
7051  break;
7052  }
7053 
7054  // Extracted values are either at Even indices <0,2,4,...> or at Odd
7055  // indices <1,3,5,...>.
7056  uint64_t Val = N->getConstantOperandVal(1);
7057  if (Val == 2 * i) {
7058  Even = true;
7059  continue;
7060  }
7061  if (Val - 1 == 2 * i) {
7062  Odd = true;
7063  continue;
7064  }
7065 
7066  // Something does not match: abort.
7067  Odd = false;
7068  Even = false;
7069  break;
7070  }
7071  if (Even || Odd) {
7072  SDValue LHS =
7073  DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SDValue(Vector, 0),
7074  DAG.getConstant(0, dl, MVT::i64));
7075  SDValue RHS =
7076  DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SDValue(Vector, 0),
7077  DAG.getConstant(NumElts, dl, MVT::i64));
7078 
7079  if (Even && !Odd)
7080  return DAG.getNode(AArch64ISD::UZP1, dl, DAG.getVTList(VT, VT), LHS,
7081  RHS);
7082  if (Odd && !Even)
7083  return DAG.getNode(AArch64ISD::UZP2, dl, DAG.getVTList(VT, VT), LHS,
7084  RHS);
7085  }
7086  }
7087 
7088  // Use DUP for non-constant splats. For f32 constant splats, reduce to
7089  // i32 and try again.
7090  if (usesOnlyOneValue) {
7091  if (!isConstant) {
7092  if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
7093  Value.getValueType() != VT) {
7094  LLVM_DEBUG(
7095  dbgs() << "LowerBUILD_VECTOR: use DUP for non-constant splats\n");
7096  return DAG.getNode(AArch64ISD::DUP, dl, VT, Value);
7097  }
7098 
7099  // This is actually a DUPLANExx operation, which keeps everything vectory.
7100 
7101  SDValue Lane = Value.getOperand(1);
7102  Value = Value.getOperand(0);
7103  if (Value.getValueSizeInBits() == 64) {
7104  LLVM_DEBUG(
7105  dbgs() << "LowerBUILD_VECTOR: DUPLANE works on 128-bit vectors, "
7106  "widening it\n");
7107  Value = WidenVector(Value, DAG);
7108  }
7109 
7110  unsigned Opcode = getDUPLANEOp(VT.getVectorElementType());
7111  return DAG.getNode(Opcode, dl, VT, Value, Lane);
7112  }
7113 
7116  EVT EltTy = VT.getVectorElementType();
7117  assert ((EltTy == MVT::f16 || EltTy == MVT::f32 || EltTy == MVT::f64) &&
7118  "Unsupported floating-point vector type");
7119  LLVM_DEBUG(
7120  dbgs() << "LowerBUILD_VECTOR: float constant splats, creating int "
7121  "BITCASTS, and try again\n");
7122  MVT NewType = MVT::getIntegerVT(EltTy.getSizeInBits());
7123  for (unsigned i = 0; i < NumElts; ++i)
7124  Ops.push_back(DAG.getNode(ISD::BITCAST, dl, NewType, Op.getOperand(i)));
7125  EVT VecVT = EVT::getVectorVT(*DAG.getContext(), NewType, NumElts);
7126  SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
7127  LLVM_DEBUG(dbgs() << "LowerBUILD_VECTOR: trying to lower new vector: ";
7128  Val.dump(););
7129  Val = LowerBUILD_VECTOR(Val, DAG);
7130  if (Val.getNode())
7131  return DAG.getNode(ISD::BITCAST, dl, VT, Val);
7132  }
7133  }
7134 
7135  // If there was only one constant value used and for more than one lane,
7136  // start by splatting that value, then replace the non-constant lanes. This
7137  // is better than the default, which will perform a separate initialization
7138  // for each lane.
7139  if (NumConstantLanes > 0 && usesOnlyOneConstantValue) {
7140  // Firstly, try to materialize the splat constant.
7141  SDValue Vec = DAG.getSplatBuildVector(VT, dl, ConstantValue),
7142  Val = ConstantBuildVector(Vec, DAG);
7143  if (!Val) {
7144  // Otherwise, materialize the constant and splat it.
7145  Val = DAG.getNode(AArch64ISD::DUP, dl, VT, ConstantValue);
7146  DAG.ReplaceAllUsesWith(Vec.getNode(), &Val);
7147  }
7148 
7149  // Now insert the non-constant lanes.
7150  for (unsigned i = 0; i < NumElts; ++i) {
7151  SDValue V = Op.getOperand(i);
7152  SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i64);
7153  if (!isa<ConstantSDNode>(V) && !isa<ConstantFPSDNode>(V))
7154  // Note that type legalization likely mucked about with the VT of the
7155  // source operand, so we may have to convert it here before inserting.
7156  Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, V, LaneIdx);
7157  }
7158  return Val;
7159  }
7160 
7161  // This will generate a load from the constant pool.
7162  if (isConstant) {
7163  LLVM_DEBUG(
7164  dbgs() << "LowerBUILD_VECTOR: all elements are constant, use default "
7165  "expansion\n");
7166  return SDValue();
7167  }
7168 
7169  // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
7170  if (NumElts >= 4) {
7171  if (SDValue shuffle = ReconstructShuffle(Op, DAG))
7172  return shuffle;
7173  }
7174 
7175  // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
7176  // know the default expansion would otherwise fall back on something even
7177  // worse. For a vector with one or two non-undef values, that's
7178  // scalar_to_vector for the elements followed by a shuffle (provided the
7179  // shuffle is valid for the target) and materialization element by element
7180  // on the stack followed by a load for everything else.
7181  if (!isConstant && !usesOnlyOneValue) {
7182  LLVM_DEBUG(
7183  dbgs() << "LowerBUILD_VECTOR: alternatives failed, creating sequence "
7184  "of INSERT_VECTOR_ELT\n");
7185 
7186  SDValue Vec = DAG.getUNDEF(VT);
7187  SDValue Op0 = Op.getOperand(0);
7188  unsigned i = 0;
7189 
7190  // Use SCALAR_TO_VECTOR for lane zero to
7191  // a) Avoid a RMW dependency on the full vector register, and
7192  // b) Allow the register coalescer to fold away the copy if the
7193  // value is already in an S or D register, and we're forced to emit an
7194  // INSERT_SUBREG that we can't fold anywhere.
7195  //
7196  // We also allow types like i8 and i16 which are illegal scalar but legal
7197  // vector element types. After type-legalization the inserted value is
7198  // extended (i32) and it is safe to cast them to the vector type by ignoring
7199  // the upper bits of the lowest lane (e.g. v8i8, v4i16).
7200  if (!Op0.isUndef()) {
7201  LLVM_DEBUG(dbgs() << "Creating node for op0, it is not undefined:\n");
7202  Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op0);
7203  ++i;
7204  }
7205  LLVM_DEBUG(if (i < NumElts) dbgs()
7206  << "Creating nodes for the other vector elements:\n";);
7207  for (; i < NumElts; ++i) {
7208  SDValue V = Op.getOperand(i);
7209  if (V.isUndef())
7210  continue;
7211  SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i64);
7212  Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
7213  }
7214  return Vec;
7215  }
7216 
7217  LLVM_DEBUG(
7218  dbgs() << "LowerBUILD_VECTOR: use default expansion, failed to find "
7219  "better alternative\n");
7220  return SDValue();
7221 }
7222 
7223 SDValue AArch64TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
7224  SelectionDAG &DAG) const {
7225  assert(Op.getOpcode() == ISD::INSERT_VECTOR_ELT && "Unknown opcode!");
7226 
7227  // Check for non-constant or out of range lane.
7228  EVT VT = Op.getOperand(0).getValueType();
7230  if (!CI || CI->getZExtValue() >= VT.getVectorNumElements())
7231  return SDValue();
7232 
7233 
7234  // Insertion/extraction are legal for V128 types.
7235  if (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32 ||
7236  VT == MVT::v2i64 || VT == MVT::v4f32 || VT == MVT::v2f64 ||
7237  VT == MVT::v8f16)
7238  return Op;
7239 
7240  if (VT != MVT::v8i8 && VT != MVT::v4i16 && VT != MVT::v2i32 &&
7241  VT != MVT::v1i64 && VT != MVT::v2f32 && VT != MVT::v4f16)
7242  return SDValue();
7243 
7244  // For V64 types, we perform insertion by expanding the value
7245  // to a V128 type and perform the insertion on that.
7246  SDLoc DL(Op);
7247  SDValue WideVec = WidenVector(Op.getOperand(0), DAG);
7248  EVT WideTy = WideVec.getValueType();
7249 
7250  SDValue Node = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, WideTy, WideVec,
7251  Op.getOperand(1), Op.getOperand(2));
7252  // Re-narrow the resultant vector.
7253  return NarrowVector(Node, DAG);
7254 }
7255 
7256 SDValue
7257 AArch64TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7258  SelectionDAG &DAG) const {
7259  assert(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && "Unknown opcode!");
7260 
7261  // Check for non-constant or out of range lane.
7262  EVT VT = Op.getOperand(0).getValueType();
7264  if (!CI || CI->getZExtValue() >= VT.getVectorNumElements())
7265  return SDValue();
7266 
7267 
7268  // Insertion/extraction are legal for V128 types.
7269  if (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32 ||
7270  VT == MVT::v2i64 || VT == MVT::v4f32 || VT == MVT::v2f64 ||
7271  VT == MVT::v8f16)
7272  return Op;
7273 
7274  if (VT != MVT::v8i8 && VT != MVT::v4i16 && VT != MVT::v2i32 &&
7275  VT != MVT::v1i64 && VT != MVT::v2f32 && VT != MVT::v4f16)
7276  return SDValue();
7277 
7278  // For V64 types, we perform extraction by expanding the value
7279  // to a V128 type and perform the extraction on that.
7280  SDLoc DL(Op);
7281  SDValue WideVec = WidenVector(Op.getOperand(0), DAG);
7282  EVT WideTy = WideVec.getValueType();
7283 
7284  EVT ExtrTy = WideTy.getVectorElementType();
7285  if (ExtrTy == MVT::i16 || ExtrTy == MVT::i8)
7286  ExtrTy = MVT::i32;
7287 
7288  // For extractions, we just return the result directly.
7289  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ExtrTy, WideVec,
7290  Op.getOperand(1));
7291 }
7292 
7293 SDValue AArch64TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
7294  SelectionDAG &DAG) const {
7295  EVT VT = Op.getOperand(0).getValueType();
7296  SDLoc dl(Op);
7297  // Just in case...
7298  if (!VT.isVector())
7299  return SDValue();
7300 
7302  if (!Cst)
7303  return SDValue();
7304  unsigned Val = Cst->getZExtValue();
7305 
7306  unsigned Size = Op.getValueSizeInBits();
7307 
7308  // This will get lowered to an appropriate EXTRACT_SUBREG in ISel.
7309  if (Val == 0)
7310  return Op;
7311 
7312  // If this is extracting the upper 64-bits of a 128-bit vector, we match
7313  // that directly.
7314  if (Size == 64 && Val * VT.getScalarSizeInBits() == 64)
7315  return Op;
7316 
7317  return SDValue();
7318 }
7319 
7321  if (VT.getVectorNumElements() == 4 &&
7322  (VT.is128BitVector() || VT.is64BitVector())) {
7323  unsigned PFIndexes[4];
7324  for (unsigned i = 0; i != 4; ++i) {
7325  if (M[i] < 0)
7326  PFIndexes[i] = 8;
7327  else
7328  PFIndexes[i] = M[i];
7329  }
7330 
7331  // Compute the index in the perfect shuffle table.
7332  unsigned PFTableIndex = PFIndexes[0] * 9 * 9 * 9 + PFIndexes[1] * 9 * 9 +
7333  PFIndexes[2] * 9 + PFIndexes[3];
7334  unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7335  unsigned Cost = (PFEntry >> 30);
7336 
7337  if (Cost <= 4)
7338  return true;
7339  }
7340 
7341  bool DummyBool;
7342  int DummyInt;
7343  unsigned DummyUnsigned;
7344 
7345  return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) || isREVMask(M, VT, 64) ||
7346  isREVMask(M, VT, 32) || isREVMask(M, VT, 16) ||
7347  isEXTMask(M, VT, DummyBool, DummyUnsigned) ||
7348  // isTBLMask(M, VT) || // FIXME: Port TBL support from ARM.
7349  isTRNMask(M, VT, DummyUnsigned) || isUZPMask(M, VT, DummyUnsigned) ||
7350  isZIPMask(M, VT, DummyUnsigned) ||
7351  isTRN_v_undef_Mask(M, VT, DummyUnsigned) ||
7352  isUZP_v_undef_Mask(M, VT, DummyUnsigned) ||
7353  isZIP_v_undef_Mask(M, VT, DummyUnsigned) ||
7354  isINSMask(M, VT.getVectorNumElements(), DummyBool, DummyInt) ||
7355  isConcatMask(M, VT, VT.getSizeInBits() == 128));
7356 }
7357 
7358 /// getVShiftImm - Check if this is a valid build_vector for the immediate
7359 /// operand of a vector shift operation, where all the elements of the
7360 /// build_vector must have the same constant integer value.
7361 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7362  // Ignore bit_converts.
7363  while (Op.getOpcode() == ISD::BITCAST)
7364  Op = Op.getOperand(0);
7366  APInt SplatBits, SplatUndef;
7367  unsigned SplatBitSize;
7368  bool HasAnyUndefs;
7369  if (!BVN || !BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7370  HasAnyUndefs, ElementBits) ||
7371  SplatBitSize > ElementBits)
7372  return false;
7373  Cnt = SplatBits.getSExtValue();
7374  return true;
7375 }
7376 
7377 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
7378 /// operand of a vector shift left operation. That value must be in the range:
7379 /// 0 <= Value < ElementBits for a left shift; or
7380 /// 0 <= Value <= ElementBits for a long left shift.
7381 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
7382  assert(VT.isVector() && "vector shift count is not a vector type");
7383  int64_t ElementBits = VT.getScalarSizeInBits();
7384  if (!getVShiftImm(Op, ElementBits, Cnt))
7385  return false;
7386  return (Cnt >= 0 && (isLong ? Cnt - 1 : Cnt) < ElementBits);
7387 }
7388 
7389 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
7390 /// operand of a vector shift right operation. The value must be in the range:
7391 /// 1 <= Value <= ElementBits for a right shift; or
7392 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, int64_t &Cnt) {
7393  assert(VT.isVector() && "vector shift count is not a vector type");
7394  int64_t ElementBits = VT.getScalarSizeInBits();
7395  if (!getVShiftImm(Op, ElementBits, Cnt))
7396  return false;
7397  return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits / 2 : ElementBits));
7398 }
7399 
7400 SDValue AArch64TargetLowering::LowerVectorSRA_SRL_SHL(SDValue Op,
7401  SelectionDAG &DAG) const {
7402  EVT VT = Op.getValueType();
7403  SDLoc DL(Op);
7404  int64_t Cnt;
7405 
7406  if (!Op.getOperand(1).getValueType().isVector())
7407  return Op;
7408  unsigned EltSize = VT.getScalarSizeInBits();
7409 
7410  switch (Op.getOpcode()) {
7411  default:
7412  llvm_unreachable("unexpected shift opcode");
7413 
7414  case ISD::SHL:
7415  if (isVShiftLImm(Op.getOperand(1), VT, false, Cnt) && Cnt < EltSize)
7416  return DAG.getNode(AArch64ISD::VSHL, DL, VT, Op.getOperand(0),
7417  DAG.getConstant(Cnt, DL, MVT::i32));
7418  return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
7419  DAG.getConstant(Intrinsic::aarch64_neon_ushl, DL,
7420  MVT::i32),
7421  Op.getOperand(0), Op.getOperand(1));
7422  case ISD::SRA:
7423  case ISD::SRL:
7424  // Right shift immediate
7425  if (isVShiftRImm(Op.getOperand(1), VT, false, Cnt) && Cnt < EltSize) {
7426  unsigned Opc =
7428  return DAG.getNode(Opc, DL, VT, Op.getOperand(0),
7429  DAG.getConstant(Cnt, DL, MVT::i32));
7430  }
7431 
7432  // Right shift register. Note, there is not a shift right register
7433  // instruction, but the shift left register instruction takes a signed
7434  // value, where negative numbers specify a right shift.
7435  unsigned Opc = (Op.getOpcode() == ISD::SRA) ? Intrinsic::aarch64_neon_sshl
7436  : Intrinsic::aarch64_neon_ushl;
7437  // negate the shift amount
7438  SDValue NegShift = DAG.getNode(AArch64ISD::NEG, DL, VT, Op.getOperand(1));
7439  SDValue NegShiftLeft =
7440  DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
7441  DAG.getConstant(Opc, DL, MVT::i32), Op.getOperand(0),
7442  NegShift);
7443  return NegShiftLeft;
7444  }
7445 
7446  return SDValue();
7447 }
7448 
7450  AArch64CC::CondCode CC, bool NoNans, EVT VT,
7451  const SDLoc &dl, SelectionDAG &DAG) {
7452  EVT SrcVT = LHS.getValueType();
7453  assert(VT.getSizeInBits() == SrcVT.getSizeInBits() &&
7454  "function only supposed to emit natural comparisons");
7455 
7457  APInt CnstBits(VT.getSizeInBits(), 0);
7458  APInt UndefBits(VT.getSizeInBits(), 0);
7459  bool IsCnst = BVN && resolveBuildVector(BVN, CnstBits, UndefBits);
7460  bool IsZero = IsCnst && (CnstBits == 0);
7461 
7462  if (SrcVT.getVectorElementType().isFloatingPoint()) {
7463  switch (CC) {
7464  default:
7465  return SDValue();
7466  case AArch64CC::NE: {
7467  SDValue Fcmeq;
7468  if (IsZero)
7469  Fcmeq = DAG.getNode(AArch64ISD::FCMEQz, dl, VT, LHS);
7470  else
7471  Fcmeq = DAG.getNode(AArch64ISD::FCMEQ, dl, VT, LHS, RHS);
7472  return DAG.getNode(AArch64ISD::NOT, dl, VT, Fcmeq);
7473  }
7474  case AArch64CC::EQ:
7475  if (IsZero)
7476  return DAG.getNode(AArch64ISD::FCMEQz, dl, VT, LHS);
7477  return DAG.getNode(AArch64ISD::FCMEQ, dl, VT, LHS, RHS);
7478  case AArch64CC::GE:
7479  if (IsZero)
7480  return DAG.getNode(AArch64ISD::FCMGEz, dl, VT, LHS);
7481  return DAG.getNode(AArch64ISD::FCMGE, dl, VT, LHS, RHS);
7482  case AArch64CC::GT:
7483  if (IsZero)
7484  return DAG.getNode(AArch64ISD::FCMGTz, dl, VT, LHS);
7485  return DAG.getNode(AArch64ISD::FCMGT, dl, VT, LHS, RHS);
7486  case AArch64CC::LS:
7487  if (IsZero)
7488  return DAG.getNode(AArch64ISD::FCMLEz, dl, VT, LHS);
7489  return DAG.getNode(AArch64ISD::FCMGE, dl, VT, RHS, LHS);
7490  case AArch64CC::LT:
7491  if (!NoNans)
7492  return SDValue();
7493  // If we ignore NaNs then we can use to the MI implementation.
7495  case AArch64CC::MI:
7496  if (IsZero)
7497  return DAG.getNode(AArch64ISD::FCMLTz, dl, VT, LHS);
7498  return DAG.getNode(AArch64ISD::FCMGT, dl, VT, RHS, LHS);
7499  }
7500  }
7501 
7502  switch (CC) {
7503  default:
7504  return SDValue();
7505  case AArch64CC::NE: {
7506  SDValue Cmeq;
7507  if (IsZero)
7508  Cmeq = DAG.getNode(AArch64ISD::CMEQz, dl, VT, LHS);
7509  else
7510  Cmeq = DAG.getNode(AArch64ISD::CMEQ, dl, VT, LHS, RHS);
7511  return DAG.getNode(AArch64ISD::NOT, dl, VT, Cmeq);
7512  }
7513  case AArch64CC::EQ:
7514  if (IsZero)
7515  return DAG.getNode(AArch64ISD::CMEQz, dl, VT, LHS);
7516  return DAG.getNode(AArch64ISD::CMEQ, dl, VT, LHS, RHS);
7517  case AArch64CC::GE:
7518  if (IsZero)
7519  return DAG.getNode(AArch64ISD::CMGEz, dl, VT, LHS);
7520  return DAG.getNode(AArch64ISD::CMGE, dl, VT, LHS, RHS);
7521  case AArch64CC::GT:
7522  if (IsZero)
7523  return DAG.getNode(AArch64ISD::CMGTz, dl, VT, LHS);
7524  return DAG.getNode(AArch64ISD::CMGT, dl, VT, LHS, RHS);
7525  case AArch64CC::LE:
7526  if (IsZero)
7527  return DAG.getNode(AArch64ISD::CMLEz, dl, VT, LHS);
7528  return DAG.getNode(AArch64ISD::CMGE, dl, VT, RHS, LHS);
7529  case AArch64CC::LS:
7530  return DAG.getNode(AArch64ISD::CMHS, dl, VT, RHS, LHS);
7531  case AArch64CC::LO:
7532  return DAG.getNode(AArch64ISD::CMHI, dl, VT, RHS, LHS);
7533  case AArch64CC::LT:
7534  if (IsZero)
7535  return DAG.getNode(AArch64ISD::CMLTz, dl, VT, LHS);
7536  return DAG.getNode(AArch64ISD::CMGT, dl, VT, RHS, LHS);
7537  case AArch64CC::HI:
7538  return DAG.getNode(AArch64ISD::CMHI, dl, VT, LHS, RHS);
7539  case AArch64CC::HS:
7540  return DAG.getNode(AArch64ISD::CMHS, dl, VT, LHS, RHS);
7541  }
7542 }
7543 
7544 SDValue AArch64TargetLowering::LowerVSETCC(SDValue Op,
7545  SelectionDAG &DAG) const {
7546  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7547  SDValue LHS = Op.getOperand(0);
7548  SDValue RHS = Op.getOperand(1);
7550  SDLoc dl(Op);
7551 
7553  assert(LHS.getValueType() == RHS.getValueType());
7555  SDValue Cmp =
7556  EmitVectorComparison(LHS, RHS, AArch64CC, false, CmpVT, dl, DAG);
7557  return DAG.getSExtOrTrunc(Cmp, dl, Op.getValueType());
7558  }
7559 
7560  const bool FullFP16 =
7561  static_cast<const AArch64Subtarget &>(DAG.getSubtarget()).hasFullFP16();
7562 
7563  // Make v4f16 (only) fcmp operations utilise vector instructions
7564  // v8f16 support will be a litle more complicated
7565  if (LHS.getValueType().getVectorElementType() == MVT::f16) {
7566  if (!FullFP16 && LHS.getValueType().getVectorNumElements() == 4) {
7567  LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v4f32, LHS);
7568  RHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v4f32, RHS);
7569  SDValue NewSetcc = DAG.getSetCC(dl, MVT::v4i16, LHS, RHS, CC);
7570  DAG.ReplaceAllUsesWith(Op, NewSetcc);
7571  CmpVT = MVT::v4i32;
7572  } else
7573  return SDValue();
7574  }
7575 
7578 
7579  // Unfortunately, the mapping of LLVM FP CC's onto AArch64 CC's isn't totally
7580  // clean. Some of them require two branches to implement.
7581  AArch64CC::CondCode CC1, CC2;
7582  bool ShouldInvert;
7583  changeVectorFPCCToAArch64CC(CC, CC1, CC2, ShouldInvert);
7584 
7586  SDValue Cmp =
7587  EmitVectorComparison(LHS, RHS, CC1, NoNaNs, CmpVT, dl, DAG);
7588  if (!Cmp.getNode())
7589  return SDValue();
7590 
7591  if (CC2 != AArch64CC::AL) {
7592  SDValue Cmp2 =
7593  EmitVectorComparison(LHS, RHS, CC2, NoNaNs, CmpVT, dl, DAG);
7594  if (!Cmp2.getNode())
7595  return SDValue();
7596 
7597  Cmp = DAG.getNode(ISD::OR, dl, CmpVT, Cmp, Cmp2);
7598  }
7599 
7600  Cmp = DAG.getSExtOrTrunc(Cmp, dl, Op.getValueType());
7601 
7602  if (ShouldInvert)
7603  return Cmp = DAG.getNOT(dl, Cmp, Cmp.getValueType());
7604 
7605  return Cmp;
7606 }
7607 
7608 static SDValue getReductionSDNode(unsigned Op, SDLoc DL, SDValue ScalarOp,
7609  SelectionDAG &DAG) {
7610  SDValue VecOp = ScalarOp.getOperand(0);
7611  auto Rdx = DAG.getNode(Op, DL, VecOp.getSimpleValueType(), VecOp);
7612  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ScalarOp.getValueType(), Rdx,
7613  DAG.getConstant(0, DL, MVT::i64));
7614 }
7615 
7616 SDValue AArch64TargetLowering::LowerVECREDUCE(SDValue Op,
7617  SelectionDAG &DAG) const {
7618  SDLoc dl(Op);
7619  switch (Op.getOpcode()) {
7620  case ISD::VECREDUCE_ADD:
7621  return getReductionSDNode(AArch64ISD::UADDV, dl, Op, DAG);
7622  case ISD::VECREDUCE_SMAX:
7623  return getReductionSDNode(AArch64ISD::SMAXV, dl, Op, DAG);
7624  case ISD::VECREDUCE_SMIN:
7625  return getReductionSDNode(AArch64ISD::SMINV, dl, Op, DAG);
7626  case ISD::VECREDUCE_UMAX:
7627  return getReductionSDNode(AArch64ISD::UMAXV, dl, Op, DAG);
7628  case ISD::VECREDUCE_UMIN:
7629  return getReductionSDNode(AArch64ISD::UMINV, dl, Op, DAG);
7630  case ISD::VECREDUCE_FMAX: {
7631  assert(Op->getFlags().hasNoNaNs() && "fmax vector reduction needs NoNaN flag");
7632  return DAG.getNode(
7634  DAG.getConstant(Intrinsic::aarch64_neon_fmaxnmv, dl, MVT::i32),
7635  Op.getOperand(0));
7636  }
7637  case ISD::VECREDUCE_FMIN: {
7638  assert(Op->getFlags().hasNoNaNs() && "fmin vector reduction needs NoNaN flag");
7639  return DAG.getNode(
7641  DAG.getConstant(Intrinsic::aarch64_neon_fminnmv, dl, MVT::i32),
7642  Op.getOperand(0));
7643  }
7644  default:
7645  llvm_unreachable("Unhandled reduction");
7646  }
7647 }
7648 
7649 SDValue AArch64TargetLowering::LowerATOMIC_LOAD_SUB(SDValue Op,
7650  SelectionDAG &DAG) const {
7651  auto &Subtarget = static_cast<const AArch64Subtarget &>(DAG.getSubtarget());
7652  if (!Subtarget.hasLSE())
7653  return SDValue();
7654 
7655  // LSE has an atomic load-add instruction, but not a load-sub.
7656  SDLoc dl(Op);
7657  MVT VT = Op.getSimpleValueType();
7658  SDValue RHS = Op.getOperand(2);
7659  AtomicSDNode *AN = cast<AtomicSDNode>(Op.getNode());
7660  RHS = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, dl, VT), RHS);
7661  return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl, AN->getMemoryVT(),
7662  Op.getOperand(0), Op.getOperand(1), RHS,
7663  AN->getMemOperand());
7664 }
7665 
7666 SDValue AArch64TargetLowering::LowerATOMIC_LOAD_AND(SDValue Op,
7667  SelectionDAG &DAG) const {
7668  auto &Subtarget = static_cast<const AArch64Subtarget &>(DAG.getSubtarget());
7669  if (!Subtarget.hasLSE())
7670  return SDValue();
7671 
7672  // LSE has an atomic load-clear instruction, but not a load-and.
7673  SDLoc dl(Op);
7674  MVT VT = Op.getSimpleValueType();
7675  SDValue RHS = Op.getOperand(2);
7676  AtomicSDNode *AN = cast<AtomicSDNode>(Op.getNode());
7677  RHS = DAG.getNode(ISD::XOR, dl, VT, DAG.getConstant(-1ULL, dl, VT), RHS);
7678  return DAG.getAtomic(ISD::ATOMIC_LOAD_CLR, dl, AN->getMemoryVT(),
7679  Op.getOperand(0), Op.getOperand(1), RHS,
7680  AN->getMemOperand());
7681 }
7682 
7683 SDValue AArch64TargetLowering::LowerWindowsDYNAMIC_STACKALLOC(
7684  SDValue Op, SDValue Chain, SDValue &Size, SelectionDAG &DAG) const {
7685  SDLoc dl(Op);
7686  EVT PtrVT = getPointerTy(DAG.getDataLayout());
7687  SDValue Callee = DAG.getTargetExternalSymbol("__chkstk", PtrVT, 0);
7688 
7689  const uint32_t *Mask =
7690  Subtarget->getRegisterInfo()->getWindowsStackProbePreservedMask();
7691 
7692  Size = DAG.getNode(ISD::SRL, dl, MVT::i64, Size,
7693  DAG.getConstant(4, dl, MVT::i64));
7694  Chain = DAG.getCopyToReg(Chain, dl, AArch64::X15, Size, SDValue());
7695  Chain =
7697  Chain, Callee, DAG.getRegister(AArch64::X15, MVT::i64),
7698  DAG.getRegisterMask(Mask), Chain.getValue(1));
7699  // To match the actual intent better, we should read the output from X15 here
7700  // again (instead of potentially spilling it to the stack), but rereading Size
7701  // from X15 here doesn't work at -O0, since it thinks that X15 is undefined
7702  // here.
7703 
7704  Size = DAG.getNode(ISD::SHL, dl, MVT::i64, Size,
7705  DAG.getConstant(4, dl, MVT::i64));
7706  return Chain;
7707 }
7708 
7709 SDValue
7710 AArch64TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
7711  SelectionDAG &DAG) const {
7712  assert(Subtarget->isTargetWindows() &&
7713  "Only Windows alloca probing supported");
7714  SDLoc dl(Op);
7715  // Get the inputs.
7716  SDNode *Node = Op.getNode();
7717  SDValue Chain = Op.getOperand(0);
7718  SDValue Size = Op.getOperand(1);
7719  unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
7720  EVT VT = Node->getValueType(0);
7721 
7723  "no-stack-arg-probe")) {
7724  SDValue SP = DAG.getCopyFromReg(Chain, dl, AArch64::SP, MVT::i64);
7725  Chain = SP.getValue(1);
7726  SP = DAG.getNode(ISD::SUB, dl, MVT::i64, SP, Size);
7727  if (Align)
7728  SP = DAG.getNode(ISD::AND, dl, VT, SP.getValue(0),
7729  DAG.getConstant(-(uint64_t)Align, dl, VT));
7730  Chain = DAG.getCopyToReg(Chain, dl, AArch64::SP, SP);
7731  SDValue Ops[2] = {SP, Chain};
7732  return DAG.getMergeValues(Ops, dl);
7733  }
7734 
7735  Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
7736 
7737  Chain = LowerWindowsDYNAMIC_STACKALLOC(Op, Chain, Size, DAG);
7738 
7739  SDValue SP = DAG.getCopyFromReg(Chain, dl, AArch64::SP, MVT::i64);
7740  Chain = SP.getValue(1);
7741  SP = DAG.getNode(ISD::SUB, dl, MVT::i64, SP, Size);
7742  if (Align)
7743  SP = DAG.getNode(ISD::AND, dl, VT, SP.getValue(0),
7744  DAG.getConstant(-(uint64_t)Align, dl, VT));
7745  Chain = DAG.getCopyToReg(Chain, dl, AArch64::SP, SP);
7746 
7747  Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true),
7748  DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
7749 
7750  SDValue Ops[2] = {SP, Chain};
7751  return DAG.getMergeValues(Ops, dl);
7752 }
7753 
7754 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
7755 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7756 /// specified in the intrinsic calls.
7758  const CallInst &I,
7759  MachineFunction &MF,
7760  unsigned Intrinsic) const {
7761  auto &DL = I.getModule()->getDataLayout();
7762  switch (Intrinsic) {
7763  case Intrinsic::aarch64_neon_ld2:
7764  case Intrinsic::aarch64_neon_ld3:
7765  case Intrinsic::aarch64_neon_ld4:
7766  case Intrinsic::aarch64_neon_ld1x2:
7767  case Intrinsic::aarch64_neon_ld1x3:
7768  case Intrinsic::aarch64_neon_ld1x4:
7769  case Intrinsic::aarch64_neon_ld2lane:
7770  case Intrinsic::aarch64_neon_ld3lane:
7771  case Intrinsic::aarch64_neon_ld4lane:
7772  case Intrinsic::aarch64_neon_ld2r:
7773  case Intrinsic::aarch64_neon_ld3r:
7774  case Intrinsic::aarch64_neon_ld4r: {
7775  Info.opc = ISD::INTRINSIC_W_CHAIN;
7776  // Conservatively set memVT to the entire set of vectors loaded.
7777  uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
7778  Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7779  Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1);
7780  Info.offset = 0;
7781  Info.align = 0;
7782  // volatile loads with NEON intrinsics not supported
7784  return true;
7785  }
7786  case Intrinsic::aarch64_neon_st2:
7787  case Intrinsic::aarch64_neon_st3:
7788  case Intrinsic::aarch64_neon_st4:
7789  case Intrinsic::aarch64_neon_st1x2:
7790  case Intrinsic::aarch64_neon_st1x3:
7791  case Intrinsic::aarch64_neon_st1x4:
7792  case Intrinsic::aarch64_neon_st2lane:
7793  case Intrinsic::aarch64_neon_st3lane:
7794  case Intrinsic::aarch64_neon_st4lane: {
7795  Info.opc = ISD::INTRINSIC_VOID;
7796  // Conservatively set memVT to the entire set of vectors stored.
7797  unsigned NumElts = 0;
7798  for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7799  Type *ArgTy = I.getArgOperand(ArgI)->getType();
7800  if (!ArgTy->isVectorTy())
7801  break;
7802  NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
7803  }
7804  Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7805  Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1);
7806  Info.offset = 0;
7807  Info.align = 0;
7808  // volatile stores with NEON intrinsics not supported
7810  return true;
7811  }
7812  case Intrinsic::aarch64_ldaxr:
7813  case Intrinsic::aarch64_ldxr: {
7814  PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
7815  Info.opc = ISD::INTRINSIC_W_CHAIN;
7816  Info.memVT = MVT::getVT(PtrTy->getElementType());
7817  Info.ptrVal = I.getArgOperand(0);
7818  Info.offset = 0;
7819  Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
7821  return true;
7822  }
7823  case Intrinsic::aarch64_stlxr:
7824  case Intrinsic::aarch64_stxr: {
7825  PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
7826  Info.opc = ISD::INTRINSIC_W_CHAIN;
7827  Info.memVT = MVT::getVT(PtrTy->getElementType());
7828  Info.ptrVal = I.getArgOperand(1);
7829  Info.offset = 0;
7830  Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
7832  return true;
7833  }
7834  case Intrinsic::aarch64_ldaxp:
7835  case Intrinsic::aarch64_ldxp:
7836  Info.opc = ISD::INTRINSIC_W_CHAIN;
7837  Info.memVT = MVT::i128;
7838  Info.ptrVal = I.getArgOperand(0);
7839  Info.offset = 0;
7840  Info.align = 16;
7842  return true;
7843  case Intrinsic::aarch64_stlxp:
7844  case Intrinsic::aarch64_stxp:
7845  Info.opc = ISD::INTRINSIC_W_CHAIN;
7846  Info.memVT = MVT::i128;
7847  Info.ptrVal = I.getArgOperand(2);
7848  Info.offset = 0;
7849  Info.align = 16;
7851  return true;
7852  default:
7853  break;
7854  }
7855 
7856  return false;
7857 }
7858 
7860  ISD::LoadExtType ExtTy,
7861  EVT NewVT) const {
7862  // If we're reducing the load width in order to avoid having to use an extra
7863  // instruction to do extension then it's probably a good idea.
7864  if (ExtTy != ISD::NON_EXTLOAD)
7865  return true;
7866  // Don't reduce load width if it would prevent us from combining a shift into
7867  // the offset.
7868  MemSDNode *Mem = dyn_cast<MemSDNode>(Load);
7869  assert(Mem);
7870  const SDValue &Base = Mem->getBasePtr();
7871  if (Base.getOpcode() == ISD::ADD &&
7872  Base.getOperand(1).getOpcode() == ISD::SHL &&
7873  Base.getOperand(1).hasOneUse() &&
7874  Base.getOperand(1).getOperand(1).getOpcode() == ISD::Constant) {
7875  // The shift can be combined if it matches the size of the value being
7876  // loaded (and so reducing the width would make it not match).
7877  uint64_t ShiftAmount = Base.getOperand(1).getConstantOperandVal(1);
7878  uint64_t LoadBytes = Mem->getMemoryVT().getSizeInBits()/8;
7879  if (ShiftAmount == Log2_32(LoadBytes))
7880  return false;
7881  }
7882  // We have no reason to disallow reducing the load width, so allow it.
7883  return true;
7884 }
7885 
7886 // Truncations from 64-bit GPR to 32-bit GPR is free.
7888  if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
7889  return false;
7890  unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7891  unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
7892  return NumBits1 > NumBits2;
7893 }
7895  if (VT1.isVector() || VT2.isVector() || !VT1.isInteger() || !VT2.isInteger())
7896  return false;
7897  unsigned NumBits1 = VT1.getSizeInBits();
7898  unsigned NumBits2 = VT2.getSizeInBits();
7899  return NumBits1 > NumBits2;
7900 }
7901 
7902 /// Check if it is profitable to hoist instruction in then/else to if.
7903 /// Not profitable if I and it's user can form a FMA instruction
7904 /// because we prefer FMSUB/FMADD.
7906  if (I->getOpcode() != Instruction::FMul)
7907  return true;
7908 
7909  if (!I->hasOneUse())
7910  return true;
7911 
7912  Instruction *User = I->user_back();
7913 
7914  if (User &&
7915  !(User->getOpcode() == Instruction::FSub ||
7916  User->getOpcode() == Instruction::FAdd))
7917  return true;
7918 
7919  const TargetOptions &Options = getTargetMachine().Options;
7920  const DataLayout &DL = I->getModule()->getDataLayout();
7921  EVT VT = getValueType(DL, User->getOperand(0)->getType());
7922 
7923  return !(isFMAFasterThanFMulAndFAdd(VT) &&
7925  (Options.AllowFPOpFusion == FPOpFusion::Fast ||
7926  Options.UnsafeFPMath));
7927 }
7928 
7929 // All 32-bit GPR operations implicitly zero the high-half of the corresponding
7930 // 64-bit GPR.
7932  if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
7933  return false;
7934  unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7935  unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
7936  return NumBits1 == 32 && NumBits2 == 64;
7937 }
7939  if (VT1.isVector() || VT2.isVector() || !VT1.isInteger() || !VT2.isInteger())
7940  return false;
7941  unsigned NumBits1 = VT1.getSizeInBits();
7942  unsigned NumBits2 = VT2.getSizeInBits();
7943  return NumBits1 == 32 && NumBits2 == 64;
7944 }
7945 
7947  EVT VT1 = Val.getValueType();
7948  if (isZExtFree(VT1, VT2)) {
7949  return true;
7950  }
7951 
7952  if (Val.getOpcode() != ISD::LOAD)
7953  return false;
7954 
7955  // 8-, 16-, and 32-bit integer loads all implicitly zero-extend.
7956  return (VT1.isSimple() && !VT1.isVector() && VT1.isInteger() &&
7957  VT2.isSimple() && !VT2.isVector() && VT2.isInteger() &&
7958  VT1.getSizeInBits() <= 32);
7959 }
7960 
7961 bool AArch64TargetLowering::isExtFreeImpl(const Instruction *Ext) const {
7962  if (isa<FPExtInst>(Ext))
7963  return false;
7964 
7965  // Vector types are not free.
7966  if (Ext->getType()->isVectorTy())
7967  return false;
7968 
7969  for (const Use &U : Ext->uses()) {
7970  // The extension is free if we can fold it with a left shift in an
7971  // addressing mode or an arithmetic operation: add, sub, and cmp.
7972 
7973  // Is there a shift?
7974  const Instruction *Instr = cast<Instruction>(U.getUser());
7975 
7976  // Is this a constant shift?
7977  switch (Instr->getOpcode()) {
7978  case Instruction::Shl:
7979  if (!isa<ConstantInt>(Instr->getOperand(1)))
7980  return false;
7981  break;
7982  case Instruction::GetElementPtr: {
7983  gep_type_iterator GTI = gep_type_begin(Instr);
7984  auto &DL = Ext->getModule()->getDataLayout();
7985  std::advance(GTI, U.getOperandNo()-1);
7986  Type *IdxTy = GTI.getIndexedType();
7987  // This extension will end up with a shift because of the scaling factor.
7988  // 8-bit sized types have a scaling factor of 1, thus a shift amount of 0.
7989  // Get the shift amount based on the scaling factor:
7990  // log2(sizeof(IdxTy)) - log2(8).
7991  uint64_t ShiftAmt =
7992  countTrailingZeros(DL.getTypeStoreSizeInBits(IdxTy)) - 3;
7993  // Is the constant foldable in the shift of the addressing mode?
7994  // I.e., shift amount is between 1 and 4 inclusive.
7995  if (ShiftAmt == 0 || ShiftAmt > 4)
7996  return false;
7997  break;
7998  }
7999  case Instruction::Trunc:
8000  // Check if this is a noop.
8001  // trunc(sext ty1 to ty2) to ty1.
8002  if (Instr->getType() == Ext->getOperand(0)->getType())
8003  continue;
8005  default:
8006  return false;
8007  }
8008 
8009  // At this point we can use the bfm family, so this extension is free
8010  // for that use.
8011  }
8012  return true;
8013 }
8014 
8016  unsigned &RequiredAligment) const {
8017  if (!LoadedType.isSimple() ||
8018  (!LoadedType.isInteger() && !LoadedType.isFloatingPoint()))
8019  return false;
8020  // Cyclone supports unaligned accesses.
8021  RequiredAligment = 0;
8022  unsigned NumBits = LoadedType.getSizeInBits();
8023  return NumBits == 32 || NumBits == 64;
8024 }
8025 
8026 /// A helper function for determining the number of interleaved accesses we
8027 /// will generate when lowering accesses of the given type.
8028 unsigned
8030  const DataLayout &DL) const {
8031  return (DL.getTypeSizeInBits(VecTy) + 127) / 128;
8032 }
8033 
8036  if (Subtarget->getProcFamily() == AArch64Subtarget::Falkor &&
8037  I.getMetadata(FALKOR_STRIDED_ACCESS_MD) != nullptr)
8038  return MOStridedAccess;
8040 }
8041 
8043  VectorType *VecTy, const DataLayout &DL) const {
8044 
8045  unsigned VecSize = DL.getTypeSizeInBits(VecTy);
8046  unsigned ElSize = DL.getTypeSizeInBits(VecTy->getElementType());
8047 
8048  // Ensure the number of vector elements is greater than 1.
8049  if (VecTy->getNumElements() < 2)
8050  return false;
8051 
8052  // Ensure the element type is legal.
8053  if (ElSize != 8 && ElSize != 16 && ElSize != 32 && ElSize != 64)
8054  return false;
8055 
8056  // Ensure the total vector size is 64 or a multiple of 128. Types larger than
8057  // 128 will be split into multiple interleaved accesses.
8058  return VecSize == 64 || VecSize % 128 == 0;
8059 }
8060 
8061 /// Lower an interleaved load into a ldN intrinsic.
8062 ///
8063 /// E.g. Lower an interleaved load (Factor = 2):
8064 /// %wide.vec = load <8 x i32>, <8 x i32>* %ptr
8065 /// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
8066 /// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
8067 ///
8068 /// Into:
8069 /// %ld2 = { <4 x i32>, <4 x i32> } call llvm.aarch64.neon.ld2(%ptr)
8070 /// %vec0 = extractelement { <4 x i32>, <4 x i32> } %ld2, i32 0
8071 /// %vec1 = extractelement { <4 x i32>, <4 x i32> } %ld2, i32 1
8074  ArrayRef<unsigned> Indices, unsigned Factor) const {
8075  assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
8076  "Invalid interleave factor");
8077  assert(!Shuffles.empty() && "Empty shufflevector input");
8078  assert(Shuffles.size() == Indices.size() &&
8079  "Unmatched number of shufflevectors and indices");
8080 
8081  const DataLayout &DL = LI->getModule()->getDataLayout();
8082 
8083  VectorType *VecTy = Shuffles[0]->getType();
8084 
8085  // Skip if we do not have NEON and skip illegal vector types. We can
8086  // "legalize" wide vector types into multiple interleaved accesses as long as
8087  // the vector types are divisible by 128.
8088  if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(VecTy, DL))
8089  return false;
8090 
8091  unsigned NumLoads = getNumInterleavedAccesses(VecTy, DL);
8092 
8093  // A pointer vector can not be the return type of the ldN intrinsics. Need to
8094  // load integer vectors first and then convert to pointer vectors.
8095  Type *EltTy = VecTy->getVectorElementType();
8096  if (EltTy->isPointerTy())
8097  VecTy =
8098  VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
8099 
8100  IRBuilder<> Builder(LI);
8101 
8102  // The base address of the load.
8103  Value *BaseAddr = LI->getPointerOperand();
8104 
8105  if (NumLoads > 1) {
8106  // If we're going to generate more than one load, reset the sub-vector type
8107  // to something legal.
8108  VecTy = VectorType::get(VecTy->getVectorElementType(),
8109  VecTy->getVectorNumElements() / NumLoads);
8110 
8111  // We will compute the pointer operand of each load from the original base
8112  // address using GEPs. Cast the base address to a pointer to the scalar
8113  // element type.
8114  BaseAddr = Builder.CreateBitCast(
8115  BaseAddr, VecTy->getVectorElementType()->getPointerTo(
8116  LI->getPointerAddressSpace()));
8117  }
8118 
8119  Type *PtrTy = VecTy->getPointerTo(LI->getPointerAddressSpace());
8120  Type *Tys[2] = {VecTy, PtrTy};
8121  static const Intrinsic::ID LoadInts[3] = {Intrinsic::aarch64_neon_ld2,
8122  Intrinsic::aarch64_neon_ld3,
8123  Intrinsic::aarch64_neon_ld4};
8124  Function *LdNFunc =
8125  Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
8126 
8127  // Holds sub-vectors extracted from the load intrinsic return values. The
8128  // sub-vectors are associated with the shufflevector instructions they will
8129  // replace.
8131 
8132  for (unsigned LoadCount = 0; LoadCount < NumLoads; ++LoadCount) {
8133 
8134  // If we're generating more than one load, compute the base address of
8135  // subsequent loads as an offset from the previous.
8136  if (LoadCount > 0)
8137  BaseAddr = Builder.CreateConstGEP1_32(
8138  BaseAddr, VecTy->getVectorNumElements() * Factor);
8139 
8140  CallInst *LdN = Builder.CreateCall(
8141  LdNFunc, Builder.CreateBitCast(BaseAddr, PtrTy), "ldN");
8142 
8143  // Extract and store the sub-vectors returned by the load intrinsic.
8144  for (unsigned i = 0; i < Shuffles.size(); i++) {
8145  ShuffleVectorInst *SVI = Shuffles[i];
8146  unsigned Index = Indices[i];
8147 
8148  Value *SubVec = Builder.CreateExtractValue(LdN, Index);
8149 
8150  // Convert the integer vector to pointer vector if the element is pointer.
8151  if (EltTy->isPointerTy())
8152  SubVec = Builder.CreateIntToPtr(
8153  SubVec, VectorType::get(SVI->getType()->getVectorElementType(),
8154  VecTy->getVectorNumElements()));
8155  SubVecs[SVI].push_back(SubVec);
8156  }
8157  }
8158 
8159  // Replace uses of the shufflevector instructions with the sub-vectors
8160  // returned by the load intrinsic. If a shufflevector instruction is
8161  // associated with more than one sub-vector, those sub-vectors will be
8162  // concatenated into a single wide vector.
8163  for (ShuffleVectorInst *SVI : Shuffles) {
8164  auto &SubVec = SubVecs[SVI];
8165  auto *WideVec =
8166  SubVec.size() > 1 ? concatenateVectors(Builder, SubVec) : SubVec[0];
8167  SVI->replaceAllUsesWith(WideVec);
8168  }
8169 
8170  return true;
8171 }
8172 
8173 /// Lower an interleaved store into a stN intrinsic.
8174 ///
8175 /// E.g. Lower an interleaved store (Factor = 3):
8176 /// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
8177 /// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
8178 /// store <12 x i32> %i.vec, <12 x i32>* %ptr
8179 ///
8180 /// Into:
8181 /// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
8182 /// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
8183 /// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
8184 /// call void llvm.aarch64.neon.st3(%sub.v0, %sub.v1, %sub.v2, %ptr)
8185 ///
8186 /// Note that the new shufflevectors will be removed and we'll only generate one
8187 /// st3 instruction in CodeGen.
8188 ///
8189 /// Example for a more general valid mask (Factor 3). Lower:
8190 /// %i.vec = shuffle <32 x i32> %v0, <32 x i32> %v1,
8191 /// <4, 32, 16, 5, 33, 17, 6, 34, 18, 7, 35, 19>
8192 /// store <12 x i32> %i.vec, <12 x i32>* %ptr
8193 ///
8194 /// Into:
8195 /// %sub.v0 = shuffle <32 x i32> %v0, <32 x i32> v1, <4, 5, 6, 7>
8196 /// %sub.v1 = shuffle <32 x i32> %v0, <32 x i32> v1, <32, 33, 34, 35>
8197 /// %sub.v2 = shuffle <32 x i32> %v0, <32 x i32> v1, <16, 17, 18, 19>
8198 /// call void llvm.aarch64.neon.st3(%sub.v0, %sub.v1, %sub.v2, %ptr)
8200  ShuffleVectorInst *SVI,
8201  unsigned Factor) const {
8202  assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
8203  "Invalid interleave factor");
8204 
8205  VectorType *VecTy = SVI->getType();
8206  assert(VecTy->getVectorNumElements() % Factor == 0 &&
8207  "Invalid interleaved store");
8208 
8209  unsigned LaneLen = VecTy->getVectorNumElements() / Factor;
8210  Type *EltTy = VecTy->getVectorElementType();
8211  VectorType *SubVecTy = VectorType::get(EltTy, LaneLen);
8212 
8213  const DataLayout &DL = SI->getModule()->getDataLayout();
8214 
8215  // Skip if we do not have NEON and skip illegal vector types. We can
8216  // "legalize" wide vector types into multiple interleaved accesses as long as
8217  // the vector types are divisible by 128.
8218  if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(SubVecTy, DL))
8219  return false;
8220 
8221  unsigned NumStores = getNumInterleavedAccesses(SubVecTy, DL);
8222 
8223  Value *Op0 = SVI->getOperand(0);
8224  Value *Op1 = SVI->getOperand(1);
8225  IRBuilder<> Builder(SI);
8226 
8227  // StN intrinsics don't support pointer vectors as arguments. Convert pointer
8228  // vectors to integer vectors.
8229  if (EltTy->isPointerTy()) {
8230  Type *IntTy = DL.getIntPtrType(EltTy);
8231  unsigned NumOpElts = Op0->getType()->getVectorNumElements();
8232 
8233  // Convert to the corresponding integer vector.
8234  Type *IntVecTy = VectorType::get(IntTy, NumOpElts);
8235  Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
8236  Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
8237 
8238  SubVecTy = VectorType::get(IntTy, LaneLen);
8239  }
8240 
8241  // The base address of the store.
8242  Value *BaseAddr = SI->getPointerOperand();
8243 
8244  if (NumStores > 1) {
8245  // If we're going to generate more than one store, reset the lane length
8246  // and sub-vector type to something legal.
8247  LaneLen /= NumStores;
8248  SubVecTy = VectorType::get(SubVecTy->getVectorElementType(), LaneLen);
8249 
8250  // We will compute the pointer operand of each store from the original base
8251  // address using GEPs. Cast the base address to a pointer to the scalar
8252  // element type.
8253  BaseAddr = Builder.CreateBitCast(
8254  BaseAddr, SubVecTy->getVectorElementType()->getPointerTo(
8255  SI->getPointerAddressSpace()));
8256  }
8257 
8258  auto Mask = SVI->getShuffleMask();
8259 
8260  Type *PtrTy = SubVecTy->getPointerTo(SI->getPointerAddressSpace());
8261  Type *Tys[2] = {SubVecTy, PtrTy};
8262  static const Intrinsic::ID StoreInts[3] = {Intrinsic::aarch64_neon_st2,
8263  Intrinsic::aarch64_neon_st3,
8264  Intrinsic::aarch64_neon_st4};
8265  Function *StNFunc =
8266  Intrinsic::getDeclaration(SI->getModule(), StoreInts[Factor - 2], Tys);
8267 
8268  for (unsigned StoreCount = 0; StoreCount < NumStores; ++StoreCount) {
8269 
8271 
8272  // Split the shufflevector operands into sub vectors for the new stN call.
8273  for (unsigned i = 0; i < Factor; i++) {
8274  unsigned IdxI = StoreCount * LaneLen * Factor + i;
8275  if (Mask[IdxI] >= 0) {
8276  Ops.push_back(Builder.CreateShuffleVector(
8277  Op0, Op1, createSequentialMask(Builder, Mask[IdxI], LaneLen, 0)));
8278  } else {
8279  unsigned StartMask = 0;
8280  for (unsigned j = 1; j < LaneLen; j++) {
8281  unsigned IdxJ = StoreCount * LaneLen * Factor + j;
8282  if (Mask[IdxJ * Factor + IdxI] >= 0) {
8283  StartMask = Mask[IdxJ * Factor + IdxI] - IdxJ;
8284  break;
8285  }
8286  }
8287  // Note: Filling undef gaps with random elements is ok, since
8288  // those elements were being written anyway (with undefs).
8289  // In the case of all undefs we're defaulting to using elems from 0
8290  // Note: StartMask cannot be negative, it's checked in
8291  // isReInterleaveMask
8292  Ops.push_back(Builder.CreateShuffleVector(
8293  Op0, Op1, createSequentialMask(Builder, StartMask, LaneLen, 0)));
8294  }
8295  }
8296 
8297  // If we generating more than one store, we compute the base address of
8298  // subsequent stores as an offset from the previous.
8299  if (StoreCount > 0)
8300  BaseAddr = Builder.CreateConstGEP1_32(BaseAddr, LaneLen * Factor);
8301 
8302  Ops.push_back(Builder.CreateBitCast(BaseAddr, PtrTy));
8303  Builder.CreateCall(StNFunc, Ops);
8304  }
8305  return true;
8306 }
8307 
8308 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8309  unsigned AlignCheck) {
8310  return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8311  (DstAlign == 0 || DstAlign % AlignCheck == 0));
8312 }
8313 
8314 EVT AArch64TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
8315  unsigned SrcAlign, bool IsMemset,
8316  bool ZeroMemset,
8317  bool MemcpyStrSrc,
8318  MachineFunction &MF) const {
8319  // Don't use AdvSIMD to implement 16-byte memset. It would have taken one
8320  // instruction to materialize the v2i64 zero and one store (with restrictive
8321  // addressing mode). Just do two i64 store of zero-registers.
8322  bool Fast;
8323  const Function &F = MF.getFunction();
8324  if (Subtarget->hasFPARMv8() && !IsMemset && Size >= 16 &&
8325  !F.hasFnAttribute(Attribute::NoImplicitFloat) &&
8326  (memOpAlign(SrcAlign, DstAlign, 16) ||
8327  (allowsMisalignedMemoryAccesses(MVT::f128, 0, 1, &Fast) && Fast)))
8328  return MVT::f128;
8329 
8330  if (Size >= 8 &&
8331  (memOpAlign(SrcAlign, DstAlign, 8) ||
8332  (allowsMisalignedMemoryAccesses(MVT::i64, 0, 1, &Fast) && Fast)))
8333  return MVT::i64;
8334 
8335  if (Size >= 4 &&
8336  (memOpAlign(SrcAlign, DstAlign, 4) ||
8337  (allowsMisalignedMemoryAccesses(MVT::i32, 0, 1, &Fast) && Fast)))
8338  return MVT::i32;
8339 
8340  return MVT::Other;
8341 }
8342 
8343 // 12-bit optionally shifted immediates are legal for adds.
8345  if (Immed == std::numeric_limits<int64_t>::min()) {
8346  LLVM_DEBUG(dbgs() << "Illegal add imm " << Immed
8347  << ": avoid UB for INT64_MIN\n");
8348  return false;
8349  }
8350  // Same encoding for add/sub, just flip the sign.
8351  Immed = std::abs(Immed);
8352  bool IsLegal = ((Immed >> 12) == 0 ||
8353  ((Immed & 0xfff) == 0 && Immed >> 24 == 0));
8354  LLVM_DEBUG(dbgs() << "Is " << Immed
8355  << " legal add imm: " << (IsLegal ? "yes" : "no") << "\n");
8356  return IsLegal;
8357 }
8358 
8359 // Integer comparisons are implemented with ADDS/SUBS, so the range of valid
8360 // immediates is the same as for an add or a sub.
8362  return isLegalAddImmediate(Immed);
8363 }
8364 
8365 /// isLegalAddressingMode - Return true if the addressing mode represented
8366 /// by AM is legal for this target, for a load/store of the specified type.
8368  const AddrMode &AM, Type *Ty,
8369  unsigned AS, Instruction *I) const {
8370  // AArch64 has five basic addressing modes:
8371  // reg
8372  // reg + 9-bit signed offset
8373  // reg + SIZE_IN_BYTES * 12-bit unsigned offset
8374  // reg1 + reg2
8375  // reg + SIZE_IN_BYTES * reg
8376 
8377  // No global is ever allowed as a base.
8378  if (AM.BaseGV)
8379  return false;
8380 
8381  // No reg+reg+imm addressing.
8382  if (AM.HasBaseReg && AM.BaseOffs && AM.Scale)
8383  return false;
8384 
8385  // check reg + imm case:
8386  // i.e., reg + 0, reg + imm9, reg + SIZE_IN_BYTES * uimm12
8387  uint64_t NumBytes = 0;
8388  if (Ty->isSized()) {
8389  uint64_t NumBits = DL.getTypeSizeInBits(Ty);
8390  NumBytes = NumBits / 8;
8391  if (!isPowerOf2_64(NumBits))
8392  NumBytes = 0;
8393  }
8394 
8395  if (!AM.Scale) {
8396  int64_t Offset = AM.BaseOffs;
8397 
8398  // 9-bit signed offset
8399  if (isInt<9>(Offset))
8400  return true;
8401 
8402  // 12-bit unsigned offset
8403  unsigned shift = Log2_64(NumBytes);
8404  if (NumBytes && Offset > 0 && (Offset / NumBytes) <= (1LL << 12) - 1 &&
8405  // Must be a multiple of NumBytes (NumBytes is a power of 2)
8406  (Offset >> shift) << shift == Offset)
8407  return true;
8408  return false;
8409  }
8410 
8411  // Check reg1 + SIZE_IN_BYTES * reg2 and reg1 + reg2
8412 
8413  return AM.Scale == 1 || (AM.Scale > 0 && (uint64_t)AM.Scale == NumBytes);
8414 }
8415 
8417  // Consider splitting large offset of struct or array.
8418  return true;
8419 }
8420 
8422  const AddrMode &AM, Type *Ty,
8423  unsigned AS) const {
8424  // Scaling factors are not free at all.
8425  // Operands | Rt Latency
8426  // -------------------------------------------
8427  // Rt, [Xn, Xm] | 4
8428  // -------------------------------------------
8429  // Rt, [Xn, Xm, lsl #imm] | Rn: 4 Rm: 5
8430  // Rt, [Xn, Wm, <extend> #imm] |
8431  if (isLegalAddressingMode(DL, AM, Ty, AS))
8432  // Scale represents reg2 * scale, thus account for 1 if
8433  // it is not equal to 0 or 1.
8434  return AM.Scale != 0 && AM.Scale != 1;
8435  return -1;
8436 }
8437 
8439  VT = VT.getScalarType();
8440 
8441  if (!VT.isSimple())
8442  return false;
8443 
8444  switch (VT.getSimpleVT().SimpleTy) {
8445  case MVT::f32:
8446  case MVT::f64:
8447  return true;
8448  default:
8449  break;
8450  }
8451 
8452  return false;
8453 }
8454 
8455 const MCPhysReg *
8457  // LR is a callee-save register, but we must treat it as clobbered by any call
8458  // site. Hence we include LR in the scratch registers, which are in turn added
8459  // as implicit-defs for stackmaps and patchpoints.
8460  static const MCPhysReg ScratchRegs[] = {
8461  AArch64::X16, AArch64::X17, AArch64::LR, 0
8462  };
8463  return ScratchRegs;
8464 }
8465 
8466 bool
8468  EVT VT = N->getValueType(0);
8469  // If N is unsigned bit extraction: ((x >> C) & mask), then do not combine
8470  // it with shift to let it be lowered to UBFX.
8471  if (N->getOpcode() == ISD::AND && (VT == MVT::i32 || VT == MVT::i64) &&
8472  isa<ConstantSDNode>(N->getOperand(1))) {
8473  uint64_t TruncMask = N->getConstantOperandVal(1);
8474  if (isMask_64(TruncMask) &&
8475  N->getOperand(0).getOpcode() == ISD::SRL &&
8476  isa<ConstantSDNode>(N->getOperand(0)->getOperand(1)))
8477  return false;
8478  }
8479  return true;
8480 }
8481 
8483  Type *Ty) const {
8484  assert(Ty->isIntegerTy());
8485 
8486  unsigned BitSize = Ty->getPrimitiveSizeInBits();
8487  if (BitSize == 0)
8488  return false;
8489 
8490  int64_t Val = Imm.getSExtValue();
8491  if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, BitSize))
8492  return true;
8493 
8494  if ((int64_t)Val < 0)
8495  Val = ~Val;
8496  if (BitSize == 32)
8497  Val &= (1LL << 32) - 1;
8498 
8499  unsigned LZ = countLeadingZeros((uint64_t)Val);
8500  unsigned Shift = (63 - LZ) / 16;
8501  // MOVZ is free so return true for one or fewer MOVK.
8502  return Shift < 3;
8503 }
8504 
8506  unsigned Index) const {
8508  return false;
8509 
8510  return (Index == 0 || Index == ResVT.getVectorNumElements());
8511 }
8512 
8513 /// Turn vector tests of the signbit in the form of:
8514 /// xor (sra X, elt_size(X)-1), -1
8515 /// into:
8516 /// cmge X, X, #0
8518  const AArch64Subtarget *Subtarget) {
8519  EVT VT = N->getValueType(0);
8520  if (!Subtarget->hasNEON() || !VT.isVector())
8521  return SDValue();
8522 
8523  // There must be a shift right algebraic before the xor, and the xor must be a
8524  // 'not' operation.
8525  SDValue Shift = N->getOperand(0);
8526  SDValue Ones = N->getOperand(1);
8527  if (Shift.getOpcode() != AArch64ISD::VASHR || !Shift.hasOneUse() ||
8529  return SDValue();
8530 
8531  // The shift should be smearing the sign bit across each vector element.
8532  auto *ShiftAmt = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
8533  EVT ShiftEltTy = Shift.getValueType().getVectorElementType();
8534  if (!ShiftAmt || ShiftAmt->getZExtValue() != ShiftEltTy.getSizeInBits() - 1)
8535  return SDValue();
8536 
8537  return DAG.getNode(AArch64ISD::CMGEz, SDLoc(N), VT, Shift.getOperand(0));
8538 }
8539 
8540 // Generate SUBS and CSEL for integer abs.
8542  EVT VT = N->getValueType(0);
8543 
8544  SDValue N0 = N->getOperand(0);
8545  SDValue N1 = N->getOperand(1);
8546  SDLoc DL(N);
8547 
8548  // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
8549  // and change it to SUB and CSEL.
8550  if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
8551  N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
8552  N1.getOpcode() == ISD::SRA && N1.getOperand(0) == N0.getOperand(0))
8553  if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
8554  if (Y1C->getAPIntValue() == VT.getSizeInBits() - 1) {
8555  SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
8556  N0.getOperand(0));
8557  // Generate SUBS & CSEL.
8558  SDValue Cmp =
8559  DAG.getNode(AArch64ISD::SUBS, DL, DAG.getVTList(VT, MVT::i32),
8560  N0.getOperand(0), DAG.getConstant(0, DL, VT));
8561  return DAG.getNode(AArch64ISD::CSEL, DL, VT, N0.getOperand(0), Neg,
8563  SDValue(Cmp.getNode(), 1));
8564  }
8565  return SDValue();
8566 }
8567 
8570  const AArch64Subtarget *Subtarget) {
8571  if (DCI.isBeforeLegalizeOps())
8572  return SDValue();
8573 
8574  if (SDValue Cmp = foldVectorXorShiftIntoCmp(N, DAG, Subtarget))
8575  return Cmp;
8576 
8577  return performIntegerAbsCombine(N, DAG);
8578 }
8579 
8580 SDValue
8581 AArch64TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
8582  SelectionDAG &DAG,
8583  SmallVectorImpl<SDNode *> &Created) const {
8585  if (isIntDivCheap(N->getValueType(0), Attr))
8586  return SDValue(N,0); // Lower SDIV as SDIV
8587 
8588  // fold (sdiv X, pow2)
8589  EVT VT = N->getValueType(0);
8590  if ((VT != MVT::i32 && VT != MVT::i64) ||
8591  !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
8592  return SDValue();
8593 
8594  SDLoc DL(N);
8595  SDValue N0 = N->getOperand(0);
8596  unsigned Lg2 = Divisor.countTrailingZeros();
8597  SDValue Zero = DAG.getConstant(0, DL, VT);
8598  SDValue Pow2MinusOne = DAG.getConstant((1ULL << Lg2) - 1, DL, VT);
8599 
8600  // Add (N0 < 0) ? Pow2 - 1 : 0;
8601  SDValue CCVal;
8602  SDValue Cmp = getAArch64Cmp(N0, Zero, ISD::SETLT, CCVal, DAG, DL);
8603  SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, Pow2MinusOne);
8604  SDValue CSel = DAG.getNode(AArch64ISD::CSEL, DL, VT, Add, N0, CCVal, Cmp);
8605 
8606  Created.push_back(Cmp.getNode());
8607  Created.push_back(Add.getNode());
8608  Created.push_back(CSel.getNode());
8609 
8610  // Divide by pow2.
8611  SDValue SRA =
8612  DAG.getNode(ISD::SRA, DL, VT, CSel, DAG.getConstant(Lg2, DL, MVT::i64));
8613 
8614  // If we're dividing by a positive value, we're done. Otherwise, we must
8615  // negate the result.
8616  if (Divisor.isNonNegative())
8617  return SRA;
8618 
8619  Created.push_back(SRA.getNode());
8620  return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), SRA);
8621 }
8622 
8625  const AArch64Subtarget *Subtarget) {
8626  if (DCI.isBeforeLegalizeOps())
8627  return SDValue();
8628 
8629  // The below optimizations require a constant RHS.
8630  if (!isa<ConstantSDNode>(N->getOperand(1)))
8631  return SDValue();
8632 
8633  ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
8634  const APInt &ConstValue = C->getAPIntValue();
8635 
8636  // Multiplication of a power of two plus/minus one can be done more
8637  // cheaply as as shift+add/sub. For now, this is true unilaterally. If
8638  // future CPUs have a cheaper MADD instruction, this may need to be
8639  // gated on a subtarget feature. For Cyclone, 32-bit MADD is 4 cycles and
8640  // 64-bit is 5 cycles, so this is always a win.
8641  // More aggressively, some multiplications N0 * C can be lowered to
8642  // shift+add+shift if the constant C = A * B where A = 2^N + 1 and B = 2^M,
8643  // e.g. 6=3*2=(2+1)*2.
8644  // TODO: consider lowering more cases, e.g. C = 14, -6, -14 or even 45
8645  // which equals to (1+2)*16-(1+2).
8646  SDValue N0 = N->getOperand(0);
8647  // TrailingZeroes is used to test if the mul can be lowered to
8648  // shift+add+shift.
8649  unsigned TrailingZeroes = ConstValue.countTrailingZeros();
8650  if (TrailingZeroes) {
8651  // Conservatively do not lower to shift+add+shift if the mul might be
8652  // folded into smul or umul.
8653  if (N0->hasOneUse() && (isSignExtended(N0.getNode(), DAG) ||
8654  isZeroExtended(N0.getNode(), DAG)))
8655  return SDValue();
8656  // Conservatively do not lower to shift+add+shift if the mul might be
8657  // folded into madd or msub.
8658  if (N->hasOneUse() && (N->use_begin()->getOpcode() == ISD::ADD ||
8659  N->use_begin()->getOpcode() == ISD::SUB))
8660  return SDValue();
8661  }
8662  // Use ShiftedConstValue instead of ConstValue to support both shift+add/sub
8663  // and shift+add+shift.
8664  APInt ShiftedConstValue = ConstValue.ashr(TrailingZeroes);
8665 
8666  unsigned ShiftAmt, AddSubOpc;
8667  // Is the shifted value the LHS operand of the add/sub?
8668  bool ShiftValUseIsN0 = true;
8669  // Do we need to negate the result?
8670  bool NegateResult = false;
8671 
8672  if (ConstValue.isNonNegative()) {
8673  // (mul x, 2^N + 1) => (add (shl x, N), x)
8674  // (mul x, 2^N - 1) => (sub (shl x, N), x)
8675  // (mul x, (2^N + 1) * 2^M) => (shl (add (shl x, N), x), M)
8676  APInt SCVMinus1 = ShiftedConstValue - 1;
8677  APInt CVPlus1 = ConstValue + 1;
8678  if (SCVMinus1.isPowerOf2()) {
8679  ShiftAmt = SCVMinus1.logBase2();
8680  AddSubOpc = ISD::ADD;
8681  } else if (CVPlus1.isPowerOf2()) {
8682  ShiftAmt = CVPlus1.logBase2();
8683  AddSubOpc = ISD::SUB;
8684  } else
8685  return SDValue();
8686  } else {
8687  // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8688  // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8689  APInt CVNegPlus1 = -ConstValue + 1;
8690  APInt CVNegMinus1 = -ConstValue - 1;
8691  if (CVNegPlus1.isPowerOf2()) {
8692  ShiftAmt = CVNegPlus1.logBase2();
8693  AddSubOpc = ISD::SUB;
8694  ShiftValUseIsN0 = false;
8695  } else if (CVNegMinus1.isPowerOf2()) {
8696  ShiftAmt = CVNegMinus1.logBase2();
8697  AddSubOpc = ISD::ADD;
8698  NegateResult = true;
8699  } else
8700  return SDValue();
8701  }
8702 
8703  SDLoc DL(N);
8704  EVT VT = N->getValueType(0);
8705  SDValue ShiftedVal = DAG.getNode(ISD::SHL, DL, VT, N0,
8706  DAG.getConstant(ShiftAmt, DL, MVT::i64));
8707 
8708  SDValue AddSubN0 = ShiftValUseIsN0 ? ShiftedVal : N0;
8709  SDValue AddSubN1 = ShiftValUseIsN0 ? N0 : ShiftedVal;
8710  SDValue Res = DAG.getNode(AddSubOpc, DL, VT, AddSubN0, AddSubN1);
8711  assert(!(NegateResult && TrailingZeroes) &&
8712  "NegateResult and TrailingZeroes cannot both be true for now.");
8713  // Negate the result.
8714  if (NegateResult)
8715  return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Res);
8716  // Shift the result.
8717  if (TrailingZeroes)
8718  return DAG.getNode(ISD::SHL, DL, VT, Res,
8719  DAG.getConstant(TrailingZeroes, DL, MVT::i64));
8720  return Res;
8721 }
8722 
8724  SelectionDAG &DAG) {
8725  // Take advantage of vector comparisons producing 0 or -1 in each lane to
8726  // optimize away operation when it's from a constant.
8727  //
8728  // The general transformation is:
8729  // UNARYOP(AND(VECTOR_CMP(x,y), constant)) -->
8730  // AND(VECTOR_CMP(x,y), constant2)
8731  // constant2 = UNARYOP(constant)
8732 
8733  // Early exit if this isn't a vector operation, the operand of the
8734  // unary operation isn't a bitwise AND, or if the sizes of the operations
8735  // aren't the same.
8736  EVT VT = N->getValueType(0);
8737  if (!VT.isVector() || N->getOperand(0)->getOpcode() != ISD::AND ||
8738  N->getOperand(0)->getOperand(0)->getOpcode() != ISD::SETCC ||
8739  VT.getSizeInBits() != N->getOperand(0)->getValueType(0).getSizeInBits())
8740  return SDValue();
8741 
8742  // Now check that the other operand of the AND is a constant. We could
8743  // make the transformation for non-constant splats as well, but it's unclear
8744  // that would be a benefit as it would not eliminate any operations, just
8745  // perform one more step in scalar code before moving to the vector unit.
8746  if (BuildVectorSDNode *BV =
8747  dyn_cast<BuildVectorSDNode>(N->getOperand(0)->getOperand(1))) {
8748  // Bail out if the vector isn't a constant.
8749  if (!BV->isConstant())
8750  return SDValue();
8751 
8752  // Everything checks out. Build up the new and improved node.
8753  SDLoc DL(N);
8754  EVT IntVT = BV->getValueType(0);
8755  // Create a new constant of the appropriate type for the transformed
8756  // DAG.
8757  SDValue SourceConst = DAG.getNode(N->getOpcode(), DL, VT, SDValue(BV, 0));
8758  // The AND node needs bitcasts to/from an integer vector type around it.
8759  SDValue MaskConst = DAG.getNode(ISD::BITCAST, DL, IntVT, SourceConst);
8760  SDValue NewAnd = DAG.getNode(ISD::AND, DL, IntVT,
8761  N->getOperand(0)->getOperand(0), MaskConst);
8762  SDValue Res = DAG.getNode(ISD::BITCAST, DL, VT, NewAnd);
8763  return Res;
8764  }
8765 
8766  return SDValue();
8767 }
8768 
8770  const AArch64Subtarget *Subtarget) {
8771  // First try to optimize away the conversion when it's conditionally from
8772  // a constant. Vectors only.
8774  return Res;
8775 
8776  EVT VT = N->getValueType(0);
8777  if (VT != MVT::f32 && VT != MVT::f64)
8778  return SDValue();
8779 
8780  // Only optimize when the source and destination types have the same width.
8781  if (VT.getSizeInBits() != N->getOperand(0).getValueSizeInBits())
8782  return SDValue();
8783 
8784  // If the result of an integer load is only used by an integer-to-float
8785  // conversion, use a fp load instead and a AdvSIMD scalar {S|U}CVTF instead.
8786  // This eliminates an "integer-to-vector-move" UOP and improves throughput.
8787  SDValue N0 = N->getOperand(0);
8788  if (Subtarget->hasNEON() && ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8789  // Do not change the width of a volatile load.
8790  !cast<LoadSDNode>(N0)->isVolatile()) {
8791  LoadSDNode *LN0 = cast<LoadSDNode>(N0);
8792  SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(), LN0->getBasePtr(),
8793  LN0->getPointerInfo(), LN0->getAlignment(),
8794  LN0->getMemOperand()->getFlags());
8795 
8796  // Make sure successors of the original load stay after it by updating them
8797  // to use the new Chain.
8798  DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), Load.getValue(1));
8799 
8800  unsigned Opcode =
8802  return DAG.getNode(Opcode, SDLoc(N), VT, Load);
8803  }
8804 
8805  return SDValue();
8806 }
8807 
8808 /// Fold a floating-point multiply by power of two into floating-point to
8809 /// fixed-point conversion.
8812  const AArch64Subtarget *Subtarget) {
8813  if (!Subtarget->hasNEON())
8814  return SDValue();
8815 
8816  SDValue Op = N->getOperand(0);
8817  if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
8818  Op.getOpcode() != ISD::FMUL)
8819  return SDValue();
8820 
8821  SDValue ConstVec = Op->getOperand(1);
8822  if (!isa<BuildVectorSDNode>(ConstVec))
8823  return SDValue();
8824 
8825  MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
8826  uint32_t FloatBits = FloatTy.getSizeInBits();
8827  if (FloatBits != 32 && FloatBits != 64)
8828  return SDValue();
8829 
8831  uint32_t IntBits = IntTy.getSizeInBits();
8832  if (IntBits != 16 && IntBits != 32 && IntBits != 64)
8833  return SDValue();
8834 
8835  // Avoid conversions where iN is larger than the float (e.g., float -> i64).
8836  if (IntBits > FloatBits)
8837  return SDValue();
8838 
8839  BitVector UndefElements;
8840  BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
8841  int32_t Bits = IntBits == 64 ? 64 : 32;
8842  int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, Bits + 1);
8843  if (C == -1 || C == 0 || C > Bits)
8844  return SDValue();
8845 
8846  MVT ResTy;
8847  unsigned NumLanes = Op.getValueType().getVectorNumElements();
8848  switch (NumLanes) {
8849  default:
8850  return SDValue();
8851  case 2:
8852  ResTy = FloatBits == 32 ? MVT::v2i32 : MVT::v2i64;
8853  break;
8854  case 4:
8855  ResTy = FloatBits == 32 ? MVT::v4i32 : MVT::v4i64;
8856  break;
8857  }
8858 
8859  if (ResTy == MVT::v4i64 && DCI.isBeforeLegalizeOps())
8860  return SDValue();
8861 
8862  assert((ResTy != MVT::v4i64 || DCI.isBeforeLegalizeOps()) &&
8863  "Illegal vector type after legalization");
8864 
8865  SDLoc DL(N);
8866  bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
8867  unsigned IntrinsicOpcode = IsSigned ? Intrinsic::aarch64_neon_vcvtfp2fxs
8868  : Intrinsic::aarch64_neon_vcvtfp2fxu;
8869  SDValue FixConv =
8870  DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, ResTy,
8871  DAG.getConstant(IntrinsicOpcode, DL, MVT::i32),
8872  Op->getOperand(0), DAG.getConstant(C, DL, MVT::i32));
8873  // We can handle smaller integers by generating an extra trunc.
8874  if (IntBits < FloatBits)
8875  FixConv = DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), FixConv);
8876 
8877  return FixConv;
8878 }
8879 
8880 /// Fold a floating-point divide by power of two into fixed-point to
8881 /// floating-point conversion.
8884  const AArch64Subtarget *Subtarget) {
8885  if (!Subtarget->hasNEON())
8886  return SDValue();
8887 
8888  SDValue Op = N->getOperand(0);
8889  unsigned Opc = Op->getOpcode();
8890  if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
8891  !Op.getOperand(0).getValueType().isSimple() ||
8892  (Opc != ISD::SINT_TO_FP && Opc != ISD::UINT_TO_FP))
8893  return SDValue();
8894 
8895  SDValue ConstVec = N->getOperand(1);
8896  if (!isa<BuildVectorSDNode>(ConstVec))
8897  return SDValue();
8898 
8900  int32_t IntBits = IntTy.getSizeInBits();
8901  if (IntBits != 16 && IntBits != 32 && IntBits != 64)
8902  return SDValue();
8903 
8904  MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
8905  int32_t FloatBits = FloatTy.getSizeInBits();
8906  if (FloatBits != 32 && FloatBits != 64)
8907  return SDValue();
8908 
8909  // Avoid conversions where iN is larger than the float (e.g., i64 -> float).
8910  if (IntBits > FloatBits)
8911  return SDValue();
8912 
8913  BitVector UndefElements;
8914  BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
8915  int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, FloatBits + 1);
8916  if (C == -1 || C == 0 || C > FloatBits)
8917  return SDValue();
8918 
8919  MVT ResTy;
8920  unsigned NumLanes = Op.getValueType().getVectorNumElements();
8921  switch (NumLanes) {
8922  default:
8923  return SDValue();
8924  case 2:
8925  ResTy = FloatBits == 32 ? MVT::v2i32 : MVT::v2i64;
8926  break;
8927  case 4:
8928  ResTy = FloatBits == 32 ? MVT::v4i32 : MVT::v4i64;
8929  break;
8930  }
8931 
8932  if (ResTy == MVT::v4i64 && DCI.isBeforeLegalizeOps())
8933  return SDValue();
8934 
8935  SDLoc DL(N);
8936  SDValue ConvInput = Op.getOperand(0);
8937  bool IsSigned = Opc == ISD::SINT_TO_FP;
8938  if (IntBits < FloatBits)
8939  ConvInput = DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, DL,
8940  ResTy, ConvInput);
8941 
8942  unsigned IntrinsicOpcode = IsSigned ? Intrinsic::aarch64_neon_vcvtfxs2fp
8943  : Intrinsic::aarch64_neon_vcvtfxu2fp;
8944  return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Op.getValueType(),
8945  DAG.getConstant(IntrinsicOpcode, DL, MVT::i32), ConvInput,
8946  DAG.getConstant(C, DL, MVT::i32));
8947 }
8948 
8949 /// An EXTR instruction is made up of two shifts, ORed together. This helper
8950 /// searches for and classifies those shifts.
8951 static bool findEXTRHalf(SDValue N, SDValue &Src, uint32_t &ShiftAmount,
8952  bool &FromHi) {
8953  if (N.getOpcode() == ISD::SHL)
8954  FromHi = false;
8955  else if (N.getOpcode() == ISD::SRL)
8956  FromHi = true;
8957  else
8958  return false;
8959 
8960  if (!isa<ConstantSDNode>(N.getOperand(1)))
8961  return false;
8962 
8963  ShiftAmount = N->getConstantOperandVal(1);
8964  Src = N->getOperand(0);
8965  return true;
8966 }
8967 
8968 /// EXTR instruction extracts a contiguous chunk of bits from two existing
8969 /// registers viewed as a high/low pair. This function looks for the pattern:
8970 /// <tt>(or (shl VAL1, \#N), (srl VAL2, \#RegWidth-N))</tt> and replaces it
8971 /// with an EXTR. Can't quite be done in TableGen because the two immediates
8972 /// aren't independent.
8975  SelectionDAG &DAG = DCI.DAG;
8976  SDLoc DL(N);
8977  EVT VT = N->getValueType(0);
8978 
8979  assert(N->getOpcode() == ISD::OR && "Unexpected root");
8980 
8981  if (VT != MVT::i32 && VT != MVT::i64)
8982  return SDValue();
8983 
8984  SDValue LHS;
8985  uint32_t ShiftLHS = 0;
8986  bool LHSFromHi = false;
8987  if (!findEXTRHalf(N->getOperand(0), LHS, ShiftLHS, LHSFromHi))
8988  return SDValue();
8989 
8990  SDValue RHS;
8991  uint32_t ShiftRHS = 0;
8992  bool RHSFromHi = false;
8993  if (!findEXTRHalf(N->getOperand(1), RHS, ShiftRHS, RHSFromHi))
8994  return SDValue();
8995 
8996  // If they're both trying to come from the high part of the register, they're
8997  // not really an EXTR.
8998  if (LHSFromHi == RHSFromHi)
8999  return SDValue();
9000 
9001  if (ShiftLHS + ShiftRHS != VT.getSizeInBits())
9002  return SDValue();
9003 
9004  if (LHSFromHi) {
9005  std::swap(LHS, RHS);
9006  std::swap(ShiftLHS, ShiftRHS);
9007  }
9008 
9009  return DAG.getNode(AArch64ISD::EXTR, DL, VT, LHS, RHS,
9010  DAG.getConstant(ShiftRHS, DL, MVT::i64));
9011 }
9012 
9015  EVT VT = N->getValueType(0);
9016  SelectionDAG &DAG = DCI.DAG;
9017  SDLoc DL(N);
9018 
9019  if (!VT.isVector())
9020  return SDValue();
9021 
9022  SDValue N0 = N->getOperand(0);
9023  if (N0.getOpcode() != ISD::AND)
9024  return SDValue();
9025 
9026  SDValue N1 = N->getOperand(1);
9027  if (N1.getOpcode() != ISD::AND)
9028  return SDValue();
9029 
9030  // We only have to look for constant vectors here since the general, variable
9031  // case can be handled in TableGen.
9032  unsigned Bits = VT.getScalarSizeInBits();
9033  uint64_t BitMask = Bits == 64 ? -1ULL : ((1ULL << Bits) - 1);
9034  for (int i = 1; i >= 0; --i)
9035  for (int j = 1; j >= 0; --j) {
9038  if (!BVN0 || !BVN1)
9039  continue;
9040 
9041  bool FoundMatch = true;
9042  for (unsigned k = 0; k < VT.getVectorNumElements(); ++k) {
9044  ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(BVN1->getOperand(k));
9045  if (!CN0 || !CN1 ||
9046  CN0->getZExtValue() != (BitMask & ~CN1->getZExtValue())) {
9047  FoundMatch = false;
9048  break;
9049  }
9050  }
9051 
9052  if (FoundMatch)
9053  return DAG.getNode(AArch64ISD::BSL, DL, VT, SDValue(BVN0, 0),
9054  N0->getOperand(1 - i), N1->getOperand(1 - j));
9055  }
9056 
9057  return SDValue();
9058 }
9059 
9061  const AArch64Subtarget *Subtarget) {
9062  // Attempt to form an EXTR from (or (shl VAL1, #N), (srl VAL2, #RegWidth-N))
9063  SelectionDAG &DAG = DCI.DAG;
9064  EVT VT = N->getValueType(0);
9065 
9066  if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9067  return SDValue();
9068 
9069  if (SDValue Res = tryCombineToEXTR(N, DCI))
9070  return Res;
9071 
9072  if (SDValue Res = tryCombineToBSL(N, DCI))
9073  return Res;
9074 
9075  return SDValue();
9076 }
9077 
9080  SelectionDAG &DAG = DCI.DAG;
9081  EVT VT = N->getValueType(0);
9082  if (VT != MVT::i32 && VT != MVT::i64)
9083  return SDValue();
9084 
9085  // Canonicalize (srl (bswap i32 x), 16) to (rotr (bswap i32 x), 16), if the
9086  // high 16-bits of x are zero. Similarly, canonicalize (srl (bswap i64 x), 32)
9087  // to (rotr (bswap i64 x), 32), if the high 32-bits of x are zero.
9088  SDValue N0 = N->getOperand(0);
9089  if (N0.getOpcode() == ISD::BSWAP) {
9090  SDLoc DL(N);
9091  SDValue N1 = N->getOperand(1);
9092  SDValue N00 = N0.getOperand(0);
9093  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9094  uint64_t ShiftAmt = C->getZExtValue();
9095  if (VT == MVT::i32 && ShiftAmt == 16 &&
9096  DAG.MaskedValueIsZero(N00, APInt::getHighBitsSet(32, 16)))
9097  return DAG.getNode(ISD::ROTR, DL, VT, N0, N1);
9098  if (VT == MVT::i64 && ShiftAmt == 32 &&
9099  DAG.MaskedValueIsZero(N00, APInt::getHighBitsSet(64, 32)))
9100  return DAG.getNode(ISD::ROTR, DL, VT, N0, N1);
9101  }
9102  }
9103  return SDValue();
9104 }
9105 
9108  SelectionDAG &DAG) {
9109  // Wait 'til after everything is legalized to try this. That way we have
9110  // legal vector types and such.
9111  if (DCI.isBeforeLegalizeOps())
9112  return SDValue();
9113 
9114  // Remove extraneous bitcasts around an extract_subvector.
9115  // For example,
9116  // (v4i16 (bitconvert
9117  // (extract_subvector (v2i64 (bitconvert (v8i16 ...)), (i64 1)))))
9118  // becomes
9119  // (extract_subvector ((v8i16 ...), (i64 4)))
9120 
9121  // Only interested in 64-bit vectors as the ultimate result.
9122  EVT VT = N->getValueType(0);
9123  if (!VT.isVector())
9124  return SDValue();
9125  if (VT.getSimpleVT().getSizeInBits() != 64)
9126  return SDValue();
9127  // Is the operand an extract_subvector starting at the beginning or halfway
9128  // point of the vector? A low half may also come through as an
9129  // EXTRACT_SUBREG, so look for that, too.
9130  SDValue Op0 = N->getOperand(0);
9131  if (Op0->getOpcode() != ISD::EXTRACT_SUBVECTOR &&
9132  !(Op0->isMachineOpcode() &&
9133  Op0->getMachineOpcode() == AArch64::EXTRACT_SUBREG))
9134  return SDValue();
9135  uint64_t idx = cast<ConstantSDNode>(Op0->getOperand(1))->getZExtValue();
9136  if (Op0->getOpcode() == ISD::EXTRACT_SUBVECTOR) {
9137  if (Op0->getValueType(0).getVectorNumElements() != idx && idx != 0)
9138  return SDValue();
9139  } else if (Op0->getMachineOpcode() == AArch64::EXTRACT_SUBREG) {
9140  if (idx != AArch64::dsub)
9141  return SDValue();
9142  // The dsub reference is equivalent to a lane zero subvector reference.
9143  idx = 0;
9144  }
9145  // Look through the bitcast of the input to the extract.
9146  if (Op0->getOperand(0)->getOpcode() != ISD::BITCAST)
9147  return SDValue();
9148  SDValue Source = Op0->getOperand(0)->getOperand(0);
9149  // If the source type has twice the number of elements as our destination
9150  // type, we know this is an extract of the high or low half of the vector.
9151  EVT SVT = Source->getValueType(0);
9152  if (!SVT.isVector() ||
9153  SVT.getVectorNumElements() != VT.getVectorNumElements() * 2)
9154  return SDValue();
9155 
9156  LLVM_DEBUG(
9157  dbgs() << "aarch64-lower: bitcast extract_subvector simplification\n");
9158 
9159  // Create the simplified form to just extract the low or high half of the
9160  // vector directly rather than bothering with the bitcasts.
9161  SDLoc dl(N);
9162  unsigned NumElements = VT.getVectorNumElements();
9163  if (idx) {
9164  SDValue HalfIdx = DAG.getConstant(NumElements, dl, MVT::i64);
9165  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Source, HalfIdx);
9166  } else {
9167  SDValue SubReg = DAG.getTargetConstant(AArch64::dsub, dl, MVT::i32);
9168  return SDValue(DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, VT,
9169  Source, SubReg),
9170  0);
9171  }
9172 }
9173 
9176  SelectionDAG &DAG) {
9177  SDLoc dl(N);
9178  EVT VT = N->getValueType(0);
9179  SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
9180 
9181  // Optimize concat_vectors of truncated vectors, where the intermediate
9182  // type is illegal, to avoid said illegality, e.g.,
9183  // (v4i16 (concat_vectors (v2i16 (truncate (v2i64))),
9184  // (v2i16 (truncate (v2i64)))))
9185  // ->
9186  // (v4i16 (truncate (vector_shuffle (v4i32 (bitcast (v2i64))),
9187  // (v4i32 (bitcast (v2i64))),
9188  // <0, 2, 4, 6>)))
9189  // This isn't really target-specific, but ISD::TRUNCATE legality isn't keyed
9190  // on both input and result type, so we might generate worse code.
9191  // On AArch64 we know it's fine for v2i64->v4i16 and v4i32->v8i8.
9192  if (N->getNumOperands() == 2 &&
9193  N0->getOpcode() == ISD::TRUNCATE &&
9194  N1->getOpcode() == ISD::TRUNCATE) {
9195  SDValue N00 = N0->getOperand(0);
9196  SDValue N10 = N1->getOperand(0);
9197  EVT N00VT = N00.getValueType();
9198 
9199  if (N00VT == N10.getValueType() &&
9200  (N00VT == MVT::v2i64 || N00VT == MVT::v4i32) &&
9201  N00VT.getScalarSizeInBits() == 4 * VT.getScalarSizeInBits()) {
9202  MVT MidVT = (N00VT == MVT::v2i64 ? MVT::v4i32 : MVT::v8i16);
9204  for (size_t i = 0; i < Mask.size(); ++i)
9205  Mask[i] = i * 2;
9206  return DAG.getNode(ISD::TRUNCATE, dl, VT,
9207  DAG.getVectorShuffle(
9208  MidVT, dl,
9209  DAG.getNode(ISD::BITCAST, dl, MidVT, N00),
9210  DAG.getNode(ISD::BITCAST, dl, MidVT, N10), Mask));
9211  }
9212  }
9213 
9214  // Wait 'til after everything is legalized to try this. That way we have
9215  // legal vector types and such.
9216  if (DCI.isBeforeLegalizeOps())
9217  return SDValue();
9218 
9219  // If we see a (concat_vectors (v1x64 A), (v1x64 A)) it's really a vector
9220  // splat. The indexed instructions are going to be expecting a DUPLANE64, so
9221  // canonicalise to that.
9222  if (N0 == N1 && VT.getVectorNumElements() == 2) {
9223  assert(VT.getScalarSizeInBits() == 64);
9224  return DAG.getNode(AArch64ISD::DUPLANE64, dl, VT, WidenVector(N0, DAG),
9225  DAG.getConstant(0, dl, MVT::i64));
9226  }
9227 
9228  // Canonicalise concat_vectors so that the right-hand vector has as few
9229  // bit-casts as possible before its real operation. The primary matching
9230  // destination for these operations will be the narrowing "2" instructions,
9231  // which depend on the operation being performed on this right-hand vector.
9232  // For example,
9233  // (concat_vectors LHS, (v1i64 (bitconvert (v4i16 RHS))))
9234  // becomes
9235  // (bitconvert (concat_vectors (v4i16 (bitconvert LHS)), RHS))
9236 
9237  if (N1->getOpcode() != ISD::BITCAST)
9238  return SDValue();
9239  SDValue RHS = N1->getOperand(0);
9240  MVT RHSTy = RHS.getValueType().getSimpleVT();
9241  // If the RHS is not a vector, this is not the pattern we're looking for.
9242  if (!RHSTy.isVector())
9243  return SDValue();
9244 
9245  LLVM_DEBUG(
9246  dbgs() << "aarch64-lower: concat_vectors bitcast simplification\n");
9247 
9248  MVT ConcatTy = MVT::getVectorVT(RHSTy.getVectorElementType(),
9249  RHSTy.getVectorNumElements() * 2);
9250  return DAG.getNode(ISD::BITCAST, dl, VT,
9251  DAG.getNode(ISD::CONCAT_VECTORS, dl, ConcatTy,
9252  DAG.getNode(ISD::BITCAST, dl, RHSTy, N0),
9253  RHS));
9254 }
9255 
9258  SelectionDAG &DAG) {
9259  // Wait until after everything is legalized to try this. That way we have
9260  // legal vector types and such.
9261  if (DCI.isBeforeLegalizeOps())
9262  return SDValue();
9263  // Transform a scalar conversion of a value from a lane extract into a
9264  // lane extract of a vector conversion. E.g., from foo1 to foo2:
9265  // double foo1(int64x2_t a) { return vcvtd_n_f64_s64(a[1], 9); }
9266  // double foo2(int64x2_t a) { return vcvtq_n_f64_s64(a, 9)[1]; }
9267  //
9268  // The second form interacts better with instruction selection and the
9269  // register allocator to avoid cross-class register copies that aren't
9270  // coalescable due to a lane reference.
9271 
9272  // Check the operand and see if it originates from a lane extract.
9273  SDValue Op1 = N->getOperand(1);
9274  if (Op1.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9275  // Yep, no additional predication needed. Perform the transform.
9276  SDValue IID = N->getOperand(0);
9277  SDValue Shift = N->getOperand(2);
9278  SDValue Vec = Op1.getOperand(0);
9279  SDValue Lane = Op1.getOperand(1);
9280  EVT ResTy = N->getValueType(0);
9281  EVT VecResTy;
9282  SDLoc DL(N);
9283 
9284  // The vector width should be 128 bits by the time we get here, even
9285  // if it started as 64 bits (the extract_vector handling will have
9286  // done so).
9287  assert(Vec.getValueSizeInBits() == 128 &&
9288  "unexpected vector size on extract_vector_elt!");
9289  if (Vec.getValueType() == MVT::v4i32)
9290  VecResTy = MVT::v4f32;
9291  else if (Vec.getValueType() == MVT::v2i64)
9292  VecResTy = MVT::v2f64;
9293  else
9294  llvm_unreachable("unexpected vector type!");
9295 
9296  SDValue Convert =
9297  DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VecResTy, IID, Vec, Shift);
9298  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResTy, Convert, Lane);
9299  }
9300  return SDValue();
9301 }
9302 
9303 // AArch64 high-vector "long" operations are formed by performing the non-high
9304 // version on an extract_subvector of each operand which gets the high half:
9305 //
9306 // (longop2 LHS, RHS) == (longop (extract_high LHS), (extract_high RHS))
9307 //
9308 // However, there are cases which don't have an extract_high explicitly, but
9309 // have another operation that can be made compatible with one for free. For
9310 // example:
9311 //
9312 // (dupv64 scalar) --> (extract_high (dup128 scalar))
9313 //
9314 // This routine does the actual conversion of such DUPs, once outer routines
9315 // have determined that everything else is in order.
9316 // It also supports immediate DUP-like nodes (MOVI/MVNi), which we can fold
9317 // similarly here.
9319  switch (N.getOpcode()) {
9320  case AArch64ISD::DUP:
9321  case AArch64ISD::DUPLANE8:
9322  case AArch64ISD::DUPLANE16:
9323  case AArch64ISD::DUPLANE32:
9324  case AArch64ISD::DUPLANE64:
9325  case AArch64ISD::MOVI:
9326  case AArch64ISD::MOVIshift:
9327  case AArch64ISD::MOVIedit:
9328  case AArch64ISD::MOVImsl:
9329  case AArch64ISD::MVNIshift:
9330  case AArch64ISD::MVNImsl:
9331  break;
9332  default:
9333  // FMOV could be supported, but isn't very useful, as it would only occur
9334  // if you passed a bitcast' floating point immediate to an eligible long
9335  // integer op (addl, smull, ...).
9336  return SDValue();
9337  }
9338 
9339  MVT NarrowTy = N.getSimpleValueType();
9340  if (!NarrowTy.is64BitVector())
9341  return SDValue();
9342 
9343  MVT ElementTy = NarrowTy.getVectorElementType();
9344  unsigned NumElems = NarrowTy.getVectorNumElements();
9345  MVT NewVT = MVT::getVectorVT(ElementTy, NumElems * 2);
9346 
9347  SDLoc dl(N);
9348  return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NarrowTy,
9349  DAG.getNode(N->getOpcode(), dl, NewVT, N->ops()),
9350  DAG.getConstant(NumElems, dl, MVT::i64));
9351 }
9352 
9354  if (N.getOpcode() == ISD::EXTRACT_SUBVECTOR)
9355  return true;
9356 
9357  return N.getOpcode() == ISD::BITCAST &&
9359 }
9360 
9361 /// Helper structure to keep track of ISD::SET_CC operands.
9363  const SDValue *Opnd0;
9364  const SDValue *Opnd1;
9366 };
9367 
9368 /// Helper structure to keep track of a SET_CC lowered into AArch64 code.
9370  const SDValue *Cmp;
9372 };
9373 
9374 /// Helper structure to keep track of SetCC information.
9375 union SetCCInfo {
9378 };
9379 
9380 /// Helper structure to be able to read SetCC information. If set to
9381 /// true, IsAArch64 field, Info is a AArch64SetCCInfo, otherwise Info is a
9382 /// GenericSetCCInfo.
9386 };
9387 
9388 /// Check whether or not \p Op is a SET_CC operation, either a generic or
9389 /// an
9390 /// AArch64 lowered one.
9391 /// \p SetCCInfo is filled accordingly.
9392 /// \post SetCCInfo is meanginfull only when this function returns true.
9393 /// \return True when Op is a kind of SET_CC operation.
9395  // If this is a setcc, this is straight forward.
9396  if (Op.getOpcode() == ISD::SETCC) {
9397  SetCCInfo.Info.Generic.Opnd0 = &Op.getOperand(0);
9398  SetCCInfo.Info.Generic.Opnd1 = &Op.getOperand(1);
9399  SetCCInfo.Info.Generic.CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9400  SetCCInfo.IsAArch64 = false;
9401  return true;
9402  }
9403  // Otherwise, check if this is a matching csel instruction.
9404  // In other words:
9405  // - csel 1, 0, cc
9406  // - csel 0, 1, !cc
9407  if (Op.getOpcode() != AArch64ISD::CSEL)
9408  return false;
9409  // Set the information about the operands.
9410  // TODO: we want the operands of the Cmp not the csel
9411  SetCCInfo.Info.AArch64.Cmp = &Op.getOperand(3);
9412  SetCCInfo.IsAArch64 = true;
9413  SetCCInfo.Info.AArch64.CC = static_cast<AArch64CC::CondCode>(
9414  cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
9415 
9416  // Check that the operands matches the constraints:
9417  // (1) Both operands must be constants.
9418  // (2) One must be 1 and the other must be 0.
9421 
9422  // Check (1).
9423  if (!TValue || !FValue)
9424  return false;
9425 
9426  // Check (2).
9427  if (!TValue->isOne()) {
9428  // Update the comparison when we are interested in !cc.
9429  std::swap(TValue, FValue);
9430  SetCCInfo.Info.AArch64.CC =
9432  }
9433  return TValue->isOne() && FValue->isNullValue();
9434 }
9435 
9436 // Returns true if Op is setcc or zext of setcc.
9437 static bool isSetCCOrZExtSetCC(const SDValue& Op, SetCCInfoAndKind &Info) {
9438  if (isSetCC(Op, Info))
9439  return true;
9440  return ((Op.getOpcode() == ISD::ZERO_EXTEND) &&
9441  isSetCC(Op->getOperand(0), Info));
9442 }
9443 
9444 // The folding we want to perform is:
9445 // (add x, [zext] (setcc cc ...) )
9446 // -->
9447 // (csel x, (add x, 1), !cc ...)
9448 //
9449 // The latter will get matched to a CSINC instruction.
9451  assert(Op && Op->getOpcode() == ISD::ADD && "Unexpected operation!");
9452  SDValue LHS = Op->getOperand(0);
9453  SDValue RHS = Op->getOperand(1);
9454  SetCCInfoAndKind InfoAndKind;
9455 
9456  // If neither operand is a SET_CC, give up.
9457  if (!isSetCCOrZExtSetCC(LHS, InfoAndKind)) {
9458  std::swap(LHS, RHS);
9459  if (!isSetCCOrZExtSetCC(LHS, InfoAndKind))
9460  return SDValue();
9461  }
9462 
9463  // FIXME: This could be generatized to work for FP comparisons.
9464  EVT CmpVT = InfoAndKind.IsAArch64
9465  ? InfoAndKind.Info.AArch64.Cmp->getOperand(0).getValueType()
9466  : InfoAndKind.Info.Generic.Opnd0->getValueType();
9467  if (CmpVT != MVT::i32 && CmpVT != MVT::i64)
9468  return SDValue();
9469 
9470  SDValue CCVal;
9471  SDValue Cmp;
9472  SDLoc dl(Op);
9473  if (InfoAndKind.IsAArch64) {
9474  CCVal = DAG.getConstant(
9475  AArch64CC::getInvertedCondCode(InfoAndKind.Info.AArch64.CC), dl,
9476  MVT::i32);
9477  Cmp = *InfoAndKind.Info.AArch64.Cmp;
9478  } else
9479  Cmp = getAArch64Cmp(*InfoAndKind.Info.Generic.Opnd0,
9480  *InfoAndKind.Info.Generic.Opnd1,
9481  ISD::getSetCCInverse(InfoAndKind.Info.Generic.CC, true),
9482  CCVal, DAG, dl);
9483 
9484  EVT VT = Op->getValueType(0);
9485  LHS = DAG.getNode(ISD::ADD, dl, VT, RHS, DAG.getConstant(1, dl, VT));
9486  return DAG.getNode(AArch64ISD::CSEL, dl, VT, RHS, LHS, CCVal, Cmp);
9487 }
9488 
9489 // The basic add/sub long vector instructions have variants with "2" on the end
9490 // which act on the high-half of their inputs. They are normally matched by
9491 // patterns like:
9492 //
9493 // (add (zeroext (extract_high LHS)),
9494 // (zeroext (extract_high RHS)))
9495 // -> uaddl2 vD, vN, vM
9496 //
9497 // However, if one of the extracts is something like a duplicate, this
9498 // instruction can still be used profitably. This function puts the DAG into a
9499 // more appropriate form for those patterns to trigger.
9502  SelectionDAG &DAG) {
9503  if (DCI.isBeforeLegalizeOps())
9504  return SDValue();
9505 
9506  MVT VT = N->getSimpleValueType(0);
9507  if (!VT.is128BitVector()) {
9508  if (N->getOpcode() == ISD::ADD)
9509  return performSetccAddFolding(N, DAG);
9510  return SDValue();
9511  }
9512 
9513  // Make sure both branches are extended in the same way.
9514  SDValue LHS = N->getOperand(0);
9515  SDValue RHS = N->getOperand(1);
9516  if ((LHS.getOpcode() != ISD::ZERO_EXTEND &&
9517  LHS.getOpcode() != ISD::SIGN_EXTEND) ||
9518  LHS.getOpcode() != RHS.getOpcode())
9519  return SDValue();
9520 
9521  unsigned ExtType = LHS.getOpcode();
9522 
9523  // It's not worth doing if at least one of the inputs isn't already an
9524  // extract, but we don't know which it'll be so we have to try both.
9526  RHS = tryExtendDUPToExtractHigh(RHS.getOperand(0), DAG);
9527  if (!RHS.getNode())
9528  return SDValue();
9529 
9530  RHS = DAG.getNode(ExtType, SDLoc(N), VT, RHS);
9531  } else if (isEssentiallyExtractSubvector(RHS.getOperand(0))) {
9532  LHS = tryExtendDUPToExtractHigh(LHS.getOperand(0), DAG);
9533  if (!LHS.getNode())
9534  return SDValue();
9535 
9536  LHS = DAG.getNode(ExtType, SDLoc(N), VT, LHS);
9537  }
9538 
9539  return DAG.getNode(N->getOpcode(), SDLoc(N), VT, LHS, RHS);
9540 }
9541 
9542 // Massage DAGs which we can use the high-half "long" operations on into
9543 // something isel will recognize better. E.g.
9544 //
9545 // (aarch64_neon_umull (extract_high vec) (dupv64 scalar)) -->
9546 // (aarch64_neon_umull (extract_high (v2i64 vec)))
9547 // (extract_high (v2i64 (dup128 scalar)))))
9548 //
9549 static SDValue tryCombineLongOpWithDup(unsigned IID, SDNode *N,
9551  SelectionDAG &DAG) {
9552  if (DCI.isBeforeLegalizeOps())
9553  return SDValue();
9554 
9555  SDValue LHS = N->getOperand(1);
9556  SDValue RHS = N->getOperand(2);
9557  assert(LHS.getValueType().is64BitVector() &&
9558  RHS.getValueType().is64BitVector() &&
9559  "unexpected shape for long operation");
9560 
9561  // Either node could be a DUP, but it's not worth doing both of them (you'd
9562  // just as well use the non-high version) so look for a corresponding extract
9563  // operation on the other "wing".
9564  if (isEssentiallyExtractSubvector(LHS)) {
9565  RHS = tryExtendDUPToExtractHigh(RHS, DAG);
9566  if (!RHS.getNode())
9567  return SDValue();
9568  } else if (isEssentiallyExtractSubvector(RHS)) {
9569  LHS = tryExtendDUPToExtractHigh(LHS, DAG);
9570  if (!LHS.getNode())
9571  return SDValue();
9572  }
9573 
9574  return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), N->getValueType(0),
9575  N->getOperand(0), LHS, RHS);
9576 }
9577 
9578 static SDValue tryCombineShiftImm(unsigned IID, SDNode *N, SelectionDAG &DAG) {
9579  MVT ElemTy = N->getSimpleValueType(0).getScalarType();
9580  unsigned ElemBits = ElemTy.getSizeInBits();
9581 
9582  int64_t ShiftAmount;
9583  if (BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(2))) {
9584  APInt SplatValue, SplatUndef;
9585  unsigned SplatBitSize;
9586  bool HasAnyUndefs;
9587  if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
9588  HasAnyUndefs, ElemBits) ||
9589  SplatBitSize != ElemBits)
9590  return SDValue();
9591 
9592  ShiftAmount = SplatValue.getSExtValue();
9593  } else if (ConstantSDNode *CVN = dyn_cast<ConstantSDNode>(N->getOperand(2))) {
9594  ShiftAmount = CVN->getSExtValue();
9595  } else
9596  return SDValue();
9597 
9598  unsigned Opcode;
9599  bool IsRightShift;
9600  switch (IID) {
9601  default:
9602  llvm_unreachable("Unknown shift intrinsic");
9603  case Intrinsic::aarch64_neon_sqshl:
9604  Opcode = AArch64ISD::SQSHL_I;
9605  IsRightShift = false;
9606  break;
9607  case Intrinsic::aarch64_neon_uqshl:
9608  Opcode = AArch64ISD::UQSHL_I;
9609  IsRightShift = false;
9610  break;
9611  case Intrinsic::aarch64_neon_srshl:
9612  Opcode = AArch64ISD::SRSHR_I;
9613  IsRightShift = true;
9614  break;
9615  case Intrinsic::aarch64_neon_urshl:
9616  Opcode = AArch64ISD::URSHR_I;
9617  IsRightShift = true;
9618  break;
9619  case Intrinsic::aarch64_neon_sqshlu:
9620  Opcode = AArch64ISD::SQSHLU_I;
9621  IsRightShift = false;
9622  break;
9623  }
9624 
9625  if (IsRightShift && ShiftAmount <= -1 && ShiftAmount >= -(int)ElemBits) {
9626  SDLoc dl(N);
9627  return DAG.getNode(Opcode, dl, N->getValueType(0), N->getOperand(1),
9628  DAG.getConstant(-ShiftAmount, dl, MVT::i32));
9629  } else if (!IsRightShift && ShiftAmount >= 0 && ShiftAmount < ElemBits) {
9630  SDLoc dl(N);
9631  return DAG.getNode(Opcode, dl, N->getValueType(0), N->getOperand(1),
9632  DAG.getConstant(ShiftAmount, dl, MVT::i32));
9633  }
9634 
9635  return SDValue();
9636 }
9637 
9638 // The CRC32[BH] instructions ignore the high bits of their data operand. Since
9639 // the intrinsics must be legal and take an i32, this means there's almost
9640 // certainly going to be a zext in the DAG which we can eliminate.
9641 static SDValue tryCombineCRC32(unsigned Mask, SDNode *N, SelectionDAG &DAG) {
9642  SDValue AndN = N->getOperand(2);
9643  if (AndN.getOpcode() != ISD::AND)
9644  return SDValue();
9645 
9646  ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(AndN.getOperand(1));
9647  if (!CMask || CMask->getZExtValue() != Mask)
9648  return SDValue();
9649 
9651  N->getOperand(0), N->getOperand(1), AndN.getOperand(0));
9652 }
9653 
9655  SelectionDAG &DAG) {
9656  SDLoc dl(N);
9657  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0),
9658  DAG.getNode(Opc, dl,
9660  N->getOperand(1)),
9661  DAG.getConstant(0, dl, MVT::i64));
9662 }
9663 
9666  const AArch64Subtarget *Subtarget) {
9667  SelectionDAG &DAG = DCI.DAG;
9668  unsigned IID = getIntrinsicID(N);
9669  switch (IID) {
9670  default:
9671  break;
9672  case Intrinsic::aarch64_neon_vcvtfxs2fp:
9673  case Intrinsic::aarch64_neon_vcvtfxu2fp:
9674  return tryCombineFixedPointConvert(N, DCI, DAG);
9675  case Intrinsic::aarch64_neon_saddv:
9677  case Intrinsic::aarch64_neon_uaddv:
9679  case Intrinsic::aarch64_neon_sminv:
9681  case Intrinsic::aarch64_neon_uminv:
9683  case Intrinsic::aarch64_neon_smaxv:
9685  case Intrinsic::aarch64_neon_umaxv:
9687  case Intrinsic::aarch64_neon_fmax:
9688  return DAG.getNode(ISD::FMAXNAN, SDLoc(N), N->getValueType(0),
9689  N->getOperand(1), N->getOperand(2));
9690  case Intrinsic::aarch64_neon_fmin:
9691  return DAG.getNode(ISD::FMINNAN, SDLoc(N), N->getValueType(0),
9692  N->getOperand(1), N->getOperand(2));
9693  case Intrinsic::aarch64_neon_fmaxnm:
9694  return DAG.getNode(ISD::FMAXNUM, SDLoc(N), N->getValueType(0),
9695  N->getOperand(1), N->getOperand(2));
9696  case Intrinsic::aarch64_neon_fminnm:
9697  return DAG.getNode(ISD::FMINNUM, SDLoc(N), N->getValueType(0),
9698  N->getOperand(1), N->getOperand(2));
9699  case Intrinsic::aarch64_neon_smull:
9700  case Intrinsic::aarch64_neon_umull:
9701  case Intrinsic::aarch64_neon_pmull:
9702  case Intrinsic::aarch64_neon_sqdmull:
9703  return tryCombineLongOpWithDup(IID, N, DCI, DAG);
9704  case Intrinsic::aarch64_neon_sqshl:
9705  case Intrinsic::aarch64_neon_uqshl:
9706  case Intrinsic::aarch64_neon_sqshlu:
9707  case Intrinsic::aarch64_neon_srshl:
9708  case Intrinsic::aarch64_neon_urshl:
9709  return tryCombineShiftImm(IID, N, DAG);
9710  case Intrinsic::aarch64_crc32b:
9711  case Intrinsic::aarch64_crc32cb:
9712  return tryCombineCRC32(0xff, N, DAG);
9713  case Intrinsic::aarch64_crc32h:
9714  case Intrinsic::aarch64_crc32ch:
9715  return tryCombineCRC32(0xffff, N, DAG);
9716  }
9717  return SDValue();
9718 }
9719 
9722  SelectionDAG &DAG) {
9723  // If we see something like (zext (sabd (extract_high ...), (DUP ...))) then
9724  // we can convert that DUP into another extract_high (of a bigger DUP), which
9725  // helps the backend to decide that an sabdl2 would be useful, saving a real
9726  // extract_high operation.
9727  if (!DCI.isBeforeLegalizeOps() && N->getOpcode() == ISD::ZERO_EXTEND &&
9729  SDNode *ABDNode = N->getOperand(0).getNode();
9730  unsigned IID = getIntrinsicID(ABDNode);
9731  if (IID == Intrinsic::aarch64_neon_sabd ||
9732  IID == Intrinsic::aarch64_neon_uabd) {
9733  SDValue NewABD = tryCombineLongOpWithDup(IID, ABDNode, DCI, DAG);
9734  if (!NewABD.getNode())
9735  return SDValue();
9736 
9737  return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), N->getValueType(0),
9738  NewABD);
9739  }
9740  }
9741 
9742  // This is effectively a custom type legalization for AArch64.
9743  //
9744  // Type legalization will split an extend of a small, legal, type to a larger
9745  // illegal type by first splitting the destination type, often creating
9746  // illegal source types, which then get legalized in isel-confusing ways,
9747  // leading to really terrible codegen. E.g.,
9748  // %result = v8i32 sext v8i8 %value
9749  // becomes
9750  // %losrc = extract_subreg %value, ...
9751  // %hisrc = extract_subreg %value, ...
9752  // %lo = v4i32 sext v4i8 %losrc
9753  // %hi = v4i32 sext v4i8 %hisrc
9754  // Things go rapidly downhill from there.
9755  //
9756  // For AArch64, the [sz]ext vector instructions can only go up one element
9757  // size, so we can, e.g., extend from i8 to i16, but to go from i8 to i32
9758  // take two instructions.
9759  //
9760  // This implies that the most efficient way to do the extend from v8i8
9761  // to two v4i32 values is to first extend the v8i8 to v8i16, then do
9762  // the normal splitting to happen for the v8i16->v8i32.
9763 
9764  // This is pre-legalization to catch some cases where the default
9765  // type legalization will create ill-tempered code.
9766  if (!DCI.isBeforeLegalizeOps())
9767  return SDValue();
9768 
9769  // We're only interested in cleaning things up for non-legal vector types
9770  // here. If both the source and destination are legal, things will just
9771  // work naturally without any fiddling.
9772  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9773  EVT ResVT = N->getValueType(0);
9774  if (!ResVT.isVector() || TLI.isTypeLegal(ResVT))
9775  return SDValue();
9776  // If the vector type isn't a simple VT, it's beyond the scope of what
9777  // we're worried about here. Let legalization do its thing and hope for
9778  // the best.
9779  SDValue Src = N->getOperand(0);
9780  EVT SrcVT = Src->getValueType(0);
9781  if (!ResVT.isSimple() || !SrcVT.isSimple())
9782  return SDValue();
9783 
9784  // If the source VT is a 64-bit vector, we can play games and get the
9785  // better results we want.
9786  if (SrcVT.getSizeInBits() != 64)
9787  return SDValue();
9788 
9789  unsigned SrcEltSize = SrcVT.getScalarSizeInBits();
9790  unsigned ElementCount = SrcVT.getVectorNumElements();
9791  SrcVT = MVT::getVectorVT(MVT::getIntegerVT(SrcEltSize * 2), ElementCount);
9792  SDLoc DL(N);
9793  Src = DAG.getNode(N->getOpcode(), DL, SrcVT, Src);
9794 
9795  // Now split the rest of the operation into two halves, each with a 64
9796  // bit source.
9797  EVT LoVT, HiVT;
9798  SDValue Lo, Hi;
9799  unsigned NumElements = ResVT.getVectorNumElements();
9800  assert(!(NumElements & 1) && "Splitting vector, but not in half!");
9801  LoVT = HiVT = EVT::getVectorVT(*DAG.getContext(),
9802  ResVT.getVectorElementType(), NumElements / 2);
9803 
9804  EVT InNVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getVectorElementType(),
9805  LoVT.getVectorNumElements());
9806  Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, Src,
9807  DAG.getConstant(0, DL, MVT::i64));
9808  Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, Src,
9809  DAG.getConstant(InNVT.getVectorNumElements(), DL, MVT::i64));
9810  Lo = DAG.getNode(N->getOpcode(), DL, LoVT, Lo);
9811  Hi = DAG.getNode(N->getOpcode(), DL, HiVT, Hi);
9812 
9813  // Now combine the parts back together so we still have a single result
9814  // like the combiner expects.
9815  return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
9816 }
9817 
9819  SDValue SplatVal, unsigned NumVecElts) {
9820  unsigned OrigAlignment = St.getAlignment();
9821  unsigned EltOffset = SplatVal.getValueType().getSizeInBits() / 8;
9822 
9823  // Create scalar stores. This is at least as good as the code sequence for a
9824  // split unaligned store which is a dup.s, ext.b, and two stores.
9825  // Most of the time the three stores should be replaced by store pair
9826  // instructions (stp).
9827  SDLoc DL(&St);
9828  SDValue BasePtr = St.getBasePtr();
9829  uint64_t BaseOffset = 0;
9830 
9831  const MachinePointerInfo &PtrInfo = St.getPointerInfo();
9832  SDValue NewST1 =
9833  DAG.getStore(St.getChain(), DL, SplatVal, BasePtr, PtrInfo,
9834  OrigAlignment, St.getMemOperand()->getFlags());
9835 
9836  // As this in ISel, we will not merge this add which may degrade results.
9837  if (BasePtr->getOpcode() == ISD::ADD &&
9838  isa<ConstantSDNode>(BasePtr->getOperand(1))) {
9839  BaseOffset = cast<ConstantSDNode>(BasePtr->getOperand(1))->getSExtValue();
9840  BasePtr = BasePtr->getOperand(0);
9841  }
9842 
9843  unsigned Offset = EltOffset;
9844  while (--NumVecElts) {
9845  unsigned Alignment = MinAlign(OrigAlignment, Offset);
9846  SDValue OffsetPtr =
9847  DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
9848  DAG.getConstant(BaseOffset + Offset, DL, MVT::i64));
9849  NewST1 = DAG.getStore(NewST1.getValue(0), DL, SplatVal, OffsetPtr,
9850  PtrInfo.getWithOffset(Offset), Alignment,
9851  St.getMemOperand()->getFlags());
9852  Offset += EltOffset;
9853  }
9854  return NewST1;
9855 }
9856 
9857 /// Replace a splat of zeros to a vector store by scalar stores of WZR/XZR. The
9858 /// load store optimizer pass will merge them to store pair stores. This should
9859 /// be better than a movi to create the vector zero followed by a vector store
9860 /// if the zero constant is not re-used, since one instructions and one register
9861 /// live range will be removed.
9862 ///
9863 /// For example, the final generated code should be:
9864 ///
9865 /// stp xzr, xzr, [x0]
9866 ///
9867 /// instead of:
9868 ///
9869 /// movi v0.2d, #0
9870 /// str q0, [x0]
9871 ///
9873  SDValue StVal = St.getValue();
9874  EVT VT = StVal.getValueType();
9875 
9876  // It is beneficial to scalarize a zero splat store for 2 or 3 i64 elements or
9877  // 2, 3 or 4 i32 elements.
9878  int NumVecElts = VT.getVectorNumElements();
9879  if (!(((NumVecElts == 2 || NumVecElts == 3) &&
9880  VT.getVectorElementType().getSizeInBits() == 64) ||
9881  ((NumVecElts == 2 || NumVecElts == 3 || NumVecElts == 4) &&
9882  VT.getVectorElementType().getSizeInBits() == 32)))
9883  return SDValue();
9884 
9885  if (StVal.getOpcode() != ISD::BUILD_VECTOR)
9886  return SDValue();
9887 
9888  // If the zero constant has more than one use then the vector store could be
9889  // better since the constant mov will be amortized and stp q instructions
9890  // should be able to be formed.
9891  if (!StVal.hasOneUse())
9892  return SDValue();
9893 
9894  // If the immediate offset of the address operand is too large for the stp
9895  // instruction, then bail out.
9896  if (DAG.isBaseWithConstantOffset(St.getBasePtr())) {
9897  int64_t Offset = St.getBasePtr()->getConstantOperandVal(1);
9898  if (Offset < -512 || Offset > 504)
9899  return SDValue();
9900  }
9901 
9902  for (int I = 0; I < NumVecElts; ++I) {
9903  SDValue EltVal = StVal.getOperand(I);
9904  if (!isNullConstant(EltVal) && !isNullFPConstant(EltVal))
9905  return SDValue();
9906  }
9907 
9908  // Use a CopyFromReg WZR/XZR here to prevent
9909  // DAGCombiner::MergeConsecutiveStores from undoing this transformation.
9910  SDLoc DL(&St);
9911  unsigned ZeroReg;
9912  EVT ZeroVT;
9913  if (VT.getVectorElementType().getSizeInBits() == 32) {
9914  ZeroReg = AArch64::WZR;
9915  ZeroVT = MVT::i32;
9916  } else {
9917  ZeroReg = AArch64::XZR;
9918  ZeroVT = MVT::i64;
9919  }
9920  SDValue SplatVal =
9921  DAG.getCopyFromReg(DAG.getEntryNode(), DL, ZeroReg, ZeroVT);
9922  return splitStoreSplat(DAG, St, SplatVal, NumVecElts);
9923 }
9924 
9925 /// Replace a splat of a scalar to a vector store by scalar stores of the scalar
9926 /// value. The load store optimizer pass will merge them to store pair stores.
9927 /// This has better performance than a splat of the scalar followed by a split
9928 /// vector store. Even if the stores are not merged it is four stores vs a dup,
9929 /// followed by an ext.b and two stores.
9931  SDValue StVal = St.getValue();
9932  EVT VT = StVal.getValueType();
9933 
9934  // Don't replace floating point stores, they possibly won't be transformed to
9935  // stp because of the store pair suppress pass.
9936  if (VT.isFloatingPoint())
9937  return SDValue();
9938 
9939  // We can express a splat as store pair(s) for 2 or 4 elements.
9940  unsigned NumVecElts = VT.getVectorNumElements();
9941  if (NumVecElts != 4 && NumVecElts != 2)
9942  return SDValue();
9943 
9944  // Check that this is a splat.
9945  // Make sure that each of the relevant vector element locations are inserted
9946  // to, i.e. 0 and 1 for v2i64 and 0, 1, 2, 3 for v4i32.
9947  std::bitset<4> IndexNotInserted((1 << NumVecElts) - 1);
9948  SDValue SplatVal;
9949  for (unsigned I = 0; I < NumVecElts; ++I) {
9950  // Check for insert vector elements.
9951  if (StVal.getOpcode() != ISD::INSERT_VECTOR_ELT)
9952  return SDValue();
9953 
9954  // Check that same value is inserted at each vector element.
9955  if (I == 0)
9956  SplatVal = StVal.getOperand(1);
9957  else if (StVal.getOperand(1) != SplatVal)
9958  return SDValue();
9959 
9960  // Check insert element index.
9961  ConstantSDNode *CIndex = dyn_cast<ConstantSDNode>(StVal.getOperand(2));
9962  if (!CIndex)
9963  return SDValue();
9964  uint64_t IndexVal = CIndex->getZExtValue();
9965  if (IndexVal >= NumVecElts)
9966  return SDValue();
9967  IndexNotInserted.reset(IndexVal);
9968 
9969  StVal = StVal.getOperand(0);
9970  }
9971  // Check that all vector element locations were inserted to.
9972  if (IndexNotInserted.any())
9973  return SDValue();
9974 
9975  return splitStoreSplat(DAG, St, SplatVal, NumVecElts);
9976 }
9977 
9979  SelectionDAG &DAG,
9980  const AArch64Subtarget *Subtarget) {
9981 
9982  StoreSDNode *S = cast<StoreSDNode>(N);
9983  if (S->isVolatile() || S->isIndexed())
9984  return SDValue();
9985 
9986  SDValue StVal = S->getValue();
9987  EVT VT = StVal.getValueType();
9988  if (!VT.isVector())
9989  return SDValue();
9990 
9991  // If we get a splat of zeros, convert this vector store to a store of
9992  // scalars. They will be merged into store pairs of xzr thereby removing one
9993  // instruction and one register.
9994  if (SDValue ReplacedZeroSplat = replaceZeroVectorStore(DAG, *S))
9995  return ReplacedZeroSplat;
9996 
9997  // FIXME: The logic for deciding if an unaligned store should be split should
9998  // be included in TLI.allowsMisalignedMemoryAccesses(), and there should be
9999  // a call to that function here.
10000 
10001  if (!Subtarget->isMisaligned128StoreSlow())
10002  return SDValue();
10003 
10004  // Don't split at -Oz.
10006  return SDValue();
10007 
10008  // Don't split v2i64 vectors. Memcpy lowering produces those and splitting
10009  // those up regresses performance on micro-benchmarks and olden/bh.
10010  if (VT.getVectorNumElements() < 2 || VT == MVT::v2i64)
10011  return SDValue();
10012 
10013  // Split unaligned 16B stores. They are terrible for performance.
10014  // Don't split stores with alignment of 1 or 2. Code that uses clang vector
10015  // extensions can use this to mark that it does not want splitting to happen
10016  // (by underspecifying alignment to be 1 or 2). Furthermore, the chance of
10017  // eliminating alignment hazards is only 1 in 8 for alignment of 2.
10018  if (VT.getSizeInBits() != 128 || S->getAlignment() >= 16 ||
10019  S->getAlignment() <= 2)
10020  return SDValue();
10021 
10022  // If we get a splat of a scalar convert this vector store to a store of
10023  // scalars. They will be merged into store pairs thereby removing two
10024  // instructions.
10025  if (SDValue ReplacedSplat = replaceSplatVectorStore(DAG, *S))
10026  return ReplacedSplat;
10027 
10028  SDLoc DL(S);
10029  unsigned NumElts = VT.getVectorNumElements() / 2;
10030  // Split VT into two.
10031  EVT HalfVT =
10032  EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), NumElts);
10033  SDValue SubVector0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, StVal,
10034  DAG.getConstant(0, DL, MVT::i64));
10035  SDValue SubVector1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, StVal,
10036  DAG.getConstant(NumElts, DL, MVT::i64));
10037  SDValue BasePtr = S->getBasePtr();
10038  SDValue NewST1 =
10039  DAG.getStore(S->getChain(), DL, SubVector0, BasePtr, S->getPointerInfo(),
10040  S->getAlignment(), S->getMemOperand()->getFlags());
10041  SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
10042  DAG.getConstant(8, DL, MVT::i64));
10043  return DAG.getStore(NewST1.getValue(0), DL, SubVector1, OffsetPtr,
10044  S->getPointerInfo(), S->getAlignment(),
10045  S->getMemOperand()->getFlags());
10046 }
10047 
10048 /// Target-specific DAG combine function for post-increment LD1 (lane) and
10049 /// post-increment LD1R.
10052  bool IsLaneOp) {
10053  if (DCI.isBeforeLegalizeOps())
10054  return SDValue();
10055 
10056  SelectionDAG &DAG = DCI.DAG;
10057  EVT VT = N->getValueType(0);
10058 
10059  unsigned LoadIdx = IsLaneOp ? 1 : 0;
10060  SDNode *LD = N->getOperand(LoadIdx).getNode();
10061  // If it is not LOAD, can not do such combine.
10062  if (LD->getOpcode() != ISD::LOAD)
10063  return SDValue();
10064 
10065  // The vector lane must be a constant in the LD1LANE opcode.
10066  SDValue Lane;
10067  if (IsLaneOp) {
10068  Lane = N->getOperand(2);
10069  auto *LaneC = dyn_cast<ConstantSDNode>(Lane);
10070  if (!LaneC || LaneC->getZExtValue() >= VT.getVectorNumElements())
10071  return SDValue();
10072  }
10073 
10074  LoadSDNode *LoadSDN = cast<LoadSDNode>(LD);
10075  EVT MemVT = LoadSDN->getMemoryVT();
10076  // Check if memory operand is the same type as the vector element.
10077  if (MemVT != VT.getVectorElementType())
10078  return SDValue();
10079 
10080  // Check if there are other uses. If so, do not combine as it will introduce
10081  // an extra load.
10082  for (SDNode::use_iterator UI = LD->use_begin(), UE = LD->use_end(); UI != UE;
10083  ++UI) {
10084  if (UI.getUse().getResNo() == 1) // Ignore uses of the chain result.
10085  continue;
10086  if (*UI != N)
10087  return SDValue();
10088  }
10089 
10090  SDValue Addr = LD->getOperand(1);
10091  SDValue Vector = N->getOperand(0);
10092  // Search for a use of the address operand that is an increment.
10093  for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), UE =
10094  Addr.getNode()->use_end(); UI != UE; ++UI) {
10095  SDNode *User = *UI;
10096  if (User->getOpcode() != ISD::ADD
10097  || UI.getUse().getResNo() != Addr.getResNo())
10098  continue;
10099 
10100  // Check that the add is independent of the load. Otherwise, folding it
10101  // would create a cycle.
10102  if (User->isPredecessorOf(LD) || LD->isPredecessorOf(User))
10103  continue;
10104  // Also check that add is not used in the vector operand. This would also
10105  // create a cycle.
10106  if (User->isPredecessorOf(Vector.getNode()))
10107  continue;
10108 
10109  // If the increment is a constant, it must match the memory ref size.
10110  SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
10111  if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
10112  uint32_t IncVal = CInc->getZExtValue();
10113  unsigned NumBytes = VT.getScalarSizeInBits() / 8;
10114  if (IncVal != NumBytes)
10115  continue;
10116  Inc = DAG.getRegister(AArch64::XZR, MVT::i64);
10117  }
10118 
10119  // Finally, check that the vector doesn't depend on the load.
10120  // Again, this would create a cycle.
10121  // The load depending on the vector is fine, as that's the case for the
10122  // LD1*post we'll eventually generate anyway.
10123  if (LoadSDN->isPredecessorOf(Vector.getNode()))
10124  continue;
10125 
10127  Ops.push_back(LD->getOperand(0)); // Chain
10128  if (IsLaneOp) {
10129  Ops.push_back(Vector); // The vector to be inserted
10130  Ops.push_back(Lane); // The lane to be inserted in the vector
10131  }
10132  Ops.push_back(Addr);
10133  Ops.push_back(Inc);
10134 
10135  EVT Tys[3] = { VT, MVT::i64, MVT::Other };
10136  SDVTList SDTys = DAG.getVTList(Tys);
10137  unsigned NewOp = IsLaneOp ? AArch64ISD::LD1LANEpost : AArch64ISD::LD1DUPpost;
10138  SDValue UpdN = DAG.getMemIntrinsicNode(NewOp, SDLoc(N), SDTys, Ops,
10139  MemVT,
10140  LoadSDN->getMemOperand());
10141 
10142  // Update the uses.
10143  SDValue NewResults[] = {
10144  SDValue(LD, 0), // The result of load
10145  SDValue(UpdN.getNode(), 2) // Chain
10146  };
10147  DCI.CombineTo(LD, NewResults);
10148  DCI.CombineTo(N, SDValue(UpdN.getNode(), 0)); // Dup/Inserted Result
10149  DCI.CombineTo(User, SDValue(UpdN.getNode(), 1)); // Write back register
10150 
10151  break;
10152  }
10153  return SDValue();
10154 }
10155 
10156 /// Simplify ``Addr`` given that the top byte of it is ignored by HW during
10157 /// address translation.
10160  SelectionDAG &DAG) {
10161  APInt DemandedMask = APInt::getLowBitsSet(64, 56);
10162  KnownBits Known;
10164  !DCI.isBeforeLegalizeOps());
10165  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10166  if (TLI.SimplifyDemandedBits(Addr, DemandedMask, Known, TLO)) {
10167  DCI.CommitTargetLoweringOpt(TLO);
10168  return true;
10169  }
10170  return false;
10171 }
10172 
10175  SelectionDAG &DAG,
10176  const AArch64Subtarget *Subtarget) {
10177  if (SDValue Split = splitStores(N, DCI, DAG, Subtarget))
10178  return Split;
10179 
10180  if (Subtarget->supportsAddressTopByteIgnored() &&
10181  performTBISimplification(N->getOperand(2), DCI, DAG))
10182  return SDValue(N, 0);
10183 
10184  return SDValue();
10185 }
10186 
10187 
10188 /// Target-specific DAG combine function for NEON load/store intrinsics
10189 /// to merge base address updates.
10192  SelectionDAG &DAG) {
10193  if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10194  return SDValue();
10195 
10196  unsigned AddrOpIdx = N->getNumOperands() - 1;
10197  SDValue Addr = N->getOperand(AddrOpIdx);
10198 
10199  // Search for a use of the address operand that is an increment.
10200  for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
10201  UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
10202  SDNode *User = *UI;
10203  if (User->getOpcode() != ISD::ADD ||
10204  UI.getUse().getResNo() != Addr.getResNo())
10205  continue;
10206 
10207  // Check that the add is independent of the load/store. Otherwise, folding
10208  // it would create a cycle.
10209  if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
10210  continue;
10211 
10212  // Find the new opcode for the updating load/store.
10213  bool IsStore = false;
10214  bool IsLaneOp = false;
10215  bool IsDupOp = false;
10216  unsigned NewOpc = 0;
10217  unsigned NumVecs = 0;
10218  unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
10219  switch (IntNo) {
10220  default: llvm_unreachable("unexpected intrinsic for Neon base update");
10221  case Intrinsic::aarch64_neon_ld2: NewOpc = AArch64ISD::LD2post;
10222  NumVecs = 2; break;
10223  case Intrinsic::aarch64_neon_ld3: NewOpc = AArch64ISD::LD3post;
10224  NumVecs = 3; break;
10225  case Intrinsic::aarch64_neon_ld4: NewOpc = AArch64ISD::LD4post;
10226  NumVecs = 4; break;
10227  case Intrinsic::aarch64_neon_st2: NewOpc = AArch64ISD::ST2post;
10228  NumVecs = 2; IsStore = true; break;
10229  case Intrinsic::aarch64_neon_st3: NewOpc = AArch64ISD::ST3post;
10230  NumVecs = 3; IsStore = true; break;
10231  case Intrinsic::aarch64_neon_st4: NewOpc = AArch64ISD::ST4post;
10232  NumVecs = 4; IsStore = true; break;
10233  case Intrinsic::aarch64_neon_ld1x2: NewOpc = AArch64ISD::LD1x2post;
10234  NumVecs = 2; break;
10235  case Intrinsic::aarch64_neon_ld1x3: NewOpc = AArch64ISD::LD1x3post;
10236  NumVecs = 3; break;
10237  case Intrinsic::aarch64_neon_ld1x4: NewOpc = AArch64ISD::LD1x4post;
10238  NumVecs = 4; break;
10239  case Intrinsic::aarch64_neon_st1x2: NewOpc = AArch64ISD::ST1x2post;
10240  NumVecs = 2; IsStore = true; break;
10241  case Intrinsic::aarch64_neon_st1x3: NewOpc = AArch64ISD::ST1x3post;
10242  NumVecs = 3; IsStore = true; break;
10243  case Intrinsic::aarch64_neon_st1x4: NewOpc = AArch64ISD::ST1x4post;
10244  NumVecs = 4; IsStore = true; break;
10245  case Intrinsic::aarch64_neon_ld2r: NewOpc = AArch64ISD::LD2DUPpost;
10246  NumVecs = 2; IsDupOp = true; break;
10247  case Intrinsic::aarch64_neon_ld3r: NewOpc = AArch64ISD::LD3DUPpost;
10248  NumVecs = 3; IsDupOp = true; break;
10249  case Intrinsic::aarch64_neon_ld4r: NewOpc = AArch64ISD::LD4DUPpost;
10250  NumVecs = 4; IsDupOp = true; break;
10251  case Intrinsic::aarch64_neon_ld2lane: NewOpc = AArch64ISD::LD2LANEpost;
10252  NumVecs = 2; IsLaneOp = true; break;
10253  case Intrinsic::aarch64_neon_ld3lane: NewOpc = AArch64ISD::LD3LANEpost;
10254  NumVecs = 3; IsLaneOp = true; break;
10255  case Intrinsic::aarch64_neon_ld4lane: NewOpc = AArch64ISD::LD4LANEpost;
10256  NumVecs = 4; IsLaneOp = true; break;
10257  case Intrinsic::aarch64_neon_st2lane: NewOpc = AArch64ISD::ST2LANEpost;
10258  NumVecs = 2; IsStore = true; IsLaneOp = true; break;
10259  case Intrinsic::aarch64_neon_st3lane: NewOpc = AArch64ISD::ST3LANEpost;
10260  NumVecs = 3; IsStore = true; IsLaneOp = true; break;
10261  case Intrinsic::aarch64_neon_st4lane: NewOpc = AArch64ISD::ST4LANEpost;
10262  NumVecs = 4; IsStore = true; IsLaneOp = true; break;
10263  }
10264 
10265  EVT VecTy;
10266  if (IsStore)
10267  VecTy = N->getOperand(2).getValueType();
10268  else
10269  VecTy = N->getValueType(0);
10270 
10271  // If the increment is a constant, it must match the memory ref size.
10272  SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
10273  if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
10274  uint32_t IncVal = CInc->getZExtValue();
10275  unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
10276  if (IsLaneOp || IsDupOp)
10277  NumBytes /= VecTy.getVectorNumElements();
10278  if (IncVal != NumBytes)
10279  continue;
10280  Inc = DAG.getRegister(AArch64::XZR, MVT::i64);
10281  }
10283  Ops.push_back(N->getOperand(0)); // Incoming chain
10284  // Load lane and store have vector list as input.
10285  if (IsLaneOp || IsStore)
10286  for (unsigned i = 2; i < AddrOpIdx; ++i)
10287  Ops.push_back(N->getOperand(i));
10288  Ops.push_back(Addr); // Base register
10289  Ops.push_back(Inc);
10290 
10291  // Return Types.
10292  EVT Tys[6];
10293  unsigned NumResultVecs = (IsStore ? 0 : NumVecs);
10294  unsigned n;
10295  for (n = 0; n < NumResultVecs; ++n)
10296  Tys[n] = VecTy;
10297  Tys[n++] = MVT::i64; // Type of write back register
10298  Tys[n] = MVT::Other; // Type of the chain
10299  SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs + 2));
10300 
10301  MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
10302  SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys, Ops,
10303  MemInt->getMemoryVT(),
10304  MemInt->getMemOperand());
10305 
10306  // Update the uses.
10307  std::vector<SDValue> NewResults;
10308  for (unsigned i = 0; i < NumResultVecs; ++i) {
10309  NewResults.push_back(SDValue(UpdN.getNode(), i));
10310  }
10311  NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs + 1));
10312  DCI.CombineTo(N, NewResults);
10313  DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
10314 
10315  break;
10316  }
10317  return SDValue();
10318 }
10319 
10320 // Checks to see if the value is the prescribed width and returns information
10321 // about its extension mode.
10322 static
10324  ExtType = ISD::NON_EXTLOAD;
10325  switch(V.getNode()->getOpcode()) {
10326  default:
10327  return false;
10328  case ISD::LOAD: {
10329  LoadSDNode *LoadNode = cast<LoadSDNode>(V.getNode());
10330  if ((LoadNode->getMemoryVT() == MVT::i8 && width == 8)
10331  || (LoadNode->getMemoryVT() == MVT::i16 && width == 16)) {
10332  ExtType = LoadNode->getExtensionType();
10333  return true;
10334  }
10335  return false;
10336  }
10337  case ISD::AssertSext: {
10338  VTSDNode *TypeNode = cast<VTSDNode>(V.getNode()->getOperand(1));
10339  if ((TypeNode->getVT() == MVT::i8 && width == 8)
10340  || (TypeNode->getVT() == MVT::i16 && width == 16)) {
10341  ExtType = ISD::SEXTLOAD;
10342  return true;
10343  }
10344  return false;
10345  }
10346  case ISD::AssertZext: {
10347  VTSDNode *TypeNode = cast<VTSDNode>(V.getNode()->getOperand(1));
10348  if ((TypeNode->getVT() == MVT::i8 && width == 8)
10349  || (TypeNode->getVT() == MVT::i16 && width == 16)) {
10350  ExtType = ISD::ZEXTLOAD;
10351  return true;
10352  }
10353  return false;
10354  }
10355  case ISD::Constant:
10356  case ISD::TargetConstant: {
10357  return std::abs(cast<ConstantSDNode>(V.getNode())->getSExtValue()) <
10358  1LL << (width - 1);
10359  }
10360  }
10361 
10362  return true;
10363 }
10364 
10365 // This function does a whole lot of voodoo to determine if the tests are
10366 // equivalent without and with a mask. Essentially what happens is that given a
10367 // DAG resembling:
10368 //
10369 // +-------------+ +-------------+ +-------------+ +-------------+
10370 // | Input | | AddConstant | | CompConstant| | CC |
10371 // +-------------+ +-------------+ +-------------+ +-------------+
10372 // | | | |
10373 // V V | +----------+
10374 // +-------------+ +----+ | |
10375 // | ADD | |0xff| | |
10376 // +-------------+ +----+ | |
10377 // | | | |
10378 // V V | |
10379 // +-------------+ | |
10380 // | AND | | |
10381 // +-------------+ | |
10382 // | | |
10383 // +-----+ | |
10384 // | | |
10385 // V V V
10386 // +-------------+
10387 // | CMP |
10388 // +-------------+
10389 //
10390 // The AND node may be safely removed for some combinations of inputs. In
10391 // particular we need to take into account the extension type of the Input,
10392 // the exact values of AddConstant, CompConstant, and CC, along with the nominal
10393 // width of the input (this can work for any width inputs, the above graph is
10394 // specific to 8 bits.
10395 //
10396 // The specific equations were worked out by generating output tables for each
10397 // AArch64CC value in terms of and AddConstant (w1), CompConstant(w2). The
10398 // problem was simplified by working with 4 bit inputs, which means we only
10399 // needed to reason about 24 distinct bit patterns: 8 patterns unique to zero
10400 // extension (8,15), 8 patterns unique to sign extensions (-8,-1), and 8
10401 // patterns present in both extensions (0,7). For every distinct set of
10402 // AddConstant and CompConstants bit patterns we can consider the masked and
10403 // unmasked versions to be equivalent if the result of this function is true for
10404 // all 16 distinct bit patterns of for the current extension type of Input (w0).
10405 //
10406 // sub w8, w0, w1
10407 // and w10, w8, #0x0f
10408 // cmp w8, w2
10409 // cset w9, AArch64CC
10410 // cmp w10, w2
10411 // cset w11, AArch64CC
10412 // cmp w9, w11
10413 // cset w0, eq
10414 // ret
10415 //
10416 // Since the above function shows when the outputs are equivalent it defines
10417 // when it is safe to remove the AND. Unfortunately it only runs on AArch64 and
10418 // would be expensive to run during compiles. The equations below were written
10419 // in a test harness that confirmed they gave equivalent outputs to the above
10420 // for all inputs function, so they can be used determine if the removal is
10421 // legal instead.
10422 //
10423 // isEquivalentMaskless() is the code for testing if the AND can be removed
10424 // factored out of the DAG recognition as the DAG can take several forms.
10425 
10426 static bool isEquivalentMaskless(unsigned CC, unsigned width,
10427  ISD::LoadExtType ExtType, int AddConstant,
10428  int CompConstant) {
10429  // By being careful about our equations and only writing the in term
10430  // symbolic values and well known constants (0, 1, -1, MaxUInt) we can
10431  // make them generally applicable to all bit widths.
10432  int MaxUInt = (1 << width);
10433 
10434  // For the purposes of these comparisons sign extending the type is
10435  // equivalent to zero extending the add and displacing it by half the integer
10436  // width. Provided we are careful and make sure our equations are valid over
10437  // the whole range we can just adjust the input and avoid writing equations
10438  // for sign extended inputs.
10439  if (ExtType == ISD::SEXTLOAD)
10440  AddConstant -= (1 << (width-1));
10441 
10442  switch(CC) {
10443  case AArch64CC::LE:
10444  case AArch64CC::GT:
10445  if ((AddConstant == 0) ||
10446  (CompConstant == MaxUInt - 1 && AddConstant < 0) ||
10447  (AddConstant >= 0 && CompConstant < 0) ||
10448  (AddConstant <= 0 && CompConstant <= 0 && CompConstant < AddConstant))
10449  return true;
10450  break;
10451  case AArch64CC::LT:
10452  case AArch64CC::GE:
10453  if ((AddConstant == 0) ||
10454  (AddConstant >= 0 && CompConstant <= 0) ||
10455  (AddConstant <= 0 && CompConstant <= 0 && CompConstant <= AddConstant))
10456  return true;
10457  break;
10458  case AArch64CC::HI:
10459  case AArch64CC::LS:
10460  if ((AddConstant >= 0 && CompConstant < 0) ||
10461  (AddConstant <= 0 && CompConstant >= -1 &&
10462  CompConstant < AddConstant + MaxUInt))
10463  return true;
10464  break;
10465  case AArch64CC::PL:
10466  case AArch64CC::MI:
10467  if ((AddConstant == 0) ||
10468  (AddConstant > 0 && CompConstant <= 0) ||
10469  (AddConstant < 0 && CompConstant <= AddConstant))
10470  return true;
10471  break;
10472  case AArch64CC::LO:
10473  case AArch64CC::HS:
10474  if ((AddConstant >= 0 && CompConstant <= 0) ||
10475  (AddConstant <= 0 && CompConstant >= 0 &&
10476  CompConstant <= AddConstant + MaxUInt))
10477  return true;
10478  break;
10479  case AArch64CC::EQ:
10480  case AArch64CC::NE:
10481  if ((AddConstant > 0 && CompConstant < 0) ||
10482  (AddConstant < 0 && CompConstant >= 0 &&
10483  CompConstant < AddConstant + MaxUInt) ||
10484  (AddConstant >= 0 && CompConstant >= 0 &&
10485  CompConstant >= AddConstant) ||
10486  (AddConstant <= 0 && CompConstant < 0 && CompConstant < AddConstant))
10487  return true;
10488  break;
10489  case AArch64CC::VS:
10490  case AArch64CC::VC:
10491  case AArch64CC::AL:
10492  case AArch64CC::NV:
10493  return true;
10494  case AArch64CC::Invalid:
10495  break;
10496  }
10497 
10498  return false;
10499 }
10500 
10501 static
10504  SelectionDAG &DAG, unsigned CCIndex,
10505  unsigned CmpIndex) {
10506  unsigned CC = cast<ConstantSDNode>(N->getOperand(CCIndex))->getSExtValue();
10507  SDNode *SubsNode = N->getOperand(CmpIndex).getNode();
10508  unsigned CondOpcode = SubsNode->getOpcode();
10509 
10510  if (CondOpcode != AArch64ISD::SUBS)
10511  return SDValue();
10512 
10513  // There is a SUBS feeding this condition. Is it fed by a mask we can
10514  // use?
10515 
10516  SDNode *AndNode = SubsNode->getOperand(0).getNode();
10517  unsigned MaskBits = 0;
10518 
10519  if (AndNode->getOpcode() != ISD::AND)
10520  return SDValue();
10521 
10522  if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(AndNode->getOperand(1))) {
10523  uint32_t CNV = CN->getZExtValue();
10524  if (CNV == 255)
10525  MaskBits = 8;
10526  else if (CNV == 65535)
10527  MaskBits = 16;
10528  }
10529 
10530  if (!MaskBits)
10531  return SDValue();
10532 
10533  SDValue AddValue = AndNode->getOperand(0);
10534 
10535  if (AddValue.getOpcode() != ISD::ADD)
10536  return SDValue();
10537 
10538  // The basic dag structure is correct, grab the inputs and validate them.
10539 
10540  SDValue AddInputValue1 = AddValue.getNode()->getOperand(0);
10541  SDValue AddInputValue2 = AddValue.getNode()->getOperand(1);
10542  SDValue SubsInputValue = SubsNode->getOperand(1);
10543 
10544  // The mask is present and the provenance of all the values is a smaller type,
10545  // lets see if the mask is superfluous.
10546 
10547  if (!isa<ConstantSDNode>(AddInputValue2.getNode()) ||
10548  !isa<ConstantSDNode>(SubsInputValue.getNode()))
10549  return SDValue();
10550 
10552 
10553  if (!checkValueWidth(SubsInputValue, MaskBits, ExtType) ||
10554  !checkValueWidth(AddInputValue2, MaskBits, ExtType) ||
10555  !checkValueWidth(AddInputValue1, MaskBits, ExtType) )
10556  return SDValue();
10557 
10558  if(!isEquivalentMaskless(CC, MaskBits, ExtType,
10559  cast<ConstantSDNode>(AddInputValue2.getNode())->getSExtValue(),
10560  cast<ConstantSDNode>(SubsInputValue.getNode())->getSExtValue()))
10561  return SDValue();
10562 
10563  // The AND is not necessary, remove it.
10564 
10565  SDVTList VTs = DAG.getVTList(SubsNode->getValueType(0),
10566  SubsNode->getValueType(1));
10567  SDValue Ops[] = { AddValue, SubsNode->getOperand(1) };
10568 
10569  SDValue NewValue = DAG.getNode(CondOpcode, SDLoc(SubsNode), VTs, Ops);
10570  DAG.ReplaceAllUsesWith(SubsNode, NewValue.getNode());
10571 
10572  return SDValue(N, 0);
10573 }
10574 
10575 // Optimize compare with zero and branch.
10578  SelectionDAG &DAG) {
10579  if (SDValue NV = performCONDCombine(N, DCI, DAG, 2, 3))
10580  N = NV.getNode();
10581  SDValue Chain = N->getOperand(0);
10582  SDValue Dest = N->getOperand(1);
10583  SDValue CCVal = N->getOperand(2);
10584  SDValue Cmp = N->getOperand(3);
10585 
10586  assert(isa<ConstantSDNode>(CCVal) && "Expected a ConstantSDNode here!");
10587  unsigned CC = cast<ConstantSDNode>(CCVal)->getZExtValue();
10588  if (CC != AArch64CC::EQ && CC != AArch64CC::NE)
10589  return SDValue();
10590 
10591  unsigned CmpOpc = Cmp.getOpcode();
10592  if (CmpOpc != AArch64ISD::ADDS && CmpOpc != AArch64ISD::SUBS)
10593  return SDValue();
10594 
10595  // Only attempt folding if there is only one use of the flag and no use of the
10596  // value.
10597  if (!Cmp->hasNUsesOfValue(0, 0) || !Cmp->hasNUsesOfValue(1, 1))
10598  return SDValue();
10599 
10600  SDValue LHS = Cmp.getOperand(0);
10601  SDValue RHS = Cmp.getOperand(1);
10602 
10603  assert(LHS.getValueType() == RHS.getValueType() &&
10604  "Expected the value type to be the same for both operands!");
10605  if (LHS.getValueType() != MVT::i32 && LHS.getValueType() != MVT::i64)
10606  return SDValue();
10607 
10608  if (isNullConstant(LHS))
10609  std::swap(LHS, RHS);
10610 
10611  if (!isNullConstant(RHS))
10612  return SDValue();
10613 
10614  if (LHS.getOpcode() == ISD::SHL || LHS.getOpcode() == ISD::SRA ||
10615  LHS.getOpcode() == ISD::SRL)
10616  return SDValue();
10617 
10618  // Fold the compare into the branch instruction.
10619  SDValue BR;
10620  if (CC == AArch64CC::EQ)
10621  BR = DAG.getNode(AArch64ISD::CBZ, SDLoc(N), MVT::Other, Chain, LHS, Dest);
10622  else
10623  BR = DAG.getNode(AArch64ISD::CBNZ, SDLoc(N), MVT::Other, Chain, LHS, Dest);
10624 
10625  // Do not add new nodes to DAG combiner worklist.
10626  DCI.CombineTo(N, BR, false);
10627 
10628  return SDValue();
10629 }
10630 
10631 // Optimize some simple tbz/tbnz cases. Returns the new operand and bit to test
10632 // as well as whether the test should be inverted. This code is required to
10633 // catch these cases (as opposed to standard dag combines) because
10634 // AArch64ISD::TBZ is matched during legalization.
10635 static SDValue getTestBitOperand(SDValue Op, unsigned &Bit, bool &Invert,
10636  SelectionDAG &DAG) {
10637 
10638  if (!Op->hasOneUse())
10639  return Op;
10640 
10641  // We don't handle undef/constant-fold cases below, as they should have
10642  // already been taken care of (e.g. and of 0, test of undefined shifted bits,
10643  // etc.)
10644 
10645  // (tbz (trunc x), b) -> (tbz x, b)
10646  // This case is just here to enable more of the below cases to be caught.
10647  if (Op->getOpcode() == ISD::TRUNCATE &&
10648  Bit < Op->getValueType(0).getSizeInBits()) {
10649  return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG);
10650  }
10651 
10652  if (Op->getNumOperands() != 2)
10653  return Op;
10654 
10655  auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(1));
10656  if (!C)
10657  return Op;
10658 
10659  switch (Op->getOpcode()) {
10660  default:
10661  return Op;
10662 
10663  // (tbz (and x, m), b) -> (tbz x, b)
10664  case ISD::AND:
10665  if ((C->getZExtValue() >> Bit) & 1)
10666  return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG);
10667  return Op;
10668 
10669  // (tbz (shl x, c), b) -> (tbz x, b-c)
10670  case ISD::SHL:
10671  if (C->getZExtValue() <= Bit &&
10672  (Bit - C->getZExtValue()) < Op->getValueType(0).getSizeInBits()) {
10673  Bit = Bit - C->getZExtValue();
10674  return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG);
10675  }
10676  return Op;
10677 
10678  // (tbz (sra x, c), b) -> (tbz x, b+c) or (tbz x, msb) if b+c is > # bits in x
10679  case ISD::SRA:
10680  Bit = Bit + C->getZExtValue();
10681  if (Bit >= Op->getValueType(0).getSizeInBits())
10682  Bit = Op->getValueType(0).getSizeInBits() - 1;
10683  return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG);
10684 
10685  // (tbz (srl x, c), b) -> (tbz x, b+c)
10686  case ISD::SRL:
10687  if ((Bit + C->getZExtValue()) < Op->getValueType(0).getSizeInBits()) {
10688  Bit = Bit + C->getZExtValue();
10689  return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG);
10690  }
10691  return Op;
10692 
10693  // (tbz (xor x, -1), b) -> (tbnz x, b)
10694  case ISD::XOR:
10695  if ((C->getZExtValue() >> Bit) & 1)
10696  Invert = !Invert;
10697  return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG);
10698  }
10699 }
10700 
10701 // Optimize test single bit zero/non-zero and branch.
10704  SelectionDAG &DAG) {
10705  unsigned Bit = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
10706  bool Invert = false;
10707  SDValue TestSrc = N->getOperand(1);
10708  SDValue NewTestSrc = getTestBitOperand(TestSrc, Bit, Invert, DAG);
10709 
10710  if (TestSrc == NewTestSrc)
10711  return SDValue();
10712 
10713  unsigned NewOpc = N->getOpcode();
10714  if (Invert) {
10715  if (NewOpc == AArch64ISD::TBZ)
10716  NewOpc = AArch64ISD::TBNZ;
10717  else {
10718  assert(NewOpc == AArch64ISD::TBNZ);
10719  NewOpc = AArch64ISD::TBZ;
10720  }
10721  }
10722 
10723  SDLoc DL(N);
10724  return DAG.getNode(NewOpc, DL, MVT::Other, N->getOperand(0), NewTestSrc,
10725  DAG.getConstant(Bit, DL, MVT::i64), N->getOperand(3));
10726 }
10727 
10728 // vselect (v1i1 setcc) ->
10729 // vselect (v1iXX setcc) (XX is the size of the compared operand type)
10730 // FIXME: Currently the type legalizer can't handle VSELECT having v1i1 as
10731 // condition. If it can legalize "VSELECT v1i1" correctly, no need to combine
10732 // such VSELECT.
10734  SDValue N0 = N->getOperand(0);
10735  EVT CCVT = N0.getValueType();
10736 
10737  if (N0.getOpcode() != ISD::SETCC || CCVT.getVectorNumElements() != 1 ||
10738  CCVT.getVectorElementType() != MVT::i1)
10739  return SDValue();
10740 
10741  EVT ResVT = N->getValueType(0);
10742  EVT CmpVT = N0.getOperand(0).getValueType();
10743  // Only combine when the result type is of the same size as the compared
10744  // operands.
10745  if (ResVT.getSizeInBits() != CmpVT.getSizeInBits())
10746  return SDValue();
10747 
10748  SDValue IfTrue = N->getOperand(1);
10749  SDValue IfFalse = N->getOperand(2);
10750  SDValue SetCC =
10752  N0.getOperand(0), N0.getOperand(1),
10753  cast<CondCodeSDNode>(N0.getOperand(2))->get());
10754  return DAG.getNode(ISD::VSELECT, SDLoc(N), ResVT, SetCC,
10755  IfTrue, IfFalse);
10756 }
10757 
10758 /// A vector select: "(select vL, vR, (setcc LHS, RHS))" is best performed with
10759 /// the compare-mask instructions rather than going via NZCV, even if LHS and
10760 /// RHS are really scalar. This replaces any scalar setcc in the above pattern
10761 /// with a vector one followed by a DUP shuffle on the result.
10764  SelectionDAG &DAG = DCI.DAG;
10765  SDValue N0 = N->getOperand(0);
10766  EVT ResVT = N->getValueType(0);
10767 
10768  if (N0.getOpcode() != ISD::SETCC)
10769  return SDValue();
10770 
10771  // Make sure the SETCC result is either i1 (initial DAG), or i32, the lowered
10772  // scalar SetCCResultType. We also don't expect vectors, because we assume
10773  // that selects fed by vector SETCCs are canonicalized to VSELECT.
10774  assert((N0.getValueType() == MVT::i1 || N0.getValueType() == MVT::i32) &&
10775  "Scalar-SETCC feeding SELECT has unexpected result type!");
10776 
10777  // If NumMaskElts == 0, the comparison is larger than select result. The
10778  // largest real NEON comparison is 64-bits per lane, which means the result is
10779  // at most 32-bits and an illegal vector. Just bail out for now.
10780  EVT SrcVT = N0.getOperand(0).getValueType();
10781 
10782  // Don't try to do this optimization when the setcc itself has i1 operands.
10783  // There are no legal vectors of i1, so this would be pointless.
10784  if (SrcVT == MVT::i1)
10785  return SDValue();
10786 
10787  int NumMaskElts = ResVT.getSizeInBits() / SrcVT.getSizeInBits();
10788  if (!ResVT.isVector() || NumMaskElts == 0)
10789  return SDValue();
10790 
10791  SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumMaskElts);
10792  EVT CCVT = SrcVT.changeVectorElementTypeToInteger();
10793 
10794  // Also bail out if the vector CCVT isn't the same size as ResVT.
10795  // This can happen if the SETCC operand size doesn't divide the ResVT size
10796  // (e.g., f64 vs v3f32).
10797  if (CCVT.getSizeInBits() != ResVT.getSizeInBits())
10798  return SDValue();
10799 
10800  // Make sure we didn't create illegal types, if we're not supposed to.
10801  assert(DCI.isBeforeLegalize() ||
10802  DAG.getTargetLoweringInfo().isTypeLegal(SrcVT));
10803 
10804  // First perform a vector comparison, where lane 0 is the one we're interested
10805  // in.
10806  SDLoc DL(N0);
10807  SDValue LHS =
10808  DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, SrcVT, N0.getOperand(0));
10809  SDValue RHS =
10810  DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, SrcVT, N0.getOperand(1));
10811  SDValue SetCC = DAG.getNode(ISD::SETCC, DL, CCVT, LHS, RHS, N0.getOperand(2));
10812 
10813  // Now duplicate the comparison mask we want across all other lanes.
10814  SmallVector<int, 8> DUPMask(CCVT.getVectorNumElements(), 0);
10815  SDValue Mask = DAG.getVectorShuffle(CCVT, DL, SetCC, SetCC, DUPMask);
10816  Mask = DAG.getNode(ISD::BITCAST, DL,
10818 
10819  return DAG.getSelect(DL, ResVT, Mask, N->getOperand(1), N->getOperand(2));
10820 }
10821 
10822 /// Get rid of unnecessary NVCASTs (that don't change the type).
10824  if (N->getValueType(0) == N->getOperand(0).getValueType())
10825  return N->getOperand(0);
10826 
10827  return SDValue();
10828 }
10829 
10830 // If all users of the globaladdr are of the form (globaladdr + constant), find
10831 // the smallest constant, fold it into the globaladdr's offset and rewrite the
10832 // globaladdr as (globaladdr + constant) - constant.
10834  const AArch64Subtarget *Subtarget,
10835  const TargetMachine &TM) {
10836  auto *GN = dyn_cast<GlobalAddressSDNode>(N);
10837  if (!GN || Subtarget->ClassifyGlobalReference(GN->getGlobal(), TM) !=
10839  return SDValue();
10840 
10841  uint64_t MinOffset = -1ull;
10842  for (SDNode *N : GN->uses()) {
10843  if (N->getOpcode() != ISD::ADD)
10844  return SDValue();
10845  auto *C = dyn_cast<ConstantSDNode>(N->getOperand(0));
10846  if (!C)
10847  C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10848  if (!C)
10849  return SDValue();
10850  MinOffset = std::min(MinOffset, C->getZExtValue());
10851  }
10852  uint64_t Offset = MinOffset + GN->getOffset();
10853 
10854  // Require that the new offset is larger than the existing one. Otherwise, we
10855  // can end up oscillating between two possible DAGs, for example,
10856  // (add (add globaladdr + 10, -1), 1) and (add globaladdr + 9, 1).
10857  if (Offset <= uint64_t(GN->getOffset()))
10858  return SDValue();
10859 
10860  // Check whether folding this offset is legal. It must not go out of bounds of
10861  // the referenced object to avoid violating the code model, and must be
10862  // smaller than 2^21 because this is the largest offset expressible in all
10863  // object formats.
10864  //
10865  // This check also prevents us from folding negative offsets, which will end
10866  // up being treated in the same way as large positive ones. They could also
10867  // cause code model violations, and aren't really common enough to matter.
10868  if (Offset >= (1 << 21))
10869  return SDValue();
10870 
10871  const GlobalValue *GV = GN->getGlobal();
10872  Type *T = GV->getValueType();
10873  if (!T->isSized() ||
10874  Offset > GV->getParent()->getDataLayout().getTypeAllocSize(T))
10875  return SDValue();
10876 
10877  SDLoc DL(GN);
10878  SDValue Result = DAG.getGlobalAddress(GV, DL, MVT::i64, Offset);
10879  return DAG.getNode(ISD::SUB, DL, MVT::i64, Result,
10880  DAG.getConstant(MinOffset, DL, MVT::i64));
10881 }
10882 
10884  DAGCombinerInfo &DCI) const {
10885  SelectionDAG &DAG = DCI.DAG;
10886  switch (N->getOpcode()) {
10887  default:
10888  LLVM_DEBUG(dbgs() << "Custom combining: skipping\n");
10889  break;
10890  case ISD::ADD:
10891  case ISD::SUB:
10892  return performAddSubLongCombine(N, DCI, DAG);
10893  case ISD::XOR:
10894  return performXorCombine(N, DAG, DCI, Subtarget);
10895  case ISD::MUL:
10896  return performMulCombine(N, DAG, DCI, Subtarget);
10897  case ISD::SINT_TO_FP:
10898  case ISD::UINT_TO_FP:
10899  return performIntToFpCombine(N, DAG, Subtarget);
10900  case ISD::FP_TO_SINT:
10901  case ISD::FP_TO_UINT:
10902  return performFpToIntCombine(N, DAG, DCI, Subtarget);
10903  case ISD::FDIV:
10904  return performFDivCombine(N, DAG, DCI, Subtarget);
10905  case ISD::OR:
10906  return performORCombine(N, DCI, Subtarget);
10907  case ISD::SRL:
10908  return performSRLCombine(N, DCI);
10910  return performIntrinsicCombine(N, DCI, Subtarget);
10911  case ISD::ANY_EXTEND:
10912  case ISD::ZERO_EXTEND:
10913  case ISD::SIGN_EXTEND:
10914  return performExtendCombine(N, DCI, DAG);
10915  case ISD::BITCAST:
10916  return performBitcastCombine(N, DCI, DAG);
10917  case ISD::CONCAT_VECTORS:
10918  return performConcatVectorsCombine(N, DCI, DAG);
10919  case ISD::SELECT:
10920  return performSelectCombine(N, DCI);
10921  case ISD::VSELECT:
10922  return performVSelectCombine(N, DCI.DAG);
10923  case ISD::LOAD:
10924  if (performTBISimplification(N->getOperand(1), DCI, DAG))
10925  return SDValue(N, 0);
10926  break;
10927  case ISD::STORE:
10928  return performSTORECombine(N, DCI, DAG, Subtarget);
10929  case AArch64ISD::BRCOND:
10930  return performBRCONDCombine(N, DCI, DAG);
10931  case AArch64ISD::TBNZ:
10932  case AArch64ISD::TBZ:
10933  return performTBZCombine(N, DCI, DAG);
10934  case AArch64ISD::CSEL:
10935  return performCONDCombine(N, DCI, DAG, 2, 3);
10936  case AArch64ISD::DUP:
10937  return performPostLD1Combine(N, DCI, false);
10938  case AArch64ISD::NVCAST:
10939  return performNVCASTCombine(N);
10941  return performPostLD1Combine(N, DCI, true);
10942  case ISD::INTRINSIC_VOID:
10944  switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10945  case Intrinsic::aarch64_neon_ld2:
10946  case Intrinsic::aarch64_neon_ld3:
10947  case Intrinsic::aarch64_neon_ld4:
10948  case Intrinsic::aarch64_neon_ld1x2:
10949  case Intrinsic::aarch64_neon_ld1x3:
10950  case Intrinsic::aarch64_neon_ld1x4:
10951  case Intrinsic::aarch64_neon_ld2lane:
10952  case Intrinsic::aarch64_neon_ld3lane:
10953  case Intrinsic::aarch64_neon_ld4lane:
10954  case Intrinsic::aarch64_neon_ld2r:
10955  case Intrinsic::aarch64_neon_ld3r:
10956  case Intrinsic::aarch64_neon_ld4r:
10957  case Intrinsic::aarch64_neon_st2:
10958  case Intrinsic::aarch64_neon_st3:
10959  case Intrinsic::aarch64_neon_st4:
10960  case Intrinsic::aarch64_neon_st1x2:
10961  case Intrinsic::aarch64_neon_st1x3:
10962  case Intrinsic::aarch64_neon_st1x4:
10963  case Intrinsic::aarch64_neon_st2lane:
10964  case Intrinsic::aarch64_neon_st3lane:
10965  case Intrinsic::aarch64_neon_st4lane:
10966  return performNEONPostLDSTCombine(N, DCI, DAG);
10967  default:
10968  break;
10969  }
10970  case ISD::GlobalAddress:
10971  return performGlobalAddressCombine(N, DAG, Subtarget, getTargetMachine());
10972  }
10973  return SDValue();
10974 }
10975 
10976 // Check if the return value is used as only a return value, as otherwise
10977 // we can't perform a tail-call. In particular, we need to check for
10978 // target ISD nodes that are returns and any other "odd" constructs
10979 // that the generic analysis code won't necessarily catch.
10980 bool AArch64TargetLowering::isUsedByReturnOnly(SDNode *N,
10981  SDValue &Chain) const {
10982  if (N->getNumValues() != 1)
10983  return false;
10984  if (!N->hasNUsesOfValue(1, 0))
10985  return false;
10986 
10987  SDValue TCChain = Chain;
10988  SDNode *Copy = *N->use_begin();
10989  if (Copy->getOpcode() == ISD::CopyToReg) {
10990  // If the copy has a glue operand, we conservatively assume it isn't safe to
10991  // perform a tail call.
10992  if (Copy->getOperand(Copy->getNumOperands() - 1).getValueType() ==
10993  MVT::Glue)
10994  return false;
10995  TCChain = Copy->getOperand(0);
10996  } else if (Copy->getOpcode() != ISD::FP_EXTEND)
10997  return false;
10998 
10999  bool HasRet = false;
11000  for (SDNode *Node : Copy->uses()) {
11001  if (Node->getOpcode() != AArch64ISD::RET_FLAG)
11002  return false;
11003  HasRet = true;
11004  }
11005 
11006  if (!HasRet)
11007  return false;
11008 
11009  Chain = TCChain;
11010  return true;
11011 }
11012 
11013 // Return whether the an instruction can potentially be optimized to a tail
11014 // call. This will cause the optimizers to attempt to move, or duplicate,
11015 // return instructions to help enable tail call optimizations for this
11016 // instruction.
11017 bool AArch64TargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
11018  return CI->isTailCall();
11019 }
11020 
11021 bool AArch64TargetLowering::getIndexedAddressParts(SDNode *Op, SDValue &Base,
11022  SDValue &Offset,
11023  ISD::MemIndexedMode &AM,
11024  bool &IsInc,
11025  SelectionDAG &DAG) const {
11026  if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)
11027  return false;
11028 
11029  Base = Op->getOperand(0);
11030  // All of the indexed addressing mode instructions take a signed
11031  // 9 bit immediate offset.
11032  if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
11033  int64_t RHSC = RHS->getSExtValue();
11034  if (Op->getOpcode() == ISD::SUB)
11035  RHSC = -(uint64_t)RHSC;
11036  if (!isInt<9>(RHSC))
11037  return false;
11038  IsInc = (Op->getOpcode() == ISD::ADD);
11039  Offset = Op->getOperand(1);
11040  return true;
11041  }
11042  return false;
11043 }
11044 
11045 bool AArch64TargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11046  SDValue &Offset,
11047  ISD::MemIndexedMode &AM,
11048  SelectionDAG &DAG) const {
11049  EVT VT;
11050  SDValue Ptr;
11051  if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11052  VT = LD->getMemoryVT();
11053  Ptr = LD->getBasePtr();
11054  } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11055  VT = ST->getMemoryVT();
11056  Ptr = ST->getBasePtr();
11057  } else
11058  return false;
11059 
11060  bool IsInc;
11061  if (!getIndexedAddressParts(Ptr.getNode(), Base, Offset, AM, IsInc, DAG))
11062  return false;
11063  AM = IsInc ? ISD::PRE_INC : ISD::PRE_DEC;
11064  return true;
11065 }
11066 
11067 bool AArch64TargetLowering::getPostIndexedAddressParts(
11068  SDNode *N, SDNode *Op, SDValue &Base, SDValue &Offset,
11069  ISD::MemIndexedMode &AM, SelectionDAG &DAG) const {
11070  EVT VT;
11071  SDValue Ptr;
11072  if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11073  VT = LD->getMemoryVT();
11074  Ptr = LD->getBasePtr();
11075  } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11076  VT = ST->getMemoryVT();
11077  Ptr = ST->getBasePtr();
11078  } else
11079  return false;
11080 
11081  bool IsInc;
11082  if (!getIndexedAddressParts(Op, Base, Offset, AM, IsInc, DAG))
11083  return false;
11084  // Post-indexing updates the base, so it's not a valid transform
11085  // if that's not the same as the load's pointer.
11086  if (Ptr != Base)
11087  return false;
11088  AM = IsInc ? ISD::POST_INC : ISD::POST_DEC;
11089  return true;
11090 }
11091 
11093  SelectionDAG &DAG) {
11094  SDLoc DL(N);
11095  SDValue Op = N->getOperand(0);
11096 
11097  if (N->getValueType(0) != MVT::i16 || Op.getValueType() != MVT::f16)
11098  return;
11099 
11100  Op = SDValue(
11101  DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL, MVT::f32,
11102  DAG.getUNDEF(MVT::i32), Op,
11103  DAG.getTargetConstant(AArch64::hsub, DL, MVT::i32)),
11104  0);
11105  Op = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op);
11106  Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Op));
11107 }
11108 
11111  SelectionDAG &DAG, unsigned InterOp,
11112  unsigned AcrossOp) {
11113  EVT LoVT, HiVT;
11114  SDValue Lo, Hi;
11115  SDLoc dl(N);
11116  std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
11117  std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0);
11118  SDValue InterVal = DAG.getNode(InterOp, dl, LoVT, Lo, Hi);
11119  SDValue SplitVal = DAG.getNode(AcrossOp, dl, LoVT, InterVal);
11120  Results.push_back(SplitVal);
11121 }
11122 
11123 static std::pair<SDValue, SDValue> splitInt128(SDValue N, SelectionDAG &DAG) {
11124  SDLoc DL(N);
11125  SDValue Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i64, N);
11127  DAG.getNode(ISD::SRL, DL, MVT::i128, N,
11128  DAG.getConstant(64, DL, MVT::i64)));
11129  return std::make_pair(Lo, Hi);
11130 }
11131 
11132 // Create an even/odd pair of X registers holding integer value V.
11134  SDLoc dl(V.getNode());
11135  SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i64);
11136  SDValue VHi = DAG.getAnyExtOrTrunc(
11137  DAG.getNode(ISD::SRL, dl, MVT::i128, V, DAG.getConstant(64, dl, MVT::i64)),
11138  dl, MVT::i64);
11139  if (DAG.getDataLayout().isBigEndian())
11140  std::swap (VLo, VHi);
11141  SDValue RegClass =
11142  DAG.getTargetConstant(AArch64::XSeqPairsClassRegClassID, dl, MVT::i32);
11143  SDValue SubReg0 = DAG.getTargetConstant(AArch64::sube64, dl, MVT::i32);
11144  SDValue SubReg1 = DAG.getTargetConstant(AArch64::subo64, dl, MVT::i32);
11145  const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
11146  return SDValue(
11147  DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
11148 }
11149 
11152  SelectionDAG &DAG,
11153  const AArch64Subtarget *Subtarget) {
11154  assert(N->getValueType(0) == MVT::i128 &&
11155  "AtomicCmpSwap on types less than 128 should be legal");
11156 
11157  if (Subtarget->hasLSE()) {
11158  // LSE has a 128-bit compare and swap (CASP), but i128 is not a legal type,
11159  // so lower it here, wrapped in REG_SEQUENCE and EXTRACT_SUBREG.
11160  SDValue Ops[] = {
11161  createGPRPairNode(DAG, N->getOperand(2)), // Compare value
11162  createGPRPairNode(DAG, N->getOperand(3)), // Store value
11163  N->getOperand(1), // Ptr
11164  N->getOperand(0), // Chain in
11165  };
11166 
11167  MachineFunction &MF = DAG.getMachineFunction();
11169  MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
11170 
11171  unsigned Opcode;
11172  switch (MemOp[0]->getOrdering()) {
11174  Opcode = AArch64::CASPX;
11175  break;
11177  Opcode = AArch64::CASPAX;
11178  break;
11180  Opcode = AArch64::CASPLX;
11181  break;
11184  Opcode = AArch64::CASPALX;
11185  break;
11186  default:
11187  llvm_unreachable("Unexpected ordering!");
11188  }
11189 
11190  MachineSDNode *CmpSwap = DAG.getMachineNode(
11191  Opcode, SDLoc(N), DAG.getVTList(MVT::Untyped, MVT::Other), Ops);
11192  CmpSwap->setMemRefs(MemOp, MemOp + 1);
11193 
11194  unsigned SubReg1 = AArch64::sube64, SubReg2 = AArch64::subo64;
11195  if (DAG.getDataLayout().isBigEndian())
11196  std::swap(SubReg1, SubReg2);
11197  Results.push_back(DAG.getTargetExtractSubreg(SubReg1, SDLoc(N), MVT::i64,
11198  SDValue(CmpSwap, 0)));
11199  Results.push_back(DAG.getTargetExtractSubreg(SubReg2, SDLoc(N), MVT::i64,
11200  SDValue(CmpSwap, 0)));
11201  Results.push_back(SDValue(CmpSwap, 1)); // Chain out
11202  return;
11203  }
11204 
11205  auto Desired = splitInt128(N->getOperand(2), DAG);
11206  auto New = splitInt128(N->getOperand(3), DAG);
11207  SDValue Ops[] = {N->getOperand(1), Desired.first, Desired.second,
11208  New.first, New.second, N->getOperand(0)};
11209  SDNode *CmpSwap = DAG.getMachineNode(
11210  AArch64::CMP_SWAP_128, SDLoc(N),
11212 
11213  MachineFunction &MF = DAG.getMachineFunction();
11215  MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
11216  cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
11217 
11218  Results.push_back(SDValue(CmpSwap, 0));
11219  Results.push_back(SDValue(CmpSwap, 1));
11220  Results.push_back(SDValue(CmpSwap, 3));
11221 }
11222 
11223 void AArch64TargetLowering::ReplaceNodeResults(
11225  switch (N->getOpcode()) {
11226  default:
11227  llvm_unreachable("Don't know how to custom expand this");
11228  case ISD::BITCAST:
11229  ReplaceBITCASTResults(N, Results, DAG);
11230  return;
11231  case ISD::VECREDUCE_ADD:
11232  case ISD::VECREDUCE_SMAX:
11233  case ISD::VECREDUCE_SMIN:
11234  case ISD::VECREDUCE_UMAX:
11235  case ISD::VECREDUCE_UMIN:
11236  Results.push_back(LowerVECREDUCE(SDValue(N, 0), DAG));
11237  return;
11238 
11239  case AArch64ISD::SADDV:
11241  return;
11242  case AArch64ISD::UADDV:
11244  return;
11245  case AArch64ISD::SMINV:
11247  return;
11248  case AArch64ISD::UMINV:
11250  return;
11251  case AArch64ISD::SMAXV:
11253  return;
11254  case AArch64ISD::UMAXV:
11256  return;
11257  case ISD::FP_TO_UINT:
11258  case ISD::FP_TO_SINT:
11259  assert(N->getValueType(0) == MVT::i128 && "unexpected illegal conversion");
11260  // Let normal code take care of it by not adding anything to Results.
11261  return;
11262  case ISD::ATOMIC_CMP_SWAP:
11263  ReplaceCMP_SWAP_128Results(N, Results, DAG, Subtarget);
11264  return;
11265  }
11266 }
11267 
11269  if (Subtarget->isTargetAndroid() || Subtarget->isTargetFuchsia())
11271  return true;
11272 }
11273 
11274 unsigned AArch64TargetLowering::combineRepeatedFPDivisors() const {
11275  // Combine multiple FDIVs with the same divisor into multiple FMULs by the
11276  // reciprocal if there are three or more FDIVs.
11277  return 3;
11278 }
11279 
11282  MVT SVT = VT.getSimpleVT();
11283  // During type legalization, we prefer to widen v1i8, v1i16, v1i32 to v8i8,
11284  // v4i16, v2i32 instead of to promote.
11285  if (SVT == MVT::v1i8 || SVT == MVT::v1i16 || SVT == MVT::v1i32
11286  || SVT == MVT::v1f32)
11287  return TypeWidenVector;
11288 
11290 }
11291 
11292 // Loads and stores less than 128-bits are already atomic; ones above that
11293 // are doomed anyway, so defer to the default libcall and blame the OS when
11294 // things go wrong.
11296  unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11297  return Size == 128;
11298 }
11299 
11300 // Loads and stores less than 128-bits are already atomic; ones above that
11301 // are doomed anyway, so defer to the default libcall and blame the OS when
11302 // things go wrong.
11305  unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11307 }
11308 
11309 // For the real atomic operations, we have ldxr/stxr up to 128 bits,
11312  unsigned Size = AI->getType()->getPrimitiveSizeInBits();
11313  if (Size > 128) return AtomicExpansionKind::None;
11314  // Nand not supported in LSE.
11316  // Leave 128 bits to LLSC.
11317  return (Subtarget->hasLSE() && Size < 128) ? AtomicExpansionKind::None : AtomicExpansionKind::LLSC;
11318 }
11319 
11321  AtomicCmpXchgInst *AI) const {
11322  // If subtarget has LSE, leave cmpxchg intact for codegen.
11323  if (Subtarget->hasLSE()) return false;
11324  // At -O0, fast-regalloc cannot cope with the live vregs necessary to
11325  // implement cmpxchg without spilling. If the address being exchanged is also
11326  // on the stack and close enough to the spill slot, this can lead to a
11327  // situation where the monitor always gets cleared and the atomic operation
11328  // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
11329  return getTargetMachine().getOptLevel() != 0;
11330 }
11331 
11333  AtomicOrdering Ord) const {
11334  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11335  Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
11336  bool IsAcquire = isAcquireOrStronger(Ord);
11337 
11338  // Since i128 isn't legal and intrinsics don't get type-lowered, the ldrexd
11339  // intrinsic must return {i64, i64} and we have to recombine them into a
11340  // single i128 here.
11341  if (ValTy->getPrimitiveSizeInBits() == 128) {
11342  Intrinsic::ID Int =
11343  IsAcquire ? Intrinsic::aarch64_ldaxp : Intrinsic::aarch64_ldxp;
11344  Function *Ldxr = Intrinsic::getDeclaration(M, Int);
11345 
11346  Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11347  Value *LoHi = Builder.CreateCall(Ldxr, Addr, "lohi");
11348 
11349  Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11350  Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
11351  Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11352  Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11353  return Builder.CreateOr(
11354  Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 64)), "val64");
11355  }
11356 
11357  Type *Tys[] = { Addr->getType() };
11358  Intrinsic::ID Int =
11359  IsAcquire ? Intrinsic::aarch64_ldaxr : Intrinsic::aarch64_ldxr;
11360  Function *Ldxr = Intrinsic::getDeclaration(M, Int, Tys);
11361 
11362  return Builder.CreateTruncOrBitCast(
11363  Builder.CreateCall(Ldxr, Addr),
11364  cast<PointerType>(Addr->getType())->getElementType());
11365 }
11366 
11368  IRBuilder<> &Builder) const {
11369  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11370  Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::aarch64_clrex));
11371 }
11372 
11374  Value *Val, Value *Addr,
11375  AtomicOrdering Ord) const {
11376  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11377  bool IsRelease = isReleaseOrStronger(Ord);
11378 
11379  // Since the intrinsics must have legal type, the i128 intrinsics take two
11380  // parameters: "i64, i64". We must marshal Val into the appropriate form
11381  // before the call.
11382  if (Val->getType()->getPrimitiveSizeInBits() == 128) {
11383  Intrinsic::ID Int =
11384  IsRelease ? Intrinsic::aarch64_stlxp : Intrinsic::aarch64_stxp;
11385  Function *Stxr = Intrinsic::getDeclaration(M, Int);
11386  Type *Int64Ty = Type::getInt64Ty(M->getContext());
11387 
11388  Value *Lo = Builder.CreateTrunc(Val, Int64Ty, "lo");
11389  Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 64), Int64Ty, "hi");
11390  Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11391  return Builder.CreateCall(Stxr, {Lo, Hi, Addr});
11392  }
11393 
11394  Intrinsic::ID Int =
11395  IsRelease ? Intrinsic::aarch64_stlxr : Intrinsic::aarch64_stxr;
11396  Type *Tys[] = { Addr->getType() };
11397  Function *Stxr = Intrinsic::getDeclaration(M, Int, Tys);
11398 
11399  return Builder.CreateCall(Stxr,
11400  {Builder.CreateZExtOrBitCast(
11401  Val, Stxr->getFunctionType()->getParamType(0)),
11402  Addr});
11403 }
11404 
11406  Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
11407  return Ty->isArrayTy();
11408 }
11409 
11410 bool AArch64TargetLowering::shouldNormalizeToSelectSequence(LLVMContext &,
11411  EVT) const {
11412  return false;
11413 }
11414 
11415 static Value *UseTlsOffset(IRBuilder<> &IRB, unsigned Offset) {
11416  Module *M = IRB.GetInsertBlock()->getParent()->getParent();
11417  Function *ThreadPointerFunc =
11418  Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
11419  return IRB.CreatePointerCast(
11420  IRB.CreateConstGEP1_32(IRB.CreateCall(ThreadPointerFunc), Offset),
11421  Type::getInt8PtrTy(IRB.getContext())->getPointerTo(0));
11422 }
11423 
11425  // Android provides a fixed TLS slot for the stack cookie. See the definition
11426  // of TLS_SLOT_STACK_GUARD in
11427  // https://android.googlesource.com/platform/bionic/+/master/libc/private/bionic_tls.h
11428  if (Subtarget->isTargetAndroid())
11429  return UseTlsOffset(IRB, 0x28);
11430 
11431  // Fuchsia is similar.
11432  // <zircon/tls.h> defines ZX_TLS_STACK_GUARD_OFFSET with this value.
11433  if (Subtarget->isTargetFuchsia())
11434  return UseTlsOffset(IRB, -0x10);
11435 
11436  return TargetLowering::getIRStackGuard(IRB);
11437 }
11438 
11440  // Android provides a fixed TLS slot for the SafeStack pointer. See the
11441  // definition of TLS_SLOT_SAFESTACK in
11442  // https://android.googlesource.com/platform/bionic/+/master/libc/private/bionic_tls.h
11443  if (Subtarget->isTargetAndroid())
11444  return UseTlsOffset(IRB, 0x48);
11445 
11446  // Fuchsia is similar.
11447  // <zircon/tls.h> defines ZX_TLS_UNSAFE_SP_OFFSET with this value.
11448  if (Subtarget->isTargetFuchsia())
11449  return UseTlsOffset(IRB, -0x8);
11450 
11452 }
11453 
11455  const Instruction &AndI) const {
11456  // Only sink 'and' mask to cmp use block if it is masking a single bit, since
11457  // this is likely to be fold the and/cmp/br into a single tbz instruction. It
11458  // may be beneficial to sink in other cases, but we would have to check that
11459  // the cmp would not get folded into the br to form a cbz for these to be
11460  // beneficial.
11462  if (!Mask)
11463  return false;
11464  return Mask->getValue().isPowerOf2();
11465 }
11466 
11468  // Update IsSplitCSR in AArch64unctionInfo.
11470  AFI->setIsSplitCSR(true);
11471 }
11472 
11474  MachineBasicBlock *Entry,
11475  const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
11476  const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
11477  const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
11478  if (!IStart)
11479  return;
11480 
11481  const TargetInstrInfo *TII = Subtarget->getInstrInfo();
11482  MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
11483  MachineBasicBlock::iterator MBBI = Entry->begin();
11484  for (const MCPhysReg *I = IStart; *I; ++I) {
11485  const TargetRegisterClass *RC = nullptr;
11486  if (AArch64::GPR64RegClass.contains(*I))
11487  RC = &AArch64::GPR64RegClass;
11488  else if (AArch64::FPR64RegClass.contains(*I))
11489  RC = &AArch64::FPR64RegClass;
11490  else
11491  llvm_unreachable("Unexpected register class in CSRsViaCopy!");
11492 
11493  unsigned NewVR = MRI->createVirtualRegister(RC);
11494  // Create copy from CSR to a virtual register.
11495  // FIXME: this currently does not emit CFI pseudo-instructions, it works
11496  // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
11497  // nounwind. If we want to generalize this later, we may need to emit
11498  // CFI pseudo-instructions.
11500  Attribute::NoUnwind) &&
11501  "Function should be nounwind in insertCopiesSplitCSR!");
11502  Entry->addLiveIn(*I);
11503  BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
11504  .addReg(*I);
11505 
11506  // Insert the copy-back instructions right before the terminator.
11507  for (auto *Exit : Exits)
11508  BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
11509  TII->get(TargetOpcode::COPY), *I)
11510  .addReg(NewVR);
11511  }
11512 }
11513 
11515  // Integer division on AArch64 is expensive. However, when aggressively
11516  // optimizing for code size, we prefer to use a div instruction, as it is
11517  // usually smaller than the alternative sequence.
11518  // The exception to this is vector division. Since AArch64 doesn't have vector
11519  // integer division, leaving the division as-is is a loss even in terms of
11520  // size, because it will have to be scalarized, while the alternative code
11521  // sequence can be performed in vector form.
11522  bool OptSize =
11523  Attr.hasAttribute(AttributeList::FunctionIndex, Attribute::MinSize);
11524  return OptSize && !VT.isVector();
11525 }
11526 
11528  return Subtarget->hasAggressiveFMA() && VT.isFloatingPoint();
11529 }
11530 
11531 unsigned
11533  if (Subtarget->isTargetDarwin() || Subtarget->isTargetWindows())
11534  return getPointerTy(DL).getSizeInBits();
11535 
11536  return 3 * getPointerTy(DL).getSizeInBits() + 2 * 32;
11537 }
11538 
11539 void AArch64TargetLowering::finalizeLowering(MachineFunction &MF) const {
11542 }
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
std::pair< Value *, Value * > ShuffleOps
We are building a shuffle to create V, which is a sequence of insertelement, extractelement pairs...
static bool isAdvSIMDModImmType6(uint64_t Imm)
Type * getVectorElementType() const
Definition: Type.h:371
static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt)
isVShiftLImm - Check if this is a valid build_vector for the immediate operand of a vector shift left...
void setFrameAddressIsTaken(bool T)
uint64_t CallInst * C
void setAllowReassociation(bool b)
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:541
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:349
X = FP_ROUND(Y, TRUNC) - Rounding &#39;Y&#39; from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:508
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned char TargetFlags=0)
Value * getValueOperand()
Definition: Instructions.h:399
unsigned getFirstUnallocated(ArrayRef< MCPhysReg > Regs) const
getFirstUnallocated - Return the index of the first unallocated register in the set, or Regs.size() if they are all allocated.
Helper structure to keep track of SetCC information.
static MVT getIntegerVT(unsigned BitWidth)
static bool isUZP_v_undef_Mask(ArrayRef< int > M, EVT VT, unsigned &WhichResult)
isUZP_v_undef_Mask - Special case of isUZPMask for canonical form of "vector_shuffle v...
void AnalyzeCallResult(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeCallResult - Analyze the return values of a call, incorporating info about the passed values i...
bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I, MachineFunction &MF, unsigned Intrinsic) const override
getTgtMemIntrinsic - Represent NEON load and store intrinsics as MemIntrinsicNodes.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:565
static SDValue performBitcastCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
EVT getValueType() const
Return the ValueType of the referenced return value.
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg If BaseGV is null...
MO_G3 - A symbol operand with this flag (granule 3) represents the high 16-bits of a 64-bit address...
Value * CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition: IRBuilder.h:1432
static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
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...
iterator_range< use_iterator > uses()
Definition: Value.h:354
static bool isConstant(const MachineInstr &MI)
bool isUndef() const
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand...
const MCPhysReg * getScratchRegisters(CallingConv::ID CC) const override
Returns a 0 terminated array of registers that can be safely used as scratch registers.
const GlobalValue * getGlobal() const
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1556
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
static uint8_t encodeAdvSIMDModImmType3(uint64_t Imm)
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
LLVMContext & Context
LLVMContext & getContext() const
Definition: IRBuilder.h:123
bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const override
Return true if EXTRACT_SUBVECTOR is cheap for this result type with this index.
DiagnosticInfoOptimizationBase::Argument NV
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond)
Helper function to make it easier to build SetCC&#39;s if you just have an ISD::CondCode instead of an SD...
Definition: SelectionDAG.h:951
bool isMisaligned128StoreSlow() const
bool isIndexed() const
Return true if this is a pre/post inc/dec load/store.
SDValue CombineTo(SDNode *N, ArrayRef< SDValue > To, bool AddTo=true)
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&#39;s not CSE&#39;d)...
Definition: SelectionDAG.h:849
static SDValue performVSelectCombine(SDNode *N, SelectionDAG &DAG)
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:35
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...
static SDValue performNEONPostLDSTCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
Target-specific DAG combine function for NEON load/store intrinsics to merge base address updates...
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an vector value) starting with the ...
Definition: ISDOpcodes.h:334
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:119
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:613
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static bool isUZPMask(ArrayRef< int > M, EVT VT, unsigned &WhichResult)
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
static bool isTRN_v_undef_Mask(ArrayRef< int > M, EVT VT, unsigned &WhichResult)
isTRN_v_undef_Mask - Special case of isTRNMask for canonical form of "vector_shuffle v...
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
static MVT getVectorVT(MVT VT, unsigned NumElements)
void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd)
Assign this MachineSDNodes&#39;s memory reference descriptor list.
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:343
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:260
static int getFP16Imm(const APInt &Imm)
getFP16Imm - Return an 8-bit floating-point version of the 16-bit floating-point value.
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:176
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
SDValue scalarizeVectorStore(StoreSDNode *ST, SelectionDAG &DAG) const
static SDValue tryCombineToEXTR(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
EXTR instruction extracts a contiguous chunk of bits from two existing registers viewed as a high/low...
static bool isAdvSIMDModImmType12(uint64_t Imm)
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:588
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:518
static bool isAdvSIMDModImmType4(uint64_t Imm)
bool isVector() const
Return true if this is a vector value type.
static SDValue performXorCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget)
static uint8_t encodeAdvSIMDModImmType1(uint64_t Imm)
static bool isConjunctionDisjunctionTree(const SDValue Val, bool &CanNegate, unsigned Depth=0)
Returns true if Val is a tree of AND/OR/SETCC operations.
bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AddrSpace=0, unsigned Align=1, bool *Fast=nullptr) const override
Returns true if the target allows unaligned memory accesses of the specified type.
const SDValue & getBasePtr() const
bool predictableSelectIsExpensive() const
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:223
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
virtual void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:285
static bool isINSMask(ArrayRef< int > M, int NumInputElements, bool &DstIsLeft, int &Anomaly)
static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG)
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(EVT VT) const override
Return the preferred vector type legalization action.
unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
static CondCode getInvertedCondCode(CondCode Code)
unsigned getReg() const
getReg - Returns the register number.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:351
bool isIntDivCheap(EVT VT, AttributeList Attr) const override
Return true if integer divide is usually cheaper than a sequence of several shifts, adds, and multiplies for this target.
const SDValue & getValue() const
static bool isZIP_v_undef_Mask(ArrayRef< int > M, EVT VT, unsigned &WhichResult)
isZIP_v_undef_Mask - Special case of isZIPMask for canonical form of "vector_shuffle v...
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain...
Definition: ISDOpcodes.h:662
TOF
Target Operand Flag enum.
This class represents a function call, abstracting a target machine&#39;s calling convention.
unsigned Reg
unsigned char ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const
ClassifyGlobalReference - Find the target operand flags that describe how a global value should be re...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:647
static SDValue ConstantBuildVector(SDValue Op, SelectionDAG &DAG)
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:253
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit...
static SDValue splitStores(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG, const AArch64Subtarget *Subtarget)
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change...
static MVT getFloatingPointVT(unsigned BitWidth)
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:251
int getScalingFactorCost(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AS) const override
Return the cost of the scaling factor used in the addressing mode represented by AM for this target...
unsigned getVectorNumElements() const
const SDValue & getChain() const
Function Alias Analysis Results
This instruction constructs a fixed permutation of two input vectors.
static SDValue performSelectCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
A vector select: "(select vL, vR, (setcc LHS, RHS))" is best performed with the compare-mask instruct...
static SDValue performVectorCompareAndMaskUnaryOpCombine(SDNode *N, SelectionDAG &DAG)
unsigned getAlignment() const
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:307
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
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...
arg_iterator arg_end()
Definition: Function.h:666
static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG)
static uint32_t Concat[]
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:34
MVT getSimpleValueType(unsigned ResNo) const
Return the type of a specified result as a simple type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:141
F(f)
MO_G0 - A symbol operand with this flag (granule 0) represents the bits 0-15 of a 64-bit address...
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select&#39;s if you just have operands and don&#39;t want to check...
Definition: SelectionDAG.h:964
static SDValue performExtendCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
static SDValue NormalizeBuildVector(SDValue Op, SelectionDAG &DAG)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:685
An instruction for reading from memory.
Definition: Instructions.h:168
static SDValue emitComparison(SDValue LHS, SDValue RHS, ISD::CondCode CC, const SDLoc &dl, SelectionDAG &DAG)
static unsigned getDUPLANEOp(EVT EltType)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
void softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, SDValue &NewRHS, ISD::CondCode &CCCode, const SDLoc &DL) const
Soften the operands of a comparison.
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:436
static bool isAdvSIMDModImmType3(uint64_t Imm)
[US]{MIN/MAX} - Binary minimum or maximum or signed or unsigned integers.
Definition: ISDOpcodes.h:360
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:882
const SDNodeFlags getFlags() const
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:681
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned char TargetFlags=0)
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:230
static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG)
static SDValue performSetccAddFolding(SDNode *Op, SelectionDAG &DAG)
This defines the Use class.
AtomicExpansionKind
Enum that specifies what an atomic load/AtomicRMWInst is expanded to, if at all.
static SDValue EmitVectorComparison(SDValue LHS, SDValue RHS, AArch64CC::CondCode CC, bool NoNans, EVT VT, const SDLoc &dl, SelectionDAG &DAG)
Same for subtraction.
Definition: ISDOpcodes.h:254
bool shouldConsiderGEPOffsetSplit() const override
bool isOperationLegalOrCustom(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
static SDValue tryCombineFixedPointConvert(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1 at the ...
Definition: ISDOpcodes.h:329
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
std::size_t countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the most significant bit to the least stopping at the first 1...
Definition: MathExtras.h:189
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, SelectionDAG &DAG) const
Lower TLS global address SDNode for target independent emulated TLS model.
static bool isZeroExtended(SDNode *N, SelectionDAG &DAG)
static std::pair< SDValue, SDValue > getAArch64XALUOOp(AArch64CC::CondCode &CC, SDValue Op, SelectionDAG &DAG)
unsigned getValueSizeInBits() const
Returns the size of the value in bits.
static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const SDLoc &dl)
GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit the specified operations t...
int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it...
bool isCallingConvWin64(CallingConv::ID CC) const
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:407
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Libcall getFPROUND(EVT OpVT, EVT RetVT)
getFPROUND - Return the FPROUND_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
bool isUnsignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs an unsigned comparison when used with intege...
Definition: ISDOpcodes.h:952
static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG)
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
return AArch64::GPR64RegClass contains(Reg)
SDValue getExternalSymbol(const char *Sym, EVT VT)
void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified truncating store does not work with the specified type and indicate what ...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:130
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:159
bool isTruncatingStore() const
Return true if the op does a truncation before store.
bool isMemLoc() const
unsigned getAddressSpace() const
Return the address space for the associated pointer.
bool needsCustom() const
A convenience struct that encapsulates a DAG, and two SDValues for returning information from TargetL...
Libcall getUINTTOFP(EVT OpVT, EVT RetVT)
getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
MO_G2 - A symbol operand with this flag (granule 2) represents the bits 32-47 of a 64-bit address...
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition: ISDOpcodes.h:210
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:136
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations...
Definition: ISDOpcodes.h:428
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.h:1625
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
static bool isAdvSIMDModImmType7(uint64_t Imm)
static SDValue tryFormConcatFromShuffle(SDValue Op, SelectionDAG &DAG)
static bool isTRNMask(ArrayRef< int > M, EVT VT, unsigned &WhichResult)
bool hasOneUse() const
Return true if there is exactly one use of this node.
cl::opt< bool > EnableAArch64ELFLocalDynamicTLSGeneration("aarch64-elf-ldtls-generation", cl::Hidden, cl::desc("Allow AArch64 Local Dynamic TLS code generation"), cl::init(false))
A description of a memory reference used in the backend.
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition: APInt.cpp:514
static SDValue performTBZCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
static SDValue performCONDCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG, unsigned CCIndex, unsigned CmpIndex)
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static void ReplaceCMP_SWAP_128Results(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG, const AArch64Subtarget *Subtarget)
const HexagonInstrInfo * TII
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:368
static SDValue tryCombineCRC32(unsigned Mask, SDNode *N, SelectionDAG &DAG)
static Value * UseTlsOffset(IRBuilder<> &IRB, unsigned Offset)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
Shift and rotation operations.
Definition: ISDOpcodes.h:382
static SDValue performAddSubLongCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:202
Class to represent struct types.
Definition: DerivedTypes.h:201
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:242
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth...
Definition: ISDOpcodes.h:369
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
static SDValue performFpToIntCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget)
Fold a floating-point multiply by power of two into floating-point to fixed-point conversion...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
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), MachineInstr opcode, and operands.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:639
BinOp getOperation() const
Definition: Instructions.h:734
static uint8_t encodeAdvSIMDModImmType6(uint64_t Imm)
CallLoweringInfo & setChain(SDValue InChain)
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:191
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
CopyToReg - This node has three operands: a chain, a register number to set to this value...
Definition: ISDOpcodes.h:170
unsigned SubReg
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
op_iterator op_end() const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:731
uint64_t getConstantOperandVal(unsigned i) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
#define INT64_MAX
Definition: DataTypes.h:77
static SDValue tryAdvSIMDModImm32(unsigned NewOp, SDValue Op, SelectionDAG &DAG, const APInt &Bits, const SDValue *LHS=nullptr)
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const override
Return the ISD::SETCC ValueType.
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
Helper structure to keep track of a SET_CC lowered into AArch64 code.
FLT_ROUNDS_ - Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest 2 Round to ...
Definition: ISDOpcodes.h:516
virtual bool useLoadStackGuardNode() const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
This file contains the simple types necessary to represent the attributes associated with functions a...
SimpleValueType SimpleTy
unsigned getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:304
bool isInConsecutiveRegs() const
The memory access is dereferenceable (i.e., doesn&#39;t trap).
static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG)
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
static bool isLogicalImmediate(uint64_t imm, unsigned regSize)
isLogicalImmediate - Return true if the immediate is valid for a logical immediate instruction of the...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:311
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:457
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...
static SDValue combineAcrossLanesIntrinsic(unsigned Opc, SDNode *N, SelectionDAG &DAG)
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:398
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
This class is used to represent EVT&#39;s, which are used to parameterize some operations.
bool isLegalInterleavedAccessType(VectorType *VecTy, const DataLayout &DL) const
Returns true if VecTy is a legal interleaved access type.
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:509
static SDValue foldVectorXorShiftIntoCmp(SDNode *N, SelectionDAG &DAG, const AArch64Subtarget *Subtarget)
Turn vector tests of the signbit in the form of: xor (sra X, elt_size(X)-1), -1 into: cmge X...
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG...
Definition: ISDOpcodes.h:73
const BlockAddress * getBlockAddress() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
This is an SDNode representing atomic operations.
uint64_t getNumElements() const
Definition: DerivedTypes.h:359
LocInfo getLocInfo() const
#define im(i)
static bool isAdvSIMDModImmType5(uint64_t Imm)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:336
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:773
This file implements a class to represent arbitrary precision integral constant values and operations...
bool is64BitVector() const
Return true if this is a 64-bit vector type.
This represents a list of ValueType&#39;s that has been intern&#39;d by a SelectionDAG.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
AtomicOrdering
Atomic ordering for LLVM&#39;s memory model.
STACKSAVE - STACKSAVE has one operand, an input chain.
Definition: ISDOpcodes.h:658
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
unsigned getSizeInBits() const
Value * getSafeStackPointerLocation(IRBuilder<> &IRB) const override
If the target has a standard location for the unsafe stack pointer, returns the address of that locat...
static const unsigned PerfectShuffleTable[6561+1]
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1624
int64_t getSExtValue() const
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...
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:408
unsigned getScalarSizeInBits() const
Definition: ValueTypes.h:298
void setArgumentStackToRestore(unsigned bytes)
This is a fast-path instruction selection class that generates poor code and doesn&#39;t support illegal ...
Definition: FastISel.h:67
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:292
unsigned getNextStackOffset() const
getNextStackOffset - Return the next stack offset such that all stack slots satisfy their alignment r...
#define UINT64_MAX
Definition: DataTypes.h:83
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1629
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1568
Constant * createSequentialMask(IRBuilder<> &Builder, unsigned Start, unsigned NumInts, unsigned NumUndefs)
Create a sequential shuffle mask.
unsigned NoNaNsFPMath
NoNaNsFPMath - This flag is enabled when the -enable-no-nans-fp-math flag is specified on the command...
static SDValue performConcatVectorsCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:395
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:450
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose...
static bool isEXTMask(ArrayRef< int > M, EVT VT, bool &ReverseEXT, unsigned &Imm)
void emitAtomicCmpXchgNoStoreLLBalance(IRBuilder<> &Builder) const override
static bool checkValueWidth(SDValue V, unsigned width, ISD::LoadExtType &ExtType)
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition: APFloat.h:1167
MO_GOT - This flag indicates that a symbol operand represents the address of the GOT entry for the sy...
SDValue getRegisterMask(const uint32_t *RegMask)
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
Definition: MathExtras.h:411
const AArch64RegisterInfo * getRegisterInfo() const override
static SDValue tryAdvSIMDModImm8(unsigned NewOp, SDValue Op, SelectionDAG &DAG, const APInt &Bits)
virtual TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(EVT VT) const
Return the preferred vector type legalization action.
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:399
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:121
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:401
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:201
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static bool isOverflowIntrOpRes(SDValue Op)
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:200
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
bool enableAggressiveFMAFusion(EVT VT) const override
Enable aggressive FMA fusion on targets that want it.
static bool isAdvSIMDModImmType2(uint64_t Imm)
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
Definition: SelectionDAG.h:865
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:210
An instruction for storing to memory.
Definition: Instructions.h:310
Natural vector cast.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out...
Definition: ISDOpcodes.h:913
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
op_iterator op_begin() const
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification, or lowering of the constant.
Definition: ISDOpcodes.h:125
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1556
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:571
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
Definition: ISDOpcodes.h:710
bool isFPImmLegal(const APFloat &Imm, EVT VT) const override
Returns true if the target can instruction select the specified FP immediate natively.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements. ...
Definition: SelectionDAG.h:746
ArrayRef< SDUse > ops() const
TargetLoweringBase::AtomicExpansionKind shouldExpandAtomicLoadInIR(LoadInst *LI) const override
Returns how the given (atomic) load should be expanded by the IR-level AtomicExpand pass...
bool parametersInCSRMatch(const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, const SmallVectorImpl< CCValAssign > &ArgLocs, const SmallVectorImpl< SDValue > &OutVals) const
Check whether parameters to a call that are passed in callee saved registers are the same as from the...
static SDValue getReductionSDNode(unsigned Op, SDLoc DL, SDValue ScalarOp, SelectionDAG &DAG)
static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG)
static const MCPhysReg GPRArgRegs[]
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo)
Value * CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1639
amdgpu Simplify well known AMD library false Value * Callee
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:151
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1007
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void initializeSplitCSR(MachineBasicBlock *Entry) const override
Perform necessary initialization to handle a subset of CSRs explicitly via copies.
bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg) const override
For some targets, an LLVM struct type must be broken down into multiple simple types, but the calling convention specifies that the entire struct must be passed in a block of consecutive registers.
bool supportsAddressTopByteIgnored() const
CPU has TBI (top byte of addresses is ignored during HW address translation) and OS enables it...
static SDValue WidenVector(SDValue V64Reg, SelectionDAG &DAG)
WidenVector - Given a value in the V64 register class, produce the equivalent value in the V128 regis...
MVT getVectorElementType() const
Value * getOperand(unsigned i) const
Definition: User.h:170
std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
static bool isREVMask(ArrayRef< int > M, EVT VT, unsigned BlockSize)
isREVMask - Check if a vector shuffle corresponds to a REV instruction with the specified blocksize...
static SDValue tryExtendDUPToExtractHigh(SDValue N, SelectionDAG &DAG)
Class to represent pointers.
Definition: DerivedTypes.h:467
unsigned getByValSize() const
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override
static bool isEssentiallyExtractSubvector(SDValue N)
This class is used to represent ISD::STORE nodes.
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:494
static SDValue performBRCONDCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:118
AArch64SetCCInfo AArch64
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector with the specified, possibly variable...
Definition: ISDOpcodes.h:303
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1130
TargetInstrInfo - Interface to description of machine instruction set.
bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
bool is128BitVector() const
Return true if this is a 128-bit vector type.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:635
bool isLegalICmpImmediate(int64_t) const override
Return true if the specified immediate is legal icmp immediate, that is the target has icmp instructi...
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:229
static bool isAdvSIMDModImmType9(uint64_t Imm)
bool isShuffleMaskLegal(ArrayRef< int > M, EVT VT) const override
Return true if the given shuffle mask can be codegen&#39;d directly, or if it should be stack expanded...
bool isZero() const
Return true if the value is positive or negative zero.
bool useEmulatedTLS() const
Returns true if this target uses emulated TLS.
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:562
bool isTruncateFree(Type *Ty1, Type *Ty2) const override
Return true if it&#39;s free to truncate a value of type FromTy to type ToTy.
The memory access is volatile.
virtual Value * getIRStackGuard(IRBuilder<> &IRB) const
If the target has a standard location for the stack protector guard, returns the address of that loca...
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:610
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:742
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static SDValue tryCombineToBSL(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
const SDValue & getBasePtr() const
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
bool isNormalLoad(const SDNode *N)
Returns true if the specified node is a non-extending and unindexed load.
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:166
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned UnsafeFPMath
UnsafeFPMath - This flag is enabled when the -enable-unsafe-fp-math flag is specified on the command ...
bool shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override
Returns true if the given atomic cmpxchg should be expanded by the IR-level AtomicExpand pass into a ...
FPOpFusion::FPOpFusionMode AllowFPOpFusion
AllowFPOpFusion - This flag is set by the -fuse-fp-ops=xxx option.
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:592
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MO_G1 - A symbol operand with this flag (granule 1) represents the bits 16-31 of a 64-bit address...
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
bool requiresStrictAlign() const
unsigned const MachineRegisterInfo * MRI
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:120
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...
static int getFP32Imm(const APInt &Imm)
getFP32Imm - Return an 8-bit floating-point version of the 32-bit floating-point value.
unsigned countPopulation() const
Count the number of bits set.
Definition: APInt.h:1651
bool isZExtFree(Type *Ty1, Type *Ty2) const override
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
Machine Value Type.
Value * concatenateVectors(IRBuilder<> &Builder, ArrayRef< Value *> Vecs)
Concatenate a list of vectors.
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
GenericSetCCInfo Generic
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type...
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
Simple binary floating point operators.
Definition: ISDOpcodes.h:260
void setTargetDAGCombine(ISD::NodeType NT)
Targets should invoke this method for each target independent node that they want to provide a custom...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:273
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE...
Definition: ISDOpcodes.h:691
iterator_range< value_op_iterator > op_values() const
const SDValue & getOperand(unsigned Num) const
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:888
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL...
Definition: ISDOpcodes.h:308
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:224
static const MVT MVT_CC
Value type used for condition codes.
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:232
static SDValue tryCombineLongOpWithDup(unsigned IID, SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
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 ...
Definition: SelectionDAG.h:837
unsigned getPrefFunctionAlignment() const
void setBooleanContents(BooleanContent Ty)
Specify how the target extends the result of integer and floating point boolean values from i1 to a w...
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...
bool isAcquireOrStronger(AtomicOrdering ao)
std::pair< SDValue, SDValue > SplitVectorOperand(const SDNode *N, unsigned OpNo)
Split the node&#39;s operand with EXTRACT_SUBVECTOR and return the low/high part.
CCAssignFn * CCAssignFnForReturn(CallingConv::ID CC) const
Selects the correct CCAssignFn for a given CallingConvention value.
Libcall getFPTOSINT(EVT OpVT, EVT RetVT)
getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
static bool isSetCCOrZExtSetCC(const SDValue &Op, SetCCInfoAndKind &Info)
static EVT getExtensionTo64Bits(const EVT &OrigVT)
static std::pair< SDValue, SDValue > splitInt128(SDValue N, SelectionDAG &DAG)
static bool isAdvSIMDModImmType1(uint64_t Imm)
static mvt_range fp_valuetypes()
static void ReplaceReductionResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG, unsigned InterOp, unsigned AcrossOp)
bool isDesirableToCommuteWithShift(const SDNode *N) const override
Returns false if N is a bit extraction pattern of (X >> C) & Mask.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:434
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This class provides iterator support for SDUse operands that use a specific SDNode.
bool useSmallAddressing() const
bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const override
Returns true if it is beneficial to convert a load of a constant to just the constant itself...
static SDValue emitConjunctionDisjunctionTree(SelectionDAG &DAG, SDValue Val, AArch64CC::CondCode &OutCC)
Emit conjunction or disjunction tree with the CMP/FCMP followed by a chain of CCMP/CFCMP ops...
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side...
bool hasNoNaNs() const
virtual ConstraintWeight getSingleConstraintMatchWeight(AsmOperandInfo &info, const char *constraint) const
Examine constraint string and operand type and determine a weight value.
static bool mayTailCallThisCC(CallingConv::ID CC)
Return true if we might ever do TCO for calls with this calling convention.
void setOperationPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
Convenience method to set an operation to Promote and specify the type in a single call...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
bool CombineTo(SDValue O, SDValue N)
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using &#39;From&#39; to use &#39;To&#39; instead.
void computeMaxCallFrameSize(const MachineFunction &MF)
Computes the maximum size of a callframe and the AdjustsStack property.
const uint32_t * getThisReturnPreservedMask(const MachineFunction &MF, CallingConv::ID) const
getThisReturnPreservedMask - Returns a call preserved mask specific to the case that &#39;returned&#39; is on...
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:730
const APInt & getAPIntValue() const
unsigned GuaranteedTailCallOpt
GuaranteedTailCallOpt - This flag is enabled when -tailcallopt is specified on the commandline...
const Triple & getTargetTriple() const
Value * getPointerOperand()
Definition: Instructions.h:274
static bool isAdvSIMDModImmType8(uint64_t Imm)
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:57
void setPrefFunctionAlignment(unsigned Align)
Set the target&#39;s preferred function alignment.
static bool resultsCompatible(CallingConv::ID CalleeCC, CallingConv::ID CallerCC, MachineFunction &MF, LLVMContext &C, const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn CalleeFn, CCAssignFn CallerFn)
Returns true if the results of the two calling conventions are compatible.
static mvt_range vector_valuetypes()
arg_iterator arg_begin()
Definition: Function.h:657
self_iterator getIterator()
Definition: ilist_node.h:82
SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, unsigned Align=0, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, unsigned Size=0)
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
The memory access is non-temporal.
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
const MCPhysReg * getCalleeSavedRegsViaCopy(const MachineFunction *MF) const
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:281
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:682
static void changeFPCCToAArch64CC(ISD::CondCode CC, AArch64CC::CondCode &CondCode, AArch64CC::CondCode &CondCode2)
changeFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64 CC.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:949
TargetLoweringBase::AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override
Returns how the IR-level AtomicExpand pass should expand the given AtomicRMW, if at all...
void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
If Opc/OrigVT is specified as being promoted, the promotion code defaults to trying a larger integer/...
lazy value info
unsigned MaxStoresPerMemmove
Specify maximum bytes of store instructions per memmove call.
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:598
Helper structure to be able to read SetCC information.
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo...
Definition: ISDOpcodes.h:759
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:526
static SDValue addRequiredExtensionForVectorMULL(SDValue N, SelectionDAG &DAG, const EVT &OrigTy, const EVT &ExtTy, unsigned ExtOpcode)
static SDValue emitConjunctionDisjunctionTreeRec(SelectionDAG &DAG, SDValue Val, AArch64CC::CondCode &OutCC, bool Negate, SDValue CCOp, AArch64CC::CondCode Predicate)
Emit conjunction or disjunction tree with the CMP/FCMP followed by a chain of CCMP/CFCMP ops...
std::vector< ArgListEntry > ArgListTy
MO_HI12 - This flag indicates that a symbol operand represents the bits 13-24 of a 64-bit address...
virtual Value * getSafeStackPointerLocation(IRBuilder<> &IRB) const
Returns the target-specific address of the unsafe stack pointer.
Extended Value Type.
Definition: ValueTypes.h:34
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:398
const AMDGPUAS & AS
static SDValue performIntToFpCombine(SDNode *N, SelectionDAG &DAG, const AArch64Subtarget *Subtarget)
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...
static bool isSingletonEXTMask(ArrayRef< int > M, EVT VT, unsigned &Imm)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:1963
This structure contains all information that is necessary for lowering calls.
size_t size() const
Definition: SmallVector.h:53
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:942
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
bool isVolatile() const
static SDValue GenerateTBL(SDValue Op, ArrayRef< int > ShuffleMask, SelectionDAG &DAG)
static SDValue LowerTruncateVectorStore(SDLoc DL, StoreSDNode *ST, EVT VT, EVT MemVT, SelectionDAG &DAG)
const TargetMachine & getTargetMachine() const
This class contains a discriminated union of information about pointers in memory operands...
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
MO_TLS - Indicates that the operand being accessed is some kind of thread-local symbol.
unsigned getNumOperands() const
Return the number of values used by this operation.
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:43
static SDValue LowerXOR(SDValue Op, SelectionDAG &DAG)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static uint8_t encodeAdvSIMDModImmType12(uint64_t Imm)
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1552
static SDValue performSTORECombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG, const AArch64Subtarget *Subtarget)
static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG)
NarrowVector - Given a value in the V128 register class, produce the equivalent value in the V64 regi...
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, 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...
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
unsigned first
static bool findEXTRHalf(SDValue N, SDValue &Src, uint32_t &ShiftAmount, bool &FromHi)
An EXTR instruction is made up of two shifts, ORed together.
The memory access writes data.
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
static const int BlockSize
Definition: TarWriter.cpp:34
static SDValue performORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget)
void setHasExtractBitsInsn(bool hasExtractInsn=true)
Tells the code generator that the target has BitExtract instructions.
static cl::opt< bool > EnableAArch64SlrGeneration("aarch64-shift-insert-generation", cl::Hidden, cl::desc("Allow AArch64 SLI/SRI formation"), cl::init(false))
bool isReleaseOrStronger(AtomicOrdering ao)
static uint8_t encodeAdvSIMDModImmType2(uint64_t Imm)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Libcall getFPEXT(EVT OpVT, EVT RetVT)
getFPEXT - Return the FPEXT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
static uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize)
encodeLogicalImmediate - Return the encoded immediate value for a logical immediate instruction of th...
SDValue getTargetConstantPool(const Constant *C, EVT VT, unsigned Align=0, int Offset=0, unsigned char TargetFlags=0)
Definition: SelectionDAG.h:634
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:946
SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags=0)
Definition: SelectionDAG.h:628
TokenFactor - This node takes multiple tokens as input and produces a single token result...
Definition: ISDOpcodes.h:50
static int getFP64Imm(const APInt &Imm)
getFP64Imm - Return an 8-bit floating-point version of the 64-bit floating-point value.
bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef...
void dump() const
Dump this node, for debugging.
static SDValue LowerBITCAST(SDValue Op, SelectionDAG &DAG)
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:401
bool lowerInterleavedLoad(LoadInst *LI, ArrayRef< ShuffleVectorInst *> Shuffles, ArrayRef< unsigned > Indices, unsigned Factor) const override
Lower an interleaved load into a ldN intrinsic.
Iterator for intrusive lists based on ilist_node.
bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy, EVT NewVT) const override
CCState - This class holds information needed while lowering arguments and return values...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
static unsigned getIntrinsicID(const SDNode *N)
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V)
static SDValue splitStoreSplat(SelectionDAG &DAG, StoreSDNode &St, SDValue SplatVal, unsigned NumVecElts)
EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset, bool MemcpyStrSrc, MachineFunction &MF) const override
Returns the target specific optimal type for load and store operations as a result of memset...
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:315
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:265
testing::Matcher< const detail::ErrorHolder & > Failed()
Definition: Error.h:148
static SDValue tryAdvSIMDModImm321s(unsigned NewOp, SDValue Op, SelectionDAG &DAG, const APInt &Bits)
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:199
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:222
MVT getScalarShiftAmountTy(const DataLayout &DL, EVT) const override
EVT is not used in-tree, but is used by out-of-tree target.
AArch64TargetLowering(const TargetMachine &TM, const AArch64Subtarget &STI)
unsigned getMaximumJumpTableSize() const
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition: Instruction.h:64
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
unsigned getMaximumJumpTableSize() const
Return upper limit for number of entries in a jump table.
static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, unsigned AlignCheck)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:729
Provides information about what library functions are available for the current target.
static bool isLegalArithImmed(uint64_t C)
void dump() const
CCValAssign - Represent assignment of one arg/retval to a location.
static unsigned getExtFactor(SDValue &V)
getExtFactor - Determine the adjustment factor for the position when generating an "extract from vect...
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, unsigned Align, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:800
iterator end() const
Definition: ArrayRef.h:138
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition: ValueTypes.h:96
BRCOND - Conditional branch.
Definition: ISDOpcodes.h:607
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
An SDNode that represents everything that will be needed to construct a MachineInstr.
static bool isAllConstantBuildVector(const SDValue &PotentialBVec, uint64_t &ConstVal)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:710
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:385
This is an abstract virtual class for memory operations.
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
const Constant * getConstVal() const
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:1948
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
static SDValue tryCombineShiftImm(unsigned IID, SDNode *N, SelectionDAG &DAG)
EVT widenIntegerVectorElementType(LLVMContext &Context) const
Return a VT for an integer vector type with the size of the elements doubled.
Definition: ValueTypes.h:342
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:621
Helper structure to keep track of ISD::SET_CC operands.
Represents one node in the SelectionDAG.
bool isProfitableToHoist(Instruction *I) const override
Check if it is profitable to hoist instruction in then/else to if.
CondCode getSetCCInverse(CondCode Operation, bool isInteger)
Return the operation corresponding to !(X op Y), where &#39;op&#39; is a valid SetCC operation.
static SDValue tryAdvSIMDModImmFP(unsigned NewOp, SDValue Op, SelectionDAG &DAG, const APInt &Bits)
static bool isSignExtended(SDNode *N, SelectionDAG &DAG)
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
int64_t getImm() const
bool targetShrinkDemandedConstant(SDValue Op, const APInt &Demanded, TargetLoweringOpt &TLO) const override
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N)
Definition: SelectionDAG.h:674
static bool Enabled
Definition: Statistic.cpp:51
const Function & getFunction() const
Return the LLVM function that this machine code represents.
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
bool lowerInterleavedStore(StoreInst *SI, ShuffleVectorInst *SVI, unsigned Factor) const override
Lower an interleaved store into a stN intrinsic.
static void changeFPCCToANDAArch64CC(ISD::CondCode CC, AArch64CC::CondCode &CondCode, AArch64CC::CondCode &CondCode2)
Convert a DAG fp condition code to an AArch64 CC.
unsigned logBase2() const
Definition: APInt.h:1741
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
static mvt_range integer_valuetypes()
static SDValue performFDivCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget)
Fold a floating-point divide by power of two into fixed-point to floating-point conversion.
MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const override
This method should be implemented by targets that mark instructions with the &#39;usesCustomInserter&#39; fla...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:539
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:924
static bool isZIPMask(ArrayRef< int > M, EVT VT, unsigned &WhichResult)
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:462
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:150
Class to represent vector types.
Definition: DerivedTypes.h:393
bool shouldExpandAtomicStoreInIR(StoreInst *SI) const override
Returns true if the given (atomic) store should be expanded by the IR-level AtomicExpand pass into an...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:56
const char * getTargetNodeName(unsigned Opcode) const override
This method returns the name of a target specific DAG node.
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:73
void setIndexedLoadAction(unsigned IdxMode, MVT VT, LegalizeAction Action)
Indicate that the specified indexed load does or does not work with the specified type and indicate w...
EVT getMemoryVT() const
Return the type of the in-memory value.
static unsigned getNZCVToSatisfyCondCode(CondCode Code)
Given a condition code, return NZCV flags that would satisfy that condition.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
static bool performTBISimplification(SDValue Addr, TargetLowering::DAGCombinerInfo &DCI, SelectionDAG &DAG)
Simplify Addr given that the top byte of it is ignored by HW during address translation.
Class for arbitrary precision integers.
Definition: APInt.h:69
unsigned getByValAlign() const
CodeModel::Model getCodeModel() const
Returns the code model.
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
Value * getIRStackGuard(IRBuilder<> &IRB) const override
If the target has a standard location for the stack protector cookie, returns the address of that loc...
iterator_range< use_iterator > uses()
A "pseudo-class" with methods for operating on BUILD_VECTORs.
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:392
void setMinFunctionAlignment(unsigned Align)
Set the target&#39;s minimum function alignment (in log2(bytes))
static void changeVectorFPCCToAArch64CC(ISD::CondCode CC, AArch64CC::CondCode &CondCode, AArch64CC::CondCode &CondCode2, bool &Invert)
changeVectorFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64 CC usable with the vector...
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:241
bool isPowerOf2() const
Check if this APInt&#39;s value is a power of two greater than zero.
Definition: APInt.h:463
static uint8_t encodeAdvSIMDModImmType10(uint64_t Imm)
static use_iterator use_end()
bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
void setPrefLoopAlignment(unsigned Align)
Set the target&#39;s preferred loop alignment.
void setMaximumJumpTableSize(unsigned)
Indicate the maximum number of entries in jump tables.
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:440
void AnalyzeCallOperands(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeCallOperands - Analyze the outgoing arguments to a call, incorporating info about the passed v...
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1051
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:443
static SDValue performNVCASTCombine(SDNode *N)
Get rid of unnecessary NVCASTs (that don&#39;t change the type).
bool isTailCall() const
static SDValue performMulCombine(SDNode *N, SelectionDAG &DAG, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget)
static SDValue tryAdvSIMDModImm64(unsigned NewOp, SDValue Op, SelectionDAG &DAG, const APInt &Bits)
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:478
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:288
FMINNAN/FMAXNAN - Behave identically to FMINNUM/FMAXNUM, except that when a single input is NaN...
Definition: ISDOpcodes.h:568
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1675
static SDValue skipExtensionForVectorMULL(SDNode *N, SelectionDAG &DAG)
static SDValue performPostLD1Combine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, bool IsLaneOp)
Target-specific DAG combine function for post-increment LD1 (lane) and post-increment LD1R...
Flags
Flags values. These may be or&#39;d together.
amdgpu Simplify well known AMD library false Value Value * Arg
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
The memory access reads data.
static uint8_t encodeAdvSIMDModImmType5(uint64_t Imm)
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:560
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
static bool isEquivalentMaskless(unsigned CC, unsigned width, ISD::LoadExtType ExtType, int AddConstant, int CompConstant)
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
CCAssignFn * CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg) const
Selects the correct CCAssignFn for a given CallingConvention value.
Value * CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1657
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:428
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:601
static mvt_range all_valuetypes()
SimpleValueType Iteration.
Representation of each machine instruction.
Definition: MachineInstr.h:60
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer, a SRCVALUE for the destination, and a SRCVALUE for the source.
Definition: ISDOpcodes.h:687
static bool isAdvSIMDModImmType10(uint64_t Imm)
#define FALKOR_STRIDED_ACCESS_MD
static uint8_t encodeAdvSIMDModImmType8(uint64_t Imm)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
These are IR-level optimization flags that may be propagated to SDNodes.
Libcall getFPTOUINT(EVT OpVT, EVT RetVT)
getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, unsigned char TargetFlags=0)
Definition: SelectionDAG.h:668
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
bool is64BitVector() const
Return true if this is a 64-bit vector type.
Definition: ValueTypes.h:177
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition: MathExtras.h:423
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 ...
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:151
MachineBasicBlock * EmitF128CSEL(MachineInstr &MI, MachineBasicBlock *BB) const
static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG)
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:363
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2, return the log base 2 integer value.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB &#39;Other&#39; at the position From, and insert it into this MBB right before &#39;...
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT)
Definition: SelectionDAG.h:700
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, bool isSigned, const SDLoc &dl, bool doesNotReturn=false, bool isReturnValueUsed=true) const
Returns a pair of (return value, chain).
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
unsigned getNumInterleavedAccesses(VectorType *VecTy, const DataLayout &DL) const
Returns the number of interleaved accesses that will be generated when lowering accesses of the given...
bool isPosZero() const
Definition: APFloat.h:1158
unsigned getLocMemOffset() const
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...
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo) const override
This method returns a target specific FastISel object, or null if the target does not support "fast" ...
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:206
static uint8_t encodeAdvSIMDModImmType4(uint64_t Imm)
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
unsigned getNumArgOperands() const
Return the number of call arguments.
bool is128BitVector() const
Return true if this is a 128-bit vector type.
Definition: ValueTypes.h:182
MO_DLLIMPORT - On a symbol operand, this represents that the reference to the symbol is for an import...
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:458
void computeKnownBits(SDValue Op, KnownBits &Known, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
static uint8_t encodeAdvSIMDModImmType11(uint64_t Imm)
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:577
PointerUnion< const Value *, const PseudoSourceValue * > ptrVal
bool isMaskAndCmp0FoldingBeneficial(const Instruction &AndI) const override
Return if the target supports combining a chain like:
TargetOptions Options
Definition: TargetMachine.h:98
static bool isSigned(Value *V)
Can the given value generate sign bits.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
#define I(x, y, z)
Definition: MD5.cpp:58
static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG)
#define N
void AnalyzeReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeReturn - Analyze the returned values of a return, incorporating info about the result values i...
const TargetSubtargetInfo & getSubtarget() const
Definition: SelectionDAG.h:400
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
Flags getFlags() const
Return the raw flags of the source value,.
unsigned MaxGluedStoresPerMemcpy
Specify max number of store instructions to glue in inlined memcpy.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1213
bool optForMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:581
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page...
The memory access always returns the same value (or traps).
unsigned MaxStoresPerMemmoveOptSize
Maximum number of store instructions that may be substituted for a call to memmove, used for functions with OptSize attribute.
unsigned MaxStoresPerMemcpyOptSize
Maximum number of store operations that may be substituted for a call to memcpy, used for functions w...
Value * emitLoadLinked(IRBuilder<> &Builder, Value *Addr, AtomicOrdering Ord) const override
Perform a load-linked operation on Addr, returning a "Value *" with the corresponding pointee type...
void setStackPointerRegisterToSaveRestore(unsigned R)
If set to a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save and restore.
static SDValue getTestBitOperand(SDValue Op, unsigned &Bit, bool &Invert, SelectionDAG &DAG)
static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, int64_t &Cnt)
isVShiftRImm - Check if this is a valid build_vector for the immediate operand of a vector shift righ...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
static bool optimizeLogicalImm(SDValue Op, unsigned Size, uint64_t Imm, const APInt &Demanded, TargetLowering::TargetLoweringOpt &TLO, unsigned NewOpc)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Type * getValueType() const
Definition: GlobalValue.h:275
static SDValue performIntrinsicCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const AArch64Subtarget *Subtarget)
Same for multiplication.
Definition: ISDOpcodes.h:257
static SDValue replaceSplatVectorStore(SelectionDAG &DAG, StoreSDNode &St)
Replace a splat of a scalar to a vector store by scalar stores of the scalar value.
static const int LAST_INDEXED_MODE
Definition: ISDOpcodes.h:876
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
unsigned getOpcode() const
FSINCOS - Compute both fsin and fcos as a single operation.
Definition: ISDOpcodes.h:571
SDValue getValue(unsigned R) const
bool useLoadStackGuardNode() const override
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
const uint32_t * getTLSCallPreservedMask() const
unsigned MaxStoresPerMemcpy
Specify maximum bytes of store instructions per memcpy call.
AArch64CC::CondCode CC
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1619
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
void setBytesInStackArgArea(unsigned bytes)
Libcall getSINTTOFP(EVT OpVT, EVT RetVT)
getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none...
bool isRegLoc() const
SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, const Value *PtrVal, unsigned Alignment, AtomicOrdering Ordering, SyncScope::ID SSID)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands...
static SDValue replaceZeroVectorStore(SelectionDAG &DAG, StoreSDNode &St)
Replace a splat of zeros to a vector store by scalar stores of WZR/XZR.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:280
const MachinePointerInfo & getPointerInfo() const
bool isLegalAddImmediate(int64_t) const override
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
ConstantSDNode * getConstantSplatNode(BitVector *UndefElements=nullptr) const
Returns the splatted constant or null if this is not a constant splat.
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:321
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if &#39;Op & Mask&#39; is known to be zero.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LegalizeTypeAction
This enum indicates whether a types are legal for a target, and if not, what action should be used to...
static AArch64CC::CondCode changeIntCCToAArch64CC(ISD::CondCode CC)
changeIntCCToAArch64CC - Convert a DAG integer condition code to an AArch64 CC
MachineMemOperand::Flags getMMOFlags(const Instruction &I) const override
This callback is used to inspect load/store instructions and add target-specific MachineMemOperand fl...
static bool isSetCC(SDValue Op, SetCCInfoAndKind &SetCCInfo)
Check whether or not Op is a SET_CC operation, either a generic or an AArch64 lowered one...
void setReturnAddressIsTaken(bool s)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
unsigned getVaListSizeInBits(const DataLayout &DL) const override
Returns the size of the platform&#39;s va_list object.
bool isPredecessorOf(const SDNode *N) const
Return true if this node is a predecessor of N.
void setSchedulingPreference(Sched::Preference Pref)
Specify the target scheduling preference.
static MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:115
ArrayRef< int > getMask() const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:73
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:278
SDValue getRegister(unsigned Reg, EVT VT)
unsigned getResNo() const
get the index which selects a specific result in the SDNode
SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:827
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
virtual void finalizeLowering(MachineFunction &MF) const
Execute target specific actions to finalize target lowering.
const AArch64InstrInfo * getInstrInfo() const override
static void ReplaceBITCASTResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:593
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
SDValue getValueType(EVT)
Value * emitStoreConditional(IRBuilder<> &Builder, Value *Val, Value *Addr, AtomicOrdering Ord) const override
Perform a store-conditional operation to Addr.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt)
getVShiftImm - Check if this is a valid build_vector for the immediate operand of a vector shift oper...
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:739
static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, bool isSigned)
bool isUndef() const
Return true if the type of the node type undefined.
bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override
Return true if folding a constant offset with the given GlobalAddress is legal.
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1072
unsigned getMaxSupportedInterleaveFactor() const override
Get the maximum supported factor for interleaved memory accesses.
void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone...
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
Type * getElementType() const
Definition: DerivedTypes.h:360
IRTranslator LLVM IR MI
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:412
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:238
static MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset, uint8_t ID=0)
Stack pointer relative access.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow...
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:415
SDValue ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const
static SDValue LowerMULH(SDValue Op, SelectionDAG &DAG)
unsigned MaxStoresPerMemset
Specify maximum number of store instructions per memset call.
static uint8_t encodeAdvSIMDModImmType9(uint64_t Imm)
unsigned MaxStoresPerMemsetOptSize
Maximum number of stores operations that may be substituted for the call to memset, used for functions with OptSize attribute.
static bool isConcatMask(ArrayRef< int > Mask, EVT VT, bool SplitLHS)
static bool isAdvSIMDModImmType11(uint64_t Imm)
bool hasPairedLoad(EVT LoadedType, unsigned &RequiredAligment) const override
Return true if the target supplies and combines to a paired load two loaded values of type LoadedType...
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1960
unsigned getNumOperands() const
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:198
static bool isVolatile(Instruction *Inst)
Conversion operators.
Definition: ISDOpcodes.h:437
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
Provide custom lowering hooks for some operations.
unsigned char classifyGlobalFunctionReference(const GlobalValue *GV, const TargetMachine &TM) const
const SDValue & getOperand(unsigned i) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
unsigned getLocReg() const
static SDValue performGlobalAddressCombine(SDNode *N, SelectionDAG &DAG, const AArch64Subtarget *Subtarget, const TargetMachine &TM)
uint64_t getZExtValue() const
bool isBigEndian() const
Definition: DataLayout.h:222
SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:446
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:126
static void Split(std::vector< std::string > &V, StringRef S)
Splits a string of comma separated items in to a vector of strings.
static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDValue CCOp, AArch64CC::CondCode Predicate, AArch64CC::CondCode OutCC, const SDLoc &DL, SelectionDAG &DAG)
Create a conditional comparison; Use CCMP, CCMN or FCCMP as appropriate.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:393
#define LLVM_DEBUG(X)
Definition: Debug.h:119
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:316
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW, FLOG, FLOG2, FLOG10, FEXP, FEXP2, FCEIL, FTRUNC, FRINT, FNEARBYINT, FROUND, FFLOOR - Perform various unary floating point operations.
Definition: ISDOpcodes.h:557
static uint8_t encodeAdvSIMDModImmType7(uint64_t Imm)
VectorType * getType() const
Overload to return most specific vector type.
The C convention as implemented on Windows/x86-64 and AArch64.
Definition: CallingConv.h:154
Value * getPointerOperand()
Definition: Instructions.h:402
static SDValue getEstimate(const AArch64Subtarget *ST, unsigned Opcode, SDValue Operand, SelectionDAG &DAG, int &ExtraSteps)
MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num)
allocateMemRefsArray - Allocate an array to hold MachineMemOperand pointers.
static SDValue performSRLCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
static cl::opt< bool > EnableOptimizeLogicalImm("aarch64-enable-logical-imm", cl::Hidden, cl::desc("Enable AArch64 logical imm instruction " "optimization"), cl::init(true))
const SDValue & getBasePtr() const
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:545
static const MachineMemOperand::Flags MOStridedAccess
LLVMContext * getContext() const
Definition: SelectionDAG.h:404
static SDValue tryAdvSIMDModImm16(unsigned NewOp, SDValue Op, SelectionDAG &DAG, const APInt &Bits, const SDValue *LHS=nullptr)
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:351
static bool isSplatMask(const int *Mask, EVT VT)
static bool resolveBuildVector(BuildVectorSDNode *BVN, APInt &CnstBits, APInt &UndefBits)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Type * getElementType() const
Definition: DerivedTypes.h:486
bool PredictableSelectIsExpensive
Tells the code generator that select is more expensive than a branch if the branch is usually predict...
unsigned getPrefLoopAlignment() const
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
static bool canGuaranteeTCO(CallingConv::ID CC)
Return true if the calling convention is one that we can guarantee TCO for.
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:221
void CommitTargetLoweringOpt(const TargetLoweringOpt &TLO)
void insertCopiesSplitCSR(MachineBasicBlock *Entry, const SmallVectorImpl< MachineBasicBlock *> &Exits) const override
Insert explicit copies in entry and exit blocks.
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned char TargetFlags=0)
Definition: SelectionDAG.h:617
bool isFMAFasterThanFMulAndFAdd(EVT VT) const override
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
static SDValue tryLowerToSLI(SDNode *N, SelectionDAG &DAG)
void setIndexedStoreAction(unsigned IdxMode, MVT VT, LegalizeAction Action)
Indicate that the specified indexed store does or does not work with the specified type and indicate ...
gep_type_iterator gep_type_begin(const User *GEP)
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1871
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:868
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:356
MachineBasicBlock * emitPatchPoint(MachineInstr &MI, MachineBasicBlock *MBB) const
Replace/modify any TargetFrameIndex operands with a targte-dependent sequence of memory operands that...
This class is used to represent ISD::LOAD nodes.
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary...
Definition: ISDOpcodes.h:586
static SDValue getAArch64Cmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDValue &AArch64cc, SelectionDAG &DAG, const SDLoc &dl)