1 //===- ARMISelLowering.cpp - ARM DAG Lowering Implementation --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the interfaces that ARM uses to lower LLVM code into a 11 // selection DAG. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ARMISelLowering.h" 16 #include "ARMBaseInstrInfo.h" 17 #include "ARMBaseRegisterInfo.h" 18 #include "ARMCallingConv.h" 19 #include "ARMConstantPoolValue.h" 20 #include "ARMMachineFunctionInfo.h" 21 #include "ARMPerfectShuffle.h" 22 #include "ARMRegisterInfo.h" 23 #include "ARMSelectionDAGInfo.h" 24 #include "ARMSubtarget.h" 25 #include "MCTargetDesc/ARMAddressingModes.h" 26 #include "MCTargetDesc/ARMBaseInfo.h" 27 #include "Utils/ARMBaseInfo.h" 28 #include "llvm/ADT/APFloat.h" 29 #include "llvm/ADT/APInt.h" 30 #include "llvm/ADT/ArrayRef.h" 31 #include "llvm/ADT/BitVector.h" 32 #include "llvm/ADT/DenseMap.h" 33 #include "llvm/ADT/STLExtras.h" 34 #include "llvm/ADT/SmallPtrSet.h" 35 #include "llvm/ADT/SmallVector.h" 36 #include "llvm/ADT/Statistic.h" 37 #include "llvm/ADT/StringExtras.h" 38 #include "llvm/ADT/StringRef.h" 39 #include "llvm/ADT/StringSwitch.h" 40 #include "llvm/ADT/Triple.h" 41 #include "llvm/ADT/Twine.h" 42 #include "llvm/Analysis/VectorUtils.h" 43 #include "llvm/CodeGen/CallingConvLower.h" 44 #include "llvm/CodeGen/ISDOpcodes.h" 45 #include "llvm/CodeGen/IntrinsicLowering.h" 46 #include "llvm/CodeGen/MachineBasicBlock.h" 47 #include "llvm/CodeGen/MachineConstantPool.h" 48 #include "llvm/CodeGen/MachineFrameInfo.h" 49 #include "llvm/CodeGen/MachineFunction.h" 50 #include "llvm/CodeGen/MachineInstr.h" 51 #include "llvm/CodeGen/MachineInstrBuilder.h" 52 #include "llvm/CodeGen/MachineJumpTableInfo.h" 53 #include "llvm/CodeGen/MachineMemOperand.h" 54 #include "llvm/CodeGen/MachineOperand.h" 55 #include "llvm/CodeGen/MachineRegisterInfo.h" 56 #include "llvm/CodeGen/MachineValueType.h" 57 #include "llvm/CodeGen/RuntimeLibcalls.h" 58 #include "llvm/CodeGen/SelectionDAG.h" 59 #include "llvm/CodeGen/SelectionDAGNodes.h" 60 #include "llvm/CodeGen/TargetInstrInfo.h" 61 #include "llvm/CodeGen/TargetLowering.h" 62 #include "llvm/CodeGen/TargetOpcodes.h" 63 #include "llvm/CodeGen/TargetRegisterInfo.h" 64 #include "llvm/CodeGen/TargetSubtargetInfo.h" 65 #include "llvm/CodeGen/ValueTypes.h" 66 #include "llvm/IR/Attributes.h" 67 #include "llvm/IR/CallingConv.h" 68 #include "llvm/IR/Constant.h" 69 #include "llvm/IR/Constants.h" 70 #include "llvm/IR/DataLayout.h" 71 #include "llvm/IR/DebugLoc.h" 72 #include "llvm/IR/DerivedTypes.h" 73 #include "llvm/IR/Function.h" 74 #include "llvm/IR/GlobalAlias.h" 75 #include "llvm/IR/GlobalValue.h" 76 #include "llvm/IR/GlobalVariable.h" 77 #include "llvm/IR/IRBuilder.h" 78 #include "llvm/IR/InlineAsm.h" 79 #include "llvm/IR/Instruction.h" 80 #include "llvm/IR/Instructions.h" 81 #include "llvm/IR/IntrinsicInst.h" 82 #include "llvm/IR/Intrinsics.h" 83 #include "llvm/IR/Module.h" 84 #include "llvm/IR/Type.h" 85 #include "llvm/IR/User.h" 86 #include "llvm/IR/Value.h" 87 #include "llvm/MC/MCInstrDesc.h" 88 #include "llvm/MC/MCInstrItineraries.h" 89 #include "llvm/MC/MCRegisterInfo.h" 90 #include "llvm/MC/MCSchedule.h" 91 #include "llvm/Support/AtomicOrdering.h" 92 #include "llvm/Support/BranchProbability.h" 93 #include "llvm/Support/Casting.h" 94 #include "llvm/Support/CodeGen.h" 95 #include "llvm/Support/CommandLine.h" 96 #include "llvm/Support/Compiler.h" 97 #include "llvm/Support/Debug.h" 98 #include "llvm/Support/ErrorHandling.h" 99 #include "llvm/Support/KnownBits.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 117 #define DEBUG_TYPE "arm-isel" 118 119 STATISTIC(NumTailCalls, "Number of tail calls"); 120 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 121 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); 122 STATISTIC(NumConstpoolPromoted, 123 "Number of constants with their storage promoted into constant pools"); 124 125 static cl::opt<bool> 126 ARMInterworking("arm-interworking", cl::Hidden, 127 cl::desc("Enable / disable ARM interworking (for debugging only)"), 128 cl::init(true)); 129 130 static cl::opt<bool> EnableConstpoolPromotion( 131 "arm-promote-constant", cl::Hidden, 132 cl::desc("Enable / disable promotion of unnamed_addr constants into " 133 "constant pools"), 134 cl::init(false)); // FIXME: set to true by default once PR32780 is fixed 135 static cl::opt<unsigned> ConstpoolPromotionMaxSize( 136 "arm-promote-constant-max-size", cl::Hidden, 137 cl::desc("Maximum size of constant to promote into a constant pool"), 138 cl::init(64)); 139 static cl::opt<unsigned> ConstpoolPromotionMaxTotal( 140 "arm-promote-constant-max-total", cl::Hidden, 141 cl::desc("Maximum size of ALL constants to promote into a constant pool"), 142 cl::init(128)); 143 144 // The APCS parameter registers. 145 static const MCPhysReg GPRArgRegs[] = { 146 ARM::R0, ARM::R1, ARM::R2, ARM::R3 147 }; 148 149 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 150 MVT PromotedBitwiseVT) { 151 if (VT != PromotedLdStVT) { 152 setOperationAction(ISD::LOAD, VT, Promote); 153 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 154 155 setOperationAction(ISD::STORE, VT, Promote); 156 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 157 } 158 159 MVT ElemTy = VT.getVectorElementType(); 160 if (ElemTy != MVT::f64) 161 setOperationAction(ISD::SETCC, VT, Custom); 162 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 163 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 164 if (ElemTy == MVT::i32) { 165 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 166 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 167 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 168 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 169 } else { 170 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 171 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 172 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 173 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 174 } 175 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 176 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 177 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 178 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 179 setOperationAction(ISD::SELECT, VT, Expand); 180 setOperationAction(ISD::SELECT_CC, VT, Expand); 181 setOperationAction(ISD::VSELECT, VT, Expand); 182 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 183 if (VT.isInteger()) { 184 setOperationAction(ISD::SHL, VT, Custom); 185 setOperationAction(ISD::SRA, VT, Custom); 186 setOperationAction(ISD::SRL, VT, Custom); 187 } 188 189 // Promote all bit-wise operations. 190 if (VT.isInteger() && VT != PromotedBitwiseVT) { 191 setOperationAction(ISD::AND, VT, Promote); 192 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 193 setOperationAction(ISD::OR, VT, Promote); 194 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 195 setOperationAction(ISD::XOR, VT, Promote); 196 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 197 } 198 199 // Neon does not support vector divide/remainder operations. 200 setOperationAction(ISD::SDIV, VT, Expand); 201 setOperationAction(ISD::UDIV, VT, Expand); 202 setOperationAction(ISD::FDIV, VT, Expand); 203 setOperationAction(ISD::SREM, VT, Expand); 204 setOperationAction(ISD::UREM, VT, Expand); 205 setOperationAction(ISD::FREM, VT, Expand); 206 207 if (!VT.isFloatingPoint() && 208 VT != MVT::v2i64 && VT != MVT::v1i64) 209 for (auto Opcode : {ISD::ABS, ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX}) 210 setOperationAction(Opcode, VT, Legal); 211 } 212 213 void ARMTargetLowering::addDRTypeForNEON(MVT VT) { 214 addRegisterClass(VT, &ARM::DPRRegClass); 215 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 216 } 217 218 void ARMTargetLowering::addQRTypeForNEON(MVT VT) { 219 addRegisterClass(VT, &ARM::DPairRegClass); 220 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 221 } 222 223 ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM, 224 const ARMSubtarget &STI) 225 : TargetLowering(TM), Subtarget(&STI) { 226 RegInfo = Subtarget->getRegisterInfo(); 227 Itins = Subtarget->getInstrItineraryData(); 228 229 setBooleanContents(ZeroOrOneBooleanContent); 230 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 231 232 if (!Subtarget->isTargetDarwin() && !Subtarget->isTargetIOS() && 233 !Subtarget->isTargetWatchOS()) { 234 bool IsHFTarget = TM.Options.FloatABIType == FloatABI::Hard; 235 for (int LCID = 0; LCID < RTLIB::UNKNOWN_LIBCALL; ++LCID) 236 setLibcallCallingConv(static_cast<RTLIB::Libcall>(LCID), 237 IsHFTarget ? CallingConv::ARM_AAPCS_VFP 238 : CallingConv::ARM_AAPCS); 239 } 240 241 if (Subtarget->isTargetMachO()) { 242 // Uses VFP for Thumb libfuncs if available. 243 if (Subtarget->isThumb() && Subtarget->hasVFP2() && 244 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) { 245 static const struct { 246 const RTLIB::Libcall Op; 247 const char * const Name; 248 const ISD::CondCode Cond; 249 } LibraryCalls[] = { 250 // Single-precision floating-point arithmetic. 251 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID }, 252 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID }, 253 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID }, 254 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID }, 255 256 // Double-precision floating-point arithmetic. 257 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID }, 258 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID }, 259 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID }, 260 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID }, 261 262 // Single-precision comparisons. 263 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE }, 264 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE }, 265 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE }, 266 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE }, 267 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE }, 268 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE }, 269 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE }, 270 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ }, 271 272 // Double-precision comparisons. 273 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE }, 274 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE }, 275 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE }, 276 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE }, 277 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE }, 278 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE }, 279 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE }, 280 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ }, 281 282 // Floating-point to integer conversions. 283 // i64 conversions are done via library routines even when generating VFP 284 // instructions, so use the same ones. 285 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID }, 286 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID }, 287 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID }, 288 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID }, 289 290 // Conversions between floating types. 291 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID }, 292 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID }, 293 294 // Integer to floating-point conversions. 295 // i64 conversions are done via library routines even when generating VFP 296 // instructions, so use the same ones. 297 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 298 // e.g., __floatunsidf vs. __floatunssidfvfp. 299 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID }, 300 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID }, 301 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID }, 302 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID }, 303 }; 304 305 for (const auto &LC : LibraryCalls) { 306 setLibcallName(LC.Op, LC.Name); 307 if (LC.Cond != ISD::SETCC_INVALID) 308 setCmpLibcallCC(LC.Op, LC.Cond); 309 } 310 } 311 312 // Set the correct calling convention for ARMv7k WatchOS. It's just 313 // AAPCS_VFP for functions as simple as libcalls. 314 if (Subtarget->isTargetWatchABI()) { 315 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) 316 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP); 317 } 318 } 319 320 // These libcalls are not available in 32-bit. 321 setLibcallName(RTLIB::SHL_I128, nullptr); 322 setLibcallName(RTLIB::SRL_I128, nullptr); 323 setLibcallName(RTLIB::SRA_I128, nullptr); 324 325 // RTLIB 326 if (Subtarget->isAAPCS_ABI() && 327 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() || 328 Subtarget->isTargetMuslAEABI() || Subtarget->isTargetAndroid())) { 329 static const struct { 330 const RTLIB::Libcall Op; 331 const char * const Name; 332 const CallingConv::ID CC; 333 const ISD::CondCode Cond; 334 } LibraryCalls[] = { 335 // Double-precision floating-point arithmetic helper functions 336 // RTABI chapter 4.1.2, Table 2 337 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 338 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 339 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 340 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 341 342 // Double-precision floating-point comparison helper functions 343 // RTABI chapter 4.1.2, Table 3 344 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 345 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 346 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 347 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 348 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 349 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 350 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 351 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 352 353 // Single-precision floating-point arithmetic helper functions 354 // RTABI chapter 4.1.2, Table 4 355 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 356 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 357 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 358 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 359 360 // Single-precision floating-point comparison helper functions 361 // RTABI chapter 4.1.2, Table 5 362 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 363 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 364 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 365 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 366 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 367 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 368 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 369 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 370 371 // Floating-point to integer conversions. 372 // RTABI chapter 4.1.2, Table 6 373 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 374 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 375 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 376 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 377 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 378 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 379 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 380 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 381 382 // Conversions between floating types. 383 // RTABI chapter 4.1.2, Table 7 384 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 385 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 386 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 387 388 // Integer to floating-point conversions. 389 // RTABI chapter 4.1.2, Table 8 390 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 391 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 392 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 393 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 394 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 395 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 396 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 397 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 398 399 // Long long helper functions 400 // RTABI chapter 4.2, Table 9 401 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 402 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 403 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 404 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 405 406 // Integer division functions 407 // RTABI chapter 4.3.1 408 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 409 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 410 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 411 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 412 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 413 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 414 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 415 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 416 }; 417 418 for (const auto &LC : LibraryCalls) { 419 setLibcallName(LC.Op, LC.Name); 420 setLibcallCallingConv(LC.Op, LC.CC); 421 if (LC.Cond != ISD::SETCC_INVALID) 422 setCmpLibcallCC(LC.Op, LC.Cond); 423 } 424 425 // EABI dependent RTLIB 426 if (TM.Options.EABIVersion == EABI::EABI4 || 427 TM.Options.EABIVersion == EABI::EABI5) { 428 static const struct { 429 const RTLIB::Libcall Op; 430 const char *const Name; 431 const CallingConv::ID CC; 432 const ISD::CondCode Cond; 433 } MemOpsLibraryCalls[] = { 434 // Memory operations 435 // RTABI chapter 4.3.4 436 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 437 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 438 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 439 }; 440 441 for (const auto &LC : MemOpsLibraryCalls) { 442 setLibcallName(LC.Op, LC.Name); 443 setLibcallCallingConv(LC.Op, LC.CC); 444 if (LC.Cond != ISD::SETCC_INVALID) 445 setCmpLibcallCC(LC.Op, LC.Cond); 446 } 447 } 448 } 449 450 if (Subtarget->isTargetWindows()) { 451 static const struct { 452 const RTLIB::Libcall Op; 453 const char * const Name; 454 const CallingConv::ID CC; 455 } LibraryCalls[] = { 456 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP }, 457 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP }, 458 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP }, 459 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP }, 460 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP }, 461 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP }, 462 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP }, 463 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP }, 464 }; 465 466 for (const auto &LC : LibraryCalls) { 467 setLibcallName(LC.Op, LC.Name); 468 setLibcallCallingConv(LC.Op, LC.CC); 469 } 470 } 471 472 // Use divmod compiler-rt calls for iOS 5.0 and later. 473 if (Subtarget->isTargetMachO() && 474 !(Subtarget->isTargetIOS() && 475 Subtarget->getTargetTriple().isOSVersionLT(5, 0))) { 476 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 477 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 478 } 479 480 // The half <-> float conversion functions are always soft-float on 481 // non-watchos platforms, but are needed for some targets which use a 482 // hard-float calling convention by default. 483 if (!Subtarget->isTargetWatchABI()) { 484 if (Subtarget->isAAPCS_ABI()) { 485 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS); 486 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS); 487 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS); 488 } else { 489 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS); 490 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS); 491 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS); 492 } 493 } 494 495 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have 496 // a __gnu_ prefix (which is the default). 497 if (Subtarget->isTargetAEABI()) { 498 static const struct { 499 const RTLIB::Libcall Op; 500 const char * const Name; 501 const CallingConv::ID CC; 502 } LibraryCalls[] = { 503 { RTLIB::FPROUND_F32_F16, "__aeabi_f2h", CallingConv::ARM_AAPCS }, 504 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS }, 505 { RTLIB::FPEXT_F16_F32, "__aeabi_h2f", CallingConv::ARM_AAPCS }, 506 }; 507 508 for (const auto &LC : LibraryCalls) { 509 setLibcallName(LC.Op, LC.Name); 510 setLibcallCallingConv(LC.Op, LC.CC); 511 } 512 } 513 514 if (Subtarget->isThumb1Only()) 515 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 516 else 517 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 518 519 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 520 !Subtarget->isThumb1Only()) { 521 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 522 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 523 } 524 525 for (MVT VT : MVT::vector_valuetypes()) { 526 for (MVT InnerVT : MVT::vector_valuetypes()) { 527 setTruncStoreAction(VT, InnerVT, Expand); 528 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 529 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 530 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 531 } 532 533 setOperationAction(ISD::MULHS, VT, Expand); 534 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 535 setOperationAction(ISD::MULHU, VT, Expand); 536 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 537 538 setOperationAction(ISD::BSWAP, VT, Expand); 539 } 540 541 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 542 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 543 544 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom); 545 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom); 546 547 if (Subtarget->hasNEON()) { 548 addDRTypeForNEON(MVT::v2f32); 549 addDRTypeForNEON(MVT::v8i8); 550 addDRTypeForNEON(MVT::v4i16); 551 addDRTypeForNEON(MVT::v2i32); 552 addDRTypeForNEON(MVT::v1i64); 553 554 addQRTypeForNEON(MVT::v4f32); 555 addQRTypeForNEON(MVT::v2f64); 556 addQRTypeForNEON(MVT::v16i8); 557 addQRTypeForNEON(MVT::v8i16); 558 addQRTypeForNEON(MVT::v4i32); 559 addQRTypeForNEON(MVT::v2i64); 560 561 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 562 // neither Neon nor VFP support any arithmetic operations on it. 563 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 564 // supported for v4f32. 565 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 566 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 567 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 568 // FIXME: Code duplication: FDIV and FREM are expanded always, see 569 // ARMTargetLowering::addTypeForNEON method for details. 570 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 571 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 572 // FIXME: Create unittest. 573 // In another words, find a way when "copysign" appears in DAG with vector 574 // operands. 575 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 576 // FIXME: Code duplication: SETCC has custom operation action, see 577 // ARMTargetLowering::addTypeForNEON method for details. 578 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 579 // FIXME: Create unittest for FNEG and for FABS. 580 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 581 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 582 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 583 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 584 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 585 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 586 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 587 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 588 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 589 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 590 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 591 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 592 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 593 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 594 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 595 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 596 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 597 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 598 599 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 600 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 601 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 602 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 603 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 604 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 605 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 606 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 607 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 608 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 609 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 610 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 611 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 612 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 613 614 // Mark v2f32 intrinsics. 615 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 616 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 617 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 618 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 619 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 620 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 621 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 622 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 623 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 624 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 625 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 626 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 627 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 628 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 629 630 // Neon does not support some operations on v1i64 and v2i64 types. 631 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 632 // Custom handling for some quad-vector types to detect VMULL. 633 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 634 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 635 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 636 // Custom handling for some vector types to avoid expensive expansions 637 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 638 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 639 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 640 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 641 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 642 // a destination type that is wider than the source, and nor does 643 // it have a FP_TO_[SU]INT instruction with a narrower destination than 644 // source. 645 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 646 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 647 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 648 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 649 650 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 651 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 652 653 // NEON does not have single instruction CTPOP for vectors with element 654 // types wider than 8-bits. However, custom lowering can leverage the 655 // v8i8/v16i8 vcnt instruction. 656 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 657 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 658 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 659 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 660 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand); 661 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand); 662 663 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand); 664 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand); 665 666 // NEON does not have single instruction CTTZ for vectors. 667 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom); 668 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom); 669 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom); 670 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom); 671 672 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom); 673 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom); 674 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom); 675 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom); 676 677 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom); 678 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom); 679 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom); 680 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom); 681 682 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom); 683 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom); 684 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom); 685 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom); 686 687 // NEON only has FMA instructions as of VFP4. 688 if (!Subtarget->hasVFP4()) { 689 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 690 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 691 } 692 693 setTargetDAGCombine(ISD::INTRINSIC_VOID); 694 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 695 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 696 setTargetDAGCombine(ISD::SHL); 697 setTargetDAGCombine(ISD::SRL); 698 setTargetDAGCombine(ISD::SRA); 699 setTargetDAGCombine(ISD::SIGN_EXTEND); 700 setTargetDAGCombine(ISD::ZERO_EXTEND); 701 setTargetDAGCombine(ISD::ANY_EXTEND); 702 setTargetDAGCombine(ISD::BUILD_VECTOR); 703 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 704 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 705 setTargetDAGCombine(ISD::STORE); 706 setTargetDAGCombine(ISD::FP_TO_SINT); 707 setTargetDAGCombine(ISD::FP_TO_UINT); 708 setTargetDAGCombine(ISD::FDIV); 709 setTargetDAGCombine(ISD::LOAD); 710 711 // It is legal to extload from v4i8 to v4i16 or v4i32. 712 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16, 713 MVT::v2i32}) { 714 for (MVT VT : MVT::integer_vector_valuetypes()) { 715 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal); 716 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal); 717 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal); 718 } 719 } 720 } 721 722 if (Subtarget->isFPOnlySP()) { 723 // When targeting a floating-point unit with only single-precision 724 // operations, f64 is legal for the few double-precision instructions which 725 // are present However, no double-precision operations other than moves, 726 // loads and stores are provided by the hardware. 727 setOperationAction(ISD::FADD, MVT::f64, Expand); 728 setOperationAction(ISD::FSUB, MVT::f64, Expand); 729 setOperationAction(ISD::FMUL, MVT::f64, Expand); 730 setOperationAction(ISD::FMA, MVT::f64, Expand); 731 setOperationAction(ISD::FDIV, MVT::f64, Expand); 732 setOperationAction(ISD::FREM, MVT::f64, Expand); 733 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 734 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand); 735 setOperationAction(ISD::FNEG, MVT::f64, Expand); 736 setOperationAction(ISD::FABS, MVT::f64, Expand); 737 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 738 setOperationAction(ISD::FSIN, MVT::f64, Expand); 739 setOperationAction(ISD::FCOS, MVT::f64, Expand); 740 setOperationAction(ISD::FPOW, MVT::f64, Expand); 741 setOperationAction(ISD::FLOG, MVT::f64, Expand); 742 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 743 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 744 setOperationAction(ISD::FEXP, MVT::f64, Expand); 745 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 746 setOperationAction(ISD::FCEIL, MVT::f64, Expand); 747 setOperationAction(ISD::FTRUNC, MVT::f64, Expand); 748 setOperationAction(ISD::FRINT, MVT::f64, Expand); 749 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand); 750 setOperationAction(ISD::FFLOOR, MVT::f64, Expand); 751 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 752 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 753 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 754 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 755 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom); 756 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom); 757 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 758 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom); 759 } 760 761 computeRegisterProperties(Subtarget->getRegisterInfo()); 762 763 // ARM does not have floating-point extending loads. 764 for (MVT VT : MVT::fp_valuetypes()) { 765 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 766 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 767 } 768 769 // ... or truncating stores 770 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 771 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 772 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 773 774 // ARM does not have i1 sign extending load. 775 for (MVT VT : MVT::integer_valuetypes()) 776 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 777 778 // ARM supports all 4 flavors of integer indexed load / store. 779 if (!Subtarget->isThumb1Only()) { 780 for (unsigned im = (unsigned)ISD::PRE_INC; 781 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 782 setIndexedLoadAction(im, MVT::i1, Legal); 783 setIndexedLoadAction(im, MVT::i8, Legal); 784 setIndexedLoadAction(im, MVT::i16, Legal); 785 setIndexedLoadAction(im, MVT::i32, Legal); 786 setIndexedStoreAction(im, MVT::i1, Legal); 787 setIndexedStoreAction(im, MVT::i8, Legal); 788 setIndexedStoreAction(im, MVT::i16, Legal); 789 setIndexedStoreAction(im, MVT::i32, Legal); 790 } 791 } else { 792 // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}. 793 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal); 794 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal); 795 } 796 797 setOperationAction(ISD::SADDO, MVT::i32, Custom); 798 setOperationAction(ISD::UADDO, MVT::i32, Custom); 799 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 800 setOperationAction(ISD::USUBO, MVT::i32, Custom); 801 802 setOperationAction(ISD::ADDCARRY, MVT::i32, Custom); 803 setOperationAction(ISD::SUBCARRY, MVT::i32, Custom); 804 805 // i64 operation support. 806 setOperationAction(ISD::MUL, MVT::i64, Expand); 807 setOperationAction(ISD::MULHU, MVT::i32, Expand); 808 if (Subtarget->isThumb1Only()) { 809 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 810 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 811 } 812 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 813 || (Subtarget->isThumb2() && !Subtarget->hasDSP())) 814 setOperationAction(ISD::MULHS, MVT::i32, Expand); 815 816 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 817 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 818 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 819 setOperationAction(ISD::SRL, MVT::i64, Custom); 820 setOperationAction(ISD::SRA, MVT::i64, Custom); 821 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom); 822 823 setOperationAction(ISD::ADDC, MVT::i32, Custom); 824 setOperationAction(ISD::ADDE, MVT::i32, Custom); 825 setOperationAction(ISD::SUBC, MVT::i32, Custom); 826 setOperationAction(ISD::SUBE, MVT::i32, Custom); 827 828 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) 829 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 830 831 // ARM does not have ROTL. 832 setOperationAction(ISD::ROTL, MVT::i32, Expand); 833 for (MVT VT : MVT::vector_valuetypes()) { 834 setOperationAction(ISD::ROTL, VT, Expand); 835 setOperationAction(ISD::ROTR, VT, Expand); 836 } 837 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 838 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 839 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 840 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 841 842 // @llvm.readcyclecounter requires the Performance Monitors extension. 843 // Default to the 0 expansion on unsupported platforms. 844 // FIXME: Technically there are older ARM CPUs that have 845 // implementation-specific ways of obtaining this information. 846 if (Subtarget->hasPerfMon()) 847 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 848 849 // Only ARMv6 has BSWAP. 850 if (!Subtarget->hasV6Ops()) 851 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 852 853 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode() 854 : Subtarget->hasDivideInARMMode(); 855 if (!hasDivide) { 856 // These are expanded into libcalls if the cpu doesn't have HW divider. 857 setOperationAction(ISD::SDIV, MVT::i32, LibCall); 858 setOperationAction(ISD::UDIV, MVT::i32, LibCall); 859 } 860 861 if (Subtarget->isTargetWindows() && !Subtarget->hasDivideInThumbMode()) { 862 setOperationAction(ISD::SDIV, MVT::i32, Custom); 863 setOperationAction(ISD::UDIV, MVT::i32, Custom); 864 865 setOperationAction(ISD::SDIV, MVT::i64, Custom); 866 setOperationAction(ISD::UDIV, MVT::i64, Custom); 867 } 868 869 setOperationAction(ISD::SREM, MVT::i32, Expand); 870 setOperationAction(ISD::UREM, MVT::i32, Expand); 871 872 // Register based DivRem for AEABI (RTABI 4.2) 873 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 874 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() || 875 Subtarget->isTargetWindows()) { 876 setOperationAction(ISD::SREM, MVT::i64, Custom); 877 setOperationAction(ISD::UREM, MVT::i64, Custom); 878 HasStandaloneRem = false; 879 880 if (Subtarget->isTargetWindows()) { 881 const struct { 882 const RTLIB::Libcall Op; 883 const char * const Name; 884 const CallingConv::ID CC; 885 } LibraryCalls[] = { 886 { RTLIB::SDIVREM_I8, "__rt_sdiv", CallingConv::ARM_AAPCS }, 887 { RTLIB::SDIVREM_I16, "__rt_sdiv", CallingConv::ARM_AAPCS }, 888 { RTLIB::SDIVREM_I32, "__rt_sdiv", CallingConv::ARM_AAPCS }, 889 { RTLIB::SDIVREM_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS }, 890 891 { RTLIB::UDIVREM_I8, "__rt_udiv", CallingConv::ARM_AAPCS }, 892 { RTLIB::UDIVREM_I16, "__rt_udiv", CallingConv::ARM_AAPCS }, 893 { RTLIB::UDIVREM_I32, "__rt_udiv", CallingConv::ARM_AAPCS }, 894 { RTLIB::UDIVREM_I64, "__rt_udiv64", CallingConv::ARM_AAPCS }, 895 }; 896 897 for (const auto &LC : LibraryCalls) { 898 setLibcallName(LC.Op, LC.Name); 899 setLibcallCallingConv(LC.Op, LC.CC); 900 } 901 } else { 902 const struct { 903 const RTLIB::Libcall Op; 904 const char * const Name; 905 const CallingConv::ID CC; 906 } LibraryCalls[] = { 907 { RTLIB::SDIVREM_I8, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 908 { RTLIB::SDIVREM_I16, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 909 { RTLIB::SDIVREM_I32, "__aeabi_idivmod", CallingConv::ARM_AAPCS }, 910 { RTLIB::SDIVREM_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS }, 911 912 { RTLIB::UDIVREM_I8, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 913 { RTLIB::UDIVREM_I16, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 914 { RTLIB::UDIVREM_I32, "__aeabi_uidivmod", CallingConv::ARM_AAPCS }, 915 { RTLIB::UDIVREM_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS }, 916 }; 917 918 for (const auto &LC : LibraryCalls) { 919 setLibcallName(LC.Op, LC.Name); 920 setLibcallCallingConv(LC.Op, LC.CC); 921 } 922 } 923 924 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 925 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 926 setOperationAction(ISD::SDIVREM, MVT::i64, Custom); 927 setOperationAction(ISD::UDIVREM, MVT::i64, Custom); 928 } else { 929 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 930 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 931 } 932 933 if (Subtarget->isTargetWindows() && Subtarget->getTargetTriple().isOSMSVCRT()) 934 for (auto &VT : {MVT::f32, MVT::f64}) 935 setOperationAction(ISD::FPOWI, VT, Custom); 936 937 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 938 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 939 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 940 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 941 942 setOperationAction(ISD::TRAP, MVT::Other, Legal); 943 944 // Use the default implementation. 945 setOperationAction(ISD::VASTART, MVT::Other, Custom); 946 setOperationAction(ISD::VAARG, MVT::Other, Expand); 947 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 948 setOperationAction(ISD::VAEND, MVT::Other, Expand); 949 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 950 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 951 952 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 953 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 954 else 955 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 956 957 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 958 // the default expansion. 959 InsertFencesForAtomic = false; 960 if (Subtarget->hasAnyDataBarrier() && 961 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) { 962 // ATOMIC_FENCE needs custom lowering; the others should have been expanded 963 // to ldrex/strex loops already. 964 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 965 if (!Subtarget->isThumb() || !Subtarget->isMClass()) 966 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 967 968 // On v8, we have particularly efficient implementations of atomic fences 969 // if they can be combined with nearby atomic loads and stores. 970 if (!Subtarget->hasV8Ops() || getTargetMachine().getOptLevel() == 0) { 971 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc. 972 InsertFencesForAtomic = true; 973 } 974 } else { 975 // If there's anything we can use as a barrier, go through custom lowering 976 // for ATOMIC_FENCE. 977 // If target has DMB in thumb, Fences can be inserted. 978 if (Subtarget->hasDataBarrier()) 979 InsertFencesForAtomic = true; 980 981 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, 982 Subtarget->hasAnyDataBarrier() ? Custom : Expand); 983 984 // Set them all for expansion, which will force libcalls. 985 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 986 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 987 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 988 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 989 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 990 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 991 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 992 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 993 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 994 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 995 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 996 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 997 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 998 // Unordered/Monotonic case. 999 if (!InsertFencesForAtomic) { 1000 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 1001 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 1002 } 1003 } 1004 1005 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 1006 1007 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 1008 if (!Subtarget->hasV6Ops()) { 1009 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 1010 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 1011 } 1012 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 1013 1014 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 1015 !Subtarget->isThumb1Only()) { 1016 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 1017 // iff target supports vfp2. 1018 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 1019 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 1020 } 1021 1022 // We want to custom lower some of our intrinsics. 1023 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 1024 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 1025 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 1026 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom); 1027 if (Subtarget->useSjLjEH()) 1028 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 1029 1030 setOperationAction(ISD::SETCC, MVT::i32, Expand); 1031 setOperationAction(ISD::SETCC, MVT::f32, Expand); 1032 setOperationAction(ISD::SETCC, MVT::f64, Expand); 1033 setOperationAction(ISD::SELECT, MVT::i32, Custom); 1034 setOperationAction(ISD::SELECT, MVT::f32, Custom); 1035 setOperationAction(ISD::SELECT, MVT::f64, Custom); 1036 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 1037 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 1038 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 1039 1040 // Thumb-1 cannot currently select ARMISD::SUBE. 1041 if (!Subtarget->isThumb1Only()) 1042 setOperationAction(ISD::SETCCE, MVT::i32, Custom); 1043 1044 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 1045 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 1046 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 1047 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 1048 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 1049 1050 // We don't support sin/cos/fmod/copysign/pow 1051 setOperationAction(ISD::FSIN, MVT::f64, Expand); 1052 setOperationAction(ISD::FSIN, MVT::f32, Expand); 1053 setOperationAction(ISD::FCOS, MVT::f32, Expand); 1054 setOperationAction(ISD::FCOS, MVT::f64, Expand); 1055 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 1056 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 1057 setOperationAction(ISD::FREM, MVT::f64, Expand); 1058 setOperationAction(ISD::FREM, MVT::f32, Expand); 1059 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 1060 !Subtarget->isThumb1Only()) { 1061 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 1062 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 1063 } 1064 setOperationAction(ISD::FPOW, MVT::f64, Expand); 1065 setOperationAction(ISD::FPOW, MVT::f32, Expand); 1066 1067 if (!Subtarget->hasVFP4()) { 1068 setOperationAction(ISD::FMA, MVT::f64, Expand); 1069 setOperationAction(ISD::FMA, MVT::f32, Expand); 1070 } 1071 1072 // Various VFP goodness 1073 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) { 1074 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded. 1075 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) { 1076 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 1077 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 1078 } 1079 1080 // fp16 is a special v7 extension that adds f16 <-> f32 conversions. 1081 if (!Subtarget->hasFP16()) { 1082 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 1083 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 1084 } 1085 } 1086 1087 // Use __sincos_stret if available. 1088 if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr && 1089 getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) { 1090 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 1091 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 1092 } 1093 1094 // FP-ARMv8 implements a lot of rounding-like FP operations. 1095 if (Subtarget->hasFPARMv8()) { 1096 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 1097 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 1098 setOperationAction(ISD::FROUND, MVT::f32, Legal); 1099 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 1100 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 1101 setOperationAction(ISD::FRINT, MVT::f32, Legal); 1102 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 1103 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 1104 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal); 1105 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal); 1106 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); 1107 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); 1108 1109 if (!Subtarget->isFPOnlySP()) { 1110 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 1111 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 1112 setOperationAction(ISD::FROUND, MVT::f64, Legal); 1113 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 1114 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 1115 setOperationAction(ISD::FRINT, MVT::f64, Legal); 1116 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 1117 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 1118 } 1119 } 1120 1121 if (Subtarget->hasNEON()) { 1122 // vmin and vmax aren't available in a scalar form, so we use 1123 // a NEON instruction with an undef lane instead. 1124 setOperationAction(ISD::FMINNAN, MVT::f32, Legal); 1125 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal); 1126 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal); 1127 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal); 1128 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal); 1129 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal); 1130 } 1131 1132 // We have target-specific dag combine patterns for the following nodes: 1133 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 1134 setTargetDAGCombine(ISD::ADD); 1135 setTargetDAGCombine(ISD::SUB); 1136 setTargetDAGCombine(ISD::MUL); 1137 setTargetDAGCombine(ISD::AND); 1138 setTargetDAGCombine(ISD::OR); 1139 setTargetDAGCombine(ISD::XOR); 1140 1141 if (Subtarget->hasV6Ops()) 1142 setTargetDAGCombine(ISD::SRL); 1143 1144 setStackPointerRegisterToSaveRestore(ARM::SP); 1145 1146 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() || 1147 !Subtarget->hasVFP2()) 1148 setSchedulingPreference(Sched::RegPressure); 1149 else 1150 setSchedulingPreference(Sched::Hybrid); 1151 1152 //// temporary - rewrite interface to use type 1153 MaxStoresPerMemset = 8; 1154 MaxStoresPerMemsetOptSize = 4; 1155 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 1156 MaxStoresPerMemcpyOptSize = 2; 1157 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 1158 MaxStoresPerMemmoveOptSize = 2; 1159 1160 // On ARM arguments smaller than 4 bytes are extended, so all arguments 1161 // are at least 4 bytes aligned. 1162 setMinStackArgumentAlignment(4); 1163 1164 // Prefer likely predicted branches to selects on out-of-order cores. 1165 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder(); 1166 1167 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 1168 } 1169 1170 bool ARMTargetLowering::useSoftFloat() const { 1171 return Subtarget->useSoftFloat(); 1172 } 1173 1174 // FIXME: It might make sense to define the representative register class as the 1175 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 1176 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 1177 // SPR's representative would be DPR_VFP2. This should work well if register 1178 // pressure tracking were modified such that a register use would increment the 1179 // pressure of the register class's representative and all of it's super 1180 // classes' representatives transitively. We have not implemented this because 1181 // of the difficulty prior to coalescing of modeling operand register classes 1182 // due to the common occurrence of cross class copies and subregister insertions 1183 // and extractions. 1184 std::pair<const TargetRegisterClass *, uint8_t> 1185 ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI, 1186 MVT VT) const { 1187 const TargetRegisterClass *RRC = nullptr; 1188 uint8_t Cost = 1; 1189 switch (VT.SimpleTy) { 1190 default: 1191 return TargetLowering::findRepresentativeClass(TRI, VT); 1192 // Use DPR as representative register class for all floating point 1193 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 1194 // the cost is 1 for both f32 and f64. 1195 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 1196 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 1197 RRC = &ARM::DPRRegClass; 1198 // When NEON is used for SP, only half of the register file is available 1199 // because operations that define both SP and DP results will be constrained 1200 // to the VFP2 class (D0-D15). We currently model this constraint prior to 1201 // coalescing by double-counting the SP regs. See the FIXME above. 1202 if (Subtarget->useNEONForSinglePrecisionFP()) 1203 Cost = 2; 1204 break; 1205 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 1206 case MVT::v4f32: case MVT::v2f64: 1207 RRC = &ARM::DPRRegClass; 1208 Cost = 2; 1209 break; 1210 case MVT::v4i64: 1211 RRC = &ARM::DPRRegClass; 1212 Cost = 4; 1213 break; 1214 case MVT::v8i64: 1215 RRC = &ARM::DPRRegClass; 1216 Cost = 8; 1217 break; 1218 } 1219 return std::make_pair(RRC, Cost); 1220 } 1221 1222 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 1223 switch ((ARMISD::NodeType)Opcode) { 1224 case ARMISD::FIRST_NUMBER: break; 1225 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 1226 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 1227 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 1228 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL"; 1229 case ARMISD::CALL: return "ARMISD::CALL"; 1230 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 1231 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 1232 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 1233 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 1234 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 1235 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 1236 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG"; 1237 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 1238 case ARMISD::CMP: return "ARMISD::CMP"; 1239 case ARMISD::CMN: return "ARMISD::CMN"; 1240 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 1241 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 1242 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 1243 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 1244 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 1245 1246 case ARMISD::CMOV: return "ARMISD::CMOV"; 1247 1248 case ARMISD::SSAT: return "ARMISD::SSAT"; 1249 case ARMISD::USAT: return "ARMISD::USAT"; 1250 1251 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 1252 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 1253 case ARMISD::RRX: return "ARMISD::RRX"; 1254 1255 case ARMISD::ADDC: return "ARMISD::ADDC"; 1256 case ARMISD::ADDE: return "ARMISD::ADDE"; 1257 case ARMISD::SUBC: return "ARMISD::SUBC"; 1258 case ARMISD::SUBE: return "ARMISD::SUBE"; 1259 1260 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1261 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1262 1263 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1264 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP"; 1265 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH"; 1266 1267 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1268 1269 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1270 1271 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1272 1273 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1274 1275 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1276 1277 case ARMISD::WIN__CHKSTK: return "ARMISD::WIN__CHKSTK"; 1278 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK"; 1279 1280 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 1281 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 1282 case ARMISD::VCGE: return "ARMISD::VCGE"; 1283 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 1284 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 1285 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 1286 case ARMISD::VCGT: return "ARMISD::VCGT"; 1287 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 1288 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 1289 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 1290 case ARMISD::VTST: return "ARMISD::VTST"; 1291 1292 case ARMISD::VSHL: return "ARMISD::VSHL"; 1293 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 1294 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 1295 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 1296 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 1297 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 1298 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 1299 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 1300 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 1301 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 1302 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 1303 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 1304 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 1305 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 1306 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 1307 case ARMISD::VSLI: return "ARMISD::VSLI"; 1308 case ARMISD::VSRI: return "ARMISD::VSRI"; 1309 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1310 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1311 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1312 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1313 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1314 case ARMISD::VDUP: return "ARMISD::VDUP"; 1315 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1316 case ARMISD::VEXT: return "ARMISD::VEXT"; 1317 case ARMISD::VREV64: return "ARMISD::VREV64"; 1318 case ARMISD::VREV32: return "ARMISD::VREV32"; 1319 case ARMISD::VREV16: return "ARMISD::VREV16"; 1320 case ARMISD::VZIP: return "ARMISD::VZIP"; 1321 case ARMISD::VUZP: return "ARMISD::VUZP"; 1322 case ARMISD::VTRN: return "ARMISD::VTRN"; 1323 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1324 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1325 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1326 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1327 case ARMISD::UMAAL: return "ARMISD::UMAAL"; 1328 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1329 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1330 case ARMISD::SMLALBB: return "ARMISD::SMLALBB"; 1331 case ARMISD::SMLALBT: return "ARMISD::SMLALBT"; 1332 case ARMISD::SMLALTB: return "ARMISD::SMLALTB"; 1333 case ARMISD::SMLALTT: return "ARMISD::SMLALTT"; 1334 case ARMISD::SMULWB: return "ARMISD::SMULWB"; 1335 case ARMISD::SMULWT: return "ARMISD::SMULWT"; 1336 case ARMISD::SMLALD: return "ARMISD::SMLALD"; 1337 case ARMISD::SMLALDX: return "ARMISD::SMLALDX"; 1338 case ARMISD::SMLSLD: return "ARMISD::SMLSLD"; 1339 case ARMISD::SMLSLDX: return "ARMISD::SMLSLDX"; 1340 case ARMISD::SMMLAR: return "ARMISD::SMMLAR"; 1341 case ARMISD::SMMLSR: return "ARMISD::SMMLSR"; 1342 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1343 case ARMISD::BFI: return "ARMISD::BFI"; 1344 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1345 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1346 case ARMISD::VBSL: return "ARMISD::VBSL"; 1347 case ARMISD::MEMCPY: return "ARMISD::MEMCPY"; 1348 case ARMISD::VLD1DUP: return "ARMISD::VLD1DUP"; 1349 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1350 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1351 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1352 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1353 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1354 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1355 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1356 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1357 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1358 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1359 case ARMISD::VLD1DUP_UPD: return "ARMISD::VLD1DUP_UPD"; 1360 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1361 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1362 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1363 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1364 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1365 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1366 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1367 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1368 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1369 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1370 } 1371 return nullptr; 1372 } 1373 1374 EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 1375 EVT VT) const { 1376 if (!VT.isVector()) 1377 return getPointerTy(DL); 1378 return VT.changeVectorElementTypeToInteger(); 1379 } 1380 1381 /// getRegClassFor - Return the register class that should be used for the 1382 /// specified value type. 1383 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const { 1384 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1385 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1386 // load / store 4 to 8 consecutive D registers. 1387 if (Subtarget->hasNEON()) { 1388 if (VT == MVT::v4i64) 1389 return &ARM::QQPRRegClass; 1390 if (VT == MVT::v8i64) 1391 return &ARM::QQQQPRRegClass; 1392 } 1393 return TargetLowering::getRegClassFor(VT); 1394 } 1395 1396 // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the 1397 // source/dest is aligned and the copy size is large enough. We therefore want 1398 // to align such objects passed to memory intrinsics. 1399 bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize, 1400 unsigned &PrefAlign) const { 1401 if (!isa<MemIntrinsic>(CI)) 1402 return false; 1403 MinSize = 8; 1404 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1 1405 // cycle faster than 4-byte aligned LDM. 1406 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4); 1407 return true; 1408 } 1409 1410 // Create a fast isel object. 1411 FastISel * 1412 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1413 const TargetLibraryInfo *libInfo) const { 1414 return ARM::createFastISel(funcInfo, libInfo); 1415 } 1416 1417 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1418 unsigned NumVals = N->getNumValues(); 1419 if (!NumVals) 1420 return Sched::RegPressure; 1421 1422 for (unsigned i = 0; i != NumVals; ++i) { 1423 EVT VT = N->getValueType(i); 1424 if (VT == MVT::Glue || VT == MVT::Other) 1425 continue; 1426 if (VT.isFloatingPoint() || VT.isVector()) 1427 return Sched::ILP; 1428 } 1429 1430 if (!N->isMachineOpcode()) 1431 return Sched::RegPressure; 1432 1433 // Load are scheduled for latency even if there instruction itinerary 1434 // is not available. 1435 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1436 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1437 1438 if (MCID.getNumDefs() == 0) 1439 return Sched::RegPressure; 1440 if (!Itins->isEmpty() && 1441 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1442 return Sched::ILP; 1443 1444 return Sched::RegPressure; 1445 } 1446 1447 //===----------------------------------------------------------------------===// 1448 // Lowering Code 1449 //===----------------------------------------------------------------------===// 1450 1451 static bool isSRL16(const SDValue &Op) { 1452 if (Op.getOpcode() != ISD::SRL) 1453 return false; 1454 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1455 return Const->getZExtValue() == 16; 1456 return false; 1457 } 1458 1459 static bool isSRA16(const SDValue &Op) { 1460 if (Op.getOpcode() != ISD::SRA) 1461 return false; 1462 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1463 return Const->getZExtValue() == 16; 1464 return false; 1465 } 1466 1467 static bool isSHL16(const SDValue &Op) { 1468 if (Op.getOpcode() != ISD::SHL) 1469 return false; 1470 if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 1471 return Const->getZExtValue() == 16; 1472 return false; 1473 } 1474 1475 // Check for a signed 16-bit value. We special case SRA because it makes it 1476 // more simple when also looking for SRAs that aren't sign extending a 1477 // smaller value. Without the check, we'd need to take extra care with 1478 // checking order for some operations. 1479 static bool isS16(const SDValue &Op, SelectionDAG &DAG) { 1480 if (isSRA16(Op)) 1481 return isSHL16(Op.getOperand(0)); 1482 return DAG.ComputeNumSignBits(Op) == 17; 1483 } 1484 1485 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1486 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1487 switch (CC) { 1488 default: llvm_unreachable("Unknown condition code!"); 1489 case ISD::SETNE: return ARMCC::NE; 1490 case ISD::SETEQ: return ARMCC::EQ; 1491 case ISD::SETGT: return ARMCC::GT; 1492 case ISD::SETGE: return ARMCC::GE; 1493 case ISD::SETLT: return ARMCC::LT; 1494 case ISD::SETLE: return ARMCC::LE; 1495 case ISD::SETUGT: return ARMCC::HI; 1496 case ISD::SETUGE: return ARMCC::HS; 1497 case ISD::SETULT: return ARMCC::LO; 1498 case ISD::SETULE: return ARMCC::LS; 1499 } 1500 } 1501 1502 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1503 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1504 ARMCC::CondCodes &CondCode2, bool &InvalidOnQNaN) { 1505 CondCode2 = ARMCC::AL; 1506 InvalidOnQNaN = true; 1507 switch (CC) { 1508 default: llvm_unreachable("Unknown FP condition!"); 1509 case ISD::SETEQ: 1510 case ISD::SETOEQ: 1511 CondCode = ARMCC::EQ; 1512 InvalidOnQNaN = false; 1513 break; 1514 case ISD::SETGT: 1515 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1516 case ISD::SETGE: 1517 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1518 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1519 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1520 case ISD::SETONE: 1521 CondCode = ARMCC::MI; 1522 CondCode2 = ARMCC::GT; 1523 InvalidOnQNaN = false; 1524 break; 1525 case ISD::SETO: CondCode = ARMCC::VC; break; 1526 case ISD::SETUO: CondCode = ARMCC::VS; break; 1527 case ISD::SETUEQ: 1528 CondCode = ARMCC::EQ; 1529 CondCode2 = ARMCC::VS; 1530 InvalidOnQNaN = false; 1531 break; 1532 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1533 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1534 case ISD::SETLT: 1535 case ISD::SETULT: CondCode = ARMCC::LT; break; 1536 case ISD::SETLE: 1537 case ISD::SETULE: CondCode = ARMCC::LE; break; 1538 case ISD::SETNE: 1539 case ISD::SETUNE: 1540 CondCode = ARMCC::NE; 1541 InvalidOnQNaN = false; 1542 break; 1543 } 1544 } 1545 1546 //===----------------------------------------------------------------------===// 1547 // Calling Convention Implementation 1548 //===----------------------------------------------------------------------===// 1549 1550 #include "ARMGenCallingConv.inc" 1551 1552 /// getEffectiveCallingConv - Get the effective calling convention, taking into 1553 /// account presence of floating point hardware and calling convention 1554 /// limitations, such as support for variadic functions. 1555 CallingConv::ID 1556 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC, 1557 bool isVarArg) const { 1558 switch (CC) { 1559 default: 1560 report_fatal_error("Unsupported calling convention"); 1561 case CallingConv::ARM_AAPCS: 1562 case CallingConv::ARM_APCS: 1563 case CallingConv::GHC: 1564 return CC; 1565 case CallingConv::PreserveMost: 1566 return CallingConv::PreserveMost; 1567 case CallingConv::ARM_AAPCS_VFP: 1568 case CallingConv::Swift: 1569 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP; 1570 case CallingConv::C: 1571 if (!Subtarget->isAAPCS_ABI()) 1572 return CallingConv::ARM_APCS; 1573 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && 1574 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1575 !isVarArg) 1576 return CallingConv::ARM_AAPCS_VFP; 1577 else 1578 return CallingConv::ARM_AAPCS; 1579 case CallingConv::Fast: 1580 case CallingConv::CXX_FAST_TLS: 1581 if (!Subtarget->isAAPCS_ABI()) { 1582 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1583 return CallingConv::Fast; 1584 return CallingConv::ARM_APCS; 1585 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1586 return CallingConv::ARM_AAPCS_VFP; 1587 else 1588 return CallingConv::ARM_AAPCS; 1589 } 1590 } 1591 1592 CCAssignFn *ARMTargetLowering::CCAssignFnForCall(CallingConv::ID CC, 1593 bool isVarArg) const { 1594 return CCAssignFnForNode(CC, false, isVarArg); 1595 } 1596 1597 CCAssignFn *ARMTargetLowering::CCAssignFnForReturn(CallingConv::ID CC, 1598 bool isVarArg) const { 1599 return CCAssignFnForNode(CC, true, isVarArg); 1600 } 1601 1602 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given 1603 /// CallingConvention. 1604 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1605 bool Return, 1606 bool isVarArg) const { 1607 switch (getEffectiveCallingConv(CC, isVarArg)) { 1608 default: 1609 report_fatal_error("Unsupported calling convention"); 1610 case CallingConv::ARM_APCS: 1611 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1612 case CallingConv::ARM_AAPCS: 1613 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1614 case CallingConv::ARM_AAPCS_VFP: 1615 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1616 case CallingConv::Fast: 1617 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1618 case CallingConv::GHC: 1619 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1620 case CallingConv::PreserveMost: 1621 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1622 } 1623 } 1624 1625 /// LowerCallResult - Lower the result values of a call into the 1626 /// appropriate copies out of appropriate physical registers. 1627 SDValue ARMTargetLowering::LowerCallResult( 1628 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, 1629 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 1630 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn, 1631 SDValue ThisVal) const { 1632 // Assign locations to each value returned by this call. 1633 SmallVector<CCValAssign, 16> RVLocs; 1634 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1635 *DAG.getContext()); 1636 CCInfo.AnalyzeCallResult(Ins, CCAssignFnForReturn(CallConv, isVarArg)); 1637 1638 // Copy all of the result registers out of their specified physreg. 1639 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1640 CCValAssign VA = RVLocs[i]; 1641 1642 // Pass 'this' value directly from the argument to return value, to avoid 1643 // reg unit interference 1644 if (i == 0 && isThisReturn) { 1645 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1646 "unexpected return calling convention register assignment"); 1647 InVals.push_back(ThisVal); 1648 continue; 1649 } 1650 1651 SDValue Val; 1652 if (VA.needsCustom()) { 1653 // Handle f64 or half of a v2f64. 1654 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1655 InFlag); 1656 Chain = Lo.getValue(1); 1657 InFlag = Lo.getValue(2); 1658 VA = RVLocs[++i]; // skip ahead to next loc 1659 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1660 InFlag); 1661 Chain = Hi.getValue(1); 1662 InFlag = Hi.getValue(2); 1663 if (!Subtarget->isLittle()) 1664 std::swap (Lo, Hi); 1665 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1666 1667 if (VA.getLocVT() == MVT::v2f64) { 1668 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1669 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1670 DAG.getConstant(0, dl, MVT::i32)); 1671 1672 VA = RVLocs[++i]; // skip ahead to next loc 1673 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1674 Chain = Lo.getValue(1); 1675 InFlag = Lo.getValue(2); 1676 VA = RVLocs[++i]; // skip ahead to next loc 1677 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1678 Chain = Hi.getValue(1); 1679 InFlag = Hi.getValue(2); 1680 if (!Subtarget->isLittle()) 1681 std::swap (Lo, Hi); 1682 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1683 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1684 DAG.getConstant(1, dl, MVT::i32)); 1685 } 1686 } else { 1687 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1688 InFlag); 1689 Chain = Val.getValue(1); 1690 InFlag = Val.getValue(2); 1691 } 1692 1693 switch (VA.getLocInfo()) { 1694 default: llvm_unreachable("Unknown loc info!"); 1695 case CCValAssign::Full: break; 1696 case CCValAssign::BCvt: 1697 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1698 break; 1699 } 1700 1701 InVals.push_back(Val); 1702 } 1703 1704 return Chain; 1705 } 1706 1707 /// LowerMemOpCallTo - Store the argument to the stack. 1708 SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, 1709 SDValue Arg, const SDLoc &dl, 1710 SelectionDAG &DAG, 1711 const CCValAssign &VA, 1712 ISD::ArgFlagsTy Flags) const { 1713 unsigned LocMemOffset = VA.getLocMemOffset(); 1714 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1715 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), 1716 StackPtr, PtrOff); 1717 return DAG.getStore( 1718 Chain, dl, Arg, PtrOff, 1719 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset)); 1720 } 1721 1722 void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG, 1723 SDValue Chain, SDValue &Arg, 1724 RegsToPassVector &RegsToPass, 1725 CCValAssign &VA, CCValAssign &NextVA, 1726 SDValue &StackPtr, 1727 SmallVectorImpl<SDValue> &MemOpChains, 1728 ISD::ArgFlagsTy Flags) const { 1729 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1730 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1731 unsigned id = Subtarget->isLittle() ? 0 : 1; 1732 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id))); 1733 1734 if (NextVA.isRegLoc()) 1735 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id))); 1736 else { 1737 assert(NextVA.isMemLoc()); 1738 if (!StackPtr.getNode()) 1739 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, 1740 getPointerTy(DAG.getDataLayout())); 1741 1742 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id), 1743 dl, DAG, NextVA, 1744 Flags)); 1745 } 1746 } 1747 1748 /// LowerCall - Lowering a call into a callseq_start <- 1749 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1750 /// nodes. 1751 SDValue 1752 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1753 SmallVectorImpl<SDValue> &InVals) const { 1754 SelectionDAG &DAG = CLI.DAG; 1755 SDLoc &dl = CLI.DL; 1756 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1757 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1758 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1759 SDValue Chain = CLI.Chain; 1760 SDValue Callee = CLI.Callee; 1761 bool &isTailCall = CLI.IsTailCall; 1762 CallingConv::ID CallConv = CLI.CallConv; 1763 bool doesNotRet = CLI.DoesNotReturn; 1764 bool isVarArg = CLI.IsVarArg; 1765 1766 MachineFunction &MF = DAG.getMachineFunction(); 1767 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1768 bool isThisReturn = false; 1769 bool isSibCall = false; 1770 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls"); 1771 1772 // Disable tail calls if they're not supported. 1773 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true") 1774 isTailCall = false; 1775 1776 if (isTailCall) { 1777 // Check if it's really possible to do a tail call. 1778 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1779 isVarArg, isStructRet, MF.getFunction().hasStructRetAttr(), 1780 Outs, OutVals, Ins, DAG); 1781 if (!isTailCall && CLI.CS && CLI.CS.isMustTailCall()) 1782 report_fatal_error("failed to perform tail call elimination on a call " 1783 "site marked musttail"); 1784 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1785 // detected sibcalls. 1786 if (isTailCall) { 1787 ++NumTailCalls; 1788 isSibCall = true; 1789 } 1790 } 1791 1792 // Analyze operands of the call, assigning locations to each operand. 1793 SmallVector<CCValAssign, 16> ArgLocs; 1794 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1795 *DAG.getContext()); 1796 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CallConv, isVarArg)); 1797 1798 // Get a count of how many bytes are to be pushed on the stack. 1799 unsigned NumBytes = CCInfo.getNextStackOffset(); 1800 1801 // For tail calls, memory operands are available in our caller's stack. 1802 if (isSibCall) 1803 NumBytes = 0; 1804 1805 // Adjust the stack pointer for the new arguments... 1806 // These operations are automatically eliminated by the prolog/epilog pass 1807 if (!isSibCall) 1808 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 1809 1810 SDValue StackPtr = 1811 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout())); 1812 1813 RegsToPassVector RegsToPass; 1814 SmallVector<SDValue, 8> MemOpChains; 1815 1816 // Walk the register/memloc assignments, inserting copies/loads. In the case 1817 // of tail call optimization, arguments are handled later. 1818 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1819 i != e; 1820 ++i, ++realArgIdx) { 1821 CCValAssign &VA = ArgLocs[i]; 1822 SDValue Arg = OutVals[realArgIdx]; 1823 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1824 bool isByVal = Flags.isByVal(); 1825 1826 // Promote the value if needed. 1827 switch (VA.getLocInfo()) { 1828 default: llvm_unreachable("Unknown loc info!"); 1829 case CCValAssign::Full: break; 1830 case CCValAssign::SExt: 1831 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1832 break; 1833 case CCValAssign::ZExt: 1834 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1835 break; 1836 case CCValAssign::AExt: 1837 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1838 break; 1839 case CCValAssign::BCvt: 1840 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1841 break; 1842 } 1843 1844 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1845 if (VA.needsCustom()) { 1846 if (VA.getLocVT() == MVT::v2f64) { 1847 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1848 DAG.getConstant(0, dl, MVT::i32)); 1849 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1850 DAG.getConstant(1, dl, MVT::i32)); 1851 1852 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1853 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1854 1855 VA = ArgLocs[++i]; // skip ahead to next loc 1856 if (VA.isRegLoc()) { 1857 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1858 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1859 } else { 1860 assert(VA.isMemLoc()); 1861 1862 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1863 dl, DAG, VA, Flags)); 1864 } 1865 } else { 1866 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1867 StackPtr, MemOpChains, Flags); 1868 } 1869 } else if (VA.isRegLoc()) { 1870 if (realArgIdx == 0 && Flags.isReturned() && !Flags.isSwiftSelf() && 1871 Outs[0].VT == MVT::i32) { 1872 assert(VA.getLocVT() == MVT::i32 && 1873 "unexpected calling convention register assignment"); 1874 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 1875 "unexpected use of 'returned'"); 1876 isThisReturn = true; 1877 } 1878 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1879 } else if (isByVal) { 1880 assert(VA.isMemLoc()); 1881 unsigned offset = 0; 1882 1883 // True if this byval aggregate will be split between registers 1884 // and memory. 1885 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 1886 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed(); 1887 1888 if (CurByValIdx < ByValArgsCount) { 1889 1890 unsigned RegBegin, RegEnd; 1891 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 1892 1893 EVT PtrVT = 1894 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 1895 unsigned int i, j; 1896 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 1897 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32); 1898 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1899 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1900 MachinePointerInfo(), 1901 DAG.InferPtrAlignment(AddArg)); 1902 MemOpChains.push_back(Load.getValue(1)); 1903 RegsToPass.push_back(std::make_pair(j, Load)); 1904 } 1905 1906 // If parameter size outsides register area, "offset" value 1907 // helps us to calculate stack slot for remained part properly. 1908 offset = RegEnd - RegBegin; 1909 1910 CCInfo.nextInRegsParam(); 1911 } 1912 1913 if (Flags.getByValSize() > 4*offset) { 1914 auto PtrVT = getPointerTy(DAG.getDataLayout()); 1915 unsigned LocMemOffset = VA.getLocMemOffset(); 1916 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1917 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff); 1918 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl); 1919 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset); 1920 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl, 1921 MVT::i32); 1922 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl, 1923 MVT::i32); 1924 1925 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 1926 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 1927 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 1928 Ops)); 1929 } 1930 } else if (!isSibCall) { 1931 assert(VA.isMemLoc()); 1932 1933 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1934 dl, DAG, VA, Flags)); 1935 } 1936 } 1937 1938 if (!MemOpChains.empty()) 1939 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 1940 1941 // Build a sequence of copy-to-reg nodes chained together with token chain 1942 // and flag operands which copy the outgoing args into the appropriate regs. 1943 SDValue InFlag; 1944 // Tail call byval lowering might overwrite argument registers so in case of 1945 // tail call optimization the copies to registers are lowered later. 1946 if (!isTailCall) 1947 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1948 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1949 RegsToPass[i].second, InFlag); 1950 InFlag = Chain.getValue(1); 1951 } 1952 1953 // For tail calls lower the arguments to the 'real' stack slot. 1954 if (isTailCall) { 1955 // Force all the incoming stack arguments to be loaded from the stack 1956 // before any new outgoing arguments are stored to the stack, because the 1957 // outgoing stack slots may alias the incoming argument stack slots, and 1958 // the alias isn't otherwise explicit. This is slightly more conservative 1959 // than necessary, because it means that each store effectively depends 1960 // on every argument instead of just those arguments it would clobber. 1961 1962 // Do not flag preceding copytoreg stuff together with the following stuff. 1963 InFlag = SDValue(); 1964 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1965 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1966 RegsToPass[i].second, InFlag); 1967 InFlag = Chain.getValue(1); 1968 } 1969 InFlag = SDValue(); 1970 } 1971 1972 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1973 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1974 // node so that legalize doesn't hack it. 1975 bool isDirect = false; 1976 1977 const TargetMachine &TM = getTargetMachine(); 1978 const Module *Mod = MF.getFunction().getParent(); 1979 const GlobalValue *GV = nullptr; 1980 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 1981 GV = G->getGlobal(); 1982 bool isStub = 1983 !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO(); 1984 1985 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1986 bool isLocalARMFunc = false; 1987 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1988 auto PtrVt = getPointerTy(DAG.getDataLayout()); 1989 1990 if (Subtarget->genLongCalls()) { 1991 assert((!isPositionIndependent() || Subtarget->isTargetWindows()) && 1992 "long-calls codegen is not position independent!"); 1993 // Handle a global address or an external symbol. If it's not one of 1994 // those, the target's already in a register, so we don't need to do 1995 // anything extra. 1996 if (isa<GlobalAddressSDNode>(Callee)) { 1997 // Create a constant pool entry for the callee address 1998 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1999 ARMConstantPoolValue *CPV = 2000 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 2001 2002 // Get the address of the callee into a register 2003 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2004 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2005 Callee = DAG.getLoad( 2006 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2007 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2008 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 2009 const char *Sym = S->getSymbol(); 2010 2011 // Create a constant pool entry for the callee address 2012 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2013 ARMConstantPoolValue *CPV = 2014 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 2015 ARMPCLabelIndex, 0); 2016 // Get the address of the callee into a register 2017 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2018 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2019 Callee = DAG.getLoad( 2020 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2021 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2022 } 2023 } else if (isa<GlobalAddressSDNode>(Callee)) { 2024 // If we're optimizing for minimum size and the function is called three or 2025 // more times in this block, we can improve codesize by calling indirectly 2026 // as BLXr has a 16-bit encoding. 2027 auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal(); 2028 auto *BB = CLI.CS.getParent(); 2029 bool PreferIndirect = 2030 Subtarget->isThumb() && MF.getFunction().optForMinSize() && 2031 count_if(GV->users(), [&BB](const User *U) { 2032 return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB; 2033 }) > 2; 2034 2035 if (!PreferIndirect) { 2036 isDirect = true; 2037 bool isDef = GV->isStrongDefinitionForLinker(); 2038 2039 // ARM call to a local ARM function is predicable. 2040 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking); 2041 // tBX takes a register source operand. 2042 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 2043 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?"); 2044 Callee = DAG.getNode( 2045 ARMISD::WrapperPIC, dl, PtrVt, 2046 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY)); 2047 Callee = DAG.getLoad( 2048 PtrVt, dl, DAG.getEntryNode(), Callee, 2049 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2050 /* Alignment = */ 0, MachineMemOperand::MODereferenceable | 2051 MachineMemOperand::MOInvariant); 2052 } else if (Subtarget->isTargetCOFF()) { 2053 assert(Subtarget->isTargetWindows() && 2054 "Windows is the only supported COFF target"); 2055 unsigned TargetFlags = GV->hasDLLImportStorageClass() 2056 ? ARMII::MO_DLLIMPORT 2057 : ARMII::MO_NO_FLAG; 2058 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, 2059 TargetFlags); 2060 if (GV->hasDLLImportStorageClass()) 2061 Callee = 2062 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), 2063 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee), 2064 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 2065 } else { 2066 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0); 2067 } 2068 } 2069 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 2070 isDirect = true; 2071 // tBX takes a register source operand. 2072 const char *Sym = S->getSymbol(); 2073 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 2074 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2075 ARMConstantPoolValue *CPV = 2076 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 2077 ARMPCLabelIndex, 4); 2078 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 2079 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2080 Callee = DAG.getLoad( 2081 PtrVt, dl, DAG.getEntryNode(), CPAddr, 2082 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2083 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2084 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel); 2085 } else { 2086 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0); 2087 } 2088 } 2089 2090 // FIXME: handle tail calls differently. 2091 unsigned CallOpc; 2092 if (Subtarget->isThumb()) { 2093 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 2094 CallOpc = ARMISD::CALL_NOLINK; 2095 else 2096 CallOpc = ARMISD::CALL; 2097 } else { 2098 if (!isDirect && !Subtarget->hasV5TOps()) 2099 CallOpc = ARMISD::CALL_NOLINK; 2100 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() && 2101 // Emit regular call when code size is the priority 2102 !MF.getFunction().optForMinSize()) 2103 // "mov lr, pc; b _foo" to avoid confusing the RSP 2104 CallOpc = ARMISD::CALL_NOLINK; 2105 else 2106 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 2107 } 2108 2109 std::vector<SDValue> Ops; 2110 Ops.push_back(Chain); 2111 Ops.push_back(Callee); 2112 2113 // Add argument registers to the end of the list so that they are known live 2114 // into the call. 2115 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 2116 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 2117 RegsToPass[i].second.getValueType())); 2118 2119 // Add a register mask operand representing the call-preserved registers. 2120 if (!isTailCall) { 2121 const uint32_t *Mask; 2122 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo(); 2123 if (isThisReturn) { 2124 // For 'this' returns, use the R0-preserving mask if applicable 2125 Mask = ARI->getThisReturnPreservedMask(MF, CallConv); 2126 if (!Mask) { 2127 // Set isThisReturn to false if the calling convention is not one that 2128 // allows 'returned' to be modeled in this way, so LowerCallResult does 2129 // not try to pass 'this' straight through 2130 isThisReturn = false; 2131 Mask = ARI->getCallPreservedMask(MF, CallConv); 2132 } 2133 } else 2134 Mask = ARI->getCallPreservedMask(MF, CallConv); 2135 2136 assert(Mask && "Missing call preserved mask for calling convention"); 2137 Ops.push_back(DAG.getRegisterMask(Mask)); 2138 } 2139 2140 if (InFlag.getNode()) 2141 Ops.push_back(InFlag); 2142 2143 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2144 if (isTailCall) { 2145 MF.getFrameInfo().setHasTailCall(); 2146 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops); 2147 } 2148 2149 // Returns a chain and a flag for retval copy to use. 2150 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 2151 InFlag = Chain.getValue(1); 2152 2153 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 2154 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 2155 if (!Ins.empty()) 2156 InFlag = Chain.getValue(1); 2157 2158 // Handle result values, copying them out of physregs into vregs that we 2159 // return. 2160 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 2161 InVals, isThisReturn, 2162 isThisReturn ? OutVals[0] : SDValue()); 2163 } 2164 2165 /// HandleByVal - Every parameter *after* a byval parameter is passed 2166 /// on the stack. Remember the next parameter register to allocate, 2167 /// and then confiscate the rest of the parameter registers to insure 2168 /// this. 2169 void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size, 2170 unsigned Align) const { 2171 // Byval (as with any stack) slots are always at least 4 byte aligned. 2172 Align = std::max(Align, 4U); 2173 2174 unsigned Reg = State->AllocateReg(GPRArgRegs); 2175 if (!Reg) 2176 return; 2177 2178 unsigned AlignInRegs = Align / 4; 2179 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs; 2180 for (unsigned i = 0; i < Waste; ++i) 2181 Reg = State->AllocateReg(GPRArgRegs); 2182 2183 if (!Reg) 2184 return; 2185 2186 unsigned Excess = 4 * (ARM::R4 - Reg); 2187 2188 // Special case when NSAA != SP and parameter size greater than size of 2189 // all remained GPR regs. In that case we can't split parameter, we must 2190 // send it to stack. We also must set NCRN to R4, so waste all 2191 // remained registers. 2192 const unsigned NSAAOffset = State->getNextStackOffset(); 2193 if (NSAAOffset != 0 && Size > Excess) { 2194 while (State->AllocateReg(GPRArgRegs)) 2195 ; 2196 return; 2197 } 2198 2199 // First register for byval parameter is the first register that wasn't 2200 // allocated before this method call, so it would be "reg". 2201 // If parameter is small enough to be saved in range [reg, r4), then 2202 // the end (first after last) register would be reg + param-size-in-regs, 2203 // else parameter would be splitted between registers and stack, 2204 // end register would be r4 in this case. 2205 unsigned ByValRegBegin = Reg; 2206 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4); 2207 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 2208 // Note, first register is allocated in the beginning of function already, 2209 // allocate remained amount of registers we need. 2210 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i) 2211 State->AllocateReg(GPRArgRegs); 2212 // A byval parameter that is split between registers and memory needs its 2213 // size truncated here. 2214 // In the case where the entire structure fits in registers, we set the 2215 // size in memory to zero. 2216 Size = std::max<int>(Size - Excess, 0); 2217 } 2218 2219 /// MatchingStackOffset - Return true if the given stack call argument is 2220 /// already available in the same position (relatively) of the caller's 2221 /// incoming argument stack. 2222 static 2223 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 2224 MachineFrameInfo &MFI, const MachineRegisterInfo *MRI, 2225 const TargetInstrInfo *TII) { 2226 unsigned Bytes = Arg.getValueSizeInBits() / 8; 2227 int FI = std::numeric_limits<int>::max(); 2228 if (Arg.getOpcode() == ISD::CopyFromReg) { 2229 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 2230 if (!TargetRegisterInfo::isVirtualRegister(VR)) 2231 return false; 2232 MachineInstr *Def = MRI->getVRegDef(VR); 2233 if (!Def) 2234 return false; 2235 if (!Flags.isByVal()) { 2236 if (!TII->isLoadFromStackSlot(*Def, FI)) 2237 return false; 2238 } else { 2239 return false; 2240 } 2241 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 2242 if (Flags.isByVal()) 2243 // ByVal argument is passed in as a pointer but it's now being 2244 // dereferenced. e.g. 2245 // define @foo(%struct.X* %A) { 2246 // tail call @bar(%struct.X* byval %A) 2247 // } 2248 return false; 2249 SDValue Ptr = Ld->getBasePtr(); 2250 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 2251 if (!FINode) 2252 return false; 2253 FI = FINode->getIndex(); 2254 } else 2255 return false; 2256 2257 assert(FI != std::numeric_limits<int>::max()); 2258 if (!MFI.isFixedObjectIndex(FI)) 2259 return false; 2260 return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI); 2261 } 2262 2263 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 2264 /// for tail call optimization. Targets which want to do tail call 2265 /// optimization should implement this function. 2266 bool 2267 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 2268 CallingConv::ID CalleeCC, 2269 bool isVarArg, 2270 bool isCalleeStructRet, 2271 bool isCallerStructRet, 2272 const SmallVectorImpl<ISD::OutputArg> &Outs, 2273 const SmallVectorImpl<SDValue> &OutVals, 2274 const SmallVectorImpl<ISD::InputArg> &Ins, 2275 SelectionDAG& DAG) const { 2276 MachineFunction &MF = DAG.getMachineFunction(); 2277 const Function &CallerF = MF.getFunction(); 2278 CallingConv::ID CallerCC = CallerF.getCallingConv(); 2279 2280 assert(Subtarget->supportsTailCall()); 2281 2282 // Tail calls to function pointers cannot be optimized for Thumb1 if the args 2283 // to the call take up r0-r3. The reason is that there are no legal registers 2284 // left to hold the pointer to the function to be called. 2285 if (Subtarget->isThumb1Only() && Outs.size() >= 4 && 2286 !isa<GlobalAddressSDNode>(Callee.getNode())) 2287 return false; 2288 2289 // Look for obvious safe cases to perform tail call optimization that do not 2290 // require ABI changes. This is what gcc calls sibcall. 2291 2292 // Exception-handling functions need a special set of instructions to indicate 2293 // a return to the hardware. Tail-calling another function would probably 2294 // break this. 2295 if (CallerF.hasFnAttribute("interrupt")) 2296 return false; 2297 2298 // Also avoid sibcall optimization if either caller or callee uses struct 2299 // return semantics. 2300 if (isCalleeStructRet || isCallerStructRet) 2301 return false; 2302 2303 // Externally-defined functions with weak linkage should not be 2304 // tail-called on ARM when the OS does not support dynamic 2305 // pre-emption of symbols, as the AAELF spec requires normal calls 2306 // to undefined weak functions to be replaced with a NOP or jump to the 2307 // next instruction. The behaviour of branch instructions in this 2308 // situation (as used for tail calls) is implementation-defined, so we 2309 // cannot rely on the linker replacing the tail call with a return. 2310 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2311 const GlobalValue *GV = G->getGlobal(); 2312 const Triple &TT = getTargetMachine().getTargetTriple(); 2313 if (GV->hasExternalWeakLinkage() && 2314 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO())) 2315 return false; 2316 } 2317 2318 // Check that the call results are passed in the same way. 2319 LLVMContext &C = *DAG.getContext(); 2320 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins, 2321 CCAssignFnForReturn(CalleeCC, isVarArg), 2322 CCAssignFnForReturn(CallerCC, isVarArg))) 2323 return false; 2324 // The callee has to preserve all registers the caller needs to preserve. 2325 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2326 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 2327 if (CalleeCC != CallerCC) { 2328 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 2329 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 2330 return false; 2331 } 2332 2333 // If Caller's vararg or byval argument has been split between registers and 2334 // stack, do not perform tail call, since part of the argument is in caller's 2335 // local frame. 2336 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>(); 2337 if (AFI_Caller->getArgRegsSaveSize()) 2338 return false; 2339 2340 // If the callee takes no arguments then go on to check the results of the 2341 // call. 2342 if (!Outs.empty()) { 2343 // Check if stack adjustment is needed. For now, do not do this if any 2344 // argument is passed on the stack. 2345 SmallVector<CCValAssign, 16> ArgLocs; 2346 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C); 2347 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg)); 2348 if (CCInfo.getNextStackOffset()) { 2349 // Check if the arguments are already laid out in the right way as 2350 // the caller's fixed stack objects. 2351 MachineFrameInfo &MFI = MF.getFrameInfo(); 2352 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2353 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 2354 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2355 i != e; 2356 ++i, ++realArgIdx) { 2357 CCValAssign &VA = ArgLocs[i]; 2358 EVT RegVT = VA.getLocVT(); 2359 SDValue Arg = OutVals[realArgIdx]; 2360 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2361 if (VA.getLocInfo() == CCValAssign::Indirect) 2362 return false; 2363 if (VA.needsCustom()) { 2364 // f64 and vector types are split into multiple registers or 2365 // register/stack-slot combinations. The types will not match 2366 // the registers; give up on memory f64 refs until we figure 2367 // out what to do about this. 2368 if (!VA.isRegLoc()) 2369 return false; 2370 if (!ArgLocs[++i].isRegLoc()) 2371 return false; 2372 if (RegVT == MVT::v2f64) { 2373 if (!ArgLocs[++i].isRegLoc()) 2374 return false; 2375 if (!ArgLocs[++i].isRegLoc()) 2376 return false; 2377 } 2378 } else if (!VA.isRegLoc()) { 2379 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2380 MFI, MRI, TII)) 2381 return false; 2382 } 2383 } 2384 } 2385 2386 const MachineRegisterInfo &MRI = MF.getRegInfo(); 2387 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals)) 2388 return false; 2389 } 2390 2391 return true; 2392 } 2393 2394 bool 2395 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2396 MachineFunction &MF, bool isVarArg, 2397 const SmallVectorImpl<ISD::OutputArg> &Outs, 2398 LLVMContext &Context) const { 2399 SmallVector<CCValAssign, 16> RVLocs; 2400 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 2401 return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2402 } 2403 2404 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 2405 const SDLoc &DL, SelectionDAG &DAG) { 2406 const MachineFunction &MF = DAG.getMachineFunction(); 2407 const Function &F = MF.getFunction(); 2408 2409 StringRef IntKind = F.getFnAttribute("interrupt").getValueAsString(); 2410 2411 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset 2412 // version of the "preferred return address". These offsets affect the return 2413 // instruction if this is a return from PL1 without hypervisor extensions. 2414 // IRQ/FIQ: +4 "subs pc, lr, #4" 2415 // SWI: 0 "subs pc, lr, #0" 2416 // ABORT: +4 "subs pc, lr, #4" 2417 // UNDEF: +4/+2 "subs pc, lr, #0" 2418 // UNDEF varies depending on where the exception came from ARM or Thumb 2419 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0. 2420 2421 int64_t LROffset; 2422 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" || 2423 IntKind == "ABORT") 2424 LROffset = 4; 2425 else if (IntKind == "SWI" || IntKind == "UNDEF") 2426 LROffset = 0; 2427 else 2428 report_fatal_error("Unsupported interrupt attribute. If present, value " 2429 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF"); 2430 2431 RetOps.insert(RetOps.begin() + 1, 2432 DAG.getConstant(LROffset, DL, MVT::i32, false)); 2433 2434 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps); 2435 } 2436 2437 SDValue 2438 ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 2439 bool isVarArg, 2440 const SmallVectorImpl<ISD::OutputArg> &Outs, 2441 const SmallVectorImpl<SDValue> &OutVals, 2442 const SDLoc &dl, SelectionDAG &DAG) const { 2443 // CCValAssign - represent the assignment of the return value to a location. 2444 SmallVector<CCValAssign, 16> RVLocs; 2445 2446 // CCState - Info about the registers and stack slots. 2447 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2448 *DAG.getContext()); 2449 2450 // Analyze outgoing return values. 2451 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2452 2453 SDValue Flag; 2454 SmallVector<SDValue, 4> RetOps; 2455 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2456 bool isLittleEndian = Subtarget->isLittle(); 2457 2458 MachineFunction &MF = DAG.getMachineFunction(); 2459 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2460 AFI->setReturnRegsCount(RVLocs.size()); 2461 2462 // Copy the result values into the output registers. 2463 for (unsigned i = 0, realRVLocIdx = 0; 2464 i != RVLocs.size(); 2465 ++i, ++realRVLocIdx) { 2466 CCValAssign &VA = RVLocs[i]; 2467 assert(VA.isRegLoc() && "Can only return in registers!"); 2468 2469 SDValue Arg = OutVals[realRVLocIdx]; 2470 2471 switch (VA.getLocInfo()) { 2472 default: llvm_unreachable("Unknown loc info!"); 2473 case CCValAssign::Full: break; 2474 case CCValAssign::BCvt: 2475 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2476 break; 2477 } 2478 2479 if (VA.needsCustom()) { 2480 if (VA.getLocVT() == MVT::v2f64) { 2481 // Extract the first half and return it in two registers. 2482 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2483 DAG.getConstant(0, dl, MVT::i32)); 2484 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2485 DAG.getVTList(MVT::i32, MVT::i32), Half); 2486 2487 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2488 HalfGPRs.getValue(isLittleEndian ? 0 : 1), 2489 Flag); 2490 Flag = Chain.getValue(1); 2491 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2492 VA = RVLocs[++i]; // skip ahead to next loc 2493 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2494 HalfGPRs.getValue(isLittleEndian ? 1 : 0), 2495 Flag); 2496 Flag = Chain.getValue(1); 2497 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2498 VA = RVLocs[++i]; // skip ahead to next loc 2499 2500 // Extract the 2nd half and fall through to handle it as an f64 value. 2501 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2502 DAG.getConstant(1, dl, MVT::i32)); 2503 } 2504 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2505 // available. 2506 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2507 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2508 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2509 fmrrd.getValue(isLittleEndian ? 0 : 1), 2510 Flag); 2511 Flag = Chain.getValue(1); 2512 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2513 VA = RVLocs[++i]; // skip ahead to next loc 2514 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2515 fmrrd.getValue(isLittleEndian ? 1 : 0), 2516 Flag); 2517 } else 2518 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2519 2520 // Guarantee that all emitted copies are 2521 // stuck together, avoiding something bad. 2522 Flag = Chain.getValue(1); 2523 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2524 } 2525 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2526 const MCPhysReg *I = 2527 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 2528 if (I) { 2529 for (; *I; ++I) { 2530 if (ARM::GPRRegClass.contains(*I)) 2531 RetOps.push_back(DAG.getRegister(*I, MVT::i32)); 2532 else if (ARM::DPRRegClass.contains(*I)) 2533 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64))); 2534 else 2535 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 2536 } 2537 } 2538 2539 // Update chain and glue. 2540 RetOps[0] = Chain; 2541 if (Flag.getNode()) 2542 RetOps.push_back(Flag); 2543 2544 // CPUs which aren't M-class use a special sequence to return from 2545 // exceptions (roughly, any instruction setting pc and cpsr simultaneously, 2546 // though we use "subs pc, lr, #N"). 2547 // 2548 // M-class CPUs actually use a normal return sequence with a special 2549 // (hardware-provided) value in LR, so the normal code path works. 2550 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt") && 2551 !Subtarget->isMClass()) { 2552 if (Subtarget->isThumb1Only()) 2553 report_fatal_error("interrupt attribute is not supported in Thumb1"); 2554 return LowerInterruptReturn(RetOps, dl, DAG); 2555 } 2556 2557 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps); 2558 } 2559 2560 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2561 if (N->getNumValues() != 1) 2562 return false; 2563 if (!N->hasNUsesOfValue(1, 0)) 2564 return false; 2565 2566 SDValue TCChain = Chain; 2567 SDNode *Copy = *N->use_begin(); 2568 if (Copy->getOpcode() == ISD::CopyToReg) { 2569 // If the copy has a glue operand, we conservatively assume it isn't safe to 2570 // perform a tail call. 2571 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2572 return false; 2573 TCChain = Copy->getOperand(0); 2574 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2575 SDNode *VMov = Copy; 2576 // f64 returned in a pair of GPRs. 2577 SmallPtrSet<SDNode*, 2> Copies; 2578 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2579 UI != UE; ++UI) { 2580 if (UI->getOpcode() != ISD::CopyToReg) 2581 return false; 2582 Copies.insert(*UI); 2583 } 2584 if (Copies.size() > 2) 2585 return false; 2586 2587 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2588 UI != UE; ++UI) { 2589 SDValue UseChain = UI->getOperand(0); 2590 if (Copies.count(UseChain.getNode())) 2591 // Second CopyToReg 2592 Copy = *UI; 2593 else { 2594 // We are at the top of this chain. 2595 // If the copy has a glue operand, we conservatively assume it 2596 // isn't safe to perform a tail call. 2597 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue) 2598 return false; 2599 // First CopyToReg 2600 TCChain = UseChain; 2601 } 2602 } 2603 } else if (Copy->getOpcode() == ISD::BITCAST) { 2604 // f32 returned in a single GPR. 2605 if (!Copy->hasOneUse()) 2606 return false; 2607 Copy = *Copy->use_begin(); 2608 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2609 return false; 2610 // If the copy has a glue operand, we conservatively assume it isn't safe to 2611 // perform a tail call. 2612 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2613 return false; 2614 TCChain = Copy->getOperand(0); 2615 } else { 2616 return false; 2617 } 2618 2619 bool HasRet = false; 2620 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2621 UI != UE; ++UI) { 2622 if (UI->getOpcode() != ARMISD::RET_FLAG && 2623 UI->getOpcode() != ARMISD::INTRET_FLAG) 2624 return false; 2625 HasRet = true; 2626 } 2627 2628 if (!HasRet) 2629 return false; 2630 2631 Chain = TCChain; 2632 return true; 2633 } 2634 2635 bool ARMTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { 2636 if (!Subtarget->supportsTailCall()) 2637 return false; 2638 2639 auto Attr = 2640 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls"); 2641 if (!CI->isTailCall() || Attr.getValueAsString() == "true") 2642 return false; 2643 2644 return true; 2645 } 2646 2647 // Trying to write a 64 bit value so need to split into two 32 bit values first, 2648 // and pass the lower and high parts through. 2649 static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) { 2650 SDLoc DL(Op); 2651 SDValue WriteValue = Op->getOperand(2); 2652 2653 // This function is only supposed to be called for i64 type argument. 2654 assert(WriteValue.getValueType() == MVT::i64 2655 && "LowerWRITE_REGISTER called for non-i64 type argument."); 2656 2657 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2658 DAG.getConstant(0, DL, MVT::i32)); 2659 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2660 DAG.getConstant(1, DL, MVT::i32)); 2661 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi }; 2662 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops); 2663 } 2664 2665 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2666 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2667 // one of the above mentioned nodes. It has to be wrapped because otherwise 2668 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2669 // be used to form addressing mode. These wrapped nodes will be selected 2670 // into MOVi. 2671 SDValue ARMTargetLowering::LowerConstantPool(SDValue Op, 2672 SelectionDAG &DAG) const { 2673 EVT PtrVT = Op.getValueType(); 2674 // FIXME there is no actual debug info here 2675 SDLoc dl(Op); 2676 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2677 SDValue Res; 2678 2679 // When generating execute-only code Constant Pools must be promoted to the 2680 // global data section. It's a bit ugly that we can't share them across basic 2681 // blocks, but this way we guarantee that execute-only behaves correct with 2682 // position-independent addressing modes. 2683 if (Subtarget->genExecuteOnly()) { 2684 auto AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 2685 auto T = const_cast<Type*>(CP->getType()); 2686 auto C = const_cast<Constant*>(CP->getConstVal()); 2687 auto M = const_cast<Module*>(DAG.getMachineFunction(). 2688 getFunction().getParent()); 2689 auto GV = new GlobalVariable( 2690 *M, T, /*isConst=*/true, GlobalVariable::InternalLinkage, C, 2691 Twine(DAG.getDataLayout().getPrivateGlobalPrefix()) + "CP" + 2692 Twine(DAG.getMachineFunction().getFunctionNumber()) + "_" + 2693 Twine(AFI->createPICLabelUId()) 2694 ); 2695 SDValue GA = DAG.getTargetGlobalAddress(dyn_cast<GlobalValue>(GV), 2696 dl, PtrVT); 2697 return LowerGlobalAddress(GA, DAG); 2698 } 2699 2700 if (CP->isMachineConstantPoolEntry()) 2701 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2702 CP->getAlignment()); 2703 else 2704 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2705 CP->getAlignment()); 2706 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2707 } 2708 2709 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2710 return MachineJumpTableInfo::EK_Inline; 2711 } 2712 2713 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2714 SelectionDAG &DAG) const { 2715 MachineFunction &MF = DAG.getMachineFunction(); 2716 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2717 unsigned ARMPCLabelIndex = 0; 2718 SDLoc DL(Op); 2719 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2720 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2721 SDValue CPAddr; 2722 bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI(); 2723 if (!IsPositionIndependent) { 2724 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2725 } else { 2726 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2727 ARMPCLabelIndex = AFI->createPICLabelUId(); 2728 ARMConstantPoolValue *CPV = 2729 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2730 ARMCP::CPBlockAddress, PCAdj); 2731 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2732 } 2733 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2734 SDValue Result = DAG.getLoad( 2735 PtrVT, DL, DAG.getEntryNode(), CPAddr, 2736 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2737 if (!IsPositionIndependent) 2738 return Result; 2739 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32); 2740 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2741 } 2742 2743 /// \brief Convert a TLS address reference into the correct sequence of loads 2744 /// and calls to compute the variable's address for Darwin, and return an 2745 /// SDValue containing the final node. 2746 2747 /// Darwin only has one TLS scheme which must be capable of dealing with the 2748 /// fully general situation, in the worst case. This means: 2749 /// + "extern __thread" declaration. 2750 /// + Defined in a possibly unknown dynamic library. 2751 /// 2752 /// The general system is that each __thread variable has a [3 x i32] descriptor 2753 /// which contains information used by the runtime to calculate the address. The 2754 /// only part of this the compiler needs to know about is the first word, which 2755 /// contains a function pointer that must be called with the address of the 2756 /// entire descriptor in "r0". 2757 /// 2758 /// Since this descriptor may be in a different unit, in general access must 2759 /// proceed along the usual ARM rules. A common sequence to produce is: 2760 /// 2761 /// movw rT1, :lower16:_var$non_lazy_ptr 2762 /// movt rT1, :upper16:_var$non_lazy_ptr 2763 /// ldr r0, [rT1] 2764 /// ldr rT2, [r0] 2765 /// blx rT2 2766 /// [...address now in r0...] 2767 SDValue 2768 ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op, 2769 SelectionDAG &DAG) const { 2770 assert(Subtarget->isTargetDarwin() && 2771 "This function expects a Darwin target"); 2772 SDLoc DL(Op); 2773 2774 // First step is to get the address of the actua global symbol. This is where 2775 // the TLS descriptor lives. 2776 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG); 2777 2778 // The first entry in the descriptor is a function pointer that we must call 2779 // to obtain the address of the variable. 2780 SDValue Chain = DAG.getEntryNode(); 2781 SDValue FuncTLVGet = DAG.getLoad( 2782 MVT::i32, DL, Chain, DescAddr, 2783 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2784 /* Alignment = */ 4, 2785 MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable | 2786 MachineMemOperand::MOInvariant); 2787 Chain = FuncTLVGet.getValue(1); 2788 2789 MachineFunction &F = DAG.getMachineFunction(); 2790 MachineFrameInfo &MFI = F.getFrameInfo(); 2791 MFI.setAdjustsStack(true); 2792 2793 // TLS calls preserve all registers except those that absolutely must be 2794 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be 2795 // silly). 2796 auto TRI = 2797 getTargetMachine().getSubtargetImpl(F.getFunction())->getRegisterInfo(); 2798 auto ARI = static_cast<const ARMRegisterInfo *>(TRI); 2799 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction()); 2800 2801 // Finally, we can make the call. This is just a degenerate version of a 2802 // normal AArch64 call node: r0 takes the address of the descriptor, and 2803 // returns the address of the variable in this thread. 2804 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue()); 2805 Chain = 2806 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue), 2807 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32), 2808 DAG.getRegisterMask(Mask), Chain.getValue(1)); 2809 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1)); 2810 } 2811 2812 SDValue 2813 ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op, 2814 SelectionDAG &DAG) const { 2815 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering"); 2816 2817 SDValue Chain = DAG.getEntryNode(); 2818 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2819 SDLoc DL(Op); 2820 2821 // Load the current TEB (thread environment block) 2822 SDValue Ops[] = {Chain, 2823 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 2824 DAG.getConstant(15, DL, MVT::i32), 2825 DAG.getConstant(0, DL, MVT::i32), 2826 DAG.getConstant(13, DL, MVT::i32), 2827 DAG.getConstant(0, DL, MVT::i32), 2828 DAG.getConstant(2, DL, MVT::i32)}; 2829 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 2830 DAG.getVTList(MVT::i32, MVT::Other), Ops); 2831 2832 SDValue TEB = CurrentTEB.getValue(0); 2833 Chain = CurrentTEB.getValue(1); 2834 2835 // Load the ThreadLocalStoragePointer from the TEB 2836 // A pointer to the TLS array is located at offset 0x2c from the TEB. 2837 SDValue TLSArray = 2838 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL)); 2839 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo()); 2840 2841 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4 2842 // offset into the TLSArray. 2843 2844 // Load the TLS index from the C runtime 2845 SDValue TLSIndex = 2846 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG); 2847 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex); 2848 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo()); 2849 2850 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex, 2851 DAG.getConstant(2, DL, MVT::i32)); 2852 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain, 2853 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot), 2854 MachinePointerInfo()); 2855 2856 // Get the offset of the start of the .tls section (section base) 2857 const auto *GA = cast<GlobalAddressSDNode>(Op); 2858 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL); 2859 SDValue Offset = DAG.getLoad( 2860 PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32, 2861 DAG.getTargetConstantPool(CPV, PtrVT, 4)), 2862 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2863 2864 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset); 2865 } 2866 2867 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2868 SDValue 2869 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2870 SelectionDAG &DAG) const { 2871 SDLoc dl(GA); 2872 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2873 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2874 MachineFunction &MF = DAG.getMachineFunction(); 2875 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2876 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2877 ARMConstantPoolValue *CPV = 2878 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2879 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2880 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2881 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2882 Argument = DAG.getLoad( 2883 PtrVT, dl, DAG.getEntryNode(), Argument, 2884 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2885 SDValue Chain = Argument.getValue(1); 2886 2887 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2888 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2889 2890 // call __tls_get_addr. 2891 ArgListTy Args; 2892 ArgListEntry Entry; 2893 Entry.Node = Argument; 2894 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2895 Args.push_back(Entry); 2896 2897 // FIXME: is there useful debug info available here? 2898 TargetLowering::CallLoweringInfo CLI(DAG); 2899 CLI.setDebugLoc(dl).setChain(Chain).setLibCallee( 2900 CallingConv::C, Type::getInt32Ty(*DAG.getContext()), 2901 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args)); 2902 2903 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2904 return CallResult.first; 2905 } 2906 2907 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2908 // "local exec" model. 2909 SDValue 2910 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2911 SelectionDAG &DAG, 2912 TLSModel::Model model) const { 2913 const GlobalValue *GV = GA->getGlobal(); 2914 SDLoc dl(GA); 2915 SDValue Offset; 2916 SDValue Chain = DAG.getEntryNode(); 2917 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2918 // Get the Thread Pointer 2919 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2920 2921 if (model == TLSModel::InitialExec) { 2922 MachineFunction &MF = DAG.getMachineFunction(); 2923 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2924 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2925 // Initial exec model. 2926 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2927 ARMConstantPoolValue *CPV = 2928 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2929 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2930 true); 2931 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2932 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2933 Offset = DAG.getLoad( 2934 PtrVT, dl, Chain, Offset, 2935 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2936 Chain = Offset.getValue(1); 2937 2938 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2939 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2940 2941 Offset = DAG.getLoad( 2942 PtrVT, dl, Chain, Offset, 2943 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2944 } else { 2945 // local exec model 2946 assert(model == TLSModel::LocalExec); 2947 ARMConstantPoolValue *CPV = 2948 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2949 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2950 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2951 Offset = DAG.getLoad( 2952 PtrVT, dl, Chain, Offset, 2953 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 2954 } 2955 2956 // The address of the thread local variable is the add of the thread 2957 // pointer with the offset of the variable. 2958 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2959 } 2960 2961 SDValue 2962 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2963 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2964 if (DAG.getTarget().Options.EmulatedTLS) 2965 return LowerToTLSEmulatedModel(GA, DAG); 2966 2967 if (Subtarget->isTargetDarwin()) 2968 return LowerGlobalTLSAddressDarwin(Op, DAG); 2969 2970 if (Subtarget->isTargetWindows()) 2971 return LowerGlobalTLSAddressWindows(Op, DAG); 2972 2973 // TODO: implement the "local dynamic" model 2974 assert(Subtarget->isTargetELF() && "Only ELF implemented here"); 2975 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 2976 2977 switch (model) { 2978 case TLSModel::GeneralDynamic: 2979 case TLSModel::LocalDynamic: 2980 return LowerToTLSGeneralDynamicModel(GA, DAG); 2981 case TLSModel::InitialExec: 2982 case TLSModel::LocalExec: 2983 return LowerToTLSExecModels(GA, DAG, model); 2984 } 2985 llvm_unreachable("bogus TLS model"); 2986 } 2987 2988 /// Return true if all users of V are within function F, looking through 2989 /// ConstantExprs. 2990 static bool allUsersAreInFunction(const Value *V, const Function *F) { 2991 SmallVector<const User*,4> Worklist; 2992 for (auto *U : V->users()) 2993 Worklist.push_back(U); 2994 while (!Worklist.empty()) { 2995 auto *U = Worklist.pop_back_val(); 2996 if (isa<ConstantExpr>(U)) { 2997 for (auto *UU : U->users()) 2998 Worklist.push_back(UU); 2999 continue; 3000 } 3001 3002 auto *I = dyn_cast<Instruction>(U); 3003 if (!I || I->getParent()->getParent() != F) 3004 return false; 3005 } 3006 return true; 3007 } 3008 3009 /// Return true if all users of V are within some (any) function, looking through 3010 /// ConstantExprs. In other words, are there any global constant users? 3011 static bool allUsersAreInFunctions(const Value *V) { 3012 SmallVector<const User*,4> Worklist; 3013 for (auto *U : V->users()) 3014 Worklist.push_back(U); 3015 while (!Worklist.empty()) { 3016 auto *U = Worklist.pop_back_val(); 3017 if (isa<ConstantExpr>(U)) { 3018 for (auto *UU : U->users()) 3019 Worklist.push_back(UU); 3020 continue; 3021 } 3022 3023 if (!isa<Instruction>(U)) 3024 return false; 3025 } 3026 return true; 3027 } 3028 3029 // Return true if T is an integer, float or an array/vector of either. 3030 static bool isSimpleType(Type *T) { 3031 if (T->isIntegerTy() || T->isFloatingPointTy()) 3032 return true; 3033 Type *SubT = nullptr; 3034 if (T->isArrayTy()) 3035 SubT = T->getArrayElementType(); 3036 else if (T->isVectorTy()) 3037 SubT = T->getVectorElementType(); 3038 else 3039 return false; 3040 return SubT->isIntegerTy() || SubT->isFloatingPointTy(); 3041 } 3042 3043 static SDValue promoteToConstantPool(const GlobalValue *GV, SelectionDAG &DAG, 3044 EVT PtrVT, const SDLoc &dl) { 3045 // If we're creating a pool entry for a constant global with unnamed address, 3046 // and the global is small enough, we can emit it inline into the constant pool 3047 // to save ourselves an indirection. 3048 // 3049 // This is a win if the constant is only used in one function (so it doesn't 3050 // need to be duplicated) or duplicating the constant wouldn't increase code 3051 // size (implying the constant is no larger than 4 bytes). 3052 const Function &F = DAG.getMachineFunction().getFunction(); 3053 3054 // We rely on this decision to inline being idemopotent and unrelated to the 3055 // use-site. We know that if we inline a variable at one use site, we'll 3056 // inline it elsewhere too (and reuse the constant pool entry). Fast-isel 3057 // doesn't know about this optimization, so bail out if it's enabled else 3058 // we could decide to inline here (and thus never emit the GV) but require 3059 // the GV from fast-isel generated code. 3060 if (!EnableConstpoolPromotion || 3061 DAG.getMachineFunction().getTarget().Options.EnableFastISel) 3062 return SDValue(); 3063 3064 auto *GVar = dyn_cast<GlobalVariable>(GV); 3065 if (!GVar || !GVar->hasInitializer() || 3066 !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() || 3067 !GVar->hasLocalLinkage()) 3068 return SDValue(); 3069 3070 // Ensure that we don't try and inline any type that contains pointers. If 3071 // we inline a value that contains relocations, we move the relocations from 3072 // .data to .text which is not ideal. 3073 auto *Init = GVar->getInitializer(); 3074 if (!isSimpleType(Init->getType())) 3075 return SDValue(); 3076 3077 // The constant islands pass can only really deal with alignment requests 3078 // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote 3079 // any type wanting greater alignment requirements than 4 bytes. We also 3080 // can only promote constants that are multiples of 4 bytes in size or 3081 // are paddable to a multiple of 4. Currently we only try and pad constants 3082 // that are strings for simplicity. 3083 auto *CDAInit = dyn_cast<ConstantDataArray>(Init); 3084 unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType()); 3085 unsigned Align = GVar->getAlignment(); 3086 unsigned RequiredPadding = 4 - (Size % 4); 3087 bool PaddingPossible = 3088 RequiredPadding == 4 || (CDAInit && CDAInit->isString()); 3089 if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize || 3090 Size == 0) 3091 return SDValue(); 3092 3093 unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding); 3094 MachineFunction &MF = DAG.getMachineFunction(); 3095 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3096 3097 // We can't bloat the constant pool too much, else the ConstantIslands pass 3098 // may fail to converge. If we haven't promoted this global yet (it may have 3099 // multiple uses), and promoting it would increase the constant pool size (Sz 3100 // > 4), ensure we have space to do so up to MaxTotal. 3101 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4) 3102 if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >= 3103 ConstpoolPromotionMaxTotal) 3104 return SDValue(); 3105 3106 // This is only valid if all users are in a single function OR it has users 3107 // in multiple functions but it no larger than a pointer. We also check if 3108 // GVar has constant (non-ConstantExpr) users. If so, it essentially has its 3109 // address taken. 3110 if (!allUsersAreInFunction(GVar, &F) && 3111 !(Size <= 4 && allUsersAreInFunctions(GVar))) 3112 return SDValue(); 3113 3114 // We're going to inline this global. Pad it out if needed. 3115 if (RequiredPadding != 4) { 3116 StringRef S = CDAInit->getAsString(); 3117 3118 SmallVector<uint8_t,16> V(S.size()); 3119 std::copy(S.bytes_begin(), S.bytes_end(), V.begin()); 3120 while (RequiredPadding--) 3121 V.push_back(0); 3122 Init = ConstantDataArray::get(*DAG.getContext(), V); 3123 } 3124 3125 auto CPVal = ARMConstantPoolConstant::Create(GVar, Init); 3126 SDValue CPAddr = 3127 DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4); 3128 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) { 3129 AFI->markGlobalAsPromotedToConstantPool(GVar); 3130 AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() + 3131 PaddedSize - 4); 3132 } 3133 ++NumConstpoolPromoted; 3134 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3135 } 3136 3137 bool ARMTargetLowering::isReadOnly(const GlobalValue *GV) const { 3138 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 3139 GV = GA->getBaseObject(); 3140 return (isa<GlobalVariable>(GV) && cast<GlobalVariable>(GV)->isConstant()) || 3141 isa<Function>(GV); 3142 } 3143 3144 SDValue ARMTargetLowering::LowerGlobalAddress(SDValue Op, 3145 SelectionDAG &DAG) const { 3146 switch (Subtarget->getTargetTriple().getObjectFormat()) { 3147 default: llvm_unreachable("unknown object format"); 3148 case Triple::COFF: 3149 return LowerGlobalAddressWindows(Op, DAG); 3150 case Triple::ELF: 3151 return LowerGlobalAddressELF(Op, DAG); 3152 case Triple::MachO: 3153 return LowerGlobalAddressDarwin(Op, DAG); 3154 } 3155 } 3156 3157 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 3158 SelectionDAG &DAG) const { 3159 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3160 SDLoc dl(Op); 3161 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3162 const TargetMachine &TM = getTargetMachine(); 3163 bool IsRO = isReadOnly(GV); 3164 3165 // promoteToConstantPool only if not generating XO text section 3166 if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV) && !Subtarget->genExecuteOnly()) 3167 if (SDValue V = promoteToConstantPool(GV, DAG, PtrVT, dl)) 3168 return V; 3169 3170 if (isPositionIndependent()) { 3171 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); 3172 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 3173 UseGOT_PREL ? ARMII::MO_GOT : 0); 3174 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G); 3175 if (UseGOT_PREL) 3176 Result = 3177 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 3178 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3179 return Result; 3180 } else if (Subtarget->isROPI() && IsRO) { 3181 // PC-relative. 3182 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT); 3183 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G); 3184 return Result; 3185 } else if (Subtarget->isRWPI() && !IsRO) { 3186 // SB-relative. 3187 SDValue RelAddr; 3188 if (Subtarget->useMovt(DAG.getMachineFunction())) { 3189 ++NumMovwMovt; 3190 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_SBREL); 3191 RelAddr = DAG.getNode(ARMISD::Wrapper, dl, PtrVT, G); 3192 } else { // use literal pool for address constant 3193 ARMConstantPoolValue *CPV = 3194 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL); 3195 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3196 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3197 RelAddr = DAG.getLoad( 3198 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3199 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3200 } 3201 SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT); 3202 SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, RelAddr); 3203 return Result; 3204 } 3205 3206 // If we have T2 ops, we can materialize the address directly via movt/movw 3207 // pair. This is always cheaper. 3208 if (Subtarget->useMovt(DAG.getMachineFunction())) { 3209 ++NumMovwMovt; 3210 // FIXME: Once remat is capable of dealing with instructions with register 3211 // operands, expand this into two nodes. 3212 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 3213 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 3214 } else { 3215 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 3216 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3217 return DAG.getLoad( 3218 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3219 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3220 } 3221 } 3222 3223 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 3224 SelectionDAG &DAG) const { 3225 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 3226 "ROPI/RWPI not currently supported for Darwin"); 3227 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3228 SDLoc dl(Op); 3229 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3230 3231 if (Subtarget->useMovt(DAG.getMachineFunction())) 3232 ++NumMovwMovt; 3233 3234 // FIXME: Once remat is capable of dealing with instructions with register 3235 // operands, expand this into multiple nodes 3236 unsigned Wrapper = 3237 isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper; 3238 3239 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY); 3240 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G); 3241 3242 if (Subtarget->isGVIndirectSymbol(GV)) 3243 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 3244 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3245 return Result; 3246 } 3247 3248 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op, 3249 SelectionDAG &DAG) const { 3250 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported"); 3251 assert(Subtarget->useMovt(DAG.getMachineFunction()) && 3252 "Windows on ARM expects to use movw/movt"); 3253 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 3254 "ROPI/RWPI not currently supported for Windows"); 3255 3256 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3257 const ARMII::TOF TargetFlags = 3258 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG); 3259 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3260 SDValue Result; 3261 SDLoc DL(Op); 3262 3263 ++NumMovwMovt; 3264 3265 // FIXME: Once remat is capable of dealing with instructions with register 3266 // operands, expand this into two nodes. 3267 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, 3268 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0, 3269 TargetFlags)); 3270 if (GV->hasDLLImportStorageClass()) 3271 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 3272 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 3273 return Result; 3274 } 3275 3276 SDValue 3277 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 3278 SDLoc dl(Op); 3279 SDValue Val = DAG.getConstant(0, dl, MVT::i32); 3280 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 3281 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 3282 Op.getOperand(1), Val); 3283 } 3284 3285 SDValue 3286 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 3287 SDLoc dl(Op); 3288 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 3289 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32)); 3290 } 3291 3292 SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op, 3293 SelectionDAG &DAG) const { 3294 SDLoc dl(Op); 3295 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other, 3296 Op.getOperand(0)); 3297 } 3298 3299 SDValue 3300 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 3301 const ARMSubtarget *Subtarget) const { 3302 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3303 SDLoc dl(Op); 3304 switch (IntNo) { 3305 default: return SDValue(); // Don't custom lower most intrinsics. 3306 case Intrinsic::thread_pointer: { 3307 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3308 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 3309 } 3310 case Intrinsic::eh_sjlj_lsda: { 3311 MachineFunction &MF = DAG.getMachineFunction(); 3312 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3313 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 3314 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3315 SDValue CPAddr; 3316 bool IsPositionIndependent = isPositionIndependent(); 3317 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0; 3318 ARMConstantPoolValue *CPV = 3319 ARMConstantPoolConstant::Create(&MF.getFunction(), ARMPCLabelIndex, 3320 ARMCP::CPLSDA, PCAdj); 3321 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 3322 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 3323 SDValue Result = DAG.getLoad( 3324 PtrVT, dl, DAG.getEntryNode(), CPAddr, 3325 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 3326 3327 if (IsPositionIndependent) { 3328 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 3329 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 3330 } 3331 return Result; 3332 } 3333 case Intrinsic::arm_neon_vabs: 3334 return DAG.getNode(ISD::ABS, SDLoc(Op), Op.getValueType(), 3335 Op.getOperand(1)); 3336 case Intrinsic::arm_neon_vmulls: 3337 case Intrinsic::arm_neon_vmullu: { 3338 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 3339 ? ARMISD::VMULLs : ARMISD::VMULLu; 3340 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3341 Op.getOperand(1), Op.getOperand(2)); 3342 } 3343 case Intrinsic::arm_neon_vminnm: 3344 case Intrinsic::arm_neon_vmaxnm: { 3345 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm) 3346 ? ISD::FMINNUM : ISD::FMAXNUM; 3347 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3348 Op.getOperand(1), Op.getOperand(2)); 3349 } 3350 case Intrinsic::arm_neon_vminu: 3351 case Intrinsic::arm_neon_vmaxu: { 3352 if (Op.getValueType().isFloatingPoint()) 3353 return SDValue(); 3354 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu) 3355 ? ISD::UMIN : ISD::UMAX; 3356 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3357 Op.getOperand(1), Op.getOperand(2)); 3358 } 3359 case Intrinsic::arm_neon_vmins: 3360 case Intrinsic::arm_neon_vmaxs: { 3361 // v{min,max}s is overloaded between signed integers and floats. 3362 if (!Op.getValueType().isFloatingPoint()) { 3363 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 3364 ? ISD::SMIN : ISD::SMAX; 3365 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3366 Op.getOperand(1), Op.getOperand(2)); 3367 } 3368 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 3369 ? ISD::FMINNAN : ISD::FMAXNAN; 3370 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 3371 Op.getOperand(1), Op.getOperand(2)); 3372 } 3373 case Intrinsic::arm_neon_vtbl1: 3374 return DAG.getNode(ARMISD::VTBL1, SDLoc(Op), Op.getValueType(), 3375 Op.getOperand(1), Op.getOperand(2)); 3376 case Intrinsic::arm_neon_vtbl2: 3377 return DAG.getNode(ARMISD::VTBL2, SDLoc(Op), Op.getValueType(), 3378 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3379 } 3380 } 3381 3382 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 3383 const ARMSubtarget *Subtarget) { 3384 SDLoc dl(Op); 3385 ConstantSDNode *SSIDNode = cast<ConstantSDNode>(Op.getOperand(2)); 3386 auto SSID = static_cast<SyncScope::ID>(SSIDNode->getZExtValue()); 3387 if (SSID == SyncScope::SingleThread) 3388 return Op; 3389 3390 if (!Subtarget->hasDataBarrier()) { 3391 // Some ARMv6 cpus can support data barriers with an mcr instruction. 3392 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 3393 // here. 3394 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 3395 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!"); 3396 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 3397 DAG.getConstant(0, dl, MVT::i32)); 3398 } 3399 3400 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 3401 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 3402 ARM_MB::MemBOpt Domain = ARM_MB::ISH; 3403 if (Subtarget->isMClass()) { 3404 // Only a full system barrier exists in the M-class architectures. 3405 Domain = ARM_MB::SY; 3406 } else if (Subtarget->preferISHSTBarriers() && 3407 Ord == AtomicOrdering::Release) { 3408 // Swift happens to implement ISHST barriers in a way that's compatible with 3409 // Release semantics but weaker than ISH so we'd be fools not to use 3410 // it. Beware: other processors probably don't! 3411 Domain = ARM_MB::ISHST; 3412 } 3413 3414 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0), 3415 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32), 3416 DAG.getConstant(Domain, dl, MVT::i32)); 3417 } 3418 3419 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 3420 const ARMSubtarget *Subtarget) { 3421 // ARM pre v5TE and Thumb1 does not have preload instructions. 3422 if (!(Subtarget->isThumb2() || 3423 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 3424 // Just preserve the chain. 3425 return Op.getOperand(0); 3426 3427 SDLoc dl(Op); 3428 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 3429 if (!isRead && 3430 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 3431 // ARMv7 with MP extension has PLDW. 3432 return Op.getOperand(0); 3433 3434 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3435 if (Subtarget->isThumb()) { 3436 // Invert the bits. 3437 isRead = ~isRead & 1; 3438 isData = ~isData & 1; 3439 } 3440 3441 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 3442 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32), 3443 DAG.getConstant(isData, dl, MVT::i32)); 3444 } 3445 3446 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 3447 MachineFunction &MF = DAG.getMachineFunction(); 3448 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 3449 3450 // vastart just stores the address of the VarArgsFrameIndex slot into the 3451 // memory location argument. 3452 SDLoc dl(Op); 3453 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 3454 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3455 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 3456 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 3457 MachinePointerInfo(SV)); 3458 } 3459 3460 SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, 3461 CCValAssign &NextVA, 3462 SDValue &Root, 3463 SelectionDAG &DAG, 3464 const SDLoc &dl) const { 3465 MachineFunction &MF = DAG.getMachineFunction(); 3466 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3467 3468 const TargetRegisterClass *RC; 3469 if (AFI->isThumb1OnlyFunction()) 3470 RC = &ARM::tGPRRegClass; 3471 else 3472 RC = &ARM::GPRRegClass; 3473 3474 // Transform the arguments stored in physical registers into virtual ones. 3475 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3476 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3477 3478 SDValue ArgValue2; 3479 if (NextVA.isMemLoc()) { 3480 MachineFrameInfo &MFI = MF.getFrameInfo(); 3481 int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true); 3482 3483 // Create load node to retrieve arguments from the stack. 3484 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3485 ArgValue2 = DAG.getLoad( 3486 MVT::i32, dl, Root, FIN, 3487 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); 3488 } else { 3489 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 3490 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3491 } 3492 if (!Subtarget->isLittle()) 3493 std::swap (ArgValue, ArgValue2); 3494 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 3495 } 3496 3497 // The remaining GPRs hold either the beginning of variable-argument 3498 // data, or the beginning of an aggregate passed by value (usually 3499 // byval). Either way, we allocate stack slots adjacent to the data 3500 // provided by our caller, and store the unallocated registers there. 3501 // If this is a variadic function, the va_list pointer will begin with 3502 // these values; otherwise, this reassembles a (byval) structure that 3503 // was split between registers and memory. 3504 // Return: The frame index registers were stored into. 3505 int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 3506 const SDLoc &dl, SDValue &Chain, 3507 const Value *OrigArg, 3508 unsigned InRegsParamRecordIdx, 3509 int ArgOffset, unsigned ArgSize) const { 3510 // Currently, two use-cases possible: 3511 // Case #1. Non-var-args function, and we meet first byval parameter. 3512 // Setup first unallocated register as first byval register; 3513 // eat all remained registers 3514 // (these two actions are performed by HandleByVal method). 3515 // Then, here, we initialize stack frame with 3516 // "store-reg" instructions. 3517 // Case #2. Var-args function, that doesn't contain byval parameters. 3518 // The same: eat all remained unallocated registers, 3519 // initialize stack frame. 3520 3521 MachineFunction &MF = DAG.getMachineFunction(); 3522 MachineFrameInfo &MFI = MF.getFrameInfo(); 3523 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3524 unsigned RBegin, REnd; 3525 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 3526 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 3527 } else { 3528 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3529 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx]; 3530 REnd = ARM::R4; 3531 } 3532 3533 if (REnd != RBegin) 3534 ArgOffset = -4 * (ARM::R4 - RBegin); 3535 3536 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3537 int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false); 3538 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT); 3539 3540 SmallVector<SDValue, 4> MemOps; 3541 const TargetRegisterClass *RC = 3542 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 3543 3544 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) { 3545 unsigned VReg = MF.addLiveIn(Reg, RC); 3546 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 3547 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3548 MachinePointerInfo(OrigArg, 4 * i)); 3549 MemOps.push_back(Store); 3550 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT)); 3551 } 3552 3553 if (!MemOps.empty()) 3554 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3555 return FrameIndex; 3556 } 3557 3558 // Setup stack frame, the va_list pointer will start from. 3559 void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 3560 const SDLoc &dl, SDValue &Chain, 3561 unsigned ArgOffset, 3562 unsigned TotalArgRegsSaveSize, 3563 bool ForceMutable) const { 3564 MachineFunction &MF = DAG.getMachineFunction(); 3565 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3566 3567 // Try to store any remaining integer argument regs 3568 // to their spots on the stack so that they may be loaded by dereferencing 3569 // the result of va_next. 3570 // If there is no regs to be stored, just point address after last 3571 // argument passed via stack. 3572 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr, 3573 CCInfo.getInRegsParamsCount(), 3574 CCInfo.getNextStackOffset(), 4); 3575 AFI->setVarArgsFrameIndex(FrameIndex); 3576 } 3577 3578 SDValue ARMTargetLowering::LowerFormalArguments( 3579 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 3580 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 3581 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3582 MachineFunction &MF = DAG.getMachineFunction(); 3583 MachineFrameInfo &MFI = MF.getFrameInfo(); 3584 3585 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3586 3587 // Assign locations to all of the incoming arguments. 3588 SmallVector<CCValAssign, 16> ArgLocs; 3589 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 3590 *DAG.getContext()); 3591 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg)); 3592 3593 SmallVector<SDValue, 16> ArgValues; 3594 SDValue ArgValue; 3595 Function::const_arg_iterator CurOrigArg = MF.getFunction().arg_begin(); 3596 unsigned CurArgIdx = 0; 3597 3598 // Initially ArgRegsSaveSize is zero. 3599 // Then we increase this value each time we meet byval parameter. 3600 // We also increase this value in case of varargs function. 3601 AFI->setArgRegsSaveSize(0); 3602 3603 // Calculate the amount of stack space that we need to allocate to store 3604 // byval and variadic arguments that are passed in registers. 3605 // We need to know this before we allocate the first byval or variadic 3606 // argument, as they will be allocated a stack slot below the CFA (Canonical 3607 // Frame Address, the stack pointer at entry to the function). 3608 unsigned ArgRegBegin = ARM::R4; 3609 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3610 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount()) 3611 break; 3612 3613 CCValAssign &VA = ArgLocs[i]; 3614 unsigned Index = VA.getValNo(); 3615 ISD::ArgFlagsTy Flags = Ins[Index].Flags; 3616 if (!Flags.isByVal()) 3617 continue; 3618 3619 assert(VA.isMemLoc() && "unexpected byval pointer in reg"); 3620 unsigned RBegin, REnd; 3621 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd); 3622 ArgRegBegin = std::min(ArgRegBegin, RBegin); 3623 3624 CCInfo.nextInRegsParam(); 3625 } 3626 CCInfo.rewindByValRegsInfo(); 3627 3628 int lastInsIndex = -1; 3629 if (isVarArg && MFI.hasVAStart()) { 3630 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3631 if (RegIdx != array_lengthof(GPRArgRegs)) 3632 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]); 3633 } 3634 3635 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin); 3636 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize); 3637 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3638 3639 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3640 CCValAssign &VA = ArgLocs[i]; 3641 if (Ins[VA.getValNo()].isOrigArg()) { 3642 std::advance(CurOrigArg, 3643 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx); 3644 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex(); 3645 } 3646 // Arguments stored in registers. 3647 if (VA.isRegLoc()) { 3648 EVT RegVT = VA.getLocVT(); 3649 3650 if (VA.needsCustom()) { 3651 // f64 and vector types are split up into multiple registers or 3652 // combinations of registers and stack slots. 3653 if (VA.getLocVT() == MVT::v2f64) { 3654 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 3655 Chain, DAG, dl); 3656 VA = ArgLocs[++i]; // skip ahead to next loc 3657 SDValue ArgValue2; 3658 if (VA.isMemLoc()) { 3659 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true); 3660 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3661 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 3662 MachinePointerInfo::getFixedStack( 3663 DAG.getMachineFunction(), FI)); 3664 } else { 3665 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 3666 Chain, DAG, dl); 3667 } 3668 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 3669 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3670 ArgValue, ArgValue1, 3671 DAG.getIntPtrConstant(0, dl)); 3672 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3673 ArgValue, ArgValue2, 3674 DAG.getIntPtrConstant(1, dl)); 3675 } else 3676 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 3677 } else { 3678 const TargetRegisterClass *RC; 3679 3680 if (RegVT == MVT::f32) 3681 RC = &ARM::SPRRegClass; 3682 else if (RegVT == MVT::f64) 3683 RC = &ARM::DPRRegClass; 3684 else if (RegVT == MVT::v2f64) 3685 RC = &ARM::QPRRegClass; 3686 else if (RegVT == MVT::i32) 3687 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass 3688 : &ARM::GPRRegClass; 3689 else 3690 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 3691 3692 // Transform the arguments in physical registers into virtual ones. 3693 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3694 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 3695 } 3696 3697 // If this is an 8 or 16-bit value, it is really passed promoted 3698 // to 32 bits. Insert an assert[sz]ext to capture this, then 3699 // truncate to the right size. 3700 switch (VA.getLocInfo()) { 3701 default: llvm_unreachable("Unknown loc info!"); 3702 case CCValAssign::Full: break; 3703 case CCValAssign::BCvt: 3704 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 3705 break; 3706 case CCValAssign::SExt: 3707 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 3708 DAG.getValueType(VA.getValVT())); 3709 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3710 break; 3711 case CCValAssign::ZExt: 3712 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 3713 DAG.getValueType(VA.getValVT())); 3714 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3715 break; 3716 } 3717 3718 InVals.push_back(ArgValue); 3719 } else { // VA.isRegLoc() 3720 // sanity check 3721 assert(VA.isMemLoc()); 3722 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 3723 3724 int index = VA.getValNo(); 3725 3726 // Some Ins[] entries become multiple ArgLoc[] entries. 3727 // Process them only once. 3728 if (index != lastInsIndex) 3729 { 3730 ISD::ArgFlagsTy Flags = Ins[index].Flags; 3731 // FIXME: For now, all byval parameter objects are marked mutable. 3732 // This can be changed with more analysis. 3733 // In case of tail call optimization mark all arguments mutable. 3734 // Since they could be overwritten by lowering of arguments in case of 3735 // a tail call. 3736 if (Flags.isByVal()) { 3737 assert(Ins[index].isOrigArg() && 3738 "Byval arguments cannot be implicit"); 3739 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed(); 3740 3741 int FrameIndex = StoreByValRegs( 3742 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex, 3743 VA.getLocMemOffset(), Flags.getByValSize()); 3744 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT)); 3745 CCInfo.nextInRegsParam(); 3746 } else { 3747 unsigned FIOffset = VA.getLocMemOffset(); 3748 int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 3749 FIOffset, true); 3750 3751 // Create load nodes to retrieve arguments from the stack. 3752 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3753 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 3754 MachinePointerInfo::getFixedStack( 3755 DAG.getMachineFunction(), FI))); 3756 } 3757 lastInsIndex = index; 3758 } 3759 } 3760 } 3761 3762 // varargs 3763 if (isVarArg && MFI.hasVAStart()) 3764 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 3765 CCInfo.getNextStackOffset(), 3766 TotalArgRegsSaveSize); 3767 3768 AFI->setArgumentStackSize(CCInfo.getNextStackOffset()); 3769 3770 return Chain; 3771 } 3772 3773 /// isFloatingPointZero - Return true if this is +0.0. 3774 static bool isFloatingPointZero(SDValue Op) { 3775 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 3776 return CFP->getValueAPF().isPosZero(); 3777 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 3778 // Maybe this has already been legalized into the constant pool? 3779 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 3780 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 3781 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 3782 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 3783 return CFP->getValueAPF().isPosZero(); 3784 } 3785 } else if (Op->getOpcode() == ISD::BITCAST && 3786 Op->getValueType(0) == MVT::f64) { 3787 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64) 3788 // created by LowerConstantFP(). 3789 SDValue BitcastOp = Op->getOperand(0); 3790 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM && 3791 isNullConstant(BitcastOp->getOperand(0))) 3792 return true; 3793 } 3794 return false; 3795 } 3796 3797 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 3798 /// the given operands. 3799 SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 3800 SDValue &ARMcc, SelectionDAG &DAG, 3801 const SDLoc &dl) const { 3802 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 3803 unsigned C = RHSC->getZExtValue(); 3804 if (!isLegalICmpImmediate(C)) { 3805 // Constant does not fit, try adjusting it by one? 3806 switch (CC) { 3807 default: break; 3808 case ISD::SETLT: 3809 case ISD::SETGE: 3810 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 3811 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 3812 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3813 } 3814 break; 3815 case ISD::SETULT: 3816 case ISD::SETUGE: 3817 if (C != 0 && isLegalICmpImmediate(C-1)) { 3818 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 3819 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3820 } 3821 break; 3822 case ISD::SETLE: 3823 case ISD::SETGT: 3824 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 3825 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 3826 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3827 } 3828 break; 3829 case ISD::SETULE: 3830 case ISD::SETUGT: 3831 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 3832 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 3833 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3834 } 3835 break; 3836 } 3837 } 3838 } else if ((ARM_AM::getShiftOpcForNode(LHS.getOpcode()) != ARM_AM::no_shift) && 3839 (ARM_AM::getShiftOpcForNode(RHS.getOpcode()) == ARM_AM::no_shift)) { 3840 // In ARM and Thumb-2, the compare instructions can shift their second 3841 // operand. 3842 CC = ISD::getSetCCSwappedOperands(CC); 3843 std::swap(LHS, RHS); 3844 } 3845 3846 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3847 ARMISD::NodeType CompareType; 3848 switch (CondCode) { 3849 default: 3850 CompareType = ARMISD::CMP; 3851 break; 3852 case ARMCC::EQ: 3853 case ARMCC::NE: 3854 // Uses only Z Flag 3855 CompareType = ARMISD::CMPZ; 3856 break; 3857 } 3858 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3859 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 3860 } 3861 3862 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 3863 SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, 3864 SelectionDAG &DAG, const SDLoc &dl, 3865 bool InvalidOnQNaN) const { 3866 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64); 3867 SDValue Cmp; 3868 SDValue C = DAG.getConstant(InvalidOnQNaN, dl, MVT::i32); 3869 if (!isFloatingPointZero(RHS)) 3870 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS, C); 3871 else 3872 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS, C); 3873 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 3874 } 3875 3876 /// duplicateCmp - Glue values can have only one use, so this function 3877 /// duplicates a comparison node. 3878 SDValue 3879 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 3880 unsigned Opc = Cmp.getOpcode(); 3881 SDLoc DL(Cmp); 3882 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 3883 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3884 3885 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 3886 Cmp = Cmp.getOperand(0); 3887 Opc = Cmp.getOpcode(); 3888 if (Opc == ARMISD::CMPFP) 3889 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0), 3890 Cmp.getOperand(1), Cmp.getOperand(2)); 3891 else { 3892 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 3893 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0), 3894 Cmp.getOperand(1)); 3895 } 3896 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 3897 } 3898 3899 // This function returns three things: the arithmetic computation itself 3900 // (Value), a comparison (OverflowCmp), and a condition code (ARMcc). The 3901 // comparison and the condition code define the case in which the arithmetic 3902 // computation *does not* overflow. 3903 std::pair<SDValue, SDValue> 3904 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG, 3905 SDValue &ARMcc) const { 3906 assert(Op.getValueType() == MVT::i32 && "Unsupported value type"); 3907 3908 SDValue Value, OverflowCmp; 3909 SDValue LHS = Op.getOperand(0); 3910 SDValue RHS = Op.getOperand(1); 3911 SDLoc dl(Op); 3912 3913 // FIXME: We are currently always generating CMPs because we don't support 3914 // generating CMN through the backend. This is not as good as the natural 3915 // CMP case because it causes a register dependency and cannot be folded 3916 // later. 3917 3918 switch (Op.getOpcode()) { 3919 default: 3920 llvm_unreachable("Unknown overflow instruction!"); 3921 case ISD::SADDO: 3922 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3923 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 3924 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3925 break; 3926 case ISD::UADDO: 3927 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3928 // We use ADDC here to correspond to its use in LowerUnsignedALUO. 3929 // We do not use it in the USUBO case as Value may not be used. 3930 Value = DAG.getNode(ARMISD::ADDC, dl, 3931 DAG.getVTList(Op.getValueType(), MVT::i32), LHS, RHS) 3932 .getValue(0); 3933 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3934 break; 3935 case ISD::SSUBO: 3936 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3937 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3938 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3939 break; 3940 case ISD::USUBO: 3941 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3942 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3943 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3944 break; 3945 case ISD::UMULO: 3946 // We generate a UMUL_LOHI and then check if the high word is 0. 3947 ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32); 3948 Value = DAG.getNode(ISD::UMUL_LOHI, dl, 3949 DAG.getVTList(Op.getValueType(), Op.getValueType()), 3950 LHS, RHS); 3951 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1), 3952 DAG.getConstant(0, dl, MVT::i32)); 3953 Value = Value.getValue(0); // We only want the low 32 bits for the result. 3954 break; 3955 case ISD::SMULO: 3956 // We generate a SMUL_LOHI and then check if all the bits of the high word 3957 // are the same as the sign bit of the low word. 3958 ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32); 3959 Value = DAG.getNode(ISD::SMUL_LOHI, dl, 3960 DAG.getVTList(Op.getValueType(), Op.getValueType()), 3961 LHS, RHS); 3962 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1), 3963 DAG.getNode(ISD::SRA, dl, Op.getValueType(), 3964 Value.getValue(0), 3965 DAG.getConstant(31, dl, MVT::i32))); 3966 Value = Value.getValue(0); // We only want the low 32 bits for the result. 3967 break; 3968 } // switch (...) 3969 3970 return std::make_pair(Value, OverflowCmp); 3971 } 3972 3973 SDValue 3974 ARMTargetLowering::LowerSignedALUO(SDValue Op, SelectionDAG &DAG) const { 3975 // Let legalize expand this if it isn't a legal type yet. 3976 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 3977 return SDValue(); 3978 3979 SDValue Value, OverflowCmp; 3980 SDValue ARMcc; 3981 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc); 3982 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3983 SDLoc dl(Op); 3984 // We use 0 and 1 as false and true values. 3985 SDValue TVal = DAG.getConstant(1, dl, MVT::i32); 3986 SDValue FVal = DAG.getConstant(0, dl, MVT::i32); 3987 EVT VT = Op.getValueType(); 3988 3989 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal, 3990 ARMcc, CCR, OverflowCmp); 3991 3992 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 3993 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 3994 } 3995 3996 static SDValue ConvertBooleanCarryToCarryFlag(SDValue BoolCarry, 3997 SelectionDAG &DAG) { 3998 SDLoc DL(BoolCarry); 3999 EVT CarryVT = BoolCarry.getValueType(); 4000 4001 APInt NegOne = APInt::getAllOnesValue(CarryVT.getScalarSizeInBits()); 4002 // This converts the boolean value carry into the carry flag by doing 4003 // ARMISD::ADDC Carry, ~0 4004 return DAG.getNode(ARMISD::ADDC, DL, DAG.getVTList(CarryVT, MVT::i32), 4005 BoolCarry, DAG.getConstant(NegOne, DL, CarryVT)); 4006 } 4007 4008 static SDValue ConvertCarryFlagToBooleanCarry(SDValue Flags, EVT VT, 4009 SelectionDAG &DAG) { 4010 SDLoc DL(Flags); 4011 4012 // Now convert the carry flag into a boolean carry. We do this 4013 // using ARMISD:ADDE 0, 0, Carry 4014 return DAG.getNode(ARMISD::ADDE, DL, DAG.getVTList(VT, MVT::i32), 4015 DAG.getConstant(0, DL, MVT::i32), 4016 DAG.getConstant(0, DL, MVT::i32), Flags); 4017 } 4018 4019 SDValue ARMTargetLowering::LowerUnsignedALUO(SDValue Op, 4020 SelectionDAG &DAG) const { 4021 // Let legalize expand this if it isn't a legal type yet. 4022 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 4023 return SDValue(); 4024 4025 SDValue LHS = Op.getOperand(0); 4026 SDValue RHS = Op.getOperand(1); 4027 SDLoc dl(Op); 4028 4029 EVT VT = Op.getValueType(); 4030 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 4031 SDValue Value; 4032 SDValue Overflow; 4033 switch (Op.getOpcode()) { 4034 default: 4035 llvm_unreachable("Unknown overflow instruction!"); 4036 case ISD::UADDO: 4037 Value = DAG.getNode(ARMISD::ADDC, dl, VTs, LHS, RHS); 4038 // Convert the carry flag into a boolean value. 4039 Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG); 4040 break; 4041 case ISD::USUBO: { 4042 Value = DAG.getNode(ARMISD::SUBC, dl, VTs, LHS, RHS); 4043 // Convert the carry flag into a boolean value. 4044 Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG); 4045 // ARMISD::SUBC returns 0 when we have to borrow, so make it an overflow 4046 // value. So compute 1 - C. 4047 Overflow = DAG.getNode(ISD::SUB, dl, MVT::i32, 4048 DAG.getConstant(1, dl, MVT::i32), Overflow); 4049 break; 4050 } 4051 } 4052 4053 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 4054 } 4055 4056 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 4057 SDValue Cond = Op.getOperand(0); 4058 SDValue SelectTrue = Op.getOperand(1); 4059 SDValue SelectFalse = Op.getOperand(2); 4060 SDLoc dl(Op); 4061 unsigned Opc = Cond.getOpcode(); 4062 4063 if (Cond.getResNo() == 1 && 4064 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4065 Opc == ISD::USUBO)) { 4066 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 4067 return SDValue(); 4068 4069 SDValue Value, OverflowCmp; 4070 SDValue ARMcc; 4071 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 4072 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4073 EVT VT = Op.getValueType(); 4074 4075 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR, 4076 OverflowCmp, DAG); 4077 } 4078 4079 // Convert: 4080 // 4081 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 4082 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 4083 // 4084 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 4085 const ConstantSDNode *CMOVTrue = 4086 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 4087 const ConstantSDNode *CMOVFalse = 4088 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 4089 4090 if (CMOVTrue && CMOVFalse) { 4091 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 4092 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 4093 4094 SDValue True; 4095 SDValue False; 4096 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 4097 True = SelectTrue; 4098 False = SelectFalse; 4099 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 4100 True = SelectFalse; 4101 False = SelectTrue; 4102 } 4103 4104 if (True.getNode() && False.getNode()) { 4105 EVT VT = Op.getValueType(); 4106 SDValue ARMcc = Cond.getOperand(2); 4107 SDValue CCR = Cond.getOperand(3); 4108 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 4109 assert(True.getValueType() == VT); 4110 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG); 4111 } 4112 } 4113 } 4114 4115 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 4116 // undefined bits before doing a full-word comparison with zero. 4117 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 4118 DAG.getConstant(1, dl, Cond.getValueType())); 4119 4120 return DAG.getSelectCC(dl, Cond, 4121 DAG.getConstant(0, dl, Cond.getValueType()), 4122 SelectTrue, SelectFalse, ISD::SETNE); 4123 } 4124 4125 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 4126 bool &swpCmpOps, bool &swpVselOps) { 4127 // Start by selecting the GE condition code for opcodes that return true for 4128 // 'equality' 4129 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 4130 CC == ISD::SETULE) 4131 CondCode = ARMCC::GE; 4132 4133 // and GT for opcodes that return false for 'equality'. 4134 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 4135 CC == ISD::SETULT) 4136 CondCode = ARMCC::GT; 4137 4138 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 4139 // to swap the compare operands. 4140 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 4141 CC == ISD::SETULT) 4142 swpCmpOps = true; 4143 4144 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 4145 // If we have an unordered opcode, we need to swap the operands to the VSEL 4146 // instruction (effectively negating the condition). 4147 // 4148 // This also has the effect of swapping which one of 'less' or 'greater' 4149 // returns true, so we also swap the compare operands. It also switches 4150 // whether we return true for 'equality', so we compensate by picking the 4151 // opposite condition code to our original choice. 4152 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 4153 CC == ISD::SETUGT) { 4154 swpCmpOps = !swpCmpOps; 4155 swpVselOps = !swpVselOps; 4156 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 4157 } 4158 4159 // 'ordered' is 'anything but unordered', so use the VS condition code and 4160 // swap the VSEL operands. 4161 if (CC == ISD::SETO) { 4162 CondCode = ARMCC::VS; 4163 swpVselOps = true; 4164 } 4165 4166 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 4167 // code and swap the VSEL operands. 4168 if (CC == ISD::SETUNE) { 4169 CondCode = ARMCC::EQ; 4170 swpVselOps = true; 4171 } 4172 } 4173 4174 SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal, 4175 SDValue TrueVal, SDValue ARMcc, SDValue CCR, 4176 SDValue Cmp, SelectionDAG &DAG) const { 4177 if (Subtarget->isFPOnlySP() && VT == MVT::f64) { 4178 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl, 4179 DAG.getVTList(MVT::i32, MVT::i32), FalseVal); 4180 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl, 4181 DAG.getVTList(MVT::i32, MVT::i32), TrueVal); 4182 4183 SDValue TrueLow = TrueVal.getValue(0); 4184 SDValue TrueHigh = TrueVal.getValue(1); 4185 SDValue FalseLow = FalseVal.getValue(0); 4186 SDValue FalseHigh = FalseVal.getValue(1); 4187 4188 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow, 4189 ARMcc, CCR, Cmp); 4190 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh, 4191 ARMcc, CCR, duplicateCmp(Cmp, DAG)); 4192 4193 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High); 4194 } else { 4195 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 4196 Cmp); 4197 } 4198 } 4199 4200 static bool isGTorGE(ISD::CondCode CC) { 4201 return CC == ISD::SETGT || CC == ISD::SETGE; 4202 } 4203 4204 static bool isLTorLE(ISD::CondCode CC) { 4205 return CC == ISD::SETLT || CC == ISD::SETLE; 4206 } 4207 4208 // See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating. 4209 // All of these conditions (and their <= and >= counterparts) will do: 4210 // x < k ? k : x 4211 // x > k ? x : k 4212 // k < x ? x : k 4213 // k > x ? k : x 4214 static bool isLowerSaturate(const SDValue LHS, const SDValue RHS, 4215 const SDValue TrueVal, const SDValue FalseVal, 4216 const ISD::CondCode CC, const SDValue K) { 4217 return (isGTorGE(CC) && 4218 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) || 4219 (isLTorLE(CC) && 4220 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))); 4221 } 4222 4223 // Similar to isLowerSaturate(), but checks for upper-saturating conditions. 4224 static bool isUpperSaturate(const SDValue LHS, const SDValue RHS, 4225 const SDValue TrueVal, const SDValue FalseVal, 4226 const ISD::CondCode CC, const SDValue K) { 4227 return (isGTorGE(CC) && 4228 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) || 4229 (isLTorLE(CC) && 4230 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))); 4231 } 4232 4233 // Check if two chained conditionals could be converted into SSAT or USAT. 4234 // 4235 // SSAT can replace a set of two conditional selectors that bound a number to an 4236 // interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples: 4237 // 4238 // x < -k ? -k : (x > k ? k : x) 4239 // x < -k ? -k : (x < k ? x : k) 4240 // x > -k ? (x > k ? k : x) : -k 4241 // x < k ? (x < -k ? -k : x) : k 4242 // etc. 4243 // 4244 // USAT works similarily to SSAT but bounds on the interval [0, k] where k + 1 is 4245 // a power of 2. 4246 // 4247 // It returns true if the conversion can be done, false otherwise. 4248 // Additionally, the variable is returned in parameter V, the constant in K and 4249 // usat is set to true if the conditional represents an unsigned saturation 4250 static bool isSaturatingConditional(const SDValue &Op, SDValue &V, 4251 uint64_t &K, bool &usat) { 4252 SDValue LHS1 = Op.getOperand(0); 4253 SDValue RHS1 = Op.getOperand(1); 4254 SDValue TrueVal1 = Op.getOperand(2); 4255 SDValue FalseVal1 = Op.getOperand(3); 4256 ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4257 4258 const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1; 4259 if (Op2.getOpcode() != ISD::SELECT_CC) 4260 return false; 4261 4262 SDValue LHS2 = Op2.getOperand(0); 4263 SDValue RHS2 = Op2.getOperand(1); 4264 SDValue TrueVal2 = Op2.getOperand(2); 4265 SDValue FalseVal2 = Op2.getOperand(3); 4266 ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get(); 4267 4268 // Find out which are the constants and which are the variables 4269 // in each conditional 4270 SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1) 4271 ? &RHS1 4272 : nullptr; 4273 SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2) 4274 ? &RHS2 4275 : nullptr; 4276 SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2; 4277 SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1; 4278 SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2; 4279 SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2; 4280 4281 // We must detect cases where the original operations worked with 16- or 4282 // 8-bit values. In such case, V2Tmp != V2 because the comparison operations 4283 // must work with sign-extended values but the select operations return 4284 // the original non-extended value. 4285 SDValue V2TmpReg = V2Tmp; 4286 if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG) 4287 V2TmpReg = V2Tmp->getOperand(0); 4288 4289 // Check that the registers and the constants have the correct values 4290 // in both conditionals 4291 if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp || 4292 V2TmpReg != V2) 4293 return false; 4294 4295 // Figure out which conditional is saturating the lower/upper bound. 4296 const SDValue *LowerCheckOp = 4297 isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1) 4298 ? &Op 4299 : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) 4300 ? &Op2 4301 : nullptr; 4302 const SDValue *UpperCheckOp = 4303 isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1) 4304 ? &Op 4305 : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) 4306 ? &Op2 4307 : nullptr; 4308 4309 if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp) 4310 return false; 4311 4312 // Check that the constant in the lower-bound check is 4313 // the opposite of the constant in the upper-bound check 4314 // in 1's complement. 4315 int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue(); 4316 int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue(); 4317 int64_t PosVal = std::max(Val1, Val2); 4318 int64_t NegVal = std::min(Val1, Val2); 4319 4320 if (((Val1 > Val2 && UpperCheckOp == &Op) || 4321 (Val1 < Val2 && UpperCheckOp == &Op2)) && 4322 isPowerOf2_64(PosVal + 1)) { 4323 4324 // Handle the difference between USAT (unsigned) and SSAT (signed) saturation 4325 if (Val1 == ~Val2) 4326 usat = false; 4327 else if (NegVal == 0) 4328 usat = true; 4329 else 4330 return false; 4331 4332 V = V2; 4333 K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive 4334 4335 return true; 4336 } 4337 4338 return false; 4339 } 4340 4341 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 4342 EVT VT = Op.getValueType(); 4343 SDLoc dl(Op); 4344 4345 // Try to convert two saturating conditional selects into a single SSAT 4346 SDValue SatValue; 4347 uint64_t SatConstant; 4348 bool SatUSat; 4349 if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) && 4350 isSaturatingConditional(Op, SatValue, SatConstant, SatUSat)) { 4351 if (SatUSat) 4352 return DAG.getNode(ARMISD::USAT, dl, VT, SatValue, 4353 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT)); 4354 else 4355 return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue, 4356 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT)); 4357 } 4358 4359 SDValue LHS = Op.getOperand(0); 4360 SDValue RHS = Op.getOperand(1); 4361 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 4362 SDValue TrueVal = Op.getOperand(2); 4363 SDValue FalseVal = Op.getOperand(3); 4364 4365 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 4366 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 4367 dl); 4368 4369 // If softenSetCCOperands only returned one value, we should compare it to 4370 // zero. 4371 if (!RHS.getNode()) { 4372 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 4373 CC = ISD::SETNE; 4374 } 4375 } 4376 4377 if (LHS.getValueType() == MVT::i32) { 4378 // Try to generate VSEL on ARMv8. 4379 // The VSEL instruction can't use all the usual ARM condition 4380 // codes: it only has two bits to select the condition code, so it's 4381 // constrained to use only GE, GT, VS and EQ. 4382 // 4383 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 4384 // swap the operands of the previous compare instruction (effectively 4385 // inverting the compare condition, swapping 'less' and 'greater') and 4386 // sometimes need to swap the operands to the VSEL (which inverts the 4387 // condition in the sense of firing whenever the previous condition didn't) 4388 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 4389 TrueVal.getValueType() == MVT::f64)) { 4390 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 4391 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 4392 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 4393 CC = ISD::getSetCCInverse(CC, true); 4394 std::swap(TrueVal, FalseVal); 4395 } 4396 } 4397 4398 SDValue ARMcc; 4399 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4400 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 4401 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 4402 } 4403 4404 ARMCC::CondCodes CondCode, CondCode2; 4405 bool InvalidOnQNaN; 4406 FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN); 4407 4408 // Try to generate VMAXNM/VMINNM on ARMv8. 4409 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 4410 TrueVal.getValueType() == MVT::f64)) { 4411 bool swpCmpOps = false; 4412 bool swpVselOps = false; 4413 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 4414 4415 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 4416 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 4417 if (swpCmpOps) 4418 std::swap(LHS, RHS); 4419 if (swpVselOps) 4420 std::swap(TrueVal, FalseVal); 4421 } 4422 } 4423 4424 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4425 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN); 4426 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4427 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 4428 if (CondCode2 != ARMCC::AL) { 4429 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32); 4430 // FIXME: Needs another CMP because flag can have but one use. 4431 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN); 4432 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG); 4433 } 4434 return Result; 4435 } 4436 4437 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 4438 /// to morph to an integer compare sequence. 4439 static bool canChangeToInt(SDValue Op, bool &SeenZero, 4440 const ARMSubtarget *Subtarget) { 4441 SDNode *N = Op.getNode(); 4442 if (!N->hasOneUse()) 4443 // Otherwise it requires moving the value from fp to integer registers. 4444 return false; 4445 if (!N->getNumValues()) 4446 return false; 4447 EVT VT = Op.getValueType(); 4448 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 4449 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 4450 // vmrs are very slow, e.g. cortex-a8. 4451 return false; 4452 4453 if (isFloatingPointZero(Op)) { 4454 SeenZero = true; 4455 return true; 4456 } 4457 return ISD::isNormalLoad(N); 4458 } 4459 4460 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 4461 if (isFloatingPointZero(Op)) 4462 return DAG.getConstant(0, SDLoc(Op), MVT::i32); 4463 4464 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 4465 return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(), 4466 Ld->getPointerInfo(), Ld->getAlignment(), 4467 Ld->getMemOperand()->getFlags()); 4468 4469 llvm_unreachable("Unknown VFP cmp argument!"); 4470 } 4471 4472 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 4473 SDValue &RetVal1, SDValue &RetVal2) { 4474 SDLoc dl(Op); 4475 4476 if (isFloatingPointZero(Op)) { 4477 RetVal1 = DAG.getConstant(0, dl, MVT::i32); 4478 RetVal2 = DAG.getConstant(0, dl, MVT::i32); 4479 return; 4480 } 4481 4482 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 4483 SDValue Ptr = Ld->getBasePtr(); 4484 RetVal1 = 4485 DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(), 4486 Ld->getAlignment(), Ld->getMemOperand()->getFlags()); 4487 4488 EVT PtrType = Ptr.getValueType(); 4489 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 4490 SDValue NewPtr = DAG.getNode(ISD::ADD, dl, 4491 PtrType, Ptr, DAG.getConstant(4, dl, PtrType)); 4492 RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr, 4493 Ld->getPointerInfo().getWithOffset(4), NewAlign, 4494 Ld->getMemOperand()->getFlags()); 4495 return; 4496 } 4497 4498 llvm_unreachable("Unknown VFP cmp argument!"); 4499 } 4500 4501 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 4502 /// f32 and even f64 comparisons to integer ones. 4503 SDValue 4504 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 4505 SDValue Chain = Op.getOperand(0); 4506 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 4507 SDValue LHS = Op.getOperand(2); 4508 SDValue RHS = Op.getOperand(3); 4509 SDValue Dest = Op.getOperand(4); 4510 SDLoc dl(Op); 4511 4512 bool LHSSeenZero = false; 4513 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 4514 bool RHSSeenZero = false; 4515 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 4516 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 4517 // If unsafe fp math optimization is enabled and there are no other uses of 4518 // the CMP operands, and the condition code is EQ or NE, we can optimize it 4519 // to an integer comparison. 4520 if (CC == ISD::SETOEQ) 4521 CC = ISD::SETEQ; 4522 else if (CC == ISD::SETUNE) 4523 CC = ISD::SETNE; 4524 4525 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32); 4526 SDValue ARMcc; 4527 if (LHS.getValueType() == MVT::f32) { 4528 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 4529 bitcastf32Toi32(LHS, DAG), Mask); 4530 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 4531 bitcastf32Toi32(RHS, DAG), Mask); 4532 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 4533 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4534 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 4535 Chain, Dest, ARMcc, CCR, Cmp); 4536 } 4537 4538 SDValue LHS1, LHS2; 4539 SDValue RHS1, RHS2; 4540 expandf64Toi32(LHS, DAG, LHS1, LHS2); 4541 expandf64Toi32(RHS, DAG, RHS1, RHS2); 4542 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 4543 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 4544 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 4545 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4546 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 4547 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 4548 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops); 4549 } 4550 4551 return SDValue(); 4552 } 4553 4554 SDValue ARMTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 4555 SDValue Chain = Op.getOperand(0); 4556 SDValue Cond = Op.getOperand(1); 4557 SDValue Dest = Op.getOperand(2); 4558 SDLoc dl(Op); 4559 4560 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 4561 // instruction. 4562 unsigned Opc = Cond.getOpcode(); 4563 if (Cond.getResNo() == 1 && 4564 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4565 Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO)) { 4566 // Only lower legal XALUO ops. 4567 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 4568 return SDValue(); 4569 4570 // The actual operation with overflow check. 4571 SDValue Value, OverflowCmp; 4572 SDValue ARMcc; 4573 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 4574 4575 // Reverse the condition code. 4576 ARMCC::CondCodes CondCode = 4577 (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue(); 4578 CondCode = ARMCC::getOppositeCondition(CondCode); 4579 ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32); 4580 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4581 4582 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR, 4583 OverflowCmp); 4584 } 4585 4586 return SDValue(); 4587 } 4588 4589 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 4590 SDValue Chain = Op.getOperand(0); 4591 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 4592 SDValue LHS = Op.getOperand(2); 4593 SDValue RHS = Op.getOperand(3); 4594 SDValue Dest = Op.getOperand(4); 4595 SDLoc dl(Op); 4596 4597 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 4598 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 4599 dl); 4600 4601 // If softenSetCCOperands only returned one value, we should compare it to 4602 // zero. 4603 if (!RHS.getNode()) { 4604 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 4605 CC = ISD::SETNE; 4606 } 4607 } 4608 4609 // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch 4610 // instruction. 4611 unsigned Opc = LHS.getOpcode(); 4612 if (LHS.getResNo() == 1 && (isOneConstant(RHS) || isNullConstant(RHS)) && 4613 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 4614 Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO) && 4615 (CC == ISD::SETEQ || CC == ISD::SETNE)) { 4616 // Only lower legal XALUO ops. 4617 if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0))) 4618 return SDValue(); 4619 4620 // The actual operation with overflow check. 4621 SDValue Value, OverflowCmp; 4622 SDValue ARMcc; 4623 std::tie(Value, OverflowCmp) = getARMXALUOOp(LHS.getValue(0), DAG, ARMcc); 4624 4625 if ((CC == ISD::SETNE) != isOneConstant(RHS)) { 4626 // Reverse the condition code. 4627 ARMCC::CondCodes CondCode = 4628 (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue(); 4629 CondCode = ARMCC::getOppositeCondition(CondCode); 4630 ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32); 4631 } 4632 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4633 4634 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR, 4635 OverflowCmp); 4636 } 4637 4638 if (LHS.getValueType() == MVT::i32) { 4639 SDValue ARMcc; 4640 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 4641 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4642 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 4643 Chain, Dest, ARMcc, CCR, Cmp); 4644 } 4645 4646 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 4647 4648 if (getTargetMachine().Options.UnsafeFPMath && 4649 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 4650 CC == ISD::SETNE || CC == ISD::SETUNE)) { 4651 if (SDValue Result = OptimizeVFPBrcond(Op, DAG)) 4652 return Result; 4653 } 4654 4655 ARMCC::CondCodes CondCode, CondCode2; 4656 bool InvalidOnQNaN; 4657 FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN); 4658 4659 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 4660 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN); 4661 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4662 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 4663 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 4664 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 4665 if (CondCode2 != ARMCC::AL) { 4666 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32); 4667 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 4668 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 4669 } 4670 return Res; 4671 } 4672 4673 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 4674 SDValue Chain = Op.getOperand(0); 4675 SDValue Table = Op.getOperand(1); 4676 SDValue Index = Op.getOperand(2); 4677 SDLoc dl(Op); 4678 4679 EVT PTy = getPointerTy(DAG.getDataLayout()); 4680 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 4681 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 4682 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI); 4683 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy)); 4684 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Index); 4685 if (Subtarget->isThumb2() || (Subtarget->hasV8MBaselineOps() && Subtarget->isThumb())) { 4686 // Thumb2 and ARMv8-M use a two-level jump. That is, it jumps into the jump table 4687 // which does another jump to the destination. This also makes it easier 4688 // to translate it to TBB / TBH later (Thumb2 only). 4689 // FIXME: This might not work if the function is extremely large. 4690 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 4691 Addr, Op.getOperand(2), JTI); 4692 } 4693 if (isPositionIndependent() || Subtarget->isROPI()) { 4694 Addr = 4695 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 4696 MachinePointerInfo::getJumpTable(DAG.getMachineFunction())); 4697 Chain = Addr.getValue(1); 4698 Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Addr); 4699 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 4700 } else { 4701 Addr = 4702 DAG.getLoad(PTy, dl, Chain, Addr, 4703 MachinePointerInfo::getJumpTable(DAG.getMachineFunction())); 4704 Chain = Addr.getValue(1); 4705 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 4706 } 4707 } 4708 4709 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 4710 EVT VT = Op.getValueType(); 4711 SDLoc dl(Op); 4712 4713 if (Op.getValueType().getVectorElementType() == MVT::i32) { 4714 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 4715 return Op; 4716 return DAG.UnrollVectorOp(Op.getNode()); 4717 } 4718 4719 assert(Op.getOperand(0).getValueType() == MVT::v4f32 && 4720 "Invalid type for custom lowering!"); 4721 if (VT != MVT::v4i16) 4722 return DAG.UnrollVectorOp(Op.getNode()); 4723 4724 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0)); 4725 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 4726 } 4727 4728 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { 4729 EVT VT = Op.getValueType(); 4730 if (VT.isVector()) 4731 return LowerVectorFP_TO_INT(Op, DAG); 4732 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) { 4733 RTLIB::Libcall LC; 4734 if (Op.getOpcode() == ISD::FP_TO_SINT) 4735 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), 4736 Op.getValueType()); 4737 else 4738 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), 4739 Op.getValueType()); 4740 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 4741 /*isSigned*/ false, SDLoc(Op)).first; 4742 } 4743 4744 return Op; 4745 } 4746 4747 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 4748 EVT VT = Op.getValueType(); 4749 SDLoc dl(Op); 4750 4751 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 4752 if (VT.getVectorElementType() == MVT::f32) 4753 return Op; 4754 return DAG.UnrollVectorOp(Op.getNode()); 4755 } 4756 4757 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 4758 "Invalid type for custom lowering!"); 4759 if (VT != MVT::v4f32) 4760 return DAG.UnrollVectorOp(Op.getNode()); 4761 4762 unsigned CastOpc; 4763 unsigned Opc; 4764 switch (Op.getOpcode()) { 4765 default: llvm_unreachable("Invalid opcode!"); 4766 case ISD::SINT_TO_FP: 4767 CastOpc = ISD::SIGN_EXTEND; 4768 Opc = ISD::SINT_TO_FP; 4769 break; 4770 case ISD::UINT_TO_FP: 4771 CastOpc = ISD::ZERO_EXTEND; 4772 Opc = ISD::UINT_TO_FP; 4773 break; 4774 } 4775 4776 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 4777 return DAG.getNode(Opc, dl, VT, Op); 4778 } 4779 4780 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const { 4781 EVT VT = Op.getValueType(); 4782 if (VT.isVector()) 4783 return LowerVectorINT_TO_FP(Op, DAG); 4784 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) { 4785 RTLIB::Libcall LC; 4786 if (Op.getOpcode() == ISD::SINT_TO_FP) 4787 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), 4788 Op.getValueType()); 4789 else 4790 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), 4791 Op.getValueType()); 4792 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 4793 /*isSigned*/ false, SDLoc(Op)).first; 4794 } 4795 4796 return Op; 4797 } 4798 4799 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 4800 // Implement fcopysign with a fabs and a conditional fneg. 4801 SDValue Tmp0 = Op.getOperand(0); 4802 SDValue Tmp1 = Op.getOperand(1); 4803 SDLoc dl(Op); 4804 EVT VT = Op.getValueType(); 4805 EVT SrcVT = Tmp1.getValueType(); 4806 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 4807 Tmp0.getOpcode() == ARMISD::VMOVDRR; 4808 bool UseNEON = !InGPR && Subtarget->hasNEON(); 4809 4810 if (UseNEON) { 4811 // Use VBSL to copy the sign bit. 4812 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 4813 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 4814 DAG.getTargetConstant(EncodedVal, dl, MVT::i32)); 4815 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 4816 if (VT == MVT::f64) 4817 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4818 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 4819 DAG.getConstant(32, dl, MVT::i32)); 4820 else /*if (VT == MVT::f32)*/ 4821 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 4822 if (SrcVT == MVT::f32) { 4823 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 4824 if (VT == MVT::f64) 4825 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4826 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 4827 DAG.getConstant(32, dl, MVT::i32)); 4828 } else if (VT == MVT::f32) 4829 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 4830 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 4831 DAG.getConstant(32, dl, MVT::i32)); 4832 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 4833 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 4834 4835 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 4836 dl, MVT::i32); 4837 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 4838 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 4839 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 4840 4841 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 4842 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 4843 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 4844 if (VT == MVT::f32) { 4845 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 4846 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 4847 DAG.getConstant(0, dl, MVT::i32)); 4848 } else { 4849 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 4850 } 4851 4852 return Res; 4853 } 4854 4855 // Bitcast operand 1 to i32. 4856 if (SrcVT == MVT::f64) 4857 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4858 Tmp1).getValue(1); 4859 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 4860 4861 // Or in the signbit with integer operations. 4862 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32); 4863 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32); 4864 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 4865 if (VT == MVT::f32) { 4866 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 4867 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 4868 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 4869 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 4870 } 4871 4872 // f64: Or the high part with signbit and then combine two parts. 4873 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4874 Tmp0); 4875 SDValue Lo = Tmp0.getValue(0); 4876 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 4877 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 4878 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 4879 } 4880 4881 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 4882 MachineFunction &MF = DAG.getMachineFunction(); 4883 MachineFrameInfo &MFI = MF.getFrameInfo(); 4884 MFI.setReturnAddressIsTaken(true); 4885 4886 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 4887 return SDValue(); 4888 4889 EVT VT = Op.getValueType(); 4890 SDLoc dl(Op); 4891 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4892 if (Depth) { 4893 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 4894 SDValue Offset = DAG.getConstant(4, dl, MVT::i32); 4895 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 4896 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 4897 MachinePointerInfo()); 4898 } 4899 4900 // Return LR, which contains the return address. Mark it an implicit live-in. 4901 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 4902 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 4903 } 4904 4905 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 4906 const ARMBaseRegisterInfo &ARI = 4907 *static_cast<const ARMBaseRegisterInfo*>(RegInfo); 4908 MachineFunction &MF = DAG.getMachineFunction(); 4909 MachineFrameInfo &MFI = MF.getFrameInfo(); 4910 MFI.setFrameAddressIsTaken(true); 4911 4912 EVT VT = Op.getValueType(); 4913 SDLoc dl(Op); // FIXME probably not meaningful 4914 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4915 unsigned FrameReg = ARI.getFrameRegister(MF); 4916 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 4917 while (Depth--) 4918 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 4919 MachinePointerInfo()); 4920 return FrameAddr; 4921 } 4922 4923 // FIXME? Maybe this could be a TableGen attribute on some registers and 4924 // this table could be generated automatically from RegInfo. 4925 unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT, 4926 SelectionDAG &DAG) const { 4927 unsigned Reg = StringSwitch<unsigned>(RegName) 4928 .Case("sp", ARM::SP) 4929 .Default(0); 4930 if (Reg) 4931 return Reg; 4932 report_fatal_error(Twine("Invalid register name \"" 4933 + StringRef(RegName) + "\".")); 4934 } 4935 4936 // Result is 64 bit value so split into two 32 bit values and return as a 4937 // pair of values. 4938 static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results, 4939 SelectionDAG &DAG) { 4940 SDLoc DL(N); 4941 4942 // This function is only supposed to be called for i64 type destination. 4943 assert(N->getValueType(0) == MVT::i64 4944 && "ExpandREAD_REGISTER called for non-i64 type result."); 4945 4946 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL, 4947 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other), 4948 N->getOperand(0), 4949 N->getOperand(1)); 4950 4951 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0), 4952 Read.getValue(1))); 4953 Results.push_back(Read.getOperand(0)); 4954 } 4955 4956 /// \p BC is a bitcast that is about to be turned into a VMOVDRR. 4957 /// When \p DstVT, the destination type of \p BC, is on the vector 4958 /// register bank and the source of bitcast, \p Op, operates on the same bank, 4959 /// it might be possible to combine them, such that everything stays on the 4960 /// vector register bank. 4961 /// \p return The node that would replace \p BT, if the combine 4962 /// is possible. 4963 static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC, 4964 SelectionDAG &DAG) { 4965 SDValue Op = BC->getOperand(0); 4966 EVT DstVT = BC->getValueType(0); 4967 4968 // The only vector instruction that can produce a scalar (remember, 4969 // since the bitcast was about to be turned into VMOVDRR, the source 4970 // type is i64) from a vector is EXTRACT_VECTOR_ELT. 4971 // Moreover, we can do this combine only if there is one use. 4972 // Finally, if the destination type is not a vector, there is not 4973 // much point on forcing everything on the vector bank. 4974 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 4975 !Op.hasOneUse()) 4976 return SDValue(); 4977 4978 // If the index is not constant, we will introduce an additional 4979 // multiply that will stick. 4980 // Give up in that case. 4981 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 4982 if (!Index) 4983 return SDValue(); 4984 unsigned DstNumElt = DstVT.getVectorNumElements(); 4985 4986 // Compute the new index. 4987 const APInt &APIntIndex = Index->getAPIntValue(); 4988 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt); 4989 NewIndex *= APIntIndex; 4990 // Check if the new constant index fits into i32. 4991 if (NewIndex.getBitWidth() > 32) 4992 return SDValue(); 4993 4994 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) -> 4995 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M) 4996 SDLoc dl(Op); 4997 SDValue ExtractSrc = Op.getOperand(0); 4998 EVT VecVT = EVT::getVectorVT( 4999 *DAG.getContext(), DstVT.getScalarType(), 5000 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt); 5001 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc); 5002 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast, 5003 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32)); 5004 } 5005 5006 /// ExpandBITCAST - If the target supports VFP, this function is called to 5007 /// expand a bit convert where either the source or destination type is i64 to 5008 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 5009 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 5010 /// vectors), since the legalizer won't know what to do with that. 5011 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 5012 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 5013 SDLoc dl(N); 5014 SDValue Op = N->getOperand(0); 5015 5016 // This function is only supposed to be called for i64 types, either as the 5017 // source or destination of the bit convert. 5018 EVT SrcVT = Op.getValueType(); 5019 EVT DstVT = N->getValueType(0); 5020 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 5021 "ExpandBITCAST called for non-i64 type"); 5022 5023 // Turn i64->f64 into VMOVDRR. 5024 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 5025 // Do not force values to GPRs (this is what VMOVDRR does for the inputs) 5026 // if we can combine the bitcast with its source. 5027 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG)) 5028 return Val; 5029 5030 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 5031 DAG.getConstant(0, dl, MVT::i32)); 5032 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 5033 DAG.getConstant(1, dl, MVT::i32)); 5034 return DAG.getNode(ISD::BITCAST, dl, DstVT, 5035 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 5036 } 5037 5038 // Turn f64->i64 into VMOVRRD. 5039 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 5040 SDValue Cvt; 5041 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() && 5042 SrcVT.getVectorNumElements() > 1) 5043 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 5044 DAG.getVTList(MVT::i32, MVT::i32), 5045 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op)); 5046 else 5047 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 5048 DAG.getVTList(MVT::i32, MVT::i32), Op); 5049 // Merge the pieces into a single i64 value. 5050 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 5051 } 5052 5053 return SDValue(); 5054 } 5055 5056 /// getZeroVector - Returns a vector of specified type with all zero elements. 5057 /// Zero vectors are used to represent vector negation and in those cases 5058 /// will be implemented with the NEON VNEG instruction. However, VNEG does 5059 /// not support i64 elements, so sometimes the zero vectors will need to be 5060 /// explicitly constructed. Regardless, use a canonical VMOV to create the 5061 /// zero vector. 5062 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) { 5063 assert(VT.isVector() && "Expected a vector type"); 5064 // The canonical modified immediate encoding of a zero vector is....0! 5065 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32); 5066 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 5067 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 5068 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5069 } 5070 5071 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 5072 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 5073 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 5074 SelectionDAG &DAG) const { 5075 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 5076 EVT VT = Op.getValueType(); 5077 unsigned VTBits = VT.getSizeInBits(); 5078 SDLoc dl(Op); 5079 SDValue ShOpLo = Op.getOperand(0); 5080 SDValue ShOpHi = Op.getOperand(1); 5081 SDValue ShAmt = Op.getOperand(2); 5082 SDValue ARMcc; 5083 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5084 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 5085 5086 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 5087 5088 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 5089 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 5090 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 5091 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 5092 DAG.getConstant(VTBits, dl, MVT::i32)); 5093 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 5094 SDValue LoSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 5095 SDValue LoBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 5096 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5097 ISD::SETGE, ARMcc, DAG, dl); 5098 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, LoBigShift, 5099 ARMcc, CCR, CmpLo); 5100 5101 SDValue HiSmallShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 5102 SDValue HiBigShift = Opc == ISD::SRA 5103 ? DAG.getNode(Opc, dl, VT, ShOpHi, 5104 DAG.getConstant(VTBits - 1, dl, VT)) 5105 : DAG.getConstant(0, dl, VT); 5106 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5107 ISD::SETGE, ARMcc, DAG, dl); 5108 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift, 5109 ARMcc, CCR, CmpHi); 5110 5111 SDValue Ops[2] = { Lo, Hi }; 5112 return DAG.getMergeValues(Ops, dl); 5113 } 5114 5115 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 5116 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 5117 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 5118 SelectionDAG &DAG) const { 5119 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 5120 EVT VT = Op.getValueType(); 5121 unsigned VTBits = VT.getSizeInBits(); 5122 SDLoc dl(Op); 5123 SDValue ShOpLo = Op.getOperand(0); 5124 SDValue ShOpHi = Op.getOperand(1); 5125 SDValue ShAmt = Op.getOperand(2); 5126 SDValue ARMcc; 5127 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5128 5129 assert(Op.getOpcode() == ISD::SHL_PARTS); 5130 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 5131 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 5132 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 5133 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 5134 SDValue HiSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 5135 5136 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 5137 DAG.getConstant(VTBits, dl, MVT::i32)); 5138 SDValue HiBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 5139 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5140 ISD::SETGE, ARMcc, DAG, dl); 5141 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift, 5142 ARMcc, CCR, CmpHi); 5143 5144 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 5145 ISD::SETGE, ARMcc, DAG, dl); 5146 SDValue LoSmallShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 5147 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, 5148 DAG.getConstant(0, dl, VT), ARMcc, CCR, CmpLo); 5149 5150 SDValue Ops[2] = { Lo, Hi }; 5151 return DAG.getMergeValues(Ops, dl); 5152 } 5153 5154 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 5155 SelectionDAG &DAG) const { 5156 // The rounding mode is in bits 23:22 of the FPSCR. 5157 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 5158 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 5159 // so that the shift + and get folded into a bitfield extract. 5160 SDLoc dl(Op); 5161 SDValue Ops[] = { DAG.getEntryNode(), 5162 DAG.getConstant(Intrinsic::arm_get_fpscr, dl, MVT::i32) }; 5163 5164 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_W_CHAIN, dl, MVT::i32, Ops); 5165 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 5166 DAG.getConstant(1U << 22, dl, MVT::i32)); 5167 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 5168 DAG.getConstant(22, dl, MVT::i32)); 5169 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 5170 DAG.getConstant(3, dl, MVT::i32)); 5171 } 5172 5173 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 5174 const ARMSubtarget *ST) { 5175 SDLoc dl(N); 5176 EVT VT = N->getValueType(0); 5177 if (VT.isVector()) { 5178 assert(ST->hasNEON()); 5179 5180 // Compute the least significant set bit: LSB = X & -X 5181 SDValue X = N->getOperand(0); 5182 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X); 5183 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX); 5184 5185 EVT ElemTy = VT.getVectorElementType(); 5186 5187 if (ElemTy == MVT::i8) { 5188 // Compute with: cttz(x) = ctpop(lsb - 1) 5189 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5190 DAG.getTargetConstant(1, dl, ElemTy)); 5191 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 5192 return DAG.getNode(ISD::CTPOP, dl, VT, Bits); 5193 } 5194 5195 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) && 5196 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) { 5197 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0 5198 unsigned NumBits = ElemTy.getSizeInBits(); 5199 SDValue WidthMinus1 = 5200 DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5201 DAG.getTargetConstant(NumBits - 1, dl, ElemTy)); 5202 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB); 5203 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ); 5204 } 5205 5206 // Compute with: cttz(x) = ctpop(lsb - 1) 5207 5208 // Since we can only compute the number of bits in a byte with vcnt.8, we 5209 // have to gather the result with pairwise addition (vpaddl) for i16, i32, 5210 // and i64. 5211 5212 // Compute LSB - 1. 5213 SDValue Bits; 5214 if (ElemTy == MVT::i64) { 5215 // Load constant 0xffff'ffff'ffff'ffff to register. 5216 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5217 DAG.getTargetConstant(0x1eff, dl, MVT::i32)); 5218 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF); 5219 } else { 5220 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 5221 DAG.getTargetConstant(1, dl, ElemTy)); 5222 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 5223 } 5224 5225 // Count #bits with vcnt.8. 5226 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 5227 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits); 5228 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8); 5229 5230 // Gather the #bits with vpaddl (pairwise add.) 5231 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 5232 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit, 5233 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32), 5234 Cnt8); 5235 if (ElemTy == MVT::i16) 5236 return Cnt16; 5237 5238 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32; 5239 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit, 5240 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32), 5241 Cnt16); 5242 if (ElemTy == MVT::i32) 5243 return Cnt32; 5244 5245 assert(ElemTy == MVT::i64); 5246 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 5247 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32), 5248 Cnt32); 5249 return Cnt64; 5250 } 5251 5252 if (!ST->hasV6T2Ops()) 5253 return SDValue(); 5254 5255 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0)); 5256 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 5257 } 5258 5259 /// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count 5260 /// for each 16-bit element from operand, repeated. The basic idea is to 5261 /// leverage vcnt to get the 8-bit counts, gather and add the results. 5262 /// 5263 /// Trace for v4i16: 5264 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 5265 /// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element) 5266 /// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi) 5267 /// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6] 5268 /// [b0 b1 b2 b3 b4 b5 b6 b7] 5269 /// +[b1 b0 b3 b2 b5 b4 b7 b6] 5270 /// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0, 5271 /// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits) 5272 static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) { 5273 EVT VT = N->getValueType(0); 5274 SDLoc DL(N); 5275 5276 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 5277 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0)); 5278 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0); 5279 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1); 5280 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2); 5281 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3); 5282 } 5283 5284 /// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the 5285 /// bit-count for each 16-bit element from the operand. We need slightly 5286 /// different sequencing for v4i16 and v8i16 to stay within NEON's available 5287 /// 64/128-bit registers. 5288 /// 5289 /// Trace for v4i16: 5290 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 5291 /// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi) 5292 /// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ] 5293 /// v4i16:Extracted = [k0 k1 k2 k3 ] 5294 static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) { 5295 EVT VT = N->getValueType(0); 5296 SDLoc DL(N); 5297 5298 SDValue BitCounts = getCTPOP16BitCounts(N, DAG); 5299 if (VT.is64BitVector()) { 5300 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts); 5301 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended, 5302 DAG.getIntPtrConstant(0, DL)); 5303 } else { 5304 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8, 5305 BitCounts, DAG.getIntPtrConstant(0, DL)); 5306 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted); 5307 } 5308 } 5309 5310 /// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the 5311 /// bit-count for each 32-bit element from the operand. The idea here is 5312 /// to split the vector into 16-bit elements, leverage the 16-bit count 5313 /// routine, and then combine the results. 5314 /// 5315 /// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged): 5316 /// input = [v0 v1 ] (vi: 32-bit elements) 5317 /// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1]) 5318 /// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi) 5319 /// vrev: N0 = [k1 k0 k3 k2 ] 5320 /// [k0 k1 k2 k3 ] 5321 /// N1 =+[k1 k0 k3 k2 ] 5322 /// [k0 k2 k1 k3 ] 5323 /// N2 =+[k1 k3 k0 k2 ] 5324 /// [k0 k2 k1 k3 ] 5325 /// Extended =+[k1 k3 k0 k2 ] 5326 /// [k0 k2 ] 5327 /// Extracted=+[k1 k3 ] 5328 /// 5329 static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) { 5330 EVT VT = N->getValueType(0); 5331 SDLoc DL(N); 5332 5333 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 5334 5335 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0)); 5336 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG); 5337 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16); 5338 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0); 5339 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1); 5340 5341 if (VT.is64BitVector()) { 5342 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2); 5343 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended, 5344 DAG.getIntPtrConstant(0, DL)); 5345 } else { 5346 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2, 5347 DAG.getIntPtrConstant(0, DL)); 5348 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted); 5349 } 5350 } 5351 5352 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 5353 const ARMSubtarget *ST) { 5354 EVT VT = N->getValueType(0); 5355 5356 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 5357 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || 5358 VT == MVT::v4i16 || VT == MVT::v8i16) && 5359 "Unexpected type for custom ctpop lowering"); 5360 5361 if (VT.getVectorElementType() == MVT::i32) 5362 return lowerCTPOP32BitElements(N, DAG); 5363 else 5364 return lowerCTPOP16BitElements(N, DAG); 5365 } 5366 5367 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 5368 const ARMSubtarget *ST) { 5369 EVT VT = N->getValueType(0); 5370 SDLoc dl(N); 5371 5372 if (!VT.isVector()) 5373 return SDValue(); 5374 5375 // Lower vector shifts on NEON to use VSHL. 5376 assert(ST->hasNEON() && "unexpected vector shift"); 5377 5378 // Left shifts translate directly to the vshiftu intrinsic. 5379 if (N->getOpcode() == ISD::SHL) 5380 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 5381 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl, 5382 MVT::i32), 5383 N->getOperand(0), N->getOperand(1)); 5384 5385 assert((N->getOpcode() == ISD::SRA || 5386 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 5387 5388 // NEON uses the same intrinsics for both left and right shifts. For 5389 // right shifts, the shift amounts are negative, so negate the vector of 5390 // shift amounts. 5391 EVT ShiftVT = N->getOperand(1).getValueType(); 5392 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 5393 getZeroVector(ShiftVT, DAG, dl), 5394 N->getOperand(1)); 5395 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 5396 Intrinsic::arm_neon_vshifts : 5397 Intrinsic::arm_neon_vshiftu); 5398 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 5399 DAG.getConstant(vshiftInt, dl, MVT::i32), 5400 N->getOperand(0), NegatedCount); 5401 } 5402 5403 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 5404 const ARMSubtarget *ST) { 5405 EVT VT = N->getValueType(0); 5406 SDLoc dl(N); 5407 5408 // We can get here for a node like i32 = ISD::SHL i32, i64 5409 if (VT != MVT::i64) 5410 return SDValue(); 5411 5412 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 5413 "Unknown shift to lower!"); 5414 5415 // We only lower SRA, SRL of 1 here, all others use generic lowering. 5416 if (!isOneConstant(N->getOperand(1))) 5417 return SDValue(); 5418 5419 // If we are in thumb mode, we don't have RRX. 5420 if (ST->isThumb1Only()) return SDValue(); 5421 5422 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 5423 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 5424 DAG.getConstant(0, dl, MVT::i32)); 5425 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 5426 DAG.getConstant(1, dl, MVT::i32)); 5427 5428 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 5429 // captures the result into a carry flag. 5430 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 5431 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi); 5432 5433 // The low part is an ARMISD::RRX operand, which shifts the carry in. 5434 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 5435 5436 // Merge the pieces into a single i64 value. 5437 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 5438 } 5439 5440 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 5441 SDValue TmpOp0, TmpOp1; 5442 bool Invert = false; 5443 bool Swap = false; 5444 unsigned Opc = 0; 5445 5446 SDValue Op0 = Op.getOperand(0); 5447 SDValue Op1 = Op.getOperand(1); 5448 SDValue CC = Op.getOperand(2); 5449 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger(); 5450 EVT VT = Op.getValueType(); 5451 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 5452 SDLoc dl(Op); 5453 5454 if (Op0.getValueType().getVectorElementType() == MVT::i64 && 5455 (SetCCOpcode == ISD::SETEQ || SetCCOpcode == ISD::SETNE)) { 5456 // Special-case integer 64-bit equality comparisons. They aren't legal, 5457 // but they can be lowered with a few vector instructions. 5458 unsigned CmpElements = CmpVT.getVectorNumElements() * 2; 5459 EVT SplitVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, CmpElements); 5460 SDValue CastOp0 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op0); 5461 SDValue CastOp1 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op1); 5462 SDValue Cmp = DAG.getNode(ISD::SETCC, dl, SplitVT, CastOp0, CastOp1, 5463 DAG.getCondCode(ISD::SETEQ)); 5464 SDValue Reversed = DAG.getNode(ARMISD::VREV64, dl, SplitVT, Cmp); 5465 SDValue Merged = DAG.getNode(ISD::AND, dl, SplitVT, Cmp, Reversed); 5466 Merged = DAG.getNode(ISD::BITCAST, dl, CmpVT, Merged); 5467 if (SetCCOpcode == ISD::SETNE) 5468 Merged = DAG.getNOT(dl, Merged, CmpVT); 5469 Merged = DAG.getSExtOrTrunc(Merged, dl, VT); 5470 return Merged; 5471 } 5472 5473 if (CmpVT.getVectorElementType() == MVT::i64) 5474 // 64-bit comparisons are not legal in general. 5475 return SDValue(); 5476 5477 if (Op1.getValueType().isFloatingPoint()) { 5478 switch (SetCCOpcode) { 5479 default: llvm_unreachable("Illegal FP comparison"); 5480 case ISD::SETUNE: 5481 case ISD::SETNE: Invert = true; LLVM_FALLTHROUGH; 5482 case ISD::SETOEQ: 5483 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 5484 case ISD::SETOLT: 5485 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH; 5486 case ISD::SETOGT: 5487 case ISD::SETGT: Opc = ARMISD::VCGT; break; 5488 case ISD::SETOLE: 5489 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH; 5490 case ISD::SETOGE: 5491 case ISD::SETGE: Opc = ARMISD::VCGE; break; 5492 case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH; 5493 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 5494 case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH; 5495 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 5496 case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH; 5497 case ISD::SETONE: 5498 // Expand this to (OLT | OGT). 5499 TmpOp0 = Op0; 5500 TmpOp1 = Op1; 5501 Opc = ISD::OR; 5502 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 5503 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1); 5504 break; 5505 case ISD::SETUO: 5506 Invert = true; 5507 LLVM_FALLTHROUGH; 5508 case ISD::SETO: 5509 // Expand this to (OLT | OGE). 5510 TmpOp0 = Op0; 5511 TmpOp1 = Op1; 5512 Opc = ISD::OR; 5513 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 5514 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1); 5515 break; 5516 } 5517 } else { 5518 // Integer comparisons. 5519 switch (SetCCOpcode) { 5520 default: llvm_unreachable("Illegal integer comparison"); 5521 case ISD::SETNE: Invert = true; LLVM_FALLTHROUGH; 5522 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 5523 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH; 5524 case ISD::SETGT: Opc = ARMISD::VCGT; break; 5525 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH; 5526 case ISD::SETGE: Opc = ARMISD::VCGE; break; 5527 case ISD::SETULT: Swap = true; LLVM_FALLTHROUGH; 5528 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 5529 case ISD::SETULE: Swap = true; LLVM_FALLTHROUGH; 5530 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 5531 } 5532 5533 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 5534 if (Opc == ARMISD::VCEQ) { 5535 SDValue AndOp; 5536 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 5537 AndOp = Op0; 5538 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 5539 AndOp = Op1; 5540 5541 // Ignore bitconvert. 5542 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 5543 AndOp = AndOp.getOperand(0); 5544 5545 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 5546 Opc = ARMISD::VTST; 5547 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0)); 5548 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1)); 5549 Invert = !Invert; 5550 } 5551 } 5552 } 5553 5554 if (Swap) 5555 std::swap(Op0, Op1); 5556 5557 // If one of the operands is a constant vector zero, attempt to fold the 5558 // comparison to a specialized compare-against-zero form. 5559 SDValue SingleOp; 5560 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 5561 SingleOp = Op0; 5562 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 5563 if (Opc == ARMISD::VCGE) 5564 Opc = ARMISD::VCLEZ; 5565 else if (Opc == ARMISD::VCGT) 5566 Opc = ARMISD::VCLTZ; 5567 SingleOp = Op1; 5568 } 5569 5570 SDValue Result; 5571 if (SingleOp.getNode()) { 5572 switch (Opc) { 5573 case ARMISD::VCEQ: 5574 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break; 5575 case ARMISD::VCGE: 5576 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break; 5577 case ARMISD::VCLEZ: 5578 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break; 5579 case ARMISD::VCGT: 5580 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break; 5581 case ARMISD::VCLTZ: 5582 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break; 5583 default: 5584 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 5585 } 5586 } else { 5587 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 5588 } 5589 5590 Result = DAG.getSExtOrTrunc(Result, dl, VT); 5591 5592 if (Invert) 5593 Result = DAG.getNOT(dl, Result, VT); 5594 5595 return Result; 5596 } 5597 5598 static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) { 5599 SDValue LHS = Op.getOperand(0); 5600 SDValue RHS = Op.getOperand(1); 5601 SDValue Carry = Op.getOperand(2); 5602 SDValue Cond = Op.getOperand(3); 5603 SDLoc DL(Op); 5604 5605 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only."); 5606 5607 assert(Carry.getOpcode() != ISD::CARRY_FALSE); 5608 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); 5609 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry); 5610 5611 SDValue FVal = DAG.getConstant(0, DL, MVT::i32); 5612 SDValue TVal = DAG.getConstant(1, DL, MVT::i32); 5613 SDValue ARMcc = DAG.getConstant( 5614 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32); 5615 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 5616 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR, 5617 Cmp.getValue(1), SDValue()); 5618 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc, 5619 CCR, Chain.getValue(1)); 5620 } 5621 5622 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 5623 /// valid vector constant for a NEON instruction with a "modified immediate" 5624 /// operand (e.g., VMOV). If so, return the encoded value. 5625 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 5626 unsigned SplatBitSize, SelectionDAG &DAG, 5627 const SDLoc &dl, EVT &VT, bool is128Bits, 5628 NEONModImmType type) { 5629 unsigned OpCmode, Imm; 5630 5631 // SplatBitSize is set to the smallest size that splats the vector, so a 5632 // zero vector will always have SplatBitSize == 8. However, NEON modified 5633 // immediate instructions others than VMOV do not support the 8-bit encoding 5634 // of a zero vector, and the default encoding of zero is supposed to be the 5635 // 32-bit version. 5636 if (SplatBits == 0) 5637 SplatBitSize = 32; 5638 5639 switch (SplatBitSize) { 5640 case 8: 5641 if (type != VMOVModImm) 5642 return SDValue(); 5643 // Any 1-byte value is OK. Op=0, Cmode=1110. 5644 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 5645 OpCmode = 0xe; 5646 Imm = SplatBits; 5647 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 5648 break; 5649 5650 case 16: 5651 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 5652 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 5653 if ((SplatBits & ~0xff) == 0) { 5654 // Value = 0x00nn: Op=x, Cmode=100x. 5655 OpCmode = 0x8; 5656 Imm = SplatBits; 5657 break; 5658 } 5659 if ((SplatBits & ~0xff00) == 0) { 5660 // Value = 0xnn00: Op=x, Cmode=101x. 5661 OpCmode = 0xa; 5662 Imm = SplatBits >> 8; 5663 break; 5664 } 5665 return SDValue(); 5666 5667 case 32: 5668 // NEON's 32-bit VMOV supports splat values where: 5669 // * only one byte is nonzero, or 5670 // * the least significant byte is 0xff and the second byte is nonzero, or 5671 // * the least significant 2 bytes are 0xff and the third is nonzero. 5672 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 5673 if ((SplatBits & ~0xff) == 0) { 5674 // Value = 0x000000nn: Op=x, Cmode=000x. 5675 OpCmode = 0; 5676 Imm = SplatBits; 5677 break; 5678 } 5679 if ((SplatBits & ~0xff00) == 0) { 5680 // Value = 0x0000nn00: Op=x, Cmode=001x. 5681 OpCmode = 0x2; 5682 Imm = SplatBits >> 8; 5683 break; 5684 } 5685 if ((SplatBits & ~0xff0000) == 0) { 5686 // Value = 0x00nn0000: Op=x, Cmode=010x. 5687 OpCmode = 0x4; 5688 Imm = SplatBits >> 16; 5689 break; 5690 } 5691 if ((SplatBits & ~0xff000000) == 0) { 5692 // Value = 0xnn000000: Op=x, Cmode=011x. 5693 OpCmode = 0x6; 5694 Imm = SplatBits >> 24; 5695 break; 5696 } 5697 5698 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 5699 if (type == OtherModImm) return SDValue(); 5700 5701 if ((SplatBits & ~0xffff) == 0 && 5702 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 5703 // Value = 0x0000nnff: Op=x, Cmode=1100. 5704 OpCmode = 0xc; 5705 Imm = SplatBits >> 8; 5706 break; 5707 } 5708 5709 if ((SplatBits & ~0xffffff) == 0 && 5710 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 5711 // Value = 0x00nnffff: Op=x, Cmode=1101. 5712 OpCmode = 0xd; 5713 Imm = SplatBits >> 16; 5714 break; 5715 } 5716 5717 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 5718 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 5719 // VMOV.I32. A (very) minor optimization would be to replicate the value 5720 // and fall through here to test for a valid 64-bit splat. But, then the 5721 // caller would also need to check and handle the change in size. 5722 return SDValue(); 5723 5724 case 64: { 5725 if (type != VMOVModImm) 5726 return SDValue(); 5727 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 5728 uint64_t BitMask = 0xff; 5729 uint64_t Val = 0; 5730 unsigned ImmMask = 1; 5731 Imm = 0; 5732 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 5733 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 5734 Val |= BitMask; 5735 Imm |= ImmMask; 5736 } else if ((SplatBits & BitMask) != 0) { 5737 return SDValue(); 5738 } 5739 BitMask <<= 8; 5740 ImmMask <<= 1; 5741 } 5742 5743 if (DAG.getDataLayout().isBigEndian()) 5744 // swap higher and lower 32 bit word 5745 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4); 5746 5747 // Op=1, Cmode=1110. 5748 OpCmode = 0x1e; 5749 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 5750 break; 5751 } 5752 5753 default: 5754 llvm_unreachable("unexpected size for isNEONModifiedImm"); 5755 } 5756 5757 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 5758 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32); 5759 } 5760 5761 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 5762 const ARMSubtarget *ST) const { 5763 bool IsDouble = Op.getValueType() == MVT::f64; 5764 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 5765 const APFloat &FPVal = CFP->getValueAPF(); 5766 5767 // Prevent floating-point constants from using literal loads 5768 // when execute-only is enabled. 5769 if (ST->genExecuteOnly()) { 5770 APInt INTVal = FPVal.bitcastToAPInt(); 5771 SDLoc DL(CFP); 5772 if (IsDouble) { 5773 SDValue Lo = DAG.getConstant(INTVal.trunc(32), DL, MVT::i32); 5774 SDValue Hi = DAG.getConstant(INTVal.lshr(32).trunc(32), DL, MVT::i32); 5775 if (!ST->isLittle()) 5776 std::swap(Lo, Hi); 5777 return DAG.getNode(ARMISD::VMOVDRR, DL, MVT::f64, Lo, Hi); 5778 } else { 5779 return DAG.getConstant(INTVal, DL, MVT::i32); 5780 } 5781 } 5782 5783 if (!ST->hasVFP3()) 5784 return SDValue(); 5785 5786 // Use the default (constant pool) lowering for double constants when we have 5787 // an SP-only FPU 5788 if (IsDouble && Subtarget->isFPOnlySP()) 5789 return SDValue(); 5790 5791 // Try splatting with a VMOV.f32... 5792 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 5793 5794 if (ImmVal != -1) { 5795 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 5796 // We have code in place to select a valid ConstantFP already, no need to 5797 // do any mangling. 5798 return Op; 5799 } 5800 5801 // It's a float and we are trying to use NEON operations where 5802 // possible. Lower it to a splat followed by an extract. 5803 SDLoc DL(Op); 5804 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32); 5805 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 5806 NewVal); 5807 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 5808 DAG.getConstant(0, DL, MVT::i32)); 5809 } 5810 5811 // The rest of our options are NEON only, make sure that's allowed before 5812 // proceeding.. 5813 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 5814 return SDValue(); 5815 5816 EVT VMovVT; 5817 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 5818 5819 // It wouldn't really be worth bothering for doubles except for one very 5820 // important value, which does happen to match: 0.0. So make sure we don't do 5821 // anything stupid. 5822 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 5823 return SDValue(); 5824 5825 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 5826 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), 5827 VMovVT, false, VMOVModImm); 5828 if (NewVal != SDValue()) { 5829 SDLoc DL(Op); 5830 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 5831 NewVal); 5832 if (IsDouble) 5833 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 5834 5835 // It's a float: cast and extract a vector element. 5836 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 5837 VecConstant); 5838 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 5839 DAG.getConstant(0, DL, MVT::i32)); 5840 } 5841 5842 // Finally, try a VMVN.i32 5843 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT, 5844 false, VMVNModImm); 5845 if (NewVal != SDValue()) { 5846 SDLoc DL(Op); 5847 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 5848 5849 if (IsDouble) 5850 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 5851 5852 // It's a float: cast and extract a vector element. 5853 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 5854 VecConstant); 5855 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 5856 DAG.getConstant(0, DL, MVT::i32)); 5857 } 5858 5859 return SDValue(); 5860 } 5861 5862 // check if an VEXT instruction can handle the shuffle mask when the 5863 // vector sources of the shuffle are the same. 5864 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 5865 unsigned NumElts = VT.getVectorNumElements(); 5866 5867 // Assume that the first shuffle index is not UNDEF. Fail if it is. 5868 if (M[0] < 0) 5869 return false; 5870 5871 Imm = M[0]; 5872 5873 // If this is a VEXT shuffle, the immediate value is the index of the first 5874 // element. The other shuffle indices must be the successive elements after 5875 // the first one. 5876 unsigned ExpectedElt = Imm; 5877 for (unsigned i = 1; i < NumElts; ++i) { 5878 // Increment the expected index. If it wraps around, just follow it 5879 // back to index zero and keep going. 5880 ++ExpectedElt; 5881 if (ExpectedElt == NumElts) 5882 ExpectedElt = 0; 5883 5884 if (M[i] < 0) continue; // ignore UNDEF indices 5885 if (ExpectedElt != static_cast<unsigned>(M[i])) 5886 return false; 5887 } 5888 5889 return true; 5890 } 5891 5892 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 5893 bool &ReverseVEXT, unsigned &Imm) { 5894 unsigned NumElts = VT.getVectorNumElements(); 5895 ReverseVEXT = false; 5896 5897 // Assume that the first shuffle index is not UNDEF. Fail if it is. 5898 if (M[0] < 0) 5899 return false; 5900 5901 Imm = M[0]; 5902 5903 // If this is a VEXT shuffle, the immediate value is the index of the first 5904 // element. The other shuffle indices must be the successive elements after 5905 // the first one. 5906 unsigned ExpectedElt = Imm; 5907 for (unsigned i = 1; i < NumElts; ++i) { 5908 // Increment the expected index. If it wraps around, it may still be 5909 // a VEXT but the source vectors must be swapped. 5910 ExpectedElt += 1; 5911 if (ExpectedElt == NumElts * 2) { 5912 ExpectedElt = 0; 5913 ReverseVEXT = true; 5914 } 5915 5916 if (M[i] < 0) continue; // ignore UNDEF indices 5917 if (ExpectedElt != static_cast<unsigned>(M[i])) 5918 return false; 5919 } 5920 5921 // Adjust the index value if the source operands will be swapped. 5922 if (ReverseVEXT) 5923 Imm -= NumElts; 5924 5925 return true; 5926 } 5927 5928 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 5929 /// instruction with the specified blocksize. (The order of the elements 5930 /// within each block of the vector is reversed.) 5931 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 5932 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 5933 "Only possible block sizes for VREV are: 16, 32, 64"); 5934 5935 unsigned EltSz = VT.getScalarSizeInBits(); 5936 if (EltSz == 64) 5937 return false; 5938 5939 unsigned NumElts = VT.getVectorNumElements(); 5940 unsigned BlockElts = M[0] + 1; 5941 // If the first shuffle index is UNDEF, be optimistic. 5942 if (M[0] < 0) 5943 BlockElts = BlockSize / EltSz; 5944 5945 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 5946 return false; 5947 5948 for (unsigned i = 0; i < NumElts; ++i) { 5949 if (M[i] < 0) continue; // ignore UNDEF indices 5950 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 5951 return false; 5952 } 5953 5954 return true; 5955 } 5956 5957 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 5958 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 5959 // range, then 0 is placed into the resulting vector. So pretty much any mask 5960 // of 8 elements can work here. 5961 return VT == MVT::v8i8 && M.size() == 8; 5962 } 5963 5964 static unsigned SelectPairHalf(unsigned Elements, ArrayRef<int> Mask, 5965 unsigned Index) { 5966 if (Mask.size() == Elements * 2) 5967 return Index / Elements; 5968 return Mask[Index] == 0 ? 0 : 1; 5969 } 5970 5971 // Checks whether the shuffle mask represents a vector transpose (VTRN) by 5972 // checking that pairs of elements in the shuffle mask represent the same index 5973 // in each vector, incrementing the expected index by 2 at each step. 5974 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6] 5975 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g} 5976 // v2={e,f,g,h} 5977 // WhichResult gives the offset for each element in the mask based on which 5978 // of the two results it belongs to. 5979 // 5980 // The transpose can be represented either as: 5981 // result1 = shufflevector v1, v2, result1_shuffle_mask 5982 // result2 = shufflevector v1, v2, result2_shuffle_mask 5983 // where v1/v2 and the shuffle masks have the same number of elements 5984 // (here WhichResult (see below) indicates which result is being checked) 5985 // 5986 // or as: 5987 // results = shufflevector v1, v2, shuffle_mask 5988 // where both results are returned in one vector and the shuffle mask has twice 5989 // as many elements as v1/v2 (here WhichResult will always be 0 if true) here we 5990 // want to check the low half and high half of the shuffle mask as if it were 5991 // the other case 5992 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5993 unsigned EltSz = VT.getScalarSizeInBits(); 5994 if (EltSz == 64) 5995 return false; 5996 5997 unsigned NumElts = VT.getVectorNumElements(); 5998 if (M.size() != NumElts && M.size() != NumElts*2) 5999 return false; 6000 6001 // If the mask is twice as long as the input vector then we need to check the 6002 // upper and lower parts of the mask with a matching value for WhichResult 6003 // FIXME: A mask with only even values will be rejected in case the first 6004 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only 6005 // M[0] is used to determine WhichResult 6006 for (unsigned i = 0; i < M.size(); i += NumElts) { 6007 WhichResult = SelectPairHalf(NumElts, M, i); 6008 for (unsigned j = 0; j < NumElts; j += 2) { 6009 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 6010 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult)) 6011 return false; 6012 } 6013 } 6014 6015 if (M.size() == NumElts*2) 6016 WhichResult = 0; 6017 6018 return true; 6019 } 6020 6021 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 6022 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6023 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 6024 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6025 unsigned EltSz = VT.getScalarSizeInBits(); 6026 if (EltSz == 64) 6027 return false; 6028 6029 unsigned NumElts = VT.getVectorNumElements(); 6030 if (M.size() != NumElts && M.size() != NumElts*2) 6031 return false; 6032 6033 for (unsigned i = 0; i < M.size(); i += NumElts) { 6034 WhichResult = SelectPairHalf(NumElts, M, i); 6035 for (unsigned j = 0; j < NumElts; j += 2) { 6036 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 6037 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult)) 6038 return false; 6039 } 6040 } 6041 6042 if (M.size() == NumElts*2) 6043 WhichResult = 0; 6044 6045 return true; 6046 } 6047 6048 // Checks whether the shuffle mask represents a vector unzip (VUZP) by checking 6049 // that the mask elements are either all even and in steps of size 2 or all odd 6050 // and in steps of size 2. 6051 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6] 6052 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g} 6053 // v2={e,f,g,h} 6054 // Requires similar checks to that of isVTRNMask with 6055 // respect the how results are returned. 6056 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6057 unsigned EltSz = VT.getScalarSizeInBits(); 6058 if (EltSz == 64) 6059 return false; 6060 6061 unsigned NumElts = VT.getVectorNumElements(); 6062 if (M.size() != NumElts && M.size() != NumElts*2) 6063 return false; 6064 6065 for (unsigned i = 0; i < M.size(); i += NumElts) { 6066 WhichResult = SelectPairHalf(NumElts, M, i); 6067 for (unsigned j = 0; j < NumElts; ++j) { 6068 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult) 6069 return false; 6070 } 6071 } 6072 6073 if (M.size() == NumElts*2) 6074 WhichResult = 0; 6075 6076 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6077 if (VT.is64BitVector() && EltSz == 32) 6078 return false; 6079 6080 return true; 6081 } 6082 6083 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 6084 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6085 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 6086 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6087 unsigned EltSz = VT.getScalarSizeInBits(); 6088 if (EltSz == 64) 6089 return false; 6090 6091 unsigned NumElts = VT.getVectorNumElements(); 6092 if (M.size() != NumElts && M.size() != NumElts*2) 6093 return false; 6094 6095 unsigned Half = NumElts / 2; 6096 for (unsigned i = 0; i < M.size(); i += NumElts) { 6097 WhichResult = SelectPairHalf(NumElts, M, i); 6098 for (unsigned j = 0; j < NumElts; j += Half) { 6099 unsigned Idx = WhichResult; 6100 for (unsigned k = 0; k < Half; ++k) { 6101 int MIdx = M[i + j + k]; 6102 if (MIdx >= 0 && (unsigned) MIdx != Idx) 6103 return false; 6104 Idx += 2; 6105 } 6106 } 6107 } 6108 6109 if (M.size() == NumElts*2) 6110 WhichResult = 0; 6111 6112 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6113 if (VT.is64BitVector() && EltSz == 32) 6114 return false; 6115 6116 return true; 6117 } 6118 6119 // Checks whether the shuffle mask represents a vector zip (VZIP) by checking 6120 // that pairs of elements of the shufflemask represent the same index in each 6121 // vector incrementing sequentially through the vectors. 6122 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5] 6123 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f} 6124 // v2={e,f,g,h} 6125 // Requires similar checks to that of isVTRNMask with respect the how results 6126 // are returned. 6127 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 6128 unsigned EltSz = VT.getScalarSizeInBits(); 6129 if (EltSz == 64) 6130 return false; 6131 6132 unsigned NumElts = VT.getVectorNumElements(); 6133 if (M.size() != NumElts && M.size() != NumElts*2) 6134 return false; 6135 6136 for (unsigned i = 0; i < M.size(); i += NumElts) { 6137 WhichResult = SelectPairHalf(NumElts, M, i); 6138 unsigned Idx = WhichResult * NumElts / 2; 6139 for (unsigned j = 0; j < NumElts; j += 2) { 6140 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 6141 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts)) 6142 return false; 6143 Idx += 1; 6144 } 6145 } 6146 6147 if (M.size() == NumElts*2) 6148 WhichResult = 0; 6149 6150 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6151 if (VT.is64BitVector() && EltSz == 32) 6152 return false; 6153 6154 return true; 6155 } 6156 6157 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 6158 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 6159 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 6160 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 6161 unsigned EltSz = VT.getScalarSizeInBits(); 6162 if (EltSz == 64) 6163 return false; 6164 6165 unsigned NumElts = VT.getVectorNumElements(); 6166 if (M.size() != NumElts && M.size() != NumElts*2) 6167 return false; 6168 6169 for (unsigned i = 0; i < M.size(); i += NumElts) { 6170 WhichResult = SelectPairHalf(NumElts, M, i); 6171 unsigned Idx = WhichResult * NumElts / 2; 6172 for (unsigned j = 0; j < NumElts; j += 2) { 6173 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 6174 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx)) 6175 return false; 6176 Idx += 1; 6177 } 6178 } 6179 6180 if (M.size() == NumElts*2) 6181 WhichResult = 0; 6182 6183 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 6184 if (VT.is64BitVector() && EltSz == 32) 6185 return false; 6186 6187 return true; 6188 } 6189 6190 /// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN), 6191 /// and return the corresponding ARMISD opcode if it is, or 0 if it isn't. 6192 static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT, 6193 unsigned &WhichResult, 6194 bool &isV_UNDEF) { 6195 isV_UNDEF = false; 6196 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 6197 return ARMISD::VTRN; 6198 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 6199 return ARMISD::VUZP; 6200 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 6201 return ARMISD::VZIP; 6202 6203 isV_UNDEF = true; 6204 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 6205 return ARMISD::VTRN; 6206 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 6207 return ARMISD::VUZP; 6208 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 6209 return ARMISD::VZIP; 6210 6211 return 0; 6212 } 6213 6214 /// \return true if this is a reverse operation on an vector. 6215 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 6216 unsigned NumElts = VT.getVectorNumElements(); 6217 // Make sure the mask has the right size. 6218 if (NumElts != M.size()) 6219 return false; 6220 6221 // Look for <15, ..., 3, -1, 1, 0>. 6222 for (unsigned i = 0; i != NumElts; ++i) 6223 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 6224 return false; 6225 6226 return true; 6227 } 6228 6229 // If N is an integer constant that can be moved into a register in one 6230 // instruction, return an SDValue of such a constant (will become a MOV 6231 // instruction). Otherwise return null. 6232 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 6233 const ARMSubtarget *ST, const SDLoc &dl) { 6234 uint64_t Val; 6235 if (!isa<ConstantSDNode>(N)) 6236 return SDValue(); 6237 Val = cast<ConstantSDNode>(N)->getZExtValue(); 6238 6239 if (ST->isThumb1Only()) { 6240 if (Val <= 255 || ~Val <= 255) 6241 return DAG.getConstant(Val, dl, MVT::i32); 6242 } else { 6243 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 6244 return DAG.getConstant(Val, dl, MVT::i32); 6245 } 6246 return SDValue(); 6247 } 6248 6249 // If this is a case we can't handle, return null and let the default 6250 // expansion code take care of it. 6251 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 6252 const ARMSubtarget *ST) const { 6253 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 6254 SDLoc dl(Op); 6255 EVT VT = Op.getValueType(); 6256 6257 APInt SplatBits, SplatUndef; 6258 unsigned SplatBitSize; 6259 bool HasAnyUndefs; 6260 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 6261 if (SplatUndef.isAllOnesValue()) 6262 return DAG.getUNDEF(VT); 6263 6264 if (SplatBitSize <= 64) { 6265 // Check if an immediate VMOV works. 6266 EVT VmovVT; 6267 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 6268 SplatUndef.getZExtValue(), SplatBitSize, 6269 DAG, dl, VmovVT, VT.is128BitVector(), 6270 VMOVModImm); 6271 if (Val.getNode()) { 6272 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 6273 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 6274 } 6275 6276 // Try an immediate VMVN. 6277 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 6278 Val = isNEONModifiedImm(NegatedImm, 6279 SplatUndef.getZExtValue(), SplatBitSize, 6280 DAG, dl, VmovVT, VT.is128BitVector(), 6281 VMVNModImm); 6282 if (Val.getNode()) { 6283 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 6284 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 6285 } 6286 6287 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 6288 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 6289 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 6290 if (ImmVal != -1) { 6291 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32); 6292 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 6293 } 6294 } 6295 } 6296 } 6297 6298 // Scan through the operands to see if only one value is used. 6299 // 6300 // As an optimisation, even if more than one value is used it may be more 6301 // profitable to splat with one value then change some lanes. 6302 // 6303 // Heuristically we decide to do this if the vector has a "dominant" value, 6304 // defined as splatted to more than half of the lanes. 6305 unsigned NumElts = VT.getVectorNumElements(); 6306 bool isOnlyLowElement = true; 6307 bool usesOnlyOneValue = true; 6308 bool hasDominantValue = false; 6309 bool isConstant = true; 6310 6311 // Map of the number of times a particular SDValue appears in the 6312 // element list. 6313 DenseMap<SDValue, unsigned> ValueCounts; 6314 SDValue Value; 6315 for (unsigned i = 0; i < NumElts; ++i) { 6316 SDValue V = Op.getOperand(i); 6317 if (V.isUndef()) 6318 continue; 6319 if (i > 0) 6320 isOnlyLowElement = false; 6321 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 6322 isConstant = false; 6323 6324 ValueCounts.insert(std::make_pair(V, 0)); 6325 unsigned &Count = ValueCounts[V]; 6326 6327 // Is this value dominant? (takes up more than half of the lanes) 6328 if (++Count > (NumElts / 2)) { 6329 hasDominantValue = true; 6330 Value = V; 6331 } 6332 } 6333 if (ValueCounts.size() != 1) 6334 usesOnlyOneValue = false; 6335 if (!Value.getNode() && !ValueCounts.empty()) 6336 Value = ValueCounts.begin()->first; 6337 6338 if (ValueCounts.empty()) 6339 return DAG.getUNDEF(VT); 6340 6341 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 6342 // Keep going if we are hitting this case. 6343 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 6344 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 6345 6346 unsigned EltSize = VT.getScalarSizeInBits(); 6347 6348 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 6349 // i32 and try again. 6350 if (hasDominantValue && EltSize <= 32) { 6351 if (!isConstant) { 6352 SDValue N; 6353 6354 // If we are VDUPing a value that comes directly from a vector, that will 6355 // cause an unnecessary move to and from a GPR, where instead we could 6356 // just use VDUPLANE. We can only do this if the lane being extracted 6357 // is at a constant index, as the VDUP from lane instructions only have 6358 // constant-index forms. 6359 ConstantSDNode *constIndex; 6360 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 6361 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) { 6362 // We need to create a new undef vector to use for the VDUPLANE if the 6363 // size of the vector from which we get the value is different than the 6364 // size of the vector that we need to create. We will insert the element 6365 // such that the register coalescer will remove unnecessary copies. 6366 if (VT != Value->getOperand(0).getValueType()) { 6367 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 6368 VT.getVectorNumElements(); 6369 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6370 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 6371 Value, DAG.getConstant(index, dl, MVT::i32)), 6372 DAG.getConstant(index, dl, MVT::i32)); 6373 } else 6374 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6375 Value->getOperand(0), Value->getOperand(1)); 6376 } else 6377 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 6378 6379 if (!usesOnlyOneValue) { 6380 // The dominant value was splatted as 'N', but we now have to insert 6381 // all differing elements. 6382 for (unsigned I = 0; I < NumElts; ++I) { 6383 if (Op.getOperand(I) == Value) 6384 continue; 6385 SmallVector<SDValue, 3> Ops; 6386 Ops.push_back(N); 6387 Ops.push_back(Op.getOperand(I)); 6388 Ops.push_back(DAG.getConstant(I, dl, MVT::i32)); 6389 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops); 6390 } 6391 } 6392 return N; 6393 } 6394 if (VT.getVectorElementType().isFloatingPoint()) { 6395 SmallVector<SDValue, 8> Ops; 6396 for (unsigned i = 0; i < NumElts; ++i) 6397 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 6398 Op.getOperand(i))); 6399 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 6400 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops); 6401 Val = LowerBUILD_VECTOR(Val, DAG, ST); 6402 if (Val.getNode()) 6403 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6404 } 6405 if (usesOnlyOneValue) { 6406 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 6407 if (isConstant && Val.getNode()) 6408 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 6409 } 6410 } 6411 6412 // If all elements are constants and the case above didn't get hit, fall back 6413 // to the default expansion, which will generate a load from the constant 6414 // pool. 6415 if (isConstant) 6416 return SDValue(); 6417 6418 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 6419 if (NumElts >= 4) { 6420 SDValue shuffle = ReconstructShuffle(Op, DAG); 6421 if (shuffle != SDValue()) 6422 return shuffle; 6423 } 6424 6425 if (VT.is128BitVector() && VT != MVT::v2f64 && VT != MVT::v4f32) { 6426 // If we haven't found an efficient lowering, try splitting a 128-bit vector 6427 // into two 64-bit vectors; we might discover a better way to lower it. 6428 SmallVector<SDValue, 64> Ops(Op->op_begin(), Op->op_begin() + NumElts); 6429 EVT ExtVT = VT.getVectorElementType(); 6430 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElts / 2); 6431 SDValue Lower = 6432 DAG.getBuildVector(HVT, dl, makeArrayRef(&Ops[0], NumElts / 2)); 6433 if (Lower.getOpcode() == ISD::BUILD_VECTOR) 6434 Lower = LowerBUILD_VECTOR(Lower, DAG, ST); 6435 SDValue Upper = DAG.getBuildVector( 6436 HVT, dl, makeArrayRef(&Ops[NumElts / 2], NumElts / 2)); 6437 if (Upper.getOpcode() == ISD::BUILD_VECTOR) 6438 Upper = LowerBUILD_VECTOR(Upper, DAG, ST); 6439 if (Lower && Upper) 6440 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lower, Upper); 6441 } 6442 6443 // Vectors with 32- or 64-bit elements can be built by directly assigning 6444 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 6445 // will be legalized. 6446 if (EltSize >= 32) { 6447 // Do the expansion with floating-point types, since that is what the VFP 6448 // registers are defined to use, and since i64 is not legal. 6449 EVT EltVT = EVT::getFloatingPointVT(EltSize); 6450 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 6451 SmallVector<SDValue, 8> Ops; 6452 for (unsigned i = 0; i < NumElts; ++i) 6453 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 6454 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 6455 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6456 } 6457 6458 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 6459 // know the default expansion would otherwise fall back on something even 6460 // worse. For a vector with one or two non-undef values, that's 6461 // scalar_to_vector for the elements followed by a shuffle (provided the 6462 // shuffle is valid for the target) and materialization element by element 6463 // on the stack followed by a load for everything else. 6464 if (!isConstant && !usesOnlyOneValue) { 6465 SDValue Vec = DAG.getUNDEF(VT); 6466 for (unsigned i = 0 ; i < NumElts; ++i) { 6467 SDValue V = Op.getOperand(i); 6468 if (V.isUndef()) 6469 continue; 6470 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32); 6471 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 6472 } 6473 return Vec; 6474 } 6475 6476 return SDValue(); 6477 } 6478 6479 // Gather data to see if the operation can be modelled as a 6480 // shuffle in combination with VEXTs. 6481 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 6482 SelectionDAG &DAG) const { 6483 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!"); 6484 SDLoc dl(Op); 6485 EVT VT = Op.getValueType(); 6486 unsigned NumElts = VT.getVectorNumElements(); 6487 6488 struct ShuffleSourceInfo { 6489 SDValue Vec; 6490 unsigned MinElt = std::numeric_limits<unsigned>::max(); 6491 unsigned MaxElt = 0; 6492 6493 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to 6494 // be compatible with the shuffle we intend to construct. As a result 6495 // ShuffleVec will be some sliding window into the original Vec. 6496 SDValue ShuffleVec; 6497 6498 // Code should guarantee that element i in Vec starts at element "WindowBase 6499 // + i * WindowScale in ShuffleVec". 6500 int WindowBase = 0; 6501 int WindowScale = 1; 6502 6503 ShuffleSourceInfo(SDValue Vec) : Vec(Vec), ShuffleVec(Vec) {} 6504 6505 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; } 6506 }; 6507 6508 // First gather all vectors used as an immediate source for this BUILD_VECTOR 6509 // node. 6510 SmallVector<ShuffleSourceInfo, 2> Sources; 6511 for (unsigned i = 0; i < NumElts; ++i) { 6512 SDValue V = Op.getOperand(i); 6513 if (V.isUndef()) 6514 continue; 6515 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 6516 // A shuffle can only come from building a vector from various 6517 // elements of other vectors. 6518 return SDValue(); 6519 } else if (!isa<ConstantSDNode>(V.getOperand(1))) { 6520 // Furthermore, shuffles require a constant mask, whereas extractelts 6521 // accept variable indices. 6522 return SDValue(); 6523 } 6524 6525 // Add this element source to the list if it's not already there. 6526 SDValue SourceVec = V.getOperand(0); 6527 auto Source = llvm::find(Sources, SourceVec); 6528 if (Source == Sources.end()) 6529 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec)); 6530 6531 // Update the minimum and maximum lane number seen. 6532 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 6533 Source->MinElt = std::min(Source->MinElt, EltNo); 6534 Source->MaxElt = std::max(Source->MaxElt, EltNo); 6535 } 6536 6537 // Currently only do something sane when at most two source vectors 6538 // are involved. 6539 if (Sources.size() > 2) 6540 return SDValue(); 6541 6542 // Find out the smallest element size among result and two sources, and use 6543 // it as element size to build the shuffle_vector. 6544 EVT SmallestEltTy = VT.getVectorElementType(); 6545 for (auto &Source : Sources) { 6546 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType(); 6547 if (SrcEltTy.bitsLT(SmallestEltTy)) 6548 SmallestEltTy = SrcEltTy; 6549 } 6550 unsigned ResMultiplier = 6551 VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits(); 6552 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits(); 6553 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts); 6554 6555 // If the source vector is too wide or too narrow, we may nevertheless be able 6556 // to construct a compatible shuffle either by concatenating it with UNDEF or 6557 // extracting a suitable range of elements. 6558 for (auto &Src : Sources) { 6559 EVT SrcVT = Src.ShuffleVec.getValueType(); 6560 6561 if (SrcVT.getSizeInBits() == VT.getSizeInBits()) 6562 continue; 6563 6564 // This stage of the search produces a source with the same element type as 6565 // the original, but with a total width matching the BUILD_VECTOR output. 6566 EVT EltVT = SrcVT.getVectorElementType(); 6567 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits(); 6568 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts); 6569 6570 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) { 6571 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits()) 6572 return SDValue(); 6573 // We can pad out the smaller vector for free, so if it's part of a 6574 // shuffle... 6575 Src.ShuffleVec = 6576 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec, 6577 DAG.getUNDEF(Src.ShuffleVec.getValueType())); 6578 continue; 6579 } 6580 6581 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits()) 6582 return SDValue(); 6583 6584 if (Src.MaxElt - Src.MinElt >= NumSrcElts) { 6585 // Span too large for a VEXT to cope 6586 return SDValue(); 6587 } 6588 6589 if (Src.MinElt >= NumSrcElts) { 6590 // The extraction can just take the second half 6591 Src.ShuffleVec = 6592 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6593 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 6594 Src.WindowBase = -NumSrcElts; 6595 } else if (Src.MaxElt < NumSrcElts) { 6596 // The extraction can just take the first half 6597 Src.ShuffleVec = 6598 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6599 DAG.getConstant(0, dl, MVT::i32)); 6600 } else { 6601 // An actual VEXT is needed 6602 SDValue VEXTSrc1 = 6603 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6604 DAG.getConstant(0, dl, MVT::i32)); 6605 SDValue VEXTSrc2 = 6606 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 6607 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 6608 6609 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1, 6610 VEXTSrc2, 6611 DAG.getConstant(Src.MinElt, dl, MVT::i32)); 6612 Src.WindowBase = -Src.MinElt; 6613 } 6614 } 6615 6616 // Another possible incompatibility occurs from the vector element types. We 6617 // can fix this by bitcasting the source vectors to the same type we intend 6618 // for the shuffle. 6619 for (auto &Src : Sources) { 6620 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType(); 6621 if (SrcEltTy == SmallestEltTy) 6622 continue; 6623 assert(ShuffleVT.getVectorElementType() == SmallestEltTy); 6624 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec); 6625 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits(); 6626 Src.WindowBase *= Src.WindowScale; 6627 } 6628 6629 // Final sanity check before we try to actually produce a shuffle. 6630 DEBUG( 6631 for (auto Src : Sources) 6632 assert(Src.ShuffleVec.getValueType() == ShuffleVT); 6633 ); 6634 6635 // The stars all align, our next step is to produce the mask for the shuffle. 6636 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1); 6637 int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits(); 6638 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 6639 SDValue Entry = Op.getOperand(i); 6640 if (Entry.isUndef()) 6641 continue; 6642 6643 auto Src = llvm::find(Sources, Entry.getOperand(0)); 6644 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue(); 6645 6646 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit 6647 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this 6648 // segment. 6649 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType(); 6650 int BitsDefined = std::min(OrigEltTy.getSizeInBits(), 6651 VT.getScalarSizeInBits()); 6652 int LanesDefined = BitsDefined / BitsPerShuffleLane; 6653 6654 // This source is expected to fill ResMultiplier lanes of the final shuffle, 6655 // starting at the appropriate offset. 6656 int *LaneMask = &Mask[i * ResMultiplier]; 6657 6658 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase; 6659 ExtractBase += NumElts * (Src - Sources.begin()); 6660 for (int j = 0; j < LanesDefined; ++j) 6661 LaneMask[j] = ExtractBase + j; 6662 } 6663 6664 // Final check before we try to produce nonsense... 6665 if (!isShuffleMaskLegal(Mask, ShuffleVT)) 6666 return SDValue(); 6667 6668 // We can't handle more than two sources. This should have already 6669 // been checked before this point. 6670 assert(Sources.size() <= 2 && "Too many sources!"); 6671 6672 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) }; 6673 for (unsigned i = 0; i < Sources.size(); ++i) 6674 ShuffleOps[i] = Sources[i].ShuffleVec; 6675 6676 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0], 6677 ShuffleOps[1], Mask); 6678 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle); 6679 } 6680 6681 /// isShuffleMaskLegal - Targets can use this to indicate that they only 6682 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 6683 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 6684 /// are assumed to be legal. 6685 bool ARMTargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const { 6686 if (VT.getVectorNumElements() == 4 && 6687 (VT.is128BitVector() || VT.is64BitVector())) { 6688 unsigned PFIndexes[4]; 6689 for (unsigned i = 0; i != 4; ++i) { 6690 if (M[i] < 0) 6691 PFIndexes[i] = 8; 6692 else 6693 PFIndexes[i] = M[i]; 6694 } 6695 6696 // Compute the index in the perfect shuffle table. 6697 unsigned PFTableIndex = 6698 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 6699 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 6700 unsigned Cost = (PFEntry >> 30); 6701 6702 if (Cost <= 4) 6703 return true; 6704 } 6705 6706 bool ReverseVEXT, isV_UNDEF; 6707 unsigned Imm, WhichResult; 6708 6709 unsigned EltSize = VT.getScalarSizeInBits(); 6710 return (EltSize >= 32 || 6711 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 6712 isVREVMask(M, VT, 64) || 6713 isVREVMask(M, VT, 32) || 6714 isVREVMask(M, VT, 16) || 6715 isVEXTMask(M, VT, ReverseVEXT, Imm) || 6716 isVTBLMask(M, VT) || 6717 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) || 6718 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT))); 6719 } 6720 6721 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 6722 /// the specified operations to build the shuffle. 6723 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 6724 SDValue RHS, SelectionDAG &DAG, 6725 const SDLoc &dl) { 6726 unsigned OpNum = (PFEntry >> 26) & 0x0F; 6727 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 6728 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 6729 6730 enum { 6731 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 6732 OP_VREV, 6733 OP_VDUP0, 6734 OP_VDUP1, 6735 OP_VDUP2, 6736 OP_VDUP3, 6737 OP_VEXT1, 6738 OP_VEXT2, 6739 OP_VEXT3, 6740 OP_VUZPL, // VUZP, left result 6741 OP_VUZPR, // VUZP, right result 6742 OP_VZIPL, // VZIP, left result 6743 OP_VZIPR, // VZIP, right result 6744 OP_VTRNL, // VTRN, left result 6745 OP_VTRNR // VTRN, right result 6746 }; 6747 6748 if (OpNum == OP_COPY) { 6749 if (LHSID == (1*9+2)*9+3) return LHS; 6750 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 6751 return RHS; 6752 } 6753 6754 SDValue OpLHS, OpRHS; 6755 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 6756 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 6757 EVT VT = OpLHS.getValueType(); 6758 6759 switch (OpNum) { 6760 default: llvm_unreachable("Unknown shuffle opcode!"); 6761 case OP_VREV: 6762 // VREV divides the vector in half and swaps within the half. 6763 if (VT.getVectorElementType() == MVT::i32 || 6764 VT.getVectorElementType() == MVT::f32) 6765 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 6766 // vrev <4 x i16> -> VREV32 6767 if (VT.getVectorElementType() == MVT::i16) 6768 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 6769 // vrev <4 x i8> -> VREV16 6770 assert(VT.getVectorElementType() == MVT::i8); 6771 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 6772 case OP_VDUP0: 6773 case OP_VDUP1: 6774 case OP_VDUP2: 6775 case OP_VDUP3: 6776 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6777 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32)); 6778 case OP_VEXT1: 6779 case OP_VEXT2: 6780 case OP_VEXT3: 6781 return DAG.getNode(ARMISD::VEXT, dl, VT, 6782 OpLHS, OpRHS, 6783 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32)); 6784 case OP_VUZPL: 6785 case OP_VUZPR: 6786 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 6787 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 6788 case OP_VZIPL: 6789 case OP_VZIPR: 6790 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 6791 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 6792 case OP_VTRNL: 6793 case OP_VTRNR: 6794 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 6795 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 6796 } 6797 } 6798 6799 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 6800 ArrayRef<int> ShuffleMask, 6801 SelectionDAG &DAG) { 6802 // Check to see if we can use the VTBL instruction. 6803 SDValue V1 = Op.getOperand(0); 6804 SDValue V2 = Op.getOperand(1); 6805 SDLoc DL(Op); 6806 6807 SmallVector<SDValue, 8> VTBLMask; 6808 for (ArrayRef<int>::iterator 6809 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 6810 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32)); 6811 6812 if (V2.getNode()->isUndef()) 6813 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 6814 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask)); 6815 6816 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 6817 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask)); 6818 } 6819 6820 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 6821 SelectionDAG &DAG) { 6822 SDLoc DL(Op); 6823 SDValue OpLHS = Op.getOperand(0); 6824 EVT VT = OpLHS.getValueType(); 6825 6826 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 6827 "Expect an v8i16/v16i8 type"); 6828 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 6829 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 6830 // extract the first 8 bytes into the top double word and the last 8 bytes 6831 // into the bottom double word. The v8i16 case is similar. 6832 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 6833 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 6834 DAG.getConstant(ExtractNum, DL, MVT::i32)); 6835 } 6836 6837 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 6838 SDValue V1 = Op.getOperand(0); 6839 SDValue V2 = Op.getOperand(1); 6840 SDLoc dl(Op); 6841 EVT VT = Op.getValueType(); 6842 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 6843 6844 // Convert shuffles that are directly supported on NEON to target-specific 6845 // DAG nodes, instead of keeping them as shuffles and matching them again 6846 // during code selection. This is more efficient and avoids the possibility 6847 // of inconsistencies between legalization and selection. 6848 // FIXME: floating-point vectors should be canonicalized to integer vectors 6849 // of the same time so that they get CSEd properly. 6850 ArrayRef<int> ShuffleMask = SVN->getMask(); 6851 6852 unsigned EltSize = VT.getScalarSizeInBits(); 6853 if (EltSize <= 32) { 6854 if (SVN->isSplat()) { 6855 int Lane = SVN->getSplatIndex(); 6856 // If this is undef splat, generate it via "just" vdup, if possible. 6857 if (Lane == -1) Lane = 0; 6858 6859 // Test if V1 is a SCALAR_TO_VECTOR. 6860 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 6861 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 6862 } 6863 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 6864 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 6865 // reaches it). 6866 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 6867 !isa<ConstantSDNode>(V1.getOperand(0))) { 6868 bool IsScalarToVector = true; 6869 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 6870 if (!V1.getOperand(i).isUndef()) { 6871 IsScalarToVector = false; 6872 break; 6873 } 6874 if (IsScalarToVector) 6875 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 6876 } 6877 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 6878 DAG.getConstant(Lane, dl, MVT::i32)); 6879 } 6880 6881 bool ReverseVEXT; 6882 unsigned Imm; 6883 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 6884 if (ReverseVEXT) 6885 std::swap(V1, V2); 6886 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 6887 DAG.getConstant(Imm, dl, MVT::i32)); 6888 } 6889 6890 if (isVREVMask(ShuffleMask, VT, 64)) 6891 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 6892 if (isVREVMask(ShuffleMask, VT, 32)) 6893 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 6894 if (isVREVMask(ShuffleMask, VT, 16)) 6895 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 6896 6897 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 6898 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 6899 DAG.getConstant(Imm, dl, MVT::i32)); 6900 } 6901 6902 // Check for Neon shuffles that modify both input vectors in place. 6903 // If both results are used, i.e., if there are two shuffles with the same 6904 // source operands and with masks corresponding to both results of one of 6905 // these operations, DAG memoization will ensure that a single node is 6906 // used for both shuffles. 6907 unsigned WhichResult; 6908 bool isV_UNDEF; 6909 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 6910 ShuffleMask, VT, WhichResult, isV_UNDEF)) { 6911 if (isV_UNDEF) 6912 V2 = V1; 6913 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2) 6914 .getValue(WhichResult); 6915 } 6916 6917 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize 6918 // shuffles that produce a result larger than their operands with: 6919 // shuffle(concat(v1, undef), concat(v2, undef)) 6920 // -> 6921 // shuffle(concat(v1, v2), undef) 6922 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine). 6923 // 6924 // This is useful in the general case, but there are special cases where 6925 // native shuffles produce larger results: the two-result ops. 6926 // 6927 // Look through the concat when lowering them: 6928 // shuffle(concat(v1, v2), undef) 6929 // -> 6930 // concat(VZIP(v1, v2):0, :1) 6931 // 6932 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) { 6933 SDValue SubV1 = V1->getOperand(0); 6934 SDValue SubV2 = V1->getOperand(1); 6935 EVT SubVT = SubV1.getValueType(); 6936 6937 // We expect these to have been canonicalized to -1. 6938 assert(llvm::all_of(ShuffleMask, [&](int i) { 6939 return i < (int)VT.getVectorNumElements(); 6940 }) && "Unexpected shuffle index into UNDEF operand!"); 6941 6942 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 6943 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) { 6944 if (isV_UNDEF) 6945 SubV2 = SubV1; 6946 assert((WhichResult == 0) && 6947 "In-place shuffle of concat can only have one result!"); 6948 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT), 6949 SubV1, SubV2); 6950 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0), 6951 Res.getValue(1)); 6952 } 6953 } 6954 } 6955 6956 // If the shuffle is not directly supported and it has 4 elements, use 6957 // the PerfectShuffle-generated table to synthesize it from other shuffles. 6958 unsigned NumElts = VT.getVectorNumElements(); 6959 if (NumElts == 4) { 6960 unsigned PFIndexes[4]; 6961 for (unsigned i = 0; i != 4; ++i) { 6962 if (ShuffleMask[i] < 0) 6963 PFIndexes[i] = 8; 6964 else 6965 PFIndexes[i] = ShuffleMask[i]; 6966 } 6967 6968 // Compute the index in the perfect shuffle table. 6969 unsigned PFTableIndex = 6970 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 6971 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 6972 unsigned Cost = (PFEntry >> 30); 6973 6974 if (Cost <= 4) 6975 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 6976 } 6977 6978 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 6979 if (EltSize >= 32) { 6980 // Do the expansion with floating-point types, since that is what the VFP 6981 // registers are defined to use, and since i64 is not legal. 6982 EVT EltVT = EVT::getFloatingPointVT(EltSize); 6983 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 6984 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 6985 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 6986 SmallVector<SDValue, 8> Ops; 6987 for (unsigned i = 0; i < NumElts; ++i) { 6988 if (ShuffleMask[i] < 0) 6989 Ops.push_back(DAG.getUNDEF(EltVT)); 6990 else 6991 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 6992 ShuffleMask[i] < (int)NumElts ? V1 : V2, 6993 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 6994 dl, MVT::i32))); 6995 } 6996 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 6997 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6998 } 6999 7000 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 7001 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 7002 7003 if (VT == MVT::v8i8) 7004 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG)) 7005 return NewOp; 7006 7007 return SDValue(); 7008 } 7009 7010 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 7011 // INSERT_VECTOR_ELT is legal only for immediate indexes. 7012 SDValue Lane = Op.getOperand(2); 7013 if (!isa<ConstantSDNode>(Lane)) 7014 return SDValue(); 7015 7016 return Op; 7017 } 7018 7019 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 7020 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 7021 SDValue Lane = Op.getOperand(1); 7022 if (!isa<ConstantSDNode>(Lane)) 7023 return SDValue(); 7024 7025 SDValue Vec = Op.getOperand(0); 7026 if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) { 7027 SDLoc dl(Op); 7028 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 7029 } 7030 7031 return Op; 7032 } 7033 7034 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 7035 // The only time a CONCAT_VECTORS operation can have legal types is when 7036 // two 64-bit vectors are concatenated to a 128-bit vector. 7037 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 7038 "unexpected CONCAT_VECTORS"); 7039 SDLoc dl(Op); 7040 SDValue Val = DAG.getUNDEF(MVT::v2f64); 7041 SDValue Op0 = Op.getOperand(0); 7042 SDValue Op1 = Op.getOperand(1); 7043 if (!Op0.isUndef()) 7044 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 7045 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 7046 DAG.getIntPtrConstant(0, dl)); 7047 if (!Op1.isUndef()) 7048 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 7049 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 7050 DAG.getIntPtrConstant(1, dl)); 7051 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 7052 } 7053 7054 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 7055 /// element has been zero/sign-extended, depending on the isSigned parameter, 7056 /// from an integer type half its size. 7057 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 7058 bool isSigned) { 7059 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 7060 EVT VT = N->getValueType(0); 7061 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 7062 SDNode *BVN = N->getOperand(0).getNode(); 7063 if (BVN->getValueType(0) != MVT::v4i32 || 7064 BVN->getOpcode() != ISD::BUILD_VECTOR) 7065 return false; 7066 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 7067 unsigned HiElt = 1 - LoElt; 7068 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 7069 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 7070 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 7071 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 7072 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 7073 return false; 7074 if (isSigned) { 7075 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 7076 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 7077 return true; 7078 } else { 7079 if (Hi0->isNullValue() && Hi1->isNullValue()) 7080 return true; 7081 } 7082 return false; 7083 } 7084 7085 if (N->getOpcode() != ISD::BUILD_VECTOR) 7086 return false; 7087 7088 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 7089 SDNode *Elt = N->getOperand(i).getNode(); 7090 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 7091 unsigned EltSize = VT.getScalarSizeInBits(); 7092 unsigned HalfSize = EltSize / 2; 7093 if (isSigned) { 7094 if (!isIntN(HalfSize, C->getSExtValue())) 7095 return false; 7096 } else { 7097 if (!isUIntN(HalfSize, C->getZExtValue())) 7098 return false; 7099 } 7100 continue; 7101 } 7102 return false; 7103 } 7104 7105 return true; 7106 } 7107 7108 /// isSignExtended - Check if a node is a vector value that is sign-extended 7109 /// or a constant BUILD_VECTOR with sign-extended elements. 7110 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 7111 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 7112 return true; 7113 if (isExtendedBUILD_VECTOR(N, DAG, true)) 7114 return true; 7115 return false; 7116 } 7117 7118 /// isZeroExtended - Check if a node is a vector value that is zero-extended 7119 /// or a constant BUILD_VECTOR with zero-extended elements. 7120 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 7121 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 7122 return true; 7123 if (isExtendedBUILD_VECTOR(N, DAG, false)) 7124 return true; 7125 return false; 7126 } 7127 7128 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 7129 if (OrigVT.getSizeInBits() >= 64) 7130 return OrigVT; 7131 7132 assert(OrigVT.isSimple() && "Expecting a simple value type"); 7133 7134 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 7135 switch (OrigSimpleTy) { 7136 default: llvm_unreachable("Unexpected Vector Type"); 7137 case MVT::v2i8: 7138 case MVT::v2i16: 7139 return MVT::v2i32; 7140 case MVT::v4i8: 7141 return MVT::v4i16; 7142 } 7143 } 7144 7145 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 7146 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 7147 /// We insert the required extension here to get the vector to fill a D register. 7148 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 7149 const EVT &OrigTy, 7150 const EVT &ExtTy, 7151 unsigned ExtOpcode) { 7152 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 7153 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 7154 // 64-bits we need to insert a new extension so that it will be 64-bits. 7155 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 7156 if (OrigTy.getSizeInBits() >= 64) 7157 return N; 7158 7159 // Must extend size to at least 64 bits to be used as an operand for VMULL. 7160 EVT NewVT = getExtensionTo64Bits(OrigTy); 7161 7162 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 7163 } 7164 7165 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 7166 /// does not do any sign/zero extension. If the original vector is less 7167 /// than 64 bits, an appropriate extension will be added after the load to 7168 /// reach a total size of 64 bits. We have to add the extension separately 7169 /// because ARM does not have a sign/zero extending load for vectors. 7170 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 7171 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 7172 7173 // The load already has the right type. 7174 if (ExtendedTy == LD->getMemoryVT()) 7175 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 7176 LD->getBasePtr(), LD->getPointerInfo(), 7177 LD->getAlignment(), LD->getMemOperand()->getFlags()); 7178 7179 // We need to create a zextload/sextload. We cannot just create a load 7180 // followed by a zext/zext node because LowerMUL is also run during normal 7181 // operation legalization where we can't create illegal types. 7182 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 7183 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 7184 LD->getMemoryVT(), LD->getAlignment(), 7185 LD->getMemOperand()->getFlags()); 7186 } 7187 7188 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 7189 /// extending load, or BUILD_VECTOR with extended elements, return the 7190 /// unextended value. The unextended vector should be 64 bits so that it can 7191 /// be used as an operand to a VMULL instruction. If the original vector size 7192 /// before extension is less than 64 bits we add a an extension to resize 7193 /// the vector to 64 bits. 7194 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 7195 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 7196 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 7197 N->getOperand(0)->getValueType(0), 7198 N->getValueType(0), 7199 N->getOpcode()); 7200 7201 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 7202 assert((ISD::isSEXTLoad(LD) || ISD::isZEXTLoad(LD)) && 7203 "Expected extending load"); 7204 7205 SDValue newLoad = SkipLoadExtensionForVMULL(LD, DAG); 7206 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), newLoad.getValue(1)); 7207 unsigned Opcode = ISD::isSEXTLoad(LD) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 7208 SDValue extLoad = 7209 DAG.getNode(Opcode, SDLoc(newLoad), LD->getValueType(0), newLoad); 7210 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 0), extLoad); 7211 7212 return newLoad; 7213 } 7214 7215 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 7216 // have been legalized as a BITCAST from v4i32. 7217 if (N->getOpcode() == ISD::BITCAST) { 7218 SDNode *BVN = N->getOperand(0).getNode(); 7219 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 7220 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 7221 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 7222 return DAG.getBuildVector( 7223 MVT::v2i32, SDLoc(N), 7224 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)}); 7225 } 7226 // Construct a new BUILD_VECTOR with elements truncated to half the size. 7227 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 7228 EVT VT = N->getValueType(0); 7229 unsigned EltSize = VT.getScalarSizeInBits() / 2; 7230 unsigned NumElts = VT.getVectorNumElements(); 7231 MVT TruncVT = MVT::getIntegerVT(EltSize); 7232 SmallVector<SDValue, 8> Ops; 7233 SDLoc dl(N); 7234 for (unsigned i = 0; i != NumElts; ++i) { 7235 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 7236 const APInt &CInt = C->getAPIntValue(); 7237 // Element types smaller than 32 bits are not legal, so use i32 elements. 7238 // The values are implicitly truncated so sext vs. zext doesn't matter. 7239 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32)); 7240 } 7241 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops); 7242 } 7243 7244 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 7245 unsigned Opcode = N->getOpcode(); 7246 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 7247 SDNode *N0 = N->getOperand(0).getNode(); 7248 SDNode *N1 = N->getOperand(1).getNode(); 7249 return N0->hasOneUse() && N1->hasOneUse() && 7250 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 7251 } 7252 return false; 7253 } 7254 7255 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 7256 unsigned Opcode = N->getOpcode(); 7257 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 7258 SDNode *N0 = N->getOperand(0).getNode(); 7259 SDNode *N1 = N->getOperand(1).getNode(); 7260 return N0->hasOneUse() && N1->hasOneUse() && 7261 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 7262 } 7263 return false; 7264 } 7265 7266 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 7267 // Multiplications are only custom-lowered for 128-bit vectors so that 7268 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 7269 EVT VT = Op.getValueType(); 7270 assert(VT.is128BitVector() && VT.isInteger() && 7271 "unexpected type for custom-lowering ISD::MUL"); 7272 SDNode *N0 = Op.getOperand(0).getNode(); 7273 SDNode *N1 = Op.getOperand(1).getNode(); 7274 unsigned NewOpc = 0; 7275 bool isMLA = false; 7276 bool isN0SExt = isSignExtended(N0, DAG); 7277 bool isN1SExt = isSignExtended(N1, DAG); 7278 if (isN0SExt && isN1SExt) 7279 NewOpc = ARMISD::VMULLs; 7280 else { 7281 bool isN0ZExt = isZeroExtended(N0, DAG); 7282 bool isN1ZExt = isZeroExtended(N1, DAG); 7283 if (isN0ZExt && isN1ZExt) 7284 NewOpc = ARMISD::VMULLu; 7285 else if (isN1SExt || isN1ZExt) { 7286 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 7287 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 7288 if (isN1SExt && isAddSubSExt(N0, DAG)) { 7289 NewOpc = ARMISD::VMULLs; 7290 isMLA = true; 7291 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 7292 NewOpc = ARMISD::VMULLu; 7293 isMLA = true; 7294 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 7295 std::swap(N0, N1); 7296 NewOpc = ARMISD::VMULLu; 7297 isMLA = true; 7298 } 7299 } 7300 7301 if (!NewOpc) { 7302 if (VT == MVT::v2i64) 7303 // Fall through to expand this. It is not legal. 7304 return SDValue(); 7305 else 7306 // Other vector multiplications are legal. 7307 return Op; 7308 } 7309 } 7310 7311 // Legalize to a VMULL instruction. 7312 SDLoc DL(Op); 7313 SDValue Op0; 7314 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 7315 if (!isMLA) { 7316 Op0 = SkipExtensionForVMULL(N0, DAG); 7317 assert(Op0.getValueType().is64BitVector() && 7318 Op1.getValueType().is64BitVector() && 7319 "unexpected types for extended operands to VMULL"); 7320 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 7321 } 7322 7323 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 7324 // isel lowering to take advantage of no-stall back to back vmul + vmla. 7325 // vmull q0, d4, d6 7326 // vmlal q0, d5, d6 7327 // is faster than 7328 // vaddl q0, d4, d5 7329 // vmovl q1, d6 7330 // vmul q0, q0, q1 7331 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 7332 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 7333 EVT Op1VT = Op1.getValueType(); 7334 return DAG.getNode(N0->getOpcode(), DL, VT, 7335 DAG.getNode(NewOpc, DL, VT, 7336 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 7337 DAG.getNode(NewOpc, DL, VT, 7338 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 7339 } 7340 7341 static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl, 7342 SelectionDAG &DAG) { 7343 // TODO: Should this propagate fast-math-flags? 7344 7345 // Convert to float 7346 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 7347 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 7348 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 7349 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 7350 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 7351 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 7352 // Get reciprocal estimate. 7353 // float4 recip = vrecpeq_f32(yf); 7354 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7355 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 7356 Y); 7357 // Because char has a smaller range than uchar, we can actually get away 7358 // without any newton steps. This requires that we use a weird bias 7359 // of 0xb000, however (again, this has been exhaustively tested). 7360 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 7361 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 7362 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 7363 Y = DAG.getConstant(0xb000, dl, MVT::v4i32); 7364 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 7365 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 7366 // Convert back to short. 7367 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 7368 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 7369 return X; 7370 } 7371 7372 static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl, 7373 SelectionDAG &DAG) { 7374 // TODO: Should this propagate fast-math-flags? 7375 7376 SDValue N2; 7377 // Convert to float. 7378 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 7379 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 7380 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 7381 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 7382 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 7383 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 7384 7385 // Use reciprocal estimate and one refinement step. 7386 // float4 recip = vrecpeq_f32(yf); 7387 // recip *= vrecpsq_f32(yf, recip); 7388 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7389 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 7390 N1); 7391 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7392 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 7393 N1, N2); 7394 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 7395 // Because short has a smaller range than ushort, we can actually get away 7396 // with only a single newton step. This requires that we use a weird bias 7397 // of 89, however (again, this has been exhaustively tested). 7398 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 7399 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 7400 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 7401 N1 = DAG.getConstant(0x89, dl, MVT::v4i32); 7402 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 7403 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 7404 // Convert back to integer and return. 7405 // return vmovn_s32(vcvt_s32_f32(result)); 7406 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 7407 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 7408 return N0; 7409 } 7410 7411 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 7412 EVT VT = Op.getValueType(); 7413 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 7414 "unexpected type for custom-lowering ISD::SDIV"); 7415 7416 SDLoc dl(Op); 7417 SDValue N0 = Op.getOperand(0); 7418 SDValue N1 = Op.getOperand(1); 7419 SDValue N2, N3; 7420 7421 if (VT == MVT::v8i8) { 7422 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 7423 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 7424 7425 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7426 DAG.getIntPtrConstant(4, dl)); 7427 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7428 DAG.getIntPtrConstant(4, dl)); 7429 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7430 DAG.getIntPtrConstant(0, dl)); 7431 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7432 DAG.getIntPtrConstant(0, dl)); 7433 7434 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 7435 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 7436 7437 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 7438 N0 = LowerCONCAT_VECTORS(N0, DAG); 7439 7440 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 7441 return N0; 7442 } 7443 return LowerSDIV_v4i16(N0, N1, dl, DAG); 7444 } 7445 7446 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 7447 // TODO: Should this propagate fast-math-flags? 7448 EVT VT = Op.getValueType(); 7449 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 7450 "unexpected type for custom-lowering ISD::UDIV"); 7451 7452 SDLoc dl(Op); 7453 SDValue N0 = Op.getOperand(0); 7454 SDValue N1 = Op.getOperand(1); 7455 SDValue N2, N3; 7456 7457 if (VT == MVT::v8i8) { 7458 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 7459 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 7460 7461 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7462 DAG.getIntPtrConstant(4, dl)); 7463 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7464 DAG.getIntPtrConstant(4, dl)); 7465 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 7466 DAG.getIntPtrConstant(0, dl)); 7467 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 7468 DAG.getIntPtrConstant(0, dl)); 7469 7470 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 7471 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 7472 7473 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 7474 N0 = LowerCONCAT_VECTORS(N0, DAG); 7475 7476 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 7477 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl, 7478 MVT::i32), 7479 N0); 7480 return N0; 7481 } 7482 7483 // v4i16 sdiv ... Convert to float. 7484 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 7485 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 7486 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 7487 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 7488 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 7489 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 7490 7491 // Use reciprocal estimate and two refinement steps. 7492 // float4 recip = vrecpeq_f32(yf); 7493 // recip *= vrecpsq_f32(yf, recip); 7494 // recip *= vrecpsq_f32(yf, recip); 7495 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7496 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 7497 BN1); 7498 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7499 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 7500 BN1, N2); 7501 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 7502 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 7503 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 7504 BN1, N2); 7505 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 7506 // Simply multiplying by the reciprocal estimate can leave us a few ulps 7507 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 7508 // and that it will never cause us to return an answer too large). 7509 // float4 result = as_float4(as_int4(xf*recip) + 2); 7510 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 7511 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 7512 N1 = DAG.getConstant(2, dl, MVT::v4i32); 7513 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 7514 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 7515 // Convert back to integer and return. 7516 // return vmovn_u32(vcvt_s32_f32(result)); 7517 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 7518 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 7519 return N0; 7520 } 7521 7522 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 7523 EVT VT = Op.getNode()->getValueType(0); 7524 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 7525 7526 unsigned Opc; 7527 bool ExtraOp = false; 7528 switch (Op.getOpcode()) { 7529 default: llvm_unreachable("Invalid code"); 7530 case ISD::ADDC: Opc = ARMISD::ADDC; break; 7531 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 7532 case ISD::SUBC: Opc = ARMISD::SUBC; break; 7533 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 7534 } 7535 7536 if (!ExtraOp) 7537 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 7538 Op.getOperand(1)); 7539 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 7540 Op.getOperand(1), Op.getOperand(2)); 7541 } 7542 7543 static SDValue LowerADDSUBCARRY(SDValue Op, SelectionDAG &DAG) { 7544 SDNode *N = Op.getNode(); 7545 EVT VT = N->getValueType(0); 7546 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 7547 7548 SDValue Carry = Op.getOperand(2); 7549 EVT CarryVT = Carry.getValueType(); 7550 7551 SDLoc DL(Op); 7552 7553 APInt NegOne = APInt::getAllOnesValue(CarryVT.getScalarSizeInBits()); 7554 7555 SDValue Result; 7556 if (Op.getOpcode() == ISD::ADDCARRY) { 7557 // This converts the boolean value carry into the carry flag. 7558 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 7559 7560 // Do the addition proper using the carry flag we wanted. 7561 Result = DAG.getNode(ARMISD::ADDE, DL, VTs, Op.getOperand(0), 7562 Op.getOperand(1), Carry.getValue(1)); 7563 7564 // Now convert the carry flag into a boolean value. 7565 Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG); 7566 } else { 7567 // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we 7568 // have to invert the carry first. 7569 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 7570 DAG.getConstant(1, DL, MVT::i32), Carry); 7571 // This converts the boolean value carry into the carry flag. 7572 Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG); 7573 7574 // Do the subtraction proper using the carry flag we wanted. 7575 Result = DAG.getNode(ARMISD::SUBE, DL, VTs, Op.getOperand(0), 7576 Op.getOperand(1), Carry.getValue(1)); 7577 7578 // Now convert the carry flag into a boolean value. 7579 Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG); 7580 // But the carry returned by ARMISD::SUBE is not a borrow as expected 7581 // by ISD::SUBCARRY, so compute 1 - C. 7582 Carry = DAG.getNode(ISD::SUB, DL, MVT::i32, 7583 DAG.getConstant(1, DL, MVT::i32), Carry); 7584 } 7585 7586 // Return both values. 7587 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Carry); 7588 } 7589 7590 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { 7591 assert(Subtarget->isTargetDarwin()); 7592 7593 // For iOS, we want to call an alternative entry point: __sincos_stret, 7594 // return values are passed via sret. 7595 SDLoc dl(Op); 7596 SDValue Arg = Op.getOperand(0); 7597 EVT ArgVT = Arg.getValueType(); 7598 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 7599 auto PtrVT = getPointerTy(DAG.getDataLayout()); 7600 7601 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 7602 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7603 7604 // Pair of floats / doubles used to pass the result. 7605 Type *RetTy = StructType::get(ArgTy, ArgTy); 7606 auto &DL = DAG.getDataLayout(); 7607 7608 ArgListTy Args; 7609 bool ShouldUseSRet = Subtarget->isAPCS_ABI(); 7610 SDValue SRet; 7611 if (ShouldUseSRet) { 7612 // Create stack object for sret. 7613 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy); 7614 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy); 7615 int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false); 7616 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL)); 7617 7618 ArgListEntry Entry; 7619 Entry.Node = SRet; 7620 Entry.Ty = RetTy->getPointerTo(); 7621 Entry.IsSExt = false; 7622 Entry.IsZExt = false; 7623 Entry.IsSRet = true; 7624 Args.push_back(Entry); 7625 RetTy = Type::getVoidTy(*DAG.getContext()); 7626 } 7627 7628 ArgListEntry Entry; 7629 Entry.Node = Arg; 7630 Entry.Ty = ArgTy; 7631 Entry.IsSExt = false; 7632 Entry.IsZExt = false; 7633 Args.push_back(Entry); 7634 7635 RTLIB::Libcall LC = 7636 (ArgVT == MVT::f64) ? RTLIB::SINCOS_STRET_F64 : RTLIB::SINCOS_STRET_F32; 7637 const char *LibcallName = getLibcallName(LC); 7638 CallingConv::ID CC = getLibcallCallingConv(LC); 7639 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL)); 7640 7641 TargetLowering::CallLoweringInfo CLI(DAG); 7642 CLI.setDebugLoc(dl) 7643 .setChain(DAG.getEntryNode()) 7644 .setCallee(CC, RetTy, Callee, std::move(Args)) 7645 .setDiscardResult(ShouldUseSRet); 7646 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 7647 7648 if (!ShouldUseSRet) 7649 return CallResult.first; 7650 7651 SDValue LoadSin = 7652 DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo()); 7653 7654 // Address of cos field. 7655 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet, 7656 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl)); 7657 SDValue LoadCos = 7658 DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo()); 7659 7660 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); 7661 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, 7662 LoadSin.getValue(0), LoadCos.getValue(0)); 7663 } 7664 7665 SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG, 7666 bool Signed, 7667 SDValue &Chain) const { 7668 EVT VT = Op.getValueType(); 7669 assert((VT == MVT::i32 || VT == MVT::i64) && 7670 "unexpected type for custom lowering DIV"); 7671 SDLoc dl(Op); 7672 7673 const auto &DL = DAG.getDataLayout(); 7674 const auto &TLI = DAG.getTargetLoweringInfo(); 7675 7676 const char *Name = nullptr; 7677 if (Signed) 7678 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64"; 7679 else 7680 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64"; 7681 7682 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL)); 7683 7684 ARMTargetLowering::ArgListTy Args; 7685 7686 for (auto AI : {1, 0}) { 7687 ArgListEntry Arg; 7688 Arg.Node = Op.getOperand(AI); 7689 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext()); 7690 Args.push_back(Arg); 7691 } 7692 7693 CallLoweringInfo CLI(DAG); 7694 CLI.setDebugLoc(dl) 7695 .setChain(Chain) 7696 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()), 7697 ES, std::move(Args)); 7698 7699 return LowerCallTo(CLI).first; 7700 } 7701 7702 SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG, 7703 bool Signed) const { 7704 assert(Op.getValueType() == MVT::i32 && 7705 "unexpected type for custom lowering DIV"); 7706 SDLoc dl(Op); 7707 7708 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, 7709 DAG.getEntryNode(), Op.getOperand(1)); 7710 7711 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 7712 } 7713 7714 static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) { 7715 SDLoc DL(N); 7716 SDValue Op = N->getOperand(1); 7717 if (N->getValueType(0) == MVT::i32) 7718 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op); 7719 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op, 7720 DAG.getConstant(0, DL, MVT::i32)); 7721 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op, 7722 DAG.getConstant(1, DL, MVT::i32)); 7723 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, 7724 DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi)); 7725 } 7726 7727 void ARMTargetLowering::ExpandDIV_Windows( 7728 SDValue Op, SelectionDAG &DAG, bool Signed, 7729 SmallVectorImpl<SDValue> &Results) const { 7730 const auto &DL = DAG.getDataLayout(); 7731 const auto &TLI = DAG.getTargetLoweringInfo(); 7732 7733 assert(Op.getValueType() == MVT::i64 && 7734 "unexpected type for custom lowering DIV"); 7735 SDLoc dl(Op); 7736 7737 SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode()); 7738 7739 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 7740 7741 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result); 7742 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result, 7743 DAG.getConstant(32, dl, TLI.getPointerTy(DL))); 7744 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper); 7745 7746 Results.push_back(Lower); 7747 Results.push_back(Upper); 7748 } 7749 7750 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 7751 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering())) 7752 // Acquire/Release load/store is not legal for targets without a dmb or 7753 // equivalent available. 7754 return SDValue(); 7755 7756 // Monotonic load/store is legal for all targets. 7757 return Op; 7758 } 7759 7760 static void ReplaceREADCYCLECOUNTER(SDNode *N, 7761 SmallVectorImpl<SDValue> &Results, 7762 SelectionDAG &DAG, 7763 const ARMSubtarget *Subtarget) { 7764 SDLoc DL(N); 7765 // Under Power Management extensions, the cycle-count is: 7766 // mrc p15, #0, <Rt>, c9, c13, #0 7767 SDValue Ops[] = { N->getOperand(0), // Chain 7768 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 7769 DAG.getConstant(15, DL, MVT::i32), 7770 DAG.getConstant(0, DL, MVT::i32), 7771 DAG.getConstant(9, DL, MVT::i32), 7772 DAG.getConstant(13, DL, MVT::i32), 7773 DAG.getConstant(0, DL, MVT::i32) 7774 }; 7775 7776 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 7777 DAG.getVTList(MVT::i32, MVT::Other), Ops); 7778 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32, 7779 DAG.getConstant(0, DL, MVT::i32))); 7780 Results.push_back(Cycles32.getValue(1)); 7781 } 7782 7783 static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) { 7784 SDLoc dl(V.getNode()); 7785 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32); 7786 SDValue VHi = DAG.getAnyExtOrTrunc( 7787 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)), 7788 dl, MVT::i32); 7789 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 7790 if (isBigEndian) 7791 std::swap (VLo, VHi); 7792 SDValue RegClass = 7793 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32); 7794 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32); 7795 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32); 7796 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 }; 7797 return SDValue( 7798 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0); 7799 } 7800 7801 static void ReplaceCMP_SWAP_64Results(SDNode *N, 7802 SmallVectorImpl<SDValue> & Results, 7803 SelectionDAG &DAG) { 7804 assert(N->getValueType(0) == MVT::i64 && 7805 "AtomicCmpSwap on types less than 64 should be legal"); 7806 SDValue Ops[] = {N->getOperand(1), 7807 createGPRPairNode(DAG, N->getOperand(2)), 7808 createGPRPairNode(DAG, N->getOperand(3)), 7809 N->getOperand(0)}; 7810 SDNode *CmpSwap = DAG.getMachineNode( 7811 ARM::CMP_SWAP_64, SDLoc(N), 7812 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops); 7813 7814 MachineFunction &MF = DAG.getMachineFunction(); 7815 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1); 7816 MemOp[0] = cast<MemSDNode>(N)->getMemOperand(); 7817 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1); 7818 7819 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 7820 7821 Results.push_back( 7822 DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_1 : ARM::gsub_0, 7823 SDLoc(N), MVT::i32, SDValue(CmpSwap, 0))); 7824 Results.push_back( 7825 DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_0 : ARM::gsub_1, 7826 SDLoc(N), MVT::i32, SDValue(CmpSwap, 0))); 7827 Results.push_back(SDValue(CmpSwap, 2)); 7828 } 7829 7830 static SDValue LowerFPOWI(SDValue Op, const ARMSubtarget &Subtarget, 7831 SelectionDAG &DAG) { 7832 const auto &TLI = DAG.getTargetLoweringInfo(); 7833 7834 assert(Subtarget.getTargetTriple().isOSMSVCRT() && 7835 "Custom lowering is MSVCRT specific!"); 7836 7837 SDLoc dl(Op); 7838 SDValue Val = Op.getOperand(0); 7839 MVT Ty = Val->getSimpleValueType(0); 7840 SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, dl, Ty, Op.getOperand(1)); 7841 SDValue Callee = DAG.getExternalSymbol(Ty == MVT::f32 ? "powf" : "pow", 7842 TLI.getPointerTy(DAG.getDataLayout())); 7843 7844 TargetLowering::ArgListTy Args; 7845 TargetLowering::ArgListEntry Entry; 7846 7847 Entry.Node = Val; 7848 Entry.Ty = Val.getValueType().getTypeForEVT(*DAG.getContext()); 7849 Entry.IsZExt = true; 7850 Args.push_back(Entry); 7851 7852 Entry.Node = Exponent; 7853 Entry.Ty = Exponent.getValueType().getTypeForEVT(*DAG.getContext()); 7854 Entry.IsZExt = true; 7855 Args.push_back(Entry); 7856 7857 Type *LCRTy = Val.getValueType().getTypeForEVT(*DAG.getContext()); 7858 7859 // In the in-chain to the call is the entry node If we are emitting a 7860 // tailcall, the chain will be mutated if the node has a non-entry input 7861 // chain. 7862 SDValue InChain = DAG.getEntryNode(); 7863 SDValue TCChain = InChain; 7864 7865 const Function &F = DAG.getMachineFunction().getFunction(); 7866 bool IsTC = TLI.isInTailCallPosition(DAG, Op.getNode(), TCChain) && 7867 F.getReturnType() == LCRTy; 7868 if (IsTC) 7869 InChain = TCChain; 7870 7871 TargetLowering::CallLoweringInfo CLI(DAG); 7872 CLI.setDebugLoc(dl) 7873 .setChain(InChain) 7874 .setCallee(CallingConv::ARM_AAPCS_VFP, LCRTy, Callee, std::move(Args)) 7875 .setTailCall(IsTC); 7876 std::pair<SDValue, SDValue> CI = TLI.LowerCallTo(CLI); 7877 7878 // Return the chain (the DAG root) if it is a tail call 7879 return !CI.second.getNode() ? DAG.getRoot() : CI.first; 7880 } 7881 7882 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 7883 DEBUG(dbgs() << "Lowering node: "; Op.dump()); 7884 switch (Op.getOpcode()) { 7885 default: llvm_unreachable("Don't know how to custom lower this!"); 7886 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG); 7887 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 7888 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 7889 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 7890 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 7891 case ISD::SELECT: return LowerSELECT(Op, DAG); 7892 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 7893 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 7894 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 7895 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 7896 case ISD::VASTART: return LowerVASTART(Op, DAG); 7897 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 7898 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 7899 case ISD::SINT_TO_FP: 7900 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 7901 case ISD::FP_TO_SINT: 7902 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 7903 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 7904 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 7905 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 7906 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 7907 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 7908 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG); 7909 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 7910 Subtarget); 7911 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 7912 case ISD::SHL: 7913 case ISD::SRL: 7914 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 7915 case ISD::SREM: return LowerREM(Op.getNode(), DAG); 7916 case ISD::UREM: return LowerREM(Op.getNode(), DAG); 7917 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 7918 case ISD::SRL_PARTS: 7919 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 7920 case ISD::CTTZ: 7921 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 7922 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 7923 case ISD::SETCC: return LowerVSETCC(Op, DAG); 7924 case ISD::SETCCE: return LowerSETCCE(Op, DAG); 7925 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 7926 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 7927 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 7928 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 7929 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 7930 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 7931 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 7932 case ISD::MUL: return LowerMUL(Op, DAG); 7933 case ISD::SDIV: 7934 if (Subtarget->isTargetWindows() && !Op.getValueType().isVector()) 7935 return LowerDIV_Windows(Op, DAG, /* Signed */ true); 7936 return LowerSDIV(Op, DAG); 7937 case ISD::UDIV: 7938 if (Subtarget->isTargetWindows() && !Op.getValueType().isVector()) 7939 return LowerDIV_Windows(Op, DAG, /* Signed */ false); 7940 return LowerUDIV(Op, DAG); 7941 case ISD::ADDC: 7942 case ISD::ADDE: 7943 case ISD::SUBC: 7944 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 7945 case ISD::ADDCARRY: 7946 case ISD::SUBCARRY: return LowerADDSUBCARRY(Op, DAG); 7947 case ISD::SADDO: 7948 case ISD::SSUBO: 7949 return LowerSignedALUO(Op, DAG); 7950 case ISD::UADDO: 7951 case ISD::USUBO: 7952 return LowerUnsignedALUO(Op, DAG); 7953 case ISD::ATOMIC_LOAD: 7954 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 7955 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); 7956 case ISD::SDIVREM: 7957 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 7958 case ISD::DYNAMIC_STACKALLOC: 7959 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 7960 return LowerDYNAMIC_STACKALLOC(Op, DAG); 7961 llvm_unreachable("Don't know how to custom lower this!"); 7962 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 7963 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 7964 case ISD::FPOWI: return LowerFPOWI(Op, *Subtarget, DAG); 7965 case ARMISD::WIN__DBZCHK: return SDValue(); 7966 } 7967 } 7968 7969 static void ReplaceLongIntrinsic(SDNode *N, SmallVectorImpl<SDValue> &Results, 7970 SelectionDAG &DAG) { 7971 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 7972 unsigned Opc = 0; 7973 if (IntNo == Intrinsic::arm_smlald) 7974 Opc = ARMISD::SMLALD; 7975 else if (IntNo == Intrinsic::arm_smlaldx) 7976 Opc = ARMISD::SMLALDX; 7977 else if (IntNo == Intrinsic::arm_smlsld) 7978 Opc = ARMISD::SMLSLD; 7979 else if (IntNo == Intrinsic::arm_smlsldx) 7980 Opc = ARMISD::SMLSLDX; 7981 else 7982 return; 7983 7984 SDLoc dl(N); 7985 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 7986 N->getOperand(3), 7987 DAG.getConstant(0, dl, MVT::i32)); 7988 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 7989 N->getOperand(3), 7990 DAG.getConstant(1, dl, MVT::i32)); 7991 7992 SDValue LongMul = DAG.getNode(Opc, dl, 7993 DAG.getVTList(MVT::i32, MVT::i32), 7994 N->getOperand(1), N->getOperand(2), 7995 Lo, Hi); 7996 Results.push_back(LongMul.getValue(0)); 7997 Results.push_back(LongMul.getValue(1)); 7998 } 7999 8000 /// ReplaceNodeResults - Replace the results of node with an illegal result 8001 /// type with new values built out of custom code. 8002 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 8003 SmallVectorImpl<SDValue> &Results, 8004 SelectionDAG &DAG) const { 8005 SDValue Res; 8006 switch (N->getOpcode()) { 8007 default: 8008 llvm_unreachable("Don't know how to custom expand this!"); 8009 case ISD::READ_REGISTER: 8010 ExpandREAD_REGISTER(N, Results, DAG); 8011 break; 8012 case ISD::BITCAST: 8013 Res = ExpandBITCAST(N, DAG); 8014 break; 8015 case ISD::SRL: 8016 case ISD::SRA: 8017 Res = Expand64BitShift(N, DAG, Subtarget); 8018 break; 8019 case ISD::SREM: 8020 case ISD::UREM: 8021 Res = LowerREM(N, DAG); 8022 break; 8023 case ISD::SDIVREM: 8024 case ISD::UDIVREM: 8025 Res = LowerDivRem(SDValue(N, 0), DAG); 8026 assert(Res.getNumOperands() == 2 && "DivRem needs two values"); 8027 Results.push_back(Res.getValue(0)); 8028 Results.push_back(Res.getValue(1)); 8029 return; 8030 case ISD::READCYCLECOUNTER: 8031 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 8032 return; 8033 case ISD::UDIV: 8034 case ISD::SDIV: 8035 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows"); 8036 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV, 8037 Results); 8038 case ISD::ATOMIC_CMP_SWAP: 8039 ReplaceCMP_SWAP_64Results(N, Results, DAG); 8040 return; 8041 case ISD::INTRINSIC_WO_CHAIN: 8042 return ReplaceLongIntrinsic(N, Results, DAG); 8043 } 8044 if (Res.getNode()) 8045 Results.push_back(Res); 8046 } 8047 8048 //===----------------------------------------------------------------------===// 8049 // ARM Scheduler Hooks 8050 //===----------------------------------------------------------------------===// 8051 8052 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 8053 /// registers the function context. 8054 void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI, 8055 MachineBasicBlock *MBB, 8056 MachineBasicBlock *DispatchBB, 8057 int FI) const { 8058 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() && 8059 "ROPI/RWPI not currently supported with SjLj"); 8060 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8061 DebugLoc dl = MI.getDebugLoc(); 8062 MachineFunction *MF = MBB->getParent(); 8063 MachineRegisterInfo *MRI = &MF->getRegInfo(); 8064 MachineConstantPool *MCP = MF->getConstantPool(); 8065 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 8066 const Function &F = MF->getFunction(); 8067 8068 bool isThumb = Subtarget->isThumb(); 8069 bool isThumb2 = Subtarget->isThumb2(); 8070 8071 unsigned PCLabelId = AFI->createPICLabelUId(); 8072 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 8073 ARMConstantPoolValue *CPV = 8074 ARMConstantPoolMBB::Create(F.getContext(), DispatchBB, PCLabelId, PCAdj); 8075 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 8076 8077 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass 8078 : &ARM::GPRRegClass; 8079 8080 // Grab constant pool and fixed stack memory operands. 8081 MachineMemOperand *CPMMO = 8082 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF), 8083 MachineMemOperand::MOLoad, 4, 4); 8084 8085 MachineMemOperand *FIMMOSt = 8086 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI), 8087 MachineMemOperand::MOStore, 4, 4); 8088 8089 // Load the address of the dispatch MBB into the jump buffer. 8090 if (isThumb2) { 8091 // Incoming value: jbuf 8092 // ldr.n r5, LCPI1_1 8093 // orr r5, r5, #1 8094 // add r5, pc 8095 // str r5, [$jbuf, #+4] ; &jbuf[1] 8096 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8097 BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 8098 .addConstantPoolIndex(CPI) 8099 .addMemOperand(CPMMO) 8100 .add(predOps(ARMCC::AL)); 8101 // Set the low bit because of thumb mode. 8102 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8103 BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 8104 .addReg(NewVReg1, RegState::Kill) 8105 .addImm(0x01) 8106 .add(predOps(ARMCC::AL)) 8107 .add(condCodeOp()); 8108 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8109 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 8110 .addReg(NewVReg2, RegState::Kill) 8111 .addImm(PCLabelId); 8112 BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 8113 .addReg(NewVReg3, RegState::Kill) 8114 .addFrameIndex(FI) 8115 .addImm(36) // &jbuf[1] :: pc 8116 .addMemOperand(FIMMOSt) 8117 .add(predOps(ARMCC::AL)); 8118 } else if (isThumb) { 8119 // Incoming value: jbuf 8120 // ldr.n r1, LCPI1_4 8121 // add r1, pc 8122 // mov r2, #1 8123 // orrs r1, r2 8124 // add r2, $jbuf, #+4 ; &jbuf[1] 8125 // str r1, [r2] 8126 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8127 BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 8128 .addConstantPoolIndex(CPI) 8129 .addMemOperand(CPMMO) 8130 .add(predOps(ARMCC::AL)); 8131 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8132 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 8133 .addReg(NewVReg1, RegState::Kill) 8134 .addImm(PCLabelId); 8135 // Set the low bit because of thumb mode. 8136 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8137 BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 8138 .addReg(ARM::CPSR, RegState::Define) 8139 .addImm(1) 8140 .add(predOps(ARMCC::AL)); 8141 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8142 BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 8143 .addReg(ARM::CPSR, RegState::Define) 8144 .addReg(NewVReg2, RegState::Kill) 8145 .addReg(NewVReg3, RegState::Kill) 8146 .add(predOps(ARMCC::AL)); 8147 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 8148 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5) 8149 .addFrameIndex(FI) 8150 .addImm(36); // &jbuf[1] :: pc 8151 BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 8152 .addReg(NewVReg4, RegState::Kill) 8153 .addReg(NewVReg5, RegState::Kill) 8154 .addImm(0) 8155 .addMemOperand(FIMMOSt) 8156 .add(predOps(ARMCC::AL)); 8157 } else { 8158 // Incoming value: jbuf 8159 // ldr r1, LCPI1_1 8160 // add r1, pc, r1 8161 // str r1, [$jbuf, #+4] ; &jbuf[1] 8162 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8163 BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 8164 .addConstantPoolIndex(CPI) 8165 .addImm(0) 8166 .addMemOperand(CPMMO) 8167 .add(predOps(ARMCC::AL)); 8168 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8169 BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 8170 .addReg(NewVReg1, RegState::Kill) 8171 .addImm(PCLabelId) 8172 .add(predOps(ARMCC::AL)); 8173 BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 8174 .addReg(NewVReg2, RegState::Kill) 8175 .addFrameIndex(FI) 8176 .addImm(36) // &jbuf[1] :: pc 8177 .addMemOperand(FIMMOSt) 8178 .add(predOps(ARMCC::AL)); 8179 } 8180 } 8181 8182 void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI, 8183 MachineBasicBlock *MBB) const { 8184 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8185 DebugLoc dl = MI.getDebugLoc(); 8186 MachineFunction *MF = MBB->getParent(); 8187 MachineRegisterInfo *MRI = &MF->getRegInfo(); 8188 MachineFrameInfo &MFI = MF->getFrameInfo(); 8189 int FI = MFI.getFunctionContextIndex(); 8190 8191 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass 8192 : &ARM::GPRnopcRegClass; 8193 8194 // Get a mapping of the call site numbers to all of the landing pads they're 8195 // associated with. 8196 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2>> CallSiteNumToLPad; 8197 unsigned MaxCSNum = 0; 8198 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 8199 ++BB) { 8200 if (!BB->isEHPad()) continue; 8201 8202 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 8203 // pad. 8204 for (MachineBasicBlock::iterator 8205 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 8206 if (!II->isEHLabel()) continue; 8207 8208 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 8209 if (!MF->hasCallSiteLandingPad(Sym)) continue; 8210 8211 SmallVectorImpl<unsigned> &CallSiteIdxs = MF->getCallSiteLandingPad(Sym); 8212 for (SmallVectorImpl<unsigned>::iterator 8213 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 8214 CSI != CSE; ++CSI) { 8215 CallSiteNumToLPad[*CSI].push_back(&*BB); 8216 MaxCSNum = std::max(MaxCSNum, *CSI); 8217 } 8218 break; 8219 } 8220 } 8221 8222 // Get an ordered list of the machine basic blocks for the jump table. 8223 std::vector<MachineBasicBlock*> LPadList; 8224 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs; 8225 LPadList.reserve(CallSiteNumToLPad.size()); 8226 for (unsigned I = 1; I <= MaxCSNum; ++I) { 8227 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 8228 for (SmallVectorImpl<MachineBasicBlock*>::iterator 8229 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 8230 LPadList.push_back(*II); 8231 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 8232 } 8233 } 8234 8235 assert(!LPadList.empty() && 8236 "No landing pad destinations for the dispatch jump table!"); 8237 8238 // Create the jump table and associated information. 8239 MachineJumpTableInfo *JTI = 8240 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 8241 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 8242 8243 // Create the MBBs for the dispatch code. 8244 8245 // Shove the dispatch's address into the return slot in the function context. 8246 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 8247 DispatchBB->setIsEHPad(); 8248 8249 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 8250 unsigned trap_opcode; 8251 if (Subtarget->isThumb()) 8252 trap_opcode = ARM::tTRAP; 8253 else 8254 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 8255 8256 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 8257 DispatchBB->addSuccessor(TrapBB); 8258 8259 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 8260 DispatchBB->addSuccessor(DispContBB); 8261 8262 // Insert and MBBs. 8263 MF->insert(MF->end(), DispatchBB); 8264 MF->insert(MF->end(), DispContBB); 8265 MF->insert(MF->end(), TrapBB); 8266 8267 // Insert code into the entry block that creates and registers the function 8268 // context. 8269 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 8270 8271 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand( 8272 MachinePointerInfo::getFixedStack(*MF, FI), 8273 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4); 8274 8275 MachineInstrBuilder MIB; 8276 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 8277 8278 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 8279 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 8280 8281 // Add a register mask with no preserved registers. This results in all 8282 // registers being marked as clobbered. This can't work if the dispatch block 8283 // is in a Thumb1 function and is linked with ARM code which uses the FP 8284 // registers, as there is no way to preserve the FP registers in Thumb1 mode. 8285 MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF)); 8286 8287 bool IsPositionIndependent = isPositionIndependent(); 8288 unsigned NumLPads = LPadList.size(); 8289 if (Subtarget->isThumb2()) { 8290 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8291 BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 8292 .addFrameIndex(FI) 8293 .addImm(4) 8294 .addMemOperand(FIMMOLd) 8295 .add(predOps(ARMCC::AL)); 8296 8297 if (NumLPads < 256) { 8298 BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 8299 .addReg(NewVReg1) 8300 .addImm(LPadList.size()) 8301 .add(predOps(ARMCC::AL)); 8302 } else { 8303 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8304 BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 8305 .addImm(NumLPads & 0xFFFF) 8306 .add(predOps(ARMCC::AL)); 8307 8308 unsigned VReg2 = VReg1; 8309 if ((NumLPads & 0xFFFF0000) != 0) { 8310 VReg2 = MRI->createVirtualRegister(TRC); 8311 BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 8312 .addReg(VReg1) 8313 .addImm(NumLPads >> 16) 8314 .add(predOps(ARMCC::AL)); 8315 } 8316 8317 BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 8318 .addReg(NewVReg1) 8319 .addReg(VReg2) 8320 .add(predOps(ARMCC::AL)); 8321 } 8322 8323 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 8324 .addMBB(TrapBB) 8325 .addImm(ARMCC::HI) 8326 .addReg(ARM::CPSR); 8327 8328 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8329 BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT), NewVReg3) 8330 .addJumpTableIndex(MJTI) 8331 .add(predOps(ARMCC::AL)); 8332 8333 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8334 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 8335 .addReg(NewVReg3, RegState::Kill) 8336 .addReg(NewVReg1) 8337 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)) 8338 .add(predOps(ARMCC::AL)) 8339 .add(condCodeOp()); 8340 8341 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 8342 .addReg(NewVReg4, RegState::Kill) 8343 .addReg(NewVReg1) 8344 .addJumpTableIndex(MJTI); 8345 } else if (Subtarget->isThumb()) { 8346 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8347 BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 8348 .addFrameIndex(FI) 8349 .addImm(1) 8350 .addMemOperand(FIMMOLd) 8351 .add(predOps(ARMCC::AL)); 8352 8353 if (NumLPads < 256) { 8354 BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 8355 .addReg(NewVReg1) 8356 .addImm(NumLPads) 8357 .add(predOps(ARMCC::AL)); 8358 } else { 8359 MachineConstantPool *ConstantPool = MF->getConstantPool(); 8360 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 8361 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 8362 8363 // MachineConstantPool wants an explicit alignment. 8364 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 8365 if (Align == 0) 8366 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 8367 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 8368 8369 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8370 BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 8371 .addReg(VReg1, RegState::Define) 8372 .addConstantPoolIndex(Idx) 8373 .add(predOps(ARMCC::AL)); 8374 BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 8375 .addReg(NewVReg1) 8376 .addReg(VReg1) 8377 .add(predOps(ARMCC::AL)); 8378 } 8379 8380 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 8381 .addMBB(TrapBB) 8382 .addImm(ARMCC::HI) 8383 .addReg(ARM::CPSR); 8384 8385 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 8386 BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 8387 .addReg(ARM::CPSR, RegState::Define) 8388 .addReg(NewVReg1) 8389 .addImm(2) 8390 .add(predOps(ARMCC::AL)); 8391 8392 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8393 BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 8394 .addJumpTableIndex(MJTI) 8395 .add(predOps(ARMCC::AL)); 8396 8397 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8398 BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 8399 .addReg(ARM::CPSR, RegState::Define) 8400 .addReg(NewVReg2, RegState::Kill) 8401 .addReg(NewVReg3) 8402 .add(predOps(ARMCC::AL)); 8403 8404 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 8405 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 8406 8407 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 8408 BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 8409 .addReg(NewVReg4, RegState::Kill) 8410 .addImm(0) 8411 .addMemOperand(JTMMOLd) 8412 .add(predOps(ARMCC::AL)); 8413 8414 unsigned NewVReg6 = NewVReg5; 8415 if (IsPositionIndependent) { 8416 NewVReg6 = MRI->createVirtualRegister(TRC); 8417 BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 8418 .addReg(ARM::CPSR, RegState::Define) 8419 .addReg(NewVReg5, RegState::Kill) 8420 .addReg(NewVReg3) 8421 .add(predOps(ARMCC::AL)); 8422 } 8423 8424 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 8425 .addReg(NewVReg6, RegState::Kill) 8426 .addJumpTableIndex(MJTI); 8427 } else { 8428 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 8429 BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 8430 .addFrameIndex(FI) 8431 .addImm(4) 8432 .addMemOperand(FIMMOLd) 8433 .add(predOps(ARMCC::AL)); 8434 8435 if (NumLPads < 256) { 8436 BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 8437 .addReg(NewVReg1) 8438 .addImm(NumLPads) 8439 .add(predOps(ARMCC::AL)); 8440 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 8441 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8442 BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 8443 .addImm(NumLPads & 0xFFFF) 8444 .add(predOps(ARMCC::AL)); 8445 8446 unsigned VReg2 = VReg1; 8447 if ((NumLPads & 0xFFFF0000) != 0) { 8448 VReg2 = MRI->createVirtualRegister(TRC); 8449 BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 8450 .addReg(VReg1) 8451 .addImm(NumLPads >> 16) 8452 .add(predOps(ARMCC::AL)); 8453 } 8454 8455 BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 8456 .addReg(NewVReg1) 8457 .addReg(VReg2) 8458 .add(predOps(ARMCC::AL)); 8459 } else { 8460 MachineConstantPool *ConstantPool = MF->getConstantPool(); 8461 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 8462 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 8463 8464 // MachineConstantPool wants an explicit alignment. 8465 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 8466 if (Align == 0) 8467 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 8468 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 8469 8470 unsigned VReg1 = MRI->createVirtualRegister(TRC); 8471 BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 8472 .addReg(VReg1, RegState::Define) 8473 .addConstantPoolIndex(Idx) 8474 .addImm(0) 8475 .add(predOps(ARMCC::AL)); 8476 BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 8477 .addReg(NewVReg1) 8478 .addReg(VReg1, RegState::Kill) 8479 .add(predOps(ARMCC::AL)); 8480 } 8481 8482 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 8483 .addMBB(TrapBB) 8484 .addImm(ARMCC::HI) 8485 .addReg(ARM::CPSR); 8486 8487 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 8488 BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 8489 .addReg(NewVReg1) 8490 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)) 8491 .add(predOps(ARMCC::AL)) 8492 .add(condCodeOp()); 8493 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 8494 BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 8495 .addJumpTableIndex(MJTI) 8496 .add(predOps(ARMCC::AL)); 8497 8498 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 8499 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 8500 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 8501 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 8502 .addReg(NewVReg3, RegState::Kill) 8503 .addReg(NewVReg4) 8504 .addImm(0) 8505 .addMemOperand(JTMMOLd) 8506 .add(predOps(ARMCC::AL)); 8507 8508 if (IsPositionIndependent) { 8509 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 8510 .addReg(NewVReg5, RegState::Kill) 8511 .addReg(NewVReg4) 8512 .addJumpTableIndex(MJTI); 8513 } else { 8514 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 8515 .addReg(NewVReg5, RegState::Kill) 8516 .addJumpTableIndex(MJTI); 8517 } 8518 } 8519 8520 // Add the jump table entries as successors to the MBB. 8521 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 8522 for (std::vector<MachineBasicBlock*>::iterator 8523 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 8524 MachineBasicBlock *CurMBB = *I; 8525 if (SeenMBBs.insert(CurMBB).second) 8526 DispContBB->addSuccessor(CurMBB); 8527 } 8528 8529 // N.B. the order the invoke BBs are processed in doesn't matter here. 8530 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF); 8531 SmallVector<MachineBasicBlock*, 64> MBBLPads; 8532 for (MachineBasicBlock *BB : InvokeBBs) { 8533 8534 // Remove the landing pad successor from the invoke block and replace it 8535 // with the new dispatch block. 8536 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 8537 BB->succ_end()); 8538 while (!Successors.empty()) { 8539 MachineBasicBlock *SMBB = Successors.pop_back_val(); 8540 if (SMBB->isEHPad()) { 8541 BB->removeSuccessor(SMBB); 8542 MBBLPads.push_back(SMBB); 8543 } 8544 } 8545 8546 BB->addSuccessor(DispatchBB, BranchProbability::getZero()); 8547 BB->normalizeSuccProbs(); 8548 8549 // Find the invoke call and mark all of the callee-saved registers as 8550 // 'implicit defined' so that they're spilled. This prevents code from 8551 // moving instructions to before the EH block, where they will never be 8552 // executed. 8553 for (MachineBasicBlock::reverse_iterator 8554 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 8555 if (!II->isCall()) continue; 8556 8557 DenseMap<unsigned, bool> DefRegs; 8558 for (MachineInstr::mop_iterator 8559 OI = II->operands_begin(), OE = II->operands_end(); 8560 OI != OE; ++OI) { 8561 if (!OI->isReg()) continue; 8562 DefRegs[OI->getReg()] = true; 8563 } 8564 8565 MachineInstrBuilder MIB(*MF, &*II); 8566 8567 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 8568 unsigned Reg = SavedRegs[i]; 8569 if (Subtarget->isThumb2() && 8570 !ARM::tGPRRegClass.contains(Reg) && 8571 !ARM::hGPRRegClass.contains(Reg)) 8572 continue; 8573 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 8574 continue; 8575 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 8576 continue; 8577 if (!DefRegs[Reg]) 8578 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 8579 } 8580 8581 break; 8582 } 8583 } 8584 8585 // Mark all former landing pads as non-landing pads. The dispatch is the only 8586 // landing pad now. 8587 for (SmallVectorImpl<MachineBasicBlock*>::iterator 8588 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 8589 (*I)->setIsEHPad(false); 8590 8591 // The instruction is gone now. 8592 MI.eraseFromParent(); 8593 } 8594 8595 static 8596 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 8597 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 8598 E = MBB->succ_end(); I != E; ++I) 8599 if (*I != Succ) 8600 return *I; 8601 llvm_unreachable("Expecting a BB with two successors!"); 8602 } 8603 8604 /// Return the load opcode for a given load size. If load size >= 8, 8605 /// neon opcode will be returned. 8606 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) { 8607 if (LdSize >= 8) 8608 return LdSize == 16 ? ARM::VLD1q32wb_fixed 8609 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0; 8610 if (IsThumb1) 8611 return LdSize == 4 ? ARM::tLDRi 8612 : LdSize == 2 ? ARM::tLDRHi 8613 : LdSize == 1 ? ARM::tLDRBi : 0; 8614 if (IsThumb2) 8615 return LdSize == 4 ? ARM::t2LDR_POST 8616 : LdSize == 2 ? ARM::t2LDRH_POST 8617 : LdSize == 1 ? ARM::t2LDRB_POST : 0; 8618 return LdSize == 4 ? ARM::LDR_POST_IMM 8619 : LdSize == 2 ? ARM::LDRH_POST 8620 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0; 8621 } 8622 8623 /// Return the store opcode for a given store size. If store size >= 8, 8624 /// neon opcode will be returned. 8625 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) { 8626 if (StSize >= 8) 8627 return StSize == 16 ? ARM::VST1q32wb_fixed 8628 : StSize == 8 ? ARM::VST1d32wb_fixed : 0; 8629 if (IsThumb1) 8630 return StSize == 4 ? ARM::tSTRi 8631 : StSize == 2 ? ARM::tSTRHi 8632 : StSize == 1 ? ARM::tSTRBi : 0; 8633 if (IsThumb2) 8634 return StSize == 4 ? ARM::t2STR_POST 8635 : StSize == 2 ? ARM::t2STRH_POST 8636 : StSize == 1 ? ARM::t2STRB_POST : 0; 8637 return StSize == 4 ? ARM::STR_POST_IMM 8638 : StSize == 2 ? ARM::STRH_POST 8639 : StSize == 1 ? ARM::STRB_POST_IMM : 0; 8640 } 8641 8642 /// Emit a post-increment load operation with given size. The instructions 8643 /// will be added to BB at Pos. 8644 static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos, 8645 const TargetInstrInfo *TII, const DebugLoc &dl, 8646 unsigned LdSize, unsigned Data, unsigned AddrIn, 8647 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 8648 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2); 8649 assert(LdOpc != 0 && "Should have a load opcode"); 8650 if (LdSize >= 8) { 8651 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8652 .addReg(AddrOut, RegState::Define) 8653 .addReg(AddrIn) 8654 .addImm(0) 8655 .add(predOps(ARMCC::AL)); 8656 } else if (IsThumb1) { 8657 // load + update AddrIn 8658 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8659 .addReg(AddrIn) 8660 .addImm(0) 8661 .add(predOps(ARMCC::AL)); 8662 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut) 8663 .add(t1CondCodeOp()) 8664 .addReg(AddrIn) 8665 .addImm(LdSize) 8666 .add(predOps(ARMCC::AL)); 8667 } else if (IsThumb2) { 8668 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8669 .addReg(AddrOut, RegState::Define) 8670 .addReg(AddrIn) 8671 .addImm(LdSize) 8672 .add(predOps(ARMCC::AL)); 8673 } else { // arm 8674 BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 8675 .addReg(AddrOut, RegState::Define) 8676 .addReg(AddrIn) 8677 .addReg(0) 8678 .addImm(LdSize) 8679 .add(predOps(ARMCC::AL)); 8680 } 8681 } 8682 8683 /// Emit a post-increment store operation with given size. The instructions 8684 /// will be added to BB at Pos. 8685 static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos, 8686 const TargetInstrInfo *TII, const DebugLoc &dl, 8687 unsigned StSize, unsigned Data, unsigned AddrIn, 8688 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 8689 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2); 8690 assert(StOpc != 0 && "Should have a store opcode"); 8691 if (StSize >= 8) { 8692 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 8693 .addReg(AddrIn) 8694 .addImm(0) 8695 .addReg(Data) 8696 .add(predOps(ARMCC::AL)); 8697 } else if (IsThumb1) { 8698 // store + update AddrIn 8699 BuildMI(*BB, Pos, dl, TII->get(StOpc)) 8700 .addReg(Data) 8701 .addReg(AddrIn) 8702 .addImm(0) 8703 .add(predOps(ARMCC::AL)); 8704 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut) 8705 .add(t1CondCodeOp()) 8706 .addReg(AddrIn) 8707 .addImm(StSize) 8708 .add(predOps(ARMCC::AL)); 8709 } else if (IsThumb2) { 8710 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 8711 .addReg(Data) 8712 .addReg(AddrIn) 8713 .addImm(StSize) 8714 .add(predOps(ARMCC::AL)); 8715 } else { // arm 8716 BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 8717 .addReg(Data) 8718 .addReg(AddrIn) 8719 .addReg(0) 8720 .addImm(StSize) 8721 .add(predOps(ARMCC::AL)); 8722 } 8723 } 8724 8725 MachineBasicBlock * 8726 ARMTargetLowering::EmitStructByval(MachineInstr &MI, 8727 MachineBasicBlock *BB) const { 8728 // This pseudo instruction has 3 operands: dst, src, size 8729 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 8730 // Otherwise, we will generate unrolled scalar copies. 8731 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8732 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8733 MachineFunction::iterator It = ++BB->getIterator(); 8734 8735 unsigned dest = MI.getOperand(0).getReg(); 8736 unsigned src = MI.getOperand(1).getReg(); 8737 unsigned SizeVal = MI.getOperand(2).getImm(); 8738 unsigned Align = MI.getOperand(3).getImm(); 8739 DebugLoc dl = MI.getDebugLoc(); 8740 8741 MachineFunction *MF = BB->getParent(); 8742 MachineRegisterInfo &MRI = MF->getRegInfo(); 8743 unsigned UnitSize = 0; 8744 const TargetRegisterClass *TRC = nullptr; 8745 const TargetRegisterClass *VecTRC = nullptr; 8746 8747 bool IsThumb1 = Subtarget->isThumb1Only(); 8748 bool IsThumb2 = Subtarget->isThumb2(); 8749 bool IsThumb = Subtarget->isThumb(); 8750 8751 if (Align & 1) { 8752 UnitSize = 1; 8753 } else if (Align & 2) { 8754 UnitSize = 2; 8755 } else { 8756 // Check whether we can use NEON instructions. 8757 if (!MF->getFunction().hasFnAttribute(Attribute::NoImplicitFloat) && 8758 Subtarget->hasNEON()) { 8759 if ((Align % 16 == 0) && SizeVal >= 16) 8760 UnitSize = 16; 8761 else if ((Align % 8 == 0) && SizeVal >= 8) 8762 UnitSize = 8; 8763 } 8764 // Can't use NEON instructions. 8765 if (UnitSize == 0) 8766 UnitSize = 4; 8767 } 8768 8769 // Select the correct opcode and register class for unit size load/store 8770 bool IsNeon = UnitSize >= 8; 8771 TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 8772 if (IsNeon) 8773 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass 8774 : UnitSize == 8 ? &ARM::DPRRegClass 8775 : nullptr; 8776 8777 unsigned BytesLeft = SizeVal % UnitSize; 8778 unsigned LoopSize = SizeVal - BytesLeft; 8779 8780 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 8781 // Use LDR and STR to copy. 8782 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 8783 // [destOut] = STR_POST(scratch, destIn, UnitSize) 8784 unsigned srcIn = src; 8785 unsigned destIn = dest; 8786 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 8787 unsigned srcOut = MRI.createVirtualRegister(TRC); 8788 unsigned destOut = MRI.createVirtualRegister(TRC); 8789 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 8790 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut, 8791 IsThumb1, IsThumb2); 8792 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut, 8793 IsThumb1, IsThumb2); 8794 srcIn = srcOut; 8795 destIn = destOut; 8796 } 8797 8798 // Handle the leftover bytes with LDRB and STRB. 8799 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 8800 // [destOut] = STRB_POST(scratch, destIn, 1) 8801 for (unsigned i = 0; i < BytesLeft; i++) { 8802 unsigned srcOut = MRI.createVirtualRegister(TRC); 8803 unsigned destOut = MRI.createVirtualRegister(TRC); 8804 unsigned scratch = MRI.createVirtualRegister(TRC); 8805 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut, 8806 IsThumb1, IsThumb2); 8807 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut, 8808 IsThumb1, IsThumb2); 8809 srcIn = srcOut; 8810 destIn = destOut; 8811 } 8812 MI.eraseFromParent(); // The instruction is gone now. 8813 return BB; 8814 } 8815 8816 // Expand the pseudo op to a loop. 8817 // thisMBB: 8818 // ... 8819 // movw varEnd, # --> with thumb2 8820 // movt varEnd, # 8821 // ldrcp varEnd, idx --> without thumb2 8822 // fallthrough --> loopMBB 8823 // loopMBB: 8824 // PHI varPhi, varEnd, varLoop 8825 // PHI srcPhi, src, srcLoop 8826 // PHI destPhi, dst, destLoop 8827 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 8828 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 8829 // subs varLoop, varPhi, #UnitSize 8830 // bne loopMBB 8831 // fallthrough --> exitMBB 8832 // exitMBB: 8833 // epilogue to handle left-over bytes 8834 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 8835 // [destOut] = STRB_POST(scratch, destLoop, 1) 8836 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 8837 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 8838 MF->insert(It, loopMBB); 8839 MF->insert(It, exitMBB); 8840 8841 // Transfer the remainder of BB and its successor edges to exitMBB. 8842 exitMBB->splice(exitMBB->begin(), BB, 8843 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8844 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8845 8846 // Load an immediate to varEnd. 8847 unsigned varEnd = MRI.createVirtualRegister(TRC); 8848 if (Subtarget->useMovt(*MF)) { 8849 unsigned Vtmp = varEnd; 8850 if ((LoopSize & 0xFFFF0000) != 0) 8851 Vtmp = MRI.createVirtualRegister(TRC); 8852 BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16), Vtmp) 8853 .addImm(LoopSize & 0xFFFF) 8854 .add(predOps(ARMCC::AL)); 8855 8856 if ((LoopSize & 0xFFFF0000) != 0) 8857 BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16), varEnd) 8858 .addReg(Vtmp) 8859 .addImm(LoopSize >> 16) 8860 .add(predOps(ARMCC::AL)); 8861 } else { 8862 MachineConstantPool *ConstantPool = MF->getConstantPool(); 8863 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext()); 8864 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 8865 8866 // MachineConstantPool wants an explicit alignment. 8867 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 8868 if (Align == 0) 8869 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 8870 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 8871 8872 if (IsThumb) 8873 BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)) 8874 .addReg(varEnd, RegState::Define) 8875 .addConstantPoolIndex(Idx) 8876 .add(predOps(ARMCC::AL)); 8877 else 8878 BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)) 8879 .addReg(varEnd, RegState::Define) 8880 .addConstantPoolIndex(Idx) 8881 .addImm(0) 8882 .add(predOps(ARMCC::AL)); 8883 } 8884 BB->addSuccessor(loopMBB); 8885 8886 // Generate the loop body: 8887 // varPhi = PHI(varLoop, varEnd) 8888 // srcPhi = PHI(srcLoop, src) 8889 // destPhi = PHI(destLoop, dst) 8890 MachineBasicBlock *entryBB = BB; 8891 BB = loopMBB; 8892 unsigned varLoop = MRI.createVirtualRegister(TRC); 8893 unsigned varPhi = MRI.createVirtualRegister(TRC); 8894 unsigned srcLoop = MRI.createVirtualRegister(TRC); 8895 unsigned srcPhi = MRI.createVirtualRegister(TRC); 8896 unsigned destLoop = MRI.createVirtualRegister(TRC); 8897 unsigned destPhi = MRI.createVirtualRegister(TRC); 8898 8899 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 8900 .addReg(varLoop).addMBB(loopMBB) 8901 .addReg(varEnd).addMBB(entryBB); 8902 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 8903 .addReg(srcLoop).addMBB(loopMBB) 8904 .addReg(src).addMBB(entryBB); 8905 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 8906 .addReg(destLoop).addMBB(loopMBB) 8907 .addReg(dest).addMBB(entryBB); 8908 8909 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 8910 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 8911 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 8912 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop, 8913 IsThumb1, IsThumb2); 8914 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop, 8915 IsThumb1, IsThumb2); 8916 8917 // Decrement loop variable by UnitSize. 8918 if (IsThumb1) { 8919 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop) 8920 .add(t1CondCodeOp()) 8921 .addReg(varPhi) 8922 .addImm(UnitSize) 8923 .add(predOps(ARMCC::AL)); 8924 } else { 8925 MachineInstrBuilder MIB = 8926 BuildMI(*BB, BB->end(), dl, 8927 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 8928 MIB.addReg(varPhi) 8929 .addImm(UnitSize) 8930 .add(predOps(ARMCC::AL)) 8931 .add(condCodeOp()); 8932 MIB->getOperand(5).setReg(ARM::CPSR); 8933 MIB->getOperand(5).setIsDef(true); 8934 } 8935 BuildMI(*BB, BB->end(), dl, 8936 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc)) 8937 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 8938 8939 // loopMBB can loop back to loopMBB or fall through to exitMBB. 8940 BB->addSuccessor(loopMBB); 8941 BB->addSuccessor(exitMBB); 8942 8943 // Add epilogue to handle BytesLeft. 8944 BB = exitMBB; 8945 auto StartOfExit = exitMBB->begin(); 8946 8947 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 8948 // [destOut] = STRB_POST(scratch, destLoop, 1) 8949 unsigned srcIn = srcLoop; 8950 unsigned destIn = destLoop; 8951 for (unsigned i = 0; i < BytesLeft; i++) { 8952 unsigned srcOut = MRI.createVirtualRegister(TRC); 8953 unsigned destOut = MRI.createVirtualRegister(TRC); 8954 unsigned scratch = MRI.createVirtualRegister(TRC); 8955 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut, 8956 IsThumb1, IsThumb2); 8957 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut, 8958 IsThumb1, IsThumb2); 8959 srcIn = srcOut; 8960 destIn = destOut; 8961 } 8962 8963 MI.eraseFromParent(); // The instruction is gone now. 8964 return BB; 8965 } 8966 8967 MachineBasicBlock * 8968 ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI, 8969 MachineBasicBlock *MBB) const { 8970 const TargetMachine &TM = getTargetMachine(); 8971 const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 8972 DebugLoc DL = MI.getDebugLoc(); 8973 8974 assert(Subtarget->isTargetWindows() && 8975 "__chkstk is only supported on Windows"); 8976 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode"); 8977 8978 // __chkstk takes the number of words to allocate on the stack in R4, and 8979 // returns the stack adjustment in number of bytes in R4. This will not 8980 // clober any other registers (other than the obvious lr). 8981 // 8982 // Although, technically, IP should be considered a register which may be 8983 // clobbered, the call itself will not touch it. Windows on ARM is a pure 8984 // thumb-2 environment, so there is no interworking required. As a result, we 8985 // do not expect a veneer to be emitted by the linker, clobbering IP. 8986 // 8987 // Each module receives its own copy of __chkstk, so no import thunk is 8988 // required, again, ensuring that IP is not clobbered. 8989 // 8990 // Finally, although some linkers may theoretically provide a trampoline for 8991 // out of range calls (which is quite common due to a 32M range limitation of 8992 // branches for Thumb), we can generate the long-call version via 8993 // -mcmodel=large, alleviating the need for the trampoline which may clobber 8994 // IP. 8995 8996 switch (TM.getCodeModel()) { 8997 case CodeModel::Small: 8998 case CodeModel::Medium: 8999 case CodeModel::Kernel: 9000 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL)) 9001 .add(predOps(ARMCC::AL)) 9002 .addExternalSymbol("__chkstk") 9003 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 9004 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 9005 .addReg(ARM::R12, 9006 RegState::Implicit | RegState::Define | RegState::Dead) 9007 .addReg(ARM::CPSR, 9008 RegState::Implicit | RegState::Define | RegState::Dead); 9009 break; 9010 case CodeModel::Large: { 9011 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 9012 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass); 9013 9014 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg) 9015 .addExternalSymbol("__chkstk"); 9016 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr)) 9017 .add(predOps(ARMCC::AL)) 9018 .addReg(Reg, RegState::Kill) 9019 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 9020 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 9021 .addReg(ARM::R12, 9022 RegState::Implicit | RegState::Define | RegState::Dead) 9023 .addReg(ARM::CPSR, 9024 RegState::Implicit | RegState::Define | RegState::Dead); 9025 break; 9026 } 9027 } 9028 9029 BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), ARM::SP) 9030 .addReg(ARM::SP, RegState::Kill) 9031 .addReg(ARM::R4, RegState::Kill) 9032 .setMIFlags(MachineInstr::FrameSetup) 9033 .add(predOps(ARMCC::AL)) 9034 .add(condCodeOp()); 9035 9036 MI.eraseFromParent(); 9037 return MBB; 9038 } 9039 9040 MachineBasicBlock * 9041 ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI, 9042 MachineBasicBlock *MBB) const { 9043 DebugLoc DL = MI.getDebugLoc(); 9044 MachineFunction *MF = MBB->getParent(); 9045 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 9046 9047 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock(); 9048 MF->insert(++MBB->getIterator(), ContBB); 9049 ContBB->splice(ContBB->begin(), MBB, 9050 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 9051 ContBB->transferSuccessorsAndUpdatePHIs(MBB); 9052 MBB->addSuccessor(ContBB); 9053 9054 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 9055 BuildMI(TrapBB, DL, TII->get(ARM::t__brkdiv0)); 9056 MF->push_back(TrapBB); 9057 MBB->addSuccessor(TrapBB); 9058 9059 BuildMI(*MBB, MI, DL, TII->get(ARM::tCMPi8)) 9060 .addReg(MI.getOperand(0).getReg()) 9061 .addImm(0) 9062 .add(predOps(ARMCC::AL)); 9063 BuildMI(*MBB, MI, DL, TII->get(ARM::t2Bcc)) 9064 .addMBB(TrapBB) 9065 .addImm(ARMCC::EQ) 9066 .addReg(ARM::CPSR); 9067 9068 MI.eraseFromParent(); 9069 return ContBB; 9070 } 9071 9072 MachineBasicBlock * 9073 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 9074 MachineBasicBlock *BB) const { 9075 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 9076 DebugLoc dl = MI.getDebugLoc(); 9077 bool isThumb2 = Subtarget->isThumb2(); 9078 switch (MI.getOpcode()) { 9079 default: { 9080 MI.print(errs()); 9081 llvm_unreachable("Unexpected instr type to insert"); 9082 } 9083 9084 // Thumb1 post-indexed loads are really just single-register LDMs. 9085 case ARM::tLDR_postidx: { 9086 MachineOperand Def(MI.getOperand(1)); 9087 if (TargetRegisterInfo::isPhysicalRegister(Def.getReg())) 9088 Def.setIsRenamable(false); 9089 BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD)) 9090 .add(Def) // Rn_wb 9091 .add(MI.getOperand(2)) // Rn 9092 .add(MI.getOperand(3)) // PredImm 9093 .add(MI.getOperand(4)) // PredReg 9094 .add(MI.getOperand(0)); // Rt 9095 MI.eraseFromParent(); 9096 return BB; 9097 } 9098 9099 // The Thumb2 pre-indexed stores have the same MI operands, they just 9100 // define them differently in the .td files from the isel patterns, so 9101 // they need pseudos. 9102 case ARM::t2STR_preidx: 9103 MI.setDesc(TII->get(ARM::t2STR_PRE)); 9104 return BB; 9105 case ARM::t2STRB_preidx: 9106 MI.setDesc(TII->get(ARM::t2STRB_PRE)); 9107 return BB; 9108 case ARM::t2STRH_preidx: 9109 MI.setDesc(TII->get(ARM::t2STRH_PRE)); 9110 return BB; 9111 9112 case ARM::STRi_preidx: 9113 case ARM::STRBi_preidx: { 9114 unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM 9115 : ARM::STRB_PRE_IMM; 9116 // Decode the offset. 9117 unsigned Offset = MI.getOperand(4).getImm(); 9118 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 9119 Offset = ARM_AM::getAM2Offset(Offset); 9120 if (isSub) 9121 Offset = -Offset; 9122 9123 MachineMemOperand *MMO = *MI.memoperands_begin(); 9124 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 9125 .add(MI.getOperand(0)) // Rn_wb 9126 .add(MI.getOperand(1)) // Rt 9127 .add(MI.getOperand(2)) // Rn 9128 .addImm(Offset) // offset (skip GPR==zero_reg) 9129 .add(MI.getOperand(5)) // pred 9130 .add(MI.getOperand(6)) 9131 .addMemOperand(MMO); 9132 MI.eraseFromParent(); 9133 return BB; 9134 } 9135 case ARM::STRr_preidx: 9136 case ARM::STRBr_preidx: 9137 case ARM::STRH_preidx: { 9138 unsigned NewOpc; 9139 switch (MI.getOpcode()) { 9140 default: llvm_unreachable("unexpected opcode!"); 9141 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 9142 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 9143 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 9144 } 9145 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 9146 for (unsigned i = 0; i < MI.getNumOperands(); ++i) 9147 MIB.add(MI.getOperand(i)); 9148 MI.eraseFromParent(); 9149 return BB; 9150 } 9151 9152 case ARM::tMOVCCr_pseudo: { 9153 // To "insert" a SELECT_CC instruction, we actually have to insert the 9154 // diamond control-flow pattern. The incoming instruction knows the 9155 // destination vreg to set, the condition code register to branch on, the 9156 // true/false values to select between, and a branch opcode to use. 9157 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 9158 MachineFunction::iterator It = ++BB->getIterator(); 9159 9160 // thisMBB: 9161 // ... 9162 // TrueVal = ... 9163 // cmpTY ccX, r1, r2 9164 // bCC copy1MBB 9165 // fallthrough --> copy0MBB 9166 MachineBasicBlock *thisMBB = BB; 9167 MachineFunction *F = BB->getParent(); 9168 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 9169 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 9170 F->insert(It, copy0MBB); 9171 F->insert(It, sinkMBB); 9172 9173 // Transfer the remainder of BB and its successor edges to sinkMBB. 9174 sinkMBB->splice(sinkMBB->begin(), BB, 9175 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9176 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 9177 9178 BB->addSuccessor(copy0MBB); 9179 BB->addSuccessor(sinkMBB); 9180 9181 BuildMI(BB, dl, TII->get(ARM::tBcc)) 9182 .addMBB(sinkMBB) 9183 .addImm(MI.getOperand(3).getImm()) 9184 .addReg(MI.getOperand(4).getReg()); 9185 9186 // copy0MBB: 9187 // %FalseValue = ... 9188 // # fallthrough to sinkMBB 9189 BB = copy0MBB; 9190 9191 // Update machine-CFG edges 9192 BB->addSuccessor(sinkMBB); 9193 9194 // sinkMBB: 9195 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 9196 // ... 9197 BB = sinkMBB; 9198 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg()) 9199 .addReg(MI.getOperand(1).getReg()) 9200 .addMBB(copy0MBB) 9201 .addReg(MI.getOperand(2).getReg()) 9202 .addMBB(thisMBB); 9203 9204 MI.eraseFromParent(); // The pseudo instruction is gone now. 9205 return BB; 9206 } 9207 9208 case ARM::BCCi64: 9209 case ARM::BCCZi64: { 9210 // If there is an unconditional branch to the other successor, remove it. 9211 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9212 9213 // Compare both parts that make up the double comparison separately for 9214 // equality. 9215 bool RHSisZero = MI.getOpcode() == ARM::BCCZi64; 9216 9217 unsigned LHS1 = MI.getOperand(1).getReg(); 9218 unsigned LHS2 = MI.getOperand(2).getReg(); 9219 if (RHSisZero) { 9220 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 9221 .addReg(LHS1) 9222 .addImm(0) 9223 .add(predOps(ARMCC::AL)); 9224 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 9225 .addReg(LHS2).addImm(0) 9226 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 9227 } else { 9228 unsigned RHS1 = MI.getOperand(3).getReg(); 9229 unsigned RHS2 = MI.getOperand(4).getReg(); 9230 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 9231 .addReg(LHS1) 9232 .addReg(RHS1) 9233 .add(predOps(ARMCC::AL)); 9234 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 9235 .addReg(LHS2).addReg(RHS2) 9236 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 9237 } 9238 9239 MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB(); 9240 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 9241 if (MI.getOperand(0).getImm() == ARMCC::NE) 9242 std::swap(destMBB, exitMBB); 9243 9244 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 9245 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 9246 if (isThumb2) 9247 BuildMI(BB, dl, TII->get(ARM::t2B)) 9248 .addMBB(exitMBB) 9249 .add(predOps(ARMCC::AL)); 9250 else 9251 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 9252 9253 MI.eraseFromParent(); // The pseudo instruction is gone now. 9254 return BB; 9255 } 9256 9257 case ARM::Int_eh_sjlj_setjmp: 9258 case ARM::Int_eh_sjlj_setjmp_nofp: 9259 case ARM::tInt_eh_sjlj_setjmp: 9260 case ARM::t2Int_eh_sjlj_setjmp: 9261 case ARM::t2Int_eh_sjlj_setjmp_nofp: 9262 return BB; 9263 9264 case ARM::Int_eh_sjlj_setup_dispatch: 9265 EmitSjLjDispatchBlock(MI, BB); 9266 return BB; 9267 9268 case ARM::ABS: 9269 case ARM::t2ABS: { 9270 // To insert an ABS instruction, we have to insert the 9271 // diamond control-flow pattern. The incoming instruction knows the 9272 // source vreg to test against 0, the destination vreg to set, 9273 // the condition code register to branch on, the 9274 // true/false values to select between, and a branch opcode to use. 9275 // It transforms 9276 // V1 = ABS V0 9277 // into 9278 // V2 = MOVS V0 9279 // BCC (branch to SinkBB if V0 >= 0) 9280 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 9281 // SinkBB: V1 = PHI(V2, V3) 9282 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 9283 MachineFunction::iterator BBI = ++BB->getIterator(); 9284 MachineFunction *Fn = BB->getParent(); 9285 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 9286 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 9287 Fn->insert(BBI, RSBBB); 9288 Fn->insert(BBI, SinkBB); 9289 9290 unsigned int ABSSrcReg = MI.getOperand(1).getReg(); 9291 unsigned int ABSDstReg = MI.getOperand(0).getReg(); 9292 bool ABSSrcKIll = MI.getOperand(1).isKill(); 9293 bool isThumb2 = Subtarget->isThumb2(); 9294 MachineRegisterInfo &MRI = Fn->getRegInfo(); 9295 // In Thumb mode S must not be specified if source register is the SP or 9296 // PC and if destination register is the SP, so restrict register class 9297 unsigned NewRsbDstReg = 9298 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass); 9299 9300 // Transfer the remainder of BB and its successor edges to sinkMBB. 9301 SinkBB->splice(SinkBB->begin(), BB, 9302 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9303 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 9304 9305 BB->addSuccessor(RSBBB); 9306 BB->addSuccessor(SinkBB); 9307 9308 // fall through to SinkMBB 9309 RSBBB->addSuccessor(SinkBB); 9310 9311 // insert a cmp at the end of BB 9312 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 9313 .addReg(ABSSrcReg) 9314 .addImm(0) 9315 .add(predOps(ARMCC::AL)); 9316 9317 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 9318 BuildMI(BB, dl, 9319 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 9320 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 9321 9322 // insert rsbri in RSBBB 9323 // Note: BCC and rsbri will be converted into predicated rsbmi 9324 // by if-conversion pass 9325 BuildMI(*RSBBB, RSBBB->begin(), dl, 9326 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 9327 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0) 9328 .addImm(0) 9329 .add(predOps(ARMCC::AL)) 9330 .add(condCodeOp()); 9331 9332 // insert PHI in SinkBB, 9333 // reuse ABSDstReg to not change uses of ABS instruction 9334 BuildMI(*SinkBB, SinkBB->begin(), dl, 9335 TII->get(ARM::PHI), ABSDstReg) 9336 .addReg(NewRsbDstReg).addMBB(RSBBB) 9337 .addReg(ABSSrcReg).addMBB(BB); 9338 9339 // remove ABS instruction 9340 MI.eraseFromParent(); 9341 9342 // return last added BB 9343 return SinkBB; 9344 } 9345 case ARM::COPY_STRUCT_BYVAL_I32: 9346 ++NumLoopByVals; 9347 return EmitStructByval(MI, BB); 9348 case ARM::WIN__CHKSTK: 9349 return EmitLowered__chkstk(MI, BB); 9350 case ARM::WIN__DBZCHK: 9351 return EmitLowered__dbzchk(MI, BB); 9352 } 9353 } 9354 9355 /// \brief Attaches vregs to MEMCPY that it will use as scratch registers 9356 /// when it is expanded into LDM/STM. This is done as a post-isel lowering 9357 /// instead of as a custom inserter because we need the use list from the SDNode. 9358 static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget, 9359 MachineInstr &MI, const SDNode *Node) { 9360 bool isThumb1 = Subtarget->isThumb1Only(); 9361 9362 DebugLoc DL = MI.getDebugLoc(); 9363 MachineFunction *MF = MI.getParent()->getParent(); 9364 MachineRegisterInfo &MRI = MF->getRegInfo(); 9365 MachineInstrBuilder MIB(*MF, MI); 9366 9367 // If the new dst/src is unused mark it as dead. 9368 if (!Node->hasAnyUseOfValue(0)) { 9369 MI.getOperand(0).setIsDead(true); 9370 } 9371 if (!Node->hasAnyUseOfValue(1)) { 9372 MI.getOperand(1).setIsDead(true); 9373 } 9374 9375 // The MEMCPY both defines and kills the scratch registers. 9376 for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) { 9377 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass 9378 : &ARM::GPRRegClass); 9379 MIB.addReg(TmpReg, RegState::Define|RegState::Dead); 9380 } 9381 } 9382 9383 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 9384 SDNode *Node) const { 9385 if (MI.getOpcode() == ARM::MEMCPY) { 9386 attachMEMCPYScratchRegs(Subtarget, MI, Node); 9387 return; 9388 } 9389 9390 const MCInstrDesc *MCID = &MI.getDesc(); 9391 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 9392 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 9393 // operand is still set to noreg. If needed, set the optional operand's 9394 // register to CPSR, and remove the redundant implicit def. 9395 // 9396 // e.g. ADCS (..., implicit-def CPSR) -> ADC (... opt:def CPSR). 9397 9398 // Rename pseudo opcodes. 9399 unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode()); 9400 unsigned ccOutIdx; 9401 if (NewOpc) { 9402 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo(); 9403 MCID = &TII->get(NewOpc); 9404 9405 assert(MCID->getNumOperands() == 9406 MI.getDesc().getNumOperands() + 5 - MI.getDesc().getSize() 9407 && "converted opcode should be the same except for cc_out" 9408 " (and, on Thumb1, pred)"); 9409 9410 MI.setDesc(*MCID); 9411 9412 // Add the optional cc_out operand 9413 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 9414 9415 // On Thumb1, move all input operands to the end, then add the predicate 9416 if (Subtarget->isThumb1Only()) { 9417 for (unsigned c = MCID->getNumOperands() - 4; c--;) { 9418 MI.addOperand(MI.getOperand(1)); 9419 MI.RemoveOperand(1); 9420 } 9421 9422 // Restore the ties 9423 for (unsigned i = MI.getNumOperands(); i--;) { 9424 const MachineOperand& op = MI.getOperand(i); 9425 if (op.isReg() && op.isUse()) { 9426 int DefIdx = MCID->getOperandConstraint(i, MCOI::TIED_TO); 9427 if (DefIdx != -1) 9428 MI.tieOperands(DefIdx, i); 9429 } 9430 } 9431 9432 MI.addOperand(MachineOperand::CreateImm(ARMCC::AL)); 9433 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/false)); 9434 ccOutIdx = 1; 9435 } else 9436 ccOutIdx = MCID->getNumOperands() - 1; 9437 } else 9438 ccOutIdx = MCID->getNumOperands() - 1; 9439 9440 // Any ARM instruction that sets the 's' bit should specify an optional 9441 // "cc_out" operand in the last operand position. 9442 if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 9443 assert(!NewOpc && "Optional cc_out operand required"); 9444 return; 9445 } 9446 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 9447 // since we already have an optional CPSR def. 9448 bool definesCPSR = false; 9449 bool deadCPSR = false; 9450 for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e; 9451 ++i) { 9452 const MachineOperand &MO = MI.getOperand(i); 9453 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 9454 definesCPSR = true; 9455 if (MO.isDead()) 9456 deadCPSR = true; 9457 MI.RemoveOperand(i); 9458 break; 9459 } 9460 } 9461 if (!definesCPSR) { 9462 assert(!NewOpc && "Optional cc_out operand required"); 9463 return; 9464 } 9465 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 9466 if (deadCPSR) { 9467 assert(!MI.getOperand(ccOutIdx).getReg() && 9468 "expect uninitialized optional cc_out operand"); 9469 // Thumb1 instructions must have the S bit even if the CPSR is dead. 9470 if (!Subtarget->isThumb1Only()) 9471 return; 9472 } 9473 9474 // If this instruction was defined with an optional CPSR def and its dag node 9475 // had a live implicit CPSR def, then activate the optional CPSR def. 9476 MachineOperand &MO = MI.getOperand(ccOutIdx); 9477 MO.setReg(ARM::CPSR); 9478 MO.setIsDef(true); 9479 } 9480 9481 //===----------------------------------------------------------------------===// 9482 // ARM Optimization Hooks 9483 //===----------------------------------------------------------------------===// 9484 9485 // Helper function that checks if N is a null or all ones constant. 9486 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 9487 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N); 9488 } 9489 9490 // Return true if N is conditionally 0 or all ones. 9491 // Detects these expressions where cc is an i1 value: 9492 // 9493 // (select cc 0, y) [AllOnes=0] 9494 // (select cc y, 0) [AllOnes=0] 9495 // (zext cc) [AllOnes=0] 9496 // (sext cc) [AllOnes=0/1] 9497 // (select cc -1, y) [AllOnes=1] 9498 // (select cc y, -1) [AllOnes=1] 9499 // 9500 // Invert is set when N is the null/all ones constant when CC is false. 9501 // OtherOp is set to the alternative value of N. 9502 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 9503 SDValue &CC, bool &Invert, 9504 SDValue &OtherOp, 9505 SelectionDAG &DAG) { 9506 switch (N->getOpcode()) { 9507 default: return false; 9508 case ISD::SELECT: { 9509 CC = N->getOperand(0); 9510 SDValue N1 = N->getOperand(1); 9511 SDValue N2 = N->getOperand(2); 9512 if (isZeroOrAllOnes(N1, AllOnes)) { 9513 Invert = false; 9514 OtherOp = N2; 9515 return true; 9516 } 9517 if (isZeroOrAllOnes(N2, AllOnes)) { 9518 Invert = true; 9519 OtherOp = N1; 9520 return true; 9521 } 9522 return false; 9523 } 9524 case ISD::ZERO_EXTEND: 9525 // (zext cc) can never be the all ones value. 9526 if (AllOnes) 9527 return false; 9528 LLVM_FALLTHROUGH; 9529 case ISD::SIGN_EXTEND: { 9530 SDLoc dl(N); 9531 EVT VT = N->getValueType(0); 9532 CC = N->getOperand(0); 9533 if (CC.getValueType() != MVT::i1 || CC.getOpcode() != ISD::SETCC) 9534 return false; 9535 Invert = !AllOnes; 9536 if (AllOnes) 9537 // When looking for an AllOnes constant, N is an sext, and the 'other' 9538 // value is 0. 9539 OtherOp = DAG.getConstant(0, dl, VT); 9540 else if (N->getOpcode() == ISD::ZERO_EXTEND) 9541 // When looking for a 0 constant, N can be zext or sext. 9542 OtherOp = DAG.getConstant(1, dl, VT); 9543 else 9544 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 9545 VT); 9546 return true; 9547 } 9548 } 9549 } 9550 9551 // Combine a constant select operand into its use: 9552 // 9553 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 9554 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 9555 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 9556 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 9557 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 9558 // 9559 // The transform is rejected if the select doesn't have a constant operand that 9560 // is null, or all ones when AllOnes is set. 9561 // 9562 // Also recognize sext/zext from i1: 9563 // 9564 // (add (zext cc), x) -> (select cc (add x, 1), x) 9565 // (add (sext cc), x) -> (select cc (add x, -1), x) 9566 // 9567 // These transformations eventually create predicated instructions. 9568 // 9569 // @param N The node to transform. 9570 // @param Slct The N operand that is a select. 9571 // @param OtherOp The other N operand (x above). 9572 // @param DCI Context. 9573 // @param AllOnes Require the select constant to be all ones instead of null. 9574 // @returns The new node, or SDValue() on failure. 9575 static 9576 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 9577 TargetLowering::DAGCombinerInfo &DCI, 9578 bool AllOnes = false) { 9579 SelectionDAG &DAG = DCI.DAG; 9580 EVT VT = N->getValueType(0); 9581 SDValue NonConstantVal; 9582 SDValue CCOp; 9583 bool SwapSelectOps; 9584 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 9585 NonConstantVal, DAG)) 9586 return SDValue(); 9587 9588 // Slct is now know to be the desired identity constant when CC is true. 9589 SDValue TrueVal = OtherOp; 9590 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 9591 OtherOp, NonConstantVal); 9592 // Unless SwapSelectOps says CC should be false. 9593 if (SwapSelectOps) 9594 std::swap(TrueVal, FalseVal); 9595 9596 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 9597 CCOp, TrueVal, FalseVal); 9598 } 9599 9600 // Attempt combineSelectAndUse on each operand of a commutative operator N. 9601 static 9602 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 9603 TargetLowering::DAGCombinerInfo &DCI) { 9604 SDValue N0 = N->getOperand(0); 9605 SDValue N1 = N->getOperand(1); 9606 if (N0.getNode()->hasOneUse()) 9607 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes)) 9608 return Result; 9609 if (N1.getNode()->hasOneUse()) 9610 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes)) 9611 return Result; 9612 return SDValue(); 9613 } 9614 9615 static bool IsVUZPShuffleNode(SDNode *N) { 9616 // VUZP shuffle node. 9617 if (N->getOpcode() == ARMISD::VUZP) 9618 return true; 9619 9620 // "VUZP" on i32 is an alias for VTRN. 9621 if (N->getOpcode() == ARMISD::VTRN && N->getValueType(0) == MVT::v2i32) 9622 return true; 9623 9624 return false; 9625 } 9626 9627 static SDValue AddCombineToVPADD(SDNode *N, SDValue N0, SDValue N1, 9628 TargetLowering::DAGCombinerInfo &DCI, 9629 const ARMSubtarget *Subtarget) { 9630 // Look for ADD(VUZP.0, VUZP.1). 9631 if (!IsVUZPShuffleNode(N0.getNode()) || N0.getNode() != N1.getNode() || 9632 N0 == N1) 9633 return SDValue(); 9634 9635 // Make sure the ADD is a 64-bit add; there is no 128-bit VPADD. 9636 if (!N->getValueType(0).is64BitVector()) 9637 return SDValue(); 9638 9639 // Generate vpadd. 9640 SelectionDAG &DAG = DCI.DAG; 9641 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9642 SDLoc dl(N); 9643 SDNode *Unzip = N0.getNode(); 9644 EVT VT = N->getValueType(0); 9645 9646 SmallVector<SDValue, 8> Ops; 9647 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpadd, dl, 9648 TLI.getPointerTy(DAG.getDataLayout()))); 9649 Ops.push_back(Unzip->getOperand(0)); 9650 Ops.push_back(Unzip->getOperand(1)); 9651 9652 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops); 9653 } 9654 9655 static SDValue AddCombineVUZPToVPADDL(SDNode *N, SDValue N0, SDValue N1, 9656 TargetLowering::DAGCombinerInfo &DCI, 9657 const ARMSubtarget *Subtarget) { 9658 // Check for two extended operands. 9659 if (!(N0.getOpcode() == ISD::SIGN_EXTEND && 9660 N1.getOpcode() == ISD::SIGN_EXTEND) && 9661 !(N0.getOpcode() == ISD::ZERO_EXTEND && 9662 N1.getOpcode() == ISD::ZERO_EXTEND)) 9663 return SDValue(); 9664 9665 SDValue N00 = N0.getOperand(0); 9666 SDValue N10 = N1.getOperand(0); 9667 9668 // Look for ADD(SEXT(VUZP.0), SEXT(VUZP.1)) 9669 if (!IsVUZPShuffleNode(N00.getNode()) || N00.getNode() != N10.getNode() || 9670 N00 == N10) 9671 return SDValue(); 9672 9673 // We only recognize Q register paddl here; this can't be reached until 9674 // after type legalization. 9675 if (!N00.getValueType().is64BitVector() || 9676 !N0.getValueType().is128BitVector()) 9677 return SDValue(); 9678 9679 // Generate vpaddl. 9680 SelectionDAG &DAG = DCI.DAG; 9681 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9682 SDLoc dl(N); 9683 EVT VT = N->getValueType(0); 9684 9685 SmallVector<SDValue, 8> Ops; 9686 // Form vpaddl.sN or vpaddl.uN depending on the kind of extension. 9687 unsigned Opcode; 9688 if (N0.getOpcode() == ISD::SIGN_EXTEND) 9689 Opcode = Intrinsic::arm_neon_vpaddls; 9690 else 9691 Opcode = Intrinsic::arm_neon_vpaddlu; 9692 Ops.push_back(DAG.getConstant(Opcode, dl, 9693 TLI.getPointerTy(DAG.getDataLayout()))); 9694 EVT ElemTy = N00.getValueType().getVectorElementType(); 9695 unsigned NumElts = VT.getVectorNumElements(); 9696 EVT ConcatVT = EVT::getVectorVT(*DAG.getContext(), ElemTy, NumElts * 2); 9697 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), ConcatVT, 9698 N00.getOperand(0), N00.getOperand(1)); 9699 Ops.push_back(Concat); 9700 9701 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops); 9702 } 9703 9704 // FIXME: This function shouldn't be necessary; if we lower BUILD_VECTOR in 9705 // an appropriate manner, we end up with ADD(VUZP(ZEXT(N))), which is 9706 // much easier to match. 9707 static SDValue 9708 AddCombineBUILD_VECTORToVPADDL(SDNode *N, SDValue N0, SDValue N1, 9709 TargetLowering::DAGCombinerInfo &DCI, 9710 const ARMSubtarget *Subtarget) { 9711 // Only perform optimization if after legalize, and if NEON is available. We 9712 // also expected both operands to be BUILD_VECTORs. 9713 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 9714 || N0.getOpcode() != ISD::BUILD_VECTOR 9715 || N1.getOpcode() != ISD::BUILD_VECTOR) 9716 return SDValue(); 9717 9718 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 9719 EVT VT = N->getValueType(0); 9720 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 9721 return SDValue(); 9722 9723 // Check that the vector operands are of the right form. 9724 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 9725 // operands, where N is the size of the formed vector. 9726 // Each EXTRACT_VECTOR should have the same input vector and odd or even 9727 // index such that we have a pair wise add pattern. 9728 9729 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 9730 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 9731 return SDValue(); 9732 SDValue Vec = N0->getOperand(0)->getOperand(0); 9733 SDNode *V = Vec.getNode(); 9734 unsigned nextIndex = 0; 9735 9736 // For each operands to the ADD which are BUILD_VECTORs, 9737 // check to see if each of their operands are an EXTRACT_VECTOR with 9738 // the same vector and appropriate index. 9739 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 9740 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 9741 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 9742 9743 SDValue ExtVec0 = N0->getOperand(i); 9744 SDValue ExtVec1 = N1->getOperand(i); 9745 9746 // First operand is the vector, verify its the same. 9747 if (V != ExtVec0->getOperand(0).getNode() || 9748 V != ExtVec1->getOperand(0).getNode()) 9749 return SDValue(); 9750 9751 // Second is the constant, verify its correct. 9752 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 9753 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 9754 9755 // For the constant, we want to see all the even or all the odd. 9756 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 9757 || C1->getZExtValue() != nextIndex+1) 9758 return SDValue(); 9759 9760 // Increment index. 9761 nextIndex+=2; 9762 } else 9763 return SDValue(); 9764 } 9765 9766 // Don't generate vpaddl+vmovn; we'll match it to vpadd later. Also make sure 9767 // we're using the entire input vector, otherwise there's a size/legality 9768 // mismatch somewhere. 9769 if (nextIndex != Vec.getValueType().getVectorNumElements() || 9770 Vec.getValueType().getVectorElementType() == VT.getVectorElementType()) 9771 return SDValue(); 9772 9773 // Create VPADDL node. 9774 SelectionDAG &DAG = DCI.DAG; 9775 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9776 9777 SDLoc dl(N); 9778 9779 // Build operand list. 9780 SmallVector<SDValue, 8> Ops; 9781 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl, 9782 TLI.getPointerTy(DAG.getDataLayout()))); 9783 9784 // Input is the vector. 9785 Ops.push_back(Vec); 9786 9787 // Get widened type and narrowed type. 9788 MVT widenType; 9789 unsigned numElem = VT.getVectorNumElements(); 9790 9791 EVT inputLaneType = Vec.getValueType().getVectorElementType(); 9792 switch (inputLaneType.getSimpleVT().SimpleTy) { 9793 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 9794 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 9795 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 9796 default: 9797 llvm_unreachable("Invalid vector element type for padd optimization."); 9798 } 9799 9800 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops); 9801 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE; 9802 return DAG.getNode(ExtOp, dl, VT, tmp); 9803 } 9804 9805 static SDValue findMUL_LOHI(SDValue V) { 9806 if (V->getOpcode() == ISD::UMUL_LOHI || 9807 V->getOpcode() == ISD::SMUL_LOHI) 9808 return V; 9809 return SDValue(); 9810 } 9811 9812 static SDValue AddCombineTo64BitSMLAL16(SDNode *AddcNode, SDNode *AddeNode, 9813 TargetLowering::DAGCombinerInfo &DCI, 9814 const ARMSubtarget *Subtarget) { 9815 if (Subtarget->isThumb()) { 9816 if (!Subtarget->hasDSP()) 9817 return SDValue(); 9818 } else if (!Subtarget->hasV5TEOps()) 9819 return SDValue(); 9820 9821 // SMLALBB, SMLALBT, SMLALTB, SMLALTT multiply two 16-bit values and 9822 // accumulates the product into a 64-bit value. The 16-bit values will 9823 // be sign extended somehow or SRA'd into 32-bit values 9824 // (addc (adde (mul 16bit, 16bit), lo), hi) 9825 SDValue Mul = AddcNode->getOperand(0); 9826 SDValue Lo = AddcNode->getOperand(1); 9827 if (Mul.getOpcode() != ISD::MUL) { 9828 Lo = AddcNode->getOperand(0); 9829 Mul = AddcNode->getOperand(1); 9830 if (Mul.getOpcode() != ISD::MUL) 9831 return SDValue(); 9832 } 9833 9834 SDValue SRA = AddeNode->getOperand(0); 9835 SDValue Hi = AddeNode->getOperand(1); 9836 if (SRA.getOpcode() != ISD::SRA) { 9837 SRA = AddeNode->getOperand(1); 9838 Hi = AddeNode->getOperand(0); 9839 if (SRA.getOpcode() != ISD::SRA) 9840 return SDValue(); 9841 } 9842 if (auto Const = dyn_cast<ConstantSDNode>(SRA.getOperand(1))) { 9843 if (Const->getZExtValue() != 31) 9844 return SDValue(); 9845 } else 9846 return SDValue(); 9847 9848 if (SRA.getOperand(0) != Mul) 9849 return SDValue(); 9850 9851 SelectionDAG &DAG = DCI.DAG; 9852 SDLoc dl(AddcNode); 9853 unsigned Opcode = 0; 9854 SDValue Op0; 9855 SDValue Op1; 9856 9857 if (isS16(Mul.getOperand(0), DAG) && isS16(Mul.getOperand(1), DAG)) { 9858 Opcode = ARMISD::SMLALBB; 9859 Op0 = Mul.getOperand(0); 9860 Op1 = Mul.getOperand(1); 9861 } else if (isS16(Mul.getOperand(0), DAG) && isSRA16(Mul.getOperand(1))) { 9862 Opcode = ARMISD::SMLALBT; 9863 Op0 = Mul.getOperand(0); 9864 Op1 = Mul.getOperand(1).getOperand(0); 9865 } else if (isSRA16(Mul.getOperand(0)) && isS16(Mul.getOperand(1), DAG)) { 9866 Opcode = ARMISD::SMLALTB; 9867 Op0 = Mul.getOperand(0).getOperand(0); 9868 Op1 = Mul.getOperand(1); 9869 } else if (isSRA16(Mul.getOperand(0)) && isSRA16(Mul.getOperand(1))) { 9870 Opcode = ARMISD::SMLALTT; 9871 Op0 = Mul->getOperand(0).getOperand(0); 9872 Op1 = Mul->getOperand(1).getOperand(0); 9873 } 9874 9875 if (!Op0 || !Op1) 9876 return SDValue(); 9877 9878 SDValue SMLAL = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32), 9879 Op0, Op1, Lo, Hi); 9880 // Replace the ADDs' nodes uses by the MLA node's values. 9881 SDValue HiMLALResult(SMLAL.getNode(), 1); 9882 SDValue LoMLALResult(SMLAL.getNode(), 0); 9883 9884 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 9885 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 9886 9887 // Return original node to notify the driver to stop replacing. 9888 SDValue resNode(AddcNode, 0); 9889 return resNode; 9890 } 9891 9892 static SDValue AddCombineTo64bitMLAL(SDNode *AddeSubeNode, 9893 TargetLowering::DAGCombinerInfo &DCI, 9894 const ARMSubtarget *Subtarget) { 9895 // Look for multiply add opportunities. 9896 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 9897 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 9898 // a glue link from the first add to the second add. 9899 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 9900 // a S/UMLAL instruction. 9901 // UMUL_LOHI 9902 // / :lo \ :hi 9903 // V \ [no multiline comment] 9904 // loAdd -> ADDC | 9905 // \ :carry / 9906 // V V 9907 // ADDE <- hiAdd 9908 // 9909 // In the special case where only the higher part of a signed result is used 9910 // and the add to the low part of the result of ISD::UMUL_LOHI adds or subtracts 9911 // a constant with the exact value of 0x80000000, we recognize we are dealing 9912 // with a "rounded multiply and add" (or subtract) and transform it into 9913 // either a ARMISD::SMMLAR or ARMISD::SMMLSR respectively. 9914 9915 assert((AddeSubeNode->getOpcode() == ARMISD::ADDE || 9916 AddeSubeNode->getOpcode() == ARMISD::SUBE) && 9917 "Expect an ADDE or SUBE"); 9918 9919 assert(AddeSubeNode->getNumOperands() == 3 && 9920 AddeSubeNode->getOperand(2).getValueType() == MVT::i32 && 9921 "ADDE node has the wrong inputs"); 9922 9923 // Check that we are chained to the right ADDC or SUBC node. 9924 SDNode *AddcSubcNode = AddeSubeNode->getOperand(2).getNode(); 9925 if ((AddeSubeNode->getOpcode() == ARMISD::ADDE && 9926 AddcSubcNode->getOpcode() != ARMISD::ADDC) || 9927 (AddeSubeNode->getOpcode() == ARMISD::SUBE && 9928 AddcSubcNode->getOpcode() != ARMISD::SUBC)) 9929 return SDValue(); 9930 9931 SDValue AddcSubcOp0 = AddcSubcNode->getOperand(0); 9932 SDValue AddcSubcOp1 = AddcSubcNode->getOperand(1); 9933 9934 // Check if the two operands are from the same mul_lohi node. 9935 if (AddcSubcOp0.getNode() == AddcSubcOp1.getNode()) 9936 return SDValue(); 9937 9938 assert(AddcSubcNode->getNumValues() == 2 && 9939 AddcSubcNode->getValueType(0) == MVT::i32 && 9940 "Expect ADDC with two result values. First: i32"); 9941 9942 // Check that the ADDC adds the low result of the S/UMUL_LOHI. If not, it 9943 // maybe a SMLAL which multiplies two 16-bit values. 9944 if (AddeSubeNode->getOpcode() == ARMISD::ADDE && 9945 AddcSubcOp0->getOpcode() != ISD::UMUL_LOHI && 9946 AddcSubcOp0->getOpcode() != ISD::SMUL_LOHI && 9947 AddcSubcOp1->getOpcode() != ISD::UMUL_LOHI && 9948 AddcSubcOp1->getOpcode() != ISD::SMUL_LOHI) 9949 return AddCombineTo64BitSMLAL16(AddcSubcNode, AddeSubeNode, DCI, Subtarget); 9950 9951 // Check for the triangle shape. 9952 SDValue AddeSubeOp0 = AddeSubeNode->getOperand(0); 9953 SDValue AddeSubeOp1 = AddeSubeNode->getOperand(1); 9954 9955 // Make sure that the ADDE/SUBE operands are not coming from the same node. 9956 if (AddeSubeOp0.getNode() == AddeSubeOp1.getNode()) 9957 return SDValue(); 9958 9959 // Find the MUL_LOHI node walking up ADDE/SUBE's operands. 9960 bool IsLeftOperandMUL = false; 9961 SDValue MULOp = findMUL_LOHI(AddeSubeOp0); 9962 if (MULOp == SDValue()) 9963 MULOp = findMUL_LOHI(AddeSubeOp1); 9964 else 9965 IsLeftOperandMUL = true; 9966 if (MULOp == SDValue()) 9967 return SDValue(); 9968 9969 // Figure out the right opcode. 9970 unsigned Opc = MULOp->getOpcode(); 9971 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 9972 9973 // Figure out the high and low input values to the MLAL node. 9974 SDValue *HiAddSub = nullptr; 9975 SDValue *LoMul = nullptr; 9976 SDValue *LowAddSub = nullptr; 9977 9978 // Ensure that ADDE/SUBE is from high result of ISD::xMUL_LOHI. 9979 if ((AddeSubeOp0 != MULOp.getValue(1)) && (AddeSubeOp1 != MULOp.getValue(1))) 9980 return SDValue(); 9981 9982 if (IsLeftOperandMUL) 9983 HiAddSub = &AddeSubeOp1; 9984 else 9985 HiAddSub = &AddeSubeOp0; 9986 9987 // Ensure that LoMul and LowAddSub are taken from correct ISD::SMUL_LOHI node 9988 // whose low result is fed to the ADDC/SUBC we are checking. 9989 9990 if (AddcSubcOp0 == MULOp.getValue(0)) { 9991 LoMul = &AddcSubcOp0; 9992 LowAddSub = &AddcSubcOp1; 9993 } 9994 if (AddcSubcOp1 == MULOp.getValue(0)) { 9995 LoMul = &AddcSubcOp1; 9996 LowAddSub = &AddcSubcOp0; 9997 } 9998 9999 if (!LoMul) 10000 return SDValue(); 10001 10002 // If HiAddSub is the same node as ADDC/SUBC or is a predecessor of ADDC/SUBC 10003 // the replacement below will create a cycle. 10004 if (AddcSubcNode == HiAddSub->getNode() || 10005 AddcSubcNode->isPredecessorOf(HiAddSub->getNode())) 10006 return SDValue(); 10007 10008 // Create the merged node. 10009 SelectionDAG &DAG = DCI.DAG; 10010 10011 // Start building operand list. 10012 SmallVector<SDValue, 8> Ops; 10013 Ops.push_back(LoMul->getOperand(0)); 10014 Ops.push_back(LoMul->getOperand(1)); 10015 10016 // Check whether we can use SMMLAR, SMMLSR or SMMULR instead. For this to be 10017 // the case, we must be doing signed multiplication and only use the higher 10018 // part of the result of the MLAL, furthermore the LowAddSub must be a constant 10019 // addition or subtraction with the value of 0x800000. 10020 if (Subtarget->hasV6Ops() && Subtarget->hasDSP() && Subtarget->useMulOps() && 10021 FinalOpc == ARMISD::SMLAL && !AddeSubeNode->hasAnyUseOfValue(1) && 10022 LowAddSub->getNode()->getOpcode() == ISD::Constant && 10023 static_cast<ConstantSDNode *>(LowAddSub->getNode())->getZExtValue() == 10024 0x80000000) { 10025 Ops.push_back(*HiAddSub); 10026 if (AddcSubcNode->getOpcode() == ARMISD::SUBC) { 10027 FinalOpc = ARMISD::SMMLSR; 10028 } else { 10029 FinalOpc = ARMISD::SMMLAR; 10030 } 10031 SDValue NewNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), MVT::i32, Ops); 10032 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), NewNode); 10033 10034 return SDValue(AddeSubeNode, 0); 10035 } else if (AddcSubcNode->getOpcode() == ARMISD::SUBC) 10036 // SMMLS is generated during instruction selection and the rest of this 10037 // function can not handle the case where AddcSubcNode is a SUBC. 10038 return SDValue(); 10039 10040 // Finish building the operand list for {U/S}MLAL 10041 Ops.push_back(*LowAddSub); 10042 Ops.push_back(*HiAddSub); 10043 10044 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), 10045 DAG.getVTList(MVT::i32, MVT::i32), Ops); 10046 10047 // Replace the ADDs' nodes uses by the MLA node's values. 10048 SDValue HiMLALResult(MLALNode.getNode(), 1); 10049 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), HiMLALResult); 10050 10051 SDValue LoMLALResult(MLALNode.getNode(), 0); 10052 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcSubcNode, 0), LoMLALResult); 10053 10054 // Return original node to notify the driver to stop replacing. 10055 return SDValue(AddeSubeNode, 0); 10056 } 10057 10058 static SDValue AddCombineTo64bitUMAAL(SDNode *AddeNode, 10059 TargetLowering::DAGCombinerInfo &DCI, 10060 const ARMSubtarget *Subtarget) { 10061 // UMAAL is similar to UMLAL except that it adds two unsigned values. 10062 // While trying to combine for the other MLAL nodes, first search for the 10063 // chance to use UMAAL. Check if Addc uses a node which has already 10064 // been combined into a UMLAL. The other pattern is UMLAL using Addc/Adde 10065 // as the addend, and it's handled in PerformUMLALCombine. 10066 10067 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP()) 10068 return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget); 10069 10070 // Check that we have a glued ADDC node. 10071 SDNode* AddcNode = AddeNode->getOperand(2).getNode(); 10072 if (AddcNode->getOpcode() != ARMISD::ADDC) 10073 return SDValue(); 10074 10075 // Find the converted UMAAL or quit if it doesn't exist. 10076 SDNode *UmlalNode = nullptr; 10077 SDValue AddHi; 10078 if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) { 10079 UmlalNode = AddcNode->getOperand(0).getNode(); 10080 AddHi = AddcNode->getOperand(1); 10081 } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) { 10082 UmlalNode = AddcNode->getOperand(1).getNode(); 10083 AddHi = AddcNode->getOperand(0); 10084 } else { 10085 return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget); 10086 } 10087 10088 // The ADDC should be glued to an ADDE node, which uses the same UMLAL as 10089 // the ADDC as well as Zero. 10090 if (!isNullConstant(UmlalNode->getOperand(3))) 10091 return SDValue(); 10092 10093 if ((isNullConstant(AddeNode->getOperand(0)) && 10094 AddeNode->getOperand(1).getNode() == UmlalNode) || 10095 (AddeNode->getOperand(0).getNode() == UmlalNode && 10096 isNullConstant(AddeNode->getOperand(1)))) { 10097 SelectionDAG &DAG = DCI.DAG; 10098 SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1), 10099 UmlalNode->getOperand(2), AddHi }; 10100 SDValue UMAAL = DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode), 10101 DAG.getVTList(MVT::i32, MVT::i32), Ops); 10102 10103 // Replace the ADDs' nodes uses by the UMAAL node's values. 10104 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1)); 10105 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0)); 10106 10107 // Return original node to notify the driver to stop replacing. 10108 return SDValue(AddeNode, 0); 10109 } 10110 return SDValue(); 10111 } 10112 10113 static SDValue PerformUMLALCombine(SDNode *N, SelectionDAG &DAG, 10114 const ARMSubtarget *Subtarget) { 10115 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP()) 10116 return SDValue(); 10117 10118 // Check that we have a pair of ADDC and ADDE as operands. 10119 // Both addends of the ADDE must be zero. 10120 SDNode* AddcNode = N->getOperand(2).getNode(); 10121 SDNode* AddeNode = N->getOperand(3).getNode(); 10122 if ((AddcNode->getOpcode() == ARMISD::ADDC) && 10123 (AddeNode->getOpcode() == ARMISD::ADDE) && 10124 isNullConstant(AddeNode->getOperand(0)) && 10125 isNullConstant(AddeNode->getOperand(1)) && 10126 (AddeNode->getOperand(2).getNode() == AddcNode)) 10127 return DAG.getNode(ARMISD::UMAAL, SDLoc(N), 10128 DAG.getVTList(MVT::i32, MVT::i32), 10129 {N->getOperand(0), N->getOperand(1), 10130 AddcNode->getOperand(0), AddcNode->getOperand(1)}); 10131 else 10132 return SDValue(); 10133 } 10134 10135 static SDValue PerformAddcSubcCombine(SDNode *N, 10136 TargetLowering::DAGCombinerInfo &DCI, 10137 const ARMSubtarget *Subtarget) { 10138 SelectionDAG &DAG(DCI.DAG); 10139 10140 if (N->getOpcode() == ARMISD::ADDC) { 10141 // (ADDC (ADDE 0, 0, C), -1) -> C 10142 SDValue LHS = N->getOperand(0); 10143 SDValue RHS = N->getOperand(1); 10144 if (LHS->getOpcode() == ARMISD::ADDE && 10145 isNullConstant(LHS->getOperand(0)) && 10146 isNullConstant(LHS->getOperand(1)) && isAllOnesConstant(RHS)) { 10147 return DCI.CombineTo(N, SDValue(N, 0), LHS->getOperand(2)); 10148 } 10149 } 10150 10151 if (Subtarget->isThumb1Only()) { 10152 SDValue RHS = N->getOperand(1); 10153 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 10154 int32_t imm = C->getSExtValue(); 10155 if (imm < 0 && imm > std::numeric_limits<int>::min()) { 10156 SDLoc DL(N); 10157 RHS = DAG.getConstant(-imm, DL, MVT::i32); 10158 unsigned Opcode = (N->getOpcode() == ARMISD::ADDC) ? ARMISD::SUBC 10159 : ARMISD::ADDC; 10160 return DAG.getNode(Opcode, DL, N->getVTList(), N->getOperand(0), RHS); 10161 } 10162 } 10163 } 10164 return SDValue(); 10165 } 10166 10167 static SDValue PerformAddeSubeCombine(SDNode *N, 10168 TargetLowering::DAGCombinerInfo &DCI, 10169 const ARMSubtarget *Subtarget) { 10170 if (Subtarget->isThumb1Only()) { 10171 SelectionDAG &DAG = DCI.DAG; 10172 SDValue RHS = N->getOperand(1); 10173 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 10174 int64_t imm = C->getSExtValue(); 10175 if (imm < 0) { 10176 SDLoc DL(N); 10177 10178 // The with-carry-in form matches bitwise not instead of the negation. 10179 // Effectively, the inverse interpretation of the carry flag already 10180 // accounts for part of the negation. 10181 RHS = DAG.getConstant(~imm, DL, MVT::i32); 10182 10183 unsigned Opcode = (N->getOpcode() == ARMISD::ADDE) ? ARMISD::SUBE 10184 : ARMISD::ADDE; 10185 return DAG.getNode(Opcode, DL, N->getVTList(), 10186 N->getOperand(0), RHS, N->getOperand(2)); 10187 } 10188 } 10189 } else if (N->getOperand(1)->getOpcode() == ISD::SMUL_LOHI) { 10190 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 10191 } 10192 return SDValue(); 10193 } 10194 10195 /// PerformADDECombine - Target-specific dag combine transform from 10196 /// ARMISD::ADDC, ARMISD::ADDE, and ISD::MUL_LOHI to MLAL or 10197 /// ARMISD::ADDC, ARMISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL 10198 static SDValue PerformADDECombine(SDNode *N, 10199 TargetLowering::DAGCombinerInfo &DCI, 10200 const ARMSubtarget *Subtarget) { 10201 // Only ARM and Thumb2 support UMLAL/SMLAL. 10202 if (Subtarget->isThumb1Only()) 10203 return PerformAddeSubeCombine(N, DCI, Subtarget); 10204 10205 // Only perform the checks after legalize when the pattern is available. 10206 if (DCI.isBeforeLegalize()) return SDValue(); 10207 10208 return AddCombineTo64bitUMAAL(N, DCI, Subtarget); 10209 } 10210 10211 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 10212 /// operands N0 and N1. This is a helper for PerformADDCombine that is 10213 /// called with the default operands, and if that fails, with commuted 10214 /// operands. 10215 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 10216 TargetLowering::DAGCombinerInfo &DCI, 10217 const ARMSubtarget *Subtarget){ 10218 // Attempt to create vpadd for this add. 10219 if (SDValue Result = AddCombineToVPADD(N, N0, N1, DCI, Subtarget)) 10220 return Result; 10221 10222 // Attempt to create vpaddl for this add. 10223 if (SDValue Result = AddCombineVUZPToVPADDL(N, N0, N1, DCI, Subtarget)) 10224 return Result; 10225 if (SDValue Result = AddCombineBUILD_VECTORToVPADDL(N, N0, N1, DCI, 10226 Subtarget)) 10227 return Result; 10228 10229 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 10230 if (N0.getNode()->hasOneUse()) 10231 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI)) 10232 return Result; 10233 return SDValue(); 10234 } 10235 10236 static SDValue PerformSHLSimplify(SDNode *N, 10237 TargetLowering::DAGCombinerInfo &DCI, 10238 const ARMSubtarget *ST) { 10239 // Allow the generic combiner to identify potential bswaps. 10240 if (DCI.isBeforeLegalize()) 10241 return SDValue(); 10242 10243 // DAG combiner will fold: 10244 // (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2) 10245 // (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2 10246 // Other code patterns that can be also be modified have the following form: 10247 // b + ((a << 1) | 510) 10248 // b + ((a << 1) & 510) 10249 // b + ((a << 1) ^ 510) 10250 // b + ((a << 1) + 510) 10251 10252 // Many instructions can perform the shift for free, but it requires both 10253 // the operands to be registers. If c1 << c2 is too large, a mov immediate 10254 // instruction will needed. So, unfold back to the original pattern if: 10255 // - if c1 and c2 are small enough that they don't require mov imms. 10256 // - the user(s) of the node can perform an shl 10257 10258 // No shifted operands for 16-bit instructions. 10259 if (ST->isThumb() && ST->isThumb1Only()) 10260 return SDValue(); 10261 10262 // Check that all the users could perform the shl themselves. 10263 for (auto U : N->uses()) { 10264 switch(U->getOpcode()) { 10265 default: 10266 return SDValue(); 10267 case ISD::SUB: 10268 case ISD::ADD: 10269 case ISD::AND: 10270 case ISD::OR: 10271 case ISD::XOR: 10272 case ISD::SETCC: 10273 case ARMISD::CMP: 10274 // Check that its not already using a shl. 10275 if (U->getOperand(0).getOpcode() == ISD::SHL || 10276 U->getOperand(1).getOpcode() == ISD::SHL) 10277 return SDValue(); 10278 break; 10279 } 10280 } 10281 10282 if (N->getOpcode() != ISD::ADD && N->getOpcode() != ISD::OR && 10283 N->getOpcode() != ISD::XOR && N->getOpcode() != ISD::AND) 10284 return SDValue(); 10285 10286 if (N->getOperand(0).getOpcode() != ISD::SHL) 10287 return SDValue(); 10288 10289 SDValue SHL = N->getOperand(0); 10290 10291 auto *C1ShlC2 = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10292 auto *C2 = dyn_cast<ConstantSDNode>(SHL.getOperand(1)); 10293 if (!C1ShlC2 || !C2) 10294 return SDValue(); 10295 10296 DEBUG(dbgs() << "Trying to simplify shl: "; N->dump()); 10297 10298 APInt C2Int = C2->getAPIntValue(); 10299 APInt C1Int = C1ShlC2->getAPIntValue(); 10300 10301 // Check that performing a lshr will not lose any information. 10302 APInt Mask = APInt::getHighBitsSet(C2Int.getBitWidth(), 10303 C2Int.getBitWidth() - C2->getZExtValue()); 10304 if ((C1Int & Mask) != C1Int) 10305 return SDValue(); 10306 10307 // Shift the first constant. 10308 C1Int.lshrInPlace(C2Int); 10309 10310 // The immediates are encoded as an 8-bit value that can be rotated. 10311 unsigned Zeros = C1Int.countLeadingZeros() + C1Int.countTrailingZeros(); 10312 if (C1Int.getBitWidth() - Zeros > 8) 10313 return SDValue(); 10314 10315 Zeros = C2Int.countLeadingZeros() + C2Int.countTrailingZeros(); 10316 if (C2Int.getBitWidth() - Zeros > 8) 10317 return SDValue(); 10318 10319 SelectionDAG &DAG = DCI.DAG; 10320 SDLoc dl(N); 10321 SDValue X = SHL.getOperand(0); 10322 SDValue BinOp = DAG.getNode(N->getOpcode(), dl, MVT::i32, X, 10323 DAG.getConstant(C1Int, dl, MVT::i32)); 10324 // Shift left to compensate for the lshr of C1Int. 10325 SDValue Res = DAG.getNode(ISD::SHL, dl, MVT::i32, BinOp, SHL.getOperand(1)); 10326 10327 DAG.ReplaceAllUsesWith(SDValue(N, 0), Res); 10328 return SDValue(N, 0); 10329 } 10330 10331 10332 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 10333 /// 10334 static SDValue PerformADDCombine(SDNode *N, 10335 TargetLowering::DAGCombinerInfo &DCI, 10336 const ARMSubtarget *Subtarget) { 10337 SDValue N0 = N->getOperand(0); 10338 SDValue N1 = N->getOperand(1); 10339 10340 // Only works one way, because it needs an immediate operand. 10341 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 10342 return Result; 10343 10344 // First try with the default operand order. 10345 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget)) 10346 return Result; 10347 10348 // If that didn't work, try again with the operands commuted. 10349 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 10350 } 10351 10352 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 10353 /// 10354 static SDValue PerformSUBCombine(SDNode *N, 10355 TargetLowering::DAGCombinerInfo &DCI) { 10356 SDValue N0 = N->getOperand(0); 10357 SDValue N1 = N->getOperand(1); 10358 10359 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 10360 if (N1.getNode()->hasOneUse()) 10361 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI)) 10362 return Result; 10363 10364 return SDValue(); 10365 } 10366 10367 /// PerformVMULCombine 10368 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 10369 /// special multiplier accumulator forwarding. 10370 /// vmul d3, d0, d2 10371 /// vmla d3, d1, d2 10372 /// is faster than 10373 /// vadd d3, d0, d1 10374 /// vmul d3, d3, d2 10375 // However, for (A + B) * (A + B), 10376 // vadd d2, d0, d1 10377 // vmul d3, d0, d2 10378 // vmla d3, d1, d2 10379 // is slower than 10380 // vadd d2, d0, d1 10381 // vmul d3, d2, d2 10382 static SDValue PerformVMULCombine(SDNode *N, 10383 TargetLowering::DAGCombinerInfo &DCI, 10384 const ARMSubtarget *Subtarget) { 10385 if (!Subtarget->hasVMLxForwarding()) 10386 return SDValue(); 10387 10388 SelectionDAG &DAG = DCI.DAG; 10389 SDValue N0 = N->getOperand(0); 10390 SDValue N1 = N->getOperand(1); 10391 unsigned Opcode = N0.getOpcode(); 10392 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 10393 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 10394 Opcode = N1.getOpcode(); 10395 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 10396 Opcode != ISD::FADD && Opcode != ISD::FSUB) 10397 return SDValue(); 10398 std::swap(N0, N1); 10399 } 10400 10401 if (N0 == N1) 10402 return SDValue(); 10403 10404 EVT VT = N->getValueType(0); 10405 SDLoc DL(N); 10406 SDValue N00 = N0->getOperand(0); 10407 SDValue N01 = N0->getOperand(1); 10408 return DAG.getNode(Opcode, DL, VT, 10409 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 10410 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 10411 } 10412 10413 static SDValue PerformMULCombine(SDNode *N, 10414 TargetLowering::DAGCombinerInfo &DCI, 10415 const ARMSubtarget *Subtarget) { 10416 SelectionDAG &DAG = DCI.DAG; 10417 10418 if (Subtarget->isThumb1Only()) 10419 return SDValue(); 10420 10421 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 10422 return SDValue(); 10423 10424 EVT VT = N->getValueType(0); 10425 if (VT.is64BitVector() || VT.is128BitVector()) 10426 return PerformVMULCombine(N, DCI, Subtarget); 10427 if (VT != MVT::i32) 10428 return SDValue(); 10429 10430 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10431 if (!C) 10432 return SDValue(); 10433 10434 int64_t MulAmt = C->getSExtValue(); 10435 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 10436 10437 ShiftAmt = ShiftAmt & (32 - 1); 10438 SDValue V = N->getOperand(0); 10439 SDLoc DL(N); 10440 10441 SDValue Res; 10442 MulAmt >>= ShiftAmt; 10443 10444 if (MulAmt >= 0) { 10445 if (isPowerOf2_32(MulAmt - 1)) { 10446 // (mul x, 2^N + 1) => (add (shl x, N), x) 10447 Res = DAG.getNode(ISD::ADD, DL, VT, 10448 V, 10449 DAG.getNode(ISD::SHL, DL, VT, 10450 V, 10451 DAG.getConstant(Log2_32(MulAmt - 1), DL, 10452 MVT::i32))); 10453 } else if (isPowerOf2_32(MulAmt + 1)) { 10454 // (mul x, 2^N - 1) => (sub (shl x, N), x) 10455 Res = DAG.getNode(ISD::SUB, DL, VT, 10456 DAG.getNode(ISD::SHL, DL, VT, 10457 V, 10458 DAG.getConstant(Log2_32(MulAmt + 1), DL, 10459 MVT::i32)), 10460 V); 10461 } else 10462 return SDValue(); 10463 } else { 10464 uint64_t MulAmtAbs = -MulAmt; 10465 if (isPowerOf2_32(MulAmtAbs + 1)) { 10466 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 10467 Res = DAG.getNode(ISD::SUB, DL, VT, 10468 V, 10469 DAG.getNode(ISD::SHL, DL, VT, 10470 V, 10471 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL, 10472 MVT::i32))); 10473 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 10474 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 10475 Res = DAG.getNode(ISD::ADD, DL, VT, 10476 V, 10477 DAG.getNode(ISD::SHL, DL, VT, 10478 V, 10479 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL, 10480 MVT::i32))); 10481 Res = DAG.getNode(ISD::SUB, DL, VT, 10482 DAG.getConstant(0, DL, MVT::i32), Res); 10483 } else 10484 return SDValue(); 10485 } 10486 10487 if (ShiftAmt != 0) 10488 Res = DAG.getNode(ISD::SHL, DL, VT, 10489 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32)); 10490 10491 // Do not add new nodes to DAG combiner worklist. 10492 DCI.CombineTo(N, Res, false); 10493 return SDValue(); 10494 } 10495 10496 static SDValue PerformANDCombine(SDNode *N, 10497 TargetLowering::DAGCombinerInfo &DCI, 10498 const ARMSubtarget *Subtarget) { 10499 // Attempt to use immediate-form VBIC 10500 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 10501 SDLoc dl(N); 10502 EVT VT = N->getValueType(0); 10503 SelectionDAG &DAG = DCI.DAG; 10504 10505 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 10506 return SDValue(); 10507 10508 APInt SplatBits, SplatUndef; 10509 unsigned SplatBitSize; 10510 bool HasAnyUndefs; 10511 if (BVN && 10512 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 10513 if (SplatBitSize <= 64) { 10514 EVT VbicVT; 10515 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 10516 SplatUndef.getZExtValue(), SplatBitSize, 10517 DAG, dl, VbicVT, VT.is128BitVector(), 10518 OtherModImm); 10519 if (Val.getNode()) { 10520 SDValue Input = 10521 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 10522 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 10523 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 10524 } 10525 } 10526 } 10527 10528 if (!Subtarget->isThumb1Only()) { 10529 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 10530 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI)) 10531 return Result; 10532 10533 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 10534 return Result; 10535 } 10536 10537 return SDValue(); 10538 } 10539 10540 // Try combining OR nodes to SMULWB, SMULWT. 10541 static SDValue PerformORCombineToSMULWBT(SDNode *OR, 10542 TargetLowering::DAGCombinerInfo &DCI, 10543 const ARMSubtarget *Subtarget) { 10544 if (!Subtarget->hasV6Ops() || 10545 (Subtarget->isThumb() && 10546 (!Subtarget->hasThumb2() || !Subtarget->hasDSP()))) 10547 return SDValue(); 10548 10549 SDValue SRL = OR->getOperand(0); 10550 SDValue SHL = OR->getOperand(1); 10551 10552 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) { 10553 SRL = OR->getOperand(1); 10554 SHL = OR->getOperand(0); 10555 } 10556 if (!isSRL16(SRL) || !isSHL16(SHL)) 10557 return SDValue(); 10558 10559 // The first operands to the shifts need to be the two results from the 10560 // same smul_lohi node. 10561 if ((SRL.getOperand(0).getNode() != SHL.getOperand(0).getNode()) || 10562 SRL.getOperand(0).getOpcode() != ISD::SMUL_LOHI) 10563 return SDValue(); 10564 10565 SDNode *SMULLOHI = SRL.getOperand(0).getNode(); 10566 if (SRL.getOperand(0) != SDValue(SMULLOHI, 0) || 10567 SHL.getOperand(0) != SDValue(SMULLOHI, 1)) 10568 return SDValue(); 10569 10570 // Now we have: 10571 // (or (srl (smul_lohi ?, ?), 16), (shl (smul_lohi ?, ?), 16))) 10572 // For SMUL[B|T] smul_lohi will take a 32-bit and a 16-bit arguments. 10573 // For SMUWB the 16-bit value will signed extended somehow. 10574 // For SMULWT only the SRA is required. 10575 // Check both sides of SMUL_LOHI 10576 SDValue OpS16 = SMULLOHI->getOperand(0); 10577 SDValue OpS32 = SMULLOHI->getOperand(1); 10578 10579 SelectionDAG &DAG = DCI.DAG; 10580 if (!isS16(OpS16, DAG) && !isSRA16(OpS16)) { 10581 OpS16 = OpS32; 10582 OpS32 = SMULLOHI->getOperand(0); 10583 } 10584 10585 SDLoc dl(OR); 10586 unsigned Opcode = 0; 10587 if (isS16(OpS16, DAG)) 10588 Opcode = ARMISD::SMULWB; 10589 else if (isSRA16(OpS16)) { 10590 Opcode = ARMISD::SMULWT; 10591 OpS16 = OpS16->getOperand(0); 10592 } 10593 else 10594 return SDValue(); 10595 10596 SDValue Res = DAG.getNode(Opcode, dl, MVT::i32, OpS32, OpS16); 10597 DAG.ReplaceAllUsesOfValueWith(SDValue(OR, 0), Res); 10598 return SDValue(OR, 0); 10599 } 10600 10601 static SDValue PerformORCombineToBFI(SDNode *N, 10602 TargetLowering::DAGCombinerInfo &DCI, 10603 const ARMSubtarget *Subtarget) { 10604 // BFI is only available on V6T2+ 10605 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 10606 return SDValue(); 10607 10608 EVT VT = N->getValueType(0); 10609 SDValue N0 = N->getOperand(0); 10610 SDValue N1 = N->getOperand(1); 10611 SelectionDAG &DAG = DCI.DAG; 10612 SDLoc DL(N); 10613 // 1) or (and A, mask), val => ARMbfi A, val, mask 10614 // iff (val & mask) == val 10615 // 10616 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 10617 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 10618 // && mask == ~mask2 10619 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 10620 // && ~mask == mask2 10621 // (i.e., copy a bitfield value into another bitfield of the same width) 10622 10623 if (VT != MVT::i32) 10624 return SDValue(); 10625 10626 SDValue N00 = N0.getOperand(0); 10627 10628 // The value and the mask need to be constants so we can verify this is 10629 // actually a bitfield set. If the mask is 0xffff, we can do better 10630 // via a movt instruction, so don't use BFI in that case. 10631 SDValue MaskOp = N0.getOperand(1); 10632 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 10633 if (!MaskC) 10634 return SDValue(); 10635 unsigned Mask = MaskC->getZExtValue(); 10636 if (Mask == 0xffff) 10637 return SDValue(); 10638 SDValue Res; 10639 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 10640 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 10641 if (N1C) { 10642 unsigned Val = N1C->getZExtValue(); 10643 if ((Val & ~Mask) != Val) 10644 return SDValue(); 10645 10646 if (ARM::isBitFieldInvertedMask(Mask)) { 10647 Val >>= countTrailingZeros(~Mask); 10648 10649 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 10650 DAG.getConstant(Val, DL, MVT::i32), 10651 DAG.getConstant(Mask, DL, MVT::i32)); 10652 10653 DCI.CombineTo(N, Res, false); 10654 // Return value from the original node to inform the combiner than N is 10655 // now dead. 10656 return SDValue(N, 0); 10657 } 10658 } else if (N1.getOpcode() == ISD::AND) { 10659 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 10660 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 10661 if (!N11C) 10662 return SDValue(); 10663 unsigned Mask2 = N11C->getZExtValue(); 10664 10665 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 10666 // as is to match. 10667 if (ARM::isBitFieldInvertedMask(Mask) && 10668 (Mask == ~Mask2)) { 10669 // The pack halfword instruction works better for masks that fit it, 10670 // so use that when it's available. 10671 if (Subtarget->hasDSP() && 10672 (Mask == 0xffff || Mask == 0xffff0000)) 10673 return SDValue(); 10674 // 2a 10675 unsigned amt = countTrailingZeros(Mask2); 10676 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 10677 DAG.getConstant(amt, DL, MVT::i32)); 10678 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 10679 DAG.getConstant(Mask, DL, MVT::i32)); 10680 DCI.CombineTo(N, Res, false); 10681 // Return value from the original node to inform the combiner than N is 10682 // now dead. 10683 return SDValue(N, 0); 10684 } else if (ARM::isBitFieldInvertedMask(~Mask) && 10685 (~Mask == Mask2)) { 10686 // The pack halfword instruction works better for masks that fit it, 10687 // so use that when it's available. 10688 if (Subtarget->hasDSP() && 10689 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 10690 return SDValue(); 10691 // 2b 10692 unsigned lsb = countTrailingZeros(Mask); 10693 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 10694 DAG.getConstant(lsb, DL, MVT::i32)); 10695 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 10696 DAG.getConstant(Mask2, DL, MVT::i32)); 10697 DCI.CombineTo(N, Res, false); 10698 // Return value from the original node to inform the combiner than N is 10699 // now dead. 10700 return SDValue(N, 0); 10701 } 10702 } 10703 10704 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 10705 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 10706 ARM::isBitFieldInvertedMask(~Mask)) { 10707 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 10708 // where lsb(mask) == #shamt and masked bits of B are known zero. 10709 SDValue ShAmt = N00.getOperand(1); 10710 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 10711 unsigned LSB = countTrailingZeros(Mask); 10712 if (ShAmtC != LSB) 10713 return SDValue(); 10714 10715 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 10716 DAG.getConstant(~Mask, DL, MVT::i32)); 10717 10718 DCI.CombineTo(N, Res, false); 10719 // Return value from the original node to inform the combiner than N is 10720 // now dead. 10721 return SDValue(N, 0); 10722 } 10723 10724 return SDValue(); 10725 } 10726 10727 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 10728 static SDValue PerformORCombine(SDNode *N, 10729 TargetLowering::DAGCombinerInfo &DCI, 10730 const ARMSubtarget *Subtarget) { 10731 // Attempt to use immediate-form VORR 10732 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 10733 SDLoc dl(N); 10734 EVT VT = N->getValueType(0); 10735 SelectionDAG &DAG = DCI.DAG; 10736 10737 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 10738 return SDValue(); 10739 10740 APInt SplatBits, SplatUndef; 10741 unsigned SplatBitSize; 10742 bool HasAnyUndefs; 10743 if (BVN && Subtarget->hasNEON() && 10744 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 10745 if (SplatBitSize <= 64) { 10746 EVT VorrVT; 10747 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 10748 SplatUndef.getZExtValue(), SplatBitSize, 10749 DAG, dl, VorrVT, VT.is128BitVector(), 10750 OtherModImm); 10751 if (Val.getNode()) { 10752 SDValue Input = 10753 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 10754 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 10755 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 10756 } 10757 } 10758 } 10759 10760 if (!Subtarget->isThumb1Only()) { 10761 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 10762 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 10763 return Result; 10764 if (SDValue Result = PerformORCombineToSMULWBT(N, DCI, Subtarget)) 10765 return Result; 10766 } 10767 10768 SDValue N0 = N->getOperand(0); 10769 SDValue N1 = N->getOperand(1); 10770 10771 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 10772 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 10773 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 10774 10775 // The code below optimizes (or (and X, Y), Z). 10776 // The AND operand needs to have a single user to make these optimizations 10777 // profitable. 10778 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 10779 return SDValue(); 10780 10781 APInt SplatUndef; 10782 unsigned SplatBitSize; 10783 bool HasAnyUndefs; 10784 10785 APInt SplatBits0, SplatBits1; 10786 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 10787 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 10788 // Ensure that the second operand of both ands are constants 10789 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 10790 HasAnyUndefs) && !HasAnyUndefs) { 10791 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 10792 HasAnyUndefs) && !HasAnyUndefs) { 10793 // Ensure that the bit width of the constants are the same and that 10794 // the splat arguments are logical inverses as per the pattern we 10795 // are trying to simplify. 10796 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 10797 SplatBits0 == ~SplatBits1) { 10798 // Canonicalize the vector type to make instruction selection 10799 // simpler. 10800 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 10801 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 10802 N0->getOperand(1), 10803 N0->getOperand(0), 10804 N1->getOperand(0)); 10805 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 10806 } 10807 } 10808 } 10809 } 10810 10811 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 10812 // reasonable. 10813 if (N0.getOpcode() == ISD::AND && N0.hasOneUse()) { 10814 if (SDValue Res = PerformORCombineToBFI(N, DCI, Subtarget)) 10815 return Res; 10816 } 10817 10818 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 10819 return Result; 10820 10821 return SDValue(); 10822 } 10823 10824 static SDValue PerformXORCombine(SDNode *N, 10825 TargetLowering::DAGCombinerInfo &DCI, 10826 const ARMSubtarget *Subtarget) { 10827 EVT VT = N->getValueType(0); 10828 SelectionDAG &DAG = DCI.DAG; 10829 10830 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 10831 return SDValue(); 10832 10833 if (!Subtarget->isThumb1Only()) { 10834 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 10835 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 10836 return Result; 10837 10838 if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget)) 10839 return Result; 10840 } 10841 10842 return SDValue(); 10843 } 10844 10845 // ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it, 10846 // and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and 10847 // their position in "to" (Rd). 10848 static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) { 10849 assert(N->getOpcode() == ARMISD::BFI); 10850 10851 SDValue From = N->getOperand(1); 10852 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue(); 10853 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation()); 10854 10855 // If the Base came from a SHR #C, we can deduce that it is really testing bit 10856 // #C in the base of the SHR. 10857 if (From->getOpcode() == ISD::SRL && 10858 isa<ConstantSDNode>(From->getOperand(1))) { 10859 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue(); 10860 assert(Shift.getLimitedValue() < 32 && "Shift too large!"); 10861 FromMask <<= Shift.getLimitedValue(31); 10862 From = From->getOperand(0); 10863 } 10864 10865 return From; 10866 } 10867 10868 // If A and B contain one contiguous set of bits, does A | B == A . B? 10869 // 10870 // Neither A nor B must be zero. 10871 static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) { 10872 unsigned LastActiveBitInA = A.countTrailingZeros(); 10873 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1; 10874 return LastActiveBitInA - 1 == FirstActiveBitInB; 10875 } 10876 10877 static SDValue FindBFIToCombineWith(SDNode *N) { 10878 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with, 10879 // if one exists. 10880 APInt ToMask, FromMask; 10881 SDValue From = ParseBFI(N, ToMask, FromMask); 10882 SDValue To = N->getOperand(0); 10883 10884 // Now check for a compatible BFI to merge with. We can pass through BFIs that 10885 // aren't compatible, but not if they set the same bit in their destination as 10886 // we do (or that of any BFI we're going to combine with). 10887 SDValue V = To; 10888 APInt CombinedToMask = ToMask; 10889 while (V.getOpcode() == ARMISD::BFI) { 10890 APInt NewToMask, NewFromMask; 10891 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask); 10892 if (NewFrom != From) { 10893 // This BFI has a different base. Keep going. 10894 CombinedToMask |= NewToMask; 10895 V = V.getOperand(0); 10896 continue; 10897 } 10898 10899 // Do the written bits conflict with any we've seen so far? 10900 if ((NewToMask & CombinedToMask).getBoolValue()) 10901 // Conflicting bits - bail out because going further is unsafe. 10902 return SDValue(); 10903 10904 // Are the new bits contiguous when combined with the old bits? 10905 if (BitsProperlyConcatenate(ToMask, NewToMask) && 10906 BitsProperlyConcatenate(FromMask, NewFromMask)) 10907 return V; 10908 if (BitsProperlyConcatenate(NewToMask, ToMask) && 10909 BitsProperlyConcatenate(NewFromMask, FromMask)) 10910 return V; 10911 10912 // We've seen a write to some bits, so track it. 10913 CombinedToMask |= NewToMask; 10914 // Keep going... 10915 V = V.getOperand(0); 10916 } 10917 10918 return SDValue(); 10919 } 10920 10921 static SDValue PerformBFICombine(SDNode *N, 10922 TargetLowering::DAGCombinerInfo &DCI) { 10923 SDValue N1 = N->getOperand(1); 10924 if (N1.getOpcode() == ISD::AND) { 10925 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 10926 // the bits being cleared by the AND are not demanded by the BFI. 10927 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 10928 if (!N11C) 10929 return SDValue(); 10930 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 10931 unsigned LSB = countTrailingZeros(~InvMask); 10932 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 10933 assert(Width < 10934 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) && 10935 "undefined behavior"); 10936 unsigned Mask = (1u << Width) - 1; 10937 unsigned Mask2 = N11C->getZExtValue(); 10938 if ((Mask & (~Mask2)) == 0) 10939 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 10940 N->getOperand(0), N1.getOperand(0), 10941 N->getOperand(2)); 10942 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) { 10943 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes. 10944 // Keep track of any consecutive bits set that all come from the same base 10945 // value. We can combine these together into a single BFI. 10946 SDValue CombineBFI = FindBFIToCombineWith(N); 10947 if (CombineBFI == SDValue()) 10948 return SDValue(); 10949 10950 // We've found a BFI. 10951 APInt ToMask1, FromMask1; 10952 SDValue From1 = ParseBFI(N, ToMask1, FromMask1); 10953 10954 APInt ToMask2, FromMask2; 10955 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2); 10956 assert(From1 == From2); 10957 (void)From2; 10958 10959 // First, unlink CombineBFI. 10960 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0)); 10961 // Then create a new BFI, combining the two together. 10962 APInt NewFromMask = FromMask1 | FromMask2; 10963 APInt NewToMask = ToMask1 | ToMask2; 10964 10965 EVT VT = N->getValueType(0); 10966 SDLoc dl(N); 10967 10968 if (NewFromMask[0] == 0) 10969 From1 = DCI.DAG.getNode( 10970 ISD::SRL, dl, VT, From1, 10971 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT)); 10972 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1, 10973 DCI.DAG.getConstant(~NewToMask, dl, VT)); 10974 } 10975 return SDValue(); 10976 } 10977 10978 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 10979 /// ARMISD::VMOVRRD. 10980 static SDValue PerformVMOVRRDCombine(SDNode *N, 10981 TargetLowering::DAGCombinerInfo &DCI, 10982 const ARMSubtarget *Subtarget) { 10983 // vmovrrd(vmovdrr x, y) -> x,y 10984 SDValue InDouble = N->getOperand(0); 10985 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP()) 10986 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 10987 10988 // vmovrrd(load f64) -> (load i32), (load i32) 10989 SDNode *InNode = InDouble.getNode(); 10990 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 10991 InNode->getValueType(0) == MVT::f64 && 10992 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 10993 !cast<LoadSDNode>(InNode)->isVolatile()) { 10994 // TODO: Should this be done for non-FrameIndex operands? 10995 LoadSDNode *LD = cast<LoadSDNode>(InNode); 10996 10997 SelectionDAG &DAG = DCI.DAG; 10998 SDLoc DL(LD); 10999 SDValue BasePtr = LD->getBasePtr(); 11000 SDValue NewLD1 = 11001 DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(), 11002 LD->getAlignment(), LD->getMemOperand()->getFlags()); 11003 11004 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 11005 DAG.getConstant(4, DL, MVT::i32)); 11006 SDValue NewLD2 = DAG.getLoad( 11007 MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, LD->getPointerInfo(), 11008 std::min(4U, LD->getAlignment() / 2), LD->getMemOperand()->getFlags()); 11009 11010 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 11011 if (DCI.DAG.getDataLayout().isBigEndian()) 11012 std::swap (NewLD1, NewLD2); 11013 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 11014 return Result; 11015 } 11016 11017 return SDValue(); 11018 } 11019 11020 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 11021 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 11022 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 11023 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 11024 SDValue Op0 = N->getOperand(0); 11025 SDValue Op1 = N->getOperand(1); 11026 if (Op0.getOpcode() == ISD::BITCAST) 11027 Op0 = Op0.getOperand(0); 11028 if (Op1.getOpcode() == ISD::BITCAST) 11029 Op1 = Op1.getOperand(0); 11030 if (Op0.getOpcode() == ARMISD::VMOVRRD && 11031 Op0.getNode() == Op1.getNode() && 11032 Op0.getResNo() == 0 && Op1.getResNo() == 1) 11033 return DAG.getNode(ISD::BITCAST, SDLoc(N), 11034 N->getValueType(0), Op0.getOperand(0)); 11035 return SDValue(); 11036 } 11037 11038 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 11039 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 11040 /// i64 vector to have f64 elements, since the value can then be loaded 11041 /// directly into a VFP register. 11042 static bool hasNormalLoadOperand(SDNode *N) { 11043 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 11044 for (unsigned i = 0; i < NumElts; ++i) { 11045 SDNode *Elt = N->getOperand(i).getNode(); 11046 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 11047 return true; 11048 } 11049 return false; 11050 } 11051 11052 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 11053 /// ISD::BUILD_VECTOR. 11054 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 11055 TargetLowering::DAGCombinerInfo &DCI, 11056 const ARMSubtarget *Subtarget) { 11057 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 11058 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 11059 // into a pair of GPRs, which is fine when the value is used as a scalar, 11060 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 11061 SelectionDAG &DAG = DCI.DAG; 11062 if (N->getNumOperands() == 2) 11063 if (SDValue RV = PerformVMOVDRRCombine(N, DAG)) 11064 return RV; 11065 11066 // Load i64 elements as f64 values so that type legalization does not split 11067 // them up into i32 values. 11068 EVT VT = N->getValueType(0); 11069 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 11070 return SDValue(); 11071 SDLoc dl(N); 11072 SmallVector<SDValue, 8> Ops; 11073 unsigned NumElts = VT.getVectorNumElements(); 11074 for (unsigned i = 0; i < NumElts; ++i) { 11075 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 11076 Ops.push_back(V); 11077 // Make the DAGCombiner fold the bitcast. 11078 DCI.AddToWorklist(V.getNode()); 11079 } 11080 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 11081 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops); 11082 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 11083 } 11084 11085 /// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 11086 static SDValue 11087 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 11088 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 11089 // At that time, we may have inserted bitcasts from integer to float. 11090 // If these bitcasts have survived DAGCombine, change the lowering of this 11091 // BUILD_VECTOR in something more vector friendly, i.e., that does not 11092 // force to use floating point types. 11093 11094 // Make sure we can change the type of the vector. 11095 // This is possible iff: 11096 // 1. The vector is only used in a bitcast to a integer type. I.e., 11097 // 1.1. Vector is used only once. 11098 // 1.2. Use is a bit convert to an integer type. 11099 // 2. The size of its operands are 32-bits (64-bits are not legal). 11100 EVT VT = N->getValueType(0); 11101 EVT EltVT = VT.getVectorElementType(); 11102 11103 // Check 1.1. and 2. 11104 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 11105 return SDValue(); 11106 11107 // By construction, the input type must be float. 11108 assert(EltVT == MVT::f32 && "Unexpected type!"); 11109 11110 // Check 1.2. 11111 SDNode *Use = *N->use_begin(); 11112 if (Use->getOpcode() != ISD::BITCAST || 11113 Use->getValueType(0).isFloatingPoint()) 11114 return SDValue(); 11115 11116 // Check profitability. 11117 // Model is, if more than half of the relevant operands are bitcast from 11118 // i32, turn the build_vector into a sequence of insert_vector_elt. 11119 // Relevant operands are everything that is not statically 11120 // (i.e., at compile time) bitcasted. 11121 unsigned NumOfBitCastedElts = 0; 11122 unsigned NumElts = VT.getVectorNumElements(); 11123 unsigned NumOfRelevantElts = NumElts; 11124 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 11125 SDValue Elt = N->getOperand(Idx); 11126 if (Elt->getOpcode() == ISD::BITCAST) { 11127 // Assume only bit cast to i32 will go away. 11128 if (Elt->getOperand(0).getValueType() == MVT::i32) 11129 ++NumOfBitCastedElts; 11130 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt)) 11131 // Constants are statically casted, thus do not count them as 11132 // relevant operands. 11133 --NumOfRelevantElts; 11134 } 11135 11136 // Check if more than half of the elements require a non-free bitcast. 11137 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 11138 return SDValue(); 11139 11140 SelectionDAG &DAG = DCI.DAG; 11141 // Create the new vector type. 11142 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 11143 // Check if the type is legal. 11144 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11145 if (!TLI.isTypeLegal(VecVT)) 11146 return SDValue(); 11147 11148 // Combine: 11149 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 11150 // => BITCAST INSERT_VECTOR_ELT 11151 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 11152 // (BITCAST EN), N. 11153 SDValue Vec = DAG.getUNDEF(VecVT); 11154 SDLoc dl(N); 11155 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 11156 SDValue V = N->getOperand(Idx); 11157 if (V.isUndef()) 11158 continue; 11159 if (V.getOpcode() == ISD::BITCAST && 11160 V->getOperand(0).getValueType() == MVT::i32) 11161 // Fold obvious case. 11162 V = V.getOperand(0); 11163 else { 11164 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 11165 // Make the DAGCombiner fold the bitcasts. 11166 DCI.AddToWorklist(V.getNode()); 11167 } 11168 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32); 11169 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 11170 } 11171 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 11172 // Make the DAGCombiner fold the bitcasts. 11173 DCI.AddToWorklist(Vec.getNode()); 11174 return Vec; 11175 } 11176 11177 /// PerformInsertEltCombine - Target-specific dag combine xforms for 11178 /// ISD::INSERT_VECTOR_ELT. 11179 static SDValue PerformInsertEltCombine(SDNode *N, 11180 TargetLowering::DAGCombinerInfo &DCI) { 11181 // Bitcast an i64 load inserted into a vector to f64. 11182 // Otherwise, the i64 value will be legalized to a pair of i32 values. 11183 EVT VT = N->getValueType(0); 11184 SDNode *Elt = N->getOperand(1).getNode(); 11185 if (VT.getVectorElementType() != MVT::i64 || 11186 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 11187 return SDValue(); 11188 11189 SelectionDAG &DAG = DCI.DAG; 11190 SDLoc dl(N); 11191 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 11192 VT.getVectorNumElements()); 11193 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 11194 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 11195 // Make the DAGCombiner fold the bitcasts. 11196 DCI.AddToWorklist(Vec.getNode()); 11197 DCI.AddToWorklist(V.getNode()); 11198 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 11199 Vec, V, N->getOperand(2)); 11200 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 11201 } 11202 11203 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 11204 /// ISD::VECTOR_SHUFFLE. 11205 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 11206 // The LLVM shufflevector instruction does not require the shuffle mask 11207 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 11208 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 11209 // operands do not match the mask length, they are extended by concatenating 11210 // them with undef vectors. That is probably the right thing for other 11211 // targets, but for NEON it is better to concatenate two double-register 11212 // size vector operands into a single quad-register size vector. Do that 11213 // transformation here: 11214 // shuffle(concat(v1, undef), concat(v2, undef)) -> 11215 // shuffle(concat(v1, v2), undef) 11216 SDValue Op0 = N->getOperand(0); 11217 SDValue Op1 = N->getOperand(1); 11218 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 11219 Op1.getOpcode() != ISD::CONCAT_VECTORS || 11220 Op0.getNumOperands() != 2 || 11221 Op1.getNumOperands() != 2) 11222 return SDValue(); 11223 SDValue Concat0Op1 = Op0.getOperand(1); 11224 SDValue Concat1Op1 = Op1.getOperand(1); 11225 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef()) 11226 return SDValue(); 11227 // Skip the transformation if any of the types are illegal. 11228 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11229 EVT VT = N->getValueType(0); 11230 if (!TLI.isTypeLegal(VT) || 11231 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 11232 !TLI.isTypeLegal(Concat1Op1.getValueType())) 11233 return SDValue(); 11234 11235 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 11236 Op0.getOperand(0), Op1.getOperand(0)); 11237 // Translate the shuffle mask. 11238 SmallVector<int, 16> NewMask; 11239 unsigned NumElts = VT.getVectorNumElements(); 11240 unsigned HalfElts = NumElts/2; 11241 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 11242 for (unsigned n = 0; n < NumElts; ++n) { 11243 int MaskElt = SVN->getMaskElt(n); 11244 int NewElt = -1; 11245 if (MaskElt < (int)HalfElts) 11246 NewElt = MaskElt; 11247 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 11248 NewElt = HalfElts + MaskElt - NumElts; 11249 NewMask.push_back(NewElt); 11250 } 11251 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 11252 DAG.getUNDEF(VT), NewMask); 11253 } 11254 11255 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP, 11256 /// NEON load/store intrinsics, and generic vector load/stores, to merge 11257 /// base address updates. 11258 /// For generic load/stores, the memory type is assumed to be a vector. 11259 /// The caller is assumed to have checked legality. 11260 static SDValue CombineBaseUpdate(SDNode *N, 11261 TargetLowering::DAGCombinerInfo &DCI) { 11262 SelectionDAG &DAG = DCI.DAG; 11263 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 11264 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 11265 const bool isStore = N->getOpcode() == ISD::STORE; 11266 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1); 11267 SDValue Addr = N->getOperand(AddrOpIdx); 11268 MemSDNode *MemN = cast<MemSDNode>(N); 11269 SDLoc dl(N); 11270 11271 // Search for a use of the address operand that is an increment. 11272 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 11273 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 11274 SDNode *User = *UI; 11275 if (User->getOpcode() != ISD::ADD || 11276 UI.getUse().getResNo() != Addr.getResNo()) 11277 continue; 11278 11279 // Check that the add is independent of the load/store. Otherwise, folding 11280 // it would create a cycle. 11281 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 11282 continue; 11283 11284 // Find the new opcode for the updating load/store. 11285 bool isLoadOp = true; 11286 bool isLaneOp = false; 11287 unsigned NewOpc = 0; 11288 unsigned NumVecs = 0; 11289 if (isIntrinsic) { 11290 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 11291 switch (IntNo) { 11292 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 11293 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 11294 NumVecs = 1; break; 11295 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 11296 NumVecs = 2; break; 11297 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 11298 NumVecs = 3; break; 11299 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 11300 NumVecs = 4; break; 11301 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 11302 NumVecs = 2; isLaneOp = true; break; 11303 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 11304 NumVecs = 3; isLaneOp = true; break; 11305 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 11306 NumVecs = 4; isLaneOp = true; break; 11307 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 11308 NumVecs = 1; isLoadOp = false; break; 11309 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 11310 NumVecs = 2; isLoadOp = false; break; 11311 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 11312 NumVecs = 3; isLoadOp = false; break; 11313 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 11314 NumVecs = 4; isLoadOp = false; break; 11315 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 11316 NumVecs = 2; isLoadOp = false; isLaneOp = true; break; 11317 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 11318 NumVecs = 3; isLoadOp = false; isLaneOp = true; break; 11319 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 11320 NumVecs = 4; isLoadOp = false; isLaneOp = true; break; 11321 } 11322 } else { 11323 isLaneOp = true; 11324 switch (N->getOpcode()) { 11325 default: llvm_unreachable("unexpected opcode for Neon base update"); 11326 case ARMISD::VLD1DUP: NewOpc = ARMISD::VLD1DUP_UPD; NumVecs = 1; break; 11327 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 11328 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 11329 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 11330 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD; 11331 NumVecs = 1; isLaneOp = false; break; 11332 case ISD::STORE: NewOpc = ARMISD::VST1_UPD; 11333 NumVecs = 1; isLaneOp = false; isLoadOp = false; break; 11334 } 11335 } 11336 11337 // Find the size of memory referenced by the load/store. 11338 EVT VecTy; 11339 if (isLoadOp) { 11340 VecTy = N->getValueType(0); 11341 } else if (isIntrinsic) { 11342 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 11343 } else { 11344 assert(isStore && "Node has to be a load, a store, or an intrinsic!"); 11345 VecTy = N->getOperand(1).getValueType(); 11346 } 11347 11348 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 11349 if (isLaneOp) 11350 NumBytes /= VecTy.getVectorNumElements(); 11351 11352 // If the increment is a constant, it must match the memory ref size. 11353 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 11354 ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode()); 11355 if (NumBytes >= 3 * 16 && (!CInc || CInc->getZExtValue() != NumBytes)) { 11356 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 11357 // separate instructions that make it harder to use a non-constant update. 11358 continue; 11359 } 11360 11361 // OK, we found an ADD we can fold into the base update. 11362 // Now, create a _UPD node, taking care of not breaking alignment. 11363 11364 EVT AlignedVecTy = VecTy; 11365 unsigned Alignment = MemN->getAlignment(); 11366 11367 // If this is a less-than-standard-aligned load/store, change the type to 11368 // match the standard alignment. 11369 // The alignment is overlooked when selecting _UPD variants; and it's 11370 // easier to introduce bitcasts here than fix that. 11371 // There are 3 ways to get to this base-update combine: 11372 // - intrinsics: they are assumed to be properly aligned (to the standard 11373 // alignment of the memory type), so we don't need to do anything. 11374 // - ARMISD::VLDx nodes: they are only generated from the aforementioned 11375 // intrinsics, so, likewise, there's nothing to do. 11376 // - generic load/store instructions: the alignment is specified as an 11377 // explicit operand, rather than implicitly as the standard alignment 11378 // of the memory type (like the intrisics). We need to change the 11379 // memory type to match the explicit alignment. That way, we don't 11380 // generate non-standard-aligned ARMISD::VLDx nodes. 11381 if (isa<LSBaseSDNode>(N)) { 11382 if (Alignment == 0) 11383 Alignment = 1; 11384 if (Alignment < VecTy.getScalarSizeInBits() / 8) { 11385 MVT EltTy = MVT::getIntegerVT(Alignment * 8); 11386 assert(NumVecs == 1 && "Unexpected multi-element generic load/store."); 11387 assert(!isLaneOp && "Unexpected generic load/store lane."); 11388 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8); 11389 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts); 11390 } 11391 // Don't set an explicit alignment on regular load/stores that we want 11392 // to transform to VLD/VST 1_UPD nodes. 11393 // This matches the behavior of regular load/stores, which only get an 11394 // explicit alignment if the MMO alignment is larger than the standard 11395 // alignment of the memory type. 11396 // Intrinsics, however, always get an explicit alignment, set to the 11397 // alignment of the MMO. 11398 Alignment = 1; 11399 } 11400 11401 // Create the new updating load/store node. 11402 // First, create an SDVTList for the new updating node's results. 11403 EVT Tys[6]; 11404 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0); 11405 unsigned n; 11406 for (n = 0; n < NumResultVecs; ++n) 11407 Tys[n] = AlignedVecTy; 11408 Tys[n++] = MVT::i32; 11409 Tys[n] = MVT::Other; 11410 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2)); 11411 11412 // Then, gather the new node's operands. 11413 SmallVector<SDValue, 8> Ops; 11414 Ops.push_back(N->getOperand(0)); // incoming chain 11415 Ops.push_back(N->getOperand(AddrOpIdx)); 11416 Ops.push_back(Inc); 11417 11418 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) { 11419 // Try to match the intrinsic's signature 11420 Ops.push_back(StN->getValue()); 11421 } else { 11422 // Loads (and of course intrinsics) match the intrinsics' signature, 11423 // so just add all but the alignment operand. 11424 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i) 11425 Ops.push_back(N->getOperand(i)); 11426 } 11427 11428 // For all node types, the alignment operand is always the last one. 11429 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32)); 11430 11431 // If this is a non-standard-aligned STORE, the penultimate operand is the 11432 // stored value. Bitcast it to the aligned type. 11433 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) { 11434 SDValue &StVal = Ops[Ops.size()-2]; 11435 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal); 11436 } 11437 11438 EVT LoadVT = isLaneOp ? VecTy.getVectorElementType() : AlignedVecTy; 11439 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, Ops, LoadVT, 11440 MemN->getMemOperand()); 11441 11442 // Update the uses. 11443 SmallVector<SDValue, 5> NewResults; 11444 for (unsigned i = 0; i < NumResultVecs; ++i) 11445 NewResults.push_back(SDValue(UpdN.getNode(), i)); 11446 11447 // If this is an non-standard-aligned LOAD, the first result is the loaded 11448 // value. Bitcast it to the expected result type. 11449 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) { 11450 SDValue &LdVal = NewResults[0]; 11451 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal); 11452 } 11453 11454 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 11455 DCI.CombineTo(N, NewResults); 11456 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 11457 11458 break; 11459 } 11460 return SDValue(); 11461 } 11462 11463 static SDValue PerformVLDCombine(SDNode *N, 11464 TargetLowering::DAGCombinerInfo &DCI) { 11465 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 11466 return SDValue(); 11467 11468 return CombineBaseUpdate(N, DCI); 11469 } 11470 11471 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 11472 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 11473 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 11474 /// return true. 11475 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 11476 SelectionDAG &DAG = DCI.DAG; 11477 EVT VT = N->getValueType(0); 11478 // vldN-dup instructions only support 64-bit vectors for N > 1. 11479 if (!VT.is64BitVector()) 11480 return false; 11481 11482 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 11483 SDNode *VLD = N->getOperand(0).getNode(); 11484 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 11485 return false; 11486 unsigned NumVecs = 0; 11487 unsigned NewOpc = 0; 11488 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 11489 if (IntNo == Intrinsic::arm_neon_vld2lane) { 11490 NumVecs = 2; 11491 NewOpc = ARMISD::VLD2DUP; 11492 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 11493 NumVecs = 3; 11494 NewOpc = ARMISD::VLD3DUP; 11495 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 11496 NumVecs = 4; 11497 NewOpc = ARMISD::VLD4DUP; 11498 } else { 11499 return false; 11500 } 11501 11502 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 11503 // numbers match the load. 11504 unsigned VLDLaneNo = 11505 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 11506 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 11507 UI != UE; ++UI) { 11508 // Ignore uses of the chain result. 11509 if (UI.getUse().getResNo() == NumVecs) 11510 continue; 11511 SDNode *User = *UI; 11512 if (User->getOpcode() != ARMISD::VDUPLANE || 11513 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 11514 return false; 11515 } 11516 11517 // Create the vldN-dup node. 11518 EVT Tys[5]; 11519 unsigned n; 11520 for (n = 0; n < NumVecs; ++n) 11521 Tys[n] = VT; 11522 Tys[n] = MVT::Other; 11523 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1)); 11524 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 11525 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 11526 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 11527 Ops, VLDMemInt->getMemoryVT(), 11528 VLDMemInt->getMemOperand()); 11529 11530 // Update the uses. 11531 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 11532 UI != UE; ++UI) { 11533 unsigned ResNo = UI.getUse().getResNo(); 11534 // Ignore uses of the chain result. 11535 if (ResNo == NumVecs) 11536 continue; 11537 SDNode *User = *UI; 11538 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 11539 } 11540 11541 // Now the vldN-lane intrinsic is dead except for its chain result. 11542 // Update uses of the chain. 11543 std::vector<SDValue> VLDDupResults; 11544 for (unsigned n = 0; n < NumVecs; ++n) 11545 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 11546 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 11547 DCI.CombineTo(VLD, VLDDupResults); 11548 11549 return true; 11550 } 11551 11552 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 11553 /// ARMISD::VDUPLANE. 11554 static SDValue PerformVDUPLANECombine(SDNode *N, 11555 TargetLowering::DAGCombinerInfo &DCI) { 11556 SDValue Op = N->getOperand(0); 11557 11558 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 11559 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 11560 if (CombineVLDDUP(N, DCI)) 11561 return SDValue(N, 0); 11562 11563 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 11564 // redundant. Ignore bit_converts for now; element sizes are checked below. 11565 while (Op.getOpcode() == ISD::BITCAST) 11566 Op = Op.getOperand(0); 11567 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 11568 return SDValue(); 11569 11570 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 11571 unsigned EltSize = Op.getScalarValueSizeInBits(); 11572 // The canonical VMOV for a zero vector uses a 32-bit element size. 11573 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 11574 unsigned EltBits; 11575 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 11576 EltSize = 8; 11577 EVT VT = N->getValueType(0); 11578 if (EltSize > VT.getScalarSizeInBits()) 11579 return SDValue(); 11580 11581 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 11582 } 11583 11584 /// PerformVDUPCombine - Target-specific dag combine xforms for ARMISD::VDUP. 11585 static SDValue PerformVDUPCombine(SDNode *N, 11586 TargetLowering::DAGCombinerInfo &DCI) { 11587 SelectionDAG &DAG = DCI.DAG; 11588 SDValue Op = N->getOperand(0); 11589 11590 // Match VDUP(LOAD) -> VLD1DUP. 11591 // We match this pattern here rather than waiting for isel because the 11592 // transform is only legal for unindexed loads. 11593 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op.getNode()); 11594 if (LD && Op.hasOneUse() && LD->isUnindexed() && 11595 LD->getMemoryVT() == N->getValueType(0).getVectorElementType()) { 11596 SDValue Ops[] = { LD->getOperand(0), LD->getOperand(1), 11597 DAG.getConstant(LD->getAlignment(), SDLoc(N), MVT::i32) }; 11598 SDVTList SDTys = DAG.getVTList(N->getValueType(0), MVT::Other); 11599 SDValue VLDDup = DAG.getMemIntrinsicNode(ARMISD::VLD1DUP, SDLoc(N), SDTys, 11600 Ops, LD->getMemoryVT(), 11601 LD->getMemOperand()); 11602 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), VLDDup.getValue(1)); 11603 return VLDDup; 11604 } 11605 11606 return SDValue(); 11607 } 11608 11609 static SDValue PerformLOADCombine(SDNode *N, 11610 TargetLowering::DAGCombinerInfo &DCI) { 11611 EVT VT = N->getValueType(0); 11612 11613 // If this is a legal vector load, try to combine it into a VLD1_UPD. 11614 if (ISD::isNormalLoad(N) && VT.isVector() && 11615 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 11616 return CombineBaseUpdate(N, DCI); 11617 11618 return SDValue(); 11619 } 11620 11621 /// PerformSTORECombine - Target-specific dag combine xforms for 11622 /// ISD::STORE. 11623 static SDValue PerformSTORECombine(SDNode *N, 11624 TargetLowering::DAGCombinerInfo &DCI) { 11625 StoreSDNode *St = cast<StoreSDNode>(N); 11626 if (St->isVolatile()) 11627 return SDValue(); 11628 11629 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 11630 // pack all of the elements in one place. Next, store to memory in fewer 11631 // chunks. 11632 SDValue StVal = St->getValue(); 11633 EVT VT = StVal.getValueType(); 11634 if (St->isTruncatingStore() && VT.isVector()) { 11635 SelectionDAG &DAG = DCI.DAG; 11636 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11637 EVT StVT = St->getMemoryVT(); 11638 unsigned NumElems = VT.getVectorNumElements(); 11639 assert(StVT != VT && "Cannot truncate to the same type"); 11640 unsigned FromEltSz = VT.getScalarSizeInBits(); 11641 unsigned ToEltSz = StVT.getScalarSizeInBits(); 11642 11643 // From, To sizes and ElemCount must be pow of two 11644 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 11645 11646 // We are going to use the original vector elt for storing. 11647 // Accumulated smaller vector elements must be a multiple of the store size. 11648 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 11649 11650 unsigned SizeRatio = FromEltSz / ToEltSz; 11651 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 11652 11653 // Create a type on which we perform the shuffle. 11654 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 11655 NumElems*SizeRatio); 11656 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 11657 11658 SDLoc DL(St); 11659 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 11660 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 11661 for (unsigned i = 0; i < NumElems; ++i) 11662 ShuffleVec[i] = DAG.getDataLayout().isBigEndian() 11663 ? (i + 1) * SizeRatio - 1 11664 : i * SizeRatio; 11665 11666 // Can't shuffle using an illegal type. 11667 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 11668 11669 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 11670 DAG.getUNDEF(WideVec.getValueType()), 11671 ShuffleVec); 11672 // At this point all of the data is stored at the bottom of the 11673 // register. We now need to save it to mem. 11674 11675 // Find the largest store unit 11676 MVT StoreType = MVT::i8; 11677 for (MVT Tp : MVT::integer_valuetypes()) { 11678 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 11679 StoreType = Tp; 11680 } 11681 // Didn't find a legal store type. 11682 if (!TLI.isTypeLegal(StoreType)) 11683 return SDValue(); 11684 11685 // Bitcast the original vector into a vector of store-size units 11686 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 11687 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 11688 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 11689 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 11690 SmallVector<SDValue, 8> Chains; 11691 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL, 11692 TLI.getPointerTy(DAG.getDataLayout())); 11693 SDValue BasePtr = St->getBasePtr(); 11694 11695 // Perform one or more big stores into memory. 11696 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 11697 for (unsigned I = 0; I < E; I++) { 11698 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 11699 StoreType, ShuffWide, 11700 DAG.getIntPtrConstant(I, DL)); 11701 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 11702 St->getPointerInfo(), St->getAlignment(), 11703 St->getMemOperand()->getFlags()); 11704 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 11705 Increment); 11706 Chains.push_back(Ch); 11707 } 11708 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 11709 } 11710 11711 if (!ISD::isNormalStore(St)) 11712 return SDValue(); 11713 11714 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 11715 // ARM stores of arguments in the same cache line. 11716 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 11717 StVal.getNode()->hasOneUse()) { 11718 SelectionDAG &DAG = DCI.DAG; 11719 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 11720 SDLoc DL(St); 11721 SDValue BasePtr = St->getBasePtr(); 11722 SDValue NewST1 = DAG.getStore( 11723 St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0), 11724 BasePtr, St->getPointerInfo(), St->getAlignment(), 11725 St->getMemOperand()->getFlags()); 11726 11727 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 11728 DAG.getConstant(4, DL, MVT::i32)); 11729 return DAG.getStore(NewST1.getValue(0), DL, 11730 StVal.getNode()->getOperand(isBigEndian ? 0 : 1), 11731 OffsetPtr, St->getPointerInfo(), 11732 std::min(4U, St->getAlignment() / 2), 11733 St->getMemOperand()->getFlags()); 11734 } 11735 11736 if (StVal.getValueType() == MVT::i64 && 11737 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 11738 11739 // Bitcast an i64 store extracted from a vector to f64. 11740 // Otherwise, the i64 value will be legalized to a pair of i32 values. 11741 SelectionDAG &DAG = DCI.DAG; 11742 SDLoc dl(StVal); 11743 SDValue IntVec = StVal.getOperand(0); 11744 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 11745 IntVec.getValueType().getVectorNumElements()); 11746 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 11747 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 11748 Vec, StVal.getOperand(1)); 11749 dl = SDLoc(N); 11750 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 11751 // Make the DAGCombiner fold the bitcasts. 11752 DCI.AddToWorklist(Vec.getNode()); 11753 DCI.AddToWorklist(ExtElt.getNode()); 11754 DCI.AddToWorklist(V.getNode()); 11755 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 11756 St->getPointerInfo(), St->getAlignment(), 11757 St->getMemOperand()->getFlags(), St->getAAInfo()); 11758 } 11759 11760 // If this is a legal vector store, try to combine it into a VST1_UPD. 11761 if (ISD::isNormalStore(N) && VT.isVector() && 11762 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 11763 return CombineBaseUpdate(N, DCI); 11764 11765 return SDValue(); 11766 } 11767 11768 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 11769 /// can replace combinations of VMUL and VCVT (floating-point to integer) 11770 /// when the VMUL has a constant operand that is a power of 2. 11771 /// 11772 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 11773 /// vmul.f32 d16, d17, d16 11774 /// vcvt.s32.f32 d16, d16 11775 /// becomes: 11776 /// vcvt.s32.f32 d16, d16, #3 11777 static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG, 11778 const ARMSubtarget *Subtarget) { 11779 if (!Subtarget->hasNEON()) 11780 return SDValue(); 11781 11782 SDValue Op = N->getOperand(0); 11783 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() || 11784 Op.getOpcode() != ISD::FMUL) 11785 return SDValue(); 11786 11787 SDValue ConstVec = Op->getOperand(1); 11788 if (!isa<BuildVectorSDNode>(ConstVec)) 11789 return SDValue(); 11790 11791 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 11792 uint32_t FloatBits = FloatTy.getSizeInBits(); 11793 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 11794 uint32_t IntBits = IntTy.getSizeInBits(); 11795 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 11796 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) { 11797 // These instructions only exist converting from f32 to i32. We can handle 11798 // smaller integers by generating an extra truncate, but larger ones would 11799 // be lossy. We also can't handle more then 4 lanes, since these intructions 11800 // only support v2i32/v4i32 types. 11801 return SDValue(); 11802 } 11803 11804 BitVector UndefElements; 11805 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 11806 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 11807 if (C == -1 || C == 0 || C > 32) 11808 return SDValue(); 11809 11810 SDLoc dl(N); 11811 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 11812 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 11813 Intrinsic::arm_neon_vcvtfp2fxu; 11814 SDValue FixConv = DAG.getNode( 11815 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 11816 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0), 11817 DAG.getConstant(C, dl, MVT::i32)); 11818 11819 if (IntBits < FloatBits) 11820 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv); 11821 11822 return FixConv; 11823 } 11824 11825 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 11826 /// can replace combinations of VCVT (integer to floating-point) and VDIV 11827 /// when the VDIV has a constant operand that is a power of 2. 11828 /// 11829 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 11830 /// vcvt.f32.s32 d16, d16 11831 /// vdiv.f32 d16, d17, d16 11832 /// becomes: 11833 /// vcvt.f32.s32 d16, d16, #3 11834 static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG, 11835 const ARMSubtarget *Subtarget) { 11836 if (!Subtarget->hasNEON()) 11837 return SDValue(); 11838 11839 SDValue Op = N->getOperand(0); 11840 unsigned OpOpcode = Op.getNode()->getOpcode(); 11841 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() || 11842 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 11843 return SDValue(); 11844 11845 SDValue ConstVec = N->getOperand(1); 11846 if (!isa<BuildVectorSDNode>(ConstVec)) 11847 return SDValue(); 11848 11849 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 11850 uint32_t FloatBits = FloatTy.getSizeInBits(); 11851 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 11852 uint32_t IntBits = IntTy.getSizeInBits(); 11853 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 11854 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) { 11855 // These instructions only exist converting from i32 to f32. We can handle 11856 // smaller integers by generating an extra extend, but larger ones would 11857 // be lossy. We also can't handle more then 4 lanes, since these intructions 11858 // only support v2i32/v4i32 types. 11859 return SDValue(); 11860 } 11861 11862 BitVector UndefElements; 11863 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 11864 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 11865 if (C == -1 || C == 0 || C > 32) 11866 return SDValue(); 11867 11868 SDLoc dl(N); 11869 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 11870 SDValue ConvInput = Op.getOperand(0); 11871 if (IntBits < FloatBits) 11872 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 11873 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 11874 ConvInput); 11875 11876 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 11877 Intrinsic::arm_neon_vcvtfxu2fp; 11878 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, 11879 Op.getValueType(), 11880 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), 11881 ConvInput, DAG.getConstant(C, dl, MVT::i32)); 11882 } 11883 11884 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 11885 /// operand of a vector shift operation, where all the elements of the 11886 /// build_vector must have the same constant integer value. 11887 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 11888 // Ignore bit_converts. 11889 while (Op.getOpcode() == ISD::BITCAST) 11890 Op = Op.getOperand(0); 11891 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 11892 APInt SplatBits, SplatUndef; 11893 unsigned SplatBitSize; 11894 bool HasAnyUndefs; 11895 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 11896 HasAnyUndefs, ElementBits) || 11897 SplatBitSize > ElementBits) 11898 return false; 11899 Cnt = SplatBits.getSExtValue(); 11900 return true; 11901 } 11902 11903 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 11904 /// operand of a vector shift left operation. That value must be in the range: 11905 /// 0 <= Value < ElementBits for a left shift; or 11906 /// 0 <= Value <= ElementBits for a long left shift. 11907 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 11908 assert(VT.isVector() && "vector shift count is not a vector type"); 11909 int64_t ElementBits = VT.getScalarSizeInBits(); 11910 if (! getVShiftImm(Op, ElementBits, Cnt)) 11911 return false; 11912 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 11913 } 11914 11915 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 11916 /// operand of a vector shift right operation. For a shift opcode, the value 11917 /// is positive, but for an intrinsic the value count must be negative. The 11918 /// absolute value must be in the range: 11919 /// 1 <= |Value| <= ElementBits for a right shift; or 11920 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 11921 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 11922 int64_t &Cnt) { 11923 assert(VT.isVector() && "vector shift count is not a vector type"); 11924 int64_t ElementBits = VT.getScalarSizeInBits(); 11925 if (! getVShiftImm(Op, ElementBits, Cnt)) 11926 return false; 11927 if (!isIntrinsic) 11928 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 11929 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) { 11930 Cnt = -Cnt; 11931 return true; 11932 } 11933 return false; 11934 } 11935 11936 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 11937 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 11938 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 11939 switch (IntNo) { 11940 default: 11941 // Don't do anything for most intrinsics. 11942 break; 11943 11944 // Vector shifts: check for immediate versions and lower them. 11945 // Note: This is done during DAG combining instead of DAG legalizing because 11946 // the build_vectors for 64-bit vector element shift counts are generally 11947 // not legal, and it is hard to see their values after they get legalized to 11948 // loads from a constant pool. 11949 case Intrinsic::arm_neon_vshifts: 11950 case Intrinsic::arm_neon_vshiftu: 11951 case Intrinsic::arm_neon_vrshifts: 11952 case Intrinsic::arm_neon_vrshiftu: 11953 case Intrinsic::arm_neon_vrshiftn: 11954 case Intrinsic::arm_neon_vqshifts: 11955 case Intrinsic::arm_neon_vqshiftu: 11956 case Intrinsic::arm_neon_vqshiftsu: 11957 case Intrinsic::arm_neon_vqshiftns: 11958 case Intrinsic::arm_neon_vqshiftnu: 11959 case Intrinsic::arm_neon_vqshiftnsu: 11960 case Intrinsic::arm_neon_vqrshiftns: 11961 case Intrinsic::arm_neon_vqrshiftnu: 11962 case Intrinsic::arm_neon_vqrshiftnsu: { 11963 EVT VT = N->getOperand(1).getValueType(); 11964 int64_t Cnt; 11965 unsigned VShiftOpc = 0; 11966 11967 switch (IntNo) { 11968 case Intrinsic::arm_neon_vshifts: 11969 case Intrinsic::arm_neon_vshiftu: 11970 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 11971 VShiftOpc = ARMISD::VSHL; 11972 break; 11973 } 11974 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 11975 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 11976 ARMISD::VSHRs : ARMISD::VSHRu); 11977 break; 11978 } 11979 return SDValue(); 11980 11981 case Intrinsic::arm_neon_vrshifts: 11982 case Intrinsic::arm_neon_vrshiftu: 11983 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 11984 break; 11985 return SDValue(); 11986 11987 case Intrinsic::arm_neon_vqshifts: 11988 case Intrinsic::arm_neon_vqshiftu: 11989 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 11990 break; 11991 return SDValue(); 11992 11993 case Intrinsic::arm_neon_vqshiftsu: 11994 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 11995 break; 11996 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 11997 11998 case Intrinsic::arm_neon_vrshiftn: 11999 case Intrinsic::arm_neon_vqshiftns: 12000 case Intrinsic::arm_neon_vqshiftnu: 12001 case Intrinsic::arm_neon_vqshiftnsu: 12002 case Intrinsic::arm_neon_vqrshiftns: 12003 case Intrinsic::arm_neon_vqrshiftnu: 12004 case Intrinsic::arm_neon_vqrshiftnsu: 12005 // Narrowing shifts require an immediate right shift. 12006 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 12007 break; 12008 llvm_unreachable("invalid shift count for narrowing vector shift " 12009 "intrinsic"); 12010 12011 default: 12012 llvm_unreachable("unhandled vector shift"); 12013 } 12014 12015 switch (IntNo) { 12016 case Intrinsic::arm_neon_vshifts: 12017 case Intrinsic::arm_neon_vshiftu: 12018 // Opcode already set above. 12019 break; 12020 case Intrinsic::arm_neon_vrshifts: 12021 VShiftOpc = ARMISD::VRSHRs; break; 12022 case Intrinsic::arm_neon_vrshiftu: 12023 VShiftOpc = ARMISD::VRSHRu; break; 12024 case Intrinsic::arm_neon_vrshiftn: 12025 VShiftOpc = ARMISD::VRSHRN; break; 12026 case Intrinsic::arm_neon_vqshifts: 12027 VShiftOpc = ARMISD::VQSHLs; break; 12028 case Intrinsic::arm_neon_vqshiftu: 12029 VShiftOpc = ARMISD::VQSHLu; break; 12030 case Intrinsic::arm_neon_vqshiftsu: 12031 VShiftOpc = ARMISD::VQSHLsu; break; 12032 case Intrinsic::arm_neon_vqshiftns: 12033 VShiftOpc = ARMISD::VQSHRNs; break; 12034 case Intrinsic::arm_neon_vqshiftnu: 12035 VShiftOpc = ARMISD::VQSHRNu; break; 12036 case Intrinsic::arm_neon_vqshiftnsu: 12037 VShiftOpc = ARMISD::VQSHRNsu; break; 12038 case Intrinsic::arm_neon_vqrshiftns: 12039 VShiftOpc = ARMISD::VQRSHRNs; break; 12040 case Intrinsic::arm_neon_vqrshiftnu: 12041 VShiftOpc = ARMISD::VQRSHRNu; break; 12042 case Intrinsic::arm_neon_vqrshiftnsu: 12043 VShiftOpc = ARMISD::VQRSHRNsu; break; 12044 } 12045 12046 SDLoc dl(N); 12047 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 12048 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32)); 12049 } 12050 12051 case Intrinsic::arm_neon_vshiftins: { 12052 EVT VT = N->getOperand(1).getValueType(); 12053 int64_t Cnt; 12054 unsigned VShiftOpc = 0; 12055 12056 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 12057 VShiftOpc = ARMISD::VSLI; 12058 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 12059 VShiftOpc = ARMISD::VSRI; 12060 else { 12061 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 12062 } 12063 12064 SDLoc dl(N); 12065 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 12066 N->getOperand(1), N->getOperand(2), 12067 DAG.getConstant(Cnt, dl, MVT::i32)); 12068 } 12069 12070 case Intrinsic::arm_neon_vqrshifts: 12071 case Intrinsic::arm_neon_vqrshiftu: 12072 // No immediate versions of these to check for. 12073 break; 12074 } 12075 12076 return SDValue(); 12077 } 12078 12079 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 12080 /// lowers them. As with the vector shift intrinsics, this is done during DAG 12081 /// combining instead of DAG legalizing because the build_vectors for 64-bit 12082 /// vector element shift counts are generally not legal, and it is hard to see 12083 /// their values after they get legalized to loads from a constant pool. 12084 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 12085 const ARMSubtarget *ST) { 12086 EVT VT = N->getValueType(0); 12087 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 12088 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 12089 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 12090 SDValue N1 = N->getOperand(1); 12091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 12092 SDValue N0 = N->getOperand(0); 12093 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 12094 DAG.MaskedValueIsZero(N0.getOperand(0), 12095 APInt::getHighBitsSet(32, 16))) 12096 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 12097 } 12098 } 12099 12100 // Nothing to be done for scalar shifts. 12101 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12102 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 12103 return SDValue(); 12104 12105 assert(ST->hasNEON() && "unexpected vector shift"); 12106 int64_t Cnt; 12107 12108 switch (N->getOpcode()) { 12109 default: llvm_unreachable("unexpected shift opcode"); 12110 12111 case ISD::SHL: 12112 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) { 12113 SDLoc dl(N); 12114 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0), 12115 DAG.getConstant(Cnt, dl, MVT::i32)); 12116 } 12117 break; 12118 12119 case ISD::SRA: 12120 case ISD::SRL: 12121 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 12122 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 12123 ARMISD::VSHRs : ARMISD::VSHRu); 12124 SDLoc dl(N); 12125 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), 12126 DAG.getConstant(Cnt, dl, MVT::i32)); 12127 } 12128 } 12129 return SDValue(); 12130 } 12131 12132 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 12133 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 12134 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 12135 const ARMSubtarget *ST) { 12136 SDValue N0 = N->getOperand(0); 12137 12138 // Check for sign- and zero-extensions of vector extract operations of 8- 12139 // and 16-bit vector elements. NEON supports these directly. They are 12140 // handled during DAG combining because type legalization will promote them 12141 // to 32-bit types and it is messy to recognize the operations after that. 12142 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 12143 SDValue Vec = N0.getOperand(0); 12144 SDValue Lane = N0.getOperand(1); 12145 EVT VT = N->getValueType(0); 12146 EVT EltVT = N0.getValueType(); 12147 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12148 12149 if (VT == MVT::i32 && 12150 (EltVT == MVT::i8 || EltVT == MVT::i16) && 12151 TLI.isTypeLegal(Vec.getValueType()) && 12152 isa<ConstantSDNode>(Lane)) { 12153 12154 unsigned Opc = 0; 12155 switch (N->getOpcode()) { 12156 default: llvm_unreachable("unexpected opcode"); 12157 case ISD::SIGN_EXTEND: 12158 Opc = ARMISD::VGETLANEs; 12159 break; 12160 case ISD::ZERO_EXTEND: 12161 case ISD::ANY_EXTEND: 12162 Opc = ARMISD::VGETLANEu; 12163 break; 12164 } 12165 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 12166 } 12167 } 12168 12169 return SDValue(); 12170 } 12171 12172 static const APInt *isPowerOf2Constant(SDValue V) { 12173 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V); 12174 if (!C) 12175 return nullptr; 12176 const APInt *CV = &C->getAPIntValue(); 12177 return CV->isPowerOf2() ? CV : nullptr; 12178 } 12179 12180 SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const { 12181 // If we have a CMOV, OR and AND combination such as: 12182 // if (x & CN) 12183 // y |= CM; 12184 // 12185 // And: 12186 // * CN is a single bit; 12187 // * All bits covered by CM are known zero in y 12188 // 12189 // Then we can convert this into a sequence of BFI instructions. This will 12190 // always be a win if CM is a single bit, will always be no worse than the 12191 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is 12192 // three bits (due to the extra IT instruction). 12193 12194 SDValue Op0 = CMOV->getOperand(0); 12195 SDValue Op1 = CMOV->getOperand(1); 12196 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2)); 12197 auto CC = CCNode->getAPIntValue().getLimitedValue(); 12198 SDValue CmpZ = CMOV->getOperand(4); 12199 12200 // The compare must be against zero. 12201 if (!isNullConstant(CmpZ->getOperand(1))) 12202 return SDValue(); 12203 12204 assert(CmpZ->getOpcode() == ARMISD::CMPZ); 12205 SDValue And = CmpZ->getOperand(0); 12206 if (And->getOpcode() != ISD::AND) 12207 return SDValue(); 12208 const APInt *AndC = isPowerOf2Constant(And->getOperand(1)); 12209 if (!AndC) 12210 return SDValue(); 12211 SDValue X = And->getOperand(0); 12212 12213 if (CC == ARMCC::EQ) { 12214 // We're performing an "equal to zero" compare. Swap the operands so we 12215 // canonicalize on a "not equal to zero" compare. 12216 std::swap(Op0, Op1); 12217 } else { 12218 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?"); 12219 } 12220 12221 if (Op1->getOpcode() != ISD::OR) 12222 return SDValue(); 12223 12224 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1)); 12225 if (!OrC) 12226 return SDValue(); 12227 SDValue Y = Op1->getOperand(0); 12228 12229 if (Op0 != Y) 12230 return SDValue(); 12231 12232 // Now, is it profitable to continue? 12233 APInt OrCI = OrC->getAPIntValue(); 12234 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2; 12235 if (OrCI.countPopulation() > Heuristic) 12236 return SDValue(); 12237 12238 // Lastly, can we determine that the bits defined by OrCI 12239 // are zero in Y? 12240 KnownBits Known; 12241 DAG.computeKnownBits(Y, Known); 12242 if ((OrCI & Known.Zero) != OrCI) 12243 return SDValue(); 12244 12245 // OK, we can do the combine. 12246 SDValue V = Y; 12247 SDLoc dl(X); 12248 EVT VT = X.getValueType(); 12249 unsigned BitInX = AndC->logBase2(); 12250 12251 if (BitInX != 0) { 12252 // We must shift X first. 12253 X = DAG.getNode(ISD::SRL, dl, VT, X, 12254 DAG.getConstant(BitInX, dl, VT)); 12255 } 12256 12257 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits(); 12258 BitInY < NumActiveBits; ++BitInY) { 12259 if (OrCI[BitInY] == 0) 12260 continue; 12261 APInt Mask(VT.getSizeInBits(), 0); 12262 Mask.setBit(BitInY); 12263 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X, 12264 // Confusingly, the operand is an *inverted* mask. 12265 DAG.getConstant(~Mask, dl, VT)); 12266 } 12267 12268 return V; 12269 } 12270 12271 /// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND. 12272 SDValue 12273 ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const { 12274 SDValue Cmp = N->getOperand(4); 12275 if (Cmp.getOpcode() != ARMISD::CMPZ) 12276 // Only looking at NE cases. 12277 return SDValue(); 12278 12279 EVT VT = N->getValueType(0); 12280 SDLoc dl(N); 12281 SDValue LHS = Cmp.getOperand(0); 12282 SDValue RHS = Cmp.getOperand(1); 12283 SDValue Chain = N->getOperand(0); 12284 SDValue BB = N->getOperand(1); 12285 SDValue ARMcc = N->getOperand(2); 12286 ARMCC::CondCodes CC = 12287 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 12288 12289 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0)) 12290 // -> (brcond Chain BB CC CPSR Cmp) 12291 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() && 12292 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV && 12293 LHS->getOperand(0)->hasOneUse()) { 12294 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0)); 12295 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1)); 12296 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 12297 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 12298 if ((LHS00C && LHS00C->getZExtValue() == 0) && 12299 (LHS01C && LHS01C->getZExtValue() == 1) && 12300 (LHS1C && LHS1C->getZExtValue() == 1) && 12301 (RHSC && RHSC->getZExtValue() == 0)) { 12302 return DAG.getNode( 12303 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2), 12304 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4)); 12305 } 12306 } 12307 12308 return SDValue(); 12309 } 12310 12311 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 12312 SDValue 12313 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 12314 SDValue Cmp = N->getOperand(4); 12315 if (Cmp.getOpcode() != ARMISD::CMPZ) 12316 // Only looking at EQ and NE cases. 12317 return SDValue(); 12318 12319 EVT VT = N->getValueType(0); 12320 SDLoc dl(N); 12321 SDValue LHS = Cmp.getOperand(0); 12322 SDValue RHS = Cmp.getOperand(1); 12323 SDValue FalseVal = N->getOperand(0); 12324 SDValue TrueVal = N->getOperand(1); 12325 SDValue ARMcc = N->getOperand(2); 12326 ARMCC::CondCodes CC = 12327 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 12328 12329 // BFI is only available on V6T2+. 12330 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) { 12331 SDValue R = PerformCMOVToBFICombine(N, DAG); 12332 if (R) 12333 return R; 12334 } 12335 12336 // Simplify 12337 // mov r1, r0 12338 // cmp r1, x 12339 // mov r0, y 12340 // moveq r0, x 12341 // to 12342 // cmp r0, x 12343 // movne r0, y 12344 // 12345 // mov r1, r0 12346 // cmp r1, x 12347 // mov r0, x 12348 // movne r0, y 12349 // to 12350 // cmp r0, x 12351 // movne r0, y 12352 /// FIXME: Turn this into a target neutral optimization? 12353 SDValue Res; 12354 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 12355 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 12356 N->getOperand(3), Cmp); 12357 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 12358 SDValue ARMcc; 12359 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 12360 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 12361 N->getOperand(3), NewCmp); 12362 } 12363 12364 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0)) 12365 // -> (cmov F T CC CPSR Cmp) 12366 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) { 12367 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)); 12368 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 12369 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 12370 if ((LHS0C && LHS0C->getZExtValue() == 0) && 12371 (LHS1C && LHS1C->getZExtValue() == 1) && 12372 (RHSC && RHSC->getZExtValue() == 0)) { 12373 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 12374 LHS->getOperand(2), LHS->getOperand(3), 12375 LHS->getOperand(4)); 12376 } 12377 } 12378 12379 if (Res.getNode()) { 12380 KnownBits Known; 12381 DAG.computeKnownBits(SDValue(N,0), Known); 12382 // Capture demanded bits information that would be otherwise lost. 12383 if (Known.Zero == 0xfffffffe) 12384 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 12385 DAG.getValueType(MVT::i1)); 12386 else if (Known.Zero == 0xffffff00) 12387 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 12388 DAG.getValueType(MVT::i8)); 12389 else if (Known.Zero == 0xffff0000) 12390 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 12391 DAG.getValueType(MVT::i16)); 12392 } 12393 12394 return Res; 12395 } 12396 12397 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 12398 DAGCombinerInfo &DCI) const { 12399 switch (N->getOpcode()) { 12400 default: break; 12401 case ARMISD::ADDE: return PerformADDECombine(N, DCI, Subtarget); 12402 case ARMISD::UMLAL: return PerformUMLALCombine(N, DCI.DAG, Subtarget); 12403 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 12404 case ISD::SUB: return PerformSUBCombine(N, DCI); 12405 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 12406 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 12407 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 12408 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 12409 case ARMISD::ADDC: 12410 case ARMISD::SUBC: return PerformAddcSubcCombine(N, DCI, Subtarget); 12411 case ARMISD::SUBE: return PerformAddeSubeCombine(N, DCI, Subtarget); 12412 case ARMISD::BFI: return PerformBFICombine(N, DCI); 12413 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget); 12414 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 12415 case ISD::STORE: return PerformSTORECombine(N, DCI); 12416 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget); 12417 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 12418 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 12419 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 12420 case ARMISD::VDUP: return PerformVDUPCombine(N, DCI); 12421 case ISD::FP_TO_SINT: 12422 case ISD::FP_TO_UINT: 12423 return PerformVCVTCombine(N, DCI.DAG, Subtarget); 12424 case ISD::FDIV: 12425 return PerformVDIVCombine(N, DCI.DAG, Subtarget); 12426 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 12427 case ISD::SHL: 12428 case ISD::SRA: 12429 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 12430 case ISD::SIGN_EXTEND: 12431 case ISD::ZERO_EXTEND: 12432 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 12433 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 12434 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG); 12435 case ISD::LOAD: return PerformLOADCombine(N, DCI); 12436 case ARMISD::VLD1DUP: 12437 case ARMISD::VLD2DUP: 12438 case ARMISD::VLD3DUP: 12439 case ARMISD::VLD4DUP: 12440 return PerformVLDCombine(N, DCI); 12441 case ARMISD::BUILD_VECTOR: 12442 return PerformARMBUILD_VECTORCombine(N, DCI); 12443 case ARMISD::SMULWB: { 12444 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12445 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16); 12446 if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)) 12447 return SDValue(); 12448 break; 12449 } 12450 case ARMISD::SMULWT: { 12451 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12452 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16); 12453 if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)) 12454 return SDValue(); 12455 break; 12456 } 12457 case ARMISD::SMLALBB: { 12458 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12459 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16); 12460 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 12461 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 12462 return SDValue(); 12463 break; 12464 } 12465 case ARMISD::SMLALBT: { 12466 unsigned LowWidth = N->getOperand(0).getValueType().getSizeInBits(); 12467 APInt LowMask = APInt::getLowBitsSet(LowWidth, 16); 12468 unsigned HighWidth = N->getOperand(1).getValueType().getSizeInBits(); 12469 APInt HighMask = APInt::getHighBitsSet(HighWidth, 16); 12470 if ((SimplifyDemandedBits(N->getOperand(0), LowMask, DCI)) || 12471 (SimplifyDemandedBits(N->getOperand(1), HighMask, DCI))) 12472 return SDValue(); 12473 break; 12474 } 12475 case ARMISD::SMLALTB: { 12476 unsigned HighWidth = N->getOperand(0).getValueType().getSizeInBits(); 12477 APInt HighMask = APInt::getHighBitsSet(HighWidth, 16); 12478 unsigned LowWidth = N->getOperand(1).getValueType().getSizeInBits(); 12479 APInt LowMask = APInt::getLowBitsSet(LowWidth, 16); 12480 if ((SimplifyDemandedBits(N->getOperand(0), HighMask, DCI)) || 12481 (SimplifyDemandedBits(N->getOperand(1), LowMask, DCI))) 12482 return SDValue(); 12483 break; 12484 } 12485 case ARMISD::SMLALTT: { 12486 unsigned BitWidth = N->getValueType(0).getSizeInBits(); 12487 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16); 12488 if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) || 12489 (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))) 12490 return SDValue(); 12491 break; 12492 } 12493 case ISD::INTRINSIC_VOID: 12494 case ISD::INTRINSIC_W_CHAIN: 12495 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 12496 case Intrinsic::arm_neon_vld1: 12497 case Intrinsic::arm_neon_vld2: 12498 case Intrinsic::arm_neon_vld3: 12499 case Intrinsic::arm_neon_vld4: 12500 case Intrinsic::arm_neon_vld2lane: 12501 case Intrinsic::arm_neon_vld3lane: 12502 case Intrinsic::arm_neon_vld4lane: 12503 case Intrinsic::arm_neon_vst1: 12504 case Intrinsic::arm_neon_vst2: 12505 case Intrinsic::arm_neon_vst3: 12506 case Intrinsic::arm_neon_vst4: 12507 case Intrinsic::arm_neon_vst2lane: 12508 case Intrinsic::arm_neon_vst3lane: 12509 case Intrinsic::arm_neon_vst4lane: 12510 return PerformVLDCombine(N, DCI); 12511 default: break; 12512 } 12513 break; 12514 } 12515 return SDValue(); 12516 } 12517 12518 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 12519 EVT VT) const { 12520 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 12521 } 12522 12523 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 12524 unsigned, 12525 unsigned, 12526 bool *Fast) const { 12527 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus 12528 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 12529 12530 switch (VT.getSimpleVT().SimpleTy) { 12531 default: 12532 return false; 12533 case MVT::i8: 12534 case MVT::i16: 12535 case MVT::i32: { 12536 // Unaligned access can use (for example) LRDB, LRDH, LDR 12537 if (AllowsUnaligned) { 12538 if (Fast) 12539 *Fast = Subtarget->hasV7Ops(); 12540 return true; 12541 } 12542 return false; 12543 } 12544 case MVT::f64: 12545 case MVT::v2f64: { 12546 // For any little-endian targets with neon, we can support unaligned ld/st 12547 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 12548 // A big-endian target may also explicitly support unaligned accesses 12549 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) { 12550 if (Fast) 12551 *Fast = true; 12552 return true; 12553 } 12554 return false; 12555 } 12556 } 12557 } 12558 12559 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 12560 unsigned AlignCheck) { 12561 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 12562 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 12563 } 12564 12565 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 12566 unsigned DstAlign, unsigned SrcAlign, 12567 bool IsMemset, bool ZeroMemset, 12568 bool MemcpyStrSrc, 12569 MachineFunction &MF) const { 12570 const Function &F = MF.getFunction(); 12571 12572 // See if we can use NEON instructions for this... 12573 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() && 12574 !F.hasFnAttribute(Attribute::NoImplicitFloat)) { 12575 bool Fast; 12576 if (Size >= 16 && 12577 (memOpAlign(SrcAlign, DstAlign, 16) || 12578 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) { 12579 return MVT::v2f64; 12580 } else if (Size >= 8 && 12581 (memOpAlign(SrcAlign, DstAlign, 8) || 12582 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) && 12583 Fast))) { 12584 return MVT::f64; 12585 } 12586 } 12587 12588 // Let the target-independent logic figure it out. 12589 return MVT::Other; 12590 } 12591 12592 // 64-bit integers are split into their high and low parts and held in two 12593 // different registers, so the trunc is free since the low register can just 12594 // be used. 12595 bool ARMTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const { 12596 if (!SrcTy->isIntegerTy() || !DstTy->isIntegerTy()) 12597 return false; 12598 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); 12599 unsigned DestBits = DstTy->getPrimitiveSizeInBits(); 12600 return (SrcBits == 64 && DestBits == 32); 12601 } 12602 12603 bool ARMTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const { 12604 if (SrcVT.isVector() || DstVT.isVector() || !SrcVT.isInteger() || 12605 !DstVT.isInteger()) 12606 return false; 12607 unsigned SrcBits = SrcVT.getSizeInBits(); 12608 unsigned DestBits = DstVT.getSizeInBits(); 12609 return (SrcBits == 64 && DestBits == 32); 12610 } 12611 12612 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 12613 if (Val.getOpcode() != ISD::LOAD) 12614 return false; 12615 12616 EVT VT1 = Val.getValueType(); 12617 if (!VT1.isSimple() || !VT1.isInteger() || 12618 !VT2.isSimple() || !VT2.isInteger()) 12619 return false; 12620 12621 switch (VT1.getSimpleVT().SimpleTy) { 12622 default: break; 12623 case MVT::i1: 12624 case MVT::i8: 12625 case MVT::i16: 12626 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 12627 return true; 12628 } 12629 12630 return false; 12631 } 12632 12633 bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 12634 EVT VT = ExtVal.getValueType(); 12635 12636 if (!isTypeLegal(VT)) 12637 return false; 12638 12639 // Don't create a loadext if we can fold the extension into a wide/long 12640 // instruction. 12641 // If there's more than one user instruction, the loadext is desirable no 12642 // matter what. There can be two uses by the same instruction. 12643 if (ExtVal->use_empty() || 12644 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode())) 12645 return true; 12646 12647 SDNode *U = *ExtVal->use_begin(); 12648 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB || 12649 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL)) 12650 return false; 12651 12652 return true; 12653 } 12654 12655 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 12656 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 12657 return false; 12658 12659 if (!isTypeLegal(EVT::getEVT(Ty1))) 12660 return false; 12661 12662 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 12663 12664 // Assuming the caller doesn't have a zeroext or signext return parameter, 12665 // truncation all the way down to i1 is valid. 12666 return true; 12667 } 12668 12669 int ARMTargetLowering::getScalingFactorCost(const DataLayout &DL, 12670 const AddrMode &AM, Type *Ty, 12671 unsigned AS) const { 12672 if (isLegalAddressingMode(DL, AM, Ty, AS)) { 12673 if (Subtarget->hasFPAO()) 12674 return AM.Scale < 0 ? 1 : 0; // positive offsets execute faster 12675 return 0; 12676 } 12677 return -1; 12678 } 12679 12680 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 12681 if (V < 0) 12682 return false; 12683 12684 unsigned Scale = 1; 12685 switch (VT.getSimpleVT().SimpleTy) { 12686 default: return false; 12687 case MVT::i1: 12688 case MVT::i8: 12689 // Scale == 1; 12690 break; 12691 case MVT::i16: 12692 // Scale == 2; 12693 Scale = 2; 12694 break; 12695 case MVT::i32: 12696 // Scale == 4; 12697 Scale = 4; 12698 break; 12699 } 12700 12701 if ((V & (Scale - 1)) != 0) 12702 return false; 12703 V /= Scale; 12704 return V == (V & ((1LL << 5) - 1)); 12705 } 12706 12707 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 12708 const ARMSubtarget *Subtarget) { 12709 bool isNeg = false; 12710 if (V < 0) { 12711 isNeg = true; 12712 V = - V; 12713 } 12714 12715 switch (VT.getSimpleVT().SimpleTy) { 12716 default: return false; 12717 case MVT::i1: 12718 case MVT::i8: 12719 case MVT::i16: 12720 case MVT::i32: 12721 // + imm12 or - imm8 12722 if (isNeg) 12723 return V == (V & ((1LL << 8) - 1)); 12724 return V == (V & ((1LL << 12) - 1)); 12725 case MVT::f32: 12726 case MVT::f64: 12727 // Same as ARM mode. FIXME: NEON? 12728 if (!Subtarget->hasVFP2()) 12729 return false; 12730 if ((V & 3) != 0) 12731 return false; 12732 V >>= 2; 12733 return V == (V & ((1LL << 8) - 1)); 12734 } 12735 } 12736 12737 /// isLegalAddressImmediate - Return true if the integer value can be used 12738 /// as the offset of the target addressing mode for load / store of the 12739 /// given type. 12740 static bool isLegalAddressImmediate(int64_t V, EVT VT, 12741 const ARMSubtarget *Subtarget) { 12742 if (V == 0) 12743 return true; 12744 12745 if (!VT.isSimple()) 12746 return false; 12747 12748 if (Subtarget->isThumb1Only()) 12749 return isLegalT1AddressImmediate(V, VT); 12750 else if (Subtarget->isThumb2()) 12751 return isLegalT2AddressImmediate(V, VT, Subtarget); 12752 12753 // ARM mode. 12754 if (V < 0) 12755 V = - V; 12756 switch (VT.getSimpleVT().SimpleTy) { 12757 default: return false; 12758 case MVT::i1: 12759 case MVT::i8: 12760 case MVT::i32: 12761 // +- imm12 12762 return V == (V & ((1LL << 12) - 1)); 12763 case MVT::i16: 12764 // +- imm8 12765 return V == (V & ((1LL << 8) - 1)); 12766 case MVT::f32: 12767 case MVT::f64: 12768 if (!Subtarget->hasVFP2()) // FIXME: NEON? 12769 return false; 12770 if ((V & 3) != 0) 12771 return false; 12772 V >>= 2; 12773 return V == (V & ((1LL << 8) - 1)); 12774 } 12775 } 12776 12777 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 12778 EVT VT) const { 12779 int Scale = AM.Scale; 12780 if (Scale < 0) 12781 return false; 12782 12783 switch (VT.getSimpleVT().SimpleTy) { 12784 default: return false; 12785 case MVT::i1: 12786 case MVT::i8: 12787 case MVT::i16: 12788 case MVT::i32: 12789 if (Scale == 1) 12790 return true; 12791 // r + r << imm 12792 Scale = Scale & ~1; 12793 return Scale == 2 || Scale == 4 || Scale == 8; 12794 case MVT::i64: 12795 // FIXME: What are we trying to model here? ldrd doesn't have an r + r 12796 // version in Thumb mode. 12797 // r + r 12798 if (Scale == 1) 12799 return true; 12800 // r * 2 (this can be lowered to r + r). 12801 if (!AM.HasBaseReg && Scale == 2) 12802 return true; 12803 return false; 12804 case MVT::isVoid: 12805 // Note, we allow "void" uses (basically, uses that aren't loads or 12806 // stores), because arm allows folding a scale into many arithmetic 12807 // operations. This should be made more precise and revisited later. 12808 12809 // Allow r << imm, but the imm has to be a multiple of two. 12810 if (Scale & 1) return false; 12811 return isPowerOf2_32(Scale); 12812 } 12813 } 12814 12815 bool ARMTargetLowering::isLegalT1ScaledAddressingMode(const AddrMode &AM, 12816 EVT VT) const { 12817 const int Scale = AM.Scale; 12818 12819 // Negative scales are not supported in Thumb1. 12820 if (Scale < 0) 12821 return false; 12822 12823 // Thumb1 addressing modes do not support register scaling excepting the 12824 // following cases: 12825 // 1. Scale == 1 means no scaling. 12826 // 2. Scale == 2 this can be lowered to r + r if there is no base register. 12827 return (Scale == 1) || (!AM.HasBaseReg && Scale == 2); 12828 } 12829 12830 /// isLegalAddressingMode - Return true if the addressing mode represented 12831 /// by AM is legal for this target, for a load/store of the specified type. 12832 bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL, 12833 const AddrMode &AM, Type *Ty, 12834 unsigned AS, Instruction *I) const { 12835 EVT VT = getValueType(DL, Ty, true); 12836 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 12837 return false; 12838 12839 // Can never fold addr of global into load/store. 12840 if (AM.BaseGV) 12841 return false; 12842 12843 switch (AM.Scale) { 12844 case 0: // no scale reg, must be "r+i" or "r", or "i". 12845 break; 12846 default: 12847 // ARM doesn't support any R+R*scale+imm addr modes. 12848 if (AM.BaseOffs) 12849 return false; 12850 12851 if (!VT.isSimple()) 12852 return false; 12853 12854 if (Subtarget->isThumb1Only()) 12855 return isLegalT1ScaledAddressingMode(AM, VT); 12856 12857 if (Subtarget->isThumb2()) 12858 return isLegalT2ScaledAddressingMode(AM, VT); 12859 12860 int Scale = AM.Scale; 12861 switch (VT.getSimpleVT().SimpleTy) { 12862 default: return false; 12863 case MVT::i1: 12864 case MVT::i8: 12865 case MVT::i32: 12866 if (Scale < 0) Scale = -Scale; 12867 if (Scale == 1) 12868 return true; 12869 // r + r << imm 12870 return isPowerOf2_32(Scale & ~1); 12871 case MVT::i16: 12872 case MVT::i64: 12873 // r +/- r 12874 if (Scale == 1 || (AM.HasBaseReg && Scale == -1)) 12875 return true; 12876 // r * 2 (this can be lowered to r + r). 12877 if (!AM.HasBaseReg && Scale == 2) 12878 return true; 12879 return false; 12880 12881 case MVT::isVoid: 12882 // Note, we allow "void" uses (basically, uses that aren't loads or 12883 // stores), because arm allows folding a scale into many arithmetic 12884 // operations. This should be made more precise and revisited later. 12885 12886 // Allow r << imm, but the imm has to be a multiple of two. 12887 if (Scale & 1) return false; 12888 return isPowerOf2_32(Scale); 12889 } 12890 } 12891 return true; 12892 } 12893 12894 /// isLegalICmpImmediate - Return true if the specified immediate is legal 12895 /// icmp immediate, that is the target has icmp instructions which can compare 12896 /// a register against the immediate without having to materialize the 12897 /// immediate into a register. 12898 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 12899 // Thumb2 and ARM modes can use cmn for negative immediates. 12900 if (!Subtarget->isThumb()) 12901 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1; 12902 if (Subtarget->isThumb2()) 12903 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1; 12904 // Thumb1 doesn't have cmn, and only 8-bit immediates. 12905 return Imm >= 0 && Imm <= 255; 12906 } 12907 12908 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 12909 /// *or sub* immediate, that is the target has add or sub instructions which can 12910 /// add a register with the immediate without having to materialize the 12911 /// immediate into a register. 12912 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 12913 // Same encoding for add/sub, just flip the sign. 12914 int64_t AbsImm = std::abs(Imm); 12915 if (!Subtarget->isThumb()) 12916 return ARM_AM::getSOImmVal(AbsImm) != -1; 12917 if (Subtarget->isThumb2()) 12918 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 12919 // Thumb1 only has 8-bit unsigned immediate. 12920 return AbsImm >= 0 && AbsImm <= 255; 12921 } 12922 12923 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 12924 bool isSEXTLoad, SDValue &Base, 12925 SDValue &Offset, bool &isInc, 12926 SelectionDAG &DAG) { 12927 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 12928 return false; 12929 12930 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 12931 // AddressingMode 3 12932 Base = Ptr->getOperand(0); 12933 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 12934 int RHSC = (int)RHS->getZExtValue(); 12935 if (RHSC < 0 && RHSC > -256) { 12936 assert(Ptr->getOpcode() == ISD::ADD); 12937 isInc = false; 12938 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 12939 return true; 12940 } 12941 } 12942 isInc = (Ptr->getOpcode() == ISD::ADD); 12943 Offset = Ptr->getOperand(1); 12944 return true; 12945 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 12946 // AddressingMode 2 12947 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 12948 int RHSC = (int)RHS->getZExtValue(); 12949 if (RHSC < 0 && RHSC > -0x1000) { 12950 assert(Ptr->getOpcode() == ISD::ADD); 12951 isInc = false; 12952 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 12953 Base = Ptr->getOperand(0); 12954 return true; 12955 } 12956 } 12957 12958 if (Ptr->getOpcode() == ISD::ADD) { 12959 isInc = true; 12960 ARM_AM::ShiftOpc ShOpcVal= 12961 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 12962 if (ShOpcVal != ARM_AM::no_shift) { 12963 Base = Ptr->getOperand(1); 12964 Offset = Ptr->getOperand(0); 12965 } else { 12966 Base = Ptr->getOperand(0); 12967 Offset = Ptr->getOperand(1); 12968 } 12969 return true; 12970 } 12971 12972 isInc = (Ptr->getOpcode() == ISD::ADD); 12973 Base = Ptr->getOperand(0); 12974 Offset = Ptr->getOperand(1); 12975 return true; 12976 } 12977 12978 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 12979 return false; 12980 } 12981 12982 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 12983 bool isSEXTLoad, SDValue &Base, 12984 SDValue &Offset, bool &isInc, 12985 SelectionDAG &DAG) { 12986 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 12987 return false; 12988 12989 Base = Ptr->getOperand(0); 12990 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 12991 int RHSC = (int)RHS->getZExtValue(); 12992 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 12993 assert(Ptr->getOpcode() == ISD::ADD); 12994 isInc = false; 12995 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 12996 return true; 12997 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 12998 isInc = Ptr->getOpcode() == ISD::ADD; 12999 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0)); 13000 return true; 13001 } 13002 } 13003 13004 return false; 13005 } 13006 13007 /// getPreIndexedAddressParts - returns true by value, base pointer and 13008 /// offset pointer and addressing mode by reference if the node's address 13009 /// can be legally represented as pre-indexed load / store address. 13010 bool 13011 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 13012 SDValue &Offset, 13013 ISD::MemIndexedMode &AM, 13014 SelectionDAG &DAG) const { 13015 if (Subtarget->isThumb1Only()) 13016 return false; 13017 13018 EVT VT; 13019 SDValue Ptr; 13020 bool isSEXTLoad = false; 13021 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 13022 Ptr = LD->getBasePtr(); 13023 VT = LD->getMemoryVT(); 13024 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 13025 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 13026 Ptr = ST->getBasePtr(); 13027 VT = ST->getMemoryVT(); 13028 } else 13029 return false; 13030 13031 bool isInc; 13032 bool isLegal = false; 13033 if (Subtarget->isThumb2()) 13034 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 13035 Offset, isInc, DAG); 13036 else 13037 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 13038 Offset, isInc, DAG); 13039 if (!isLegal) 13040 return false; 13041 13042 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 13043 return true; 13044 } 13045 13046 /// getPostIndexedAddressParts - returns true by value, base pointer and 13047 /// offset pointer and addressing mode by reference if this node can be 13048 /// combined with a load / store to form a post-indexed load / store. 13049 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 13050 SDValue &Base, 13051 SDValue &Offset, 13052 ISD::MemIndexedMode &AM, 13053 SelectionDAG &DAG) const { 13054 EVT VT; 13055 SDValue Ptr; 13056 bool isSEXTLoad = false, isNonExt; 13057 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 13058 VT = LD->getMemoryVT(); 13059 Ptr = LD->getBasePtr(); 13060 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 13061 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD; 13062 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 13063 VT = ST->getMemoryVT(); 13064 Ptr = ST->getBasePtr(); 13065 isNonExt = !ST->isTruncatingStore(); 13066 } else 13067 return false; 13068 13069 if (Subtarget->isThumb1Only()) { 13070 // Thumb-1 can do a limited post-inc load or store as an updating LDM. It 13071 // must be non-extending/truncating, i32, with an offset of 4. 13072 assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!"); 13073 if (Op->getOpcode() != ISD::ADD || !isNonExt) 13074 return false; 13075 auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1)); 13076 if (!RHS || RHS->getZExtValue() != 4) 13077 return false; 13078 13079 Offset = Op->getOperand(1); 13080 Base = Op->getOperand(0); 13081 AM = ISD::POST_INC; 13082 return true; 13083 } 13084 13085 bool isInc; 13086 bool isLegal = false; 13087 if (Subtarget->isThumb2()) 13088 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 13089 isInc, DAG); 13090 else 13091 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 13092 isInc, DAG); 13093 if (!isLegal) 13094 return false; 13095 13096 if (Ptr != Base) { 13097 // Swap base ptr and offset to catch more post-index load / store when 13098 // it's legal. In Thumb2 mode, offset must be an immediate. 13099 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 13100 !Subtarget->isThumb2()) 13101 std::swap(Base, Offset); 13102 13103 // Post-indexed load / store update the base pointer. 13104 if (Ptr != Base) 13105 return false; 13106 } 13107 13108 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 13109 return true; 13110 } 13111 13112 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 13113 KnownBits &Known, 13114 const APInt &DemandedElts, 13115 const SelectionDAG &DAG, 13116 unsigned Depth) const { 13117 unsigned BitWidth = Known.getBitWidth(); 13118 Known.resetAll(); 13119 switch (Op.getOpcode()) { 13120 default: break; 13121 case ARMISD::ADDC: 13122 case ARMISD::ADDE: 13123 case ARMISD::SUBC: 13124 case ARMISD::SUBE: 13125 // Special cases when we convert a carry to a boolean. 13126 if (Op.getResNo() == 0) { 13127 SDValue LHS = Op.getOperand(0); 13128 SDValue RHS = Op.getOperand(1); 13129 // (ADDE 0, 0, C) will give us a single bit. 13130 if (Op->getOpcode() == ARMISD::ADDE && isNullConstant(LHS) && 13131 isNullConstant(RHS)) { 13132 Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 13133 return; 13134 } 13135 } 13136 break; 13137 case ARMISD::CMOV: { 13138 // Bits are known zero/one if known on the LHS and RHS. 13139 DAG.computeKnownBits(Op.getOperand(0), Known, Depth+1); 13140 if (Known.isUnknown()) 13141 return; 13142 13143 KnownBits KnownRHS; 13144 DAG.computeKnownBits(Op.getOperand(1), KnownRHS, Depth+1); 13145 Known.Zero &= KnownRHS.Zero; 13146 Known.One &= KnownRHS.One; 13147 return; 13148 } 13149 case ISD::INTRINSIC_W_CHAIN: { 13150 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 13151 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 13152 switch (IntID) { 13153 default: return; 13154 case Intrinsic::arm_ldaex: 13155 case Intrinsic::arm_ldrex: { 13156 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 13157 unsigned MemBits = VT.getScalarSizeInBits(); 13158 Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 13159 return; 13160 } 13161 } 13162 } 13163 case ARMISD::BFI: { 13164 // Conservatively, we can recurse down the first operand 13165 // and just mask out all affected bits. 13166 DAG.computeKnownBits(Op.getOperand(0), Known, Depth + 1); 13167 13168 // The operand to BFI is already a mask suitable for removing the bits it 13169 // sets. 13170 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2)); 13171 const APInt &Mask = CI->getAPIntValue(); 13172 Known.Zero &= Mask; 13173 Known.One &= Mask; 13174 return; 13175 } 13176 } 13177 } 13178 13179 //===----------------------------------------------------------------------===// 13180 // ARM Inline Assembly Support 13181 //===----------------------------------------------------------------------===// 13182 13183 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 13184 // Looking for "rev" which is V6+. 13185 if (!Subtarget->hasV6Ops()) 13186 return false; 13187 13188 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 13189 std::string AsmStr = IA->getAsmString(); 13190 SmallVector<StringRef, 4> AsmPieces; 13191 SplitString(AsmStr, AsmPieces, ";\n"); 13192 13193 switch (AsmPieces.size()) { 13194 default: return false; 13195 case 1: 13196 AsmStr = AsmPieces[0]; 13197 AsmPieces.clear(); 13198 SplitString(AsmStr, AsmPieces, " \t,"); 13199 13200 // rev $0, $1 13201 if (AsmPieces.size() == 3 && 13202 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 13203 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 13204 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 13205 if (Ty && Ty->getBitWidth() == 32) 13206 return IntrinsicLowering::LowerToByteSwap(CI); 13207 } 13208 break; 13209 } 13210 13211 return false; 13212 } 13213 13214 const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const { 13215 // At this point, we have to lower this constraint to something else, so we 13216 // lower it to an "r" or "w". However, by doing this we will force the result 13217 // to be in register, while the X constraint is much more permissive. 13218 // 13219 // Although we are correct (we are free to emit anything, without 13220 // constraints), we might break use cases that would expect us to be more 13221 // efficient and emit something else. 13222 if (!Subtarget->hasVFP2()) 13223 return "r"; 13224 if (ConstraintVT.isFloatingPoint()) 13225 return "w"; 13226 if (ConstraintVT.isVector() && Subtarget->hasNEON() && 13227 (ConstraintVT.getSizeInBits() == 64 || 13228 ConstraintVT.getSizeInBits() == 128)) 13229 return "w"; 13230 13231 return "r"; 13232 } 13233 13234 /// getConstraintType - Given a constraint letter, return the type of 13235 /// constraint it is for this target. 13236 ARMTargetLowering::ConstraintType 13237 ARMTargetLowering::getConstraintType(StringRef Constraint) const { 13238 if (Constraint.size() == 1) { 13239 switch (Constraint[0]) { 13240 default: break; 13241 case 'l': return C_RegisterClass; 13242 case 'w': return C_RegisterClass; 13243 case 'h': return C_RegisterClass; 13244 case 'x': return C_RegisterClass; 13245 case 't': return C_RegisterClass; 13246 case 'j': return C_Other; // Constant for movw. 13247 // An address with a single base register. Due to the way we 13248 // currently handle addresses it is the same as an 'r' memory constraint. 13249 case 'Q': return C_Memory; 13250 } 13251 } else if (Constraint.size() == 2) { 13252 switch (Constraint[0]) { 13253 default: break; 13254 // All 'U+' constraints are addresses. 13255 case 'U': return C_Memory; 13256 } 13257 } 13258 return TargetLowering::getConstraintType(Constraint); 13259 } 13260 13261 /// Examine constraint type and operand type and determine a weight value. 13262 /// This object must already have been set up with the operand type 13263 /// and the current alternative constraint selected. 13264 TargetLowering::ConstraintWeight 13265 ARMTargetLowering::getSingleConstraintMatchWeight( 13266 AsmOperandInfo &info, const char *constraint) const { 13267 ConstraintWeight weight = CW_Invalid; 13268 Value *CallOperandVal = info.CallOperandVal; 13269 // If we don't have a value, we can't do a match, 13270 // but allow it at the lowest weight. 13271 if (!CallOperandVal) 13272 return CW_Default; 13273 Type *type = CallOperandVal->getType(); 13274 // Look at the constraint type. 13275 switch (*constraint) { 13276 default: 13277 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 13278 break; 13279 case 'l': 13280 if (type->isIntegerTy()) { 13281 if (Subtarget->isThumb()) 13282 weight = CW_SpecificReg; 13283 else 13284 weight = CW_Register; 13285 } 13286 break; 13287 case 'w': 13288 if (type->isFloatingPointTy()) 13289 weight = CW_Register; 13290 break; 13291 } 13292 return weight; 13293 } 13294 13295 using RCPair = std::pair<unsigned, const TargetRegisterClass *>; 13296 13297 RCPair ARMTargetLowering::getRegForInlineAsmConstraint( 13298 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 13299 if (Constraint.size() == 1) { 13300 // GCC ARM Constraint Letters 13301 switch (Constraint[0]) { 13302 case 'l': // Low regs or general regs. 13303 if (Subtarget->isThumb()) 13304 return RCPair(0U, &ARM::tGPRRegClass); 13305 return RCPair(0U, &ARM::GPRRegClass); 13306 case 'h': // High regs or no regs. 13307 if (Subtarget->isThumb()) 13308 return RCPair(0U, &ARM::hGPRRegClass); 13309 break; 13310 case 'r': 13311 if (Subtarget->isThumb1Only()) 13312 return RCPair(0U, &ARM::tGPRRegClass); 13313 return RCPair(0U, &ARM::GPRRegClass); 13314 case 'w': 13315 if (VT == MVT::Other) 13316 break; 13317 if (VT == MVT::f32) 13318 return RCPair(0U, &ARM::SPRRegClass); 13319 if (VT.getSizeInBits() == 64) 13320 return RCPair(0U, &ARM::DPRRegClass); 13321 if (VT.getSizeInBits() == 128) 13322 return RCPair(0U, &ARM::QPRRegClass); 13323 break; 13324 case 'x': 13325 if (VT == MVT::Other) 13326 break; 13327 if (VT == MVT::f32) 13328 return RCPair(0U, &ARM::SPR_8RegClass); 13329 if (VT.getSizeInBits() == 64) 13330 return RCPair(0U, &ARM::DPR_8RegClass); 13331 if (VT.getSizeInBits() == 128) 13332 return RCPair(0U, &ARM::QPR_8RegClass); 13333 break; 13334 case 't': 13335 if (VT == MVT::f32 || VT == MVT::i32) 13336 return RCPair(0U, &ARM::SPRRegClass); 13337 break; 13338 } 13339 } 13340 if (StringRef("{cc}").equals_lower(Constraint)) 13341 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 13342 13343 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 13344 } 13345 13346 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 13347 /// vector. If it is invalid, don't add anything to Ops. 13348 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 13349 std::string &Constraint, 13350 std::vector<SDValue>&Ops, 13351 SelectionDAG &DAG) const { 13352 SDValue Result; 13353 13354 // Currently only support length 1 constraints. 13355 if (Constraint.length() != 1) return; 13356 13357 char ConstraintLetter = Constraint[0]; 13358 switch (ConstraintLetter) { 13359 default: break; 13360 case 'j': 13361 case 'I': case 'J': case 'K': case 'L': 13362 case 'M': case 'N': case 'O': 13363 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 13364 if (!C) 13365 return; 13366 13367 int64_t CVal64 = C->getSExtValue(); 13368 int CVal = (int) CVal64; 13369 // None of these constraints allow values larger than 32 bits. Check 13370 // that the value fits in an int. 13371 if (CVal != CVal64) 13372 return; 13373 13374 switch (ConstraintLetter) { 13375 case 'j': 13376 // Constant suitable for movw, must be between 0 and 13377 // 65535. 13378 if (Subtarget->hasV6T2Ops()) 13379 if (CVal >= 0 && CVal <= 65535) 13380 break; 13381 return; 13382 case 'I': 13383 if (Subtarget->isThumb1Only()) { 13384 // This must be a constant between 0 and 255, for ADD 13385 // immediates. 13386 if (CVal >= 0 && CVal <= 255) 13387 break; 13388 } else if (Subtarget->isThumb2()) { 13389 // A constant that can be used as an immediate value in a 13390 // data-processing instruction. 13391 if (ARM_AM::getT2SOImmVal(CVal) != -1) 13392 break; 13393 } else { 13394 // A constant that can be used as an immediate value in a 13395 // data-processing instruction. 13396 if (ARM_AM::getSOImmVal(CVal) != -1) 13397 break; 13398 } 13399 return; 13400 13401 case 'J': 13402 if (Subtarget->isThumb1Only()) { 13403 // This must be a constant between -255 and -1, for negated ADD 13404 // immediates. This can be used in GCC with an "n" modifier that 13405 // prints the negated value, for use with SUB instructions. It is 13406 // not useful otherwise but is implemented for compatibility. 13407 if (CVal >= -255 && CVal <= -1) 13408 break; 13409 } else { 13410 // This must be a constant between -4095 and 4095. It is not clear 13411 // what this constraint is intended for. Implemented for 13412 // compatibility with GCC. 13413 if (CVal >= -4095 && CVal <= 4095) 13414 break; 13415 } 13416 return; 13417 13418 case 'K': 13419 if (Subtarget->isThumb1Only()) { 13420 // A 32-bit value where only one byte has a nonzero value. Exclude 13421 // zero to match GCC. This constraint is used by GCC internally for 13422 // constants that can be loaded with a move/shift combination. 13423 // It is not useful otherwise but is implemented for compatibility. 13424 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 13425 break; 13426 } else if (Subtarget->isThumb2()) { 13427 // A constant whose bitwise inverse can be used as an immediate 13428 // value in a data-processing instruction. This can be used in GCC 13429 // with a "B" modifier that prints the inverted value, for use with 13430 // BIC and MVN instructions. It is not useful otherwise but is 13431 // implemented for compatibility. 13432 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 13433 break; 13434 } else { 13435 // A constant whose bitwise inverse can be used as an immediate 13436 // value in a data-processing instruction. This can be used in GCC 13437 // with a "B" modifier that prints the inverted value, for use with 13438 // BIC and MVN instructions. It is not useful otherwise but is 13439 // implemented for compatibility. 13440 if (ARM_AM::getSOImmVal(~CVal) != -1) 13441 break; 13442 } 13443 return; 13444 13445 case 'L': 13446 if (Subtarget->isThumb1Only()) { 13447 // This must be a constant between -7 and 7, 13448 // for 3-operand ADD/SUB immediate instructions. 13449 if (CVal >= -7 && CVal < 7) 13450 break; 13451 } else if (Subtarget->isThumb2()) { 13452 // A constant whose negation can be used as an immediate value in a 13453 // data-processing instruction. This can be used in GCC with an "n" 13454 // modifier that prints the negated value, for use with SUB 13455 // instructions. It is not useful otherwise but is implemented for 13456 // compatibility. 13457 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 13458 break; 13459 } else { 13460 // A constant whose negation can be used as an immediate value in a 13461 // data-processing instruction. This can be used in GCC with an "n" 13462 // modifier that prints the negated value, for use with SUB 13463 // instructions. It is not useful otherwise but is implemented for 13464 // compatibility. 13465 if (ARM_AM::getSOImmVal(-CVal) != -1) 13466 break; 13467 } 13468 return; 13469 13470 case 'M': 13471 if (Subtarget->isThumb1Only()) { 13472 // This must be a multiple of 4 between 0 and 1020, for 13473 // ADD sp + immediate. 13474 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 13475 break; 13476 } else { 13477 // A power of two or a constant between 0 and 32. This is used in 13478 // GCC for the shift amount on shifted register operands, but it is 13479 // useful in general for any shift amounts. 13480 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 13481 break; 13482 } 13483 return; 13484 13485 case 'N': 13486 if (Subtarget->isThumb()) { // FIXME thumb2 13487 // This must be a constant between 0 and 31, for shift amounts. 13488 if (CVal >= 0 && CVal <= 31) 13489 break; 13490 } 13491 return; 13492 13493 case 'O': 13494 if (Subtarget->isThumb()) { // FIXME thumb2 13495 // This must be a multiple of 4 between -508 and 508, for 13496 // ADD/SUB sp = sp + immediate. 13497 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 13498 break; 13499 } 13500 return; 13501 } 13502 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType()); 13503 break; 13504 } 13505 13506 if (Result.getNode()) { 13507 Ops.push_back(Result); 13508 return; 13509 } 13510 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 13511 } 13512 13513 static RTLIB::Libcall getDivRemLibcall( 13514 const SDNode *N, MVT::SimpleValueType SVT) { 13515 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 13516 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 13517 "Unhandled Opcode in getDivRemLibcall"); 13518 bool isSigned = N->getOpcode() == ISD::SDIVREM || 13519 N->getOpcode() == ISD::SREM; 13520 RTLIB::Libcall LC; 13521 switch (SVT) { 13522 default: llvm_unreachable("Unexpected request for libcall!"); 13523 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 13524 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 13525 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 13526 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 13527 } 13528 return LC; 13529 } 13530 13531 static TargetLowering::ArgListTy getDivRemArgList( 13532 const SDNode *N, LLVMContext *Context, const ARMSubtarget *Subtarget) { 13533 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 13534 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 13535 "Unhandled Opcode in getDivRemArgList"); 13536 bool isSigned = N->getOpcode() == ISD::SDIVREM || 13537 N->getOpcode() == ISD::SREM; 13538 TargetLowering::ArgListTy Args; 13539 TargetLowering::ArgListEntry Entry; 13540 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 13541 EVT ArgVT = N->getOperand(i).getValueType(); 13542 Type *ArgTy = ArgVT.getTypeForEVT(*Context); 13543 Entry.Node = N->getOperand(i); 13544 Entry.Ty = ArgTy; 13545 Entry.IsSExt = isSigned; 13546 Entry.IsZExt = !isSigned; 13547 Args.push_back(Entry); 13548 } 13549 if (Subtarget->isTargetWindows() && Args.size() >= 2) 13550 std::swap(Args[0], Args[1]); 13551 return Args; 13552 } 13553 13554 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 13555 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 13556 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() || 13557 Subtarget->isTargetWindows()) && 13558 "Register-based DivRem lowering only"); 13559 unsigned Opcode = Op->getOpcode(); 13560 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 13561 "Invalid opcode for Div/Rem lowering"); 13562 bool isSigned = (Opcode == ISD::SDIVREM); 13563 EVT VT = Op->getValueType(0); 13564 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 13565 SDLoc dl(Op); 13566 13567 // If the target has hardware divide, use divide + multiply + subtract: 13568 // div = a / b 13569 // rem = a - b * div 13570 // return {div, rem} 13571 // This should be lowered into UDIV/SDIV + MLS later on. 13572 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode() 13573 : Subtarget->hasDivideInARMMode(); 13574 if (hasDivide && Op->getValueType(0).isSimple() && 13575 Op->getSimpleValueType(0) == MVT::i32) { 13576 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV; 13577 const SDValue Dividend = Op->getOperand(0); 13578 const SDValue Divisor = Op->getOperand(1); 13579 SDValue Div = DAG.getNode(DivOpcode, dl, VT, Dividend, Divisor); 13580 SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Div, Divisor); 13581 SDValue Rem = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul); 13582 13583 SDValue Values[2] = {Div, Rem}; 13584 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VT, VT), Values); 13585 } 13586 13587 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(), 13588 VT.getSimpleVT().SimpleTy); 13589 SDValue InChain = DAG.getEntryNode(); 13590 13591 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(), 13592 DAG.getContext(), 13593 Subtarget); 13594 13595 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 13596 getPointerTy(DAG.getDataLayout())); 13597 13598 Type *RetTy = StructType::get(Ty, Ty); 13599 13600 if (Subtarget->isTargetWindows()) 13601 InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain); 13602 13603 TargetLowering::CallLoweringInfo CLI(DAG); 13604 CLI.setDebugLoc(dl).setChain(InChain) 13605 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args)) 13606 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned); 13607 13608 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 13609 return CallInfo.first; 13610 } 13611 13612 // Lowers REM using divmod helpers 13613 // see RTABI section 4.2/4.3 13614 SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const { 13615 // Build return types (div and rem) 13616 std::vector<Type*> RetTyParams; 13617 Type *RetTyElement; 13618 13619 switch (N->getValueType(0).getSimpleVT().SimpleTy) { 13620 default: llvm_unreachable("Unexpected request for libcall!"); 13621 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break; 13622 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break; 13623 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break; 13624 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break; 13625 } 13626 13627 RetTyParams.push_back(RetTyElement); 13628 RetTyParams.push_back(RetTyElement); 13629 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams); 13630 Type *RetTy = StructType::get(*DAG.getContext(), ret); 13631 13632 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT(). 13633 SimpleTy); 13634 SDValue InChain = DAG.getEntryNode(); 13635 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext(), 13636 Subtarget); 13637 bool isSigned = N->getOpcode() == ISD::SREM; 13638 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 13639 getPointerTy(DAG.getDataLayout())); 13640 13641 if (Subtarget->isTargetWindows()) 13642 InChain = WinDBZCheckDenominator(DAG, N, InChain); 13643 13644 // Lower call 13645 CallLoweringInfo CLI(DAG); 13646 CLI.setChain(InChain) 13647 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args)) 13648 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N)); 13649 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 13650 13651 // Return second (rem) result operand (first contains div) 13652 SDNode *ResNode = CallResult.first.getNode(); 13653 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands"); 13654 return ResNode->getOperand(1); 13655 } 13656 13657 SDValue 13658 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 13659 assert(Subtarget->isTargetWindows() && "unsupported target platform"); 13660 SDLoc DL(Op); 13661 13662 // Get the inputs. 13663 SDValue Chain = Op.getOperand(0); 13664 SDValue Size = Op.getOperand(1); 13665 13666 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size, 13667 DAG.getConstant(2, DL, MVT::i32)); 13668 13669 SDValue Flag; 13670 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag); 13671 Flag = Chain.getValue(1); 13672 13673 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 13674 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag); 13675 13676 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 13677 Chain = NewSP.getValue(1); 13678 13679 SDValue Ops[2] = { NewSP, Chain }; 13680 return DAG.getMergeValues(Ops, DL); 13681 } 13682 13683 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 13684 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() && 13685 "Unexpected type for custom-lowering FP_EXTEND"); 13686 13687 RTLIB::Libcall LC; 13688 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 13689 13690 SDValue SrcVal = Op.getOperand(0); 13691 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 13692 SDLoc(Op)).first; 13693 } 13694 13695 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 13696 assert(Op.getOperand(0).getValueType() == MVT::f64 && 13697 Subtarget->isFPOnlySP() && 13698 "Unexpected type for custom-lowering FP_ROUND"); 13699 13700 RTLIB::Libcall LC; 13701 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 13702 13703 SDValue SrcVal = Op.getOperand(0); 13704 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 13705 SDLoc(Op)).first; 13706 } 13707 13708 bool 13709 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 13710 // The ARM target isn't yet aware of offsets. 13711 return false; 13712 } 13713 13714 bool ARM::isBitFieldInvertedMask(unsigned v) { 13715 if (v == 0xffffffff) 13716 return false; 13717 13718 // there can be 1's on either or both "outsides", all the "inside" 13719 // bits must be 0's 13720 return isShiftedMask_32(~v); 13721 } 13722 13723 /// isFPImmLegal - Returns true if the target can instruction select the 13724 /// specified FP immediate natively. If false, the legalizer will 13725 /// materialize the FP immediate as a load from a constant pool. 13726 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 13727 if (!Subtarget->hasVFP3()) 13728 return false; 13729 if (VT == MVT::f32) 13730 return ARM_AM::getFP32Imm(Imm) != -1; 13731 if (VT == MVT::f64 && !Subtarget->isFPOnlySP()) 13732 return ARM_AM::getFP64Imm(Imm) != -1; 13733 return false; 13734 } 13735 13736 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 13737 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 13738 /// specified in the intrinsic calls. 13739 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 13740 const CallInst &I, 13741 MachineFunction &MF, 13742 unsigned Intrinsic) const { 13743 switch (Intrinsic) { 13744 case Intrinsic::arm_neon_vld1: 13745 case Intrinsic::arm_neon_vld2: 13746 case Intrinsic::arm_neon_vld3: 13747 case Intrinsic::arm_neon_vld4: 13748 case Intrinsic::arm_neon_vld2lane: 13749 case Intrinsic::arm_neon_vld3lane: 13750 case Intrinsic::arm_neon_vld4lane: { 13751 Info.opc = ISD::INTRINSIC_W_CHAIN; 13752 // Conservatively set memVT to the entire set of vectors loaded. 13753 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 13754 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64; 13755 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 13756 Info.ptrVal = I.getArgOperand(0); 13757 Info.offset = 0; 13758 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 13759 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 13760 // volatile loads with NEON intrinsics not supported 13761 Info.flags = MachineMemOperand::MOLoad; 13762 return true; 13763 } 13764 case Intrinsic::arm_neon_vst1: 13765 case Intrinsic::arm_neon_vst2: 13766 case Intrinsic::arm_neon_vst3: 13767 case Intrinsic::arm_neon_vst4: 13768 case Intrinsic::arm_neon_vst2lane: 13769 case Intrinsic::arm_neon_vst3lane: 13770 case Intrinsic::arm_neon_vst4lane: { 13771 Info.opc = ISD::INTRINSIC_VOID; 13772 // Conservatively set memVT to the entire set of vectors stored. 13773 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 13774 unsigned NumElts = 0; 13775 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 13776 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 13777 if (!ArgTy->isVectorTy()) 13778 break; 13779 NumElts += DL.getTypeSizeInBits(ArgTy) / 64; 13780 } 13781 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 13782 Info.ptrVal = I.getArgOperand(0); 13783 Info.offset = 0; 13784 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 13785 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 13786 // volatile stores with NEON intrinsics not supported 13787 Info.flags = MachineMemOperand::MOStore; 13788 return true; 13789 } 13790 case Intrinsic::arm_ldaex: 13791 case Intrinsic::arm_ldrex: { 13792 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 13793 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 13794 Info.opc = ISD::INTRINSIC_W_CHAIN; 13795 Info.memVT = MVT::getVT(PtrTy->getElementType()); 13796 Info.ptrVal = I.getArgOperand(0); 13797 Info.offset = 0; 13798 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 13799 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 13800 return true; 13801 } 13802 case Intrinsic::arm_stlex: 13803 case Intrinsic::arm_strex: { 13804 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 13805 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 13806 Info.opc = ISD::INTRINSIC_W_CHAIN; 13807 Info.memVT = MVT::getVT(PtrTy->getElementType()); 13808 Info.ptrVal = I.getArgOperand(1); 13809 Info.offset = 0; 13810 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 13811 Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile; 13812 return true; 13813 } 13814 case Intrinsic::arm_stlexd: 13815 case Intrinsic::arm_strexd: 13816 Info.opc = ISD::INTRINSIC_W_CHAIN; 13817 Info.memVT = MVT::i64; 13818 Info.ptrVal = I.getArgOperand(2); 13819 Info.offset = 0; 13820 Info.align = 8; 13821 Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile; 13822 return true; 13823 13824 case Intrinsic::arm_ldaexd: 13825 case Intrinsic::arm_ldrexd: 13826 Info.opc = ISD::INTRINSIC_W_CHAIN; 13827 Info.memVT = MVT::i64; 13828 Info.ptrVal = I.getArgOperand(0); 13829 Info.offset = 0; 13830 Info.align = 8; 13831 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 13832 return true; 13833 13834 default: 13835 break; 13836 } 13837 13838 return false; 13839 } 13840 13841 /// \brief Returns true if it is beneficial to convert a load of a constant 13842 /// to just the constant itself. 13843 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 13844 Type *Ty) const { 13845 assert(Ty->isIntegerTy()); 13846 13847 unsigned Bits = Ty->getPrimitiveSizeInBits(); 13848 if (Bits == 0 || Bits > 32) 13849 return false; 13850 return true; 13851 } 13852 13853 bool ARMTargetLowering::isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, 13854 unsigned Index) const { 13855 if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT)) 13856 return false; 13857 13858 return (Index == 0 || Index == ResVT.getVectorNumElements()); 13859 } 13860 13861 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder, 13862 ARM_MB::MemBOpt Domain) const { 13863 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 13864 13865 // First, if the target has no DMB, see what fallback we can use. 13866 if (!Subtarget->hasDataBarrier()) { 13867 // Some ARMv6 cpus can support data barriers with an mcr instruction. 13868 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 13869 // here. 13870 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) { 13871 Function *MCR = Intrinsic::getDeclaration(M, Intrinsic::arm_mcr); 13872 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0), 13873 Builder.getInt32(0), Builder.getInt32(7), 13874 Builder.getInt32(10), Builder.getInt32(5)}; 13875 return Builder.CreateCall(MCR, args); 13876 } else { 13877 // Instead of using barriers, atomic accesses on these subtargets use 13878 // libcalls. 13879 llvm_unreachable("makeDMB on a target so old that it has no barriers"); 13880 } 13881 } else { 13882 Function *DMB = Intrinsic::getDeclaration(M, Intrinsic::arm_dmb); 13883 // Only a full system barrier exists in the M-class architectures. 13884 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain; 13885 Constant *CDomain = Builder.getInt32(Domain); 13886 return Builder.CreateCall(DMB, CDomain); 13887 } 13888 } 13889 13890 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 13891 Instruction *ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 13892 Instruction *Inst, 13893 AtomicOrdering Ord) const { 13894 switch (Ord) { 13895 case AtomicOrdering::NotAtomic: 13896 case AtomicOrdering::Unordered: 13897 llvm_unreachable("Invalid fence: unordered/non-atomic"); 13898 case AtomicOrdering::Monotonic: 13899 case AtomicOrdering::Acquire: 13900 return nullptr; // Nothing to do 13901 case AtomicOrdering::SequentiallyConsistent: 13902 if (!Inst->hasAtomicStore()) 13903 return nullptr; // Nothing to do 13904 LLVM_FALLTHROUGH; 13905 case AtomicOrdering::Release: 13906 case AtomicOrdering::AcquireRelease: 13907 if (Subtarget->preferISHSTBarriers()) 13908 return makeDMB(Builder, ARM_MB::ISHST); 13909 // FIXME: add a comment with a link to documentation justifying this. 13910 else 13911 return makeDMB(Builder, ARM_MB::ISH); 13912 } 13913 llvm_unreachable("Unknown fence ordering in emitLeadingFence"); 13914 } 13915 13916 Instruction *ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 13917 Instruction *Inst, 13918 AtomicOrdering Ord) const { 13919 switch (Ord) { 13920 case AtomicOrdering::NotAtomic: 13921 case AtomicOrdering::Unordered: 13922 llvm_unreachable("Invalid fence: unordered/not-atomic"); 13923 case AtomicOrdering::Monotonic: 13924 case AtomicOrdering::Release: 13925 return nullptr; // Nothing to do 13926 case AtomicOrdering::Acquire: 13927 case AtomicOrdering::AcquireRelease: 13928 case AtomicOrdering::SequentiallyConsistent: 13929 return makeDMB(Builder, ARM_MB::ISH); 13930 } 13931 llvm_unreachable("Unknown fence ordering in emitTrailingFence"); 13932 } 13933 13934 // Loads and stores less than 64-bits are already atomic; ones above that 13935 // are doomed anyway, so defer to the default libcall and blame the OS when 13936 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 13937 // anything for those. 13938 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 13939 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 13940 return (Size == 64) && !Subtarget->isMClass(); 13941 } 13942 13943 // Loads and stores less than 64-bits are already atomic; ones above that 13944 // are doomed anyway, so defer to the default libcall and blame the OS when 13945 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 13946 // anything for those. 13947 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that 13948 // guarantee, see DDI0406C ARM architecture reference manual, 13949 // sections A8.8.72-74 LDRD) 13950 TargetLowering::AtomicExpansionKind 13951 ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 13952 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 13953 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly 13954 : AtomicExpansionKind::None; 13955 } 13956 13957 // For the real atomic operations, we have ldrex/strex up to 32 bits, 13958 // and up to 64 bits on the non-M profiles 13959 TargetLowering::AtomicExpansionKind 13960 ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 13961 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 13962 bool hasAtomicRMW = !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps(); 13963 return (Size <= (Subtarget->isMClass() ? 32U : 64U) && hasAtomicRMW) 13964 ? AtomicExpansionKind::LLSC 13965 : AtomicExpansionKind::None; 13966 } 13967 13968 bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR( 13969 AtomicCmpXchgInst *AI) const { 13970 // At -O0, fast-regalloc cannot cope with the live vregs necessary to 13971 // implement cmpxchg without spilling. If the address being exchanged is also 13972 // on the stack and close enough to the spill slot, this can lead to a 13973 // situation where the monitor always gets cleared and the atomic operation 13974 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead. 13975 bool hasAtomicCmpXchg = 13976 !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps(); 13977 return getTargetMachine().getOptLevel() != 0 && hasAtomicCmpXchg; 13978 } 13979 13980 bool ARMTargetLowering::shouldInsertFencesForAtomic( 13981 const Instruction *I) const { 13982 return InsertFencesForAtomic; 13983 } 13984 13985 // This has so far only been implemented for MachO. 13986 bool ARMTargetLowering::useLoadStackGuardNode() const { 13987 return Subtarget->isTargetMachO(); 13988 } 13989 13990 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx, 13991 unsigned &Cost) const { 13992 // If we do not have NEON, vector types are not natively supported. 13993 if (!Subtarget->hasNEON()) 13994 return false; 13995 13996 // Floating point values and vector values map to the same register file. 13997 // Therefore, although we could do a store extract of a vector type, this is 13998 // better to leave at float as we have more freedom in the addressing mode for 13999 // those. 14000 if (VectorTy->isFPOrFPVectorTy()) 14001 return false; 14002 14003 // If the index is unknown at compile time, this is very expensive to lower 14004 // and it is not possible to combine the store with the extract. 14005 if (!isa<ConstantInt>(Idx)) 14006 return false; 14007 14008 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type"); 14009 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth(); 14010 // We can do a store + vector extract on any vector that fits perfectly in a D 14011 // or Q register. 14012 if (BitWidth == 64 || BitWidth == 128) { 14013 Cost = 0; 14014 return true; 14015 } 14016 return false; 14017 } 14018 14019 bool ARMTargetLowering::isCheapToSpeculateCttz() const { 14020 return Subtarget->hasV6T2Ops(); 14021 } 14022 14023 bool ARMTargetLowering::isCheapToSpeculateCtlz() const { 14024 return Subtarget->hasV6T2Ops(); 14025 } 14026 14027 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 14028 AtomicOrdering Ord) const { 14029 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14030 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 14031 bool IsAcquire = isAcquireOrStronger(Ord); 14032 14033 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd 14034 // intrinsic must return {i32, i32} and we have to recombine them into a 14035 // single i64 here. 14036 if (ValTy->getPrimitiveSizeInBits() == 64) { 14037 Intrinsic::ID Int = 14038 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd; 14039 Function *Ldrex = Intrinsic::getDeclaration(M, Int); 14040 14041 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 14042 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi"); 14043 14044 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 14045 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 14046 if (!Subtarget->isLittle()) 14047 std::swap (Lo, Hi); 14048 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 14049 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 14050 return Builder.CreateOr( 14051 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64"); 14052 } 14053 14054 Type *Tys[] = { Addr->getType() }; 14055 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex; 14056 Function *Ldrex = Intrinsic::getDeclaration(M, Int, Tys); 14057 14058 return Builder.CreateTruncOrBitCast( 14059 Builder.CreateCall(Ldrex, Addr), 14060 cast<PointerType>(Addr->getType())->getElementType()); 14061 } 14062 14063 void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance( 14064 IRBuilder<> &Builder) const { 14065 if (!Subtarget->hasV7Ops()) 14066 return; 14067 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14068 Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::arm_clrex)); 14069 } 14070 14071 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val, 14072 Value *Addr, 14073 AtomicOrdering Ord) const { 14074 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 14075 bool IsRelease = isReleaseOrStronger(Ord); 14076 14077 // Since the intrinsics must have legal type, the i64 intrinsics take two 14078 // parameters: "i32, i32". We must marshal Val into the appropriate form 14079 // before the call. 14080 if (Val->getType()->getPrimitiveSizeInBits() == 64) { 14081 Intrinsic::ID Int = 14082 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd; 14083 Function *Strex = Intrinsic::getDeclaration(M, Int); 14084 Type *Int32Ty = Type::getInt32Ty(M->getContext()); 14085 14086 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo"); 14087 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi"); 14088 if (!Subtarget->isLittle()) 14089 std::swap(Lo, Hi); 14090 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 14091 return Builder.CreateCall(Strex, {Lo, Hi, Addr}); 14092 } 14093 14094 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex; 14095 Type *Tys[] = { Addr->getType() }; 14096 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys); 14097 14098 return Builder.CreateCall( 14099 Strex, {Builder.CreateZExtOrBitCast( 14100 Val, Strex->getFunctionType()->getParamType(0)), 14101 Addr}); 14102 } 14103 14104 /// A helper function for determining the number of interleaved accesses we 14105 /// will generate when lowering accesses of the given type. 14106 unsigned 14107 ARMTargetLowering::getNumInterleavedAccesses(VectorType *VecTy, 14108 const DataLayout &DL) const { 14109 return (DL.getTypeSizeInBits(VecTy) + 127) / 128; 14110 } 14111 14112 bool ARMTargetLowering::isLegalInterleavedAccessType( 14113 VectorType *VecTy, const DataLayout &DL) const { 14114 14115 unsigned VecSize = DL.getTypeSizeInBits(VecTy); 14116 unsigned ElSize = DL.getTypeSizeInBits(VecTy->getElementType()); 14117 14118 // Ensure the vector doesn't have f16 elements. Even though we could do an 14119 // i16 vldN, we can't hold the f16 vectors and will end up converting via 14120 // f32. 14121 if (VecTy->getElementType()->isHalfTy()) 14122 return false; 14123 14124 // Ensure the number of vector elements is greater than 1. 14125 if (VecTy->getNumElements() < 2) 14126 return false; 14127 14128 // Ensure the element type is legal. 14129 if (ElSize != 8 && ElSize != 16 && ElSize != 32) 14130 return false; 14131 14132 // Ensure the total vector size is 64 or a multiple of 128. Types larger than 14133 // 128 will be split into multiple interleaved accesses. 14134 return VecSize == 64 || VecSize % 128 == 0; 14135 } 14136 14137 /// \brief Lower an interleaved load into a vldN intrinsic. 14138 /// 14139 /// E.g. Lower an interleaved load (Factor = 2): 14140 /// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4 14141 /// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements 14142 /// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements 14143 /// 14144 /// Into: 14145 /// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4) 14146 /// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0 14147 /// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1 14148 bool ARMTargetLowering::lowerInterleavedLoad( 14149 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles, 14150 ArrayRef<unsigned> Indices, unsigned Factor) const { 14151 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 14152 "Invalid interleave factor"); 14153 assert(!Shuffles.empty() && "Empty shufflevector input"); 14154 assert(Shuffles.size() == Indices.size() && 14155 "Unmatched number of shufflevectors and indices"); 14156 14157 VectorType *VecTy = Shuffles[0]->getType(); 14158 Type *EltTy = VecTy->getVectorElementType(); 14159 14160 const DataLayout &DL = LI->getModule()->getDataLayout(); 14161 14162 // Skip if we do not have NEON and skip illegal vector types. We can 14163 // "legalize" wide vector types into multiple interleaved accesses as long as 14164 // the vector types are divisible by 128. 14165 if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(VecTy, DL)) 14166 return false; 14167 14168 unsigned NumLoads = getNumInterleavedAccesses(VecTy, DL); 14169 14170 // A pointer vector can not be the return type of the ldN intrinsics. Need to 14171 // load integer vectors first and then convert to pointer vectors. 14172 if (EltTy->isPointerTy()) 14173 VecTy = 14174 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements()); 14175 14176 IRBuilder<> Builder(LI); 14177 14178 // The base address of the load. 14179 Value *BaseAddr = LI->getPointerOperand(); 14180 14181 if (NumLoads > 1) { 14182 // If we're going to generate more than one load, reset the sub-vector type 14183 // to something legal. 14184 VecTy = VectorType::get(VecTy->getVectorElementType(), 14185 VecTy->getVectorNumElements() / NumLoads); 14186 14187 // We will compute the pointer operand of each load from the original base 14188 // address using GEPs. Cast the base address to a pointer to the scalar 14189 // element type. 14190 BaseAddr = Builder.CreateBitCast( 14191 BaseAddr, VecTy->getVectorElementType()->getPointerTo( 14192 LI->getPointerAddressSpace())); 14193 } 14194 14195 assert(isTypeLegal(EVT::getEVT(VecTy)) && "Illegal vldN vector type!"); 14196 14197 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace()); 14198 Type *Tys[] = {VecTy, Int8Ptr}; 14199 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2, 14200 Intrinsic::arm_neon_vld3, 14201 Intrinsic::arm_neon_vld4}; 14202 Function *VldnFunc = 14203 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys); 14204 14205 // Holds sub-vectors extracted from the load intrinsic return values. The 14206 // sub-vectors are associated with the shufflevector instructions they will 14207 // replace. 14208 DenseMap<ShuffleVectorInst *, SmallVector<Value *, 4>> SubVecs; 14209 14210 for (unsigned LoadCount = 0; LoadCount < NumLoads; ++LoadCount) { 14211 // If we're generating more than one load, compute the base address of 14212 // subsequent loads as an offset from the previous. 14213 if (LoadCount > 0) 14214 BaseAddr = Builder.CreateConstGEP1_32( 14215 BaseAddr, VecTy->getVectorNumElements() * Factor); 14216 14217 SmallVector<Value *, 2> Ops; 14218 Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr)); 14219 Ops.push_back(Builder.getInt32(LI->getAlignment())); 14220 14221 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN"); 14222 14223 // Replace uses of each shufflevector with the corresponding vector loaded 14224 // by ldN. 14225 for (unsigned i = 0; i < Shuffles.size(); i++) { 14226 ShuffleVectorInst *SV = Shuffles[i]; 14227 unsigned Index = Indices[i]; 14228 14229 Value *SubVec = Builder.CreateExtractValue(VldN, Index); 14230 14231 // Convert the integer vector to pointer vector if the element is pointer. 14232 if (EltTy->isPointerTy()) 14233 SubVec = Builder.CreateIntToPtr( 14234 SubVec, VectorType::get(SV->getType()->getVectorElementType(), 14235 VecTy->getVectorNumElements())); 14236 14237 SubVecs[SV].push_back(SubVec); 14238 } 14239 } 14240 14241 // Replace uses of the shufflevector instructions with the sub-vectors 14242 // returned by the load intrinsic. If a shufflevector instruction is 14243 // associated with more than one sub-vector, those sub-vectors will be 14244 // concatenated into a single wide vector. 14245 for (ShuffleVectorInst *SVI : Shuffles) { 14246 auto &SubVec = SubVecs[SVI]; 14247 auto *WideVec = 14248 SubVec.size() > 1 ? concatenateVectors(Builder, SubVec) : SubVec[0]; 14249 SVI->replaceAllUsesWith(WideVec); 14250 } 14251 14252 return true; 14253 } 14254 14255 /// \brief Lower an interleaved store into a vstN intrinsic. 14256 /// 14257 /// E.g. Lower an interleaved store (Factor = 3): 14258 /// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, 14259 /// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> 14260 /// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4 14261 /// 14262 /// Into: 14263 /// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3> 14264 /// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7> 14265 /// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11> 14266 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 14267 /// 14268 /// Note that the new shufflevectors will be removed and we'll only generate one 14269 /// vst3 instruction in CodeGen. 14270 /// 14271 /// Example for a more general valid mask (Factor 3). Lower: 14272 /// %i.vec = shuffle <32 x i32> %v0, <32 x i32> %v1, 14273 /// <4, 32, 16, 5, 33, 17, 6, 34, 18, 7, 35, 19> 14274 /// store <12 x i32> %i.vec, <12 x i32>* %ptr 14275 /// 14276 /// Into: 14277 /// %sub.v0 = shuffle <32 x i32> %v0, <32 x i32> v1, <4, 5, 6, 7> 14278 /// %sub.v1 = shuffle <32 x i32> %v0, <32 x i32> v1, <32, 33, 34, 35> 14279 /// %sub.v2 = shuffle <32 x i32> %v0, <32 x i32> v1, <16, 17, 18, 19> 14280 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 14281 bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI, 14282 ShuffleVectorInst *SVI, 14283 unsigned Factor) const { 14284 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 14285 "Invalid interleave factor"); 14286 14287 VectorType *VecTy = SVI->getType(); 14288 assert(VecTy->getVectorNumElements() % Factor == 0 && 14289 "Invalid interleaved store"); 14290 14291 unsigned LaneLen = VecTy->getVectorNumElements() / Factor; 14292 Type *EltTy = VecTy->getVectorElementType(); 14293 VectorType *SubVecTy = VectorType::get(EltTy, LaneLen); 14294 14295 const DataLayout &DL = SI->getModule()->getDataLayout(); 14296 14297 // Skip if we do not have NEON and skip illegal vector types. We can 14298 // "legalize" wide vector types into multiple interleaved accesses as long as 14299 // the vector types are divisible by 128. 14300 if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(SubVecTy, DL)) 14301 return false; 14302 14303 unsigned NumStores = getNumInterleavedAccesses(SubVecTy, DL); 14304 14305 Value *Op0 = SVI->getOperand(0); 14306 Value *Op1 = SVI->getOperand(1); 14307 IRBuilder<> Builder(SI); 14308 14309 // StN intrinsics don't support pointer vectors as arguments. Convert pointer 14310 // vectors to integer vectors. 14311 if (EltTy->isPointerTy()) { 14312 Type *IntTy = DL.getIntPtrType(EltTy); 14313 14314 // Convert to the corresponding integer vector. 14315 Type *IntVecTy = 14316 VectorType::get(IntTy, Op0->getType()->getVectorNumElements()); 14317 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy); 14318 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy); 14319 14320 SubVecTy = VectorType::get(IntTy, LaneLen); 14321 } 14322 14323 // The base address of the store. 14324 Value *BaseAddr = SI->getPointerOperand(); 14325 14326 if (NumStores > 1) { 14327 // If we're going to generate more than one store, reset the lane length 14328 // and sub-vector type to something legal. 14329 LaneLen /= NumStores; 14330 SubVecTy = VectorType::get(SubVecTy->getVectorElementType(), LaneLen); 14331 14332 // We will compute the pointer operand of each store from the original base 14333 // address using GEPs. Cast the base address to a pointer to the scalar 14334 // element type. 14335 BaseAddr = Builder.CreateBitCast( 14336 BaseAddr, SubVecTy->getVectorElementType()->getPointerTo( 14337 SI->getPointerAddressSpace())); 14338 } 14339 14340 assert(isTypeLegal(EVT::getEVT(SubVecTy)) && "Illegal vstN vector type!"); 14341 14342 auto Mask = SVI->getShuffleMask(); 14343 14344 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace()); 14345 Type *Tys[] = {Int8Ptr, SubVecTy}; 14346 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2, 14347 Intrinsic::arm_neon_vst3, 14348 Intrinsic::arm_neon_vst4}; 14349 14350 for (unsigned StoreCount = 0; StoreCount < NumStores; ++StoreCount) { 14351 // If we generating more than one store, we compute the base address of 14352 // subsequent stores as an offset from the previous. 14353 if (StoreCount > 0) 14354 BaseAddr = Builder.CreateConstGEP1_32(BaseAddr, LaneLen * Factor); 14355 14356 SmallVector<Value *, 6> Ops; 14357 Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr)); 14358 14359 Function *VstNFunc = 14360 Intrinsic::getDeclaration(SI->getModule(), StoreInts[Factor - 2], Tys); 14361 14362 // Split the shufflevector operands into sub vectors for the new vstN call. 14363 for (unsigned i = 0; i < Factor; i++) { 14364 unsigned IdxI = StoreCount * LaneLen * Factor + i; 14365 if (Mask[IdxI] >= 0) { 14366 Ops.push_back(Builder.CreateShuffleVector( 14367 Op0, Op1, createSequentialMask(Builder, Mask[IdxI], LaneLen, 0))); 14368 } else { 14369 unsigned StartMask = 0; 14370 for (unsigned j = 1; j < LaneLen; j++) { 14371 unsigned IdxJ = StoreCount * LaneLen * Factor + j; 14372 if (Mask[IdxJ * Factor + IdxI] >= 0) { 14373 StartMask = Mask[IdxJ * Factor + IdxI] - IdxJ; 14374 break; 14375 } 14376 } 14377 // Note: If all elements in a chunk are undefs, StartMask=0! 14378 // Note: Filling undef gaps with random elements is ok, since 14379 // those elements were being written anyway (with undefs). 14380 // In the case of all undefs we're defaulting to using elems from 0 14381 // Note: StartMask cannot be negative, it's checked in 14382 // isReInterleaveMask 14383 Ops.push_back(Builder.CreateShuffleVector( 14384 Op0, Op1, createSequentialMask(Builder, StartMask, LaneLen, 0))); 14385 } 14386 } 14387 14388 Ops.push_back(Builder.getInt32(SI->getAlignment())); 14389 Builder.CreateCall(VstNFunc, Ops); 14390 } 14391 return true; 14392 } 14393 14394 enum HABaseType { 14395 HA_UNKNOWN = 0, 14396 HA_FLOAT, 14397 HA_DOUBLE, 14398 HA_VECT64, 14399 HA_VECT128 14400 }; 14401 14402 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base, 14403 uint64_t &Members) { 14404 if (auto *ST = dyn_cast<StructType>(Ty)) { 14405 for (unsigned i = 0; i < ST->getNumElements(); ++i) { 14406 uint64_t SubMembers = 0; 14407 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers)) 14408 return false; 14409 Members += SubMembers; 14410 } 14411 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) { 14412 uint64_t SubMembers = 0; 14413 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers)) 14414 return false; 14415 Members += SubMembers * AT->getNumElements(); 14416 } else if (Ty->isFloatTy()) { 14417 if (Base != HA_UNKNOWN && Base != HA_FLOAT) 14418 return false; 14419 Members = 1; 14420 Base = HA_FLOAT; 14421 } else if (Ty->isDoubleTy()) { 14422 if (Base != HA_UNKNOWN && Base != HA_DOUBLE) 14423 return false; 14424 Members = 1; 14425 Base = HA_DOUBLE; 14426 } else if (auto *VT = dyn_cast<VectorType>(Ty)) { 14427 Members = 1; 14428 switch (Base) { 14429 case HA_FLOAT: 14430 case HA_DOUBLE: 14431 return false; 14432 case HA_VECT64: 14433 return VT->getBitWidth() == 64; 14434 case HA_VECT128: 14435 return VT->getBitWidth() == 128; 14436 case HA_UNKNOWN: 14437 switch (VT->getBitWidth()) { 14438 case 64: 14439 Base = HA_VECT64; 14440 return true; 14441 case 128: 14442 Base = HA_VECT128; 14443 return true; 14444 default: 14445 return false; 14446 } 14447 } 14448 } 14449 14450 return (Members > 0 && Members <= 4); 14451 } 14452 14453 /// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of 14454 /// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when 14455 /// passing according to AAPCS rules. 14456 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters( 14457 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 14458 if (getEffectiveCallingConv(CallConv, isVarArg) != 14459 CallingConv::ARM_AAPCS_VFP) 14460 return false; 14461 14462 HABaseType Base = HA_UNKNOWN; 14463 uint64_t Members = 0; 14464 bool IsHA = isHomogeneousAggregate(Ty, Base, Members); 14465 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump()); 14466 14467 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy(); 14468 return IsHA || IsIntArray; 14469 } 14470 14471 unsigned ARMTargetLowering::getExceptionPointerRegister( 14472 const Constant *PersonalityFn) const { 14473 // Platforms which do not use SjLj EH may return values in these registers 14474 // via the personality function. 14475 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0; 14476 } 14477 14478 unsigned ARMTargetLowering::getExceptionSelectorRegister( 14479 const Constant *PersonalityFn) const { 14480 // Platforms which do not use SjLj EH may return values in these registers 14481 // via the personality function. 14482 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1; 14483 } 14484 14485 void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 14486 // Update IsSplitCSR in ARMFunctionInfo. 14487 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>(); 14488 AFI->setIsSplitCSR(true); 14489 } 14490 14491 void ARMTargetLowering::insertCopiesSplitCSR( 14492 MachineBasicBlock *Entry, 14493 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 14494 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 14495 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 14496 if (!IStart) 14497 return; 14498 14499 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 14500 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 14501 MachineBasicBlock::iterator MBBI = Entry->begin(); 14502 for (const MCPhysReg *I = IStart; *I; ++I) { 14503 const TargetRegisterClass *RC = nullptr; 14504 if (ARM::GPRRegClass.contains(*I)) 14505 RC = &ARM::GPRRegClass; 14506 else if (ARM::DPRRegClass.contains(*I)) 14507 RC = &ARM::DPRRegClass; 14508 else 14509 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 14510 14511 unsigned NewVR = MRI->createVirtualRegister(RC); 14512 // Create copy from CSR to a virtual register. 14513 // FIXME: this currently does not emit CFI pseudo-instructions, it works 14514 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be 14515 // nounwind. If we want to generalize this later, we may need to emit 14516 // CFI pseudo-instructions. 14517 assert(Entry->getParent()->getFunction().hasFnAttribute( 14518 Attribute::NoUnwind) && 14519 "Function should be nounwind in insertCopiesSplitCSR!"); 14520 Entry->addLiveIn(*I); 14521 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 14522 .addReg(*I); 14523 14524 // Insert the copy-back instructions right before the terminator. 14525 for (auto *Exit : Exits) 14526 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 14527 TII->get(TargetOpcode::COPY), *I) 14528 .addReg(NewVR); 14529 } 14530 } 14531 14532 void ARMTargetLowering::finalizeLowering(MachineFunction &MF) const { 14533 MF.getFrameInfo().computeMaxCallFrameSize(MF); 14534 TargetLoweringBase::finalizeLowering(MF); 14535 } 14536