1 //===- ARMISelLowering.cpp - ARM DAG Lowering Implementation --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the interfaces that ARM uses to lower LLVM code into a 10 // selection DAG. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMISelLowering.h" 15 #include "ARMBaseInstrInfo.h" 16 #include "ARMBaseRegisterInfo.h" 17 #include "ARMCallingConv.h" 18 #include "ARMConstantPoolValue.h" 19 #include "ARMMachineFunctionInfo.h" 20 #include "ARMPerfectShuffle.h" 21 #include "ARMRegisterInfo.h" 22 #include "ARMSelectionDAGInfo.h" 23 #include "ARMSubtarget.h" 24 #include "MCTargetDesc/ARMAddressingModes.h" 25 #include "MCTargetDesc/ARMBaseInfo.h" 26 #include "Utils/ARMBaseInfo.h" 27 #include "llvm/ADT/APFloat.h" 28 #include "llvm/ADT/APInt.h" 29 #include "llvm/ADT/ArrayRef.h" 30 #include "llvm/ADT/BitVector.h" 31 #include "llvm/ADT/DenseMap.h" 32 #include "llvm/ADT/STLExtras.h" 33 #include "llvm/ADT/SmallPtrSet.h" 34 #include "llvm/ADT/SmallVector.h" 35 #include "llvm/ADT/Statistic.h" 36 #include "llvm/ADT/StringExtras.h" 37 #include "llvm/ADT/StringRef.h" 38 #include "llvm/ADT/StringSwitch.h" 39 #include "llvm/ADT/Triple.h" 40 #include "llvm/ADT/Twine.h" 41 #include "llvm/Analysis/VectorUtils.h" 42 #include "llvm/CodeGen/CallingConvLower.h" 43 #include "llvm/CodeGen/ISDOpcodes.h" 44 #include "llvm/CodeGen/IntrinsicLowering.h" 45 #include "llvm/CodeGen/MachineBasicBlock.h" 46 #include "llvm/CodeGen/MachineConstantPool.h" 47 #include "llvm/CodeGen/MachineFrameInfo.h" 48 #include "llvm/CodeGen/MachineFunction.h" 49 #include "llvm/CodeGen/MachineInstr.h" 50 #include "llvm/CodeGen/MachineInstrBuilder.h" 51 #include "llvm/CodeGen/MachineJumpTableInfo.h" 52 #include "llvm/CodeGen/MachineMemOperand.h" 53 #include "llvm/CodeGen/MachineOperand.h" 54 #include "llvm/CodeGen/MachineRegisterInfo.h" 55 #include "llvm/CodeGen/RuntimeLibcalls.h" 56 #include "llvm/CodeGen/SelectionDAG.h" 57 #include "llvm/CodeGen/SelectionDAGNodes.h" 58 #include "llvm/CodeGen/TargetInstrInfo.h" 59 #include "llvm/CodeGen/TargetLowering.h" 60 #include "llvm/CodeGen/TargetOpcodes.h" 61 #include "llvm/CodeGen/TargetRegisterInfo.h" 62 #include "llvm/CodeGen/TargetSubtargetInfo.h" 63 #include "llvm/CodeGen/ValueTypes.h" 64 #include "llvm/IR/Attributes.h" 65 #include "llvm/IR/CallingConv.h" 66 #include "llvm/IR/Constant.h" 67 #include "llvm/IR/Constants.h" 68 #include "llvm/IR/DataLayout.h" 69 #include "llvm/IR/DebugLoc.h" 70 #include "llvm/IR/DerivedTypes.h" 71 #include "llvm/IR/Function.h" 72 #include "llvm/IR/GlobalAlias.h" 73 #include "llvm/IR/GlobalValue.h" 74 #include "llvm/IR/GlobalVariable.h" 75 #include "llvm/IR/IRBuilder.h" 76 #include "llvm/IR/InlineAsm.h" 77 #include "llvm/IR/Instruction.h" 78 #include "llvm/IR/Instructions.h" 79 #include "llvm/IR/IntrinsicInst.h" 80 #include "llvm/IR/Intrinsics.h" 81 #include "llvm/IR/Module.h" 82 #include "llvm/IR/PatternMatch.h" 83 #include "llvm/IR/Type.h" 84 #include "llvm/IR/User.h" 85 #include "llvm/IR/Value.h" 86 #include "llvm/MC/MCInstrDesc.h" 87 #include "llvm/MC/MCInstrItineraries.h" 88 #include "llvm/MC/MCRegisterInfo.h" 89 #include "llvm/MC/MCSchedule.h" 90 #include "llvm/Support/AtomicOrdering.h" 91 #include "llvm/Support/BranchProbability.h" 92 #include "llvm/Support/Casting.h" 93 #include "llvm/Support/CodeGen.h" 94 #include "llvm/Support/CommandLine.h" 95 #include "llvm/Support/Compiler.h" 96 #include "llvm/Support/Debug.h" 97 #include "llvm/Support/ErrorHandling.h" 98 #include "llvm/Support/KnownBits.h" 99 #include "llvm/Support/MachineValueType.h" 100 #include "llvm/Support/MathExtras.h" 101 #include "llvm/Support/raw_ostream.h" 102 #include "llvm/Target/TargetMachine.h" 103 #include "llvm/Target/TargetOptions.h" 104 #include <algorithm> 105 #include <cassert> 106 #include <cstdint> 107 #include <cstdlib> 108 #include <iterator> 109 #include <limits> 110 #include <string> 111 #include <tuple> 112 #include <utility> 113 #include <vector> 114 115 using namespace llvm; 116 using namespace llvm::PatternMatch; 117 118 #define DEBUG_TYPE "arm-isel" 119 120 STATISTIC(NumTailCalls, "Number of tail calls"); 121 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 122 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); 123 STATISTIC(NumConstpoolPromoted, 124 "Number of constants with their storage promoted into constant pools"); 125 126 static cl::opt<bool> 127 ARMInterworking("arm-interworking", cl::Hidden, 128 cl::desc("Enable / disable ARM interworking (for debugging only)"), 129 cl::init(true)); 130 131 static cl::opt<bool> EnableConstpoolPromotion( 132 "arm-promote-constant", cl::Hidden, 133 cl::desc("Enable / disable promotion of unnamed_addr constants into " 134 "constant pools"), 135 cl::init(false)); // FIXME: set to true by default once PR32780 is fixed 136 static cl::opt<unsigned> ConstpoolPromotionMaxSize( 137 "arm-promote-constant-max-size", cl::Hidden, 138 cl::desc("Maximum size of constant to promote into a constant pool"), 139 cl::init(64)); 140 static cl::opt<unsigned> ConstpoolPromotionMaxTotal( 141 "arm-promote-constant-max-total", cl::Hidden, 142 cl::desc("Maximum size of ALL constants to promote into a constant pool"), 143 cl::init(128)); 144 145 // The APCS parameter registers. 146 static const MCPhysReg GPRArgRegs[] = { 147 ARM::R0, ARM::R1, ARM::R2, ARM::R3 148 }; 149 150 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 151 MVT PromotedBitwiseVT) { 152 if (VT != PromotedLdStVT) { 153 setOperationAction(ISD::LOAD, VT, Promote); 154 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 155 156 setOperationAction(ISD::STORE, VT, Promote); 157 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 158 } 159 160 MVT ElemTy = VT.getVectorElementType(); 161 if (ElemTy != MVT::f64) 162 setOperationAction(ISD::SETCC, VT, Custom); 163 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 164 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 165 if (ElemTy == MVT::i32) { 166 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 167 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 168 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 169 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 170 } else { 171 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 172 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 173 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 174 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 175 } 176 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 177 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 178 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 179 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 180 setOperationAction(ISD::SELECT, VT, Expand); 181 setOperationAction(ISD::SELECT_CC, VT, Expand); 182 setOperationAction(ISD::VSELECT, VT, Expand); 183 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 184 if (VT.isInteger()) { 185 setOperationAction(ISD::SHL, VT, Custom); 186 setOperationAction(ISD::SRA, VT, Custom); 187 setOperationAction(ISD::SRL, VT, Custom); 188 } 189 190 // Promote all bit-wise operations. 191 if (VT.isInteger() && VT != PromotedBitwiseVT) { 192 setOperationAction(ISD::AND, VT, Promote); 193 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 194 setOperationAction(ISD::OR, VT, Promote); 195 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 196 setOperationAction(ISD::XOR, VT, Promote); 197 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 198 } 199 200 // Neon does not support vector divide/remainder operations. 201 setOperationAction(ISD::SDIV, VT, Expand); 202 setOperationAction(ISD::UDIV, VT, Expand); 203 setOperationAction(ISD::FDIV, VT, Expand); 204 setOperationAction(ISD::SREM, VT, Expand); 205 setOperationAction(ISD::UREM, VT, Expand); 206 setOperationAction(ISD::FREM, VT, Expand); 207 208 if (!VT.isFloatingPoint() && 209 VT != MVT::v2i64 && VT != MVT::v1i64) 210 for (auto Opcode : {ISD::ABS, ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX}) 211 setOperationAction(Opcode, VT, Legal); 212 } 213 214 void ARMTargetLowering::addDRTypeForNEON(MVT VT) { 215 addRegisterClass(VT, &ARM::DPRRegClass); 216 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 217 } 218 219 void ARMTargetLowering::addQRTypeForNEON(MVT VT) { 220 addRegisterClass(VT, &ARM::DPairRegClass); 221 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 222 } 223 224 ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM, 225 const ARMSubtarget &STI) 226 : TargetLowering(TM), Subtarget(&STI) { 227 RegInfo = Subtarget->getRegisterInfo(); 228 Itins = Subtarget->getInstrItineraryData(); 229 230 setBooleanContents(ZeroOrOneBooleanContent); 231 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 232 233 if (!Subtarget->isTargetDarwin() && !Subtarget->isTargetIOS() && 234 !Subtarget->isTargetWatchOS()) { 235 bool IsHFTarget = TM.Options.FloatABIType == FloatABI::Hard; 236 for (int LCID = 0; LCID < RTLIB::UNKNOWN_LIBCALL; ++LCID) 237 setLibcallCallingConv(static_cast<RTLIB::Libcall>(LCID), 238 IsHFTarget ? CallingConv::ARM_AAPCS_VFP 239 : CallingConv::ARM_AAPCS); 240 } 241 242 if (Subtarget->isTargetMachO()) { 243 // Uses VFP for Thumb libfuncs if available. 244 if (Subtarget->isThumb() && Subtarget->hasVFP2Base() && 245 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) { 246 static const struct { 247 const RTLIB::Libcall Op; 248 const char * const Name; 249 const ISD::CondCode Cond; 250 } LibraryCalls[] = { 251 // Single-precision floating-point arithmetic. 252 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID }, 253 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID }, 254 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID }, 255 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID }, 256 257 // Double-precision floating-point arithmetic. 258 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID }, 259 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID }, 260 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID }, 261 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID }, 262 263 // Single-precision comparisons. 264 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE }, 265 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE }, 266 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE }, 267 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE }, 268 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE }, 269 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE }, 270 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE }, 271 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ }, 272 273 // Double-precision comparisons. 274 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE }, 275 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE }, 276 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE }, 277 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE }, 278 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE }, 279 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE }, 280 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE }, 281 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ }, 282 283 // Floating-point to integer conversions. 284 // i64 conversions are done via library routines even when generating VFP 285 // instructions, so use the same ones. 286 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID }, 287 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID }, 288 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID }, 289 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID }, 290 291 // Conversions between floating types. 292 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID }, 293 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID }, 294 295 // Integer to floating-point conversions. 296 // i64 conversions are done via library routines even when generating VFP 297 // instructions, so use the same ones. 298 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 299 // e.g., __floatunsidf vs. __floatunssidfvfp. 300 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID }, 301 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID }, 302 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID }, 303 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID }, 304 }; 305 306 for (const auto &LC : LibraryCalls) { 307 setLibcallName(LC.Op, LC.Name); 308 if (LC.Cond != ISD::SETCC_INVALID) 309 setCmpLibcallCC(LC.Op, LC.Cond); 310 } 311 } 312 } 313 314 // These libcalls are not available in 32-bit. 315 setLibcallName(RTLIB::SHL_I128, nullptr); 316 setLibcallName(RTLIB::SRL_I128, nullptr); 317 setLibcallName(RTLIB::SRA_I128, nullptr); 318 319 // RTLIB 320 if (Subtarget->isAAPCS_ABI() && 321 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() || 322 Subtarget->isTargetMuslAEABI() || Subtarget->isTargetAndroid())) { 323 static const struct { 324 const RTLIB::Libcall Op; 325 const char * const Name; 326 const CallingConv::ID CC; 327 const ISD::CondCode Cond; 328 } LibraryCalls[] = { 329 // Double-precision floating-point arithmetic helper functions 330 // RTABI chapter 4.1.2, Table 2 331 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 332 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 333 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 334 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 335 336 // Double-precision floating-point comparison helper functions 337 // RTABI chapter 4.1.2, Table 3 338 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 339 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 340 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 341 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 342 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 343 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 344 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 345 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 346 347 // Single-precision floating-point arithmetic helper functions 348 // RTABI chapter 4.1.2, Table 4 349 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 350 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 351 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 352 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 353 354 // Single-precision floating-point comparison helper functions 355 // RTABI chapter 4.1.2, Table 5 356 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 357 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 358 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 359 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 360 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 361 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 362 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 363 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 364 365 // Floating-point to integer conversions. 366 // RTABI chapter 4.1.2, Table 6 367 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 368 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 369 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 370 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 371 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 372 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 373 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 374 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 375 376 // Conversions between floating types. 377 // RTABI chapter 4.1.2, Table 7 378 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 379 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 380 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 381 382 // Integer to floating-point conversions. 383 // RTABI chapter 4.1.2, Table 8 384 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 385 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 386 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 387 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 388 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 389 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 390 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 391 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 392 393 // Long long helper functions 394 // RTABI chapter 4.2, Table 9 395 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 396 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 397 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 398 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 399 400 // Integer division functions 401 // RTABI chapter 4.3.1 402 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 403 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 404 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 405 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 406 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 407 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 408 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 409 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 410 }; 411 412 for (const auto &LC : LibraryCalls) { 413 setLibcallName(LC.Op, LC.Name); 414 setLibcallCallingConv(LC.Op, LC.CC); 415 if (LC.Cond != ISD::SETCC_INVALID) 416 setCmpLibcallCC(LC.Op, LC.Cond); 417 } 418 419 // EABI dependent RTLIB 420 if (TM.Options.EABIVersion == EABI::EABI4 || 421 TM.Options.EABIVersion == EABI::EABI5) { 422 static const struct { 423 const RTLIB::Libcall Op; 424 const char *const Name; 425 const CallingConv::ID CC; 426 const ISD::CondCode Cond; 427 } MemOpsLibraryCalls[] = { 428 // Memory operations 429 // RTABI chapter 4.3.4 430 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 431 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 432 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 433 }; 434 435 for (const auto &LC : MemOpsLibraryCalls) { 436 setLibcallName(LC.Op, LC.Name); 437 setLibcallCallingConv(LC.Op, LC.CC); 438 if (LC.Cond != ISD::SETCC_INVALID) 439 setCmpLibcallCC(LC.Op, LC.Cond); 440 } 441 } 442 } 443 444 if (Subtarget->isTargetWindows()) { 445 static const struct { 446 const RTLIB::Libcall Op; 447 const char * const Name; 448 const CallingConv::ID CC; 449 } LibraryCalls[] = { 450 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP }, 451 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP }, 452 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP }, 453 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP }, 454 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP }, 455 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP }, 456 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP }, 457 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP }, 458 }; 459 460 for (const auto &LC : LibraryCalls) { 461 setLibcallName(LC.Op, LC.Name); 462 setLibcallCallingConv(LC.Op, LC.CC); 463 } 464 } 465 466 // Use divmod compiler-rt calls for iOS 5.0 and later. 467 if (Subtarget->isTargetMachO() && 468 !(Subtarget->isTargetIOS() && 469 Subtarget->getTargetTriple().isOSVersionLT(5, 0))) { 470 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 471 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 472 } 473 474 // The half <-> float conversion functions are always soft-float on 475 // non-watchos platforms, but are needed for some targets which use a 476 // hard-float calling convention by default. 477 if (!Subtarget->isTargetWatchABI()) { 478 if (Subtarget->isAAPCS_ABI()) { 479 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS); 480 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS); 481 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS); 482 } else { 483 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS); 484 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS); 485 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS); 486 } 487 } 488 489 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have 490 // a __gnu_ prefix (which is the default). 491 if (Subtarget->isTargetAEABI()) { 492 static const struct { 493 const RTLIB::Libcall Op; 494 const char * const Name; 495 const CallingConv::ID CC; 496 } LibraryCalls[] = { 497 { RTLIB::FPROUND_F32_F16, "__aeabi_f2h", CallingConv::ARM_AAPCS }, 498 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS }, 499 { RTLIB::FPEXT_F16_F32, "__aeabi_h2f", CallingConv::ARM_AAPCS }, 500 }; 501 502 for (const auto &LC : LibraryCalls) { 503 setLibcallName(LC.Op, LC.Name); 504 setLibcallCallingConv(LC.Op, LC.CC); 505 } 506 } 507 508 if (Subtarget->isThumb1Only()) 509 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 510 else 511 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 512 513 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2Base() && 514 !Subtarget->isThumb1Only()) { 515 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 516 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 517 } 518 519 if (Subtarget->hasFullFP16()) { 520 addRegisterClass(MVT::f16, &ARM::HPRRegClass); 521 setOperationAction(ISD::BITCAST, MVT::i16, Custom); 522 setOperationAction(ISD::BITCAST, MVT::i32, Custom); 523 setOperationAction(ISD::BITCAST, MVT::f16, Custom); 524 525 setOperationAction(ISD::FMINNUM, MVT::f16, Legal); 526 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal); 527 } 528 529 for (MVT VT : MVT::vector_valuetypes()) { 530 for (MVT InnerVT : MVT::vector_valuetypes()) { 531 setTruncStoreAction(VT, InnerVT, Expand); 532 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 533 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 534 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 535 } 536 537 setOperationAction(ISD::MULHS, VT, Expand); 538 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 539 setOperationAction(ISD::MULHU, VT, Expand); 540 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 541 542 setOperationAction(ISD::BSWAP, VT, Expand); 543 } 544 545 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 546 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 547 548 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom); 549 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom); 550 551 if (Subtarget->hasNEON()) { 552 addDRTypeForNEON(MVT::v2f32); 553 addDRTypeForNEON(MVT::v8i8); 554 addDRTypeForNEON(MVT::v4i16); 555 addDRTypeForNEON(MVT::v2i32); 556 addDRTypeForNEON(MVT::v1i64); 557 558 addQRTypeForNEON(MVT::v4f32); 559 addQRTypeForNEON(MVT::v2f64); 560 addQRTypeForNEON(MVT::v16i8); 561 addQRTypeForNEON(MVT::v8i16); 562 addQRTypeForNEON(MVT::v4i32); 563 addQRTypeForNEON(MVT::v2i64); 564 565 if (Subtarget->hasFullFP16()) { 566 addQRTypeForNEON(MVT::v8f16); 567 addDRTypeForNEON(MVT::v4f16); 568 } 569 570 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 571 // neither Neon nor VFP support any arithmetic operations on it. 572 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 573 // supported for v4f32. 574 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 575 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 576 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 577 // FIXME: Code duplication: FDIV and FREM are expanded always, see 578 // ARMTargetLowering::addTypeForNEON method for details. 579 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 580 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 581 // FIXME: Create unittest. 582 // In another words, find a way when "copysign" appears in DAG with vector 583 // operands. 584 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 585 // FIXME: Code duplication: SETCC has custom operation action, see 586 // ARMTargetLowering::addTypeForNEON method for details. 587 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 588 // FIXME: Create unittest for FNEG and for FABS. 589 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 590 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 591 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 592 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 593 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 594 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 595 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 596 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 597 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 598 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 599 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 600 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 601 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 602 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 603 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 604 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 605 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 606 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 607 608 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 609 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 610 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 611 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 612 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 613 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 614 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 615 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 616 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 617 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 618 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 619 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 620 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 621 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 622 623 // Mark v2f32 intrinsics. 624 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 625 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 626 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 627 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 628 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 629 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 630 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 631 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 632 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 633 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 634 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 635 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 636 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 637 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 638 639 // Neon does not support some operations on v1i64 and v2i64 types. 640 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 641 // Custom handling for some quad-vector types to detect VMULL. 642 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 643 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 644 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 645 // Custom handling for some vector types to avoid expensive expansions 646 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 647 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 648 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 649 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 650 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 651 // a destination type that is wider than the source, and nor does 652 // it have a FP_TO_[SU]INT instruction with a narrower destination than 653 // source. 654 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 655 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Custom); 656 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 657 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom); 658 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 659 setOperationAction(ISD::FP_TO_UINT, MVT::v8i16, Custom); 660 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 661 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom); 662 663 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 664 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 665 666 // NEON does not have single instruction CTPOP for vectors with element 667 // types wider than 8-bits. However, custom lowering can leverage the 668 // v8i8/v16i8 vcnt instruction. 669 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 670 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 671 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 672 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 673 setOperationAction(ISD::CTPOP, MVT::v1i64, Custom); 674 setOperationAction(ISD::CTPOP, MVT::v2i64, Custom); 675 676 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand); 677 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand); 678 679 // NEON does not have single instruction CTTZ for vectors. 680 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom); 681 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom); 682 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom); 683 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom); 684 685 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom); 686 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom); 687 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom); 688 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom); 689 690 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom); 691 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom); 692 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom); 693 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom); 694 695 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom); 696 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom); 697 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom); 698 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom); 699 700 // NEON only has FMA instructions as of VFP4. 701 if (!Subtarget->hasVFP4Base()) { 702 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 703 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 704 } 705 706 setTargetDAGCombine(ISD::INTRINSIC_VOID); 707 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 708 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 709 setTargetDAGCombine(ISD::SHL); 710 setTargetDAGCombine(ISD::SRL); 711 setTargetDAGCombine(ISD::SRA); 712 setTargetDAGCombine(ISD::SIGN_EXTEND); 713 setTargetDAGCombine(ISD::ZERO_EXTEND); 714 setTargetDAGCombine(ISD::ANY_EXTEND); 715 setTargetDAGCombine(ISD::BUILD_VECTOR); 716 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 717 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 718 setTargetDAGCombine(ISD::STORE); 719 setTargetDAGCombine(ISD::FP_TO_SINT); 720 setTargetDAGCombine(ISD::FP_TO_UINT); 721 setTargetDAGCombine(ISD::FDIV); 722 setTargetDAGCombine(ISD::LOAD); 723 724 // It is legal to extload from v4i8 to v4i16 or v4i32. 725 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16, 726 MVT::v2i32}) { 727 for (MVT VT : MVT::integer_vector_valuetypes()) { 728 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal); 729 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal); 730 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal); 731 } 732 } 733 } 734 735 if (!Subtarget->hasFP64()) { 736 // When targeting a floating-point unit with only single-precision 737 // operations, f64 is legal for the few double-precision instructions which 738 // are present However, no double-precision operations other than moves, 739 // loads and stores are provided by the hardware. 740 setOperationAction(ISD::FADD, MVT::f64, Expand); 741 setOperationAction(ISD::FSUB, MVT::f64, Expand); 742 setOperationAction(ISD::FMUL, MVT::f64, Expand); 743 setOperationAction(ISD::FMA, MVT::f64, Expand); 744 setOperationAction(ISD::FDIV, MVT::f64, Expand); 745 setOperationAction(ISD::FREM, MVT::f64, Expand); 746 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 747 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand); 748 setOperationAction(ISD::FNEG, MVT::f64, Expand); 749 setOperationAction(ISD::FABS, MVT::f64, Expand); 750 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 751 setOperationAction(ISD::FSIN, MVT::f64, Expand); 752 setOperationAction(ISD::FCOS, MVT::f64, Expand); 753 setOperationAction(ISD::FPOW, MVT::f64, Expand); 754 setOperationAction(ISD::FLOG, MVT::f64, Expand); 755 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 756 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 757 setOperationAction(ISD::FEXP, MVT::f64, Expand); 758 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 759 setOperationAction(ISD::FCEIL, MVT::f64, Expand); 760 setOperationAction(ISD::FTRUNC, MVT::f64, Expand); 761 setOperationAction(ISD::FRINT, MVT::f64, Expand); 762 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand); 763 setOperationAction(ISD::FFLOOR, MVT::f64, Expand); 764 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 765 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 766 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 767 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 768 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom); 769 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom); 770 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 771 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom); 772 } 773 774 computeRegisterProperties(Subtarget->getRegisterInfo()); 775 776 // ARM does not have floating-point extending loads. 777 for (MVT VT : MVT::fp_valuetypes()) { 778 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 779 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 780 } 781 782 // ... or truncating stores 783 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 784 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 785 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 786 787 // ARM does not have i1 sign extending load. 788 for (MVT VT : MVT::integer_valuetypes()) 789 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 790 791 // ARM supports all 4 flavors of integer indexed load / store. 792 if (!Subtarget->isThumb1Only()) { 793 for (unsigned im = (unsigned)ISD::PRE_INC; 794 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 795 setIndexedLoadAction(im, MVT::i1, Legal); 796 setIndexedLoadAction(im, MVT::i8, Legal); 797 setIndexedLoadAction(im, MVT::i16, Legal); 798 setIndexedLoadAction(im, MVT::i32, Legal); 799 setIndexedStoreAction(im, MVT::i1, Legal); 800 setIndexedStoreAction(im, MVT::i8, Legal); 801 setIndexedStoreAction(im, MVT::i16, Legal); 802 setIndexedStoreAction(im, MVT::i32, Legal); 803 } 804 } else { 805 // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}. 806 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal); 807 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal); 808 } 809 810 setOperationAction(ISD::SADDO, MVT::i32, Custom); 811 setOperationAction(ISD::UADDO, MVT::i32, Custom); 812 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 813 setOperationAction(ISD::USUBO, MVT::i32, Custom); 814 815 setOperationAction(ISD::ADDCARRY, MVT::i32, Custom); 816 setOperationAction(ISD::SUBCARRY, MVT::i32, Custom); 817 818 // i64 operation support. 819 setOperationAction(ISD::MUL, MVT::i64, Expand); 820 setOperationAction(ISD::MULHU, MVT::i32, Expand); 821 if (Subtarget->isThumb1Only()) { 822 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 823 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 824 } 825 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 826 || (Subtarget->isThumb2() && !Subtarget->hasDSP())) 827 setOperationAction(ISD::MULHS, MVT::i32, Expand); 828 829 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 830 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 831 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 832 setOperationAction(ISD::SRL, MVT::i64, Custom); 833 setOperationAction(ISD::SRA, MVT::i64, Custom); 834 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom); 835 836 // Expand to __aeabi_l{lsl,lsr,asr} calls for Thumb1. 837 if (Subtarget->isThumb1Only()) { 838 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); 839 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); 840 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); 841 } 842 843 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) 844 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 845 846 // ARM does not have ROTL. 847 setOperationAction(ISD::ROTL, MVT::i32, Expand); 848 for (MVT VT : MVT::vector_valuetypes()) { 849 setOperationAction(ISD::ROTL, VT, Expand); 850 setOperationAction(ISD::ROTR, VT, Expand); 851 } 852 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 853 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 854 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) { 855 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 856 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, LibCall); 857 } 858 859 // @llvm.readcyclecounter requires the Performance Monitors extension. 860 // Default to the 0 expansion on unsupported platforms. 861 // FIXME: Technically there are older ARM CPUs that have 862 // implementation-specific ways of obtaining this information. 863 if (Subtarget->hasPerfMon()) 864 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 865 866 // Only ARMv6 has BSWAP. 867 if (!Subtarget->hasV6Ops()) 868 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 869 870 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode() 871 : Subtarget->hasDivideInARMMode(); 872 if (!hasDivide) { 873 // These are expanded into libcalls if the cpu doesn't have HW divider. 874 setOperationAction(ISD::SDIV, MVT::i32, LibCall); 875 setOperationAction(ISD::UDIV, MVT::i32, LibCall); 876 } 877 878 if (Subtarget->isTargetWindows() && !Subtarget->hasDivideInThumbMode()) { 879 setOperationAction(ISD::SDIV, MVT::i32, Custom); 880 setOperationAction(ISD::UDIV, MVT::i32, Custom); 881 882 setOperationAction(ISD::SDIV, MVT::i64, Custom); 883 setOperationAction(ISD::UDIV, MVT::i64, Custom); 884 } 885 886 setOperationAction(ISD::SREM, MVT::i32, Expand); 887 setOperationAction(ISD::UREM, MVT::i32, Expand); 888 889 // Register based DivRem for AEABI (RTABI 4.2) 890 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 891 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() || 892 Subtarget->isTargetWindows()) { 893 setOperationAction(ISD::SREM, MVT::i64, Custom); 894 setOperationAction(ISD::UREM, MVT::i64, Custom); 895 HasStandaloneRem = false; 896 897 if (Subtarget->isTargetWindows()) { 898 const struct { 899 const RTLIB::Libcall Op; 900 const char * const Name; 901 const CallingConv::ID CC; 902 } LibraryCalls[] = { 903 { RTLIB::SDIVREM_I8, "__rt_sdiv", CallingConv::ARM_AAPCS }, 904 { RTLIB::SDIVREM_I16, "__rt_sdiv", CallingConv::ARM_AAPCS }, 905 { RTLIB::SDIVREM_I32, "__rt_sdiv", CallingConv::ARM_AAPCS }, 906 { RTLIB::SDIVREM_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS }, 907 908 { RTLIB::UDIVREM_I8, "__rt_udiv", CallingConv::ARM_AAPCS }, 909 { RTLIB::UDIVREM_I16, "__rt_udiv", CallingConv::ARM_AAPCS }, 910 { RTLIB::UDIVREM_I32, "__rt_udiv", CallingConv::ARM_AAPCS }, 911 { RTLIB::UDIVREM_I64, "__rt_udiv64", CallingConv::ARM_AAPCS }, 912 }; 913 914 for (const auto &LC : LibraryCalls) { 915 setLibcallName(LC.Op, LC.Name); 916 setLibcallCallingConv(LC.Op, LC.CC); 917 } 918 } else { 919 const struct { 920 const RTLIB::Libcall Op; 921 const char * const Name; 922 const CallingConv::ID CC; 923 } LibraryCalls[] = { 924 { RTLIB::SDIVREM_I8, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 925 { RTLIB::SDIVREM_I16, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 926 { RTLIB::SDIVREM_I32, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 927 { RTLIB::SDIVREM_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS }, 928 929 { RTLIB::UDIVREM_I8, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 930 { RTLIB::UDIVREM_I16, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 931 { RTLIB::UDIVREM_I32, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 932 { RTLIB::UDIVREM_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS }, 933 }; 934 935 for (const auto &LC : LibraryCalls) { 936 setLibcallName(LC.Op, LC.Name); 937 setLibcallCallingConv(LC.Op, LC.CC); 938 } 939 } 940 941 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 942 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 943 setOperationAction(ISD::SDIVREM, MVT::i64, Custom); 944 setOperationAction(ISD::UDIVREM, MVT::i64, Custom); 945 } else { 946 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 947 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 948 } 949 950 if (Subtarget->isTargetWindows() && Subtarget->getTargetTriple().isOSMSVCRT()) 951 for (auto &VT : {MVT::f32, MVT::f64}) 952 setOperationAction(ISD::FPOWI, VT, Custom); 953 954 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 955 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 956 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 957 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 958 959 setOperationAction(ISD::TRAP, MVT::Other, Legal); 960 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal); 961 962 // Use the default implementation. 963 setOperationAction(ISD::VASTART, MVT::Other, Custom); 964 setOperationAction(ISD::VAARG, MVT::Other, Expand); 965 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 966 setOperationAction(ISD::VAEND, MVT::Other, Expand); 967 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 968 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 969 970 if (Subtarget->isTargetWindows()) 971 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 972 else 973 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 974 975 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 976 // the default expansion. 977 InsertFencesForAtomic = false; 978 if (Subtarget->hasAnyDataBarrier() && 979 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) { 980 // ATOMIC_FENCE needs custom lowering; the others should have been expanded 981 // to ldrex/strex loops already. 982 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 983 if (!Subtarget->isThumb() || !Subtarget->isMClass()) 984 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 985 986 // On v8, we have particularly efficient implementations of atomic fences 987 // if they can be combined with nearby atomic loads and stores. 988 if (!Subtarget->hasAcquireRelease() || 989 getTargetMachine().getOptLevel() == 0) { 990 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc. 991 InsertFencesForAtomic = true; 992 } 993 } else { 994 // If there's anything we can use as a barrier, go through custom lowering 995 // for ATOMIC_FENCE. 996 // If target has DMB in thumb, Fences can be inserted. 997 if (Subtarget->hasDataBarrier()) 998 InsertFencesForAtomic = true; 999 1000 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, 1001 Subtarget->hasAnyDataBarrier() ? Custom : Expand); 1002 1003 // Set them all for expansion, which will force libcalls. 1004 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 1005 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 1006 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 1007 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 1008 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 1009 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 1010 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 1011 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 1012 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 1013 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 1014 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 1015 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 1016 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 1017 // Unordered/Monotonic case. 1018 if (!InsertFencesForAtomic) { 1019 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 1020 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 1021 } 1022 } 1023 1024 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 1025 1026 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 1027 if (!Subtarget->hasV6Ops()) { 1028 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 1029 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 1030 } 1031 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 1032 1033 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2Base() && 1034 !Subtarget->isThumb1Only()) { 1035 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 1036 // iff target supports vfp2. 1037 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 1038 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 1039 } 1040 1041 // We want to custom lower some of our intrinsics. 1042 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 1043 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 1044 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 1045 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom); 1046 if (Subtarget->useSjLjEH()) 1047 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 1048 1049 setOperationAction(ISD::SETCC, MVT::i32, Expand); 1050 setOperationAction(ISD::SETCC, MVT::f32, Expand); 1051 setOperationAction(ISD::SETCC, MVT::f64, Expand); 1052 setOperationAction(ISD::SELECT, MVT::i32, Custom); 1053 setOperationAction(ISD::SELECT, MVT::f32, Custom); 1054 setOperationAction(ISD::SELECT, MVT::f64, Custom); 1055 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 1056 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 1057 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 1058 if (Subtarget->hasFullFP16()) { 1059 setOperationAction(ISD::SETCC, MVT::f16, Expand); 1060 setOperationAction(ISD::SELECT, MVT::f16, Custom); 1061 setOperationAction(ISD::SELECT_CC, MVT::f16, Custom); 1062 } 1063 1064 setOperationAction(ISD::SETCCCARRY, MVT::i32, Custom); 1065 1066 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 1067 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 1068 if (Subtarget->hasFullFP16()) 1069 setOperationAction(ISD::BR_CC, MVT::f16, Custom); 1070 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 1071 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 1072 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 1073 1074 // We don't support sin/cos/fmod/copysign/pow 1075 setOperationAction(ISD::FSIN, MVT::f64, Expand); 1076 setOperationAction(ISD::FSIN, MVT::f32, Expand); 1077 setOperationAction(ISD::FCOS, MVT::f32, Expand); 1078 setOperationAction(ISD::FCOS, MVT::f64, Expand); 1079 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 1080 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 1081 setOperationAction(ISD::FREM, MVT::f64, Expand); 1082 setOperationAction(ISD::FREM, MVT::f32, Expand); 1083 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2Base() && 1084 !Subtarget->isThumb1Only()) { 1085 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 1086 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 1087 } 1088 setOperationAction(ISD::FPOW, MVT::f64, Expand); 1089 setOperationAction(ISD::FPOW, MVT::f32, Expand); 1090 1091 if (!Subtarget->hasVFP4Base()) { 1092 setOperationAction(ISD::FMA, MVT::f64, Expand); 1093 setOperationAction(ISD::FMA, MVT::f32, Expand); 1094 } 1095 1096 // Various VFP goodness 1097 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) { 1098 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded. 1099 if (!Subtarget->hasFPARMv8Base() || !Subtarget->hasFP64()) { 1100 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 1101 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 1102 } 1103 1104 // fp16 is a special v7 extension that adds f16 <-> f32 conversions. 1105 if (!Subtarget->hasFP16()) { 1106 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 1107 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 1108 } 1109 } 1110 1111 // Use __sincos_stret if available. 1112 if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr && 1113 getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) { 1114 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 1115 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 1116 } 1117 1118 // FP-ARMv8 implements a lot of rounding-like FP operations. 1119 if (Subtarget->hasFPARMv8Base()) { 1120 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 1121 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 1122 setOperationAction(ISD::FROUND, MVT::f32, Legal); 1123 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 1124 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 1125 setOperationAction(ISD::FRINT, MVT::f32, Legal); 1126 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 1127 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 1128 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal); 1129 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal); 1130 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); 1131 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); 1132 1133 if (Subtarget->hasFP64()) { 1134 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 1135 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 1136 setOperationAction(ISD::FROUND, MVT::f64, Legal); 1137 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 1138 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 1139 setOperationAction(ISD::FRINT, MVT::f64, Legal); 1140 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 1141 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 1142 } 1143 } 1144 1145 // FP16 often need to be promoted to call lib functions 1146 if (Subtarget->hasFullFP16()) { 1147 setOperationAction(ISD::FREM, MVT::f16, Promote); 1148 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Expand); 1149 setOperationAction(ISD::FSIN, MVT::f16, Promote); 1150 setOperationAction(ISD::FCOS, MVT::f16, Promote); 1151 setOperationAction(ISD::FSINCOS, MVT::f16, Promote); 1152 setOperationAction(ISD::FPOWI, MVT::f16, Promote); 1153 setOperationAction(ISD::FPOW, MVT::f16, Promote); 1154 setOperationAction(ISD::FEXP, MVT::f16, Promote); 1155 setOperationAction(ISD::FEXP2, MVT::f16, Promote); 1156 setOperationAction(ISD::FLOG, MVT::f16, Promote); 1157 setOperationAction(ISD::FLOG10, MVT::f16, Promote); 1158 setOperationAction(ISD::FLOG2, MVT::f16, Promote); 1159 1160 setOperationAction(ISD::FROUND, MVT::f16, Legal); 1161 } 1162 1163 if (Subtarget->hasNEON()) { 1164 // vmin and vmax aren't available in a scalar form, so we use 1165 // a NEON instruction with an undef lane instead. 1166 setOperationAction(ISD::FMINIMUM, MVT::f16, Legal); 1167 setOperationAction(ISD::FMAXIMUM, MVT::f16, Legal); 1168 setOperationAction(ISD::FMINIMUM, MVT::f32, Legal); 1169 setOperationAction(ISD::FMAXIMUM, MVT::f32, Legal); 1170 setOperationAction(ISD::FMINIMUM, MVT::v2f32, Legal); 1171 setOperationAction(ISD::FMAXIMUM, MVT::v2f32, Legal); 1172 setOperationAction(ISD::FMINIMUM, MVT::v4f32, Legal); 1173 setOperationAction(ISD::FMAXIMUM, MVT::v4f32, Legal); 1174 1175 if (Subtarget->hasFullFP16()) { 1176 setOperationAction(ISD::FMINNUM, MVT::v4f16, Legal); 1177 setOperationAction(ISD::FMAXNUM, MVT::v4f16, Legal); 1178 setOperationAction(ISD::FMINNUM, MVT::v8f16, Legal); 1179 setOperationAction(ISD::FMAXNUM, MVT::v8f16, Legal); 1180 1181 setOperationAction(ISD::FMINIMUM, MVT::v4f16, Legal); 1182 setOperationAction(ISD::FMAXIMUM, MVT::v4f16, Legal); 1183 setOperationAction(ISD::FMINIMUM, MVT::v8f16, Legal); 1184 setOperationAction(ISD::FMAXIMUM, MVT::v8f16, Legal); 1185 } 1186 } 1187 1188 // We have target-specific dag combine patterns for the following nodes: 1189 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 1190 setTargetDAGCombine(ISD::ADD); 1191 setTargetDAGCombine(ISD::SUB); 1192 setTargetDAGCombine(ISD::MUL); 1193 setTargetDAGCombine(ISD::AND); 1194 setTargetDAGCombine(ISD::OR); 1195 setTargetDAGCombine(ISD::XOR); 1196 1197 if (Subtarget->hasV6Ops()) 1198 setTargetDAGCombine(ISD::SRL); 1199 if (Subtarget->isThumb1Only()) 1200 setTargetDAGCombine(ISD::SHL); 1201 1202 setStackPointerRegisterToSaveRestore(ARM::SP); 1203 1204 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() || 1205 !Subtarget->hasVFP2Base() || Subtarget->hasMinSize()) 1206 setSchedulingPreference(Sched::RegPressure); 1207 else 1208 setSchedulingPreference(Sched::Hybrid); 1209 1210 //// temporary - rewrite interface to use type 1211 MaxStoresPerMemset = 8; 1212 MaxStoresPerMemsetOptSize = 4; 1213 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 1214 MaxStoresPerMemcpyOptSize = 2; 1215 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 1216 MaxStoresPerMemmoveOptSize = 2; 1217 1218 // On ARM arguments smaller than 4 bytes are extended, so all arguments 1219 // are at least 4 bytes aligned. 1220 setMinStackArgumentAlignment(4); 1221 1222 // Prefer likely predicted branches to selects on out-of-order cores. 1223 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder(); 1224 1225 setPrefLoopAlignment(Subtarget->getPrefLoopAlignment()); 1226 1227 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 1228 1229 if (Subtarget->isThumb() || Subtarget->isThumb2()) 1230 setTargetDAGCombine(ISD::ABS); 1231 } 1232 1233 bool ARMTargetLowering::useSoftFloat() const { 1234 return Subtarget->useSoftFloat(); 1235 } 1236 1237 // FIXME: It might make sense to define the representative register class as the 1238 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 1239 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 1240 // SPR's representative would be DPR_VFP2. This should work well if register 1241 // pressure tracking were modified such that a register use would increment the 1242 // pressure of the register class's representative and all of it's super 1243 // classes' representatives transitively. We have not implemented this because 1244 // of the difficulty prior to coalescing of modeling operand register classes 1245 // due to the common occurrence of cross class copies and subregister insertions 1246 // and extractions. 1247 std::pair<const TargetRegisterClass *, uint8_t> 1248 ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI, 1249 MVT VT) const { 1250 const TargetRegisterClass *RRC = nullptr; 1251 uint8_t Cost = 1; 1252 switch (VT.SimpleTy) { 1253 default: 1254 return TargetLowering::findRepresentativeClass(TRI, VT); 1255 // Use DPR as representative register class for all floating point 1256 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 1257 // the cost is 1 for both f32 and f64. 1258 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 1259 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 1260 RRC = &ARM::DPRRegClass; 1261 // When NEON is used for SP, only half of the register file is available 1262 // because operations that define both SP and DP results will be constrained 1263 // to the VFP2 class (D0-D15). We currently model this constraint prior to 1264 // coalescing by double-counting the SP regs. See the FIXME above. 1265 if (Subtarget->useNEONForSinglePrecisionFP()) 1266 Cost = 2; 1267 break; 1268 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 1269 case MVT::v4f32: case MVT::v2f64: 1270 RRC = &ARM::DPRRegClass; 1271 Cost = 2; 1272 break; 1273 case MVT::v4i64: 1274 RRC = &ARM::DPRRegClass; 1275 Cost = 4; 1276 break; 1277 case MVT::v8i64: 1278 RRC = &ARM::DPRRegClass; 1279 Cost = 8; 1280 break; 1281 } 1282 return std::make_pair(RRC, Cost); 1283 } 1284 1285 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 1286 switch ((ARMISD::NodeType)Opcode) { 1287 case ARMISD::FIRST_NUMBER: break; 1288 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 1289 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 1290 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 1291 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL"; 1292 case ARMISD::CALL: return "ARMISD::CALL"; 1293 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 1294 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 1295 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 1296 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 1297 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 1298 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 1299 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG"; 1300 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 1301 case ARMISD::CMP: return "ARMISD::CMP"; 1302 case ARMISD::CMN: return "ARMISD::CMN"; 1303 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 1304 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 1305 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 1306 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 1307 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 1308 1309 case ARMISD::CMOV: return "ARMISD::CMOV"; 1310 case ARMISD::SUBS: return "ARMISD::SUBS"; 1311 1312 case ARMISD::SSAT: return "ARMISD::SSAT"; 1313 case ARMISD::USAT: return "ARMISD::USAT"; 1314 1315 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 1316 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 1317 case ARMISD::RRX: return "ARMISD::RRX"; 1318 1319 case ARMISD::ADDC: return "ARMISD::ADDC"; 1320 case ARMISD::ADDE: return "ARMISD::ADDE"; 1321 case ARMISD::SUBC: return "ARMISD::SUBC"; 1322 case ARMISD::SUBE: return "ARMISD::SUBE"; 1323 1324 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1325 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1326 case ARMISD::VMOVhr: return "ARMISD::VMOVhr"; 1327 case ARMISD::VMOVrh: return "ARMISD::VMOVrh"; 1328 case ARMISD::VMOVSR: return "ARMISD::VMOVSR"; 1329 1330 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1331 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP"; 1332 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH"; 1333 1334 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1335 1336 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1337 1338 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1339 1340 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1341 1342 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1343 1344 case ARMISD::WIN__CHKSTK: return "ARMISD::WIN__CHKSTK"; 1345 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK"; 1346 1347 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 1348 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 1349 case ARMISD::VCGE: return "ARMISD::VCGE"; 1350 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 1351 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 1352 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 1353 case ARMISD::VCGT: return "ARMISD::VCGT"; 1354 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 1355 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 1356 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 1357 case ARMISD::VTST: return "ARMISD::VTST"; 1358 1359 case ARMISD::VSHL: return "ARMISD::VSHL"; 1360 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 1361 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 1362 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 1363 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 1364 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 1365 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 1366 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 1367 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 1368 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 1369 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 1370 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 1371 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 1372 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 1373 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 1374 case ARMISD::VSLI: return "ARMISD::VSLI"; 1375 case ARMISD::VSRI: return "ARMISD::VSRI"; 1376 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1377 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1378 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1379 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1380 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1381 case ARMISD::VDUP: return "ARMISD::VDUP"; 1382 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1383 case ARMISD::VEXT: return "ARMISD::VEXT"; 1384 case ARMISD::VREV64: return "ARMISD::VREV64"; 1385 case ARMISD::VREV32: return "ARMISD::VREV32"; 1386 case ARMISD::VREV16: return "ARMISD::VREV16"; 1387 case ARMISD::VZIP: return "ARMISD::VZIP"; 1388 case ARMISD::VUZP: return "ARMISD::VUZP"; 1389 case ARMISD::VTRN: return "ARMISD::VTRN"; 1390 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1391 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1392 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1393 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1394 case ARMISD::UMAAL: return "ARMISD::UMAAL"; 1395 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1396 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1397 case ARMISD::SMLALBB: return "ARMISD::SMLALBB"; 1398 case ARMISD::SMLALBT: return "ARMISD::SMLALBT"; 1399 case ARMISD::SMLALTB: return "ARMISD::SMLALTB"; 1400 case ARMISD::SMLALTT: return "ARMISD::SMLALTT"; 1401 case ARMISD::SMULWB: return "ARMISD::SMULWB"; 1402 case ARMISD::SMULWT: return "ARMISD::SMULWT"; 1403 case ARMISD::SMLALD: return "ARMISD::SMLALD"; 1404 case ARMISD::SMLALDX: return "ARMISD::SMLALDX"; 1405 case ARMISD::SMLSLD: return "ARMISD::SMLSLD"; 1406 case ARMISD::SMLSLDX: return "ARMISD::SMLSLDX"; 1407 case ARMISD::SMMLAR: return "ARMISD::SMMLAR"; 1408 case ARMISD::SMMLSR: return "ARMISD::SMMLSR"; 1409 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1410 case ARMISD::BFI: return "ARMISD::BFI"; 1411 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1412 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1413 case ARMISD::VBSL: return "ARMISD::VBSL"; 1414 case ARMISD::MEMCPY: return "ARMISD::MEMCPY"; 1415 case ARMISD::VLD1DUP: return "ARMISD::VLD1DUP"; 1416 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1417 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1418 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1419 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1420 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1421 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1422 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1423 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1424 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1425 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1426 case ARMISD::VLD1DUP_UPD: return "ARMISD::VLD1DUP_UPD"; 1427 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1428 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1429 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1430 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1431 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1432 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1433 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1434 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1435 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1436 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1437 } 1438 return nullptr; 1439 } 1440 1441 EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 1442 EVT VT) const { 1443 if (!VT.isVector()) 1444 return getPointerTy(DL); 1445 return VT.changeVectorElementTypeToInteger(); 1446 } 1447 1448 /// getRegClassFor - Return the register class that should be used for the 1449 /// specified value type. 1450 const TargetRegisterClass * 1451 ARMTargetLowering::getRegClassFor(MVT VT, bool isDivergent) const { 1452 (void)isDivergent; 1453 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1454 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1455 // load / store 4 to 8 consecutive D registers. 1456 if (Subtarget->hasNEON()) { 1457 if (VT == MVT::v4i64) 1458 return &ARM::QQPRRegClass; 1459 if (VT == MVT::v8i64) 1460 return &ARM::QQQQPRRegClass; 1461 } 1462 return TargetLowering::getRegClassFor(VT); 1463 } 1464 1465 // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the 1466 // source/dest is aligned and the copy size is large enough. We therefore want 1467 // to align such objects passed to memory intrinsics. 1468 bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize, 1469 unsigned &PrefAlign) const { 1470 if (!isa<MemIntrinsic>(CI)) 1471 return false; 1472 MinSize = 8; 1473 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1 1474 // cycle faster than 4-byte aligned LDM. 1475 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4); 1476 return true; 1477 } 1478 1479 // Create a fast isel object. 1480 FastISel * 1481 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1482 const TargetLibraryInfo *libInfo) const { 1483 return ARM::createFastISel(funcInfo, libInfo); 1484 } 1485 1486 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1487 unsigned NumVals = N->getNumValues(); 1488 if (!NumVals) 1489 return Sched::RegPressure; 1490 1491 for (unsigned i = 0; i != NumVals; ++i) { 1492 EVT VT = N->getValueType(i); 1493 if (VT == MVT::Glue || VT == MVT::Other) 1494 continue; 1495 if (VT.isFloatingPoint() || VT.isVector()) 1496 return Sched::ILP; 1497 } 1498 1499 if (!N->isMachineOpcode()) 1500 return Sched::RegPressure; 1501 1502 // Load are scheduled for latency even if there instruction itinerary 1503 // is not available. 1504 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1505 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1506 1507 if (MCID.getNumDefs() == 0) 1508 return Sched::RegPressure; 1509 if (!Itins->isEmpty() && 1510 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1511 return Sched::ILP; 1512 1513 return Sched::RegPressure; 1514 } 1515 1516 //===----------------------------------------------------------------------===// 1517 // Lowering Code 1518 //===----------------------------------------------------------------------===// 1519 1520 static bool isSRL16(const SDValue &Op) { 1521 if (Op.getOpcode() != ISD::SRL) 1522 return false; 1523 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1524 return Const->getZExtValue() == 16; 1525 return false; 1526 } 1527 1528 static bool isSRA16(const SDValue &Op) { 1529 if (Op.getOpcode() != ISD::SRA) 1530 return false; 1531 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1532 return Const->getZExtValue() == 16; 1533 return false; 1534 } 1535 1536 static bool isSHL16(const SDValue &Op) { 1537 if (Op.getOpcode() != ISD::SHL) 1538 return false; 1539 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1540 return Const->getZExtValue() == 16; 1541 return false; 1542 } 1543 1544 // Check for a signed 16-bit value. We special case SRA because it makes it 1545 // more simple when also looking for SRAs that aren't sign extending a 1546 // smaller value. Without the check, we'd need to take extra care with 1547 // checking order for some operations. 1548 static bool isS16(const SDValue &Op, SelectionDAG &DAG) { 1549 if (isSRA16(Op)) 1550 return isSHL16(Op.getOperand(0)); 1551 return DAG.ComputeNumSignBits(Op) == 17; 1552 } 1553 1554 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1555 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1556 switch (CC) { 1557 default: llvm_unreachable("Unknown condition code!"); 1558 case ISD::SETNE: return ARMCC::NE; 1559 case ISD::SETEQ: return ARMCC::EQ; 1560 case ISD::SETGT: return ARMCC::GT; 1561 case ISD::SETGE: return ARMCC::GE; 1562 case ISD::SETLT: return ARMCC::LT; 1563 case ISD::SETLE: return ARMCC::LE; 1564 case ISD::SETUGT: return ARMCC::HI; 1565 case ISD::SETUGE: return ARMCC::HS; 1566 case ISD::SETULT: return ARMCC::LO; 1567 case ISD::SETULE: return ARMCC::LS; 1568 } 1569 } 1570 1571 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1572 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1573 ARMCC::CondCodes &CondCode2, bool &InvalidOnQNaN) { 1574 CondCode2 = ARMCC::AL; 1575 InvalidOnQNaN = true; 1576 switch (CC) { 1577 default: llvm_unreachable("Unknown FP condition!"); 1578 case ISD::SETEQ: 1579 case ISD::SETOEQ: 1580 CondCode = ARMCC::EQ; 1581 InvalidOnQNaN = false; 1582 break; 1583 case ISD::SETGT: 1584 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1585 case ISD::SETGE: 1586 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1587 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1588 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1589 case ISD::SETONE: 1590 CondCode = ARMCC::MI; 1591 CondCode2 = ARMCC::GT; 1592 InvalidOnQNaN = false; 1593 break; 1594 case ISD::SETO: CondCode = ARMCC::VC; break; 1595 case ISD::SETUO: CondCode = ARMCC::VS; break; 1596 case ISD::SETUEQ: 1597 CondCode = ARMCC::EQ; 1598 CondCode2 = ARMCC::VS; 1599 InvalidOnQNaN = false; 1600 break; 1601 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1602 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1603 case ISD::SETLT: 1604 case ISD::SETULT: CondCode = ARMCC::LT; break; 1605 case ISD::SETLE: 1606 case ISD::SETULE: CondCode = ARMCC::LE; break; 1607 case ISD::SETNE: 1608 case ISD::SETUNE: 1609 CondCode = ARMCC::NE; 1610 InvalidOnQNaN = false; 1611 break; 1612 } 1613 } 1614 1615 //===----------------------------------------------------------------------===// 1616 // Calling Convention Implementation 1617 //===----------------------------------------------------------------------===// 1618 1619 /// getEffectiveCallingConv - Get the effective calling convention, taking into 1620 /// account presence of floating point hardware and calling convention 1621 /// limitations, such as support for variadic functions. 1622 CallingConv::ID 1623 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC, 1624 bool isVarArg) const { 1625 switch (CC) { 1626 default: 1627 report_fatal_error("Unsupported calling convention"); 1628 case CallingConv::ARM_AAPCS: 1629 case CallingConv::ARM_APCS: 1630 case CallingConv::GHC: 1631 return CC; 1632 case CallingConv::PreserveMost: 1633 return CallingConv::PreserveMost; 1634 case CallingConv::ARM_AAPCS_VFP: 1635 case CallingConv::Swift: 1636 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP; 1637 case CallingConv::C: 1638 if (!Subtarget->isAAPCS_ABI()) 1639 return CallingConv::ARM_APCS; 1640 else if (Subtarget->hasVFP2Base() && !Subtarget->isThumb1Only() && 1641 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1642 !isVarArg) 1643 return CallingConv::ARM_AAPCS_VFP; 1644 else 1645 return CallingConv::ARM_AAPCS; 1646 case CallingConv::Fast: 1647 case CallingConv::CXX_FAST_TLS: 1648 if (!Subtarget->isAAPCS_ABI()) { 1649 if (Subtarget->hasVFP2Base() && !Subtarget->isThumb1Only() && !isVarArg) 1650 return CallingConv::Fast; 1651 return CallingConv::ARM_APCS; 1652 } else if (Subtarget->hasVFP2Base() && 1653 !Subtarget->isThumb1Only() && !isVarArg) 1654 return CallingConv::ARM_AAPCS_VFP; 1655 else 1656 return CallingConv::ARM_AAPCS; 1657 } 1658 } 1659 1660 CCAssignFn *ARMTargetLowering::CCAssignFnForCall(CallingConv::ID CC, 1661 bool isVarArg) const { 1662 return CCAssignFnForNode(CC, false, isVarArg); 1663 } 1664 1665 CCAssignFn *ARMTargetLowering::CCAssignFnForReturn(CallingConv::ID CC, 1666 bool isVarArg) const { 1667 return CCAssignFnForNode(CC, true, isVarArg); 1668 } 1669 1670 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given 1671 /// CallingConvention. 1672 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1673 bool Return, 1674 bool isVarArg) const { 1675 switch (getEffectiveCallingConv(CC, isVarArg)) { 1676 default: 1677 report_fatal_error("Unsupported calling convention"); 1678 case CallingConv::ARM_APCS: 1679 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1680 case CallingConv::ARM_AAPCS: 1681 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1682 case CallingConv::ARM_AAPCS_VFP: 1683 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1684 case CallingConv::Fast: 1685 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1686 case CallingConv::GHC: 1687 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1688 case CallingConv::PreserveMost: 1689 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1690 } 1691 } 1692 1693 /// LowerCallResult - Lower the result values of a call into the 1694 /// appropriate copies out of appropriate physical registers. 1695 SDValue ARMTargetLowering::LowerCallResult( 1696 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, 1697 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 1698 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn, 1699 SDValue ThisVal) const { 1700 // Assign locations to each value returned by this call. 1701 SmallVector<CCValAssign, 16> RVLocs; 1702 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1703 *DAG.getContext()); 1704 CCInfo.AnalyzeCallResult(Ins, CCAssignFnForReturn(CallConv, isVarArg)); 1705 1706 // Copy all of the result registers out of their specified physreg. 1707 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1708 CCValAssign VA = RVLocs[i]; 1709 1710 // Pass 'this' value directly from the argument to return value, to avoid 1711 // reg unit interference 1712 if (i == 0 && isThisReturn) { 1713 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1714 "unexpected return calling convention register assignment"); 1715 InVals.push_back(ThisVal); 1716 continue; 1717 } 1718 1719 SDValue Val; 1720 if (VA.needsCustom()) { 1721 // Handle f64 or half of a v2f64. 1722 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1723 InFlag); 1724 Chain = Lo.getValue(1); 1725 InFlag = Lo.getValue(2); 1726 VA = RVLocs[++i]; // skip ahead to next loc 1727 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1728 InFlag); 1729 Chain = Hi.getValue(1); 1730 InFlag = Hi.getValue(2); 1731 if (!Subtarget->isLittle()) 1732 std::swap (Lo, Hi); 1733 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1734 1735 if (VA.getLocVT() == MVT::v2f64) { 1736 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1737 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1738 DAG.getConstant(0, dl, MVT::i32)); 1739 1740 VA = RVLocs[++i]; // skip ahead to next loc 1741 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1742 Chain = Lo.getValue(1); 1743 InFlag = Lo.getValue(2); 1744 VA = RVLocs[++i]; // skip ahead to next loc 1745 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1746 Chain = Hi.getValue(1); 1747 InFlag = Hi.getValue(2); 1748 if (!Subtarget->isLittle()) 1749 std::swap (Lo, Hi); 1750 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1751 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1752 DAG.getConstant(1, dl, MVT::i32)); 1753 } 1754 } else { 1755 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1756 InFlag); 1757 Chain = Val.getValue(1); 1758 InFlag = Val.getValue(2); 1759 } 1760 1761 switch (VA.getLocInfo()) { 1762 default: llvm_unreachable("Unknown loc info!"); 1763 case CCValAssign::Full: break; 1764 case CCValAssign::BCvt: 1765 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1766 break; 1767 } 1768 1769 InVals.push_back(Val); 1770 } 1771 1772 return Chain; 1773 } 1774 1775 /// LowerMemOpCallTo - Store the argument to the stack. 1776 SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, 1777 SDValue Arg, const SDLoc &dl, 1778 SelectionDAG &DAG, 1779 const CCValAssign &VA, 1780 ISD::ArgFlagsTy Flags) const { 1781 unsigned LocMemOffset = VA.getLocMemOffset(); 1782 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1783 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), 1784 StackPtr, PtrOff); 1785 return DAG.getStore( 1786 Chain, dl, Arg, PtrOff, 1787 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset)); 1788 } 1789 1790 void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG, 1791 SDValue Chain, SDValue &Arg, 1792 RegsToPassVector &RegsToPass, 1793 CCValAssign &VA, CCValAssign &NextVA, 1794 SDValue &StackPtr, 1795 SmallVectorImpl<SDValue> &MemOpChains, 1796 ISD::ArgFlagsTy Flags) const { 1797 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1798 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1799 unsigned id = Subtarget->isLittle() ? 0 : 1; 1800 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id))); 1801 1802 if (NextVA.isRegLoc()) 1803 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id))); 1804 else { 1805 assert(NextVA.isMemLoc()); 1806 if (!StackPtr.getNode()) 1807 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, 1808 getPointerTy(DAG.getDataLayout())); 1809 1810 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id), 1811 dl, DAG, NextVA, 1812 Flags)); 1813 } 1814 } 1815 1816 /// LowerCall - Lowering a call into a callseq_start <- 1817 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1818 /// nodes. 1819 SDValue 1820 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1821 SmallVectorImpl<SDValue> &InVals) const { 1822 SelectionDAG &DAG = CLI.DAG; 1823 SDLoc &dl = CLI.DL; 1824 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1825 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1826 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1827 SDValue Chain = CLI.Chain; 1828 SDValue Callee = CLI.Callee; 1829 bool &isTailCall = CLI.IsTailCall; 1830 CallingConv::ID CallConv = CLI.CallConv; 1831 bool doesNotRet = CLI.DoesNotReturn; 1832 bool isVarArg = CLI.IsVarArg; 1833 1834 MachineFunction &MF = DAG.getMachineFunction(); 1835 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1836 bool isThisReturn = false; 1837 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls"); 1838 bool PreferIndirect = false; 1839 1840 // Disable tail calls if they're not supported. 1841 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true") 1842 isTailCall = false; 1843 1844 if (isa<GlobalAddressSDNode>(Callee)) { 1845 // If we're optimizing for minimum size and the function is called three or 1846 // more times in this block, we can improve codesize by calling indirectly 1847 // as BLXr has a 16-bit encoding. 1848 auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal(); 1849 auto *BB = CLI.CS.getParent(); 1850 PreferIndirect = 1851 Subtarget->isThumb() && Subtarget->hasMinSize() && 1852 count_if(GV->users(), [&BB](const User *U) { 1853 return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB; 1854 }) > 2; 1855 } 1856 if (isTailCall) { 1857 // Check if it's really possible to do a tail call. 1858 isTailCall = IsEligibleForTailCallOptimization( 1859 Callee, CallConv, isVarArg, isStructRet, 1860 MF.getFunction().hasStructRetAttr(), Outs, OutVals, Ins, DAG, 1861 PreferIndirect); 1862 if (!isTailCall && CLI.CS && CLI.CS.isMustTailCall()) 1863 report_fatal_error("failed to perform tail call elimination on a call " 1864 "site marked musttail"); 1865 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1866 // detected sibcalls. 1867 if (isTailCall) 1868 ++NumTailCalls; 1869 } 1870 1871 // Analyze operands of the call, assigning locations to each operand. 1872 SmallVector<CCValAssign, 16> ArgLocs; 1873 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1874 *DAG.getContext()); 1875 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CallConv, isVarArg)); 1876 1877 // Get a count of how many bytes are to be pushed on the stack. 1878 unsigned NumBytes = CCInfo.getNextStackOffset(); 1879 1880 if (isTailCall) { 1881 // For tail calls, memory operands are available in our caller's stack. 1882 NumBytes = 0; 1883 } else { 1884 // Adjust the stack pointer for the new arguments... 1885 // These operations are automatically eliminated by the prolog/epilog pass 1886 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 1887 } 1888 1889 SDValue StackPtr = 1890 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout())); 1891 1892 RegsToPassVector RegsToPass; 1893 SmallVector<SDValue, 8> MemOpChains; 1894 1895 // Walk the register/memloc assignments, inserting copies/loads. In the case 1896 // of tail call optimization, arguments are handled later. 1897 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1898 i != e; 1899 ++i, ++realArgIdx) { 1900 CCValAssign &VA = ArgLocs[i]; 1901 SDValue Arg = OutVals[realArgIdx]; 1902 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1903 bool isByVal = Flags.isByVal(); 1904 1905 // Promote the value if needed. 1906 switch (VA.getLocInfo()) { 1907 default: llvm_unreachable("Unknown loc info!"); 1908 case CCValAssign::Full: break; 1909 case CCValAssign::SExt: 1910 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1911 break; 1912 case CCValAssign::ZExt: 1913 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1914 break; 1915 case CCValAssign::AExt: 1916 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1917 break; 1918 case CCValAssign::BCvt: 1919 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1920 break; 1921 } 1922 1923 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1924 if (VA.needsCustom()) { 1925 if (VA.getLocVT() == MVT::v2f64) { 1926 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1927 DAG.getConstant(0, dl, MVT::i32)); 1928 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1929 DAG.getConstant(1, dl, MVT::i32)); 1930 1931 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1932 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1933 1934 VA = ArgLocs[++i]; // skip ahead to next loc 1935 if (VA.isRegLoc()) { 1936 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1937 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1938 } else { 1939 assert(VA.isMemLoc()); 1940 1941 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1942 dl, DAG, VA, Flags)); 1943 } 1944 } else { 1945 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1946 StackPtr, MemOpChains, Flags); 1947 } 1948 } else if (VA.isRegLoc()) { 1949 if (realArgIdx == 0 && Flags.isReturned() && !Flags.isSwiftSelf() && 1950 Outs[0].VT == MVT::i32) { 1951 assert(VA.getLocVT() == MVT::i32 && 1952 "unexpected calling convention register assignment"); 1953 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 1954 "unexpected use of 'returned'"); 1955 isThisReturn = true; 1956 } 1957 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1958 } else if (isByVal) { 1959 assert(VA.isMemLoc()); 1960 unsigned offset = 0; 1961 1962 // True if this byval aggregate will be split between registers 1963 // and memory. 1964 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 1965 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed(); 1966 1967 if (CurByValIdx < ByValArgsCount) { 1968 1969 unsigned RegBegin, RegEnd; 1970 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 1971 1972 EVT PtrVT = 1973 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 1974 unsigned int i, j; 1975 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 1976 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32); 1977 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1978 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1979 MachinePointerInfo(), 1980 DAG.InferPtrAlignment(AddArg)); 1981 MemOpChains.push_back(Load.getValue(1)); 1982 RegsToPass.push_back(std::make_pair(j, Load)); 1983 } 1984 1985 // If parameter size outsides register area, "offset" value 1986 // helps us to calculate stack slot for remained part properly. 1987 offset = RegEnd - RegBegin; 1988 1989 CCInfo.nextInRegsParam(); 1990 } 1991 1992 if (Flags.getByValSize() > 4*offset) { 1993 auto PtrVT = getPointerTy(DAG.getDataLayout()); 1994 unsigned LocMemOffset = VA.getLocMemOffset(); 1995 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1996 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff); 1997 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl); 1998 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset); 1999 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl, 2000 MVT::i32); 2001 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl, 2002 MVT::i32); 2003 2004 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 2005 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 2006 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 2007 Ops)); 2008 } 2009 } else if (!isTailCall) { 2010 assert(VA.isMemLoc()); 2011 2012 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 2013 dl, DAG, VA, Flags)); 2014 } 2015 } 2016 2017 if (!MemOpChains.empty()) 2018 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 2019 2020 // Build a sequence of copy-to-reg nodes chained together with token chain 2021 // and flag operands which copy the outgoing args into the appropriate regs. 2022 SDValue InFlag; 2023 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 2024 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 2025 RegsToPass[i].second, InFlag); 2026 InFlag = Chain.getValue(1); 2027 } 2028 2029 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 2030 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 2031 // node so that legalize doesn't hack it. 2032 bool isDirect = false; 2033 2034 const TargetMachine &TM = getTargetMachine(); 2035 const Module *Mod = MF.getFunction().getParent(); 2036 const GlobalValue *GV = nullptr; 2037 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 2038 GV = G->getGlobal(); 2039 bool isStub = 2040 !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO(); 2041 2042 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 2043 bool isLocalARMFunc = false; 2044 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2045 auto PtrVt = getPointerTy(DAG.getDataLayout()); 2046 2047 if (Subtarget->genLongCalls()) { 2048 assert((!isPositionIndependent() || Subtarget->isTargetWindows()) && 2049 "long-calls codegen is not position independent!"); 2050 // Handle a global address or an external symbol. If it's not one of 2051 // those, the target's already in a register, so we don't need to do 2052 // anything extra. 2053 if (isa<GlobalAddressSDNode>(Callee)) { 2054 // Create a constant pool entry for the callee address 2055 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2056 ARMConstantPoolValue *CPV = 2057 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 2058 2059 // Get the address of the callee into a register 2060 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2061 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2062 Callee = DAG.getLoad( 2063 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2064 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2065 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 2066 const char *Sym = S->getSymbol(); 2067 2068 // Create a constant pool entry for the callee address 2069 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2070 ARMConstantPoolValue *CPV = 2071 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 2072 ARMPCLabelIndex, 0); 2073 // Get the address of the callee into a register 2074 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2075 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2076 Callee = DAG.getLoad( 2077 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2078 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2079 } 2080 } else if (isa<GlobalAddressSDNode>(Callee)) { 2081 if (!PreferIndirect) { 2082 isDirect = true; 2083 bool isDef = GV->isStrongDefinitionForLinker(); 2084 2085 // ARM call to a local ARM function is predicable. 2086 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking); 2087 // tBX takes a register source operand. 2088 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 2089 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?"); 2090 Callee = DAG.getNode( 2091 ARMISD::WrapperPIC, dl, PtrVt, 2092 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY)); 2093 Callee = DAG.getLoad( 2094 PtrVt, dl, DAG.getEntryNode(), Callee, 2095 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2096 /* Alignment = */ 0, MachineMemOperand::MODereferenceable | 2097 MachineMemOperand::MOInvariant); 2098 } else if (Subtarget->isTargetCOFF()) { 2099 assert(Subtarget->isTargetWindows() && 2100 "Windows is the only supported COFF target"); 2101 unsigned TargetFlags = GV->hasDLLImportStorageClass() 2102 ? ARMII::MO_DLLIMPORT 2103 : ARMII::MO_NO_FLAG; 2104 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, 2105 TargetFlags); 2106 if (GV->hasDLLImportStorageClass()) 2107 Callee = 2108 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), 2109 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee), 2110 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 2111 } else { 2112 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0); 2113 } 2114 } 2115 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 2116 isDirect = true; 2117 // tBX takes a register source operand. 2118 const char *Sym = S->getSymbol(); 2119 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 2120 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2121 ARMConstantPoolValue *CPV = 2122 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 2123 ARMPCLabelIndex, 4); 2124 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2125 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2126 Callee = DAG.getLoad( 2127 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2128 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2129 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2130 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel); 2131 } else { 2132 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0); 2133 } 2134 } 2135 2136 // FIXME: handle tail calls differently. 2137 unsigned CallOpc; 2138 if (Subtarget->isThumb()) { 2139 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 2140 CallOpc = ARMISD::CALL_NOLINK; 2141 else 2142 CallOpc = ARMISD::CALL; 2143 } else { 2144 if (!isDirect && !Subtarget->hasV5TOps()) 2145 CallOpc = ARMISD::CALL_NOLINK; 2146 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() && 2147 // Emit regular call when code size is the priority 2148 !Subtarget->hasMinSize()) 2149 // "mov lr, pc; b _foo" to avoid confusing the RSP 2150 CallOpc = ARMISD::CALL_NOLINK; 2151 else 2152 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 2153 } 2154 2155 std::vector<SDValue> Ops; 2156 Ops.push_back(Chain); 2157 Ops.push_back(Callee); 2158 2159 // Add argument registers to the end of the list so that they are known live 2160 // into the call. 2161 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 2162 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 2163 RegsToPass[i].second.getValueType())); 2164 2165 // Add a register mask operand representing the call-preserved registers. 2166 if (!isTailCall) { 2167 const uint32_t *Mask; 2168 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo(); 2169 if (isThisReturn) { 2170 // For 'this' returns, use the R0-preserving mask if applicable 2171 Mask = ARI->getThisReturnPreservedMask(MF, CallConv); 2172 if (!Mask) { 2173 // Set isThisReturn to false if the calling convention is not one that 2174 // allows 'returned' to be modeled in this way, so LowerCallResult does 2175 // not try to pass 'this' straight through 2176 isThisReturn = false; 2177 Mask = ARI->getCallPreservedMask(MF, CallConv); 2178 } 2179 } else 2180 Mask = ARI->getCallPreservedMask(MF, CallConv); 2181 2182 assert(Mask && "Missing call preserved mask for calling convention"); 2183 Ops.push_back(DAG.getRegisterMask(Mask)); 2184 } 2185 2186 if (InFlag.getNode()) 2187 Ops.push_back(InFlag); 2188 2189 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2190 if (isTailCall) { 2191 MF.getFrameInfo().setHasTailCall(); 2192 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops); 2193 } 2194 2195 // Returns a chain and a flag for retval copy to use. 2196 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 2197 InFlag = Chain.getValue(1); 2198 2199 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 2200 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 2201 if (!Ins.empty()) 2202 InFlag = Chain.getValue(1); 2203 2204 // Handle result values, copying them out of physregs into vregs that we 2205 // return. 2206 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 2207 InVals, isThisReturn, 2208 isThisReturn ? OutVals[0] : SDValue()); 2209 } 2210 2211 /// HandleByVal - Every parameter *after* a byval parameter is passed 2212 /// on the stack. Remember the next parameter register to allocate, 2213 /// and then confiscate the rest of the parameter registers to insure 2214 /// this. 2215 void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size, 2216 unsigned Align) const { 2217 // Byval (as with any stack) slots are always at least 4 byte aligned. 2218 Align = std::max(Align, 4U); 2219 2220 unsigned Reg = State->AllocateReg(GPRArgRegs); 2221 if (!Reg) 2222 return; 2223 2224 unsigned AlignInRegs = Align / 4; 2225 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs; 2226 for (unsigned i = 0; i < Waste; ++i) 2227 Reg = State->AllocateReg(GPRArgRegs); 2228 2229 if (!Reg) 2230 return; 2231 2232 unsigned Excess = 4 * (ARM::R4 - Reg); 2233 2234 // Special case when NSAA != SP and parameter size greater than size of 2235 // all remained GPR regs. In that case we can't split parameter, we must 2236 // send it to stack. We also must set NCRN to R4, so waste all 2237 // remained registers. 2238 const unsigned NSAAOffset = State->getNextStackOffset(); 2239 if (NSAAOffset != 0 && Size > Excess) { 2240 while (State->AllocateReg(GPRArgRegs)) 2241 ; 2242 return; 2243 } 2244 2245 // First register for byval parameter is the first register that wasn't 2246 // allocated before this method call, so it would be "reg". 2247 // If parameter is small enough to be saved in range [reg, r4), then 2248 // the end (first after last) register would be reg + param-size-in-regs, 2249 // else parameter would be splitted between registers and stack, 2250 // end register would be r4 in this case. 2251 unsigned ByValRegBegin = Reg; 2252 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4); 2253 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 2254 // Note, first register is allocated in the beginning of function already, 2255 // allocate remained amount of registers we need. 2256 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i) 2257 State->AllocateReg(GPRArgRegs); 2258 // A byval parameter that is split between registers and memory needs its 2259 // size truncated here. 2260 // In the case where the entire structure fits in registers, we set the 2261 // size in memory to zero. 2262 Size = std::max<int>(Size - Excess, 0); 2263 } 2264 2265 /// MatchingStackOffset - Return true if the given stack call argument is 2266 /// already available in the same position (relatively) of the caller's 2267 /// incoming argument stack. 2268 static 2269 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 2270 MachineFrameInfo &MFI, const MachineRegisterInfo *MRI, 2271 const TargetInstrInfo *TII) { 2272 unsigned Bytes = Arg.getValueSizeInBits() / 8; 2273 int FI = std::numeric_limits<int>::max(); 2274 if (Arg.getOpcode() == ISD::CopyFromReg) { 2275 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 2276 if (!TargetRegisterInfo::isVirtualRegister(VR)) 2277 return false; 2278 MachineInstr *Def = MRI->getVRegDef(VR); 2279 if (!Def) 2280 return false; 2281 if (!Flags.isByVal()) { 2282 if (!TII->isLoadFromStackSlot(*Def, FI)) 2283 return false; 2284 } else { 2285 return false; 2286 } 2287 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 2288 if (Flags.isByVal()) 2289 // ByVal argument is passed in as a pointer but it's now being 2290 // dereferenced. e.g. 2291 // define @foo(%struct.X* %A) { 2292 // tail call @bar(%struct.X* byval %A) 2293 // } 2294 return false; 2295 SDValue Ptr = Ld->getBasePtr(); 2296 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 2297 if (!FINode) 2298 return false; 2299 FI = FINode->getIndex(); 2300 } else 2301 return false; 2302 2303 assert(FI != std::numeric_limits<int>::max()); 2304 if (!MFI.isFixedObjectIndex(FI)) 2305 return false; 2306 return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI); 2307 } 2308 2309 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 2310 /// for tail call optimization. Targets which want to do tail call 2311 /// optimization should implement this function. 2312 bool ARMTargetLowering::IsEligibleForTailCallOptimization( 2313 SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg, 2314 bool isCalleeStructRet, bool isCallerStructRet, 2315 const SmallVectorImpl<ISD::OutputArg> &Outs, 2316 const SmallVectorImpl<SDValue> &OutVals, 2317 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG, 2318 const bool isIndirect) const { 2319 MachineFunction &MF = DAG.getMachineFunction(); 2320 const Function &CallerF = MF.getFunction(); 2321 CallingConv::ID CallerCC = CallerF.getCallingConv(); 2322 2323 assert(Subtarget->supportsTailCall()); 2324 2325 // Indirect tail calls cannot be optimized for Thumb1 if the args 2326 // to the call take up r0-r3. The reason is that there are no legal registers 2327 // left to hold the pointer to the function to be called. 2328 if (Subtarget->isThumb1Only() && Outs.size() >= 4 && 2329 (!isa<GlobalAddressSDNode>(Callee.getNode()) || isIndirect)) 2330 return false; 2331 2332 // Look for obvious safe cases to perform tail call optimization that do not 2333 // require ABI changes. This is what gcc calls sibcall. 2334 2335 // Exception-handling functions need a special set of instructions to indicate 2336 // a return to the hardware. Tail-calling another function would probably 2337 // break this. 2338 if (CallerF.hasFnAttribute("interrupt")) 2339 return false; 2340 2341 // Also avoid sibcall optimization if either caller or callee uses struct 2342 // return semantics. 2343 if (isCalleeStructRet || isCallerStructRet) 2344 return false; 2345 2346 // Externally-defined functions with weak linkage should not be 2347 // tail-called on ARM when the OS does not support dynamic 2348 // pre-emption of symbols, as the AAELF spec requires normal calls 2349 // to undefined weak functions to be replaced with a NOP or jump to the 2350 // next instruction. The behaviour of branch instructions in this 2351 // situation (as used for tail calls) is implementation-defined, so we 2352 // cannot rely on the linker replacing the tail call with a return. 2353 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2354 const GlobalValue *GV = G->getGlobal(); 2355 const Triple &TT = getTargetMachine().getTargetTriple(); 2356 if (GV->hasExternalWeakLinkage() && 2357 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO())) 2358 return false; 2359 } 2360 2361 // Check that the call results are passed in the same way. 2362 LLVMContext &C = *DAG.getContext(); 2363 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins, 2364 CCAssignFnForReturn(CalleeCC, isVarArg), 2365 CCAssignFnForReturn(CallerCC, isVarArg))) 2366 return false; 2367 // The callee has to preserve all registers the caller needs to preserve. 2368 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2369 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 2370 if (CalleeCC != CallerCC) { 2371 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 2372 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 2373 return false; 2374 } 2375 2376 // If Caller's vararg or byval argument has been split between registers and 2377 // stack, do not perform tail call, since part of the argument is in caller's 2378 // local frame. 2379 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>(); 2380 if (AFI_Caller->getArgRegsSaveSize()) 2381 return false; 2382 2383 // If the callee takes no arguments then go on to check the results of the 2384 // call. 2385 if (!Outs.empty()) { 2386 // Check if stack adjustment is needed. For now, do not do this if any 2387 // argument is passed on the stack. 2388 SmallVector<CCValAssign, 16> ArgLocs; 2389 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C); 2390 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg)); 2391 if (CCInfo.getNextStackOffset()) { 2392 // Check if the arguments are already laid out in the right way as 2393 // the caller's fixed stack objects. 2394 MachineFrameInfo &MFI = MF.getFrameInfo(); 2395 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2396 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 2397 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2398 i != e; 2399 ++i, ++realArgIdx) { 2400 CCValAssign &VA = ArgLocs[i]; 2401 EVT RegVT = VA.getLocVT(); 2402 SDValue Arg = OutVals[realArgIdx]; 2403 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2404 if (VA.getLocInfo() == CCValAssign::Indirect) 2405 return false; 2406 if (VA.needsCustom()) { 2407 // f64 and vector types are split into multiple registers or 2408 // register/stack-slot combinations. The types will not match 2409 // the registers; give up on memory f64 refs until we figure 2410 // out what to do about this. 2411 if (!VA.isRegLoc()) 2412 return false; 2413 if (!ArgLocs[++i].isRegLoc()) 2414 return false; 2415 if (RegVT == MVT::v2f64) { 2416 if (!ArgLocs[++i].isRegLoc()) 2417 return false; 2418 if (!ArgLocs[++i].isRegLoc()) 2419 return false; 2420 } 2421 } else if (!VA.isRegLoc()) { 2422 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2423 MFI, MRI, TII)) 2424 return false; 2425 } 2426 } 2427 } 2428 2429 const MachineRegisterInfo &MRI = MF.getRegInfo(); 2430 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals)) 2431 return false; 2432 } 2433 2434 return true; 2435 } 2436 2437 bool 2438 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2439 MachineFunction &MF, bool isVarArg, 2440 const SmallVectorImpl<ISD::OutputArg> &Outs, 2441 LLVMContext &Context) const { 2442 SmallVector<CCValAssign, 16> RVLocs; 2443 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 2444 return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2445 } 2446 2447 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 2448 const SDLoc &DL, SelectionDAG &DAG) { 2449 const MachineFunction &MF = DAG.getMachineFunction(); 2450 const Function &F = MF.getFunction(); 2451 2452 StringRef IntKind = F.getFnAttribute("interrupt").getValueAsString(); 2453 2454 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset 2455 // version of the "preferred return address". These offsets affect the return 2456 // instruction if this is a return from PL1 without hypervisor extensions. 2457 // IRQ/FIQ: +4 "subs pc, lr, #4" 2458 // SWI: 0 "subs pc, lr, #0" 2459 // ABORT: +4 "subs pc, lr, #4" 2460 // UNDEF: +4/+2 "subs pc, lr, #0" 2461 // UNDEF varies depending on where the exception came from ARM or Thumb 2462 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0. 2463 2464 int64_t LROffset; 2465 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" || 2466 IntKind == "ABORT") 2467 LROffset = 4; 2468 else if (IntKind == "SWI" || IntKind == "UNDEF") 2469 LROffset = 0; 2470 else 2471 report_fatal_error("Unsupported interrupt attribute. If present, value " 2472 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF"); 2473 2474 RetOps.insert(RetOps.begin() + 1, 2475 DAG.getConstant(LROffset, DL, MVT::i32, false)); 2476 2477 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps); 2478 } 2479 2480 SDValue 2481 ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 2482 bool isVarArg, 2483 const SmallVectorImpl<ISD::OutputArg> &Outs, 2484 const SmallVectorImpl<SDValue> &OutVals, 2485 const SDLoc &dl, SelectionDAG &DAG) const { 2486 // CCValAssign - represent the assignment of the return value to a location. 2487 SmallVector<CCValAssign, 16> RVLocs; 2488 2489 // CCState - Info about the registers and stack slots. 2490 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2491 *DAG.getContext()); 2492 2493 // Analyze outgoing return values. 2494 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2495 2496 SDValue Flag; 2497 SmallVector<SDValue, 4> RetOps; 2498 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2499 bool isLittleEndian = Subtarget->isLittle(); 2500 2501 MachineFunction &MF = DAG.getMachineFunction(); 2502 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2503 AFI->setReturnRegsCount(RVLocs.size()); 2504 2505 // Copy the result values into the output registers. 2506 for (unsigned i = 0, realRVLocIdx = 0; 2507 i != RVLocs.size(); 2508 ++i, ++realRVLocIdx) { 2509 CCValAssign &VA = RVLocs[i]; 2510 assert(VA.isRegLoc() && "Can only return in registers!"); 2511 2512 SDValue Arg = OutVals[realRVLocIdx]; 2513 bool ReturnF16 = false; 2514 2515 if (Subtarget->hasFullFP16() && Subtarget->isTargetHardFloat()) { 2516 // Half-precision return values can be returned like this: 2517 // 2518 // t11 f16 = fadd ... 2519 // t12: i16 = bitcast t11 2520 // t13: i32 = zero_extend t12 2521 // t14: f32 = bitcast t13 <~~~~~~~ Arg 2522 // 2523 // to avoid code generation for bitcasts, we simply set Arg to the node 2524 // that produces the f16 value, t11 in this case. 2525 // 2526 if (Arg.getValueType() == MVT::f32 && Arg.getOpcode() == ISD::BITCAST) { 2527 SDValue ZE = Arg.getOperand(0); 2528 if (ZE.getOpcode() == ISD::ZERO_EXTEND && ZE.getValueType() == MVT::i32) { 2529 SDValue BC = ZE.getOperand(0); 2530 if (BC.getOpcode() == ISD::BITCAST && BC.getValueType() == MVT::i16) { 2531 Arg = BC.getOperand(0); 2532 ReturnF16 = true; 2533 } 2534 } 2535 } 2536 } 2537 2538 switch (VA.getLocInfo()) { 2539 default: llvm_unreachable("Unknown loc info!"); 2540 case CCValAssign::Full: break; 2541 case CCValAssign::BCvt: 2542 if (!ReturnF16) 2543 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2544 break; 2545 } 2546 2547 if (VA.needsCustom()) { 2548 if (VA.getLocVT() == MVT::v2f64) { 2549 // Extract the first half and return it in two registers. 2550 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2551 DAG.getConstant(0, dl, MVT::i32)); 2552 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2553 DAG.getVTList(MVT::i32, MVT::i32), Half); 2554 2555 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2556 HalfGPRs.getValue(isLittleEndian ? 0 : 1), 2557 Flag); 2558 Flag = Chain.getValue(1); 2559 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2560 VA = RVLocs[++i]; // skip ahead to next loc 2561 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2562 HalfGPRs.getValue(isLittleEndian ? 1 : 0), 2563 Flag); 2564 Flag = Chain.getValue(1); 2565 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2566 VA = RVLocs[++i]; // skip ahead to next loc 2567 2568 // Extract the 2nd half and fall through to handle it as an f64 value. 2569 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2570 DAG.getConstant(1, dl, MVT::i32)); 2571 } 2572 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2573 // available. 2574 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2575 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2576 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2577 fmrrd.getValue(isLittleEndian ? 0 : 1), 2578 Flag); 2579 Flag = Chain.getValue(1); 2580 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2581 VA = RVLocs[++i]; // skip ahead to next loc 2582 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2583 fmrrd.getValue(isLittleEndian ? 1 : 0), 2584 Flag); 2585 } else 2586 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2587 2588 // Guarantee that all emitted copies are 2589 // stuck together, avoiding something bad. 2590 Flag = Chain.getValue(1); 2591 RetOps.push_back(DAG.getRegister(VA.getLocReg(), 2592 ReturnF16 ? MVT::f16 : VA.getLocVT())); 2593 } 2594 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2595 const MCPhysReg *I = 2596 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 2597 if (I) { 2598 for (; *I; ++I) { 2599 if (ARM::GPRRegClass.contains(*I)) 2600 RetOps.push_back(DAG.getRegister(*I, MVT::i32)); 2601 else if (ARM::DPRRegClass.contains(*I)) 2602 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64))); 2603 else 2604 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 2605 } 2606 } 2607 2608 // Update chain and glue. 2609 RetOps[0] = Chain; 2610 if (Flag.getNode()) 2611 RetOps.push_back(Flag); 2612 2613 // CPUs which aren't M-class use a special sequence to return from 2614 // exceptions (roughly, any instruction setting pc and cpsr simultaneously, 2615 // though we use "subs pc, lr, #N"). 2616 // 2617 // M-class CPUs actually use a normal return sequence with a special 2618 // (hardware-provided) value in LR, so the normal code path works. 2619 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt") && 2620 !Subtarget->isMClass()) { 2621 if (Subtarget->isThumb1Only()) 2622 report_fatal_error("interrupt attribute is not supported in Thumb1"); 2623 return LowerInterruptReturn(RetOps, dl, DAG); 2624 } 2625 2626 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps); 2627 } 2628 2629 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2630 if (N->getNumValues() != 1) 2631 return false; 2632 if (!N->hasNUsesOfValue(1, 0)) 2633 return false; 2634 2635 SDValue TCChain = Chain; 2636 SDNode *Copy = *N->use_begin(); 2637 if (Copy->getOpcode() == ISD::CopyToReg) { 2638 // If the copy has a glue operand, we conservatively assume it isn't safe to 2639 // perform a tail call. 2640 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2641 return false; 2642 TCChain = Copy->getOperand(0); 2643 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2644 SDNode *VMov = Copy; 2645 // f64 returned in a pair of GPRs. 2646 SmallPtrSet<SDNode*, 2> Copies; 2647 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2648 UI != UE; ++UI) { 2649 if (UI->getOpcode() != ISD::CopyToReg) 2650 return false; 2651 Copies.insert(*UI); 2652 } 2653 if (Copies.size() > 2) 2654 return false; 2655 2656 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2657 UI != UE; ++UI) { 2658 SDValue UseChain = UI->getOperand(0); 2659 if (Copies.count(UseChain.getNode())) 2660 // Second CopyToReg 2661 Copy = *UI; 2662 else { 2663 // We are at the top of this chain. 2664 // If the copy has a glue operand, we conservatively assume it 2665 // isn't safe to perform a tail call. 2666 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue) 2667 return false; 2668 // First CopyToReg 2669 TCChain = UseChain; 2670 } 2671 } 2672 } else if (Copy->getOpcode() == ISD::BITCAST) { 2673 // f32 returned in a single GPR. 2674 if (!Copy->hasOneUse()) 2675 return false; 2676 Copy = *Copy->use_begin(); 2677 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2678 return false; 2679 // If the copy has a glue operand, we conservatively assume it isn't safe to 2680 // perform a tail call. 2681 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2682 return false; 2683 TCChain = Copy->getOperand(0); 2684 } else { 2685 return false; 2686 } 2687 2688 bool HasRet = false; 2689 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2690 UI != UE; ++UI) { 2691 if (UI->getOpcode() != ARMISD::RET_FLAG && 2692 UI->getOpcode() != ARMISD::INTRET_FLAG) 2693 return false; 2694 HasRet = true; 2695 } 2696 2697 if (!HasRet) 2698 return false; 2699 2700 Chain = TCChain; 2701 return true; 2702 } 2703 2704 bool ARMTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { 2705 if (!Subtarget->supportsTailCall()) 2706 return false; 2707 2708 auto Attr = 2709 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls"); 2710 if (!CI->isTailCall() || Attr.getValueAsString() == "true") 2711 return false; 2712 2713 return true; 2714 } 2715 2716 // Trying to write a 64 bit value so need to split into two 32 bit values first, 2717 // and pass the lower and high parts through. 2718 static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) { 2719 SDLoc DL(Op); 2720 SDValue WriteValue = Op->getOperand(2); 2721 2722 // This function is only supposed to be called for i64 type argument. 2723 assert(WriteValue.getValueType() == MVT::i64 2724 && "LowerWRITE_REGISTER called for non-i64 type argument."); 2725 2726 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2727 DAG.getConstant(0, DL, MVT::i32)); 2728 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2729 DAG.getConstant(1, DL, MVT::i32)); 2730 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi }; 2731 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops); 2732 } 2733 2734 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2735 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2736 // one of the above mentioned nodes. It has to be wrapped because otherwise 2737 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2738 // be used to form addressing mode. These wrapped nodes will be selected 2739 // into MOVi. 2740 SDValue ARMTargetLowering::LowerConstantPool(SDValue Op, 2741 SelectionDAG &DAG) const { 2742 EVT PtrVT = Op.getValueType(); 2743 // FIXME there is no actual debug info here 2744 SDLoc dl(Op); 2745 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2746 SDValue Res; 2747 2748 // When generating execute-only code Constant Pools must be promoted to the 2749 // global data section. It's a bit ugly that we can't share them across basic 2750 // blocks, but this way we guarantee that execute-only behaves correct with 2751 // position-independent addressing modes. 2752 if (Subtarget->genExecuteOnly()) { 2753 auto AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 2754 auto T = const_cast<Type*>(CP->getType()); 2755 auto C = const_cast<Constant*>(CP->getConstVal()); 2756 auto M = const_cast<Module*>(DAG.getMachineFunction(). 2757 getFunction().getParent()); 2758 auto GV = new GlobalVariable( 2759 *M, T, /*isConst=*/true, GlobalVariable::InternalLinkage, C, 2760 Twine(DAG.getDataLayout().getPrivateGlobalPrefix()) + "CP" + 2761 Twine(DAG.getMachineFunction().getFunctionNumber()) + "_" + 2762 Twine(AFI->createPICLabelUId()) 2763 ); 2764 SDValue GA = DAG.getTargetGlobalAddress(dyn_cast<GlobalValue>(GV), 2765 dl, PtrVT); 2766 return LowerGlobalAddress(GA, DAG); 2767 } 2768 2769 if (CP->isMachineConstantPoolEntry()) 2770 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2771 CP->getAlignment()); 2772 else 2773 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2774 CP->getAlignment()); 2775 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2776 } 2777 2778 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2779 return MachineJumpTableInfo::EK_Inline; 2780 } 2781 2782 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2783 SelectionDAG &DAG) const { 2784 MachineFunction &MF = DAG.getMachineFunction(); 2785 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2786 unsigned ARMPCLabelIndex = 0; 2787 SDLoc DL(Op); 2788 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2789 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2790 SDValue CPAddr; 2791 bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI(); 2792 if (!IsPositionIndependent) { 2793 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2794 } else { 2795 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2796 ARMPCLabelIndex = AFI->createPICLabelUId(); 2797 ARMConstantPoolValue *CPV = 2798 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2799 ARMCP::CPBlockAddress, PCAdj); 2800 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2801 } 2802 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2803 SDValue Result = DAG.getLoad( 2804 PtrVT, DL, DAG.getEntryNode(), CPAddr, 2805 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2806 if (!IsPositionIndependent) 2807 return Result; 2808 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32); 2809 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2810 } 2811 2812 /// Convert a TLS address reference into the correct sequence of loads 2813 /// and calls to compute the variable's address for Darwin, and return an 2814 /// SDValue containing the final node. 2815 2816 /// Darwin only has one TLS scheme which must be capable of dealing with the 2817 /// fully general situation, in the worst case. This means: 2818 /// + "extern __thread" declaration. 2819 /// + Defined in a possibly unknown dynamic library. 2820 /// 2821 /// The general system is that each __thread variable has a [3 x i32] descriptor 2822 /// which contains information used by the runtime to calculate the address. The 2823 /// only part of this the compiler needs to know about is the first word, which 2824 /// contains a function pointer that must be called with the address of the 2825 /// entire descriptor in "r0". 2826 /// 2827 /// Since this descriptor may be in a different unit, in general access must 2828 /// proceed along the usual ARM rules. A common sequence to produce is: 2829 /// 2830 /// movw rT1, :lower16:_var$non_lazy_ptr 2831 /// movt rT1, :upper16:_var$non_lazy_ptr 2832 /// ldr r0, [rT1] 2833 /// ldr rT2, [r0] 2834 /// blx rT2 2835 /// [...address now in r0...] 2836 SDValue 2837 ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op, 2838 SelectionDAG &DAG) const { 2839 assert(Subtarget->isTargetDarwin() && 2840 "This function expects a Darwin target"); 2841 SDLoc DL(Op); 2842 2843 // First step is to get the address of the actua global symbol. This is where 2844 // the TLS descriptor lives. 2845 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG); 2846 2847 // The first entry in the descriptor is a function pointer that we must call 2848 // to obtain the address of the variable. 2849 SDValue Chain = DAG.getEntryNode(); 2850 SDValue FuncTLVGet = DAG.getLoad( 2851 MVT::i32, DL, Chain, DescAddr, 2852 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2853 /* Alignment = */ 4, 2854 MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable | 2855 MachineMemOperand::MOInvariant); 2856 Chain = FuncTLVGet.getValue(1); 2857 2858 MachineFunction &F = DAG.getMachineFunction(); 2859 MachineFrameInfo &MFI = F.getFrameInfo(); 2860 MFI.setAdjustsStack(true); 2861 2862 // TLS calls preserve all registers except those that absolutely must be 2863 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be 2864 // silly). 2865 auto TRI = 2866 getTargetMachine().getSubtargetImpl(F.getFunction())->getRegisterInfo(); 2867 auto ARI = static_cast<const ARMRegisterInfo *>(TRI); 2868 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction()); 2869 2870 // Finally, we can make the call. This is just a degenerate version of a 2871 // normal AArch64 call node: r0 takes the address of the descriptor, and 2872 // returns the address of the variable in this thread. 2873 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue()); 2874 Chain = 2875 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue), 2876 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32), 2877 DAG.getRegisterMask(Mask), Chain.getValue(1)); 2878 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1)); 2879 } 2880 2881 SDValue 2882 ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op, 2883 SelectionDAG &DAG) const { 2884 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering"); 2885 2886 SDValue Chain = DAG.getEntryNode(); 2887 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2888 SDLoc DL(Op); 2889 2890 // Load the current TEB (thread environment block) 2891 SDValue Ops[] = {Chain, 2892 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 2893 DAG.getConstant(15, DL, MVT::i32), 2894 DAG.getConstant(0, DL, MVT::i32), 2895 DAG.getConstant(13, DL, MVT::i32), 2896 DAG.getConstant(0, DL, MVT::i32), 2897 DAG.getConstant(2, DL, MVT::i32)}; 2898 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 2899 DAG.getVTList(MVT::i32, MVT::Other), Ops); 2900 2901 SDValue TEB = CurrentTEB.getValue(0); 2902 Chain = CurrentTEB.getValue(1); 2903 2904 // Load the ThreadLocalStoragePointer from the TEB 2905 // A pointer to the TLS array is located at offset 0x2c from the TEB. 2906 SDValue TLSArray = 2907 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL)); 2908 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo()); 2909 2910 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4 2911 // offset into the TLSArray. 2912 2913 // Load the TLS index from the C runtime 2914 SDValue TLSIndex = 2915 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG); 2916 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex); 2917 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo()); 2918 2919 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex, 2920 DAG.getConstant(2, DL, MVT::i32)); 2921 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain, 2922 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot), 2923 MachinePointerInfo()); 2924 2925 // Get the offset of the start of the .tls section (section base) 2926 const auto *GA = cast<GlobalAddressSDNode>(Op); 2927 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL); 2928 SDValue Offset = DAG.getLoad( 2929 PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32, 2930 DAG.getTargetConstantPool(CPV, PtrVT, 4)), 2931 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2932 2933 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset); 2934 } 2935 2936 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2937 SDValue 2938 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2939 SelectionDAG &DAG) const { 2940 SDLoc dl(GA); 2941 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2942 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2943 MachineFunction &MF = DAG.getMachineFunction(); 2944 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2945 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2946 ARMConstantPoolValue *CPV = 2947 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2948 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2949 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2950 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2951 Argument = DAG.getLoad( 2952 PtrVT, dl, DAG.getEntryNode(), Argument, 2953 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2954 SDValue Chain = Argument.getValue(1); 2955 2956 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2957 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2958 2959 // call __tls_get_addr. 2960 ArgListTy Args; 2961 ArgListEntry Entry; 2962 Entry.Node = Argument; 2963 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2964 Args.push_back(Entry); 2965 2966 // FIXME: is there useful debug info available here? 2967 TargetLowering::CallLoweringInfo CLI(DAG); 2968 CLI.setDebugLoc(dl).setChain(Chain).setLibCallee( 2969 CallingConv::C, Type::getInt32Ty(*DAG.getContext()), 2970 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args)); 2971 2972 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2973 return CallResult.first; 2974 } 2975 2976 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2977 // "local exec" model. 2978 SDValue 2979 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2980 SelectionDAG &DAG, 2981 TLSModel::Model model) const { 2982 const GlobalValue *GV = GA->getGlobal(); 2983 SDLoc dl(GA); 2984 SDValue Offset; 2985 SDValue Chain = DAG.getEntryNode(); 2986 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2987 // Get the Thread Pointer 2988 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2989 2990 if (model == TLSModel::InitialExec) { 2991 MachineFunction &MF = DAG.getMachineFunction(); 2992 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2993 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2994 // Initial exec model. 2995 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2996 ARMConstantPoolValue *CPV = 2997 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2998 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2999 true); 3000 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3001 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 3002 Offset = DAG.getLoad( 3003 PtrVT, dl, Chain, Offset, 3004 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3005 Chain = Offset.getValue(1); 3006 3007 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 3008 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 3009 3010 Offset = DAG.getLoad( 3011 PtrVT, dl, Chain, Offset, 3012 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3013 } else { 3014 // local exec model 3015 assert(model == TLSModel::LocalExec); 3016 ARMConstantPoolValue *CPV = 3017 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 3018 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3019 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 3020 Offset = DAG.getLoad( 3021 PtrVT, dl, Chain, Offset, 3022 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3023 } 3024 3025 // The address of the thread local variable is the add of the thread 3026 // pointer with the offset of the variable. 3027 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 3028 } 3029 3030 SDValue 3031 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 3032 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 3033 if (DAG.getTarget().useEmulatedTLS()) 3034 return LowerToTLSEmulatedModel(GA, DAG); 3035 3036 if (Subtarget->isTargetDarwin()) 3037 return LowerGlobalTLSAddressDarwin(Op, DAG); 3038 3039 if (Subtarget->isTargetWindows()) 3040 return LowerGlobalTLSAddressWindows(Op, DAG); 3041 3042 // TODO: implement the "local dynamic" model 3043 assert(Subtarget->isTargetELF() && "Only ELF implemented here"); 3044 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 3045 3046 switch (model) { 3047 case TLSModel::GeneralDynamic: 3048 case TLSModel::LocalDynamic: 3049 return LowerToTLSGeneralDynamicModel(GA, DAG); 3050 case TLSModel::InitialExec: 3051 case TLSModel::LocalExec: 3052 return LowerToTLSExecModels(GA, DAG, model); 3053 } 3054 llvm_unreachable("bogus TLS model"); 3055 } 3056 3057 /// Return true if all users of V are within function F, looking through 3058 /// ConstantExprs. 3059 static bool allUsersAreInFunction(const Value *V, const Function *F) { 3060 SmallVector<const User*,4> Worklist; 3061 for (auto *U : V->users()) 3062 Worklist.push_back(U); 3063 while (!Worklist.empty()) { 3064 auto *U = Worklist.pop_back_val(); 3065 if (isa<ConstantExpr>(U)) { 3066 for (auto *UU : U->users()) 3067 Worklist.push_back(UU); 3068 continue; 3069 } 3070 3071 auto *I = dyn_cast<Instruction>(U); 3072 if (!I || I->getParent()->getParent() != F) 3073 return false; 3074 } 3075 return true; 3076 } 3077 3078 static SDValue promoteToConstantPool(const ARMTargetLowering *TLI, 3079 const GlobalValue *GV, SelectionDAG &DAG, 3080 EVT PtrVT, const SDLoc &dl) { 3081 // If we're creating a pool entry for a constant global with unnamed address, 3082 // and the global is small enough, we can emit it inline into the constant pool 3083 // to save ourselves an indirection. 3084 // 3085 // This is a win if the constant is only used in one function (so it doesn't 3086 // need to be duplicated) or duplicating the constant wouldn't increase code 3087 // size (implying the constant is no larger than 4 bytes). 3088 const Function &F = DAG.getMachineFunction().getFunction(); 3089 3090 // We rely on this decision to inline being idemopotent and unrelated to the 3091 // use-site. We know that if we inline a variable at one use site, we'll 3092 // inline it elsewhere too (and reuse the constant pool entry). Fast-isel 3093 // doesn't know about this optimization, so bail out if it's enabled else 3094 // we could decide to inline here (and thus never emit the GV) but require 3095 // the GV from fast-isel generated code. 3096 if (!EnableConstpoolPromotion || 3097 DAG.getMachineFunction().getTarget().Options.EnableFastISel) 3098 return SDValue(); 3099 3100 auto *GVar = dyn_cast<GlobalVariable>(GV); 3101 if (!GVar || !GVar->hasInitializer() || 3102 !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() || 3103 !GVar->hasLocalLinkage()) 3104 return SDValue(); 3105 3106 // If we inline a value that contains relocations, we move the relocations 3107 // from .data to .text. This is not allowed in position-independent code. 3108 auto *Init = GVar->getInitializer(); 3109 if ((TLI->isPositionIndependent() || TLI->getSubtarget()->isROPI()) && 3110 Init->needsRelocation()) 3111 return SDValue(); 3112 3113 // The constant islands pass can only really deal with alignment requests 3114 // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote 3115 // any type wanting greater alignment requirements than 4 bytes. We also 3116 // can only promote constants that are multiples of 4 bytes in size or 3117 // are paddable to a multiple of 4. Currently we only try and pad constants 3118 // that are strings for simplicity. 3119 auto *CDAInit = dyn_cast<ConstantDataArray>(Init); 3120 unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType()); 3121 unsigned Align = DAG.getDataLayout().getPreferredAlignment(GVar); 3122 unsigned RequiredPadding = 4 - (Size % 4); 3123 bool PaddingPossible = 3124 RequiredPadding == 4 || (CDAInit && CDAInit->isString()); 3125 if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize || 3126 Size == 0) 3127 return SDValue(); 3128 3129 unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding); 3130 MachineFunction &MF = DAG.getMachineFunction(); 3131 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3132 3133 // We can't bloat the constant pool too much, else the ConstantIslands pass 3134 // may fail to converge. If we haven't promoted this global yet (it may have 3135 // multiple uses), and promoting it would increase the constant pool size (Sz 3136 // > 4), ensure we have space to do so up to MaxTotal. 3137 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4) 3138 if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >= 3139 ConstpoolPromotionMaxTotal) 3140 return SDValue(); 3141 3142 // This is only valid if all users are in a single function; we can't clone 3143 // the constant in general. The LLVM IR unnamed_addr allows merging 3144 // constants, but not cloning them. 3145 // 3146 // We could potentially allow cloning if we could prove all uses of the 3147 // constant in the current function don't care about the address, like 3148 // printf format strings. But that isn't implemented for now. 3149 if (!allUsersAreInFunction(GVar, &F)) 3150 return SDValue(); 3151 3152 // We're going to inline this global. Pad it out if needed. 3153 if (RequiredPadding != 4) { 3154 StringRef S = CDAInit->getAsString(); 3155 3156 SmallVector<uint8_t,16> V(S.size()); 3157 std::copy(S.bytes_begin(), S.bytes_end(), V.begin()); 3158 while (RequiredPadding--) 3159 V.push_back(0); 3160 Init = ConstantDataArray::get(*DAG.getContext(), V); 3161 } 3162 3163 auto CPVal = ARMConstantPoolConstant::Create(GVar, Init); 3164 SDValue CPAddr = 3165 DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4); 3166 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) { 3167 AFI->markGlobalAsPromotedToConstantPool(GVar); 3168 AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() + 3169 PaddedSize - 4); 3170 } 3171 ++NumConstpoolPromoted; 3172 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3173 } 3174 3175 bool ARMTargetLowering::isReadOnly(const GlobalValue *GV) const { 3176 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 3177 if (!(GV = GA->getBaseObject())) 3178 return false; 3179 if (const auto *V = dyn_cast<GlobalVariable>(GV)) 3180 return V->isConstant(); 3181 return isa<Function>(GV); 3182 } 3183 3184 SDValue ARMTargetLowering::LowerGlobalAddress(SDValue Op, 3185 SelectionDAG &DAG) const { 3186 switch (Subtarget->getTargetTriple().getObjectFormat()) { 3187 default: llvm_unreachable("unknown object format"); 3188 case Triple::COFF: 3189 return LowerGlobalAddressWindows(Op, DAG); 3190 case Triple::ELF: 3191 return LowerGlobalAddressELF(Op, DAG); 3192 case Triple::MachO: 3193 return LowerGlobalAddressDarwin(Op, DAG); 3194 } 3195 } 3196 3197 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 3198 SelectionDAG &DAG) const { 3199 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3200 SDLoc dl(Op); 3201 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3202 const TargetMachine &TM = getTargetMachine(); 3203 bool IsRO = isReadOnly(GV); 3204 3205 // promoteToConstantPool only if not generating XO text section 3206 if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV) && !Subtarget->genExecuteOnly()) 3207 if (SDValue V = promoteToConstantPool(this, GV, DAG, PtrVT, dl)) 3208 return V; 3209 3210 if (isPositionIndependent()) { 3211 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); 3212 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 3213 UseGOT_PREL ? ARMII::MO_GOT : 0); 3214 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G); 3215 if (UseGOT_PREL) 3216 Result = 3217 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 3218 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3219 return Result; 3220 } else if (Subtarget->isROPI() && IsRO) { 3221 // PC-relative. 3222 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT); 3223 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G); 3224 return Result; 3225 } else if (Subtarget->isRWPI() && !IsRO) { 3226 // SB-relative. 3227 SDValue RelAddr; 3228 if (Subtarget->useMovt()) { 3229 ++NumMovwMovt; 3230 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_SBREL); 3231 RelAddr = DAG.getNode(ARMISD::Wrapper, dl, PtrVT, G); 3232 } else { // use literal pool for address constant 3233 ARMConstantPoolValue *CPV = 3234 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL); 3235 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3236 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3237 RelAddr = DAG.getLoad( 3238 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3239 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3240 } 3241 SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT); 3242 SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, RelAddr); 3243 return Result; 3244 } 3245 3246 // If we have T2 ops, we can materialize the address directly via movt/movw 3247 // pair. This is always cheaper. 3248 if (Subtarget->useMovt()) { 3249 ++NumMovwMovt; 3250 // FIXME: Once remat is capable of dealing with instructions with register 3251 // operands, expand this into two nodes. 3252 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 3253 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 3254 } else { 3255 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 3256 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3257 return DAG.getLoad( 3258 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3259 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3260 } 3261 } 3262 3263 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 3264 SelectionDAG &DAG) const { 3265 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 3266 "ROPI/RWPI not currently supported for Darwin"); 3267 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3268 SDLoc dl(Op); 3269 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3270 3271 if (Subtarget->useMovt()) 3272 ++NumMovwMovt; 3273 3274 // FIXME: Once remat is capable of dealing with instructions with register 3275 // operands, expand this into multiple nodes 3276 unsigned Wrapper = 3277 isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper; 3278 3279 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY); 3280 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G); 3281 3282 if (Subtarget->isGVIndirectSymbol(GV)) 3283 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 3284 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3285 return Result; 3286 } 3287 3288 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op, 3289 SelectionDAG &DAG) const { 3290 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported"); 3291 assert(Subtarget->useMovt() && 3292 "Windows on ARM expects to use movw/movt"); 3293 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 3294 "ROPI/RWPI not currently supported for Windows"); 3295 3296 const TargetMachine &TM = getTargetMachine(); 3297 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3298 ARMII::TOF TargetFlags = ARMII::MO_NO_FLAG; 3299 if (GV->hasDLLImportStorageClass()) 3300 TargetFlags = ARMII::MO_DLLIMPORT; 3301 else if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) 3302 TargetFlags = ARMII::MO_COFFSTUB; 3303 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3304 SDValue Result; 3305 SDLoc DL(Op); 3306 3307 ++NumMovwMovt; 3308 3309 // FIXME: Once remat is capable of dealing with instructions with register 3310 // operands, expand this into two nodes. 3311 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, 3312 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0, 3313 TargetFlags)); 3314 if (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB)) 3315 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 3316 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3317 return Result; 3318 } 3319 3320 SDValue 3321 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 3322 SDLoc dl(Op); 3323 SDValue Val = DAG.getConstant(0, dl, MVT::i32); 3324 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 3325 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 3326 Op.getOperand(1), Val); 3327 } 3328 3329 SDValue 3330 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 3331 SDLoc dl(Op); 3332 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 3333 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32)); 3334 } 3335 3336 SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op, 3337 SelectionDAG &DAG) const { 3338 SDLoc dl(Op); 3339 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other, 3340 Op.getOperand(0)); 3341 } 3342 3343 SDValue 3344 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 3345 const ARMSubtarget *Subtarget) const { 3346 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3347 SDLoc dl(Op); 3348 switch (IntNo) { 3349 default: return SDValue(); // Don't custom lower most intrinsics. 3350 case Intrinsic::thread_pointer: { 3351 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3352 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 3353 } 3354 case Intrinsic::eh_sjlj_lsda: { 3355 MachineFunction &MF = DAG.getMachineFunction(); 3356 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3357 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 3358 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3359 SDValue CPAddr; 3360 bool IsPositionIndependent = isPositionIndependent(); 3361 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0; 3362 ARMConstantPoolValue *CPV = 3363 ARMConstantPoolConstant::Create(&MF.getFunction(), ARMPCLabelIndex, 3364 ARMCP::CPLSDA, PCAdj); 3365 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3366 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3367 SDValue Result = DAG.getLoad( 3368 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3369 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3370 3371 if (IsPositionIndependent) { 3372 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 3373 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 3374 } 3375 return Result; 3376 } 3377 case Intrinsic::arm_neon_vabs: 3378 return DAG.getNode(ISD::ABS, SDLoc(Op), Op.getValueType(), 3379 Op.getOperand(1)); 3380 case Intrinsic::arm_neon_vmulls: 3381 case Intrinsic::arm_neon_vmullu: { 3382 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 3383 ? ARMISD::VMULLs : ARMISD::VMULLu; 3384 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3385 Op.getOperand(1), Op.getOperand(2)); 3386 } 3387 case Intrinsic::arm_neon_vminnm: 3388 case Intrinsic::arm_neon_vmaxnm: { 3389 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm) 3390 ? ISD::FMINNUM : ISD::FMAXNUM; 3391 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3392 Op.getOperand(1), Op.getOperand(2)); 3393 } 3394 case Intrinsic::arm_neon_vminu: 3395 case Intrinsic::arm_neon_vmaxu: { 3396 if (Op.getValueType().isFloatingPoint()) 3397 return SDValue(); 3398 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu) 3399 ? ISD::UMIN : ISD::UMAX; 3400 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3401 Op.getOperand(1), Op.getOperand(2)); 3402 } 3403 case Intrinsic::arm_neon_vmins: 3404 case Intrinsic::arm_neon_vmaxs: { 3405 // v{min,max}s is overloaded between signed integers and floats. 3406 if (!Op.getValueType().isFloatingPoint()) { 3407 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 3408 ? ISD::SMIN : ISD::SMAX; 3409 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3410 Op.getOperand(1), Op.getOperand(2)); 3411 } 3412 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 3413 ? ISD::FMINIMUM : ISD::FMAXIMUM; 3414 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3415 Op.getOperand(1), Op.getOperand(2)); 3416 } 3417 case Intrinsic::arm_neon_vtbl1: 3418 return DAG.getNode(ARMISD::VTBL1, SDLoc(Op), Op.getValueType(), 3419 Op.getOperand(1), Op.getOperand(2)); 3420 case Intrinsic::arm_neon_vtbl2: 3421 return DAG.getNode(ARMISD::VTBL2, SDLoc(Op), Op.getValueType(), 3422 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3423 } 3424 } 3425 3426 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 3427 const ARMSubtarget *Subtarget) { 3428 SDLoc dl(Op); 3429 ConstantSDNode *SSIDNode = cast<ConstantSDNode>(Op.getOperand(2)); 3430 auto SSID = static_cast<SyncScope::ID>(SSIDNode->getZExtValue()); 3431 if (SSID == SyncScope::SingleThread) 3432 return Op; 3433 3434 if (!Subtarget->hasDataBarrier()) { 3435 // Some ARMv6 cpus can support data barriers with an mcr instruction. 3436 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 3437 // here. 3438 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 3439 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!"); 3440 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 3441 DAG.getConstant(0, dl, MVT::i32)); 3442 } 3443 3444 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 3445 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 3446 ARM_MB::MemBOpt Domain = ARM_MB::ISH; 3447 if (Subtarget->isMClass()) { 3448 // Only a full system barrier exists in the M-class architectures. 3449 Domain = ARM_MB::SY; 3450 } else if (Subtarget->preferISHSTBarriers() && 3451 Ord == AtomicOrdering::Release) { 3452 // Swift happens to implement ISHST barriers in a way that's compatible with 3453 // Release semantics but weaker than ISH so we'd be fools not to use 3454 // it. Beware: other processors probably don't! 3455 Domain = ARM_MB::ISHST; 3456 } 3457 3458 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0), 3459 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32), 3460 DAG.getConstant(Domain, dl, MVT::i32)); 3461 } 3462 3463 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 3464 const ARMSubtarget *Subtarget) { 3465 // ARM pre v5TE and Thumb1 does not have preload instructions. 3466 if (!(Subtarget->isThumb2() || 3467 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 3468 // Just preserve the chain. 3469 return Op.getOperand(0); 3470 3471 SDLoc dl(Op); 3472 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 3473 if (!isRead && 3474 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 3475 // ARMv7 with MP extension has PLDW. 3476 return Op.getOperand(0); 3477 3478 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3479 if (Subtarget->isThumb()) { 3480 // Invert the bits. 3481 isRead = ~isRead & 1; 3482 isData = ~isData & 1; 3483 } 3484 3485 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 3486 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32), 3487 DAG.getConstant(isData, dl, MVT::i32)); 3488 } 3489 3490 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 3491 MachineFunction &MF = DAG.getMachineFunction(); 3492 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 3493 3494 // vastart just stores the address of the VarArgsFrameIndex slot into the 3495 // memory location argument. 3496 SDLoc dl(Op); 3497 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 3498 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3499 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 3500 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 3501 MachinePointerInfo(SV)); 3502 } 3503 3504 SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, 3505 CCValAssign &NextVA, 3506 SDValue &Root, 3507 SelectionDAG &DAG, 3508 const SDLoc &dl) const { 3509 MachineFunction &MF = DAG.getMachineFunction(); 3510 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3511 3512 const TargetRegisterClass *RC; 3513 if (AFI->isThumb1OnlyFunction()) 3514 RC = &ARM::tGPRRegClass; 3515 else 3516 RC = &ARM::GPRRegClass; 3517 3518 // Transform the arguments stored in physical registers into virtual ones. 3519 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3520 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3521 3522 SDValue ArgValue2; 3523 if (NextVA.isMemLoc()) { 3524 MachineFrameInfo &MFI = MF.getFrameInfo(); 3525 int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true); 3526 3527 // Create load node to retrieve arguments from the stack. 3528 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3529 ArgValue2 = DAG.getLoad( 3530 MVT::i32, dl, Root, FIN, 3531 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); 3532 } else { 3533 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 3534 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3535 } 3536 if (!Subtarget->isLittle()) 3537 std::swap (ArgValue, ArgValue2); 3538 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 3539 } 3540 3541 // The remaining GPRs hold either the beginning of variable-argument 3542 // data, or the beginning of an aggregate passed by value (usually 3543 // byval). Either way, we allocate stack slots adjacent to the data 3544 // provided by our caller, and store the unallocated registers there. 3545 // If this is a variadic function, the va_list pointer will begin with 3546 // these values; otherwise, this reassembles a (byval) structure that 3547 // was split between registers and memory. 3548 // Return: The frame index registers were stored into. 3549 int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 3550 const SDLoc &dl, SDValue &Chain, 3551 const Value *OrigArg, 3552 unsigned InRegsParamRecordIdx, 3553 int ArgOffset, unsigned ArgSize) const { 3554 // Currently, two use-cases possible: 3555 // Case #1. Non-var-args function, and we meet first byval parameter. 3556 // Setup first unallocated register as first byval register; 3557 // eat all remained registers 3558 // (these two actions are performed by HandleByVal method). 3559 // Then, here, we initialize stack frame with 3560 // "store-reg" instructions. 3561 // Case #2. Var-args function, that doesn't contain byval parameters. 3562 // The same: eat all remained unallocated registers, 3563 // initialize stack frame. 3564 3565 MachineFunction &MF = DAG.getMachineFunction(); 3566 MachineFrameInfo &MFI = MF.getFrameInfo(); 3567 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3568 unsigned RBegin, REnd; 3569 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 3570 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 3571 } else { 3572 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3573 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx]; 3574 REnd = ARM::R4; 3575 } 3576 3577 if (REnd != RBegin) 3578 ArgOffset = -4 * (ARM::R4 - RBegin); 3579 3580 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3581 int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false); 3582 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT); 3583 3584 SmallVector<SDValue, 4> MemOps; 3585 const TargetRegisterClass *RC = 3586 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 3587 3588 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) { 3589 unsigned VReg = MF.addLiveIn(Reg, RC); 3590 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 3591 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3592 MachinePointerInfo(OrigArg, 4 * i)); 3593 MemOps.push_back(Store); 3594 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT)); 3595 } 3596 3597 if (!MemOps.empty()) 3598 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3599 return FrameIndex; 3600 } 3601 3602 // Setup stack frame, the va_list pointer will start from. 3603 void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 3604 const SDLoc &dl, SDValue &Chain, 3605 unsigned ArgOffset, 3606 unsigned TotalArgRegsSaveSize, 3607 bool ForceMutable) const { 3608 MachineFunction &MF = DAG.getMachineFunction(); 3609 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3610 3611 // Try to store any remaining integer argument regs 3612 // to their spots on the stack so that they may be loaded by dereferencing 3613 // the result of va_next. 3614 // If there is no regs to be stored, just point address after last 3615 // argument passed via stack. 3616 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr, 3617 CCInfo.getInRegsParamsCount(), 3618 CCInfo.getNextStackOffset(), 4); 3619 AFI->setVarArgsFrameIndex(FrameIndex); 3620 } 3621 3622 SDValue ARMTargetLowering::LowerFormalArguments( 3623 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 3624 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 3625 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3626 MachineFunction &MF = DAG.getMachineFunction(); 3627 MachineFrameInfo &MFI = MF.getFrameInfo(); 3628 3629 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3630 3631 // Assign locations to all of the incoming arguments. 3632 SmallVector<CCValAssign, 16> ArgLocs; 3633 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 3634 *DAG.getContext()); 3635 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg)); 3636 3637 SmallVector<SDValue, 16> ArgValues; 3638 SDValue ArgValue; 3639 Function::const_arg_iterator CurOrigArg = MF.getFunction().arg_begin(); 3640 unsigned CurArgIdx = 0; 3641 3642 // Initially ArgRegsSaveSize is zero. 3643 // Then we increase this value each time we meet byval parameter. 3644 // We also increase this value in case of varargs function. 3645 AFI->setArgRegsSaveSize(0); 3646 3647 // Calculate the amount of stack space that we need to allocate to store 3648 // byval and variadic arguments that are passed in registers. 3649 // We need to know this before we allocate the first byval or variadic 3650 // argument, as they will be allocated a stack slot below the CFA (Canonical 3651 // Frame Address, the stack pointer at entry to the function). 3652 unsigned ArgRegBegin = ARM::R4; 3653 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3654 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount()) 3655 break; 3656 3657 CCValAssign &VA = ArgLocs[i]; 3658 unsigned Index = VA.getValNo(); 3659 ISD::ArgFlagsTy Flags = Ins[Index].Flags; 3660 if (!Flags.isByVal()) 3661 continue; 3662 3663 assert(VA.isMemLoc() && "unexpected byval pointer in reg"); 3664 unsigned RBegin, REnd; 3665 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd); 3666 ArgRegBegin = std::min(ArgRegBegin, RBegin); 3667 3668 CCInfo.nextInRegsParam(); 3669 } 3670 CCInfo.rewindByValRegsInfo(); 3671 3672 int lastInsIndex = -1; 3673 if (isVarArg && MFI.hasVAStart()) { 3674 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3675 if (RegIdx != array_lengthof(GPRArgRegs)) 3676 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]); 3677 } 3678 3679 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin); 3680 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize); 3681 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3682 3683 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3684 CCValAssign &VA = ArgLocs[i]; 3685 if (Ins[VA.getValNo()].isOrigArg()) { 3686 std::advance(CurOrigArg, 3687 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx); 3688 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex(); 3689 } 3690 // Arguments stored in registers. 3691 if (VA.isRegLoc()) { 3692 EVT RegVT = VA.getLocVT(); 3693 3694 if (VA.needsCustom()) { 3695 // f64 and vector types are split up into multiple registers or 3696 // combinations of registers and stack slots. 3697 if (VA.getLocVT() == MVT::v2f64) { 3698 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 3699 Chain, DAG, dl); 3700 VA = ArgLocs[++i]; // skip ahead to next loc 3701 SDValue ArgValue2; 3702 if (VA.isMemLoc()) { 3703 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true); 3704 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3705 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 3706 MachinePointerInfo::getFixedStack( 3707 DAG.getMachineFunction(), FI)); 3708 } else { 3709 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 3710 Chain, DAG, dl); 3711 } 3712 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 3713 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3714 ArgValue, ArgValue1, 3715 DAG.getIntPtrConstant(0, dl)); 3716 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3717 ArgValue, ArgValue2, 3718 DAG.getIntPtrConstant(1, dl)); 3719 } else 3720 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 3721 } else { 3722 const TargetRegisterClass *RC; 3723 3724 3725 if (RegVT == MVT::f16) 3726 RC = &ARM::HPRRegClass; 3727 else if (RegVT == MVT::f32) 3728 RC = &ARM::SPRRegClass; 3729 else if (RegVT == MVT::f64 || RegVT == MVT::v4f16) 3730 RC = &ARM::DPRRegClass; 3731 else if (RegVT == MVT::v2f64 || RegVT == MVT::v8f16) 3732 RC = &ARM::QPRRegClass; 3733 else if (RegVT == MVT::i32) 3734 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass 3735 : &ARM::GPRRegClass; 3736 else 3737 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 3738 3739 // Transform the arguments in physical registers into virtual ones. 3740 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3741 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 3742 } 3743 3744 // If this is an 8 or 16-bit value, it is really passed promoted 3745 // to 32 bits. Insert an assert[sz]ext to capture this, then 3746 // truncate to the right size. 3747 switch (VA.getLocInfo()) { 3748 default: llvm_unreachable("Unknown loc info!"); 3749 case CCValAssign::Full: break; 3750 case CCValAssign::BCvt: 3751 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 3752 break; 3753 case CCValAssign::SExt: 3754 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 3755 DAG.getValueType(VA.getValVT())); 3756 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3757 break; 3758 case CCValAssign::ZExt: 3759 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 3760 DAG.getValueType(VA.getValVT())); 3761 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3762 break; 3763 } 3764 3765 InVals.push_back(ArgValue); 3766 } else { // VA.isRegLoc() 3767 // sanity check 3768 assert(VA.isMemLoc()); 3769 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 3770 3771 int index = VA.getValNo(); 3772 3773 // Some Ins[] entries become multiple ArgLoc[] entries. 3774 // Process them only once. 3775 if (index != lastInsIndex) 3776 { 3777 ISD::ArgFlagsTy Flags = Ins[index].Flags; 3778 // FIXME: For now, all byval parameter objects are marked mutable. 3779 // This can be changed with more analysis. 3780 // In case of tail call optimization mark all arguments mutable. 3781 // Since they could be overwritten by lowering of arguments in case of 3782 // a tail call. 3783 if (Flags.isByVal()) { 3784 assert(Ins[index].isOrigArg() && 3785 "Byval arguments cannot be implicit"); 3786 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed(); 3787 3788 int FrameIndex = StoreByValRegs( 3789 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex, 3790 VA.getLocMemOffset(), Flags.getByValSize()); 3791 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT)); 3792 CCInfo.nextInRegsParam(); 3793 } else { 3794 unsigned FIOffset = VA.getLocMemOffset(); 3795 int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 3796 FIOffset, true); 3797 3798 // Create load nodes to retrieve arguments from the stack. 3799 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3800 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 3801 MachinePointerInfo::getFixedStack( 3802 DAG.getMachineFunction(), FI))); 3803 } 3804 lastInsIndex = index; 3805 } 3806 } 3807 } 3808 3809 // varargs 3810 if (isVarArg && MFI.hasVAStart()) 3811 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 3812 CCInfo.getNextStackOffset(), 3813 TotalArgRegsSaveSize); 3814 3815 AFI->setArgumentStackSize(CCInfo.getNextStackOffset()); 3816 3817 return Chain; 3818 } 3819 3820 /// isFloatingPointZero - Return true if this is +0.0. 3821 static bool isFloatingPointZero(SDValue Op) { 3822 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 3823 return CFP->getValueAPF().isPosZero(); 3824 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 3825 // Maybe this has already been legalized into the constant pool? 3826 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 3827 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 3828 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 3829 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 3830 return CFP->getValueAPF().isPosZero(); 3831 } 3832 } else if (Op->getOpcode() == ISD::BITCAST && 3833 Op->getValueType(0) == MVT::f64) { 3834 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64) 3835 // created by LowerConstantFP(). 3836 SDValue BitcastOp = Op->getOperand(0); 3837 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM && 3838 isNullConstant(BitcastOp->getOperand(0))) 3839 return true; 3840 } 3841 return false; 3842 } 3843 3844 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 3845 /// the given operands. 3846 SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 3847 SDValue &ARMcc, SelectionDAG &DAG, 3848 const SDLoc &dl) const { 3849 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 3850 unsigned C = RHSC->getZExtValue(); 3851 if (!isLegalICmpImmediate((int32_t)C)) { 3852 // Constant does not fit, try adjusting it by one. 3853 switch (CC) { 3854 default: break; 3855 case ISD::SETLT: 3856 case ISD::SETGE: 3857 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 3858 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 3859 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3860 } 3861 break; 3862 case ISD::SETULT: 3863 case ISD::SETUGE: 3864 if (C != 0 && isLegalICmpImmediate(C-1)) { 3865 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 3866 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3867 } 3868 break; 3869 case ISD::SETLE: 3870 case ISD::SETGT: 3871 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 3872 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 3873 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3874 } 3875 break; 3876 case ISD::SETULE: 3877 case ISD::SETUGT: 3878 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 3879 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 3880 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3881 } 3882 break; 3883 } 3884 } 3885 } else if ((ARM_AM::getShiftOpcForNode(LHS.getOpcode()) != ARM_AM::no_shift) && 3886 (ARM_AM::getShiftOpcForNode(RHS.getOpcode()) == ARM_AM::no_shift)) { 3887 // In ARM and Thumb-2, the compare instructions can shift their second 3888 // operand. 3889 CC = ISD::getSetCCSwappedOperands(CC); 3890 std::swap(LHS, RHS); 3891 } 3892 3893 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3894 ARMISD::NodeType CompareType; 3895 switch (CondCode) { 3896 default: 3897 CompareType = ARMISD::CMP; 3898 break; 3899 case ARMCC::EQ: 3900 case ARMCC::NE: 3901 // Uses only Z Flag 3902 CompareType = ARMISD::CMPZ; 3903 break; 3904 } 3905 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3906 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 3907 } 3908 3909 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 3910 SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, 3911 SelectionDAG &DAG, const SDLoc &dl, 3912 bool InvalidOnQNaN) const { 3913 assert(Subtarget->hasFP64() || RHS.getValueType() != MVT::f64); 3914 SDValue Cmp; 3915 SDValue C = DAG.getConstant(InvalidOnQNaN, dl, MVT::i32); 3916 if (!isFloatingPointZero(RHS)) 3917 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS, C); 3918 else 3919 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS, C); 3920 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 3921 } 3922 3923 /// duplicateCmp - Glue values can have only one use, so this function 3924 /// duplicates a comparison node. 3925 SDValue 3926 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 3927 unsigned Opc = Cmp.getOpcode(); 3928 SDLoc DL(Cmp); 3929 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 3930 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3931 3932 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 3933 Cmp = Cmp.getOperand(0); 3934 Opc = Cmp.getOpcode(); 3935 if (Opc == ARMISD::CMPFP) 3936 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0), 3937 Cmp.getOperand(1), Cmp.getOperand(2)); 3938 else { 3939 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 3940 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0), 3941 Cmp.getOperand(1)); 3942 } 3943 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 3944 } 3945 3946 // This function returns three things: the arithmetic computation itself 3947 // (Value), a comparison (OverflowCmp), and a condition code (ARMcc). The 3948 // comparison and the condition code define the case in which the arithmetic 3949 // computation *does not* overflow. 3950 std::pair<SDValue, SDValue> 3951 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG, 3952 SDValue &ARMcc) const { 3953 assert(Op.getValueType() == MVT::i32 && "Unsupported value type"); 3954 3955 SDValue Value, OverflowCmp; 3956 SDValue LHS = Op.getOperand(0); 3957 SDValue RHS = Op.getOperand(1); 3958 SDLoc dl(Op); 3959 3960 // FIXME: We are currently always generating CMPs because we don't support 3961 // generating CMN through the backend. This is not as good as the natural 3962 // CMP case because it causes a register dependency and cannot be folded 3963 // later. 3964 3965 switch (Op.getOpcode()) { 3966 default: 3967 llvm_unreachable("Unknown overflow instruction!"); 3968 case ISD::SADDO: 3969 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3970 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 3971 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3972 break; 3973 case ISD::UADDO: 3974 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3975 // We use ADDC here to correspond to its use in LowerUnsignedALUO. 3976 // We do not use it in the USUBO case as Value may not be used. 3977 Value = DAG.getNode(ARMISD::ADDC, dl, 3978 DAG.getVTList(Op.getValueType(), MVT::i32), LHS, RHS) 3979 .getValue(0); 3980 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3981 break; 3982 case ISD::SSUBO: 3983 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3984 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3985 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3986 break; 3987 case ISD::USUBO: 3988 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3989 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3990 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3991 break; 3992 case ISD::UMULO: 3993 // We generate a UMUL_LOHI and then check if the high word is 0. 3994 ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32); 3995 Value = DAG.getNode(ISD::UMUL_LOHI, dl, 3996 DAG.getVTList(Op.getValueType(), Op.getValueType()), 3997 LHS, RHS); 3998 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1), 3999 DAG.getConstant(0, dl, MVT::i32)); 4000 Value = Value.getValue(0); // We only want the low 32 bits for the result. 4001 break; 4002 case ISD::SMULO: 4003 // We generate a SMUL_LOHI and then check if all the bits of the high word 4004 // are the same as the sign bit of the low word. 4005 ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32); 4006 Value = DAG.getNode(ISD::SMUL_LOHI, dl, 4007 DAG.getVTList(Op.getValueType(), Op.getValueType()), 4008 LHS, RHS); 4009 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1), 4010 DAG.getNode(ISD::SRA, dl, Op.getValueType(), 4011 Value.getValue(0), 4012 DAG.getConstant(31, dl, MVT::i32))); 4013 Value = Value.getValue(0); // We only want the low 32 bits for the result. 4014 break; 4015 } // switch (...) 4016 4017 return std::make_pair(Value, OverflowCmp); 4018 } 4019 4020 SDValue 4021 ARMTargetLowering::LowerSignedALUO(SDValue Op, SelectionDAG &DAG) const { 4022 // Let legalize expand this if it isn't a legal type yet. 4023 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 4024 return SDValue(); 4025 4026 SDValue Value, OverflowCmp; 4027 SDValue ARMcc; 4028 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc); 4029 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4030 SDLoc dl(Op); 4031 // We use 0 and 1 as false and true values. 4032 SDValue TVal = DAG.getConstant(1, dl, MVT::i32); 4033 SDValue FVal = DAG.getConstant(0, dl, MVT::i32); 4034 EVT VT = Op.getValueType(); 4035 4036 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal, 4037 ARMcc, CCR, OverflowCmp); 4038 4039 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 4040 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 4041 } 4042 4043 static SDValue ConvertBooleanCarryToCarryFlag(SDValue BoolCarry, 4044 SelectionDAG &DAG) { 4045 SDLoc DL(BoolCarry); 4046 EVT CarryVT = BoolCarry.getValueType(); 4047 4048 // This converts the boolean value carry into the carry flag by doing 4049 // ARMISD::SUBC Carry, 1 4050 SDValue Carry = DAG.getNode(ARMISD::SUBC, DL, 4051 DAG.getVTList(CarryVT, MVT::i32), 4052 BoolCarry, DAG.getConstant(1, DL, CarryVT)); 4053 return Carry.getValue(1); 4054 } 4055 4056 static SDValue ConvertCarryFlagToBooleanCarry(SDValue Flags, EVT VT, 4057 SelectionDAG &DAG) { 4058 SDLoc DL(Flags); 4059 4060 // Now convert the carry flag into a boolean carry. We do this 4061 // using ARMISD:ADDE 0, 0, Carry 4062 return DAG.getNode(ARMISD::ADDE, DL, DAG.getVTList(VT, MVT::i32), 4063 DAG.getConstant(0, DL, MVT::i32), 4064 DAG.getConstant(0, DL, MVT::i32), Flags); 4065 } 4066 4067 SDValue ARMTargetLowering::LowerUnsignedALUO(SDValue Op, 4068 SelectionDAG &DAG) const { 4069 // Let legalize expand this if it isn't a legal type yet. 4070 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 4071 return SDValue(); 4072 4073 SDValue LHS = Op.getOperand(0); 4074 SDValue RHS = Op.getOperand(1); 4075 SDLoc dl(Op); 4076 4077 EVT VT = Op.getValueType(); 4078 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 4079 SDValue Value; 4080 SDValue Overflow; 4081 switch (Op.getOpcode()) { 4082 default: 4083 llvm_unreachable("Unknown overflow instruction!"); 4084 case ISD::UADDO: 4085 Value = DAG.getNode(ARMISD::ADDC, dl, VTs, LHS, RHS); 4086 // Convert the carry flag into a boolean value. 4087 Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG); 4088 break; 4089 case ISD::USUBO: { 4090 Value = DAG.getNode(ARMISD::SUBC, dl, VTs, LHS, RHS); 4091 // Convert the carry flag into a boolean value. 4092 Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG); 4093 // ARMISD::SUBC returns 0 when we have to borrow, so make it an overflow 4094 // value. So compute 1 - C. 4095 Overflow = DAG.getNode(ISD::SUB, dl, MVT::i32, 4096 DAG.getConstant(1, dl, MVT::i32), Overflow); 4097 break; 4098 } 4099 } 4100 4101 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 4102 } 4103 4104 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 4105 SDValue Cond = Op.getOperand(0); 4106 SDValue SelectTrue = Op.getOperand(1); 4107 SDValue SelectFalse = Op.getOperand(2); 4108 SDLoc dl(Op); 4109 unsigned Opc = Cond.getOpcode(); 4110 4111 if (Cond.getResNo() == 1 && 4112 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4113 Opc == ISD::USUBO)) { 4114 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 4115 return SDValue(); 4116 4117 SDValue Value, OverflowCmp; 4118 SDValue ARMcc; 4119 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 4120 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4121 EVT VT = Op.getValueType(); 4122 4123 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR, 4124 OverflowCmp, DAG); 4125 } 4126 4127 // Convert: 4128 // 4129 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 4130 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 4131 // 4132 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 4133 const ConstantSDNode *CMOVTrue = 4134 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 4135 const ConstantSDNode *CMOVFalse = 4136 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 4137 4138 if (CMOVTrue && CMOVFalse) { 4139 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 4140 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 4141 4142 SDValue True; 4143 SDValue False; 4144 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 4145 True = SelectTrue; 4146 False = SelectFalse; 4147 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 4148 True = SelectFalse; 4149 False = SelectTrue; 4150 } 4151 4152 if (True.getNode() && False.getNode()) { 4153 EVT VT = Op.getValueType(); 4154 SDValue ARMcc = Cond.getOperand(2); 4155 SDValue CCR = Cond.getOperand(3); 4156 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 4157 assert(True.getValueType() == VT); 4158 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG); 4159 } 4160 } 4161 } 4162 4163 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 4164 // undefined bits before doing a full-word comparison with zero. 4165 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 4166 DAG.getConstant(1, dl, Cond.getValueType())); 4167 4168 return DAG.getSelectCC(dl, Cond, 4169 DAG.getConstant(0, dl, Cond.getValueType()), 4170 SelectTrue, SelectFalse, ISD::SETNE); 4171 } 4172 4173 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 4174 bool &swpCmpOps, bool &swpVselOps) { 4175 // Start by selecting the GE condition code for opcodes that return true for 4176 // 'equality' 4177 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 4178 CC == ISD::SETULE || CC == ISD::SETGE || CC == ISD::SETLE) 4179 CondCode = ARMCC::GE; 4180 4181 // and GT for opcodes that return false for 'equality'. 4182 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 4183 CC == ISD::SETULT || CC == ISD::SETGT || CC == ISD::SETLT) 4184 CondCode = ARMCC::GT; 4185 4186 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 4187 // to swap the compare operands. 4188 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 4189 CC == ISD::SETULT || CC == ISD::SETLE || CC == ISD::SETLT) 4190 swpCmpOps = true; 4191 4192 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 4193 // If we have an unordered opcode, we need to swap the operands to the VSEL 4194 // instruction (effectively negating the condition). 4195 // 4196 // This also has the effect of swapping which one of 'less' or 'greater' 4197 // returns true, so we also swap the compare operands. It also switches 4198 // whether we return true for 'equality', so we compensate by picking the 4199 // opposite condition code to our original choice. 4200 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 4201 CC == ISD::SETUGT) { 4202 swpCmpOps = !swpCmpOps; 4203 swpVselOps = !swpVselOps; 4204 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 4205 } 4206 4207 // 'ordered' is 'anything but unordered', so use the VS condition code and 4208 // swap the VSEL operands. 4209 if (CC == ISD::SETO) { 4210 CondCode = ARMCC::VS; 4211 swpVselOps = true; 4212 } 4213 4214 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 4215 // code and swap the VSEL operands. Also do this if we don't care about the 4216 // unordered case. 4217 if (CC == ISD::SETUNE || CC == ISD::SETNE) { 4218 CondCode = ARMCC::EQ; 4219 swpVselOps = true; 4220 } 4221 } 4222 4223 SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal, 4224 SDValue TrueVal, SDValue ARMcc, SDValue CCR, 4225 SDValue Cmp, SelectionDAG &DAG) const { 4226 if (!Subtarget->hasFP64() && VT == MVT::f64) { 4227 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl, 4228 DAG.getVTList(MVT::i32, MVT::i32), FalseVal); 4229 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl, 4230 DAG.getVTList(MVT::i32, MVT::i32), TrueVal); 4231 4232 SDValue TrueLow = TrueVal.getValue(0); 4233 SDValue TrueHigh = TrueVal.getValue(1); 4234 SDValue FalseLow = FalseVal.getValue(0); 4235 SDValue FalseHigh = FalseVal.getValue(1); 4236 4237 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow, 4238 ARMcc, CCR, Cmp); 4239 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh, 4240 ARMcc, CCR, duplicateCmp(Cmp, DAG)); 4241 4242 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High); 4243 } else { 4244 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 4245 Cmp); 4246 } 4247 } 4248 4249 static bool isGTorGE(ISD::CondCode CC) { 4250 return CC == ISD::SETGT || CC == ISD::SETGE; 4251 } 4252 4253 static bool isLTorLE(ISD::CondCode CC) { 4254 return CC == ISD::SETLT || CC == ISD::SETLE; 4255 } 4256 4257 // See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating. 4258 // All of these conditions (and their <= and >= counterparts) will do: 4259 // x < k ? k : x 4260 // x > k ? x : k 4261 // k < x ? x : k 4262 // k > x ? k : x 4263 static bool isLowerSaturate(const SDValue LHS, const SDValue RHS, 4264 const SDValue TrueVal, const SDValue FalseVal, 4265 const ISD::CondCode CC, const SDValue K) { 4266 return (isGTorGE(CC) && 4267 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) || 4268 (isLTorLE(CC) && 4269 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))); 4270 } 4271 4272 // Similar to isLowerSaturate(), but checks for upper-saturating conditions. 4273 static bool isUpperSaturate(const SDValue LHS, const SDValue RHS, 4274 const SDValue TrueVal, const SDValue FalseVal, 4275 const ISD::CondCode CC, const SDValue K) { 4276 return (isGTorGE(CC) && 4277 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) || 4278 (isLTorLE(CC) && 4279 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))); 4280 } 4281 4282 // Check if two chained conditionals could be converted into SSAT or USAT. 4283 // 4284 // SSAT can replace a set of two conditional selectors that bound a number to an 4285 // interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples: 4286 // 4287 // x < -k ? -k : (x > k ? k : x) 4288 // x < -k ? -k : (x < k ? x : k) 4289 // x > -k ? (x > k ? k : x) : -k 4290 // x < k ? (x < -k ? -k : x) : k 4291 // etc. 4292 // 4293 // USAT works similarily to SSAT but bounds on the interval [0, k] where k + 1 is 4294 // a power of 2. 4295 // 4296 // It returns true if the conversion can be done, false otherwise. 4297 // Additionally, the variable is returned in parameter V, the constant in K and 4298 // usat is set to true if the conditional represents an unsigned saturation 4299 static bool isSaturatingConditional(const SDValue &Op, SDValue &V, 4300 uint64_t &K, bool &usat) { 4301 SDValue LHS1 = Op.getOperand(0); 4302 SDValue RHS1 = Op.getOperand(1); 4303 SDValue TrueVal1 = Op.getOperand(2); 4304 SDValue FalseVal1 = Op.getOperand(3); 4305 ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4306 4307 const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1; 4308 if (Op2.getOpcode() != ISD::SELECT_CC) 4309 return false; 4310 4311 SDValue LHS2 = Op2.getOperand(0); 4312 SDValue RHS2 = Op2.getOperand(1); 4313 SDValue TrueVal2 = Op2.getOperand(2); 4314 SDValue FalseVal2 = Op2.getOperand(3); 4315 ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get(); 4316 4317 // Find out which are the constants and which are the variables 4318 // in each conditional 4319 SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1) 4320 ? &RHS1 4321 : nullptr; 4322 SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2) 4323 ? &RHS2 4324 : nullptr; 4325 SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2; 4326 SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1; 4327 SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2; 4328 SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2; 4329 4330 // We must detect cases where the original operations worked with 16- or 4331 // 8-bit values. In such case, V2Tmp != V2 because the comparison operations 4332 // must work with sign-extended values but the select operations return 4333 // the original non-extended value. 4334 SDValue V2TmpReg = V2Tmp; 4335 if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG) 4336 V2TmpReg = V2Tmp->getOperand(0); 4337 4338 // Check that the registers and the constants have the correct values 4339 // in both conditionals 4340 if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp || 4341 V2TmpReg != V2) 4342 return false; 4343 4344 // Figure out which conditional is saturating the lower/upper bound. 4345 const SDValue *LowerCheckOp = 4346 isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1) 4347 ? &Op 4348 : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) 4349 ? &Op2 4350 : nullptr; 4351 const SDValue *UpperCheckOp = 4352 isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1) 4353 ? &Op 4354 : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) 4355 ? &Op2 4356 : nullptr; 4357 4358 if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp) 4359 return false; 4360 4361 // Check that the constant in the lower-bound check is 4362 // the opposite of the constant in the upper-bound check 4363 // in 1's complement. 4364 int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue(); 4365 int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue(); 4366 int64_t PosVal = std::max(Val1, Val2); 4367 int64_t NegVal = std::min(Val1, Val2); 4368 4369 if (((Val1 > Val2 && UpperCheckOp == &Op) || 4370 (Val1 < Val2 && UpperCheckOp == &Op2)) && 4371 isPowerOf2_64(PosVal + 1)) { 4372 4373 // Handle the difference between USAT (unsigned) and SSAT (signed) saturation 4374 if (Val1 == ~Val2) 4375 usat = false; 4376 else if (NegVal == 0) 4377 usat = true; 4378 else 4379 return false; 4380 4381 V = V2; 4382 K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive 4383 4384 return true; 4385 } 4386 4387 return false; 4388 } 4389 4390 // Check if a condition of the type x < k ? k : x can be converted into a 4391 // bit operation instead of conditional moves. 4392 // Currently this is allowed given: 4393 // - The conditions and values match up 4394 // - k is 0 or -1 (all ones) 4395 // This function will not check the last condition, thats up to the caller 4396 // It returns true if the transformation can be made, and in such case 4397 // returns x in V, and k in SatK. 4398 static bool isLowerSaturatingConditional(const SDValue &Op, SDValue &V, 4399 SDValue &SatK) 4400 { 4401 SDValue LHS = Op.getOperand(0); 4402 SDValue RHS = Op.getOperand(1); 4403 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4404 SDValue TrueVal = Op.getOperand(2); 4405 SDValue FalseVal = Op.getOperand(3); 4406 4407 SDValue *K = isa<ConstantSDNode>(LHS) ? &LHS : isa<ConstantSDNode>(RHS) 4408 ? &RHS 4409 : nullptr; 4410 4411 // No constant operation in comparison, early out 4412 if (!K) 4413 return false; 4414 4415 SDValue KTmp = isa<ConstantSDNode>(TrueVal) ? TrueVal : FalseVal; 4416 V = (KTmp == TrueVal) ? FalseVal : TrueVal; 4417 SDValue VTmp = (K && *K == LHS) ? RHS : LHS; 4418 4419 // If the constant on left and right side, or variable on left and right, 4420 // does not match, early out 4421 if (*K != KTmp || V != VTmp) 4422 return false; 4423 4424 if (isLowerSaturate(LHS, RHS, TrueVal, FalseVal, CC, *K)) { 4425 SatK = *K; 4426 return true; 4427 } 4428 4429 return false; 4430 } 4431 4432 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 4433 EVT VT = Op.getValueType(); 4434 SDLoc dl(Op); 4435 4436 // Try to convert two saturating conditional selects into a single SSAT 4437 SDValue SatValue; 4438 uint64_t SatConstant; 4439 bool SatUSat; 4440 if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) && 4441 isSaturatingConditional(Op, SatValue, SatConstant, SatUSat)) { 4442 if (SatUSat) 4443 return DAG.getNode(ARMISD::USAT, dl, VT, SatValue, 4444 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT)); 4445 else 4446 return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue, 4447 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT)); 4448 } 4449 4450 // Try to convert expressions of the form x < k ? k : x (and similar forms) 4451 // into more efficient bit operations, which is possible when k is 0 or -1 4452 // On ARM and Thumb-2 which have flexible operand 2 this will result in 4453 // single instructions. On Thumb the shift and the bit operation will be two 4454 // instructions. 4455 // Only allow this transformation on full-width (32-bit) operations 4456 SDValue LowerSatConstant; 4457 if (VT == MVT::i32 && 4458 isLowerSaturatingConditional(Op, SatValue, LowerSatConstant)) { 4459 SDValue ShiftV = DAG.getNode(ISD::SRA, dl, VT, SatValue, 4460 DAG.getConstant(31, dl, VT)); 4461 if (isNullConstant(LowerSatConstant)) { 4462 SDValue NotShiftV = DAG.getNode(ISD::XOR, dl, VT, ShiftV, 4463 DAG.getAllOnesConstant(dl, VT)); 4464 return DAG.getNode(ISD::AND, dl, VT, SatValue, NotShiftV); 4465 } else if (isAllOnesConstant(LowerSatConstant)) 4466 return DAG.getNode(ISD::OR, dl, VT, SatValue, ShiftV); 4467 } 4468 4469 SDValue LHS = Op.getOperand(0); 4470 SDValue RHS = Op.getOperand(1); 4471 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4472 SDValue TrueVal = Op.getOperand(2); 4473 SDValue FalseVal = Op.getOperand(3); 4474 4475 if (!Subtarget->hasFP64() && LHS.getValueType() == MVT::f64) { 4476 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 4477 dl); 4478 4479 // If softenSetCCOperands only returned one value, we should compare it to 4480 // zero. 4481 if (!RHS.getNode()) { 4482 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 4483 CC = ISD::SETNE; 4484 } 4485 } 4486 4487 if (LHS.getValueType() == MVT::i32) { 4488 // Try to generate VSEL on ARMv8. 4489 // The VSEL instruction can't use all the usual ARM condition 4490 // codes: it only has two bits to select the condition code, so it's 4491 // constrained to use only GE, GT, VS and EQ. 4492 // 4493 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 4494 // swap the operands of the previous compare instruction (effectively 4495 // inverting the compare condition, swapping 'less' and 'greater') and 4496 // sometimes need to swap the operands to the VSEL (which inverts the 4497 // condition in the sense of firing whenever the previous condition didn't) 4498 if (Subtarget->hasFPARMv8Base() && (TrueVal.getValueType() == MVT::f16 || 4499 TrueVal.getValueType() == MVT::f32 || 4500 TrueVal.getValueType() == MVT::f64)) { 4501 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 4502 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 4503 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 4504 CC = ISD::getSetCCInverse(CC, true); 4505 std::swap(TrueVal, FalseVal); 4506 } 4507 } 4508 4509 SDValue ARMcc; 4510 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4511 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 4512 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 4513 } 4514 4515 ARMCC::CondCodes CondCode, CondCode2; 4516 bool InvalidOnQNaN; 4517 FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN); 4518 4519 // Normalize the fp compare. If RHS is zero we prefer to keep it there so we 4520 // match CMPFPw0 instead of CMPFP, though we don't do this for f16 because we 4521 // must use VSEL (limited condition codes), due to not having conditional f16 4522 // moves. 4523 if (Subtarget->hasFPARMv8Base() && 4524 !(isFloatingPointZero(RHS) && TrueVal.getValueType() != MVT::f16) && 4525 (TrueVal.getValueType() == MVT::f16 || 4526 TrueVal.getValueType() == MVT::f32 || 4527 TrueVal.getValueType() == MVT::f64)) { 4528 bool swpCmpOps = false; 4529 bool swpVselOps = false; 4530 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 4531 4532 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 4533 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 4534 if (swpCmpOps) 4535 std::swap(LHS, RHS); 4536 if (swpVselOps) 4537 std::swap(TrueVal, FalseVal); 4538 } 4539 } 4540 4541 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4542 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN); 4543 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4544 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 4545 if (CondCode2 != ARMCC::AL) { 4546 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32); 4547 // FIXME: Needs another CMP because flag can have but one use. 4548 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN); 4549 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG); 4550 } 4551 return Result; 4552 } 4553 4554 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 4555 /// to morph to an integer compare sequence. 4556 static bool canChangeToInt(SDValue Op, bool &SeenZero, 4557 const ARMSubtarget *Subtarget) { 4558 SDNode *N = Op.getNode(); 4559 if (!N->hasOneUse()) 4560 // Otherwise it requires moving the value from fp to integer registers. 4561 return false; 4562 if (!N->getNumValues()) 4563 return false; 4564 EVT VT = Op.getValueType(); 4565 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 4566 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 4567 // vmrs are very slow, e.g. cortex-a8. 4568 return false; 4569 4570 if (isFloatingPointZero(Op)) { 4571 SeenZero = true; 4572 return true; 4573 } 4574 return ISD::isNormalLoad(N); 4575 } 4576 4577 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 4578 if (isFloatingPointZero(Op)) 4579 return DAG.getConstant(0, SDLoc(Op), MVT::i32); 4580 4581 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 4582 return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(), 4583 Ld->getPointerInfo(), Ld->getAlignment(), 4584 Ld->getMemOperand()->getFlags()); 4585 4586 llvm_unreachable("Unknown VFP cmp argument!"); 4587 } 4588 4589 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 4590 SDValue &RetVal1, SDValue &RetVal2) { 4591 SDLoc dl(Op); 4592 4593 if (isFloatingPointZero(Op)) { 4594 RetVal1 = DAG.getConstant(0, dl, MVT::i32); 4595 RetVal2 = DAG.getConstant(0, dl, MVT::i32); 4596 return; 4597 } 4598 4599 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 4600 SDValue Ptr = Ld->getBasePtr(); 4601 RetVal1 = 4602 DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(), 4603 Ld->getAlignment(), Ld->getMemOperand()->getFlags()); 4604 4605 EVT PtrType = Ptr.getValueType(); 4606 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 4607 SDValue NewPtr = DAG.getNode(ISD::ADD, dl, 4608 PtrType, Ptr, DAG.getConstant(4, dl, PtrType)); 4609 RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr, 4610 Ld->getPointerInfo().getWithOffset(4), NewAlign, 4611 Ld->getMemOperand()->getFlags()); 4612 return; 4613 } 4614 4615 llvm_unreachable("Unknown VFP cmp argument!"); 4616 } 4617 4618 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 4619 /// f32 and even f64 comparisons to integer ones. 4620 SDValue 4621 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 4622 SDValue Chain = Op.getOperand(0); 4623 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 4624 SDValue LHS = Op.getOperand(2); 4625 SDValue RHS = Op.getOperand(3); 4626 SDValue Dest = Op.getOperand(4); 4627 SDLoc dl(Op); 4628 4629 bool LHSSeenZero = false; 4630 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 4631 bool RHSSeenZero = false; 4632 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 4633 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 4634 // If unsafe fp math optimization is enabled and there are no other uses of 4635 // the CMP operands, and the condition code is EQ or NE, we can optimize it 4636 // to an integer comparison. 4637 if (CC == ISD::SETOEQ) 4638 CC = ISD::SETEQ; 4639 else if (CC == ISD::SETUNE) 4640 CC = ISD::SETNE; 4641 4642 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32); 4643 SDValue ARMcc; 4644 if (LHS.getValueType() == MVT::f32) { 4645 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 4646 bitcastf32Toi32(LHS, DAG), Mask); 4647 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 4648 bitcastf32Toi32(RHS, DAG), Mask); 4649 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 4650 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4651 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 4652 Chain, Dest, ARMcc, CCR, Cmp); 4653 } 4654 4655 SDValue LHS1, LHS2; 4656 SDValue RHS1, RHS2; 4657 expandf64Toi32(LHS, DAG, LHS1, LHS2); 4658 expandf64Toi32(RHS, DAG, RHS1, RHS2); 4659 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 4660 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 4661 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 4662 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4663 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 4664 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 4665 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops); 4666 } 4667 4668 return SDValue(); 4669 } 4670 4671 SDValue ARMTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 4672 SDValue Chain = Op.getOperand(0); 4673 SDValue Cond = Op.getOperand(1); 4674 SDValue Dest = Op.getOperand(2); 4675 SDLoc dl(Op); 4676 4677 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 4678 // instruction. 4679 unsigned Opc = Cond.getOpcode(); 4680 bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) && 4681 !Subtarget->isThumb1Only(); 4682 if (Cond.getResNo() == 1 && 4683 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4684 Opc == ISD::USUBO || OptimizeMul)) { 4685 // Only lower legal XALUO ops. 4686 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 4687 return SDValue(); 4688 4689 // The actual operation with overflow check. 4690 SDValue Value, OverflowCmp; 4691 SDValue ARMcc; 4692 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 4693 4694 // Reverse the condition code. 4695 ARMCC::CondCodes CondCode = 4696 (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue(); 4697 CondCode = ARMCC::getOppositeCondition(CondCode); 4698 ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32); 4699 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4700 4701 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR, 4702 OverflowCmp); 4703 } 4704 4705 return SDValue(); 4706 } 4707 4708 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 4709 SDValue Chain = Op.getOperand(0); 4710 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 4711 SDValue LHS = Op.getOperand(2); 4712 SDValue RHS = Op.getOperand(3); 4713 SDValue Dest = Op.getOperand(4); 4714 SDLoc dl(Op); 4715 4716 if (!Subtarget->hasFP64() && LHS.getValueType() == MVT::f64) { 4717 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 4718 dl); 4719 4720 // If softenSetCCOperands only returned one value, we should compare it to 4721 // zero. 4722 if (!RHS.getNode()) { 4723 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 4724 CC = ISD::SETNE; 4725 } 4726 } 4727 4728 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 4729 // instruction. 4730 unsigned Opc = LHS.getOpcode(); 4731 bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) && 4732 !Subtarget->isThumb1Only(); 4733 if (LHS.getResNo() == 1 && (isOneConstant(RHS) || isNullConstant(RHS)) && 4734 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4735 Opc == ISD::USUBO || OptimizeMul) && 4736 (CC == ISD::SETEQ || CC == ISD::SETNE)) { 4737 // Only lower legal XALUO ops. 4738 if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0))) 4739 return SDValue(); 4740 4741 // The actual operation with overflow check. 4742 SDValue Value, OverflowCmp; 4743 SDValue ARMcc; 4744 std::tie(Value, OverflowCmp) = getARMXALUOOp(LHS.getValue(0), DAG, ARMcc); 4745 4746 if ((CC == ISD::SETNE) != isOneConstant(RHS)) { 4747 // Reverse the condition code. 4748 ARMCC::CondCodes CondCode = 4749 (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue(); 4750 CondCode = ARMCC::getOppositeCondition(CondCode); 4751 ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32); 4752 } 4753 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4754 4755 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR, 4756 OverflowCmp); 4757 } 4758 4759 if (LHS.getValueType() == MVT::i32) { 4760 SDValue ARMcc; 4761 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 4762 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4763 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 4764 Chain, Dest, ARMcc, CCR, Cmp); 4765 } 4766 4767 if (getTargetMachine().Options.UnsafeFPMath && 4768 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 4769 CC == ISD::SETNE || CC == ISD::SETUNE)) { 4770 if (SDValue Result = OptimizeVFPBrcond(Op, DAG)) 4771 return Result; 4772 } 4773 4774 ARMCC::CondCodes CondCode, CondCode2; 4775 bool InvalidOnQNaN; 4776 FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN); 4777 4778 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4779 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN); 4780 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4781 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 4782 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 4783 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 4784 if (CondCode2 != ARMCC::AL) { 4785 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32); 4786 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 4787 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 4788 } 4789 return Res; 4790 } 4791 4792 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 4793 SDValue Chain = Op.getOperand(0); 4794 SDValue Table = Op.getOperand(1); 4795 SDValue Index = Op.getOperand(2); 4796 SDLoc dl(Op); 4797 4798 EVT PTy = getPointerTy(DAG.getDataLayout()); 4799 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 4800 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 4801 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI); 4802 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy)); 4803 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Index); 4804 if (Subtarget->isThumb2() || (Subtarget->hasV8MBaselineOps() && Subtarget->isThumb())) { 4805 // Thumb2 and ARMv8-M use a two-level jump. That is, it jumps into the jump table 4806 // which does another jump to the destination. This also makes it easier 4807 // to translate it to TBB / TBH later (Thumb2 only). 4808 // FIXME: This might not work if the function is extremely large. 4809 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 4810 Addr, Op.getOperand(2), JTI); 4811 } 4812 if (isPositionIndependent() || Subtarget->isROPI()) { 4813 Addr = 4814 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 4815 MachinePointerInfo::getJumpTable(DAG.getMachineFunction())); 4816 Chain = Addr.getValue(1); 4817 Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Addr); 4818 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 4819 } else { 4820 Addr = 4821 DAG.getLoad(PTy, dl, Chain, Addr, 4822 MachinePointerInfo::getJumpTable(DAG.getMachineFunction())); 4823 Chain = Addr.getValue(1); 4824 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 4825 } 4826 } 4827 4828 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 4829 EVT VT = Op.getValueType(); 4830 SDLoc dl(Op); 4831 4832 if (Op.getValueType().getVectorElementType() == MVT::i32) { 4833 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 4834 return Op; 4835 return DAG.UnrollVectorOp(Op.getNode()); 4836 } 4837 4838 const bool HasFullFP16 = 4839 static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16(); 4840 4841 EVT NewTy; 4842 const EVT OpTy = Op.getOperand(0).getValueType(); 4843 if (OpTy == MVT::v4f32) 4844 NewTy = MVT::v4i32; 4845 else if (OpTy == MVT::v4f16 && HasFullFP16) 4846 NewTy = MVT::v4i16; 4847 else if (OpTy == MVT::v8f16 && HasFullFP16) 4848 NewTy = MVT::v8i16; 4849 else 4850 llvm_unreachable("Invalid type for custom lowering!"); 4851 4852 if (VT != MVT::v4i16 && VT != MVT::v8i16) 4853 return DAG.UnrollVectorOp(Op.getNode()); 4854 4855 Op = DAG.getNode(Op.getOpcode(), dl, NewTy, Op.getOperand(0)); 4856 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 4857 } 4858 4859 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { 4860 EVT VT = Op.getValueType(); 4861 if (VT.isVector()) 4862 return LowerVectorFP_TO_INT(Op, DAG); 4863 if (!Subtarget->hasFP64() && Op.getOperand(0).getValueType() == MVT::f64) { 4864 RTLIB::Libcall LC; 4865 if (Op.getOpcode() == ISD::FP_TO_SINT) 4866 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), 4867 Op.getValueType()); 4868 else 4869 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), 4870 Op.getValueType()); 4871 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 4872 /*isSigned*/ false, SDLoc(Op)).first; 4873 } 4874 4875 return Op; 4876 } 4877 4878 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 4879 EVT VT = Op.getValueType(); 4880 SDLoc dl(Op); 4881 4882 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 4883 if (VT.getVectorElementType() == MVT::f32) 4884 return Op; 4885 return DAG.UnrollVectorOp(Op.getNode()); 4886 } 4887 4888 assert((Op.getOperand(0).getValueType() == MVT::v4i16 || 4889 Op.getOperand(0).getValueType() == MVT::v8i16) && 4890 "Invalid type for custom lowering!"); 4891 4892 const bool HasFullFP16 = 4893 static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16(); 4894 4895 EVT DestVecType; 4896 if (VT == MVT::v4f32) 4897 DestVecType = MVT::v4i32; 4898 else if (VT == MVT::v4f16 && HasFullFP16) 4899 DestVecType = MVT::v4i16; 4900 else if (VT == MVT::v8f16 && HasFullFP16) 4901 DestVecType = MVT::v8i16; 4902 else 4903 return DAG.UnrollVectorOp(Op.getNode()); 4904 4905 unsigned CastOpc; 4906 unsigned Opc; 4907 switch (Op.getOpcode()) { 4908 default: llvm_unreachable("Invalid opcode!"); 4909 case ISD::SINT_TO_FP: 4910 CastOpc = ISD::SIGN_EXTEND; 4911 Opc = ISD::SINT_TO_FP; 4912 break; 4913 case ISD::UINT_TO_FP: 4914 CastOpc = ISD::ZERO_EXTEND; 4915 Opc = ISD::UINT_TO_FP; 4916 break; 4917 } 4918 4919 Op = DAG.getNode(CastOpc, dl, DestVecType, Op.getOperand(0)); 4920 return DAG.getNode(Opc, dl, VT, Op); 4921 } 4922 4923 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const { 4924 EVT VT = Op.getValueType(); 4925 if (VT.isVector()) 4926 return LowerVectorINT_TO_FP(Op, DAG); 4927 if (!Subtarget->hasFP64() && Op.getValueType() == MVT::f64) { 4928 RTLIB::Libcall LC; 4929 if (Op.getOpcode() == ISD::SINT_TO_FP) 4930 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), 4931 Op.getValueType()); 4932 else 4933 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), 4934 Op.getValueType()); 4935 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 4936 /*isSigned*/ false, SDLoc(Op)).first; 4937 } 4938 4939 return Op; 4940 } 4941 4942 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 4943 // Implement fcopysign with a fabs and a conditional fneg. 4944 SDValue Tmp0 = Op.getOperand(0); 4945 SDValue Tmp1 = Op.getOperand(1); 4946 SDLoc dl(Op); 4947 EVT VT = Op.getValueType(); 4948 EVT SrcVT = Tmp1.getValueType(); 4949 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 4950 Tmp0.getOpcode() == ARMISD::VMOVDRR; 4951 bool UseNEON = !InGPR && Subtarget->hasNEON(); 4952 4953 if (UseNEON) { 4954 // Use VBSL to copy the sign bit. 4955 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 4956 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 4957 DAG.getTargetConstant(EncodedVal, dl, MVT::i32)); 4958 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 4959 if (VT == MVT::f64) 4960 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4961 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 4962 DAG.getConstant(32, dl, MVT::i32)); 4963 else /*if (VT == MVT::f32)*/ 4964 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 4965 if (SrcVT == MVT::f32) { 4966 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 4967 if (VT == MVT::f64) 4968 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4969 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 4970 DAG.getConstant(32, dl, MVT::i32)); 4971 } else if (VT == MVT::f32) 4972 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 4973 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 4974 DAG.getConstant(32, dl, MVT::i32)); 4975 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 4976 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 4977 4978 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 4979 dl, MVT::i32); 4980 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 4981 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 4982 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 4983 4984 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 4985 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 4986 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 4987 if (VT == MVT::f32) { 4988 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 4989 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 4990 DAG.getConstant(0, dl, MVT::i32)); 4991 } else { 4992 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 4993 } 4994 4995 return Res; 4996 } 4997 4998 // Bitcast operand 1 to i32. 4999 if (SrcVT == MVT::f64) 5000 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 5001 Tmp1).getValue(1); 5002 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 5003 5004 // Or in the signbit with integer operations. 5005 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32); 5006 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32); 5007 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 5008 if (VT == MVT::f32) { 5009 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 5010 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 5011 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 5012 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 5013 } 5014 5015 // f64: Or the high part with signbit and then combine two parts. 5016 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 5017 Tmp0); 5018 SDValue Lo = Tmp0.getValue(0); 5019 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 5020 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 5021 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 5022 } 5023 5024 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 5025 MachineFunction &MF = DAG.getMachineFunction(); 5026 MachineFrameInfo &MFI = MF.getFrameInfo(); 5027 MFI.setReturnAddressIsTaken(true); 5028 5029 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 5030 return SDValue(); 5031 5032 EVT VT = Op.getValueType(); 5033 SDLoc dl(Op); 5034 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 5035 if (Depth) { 5036 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 5037 SDValue Offset = DAG.getConstant(4, dl, MVT::i32); 5038 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 5039 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 5040 MachinePointerInfo()); 5041 } 5042 5043 // Return LR, which contains the return address. Mark it an implicit live-in. 5044 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 5045 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 5046 } 5047 5048 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 5049 const ARMBaseRegisterInfo &ARI = 5050 *static_cast<const ARMBaseRegisterInfo*>(RegInfo); 5051 MachineFunction &MF = DAG.getMachineFunction(); 5052 MachineFrameInfo &MFI = MF.getFrameInfo(); 5053 MFI.setFrameAddressIsTaken(true); 5054 5055 EVT VT = Op.getValueType(); 5056 SDLoc dl(Op); // FIXME probably not meaningful 5057 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 5058 unsigned FrameReg = ARI.getFrameRegister(MF); 5059 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 5060 while (Depth--) 5061 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 5062 MachinePointerInfo()); 5063 return FrameAddr; 5064 } 5065 5066 // FIXME? Maybe this could be a TableGen attribute on some registers and 5067 // this table could be generated automatically from RegInfo. 5068 unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT, 5069 SelectionDAG &DAG) const { 5070 unsigned Reg = StringSwitch<unsigned>(RegName) 5071 .Case("sp", ARM::SP) 5072 .Default(0); 5073 if (Reg) 5074 return Reg; 5075 report_fatal_error(Twine("Invalid register name \"" 5076 + StringRef(RegName) + "\".")); 5077 } 5078 5079 // Result is 64 bit value so split into two 32 bit values and return as a 5080 // pair of values. 5081 static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results, 5082 SelectionDAG &DAG) { 5083 SDLoc DL(N); 5084 5085 // This function is only supposed to be called for i64 type destination. 5086 assert(N->getValueType(0) == MVT::i64 5087 && "ExpandREAD_REGISTER called for non-i64 type result."); 5088 5089 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL, 5090 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other), 5091 N->getOperand(0), 5092 N->getOperand(1)); 5093 5094 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0), 5095 Read.getValue(1))); 5096 Results.push_back(Read.getOperand(0)); 5097 } 5098 5099 /// \p BC is a bitcast that is about to be turned into a VMOVDRR. 5100 /// When \p DstVT, the destination type of \p BC, is on the vector 5101 /// register bank and the source of bitcast, \p Op, operates on the same bank, 5102 /// it might be possible to combine them, such that everything stays on the 5103 /// vector register bank. 5104 /// \p return The node that would replace \p BT, if the combine 5105 /// is possible. 5106 static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC, 5107 SelectionDAG &DAG) { 5108 SDValue Op = BC->getOperand(0); 5109 EVT DstVT = BC->getValueType(0); 5110 5111 // The only vector instruction that can produce a scalar (remember, 5112 // since the bitcast was about to be turned into VMOVDRR, the source 5113 // type is i64) from a vector is EXTRACT_VECTOR_ELT. 5114 // Moreover, we can do this combine only if there is one use. 5115 // Finally, if the destination type is not a vector, there is not 5116 // much point on forcing everything on the vector bank. 5117 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 5118 !Op.hasOneUse()) 5119 return SDValue(); 5120 5121 // If the index is not constant, we will introduce an additional 5122 // multiply that will stick. 5123 // Give up in that case. 5124 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 5125 if (!Index) 5126 return SDValue(); 5127 unsigned DstNumElt = DstVT.getVectorNumElements(); 5128 5129 // Compute the new index. 5130 const APInt &APIntIndex = Index->getAPIntValue(); 5131 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt); 5132 NewIndex *= APIntIndex; 5133 // Check if the new constant index fits into i32. 5134 if (NewIndex.getBitWidth() > 32) 5135 return SDValue(); 5136 5137 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) -> 5138 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M) 5139 SDLoc dl(Op); 5140 SDValue ExtractSrc = Op.getOperand(0); 5141 EVT VecVT = EVT::getVectorVT( 5142 *DAG.getContext(), DstVT.getScalarType(), 5143 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt); 5144 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc); 5145 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast, 5146 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32)); 5147 } 5148 5149 /// ExpandBITCAST - If the target supports VFP, this function is called to 5150 /// expand a bit convert where either the source or destination type is i64 to 5151 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 5152 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 5153 /// vectors), since the legalizer won't know what to do with that. 5154 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG, 5155 const ARMSubtarget *Subtarget) { 5156 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 5157 SDLoc dl(N); 5158 SDValue Op = N->getOperand(0); 5159 5160 // This function is only supposed to be called for i64 types, either as the 5161 // source or destination of the bit convert. 5162 EVT SrcVT = Op.getValueType(); 5163 EVT DstVT = N->getValueType(0); 5164 const bool HasFullFP16 = Subtarget->hasFullFP16(); 5165 5166 if (SrcVT == MVT::f32 && DstVT == MVT::i32) { 5167 // FullFP16: half values are passed in S-registers, and we don't 5168 // need any of the bitcast and moves: 5169 // 5170 // t2: f32,ch = CopyFromReg t0, Register:f32 %0 5171 // t5: i32 = bitcast t2 5172 // t18: f16 = ARMISD::VMOVhr t5 5173 if (Op.getOpcode() != ISD::CopyFromReg || 5174 Op.getValueType() != MVT::f32) 5175 return SDValue(); 5176 5177 auto Move = N->use_begin(); 5178 if (Move->getOpcode() != ARMISD::VMOVhr) 5179 return SDValue(); 5180 5181 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; 5182 SDValue Copy = DAG.getNode(ISD::CopyFromReg, SDLoc(Op), MVT::f16, Ops); 5183 DAG.ReplaceAllUsesWith(*Move, &Copy); 5184 return Copy; 5185 } 5186 5187 if (SrcVT == MVT::i16 && DstVT == MVT::f16) { 5188 if (!HasFullFP16) 5189 return SDValue(); 5190 // SoftFP: read half-precision arguments: 5191 // 5192 // t2: i32,ch = ... 5193 // t7: i16 = truncate t2 <~~~~ Op 5194 // t8: f16 = bitcast t7 <~~~~ N 5195 // 5196 if (Op.getOperand(0).getValueType() == MVT::i32) 5197 return DAG.getNode(ARMISD::VMOVhr, SDLoc(Op), 5198 MVT::f16, Op.getOperand(0)); 5199 5200 return SDValue(); 5201 } 5202 5203 // Half-precision return values 5204 if (SrcVT == MVT::f16 && DstVT == MVT::i16) { 5205 if (!HasFullFP16) 5206 return SDValue(); 5207 // 5208 // t11: f16 = fadd t8, t10 5209 // t12: i16 = bitcast t11 <~~~ SDNode N 5210 // t13: i32 = zero_extend t12 5211 // t16: ch,glue = CopyToReg t0, Register:i32 %r0, t13 5212 // t17: ch = ARMISD::RET_FLAG t16, Register:i32 %r0, t16:1 5213 // 5214 // transform this into: 5215 // 5216 // t20: i32 = ARMISD::VMOVrh t11 5217 // t16: ch,glue = CopyToReg t0, Register:i32 %r0, t20 5218 // 5219 auto ZeroExtend = N->use_begin(); 5220 if (N->use_size() != 1 || ZeroExtend->getOpcode() != ISD::ZERO_EXTEND || 5221 ZeroExtend->getValueType(0) != MVT::i32) 5222 return SDValue(); 5223 5224 auto Copy = ZeroExtend->use_begin(); 5225 if (Copy->getOpcode() == ISD::CopyToReg && 5226 Copy->use_begin()->getOpcode() == ARMISD::RET_FLAG) { 5227 SDValue Cvt = DAG.getNode(ARMISD::VMOVrh, SDLoc(Op), MVT::i32, Op); 5228 DAG.ReplaceAllUsesWith(*ZeroExtend, &Cvt); 5229 return Cvt; 5230 } 5231 return SDValue(); 5232 } 5233 5234 if (!(SrcVT == MVT::i64 || DstVT == MVT::i64)) 5235 return SDValue(); 5236 5237 // Turn i64->f64 into VMOVDRR. 5238 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 5239 // Do not force values to GPRs (this is what VMOVDRR does for the inputs) 5240 // if we can combine the bitcast with its source. 5241 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG)) 5242 return Val; 5243 5244 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 5245 DAG.getConstant(0, dl, MVT::i32)); 5246 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 5247 DAG.getConstant(1, dl, MVT::i32)); 5248 return DAG.getNode(ISD::BITCAST, dl, DstVT, 5249 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 5250 } 5251 5252 // Turn f64->i64 into VMOVRRD. 5253 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 5254 SDValue Cvt; 5255 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() && 5256 SrcVT.getVectorNumElements() > 1) 5257 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 5258 DAG.getVTList(MVT::i32, MVT::i32), 5259 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op)); 5260 else 5261 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 5262 DAG.getVTList(MVT::i32, MVT::i32), Op); 5263 // Merge the pieces into a single i64 value. 5264 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 5265 } 5266 5267 return SDValue(); 5268 } 5269 5270 /// getZeroVector - Returns a vector of specified type with all zero elements. 5271 /// Zero vectors are used to represent vector negation and in those cases 5272 /// will be implemented with the NEON VNEG instruction. However, VNEG does 5273 /// not support i64 elements, so sometimes the zero vectors will need to be 5274 /// explicitly constructed. Regardless, use a canonical VMOV to create the 5275 /// zero vector. 5276 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) { 5277 assert(VT.isVector() && "Expected a vector type"); 5278 // The canonical modified immediate encoding of a zero vector is....0! 5279 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32); 5280 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 5281 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 5282 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5283 } 5284 5285 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 5286 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 5287 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 5288 SelectionDAG &DAG) const { 5289 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 5290 EVT VT = Op.getValueType(); 5291 unsigned VTBits = VT.getSizeInBits(); 5292 SDLoc dl(Op); 5293 SDValue ShOpLo = Op.getOperand(0); 5294 SDValue ShOpHi = Op.getOperand(1); 5295 SDValue ShAmt = Op.getOperand(2); 5296 SDValue ARMcc; 5297 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5298 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 5299 5300 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 5301 5302 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 5303 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 5304 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 5305 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 5306 DAG.getConstant(VTBits, dl, MVT::i32)); 5307 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 5308 SDValue LoSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 5309 SDValue LoBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 5310 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5311 ISD::SETGE, ARMcc, DAG, dl); 5312 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, LoBigShift, 5313 ARMcc, CCR, CmpLo); 5314 5315 SDValue HiSmallShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 5316 SDValue HiBigShift = Opc == ISD::SRA 5317 ? DAG.getNode(Opc, dl, VT, ShOpHi, 5318 DAG.getConstant(VTBits - 1, dl, VT)) 5319 : DAG.getConstant(0, dl, VT); 5320 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5321 ISD::SETGE, ARMcc, DAG, dl); 5322 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift, 5323 ARMcc, CCR, CmpHi); 5324 5325 SDValue Ops[2] = { Lo, Hi }; 5326 return DAG.getMergeValues(Ops, dl); 5327 } 5328 5329 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 5330 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 5331 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 5332 SelectionDAG &DAG) const { 5333 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 5334 EVT VT = Op.getValueType(); 5335 unsigned VTBits = VT.getSizeInBits(); 5336 SDLoc dl(Op); 5337 SDValue ShOpLo = Op.getOperand(0); 5338 SDValue ShOpHi = Op.getOperand(1); 5339 SDValue ShAmt = Op.getOperand(2); 5340 SDValue ARMcc; 5341 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5342 5343 assert(Op.getOpcode() == ISD::SHL_PARTS); 5344 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 5345 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 5346 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 5347 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 5348 SDValue HiSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 5349 5350 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 5351 DAG.getConstant(VTBits, dl, MVT::i32)); 5352 SDValue HiBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 5353 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5354 ISD::SETGE, ARMcc, DAG, dl); 5355 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift, 5356 ARMcc, CCR, CmpHi); 5357 5358 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5359 ISD::SETGE, ARMcc, DAG, dl); 5360 SDValue LoSmallShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 5361 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, 5362 DAG.getConstant(0, dl, VT), ARMcc, CCR, CmpLo); 5363 5364 SDValue Ops[2] = { Lo, Hi }; 5365 return DAG.getMergeValues(Ops, dl); 5366 } 5367 5368 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 5369 SelectionDAG &DAG) const { 5370 // The rounding mode is in bits 23:22 of the FPSCR. 5371 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 5372 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 5373 // so that the shift + and get folded into a bitfield extract. 5374 SDLoc dl(Op); 5375 SDValue Ops[] = { DAG.getEntryNode(), 5376 DAG.getConstant(Intrinsic::arm_get_fpscr, dl, MVT::i32) }; 5377 5378 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_W_CHAIN, dl, MVT::i32, Ops); 5379 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 5380 DAG.getConstant(1U << 22, dl, MVT::i32)); 5381 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 5382 DAG.getConstant(22, dl, MVT::i32)); 5383 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 5384 DAG.getConstant(3, dl, MVT::i32)); 5385 } 5386 5387 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 5388 const ARMSubtarget *ST) { 5389 SDLoc dl(N); 5390 EVT VT = N->getValueType(0); 5391 if (VT.isVector()) { 5392 assert(ST->hasNEON()); 5393 5394 // Compute the least significant set bit: LSB = X & -X 5395 SDValue X = N->getOperand(0); 5396 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X); 5397 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX); 5398 5399 EVT ElemTy = VT.getVectorElementType(); 5400 5401 if (ElemTy == MVT::i8) { 5402 // Compute with: cttz(x) = ctpop(lsb - 1) 5403 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5404 DAG.getTargetConstant(1, dl, ElemTy)); 5405 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 5406 return DAG.getNode(ISD::CTPOP, dl, VT, Bits); 5407 } 5408 5409 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) && 5410 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) { 5411 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0 5412 unsigned NumBits = ElemTy.getSizeInBits(); 5413 SDValue WidthMinus1 = 5414 DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5415 DAG.getTargetConstant(NumBits - 1, dl, ElemTy)); 5416 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB); 5417 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ); 5418 } 5419 5420 // Compute with: cttz(x) = ctpop(lsb - 1) 5421 5422 // Compute LSB - 1. 5423 SDValue Bits; 5424 if (ElemTy == MVT::i64) { 5425 // Load constant 0xffff'ffff'ffff'ffff to register. 5426 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5427 DAG.getTargetConstant(0x1eff, dl, MVT::i32)); 5428 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF); 5429 } else { 5430 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5431 DAG.getTargetConstant(1, dl, ElemTy)); 5432 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 5433 } 5434 return DAG.getNode(ISD::CTPOP, dl, VT, Bits); 5435 } 5436 5437 if (!ST->hasV6T2Ops()) 5438 return SDValue(); 5439 5440 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0)); 5441 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 5442 } 5443 5444 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 5445 const ARMSubtarget *ST) { 5446 EVT VT = N->getValueType(0); 5447 SDLoc DL(N); 5448 5449 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 5450 assert((VT == MVT::v1i64 || VT == MVT::v2i64 || VT == MVT::v2i32 || 5451 VT == MVT::v4i32 || VT == MVT::v4i16 || VT == MVT::v8i16) && 5452 "Unexpected type for custom ctpop lowering"); 5453 5454 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 5455 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 5456 SDValue Res = DAG.getBitcast(VT8Bit, N->getOperand(0)); 5457 Res = DAG.getNode(ISD::CTPOP, DL, VT8Bit, Res); 5458 5459 // Widen v8i8/v16i8 CTPOP result to VT by repeatedly widening pairwise adds. 5460 unsigned EltSize = 8; 5461 unsigned NumElts = VT.is64BitVector() ? 8 : 16; 5462 while (EltSize != VT.getScalarSizeInBits()) { 5463 SmallVector<SDValue, 8> Ops; 5464 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddlu, DL, 5465 TLI.getPointerTy(DAG.getDataLayout()))); 5466 Ops.push_back(Res); 5467 5468 EltSize *= 2; 5469 NumElts /= 2; 5470 MVT WidenVT = MVT::getVectorVT(MVT::getIntegerVT(EltSize), NumElts); 5471 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, WidenVT, Ops); 5472 } 5473 5474 return Res; 5475 } 5476 5477 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 5478 const ARMSubtarget *ST) { 5479 EVT VT = N->getValueType(0); 5480 SDLoc dl(N); 5481 5482 if (!VT.isVector()) 5483 return SDValue(); 5484 5485 // Lower vector shifts on NEON to use VSHL. 5486 assert(ST->hasNEON() && "unexpected vector shift"); 5487 5488 // Left shifts translate directly to the vshiftu intrinsic. 5489 if (N->getOpcode() == ISD::SHL) 5490 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 5491 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl, 5492 MVT::i32), 5493 N->getOperand(0), N->getOperand(1)); 5494 5495 assert((N->getOpcode() == ISD::SRA || 5496 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 5497 5498 // NEON uses the same intrinsics for both left and right shifts. For 5499 // right shifts, the shift amounts are negative, so negate the vector of 5500 // shift amounts. 5501 EVT ShiftVT = N->getOperand(1).getValueType(); 5502 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 5503 getZeroVector(ShiftVT, DAG, dl), 5504 N->getOperand(1)); 5505 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 5506 Intrinsic::arm_neon_vshifts : 5507 Intrinsic::arm_neon_vshiftu); 5508 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 5509 DAG.getConstant(vshiftInt, dl, MVT::i32), 5510 N->getOperand(0), NegatedCount); 5511 } 5512 5513 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 5514 const ARMSubtarget *ST) { 5515 EVT VT = N->getValueType(0); 5516 SDLoc dl(N); 5517 5518 // We can get here for a node like i32 = ISD::SHL i32, i64 5519 if (VT != MVT::i64) 5520 return SDValue(); 5521 5522 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 5523 "Unknown shift to lower!"); 5524 5525 // We only lower SRA, SRL of 1 here, all others use generic lowering. 5526 if (!isOneConstant(N->getOperand(1))) 5527 return SDValue(); 5528 5529 // If we are in thumb mode, we don't have RRX. 5530 if (ST->isThumb1Only()) return SDValue(); 5531 5532 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 5533 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 5534 DAG.getConstant(0, dl, MVT::i32)); 5535 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 5536 DAG.getConstant(1, dl, MVT::i32)); 5537 5538 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 5539 // captures the result into a carry flag. 5540 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 5541 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi); 5542 5543 // The low part is an ARMISD::RRX operand, which shifts the carry in. 5544 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 5545 5546 // Merge the pieces into a single i64 value. 5547 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 5548 } 5549 5550 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 5551 SDValue TmpOp0, TmpOp1; 5552 bool Invert = false; 5553 bool Swap = false; 5554 unsigned Opc = 0; 5555 5556 SDValue Op0 = Op.getOperand(0); 5557 SDValue Op1 = Op.getOperand(1); 5558 SDValue CC = Op.getOperand(2); 5559 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger(); 5560 EVT VT = Op.getValueType(); 5561 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 5562 SDLoc dl(Op); 5563 5564 if (Op0.getValueType().getVectorElementType() == MVT::i64 && 5565 (SetCCOpcode == ISD::SETEQ || SetCCOpcode == ISD::SETNE)) { 5566 // Special-case integer 64-bit equality comparisons. They aren't legal, 5567 // but they can be lowered with a few vector instructions. 5568 unsigned CmpElements = CmpVT.getVectorNumElements() * 2; 5569 EVT SplitVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, CmpElements); 5570 SDValue CastOp0 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op0); 5571 SDValue CastOp1 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op1); 5572 SDValue Cmp = DAG.getNode(ISD::SETCC, dl, SplitVT, CastOp0, CastOp1, 5573 DAG.getCondCode(ISD::SETEQ)); 5574 SDValue Reversed = DAG.getNode(ARMISD::VREV64, dl, SplitVT, Cmp); 5575 SDValue Merged = DAG.getNode(ISD::AND, dl, SplitVT, Cmp, Reversed); 5576 Merged = DAG.getNode(ISD::BITCAST, dl, CmpVT, Merged); 5577 if (SetCCOpcode == ISD::SETNE) 5578 Merged = DAG.getNOT(dl, Merged, CmpVT); 5579 Merged = DAG.getSExtOrTrunc(Merged, dl, VT); 5580 return Merged; 5581 } 5582 5583 if (CmpVT.getVectorElementType() == MVT::i64) 5584 // 64-bit comparisons are not legal in general. 5585 return SDValue(); 5586 5587 if (Op1.getValueType().isFloatingPoint()) { 5588 switch (SetCCOpcode) { 5589 default: llvm_unreachable("Illegal FP comparison"); 5590 case ISD::SETUNE: 5591 case ISD::SETNE: Invert = true; LLVM_FALLTHROUGH; 5592 case ISD::SETOEQ: 5593 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 5594 case ISD::SETOLT: 5595 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH; 5596 case ISD::SETOGT: 5597 case ISD::SETGT: Opc = ARMISD::VCGT; break; 5598 case ISD::SETOLE: 5599 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH; 5600 case ISD::SETOGE: 5601 case ISD::SETGE: Opc = ARMISD::VCGE; break; 5602 case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH; 5603 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 5604 case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH; 5605 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 5606 case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH; 5607 case ISD::SETONE: 5608 // Expand this to (OLT | OGT). 5609 TmpOp0 = Op0; 5610 TmpOp1 = Op1; 5611 Opc = ISD::OR; 5612 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 5613 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1); 5614 break; 5615 case ISD::SETUO: 5616 Invert = true; 5617 LLVM_FALLTHROUGH; 5618 case ISD::SETO: 5619 // Expand this to (OLT | OGE). 5620 TmpOp0 = Op0; 5621 TmpOp1 = Op1; 5622 Opc = ISD::OR; 5623 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 5624 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1); 5625 break; 5626 } 5627 } else { 5628 // Integer comparisons. 5629 switch (SetCCOpcode) { 5630 default: llvm_unreachable("Illegal integer comparison"); 5631 case ISD::SETNE: Invert = true; LLVM_FALLTHROUGH; 5632 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 5633 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH; 5634 case ISD::SETGT: Opc = ARMISD::VCGT; break; 5635 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH; 5636 case ISD::SETGE: Opc = ARMISD::VCGE; break; 5637 case ISD::SETULT: Swap = true; LLVM_FALLTHROUGH; 5638 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 5639 case ISD::SETULE: Swap = true; LLVM_FALLTHROUGH; 5640 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 5641 } 5642 5643 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 5644 if (Opc == ARMISD::VCEQ) { 5645 SDValue AndOp; 5646 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 5647 AndOp = Op0; 5648 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 5649 AndOp = Op1; 5650 5651 // Ignore bitconvert. 5652 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 5653 AndOp = AndOp.getOperand(0); 5654 5655 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 5656 Opc = ARMISD::VTST; 5657 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0)); 5658 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1)); 5659 Invert = !Invert; 5660 } 5661 } 5662 } 5663 5664 if (Swap) 5665 std::swap(Op0, Op1); 5666 5667 // If one of the operands is a constant vector zero, attempt to fold the 5668 // comparison to a specialized compare-against-zero form. 5669 SDValue SingleOp; 5670 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 5671 SingleOp = Op0; 5672 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 5673 if (Opc == ARMISD::VCGE) 5674 Opc = ARMISD::VCLEZ; 5675 else if (Opc == ARMISD::VCGT) 5676 Opc = ARMISD::VCLTZ; 5677 SingleOp = Op1; 5678 } 5679 5680 SDValue Result; 5681 if (SingleOp.getNode()) { 5682 switch (Opc) { 5683 case ARMISD::VCEQ: 5684 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break; 5685 case ARMISD::VCGE: 5686 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break; 5687 case ARMISD::VCLEZ: 5688 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break; 5689 case ARMISD::VCGT: 5690 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break; 5691 case ARMISD::VCLTZ: 5692 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break; 5693 default: 5694 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 5695 } 5696 } else { 5697 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 5698 } 5699 5700 Result = DAG.getSExtOrTrunc(Result, dl, VT); 5701 5702 if (Invert) 5703 Result = DAG.getNOT(dl, Result, VT); 5704 5705 return Result; 5706 } 5707 5708 static SDValue LowerSETCCCARRY(SDValue Op, SelectionDAG &DAG) { 5709 SDValue LHS = Op.getOperand(0); 5710 SDValue RHS = Op.getOperand(1); 5711 SDValue Carry = Op.getOperand(2); 5712 SDValue Cond = Op.getOperand(3); 5713 SDLoc DL(Op); 5714 5715 assert(LHS.getSimpleValueType().isInteger() && "SETCCCARRY is integer only."); 5716 5717 // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we 5718 // have to invert the carry first. 5719 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 5720 DAG.getConstant(1, DL, MVT::i32), Carry); 5721 // This converts the boolean value carry into the carry flag. 5722 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 5723 5724 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); 5725 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry); 5726 5727 SDValue FVal = DAG.getConstant(0, DL, MVT::i32); 5728 SDValue TVal = DAG.getConstant(1, DL, MVT::i32); 5729 SDValue ARMcc = DAG.getConstant( 5730 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32); 5731 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5732 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR, 5733 Cmp.getValue(1), SDValue()); 5734 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc, 5735 CCR, Chain.getValue(1)); 5736 } 5737 5738 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 5739 /// valid vector constant for a NEON instruction with a "modified immediate" 5740 /// operand (e.g., VMOV). If so, return the encoded value. 5741 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 5742 unsigned SplatBitSize, SelectionDAG &DAG, 5743 const SDLoc &dl, EVT &VT, bool is128Bits, 5744 NEONModImmType type) { 5745 unsigned OpCmode, Imm; 5746 5747 // SplatBitSize is set to the smallest size that splats the vector, so a 5748 // zero vector will always have SplatBitSize == 8. However, NEON modified 5749 // immediate instructions others than VMOV do not support the 8-bit encoding 5750 // of a zero vector, and the default encoding of zero is supposed to be the 5751 // 32-bit version. 5752 if (SplatBits == 0) 5753 SplatBitSize = 32; 5754 5755 switch (SplatBitSize) { 5756 case 8: 5757 if (type != VMOVModImm) 5758 return SDValue(); 5759 // Any 1-byte value is OK. Op=0, Cmode=1110. 5760 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 5761 OpCmode = 0xe; 5762 Imm = SplatBits; 5763 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 5764 break; 5765 5766 case 16: 5767 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 5768 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 5769 if ((SplatBits & ~0xff) == 0) { 5770 // Value = 0x00nn: Op=x, Cmode=100x. 5771 OpCmode = 0x8; 5772 Imm = SplatBits; 5773 break; 5774 } 5775 if ((SplatBits & ~0xff00) == 0) { 5776 // Value = 0xnn00: Op=x, Cmode=101x. 5777 OpCmode = 0xa; 5778 Imm = SplatBits >> 8; 5779 break; 5780 } 5781 return SDValue(); 5782 5783 case 32: 5784 // NEON's 32-bit VMOV supports splat values where: 5785 // * only one byte is nonzero, or 5786 // * the least significant byte is 0xff and the second byte is nonzero, or 5787 // * the least significant 2 bytes are 0xff and the third is nonzero. 5788 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 5789 if ((SplatBits & ~0xff) == 0) { 5790 // Value = 0x000000nn: Op=x, Cmode=000x. 5791 OpCmode = 0; 5792 Imm = SplatBits; 5793 break; 5794 } 5795 if ((SplatBits & ~0xff00) == 0) { 5796 // Value = 0x0000nn00: Op=x, Cmode=001x. 5797 OpCmode = 0x2; 5798 Imm = SplatBits >> 8; 5799 break; 5800 } 5801 if ((SplatBits & ~0xff0000) == 0) { 5802 // Value = 0x00nn0000: Op=x, Cmode=010x. 5803 OpCmode = 0x4; 5804 Imm = SplatBits >> 16; 5805 break; 5806 } 5807 if ((SplatBits & ~0xff000000) == 0) { 5808 // Value = 0xnn000000: Op=x, Cmode=011x. 5809 OpCmode = 0x6; 5810 Imm = SplatBits >> 24; 5811 break; 5812 } 5813 5814 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 5815 if (type == OtherModImm) return SDValue(); 5816 5817 if ((SplatBits & ~0xffff) == 0 && 5818 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 5819 // Value = 0x0000nnff: Op=x, Cmode=1100. 5820 OpCmode = 0xc; 5821 Imm = SplatBits >> 8; 5822 break; 5823 } 5824 5825 if ((SplatBits & ~0xffffff) == 0 && 5826 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 5827 // Value = 0x00nnffff: Op=x, Cmode=1101. 5828 OpCmode = 0xd; 5829 Imm = SplatBits >> 16; 5830 break; 5831 } 5832 5833 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 5834 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 5835 // VMOV.I32. A (very) minor optimization would be to replicate the value 5836 // and fall through here to test for a valid 64-bit splat. But, then the 5837 // caller would also need to check and handle the change in size. 5838 return SDValue(); 5839 5840 case 64: { 5841 if (type != VMOVModImm) 5842 return SDValue(); 5843 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 5844 uint64_t BitMask = 0xff; 5845 uint64_t Val = 0; 5846 unsigned ImmMask = 1; 5847 Imm = 0; 5848 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 5849 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 5850 Val |= BitMask; 5851 Imm |= ImmMask; 5852 } else if ((SplatBits & BitMask) != 0) { 5853 return SDValue(); 5854 } 5855 BitMask <<= 8; 5856 ImmMask <<= 1; 5857 } 5858 5859 if (DAG.getDataLayout().isBigEndian()) 5860 // swap higher and lower 32 bit word 5861 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4); 5862 5863 // Op=1, Cmode=1110. 5864 OpCmode = 0x1e; 5865 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 5866 break; 5867 } 5868 5869 default: 5870 llvm_unreachable("unexpected size for isNEONModifiedImm"); 5871 } 5872 5873 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 5874 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32); 5875 } 5876 5877 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 5878 const ARMSubtarget *ST) const { 5879 EVT VT = Op.getValueType(); 5880 bool IsDouble = (VT == MVT::f64); 5881 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 5882 const APFloat &FPVal = CFP->getValueAPF(); 5883 5884 // Prevent floating-point constants from using literal loads 5885 // when execute-only is enabled. 5886 if (ST->genExecuteOnly()) { 5887 // If we can represent the constant as an immediate, don't lower it 5888 if (isFPImmLegal(FPVal, VT)) 5889 return Op; 5890 // Otherwise, construct as integer, and move to float register 5891 APInt INTVal = FPVal.bitcastToAPInt(); 5892 SDLoc DL(CFP); 5893 switch (VT.getSimpleVT().SimpleTy) { 5894 default: 5895 llvm_unreachable("Unknown floating point type!"); 5896 break; 5897 case MVT::f64: { 5898 SDValue Lo = DAG.getConstant(INTVal.trunc(32), DL, MVT::i32); 5899 SDValue Hi = DAG.getConstant(INTVal.lshr(32).trunc(32), DL, MVT::i32); 5900 if (!ST->isLittle()) 5901 std::swap(Lo, Hi); 5902 return DAG.getNode(ARMISD::VMOVDRR, DL, MVT::f64, Lo, Hi); 5903 } 5904 case MVT::f32: 5905 return DAG.getNode(ARMISD::VMOVSR, DL, VT, 5906 DAG.getConstant(INTVal, DL, MVT::i32)); 5907 } 5908 } 5909 5910 if (!ST->hasVFP3Base()) 5911 return SDValue(); 5912 5913 // Use the default (constant pool) lowering for double constants when we have 5914 // an SP-only FPU 5915 if (IsDouble && !Subtarget->hasFP64()) 5916 return SDValue(); 5917 5918 // Try splatting with a VMOV.f32... 5919 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 5920 5921 if (ImmVal != -1) { 5922 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 5923 // We have code in place to select a valid ConstantFP already, no need to 5924 // do any mangling. 5925 return Op; 5926 } 5927 5928 // It's a float and we are trying to use NEON operations where 5929 // possible. Lower it to a splat followed by an extract. 5930 SDLoc DL(Op); 5931 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32); 5932 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 5933 NewVal); 5934 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 5935 DAG.getConstant(0, DL, MVT::i32)); 5936 } 5937 5938 // The rest of our options are NEON only, make sure that's allowed before 5939 // proceeding.. 5940 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 5941 return SDValue(); 5942 5943 EVT VMovVT; 5944 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 5945 5946 // It wouldn't really be worth bothering for doubles except for one very 5947 // important value, which does happen to match: 0.0. So make sure we don't do 5948 // anything stupid. 5949 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 5950 return SDValue(); 5951 5952 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 5953 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), 5954 VMovVT, false, VMOVModImm); 5955 if (NewVal != SDValue()) { 5956 SDLoc DL(Op); 5957 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 5958 NewVal); 5959 if (IsDouble) 5960 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 5961 5962 // It's a float: cast and extract a vector element. 5963 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 5964 VecConstant); 5965 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 5966 DAG.getConstant(0, DL, MVT::i32)); 5967 } 5968 5969 // Finally, try a VMVN.i32 5970 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT, 5971 false, VMVNModImm); 5972 if (NewVal != SDValue()) { 5973 SDLoc DL(Op); 5974 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 5975 5976 if (IsDouble) 5977 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 5978 5979 // It's a float: cast and extract a vector element. 5980 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 5981 VecConstant); 5982 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 5983 DAG.getConstant(0, DL, MVT::i32)); 5984 } 5985 5986 return SDValue(); 5987 } 5988 5989 // check if an VEXT instruction can handle the shuffle mask when the 5990 // vector sources of the shuffle are the same. 5991 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 5992 unsigned NumElts = VT.getVectorNumElements(); 5993 5994 // Assume that the first shuffle index is not UNDEF. Fail if it is. 5995 if (M[0] < 0) 5996 return false; 5997 5998 Imm = M[0]; 5999 6000 // If this is a VEXT shuffle, the immediate value is the index of the first 6001 // element. The other shuffle indices must be the successive elements after 6002 // the first one. 6003 unsigned ExpectedElt = Imm; 6004 for (unsigned i = 1; i < NumElts; ++i) { 6005 // Increment the expected index. If it wraps around, just follow it 6006 // back to index zero and keep going. 6007 ++ExpectedElt; 6008 if (ExpectedElt == NumElts) 6009 ExpectedElt = 0; 6010 6011 if (M[i] < 0) continue; // ignore UNDEF indices 6012 if (ExpectedElt != static_cast<unsigned>(M[i])) 6013 return false; 6014 } 6015 6016 return true; 6017 } 6018 6019 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 6020 bool &ReverseVEXT, unsigned &Imm) { 6021 unsigned NumElts = VT.getVectorNumElements(); 6022 ReverseVEXT = false; 6023 6024 // Assume that the first shuffle index is not UNDEF. Fail if it is. 6025 if (M[0] < 0) 6026 return false; 6027 6028 Imm = M[0]; 6029 6030 // If this is a VEXT shuffle, the immediate value is the index of the first 6031 // element. The other shuffle indices must be the successive elements after 6032 // the first one. 6033 unsigned ExpectedElt = Imm; 6034 for (unsigned i = 1; i < NumElts; ++i) { 6035 // Increment the expected index. If it wraps around, it may still be 6036 // a VEXT but the source vectors must be swapped. 6037 ExpectedElt += 1; 6038 if (ExpectedElt == NumElts * 2) { 6039 ExpectedElt = 0; 6040 ReverseVEXT = true; 6041 } 6042 6043 if (M[i] < 0) continue; // ignore UNDEF indices 6044 if (ExpectedElt != static_cast<unsigned>(M[i])) 6045 return false; 6046 } 6047 6048 // Adjust the index value if the source operands will be swapped. 6049 if (ReverseVEXT) 6050 Imm -= NumElts; 6051 6052 return true; 6053 } 6054 6055 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 6056 /// instruction with the specified blocksize. (The order of the elements 6057 /// within each block of the vector is reversed.) 6058 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 6059 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 6060 "Only possible block sizes for VREV are: 16, 32, 64"); 6061 6062 unsigned EltSz = VT.getScalarSizeInBits(); 6063 if (EltSz == 64) 6064 return false; 6065 6066 unsigned NumElts = VT.getVectorNumElements(); 6067 unsigned BlockElts = M[0] + 1; 6068 // If the first shuffle index is UNDEF, be optimistic. 6069 if (M[0] < 0) 6070 BlockElts = BlockSize / EltSz; 6071 6072 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 6073 return false; 6074 6075 for (unsigned i = 0; i < NumElts; ++i) { 6076 if (M[i] < 0) continue; // ignore UNDEF indices 6077 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 6078 return false; 6079 } 6080 6081 return true; 6082 } 6083 6084 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 6085 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 6086 // range, then 0 is placed into the resulting vector. So pretty much any mask 6087 // of 8 elements can work here. 6088 return VT == MVT::v8i8 && M.size() == 8; 6089 } 6090 6091 static unsigned SelectPairHalf(unsigned Elements, ArrayRef<int> Mask, 6092 unsigned Index) { 6093 if (Mask.size() == Elements * 2) 6094 return Index / Elements; 6095 return Mask[Index] == 0 ? 0 : 1; 6096 } 6097 6098 // Checks whether the shuffle mask represents a vector transpose (VTRN) by 6099 // checking that pairs of elements in the shuffle mask represent the same index 6100 // in each vector, incrementing the expected index by 2 at each step. 6101 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6] 6102 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g} 6103 // v2={e,f,g,h} 6104 // WhichResult gives the offset for each element in the mask based on which 6105 // of the two results it belongs to. 6106 // 6107 // The transpose can be represented either as: 6108 // result1 = shufflevector v1, v2, result1_shuffle_mask 6109 // result2 = shufflevector v1, v2, result2_shuffle_mask 6110 // where v1/v2 and the shuffle masks have the same number of elements 6111 // (here WhichResult (see below) indicates which result is being checked) 6112 // 6113 // or as: 6114 // results = shufflevector v1, v2, shuffle_mask 6115 // where both results are returned in one vector and the shuffle mask has twice 6116 // as many elements as v1/v2 (here WhichResult will always be 0 if true) here we 6117 // want to check the low half and high half of the shuffle mask as if it were 6118 // the other case 6119 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6120 unsigned EltSz = VT.getScalarSizeInBits(); 6121 if (EltSz == 64) 6122 return false; 6123 6124 unsigned NumElts = VT.getVectorNumElements(); 6125 if (M.size() != NumElts && M.size() != NumElts*2) 6126 return false; 6127 6128 // If the mask is twice as long as the input vector then we need to check the 6129 // upper and lower parts of the mask with a matching value for WhichResult 6130 // FIXME: A mask with only even values will be rejected in case the first 6131 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only 6132 // M[0] is used to determine WhichResult 6133 for (unsigned i = 0; i < M.size(); i += NumElts) { 6134 WhichResult = SelectPairHalf(NumElts, M, i); 6135 for (unsigned j = 0; j < NumElts; j += 2) { 6136 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 6137 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult)) 6138 return false; 6139 } 6140 } 6141 6142 if (M.size() == NumElts*2) 6143 WhichResult = 0; 6144 6145 return true; 6146 } 6147 6148 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 6149 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6150 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 6151 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6152 unsigned EltSz = VT.getScalarSizeInBits(); 6153 if (EltSz == 64) 6154 return false; 6155 6156 unsigned NumElts = VT.getVectorNumElements(); 6157 if (M.size() != NumElts && M.size() != NumElts*2) 6158 return false; 6159 6160 for (unsigned i = 0; i < M.size(); i += NumElts) { 6161 WhichResult = SelectPairHalf(NumElts, M, i); 6162 for (unsigned j = 0; j < NumElts; j += 2) { 6163 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 6164 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult)) 6165 return false; 6166 } 6167 } 6168 6169 if (M.size() == NumElts*2) 6170 WhichResult = 0; 6171 6172 return true; 6173 } 6174 6175 // Checks whether the shuffle mask represents a vector unzip (VUZP) by checking 6176 // that the mask elements are either all even and in steps of size 2 or all odd 6177 // and in steps of size 2. 6178 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6] 6179 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g} 6180 // v2={e,f,g,h} 6181 // Requires similar checks to that of isVTRNMask with 6182 // respect the how results are returned. 6183 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6184 unsigned EltSz = VT.getScalarSizeInBits(); 6185 if (EltSz == 64) 6186 return false; 6187 6188 unsigned NumElts = VT.getVectorNumElements(); 6189 if (M.size() != NumElts && M.size() != NumElts*2) 6190 return false; 6191 6192 for (unsigned i = 0; i < M.size(); i += NumElts) { 6193 WhichResult = SelectPairHalf(NumElts, M, i); 6194 for (unsigned j = 0; j < NumElts; ++j) { 6195 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult) 6196 return false; 6197 } 6198 } 6199 6200 if (M.size() == NumElts*2) 6201 WhichResult = 0; 6202 6203 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6204 if (VT.is64BitVector() && EltSz == 32) 6205 return false; 6206 6207 return true; 6208 } 6209 6210 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 6211 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6212 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 6213 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6214 unsigned EltSz = VT.getScalarSizeInBits(); 6215 if (EltSz == 64) 6216 return false; 6217 6218 unsigned NumElts = VT.getVectorNumElements(); 6219 if (M.size() != NumElts && M.size() != NumElts*2) 6220 return false; 6221 6222 unsigned Half = NumElts / 2; 6223 for (unsigned i = 0; i < M.size(); i += NumElts) { 6224 WhichResult = SelectPairHalf(NumElts, M, i); 6225 for (unsigned j = 0; j < NumElts; j += Half) { 6226 unsigned Idx = WhichResult; 6227 for (unsigned k = 0; k < Half; ++k) { 6228 int MIdx = M[i + j + k]; 6229 if (MIdx >= 0 && (unsigned) MIdx != Idx) 6230 return false; 6231 Idx += 2; 6232 } 6233 } 6234 } 6235 6236 if (M.size() == NumElts*2) 6237 WhichResult = 0; 6238 6239 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6240 if (VT.is64BitVector() && EltSz == 32) 6241 return false; 6242 6243 return true; 6244 } 6245 6246 // Checks whether the shuffle mask represents a vector zip (VZIP) by checking 6247 // that pairs of elements of the shufflemask represent the same index in each 6248 // vector incrementing sequentially through the vectors. 6249 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5] 6250 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f} 6251 // v2={e,f,g,h} 6252 // Requires similar checks to that of isVTRNMask with respect the how results 6253 // are returned. 6254 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6255 unsigned EltSz = VT.getScalarSizeInBits(); 6256 if (EltSz == 64) 6257 return false; 6258 6259 unsigned NumElts = VT.getVectorNumElements(); 6260 if (M.size() != NumElts && M.size() != NumElts*2) 6261 return false; 6262 6263 for (unsigned i = 0; i < M.size(); i += NumElts) { 6264 WhichResult = SelectPairHalf(NumElts, M, i); 6265 unsigned Idx = WhichResult * NumElts / 2; 6266 for (unsigned j = 0; j < NumElts; j += 2) { 6267 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 6268 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts)) 6269 return false; 6270 Idx += 1; 6271 } 6272 } 6273 6274 if (M.size() == NumElts*2) 6275 WhichResult = 0; 6276 6277 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6278 if (VT.is64BitVector() && EltSz == 32) 6279 return false; 6280 6281 return true; 6282 } 6283 6284 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 6285 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6286 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 6287 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6288 unsigned EltSz = VT.getScalarSizeInBits(); 6289 if (EltSz == 64) 6290 return false; 6291 6292 unsigned NumElts = VT.getVectorNumElements(); 6293 if (M.size() != NumElts && M.size() != NumElts*2) 6294 return false; 6295 6296 for (unsigned i = 0; i < M.size(); i += NumElts) { 6297 WhichResult = SelectPairHalf(NumElts, M, i); 6298 unsigned Idx = WhichResult * NumElts / 2; 6299 for (unsigned j = 0; j < NumElts; j += 2) { 6300 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 6301 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx)) 6302 return false; 6303 Idx += 1; 6304 } 6305 } 6306 6307 if (M.size() == NumElts*2) 6308 WhichResult = 0; 6309 6310 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6311 if (VT.is64BitVector() && EltSz == 32) 6312 return false; 6313 6314 return true; 6315 } 6316 6317 /// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN), 6318 /// and return the corresponding ARMISD opcode if it is, or 0 if it isn't. 6319 static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT, 6320 unsigned &WhichResult, 6321 bool &isV_UNDEF) { 6322 isV_UNDEF = false; 6323 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 6324 return ARMISD::VTRN; 6325 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 6326 return ARMISD::VUZP; 6327 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 6328 return ARMISD::VZIP; 6329 6330 isV_UNDEF = true; 6331 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 6332 return ARMISD::VTRN; 6333 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 6334 return ARMISD::VUZP; 6335 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 6336 return ARMISD::VZIP; 6337 6338 return 0; 6339 } 6340 6341 /// \return true if this is a reverse operation on an vector. 6342 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 6343 unsigned NumElts = VT.getVectorNumElements(); 6344 // Make sure the mask has the right size. 6345 if (NumElts != M.size()) 6346 return false; 6347 6348 // Look for <15, ..., 3, -1, 1, 0>. 6349 for (unsigned i = 0; i != NumElts; ++i) 6350 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 6351 return false; 6352 6353 return true; 6354 } 6355 6356 // If N is an integer constant that can be moved into a register in one 6357 // instruction, return an SDValue of such a constant (will become a MOV 6358 // instruction). Otherwise return null. 6359 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 6360 const ARMSubtarget *ST, const SDLoc &dl) { 6361 uint64_t Val; 6362 if (!isa<ConstantSDNode>(N)) 6363 return SDValue(); 6364 Val = cast<ConstantSDNode>(N)->getZExtValue(); 6365 6366 if (ST->isThumb1Only()) { 6367 if (Val <= 255 || ~Val <= 255) 6368 return DAG.getConstant(Val, dl, MVT::i32); 6369 } else { 6370 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 6371 return DAG.getConstant(Val, dl, MVT::i32); 6372 } 6373 return SDValue(); 6374 } 6375 6376 // If this is a case we can't handle, return null and let the default 6377 // expansion code take care of it. 6378 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 6379 const ARMSubtarget *ST) const { 6380 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 6381 SDLoc dl(Op); 6382 EVT VT = Op.getValueType(); 6383 6384 APInt SplatBits, SplatUndef; 6385 unsigned SplatBitSize; 6386 bool HasAnyUndefs; 6387 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 6388 if (SplatUndef.isAllOnesValue()) 6389 return DAG.getUNDEF(VT); 6390 6391 if (SplatBitSize <= 64) { 6392 // Check if an immediate VMOV works. 6393 EVT VmovVT; 6394 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 6395 SplatUndef.getZExtValue(), SplatBitSize, 6396 DAG, dl, VmovVT, VT.is128BitVector(), 6397 VMOVModImm); 6398 if (Val.getNode()) { 6399 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 6400 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 6401 } 6402 6403 // Try an immediate VMVN. 6404 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 6405 Val = isNEONModifiedImm(NegatedImm, 6406 SplatUndef.getZExtValue(), SplatBitSize, 6407 DAG, dl, VmovVT, VT.is128BitVector(), 6408 VMVNModImm); 6409 if (Val.getNode()) { 6410 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 6411 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 6412 } 6413 6414 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 6415 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 6416 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 6417 if (ImmVal != -1) { 6418 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32); 6419 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 6420 } 6421 } 6422 } 6423 } 6424 6425 // Scan through the operands to see if only one value is used. 6426 // 6427 // As an optimisation, even if more than one value is used it may be more 6428 // profitable to splat with one value then change some lanes. 6429 // 6430 // Heuristically we decide to do this if the vector has a "dominant" value, 6431 // defined as splatted to more than half of the lanes. 6432 unsigned NumElts = VT.getVectorNumElements(); 6433 bool isOnlyLowElement = true; 6434 bool usesOnlyOneValue = true; 6435 bool hasDominantValue = false; 6436 bool isConstant = true; 6437 6438 // Map of the number of times a particular SDValue appears in the 6439 // element list. 6440 DenseMap<SDValue, unsigned> ValueCounts; 6441 SDValue Value; 6442 for (unsigned i = 0; i < NumElts; ++i) { 6443 SDValue V = Op.getOperand(i); 6444 if (V.isUndef()) 6445 continue; 6446 if (i > 0) 6447 isOnlyLowElement = false; 6448 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 6449 isConstant = false; 6450 6451 ValueCounts.insert(std::make_pair(V, 0)); 6452 unsigned &Count = ValueCounts[V]; 6453 6454 // Is this value dominant? (takes up more than half of the lanes) 6455 if (++Count > (NumElts / 2)) { 6456 hasDominantValue = true; 6457 Value = V; 6458 } 6459 } 6460 if (ValueCounts.size() != 1) 6461 usesOnlyOneValue = false; 6462 if (!Value.getNode() && !ValueCounts.empty()) 6463 Value = ValueCounts.begin()->first; 6464 6465 if (ValueCounts.empty()) 6466 return DAG.getUNDEF(VT); 6467 6468 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 6469 // Keep going if we are hitting this case. 6470 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 6471 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 6472 6473 unsigned EltSize = VT.getScalarSizeInBits(); 6474 6475 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 6476 // i32 and try again. 6477 if (hasDominantValue && EltSize <= 32) { 6478 if (!isConstant) { 6479 SDValue N; 6480 6481 // If we are VDUPing a value that comes directly from a vector, that will 6482 // cause an unnecessary move to and from a GPR, where instead we could 6483 // just use VDUPLANE. We can only do this if the lane being extracted 6484 // is at a constant index, as the VDUP from lane instructions only have 6485 // constant-index forms. 6486 ConstantSDNode *constIndex; 6487 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 6488 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) { 6489 // We need to create a new undef vector to use for the VDUPLANE if the 6490 // size of the vector from which we get the value is different than the 6491 // size of the vector that we need to create. We will insert the element 6492 // such that the register coalescer will remove unnecessary copies. 6493 if (VT != Value->getOperand(0).getValueType()) { 6494 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 6495 VT.getVectorNumElements(); 6496 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6497 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 6498 Value, DAG.getConstant(index, dl, MVT::i32)), 6499 DAG.getConstant(index, dl, MVT::i32)); 6500 } else 6501 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6502 Value->getOperand(0), Value->getOperand(1)); 6503 } else 6504 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 6505 6506 if (!usesOnlyOneValue) { 6507 // The dominant value was splatted as 'N', but we now have to insert 6508 // all differing elements. 6509 for (unsigned I = 0; I < NumElts; ++I) { 6510 if (Op.getOperand(I) == Value) 6511 continue; 6512 SmallVector<SDValue, 3> Ops; 6513 Ops.push_back(N); 6514 Ops.push_back(Op.getOperand(I)); 6515 Ops.push_back(DAG.getConstant(I, dl, MVT::i32)); 6516 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops); 6517 } 6518 } 6519 return N; 6520 } 6521 if (VT.getVectorElementType().isFloatingPoint()) { 6522 SmallVector<SDValue, 8> Ops; 6523 for (unsigned i = 0; i < NumElts; ++i) 6524 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 6525 Op.getOperand(i))); 6526 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 6527 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops); 6528 Val = LowerBUILD_VECTOR(Val, DAG, ST); 6529 if (Val.getNode()) 6530 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6531 } 6532 if (usesOnlyOneValue) { 6533 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 6534 if (isConstant && Val.getNode()) 6535 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 6536 } 6537 } 6538 6539 // If all elements are constants and the case above didn't get hit, fall back 6540 // to the default expansion, which will generate a load from the constant 6541 // pool. 6542 if (isConstant) 6543 return SDValue(); 6544 6545 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 6546 if (NumElts >= 4) { 6547 SDValue shuffle = ReconstructShuffle(Op, DAG); 6548 if (shuffle != SDValue()) 6549 return shuffle; 6550 } 6551 6552 if (VT.is128BitVector() && VT != MVT::v2f64 && VT != MVT::v4f32) { 6553 // If we haven't found an efficient lowering, try splitting a 128-bit vector 6554 // into two 64-bit vectors; we might discover a better way to lower it. 6555 SmallVector<SDValue, 64> Ops(Op->op_begin(), Op->op_begin() + NumElts); 6556 EVT ExtVT = VT.getVectorElementType(); 6557 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElts / 2); 6558 SDValue Lower = 6559 DAG.getBuildVector(HVT, dl, makeArrayRef(&Ops[0], NumElts / 2)); 6560 if (Lower.getOpcode() == ISD::BUILD_VECTOR) 6561 Lower = LowerBUILD_VECTOR(Lower, DAG, ST); 6562 SDValue Upper = DAG.getBuildVector( 6563 HVT, dl, makeArrayRef(&Ops[NumElts / 2], NumElts / 2)); 6564 if (Upper.getOpcode() == ISD::BUILD_VECTOR) 6565 Upper = LowerBUILD_VECTOR(Upper, DAG, ST); 6566 if (Lower && Upper) 6567 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lower, Upper); 6568 } 6569 6570 // Vectors with 32- or 64-bit elements can be built by directly assigning 6571 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 6572 // will be legalized. 6573 if (EltSize >= 32) { 6574 // Do the expansion with floating-point types, since that is what the VFP 6575 // registers are defined to use, and since i64 is not legal. 6576 EVT EltVT = EVT::getFloatingPointVT(EltSize); 6577 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 6578 SmallVector<SDValue, 8> Ops; 6579 for (unsigned i = 0; i < NumElts; ++i) 6580 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 6581 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 6582 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6583 } 6584 6585 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 6586 // know the default expansion would otherwise fall back on something even 6587 // worse. For a vector with one or two non-undef values, that's 6588 // scalar_to_vector for the elements followed by a shuffle (provided the 6589 // shuffle is valid for the target) and materialization element by element 6590 // on the stack followed by a load for everything else. 6591 if (!isConstant && !usesOnlyOneValue) { 6592 SDValue Vec = DAG.getUNDEF(VT); 6593 for (unsigned i = 0 ; i < NumElts; ++i) { 6594 SDValue V = Op.getOperand(i); 6595 if (V.isUndef()) 6596 continue; 6597 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32); 6598 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 6599 } 6600 return Vec; 6601 } 6602 6603 return SDValue(); 6604 } 6605 6606 // Gather data to see if the operation can be modelled as a 6607 // shuffle in combination with VEXTs. 6608 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 6609 SelectionDAG &DAG) const { 6610 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!"); 6611 SDLoc dl(Op); 6612 EVT VT = Op.getValueType(); 6613 unsigned NumElts = VT.getVectorNumElements(); 6614 6615 struct ShuffleSourceInfo { 6616 SDValue Vec; 6617 unsigned MinElt = std::numeric_limits<unsigned>::max(); 6618 unsigned MaxElt = 0; 6619 6620 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to 6621 // be compatible with the shuffle we intend to construct. As a result 6622 // ShuffleVec will be some sliding window into the original Vec. 6623 SDValue ShuffleVec; 6624 6625 // Code should guarantee that element i in Vec starts at element "WindowBase 6626 // + i * WindowScale in ShuffleVec". 6627 int WindowBase = 0; 6628 int WindowScale = 1; 6629 6630 ShuffleSourceInfo(SDValue Vec) : Vec(Vec), ShuffleVec(Vec) {} 6631 6632 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; } 6633 }; 6634 6635 // First gather all vectors used as an immediate source for this BUILD_VECTOR 6636 // node. 6637 SmallVector<ShuffleSourceInfo, 2> Sources; 6638 for (unsigned i = 0; i < NumElts; ++i) { 6639 SDValue V = Op.getOperand(i); 6640 if (V.isUndef()) 6641 continue; 6642 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 6643 // A shuffle can only come from building a vector from various 6644 // elements of other vectors. 6645 return SDValue(); 6646 } else if (!isa<ConstantSDNode>(V.getOperand(1))) { 6647 // Furthermore, shuffles require a constant mask, whereas extractelts 6648 // accept variable indices. 6649 return SDValue(); 6650 } 6651 6652 // Add this element source to the list if it's not already there. 6653 SDValue SourceVec = V.getOperand(0); 6654 auto Source = llvm::find(Sources, SourceVec); 6655 if (Source == Sources.end()) 6656 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec)); 6657 6658 // Update the minimum and maximum lane number seen. 6659 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 6660 Source->MinElt = std::min(Source->MinElt, EltNo); 6661 Source->MaxElt = std::max(Source->MaxElt, EltNo); 6662 } 6663 6664 // Currently only do something sane when at most two source vectors 6665 // are involved. 6666 if (Sources.size() > 2) 6667 return SDValue(); 6668 6669 // Find out the smallest element size among result and two sources, and use 6670 // it as element size to build the shuffle_vector. 6671 EVT SmallestEltTy = VT.getVectorElementType(); 6672 for (auto &Source : Sources) { 6673 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType(); 6674 if (SrcEltTy.bitsLT(SmallestEltTy)) 6675 SmallestEltTy = SrcEltTy; 6676 } 6677 unsigned ResMultiplier = 6678 VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits(); 6679 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits(); 6680 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts); 6681 6682 // If the source vector is too wide or too narrow, we may nevertheless be able 6683 // to construct a compatible shuffle either by concatenating it with UNDEF or 6684 // extracting a suitable range of elements. 6685 for (auto &Src : Sources) { 6686 EVT SrcVT = Src.ShuffleVec.getValueType(); 6687 6688 if (SrcVT.getSizeInBits() == VT.getSizeInBits()) 6689 continue; 6690 6691 // This stage of the search produces a source with the same element type as 6692 // the original, but with a total width matching the BUILD_VECTOR output. 6693 EVT EltVT = SrcVT.getVectorElementType(); 6694 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits(); 6695 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts); 6696 6697 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) { 6698 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits()) 6699 return SDValue(); 6700 // We can pad out the smaller vector for free, so if it's part of a 6701 // shuffle... 6702 Src.ShuffleVec = 6703 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec, 6704 DAG.getUNDEF(Src.ShuffleVec.getValueType())); 6705 continue; 6706 } 6707 6708 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits()) 6709 return SDValue(); 6710 6711 if (Src.MaxElt - Src.MinElt >= NumSrcElts) { 6712 // Span too large for a VEXT to cope 6713 return SDValue(); 6714 } 6715 6716 if (Src.MinElt >= NumSrcElts) { 6717 // The extraction can just take the second half 6718 Src.ShuffleVec = 6719 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6720 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 6721 Src.WindowBase = -NumSrcElts; 6722 } else if (Src.MaxElt < NumSrcElts) { 6723 // The extraction can just take the first half 6724 Src.ShuffleVec = 6725 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6726 DAG.getConstant(0, dl, MVT::i32)); 6727 } else { 6728 // An actual VEXT is needed 6729 SDValue VEXTSrc1 = 6730 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6731 DAG.getConstant(0, dl, MVT::i32)); 6732 SDValue VEXTSrc2 = 6733 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6734 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 6735 6736 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1, 6737 VEXTSrc2, 6738 DAG.getConstant(Src.MinElt, dl, MVT::i32)); 6739 Src.WindowBase = -Src.MinElt; 6740 } 6741 } 6742 6743 // Another possible incompatibility occurs from the vector element types. We 6744 // can fix this by bitcasting the source vectors to the same type we intend 6745 // for the shuffle. 6746 for (auto &Src : Sources) { 6747 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType(); 6748 if (SrcEltTy == SmallestEltTy) 6749 continue; 6750 assert(ShuffleVT.getVectorElementType() == SmallestEltTy); 6751 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec); 6752 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits(); 6753 Src.WindowBase *= Src.WindowScale; 6754 } 6755 6756 // Final sanity check before we try to actually produce a shuffle. 6757 LLVM_DEBUG(for (auto Src 6758 : Sources) 6759 assert(Src.ShuffleVec.getValueType() == ShuffleVT);); 6760 6761 // The stars all align, our next step is to produce the mask for the shuffle. 6762 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1); 6763 int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits(); 6764 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 6765 SDValue Entry = Op.getOperand(i); 6766 if (Entry.isUndef()) 6767 continue; 6768 6769 auto Src = llvm::find(Sources, Entry.getOperand(0)); 6770 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue(); 6771 6772 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit 6773 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this 6774 // segment. 6775 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType(); 6776 int BitsDefined = std::min(OrigEltTy.getSizeInBits(), 6777 VT.getScalarSizeInBits()); 6778 int LanesDefined = BitsDefined / BitsPerShuffleLane; 6779 6780 // This source is expected to fill ResMultiplier lanes of the final shuffle, 6781 // starting at the appropriate offset. 6782 int *LaneMask = &Mask[i * ResMultiplier]; 6783 6784 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase; 6785 ExtractBase += NumElts * (Src - Sources.begin()); 6786 for (int j = 0; j < LanesDefined; ++j) 6787 LaneMask[j] = ExtractBase + j; 6788 } 6789 6790 // Final check before we try to produce nonsense... 6791 if (!isShuffleMaskLegal(Mask, ShuffleVT)) 6792 return SDValue(); 6793 6794 // We can't handle more than two sources. This should have already 6795 // been checked before this point. 6796 assert(Sources.size() <= 2 && "Too many sources!"); 6797 6798 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) }; 6799 for (unsigned i = 0; i < Sources.size(); ++i) 6800 ShuffleOps[i] = Sources[i].ShuffleVec; 6801 6802 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0], 6803 ShuffleOps[1], Mask); 6804 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle); 6805 } 6806 6807 /// isShuffleMaskLegal - Targets can use this to indicate that they only 6808 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 6809 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 6810 /// are assumed to be legal. 6811 bool ARMTargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const { 6812 if (VT.getVectorNumElements() == 4 && 6813 (VT.is128BitVector() || VT.is64BitVector())) { 6814 unsigned PFIndexes[4]; 6815 for (unsigned i = 0; i != 4; ++i) { 6816 if (M[i] < 0) 6817 PFIndexes[i] = 8; 6818 else 6819 PFIndexes[i] = M[i]; 6820 } 6821 6822 // Compute the index in the perfect shuffle table. 6823 unsigned PFTableIndex = 6824 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 6825 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 6826 unsigned Cost = (PFEntry >> 30); 6827 6828 if (Cost <= 4) 6829 return true; 6830 } 6831 6832 bool ReverseVEXT, isV_UNDEF; 6833 unsigned Imm, WhichResult; 6834 6835 unsigned EltSize = VT.getScalarSizeInBits(); 6836 return (EltSize >= 32 || 6837 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 6838 isVREVMask(M, VT, 64) || 6839 isVREVMask(M, VT, 32) || 6840 isVREVMask(M, VT, 16) || 6841 isVEXTMask(M, VT, ReverseVEXT, Imm) || 6842 isVTBLMask(M, VT) || 6843 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) || 6844 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT))); 6845 } 6846 6847 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 6848 /// the specified operations to build the shuffle. 6849 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 6850 SDValue RHS, SelectionDAG &DAG, 6851 const SDLoc &dl) { 6852 unsigned OpNum = (PFEntry >> 26) & 0x0F; 6853 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 6854 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 6855 6856 enum { 6857 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 6858 OP_VREV, 6859 OP_VDUP0, 6860 OP_VDUP1, 6861 OP_VDUP2, 6862 OP_VDUP3, 6863 OP_VEXT1, 6864 OP_VEXT2, 6865 OP_VEXT3, 6866 OP_VUZPL, // VUZP, left result 6867 OP_VUZPR, // VUZP, right result 6868 OP_VZIPL, // VZIP, left result 6869 OP_VZIPR, // VZIP, right result 6870 OP_VTRNL, // VTRN, left result 6871 OP_VTRNR // VTRN, right result 6872 }; 6873 6874 if (OpNum == OP_COPY) { 6875 if (LHSID == (1*9+2)*9+3) return LHS; 6876 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 6877 return RHS; 6878 } 6879 6880 SDValue OpLHS, OpRHS; 6881 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 6882 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 6883 EVT VT = OpLHS.getValueType(); 6884 6885 switch (OpNum) { 6886 default: llvm_unreachable("Unknown shuffle opcode!"); 6887 case OP_VREV: 6888 // VREV divides the vector in half and swaps within the half. 6889 if (VT.getVectorElementType() == MVT::i32 || 6890 VT.getVectorElementType() == MVT::f32) 6891 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 6892 // vrev <4 x i16> -> VREV32 6893 if (VT.getVectorElementType() == MVT::i16) 6894 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 6895 // vrev <4 x i8> -> VREV16 6896 assert(VT.getVectorElementType() == MVT::i8); 6897 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 6898 case OP_VDUP0: 6899 case OP_VDUP1: 6900 case OP_VDUP2: 6901 case OP_VDUP3: 6902 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6903 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32)); 6904 case OP_VEXT1: 6905 case OP_VEXT2: 6906 case OP_VEXT3: 6907 return DAG.getNode(ARMISD::VEXT, dl, VT, 6908 OpLHS, OpRHS, 6909 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32)); 6910 case OP_VUZPL: 6911 case OP_VUZPR: 6912 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 6913 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 6914 case OP_VZIPL: 6915 case OP_VZIPR: 6916 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 6917 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 6918 case OP_VTRNL: 6919 case OP_VTRNR: 6920 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 6921 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 6922 } 6923 } 6924 6925 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 6926 ArrayRef<int> ShuffleMask, 6927 SelectionDAG &DAG) { 6928 // Check to see if we can use the VTBL instruction. 6929 SDValue V1 = Op.getOperand(0); 6930 SDValue V2 = Op.getOperand(1); 6931 SDLoc DL(Op); 6932 6933 SmallVector<SDValue, 8> VTBLMask; 6934 for (ArrayRef<int>::iterator 6935 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 6936 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32)); 6937 6938 if (V2.getNode()->isUndef()) 6939 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 6940 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask)); 6941 6942 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 6943 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask)); 6944 } 6945 6946 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 6947 SelectionDAG &DAG) { 6948 SDLoc DL(Op); 6949 SDValue OpLHS = Op.getOperand(0); 6950 EVT VT = OpLHS.getValueType(); 6951 6952 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 6953 "Expect an v8i16/v16i8 type"); 6954 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 6955 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 6956 // extract the first 8 bytes into the top double word and the last 8 bytes 6957 // into the bottom double word. The v8i16 case is similar. 6958 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 6959 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 6960 DAG.getConstant(ExtractNum, DL, MVT::i32)); 6961 } 6962 6963 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 6964 SDValue V1 = Op.getOperand(0); 6965 SDValue V2 = Op.getOperand(1); 6966 SDLoc dl(Op); 6967 EVT VT = Op.getValueType(); 6968 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 6969 6970 // Convert shuffles that are directly supported on NEON to target-specific 6971 // DAG nodes, instead of keeping them as shuffles and matching them again 6972 // during code selection. This is more efficient and avoids the possibility 6973 // of inconsistencies between legalization and selection. 6974 // FIXME: floating-point vectors should be canonicalized to integer vectors 6975 // of the same time so that they get CSEd properly. 6976 ArrayRef<int> ShuffleMask = SVN->getMask(); 6977 6978 unsigned EltSize = VT.getScalarSizeInBits(); 6979 if (EltSize <= 32) { 6980 if (SVN->isSplat()) { 6981 int Lane = SVN->getSplatIndex(); 6982 // If this is undef splat, generate it via "just" vdup, if possible. 6983 if (Lane == -1) Lane = 0; 6984 6985 // Test if V1 is a SCALAR_TO_VECTOR. 6986 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 6987 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 6988 } 6989 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 6990 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 6991 // reaches it). 6992 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 6993 !isa<ConstantSDNode>(V1.getOperand(0))) { 6994 bool IsScalarToVector = true; 6995 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 6996 if (!V1.getOperand(i).isUndef()) { 6997 IsScalarToVector = false; 6998 break; 6999 } 7000 if (IsScalarToVector) 7001 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 7002 } 7003 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 7004 DAG.getConstant(Lane, dl, MVT::i32)); 7005 } 7006 7007 bool ReverseVEXT = false; 7008 unsigned Imm = 0; 7009 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 7010 if (ReverseVEXT) 7011 std::swap(V1, V2); 7012 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 7013 DAG.getConstant(Imm, dl, MVT::i32)); 7014 } 7015 7016 if (isVREVMask(ShuffleMask, VT, 64)) 7017 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 7018 if (isVREVMask(ShuffleMask, VT, 32)) 7019 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 7020 if (isVREVMask(ShuffleMask, VT, 16)) 7021 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 7022 7023 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 7024 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 7025 DAG.getConstant(Imm, dl, MVT::i32)); 7026 } 7027 7028 // Check for Neon shuffles that modify both input vectors in place. 7029 // If both results are used, i.e., if there are two shuffles with the same 7030 // source operands and with masks corresponding to both results of one of 7031 // these operations, DAG memoization will ensure that a single node is 7032 // used for both shuffles. 7033 unsigned WhichResult = 0; 7034 bool isV_UNDEF = false; 7035 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 7036 ShuffleMask, VT, WhichResult, isV_UNDEF)) { 7037 if (isV_UNDEF) 7038 V2 = V1; 7039 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2) 7040 .getValue(WhichResult); 7041 } 7042 7043 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize 7044 // shuffles that produce a result larger than their operands with: 7045 // shuffle(concat(v1, undef), concat(v2, undef)) 7046 // -> 7047 // shuffle(concat(v1, v2), undef) 7048 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine). 7049 // 7050 // This is useful in the general case, but there are special cases where 7051 // native shuffles produce larger results: the two-result ops. 7052 // 7053 // Look through the concat when lowering them: 7054 // shuffle(concat(v1, v2), undef) 7055 // -> 7056 // concat(VZIP(v1, v2):0, :1) 7057 // 7058 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) { 7059 SDValue SubV1 = V1->getOperand(0); 7060 SDValue SubV2 = V1->getOperand(1); 7061 EVT SubVT = SubV1.getValueType(); 7062 7063 // We expect these to have been canonicalized to -1. 7064 assert(llvm::all_of(ShuffleMask, [&](int i) { 7065 return i < (int)VT.getVectorNumElements(); 7066 }) && "Unexpected shuffle index into UNDEF operand!"); 7067 7068 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 7069 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) { 7070 if (isV_UNDEF) 7071 SubV2 = SubV1; 7072 assert((WhichResult == 0) && 7073 "In-place shuffle of concat can only have one result!"); 7074 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT), 7075 SubV1, SubV2); 7076 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0), 7077 Res.getValue(1)); 7078 } 7079 } 7080 } 7081 7082 // If the shuffle is not directly supported and it has 4 elements, use 7083 // the PerfectShuffle-generated table to synthesize it from other shuffles. 7084 unsigned NumElts = VT.getVectorNumElements(); 7085 if (NumElts == 4) { 7086 unsigned PFIndexes[4]; 7087 for (unsigned i = 0; i != 4; ++i) { 7088 if (ShuffleMask[i] < 0) 7089 PFIndexes[i] = 8; 7090 else 7091 PFIndexes[i] = ShuffleMask[i]; 7092 } 7093 7094 // Compute the index in the perfect shuffle table. 7095 unsigned PFTableIndex = 7096 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 7097 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 7098 unsigned Cost = (PFEntry >> 30); 7099 7100 if (Cost <= 4) 7101 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 7102 } 7103 7104 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 7105 if (EltSize >= 32) { 7106 // Do the expansion with floating-point types, since that is what the VFP 7107 // registers are defined to use, and since i64 is not legal. 7108 EVT EltVT = EVT::getFloatingPointVT(EltSize); 7109 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 7110 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 7111 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 7112 SmallVector<SDValue, 8> Ops; 7113 for (unsigned i = 0; i < NumElts; ++i) { 7114 if (ShuffleMask[i] < 0) 7115 Ops.push_back(DAG.getUNDEF(EltVT)); 7116 else 7117 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 7118 ShuffleMask[i] < (int)NumElts ? V1 : V2, 7119 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 7120 dl, MVT::i32))); 7121 } 7122 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 7123 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 7124 } 7125 7126 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 7127 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 7128 7129 if (VT == MVT::v8i8) 7130 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG)) 7131 return NewOp; 7132 7133 return SDValue(); 7134 } 7135 7136 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 7137 // INSERT_VECTOR_ELT is legal only for immediate indexes. 7138 SDValue Lane = Op.getOperand(2); 7139 if (!isa<ConstantSDNode>(Lane)) 7140 return SDValue(); 7141 7142 return Op; 7143 } 7144 7145 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 7146 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 7147 SDValue Lane = Op.getOperand(1); 7148 if (!isa<ConstantSDNode>(Lane)) 7149 return SDValue(); 7150 7151 SDValue Vec = Op.getOperand(0); 7152 if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) { 7153 SDLoc dl(Op); 7154 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 7155 } 7156 7157 return Op; 7158 } 7159 7160 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 7161 // The only time a CONCAT_VECTORS operation can have legal types is when 7162 // two 64-bit vectors are concatenated to a 128-bit vector. 7163 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 7164 "unexpected CONCAT_VECTORS"); 7165 SDLoc dl(Op); 7166 SDValue Val = DAG.getUNDEF(MVT::v2f64); 7167 SDValue Op0 = Op.getOperand(0); 7168 SDValue Op1 = Op.getOperand(1); 7169 if (!Op0.isUndef()) 7170 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 7171 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 7172 DAG.getIntPtrConstant(0, dl)); 7173 if (!Op1.isUndef()) 7174 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 7175 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 7176 DAG.getIntPtrConstant(1, dl)); 7177 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 7178 } 7179 7180 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 7181 /// element has been zero/sign-extended, depending on the isSigned parameter, 7182 /// from an integer type half its size. 7183 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 7184 bool isSigned) { 7185 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 7186 EVT VT = N->getValueType(0); 7187 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 7188 SDNode *BVN = N->getOperand(0).getNode(); 7189 if (BVN->getValueType(0) != MVT::v4i32 || 7190 BVN->getOpcode() != ISD::BUILD_VECTOR) 7191 return false; 7192 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 7193 unsigned HiElt = 1 - LoElt; 7194 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 7195 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 7196 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 7197 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 7198 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 7199 return false; 7200 if (isSigned) { 7201 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 7202 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 7203 return true; 7204 } else { 7205 if (Hi0->isNullValue() && Hi1->isNullValue()) 7206 return true; 7207 } 7208 return false; 7209 } 7210 7211 if (N->getOpcode() != ISD::BUILD_VECTOR) 7212 return false; 7213 7214 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 7215 SDNode *Elt = N->getOperand(i).getNode(); 7216 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 7217 unsigned EltSize = VT.getScalarSizeInBits(); 7218 unsigned HalfSize = EltSize / 2; 7219 if (isSigned) { 7220 if (!isIntN(HalfSize, C->getSExtValue())) 7221 return false; 7222 } else { 7223 if (!isUIntN(HalfSize, C->getZExtValue())) 7224 return false; 7225 } 7226 continue; 7227 } 7228 return false; 7229 } 7230 7231 return true; 7232 } 7233 7234 /// isSignExtended - Check if a node is a vector value that is sign-extended 7235 /// or a constant BUILD_VECTOR with sign-extended elements. 7236 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 7237 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 7238 return true; 7239 if (isExtendedBUILD_VECTOR(N, DAG, true)) 7240 return true; 7241 return false; 7242 } 7243 7244 /// isZeroExtended - Check if a node is a vector value that is zero-extended 7245 /// or a constant BUILD_VECTOR with zero-extended elements. 7246 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 7247 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 7248 return true; 7249 if (isExtendedBUILD_VECTOR(N, DAG, false)) 7250 return true; 7251 return false; 7252 } 7253 7254 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 7255 if (OrigVT.getSizeInBits() >= 64) 7256 return OrigVT; 7257 7258 assert(OrigVT.isSimple() && "Expecting a simple value type"); 7259 7260 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 7261 switch (OrigSimpleTy) { 7262 default: llvm_unreachable("Unexpected Vector Type"); 7263 case MVT::v2i8: 7264 case MVT::v2i16: 7265 return MVT::v2i32; 7266 case MVT::v4i8: 7267 return MVT::v4i16; 7268 } 7269 } 7270 7271 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 7272 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 7273 /// We insert the required extension here to get the vector to fill a D register. 7274 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 7275 const EVT &OrigTy, 7276 const EVT &ExtTy, 7277 unsigned ExtOpcode) { 7278 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 7279 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 7280 // 64-bits we need to insert a new extension so that it will be 64-bits. 7281 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 7282 if (OrigTy.getSizeInBits() >= 64) 7283 return N; 7284 7285 // Must extend size to at least 64 bits to be used as an operand for VMULL. 7286 EVT NewVT = getExtensionTo64Bits(OrigTy); 7287 7288 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 7289 } 7290 7291 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 7292 /// does not do any sign/zero extension. If the original vector is less 7293 /// than 64 bits, an appropriate extension will be added after the load to 7294 /// reach a total size of 64 bits. We have to add the extension separately 7295 /// because ARM does not have a sign/zero extending load for vectors. 7296 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 7297 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 7298 7299 // The load already has the right type. 7300 if (ExtendedTy == LD->getMemoryVT()) 7301 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 7302 LD->getBasePtr(), LD->getPointerInfo(), 7303 LD->getAlignment(), LD->getMemOperand()->getFlags()); 7304 7305 // We need to create a zextload/sextload. We cannot just create a load 7306 // followed by a zext/zext node because LowerMUL is also run during normal 7307 // operation legalization where we can't create illegal types. 7308 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 7309 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 7310 LD->getMemoryVT(), LD->getAlignment(), 7311 LD->getMemOperand()->getFlags()); 7312 } 7313 7314 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 7315 /// extending load, or BUILD_VECTOR with extended elements, return the 7316 /// unextended value. The unextended vector should be 64 bits so that it can 7317 /// be used as an operand to a VMULL instruction. If the original vector size 7318 /// before extension is less than 64 bits we add a an extension to resize 7319 /// the vector to 64 bits. 7320 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 7321 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 7322 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 7323 N->getOperand(0)->getValueType(0), 7324 N->getValueType(0), 7325 N->getOpcode()); 7326 7327 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 7328 assert((ISD::isSEXTLoad(LD) || ISD::isZEXTLoad(LD)) && 7329 "Expected extending load"); 7330 7331 SDValue newLoad = SkipLoadExtensionForVMULL(LD, DAG); 7332 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), newLoad.getValue(1)); 7333 unsigned Opcode = ISD::isSEXTLoad(LD) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 7334 SDValue extLoad = 7335 DAG.getNode(Opcode, SDLoc(newLoad), LD->getValueType(0), newLoad); 7336 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 0), extLoad); 7337 7338 return newLoad; 7339 } 7340 7341 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 7342 // have been legalized as a BITCAST from v4i32. 7343 if (N->getOpcode() == ISD::BITCAST) { 7344 SDNode *BVN = N->getOperand(0).getNode(); 7345 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 7346 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 7347 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 7348 return DAG.getBuildVector( 7349 MVT::v2i32, SDLoc(N), 7350 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)}); 7351 } 7352 // Construct a new BUILD_VECTOR with elements truncated to half the size. 7353 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 7354 EVT VT = N->getValueType(0); 7355 unsigned EltSize = VT.getScalarSizeInBits() / 2; 7356 unsigned NumElts = VT.getVectorNumElements(); 7357 MVT TruncVT = MVT::getIntegerVT(EltSize); 7358 SmallVector<SDValue, 8> Ops; 7359 SDLoc dl(N); 7360 for (unsigned i = 0; i != NumElts; ++i) { 7361 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 7362 const APInt &CInt = C->getAPIntValue(); 7363 // Element types smaller than 32 bits are not legal, so use i32 elements. 7364 // The values are implicitly truncated so sext vs. zext doesn't matter. 7365 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32)); 7366 } 7367 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops); 7368 } 7369 7370 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 7371 unsigned Opcode = N->getOpcode(); 7372 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 7373 SDNode *N0 = N->getOperand(0).getNode(); 7374 SDNode *N1 = N->getOperand(1).getNode(); 7375 return N0->hasOneUse() && N1->hasOneUse() && 7376 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 7377 } 7378 return false; 7379 } 7380 7381 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 7382 unsigned Opcode = N->getOpcode(); 7383 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 7384 SDNode *N0 = N->getOperand(0).getNode(); 7385 SDNode *N1 = N->getOperand(1).getNode(); 7386 return N0->hasOneUse() && N1->hasOneUse() && 7387 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 7388 } 7389 return false; 7390 } 7391 7392 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 7393 // Multiplications are only custom-lowered for 128-bit vectors so that 7394 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 7395 EVT VT = Op.getValueType(); 7396 assert(VT.is128BitVector() && VT.isInteger() && 7397 "unexpected type for custom-lowering ISD::MUL"); 7398 SDNode *N0 = Op.getOperand(0).getNode(); 7399 SDNode *N1 = Op.getOperand(1).getNode(); 7400 unsigned NewOpc = 0; 7401 bool isMLA = false; 7402 bool isN0SExt = isSignExtended(N0, DAG); 7403 bool isN1SExt = isSignExtended(N1, DAG); 7404 if (isN0SExt && isN1SExt) 7405 NewOpc = ARMISD::VMULLs; 7406 else { 7407 bool isN0ZExt = isZeroExtended(N0, DAG); 7408 bool isN1ZExt = isZeroExtended(N1, DAG); 7409 if (isN0ZExt && isN1ZExt) 7410 NewOpc = ARMISD::VMULLu; 7411 else if (isN1SExt || isN1ZExt) { 7412 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 7413 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 7414 if (isN1SExt && isAddSubSExt(N0, DAG)) { 7415 NewOpc = ARMISD::VMULLs; 7416 isMLA = true; 7417 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 7418 NewOpc = ARMISD::VMULLu; 7419 isMLA = true; 7420 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 7421 std::swap(N0, N1); 7422 NewOpc = ARMISD::VMULLu; 7423 isMLA = true; 7424 } 7425 } 7426 7427 if (!NewOpc) { 7428 if (VT == MVT::v2i64) 7429 // Fall through to expand this. It is not legal. 7430 return SDValue(); 7431 else 7432 // Other vector multiplications are legal. 7433 return Op; 7434 } 7435 } 7436 7437 // Legalize to a VMULL instruction. 7438 SDLoc DL(Op); 7439 SDValue Op0; 7440 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 7441 if (!isMLA) { 7442 Op0 = SkipExtensionForVMULL(N0, DAG); 7443 assert(Op0.getValueType().is64BitVector() && 7444 Op1.getValueType().is64BitVector() && 7445 "unexpected types for extended operands to VMULL"); 7446 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 7447 } 7448 7449 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 7450 // isel lowering to take advantage of no-stall back to back vmul + vmla. 7451 // vmull q0, d4, d6 7452 // vmlal q0, d5, d6 7453 // is faster than 7454 // vaddl q0, d4, d5 7455 // vmovl q1, d6 7456 // vmul q0, q0, q1 7457 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 7458 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 7459 EVT Op1VT = Op1.getValueType(); 7460 return DAG.getNode(N0->getOpcode(), DL, VT, 7461 DAG.getNode(NewOpc, DL, VT, 7462 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 7463 DAG.getNode(NewOpc, DL, VT, 7464 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 7465 } 7466 7467 static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl, 7468 SelectionDAG &DAG) { 7469 // TODO: Should this propagate fast-math-flags? 7470 7471 // Convert to float 7472 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 7473 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 7474 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 7475 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 7476 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 7477 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 7478 // Get reciprocal estimate. 7479 // float4 recip = vrecpeq_f32(yf); 7480 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7481 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 7482 Y); 7483 // Because char has a smaller range than uchar, we can actually get away 7484 // without any newton steps. This requires that we use a weird bias 7485 // of 0xb000, however (again, this has been exhaustively tested). 7486 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 7487 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 7488 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 7489 Y = DAG.getConstant(0xb000, dl, MVT::v4i32); 7490 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 7491 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 7492 // Convert back to short. 7493 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 7494 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 7495 return X; 7496 } 7497 7498 static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl, 7499 SelectionDAG &DAG) { 7500 // TODO: Should this propagate fast-math-flags? 7501 7502 SDValue N2; 7503 // Convert to float. 7504 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 7505 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 7506 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 7507 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 7508 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 7509 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 7510 7511 // Use reciprocal estimate and one refinement step. 7512 // float4 recip = vrecpeq_f32(yf); 7513 // recip *= vrecpsq_f32(yf, recip); 7514 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7515 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 7516 N1); 7517 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7518 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 7519 N1, N2); 7520 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 7521 // Because short has a smaller range than ushort, we can actually get away 7522 // with only a single newton step. This requires that we use a weird bias 7523 // of 89, however (again, this has been exhaustively tested). 7524 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 7525 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 7526 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 7527 N1 = DAG.getConstant(0x89, dl, MVT::v4i32); 7528 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 7529 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 7530 // Convert back to integer and return. 7531 // return vmovn_s32(vcvt_s32_f32(result)); 7532 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 7533 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 7534 return N0; 7535 } 7536 7537 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 7538 EVT VT = Op.getValueType(); 7539 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 7540 "unexpected type for custom-lowering ISD::SDIV"); 7541 7542 SDLoc dl(Op); 7543 SDValue N0 = Op.getOperand(0); 7544 SDValue N1 = Op.getOperand(1); 7545 SDValue N2, N3; 7546 7547 if (VT == MVT::v8i8) { 7548 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 7549 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 7550 7551 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7552 DAG.getIntPtrConstant(4, dl)); 7553 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7554 DAG.getIntPtrConstant(4, dl)); 7555 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7556 DAG.getIntPtrConstant(0, dl)); 7557 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7558 DAG.getIntPtrConstant(0, dl)); 7559 7560 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 7561 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 7562 7563 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 7564 N0 = LowerCONCAT_VECTORS(N0, DAG); 7565 7566 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 7567 return N0; 7568 } 7569 return LowerSDIV_v4i16(N0, N1, dl, DAG); 7570 } 7571 7572 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 7573 // TODO: Should this propagate fast-math-flags? 7574 EVT VT = Op.getValueType(); 7575 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 7576 "unexpected type for custom-lowering ISD::UDIV"); 7577 7578 SDLoc dl(Op); 7579 SDValue N0 = Op.getOperand(0); 7580 SDValue N1 = Op.getOperand(1); 7581 SDValue N2, N3; 7582 7583 if (VT == MVT::v8i8) { 7584 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 7585 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 7586 7587 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7588 DAG.getIntPtrConstant(4, dl)); 7589 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7590 DAG.getIntPtrConstant(4, dl)); 7591 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7592 DAG.getIntPtrConstant(0, dl)); 7593 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7594 DAG.getIntPtrConstant(0, dl)); 7595 7596 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 7597 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 7598 7599 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 7600 N0 = LowerCONCAT_VECTORS(N0, DAG); 7601 7602 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 7603 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl, 7604 MVT::i32), 7605 N0); 7606 return N0; 7607 } 7608 7609 // v4i16 sdiv ... Convert to float. 7610 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 7611 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 7612 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 7613 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 7614 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 7615 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 7616 7617 // Use reciprocal estimate and two refinement steps. 7618 // float4 recip = vrecpeq_f32(yf); 7619 // recip *= vrecpsq_f32(yf, recip); 7620 // recip *= vrecpsq_f32(yf, recip); 7621 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7622 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 7623 BN1); 7624 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7625 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 7626 BN1, N2); 7627 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 7628 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7629 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 7630 BN1, N2); 7631 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 7632 // Simply multiplying by the reciprocal estimate can leave us a few ulps 7633 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 7634 // and that it will never cause us to return an answer too large). 7635 // float4 result = as_float4(as_int4(xf*recip) + 2); 7636 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 7637 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 7638 N1 = DAG.getConstant(2, dl, MVT::v4i32); 7639 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 7640 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 7641 // Convert back to integer and return. 7642 // return vmovn_u32(vcvt_s32_f32(result)); 7643 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 7644 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 7645 return N0; 7646 } 7647 7648 static SDValue LowerADDSUBCARRY(SDValue Op, SelectionDAG &DAG) { 7649 SDNode *N = Op.getNode(); 7650 EVT VT = N->getValueType(0); 7651 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 7652 7653 SDValue Carry = Op.getOperand(2); 7654 7655 SDLoc DL(Op); 7656 7657 SDValue Result; 7658 if (Op.getOpcode() == ISD::ADDCARRY) { 7659 // This converts the boolean value carry into the carry flag. 7660 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 7661 7662 // Do the addition proper using the carry flag we wanted. 7663 Result = DAG.getNode(ARMISD::ADDE, DL, VTs, Op.getOperand(0), 7664 Op.getOperand(1), Carry); 7665 7666 // Now convert the carry flag into a boolean value. 7667 Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG); 7668 } else { 7669 // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we 7670 // have to invert the carry first. 7671 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 7672 DAG.getConstant(1, DL, MVT::i32), Carry); 7673 // This converts the boolean value carry into the carry flag. 7674 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 7675 7676 // Do the subtraction proper using the carry flag we wanted. 7677 Result = DAG.getNode(ARMISD::SUBE, DL, VTs, Op.getOperand(0), 7678 Op.getOperand(1), Carry); 7679 7680 // Now convert the carry flag into a boolean value. 7681 Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG); 7682 // But the carry returned by ARMISD::SUBE is not a borrow as expected 7683 // by ISD::SUBCARRY, so compute 1 - C. 7684 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 7685 DAG.getConstant(1, DL, MVT::i32), Carry); 7686 } 7687 7688 // Return both values. 7689 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Carry); 7690 } 7691 7692 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { 7693 assert(Subtarget->isTargetDarwin()); 7694 7695 // For iOS, we want to call an alternative entry point: __sincos_stret, 7696 // return values are passed via sret. 7697 SDLoc dl(Op); 7698 SDValue Arg = Op.getOperand(0); 7699 EVT ArgVT = Arg.getValueType(); 7700 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 7701 auto PtrVT = getPointerTy(DAG.getDataLayout()); 7702 7703 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 7704 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7705 7706 // Pair of floats / doubles used to pass the result. 7707 Type *RetTy = StructType::get(ArgTy, ArgTy); 7708 auto &DL = DAG.getDataLayout(); 7709 7710 ArgListTy Args; 7711 bool ShouldUseSRet = Subtarget->isAPCS_ABI(); 7712 SDValue SRet; 7713 if (ShouldUseSRet) { 7714 // Create stack object for sret. 7715 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy); 7716 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy); 7717 int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false); 7718 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL)); 7719 7720 ArgListEntry Entry; 7721 Entry.Node = SRet; 7722 Entry.Ty = RetTy->getPointerTo(); 7723 Entry.IsSExt = false; 7724 Entry.IsZExt = false; 7725 Entry.IsSRet = true; 7726 Args.push_back(Entry); 7727 RetTy = Type::getVoidTy(*DAG.getContext()); 7728 } 7729 7730 ArgListEntry Entry; 7731 Entry.Node = Arg; 7732 Entry.Ty = ArgTy; 7733 Entry.IsSExt = false; 7734 Entry.IsZExt = false; 7735 Args.push_back(Entry); 7736 7737 RTLIB::Libcall LC = 7738 (ArgVT == MVT::f64) ? RTLIB::SINCOS_STRET_F64 : RTLIB::SINCOS_STRET_F32; 7739 const char *LibcallName = getLibcallName(LC); 7740 CallingConv::ID CC = getLibcallCallingConv(LC); 7741 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL)); 7742 7743 TargetLowering::CallLoweringInfo CLI(DAG); 7744 CLI.setDebugLoc(dl) 7745 .setChain(DAG.getEntryNode()) 7746 .setCallee(CC, RetTy, Callee, std::move(Args)) 7747 .setDiscardResult(ShouldUseSRet); 7748 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 7749 7750 if (!ShouldUseSRet) 7751 return CallResult.first; 7752 7753 SDValue LoadSin = 7754 DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo()); 7755 7756 // Address of cos field. 7757 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet, 7758 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl)); 7759 SDValue LoadCos = 7760 DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo()); 7761 7762 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); 7763 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, 7764 LoadSin.getValue(0), LoadCos.getValue(0)); 7765 } 7766 7767 SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG, 7768 bool Signed, 7769 SDValue &Chain) const { 7770 EVT VT = Op.getValueType(); 7771 assert((VT == MVT::i32 || VT == MVT::i64) && 7772 "unexpected type for custom lowering DIV"); 7773 SDLoc dl(Op); 7774 7775 const auto &DL = DAG.getDataLayout(); 7776 const auto &TLI = DAG.getTargetLoweringInfo(); 7777 7778 const char *Name = nullptr; 7779 if (Signed) 7780 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64"; 7781 else 7782 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64"; 7783 7784 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL)); 7785 7786 ARMTargetLowering::ArgListTy Args; 7787 7788 for (auto AI : {1, 0}) { 7789 ArgListEntry Arg; 7790 Arg.Node = Op.getOperand(AI); 7791 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext()); 7792 Args.push_back(Arg); 7793 } 7794 7795 CallLoweringInfo CLI(DAG); 7796 CLI.setDebugLoc(dl) 7797 .setChain(Chain) 7798 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()), 7799 ES, std::move(Args)); 7800 7801 return LowerCallTo(CLI).first; 7802 } 7803 7804 // This is a code size optimisation: return the original SDIV node to 7805 // DAGCombiner when we don't want to expand SDIV into a sequence of 7806 // instructions, and an empty node otherwise which will cause the 7807 // SDIV to be expanded in DAGCombine. 7808 SDValue 7809 ARMTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 7810 SelectionDAG &DAG, 7811 SmallVectorImpl<SDNode *> &Created) const { 7812 // TODO: Support SREM 7813 if (N->getOpcode() != ISD::SDIV) 7814 return SDValue(); 7815 7816 const auto &ST = static_cast<const ARMSubtarget&>(DAG.getSubtarget()); 7817 const bool MinSize = ST.hasMinSize(); 7818 const bool HasDivide = ST.isThumb() ? ST.hasDivideInThumbMode() 7819 : ST.hasDivideInARMMode(); 7820 7821 // Don't touch vector types; rewriting this may lead to scalarizing 7822 // the int divs. 7823 if (N->getOperand(0).getValueType().isVector()) 7824 return SDValue(); 7825 7826 // Bail if MinSize is not set, and also for both ARM and Thumb mode we need 7827 // hwdiv support for this to be really profitable. 7828 if (!(MinSize && HasDivide)) 7829 return SDValue(); 7830 7831 // ARM mode is a bit simpler than Thumb: we can handle large power 7832 // of 2 immediates with 1 mov instruction; no further checks required, 7833 // just return the sdiv node. 7834 if (!ST.isThumb()) 7835 return SDValue(N, 0); 7836 7837 // In Thumb mode, immediates larger than 128 need a wide 4-byte MOV, 7838 // and thus lose the code size benefits of a MOVS that requires only 2. 7839 // TargetTransformInfo and 'getIntImmCodeSizeCost' could be helpful here, 7840 // but as it's doing exactly this, it's not worth the trouble to get TTI. 7841 if (Divisor.sgt(128)) 7842 return SDValue(); 7843 7844 return SDValue(N, 0); 7845 } 7846 7847 SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG, 7848 bool Signed) const { 7849 assert(Op.getValueType() == MVT::i32 && 7850 "unexpected type for custom lowering DIV"); 7851 SDLoc dl(Op); 7852 7853 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, 7854 DAG.getEntryNode(), Op.getOperand(1)); 7855 7856 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 7857 } 7858 7859 static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) { 7860 SDLoc DL(N); 7861 SDValue Op = N->getOperand(1); 7862 if (N->getValueType(0) == MVT::i32) 7863 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op); 7864 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op, 7865 DAG.getConstant(0, DL, MVT::i32)); 7866 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op, 7867 DAG.getConstant(1, DL, MVT::i32)); 7868 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, 7869 DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi)); 7870 } 7871 7872 void ARMTargetLowering::ExpandDIV_Windows( 7873 SDValue Op, SelectionDAG &DAG, bool Signed, 7874 SmallVectorImpl<SDValue> &Results) const { 7875 const auto &DL = DAG.getDataLayout(); 7876 const auto &TLI = DAG.getTargetLoweringInfo(); 7877 7878 assert(Op.getValueType() == MVT::i64 && 7879 "unexpected type for custom lowering DIV"); 7880 SDLoc dl(Op); 7881 7882 SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode()); 7883 7884 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 7885 7886 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result); 7887 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result, 7888 DAG.getConstant(32, dl, TLI.getPointerTy(DL))); 7889 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper); 7890 7891 Results.push_back(Lower); 7892 Results.push_back(Upper); 7893 } 7894 7895 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 7896 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering())) 7897 // Acquire/Release load/store is not legal for targets without a dmb or 7898 // equivalent available. 7899 return SDValue(); 7900 7901 // Monotonic load/store is legal for all targets. 7902 return Op; 7903 } 7904 7905 static void ReplaceREADCYCLECOUNTER(SDNode *N, 7906 SmallVectorImpl<SDValue> &Results, 7907 SelectionDAG &DAG, 7908 const ARMSubtarget *Subtarget) { 7909 SDLoc DL(N); 7910 // Under Power Management extensions, the cycle-count is: 7911 // mrc p15, #0, <Rt>, c9, c13, #0 7912 SDValue Ops[] = { N->getOperand(0), // Chain 7913 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 7914 DAG.getConstant(15, DL, MVT::i32), 7915 DAG.getConstant(0, DL, MVT::i32), 7916 DAG.getConstant(9, DL, MVT::i32), 7917 DAG.getConstant(13, DL, MVT::i32), 7918 DAG.getConstant(0, DL, MVT::i32) 7919 }; 7920 7921 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 7922 DAG.getVTList(MVT::i32, MVT::Other), Ops); 7923 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32, 7924 DAG.getConstant(0, DL, MVT::i32))); 7925 Results.push_back(Cycles32.getValue(1)); 7926 } 7927 7928 static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) { 7929 SDLoc dl(V.getNode()); 7930 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32); 7931 SDValue VHi = DAG.getAnyExtOrTrunc( 7932 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)), 7933 dl, MVT::i32); 7934 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 7935 if (isBigEndian) 7936 std::swap (VLo, VHi); 7937 SDValue RegClass = 7938 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32); 7939 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32); 7940 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32); 7941 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 }; 7942 return SDValue( 7943 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0); 7944 } 7945 7946 static void ReplaceCMP_SWAP_64Results(SDNode *N, 7947 SmallVectorImpl<SDValue> & Results, 7948 SelectionDAG &DAG) { 7949 assert(N->getValueType(0) == MVT::i64 && 7950 "AtomicCmpSwap on types less than 64 should be legal"); 7951 SDValue Ops[] = {N->getOperand(1), 7952 createGPRPairNode(DAG, N->getOperand(2)), 7953 createGPRPairNode(DAG, N->getOperand(3)), 7954 N->getOperand(0)}; 7955 SDNode *CmpSwap = DAG.getMachineNode( 7956 ARM::CMP_SWAP_64, SDLoc(N), 7957 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops); 7958 7959 MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand(); 7960 DAG.setNodeMemRefs(cast<MachineSDNode>(CmpSwap), {MemOp}); 7961 7962 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 7963 7964 Results.push_back( 7965 DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_1 : ARM::gsub_0, 7966 SDLoc(N), MVT::i32, SDValue(CmpSwap, 0))); 7967 Results.push_back( 7968 DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_0 : ARM::gsub_1, 7969 SDLoc(N), MVT::i32, SDValue(CmpSwap, 0))); 7970 Results.push_back(SDValue(CmpSwap, 2)); 7971 } 7972 7973 static SDValue LowerFPOWI(SDValue Op, const ARMSubtarget &Subtarget, 7974 SelectionDAG &DAG) { 7975 const auto &TLI = DAG.getTargetLoweringInfo(); 7976 7977 assert(Subtarget.getTargetTriple().isOSMSVCRT() && 7978 "Custom lowering is MSVCRT specific!"); 7979 7980 SDLoc dl(Op); 7981 SDValue Val = Op.getOperand(0); 7982 MVT Ty = Val->getSimpleValueType(0); 7983 SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, dl, Ty, Op.getOperand(1)); 7984 SDValue Callee = DAG.getExternalSymbol(Ty == MVT::f32 ? "powf" : "pow", 7985 TLI.getPointerTy(DAG.getDataLayout())); 7986 7987 TargetLowering::ArgListTy Args; 7988 TargetLowering::ArgListEntry Entry; 7989 7990 Entry.Node = Val; 7991 Entry.Ty = Val.getValueType().getTypeForEVT(*DAG.getContext()); 7992 Entry.IsZExt = true; 7993 Args.push_back(Entry); 7994 7995 Entry.Node = Exponent; 7996 Entry.Ty = Exponent.getValueType().getTypeForEVT(*DAG.getContext()); 7997 Entry.IsZExt = true; 7998 Args.push_back(Entry); 7999 8000 Type *LCRTy = Val.getValueType().getTypeForEVT(*DAG.getContext()); 8001 8002 // In the in-chain to the call is the entry node If we are emitting a 8003 // tailcall, the chain will be mutated if the node has a non-entry input 8004 // chain. 8005 SDValue InChain = DAG.getEntryNode(); 8006 SDValue TCChain = InChain; 8007 8008 const Function &F = DAG.getMachineFunction().getFunction(); 8009 bool IsTC = TLI.isInTailCallPosition(DAG, Op.getNode(), TCChain) && 8010 F.getReturnType() == LCRTy; 8011 if (IsTC) 8012 InChain = TCChain; 8013 8014 TargetLowering::CallLoweringInfo CLI(DAG); 8015 CLI.setDebugLoc(dl) 8016 .setChain(InChain) 8017 .setCallee(CallingConv::ARM_AAPCS_VFP, LCRTy, Callee, std::move(Args)) 8018 .setTailCall(IsTC); 8019 std::pair<SDValue, SDValue> CI = TLI.LowerCallTo(CLI); 8020 8021 // Return the chain (the DAG root) if it is a tail call 8022 return !CI.second.getNode() ? DAG.getRoot() : CI.first; 8023 } 8024 8025 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 8026 LLVM_DEBUG(dbgs() << "Lowering node: "; Op.dump()); 8027 switch (Op.getOpcode()) { 8028 default: llvm_unreachable("Don't know how to custom lower this!"); 8029 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG); 8030 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 8031 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 8032 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 8033 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 8034 case ISD::SELECT: return LowerSELECT(Op, DAG); 8035 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 8036 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 8037 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 8038 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 8039 case ISD::VASTART: return LowerVASTART(Op, DAG); 8040 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 8041 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 8042 case ISD::SINT_TO_FP: 8043 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 8044 case ISD::FP_TO_SINT: 8045 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 8046 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 8047 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 8048 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 8049 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 8050 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 8051 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG); 8052 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 8053 Subtarget); 8054 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG, Subtarget); 8055 case ISD::SHL: 8056 case ISD::SRL: 8057 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 8058 case ISD::SREM: return LowerREM(Op.getNode(), DAG); 8059 case ISD::UREM: return LowerREM(Op.getNode(), DAG); 8060 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 8061 case ISD::SRL_PARTS: 8062 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 8063 case ISD::CTTZ: 8064 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 8065 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 8066 case ISD::SETCC: return LowerVSETCC(Op, DAG); 8067 case ISD::SETCCCARRY: return LowerSETCCCARRY(Op, DAG); 8068 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 8069 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 8070 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 8071 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 8072 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 8073 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 8074 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 8075 case ISD::MUL: return LowerMUL(Op, DAG); 8076 case ISD::SDIV: 8077 if (Subtarget->isTargetWindows() && !Op.getValueType().isVector()) 8078 return LowerDIV_Windows(Op, DAG, /* Signed */ true); 8079 return LowerSDIV(Op, DAG); 8080 case ISD::UDIV: 8081 if (Subtarget->isTargetWindows() && !Op.getValueType().isVector()) 8082 return LowerDIV_Windows(Op, DAG, /* Signed */ false); 8083 return LowerUDIV(Op, DAG); 8084 case ISD::ADDCARRY: 8085 case ISD::SUBCARRY: return LowerADDSUBCARRY(Op, DAG); 8086 case ISD::SADDO: 8087 case ISD::SSUBO: 8088 return LowerSignedALUO(Op, DAG); 8089 case ISD::UADDO: 8090 case ISD::USUBO: 8091 return LowerUnsignedALUO(Op, DAG); 8092 case ISD::ATOMIC_LOAD: 8093 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 8094 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); 8095 case ISD::SDIVREM: 8096 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 8097 case ISD::DYNAMIC_STACKALLOC: 8098 if (Subtarget->isTargetWindows()) 8099 return LowerDYNAMIC_STACKALLOC(Op, DAG); 8100 llvm_unreachable("Don't know how to custom lower this!"); 8101 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 8102 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 8103 case ISD::FPOWI: return LowerFPOWI(Op, *Subtarget, DAG); 8104 case ARMISD::WIN__DBZCHK: return SDValue(); 8105 } 8106 } 8107 8108 static void ReplaceLongIntrinsic(SDNode *N, SmallVectorImpl<SDValue> &Results, 8109 SelectionDAG &DAG) { 8110 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 8111 unsigned Opc = 0; 8112 if (IntNo == Intrinsic::arm_smlald) 8113 Opc = ARMISD::SMLALD; 8114 else if (IntNo == Intrinsic::arm_smlaldx) 8115 Opc = ARMISD::SMLALDX; 8116 else if (IntNo == Intrinsic::arm_smlsld) 8117 Opc = ARMISD::SMLSLD; 8118 else if (IntNo == Intrinsic::arm_smlsldx) 8119 Opc = ARMISD::SMLSLDX; 8120 else 8121 return; 8122 8123 SDLoc dl(N); 8124 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 8125 N->getOperand(3), 8126 DAG.getConstant(0, dl, MVT::i32)); 8127 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 8128 N->getOperand(3), 8129 DAG.getConstant(1, dl, MVT::i32)); 8130 8131 SDValue LongMul = DAG.getNode(Opc, dl, 8132 DAG.getVTList(MVT::i32, MVT::i32), 8133 N->getOperand(1), N->getOperand(2), 8134 Lo, Hi); 8135 Results.push_back(LongMul.getValue(0)); 8136 Results.push_back(LongMul.getValue(1)); 8137 } 8138 8139 /// ReplaceNodeResults - Replace the results of node with an illegal result 8140 /// type with new values built out of custom code. 8141 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 8142 SmallVectorImpl<SDValue> &Results, 8143 SelectionDAG &DAG) const { 8144 SDValue Res; 8145 switch (N->getOpcode()) { 8146 default: 8147 llvm_unreachable("Don't know how to custom expand this!"); 8148 case ISD::READ_REGISTER: 8149 ExpandREAD_REGISTER(N, Results, DAG); 8150 break; 8151 case ISD::BITCAST: 8152 Res = ExpandBITCAST(N, DAG, Subtarget); 8153 break; 8154 case ISD::SRL: 8155 case ISD::SRA: 8156 Res = Expand64BitShift(N, DAG, Subtarget); 8157 break; 8158 case ISD::SREM: 8159 case ISD::UREM: 8160 Res = LowerREM(N, DAG); 8161 break; 8162 case ISD::SDIVREM: 8163 case ISD::UDIVREM: 8164 Res = LowerDivRem(SDValue(N, 0), DAG); 8165 assert(Res.getNumOperands() == 2 && "DivRem needs two values"); 8166 Results.push_back(Res.getValue(0)); 8167 Results.push_back(Res.getValue(1)); 8168 return; 8169 case ISD::READCYCLECOUNTER: 8170 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 8171 return; 8172 case ISD::UDIV: 8173 case ISD::SDIV: 8174 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows"); 8175 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV, 8176 Results); 8177 case ISD::ATOMIC_CMP_SWAP: 8178 ReplaceCMP_SWAP_64Results(N, Results, DAG); 8179 return; 8180 case ISD::INTRINSIC_WO_CHAIN: 8181 return ReplaceLongIntrinsic(N, Results, DAG); 8182 case ISD::ABS: 8183 lowerABS(N, Results, DAG); 8184 return ; 8185 8186 } 8187 if (Res.getNode()) 8188 Results.push_back(Res); 8189 } 8190 8191 //===----------------------------------------------------------------------===// 8192 // ARM Scheduler Hooks 8193 //===----------------------------------------------------------------------===// 8194 8195 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 8196 /// registers the function context. 8197 void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI, 8198 MachineBasicBlock *MBB, 8199 MachineBasicBlock *DispatchBB, 8200 int FI) const { 8201 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 8202 "ROPI/RWPI not currently supported with SjLj"); 8203 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8204 DebugLoc dl = MI.getDebugLoc(); 8205 MachineFunction *MF = MBB->getParent(); 8206 MachineRegisterInfo *MRI = &MF->getRegInfo(); 8207 MachineConstantPool *MCP = MF->getConstantPool(); 8208 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 8209 const Function &F = MF->getFunction(); 8210 8211 bool isThumb = Subtarget->isThumb(); 8212 bool isThumb2 = Subtarget->isThumb2(); 8213 8214 unsigned PCLabelId = AFI->createPICLabelUId(); 8215 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 8216 ARMConstantPoolValue *CPV = 8217 ARMConstantPoolMBB::Create(F.getContext(), DispatchBB, PCLabelId, PCAdj); 8218 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 8219 8220 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass 8221 : &ARM::GPRRegClass; 8222 8223 // Grab constant pool and fixed stack memory operands. 8224 MachineMemOperand *CPMMO = 8225 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF), 8226 MachineMemOperand::MOLoad, 4, 4); 8227 8228 MachineMemOperand *FIMMOSt = 8229 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI), 8230 MachineMemOperand::MOStore, 4, 4); 8231 8232 // Load the address of the dispatch MBB into the jump buffer. 8233 if (isThumb2) { 8234 // Incoming value: jbuf 8235 // ldr.n r5, LCPI1_1 8236 // orr r5, r5, #1 8237 // add r5, pc 8238 // str r5, [$jbuf, #+4] ; &jbuf[1] 8239 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8240 BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 8241 .addConstantPoolIndex(CPI) 8242 .addMemOperand(CPMMO) 8243 .add(predOps(ARMCC::AL)); 8244 // Set the low bit because of thumb mode. 8245 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8246 BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 8247 .addReg(NewVReg1, RegState::Kill) 8248 .addImm(0x01) 8249 .add(predOps(ARMCC::AL)) 8250 .add(condCodeOp()); 8251 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8252 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 8253 .addReg(NewVReg2, RegState::Kill) 8254 .addImm(PCLabelId); 8255 BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 8256 .addReg(NewVReg3, RegState::Kill) 8257 .addFrameIndex(FI) 8258 .addImm(36) // &jbuf[1] :: pc 8259 .addMemOperand(FIMMOSt) 8260 .add(predOps(ARMCC::AL)); 8261 } else if (isThumb) { 8262 // Incoming value: jbuf 8263 // ldr.n r1, LCPI1_4 8264 // add r1, pc 8265 // mov r2, #1 8266 // orrs r1, r2 8267 // add r2, $jbuf, #+4 ; &jbuf[1] 8268 // str r1, [r2] 8269 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8270 BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 8271 .addConstantPoolIndex(CPI) 8272 .addMemOperand(CPMMO) 8273 .add(predOps(ARMCC::AL)); 8274 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8275 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 8276 .addReg(NewVReg1, RegState::Kill) 8277 .addImm(PCLabelId); 8278 // Set the low bit because of thumb mode. 8279 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8280 BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 8281 .addReg(ARM::CPSR, RegState::Define) 8282 .addImm(1) 8283 .add(predOps(ARMCC::AL)); 8284 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8285 BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 8286 .addReg(ARM::CPSR, RegState::Define) 8287 .addReg(NewVReg2, RegState::Kill) 8288 .addReg(NewVReg3, RegState::Kill) 8289 .add(predOps(ARMCC::AL)); 8290 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 8291 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5) 8292 .addFrameIndex(FI) 8293 .addImm(36); // &jbuf[1] :: pc 8294 BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 8295 .addReg(NewVReg4, RegState::Kill) 8296 .addReg(NewVReg5, RegState::Kill) 8297 .addImm(0) 8298 .addMemOperand(FIMMOSt) 8299 .add(predOps(ARMCC::AL)); 8300 } else { 8301 // Incoming value: jbuf 8302 // ldr r1, LCPI1_1 8303 // add r1, pc, r1 8304 // str r1, [$jbuf, #+4] ; &jbuf[1] 8305 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8306 BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 8307 .addConstantPoolIndex(CPI) 8308 .addImm(0) 8309 .addMemOperand(CPMMO) 8310 .add(predOps(ARMCC::AL)); 8311 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8312 BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 8313 .addReg(NewVReg1, RegState::Kill) 8314 .addImm(PCLabelId) 8315 .add(predOps(ARMCC::AL)); 8316 BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 8317 .addReg(NewVReg2, RegState::Kill) 8318 .addFrameIndex(FI) 8319 .addImm(36) // &jbuf[1] :: pc 8320 .addMemOperand(FIMMOSt) 8321 .add(predOps(ARMCC::AL)); 8322 } 8323 } 8324 8325 void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI, 8326 MachineBasicBlock *MBB) const { 8327 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8328 DebugLoc dl = MI.getDebugLoc(); 8329 MachineFunction *MF = MBB->getParent(); 8330 MachineRegisterInfo *MRI = &MF->getRegInfo(); 8331 MachineFrameInfo &MFI = MF->getFrameInfo(); 8332 int FI = MFI.getFunctionContextIndex(); 8333 8334 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass 8335 : &ARM::GPRnopcRegClass; 8336 8337 // Get a mapping of the call site numbers to all of the landing pads they're 8338 // associated with. 8339 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2>> CallSiteNumToLPad; 8340 unsigned MaxCSNum = 0; 8341 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 8342 ++BB) { 8343 if (!BB->isEHPad()) continue; 8344 8345 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 8346 // pad. 8347 for (MachineBasicBlock::iterator 8348 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 8349 if (!II->isEHLabel()) continue; 8350 8351 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 8352 if (!MF->hasCallSiteLandingPad(Sym)) continue; 8353 8354 SmallVectorImpl<unsigned> &CallSiteIdxs = MF->getCallSiteLandingPad(Sym); 8355 for (SmallVectorImpl<unsigned>::iterator 8356 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 8357 CSI != CSE; ++CSI) { 8358 CallSiteNumToLPad[*CSI].push_back(&*BB); 8359 MaxCSNum = std::max(MaxCSNum, *CSI); 8360 } 8361 break; 8362 } 8363 } 8364 8365 // Get an ordered list of the machine basic blocks for the jump table. 8366 std::vector<MachineBasicBlock*> LPadList; 8367 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs; 8368 LPadList.reserve(CallSiteNumToLPad.size()); 8369 for (unsigned I = 1; I <= MaxCSNum; ++I) { 8370 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 8371 for (SmallVectorImpl<MachineBasicBlock*>::iterator 8372 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 8373 LPadList.push_back(*II); 8374 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 8375 } 8376 } 8377 8378 assert(!LPadList.empty() && 8379 "No landing pad destinations for the dispatch jump table!"); 8380 8381 // Create the jump table and associated information. 8382 MachineJumpTableInfo *JTI = 8383 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 8384 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 8385 8386 // Create the MBBs for the dispatch code. 8387 8388 // Shove the dispatch's address into the return slot in the function context. 8389 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 8390 DispatchBB->setIsEHPad(); 8391 8392 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 8393 unsigned trap_opcode; 8394 if (Subtarget->isThumb()) 8395 trap_opcode = ARM::tTRAP; 8396 else 8397 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 8398 8399 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 8400 DispatchBB->addSuccessor(TrapBB); 8401 8402 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 8403 DispatchBB->addSuccessor(DispContBB); 8404 8405 // Insert and MBBs. 8406 MF->insert(MF->end(), DispatchBB); 8407 MF->insert(MF->end(), DispContBB); 8408 MF->insert(MF->end(), TrapBB); 8409 8410 // Insert code into the entry block that creates and registers the function 8411 // context. 8412 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 8413 8414 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand( 8415 MachinePointerInfo::getFixedStack(*MF, FI), 8416 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4); 8417 8418 MachineInstrBuilder MIB; 8419 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 8420 8421 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 8422 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 8423 8424 // Add a register mask with no preserved registers. This results in all 8425 // registers being marked as clobbered. This can't work if the dispatch block 8426 // is in a Thumb1 function and is linked with ARM code which uses the FP 8427 // registers, as there is no way to preserve the FP registers in Thumb1 mode. 8428 MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF)); 8429 8430 bool IsPositionIndependent = isPositionIndependent(); 8431 unsigned NumLPads = LPadList.size(); 8432 if (Subtarget->isThumb2()) { 8433 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8434 BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 8435 .addFrameIndex(FI) 8436 .addImm(4) 8437 .addMemOperand(FIMMOLd) 8438 .add(predOps(ARMCC::AL)); 8439 8440 if (NumLPads < 256) { 8441 BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 8442 .addReg(NewVReg1) 8443 .addImm(LPadList.size()) 8444 .add(predOps(ARMCC::AL)); 8445 } else { 8446 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8447 BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 8448 .addImm(NumLPads & 0xFFFF) 8449 .add(predOps(ARMCC::AL)); 8450 8451 unsigned VReg2 = VReg1; 8452 if ((NumLPads & 0xFFFF0000) != 0) { 8453 VReg2 = MRI->createVirtualRegister(TRC); 8454 BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 8455 .addReg(VReg1) 8456 .addImm(NumLPads >> 16) 8457 .add(predOps(ARMCC::AL)); 8458 } 8459 8460 BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 8461 .addReg(NewVReg1) 8462 .addReg(VReg2) 8463 .add(predOps(ARMCC::AL)); 8464 } 8465 8466 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 8467 .addMBB(TrapBB) 8468 .addImm(ARMCC::HI) 8469 .addReg(ARM::CPSR); 8470 8471 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8472 BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT), NewVReg3) 8473 .addJumpTableIndex(MJTI) 8474 .add(predOps(ARMCC::AL)); 8475 8476 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8477 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 8478 .addReg(NewVReg3, RegState::Kill) 8479 .addReg(NewVReg1) 8480 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)) 8481 .add(predOps(ARMCC::AL)) 8482 .add(condCodeOp()); 8483 8484 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 8485 .addReg(NewVReg4, RegState::Kill) 8486 .addReg(NewVReg1) 8487 .addJumpTableIndex(MJTI); 8488 } else if (Subtarget->isThumb()) { 8489 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8490 BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 8491 .addFrameIndex(FI) 8492 .addImm(1) 8493 .addMemOperand(FIMMOLd) 8494 .add(predOps(ARMCC::AL)); 8495 8496 if (NumLPads < 256) { 8497 BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 8498 .addReg(NewVReg1) 8499 .addImm(NumLPads) 8500 .add(predOps(ARMCC::AL)); 8501 } else { 8502 MachineConstantPool *ConstantPool = MF->getConstantPool(); 8503 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 8504 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 8505 8506 // MachineConstantPool wants an explicit alignment. 8507 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 8508 if (Align == 0) 8509 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 8510 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 8511 8512 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8513 BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 8514 .addReg(VReg1, RegState::Define) 8515 .addConstantPoolIndex(Idx) 8516 .add(predOps(ARMCC::AL)); 8517 BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 8518 .addReg(NewVReg1) 8519 .addReg(VReg1) 8520 .add(predOps(ARMCC::AL)); 8521 } 8522 8523 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 8524 .addMBB(TrapBB) 8525 .addImm(ARMCC::HI) 8526 .addReg(ARM::CPSR); 8527 8528 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8529 BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 8530 .addReg(ARM::CPSR, RegState::Define) 8531 .addReg(NewVReg1) 8532 .addImm(2) 8533 .add(predOps(ARMCC::AL)); 8534 8535 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8536 BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 8537 .addJumpTableIndex(MJTI) 8538 .add(predOps(ARMCC::AL)); 8539 8540 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8541 BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 8542 .addReg(ARM::CPSR, RegState::Define) 8543 .addReg(NewVReg2, RegState::Kill) 8544 .addReg(NewVReg3) 8545 .add(predOps(ARMCC::AL)); 8546 8547 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 8548 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 8549 8550 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 8551 BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 8552 .addReg(NewVReg4, RegState::Kill) 8553 .addImm(0) 8554 .addMemOperand(JTMMOLd) 8555 .add(predOps(ARMCC::AL)); 8556 8557 unsigned NewVReg6 = NewVReg5; 8558 if (IsPositionIndependent) { 8559 NewVReg6 = MRI->createVirtualRegister(TRC); 8560 BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 8561 .addReg(ARM::CPSR, RegState::Define) 8562 .addReg(NewVReg5, RegState::Kill) 8563 .addReg(NewVReg3) 8564 .add(predOps(ARMCC::AL)); 8565 } 8566 8567 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 8568 .addReg(NewVReg6, RegState::Kill) 8569 .addJumpTableIndex(MJTI); 8570 } else { 8571 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8572 BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 8573 .addFrameIndex(FI) 8574 .addImm(4) 8575 .addMemOperand(FIMMOLd) 8576 .add(predOps(ARMCC::AL)); 8577 8578 if (NumLPads < 256) { 8579 BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 8580 .addReg(NewVReg1) 8581 .addImm(NumLPads) 8582 .add(predOps(ARMCC::AL)); 8583 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 8584 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8585 BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 8586 .addImm(NumLPads & 0xFFFF) 8587 .add(predOps(ARMCC::AL)); 8588 8589 unsigned VReg2 = VReg1; 8590 if ((NumLPads & 0xFFFF0000) != 0) { 8591 VReg2 = MRI->createVirtualRegister(TRC); 8592 BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 8593 .addReg(VReg1) 8594 .addImm(NumLPads >> 16) 8595 .add(predOps(ARMCC::AL)); 8596 } 8597 8598 BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 8599 .addReg(NewVReg1) 8600 .addReg(VReg2) 8601 .add(predOps(ARMCC::AL)); 8602 } else { 8603 MachineConstantPool *ConstantPool = MF->getConstantPool(); 8604 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 8605 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 8606 8607 // MachineConstantPool wants an explicit alignment. 8608 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 8609 if (Align == 0) 8610 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 8611 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 8612 8613 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8614 BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 8615 .addReg(VReg1, RegState::Define) 8616 .addConstantPoolIndex(Idx) 8617 .addImm(0) 8618 .add(predOps(ARMCC::AL)); 8619 BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 8620 .addReg(NewVReg1) 8621 .addReg(VReg1, RegState::Kill) 8622 .add(predOps(ARMCC::AL)); 8623 } 8624 8625 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 8626 .addMBB(TrapBB) 8627 .addImm(ARMCC::HI) 8628 .addReg(ARM::CPSR); 8629 8630 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8631 BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 8632 .addReg(NewVReg1) 8633 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)) 8634 .add(predOps(ARMCC::AL)) 8635 .add(condCodeOp()); 8636 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8637 BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 8638 .addJumpTableIndex(MJTI) 8639 .add(predOps(ARMCC::AL)); 8640 8641 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 8642 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 8643 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 8644 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 8645 .addReg(NewVReg3, RegState::Kill) 8646 .addReg(NewVReg4) 8647 .addImm(0) 8648 .addMemOperand(JTMMOLd) 8649 .add(predOps(ARMCC::AL)); 8650 8651 if (IsPositionIndependent) { 8652 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 8653 .addReg(NewVReg5, RegState::Kill) 8654 .addReg(NewVReg4) 8655 .addJumpTableIndex(MJTI); 8656 } else { 8657 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 8658 .addReg(NewVReg5, RegState::Kill) 8659 .addJumpTableIndex(MJTI); 8660 } 8661 } 8662 8663 // Add the jump table entries as successors to the MBB. 8664 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 8665 for (std::vector<MachineBasicBlock*>::iterator 8666 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 8667 MachineBasicBlock *CurMBB = *I; 8668 if (SeenMBBs.insert(CurMBB).second) 8669 DispContBB->addSuccessor(CurMBB); 8670 } 8671 8672 // N.B. the order the invoke BBs are processed in doesn't matter here. 8673 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF); 8674 SmallVector<MachineBasicBlock*, 64> MBBLPads; 8675 for (MachineBasicBlock *BB : InvokeBBs) { 8676 8677 // Remove the landing pad successor from the invoke block and replace it 8678 // with the new dispatch block. 8679 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 8680 BB->succ_end()); 8681 while (!Successors.empty()) { 8682 MachineBasicBlock *SMBB = Successors.pop_back_val(); 8683 if (SMBB->isEHPad()) { 8684 BB->removeSuccessor(SMBB); 8685 MBBLPads.push_back(SMBB); 8686 } 8687 } 8688 8689 BB->addSuccessor(DispatchBB, BranchProbability::getZero()); 8690 BB->normalizeSuccProbs(); 8691 8692 // Find the invoke call and mark all of the callee-saved registers as 8693 // 'implicit defined' so that they're spilled. This prevents code from 8694 // moving instructions to before the EH block, where they will never be 8695 // executed. 8696 for (MachineBasicBlock::reverse_iterator 8697 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 8698 if (!II->isCall()) continue; 8699 8700 DenseMap<unsigned, bool> DefRegs; 8701 for (MachineInstr::mop_iterator 8702 OI = II->operands_begin(), OE = II->operands_end(); 8703 OI != OE; ++OI) { 8704 if (!OI->isReg()) continue; 8705 DefRegs[OI->getReg()] = true; 8706 } 8707 8708 MachineInstrBuilder MIB(*MF, &*II); 8709 8710 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 8711 unsigned Reg = SavedRegs[i]; 8712 if (Subtarget->isThumb2() && 8713 !ARM::tGPRRegClass.contains(Reg) && 8714 !ARM::hGPRRegClass.contains(Reg)) 8715 continue; 8716 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 8717 continue; 8718 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 8719 continue; 8720 if (!DefRegs[Reg]) 8721 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 8722 } 8723 8724 break; 8725 } 8726 } 8727 8728 // Mark all former landing pads as non-landing pads. The dispatch is the only 8729 // landing pad now. 8730 for (SmallVectorImpl<MachineBasicBlock*>::iterator 8731 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 8732 (*I)->setIsEHPad(false); 8733 8734 // The instruction is gone now. 8735 MI.eraseFromParent(); 8736 } 8737 8738 static 8739 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 8740 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 8741 E = MBB->succ_end(); I != E; ++I) 8742 if (*I != Succ) 8743 return *I; 8744 llvm_unreachable("Expecting a BB with two successors!"); 8745 } 8746 8747 /// Return the load opcode for a given load size. If load size >= 8, 8748 /// neon opcode will be returned. 8749 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) { 8750 if (LdSize >= 8) 8751 return LdSize == 16 ? ARM::VLD1q32wb_fixed 8752 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0; 8753 if (IsThumb1) 8754 return LdSize == 4 ? ARM::tLDRi 8755 : LdSize == 2 ? ARM::tLDRHi 8756 : LdSize == 1 ? ARM::tLDRBi : 0; 8757 if (IsThumb2) 8758 return LdSize == 4 ? ARM::t2LDR_POST 8759 : LdSize == 2 ? ARM::t2LDRH_POST 8760 : LdSize == 1 ? ARM::t2LDRB_POST : 0; 8761 return LdSize == 4 ? ARM::LDR_POST_IMM 8762 : LdSize == 2 ? ARM::LDRH_POST 8763 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0; 8764 } 8765 8766 /// Return the store opcode for a given store size. If store size >= 8, 8767 /// neon opcode will be returned. 8768 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) { 8769 if (StSize >= 8) 8770 return StSize == 16 ? ARM::VST1q32wb_fixed 8771 : StSize == 8 ? ARM::VST1d32wb_fixed : 0; 8772 if (IsThumb1) 8773 return StSize == 4 ? ARM::tSTRi 8774 : StSize == 2 ? ARM::tSTRHi 8775 : StSize == 1 ? ARM::tSTRBi : 0; 8776 if (IsThumb2) 8777 return StSize == 4 ? ARM::t2STR_POST 8778 : StSize == 2 ? ARM::t2STRH_POST 8779 : StSize == 1 ? ARM::t2STRB_POST : 0; 8780 return StSize == 4 ? ARM::STR_POST_IMM 8781 : StSize == 2 ? ARM::STRH_POST 8782 : StSize == 1 ? ARM::STRB_POST_IMM : 0; 8783 } 8784 8785 /// Emit a post-increment load operation with given size. The instructions 8786 /// will be added to BB at Pos. 8787 static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos, 8788 const TargetInstrInfo *TII, const DebugLoc &dl, 8789 unsigned LdSize, unsigned Data, unsigned AddrIn, 8790 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 8791 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2); 8792 assert(LdOpc != 0 && "Should have a load opcode"); 8793 if (LdSize >= 8) { 8794 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8795 .addReg(AddrOut, RegState::Define) 8796 .addReg(AddrIn) 8797 .addImm(0) 8798 .add(predOps(ARMCC::AL)); 8799 } else if (IsThumb1) { 8800 // load + update AddrIn 8801 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8802 .addReg(AddrIn) 8803 .addImm(0) 8804 .add(predOps(ARMCC::AL)); 8805 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut) 8806 .add(t1CondCodeOp()) 8807 .addReg(AddrIn) 8808 .addImm(LdSize) 8809 .add(predOps(ARMCC::AL)); 8810 } else if (IsThumb2) { 8811 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8812 .addReg(AddrOut, RegState::Define) 8813 .addReg(AddrIn) 8814 .addImm(LdSize) 8815 .add(predOps(ARMCC::AL)); 8816 } else { // arm 8817 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8818 .addReg(AddrOut, RegState::Define) 8819 .addReg(AddrIn) 8820 .addReg(0) 8821 .addImm(LdSize) 8822 .add(predOps(ARMCC::AL)); 8823 } 8824 } 8825 8826 /// Emit a post-increment store operation with given size. The instructions 8827 /// will be added to BB at Pos. 8828 static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos, 8829 const TargetInstrInfo *TII, const DebugLoc &dl, 8830 unsigned StSize, unsigned Data, unsigned AddrIn, 8831 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 8832 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2); 8833 assert(StOpc != 0 && "Should have a store opcode"); 8834 if (StSize >= 8) { 8835 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 8836 .addReg(AddrIn) 8837 .addImm(0) 8838 .addReg(Data) 8839 .add(predOps(ARMCC::AL)); 8840 } else if (IsThumb1) { 8841 // store + update AddrIn 8842 BuildMI(*BB, Pos, dl, TII->get(StOpc)) 8843 .addReg(Data) 8844 .addReg(AddrIn) 8845 .addImm(0) 8846 .add(predOps(ARMCC::AL)); 8847 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut) 8848 .add(t1CondCodeOp()) 8849 .addReg(AddrIn) 8850 .addImm(StSize) 8851 .add(predOps(ARMCC::AL)); 8852 } else if (IsThumb2) { 8853 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 8854 .addReg(Data) 8855 .addReg(AddrIn) 8856 .addImm(StSize) 8857 .add(predOps(ARMCC::AL)); 8858 } else { // arm 8859 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 8860 .addReg(Data) 8861 .addReg(AddrIn) 8862 .addReg(0) 8863 .addImm(StSize) 8864 .add(predOps(ARMCC::AL)); 8865 } 8866 } 8867 8868 MachineBasicBlock * 8869 ARMTargetLowering::EmitStructByval(MachineInstr &MI, 8870 MachineBasicBlock *BB) const { 8871 // This pseudo instruction has 3 operands: dst, src, size 8872 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 8873 // Otherwise, we will generate unrolled scalar copies. 8874 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8875 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8876 MachineFunction::iterator It = ++BB->getIterator(); 8877 8878 unsigned dest = MI.getOperand(0).getReg(); 8879 unsigned src = MI.getOperand(1).getReg(); 8880 unsigned SizeVal = MI.getOperand(2).getImm(); 8881 unsigned Align = MI.getOperand(3).getImm(); 8882 DebugLoc dl = MI.getDebugLoc(); 8883 8884 MachineFunction *MF = BB->getParent(); 8885 MachineRegisterInfo &MRI = MF->getRegInfo(); 8886 unsigned UnitSize = 0; 8887 const TargetRegisterClass *TRC = nullptr; 8888 const TargetRegisterClass *VecTRC = nullptr; 8889 8890 bool IsThumb1 = Subtarget->isThumb1Only(); 8891 bool IsThumb2 = Subtarget->isThumb2(); 8892 bool IsThumb = Subtarget->isThumb(); 8893 8894 if (Align & 1) { 8895 UnitSize = 1; 8896 } else if (Align & 2) { 8897 UnitSize = 2; 8898 } else { 8899 // Check whether we can use NEON instructions. 8900 if (!MF->getFunction().hasFnAttribute(Attribute::NoImplicitFloat) && 8901 Subtarget->hasNEON()) { 8902 if ((Align % 16 == 0) && SizeVal >= 16) 8903 UnitSize = 16; 8904 else if ((Align % 8 == 0) && SizeVal >= 8) 8905 UnitSize = 8; 8906 } 8907 // Can't use NEON instructions. 8908 if (UnitSize == 0) 8909 UnitSize = 4; 8910 } 8911 8912 // Select the correct opcode and register class for unit size load/store 8913 bool IsNeon = UnitSize >= 8; 8914 TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 8915 if (IsNeon) 8916 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass 8917 : UnitSize == 8 ? &ARM::DPRRegClass 8918 : nullptr; 8919 8920 unsigned BytesLeft = SizeVal % UnitSize; 8921 unsigned LoopSize = SizeVal - BytesLeft; 8922 8923 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 8924 // Use LDR and STR to copy. 8925 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 8926 // [destOut] = STR_POST(scratch, destIn, UnitSize) 8927 unsigned srcIn = src; 8928 unsigned destIn = dest; 8929 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 8930 unsigned srcOut = MRI.createVirtualRegister(TRC); 8931 unsigned destOut = MRI.createVirtualRegister(TRC); 8932 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 8933 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut, 8934 IsThumb1, IsThumb2); 8935 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut, 8936 IsThumb1, IsThumb2); 8937 srcIn = srcOut; 8938 destIn = destOut; 8939 } 8940 8941 // Handle the leftover bytes with LDRB and STRB. 8942 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 8943 // [destOut] = STRB_POST(scratch, destIn, 1) 8944 for (unsigned i = 0; i < BytesLeft; i++) { 8945 unsigned srcOut = MRI.createVirtualRegister(TRC); 8946 unsigned destOut = MRI.createVirtualRegister(TRC); 8947 unsigned scratch = MRI.createVirtualRegister(TRC); 8948 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut, 8949 IsThumb1, IsThumb2); 8950 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut, 8951 IsThumb1, IsThumb2); 8952 srcIn = srcOut; 8953 destIn = destOut; 8954 } 8955 MI.eraseFromParent(); // The instruction is gone now. 8956 return BB; 8957 } 8958 8959 // Expand the pseudo op to a loop. 8960 // thisMBB: 8961 // ... 8962 // movw varEnd, # --> with thumb2 8963 // movt varEnd, # 8964 // ldrcp varEnd, idx --> without thumb2 8965 // fallthrough --> loopMBB 8966 // loopMBB: 8967 // PHI varPhi, varEnd, varLoop 8968 // PHI srcPhi, src, srcLoop 8969 // PHI destPhi, dst, destLoop 8970 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 8971 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 8972 // subs varLoop, varPhi, #UnitSize 8973 // bne loopMBB 8974 // fallthrough --> exitMBB 8975 // exitMBB: 8976 // epilogue to handle left-over bytes 8977 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 8978 // [destOut] = STRB_POST(scratch, destLoop, 1) 8979 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 8980 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 8981 MF->insert(It, loopMBB); 8982 MF->insert(It, exitMBB); 8983 8984 // Transfer the remainder of BB and its successor edges to exitMBB. 8985 exitMBB->splice(exitMBB->begin(), BB, 8986 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8987 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8988 8989 // Load an immediate to varEnd. 8990 unsigned varEnd = MRI.createVirtualRegister(TRC); 8991 if (Subtarget->useMovt()) { 8992 unsigned Vtmp = varEnd; 8993 if ((LoopSize & 0xFFFF0000) != 0) 8994 Vtmp = MRI.createVirtualRegister(TRC); 8995 BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16), Vtmp) 8996 .addImm(LoopSize & 0xFFFF) 8997 .add(predOps(ARMCC::AL)); 8998 8999 if ((LoopSize & 0xFFFF0000) != 0) 9000 BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16), varEnd) 9001 .addReg(Vtmp) 9002 .addImm(LoopSize >> 16) 9003 .add(predOps(ARMCC::AL)); 9004 } else { 9005 MachineConstantPool *ConstantPool = MF->getConstantPool(); 9006 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 9007 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 9008 9009 // MachineConstantPool wants an explicit alignment. 9010 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 9011 if (Align == 0) 9012 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 9013 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 9014 MachineMemOperand *CPMMO = 9015 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF), 9016 MachineMemOperand::MOLoad, 4, 4); 9017 9018 if (IsThumb) 9019 BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)) 9020 .addReg(varEnd, RegState::Define) 9021 .addConstantPoolIndex(Idx) 9022 .add(predOps(ARMCC::AL)) 9023 .addMemOperand(CPMMO); 9024 else 9025 BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)) 9026 .addReg(varEnd, RegState::Define) 9027 .addConstantPoolIndex(Idx) 9028 .addImm(0) 9029 .add(predOps(ARMCC::AL)) 9030 .addMemOperand(CPMMO); 9031 } 9032 BB->addSuccessor(loopMBB); 9033 9034 // Generate the loop body: 9035 // varPhi = PHI(varLoop, varEnd) 9036 // srcPhi = PHI(srcLoop, src) 9037 // destPhi = PHI(destLoop, dst) 9038 MachineBasicBlock *entryBB = BB; 9039 BB = loopMBB; 9040 unsigned varLoop = MRI.createVirtualRegister(TRC); 9041 unsigned varPhi = MRI.createVirtualRegister(TRC); 9042 unsigned srcLoop = MRI.createVirtualRegister(TRC); 9043 unsigned srcPhi = MRI.createVirtualRegister(TRC); 9044 unsigned destLoop = MRI.createVirtualRegister(TRC); 9045 unsigned destPhi = MRI.createVirtualRegister(TRC); 9046 9047 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 9048 .addReg(varLoop).addMBB(loopMBB) 9049 .addReg(varEnd).addMBB(entryBB); 9050 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 9051 .addReg(srcLoop).addMBB(loopMBB) 9052 .addReg(src).addMBB(entryBB); 9053 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 9054 .addReg(destLoop).addMBB(loopMBB) 9055 .addReg(dest).addMBB(entryBB); 9056 9057 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 9058 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 9059 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 9060 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop, 9061 IsThumb1, IsThumb2); 9062 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop, 9063 IsThumb1, IsThumb2); 9064 9065 // Decrement loop variable by UnitSize. 9066 if (IsThumb1) { 9067 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop) 9068 .add(t1CondCodeOp()) 9069 .addReg(varPhi) 9070 .addImm(UnitSize) 9071 .add(predOps(ARMCC::AL)); 9072 } else { 9073 MachineInstrBuilder MIB = 9074 BuildMI(*BB, BB->end(), dl, 9075 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 9076 MIB.addReg(varPhi) 9077 .addImm(UnitSize) 9078 .add(predOps(ARMCC::AL)) 9079 .add(condCodeOp()); 9080 MIB->getOperand(5).setReg(ARM::CPSR); 9081 MIB->getOperand(5).setIsDef(true); 9082 } 9083 BuildMI(*BB, BB->end(), dl, 9084 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc)) 9085 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 9086 9087 // loopMBB can loop back to loopMBB or fall through to exitMBB. 9088 BB->addSuccessor(loopMBB); 9089 BB->addSuccessor(exitMBB); 9090 9091 // Add epilogue to handle BytesLeft. 9092 BB = exitMBB; 9093 auto StartOfExit = exitMBB->begin(); 9094 9095 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 9096 // [destOut] = STRB_POST(scratch, destLoop, 1) 9097 unsigned srcIn = srcLoop; 9098 unsigned destIn = destLoop; 9099 for (unsigned i = 0; i < BytesLeft; i++) { 9100 unsigned srcOut = MRI.createVirtualRegister(TRC); 9101 unsigned destOut = MRI.createVirtualRegister(TRC); 9102 unsigned scratch = MRI.createVirtualRegister(TRC); 9103 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut, 9104 IsThumb1, IsThumb2); 9105 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut, 9106 IsThumb1, IsThumb2); 9107 srcIn = srcOut; 9108 destIn = destOut; 9109 } 9110 9111 MI.eraseFromParent(); // The instruction is gone now. 9112 return BB; 9113 } 9114 9115 MachineBasicBlock * 9116 ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI, 9117 MachineBasicBlock *MBB) const { 9118 const TargetMachine &TM = getTargetMachine(); 9119 const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 9120 DebugLoc DL = MI.getDebugLoc(); 9121 9122 assert(Subtarget->isTargetWindows() && 9123 "__chkstk is only supported on Windows"); 9124 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode"); 9125 9126 // __chkstk takes the number of words to allocate on the stack in R4, and 9127 // returns the stack adjustment in number of bytes in R4. This will not 9128 // clober any other registers (other than the obvious lr). 9129 // 9130 // Although, technically, IP should be considered a register which may be 9131 // clobbered, the call itself will not touch it. Windows on ARM is a pure 9132 // thumb-2 environment, so there is no interworking required. As a result, we 9133 // do not expect a veneer to be emitted by the linker, clobbering IP. 9134 // 9135 // Each module receives its own copy of __chkstk, so no import thunk is 9136 // required, again, ensuring that IP is not clobbered. 9137 // 9138 // Finally, although some linkers may theoretically provide a trampoline for 9139 // out of range calls (which is quite common due to a 32M range limitation of 9140 // branches for Thumb), we can generate the long-call version via 9141 // -mcmodel=large, alleviating the need for the trampoline which may clobber 9142 // IP. 9143 9144 switch (TM.getCodeModel()) { 9145 case CodeModel::Tiny: 9146 llvm_unreachable("Tiny code model not available on ARM."); 9147 case CodeModel::Small: 9148 case CodeModel::Medium: 9149 case CodeModel::Kernel: 9150 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL)) 9151 .add(predOps(ARMCC::AL)) 9152 .addExternalSymbol("__chkstk") 9153 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 9154 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 9155 .addReg(ARM::R12, 9156 RegState::Implicit | RegState::Define | RegState::Dead) 9157 .addReg(ARM::CPSR, 9158 RegState::Implicit | RegState::Define | RegState::Dead); 9159 break; 9160 case CodeModel::Large: { 9161 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 9162 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass); 9163 9164 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg) 9165 .addExternalSymbol("__chkstk"); 9166 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr)) 9167 .add(predOps(ARMCC::AL)) 9168 .addReg(Reg, RegState::Kill) 9169 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 9170 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 9171 .addReg(ARM::R12, 9172 RegState::Implicit | RegState::Define | RegState::Dead) 9173 .addReg(ARM::CPSR, 9174 RegState::Implicit | RegState::Define | RegState::Dead); 9175 break; 9176 } 9177 } 9178 9179 BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), ARM::SP) 9180 .addReg(ARM::SP, RegState::Kill) 9181 .addReg(ARM::R4, RegState::Kill) 9182 .setMIFlags(MachineInstr::FrameSetup) 9183 .add(predOps(ARMCC::AL)) 9184 .add(condCodeOp()); 9185 9186 MI.eraseFromParent(); 9187 return MBB; 9188 } 9189 9190 MachineBasicBlock * 9191 ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI, 9192 MachineBasicBlock *MBB) const { 9193 DebugLoc DL = MI.getDebugLoc(); 9194 MachineFunction *MF = MBB->getParent(); 9195 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 9196 9197 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock(); 9198 MF->insert(++MBB->getIterator(), ContBB); 9199 ContBB->splice(ContBB->begin(), MBB, 9200 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 9201 ContBB->transferSuccessorsAndUpdatePHIs(MBB); 9202 MBB->addSuccessor(ContBB); 9203 9204 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 9205 BuildMI(TrapBB, DL, TII->get(ARM::t__brkdiv0)); 9206 MF->push_back(TrapBB); 9207 MBB->addSuccessor(TrapBB); 9208 9209 BuildMI(*MBB, MI, DL, TII->get(ARM::tCMPi8)) 9210 .addReg(MI.getOperand(0).getReg()) 9211 .addImm(0) 9212 .add(predOps(ARMCC::AL)); 9213 BuildMI(*MBB, MI, DL, TII->get(ARM::t2Bcc)) 9214 .addMBB(TrapBB) 9215 .addImm(ARMCC::EQ) 9216 .addReg(ARM::CPSR); 9217 9218 MI.eraseFromParent(); 9219 return ContBB; 9220 } 9221 9222 // The CPSR operand of SelectItr might be missing a kill marker 9223 // because there were multiple uses of CPSR, and ISel didn't know 9224 // which to mark. Figure out whether SelectItr should have had a 9225 // kill marker, and set it if it should. Returns the correct kill 9226 // marker value. 9227 static bool checkAndUpdateCPSRKill(MachineBasicBlock::iterator SelectItr, 9228 MachineBasicBlock* BB, 9229 const TargetRegisterInfo* TRI) { 9230 // Scan forward through BB for a use/def of CPSR. 9231 MachineBasicBlock::iterator miI(std::next(SelectItr)); 9232 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) { 9233 const MachineInstr& mi = *miI; 9234 if (mi.readsRegister(ARM::CPSR)) 9235 return false; 9236 if (mi.definesRegister(ARM::CPSR)) 9237 break; // Should have kill-flag - update below. 9238 } 9239 9240 // If we hit the end of the block, check whether CPSR is live into a 9241 // successor. 9242 if (miI == BB->end()) { 9243 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(), 9244 sEnd = BB->succ_end(); 9245 sItr != sEnd; ++sItr) { 9246 MachineBasicBlock* succ = *sItr; 9247 if (succ->isLiveIn(ARM::CPSR)) 9248 return false; 9249 } 9250 } 9251 9252 // We found a def, or hit the end of the basic block and CPSR wasn't live 9253 // out. SelectMI should have a kill flag on CPSR. 9254 SelectItr->addRegisterKilled(ARM::CPSR, TRI); 9255 return true; 9256 } 9257 9258 MachineBasicBlock * 9259 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 9260 MachineBasicBlock *BB) const { 9261 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 9262 DebugLoc dl = MI.getDebugLoc(); 9263 bool isThumb2 = Subtarget->isThumb2(); 9264 switch (MI.getOpcode()) { 9265 default: { 9266 MI.print(errs()); 9267 llvm_unreachable("Unexpected instr type to insert"); 9268 } 9269 9270 // Thumb1 post-indexed loads are really just single-register LDMs. 9271 case ARM::tLDR_postidx: { 9272 MachineOperand Def(MI.getOperand(1)); 9273 BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD)) 9274 .add(Def) // Rn_wb 9275 .add(MI.getOperand(2)) // Rn 9276 .add(MI.getOperand(3)) // PredImm 9277 .add(MI.getOperand(4)) // PredReg 9278 .add(MI.getOperand(0)) // Rt 9279 .cloneMemRefs(MI); 9280 MI.eraseFromParent(); 9281 return BB; 9282 } 9283 9284 // The Thumb2 pre-indexed stores have the same MI operands, they just 9285 // define them differently in the .td files from the isel patterns, so 9286 // they need pseudos. 9287 case ARM::t2STR_preidx: 9288 MI.setDesc(TII->get(ARM::t2STR_PRE)); 9289 return BB; 9290 case ARM::t2STRB_preidx: 9291 MI.setDesc(TII->get(ARM::t2STRB_PRE)); 9292 return BB; 9293 case ARM::t2STRH_preidx: 9294 MI.setDesc(TII->get(ARM::t2STRH_PRE)); 9295 return BB; 9296 9297 case ARM::STRi_preidx: 9298 case ARM::STRBi_preidx: { 9299 unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM 9300 : ARM::STRB_PRE_IMM; 9301 // Decode the offset. 9302 unsigned Offset = MI.getOperand(4).getImm(); 9303 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 9304 Offset = ARM_AM::getAM2Offset(Offset); 9305 if (isSub) 9306 Offset = -Offset; 9307 9308 MachineMemOperand *MMO = *MI.memoperands_begin(); 9309 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 9310 .add(MI.getOperand(0)) // Rn_wb 9311 .add(MI.getOperand(1)) // Rt 9312 .add(MI.getOperand(2)) // Rn 9313 .addImm(Offset) // offset (skip GPR==zero_reg) 9314 .add(MI.getOperand(5)) // pred 9315 .add(MI.getOperand(6)) 9316 .addMemOperand(MMO); 9317 MI.eraseFromParent(); 9318 return BB; 9319 } 9320 case ARM::STRr_preidx: 9321 case ARM::STRBr_preidx: 9322 case ARM::STRH_preidx: { 9323 unsigned NewOpc; 9324 switch (MI.getOpcode()) { 9325 default: llvm_unreachable("unexpected opcode!"); 9326 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 9327 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 9328 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 9329 } 9330 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 9331 for (unsigned i = 0; i < MI.getNumOperands(); ++i) 9332 MIB.add(MI.getOperand(i)); 9333 MI.eraseFromParent(); 9334 return BB; 9335 } 9336 9337 case ARM::tMOVCCr_pseudo: { 9338 // To "insert" a SELECT_CC instruction, we actually have to insert the 9339 // diamond control-flow pattern. The incoming instruction knows the 9340 // destination vreg to set, the condition code register to branch on, the 9341 // true/false values to select between, and a branch opcode to use. 9342 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 9343 MachineFunction::iterator It = ++BB->getIterator(); 9344 9345 // thisMBB: 9346 // ... 9347 // TrueVal = ... 9348 // cmpTY ccX, r1, r2 9349 // bCC copy1MBB 9350 // fallthrough --> copy0MBB 9351 MachineBasicBlock *thisMBB = BB; 9352 MachineFunction *F = BB->getParent(); 9353 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 9354 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 9355 F->insert(It, copy0MBB); 9356 F->insert(It, sinkMBB); 9357 9358 // Check whether CPSR is live past the tMOVCCr_pseudo. 9359 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); 9360 if (!MI.killsRegister(ARM::CPSR) && 9361 !checkAndUpdateCPSRKill(MI, thisMBB, TRI)) { 9362 copy0MBB->addLiveIn(ARM::CPSR); 9363 sinkMBB->addLiveIn(ARM::CPSR); 9364 } 9365 9366 // Transfer the remainder of BB and its successor edges to sinkMBB. 9367 sinkMBB->splice(sinkMBB->begin(), BB, 9368 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9369 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 9370 9371 BB->addSuccessor(copy0MBB); 9372 BB->addSuccessor(sinkMBB); 9373 9374 BuildMI(BB, dl, TII->get(ARM::tBcc)) 9375 .addMBB(sinkMBB) 9376 .addImm(MI.getOperand(3).getImm()) 9377 .addReg(MI.getOperand(4).getReg()); 9378 9379 // copy0MBB: 9380 // %FalseValue = ... 9381 // # fallthrough to sinkMBB 9382 BB = copy0MBB; 9383 9384 // Update machine-CFG edges 9385 BB->addSuccessor(sinkMBB); 9386 9387 // sinkMBB: 9388 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 9389 // ... 9390 BB = sinkMBB; 9391 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg()) 9392 .addReg(MI.getOperand(1).getReg()) 9393 .addMBB(copy0MBB) 9394 .addReg(MI.getOperand(2).getReg()) 9395 .addMBB(thisMBB); 9396 9397 MI.eraseFromParent(); // The pseudo instruction is gone now. 9398 return BB; 9399 } 9400 9401 case ARM::BCCi64: 9402 case ARM::BCCZi64: { 9403 // If there is an unconditional branch to the other successor, remove it. 9404 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9405 9406 // Compare both parts that make up the double comparison separately for 9407 // equality. 9408 bool RHSisZero = MI.getOpcode() == ARM::BCCZi64; 9409 9410 unsigned LHS1 = MI.getOperand(1).getReg(); 9411 unsigned LHS2 = MI.getOperand(2).getReg(); 9412 if (RHSisZero) { 9413 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 9414 .addReg(LHS1) 9415 .addImm(0) 9416 .add(predOps(ARMCC::AL)); 9417 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 9418 .addReg(LHS2).addImm(0) 9419 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 9420 } else { 9421 unsigned RHS1 = MI.getOperand(3).getReg(); 9422 unsigned RHS2 = MI.getOperand(4).getReg(); 9423 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 9424 .addReg(LHS1) 9425 .addReg(RHS1) 9426 .add(predOps(ARMCC::AL)); 9427 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 9428 .addReg(LHS2).addReg(RHS2) 9429 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 9430 } 9431 9432 MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB(); 9433 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 9434 if (MI.getOperand(0).getImm() == ARMCC::NE) 9435 std::swap(destMBB, exitMBB); 9436 9437 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 9438 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 9439 if (isThumb2) 9440 BuildMI(BB, dl, TII->get(ARM::t2B)) 9441 .addMBB(exitMBB) 9442 .add(predOps(ARMCC::AL)); 9443 else 9444 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 9445 9446 MI.eraseFromParent(); // The pseudo instruction is gone now. 9447 return BB; 9448 } 9449 9450 case ARM::Int_eh_sjlj_setjmp: 9451 case ARM::Int_eh_sjlj_setjmp_nofp: 9452 case ARM::tInt_eh_sjlj_setjmp: 9453 case ARM::t2Int_eh_sjlj_setjmp: 9454 case ARM::t2Int_eh_sjlj_setjmp_nofp: 9455 return BB; 9456 9457 case ARM::Int_eh_sjlj_setup_dispatch: 9458 EmitSjLjDispatchBlock(MI, BB); 9459 return BB; 9460 9461 case ARM::ABS: 9462 case ARM::t2ABS: { 9463 // To insert an ABS instruction, we have to insert the 9464 // diamond control-flow pattern. The incoming instruction knows the 9465 // source vreg to test against 0, the destination vreg to set, 9466 // the condition code register to branch on, the 9467 // true/false values to select between, and a branch opcode to use. 9468 // It transforms 9469 // V1 = ABS V0 9470 // into 9471 // V2 = MOVS V0 9472 // BCC (branch to SinkBB if V0 >= 0) 9473 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 9474 // SinkBB: V1 = PHI(V2, V3) 9475 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 9476 MachineFunction::iterator BBI = ++BB->getIterator(); 9477 MachineFunction *Fn = BB->getParent(); 9478 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 9479 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 9480 Fn->insert(BBI, RSBBB); 9481 Fn->insert(BBI, SinkBB); 9482 9483 unsigned int ABSSrcReg = MI.getOperand(1).getReg(); 9484 unsigned int ABSDstReg = MI.getOperand(0).getReg(); 9485 bool ABSSrcKIll = MI.getOperand(1).isKill(); 9486 bool isThumb2 = Subtarget->isThumb2(); 9487 MachineRegisterInfo &MRI = Fn->getRegInfo(); 9488 // In Thumb mode S must not be specified if source register is the SP or 9489 // PC and if destination register is the SP, so restrict register class 9490 unsigned NewRsbDstReg = 9491 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass); 9492 9493 // Transfer the remainder of BB and its successor edges to sinkMBB. 9494 SinkBB->splice(SinkBB->begin(), BB, 9495 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9496 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 9497 9498 BB->addSuccessor(RSBBB); 9499 BB->addSuccessor(SinkBB); 9500 9501 // fall through to SinkMBB 9502 RSBBB->addSuccessor(SinkBB); 9503 9504 // insert a cmp at the end of BB 9505 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 9506 .addReg(ABSSrcReg) 9507 .addImm(0) 9508 .add(predOps(ARMCC::AL)); 9509 9510 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 9511 BuildMI(BB, dl, 9512 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 9513 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 9514 9515 // insert rsbri in RSBBB 9516 // Note: BCC and rsbri will be converted into predicated rsbmi 9517 // by if-conversion pass 9518 BuildMI(*RSBBB, RSBBB->begin(), dl, 9519 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 9520 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0) 9521 .addImm(0) 9522 .add(predOps(ARMCC::AL)) 9523 .add(condCodeOp()); 9524 9525 // insert PHI in SinkBB, 9526 // reuse ABSDstReg to not change uses of ABS instruction 9527 BuildMI(*SinkBB, SinkBB->begin(), dl, 9528 TII->get(ARM::PHI), ABSDstReg) 9529 .addReg(NewRsbDstReg).addMBB(RSBBB) 9530 .addReg(ABSSrcReg).addMBB(BB); 9531 9532 // remove ABS instruction 9533 MI.eraseFromParent(); 9534 9535 // return last added BB 9536 return SinkBB; 9537 } 9538 case ARM::COPY_STRUCT_BYVAL_I32: 9539 ++NumLoopByVals; 9540 return EmitStructByval(MI, BB); 9541 case ARM::WIN__CHKSTK: 9542 return EmitLowered__chkstk(MI, BB); 9543 case ARM::WIN__DBZCHK: 9544 return EmitLowered__dbzchk(MI, BB); 9545 } 9546 } 9547 9548 /// Attaches vregs to MEMCPY that it will use as scratch registers 9549 /// when it is expanded into LDM/STM. This is done as a post-isel lowering 9550 /// instead of as a custom inserter because we need the use list from the SDNode. 9551 static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget, 9552 MachineInstr &MI, const SDNode *Node) { 9553 bool isThumb1 = Subtarget->isThumb1Only(); 9554 9555 DebugLoc DL = MI.getDebugLoc(); 9556 MachineFunction *MF = MI.getParent()->getParent(); 9557 MachineRegisterInfo &MRI = MF->getRegInfo(); 9558 MachineInstrBuilder MIB(*MF, MI); 9559 9560 // If the new dst/src is unused mark it as dead. 9561 if (!Node->hasAnyUseOfValue(0)) { 9562 MI.getOperand(0).setIsDead(true); 9563 } 9564 if (!Node->hasAnyUseOfValue(1)) { 9565 MI.getOperand(1).setIsDead(true); 9566 } 9567 9568 // The MEMCPY both defines and kills the scratch registers. 9569 for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) { 9570 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass 9571 : &ARM::GPRRegClass); 9572 MIB.addReg(TmpReg, RegState::Define|RegState::Dead); 9573 } 9574 } 9575 9576 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 9577 SDNode *Node) const { 9578 if (MI.getOpcode() == ARM::MEMCPY) { 9579 attachMEMCPYScratchRegs(Subtarget, MI, Node); 9580 return; 9581 } 9582 9583 const MCInstrDesc *MCID = &MI.getDesc(); 9584 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 9585 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 9586 // operand is still set to noreg. If needed, set the optional operand's 9587 // register to CPSR, and remove the redundant implicit def. 9588 // 9589 // e.g. ADCS (..., implicit-def CPSR) -> ADC (... opt:def CPSR). 9590 9591 // Rename pseudo opcodes. 9592 unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode()); 9593 unsigned ccOutIdx; 9594 if (NewOpc) { 9595 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo(); 9596 MCID = &TII->get(NewOpc); 9597 9598 assert(MCID->getNumOperands() == 9599 MI.getDesc().getNumOperands() + 5 - MI.getDesc().getSize() 9600 && "converted opcode should be the same except for cc_out" 9601 " (and, on Thumb1, pred)"); 9602 9603 MI.setDesc(*MCID); 9604 9605 // Add the optional cc_out operand 9606 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 9607 9608 // On Thumb1, move all input operands to the end, then add the predicate 9609 if (Subtarget->isThumb1Only()) { 9610 for (unsigned c = MCID->getNumOperands() - 4; c--;) { 9611 MI.addOperand(MI.getOperand(1)); 9612 MI.RemoveOperand(1); 9613 } 9614 9615 // Restore the ties 9616 for (unsigned i = MI.getNumOperands(); i--;) { 9617 const MachineOperand& op = MI.getOperand(i); 9618 if (op.isReg() && op.isUse()) { 9619 int DefIdx = MCID->getOperandConstraint(i, MCOI::TIED_TO); 9620 if (DefIdx != -1) 9621 MI.tieOperands(DefIdx, i); 9622 } 9623 } 9624 9625 MI.addOperand(MachineOperand::CreateImm(ARMCC::AL)); 9626 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/false)); 9627 ccOutIdx = 1; 9628 } else 9629 ccOutIdx = MCID->getNumOperands() - 1; 9630 } else 9631 ccOutIdx = MCID->getNumOperands() - 1; 9632 9633 // Any ARM instruction that sets the 's' bit should specify an optional 9634 // "cc_out" operand in the last operand position. 9635 if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 9636 assert(!NewOpc && "Optional cc_out operand required"); 9637 return; 9638 } 9639 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 9640 // since we already have an optional CPSR def. 9641 bool definesCPSR = false; 9642 bool deadCPSR = false; 9643 for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e; 9644 ++i) { 9645 const MachineOperand &MO = MI.getOperand(i); 9646 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 9647 definesCPSR = true; 9648 if (MO.isDead()) 9649 deadCPSR = true; 9650 MI.RemoveOperand(i); 9651 break; 9652 } 9653 } 9654 if (!definesCPSR) { 9655 assert(!NewOpc && "Optional cc_out operand required"); 9656 return; 9657 } 9658 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 9659 if (deadCPSR) { 9660 assert(!MI.getOperand(ccOutIdx).getReg() && 9661 "expect uninitialized optional cc_out operand"); 9662 // Thumb1 instructions must have the S bit even if the CPSR is dead. 9663 if (!Subtarget->isThumb1Only()) 9664 return; 9665 } 9666 9667 // If this instruction was defined with an optional CPSR def and its dag node 9668 // had a live implicit CPSR def, then activate the optional CPSR def. 9669 MachineOperand &MO = MI.getOperand(ccOutIdx); 9670 MO.setReg(ARM::CPSR); 9671 MO.setIsDef(true); 9672 } 9673 9674 //===----------------------------------------------------------------------===// 9675 // ARM Optimization Hooks 9676 //===----------------------------------------------------------------------===// 9677 9678 // Helper function that checks if N is a null or all ones constant. 9679 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 9680 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N); 9681 } 9682 9683 // Return true if N is conditionally 0 or all ones. 9684 // Detects these expressions where cc is an i1 value: 9685 // 9686 // (select cc 0, y) [AllOnes=0] 9687 // (select cc y, 0) [AllOnes=0] 9688 // (zext cc) [AllOnes=0] 9689 // (sext cc) [AllOnes=0/1] 9690 // (select cc -1, y) [AllOnes=1] 9691 // (select cc y, -1) [AllOnes=1] 9692 // 9693 // Invert is set when N is the null/all ones constant when CC is false. 9694 // OtherOp is set to the alternative value of N. 9695 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 9696 SDValue &CC, bool &Invert, 9697 SDValue &OtherOp, 9698 SelectionDAG &DAG) { 9699 switch (N->getOpcode()) { 9700 default: return false; 9701 case ISD::SELECT: { 9702 CC = N->getOperand(0); 9703 SDValue N1 = N->getOperand(1); 9704 SDValue N2 = N->getOperand(2); 9705 if (isZeroOrAllOnes(N1, AllOnes)) { 9706 Invert = false; 9707 OtherOp = N2; 9708 return true; 9709 } 9710 if (isZeroOrAllOnes(N2, AllOnes)) { 9711 Invert = true; 9712 OtherOp = N1; 9713 return true; 9714 } 9715 return false; 9716 } 9717 case ISD::ZERO_EXTEND: 9718 // (zext cc) can never be the all ones value. 9719 if (AllOnes) 9720 return false; 9721 LLVM_FALLTHROUGH; 9722 case ISD::SIGN_EXTEND: { 9723 SDLoc dl(N); 9724 EVT VT = N->getValueType(0); 9725 CC = N->getOperand(0); 9726 if (CC.getValueType() != MVT::i1 || CC.getOpcode() != ISD::SETCC) 9727 return false; 9728 Invert = !AllOnes; 9729 if (AllOnes) 9730 // When looking for an AllOnes constant, N is an sext, and the 'other' 9731 // value is 0. 9732 OtherOp = DAG.getConstant(0, dl, VT); 9733 else if (N->getOpcode() == ISD::ZERO_EXTEND) 9734 // When looking for a 0 constant, N can be zext or sext. 9735 OtherOp = DAG.getConstant(1, dl, VT); 9736 else 9737 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 9738 VT); 9739 return true; 9740 } 9741 } 9742 } 9743 9744 // Combine a constant select operand into its use: 9745 // 9746 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 9747 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 9748 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 9749 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 9750 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 9751 // 9752 // The transform is rejected if the select doesn't have a constant operand that 9753 // is null, or all ones when AllOnes is set. 9754 // 9755 // Also recognize sext/zext from i1: 9756 // 9757 // (add (zext cc), x) -> (select cc (add x, 1), x) 9758 // (add (sext cc), x) -> (select cc (add x, -1), x) 9759 // 9760 // These transformations eventually create predicated instructions. 9761 // 9762 // @param N The node to transform. 9763 // @param Slct The N operand that is a select. 9764 // @param OtherOp The other N operand (x above). 9765 // @param DCI Context. 9766 // @param AllOnes Require the select constant to be all ones instead of null. 9767 // @returns The new node, or SDValue() on failure. 9768 static 9769 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 9770 TargetLowering::DAGCombinerInfo &DCI, 9771 bool AllOnes = false) { 9772 SelectionDAG &DAG = DCI.DAG; 9773 EVT VT = N->getValueType(0); 9774 SDValue NonConstantVal; 9775 SDValue CCOp; 9776 bool SwapSelectOps; 9777 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 9778 NonConstantVal, DAG)) 9779 return SDValue(); 9780 9781 // Slct is now know to be the desired identity constant when CC is true. 9782 SDValue TrueVal = OtherOp; 9783 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 9784 OtherOp, NonConstantVal); 9785 // Unless SwapSelectOps says CC should be false. 9786 if (SwapSelectOps) 9787 std::swap(TrueVal, FalseVal); 9788 9789 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 9790 CCOp, TrueVal, FalseVal); 9791 } 9792 9793 // Attempt combineSelectAndUse on each operand of a commutative operator N. 9794 static 9795 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 9796 TargetLowering::DAGCombinerInfo &DCI) { 9797 SDValue N0 = N->getOperand(0); 9798 SDValue N1 = N->getOperand(1); 9799 if (N0.getNode()->hasOneUse()) 9800 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes)) 9801 return Result; 9802 if (N1.getNode()->hasOneUse()) 9803 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes)) 9804 return Result; 9805 return SDValue(); 9806 } 9807 9808 static bool IsVUZPShuffleNode(SDNode *N) { 9809 // VUZP shuffle node. 9810 if (N->getOpcode() == ARMISD::VUZP) 9811 return true; 9812 9813 // "VUZP" on i32 is an alias for VTRN. 9814 if (N->getOpcode() == ARMISD::VTRN && N->getValueType(0) == MVT::v2i32) 9815 return true; 9816 9817 return false; 9818 } 9819 9820 static SDValue AddCombineToVPADD(SDNode *N, SDValue N0, SDValue N1, 9821 TargetLowering::DAGCombinerInfo &DCI, 9822 const ARMSubtarget *Subtarget) { 9823 // Look for ADD(VUZP.0, VUZP.1). 9824 if (!IsVUZPShuffleNode(N0.getNode()) || N0.getNode() != N1.getNode() || 9825 N0 == N1) 9826 return SDValue(); 9827 9828 // Make sure the ADD is a 64-bit add; there is no 128-bit VPADD. 9829 if (!N->getValueType(0).is64BitVector()) 9830 return SDValue(); 9831 9832 // Generate vpadd. 9833 SelectionDAG &DAG = DCI.DAG; 9834 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9835 SDLoc dl(N); 9836 SDNode *Unzip = N0.getNode(); 9837 EVT VT = N->getValueType(0); 9838 9839 SmallVector<SDValue, 8> Ops; 9840 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpadd, dl, 9841 TLI.getPointerTy(DAG.getDataLayout()))); 9842 Ops.push_back(Unzip->getOperand(0)); 9843 Ops.push_back(Unzip->getOperand(1)); 9844 9845 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops); 9846 } 9847 9848 static SDValue AddCombineVUZPToVPADDL(SDNode *N, SDValue N0, SDValue N1, 9849 TargetLowering::DAGCombinerInfo &DCI, 9850 const ARMSubtarget *Subtarget) { 9851 // Check for two extended operands. 9852 if (!(N0.getOpcode() == ISD::SIGN_EXTEND && 9853 N1.getOpcode() == ISD::SIGN_EXTEND) && 9854 !(N0.getOpcode() == ISD::ZERO_EXTEND && 9855 N1.getOpcode() == ISD::ZERO_EXTEND)) 9856 return SDValue(); 9857 9858 SDValue N00 = N0.getOperand(0); 9859 SDValue N10 = N1.getOperand(0); 9860 9861 // Look for ADD(SEXT(VUZP.0), SEXT(VUZP.1)) 9862 if (!IsVUZPShuffleNode(N00.getNode()) || N00.getNode() != N10.getNode() || 9863 N00 == N10) 9864 return SDValue(); 9865 9866 // We only recognize Q register paddl here; this can't be reached until 9867 // after type legalization. 9868 if (!N00.getValueType().is64BitVector() || 9869 !N0.getValueType().is128BitVector()) 9870 return SDValue(); 9871 9872 // Generate vpaddl. 9873 SelectionDAG &DAG = DCI.DAG; 9874 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9875 SDLoc dl(N); 9876 EVT VT = N->getValueType(0); 9877 9878 SmallVector<SDValue, 8> Ops; 9879 // Form vpaddl.sN or vpaddl.uN depending on the kind of extension. 9880 unsigned Opcode; 9881 if (N0.getOpcode() == ISD::SIGN_EXTEND) 9882 Opcode = Intrinsic::arm_neon_vpaddls; 9883 else 9884 Opcode = Intrinsic::arm_neon_vpaddlu; 9885 Ops.push_back(DAG.getConstant(Opcode, dl, 9886 TLI.getPointerTy(DAG.getDataLayout()))); 9887 EVT ElemTy = N00.getValueType().getVectorElementType(); 9888 unsigned NumElts = VT.getVectorNumElements(); 9889 EVT ConcatVT = EVT::getVectorVT(*DAG.getContext(), ElemTy, NumElts * 2); 9890 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), ConcatVT, 9891 N00.getOperand(0), N00.getOperand(1)); 9892 Ops.push_back(Concat); 9893 9894 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops); 9895 } 9896 9897 // FIXME: This function shouldn't be necessary; if we lower BUILD_VECTOR in 9898 // an appropriate manner, we end up with ADD(VUZP(ZEXT(N))), which is 9899 // much easier to match. 9900 static SDValue 9901 AddCombineBUILD_VECTORToVPADDL(SDNode *N, SDValue N0, SDValue N1, 9902 TargetLowering::DAGCombinerInfo &DCI, 9903 const ARMSubtarget *Subtarget) { 9904 // Only perform optimization if after legalize, and if NEON is available. We 9905 // also expected both operands to be BUILD_VECTORs. 9906 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 9907 || N0.getOpcode() != ISD::BUILD_VECTOR 9908 || N1.getOpcode() != ISD::BUILD_VECTOR) 9909 return SDValue(); 9910 9911 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 9912 EVT VT = N->getValueType(0); 9913 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 9914 return SDValue(); 9915 9916 // Check that the vector operands are of the right form. 9917 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 9918 // operands, where N is the size of the formed vector. 9919 // Each EXTRACT_VECTOR should have the same input vector and odd or even 9920 // index such that we have a pair wise add pattern. 9921 9922 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 9923 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 9924 return SDValue(); 9925 SDValue Vec = N0->getOperand(0)->getOperand(0); 9926 SDNode *V = Vec.getNode(); 9927 unsigned nextIndex = 0; 9928 9929 // For each operands to the ADD which are BUILD_VECTORs, 9930 // check to see if each of their operands are an EXTRACT_VECTOR with 9931 // the same vector and appropriate index. 9932 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 9933 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 9934 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 9935 9936 SDValue ExtVec0 = N0->getOperand(i); 9937 SDValue ExtVec1 = N1->getOperand(i); 9938 9939 // First operand is the vector, verify its the same. 9940 if (V != ExtVec0->getOperand(0).getNode() || 9941 V != ExtVec1->getOperand(0).getNode()) 9942 return SDValue(); 9943 9944 // Second is the constant, verify its correct. 9945 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 9946 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 9947 9948 // For the constant, we want to see all the even or all the odd. 9949 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 9950 || C1->getZExtValue() != nextIndex+1) 9951 return SDValue(); 9952 9953 // Increment index. 9954 nextIndex+=2; 9955 } else 9956 return SDValue(); 9957 } 9958 9959 // Don't generate vpaddl+vmovn; we'll match it to vpadd later. Also make sure 9960 // we're using the entire input vector, otherwise there's a size/legality 9961 // mismatch somewhere. 9962 if (nextIndex != Vec.getValueType().getVectorNumElements() || 9963 Vec.getValueType().getVectorElementType() == VT.getVectorElementType()) 9964 return SDValue(); 9965 9966 // Create VPADDL node. 9967 SelectionDAG &DAG = DCI.DAG; 9968 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9969 9970 SDLoc dl(N); 9971 9972 // Build operand list. 9973 SmallVector<SDValue, 8> Ops; 9974 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl, 9975 TLI.getPointerTy(DAG.getDataLayout()))); 9976 9977 // Input is the vector. 9978 Ops.push_back(Vec); 9979 9980 // Get widened type and narrowed type. 9981 MVT widenType; 9982 unsigned numElem = VT.getVectorNumElements(); 9983 9984 EVT inputLaneType = Vec.getValueType().getVectorElementType(); 9985 switch (inputLaneType.getSimpleVT().SimpleTy) { 9986 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 9987 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 9988 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 9989 default: 9990 llvm_unreachable("Invalid vector element type for padd optimization."); 9991 } 9992 9993 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops); 9994 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE; 9995 return DAG.getNode(ExtOp, dl, VT, tmp); 9996 } 9997 9998 static SDValue findMUL_LOHI(SDValue V) { 9999 if (V->getOpcode() == ISD::UMUL_LOHI || 10000 V->getOpcode() == ISD::SMUL_LOHI) 10001 return V; 10002 return SDValue(); 10003 } 10004 10005 static SDValue AddCombineTo64BitSMLAL16(SDNode *AddcNode, SDNode *AddeNode, 10006 TargetLowering::DAGCombinerInfo &DCI, 10007 const ARMSubtarget *Subtarget) { 10008 if (Subtarget->isThumb()) { 10009 if (!Subtarget->hasDSP()) 10010 return SDValue(); 10011 } else if (!Subtarget->hasV5TEOps()) 10012 return SDValue(); 10013 10014 // SMLALBB, SMLALBT, SMLALTB, SMLALTT multiply two 16-bit values and 10015 // accumulates the product into a 64-bit value. The 16-bit values will 10016 // be sign extended somehow or SRA'd into 32-bit values 10017 // (addc (adde (mul 16bit, 16bit), lo), hi) 10018 SDValue Mul = AddcNode->getOperand(0); 10019 SDValue Lo = AddcNode->getOperand(1); 10020 if (Mul.getOpcode() != ISD::MUL) { 10021 Lo = AddcNode->getOperand(0); 10022 Mul = AddcNode->getOperand(1); 10023 if (Mul.getOpcode() != ISD::MUL) 10024 return SDValue(); 10025 } 10026 10027 SDValue SRA = AddeNode->getOperand(0); 10028 SDValue Hi = AddeNode->getOperand(1); 10029 if (SRA.getOpcode() != ISD::SRA) { 10030 SRA = AddeNode->getOperand(1); 10031 Hi = AddeNode->getOperand(0); 10032 if (SRA.getOpcode() != ISD::SRA) 10033 return SDValue(); 10034 } 10035 if (auto Const = dyn_cast<ConstantSDNode>(SRA.getOperand(1))) { 10036 if (Const->getZExtValue() != 31) 10037 return SDValue(); 10038 } else 10039 return SDValue(); 10040 10041 if (SRA.getOperand(0) != Mul) 10042 return SDValue(); 10043 10044 SelectionDAG &DAG = DCI.DAG; 10045 SDLoc dl(AddcNode); 10046 unsigned Opcode = 0; 10047 SDValue Op0; 10048 SDValue Op1; 10049 10050 if (isS16(Mul.getOperand(0), DAG) && isS16(Mul.getOperand(1), DAG)) { 10051 Opcode = ARMISD::SMLALBB; 10052 Op0 = Mul.getOperand(0); 10053 Op1 = Mul.getOperand(1); 10054 } else if (isS16(Mul.getOperand(0), DAG) && isSRA16(Mul.getOperand(1))) { 10055 Opcode = ARMISD::SMLALBT; 10056 Op0 = Mul.getOperand(0); 10057 Op1 = Mul.getOperand(1).getOperand(0); 10058 } else if (isSRA16(Mul.getOperand(0)) && isS16(Mul.getOperand(1), DAG)) { 10059 Opcode = ARMISD::SMLALTB; 10060 Op0 = Mul.getOperand(0).getOperand(0); 10061 Op1 = Mul.getOperand(1); 10062 } else if (isSRA16(Mul.getOperand(0)) && isSRA16(Mul.getOperand(1))) { 10063 Opcode = ARMISD::SMLALTT; 10064 Op0 = Mul->getOperand(0).getOperand(0); 10065 Op1 = Mul->getOperand(1).getOperand(0); 10066 } 10067 10068 if (!Op0 || !Op1) 10069 return SDValue(); 10070 10071 SDValue SMLAL = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32), 10072 Op0, Op1, Lo, Hi); 10073 // Replace the ADDs' nodes uses by the MLA node's values. 10074 SDValue HiMLALResult(SMLAL.getNode(), 1); 10075 SDValue LoMLALResult(SMLAL.getNode(), 0); 10076 10077 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 10078 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 10079 10080 // Return original node to notify the driver to stop replacing. 10081 SDValue resNode(AddcNode, 0); 10082 return resNode; 10083 } 10084 10085 static SDValue AddCombineTo64bitMLAL(SDNode *AddeSubeNode, 10086 TargetLowering::DAGCombinerInfo &DCI, 10087 const ARMSubtarget *Subtarget) { 10088 // Look for multiply add opportunities. 10089 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 10090 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 10091 // a glue link from the first add to the second add. 10092 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 10093 // a S/UMLAL instruction. 10094 // UMUL_LOHI 10095 // / :lo \ :hi 10096 // V \ [no multiline comment] 10097 // loAdd -> ADDC | 10098 // \ :carry / 10099 // V V 10100 // ADDE <- hiAdd 10101 // 10102 // In the special case where only the higher part of a signed result is used 10103 // and the add to the low part of the result of ISD::UMUL_LOHI adds or subtracts 10104 // a constant with the exact value of 0x80000000, we recognize we are dealing 10105 // with a "rounded multiply and add" (or subtract) and transform it into 10106 // either a ARMISD::SMMLAR or ARMISD::SMMLSR respectively. 10107 10108 assert((AddeSubeNode->getOpcode() == ARMISD::ADDE || 10109 AddeSubeNode->getOpcode() == ARMISD::SUBE) && 10110 "Expect an ADDE or SUBE"); 10111 10112 assert(AddeSubeNode->getNumOperands() == 3 && 10113 AddeSubeNode->getOperand(2).getValueType() == MVT::i32 && 10114 "ADDE node has the wrong inputs"); 10115 10116 // Check that we are chained to the right ADDC or SUBC node. 10117 SDNode *AddcSubcNode = AddeSubeNode->getOperand(2).getNode(); 10118 if ((AddeSubeNode->getOpcode() == ARMISD::ADDE && 10119 AddcSubcNode->getOpcode() != ARMISD::ADDC) || 10120 (AddeSubeNode->getOpcode() == ARMISD::SUBE && 10121 AddcSubcNode->getOpcode() != ARMISD::SUBC)) 10122 return SDValue(); 10123 10124 SDValue AddcSubcOp0 = AddcSubcNode->getOperand(0); 10125 SDValue AddcSubcOp1 = AddcSubcNode->getOperand(1); 10126 10127 // Check if the two operands are from the same mul_lohi node. 10128 if (AddcSubcOp0.getNode() == AddcSubcOp1.getNode()) 10129 return SDValue(); 10130 10131 assert(AddcSubcNode->getNumValues() == 2 && 10132 AddcSubcNode->getValueType(0) == MVT::i32 && 10133 "Expect ADDC with two result values. First: i32"); 10134 10135 // Check that the ADDC adds the low result of the S/UMUL_LOHI. If not, it 10136 // maybe a SMLAL which multiplies two 16-bit values. 10137 if (AddeSubeNode->getOpcode() == ARMISD::ADDE && 10138 AddcSubcOp0->getOpcode() != ISD::UMUL_LOHI && 10139 AddcSubcOp0->getOpcode() != ISD::SMUL_LOHI && 10140 AddcSubcOp1->getOpcode() != ISD::UMUL_LOHI && 10141 AddcSubcOp1->getOpcode() != ISD::SMUL_LOHI) 10142 return AddCombineTo64BitSMLAL16(AddcSubcNode, AddeSubeNode, DCI, Subtarget); 10143 10144 // Check for the triangle shape. 10145 SDValue AddeSubeOp0 = AddeSubeNode->getOperand(0); 10146 SDValue AddeSubeOp1 = AddeSubeNode->getOperand(1); 10147 10148 // Make sure that the ADDE/SUBE operands are not coming from the same node. 10149 if (AddeSubeOp0.getNode() == AddeSubeOp1.getNode()) 10150 return SDValue(); 10151 10152 // Find the MUL_LOHI node walking up ADDE/SUBE's operands. 10153 bool IsLeftOperandMUL = false; 10154 SDValue MULOp = findMUL_LOHI(AddeSubeOp0); 10155 if (MULOp == SDValue()) 10156 MULOp = findMUL_LOHI(AddeSubeOp1); 10157 else 10158 IsLeftOperandMUL = true; 10159 if (MULOp == SDValue()) 10160 return SDValue(); 10161 10162 // Figure out the right opcode. 10163 unsigned Opc = MULOp->getOpcode(); 10164 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 10165 10166 // Figure out the high and low input values to the MLAL node. 10167 SDValue *HiAddSub = nullptr; 10168 SDValue *LoMul = nullptr; 10169 SDValue *LowAddSub = nullptr; 10170 10171 // Ensure that ADDE/SUBE is from high result of ISD::xMUL_LOHI. 10172 if ((AddeSubeOp0 != MULOp.getValue(1)) && (AddeSubeOp1 != MULOp.getValue(1))) 10173 return SDValue(); 10174 10175 if (IsLeftOperandMUL) 10176 HiAddSub = &AddeSubeOp1; 10177 else 10178 HiAddSub = &AddeSubeOp0; 10179 10180 // Ensure that LoMul and LowAddSub are taken from correct ISD::SMUL_LOHI node 10181 // whose low result is fed to the ADDC/SUBC we are checking. 10182 10183 if (AddcSubcOp0 == MULOp.getValue(0)) { 10184 LoMul = &AddcSubcOp0; 10185 LowAddSub = &AddcSubcOp1; 10186 } 10187 if (AddcSubcOp1 == MULOp.getValue(0)) { 10188 LoMul = &AddcSubcOp1; 10189 LowAddSub = &AddcSubcOp0; 10190 } 10191 10192 if (!LoMul) 10193 return SDValue(); 10194 10195 // If HiAddSub is the same node as ADDC/SUBC or is a predecessor of ADDC/SUBC 10196 // the replacement below will create a cycle. 10197 if (AddcSubcNode == HiAddSub->getNode() || 10198 AddcSubcNode->isPredecessorOf(HiAddSub->getNode())) 10199 return SDValue(); 10200 10201 // Create the merged node. 10202 SelectionDAG &DAG = DCI.DAG; 10203 10204 // Start building operand list. 10205 SmallVector<SDValue, 8> Ops; 10206 Ops.push_back(LoMul->getOperand(0)); 10207 Ops.push_back(LoMul->getOperand(1)); 10208 10209 // Check whether we can use SMMLAR, SMMLSR or SMMULR instead. For this to be 10210 // the case, we must be doing signed multiplication and only use the higher 10211 // part of the result of the MLAL, furthermore the LowAddSub must be a constant 10212 // addition or subtraction with the value of 0x800000. 10213 if (Subtarget->hasV6Ops() && Subtarget->hasDSP() && Subtarget->useMulOps() && 10214 FinalOpc == ARMISD::SMLAL && !AddeSubeNode->hasAnyUseOfValue(1) && 10215 LowAddSub->getNode()->getOpcode() == ISD::Constant && 10216 static_cast<ConstantSDNode *>(LowAddSub->getNode())->getZExtValue() == 10217 0x80000000) { 10218 Ops.push_back(*HiAddSub); 10219 if (AddcSubcNode->getOpcode() == ARMISD::SUBC) { 10220 FinalOpc = ARMISD::SMMLSR; 10221 } else { 10222 FinalOpc = ARMISD::SMMLAR; 10223 } 10224 SDValue NewNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), MVT::i32, Ops); 10225 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), NewNode); 10226 10227 return SDValue(AddeSubeNode, 0); 10228 } else if (AddcSubcNode->getOpcode() == ARMISD::SUBC) 10229 // SMMLS is generated during instruction selection and the rest of this 10230 // function can not handle the case where AddcSubcNode is a SUBC. 10231 return SDValue(); 10232 10233 // Finish building the operand list for {U/S}MLAL 10234 Ops.push_back(*LowAddSub); 10235 Ops.push_back(*HiAddSub); 10236 10237 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), 10238 DAG.getVTList(MVT::i32, MVT::i32), Ops); 10239 10240 // Replace the ADDs' nodes uses by the MLA node's values. 10241 SDValue HiMLALResult(MLALNode.getNode(), 1); 10242 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), HiMLALResult); 10243 10244 SDValue LoMLALResult(MLALNode.getNode(), 0); 10245 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcSubcNode, 0), LoMLALResult); 10246 10247 // Return original node to notify the driver to stop replacing. 10248 return SDValue(AddeSubeNode, 0); 10249 } 10250 10251 static SDValue AddCombineTo64bitUMAAL(SDNode *AddeNode, 10252 TargetLowering::DAGCombinerInfo &DCI, 10253 const ARMSubtarget *Subtarget) { 10254 // UMAAL is similar to UMLAL except that it adds two unsigned values. 10255 // While trying to combine for the other MLAL nodes, first search for the 10256 // chance to use UMAAL. Check if Addc uses a node which has already 10257 // been combined into a UMLAL. The other pattern is UMLAL using Addc/Adde 10258 // as the addend, and it's handled in PerformUMLALCombine. 10259 10260 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP()) 10261 return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget); 10262 10263 // Check that we have a glued ADDC node. 10264 SDNode* AddcNode = AddeNode->getOperand(2).getNode(); 10265 if (AddcNode->getOpcode() != ARMISD::ADDC) 10266 return SDValue(); 10267 10268 // Find the converted UMAAL or quit if it doesn't exist. 10269 SDNode *UmlalNode = nullptr; 10270 SDValue AddHi; 10271 if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) { 10272 UmlalNode = AddcNode->getOperand(0).getNode(); 10273 AddHi = AddcNode->getOperand(1); 10274 } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) { 10275 UmlalNode = AddcNode->getOperand(1).getNode(); 10276 AddHi = AddcNode->getOperand(0); 10277 } else { 10278 return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget); 10279 } 10280 10281 // The ADDC should be glued to an ADDE node, which uses the same UMLAL as 10282 // the ADDC as well as Zero. 10283 if (!isNullConstant(UmlalNode->getOperand(3))) 10284 return SDValue(); 10285 10286 if ((isNullConstant(AddeNode->getOperand(0)) && 10287 AddeNode->getOperand(1).getNode() == UmlalNode) || 10288 (AddeNode->getOperand(0).getNode() == UmlalNode && 10289 isNullConstant(AddeNode->getOperand(1)))) { 10290 SelectionDAG &DAG = DCI.DAG; 10291 SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1), 10292 UmlalNode->getOperand(2), AddHi }; 10293 SDValue UMAAL = DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode), 10294 DAG.getVTList(MVT::i32, MVT::i32), Ops); 10295 10296 // Replace the ADDs' nodes uses by the UMAAL node's values. 10297 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1)); 10298 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0)); 10299 10300 // Return original node to notify the driver to stop replacing. 10301 return SDValue(AddeNode, 0); 10302 } 10303 return SDValue(); 10304 } 10305 10306 static SDValue PerformUMLALCombine(SDNode *N, SelectionDAG &DAG, 10307 const ARMSubtarget *Subtarget) { 10308 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP()) 10309 return SDValue(); 10310 10311 // Check that we have a pair of ADDC and ADDE as operands. 10312 // Both addends of the ADDE must be zero. 10313 SDNode* AddcNode = N->getOperand(2).getNode(); 10314 SDNode* AddeNode = N->getOperand(3).getNode(); 10315 if ((AddcNode->getOpcode() == ARMISD::ADDC) && 10316 (AddeNode->getOpcode() == ARMISD::ADDE) && 10317 isNullConstant(AddeNode->getOperand(0)) && 10318 isNullConstant(AddeNode->getOperand(1)) && 10319 (AddeNode->getOperand(2).getNode() == AddcNode)) 10320 return DAG.getNode(ARMISD::UMAAL, SDLoc(N), 10321 DAG.getVTList(MVT::i32, MVT::i32), 10322 {N->getOperand(0), N->getOperand(1), 10323 AddcNode->getOperand(0), AddcNode->getOperand(1)}); 10324 else 10325 return SDValue(); 10326 } 10327 10328 static SDValue PerformAddcSubcCombine(SDNode *N, 10329 TargetLowering::DAGCombinerInfo &DCI, 10330 const ARMSubtarget *Subtarget) { 10331 SelectionDAG &DAG(DCI.DAG); 10332 10333 if (N->getOpcode() == ARMISD::SUBC) { 10334 // (SUBC (ADDE 0, 0, C), 1) -> C 10335 SDValue LHS = N->getOperand(0); 10336 SDValue RHS = N->getOperand(1); 10337 if (LHS->getOpcode() == ARMISD::ADDE && 10338 isNullConstant(LHS->getOperand(0)) && 10339 isNullConstant(LHS->getOperand(1)) && isOneConstant(RHS)) { 10340 return DCI.CombineTo(N, SDValue(N, 0), LHS->getOperand(2)); 10341 } 10342 } 10343 10344 if (Subtarget->isThumb1Only()) { 10345 SDValue RHS = N->getOperand(1); 10346 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 10347 int32_t imm = C->getSExtValue(); 10348 if (imm < 0 && imm > std::numeric_limits<int>::min()) { 10349 SDLoc DL(N); 10350 RHS = DAG.getConstant(-imm, DL, MVT::i32); 10351 unsigned Opcode = (N->getOpcode() == ARMISD::ADDC) ? ARMISD::SUBC 10352 : ARMISD::ADDC; 10353 return DAG.getNode(Opcode, DL, N->getVTList(), N->getOperand(0), RHS); 10354 } 10355 } 10356 } 10357 10358 return SDValue(); 10359 } 10360 10361 static SDValue PerformAddeSubeCombine(SDNode *N, 10362 TargetLowering::DAGCombinerInfo &DCI, 10363 const ARMSubtarget *Subtarget) { 10364 if (Subtarget->isThumb1Only()) { 10365 SelectionDAG &DAG = DCI.DAG; 10366 SDValue RHS = N->getOperand(1); 10367 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 10368 int64_t imm = C->getSExtValue(); 10369 if (imm < 0) { 10370 SDLoc DL(N); 10371 10372 // The with-carry-in form matches bitwise not instead of the negation. 10373 // Effectively, the inverse interpretation of the carry flag already 10374 // accounts for part of the negation. 10375 RHS = DAG.getConstant(~imm, DL, MVT::i32); 10376 10377 unsigned Opcode = (N->getOpcode() == ARMISD::ADDE) ? ARMISD::SUBE 10378 : ARMISD::ADDE; 10379 return DAG.getNode(Opcode, DL, N->getVTList(), 10380 N->getOperand(0), RHS, N->getOperand(2)); 10381 } 10382 } 10383 } else if (N->getOperand(1)->getOpcode() == ISD::SMUL_LOHI) { 10384 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 10385 } 10386 return SDValue(); 10387 } 10388 10389 static SDValue PerformABSCombine(SDNode *N, 10390 TargetLowering::DAGCombinerInfo &DCI, 10391 const ARMSubtarget *Subtarget) { 10392 SDValue res; 10393 SelectionDAG &DAG = DCI.DAG; 10394 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10395 10396 if (TLI.isOperationLegal(N->getOpcode(), N->getValueType(0))) 10397 return SDValue(); 10398 10399 if (!TLI.expandABS(N, res, DAG)) 10400 return SDValue(); 10401 10402 return res; 10403 } 10404 10405 /// PerformADDECombine - Target-specific dag combine transform from 10406 /// ARMISD::ADDC, ARMISD::ADDE, and ISD::MUL_LOHI to MLAL or 10407 /// ARMISD::ADDC, ARMISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL 10408 static SDValue PerformADDECombine(SDNode *N, 10409 TargetLowering::DAGCombinerInfo &DCI, 10410 const ARMSubtarget *Subtarget) { 10411 // Only ARM and Thumb2 support UMLAL/SMLAL. 10412 if (Subtarget->isThumb1Only()) 10413 return PerformAddeSubeCombine(N, DCI, Subtarget); 10414 10415 // Only perform the checks after legalize when the pattern is available. 10416 if (DCI.isBeforeLegalize()) return SDValue(); 10417 10418 return AddCombineTo64bitUMAAL(N, DCI, Subtarget); 10419 } 10420 10421 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 10422 /// operands N0 and N1. This is a helper for PerformADDCombine that is 10423 /// called with the default operands, and if that fails, with commuted 10424 /// operands. 10425 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 10426 TargetLowering::DAGCombinerInfo &DCI, 10427 const ARMSubtarget *Subtarget){ 10428 // Attempt to create vpadd for this add. 10429 if (SDValue Result = AddCombineToVPADD(N, N0, N1, DCI, Subtarget)) 10430 return Result; 10431 10432 // Attempt to create vpaddl for this add. 10433 if (SDValue Result = AddCombineVUZPToVPADDL(N, N0, N1, DCI, Subtarget)) 10434 return Result; 10435 if (SDValue Result = AddCombineBUILD_VECTORToVPADDL(N, N0, N1, DCI, 10436 Subtarget)) 10437 return Result; 10438 10439 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 10440 if (N0.getNode()->hasOneUse()) 10441 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI)) 10442 return Result; 10443 return SDValue(); 10444 } 10445 10446 bool 10447 ARMTargetLowering::isDesirableToCommuteWithShift(const SDNode *N, 10448 CombineLevel Level) const { 10449 if (Level == BeforeLegalizeTypes) 10450 return true; 10451 10452 if (N->getOpcode() != ISD::SHL) 10453 return true; 10454 10455 if (Subtarget->isThumb1Only()) { 10456 // Avoid making expensive immediates by commuting shifts. (This logic 10457 // only applies to Thumb1 because ARM and Thumb2 immediates can be shifted 10458 // for free.) 10459 if (N->getOpcode() != ISD::SHL) 10460 return true; 10461 SDValue N1 = N->getOperand(0); 10462 if (N1->getOpcode() != ISD::ADD && N1->getOpcode() != ISD::AND && 10463 N1->getOpcode() != ISD::OR && N1->getOpcode() != ISD::XOR) 10464 return true; 10465 if (auto *Const = dyn_cast<ConstantSDNode>(N1->getOperand(1))) { 10466 if (Const->getAPIntValue().ult(256)) 10467 return false; 10468 if (N1->getOpcode() == ISD::ADD && Const->getAPIntValue().slt(0) && 10469 Const->getAPIntValue().sgt(-256)) 10470 return false; 10471 } 10472 return true; 10473 } 10474 10475 // Turn off commute-with-shift transform after legalization, so it doesn't 10476 // conflict with PerformSHLSimplify. (We could try to detect when 10477 // PerformSHLSimplify would trigger more precisely, but it isn't 10478 // really necessary.) 10479 return false; 10480 } 10481 10482 bool ARMTargetLowering::shouldFoldConstantShiftPairToMask( 10483 const SDNode *N, CombineLevel Level) const { 10484 if (!Subtarget->isThumb1Only()) 10485 return true; 10486 10487 if (Level == BeforeLegalizeTypes) 10488 return true; 10489 10490 return false; 10491 } 10492 10493 static SDValue PerformSHLSimplify(SDNode *N, 10494 TargetLowering::DAGCombinerInfo &DCI, 10495 const ARMSubtarget *ST) { 10496 // Allow the generic combiner to identify potential bswaps. 10497 if (DCI.isBeforeLegalize()) 10498 return SDValue(); 10499 10500 // DAG combiner will fold: 10501 // (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2) 10502 // (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2 10503 // Other code patterns that can be also be modified have the following form: 10504 // b + ((a << 1) | 510) 10505 // b + ((a << 1) & 510) 10506 // b + ((a << 1) ^ 510) 10507 // b + ((a << 1) + 510) 10508 10509 // Many instructions can perform the shift for free, but it requires both 10510 // the operands to be registers. If c1 << c2 is too large, a mov immediate 10511 // instruction will needed. So, unfold back to the original pattern if: 10512 // - if c1 and c2 are small enough that they don't require mov imms. 10513 // - the user(s) of the node can perform an shl 10514 10515 // No shifted operands for 16-bit instructions. 10516 if (ST->isThumb() && ST->isThumb1Only()) 10517 return SDValue(); 10518 10519 // Check that all the users could perform the shl themselves. 10520 for (auto U : N->uses()) { 10521 switch(U->getOpcode()) { 10522 default: 10523 return SDValue(); 10524 case ISD::SUB: 10525 case ISD::ADD: 10526 case ISD::AND: 10527 case ISD::OR: 10528 case ISD::XOR: 10529 case ISD::SETCC: 10530 case ARMISD::CMP: 10531 // Check that the user isn't already using a constant because there 10532 // aren't any instructions that support an immediate operand and a 10533 // shifted operand. 10534 if (isa<ConstantSDNode>(U->getOperand(0)) || 10535 isa<ConstantSDNode>(U->getOperand(1))) 10536 return SDValue(); 10537 10538 // Check that it's not already using a shift. 10539 if (U->getOperand(0).getOpcode() == ISD::SHL || 10540 U->getOperand(1).getOpcode() == ISD::SHL) 10541 return SDValue(); 10542 break; 10543 } 10544 } 10545 10546 if (N->getOpcode() != ISD::ADD && N->getOpcode() != ISD::OR && 10547 N->getOpcode() != ISD::XOR && N->getOpcode() != ISD::AND) 10548 return SDValue(); 10549 10550 if (N->getOperand(0).getOpcode() != ISD::SHL) 10551 return SDValue(); 10552 10553 SDValue SHL = N->getOperand(0); 10554 10555 auto *C1ShlC2 = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10556 auto *C2 = dyn_cast<ConstantSDNode>(SHL.getOperand(1)); 10557 if (!C1ShlC2 || !C2) 10558 return SDValue(); 10559 10560 APInt C2Int = C2->getAPIntValue(); 10561 APInt C1Int = C1ShlC2->getAPIntValue(); 10562 10563 // Check that performing a lshr will not lose any information. 10564 APInt Mask = APInt::getHighBitsSet(C2Int.getBitWidth(), 10565 C2Int.getBitWidth() - C2->getZExtValue()); 10566 if ((C1Int & Mask) != C1Int) 10567 return SDValue(); 10568 10569 // Shift the first constant. 10570 C1Int.lshrInPlace(C2Int); 10571 10572 // The immediates are encoded as an 8-bit value that can be rotated. 10573 auto LargeImm = [](const APInt &Imm) { 10574 unsigned Zeros = Imm.countLeadingZeros() + Imm.countTrailingZeros(); 10575 return Imm.getBitWidth() - Zeros > 8; 10576 }; 10577 10578 if (LargeImm(C1Int) || LargeImm(C2Int)) 10579 return SDValue(); 10580 10581 SelectionDAG &DAG = DCI.DAG; 10582 SDLoc dl(N); 10583 SDValue X = SHL.getOperand(0); 10584 SDValue BinOp = DAG.getNode(N->getOpcode(), dl, MVT::i32, X, 10585 DAG.getConstant(C1Int, dl, MVT::i32)); 10586 // Shift left to compensate for the lshr of C1Int. 10587 SDValue Res = DAG.getNode(ISD::SHL, dl, MVT::i32, BinOp, SHL.getOperand(1)); 10588 10589 LLVM_DEBUG(dbgs() << "Simplify shl use:\n"; SHL.getOperand(0).dump(); 10590 SHL.dump(); N->dump()); 10591 LLVM_DEBUG(dbgs() << "Into:\n"; X.dump(); BinOp.dump(); Res.dump()); 10592 return Res; 10593 } 10594 10595 10596 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 10597 /// 10598 static SDValue PerformADDCombine(SDNode *N, 10599 TargetLowering::DAGCombinerInfo &DCI, 10600 const ARMSubtarget *Subtarget) { 10601 SDValue N0 = N->getOperand(0); 10602 SDValue N1 = N->getOperand(1); 10603 10604 // Only works one way, because it needs an immediate operand. 10605 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 10606 return Result; 10607 10608 // First try with the default operand order. 10609 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget)) 10610 return Result; 10611 10612 // If that didn't work, try again with the operands commuted. 10613 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 10614 } 10615 10616 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 10617 /// 10618 static SDValue PerformSUBCombine(SDNode *N, 10619 TargetLowering::DAGCombinerInfo &DCI) { 10620 SDValue N0 = N->getOperand(0); 10621 SDValue N1 = N->getOperand(1); 10622 10623 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 10624 if (N1.getNode()->hasOneUse()) 10625 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI)) 10626 return Result; 10627 10628 return SDValue(); 10629 } 10630 10631 /// PerformVMULCombine 10632 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 10633 /// special multiplier accumulator forwarding. 10634 /// vmul d3, d0, d2 10635 /// vmla d3, d1, d2 10636 /// is faster than 10637 /// vadd d3, d0, d1 10638 /// vmul d3, d3, d2 10639 // However, for (A + B) * (A + B), 10640 // vadd d2, d0, d1 10641 // vmul d3, d0, d2 10642 // vmla d3, d1, d2 10643 // is slower than 10644 // vadd d2, d0, d1 10645 // vmul d3, d2, d2 10646 static SDValue PerformVMULCombine(SDNode *N, 10647 TargetLowering::DAGCombinerInfo &DCI, 10648 const ARMSubtarget *Subtarget) { 10649 if (!Subtarget->hasVMLxForwarding()) 10650 return SDValue(); 10651 10652 SelectionDAG &DAG = DCI.DAG; 10653 SDValue N0 = N->getOperand(0); 10654 SDValue N1 = N->getOperand(1); 10655 unsigned Opcode = N0.getOpcode(); 10656 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 10657 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 10658 Opcode = N1.getOpcode(); 10659 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 10660 Opcode != ISD::FADD && Opcode != ISD::FSUB) 10661 return SDValue(); 10662 std::swap(N0, N1); 10663 } 10664 10665 if (N0 == N1) 10666 return SDValue(); 10667 10668 EVT VT = N->getValueType(0); 10669 SDLoc DL(N); 10670 SDValue N00 = N0->getOperand(0); 10671 SDValue N01 = N0->getOperand(1); 10672 return DAG.getNode(Opcode, DL, VT, 10673 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 10674 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 10675 } 10676 10677 static SDValue PerformMULCombine(SDNode *N, 10678 TargetLowering::DAGCombinerInfo &DCI, 10679 const ARMSubtarget *Subtarget) { 10680 SelectionDAG &DAG = DCI.DAG; 10681 10682 if (Subtarget->isThumb1Only()) 10683 return SDValue(); 10684 10685 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 10686 return SDValue(); 10687 10688 EVT VT = N->getValueType(0); 10689 if (VT.is64BitVector() || VT.is128BitVector()) 10690 return PerformVMULCombine(N, DCI, Subtarget); 10691 if (VT != MVT::i32) 10692 return SDValue(); 10693 10694 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10695 if (!C) 10696 return SDValue(); 10697 10698 int64_t MulAmt = C->getSExtValue(); 10699 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 10700 10701 ShiftAmt = ShiftAmt & (32 - 1); 10702 SDValue V = N->getOperand(0); 10703 SDLoc DL(N); 10704 10705 SDValue Res; 10706 MulAmt >>= ShiftAmt; 10707 10708 if (MulAmt >= 0) { 10709 if (isPowerOf2_32(MulAmt - 1)) { 10710 // (mul x, 2^N + 1) => (add (shl x, N), x) 10711 Res = DAG.getNode(ISD::ADD, DL, VT, 10712 V, 10713 DAG.getNode(ISD::SHL, DL, VT, 10714 V, 10715 DAG.getConstant(Log2_32(MulAmt - 1), DL, 10716 MVT::i32))); 10717 } else if (isPowerOf2_32(MulAmt + 1)) { 10718 // (mul x, 2^N - 1) => (sub (shl x, N), x) 10719 Res = DAG.getNode(ISD::SUB, DL, VT, 10720 DAG.getNode(ISD::SHL, DL, VT, 10721 V, 10722 DAG.getConstant(Log2_32(MulAmt + 1), DL, 10723 MVT::i32)), 10724 V); 10725 } else 10726 return SDValue(); 10727 } else { 10728 uint64_t MulAmtAbs = -MulAmt; 10729 if (isPowerOf2_32(MulAmtAbs + 1)) { 10730 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 10731 Res = DAG.getNode(ISD::SUB, DL, VT, 10732 V, 10733 DAG.getNode(ISD::SHL, DL, VT, 10734 V, 10735 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL, 10736 MVT::i32))); 10737 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 10738 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 10739 Res = DAG.getNode(ISD::ADD, DL, VT, 10740 V, 10741 DAG.getNode(ISD::SHL, DL, VT, 10742 V, 10743 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL, 10744 MVT::i32))); 10745 Res = DAG.getNode(ISD::SUB, DL, VT, 10746 DAG.getConstant(0, DL, MVT::i32), Res); 10747 } else 10748 return SDValue(); 10749 } 10750 10751 if (ShiftAmt != 0) 10752 Res = DAG.getNode(ISD::SHL, DL, VT, 10753 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32)); 10754 10755 // Do not add new nodes to DAG combiner worklist. 10756 DCI.CombineTo(N, Res, false); 10757 return SDValue(); 10758 } 10759 10760 static SDValue CombineANDShift(SDNode *N, 10761 TargetLowering::DAGCombinerInfo &DCI, 10762 const ARMSubtarget *Subtarget) { 10763 // Allow DAGCombine to pattern-match before we touch the canonical form. 10764 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 10765 return SDValue(); 10766 10767 if (N->getValueType(0) != MVT::i32) 10768 return SDValue(); 10769 10770 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10771 if (!N1C) 10772 return SDValue(); 10773 10774 uint32_t C1 = (uint32_t)N1C->getZExtValue(); 10775 // Don't transform uxtb/uxth. 10776 if (C1 == 255 || C1 == 65535) 10777 return SDValue(); 10778 10779 SDNode *N0 = N->getOperand(0).getNode(); 10780 if (!N0->hasOneUse()) 10781 return SDValue(); 10782 10783 if (N0->getOpcode() != ISD::SHL && N0->getOpcode() != ISD::SRL) 10784 return SDValue(); 10785 10786 bool LeftShift = N0->getOpcode() == ISD::SHL; 10787 10788 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0->getOperand(1)); 10789 if (!N01C) 10790 return SDValue(); 10791 10792 uint32_t C2 = (uint32_t)N01C->getZExtValue(); 10793 if (!C2 || C2 >= 32) 10794 return SDValue(); 10795 10796 // Clear irrelevant bits in the mask. 10797 if (LeftShift) 10798 C1 &= (-1U << C2); 10799 else 10800 C1 &= (-1U >> C2); 10801 10802 SelectionDAG &DAG = DCI.DAG; 10803 SDLoc DL(N); 10804 10805 // We have a pattern of the form "(and (shl x, c2) c1)" or 10806 // "(and (srl x, c2) c1)", where c1 is a shifted mask. Try to 10807 // transform to a pair of shifts, to save materializing c1. 10808 10809 // First pattern: right shift, then mask off leading bits. 10810 // FIXME: Use demanded bits? 10811 if (!LeftShift && isMask_32(C1)) { 10812 uint32_t C3 = countLeadingZeros(C1); 10813 if (C2 < C3) { 10814 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0), 10815 DAG.getConstant(C3 - C2, DL, MVT::i32)); 10816 return DAG.getNode(ISD::SRL, DL, MVT::i32, SHL, 10817 DAG.getConstant(C3, DL, MVT::i32)); 10818 } 10819 } 10820 10821 // First pattern, reversed: left shift, then mask off trailing bits. 10822 if (LeftShift && isMask_32(~C1)) { 10823 uint32_t C3 = countTrailingZeros(C1); 10824 if (C2 < C3) { 10825 SDValue SHL = DAG.getNode(ISD::SRL, DL, MVT::i32, N0->getOperand(0), 10826 DAG.getConstant(C3 - C2, DL, MVT::i32)); 10827 return DAG.getNode(ISD::SHL, DL, MVT::i32, SHL, 10828 DAG.getConstant(C3, DL, MVT::i32)); 10829 } 10830 } 10831 10832 // Second pattern: left shift, then mask off leading bits. 10833 // FIXME: Use demanded bits? 10834 if (LeftShift && isShiftedMask_32(C1)) { 10835 uint32_t Trailing = countTrailingZeros(C1); 10836 uint32_t C3 = countLeadingZeros(C1); 10837 if (Trailing == C2 && C2 + C3 < 32) { 10838 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0), 10839 DAG.getConstant(C2 + C3, DL, MVT::i32)); 10840 return DAG.getNode(ISD::SRL, DL, MVT::i32, SHL, 10841 DAG.getConstant(C3, DL, MVT::i32)); 10842 } 10843 } 10844 10845 // Second pattern, reversed: right shift, then mask off trailing bits. 10846 // FIXME: Handle other patterns of known/demanded bits. 10847 if (!LeftShift && isShiftedMask_32(C1)) { 10848 uint32_t Leading = countLeadingZeros(C1); 10849 uint32_t C3 = countTrailingZeros(C1); 10850 if (Leading == C2 && C2 + C3 < 32) { 10851 SDValue SHL = DAG.getNode(ISD::SRL, DL, MVT::i32, N0->getOperand(0), 10852 DAG.getConstant(C2 + C3, DL, MVT::i32)); 10853 return DAG.getNode(ISD::SHL, DL, MVT::i32, SHL, 10854 DAG.getConstant(C3, DL, MVT::i32)); 10855 } 10856 } 10857 10858 // FIXME: Transform "(and (shl x, c2) c1)" -> 10859 // "(shl (and x, c1>>c2), c2)" if "c1 >> c2" is a cheaper immediate than 10860 // c1. 10861 return SDValue(); 10862 } 10863 10864 static SDValue PerformANDCombine(SDNode *N, 10865 TargetLowering::DAGCombinerInfo &DCI, 10866 const ARMSubtarget *Subtarget) { 10867 // Attempt to use immediate-form VBIC 10868 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 10869 SDLoc dl(N); 10870 EVT VT = N->getValueType(0); 10871 SelectionDAG &DAG = DCI.DAG; 10872 10873 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 10874 return SDValue(); 10875 10876 APInt SplatBits, SplatUndef; 10877 unsigned SplatBitSize; 10878 bool HasAnyUndefs; 10879 if (BVN && 10880 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 10881 if (SplatBitSize <= 64) { 10882 EVT VbicVT; 10883 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 10884 SplatUndef.getZExtValue(), SplatBitSize, 10885 DAG, dl, VbicVT, VT.is128BitVector(), 10886 OtherModImm); 10887 if (Val.getNode()) { 10888 SDValue Input = 10889 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 10890 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 10891 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 10892 } 10893 } 10894 } 10895 10896 if (!Subtarget->isThumb1Only()) { 10897 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 10898 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI)) 10899 return Result; 10900 10901 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 10902 return Result; 10903 } 10904 10905 if (Subtarget->isThumb1Only()) 10906 if (SDValue Result = CombineANDShift(N, DCI, Subtarget)) 10907 return Result; 10908 10909 return SDValue(); 10910 } 10911 10912 // Try combining OR nodes to SMULWB, SMULWT. 10913 static SDValue PerformORCombineToSMULWBT(SDNode *OR, 10914 TargetLowering::DAGCombinerInfo &DCI, 10915 const ARMSubtarget *Subtarget) { 10916 if (!Subtarget->hasV6Ops() || 10917 (Subtarget->isThumb() && 10918 (!Subtarget->hasThumb2() || !Subtarget->hasDSP()))) 10919 return SDValue(); 10920 10921 SDValue SRL = OR->getOperand(0); 10922 SDValue SHL = OR->getOperand(1); 10923 10924 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) { 10925 SRL = OR->getOperand(1); 10926 SHL = OR->getOperand(0); 10927 } 10928 if (!isSRL16(SRL) || !isSHL16(SHL)) 10929 return SDValue(); 10930 10931 // The first operands to the shifts need to be the two results from the 10932 // same smul_lohi node. 10933 if ((SRL.getOperand(0).getNode() != SHL.getOperand(0).getNode()) || 10934 SRL.getOperand(0).getOpcode() != ISD::SMUL_LOHI) 10935 return SDValue(); 10936 10937 SDNode *SMULLOHI = SRL.getOperand(0).getNode(); 10938 if (SRL.getOperand(0) != SDValue(SMULLOHI, 0) || 10939 SHL.getOperand(0) != SDValue(SMULLOHI, 1)) 10940 return SDValue(); 10941 10942 // Now we have: 10943 // (or (srl (smul_lohi ?, ?), 16), (shl (smul_lohi ?, ?), 16))) 10944 // For SMUL[B|T] smul_lohi will take a 32-bit and a 16-bit arguments. 10945 // For SMUWB the 16-bit value will signed extended somehow. 10946 // For SMULWT only the SRA is required. 10947 // Check both sides of SMUL_LOHI 10948 SDValue OpS16 = SMULLOHI->getOperand(0); 10949 SDValue OpS32 = SMULLOHI->getOperand(1); 10950 10951 SelectionDAG &DAG = DCI.DAG; 10952 if (!isS16(OpS16, DAG) && !isSRA16(OpS16)) { 10953 OpS16 = OpS32; 10954 OpS32 = SMULLOHI->getOperand(0); 10955 } 10956 10957 SDLoc dl(OR); 10958 unsigned Opcode = 0; 10959 if (isS16(OpS16, DAG)) 10960 Opcode = ARMISD::SMULWB; 10961 else if (isSRA16(OpS16)) { 10962 Opcode = ARMISD::SMULWT; 10963 OpS16 = OpS16->getOperand(0); 10964 } 10965 else 10966 return SDValue(); 10967 10968 SDValue Res = DAG.getNode(Opcode, dl, MVT::i32, OpS32, OpS16); 10969 DAG.ReplaceAllUsesOfValueWith(SDValue(OR, 0), Res); 10970 return SDValue(OR, 0); 10971 } 10972 10973 static SDValue PerformORCombineToBFI(SDNode *N, 10974 TargetLowering::DAGCombinerInfo &DCI, 10975 const ARMSubtarget *Subtarget) { 10976 // BFI is only available on V6T2+ 10977 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 10978 return SDValue(); 10979 10980 EVT VT = N->getValueType(0); 10981 SDValue N0 = N->getOperand(0); 10982 SDValue N1 = N->getOperand(1); 10983 SelectionDAG &DAG = DCI.DAG; 10984 SDLoc DL(N); 10985 // 1) or (and A, mask), val => ARMbfi A, val, mask 10986 // iff (val & mask) == val 10987 // 10988 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 10989 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 10990 // && mask == ~mask2 10991 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 10992 // && ~mask == mask2 10993 // (i.e., copy a bitfield value into another bitfield of the same width) 10994 10995 if (VT != MVT::i32) 10996 return SDValue(); 10997 10998 SDValue N00 = N0.getOperand(0); 10999 11000 // The value and the mask need to be constants so we can verify this is 11001 // actually a bitfield set. If the mask is 0xffff, we can do better 11002 // via a movt instruction, so don't use BFI in that case. 11003 SDValue MaskOp = N0.getOperand(1); 11004 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 11005 if (!MaskC) 11006 return SDValue(); 11007 unsigned Mask = MaskC->getZExtValue(); 11008 if (Mask == 0xffff) 11009 return SDValue(); 11010 SDValue Res; 11011 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 11012 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 11013 if (N1C) { 11014 unsigned Val = N1C->getZExtValue(); 11015 if ((Val & ~Mask) != Val) 11016 return SDValue(); 11017 11018 if (ARM::isBitFieldInvertedMask(Mask)) { 11019 Val >>= countTrailingZeros(~Mask); 11020 11021 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 11022 DAG.getConstant(Val, DL, MVT::i32), 11023 DAG.getConstant(Mask, DL, MVT::i32)); 11024 11025 DCI.CombineTo(N, Res, false); 11026 // Return value from the original node to inform the combiner than N is 11027 // now dead. 11028 return SDValue(N, 0); 11029 } 11030 } else if (N1.getOpcode() == ISD::AND) { 11031 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 11032 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 11033 if (!N11C) 11034 return SDValue(); 11035 unsigned Mask2 = N11C->getZExtValue(); 11036 11037 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 11038 // as is to match. 11039 if (ARM::isBitFieldInvertedMask(Mask) && 11040 (Mask == ~Mask2)) { 11041 // The pack halfword instruction works better for masks that fit it, 11042 // so use that when it's available. 11043 if (Subtarget->hasDSP() && 11044 (Mask == 0xffff || Mask == 0xffff0000)) 11045 return SDValue(); 11046 // 2a 11047 unsigned amt = countTrailingZeros(Mask2); 11048 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 11049 DAG.getConstant(amt, DL, MVT::i32)); 11050 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 11051 DAG.getConstant(Mask, DL, MVT::i32)); 11052 DCI.CombineTo(N, Res, false); 11053 // Return value from the original node to inform the combiner than N is 11054 // now dead. 11055 return SDValue(N, 0); 11056 } else if (ARM::isBitFieldInvertedMask(~Mask) && 11057 (~Mask == Mask2)) { 11058 // The pack halfword instruction works better for masks that fit it, 11059 // so use that when it's available. 11060 if (Subtarget->hasDSP() && 11061 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 11062 return SDValue(); 11063 // 2b 11064 unsigned lsb = countTrailingZeros(Mask); 11065 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 11066 DAG.getConstant(lsb, DL, MVT::i32)); 11067 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 11068 DAG.getConstant(Mask2, DL, MVT::i32)); 11069 DCI.CombineTo(N, Res, false); 11070 // Return value from the original node to inform the combiner than N is 11071 // now dead. 11072 return SDValue(N, 0); 11073 } 11074 } 11075 11076 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 11077 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 11078 ARM::isBitFieldInvertedMask(~Mask)) { 11079 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 11080 // where lsb(mask) == #shamt and masked bits of B are known zero. 11081 SDValue ShAmt = N00.getOperand(1); 11082 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 11083 unsigned LSB = countTrailingZeros(Mask); 11084 if (ShAmtC != LSB) 11085 return SDValue(); 11086 11087 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 11088 DAG.getConstant(~Mask, DL, MVT::i32)); 11089 11090 DCI.CombineTo(N, Res, false); 11091 // Return value from the original node to inform the combiner than N is 11092 // now dead. 11093 return SDValue(N, 0); 11094 } 11095 11096 return SDValue(); 11097 } 11098 11099 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 11100 static SDValue PerformORCombine(SDNode *N, 11101 TargetLowering::DAGCombinerInfo &DCI, 11102 const ARMSubtarget *Subtarget) { 11103 // Attempt to use immediate-form VORR 11104 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 11105 SDLoc dl(N); 11106 EVT VT = N->getValueType(0); 11107 SelectionDAG &DAG = DCI.DAG; 11108 11109 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 11110 return SDValue(); 11111 11112 APInt SplatBits, SplatUndef; 11113 unsigned SplatBitSize; 11114 bool HasAnyUndefs; 11115 if (BVN && Subtarget->hasNEON() && 11116 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 11117 if (SplatBitSize <= 64) { 11118 EVT VorrVT; 11119 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 11120 SplatUndef.getZExtValue(), SplatBitSize, 11121 DAG, dl, VorrVT, VT.is128BitVector(), 11122 OtherModImm); 11123 if (Val.getNode()) { 11124 SDValue Input = 11125 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 11126 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 11127 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 11128 } 11129 } 11130 } 11131 11132 if (!Subtarget->isThumb1Only()) { 11133 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 11134 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 11135 return Result; 11136 if (SDValue Result = PerformORCombineToSMULWBT(N, DCI, Subtarget)) 11137 return Result; 11138 } 11139 11140 SDValue N0 = N->getOperand(0); 11141 SDValue N1 = N->getOperand(1); 11142 11143 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 11144 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 11145 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 11146 11147 // The code below optimizes (or (and X, Y), Z). 11148 // The AND operand needs to have a single user to make these optimizations 11149 // profitable. 11150 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 11151 return SDValue(); 11152 11153 APInt SplatUndef; 11154 unsigned SplatBitSize; 11155 bool HasAnyUndefs; 11156 11157 APInt SplatBits0, SplatBits1; 11158 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 11159 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 11160 // Ensure that the second operand of both ands are constants 11161 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 11162 HasAnyUndefs) && !HasAnyUndefs) { 11163 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 11164 HasAnyUndefs) && !HasAnyUndefs) { 11165 // Ensure that the bit width of the constants are the same and that 11166 // the splat arguments are logical inverses as per the pattern we 11167 // are trying to simplify. 11168 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 11169 SplatBits0 == ~SplatBits1) { 11170 // Canonicalize the vector type to make instruction selection 11171 // simpler. 11172 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 11173 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 11174 N0->getOperand(1), 11175 N0->getOperand(0), 11176 N1->getOperand(0)); 11177 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 11178 } 11179 } 11180 } 11181 } 11182 11183 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 11184 // reasonable. 11185 if (N0.getOpcode() == ISD::AND && N0.hasOneUse()) { 11186 if (SDValue Res = PerformORCombineToBFI(N, DCI, Subtarget)) 11187 return Res; 11188 } 11189 11190 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 11191 return Result; 11192 11193 return SDValue(); 11194 } 11195 11196 static SDValue PerformXORCombine(SDNode *N, 11197 TargetLowering::DAGCombinerInfo &DCI, 11198 const ARMSubtarget *Subtarget) { 11199 EVT VT = N->getValueType(0); 11200 SelectionDAG &DAG = DCI.DAG; 11201 11202 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 11203 return SDValue(); 11204 11205 if (!Subtarget->isThumb1Only()) { 11206 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 11207 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 11208 return Result; 11209 11210 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 11211 return Result; 11212 } 11213 11214 return SDValue(); 11215 } 11216 11217 // ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it, 11218 // and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and 11219 // their position in "to" (Rd). 11220 static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) { 11221 assert(N->getOpcode() == ARMISD::BFI); 11222 11223 SDValue From = N->getOperand(1); 11224 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue(); 11225 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation()); 11226 11227 // If the Base came from a SHR #C, we can deduce that it is really testing bit 11228 // #C in the base of the SHR. 11229 if (From->getOpcode() == ISD::SRL && 11230 isa<ConstantSDNode>(From->getOperand(1))) { 11231 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue(); 11232 assert(Shift.getLimitedValue() < 32 && "Shift too large!"); 11233 FromMask <<= Shift.getLimitedValue(31); 11234 From = From->getOperand(0); 11235 } 11236 11237 return From; 11238 } 11239 11240 // If A and B contain one contiguous set of bits, does A | B == A . B? 11241 // 11242 // Neither A nor B must be zero. 11243 static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) { 11244 unsigned LastActiveBitInA = A.countTrailingZeros(); 11245 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1; 11246 return LastActiveBitInA - 1 == FirstActiveBitInB; 11247 } 11248 11249 static SDValue FindBFIToCombineWith(SDNode *N) { 11250 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with, 11251 // if one exists. 11252 APInt ToMask, FromMask; 11253 SDValue From = ParseBFI(N, ToMask, FromMask); 11254 SDValue To = N->getOperand(0); 11255 11256 // Now check for a compatible BFI to merge with. We can pass through BFIs that 11257 // aren't compatible, but not if they set the same bit in their destination as 11258 // we do (or that of any BFI we're going to combine with). 11259 SDValue V = To; 11260 APInt CombinedToMask = ToMask; 11261 while (V.getOpcode() == ARMISD::BFI) { 11262 APInt NewToMask, NewFromMask; 11263 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask); 11264 if (NewFrom != From) { 11265 // This BFI has a different base. Keep going. 11266 CombinedToMask |= NewToMask; 11267 V = V.getOperand(0); 11268 continue; 11269 } 11270 11271 // Do the written bits conflict with any we've seen so far? 11272 if ((NewToMask & CombinedToMask).getBoolValue()) 11273 // Conflicting bits - bail out because going further is unsafe. 11274 return SDValue(); 11275 11276 // Are the new bits contiguous when combined with the old bits? 11277 if (BitsProperlyConcatenate(ToMask, NewToMask) && 11278 BitsProperlyConcatenate(FromMask, NewFromMask)) 11279 return V; 11280 if (BitsProperlyConcatenate(NewToMask, ToMask) && 11281 BitsProperlyConcatenate(NewFromMask, FromMask)) 11282 return V; 11283 11284 // We've seen a write to some bits, so track it. 11285 CombinedToMask |= NewToMask; 11286 // Keep going... 11287 V = V.getOperand(0); 11288 } 11289 11290 return SDValue(); 11291 } 11292 11293 static SDValue PerformBFICombine(SDNode *N, 11294 TargetLowering::DAGCombinerInfo &DCI) { 11295 SDValue N1 = N->getOperand(1); 11296 if (N1.getOpcode() == ISD::AND) { 11297 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 11298 // the bits being cleared by the AND are not demanded by the BFI. 11299 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 11300 if (!N11C) 11301 return SDValue(); 11302 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 11303 unsigned LSB = countTrailingZeros(~InvMask); 11304 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 11305 assert(Width < 11306 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) && 11307 "undefined behavior"); 11308 unsigned Mask = (1u << Width) - 1; 11309 unsigned Mask2 = N11C->getZExtValue(); 11310 if ((Mask & (~Mask2)) == 0) 11311 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 11312 N->getOperand(0), N1.getOperand(0), 11313 N->getOperand(2)); 11314 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) { 11315 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes. 11316 // Keep track of any consecutive bits set that all come from the same base 11317 // value. We can combine these together into a single BFI. 11318 SDValue CombineBFI = FindBFIToCombineWith(N); 11319 if (CombineBFI == SDValue()) 11320 return SDValue(); 11321 11322 // We've found a BFI. 11323 APInt ToMask1, FromMask1; 11324 SDValue From1 = ParseBFI(N, ToMask1, FromMask1); 11325 11326 APInt ToMask2, FromMask2; 11327 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2); 11328 assert(From1 == From2); 11329 (void)From2; 11330 11331 // First, unlink CombineBFI. 11332 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0)); 11333 // Then create a new BFI, combining the two together. 11334 APInt NewFromMask = FromMask1 | FromMask2; 11335 APInt NewToMask = ToMask1 | ToMask2; 11336 11337 EVT VT = N->getValueType(0); 11338 SDLoc dl(N); 11339 11340 if (NewFromMask[0] == 0) 11341 From1 = DCI.DAG.getNode( 11342 ISD::SRL, dl, VT, From1, 11343 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT)); 11344 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1, 11345 DCI.DAG.getConstant(~NewToMask, dl, VT)); 11346 } 11347 return SDValue(); 11348 } 11349 11350 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 11351 /// ARMISD::VMOVRRD. 11352 static SDValue PerformVMOVRRDCombine(SDNode *N, 11353 TargetLowering::DAGCombinerInfo &DCI, 11354 const ARMSubtarget *Subtarget) { 11355 // vmovrrd(vmovdrr x, y) -> x,y 11356 SDValue InDouble = N->getOperand(0); 11357 if (InDouble.getOpcode() == ARMISD::VMOVDRR && Subtarget->hasFP64()) 11358 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 11359 11360 // vmovrrd(load f64) -> (load i32), (load i32) 11361 SDNode *InNode = InDouble.getNode(); 11362 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 11363 InNode->getValueType(0) == MVT::f64 && 11364 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 11365 !cast<LoadSDNode>(InNode)->isVolatile()) { 11366 // TODO: Should this be done for non-FrameIndex operands? 11367 LoadSDNode *LD = cast<LoadSDNode>(InNode); 11368 11369 SelectionDAG &DAG = DCI.DAG; 11370 SDLoc DL(LD); 11371 SDValue BasePtr = LD->getBasePtr(); 11372 SDValue NewLD1 = 11373 DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(), 11374 LD->getAlignment(), LD->getMemOperand()->getFlags()); 11375 11376 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 11377 DAG.getConstant(4, DL, MVT::i32)); 11378 SDValue NewLD2 = DAG.getLoad( 11379 MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, LD->getPointerInfo(), 11380 std::min(4U, LD->getAlignment() / 2), LD->getMemOperand()->getFlags()); 11381 11382 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 11383 if (DCI.DAG.getDataLayout().isBigEndian()) 11384 std::swap (NewLD1, NewLD2); 11385 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 11386 return Result; 11387 } 11388 11389 return SDValue(); 11390 } 11391 11392 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 11393 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 11394 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 11395 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 11396 SDValue Op0 = N->getOperand(0); 11397 SDValue Op1 = N->getOperand(1); 11398 if (Op0.getOpcode() == ISD::BITCAST) 11399 Op0 = Op0.getOperand(0); 11400 if (Op1.getOpcode() == ISD::BITCAST) 11401 Op1 = Op1.getOperand(0); 11402 if (Op0.getOpcode() == ARMISD::VMOVRRD && 11403 Op0.getNode() == Op1.getNode() && 11404 Op0.getResNo() == 0 && Op1.getResNo() == 1) 11405 return DAG.getNode(ISD::BITCAST, SDLoc(N), 11406 N->getValueType(0), Op0.getOperand(0)); 11407 return SDValue(); 11408 } 11409 11410 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 11411 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 11412 /// i64 vector to have f64 elements, since the value can then be loaded 11413 /// directly into a VFP register. 11414 static bool hasNormalLoadOperand(SDNode *N) { 11415 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 11416 for (unsigned i = 0; i < NumElts; ++i) { 11417 SDNode *Elt = N->getOperand(i).getNode(); 11418 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 11419 return true; 11420 } 11421 return false; 11422 } 11423 11424 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 11425 /// ISD::BUILD_VECTOR. 11426 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 11427 TargetLowering::DAGCombinerInfo &DCI, 11428 const ARMSubtarget *Subtarget) { 11429 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 11430 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 11431 // into a pair of GPRs, which is fine when the value is used as a scalar, 11432 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 11433 SelectionDAG &DAG = DCI.DAG; 11434 if (N->getNumOperands() == 2) 11435 if (SDValue RV = PerformVMOVDRRCombine(N, DAG)) 11436 return RV; 11437 11438 // Load i64 elements as f64 values so that type legalization does not split 11439 // them up into i32 values. 11440 EVT VT = N->getValueType(0); 11441 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 11442 return SDValue(); 11443 SDLoc dl(N); 11444 SmallVector<SDValue, 8> Ops; 11445 unsigned NumElts = VT.getVectorNumElements(); 11446 for (unsigned i = 0; i < NumElts; ++i) { 11447 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 11448 Ops.push_back(V); 11449 // Make the DAGCombiner fold the bitcast. 11450 DCI.AddToWorklist(V.getNode()); 11451 } 11452 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 11453 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops); 11454 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 11455 } 11456 11457 /// Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 11458 static SDValue 11459 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 11460 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 11461 // At that time, we may have inserted bitcasts from integer to float. 11462 // If these bitcasts have survived DAGCombine, change the lowering of this 11463 // BUILD_VECTOR in something more vector friendly, i.e., that does not 11464 // force to use floating point types. 11465 11466 // Make sure we can change the type of the vector. 11467 // This is possible iff: 11468 // 1. The vector is only used in a bitcast to a integer type. I.e., 11469 // 1.1. Vector is used only once. 11470 // 1.2. Use is a bit convert to an integer type. 11471 // 2. The size of its operands are 32-bits (64-bits are not legal). 11472 EVT VT = N->getValueType(0); 11473 EVT EltVT = VT.getVectorElementType(); 11474 11475 // Check 1.1. and 2. 11476 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 11477 return SDValue(); 11478 11479 // By construction, the input type must be float. 11480 assert(EltVT == MVT::f32 && "Unexpected type!"); 11481 11482 // Check 1.2. 11483 SDNode *Use = *N->use_begin(); 11484 if (Use->getOpcode() != ISD::BITCAST || 11485 Use->getValueType(0).isFloatingPoint()) 11486 return SDValue(); 11487 11488 // Check profitability. 11489 // Model is, if more than half of the relevant operands are bitcast from 11490 // i32, turn the build_vector into a sequence of insert_vector_elt. 11491 // Relevant operands are everything that is not statically 11492 // (i.e., at compile time) bitcasted. 11493 unsigned NumOfBitCastedElts = 0; 11494 unsigned NumElts = VT.getVectorNumElements(); 11495 unsigned NumOfRelevantElts = NumElts; 11496 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 11497 SDValue Elt = N->getOperand(Idx); 11498 if (Elt->getOpcode() == ISD::BITCAST) { 11499 // Assume only bit cast to i32 will go away. 11500 if (Elt->getOperand(0).getValueType() == MVT::i32) 11501 ++NumOfBitCastedElts; 11502 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt)) 11503 // Constants are statically casted, thus do not count them as 11504 // relevant operands. 11505 --NumOfRelevantElts; 11506 } 11507 11508 // Check if more than half of the elements require a non-free bitcast. 11509 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 11510 return SDValue(); 11511 11512 SelectionDAG &DAG = DCI.DAG; 11513 // Create the new vector type. 11514 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 11515 // Check if the type is legal. 11516 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11517 if (!TLI.isTypeLegal(VecVT)) 11518 return SDValue(); 11519 11520 // Combine: 11521 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 11522 // => BITCAST INSERT_VECTOR_ELT 11523 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 11524 // (BITCAST EN), N. 11525 SDValue Vec = DAG.getUNDEF(VecVT); 11526 SDLoc dl(N); 11527 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 11528 SDValue V = N->getOperand(Idx); 11529 if (V.isUndef()) 11530 continue; 11531 if (V.getOpcode() == ISD::BITCAST && 11532 V->getOperand(0).getValueType() == MVT::i32) 11533 // Fold obvious case. 11534 V = V.getOperand(0); 11535 else { 11536 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 11537 // Make the DAGCombiner fold the bitcasts. 11538 DCI.AddToWorklist(V.getNode()); 11539 } 11540 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32); 11541 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 11542 } 11543 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 11544 // Make the DAGCombiner fold the bitcasts. 11545 DCI.AddToWorklist(Vec.getNode()); 11546 return Vec; 11547 } 11548 11549 /// PerformInsertEltCombine - Target-specific dag combine xforms for 11550 /// ISD::INSERT_VECTOR_ELT. 11551 static SDValue PerformInsertEltCombine(SDNode *N, 11552 TargetLowering::DAGCombinerInfo &DCI) { 11553 // Bitcast an i64 load inserted into a vector to f64. 11554 // Otherwise, the i64 value will be legalized to a pair of i32 values. 11555 EVT VT = N->getValueType(0); 11556 SDNode *Elt = N->getOperand(1).getNode(); 11557 if (VT.getVectorElementType() != MVT::i64 || 11558 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 11559 return SDValue(); 11560 11561 SelectionDAG &DAG = DCI.DAG; 11562 SDLoc dl(N); 11563 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 11564 VT.getVectorNumElements()); 11565 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 11566 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 11567 // Make the DAGCombiner fold the bitcasts. 11568 DCI.AddToWorklist(Vec.getNode()); 11569 DCI.AddToWorklist(V.getNode()); 11570 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 11571 Vec, V, N->getOperand(2)); 11572 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 11573 } 11574 11575 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 11576 /// ISD::VECTOR_SHUFFLE. 11577 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 11578 // The LLVM shufflevector instruction does not require the shuffle mask 11579 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 11580 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 11581 // operands do not match the mask length, they are extended by concatenating 11582 // them with undef vectors. That is probably the right thing for other 11583 // targets, but for NEON it is better to concatenate two double-register 11584 // size vector operands into a single quad-register size vector. Do that 11585 // transformation here: 11586 // shuffle(concat(v1, undef), concat(v2, undef)) -> 11587 // shuffle(concat(v1, v2), undef) 11588 SDValue Op0 = N->getOperand(0); 11589 SDValue Op1 = N->getOperand(1); 11590 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 11591 Op1.getOpcode() != ISD::CONCAT_VECTORS || 11592 Op0.getNumOperands() != 2 || 11593 Op1.getNumOperands() != 2) 11594 return SDValue(); 11595 SDValue Concat0Op1 = Op0.getOperand(1); 11596 SDValue Concat1Op1 = Op1.getOperand(1); 11597 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef()) 11598 return SDValue(); 11599 // Skip the transformation if any of the types are illegal. 11600 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11601 EVT VT = N->getValueType(0); 11602 if (!TLI.isTypeLegal(VT) || 11603 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 11604 !TLI.isTypeLegal(Concat1Op1.getValueType())) 11605 return SDValue(); 11606 11607 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 11608 Op0.getOperand(0), Op1.getOperand(0)); 11609 // Translate the shuffle mask. 11610 SmallVector<int, 16> NewMask; 11611 unsigned NumElts = VT.getVectorNumElements(); 11612 unsigned HalfElts = NumElts/2; 11613 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 11614 for (unsigned n = 0; n < NumElts; ++n) { 11615 int MaskElt = SVN->getMaskElt(n); 11616 int NewElt = -1; 11617 if (MaskElt < (int)HalfElts) 11618 NewElt = MaskElt; 11619 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 11620 NewElt = HalfElts + MaskElt - NumElts; 11621 NewMask.push_back(NewElt); 11622 } 11623 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 11624 DAG.getUNDEF(VT), NewMask); 11625 } 11626 11627 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP, 11628 /// NEON load/store intrinsics, and generic vector load/stores, to merge 11629 /// base address updates. 11630 /// For generic load/stores, the memory type is assumed to be a vector. 11631 /// The caller is assumed to have checked legality. 11632 static SDValue CombineBaseUpdate(SDNode *N, 11633 TargetLowering::DAGCombinerInfo &DCI) { 11634 SelectionDAG &DAG = DCI.DAG; 11635 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 11636 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 11637 const bool isStore = N->getOpcode() == ISD::STORE; 11638 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1); 11639 SDValue Addr = N->getOperand(AddrOpIdx); 11640 MemSDNode *MemN = cast<MemSDNode>(N); 11641 SDLoc dl(N); 11642 11643 // Search for a use of the address operand that is an increment. 11644 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 11645 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 11646 SDNode *User = *UI; 11647 if (User->getOpcode() != ISD::ADD || 11648 UI.getUse().getResNo() != Addr.getResNo()) 11649 continue; 11650 11651 // Check that the add is independent of the load/store. Otherwise, folding 11652 // it would create a cycle. We can avoid searching through Addr as it's a 11653 // predecessor to both. 11654 SmallPtrSet<const SDNode *, 32> Visited; 11655 SmallVector<const SDNode *, 16> Worklist; 11656 Visited.insert(Addr.getNode()); 11657 Worklist.push_back(N); 11658 Worklist.push_back(User); 11659 if (SDNode::hasPredecessorHelper(N, Visited, Worklist) || 11660 SDNode::hasPredecessorHelper(User, Visited, Worklist)) 11661 continue; 11662 11663 // Find the new opcode for the updating load/store. 11664 bool isLoadOp = true; 11665 bool isLaneOp = false; 11666 unsigned NewOpc = 0; 11667 unsigned NumVecs = 0; 11668 if (isIntrinsic) { 11669 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 11670 switch (IntNo) { 11671 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 11672 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 11673 NumVecs = 1; break; 11674 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 11675 NumVecs = 2; break; 11676 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 11677 NumVecs = 3; break; 11678 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 11679 NumVecs = 4; break; 11680 case Intrinsic::arm_neon_vld2dup: 11681 case Intrinsic::arm_neon_vld3dup: 11682 case Intrinsic::arm_neon_vld4dup: 11683 // TODO: Support updating VLDxDUP nodes. For now, we just skip 11684 // combining base updates for such intrinsics. 11685 continue; 11686 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 11687 NumVecs = 2; isLaneOp = true; break; 11688 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 11689 NumVecs = 3; isLaneOp = true; break; 11690 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 11691 NumVecs = 4; isLaneOp = true; break; 11692 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 11693 NumVecs = 1; isLoadOp = false; break; 11694 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 11695 NumVecs = 2; isLoadOp = false; break; 11696 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 11697 NumVecs = 3; isLoadOp = false; break; 11698 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 11699 NumVecs = 4; isLoadOp = false; break; 11700 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 11701 NumVecs = 2; isLoadOp = false; isLaneOp = true; break; 11702 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 11703 NumVecs = 3; isLoadOp = false; isLaneOp = true; break; 11704 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 11705 NumVecs = 4; isLoadOp = false; isLaneOp = true; break; 11706 } 11707 } else { 11708 isLaneOp = true; 11709 switch (N->getOpcode()) { 11710 default: llvm_unreachable("unexpected opcode for Neon base update"); 11711 case ARMISD::VLD1DUP: NewOpc = ARMISD::VLD1DUP_UPD; NumVecs = 1; break; 11712 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 11713 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 11714 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 11715 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD; 11716 NumVecs = 1; isLaneOp = false; break; 11717 case ISD::STORE: NewOpc = ARMISD::VST1_UPD; 11718 NumVecs = 1; isLaneOp = false; isLoadOp = false; break; 11719 } 11720 } 11721 11722 // Find the size of memory referenced by the load/store. 11723 EVT VecTy; 11724 if (isLoadOp) { 11725 VecTy = N->getValueType(0); 11726 } else if (isIntrinsic) { 11727 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 11728 } else { 11729 assert(isStore && "Node has to be a load, a store, or an intrinsic!"); 11730 VecTy = N->getOperand(1).getValueType(); 11731 } 11732 11733 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 11734 if (isLaneOp) 11735 NumBytes /= VecTy.getVectorNumElements(); 11736 11737 // If the increment is a constant, it must match the memory ref size. 11738 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 11739 ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode()); 11740 if (NumBytes >= 3 * 16 && (!CInc || CInc->getZExtValue() != NumBytes)) { 11741 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 11742 // separate instructions that make it harder to use a non-constant update. 11743 continue; 11744 } 11745 11746 // OK, we found an ADD we can fold into the base update. 11747 // Now, create a _UPD node, taking care of not breaking alignment. 11748 11749 EVT AlignedVecTy = VecTy; 11750 unsigned Alignment = MemN->getAlignment(); 11751 11752 // If this is a less-than-standard-aligned load/store, change the type to 11753 // match the standard alignment. 11754 // The alignment is overlooked when selecting _UPD variants; and it's 11755 // easier to introduce bitcasts here than fix that. 11756 // There are 3 ways to get to this base-update combine: 11757 // - intrinsics: they are assumed to be properly aligned (to the standard 11758 // alignment of the memory type), so we don't need to do anything. 11759 // - ARMISD::VLDx nodes: they are only generated from the aforementioned 11760 // intrinsics, so, likewise, there's nothing to do. 11761 // - generic load/store instructions: the alignment is specified as an 11762 // explicit operand, rather than implicitly as the standard alignment 11763 // of the memory type (like the intrisics). We need to change the 11764 // memory type to match the explicit alignment. That way, we don't 11765 // generate non-standard-aligned ARMISD::VLDx nodes. 11766 if (isa<LSBaseSDNode>(N)) { 11767 if (Alignment == 0) 11768 Alignment = 1; 11769 if (Alignment < VecTy.getScalarSizeInBits() / 8) { 11770 MVT EltTy = MVT::getIntegerVT(Alignment * 8); 11771 assert(NumVecs == 1 && "Unexpected multi-element generic load/store."); 11772 assert(!isLaneOp && "Unexpected generic load/store lane."); 11773 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8); 11774 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts); 11775 } 11776 // Don't set an explicit alignment on regular load/stores that we want 11777 // to transform to VLD/VST 1_UPD nodes. 11778 // This matches the behavior of regular load/stores, which only get an 11779 // explicit alignment if the MMO alignment is larger than the standard 11780 // alignment of the memory type. 11781 // Intrinsics, however, always get an explicit alignment, set to the 11782 // alignment of the MMO. 11783 Alignment = 1; 11784 } 11785 11786 // Create the new updating load/store node. 11787 // First, create an SDVTList for the new updating node's results. 11788 EVT Tys[6]; 11789 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0); 11790 unsigned n; 11791 for (n = 0; n < NumResultVecs; ++n) 11792 Tys[n] = AlignedVecTy; 11793 Tys[n++] = MVT::i32; 11794 Tys[n] = MVT::Other; 11795 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2)); 11796 11797 // Then, gather the new node's operands. 11798 SmallVector<SDValue, 8> Ops; 11799 Ops.push_back(N->getOperand(0)); // incoming chain 11800 Ops.push_back(N->getOperand(AddrOpIdx)); 11801 Ops.push_back(Inc); 11802 11803 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) { 11804 // Try to match the intrinsic's signature 11805 Ops.push_back(StN->getValue()); 11806 } else { 11807 // Loads (and of course intrinsics) match the intrinsics' signature, 11808 // so just add all but the alignment operand. 11809 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i) 11810 Ops.push_back(N->getOperand(i)); 11811 } 11812 11813 // For all node types, the alignment operand is always the last one. 11814 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32)); 11815 11816 // If this is a non-standard-aligned STORE, the penultimate operand is the 11817 // stored value. Bitcast it to the aligned type. 11818 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) { 11819 SDValue &StVal = Ops[Ops.size()-2]; 11820 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal); 11821 } 11822 11823 EVT LoadVT = isLaneOp ? VecTy.getVectorElementType() : AlignedVecTy; 11824 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, Ops, LoadVT, 11825 MemN->getMemOperand()); 11826 11827 // Update the uses. 11828 SmallVector<SDValue, 5> NewResults; 11829 for (unsigned i = 0; i < NumResultVecs; ++i) 11830 NewResults.push_back(SDValue(UpdN.getNode(), i)); 11831 11832 // If this is an non-standard-aligned LOAD, the first result is the loaded 11833 // value. Bitcast it to the expected result type. 11834 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) { 11835 SDValue &LdVal = NewResults[0]; 11836 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal); 11837 } 11838 11839 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 11840 DCI.CombineTo(N, NewResults); 11841 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 11842 11843 break; 11844 } 11845 return SDValue(); 11846 } 11847 11848 static SDValue PerformVLDCombine(SDNode *N, 11849 TargetLowering::DAGCombinerInfo &DCI) { 11850 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 11851 return SDValue(); 11852 11853 return CombineBaseUpdate(N, DCI); 11854 } 11855 11856 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 11857 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 11858 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 11859 /// return true. 11860 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 11861 SelectionDAG &DAG = DCI.DAG; 11862 EVT VT = N->getValueType(0); 11863 // vldN-dup instructions only support 64-bit vectors for N > 1. 11864 if (!VT.is64BitVector()) 11865 return false; 11866 11867 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 11868 SDNode *VLD = N->getOperand(0).getNode(); 11869 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 11870 return false; 11871 unsigned NumVecs = 0; 11872 unsigned NewOpc = 0; 11873 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 11874 if (IntNo == Intrinsic::arm_neon_vld2lane) { 11875 NumVecs = 2; 11876 NewOpc = ARMISD::VLD2DUP; 11877 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 11878 NumVecs = 3; 11879 NewOpc = ARMISD::VLD3DUP; 11880 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 11881 NumVecs = 4; 11882 NewOpc = ARMISD::VLD4DUP; 11883 } else { 11884 return false; 11885 } 11886 11887 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 11888 // numbers match the load. 11889 unsigned VLDLaneNo = 11890 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 11891 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 11892 UI != UE; ++UI) { 11893 // Ignore uses of the chain result. 11894 if (UI.getUse().getResNo() == NumVecs) 11895 continue; 11896 SDNode *User = *UI; 11897 if (User->getOpcode() != ARMISD::VDUPLANE || 11898 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 11899 return false; 11900 } 11901 11902 // Create the vldN-dup node. 11903 EVT Tys[5]; 11904 unsigned n; 11905 for (n = 0; n < NumVecs; ++n) 11906 Tys[n] = VT; 11907 Tys[n] = MVT::Other; 11908 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1)); 11909 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 11910 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 11911 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 11912 Ops, VLDMemInt->getMemoryVT(), 11913 VLDMemInt->getMemOperand()); 11914 11915 // Update the uses. 11916 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 11917 UI != UE; ++UI) { 11918 unsigned ResNo = UI.getUse().getResNo(); 11919 // Ignore uses of the chain result. 11920 if (ResNo == NumVecs) 11921 continue; 11922 SDNode *User = *UI; 11923 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 11924 } 11925 11926 // Now the vldN-lane intrinsic is dead except for its chain result. 11927 // Update uses of the chain. 11928 std::vector<SDValue> VLDDupResults; 11929 for (unsigned n = 0; n < NumVecs; ++n) 11930 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 11931 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 11932 DCI.CombineTo(VLD, VLDDupResults); 11933 11934 return true; 11935 } 11936 11937 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 11938 /// ARMISD::VDUPLANE. 11939 static SDValue PerformVDUPLANECombine(SDNode *N, 11940 TargetLowering::DAGCombinerInfo &DCI) { 11941 SDValue Op = N->getOperand(0); 11942 11943 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 11944 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 11945 if (CombineVLDDUP(N, DCI)) 11946 return SDValue(N, 0); 11947 11948 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 11949 // redundant. Ignore bit_converts for now; element sizes are checked below. 11950 while (Op.getOpcode() == ISD::BITCAST) 11951 Op = Op.getOperand(0); 11952 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 11953 return SDValue(); 11954 11955 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 11956 unsigned EltSize = Op.getScalarValueSizeInBits(); 11957 // The canonical VMOV for a zero vector uses a 32-bit element size. 11958 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 11959 unsigned EltBits; 11960 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 11961 EltSize = 8; 11962 EVT VT = N->getValueType(0); 11963 if (EltSize > VT.getScalarSizeInBits()) 11964 return SDValue(); 11965 11966 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 11967 } 11968 11969 /// PerformVDUPCombine - Target-specific dag combine xforms for ARMISD::VDUP. 11970 static SDValue PerformVDUPCombine(SDNode *N, 11971 TargetLowering::DAGCombinerInfo &DCI) { 11972 SelectionDAG &DAG = DCI.DAG; 11973 SDValue Op = N->getOperand(0); 11974 11975 // Match VDUP(LOAD) -> VLD1DUP. 11976 // We match this pattern here rather than waiting for isel because the 11977 // transform is only legal for unindexed loads. 11978 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op.getNode()); 11979 if (LD && Op.hasOneUse() && LD->isUnindexed() && 11980 LD->getMemoryVT() == N->getValueType(0).getVectorElementType()) { 11981 SDValue Ops[] = { LD->getOperand(0), LD->getOperand(1), 11982 DAG.getConstant(LD->getAlignment(), SDLoc(N), MVT::i32) }; 11983 SDVTList SDTys = DAG.getVTList(N->getValueType(0), MVT::Other); 11984 SDValue VLDDup = DAG.getMemIntrinsicNode(ARMISD::VLD1DUP, SDLoc(N), SDTys, 11985 Ops, LD->getMemoryVT(), 11986 LD->getMemOperand()); 11987 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), VLDDup.getValue(1)); 11988 return VLDDup; 11989 } 11990 11991 return SDValue(); 11992 } 11993 11994 static SDValue PerformLOADCombine(SDNode *N, 11995 TargetLowering::DAGCombinerInfo &DCI) { 11996 EVT VT = N->getValueType(0); 11997 11998 // If this is a legal vector load, try to combine it into a VLD1_UPD. 11999 if (ISD::isNormalLoad(N) && VT.isVector() && 12000 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 12001 return CombineBaseUpdate(N, DCI); 12002 12003 return SDValue(); 12004 } 12005 12006 /// PerformSTORECombine - Target-specific dag combine xforms for 12007 /// ISD::STORE. 12008 static SDValue PerformSTORECombine(SDNode *N, 12009 TargetLowering::DAGCombinerInfo &DCI) { 12010 StoreSDNode *St = cast<StoreSDNode>(N); 12011 if (St->isVolatile()) 12012 return SDValue(); 12013 12014 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 12015 // pack all of the elements in one place. Next, store to memory in fewer 12016 // chunks. 12017 SDValue StVal = St->getValue(); 12018 EVT VT = StVal.getValueType(); 12019 if (St->isTruncatingStore() && VT.isVector()) { 12020 SelectionDAG &DAG = DCI.DAG; 12021 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12022 EVT StVT = St->getMemoryVT(); 12023 unsigned NumElems = VT.getVectorNumElements(); 12024 assert(StVT != VT && "Cannot truncate to the same type"); 12025 unsigned FromEltSz = VT.getScalarSizeInBits(); 12026 unsigned ToEltSz = StVT.getScalarSizeInBits(); 12027 12028 // From, To sizes and ElemCount must be pow of two 12029 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 12030 12031 // We are going to use the original vector elt for storing. 12032 // Accumulated smaller vector elements must be a multiple of the store size. 12033 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 12034 12035 unsigned SizeRatio = FromEltSz / ToEltSz; 12036 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 12037 12038 // Create a type on which we perform the shuffle. 12039 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 12040 NumElems*SizeRatio); 12041 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 12042 12043 SDLoc DL(St); 12044 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 12045 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 12046 for (unsigned i = 0; i < NumElems; ++i) 12047 ShuffleVec[i] = DAG.getDataLayout().isBigEndian() 12048 ? (i + 1) * SizeRatio - 1 12049 : i * SizeRatio; 12050 12051 // Can't shuffle using an illegal type. 12052 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 12053 12054 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 12055 DAG.getUNDEF(WideVec.getValueType()), 12056 ShuffleVec); 12057 // At this point all of the data is stored at the bottom of the 12058 // register. We now need to save it to mem. 12059 12060 // Find the largest store unit 12061 MVT StoreType = MVT::i8; 12062 for (MVT Tp : MVT::integer_valuetypes()) { 12063 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 12064 StoreType = Tp; 12065 } 12066 // Didn't find a legal store type. 12067 if (!TLI.isTypeLegal(StoreType)) 12068 return SDValue(); 12069 12070 // Bitcast the original vector into a vector of store-size units 12071 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 12072 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 12073 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 12074 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 12075 SmallVector<SDValue, 8> Chains; 12076 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL, 12077 TLI.getPointerTy(DAG.getDataLayout())); 12078 SDValue BasePtr = St->getBasePtr(); 12079 12080 // Perform one or more big stores into memory. 12081 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 12082 for (unsigned I = 0; I < E; I++) { 12083 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 12084 StoreType, ShuffWide, 12085 DAG.getIntPtrConstant(I, DL)); 12086 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 12087 St->getPointerInfo(), St->getAlignment(), 12088 St->getMemOperand()->getFlags()); 12089 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 12090 Increment); 12091 Chains.push_back(Ch); 12092 } 12093 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 12094 } 12095 12096 if (!ISD::isNormalStore(St)) 12097 return SDValue(); 12098 12099 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 12100 // ARM stores of arguments in the same cache line. 12101 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 12102 StVal.getNode()->hasOneUse()) { 12103 SelectionDAG &DAG = DCI.DAG; 12104 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 12105 SDLoc DL(St); 12106 SDValue BasePtr = St->getBasePtr(); 12107 SDValue NewST1 = DAG.getStore( 12108 St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0), 12109 BasePtr, St->getPointerInfo(), St->getAlignment(), 12110 St->getMemOperand()->getFlags()); 12111 12112 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 12113 DAG.getConstant(4, DL, MVT::i32)); 12114 return DAG.getStore(NewST1.getValue(0), DL, 12115 StVal.getNode()->getOperand(isBigEndian ? 0 : 1), 12116 OffsetPtr, St->getPointerInfo(), 12117 std::min(4U, St->getAlignment() / 2), 12118 St->getMemOperand()->getFlags()); 12119 } 12120 12121 if (StVal.getValueType() == MVT::i64 && 12122 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 12123 12124 // Bitcast an i64 store extracted from a vector to f64. 12125 // Otherwise, the i64 value will be legalized to a pair of i32 values. 12126 SelectionDAG &DAG = DCI.DAG; 12127 SDLoc dl(StVal); 12128 SDValue IntVec = StVal.getOperand(0); 12129 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 12130 IntVec.getValueType().getVectorNumElements()); 12131 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 12132 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 12133 Vec, StVal.getOperand(1)); 12134 dl = SDLoc(N); 12135 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 12136 // Make the DAGCombiner fold the bitcasts. 12137 DCI.AddToWorklist(Vec.getNode()); 12138 DCI.AddToWorklist(ExtElt.getNode()); 12139 DCI.AddToWorklist(V.getNode()); 12140 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 12141 St->getPointerInfo(), St->getAlignment(), 12142 St->getMemOperand()->getFlags(), St->getAAInfo()); 12143 } 12144 12145 // If this is a legal vector store, try to combine it into a VST1_UPD. 12146 if (ISD::isNormalStore(N) && VT.isVector() && 12147 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 12148 return CombineBaseUpdate(N, DCI); 12149 12150 return SDValue(); 12151 } 12152 12153 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 12154 /// can replace combinations of VMUL and VCVT (floating-point to integer) 12155 /// when the VMUL has a constant operand that is a power of 2. 12156 /// 12157 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 12158 /// vmul.f32 d16, d17, d16 12159 /// vcvt.s32.f32 d16, d16 12160 /// becomes: 12161 /// vcvt.s32.f32 d16, d16, #3 12162 static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG, 12163 const ARMSubtarget *Subtarget) { 12164 if (!Subtarget->hasNEON()) 12165 return SDValue(); 12166 12167 SDValue Op = N->getOperand(0); 12168 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() || 12169 Op.getOpcode() != ISD::FMUL) 12170 return SDValue(); 12171 12172 SDValue ConstVec = Op->getOperand(1); 12173 if (!isa<BuildVectorSDNode>(ConstVec)) 12174 return SDValue(); 12175 12176 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 12177 uint32_t FloatBits = FloatTy.getSizeInBits(); 12178 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 12179 uint32_t IntBits = IntTy.getSizeInBits(); 12180 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 12181 if (FloatBits != 32 || IntBits > 32 || (NumLanes != 4 && NumLanes != 2)) { 12182 // These instructions only exist converting from f32 to i32. We can handle 12183 // smaller integers by generating an extra truncate, but larger ones would 12184 // be lossy. We also can't handle anything other than 2 or 4 lanes, since 12185 // these intructions only support v2i32/v4i32 types. 12186 return SDValue(); 12187 } 12188 12189 BitVector UndefElements; 12190 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 12191 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 12192 if (C == -1 || C == 0 || C > 32) 12193 return SDValue(); 12194 12195 SDLoc dl(N); 12196 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 12197 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 12198 Intrinsic::arm_neon_vcvtfp2fxu; 12199 SDValue FixConv = DAG.getNode( 12200 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 12201 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0), 12202 DAG.getConstant(C, dl, MVT::i32)); 12203 12204 if (IntBits < FloatBits) 12205 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv); 12206 12207 return FixConv; 12208 } 12209 12210 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 12211 /// can replace combinations of VCVT (integer to floating-point) and VDIV 12212 /// when the VDIV has a constant operand that is a power of 2. 12213 /// 12214 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 12215 /// vcvt.f32.s32 d16, d16 12216 /// vdiv.f32 d16, d17, d16 12217 /// becomes: 12218 /// vcvt.f32.s32 d16, d16, #3 12219 static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG, 12220 const ARMSubtarget *Subtarget) { 12221 if (!Subtarget->hasNEON()) 12222 return SDValue(); 12223 12224 SDValue Op = N->getOperand(0); 12225 unsigned OpOpcode = Op.getNode()->getOpcode(); 12226 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() || 12227 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 12228 return SDValue(); 12229 12230 SDValue ConstVec = N->getOperand(1); 12231 if (!isa<BuildVectorSDNode>(ConstVec)) 12232 return SDValue(); 12233 12234 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 12235 uint32_t FloatBits = FloatTy.getSizeInBits(); 12236 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 12237 uint32_t IntBits = IntTy.getSizeInBits(); 12238 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 12239 if (FloatBits != 32 || IntBits > 32 || (NumLanes != 4 && NumLanes != 2)) { 12240 // These instructions only exist converting from i32 to f32. We can handle 12241 // smaller integers by generating an extra extend, but larger ones would 12242 // be lossy. We also can't handle anything other than 2 or 4 lanes, since 12243 // these intructions only support v2i32/v4i32 types. 12244 return SDValue(); 12245 } 12246 12247 BitVector UndefElements; 12248 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 12249 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 12250 if (C == -1 || C == 0 || C > 32) 12251 return SDValue(); 12252 12253 SDLoc dl(N); 12254 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 12255 SDValue ConvInput = Op.getOperand(0); 12256 if (IntBits < FloatBits) 12257 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 12258 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 12259 ConvInput); 12260 12261 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 12262 Intrinsic::arm_neon_vcvtfxu2fp; 12263 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, 12264 Op.getValueType(), 12265 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), 12266 ConvInput, DAG.getConstant(C, dl, MVT::i32)); 12267 } 12268 12269 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 12270 /// operand of a vector shift operation, where all the elements of the 12271 /// build_vector must have the same constant integer value. 12272 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 12273 // Ignore bit_converts. 12274 while (Op.getOpcode() == ISD::BITCAST) 12275 Op = Op.getOperand(0); 12276 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 12277 APInt SplatBits, SplatUndef; 12278 unsigned SplatBitSize; 12279 bool HasAnyUndefs; 12280 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 12281 HasAnyUndefs, ElementBits) || 12282 SplatBitSize > ElementBits) 12283 return false; 12284 Cnt = SplatBits.getSExtValue(); 12285 return true; 12286 } 12287 12288 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 12289 /// operand of a vector shift left operation. That value must be in the range: 12290 /// 0 <= Value < ElementBits for a left shift; or 12291 /// 0 <= Value <= ElementBits for a long left shift. 12292 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 12293 assert(VT.isVector() && "vector shift count is not a vector type"); 12294 int64_t ElementBits = VT.getScalarSizeInBits(); 12295 if (! getVShiftImm(Op, ElementBits, Cnt)) 12296 return false; 12297 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 12298 } 12299 12300 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 12301 /// operand of a vector shift right operation. For a shift opcode, the value 12302 /// is positive, but for an intrinsic the value count must be negative. The 12303 /// absolute value must be in the range: 12304 /// 1 <= |Value| <= ElementBits for a right shift; or 12305 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 12306 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 12307 int64_t &Cnt) { 12308 assert(VT.isVector() && "vector shift count is not a vector type"); 12309 int64_t ElementBits = VT.getScalarSizeInBits(); 12310 if (! getVShiftImm(Op, ElementBits, Cnt)) 12311 return false; 12312 if (!isIntrinsic) 12313 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 12314 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) { 12315 Cnt = -Cnt; 12316 return true; 12317 } 12318 return false; 12319 } 12320 12321 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 12322 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 12323 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 12324 switch (IntNo) { 12325 default: 12326 // Don't do anything for most intrinsics. 12327 break; 12328 12329 // Vector shifts: check for immediate versions and lower them. 12330 // Note: This is done during DAG combining instead of DAG legalizing because 12331 // the build_vectors for 64-bit vector element shift counts are generally 12332 // not legal, and it is hard to see their values after they get legalized to 12333 // loads from a constant pool. 12334 case Intrinsic::arm_neon_vshifts: 12335 case Intrinsic::arm_neon_vshiftu: 12336 case Intrinsic::arm_neon_vrshifts: 12337 case Intrinsic::arm_neon_vrshiftu: 12338 case Intrinsic::arm_neon_vrshiftn: 12339 case Intrinsic::arm_neon_vqshifts: 12340 case Intrinsic::arm_neon_vqshiftu: 12341 case Intrinsic::arm_neon_vqshiftsu: 12342 case Intrinsic::arm_neon_vqshiftns: 12343 case Intrinsic::arm_neon_vqshiftnu: 12344 case Intrinsic::arm_neon_vqshiftnsu: 12345 case Intrinsic::arm_neon_vqrshiftns: 12346 case Intrinsic::arm_neon_vqrshiftnu: 12347 case Intrinsic::arm_neon_vqrshiftnsu: { 12348 EVT VT = N->getOperand(1).getValueType(); 12349 int64_t Cnt; 12350 unsigned VShiftOpc = 0; 12351 12352 switch (IntNo) { 12353 case Intrinsic::arm_neon_vshifts: 12354 case Intrinsic::arm_neon_vshiftu: 12355 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 12356 VShiftOpc = ARMISD::VSHL; 12357 break; 12358 } 12359 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 12360 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 12361 ARMISD::VSHRs : ARMISD::VSHRu); 12362 break; 12363 } 12364 return SDValue(); 12365 12366 case Intrinsic::arm_neon_vrshifts: 12367 case Intrinsic::arm_neon_vrshiftu: 12368 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 12369 break; 12370 return SDValue(); 12371 12372 case Intrinsic::arm_neon_vqshifts: 12373 case Intrinsic::arm_neon_vqshiftu: 12374 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 12375 break; 12376 return SDValue(); 12377 12378 case Intrinsic::arm_neon_vqshiftsu: 12379 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 12380 break; 12381 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 12382 12383 case Intrinsic::arm_neon_vrshiftn: 12384 case Intrinsic::arm_neon_vqshiftns: 12385 case Intrinsic::arm_neon_vqshiftnu: 12386 case Intrinsic::arm_neon_vqshiftnsu: 12387 case Intrinsic::arm_neon_vqrshiftns: 12388 case Intrinsic::arm_neon_vqrshiftnu: 12389 case Intrinsic::arm_neon_vqrshiftnsu: 12390 // Narrowing shifts require an immediate right shift. 12391 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 12392 break; 12393 llvm_unreachable("invalid shift count for narrowing vector shift " 12394 "intrinsic"); 12395 12396 default: 12397 llvm_unreachable("unhandled vector shift"); 12398 } 12399 12400 switch (IntNo) { 12401 case Intrinsic::arm_neon_vshifts: 12402 case Intrinsic::arm_neon_vshiftu: 12403 // Opcode already set above. 12404 break; 12405 case Intrinsic::arm_neon_vrshifts: 12406 VShiftOpc = ARMISD::VRSHRs; break; 12407 case Intrinsic::arm_neon_vrshiftu: 12408 VShiftOpc = ARMISD::VRSHRu; break; 12409 case Intrinsic::arm_neon_vrshiftn: 12410 VShiftOpc = ARMISD::VRSHRN; break; 12411 case Intrinsic::arm_neon_vqshifts: 12412 VShiftOpc = ARMISD::VQSHLs; break; 12413 case Intrinsic::arm_neon_vqshiftu: 12414 VShiftOpc = ARMISD::VQSHLu; break; 12415 case Intrinsic::arm_neon_vqshiftsu: 12416 VShiftOpc = ARMISD::VQSHLsu; break; 12417 case Intrinsic::arm_neon_vqshiftns: 12418 VShiftOpc = ARMISD::VQSHRNs; break; 12419 case Intrinsic::arm_neon_vqshiftnu: 12420 VShiftOpc = ARMISD::VQSHRNu; break; 12421 case Intrinsic::arm_neon_vqshiftnsu: 12422 VShiftOpc = ARMISD::VQSHRNsu; break; 12423 case Intrinsic::arm_neon_vqrshiftns: 12424 VShiftOpc = ARMISD::VQRSHRNs; break; 12425 case Intrinsic::arm_neon_vqrshiftnu: 12426 VShiftOpc = ARMISD::VQRSHRNu; break; 12427 case Intrinsic::arm_neon_vqrshiftnsu: 12428 VShiftOpc = ARMISD::VQRSHRNsu; break; 12429 } 12430 12431 SDLoc dl(N); 12432 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 12433 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32)); 12434 } 12435 12436 case Intrinsic::arm_neon_vshiftins: { 12437 EVT VT = N->getOperand(1).getValueType(); 12438 int64_t Cnt; 12439 unsigned VShiftOpc = 0; 12440 12441 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 12442 VShiftOpc = ARMISD::VSLI; 12443 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 12444 VShiftOpc = ARMISD::VSRI; 12445 else { 12446 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 12447 } 12448 12449 SDLoc dl(N); 12450 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 12451 N->getOperand(1), N->getOperand(2), 12452 DAG.getConstant(Cnt, dl, MVT::i32)); 12453 } 12454 12455 case Intrinsic::arm_neon_vqrshifts: 12456 case Intrinsic::arm_neon_vqrshiftu: 12457 // No immediate versions of these to check for. 12458 break; 12459 } 12460 12461 return SDValue(); 12462 } 12463 12464 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 12465 /// lowers them. As with the vector shift intrinsics, this is done during DAG 12466 /// combining instead of DAG legalizing because the build_vectors for 64-bit 12467 /// vector element shift counts are generally not legal, and it is hard to see 12468 /// their values after they get legalized to loads from a constant pool. 12469 static SDValue PerformShiftCombine(SDNode *N, 12470 TargetLowering::DAGCombinerInfo &DCI, 12471 const ARMSubtarget *ST) { 12472 SelectionDAG &DAG = DCI.DAG; 12473 EVT VT = N->getValueType(0); 12474 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 12475 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 12476 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 12477 SDValue N1 = N->getOperand(1); 12478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 12479 SDValue N0 = N->getOperand(0); 12480 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 12481 DAG.MaskedValueIsZero(N0.getOperand(0), 12482 APInt::getHighBitsSet(32, 16))) 12483 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 12484 } 12485 } 12486 12487 if (ST->isThumb1Only() && N->getOpcode() == ISD::SHL && VT == MVT::i32 && 12488 N->getOperand(0)->getOpcode() == ISD::AND && 12489 N->getOperand(0)->hasOneUse()) { 12490 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 12491 return SDValue(); 12492 // Look for the pattern (shl (and x, AndMask), ShiftAmt). This doesn't 12493 // usually show up because instcombine prefers to canonicalize it to 12494 // (and (shl x, ShiftAmt) (shl AndMask, ShiftAmt)), but the shift can come 12495 // out of GEP lowering in some cases. 12496 SDValue N0 = N->getOperand(0); 12497 ConstantSDNode *ShiftAmtNode = dyn_cast<ConstantSDNode>(N->getOperand(1)); 12498 if (!ShiftAmtNode) 12499 return SDValue(); 12500 uint32_t ShiftAmt = static_cast<uint32_t>(ShiftAmtNode->getZExtValue()); 12501 ConstantSDNode *AndMaskNode = dyn_cast<ConstantSDNode>(N0->getOperand(1)); 12502 if (!AndMaskNode) 12503 return SDValue(); 12504 uint32_t AndMask = static_cast<uint32_t>(AndMaskNode->getZExtValue()); 12505 // Don't transform uxtb/uxth. 12506 if (AndMask == 255 || AndMask == 65535) 12507 return SDValue(); 12508 if (isMask_32(AndMask)) { 12509 uint32_t MaskedBits = countLeadingZeros(AndMask); 12510 if (MaskedBits > ShiftAmt) { 12511 SDLoc DL(N); 12512 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0), 12513 DAG.getConstant(MaskedBits, DL, MVT::i32)); 12514 return DAG.getNode( 12515 ISD::SRL, DL, MVT::i32, SHL, 12516 DAG.getConstant(MaskedBits - ShiftAmt, DL, MVT::i32)); 12517 } 12518 } 12519 } 12520 12521 // Nothing to be done for scalar shifts. 12522 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12523 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 12524 return SDValue(); 12525 12526 assert(ST->hasNEON() && "unexpected vector shift"); 12527 int64_t Cnt; 12528 12529 switch (N->getOpcode()) { 12530 default: llvm_unreachable("unexpected shift opcode"); 12531 12532 case ISD::SHL: 12533 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) { 12534 SDLoc dl(N); 12535 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0), 12536 DAG.getConstant(Cnt, dl, MVT::i32)); 12537 } 12538 break; 12539 12540 case ISD::SRA: 12541 case ISD::SRL: 12542 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 12543 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 12544 ARMISD::VSHRs : ARMISD::VSHRu); 12545 SDLoc dl(N); 12546 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), 12547 DAG.getConstant(Cnt, dl, MVT::i32)); 12548 } 12549 } 12550 return SDValue(); 12551 } 12552 12553 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 12554 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 12555 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 12556 const ARMSubtarget *ST) { 12557 SDValue N0 = N->getOperand(0); 12558 12559 // Check for sign- and zero-extensions of vector extract operations of 8- 12560 // and 16-bit vector elements. NEON supports these directly. They are 12561 // handled during DAG combining because type legalization will promote them 12562 // to 32-bit types and it is messy to recognize the operations after that. 12563 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 12564 SDValue Vec = N0.getOperand(0); 12565 SDValue Lane = N0.getOperand(1); 12566 EVT VT = N->getValueType(0); 12567 EVT EltVT = N0.getValueType(); 12568 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12569 12570 if (VT == MVT::i32 && 12571 (EltVT == MVT::i8 || EltVT == MVT::i16) && 12572 TLI.isTypeLegal(Vec.getValueType()) && 12573 isa<ConstantSDNode>(Lane)) { 12574 12575 unsigned Opc = 0; 12576 switch (N->getOpcode()) { 12577 default: llvm_unreachable("unexpected opcode"); 12578 case ISD::SIGN_EXTEND: 12579 Opc = ARMISD::VGETLANEs; 12580 break; 12581 case ISD::ZERO_EXTEND: 12582 case ISD::ANY_EXTEND: 12583 Opc = ARMISD::VGETLANEu; 12584 break; 12585 } 12586 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 12587 } 12588 } 12589 12590 return SDValue(); 12591 } 12592 12593 static const APInt *isPowerOf2Constant(SDValue V) { 12594 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V); 12595 if (!C) 12596 return nullptr; 12597 const APInt *CV = &C->getAPIntValue(); 12598 return CV->isPowerOf2() ? CV : nullptr; 12599 } 12600 12601 SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const { 12602 // If we have a CMOV, OR and AND combination such as: 12603 // if (x & CN) 12604 // y |= CM; 12605 // 12606 // And: 12607 // * CN is a single bit; 12608 // * All bits covered by CM are known zero in y 12609 // 12610 // Then we can convert this into a sequence of BFI instructions. This will 12611 // always be a win if CM is a single bit, will always be no worse than the 12612 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is 12613 // three bits (due to the extra IT instruction). 12614 12615 SDValue Op0 = CMOV->getOperand(0); 12616 SDValue Op1 = CMOV->getOperand(1); 12617 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2)); 12618 auto CC = CCNode->getAPIntValue().getLimitedValue(); 12619 SDValue CmpZ = CMOV->getOperand(4); 12620 12621 // The compare must be against zero. 12622 if (!isNullConstant(CmpZ->getOperand(1))) 12623 return SDValue(); 12624 12625 assert(CmpZ->getOpcode() == ARMISD::CMPZ); 12626 SDValue And = CmpZ->getOperand(0); 12627 if (And->getOpcode() != ISD::AND) 12628 return SDValue(); 12629 const APInt *AndC = isPowerOf2Constant(And->getOperand(1)); 12630 if (!AndC) 12631 return SDValue(); 12632 SDValue X = And->getOperand(0); 12633 12634 if (CC == ARMCC::EQ) { 12635 // We're performing an "equal to zero" compare. Swap the operands so we 12636 // canonicalize on a "not equal to zero" compare. 12637 std::swap(Op0, Op1); 12638 } else { 12639 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?"); 12640 } 12641 12642 if (Op1->getOpcode() != ISD::OR) 12643 return SDValue(); 12644 12645 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1)); 12646 if (!OrC) 12647 return SDValue(); 12648 SDValue Y = Op1->getOperand(0); 12649 12650 if (Op0 != Y) 12651 return SDValue(); 12652 12653 // Now, is it profitable to continue? 12654 APInt OrCI = OrC->getAPIntValue(); 12655 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2; 12656 if (OrCI.countPopulation() > Heuristic) 12657 return SDValue(); 12658 12659 // Lastly, can we determine that the bits defined by OrCI 12660 // are zero in Y? 12661 KnownBits Known = DAG.computeKnownBits(Y); 12662 if ((OrCI & Known.Zero) != OrCI) 12663 return SDValue(); 12664 12665 // OK, we can do the combine. 12666 SDValue V = Y; 12667 SDLoc dl(X); 12668 EVT VT = X.getValueType(); 12669 unsigned BitInX = AndC->logBase2(); 12670 12671 if (BitInX != 0) { 12672 // We must shift X first. 12673 X = DAG.getNode(ISD::SRL, dl, VT, X, 12674 DAG.getConstant(BitInX, dl, VT)); 12675 } 12676 12677 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits(); 12678 BitInY < NumActiveBits; ++BitInY) { 12679 if (OrCI[BitInY] == 0) 12680 continue; 12681 APInt Mask(VT.getSizeInBits(), 0); 12682 Mask.setBit(BitInY); 12683 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X, 12684 // Confusingly, the operand is an *inverted* mask. 12685 DAG.getConstant(~Mask, dl, VT)); 12686 } 12687 12688 return V; 12689 } 12690 12691 /// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND. 12692 SDValue 12693 ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const { 12694 SDValue Cmp = N->getOperand(4); 12695 if (Cmp.getOpcode() != ARMISD::CMPZ) 12696 // Only looking at NE cases. 12697 return SDValue(); 12698 12699 EVT VT = N->getValueType(0); 12700 SDLoc dl(N); 12701 SDValue LHS = Cmp.getOperand(0); 12702 SDValue RHS = Cmp.getOperand(1); 12703 SDValue Chain = N->getOperand(0); 12704 SDValue BB = N->getOperand(1); 12705 SDValue ARMcc = N->getOperand(2); 12706 ARMCC::CondCodes CC = 12707 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 12708 12709 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0)) 12710 // -> (brcond Chain BB CC CPSR Cmp) 12711 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() && 12712 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV && 12713 LHS->getOperand(0)->hasOneUse()) { 12714 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0)); 12715 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1)); 12716 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 12717 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 12718 if ((LHS00C && LHS00C->getZExtValue() == 0) && 12719 (LHS01C && LHS01C->getZExtValue() == 1) && 12720 (LHS1C && LHS1C->getZExtValue() == 1) && 12721 (RHSC && RHSC->getZExtValue() == 0)) { 12722 return DAG.getNode( 12723 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2), 12724 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4)); 12725 } 12726 } 12727 12728 return SDValue(); 12729 } 12730 12731 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 12732 SDValue 12733 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 12734 SDValue Cmp = N->getOperand(4); 12735 if (Cmp.getOpcode() != ARMISD::CMPZ) 12736 // Only looking at EQ and NE cases. 12737 return SDValue(); 12738 12739 EVT VT = N->getValueType(0); 12740 SDLoc dl(N); 12741 SDValue LHS = Cmp.getOperand(0); 12742 SDValue RHS = Cmp.getOperand(1); 12743 SDValue FalseVal = N->getOperand(0); 12744 SDValue TrueVal = N->getOperand(1); 12745 SDValue ARMcc = N->getOperand(2); 12746 ARMCC::CondCodes CC = 12747 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 12748 12749 // BFI is only available on V6T2+. 12750 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) { 12751 SDValue R = PerformCMOVToBFICombine(N, DAG); 12752 if (R) 12753 return R; 12754 } 12755 12756 // Simplify 12757 // mov r1, r0 12758 // cmp r1, x 12759 // mov r0, y 12760 // moveq r0, x 12761 // to 12762 // cmp r0, x 12763 // movne r0, y 12764 // 12765 // mov r1, r0 12766 // cmp r1, x 12767 // mov r0, x 12768 // movne r0, y 12769 // to 12770 // cmp r0, x 12771 // movne r0, y 12772 /// FIXME: Turn this into a target neutral optimization? 12773 SDValue Res; 12774 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 12775 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 12776 N->getOperand(3), Cmp); 12777 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 12778 SDValue ARMcc; 12779 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 12780 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 12781 N->getOperand(3), NewCmp); 12782 } 12783 12784 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0)) 12785 // -> (cmov F T CC CPSR Cmp) 12786 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) { 12787 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)); 12788 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 12789 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 12790 if ((LHS0C && LHS0C->getZExtValue() == 0) && 12791 (LHS1C && LHS1C->getZExtValue() == 1) && 12792 (RHSC && RHSC->getZExtValue() == 0)) { 12793 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 12794 LHS->getOperand(2), LHS->getOperand(3), 12795 LHS->getOperand(4)); 12796 } 12797 } 12798 12799 if (!VT.isInteger()) 12800 return SDValue(); 12801 12802 // Materialize a boolean comparison for integers so we can avoid branching. 12803 if (isNullConstant(FalseVal)) { 12804 if (CC == ARMCC::EQ && isOneConstant(TrueVal)) { 12805 if (!Subtarget->isThumb1Only() && Subtarget->hasV5TOps()) { 12806 // If x == y then x - y == 0 and ARM's CLZ will return 32, shifting it 12807 // right 5 bits will make that 32 be 1, otherwise it will be 0. 12808 // CMOV 0, 1, ==, (CMPZ x, y) -> SRL (CTLZ (SUB x, y)), 5 12809 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, LHS, RHS); 12810 Res = DAG.getNode(ISD::SRL, dl, VT, DAG.getNode(ISD::CTLZ, dl, VT, Sub), 12811 DAG.getConstant(5, dl, MVT::i32)); 12812 } else { 12813 // CMOV 0, 1, ==, (CMPZ x, y) -> 12814 // (ADDCARRY (SUB x, y), t:0, t:1) 12815 // where t = (SUBCARRY 0, (SUB x, y), 0) 12816 // 12817 // The SUBCARRY computes 0 - (x - y) and this will give a borrow when 12818 // x != y. In other words, a carry C == 1 when x == y, C == 0 12819 // otherwise. 12820 // The final ADDCARRY computes 12821 // x - y + (0 - (x - y)) + C == C 12822 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, LHS, RHS); 12823 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 12824 SDValue Neg = DAG.getNode(ISD::USUBO, dl, VTs, FalseVal, Sub); 12825 // ISD::SUBCARRY returns a borrow but we want the carry here 12826 // actually. 12827 SDValue Carry = 12828 DAG.getNode(ISD::SUB, dl, MVT::i32, 12829 DAG.getConstant(1, dl, MVT::i32), Neg.getValue(1)); 12830 Res = DAG.getNode(ISD::ADDCARRY, dl, VTs, Sub, Neg, Carry); 12831 } 12832 } else if (CC == ARMCC::NE && !isNullConstant(RHS) && 12833 (!Subtarget->isThumb1Only() || isPowerOf2Constant(TrueVal))) { 12834 // This seems pointless but will allow us to combine it further below. 12835 // CMOV 0, z, !=, (CMPZ x, y) -> CMOV (SUBS x, y), z, !=, (SUBS x, y):1 12836 SDValue Sub = 12837 DAG.getNode(ARMISD::SUBS, dl, DAG.getVTList(VT, MVT::i32), LHS, RHS); 12838 SDValue CPSRGlue = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR, 12839 Sub.getValue(1), SDValue()); 12840 Res = DAG.getNode(ARMISD::CMOV, dl, VT, Sub, TrueVal, ARMcc, 12841 N->getOperand(3), CPSRGlue.getValue(1)); 12842 FalseVal = Sub; 12843 } 12844 } else if (isNullConstant(TrueVal)) { 12845 if (CC == ARMCC::EQ && !isNullConstant(RHS) && 12846 (!Subtarget->isThumb1Only() || isPowerOf2Constant(FalseVal))) { 12847 // This seems pointless but will allow us to combine it further below 12848 // Note that we change == for != as this is the dual for the case above. 12849 // CMOV z, 0, ==, (CMPZ x, y) -> CMOV (SUBS x, y), z, !=, (SUBS x, y):1 12850 SDValue Sub = 12851 DAG.getNode(ARMISD::SUBS, dl, DAG.getVTList(VT, MVT::i32), LHS, RHS); 12852 SDValue CPSRGlue = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR, 12853 Sub.getValue(1), SDValue()); 12854 Res = DAG.getNode(ARMISD::CMOV, dl, VT, Sub, FalseVal, 12855 DAG.getConstant(ARMCC::NE, dl, MVT::i32), 12856 N->getOperand(3), CPSRGlue.getValue(1)); 12857 FalseVal = Sub; 12858 } 12859 } 12860 12861 // On Thumb1, the DAG above may be further combined if z is a power of 2 12862 // (z == 2 ^ K). 12863 // CMOV (SUBS x, y), z, !=, (SUBS x, y):1 -> 12864 // t1 = (USUBO (SUB x, y), 1) 12865 // t2 = (SUBCARRY (SUB x, y), t1:0, t1:1) 12866 // Result = if K != 0 then (SHL t2:0, K) else t2:0 12867 // 12868 // This also handles the special case of comparing against zero; it's 12869 // essentially, the same pattern, except there's no SUBS: 12870 // CMOV x, z, !=, (CMPZ x, 0) -> 12871 // t1 = (USUBO x, 1) 12872 // t2 = (SUBCARRY x, t1:0, t1:1) 12873 // Result = if K != 0 then (SHL t2:0, K) else t2:0 12874 const APInt *TrueConst; 12875 if (Subtarget->isThumb1Only() && CC == ARMCC::NE && 12876 ((FalseVal.getOpcode() == ARMISD::SUBS && 12877 FalseVal.getOperand(0) == LHS && FalseVal.getOperand(1) == RHS) || 12878 (FalseVal == LHS && isNullConstant(RHS))) && 12879 (TrueConst = isPowerOf2Constant(TrueVal))) { 12880 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 12881 unsigned ShiftAmount = TrueConst->logBase2(); 12882 if (ShiftAmount) 12883 TrueVal = DAG.getConstant(1, dl, VT); 12884 SDValue Subc = DAG.getNode(ISD::USUBO, dl, VTs, FalseVal, TrueVal); 12885 Res = DAG.getNode(ISD::SUBCARRY, dl, VTs, FalseVal, Subc, Subc.getValue(1)); 12886 12887 if (ShiftAmount) 12888 Res = DAG.getNode(ISD::SHL, dl, VT, Res, 12889 DAG.getConstant(ShiftAmount, dl, MVT::i32)); 12890 } 12891 12892 if (Res.getNode()) { 12893 KnownBits Known = DAG.computeKnownBits(SDValue(N,0)); 12894 // Capture demanded bits information that would be otherwise lost. 12895 if (Known.Zero == 0xfffffffe) 12896 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 12897 DAG.getValueType(MVT::i1)); 12898 else if (Known.Zero == 0xffffff00) 12899 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 12900 DAG.getValueType(MVT::i8)); 12901 else if (Known.Zero == 0xffff0000) 12902 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 12903 DAG.getValueType(MVT::i16)); 12904 } 12905 12906 return Res; 12907 } 12908 12909 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 12910 DAGCombinerInfo &DCI) const { 12911 switch (N->getOpcode()) { 12912 default: break; 12913 case ISD::ABS: return PerformABSCombine(N, DCI, Subtarget); 12914 case ARMISD::ADDE: return PerformADDECombine(N, DCI, Subtarget); 12915 case ARMISD::UMLAL: return PerformUMLALCombine(N, DCI.DAG, Subtarget); 12916 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 12917 case ISD::SUB: return PerformSUBCombine(N, DCI); 12918 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 12919 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 12920 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 12921 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 12922 case ARMISD::ADDC: 12923 case ARMISD::SUBC: return PerformAddcSubcCombine(N, DCI, Subtarget); 12924 case ARMISD::SUBE: return PerformAddeSubeCombine(N, DCI, Subtarget); 12925 case ARMISD::BFI: return PerformBFICombine(N, DCI); 12926 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget); 12927 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 12928 case ISD::STORE: return PerformSTORECombine(N, DCI); 12929 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget); 12930 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 12931 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 12932 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 12933 case ARMISD::VDUP: return PerformVDUPCombine(N, DCI); 12934 case ISD::FP_TO_SINT: 12935 case ISD::FP_TO_UINT: 12936 return PerformVCVTCombine(N, DCI.DAG, Subtarget); 12937 case ISD::FDIV: 12938 return PerformVDIVCombine(N, DCI.DAG, Subtarget); 12939 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 12940 case ISD::SHL: 12941 case ISD::SRA: 12942 case ISD::SRL: 12943 return PerformShiftCombine(N, DCI, Subtarget); 12944 case ISD::SIGN_EXTEND: 12945 case ISD::ZERO_EXTEND: 12946 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 12947 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 12948 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG); 12949 case ISD::LOAD: return PerformLOADCombine(N, DCI); 12950 case ARMISD::VLD1DUP: 12951 case ARMISD::VLD2DUP: 12952 case ARMISD::VLD3DUP: 12953 case ARMISD::VLD4DUP: 12954 return PerformVLDCombine(N, DCI); 12955 case ARMISD::BUILD_VECTOR: 12956 return PerformARMBUILD_VECTORCombine(N, DCI); 12957 case ARMISD::SMULWB: { 12958 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12959 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16); 12960 if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)) 12961 return SDValue(); 12962 break; 12963 } 12964 case ARMISD::SMULWT: { 12965 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12966 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16); 12967 if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)) 12968 return SDValue(); 12969 break; 12970 } 12971 case ARMISD::SMLALBB: { 12972 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12973 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16); 12974 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 12975 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 12976 return SDValue(); 12977 break; 12978 } 12979 case ARMISD::SMLALBT: { 12980 unsigned LowWidth = N->getOperand(0).getValueType().getSizeInBits(); 12981 APInt LowMask = APInt::getLowBitsSet(LowWidth, 16); 12982 unsigned HighWidth = N->getOperand(1).getValueType().getSizeInBits(); 12983 APInt HighMask = APInt::getHighBitsSet(HighWidth, 16); 12984 if ((SimplifyDemandedBits(N->getOperand(0), LowMask, DCI)) || 12985 (SimplifyDemandedBits(N->getOperand(1), HighMask, DCI))) 12986 return SDValue(); 12987 break; 12988 } 12989 case ARMISD::SMLALTB: { 12990 unsigned HighWidth = N->getOperand(0).getValueType().getSizeInBits(); 12991 APInt HighMask = APInt::getHighBitsSet(HighWidth, 16); 12992 unsigned LowWidth = N->getOperand(1).getValueType().getSizeInBits(); 12993 APInt LowMask = APInt::getLowBitsSet(LowWidth, 16); 12994 if ((SimplifyDemandedBits(N->getOperand(0), HighMask, DCI)) || 12995 (SimplifyDemandedBits(N->getOperand(1), LowMask, DCI))) 12996 return SDValue(); 12997 break; 12998 } 12999 case ARMISD::SMLALTT: { 13000 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 13001 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16); 13002 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 13003 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 13004 return SDValue(); 13005 break; 13006 } 13007 case ISD::INTRINSIC_VOID: 13008 case ISD::INTRINSIC_W_CHAIN: 13009 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 13010 case Intrinsic::arm_neon_vld1: 13011 case Intrinsic::arm_neon_vld1x2: 13012 case Intrinsic::arm_neon_vld1x3: 13013 case Intrinsic::arm_neon_vld1x4: 13014 case Intrinsic::arm_neon_vld2: 13015 case Intrinsic::arm_neon_vld3: 13016 case Intrinsic::arm_neon_vld4: 13017 case Intrinsic::arm_neon_vld2lane: 13018 case Intrinsic::arm_neon_vld3lane: 13019 case Intrinsic::arm_neon_vld4lane: 13020 case Intrinsic::arm_neon_vld2dup: 13021 case Intrinsic::arm_neon_vld3dup: 13022 case Intrinsic::arm_neon_vld4dup: 13023 case Intrinsic::arm_neon_vst1: 13024 case Intrinsic::arm_neon_vst1x2: 13025 case Intrinsic::arm_neon_vst1x3: 13026 case Intrinsic::arm_neon_vst1x4: 13027 case Intrinsic::arm_neon_vst2: 13028 case Intrinsic::arm_neon_vst3: 13029 case Intrinsic::arm_neon_vst4: 13030 case Intrinsic::arm_neon_vst2lane: 13031 case Intrinsic::arm_neon_vst3lane: 13032 case Intrinsic::arm_neon_vst4lane: 13033 return PerformVLDCombine(N, DCI); 13034 default: break; 13035 } 13036 break; 13037 } 13038 return SDValue(); 13039 } 13040 13041 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 13042 EVT VT) const { 13043 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 13044 } 13045 13046 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, unsigned, 13047 unsigned, 13048 MachineMemOperand::Flags, 13049 bool *Fast) const { 13050 // Depends what it gets converted into if the type is weird. 13051 if (!VT.isSimple()) 13052 return false; 13053 13054 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus 13055 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 13056 13057 switch (VT.getSimpleVT().SimpleTy) { 13058 default: 13059 return false; 13060 case MVT::i8: 13061 case MVT::i16: 13062 case MVT::i32: { 13063 // Unaligned access can use (for example) LRDB, LRDH, LDR 13064 if (AllowsUnaligned) { 13065 if (Fast) 13066 *Fast = Subtarget->hasV7Ops(); 13067 return true; 13068 } 13069 return false; 13070 } 13071 case MVT::f64: 13072 case MVT::v2f64: { 13073 // For any little-endian targets with neon, we can support unaligned ld/st 13074 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 13075 // A big-endian target may also explicitly support unaligned accesses 13076 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) { 13077 if (Fast) 13078 *Fast = true; 13079 return true; 13080 } 13081 return false; 13082 } 13083 } 13084 } 13085 13086 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 13087 unsigned AlignCheck) { 13088 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 13089 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 13090 } 13091 13092 EVT ARMTargetLowering::getOptimalMemOpType( 13093 uint64_t Size, unsigned DstAlign, unsigned SrcAlign, bool IsMemset, 13094 bool ZeroMemset, bool MemcpyStrSrc, 13095 const AttributeList &FuncAttributes) const { 13096 // See if we can use NEON instructions for this... 13097 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() && 13098 !FuncAttributes.hasFnAttribute(Attribute::NoImplicitFloat)) { 13099 bool Fast; 13100 if (Size >= 16 && 13101 (memOpAlign(SrcAlign, DstAlign, 16) || 13102 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, 13103 MachineMemOperand::MONone, &Fast) && 13104 Fast))) { 13105 return MVT::v2f64; 13106 } else if (Size >= 8 && 13107 (memOpAlign(SrcAlign, DstAlign, 8) || 13108 (allowsMisalignedMemoryAccesses( 13109 MVT::f64, 0, 1, MachineMemOperand::MONone, &Fast) && 13110 Fast))) { 13111 return MVT::f64; 13112 } 13113 } 13114 13115 // Let the target-independent logic figure it out. 13116 return MVT::Other; 13117 } 13118 13119 // 64-bit integers are split into their high and low parts and held in two 13120 // different registers, so the trunc is free since the low register can just 13121 // be used. 13122 bool ARMTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const { 13123 if (!SrcTy->isIntegerTy() || !DstTy->isIntegerTy()) 13124 return false; 13125 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); 13126 unsigned DestBits = DstTy->getPrimitiveSizeInBits(); 13127 return (SrcBits == 64 && DestBits == 32); 13128 } 13129 13130 bool ARMTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const { 13131 if (SrcVT.isVector() || DstVT.isVector() || !SrcVT.isInteger() || 13132 !DstVT.isInteger()) 13133 return false; 13134 unsigned SrcBits = SrcVT.getSizeInBits(); 13135 unsigned DestBits = DstVT.getSizeInBits(); 13136 return (SrcBits == 64 && DestBits == 32); 13137 } 13138 13139 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 13140 if (Val.getOpcode() != ISD::LOAD) 13141 return false; 13142 13143 EVT VT1 = Val.getValueType(); 13144 if (!VT1.isSimple() || !VT1.isInteger() || 13145 !VT2.isSimple() || !VT2.isInteger()) 13146 return false; 13147 13148 switch (VT1.getSimpleVT().SimpleTy) { 13149 default: break; 13150 case MVT::i1: 13151 case MVT::i8: 13152 case MVT::i16: 13153 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 13154 return true; 13155 } 13156 13157 return false; 13158 } 13159 13160 bool ARMTargetLowering::isFNegFree(EVT VT) const { 13161 if (!VT.isSimple()) 13162 return false; 13163 13164 // There are quite a few FP16 instructions (e.g. VNMLA, VNMLS, etc.) that 13165 // negate values directly (fneg is free). So, we don't want to let the DAG 13166 // combiner rewrite fneg into xors and some other instructions. For f16 and 13167 // FullFP16 argument passing, some bitcast nodes may be introduced, 13168 // triggering this DAG combine rewrite, so we are avoiding that with this. 13169 switch (VT.getSimpleVT().SimpleTy) { 13170 default: break; 13171 case MVT::f16: 13172 return Subtarget->hasFullFP16(); 13173 } 13174 13175 return false; 13176 } 13177 13178 /// Check if Ext1 and Ext2 are extends of the same type, doubling the bitwidth 13179 /// of the vector elements. 13180 static bool areExtractExts(Value *Ext1, Value *Ext2) { 13181 auto areExtDoubled = [](Instruction *Ext) { 13182 return Ext->getType()->getScalarSizeInBits() == 13183 2 * Ext->getOperand(0)->getType()->getScalarSizeInBits(); 13184 }; 13185 13186 if (!match(Ext1, m_ZExtOrSExt(m_Value())) || 13187 !match(Ext2, m_ZExtOrSExt(m_Value())) || 13188 !areExtDoubled(cast<Instruction>(Ext1)) || 13189 !areExtDoubled(cast<Instruction>(Ext2))) 13190 return false; 13191 13192 return true; 13193 } 13194 13195 /// Check if sinking \p I's operands to I's basic block is profitable, because 13196 /// the operands can be folded into a target instruction, e.g. 13197 /// sext/zext can be folded into vsubl. 13198 bool ARMTargetLowering::shouldSinkOperands(Instruction *I, 13199 SmallVectorImpl<Use *> &Ops) const { 13200 if (!Subtarget->hasNEON() || !I->getType()->isVectorTy()) 13201 return false; 13202 13203 switch (I->getOpcode()) { 13204 case Instruction::Sub: 13205 case Instruction::Add: { 13206 if (!areExtractExts(I->getOperand(0), I->getOperand(1))) 13207 return false; 13208 Ops.push_back(&I->getOperandUse(0)); 13209 Ops.push_back(&I->getOperandUse(1)); 13210 return true; 13211 } 13212 default: 13213 return false; 13214 } 13215 return false; 13216 } 13217 13218 bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 13219 EVT VT = ExtVal.getValueType(); 13220 13221 if (!isTypeLegal(VT)) 13222 return false; 13223 13224 // Don't create a loadext if we can fold the extension into a wide/long 13225 // instruction. 13226 // If there's more than one user instruction, the loadext is desirable no 13227 // matter what. There can be two uses by the same instruction. 13228 if (ExtVal->use_empty() || 13229 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode())) 13230 return true; 13231 13232 SDNode *U = *ExtVal->use_begin(); 13233 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB || 13234 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL)) 13235 return false; 13236 13237 return true; 13238 } 13239 13240 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 13241 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 13242 return false; 13243 13244 if (!isTypeLegal(EVT::getEVT(Ty1))) 13245 return false; 13246 13247 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 13248 13249 // Assuming the caller doesn't have a zeroext or signext return parameter, 13250 // truncation all the way down to i1 is valid. 13251 return true; 13252 } 13253 13254 int ARMTargetLowering::getScalingFactorCost(const DataLayout &DL, 13255 const AddrMode &AM, Type *Ty, 13256 unsigned AS) const { 13257 if (isLegalAddressingMode(DL, AM, Ty, AS)) { 13258 if (Subtarget->hasFPAO()) 13259 return AM.Scale < 0 ? 1 : 0; // positive offsets execute faster 13260 return 0; 13261 } 13262 return -1; 13263 } 13264 13265 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 13266 if (V < 0) 13267 return false; 13268 13269 unsigned Scale = 1; 13270 switch (VT.getSimpleVT().SimpleTy) { 13271 case MVT::i1: 13272 case MVT::i8: 13273 // Scale == 1; 13274 break; 13275 case MVT::i16: 13276 // Scale == 2; 13277 Scale = 2; 13278 break; 13279 default: 13280 // On thumb1 we load most things (i32, i64, floats, etc) with a LDR 13281 // Scale == 4; 13282 Scale = 4; 13283 break; 13284 } 13285 13286 if ((V & (Scale - 1)) != 0) 13287 return false; 13288 return isUInt<5>(V / Scale); 13289 } 13290 13291 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 13292 const ARMSubtarget *Subtarget) { 13293 if (!VT.isInteger() && !VT.isFloatingPoint()) 13294 return false; 13295 if (VT.isVector() && Subtarget->hasNEON()) 13296 return false; 13297 if (VT.isVector() && VT.isFloatingPoint() && Subtarget->hasMVEIntegerOps() && 13298 !Subtarget->hasMVEFloatOps()) 13299 return false; 13300 13301 bool IsNeg = false; 13302 if (V < 0) { 13303 IsNeg = true; 13304 V = -V; 13305 } 13306 13307 unsigned NumBytes = std::max(VT.getSizeInBits() / 8, 1U); 13308 13309 // MVE: size * imm7 13310 if (VT.isVector() && Subtarget->hasMVEIntegerOps()) { 13311 switch (VT.getSimpleVT().getVectorElementType().SimpleTy) { 13312 case MVT::i32: 13313 case MVT::f32: 13314 return isShiftedUInt<7,2>(V); 13315 case MVT::i16: 13316 case MVT::f16: 13317 return isShiftedUInt<7,1>(V); 13318 case MVT::i8: 13319 return isUInt<7>(V); 13320 default: 13321 return false; 13322 } 13323 } 13324 13325 // half VLDR: 2 * imm8 13326 if (VT.isFloatingPoint() && NumBytes == 2 && Subtarget->hasFPRegs16()) 13327 return isShiftedUInt<8, 1>(V); 13328 // VLDR and LDRD: 4 * imm8 13329 if ((VT.isFloatingPoint() && Subtarget->hasVFP2Base()) || NumBytes == 8) 13330 return isShiftedUInt<8, 2>(V); 13331 13332 if (NumBytes == 1 || NumBytes == 2 || NumBytes == 4) { 13333 // + imm12 or - imm8 13334 if (IsNeg) 13335 return isUInt<8>(V); 13336 return isUInt<12>(V); 13337 } 13338 13339 return false; 13340 } 13341 13342 /// isLegalAddressImmediate - Return true if the integer value can be used 13343 /// as the offset of the target addressing mode for load / store of the 13344 /// given type. 13345 static bool isLegalAddressImmediate(int64_t V, EVT VT, 13346 const ARMSubtarget *Subtarget) { 13347 if (V == 0) 13348 return true; 13349 13350 if (!VT.isSimple()) 13351 return false; 13352 13353 if (Subtarget->isThumb1Only()) 13354 return isLegalT1AddressImmediate(V, VT); 13355 else if (Subtarget->isThumb2()) 13356 return isLegalT2AddressImmediate(V, VT, Subtarget); 13357 13358 // ARM mode. 13359 if (V < 0) 13360 V = - V; 13361 switch (VT.getSimpleVT().SimpleTy) { 13362 default: return false; 13363 case MVT::i1: 13364 case MVT::i8: 13365 case MVT::i32: 13366 // +- imm12 13367 return isUInt<12>(V); 13368 case MVT::i16: 13369 // +- imm8 13370 return isUInt<8>(V); 13371 case MVT::f32: 13372 case MVT::f64: 13373 if (!Subtarget->hasVFP2Base()) // FIXME: NEON? 13374 return false; 13375 return isShiftedUInt<8, 2>(V); 13376 } 13377 } 13378 13379 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 13380 EVT VT) const { 13381 int Scale = AM.Scale; 13382 if (Scale < 0) 13383 return false; 13384 13385 switch (VT.getSimpleVT().SimpleTy) { 13386 default: return false; 13387 case MVT::i1: 13388 case MVT::i8: 13389 case MVT::i16: 13390 case MVT::i32: 13391 if (Scale == 1) 13392 return true; 13393 // r + r << imm 13394 Scale = Scale & ~1; 13395 return Scale == 2 || Scale == 4 || Scale == 8; 13396 case MVT::i64: 13397 // FIXME: What are we trying to model here? ldrd doesn't have an r + r 13398 // version in Thumb mode. 13399 // r + r 13400 if (Scale == 1) 13401 return true; 13402 // r * 2 (this can be lowered to r + r). 13403 if (!AM.HasBaseReg && Scale == 2) 13404 return true; 13405 return false; 13406 case MVT::isVoid: 13407 // Note, we allow "void" uses (basically, uses that aren't loads or 13408 // stores), because arm allows folding a scale into many arithmetic 13409 // operations. This should be made more precise and revisited later. 13410 13411 // Allow r << imm, but the imm has to be a multiple of two. 13412 if (Scale & 1) return false; 13413 return isPowerOf2_32(Scale); 13414 } 13415 } 13416 13417 bool ARMTargetLowering::isLegalT1ScaledAddressingMode(const AddrMode &AM, 13418 EVT VT) const { 13419 const int Scale = AM.Scale; 13420 13421 // Negative scales are not supported in Thumb1. 13422 if (Scale < 0) 13423 return false; 13424 13425 // Thumb1 addressing modes do not support register scaling excepting the 13426 // following cases: 13427 // 1. Scale == 1 means no scaling. 13428 // 2. Scale == 2 this can be lowered to r + r if there is no base register. 13429 return (Scale == 1) || (!AM.HasBaseReg && Scale == 2); 13430 } 13431 13432 /// isLegalAddressingMode - Return true if the addressing mode represented 13433 /// by AM is legal for this target, for a load/store of the specified type. 13434 bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL, 13435 const AddrMode &AM, Type *Ty, 13436 unsigned AS, Instruction *I) const { 13437 EVT VT = getValueType(DL, Ty, true); 13438 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 13439 return false; 13440 13441 // Can never fold addr of global into load/store. 13442 if (AM.BaseGV) 13443 return false; 13444 13445 switch (AM.Scale) { 13446 case 0: // no scale reg, must be "r+i" or "r", or "i". 13447 break; 13448 default: 13449 // ARM doesn't support any R+R*scale+imm addr modes. 13450 if (AM.BaseOffs) 13451 return false; 13452 13453 if (!VT.isSimple()) 13454 return false; 13455 13456 if (Subtarget->isThumb1Only()) 13457 return isLegalT1ScaledAddressingMode(AM, VT); 13458 13459 if (Subtarget->isThumb2()) 13460 return isLegalT2ScaledAddressingMode(AM, VT); 13461 13462 int Scale = AM.Scale; 13463 switch (VT.getSimpleVT().SimpleTy) { 13464 default: return false; 13465 case MVT::i1: 13466 case MVT::i8: 13467 case MVT::i32: 13468 if (Scale < 0) Scale = -Scale; 13469 if (Scale == 1) 13470 return true; 13471 // r + r << imm 13472 return isPowerOf2_32(Scale & ~1); 13473 case MVT::i16: 13474 case MVT::i64: 13475 // r +/- r 13476 if (Scale == 1 || (AM.HasBaseReg && Scale == -1)) 13477 return true; 13478 // r * 2 (this can be lowered to r + r). 13479 if (!AM.HasBaseReg && Scale == 2) 13480 return true; 13481 return false; 13482 13483 case MVT::isVoid: 13484 // Note, we allow "void" uses (basically, uses that aren't loads or 13485 // stores), because arm allows folding a scale into many arithmetic 13486 // operations. This should be made more precise and revisited later. 13487 13488 // Allow r << imm, but the imm has to be a multiple of two. 13489 if (Scale & 1) return false; 13490 return isPowerOf2_32(Scale); 13491 } 13492 } 13493 return true; 13494 } 13495 13496 /// isLegalICmpImmediate - Return true if the specified immediate is legal 13497 /// icmp immediate, that is the target has icmp instructions which can compare 13498 /// a register against the immediate without having to materialize the 13499 /// immediate into a register. 13500 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 13501 // Thumb2 and ARM modes can use cmn for negative immediates. 13502 if (!Subtarget->isThumb()) 13503 return ARM_AM::getSOImmVal((uint32_t)Imm) != -1 || 13504 ARM_AM::getSOImmVal(-(uint32_t)Imm) != -1; 13505 if (Subtarget->isThumb2()) 13506 return ARM_AM::getT2SOImmVal((uint32_t)Imm) != -1 || 13507 ARM_AM::getT2SOImmVal(-(uint32_t)Imm) != -1; 13508 // Thumb1 doesn't have cmn, and only 8-bit immediates. 13509 return Imm >= 0 && Imm <= 255; 13510 } 13511 13512 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 13513 /// *or sub* immediate, that is the target has add or sub instructions which can 13514 /// add a register with the immediate without having to materialize the 13515 /// immediate into a register. 13516 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 13517 // Same encoding for add/sub, just flip the sign. 13518 int64_t AbsImm = std::abs(Imm); 13519 if (!Subtarget->isThumb()) 13520 return ARM_AM::getSOImmVal(AbsImm) != -1; 13521 if (Subtarget->isThumb2()) 13522 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 13523 // Thumb1 only has 8-bit unsigned immediate. 13524 return AbsImm >= 0 && AbsImm <= 255; 13525 } 13526 13527 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 13528 bool isSEXTLoad, SDValue &Base, 13529 SDValue &Offset, bool &isInc, 13530 SelectionDAG &DAG) { 13531 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 13532 return false; 13533 13534 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 13535 // AddressingMode 3 13536 Base = Ptr->getOperand(0); 13537 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 13538 int RHSC = (int)RHS->getZExtValue(); 13539 if (RHSC < 0 && RHSC > -256) { 13540 assert(Ptr->getOpcode() == ISD::ADD); 13541 isInc = false; 13542 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 13543 return true; 13544 } 13545 } 13546 isInc = (Ptr->getOpcode() == ISD::ADD); 13547 Offset = Ptr->getOperand(1); 13548 return true; 13549 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 13550 // AddressingMode 2 13551 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 13552 int RHSC = (int)RHS->getZExtValue(); 13553 if (RHSC < 0 && RHSC > -0x1000) { 13554 assert(Ptr->getOpcode() == ISD::ADD); 13555 isInc = false; 13556 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 13557 Base = Ptr->getOperand(0); 13558 return true; 13559 } 13560 } 13561 13562 if (Ptr->getOpcode() == ISD::ADD) { 13563 isInc = true; 13564 ARM_AM::ShiftOpc ShOpcVal= 13565 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 13566 if (ShOpcVal != ARM_AM::no_shift) { 13567 Base = Ptr->getOperand(1); 13568 Offset = Ptr->getOperand(0); 13569 } else { 13570 Base = Ptr->getOperand(0); 13571 Offset = Ptr->getOperand(1); 13572 } 13573 return true; 13574 } 13575 13576 isInc = (Ptr->getOpcode() == ISD::ADD); 13577 Base = Ptr->getOperand(0); 13578 Offset = Ptr->getOperand(1); 13579 return true; 13580 } 13581 13582 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 13583 return false; 13584 } 13585 13586 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 13587 bool isSEXTLoad, SDValue &Base, 13588 SDValue &Offset, bool &isInc, 13589 SelectionDAG &DAG) { 13590 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 13591 return false; 13592 13593 Base = Ptr->getOperand(0); 13594 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 13595 int RHSC = (int)RHS->getZExtValue(); 13596 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 13597 assert(Ptr->getOpcode() == ISD::ADD); 13598 isInc = false; 13599 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 13600 return true; 13601 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 13602 isInc = Ptr->getOpcode() == ISD::ADD; 13603 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0)); 13604 return true; 13605 } 13606 } 13607 13608 return false; 13609 } 13610 13611 /// getPreIndexedAddressParts - returns true by value, base pointer and 13612 /// offset pointer and addressing mode by reference if the node's address 13613 /// can be legally represented as pre-indexed load / store address. 13614 bool 13615 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 13616 SDValue &Offset, 13617 ISD::MemIndexedMode &AM, 13618 SelectionDAG &DAG) const { 13619 if (Subtarget->isThumb1Only()) 13620 return false; 13621 13622 EVT VT; 13623 SDValue Ptr; 13624 bool isSEXTLoad = false; 13625 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 13626 Ptr = LD->getBasePtr(); 13627 VT = LD->getMemoryVT(); 13628 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 13629 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 13630 Ptr = ST->getBasePtr(); 13631 VT = ST->getMemoryVT(); 13632 } else 13633 return false; 13634 13635 bool isInc; 13636 bool isLegal = false; 13637 if (Subtarget->isThumb2()) 13638 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 13639 Offset, isInc, DAG); 13640 else 13641 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 13642 Offset, isInc, DAG); 13643 if (!isLegal) 13644 return false; 13645 13646 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 13647 return true; 13648 } 13649 13650 /// getPostIndexedAddressParts - returns true by value, base pointer and 13651 /// offset pointer and addressing mode by reference if this node can be 13652 /// combined with a load / store to form a post-indexed load / store. 13653 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 13654 SDValue &Base, 13655 SDValue &Offset, 13656 ISD::MemIndexedMode &AM, 13657 SelectionDAG &DAG) const { 13658 EVT VT; 13659 SDValue Ptr; 13660 bool isSEXTLoad = false, isNonExt; 13661 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 13662 VT = LD->getMemoryVT(); 13663 Ptr = LD->getBasePtr(); 13664 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 13665 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD; 13666 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 13667 VT = ST->getMemoryVT(); 13668 Ptr = ST->getBasePtr(); 13669 isNonExt = !ST->isTruncatingStore(); 13670 } else 13671 return false; 13672 13673 if (Subtarget->isThumb1Only()) { 13674 // Thumb-1 can do a limited post-inc load or store as an updating LDM. It 13675 // must be non-extending/truncating, i32, with an offset of 4. 13676 assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!"); 13677 if (Op->getOpcode() != ISD::ADD || !isNonExt) 13678 return false; 13679 auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1)); 13680 if (!RHS || RHS->getZExtValue() != 4) 13681 return false; 13682 13683 Offset = Op->getOperand(1); 13684 Base = Op->getOperand(0); 13685 AM = ISD::POST_INC; 13686 return true; 13687 } 13688 13689 bool isInc; 13690 bool isLegal = false; 13691 if (Subtarget->isThumb2()) 13692 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 13693 isInc, DAG); 13694 else 13695 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 13696 isInc, DAG); 13697 if (!isLegal) 13698 return false; 13699 13700 if (Ptr != Base) { 13701 // Swap base ptr and offset to catch more post-index load / store when 13702 // it's legal. In Thumb2 mode, offset must be an immediate. 13703 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 13704 !Subtarget->isThumb2()) 13705 std::swap(Base, Offset); 13706 13707 // Post-indexed load / store update the base pointer. 13708 if (Ptr != Base) 13709 return false; 13710 } 13711 13712 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 13713 return true; 13714 } 13715 13716 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 13717 KnownBits &Known, 13718 const APInt &DemandedElts, 13719 const SelectionDAG &DAG, 13720 unsigned Depth) const { 13721 unsigned BitWidth = Known.getBitWidth(); 13722 Known.resetAll(); 13723 switch (Op.getOpcode()) { 13724 default: break; 13725 case ARMISD::ADDC: 13726 case ARMISD::ADDE: 13727 case ARMISD::SUBC: 13728 case ARMISD::SUBE: 13729 // Special cases when we convert a carry to a boolean. 13730 if (Op.getResNo() == 0) { 13731 SDValue LHS = Op.getOperand(0); 13732 SDValue RHS = Op.getOperand(1); 13733 // (ADDE 0, 0, C) will give us a single bit. 13734 if (Op->getOpcode() == ARMISD::ADDE && isNullConstant(LHS) && 13735 isNullConstant(RHS)) { 13736 Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 13737 return; 13738 } 13739 } 13740 break; 13741 case ARMISD::CMOV: { 13742 // Bits are known zero/one if known on the LHS and RHS. 13743 Known = DAG.computeKnownBits(Op.getOperand(0), Depth+1); 13744 if (Known.isUnknown()) 13745 return; 13746 13747 KnownBits KnownRHS = DAG.computeKnownBits(Op.getOperand(1), Depth+1); 13748 Known.Zero &= KnownRHS.Zero; 13749 Known.One &= KnownRHS.One; 13750 return; 13751 } 13752 case ISD::INTRINSIC_W_CHAIN: { 13753 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 13754 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 13755 switch (IntID) { 13756 default: return; 13757 case Intrinsic::arm_ldaex: 13758 case Intrinsic::arm_ldrex: { 13759 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 13760 unsigned MemBits = VT.getScalarSizeInBits(); 13761 Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 13762 return; 13763 } 13764 } 13765 } 13766 case ARMISD::BFI: { 13767 // Conservatively, we can recurse down the first operand 13768 // and just mask out all affected bits. 13769 Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); 13770 13771 // The operand to BFI is already a mask suitable for removing the bits it 13772 // sets. 13773 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2)); 13774 const APInt &Mask = CI->getAPIntValue(); 13775 Known.Zero &= Mask; 13776 Known.One &= Mask; 13777 return; 13778 } 13779 case ARMISD::VGETLANEs: 13780 case ARMISD::VGETLANEu: { 13781 const SDValue &SrcSV = Op.getOperand(0); 13782 EVT VecVT = SrcSV.getValueType(); 13783 assert(VecVT.isVector() && "VGETLANE expected a vector type"); 13784 const unsigned NumSrcElts = VecVT.getVectorNumElements(); 13785 ConstantSDNode *Pos = cast<ConstantSDNode>(Op.getOperand(1).getNode()); 13786 assert(Pos->getAPIntValue().ult(NumSrcElts) && 13787 "VGETLANE index out of bounds"); 13788 unsigned Idx = Pos->getZExtValue(); 13789 APInt DemandedElt = APInt::getOneBitSet(NumSrcElts, Idx); 13790 Known = DAG.computeKnownBits(SrcSV, DemandedElt, Depth + 1); 13791 13792 EVT VT = Op.getValueType(); 13793 const unsigned DstSz = VT.getScalarSizeInBits(); 13794 const unsigned SrcSz = VecVT.getVectorElementType().getSizeInBits(); 13795 (void)SrcSz; 13796 assert(SrcSz == Known.getBitWidth()); 13797 assert(DstSz > SrcSz); 13798 if (Op.getOpcode() == ARMISD::VGETLANEs) 13799 Known = Known.sext(DstSz); 13800 else { 13801 Known = Known.zext(DstSz, true /* extended bits are known zero */); 13802 } 13803 assert(DstSz == Known.getBitWidth()); 13804 break; 13805 } 13806 } 13807 } 13808 13809 bool 13810 ARMTargetLowering::targetShrinkDemandedConstant(SDValue Op, 13811 const APInt &DemandedAPInt, 13812 TargetLoweringOpt &TLO) const { 13813 // Delay optimization, so we don't have to deal with illegal types, or block 13814 // optimizations. 13815 if (!TLO.LegalOps) 13816 return false; 13817 13818 // Only optimize AND for now. 13819 if (Op.getOpcode() != ISD::AND) 13820 return false; 13821 13822 EVT VT = Op.getValueType(); 13823 13824 // Ignore vectors. 13825 if (VT.isVector()) 13826 return false; 13827 13828 assert(VT == MVT::i32 && "Unexpected integer type"); 13829 13830 // Make sure the RHS really is a constant. 13831 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 13832 if (!C) 13833 return false; 13834 13835 unsigned Mask = C->getZExtValue(); 13836 13837 unsigned Demanded = DemandedAPInt.getZExtValue(); 13838 unsigned ShrunkMask = Mask & Demanded; 13839 unsigned ExpandedMask = Mask | ~Demanded; 13840 13841 // If the mask is all zeros, let the target-independent code replace the 13842 // result with zero. 13843 if (ShrunkMask == 0) 13844 return false; 13845 13846 // If the mask is all ones, erase the AND. (Currently, the target-independent 13847 // code won't do this, so we have to do it explicitly to avoid an infinite 13848 // loop in obscure cases.) 13849 if (ExpandedMask == ~0U) 13850 return TLO.CombineTo(Op, Op.getOperand(0)); 13851 13852 auto IsLegalMask = [ShrunkMask, ExpandedMask](unsigned Mask) -> bool { 13853 return (ShrunkMask & Mask) == ShrunkMask && (~ExpandedMask & Mask) == 0; 13854 }; 13855 auto UseMask = [Mask, Op, VT, &TLO](unsigned NewMask) -> bool { 13856 if (NewMask == Mask) 13857 return true; 13858 SDLoc DL(Op); 13859 SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT); 13860 SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC); 13861 return TLO.CombineTo(Op, NewOp); 13862 }; 13863 13864 // Prefer uxtb mask. 13865 if (IsLegalMask(0xFF)) 13866 return UseMask(0xFF); 13867 13868 // Prefer uxth mask. 13869 if (IsLegalMask(0xFFFF)) 13870 return UseMask(0xFFFF); 13871 13872 // [1, 255] is Thumb1 movs+ands, legal immediate for ARM/Thumb2. 13873 // FIXME: Prefer a contiguous sequence of bits for other optimizations. 13874 if (ShrunkMask < 256) 13875 return UseMask(ShrunkMask); 13876 13877 // [-256, -2] is Thumb1 movs+bics, legal immediate for ARM/Thumb2. 13878 // FIXME: Prefer a contiguous sequence of bits for other optimizations. 13879 if ((int)ExpandedMask <= -2 && (int)ExpandedMask >= -256) 13880 return UseMask(ExpandedMask); 13881 13882 // Potential improvements: 13883 // 13884 // We could try to recognize lsls+lsrs or lsrs+lsls pairs here. 13885 // We could try to prefer Thumb1 immediates which can be lowered to a 13886 // two-instruction sequence. 13887 // We could try to recognize more legal ARM/Thumb2 immediates here. 13888 13889 return false; 13890 } 13891 13892 13893 //===----------------------------------------------------------------------===// 13894 // ARM Inline Assembly Support 13895 //===----------------------------------------------------------------------===// 13896 13897 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 13898 // Looking for "rev" which is V6+. 13899 if (!Subtarget->hasV6Ops()) 13900 return false; 13901 13902 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 13903 std::string AsmStr = IA->getAsmString(); 13904 SmallVector<StringRef, 4> AsmPieces; 13905 SplitString(AsmStr, AsmPieces, ";\n"); 13906 13907 switch (AsmPieces.size()) { 13908 default: return false; 13909 case 1: 13910 AsmStr = AsmPieces[0]; 13911 AsmPieces.clear(); 13912 SplitString(AsmStr, AsmPieces, " \t,"); 13913 13914 // rev $0, $1 13915 if (AsmPieces.size() == 3 && 13916 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 13917 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 13918 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 13919 if (Ty && Ty->getBitWidth() == 32) 13920 return IntrinsicLowering::LowerToByteSwap(CI); 13921 } 13922 break; 13923 } 13924 13925 return false; 13926 } 13927 13928 const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const { 13929 // At this point, we have to lower this constraint to something else, so we 13930 // lower it to an "r" or "w". However, by doing this we will force the result 13931 // to be in register, while the X constraint is much more permissive. 13932 // 13933 // Although we are correct (we are free to emit anything, without 13934 // constraints), we might break use cases that would expect us to be more 13935 // efficient and emit something else. 13936 if (!Subtarget->hasVFP2Base()) 13937 return "r"; 13938 if (ConstraintVT.isFloatingPoint()) 13939 return "w"; 13940 if (ConstraintVT.isVector() && Subtarget->hasNEON() && 13941 (ConstraintVT.getSizeInBits() == 64 || 13942 ConstraintVT.getSizeInBits() == 128)) 13943 return "w"; 13944 13945 return "r"; 13946 } 13947 13948 /// getConstraintType - Given a constraint letter, return the type of 13949 /// constraint it is for this target. 13950 ARMTargetLowering::ConstraintType 13951 ARMTargetLowering::getConstraintType(StringRef Constraint) const { 13952 if (Constraint.size() == 1) { 13953 switch (Constraint[0]) { 13954 default: break; 13955 case 'l': return C_RegisterClass; 13956 case 'w': return C_RegisterClass; 13957 case 'h': return C_RegisterClass; 13958 case 'x': return C_RegisterClass; 13959 case 't': return C_RegisterClass; 13960 case 'j': return C_Other; // Constant for movw. 13961 // An address with a single base register. Due to the way we 13962 // currently handle addresses it is the same as an 'r' memory constraint. 13963 case 'Q': return C_Memory; 13964 } 13965 } else if (Constraint.size() == 2) { 13966 switch (Constraint[0]) { 13967 default: break; 13968 // All 'U+' constraints are addresses. 13969 case 'U': return C_Memory; 13970 } 13971 } 13972 return TargetLowering::getConstraintType(Constraint); 13973 } 13974 13975 /// Examine constraint type and operand type and determine a weight value. 13976 /// This object must already have been set up with the operand type 13977 /// and the current alternative constraint selected. 13978 TargetLowering::ConstraintWeight 13979 ARMTargetLowering::getSingleConstraintMatchWeight( 13980 AsmOperandInfo &info, const char *constraint) const { 13981 ConstraintWeight weight = CW_Invalid; 13982 Value *CallOperandVal = info.CallOperandVal; 13983 // If we don't have a value, we can't do a match, 13984 // but allow it at the lowest weight. 13985 if (!CallOperandVal) 13986 return CW_Default; 13987 Type *type = CallOperandVal->getType(); 13988 // Look at the constraint type. 13989 switch (*constraint) { 13990 default: 13991 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 13992 break; 13993 case 'l': 13994 if (type->isIntegerTy()) { 13995 if (Subtarget->isThumb()) 13996 weight = CW_SpecificReg; 13997 else 13998 weight = CW_Register; 13999 } 14000 break; 14001 case 'w': 14002 if (type->isFloatingPointTy()) 14003 weight = CW_Register; 14004 break; 14005 } 14006 return weight; 14007 } 14008 14009 using RCPair = std::pair<unsigned, const TargetRegisterClass *>; 14010 14011 RCPair ARMTargetLowering::getRegForInlineAsmConstraint( 14012 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 14013 if (Constraint.size() == 1) { 14014 // GCC ARM Constraint Letters 14015 switch (Constraint[0]) { 14016 case 'l': // Low regs or general regs. 14017 if (Subtarget->isThumb()) 14018 return RCPair(0U, &ARM::tGPRRegClass); 14019 return RCPair(0U, &ARM::GPRRegClass); 14020 case 'h': // High regs or no regs. 14021 if (Subtarget->isThumb()) 14022 return RCPair(0U, &ARM::hGPRRegClass); 14023 break; 14024 case 'r': 14025 if (Subtarget->isThumb1Only()) 14026 return RCPair(0U, &ARM::tGPRRegClass); 14027 return RCPair(0U, &ARM::GPRRegClass); 14028 case 'w': 14029 if (VT == MVT::Other) 14030 break; 14031 if (VT == MVT::f32) 14032 return RCPair(0U, &ARM::SPRRegClass); 14033 if (VT.getSizeInBits() == 64) 14034 return RCPair(0U, &ARM::DPRRegClass); 14035 if (VT.getSizeInBits() == 128) 14036 return RCPair(0U, &ARM::QPRRegClass); 14037 break; 14038 case 'x': 14039 if (VT == MVT::Other) 14040 break; 14041 if (VT == MVT::f32) 14042 return RCPair(0U, &ARM::SPR_8RegClass); 14043 if (VT.getSizeInBits() == 64) 14044 return RCPair(0U, &ARM::DPR_8RegClass); 14045 if (VT.getSizeInBits() == 128) 14046 return RCPair(0U, &ARM::QPR_8RegClass); 14047 break; 14048 case 't': 14049 if (VT == MVT::Other) 14050 break; 14051 if (VT == MVT::f32 || VT == MVT::i32) 14052 return RCPair(0U, &ARM::SPRRegClass); 14053 if (VT.getSizeInBits() == 64) 14054 return RCPair(0U, &ARM::DPR_VFP2RegClass); 14055 if (VT.getSizeInBits() == 128) 14056 return RCPair(0U, &ARM::QPR_VFP2RegClass); 14057 break; 14058 } 14059 } 14060 if (StringRef("{cc}").equals_lower(Constraint)) 14061 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 14062 14063 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 14064 } 14065 14066 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 14067 /// vector. If it is invalid, don't add anything to Ops. 14068 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 14069 std::string &Constraint, 14070 std::vector<SDValue>&Ops, 14071 SelectionDAG &DAG) const { 14072 SDValue Result; 14073 14074 // Currently only support length 1 constraints. 14075 if (Constraint.length() != 1) return; 14076 14077 char ConstraintLetter = Constraint[0]; 14078 switch (ConstraintLetter) { 14079 default: break; 14080 case 'j': 14081 case 'I': case 'J': case 'K': case 'L': 14082 case 'M': case 'N': case 'O': 14083 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 14084 if (!C) 14085 return; 14086 14087 int64_t CVal64 = C->getSExtValue(); 14088 int CVal = (int) CVal64; 14089 // None of these constraints allow values larger than 32 bits. Check 14090 // that the value fits in an int. 14091 if (CVal != CVal64) 14092 return; 14093 14094 switch (ConstraintLetter) { 14095 case 'j': 14096 // Constant suitable for movw, must be between 0 and 14097 // 65535. 14098 if (Subtarget->hasV6T2Ops()) 14099 if (CVal >= 0 && CVal <= 65535) 14100 break; 14101 return; 14102 case 'I': 14103 if (Subtarget->isThumb1Only()) { 14104 // This must be a constant between 0 and 255, for ADD 14105 // immediates. 14106 if (CVal >= 0 && CVal <= 255) 14107 break; 14108 } else if (Subtarget->isThumb2()) { 14109 // A constant that can be used as an immediate value in a 14110 // data-processing instruction. 14111 if (ARM_AM::getT2SOImmVal(CVal) != -1) 14112 break; 14113 } else { 14114 // A constant that can be used as an immediate value in a 14115 // data-processing instruction. 14116 if (ARM_AM::getSOImmVal(CVal) != -1) 14117 break; 14118 } 14119 return; 14120 14121 case 'J': 14122 if (Subtarget->isThumb1Only()) { 14123 // This must be a constant between -255 and -1, for negated ADD 14124 // immediates. This can be used in GCC with an "n" modifier that 14125 // prints the negated value, for use with SUB instructions. It is 14126 // not useful otherwise but is implemented for compatibility. 14127 if (CVal >= -255 && CVal <= -1) 14128 break; 14129 } else { 14130 // This must be a constant between -4095 and 4095. It is not clear 14131 // what this constraint is intended for. Implemented for 14132 // compatibility with GCC. 14133 if (CVal >= -4095 && CVal <= 4095) 14134 break; 14135 } 14136 return; 14137 14138 case 'K': 14139 if (Subtarget->isThumb1Only()) { 14140 // A 32-bit value where only one byte has a nonzero value. Exclude 14141 // zero to match GCC. This constraint is used by GCC internally for 14142 // constants that can be loaded with a move/shift combination. 14143 // It is not useful otherwise but is implemented for compatibility. 14144 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 14145 break; 14146 } else if (Subtarget->isThumb2()) { 14147 // A constant whose bitwise inverse can be used as an immediate 14148 // value in a data-processing instruction. This can be used in GCC 14149 // with a "B" modifier that prints the inverted value, for use with 14150 // BIC and MVN instructions. It is not useful otherwise but is 14151 // implemented for compatibility. 14152 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 14153 break; 14154 } else { 14155 // A constant whose bitwise inverse can be used as an immediate 14156 // value in a data-processing instruction. This can be used in GCC 14157 // with a "B" modifier that prints the inverted value, for use with 14158 // BIC and MVN instructions. It is not useful otherwise but is 14159 // implemented for compatibility. 14160 if (ARM_AM::getSOImmVal(~CVal) != -1) 14161 break; 14162 } 14163 return; 14164 14165 case 'L': 14166 if (Subtarget->isThumb1Only()) { 14167 // This must be a constant between -7 and 7, 14168 // for 3-operand ADD/SUB immediate instructions. 14169 if (CVal >= -7 && CVal < 7) 14170 break; 14171 } else if (Subtarget->isThumb2()) { 14172 // A constant whose negation can be used as an immediate value in a 14173 // data-processing instruction. This can be used in GCC with an "n" 14174 // modifier that prints the negated value, for use with SUB 14175 // instructions. It is not useful otherwise but is implemented for 14176 // compatibility. 14177 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 14178 break; 14179 } else { 14180 // A constant whose negation can be used as an immediate value in a 14181 // data-processing instruction. This can be used in GCC with an "n" 14182 // modifier that prints the negated value, for use with SUB 14183 // instructions. It is not useful otherwise but is implemented for 14184 // compatibility. 14185 if (ARM_AM::getSOImmVal(-CVal) != -1) 14186 break; 14187 } 14188 return; 14189 14190 case 'M': 14191 if (Subtarget->isThumb1Only()) { 14192 // This must be a multiple of 4 between 0 and 1020, for 14193 // ADD sp + immediate. 14194 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 14195 break; 14196 } else { 14197 // A power of two or a constant between 0 and 32. This is used in 14198 // GCC for the shift amount on shifted register operands, but it is 14199 // useful in general for any shift amounts. 14200 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 14201 break; 14202 } 14203 return; 14204 14205 case 'N': 14206 if (Subtarget->isThumb()) { // FIXME thumb2 14207 // This must be a constant between 0 and 31, for shift amounts. 14208 if (CVal >= 0 && CVal <= 31) 14209 break; 14210 } 14211 return; 14212 14213 case 'O': 14214 if (Subtarget->isThumb()) { // FIXME thumb2 14215 // This must be a multiple of 4 between -508 and 508, for 14216 // ADD/SUB sp = sp + immediate. 14217 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 14218 break; 14219 } 14220 return; 14221 } 14222 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType()); 14223 break; 14224 } 14225 14226 if (Result.getNode()) { 14227 Ops.push_back(Result); 14228 return; 14229 } 14230 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 14231 } 14232 14233 static RTLIB::Libcall getDivRemLibcall( 14234 const SDNode *N, MVT::SimpleValueType SVT) { 14235 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 14236 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 14237 "Unhandled Opcode in getDivRemLibcall"); 14238 bool isSigned = N->getOpcode() == ISD::SDIVREM || 14239 N->getOpcode() == ISD::SREM; 14240 RTLIB::Libcall LC; 14241 switch (SVT) { 14242 default: llvm_unreachable("Unexpected request for libcall!"); 14243 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 14244 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 14245 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 14246 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 14247 } 14248 return LC; 14249 } 14250 14251 static TargetLowering::ArgListTy getDivRemArgList( 14252 const SDNode *N, LLVMContext *Context, const ARMSubtarget *Subtarget) { 14253 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 14254 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 14255 "Unhandled Opcode in getDivRemArgList"); 14256 bool isSigned = N->getOpcode() == ISD::SDIVREM || 14257 N->getOpcode() == ISD::SREM; 14258 TargetLowering::ArgListTy Args; 14259 TargetLowering::ArgListEntry Entry; 14260 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 14261 EVT ArgVT = N->getOperand(i).getValueType(); 14262 Type *ArgTy = ArgVT.getTypeForEVT(*Context); 14263 Entry.Node = N->getOperand(i); 14264 Entry.Ty = ArgTy; 14265 Entry.IsSExt = isSigned; 14266 Entry.IsZExt = !isSigned; 14267 Args.push_back(Entry); 14268 } 14269 if (Subtarget->isTargetWindows() && Args.size() >= 2) 14270 std::swap(Args[0], Args[1]); 14271 return Args; 14272 } 14273 14274 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 14275 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 14276 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() || 14277 Subtarget->isTargetWindows()) && 14278 "Register-based DivRem lowering only"); 14279 unsigned Opcode = Op->getOpcode(); 14280 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 14281 "Invalid opcode for Div/Rem lowering"); 14282 bool isSigned = (Opcode == ISD::SDIVREM); 14283 EVT VT = Op->getValueType(0); 14284 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 14285 SDLoc dl(Op); 14286 14287 // If the target has hardware divide, use divide + multiply + subtract: 14288 // div = a / b 14289 // rem = a - b * div 14290 // return {div, rem} 14291 // This should be lowered into UDIV/SDIV + MLS later on. 14292 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode() 14293 : Subtarget->hasDivideInARMMode(); 14294 if (hasDivide && Op->getValueType(0).isSimple() && 14295 Op->getSimpleValueType(0) == MVT::i32) { 14296 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV; 14297 const SDValue Dividend = Op->getOperand(0); 14298 const SDValue Divisor = Op->getOperand(1); 14299 SDValue Div = DAG.getNode(DivOpcode, dl, VT, Dividend, Divisor); 14300 SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Div, Divisor); 14301 SDValue Rem = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul); 14302 14303 SDValue Values[2] = {Div, Rem}; 14304 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VT, VT), Values); 14305 } 14306 14307 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(), 14308 VT.getSimpleVT().SimpleTy); 14309 SDValue InChain = DAG.getEntryNode(); 14310 14311 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(), 14312 DAG.getContext(), 14313 Subtarget); 14314 14315 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 14316 getPointerTy(DAG.getDataLayout())); 14317 14318 Type *RetTy = StructType::get(Ty, Ty); 14319 14320 if (Subtarget->isTargetWindows()) 14321 InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain); 14322 14323 TargetLowering::CallLoweringInfo CLI(DAG); 14324 CLI.setDebugLoc(dl).setChain(InChain) 14325 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args)) 14326 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned); 14327 14328 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 14329 return CallInfo.first; 14330 } 14331 14332 // Lowers REM using divmod helpers 14333 // see RTABI section 4.2/4.3 14334 SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const { 14335 // Build return types (div and rem) 14336 std::vector<Type*> RetTyParams; 14337 Type *RetTyElement; 14338 14339 switch (N->getValueType(0).getSimpleVT().SimpleTy) { 14340 default: llvm_unreachable("Unexpected request for libcall!"); 14341 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break; 14342 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break; 14343 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break; 14344 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break; 14345 } 14346 14347 RetTyParams.push_back(RetTyElement); 14348 RetTyParams.push_back(RetTyElement); 14349 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams); 14350 Type *RetTy = StructType::get(*DAG.getContext(), ret); 14351 14352 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT(). 14353 SimpleTy); 14354 SDValue InChain = DAG.getEntryNode(); 14355 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext(), 14356 Subtarget); 14357 bool isSigned = N->getOpcode() == ISD::SREM; 14358 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 14359 getPointerTy(DAG.getDataLayout())); 14360 14361 if (Subtarget->isTargetWindows()) 14362 InChain = WinDBZCheckDenominator(DAG, N, InChain); 14363 14364 // Lower call 14365 CallLoweringInfo CLI(DAG); 14366 CLI.setChain(InChain) 14367 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args)) 14368 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N)); 14369 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 14370 14371 // Return second (rem) result operand (first contains div) 14372 SDNode *ResNode = CallResult.first.getNode(); 14373 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands"); 14374 return ResNode->getOperand(1); 14375 } 14376 14377 SDValue 14378 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 14379 assert(Subtarget->isTargetWindows() && "unsupported target platform"); 14380 SDLoc DL(Op); 14381 14382 // Get the inputs. 14383 SDValue Chain = Op.getOperand(0); 14384 SDValue Size = Op.getOperand(1); 14385 14386 if (DAG.getMachineFunction().getFunction().hasFnAttribute( 14387 "no-stack-arg-probe")) { 14388 unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 14389 SDValue SP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 14390 Chain = SP.getValue(1); 14391 SP = DAG.getNode(ISD::SUB, DL, MVT::i32, SP, Size); 14392 if (Align) 14393 SP = DAG.getNode(ISD::AND, DL, MVT::i32, SP.getValue(0), 14394 DAG.getConstant(-(uint64_t)Align, DL, MVT::i32)); 14395 Chain = DAG.getCopyToReg(Chain, DL, ARM::SP, SP); 14396 SDValue Ops[2] = { SP, Chain }; 14397 return DAG.getMergeValues(Ops, DL); 14398 } 14399 14400 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size, 14401 DAG.getConstant(2, DL, MVT::i32)); 14402 14403 SDValue Flag; 14404 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag); 14405 Flag = Chain.getValue(1); 14406 14407 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 14408 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag); 14409 14410 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 14411 Chain = NewSP.getValue(1); 14412 14413 SDValue Ops[2] = { NewSP, Chain }; 14414 return DAG.getMergeValues(Ops, DL); 14415 } 14416 14417 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 14418 assert(Op.getValueType() == MVT::f64 && !Subtarget->hasFP64() && 14419 "Unexpected type for custom-lowering FP_EXTEND"); 14420 14421 RTLIB::Libcall LC; 14422 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 14423 14424 SDValue SrcVal = Op.getOperand(0); 14425 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 14426 SDLoc(Op)).first; 14427 } 14428 14429 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 14430 assert(Op.getOperand(0).getValueType() == MVT::f64 && !Subtarget->hasFP64() && 14431 "Unexpected type for custom-lowering FP_ROUND"); 14432 14433 RTLIB::Libcall LC; 14434 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 14435 14436 SDValue SrcVal = Op.getOperand(0); 14437 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 14438 SDLoc(Op)).first; 14439 } 14440 14441 void ARMTargetLowering::lowerABS(SDNode *N, SmallVectorImpl<SDValue> &Results, 14442 SelectionDAG &DAG) const { 14443 assert(N->getValueType(0) == MVT::i64 && "Unexpected type (!= i64) on ABS."); 14444 MVT HalfT = MVT::i32; 14445 SDLoc dl(N); 14446 SDValue Hi, Lo, Tmp; 14447 14448 if (!isOperationLegalOrCustom(ISD::ADDCARRY, HalfT) || 14449 !isOperationLegalOrCustom(ISD::UADDO, HalfT)) 14450 return ; 14451 14452 unsigned OpTypeBits = HalfT.getScalarSizeInBits(); 14453 SDVTList VTList = DAG.getVTList(HalfT, MVT::i1); 14454 14455 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(0), 14456 DAG.getConstant(0, dl, HalfT)); 14457 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(0), 14458 DAG.getConstant(1, dl, HalfT)); 14459 14460 Tmp = DAG.getNode(ISD::SRA, dl, HalfT, Hi, 14461 DAG.getConstant(OpTypeBits - 1, dl, 14462 getShiftAmountTy(HalfT, DAG.getDataLayout()))); 14463 Lo = DAG.getNode(ISD::UADDO, dl, VTList, Tmp, Lo); 14464 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, Tmp, Hi, 14465 SDValue(Lo.getNode(), 1)); 14466 Hi = DAG.getNode(ISD::XOR, dl, HalfT, Tmp, Hi); 14467 Lo = DAG.getNode(ISD::XOR, dl, HalfT, Tmp, Lo); 14468 14469 Results.push_back(Lo); 14470 Results.push_back(Hi); 14471 } 14472 14473 bool 14474 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 14475 // The ARM target isn't yet aware of offsets. 14476 return false; 14477 } 14478 14479 bool ARM::isBitFieldInvertedMask(unsigned v) { 14480 if (v == 0xffffffff) 14481 return false; 14482 14483 // there can be 1's on either or both "outsides", all the "inside" 14484 // bits must be 0's 14485 return isShiftedMask_32(~v); 14486 } 14487 14488 /// isFPImmLegal - Returns true if the target can instruction select the 14489 /// specified FP immediate natively. If false, the legalizer will 14490 /// materialize the FP immediate as a load from a constant pool. 14491 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, 14492 bool ForCodeSize) const { 14493 if (!Subtarget->hasVFP3Base()) 14494 return false; 14495 if (VT == MVT::f16 && Subtarget->hasFullFP16()) 14496 return ARM_AM::getFP16Imm(Imm) != -1; 14497 if (VT == MVT::f32) 14498 return ARM_AM::getFP32Imm(Imm) != -1; 14499 if (VT == MVT::f64 && Subtarget->hasFP64()) 14500 return ARM_AM::getFP64Imm(Imm) != -1; 14501 return false; 14502 } 14503 14504 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 14505 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 14506 /// specified in the intrinsic calls. 14507 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 14508 const CallInst &I, 14509 MachineFunction &MF, 14510 unsigned Intrinsic) const { 14511 switch (Intrinsic) { 14512 case Intrinsic::arm_neon_vld1: 14513 case Intrinsic::arm_neon_vld2: 14514 case Intrinsic::arm_neon_vld3: 14515 case Intrinsic::arm_neon_vld4: 14516 case Intrinsic::arm_neon_vld2lane: 14517 case Intrinsic::arm_neon_vld3lane: 14518 case Intrinsic::arm_neon_vld4lane: 14519 case Intrinsic::arm_neon_vld2dup: 14520 case Intrinsic::arm_neon_vld3dup: 14521 case Intrinsic::arm_neon_vld4dup: { 14522 Info.opc = ISD::INTRINSIC_W_CHAIN; 14523 // Conservatively set memVT to the entire set of vectors loaded. 14524 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 14525 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64; 14526 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 14527 Info.ptrVal = I.getArgOperand(0); 14528 Info.offset = 0; 14529 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 14530 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 14531 // volatile loads with NEON intrinsics not supported 14532 Info.flags = MachineMemOperand::MOLoad; 14533 return true; 14534 } 14535 case Intrinsic::arm_neon_vld1x2: 14536 case Intrinsic::arm_neon_vld1x3: 14537 case Intrinsic::arm_neon_vld1x4: { 14538 Info.opc = ISD::INTRINSIC_W_CHAIN; 14539 // Conservatively set memVT to the entire set of vectors loaded. 14540 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 14541 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64; 14542 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 14543 Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1); 14544 Info.offset = 0; 14545 Info.align = 0; 14546 // volatile loads with NEON intrinsics not supported 14547 Info.flags = MachineMemOperand::MOLoad; 14548 return true; 14549 } 14550 case Intrinsic::arm_neon_vst1: 14551 case Intrinsic::arm_neon_vst2: 14552 case Intrinsic::arm_neon_vst3: 14553 case Intrinsic::arm_neon_vst4: 14554 case Intrinsic::arm_neon_vst2lane: 14555 case Intrinsic::arm_neon_vst3lane: 14556 case Intrinsic::arm_neon_vst4lane: { 14557 Info.opc = ISD::INTRINSIC_VOID; 14558 // Conservatively set memVT to the entire set of vectors stored. 14559 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 14560 unsigned NumElts = 0; 14561 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 14562 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 14563 if (!ArgTy->isVectorTy()) 14564 break; 14565 NumElts += DL.getTypeSizeInBits(ArgTy) / 64; 14566 } 14567 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 14568 Info.ptrVal = I.getArgOperand(0); 14569 Info.offset = 0; 14570 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 14571 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 14572 // volatile stores with NEON intrinsics not supported 14573 Info.flags = MachineMemOperand::MOStore; 14574 return true; 14575 } 14576 case Intrinsic::arm_neon_vst1x2: 14577 case Intrinsic::arm_neon_vst1x3: 14578 case Intrinsic::arm_neon_vst1x4: { 14579 Info.opc = ISD::INTRINSIC_VOID; 14580 // Conservatively set memVT to the entire set of vectors stored. 14581 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 14582 unsigned NumElts = 0; 14583 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 14584 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 14585 if (!ArgTy->isVectorTy()) 14586 break; 14587 NumElts += DL.getTypeSizeInBits(ArgTy) / 64; 14588 } 14589 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 14590 Info.ptrVal = I.getArgOperand(0); 14591 Info.offset = 0; 14592 Info.align = 0; 14593 // volatile stores with NEON intrinsics not supported 14594 Info.flags = MachineMemOperand::MOStore; 14595 return true; 14596 } 14597 case Intrinsic::arm_ldaex: 14598 case Intrinsic::arm_ldrex: { 14599 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 14600 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 14601 Info.opc = ISD::INTRINSIC_W_CHAIN; 14602 Info.memVT = MVT::getVT(PtrTy->getElementType()); 14603 Info.ptrVal = I.getArgOperand(0); 14604 Info.offset = 0; 14605 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 14606 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 14607 return true; 14608 } 14609 case Intrinsic::arm_stlex: 14610 case Intrinsic::arm_strex: { 14611 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 14612 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 14613 Info.opc = ISD::INTRINSIC_W_CHAIN; 14614 Info.memVT = MVT::getVT(PtrTy->getElementType()); 14615 Info.ptrVal = I.getArgOperand(1); 14616 Info.offset = 0; 14617 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 14618 Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile; 14619 return true; 14620 } 14621 case Intrinsic::arm_stlexd: 14622 case Intrinsic::arm_strexd: 14623 Info.opc = ISD::INTRINSIC_W_CHAIN; 14624 Info.memVT = MVT::i64; 14625 Info.ptrVal = I.getArgOperand(2); 14626 Info.offset = 0; 14627 Info.align = 8; 14628 Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile; 14629 return true; 14630 14631 case Intrinsic::arm_ldaexd: 14632 case Intrinsic::arm_ldrexd: 14633 Info.opc = ISD::INTRINSIC_W_CHAIN; 14634 Info.memVT = MVT::i64; 14635 Info.ptrVal = I.getArgOperand(0); 14636 Info.offset = 0; 14637 Info.align = 8; 14638 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 14639 return true; 14640 14641 default: 14642 break; 14643 } 14644 14645 return false; 14646 } 14647 14648 /// Returns true if it is beneficial to convert a load of a constant 14649 /// to just the constant itself. 14650 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 14651 Type *Ty) const { 14652 assert(Ty->isIntegerTy()); 14653 14654 unsigned Bits = Ty->getPrimitiveSizeInBits(); 14655 if (Bits == 0 || Bits > 32) 14656 return false; 14657 return true; 14658 } 14659 14660 bool ARMTargetLowering::isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, 14661 unsigned Index) const { 14662 if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT)) 14663 return false; 14664 14665 return (Index == 0 || Index == ResVT.getVectorNumElements()); 14666 } 14667 14668 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder, 14669 ARM_MB::MemBOpt Domain) const { 14670 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14671 14672 // First, if the target has no DMB, see what fallback we can use. 14673 if (!Subtarget->hasDataBarrier()) { 14674 // Some ARMv6 cpus can support data barriers with an mcr instruction. 14675 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 14676 // here. 14677 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) { 14678 Function *MCR = Intrinsic::getDeclaration(M, Intrinsic::arm_mcr); 14679 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0), 14680 Builder.getInt32(0), Builder.getInt32(7), 14681 Builder.getInt32(10), Builder.getInt32(5)}; 14682 return Builder.CreateCall(MCR, args); 14683 } else { 14684 // Instead of using barriers, atomic accesses on these subtargets use 14685 // libcalls. 14686 llvm_unreachable("makeDMB on a target so old that it has no barriers"); 14687 } 14688 } else { 14689 Function *DMB = Intrinsic::getDeclaration(M, Intrinsic::arm_dmb); 14690 // Only a full system barrier exists in the M-class architectures. 14691 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain; 14692 Constant *CDomain = Builder.getInt32(Domain); 14693 return Builder.CreateCall(DMB, CDomain); 14694 } 14695 } 14696 14697 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 14698 Instruction *ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 14699 Instruction *Inst, 14700 AtomicOrdering Ord) const { 14701 switch (Ord) { 14702 case AtomicOrdering::NotAtomic: 14703 case AtomicOrdering::Unordered: 14704 llvm_unreachable("Invalid fence: unordered/non-atomic"); 14705 case AtomicOrdering::Monotonic: 14706 case AtomicOrdering::Acquire: 14707 return nullptr; // Nothing to do 14708 case AtomicOrdering::SequentiallyConsistent: 14709 if (!Inst->hasAtomicStore()) 14710 return nullptr; // Nothing to do 14711 LLVM_FALLTHROUGH; 14712 case AtomicOrdering::Release: 14713 case AtomicOrdering::AcquireRelease: 14714 if (Subtarget->preferISHSTBarriers()) 14715 return makeDMB(Builder, ARM_MB::ISHST); 14716 // FIXME: add a comment with a link to documentation justifying this. 14717 else 14718 return makeDMB(Builder, ARM_MB::ISH); 14719 } 14720 llvm_unreachable("Unknown fence ordering in emitLeadingFence"); 14721 } 14722 14723 Instruction *ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 14724 Instruction *Inst, 14725 AtomicOrdering Ord) const { 14726 switch (Ord) { 14727 case AtomicOrdering::NotAtomic: 14728 case AtomicOrdering::Unordered: 14729 llvm_unreachable("Invalid fence: unordered/not-atomic"); 14730 case AtomicOrdering::Monotonic: 14731 case AtomicOrdering::Release: 14732 return nullptr; // Nothing to do 14733 case AtomicOrdering::Acquire: 14734 case AtomicOrdering::AcquireRelease: 14735 case AtomicOrdering::SequentiallyConsistent: 14736 return makeDMB(Builder, ARM_MB::ISH); 14737 } 14738 llvm_unreachable("Unknown fence ordering in emitTrailingFence"); 14739 } 14740 14741 // Loads and stores less than 64-bits are already atomic; ones above that 14742 // are doomed anyway, so defer to the default libcall and blame the OS when 14743 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 14744 // anything for those. 14745 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 14746 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 14747 return (Size == 64) && !Subtarget->isMClass(); 14748 } 14749 14750 // Loads and stores less than 64-bits are already atomic; ones above that 14751 // are doomed anyway, so defer to the default libcall and blame the OS when 14752 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 14753 // anything for those. 14754 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that 14755 // guarantee, see DDI0406C ARM architecture reference manual, 14756 // sections A8.8.72-74 LDRD) 14757 TargetLowering::AtomicExpansionKind 14758 ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 14759 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 14760 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly 14761 : AtomicExpansionKind::None; 14762 } 14763 14764 // For the real atomic operations, we have ldrex/strex up to 32 bits, 14765 // and up to 64 bits on the non-M profiles 14766 TargetLowering::AtomicExpansionKind 14767 ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 14768 if (AI->isFloatingPointOperation()) 14769 return AtomicExpansionKind::CmpXChg; 14770 14771 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 14772 bool hasAtomicRMW = !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps(); 14773 return (Size <= (Subtarget->isMClass() ? 32U : 64U) && hasAtomicRMW) 14774 ? AtomicExpansionKind::LLSC 14775 : AtomicExpansionKind::None; 14776 } 14777 14778 TargetLowering::AtomicExpansionKind 14779 ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const { 14780 // At -O0, fast-regalloc cannot cope with the live vregs necessary to 14781 // implement cmpxchg without spilling. If the address being exchanged is also 14782 // on the stack and close enough to the spill slot, this can lead to a 14783 // situation where the monitor always gets cleared and the atomic operation 14784 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead. 14785 bool HasAtomicCmpXchg = 14786 !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps(); 14787 if (getTargetMachine().getOptLevel() != 0 && HasAtomicCmpXchg) 14788 return AtomicExpansionKind::LLSC; 14789 return AtomicExpansionKind::None; 14790 } 14791 14792 bool ARMTargetLowering::shouldInsertFencesForAtomic( 14793 const Instruction *I) const { 14794 return InsertFencesForAtomic; 14795 } 14796 14797 // This has so far only been implemented for MachO. 14798 bool ARMTargetLowering::useLoadStackGuardNode() const { 14799 return Subtarget->isTargetMachO(); 14800 } 14801 14802 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx, 14803 unsigned &Cost) const { 14804 // If we do not have NEON, vector types are not natively supported. 14805 if (!Subtarget->hasNEON()) 14806 return false; 14807 14808 // Floating point values and vector values map to the same register file. 14809 // Therefore, although we could do a store extract of a vector type, this is 14810 // better to leave at float as we have more freedom in the addressing mode for 14811 // those. 14812 if (VectorTy->isFPOrFPVectorTy()) 14813 return false; 14814 14815 // If the index is unknown at compile time, this is very expensive to lower 14816 // and it is not possible to combine the store with the extract. 14817 if (!isa<ConstantInt>(Idx)) 14818 return false; 14819 14820 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type"); 14821 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth(); 14822 // We can do a store + vector extract on any vector that fits perfectly in a D 14823 // or Q register. 14824 if (BitWidth == 64 || BitWidth == 128) { 14825 Cost = 0; 14826 return true; 14827 } 14828 return false; 14829 } 14830 14831 bool ARMTargetLowering::isCheapToSpeculateCttz() const { 14832 return Subtarget->hasV6T2Ops(); 14833 } 14834 14835 bool ARMTargetLowering::isCheapToSpeculateCtlz() const { 14836 return Subtarget->hasV6T2Ops(); 14837 } 14838 14839 bool ARMTargetLowering::shouldExpandShift(SelectionDAG &DAG, SDNode *N) const { 14840 return !Subtarget->hasMinSize(); 14841 } 14842 14843 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 14844 AtomicOrdering Ord) const { 14845 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14846 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 14847 bool IsAcquire = isAcquireOrStronger(Ord); 14848 14849 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd 14850 // intrinsic must return {i32, i32} and we have to recombine them into a 14851 // single i64 here. 14852 if (ValTy->getPrimitiveSizeInBits() == 64) { 14853 Intrinsic::ID Int = 14854 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd; 14855 Function *Ldrex = Intrinsic::getDeclaration(M, Int); 14856 14857 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 14858 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi"); 14859 14860 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 14861 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 14862 if (!Subtarget->isLittle()) 14863 std::swap (Lo, Hi); 14864 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 14865 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 14866 return Builder.CreateOr( 14867 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64"); 14868 } 14869 14870 Type *Tys[] = { Addr->getType() }; 14871 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex; 14872 Function *Ldrex = Intrinsic::getDeclaration(M, Int, Tys); 14873 14874 return Builder.CreateTruncOrBitCast( 14875 Builder.CreateCall(Ldrex, Addr), 14876 cast<PointerType>(Addr->getType())->getElementType()); 14877 } 14878 14879 void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance( 14880 IRBuilder<> &Builder) const { 14881 if (!Subtarget->hasV7Ops()) 14882 return; 14883 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14884 Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::arm_clrex)); 14885 } 14886 14887 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val, 14888 Value *Addr, 14889 AtomicOrdering Ord) const { 14890 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14891 bool IsRelease = isReleaseOrStronger(Ord); 14892 14893 // Since the intrinsics must have legal type, the i64 intrinsics take two 14894 // parameters: "i32, i32". We must marshal Val into the appropriate form 14895 // before the call. 14896 if (Val->getType()->getPrimitiveSizeInBits() == 64) { 14897 Intrinsic::ID Int = 14898 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd; 14899 Function *Strex = Intrinsic::getDeclaration(M, Int); 14900 Type *Int32Ty = Type::getInt32Ty(M->getContext()); 14901 14902 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo"); 14903 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi"); 14904 if (!Subtarget->isLittle()) 14905 std::swap(Lo, Hi); 14906 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 14907 return Builder.CreateCall(Strex, {Lo, Hi, Addr}); 14908 } 14909 14910 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex; 14911 Type *Tys[] = { Addr->getType() }; 14912 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys); 14913 14914 return Builder.CreateCall( 14915 Strex, {Builder.CreateZExtOrBitCast( 14916 Val, Strex->getFunctionType()->getParamType(0)), 14917 Addr}); 14918 } 14919 14920 14921 bool ARMTargetLowering::alignLoopsWithOptSize() const { 14922 return Subtarget->isMClass(); 14923 } 14924 14925 /// A helper function for determining the number of interleaved accesses we 14926 /// will generate when lowering accesses of the given type. 14927 unsigned 14928 ARMTargetLowering::getNumInterleavedAccesses(VectorType *VecTy, 14929 const DataLayout &DL) const { 14930 return (DL.getTypeSizeInBits(VecTy) + 127) / 128; 14931 } 14932 14933 bool ARMTargetLowering::isLegalInterleavedAccessType( 14934 VectorType *VecTy, const DataLayout &DL) const { 14935 14936 unsigned VecSize = DL.getTypeSizeInBits(VecTy); 14937 unsigned ElSize = DL.getTypeSizeInBits(VecTy->getElementType()); 14938 14939 // Ensure the vector doesn't have f16 elements. Even though we could do an 14940 // i16 vldN, we can't hold the f16 vectors and will end up converting via 14941 // f32. 14942 if (VecTy->getElementType()->isHalfTy()) 14943 return false; 14944 14945 // Ensure the number of vector elements is greater than 1. 14946 if (VecTy->getNumElements() < 2) 14947 return false; 14948 14949 // Ensure the element type is legal. 14950 if (ElSize != 8 && ElSize != 16 && ElSize != 32) 14951 return false; 14952 14953 // Ensure the total vector size is 64 or a multiple of 128. Types larger than 14954 // 128 will be split into multiple interleaved accesses. 14955 return VecSize == 64 || VecSize % 128 == 0; 14956 } 14957 14958 /// Lower an interleaved load into a vldN intrinsic. 14959 /// 14960 /// E.g. Lower an interleaved load (Factor = 2): 14961 /// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4 14962 /// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements 14963 /// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements 14964 /// 14965 /// Into: 14966 /// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4) 14967 /// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0 14968 /// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1 14969 bool ARMTargetLowering::lowerInterleavedLoad( 14970 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles, 14971 ArrayRef<unsigned> Indices, unsigned Factor) const { 14972 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 14973 "Invalid interleave factor"); 14974 assert(!Shuffles.empty() && "Empty shufflevector input"); 14975 assert(Shuffles.size() == Indices.size() && 14976 "Unmatched number of shufflevectors and indices"); 14977 14978 VectorType *VecTy = Shuffles[0]->getType(); 14979 Type *EltTy = VecTy->getVectorElementType(); 14980 14981 const DataLayout &DL = LI->getModule()->getDataLayout(); 14982 14983 // Skip if we do not have NEON and skip illegal vector types. We can 14984 // "legalize" wide vector types into multiple interleaved accesses as long as 14985 // the vector types are divisible by 128. 14986 if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(VecTy, DL)) 14987 return false; 14988 14989 unsigned NumLoads = getNumInterleavedAccesses(VecTy, DL); 14990 14991 // A pointer vector can not be the return type of the ldN intrinsics. Need to 14992 // load integer vectors first and then convert to pointer vectors. 14993 if (EltTy->isPointerTy()) 14994 VecTy = 14995 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements()); 14996 14997 IRBuilder<> Builder(LI); 14998 14999 // The base address of the load. 15000 Value *BaseAddr = LI->getPointerOperand(); 15001 15002 if (NumLoads > 1) { 15003 // If we're going to generate more than one load, reset the sub-vector type 15004 // to something legal. 15005 VecTy = VectorType::get(VecTy->getVectorElementType(), 15006 VecTy->getVectorNumElements() / NumLoads); 15007 15008 // We will compute the pointer operand of each load from the original base 15009 // address using GEPs. Cast the base address to a pointer to the scalar 15010 // element type. 15011 BaseAddr = Builder.CreateBitCast( 15012 BaseAddr, VecTy->getVectorElementType()->getPointerTo( 15013 LI->getPointerAddressSpace())); 15014 } 15015 15016 assert(isTypeLegal(EVT::getEVT(VecTy)) && "Illegal vldN vector type!"); 15017 15018 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace()); 15019 Type *Tys[] = {VecTy, Int8Ptr}; 15020 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2, 15021 Intrinsic::arm_neon_vld3, 15022 Intrinsic::arm_neon_vld4}; 15023 Function *VldnFunc = 15024 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys); 15025 15026 // Holds sub-vectors extracted from the load intrinsic return values. The 15027 // sub-vectors are associated with the shufflevector instructions they will 15028 // replace. 15029 DenseMap<ShuffleVectorInst *, SmallVector<Value *, 4>> SubVecs; 15030 15031 for (unsigned LoadCount = 0; LoadCount < NumLoads; ++LoadCount) { 15032 // If we're generating more than one load, compute the base address of 15033 // subsequent loads as an offset from the previous. 15034 if (LoadCount > 0) 15035 BaseAddr = 15036 Builder.CreateConstGEP1_32(VecTy->getVectorElementType(), BaseAddr, 15037 VecTy->getVectorNumElements() * Factor); 15038 15039 SmallVector<Value *, 2> Ops; 15040 Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr)); 15041 Ops.push_back(Builder.getInt32(LI->getAlignment())); 15042 15043 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN"); 15044 15045 // Replace uses of each shufflevector with the corresponding vector loaded 15046 // by ldN. 15047 for (unsigned i = 0; i < Shuffles.size(); i++) { 15048 ShuffleVectorInst *SV = Shuffles[i]; 15049 unsigned Index = Indices[i]; 15050 15051 Value *SubVec = Builder.CreateExtractValue(VldN, Index); 15052 15053 // Convert the integer vector to pointer vector if the element is pointer. 15054 if (EltTy->isPointerTy()) 15055 SubVec = Builder.CreateIntToPtr( 15056 SubVec, VectorType::get(SV->getType()->getVectorElementType(), 15057 VecTy->getVectorNumElements())); 15058 15059 SubVecs[SV].push_back(SubVec); 15060 } 15061 } 15062 15063 // Replace uses of the shufflevector instructions with the sub-vectors 15064 // returned by the load intrinsic. If a shufflevector instruction is 15065 // associated with more than one sub-vector, those sub-vectors will be 15066 // concatenated into a single wide vector. 15067 for (ShuffleVectorInst *SVI : Shuffles) { 15068 auto &SubVec = SubVecs[SVI]; 15069 auto *WideVec = 15070 SubVec.size() > 1 ? concatenateVectors(Builder, SubVec) : SubVec[0]; 15071 SVI->replaceAllUsesWith(WideVec); 15072 } 15073 15074 return true; 15075 } 15076 15077 /// Lower an interleaved store into a vstN intrinsic. 15078 /// 15079 /// E.g. Lower an interleaved store (Factor = 3): 15080 /// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, 15081 /// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> 15082 /// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4 15083 /// 15084 /// Into: 15085 /// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3> 15086 /// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7> 15087 /// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11> 15088 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 15089 /// 15090 /// Note that the new shufflevectors will be removed and we'll only generate one 15091 /// vst3 instruction in CodeGen. 15092 /// 15093 /// Example for a more general valid mask (Factor 3). Lower: 15094 /// %i.vec = shuffle <32 x i32> %v0, <32 x i32> %v1, 15095 /// <4, 32, 16, 5, 33, 17, 6, 34, 18, 7, 35, 19> 15096 /// store <12 x i32> %i.vec, <12 x i32>* %ptr 15097 /// 15098 /// Into: 15099 /// %sub.v0 = shuffle <32 x i32> %v0, <32 x i32> v1, <4, 5, 6, 7> 15100 /// %sub.v1 = shuffle <32 x i32> %v0, <32 x i32> v1, <32, 33, 34, 35> 15101 /// %sub.v2 = shuffle <32 x i32> %v0, <32 x i32> v1, <16, 17, 18, 19> 15102 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 15103 bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI, 15104 ShuffleVectorInst *SVI, 15105 unsigned Factor) const { 15106 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 15107 "Invalid interleave factor"); 15108 15109 VectorType *VecTy = SVI->getType(); 15110 assert(VecTy->getVectorNumElements() % Factor == 0 && 15111 "Invalid interleaved store"); 15112 15113 unsigned LaneLen = VecTy->getVectorNumElements() / Factor; 15114 Type *EltTy = VecTy->getVectorElementType(); 15115 VectorType *SubVecTy = VectorType::get(EltTy, LaneLen); 15116 15117 const DataLayout &DL = SI->getModule()->getDataLayout(); 15118 15119 // Skip if we do not have NEON and skip illegal vector types. We can 15120 // "legalize" wide vector types into multiple interleaved accesses as long as 15121 // the vector types are divisible by 128. 15122 if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(SubVecTy, DL)) 15123 return false; 15124 15125 unsigned NumStores = getNumInterleavedAccesses(SubVecTy, DL); 15126 15127 Value *Op0 = SVI->getOperand(0); 15128 Value *Op1 = SVI->getOperand(1); 15129 IRBuilder<> Builder(SI); 15130 15131 // StN intrinsics don't support pointer vectors as arguments. Convert pointer 15132 // vectors to integer vectors. 15133 if (EltTy->isPointerTy()) { 15134 Type *IntTy = DL.getIntPtrType(EltTy); 15135 15136 // Convert to the corresponding integer vector. 15137 Type *IntVecTy = 15138 VectorType::get(IntTy, Op0->getType()->getVectorNumElements()); 15139 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy); 15140 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy); 15141 15142 SubVecTy = VectorType::get(IntTy, LaneLen); 15143 } 15144 15145 // The base address of the store. 15146 Value *BaseAddr = SI->getPointerOperand(); 15147 15148 if (NumStores > 1) { 15149 // If we're going to generate more than one store, reset the lane length 15150 // and sub-vector type to something legal. 15151 LaneLen /= NumStores; 15152 SubVecTy = VectorType::get(SubVecTy->getVectorElementType(), LaneLen); 15153 15154 // We will compute the pointer operand of each store from the original base 15155 // address using GEPs. Cast the base address to a pointer to the scalar 15156 // element type. 15157 BaseAddr = Builder.CreateBitCast( 15158 BaseAddr, SubVecTy->getVectorElementType()->getPointerTo( 15159 SI->getPointerAddressSpace())); 15160 } 15161 15162 assert(isTypeLegal(EVT::getEVT(SubVecTy)) && "Illegal vstN vector type!"); 15163 15164 auto Mask = SVI->getShuffleMask(); 15165 15166 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace()); 15167 Type *Tys[] = {Int8Ptr, SubVecTy}; 15168 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2, 15169 Intrinsic::arm_neon_vst3, 15170 Intrinsic::arm_neon_vst4}; 15171 15172 for (unsigned StoreCount = 0; StoreCount < NumStores; ++StoreCount) { 15173 // If we generating more than one store, we compute the base address of 15174 // subsequent stores as an offset from the previous. 15175 if (StoreCount > 0) 15176 BaseAddr = Builder.CreateConstGEP1_32(SubVecTy->getVectorElementType(), 15177 BaseAddr, LaneLen * Factor); 15178 15179 SmallVector<Value *, 6> Ops; 15180 Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr)); 15181 15182 Function *VstNFunc = 15183 Intrinsic::getDeclaration(SI->getModule(), StoreInts[Factor - 2], Tys); 15184 15185 // Split the shufflevector operands into sub vectors for the new vstN call. 15186 for (unsigned i = 0; i < Factor; i++) { 15187 unsigned IdxI = StoreCount * LaneLen * Factor + i; 15188 if (Mask[IdxI] >= 0) { 15189 Ops.push_back(Builder.CreateShuffleVector( 15190 Op0, Op1, createSequentialMask(Builder, Mask[IdxI], LaneLen, 0))); 15191 } else { 15192 unsigned StartMask = 0; 15193 for (unsigned j = 1; j < LaneLen; j++) { 15194 unsigned IdxJ = StoreCount * LaneLen * Factor + j; 15195 if (Mask[IdxJ * Factor + IdxI] >= 0) { 15196 StartMask = Mask[IdxJ * Factor + IdxI] - IdxJ; 15197 break; 15198 } 15199 } 15200 // Note: If all elements in a chunk are undefs, StartMask=0! 15201 // Note: Filling undef gaps with random elements is ok, since 15202 // those elements were being written anyway (with undefs). 15203 // In the case of all undefs we're defaulting to using elems from 0 15204 // Note: StartMask cannot be negative, it's checked in 15205 // isReInterleaveMask 15206 Ops.push_back(Builder.CreateShuffleVector( 15207 Op0, Op1, createSequentialMask(Builder, StartMask, LaneLen, 0))); 15208 } 15209 } 15210 15211 Ops.push_back(Builder.getInt32(SI->getAlignment())); 15212 Builder.CreateCall(VstNFunc, Ops); 15213 } 15214 return true; 15215 } 15216 15217 enum HABaseType { 15218 HA_UNKNOWN = 0, 15219 HA_FLOAT, 15220 HA_DOUBLE, 15221 HA_VECT64, 15222 HA_VECT128 15223 }; 15224 15225 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base, 15226 uint64_t &Members) { 15227 if (auto *ST = dyn_cast<StructType>(Ty)) { 15228 for (unsigned i = 0; i < ST->getNumElements(); ++i) { 15229 uint64_t SubMembers = 0; 15230 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers)) 15231 return false; 15232 Members += SubMembers; 15233 } 15234 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) { 15235 uint64_t SubMembers = 0; 15236 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers)) 15237 return false; 15238 Members += SubMembers * AT->getNumElements(); 15239 } else if (Ty->isFloatTy()) { 15240 if (Base != HA_UNKNOWN && Base != HA_FLOAT) 15241 return false; 15242 Members = 1; 15243 Base = HA_FLOAT; 15244 } else if (Ty->isDoubleTy()) { 15245 if (Base != HA_UNKNOWN && Base != HA_DOUBLE) 15246 return false; 15247 Members = 1; 15248 Base = HA_DOUBLE; 15249 } else if (auto *VT = dyn_cast<VectorType>(Ty)) { 15250 Members = 1; 15251 switch (Base) { 15252 case HA_FLOAT: 15253 case HA_DOUBLE: 15254 return false; 15255 case HA_VECT64: 15256 return VT->getBitWidth() == 64; 15257 case HA_VECT128: 15258 return VT->getBitWidth() == 128; 15259 case HA_UNKNOWN: 15260 switch (VT->getBitWidth()) { 15261 case 64: 15262 Base = HA_VECT64; 15263 return true; 15264 case 128: 15265 Base = HA_VECT128; 15266 return true; 15267 default: 15268 return false; 15269 } 15270 } 15271 } 15272 15273 return (Members > 0 && Members <= 4); 15274 } 15275 15276 /// Return the correct alignment for the current calling convention. 15277 unsigned 15278 ARMTargetLowering::getABIAlignmentForCallingConv(Type *ArgTy, 15279 DataLayout DL) const { 15280 if (!ArgTy->isVectorTy()) 15281 return DL.getABITypeAlignment(ArgTy); 15282 15283 // Avoid over-aligning vector parameters. It would require realigning the 15284 // stack and waste space for no real benefit. 15285 return std::min(DL.getABITypeAlignment(ArgTy), DL.getStackAlignment()); 15286 } 15287 15288 /// Return true if a type is an AAPCS-VFP homogeneous aggregate or one of 15289 /// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when 15290 /// passing according to AAPCS rules. 15291 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters( 15292 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 15293 if (getEffectiveCallingConv(CallConv, isVarArg) != 15294 CallingConv::ARM_AAPCS_VFP) 15295 return false; 15296 15297 HABaseType Base = HA_UNKNOWN; 15298 uint64_t Members = 0; 15299 bool IsHA = isHomogeneousAggregate(Ty, Base, Members); 15300 LLVM_DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump()); 15301 15302 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy(); 15303 return IsHA || IsIntArray; 15304 } 15305 15306 unsigned ARMTargetLowering::getExceptionPointerRegister( 15307 const Constant *PersonalityFn) const { 15308 // Platforms which do not use SjLj EH may return values in these registers 15309 // via the personality function. 15310 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0; 15311 } 15312 15313 unsigned ARMTargetLowering::getExceptionSelectorRegister( 15314 const Constant *PersonalityFn) const { 15315 // Platforms which do not use SjLj EH may return values in these registers 15316 // via the personality function. 15317 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1; 15318 } 15319 15320 void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 15321 // Update IsSplitCSR in ARMFunctionInfo. 15322 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>(); 15323 AFI->setIsSplitCSR(true); 15324 } 15325 15326 void ARMTargetLowering::insertCopiesSplitCSR( 15327 MachineBasicBlock *Entry, 15328 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 15329 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 15330 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 15331 if (!IStart) 15332 return; 15333 15334 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 15335 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 15336 MachineBasicBlock::iterator MBBI = Entry->begin(); 15337 for (const MCPhysReg *I = IStart; *I; ++I) { 15338 const TargetRegisterClass *RC = nullptr; 15339 if (ARM::GPRRegClass.contains(*I)) 15340 RC = &ARM::GPRRegClass; 15341 else if (ARM::DPRRegClass.contains(*I)) 15342 RC = &ARM::DPRRegClass; 15343 else 15344 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 15345 15346 unsigned NewVR = MRI->createVirtualRegister(RC); 15347 // Create copy from CSR to a virtual register. 15348 // FIXME: this currently does not emit CFI pseudo-instructions, it works 15349 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be 15350 // nounwind. If we want to generalize this later, we may need to emit 15351 // CFI pseudo-instructions. 15352 assert(Entry->getParent()->getFunction().hasFnAttribute( 15353 Attribute::NoUnwind) && 15354 "Function should be nounwind in insertCopiesSplitCSR!"); 15355 Entry->addLiveIn(*I); 15356 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 15357 .addReg(*I); 15358 15359 // Insert the copy-back instructions right before the terminator. 15360 for (auto *Exit : Exits) 15361 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 15362 TII->get(TargetOpcode::COPY), *I) 15363 .addReg(NewVR); 15364 } 15365 } 15366 15367 void ARMTargetLowering::finalizeLowering(MachineFunction &MF) const { 15368 MF.getFrameInfo().computeMaxCallFrameSize(MF); 15369 TargetLoweringBase::finalizeLowering(MF); 15370 } 15371