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 #define DEBUG_TYPE "arm-isel" 16 #include "ARMISelLowering.h" 17 #include "ARM.h" 18 #include "ARMCallingConv.h" 19 #include "ARMConstantPoolValue.h" 20 #include "ARMMachineFunctionInfo.h" 21 #include "ARMPerfectShuffle.h" 22 #include "ARMSubtarget.h" 23 #include "ARMTargetMachine.h" 24 #include "ARMTargetObjectFile.h" 25 #include "MCTargetDesc/ARMAddressingModes.h" 26 #include "llvm/ADT/Statistic.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/CodeGen/CallingConvLower.h" 29 #include "llvm/CodeGen/IntrinsicLowering.h" 30 #include "llvm/CodeGen/MachineBasicBlock.h" 31 #include "llvm/CodeGen/MachineFrameInfo.h" 32 #include "llvm/CodeGen/MachineFunction.h" 33 #include "llvm/CodeGen/MachineInstrBuilder.h" 34 #include "llvm/CodeGen/MachineModuleInfo.h" 35 #include "llvm/CodeGen/MachineRegisterInfo.h" 36 #include "llvm/CodeGen/SelectionDAG.h" 37 #include "llvm/IR/CallingConv.h" 38 #include "llvm/IR/Constants.h" 39 #include "llvm/IR/Function.h" 40 #include "llvm/IR/GlobalValue.h" 41 #include "llvm/IR/Instruction.h" 42 #include "llvm/IR/Instructions.h" 43 #include "llvm/IR/Intrinsics.h" 44 #include "llvm/IR/Type.h" 45 #include "llvm/MC/MCSectionMachO.h" 46 #include "llvm/Support/CommandLine.h" 47 #include "llvm/Support/ErrorHandling.h" 48 #include "llvm/Support/MathExtras.h" 49 #include "llvm/Support/raw_ostream.h" 50 #include "llvm/Target/TargetOptions.h" 51 using namespace llvm; 52 53 STATISTIC(NumTailCalls, "Number of tail calls"); 54 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 55 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); 56 57 // This option should go away when tail calls fully work. 58 static cl::opt<bool> 59 EnableARMTailCalls("arm-tail-calls", cl::Hidden, 60 cl::desc("Generate tail calls (TEMPORARY OPTION)."), 61 cl::init(false)); 62 63 cl::opt<bool> 64 EnableARMLongCalls("arm-long-calls", cl::Hidden, 65 cl::desc("Generate calls via indirect call instructions"), 66 cl::init(false)); 67 68 static cl::opt<bool> 69 ARMInterworking("arm-interworking", cl::Hidden, 70 cl::desc("Enable / disable ARM interworking (for debugging only)"), 71 cl::init(true)); 72 73 namespace { 74 class ARMCCState : public CCState { 75 public: 76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, 77 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs, 78 LLVMContext &C, ParmContext PC) 79 : CCState(CC, isVarArg, MF, TM, locs, C) { 80 assert(((PC == Call) || (PC == Prologue)) && 81 "ARMCCState users must specify whether their context is call" 82 "or prologue generation."); 83 CallOrPrologue = PC; 84 } 85 }; 86 } 87 88 // The APCS parameter registers. 89 static const uint16_t GPRArgRegs[] = { 90 ARM::R0, ARM::R1, ARM::R2, ARM::R3 91 }; 92 93 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 94 MVT PromotedBitwiseVT) { 95 if (VT != PromotedLdStVT) { 96 setOperationAction(ISD::LOAD, VT, Promote); 97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 98 99 setOperationAction(ISD::STORE, VT, Promote); 100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 101 } 102 103 MVT ElemTy = VT.getVectorElementType(); 104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64) 105 setOperationAction(ISD::SETCC, VT, Custom); 106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 108 if (ElemTy == MVT::i32) { 109 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 110 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 111 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 112 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 113 } else { 114 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 115 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 116 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 117 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 118 } 119 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 120 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 121 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 123 setOperationAction(ISD::SELECT, VT, Expand); 124 setOperationAction(ISD::SELECT_CC, VT, Expand); 125 setOperationAction(ISD::VSELECT, VT, Expand); 126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 127 if (VT.isInteger()) { 128 setOperationAction(ISD::SHL, VT, Custom); 129 setOperationAction(ISD::SRA, VT, Custom); 130 setOperationAction(ISD::SRL, VT, Custom); 131 } 132 133 // Promote all bit-wise operations. 134 if (VT.isInteger() && VT != PromotedBitwiseVT) { 135 setOperationAction(ISD::AND, VT, Promote); 136 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 137 setOperationAction(ISD::OR, VT, Promote); 138 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 139 setOperationAction(ISD::XOR, VT, Promote); 140 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 141 } 142 143 // Neon does not support vector divide/remainder operations. 144 setOperationAction(ISD::SDIV, VT, Expand); 145 setOperationAction(ISD::UDIV, VT, Expand); 146 setOperationAction(ISD::FDIV, VT, Expand); 147 setOperationAction(ISD::SREM, VT, Expand); 148 setOperationAction(ISD::UREM, VT, Expand); 149 setOperationAction(ISD::FREM, VT, Expand); 150 } 151 152 void ARMTargetLowering::addDRTypeForNEON(MVT VT) { 153 addRegisterClass(VT, &ARM::DPRRegClass); 154 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 155 } 156 157 void ARMTargetLowering::addQRTypeForNEON(MVT VT) { 158 addRegisterClass(VT, &ARM::QPRRegClass); 159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 160 } 161 162 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) { 163 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin()) 164 return new TargetLoweringObjectFileMachO(); 165 166 return new ARMElfTargetObjectFile(); 167 } 168 169 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM) 170 : TargetLowering(TM, createTLOF(TM)) { 171 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 172 RegInfo = TM.getRegisterInfo(); 173 Itins = TM.getInstrItineraryData(); 174 175 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 176 177 if (Subtarget->isTargetDarwin()) { 178 // Uses VFP for Thumb libfuncs if available. 179 if (Subtarget->isThumb() && Subtarget->hasVFP2()) { 180 // Single-precision floating-point arithmetic. 181 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp"); 182 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp"); 183 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp"); 184 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp"); 185 186 // Double-precision floating-point arithmetic. 187 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp"); 188 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp"); 189 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp"); 190 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp"); 191 192 // Single-precision comparisons. 193 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp"); 194 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp"); 195 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp"); 196 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp"); 197 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp"); 198 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp"); 199 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp"); 200 setLibcallName(RTLIB::O_F32, "__unordsf2vfp"); 201 202 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 203 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); 204 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 205 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 206 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 207 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 208 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 209 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 210 211 // Double-precision comparisons. 212 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp"); 213 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp"); 214 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp"); 215 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp"); 216 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp"); 217 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp"); 218 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp"); 219 setLibcallName(RTLIB::O_F64, "__unorddf2vfp"); 220 221 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 222 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE); 223 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 224 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 225 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 226 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 227 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 228 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 229 230 // Floating-point to integer conversions. 231 // i64 conversions are done via library routines even when generating VFP 232 // instructions, so use the same ones. 233 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp"); 234 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp"); 235 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp"); 236 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp"); 237 238 // Conversions between floating types. 239 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp"); 240 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp"); 241 242 // Integer to floating-point conversions. 243 // i64 conversions are done via library routines even when generating VFP 244 // instructions, so use the same ones. 245 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 246 // e.g., __floatunsidf vs. __floatunssidfvfp. 247 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp"); 248 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp"); 249 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp"); 250 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp"); 251 } 252 } 253 254 // These libcalls are not available in 32-bit. 255 setLibcallName(RTLIB::SHL_I128, 0); 256 setLibcallName(RTLIB::SRL_I128, 0); 257 setLibcallName(RTLIB::SRA_I128, 0); 258 259 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) { 260 // Double-precision floating-point arithmetic helper functions 261 // RTABI chapter 4.1.2, Table 2 262 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd"); 263 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv"); 264 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul"); 265 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub"); 266 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS); 267 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS); 268 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS); 269 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS); 270 271 // Double-precision floating-point comparison helper functions 272 // RTABI chapter 4.1.2, Table 3 273 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq"); 274 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 275 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq"); 276 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ); 277 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt"); 278 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 279 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple"); 280 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 281 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge"); 282 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 283 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt"); 284 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 285 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun"); 286 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 287 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun"); 288 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 289 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS); 290 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS); 291 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS); 292 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS); 293 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS); 294 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS); 295 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS); 296 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS); 297 298 // Single-precision floating-point arithmetic helper functions 299 // RTABI chapter 4.1.2, Table 4 300 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd"); 301 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv"); 302 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul"); 303 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub"); 304 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS); 305 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS); 306 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS); 307 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS); 308 309 // Single-precision floating-point comparison helper functions 310 // RTABI chapter 4.1.2, Table 5 311 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq"); 312 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 313 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq"); 314 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ); 315 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt"); 316 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 317 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple"); 318 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 319 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge"); 320 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 321 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt"); 322 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 323 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun"); 324 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 325 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun"); 326 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 327 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS); 328 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS); 329 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS); 330 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS); 331 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS); 332 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS); 333 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS); 334 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS); 335 336 // Floating-point to integer conversions. 337 // RTABI chapter 4.1.2, Table 6 338 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz"); 339 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz"); 340 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz"); 341 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz"); 342 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz"); 343 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz"); 344 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz"); 345 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz"); 346 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS); 347 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS); 348 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS); 349 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS); 350 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS); 351 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS); 352 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS); 353 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS); 354 355 // Conversions between floating types. 356 // RTABI chapter 4.1.2, Table 7 357 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f"); 358 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d"); 359 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS); 360 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS); 361 362 // Integer to floating-point conversions. 363 // RTABI chapter 4.1.2, Table 8 364 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d"); 365 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d"); 366 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d"); 367 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d"); 368 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f"); 369 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f"); 370 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f"); 371 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f"); 372 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS); 373 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS); 374 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS); 375 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS); 376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS); 377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS); 378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS); 379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS); 380 381 // Long long helper functions 382 // RTABI chapter 4.2, Table 9 383 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul"); 384 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl"); 385 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr"); 386 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr"); 387 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS); 388 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS); 389 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS); 390 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS); 391 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS); 392 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS); 393 394 // Integer division functions 395 // RTABI chapter 4.3.1 396 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv"); 397 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv"); 398 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv"); 399 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod"); 400 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv"); 401 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv"); 402 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv"); 403 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod"); 404 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS); 405 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS); 406 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS); 407 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS); 408 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS); 409 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS); 410 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS); 411 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS); 412 413 // Memory operations 414 // RTABI chapter 4.3.4 415 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy"); 416 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove"); 417 setLibcallName(RTLIB::MEMSET, "__aeabi_memset"); 418 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS); 419 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS); 420 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS); 421 } 422 423 // Use divmod compiler-rt calls for iOS 5.0 and later. 424 if (Subtarget->getTargetTriple().isiOS() && 425 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) { 426 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 427 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 428 } 429 430 if (Subtarget->isThumb1Only()) 431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 432 else 433 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 434 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 435 !Subtarget->isThumb1Only()) { 436 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 437 if (!Subtarget->isFPOnlySP()) 438 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 439 440 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 441 } 442 443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { 445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT) 447 setTruncStoreAction((MVT::SimpleValueType)VT, 448 (MVT::SimpleValueType)InnerVT, Expand); 449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand); 450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand); 451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand); 452 } 453 454 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 455 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 456 457 if (Subtarget->hasNEON()) { 458 addDRTypeForNEON(MVT::v2f32); 459 addDRTypeForNEON(MVT::v8i8); 460 addDRTypeForNEON(MVT::v4i16); 461 addDRTypeForNEON(MVT::v2i32); 462 addDRTypeForNEON(MVT::v1i64); 463 464 addQRTypeForNEON(MVT::v4f32); 465 addQRTypeForNEON(MVT::v2f64); 466 addQRTypeForNEON(MVT::v16i8); 467 addQRTypeForNEON(MVT::v8i16); 468 addQRTypeForNEON(MVT::v4i32); 469 addQRTypeForNEON(MVT::v2i64); 470 471 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 472 // neither Neon nor VFP support any arithmetic operations on it. 473 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 474 // supported for v4f32. 475 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 476 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 477 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 478 // FIXME: Code duplication: FDIV and FREM are expanded always, see 479 // ARMTargetLowering::addTypeForNEON method for details. 480 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 481 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 482 // FIXME: Create unittest. 483 // In another words, find a way when "copysign" appears in DAG with vector 484 // operands. 485 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 486 // FIXME: Code duplication: SETCC has custom operation action, see 487 // ARMTargetLowering::addTypeForNEON method for details. 488 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 489 // FIXME: Create unittest for FNEG and for FABS. 490 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 491 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 492 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 493 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 494 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 495 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand); 496 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 497 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 498 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 499 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 500 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 501 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 502 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 503 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 504 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 505 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 506 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 507 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 508 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 509 510 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 511 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 512 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 513 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand); 514 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 515 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 516 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 517 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 518 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 519 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 520 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 521 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 522 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 523 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 524 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 525 526 // Mark v2f32 intrinsics. 527 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 528 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 529 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 530 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand); 531 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 532 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 533 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 534 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 535 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 536 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 537 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 538 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 539 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 540 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 541 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 542 543 // Neon does not support some operations on v1i64 and v2i64 types. 544 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 545 // Custom handling for some quad-vector types to detect VMULL. 546 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 547 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 548 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 549 // Custom handling for some vector types to avoid expensive expansions 550 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 551 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 552 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 553 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 554 setOperationAction(ISD::SETCC, MVT::v1i64, Expand); 555 setOperationAction(ISD::SETCC, MVT::v2i64, Expand); 556 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 557 // a destination type that is wider than the source, and nor does 558 // it have a FP_TO_[SU]INT instruction with a narrower destination than 559 // source. 560 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 561 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 562 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 563 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 564 565 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 566 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 567 568 // Custom expand long extensions to vectors. 569 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom); 570 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom); 571 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom); 572 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom); 573 setOperationAction(ISD::SIGN_EXTEND, MVT::v16i32, Custom); 574 setOperationAction(ISD::ZERO_EXTEND, MVT::v16i32, Custom); 575 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i64, Custom); 576 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i64, Custom); 577 578 // NEON does not have single instruction CTPOP for vectors with element 579 // types wider than 8-bits. However, custom lowering can leverage the 580 // v8i8/v16i8 vcnt instruction. 581 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 582 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 583 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 584 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 585 586 // NEON only has FMA instructions as of VFP4. 587 if (!Subtarget->hasVFP4()) { 588 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 589 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 590 } 591 592 setTargetDAGCombine(ISD::INTRINSIC_VOID); 593 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 594 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 595 setTargetDAGCombine(ISD::SHL); 596 setTargetDAGCombine(ISD::SRL); 597 setTargetDAGCombine(ISD::SRA); 598 setTargetDAGCombine(ISD::SIGN_EXTEND); 599 setTargetDAGCombine(ISD::ZERO_EXTEND); 600 setTargetDAGCombine(ISD::ANY_EXTEND); 601 setTargetDAGCombine(ISD::SELECT_CC); 602 setTargetDAGCombine(ISD::BUILD_VECTOR); 603 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 604 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 605 setTargetDAGCombine(ISD::STORE); 606 setTargetDAGCombine(ISD::FP_TO_SINT); 607 setTargetDAGCombine(ISD::FP_TO_UINT); 608 setTargetDAGCombine(ISD::FDIV); 609 610 // It is legal to extload from v4i8 to v4i16 or v4i32. 611 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8, 612 MVT::v4i16, MVT::v2i16, 613 MVT::v2i32}; 614 for (unsigned i = 0; i < 6; ++i) { 615 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal); 616 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal); 617 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal); 618 } 619 } 620 621 // ARM and Thumb2 support UMLAL/SMLAL. 622 if (!Subtarget->isThumb1Only()) 623 setTargetDAGCombine(ISD::ADDC); 624 625 626 computeRegisterProperties(); 627 628 // ARM does not have f32 extending load. 629 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 630 631 // ARM does not have i1 sign extending load. 632 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 633 634 // ARM supports all 4 flavors of integer indexed load / store. 635 if (!Subtarget->isThumb1Only()) { 636 for (unsigned im = (unsigned)ISD::PRE_INC; 637 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 638 setIndexedLoadAction(im, MVT::i1, Legal); 639 setIndexedLoadAction(im, MVT::i8, Legal); 640 setIndexedLoadAction(im, MVT::i16, Legal); 641 setIndexedLoadAction(im, MVT::i32, Legal); 642 setIndexedStoreAction(im, MVT::i1, Legal); 643 setIndexedStoreAction(im, MVT::i8, Legal); 644 setIndexedStoreAction(im, MVT::i16, Legal); 645 setIndexedStoreAction(im, MVT::i32, Legal); 646 } 647 } 648 649 // i64 operation support. 650 setOperationAction(ISD::MUL, MVT::i64, Expand); 651 setOperationAction(ISD::MULHU, MVT::i32, Expand); 652 if (Subtarget->isThumb1Only()) { 653 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 654 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 655 } 656 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 657 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP())) 658 setOperationAction(ISD::MULHS, MVT::i32, Expand); 659 660 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 661 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 662 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 663 setOperationAction(ISD::SRL, MVT::i64, Custom); 664 setOperationAction(ISD::SRA, MVT::i64, Custom); 665 666 if (!Subtarget->isThumb1Only()) { 667 // FIXME: We should do this for Thumb1 as well. 668 setOperationAction(ISD::ADDC, MVT::i32, Custom); 669 setOperationAction(ISD::ADDE, MVT::i32, Custom); 670 setOperationAction(ISD::SUBC, MVT::i32, Custom); 671 setOperationAction(ISD::SUBE, MVT::i32, Custom); 672 } 673 674 // ARM does not have ROTL. 675 setOperationAction(ISD::ROTL, MVT::i32, Expand); 676 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 677 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 678 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 679 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 680 681 // These just redirect to CTTZ and CTLZ on ARM. 682 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand); 683 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand); 684 685 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 686 687 // Only ARMv6 has BSWAP. 688 if (!Subtarget->hasV6Ops()) 689 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 690 691 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) && 692 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) { 693 // These are expanded into libcalls if the cpu doesn't have HW divider. 694 setOperationAction(ISD::SDIV, MVT::i32, Expand); 695 setOperationAction(ISD::UDIV, MVT::i32, Expand); 696 } 697 698 // FIXME: Also set divmod for SREM on EABI 699 setOperationAction(ISD::SREM, MVT::i32, Expand); 700 setOperationAction(ISD::UREM, MVT::i32, Expand); 701 // Register based DivRem for AEABI (RTABI 4.2) 702 if (Subtarget->isTargetAEABI()) { 703 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod"); 704 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod"); 705 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod"); 706 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod"); 707 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod"); 708 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod"); 709 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod"); 710 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod"); 711 712 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS); 713 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS); 714 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS); 715 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS); 716 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS); 717 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS); 718 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS); 719 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS); 720 721 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 722 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 723 } else { 724 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 725 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 726 } 727 728 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 729 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 730 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom); 731 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 732 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 733 734 setOperationAction(ISD::TRAP, MVT::Other, Legal); 735 736 // Use the default implementation. 737 setOperationAction(ISD::VASTART, MVT::Other, Custom); 738 setOperationAction(ISD::VAARG, MVT::Other, Expand); 739 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 740 setOperationAction(ISD::VAEND, MVT::Other, Expand); 741 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 742 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 743 744 if (!Subtarget->isTargetDarwin()) { 745 // Non-Darwin platforms may return values in these registers via the 746 // personality function. 747 setExceptionPointerRegister(ARM::R0); 748 setExceptionSelectorRegister(ARM::R1); 749 } 750 751 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 752 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 753 // the default expansion. 754 // FIXME: This should be checking for v6k, not just v6. 755 if (Subtarget->hasDataBarrier() || 756 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) { 757 // membarrier needs custom lowering; the rest are legal and handled 758 // normally. 759 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 760 // Custom lowering for 64-bit ops 761 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); 762 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); 763 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); 764 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); 765 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); 766 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); 767 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom); 768 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom); 769 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom); 770 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom); 771 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 772 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc. 773 setInsertFencesForAtomic(true); 774 } else { 775 // Set them all for expansion, which will force libcalls. 776 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand); 777 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 778 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 779 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 780 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 781 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 782 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 783 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 784 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 785 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 786 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 787 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 788 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 789 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 790 // Unordered/Monotonic case. 791 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 792 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 793 } 794 795 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 796 797 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 798 if (!Subtarget->hasV6Ops()) { 799 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 800 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 801 } 802 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 803 804 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 805 !Subtarget->isThumb1Only()) { 806 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 807 // iff target supports vfp2. 808 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 809 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 810 } 811 812 // We want to custom lower some of our intrinsics. 813 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 814 if (Subtarget->isTargetDarwin()) { 815 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 816 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 817 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 818 } 819 820 setOperationAction(ISD::SETCC, MVT::i32, Expand); 821 setOperationAction(ISD::SETCC, MVT::f32, Expand); 822 setOperationAction(ISD::SETCC, MVT::f64, Expand); 823 setOperationAction(ISD::SELECT, MVT::i32, Custom); 824 setOperationAction(ISD::SELECT, MVT::f32, Custom); 825 setOperationAction(ISD::SELECT, MVT::f64, Custom); 826 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 827 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 828 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 829 830 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 831 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 832 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 833 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 834 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 835 836 // We don't support sin/cos/fmod/copysign/pow 837 setOperationAction(ISD::FSIN, MVT::f64, Expand); 838 setOperationAction(ISD::FSIN, MVT::f32, Expand); 839 setOperationAction(ISD::FCOS, MVT::f32, Expand); 840 setOperationAction(ISD::FCOS, MVT::f64, Expand); 841 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 842 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 843 setOperationAction(ISD::FREM, MVT::f64, Expand); 844 setOperationAction(ISD::FREM, MVT::f32, Expand); 845 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 846 !Subtarget->isThumb1Only()) { 847 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 848 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 849 } 850 setOperationAction(ISD::FPOW, MVT::f64, Expand); 851 setOperationAction(ISD::FPOW, MVT::f32, Expand); 852 853 if (!Subtarget->hasVFP4()) { 854 setOperationAction(ISD::FMA, MVT::f64, Expand); 855 setOperationAction(ISD::FMA, MVT::f32, Expand); 856 } 857 858 // Various VFP goodness 859 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) { 860 // int <-> fp are custom expanded into bit_convert + ARMISD ops. 861 if (Subtarget->hasVFP2()) { 862 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 863 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 864 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 865 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 866 } 867 // Special handling for half-precision FP. 868 if (!Subtarget->hasFP16()) { 869 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand); 870 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand); 871 } 872 } 873 874 // We have target-specific dag combine patterns for the following nodes: 875 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 876 setTargetDAGCombine(ISD::ADD); 877 setTargetDAGCombine(ISD::SUB); 878 setTargetDAGCombine(ISD::MUL); 879 setTargetDAGCombine(ISD::AND); 880 setTargetDAGCombine(ISD::OR); 881 setTargetDAGCombine(ISD::XOR); 882 883 if (Subtarget->hasV6Ops()) 884 setTargetDAGCombine(ISD::SRL); 885 886 setStackPointerRegisterToSaveRestore(ARM::SP); 887 888 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() || 889 !Subtarget->hasVFP2()) 890 setSchedulingPreference(Sched::RegPressure); 891 else 892 setSchedulingPreference(Sched::Hybrid); 893 894 //// temporary - rewrite interface to use type 895 MaxStoresPerMemset = 8; 896 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4; 897 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 898 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2; 899 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 900 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2; 901 902 // On ARM arguments smaller than 4 bytes are extended, so all arguments 903 // are at least 4 bytes aligned. 904 setMinStackArgumentAlignment(4); 905 906 // Prefer likely predicted branches to selects on out-of-order cores. 907 PredictableSelectIsExpensive = Subtarget->isLikeA9(); 908 909 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 910 } 911 912 // FIXME: It might make sense to define the representative register class as the 913 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 914 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 915 // SPR's representative would be DPR_VFP2. This should work well if register 916 // pressure tracking were modified such that a register use would increment the 917 // pressure of the register class's representative and all of it's super 918 // classes' representatives transitively. We have not implemented this because 919 // of the difficulty prior to coalescing of modeling operand register classes 920 // due to the common occurrence of cross class copies and subregister insertions 921 // and extractions. 922 std::pair<const TargetRegisterClass*, uint8_t> 923 ARMTargetLowering::findRepresentativeClass(MVT VT) const{ 924 const TargetRegisterClass *RRC = 0; 925 uint8_t Cost = 1; 926 switch (VT.SimpleTy) { 927 default: 928 return TargetLowering::findRepresentativeClass(VT); 929 // Use DPR as representative register class for all floating point 930 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 931 // the cost is 1 for both f32 and f64. 932 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 933 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 934 RRC = &ARM::DPRRegClass; 935 // When NEON is used for SP, only half of the register file is available 936 // because operations that define both SP and DP results will be constrained 937 // to the VFP2 class (D0-D15). We currently model this constraint prior to 938 // coalescing by double-counting the SP regs. See the FIXME above. 939 if (Subtarget->useNEONForSinglePrecisionFP()) 940 Cost = 2; 941 break; 942 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 943 case MVT::v4f32: case MVT::v2f64: 944 RRC = &ARM::DPRRegClass; 945 Cost = 2; 946 break; 947 case MVT::v4i64: 948 RRC = &ARM::DPRRegClass; 949 Cost = 4; 950 break; 951 case MVT::v8i64: 952 RRC = &ARM::DPRRegClass; 953 Cost = 8; 954 break; 955 } 956 return std::make_pair(RRC, Cost); 957 } 958 959 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 960 switch (Opcode) { 961 default: return 0; 962 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 963 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN"; 964 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 965 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 966 case ARMISD::CALL: return "ARMISD::CALL"; 967 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 968 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 969 case ARMISD::tCALL: return "ARMISD::tCALL"; 970 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 971 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 972 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 973 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 974 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 975 case ARMISD::CMP: return "ARMISD::CMP"; 976 case ARMISD::CMN: return "ARMISD::CMN"; 977 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 978 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 979 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 980 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 981 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 982 983 case ARMISD::CMOV: return "ARMISD::CMOV"; 984 985 case ARMISD::RBIT: return "ARMISD::RBIT"; 986 987 case ARMISD::FTOSI: return "ARMISD::FTOSI"; 988 case ARMISD::FTOUI: return "ARMISD::FTOUI"; 989 case ARMISD::SITOF: return "ARMISD::SITOF"; 990 case ARMISD::UITOF: return "ARMISD::UITOF"; 991 992 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 993 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 994 case ARMISD::RRX: return "ARMISD::RRX"; 995 996 case ARMISD::ADDC: return "ARMISD::ADDC"; 997 case ARMISD::ADDE: return "ARMISD::ADDE"; 998 case ARMISD::SUBC: return "ARMISD::SUBC"; 999 case ARMISD::SUBE: return "ARMISD::SUBE"; 1000 1001 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1002 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1003 1004 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1005 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP"; 1006 1007 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1008 1009 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1010 1011 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1012 1013 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER"; 1014 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1015 1016 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1017 1018 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 1019 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 1020 case ARMISD::VCGE: return "ARMISD::VCGE"; 1021 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 1022 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 1023 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 1024 case ARMISD::VCGT: return "ARMISD::VCGT"; 1025 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 1026 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 1027 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 1028 case ARMISD::VTST: return "ARMISD::VTST"; 1029 1030 case ARMISD::VSHL: return "ARMISD::VSHL"; 1031 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 1032 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 1033 case ARMISD::VSHLLs: return "ARMISD::VSHLLs"; 1034 case ARMISD::VSHLLu: return "ARMISD::VSHLLu"; 1035 case ARMISD::VSHLLi: return "ARMISD::VSHLLi"; 1036 case ARMISD::VSHRN: return "ARMISD::VSHRN"; 1037 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 1038 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 1039 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 1040 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 1041 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 1042 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 1043 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 1044 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 1045 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 1046 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 1047 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 1048 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 1049 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1050 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1051 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1052 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1053 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1054 case ARMISD::VDUP: return "ARMISD::VDUP"; 1055 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1056 case ARMISD::VEXT: return "ARMISD::VEXT"; 1057 case ARMISD::VREV64: return "ARMISD::VREV64"; 1058 case ARMISD::VREV32: return "ARMISD::VREV32"; 1059 case ARMISD::VREV16: return "ARMISD::VREV16"; 1060 case ARMISD::VZIP: return "ARMISD::VZIP"; 1061 case ARMISD::VUZP: return "ARMISD::VUZP"; 1062 case ARMISD::VTRN: return "ARMISD::VTRN"; 1063 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1064 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1065 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1066 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1067 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1068 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1069 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1070 case ARMISD::FMAX: return "ARMISD::FMAX"; 1071 case ARMISD::FMIN: return "ARMISD::FMIN"; 1072 case ARMISD::VMAXNM: return "ARMISD::VMAX"; 1073 case ARMISD::VMINNM: return "ARMISD::VMIN"; 1074 case ARMISD::BFI: return "ARMISD::BFI"; 1075 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1076 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1077 case ARMISD::VBSL: return "ARMISD::VBSL"; 1078 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1079 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1080 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1081 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1082 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1083 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1084 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1085 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1086 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1087 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1088 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1089 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1090 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1091 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1092 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1093 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1094 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1095 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1096 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1097 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1098 1099 case ARMISD::ATOMADD64_DAG: return "ATOMADD64_DAG"; 1100 case ARMISD::ATOMSUB64_DAG: return "ATOMSUB64_DAG"; 1101 case ARMISD::ATOMOR64_DAG: return "ATOMOR64_DAG"; 1102 case ARMISD::ATOMXOR64_DAG: return "ATOMXOR64_DAG"; 1103 case ARMISD::ATOMAND64_DAG: return "ATOMAND64_DAG"; 1104 case ARMISD::ATOMNAND64_DAG: return "ATOMNAND64_DAG"; 1105 case ARMISD::ATOMSWAP64_DAG: return "ATOMSWAP64_DAG"; 1106 case ARMISD::ATOMCMPXCHG64_DAG: return "ATOMCMPXCHG64_DAG"; 1107 case ARMISD::ATOMMIN64_DAG: return "ATOMMIN64_DAG"; 1108 case ARMISD::ATOMUMIN64_DAG: return "ATOMUMIN64_DAG"; 1109 case ARMISD::ATOMMAX64_DAG: return "ATOMMAX64_DAG"; 1110 case ARMISD::ATOMUMAX64_DAG: return "ATOMUMAX64_DAG"; 1111 } 1112 } 1113 1114 EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { 1115 if (!VT.isVector()) return getPointerTy(); 1116 return VT.changeVectorElementTypeToInteger(); 1117 } 1118 1119 /// getRegClassFor - Return the register class that should be used for the 1120 /// specified value type. 1121 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const { 1122 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1123 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1124 // load / store 4 to 8 consecutive D registers. 1125 if (Subtarget->hasNEON()) { 1126 if (VT == MVT::v4i64) 1127 return &ARM::QQPRRegClass; 1128 if (VT == MVT::v8i64) 1129 return &ARM::QQQQPRRegClass; 1130 } 1131 return TargetLowering::getRegClassFor(VT); 1132 } 1133 1134 // Create a fast isel object. 1135 FastISel * 1136 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1137 const TargetLibraryInfo *libInfo) const { 1138 return ARM::createFastISel(funcInfo, libInfo); 1139 } 1140 1141 /// getMaximalGlobalOffset - Returns the maximal possible offset which can 1142 /// be used for loads / stores from the global. 1143 unsigned ARMTargetLowering::getMaximalGlobalOffset() const { 1144 return (Subtarget->isThumb1Only() ? 127 : 4095); 1145 } 1146 1147 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1148 unsigned NumVals = N->getNumValues(); 1149 if (!NumVals) 1150 return Sched::RegPressure; 1151 1152 for (unsigned i = 0; i != NumVals; ++i) { 1153 EVT VT = N->getValueType(i); 1154 if (VT == MVT::Glue || VT == MVT::Other) 1155 continue; 1156 if (VT.isFloatingPoint() || VT.isVector()) 1157 return Sched::ILP; 1158 } 1159 1160 if (!N->isMachineOpcode()) 1161 return Sched::RegPressure; 1162 1163 // Load are scheduled for latency even if there instruction itinerary 1164 // is not available. 1165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1166 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1167 1168 if (MCID.getNumDefs() == 0) 1169 return Sched::RegPressure; 1170 if (!Itins->isEmpty() && 1171 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1172 return Sched::ILP; 1173 1174 return Sched::RegPressure; 1175 } 1176 1177 //===----------------------------------------------------------------------===// 1178 // Lowering Code 1179 //===----------------------------------------------------------------------===// 1180 1181 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1182 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1183 switch (CC) { 1184 default: llvm_unreachable("Unknown condition code!"); 1185 case ISD::SETNE: return ARMCC::NE; 1186 case ISD::SETEQ: return ARMCC::EQ; 1187 case ISD::SETGT: return ARMCC::GT; 1188 case ISD::SETGE: return ARMCC::GE; 1189 case ISD::SETLT: return ARMCC::LT; 1190 case ISD::SETLE: return ARMCC::LE; 1191 case ISD::SETUGT: return ARMCC::HI; 1192 case ISD::SETUGE: return ARMCC::HS; 1193 case ISD::SETULT: return ARMCC::LO; 1194 case ISD::SETULE: return ARMCC::LS; 1195 } 1196 } 1197 1198 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1199 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1200 ARMCC::CondCodes &CondCode2) { 1201 CondCode2 = ARMCC::AL; 1202 switch (CC) { 1203 default: llvm_unreachable("Unknown FP condition!"); 1204 case ISD::SETEQ: 1205 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1206 case ISD::SETGT: 1207 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1208 case ISD::SETGE: 1209 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1210 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1211 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1212 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1213 case ISD::SETO: CondCode = ARMCC::VC; break; 1214 case ISD::SETUO: CondCode = ARMCC::VS; break; 1215 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1216 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1217 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1218 case ISD::SETLT: 1219 case ISD::SETULT: CondCode = ARMCC::LT; break; 1220 case ISD::SETLE: 1221 case ISD::SETULE: CondCode = ARMCC::LE; break; 1222 case ISD::SETNE: 1223 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1224 } 1225 } 1226 1227 //===----------------------------------------------------------------------===// 1228 // Calling Convention Implementation 1229 //===----------------------------------------------------------------------===// 1230 1231 #include "ARMGenCallingConv.inc" 1232 1233 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the 1234 /// given CallingConvention value. 1235 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1236 bool Return, 1237 bool isVarArg) const { 1238 switch (CC) { 1239 default: 1240 llvm_unreachable("Unsupported calling convention"); 1241 case CallingConv::Fast: 1242 if (Subtarget->hasVFP2() && !isVarArg) { 1243 if (!Subtarget->isAAPCS_ABI()) 1244 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1245 // For AAPCS ABI targets, just use VFP variant of the calling convention. 1246 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1247 } 1248 // Fallthrough 1249 case CallingConv::C: { 1250 // Use target triple & subtarget features to do actual dispatch. 1251 if (!Subtarget->isAAPCS_ABI()) 1252 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1253 else if (Subtarget->hasVFP2() && 1254 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1255 !isVarArg) 1256 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1257 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1258 } 1259 case CallingConv::ARM_AAPCS_VFP: 1260 if (!isVarArg) 1261 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1262 // Fallthrough 1263 case CallingConv::ARM_AAPCS: 1264 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1265 case CallingConv::ARM_APCS: 1266 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1267 case CallingConv::GHC: 1268 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1269 } 1270 } 1271 1272 /// LowerCallResult - Lower the result values of a call into the 1273 /// appropriate copies out of appropriate physical registers. 1274 SDValue 1275 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1276 CallingConv::ID CallConv, bool isVarArg, 1277 const SmallVectorImpl<ISD::InputArg> &Ins, 1278 SDLoc dl, SelectionDAG &DAG, 1279 SmallVectorImpl<SDValue> &InVals, 1280 bool isThisReturn, SDValue ThisVal) const { 1281 1282 // Assign locations to each value returned by this call. 1283 SmallVector<CCValAssign, 16> RVLocs; 1284 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1285 getTargetMachine(), RVLocs, *DAG.getContext(), Call); 1286 CCInfo.AnalyzeCallResult(Ins, 1287 CCAssignFnForNode(CallConv, /* Return*/ true, 1288 isVarArg)); 1289 1290 // Copy all of the result registers out of their specified physreg. 1291 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1292 CCValAssign VA = RVLocs[i]; 1293 1294 // Pass 'this' value directly from the argument to return value, to avoid 1295 // reg unit interference 1296 if (i == 0 && isThisReturn) { 1297 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1298 "unexpected return calling convention register assignment"); 1299 InVals.push_back(ThisVal); 1300 continue; 1301 } 1302 1303 SDValue Val; 1304 if (VA.needsCustom()) { 1305 // Handle f64 or half of a v2f64. 1306 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1307 InFlag); 1308 Chain = Lo.getValue(1); 1309 InFlag = Lo.getValue(2); 1310 VA = RVLocs[++i]; // skip ahead to next loc 1311 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1312 InFlag); 1313 Chain = Hi.getValue(1); 1314 InFlag = Hi.getValue(2); 1315 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1316 1317 if (VA.getLocVT() == MVT::v2f64) { 1318 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1319 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1320 DAG.getConstant(0, MVT::i32)); 1321 1322 VA = RVLocs[++i]; // skip ahead to next loc 1323 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1324 Chain = Lo.getValue(1); 1325 InFlag = Lo.getValue(2); 1326 VA = RVLocs[++i]; // skip ahead to next loc 1327 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1328 Chain = Hi.getValue(1); 1329 InFlag = Hi.getValue(2); 1330 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1331 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1332 DAG.getConstant(1, MVT::i32)); 1333 } 1334 } else { 1335 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1336 InFlag); 1337 Chain = Val.getValue(1); 1338 InFlag = Val.getValue(2); 1339 } 1340 1341 switch (VA.getLocInfo()) { 1342 default: llvm_unreachable("Unknown loc info!"); 1343 case CCValAssign::Full: break; 1344 case CCValAssign::BCvt: 1345 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1346 break; 1347 } 1348 1349 InVals.push_back(Val); 1350 } 1351 1352 return Chain; 1353 } 1354 1355 /// LowerMemOpCallTo - Store the argument to the stack. 1356 SDValue 1357 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, 1358 SDValue StackPtr, SDValue Arg, 1359 SDLoc dl, SelectionDAG &DAG, 1360 const CCValAssign &VA, 1361 ISD::ArgFlagsTy Flags) const { 1362 unsigned LocMemOffset = VA.getLocMemOffset(); 1363 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); 1364 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1365 return DAG.getStore(Chain, dl, Arg, PtrOff, 1366 MachinePointerInfo::getStack(LocMemOffset), 1367 false, false, 0); 1368 } 1369 1370 void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG, 1371 SDValue Chain, SDValue &Arg, 1372 RegsToPassVector &RegsToPass, 1373 CCValAssign &VA, CCValAssign &NextVA, 1374 SDValue &StackPtr, 1375 SmallVectorImpl<SDValue> &MemOpChains, 1376 ISD::ArgFlagsTy Flags) const { 1377 1378 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1379 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1380 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd)); 1381 1382 if (NextVA.isRegLoc()) 1383 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1))); 1384 else { 1385 assert(NextVA.isMemLoc()); 1386 if (StackPtr.getNode() == 0) 1387 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1388 1389 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1), 1390 dl, DAG, NextVA, 1391 Flags)); 1392 } 1393 } 1394 1395 /// LowerCall - Lowering a call into a callseq_start <- 1396 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1397 /// nodes. 1398 SDValue 1399 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1400 SmallVectorImpl<SDValue> &InVals) const { 1401 SelectionDAG &DAG = CLI.DAG; 1402 SDLoc &dl = CLI.DL; 1403 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1404 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1405 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1406 SDValue Chain = CLI.Chain; 1407 SDValue Callee = CLI.Callee; 1408 bool &isTailCall = CLI.IsTailCall; 1409 CallingConv::ID CallConv = CLI.CallConv; 1410 bool doesNotRet = CLI.DoesNotReturn; 1411 bool isVarArg = CLI.IsVarArg; 1412 1413 MachineFunction &MF = DAG.getMachineFunction(); 1414 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1415 bool isThisReturn = false; 1416 bool isSibCall = false; 1417 // Disable tail calls if they're not supported. 1418 if (!EnableARMTailCalls && !Subtarget->supportsTailCall()) 1419 isTailCall = false; 1420 if (isTailCall) { 1421 // Check if it's really possible to do a tail call. 1422 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1423 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(), 1424 Outs, OutVals, Ins, DAG); 1425 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1426 // detected sibcalls. 1427 if (isTailCall) { 1428 ++NumTailCalls; 1429 isSibCall = true; 1430 } 1431 } 1432 1433 // Analyze operands of the call, assigning locations to each operand. 1434 SmallVector<CCValAssign, 16> ArgLocs; 1435 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1436 getTargetMachine(), ArgLocs, *DAG.getContext(), Call); 1437 CCInfo.AnalyzeCallOperands(Outs, 1438 CCAssignFnForNode(CallConv, /* Return*/ false, 1439 isVarArg)); 1440 1441 // Get a count of how many bytes are to be pushed on the stack. 1442 unsigned NumBytes = CCInfo.getNextStackOffset(); 1443 1444 // For tail calls, memory operands are available in our caller's stack. 1445 if (isSibCall) 1446 NumBytes = 0; 1447 1448 // Adjust the stack pointer for the new arguments... 1449 // These operations are automatically eliminated by the prolog/epilog pass 1450 if (!isSibCall) 1451 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), 1452 dl); 1453 1454 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1455 1456 RegsToPassVector RegsToPass; 1457 SmallVector<SDValue, 8> MemOpChains; 1458 1459 // Walk the register/memloc assignments, inserting copies/loads. In the case 1460 // of tail call optimization, arguments are handled later. 1461 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1462 i != e; 1463 ++i, ++realArgIdx) { 1464 CCValAssign &VA = ArgLocs[i]; 1465 SDValue Arg = OutVals[realArgIdx]; 1466 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1467 bool isByVal = Flags.isByVal(); 1468 1469 // Promote the value if needed. 1470 switch (VA.getLocInfo()) { 1471 default: llvm_unreachable("Unknown loc info!"); 1472 case CCValAssign::Full: break; 1473 case CCValAssign::SExt: 1474 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1475 break; 1476 case CCValAssign::ZExt: 1477 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1478 break; 1479 case CCValAssign::AExt: 1480 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1481 break; 1482 case CCValAssign::BCvt: 1483 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1484 break; 1485 } 1486 1487 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1488 if (VA.needsCustom()) { 1489 if (VA.getLocVT() == MVT::v2f64) { 1490 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1491 DAG.getConstant(0, MVT::i32)); 1492 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1493 DAG.getConstant(1, MVT::i32)); 1494 1495 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1496 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1497 1498 VA = ArgLocs[++i]; // skip ahead to next loc 1499 if (VA.isRegLoc()) { 1500 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1501 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1502 } else { 1503 assert(VA.isMemLoc()); 1504 1505 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1506 dl, DAG, VA, Flags)); 1507 } 1508 } else { 1509 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1510 StackPtr, MemOpChains, Flags); 1511 } 1512 } else if (VA.isRegLoc()) { 1513 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) { 1514 assert(VA.getLocVT() == MVT::i32 && 1515 "unexpected calling convention register assignment"); 1516 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 1517 "unexpected use of 'returned'"); 1518 isThisReturn = true; 1519 } 1520 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1521 } else if (isByVal) { 1522 assert(VA.isMemLoc()); 1523 unsigned offset = 0; 1524 1525 // True if this byval aggregate will be split between registers 1526 // and memory. 1527 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 1528 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed(); 1529 1530 if (CurByValIdx < ByValArgsCount) { 1531 1532 unsigned RegBegin, RegEnd; 1533 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 1534 1535 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 1536 unsigned int i, j; 1537 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 1538 SDValue Const = DAG.getConstant(4*i, MVT::i32); 1539 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1540 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1541 MachinePointerInfo(), 1542 false, false, false, 0); 1543 MemOpChains.push_back(Load.getValue(1)); 1544 RegsToPass.push_back(std::make_pair(j, Load)); 1545 } 1546 1547 // If parameter size outsides register area, "offset" value 1548 // helps us to calculate stack slot for remained part properly. 1549 offset = RegEnd - RegBegin; 1550 1551 CCInfo.nextInRegsParam(); 1552 } 1553 1554 if (Flags.getByValSize() > 4*offset) { 1555 unsigned LocMemOffset = VA.getLocMemOffset(); 1556 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset); 1557 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, 1558 StkPtrOff); 1559 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset); 1560 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset); 1561 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, 1562 MVT::i32); 1563 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32); 1564 1565 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 1566 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 1567 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 1568 Ops, array_lengthof(Ops))); 1569 } 1570 } else if (!isSibCall) { 1571 assert(VA.isMemLoc()); 1572 1573 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1574 dl, DAG, VA, Flags)); 1575 } 1576 } 1577 1578 if (!MemOpChains.empty()) 1579 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1580 &MemOpChains[0], MemOpChains.size()); 1581 1582 // Build a sequence of copy-to-reg nodes chained together with token chain 1583 // and flag operands which copy the outgoing args into the appropriate regs. 1584 SDValue InFlag; 1585 // Tail call byval lowering might overwrite argument registers so in case of 1586 // tail call optimization the copies to registers are lowered later. 1587 if (!isTailCall) 1588 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1589 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1590 RegsToPass[i].second, InFlag); 1591 InFlag = Chain.getValue(1); 1592 } 1593 1594 // For tail calls lower the arguments to the 'real' stack slot. 1595 if (isTailCall) { 1596 // Force all the incoming stack arguments to be loaded from the stack 1597 // before any new outgoing arguments are stored to the stack, because the 1598 // outgoing stack slots may alias the incoming argument stack slots, and 1599 // the alias isn't otherwise explicit. This is slightly more conservative 1600 // than necessary, because it means that each store effectively depends 1601 // on every argument instead of just those arguments it would clobber. 1602 1603 // Do not flag preceding copytoreg stuff together with the following stuff. 1604 InFlag = SDValue(); 1605 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1606 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1607 RegsToPass[i].second, InFlag); 1608 InFlag = Chain.getValue(1); 1609 } 1610 InFlag = SDValue(); 1611 } 1612 1613 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1614 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1615 // node so that legalize doesn't hack it. 1616 bool isDirect = false; 1617 bool isARMFunc = false; 1618 bool isLocalARMFunc = false; 1619 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1620 1621 if (EnableARMLongCalls) { 1622 assert (getTargetMachine().getRelocationModel() == Reloc::Static 1623 && "long-calls with non-static relocation model!"); 1624 // Handle a global address or an external symbol. If it's not one of 1625 // those, the target's already in a register, so we don't need to do 1626 // anything extra. 1627 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1628 const GlobalValue *GV = G->getGlobal(); 1629 // Create a constant pool entry for the callee address 1630 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1631 ARMConstantPoolValue *CPV = 1632 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 1633 1634 // Get the address of the callee into a register 1635 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1636 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1637 Callee = DAG.getLoad(getPointerTy(), dl, 1638 DAG.getEntryNode(), CPAddr, 1639 MachinePointerInfo::getConstantPool(), 1640 false, false, false, 0); 1641 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 1642 const char *Sym = S->getSymbol(); 1643 1644 // Create a constant pool entry for the callee address 1645 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1646 ARMConstantPoolValue *CPV = 1647 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1648 ARMPCLabelIndex, 0); 1649 // Get the address of the callee into a register 1650 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1651 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1652 Callee = DAG.getLoad(getPointerTy(), dl, 1653 DAG.getEntryNode(), CPAddr, 1654 MachinePointerInfo::getConstantPool(), 1655 false, false, false, 0); 1656 } 1657 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1658 const GlobalValue *GV = G->getGlobal(); 1659 isDirect = true; 1660 bool isExt = GV->isDeclaration() || GV->isWeakForLinker(); 1661 bool isStub = (isExt && Subtarget->isTargetDarwin()) && 1662 getTargetMachine().getRelocationModel() != Reloc::Static; 1663 isARMFunc = !Subtarget->isThumb() || isStub; 1664 // ARM call to a local ARM function is predicable. 1665 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking); 1666 // tBX takes a register source operand. 1667 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1668 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1669 ARMConstantPoolValue *CPV = 1670 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4); 1671 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1672 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1673 Callee = DAG.getLoad(getPointerTy(), dl, 1674 DAG.getEntryNode(), CPAddr, 1675 MachinePointerInfo::getConstantPool(), 1676 false, false, false, 0); 1677 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1678 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1679 getPointerTy(), Callee, PICLabel); 1680 } else { 1681 // On ELF targets for PIC code, direct calls should go through the PLT 1682 unsigned OpFlags = 0; 1683 if (Subtarget->isTargetELF() && 1684 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1685 OpFlags = ARMII::MO_PLT; 1686 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags); 1687 } 1688 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1689 isDirect = true; 1690 bool isStub = Subtarget->isTargetDarwin() && 1691 getTargetMachine().getRelocationModel() != Reloc::Static; 1692 isARMFunc = !Subtarget->isThumb() || isStub; 1693 // tBX takes a register source operand. 1694 const char *Sym = S->getSymbol(); 1695 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1696 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1697 ARMConstantPoolValue *CPV = 1698 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1699 ARMPCLabelIndex, 4); 1700 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1701 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1702 Callee = DAG.getLoad(getPointerTy(), dl, 1703 DAG.getEntryNode(), CPAddr, 1704 MachinePointerInfo::getConstantPool(), 1705 false, false, false, 0); 1706 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1707 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1708 getPointerTy(), Callee, PICLabel); 1709 } else { 1710 unsigned OpFlags = 0; 1711 // On ELF targets for PIC code, direct calls should go through the PLT 1712 if (Subtarget->isTargetELF() && 1713 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1714 OpFlags = ARMII::MO_PLT; 1715 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags); 1716 } 1717 } 1718 1719 // FIXME: handle tail calls differently. 1720 unsigned CallOpc; 1721 bool HasMinSizeAttr = MF.getFunction()->getAttributes(). 1722 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize); 1723 if (Subtarget->isThumb()) { 1724 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 1725 CallOpc = ARMISD::CALL_NOLINK; 1726 else 1727 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; 1728 } else { 1729 if (!isDirect && !Subtarget->hasV5TOps()) 1730 CallOpc = ARMISD::CALL_NOLINK; 1731 else if (doesNotRet && isDirect && Subtarget->hasRAS() && 1732 // Emit regular call when code size is the priority 1733 !HasMinSizeAttr) 1734 // "mov lr, pc; b _foo" to avoid confusing the RSP 1735 CallOpc = ARMISD::CALL_NOLINK; 1736 else 1737 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 1738 } 1739 1740 std::vector<SDValue> Ops; 1741 Ops.push_back(Chain); 1742 Ops.push_back(Callee); 1743 1744 // Add argument registers to the end of the list so that they are known live 1745 // into the call. 1746 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1747 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1748 RegsToPass[i].second.getValueType())); 1749 1750 // Add a register mask operand representing the call-preserved registers. 1751 const uint32_t *Mask; 1752 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 1753 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI); 1754 if (isThisReturn) { 1755 // For 'this' returns, use the R0-preserving mask if applicable 1756 Mask = ARI->getThisReturnPreservedMask(CallConv); 1757 if (!Mask) { 1758 // Set isThisReturn to false if the calling convention is not one that 1759 // allows 'returned' to be modeled in this way, so LowerCallResult does 1760 // not try to pass 'this' straight through 1761 isThisReturn = false; 1762 Mask = ARI->getCallPreservedMask(CallConv); 1763 } 1764 } else 1765 Mask = ARI->getCallPreservedMask(CallConv); 1766 1767 assert(Mask && "Missing call preserved mask for calling convention"); 1768 Ops.push_back(DAG.getRegisterMask(Mask)); 1769 1770 if (InFlag.getNode()) 1771 Ops.push_back(InFlag); 1772 1773 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1774 if (isTailCall) 1775 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size()); 1776 1777 // Returns a chain and a flag for retval copy to use. 1778 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size()); 1779 InFlag = Chain.getValue(1); 1780 1781 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1782 DAG.getIntPtrConstant(0, true), InFlag, dl); 1783 if (!Ins.empty()) 1784 InFlag = Chain.getValue(1); 1785 1786 // Handle result values, copying them out of physregs into vregs that we 1787 // return. 1788 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 1789 InVals, isThisReturn, 1790 isThisReturn ? OutVals[0] : SDValue()); 1791 } 1792 1793 /// HandleByVal - Every parameter *after* a byval parameter is passed 1794 /// on the stack. Remember the next parameter register to allocate, 1795 /// and then confiscate the rest of the parameter registers to insure 1796 /// this. 1797 void 1798 ARMTargetLowering::HandleByVal( 1799 CCState *State, unsigned &size, unsigned Align) const { 1800 unsigned reg = State->AllocateReg(GPRArgRegs, 4); 1801 assert((State->getCallOrPrologue() == Prologue || 1802 State->getCallOrPrologue() == Call) && 1803 "unhandled ParmContext"); 1804 1805 // For in-prologue parameters handling, we also introduce stack offset 1806 // for byval registers: see CallingConvLower.cpp, CCState::HandleByVal. 1807 // This behaviour outsides AAPCS rules (5.5 Parameters Passing) of how 1808 // NSAA should be evaluted (NSAA means "next stacked argument address"). 1809 // So: NextStackOffset = NSAAOffset + SizeOfByValParamsStoredInRegs. 1810 // Then: NSAAOffset = NextStackOffset - SizeOfByValParamsStoredInRegs. 1811 unsigned NSAAOffset = State->getNextStackOffset(); 1812 if (State->getCallOrPrologue() != Call) { 1813 for (unsigned i = 0, e = State->getInRegsParamsCount(); i != e; ++i) { 1814 unsigned RB, RE; 1815 State->getInRegsParamInfo(i, RB, RE); 1816 assert(NSAAOffset >= (RE-RB)*4 && 1817 "Stack offset for byval regs doesn't introduced anymore?"); 1818 NSAAOffset -= (RE-RB)*4; 1819 } 1820 } 1821 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) { 1822 if (Subtarget->isAAPCS_ABI() && Align > 4) { 1823 unsigned AlignInRegs = Align / 4; 1824 unsigned Waste = (ARM::R4 - reg) % AlignInRegs; 1825 for (unsigned i = 0; i < Waste; ++i) 1826 reg = State->AllocateReg(GPRArgRegs, 4); 1827 } 1828 if (reg != 0) { 1829 unsigned excess = 4 * (ARM::R4 - reg); 1830 1831 // Special case when NSAA != SP and parameter size greater than size of 1832 // all remained GPR regs. In that case we can't split parameter, we must 1833 // send it to stack. We also must set NCRN to R4, so waste all 1834 // remained registers. 1835 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) { 1836 while (State->AllocateReg(GPRArgRegs, 4)) 1837 ; 1838 return; 1839 } 1840 1841 // First register for byval parameter is the first register that wasn't 1842 // allocated before this method call, so it would be "reg". 1843 // If parameter is small enough to be saved in range [reg, r4), then 1844 // the end (first after last) register would be reg + param-size-in-regs, 1845 // else parameter would be splitted between registers and stack, 1846 // end register would be r4 in this case. 1847 unsigned ByValRegBegin = reg; 1848 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4; 1849 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 1850 // Note, first register is allocated in the beginning of function already, 1851 // allocate remained amount of registers we need. 1852 for (unsigned i = reg+1; i != ByValRegEnd; ++i) 1853 State->AllocateReg(GPRArgRegs, 4); 1854 // At a call site, a byval parameter that is split between 1855 // registers and memory needs its size truncated here. In a 1856 // function prologue, such byval parameters are reassembled in 1857 // memory, and are not truncated. 1858 if (State->getCallOrPrologue() == Call) { 1859 // Make remained size equal to 0 in case, when 1860 // the whole structure may be stored into registers. 1861 if (size < excess) 1862 size = 0; 1863 else 1864 size -= excess; 1865 } 1866 } 1867 } 1868 } 1869 1870 /// MatchingStackOffset - Return true if the given stack call argument is 1871 /// already available in the same position (relatively) of the caller's 1872 /// incoming argument stack. 1873 static 1874 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 1875 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, 1876 const TargetInstrInfo *TII) { 1877 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; 1878 int FI = INT_MAX; 1879 if (Arg.getOpcode() == ISD::CopyFromReg) { 1880 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 1881 if (!TargetRegisterInfo::isVirtualRegister(VR)) 1882 return false; 1883 MachineInstr *Def = MRI->getVRegDef(VR); 1884 if (!Def) 1885 return false; 1886 if (!Flags.isByVal()) { 1887 if (!TII->isLoadFromStackSlot(Def, FI)) 1888 return false; 1889 } else { 1890 return false; 1891 } 1892 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 1893 if (Flags.isByVal()) 1894 // ByVal argument is passed in as a pointer but it's now being 1895 // dereferenced. e.g. 1896 // define @foo(%struct.X* %A) { 1897 // tail call @bar(%struct.X* byval %A) 1898 // } 1899 return false; 1900 SDValue Ptr = Ld->getBasePtr(); 1901 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 1902 if (!FINode) 1903 return false; 1904 FI = FINode->getIndex(); 1905 } else 1906 return false; 1907 1908 assert(FI != INT_MAX); 1909 if (!MFI->isFixedObjectIndex(FI)) 1910 return false; 1911 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); 1912 } 1913 1914 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 1915 /// for tail call optimization. Targets which want to do tail call 1916 /// optimization should implement this function. 1917 bool 1918 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 1919 CallingConv::ID CalleeCC, 1920 bool isVarArg, 1921 bool isCalleeStructRet, 1922 bool isCallerStructRet, 1923 const SmallVectorImpl<ISD::OutputArg> &Outs, 1924 const SmallVectorImpl<SDValue> &OutVals, 1925 const SmallVectorImpl<ISD::InputArg> &Ins, 1926 SelectionDAG& DAG) const { 1927 const Function *CallerF = DAG.getMachineFunction().getFunction(); 1928 CallingConv::ID CallerCC = CallerF->getCallingConv(); 1929 bool CCMatch = CallerCC == CalleeCC; 1930 1931 // Look for obvious safe cases to perform tail call optimization that do not 1932 // require ABI changes. This is what gcc calls sibcall. 1933 1934 // Do not sibcall optimize vararg calls unless the call site is not passing 1935 // any arguments. 1936 if (isVarArg && !Outs.empty()) 1937 return false; 1938 1939 // Also avoid sibcall optimization if either caller or callee uses struct 1940 // return semantics. 1941 if (isCalleeStructRet || isCallerStructRet) 1942 return false; 1943 1944 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo:: 1945 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as 1946 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation 1947 // support in the assembler and linker to be used. This would need to be 1948 // fixed to fully support tail calls in Thumb1. 1949 // 1950 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take 1951 // LR. This means if we need to reload LR, it takes an extra instructions, 1952 // which outweighs the value of the tail call; but here we don't know yet 1953 // whether LR is going to be used. Probably the right approach is to 1954 // generate the tail call here and turn it back into CALL/RET in 1955 // emitEpilogue if LR is used. 1956 1957 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls, 1958 // but we need to make sure there are enough registers; the only valid 1959 // registers are the 4 used for parameters. We don't currently do this 1960 // case. 1961 if (Subtarget->isThumb1Only()) 1962 return false; 1963 1964 // If the calling conventions do not match, then we'd better make sure the 1965 // results are returned in the same way as what the caller expects. 1966 if (!CCMatch) { 1967 SmallVector<CCValAssign, 16> RVLocs1; 1968 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), 1969 getTargetMachine(), RVLocs1, *DAG.getContext(), Call); 1970 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg)); 1971 1972 SmallVector<CCValAssign, 16> RVLocs2; 1973 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), 1974 getTargetMachine(), RVLocs2, *DAG.getContext(), Call); 1975 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg)); 1976 1977 if (RVLocs1.size() != RVLocs2.size()) 1978 return false; 1979 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 1980 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 1981 return false; 1982 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 1983 return false; 1984 if (RVLocs1[i].isRegLoc()) { 1985 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 1986 return false; 1987 } else { 1988 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 1989 return false; 1990 } 1991 } 1992 } 1993 1994 // If Caller's vararg or byval argument has been split between registers and 1995 // stack, do not perform tail call, since part of the argument is in caller's 1996 // local frame. 1997 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction(). 1998 getInfo<ARMFunctionInfo>(); 1999 if (AFI_Caller->getArgRegsSaveSize()) 2000 return false; 2001 2002 // If the callee takes no arguments then go on to check the results of the 2003 // call. 2004 if (!Outs.empty()) { 2005 // Check if stack adjustment is needed. For now, do not do this if any 2006 // argument is passed on the stack. 2007 SmallVector<CCValAssign, 16> ArgLocs; 2008 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), 2009 getTargetMachine(), ArgLocs, *DAG.getContext(), Call); 2010 CCInfo.AnalyzeCallOperands(Outs, 2011 CCAssignFnForNode(CalleeCC, false, isVarArg)); 2012 if (CCInfo.getNextStackOffset()) { 2013 MachineFunction &MF = DAG.getMachineFunction(); 2014 2015 // Check if the arguments are already laid out in the right way as 2016 // the caller's fixed stack objects. 2017 MachineFrameInfo *MFI = MF.getFrameInfo(); 2018 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2019 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 2020 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2021 i != e; 2022 ++i, ++realArgIdx) { 2023 CCValAssign &VA = ArgLocs[i]; 2024 EVT RegVT = VA.getLocVT(); 2025 SDValue Arg = OutVals[realArgIdx]; 2026 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2027 if (VA.getLocInfo() == CCValAssign::Indirect) 2028 return false; 2029 if (VA.needsCustom()) { 2030 // f64 and vector types are split into multiple registers or 2031 // register/stack-slot combinations. The types will not match 2032 // the registers; give up on memory f64 refs until we figure 2033 // out what to do about this. 2034 if (!VA.isRegLoc()) 2035 return false; 2036 if (!ArgLocs[++i].isRegLoc()) 2037 return false; 2038 if (RegVT == MVT::v2f64) { 2039 if (!ArgLocs[++i].isRegLoc()) 2040 return false; 2041 if (!ArgLocs[++i].isRegLoc()) 2042 return false; 2043 } 2044 } else if (!VA.isRegLoc()) { 2045 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2046 MFI, MRI, TII)) 2047 return false; 2048 } 2049 } 2050 } 2051 } 2052 2053 return true; 2054 } 2055 2056 bool 2057 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2058 MachineFunction &MF, bool isVarArg, 2059 const SmallVectorImpl<ISD::OutputArg> &Outs, 2060 LLVMContext &Context) const { 2061 SmallVector<CCValAssign, 16> RVLocs; 2062 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context); 2063 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true, 2064 isVarArg)); 2065 } 2066 2067 SDValue 2068 ARMTargetLowering::LowerReturn(SDValue Chain, 2069 CallingConv::ID CallConv, bool isVarArg, 2070 const SmallVectorImpl<ISD::OutputArg> &Outs, 2071 const SmallVectorImpl<SDValue> &OutVals, 2072 SDLoc dl, SelectionDAG &DAG) const { 2073 2074 // CCValAssign - represent the assignment of the return value to a location. 2075 SmallVector<CCValAssign, 16> RVLocs; 2076 2077 // CCState - Info about the registers and stack slots. 2078 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 2079 getTargetMachine(), RVLocs, *DAG.getContext(), Call); 2080 2081 // Analyze outgoing return values. 2082 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true, 2083 isVarArg)); 2084 2085 SDValue Flag; 2086 SmallVector<SDValue, 4> RetOps; 2087 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2088 2089 // Copy the result values into the output registers. 2090 for (unsigned i = 0, realRVLocIdx = 0; 2091 i != RVLocs.size(); 2092 ++i, ++realRVLocIdx) { 2093 CCValAssign &VA = RVLocs[i]; 2094 assert(VA.isRegLoc() && "Can only return in registers!"); 2095 2096 SDValue Arg = OutVals[realRVLocIdx]; 2097 2098 switch (VA.getLocInfo()) { 2099 default: llvm_unreachable("Unknown loc info!"); 2100 case CCValAssign::Full: break; 2101 case CCValAssign::BCvt: 2102 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2103 break; 2104 } 2105 2106 if (VA.needsCustom()) { 2107 if (VA.getLocVT() == MVT::v2f64) { 2108 // Extract the first half and return it in two registers. 2109 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2110 DAG.getConstant(0, MVT::i32)); 2111 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2112 DAG.getVTList(MVT::i32, MVT::i32), Half); 2113 2114 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag); 2115 Flag = Chain.getValue(1); 2116 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2117 VA = RVLocs[++i]; // skip ahead to next loc 2118 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2119 HalfGPRs.getValue(1), Flag); 2120 Flag = Chain.getValue(1); 2121 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2122 VA = RVLocs[++i]; // skip ahead to next loc 2123 2124 // Extract the 2nd half and fall through to handle it as an f64 value. 2125 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2126 DAG.getConstant(1, MVT::i32)); 2127 } 2128 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2129 // available. 2130 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2131 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1); 2132 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag); 2133 Flag = Chain.getValue(1); 2134 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2135 VA = RVLocs[++i]; // skip ahead to next loc 2136 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1), 2137 Flag); 2138 } else 2139 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2140 2141 // Guarantee that all emitted copies are 2142 // stuck together, avoiding something bad. 2143 Flag = Chain.getValue(1); 2144 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2145 } 2146 2147 // Update chain and glue. 2148 RetOps[0] = Chain; 2149 if (Flag.getNode()) 2150 RetOps.push_back(Flag); 2151 2152 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, 2153 RetOps.data(), RetOps.size()); 2154 } 2155 2156 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2157 if (N->getNumValues() != 1) 2158 return false; 2159 if (!N->hasNUsesOfValue(1, 0)) 2160 return false; 2161 2162 SDValue TCChain = Chain; 2163 SDNode *Copy = *N->use_begin(); 2164 if (Copy->getOpcode() == ISD::CopyToReg) { 2165 // If the copy has a glue operand, we conservatively assume it isn't safe to 2166 // perform a tail call. 2167 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2168 return false; 2169 TCChain = Copy->getOperand(0); 2170 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2171 SDNode *VMov = Copy; 2172 // f64 returned in a pair of GPRs. 2173 SmallPtrSet<SDNode*, 2> Copies; 2174 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2175 UI != UE; ++UI) { 2176 if (UI->getOpcode() != ISD::CopyToReg) 2177 return false; 2178 Copies.insert(*UI); 2179 } 2180 if (Copies.size() > 2) 2181 return false; 2182 2183 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2184 UI != UE; ++UI) { 2185 SDValue UseChain = UI->getOperand(0); 2186 if (Copies.count(UseChain.getNode())) 2187 // Second CopyToReg 2188 Copy = *UI; 2189 else 2190 // First CopyToReg 2191 TCChain = UseChain; 2192 } 2193 } else if (Copy->getOpcode() == ISD::BITCAST) { 2194 // f32 returned in a single GPR. 2195 if (!Copy->hasOneUse()) 2196 return false; 2197 Copy = *Copy->use_begin(); 2198 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2199 return false; 2200 TCChain = Copy->getOperand(0); 2201 } else { 2202 return false; 2203 } 2204 2205 bool HasRet = false; 2206 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2207 UI != UE; ++UI) { 2208 if (UI->getOpcode() != ARMISD::RET_FLAG) 2209 return false; 2210 HasRet = true; 2211 } 2212 2213 if (!HasRet) 2214 return false; 2215 2216 Chain = TCChain; 2217 return true; 2218 } 2219 2220 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 2221 if (!EnableARMTailCalls && !Subtarget->supportsTailCall()) 2222 return false; 2223 2224 if (!CI->isTailCall()) 2225 return false; 2226 2227 return !Subtarget->isThumb1Only(); 2228 } 2229 2230 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2231 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2232 // one of the above mentioned nodes. It has to be wrapped because otherwise 2233 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2234 // be used to form addressing mode. These wrapped nodes will be selected 2235 // into MOVi. 2236 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 2237 EVT PtrVT = Op.getValueType(); 2238 // FIXME there is no actual debug info here 2239 SDLoc dl(Op); 2240 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2241 SDValue Res; 2242 if (CP->isMachineConstantPoolEntry()) 2243 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2244 CP->getAlignment()); 2245 else 2246 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2247 CP->getAlignment()); 2248 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2249 } 2250 2251 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2252 return MachineJumpTableInfo::EK_Inline; 2253 } 2254 2255 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2256 SelectionDAG &DAG) const { 2257 MachineFunction &MF = DAG.getMachineFunction(); 2258 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2259 unsigned ARMPCLabelIndex = 0; 2260 SDLoc DL(Op); 2261 EVT PtrVT = getPointerTy(); 2262 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2263 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2264 SDValue CPAddr; 2265 if (RelocM == Reloc::Static) { 2266 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2267 } else { 2268 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2269 ARMPCLabelIndex = AFI->createPICLabelUId(); 2270 ARMConstantPoolValue *CPV = 2271 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2272 ARMCP::CPBlockAddress, PCAdj); 2273 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2274 } 2275 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2276 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr, 2277 MachinePointerInfo::getConstantPool(), 2278 false, false, false, 0); 2279 if (RelocM == Reloc::Static) 2280 return Result; 2281 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2282 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2283 } 2284 2285 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2286 SDValue 2287 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2288 SelectionDAG &DAG) const { 2289 SDLoc dl(GA); 2290 EVT PtrVT = getPointerTy(); 2291 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2292 MachineFunction &MF = DAG.getMachineFunction(); 2293 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2294 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2295 ARMConstantPoolValue *CPV = 2296 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2297 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2298 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2299 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2300 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, 2301 MachinePointerInfo::getConstantPool(), 2302 false, false, false, 0); 2303 SDValue Chain = Argument.getValue(1); 2304 2305 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2306 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2307 2308 // call __tls_get_addr. 2309 ArgListTy Args; 2310 ArgListEntry Entry; 2311 Entry.Node = Argument; 2312 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2313 Args.push_back(Entry); 2314 // FIXME: is there useful debug info available here? 2315 TargetLowering::CallLoweringInfo CLI(Chain, 2316 (Type *) Type::getInt32Ty(*DAG.getContext()), 2317 false, false, false, false, 2318 0, CallingConv::C, /*isTailCall=*/false, 2319 /*doesNotRet=*/false, /*isReturnValueUsed=*/true, 2320 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl); 2321 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2322 return CallResult.first; 2323 } 2324 2325 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2326 // "local exec" model. 2327 SDValue 2328 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2329 SelectionDAG &DAG, 2330 TLSModel::Model model) const { 2331 const GlobalValue *GV = GA->getGlobal(); 2332 SDLoc dl(GA); 2333 SDValue Offset; 2334 SDValue Chain = DAG.getEntryNode(); 2335 EVT PtrVT = getPointerTy(); 2336 // Get the Thread Pointer 2337 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2338 2339 if (model == TLSModel::InitialExec) { 2340 MachineFunction &MF = DAG.getMachineFunction(); 2341 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2342 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2343 // Initial exec model. 2344 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2345 ARMConstantPoolValue *CPV = 2346 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2347 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2348 true); 2349 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2350 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2351 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2352 MachinePointerInfo::getConstantPool(), 2353 false, false, false, 0); 2354 Chain = Offset.getValue(1); 2355 2356 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2357 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2358 2359 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2360 MachinePointerInfo::getConstantPool(), 2361 false, false, false, 0); 2362 } else { 2363 // local exec model 2364 assert(model == TLSModel::LocalExec); 2365 ARMConstantPoolValue *CPV = 2366 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2367 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2368 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2369 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2370 MachinePointerInfo::getConstantPool(), 2371 false, false, false, 0); 2372 } 2373 2374 // The address of the thread local variable is the add of the thread 2375 // pointer with the offset of the variable. 2376 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2377 } 2378 2379 SDValue 2380 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2381 // TODO: implement the "local dynamic" model 2382 assert(Subtarget->isTargetELF() && 2383 "TLS not implemented for non-ELF targets"); 2384 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2385 2386 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 2387 2388 switch (model) { 2389 case TLSModel::GeneralDynamic: 2390 case TLSModel::LocalDynamic: 2391 return LowerToTLSGeneralDynamicModel(GA, DAG); 2392 case TLSModel::InitialExec: 2393 case TLSModel::LocalExec: 2394 return LowerToTLSExecModels(GA, DAG, model); 2395 } 2396 llvm_unreachable("bogus TLS model"); 2397 } 2398 2399 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 2400 SelectionDAG &DAG) const { 2401 EVT PtrVT = getPointerTy(); 2402 SDLoc dl(Op); 2403 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2404 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 2405 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility(); 2406 ARMConstantPoolValue *CPV = 2407 ARMConstantPoolConstant::Create(GV, 2408 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT); 2409 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2410 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2411 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 2412 CPAddr, 2413 MachinePointerInfo::getConstantPool(), 2414 false, false, false, 0); 2415 SDValue Chain = Result.getValue(1); 2416 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); 2417 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT); 2418 if (!UseGOTOFF) 2419 Result = DAG.getLoad(PtrVT, dl, Chain, Result, 2420 MachinePointerInfo::getGOT(), 2421 false, false, false, 0); 2422 return Result; 2423 } 2424 2425 // If we have T2 ops, we can materialize the address directly via movt/movw 2426 // pair. This is always cheaper. 2427 if (Subtarget->useMovt()) { 2428 ++NumMovwMovt; 2429 // FIXME: Once remat is capable of dealing with instructions with register 2430 // operands, expand this into two nodes. 2431 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2432 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2433 } else { 2434 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2435 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2436 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2437 MachinePointerInfo::getConstantPool(), 2438 false, false, false, 0); 2439 } 2440 } 2441 2442 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 2443 SelectionDAG &DAG) const { 2444 EVT PtrVT = getPointerTy(); 2445 SDLoc dl(Op); 2446 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2447 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2448 2449 // FIXME: Enable this for static codegen when tool issues are fixed. Also 2450 // update ARMFastISel::ARMMaterializeGV. 2451 if (Subtarget->useMovt() && RelocM != Reloc::Static) { 2452 ++NumMovwMovt; 2453 // FIXME: Once remat is capable of dealing with instructions with register 2454 // operands, expand this into two nodes. 2455 if (RelocM == Reloc::Static) 2456 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2457 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2458 2459 unsigned Wrapper = (RelocM == Reloc::PIC_) 2460 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN; 2461 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, 2462 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2463 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2464 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 2465 MachinePointerInfo::getGOT(), 2466 false, false, false, 0); 2467 return Result; 2468 } 2469 2470 unsigned ARMPCLabelIndex = 0; 2471 SDValue CPAddr; 2472 if (RelocM == Reloc::Static) { 2473 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2474 } else { 2475 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 2476 ARMPCLabelIndex = AFI->createPICLabelUId(); 2477 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8); 2478 ARMConstantPoolValue *CPV = 2479 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 2480 PCAdj); 2481 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2482 } 2483 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2484 2485 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2486 MachinePointerInfo::getConstantPool(), 2487 false, false, false, 0); 2488 SDValue Chain = Result.getValue(1); 2489 2490 if (RelocM == Reloc::PIC_) { 2491 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2492 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2493 } 2494 2495 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2496 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(), 2497 false, false, false, 0); 2498 2499 return Result; 2500 } 2501 2502 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, 2503 SelectionDAG &DAG) const { 2504 assert(Subtarget->isTargetELF() && 2505 "GLOBAL OFFSET TABLE not implemented for non-ELF targets"); 2506 MachineFunction &MF = DAG.getMachineFunction(); 2507 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2508 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2509 EVT PtrVT = getPointerTy(); 2510 SDLoc dl(Op); 2511 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2512 ARMConstantPoolValue *CPV = 2513 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_", 2514 ARMPCLabelIndex, PCAdj); 2515 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2516 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2517 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2518 MachinePointerInfo::getConstantPool(), 2519 false, false, false, 0); 2520 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2521 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2522 } 2523 2524 SDValue 2525 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 2526 SDLoc dl(Op); 2527 SDValue Val = DAG.getConstant(0, MVT::i32); 2528 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 2529 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 2530 Op.getOperand(1), Val); 2531 } 2532 2533 SDValue 2534 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 2535 SDLoc dl(Op); 2536 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 2537 Op.getOperand(1), DAG.getConstant(0, MVT::i32)); 2538 } 2539 2540 SDValue 2541 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 2542 const ARMSubtarget *Subtarget) const { 2543 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2544 SDLoc dl(Op); 2545 switch (IntNo) { 2546 default: return SDValue(); // Don't custom lower most intrinsics. 2547 case Intrinsic::arm_thread_pointer: { 2548 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2549 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2550 } 2551 case Intrinsic::eh_sjlj_lsda: { 2552 MachineFunction &MF = DAG.getMachineFunction(); 2553 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2554 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2555 EVT PtrVT = getPointerTy(); 2556 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2557 SDValue CPAddr; 2558 unsigned PCAdj = (RelocM != Reloc::PIC_) 2559 ? 0 : (Subtarget->isThumb() ? 4 : 8); 2560 ARMConstantPoolValue *CPV = 2561 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex, 2562 ARMCP::CPLSDA, PCAdj); 2563 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2564 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2565 SDValue Result = 2566 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2567 MachinePointerInfo::getConstantPool(), 2568 false, false, false, 0); 2569 2570 if (RelocM == Reloc::PIC_) { 2571 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2572 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2573 } 2574 return Result; 2575 } 2576 case Intrinsic::arm_neon_vmulls: 2577 case Intrinsic::arm_neon_vmullu: { 2578 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 2579 ? ARMISD::VMULLs : ARMISD::VMULLu; 2580 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2581 Op.getOperand(1), Op.getOperand(2)); 2582 } 2583 } 2584 } 2585 2586 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 2587 const ARMSubtarget *Subtarget) { 2588 // FIXME: handle "fence singlethread" more efficiently. 2589 SDLoc dl(Op); 2590 if (!Subtarget->hasDataBarrier()) { 2591 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2592 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2593 // here. 2594 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2595 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!"); 2596 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2597 DAG.getConstant(0, MVT::i32)); 2598 } 2599 2600 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 2601 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 2602 unsigned Domain = ARM_MB::ISH; 2603 if (Subtarget->isMClass()) { 2604 // Only a full system barrier exists in the M-class architectures. 2605 Domain = ARM_MB::SY; 2606 } else if (Subtarget->isSwift() && Ord == Release) { 2607 // Swift happens to implement ISHST barriers in a way that's compatible with 2608 // Release semantics but weaker than ISH so we'd be fools not to use 2609 // it. Beware: other processors probably don't! 2610 Domain = ARM_MB::ISHST; 2611 } 2612 2613 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0), 2614 DAG.getConstant(Domain, MVT::i32)); 2615 } 2616 2617 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 2618 const ARMSubtarget *Subtarget) { 2619 // ARM pre v5TE and Thumb1 does not have preload instructions. 2620 if (!(Subtarget->isThumb2() || 2621 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 2622 // Just preserve the chain. 2623 return Op.getOperand(0); 2624 2625 SDLoc dl(Op); 2626 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 2627 if (!isRead && 2628 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 2629 // ARMv7 with MP extension has PLDW. 2630 return Op.getOperand(0); 2631 2632 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 2633 if (Subtarget->isThumb()) { 2634 // Invert the bits. 2635 isRead = ~isRead & 1; 2636 isData = ~isData & 1; 2637 } 2638 2639 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 2640 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32), 2641 DAG.getConstant(isData, MVT::i32)); 2642 } 2643 2644 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 2645 MachineFunction &MF = DAG.getMachineFunction(); 2646 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 2647 2648 // vastart just stores the address of the VarArgsFrameIndex slot into the 2649 // memory location argument. 2650 SDLoc dl(Op); 2651 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2652 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2653 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2654 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2655 MachinePointerInfo(SV), false, false, 0); 2656 } 2657 2658 SDValue 2659 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA, 2660 SDValue &Root, SelectionDAG &DAG, 2661 SDLoc dl) const { 2662 MachineFunction &MF = DAG.getMachineFunction(); 2663 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2664 2665 const TargetRegisterClass *RC; 2666 if (AFI->isThumb1OnlyFunction()) 2667 RC = &ARM::tGPRRegClass; 2668 else 2669 RC = &ARM::GPRRegClass; 2670 2671 // Transform the arguments stored in physical registers into virtual ones. 2672 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2673 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2674 2675 SDValue ArgValue2; 2676 if (NextVA.isMemLoc()) { 2677 MachineFrameInfo *MFI = MF.getFrameInfo(); 2678 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true); 2679 2680 // Create load node to retrieve arguments from the stack. 2681 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2682 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, 2683 MachinePointerInfo::getFixedStack(FI), 2684 false, false, false, 0); 2685 } else { 2686 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 2687 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2688 } 2689 2690 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 2691 } 2692 2693 void 2694 ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF, 2695 unsigned InRegsParamRecordIdx, 2696 unsigned ArgSize, 2697 unsigned &ArgRegsSize, 2698 unsigned &ArgRegsSaveSize) 2699 const { 2700 unsigned NumGPRs; 2701 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 2702 unsigned RBegin, REnd; 2703 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 2704 NumGPRs = REnd - RBegin; 2705 } else { 2706 unsigned int firstUnalloced; 2707 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs, 2708 sizeof(GPRArgRegs) / 2709 sizeof(GPRArgRegs[0])); 2710 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0; 2711 } 2712 2713 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment(); 2714 ArgRegsSize = NumGPRs * 4; 2715 2716 // If parameter is split between stack and GPRs... 2717 if (NumGPRs && Align == 8 && 2718 (ArgRegsSize < ArgSize || 2719 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) { 2720 // Add padding for part of param recovered from GPRs, so 2721 // its last byte must be at address K*8 - 1. 2722 // We need to do it, since remained (stack) part of parameter has 2723 // stack alignment, and we need to "attach" "GPRs head" without gaps 2724 // to it: 2725 // Stack: 2726 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes... 2727 // [ [padding] [GPRs head] ] [ Tail passed via stack .... 2728 // 2729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2730 unsigned Padding = 2731 ((ArgRegsSize + AFI->getArgRegsSaveSize() + Align - 1) & ~(Align-1)) - 2732 (ArgRegsSize + AFI->getArgRegsSaveSize()); 2733 ArgRegsSaveSize = ArgRegsSize + Padding; 2734 } else 2735 // We don't need to extend regs save size for byval parameters if they 2736 // are passed via GPRs only. 2737 ArgRegsSaveSize = ArgRegsSize; 2738 } 2739 2740 // The remaining GPRs hold either the beginning of variable-argument 2741 // data, or the beginning of an aggregate passed by value (usually 2742 // byval). Either way, we allocate stack slots adjacent to the data 2743 // provided by our caller, and store the unallocated registers there. 2744 // If this is a variadic function, the va_list pointer will begin with 2745 // these values; otherwise, this reassembles a (byval) structure that 2746 // was split between registers and memory. 2747 // Return: The frame index registers were stored into. 2748 int 2749 ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 2750 SDLoc dl, SDValue &Chain, 2751 const Value *OrigArg, 2752 unsigned InRegsParamRecordIdx, 2753 unsigned OffsetFromOrigArg, 2754 unsigned ArgOffset, 2755 unsigned ArgSize, 2756 bool ForceMutable) const { 2757 2758 // Currently, two use-cases possible: 2759 // Case #1. Non var-args function, and we meet first byval parameter. 2760 // Setup first unallocated register as first byval register; 2761 // eat all remained registers 2762 // (these two actions are performed by HandleByVal method). 2763 // Then, here, we initialize stack frame with 2764 // "store-reg" instructions. 2765 // Case #2. Var-args function, that doesn't contain byval parameters. 2766 // The same: eat all remained unallocated registers, 2767 // initialize stack frame. 2768 2769 MachineFunction &MF = DAG.getMachineFunction(); 2770 MachineFrameInfo *MFI = MF.getFrameInfo(); 2771 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2772 unsigned firstRegToSaveIndex, lastRegToSaveIndex; 2773 unsigned RBegin, REnd; 2774 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 2775 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 2776 firstRegToSaveIndex = RBegin - ARM::R0; 2777 lastRegToSaveIndex = REnd - ARM::R0; 2778 } else { 2779 firstRegToSaveIndex = CCInfo.getFirstUnallocated 2780 (GPRArgRegs, array_lengthof(GPRArgRegs)); 2781 lastRegToSaveIndex = 4; 2782 } 2783 2784 unsigned ArgRegsSize, ArgRegsSaveSize; 2785 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize, 2786 ArgRegsSize, ArgRegsSaveSize); 2787 2788 // Store any by-val regs to their spots on the stack so that they may be 2789 // loaded by deferencing the result of formal parameter pointer or va_next. 2790 // Note: once stack area for byval/varargs registers 2791 // was initialized, it can't be initialized again. 2792 if (ArgRegsSaveSize) { 2793 2794 unsigned Padding = ArgRegsSaveSize - ArgRegsSize; 2795 2796 if (Padding) { 2797 assert(AFI->getStoredByValParamsPadding() == 0 && 2798 "The only parameter may be padded."); 2799 AFI->setStoredByValParamsPadding(Padding); 2800 } 2801 2802 int FrameIndex = MFI->CreateFixedObject( 2803 ArgRegsSaveSize, 2804 Padding + ArgOffset, 2805 false); 2806 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy()); 2807 2808 SmallVector<SDValue, 4> MemOps; 2809 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex; 2810 ++firstRegToSaveIndex, ++i) { 2811 const TargetRegisterClass *RC; 2812 if (AFI->isThumb1OnlyFunction()) 2813 RC = &ARM::tGPRRegClass; 2814 else 2815 RC = &ARM::GPRRegClass; 2816 2817 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC); 2818 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 2819 SDValue Store = 2820 DAG.getStore(Val.getValue(1), dl, Val, FIN, 2821 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i), 2822 false, false, 0); 2823 MemOps.push_back(Store); 2824 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 2825 DAG.getConstant(4, getPointerTy())); 2826 } 2827 2828 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize()); 2829 2830 if (!MemOps.empty()) 2831 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 2832 &MemOps[0], MemOps.size()); 2833 return FrameIndex; 2834 } else 2835 // This will point to the next argument passed via stack. 2836 return MFI->CreateFixedObject( 2837 4, AFI->getStoredByValParamsPadding() + ArgOffset, !ForceMutable); 2838 } 2839 2840 // Setup stack frame, the va_list pointer will start from. 2841 void 2842 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 2843 SDLoc dl, SDValue &Chain, 2844 unsigned ArgOffset, 2845 bool ForceMutable) const { 2846 MachineFunction &MF = DAG.getMachineFunction(); 2847 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2848 2849 // Try to store any remaining integer argument regs 2850 // to their spots on the stack so that they may be loaded by deferencing 2851 // the result of va_next. 2852 // If there is no regs to be stored, just point address after last 2853 // argument passed via stack. 2854 int FrameIndex = 2855 StoreByValRegs(CCInfo, DAG, dl, Chain, 0, CCInfo.getInRegsParamsCount(), 2856 0, ArgOffset, 0, ForceMutable); 2857 2858 AFI->setVarArgsFrameIndex(FrameIndex); 2859 } 2860 2861 SDValue 2862 ARMTargetLowering::LowerFormalArguments(SDValue Chain, 2863 CallingConv::ID CallConv, bool isVarArg, 2864 const SmallVectorImpl<ISD::InputArg> 2865 &Ins, 2866 SDLoc dl, SelectionDAG &DAG, 2867 SmallVectorImpl<SDValue> &InVals) 2868 const { 2869 MachineFunction &MF = DAG.getMachineFunction(); 2870 MachineFrameInfo *MFI = MF.getFrameInfo(); 2871 2872 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2873 2874 // Assign locations to all of the incoming arguments. 2875 SmallVector<CCValAssign, 16> ArgLocs; 2876 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 2877 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue); 2878 CCInfo.AnalyzeFormalArguments(Ins, 2879 CCAssignFnForNode(CallConv, /* Return*/ false, 2880 isVarArg)); 2881 2882 SmallVector<SDValue, 16> ArgValues; 2883 int lastInsIndex = -1; 2884 SDValue ArgValue; 2885 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin(); 2886 unsigned CurArgIdx = 0; 2887 2888 // Initially ArgRegsSaveSize is zero. 2889 // Then we increase this value each time we meet byval parameter. 2890 // We also increase this value in case of varargs function. 2891 AFI->setArgRegsSaveSize(0); 2892 2893 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2894 CCValAssign &VA = ArgLocs[i]; 2895 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx); 2896 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex; 2897 // Arguments stored in registers. 2898 if (VA.isRegLoc()) { 2899 EVT RegVT = VA.getLocVT(); 2900 2901 if (VA.needsCustom()) { 2902 // f64 and vector types are split up into multiple registers or 2903 // combinations of registers and stack slots. 2904 if (VA.getLocVT() == MVT::v2f64) { 2905 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 2906 Chain, DAG, dl); 2907 VA = ArgLocs[++i]; // skip ahead to next loc 2908 SDValue ArgValue2; 2909 if (VA.isMemLoc()) { 2910 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true); 2911 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2912 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 2913 MachinePointerInfo::getFixedStack(FI), 2914 false, false, false, 0); 2915 } else { 2916 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 2917 Chain, DAG, dl); 2918 } 2919 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 2920 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 2921 ArgValue, ArgValue1, DAG.getIntPtrConstant(0)); 2922 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 2923 ArgValue, ArgValue2, DAG.getIntPtrConstant(1)); 2924 } else 2925 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 2926 2927 } else { 2928 const TargetRegisterClass *RC; 2929 2930 if (RegVT == MVT::f32) 2931 RC = &ARM::SPRRegClass; 2932 else if (RegVT == MVT::f64) 2933 RC = &ARM::DPRRegClass; 2934 else if (RegVT == MVT::v2f64) 2935 RC = &ARM::QPRRegClass; 2936 else if (RegVT == MVT::i32) 2937 RC = AFI->isThumb1OnlyFunction() ? 2938 (const TargetRegisterClass*)&ARM::tGPRRegClass : 2939 (const TargetRegisterClass*)&ARM::GPRRegClass; 2940 else 2941 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 2942 2943 // Transform the arguments in physical registers into virtual ones. 2944 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2945 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 2946 } 2947 2948 // If this is an 8 or 16-bit value, it is really passed promoted 2949 // to 32 bits. Insert an assert[sz]ext to capture this, then 2950 // truncate to the right size. 2951 switch (VA.getLocInfo()) { 2952 default: llvm_unreachable("Unknown loc info!"); 2953 case CCValAssign::Full: break; 2954 case CCValAssign::BCvt: 2955 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 2956 break; 2957 case CCValAssign::SExt: 2958 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 2959 DAG.getValueType(VA.getValVT())); 2960 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 2961 break; 2962 case CCValAssign::ZExt: 2963 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 2964 DAG.getValueType(VA.getValVT())); 2965 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 2966 break; 2967 } 2968 2969 InVals.push_back(ArgValue); 2970 2971 } else { // VA.isRegLoc() 2972 2973 // sanity check 2974 assert(VA.isMemLoc()); 2975 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 2976 2977 int index = ArgLocs[i].getValNo(); 2978 2979 // Some Ins[] entries become multiple ArgLoc[] entries. 2980 // Process them only once. 2981 if (index != lastInsIndex) 2982 { 2983 ISD::ArgFlagsTy Flags = Ins[index].Flags; 2984 // FIXME: For now, all byval parameter objects are marked mutable. 2985 // This can be changed with more analysis. 2986 // In case of tail call optimization mark all arguments mutable. 2987 // Since they could be overwritten by lowering of arguments in case of 2988 // a tail call. 2989 if (Flags.isByVal()) { 2990 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed(); 2991 int FrameIndex = StoreByValRegs( 2992 CCInfo, DAG, dl, Chain, CurOrigArg, 2993 CurByValIndex, 2994 Ins[VA.getValNo()].PartOffset, 2995 VA.getLocMemOffset(), 2996 Flags.getByValSize(), 2997 true /*force mutable frames*/); 2998 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy())); 2999 CCInfo.nextInRegsParam(); 3000 } else { 3001 unsigned FIOffset = VA.getLocMemOffset() + 3002 AFI->getStoredByValParamsPadding(); 3003 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 3004 FIOffset, true); 3005 3006 // Create load nodes to retrieve arguments from the stack. 3007 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 3008 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 3009 MachinePointerInfo::getFixedStack(FI), 3010 false, false, false, 0)); 3011 } 3012 lastInsIndex = index; 3013 } 3014 } 3015 } 3016 3017 // varargs 3018 if (isVarArg) 3019 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 3020 CCInfo.getNextStackOffset()); 3021 3022 return Chain; 3023 } 3024 3025 /// isFloatingPointZero - Return true if this is +0.0. 3026 static bool isFloatingPointZero(SDValue Op) { 3027 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 3028 return CFP->getValueAPF().isPosZero(); 3029 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 3030 // Maybe this has already been legalized into the constant pool? 3031 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 3032 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 3033 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 3034 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 3035 return CFP->getValueAPF().isPosZero(); 3036 } 3037 } 3038 return false; 3039 } 3040 3041 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 3042 /// the given operands. 3043 SDValue 3044 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 3045 SDValue &ARMcc, SelectionDAG &DAG, 3046 SDLoc dl) const { 3047 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 3048 unsigned C = RHSC->getZExtValue(); 3049 if (!isLegalICmpImmediate(C)) { 3050 // Constant does not fit, try adjusting it by one? 3051 switch (CC) { 3052 default: break; 3053 case ISD::SETLT: 3054 case ISD::SETGE: 3055 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 3056 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 3057 RHS = DAG.getConstant(C-1, MVT::i32); 3058 } 3059 break; 3060 case ISD::SETULT: 3061 case ISD::SETUGE: 3062 if (C != 0 && isLegalICmpImmediate(C-1)) { 3063 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 3064 RHS = DAG.getConstant(C-1, MVT::i32); 3065 } 3066 break; 3067 case ISD::SETLE: 3068 case ISD::SETGT: 3069 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 3070 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 3071 RHS = DAG.getConstant(C+1, MVT::i32); 3072 } 3073 break; 3074 case ISD::SETULE: 3075 case ISD::SETUGT: 3076 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 3077 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 3078 RHS = DAG.getConstant(C+1, MVT::i32); 3079 } 3080 break; 3081 } 3082 } 3083 } 3084 3085 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3086 ARMISD::NodeType CompareType; 3087 switch (CondCode) { 3088 default: 3089 CompareType = ARMISD::CMP; 3090 break; 3091 case ARMCC::EQ: 3092 case ARMCC::NE: 3093 // Uses only Z Flag 3094 CompareType = ARMISD::CMPZ; 3095 break; 3096 } 3097 ARMcc = DAG.getConstant(CondCode, MVT::i32); 3098 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 3099 } 3100 3101 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 3102 SDValue 3103 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG, 3104 SDLoc dl) const { 3105 SDValue Cmp; 3106 if (!isFloatingPointZero(RHS)) 3107 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS); 3108 else 3109 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS); 3110 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 3111 } 3112 3113 /// duplicateCmp - Glue values can have only one use, so this function 3114 /// duplicates a comparison node. 3115 SDValue 3116 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 3117 unsigned Opc = Cmp.getOpcode(); 3118 SDLoc DL(Cmp); 3119 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 3120 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3121 3122 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 3123 Cmp = Cmp.getOperand(0); 3124 Opc = Cmp.getOpcode(); 3125 if (Opc == ARMISD::CMPFP) 3126 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3127 else { 3128 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 3129 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 3130 } 3131 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 3132 } 3133 3134 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 3135 SDValue Cond = Op.getOperand(0); 3136 SDValue SelectTrue = Op.getOperand(1); 3137 SDValue SelectFalse = Op.getOperand(2); 3138 SDLoc dl(Op); 3139 3140 // Convert: 3141 // 3142 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 3143 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 3144 // 3145 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 3146 const ConstantSDNode *CMOVTrue = 3147 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 3148 const ConstantSDNode *CMOVFalse = 3149 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3150 3151 if (CMOVTrue && CMOVFalse) { 3152 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 3153 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 3154 3155 SDValue True; 3156 SDValue False; 3157 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 3158 True = SelectTrue; 3159 False = SelectFalse; 3160 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 3161 True = SelectFalse; 3162 False = SelectTrue; 3163 } 3164 3165 if (True.getNode() && False.getNode()) { 3166 EVT VT = Op.getValueType(); 3167 SDValue ARMcc = Cond.getOperand(2); 3168 SDValue CCR = Cond.getOperand(3); 3169 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 3170 assert(True.getValueType() == VT); 3171 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp); 3172 } 3173 } 3174 } 3175 3176 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 3177 // undefined bits before doing a full-word comparison with zero. 3178 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 3179 DAG.getConstant(1, Cond.getValueType())); 3180 3181 return DAG.getSelectCC(dl, Cond, 3182 DAG.getConstant(0, Cond.getValueType()), 3183 SelectTrue, SelectFalse, ISD::SETNE); 3184 } 3185 3186 static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) { 3187 if (CC == ISD::SETNE) 3188 return ISD::SETEQ; 3189 return ISD::getSetCCSwappedOperands(CC); 3190 } 3191 3192 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 3193 bool &swpCmpOps, bool &swpVselOps) { 3194 // Start by selecting the GE condition code for opcodes that return true for 3195 // 'equality' 3196 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 3197 CC == ISD::SETULE) 3198 CondCode = ARMCC::GE; 3199 3200 // and GT for opcodes that return false for 'equality'. 3201 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 3202 CC == ISD::SETULT) 3203 CondCode = ARMCC::GT; 3204 3205 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 3206 // to swap the compare operands. 3207 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 3208 CC == ISD::SETULT) 3209 swpCmpOps = true; 3210 3211 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 3212 // If we have an unordered opcode, we need to swap the operands to the VSEL 3213 // instruction (effectively negating the condition). 3214 // 3215 // This also has the effect of swapping which one of 'less' or 'greater' 3216 // returns true, so we also swap the compare operands. It also switches 3217 // whether we return true for 'equality', so we compensate by picking the 3218 // opposite condition code to our original choice. 3219 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 3220 CC == ISD::SETUGT) { 3221 swpCmpOps = !swpCmpOps; 3222 swpVselOps = !swpVselOps; 3223 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 3224 } 3225 3226 // 'ordered' is 'anything but unordered', so use the VS condition code and 3227 // swap the VSEL operands. 3228 if (CC == ISD::SETO) { 3229 CondCode = ARMCC::VS; 3230 swpVselOps = true; 3231 } 3232 3233 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 3234 // code and swap the VSEL operands. 3235 if (CC == ISD::SETUNE) { 3236 CondCode = ARMCC::EQ; 3237 swpVselOps = true; 3238 } 3239 } 3240 3241 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 3242 EVT VT = Op.getValueType(); 3243 SDValue LHS = Op.getOperand(0); 3244 SDValue RHS = Op.getOperand(1); 3245 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 3246 SDValue TrueVal = Op.getOperand(2); 3247 SDValue FalseVal = Op.getOperand(3); 3248 SDLoc dl(Op); 3249 3250 if (LHS.getValueType() == MVT::i32) { 3251 // Try to generate VSEL on ARMv8. 3252 // The VSEL instruction can't use all the usual ARM condition 3253 // codes: it only has two bits to select the condition code, so it's 3254 // constrained to use only GE, GT, VS and EQ. 3255 // 3256 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 3257 // swap the operands of the previous compare instruction (effectively 3258 // inverting the compare condition, swapping 'less' and 'greater') and 3259 // sometimes need to swap the operands to the VSEL (which inverts the 3260 // condition in the sense of firing whenever the previous condition didn't) 3261 if (getSubtarget()->hasV8FP() && (TrueVal.getValueType() == MVT::f32 || 3262 TrueVal.getValueType() == MVT::f64)) { 3263 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3264 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 3265 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 3266 CC = getInverseCCForVSEL(CC); 3267 std::swap(TrueVal, FalseVal); 3268 } 3269 } 3270 3271 SDValue ARMcc; 3272 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3273 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3274 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 3275 Cmp); 3276 } 3277 3278 ARMCC::CondCodes CondCode, CondCode2; 3279 FPCCToARMCC(CC, CondCode, CondCode2); 3280 3281 // Try to generate VSEL on ARMv8. 3282 if (getSubtarget()->hasV8FP() && (TrueVal.getValueType() == MVT::f32 || 3283 TrueVal.getValueType() == MVT::f64)) { 3284 // We can select VMAXNM/VMINNM from a compare followed by a select with the 3285 // same operands, as follows: 3286 // c = fcmp [ogt, olt, ugt, ult] a, b 3287 // select c, a, b 3288 // We only do this in unsafe-fp-math, because signed zeros and NaNs are 3289 // handled differently than the original code sequence. 3290 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal && 3291 RHS == FalseVal) { 3292 if (CC == ISD::SETOGT || CC == ISD::SETUGT) 3293 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal); 3294 if (CC == ISD::SETOLT || CC == ISD::SETULT) 3295 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal); 3296 } 3297 3298 bool swpCmpOps = false; 3299 bool swpVselOps = false; 3300 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 3301 3302 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 3303 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 3304 if (swpCmpOps) 3305 std::swap(LHS, RHS); 3306 if (swpVselOps) 3307 std::swap(TrueVal, FalseVal); 3308 } 3309 } 3310 3311 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 3312 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3313 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3314 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 3315 ARMcc, CCR, Cmp); 3316 if (CondCode2 != ARMCC::AL) { 3317 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32); 3318 // FIXME: Needs another CMP because flag can have but one use. 3319 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 3320 Result = DAG.getNode(ARMISD::CMOV, dl, VT, 3321 Result, TrueVal, ARMcc2, CCR, Cmp2); 3322 } 3323 return Result; 3324 } 3325 3326 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 3327 /// to morph to an integer compare sequence. 3328 static bool canChangeToInt(SDValue Op, bool &SeenZero, 3329 const ARMSubtarget *Subtarget) { 3330 SDNode *N = Op.getNode(); 3331 if (!N->hasOneUse()) 3332 // Otherwise it requires moving the value from fp to integer registers. 3333 return false; 3334 if (!N->getNumValues()) 3335 return false; 3336 EVT VT = Op.getValueType(); 3337 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 3338 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 3339 // vmrs are very slow, e.g. cortex-a8. 3340 return false; 3341 3342 if (isFloatingPointZero(Op)) { 3343 SeenZero = true; 3344 return true; 3345 } 3346 return ISD::isNormalLoad(N); 3347 } 3348 3349 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 3350 if (isFloatingPointZero(Op)) 3351 return DAG.getConstant(0, MVT::i32); 3352 3353 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 3354 return DAG.getLoad(MVT::i32, SDLoc(Op), 3355 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(), 3356 Ld->isVolatile(), Ld->isNonTemporal(), 3357 Ld->isInvariant(), Ld->getAlignment()); 3358 3359 llvm_unreachable("Unknown VFP cmp argument!"); 3360 } 3361 3362 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 3363 SDValue &RetVal1, SDValue &RetVal2) { 3364 if (isFloatingPointZero(Op)) { 3365 RetVal1 = DAG.getConstant(0, MVT::i32); 3366 RetVal2 = DAG.getConstant(0, MVT::i32); 3367 return; 3368 } 3369 3370 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 3371 SDValue Ptr = Ld->getBasePtr(); 3372 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op), 3373 Ld->getChain(), Ptr, 3374 Ld->getPointerInfo(), 3375 Ld->isVolatile(), Ld->isNonTemporal(), 3376 Ld->isInvariant(), Ld->getAlignment()); 3377 3378 EVT PtrType = Ptr.getValueType(); 3379 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 3380 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op), 3381 PtrType, Ptr, DAG.getConstant(4, PtrType)); 3382 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op), 3383 Ld->getChain(), NewPtr, 3384 Ld->getPointerInfo().getWithOffset(4), 3385 Ld->isVolatile(), Ld->isNonTemporal(), 3386 Ld->isInvariant(), NewAlign); 3387 return; 3388 } 3389 3390 llvm_unreachable("Unknown VFP cmp argument!"); 3391 } 3392 3393 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 3394 /// f32 and even f64 comparisons to integer ones. 3395 SDValue 3396 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 3397 SDValue Chain = Op.getOperand(0); 3398 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3399 SDValue LHS = Op.getOperand(2); 3400 SDValue RHS = Op.getOperand(3); 3401 SDValue Dest = Op.getOperand(4); 3402 SDLoc dl(Op); 3403 3404 bool LHSSeenZero = false; 3405 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 3406 bool RHSSeenZero = false; 3407 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 3408 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 3409 // If unsafe fp math optimization is enabled and there are no other uses of 3410 // the CMP operands, and the condition code is EQ or NE, we can optimize it 3411 // to an integer comparison. 3412 if (CC == ISD::SETOEQ) 3413 CC = ISD::SETEQ; 3414 else if (CC == ISD::SETUNE) 3415 CC = ISD::SETNE; 3416 3417 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32); 3418 SDValue ARMcc; 3419 if (LHS.getValueType() == MVT::f32) { 3420 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3421 bitcastf32Toi32(LHS, DAG), Mask); 3422 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3423 bitcastf32Toi32(RHS, DAG), Mask); 3424 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3425 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3426 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3427 Chain, Dest, ARMcc, CCR, Cmp); 3428 } 3429 3430 SDValue LHS1, LHS2; 3431 SDValue RHS1, RHS2; 3432 expandf64Toi32(LHS, DAG, LHS1, LHS2); 3433 expandf64Toi32(RHS, DAG, RHS1, RHS2); 3434 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 3435 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 3436 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3437 ARMcc = DAG.getConstant(CondCode, MVT::i32); 3438 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3439 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 3440 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7); 3441 } 3442 3443 return SDValue(); 3444 } 3445 3446 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 3447 SDValue Chain = Op.getOperand(0); 3448 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3449 SDValue LHS = Op.getOperand(2); 3450 SDValue RHS = Op.getOperand(3); 3451 SDValue Dest = Op.getOperand(4); 3452 SDLoc dl(Op); 3453 3454 if (LHS.getValueType() == MVT::i32) { 3455 SDValue ARMcc; 3456 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3457 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3458 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3459 Chain, Dest, ARMcc, CCR, Cmp); 3460 } 3461 3462 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3463 3464 if (getTargetMachine().Options.UnsafeFPMath && 3465 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 3466 CC == ISD::SETNE || CC == ISD::SETUNE)) { 3467 SDValue Result = OptimizeVFPBrcond(Op, DAG); 3468 if (Result.getNode()) 3469 return Result; 3470 } 3471 3472 ARMCC::CondCodes CondCode, CondCode2; 3473 FPCCToARMCC(CC, CondCode, CondCode2); 3474 3475 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 3476 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3477 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3478 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3479 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 3480 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5); 3481 if (CondCode2 != ARMCC::AL) { 3482 ARMcc = DAG.getConstant(CondCode2, MVT::i32); 3483 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 3484 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5); 3485 } 3486 return Res; 3487 } 3488 3489 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 3490 SDValue Chain = Op.getOperand(0); 3491 SDValue Table = Op.getOperand(1); 3492 SDValue Index = Op.getOperand(2); 3493 SDLoc dl(Op); 3494 3495 EVT PTy = getPointerTy(); 3496 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 3497 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 3498 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy); 3499 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 3500 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId); 3501 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy)); 3502 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 3503 if (Subtarget->isThumb2()) { 3504 // Thumb2 uses a two-level jump. That is, it jumps into the jump table 3505 // which does another jump to the destination. This also makes it easier 3506 // to translate it to TBB / TBH later. 3507 // FIXME: This might not work if the function is extremely large. 3508 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 3509 Addr, Op.getOperand(2), JTI, UId); 3510 } 3511 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 3512 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 3513 MachinePointerInfo::getJumpTable(), 3514 false, false, false, 0); 3515 Chain = Addr.getValue(1); 3516 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table); 3517 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3518 } else { 3519 Addr = DAG.getLoad(PTy, dl, Chain, Addr, 3520 MachinePointerInfo::getJumpTable(), 3521 false, false, false, 0); 3522 Chain = Addr.getValue(1); 3523 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3524 } 3525 } 3526 3527 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3528 EVT VT = Op.getValueType(); 3529 SDLoc dl(Op); 3530 3531 if (Op.getValueType().getVectorElementType() == MVT::i32) { 3532 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 3533 return Op; 3534 return DAG.UnrollVectorOp(Op.getNode()); 3535 } 3536 3537 assert(Op.getOperand(0).getValueType() == MVT::v4f32 && 3538 "Invalid type for custom lowering!"); 3539 if (VT != MVT::v4i16) 3540 return DAG.UnrollVectorOp(Op.getNode()); 3541 3542 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0)); 3543 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 3544 } 3545 3546 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3547 EVT VT = Op.getValueType(); 3548 if (VT.isVector()) 3549 return LowerVectorFP_TO_INT(Op, DAG); 3550 3551 SDLoc dl(Op); 3552 unsigned Opc; 3553 3554 switch (Op.getOpcode()) { 3555 default: llvm_unreachable("Invalid opcode!"); 3556 case ISD::FP_TO_SINT: 3557 Opc = ARMISD::FTOSI; 3558 break; 3559 case ISD::FP_TO_UINT: 3560 Opc = ARMISD::FTOUI; 3561 break; 3562 } 3563 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0)); 3564 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 3565 } 3566 3567 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3568 EVT VT = Op.getValueType(); 3569 SDLoc dl(Op); 3570 3571 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 3572 if (VT.getVectorElementType() == MVT::f32) 3573 return Op; 3574 return DAG.UnrollVectorOp(Op.getNode()); 3575 } 3576 3577 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 3578 "Invalid type for custom lowering!"); 3579 if (VT != MVT::v4f32) 3580 return DAG.UnrollVectorOp(Op.getNode()); 3581 3582 unsigned CastOpc; 3583 unsigned Opc; 3584 switch (Op.getOpcode()) { 3585 default: llvm_unreachable("Invalid opcode!"); 3586 case ISD::SINT_TO_FP: 3587 CastOpc = ISD::SIGN_EXTEND; 3588 Opc = ISD::SINT_TO_FP; 3589 break; 3590 case ISD::UINT_TO_FP: 3591 CastOpc = ISD::ZERO_EXTEND; 3592 Opc = ISD::UINT_TO_FP; 3593 break; 3594 } 3595 3596 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 3597 return DAG.getNode(Opc, dl, VT, Op); 3598 } 3599 3600 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3601 EVT VT = Op.getValueType(); 3602 if (VT.isVector()) 3603 return LowerVectorINT_TO_FP(Op, DAG); 3604 3605 SDLoc dl(Op); 3606 unsigned Opc; 3607 3608 switch (Op.getOpcode()) { 3609 default: llvm_unreachable("Invalid opcode!"); 3610 case ISD::SINT_TO_FP: 3611 Opc = ARMISD::SITOF; 3612 break; 3613 case ISD::UINT_TO_FP: 3614 Opc = ARMISD::UITOF; 3615 break; 3616 } 3617 3618 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0)); 3619 return DAG.getNode(Opc, dl, VT, Op); 3620 } 3621 3622 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 3623 // Implement fcopysign with a fabs and a conditional fneg. 3624 SDValue Tmp0 = Op.getOperand(0); 3625 SDValue Tmp1 = Op.getOperand(1); 3626 SDLoc dl(Op); 3627 EVT VT = Op.getValueType(); 3628 EVT SrcVT = Tmp1.getValueType(); 3629 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 3630 Tmp0.getOpcode() == ARMISD::VMOVDRR; 3631 bool UseNEON = !InGPR && Subtarget->hasNEON(); 3632 3633 if (UseNEON) { 3634 // Use VBSL to copy the sign bit. 3635 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 3636 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 3637 DAG.getTargetConstant(EncodedVal, MVT::i32)); 3638 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 3639 if (VT == MVT::f64) 3640 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3641 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 3642 DAG.getConstant(32, MVT::i32)); 3643 else /*if (VT == MVT::f32)*/ 3644 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 3645 if (SrcVT == MVT::f32) { 3646 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 3647 if (VT == MVT::f64) 3648 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3649 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 3650 DAG.getConstant(32, MVT::i32)); 3651 } else if (VT == MVT::f32) 3652 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 3653 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 3654 DAG.getConstant(32, MVT::i32)); 3655 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 3656 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 3657 3658 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 3659 MVT::i32); 3660 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 3661 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 3662 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 3663 3664 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 3665 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 3666 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 3667 if (VT == MVT::f32) { 3668 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 3669 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 3670 DAG.getConstant(0, MVT::i32)); 3671 } else { 3672 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 3673 } 3674 3675 return Res; 3676 } 3677 3678 // Bitcast operand 1 to i32. 3679 if (SrcVT == MVT::f64) 3680 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 3681 &Tmp1, 1).getValue(1); 3682 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 3683 3684 // Or in the signbit with integer operations. 3685 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32); 3686 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32); 3687 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 3688 if (VT == MVT::f32) { 3689 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 3690 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 3691 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 3692 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 3693 } 3694 3695 // f64: Or the high part with signbit and then combine two parts. 3696 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 3697 &Tmp0, 1); 3698 SDValue Lo = Tmp0.getValue(0); 3699 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 3700 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 3701 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 3702 } 3703 3704 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 3705 MachineFunction &MF = DAG.getMachineFunction(); 3706 MachineFrameInfo *MFI = MF.getFrameInfo(); 3707 MFI->setReturnAddressIsTaken(true); 3708 3709 EVT VT = Op.getValueType(); 3710 SDLoc dl(Op); 3711 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3712 if (Depth) { 3713 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 3714 SDValue Offset = DAG.getConstant(4, MVT::i32); 3715 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 3716 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 3717 MachinePointerInfo(), false, false, false, 0); 3718 } 3719 3720 // Return LR, which contains the return address. Mark it an implicit live-in. 3721 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 3722 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 3723 } 3724 3725 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 3726 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 3727 MFI->setFrameAddressIsTaken(true); 3728 3729 EVT VT = Op.getValueType(); 3730 SDLoc dl(Op); // FIXME probably not meaningful 3731 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3732 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin()) 3733 ? ARM::R7 : ARM::R11; 3734 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 3735 while (Depth--) 3736 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 3737 MachinePointerInfo(), 3738 false, false, false, 0); 3739 return FrameAddr; 3740 } 3741 3742 /// Custom Expand long vector extensions, where size(DestVec) > 2*size(SrcVec), 3743 /// and size(DestVec) > 128-bits. 3744 /// This is achieved by doing the one extension from the SrcVec, splitting the 3745 /// result, extending these parts, and then concatenating these into the 3746 /// destination. 3747 static SDValue ExpandVectorExtension(SDNode *N, SelectionDAG &DAG) { 3748 SDValue Op = N->getOperand(0); 3749 EVT SrcVT = Op.getValueType(); 3750 EVT DestVT = N->getValueType(0); 3751 3752 assert(DestVT.getSizeInBits() > 128 && 3753 "Custom sext/zext expansion needs >128-bit vector."); 3754 // If this is a normal length extension, use the default expansion. 3755 if (SrcVT.getSizeInBits()*4 != DestVT.getSizeInBits() && 3756 SrcVT.getSizeInBits()*8 != DestVT.getSizeInBits()) 3757 return SDValue(); 3758 3759 SDLoc dl(N); 3760 unsigned SrcEltSize = SrcVT.getVectorElementType().getSizeInBits(); 3761 unsigned DestEltSize = DestVT.getVectorElementType().getSizeInBits(); 3762 unsigned NumElts = SrcVT.getVectorNumElements(); 3763 LLVMContext &Ctx = *DAG.getContext(); 3764 SDValue Mid, SplitLo, SplitHi, ExtLo, ExtHi; 3765 3766 EVT MidVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, SrcEltSize*2), 3767 NumElts); 3768 EVT SplitVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, SrcEltSize*2), 3769 NumElts/2); 3770 EVT ExtVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, DestEltSize), 3771 NumElts/2); 3772 3773 Mid = DAG.getNode(N->getOpcode(), dl, MidVT, Op); 3774 SplitLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SplitVT, Mid, 3775 DAG.getIntPtrConstant(0)); 3776 SplitHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SplitVT, Mid, 3777 DAG.getIntPtrConstant(NumElts/2)); 3778 ExtLo = DAG.getNode(N->getOpcode(), dl, ExtVT, SplitLo); 3779 ExtHi = DAG.getNode(N->getOpcode(), dl, ExtVT, SplitHi); 3780 return DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, ExtLo, ExtHi); 3781 } 3782 3783 /// ExpandBITCAST - If the target supports VFP, this function is called to 3784 /// expand a bit convert where either the source or destination type is i64 to 3785 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 3786 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 3787 /// vectors), since the legalizer won't know what to do with that. 3788 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 3789 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3790 SDLoc dl(N); 3791 SDValue Op = N->getOperand(0); 3792 3793 // This function is only supposed to be called for i64 types, either as the 3794 // source or destination of the bit convert. 3795 EVT SrcVT = Op.getValueType(); 3796 EVT DstVT = N->getValueType(0); 3797 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 3798 "ExpandBITCAST called for non-i64 type"); 3799 3800 // Turn i64->f64 into VMOVDRR. 3801 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 3802 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 3803 DAG.getConstant(0, MVT::i32)); 3804 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 3805 DAG.getConstant(1, MVT::i32)); 3806 return DAG.getNode(ISD::BITCAST, dl, DstVT, 3807 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 3808 } 3809 3810 // Turn f64->i64 into VMOVRRD. 3811 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 3812 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 3813 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1); 3814 // Merge the pieces into a single i64 value. 3815 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 3816 } 3817 3818 return SDValue(); 3819 } 3820 3821 /// getZeroVector - Returns a vector of specified type with all zero elements. 3822 /// Zero vectors are used to represent vector negation and in those cases 3823 /// will be implemented with the NEON VNEG instruction. However, VNEG does 3824 /// not support i64 elements, so sometimes the zero vectors will need to be 3825 /// explicitly constructed. Regardless, use a canonical VMOV to create the 3826 /// zero vector. 3827 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) { 3828 assert(VT.isVector() && "Expected a vector type"); 3829 // The canonical modified immediate encoding of a zero vector is....0! 3830 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32); 3831 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 3832 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 3833 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 3834 } 3835 3836 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 3837 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 3838 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 3839 SelectionDAG &DAG) const { 3840 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 3841 EVT VT = Op.getValueType(); 3842 unsigned VTBits = VT.getSizeInBits(); 3843 SDLoc dl(Op); 3844 SDValue ShOpLo = Op.getOperand(0); 3845 SDValue ShOpHi = Op.getOperand(1); 3846 SDValue ShAmt = Op.getOperand(2); 3847 SDValue ARMcc; 3848 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 3849 3850 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 3851 3852 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 3853 DAG.getConstant(VTBits, MVT::i32), ShAmt); 3854 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 3855 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 3856 DAG.getConstant(VTBits, MVT::i32)); 3857 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 3858 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 3859 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 3860 3861 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3862 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 3863 ARMcc, DAG, dl); 3864 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 3865 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, 3866 CCR, Cmp); 3867 3868 SDValue Ops[2] = { Lo, Hi }; 3869 return DAG.getMergeValues(Ops, 2, dl); 3870 } 3871 3872 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 3873 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 3874 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 3875 SelectionDAG &DAG) const { 3876 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 3877 EVT VT = Op.getValueType(); 3878 unsigned VTBits = VT.getSizeInBits(); 3879 SDLoc dl(Op); 3880 SDValue ShOpLo = Op.getOperand(0); 3881 SDValue ShOpHi = Op.getOperand(1); 3882 SDValue ShAmt = Op.getOperand(2); 3883 SDValue ARMcc; 3884 3885 assert(Op.getOpcode() == ISD::SHL_PARTS); 3886 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 3887 DAG.getConstant(VTBits, MVT::i32), ShAmt); 3888 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 3889 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 3890 DAG.getConstant(VTBits, MVT::i32)); 3891 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 3892 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 3893 3894 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 3895 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3896 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 3897 ARMcc, DAG, dl); 3898 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 3899 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc, 3900 CCR, Cmp); 3901 3902 SDValue Ops[2] = { Lo, Hi }; 3903 return DAG.getMergeValues(Ops, 2, dl); 3904 } 3905 3906 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 3907 SelectionDAG &DAG) const { 3908 // The rounding mode is in bits 23:22 of the FPSCR. 3909 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 3910 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 3911 // so that the shift + and get folded into a bitfield extract. 3912 SDLoc dl(Op); 3913 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 3914 DAG.getConstant(Intrinsic::arm_get_fpscr, 3915 MVT::i32)); 3916 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 3917 DAG.getConstant(1U << 22, MVT::i32)); 3918 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 3919 DAG.getConstant(22, MVT::i32)); 3920 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 3921 DAG.getConstant(3, MVT::i32)); 3922 } 3923 3924 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 3925 const ARMSubtarget *ST) { 3926 EVT VT = N->getValueType(0); 3927 SDLoc dl(N); 3928 3929 if (!ST->hasV6T2Ops()) 3930 return SDValue(); 3931 3932 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0)); 3933 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 3934 } 3935 3936 /// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count 3937 /// for each 16-bit element from operand, repeated. The basic idea is to 3938 /// leverage vcnt to get the 8-bit counts, gather and add the results. 3939 /// 3940 /// Trace for v4i16: 3941 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 3942 /// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element) 3943 /// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi) 3944 /// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6] 3945 /// [b0 b1 b2 b3 b4 b5 b6 b7] 3946 /// +[b1 b0 b3 b2 b5 b4 b7 b6] 3947 /// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0, 3948 /// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits) 3949 static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) { 3950 EVT VT = N->getValueType(0); 3951 SDLoc DL(N); 3952 3953 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 3954 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0)); 3955 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0); 3956 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1); 3957 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2); 3958 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3); 3959 } 3960 3961 /// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the 3962 /// bit-count for each 16-bit element from the operand. We need slightly 3963 /// different sequencing for v4i16 and v8i16 to stay within NEON's available 3964 /// 64/128-bit registers. 3965 /// 3966 /// Trace for v4i16: 3967 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 3968 /// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi) 3969 /// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ] 3970 /// v4i16:Extracted = [k0 k1 k2 k3 ] 3971 static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) { 3972 EVT VT = N->getValueType(0); 3973 SDLoc DL(N); 3974 3975 SDValue BitCounts = getCTPOP16BitCounts(N, DAG); 3976 if (VT.is64BitVector()) { 3977 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts); 3978 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended, 3979 DAG.getIntPtrConstant(0)); 3980 } else { 3981 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8, 3982 BitCounts, DAG.getIntPtrConstant(0)); 3983 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted); 3984 } 3985 } 3986 3987 /// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the 3988 /// bit-count for each 32-bit element from the operand. The idea here is 3989 /// to split the vector into 16-bit elements, leverage the 16-bit count 3990 /// routine, and then combine the results. 3991 /// 3992 /// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged): 3993 /// input = [v0 v1 ] (vi: 32-bit elements) 3994 /// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1]) 3995 /// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi) 3996 /// vrev: N0 = [k1 k0 k3 k2 ] 3997 /// [k0 k1 k2 k3 ] 3998 /// N1 =+[k1 k0 k3 k2 ] 3999 /// [k0 k2 k1 k3 ] 4000 /// N2 =+[k1 k3 k0 k2 ] 4001 /// [k0 k2 k1 k3 ] 4002 /// Extended =+[k1 k3 k0 k2 ] 4003 /// [k0 k2 ] 4004 /// Extracted=+[k1 k3 ] 4005 /// 4006 static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) { 4007 EVT VT = N->getValueType(0); 4008 SDLoc DL(N); 4009 4010 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 4011 4012 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0)); 4013 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG); 4014 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16); 4015 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0); 4016 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1); 4017 4018 if (VT.is64BitVector()) { 4019 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2); 4020 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended, 4021 DAG.getIntPtrConstant(0)); 4022 } else { 4023 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2, 4024 DAG.getIntPtrConstant(0)); 4025 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted); 4026 } 4027 } 4028 4029 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 4030 const ARMSubtarget *ST) { 4031 EVT VT = N->getValueType(0); 4032 4033 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 4034 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || 4035 VT == MVT::v4i16 || VT == MVT::v8i16) && 4036 "Unexpected type for custom ctpop lowering"); 4037 4038 if (VT.getVectorElementType() == MVT::i32) 4039 return lowerCTPOP32BitElements(N, DAG); 4040 else 4041 return lowerCTPOP16BitElements(N, DAG); 4042 } 4043 4044 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 4045 const ARMSubtarget *ST) { 4046 EVT VT = N->getValueType(0); 4047 SDLoc dl(N); 4048 4049 if (!VT.isVector()) 4050 return SDValue(); 4051 4052 // Lower vector shifts on NEON to use VSHL. 4053 assert(ST->hasNEON() && "unexpected vector shift"); 4054 4055 // Left shifts translate directly to the vshiftu intrinsic. 4056 if (N->getOpcode() == ISD::SHL) 4057 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4058 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32), 4059 N->getOperand(0), N->getOperand(1)); 4060 4061 assert((N->getOpcode() == ISD::SRA || 4062 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 4063 4064 // NEON uses the same intrinsics for both left and right shifts. For 4065 // right shifts, the shift amounts are negative, so negate the vector of 4066 // shift amounts. 4067 EVT ShiftVT = N->getOperand(1).getValueType(); 4068 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 4069 getZeroVector(ShiftVT, DAG, dl), 4070 N->getOperand(1)); 4071 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 4072 Intrinsic::arm_neon_vshifts : 4073 Intrinsic::arm_neon_vshiftu); 4074 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4075 DAG.getConstant(vshiftInt, MVT::i32), 4076 N->getOperand(0), NegatedCount); 4077 } 4078 4079 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 4080 const ARMSubtarget *ST) { 4081 EVT VT = N->getValueType(0); 4082 SDLoc dl(N); 4083 4084 // We can get here for a node like i32 = ISD::SHL i32, i64 4085 if (VT != MVT::i64) 4086 return SDValue(); 4087 4088 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 4089 "Unknown shift to lower!"); 4090 4091 // We only lower SRA, SRL of 1 here, all others use generic lowering. 4092 if (!isa<ConstantSDNode>(N->getOperand(1)) || 4093 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1) 4094 return SDValue(); 4095 4096 // If we are in thumb mode, we don't have RRX. 4097 if (ST->isThumb1Only()) return SDValue(); 4098 4099 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 4100 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4101 DAG.getConstant(0, MVT::i32)); 4102 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4103 DAG.getConstant(1, MVT::i32)); 4104 4105 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 4106 // captures the result into a carry flag. 4107 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 4108 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1); 4109 4110 // The low part is an ARMISD::RRX operand, which shifts the carry in. 4111 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 4112 4113 // Merge the pieces into a single i64 value. 4114 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 4115 } 4116 4117 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 4118 SDValue TmpOp0, TmpOp1; 4119 bool Invert = false; 4120 bool Swap = false; 4121 unsigned Opc = 0; 4122 4123 SDValue Op0 = Op.getOperand(0); 4124 SDValue Op1 = Op.getOperand(1); 4125 SDValue CC = Op.getOperand(2); 4126 EVT VT = Op.getValueType(); 4127 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 4128 SDLoc dl(Op); 4129 4130 if (Op.getOperand(1).getValueType().isFloatingPoint()) { 4131 switch (SetCCOpcode) { 4132 default: llvm_unreachable("Illegal FP comparison"); 4133 case ISD::SETUNE: 4134 case ISD::SETNE: Invert = true; // Fallthrough 4135 case ISD::SETOEQ: 4136 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4137 case ISD::SETOLT: 4138 case ISD::SETLT: Swap = true; // Fallthrough 4139 case ISD::SETOGT: 4140 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4141 case ISD::SETOLE: 4142 case ISD::SETLE: Swap = true; // Fallthrough 4143 case ISD::SETOGE: 4144 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4145 case ISD::SETUGE: Swap = true; // Fallthrough 4146 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 4147 case ISD::SETUGT: Swap = true; // Fallthrough 4148 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 4149 case ISD::SETUEQ: Invert = true; // Fallthrough 4150 case ISD::SETONE: 4151 // Expand this to (OLT | OGT). 4152 TmpOp0 = Op0; 4153 TmpOp1 = Op1; 4154 Opc = ISD::OR; 4155 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 4156 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1); 4157 break; 4158 case ISD::SETUO: Invert = true; // Fallthrough 4159 case ISD::SETO: 4160 // Expand this to (OLT | OGE). 4161 TmpOp0 = Op0; 4162 TmpOp1 = Op1; 4163 Opc = ISD::OR; 4164 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 4165 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1); 4166 break; 4167 } 4168 } else { 4169 // Integer comparisons. 4170 switch (SetCCOpcode) { 4171 default: llvm_unreachable("Illegal integer comparison"); 4172 case ISD::SETNE: Invert = true; 4173 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4174 case ISD::SETLT: Swap = true; 4175 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4176 case ISD::SETLE: Swap = true; 4177 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4178 case ISD::SETULT: Swap = true; 4179 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 4180 case ISD::SETULE: Swap = true; 4181 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 4182 } 4183 4184 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 4185 if (Opc == ARMISD::VCEQ) { 4186 4187 SDValue AndOp; 4188 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4189 AndOp = Op0; 4190 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 4191 AndOp = Op1; 4192 4193 // Ignore bitconvert. 4194 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 4195 AndOp = AndOp.getOperand(0); 4196 4197 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 4198 Opc = ARMISD::VTST; 4199 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0)); 4200 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1)); 4201 Invert = !Invert; 4202 } 4203 } 4204 } 4205 4206 if (Swap) 4207 std::swap(Op0, Op1); 4208 4209 // If one of the operands is a constant vector zero, attempt to fold the 4210 // comparison to a specialized compare-against-zero form. 4211 SDValue SingleOp; 4212 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4213 SingleOp = Op0; 4214 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 4215 if (Opc == ARMISD::VCGE) 4216 Opc = ARMISD::VCLEZ; 4217 else if (Opc == ARMISD::VCGT) 4218 Opc = ARMISD::VCLTZ; 4219 SingleOp = Op1; 4220 } 4221 4222 SDValue Result; 4223 if (SingleOp.getNode()) { 4224 switch (Opc) { 4225 case ARMISD::VCEQ: 4226 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break; 4227 case ARMISD::VCGE: 4228 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break; 4229 case ARMISD::VCLEZ: 4230 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break; 4231 case ARMISD::VCGT: 4232 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break; 4233 case ARMISD::VCLTZ: 4234 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break; 4235 default: 4236 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 4237 } 4238 } else { 4239 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 4240 } 4241 4242 if (Invert) 4243 Result = DAG.getNOT(dl, Result, VT); 4244 4245 return Result; 4246 } 4247 4248 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 4249 /// valid vector constant for a NEON instruction with a "modified immediate" 4250 /// operand (e.g., VMOV). If so, return the encoded value. 4251 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 4252 unsigned SplatBitSize, SelectionDAG &DAG, 4253 EVT &VT, bool is128Bits, NEONModImmType type) { 4254 unsigned OpCmode, Imm; 4255 4256 // SplatBitSize is set to the smallest size that splats the vector, so a 4257 // zero vector will always have SplatBitSize == 8. However, NEON modified 4258 // immediate instructions others than VMOV do not support the 8-bit encoding 4259 // of a zero vector, and the default encoding of zero is supposed to be the 4260 // 32-bit version. 4261 if (SplatBits == 0) 4262 SplatBitSize = 32; 4263 4264 switch (SplatBitSize) { 4265 case 8: 4266 if (type != VMOVModImm) 4267 return SDValue(); 4268 // Any 1-byte value is OK. Op=0, Cmode=1110. 4269 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 4270 OpCmode = 0xe; 4271 Imm = SplatBits; 4272 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 4273 break; 4274 4275 case 16: 4276 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 4277 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 4278 if ((SplatBits & ~0xff) == 0) { 4279 // Value = 0x00nn: Op=x, Cmode=100x. 4280 OpCmode = 0x8; 4281 Imm = SplatBits; 4282 break; 4283 } 4284 if ((SplatBits & ~0xff00) == 0) { 4285 // Value = 0xnn00: Op=x, Cmode=101x. 4286 OpCmode = 0xa; 4287 Imm = SplatBits >> 8; 4288 break; 4289 } 4290 return SDValue(); 4291 4292 case 32: 4293 // NEON's 32-bit VMOV supports splat values where: 4294 // * only one byte is nonzero, or 4295 // * the least significant byte is 0xff and the second byte is nonzero, or 4296 // * the least significant 2 bytes are 0xff and the third is nonzero. 4297 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 4298 if ((SplatBits & ~0xff) == 0) { 4299 // Value = 0x000000nn: Op=x, Cmode=000x. 4300 OpCmode = 0; 4301 Imm = SplatBits; 4302 break; 4303 } 4304 if ((SplatBits & ~0xff00) == 0) { 4305 // Value = 0x0000nn00: Op=x, Cmode=001x. 4306 OpCmode = 0x2; 4307 Imm = SplatBits >> 8; 4308 break; 4309 } 4310 if ((SplatBits & ~0xff0000) == 0) { 4311 // Value = 0x00nn0000: Op=x, Cmode=010x. 4312 OpCmode = 0x4; 4313 Imm = SplatBits >> 16; 4314 break; 4315 } 4316 if ((SplatBits & ~0xff000000) == 0) { 4317 // Value = 0xnn000000: Op=x, Cmode=011x. 4318 OpCmode = 0x6; 4319 Imm = SplatBits >> 24; 4320 break; 4321 } 4322 4323 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 4324 if (type == OtherModImm) return SDValue(); 4325 4326 if ((SplatBits & ~0xffff) == 0 && 4327 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 4328 // Value = 0x0000nnff: Op=x, Cmode=1100. 4329 OpCmode = 0xc; 4330 Imm = SplatBits >> 8; 4331 SplatBits |= 0xff; 4332 break; 4333 } 4334 4335 if ((SplatBits & ~0xffffff) == 0 && 4336 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 4337 // Value = 0x00nnffff: Op=x, Cmode=1101. 4338 OpCmode = 0xd; 4339 Imm = SplatBits >> 16; 4340 SplatBits |= 0xffff; 4341 break; 4342 } 4343 4344 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 4345 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 4346 // VMOV.I32. A (very) minor optimization would be to replicate the value 4347 // and fall through here to test for a valid 64-bit splat. But, then the 4348 // caller would also need to check and handle the change in size. 4349 return SDValue(); 4350 4351 case 64: { 4352 if (type != VMOVModImm) 4353 return SDValue(); 4354 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 4355 uint64_t BitMask = 0xff; 4356 uint64_t Val = 0; 4357 unsigned ImmMask = 1; 4358 Imm = 0; 4359 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 4360 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 4361 Val |= BitMask; 4362 Imm |= ImmMask; 4363 } else if ((SplatBits & BitMask) != 0) { 4364 return SDValue(); 4365 } 4366 BitMask <<= 8; 4367 ImmMask <<= 1; 4368 } 4369 // Op=1, Cmode=1110. 4370 OpCmode = 0x1e; 4371 SplatBits = Val; 4372 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 4373 break; 4374 } 4375 4376 default: 4377 llvm_unreachable("unexpected size for isNEONModifiedImm"); 4378 } 4379 4380 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 4381 return DAG.getTargetConstant(EncodedVal, MVT::i32); 4382 } 4383 4384 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 4385 const ARMSubtarget *ST) const { 4386 if (!ST->hasVFP3()) 4387 return SDValue(); 4388 4389 bool IsDouble = Op.getValueType() == MVT::f64; 4390 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 4391 4392 // Try splatting with a VMOV.f32... 4393 APFloat FPVal = CFP->getValueAPF(); 4394 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 4395 4396 if (ImmVal != -1) { 4397 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 4398 // We have code in place to select a valid ConstantFP already, no need to 4399 // do any mangling. 4400 return Op; 4401 } 4402 4403 // It's a float and we are trying to use NEON operations where 4404 // possible. Lower it to a splat followed by an extract. 4405 SDLoc DL(Op); 4406 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32); 4407 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 4408 NewVal); 4409 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 4410 DAG.getConstant(0, MVT::i32)); 4411 } 4412 4413 // The rest of our options are NEON only, make sure that's allowed before 4414 // proceeding.. 4415 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 4416 return SDValue(); 4417 4418 EVT VMovVT; 4419 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 4420 4421 // It wouldn't really be worth bothering for doubles except for one very 4422 // important value, which does happen to match: 0.0. So make sure we don't do 4423 // anything stupid. 4424 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 4425 return SDValue(); 4426 4427 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 4428 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT, 4429 false, VMOVModImm); 4430 if (NewVal != SDValue()) { 4431 SDLoc DL(Op); 4432 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 4433 NewVal); 4434 if (IsDouble) 4435 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 4436 4437 // It's a float: cast and extract a vector element. 4438 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 4439 VecConstant); 4440 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 4441 DAG.getConstant(0, MVT::i32)); 4442 } 4443 4444 // Finally, try a VMVN.i32 4445 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT, 4446 false, VMVNModImm); 4447 if (NewVal != SDValue()) { 4448 SDLoc DL(Op); 4449 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 4450 4451 if (IsDouble) 4452 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 4453 4454 // It's a float: cast and extract a vector element. 4455 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 4456 VecConstant); 4457 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 4458 DAG.getConstant(0, MVT::i32)); 4459 } 4460 4461 return SDValue(); 4462 } 4463 4464 // check if an VEXT instruction can handle the shuffle mask when the 4465 // vector sources of the shuffle are the same. 4466 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 4467 unsigned NumElts = VT.getVectorNumElements(); 4468 4469 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4470 if (M[0] < 0) 4471 return false; 4472 4473 Imm = M[0]; 4474 4475 // If this is a VEXT shuffle, the immediate value is the index of the first 4476 // element. The other shuffle indices must be the successive elements after 4477 // the first one. 4478 unsigned ExpectedElt = Imm; 4479 for (unsigned i = 1; i < NumElts; ++i) { 4480 // Increment the expected index. If it wraps around, just follow it 4481 // back to index zero and keep going. 4482 ++ExpectedElt; 4483 if (ExpectedElt == NumElts) 4484 ExpectedElt = 0; 4485 4486 if (M[i] < 0) continue; // ignore UNDEF indices 4487 if (ExpectedElt != static_cast<unsigned>(M[i])) 4488 return false; 4489 } 4490 4491 return true; 4492 } 4493 4494 4495 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 4496 bool &ReverseVEXT, unsigned &Imm) { 4497 unsigned NumElts = VT.getVectorNumElements(); 4498 ReverseVEXT = false; 4499 4500 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4501 if (M[0] < 0) 4502 return false; 4503 4504 Imm = M[0]; 4505 4506 // If this is a VEXT shuffle, the immediate value is the index of the first 4507 // element. The other shuffle indices must be the successive elements after 4508 // the first one. 4509 unsigned ExpectedElt = Imm; 4510 for (unsigned i = 1; i < NumElts; ++i) { 4511 // Increment the expected index. If it wraps around, it may still be 4512 // a VEXT but the source vectors must be swapped. 4513 ExpectedElt += 1; 4514 if (ExpectedElt == NumElts * 2) { 4515 ExpectedElt = 0; 4516 ReverseVEXT = true; 4517 } 4518 4519 if (M[i] < 0) continue; // ignore UNDEF indices 4520 if (ExpectedElt != static_cast<unsigned>(M[i])) 4521 return false; 4522 } 4523 4524 // Adjust the index value if the source operands will be swapped. 4525 if (ReverseVEXT) 4526 Imm -= NumElts; 4527 4528 return true; 4529 } 4530 4531 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 4532 /// instruction with the specified blocksize. (The order of the elements 4533 /// within each block of the vector is reversed.) 4534 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 4535 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 4536 "Only possible block sizes for VREV are: 16, 32, 64"); 4537 4538 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4539 if (EltSz == 64) 4540 return false; 4541 4542 unsigned NumElts = VT.getVectorNumElements(); 4543 unsigned BlockElts = M[0] + 1; 4544 // If the first shuffle index is UNDEF, be optimistic. 4545 if (M[0] < 0) 4546 BlockElts = BlockSize / EltSz; 4547 4548 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 4549 return false; 4550 4551 for (unsigned i = 0; i < NumElts; ++i) { 4552 if (M[i] < 0) continue; // ignore UNDEF indices 4553 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 4554 return false; 4555 } 4556 4557 return true; 4558 } 4559 4560 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 4561 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 4562 // range, then 0 is placed into the resulting vector. So pretty much any mask 4563 // of 8 elements can work here. 4564 return VT == MVT::v8i8 && M.size() == 8; 4565 } 4566 4567 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4568 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4569 if (EltSz == 64) 4570 return false; 4571 4572 unsigned NumElts = VT.getVectorNumElements(); 4573 WhichResult = (M[0] == 0 ? 0 : 1); 4574 for (unsigned i = 0; i < NumElts; i += 2) { 4575 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 4576 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult)) 4577 return false; 4578 } 4579 return true; 4580 } 4581 4582 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 4583 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4584 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 4585 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4586 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4587 if (EltSz == 64) 4588 return false; 4589 4590 unsigned NumElts = VT.getVectorNumElements(); 4591 WhichResult = (M[0] == 0 ? 0 : 1); 4592 for (unsigned i = 0; i < NumElts; i += 2) { 4593 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 4594 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult)) 4595 return false; 4596 } 4597 return true; 4598 } 4599 4600 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4601 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4602 if (EltSz == 64) 4603 return false; 4604 4605 unsigned NumElts = VT.getVectorNumElements(); 4606 WhichResult = (M[0] == 0 ? 0 : 1); 4607 for (unsigned i = 0; i != NumElts; ++i) { 4608 if (M[i] < 0) continue; // ignore UNDEF indices 4609 if ((unsigned) M[i] != 2 * i + WhichResult) 4610 return false; 4611 } 4612 4613 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4614 if (VT.is64BitVector() && EltSz == 32) 4615 return false; 4616 4617 return true; 4618 } 4619 4620 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 4621 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4622 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 4623 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4624 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4625 if (EltSz == 64) 4626 return false; 4627 4628 unsigned Half = VT.getVectorNumElements() / 2; 4629 WhichResult = (M[0] == 0 ? 0 : 1); 4630 for (unsigned j = 0; j != 2; ++j) { 4631 unsigned Idx = WhichResult; 4632 for (unsigned i = 0; i != Half; ++i) { 4633 int MIdx = M[i + j * Half]; 4634 if (MIdx >= 0 && (unsigned) MIdx != Idx) 4635 return false; 4636 Idx += 2; 4637 } 4638 } 4639 4640 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4641 if (VT.is64BitVector() && EltSz == 32) 4642 return false; 4643 4644 return true; 4645 } 4646 4647 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4648 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4649 if (EltSz == 64) 4650 return false; 4651 4652 unsigned NumElts = VT.getVectorNumElements(); 4653 WhichResult = (M[0] == 0 ? 0 : 1); 4654 unsigned Idx = WhichResult * NumElts / 2; 4655 for (unsigned i = 0; i != NumElts; i += 2) { 4656 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 4657 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts)) 4658 return false; 4659 Idx += 1; 4660 } 4661 4662 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4663 if (VT.is64BitVector() && EltSz == 32) 4664 return false; 4665 4666 return true; 4667 } 4668 4669 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 4670 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4671 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 4672 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4673 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4674 if (EltSz == 64) 4675 return false; 4676 4677 unsigned NumElts = VT.getVectorNumElements(); 4678 WhichResult = (M[0] == 0 ? 0 : 1); 4679 unsigned Idx = WhichResult * NumElts / 2; 4680 for (unsigned i = 0; i != NumElts; i += 2) { 4681 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 4682 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx)) 4683 return false; 4684 Idx += 1; 4685 } 4686 4687 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4688 if (VT.is64BitVector() && EltSz == 32) 4689 return false; 4690 4691 return true; 4692 } 4693 4694 /// \return true if this is a reverse operation on an vector. 4695 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 4696 unsigned NumElts = VT.getVectorNumElements(); 4697 // Make sure the mask has the right size. 4698 if (NumElts != M.size()) 4699 return false; 4700 4701 // Look for <15, ..., 3, -1, 1, 0>. 4702 for (unsigned i = 0; i != NumElts; ++i) 4703 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 4704 return false; 4705 4706 return true; 4707 } 4708 4709 // If N is an integer constant that can be moved into a register in one 4710 // instruction, return an SDValue of such a constant (will become a MOV 4711 // instruction). Otherwise return null. 4712 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 4713 const ARMSubtarget *ST, SDLoc dl) { 4714 uint64_t Val; 4715 if (!isa<ConstantSDNode>(N)) 4716 return SDValue(); 4717 Val = cast<ConstantSDNode>(N)->getZExtValue(); 4718 4719 if (ST->isThumb1Only()) { 4720 if (Val <= 255 || ~Val <= 255) 4721 return DAG.getConstant(Val, MVT::i32); 4722 } else { 4723 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 4724 return DAG.getConstant(Val, MVT::i32); 4725 } 4726 return SDValue(); 4727 } 4728 4729 // If this is a case we can't handle, return null and let the default 4730 // expansion code take care of it. 4731 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 4732 const ARMSubtarget *ST) const { 4733 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 4734 SDLoc dl(Op); 4735 EVT VT = Op.getValueType(); 4736 4737 APInt SplatBits, SplatUndef; 4738 unsigned SplatBitSize; 4739 bool HasAnyUndefs; 4740 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 4741 if (SplatBitSize <= 64) { 4742 // Check if an immediate VMOV works. 4743 EVT VmovVT; 4744 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 4745 SplatUndef.getZExtValue(), SplatBitSize, 4746 DAG, VmovVT, VT.is128BitVector(), 4747 VMOVModImm); 4748 if (Val.getNode()) { 4749 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 4750 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4751 } 4752 4753 // Try an immediate VMVN. 4754 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 4755 Val = isNEONModifiedImm(NegatedImm, 4756 SplatUndef.getZExtValue(), SplatBitSize, 4757 DAG, VmovVT, VT.is128BitVector(), 4758 VMVNModImm); 4759 if (Val.getNode()) { 4760 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 4761 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4762 } 4763 4764 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 4765 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 4766 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 4767 if (ImmVal != -1) { 4768 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32); 4769 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 4770 } 4771 } 4772 } 4773 } 4774 4775 // Scan through the operands to see if only one value is used. 4776 // 4777 // As an optimisation, even if more than one value is used it may be more 4778 // profitable to splat with one value then change some lanes. 4779 // 4780 // Heuristically we decide to do this if the vector has a "dominant" value, 4781 // defined as splatted to more than half of the lanes. 4782 unsigned NumElts = VT.getVectorNumElements(); 4783 bool isOnlyLowElement = true; 4784 bool usesOnlyOneValue = true; 4785 bool hasDominantValue = false; 4786 bool isConstant = true; 4787 4788 // Map of the number of times a particular SDValue appears in the 4789 // element list. 4790 DenseMap<SDValue, unsigned> ValueCounts; 4791 SDValue Value; 4792 for (unsigned i = 0; i < NumElts; ++i) { 4793 SDValue V = Op.getOperand(i); 4794 if (V.getOpcode() == ISD::UNDEF) 4795 continue; 4796 if (i > 0) 4797 isOnlyLowElement = false; 4798 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 4799 isConstant = false; 4800 4801 ValueCounts.insert(std::make_pair(V, 0)); 4802 unsigned &Count = ValueCounts[V]; 4803 4804 // Is this value dominant? (takes up more than half of the lanes) 4805 if (++Count > (NumElts / 2)) { 4806 hasDominantValue = true; 4807 Value = V; 4808 } 4809 } 4810 if (ValueCounts.size() != 1) 4811 usesOnlyOneValue = false; 4812 if (!Value.getNode() && ValueCounts.size() > 0) 4813 Value = ValueCounts.begin()->first; 4814 4815 if (ValueCounts.size() == 0) 4816 return DAG.getUNDEF(VT); 4817 4818 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 4819 // Keep going if we are hitting this case. 4820 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 4821 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 4822 4823 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4824 4825 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 4826 // i32 and try again. 4827 if (hasDominantValue && EltSize <= 32) { 4828 if (!isConstant) { 4829 SDValue N; 4830 4831 // If we are VDUPing a value that comes directly from a vector, that will 4832 // cause an unnecessary move to and from a GPR, where instead we could 4833 // just use VDUPLANE. We can only do this if the lane being extracted 4834 // is at a constant index, as the VDUP from lane instructions only have 4835 // constant-index forms. 4836 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4837 isa<ConstantSDNode>(Value->getOperand(1))) { 4838 // We need to create a new undef vector to use for the VDUPLANE if the 4839 // size of the vector from which we get the value is different than the 4840 // size of the vector that we need to create. We will insert the element 4841 // such that the register coalescer will remove unnecessary copies. 4842 if (VT != Value->getOperand(0).getValueType()) { 4843 ConstantSDNode *constIndex; 4844 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)); 4845 assert(constIndex && "The index is not a constant!"); 4846 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 4847 VT.getVectorNumElements(); 4848 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 4849 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 4850 Value, DAG.getConstant(index, MVT::i32)), 4851 DAG.getConstant(index, MVT::i32)); 4852 } else 4853 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 4854 Value->getOperand(0), Value->getOperand(1)); 4855 } else 4856 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 4857 4858 if (!usesOnlyOneValue) { 4859 // The dominant value was splatted as 'N', but we now have to insert 4860 // all differing elements. 4861 for (unsigned I = 0; I < NumElts; ++I) { 4862 if (Op.getOperand(I) == Value) 4863 continue; 4864 SmallVector<SDValue, 3> Ops; 4865 Ops.push_back(N); 4866 Ops.push_back(Op.getOperand(I)); 4867 Ops.push_back(DAG.getConstant(I, MVT::i32)); 4868 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3); 4869 } 4870 } 4871 return N; 4872 } 4873 if (VT.getVectorElementType().isFloatingPoint()) { 4874 SmallVector<SDValue, 8> Ops; 4875 for (unsigned i = 0; i < NumElts; ++i) 4876 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 4877 Op.getOperand(i))); 4878 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 4879 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts); 4880 Val = LowerBUILD_VECTOR(Val, DAG, ST); 4881 if (Val.getNode()) 4882 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4883 } 4884 if (usesOnlyOneValue) { 4885 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 4886 if (isConstant && Val.getNode()) 4887 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 4888 } 4889 } 4890 4891 // If all elements are constants and the case above didn't get hit, fall back 4892 // to the default expansion, which will generate a load from the constant 4893 // pool. 4894 if (isConstant) 4895 return SDValue(); 4896 4897 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 4898 if (NumElts >= 4) { 4899 SDValue shuffle = ReconstructShuffle(Op, DAG); 4900 if (shuffle != SDValue()) 4901 return shuffle; 4902 } 4903 4904 // Vectors with 32- or 64-bit elements can be built by directly assigning 4905 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 4906 // will be legalized. 4907 if (EltSize >= 32) { 4908 // Do the expansion with floating-point types, since that is what the VFP 4909 // registers are defined to use, and since i64 is not legal. 4910 EVT EltVT = EVT::getFloatingPointVT(EltSize); 4911 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 4912 SmallVector<SDValue, 8> Ops; 4913 for (unsigned i = 0; i < NumElts; ++i) 4914 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 4915 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts); 4916 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4917 } 4918 4919 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 4920 // know the default expansion would otherwise fall back on something even 4921 // worse. For a vector with one or two non-undef values, that's 4922 // scalar_to_vector for the elements followed by a shuffle (provided the 4923 // shuffle is valid for the target) and materialization element by element 4924 // on the stack followed by a load for everything else. 4925 if (!isConstant && !usesOnlyOneValue) { 4926 SDValue Vec = DAG.getUNDEF(VT); 4927 for (unsigned i = 0 ; i < NumElts; ++i) { 4928 SDValue V = Op.getOperand(i); 4929 if (V.getOpcode() == ISD::UNDEF) 4930 continue; 4931 SDValue LaneIdx = DAG.getConstant(i, MVT::i32); 4932 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 4933 } 4934 return Vec; 4935 } 4936 4937 return SDValue(); 4938 } 4939 4940 // Gather data to see if the operation can be modelled as a 4941 // shuffle in combination with VEXTs. 4942 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 4943 SelectionDAG &DAG) const { 4944 SDLoc dl(Op); 4945 EVT VT = Op.getValueType(); 4946 unsigned NumElts = VT.getVectorNumElements(); 4947 4948 SmallVector<SDValue, 2> SourceVecs; 4949 SmallVector<unsigned, 2> MinElts; 4950 SmallVector<unsigned, 2> MaxElts; 4951 4952 for (unsigned i = 0; i < NumElts; ++i) { 4953 SDValue V = Op.getOperand(i); 4954 if (V.getOpcode() == ISD::UNDEF) 4955 continue; 4956 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 4957 // A shuffle can only come from building a vector from various 4958 // elements of other vectors. 4959 return SDValue(); 4960 } else if (V.getOperand(0).getValueType().getVectorElementType() != 4961 VT.getVectorElementType()) { 4962 // This code doesn't know how to handle shuffles where the vector 4963 // element types do not match (this happens because type legalization 4964 // promotes the return type of EXTRACT_VECTOR_ELT). 4965 // FIXME: It might be appropriate to extend this code to handle 4966 // mismatched types. 4967 return SDValue(); 4968 } 4969 4970 // Record this extraction against the appropriate vector if possible... 4971 SDValue SourceVec = V.getOperand(0); 4972 // If the element number isn't a constant, we can't effectively 4973 // analyze what's going on. 4974 if (!isa<ConstantSDNode>(V.getOperand(1))) 4975 return SDValue(); 4976 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 4977 bool FoundSource = false; 4978 for (unsigned j = 0; j < SourceVecs.size(); ++j) { 4979 if (SourceVecs[j] == SourceVec) { 4980 if (MinElts[j] > EltNo) 4981 MinElts[j] = EltNo; 4982 if (MaxElts[j] < EltNo) 4983 MaxElts[j] = EltNo; 4984 FoundSource = true; 4985 break; 4986 } 4987 } 4988 4989 // Or record a new source if not... 4990 if (!FoundSource) { 4991 SourceVecs.push_back(SourceVec); 4992 MinElts.push_back(EltNo); 4993 MaxElts.push_back(EltNo); 4994 } 4995 } 4996 4997 // Currently only do something sane when at most two source vectors 4998 // involved. 4999 if (SourceVecs.size() > 2) 5000 return SDValue(); 5001 5002 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) }; 5003 int VEXTOffsets[2] = {0, 0}; 5004 5005 // This loop extracts the usage patterns of the source vectors 5006 // and prepares appropriate SDValues for a shuffle if possible. 5007 for (unsigned i = 0; i < SourceVecs.size(); ++i) { 5008 if (SourceVecs[i].getValueType() == VT) { 5009 // No VEXT necessary 5010 ShuffleSrcs[i] = SourceVecs[i]; 5011 VEXTOffsets[i] = 0; 5012 continue; 5013 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) { 5014 // It probably isn't worth padding out a smaller vector just to 5015 // break it down again in a shuffle. 5016 return SDValue(); 5017 } 5018 5019 // Since only 64-bit and 128-bit vectors are legal on ARM and 5020 // we've eliminated the other cases... 5021 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts && 5022 "unexpected vector sizes in ReconstructShuffle"); 5023 5024 if (MaxElts[i] - MinElts[i] >= NumElts) { 5025 // Span too large for a VEXT to cope 5026 return SDValue(); 5027 } 5028 5029 if (MinElts[i] >= NumElts) { 5030 // The extraction can just take the second half 5031 VEXTOffsets[i] = NumElts; 5032 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5033 SourceVecs[i], 5034 DAG.getIntPtrConstant(NumElts)); 5035 } else if (MaxElts[i] < NumElts) { 5036 // The extraction can just take the first half 5037 VEXTOffsets[i] = 0; 5038 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5039 SourceVecs[i], 5040 DAG.getIntPtrConstant(0)); 5041 } else { 5042 // An actual VEXT is needed 5043 VEXTOffsets[i] = MinElts[i]; 5044 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5045 SourceVecs[i], 5046 DAG.getIntPtrConstant(0)); 5047 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5048 SourceVecs[i], 5049 DAG.getIntPtrConstant(NumElts)); 5050 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2, 5051 DAG.getConstant(VEXTOffsets[i], MVT::i32)); 5052 } 5053 } 5054 5055 SmallVector<int, 8> Mask; 5056 5057 for (unsigned i = 0; i < NumElts; ++i) { 5058 SDValue Entry = Op.getOperand(i); 5059 if (Entry.getOpcode() == ISD::UNDEF) { 5060 Mask.push_back(-1); 5061 continue; 5062 } 5063 5064 SDValue ExtractVec = Entry.getOperand(0); 5065 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i) 5066 .getOperand(1))->getSExtValue(); 5067 if (ExtractVec == SourceVecs[0]) { 5068 Mask.push_back(ExtractElt - VEXTOffsets[0]); 5069 } else { 5070 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]); 5071 } 5072 } 5073 5074 // Final check before we try to produce nonsense... 5075 if (isShuffleMaskLegal(Mask, VT)) 5076 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], 5077 &Mask[0]); 5078 5079 return SDValue(); 5080 } 5081 5082 /// isShuffleMaskLegal - Targets can use this to indicate that they only 5083 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 5084 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 5085 /// are assumed to be legal. 5086 bool 5087 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 5088 EVT VT) const { 5089 if (VT.getVectorNumElements() == 4 && 5090 (VT.is128BitVector() || VT.is64BitVector())) { 5091 unsigned PFIndexes[4]; 5092 for (unsigned i = 0; i != 4; ++i) { 5093 if (M[i] < 0) 5094 PFIndexes[i] = 8; 5095 else 5096 PFIndexes[i] = M[i]; 5097 } 5098 5099 // Compute the index in the perfect shuffle table. 5100 unsigned PFTableIndex = 5101 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5102 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5103 unsigned Cost = (PFEntry >> 30); 5104 5105 if (Cost <= 4) 5106 return true; 5107 } 5108 5109 bool ReverseVEXT; 5110 unsigned Imm, WhichResult; 5111 5112 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5113 return (EltSize >= 32 || 5114 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 5115 isVREVMask(M, VT, 64) || 5116 isVREVMask(M, VT, 32) || 5117 isVREVMask(M, VT, 16) || 5118 isVEXTMask(M, VT, ReverseVEXT, Imm) || 5119 isVTBLMask(M, VT) || 5120 isVTRNMask(M, VT, WhichResult) || 5121 isVUZPMask(M, VT, WhichResult) || 5122 isVZIPMask(M, VT, WhichResult) || 5123 isVTRN_v_undef_Mask(M, VT, WhichResult) || 5124 isVUZP_v_undef_Mask(M, VT, WhichResult) || 5125 isVZIP_v_undef_Mask(M, VT, WhichResult) || 5126 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT))); 5127 } 5128 5129 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 5130 /// the specified operations to build the shuffle. 5131 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 5132 SDValue RHS, SelectionDAG &DAG, 5133 SDLoc dl) { 5134 unsigned OpNum = (PFEntry >> 26) & 0x0F; 5135 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 5136 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 5137 5138 enum { 5139 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 5140 OP_VREV, 5141 OP_VDUP0, 5142 OP_VDUP1, 5143 OP_VDUP2, 5144 OP_VDUP3, 5145 OP_VEXT1, 5146 OP_VEXT2, 5147 OP_VEXT3, 5148 OP_VUZPL, // VUZP, left result 5149 OP_VUZPR, // VUZP, right result 5150 OP_VZIPL, // VZIP, left result 5151 OP_VZIPR, // VZIP, right result 5152 OP_VTRNL, // VTRN, left result 5153 OP_VTRNR // VTRN, right result 5154 }; 5155 5156 if (OpNum == OP_COPY) { 5157 if (LHSID == (1*9+2)*9+3) return LHS; 5158 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 5159 return RHS; 5160 } 5161 5162 SDValue OpLHS, OpRHS; 5163 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 5164 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 5165 EVT VT = OpLHS.getValueType(); 5166 5167 switch (OpNum) { 5168 default: llvm_unreachable("Unknown shuffle opcode!"); 5169 case OP_VREV: 5170 // VREV divides the vector in half and swaps within the half. 5171 if (VT.getVectorElementType() == MVT::i32 || 5172 VT.getVectorElementType() == MVT::f32) 5173 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 5174 // vrev <4 x i16> -> VREV32 5175 if (VT.getVectorElementType() == MVT::i16) 5176 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 5177 // vrev <4 x i8> -> VREV16 5178 assert(VT.getVectorElementType() == MVT::i8); 5179 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 5180 case OP_VDUP0: 5181 case OP_VDUP1: 5182 case OP_VDUP2: 5183 case OP_VDUP3: 5184 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5185 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32)); 5186 case OP_VEXT1: 5187 case OP_VEXT2: 5188 case OP_VEXT3: 5189 return DAG.getNode(ARMISD::VEXT, dl, VT, 5190 OpLHS, OpRHS, 5191 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32)); 5192 case OP_VUZPL: 5193 case OP_VUZPR: 5194 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5195 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 5196 case OP_VZIPL: 5197 case OP_VZIPR: 5198 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5199 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 5200 case OP_VTRNL: 5201 case OP_VTRNR: 5202 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5203 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 5204 } 5205 } 5206 5207 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 5208 ArrayRef<int> ShuffleMask, 5209 SelectionDAG &DAG) { 5210 // Check to see if we can use the VTBL instruction. 5211 SDValue V1 = Op.getOperand(0); 5212 SDValue V2 = Op.getOperand(1); 5213 SDLoc DL(Op); 5214 5215 SmallVector<SDValue, 8> VTBLMask; 5216 for (ArrayRef<int>::iterator 5217 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 5218 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32)); 5219 5220 if (V2.getNode()->getOpcode() == ISD::UNDEF) 5221 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 5222 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, 5223 &VTBLMask[0], 8)); 5224 5225 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 5226 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, 5227 &VTBLMask[0], 8)); 5228 } 5229 5230 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 5231 SelectionDAG &DAG) { 5232 SDLoc DL(Op); 5233 SDValue OpLHS = Op.getOperand(0); 5234 EVT VT = OpLHS.getValueType(); 5235 5236 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 5237 "Expect an v8i16/v16i8 type"); 5238 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 5239 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 5240 // extract the first 8 bytes into the top double word and the last 8 bytes 5241 // into the bottom double word. The v8i16 case is similar. 5242 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 5243 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 5244 DAG.getConstant(ExtractNum, MVT::i32)); 5245 } 5246 5247 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 5248 SDValue V1 = Op.getOperand(0); 5249 SDValue V2 = Op.getOperand(1); 5250 SDLoc dl(Op); 5251 EVT VT = Op.getValueType(); 5252 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 5253 5254 // Convert shuffles that are directly supported on NEON to target-specific 5255 // DAG nodes, instead of keeping them as shuffles and matching them again 5256 // during code selection. This is more efficient and avoids the possibility 5257 // of inconsistencies between legalization and selection. 5258 // FIXME: floating-point vectors should be canonicalized to integer vectors 5259 // of the same time so that they get CSEd properly. 5260 ArrayRef<int> ShuffleMask = SVN->getMask(); 5261 5262 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5263 if (EltSize <= 32) { 5264 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 5265 int Lane = SVN->getSplatIndex(); 5266 // If this is undef splat, generate it via "just" vdup, if possible. 5267 if (Lane == -1) Lane = 0; 5268 5269 // Test if V1 is a SCALAR_TO_VECTOR. 5270 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 5271 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 5272 } 5273 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 5274 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 5275 // reaches it). 5276 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 5277 !isa<ConstantSDNode>(V1.getOperand(0))) { 5278 bool IsScalarToVector = true; 5279 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 5280 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) { 5281 IsScalarToVector = false; 5282 break; 5283 } 5284 if (IsScalarToVector) 5285 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 5286 } 5287 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 5288 DAG.getConstant(Lane, MVT::i32)); 5289 } 5290 5291 bool ReverseVEXT; 5292 unsigned Imm; 5293 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 5294 if (ReverseVEXT) 5295 std::swap(V1, V2); 5296 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 5297 DAG.getConstant(Imm, MVT::i32)); 5298 } 5299 5300 if (isVREVMask(ShuffleMask, VT, 64)) 5301 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 5302 if (isVREVMask(ShuffleMask, VT, 32)) 5303 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 5304 if (isVREVMask(ShuffleMask, VT, 16)) 5305 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 5306 5307 if (V2->getOpcode() == ISD::UNDEF && 5308 isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 5309 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 5310 DAG.getConstant(Imm, MVT::i32)); 5311 } 5312 5313 // Check for Neon shuffles that modify both input vectors in place. 5314 // If both results are used, i.e., if there are two shuffles with the same 5315 // source operands and with masks corresponding to both results of one of 5316 // these operations, DAG memoization will ensure that a single node is 5317 // used for both shuffles. 5318 unsigned WhichResult; 5319 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 5320 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5321 V1, V2).getValue(WhichResult); 5322 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 5323 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5324 V1, V2).getValue(WhichResult); 5325 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 5326 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5327 V1, V2).getValue(WhichResult); 5328 5329 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5330 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5331 V1, V1).getValue(WhichResult); 5332 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5333 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5334 V1, V1).getValue(WhichResult); 5335 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5336 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5337 V1, V1).getValue(WhichResult); 5338 } 5339 5340 // If the shuffle is not directly supported and it has 4 elements, use 5341 // the PerfectShuffle-generated table to synthesize it from other shuffles. 5342 unsigned NumElts = VT.getVectorNumElements(); 5343 if (NumElts == 4) { 5344 unsigned PFIndexes[4]; 5345 for (unsigned i = 0; i != 4; ++i) { 5346 if (ShuffleMask[i] < 0) 5347 PFIndexes[i] = 8; 5348 else 5349 PFIndexes[i] = ShuffleMask[i]; 5350 } 5351 5352 // Compute the index in the perfect shuffle table. 5353 unsigned PFTableIndex = 5354 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5355 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5356 unsigned Cost = (PFEntry >> 30); 5357 5358 if (Cost <= 4) 5359 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 5360 } 5361 5362 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 5363 if (EltSize >= 32) { 5364 // Do the expansion with floating-point types, since that is what the VFP 5365 // registers are defined to use, and since i64 is not legal. 5366 EVT EltVT = EVT::getFloatingPointVT(EltSize); 5367 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 5368 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 5369 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 5370 SmallVector<SDValue, 8> Ops; 5371 for (unsigned i = 0; i < NumElts; ++i) { 5372 if (ShuffleMask[i] < 0) 5373 Ops.push_back(DAG.getUNDEF(EltVT)); 5374 else 5375 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 5376 ShuffleMask[i] < (int)NumElts ? V1 : V2, 5377 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 5378 MVT::i32))); 5379 } 5380 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts); 5381 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5382 } 5383 5384 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 5385 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 5386 5387 if (VT == MVT::v8i8) { 5388 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG); 5389 if (NewOp.getNode()) 5390 return NewOp; 5391 } 5392 5393 return SDValue(); 5394 } 5395 5396 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 5397 // INSERT_VECTOR_ELT is legal only for immediate indexes. 5398 SDValue Lane = Op.getOperand(2); 5399 if (!isa<ConstantSDNode>(Lane)) 5400 return SDValue(); 5401 5402 return Op; 5403 } 5404 5405 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 5406 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 5407 SDValue Lane = Op.getOperand(1); 5408 if (!isa<ConstantSDNode>(Lane)) 5409 return SDValue(); 5410 5411 SDValue Vec = Op.getOperand(0); 5412 if (Op.getValueType() == MVT::i32 && 5413 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) { 5414 SDLoc dl(Op); 5415 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 5416 } 5417 5418 return Op; 5419 } 5420 5421 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 5422 // The only time a CONCAT_VECTORS operation can have legal types is when 5423 // two 64-bit vectors are concatenated to a 128-bit vector. 5424 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 5425 "unexpected CONCAT_VECTORS"); 5426 SDLoc dl(Op); 5427 SDValue Val = DAG.getUNDEF(MVT::v2f64); 5428 SDValue Op0 = Op.getOperand(0); 5429 SDValue Op1 = Op.getOperand(1); 5430 if (Op0.getOpcode() != ISD::UNDEF) 5431 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 5432 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 5433 DAG.getIntPtrConstant(0)); 5434 if (Op1.getOpcode() != ISD::UNDEF) 5435 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 5436 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 5437 DAG.getIntPtrConstant(1)); 5438 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 5439 } 5440 5441 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 5442 /// element has been zero/sign-extended, depending on the isSigned parameter, 5443 /// from an integer type half its size. 5444 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 5445 bool isSigned) { 5446 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 5447 EVT VT = N->getValueType(0); 5448 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 5449 SDNode *BVN = N->getOperand(0).getNode(); 5450 if (BVN->getValueType(0) != MVT::v4i32 || 5451 BVN->getOpcode() != ISD::BUILD_VECTOR) 5452 return false; 5453 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 5454 unsigned HiElt = 1 - LoElt; 5455 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 5456 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 5457 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 5458 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 5459 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 5460 return false; 5461 if (isSigned) { 5462 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 5463 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 5464 return true; 5465 } else { 5466 if (Hi0->isNullValue() && Hi1->isNullValue()) 5467 return true; 5468 } 5469 return false; 5470 } 5471 5472 if (N->getOpcode() != ISD::BUILD_VECTOR) 5473 return false; 5474 5475 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 5476 SDNode *Elt = N->getOperand(i).getNode(); 5477 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 5478 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5479 unsigned HalfSize = EltSize / 2; 5480 if (isSigned) { 5481 if (!isIntN(HalfSize, C->getSExtValue())) 5482 return false; 5483 } else { 5484 if (!isUIntN(HalfSize, C->getZExtValue())) 5485 return false; 5486 } 5487 continue; 5488 } 5489 return false; 5490 } 5491 5492 return true; 5493 } 5494 5495 /// isSignExtended - Check if a node is a vector value that is sign-extended 5496 /// or a constant BUILD_VECTOR with sign-extended elements. 5497 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 5498 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 5499 return true; 5500 if (isExtendedBUILD_VECTOR(N, DAG, true)) 5501 return true; 5502 return false; 5503 } 5504 5505 /// isZeroExtended - Check if a node is a vector value that is zero-extended 5506 /// or a constant BUILD_VECTOR with zero-extended elements. 5507 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 5508 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 5509 return true; 5510 if (isExtendedBUILD_VECTOR(N, DAG, false)) 5511 return true; 5512 return false; 5513 } 5514 5515 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 5516 if (OrigVT.getSizeInBits() >= 64) 5517 return OrigVT; 5518 5519 assert(OrigVT.isSimple() && "Expecting a simple value type"); 5520 5521 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 5522 switch (OrigSimpleTy) { 5523 default: llvm_unreachable("Unexpected Vector Type"); 5524 case MVT::v2i8: 5525 case MVT::v2i16: 5526 return MVT::v2i32; 5527 case MVT::v4i8: 5528 return MVT::v4i16; 5529 } 5530 } 5531 5532 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 5533 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 5534 /// We insert the required extension here to get the vector to fill a D register. 5535 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 5536 const EVT &OrigTy, 5537 const EVT &ExtTy, 5538 unsigned ExtOpcode) { 5539 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 5540 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 5541 // 64-bits we need to insert a new extension so that it will be 64-bits. 5542 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 5543 if (OrigTy.getSizeInBits() >= 64) 5544 return N; 5545 5546 // Must extend size to at least 64 bits to be used as an operand for VMULL. 5547 EVT NewVT = getExtensionTo64Bits(OrigTy); 5548 5549 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 5550 } 5551 5552 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 5553 /// does not do any sign/zero extension. If the original vector is less 5554 /// than 64 bits, an appropriate extension will be added after the load to 5555 /// reach a total size of 64 bits. We have to add the extension separately 5556 /// because ARM does not have a sign/zero extending load for vectors. 5557 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 5558 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 5559 5560 // The load already has the right type. 5561 if (ExtendedTy == LD->getMemoryVT()) 5562 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 5563 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(), 5564 LD->isNonTemporal(), LD->isInvariant(), 5565 LD->getAlignment()); 5566 5567 // We need to create a zextload/sextload. We cannot just create a load 5568 // followed by a zext/zext node because LowerMUL is also run during normal 5569 // operation legalization where we can't create illegal types. 5570 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 5571 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 5572 LD->getMemoryVT(), LD->isVolatile(), 5573 LD->isNonTemporal(), LD->getAlignment()); 5574 } 5575 5576 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 5577 /// extending load, or BUILD_VECTOR with extended elements, return the 5578 /// unextended value. The unextended vector should be 64 bits so that it can 5579 /// be used as an operand to a VMULL instruction. If the original vector size 5580 /// before extension is less than 64 bits we add a an extension to resize 5581 /// the vector to 64 bits. 5582 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 5583 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 5584 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 5585 N->getOperand(0)->getValueType(0), 5586 N->getValueType(0), 5587 N->getOpcode()); 5588 5589 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) 5590 return SkipLoadExtensionForVMULL(LD, DAG); 5591 5592 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 5593 // have been legalized as a BITCAST from v4i32. 5594 if (N->getOpcode() == ISD::BITCAST) { 5595 SDNode *BVN = N->getOperand(0).getNode(); 5596 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 5597 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 5598 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 5599 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32, 5600 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2)); 5601 } 5602 // Construct a new BUILD_VECTOR with elements truncated to half the size. 5603 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 5604 EVT VT = N->getValueType(0); 5605 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 5606 unsigned NumElts = VT.getVectorNumElements(); 5607 MVT TruncVT = MVT::getIntegerVT(EltSize); 5608 SmallVector<SDValue, 8> Ops; 5609 for (unsigned i = 0; i != NumElts; ++i) { 5610 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 5611 const APInt &CInt = C->getAPIntValue(); 5612 // Element types smaller than 32 bits are not legal, so use i32 elements. 5613 // The values are implicitly truncated so sext vs. zext doesn't matter. 5614 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32)); 5615 } 5616 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), 5617 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts); 5618 } 5619 5620 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 5621 unsigned Opcode = N->getOpcode(); 5622 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 5623 SDNode *N0 = N->getOperand(0).getNode(); 5624 SDNode *N1 = N->getOperand(1).getNode(); 5625 return N0->hasOneUse() && N1->hasOneUse() && 5626 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 5627 } 5628 return false; 5629 } 5630 5631 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 5632 unsigned Opcode = N->getOpcode(); 5633 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 5634 SDNode *N0 = N->getOperand(0).getNode(); 5635 SDNode *N1 = N->getOperand(1).getNode(); 5636 return N0->hasOneUse() && N1->hasOneUse() && 5637 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 5638 } 5639 return false; 5640 } 5641 5642 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 5643 // Multiplications are only custom-lowered for 128-bit vectors so that 5644 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 5645 EVT VT = Op.getValueType(); 5646 assert(VT.is128BitVector() && VT.isInteger() && 5647 "unexpected type for custom-lowering ISD::MUL"); 5648 SDNode *N0 = Op.getOperand(0).getNode(); 5649 SDNode *N1 = Op.getOperand(1).getNode(); 5650 unsigned NewOpc = 0; 5651 bool isMLA = false; 5652 bool isN0SExt = isSignExtended(N0, DAG); 5653 bool isN1SExt = isSignExtended(N1, DAG); 5654 if (isN0SExt && isN1SExt) 5655 NewOpc = ARMISD::VMULLs; 5656 else { 5657 bool isN0ZExt = isZeroExtended(N0, DAG); 5658 bool isN1ZExt = isZeroExtended(N1, DAG); 5659 if (isN0ZExt && isN1ZExt) 5660 NewOpc = ARMISD::VMULLu; 5661 else if (isN1SExt || isN1ZExt) { 5662 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 5663 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 5664 if (isN1SExt && isAddSubSExt(N0, DAG)) { 5665 NewOpc = ARMISD::VMULLs; 5666 isMLA = true; 5667 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 5668 NewOpc = ARMISD::VMULLu; 5669 isMLA = true; 5670 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 5671 std::swap(N0, N1); 5672 NewOpc = ARMISD::VMULLu; 5673 isMLA = true; 5674 } 5675 } 5676 5677 if (!NewOpc) { 5678 if (VT == MVT::v2i64) 5679 // Fall through to expand this. It is not legal. 5680 return SDValue(); 5681 else 5682 // Other vector multiplications are legal. 5683 return Op; 5684 } 5685 } 5686 5687 // Legalize to a VMULL instruction. 5688 SDLoc DL(Op); 5689 SDValue Op0; 5690 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 5691 if (!isMLA) { 5692 Op0 = SkipExtensionForVMULL(N0, DAG); 5693 assert(Op0.getValueType().is64BitVector() && 5694 Op1.getValueType().is64BitVector() && 5695 "unexpected types for extended operands to VMULL"); 5696 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 5697 } 5698 5699 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 5700 // isel lowering to take advantage of no-stall back to back vmul + vmla. 5701 // vmull q0, d4, d6 5702 // vmlal q0, d5, d6 5703 // is faster than 5704 // vaddl q0, d4, d5 5705 // vmovl q1, d6 5706 // vmul q0, q0, q1 5707 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 5708 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 5709 EVT Op1VT = Op1.getValueType(); 5710 return DAG.getNode(N0->getOpcode(), DL, VT, 5711 DAG.getNode(NewOpc, DL, VT, 5712 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 5713 DAG.getNode(NewOpc, DL, VT, 5714 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 5715 } 5716 5717 static SDValue 5718 LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) { 5719 // Convert to float 5720 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 5721 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 5722 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 5723 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 5724 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 5725 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 5726 // Get reciprocal estimate. 5727 // float4 recip = vrecpeq_f32(yf); 5728 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5729 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y); 5730 // Because char has a smaller range than uchar, we can actually get away 5731 // without any newton steps. This requires that we use a weird bias 5732 // of 0xb000, however (again, this has been exhaustively tested). 5733 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 5734 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 5735 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 5736 Y = DAG.getConstant(0xb000, MVT::i32); 5737 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y); 5738 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 5739 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 5740 // Convert back to short. 5741 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 5742 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 5743 return X; 5744 } 5745 5746 static SDValue 5747 LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) { 5748 SDValue N2; 5749 // Convert to float. 5750 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 5751 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 5752 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 5753 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 5754 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 5755 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 5756 5757 // Use reciprocal estimate and one refinement step. 5758 // float4 recip = vrecpeq_f32(yf); 5759 // recip *= vrecpsq_f32(yf, recip); 5760 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5761 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1); 5762 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5763 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 5764 N1, N2); 5765 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 5766 // Because short has a smaller range than ushort, we can actually get away 5767 // with only a single newton step. This requires that we use a weird bias 5768 // of 89, however (again, this has been exhaustively tested). 5769 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 5770 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 5771 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 5772 N1 = DAG.getConstant(0x89, MVT::i32); 5773 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 5774 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 5775 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 5776 // Convert back to integer and return. 5777 // return vmovn_s32(vcvt_s32_f32(result)); 5778 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 5779 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 5780 return N0; 5781 } 5782 5783 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 5784 EVT VT = Op.getValueType(); 5785 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 5786 "unexpected type for custom-lowering ISD::SDIV"); 5787 5788 SDLoc dl(Op); 5789 SDValue N0 = Op.getOperand(0); 5790 SDValue N1 = Op.getOperand(1); 5791 SDValue N2, N3; 5792 5793 if (VT == MVT::v8i8) { 5794 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 5795 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 5796 5797 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5798 DAG.getIntPtrConstant(4)); 5799 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5800 DAG.getIntPtrConstant(4)); 5801 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5802 DAG.getIntPtrConstant(0)); 5803 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5804 DAG.getIntPtrConstant(0)); 5805 5806 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 5807 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 5808 5809 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 5810 N0 = LowerCONCAT_VECTORS(N0, DAG); 5811 5812 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 5813 return N0; 5814 } 5815 return LowerSDIV_v4i16(N0, N1, dl, DAG); 5816 } 5817 5818 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 5819 EVT VT = Op.getValueType(); 5820 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 5821 "unexpected type for custom-lowering ISD::UDIV"); 5822 5823 SDLoc dl(Op); 5824 SDValue N0 = Op.getOperand(0); 5825 SDValue N1 = Op.getOperand(1); 5826 SDValue N2, N3; 5827 5828 if (VT == MVT::v8i8) { 5829 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 5830 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 5831 5832 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5833 DAG.getIntPtrConstant(4)); 5834 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5835 DAG.getIntPtrConstant(4)); 5836 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5837 DAG.getIntPtrConstant(0)); 5838 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5839 DAG.getIntPtrConstant(0)); 5840 5841 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 5842 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 5843 5844 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 5845 N0 = LowerCONCAT_VECTORS(N0, DAG); 5846 5847 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 5848 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32), 5849 N0); 5850 return N0; 5851 } 5852 5853 // v4i16 sdiv ... Convert to float. 5854 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 5855 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 5856 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 5857 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 5858 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 5859 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 5860 5861 // Use reciprocal estimate and two refinement steps. 5862 // float4 recip = vrecpeq_f32(yf); 5863 // recip *= vrecpsq_f32(yf, recip); 5864 // recip *= vrecpsq_f32(yf, recip); 5865 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5866 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1); 5867 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5868 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 5869 BN1, N2); 5870 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 5871 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5872 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 5873 BN1, N2); 5874 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 5875 // Simply multiplying by the reciprocal estimate can leave us a few ulps 5876 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 5877 // and that it will never cause us to return an answer too large). 5878 // float4 result = as_float4(as_int4(xf*recip) + 2); 5879 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 5880 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 5881 N1 = DAG.getConstant(2, MVT::i32); 5882 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 5883 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 5884 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 5885 // Convert back to integer and return. 5886 // return vmovn_u32(vcvt_s32_f32(result)); 5887 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 5888 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 5889 return N0; 5890 } 5891 5892 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 5893 EVT VT = Op.getNode()->getValueType(0); 5894 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 5895 5896 unsigned Opc; 5897 bool ExtraOp = false; 5898 switch (Op.getOpcode()) { 5899 default: llvm_unreachable("Invalid code"); 5900 case ISD::ADDC: Opc = ARMISD::ADDC; break; 5901 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 5902 case ISD::SUBC: Opc = ARMISD::SUBC; break; 5903 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 5904 } 5905 5906 if (!ExtraOp) 5907 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 5908 Op.getOperand(1)); 5909 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 5910 Op.getOperand(1), Op.getOperand(2)); 5911 } 5912 5913 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 5914 // Monotonic load/store is legal for all targets 5915 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic) 5916 return Op; 5917 5918 // Aquire/Release load/store is not legal for targets without a 5919 // dmb or equivalent available. 5920 return SDValue(); 5921 } 5922 5923 static void 5924 ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results, 5925 SelectionDAG &DAG, unsigned NewOp) { 5926 SDLoc dl(Node); 5927 assert (Node->getValueType(0) == MVT::i64 && 5928 "Only know how to expand i64 atomics"); 5929 5930 SmallVector<SDValue, 6> Ops; 5931 Ops.push_back(Node->getOperand(0)); // Chain 5932 Ops.push_back(Node->getOperand(1)); // Ptr 5933 // Low part of Val1 5934 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5935 Node->getOperand(2), DAG.getIntPtrConstant(0))); 5936 // High part of Val1 5937 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5938 Node->getOperand(2), DAG.getIntPtrConstant(1))); 5939 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) { 5940 // High part of Val1 5941 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5942 Node->getOperand(3), DAG.getIntPtrConstant(0))); 5943 // High part of Val2 5944 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5945 Node->getOperand(3), DAG.getIntPtrConstant(1))); 5946 } 5947 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 5948 SDValue Result = 5949 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64, 5950 cast<MemSDNode>(Node)->getMemOperand()); 5951 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) }; 5952 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); 5953 Results.push_back(Result.getValue(2)); 5954 } 5955 5956 static void ReplaceREADCYCLECOUNTER(SDNode *N, 5957 SmallVectorImpl<SDValue> &Results, 5958 SelectionDAG &DAG, 5959 const ARMSubtarget *Subtarget) { 5960 SDLoc DL(N); 5961 SDValue Cycles32, OutChain; 5962 5963 if (Subtarget->hasPerfMon()) { 5964 // Under Power Management extensions, the cycle-count is: 5965 // mrc p15, #0, <Rt>, c9, c13, #0 5966 SDValue Ops[] = { N->getOperand(0), // Chain 5967 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32), 5968 DAG.getConstant(15, MVT::i32), 5969 DAG.getConstant(0, MVT::i32), 5970 DAG.getConstant(9, MVT::i32), 5971 DAG.getConstant(13, MVT::i32), 5972 DAG.getConstant(0, MVT::i32) 5973 }; 5974 5975 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 5976 DAG.getVTList(MVT::i32, MVT::Other), &Ops[0], 5977 array_lengthof(Ops)); 5978 OutChain = Cycles32.getValue(1); 5979 } else { 5980 // Intrinsic is defined to return 0 on unsupported platforms. Technically 5981 // there are older ARM CPUs that have implementation-specific ways of 5982 // obtaining this information (FIXME!). 5983 Cycles32 = DAG.getConstant(0, MVT::i32); 5984 OutChain = DAG.getEntryNode(); 5985 } 5986 5987 5988 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, 5989 Cycles32, DAG.getConstant(0, MVT::i32)); 5990 Results.push_back(Cycles64); 5991 Results.push_back(OutChain); 5992 } 5993 5994 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 5995 switch (Op.getOpcode()) { 5996 default: llvm_unreachable("Don't know how to custom lower this!"); 5997 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 5998 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 5999 case ISD::GlobalAddress: 6000 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) : 6001 LowerGlobalAddressELF(Op, DAG); 6002 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 6003 case ISD::SELECT: return LowerSELECT(Op, DAG); 6004 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 6005 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 6006 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 6007 case ISD::VASTART: return LowerVASTART(Op, DAG); 6008 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 6009 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 6010 case ISD::SINT_TO_FP: 6011 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 6012 case ISD::FP_TO_SINT: 6013 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 6014 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 6015 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 6016 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 6017 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG); 6018 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 6019 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 6020 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 6021 Subtarget); 6022 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 6023 case ISD::SHL: 6024 case ISD::SRL: 6025 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 6026 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 6027 case ISD::SRL_PARTS: 6028 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 6029 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 6030 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 6031 case ISD::SETCC: return LowerVSETCC(Op, DAG); 6032 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 6033 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 6034 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 6035 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 6036 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 6037 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 6038 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 6039 case ISD::MUL: return LowerMUL(Op, DAG); 6040 case ISD::SDIV: return LowerSDIV(Op, DAG); 6041 case ISD::UDIV: return LowerUDIV(Op, DAG); 6042 case ISD::ADDC: 6043 case ISD::ADDE: 6044 case ISD::SUBC: 6045 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 6046 case ISD::ATOMIC_LOAD: 6047 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 6048 case ISD::SDIVREM: 6049 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 6050 } 6051 } 6052 6053 /// ReplaceNodeResults - Replace the results of node with an illegal result 6054 /// type with new values built out of custom code. 6055 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 6056 SmallVectorImpl<SDValue>&Results, 6057 SelectionDAG &DAG) const { 6058 SDValue Res; 6059 switch (N->getOpcode()) { 6060 default: 6061 llvm_unreachable("Don't know how to custom expand this!"); 6062 case ISD::BITCAST: 6063 Res = ExpandBITCAST(N, DAG); 6064 break; 6065 case ISD::SIGN_EXTEND: 6066 case ISD::ZERO_EXTEND: 6067 Res = ExpandVectorExtension(N, DAG); 6068 break; 6069 case ISD::SRL: 6070 case ISD::SRA: 6071 Res = Expand64BitShift(N, DAG, Subtarget); 6072 break; 6073 case ISD::READCYCLECOUNTER: 6074 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 6075 return; 6076 case ISD::ATOMIC_LOAD_ADD: 6077 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG); 6078 return; 6079 case ISD::ATOMIC_LOAD_AND: 6080 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG); 6081 return; 6082 case ISD::ATOMIC_LOAD_NAND: 6083 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG); 6084 return; 6085 case ISD::ATOMIC_LOAD_OR: 6086 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG); 6087 return; 6088 case ISD::ATOMIC_LOAD_SUB: 6089 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG); 6090 return; 6091 case ISD::ATOMIC_LOAD_XOR: 6092 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG); 6093 return; 6094 case ISD::ATOMIC_SWAP: 6095 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG); 6096 return; 6097 case ISD::ATOMIC_CMP_SWAP: 6098 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG); 6099 return; 6100 case ISD::ATOMIC_LOAD_MIN: 6101 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG); 6102 return; 6103 case ISD::ATOMIC_LOAD_UMIN: 6104 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG); 6105 return; 6106 case ISD::ATOMIC_LOAD_MAX: 6107 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG); 6108 return; 6109 case ISD::ATOMIC_LOAD_UMAX: 6110 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG); 6111 return; 6112 } 6113 if (Res.getNode()) 6114 Results.push_back(Res); 6115 } 6116 6117 //===----------------------------------------------------------------------===// 6118 // ARM Scheduler Hooks 6119 //===----------------------------------------------------------------------===// 6120 6121 MachineBasicBlock * 6122 ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI, 6123 MachineBasicBlock *BB, 6124 unsigned Size) const { 6125 unsigned dest = MI->getOperand(0).getReg(); 6126 unsigned ptr = MI->getOperand(1).getReg(); 6127 unsigned oldval = MI->getOperand(2).getReg(); 6128 unsigned newval = MI->getOperand(3).getReg(); 6129 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6130 DebugLoc dl = MI->getDebugLoc(); 6131 bool isThumb2 = Subtarget->isThumb2(); 6132 6133 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 6134 unsigned scratch = MRI.createVirtualRegister(isThumb2 ? 6135 (const TargetRegisterClass*)&ARM::rGPRRegClass : 6136 (const TargetRegisterClass*)&ARM::GPRRegClass); 6137 6138 if (isThumb2) { 6139 MRI.constrainRegClass(dest, &ARM::rGPRRegClass); 6140 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass); 6141 MRI.constrainRegClass(newval, &ARM::rGPRRegClass); 6142 } 6143 6144 unsigned ldrOpc, strOpc; 6145 switch (Size) { 6146 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 6147 case 1: 6148 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 6149 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 6150 break; 6151 case 2: 6152 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 6153 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 6154 break; 6155 case 4: 6156 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 6157 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 6158 break; 6159 } 6160 6161 MachineFunction *MF = BB->getParent(); 6162 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6163 MachineFunction::iterator It = BB; 6164 ++It; // insert the new blocks after the current block 6165 6166 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); 6167 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); 6168 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6169 MF->insert(It, loop1MBB); 6170 MF->insert(It, loop2MBB); 6171 MF->insert(It, exitMBB); 6172 6173 // Transfer the remainder of BB and its successor edges to exitMBB. 6174 exitMBB->splice(exitMBB->begin(), BB, 6175 llvm::next(MachineBasicBlock::iterator(MI)), 6176 BB->end()); 6177 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 6178 6179 // thisMBB: 6180 // ... 6181 // fallthrough --> loop1MBB 6182 BB->addSuccessor(loop1MBB); 6183 6184 // loop1MBB: 6185 // ldrex dest, [ptr] 6186 // cmp dest, oldval 6187 // bne exitMBB 6188 BB = loop1MBB; 6189 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 6190 if (ldrOpc == ARM::t2LDREX) 6191 MIB.addImm(0); 6192 AddDefaultPred(MIB); 6193 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 6194 .addReg(dest).addReg(oldval)); 6195 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6196 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6197 BB->addSuccessor(loop2MBB); 6198 BB->addSuccessor(exitMBB); 6199 6200 // loop2MBB: 6201 // strex scratch, newval, [ptr] 6202 // cmp scratch, #0 6203 // bne loop1MBB 6204 BB = loop2MBB; 6205 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr); 6206 if (strOpc == ARM::t2STREX) 6207 MIB.addImm(0); 6208 AddDefaultPred(MIB); 6209 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6210 .addReg(scratch).addImm(0)); 6211 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6212 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6213 BB->addSuccessor(loop1MBB); 6214 BB->addSuccessor(exitMBB); 6215 6216 // exitMBB: 6217 // ... 6218 BB = exitMBB; 6219 6220 MI->eraseFromParent(); // The instruction is gone now. 6221 6222 return BB; 6223 } 6224 6225 MachineBasicBlock * 6226 ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, 6227 unsigned Size, unsigned BinOpcode) const { 6228 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 6229 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6230 6231 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6232 MachineFunction *MF = BB->getParent(); 6233 MachineFunction::iterator It = BB; 6234 ++It; 6235 6236 unsigned dest = MI->getOperand(0).getReg(); 6237 unsigned ptr = MI->getOperand(1).getReg(); 6238 unsigned incr = MI->getOperand(2).getReg(); 6239 DebugLoc dl = MI->getDebugLoc(); 6240 bool isThumb2 = Subtarget->isThumb2(); 6241 6242 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 6243 if (isThumb2) { 6244 MRI.constrainRegClass(dest, &ARM::rGPRRegClass); 6245 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass); 6246 } 6247 6248 unsigned ldrOpc, strOpc; 6249 switch (Size) { 6250 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 6251 case 1: 6252 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 6253 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 6254 break; 6255 case 2: 6256 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 6257 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 6258 break; 6259 case 4: 6260 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 6261 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 6262 break; 6263 } 6264 6265 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6266 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6267 MF->insert(It, loopMBB); 6268 MF->insert(It, exitMBB); 6269 6270 // Transfer the remainder of BB and its successor edges to exitMBB. 6271 exitMBB->splice(exitMBB->begin(), BB, 6272 llvm::next(MachineBasicBlock::iterator(MI)), 6273 BB->end()); 6274 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 6275 6276 const TargetRegisterClass *TRC = isThumb2 ? 6277 (const TargetRegisterClass*)&ARM::rGPRRegClass : 6278 (const TargetRegisterClass*)&ARM::GPRRegClass; 6279 unsigned scratch = MRI.createVirtualRegister(TRC); 6280 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC); 6281 6282 // thisMBB: 6283 // ... 6284 // fallthrough --> loopMBB 6285 BB->addSuccessor(loopMBB); 6286 6287 // loopMBB: 6288 // ldrex dest, ptr 6289 // <binop> scratch2, dest, incr 6290 // strex scratch, scratch2, ptr 6291 // cmp scratch, #0 6292 // bne- loopMBB 6293 // fallthrough --> exitMBB 6294 BB = loopMBB; 6295 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 6296 if (ldrOpc == ARM::t2LDREX) 6297 MIB.addImm(0); 6298 AddDefaultPred(MIB); 6299 if (BinOpcode) { 6300 // operand order needs to go the other way for NAND 6301 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr) 6302 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2). 6303 addReg(incr).addReg(dest)).addReg(0); 6304 else 6305 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2). 6306 addReg(dest).addReg(incr)).addReg(0); 6307 } 6308 6309 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr); 6310 if (strOpc == ARM::t2STREX) 6311 MIB.addImm(0); 6312 AddDefaultPred(MIB); 6313 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6314 .addReg(scratch).addImm(0)); 6315 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6316 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6317 6318 BB->addSuccessor(loopMBB); 6319 BB->addSuccessor(exitMBB); 6320 6321 // exitMBB: 6322 // ... 6323 BB = exitMBB; 6324 6325 MI->eraseFromParent(); // The instruction is gone now. 6326 6327 return BB; 6328 } 6329 6330 MachineBasicBlock * 6331 ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI, 6332 MachineBasicBlock *BB, 6333 unsigned Size, 6334 bool signExtend, 6335 ARMCC::CondCodes Cond) const { 6336 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6337 6338 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6339 MachineFunction *MF = BB->getParent(); 6340 MachineFunction::iterator It = BB; 6341 ++It; 6342 6343 unsigned dest = MI->getOperand(0).getReg(); 6344 unsigned ptr = MI->getOperand(1).getReg(); 6345 unsigned incr = MI->getOperand(2).getReg(); 6346 unsigned oldval = dest; 6347 DebugLoc dl = MI->getDebugLoc(); 6348 bool isThumb2 = Subtarget->isThumb2(); 6349 6350 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 6351 if (isThumb2) { 6352 MRI.constrainRegClass(dest, &ARM::rGPRRegClass); 6353 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass); 6354 } 6355 6356 unsigned ldrOpc, strOpc, extendOpc; 6357 switch (Size) { 6358 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 6359 case 1: 6360 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 6361 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 6362 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB; 6363 break; 6364 case 2: 6365 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 6366 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 6367 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH; 6368 break; 6369 case 4: 6370 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 6371 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 6372 extendOpc = 0; 6373 break; 6374 } 6375 6376 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6377 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6378 MF->insert(It, loopMBB); 6379 MF->insert(It, exitMBB); 6380 6381 // Transfer the remainder of BB and its successor edges to exitMBB. 6382 exitMBB->splice(exitMBB->begin(), BB, 6383 llvm::next(MachineBasicBlock::iterator(MI)), 6384 BB->end()); 6385 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 6386 6387 const TargetRegisterClass *TRC = isThumb2 ? 6388 (const TargetRegisterClass*)&ARM::rGPRRegClass : 6389 (const TargetRegisterClass*)&ARM::GPRRegClass; 6390 unsigned scratch = MRI.createVirtualRegister(TRC); 6391 unsigned scratch2 = MRI.createVirtualRegister(TRC); 6392 6393 // thisMBB: 6394 // ... 6395 // fallthrough --> loopMBB 6396 BB->addSuccessor(loopMBB); 6397 6398 // loopMBB: 6399 // ldrex dest, ptr 6400 // (sign extend dest, if required) 6401 // cmp dest, incr 6402 // cmov.cond scratch2, incr, dest 6403 // strex scratch, scratch2, ptr 6404 // cmp scratch, #0 6405 // bne- loopMBB 6406 // fallthrough --> exitMBB 6407 BB = loopMBB; 6408 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 6409 if (ldrOpc == ARM::t2LDREX) 6410 MIB.addImm(0); 6411 AddDefaultPred(MIB); 6412 6413 // Sign extend the value, if necessary. 6414 if (signExtend && extendOpc) { 6415 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass); 6416 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval) 6417 .addReg(dest) 6418 .addImm(0)); 6419 } 6420 6421 // Build compare and cmov instructions. 6422 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 6423 .addReg(oldval).addReg(incr)); 6424 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2) 6425 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR); 6426 6427 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr); 6428 if (strOpc == ARM::t2STREX) 6429 MIB.addImm(0); 6430 AddDefaultPred(MIB); 6431 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6432 .addReg(scratch).addImm(0)); 6433 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6434 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6435 6436 BB->addSuccessor(loopMBB); 6437 BB->addSuccessor(exitMBB); 6438 6439 // exitMBB: 6440 // ... 6441 BB = exitMBB; 6442 6443 MI->eraseFromParent(); // The instruction is gone now. 6444 6445 return BB; 6446 } 6447 6448 MachineBasicBlock * 6449 ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB, 6450 unsigned Op1, unsigned Op2, 6451 bool NeedsCarry, bool IsCmpxchg, 6452 bool IsMinMax, ARMCC::CondCodes CC) const { 6453 // This also handles ATOMIC_SWAP, indicated by Op1==0. 6454 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6455 6456 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6457 MachineFunction *MF = BB->getParent(); 6458 MachineFunction::iterator It = BB; 6459 ++It; 6460 6461 unsigned destlo = MI->getOperand(0).getReg(); 6462 unsigned desthi = MI->getOperand(1).getReg(); 6463 unsigned ptr = MI->getOperand(2).getReg(); 6464 unsigned vallo = MI->getOperand(3).getReg(); 6465 unsigned valhi = MI->getOperand(4).getReg(); 6466 DebugLoc dl = MI->getDebugLoc(); 6467 bool isThumb2 = Subtarget->isThumb2(); 6468 6469 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 6470 if (isThumb2) { 6471 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass); 6472 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass); 6473 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass); 6474 MRI.constrainRegClass(vallo, &ARM::rGPRRegClass); 6475 MRI.constrainRegClass(valhi, &ARM::rGPRRegClass); 6476 } 6477 6478 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6479 MachineBasicBlock *contBB = 0, *cont2BB = 0; 6480 if (IsCmpxchg || IsMinMax) 6481 contBB = MF->CreateMachineBasicBlock(LLVM_BB); 6482 if (IsCmpxchg) 6483 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB); 6484 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6485 6486 MF->insert(It, loopMBB); 6487 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB); 6488 if (IsCmpxchg) MF->insert(It, cont2BB); 6489 MF->insert(It, exitMBB); 6490 6491 // Transfer the remainder of BB and its successor edges to exitMBB. 6492 exitMBB->splice(exitMBB->begin(), BB, 6493 llvm::next(MachineBasicBlock::iterator(MI)), 6494 BB->end()); 6495 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 6496 6497 const TargetRegisterClass *TRC = isThumb2 ? 6498 (const TargetRegisterClass*)&ARM::tGPRRegClass : 6499 (const TargetRegisterClass*)&ARM::GPRRegClass; 6500 unsigned storesuccess = MRI.createVirtualRegister(TRC); 6501 6502 // thisMBB: 6503 // ... 6504 // fallthrough --> loopMBB 6505 BB->addSuccessor(loopMBB); 6506 6507 // loopMBB: 6508 // ldrexd r2, r3, ptr 6509 // <binopa> r0, r2, incr 6510 // <binopb> r1, r3, incr 6511 // strexd storesuccess, r0, r1, ptr 6512 // cmp storesuccess, #0 6513 // bne- loopMBB 6514 // fallthrough --> exitMBB 6515 BB = loopMBB; 6516 6517 // Load 6518 if (isThumb2) { 6519 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2LDREXD)) 6520 .addReg(destlo, RegState::Define) 6521 .addReg(desthi, RegState::Define) 6522 .addReg(ptr)); 6523 } else { 6524 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass); 6525 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDREXD)) 6526 .addReg(GPRPair0, RegState::Define).addReg(ptr)); 6527 // Copy r2/r3 into dest. (This copy will normally be coalesced.) 6528 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo) 6529 .addReg(GPRPair0, 0, ARM::gsub_0); 6530 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi) 6531 .addReg(GPRPair0, 0, ARM::gsub_1); 6532 } 6533 6534 unsigned StoreLo, StoreHi; 6535 if (IsCmpxchg) { 6536 // Add early exit 6537 for (unsigned i = 0; i < 2; i++) { 6538 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : 6539 ARM::CMPrr)) 6540 .addReg(i == 0 ? destlo : desthi) 6541 .addReg(i == 0 ? vallo : valhi)); 6542 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6543 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6544 BB->addSuccessor(exitMBB); 6545 BB->addSuccessor(i == 0 ? contBB : cont2BB); 6546 BB = (i == 0 ? contBB : cont2BB); 6547 } 6548 6549 // Copy to physregs for strexd 6550 StoreLo = MI->getOperand(5).getReg(); 6551 StoreHi = MI->getOperand(6).getReg(); 6552 } else if (Op1) { 6553 // Perform binary operation 6554 unsigned tmpRegLo = MRI.createVirtualRegister(TRC); 6555 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo) 6556 .addReg(destlo).addReg(vallo)) 6557 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry)); 6558 unsigned tmpRegHi = MRI.createVirtualRegister(TRC); 6559 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi) 6560 .addReg(desthi).addReg(valhi)) 6561 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax)); 6562 6563 StoreLo = tmpRegLo; 6564 StoreHi = tmpRegHi; 6565 } else { 6566 // Copy to physregs for strexd 6567 StoreLo = vallo; 6568 StoreHi = valhi; 6569 } 6570 if (IsMinMax) { 6571 // Compare and branch to exit block. 6572 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6573 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR); 6574 BB->addSuccessor(exitMBB); 6575 BB->addSuccessor(contBB); 6576 BB = contBB; 6577 StoreLo = vallo; 6578 StoreHi = valhi; 6579 } 6580 6581 // Store 6582 if (isThumb2) { 6583 MRI.constrainRegClass(StoreLo, &ARM::rGPRRegClass); 6584 MRI.constrainRegClass(StoreHi, &ARM::rGPRRegClass); 6585 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2STREXD), storesuccess) 6586 .addReg(StoreLo).addReg(StoreHi).addReg(ptr)); 6587 } else { 6588 // Marshal a pair... 6589 unsigned StorePair = MRI.createVirtualRegister(&ARM::GPRPairRegClass); 6590 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass); 6591 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass); 6592 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair); 6593 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1) 6594 .addReg(UndefPair) 6595 .addReg(StoreLo) 6596 .addImm(ARM::gsub_0); 6597 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), StorePair) 6598 .addReg(r1) 6599 .addReg(StoreHi) 6600 .addImm(ARM::gsub_1); 6601 6602 // ...and store it 6603 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::STREXD), storesuccess) 6604 .addReg(StorePair).addReg(ptr)); 6605 } 6606 // Cmp+jump 6607 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6608 .addReg(storesuccess).addImm(0)); 6609 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6610 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6611 6612 BB->addSuccessor(loopMBB); 6613 BB->addSuccessor(exitMBB); 6614 6615 // exitMBB: 6616 // ... 6617 BB = exitMBB; 6618 6619 MI->eraseFromParent(); // The instruction is gone now. 6620 6621 return BB; 6622 } 6623 6624 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 6625 /// registers the function context. 6626 void ARMTargetLowering:: 6627 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB, 6628 MachineBasicBlock *DispatchBB, int FI) const { 6629 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6630 DebugLoc dl = MI->getDebugLoc(); 6631 MachineFunction *MF = MBB->getParent(); 6632 MachineRegisterInfo *MRI = &MF->getRegInfo(); 6633 MachineConstantPool *MCP = MF->getConstantPool(); 6634 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 6635 const Function *F = MF->getFunction(); 6636 6637 bool isThumb = Subtarget->isThumb(); 6638 bool isThumb2 = Subtarget->isThumb2(); 6639 6640 unsigned PCLabelId = AFI->createPICLabelUId(); 6641 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 6642 ARMConstantPoolValue *CPV = 6643 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj); 6644 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 6645 6646 const TargetRegisterClass *TRC = isThumb ? 6647 (const TargetRegisterClass*)&ARM::tGPRRegClass : 6648 (const TargetRegisterClass*)&ARM::GPRRegClass; 6649 6650 // Grab constant pool and fixed stack memory operands. 6651 MachineMemOperand *CPMMO = 6652 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(), 6653 MachineMemOperand::MOLoad, 4, 4); 6654 6655 MachineMemOperand *FIMMOSt = 6656 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 6657 MachineMemOperand::MOStore, 4, 4); 6658 6659 // Load the address of the dispatch MBB into the jump buffer. 6660 if (isThumb2) { 6661 // Incoming value: jbuf 6662 // ldr.n r5, LCPI1_1 6663 // orr r5, r5, #1 6664 // add r5, pc 6665 // str r5, [$jbuf, #+4] ; &jbuf[1] 6666 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6667 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 6668 .addConstantPoolIndex(CPI) 6669 .addMemOperand(CPMMO)); 6670 // Set the low bit because of thumb mode. 6671 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6672 AddDefaultCC( 6673 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 6674 .addReg(NewVReg1, RegState::Kill) 6675 .addImm(0x01))); 6676 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6677 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 6678 .addReg(NewVReg2, RegState::Kill) 6679 .addImm(PCLabelId); 6680 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 6681 .addReg(NewVReg3, RegState::Kill) 6682 .addFrameIndex(FI) 6683 .addImm(36) // &jbuf[1] :: pc 6684 .addMemOperand(FIMMOSt)); 6685 } else if (isThumb) { 6686 // Incoming value: jbuf 6687 // ldr.n r1, LCPI1_4 6688 // add r1, pc 6689 // mov r2, #1 6690 // orrs r1, r2 6691 // add r2, $jbuf, #+4 ; &jbuf[1] 6692 // str r1, [r2] 6693 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6694 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 6695 .addConstantPoolIndex(CPI) 6696 .addMemOperand(CPMMO)); 6697 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6698 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 6699 .addReg(NewVReg1, RegState::Kill) 6700 .addImm(PCLabelId); 6701 // Set the low bit because of thumb mode. 6702 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6703 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 6704 .addReg(ARM::CPSR, RegState::Define) 6705 .addImm(1)); 6706 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6707 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 6708 .addReg(ARM::CPSR, RegState::Define) 6709 .addReg(NewVReg2, RegState::Kill) 6710 .addReg(NewVReg3, RegState::Kill)); 6711 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6712 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5) 6713 .addFrameIndex(FI) 6714 .addImm(36)); // &jbuf[1] :: pc 6715 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 6716 .addReg(NewVReg4, RegState::Kill) 6717 .addReg(NewVReg5, RegState::Kill) 6718 .addImm(0) 6719 .addMemOperand(FIMMOSt)); 6720 } else { 6721 // Incoming value: jbuf 6722 // ldr r1, LCPI1_1 6723 // add r1, pc, r1 6724 // str r1, [$jbuf, #+4] ; &jbuf[1] 6725 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6726 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 6727 .addConstantPoolIndex(CPI) 6728 .addImm(0) 6729 .addMemOperand(CPMMO)); 6730 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6731 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 6732 .addReg(NewVReg1, RegState::Kill) 6733 .addImm(PCLabelId)); 6734 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 6735 .addReg(NewVReg2, RegState::Kill) 6736 .addFrameIndex(FI) 6737 .addImm(36) // &jbuf[1] :: pc 6738 .addMemOperand(FIMMOSt)); 6739 } 6740 } 6741 6742 MachineBasicBlock *ARMTargetLowering:: 6743 EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const { 6744 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6745 DebugLoc dl = MI->getDebugLoc(); 6746 MachineFunction *MF = MBB->getParent(); 6747 MachineRegisterInfo *MRI = &MF->getRegInfo(); 6748 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 6749 MachineFrameInfo *MFI = MF->getFrameInfo(); 6750 int FI = MFI->getFunctionContextIndex(); 6751 6752 const TargetRegisterClass *TRC = Subtarget->isThumb() ? 6753 (const TargetRegisterClass*)&ARM::tGPRRegClass : 6754 (const TargetRegisterClass*)&ARM::GPRnopcRegClass; 6755 6756 // Get a mapping of the call site numbers to all of the landing pads they're 6757 // associated with. 6758 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad; 6759 unsigned MaxCSNum = 0; 6760 MachineModuleInfo &MMI = MF->getMMI(); 6761 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 6762 ++BB) { 6763 if (!BB->isLandingPad()) continue; 6764 6765 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 6766 // pad. 6767 for (MachineBasicBlock::iterator 6768 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 6769 if (!II->isEHLabel()) continue; 6770 6771 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 6772 if (!MMI.hasCallSiteLandingPad(Sym)) continue; 6773 6774 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym); 6775 for (SmallVectorImpl<unsigned>::iterator 6776 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 6777 CSI != CSE; ++CSI) { 6778 CallSiteNumToLPad[*CSI].push_back(BB); 6779 MaxCSNum = std::max(MaxCSNum, *CSI); 6780 } 6781 break; 6782 } 6783 } 6784 6785 // Get an ordered list of the machine basic blocks for the jump table. 6786 std::vector<MachineBasicBlock*> LPadList; 6787 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs; 6788 LPadList.reserve(CallSiteNumToLPad.size()); 6789 for (unsigned I = 1; I <= MaxCSNum; ++I) { 6790 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 6791 for (SmallVectorImpl<MachineBasicBlock*>::iterator 6792 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 6793 LPadList.push_back(*II); 6794 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 6795 } 6796 } 6797 6798 assert(!LPadList.empty() && 6799 "No landing pad destinations for the dispatch jump table!"); 6800 6801 // Create the jump table and associated information. 6802 MachineJumpTableInfo *JTI = 6803 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 6804 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 6805 unsigned UId = AFI->createJumpTableUId(); 6806 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 6807 6808 // Create the MBBs for the dispatch code. 6809 6810 // Shove the dispatch's address into the return slot in the function context. 6811 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 6812 DispatchBB->setIsLandingPad(); 6813 6814 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 6815 unsigned trap_opcode; 6816 if (Subtarget->isThumb()) 6817 trap_opcode = ARM::tTRAP; 6818 else 6819 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 6820 6821 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 6822 DispatchBB->addSuccessor(TrapBB); 6823 6824 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 6825 DispatchBB->addSuccessor(DispContBB); 6826 6827 // Insert and MBBs. 6828 MF->insert(MF->end(), DispatchBB); 6829 MF->insert(MF->end(), DispContBB); 6830 MF->insert(MF->end(), TrapBB); 6831 6832 // Insert code into the entry block that creates and registers the function 6833 // context. 6834 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 6835 6836 MachineMemOperand *FIMMOLd = 6837 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 6838 MachineMemOperand::MOLoad | 6839 MachineMemOperand::MOVolatile, 4, 4); 6840 6841 MachineInstrBuilder MIB; 6842 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 6843 6844 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 6845 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 6846 6847 // Add a register mask with no preserved registers. This results in all 6848 // registers being marked as clobbered. 6849 MIB.addRegMask(RI.getNoPreservedMask()); 6850 6851 unsigned NumLPads = LPadList.size(); 6852 if (Subtarget->isThumb2()) { 6853 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6854 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 6855 .addFrameIndex(FI) 6856 .addImm(4) 6857 .addMemOperand(FIMMOLd)); 6858 6859 if (NumLPads < 256) { 6860 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 6861 .addReg(NewVReg1) 6862 .addImm(LPadList.size())); 6863 } else { 6864 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6865 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 6866 .addImm(NumLPads & 0xFFFF)); 6867 6868 unsigned VReg2 = VReg1; 6869 if ((NumLPads & 0xFFFF0000) != 0) { 6870 VReg2 = MRI->createVirtualRegister(TRC); 6871 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 6872 .addReg(VReg1) 6873 .addImm(NumLPads >> 16)); 6874 } 6875 6876 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 6877 .addReg(NewVReg1) 6878 .addReg(VReg2)); 6879 } 6880 6881 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 6882 .addMBB(TrapBB) 6883 .addImm(ARMCC::HI) 6884 .addReg(ARM::CPSR); 6885 6886 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6887 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3) 6888 .addJumpTableIndex(MJTI) 6889 .addImm(UId)); 6890 6891 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6892 AddDefaultCC( 6893 AddDefaultPred( 6894 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 6895 .addReg(NewVReg3, RegState::Kill) 6896 .addReg(NewVReg1) 6897 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 6898 6899 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 6900 .addReg(NewVReg4, RegState::Kill) 6901 .addReg(NewVReg1) 6902 .addJumpTableIndex(MJTI) 6903 .addImm(UId); 6904 } else if (Subtarget->isThumb()) { 6905 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6906 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 6907 .addFrameIndex(FI) 6908 .addImm(1) 6909 .addMemOperand(FIMMOLd)); 6910 6911 if (NumLPads < 256) { 6912 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 6913 .addReg(NewVReg1) 6914 .addImm(NumLPads)); 6915 } else { 6916 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6917 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6918 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6919 6920 // MachineConstantPool wants an explicit alignment. 6921 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 6922 if (Align == 0) 6923 Align = getDataLayout()->getTypeAllocSize(C->getType()); 6924 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6925 6926 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6927 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 6928 .addReg(VReg1, RegState::Define) 6929 .addConstantPoolIndex(Idx)); 6930 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 6931 .addReg(NewVReg1) 6932 .addReg(VReg1)); 6933 } 6934 6935 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 6936 .addMBB(TrapBB) 6937 .addImm(ARMCC::HI) 6938 .addReg(ARM::CPSR); 6939 6940 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6941 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 6942 .addReg(ARM::CPSR, RegState::Define) 6943 .addReg(NewVReg1) 6944 .addImm(2)); 6945 6946 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6947 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 6948 .addJumpTableIndex(MJTI) 6949 .addImm(UId)); 6950 6951 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6952 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 6953 .addReg(ARM::CPSR, RegState::Define) 6954 .addReg(NewVReg2, RegState::Kill) 6955 .addReg(NewVReg3)); 6956 6957 MachineMemOperand *JTMMOLd = 6958 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6959 MachineMemOperand::MOLoad, 4, 4); 6960 6961 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6962 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 6963 .addReg(NewVReg4, RegState::Kill) 6964 .addImm(0) 6965 .addMemOperand(JTMMOLd)); 6966 6967 unsigned NewVReg6 = NewVReg5; 6968 if (RelocM == Reloc::PIC_) { 6969 NewVReg6 = MRI->createVirtualRegister(TRC); 6970 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 6971 .addReg(ARM::CPSR, RegState::Define) 6972 .addReg(NewVReg5, RegState::Kill) 6973 .addReg(NewVReg3)); 6974 } 6975 6976 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 6977 .addReg(NewVReg6, RegState::Kill) 6978 .addJumpTableIndex(MJTI) 6979 .addImm(UId); 6980 } else { 6981 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6982 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 6983 .addFrameIndex(FI) 6984 .addImm(4) 6985 .addMemOperand(FIMMOLd)); 6986 6987 if (NumLPads < 256) { 6988 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 6989 .addReg(NewVReg1) 6990 .addImm(NumLPads)); 6991 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 6992 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6993 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 6994 .addImm(NumLPads & 0xFFFF)); 6995 6996 unsigned VReg2 = VReg1; 6997 if ((NumLPads & 0xFFFF0000) != 0) { 6998 VReg2 = MRI->createVirtualRegister(TRC); 6999 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 7000 .addReg(VReg1) 7001 .addImm(NumLPads >> 16)); 7002 } 7003 7004 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 7005 .addReg(NewVReg1) 7006 .addReg(VReg2)); 7007 } else { 7008 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7009 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7010 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 7011 7012 // MachineConstantPool wants an explicit alignment. 7013 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 7014 if (Align == 0) 7015 Align = getDataLayout()->getTypeAllocSize(C->getType()); 7016 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7017 7018 unsigned VReg1 = MRI->createVirtualRegister(TRC); 7019 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 7020 .addReg(VReg1, RegState::Define) 7021 .addConstantPoolIndex(Idx) 7022 .addImm(0)); 7023 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 7024 .addReg(NewVReg1) 7025 .addReg(VReg1, RegState::Kill)); 7026 } 7027 7028 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 7029 .addMBB(TrapBB) 7030 .addImm(ARMCC::HI) 7031 .addReg(ARM::CPSR); 7032 7033 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 7034 AddDefaultCC( 7035 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 7036 .addReg(NewVReg1) 7037 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 7038 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 7039 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 7040 .addJumpTableIndex(MJTI) 7041 .addImm(UId)); 7042 7043 MachineMemOperand *JTMMOLd = 7044 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 7045 MachineMemOperand::MOLoad, 4, 4); 7046 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 7047 AddDefaultPred( 7048 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 7049 .addReg(NewVReg3, RegState::Kill) 7050 .addReg(NewVReg4) 7051 .addImm(0) 7052 .addMemOperand(JTMMOLd)); 7053 7054 if (RelocM == Reloc::PIC_) { 7055 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 7056 .addReg(NewVReg5, RegState::Kill) 7057 .addReg(NewVReg4) 7058 .addJumpTableIndex(MJTI) 7059 .addImm(UId); 7060 } else { 7061 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 7062 .addReg(NewVReg5, RegState::Kill) 7063 .addJumpTableIndex(MJTI) 7064 .addImm(UId); 7065 } 7066 } 7067 7068 // Add the jump table entries as successors to the MBB. 7069 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 7070 for (std::vector<MachineBasicBlock*>::iterator 7071 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 7072 MachineBasicBlock *CurMBB = *I; 7073 if (SeenMBBs.insert(CurMBB)) 7074 DispContBB->addSuccessor(CurMBB); 7075 } 7076 7077 // N.B. the order the invoke BBs are processed in doesn't matter here. 7078 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF); 7079 SmallVector<MachineBasicBlock*, 64> MBBLPads; 7080 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator 7081 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) { 7082 MachineBasicBlock *BB = *I; 7083 7084 // Remove the landing pad successor from the invoke block and replace it 7085 // with the new dispatch block. 7086 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 7087 BB->succ_end()); 7088 while (!Successors.empty()) { 7089 MachineBasicBlock *SMBB = Successors.pop_back_val(); 7090 if (SMBB->isLandingPad()) { 7091 BB->removeSuccessor(SMBB); 7092 MBBLPads.push_back(SMBB); 7093 } 7094 } 7095 7096 BB->addSuccessor(DispatchBB); 7097 7098 // Find the invoke call and mark all of the callee-saved registers as 7099 // 'implicit defined' so that they're spilled. This prevents code from 7100 // moving instructions to before the EH block, where they will never be 7101 // executed. 7102 for (MachineBasicBlock::reverse_iterator 7103 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 7104 if (!II->isCall()) continue; 7105 7106 DenseMap<unsigned, bool> DefRegs; 7107 for (MachineInstr::mop_iterator 7108 OI = II->operands_begin(), OE = II->operands_end(); 7109 OI != OE; ++OI) { 7110 if (!OI->isReg()) continue; 7111 DefRegs[OI->getReg()] = true; 7112 } 7113 7114 MachineInstrBuilder MIB(*MF, &*II); 7115 7116 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 7117 unsigned Reg = SavedRegs[i]; 7118 if (Subtarget->isThumb2() && 7119 !ARM::tGPRRegClass.contains(Reg) && 7120 !ARM::hGPRRegClass.contains(Reg)) 7121 continue; 7122 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 7123 continue; 7124 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 7125 continue; 7126 if (!DefRegs[Reg]) 7127 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 7128 } 7129 7130 break; 7131 } 7132 } 7133 7134 // Mark all former landing pads as non-landing pads. The dispatch is the only 7135 // landing pad now. 7136 for (SmallVectorImpl<MachineBasicBlock*>::iterator 7137 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 7138 (*I)->setIsLandingPad(false); 7139 7140 // The instruction is gone now. 7141 MI->eraseFromParent(); 7142 7143 return MBB; 7144 } 7145 7146 static 7147 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 7148 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 7149 E = MBB->succ_end(); I != E; ++I) 7150 if (*I != Succ) 7151 return *I; 7152 llvm_unreachable("Expecting a BB with two successors!"); 7153 } 7154 7155 MachineBasicBlock *ARMTargetLowering:: 7156 EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const { 7157 // This pseudo instruction has 3 operands: dst, src, size 7158 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 7159 // Otherwise, we will generate unrolled scalar copies. 7160 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 7161 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7162 MachineFunction::iterator It = BB; 7163 ++It; 7164 7165 unsigned dest = MI->getOperand(0).getReg(); 7166 unsigned src = MI->getOperand(1).getReg(); 7167 unsigned SizeVal = MI->getOperand(2).getImm(); 7168 unsigned Align = MI->getOperand(3).getImm(); 7169 DebugLoc dl = MI->getDebugLoc(); 7170 7171 bool isThumb2 = Subtarget->isThumb2(); 7172 MachineFunction *MF = BB->getParent(); 7173 MachineRegisterInfo &MRI = MF->getRegInfo(); 7174 unsigned ldrOpc, strOpc, UnitSize = 0; 7175 7176 const TargetRegisterClass *TRC = isThumb2 ? 7177 (const TargetRegisterClass*)&ARM::tGPRRegClass : 7178 (const TargetRegisterClass*)&ARM::GPRRegClass; 7179 const TargetRegisterClass *TRC_Vec = 0; 7180 7181 if (Align & 1) { 7182 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM; 7183 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM; 7184 UnitSize = 1; 7185 } else if (Align & 2) { 7186 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST; 7187 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST; 7188 UnitSize = 2; 7189 } else { 7190 // Check whether we can use NEON instructions. 7191 if (!MF->getFunction()->getAttributes(). 7192 hasAttribute(AttributeSet::FunctionIndex, 7193 Attribute::NoImplicitFloat) && 7194 Subtarget->hasNEON()) { 7195 if ((Align % 16 == 0) && SizeVal >= 16) { 7196 ldrOpc = ARM::VLD1q32wb_fixed; 7197 strOpc = ARM::VST1q32wb_fixed; 7198 UnitSize = 16; 7199 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass; 7200 } 7201 else if ((Align % 8 == 0) && SizeVal >= 8) { 7202 ldrOpc = ARM::VLD1d32wb_fixed; 7203 strOpc = ARM::VST1d32wb_fixed; 7204 UnitSize = 8; 7205 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass; 7206 } 7207 } 7208 // Can't use NEON instructions. 7209 if (UnitSize == 0) { 7210 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM; 7211 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM; 7212 UnitSize = 4; 7213 } 7214 } 7215 7216 unsigned BytesLeft = SizeVal % UnitSize; 7217 unsigned LoopSize = SizeVal - BytesLeft; 7218 7219 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 7220 // Use LDR and STR to copy. 7221 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 7222 // [destOut] = STR_POST(scratch, destIn, UnitSize) 7223 unsigned srcIn = src; 7224 unsigned destIn = dest; 7225 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 7226 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC); 7227 unsigned srcOut = MRI.createVirtualRegister(TRC); 7228 unsigned destOut = MRI.createVirtualRegister(TRC); 7229 if (UnitSize >= 8) { 7230 AddDefaultPred(BuildMI(*BB, MI, dl, 7231 TII->get(ldrOpc), scratch) 7232 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0)); 7233 7234 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 7235 .addReg(destIn).addImm(0).addReg(scratch)); 7236 } else if (isThumb2) { 7237 AddDefaultPred(BuildMI(*BB, MI, dl, 7238 TII->get(ldrOpc), scratch) 7239 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize)); 7240 7241 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 7242 .addReg(scratch).addReg(destIn) 7243 .addImm(UnitSize)); 7244 } else { 7245 AddDefaultPred(BuildMI(*BB, MI, dl, 7246 TII->get(ldrOpc), scratch) 7247 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0) 7248 .addImm(UnitSize)); 7249 7250 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 7251 .addReg(scratch).addReg(destIn) 7252 .addReg(0).addImm(UnitSize)); 7253 } 7254 srcIn = srcOut; 7255 destIn = destOut; 7256 } 7257 7258 // Handle the leftover bytes with LDRB and STRB. 7259 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 7260 // [destOut] = STRB_POST(scratch, destIn, 1) 7261 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM; 7262 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM; 7263 for (unsigned i = 0; i < BytesLeft; i++) { 7264 unsigned scratch = MRI.createVirtualRegister(TRC); 7265 unsigned srcOut = MRI.createVirtualRegister(TRC); 7266 unsigned destOut = MRI.createVirtualRegister(TRC); 7267 if (isThumb2) { 7268 AddDefaultPred(BuildMI(*BB, MI, dl, 7269 TII->get(ldrOpc),scratch) 7270 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1)); 7271 7272 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 7273 .addReg(scratch).addReg(destIn) 7274 .addReg(0).addImm(1)); 7275 } else { 7276 AddDefaultPred(BuildMI(*BB, MI, dl, 7277 TII->get(ldrOpc),scratch) 7278 .addReg(srcOut, RegState::Define).addReg(srcIn) 7279 .addReg(0).addImm(1)); 7280 7281 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 7282 .addReg(scratch).addReg(destIn) 7283 .addReg(0).addImm(1)); 7284 } 7285 srcIn = srcOut; 7286 destIn = destOut; 7287 } 7288 MI->eraseFromParent(); // The instruction is gone now. 7289 return BB; 7290 } 7291 7292 // Expand the pseudo op to a loop. 7293 // thisMBB: 7294 // ... 7295 // movw varEnd, # --> with thumb2 7296 // movt varEnd, # 7297 // ldrcp varEnd, idx --> without thumb2 7298 // fallthrough --> loopMBB 7299 // loopMBB: 7300 // PHI varPhi, varEnd, varLoop 7301 // PHI srcPhi, src, srcLoop 7302 // PHI destPhi, dst, destLoop 7303 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7304 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 7305 // subs varLoop, varPhi, #UnitSize 7306 // bne loopMBB 7307 // fallthrough --> exitMBB 7308 // exitMBB: 7309 // epilogue to handle left-over bytes 7310 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7311 // [destOut] = STRB_POST(scratch, destLoop, 1) 7312 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7313 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7314 MF->insert(It, loopMBB); 7315 MF->insert(It, exitMBB); 7316 7317 // Transfer the remainder of BB and its successor edges to exitMBB. 7318 exitMBB->splice(exitMBB->begin(), BB, 7319 llvm::next(MachineBasicBlock::iterator(MI)), 7320 BB->end()); 7321 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 7322 7323 // Load an immediate to varEnd. 7324 unsigned varEnd = MRI.createVirtualRegister(TRC); 7325 if (isThumb2) { 7326 unsigned VReg1 = varEnd; 7327 if ((LoopSize & 0xFFFF0000) != 0) 7328 VReg1 = MRI.createVirtualRegister(TRC); 7329 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1) 7330 .addImm(LoopSize & 0xFFFF)); 7331 7332 if ((LoopSize & 0xFFFF0000) != 0) 7333 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd) 7334 .addReg(VReg1) 7335 .addImm(LoopSize >> 16)); 7336 } else { 7337 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7338 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7339 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 7340 7341 // MachineConstantPool wants an explicit alignment. 7342 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 7343 if (Align == 0) 7344 Align = getDataLayout()->getTypeAllocSize(C->getType()); 7345 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7346 7347 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp)) 7348 .addReg(varEnd, RegState::Define) 7349 .addConstantPoolIndex(Idx) 7350 .addImm(0)); 7351 } 7352 BB->addSuccessor(loopMBB); 7353 7354 // Generate the loop body: 7355 // varPhi = PHI(varLoop, varEnd) 7356 // srcPhi = PHI(srcLoop, src) 7357 // destPhi = PHI(destLoop, dst) 7358 MachineBasicBlock *entryBB = BB; 7359 BB = loopMBB; 7360 unsigned varLoop = MRI.createVirtualRegister(TRC); 7361 unsigned varPhi = MRI.createVirtualRegister(TRC); 7362 unsigned srcLoop = MRI.createVirtualRegister(TRC); 7363 unsigned srcPhi = MRI.createVirtualRegister(TRC); 7364 unsigned destLoop = MRI.createVirtualRegister(TRC); 7365 unsigned destPhi = MRI.createVirtualRegister(TRC); 7366 7367 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 7368 .addReg(varLoop).addMBB(loopMBB) 7369 .addReg(varEnd).addMBB(entryBB); 7370 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 7371 .addReg(srcLoop).addMBB(loopMBB) 7372 .addReg(src).addMBB(entryBB); 7373 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 7374 .addReg(destLoop).addMBB(loopMBB) 7375 .addReg(dest).addMBB(entryBB); 7376 7377 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7378 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 7379 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC); 7380 if (UnitSize >= 8) { 7381 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch) 7382 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0)); 7383 7384 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop) 7385 .addReg(destPhi).addImm(0).addReg(scratch)); 7386 } else if (isThumb2) { 7387 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch) 7388 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize)); 7389 7390 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop) 7391 .addReg(scratch).addReg(destPhi) 7392 .addImm(UnitSize)); 7393 } else { 7394 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch) 7395 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0) 7396 .addImm(UnitSize)); 7397 7398 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop) 7399 .addReg(scratch).addReg(destPhi) 7400 .addReg(0).addImm(UnitSize)); 7401 } 7402 7403 // Decrement loop variable by UnitSize. 7404 MachineInstrBuilder MIB = BuildMI(BB, dl, 7405 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 7406 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize))); 7407 MIB->getOperand(5).setReg(ARM::CPSR); 7408 MIB->getOperand(5).setIsDef(true); 7409 7410 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7411 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 7412 7413 // loopMBB can loop back to loopMBB or fall through to exitMBB. 7414 BB->addSuccessor(loopMBB); 7415 BB->addSuccessor(exitMBB); 7416 7417 // Add epilogue to handle BytesLeft. 7418 BB = exitMBB; 7419 MachineInstr *StartOfExit = exitMBB->begin(); 7420 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM; 7421 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM; 7422 7423 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7424 // [destOut] = STRB_POST(scratch, destLoop, 1) 7425 unsigned srcIn = srcLoop; 7426 unsigned destIn = destLoop; 7427 for (unsigned i = 0; i < BytesLeft; i++) { 7428 unsigned scratch = MRI.createVirtualRegister(TRC); 7429 unsigned srcOut = MRI.createVirtualRegister(TRC); 7430 unsigned destOut = MRI.createVirtualRegister(TRC); 7431 if (isThumb2) { 7432 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, 7433 TII->get(ldrOpc),scratch) 7434 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1)); 7435 7436 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut) 7437 .addReg(scratch).addReg(destIn) 7438 .addImm(1)); 7439 } else { 7440 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, 7441 TII->get(ldrOpc),scratch) 7442 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1)); 7443 7444 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut) 7445 .addReg(scratch).addReg(destIn) 7446 .addReg(0).addImm(1)); 7447 } 7448 srcIn = srcOut; 7449 destIn = destOut; 7450 } 7451 7452 MI->eraseFromParent(); // The instruction is gone now. 7453 return BB; 7454 } 7455 7456 MachineBasicBlock * 7457 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 7458 MachineBasicBlock *BB) const { 7459 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 7460 DebugLoc dl = MI->getDebugLoc(); 7461 bool isThumb2 = Subtarget->isThumb2(); 7462 switch (MI->getOpcode()) { 7463 default: { 7464 MI->dump(); 7465 llvm_unreachable("Unexpected instr type to insert"); 7466 } 7467 // The Thumb2 pre-indexed stores have the same MI operands, they just 7468 // define them differently in the .td files from the isel patterns, so 7469 // they need pseudos. 7470 case ARM::t2STR_preidx: 7471 MI->setDesc(TII->get(ARM::t2STR_PRE)); 7472 return BB; 7473 case ARM::t2STRB_preidx: 7474 MI->setDesc(TII->get(ARM::t2STRB_PRE)); 7475 return BB; 7476 case ARM::t2STRH_preidx: 7477 MI->setDesc(TII->get(ARM::t2STRH_PRE)); 7478 return BB; 7479 7480 case ARM::STRi_preidx: 7481 case ARM::STRBi_preidx: { 7482 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ? 7483 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM; 7484 // Decode the offset. 7485 unsigned Offset = MI->getOperand(4).getImm(); 7486 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 7487 Offset = ARM_AM::getAM2Offset(Offset); 7488 if (isSub) 7489 Offset = -Offset; 7490 7491 MachineMemOperand *MMO = *MI->memoperands_begin(); 7492 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 7493 .addOperand(MI->getOperand(0)) // Rn_wb 7494 .addOperand(MI->getOperand(1)) // Rt 7495 .addOperand(MI->getOperand(2)) // Rn 7496 .addImm(Offset) // offset (skip GPR==zero_reg) 7497 .addOperand(MI->getOperand(5)) // pred 7498 .addOperand(MI->getOperand(6)) 7499 .addMemOperand(MMO); 7500 MI->eraseFromParent(); 7501 return BB; 7502 } 7503 case ARM::STRr_preidx: 7504 case ARM::STRBr_preidx: 7505 case ARM::STRH_preidx: { 7506 unsigned NewOpc; 7507 switch (MI->getOpcode()) { 7508 default: llvm_unreachable("unexpected opcode!"); 7509 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 7510 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 7511 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 7512 } 7513 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 7514 for (unsigned i = 0; i < MI->getNumOperands(); ++i) 7515 MIB.addOperand(MI->getOperand(i)); 7516 MI->eraseFromParent(); 7517 return BB; 7518 } 7519 case ARM::ATOMIC_LOAD_ADD_I8: 7520 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 7521 case ARM::ATOMIC_LOAD_ADD_I16: 7522 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 7523 case ARM::ATOMIC_LOAD_ADD_I32: 7524 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 7525 7526 case ARM::ATOMIC_LOAD_AND_I8: 7527 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 7528 case ARM::ATOMIC_LOAD_AND_I16: 7529 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 7530 case ARM::ATOMIC_LOAD_AND_I32: 7531 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 7532 7533 case ARM::ATOMIC_LOAD_OR_I8: 7534 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 7535 case ARM::ATOMIC_LOAD_OR_I16: 7536 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 7537 case ARM::ATOMIC_LOAD_OR_I32: 7538 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 7539 7540 case ARM::ATOMIC_LOAD_XOR_I8: 7541 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 7542 case ARM::ATOMIC_LOAD_XOR_I16: 7543 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 7544 case ARM::ATOMIC_LOAD_XOR_I32: 7545 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 7546 7547 case ARM::ATOMIC_LOAD_NAND_I8: 7548 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 7549 case ARM::ATOMIC_LOAD_NAND_I16: 7550 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 7551 case ARM::ATOMIC_LOAD_NAND_I32: 7552 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 7553 7554 case ARM::ATOMIC_LOAD_SUB_I8: 7555 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 7556 case ARM::ATOMIC_LOAD_SUB_I16: 7557 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 7558 case ARM::ATOMIC_LOAD_SUB_I32: 7559 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 7560 7561 case ARM::ATOMIC_LOAD_MIN_I8: 7562 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT); 7563 case ARM::ATOMIC_LOAD_MIN_I16: 7564 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT); 7565 case ARM::ATOMIC_LOAD_MIN_I32: 7566 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT); 7567 7568 case ARM::ATOMIC_LOAD_MAX_I8: 7569 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT); 7570 case ARM::ATOMIC_LOAD_MAX_I16: 7571 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT); 7572 case ARM::ATOMIC_LOAD_MAX_I32: 7573 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT); 7574 7575 case ARM::ATOMIC_LOAD_UMIN_I8: 7576 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO); 7577 case ARM::ATOMIC_LOAD_UMIN_I16: 7578 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO); 7579 case ARM::ATOMIC_LOAD_UMIN_I32: 7580 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO); 7581 7582 case ARM::ATOMIC_LOAD_UMAX_I8: 7583 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI); 7584 case ARM::ATOMIC_LOAD_UMAX_I16: 7585 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI); 7586 case ARM::ATOMIC_LOAD_UMAX_I32: 7587 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI); 7588 7589 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0); 7590 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0); 7591 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0); 7592 7593 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1); 7594 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2); 7595 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4); 7596 7597 7598 case ARM::ATOMADD6432: 7599 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr, 7600 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr, 7601 /*NeedsCarry*/ true); 7602 case ARM::ATOMSUB6432: 7603 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 7604 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 7605 /*NeedsCarry*/ true); 7606 case ARM::ATOMOR6432: 7607 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr, 7608 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 7609 case ARM::ATOMXOR6432: 7610 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr, 7611 isThumb2 ? ARM::t2EORrr : ARM::EORrr); 7612 case ARM::ATOMAND6432: 7613 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr, 7614 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 7615 case ARM::ATOMSWAP6432: 7616 return EmitAtomicBinary64(MI, BB, 0, 0, false); 7617 case ARM::ATOMCMPXCHG6432: 7618 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 7619 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 7620 /*NeedsCarry*/ false, /*IsCmpxchg*/true); 7621 case ARM::ATOMMIN6432: 7622 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 7623 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 7624 /*NeedsCarry*/ true, /*IsCmpxchg*/false, 7625 /*IsMinMax*/ true, ARMCC::LT); 7626 case ARM::ATOMMAX6432: 7627 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 7628 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 7629 /*NeedsCarry*/ true, /*IsCmpxchg*/false, 7630 /*IsMinMax*/ true, ARMCC::GE); 7631 case ARM::ATOMUMIN6432: 7632 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 7633 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 7634 /*NeedsCarry*/ true, /*IsCmpxchg*/false, 7635 /*IsMinMax*/ true, ARMCC::LO); 7636 case ARM::ATOMUMAX6432: 7637 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 7638 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 7639 /*NeedsCarry*/ true, /*IsCmpxchg*/false, 7640 /*IsMinMax*/ true, ARMCC::HS); 7641 7642 case ARM::tMOVCCr_pseudo: { 7643 // To "insert" a SELECT_CC instruction, we actually have to insert the 7644 // diamond control-flow pattern. The incoming instruction knows the 7645 // destination vreg to set, the condition code register to branch on, the 7646 // true/false values to select between, and a branch opcode to use. 7647 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7648 MachineFunction::iterator It = BB; 7649 ++It; 7650 7651 // thisMBB: 7652 // ... 7653 // TrueVal = ... 7654 // cmpTY ccX, r1, r2 7655 // bCC copy1MBB 7656 // fallthrough --> copy0MBB 7657 MachineBasicBlock *thisMBB = BB; 7658 MachineFunction *F = BB->getParent(); 7659 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 7660 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 7661 F->insert(It, copy0MBB); 7662 F->insert(It, sinkMBB); 7663 7664 // Transfer the remainder of BB and its successor edges to sinkMBB. 7665 sinkMBB->splice(sinkMBB->begin(), BB, 7666 llvm::next(MachineBasicBlock::iterator(MI)), 7667 BB->end()); 7668 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 7669 7670 BB->addSuccessor(copy0MBB); 7671 BB->addSuccessor(sinkMBB); 7672 7673 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB) 7674 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); 7675 7676 // copy0MBB: 7677 // %FalseValue = ... 7678 // # fallthrough to sinkMBB 7679 BB = copy0MBB; 7680 7681 // Update machine-CFG edges 7682 BB->addSuccessor(sinkMBB); 7683 7684 // sinkMBB: 7685 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 7686 // ... 7687 BB = sinkMBB; 7688 BuildMI(*BB, BB->begin(), dl, 7689 TII->get(ARM::PHI), MI->getOperand(0).getReg()) 7690 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 7691 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 7692 7693 MI->eraseFromParent(); // The pseudo instruction is gone now. 7694 return BB; 7695 } 7696 7697 case ARM::BCCi64: 7698 case ARM::BCCZi64: { 7699 // If there is an unconditional branch to the other successor, remove it. 7700 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end()); 7701 7702 // Compare both parts that make up the double comparison separately for 7703 // equality. 7704 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64; 7705 7706 unsigned LHS1 = MI->getOperand(1).getReg(); 7707 unsigned LHS2 = MI->getOperand(2).getReg(); 7708 if (RHSisZero) { 7709 AddDefaultPred(BuildMI(BB, dl, 7710 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7711 .addReg(LHS1).addImm(0)); 7712 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7713 .addReg(LHS2).addImm(0) 7714 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 7715 } else { 7716 unsigned RHS1 = MI->getOperand(3).getReg(); 7717 unsigned RHS2 = MI->getOperand(4).getReg(); 7718 AddDefaultPred(BuildMI(BB, dl, 7719 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 7720 .addReg(LHS1).addReg(RHS1)); 7721 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 7722 .addReg(LHS2).addReg(RHS2) 7723 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 7724 } 7725 7726 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB(); 7727 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 7728 if (MI->getOperand(0).getImm() == ARMCC::NE) 7729 std::swap(destMBB, exitMBB); 7730 7731 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7732 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 7733 if (isThumb2) 7734 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB)); 7735 else 7736 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 7737 7738 MI->eraseFromParent(); // The pseudo instruction is gone now. 7739 return BB; 7740 } 7741 7742 case ARM::Int_eh_sjlj_setjmp: 7743 case ARM::Int_eh_sjlj_setjmp_nofp: 7744 case ARM::tInt_eh_sjlj_setjmp: 7745 case ARM::t2Int_eh_sjlj_setjmp: 7746 case ARM::t2Int_eh_sjlj_setjmp_nofp: 7747 EmitSjLjDispatchBlock(MI, BB); 7748 return BB; 7749 7750 case ARM::ABS: 7751 case ARM::t2ABS: { 7752 // To insert an ABS instruction, we have to insert the 7753 // diamond control-flow pattern. The incoming instruction knows the 7754 // source vreg to test against 0, the destination vreg to set, 7755 // the condition code register to branch on, the 7756 // true/false values to select between, and a branch opcode to use. 7757 // It transforms 7758 // V1 = ABS V0 7759 // into 7760 // V2 = MOVS V0 7761 // BCC (branch to SinkBB if V0 >= 0) 7762 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 7763 // SinkBB: V1 = PHI(V2, V3) 7764 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7765 MachineFunction::iterator BBI = BB; 7766 ++BBI; 7767 MachineFunction *Fn = BB->getParent(); 7768 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 7769 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 7770 Fn->insert(BBI, RSBBB); 7771 Fn->insert(BBI, SinkBB); 7772 7773 unsigned int ABSSrcReg = MI->getOperand(1).getReg(); 7774 unsigned int ABSDstReg = MI->getOperand(0).getReg(); 7775 bool isThumb2 = Subtarget->isThumb2(); 7776 MachineRegisterInfo &MRI = Fn->getRegInfo(); 7777 // In Thumb mode S must not be specified if source register is the SP or 7778 // PC and if destination register is the SP, so restrict register class 7779 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ? 7780 (const TargetRegisterClass*)&ARM::rGPRRegClass : 7781 (const TargetRegisterClass*)&ARM::GPRRegClass); 7782 7783 // Transfer the remainder of BB and its successor edges to sinkMBB. 7784 SinkBB->splice(SinkBB->begin(), BB, 7785 llvm::next(MachineBasicBlock::iterator(MI)), 7786 BB->end()); 7787 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 7788 7789 BB->addSuccessor(RSBBB); 7790 BB->addSuccessor(SinkBB); 7791 7792 // fall through to SinkMBB 7793 RSBBB->addSuccessor(SinkBB); 7794 7795 // insert a cmp at the end of BB 7796 AddDefaultPred(BuildMI(BB, dl, 7797 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7798 .addReg(ABSSrcReg).addImm(0)); 7799 7800 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 7801 BuildMI(BB, dl, 7802 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 7803 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 7804 7805 // insert rsbri in RSBBB 7806 // Note: BCC and rsbri will be converted into predicated rsbmi 7807 // by if-conversion pass 7808 BuildMI(*RSBBB, RSBBB->begin(), dl, 7809 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 7810 .addReg(ABSSrcReg, RegState::Kill) 7811 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 7812 7813 // insert PHI in SinkBB, 7814 // reuse ABSDstReg to not change uses of ABS instruction 7815 BuildMI(*SinkBB, SinkBB->begin(), dl, 7816 TII->get(ARM::PHI), ABSDstReg) 7817 .addReg(NewRsbDstReg).addMBB(RSBBB) 7818 .addReg(ABSSrcReg).addMBB(BB); 7819 7820 // remove ABS instruction 7821 MI->eraseFromParent(); 7822 7823 // return last added BB 7824 return SinkBB; 7825 } 7826 case ARM::COPY_STRUCT_BYVAL_I32: 7827 ++NumLoopByVals; 7828 return EmitStructByval(MI, BB); 7829 } 7830 } 7831 7832 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 7833 SDNode *Node) const { 7834 if (!MI->hasPostISelHook()) { 7835 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) && 7836 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'"); 7837 return; 7838 } 7839 7840 const MCInstrDesc *MCID = &MI->getDesc(); 7841 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 7842 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 7843 // operand is still set to noreg. If needed, set the optional operand's 7844 // register to CPSR, and remove the redundant implicit def. 7845 // 7846 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>). 7847 7848 // Rename pseudo opcodes. 7849 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode()); 7850 if (NewOpc) { 7851 const ARMBaseInstrInfo *TII = 7852 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo()); 7853 MCID = &TII->get(NewOpc); 7854 7855 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 && 7856 "converted opcode should be the same except for cc_out"); 7857 7858 MI->setDesc(*MCID); 7859 7860 // Add the optional cc_out operand 7861 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 7862 } 7863 unsigned ccOutIdx = MCID->getNumOperands() - 1; 7864 7865 // Any ARM instruction that sets the 's' bit should specify an optional 7866 // "cc_out" operand in the last operand position. 7867 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 7868 assert(!NewOpc && "Optional cc_out operand required"); 7869 return; 7870 } 7871 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 7872 // since we already have an optional CPSR def. 7873 bool definesCPSR = false; 7874 bool deadCPSR = false; 7875 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands(); 7876 i != e; ++i) { 7877 const MachineOperand &MO = MI->getOperand(i); 7878 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 7879 definesCPSR = true; 7880 if (MO.isDead()) 7881 deadCPSR = true; 7882 MI->RemoveOperand(i); 7883 break; 7884 } 7885 } 7886 if (!definesCPSR) { 7887 assert(!NewOpc && "Optional cc_out operand required"); 7888 return; 7889 } 7890 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 7891 if (deadCPSR) { 7892 assert(!MI->getOperand(ccOutIdx).getReg() && 7893 "expect uninitialized optional cc_out operand"); 7894 return; 7895 } 7896 7897 // If this instruction was defined with an optional CPSR def and its dag node 7898 // had a live implicit CPSR def, then activate the optional CPSR def. 7899 MachineOperand &MO = MI->getOperand(ccOutIdx); 7900 MO.setReg(ARM::CPSR); 7901 MO.setIsDef(true); 7902 } 7903 7904 //===----------------------------------------------------------------------===// 7905 // ARM Optimization Hooks 7906 //===----------------------------------------------------------------------===// 7907 7908 // Helper function that checks if N is a null or all ones constant. 7909 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 7910 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N); 7911 if (!C) 7912 return false; 7913 return AllOnes ? C->isAllOnesValue() : C->isNullValue(); 7914 } 7915 7916 // Return true if N is conditionally 0 or all ones. 7917 // Detects these expressions where cc is an i1 value: 7918 // 7919 // (select cc 0, y) [AllOnes=0] 7920 // (select cc y, 0) [AllOnes=0] 7921 // (zext cc) [AllOnes=0] 7922 // (sext cc) [AllOnes=0/1] 7923 // (select cc -1, y) [AllOnes=1] 7924 // (select cc y, -1) [AllOnes=1] 7925 // 7926 // Invert is set when N is the null/all ones constant when CC is false. 7927 // OtherOp is set to the alternative value of N. 7928 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 7929 SDValue &CC, bool &Invert, 7930 SDValue &OtherOp, 7931 SelectionDAG &DAG) { 7932 switch (N->getOpcode()) { 7933 default: return false; 7934 case ISD::SELECT: { 7935 CC = N->getOperand(0); 7936 SDValue N1 = N->getOperand(1); 7937 SDValue N2 = N->getOperand(2); 7938 if (isZeroOrAllOnes(N1, AllOnes)) { 7939 Invert = false; 7940 OtherOp = N2; 7941 return true; 7942 } 7943 if (isZeroOrAllOnes(N2, AllOnes)) { 7944 Invert = true; 7945 OtherOp = N1; 7946 return true; 7947 } 7948 return false; 7949 } 7950 case ISD::ZERO_EXTEND: 7951 // (zext cc) can never be the all ones value. 7952 if (AllOnes) 7953 return false; 7954 // Fall through. 7955 case ISD::SIGN_EXTEND: { 7956 EVT VT = N->getValueType(0); 7957 CC = N->getOperand(0); 7958 if (CC.getValueType() != MVT::i1) 7959 return false; 7960 Invert = !AllOnes; 7961 if (AllOnes) 7962 // When looking for an AllOnes constant, N is an sext, and the 'other' 7963 // value is 0. 7964 OtherOp = DAG.getConstant(0, VT); 7965 else if (N->getOpcode() == ISD::ZERO_EXTEND) 7966 // When looking for a 0 constant, N can be zext or sext. 7967 OtherOp = DAG.getConstant(1, VT); 7968 else 7969 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT); 7970 return true; 7971 } 7972 } 7973 } 7974 7975 // Combine a constant select operand into its use: 7976 // 7977 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 7978 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 7979 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 7980 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 7981 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 7982 // 7983 // The transform is rejected if the select doesn't have a constant operand that 7984 // is null, or all ones when AllOnes is set. 7985 // 7986 // Also recognize sext/zext from i1: 7987 // 7988 // (add (zext cc), x) -> (select cc (add x, 1), x) 7989 // (add (sext cc), x) -> (select cc (add x, -1), x) 7990 // 7991 // These transformations eventually create predicated instructions. 7992 // 7993 // @param N The node to transform. 7994 // @param Slct The N operand that is a select. 7995 // @param OtherOp The other N operand (x above). 7996 // @param DCI Context. 7997 // @param AllOnes Require the select constant to be all ones instead of null. 7998 // @returns The new node, or SDValue() on failure. 7999 static 8000 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 8001 TargetLowering::DAGCombinerInfo &DCI, 8002 bool AllOnes = false) { 8003 SelectionDAG &DAG = DCI.DAG; 8004 EVT VT = N->getValueType(0); 8005 SDValue NonConstantVal; 8006 SDValue CCOp; 8007 bool SwapSelectOps; 8008 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 8009 NonConstantVal, DAG)) 8010 return SDValue(); 8011 8012 // Slct is now know to be the desired identity constant when CC is true. 8013 SDValue TrueVal = OtherOp; 8014 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 8015 OtherOp, NonConstantVal); 8016 // Unless SwapSelectOps says CC should be false. 8017 if (SwapSelectOps) 8018 std::swap(TrueVal, FalseVal); 8019 8020 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 8021 CCOp, TrueVal, FalseVal); 8022 } 8023 8024 // Attempt combineSelectAndUse on each operand of a commutative operator N. 8025 static 8026 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 8027 TargetLowering::DAGCombinerInfo &DCI) { 8028 SDValue N0 = N->getOperand(0); 8029 SDValue N1 = N->getOperand(1); 8030 if (N0.getNode()->hasOneUse()) { 8031 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes); 8032 if (Result.getNode()) 8033 return Result; 8034 } 8035 if (N1.getNode()->hasOneUse()) { 8036 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes); 8037 if (Result.getNode()) 8038 return Result; 8039 } 8040 return SDValue(); 8041 } 8042 8043 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction 8044 // (only after legalization). 8045 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1, 8046 TargetLowering::DAGCombinerInfo &DCI, 8047 const ARMSubtarget *Subtarget) { 8048 8049 // Only perform optimization if after legalize, and if NEON is available. We 8050 // also expected both operands to be BUILD_VECTORs. 8051 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 8052 || N0.getOpcode() != ISD::BUILD_VECTOR 8053 || N1.getOpcode() != ISD::BUILD_VECTOR) 8054 return SDValue(); 8055 8056 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 8057 EVT VT = N->getValueType(0); 8058 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 8059 return SDValue(); 8060 8061 // Check that the vector operands are of the right form. 8062 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 8063 // operands, where N is the size of the formed vector. 8064 // Each EXTRACT_VECTOR should have the same input vector and odd or even 8065 // index such that we have a pair wise add pattern. 8066 8067 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 8068 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8069 return SDValue(); 8070 SDValue Vec = N0->getOperand(0)->getOperand(0); 8071 SDNode *V = Vec.getNode(); 8072 unsigned nextIndex = 0; 8073 8074 // For each operands to the ADD which are BUILD_VECTORs, 8075 // check to see if each of their operands are an EXTRACT_VECTOR with 8076 // the same vector and appropriate index. 8077 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 8078 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 8079 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 8080 8081 SDValue ExtVec0 = N0->getOperand(i); 8082 SDValue ExtVec1 = N1->getOperand(i); 8083 8084 // First operand is the vector, verify its the same. 8085 if (V != ExtVec0->getOperand(0).getNode() || 8086 V != ExtVec1->getOperand(0).getNode()) 8087 return SDValue(); 8088 8089 // Second is the constant, verify its correct. 8090 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 8091 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 8092 8093 // For the constant, we want to see all the even or all the odd. 8094 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 8095 || C1->getZExtValue() != nextIndex+1) 8096 return SDValue(); 8097 8098 // Increment index. 8099 nextIndex+=2; 8100 } else 8101 return SDValue(); 8102 } 8103 8104 // Create VPADDL node. 8105 SelectionDAG &DAG = DCI.DAG; 8106 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8107 8108 // Build operand list. 8109 SmallVector<SDValue, 8> Ops; 8110 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, 8111 TLI.getPointerTy())); 8112 8113 // Input is the vector. 8114 Ops.push_back(Vec); 8115 8116 // Get widened type and narrowed type. 8117 MVT widenType; 8118 unsigned numElem = VT.getVectorNumElements(); 8119 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) { 8120 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 8121 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 8122 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 8123 default: 8124 llvm_unreachable("Invalid vector element type for padd optimization."); 8125 } 8126 8127 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), 8128 widenType, &Ops[0], Ops.size()); 8129 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, tmp); 8130 } 8131 8132 static SDValue findMUL_LOHI(SDValue V) { 8133 if (V->getOpcode() == ISD::UMUL_LOHI || 8134 V->getOpcode() == ISD::SMUL_LOHI) 8135 return V; 8136 return SDValue(); 8137 } 8138 8139 static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode, 8140 TargetLowering::DAGCombinerInfo &DCI, 8141 const ARMSubtarget *Subtarget) { 8142 8143 if (Subtarget->isThumb1Only()) return SDValue(); 8144 8145 // Only perform the checks after legalize when the pattern is available. 8146 if (DCI.isBeforeLegalize()) return SDValue(); 8147 8148 // Look for multiply add opportunities. 8149 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 8150 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 8151 // a glue link from the first add to the second add. 8152 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 8153 // a S/UMLAL instruction. 8154 // loAdd UMUL_LOHI 8155 // \ / :lo \ :hi 8156 // \ / \ [no multiline comment] 8157 // ADDC | hiAdd 8158 // \ :glue / / 8159 // \ / / 8160 // ADDE 8161 // 8162 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC"); 8163 SDValue AddcOp0 = AddcNode->getOperand(0); 8164 SDValue AddcOp1 = AddcNode->getOperand(1); 8165 8166 // Check if the two operands are from the same mul_lohi node. 8167 if (AddcOp0.getNode() == AddcOp1.getNode()) 8168 return SDValue(); 8169 8170 assert(AddcNode->getNumValues() == 2 && 8171 AddcNode->getValueType(0) == MVT::i32 && 8172 "Expect ADDC with two result values. First: i32"); 8173 8174 // Check that we have a glued ADDC node. 8175 if (AddcNode->getValueType(1) != MVT::Glue) 8176 return SDValue(); 8177 8178 // Check that the ADDC adds the low result of the S/UMUL_LOHI. 8179 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI && 8180 AddcOp0->getOpcode() != ISD::SMUL_LOHI && 8181 AddcOp1->getOpcode() != ISD::UMUL_LOHI && 8182 AddcOp1->getOpcode() != ISD::SMUL_LOHI) 8183 return SDValue(); 8184 8185 // Look for the glued ADDE. 8186 SDNode* AddeNode = AddcNode->getGluedUser(); 8187 if (AddeNode == NULL) 8188 return SDValue(); 8189 8190 // Make sure it is really an ADDE. 8191 if (AddeNode->getOpcode() != ISD::ADDE) 8192 return SDValue(); 8193 8194 assert(AddeNode->getNumOperands() == 3 && 8195 AddeNode->getOperand(2).getValueType() == MVT::Glue && 8196 "ADDE node has the wrong inputs"); 8197 8198 // Check for the triangle shape. 8199 SDValue AddeOp0 = AddeNode->getOperand(0); 8200 SDValue AddeOp1 = AddeNode->getOperand(1); 8201 8202 // Make sure that the ADDE operands are not coming from the same node. 8203 if (AddeOp0.getNode() == AddeOp1.getNode()) 8204 return SDValue(); 8205 8206 // Find the MUL_LOHI node walking up ADDE's operands. 8207 bool IsLeftOperandMUL = false; 8208 SDValue MULOp = findMUL_LOHI(AddeOp0); 8209 if (MULOp == SDValue()) 8210 MULOp = findMUL_LOHI(AddeOp1); 8211 else 8212 IsLeftOperandMUL = true; 8213 if (MULOp == SDValue()) 8214 return SDValue(); 8215 8216 // Figure out the right opcode. 8217 unsigned Opc = MULOp->getOpcode(); 8218 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 8219 8220 // Figure out the high and low input values to the MLAL node. 8221 SDValue* HiMul = &MULOp; 8222 SDValue* HiAdd = NULL; 8223 SDValue* LoMul = NULL; 8224 SDValue* LowAdd = NULL; 8225 8226 if (IsLeftOperandMUL) 8227 HiAdd = &AddeOp1; 8228 else 8229 HiAdd = &AddeOp0; 8230 8231 8232 if (AddcOp0->getOpcode() == Opc) { 8233 LoMul = &AddcOp0; 8234 LowAdd = &AddcOp1; 8235 } 8236 if (AddcOp1->getOpcode() == Opc) { 8237 LoMul = &AddcOp1; 8238 LowAdd = &AddcOp0; 8239 } 8240 8241 if (LoMul == NULL) 8242 return SDValue(); 8243 8244 if (LoMul->getNode() != HiMul->getNode()) 8245 return SDValue(); 8246 8247 // Create the merged node. 8248 SelectionDAG &DAG = DCI.DAG; 8249 8250 // Build operand list. 8251 SmallVector<SDValue, 8> Ops; 8252 Ops.push_back(LoMul->getOperand(0)); 8253 Ops.push_back(LoMul->getOperand(1)); 8254 Ops.push_back(*LowAdd); 8255 Ops.push_back(*HiAdd); 8256 8257 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode), 8258 DAG.getVTList(MVT::i32, MVT::i32), 8259 &Ops[0], Ops.size()); 8260 8261 // Replace the ADDs' nodes uses by the MLA node's values. 8262 SDValue HiMLALResult(MLALNode.getNode(), 1); 8263 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 8264 8265 SDValue LoMLALResult(MLALNode.getNode(), 0); 8266 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 8267 8268 // Return original node to notify the driver to stop replacing. 8269 SDValue resNode(AddcNode, 0); 8270 return resNode; 8271 } 8272 8273 /// PerformADDCCombine - Target-specific dag combine transform from 8274 /// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL. 8275 static SDValue PerformADDCCombine(SDNode *N, 8276 TargetLowering::DAGCombinerInfo &DCI, 8277 const ARMSubtarget *Subtarget) { 8278 8279 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 8280 8281 } 8282 8283 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 8284 /// operands N0 and N1. This is a helper for PerformADDCombine that is 8285 /// called with the default operands, and if that fails, with commuted 8286 /// operands. 8287 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 8288 TargetLowering::DAGCombinerInfo &DCI, 8289 const ARMSubtarget *Subtarget){ 8290 8291 // Attempt to create vpaddl for this add. 8292 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget); 8293 if (Result.getNode()) 8294 return Result; 8295 8296 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 8297 if (N0.getNode()->hasOneUse()) { 8298 SDValue Result = combineSelectAndUse(N, N0, N1, DCI); 8299 if (Result.getNode()) return Result; 8300 } 8301 return SDValue(); 8302 } 8303 8304 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 8305 /// 8306 static SDValue PerformADDCombine(SDNode *N, 8307 TargetLowering::DAGCombinerInfo &DCI, 8308 const ARMSubtarget *Subtarget) { 8309 SDValue N0 = N->getOperand(0); 8310 SDValue N1 = N->getOperand(1); 8311 8312 // First try with the default operand order. 8313 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget); 8314 if (Result.getNode()) 8315 return Result; 8316 8317 // If that didn't work, try again with the operands commuted. 8318 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 8319 } 8320 8321 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 8322 /// 8323 static SDValue PerformSUBCombine(SDNode *N, 8324 TargetLowering::DAGCombinerInfo &DCI) { 8325 SDValue N0 = N->getOperand(0); 8326 SDValue N1 = N->getOperand(1); 8327 8328 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 8329 if (N1.getNode()->hasOneUse()) { 8330 SDValue Result = combineSelectAndUse(N, N1, N0, DCI); 8331 if (Result.getNode()) return Result; 8332 } 8333 8334 return SDValue(); 8335 } 8336 8337 /// PerformVMULCombine 8338 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 8339 /// special multiplier accumulator forwarding. 8340 /// vmul d3, d0, d2 8341 /// vmla d3, d1, d2 8342 /// is faster than 8343 /// vadd d3, d0, d1 8344 /// vmul d3, d3, d2 8345 static SDValue PerformVMULCombine(SDNode *N, 8346 TargetLowering::DAGCombinerInfo &DCI, 8347 const ARMSubtarget *Subtarget) { 8348 if (!Subtarget->hasVMLxForwarding()) 8349 return SDValue(); 8350 8351 SelectionDAG &DAG = DCI.DAG; 8352 SDValue N0 = N->getOperand(0); 8353 SDValue N1 = N->getOperand(1); 8354 unsigned Opcode = N0.getOpcode(); 8355 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8356 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 8357 Opcode = N1.getOpcode(); 8358 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8359 Opcode != ISD::FADD && Opcode != ISD::FSUB) 8360 return SDValue(); 8361 std::swap(N0, N1); 8362 } 8363 8364 EVT VT = N->getValueType(0); 8365 SDLoc DL(N); 8366 SDValue N00 = N0->getOperand(0); 8367 SDValue N01 = N0->getOperand(1); 8368 return DAG.getNode(Opcode, DL, VT, 8369 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 8370 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 8371 } 8372 8373 static SDValue PerformMULCombine(SDNode *N, 8374 TargetLowering::DAGCombinerInfo &DCI, 8375 const ARMSubtarget *Subtarget) { 8376 SelectionDAG &DAG = DCI.DAG; 8377 8378 if (Subtarget->isThumb1Only()) 8379 return SDValue(); 8380 8381 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 8382 return SDValue(); 8383 8384 EVT VT = N->getValueType(0); 8385 if (VT.is64BitVector() || VT.is128BitVector()) 8386 return PerformVMULCombine(N, DCI, Subtarget); 8387 if (VT != MVT::i32) 8388 return SDValue(); 8389 8390 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8391 if (!C) 8392 return SDValue(); 8393 8394 int64_t MulAmt = C->getSExtValue(); 8395 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 8396 8397 ShiftAmt = ShiftAmt & (32 - 1); 8398 SDValue V = N->getOperand(0); 8399 SDLoc DL(N); 8400 8401 SDValue Res; 8402 MulAmt >>= ShiftAmt; 8403 8404 if (MulAmt >= 0) { 8405 if (isPowerOf2_32(MulAmt - 1)) { 8406 // (mul x, 2^N + 1) => (add (shl x, N), x) 8407 Res = DAG.getNode(ISD::ADD, DL, VT, 8408 V, 8409 DAG.getNode(ISD::SHL, DL, VT, 8410 V, 8411 DAG.getConstant(Log2_32(MulAmt - 1), 8412 MVT::i32))); 8413 } else if (isPowerOf2_32(MulAmt + 1)) { 8414 // (mul x, 2^N - 1) => (sub (shl x, N), x) 8415 Res = DAG.getNode(ISD::SUB, DL, VT, 8416 DAG.getNode(ISD::SHL, DL, VT, 8417 V, 8418 DAG.getConstant(Log2_32(MulAmt + 1), 8419 MVT::i32)), 8420 V); 8421 } else 8422 return SDValue(); 8423 } else { 8424 uint64_t MulAmtAbs = -MulAmt; 8425 if (isPowerOf2_32(MulAmtAbs + 1)) { 8426 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 8427 Res = DAG.getNode(ISD::SUB, DL, VT, 8428 V, 8429 DAG.getNode(ISD::SHL, DL, VT, 8430 V, 8431 DAG.getConstant(Log2_32(MulAmtAbs + 1), 8432 MVT::i32))); 8433 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 8434 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 8435 Res = DAG.getNode(ISD::ADD, DL, VT, 8436 V, 8437 DAG.getNode(ISD::SHL, DL, VT, 8438 V, 8439 DAG.getConstant(Log2_32(MulAmtAbs-1), 8440 MVT::i32))); 8441 Res = DAG.getNode(ISD::SUB, DL, VT, 8442 DAG.getConstant(0, MVT::i32),Res); 8443 8444 } else 8445 return SDValue(); 8446 } 8447 8448 if (ShiftAmt != 0) 8449 Res = DAG.getNode(ISD::SHL, DL, VT, 8450 Res, DAG.getConstant(ShiftAmt, MVT::i32)); 8451 8452 // Do not add new nodes to DAG combiner worklist. 8453 DCI.CombineTo(N, Res, false); 8454 return SDValue(); 8455 } 8456 8457 static SDValue PerformANDCombine(SDNode *N, 8458 TargetLowering::DAGCombinerInfo &DCI, 8459 const ARMSubtarget *Subtarget) { 8460 8461 // Attempt to use immediate-form VBIC 8462 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8463 SDLoc dl(N); 8464 EVT VT = N->getValueType(0); 8465 SelectionDAG &DAG = DCI.DAG; 8466 8467 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8468 return SDValue(); 8469 8470 APInt SplatBits, SplatUndef; 8471 unsigned SplatBitSize; 8472 bool HasAnyUndefs; 8473 if (BVN && 8474 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8475 if (SplatBitSize <= 64) { 8476 EVT VbicVT; 8477 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 8478 SplatUndef.getZExtValue(), SplatBitSize, 8479 DAG, VbicVT, VT.is128BitVector(), 8480 OtherModImm); 8481 if (Val.getNode()) { 8482 SDValue Input = 8483 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 8484 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 8485 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 8486 } 8487 } 8488 } 8489 8490 if (!Subtarget->isThumb1Only()) { 8491 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 8492 SDValue Result = combineSelectAndUseCommutative(N, true, DCI); 8493 if (Result.getNode()) 8494 return Result; 8495 } 8496 8497 return SDValue(); 8498 } 8499 8500 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 8501 static SDValue PerformORCombine(SDNode *N, 8502 TargetLowering::DAGCombinerInfo &DCI, 8503 const ARMSubtarget *Subtarget) { 8504 // Attempt to use immediate-form VORR 8505 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8506 SDLoc dl(N); 8507 EVT VT = N->getValueType(0); 8508 SelectionDAG &DAG = DCI.DAG; 8509 8510 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8511 return SDValue(); 8512 8513 APInt SplatBits, SplatUndef; 8514 unsigned SplatBitSize; 8515 bool HasAnyUndefs; 8516 if (BVN && Subtarget->hasNEON() && 8517 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8518 if (SplatBitSize <= 64) { 8519 EVT VorrVT; 8520 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 8521 SplatUndef.getZExtValue(), SplatBitSize, 8522 DAG, VorrVT, VT.is128BitVector(), 8523 OtherModImm); 8524 if (Val.getNode()) { 8525 SDValue Input = 8526 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 8527 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 8528 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 8529 } 8530 } 8531 } 8532 8533 if (!Subtarget->isThumb1Only()) { 8534 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 8535 SDValue Result = combineSelectAndUseCommutative(N, false, DCI); 8536 if (Result.getNode()) 8537 return Result; 8538 } 8539 8540 // The code below optimizes (or (and X, Y), Z). 8541 // The AND operand needs to have a single user to make these optimizations 8542 // profitable. 8543 SDValue N0 = N->getOperand(0); 8544 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 8545 return SDValue(); 8546 SDValue N1 = N->getOperand(1); 8547 8548 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 8549 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 8550 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 8551 APInt SplatUndef; 8552 unsigned SplatBitSize; 8553 bool HasAnyUndefs; 8554 8555 APInt SplatBits0, SplatBits1; 8556 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 8557 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 8558 // Ensure that the second operand of both ands are constants 8559 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 8560 HasAnyUndefs) && !HasAnyUndefs) { 8561 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 8562 HasAnyUndefs) && !HasAnyUndefs) { 8563 // Ensure that the bit width of the constants are the same and that 8564 // the splat arguments are logical inverses as per the pattern we 8565 // are trying to simplify. 8566 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 8567 SplatBits0 == ~SplatBits1) { 8568 // Canonicalize the vector type to make instruction selection 8569 // simpler. 8570 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 8571 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 8572 N0->getOperand(1), 8573 N0->getOperand(0), 8574 N1->getOperand(0)); 8575 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 8576 } 8577 } 8578 } 8579 } 8580 8581 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 8582 // reasonable. 8583 8584 // BFI is only available on V6T2+ 8585 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 8586 return SDValue(); 8587 8588 SDLoc DL(N); 8589 // 1) or (and A, mask), val => ARMbfi A, val, mask 8590 // iff (val & mask) == val 8591 // 8592 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 8593 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 8594 // && mask == ~mask2 8595 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 8596 // && ~mask == mask2 8597 // (i.e., copy a bitfield value into another bitfield of the same width) 8598 8599 if (VT != MVT::i32) 8600 return SDValue(); 8601 8602 SDValue N00 = N0.getOperand(0); 8603 8604 // The value and the mask need to be constants so we can verify this is 8605 // actually a bitfield set. If the mask is 0xffff, we can do better 8606 // via a movt instruction, so don't use BFI in that case. 8607 SDValue MaskOp = N0.getOperand(1); 8608 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 8609 if (!MaskC) 8610 return SDValue(); 8611 unsigned Mask = MaskC->getZExtValue(); 8612 if (Mask == 0xffff) 8613 return SDValue(); 8614 SDValue Res; 8615 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 8616 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 8617 if (N1C) { 8618 unsigned Val = N1C->getZExtValue(); 8619 if ((Val & ~Mask) != Val) 8620 return SDValue(); 8621 8622 if (ARM::isBitFieldInvertedMask(Mask)) { 8623 Val >>= countTrailingZeros(~Mask); 8624 8625 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 8626 DAG.getConstant(Val, MVT::i32), 8627 DAG.getConstant(Mask, MVT::i32)); 8628 8629 // Do not add new nodes to DAG combiner worklist. 8630 DCI.CombineTo(N, Res, false); 8631 return SDValue(); 8632 } 8633 } else if (N1.getOpcode() == ISD::AND) { 8634 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 8635 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 8636 if (!N11C) 8637 return SDValue(); 8638 unsigned Mask2 = N11C->getZExtValue(); 8639 8640 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 8641 // as is to match. 8642 if (ARM::isBitFieldInvertedMask(Mask) && 8643 (Mask == ~Mask2)) { 8644 // The pack halfword instruction works better for masks that fit it, 8645 // so use that when it's available. 8646 if (Subtarget->hasT2ExtractPack() && 8647 (Mask == 0xffff || Mask == 0xffff0000)) 8648 return SDValue(); 8649 // 2a 8650 unsigned amt = countTrailingZeros(Mask2); 8651 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 8652 DAG.getConstant(amt, MVT::i32)); 8653 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 8654 DAG.getConstant(Mask, MVT::i32)); 8655 // Do not add new nodes to DAG combiner worklist. 8656 DCI.CombineTo(N, Res, false); 8657 return SDValue(); 8658 } else if (ARM::isBitFieldInvertedMask(~Mask) && 8659 (~Mask == Mask2)) { 8660 // The pack halfword instruction works better for masks that fit it, 8661 // so use that when it's available. 8662 if (Subtarget->hasT2ExtractPack() && 8663 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 8664 return SDValue(); 8665 // 2b 8666 unsigned lsb = countTrailingZeros(Mask); 8667 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 8668 DAG.getConstant(lsb, MVT::i32)); 8669 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 8670 DAG.getConstant(Mask2, MVT::i32)); 8671 // Do not add new nodes to DAG combiner worklist. 8672 DCI.CombineTo(N, Res, false); 8673 return SDValue(); 8674 } 8675 } 8676 8677 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 8678 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 8679 ARM::isBitFieldInvertedMask(~Mask)) { 8680 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 8681 // where lsb(mask) == #shamt and masked bits of B are known zero. 8682 SDValue ShAmt = N00.getOperand(1); 8683 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 8684 unsigned LSB = countTrailingZeros(Mask); 8685 if (ShAmtC != LSB) 8686 return SDValue(); 8687 8688 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 8689 DAG.getConstant(~Mask, MVT::i32)); 8690 8691 // Do not add new nodes to DAG combiner worklist. 8692 DCI.CombineTo(N, Res, false); 8693 } 8694 8695 return SDValue(); 8696 } 8697 8698 static SDValue PerformXORCombine(SDNode *N, 8699 TargetLowering::DAGCombinerInfo &DCI, 8700 const ARMSubtarget *Subtarget) { 8701 EVT VT = N->getValueType(0); 8702 SelectionDAG &DAG = DCI.DAG; 8703 8704 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8705 return SDValue(); 8706 8707 if (!Subtarget->isThumb1Only()) { 8708 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 8709 SDValue Result = combineSelectAndUseCommutative(N, false, DCI); 8710 if (Result.getNode()) 8711 return Result; 8712 } 8713 8714 return SDValue(); 8715 } 8716 8717 /// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 8718 /// the bits being cleared by the AND are not demanded by the BFI. 8719 static SDValue PerformBFICombine(SDNode *N, 8720 TargetLowering::DAGCombinerInfo &DCI) { 8721 SDValue N1 = N->getOperand(1); 8722 if (N1.getOpcode() == ISD::AND) { 8723 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 8724 if (!N11C) 8725 return SDValue(); 8726 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 8727 unsigned LSB = countTrailingZeros(~InvMask); 8728 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 8729 unsigned Mask = (1 << Width)-1; 8730 unsigned Mask2 = N11C->getZExtValue(); 8731 if ((Mask & (~Mask2)) == 0) 8732 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 8733 N->getOperand(0), N1.getOperand(0), 8734 N->getOperand(2)); 8735 } 8736 return SDValue(); 8737 } 8738 8739 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 8740 /// ARMISD::VMOVRRD. 8741 static SDValue PerformVMOVRRDCombine(SDNode *N, 8742 TargetLowering::DAGCombinerInfo &DCI) { 8743 // vmovrrd(vmovdrr x, y) -> x,y 8744 SDValue InDouble = N->getOperand(0); 8745 if (InDouble.getOpcode() == ARMISD::VMOVDRR) 8746 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 8747 8748 // vmovrrd(load f64) -> (load i32), (load i32) 8749 SDNode *InNode = InDouble.getNode(); 8750 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 8751 InNode->getValueType(0) == MVT::f64 && 8752 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 8753 !cast<LoadSDNode>(InNode)->isVolatile()) { 8754 // TODO: Should this be done for non-FrameIndex operands? 8755 LoadSDNode *LD = cast<LoadSDNode>(InNode); 8756 8757 SelectionDAG &DAG = DCI.DAG; 8758 SDLoc DL(LD); 8759 SDValue BasePtr = LD->getBasePtr(); 8760 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, 8761 LD->getPointerInfo(), LD->isVolatile(), 8762 LD->isNonTemporal(), LD->isInvariant(), 8763 LD->getAlignment()); 8764 8765 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 8766 DAG.getConstant(4, MVT::i32)); 8767 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, 8768 LD->getPointerInfo(), LD->isVolatile(), 8769 LD->isNonTemporal(), LD->isInvariant(), 8770 std::min(4U, LD->getAlignment() / 2)); 8771 8772 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 8773 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 8774 DCI.RemoveFromWorklist(LD); 8775 DAG.DeleteNode(LD); 8776 return Result; 8777 } 8778 8779 return SDValue(); 8780 } 8781 8782 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 8783 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 8784 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 8785 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 8786 SDValue Op0 = N->getOperand(0); 8787 SDValue Op1 = N->getOperand(1); 8788 if (Op0.getOpcode() == ISD::BITCAST) 8789 Op0 = Op0.getOperand(0); 8790 if (Op1.getOpcode() == ISD::BITCAST) 8791 Op1 = Op1.getOperand(0); 8792 if (Op0.getOpcode() == ARMISD::VMOVRRD && 8793 Op0.getNode() == Op1.getNode() && 8794 Op0.getResNo() == 0 && Op1.getResNo() == 1) 8795 return DAG.getNode(ISD::BITCAST, SDLoc(N), 8796 N->getValueType(0), Op0.getOperand(0)); 8797 return SDValue(); 8798 } 8799 8800 /// PerformSTORECombine - Target-specific dag combine xforms for 8801 /// ISD::STORE. 8802 static SDValue PerformSTORECombine(SDNode *N, 8803 TargetLowering::DAGCombinerInfo &DCI) { 8804 StoreSDNode *St = cast<StoreSDNode>(N); 8805 if (St->isVolatile()) 8806 return SDValue(); 8807 8808 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 8809 // pack all of the elements in one place. Next, store to memory in fewer 8810 // chunks. 8811 SDValue StVal = St->getValue(); 8812 EVT VT = StVal.getValueType(); 8813 if (St->isTruncatingStore() && VT.isVector()) { 8814 SelectionDAG &DAG = DCI.DAG; 8815 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8816 EVT StVT = St->getMemoryVT(); 8817 unsigned NumElems = VT.getVectorNumElements(); 8818 assert(StVT != VT && "Cannot truncate to the same type"); 8819 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits(); 8820 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits(); 8821 8822 // From, To sizes and ElemCount must be pow of two 8823 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 8824 8825 // We are going to use the original vector elt for storing. 8826 // Accumulated smaller vector elements must be a multiple of the store size. 8827 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 8828 8829 unsigned SizeRatio = FromEltSz / ToEltSz; 8830 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 8831 8832 // Create a type on which we perform the shuffle. 8833 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 8834 NumElems*SizeRatio); 8835 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 8836 8837 SDLoc DL(St); 8838 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 8839 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 8840 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio; 8841 8842 // Can't shuffle using an illegal type. 8843 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 8844 8845 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 8846 DAG.getUNDEF(WideVec.getValueType()), 8847 ShuffleVec.data()); 8848 // At this point all of the data is stored at the bottom of the 8849 // register. We now need to save it to mem. 8850 8851 // Find the largest store unit 8852 MVT StoreType = MVT::i8; 8853 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE; 8854 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) { 8855 MVT Tp = (MVT::SimpleValueType)tp; 8856 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 8857 StoreType = Tp; 8858 } 8859 // Didn't find a legal store type. 8860 if (!TLI.isTypeLegal(StoreType)) 8861 return SDValue(); 8862 8863 // Bitcast the original vector into a vector of store-size units 8864 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 8865 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 8866 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 8867 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 8868 SmallVector<SDValue, 8> Chains; 8869 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8, 8870 TLI.getPointerTy()); 8871 SDValue BasePtr = St->getBasePtr(); 8872 8873 // Perform one or more big stores into memory. 8874 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 8875 for (unsigned I = 0; I < E; I++) { 8876 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 8877 StoreType, ShuffWide, 8878 DAG.getIntPtrConstant(I)); 8879 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 8880 St->getPointerInfo(), St->isVolatile(), 8881 St->isNonTemporal(), St->getAlignment()); 8882 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 8883 Increment); 8884 Chains.push_back(Ch); 8885 } 8886 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0], 8887 Chains.size()); 8888 } 8889 8890 if (!ISD::isNormalStore(St)) 8891 return SDValue(); 8892 8893 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 8894 // ARM stores of arguments in the same cache line. 8895 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 8896 StVal.getNode()->hasOneUse()) { 8897 SelectionDAG &DAG = DCI.DAG; 8898 SDLoc DL(St); 8899 SDValue BasePtr = St->getBasePtr(); 8900 SDValue NewST1 = DAG.getStore(St->getChain(), DL, 8901 StVal.getNode()->getOperand(0), BasePtr, 8902 St->getPointerInfo(), St->isVolatile(), 8903 St->isNonTemporal(), St->getAlignment()); 8904 8905 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 8906 DAG.getConstant(4, MVT::i32)); 8907 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1), 8908 OffsetPtr, St->getPointerInfo(), St->isVolatile(), 8909 St->isNonTemporal(), 8910 std::min(4U, St->getAlignment() / 2)); 8911 } 8912 8913 if (StVal.getValueType() != MVT::i64 || 8914 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8915 return SDValue(); 8916 8917 // Bitcast an i64 store extracted from a vector to f64. 8918 // Otherwise, the i64 value will be legalized to a pair of i32 values. 8919 SelectionDAG &DAG = DCI.DAG; 8920 SDLoc dl(StVal); 8921 SDValue IntVec = StVal.getOperand(0); 8922 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 8923 IntVec.getValueType().getVectorNumElements()); 8924 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 8925 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 8926 Vec, StVal.getOperand(1)); 8927 dl = SDLoc(N); 8928 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 8929 // Make the DAGCombiner fold the bitcasts. 8930 DCI.AddToWorklist(Vec.getNode()); 8931 DCI.AddToWorklist(ExtElt.getNode()); 8932 DCI.AddToWorklist(V.getNode()); 8933 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 8934 St->getPointerInfo(), St->isVolatile(), 8935 St->isNonTemporal(), St->getAlignment(), 8936 St->getTBAAInfo()); 8937 } 8938 8939 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 8940 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 8941 /// i64 vector to have f64 elements, since the value can then be loaded 8942 /// directly into a VFP register. 8943 static bool hasNormalLoadOperand(SDNode *N) { 8944 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 8945 for (unsigned i = 0; i < NumElts; ++i) { 8946 SDNode *Elt = N->getOperand(i).getNode(); 8947 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 8948 return true; 8949 } 8950 return false; 8951 } 8952 8953 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 8954 /// ISD::BUILD_VECTOR. 8955 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 8956 TargetLowering::DAGCombinerInfo &DCI){ 8957 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 8958 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 8959 // into a pair of GPRs, which is fine when the value is used as a scalar, 8960 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 8961 SelectionDAG &DAG = DCI.DAG; 8962 if (N->getNumOperands() == 2) { 8963 SDValue RV = PerformVMOVDRRCombine(N, DAG); 8964 if (RV.getNode()) 8965 return RV; 8966 } 8967 8968 // Load i64 elements as f64 values so that type legalization does not split 8969 // them up into i32 values. 8970 EVT VT = N->getValueType(0); 8971 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 8972 return SDValue(); 8973 SDLoc dl(N); 8974 SmallVector<SDValue, 8> Ops; 8975 unsigned NumElts = VT.getVectorNumElements(); 8976 for (unsigned i = 0; i < NumElts; ++i) { 8977 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 8978 Ops.push_back(V); 8979 // Make the DAGCombiner fold the bitcast. 8980 DCI.AddToWorklist(V.getNode()); 8981 } 8982 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 8983 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts); 8984 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 8985 } 8986 8987 /// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 8988 static SDValue 8989 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 8990 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 8991 // At that time, we may have inserted bitcasts from integer to float. 8992 // If these bitcasts have survived DAGCombine, change the lowering of this 8993 // BUILD_VECTOR in something more vector friendly, i.e., that does not 8994 // force to use floating point types. 8995 8996 // Make sure we can change the type of the vector. 8997 // This is possible iff: 8998 // 1. The vector is only used in a bitcast to a integer type. I.e., 8999 // 1.1. Vector is used only once. 9000 // 1.2. Use is a bit convert to an integer type. 9001 // 2. The size of its operands are 32-bits (64-bits are not legal). 9002 EVT VT = N->getValueType(0); 9003 EVT EltVT = VT.getVectorElementType(); 9004 9005 // Check 1.1. and 2. 9006 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 9007 return SDValue(); 9008 9009 // By construction, the input type must be float. 9010 assert(EltVT == MVT::f32 && "Unexpected type!"); 9011 9012 // Check 1.2. 9013 SDNode *Use = *N->use_begin(); 9014 if (Use->getOpcode() != ISD::BITCAST || 9015 Use->getValueType(0).isFloatingPoint()) 9016 return SDValue(); 9017 9018 // Check profitability. 9019 // Model is, if more than half of the relevant operands are bitcast from 9020 // i32, turn the build_vector into a sequence of insert_vector_elt. 9021 // Relevant operands are everything that is not statically 9022 // (i.e., at compile time) bitcasted. 9023 unsigned NumOfBitCastedElts = 0; 9024 unsigned NumElts = VT.getVectorNumElements(); 9025 unsigned NumOfRelevantElts = NumElts; 9026 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 9027 SDValue Elt = N->getOperand(Idx); 9028 if (Elt->getOpcode() == ISD::BITCAST) { 9029 // Assume only bit cast to i32 will go away. 9030 if (Elt->getOperand(0).getValueType() == MVT::i32) 9031 ++NumOfBitCastedElts; 9032 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt)) 9033 // Constants are statically casted, thus do not count them as 9034 // relevant operands. 9035 --NumOfRelevantElts; 9036 } 9037 9038 // Check if more than half of the elements require a non-free bitcast. 9039 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 9040 return SDValue(); 9041 9042 SelectionDAG &DAG = DCI.DAG; 9043 // Create the new vector type. 9044 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 9045 // Check if the type is legal. 9046 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9047 if (!TLI.isTypeLegal(VecVT)) 9048 return SDValue(); 9049 9050 // Combine: 9051 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 9052 // => BITCAST INSERT_VECTOR_ELT 9053 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 9054 // (BITCAST EN), N. 9055 SDValue Vec = DAG.getUNDEF(VecVT); 9056 SDLoc dl(N); 9057 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 9058 SDValue V = N->getOperand(Idx); 9059 if (V.getOpcode() == ISD::UNDEF) 9060 continue; 9061 if (V.getOpcode() == ISD::BITCAST && 9062 V->getOperand(0).getValueType() == MVT::i32) 9063 // Fold obvious case. 9064 V = V.getOperand(0); 9065 else { 9066 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 9067 // Make the DAGCombiner fold the bitcasts. 9068 DCI.AddToWorklist(V.getNode()); 9069 } 9070 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32); 9071 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 9072 } 9073 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 9074 // Make the DAGCombiner fold the bitcasts. 9075 DCI.AddToWorklist(Vec.getNode()); 9076 return Vec; 9077 } 9078 9079 /// PerformInsertEltCombine - Target-specific dag combine xforms for 9080 /// ISD::INSERT_VECTOR_ELT. 9081 static SDValue PerformInsertEltCombine(SDNode *N, 9082 TargetLowering::DAGCombinerInfo &DCI) { 9083 // Bitcast an i64 load inserted into a vector to f64. 9084 // Otherwise, the i64 value will be legalized to a pair of i32 values. 9085 EVT VT = N->getValueType(0); 9086 SDNode *Elt = N->getOperand(1).getNode(); 9087 if (VT.getVectorElementType() != MVT::i64 || 9088 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 9089 return SDValue(); 9090 9091 SelectionDAG &DAG = DCI.DAG; 9092 SDLoc dl(N); 9093 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 9094 VT.getVectorNumElements()); 9095 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 9096 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 9097 // Make the DAGCombiner fold the bitcasts. 9098 DCI.AddToWorklist(Vec.getNode()); 9099 DCI.AddToWorklist(V.getNode()); 9100 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 9101 Vec, V, N->getOperand(2)); 9102 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 9103 } 9104 9105 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 9106 /// ISD::VECTOR_SHUFFLE. 9107 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 9108 // The LLVM shufflevector instruction does not require the shuffle mask 9109 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 9110 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 9111 // operands do not match the mask length, they are extended by concatenating 9112 // them with undef vectors. That is probably the right thing for other 9113 // targets, but for NEON it is better to concatenate two double-register 9114 // size vector operands into a single quad-register size vector. Do that 9115 // transformation here: 9116 // shuffle(concat(v1, undef), concat(v2, undef)) -> 9117 // shuffle(concat(v1, v2), undef) 9118 SDValue Op0 = N->getOperand(0); 9119 SDValue Op1 = N->getOperand(1); 9120 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 9121 Op1.getOpcode() != ISD::CONCAT_VECTORS || 9122 Op0.getNumOperands() != 2 || 9123 Op1.getNumOperands() != 2) 9124 return SDValue(); 9125 SDValue Concat0Op1 = Op0.getOperand(1); 9126 SDValue Concat1Op1 = Op1.getOperand(1); 9127 if (Concat0Op1.getOpcode() != ISD::UNDEF || 9128 Concat1Op1.getOpcode() != ISD::UNDEF) 9129 return SDValue(); 9130 // Skip the transformation if any of the types are illegal. 9131 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9132 EVT VT = N->getValueType(0); 9133 if (!TLI.isTypeLegal(VT) || 9134 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 9135 !TLI.isTypeLegal(Concat1Op1.getValueType())) 9136 return SDValue(); 9137 9138 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 9139 Op0.getOperand(0), Op1.getOperand(0)); 9140 // Translate the shuffle mask. 9141 SmallVector<int, 16> NewMask; 9142 unsigned NumElts = VT.getVectorNumElements(); 9143 unsigned HalfElts = NumElts/2; 9144 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 9145 for (unsigned n = 0; n < NumElts; ++n) { 9146 int MaskElt = SVN->getMaskElt(n); 9147 int NewElt = -1; 9148 if (MaskElt < (int)HalfElts) 9149 NewElt = MaskElt; 9150 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 9151 NewElt = HalfElts + MaskElt - NumElts; 9152 NewMask.push_back(NewElt); 9153 } 9154 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 9155 DAG.getUNDEF(VT), NewMask.data()); 9156 } 9157 9158 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and 9159 /// NEON load/store intrinsics to merge base address updates. 9160 static SDValue CombineBaseUpdate(SDNode *N, 9161 TargetLowering::DAGCombinerInfo &DCI) { 9162 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 9163 return SDValue(); 9164 9165 SelectionDAG &DAG = DCI.DAG; 9166 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 9167 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 9168 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1); 9169 SDValue Addr = N->getOperand(AddrOpIdx); 9170 9171 // Search for a use of the address operand that is an increment. 9172 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 9173 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 9174 SDNode *User = *UI; 9175 if (User->getOpcode() != ISD::ADD || 9176 UI.getUse().getResNo() != Addr.getResNo()) 9177 continue; 9178 9179 // Check that the add is independent of the load/store. Otherwise, folding 9180 // it would create a cycle. 9181 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 9182 continue; 9183 9184 // Find the new opcode for the updating load/store. 9185 bool isLoad = true; 9186 bool isLaneOp = false; 9187 unsigned NewOpc = 0; 9188 unsigned NumVecs = 0; 9189 if (isIntrinsic) { 9190 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 9191 switch (IntNo) { 9192 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 9193 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 9194 NumVecs = 1; break; 9195 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 9196 NumVecs = 2; break; 9197 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 9198 NumVecs = 3; break; 9199 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 9200 NumVecs = 4; break; 9201 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 9202 NumVecs = 2; isLaneOp = true; break; 9203 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 9204 NumVecs = 3; isLaneOp = true; break; 9205 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 9206 NumVecs = 4; isLaneOp = true; break; 9207 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 9208 NumVecs = 1; isLoad = false; break; 9209 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 9210 NumVecs = 2; isLoad = false; break; 9211 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 9212 NumVecs = 3; isLoad = false; break; 9213 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 9214 NumVecs = 4; isLoad = false; break; 9215 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 9216 NumVecs = 2; isLoad = false; isLaneOp = true; break; 9217 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 9218 NumVecs = 3; isLoad = false; isLaneOp = true; break; 9219 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 9220 NumVecs = 4; isLoad = false; isLaneOp = true; break; 9221 } 9222 } else { 9223 isLaneOp = true; 9224 switch (N->getOpcode()) { 9225 default: llvm_unreachable("unexpected opcode for Neon base update"); 9226 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 9227 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 9228 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 9229 } 9230 } 9231 9232 // Find the size of memory referenced by the load/store. 9233 EVT VecTy; 9234 if (isLoad) 9235 VecTy = N->getValueType(0); 9236 else 9237 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 9238 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 9239 if (isLaneOp) 9240 NumBytes /= VecTy.getVectorNumElements(); 9241 9242 // If the increment is a constant, it must match the memory ref size. 9243 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 9244 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 9245 uint64_t IncVal = CInc->getZExtValue(); 9246 if (IncVal != NumBytes) 9247 continue; 9248 } else if (NumBytes >= 3 * 16) { 9249 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 9250 // separate instructions that make it harder to use a non-constant update. 9251 continue; 9252 } 9253 9254 // Create the new updating load/store node. 9255 EVT Tys[6]; 9256 unsigned NumResultVecs = (isLoad ? NumVecs : 0); 9257 unsigned n; 9258 for (n = 0; n < NumResultVecs; ++n) 9259 Tys[n] = VecTy; 9260 Tys[n++] = MVT::i32; 9261 Tys[n] = MVT::Other; 9262 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2); 9263 SmallVector<SDValue, 8> Ops; 9264 Ops.push_back(N->getOperand(0)); // incoming chain 9265 Ops.push_back(N->getOperand(AddrOpIdx)); 9266 Ops.push_back(Inc); 9267 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) { 9268 Ops.push_back(N->getOperand(i)); 9269 } 9270 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N); 9271 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys, 9272 Ops.data(), Ops.size(), 9273 MemInt->getMemoryVT(), 9274 MemInt->getMemOperand()); 9275 9276 // Update the uses. 9277 std::vector<SDValue> NewResults; 9278 for (unsigned i = 0; i < NumResultVecs; ++i) { 9279 NewResults.push_back(SDValue(UpdN.getNode(), i)); 9280 } 9281 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 9282 DCI.CombineTo(N, NewResults); 9283 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 9284 9285 break; 9286 } 9287 return SDValue(); 9288 } 9289 9290 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 9291 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 9292 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 9293 /// return true. 9294 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 9295 SelectionDAG &DAG = DCI.DAG; 9296 EVT VT = N->getValueType(0); 9297 // vldN-dup instructions only support 64-bit vectors for N > 1. 9298 if (!VT.is64BitVector()) 9299 return false; 9300 9301 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 9302 SDNode *VLD = N->getOperand(0).getNode(); 9303 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 9304 return false; 9305 unsigned NumVecs = 0; 9306 unsigned NewOpc = 0; 9307 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 9308 if (IntNo == Intrinsic::arm_neon_vld2lane) { 9309 NumVecs = 2; 9310 NewOpc = ARMISD::VLD2DUP; 9311 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 9312 NumVecs = 3; 9313 NewOpc = ARMISD::VLD3DUP; 9314 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 9315 NumVecs = 4; 9316 NewOpc = ARMISD::VLD4DUP; 9317 } else { 9318 return false; 9319 } 9320 9321 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 9322 // numbers match the load. 9323 unsigned VLDLaneNo = 9324 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 9325 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9326 UI != UE; ++UI) { 9327 // Ignore uses of the chain result. 9328 if (UI.getUse().getResNo() == NumVecs) 9329 continue; 9330 SDNode *User = *UI; 9331 if (User->getOpcode() != ARMISD::VDUPLANE || 9332 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 9333 return false; 9334 } 9335 9336 // Create the vldN-dup node. 9337 EVT Tys[5]; 9338 unsigned n; 9339 for (n = 0; n < NumVecs; ++n) 9340 Tys[n] = VT; 9341 Tys[n] = MVT::Other; 9342 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1); 9343 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 9344 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 9345 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 9346 Ops, 2, VLDMemInt->getMemoryVT(), 9347 VLDMemInt->getMemOperand()); 9348 9349 // Update the uses. 9350 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9351 UI != UE; ++UI) { 9352 unsigned ResNo = UI.getUse().getResNo(); 9353 // Ignore uses of the chain result. 9354 if (ResNo == NumVecs) 9355 continue; 9356 SDNode *User = *UI; 9357 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 9358 } 9359 9360 // Now the vldN-lane intrinsic is dead except for its chain result. 9361 // Update uses of the chain. 9362 std::vector<SDValue> VLDDupResults; 9363 for (unsigned n = 0; n < NumVecs; ++n) 9364 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 9365 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 9366 DCI.CombineTo(VLD, VLDDupResults); 9367 9368 return true; 9369 } 9370 9371 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 9372 /// ARMISD::VDUPLANE. 9373 static SDValue PerformVDUPLANECombine(SDNode *N, 9374 TargetLowering::DAGCombinerInfo &DCI) { 9375 SDValue Op = N->getOperand(0); 9376 9377 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 9378 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 9379 if (CombineVLDDUP(N, DCI)) 9380 return SDValue(N, 0); 9381 9382 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 9383 // redundant. Ignore bit_converts for now; element sizes are checked below. 9384 while (Op.getOpcode() == ISD::BITCAST) 9385 Op = Op.getOperand(0); 9386 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 9387 return SDValue(); 9388 9389 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 9390 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits(); 9391 // The canonical VMOV for a zero vector uses a 32-bit element size. 9392 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 9393 unsigned EltBits; 9394 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 9395 EltSize = 8; 9396 EVT VT = N->getValueType(0); 9397 if (EltSize > VT.getVectorElementType().getSizeInBits()) 9398 return SDValue(); 9399 9400 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 9401 } 9402 9403 // isConstVecPow2 - Return true if each vector element is a power of 2, all 9404 // elements are the same constant, C, and Log2(C) ranges from 1 to 32. 9405 static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C) 9406 { 9407 integerPart cN; 9408 integerPart c0 = 0; 9409 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements(); 9410 I != E; I++) { 9411 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I)); 9412 if (!C) 9413 return false; 9414 9415 bool isExact; 9416 APFloat APF = C->getValueAPF(); 9417 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact) 9418 != APFloat::opOK || !isExact) 9419 return false; 9420 9421 c0 = (I == 0) ? cN : c0; 9422 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32) 9423 return false; 9424 } 9425 C = c0; 9426 return true; 9427 } 9428 9429 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 9430 /// can replace combinations of VMUL and VCVT (floating-point to integer) 9431 /// when the VMUL has a constant operand that is a power of 2. 9432 /// 9433 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 9434 /// vmul.f32 d16, d17, d16 9435 /// vcvt.s32.f32 d16, d16 9436 /// becomes: 9437 /// vcvt.s32.f32 d16, d16, #3 9438 static SDValue PerformVCVTCombine(SDNode *N, 9439 TargetLowering::DAGCombinerInfo &DCI, 9440 const ARMSubtarget *Subtarget) { 9441 SelectionDAG &DAG = DCI.DAG; 9442 SDValue Op = N->getOperand(0); 9443 9444 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() || 9445 Op.getOpcode() != ISD::FMUL) 9446 return SDValue(); 9447 9448 uint64_t C; 9449 SDValue N0 = Op->getOperand(0); 9450 SDValue ConstVec = Op->getOperand(1); 9451 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 9452 9453 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 9454 !isConstVecPow2(ConstVec, isSigned, C)) 9455 return SDValue(); 9456 9457 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 9458 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 9459 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) { 9460 // These instructions only exist converting from f32 to i32. We can handle 9461 // smaller integers by generating an extra truncate, but larger ones would 9462 // be lossy. 9463 return SDValue(); 9464 } 9465 9466 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 9467 Intrinsic::arm_neon_vcvtfp2fxu; 9468 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 9469 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), 9470 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 9471 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0, 9472 DAG.getConstant(Log2_64(C), MVT::i32)); 9473 9474 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits()) 9475 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv); 9476 9477 return FixConv; 9478 } 9479 9480 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 9481 /// can replace combinations of VCVT (integer to floating-point) and VDIV 9482 /// when the VDIV has a constant operand that is a power of 2. 9483 /// 9484 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 9485 /// vcvt.f32.s32 d16, d16 9486 /// vdiv.f32 d16, d17, d16 9487 /// becomes: 9488 /// vcvt.f32.s32 d16, d16, #3 9489 static SDValue PerformVDIVCombine(SDNode *N, 9490 TargetLowering::DAGCombinerInfo &DCI, 9491 const ARMSubtarget *Subtarget) { 9492 SelectionDAG &DAG = DCI.DAG; 9493 SDValue Op = N->getOperand(0); 9494 unsigned OpOpcode = Op.getNode()->getOpcode(); 9495 9496 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() || 9497 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 9498 return SDValue(); 9499 9500 uint64_t C; 9501 SDValue ConstVec = N->getOperand(1); 9502 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 9503 9504 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 9505 !isConstVecPow2(ConstVec, isSigned, C)) 9506 return SDValue(); 9507 9508 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 9509 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 9510 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) { 9511 // These instructions only exist converting from i32 to f32. We can handle 9512 // smaller integers by generating an extra extend, but larger ones would 9513 // be lossy. 9514 return SDValue(); 9515 } 9516 9517 SDValue ConvInput = Op.getOperand(0); 9518 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 9519 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits()) 9520 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 9521 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 9522 ConvInput); 9523 9524 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 9525 Intrinsic::arm_neon_vcvtfxu2fp; 9526 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), 9527 Op.getValueType(), 9528 DAG.getConstant(IntrinsicOpcode, MVT::i32), 9529 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32)); 9530 } 9531 9532 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 9533 /// operand of a vector shift operation, where all the elements of the 9534 /// build_vector must have the same constant integer value. 9535 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 9536 // Ignore bit_converts. 9537 while (Op.getOpcode() == ISD::BITCAST) 9538 Op = Op.getOperand(0); 9539 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 9540 APInt SplatBits, SplatUndef; 9541 unsigned SplatBitSize; 9542 bool HasAnyUndefs; 9543 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 9544 HasAnyUndefs, ElementBits) || 9545 SplatBitSize > ElementBits) 9546 return false; 9547 Cnt = SplatBits.getSExtValue(); 9548 return true; 9549 } 9550 9551 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 9552 /// operand of a vector shift left operation. That value must be in the range: 9553 /// 0 <= Value < ElementBits for a left shift; or 9554 /// 0 <= Value <= ElementBits for a long left shift. 9555 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 9556 assert(VT.isVector() && "vector shift count is not a vector type"); 9557 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 9558 if (! getVShiftImm(Op, ElementBits, Cnt)) 9559 return false; 9560 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 9561 } 9562 9563 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 9564 /// operand of a vector shift right operation. For a shift opcode, the value 9565 /// is positive, but for an intrinsic the value count must be negative. The 9566 /// absolute value must be in the range: 9567 /// 1 <= |Value| <= ElementBits for a right shift; or 9568 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 9569 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 9570 int64_t &Cnt) { 9571 assert(VT.isVector() && "vector shift count is not a vector type"); 9572 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 9573 if (! getVShiftImm(Op, ElementBits, Cnt)) 9574 return false; 9575 if (isIntrinsic) 9576 Cnt = -Cnt; 9577 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 9578 } 9579 9580 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 9581 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 9582 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 9583 switch (IntNo) { 9584 default: 9585 // Don't do anything for most intrinsics. 9586 break; 9587 9588 // Vector shifts: check for immediate versions and lower them. 9589 // Note: This is done during DAG combining instead of DAG legalizing because 9590 // the build_vectors for 64-bit vector element shift counts are generally 9591 // not legal, and it is hard to see their values after they get legalized to 9592 // loads from a constant pool. 9593 case Intrinsic::arm_neon_vshifts: 9594 case Intrinsic::arm_neon_vshiftu: 9595 case Intrinsic::arm_neon_vshiftls: 9596 case Intrinsic::arm_neon_vshiftlu: 9597 case Intrinsic::arm_neon_vshiftn: 9598 case Intrinsic::arm_neon_vrshifts: 9599 case Intrinsic::arm_neon_vrshiftu: 9600 case Intrinsic::arm_neon_vrshiftn: 9601 case Intrinsic::arm_neon_vqshifts: 9602 case Intrinsic::arm_neon_vqshiftu: 9603 case Intrinsic::arm_neon_vqshiftsu: 9604 case Intrinsic::arm_neon_vqshiftns: 9605 case Intrinsic::arm_neon_vqshiftnu: 9606 case Intrinsic::arm_neon_vqshiftnsu: 9607 case Intrinsic::arm_neon_vqrshiftns: 9608 case Intrinsic::arm_neon_vqrshiftnu: 9609 case Intrinsic::arm_neon_vqrshiftnsu: { 9610 EVT VT = N->getOperand(1).getValueType(); 9611 int64_t Cnt; 9612 unsigned VShiftOpc = 0; 9613 9614 switch (IntNo) { 9615 case Intrinsic::arm_neon_vshifts: 9616 case Intrinsic::arm_neon_vshiftu: 9617 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 9618 VShiftOpc = ARMISD::VSHL; 9619 break; 9620 } 9621 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 9622 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 9623 ARMISD::VSHRs : ARMISD::VSHRu); 9624 break; 9625 } 9626 return SDValue(); 9627 9628 case Intrinsic::arm_neon_vshiftls: 9629 case Intrinsic::arm_neon_vshiftlu: 9630 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt)) 9631 break; 9632 llvm_unreachable("invalid shift count for vshll intrinsic"); 9633 9634 case Intrinsic::arm_neon_vrshifts: 9635 case Intrinsic::arm_neon_vrshiftu: 9636 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 9637 break; 9638 return SDValue(); 9639 9640 case Intrinsic::arm_neon_vqshifts: 9641 case Intrinsic::arm_neon_vqshiftu: 9642 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 9643 break; 9644 return SDValue(); 9645 9646 case Intrinsic::arm_neon_vqshiftsu: 9647 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 9648 break; 9649 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 9650 9651 case Intrinsic::arm_neon_vshiftn: 9652 case Intrinsic::arm_neon_vrshiftn: 9653 case Intrinsic::arm_neon_vqshiftns: 9654 case Intrinsic::arm_neon_vqshiftnu: 9655 case Intrinsic::arm_neon_vqshiftnsu: 9656 case Intrinsic::arm_neon_vqrshiftns: 9657 case Intrinsic::arm_neon_vqrshiftnu: 9658 case Intrinsic::arm_neon_vqrshiftnsu: 9659 // Narrowing shifts require an immediate right shift. 9660 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 9661 break; 9662 llvm_unreachable("invalid shift count for narrowing vector shift " 9663 "intrinsic"); 9664 9665 default: 9666 llvm_unreachable("unhandled vector shift"); 9667 } 9668 9669 switch (IntNo) { 9670 case Intrinsic::arm_neon_vshifts: 9671 case Intrinsic::arm_neon_vshiftu: 9672 // Opcode already set above. 9673 break; 9674 case Intrinsic::arm_neon_vshiftls: 9675 case Intrinsic::arm_neon_vshiftlu: 9676 if (Cnt == VT.getVectorElementType().getSizeInBits()) 9677 VShiftOpc = ARMISD::VSHLLi; 9678 else 9679 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ? 9680 ARMISD::VSHLLs : ARMISD::VSHLLu); 9681 break; 9682 case Intrinsic::arm_neon_vshiftn: 9683 VShiftOpc = ARMISD::VSHRN; break; 9684 case Intrinsic::arm_neon_vrshifts: 9685 VShiftOpc = ARMISD::VRSHRs; break; 9686 case Intrinsic::arm_neon_vrshiftu: 9687 VShiftOpc = ARMISD::VRSHRu; break; 9688 case Intrinsic::arm_neon_vrshiftn: 9689 VShiftOpc = ARMISD::VRSHRN; break; 9690 case Intrinsic::arm_neon_vqshifts: 9691 VShiftOpc = ARMISD::VQSHLs; break; 9692 case Intrinsic::arm_neon_vqshiftu: 9693 VShiftOpc = ARMISD::VQSHLu; break; 9694 case Intrinsic::arm_neon_vqshiftsu: 9695 VShiftOpc = ARMISD::VQSHLsu; break; 9696 case Intrinsic::arm_neon_vqshiftns: 9697 VShiftOpc = ARMISD::VQSHRNs; break; 9698 case Intrinsic::arm_neon_vqshiftnu: 9699 VShiftOpc = ARMISD::VQSHRNu; break; 9700 case Intrinsic::arm_neon_vqshiftnsu: 9701 VShiftOpc = ARMISD::VQSHRNsu; break; 9702 case Intrinsic::arm_neon_vqrshiftns: 9703 VShiftOpc = ARMISD::VQRSHRNs; break; 9704 case Intrinsic::arm_neon_vqrshiftnu: 9705 VShiftOpc = ARMISD::VQRSHRNu; break; 9706 case Intrinsic::arm_neon_vqrshiftnsu: 9707 VShiftOpc = ARMISD::VQRSHRNsu; break; 9708 } 9709 9710 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0), 9711 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32)); 9712 } 9713 9714 case Intrinsic::arm_neon_vshiftins: { 9715 EVT VT = N->getOperand(1).getValueType(); 9716 int64_t Cnt; 9717 unsigned VShiftOpc = 0; 9718 9719 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 9720 VShiftOpc = ARMISD::VSLI; 9721 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 9722 VShiftOpc = ARMISD::VSRI; 9723 else { 9724 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 9725 } 9726 9727 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0), 9728 N->getOperand(1), N->getOperand(2), 9729 DAG.getConstant(Cnt, MVT::i32)); 9730 } 9731 9732 case Intrinsic::arm_neon_vqrshifts: 9733 case Intrinsic::arm_neon_vqrshiftu: 9734 // No immediate versions of these to check for. 9735 break; 9736 } 9737 9738 return SDValue(); 9739 } 9740 9741 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 9742 /// lowers them. As with the vector shift intrinsics, this is done during DAG 9743 /// combining instead of DAG legalizing because the build_vectors for 64-bit 9744 /// vector element shift counts are generally not legal, and it is hard to see 9745 /// their values after they get legalized to loads from a constant pool. 9746 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 9747 const ARMSubtarget *ST) { 9748 EVT VT = N->getValueType(0); 9749 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 9750 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 9751 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 9752 SDValue N1 = N->getOperand(1); 9753 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 9754 SDValue N0 = N->getOperand(0); 9755 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 9756 DAG.MaskedValueIsZero(N0.getOperand(0), 9757 APInt::getHighBitsSet(32, 16))) 9758 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 9759 } 9760 } 9761 9762 // Nothing to be done for scalar shifts. 9763 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9764 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 9765 return SDValue(); 9766 9767 assert(ST->hasNEON() && "unexpected vector shift"); 9768 int64_t Cnt; 9769 9770 switch (N->getOpcode()) { 9771 default: llvm_unreachable("unexpected shift opcode"); 9772 9773 case ISD::SHL: 9774 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) 9775 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0), 9776 DAG.getConstant(Cnt, MVT::i32)); 9777 break; 9778 9779 case ISD::SRA: 9780 case ISD::SRL: 9781 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 9782 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 9783 ARMISD::VSHRs : ARMISD::VSHRu); 9784 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0), 9785 DAG.getConstant(Cnt, MVT::i32)); 9786 } 9787 } 9788 return SDValue(); 9789 } 9790 9791 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 9792 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 9793 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 9794 const ARMSubtarget *ST) { 9795 SDValue N0 = N->getOperand(0); 9796 9797 // Check for sign- and zero-extensions of vector extract operations of 8- 9798 // and 16-bit vector elements. NEON supports these directly. They are 9799 // handled during DAG combining because type legalization will promote them 9800 // to 32-bit types and it is messy to recognize the operations after that. 9801 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 9802 SDValue Vec = N0.getOperand(0); 9803 SDValue Lane = N0.getOperand(1); 9804 EVT VT = N->getValueType(0); 9805 EVT EltVT = N0.getValueType(); 9806 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9807 9808 if (VT == MVT::i32 && 9809 (EltVT == MVT::i8 || EltVT == MVT::i16) && 9810 TLI.isTypeLegal(Vec.getValueType()) && 9811 isa<ConstantSDNode>(Lane)) { 9812 9813 unsigned Opc = 0; 9814 switch (N->getOpcode()) { 9815 default: llvm_unreachable("unexpected opcode"); 9816 case ISD::SIGN_EXTEND: 9817 Opc = ARMISD::VGETLANEs; 9818 break; 9819 case ISD::ZERO_EXTEND: 9820 case ISD::ANY_EXTEND: 9821 Opc = ARMISD::VGETLANEu; 9822 break; 9823 } 9824 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 9825 } 9826 } 9827 9828 return SDValue(); 9829 } 9830 9831 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC 9832 /// to match f32 max/min patterns to use NEON vmax/vmin instructions. 9833 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG, 9834 const ARMSubtarget *ST) { 9835 // If the target supports NEON, try to use vmax/vmin instructions for f32 9836 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set, 9837 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is 9838 // a NaN; only do the transformation when it matches that behavior. 9839 9840 // For now only do this when using NEON for FP operations; if using VFP, it 9841 // is not obvious that the benefit outweighs the cost of switching to the 9842 // NEON pipeline. 9843 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() || 9844 N->getValueType(0) != MVT::f32) 9845 return SDValue(); 9846 9847 SDValue CondLHS = N->getOperand(0); 9848 SDValue CondRHS = N->getOperand(1); 9849 SDValue LHS = N->getOperand(2); 9850 SDValue RHS = N->getOperand(3); 9851 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 9852 9853 unsigned Opcode = 0; 9854 bool IsReversed; 9855 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) { 9856 IsReversed = false; // x CC y ? x : y 9857 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) { 9858 IsReversed = true ; // x CC y ? y : x 9859 } else { 9860 return SDValue(); 9861 } 9862 9863 bool IsUnordered; 9864 switch (CC) { 9865 default: break; 9866 case ISD::SETOLT: 9867 case ISD::SETOLE: 9868 case ISD::SETLT: 9869 case ISD::SETLE: 9870 case ISD::SETULT: 9871 case ISD::SETULE: 9872 // If LHS is NaN, an ordered comparison will be false and the result will 9873 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS 9874 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 9875 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE); 9876 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 9877 break; 9878 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin 9879 // will return -0, so vmin can only be used for unsafe math or if one of 9880 // the operands is known to be nonzero. 9881 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) && 9882 !DAG.getTarget().Options.UnsafeFPMath && 9883 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 9884 break; 9885 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN; 9886 break; 9887 9888 case ISD::SETOGT: 9889 case ISD::SETOGE: 9890 case ISD::SETGT: 9891 case ISD::SETGE: 9892 case ISD::SETUGT: 9893 case ISD::SETUGE: 9894 // If LHS is NaN, an ordered comparison will be false and the result will 9895 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS 9896 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 9897 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE); 9898 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 9899 break; 9900 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax 9901 // will return +0, so vmax can only be used for unsafe math or if one of 9902 // the operands is known to be nonzero. 9903 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) && 9904 !DAG.getTarget().Options.UnsafeFPMath && 9905 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 9906 break; 9907 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX; 9908 break; 9909 } 9910 9911 if (!Opcode) 9912 return SDValue(); 9913 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS); 9914 } 9915 9916 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 9917 SDValue 9918 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 9919 SDValue Cmp = N->getOperand(4); 9920 if (Cmp.getOpcode() != ARMISD::CMPZ) 9921 // Only looking at EQ and NE cases. 9922 return SDValue(); 9923 9924 EVT VT = N->getValueType(0); 9925 SDLoc dl(N); 9926 SDValue LHS = Cmp.getOperand(0); 9927 SDValue RHS = Cmp.getOperand(1); 9928 SDValue FalseVal = N->getOperand(0); 9929 SDValue TrueVal = N->getOperand(1); 9930 SDValue ARMcc = N->getOperand(2); 9931 ARMCC::CondCodes CC = 9932 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 9933 9934 // Simplify 9935 // mov r1, r0 9936 // cmp r1, x 9937 // mov r0, y 9938 // moveq r0, x 9939 // to 9940 // cmp r0, x 9941 // movne r0, y 9942 // 9943 // mov r1, r0 9944 // cmp r1, x 9945 // mov r0, x 9946 // movne r0, y 9947 // to 9948 // cmp r0, x 9949 // movne r0, y 9950 /// FIXME: Turn this into a target neutral optimization? 9951 SDValue Res; 9952 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 9953 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 9954 N->getOperand(3), Cmp); 9955 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 9956 SDValue ARMcc; 9957 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 9958 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 9959 N->getOperand(3), NewCmp); 9960 } 9961 9962 if (Res.getNode()) { 9963 APInt KnownZero, KnownOne; 9964 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne); 9965 // Capture demanded bits information that would be otherwise lost. 9966 if (KnownZero == 0xfffffffe) 9967 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9968 DAG.getValueType(MVT::i1)); 9969 else if (KnownZero == 0xffffff00) 9970 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9971 DAG.getValueType(MVT::i8)); 9972 else if (KnownZero == 0xffff0000) 9973 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9974 DAG.getValueType(MVT::i16)); 9975 } 9976 9977 return Res; 9978 } 9979 9980 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 9981 DAGCombinerInfo &DCI) const { 9982 switch (N->getOpcode()) { 9983 default: break; 9984 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget); 9985 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 9986 case ISD::SUB: return PerformSUBCombine(N, DCI); 9987 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 9988 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 9989 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 9990 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 9991 case ARMISD::BFI: return PerformBFICombine(N, DCI); 9992 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI); 9993 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 9994 case ISD::STORE: return PerformSTORECombine(N, DCI); 9995 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI); 9996 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 9997 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 9998 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 9999 case ISD::FP_TO_SINT: 10000 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget); 10001 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget); 10002 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 10003 case ISD::SHL: 10004 case ISD::SRA: 10005 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 10006 case ISD::SIGN_EXTEND: 10007 case ISD::ZERO_EXTEND: 10008 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 10009 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget); 10010 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 10011 case ARMISD::VLD2DUP: 10012 case ARMISD::VLD3DUP: 10013 case ARMISD::VLD4DUP: 10014 return CombineBaseUpdate(N, DCI); 10015 case ARMISD::BUILD_VECTOR: 10016 return PerformARMBUILD_VECTORCombine(N, DCI); 10017 case ISD::INTRINSIC_VOID: 10018 case ISD::INTRINSIC_W_CHAIN: 10019 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 10020 case Intrinsic::arm_neon_vld1: 10021 case Intrinsic::arm_neon_vld2: 10022 case Intrinsic::arm_neon_vld3: 10023 case Intrinsic::arm_neon_vld4: 10024 case Intrinsic::arm_neon_vld2lane: 10025 case Intrinsic::arm_neon_vld3lane: 10026 case Intrinsic::arm_neon_vld4lane: 10027 case Intrinsic::arm_neon_vst1: 10028 case Intrinsic::arm_neon_vst2: 10029 case Intrinsic::arm_neon_vst3: 10030 case Intrinsic::arm_neon_vst4: 10031 case Intrinsic::arm_neon_vst2lane: 10032 case Intrinsic::arm_neon_vst3lane: 10033 case Intrinsic::arm_neon_vst4lane: 10034 return CombineBaseUpdate(N, DCI); 10035 default: break; 10036 } 10037 break; 10038 } 10039 return SDValue(); 10040 } 10041 10042 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 10043 EVT VT) const { 10044 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 10045 } 10046 10047 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const { 10048 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus 10049 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 10050 10051 switch (VT.getSimpleVT().SimpleTy) { 10052 default: 10053 return false; 10054 case MVT::i8: 10055 case MVT::i16: 10056 case MVT::i32: { 10057 // Unaligned access can use (for example) LRDB, LRDH, LDR 10058 if (AllowsUnaligned) { 10059 if (Fast) 10060 *Fast = Subtarget->hasV7Ops(); 10061 return true; 10062 } 10063 return false; 10064 } 10065 case MVT::f64: 10066 case MVT::v2f64: { 10067 // For any little-endian targets with neon, we can support unaligned ld/st 10068 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 10069 // A big-endian target may also explictly support unaligned accesses 10070 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) { 10071 if (Fast) 10072 *Fast = true; 10073 return true; 10074 } 10075 return false; 10076 } 10077 } 10078 } 10079 10080 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 10081 unsigned AlignCheck) { 10082 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 10083 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 10084 } 10085 10086 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 10087 unsigned DstAlign, unsigned SrcAlign, 10088 bool IsMemset, bool ZeroMemset, 10089 bool MemcpyStrSrc, 10090 MachineFunction &MF) const { 10091 const Function *F = MF.getFunction(); 10092 10093 // See if we can use NEON instructions for this... 10094 if ((!IsMemset || ZeroMemset) && 10095 Subtarget->hasNEON() && 10096 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 10097 Attribute::NoImplicitFloat)) { 10098 bool Fast; 10099 if (Size >= 16 && 10100 (memOpAlign(SrcAlign, DstAlign, 16) || 10101 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) { 10102 return MVT::v2f64; 10103 } else if (Size >= 8 && 10104 (memOpAlign(SrcAlign, DstAlign, 8) || 10105 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) { 10106 return MVT::f64; 10107 } 10108 } 10109 10110 // Lowering to i32/i16 if the size permits. 10111 if (Size >= 4) 10112 return MVT::i32; 10113 else if (Size >= 2) 10114 return MVT::i16; 10115 10116 // Let the target-independent logic figure it out. 10117 return MVT::Other; 10118 } 10119 10120 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 10121 if (Val.getOpcode() != ISD::LOAD) 10122 return false; 10123 10124 EVT VT1 = Val.getValueType(); 10125 if (!VT1.isSimple() || !VT1.isInteger() || 10126 !VT2.isSimple() || !VT2.isInteger()) 10127 return false; 10128 10129 switch (VT1.getSimpleVT().SimpleTy) { 10130 default: break; 10131 case MVT::i1: 10132 case MVT::i8: 10133 case MVT::i16: 10134 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 10135 return true; 10136 } 10137 10138 return false; 10139 } 10140 10141 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 10142 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 10143 return false; 10144 10145 if (!isTypeLegal(EVT::getEVT(Ty1))) 10146 return false; 10147 10148 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 10149 10150 // Assuming the caller doesn't have a zeroext or signext return parameter, 10151 // truncation all the way down to i1 is valid. 10152 return true; 10153 } 10154 10155 10156 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 10157 if (V < 0) 10158 return false; 10159 10160 unsigned Scale = 1; 10161 switch (VT.getSimpleVT().SimpleTy) { 10162 default: return false; 10163 case MVT::i1: 10164 case MVT::i8: 10165 // Scale == 1; 10166 break; 10167 case MVT::i16: 10168 // Scale == 2; 10169 Scale = 2; 10170 break; 10171 case MVT::i32: 10172 // Scale == 4; 10173 Scale = 4; 10174 break; 10175 } 10176 10177 if ((V & (Scale - 1)) != 0) 10178 return false; 10179 V /= Scale; 10180 return V == (V & ((1LL << 5) - 1)); 10181 } 10182 10183 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 10184 const ARMSubtarget *Subtarget) { 10185 bool isNeg = false; 10186 if (V < 0) { 10187 isNeg = true; 10188 V = - V; 10189 } 10190 10191 switch (VT.getSimpleVT().SimpleTy) { 10192 default: return false; 10193 case MVT::i1: 10194 case MVT::i8: 10195 case MVT::i16: 10196 case MVT::i32: 10197 // + imm12 or - imm8 10198 if (isNeg) 10199 return V == (V & ((1LL << 8) - 1)); 10200 return V == (V & ((1LL << 12) - 1)); 10201 case MVT::f32: 10202 case MVT::f64: 10203 // Same as ARM mode. FIXME: NEON? 10204 if (!Subtarget->hasVFP2()) 10205 return false; 10206 if ((V & 3) != 0) 10207 return false; 10208 V >>= 2; 10209 return V == (V & ((1LL << 8) - 1)); 10210 } 10211 } 10212 10213 /// isLegalAddressImmediate - Return true if the integer value can be used 10214 /// as the offset of the target addressing mode for load / store of the 10215 /// given type. 10216 static bool isLegalAddressImmediate(int64_t V, EVT VT, 10217 const ARMSubtarget *Subtarget) { 10218 if (V == 0) 10219 return true; 10220 10221 if (!VT.isSimple()) 10222 return false; 10223 10224 if (Subtarget->isThumb1Only()) 10225 return isLegalT1AddressImmediate(V, VT); 10226 else if (Subtarget->isThumb2()) 10227 return isLegalT2AddressImmediate(V, VT, Subtarget); 10228 10229 // ARM mode. 10230 if (V < 0) 10231 V = - V; 10232 switch (VT.getSimpleVT().SimpleTy) { 10233 default: return false; 10234 case MVT::i1: 10235 case MVT::i8: 10236 case MVT::i32: 10237 // +- imm12 10238 return V == (V & ((1LL << 12) - 1)); 10239 case MVT::i16: 10240 // +- imm8 10241 return V == (V & ((1LL << 8) - 1)); 10242 case MVT::f32: 10243 case MVT::f64: 10244 if (!Subtarget->hasVFP2()) // FIXME: NEON? 10245 return false; 10246 if ((V & 3) != 0) 10247 return false; 10248 V >>= 2; 10249 return V == (V & ((1LL << 8) - 1)); 10250 } 10251 } 10252 10253 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 10254 EVT VT) const { 10255 int Scale = AM.Scale; 10256 if (Scale < 0) 10257 return false; 10258 10259 switch (VT.getSimpleVT().SimpleTy) { 10260 default: return false; 10261 case MVT::i1: 10262 case MVT::i8: 10263 case MVT::i16: 10264 case MVT::i32: 10265 if (Scale == 1) 10266 return true; 10267 // r + r << imm 10268 Scale = Scale & ~1; 10269 return Scale == 2 || Scale == 4 || Scale == 8; 10270 case MVT::i64: 10271 // r + r 10272 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 10273 return true; 10274 return false; 10275 case MVT::isVoid: 10276 // Note, we allow "void" uses (basically, uses that aren't loads or 10277 // stores), because arm allows folding a scale into many arithmetic 10278 // operations. This should be made more precise and revisited later. 10279 10280 // Allow r << imm, but the imm has to be a multiple of two. 10281 if (Scale & 1) return false; 10282 return isPowerOf2_32(Scale); 10283 } 10284 } 10285 10286 /// isLegalAddressingMode - Return true if the addressing mode represented 10287 /// by AM is legal for this target, for a load/store of the specified type. 10288 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, 10289 Type *Ty) const { 10290 EVT VT = getValueType(Ty, true); 10291 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 10292 return false; 10293 10294 // Can never fold addr of global into load/store. 10295 if (AM.BaseGV) 10296 return false; 10297 10298 switch (AM.Scale) { 10299 case 0: // no scale reg, must be "r+i" or "r", or "i". 10300 break; 10301 case 1: 10302 if (Subtarget->isThumb1Only()) 10303 return false; 10304 // FALL THROUGH. 10305 default: 10306 // ARM doesn't support any R+R*scale+imm addr modes. 10307 if (AM.BaseOffs) 10308 return false; 10309 10310 if (!VT.isSimple()) 10311 return false; 10312 10313 if (Subtarget->isThumb2()) 10314 return isLegalT2ScaledAddressingMode(AM, VT); 10315 10316 int Scale = AM.Scale; 10317 switch (VT.getSimpleVT().SimpleTy) { 10318 default: return false; 10319 case MVT::i1: 10320 case MVT::i8: 10321 case MVT::i32: 10322 if (Scale < 0) Scale = -Scale; 10323 if (Scale == 1) 10324 return true; 10325 // r + r << imm 10326 return isPowerOf2_32(Scale & ~1); 10327 case MVT::i16: 10328 case MVT::i64: 10329 // r + r 10330 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 10331 return true; 10332 return false; 10333 10334 case MVT::isVoid: 10335 // Note, we allow "void" uses (basically, uses that aren't loads or 10336 // stores), because arm allows folding a scale into many arithmetic 10337 // operations. This should be made more precise and revisited later. 10338 10339 // Allow r << imm, but the imm has to be a multiple of two. 10340 if (Scale & 1) return false; 10341 return isPowerOf2_32(Scale); 10342 } 10343 } 10344 return true; 10345 } 10346 10347 /// isLegalICmpImmediate - Return true if the specified immediate is legal 10348 /// icmp immediate, that is the target has icmp instructions which can compare 10349 /// a register against the immediate without having to materialize the 10350 /// immediate into a register. 10351 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 10352 // Thumb2 and ARM modes can use cmn for negative immediates. 10353 if (!Subtarget->isThumb()) 10354 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1; 10355 if (Subtarget->isThumb2()) 10356 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1; 10357 // Thumb1 doesn't have cmn, and only 8-bit immediates. 10358 return Imm >= 0 && Imm <= 255; 10359 } 10360 10361 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 10362 /// *or sub* immediate, that is the target has add or sub instructions which can 10363 /// add a register with the immediate without having to materialize the 10364 /// immediate into a register. 10365 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 10366 // Same encoding for add/sub, just flip the sign. 10367 int64_t AbsImm = llvm::abs64(Imm); 10368 if (!Subtarget->isThumb()) 10369 return ARM_AM::getSOImmVal(AbsImm) != -1; 10370 if (Subtarget->isThumb2()) 10371 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 10372 // Thumb1 only has 8-bit unsigned immediate. 10373 return AbsImm >= 0 && AbsImm <= 255; 10374 } 10375 10376 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 10377 bool isSEXTLoad, SDValue &Base, 10378 SDValue &Offset, bool &isInc, 10379 SelectionDAG &DAG) { 10380 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 10381 return false; 10382 10383 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 10384 // AddressingMode 3 10385 Base = Ptr->getOperand(0); 10386 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10387 int RHSC = (int)RHS->getZExtValue(); 10388 if (RHSC < 0 && RHSC > -256) { 10389 assert(Ptr->getOpcode() == ISD::ADD); 10390 isInc = false; 10391 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 10392 return true; 10393 } 10394 } 10395 isInc = (Ptr->getOpcode() == ISD::ADD); 10396 Offset = Ptr->getOperand(1); 10397 return true; 10398 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 10399 // AddressingMode 2 10400 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10401 int RHSC = (int)RHS->getZExtValue(); 10402 if (RHSC < 0 && RHSC > -0x1000) { 10403 assert(Ptr->getOpcode() == ISD::ADD); 10404 isInc = false; 10405 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 10406 Base = Ptr->getOperand(0); 10407 return true; 10408 } 10409 } 10410 10411 if (Ptr->getOpcode() == ISD::ADD) { 10412 isInc = true; 10413 ARM_AM::ShiftOpc ShOpcVal= 10414 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 10415 if (ShOpcVal != ARM_AM::no_shift) { 10416 Base = Ptr->getOperand(1); 10417 Offset = Ptr->getOperand(0); 10418 } else { 10419 Base = Ptr->getOperand(0); 10420 Offset = Ptr->getOperand(1); 10421 } 10422 return true; 10423 } 10424 10425 isInc = (Ptr->getOpcode() == ISD::ADD); 10426 Base = Ptr->getOperand(0); 10427 Offset = Ptr->getOperand(1); 10428 return true; 10429 } 10430 10431 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 10432 return false; 10433 } 10434 10435 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 10436 bool isSEXTLoad, SDValue &Base, 10437 SDValue &Offset, bool &isInc, 10438 SelectionDAG &DAG) { 10439 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 10440 return false; 10441 10442 Base = Ptr->getOperand(0); 10443 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10444 int RHSC = (int)RHS->getZExtValue(); 10445 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 10446 assert(Ptr->getOpcode() == ISD::ADD); 10447 isInc = false; 10448 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 10449 return true; 10450 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 10451 isInc = Ptr->getOpcode() == ISD::ADD; 10452 Offset = DAG.getConstant(RHSC, RHS->getValueType(0)); 10453 return true; 10454 } 10455 } 10456 10457 return false; 10458 } 10459 10460 /// getPreIndexedAddressParts - returns true by value, base pointer and 10461 /// offset pointer and addressing mode by reference if the node's address 10462 /// can be legally represented as pre-indexed load / store address. 10463 bool 10464 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 10465 SDValue &Offset, 10466 ISD::MemIndexedMode &AM, 10467 SelectionDAG &DAG) const { 10468 if (Subtarget->isThumb1Only()) 10469 return false; 10470 10471 EVT VT; 10472 SDValue Ptr; 10473 bool isSEXTLoad = false; 10474 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10475 Ptr = LD->getBasePtr(); 10476 VT = LD->getMemoryVT(); 10477 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 10478 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10479 Ptr = ST->getBasePtr(); 10480 VT = ST->getMemoryVT(); 10481 } else 10482 return false; 10483 10484 bool isInc; 10485 bool isLegal = false; 10486 if (Subtarget->isThumb2()) 10487 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 10488 Offset, isInc, DAG); 10489 else 10490 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 10491 Offset, isInc, DAG); 10492 if (!isLegal) 10493 return false; 10494 10495 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 10496 return true; 10497 } 10498 10499 /// getPostIndexedAddressParts - returns true by value, base pointer and 10500 /// offset pointer and addressing mode by reference if this node can be 10501 /// combined with a load / store to form a post-indexed load / store. 10502 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 10503 SDValue &Base, 10504 SDValue &Offset, 10505 ISD::MemIndexedMode &AM, 10506 SelectionDAG &DAG) const { 10507 if (Subtarget->isThumb1Only()) 10508 return false; 10509 10510 EVT VT; 10511 SDValue Ptr; 10512 bool isSEXTLoad = false; 10513 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10514 VT = LD->getMemoryVT(); 10515 Ptr = LD->getBasePtr(); 10516 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 10517 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10518 VT = ST->getMemoryVT(); 10519 Ptr = ST->getBasePtr(); 10520 } else 10521 return false; 10522 10523 bool isInc; 10524 bool isLegal = false; 10525 if (Subtarget->isThumb2()) 10526 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 10527 isInc, DAG); 10528 else 10529 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 10530 isInc, DAG); 10531 if (!isLegal) 10532 return false; 10533 10534 if (Ptr != Base) { 10535 // Swap base ptr and offset to catch more post-index load / store when 10536 // it's legal. In Thumb2 mode, offset must be an immediate. 10537 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 10538 !Subtarget->isThumb2()) 10539 std::swap(Base, Offset); 10540 10541 // Post-indexed load / store update the base pointer. 10542 if (Ptr != Base) 10543 return false; 10544 } 10545 10546 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 10547 return true; 10548 } 10549 10550 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, 10551 APInt &KnownZero, 10552 APInt &KnownOne, 10553 const SelectionDAG &DAG, 10554 unsigned Depth) const { 10555 unsigned BitWidth = KnownOne.getBitWidth(); 10556 KnownZero = KnownOne = APInt(BitWidth, 0); 10557 switch (Op.getOpcode()) { 10558 default: break; 10559 case ARMISD::ADDC: 10560 case ARMISD::ADDE: 10561 case ARMISD::SUBC: 10562 case ARMISD::SUBE: 10563 // These nodes' second result is a boolean 10564 if (Op.getResNo() == 0) 10565 break; 10566 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 10567 break; 10568 case ARMISD::CMOV: { 10569 // Bits are known zero/one if known on the LHS and RHS. 10570 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 10571 if (KnownZero == 0 && KnownOne == 0) return; 10572 10573 APInt KnownZeroRHS, KnownOneRHS; 10574 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1); 10575 KnownZero &= KnownZeroRHS; 10576 KnownOne &= KnownOneRHS; 10577 return; 10578 } 10579 } 10580 } 10581 10582 //===----------------------------------------------------------------------===// 10583 // ARM Inline Assembly Support 10584 //===----------------------------------------------------------------------===// 10585 10586 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 10587 // Looking for "rev" which is V6+. 10588 if (!Subtarget->hasV6Ops()) 10589 return false; 10590 10591 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 10592 std::string AsmStr = IA->getAsmString(); 10593 SmallVector<StringRef, 4> AsmPieces; 10594 SplitString(AsmStr, AsmPieces, ";\n"); 10595 10596 switch (AsmPieces.size()) { 10597 default: return false; 10598 case 1: 10599 AsmStr = AsmPieces[0]; 10600 AsmPieces.clear(); 10601 SplitString(AsmStr, AsmPieces, " \t,"); 10602 10603 // rev $0, $1 10604 if (AsmPieces.size() == 3 && 10605 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 10606 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 10607 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 10608 if (Ty && Ty->getBitWidth() == 32) 10609 return IntrinsicLowering::LowerToByteSwap(CI); 10610 } 10611 break; 10612 } 10613 10614 return false; 10615 } 10616 10617 /// getConstraintType - Given a constraint letter, return the type of 10618 /// constraint it is for this target. 10619 ARMTargetLowering::ConstraintType 10620 ARMTargetLowering::getConstraintType(const std::string &Constraint) const { 10621 if (Constraint.size() == 1) { 10622 switch (Constraint[0]) { 10623 default: break; 10624 case 'l': return C_RegisterClass; 10625 case 'w': return C_RegisterClass; 10626 case 'h': return C_RegisterClass; 10627 case 'x': return C_RegisterClass; 10628 case 't': return C_RegisterClass; 10629 case 'j': return C_Other; // Constant for movw. 10630 // An address with a single base register. Due to the way we 10631 // currently handle addresses it is the same as an 'r' memory constraint. 10632 case 'Q': return C_Memory; 10633 } 10634 } else if (Constraint.size() == 2) { 10635 switch (Constraint[0]) { 10636 default: break; 10637 // All 'U+' constraints are addresses. 10638 case 'U': return C_Memory; 10639 } 10640 } 10641 return TargetLowering::getConstraintType(Constraint); 10642 } 10643 10644 /// Examine constraint type and operand type and determine a weight value. 10645 /// This object must already have been set up with the operand type 10646 /// and the current alternative constraint selected. 10647 TargetLowering::ConstraintWeight 10648 ARMTargetLowering::getSingleConstraintMatchWeight( 10649 AsmOperandInfo &info, const char *constraint) const { 10650 ConstraintWeight weight = CW_Invalid; 10651 Value *CallOperandVal = info.CallOperandVal; 10652 // If we don't have a value, we can't do a match, 10653 // but allow it at the lowest weight. 10654 if (CallOperandVal == NULL) 10655 return CW_Default; 10656 Type *type = CallOperandVal->getType(); 10657 // Look at the constraint type. 10658 switch (*constraint) { 10659 default: 10660 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 10661 break; 10662 case 'l': 10663 if (type->isIntegerTy()) { 10664 if (Subtarget->isThumb()) 10665 weight = CW_SpecificReg; 10666 else 10667 weight = CW_Register; 10668 } 10669 break; 10670 case 'w': 10671 if (type->isFloatingPointTy()) 10672 weight = CW_Register; 10673 break; 10674 } 10675 return weight; 10676 } 10677 10678 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair; 10679 RCPair 10680 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 10681 MVT VT) const { 10682 if (Constraint.size() == 1) { 10683 // GCC ARM Constraint Letters 10684 switch (Constraint[0]) { 10685 case 'l': // Low regs or general regs. 10686 if (Subtarget->isThumb()) 10687 return RCPair(0U, &ARM::tGPRRegClass); 10688 return RCPair(0U, &ARM::GPRRegClass); 10689 case 'h': // High regs or no regs. 10690 if (Subtarget->isThumb()) 10691 return RCPair(0U, &ARM::hGPRRegClass); 10692 break; 10693 case 'r': 10694 return RCPair(0U, &ARM::GPRRegClass); 10695 case 'w': 10696 if (VT == MVT::f32) 10697 return RCPair(0U, &ARM::SPRRegClass); 10698 if (VT.getSizeInBits() == 64) 10699 return RCPair(0U, &ARM::DPRRegClass); 10700 if (VT.getSizeInBits() == 128) 10701 return RCPair(0U, &ARM::QPRRegClass); 10702 break; 10703 case 'x': 10704 if (VT == MVT::f32) 10705 return RCPair(0U, &ARM::SPR_8RegClass); 10706 if (VT.getSizeInBits() == 64) 10707 return RCPair(0U, &ARM::DPR_8RegClass); 10708 if (VT.getSizeInBits() == 128) 10709 return RCPair(0U, &ARM::QPR_8RegClass); 10710 break; 10711 case 't': 10712 if (VT == MVT::f32) 10713 return RCPair(0U, &ARM::SPRRegClass); 10714 break; 10715 } 10716 } 10717 if (StringRef("{cc}").equals_lower(Constraint)) 10718 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 10719 10720 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 10721 } 10722 10723 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 10724 /// vector. If it is invalid, don't add anything to Ops. 10725 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 10726 std::string &Constraint, 10727 std::vector<SDValue>&Ops, 10728 SelectionDAG &DAG) const { 10729 SDValue Result(0, 0); 10730 10731 // Currently only support length 1 constraints. 10732 if (Constraint.length() != 1) return; 10733 10734 char ConstraintLetter = Constraint[0]; 10735 switch (ConstraintLetter) { 10736 default: break; 10737 case 'j': 10738 case 'I': case 'J': case 'K': case 'L': 10739 case 'M': case 'N': case 'O': 10740 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 10741 if (!C) 10742 return; 10743 10744 int64_t CVal64 = C->getSExtValue(); 10745 int CVal = (int) CVal64; 10746 // None of these constraints allow values larger than 32 bits. Check 10747 // that the value fits in an int. 10748 if (CVal != CVal64) 10749 return; 10750 10751 switch (ConstraintLetter) { 10752 case 'j': 10753 // Constant suitable for movw, must be between 0 and 10754 // 65535. 10755 if (Subtarget->hasV6T2Ops()) 10756 if (CVal >= 0 && CVal <= 65535) 10757 break; 10758 return; 10759 case 'I': 10760 if (Subtarget->isThumb1Only()) { 10761 // This must be a constant between 0 and 255, for ADD 10762 // immediates. 10763 if (CVal >= 0 && CVal <= 255) 10764 break; 10765 } else if (Subtarget->isThumb2()) { 10766 // A constant that can be used as an immediate value in a 10767 // data-processing instruction. 10768 if (ARM_AM::getT2SOImmVal(CVal) != -1) 10769 break; 10770 } else { 10771 // A constant that can be used as an immediate value in a 10772 // data-processing instruction. 10773 if (ARM_AM::getSOImmVal(CVal) != -1) 10774 break; 10775 } 10776 return; 10777 10778 case 'J': 10779 if (Subtarget->isThumb()) { // FIXME thumb2 10780 // This must be a constant between -255 and -1, for negated ADD 10781 // immediates. This can be used in GCC with an "n" modifier that 10782 // prints the negated value, for use with SUB instructions. It is 10783 // not useful otherwise but is implemented for compatibility. 10784 if (CVal >= -255 && CVal <= -1) 10785 break; 10786 } else { 10787 // This must be a constant between -4095 and 4095. It is not clear 10788 // what this constraint is intended for. Implemented for 10789 // compatibility with GCC. 10790 if (CVal >= -4095 && CVal <= 4095) 10791 break; 10792 } 10793 return; 10794 10795 case 'K': 10796 if (Subtarget->isThumb1Only()) { 10797 // A 32-bit value where only one byte has a nonzero value. Exclude 10798 // zero to match GCC. This constraint is used by GCC internally for 10799 // constants that can be loaded with a move/shift combination. 10800 // It is not useful otherwise but is implemented for compatibility. 10801 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 10802 break; 10803 } else if (Subtarget->isThumb2()) { 10804 // A constant whose bitwise inverse can be used as an immediate 10805 // value in a data-processing instruction. This can be used in GCC 10806 // with a "B" modifier that prints the inverted value, for use with 10807 // BIC and MVN instructions. It is not useful otherwise but is 10808 // implemented for compatibility. 10809 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 10810 break; 10811 } else { 10812 // A constant whose bitwise inverse can be used as an immediate 10813 // value in a data-processing instruction. This can be used in GCC 10814 // with a "B" modifier that prints the inverted value, for use with 10815 // BIC and MVN instructions. It is not useful otherwise but is 10816 // implemented for compatibility. 10817 if (ARM_AM::getSOImmVal(~CVal) != -1) 10818 break; 10819 } 10820 return; 10821 10822 case 'L': 10823 if (Subtarget->isThumb1Only()) { 10824 // This must be a constant between -7 and 7, 10825 // for 3-operand ADD/SUB immediate instructions. 10826 if (CVal >= -7 && CVal < 7) 10827 break; 10828 } else if (Subtarget->isThumb2()) { 10829 // A constant whose negation can be used as an immediate value in a 10830 // data-processing instruction. This can be used in GCC with an "n" 10831 // modifier that prints the negated value, for use with SUB 10832 // instructions. It is not useful otherwise but is implemented for 10833 // compatibility. 10834 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 10835 break; 10836 } else { 10837 // A constant whose negation can be used as an immediate value in a 10838 // data-processing instruction. This can be used in GCC with an "n" 10839 // modifier that prints the negated value, for use with SUB 10840 // instructions. It is not useful otherwise but is implemented for 10841 // compatibility. 10842 if (ARM_AM::getSOImmVal(-CVal) != -1) 10843 break; 10844 } 10845 return; 10846 10847 case 'M': 10848 if (Subtarget->isThumb()) { // FIXME thumb2 10849 // This must be a multiple of 4 between 0 and 1020, for 10850 // ADD sp + immediate. 10851 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 10852 break; 10853 } else { 10854 // A power of two or a constant between 0 and 32. This is used in 10855 // GCC for the shift amount on shifted register operands, but it is 10856 // useful in general for any shift amounts. 10857 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 10858 break; 10859 } 10860 return; 10861 10862 case 'N': 10863 if (Subtarget->isThumb()) { // FIXME thumb2 10864 // This must be a constant between 0 and 31, for shift amounts. 10865 if (CVal >= 0 && CVal <= 31) 10866 break; 10867 } 10868 return; 10869 10870 case 'O': 10871 if (Subtarget->isThumb()) { // FIXME thumb2 10872 // This must be a multiple of 4 between -508 and 508, for 10873 // ADD/SUB sp = sp + immediate. 10874 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 10875 break; 10876 } 10877 return; 10878 } 10879 Result = DAG.getTargetConstant(CVal, Op.getValueType()); 10880 break; 10881 } 10882 10883 if (Result.getNode()) { 10884 Ops.push_back(Result); 10885 return; 10886 } 10887 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 10888 } 10889 10890 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 10891 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only"); 10892 unsigned Opcode = Op->getOpcode(); 10893 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 10894 "Invalid opcode for Div/Rem lowering"); 10895 bool isSigned = (Opcode == ISD::SDIVREM); 10896 EVT VT = Op->getValueType(0); 10897 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 10898 10899 RTLIB::Libcall LC; 10900 switch (VT.getSimpleVT().SimpleTy) { 10901 default: llvm_unreachable("Unexpected request for libcall!"); 10902 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 10903 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 10904 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 10905 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 10906 } 10907 10908 SDValue InChain = DAG.getEntryNode(); 10909 10910 TargetLowering::ArgListTy Args; 10911 TargetLowering::ArgListEntry Entry; 10912 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) { 10913 EVT ArgVT = Op->getOperand(i).getValueType(); 10914 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 10915 Entry.Node = Op->getOperand(i); 10916 Entry.Ty = ArgTy; 10917 Entry.isSExt = isSigned; 10918 Entry.isZExt = !isSigned; 10919 Args.push_back(Entry); 10920 } 10921 10922 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 10923 getPointerTy()); 10924 10925 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL); 10926 10927 SDLoc dl(Op); 10928 TargetLowering:: 10929 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, true, 10930 0, getLibcallCallingConv(LC), /*isTailCall=*/false, 10931 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true, 10932 Callee, Args, DAG, dl); 10933 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 10934 10935 return CallInfo.first; 10936 } 10937 10938 bool 10939 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 10940 // The ARM target isn't yet aware of offsets. 10941 return false; 10942 } 10943 10944 bool ARM::isBitFieldInvertedMask(unsigned v) { 10945 if (v == 0xffffffff) 10946 return false; 10947 10948 // there can be 1's on either or both "outsides", all the "inside" 10949 // bits must be 0's 10950 unsigned TO = CountTrailingOnes_32(v); 10951 unsigned LO = CountLeadingOnes_32(v); 10952 v = (v >> TO) << TO; 10953 v = (v << LO) >> LO; 10954 return v == 0; 10955 } 10956 10957 /// isFPImmLegal - Returns true if the target can instruction select the 10958 /// specified FP immediate natively. If false, the legalizer will 10959 /// materialize the FP immediate as a load from a constant pool. 10960 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 10961 if (!Subtarget->hasVFP3()) 10962 return false; 10963 if (VT == MVT::f32) 10964 return ARM_AM::getFP32Imm(Imm) != -1; 10965 if (VT == MVT::f64) 10966 return ARM_AM::getFP64Imm(Imm) != -1; 10967 return false; 10968 } 10969 10970 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 10971 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 10972 /// specified in the intrinsic calls. 10973 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 10974 const CallInst &I, 10975 unsigned Intrinsic) const { 10976 switch (Intrinsic) { 10977 case Intrinsic::arm_neon_vld1: 10978 case Intrinsic::arm_neon_vld2: 10979 case Intrinsic::arm_neon_vld3: 10980 case Intrinsic::arm_neon_vld4: 10981 case Intrinsic::arm_neon_vld2lane: 10982 case Intrinsic::arm_neon_vld3lane: 10983 case Intrinsic::arm_neon_vld4lane: { 10984 Info.opc = ISD::INTRINSIC_W_CHAIN; 10985 // Conservatively set memVT to the entire set of vectors loaded. 10986 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8; 10987 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 10988 Info.ptrVal = I.getArgOperand(0); 10989 Info.offset = 0; 10990 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 10991 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 10992 Info.vol = false; // volatile loads with NEON intrinsics not supported 10993 Info.readMem = true; 10994 Info.writeMem = false; 10995 return true; 10996 } 10997 case Intrinsic::arm_neon_vst1: 10998 case Intrinsic::arm_neon_vst2: 10999 case Intrinsic::arm_neon_vst3: 11000 case Intrinsic::arm_neon_vst4: 11001 case Intrinsic::arm_neon_vst2lane: 11002 case Intrinsic::arm_neon_vst3lane: 11003 case Intrinsic::arm_neon_vst4lane: { 11004 Info.opc = ISD::INTRINSIC_VOID; 11005 // Conservatively set memVT to the entire set of vectors stored. 11006 unsigned NumElts = 0; 11007 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 11008 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 11009 if (!ArgTy->isVectorTy()) 11010 break; 11011 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8; 11012 } 11013 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 11014 Info.ptrVal = I.getArgOperand(0); 11015 Info.offset = 0; 11016 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 11017 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 11018 Info.vol = false; // volatile stores with NEON intrinsics not supported 11019 Info.readMem = false; 11020 Info.writeMem = true; 11021 return true; 11022 } 11023 case Intrinsic::arm_ldrex: { 11024 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 11025 Info.opc = ISD::INTRINSIC_W_CHAIN; 11026 Info.memVT = MVT::getVT(PtrTy->getElementType()); 11027 Info.ptrVal = I.getArgOperand(0); 11028 Info.offset = 0; 11029 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType()); 11030 Info.vol = true; 11031 Info.readMem = true; 11032 Info.writeMem = false; 11033 return true; 11034 } 11035 case Intrinsic::arm_strex: { 11036 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 11037 Info.opc = ISD::INTRINSIC_W_CHAIN; 11038 Info.memVT = MVT::getVT(PtrTy->getElementType()); 11039 Info.ptrVal = I.getArgOperand(1); 11040 Info.offset = 0; 11041 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType()); 11042 Info.vol = true; 11043 Info.readMem = false; 11044 Info.writeMem = true; 11045 return true; 11046 } 11047 case Intrinsic::arm_strexd: { 11048 Info.opc = ISD::INTRINSIC_W_CHAIN; 11049 Info.memVT = MVT::i64; 11050 Info.ptrVal = I.getArgOperand(2); 11051 Info.offset = 0; 11052 Info.align = 8; 11053 Info.vol = true; 11054 Info.readMem = false; 11055 Info.writeMem = true; 11056 return true; 11057 } 11058 case Intrinsic::arm_ldrexd: { 11059 Info.opc = ISD::INTRINSIC_W_CHAIN; 11060 Info.memVT = MVT::i64; 11061 Info.ptrVal = I.getArgOperand(0); 11062 Info.offset = 0; 11063 Info.align = 8; 11064 Info.vol = true; 11065 Info.readMem = true; 11066 Info.writeMem = false; 11067 return true; 11068 } 11069 default: 11070 break; 11071 } 11072 11073 return false; 11074 } 11075