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/CallingConv.h" 27 #include "llvm/Constants.h" 28 #include "llvm/Function.h" 29 #include "llvm/GlobalValue.h" 30 #include "llvm/Instruction.h" 31 #include "llvm/Instructions.h" 32 #include "llvm/Intrinsics.h" 33 #include "llvm/Type.h" 34 #include "llvm/CodeGen/CallingConvLower.h" 35 #include "llvm/CodeGen/IntrinsicLowering.h" 36 #include "llvm/CodeGen/MachineBasicBlock.h" 37 #include "llvm/CodeGen/MachineFrameInfo.h" 38 #include "llvm/CodeGen/MachineFunction.h" 39 #include "llvm/CodeGen/MachineInstrBuilder.h" 40 #include "llvm/CodeGen/MachineModuleInfo.h" 41 #include "llvm/CodeGen/MachineRegisterInfo.h" 42 #include "llvm/CodeGen/SelectionDAG.h" 43 #include "llvm/MC/MCSectionMachO.h" 44 #include "llvm/Target/TargetOptions.h" 45 #include "llvm/ADT/StringExtras.h" 46 #include "llvm/ADT/Statistic.h" 47 #include "llvm/Support/CommandLine.h" 48 #include "llvm/Support/ErrorHandling.h" 49 #include "llvm/Support/MathExtras.h" 50 #include "llvm/Support/raw_ostream.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, SmallVector<CCValAssign, 16> &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(EVT VT, EVT PromotedLdStVT, 94 EVT PromotedBitwiseVT) { 95 if (VT != PromotedLdStVT) { 96 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote); 97 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(), 98 PromotedLdStVT.getSimpleVT()); 99 100 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote); 101 AddPromotedToType (ISD::STORE, VT.getSimpleVT(), 102 PromotedLdStVT.getSimpleVT()); 103 } 104 105 EVT ElemTy = VT.getVectorElementType(); 106 if (ElemTy != MVT::i64 && ElemTy != MVT::f64) 107 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom); 108 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom); 109 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom); 110 if (ElemTy == MVT::i32) { 111 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom); 112 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom); 113 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom); 114 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom); 115 } else { 116 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand); 117 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand); 118 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand); 119 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand); 120 } 121 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom); 122 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom); 123 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal); 124 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal); 125 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand); 126 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand); 127 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand); 128 if (VT.isInteger()) { 129 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom); 130 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom); 131 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom); 132 } 133 134 // Promote all bit-wise operations. 135 if (VT.isInteger() && VT != PromotedBitwiseVT) { 136 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote); 137 AddPromotedToType (ISD::AND, VT.getSimpleVT(), 138 PromotedBitwiseVT.getSimpleVT()); 139 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote); 140 AddPromotedToType (ISD::OR, VT.getSimpleVT(), 141 PromotedBitwiseVT.getSimpleVT()); 142 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote); 143 AddPromotedToType (ISD::XOR, VT.getSimpleVT(), 144 PromotedBitwiseVT.getSimpleVT()); 145 } 146 147 // Neon does not support vector divide/remainder operations. 148 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand); 149 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand); 150 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand); 151 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand); 152 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand); 153 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand); 154 } 155 156 void ARMTargetLowering::addDRTypeForNEON(EVT VT) { 157 addRegisterClass(VT, &ARM::DPRRegClass); 158 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 159 } 160 161 void ARMTargetLowering::addQRTypeForNEON(EVT VT) { 162 addRegisterClass(VT, &ARM::QPRRegClass); 163 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 164 } 165 166 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) { 167 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin()) 168 return new TargetLoweringObjectFileMachO(); 169 170 return new ARMElfTargetObjectFile(); 171 } 172 173 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM) 174 : TargetLowering(TM, createTLOF(TM)) { 175 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 176 RegInfo = TM.getRegisterInfo(); 177 Itins = TM.getInstrItineraryData(); 178 179 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 180 181 if (Subtarget->isTargetDarwin()) { 182 // Uses VFP for Thumb libfuncs if available. 183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) { 184 // Single-precision floating-point arithmetic. 185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp"); 186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp"); 187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp"); 188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp"); 189 190 // Double-precision floating-point arithmetic. 191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp"); 192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp"); 193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp"); 194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp"); 195 196 // Single-precision comparisons. 197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp"); 198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp"); 199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp"); 200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp"); 201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp"); 202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp"); 203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp"); 204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp"); 205 206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); 208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 214 215 // Double-precision comparisons. 216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp"); 217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp"); 218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp"); 219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp"); 220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp"); 221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp"); 222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp"); 223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp"); 224 225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE); 227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 233 234 // Floating-point to integer conversions. 235 // i64 conversions are done via library routines even when generating VFP 236 // instructions, so use the same ones. 237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp"); 238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp"); 239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp"); 240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp"); 241 242 // Conversions between floating types. 243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp"); 244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp"); 245 246 // Integer to floating-point conversions. 247 // i64 conversions are done via library routines even when generating VFP 248 // instructions, so use the same ones. 249 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 250 // e.g., __floatunsidf vs. __floatunssidfvfp. 251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp"); 252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp"); 253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp"); 254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp"); 255 } 256 } 257 258 // These libcalls are not available in 32-bit. 259 setLibcallName(RTLIB::SHL_I128, 0); 260 setLibcallName(RTLIB::SRL_I128, 0); 261 setLibcallName(RTLIB::SRA_I128, 0); 262 263 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) { 264 // Double-precision floating-point arithmetic helper functions 265 // RTABI chapter 4.1.2, Table 2 266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd"); 267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv"); 268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul"); 269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub"); 270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS); 271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS); 272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS); 273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS); 274 275 // Double-precision floating-point comparison helper functions 276 // RTABI chapter 4.1.2, Table 3 277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq"); 278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq"); 280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ); 281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt"); 282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple"); 284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge"); 286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt"); 288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun"); 290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun"); 292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS); 294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS); 295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS); 296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS); 297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS); 298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS); 299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS); 300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS); 301 302 // Single-precision floating-point arithmetic helper functions 303 // RTABI chapter 4.1.2, Table 4 304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd"); 305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv"); 306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul"); 307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub"); 308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS); 309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS); 310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS); 311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS); 312 313 // Single-precision floating-point comparison helper functions 314 // RTABI chapter 4.1.2, Table 5 315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq"); 316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq"); 318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ); 319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt"); 320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple"); 322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge"); 324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt"); 326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun"); 328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun"); 330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS); 332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS); 333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS); 334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS); 335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS); 336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS); 337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS); 338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS); 339 340 // Floating-point to integer conversions. 341 // RTABI chapter 4.1.2, Table 6 342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz"); 343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz"); 344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz"); 345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz"); 346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz"); 347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz"); 348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz"); 349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz"); 350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS); 351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS); 352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS); 353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS); 354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS); 355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS); 356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS); 357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS); 358 359 // Conversions between floating types. 360 // RTABI chapter 4.1.2, Table 7 361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f"); 362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d"); 363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS); 364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS); 365 366 // Integer to floating-point conversions. 367 // RTABI chapter 4.1.2, Table 8 368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d"); 369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d"); 370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d"); 371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d"); 372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f"); 373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f"); 374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f"); 375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f"); 376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS); 377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS); 378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS); 379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS); 380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS); 381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS); 382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS); 383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS); 384 385 // Long long helper functions 386 // RTABI chapter 4.2, Table 9 387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul"); 388 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl"); 389 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr"); 390 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr"); 391 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS); 392 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS); 393 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS); 394 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS); 395 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS); 396 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS); 397 398 // Integer division functions 399 // RTABI chapter 4.3.1 400 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv"); 401 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv"); 402 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv"); 403 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod"); 404 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv"); 405 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv"); 406 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv"); 407 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod"); 408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS); 409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS); 410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS); 411 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS); 412 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS); 413 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS); 414 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS); 415 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS); 416 417 // Memory operations 418 // RTABI chapter 4.3.4 419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy"); 420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove"); 421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset"); 422 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS); 423 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS); 424 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS); 425 } 426 427 // Use divmod compiler-rt calls for iOS 5.0 and later. 428 if (Subtarget->getTargetTriple().getOS() == Triple::IOS && 429 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) { 430 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 431 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 432 } 433 434 if (Subtarget->isThumb1Only()) 435 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 436 else 437 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 438 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 439 !Subtarget->isThumb1Only()) { 440 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 441 if (!Subtarget->isFPOnlySP()) 442 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 443 444 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 445 } 446 447 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 448 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { 449 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 450 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT) 451 setTruncStoreAction((MVT::SimpleValueType)VT, 452 (MVT::SimpleValueType)InnerVT, Expand); 453 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand); 454 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand); 455 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand); 456 } 457 458 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 459 460 if (Subtarget->hasNEON()) { 461 addDRTypeForNEON(MVT::v2f32); 462 addDRTypeForNEON(MVT::v8i8); 463 addDRTypeForNEON(MVT::v4i16); 464 addDRTypeForNEON(MVT::v2i32); 465 addDRTypeForNEON(MVT::v1i64); 466 467 addQRTypeForNEON(MVT::v4f32); 468 addQRTypeForNEON(MVT::v2f64); 469 addQRTypeForNEON(MVT::v16i8); 470 addQRTypeForNEON(MVT::v8i16); 471 addQRTypeForNEON(MVT::v4i32); 472 addQRTypeForNEON(MVT::v2i64); 473 474 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 475 // neither Neon nor VFP support any arithmetic operations on it. 476 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 477 // supported for v4f32. 478 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 479 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 480 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 481 // FIXME: Code duplication: FDIV and FREM are expanded always, see 482 // ARMTargetLowering::addTypeForNEON method for details. 483 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 484 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 485 // FIXME: Create unittest. 486 // In another words, find a way when "copysign" appears in DAG with vector 487 // operands. 488 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 489 // FIXME: Code duplication: SETCC has custom operation action, see 490 // ARMTargetLowering::addTypeForNEON method for details. 491 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 492 // FIXME: Create unittest for FNEG and for FABS. 493 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 494 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 495 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 496 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 497 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 498 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand); 499 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 500 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 501 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 502 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 503 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 504 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 505 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 506 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 507 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 508 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 509 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 510 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 511 512 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 513 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 514 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 515 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand); 516 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 517 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 518 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 519 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 520 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 521 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 522 523 // Neon does not support some operations on v1i64 and v2i64 types. 524 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 525 // Custom handling for some quad-vector types to detect VMULL. 526 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 527 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 528 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 529 // Custom handling for some vector types to avoid expensive expansions 530 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 531 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 532 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 533 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 534 setOperationAction(ISD::SETCC, MVT::v1i64, Expand); 535 setOperationAction(ISD::SETCC, MVT::v2i64, Expand); 536 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 537 // a destination type that is wider than the source, and nor does 538 // it have a FP_TO_[SU]INT instruction with a narrower destination than 539 // source. 540 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 541 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 542 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 543 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 544 545 setTargetDAGCombine(ISD::INTRINSIC_VOID); 546 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 547 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 548 setTargetDAGCombine(ISD::SHL); 549 setTargetDAGCombine(ISD::SRL); 550 setTargetDAGCombine(ISD::SRA); 551 setTargetDAGCombine(ISD::SIGN_EXTEND); 552 setTargetDAGCombine(ISD::ZERO_EXTEND); 553 setTargetDAGCombine(ISD::ANY_EXTEND); 554 setTargetDAGCombine(ISD::SELECT_CC); 555 setTargetDAGCombine(ISD::BUILD_VECTOR); 556 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 557 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 558 setTargetDAGCombine(ISD::STORE); 559 setTargetDAGCombine(ISD::FP_TO_SINT); 560 setTargetDAGCombine(ISD::FP_TO_UINT); 561 setTargetDAGCombine(ISD::FDIV); 562 563 // It is legal to extload from v4i8 to v4i16 or v4i32. 564 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8, 565 MVT::v4i16, MVT::v2i16, 566 MVT::v2i32}; 567 for (unsigned i = 0; i < 6; ++i) { 568 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal); 569 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal); 570 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal); 571 } 572 } 573 574 computeRegisterProperties(); 575 576 // ARM does not have f32 extending load. 577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 578 579 // ARM does not have i1 sign extending load. 580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 581 582 // ARM supports all 4 flavors of integer indexed load / store. 583 if (!Subtarget->isThumb1Only()) { 584 for (unsigned im = (unsigned)ISD::PRE_INC; 585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 586 setIndexedLoadAction(im, MVT::i1, Legal); 587 setIndexedLoadAction(im, MVT::i8, Legal); 588 setIndexedLoadAction(im, MVT::i16, Legal); 589 setIndexedLoadAction(im, MVT::i32, Legal); 590 setIndexedStoreAction(im, MVT::i1, Legal); 591 setIndexedStoreAction(im, MVT::i8, Legal); 592 setIndexedStoreAction(im, MVT::i16, Legal); 593 setIndexedStoreAction(im, MVT::i32, Legal); 594 } 595 } 596 597 // i64 operation support. 598 setOperationAction(ISD::MUL, MVT::i64, Expand); 599 setOperationAction(ISD::MULHU, MVT::i32, Expand); 600 if (Subtarget->isThumb1Only()) { 601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 603 } 604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP())) 606 setOperationAction(ISD::MULHS, MVT::i32, Expand); 607 608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 611 setOperationAction(ISD::SRL, MVT::i64, Custom); 612 setOperationAction(ISD::SRA, MVT::i64, Custom); 613 614 if (!Subtarget->isThumb1Only()) { 615 // FIXME: We should do this for Thumb1 as well. 616 setOperationAction(ISD::ADDC, MVT::i32, Custom); 617 setOperationAction(ISD::ADDE, MVT::i32, Custom); 618 setOperationAction(ISD::SUBC, MVT::i32, Custom); 619 setOperationAction(ISD::SUBE, MVT::i32, Custom); 620 } 621 622 // ARM does not have ROTL. 623 setOperationAction(ISD::ROTL, MVT::i32, Expand); 624 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 625 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 627 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 628 629 // These just redirect to CTTZ and CTLZ on ARM. 630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand); 631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand); 632 633 // Only ARMv6 has BSWAP. 634 if (!Subtarget->hasV6Ops()) 635 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 636 637 // These are expanded into libcalls. 638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) { 639 // v7M has a hardware divider 640 setOperationAction(ISD::SDIV, MVT::i32, Expand); 641 setOperationAction(ISD::UDIV, MVT::i32, Expand); 642 } 643 setOperationAction(ISD::SREM, MVT::i32, Expand); 644 setOperationAction(ISD::UREM, MVT::i32, Expand); 645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 647 648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom); 651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 653 654 setOperationAction(ISD::TRAP, MVT::Other, Legal); 655 656 // Use the default implementation. 657 setOperationAction(ISD::VASTART, MVT::Other, Custom); 658 setOperationAction(ISD::VAARG, MVT::Other, Expand); 659 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 660 setOperationAction(ISD::VAEND, MVT::Other, Expand); 661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 663 664 if (!Subtarget->isTargetDarwin()) { 665 // Non-Darwin platforms may return values in these registers via the 666 // personality function. 667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); 668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); 669 setExceptionPointerRegister(ARM::R0); 670 setExceptionSelectorRegister(ARM::R1); 671 } 672 673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 675 // the default expansion. 676 // FIXME: This should be checking for v6k, not just v6. 677 if (Subtarget->hasDataBarrier() || 678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) { 679 // membarrier needs custom lowering; the rest are legal and handled 680 // normally. 681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom); 682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 683 // Custom lowering for 64-bit ops 684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); 685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); 686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); 687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); 688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); 689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); 690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc. 692 setInsertFencesForAtomic(true); 693 } else { 694 // Set them all for expansion, which will force libcalls. 695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand); 696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand); 697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 710 // Unordered/Monotonic case. 711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 713 // Since the libcalls include locking, fold in the fences 714 setShouldFoldAtomicFences(true); 715 } 716 717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 718 719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 720 if (!Subtarget->hasV6Ops()) { 721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 723 } 724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 725 726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 727 !Subtarget->isThumb1Only()) { 728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 729 // iff target supports vfp2. 730 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 732 } 733 734 // We want to custom lower some of our intrinsics. 735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 736 if (Subtarget->isTargetDarwin()) { 737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 740 } 741 742 setOperationAction(ISD::SETCC, MVT::i32, Expand); 743 setOperationAction(ISD::SETCC, MVT::f32, Expand); 744 setOperationAction(ISD::SETCC, MVT::f64, Expand); 745 setOperationAction(ISD::SELECT, MVT::i32, Custom); 746 setOperationAction(ISD::SELECT, MVT::f32, Custom); 747 setOperationAction(ISD::SELECT, MVT::f64, Custom); 748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 751 752 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 753 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 754 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 755 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 756 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 757 758 // We don't support sin/cos/fmod/copysign/pow 759 setOperationAction(ISD::FSIN, MVT::f64, Expand); 760 setOperationAction(ISD::FSIN, MVT::f32, Expand); 761 setOperationAction(ISD::FCOS, MVT::f32, Expand); 762 setOperationAction(ISD::FCOS, MVT::f64, Expand); 763 setOperationAction(ISD::FREM, MVT::f64, Expand); 764 setOperationAction(ISD::FREM, MVT::f32, Expand); 765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 766 !Subtarget->isThumb1Only()) { 767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 769 } 770 setOperationAction(ISD::FPOW, MVT::f64, Expand); 771 setOperationAction(ISD::FPOW, MVT::f32, Expand); 772 773 if (!Subtarget->hasVFP4()) { 774 setOperationAction(ISD::FMA, MVT::f64, Expand); 775 setOperationAction(ISD::FMA, MVT::f32, Expand); 776 } 777 778 // Various VFP goodness 779 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) { 780 // int <-> fp are custom expanded into bit_convert + ARMISD ops. 781 if (Subtarget->hasVFP2()) { 782 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 783 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 784 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 785 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 786 } 787 // Special handling for half-precision FP. 788 if (!Subtarget->hasFP16()) { 789 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand); 790 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand); 791 } 792 } 793 794 // We have target-specific dag combine patterns for the following nodes: 795 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 796 setTargetDAGCombine(ISD::ADD); 797 setTargetDAGCombine(ISD::SUB); 798 setTargetDAGCombine(ISD::MUL); 799 800 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) { 801 setTargetDAGCombine(ISD::AND); 802 setTargetDAGCombine(ISD::OR); 803 setTargetDAGCombine(ISD::XOR); 804 } 805 806 if (Subtarget->hasV6Ops()) 807 setTargetDAGCombine(ISD::SRL); 808 809 setStackPointerRegisterToSaveRestore(ARM::SP); 810 811 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() || 812 !Subtarget->hasVFP2()) 813 setSchedulingPreference(Sched::RegPressure); 814 else 815 setSchedulingPreference(Sched::Hybrid); 816 817 //// temporary - rewrite interface to use type 818 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1; 819 maxStoresPerMemset = 16; 820 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4; 821 822 // On ARM arguments smaller than 4 bytes are extended, so all arguments 823 // are at least 4 bytes aligned. 824 setMinStackArgumentAlignment(4); 825 826 benefitFromCodePlacementOpt = true; 827 828 // Prefer likely predicted branches to selects on out-of-order cores. 829 predictableSelectIsExpensive = Subtarget->isCortexA9(); 830 831 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 832 } 833 834 // FIXME: It might make sense to define the representative register class as the 835 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 836 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 837 // SPR's representative would be DPR_VFP2. This should work well if register 838 // pressure tracking were modified such that a register use would increment the 839 // pressure of the register class's representative and all of it's super 840 // classes' representatives transitively. We have not implemented this because 841 // of the difficulty prior to coalescing of modeling operand register classes 842 // due to the common occurrence of cross class copies and subregister insertions 843 // and extractions. 844 std::pair<const TargetRegisterClass*, uint8_t> 845 ARMTargetLowering::findRepresentativeClass(EVT VT) const{ 846 const TargetRegisterClass *RRC = 0; 847 uint8_t Cost = 1; 848 switch (VT.getSimpleVT().SimpleTy) { 849 default: 850 return TargetLowering::findRepresentativeClass(VT); 851 // Use DPR as representative register class for all floating point 852 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 853 // the cost is 1 for both f32 and f64. 854 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 855 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 856 RRC = &ARM::DPRRegClass; 857 // When NEON is used for SP, only half of the register file is available 858 // because operations that define both SP and DP results will be constrained 859 // to the VFP2 class (D0-D15). We currently model this constraint prior to 860 // coalescing by double-counting the SP regs. See the FIXME above. 861 if (Subtarget->useNEONForSinglePrecisionFP()) 862 Cost = 2; 863 break; 864 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 865 case MVT::v4f32: case MVT::v2f64: 866 RRC = &ARM::DPRRegClass; 867 Cost = 2; 868 break; 869 case MVT::v4i64: 870 RRC = &ARM::DPRRegClass; 871 Cost = 4; 872 break; 873 case MVT::v8i64: 874 RRC = &ARM::DPRRegClass; 875 Cost = 8; 876 break; 877 } 878 return std::make_pair(RRC, Cost); 879 } 880 881 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 882 switch (Opcode) { 883 default: return 0; 884 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 885 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN"; 886 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 887 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 888 case ARMISD::CALL: return "ARMISD::CALL"; 889 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 890 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 891 case ARMISD::tCALL: return "ARMISD::tCALL"; 892 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 893 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 894 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 895 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 896 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 897 case ARMISD::CMP: return "ARMISD::CMP"; 898 case ARMISD::CMN: return "ARMISD::CMN"; 899 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 900 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 901 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 902 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 903 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 904 905 case ARMISD::CMOV: return "ARMISD::CMOV"; 906 case ARMISD::CAND: return "ARMISD::CAND"; 907 case ARMISD::COR: return "ARMISD::COR"; 908 case ARMISD::CXOR: return "ARMISD::CXOR"; 909 910 case ARMISD::RBIT: return "ARMISD::RBIT"; 911 912 case ARMISD::FTOSI: return "ARMISD::FTOSI"; 913 case ARMISD::FTOUI: return "ARMISD::FTOUI"; 914 case ARMISD::SITOF: return "ARMISD::SITOF"; 915 case ARMISD::UITOF: return "ARMISD::UITOF"; 916 917 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 918 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 919 case ARMISD::RRX: return "ARMISD::RRX"; 920 921 case ARMISD::ADDC: return "ARMISD::ADDC"; 922 case ARMISD::ADDE: return "ARMISD::ADDE"; 923 case ARMISD::SUBC: return "ARMISD::SUBC"; 924 case ARMISD::SUBE: return "ARMISD::SUBE"; 925 926 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 927 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 928 929 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 930 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP"; 931 932 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 933 934 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 935 936 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 937 938 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER"; 939 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 940 941 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 942 943 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 944 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 945 case ARMISD::VCGE: return "ARMISD::VCGE"; 946 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 947 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 948 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 949 case ARMISD::VCGT: return "ARMISD::VCGT"; 950 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 951 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 952 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 953 case ARMISD::VTST: return "ARMISD::VTST"; 954 955 case ARMISD::VSHL: return "ARMISD::VSHL"; 956 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 957 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 958 case ARMISD::VSHLLs: return "ARMISD::VSHLLs"; 959 case ARMISD::VSHLLu: return "ARMISD::VSHLLu"; 960 case ARMISD::VSHLLi: return "ARMISD::VSHLLi"; 961 case ARMISD::VSHRN: return "ARMISD::VSHRN"; 962 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 963 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 964 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 965 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 966 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 967 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 968 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 969 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 970 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 971 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 972 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 973 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 974 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 975 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 976 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 977 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 978 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 979 case ARMISD::VDUP: return "ARMISD::VDUP"; 980 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 981 case ARMISD::VEXT: return "ARMISD::VEXT"; 982 case ARMISD::VREV64: return "ARMISD::VREV64"; 983 case ARMISD::VREV32: return "ARMISD::VREV32"; 984 case ARMISD::VREV16: return "ARMISD::VREV16"; 985 case ARMISD::VZIP: return "ARMISD::VZIP"; 986 case ARMISD::VUZP: return "ARMISD::VUZP"; 987 case ARMISD::VTRN: return "ARMISD::VTRN"; 988 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 989 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 990 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 991 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 992 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 993 case ARMISD::FMAX: return "ARMISD::FMAX"; 994 case ARMISD::FMIN: return "ARMISD::FMIN"; 995 case ARMISD::BFI: return "ARMISD::BFI"; 996 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 997 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 998 case ARMISD::VBSL: return "ARMISD::VBSL"; 999 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1000 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1001 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1002 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1003 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1004 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1005 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1006 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1007 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1008 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1009 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1010 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1011 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1012 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1013 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1014 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1015 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1016 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1017 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1018 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1019 } 1020 } 1021 1022 EVT ARMTargetLowering::getSetCCResultType(EVT VT) const { 1023 if (!VT.isVector()) return getPointerTy(); 1024 return VT.changeVectorElementTypeToInteger(); 1025 } 1026 1027 /// getRegClassFor - Return the register class that should be used for the 1028 /// specified value type. 1029 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const { 1030 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1031 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1032 // load / store 4 to 8 consecutive D registers. 1033 if (Subtarget->hasNEON()) { 1034 if (VT == MVT::v4i64) 1035 return &ARM::QQPRRegClass; 1036 if (VT == MVT::v8i64) 1037 return &ARM::QQQQPRRegClass; 1038 } 1039 return TargetLowering::getRegClassFor(VT); 1040 } 1041 1042 // Create a fast isel object. 1043 FastISel * 1044 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const { 1045 return ARM::createFastISel(funcInfo); 1046 } 1047 1048 /// getMaximalGlobalOffset - Returns the maximal possible offset which can 1049 /// be used for loads / stores from the global. 1050 unsigned ARMTargetLowering::getMaximalGlobalOffset() const { 1051 return (Subtarget->isThumb1Only() ? 127 : 4095); 1052 } 1053 1054 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1055 unsigned NumVals = N->getNumValues(); 1056 if (!NumVals) 1057 return Sched::RegPressure; 1058 1059 for (unsigned i = 0; i != NumVals; ++i) { 1060 EVT VT = N->getValueType(i); 1061 if (VT == MVT::Glue || VT == MVT::Other) 1062 continue; 1063 if (VT.isFloatingPoint() || VT.isVector()) 1064 return Sched::ILP; 1065 } 1066 1067 if (!N->isMachineOpcode()) 1068 return Sched::RegPressure; 1069 1070 // Load are scheduled for latency even if there instruction itinerary 1071 // is not available. 1072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1073 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1074 1075 if (MCID.getNumDefs() == 0) 1076 return Sched::RegPressure; 1077 if (!Itins->isEmpty() && 1078 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1079 return Sched::ILP; 1080 1081 return Sched::RegPressure; 1082 } 1083 1084 //===----------------------------------------------------------------------===// 1085 // Lowering Code 1086 //===----------------------------------------------------------------------===// 1087 1088 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1089 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1090 switch (CC) { 1091 default: llvm_unreachable("Unknown condition code!"); 1092 case ISD::SETNE: return ARMCC::NE; 1093 case ISD::SETEQ: return ARMCC::EQ; 1094 case ISD::SETGT: return ARMCC::GT; 1095 case ISD::SETGE: return ARMCC::GE; 1096 case ISD::SETLT: return ARMCC::LT; 1097 case ISD::SETLE: return ARMCC::LE; 1098 case ISD::SETUGT: return ARMCC::HI; 1099 case ISD::SETUGE: return ARMCC::HS; 1100 case ISD::SETULT: return ARMCC::LO; 1101 case ISD::SETULE: return ARMCC::LS; 1102 } 1103 } 1104 1105 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1106 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1107 ARMCC::CondCodes &CondCode2) { 1108 CondCode2 = ARMCC::AL; 1109 switch (CC) { 1110 default: llvm_unreachable("Unknown FP condition!"); 1111 case ISD::SETEQ: 1112 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1113 case ISD::SETGT: 1114 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1115 case ISD::SETGE: 1116 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1117 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1118 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1119 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1120 case ISD::SETO: CondCode = ARMCC::VC; break; 1121 case ISD::SETUO: CondCode = ARMCC::VS; break; 1122 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1123 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1124 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1125 case ISD::SETLT: 1126 case ISD::SETULT: CondCode = ARMCC::LT; break; 1127 case ISD::SETLE: 1128 case ISD::SETULE: CondCode = ARMCC::LE; break; 1129 case ISD::SETNE: 1130 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1131 } 1132 } 1133 1134 //===----------------------------------------------------------------------===// 1135 // Calling Convention Implementation 1136 //===----------------------------------------------------------------------===// 1137 1138 #include "ARMGenCallingConv.inc" 1139 1140 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the 1141 /// given CallingConvention value. 1142 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1143 bool Return, 1144 bool isVarArg) const { 1145 switch (CC) { 1146 default: 1147 llvm_unreachable("Unsupported calling convention"); 1148 case CallingConv::Fast: 1149 if (Subtarget->hasVFP2() && !isVarArg) { 1150 if (!Subtarget->isAAPCS_ABI()) 1151 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1152 // For AAPCS ABI targets, just use VFP variant of the calling convention. 1153 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1154 } 1155 // Fallthrough 1156 case CallingConv::C: { 1157 // Use target triple & subtarget features to do actual dispatch. 1158 if (!Subtarget->isAAPCS_ABI()) 1159 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1160 else if (Subtarget->hasVFP2() && 1161 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1162 !isVarArg) 1163 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1164 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1165 } 1166 case CallingConv::ARM_AAPCS_VFP: 1167 if (!isVarArg) 1168 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1169 // Fallthrough 1170 case CallingConv::ARM_AAPCS: 1171 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1172 case CallingConv::ARM_APCS: 1173 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1174 } 1175 } 1176 1177 /// LowerCallResult - Lower the result values of a call into the 1178 /// appropriate copies out of appropriate physical registers. 1179 SDValue 1180 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1181 CallingConv::ID CallConv, bool isVarArg, 1182 const SmallVectorImpl<ISD::InputArg> &Ins, 1183 DebugLoc dl, SelectionDAG &DAG, 1184 SmallVectorImpl<SDValue> &InVals) const { 1185 1186 // Assign locations to each value returned by this call. 1187 SmallVector<CCValAssign, 16> RVLocs; 1188 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1189 getTargetMachine(), RVLocs, *DAG.getContext(), Call); 1190 CCInfo.AnalyzeCallResult(Ins, 1191 CCAssignFnForNode(CallConv, /* Return*/ true, 1192 isVarArg)); 1193 1194 // Copy all of the result registers out of their specified physreg. 1195 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1196 CCValAssign VA = RVLocs[i]; 1197 1198 SDValue Val; 1199 if (VA.needsCustom()) { 1200 // Handle f64 or half of a v2f64. 1201 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1202 InFlag); 1203 Chain = Lo.getValue(1); 1204 InFlag = Lo.getValue(2); 1205 VA = RVLocs[++i]; // skip ahead to next loc 1206 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1207 InFlag); 1208 Chain = Hi.getValue(1); 1209 InFlag = Hi.getValue(2); 1210 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1211 1212 if (VA.getLocVT() == MVT::v2f64) { 1213 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1214 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1215 DAG.getConstant(0, MVT::i32)); 1216 1217 VA = RVLocs[++i]; // skip ahead to next loc 1218 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1219 Chain = Lo.getValue(1); 1220 InFlag = Lo.getValue(2); 1221 VA = RVLocs[++i]; // skip ahead to next loc 1222 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1223 Chain = Hi.getValue(1); 1224 InFlag = Hi.getValue(2); 1225 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1226 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1227 DAG.getConstant(1, MVT::i32)); 1228 } 1229 } else { 1230 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1231 InFlag); 1232 Chain = Val.getValue(1); 1233 InFlag = Val.getValue(2); 1234 } 1235 1236 switch (VA.getLocInfo()) { 1237 default: llvm_unreachable("Unknown loc info!"); 1238 case CCValAssign::Full: break; 1239 case CCValAssign::BCvt: 1240 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1241 break; 1242 } 1243 1244 InVals.push_back(Val); 1245 } 1246 1247 return Chain; 1248 } 1249 1250 /// LowerMemOpCallTo - Store the argument to the stack. 1251 SDValue 1252 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, 1253 SDValue StackPtr, SDValue Arg, 1254 DebugLoc dl, SelectionDAG &DAG, 1255 const CCValAssign &VA, 1256 ISD::ArgFlagsTy Flags) const { 1257 unsigned LocMemOffset = VA.getLocMemOffset(); 1258 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); 1259 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1260 return DAG.getStore(Chain, dl, Arg, PtrOff, 1261 MachinePointerInfo::getStack(LocMemOffset), 1262 false, false, 0); 1263 } 1264 1265 void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG, 1266 SDValue Chain, SDValue &Arg, 1267 RegsToPassVector &RegsToPass, 1268 CCValAssign &VA, CCValAssign &NextVA, 1269 SDValue &StackPtr, 1270 SmallVector<SDValue, 8> &MemOpChains, 1271 ISD::ArgFlagsTy Flags) const { 1272 1273 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1274 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1275 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd)); 1276 1277 if (NextVA.isRegLoc()) 1278 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1))); 1279 else { 1280 assert(NextVA.isMemLoc()); 1281 if (StackPtr.getNode() == 0) 1282 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1283 1284 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1), 1285 dl, DAG, NextVA, 1286 Flags)); 1287 } 1288 } 1289 1290 /// LowerCall - Lowering a call into a callseq_start <- 1291 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1292 /// nodes. 1293 SDValue 1294 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1295 SmallVectorImpl<SDValue> &InVals) const { 1296 SelectionDAG &DAG = CLI.DAG; 1297 DebugLoc &dl = CLI.DL; 1298 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; 1299 SmallVector<SDValue, 32> &OutVals = CLI.OutVals; 1300 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; 1301 SDValue Chain = CLI.Chain; 1302 SDValue Callee = CLI.Callee; 1303 bool &isTailCall = CLI.IsTailCall; 1304 CallingConv::ID CallConv = CLI.CallConv; 1305 bool doesNotRet = CLI.DoesNotReturn; 1306 bool isVarArg = CLI.IsVarArg; 1307 1308 MachineFunction &MF = DAG.getMachineFunction(); 1309 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1310 bool IsSibCall = false; 1311 // Disable tail calls if they're not supported. 1312 if (!EnableARMTailCalls && !Subtarget->supportsTailCall()) 1313 isTailCall = false; 1314 if (isTailCall) { 1315 // Check if it's really possible to do a tail call. 1316 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1317 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(), 1318 Outs, OutVals, Ins, DAG); 1319 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1320 // detected sibcalls. 1321 if (isTailCall) { 1322 ++NumTailCalls; 1323 IsSibCall = true; 1324 } 1325 } 1326 1327 // Analyze operands of the call, assigning locations to each operand. 1328 SmallVector<CCValAssign, 16> ArgLocs; 1329 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1330 getTargetMachine(), ArgLocs, *DAG.getContext(), Call); 1331 CCInfo.AnalyzeCallOperands(Outs, 1332 CCAssignFnForNode(CallConv, /* Return*/ false, 1333 isVarArg)); 1334 1335 // Get a count of how many bytes are to be pushed on the stack. 1336 unsigned NumBytes = CCInfo.getNextStackOffset(); 1337 1338 // For tail calls, memory operands are available in our caller's stack. 1339 if (IsSibCall) 1340 NumBytes = 0; 1341 1342 // Adjust the stack pointer for the new arguments... 1343 // These operations are automatically eliminated by the prolog/epilog pass 1344 if (!IsSibCall) 1345 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); 1346 1347 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1348 1349 RegsToPassVector RegsToPass; 1350 SmallVector<SDValue, 8> MemOpChains; 1351 1352 // Walk the register/memloc assignments, inserting copies/loads. In the case 1353 // of tail call optimization, arguments are handled later. 1354 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1355 i != e; 1356 ++i, ++realArgIdx) { 1357 CCValAssign &VA = ArgLocs[i]; 1358 SDValue Arg = OutVals[realArgIdx]; 1359 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1360 bool isByVal = Flags.isByVal(); 1361 1362 // Promote the value if needed. 1363 switch (VA.getLocInfo()) { 1364 default: llvm_unreachable("Unknown loc info!"); 1365 case CCValAssign::Full: break; 1366 case CCValAssign::SExt: 1367 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1368 break; 1369 case CCValAssign::ZExt: 1370 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1371 break; 1372 case CCValAssign::AExt: 1373 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1374 break; 1375 case CCValAssign::BCvt: 1376 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1377 break; 1378 } 1379 1380 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1381 if (VA.needsCustom()) { 1382 if (VA.getLocVT() == MVT::v2f64) { 1383 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1384 DAG.getConstant(0, MVT::i32)); 1385 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1386 DAG.getConstant(1, MVT::i32)); 1387 1388 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1389 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1390 1391 VA = ArgLocs[++i]; // skip ahead to next loc 1392 if (VA.isRegLoc()) { 1393 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1394 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1395 } else { 1396 assert(VA.isMemLoc()); 1397 1398 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1399 dl, DAG, VA, Flags)); 1400 } 1401 } else { 1402 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1403 StackPtr, MemOpChains, Flags); 1404 } 1405 } else if (VA.isRegLoc()) { 1406 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1407 } else if (isByVal) { 1408 assert(VA.isMemLoc()); 1409 unsigned offset = 0; 1410 1411 // True if this byval aggregate will be split between registers 1412 // and memory. 1413 if (CCInfo.isFirstByValRegValid()) { 1414 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 1415 unsigned int i, j; 1416 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) { 1417 SDValue Const = DAG.getConstant(4*i, MVT::i32); 1418 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1419 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1420 MachinePointerInfo(), 1421 false, false, false, 0); 1422 MemOpChains.push_back(Load.getValue(1)); 1423 RegsToPass.push_back(std::make_pair(j, Load)); 1424 } 1425 offset = ARM::R4 - CCInfo.getFirstByValReg(); 1426 CCInfo.clearFirstByValReg(); 1427 } 1428 1429 if (Flags.getByValSize() - 4*offset > 0) { 1430 unsigned LocMemOffset = VA.getLocMemOffset(); 1431 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset); 1432 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, 1433 StkPtrOff); 1434 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset); 1435 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset); 1436 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, 1437 MVT::i32); 1438 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32); 1439 1440 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 1441 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 1442 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 1443 Ops, array_lengthof(Ops))); 1444 } 1445 } else if (!IsSibCall) { 1446 assert(VA.isMemLoc()); 1447 1448 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1449 dl, DAG, VA, Flags)); 1450 } 1451 } 1452 1453 if (!MemOpChains.empty()) 1454 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1455 &MemOpChains[0], MemOpChains.size()); 1456 1457 // Build a sequence of copy-to-reg nodes chained together with token chain 1458 // and flag operands which copy the outgoing args into the appropriate regs. 1459 SDValue InFlag; 1460 // Tail call byval lowering might overwrite argument registers so in case of 1461 // tail call optimization the copies to registers are lowered later. 1462 if (!isTailCall) 1463 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1464 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1465 RegsToPass[i].second, InFlag); 1466 InFlag = Chain.getValue(1); 1467 } 1468 1469 // For tail calls lower the arguments to the 'real' stack slot. 1470 if (isTailCall) { 1471 // Force all the incoming stack arguments to be loaded from the stack 1472 // before any new outgoing arguments are stored to the stack, because the 1473 // outgoing stack slots may alias the incoming argument stack slots, and 1474 // the alias isn't otherwise explicit. This is slightly more conservative 1475 // than necessary, because it means that each store effectively depends 1476 // on every argument instead of just those arguments it would clobber. 1477 1478 // Do not flag preceding copytoreg stuff together with the following stuff. 1479 InFlag = SDValue(); 1480 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1481 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1482 RegsToPass[i].second, InFlag); 1483 InFlag = Chain.getValue(1); 1484 } 1485 InFlag =SDValue(); 1486 } 1487 1488 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1489 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1490 // node so that legalize doesn't hack it. 1491 bool isDirect = false; 1492 bool isARMFunc = false; 1493 bool isLocalARMFunc = false; 1494 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1495 1496 if (EnableARMLongCalls) { 1497 assert (getTargetMachine().getRelocationModel() == Reloc::Static 1498 && "long-calls with non-static relocation model!"); 1499 // Handle a global address or an external symbol. If it's not one of 1500 // those, the target's already in a register, so we don't need to do 1501 // anything extra. 1502 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1503 const GlobalValue *GV = G->getGlobal(); 1504 // Create a constant pool entry for the callee address 1505 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1506 ARMConstantPoolValue *CPV = 1507 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 1508 1509 // Get the address of the callee into a register 1510 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1511 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1512 Callee = DAG.getLoad(getPointerTy(), dl, 1513 DAG.getEntryNode(), CPAddr, 1514 MachinePointerInfo::getConstantPool(), 1515 false, false, false, 0); 1516 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 1517 const char *Sym = S->getSymbol(); 1518 1519 // Create a constant pool entry for the callee address 1520 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1521 ARMConstantPoolValue *CPV = 1522 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1523 ARMPCLabelIndex, 0); 1524 // Get the address of the callee into a register 1525 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1526 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1527 Callee = DAG.getLoad(getPointerTy(), dl, 1528 DAG.getEntryNode(), CPAddr, 1529 MachinePointerInfo::getConstantPool(), 1530 false, false, false, 0); 1531 } 1532 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1533 const GlobalValue *GV = G->getGlobal(); 1534 isDirect = true; 1535 bool isExt = GV->isDeclaration() || GV->isWeakForLinker(); 1536 bool isStub = (isExt && Subtarget->isTargetDarwin()) && 1537 getTargetMachine().getRelocationModel() != Reloc::Static; 1538 isARMFunc = !Subtarget->isThumb() || isStub; 1539 // ARM call to a local ARM function is predicable. 1540 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking); 1541 // tBX takes a register source operand. 1542 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1543 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1544 ARMConstantPoolValue *CPV = 1545 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4); 1546 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1547 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1548 Callee = DAG.getLoad(getPointerTy(), dl, 1549 DAG.getEntryNode(), CPAddr, 1550 MachinePointerInfo::getConstantPool(), 1551 false, false, false, 0); 1552 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1553 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1554 getPointerTy(), Callee, PICLabel); 1555 } else { 1556 // On ELF targets for PIC code, direct calls should go through the PLT 1557 unsigned OpFlags = 0; 1558 if (Subtarget->isTargetELF() && 1559 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1560 OpFlags = ARMII::MO_PLT; 1561 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags); 1562 } 1563 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1564 isDirect = true; 1565 bool isStub = Subtarget->isTargetDarwin() && 1566 getTargetMachine().getRelocationModel() != Reloc::Static; 1567 isARMFunc = !Subtarget->isThumb() || isStub; 1568 // tBX takes a register source operand. 1569 const char *Sym = S->getSymbol(); 1570 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1571 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1572 ARMConstantPoolValue *CPV = 1573 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1574 ARMPCLabelIndex, 4); 1575 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1577 Callee = DAG.getLoad(getPointerTy(), dl, 1578 DAG.getEntryNode(), CPAddr, 1579 MachinePointerInfo::getConstantPool(), 1580 false, false, false, 0); 1581 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1582 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1583 getPointerTy(), Callee, PICLabel); 1584 } else { 1585 unsigned OpFlags = 0; 1586 // On ELF targets for PIC code, direct calls should go through the PLT 1587 if (Subtarget->isTargetELF() && 1588 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1589 OpFlags = ARMII::MO_PLT; 1590 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags); 1591 } 1592 } 1593 1594 // FIXME: handle tail calls differently. 1595 unsigned CallOpc; 1596 if (Subtarget->isThumb()) { 1597 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 1598 CallOpc = ARMISD::CALL_NOLINK; 1599 else if (doesNotRet && isDirect && !isARMFunc && 1600 Subtarget->hasRAS() && !Subtarget->isThumb1Only()) 1601 // "mov lr, pc; b _foo" to avoid confusing the RSP 1602 CallOpc = ARMISD::CALL_NOLINK; 1603 else 1604 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; 1605 } else { 1606 if (!isDirect && !Subtarget->hasV5TOps()) { 1607 CallOpc = ARMISD::CALL_NOLINK; 1608 } else if (doesNotRet && isDirect && Subtarget->hasRAS()) 1609 // "mov lr, pc; b _foo" to avoid confusing the RSP 1610 CallOpc = ARMISD::CALL_NOLINK; 1611 else 1612 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 1613 } 1614 1615 std::vector<SDValue> Ops; 1616 Ops.push_back(Chain); 1617 Ops.push_back(Callee); 1618 1619 // Add argument registers to the end of the list so that they are known live 1620 // into the call. 1621 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1622 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1623 RegsToPass[i].second.getValueType())); 1624 1625 // Add a register mask operand representing the call-preserved registers. 1626 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 1627 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv); 1628 assert(Mask && "Missing call preserved mask for calling convention"); 1629 Ops.push_back(DAG.getRegisterMask(Mask)); 1630 1631 if (InFlag.getNode()) 1632 Ops.push_back(InFlag); 1633 1634 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1635 if (isTailCall) 1636 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size()); 1637 1638 // Returns a chain and a flag for retval copy to use. 1639 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size()); 1640 InFlag = Chain.getValue(1); 1641 1642 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1643 DAG.getIntPtrConstant(0, true), InFlag); 1644 if (!Ins.empty()) 1645 InFlag = Chain.getValue(1); 1646 1647 // Handle result values, copying them out of physregs into vregs that we 1648 // return. 1649 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, 1650 dl, DAG, InVals); 1651 } 1652 1653 /// HandleByVal - Every parameter *after* a byval parameter is passed 1654 /// on the stack. Remember the next parameter register to allocate, 1655 /// and then confiscate the rest of the parameter registers to insure 1656 /// this. 1657 void 1658 ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const { 1659 unsigned reg = State->AllocateReg(GPRArgRegs, 4); 1660 assert((State->getCallOrPrologue() == Prologue || 1661 State->getCallOrPrologue() == Call) && 1662 "unhandled ParmContext"); 1663 if ((!State->isFirstByValRegValid()) && 1664 (ARM::R0 <= reg) && (reg <= ARM::R3)) { 1665 State->setFirstByValReg(reg); 1666 // At a call site, a byval parameter that is split between 1667 // registers and memory needs its size truncated here. In a 1668 // function prologue, such byval parameters are reassembled in 1669 // memory, and are not truncated. 1670 if (State->getCallOrPrologue() == Call) { 1671 unsigned excess = 4 * (ARM::R4 - reg); 1672 assert(size >= excess && "expected larger existing stack allocation"); 1673 size -= excess; 1674 } 1675 } 1676 // Confiscate any remaining parameter registers to preclude their 1677 // assignment to subsequent parameters. 1678 while (State->AllocateReg(GPRArgRegs, 4)) 1679 ; 1680 } 1681 1682 /// MatchingStackOffset - Return true if the given stack call argument is 1683 /// already available in the same position (relatively) of the caller's 1684 /// incoming argument stack. 1685 static 1686 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 1687 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, 1688 const TargetInstrInfo *TII) { 1689 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; 1690 int FI = INT_MAX; 1691 if (Arg.getOpcode() == ISD::CopyFromReg) { 1692 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 1693 if (!TargetRegisterInfo::isVirtualRegister(VR)) 1694 return false; 1695 MachineInstr *Def = MRI->getVRegDef(VR); 1696 if (!Def) 1697 return false; 1698 if (!Flags.isByVal()) { 1699 if (!TII->isLoadFromStackSlot(Def, FI)) 1700 return false; 1701 } else { 1702 return false; 1703 } 1704 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 1705 if (Flags.isByVal()) 1706 // ByVal argument is passed in as a pointer but it's now being 1707 // dereferenced. e.g. 1708 // define @foo(%struct.X* %A) { 1709 // tail call @bar(%struct.X* byval %A) 1710 // } 1711 return false; 1712 SDValue Ptr = Ld->getBasePtr(); 1713 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 1714 if (!FINode) 1715 return false; 1716 FI = FINode->getIndex(); 1717 } else 1718 return false; 1719 1720 assert(FI != INT_MAX); 1721 if (!MFI->isFixedObjectIndex(FI)) 1722 return false; 1723 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); 1724 } 1725 1726 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 1727 /// for tail call optimization. Targets which want to do tail call 1728 /// optimization should implement this function. 1729 bool 1730 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 1731 CallingConv::ID CalleeCC, 1732 bool isVarArg, 1733 bool isCalleeStructRet, 1734 bool isCallerStructRet, 1735 const SmallVectorImpl<ISD::OutputArg> &Outs, 1736 const SmallVectorImpl<SDValue> &OutVals, 1737 const SmallVectorImpl<ISD::InputArg> &Ins, 1738 SelectionDAG& DAG) const { 1739 const Function *CallerF = DAG.getMachineFunction().getFunction(); 1740 CallingConv::ID CallerCC = CallerF->getCallingConv(); 1741 bool CCMatch = CallerCC == CalleeCC; 1742 1743 // Look for obvious safe cases to perform tail call optimization that do not 1744 // require ABI changes. This is what gcc calls sibcall. 1745 1746 // Do not sibcall optimize vararg calls unless the call site is not passing 1747 // any arguments. 1748 if (isVarArg && !Outs.empty()) 1749 return false; 1750 1751 // Also avoid sibcall optimization if either caller or callee uses struct 1752 // return semantics. 1753 if (isCalleeStructRet || isCallerStructRet) 1754 return false; 1755 1756 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo:: 1757 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as 1758 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation 1759 // support in the assembler and linker to be used. This would need to be 1760 // fixed to fully support tail calls in Thumb1. 1761 // 1762 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take 1763 // LR. This means if we need to reload LR, it takes an extra instructions, 1764 // which outweighs the value of the tail call; but here we don't know yet 1765 // whether LR is going to be used. Probably the right approach is to 1766 // generate the tail call here and turn it back into CALL/RET in 1767 // emitEpilogue if LR is used. 1768 1769 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls, 1770 // but we need to make sure there are enough registers; the only valid 1771 // registers are the 4 used for parameters. We don't currently do this 1772 // case. 1773 if (Subtarget->isThumb1Only()) 1774 return false; 1775 1776 // If the calling conventions do not match, then we'd better make sure the 1777 // results are returned in the same way as what the caller expects. 1778 if (!CCMatch) { 1779 SmallVector<CCValAssign, 16> RVLocs1; 1780 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), 1781 getTargetMachine(), RVLocs1, *DAG.getContext(), Call); 1782 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg)); 1783 1784 SmallVector<CCValAssign, 16> RVLocs2; 1785 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), 1786 getTargetMachine(), RVLocs2, *DAG.getContext(), Call); 1787 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg)); 1788 1789 if (RVLocs1.size() != RVLocs2.size()) 1790 return false; 1791 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 1792 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 1793 return false; 1794 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 1795 return false; 1796 if (RVLocs1[i].isRegLoc()) { 1797 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 1798 return false; 1799 } else { 1800 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 1801 return false; 1802 } 1803 } 1804 } 1805 1806 // If the callee takes no arguments then go on to check the results of the 1807 // call. 1808 if (!Outs.empty()) { 1809 // Check if stack adjustment is needed. For now, do not do this if any 1810 // argument is passed on the stack. 1811 SmallVector<CCValAssign, 16> ArgLocs; 1812 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), 1813 getTargetMachine(), ArgLocs, *DAG.getContext(), Call); 1814 CCInfo.AnalyzeCallOperands(Outs, 1815 CCAssignFnForNode(CalleeCC, false, isVarArg)); 1816 if (CCInfo.getNextStackOffset()) { 1817 MachineFunction &MF = DAG.getMachineFunction(); 1818 1819 // Check if the arguments are already laid out in the right way as 1820 // the caller's fixed stack objects. 1821 MachineFrameInfo *MFI = MF.getFrameInfo(); 1822 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 1823 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1824 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1825 i != e; 1826 ++i, ++realArgIdx) { 1827 CCValAssign &VA = ArgLocs[i]; 1828 EVT RegVT = VA.getLocVT(); 1829 SDValue Arg = OutVals[realArgIdx]; 1830 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1831 if (VA.getLocInfo() == CCValAssign::Indirect) 1832 return false; 1833 if (VA.needsCustom()) { 1834 // f64 and vector types are split into multiple registers or 1835 // register/stack-slot combinations. The types will not match 1836 // the registers; give up on memory f64 refs until we figure 1837 // out what to do about this. 1838 if (!VA.isRegLoc()) 1839 return false; 1840 if (!ArgLocs[++i].isRegLoc()) 1841 return false; 1842 if (RegVT == MVT::v2f64) { 1843 if (!ArgLocs[++i].isRegLoc()) 1844 return false; 1845 if (!ArgLocs[++i].isRegLoc()) 1846 return false; 1847 } 1848 } else if (!VA.isRegLoc()) { 1849 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 1850 MFI, MRI, TII)) 1851 return false; 1852 } 1853 } 1854 } 1855 } 1856 1857 return true; 1858 } 1859 1860 SDValue 1861 ARMTargetLowering::LowerReturn(SDValue Chain, 1862 CallingConv::ID CallConv, bool isVarArg, 1863 const SmallVectorImpl<ISD::OutputArg> &Outs, 1864 const SmallVectorImpl<SDValue> &OutVals, 1865 DebugLoc dl, SelectionDAG &DAG) const { 1866 1867 // CCValAssign - represent the assignment of the return value to a location. 1868 SmallVector<CCValAssign, 16> RVLocs; 1869 1870 // CCState - Info about the registers and stack slots. 1871 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1872 getTargetMachine(), RVLocs, *DAG.getContext(), Call); 1873 1874 // Analyze outgoing return values. 1875 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true, 1876 isVarArg)); 1877 1878 // If this is the first return lowered for this function, add 1879 // the regs to the liveout set for the function. 1880 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { 1881 for (unsigned i = 0; i != RVLocs.size(); ++i) 1882 if (RVLocs[i].isRegLoc()) 1883 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); 1884 } 1885 1886 SDValue Flag; 1887 1888 // Copy the result values into the output registers. 1889 for (unsigned i = 0, realRVLocIdx = 0; 1890 i != RVLocs.size(); 1891 ++i, ++realRVLocIdx) { 1892 CCValAssign &VA = RVLocs[i]; 1893 assert(VA.isRegLoc() && "Can only return in registers!"); 1894 1895 SDValue Arg = OutVals[realRVLocIdx]; 1896 1897 switch (VA.getLocInfo()) { 1898 default: llvm_unreachable("Unknown loc info!"); 1899 case CCValAssign::Full: break; 1900 case CCValAssign::BCvt: 1901 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1902 break; 1903 } 1904 1905 if (VA.needsCustom()) { 1906 if (VA.getLocVT() == MVT::v2f64) { 1907 // Extract the first half and return it in two registers. 1908 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1909 DAG.getConstant(0, MVT::i32)); 1910 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 1911 DAG.getVTList(MVT::i32, MVT::i32), Half); 1912 1913 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag); 1914 Flag = Chain.getValue(1); 1915 VA = RVLocs[++i]; // skip ahead to next loc 1916 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 1917 HalfGPRs.getValue(1), Flag); 1918 Flag = Chain.getValue(1); 1919 VA = RVLocs[++i]; // skip ahead to next loc 1920 1921 // Extract the 2nd half and fall through to handle it as an f64 value. 1922 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1923 DAG.getConstant(1, MVT::i32)); 1924 } 1925 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 1926 // available. 1927 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1928 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1); 1929 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag); 1930 Flag = Chain.getValue(1); 1931 VA = RVLocs[++i]; // skip ahead to next loc 1932 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1), 1933 Flag); 1934 } else 1935 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 1936 1937 // Guarantee that all emitted copies are 1938 // stuck together, avoiding something bad. 1939 Flag = Chain.getValue(1); 1940 } 1941 1942 SDValue result; 1943 if (Flag.getNode()) 1944 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag); 1945 else // Return Void 1946 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain); 1947 1948 return result; 1949 } 1950 1951 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 1952 if (N->getNumValues() != 1) 1953 return false; 1954 if (!N->hasNUsesOfValue(1, 0)) 1955 return false; 1956 1957 SDValue TCChain = Chain; 1958 SDNode *Copy = *N->use_begin(); 1959 if (Copy->getOpcode() == ISD::CopyToReg) { 1960 // If the copy has a glue operand, we conservatively assume it isn't safe to 1961 // perform a tail call. 1962 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 1963 return false; 1964 TCChain = Copy->getOperand(0); 1965 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 1966 SDNode *VMov = Copy; 1967 // f64 returned in a pair of GPRs. 1968 SmallPtrSet<SDNode*, 2> Copies; 1969 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 1970 UI != UE; ++UI) { 1971 if (UI->getOpcode() != ISD::CopyToReg) 1972 return false; 1973 Copies.insert(*UI); 1974 } 1975 if (Copies.size() > 2) 1976 return false; 1977 1978 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 1979 UI != UE; ++UI) { 1980 SDValue UseChain = UI->getOperand(0); 1981 if (Copies.count(UseChain.getNode())) 1982 // Second CopyToReg 1983 Copy = *UI; 1984 else 1985 // First CopyToReg 1986 TCChain = UseChain; 1987 } 1988 } else if (Copy->getOpcode() == ISD::BITCAST) { 1989 // f32 returned in a single GPR. 1990 if (!Copy->hasOneUse()) 1991 return false; 1992 Copy = *Copy->use_begin(); 1993 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 1994 return false; 1995 Chain = Copy->getOperand(0); 1996 } else { 1997 return false; 1998 } 1999 2000 bool HasRet = false; 2001 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2002 UI != UE; ++UI) { 2003 if (UI->getOpcode() != ARMISD::RET_FLAG) 2004 return false; 2005 HasRet = true; 2006 } 2007 2008 if (!HasRet) 2009 return false; 2010 2011 Chain = TCChain; 2012 return true; 2013 } 2014 2015 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 2016 if (!EnableARMTailCalls && !Subtarget->supportsTailCall()) 2017 return false; 2018 2019 if (!CI->isTailCall()) 2020 return false; 2021 2022 return !Subtarget->isThumb1Only(); 2023 } 2024 2025 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2026 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2027 // one of the above mentioned nodes. It has to be wrapped because otherwise 2028 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2029 // be used to form addressing mode. These wrapped nodes will be selected 2030 // into MOVi. 2031 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 2032 EVT PtrVT = Op.getValueType(); 2033 // FIXME there is no actual debug info here 2034 DebugLoc dl = Op.getDebugLoc(); 2035 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2036 SDValue Res; 2037 if (CP->isMachineConstantPoolEntry()) 2038 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2039 CP->getAlignment()); 2040 else 2041 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2042 CP->getAlignment()); 2043 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2044 } 2045 2046 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2047 return MachineJumpTableInfo::EK_Inline; 2048 } 2049 2050 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2051 SelectionDAG &DAG) const { 2052 MachineFunction &MF = DAG.getMachineFunction(); 2053 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2054 unsigned ARMPCLabelIndex = 0; 2055 DebugLoc DL = Op.getDebugLoc(); 2056 EVT PtrVT = getPointerTy(); 2057 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2058 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2059 SDValue CPAddr; 2060 if (RelocM == Reloc::Static) { 2061 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2062 } else { 2063 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2064 ARMPCLabelIndex = AFI->createPICLabelUId(); 2065 ARMConstantPoolValue *CPV = 2066 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2067 ARMCP::CPBlockAddress, PCAdj); 2068 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2069 } 2070 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2071 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr, 2072 MachinePointerInfo::getConstantPool(), 2073 false, false, false, 0); 2074 if (RelocM == Reloc::Static) 2075 return Result; 2076 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2077 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2078 } 2079 2080 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2081 SDValue 2082 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2083 SelectionDAG &DAG) const { 2084 DebugLoc dl = GA->getDebugLoc(); 2085 EVT PtrVT = getPointerTy(); 2086 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2087 MachineFunction &MF = DAG.getMachineFunction(); 2088 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2089 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2090 ARMConstantPoolValue *CPV = 2091 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2092 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2093 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2094 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2095 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, 2096 MachinePointerInfo::getConstantPool(), 2097 false, false, false, 0); 2098 SDValue Chain = Argument.getValue(1); 2099 2100 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2101 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2102 2103 // call __tls_get_addr. 2104 ArgListTy Args; 2105 ArgListEntry Entry; 2106 Entry.Node = Argument; 2107 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2108 Args.push_back(Entry); 2109 // FIXME: is there useful debug info available here? 2110 TargetLowering::CallLoweringInfo CLI(Chain, 2111 (Type *) Type::getInt32Ty(*DAG.getContext()), 2112 false, false, false, false, 2113 0, CallingConv::C, /*isTailCall=*/false, 2114 /*doesNotRet=*/false, /*isReturnValueUsed=*/true, 2115 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl); 2116 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2117 return CallResult.first; 2118 } 2119 2120 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2121 // "local exec" model. 2122 SDValue 2123 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2124 SelectionDAG &DAG, 2125 TLSModel::Model model) const { 2126 const GlobalValue *GV = GA->getGlobal(); 2127 DebugLoc dl = GA->getDebugLoc(); 2128 SDValue Offset; 2129 SDValue Chain = DAG.getEntryNode(); 2130 EVT PtrVT = getPointerTy(); 2131 // Get the Thread Pointer 2132 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2133 2134 if (model == TLSModel::InitialExec) { 2135 MachineFunction &MF = DAG.getMachineFunction(); 2136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2137 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2138 // Initial exec model. 2139 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2140 ARMConstantPoolValue *CPV = 2141 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2142 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2143 true); 2144 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2145 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2146 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2147 MachinePointerInfo::getConstantPool(), 2148 false, false, false, 0); 2149 Chain = Offset.getValue(1); 2150 2151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2152 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2153 2154 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2155 MachinePointerInfo::getConstantPool(), 2156 false, false, false, 0); 2157 } else { 2158 // local exec model 2159 assert(model == TLSModel::LocalExec); 2160 ARMConstantPoolValue *CPV = 2161 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2162 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2163 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2164 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2165 MachinePointerInfo::getConstantPool(), 2166 false, false, false, 0); 2167 } 2168 2169 // The address of the thread local variable is the add of the thread 2170 // pointer with the offset of the variable. 2171 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2172 } 2173 2174 SDValue 2175 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2176 // TODO: implement the "local dynamic" model 2177 assert(Subtarget->isTargetELF() && 2178 "TLS not implemented for non-ELF targets"); 2179 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2180 2181 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 2182 2183 switch (model) { 2184 case TLSModel::GeneralDynamic: 2185 case TLSModel::LocalDynamic: 2186 return LowerToTLSGeneralDynamicModel(GA, DAG); 2187 case TLSModel::InitialExec: 2188 case TLSModel::LocalExec: 2189 return LowerToTLSExecModels(GA, DAG, model); 2190 } 2191 llvm_unreachable("bogus TLS model"); 2192 } 2193 2194 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 2195 SelectionDAG &DAG) const { 2196 EVT PtrVT = getPointerTy(); 2197 DebugLoc dl = Op.getDebugLoc(); 2198 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2199 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2200 if (RelocM == Reloc::PIC_) { 2201 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility(); 2202 ARMConstantPoolValue *CPV = 2203 ARMConstantPoolConstant::Create(GV, 2204 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT); 2205 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2207 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 2208 CPAddr, 2209 MachinePointerInfo::getConstantPool(), 2210 false, false, false, 0); 2211 SDValue Chain = Result.getValue(1); 2212 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); 2213 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT); 2214 if (!UseGOTOFF) 2215 Result = DAG.getLoad(PtrVT, dl, Chain, Result, 2216 MachinePointerInfo::getGOT(), 2217 false, false, false, 0); 2218 return Result; 2219 } 2220 2221 // If we have T2 ops, we can materialize the address directly via movt/movw 2222 // pair. This is always cheaper. 2223 if (Subtarget->useMovt()) { 2224 ++NumMovwMovt; 2225 // FIXME: Once remat is capable of dealing with instructions with register 2226 // operands, expand this into two nodes. 2227 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2228 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2229 } else { 2230 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2231 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2232 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2233 MachinePointerInfo::getConstantPool(), 2234 false, false, false, 0); 2235 } 2236 } 2237 2238 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 2239 SelectionDAG &DAG) const { 2240 EVT PtrVT = getPointerTy(); 2241 DebugLoc dl = Op.getDebugLoc(); 2242 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2243 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2244 MachineFunction &MF = DAG.getMachineFunction(); 2245 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2246 2247 // FIXME: Enable this for static codegen when tool issues are fixed. Also 2248 // update ARMFastISel::ARMMaterializeGV. 2249 if (Subtarget->useMovt() && RelocM != Reloc::Static) { 2250 ++NumMovwMovt; 2251 // FIXME: Once remat is capable of dealing with instructions with register 2252 // operands, expand this into two nodes. 2253 if (RelocM == Reloc::Static) 2254 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2255 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2256 2257 unsigned Wrapper = (RelocM == Reloc::PIC_) 2258 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN; 2259 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, 2260 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2261 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2262 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 2263 MachinePointerInfo::getGOT(), 2264 false, false, false, 0); 2265 return Result; 2266 } 2267 2268 unsigned ARMPCLabelIndex = 0; 2269 SDValue CPAddr; 2270 if (RelocM == Reloc::Static) { 2271 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2272 } else { 2273 ARMPCLabelIndex = AFI->createPICLabelUId(); 2274 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8); 2275 ARMConstantPoolValue *CPV = 2276 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 2277 PCAdj); 2278 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2279 } 2280 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2281 2282 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2283 MachinePointerInfo::getConstantPool(), 2284 false, false, false, 0); 2285 SDValue Chain = Result.getValue(1); 2286 2287 if (RelocM == Reloc::PIC_) { 2288 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2289 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2290 } 2291 2292 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2293 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(), 2294 false, false, false, 0); 2295 2296 return Result; 2297 } 2298 2299 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, 2300 SelectionDAG &DAG) const { 2301 assert(Subtarget->isTargetELF() && 2302 "GLOBAL OFFSET TABLE not implemented for non-ELF targets"); 2303 MachineFunction &MF = DAG.getMachineFunction(); 2304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2305 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2306 EVT PtrVT = getPointerTy(); 2307 DebugLoc dl = Op.getDebugLoc(); 2308 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2309 ARMConstantPoolValue *CPV = 2310 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_", 2311 ARMPCLabelIndex, PCAdj); 2312 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2313 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2314 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2315 MachinePointerInfo::getConstantPool(), 2316 false, false, false, 0); 2317 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2318 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2319 } 2320 2321 SDValue 2322 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 2323 DebugLoc dl = Op.getDebugLoc(); 2324 SDValue Val = DAG.getConstant(0, MVT::i32); 2325 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 2326 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 2327 Op.getOperand(1), Val); 2328 } 2329 2330 SDValue 2331 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 2332 DebugLoc dl = Op.getDebugLoc(); 2333 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 2334 Op.getOperand(1), DAG.getConstant(0, MVT::i32)); 2335 } 2336 2337 SDValue 2338 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 2339 const ARMSubtarget *Subtarget) const { 2340 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2341 DebugLoc dl = Op.getDebugLoc(); 2342 switch (IntNo) { 2343 default: return SDValue(); // Don't custom lower most intrinsics. 2344 case Intrinsic::arm_thread_pointer: { 2345 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2346 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2347 } 2348 case Intrinsic::eh_sjlj_lsda: { 2349 MachineFunction &MF = DAG.getMachineFunction(); 2350 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2351 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2352 EVT PtrVT = getPointerTy(); 2353 DebugLoc dl = Op.getDebugLoc(); 2354 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2355 SDValue CPAddr; 2356 unsigned PCAdj = (RelocM != Reloc::PIC_) 2357 ? 0 : (Subtarget->isThumb() ? 4 : 8); 2358 ARMConstantPoolValue *CPV = 2359 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex, 2360 ARMCP::CPLSDA, PCAdj); 2361 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2362 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2363 SDValue Result = 2364 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2365 MachinePointerInfo::getConstantPool(), 2366 false, false, false, 0); 2367 2368 if (RelocM == Reloc::PIC_) { 2369 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2370 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2371 } 2372 return Result; 2373 } 2374 case Intrinsic::arm_neon_vmulls: 2375 case Intrinsic::arm_neon_vmullu: { 2376 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 2377 ? ARMISD::VMULLs : ARMISD::VMULLu; 2378 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(), 2379 Op.getOperand(1), Op.getOperand(2)); 2380 } 2381 } 2382 } 2383 2384 static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG, 2385 const ARMSubtarget *Subtarget) { 2386 DebugLoc dl = Op.getDebugLoc(); 2387 if (!Subtarget->hasDataBarrier()) { 2388 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2389 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2390 // here. 2391 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2392 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!"); 2393 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2394 DAG.getConstant(0, MVT::i32)); 2395 } 2396 2397 SDValue Op5 = Op.getOperand(5); 2398 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0; 2399 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2400 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 2401 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0); 2402 2403 ARM_MB::MemBOpt DMBOpt; 2404 if (isDeviceBarrier) 2405 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY; 2406 else 2407 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH; 2408 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0), 2409 DAG.getConstant(DMBOpt, MVT::i32)); 2410 } 2411 2412 2413 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 2414 const ARMSubtarget *Subtarget) { 2415 // FIXME: handle "fence singlethread" more efficiently. 2416 DebugLoc dl = Op.getDebugLoc(); 2417 if (!Subtarget->hasDataBarrier()) { 2418 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2419 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2420 // here. 2421 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2422 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!"); 2423 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2424 DAG.getConstant(0, MVT::i32)); 2425 } 2426 2427 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0), 2428 DAG.getConstant(ARM_MB::ISH, MVT::i32)); 2429 } 2430 2431 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 2432 const ARMSubtarget *Subtarget) { 2433 // ARM pre v5TE and Thumb1 does not have preload instructions. 2434 if (!(Subtarget->isThumb2() || 2435 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 2436 // Just preserve the chain. 2437 return Op.getOperand(0); 2438 2439 DebugLoc dl = Op.getDebugLoc(); 2440 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 2441 if (!isRead && 2442 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 2443 // ARMv7 with MP extension has PLDW. 2444 return Op.getOperand(0); 2445 2446 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 2447 if (Subtarget->isThumb()) { 2448 // Invert the bits. 2449 isRead = ~isRead & 1; 2450 isData = ~isData & 1; 2451 } 2452 2453 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 2454 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32), 2455 DAG.getConstant(isData, MVT::i32)); 2456 } 2457 2458 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 2459 MachineFunction &MF = DAG.getMachineFunction(); 2460 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 2461 2462 // vastart just stores the address of the VarArgsFrameIndex slot into the 2463 // memory location argument. 2464 DebugLoc dl = Op.getDebugLoc(); 2465 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2466 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2467 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2468 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2469 MachinePointerInfo(SV), false, false, 0); 2470 } 2471 2472 SDValue 2473 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA, 2474 SDValue &Root, SelectionDAG &DAG, 2475 DebugLoc dl) const { 2476 MachineFunction &MF = DAG.getMachineFunction(); 2477 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2478 2479 const TargetRegisterClass *RC; 2480 if (AFI->isThumb1OnlyFunction()) 2481 RC = &ARM::tGPRRegClass; 2482 else 2483 RC = &ARM::GPRRegClass; 2484 2485 // Transform the arguments stored in physical registers into virtual ones. 2486 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2487 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2488 2489 SDValue ArgValue2; 2490 if (NextVA.isMemLoc()) { 2491 MachineFrameInfo *MFI = MF.getFrameInfo(); 2492 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true); 2493 2494 // Create load node to retrieve arguments from the stack. 2495 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2496 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, 2497 MachinePointerInfo::getFixedStack(FI), 2498 false, false, false, 0); 2499 } else { 2500 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 2501 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2502 } 2503 2504 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 2505 } 2506 2507 void 2508 ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF, 2509 unsigned &VARegSize, unsigned &VARegSaveSize) 2510 const { 2511 unsigned NumGPRs; 2512 if (CCInfo.isFirstByValRegValid()) 2513 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg(); 2514 else { 2515 unsigned int firstUnalloced; 2516 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs, 2517 sizeof(GPRArgRegs) / 2518 sizeof(GPRArgRegs[0])); 2519 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0; 2520 } 2521 2522 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment(); 2523 VARegSize = NumGPRs * 4; 2524 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1); 2525 } 2526 2527 // The remaining GPRs hold either the beginning of variable-argument 2528 // data, or the beginning of an aggregate passed by value (usuall 2529 // byval). Either way, we allocate stack slots adjacent to the data 2530 // provided by our caller, and store the unallocated registers there. 2531 // If this is a variadic function, the va_list pointer will begin with 2532 // these values; otherwise, this reassembles a (byval) structure that 2533 // was split between registers and memory. 2534 void 2535 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 2536 DebugLoc dl, SDValue &Chain, 2537 unsigned ArgOffset) const { 2538 MachineFunction &MF = DAG.getMachineFunction(); 2539 MachineFrameInfo *MFI = MF.getFrameInfo(); 2540 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2541 unsigned firstRegToSaveIndex; 2542 if (CCInfo.isFirstByValRegValid()) 2543 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0; 2544 else { 2545 firstRegToSaveIndex = CCInfo.getFirstUnallocated 2546 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0])); 2547 } 2548 2549 unsigned VARegSize, VARegSaveSize; 2550 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize); 2551 if (VARegSaveSize) { 2552 // If this function is vararg, store any remaining integer argument regs 2553 // to their spots on the stack so that they may be loaded by deferencing 2554 // the result of va_next. 2555 AFI->setVarArgsRegSaveSize(VARegSaveSize); 2556 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize, 2557 ArgOffset + VARegSaveSize 2558 - VARegSize, 2559 false)); 2560 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(), 2561 getPointerTy()); 2562 2563 SmallVector<SDValue, 4> MemOps; 2564 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) { 2565 const TargetRegisterClass *RC; 2566 if (AFI->isThumb1OnlyFunction()) 2567 RC = &ARM::tGPRRegClass; 2568 else 2569 RC = &ARM::GPRRegClass; 2570 2571 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC); 2572 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 2573 SDValue Store = 2574 DAG.getStore(Val.getValue(1), dl, Val, FIN, 2575 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()), 2576 false, false, 0); 2577 MemOps.push_back(Store); 2578 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 2579 DAG.getConstant(4, getPointerTy())); 2580 } 2581 if (!MemOps.empty()) 2582 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 2583 &MemOps[0], MemOps.size()); 2584 } else 2585 // This will point to the next argument passed via stack. 2586 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true)); 2587 } 2588 2589 SDValue 2590 ARMTargetLowering::LowerFormalArguments(SDValue Chain, 2591 CallingConv::ID CallConv, bool isVarArg, 2592 const SmallVectorImpl<ISD::InputArg> 2593 &Ins, 2594 DebugLoc dl, SelectionDAG &DAG, 2595 SmallVectorImpl<SDValue> &InVals) 2596 const { 2597 MachineFunction &MF = DAG.getMachineFunction(); 2598 MachineFrameInfo *MFI = MF.getFrameInfo(); 2599 2600 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2601 2602 // Assign locations to all of the incoming arguments. 2603 SmallVector<CCValAssign, 16> ArgLocs; 2604 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 2605 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue); 2606 CCInfo.AnalyzeFormalArguments(Ins, 2607 CCAssignFnForNode(CallConv, /* Return*/ false, 2608 isVarArg)); 2609 2610 SmallVector<SDValue, 16> ArgValues; 2611 int lastInsIndex = -1; 2612 2613 SDValue ArgValue; 2614 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2615 CCValAssign &VA = ArgLocs[i]; 2616 2617 // Arguments stored in registers. 2618 if (VA.isRegLoc()) { 2619 EVT RegVT = VA.getLocVT(); 2620 2621 if (VA.needsCustom()) { 2622 // f64 and vector types are split up into multiple registers or 2623 // combinations of registers and stack slots. 2624 if (VA.getLocVT() == MVT::v2f64) { 2625 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 2626 Chain, DAG, dl); 2627 VA = ArgLocs[++i]; // skip ahead to next loc 2628 SDValue ArgValue2; 2629 if (VA.isMemLoc()) { 2630 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true); 2631 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2632 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 2633 MachinePointerInfo::getFixedStack(FI), 2634 false, false, false, 0); 2635 } else { 2636 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 2637 Chain, DAG, dl); 2638 } 2639 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 2640 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 2641 ArgValue, ArgValue1, DAG.getIntPtrConstant(0)); 2642 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 2643 ArgValue, ArgValue2, DAG.getIntPtrConstant(1)); 2644 } else 2645 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 2646 2647 } else { 2648 const TargetRegisterClass *RC; 2649 2650 if (RegVT == MVT::f32) 2651 RC = &ARM::SPRRegClass; 2652 else if (RegVT == MVT::f64) 2653 RC = &ARM::DPRRegClass; 2654 else if (RegVT == MVT::v2f64) 2655 RC = &ARM::QPRRegClass; 2656 else if (RegVT == MVT::i32) 2657 RC = AFI->isThumb1OnlyFunction() ? 2658 (const TargetRegisterClass*)&ARM::tGPRRegClass : 2659 (const TargetRegisterClass*)&ARM::GPRRegClass; 2660 else 2661 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 2662 2663 // Transform the arguments in physical registers into virtual ones. 2664 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2665 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 2666 } 2667 2668 // If this is an 8 or 16-bit value, it is really passed promoted 2669 // to 32 bits. Insert an assert[sz]ext to capture this, then 2670 // truncate to the right size. 2671 switch (VA.getLocInfo()) { 2672 default: llvm_unreachable("Unknown loc info!"); 2673 case CCValAssign::Full: break; 2674 case CCValAssign::BCvt: 2675 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 2676 break; 2677 case CCValAssign::SExt: 2678 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 2679 DAG.getValueType(VA.getValVT())); 2680 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 2681 break; 2682 case CCValAssign::ZExt: 2683 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 2684 DAG.getValueType(VA.getValVT())); 2685 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 2686 break; 2687 } 2688 2689 InVals.push_back(ArgValue); 2690 2691 } else { // VA.isRegLoc() 2692 2693 // sanity check 2694 assert(VA.isMemLoc()); 2695 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 2696 2697 int index = ArgLocs[i].getValNo(); 2698 2699 // Some Ins[] entries become multiple ArgLoc[] entries. 2700 // Process them only once. 2701 if (index != lastInsIndex) 2702 { 2703 ISD::ArgFlagsTy Flags = Ins[index].Flags; 2704 // FIXME: For now, all byval parameter objects are marked mutable. 2705 // This can be changed with more analysis. 2706 // In case of tail call optimization mark all arguments mutable. 2707 // Since they could be overwritten by lowering of arguments in case of 2708 // a tail call. 2709 if (Flags.isByVal()) { 2710 unsigned VARegSize, VARegSaveSize; 2711 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize); 2712 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0); 2713 unsigned Bytes = Flags.getByValSize() - VARegSize; 2714 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects. 2715 int FI = MFI->CreateFixedObject(Bytes, 2716 VA.getLocMemOffset(), false); 2717 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy())); 2718 } else { 2719 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 2720 VA.getLocMemOffset(), true); 2721 2722 // Create load nodes to retrieve arguments from the stack. 2723 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2724 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 2725 MachinePointerInfo::getFixedStack(FI), 2726 false, false, false, 0)); 2727 } 2728 lastInsIndex = index; 2729 } 2730 } 2731 } 2732 2733 // varargs 2734 if (isVarArg) 2735 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset()); 2736 2737 return Chain; 2738 } 2739 2740 /// isFloatingPointZero - Return true if this is +0.0. 2741 static bool isFloatingPointZero(SDValue Op) { 2742 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 2743 return CFP->getValueAPF().isPosZero(); 2744 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 2745 // Maybe this has already been legalized into the constant pool? 2746 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 2747 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 2748 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 2749 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 2750 return CFP->getValueAPF().isPosZero(); 2751 } 2752 } 2753 return false; 2754 } 2755 2756 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 2757 /// the given operands. 2758 SDValue 2759 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 2760 SDValue &ARMcc, SelectionDAG &DAG, 2761 DebugLoc dl) const { 2762 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 2763 unsigned C = RHSC->getZExtValue(); 2764 if (!isLegalICmpImmediate(C)) { 2765 // Constant does not fit, try adjusting it by one? 2766 switch (CC) { 2767 default: break; 2768 case ISD::SETLT: 2769 case ISD::SETGE: 2770 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 2771 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 2772 RHS = DAG.getConstant(C-1, MVT::i32); 2773 } 2774 break; 2775 case ISD::SETULT: 2776 case ISD::SETUGE: 2777 if (C != 0 && isLegalICmpImmediate(C-1)) { 2778 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 2779 RHS = DAG.getConstant(C-1, MVT::i32); 2780 } 2781 break; 2782 case ISD::SETLE: 2783 case ISD::SETGT: 2784 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 2785 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 2786 RHS = DAG.getConstant(C+1, MVT::i32); 2787 } 2788 break; 2789 case ISD::SETULE: 2790 case ISD::SETUGT: 2791 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 2792 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 2793 RHS = DAG.getConstant(C+1, MVT::i32); 2794 } 2795 break; 2796 } 2797 } 2798 } 2799 2800 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 2801 ARMISD::NodeType CompareType; 2802 switch (CondCode) { 2803 default: 2804 CompareType = ARMISD::CMP; 2805 break; 2806 case ARMCC::EQ: 2807 case ARMCC::NE: 2808 // Uses only Z Flag 2809 CompareType = ARMISD::CMPZ; 2810 break; 2811 } 2812 ARMcc = DAG.getConstant(CondCode, MVT::i32); 2813 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 2814 } 2815 2816 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 2817 SDValue 2818 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG, 2819 DebugLoc dl) const { 2820 SDValue Cmp; 2821 if (!isFloatingPointZero(RHS)) 2822 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS); 2823 else 2824 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS); 2825 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 2826 } 2827 2828 /// duplicateCmp - Glue values can have only one use, so this function 2829 /// duplicates a comparison node. 2830 SDValue 2831 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 2832 unsigned Opc = Cmp.getOpcode(); 2833 DebugLoc DL = Cmp.getDebugLoc(); 2834 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 2835 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 2836 2837 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 2838 Cmp = Cmp.getOperand(0); 2839 Opc = Cmp.getOpcode(); 2840 if (Opc == ARMISD::CMPFP) 2841 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 2842 else { 2843 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 2844 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 2845 } 2846 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 2847 } 2848 2849 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 2850 SDValue Cond = Op.getOperand(0); 2851 SDValue SelectTrue = Op.getOperand(1); 2852 SDValue SelectFalse = Op.getOperand(2); 2853 DebugLoc dl = Op.getDebugLoc(); 2854 2855 // Convert: 2856 // 2857 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 2858 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 2859 // 2860 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 2861 const ConstantSDNode *CMOVTrue = 2862 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 2863 const ConstantSDNode *CMOVFalse = 2864 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 2865 2866 if (CMOVTrue && CMOVFalse) { 2867 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 2868 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 2869 2870 SDValue True; 2871 SDValue False; 2872 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 2873 True = SelectTrue; 2874 False = SelectFalse; 2875 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 2876 True = SelectFalse; 2877 False = SelectTrue; 2878 } 2879 2880 if (True.getNode() && False.getNode()) { 2881 EVT VT = Op.getValueType(); 2882 SDValue ARMcc = Cond.getOperand(2); 2883 SDValue CCR = Cond.getOperand(3); 2884 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 2885 assert(True.getValueType() == VT); 2886 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp); 2887 } 2888 } 2889 } 2890 2891 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 2892 // undefined bits before doing a full-word comparison with zero. 2893 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 2894 DAG.getConstant(1, Cond.getValueType())); 2895 2896 return DAG.getSelectCC(dl, Cond, 2897 DAG.getConstant(0, Cond.getValueType()), 2898 SelectTrue, SelectFalse, ISD::SETNE); 2899 } 2900 2901 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 2902 EVT VT = Op.getValueType(); 2903 SDValue LHS = Op.getOperand(0); 2904 SDValue RHS = Op.getOperand(1); 2905 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 2906 SDValue TrueVal = Op.getOperand(2); 2907 SDValue FalseVal = Op.getOperand(3); 2908 DebugLoc dl = Op.getDebugLoc(); 2909 2910 if (LHS.getValueType() == MVT::i32) { 2911 SDValue ARMcc; 2912 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2913 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 2914 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp); 2915 } 2916 2917 ARMCC::CondCodes CondCode, CondCode2; 2918 FPCCToARMCC(CC, CondCode, CondCode2); 2919 2920 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 2921 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 2922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2923 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 2924 ARMcc, CCR, Cmp); 2925 if (CondCode2 != ARMCC::AL) { 2926 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32); 2927 // FIXME: Needs another CMP because flag can have but one use. 2928 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 2929 Result = DAG.getNode(ARMISD::CMOV, dl, VT, 2930 Result, TrueVal, ARMcc2, CCR, Cmp2); 2931 } 2932 return Result; 2933 } 2934 2935 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 2936 /// to morph to an integer compare sequence. 2937 static bool canChangeToInt(SDValue Op, bool &SeenZero, 2938 const ARMSubtarget *Subtarget) { 2939 SDNode *N = Op.getNode(); 2940 if (!N->hasOneUse()) 2941 // Otherwise it requires moving the value from fp to integer registers. 2942 return false; 2943 if (!N->getNumValues()) 2944 return false; 2945 EVT VT = Op.getValueType(); 2946 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 2947 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 2948 // vmrs are very slow, e.g. cortex-a8. 2949 return false; 2950 2951 if (isFloatingPointZero(Op)) { 2952 SeenZero = true; 2953 return true; 2954 } 2955 return ISD::isNormalLoad(N); 2956 } 2957 2958 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 2959 if (isFloatingPointZero(Op)) 2960 return DAG.getConstant(0, MVT::i32); 2961 2962 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 2963 return DAG.getLoad(MVT::i32, Op.getDebugLoc(), 2964 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(), 2965 Ld->isVolatile(), Ld->isNonTemporal(), 2966 Ld->isInvariant(), Ld->getAlignment()); 2967 2968 llvm_unreachable("Unknown VFP cmp argument!"); 2969 } 2970 2971 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 2972 SDValue &RetVal1, SDValue &RetVal2) { 2973 if (isFloatingPointZero(Op)) { 2974 RetVal1 = DAG.getConstant(0, MVT::i32); 2975 RetVal2 = DAG.getConstant(0, MVT::i32); 2976 return; 2977 } 2978 2979 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 2980 SDValue Ptr = Ld->getBasePtr(); 2981 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(), 2982 Ld->getChain(), Ptr, 2983 Ld->getPointerInfo(), 2984 Ld->isVolatile(), Ld->isNonTemporal(), 2985 Ld->isInvariant(), Ld->getAlignment()); 2986 2987 EVT PtrType = Ptr.getValueType(); 2988 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 2989 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(), 2990 PtrType, Ptr, DAG.getConstant(4, PtrType)); 2991 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(), 2992 Ld->getChain(), NewPtr, 2993 Ld->getPointerInfo().getWithOffset(4), 2994 Ld->isVolatile(), Ld->isNonTemporal(), 2995 Ld->isInvariant(), NewAlign); 2996 return; 2997 } 2998 2999 llvm_unreachable("Unknown VFP cmp argument!"); 3000 } 3001 3002 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 3003 /// f32 and even f64 comparisons to integer ones. 3004 SDValue 3005 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 3006 SDValue Chain = Op.getOperand(0); 3007 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3008 SDValue LHS = Op.getOperand(2); 3009 SDValue RHS = Op.getOperand(3); 3010 SDValue Dest = Op.getOperand(4); 3011 DebugLoc dl = Op.getDebugLoc(); 3012 3013 bool LHSSeenZero = false; 3014 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 3015 bool RHSSeenZero = false; 3016 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 3017 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 3018 // If unsafe fp math optimization is enabled and there are no other uses of 3019 // the CMP operands, and the condition code is EQ or NE, we can optimize it 3020 // to an integer comparison. 3021 if (CC == ISD::SETOEQ) 3022 CC = ISD::SETEQ; 3023 else if (CC == ISD::SETUNE) 3024 CC = ISD::SETNE; 3025 3026 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32); 3027 SDValue ARMcc; 3028 if (LHS.getValueType() == MVT::f32) { 3029 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3030 bitcastf32Toi32(LHS, DAG), Mask); 3031 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3032 bitcastf32Toi32(RHS, DAG), Mask); 3033 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3034 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3035 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3036 Chain, Dest, ARMcc, CCR, Cmp); 3037 } 3038 3039 SDValue LHS1, LHS2; 3040 SDValue RHS1, RHS2; 3041 expandf64Toi32(LHS, DAG, LHS1, LHS2); 3042 expandf64Toi32(RHS, DAG, RHS1, RHS2); 3043 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 3044 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 3045 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3046 ARMcc = DAG.getConstant(CondCode, MVT::i32); 3047 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3048 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 3049 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7); 3050 } 3051 3052 return SDValue(); 3053 } 3054 3055 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 3056 SDValue Chain = Op.getOperand(0); 3057 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3058 SDValue LHS = Op.getOperand(2); 3059 SDValue RHS = Op.getOperand(3); 3060 SDValue Dest = Op.getOperand(4); 3061 DebugLoc dl = Op.getDebugLoc(); 3062 3063 if (LHS.getValueType() == MVT::i32) { 3064 SDValue ARMcc; 3065 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3066 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3067 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3068 Chain, Dest, ARMcc, CCR, Cmp); 3069 } 3070 3071 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3072 3073 if (getTargetMachine().Options.UnsafeFPMath && 3074 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 3075 CC == ISD::SETNE || CC == ISD::SETUNE)) { 3076 SDValue Result = OptimizeVFPBrcond(Op, DAG); 3077 if (Result.getNode()) 3078 return Result; 3079 } 3080 3081 ARMCC::CondCodes CondCode, CondCode2; 3082 FPCCToARMCC(CC, CondCode, CondCode2); 3083 3084 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 3085 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3086 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3087 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3088 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 3089 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5); 3090 if (CondCode2 != ARMCC::AL) { 3091 ARMcc = DAG.getConstant(CondCode2, MVT::i32); 3092 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 3093 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5); 3094 } 3095 return Res; 3096 } 3097 3098 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 3099 SDValue Chain = Op.getOperand(0); 3100 SDValue Table = Op.getOperand(1); 3101 SDValue Index = Op.getOperand(2); 3102 DebugLoc dl = Op.getDebugLoc(); 3103 3104 EVT PTy = getPointerTy(); 3105 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 3106 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 3107 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy); 3108 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 3109 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId); 3110 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy)); 3111 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 3112 if (Subtarget->isThumb2()) { 3113 // Thumb2 uses a two-level jump. That is, it jumps into the jump table 3114 // which does another jump to the destination. This also makes it easier 3115 // to translate it to TBB / TBH later. 3116 // FIXME: This might not work if the function is extremely large. 3117 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 3118 Addr, Op.getOperand(2), JTI, UId); 3119 } 3120 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 3121 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 3122 MachinePointerInfo::getJumpTable(), 3123 false, false, false, 0); 3124 Chain = Addr.getValue(1); 3125 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table); 3126 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3127 } else { 3128 Addr = DAG.getLoad(PTy, dl, Chain, Addr, 3129 MachinePointerInfo::getJumpTable(), 3130 false, false, false, 0); 3131 Chain = Addr.getValue(1); 3132 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3133 } 3134 } 3135 3136 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3137 EVT VT = Op.getValueType(); 3138 DebugLoc dl = Op.getDebugLoc(); 3139 3140 if (Op.getValueType().getVectorElementType() == MVT::i32) { 3141 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 3142 return Op; 3143 return DAG.UnrollVectorOp(Op.getNode()); 3144 } 3145 3146 assert(Op.getOperand(0).getValueType() == MVT::v4f32 && 3147 "Invalid type for custom lowering!"); 3148 if (VT != MVT::v4i16) 3149 return DAG.UnrollVectorOp(Op.getNode()); 3150 3151 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0)); 3152 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 3153 } 3154 3155 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3156 EVT VT = Op.getValueType(); 3157 if (VT.isVector()) 3158 return LowerVectorFP_TO_INT(Op, DAG); 3159 3160 DebugLoc dl = Op.getDebugLoc(); 3161 unsigned Opc; 3162 3163 switch (Op.getOpcode()) { 3164 default: llvm_unreachable("Invalid opcode!"); 3165 case ISD::FP_TO_SINT: 3166 Opc = ARMISD::FTOSI; 3167 break; 3168 case ISD::FP_TO_UINT: 3169 Opc = ARMISD::FTOUI; 3170 break; 3171 } 3172 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0)); 3173 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 3174 } 3175 3176 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3177 EVT VT = Op.getValueType(); 3178 DebugLoc dl = Op.getDebugLoc(); 3179 3180 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 3181 if (VT.getVectorElementType() == MVT::f32) 3182 return Op; 3183 return DAG.UnrollVectorOp(Op.getNode()); 3184 } 3185 3186 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 3187 "Invalid type for custom lowering!"); 3188 if (VT != MVT::v4f32) 3189 return DAG.UnrollVectorOp(Op.getNode()); 3190 3191 unsigned CastOpc; 3192 unsigned Opc; 3193 switch (Op.getOpcode()) { 3194 default: llvm_unreachable("Invalid opcode!"); 3195 case ISD::SINT_TO_FP: 3196 CastOpc = ISD::SIGN_EXTEND; 3197 Opc = ISD::SINT_TO_FP; 3198 break; 3199 case ISD::UINT_TO_FP: 3200 CastOpc = ISD::ZERO_EXTEND; 3201 Opc = ISD::UINT_TO_FP; 3202 break; 3203 } 3204 3205 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 3206 return DAG.getNode(Opc, dl, VT, Op); 3207 } 3208 3209 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3210 EVT VT = Op.getValueType(); 3211 if (VT.isVector()) 3212 return LowerVectorINT_TO_FP(Op, DAG); 3213 3214 DebugLoc dl = Op.getDebugLoc(); 3215 unsigned Opc; 3216 3217 switch (Op.getOpcode()) { 3218 default: llvm_unreachable("Invalid opcode!"); 3219 case ISD::SINT_TO_FP: 3220 Opc = ARMISD::SITOF; 3221 break; 3222 case ISD::UINT_TO_FP: 3223 Opc = ARMISD::UITOF; 3224 break; 3225 } 3226 3227 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0)); 3228 return DAG.getNode(Opc, dl, VT, Op); 3229 } 3230 3231 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 3232 // Implement fcopysign with a fabs and a conditional fneg. 3233 SDValue Tmp0 = Op.getOperand(0); 3234 SDValue Tmp1 = Op.getOperand(1); 3235 DebugLoc dl = Op.getDebugLoc(); 3236 EVT VT = Op.getValueType(); 3237 EVT SrcVT = Tmp1.getValueType(); 3238 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 3239 Tmp0.getOpcode() == ARMISD::VMOVDRR; 3240 bool UseNEON = !InGPR && Subtarget->hasNEON(); 3241 3242 if (UseNEON) { 3243 // Use VBSL to copy the sign bit. 3244 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 3245 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 3246 DAG.getTargetConstant(EncodedVal, MVT::i32)); 3247 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 3248 if (VT == MVT::f64) 3249 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3250 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 3251 DAG.getConstant(32, MVT::i32)); 3252 else /*if (VT == MVT::f32)*/ 3253 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 3254 if (SrcVT == MVT::f32) { 3255 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 3256 if (VT == MVT::f64) 3257 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3258 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 3259 DAG.getConstant(32, MVT::i32)); 3260 } else if (VT == MVT::f32) 3261 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 3262 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 3263 DAG.getConstant(32, MVT::i32)); 3264 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 3265 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 3266 3267 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 3268 MVT::i32); 3269 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 3270 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 3271 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 3272 3273 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 3274 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 3275 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 3276 if (VT == MVT::f32) { 3277 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 3278 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 3279 DAG.getConstant(0, MVT::i32)); 3280 } else { 3281 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 3282 } 3283 3284 return Res; 3285 } 3286 3287 // Bitcast operand 1 to i32. 3288 if (SrcVT == MVT::f64) 3289 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 3290 &Tmp1, 1).getValue(1); 3291 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 3292 3293 // Or in the signbit with integer operations. 3294 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32); 3295 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32); 3296 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 3297 if (VT == MVT::f32) { 3298 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 3299 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 3300 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 3301 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 3302 } 3303 3304 // f64: Or the high part with signbit and then combine two parts. 3305 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 3306 &Tmp0, 1); 3307 SDValue Lo = Tmp0.getValue(0); 3308 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 3309 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 3310 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 3311 } 3312 3313 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 3314 MachineFunction &MF = DAG.getMachineFunction(); 3315 MachineFrameInfo *MFI = MF.getFrameInfo(); 3316 MFI->setReturnAddressIsTaken(true); 3317 3318 EVT VT = Op.getValueType(); 3319 DebugLoc dl = Op.getDebugLoc(); 3320 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3321 if (Depth) { 3322 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 3323 SDValue Offset = DAG.getConstant(4, MVT::i32); 3324 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 3325 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 3326 MachinePointerInfo(), false, false, false, 0); 3327 } 3328 3329 // Return LR, which contains the return address. Mark it an implicit live-in. 3330 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 3331 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 3332 } 3333 3334 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 3335 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 3336 MFI->setFrameAddressIsTaken(true); 3337 3338 EVT VT = Op.getValueType(); 3339 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful 3340 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3341 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin()) 3342 ? ARM::R7 : ARM::R11; 3343 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 3344 while (Depth--) 3345 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 3346 MachinePointerInfo(), 3347 false, false, false, 0); 3348 return FrameAddr; 3349 } 3350 3351 /// ExpandBITCAST - If the target supports VFP, this function is called to 3352 /// expand a bit convert where either the source or destination type is i64 to 3353 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 3354 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 3355 /// vectors), since the legalizer won't know what to do with that. 3356 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 3357 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3358 DebugLoc dl = N->getDebugLoc(); 3359 SDValue Op = N->getOperand(0); 3360 3361 // This function is only supposed to be called for i64 types, either as the 3362 // source or destination of the bit convert. 3363 EVT SrcVT = Op.getValueType(); 3364 EVT DstVT = N->getValueType(0); 3365 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 3366 "ExpandBITCAST called for non-i64 type"); 3367 3368 // Turn i64->f64 into VMOVDRR. 3369 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 3370 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 3371 DAG.getConstant(0, MVT::i32)); 3372 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 3373 DAG.getConstant(1, MVT::i32)); 3374 return DAG.getNode(ISD::BITCAST, dl, DstVT, 3375 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 3376 } 3377 3378 // Turn f64->i64 into VMOVRRD. 3379 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 3380 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 3381 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1); 3382 // Merge the pieces into a single i64 value. 3383 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 3384 } 3385 3386 return SDValue(); 3387 } 3388 3389 /// getZeroVector - Returns a vector of specified type with all zero elements. 3390 /// Zero vectors are used to represent vector negation and in those cases 3391 /// will be implemented with the NEON VNEG instruction. However, VNEG does 3392 /// not support i64 elements, so sometimes the zero vectors will need to be 3393 /// explicitly constructed. Regardless, use a canonical VMOV to create the 3394 /// zero vector. 3395 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) { 3396 assert(VT.isVector() && "Expected a vector type"); 3397 // The canonical modified immediate encoding of a zero vector is....0! 3398 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32); 3399 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 3400 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 3401 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 3402 } 3403 3404 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 3405 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 3406 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 3407 SelectionDAG &DAG) const { 3408 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 3409 EVT VT = Op.getValueType(); 3410 unsigned VTBits = VT.getSizeInBits(); 3411 DebugLoc dl = Op.getDebugLoc(); 3412 SDValue ShOpLo = Op.getOperand(0); 3413 SDValue ShOpHi = Op.getOperand(1); 3414 SDValue ShAmt = Op.getOperand(2); 3415 SDValue ARMcc; 3416 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 3417 3418 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 3419 3420 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 3421 DAG.getConstant(VTBits, MVT::i32), ShAmt); 3422 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 3423 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 3424 DAG.getConstant(VTBits, MVT::i32)); 3425 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 3426 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 3427 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 3428 3429 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3430 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 3431 ARMcc, DAG, dl); 3432 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 3433 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, 3434 CCR, Cmp); 3435 3436 SDValue Ops[2] = { Lo, Hi }; 3437 return DAG.getMergeValues(Ops, 2, dl); 3438 } 3439 3440 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 3441 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 3442 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 3443 SelectionDAG &DAG) const { 3444 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 3445 EVT VT = Op.getValueType(); 3446 unsigned VTBits = VT.getSizeInBits(); 3447 DebugLoc dl = Op.getDebugLoc(); 3448 SDValue ShOpLo = Op.getOperand(0); 3449 SDValue ShOpHi = Op.getOperand(1); 3450 SDValue ShAmt = Op.getOperand(2); 3451 SDValue ARMcc; 3452 3453 assert(Op.getOpcode() == ISD::SHL_PARTS); 3454 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 3455 DAG.getConstant(VTBits, MVT::i32), ShAmt); 3456 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 3457 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 3458 DAG.getConstant(VTBits, MVT::i32)); 3459 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 3460 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 3461 3462 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 3463 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3464 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 3465 ARMcc, DAG, dl); 3466 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 3467 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc, 3468 CCR, Cmp); 3469 3470 SDValue Ops[2] = { Lo, Hi }; 3471 return DAG.getMergeValues(Ops, 2, dl); 3472 } 3473 3474 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 3475 SelectionDAG &DAG) const { 3476 // The rounding mode is in bits 23:22 of the FPSCR. 3477 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 3478 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 3479 // so that the shift + and get folded into a bitfield extract. 3480 DebugLoc dl = Op.getDebugLoc(); 3481 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 3482 DAG.getConstant(Intrinsic::arm_get_fpscr, 3483 MVT::i32)); 3484 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 3485 DAG.getConstant(1U << 22, MVT::i32)); 3486 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 3487 DAG.getConstant(22, MVT::i32)); 3488 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 3489 DAG.getConstant(3, MVT::i32)); 3490 } 3491 3492 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 3493 const ARMSubtarget *ST) { 3494 EVT VT = N->getValueType(0); 3495 DebugLoc dl = N->getDebugLoc(); 3496 3497 if (!ST->hasV6T2Ops()) 3498 return SDValue(); 3499 3500 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0)); 3501 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 3502 } 3503 3504 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 3505 const ARMSubtarget *ST) { 3506 EVT VT = N->getValueType(0); 3507 DebugLoc dl = N->getDebugLoc(); 3508 3509 if (!VT.isVector()) 3510 return SDValue(); 3511 3512 // Lower vector shifts on NEON to use VSHL. 3513 assert(ST->hasNEON() && "unexpected vector shift"); 3514 3515 // Left shifts translate directly to the vshiftu intrinsic. 3516 if (N->getOpcode() == ISD::SHL) 3517 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 3518 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32), 3519 N->getOperand(0), N->getOperand(1)); 3520 3521 assert((N->getOpcode() == ISD::SRA || 3522 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 3523 3524 // NEON uses the same intrinsics for both left and right shifts. For 3525 // right shifts, the shift amounts are negative, so negate the vector of 3526 // shift amounts. 3527 EVT ShiftVT = N->getOperand(1).getValueType(); 3528 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 3529 getZeroVector(ShiftVT, DAG, dl), 3530 N->getOperand(1)); 3531 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 3532 Intrinsic::arm_neon_vshifts : 3533 Intrinsic::arm_neon_vshiftu); 3534 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 3535 DAG.getConstant(vshiftInt, MVT::i32), 3536 N->getOperand(0), NegatedCount); 3537 } 3538 3539 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 3540 const ARMSubtarget *ST) { 3541 EVT VT = N->getValueType(0); 3542 DebugLoc dl = N->getDebugLoc(); 3543 3544 // We can get here for a node like i32 = ISD::SHL i32, i64 3545 if (VT != MVT::i64) 3546 return SDValue(); 3547 3548 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 3549 "Unknown shift to lower!"); 3550 3551 // We only lower SRA, SRL of 1 here, all others use generic lowering. 3552 if (!isa<ConstantSDNode>(N->getOperand(1)) || 3553 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1) 3554 return SDValue(); 3555 3556 // If we are in thumb mode, we don't have RRX. 3557 if (ST->isThumb1Only()) return SDValue(); 3558 3559 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 3560 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 3561 DAG.getConstant(0, MVT::i32)); 3562 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 3563 DAG.getConstant(1, MVT::i32)); 3564 3565 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 3566 // captures the result into a carry flag. 3567 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 3568 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1); 3569 3570 // The low part is an ARMISD::RRX operand, which shifts the carry in. 3571 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 3572 3573 // Merge the pieces into a single i64 value. 3574 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 3575 } 3576 3577 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 3578 SDValue TmpOp0, TmpOp1; 3579 bool Invert = false; 3580 bool Swap = false; 3581 unsigned Opc = 0; 3582 3583 SDValue Op0 = Op.getOperand(0); 3584 SDValue Op1 = Op.getOperand(1); 3585 SDValue CC = Op.getOperand(2); 3586 EVT VT = Op.getValueType(); 3587 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 3588 DebugLoc dl = Op.getDebugLoc(); 3589 3590 if (Op.getOperand(1).getValueType().isFloatingPoint()) { 3591 switch (SetCCOpcode) { 3592 default: llvm_unreachable("Illegal FP comparison"); 3593 case ISD::SETUNE: 3594 case ISD::SETNE: Invert = true; // Fallthrough 3595 case ISD::SETOEQ: 3596 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 3597 case ISD::SETOLT: 3598 case ISD::SETLT: Swap = true; // Fallthrough 3599 case ISD::SETOGT: 3600 case ISD::SETGT: Opc = ARMISD::VCGT; break; 3601 case ISD::SETOLE: 3602 case ISD::SETLE: Swap = true; // Fallthrough 3603 case ISD::SETOGE: 3604 case ISD::SETGE: Opc = ARMISD::VCGE; break; 3605 case ISD::SETUGE: Swap = true; // Fallthrough 3606 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 3607 case ISD::SETUGT: Swap = true; // Fallthrough 3608 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 3609 case ISD::SETUEQ: Invert = true; // Fallthrough 3610 case ISD::SETONE: 3611 // Expand this to (OLT | OGT). 3612 TmpOp0 = Op0; 3613 TmpOp1 = Op1; 3614 Opc = ISD::OR; 3615 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 3616 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1); 3617 break; 3618 case ISD::SETUO: Invert = true; // Fallthrough 3619 case ISD::SETO: 3620 // Expand this to (OLT | OGE). 3621 TmpOp0 = Op0; 3622 TmpOp1 = Op1; 3623 Opc = ISD::OR; 3624 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 3625 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1); 3626 break; 3627 } 3628 } else { 3629 // Integer comparisons. 3630 switch (SetCCOpcode) { 3631 default: llvm_unreachable("Illegal integer comparison"); 3632 case ISD::SETNE: Invert = true; 3633 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 3634 case ISD::SETLT: Swap = true; 3635 case ISD::SETGT: Opc = ARMISD::VCGT; break; 3636 case ISD::SETLE: Swap = true; 3637 case ISD::SETGE: Opc = ARMISD::VCGE; break; 3638 case ISD::SETULT: Swap = true; 3639 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 3640 case ISD::SETULE: Swap = true; 3641 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 3642 } 3643 3644 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 3645 if (Opc == ARMISD::VCEQ) { 3646 3647 SDValue AndOp; 3648 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 3649 AndOp = Op0; 3650 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 3651 AndOp = Op1; 3652 3653 // Ignore bitconvert. 3654 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 3655 AndOp = AndOp.getOperand(0); 3656 3657 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 3658 Opc = ARMISD::VTST; 3659 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0)); 3660 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1)); 3661 Invert = !Invert; 3662 } 3663 } 3664 } 3665 3666 if (Swap) 3667 std::swap(Op0, Op1); 3668 3669 // If one of the operands is a constant vector zero, attempt to fold the 3670 // comparison to a specialized compare-against-zero form. 3671 SDValue SingleOp; 3672 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 3673 SingleOp = Op0; 3674 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 3675 if (Opc == ARMISD::VCGE) 3676 Opc = ARMISD::VCLEZ; 3677 else if (Opc == ARMISD::VCGT) 3678 Opc = ARMISD::VCLTZ; 3679 SingleOp = Op1; 3680 } 3681 3682 SDValue Result; 3683 if (SingleOp.getNode()) { 3684 switch (Opc) { 3685 case ARMISD::VCEQ: 3686 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break; 3687 case ARMISD::VCGE: 3688 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break; 3689 case ARMISD::VCLEZ: 3690 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break; 3691 case ARMISD::VCGT: 3692 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break; 3693 case ARMISD::VCLTZ: 3694 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break; 3695 default: 3696 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 3697 } 3698 } else { 3699 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 3700 } 3701 3702 if (Invert) 3703 Result = DAG.getNOT(dl, Result, VT); 3704 3705 return Result; 3706 } 3707 3708 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 3709 /// valid vector constant for a NEON instruction with a "modified immediate" 3710 /// operand (e.g., VMOV). If so, return the encoded value. 3711 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 3712 unsigned SplatBitSize, SelectionDAG &DAG, 3713 EVT &VT, bool is128Bits, NEONModImmType type) { 3714 unsigned OpCmode, Imm; 3715 3716 // SplatBitSize is set to the smallest size that splats the vector, so a 3717 // zero vector will always have SplatBitSize == 8. However, NEON modified 3718 // immediate instructions others than VMOV do not support the 8-bit encoding 3719 // of a zero vector, and the default encoding of zero is supposed to be the 3720 // 32-bit version. 3721 if (SplatBits == 0) 3722 SplatBitSize = 32; 3723 3724 switch (SplatBitSize) { 3725 case 8: 3726 if (type != VMOVModImm) 3727 return SDValue(); 3728 // Any 1-byte value is OK. Op=0, Cmode=1110. 3729 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 3730 OpCmode = 0xe; 3731 Imm = SplatBits; 3732 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 3733 break; 3734 3735 case 16: 3736 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 3737 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 3738 if ((SplatBits & ~0xff) == 0) { 3739 // Value = 0x00nn: Op=x, Cmode=100x. 3740 OpCmode = 0x8; 3741 Imm = SplatBits; 3742 break; 3743 } 3744 if ((SplatBits & ~0xff00) == 0) { 3745 // Value = 0xnn00: Op=x, Cmode=101x. 3746 OpCmode = 0xa; 3747 Imm = SplatBits >> 8; 3748 break; 3749 } 3750 return SDValue(); 3751 3752 case 32: 3753 // NEON's 32-bit VMOV supports splat values where: 3754 // * only one byte is nonzero, or 3755 // * the least significant byte is 0xff and the second byte is nonzero, or 3756 // * the least significant 2 bytes are 0xff and the third is nonzero. 3757 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 3758 if ((SplatBits & ~0xff) == 0) { 3759 // Value = 0x000000nn: Op=x, Cmode=000x. 3760 OpCmode = 0; 3761 Imm = SplatBits; 3762 break; 3763 } 3764 if ((SplatBits & ~0xff00) == 0) { 3765 // Value = 0x0000nn00: Op=x, Cmode=001x. 3766 OpCmode = 0x2; 3767 Imm = SplatBits >> 8; 3768 break; 3769 } 3770 if ((SplatBits & ~0xff0000) == 0) { 3771 // Value = 0x00nn0000: Op=x, Cmode=010x. 3772 OpCmode = 0x4; 3773 Imm = SplatBits >> 16; 3774 break; 3775 } 3776 if ((SplatBits & ~0xff000000) == 0) { 3777 // Value = 0xnn000000: Op=x, Cmode=011x. 3778 OpCmode = 0x6; 3779 Imm = SplatBits >> 24; 3780 break; 3781 } 3782 3783 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 3784 if (type == OtherModImm) return SDValue(); 3785 3786 if ((SplatBits & ~0xffff) == 0 && 3787 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 3788 // Value = 0x0000nnff: Op=x, Cmode=1100. 3789 OpCmode = 0xc; 3790 Imm = SplatBits >> 8; 3791 SplatBits |= 0xff; 3792 break; 3793 } 3794 3795 if ((SplatBits & ~0xffffff) == 0 && 3796 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 3797 // Value = 0x00nnffff: Op=x, Cmode=1101. 3798 OpCmode = 0xd; 3799 Imm = SplatBits >> 16; 3800 SplatBits |= 0xffff; 3801 break; 3802 } 3803 3804 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 3805 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 3806 // VMOV.I32. A (very) minor optimization would be to replicate the value 3807 // and fall through here to test for a valid 64-bit splat. But, then the 3808 // caller would also need to check and handle the change in size. 3809 return SDValue(); 3810 3811 case 64: { 3812 if (type != VMOVModImm) 3813 return SDValue(); 3814 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 3815 uint64_t BitMask = 0xff; 3816 uint64_t Val = 0; 3817 unsigned ImmMask = 1; 3818 Imm = 0; 3819 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 3820 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 3821 Val |= BitMask; 3822 Imm |= ImmMask; 3823 } else if ((SplatBits & BitMask) != 0) { 3824 return SDValue(); 3825 } 3826 BitMask <<= 8; 3827 ImmMask <<= 1; 3828 } 3829 // Op=1, Cmode=1110. 3830 OpCmode = 0x1e; 3831 SplatBits = Val; 3832 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 3833 break; 3834 } 3835 3836 default: 3837 llvm_unreachable("unexpected size for isNEONModifiedImm"); 3838 } 3839 3840 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 3841 return DAG.getTargetConstant(EncodedVal, MVT::i32); 3842 } 3843 3844 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 3845 const ARMSubtarget *ST) const { 3846 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16()) 3847 return SDValue(); 3848 3849 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 3850 assert(Op.getValueType() == MVT::f32 && 3851 "ConstantFP custom lowering should only occur for f32."); 3852 3853 // Try splatting with a VMOV.f32... 3854 APFloat FPVal = CFP->getValueAPF(); 3855 int ImmVal = ARM_AM::getFP32Imm(FPVal); 3856 if (ImmVal != -1) { 3857 DebugLoc DL = Op.getDebugLoc(); 3858 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32); 3859 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 3860 NewVal); 3861 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 3862 DAG.getConstant(0, MVT::i32)); 3863 } 3864 3865 // If that fails, try a VMOV.i32 3866 EVT VMovVT; 3867 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue(); 3868 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false, 3869 VMOVModImm); 3870 if (NewVal != SDValue()) { 3871 DebugLoc DL = Op.getDebugLoc(); 3872 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 3873 NewVal); 3874 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 3875 VecConstant); 3876 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 3877 DAG.getConstant(0, MVT::i32)); 3878 } 3879 3880 // Finally, try a VMVN.i32 3881 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false, 3882 VMVNModImm); 3883 if (NewVal != SDValue()) { 3884 DebugLoc DL = Op.getDebugLoc(); 3885 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 3886 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 3887 VecConstant); 3888 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 3889 DAG.getConstant(0, MVT::i32)); 3890 } 3891 3892 return SDValue(); 3893 } 3894 3895 3896 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 3897 bool &ReverseVEXT, unsigned &Imm) { 3898 unsigned NumElts = VT.getVectorNumElements(); 3899 ReverseVEXT = false; 3900 3901 // Assume that the first shuffle index is not UNDEF. Fail if it is. 3902 if (M[0] < 0) 3903 return false; 3904 3905 Imm = M[0]; 3906 3907 // If this is a VEXT shuffle, the immediate value is the index of the first 3908 // element. The other shuffle indices must be the successive elements after 3909 // the first one. 3910 unsigned ExpectedElt = Imm; 3911 for (unsigned i = 1; i < NumElts; ++i) { 3912 // Increment the expected index. If it wraps around, it may still be 3913 // a VEXT but the source vectors must be swapped. 3914 ExpectedElt += 1; 3915 if (ExpectedElt == NumElts * 2) { 3916 ExpectedElt = 0; 3917 ReverseVEXT = true; 3918 } 3919 3920 if (M[i] < 0) continue; // ignore UNDEF indices 3921 if (ExpectedElt != static_cast<unsigned>(M[i])) 3922 return false; 3923 } 3924 3925 // Adjust the index value if the source operands will be swapped. 3926 if (ReverseVEXT) 3927 Imm -= NumElts; 3928 3929 return true; 3930 } 3931 3932 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 3933 /// instruction with the specified blocksize. (The order of the elements 3934 /// within each block of the vector is reversed.) 3935 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 3936 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 3937 "Only possible block sizes for VREV are: 16, 32, 64"); 3938 3939 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3940 if (EltSz == 64) 3941 return false; 3942 3943 unsigned NumElts = VT.getVectorNumElements(); 3944 unsigned BlockElts = M[0] + 1; 3945 // If the first shuffle index is UNDEF, be optimistic. 3946 if (M[0] < 0) 3947 BlockElts = BlockSize / EltSz; 3948 3949 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 3950 return false; 3951 3952 for (unsigned i = 0; i < NumElts; ++i) { 3953 if (M[i] < 0) continue; // ignore UNDEF indices 3954 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 3955 return false; 3956 } 3957 3958 return true; 3959 } 3960 3961 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 3962 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 3963 // range, then 0 is placed into the resulting vector. So pretty much any mask 3964 // of 8 elements can work here. 3965 return VT == MVT::v8i8 && M.size() == 8; 3966 } 3967 3968 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 3969 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3970 if (EltSz == 64) 3971 return false; 3972 3973 unsigned NumElts = VT.getVectorNumElements(); 3974 WhichResult = (M[0] == 0 ? 0 : 1); 3975 for (unsigned i = 0; i < NumElts; i += 2) { 3976 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 3977 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult)) 3978 return false; 3979 } 3980 return true; 3981 } 3982 3983 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 3984 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 3985 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 3986 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 3987 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3988 if (EltSz == 64) 3989 return false; 3990 3991 unsigned NumElts = VT.getVectorNumElements(); 3992 WhichResult = (M[0] == 0 ? 0 : 1); 3993 for (unsigned i = 0; i < NumElts; i += 2) { 3994 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 3995 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult)) 3996 return false; 3997 } 3998 return true; 3999 } 4000 4001 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4002 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4003 if (EltSz == 64) 4004 return false; 4005 4006 unsigned NumElts = VT.getVectorNumElements(); 4007 WhichResult = (M[0] == 0 ? 0 : 1); 4008 for (unsigned i = 0; i != NumElts; ++i) { 4009 if (M[i] < 0) continue; // ignore UNDEF indices 4010 if ((unsigned) M[i] != 2 * i + WhichResult) 4011 return false; 4012 } 4013 4014 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4015 if (VT.is64BitVector() && EltSz == 32) 4016 return false; 4017 4018 return true; 4019 } 4020 4021 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 4022 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4023 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 4024 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4025 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4026 if (EltSz == 64) 4027 return false; 4028 4029 unsigned Half = VT.getVectorNumElements() / 2; 4030 WhichResult = (M[0] == 0 ? 0 : 1); 4031 for (unsigned j = 0; j != 2; ++j) { 4032 unsigned Idx = WhichResult; 4033 for (unsigned i = 0; i != Half; ++i) { 4034 int MIdx = M[i + j * Half]; 4035 if (MIdx >= 0 && (unsigned) MIdx != Idx) 4036 return false; 4037 Idx += 2; 4038 } 4039 } 4040 4041 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4042 if (VT.is64BitVector() && EltSz == 32) 4043 return false; 4044 4045 return true; 4046 } 4047 4048 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4049 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4050 if (EltSz == 64) 4051 return false; 4052 4053 unsigned NumElts = VT.getVectorNumElements(); 4054 WhichResult = (M[0] == 0 ? 0 : 1); 4055 unsigned Idx = WhichResult * NumElts / 2; 4056 for (unsigned i = 0; i != NumElts; i += 2) { 4057 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 4058 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts)) 4059 return false; 4060 Idx += 1; 4061 } 4062 4063 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4064 if (VT.is64BitVector() && EltSz == 32) 4065 return false; 4066 4067 return true; 4068 } 4069 4070 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 4071 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4072 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 4073 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4074 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4075 if (EltSz == 64) 4076 return false; 4077 4078 unsigned NumElts = VT.getVectorNumElements(); 4079 WhichResult = (M[0] == 0 ? 0 : 1); 4080 unsigned Idx = WhichResult * NumElts / 2; 4081 for (unsigned i = 0; i != NumElts; i += 2) { 4082 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 4083 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx)) 4084 return false; 4085 Idx += 1; 4086 } 4087 4088 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4089 if (VT.is64BitVector() && EltSz == 32) 4090 return false; 4091 4092 return true; 4093 } 4094 4095 // If N is an integer constant that can be moved into a register in one 4096 // instruction, return an SDValue of such a constant (will become a MOV 4097 // instruction). Otherwise return null. 4098 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 4099 const ARMSubtarget *ST, DebugLoc dl) { 4100 uint64_t Val; 4101 if (!isa<ConstantSDNode>(N)) 4102 return SDValue(); 4103 Val = cast<ConstantSDNode>(N)->getZExtValue(); 4104 4105 if (ST->isThumb1Only()) { 4106 if (Val <= 255 || ~Val <= 255) 4107 return DAG.getConstant(Val, MVT::i32); 4108 } else { 4109 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 4110 return DAG.getConstant(Val, MVT::i32); 4111 } 4112 return SDValue(); 4113 } 4114 4115 // If this is a case we can't handle, return null and let the default 4116 // expansion code take care of it. 4117 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 4118 const ARMSubtarget *ST) const { 4119 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 4120 DebugLoc dl = Op.getDebugLoc(); 4121 EVT VT = Op.getValueType(); 4122 4123 APInt SplatBits, SplatUndef; 4124 unsigned SplatBitSize; 4125 bool HasAnyUndefs; 4126 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 4127 if (SplatBitSize <= 64) { 4128 // Check if an immediate VMOV works. 4129 EVT VmovVT; 4130 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 4131 SplatUndef.getZExtValue(), SplatBitSize, 4132 DAG, VmovVT, VT.is128BitVector(), 4133 VMOVModImm); 4134 if (Val.getNode()) { 4135 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 4136 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4137 } 4138 4139 // Try an immediate VMVN. 4140 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 4141 Val = isNEONModifiedImm(NegatedImm, 4142 SplatUndef.getZExtValue(), SplatBitSize, 4143 DAG, VmovVT, VT.is128BitVector(), 4144 VMVNModImm); 4145 if (Val.getNode()) { 4146 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 4147 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4148 } 4149 4150 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 4151 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 4152 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 4153 if (ImmVal != -1) { 4154 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32); 4155 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 4156 } 4157 } 4158 } 4159 } 4160 4161 // Scan through the operands to see if only one value is used. 4162 unsigned NumElts = VT.getVectorNumElements(); 4163 bool isOnlyLowElement = true; 4164 bool usesOnlyOneValue = true; 4165 bool isConstant = true; 4166 SDValue Value; 4167 for (unsigned i = 0; i < NumElts; ++i) { 4168 SDValue V = Op.getOperand(i); 4169 if (V.getOpcode() == ISD::UNDEF) 4170 continue; 4171 if (i > 0) 4172 isOnlyLowElement = false; 4173 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 4174 isConstant = false; 4175 4176 if (!Value.getNode()) 4177 Value = V; 4178 else if (V != Value) 4179 usesOnlyOneValue = false; 4180 } 4181 4182 if (!Value.getNode()) 4183 return DAG.getUNDEF(VT); 4184 4185 if (isOnlyLowElement) 4186 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 4187 4188 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4189 4190 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 4191 // i32 and try again. 4192 if (usesOnlyOneValue && EltSize <= 32) { 4193 if (!isConstant) 4194 return DAG.getNode(ARMISD::VDUP, dl, VT, Value); 4195 if (VT.getVectorElementType().isFloatingPoint()) { 4196 SmallVector<SDValue, 8> Ops; 4197 for (unsigned i = 0; i < NumElts; ++i) 4198 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 4199 Op.getOperand(i))); 4200 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 4201 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts); 4202 Val = LowerBUILD_VECTOR(Val, DAG, ST); 4203 if (Val.getNode()) 4204 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4205 } 4206 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 4207 if (Val.getNode()) 4208 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 4209 } 4210 4211 // If all elements are constants and the case above didn't get hit, fall back 4212 // to the default expansion, which will generate a load from the constant 4213 // pool. 4214 if (isConstant) 4215 return SDValue(); 4216 4217 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 4218 if (NumElts >= 4) { 4219 SDValue shuffle = ReconstructShuffle(Op, DAG); 4220 if (shuffle != SDValue()) 4221 return shuffle; 4222 } 4223 4224 // Vectors with 32- or 64-bit elements can be built by directly assigning 4225 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 4226 // will be legalized. 4227 if (EltSize >= 32) { 4228 // Do the expansion with floating-point types, since that is what the VFP 4229 // registers are defined to use, and since i64 is not legal. 4230 EVT EltVT = EVT::getFloatingPointVT(EltSize); 4231 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 4232 SmallVector<SDValue, 8> Ops; 4233 for (unsigned i = 0; i < NumElts; ++i) 4234 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 4235 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts); 4236 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4237 } 4238 4239 return SDValue(); 4240 } 4241 4242 // Gather data to see if the operation can be modelled as a 4243 // shuffle in combination with VEXTs. 4244 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 4245 SelectionDAG &DAG) const { 4246 DebugLoc dl = Op.getDebugLoc(); 4247 EVT VT = Op.getValueType(); 4248 unsigned NumElts = VT.getVectorNumElements(); 4249 4250 SmallVector<SDValue, 2> SourceVecs; 4251 SmallVector<unsigned, 2> MinElts; 4252 SmallVector<unsigned, 2> MaxElts; 4253 4254 for (unsigned i = 0; i < NumElts; ++i) { 4255 SDValue V = Op.getOperand(i); 4256 if (V.getOpcode() == ISD::UNDEF) 4257 continue; 4258 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 4259 // A shuffle can only come from building a vector from various 4260 // elements of other vectors. 4261 return SDValue(); 4262 } else if (V.getOperand(0).getValueType().getVectorElementType() != 4263 VT.getVectorElementType()) { 4264 // This code doesn't know how to handle shuffles where the vector 4265 // element types do not match (this happens because type legalization 4266 // promotes the return type of EXTRACT_VECTOR_ELT). 4267 // FIXME: It might be appropriate to extend this code to handle 4268 // mismatched types. 4269 return SDValue(); 4270 } 4271 4272 // Record this extraction against the appropriate vector if possible... 4273 SDValue SourceVec = V.getOperand(0); 4274 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 4275 bool FoundSource = false; 4276 for (unsigned j = 0; j < SourceVecs.size(); ++j) { 4277 if (SourceVecs[j] == SourceVec) { 4278 if (MinElts[j] > EltNo) 4279 MinElts[j] = EltNo; 4280 if (MaxElts[j] < EltNo) 4281 MaxElts[j] = EltNo; 4282 FoundSource = true; 4283 break; 4284 } 4285 } 4286 4287 // Or record a new source if not... 4288 if (!FoundSource) { 4289 SourceVecs.push_back(SourceVec); 4290 MinElts.push_back(EltNo); 4291 MaxElts.push_back(EltNo); 4292 } 4293 } 4294 4295 // Currently only do something sane when at most two source vectors 4296 // involved. 4297 if (SourceVecs.size() > 2) 4298 return SDValue(); 4299 4300 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) }; 4301 int VEXTOffsets[2] = {0, 0}; 4302 4303 // This loop extracts the usage patterns of the source vectors 4304 // and prepares appropriate SDValues for a shuffle if possible. 4305 for (unsigned i = 0; i < SourceVecs.size(); ++i) { 4306 if (SourceVecs[i].getValueType() == VT) { 4307 // No VEXT necessary 4308 ShuffleSrcs[i] = SourceVecs[i]; 4309 VEXTOffsets[i] = 0; 4310 continue; 4311 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) { 4312 // It probably isn't worth padding out a smaller vector just to 4313 // break it down again in a shuffle. 4314 return SDValue(); 4315 } 4316 4317 // Since only 64-bit and 128-bit vectors are legal on ARM and 4318 // we've eliminated the other cases... 4319 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts && 4320 "unexpected vector sizes in ReconstructShuffle"); 4321 4322 if (MaxElts[i] - MinElts[i] >= NumElts) { 4323 // Span too large for a VEXT to cope 4324 return SDValue(); 4325 } 4326 4327 if (MinElts[i] >= NumElts) { 4328 // The extraction can just take the second half 4329 VEXTOffsets[i] = NumElts; 4330 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4331 SourceVecs[i], 4332 DAG.getIntPtrConstant(NumElts)); 4333 } else if (MaxElts[i] < NumElts) { 4334 // The extraction can just take the first half 4335 VEXTOffsets[i] = 0; 4336 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4337 SourceVecs[i], 4338 DAG.getIntPtrConstant(0)); 4339 } else { 4340 // An actual VEXT is needed 4341 VEXTOffsets[i] = MinElts[i]; 4342 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4343 SourceVecs[i], 4344 DAG.getIntPtrConstant(0)); 4345 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4346 SourceVecs[i], 4347 DAG.getIntPtrConstant(NumElts)); 4348 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2, 4349 DAG.getConstant(VEXTOffsets[i], MVT::i32)); 4350 } 4351 } 4352 4353 SmallVector<int, 8> Mask; 4354 4355 for (unsigned i = 0; i < NumElts; ++i) { 4356 SDValue Entry = Op.getOperand(i); 4357 if (Entry.getOpcode() == ISD::UNDEF) { 4358 Mask.push_back(-1); 4359 continue; 4360 } 4361 4362 SDValue ExtractVec = Entry.getOperand(0); 4363 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i) 4364 .getOperand(1))->getSExtValue(); 4365 if (ExtractVec == SourceVecs[0]) { 4366 Mask.push_back(ExtractElt - VEXTOffsets[0]); 4367 } else { 4368 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]); 4369 } 4370 } 4371 4372 // Final check before we try to produce nonsense... 4373 if (isShuffleMaskLegal(Mask, VT)) 4374 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], 4375 &Mask[0]); 4376 4377 return SDValue(); 4378 } 4379 4380 /// isShuffleMaskLegal - Targets can use this to indicate that they only 4381 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 4382 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 4383 /// are assumed to be legal. 4384 bool 4385 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 4386 EVT VT) const { 4387 if (VT.getVectorNumElements() == 4 && 4388 (VT.is128BitVector() || VT.is64BitVector())) { 4389 unsigned PFIndexes[4]; 4390 for (unsigned i = 0; i != 4; ++i) { 4391 if (M[i] < 0) 4392 PFIndexes[i] = 8; 4393 else 4394 PFIndexes[i] = M[i]; 4395 } 4396 4397 // Compute the index in the perfect shuffle table. 4398 unsigned PFTableIndex = 4399 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 4400 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 4401 unsigned Cost = (PFEntry >> 30); 4402 4403 if (Cost <= 4) 4404 return true; 4405 } 4406 4407 bool ReverseVEXT; 4408 unsigned Imm, WhichResult; 4409 4410 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4411 return (EltSize >= 32 || 4412 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 4413 isVREVMask(M, VT, 64) || 4414 isVREVMask(M, VT, 32) || 4415 isVREVMask(M, VT, 16) || 4416 isVEXTMask(M, VT, ReverseVEXT, Imm) || 4417 isVTBLMask(M, VT) || 4418 isVTRNMask(M, VT, WhichResult) || 4419 isVUZPMask(M, VT, WhichResult) || 4420 isVZIPMask(M, VT, WhichResult) || 4421 isVTRN_v_undef_Mask(M, VT, WhichResult) || 4422 isVUZP_v_undef_Mask(M, VT, WhichResult) || 4423 isVZIP_v_undef_Mask(M, VT, WhichResult)); 4424 } 4425 4426 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 4427 /// the specified operations to build the shuffle. 4428 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 4429 SDValue RHS, SelectionDAG &DAG, 4430 DebugLoc dl) { 4431 unsigned OpNum = (PFEntry >> 26) & 0x0F; 4432 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 4433 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 4434 4435 enum { 4436 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 4437 OP_VREV, 4438 OP_VDUP0, 4439 OP_VDUP1, 4440 OP_VDUP2, 4441 OP_VDUP3, 4442 OP_VEXT1, 4443 OP_VEXT2, 4444 OP_VEXT3, 4445 OP_VUZPL, // VUZP, left result 4446 OP_VUZPR, // VUZP, right result 4447 OP_VZIPL, // VZIP, left result 4448 OP_VZIPR, // VZIP, right result 4449 OP_VTRNL, // VTRN, left result 4450 OP_VTRNR // VTRN, right result 4451 }; 4452 4453 if (OpNum == OP_COPY) { 4454 if (LHSID == (1*9+2)*9+3) return LHS; 4455 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 4456 return RHS; 4457 } 4458 4459 SDValue OpLHS, OpRHS; 4460 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 4461 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 4462 EVT VT = OpLHS.getValueType(); 4463 4464 switch (OpNum) { 4465 default: llvm_unreachable("Unknown shuffle opcode!"); 4466 case OP_VREV: 4467 // VREV divides the vector in half and swaps within the half. 4468 if (VT.getVectorElementType() == MVT::i32 || 4469 VT.getVectorElementType() == MVT::f32) 4470 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 4471 // vrev <4 x i16> -> VREV32 4472 if (VT.getVectorElementType() == MVT::i16) 4473 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 4474 // vrev <4 x i8> -> VREV16 4475 assert(VT.getVectorElementType() == MVT::i8); 4476 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 4477 case OP_VDUP0: 4478 case OP_VDUP1: 4479 case OP_VDUP2: 4480 case OP_VDUP3: 4481 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 4482 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32)); 4483 case OP_VEXT1: 4484 case OP_VEXT2: 4485 case OP_VEXT3: 4486 return DAG.getNode(ARMISD::VEXT, dl, VT, 4487 OpLHS, OpRHS, 4488 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32)); 4489 case OP_VUZPL: 4490 case OP_VUZPR: 4491 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 4492 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 4493 case OP_VZIPL: 4494 case OP_VZIPR: 4495 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 4496 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 4497 case OP_VTRNL: 4498 case OP_VTRNR: 4499 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 4500 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 4501 } 4502 } 4503 4504 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 4505 ArrayRef<int> ShuffleMask, 4506 SelectionDAG &DAG) { 4507 // Check to see if we can use the VTBL instruction. 4508 SDValue V1 = Op.getOperand(0); 4509 SDValue V2 = Op.getOperand(1); 4510 DebugLoc DL = Op.getDebugLoc(); 4511 4512 SmallVector<SDValue, 8> VTBLMask; 4513 for (ArrayRef<int>::iterator 4514 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 4515 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32)); 4516 4517 if (V2.getNode()->getOpcode() == ISD::UNDEF) 4518 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 4519 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, 4520 &VTBLMask[0], 8)); 4521 4522 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 4523 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, 4524 &VTBLMask[0], 8)); 4525 } 4526 4527 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 4528 SDValue V1 = Op.getOperand(0); 4529 SDValue V2 = Op.getOperand(1); 4530 DebugLoc dl = Op.getDebugLoc(); 4531 EVT VT = Op.getValueType(); 4532 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 4533 4534 // Convert shuffles that are directly supported on NEON to target-specific 4535 // DAG nodes, instead of keeping them as shuffles and matching them again 4536 // during code selection. This is more efficient and avoids the possibility 4537 // of inconsistencies between legalization and selection. 4538 // FIXME: floating-point vectors should be canonicalized to integer vectors 4539 // of the same time so that they get CSEd properly. 4540 ArrayRef<int> ShuffleMask = SVN->getMask(); 4541 4542 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4543 if (EltSize <= 32) { 4544 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 4545 int Lane = SVN->getSplatIndex(); 4546 // If this is undef splat, generate it via "just" vdup, if possible. 4547 if (Lane == -1) Lane = 0; 4548 4549 // Test if V1 is a SCALAR_TO_VECTOR. 4550 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 4551 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 4552 } 4553 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 4554 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 4555 // reaches it). 4556 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 4557 !isa<ConstantSDNode>(V1.getOperand(0))) { 4558 bool IsScalarToVector = true; 4559 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 4560 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) { 4561 IsScalarToVector = false; 4562 break; 4563 } 4564 if (IsScalarToVector) 4565 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 4566 } 4567 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 4568 DAG.getConstant(Lane, MVT::i32)); 4569 } 4570 4571 bool ReverseVEXT; 4572 unsigned Imm; 4573 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 4574 if (ReverseVEXT) 4575 std::swap(V1, V2); 4576 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 4577 DAG.getConstant(Imm, MVT::i32)); 4578 } 4579 4580 if (isVREVMask(ShuffleMask, VT, 64)) 4581 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 4582 if (isVREVMask(ShuffleMask, VT, 32)) 4583 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 4584 if (isVREVMask(ShuffleMask, VT, 16)) 4585 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 4586 4587 // Check for Neon shuffles that modify both input vectors in place. 4588 // If both results are used, i.e., if there are two shuffles with the same 4589 // source operands and with masks corresponding to both results of one of 4590 // these operations, DAG memoization will ensure that a single node is 4591 // used for both shuffles. 4592 unsigned WhichResult; 4593 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 4594 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 4595 V1, V2).getValue(WhichResult); 4596 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 4597 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 4598 V1, V2).getValue(WhichResult); 4599 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 4600 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 4601 V1, V2).getValue(WhichResult); 4602 4603 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 4604 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 4605 V1, V1).getValue(WhichResult); 4606 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 4607 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 4608 V1, V1).getValue(WhichResult); 4609 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 4610 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 4611 V1, V1).getValue(WhichResult); 4612 } 4613 4614 // If the shuffle is not directly supported and it has 4 elements, use 4615 // the PerfectShuffle-generated table to synthesize it from other shuffles. 4616 unsigned NumElts = VT.getVectorNumElements(); 4617 if (NumElts == 4) { 4618 unsigned PFIndexes[4]; 4619 for (unsigned i = 0; i != 4; ++i) { 4620 if (ShuffleMask[i] < 0) 4621 PFIndexes[i] = 8; 4622 else 4623 PFIndexes[i] = ShuffleMask[i]; 4624 } 4625 4626 // Compute the index in the perfect shuffle table. 4627 unsigned PFTableIndex = 4628 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 4629 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 4630 unsigned Cost = (PFEntry >> 30); 4631 4632 if (Cost <= 4) 4633 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 4634 } 4635 4636 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 4637 if (EltSize >= 32) { 4638 // Do the expansion with floating-point types, since that is what the VFP 4639 // registers are defined to use, and since i64 is not legal. 4640 EVT EltVT = EVT::getFloatingPointVT(EltSize); 4641 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 4642 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 4643 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 4644 SmallVector<SDValue, 8> Ops; 4645 for (unsigned i = 0; i < NumElts; ++i) { 4646 if (ShuffleMask[i] < 0) 4647 Ops.push_back(DAG.getUNDEF(EltVT)); 4648 else 4649 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 4650 ShuffleMask[i] < (int)NumElts ? V1 : V2, 4651 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 4652 MVT::i32))); 4653 } 4654 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts); 4655 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4656 } 4657 4658 if (VT == MVT::v8i8) { 4659 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG); 4660 if (NewOp.getNode()) 4661 return NewOp; 4662 } 4663 4664 return SDValue(); 4665 } 4666 4667 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 4668 // INSERT_VECTOR_ELT is legal only for immediate indexes. 4669 SDValue Lane = Op.getOperand(2); 4670 if (!isa<ConstantSDNode>(Lane)) 4671 return SDValue(); 4672 4673 return Op; 4674 } 4675 4676 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 4677 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 4678 SDValue Lane = Op.getOperand(1); 4679 if (!isa<ConstantSDNode>(Lane)) 4680 return SDValue(); 4681 4682 SDValue Vec = Op.getOperand(0); 4683 if (Op.getValueType() == MVT::i32 && 4684 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) { 4685 DebugLoc dl = Op.getDebugLoc(); 4686 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 4687 } 4688 4689 return Op; 4690 } 4691 4692 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 4693 // The only time a CONCAT_VECTORS operation can have legal types is when 4694 // two 64-bit vectors are concatenated to a 128-bit vector. 4695 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 4696 "unexpected CONCAT_VECTORS"); 4697 DebugLoc dl = Op.getDebugLoc(); 4698 SDValue Val = DAG.getUNDEF(MVT::v2f64); 4699 SDValue Op0 = Op.getOperand(0); 4700 SDValue Op1 = Op.getOperand(1); 4701 if (Op0.getOpcode() != ISD::UNDEF) 4702 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 4703 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 4704 DAG.getIntPtrConstant(0)); 4705 if (Op1.getOpcode() != ISD::UNDEF) 4706 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 4707 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 4708 DAG.getIntPtrConstant(1)); 4709 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 4710 } 4711 4712 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 4713 /// element has been zero/sign-extended, depending on the isSigned parameter, 4714 /// from an integer type half its size. 4715 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 4716 bool isSigned) { 4717 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 4718 EVT VT = N->getValueType(0); 4719 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 4720 SDNode *BVN = N->getOperand(0).getNode(); 4721 if (BVN->getValueType(0) != MVT::v4i32 || 4722 BVN->getOpcode() != ISD::BUILD_VECTOR) 4723 return false; 4724 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 4725 unsigned HiElt = 1 - LoElt; 4726 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 4727 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 4728 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 4729 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 4730 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 4731 return false; 4732 if (isSigned) { 4733 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 4734 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 4735 return true; 4736 } else { 4737 if (Hi0->isNullValue() && Hi1->isNullValue()) 4738 return true; 4739 } 4740 return false; 4741 } 4742 4743 if (N->getOpcode() != ISD::BUILD_VECTOR) 4744 return false; 4745 4746 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 4747 SDNode *Elt = N->getOperand(i).getNode(); 4748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 4749 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4750 unsigned HalfSize = EltSize / 2; 4751 if (isSigned) { 4752 if (!isIntN(HalfSize, C->getSExtValue())) 4753 return false; 4754 } else { 4755 if (!isUIntN(HalfSize, C->getZExtValue())) 4756 return false; 4757 } 4758 continue; 4759 } 4760 return false; 4761 } 4762 4763 return true; 4764 } 4765 4766 /// isSignExtended - Check if a node is a vector value that is sign-extended 4767 /// or a constant BUILD_VECTOR with sign-extended elements. 4768 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 4769 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 4770 return true; 4771 if (isExtendedBUILD_VECTOR(N, DAG, true)) 4772 return true; 4773 return false; 4774 } 4775 4776 /// isZeroExtended - Check if a node is a vector value that is zero-extended 4777 /// or a constant BUILD_VECTOR with zero-extended elements. 4778 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 4779 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 4780 return true; 4781 if (isExtendedBUILD_VECTOR(N, DAG, false)) 4782 return true; 4783 return false; 4784 } 4785 4786 /// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending 4787 /// load, or BUILD_VECTOR with extended elements, return the unextended value. 4788 static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) { 4789 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 4790 return N->getOperand(0); 4791 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) 4792 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(), 4793 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(), 4794 LD->isNonTemporal(), LD->isInvariant(), 4795 LD->getAlignment()); 4796 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 4797 // have been legalized as a BITCAST from v4i32. 4798 if (N->getOpcode() == ISD::BITCAST) { 4799 SDNode *BVN = N->getOperand(0).getNode(); 4800 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 4801 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 4802 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 4803 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32, 4804 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2)); 4805 } 4806 // Construct a new BUILD_VECTOR with elements truncated to half the size. 4807 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 4808 EVT VT = N->getValueType(0); 4809 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 4810 unsigned NumElts = VT.getVectorNumElements(); 4811 MVT TruncVT = MVT::getIntegerVT(EltSize); 4812 SmallVector<SDValue, 8> Ops; 4813 for (unsigned i = 0; i != NumElts; ++i) { 4814 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 4815 const APInt &CInt = C->getAPIntValue(); 4816 // Element types smaller than 32 bits are not legal, so use i32 elements. 4817 // The values are implicitly truncated so sext vs. zext doesn't matter. 4818 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32)); 4819 } 4820 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), 4821 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts); 4822 } 4823 4824 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 4825 unsigned Opcode = N->getOpcode(); 4826 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 4827 SDNode *N0 = N->getOperand(0).getNode(); 4828 SDNode *N1 = N->getOperand(1).getNode(); 4829 return N0->hasOneUse() && N1->hasOneUse() && 4830 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 4831 } 4832 return false; 4833 } 4834 4835 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 4836 unsigned Opcode = N->getOpcode(); 4837 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 4838 SDNode *N0 = N->getOperand(0).getNode(); 4839 SDNode *N1 = N->getOperand(1).getNode(); 4840 return N0->hasOneUse() && N1->hasOneUse() && 4841 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 4842 } 4843 return false; 4844 } 4845 4846 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 4847 // Multiplications are only custom-lowered for 128-bit vectors so that 4848 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 4849 EVT VT = Op.getValueType(); 4850 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL"); 4851 SDNode *N0 = Op.getOperand(0).getNode(); 4852 SDNode *N1 = Op.getOperand(1).getNode(); 4853 unsigned NewOpc = 0; 4854 bool isMLA = false; 4855 bool isN0SExt = isSignExtended(N0, DAG); 4856 bool isN1SExt = isSignExtended(N1, DAG); 4857 if (isN0SExt && isN1SExt) 4858 NewOpc = ARMISD::VMULLs; 4859 else { 4860 bool isN0ZExt = isZeroExtended(N0, DAG); 4861 bool isN1ZExt = isZeroExtended(N1, DAG); 4862 if (isN0ZExt && isN1ZExt) 4863 NewOpc = ARMISD::VMULLu; 4864 else if (isN1SExt || isN1ZExt) { 4865 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 4866 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 4867 if (isN1SExt && isAddSubSExt(N0, DAG)) { 4868 NewOpc = ARMISD::VMULLs; 4869 isMLA = true; 4870 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 4871 NewOpc = ARMISD::VMULLu; 4872 isMLA = true; 4873 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 4874 std::swap(N0, N1); 4875 NewOpc = ARMISD::VMULLu; 4876 isMLA = true; 4877 } 4878 } 4879 4880 if (!NewOpc) { 4881 if (VT == MVT::v2i64) 4882 // Fall through to expand this. It is not legal. 4883 return SDValue(); 4884 else 4885 // Other vector multiplications are legal. 4886 return Op; 4887 } 4888 } 4889 4890 // Legalize to a VMULL instruction. 4891 DebugLoc DL = Op.getDebugLoc(); 4892 SDValue Op0; 4893 SDValue Op1 = SkipExtension(N1, DAG); 4894 if (!isMLA) { 4895 Op0 = SkipExtension(N0, DAG); 4896 assert(Op0.getValueType().is64BitVector() && 4897 Op1.getValueType().is64BitVector() && 4898 "unexpected types for extended operands to VMULL"); 4899 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 4900 } 4901 4902 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 4903 // isel lowering to take advantage of no-stall back to back vmul + vmla. 4904 // vmull q0, d4, d6 4905 // vmlal q0, d5, d6 4906 // is faster than 4907 // vaddl q0, d4, d5 4908 // vmovl q1, d6 4909 // vmul q0, q0, q1 4910 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG); 4911 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG); 4912 EVT Op1VT = Op1.getValueType(); 4913 return DAG.getNode(N0->getOpcode(), DL, VT, 4914 DAG.getNode(NewOpc, DL, VT, 4915 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 4916 DAG.getNode(NewOpc, DL, VT, 4917 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 4918 } 4919 4920 static SDValue 4921 LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) { 4922 // Convert to float 4923 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 4924 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 4925 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 4926 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 4927 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 4928 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 4929 // Get reciprocal estimate. 4930 // float4 recip = vrecpeq_f32(yf); 4931 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4932 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y); 4933 // Because char has a smaller range than uchar, we can actually get away 4934 // without any newton steps. This requires that we use a weird bias 4935 // of 0xb000, however (again, this has been exhaustively tested). 4936 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 4937 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 4938 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 4939 Y = DAG.getConstant(0xb000, MVT::i32); 4940 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y); 4941 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 4942 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 4943 // Convert back to short. 4944 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 4945 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 4946 return X; 4947 } 4948 4949 static SDValue 4950 LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) { 4951 SDValue N2; 4952 // Convert to float. 4953 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 4954 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 4955 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 4956 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 4957 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 4958 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 4959 4960 // Use reciprocal estimate and one refinement step. 4961 // float4 recip = vrecpeq_f32(yf); 4962 // recip *= vrecpsq_f32(yf, recip); 4963 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4964 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1); 4965 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4966 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 4967 N1, N2); 4968 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 4969 // Because short has a smaller range than ushort, we can actually get away 4970 // with only a single newton step. This requires that we use a weird bias 4971 // of 89, however (again, this has been exhaustively tested). 4972 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 4973 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 4974 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 4975 N1 = DAG.getConstant(0x89, MVT::i32); 4976 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 4977 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 4978 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 4979 // Convert back to integer and return. 4980 // return vmovn_s32(vcvt_s32_f32(result)); 4981 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 4982 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 4983 return N0; 4984 } 4985 4986 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 4987 EVT VT = Op.getValueType(); 4988 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 4989 "unexpected type for custom-lowering ISD::SDIV"); 4990 4991 DebugLoc dl = Op.getDebugLoc(); 4992 SDValue N0 = Op.getOperand(0); 4993 SDValue N1 = Op.getOperand(1); 4994 SDValue N2, N3; 4995 4996 if (VT == MVT::v8i8) { 4997 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 4998 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 4999 5000 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5001 DAG.getIntPtrConstant(4)); 5002 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5003 DAG.getIntPtrConstant(4)); 5004 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5005 DAG.getIntPtrConstant(0)); 5006 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5007 DAG.getIntPtrConstant(0)); 5008 5009 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 5010 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 5011 5012 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 5013 N0 = LowerCONCAT_VECTORS(N0, DAG); 5014 5015 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 5016 return N0; 5017 } 5018 return LowerSDIV_v4i16(N0, N1, dl, DAG); 5019 } 5020 5021 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 5022 EVT VT = Op.getValueType(); 5023 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 5024 "unexpected type for custom-lowering ISD::UDIV"); 5025 5026 DebugLoc dl = Op.getDebugLoc(); 5027 SDValue N0 = Op.getOperand(0); 5028 SDValue N1 = Op.getOperand(1); 5029 SDValue N2, N3; 5030 5031 if (VT == MVT::v8i8) { 5032 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 5033 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 5034 5035 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5036 DAG.getIntPtrConstant(4)); 5037 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5038 DAG.getIntPtrConstant(4)); 5039 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 5040 DAG.getIntPtrConstant(0)); 5041 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 5042 DAG.getIntPtrConstant(0)); 5043 5044 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 5045 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 5046 5047 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 5048 N0 = LowerCONCAT_VECTORS(N0, DAG); 5049 5050 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 5051 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32), 5052 N0); 5053 return N0; 5054 } 5055 5056 // v4i16 sdiv ... Convert to float. 5057 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 5058 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 5059 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 5060 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 5061 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 5062 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 5063 5064 // Use reciprocal estimate and two refinement steps. 5065 // float4 recip = vrecpeq_f32(yf); 5066 // recip *= vrecpsq_f32(yf, recip); 5067 // recip *= vrecpsq_f32(yf, recip); 5068 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5069 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1); 5070 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5071 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 5072 BN1, N2); 5073 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 5074 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 5075 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 5076 BN1, N2); 5077 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 5078 // Simply multiplying by the reciprocal estimate can leave us a few ulps 5079 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 5080 // and that it will never cause us to return an answer too large). 5081 // float4 result = as_float4(as_int4(xf*recip) + 2); 5082 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 5083 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 5084 N1 = DAG.getConstant(2, MVT::i32); 5085 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 5086 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 5087 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 5088 // Convert back to integer and return. 5089 // return vmovn_u32(vcvt_s32_f32(result)); 5090 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 5091 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 5092 return N0; 5093 } 5094 5095 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 5096 EVT VT = Op.getNode()->getValueType(0); 5097 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 5098 5099 unsigned Opc; 5100 bool ExtraOp = false; 5101 switch (Op.getOpcode()) { 5102 default: llvm_unreachable("Invalid code"); 5103 case ISD::ADDC: Opc = ARMISD::ADDC; break; 5104 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 5105 case ISD::SUBC: Opc = ARMISD::SUBC; break; 5106 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 5107 } 5108 5109 if (!ExtraOp) 5110 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0), 5111 Op.getOperand(1)); 5112 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0), 5113 Op.getOperand(1), Op.getOperand(2)); 5114 } 5115 5116 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 5117 // Monotonic load/store is legal for all targets 5118 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic) 5119 return Op; 5120 5121 // Aquire/Release load/store is not legal for targets without a 5122 // dmb or equivalent available. 5123 return SDValue(); 5124 } 5125 5126 5127 static void 5128 ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results, 5129 SelectionDAG &DAG, unsigned NewOp) { 5130 DebugLoc dl = Node->getDebugLoc(); 5131 assert (Node->getValueType(0) == MVT::i64 && 5132 "Only know how to expand i64 atomics"); 5133 5134 SmallVector<SDValue, 6> Ops; 5135 Ops.push_back(Node->getOperand(0)); // Chain 5136 Ops.push_back(Node->getOperand(1)); // Ptr 5137 // Low part of Val1 5138 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5139 Node->getOperand(2), DAG.getIntPtrConstant(0))); 5140 // High part of Val1 5141 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5142 Node->getOperand(2), DAG.getIntPtrConstant(1))); 5143 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) { 5144 // High part of Val1 5145 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5146 Node->getOperand(3), DAG.getIntPtrConstant(0))); 5147 // High part of Val2 5148 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 5149 Node->getOperand(3), DAG.getIntPtrConstant(1))); 5150 } 5151 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 5152 SDValue Result = 5153 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64, 5154 cast<MemSDNode>(Node)->getMemOperand()); 5155 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) }; 5156 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); 5157 Results.push_back(Result.getValue(2)); 5158 } 5159 5160 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 5161 switch (Op.getOpcode()) { 5162 default: llvm_unreachable("Don't know how to custom lower this!"); 5163 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 5164 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 5165 case ISD::GlobalAddress: 5166 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) : 5167 LowerGlobalAddressELF(Op, DAG); 5168 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 5169 case ISD::SELECT: return LowerSELECT(Op, DAG); 5170 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 5171 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 5172 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 5173 case ISD::VASTART: return LowerVASTART(Op, DAG); 5174 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget); 5175 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 5176 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 5177 case ISD::SINT_TO_FP: 5178 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 5179 case ISD::FP_TO_SINT: 5180 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 5181 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 5182 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 5183 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 5184 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG); 5185 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 5186 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 5187 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 5188 Subtarget); 5189 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 5190 case ISD::SHL: 5191 case ISD::SRL: 5192 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 5193 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 5194 case ISD::SRL_PARTS: 5195 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 5196 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 5197 case ISD::SETCC: return LowerVSETCC(Op, DAG); 5198 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 5199 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 5200 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 5201 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 5202 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 5203 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 5204 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 5205 case ISD::MUL: return LowerMUL(Op, DAG); 5206 case ISD::SDIV: return LowerSDIV(Op, DAG); 5207 case ISD::UDIV: return LowerUDIV(Op, DAG); 5208 case ISD::ADDC: 5209 case ISD::ADDE: 5210 case ISD::SUBC: 5211 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 5212 case ISD::ATOMIC_LOAD: 5213 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 5214 } 5215 } 5216 5217 /// ReplaceNodeResults - Replace the results of node with an illegal result 5218 /// type with new values built out of custom code. 5219 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 5220 SmallVectorImpl<SDValue>&Results, 5221 SelectionDAG &DAG) const { 5222 SDValue Res; 5223 switch (N->getOpcode()) { 5224 default: 5225 llvm_unreachable("Don't know how to custom expand this!"); 5226 case ISD::BITCAST: 5227 Res = ExpandBITCAST(N, DAG); 5228 break; 5229 case ISD::SRL: 5230 case ISD::SRA: 5231 Res = Expand64BitShift(N, DAG, Subtarget); 5232 break; 5233 case ISD::ATOMIC_LOAD_ADD: 5234 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG); 5235 return; 5236 case ISD::ATOMIC_LOAD_AND: 5237 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG); 5238 return; 5239 case ISD::ATOMIC_LOAD_NAND: 5240 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG); 5241 return; 5242 case ISD::ATOMIC_LOAD_OR: 5243 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG); 5244 return; 5245 case ISD::ATOMIC_LOAD_SUB: 5246 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG); 5247 return; 5248 case ISD::ATOMIC_LOAD_XOR: 5249 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG); 5250 return; 5251 case ISD::ATOMIC_SWAP: 5252 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG); 5253 return; 5254 case ISD::ATOMIC_CMP_SWAP: 5255 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG); 5256 return; 5257 } 5258 if (Res.getNode()) 5259 Results.push_back(Res); 5260 } 5261 5262 //===----------------------------------------------------------------------===// 5263 // ARM Scheduler Hooks 5264 //===----------------------------------------------------------------------===// 5265 5266 MachineBasicBlock * 5267 ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI, 5268 MachineBasicBlock *BB, 5269 unsigned Size) const { 5270 unsigned dest = MI->getOperand(0).getReg(); 5271 unsigned ptr = MI->getOperand(1).getReg(); 5272 unsigned oldval = MI->getOperand(2).getReg(); 5273 unsigned newval = MI->getOperand(3).getReg(); 5274 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5275 DebugLoc dl = MI->getDebugLoc(); 5276 bool isThumb2 = Subtarget->isThumb2(); 5277 5278 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5279 unsigned scratch = MRI.createVirtualRegister(isThumb2 ? 5280 (const TargetRegisterClass*)&ARM::rGPRRegClass : 5281 (const TargetRegisterClass*)&ARM::GPRRegClass); 5282 5283 if (isThumb2) { 5284 MRI.constrainRegClass(dest, &ARM::rGPRRegClass); 5285 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass); 5286 MRI.constrainRegClass(newval, &ARM::rGPRRegClass); 5287 } 5288 5289 unsigned ldrOpc, strOpc; 5290 switch (Size) { 5291 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 5292 case 1: 5293 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 5294 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 5295 break; 5296 case 2: 5297 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 5298 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 5299 break; 5300 case 4: 5301 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 5302 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 5303 break; 5304 } 5305 5306 MachineFunction *MF = BB->getParent(); 5307 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5308 MachineFunction::iterator It = BB; 5309 ++It; // insert the new blocks after the current block 5310 5311 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); 5312 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); 5313 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5314 MF->insert(It, loop1MBB); 5315 MF->insert(It, loop2MBB); 5316 MF->insert(It, exitMBB); 5317 5318 // Transfer the remainder of BB and its successor edges to exitMBB. 5319 exitMBB->splice(exitMBB->begin(), BB, 5320 llvm::next(MachineBasicBlock::iterator(MI)), 5321 BB->end()); 5322 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5323 5324 // thisMBB: 5325 // ... 5326 // fallthrough --> loop1MBB 5327 BB->addSuccessor(loop1MBB); 5328 5329 // loop1MBB: 5330 // ldrex dest, [ptr] 5331 // cmp dest, oldval 5332 // bne exitMBB 5333 BB = loop1MBB; 5334 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 5335 if (ldrOpc == ARM::t2LDREX) 5336 MIB.addImm(0); 5337 AddDefaultPred(MIB); 5338 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 5339 .addReg(dest).addReg(oldval)); 5340 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5341 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5342 BB->addSuccessor(loop2MBB); 5343 BB->addSuccessor(exitMBB); 5344 5345 // loop2MBB: 5346 // strex scratch, newval, [ptr] 5347 // cmp scratch, #0 5348 // bne loop1MBB 5349 BB = loop2MBB; 5350 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr); 5351 if (strOpc == ARM::t2STREX) 5352 MIB.addImm(0); 5353 AddDefaultPred(MIB); 5354 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5355 .addReg(scratch).addImm(0)); 5356 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5357 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5358 BB->addSuccessor(loop1MBB); 5359 BB->addSuccessor(exitMBB); 5360 5361 // exitMBB: 5362 // ... 5363 BB = exitMBB; 5364 5365 MI->eraseFromParent(); // The instruction is gone now. 5366 5367 return BB; 5368 } 5369 5370 MachineBasicBlock * 5371 ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, 5372 unsigned Size, unsigned BinOpcode) const { 5373 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 5374 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5375 5376 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5377 MachineFunction *MF = BB->getParent(); 5378 MachineFunction::iterator It = BB; 5379 ++It; 5380 5381 unsigned dest = MI->getOperand(0).getReg(); 5382 unsigned ptr = MI->getOperand(1).getReg(); 5383 unsigned incr = MI->getOperand(2).getReg(); 5384 DebugLoc dl = MI->getDebugLoc(); 5385 bool isThumb2 = Subtarget->isThumb2(); 5386 5387 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5388 if (isThumb2) { 5389 MRI.constrainRegClass(dest, &ARM::rGPRRegClass); 5390 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass); 5391 } 5392 5393 unsigned ldrOpc, strOpc; 5394 switch (Size) { 5395 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 5396 case 1: 5397 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 5398 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 5399 break; 5400 case 2: 5401 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 5402 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 5403 break; 5404 case 4: 5405 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 5406 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 5407 break; 5408 } 5409 5410 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5411 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5412 MF->insert(It, loopMBB); 5413 MF->insert(It, exitMBB); 5414 5415 // Transfer the remainder of BB and its successor edges to exitMBB. 5416 exitMBB->splice(exitMBB->begin(), BB, 5417 llvm::next(MachineBasicBlock::iterator(MI)), 5418 BB->end()); 5419 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5420 5421 const TargetRegisterClass *TRC = isThumb2 ? 5422 (const TargetRegisterClass*)&ARM::tGPRRegClass : 5423 (const TargetRegisterClass*)&ARM::GPRRegClass; 5424 unsigned scratch = MRI.createVirtualRegister(TRC); 5425 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC); 5426 5427 // thisMBB: 5428 // ... 5429 // fallthrough --> loopMBB 5430 BB->addSuccessor(loopMBB); 5431 5432 // loopMBB: 5433 // ldrex dest, ptr 5434 // <binop> scratch2, dest, incr 5435 // strex scratch, scratch2, ptr 5436 // cmp scratch, #0 5437 // bne- loopMBB 5438 // fallthrough --> exitMBB 5439 BB = loopMBB; 5440 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 5441 if (ldrOpc == ARM::t2LDREX) 5442 MIB.addImm(0); 5443 AddDefaultPred(MIB); 5444 if (BinOpcode) { 5445 // operand order needs to go the other way for NAND 5446 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr) 5447 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2). 5448 addReg(incr).addReg(dest)).addReg(0); 5449 else 5450 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2). 5451 addReg(dest).addReg(incr)).addReg(0); 5452 } 5453 5454 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr); 5455 if (strOpc == ARM::t2STREX) 5456 MIB.addImm(0); 5457 AddDefaultPred(MIB); 5458 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5459 .addReg(scratch).addImm(0)); 5460 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5461 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5462 5463 BB->addSuccessor(loopMBB); 5464 BB->addSuccessor(exitMBB); 5465 5466 // exitMBB: 5467 // ... 5468 BB = exitMBB; 5469 5470 MI->eraseFromParent(); // The instruction is gone now. 5471 5472 return BB; 5473 } 5474 5475 MachineBasicBlock * 5476 ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI, 5477 MachineBasicBlock *BB, 5478 unsigned Size, 5479 bool signExtend, 5480 ARMCC::CondCodes Cond) const { 5481 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5482 5483 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5484 MachineFunction *MF = BB->getParent(); 5485 MachineFunction::iterator It = BB; 5486 ++It; 5487 5488 unsigned dest = MI->getOperand(0).getReg(); 5489 unsigned ptr = MI->getOperand(1).getReg(); 5490 unsigned incr = MI->getOperand(2).getReg(); 5491 unsigned oldval = dest; 5492 DebugLoc dl = MI->getDebugLoc(); 5493 bool isThumb2 = Subtarget->isThumb2(); 5494 5495 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5496 if (isThumb2) { 5497 MRI.constrainRegClass(dest, &ARM::rGPRRegClass); 5498 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass); 5499 } 5500 5501 unsigned ldrOpc, strOpc, extendOpc; 5502 switch (Size) { 5503 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 5504 case 1: 5505 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 5506 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 5507 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB; 5508 break; 5509 case 2: 5510 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 5511 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 5512 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH; 5513 break; 5514 case 4: 5515 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 5516 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 5517 extendOpc = 0; 5518 break; 5519 } 5520 5521 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5522 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5523 MF->insert(It, loopMBB); 5524 MF->insert(It, exitMBB); 5525 5526 // Transfer the remainder of BB and its successor edges to exitMBB. 5527 exitMBB->splice(exitMBB->begin(), BB, 5528 llvm::next(MachineBasicBlock::iterator(MI)), 5529 BB->end()); 5530 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5531 5532 const TargetRegisterClass *TRC = isThumb2 ? 5533 (const TargetRegisterClass*)&ARM::tGPRRegClass : 5534 (const TargetRegisterClass*)&ARM::GPRRegClass; 5535 unsigned scratch = MRI.createVirtualRegister(TRC); 5536 unsigned scratch2 = MRI.createVirtualRegister(TRC); 5537 5538 // thisMBB: 5539 // ... 5540 // fallthrough --> loopMBB 5541 BB->addSuccessor(loopMBB); 5542 5543 // loopMBB: 5544 // ldrex dest, ptr 5545 // (sign extend dest, if required) 5546 // cmp dest, incr 5547 // cmov.cond scratch2, dest, incr 5548 // strex scratch, scratch2, ptr 5549 // cmp scratch, #0 5550 // bne- loopMBB 5551 // fallthrough --> exitMBB 5552 BB = loopMBB; 5553 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 5554 if (ldrOpc == ARM::t2LDREX) 5555 MIB.addImm(0); 5556 AddDefaultPred(MIB); 5557 5558 // Sign extend the value, if necessary. 5559 if (signExtend && extendOpc) { 5560 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass); 5561 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval) 5562 .addReg(dest) 5563 .addImm(0)); 5564 } 5565 5566 // Build compare and cmov instructions. 5567 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 5568 .addReg(oldval).addReg(incr)); 5569 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2) 5570 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR); 5571 5572 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr); 5573 if (strOpc == ARM::t2STREX) 5574 MIB.addImm(0); 5575 AddDefaultPred(MIB); 5576 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5577 .addReg(scratch).addImm(0)); 5578 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5579 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5580 5581 BB->addSuccessor(loopMBB); 5582 BB->addSuccessor(exitMBB); 5583 5584 // exitMBB: 5585 // ... 5586 BB = exitMBB; 5587 5588 MI->eraseFromParent(); // The instruction is gone now. 5589 5590 return BB; 5591 } 5592 5593 MachineBasicBlock * 5594 ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB, 5595 unsigned Op1, unsigned Op2, 5596 bool NeedsCarry, bool IsCmpxchg) const { 5597 // This also handles ATOMIC_SWAP, indicated by Op1==0. 5598 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5599 5600 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5601 MachineFunction *MF = BB->getParent(); 5602 MachineFunction::iterator It = BB; 5603 ++It; 5604 5605 unsigned destlo = MI->getOperand(0).getReg(); 5606 unsigned desthi = MI->getOperand(1).getReg(); 5607 unsigned ptr = MI->getOperand(2).getReg(); 5608 unsigned vallo = MI->getOperand(3).getReg(); 5609 unsigned valhi = MI->getOperand(4).getReg(); 5610 DebugLoc dl = MI->getDebugLoc(); 5611 bool isThumb2 = Subtarget->isThumb2(); 5612 5613 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5614 if (isThumb2) { 5615 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass); 5616 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass); 5617 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass); 5618 } 5619 5620 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD; 5621 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD; 5622 5623 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5624 MachineBasicBlock *contBB = 0, *cont2BB = 0; 5625 if (IsCmpxchg) { 5626 contBB = MF->CreateMachineBasicBlock(LLVM_BB); 5627 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB); 5628 } 5629 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5630 MF->insert(It, loopMBB); 5631 if (IsCmpxchg) { 5632 MF->insert(It, contBB); 5633 MF->insert(It, cont2BB); 5634 } 5635 MF->insert(It, exitMBB); 5636 5637 // Transfer the remainder of BB and its successor edges to exitMBB. 5638 exitMBB->splice(exitMBB->begin(), BB, 5639 llvm::next(MachineBasicBlock::iterator(MI)), 5640 BB->end()); 5641 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5642 5643 const TargetRegisterClass *TRC = isThumb2 ? 5644 (const TargetRegisterClass*)&ARM::tGPRRegClass : 5645 (const TargetRegisterClass*)&ARM::GPRRegClass; 5646 unsigned storesuccess = MRI.createVirtualRegister(TRC); 5647 5648 // thisMBB: 5649 // ... 5650 // fallthrough --> loopMBB 5651 BB->addSuccessor(loopMBB); 5652 5653 // loopMBB: 5654 // ldrexd r2, r3, ptr 5655 // <binopa> r0, r2, incr 5656 // <binopb> r1, r3, incr 5657 // strexd storesuccess, r0, r1, ptr 5658 // cmp storesuccess, #0 5659 // bne- loopMBB 5660 // fallthrough --> exitMBB 5661 // 5662 // Note that the registers are explicitly specified because there is not any 5663 // way to force the register allocator to allocate a register pair. 5664 // 5665 // FIXME: The hardcoded registers are not necessary for Thumb2, but we 5666 // need to properly enforce the restriction that the two output registers 5667 // for ldrexd must be different. 5668 BB = loopMBB; 5669 // Load 5670 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc)) 5671 .addReg(ARM::R2, RegState::Define) 5672 .addReg(ARM::R3, RegState::Define).addReg(ptr)); 5673 // Copy r2/r3 into dest. (This copy will normally be coalesced.) 5674 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2); 5675 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3); 5676 5677 if (IsCmpxchg) { 5678 // Add early exit 5679 for (unsigned i = 0; i < 2; i++) { 5680 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : 5681 ARM::CMPrr)) 5682 .addReg(i == 0 ? destlo : desthi) 5683 .addReg(i == 0 ? vallo : valhi)); 5684 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5685 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5686 BB->addSuccessor(exitMBB); 5687 BB->addSuccessor(i == 0 ? contBB : cont2BB); 5688 BB = (i == 0 ? contBB : cont2BB); 5689 } 5690 5691 // Copy to physregs for strexd 5692 unsigned setlo = MI->getOperand(5).getReg(); 5693 unsigned sethi = MI->getOperand(6).getReg(); 5694 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo); 5695 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi); 5696 } else if (Op1) { 5697 // Perform binary operation 5698 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0) 5699 .addReg(destlo).addReg(vallo)) 5700 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry)); 5701 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1) 5702 .addReg(desthi).addReg(valhi)).addReg(0); 5703 } else { 5704 // Copy to physregs for strexd 5705 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo); 5706 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi); 5707 } 5708 5709 // Store 5710 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess) 5711 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr)); 5712 // Cmp+jump 5713 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5714 .addReg(storesuccess).addImm(0)); 5715 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5716 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5717 5718 BB->addSuccessor(loopMBB); 5719 BB->addSuccessor(exitMBB); 5720 5721 // exitMBB: 5722 // ... 5723 BB = exitMBB; 5724 5725 MI->eraseFromParent(); // The instruction is gone now. 5726 5727 return BB; 5728 } 5729 5730 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 5731 /// registers the function context. 5732 void ARMTargetLowering:: 5733 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB, 5734 MachineBasicBlock *DispatchBB, int FI) const { 5735 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5736 DebugLoc dl = MI->getDebugLoc(); 5737 MachineFunction *MF = MBB->getParent(); 5738 MachineRegisterInfo *MRI = &MF->getRegInfo(); 5739 MachineConstantPool *MCP = MF->getConstantPool(); 5740 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 5741 const Function *F = MF->getFunction(); 5742 5743 bool isThumb = Subtarget->isThumb(); 5744 bool isThumb2 = Subtarget->isThumb2(); 5745 5746 unsigned PCLabelId = AFI->createPICLabelUId(); 5747 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 5748 ARMConstantPoolValue *CPV = 5749 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj); 5750 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 5751 5752 const TargetRegisterClass *TRC = isThumb ? 5753 (const TargetRegisterClass*)&ARM::tGPRRegClass : 5754 (const TargetRegisterClass*)&ARM::GPRRegClass; 5755 5756 // Grab constant pool and fixed stack memory operands. 5757 MachineMemOperand *CPMMO = 5758 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(), 5759 MachineMemOperand::MOLoad, 4, 4); 5760 5761 MachineMemOperand *FIMMOSt = 5762 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 5763 MachineMemOperand::MOStore, 4, 4); 5764 5765 // Load the address of the dispatch MBB into the jump buffer. 5766 if (isThumb2) { 5767 // Incoming value: jbuf 5768 // ldr.n r5, LCPI1_1 5769 // orr r5, r5, #1 5770 // add r5, pc 5771 // str r5, [$jbuf, #+4] ; &jbuf[1] 5772 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5773 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 5774 .addConstantPoolIndex(CPI) 5775 .addMemOperand(CPMMO)); 5776 // Set the low bit because of thumb mode. 5777 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5778 AddDefaultCC( 5779 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 5780 .addReg(NewVReg1, RegState::Kill) 5781 .addImm(0x01))); 5782 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5783 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 5784 .addReg(NewVReg2, RegState::Kill) 5785 .addImm(PCLabelId); 5786 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 5787 .addReg(NewVReg3, RegState::Kill) 5788 .addFrameIndex(FI) 5789 .addImm(36) // &jbuf[1] :: pc 5790 .addMemOperand(FIMMOSt)); 5791 } else if (isThumb) { 5792 // Incoming value: jbuf 5793 // ldr.n r1, LCPI1_4 5794 // add r1, pc 5795 // mov r2, #1 5796 // orrs r1, r2 5797 // add r2, $jbuf, #+4 ; &jbuf[1] 5798 // str r1, [r2] 5799 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5800 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 5801 .addConstantPoolIndex(CPI) 5802 .addMemOperand(CPMMO)); 5803 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5804 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 5805 .addReg(NewVReg1, RegState::Kill) 5806 .addImm(PCLabelId); 5807 // Set the low bit because of thumb mode. 5808 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5809 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 5810 .addReg(ARM::CPSR, RegState::Define) 5811 .addImm(1)); 5812 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 5813 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 5814 .addReg(ARM::CPSR, RegState::Define) 5815 .addReg(NewVReg2, RegState::Kill) 5816 .addReg(NewVReg3, RegState::Kill)); 5817 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 5818 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5) 5819 .addFrameIndex(FI) 5820 .addImm(36)); // &jbuf[1] :: pc 5821 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 5822 .addReg(NewVReg4, RegState::Kill) 5823 .addReg(NewVReg5, RegState::Kill) 5824 .addImm(0) 5825 .addMemOperand(FIMMOSt)); 5826 } else { 5827 // Incoming value: jbuf 5828 // ldr r1, LCPI1_1 5829 // add r1, pc, r1 5830 // str r1, [$jbuf, #+4] ; &jbuf[1] 5831 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5832 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 5833 .addConstantPoolIndex(CPI) 5834 .addImm(0) 5835 .addMemOperand(CPMMO)); 5836 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5837 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 5838 .addReg(NewVReg1, RegState::Kill) 5839 .addImm(PCLabelId)); 5840 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 5841 .addReg(NewVReg2, RegState::Kill) 5842 .addFrameIndex(FI) 5843 .addImm(36) // &jbuf[1] :: pc 5844 .addMemOperand(FIMMOSt)); 5845 } 5846 } 5847 5848 MachineBasicBlock *ARMTargetLowering:: 5849 EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const { 5850 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5851 DebugLoc dl = MI->getDebugLoc(); 5852 MachineFunction *MF = MBB->getParent(); 5853 MachineRegisterInfo *MRI = &MF->getRegInfo(); 5854 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 5855 MachineFrameInfo *MFI = MF->getFrameInfo(); 5856 int FI = MFI->getFunctionContextIndex(); 5857 5858 const TargetRegisterClass *TRC = Subtarget->isThumb() ? 5859 (const TargetRegisterClass*)&ARM::tGPRRegClass : 5860 (const TargetRegisterClass*)&ARM::GPRnopcRegClass; 5861 5862 // Get a mapping of the call site numbers to all of the landing pads they're 5863 // associated with. 5864 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad; 5865 unsigned MaxCSNum = 0; 5866 MachineModuleInfo &MMI = MF->getMMI(); 5867 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 5868 ++BB) { 5869 if (!BB->isLandingPad()) continue; 5870 5871 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 5872 // pad. 5873 for (MachineBasicBlock::iterator 5874 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 5875 if (!II->isEHLabel()) continue; 5876 5877 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 5878 if (!MMI.hasCallSiteLandingPad(Sym)) continue; 5879 5880 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym); 5881 for (SmallVectorImpl<unsigned>::iterator 5882 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 5883 CSI != CSE; ++CSI) { 5884 CallSiteNumToLPad[*CSI].push_back(BB); 5885 MaxCSNum = std::max(MaxCSNum, *CSI); 5886 } 5887 break; 5888 } 5889 } 5890 5891 // Get an ordered list of the machine basic blocks for the jump table. 5892 std::vector<MachineBasicBlock*> LPadList; 5893 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs; 5894 LPadList.reserve(CallSiteNumToLPad.size()); 5895 for (unsigned I = 1; I <= MaxCSNum; ++I) { 5896 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 5897 for (SmallVectorImpl<MachineBasicBlock*>::iterator 5898 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 5899 LPadList.push_back(*II); 5900 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 5901 } 5902 } 5903 5904 assert(!LPadList.empty() && 5905 "No landing pad destinations for the dispatch jump table!"); 5906 5907 // Create the jump table and associated information. 5908 MachineJumpTableInfo *JTI = 5909 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 5910 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 5911 unsigned UId = AFI->createJumpTableUId(); 5912 5913 // Create the MBBs for the dispatch code. 5914 5915 // Shove the dispatch's address into the return slot in the function context. 5916 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 5917 DispatchBB->setIsLandingPad(); 5918 5919 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 5920 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP)); 5921 DispatchBB->addSuccessor(TrapBB); 5922 5923 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 5924 DispatchBB->addSuccessor(DispContBB); 5925 5926 // Insert and MBBs. 5927 MF->insert(MF->end(), DispatchBB); 5928 MF->insert(MF->end(), DispContBB); 5929 MF->insert(MF->end(), TrapBB); 5930 5931 // Insert code into the entry block that creates and registers the function 5932 // context. 5933 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 5934 5935 MachineMemOperand *FIMMOLd = 5936 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 5937 MachineMemOperand::MOLoad | 5938 MachineMemOperand::MOVolatile, 4, 4); 5939 5940 if (AFI->isThumb1OnlyFunction()) 5941 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup)); 5942 else if (!Subtarget->hasVFP2()) 5943 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp)); 5944 else 5945 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 5946 5947 unsigned NumLPads = LPadList.size(); 5948 if (Subtarget->isThumb2()) { 5949 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5950 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 5951 .addFrameIndex(FI) 5952 .addImm(4) 5953 .addMemOperand(FIMMOLd)); 5954 5955 if (NumLPads < 256) { 5956 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 5957 .addReg(NewVReg1) 5958 .addImm(LPadList.size())); 5959 } else { 5960 unsigned VReg1 = MRI->createVirtualRegister(TRC); 5961 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 5962 .addImm(NumLPads & 0xFFFF)); 5963 5964 unsigned VReg2 = VReg1; 5965 if ((NumLPads & 0xFFFF0000) != 0) { 5966 VReg2 = MRI->createVirtualRegister(TRC); 5967 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 5968 .addReg(VReg1) 5969 .addImm(NumLPads >> 16)); 5970 } 5971 5972 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 5973 .addReg(NewVReg1) 5974 .addReg(VReg2)); 5975 } 5976 5977 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 5978 .addMBB(TrapBB) 5979 .addImm(ARMCC::HI) 5980 .addReg(ARM::CPSR); 5981 5982 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5983 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3) 5984 .addJumpTableIndex(MJTI) 5985 .addImm(UId)); 5986 5987 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 5988 AddDefaultCC( 5989 AddDefaultPred( 5990 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 5991 .addReg(NewVReg3, RegState::Kill) 5992 .addReg(NewVReg1) 5993 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 5994 5995 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 5996 .addReg(NewVReg4, RegState::Kill) 5997 .addReg(NewVReg1) 5998 .addJumpTableIndex(MJTI) 5999 .addImm(UId); 6000 } else if (Subtarget->isThumb()) { 6001 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6002 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 6003 .addFrameIndex(FI) 6004 .addImm(1) 6005 .addMemOperand(FIMMOLd)); 6006 6007 if (NumLPads < 256) { 6008 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 6009 .addReg(NewVReg1) 6010 .addImm(NumLPads)); 6011 } else { 6012 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6013 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6014 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6015 6016 // MachineConstantPool wants an explicit alignment. 6017 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty); 6018 if (Align == 0) 6019 Align = getTargetData()->getTypeAllocSize(C->getType()); 6020 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6021 6022 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6023 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 6024 .addReg(VReg1, RegState::Define) 6025 .addConstantPoolIndex(Idx)); 6026 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 6027 .addReg(NewVReg1) 6028 .addReg(VReg1)); 6029 } 6030 6031 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 6032 .addMBB(TrapBB) 6033 .addImm(ARMCC::HI) 6034 .addReg(ARM::CPSR); 6035 6036 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6037 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 6038 .addReg(ARM::CPSR, RegState::Define) 6039 .addReg(NewVReg1) 6040 .addImm(2)); 6041 6042 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6043 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 6044 .addJumpTableIndex(MJTI) 6045 .addImm(UId)); 6046 6047 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6048 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 6049 .addReg(ARM::CPSR, RegState::Define) 6050 .addReg(NewVReg2, RegState::Kill) 6051 .addReg(NewVReg3)); 6052 6053 MachineMemOperand *JTMMOLd = 6054 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6055 MachineMemOperand::MOLoad, 4, 4); 6056 6057 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6058 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 6059 .addReg(NewVReg4, RegState::Kill) 6060 .addImm(0) 6061 .addMemOperand(JTMMOLd)); 6062 6063 unsigned NewVReg6 = MRI->createVirtualRegister(TRC); 6064 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 6065 .addReg(ARM::CPSR, RegState::Define) 6066 .addReg(NewVReg5, RegState::Kill) 6067 .addReg(NewVReg3)); 6068 6069 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 6070 .addReg(NewVReg6, RegState::Kill) 6071 .addJumpTableIndex(MJTI) 6072 .addImm(UId); 6073 } else { 6074 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6075 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 6076 .addFrameIndex(FI) 6077 .addImm(4) 6078 .addMemOperand(FIMMOLd)); 6079 6080 if (NumLPads < 256) { 6081 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 6082 .addReg(NewVReg1) 6083 .addImm(NumLPads)); 6084 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 6085 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6086 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 6087 .addImm(NumLPads & 0xFFFF)); 6088 6089 unsigned VReg2 = VReg1; 6090 if ((NumLPads & 0xFFFF0000) != 0) { 6091 VReg2 = MRI->createVirtualRegister(TRC); 6092 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 6093 .addReg(VReg1) 6094 .addImm(NumLPads >> 16)); 6095 } 6096 6097 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 6098 .addReg(NewVReg1) 6099 .addReg(VReg2)); 6100 } else { 6101 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6102 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6103 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6104 6105 // MachineConstantPool wants an explicit alignment. 6106 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty); 6107 if (Align == 0) 6108 Align = getTargetData()->getTypeAllocSize(C->getType()); 6109 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6110 6111 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6112 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 6113 .addReg(VReg1, RegState::Define) 6114 .addConstantPoolIndex(Idx) 6115 .addImm(0)); 6116 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 6117 .addReg(NewVReg1) 6118 .addReg(VReg1, RegState::Kill)); 6119 } 6120 6121 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 6122 .addMBB(TrapBB) 6123 .addImm(ARMCC::HI) 6124 .addReg(ARM::CPSR); 6125 6126 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6127 AddDefaultCC( 6128 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 6129 .addReg(NewVReg1) 6130 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 6131 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6132 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 6133 .addJumpTableIndex(MJTI) 6134 .addImm(UId)); 6135 6136 MachineMemOperand *JTMMOLd = 6137 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6138 MachineMemOperand::MOLoad, 4, 4); 6139 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6140 AddDefaultPred( 6141 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 6142 .addReg(NewVReg3, RegState::Kill) 6143 .addReg(NewVReg4) 6144 .addImm(0) 6145 .addMemOperand(JTMMOLd)); 6146 6147 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 6148 .addReg(NewVReg5, RegState::Kill) 6149 .addReg(NewVReg4) 6150 .addJumpTableIndex(MJTI) 6151 .addImm(UId); 6152 } 6153 6154 // Add the jump table entries as successors to the MBB. 6155 MachineBasicBlock *PrevMBB = 0; 6156 for (std::vector<MachineBasicBlock*>::iterator 6157 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 6158 MachineBasicBlock *CurMBB = *I; 6159 if (PrevMBB != CurMBB) 6160 DispContBB->addSuccessor(CurMBB); 6161 PrevMBB = CurMBB; 6162 } 6163 6164 // N.B. the order the invoke BBs are processed in doesn't matter here. 6165 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 6166 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 6167 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF); 6168 SmallVector<MachineBasicBlock*, 64> MBBLPads; 6169 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator 6170 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) { 6171 MachineBasicBlock *BB = *I; 6172 6173 // Remove the landing pad successor from the invoke block and replace it 6174 // with the new dispatch block. 6175 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 6176 BB->succ_end()); 6177 while (!Successors.empty()) { 6178 MachineBasicBlock *SMBB = Successors.pop_back_val(); 6179 if (SMBB->isLandingPad()) { 6180 BB->removeSuccessor(SMBB); 6181 MBBLPads.push_back(SMBB); 6182 } 6183 } 6184 6185 BB->addSuccessor(DispatchBB); 6186 6187 // Find the invoke call and mark all of the callee-saved registers as 6188 // 'implicit defined' so that they're spilled. This prevents code from 6189 // moving instructions to before the EH block, where they will never be 6190 // executed. 6191 for (MachineBasicBlock::reverse_iterator 6192 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 6193 if (!II->isCall()) continue; 6194 6195 DenseMap<unsigned, bool> DefRegs; 6196 for (MachineInstr::mop_iterator 6197 OI = II->operands_begin(), OE = II->operands_end(); 6198 OI != OE; ++OI) { 6199 if (!OI->isReg()) continue; 6200 DefRegs[OI->getReg()] = true; 6201 } 6202 6203 MachineInstrBuilder MIB(&*II); 6204 6205 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 6206 unsigned Reg = SavedRegs[i]; 6207 if (Subtarget->isThumb2() && 6208 !ARM::tGPRRegClass.contains(Reg) && 6209 !ARM::hGPRRegClass.contains(Reg)) 6210 continue; 6211 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 6212 continue; 6213 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 6214 continue; 6215 if (!DefRegs[Reg]) 6216 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 6217 } 6218 6219 break; 6220 } 6221 } 6222 6223 // Mark all former landing pads as non-landing pads. The dispatch is the only 6224 // landing pad now. 6225 for (SmallVectorImpl<MachineBasicBlock*>::iterator 6226 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 6227 (*I)->setIsLandingPad(false); 6228 6229 // The instruction is gone now. 6230 MI->eraseFromParent(); 6231 6232 return MBB; 6233 } 6234 6235 static 6236 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 6237 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 6238 E = MBB->succ_end(); I != E; ++I) 6239 if (*I != Succ) 6240 return *I; 6241 llvm_unreachable("Expecting a BB with two successors!"); 6242 } 6243 6244 MachineBasicBlock *ARMTargetLowering:: 6245 EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const { 6246 // This pseudo instruction has 3 operands: dst, src, size 6247 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 6248 // Otherwise, we will generate unrolled scalar copies. 6249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6250 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6251 MachineFunction::iterator It = BB; 6252 ++It; 6253 6254 unsigned dest = MI->getOperand(0).getReg(); 6255 unsigned src = MI->getOperand(1).getReg(); 6256 unsigned SizeVal = MI->getOperand(2).getImm(); 6257 unsigned Align = MI->getOperand(3).getImm(); 6258 DebugLoc dl = MI->getDebugLoc(); 6259 6260 bool isThumb2 = Subtarget->isThumb2(); 6261 MachineFunction *MF = BB->getParent(); 6262 MachineRegisterInfo &MRI = MF->getRegInfo(); 6263 unsigned ldrOpc, strOpc, UnitSize; 6264 6265 const TargetRegisterClass *TRC = isThumb2 ? 6266 (const TargetRegisterClass*)&ARM::tGPRRegClass : 6267 (const TargetRegisterClass*)&ARM::GPRRegClass; 6268 6269 if (Align & 1) { 6270 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM; 6271 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM; 6272 UnitSize = 1; 6273 } else if (Align & 2) { 6274 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST; 6275 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST; 6276 UnitSize = 2; 6277 } else { 6278 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM; 6279 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM; 6280 UnitSize = 4; 6281 } 6282 unsigned BytesLeft = SizeVal % UnitSize; 6283 unsigned LoopSize = SizeVal - BytesLeft; 6284 6285 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 6286 // Use LDR and STR to copy. 6287 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 6288 // [destOut] = STR_POST(scratch, destIn, UnitSize) 6289 unsigned srcIn = src; 6290 unsigned destIn = dest; 6291 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 6292 unsigned scratch = MRI.createVirtualRegister(TRC); 6293 unsigned srcOut = MRI.createVirtualRegister(TRC); 6294 unsigned destOut = MRI.createVirtualRegister(TRC); 6295 if (isThumb2) { 6296 AddDefaultPred(BuildMI(*BB, MI, dl, 6297 TII->get(ldrOpc), scratch) 6298 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize)); 6299 6300 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 6301 .addReg(scratch).addReg(destIn) 6302 .addImm(UnitSize)); 6303 } else { 6304 AddDefaultPred(BuildMI(*BB, MI, dl, 6305 TII->get(ldrOpc), scratch) 6306 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0) 6307 .addImm(UnitSize)); 6308 6309 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 6310 .addReg(scratch).addReg(destIn) 6311 .addReg(0).addImm(UnitSize)); 6312 } 6313 srcIn = srcOut; 6314 destIn = destOut; 6315 } 6316 6317 // Handle the leftover bytes with LDRB and STRB. 6318 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 6319 // [destOut] = STRB_POST(scratch, destIn, 1) 6320 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM; 6321 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM; 6322 for (unsigned i = 0; i < BytesLeft; i++) { 6323 unsigned scratch = MRI.createVirtualRegister(TRC); 6324 unsigned srcOut = MRI.createVirtualRegister(TRC); 6325 unsigned destOut = MRI.createVirtualRegister(TRC); 6326 if (isThumb2) { 6327 AddDefaultPred(BuildMI(*BB, MI, dl, 6328 TII->get(ldrOpc),scratch) 6329 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1)); 6330 6331 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 6332 .addReg(scratch).addReg(destIn) 6333 .addReg(0).addImm(1)); 6334 } else { 6335 AddDefaultPred(BuildMI(*BB, MI, dl, 6336 TII->get(ldrOpc),scratch) 6337 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1)); 6338 6339 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut) 6340 .addReg(scratch).addReg(destIn) 6341 .addReg(0).addImm(1)); 6342 } 6343 srcIn = srcOut; 6344 destIn = destOut; 6345 } 6346 MI->eraseFromParent(); // The instruction is gone now. 6347 return BB; 6348 } 6349 6350 // Expand the pseudo op to a loop. 6351 // thisMBB: 6352 // ... 6353 // movw varEnd, # --> with thumb2 6354 // movt varEnd, # 6355 // ldrcp varEnd, idx --> without thumb2 6356 // fallthrough --> loopMBB 6357 // loopMBB: 6358 // PHI varPhi, varEnd, varLoop 6359 // PHI srcPhi, src, srcLoop 6360 // PHI destPhi, dst, destLoop 6361 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 6362 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 6363 // subs varLoop, varPhi, #UnitSize 6364 // bne loopMBB 6365 // fallthrough --> exitMBB 6366 // exitMBB: 6367 // epilogue to handle left-over bytes 6368 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 6369 // [destOut] = STRB_POST(scratch, destLoop, 1) 6370 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6371 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 6372 MF->insert(It, loopMBB); 6373 MF->insert(It, exitMBB); 6374 6375 // Transfer the remainder of BB and its successor edges to exitMBB. 6376 exitMBB->splice(exitMBB->begin(), BB, 6377 llvm::next(MachineBasicBlock::iterator(MI)), 6378 BB->end()); 6379 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 6380 6381 // Load an immediate to varEnd. 6382 unsigned varEnd = MRI.createVirtualRegister(TRC); 6383 if (isThumb2) { 6384 unsigned VReg1 = varEnd; 6385 if ((LoopSize & 0xFFFF0000) != 0) 6386 VReg1 = MRI.createVirtualRegister(TRC); 6387 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1) 6388 .addImm(LoopSize & 0xFFFF)); 6389 6390 if ((LoopSize & 0xFFFF0000) != 0) 6391 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd) 6392 .addReg(VReg1) 6393 .addImm(LoopSize >> 16)); 6394 } else { 6395 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6396 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6397 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 6398 6399 // MachineConstantPool wants an explicit alignment. 6400 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty); 6401 if (Align == 0) 6402 Align = getTargetData()->getTypeAllocSize(C->getType()); 6403 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6404 6405 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp)) 6406 .addReg(varEnd, RegState::Define) 6407 .addConstantPoolIndex(Idx) 6408 .addImm(0)); 6409 } 6410 BB->addSuccessor(loopMBB); 6411 6412 // Generate the loop body: 6413 // varPhi = PHI(varLoop, varEnd) 6414 // srcPhi = PHI(srcLoop, src) 6415 // destPhi = PHI(destLoop, dst) 6416 MachineBasicBlock *entryBB = BB; 6417 BB = loopMBB; 6418 unsigned varLoop = MRI.createVirtualRegister(TRC); 6419 unsigned varPhi = MRI.createVirtualRegister(TRC); 6420 unsigned srcLoop = MRI.createVirtualRegister(TRC); 6421 unsigned srcPhi = MRI.createVirtualRegister(TRC); 6422 unsigned destLoop = MRI.createVirtualRegister(TRC); 6423 unsigned destPhi = MRI.createVirtualRegister(TRC); 6424 6425 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 6426 .addReg(varLoop).addMBB(loopMBB) 6427 .addReg(varEnd).addMBB(entryBB); 6428 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 6429 .addReg(srcLoop).addMBB(loopMBB) 6430 .addReg(src).addMBB(entryBB); 6431 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 6432 .addReg(destLoop).addMBB(loopMBB) 6433 .addReg(dest).addMBB(entryBB); 6434 6435 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 6436 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 6437 unsigned scratch = MRI.createVirtualRegister(TRC); 6438 if (isThumb2) { 6439 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch) 6440 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize)); 6441 6442 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop) 6443 .addReg(scratch).addReg(destPhi) 6444 .addImm(UnitSize)); 6445 } else { 6446 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch) 6447 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0) 6448 .addImm(UnitSize)); 6449 6450 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop) 6451 .addReg(scratch).addReg(destPhi) 6452 .addReg(0).addImm(UnitSize)); 6453 } 6454 6455 // Decrement loop variable by UnitSize. 6456 MachineInstrBuilder MIB = BuildMI(BB, dl, 6457 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 6458 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize))); 6459 MIB->getOperand(5).setReg(ARM::CPSR); 6460 MIB->getOperand(5).setIsDef(true); 6461 6462 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6463 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 6464 6465 // loopMBB can loop back to loopMBB or fall through to exitMBB. 6466 BB->addSuccessor(loopMBB); 6467 BB->addSuccessor(exitMBB); 6468 6469 // Add epilogue to handle BytesLeft. 6470 BB = exitMBB; 6471 MachineInstr *StartOfExit = exitMBB->begin(); 6472 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM; 6473 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM; 6474 6475 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 6476 // [destOut] = STRB_POST(scratch, destLoop, 1) 6477 unsigned srcIn = srcLoop; 6478 unsigned destIn = destLoop; 6479 for (unsigned i = 0; i < BytesLeft; i++) { 6480 unsigned scratch = MRI.createVirtualRegister(TRC); 6481 unsigned srcOut = MRI.createVirtualRegister(TRC); 6482 unsigned destOut = MRI.createVirtualRegister(TRC); 6483 if (isThumb2) { 6484 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, 6485 TII->get(ldrOpc),scratch) 6486 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1)); 6487 6488 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut) 6489 .addReg(scratch).addReg(destIn) 6490 .addImm(1)); 6491 } else { 6492 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, 6493 TII->get(ldrOpc),scratch) 6494 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1)); 6495 6496 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut) 6497 .addReg(scratch).addReg(destIn) 6498 .addReg(0).addImm(1)); 6499 } 6500 srcIn = srcOut; 6501 destIn = destOut; 6502 } 6503 6504 MI->eraseFromParent(); // The instruction is gone now. 6505 return BB; 6506 } 6507 6508 MachineBasicBlock * 6509 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 6510 MachineBasicBlock *BB) const { 6511 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6512 DebugLoc dl = MI->getDebugLoc(); 6513 bool isThumb2 = Subtarget->isThumb2(); 6514 switch (MI->getOpcode()) { 6515 default: { 6516 MI->dump(); 6517 llvm_unreachable("Unexpected instr type to insert"); 6518 } 6519 // The Thumb2 pre-indexed stores have the same MI operands, they just 6520 // define them differently in the .td files from the isel patterns, so 6521 // they need pseudos. 6522 case ARM::t2STR_preidx: 6523 MI->setDesc(TII->get(ARM::t2STR_PRE)); 6524 return BB; 6525 case ARM::t2STRB_preidx: 6526 MI->setDesc(TII->get(ARM::t2STRB_PRE)); 6527 return BB; 6528 case ARM::t2STRH_preidx: 6529 MI->setDesc(TII->get(ARM::t2STRH_PRE)); 6530 return BB; 6531 6532 case ARM::STRi_preidx: 6533 case ARM::STRBi_preidx: { 6534 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ? 6535 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM; 6536 // Decode the offset. 6537 unsigned Offset = MI->getOperand(4).getImm(); 6538 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 6539 Offset = ARM_AM::getAM2Offset(Offset); 6540 if (isSub) 6541 Offset = -Offset; 6542 6543 MachineMemOperand *MMO = *MI->memoperands_begin(); 6544 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 6545 .addOperand(MI->getOperand(0)) // Rn_wb 6546 .addOperand(MI->getOperand(1)) // Rt 6547 .addOperand(MI->getOperand(2)) // Rn 6548 .addImm(Offset) // offset (skip GPR==zero_reg) 6549 .addOperand(MI->getOperand(5)) // pred 6550 .addOperand(MI->getOperand(6)) 6551 .addMemOperand(MMO); 6552 MI->eraseFromParent(); 6553 return BB; 6554 } 6555 case ARM::STRr_preidx: 6556 case ARM::STRBr_preidx: 6557 case ARM::STRH_preidx: { 6558 unsigned NewOpc; 6559 switch (MI->getOpcode()) { 6560 default: llvm_unreachable("unexpected opcode!"); 6561 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 6562 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 6563 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 6564 } 6565 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 6566 for (unsigned i = 0; i < MI->getNumOperands(); ++i) 6567 MIB.addOperand(MI->getOperand(i)); 6568 MI->eraseFromParent(); 6569 return BB; 6570 } 6571 case ARM::ATOMIC_LOAD_ADD_I8: 6572 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 6573 case ARM::ATOMIC_LOAD_ADD_I16: 6574 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 6575 case ARM::ATOMIC_LOAD_ADD_I32: 6576 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 6577 6578 case ARM::ATOMIC_LOAD_AND_I8: 6579 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6580 case ARM::ATOMIC_LOAD_AND_I16: 6581 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6582 case ARM::ATOMIC_LOAD_AND_I32: 6583 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6584 6585 case ARM::ATOMIC_LOAD_OR_I8: 6586 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6587 case ARM::ATOMIC_LOAD_OR_I16: 6588 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6589 case ARM::ATOMIC_LOAD_OR_I32: 6590 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6591 6592 case ARM::ATOMIC_LOAD_XOR_I8: 6593 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6594 case ARM::ATOMIC_LOAD_XOR_I16: 6595 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6596 case ARM::ATOMIC_LOAD_XOR_I32: 6597 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6598 6599 case ARM::ATOMIC_LOAD_NAND_I8: 6600 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 6601 case ARM::ATOMIC_LOAD_NAND_I16: 6602 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 6603 case ARM::ATOMIC_LOAD_NAND_I32: 6604 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 6605 6606 case ARM::ATOMIC_LOAD_SUB_I8: 6607 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 6608 case ARM::ATOMIC_LOAD_SUB_I16: 6609 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 6610 case ARM::ATOMIC_LOAD_SUB_I32: 6611 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 6612 6613 case ARM::ATOMIC_LOAD_MIN_I8: 6614 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT); 6615 case ARM::ATOMIC_LOAD_MIN_I16: 6616 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT); 6617 case ARM::ATOMIC_LOAD_MIN_I32: 6618 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT); 6619 6620 case ARM::ATOMIC_LOAD_MAX_I8: 6621 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT); 6622 case ARM::ATOMIC_LOAD_MAX_I16: 6623 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT); 6624 case ARM::ATOMIC_LOAD_MAX_I32: 6625 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT); 6626 6627 case ARM::ATOMIC_LOAD_UMIN_I8: 6628 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO); 6629 case ARM::ATOMIC_LOAD_UMIN_I16: 6630 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO); 6631 case ARM::ATOMIC_LOAD_UMIN_I32: 6632 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO); 6633 6634 case ARM::ATOMIC_LOAD_UMAX_I8: 6635 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI); 6636 case ARM::ATOMIC_LOAD_UMAX_I16: 6637 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI); 6638 case ARM::ATOMIC_LOAD_UMAX_I32: 6639 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI); 6640 6641 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0); 6642 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0); 6643 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0); 6644 6645 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1); 6646 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2); 6647 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4); 6648 6649 6650 case ARM::ATOMADD6432: 6651 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr, 6652 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr, 6653 /*NeedsCarry*/ true); 6654 case ARM::ATOMSUB6432: 6655 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 6656 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 6657 /*NeedsCarry*/ true); 6658 case ARM::ATOMOR6432: 6659 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr, 6660 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6661 case ARM::ATOMXOR6432: 6662 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr, 6663 isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6664 case ARM::ATOMAND6432: 6665 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr, 6666 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6667 case ARM::ATOMSWAP6432: 6668 return EmitAtomicBinary64(MI, BB, 0, 0, false); 6669 case ARM::ATOMCMPXCHG6432: 6670 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 6671 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 6672 /*NeedsCarry*/ false, /*IsCmpxchg*/true); 6673 6674 case ARM::tMOVCCr_pseudo: { 6675 // To "insert" a SELECT_CC instruction, we actually have to insert the 6676 // diamond control-flow pattern. The incoming instruction knows the 6677 // destination vreg to set, the condition code register to branch on, the 6678 // true/false values to select between, and a branch opcode to use. 6679 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6680 MachineFunction::iterator It = BB; 6681 ++It; 6682 6683 // thisMBB: 6684 // ... 6685 // TrueVal = ... 6686 // cmpTY ccX, r1, r2 6687 // bCC copy1MBB 6688 // fallthrough --> copy0MBB 6689 MachineBasicBlock *thisMBB = BB; 6690 MachineFunction *F = BB->getParent(); 6691 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 6692 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 6693 F->insert(It, copy0MBB); 6694 F->insert(It, sinkMBB); 6695 6696 // Transfer the remainder of BB and its successor edges to sinkMBB. 6697 sinkMBB->splice(sinkMBB->begin(), BB, 6698 llvm::next(MachineBasicBlock::iterator(MI)), 6699 BB->end()); 6700 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 6701 6702 BB->addSuccessor(copy0MBB); 6703 BB->addSuccessor(sinkMBB); 6704 6705 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB) 6706 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); 6707 6708 // copy0MBB: 6709 // %FalseValue = ... 6710 // # fallthrough to sinkMBB 6711 BB = copy0MBB; 6712 6713 // Update machine-CFG edges 6714 BB->addSuccessor(sinkMBB); 6715 6716 // sinkMBB: 6717 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 6718 // ... 6719 BB = sinkMBB; 6720 BuildMI(*BB, BB->begin(), dl, 6721 TII->get(ARM::PHI), MI->getOperand(0).getReg()) 6722 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 6723 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 6724 6725 MI->eraseFromParent(); // The pseudo instruction is gone now. 6726 return BB; 6727 } 6728 6729 case ARM::BCCi64: 6730 case ARM::BCCZi64: { 6731 // If there is an unconditional branch to the other successor, remove it. 6732 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end()); 6733 6734 // Compare both parts that make up the double comparison separately for 6735 // equality. 6736 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64; 6737 6738 unsigned LHS1 = MI->getOperand(1).getReg(); 6739 unsigned LHS2 = MI->getOperand(2).getReg(); 6740 if (RHSisZero) { 6741 AddDefaultPred(BuildMI(BB, dl, 6742 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6743 .addReg(LHS1).addImm(0)); 6744 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6745 .addReg(LHS2).addImm(0) 6746 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 6747 } else { 6748 unsigned RHS1 = MI->getOperand(3).getReg(); 6749 unsigned RHS2 = MI->getOperand(4).getReg(); 6750 AddDefaultPred(BuildMI(BB, dl, 6751 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 6752 .addReg(LHS1).addReg(RHS1)); 6753 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 6754 .addReg(LHS2).addReg(RHS2) 6755 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 6756 } 6757 6758 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB(); 6759 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 6760 if (MI->getOperand(0).getImm() == ARMCC::NE) 6761 std::swap(destMBB, exitMBB); 6762 6763 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6764 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 6765 if (isThumb2) 6766 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB)); 6767 else 6768 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 6769 6770 MI->eraseFromParent(); // The pseudo instruction is gone now. 6771 return BB; 6772 } 6773 6774 case ARM::Int_eh_sjlj_setjmp: 6775 case ARM::Int_eh_sjlj_setjmp_nofp: 6776 case ARM::tInt_eh_sjlj_setjmp: 6777 case ARM::t2Int_eh_sjlj_setjmp: 6778 case ARM::t2Int_eh_sjlj_setjmp_nofp: 6779 EmitSjLjDispatchBlock(MI, BB); 6780 return BB; 6781 6782 case ARM::ABS: 6783 case ARM::t2ABS: { 6784 // To insert an ABS instruction, we have to insert the 6785 // diamond control-flow pattern. The incoming instruction knows the 6786 // source vreg to test against 0, the destination vreg to set, 6787 // the condition code register to branch on, the 6788 // true/false values to select between, and a branch opcode to use. 6789 // It transforms 6790 // V1 = ABS V0 6791 // into 6792 // V2 = MOVS V0 6793 // BCC (branch to SinkBB if V0 >= 0) 6794 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 6795 // SinkBB: V1 = PHI(V2, V3) 6796 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6797 MachineFunction::iterator BBI = BB; 6798 ++BBI; 6799 MachineFunction *Fn = BB->getParent(); 6800 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 6801 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 6802 Fn->insert(BBI, RSBBB); 6803 Fn->insert(BBI, SinkBB); 6804 6805 unsigned int ABSSrcReg = MI->getOperand(1).getReg(); 6806 unsigned int ABSDstReg = MI->getOperand(0).getReg(); 6807 bool isThumb2 = Subtarget->isThumb2(); 6808 MachineRegisterInfo &MRI = Fn->getRegInfo(); 6809 // In Thumb mode S must not be specified if source register is the SP or 6810 // PC and if destination register is the SP, so restrict register class 6811 unsigned NewMovDstReg = MRI.createVirtualRegister(isThumb2 ? 6812 (const TargetRegisterClass*)&ARM::rGPRRegClass : 6813 (const TargetRegisterClass*)&ARM::GPRRegClass); 6814 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ? 6815 (const TargetRegisterClass*)&ARM::rGPRRegClass : 6816 (const TargetRegisterClass*)&ARM::GPRRegClass); 6817 6818 // Transfer the remainder of BB and its successor edges to sinkMBB. 6819 SinkBB->splice(SinkBB->begin(), BB, 6820 llvm::next(MachineBasicBlock::iterator(MI)), 6821 BB->end()); 6822 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 6823 6824 BB->addSuccessor(RSBBB); 6825 BB->addSuccessor(SinkBB); 6826 6827 // fall through to SinkMBB 6828 RSBBB->addSuccessor(SinkBB); 6829 6830 // insert a movs at the end of BB 6831 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr), 6832 NewMovDstReg) 6833 .addReg(ABSSrcReg, RegState::Kill) 6834 .addImm((unsigned)ARMCC::AL).addReg(0) 6835 .addReg(ARM::CPSR, RegState::Define); 6836 6837 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 6838 BuildMI(BB, dl, 6839 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 6840 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 6841 6842 // insert rsbri in RSBBB 6843 // Note: BCC and rsbri will be converted into predicated rsbmi 6844 // by if-conversion pass 6845 BuildMI(*RSBBB, RSBBB->begin(), dl, 6846 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 6847 .addReg(NewMovDstReg, RegState::Kill) 6848 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 6849 6850 // insert PHI in SinkBB, 6851 // reuse ABSDstReg to not change uses of ABS instruction 6852 BuildMI(*SinkBB, SinkBB->begin(), dl, 6853 TII->get(ARM::PHI), ABSDstReg) 6854 .addReg(NewRsbDstReg).addMBB(RSBBB) 6855 .addReg(NewMovDstReg).addMBB(BB); 6856 6857 // remove ABS instruction 6858 MI->eraseFromParent(); 6859 6860 // return last added BB 6861 return SinkBB; 6862 } 6863 case ARM::COPY_STRUCT_BYVAL_I32: 6864 ++NumLoopByVals; 6865 return EmitStructByval(MI, BB); 6866 } 6867 } 6868 6869 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 6870 SDNode *Node) const { 6871 if (!MI->hasPostISelHook()) { 6872 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) && 6873 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'"); 6874 return; 6875 } 6876 6877 const MCInstrDesc *MCID = &MI->getDesc(); 6878 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 6879 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 6880 // operand is still set to noreg. If needed, set the optional operand's 6881 // register to CPSR, and remove the redundant implicit def. 6882 // 6883 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>). 6884 6885 // Rename pseudo opcodes. 6886 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode()); 6887 if (NewOpc) { 6888 const ARMBaseInstrInfo *TII = 6889 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo()); 6890 MCID = &TII->get(NewOpc); 6891 6892 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 && 6893 "converted opcode should be the same except for cc_out"); 6894 6895 MI->setDesc(*MCID); 6896 6897 // Add the optional cc_out operand 6898 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 6899 } 6900 unsigned ccOutIdx = MCID->getNumOperands() - 1; 6901 6902 // Any ARM instruction that sets the 's' bit should specify an optional 6903 // "cc_out" operand in the last operand position. 6904 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 6905 assert(!NewOpc && "Optional cc_out operand required"); 6906 return; 6907 } 6908 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 6909 // since we already have an optional CPSR def. 6910 bool definesCPSR = false; 6911 bool deadCPSR = false; 6912 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands(); 6913 i != e; ++i) { 6914 const MachineOperand &MO = MI->getOperand(i); 6915 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 6916 definesCPSR = true; 6917 if (MO.isDead()) 6918 deadCPSR = true; 6919 MI->RemoveOperand(i); 6920 break; 6921 } 6922 } 6923 if (!definesCPSR) { 6924 assert(!NewOpc && "Optional cc_out operand required"); 6925 return; 6926 } 6927 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 6928 if (deadCPSR) { 6929 assert(!MI->getOperand(ccOutIdx).getReg() && 6930 "expect uninitialized optional cc_out operand"); 6931 return; 6932 } 6933 6934 // If this instruction was defined with an optional CPSR def and its dag node 6935 // had a live implicit CPSR def, then activate the optional CPSR def. 6936 MachineOperand &MO = MI->getOperand(ccOutIdx); 6937 MO.setReg(ARM::CPSR); 6938 MO.setIsDef(true); 6939 } 6940 6941 //===----------------------------------------------------------------------===// 6942 // ARM Optimization Hooks 6943 //===----------------------------------------------------------------------===// 6944 6945 static 6946 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 6947 TargetLowering::DAGCombinerInfo &DCI) { 6948 SelectionDAG &DAG = DCI.DAG; 6949 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6950 EVT VT = N->getValueType(0); 6951 unsigned Opc = N->getOpcode(); 6952 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC; 6953 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1); 6954 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2); 6955 ISD::CondCode CC = ISD::SETCC_INVALID; 6956 6957 if (isSlctCC) { 6958 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get(); 6959 } else { 6960 SDValue CCOp = Slct.getOperand(0); 6961 if (CCOp.getOpcode() == ISD::SETCC) 6962 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get(); 6963 } 6964 6965 bool DoXform = false; 6966 bool InvCC = false; 6967 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) && 6968 "Bad input!"); 6969 6970 if (LHS.getOpcode() == ISD::Constant && 6971 cast<ConstantSDNode>(LHS)->isNullValue()) { 6972 DoXform = true; 6973 } else if (CC != ISD::SETCC_INVALID && 6974 RHS.getOpcode() == ISD::Constant && 6975 cast<ConstantSDNode>(RHS)->isNullValue()) { 6976 std::swap(LHS, RHS); 6977 SDValue Op0 = Slct.getOperand(0); 6978 EVT OpVT = isSlctCC ? Op0.getValueType() : 6979 Op0.getOperand(0).getValueType(); 6980 bool isInt = OpVT.isInteger(); 6981 CC = ISD::getSetCCInverse(CC, isInt); 6982 6983 if (!TLI.isCondCodeLegal(CC, OpVT)) 6984 return SDValue(); // Inverse operator isn't legal. 6985 6986 DoXform = true; 6987 InvCC = true; 6988 } 6989 6990 if (DoXform) { 6991 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS); 6992 if (isSlctCC) 6993 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result, 6994 Slct.getOperand(0), Slct.getOperand(1), CC); 6995 SDValue CCOp = Slct.getOperand(0); 6996 if (InvCC) 6997 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(), 6998 CCOp.getOperand(0), CCOp.getOperand(1), CC); 6999 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT, 7000 CCOp, OtherOp, Result); 7001 } 7002 return SDValue(); 7003 } 7004 7005 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction 7006 // (only after legalization). 7007 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1, 7008 TargetLowering::DAGCombinerInfo &DCI, 7009 const ARMSubtarget *Subtarget) { 7010 7011 // Only perform optimization if after legalize, and if NEON is available. We 7012 // also expected both operands to be BUILD_VECTORs. 7013 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 7014 || N0.getOpcode() != ISD::BUILD_VECTOR 7015 || N1.getOpcode() != ISD::BUILD_VECTOR) 7016 return SDValue(); 7017 7018 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 7019 EVT VT = N->getValueType(0); 7020 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 7021 return SDValue(); 7022 7023 // Check that the vector operands are of the right form. 7024 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 7025 // operands, where N is the size of the formed vector. 7026 // Each EXTRACT_VECTOR should have the same input vector and odd or even 7027 // index such that we have a pair wise add pattern. 7028 7029 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 7030 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 7031 return SDValue(); 7032 SDValue Vec = N0->getOperand(0)->getOperand(0); 7033 SDNode *V = Vec.getNode(); 7034 unsigned nextIndex = 0; 7035 7036 // For each operands to the ADD which are BUILD_VECTORs, 7037 // check to see if each of their operands are an EXTRACT_VECTOR with 7038 // the same vector and appropriate index. 7039 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 7040 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 7041 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 7042 7043 SDValue ExtVec0 = N0->getOperand(i); 7044 SDValue ExtVec1 = N1->getOperand(i); 7045 7046 // First operand is the vector, verify its the same. 7047 if (V != ExtVec0->getOperand(0).getNode() || 7048 V != ExtVec1->getOperand(0).getNode()) 7049 return SDValue(); 7050 7051 // Second is the constant, verify its correct. 7052 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 7053 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 7054 7055 // For the constant, we want to see all the even or all the odd. 7056 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 7057 || C1->getZExtValue() != nextIndex+1) 7058 return SDValue(); 7059 7060 // Increment index. 7061 nextIndex+=2; 7062 } else 7063 return SDValue(); 7064 } 7065 7066 // Create VPADDL node. 7067 SelectionDAG &DAG = DCI.DAG; 7068 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7069 7070 // Build operand list. 7071 SmallVector<SDValue, 8> Ops; 7072 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, 7073 TLI.getPointerTy())); 7074 7075 // Input is the vector. 7076 Ops.push_back(Vec); 7077 7078 // Get widened type and narrowed type. 7079 MVT widenType; 7080 unsigned numElem = VT.getVectorNumElements(); 7081 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) { 7082 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 7083 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 7084 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 7085 default: 7086 llvm_unreachable("Invalid vector element type for padd optimization."); 7087 } 7088 7089 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), 7090 widenType, &Ops[0], Ops.size()); 7091 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp); 7092 } 7093 7094 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 7095 /// operands N0 and N1. This is a helper for PerformADDCombine that is 7096 /// called with the default operands, and if that fails, with commuted 7097 /// operands. 7098 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 7099 TargetLowering::DAGCombinerInfo &DCI, 7100 const ARMSubtarget *Subtarget){ 7101 7102 // Attempt to create vpaddl for this add. 7103 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget); 7104 if (Result.getNode()) 7105 return Result; 7106 7107 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 7108 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) { 7109 SDValue Result = combineSelectAndUse(N, N0, N1, DCI); 7110 if (Result.getNode()) return Result; 7111 } 7112 return SDValue(); 7113 } 7114 7115 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 7116 /// 7117 static SDValue PerformADDCombine(SDNode *N, 7118 TargetLowering::DAGCombinerInfo &DCI, 7119 const ARMSubtarget *Subtarget) { 7120 SDValue N0 = N->getOperand(0); 7121 SDValue N1 = N->getOperand(1); 7122 7123 // First try with the default operand order. 7124 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget); 7125 if (Result.getNode()) 7126 return Result; 7127 7128 // If that didn't work, try again with the operands commuted. 7129 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 7130 } 7131 7132 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 7133 /// 7134 static SDValue PerformSUBCombine(SDNode *N, 7135 TargetLowering::DAGCombinerInfo &DCI) { 7136 SDValue N0 = N->getOperand(0); 7137 SDValue N1 = N->getOperand(1); 7138 7139 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 7140 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) { 7141 SDValue Result = combineSelectAndUse(N, N1, N0, DCI); 7142 if (Result.getNode()) return Result; 7143 } 7144 7145 return SDValue(); 7146 } 7147 7148 /// PerformVMULCombine 7149 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 7150 /// special multiplier accumulator forwarding. 7151 /// vmul d3, d0, d2 7152 /// vmla d3, d1, d2 7153 /// is faster than 7154 /// vadd d3, d0, d1 7155 /// vmul d3, d3, d2 7156 static SDValue PerformVMULCombine(SDNode *N, 7157 TargetLowering::DAGCombinerInfo &DCI, 7158 const ARMSubtarget *Subtarget) { 7159 if (!Subtarget->hasVMLxForwarding()) 7160 return SDValue(); 7161 7162 SelectionDAG &DAG = DCI.DAG; 7163 SDValue N0 = N->getOperand(0); 7164 SDValue N1 = N->getOperand(1); 7165 unsigned Opcode = N0.getOpcode(); 7166 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 7167 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 7168 Opcode = N1.getOpcode(); 7169 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 7170 Opcode != ISD::FADD && Opcode != ISD::FSUB) 7171 return SDValue(); 7172 std::swap(N0, N1); 7173 } 7174 7175 EVT VT = N->getValueType(0); 7176 DebugLoc DL = N->getDebugLoc(); 7177 SDValue N00 = N0->getOperand(0); 7178 SDValue N01 = N0->getOperand(1); 7179 return DAG.getNode(Opcode, DL, VT, 7180 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 7181 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 7182 } 7183 7184 static SDValue PerformMULCombine(SDNode *N, 7185 TargetLowering::DAGCombinerInfo &DCI, 7186 const ARMSubtarget *Subtarget) { 7187 SelectionDAG &DAG = DCI.DAG; 7188 7189 if (Subtarget->isThumb1Only()) 7190 return SDValue(); 7191 7192 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 7193 return SDValue(); 7194 7195 EVT VT = N->getValueType(0); 7196 if (VT.is64BitVector() || VT.is128BitVector()) 7197 return PerformVMULCombine(N, DCI, Subtarget); 7198 if (VT != MVT::i32) 7199 return SDValue(); 7200 7201 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 7202 if (!C) 7203 return SDValue(); 7204 7205 int64_t MulAmt = C->getSExtValue(); 7206 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt); 7207 7208 ShiftAmt = ShiftAmt & (32 - 1); 7209 SDValue V = N->getOperand(0); 7210 DebugLoc DL = N->getDebugLoc(); 7211 7212 SDValue Res; 7213 MulAmt >>= ShiftAmt; 7214 7215 if (MulAmt >= 0) { 7216 if (isPowerOf2_32(MulAmt - 1)) { 7217 // (mul x, 2^N + 1) => (add (shl x, N), x) 7218 Res = DAG.getNode(ISD::ADD, DL, VT, 7219 V, 7220 DAG.getNode(ISD::SHL, DL, VT, 7221 V, 7222 DAG.getConstant(Log2_32(MulAmt - 1), 7223 MVT::i32))); 7224 } else if (isPowerOf2_32(MulAmt + 1)) { 7225 // (mul x, 2^N - 1) => (sub (shl x, N), x) 7226 Res = DAG.getNode(ISD::SUB, DL, VT, 7227 DAG.getNode(ISD::SHL, DL, VT, 7228 V, 7229 DAG.getConstant(Log2_32(MulAmt + 1), 7230 MVT::i32)), 7231 V); 7232 } else 7233 return SDValue(); 7234 } else { 7235 uint64_t MulAmtAbs = -MulAmt; 7236 if (isPowerOf2_32(MulAmtAbs + 1)) { 7237 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 7238 Res = DAG.getNode(ISD::SUB, DL, VT, 7239 V, 7240 DAG.getNode(ISD::SHL, DL, VT, 7241 V, 7242 DAG.getConstant(Log2_32(MulAmtAbs + 1), 7243 MVT::i32))); 7244 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 7245 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 7246 Res = DAG.getNode(ISD::ADD, DL, VT, 7247 V, 7248 DAG.getNode(ISD::SHL, DL, VT, 7249 V, 7250 DAG.getConstant(Log2_32(MulAmtAbs-1), 7251 MVT::i32))); 7252 Res = DAG.getNode(ISD::SUB, DL, VT, 7253 DAG.getConstant(0, MVT::i32),Res); 7254 7255 } else 7256 return SDValue(); 7257 } 7258 7259 if (ShiftAmt != 0) 7260 Res = DAG.getNode(ISD::SHL, DL, VT, 7261 Res, DAG.getConstant(ShiftAmt, MVT::i32)); 7262 7263 // Do not add new nodes to DAG combiner worklist. 7264 DCI.CombineTo(N, Res, false); 7265 return SDValue(); 7266 } 7267 7268 static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) { 7269 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse()) 7270 return false; 7271 7272 SDValue FalseVal = N.getOperand(0); 7273 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal); 7274 if (!C) 7275 return false; 7276 if (AllOnes) 7277 return C->isAllOnesValue(); 7278 return C->isNullValue(); 7279 } 7280 7281 /// formConditionalOp - Combine an operation with a conditional move operand 7282 /// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y) 7283 /// (and x, (cmov -1, y, cond)) => (and.cond, x, y) 7284 static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG, 7285 bool Commutable) { 7286 SDValue N0 = N->getOperand(0); 7287 SDValue N1 = N->getOperand(1); 7288 7289 bool isAND = N->getOpcode() == ISD::AND; 7290 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND); 7291 if (!isCand && Commutable) { 7292 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND); 7293 if (isCand) 7294 std::swap(N0, N1); 7295 } 7296 if (!isCand) 7297 return SDValue(); 7298 7299 unsigned Opc = 0; 7300 switch (N->getOpcode()) { 7301 default: llvm_unreachable("Unexpected node"); 7302 case ISD::AND: Opc = ARMISD::CAND; break; 7303 case ISD::OR: Opc = ARMISD::COR; break; 7304 case ISD::XOR: Opc = ARMISD::CXOR; break; 7305 } 7306 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0, 7307 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3), 7308 N1.getOperand(4)); 7309 } 7310 7311 static SDValue PerformANDCombine(SDNode *N, 7312 TargetLowering::DAGCombinerInfo &DCI, 7313 const ARMSubtarget *Subtarget) { 7314 7315 // Attempt to use immediate-form VBIC 7316 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 7317 DebugLoc dl = N->getDebugLoc(); 7318 EVT VT = N->getValueType(0); 7319 SelectionDAG &DAG = DCI.DAG; 7320 7321 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 7322 return SDValue(); 7323 7324 APInt SplatBits, SplatUndef; 7325 unsigned SplatBitSize; 7326 bool HasAnyUndefs; 7327 if (BVN && 7328 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 7329 if (SplatBitSize <= 64) { 7330 EVT VbicVT; 7331 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 7332 SplatUndef.getZExtValue(), SplatBitSize, 7333 DAG, VbicVT, VT.is128BitVector(), 7334 OtherModImm); 7335 if (Val.getNode()) { 7336 SDValue Input = 7337 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 7338 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 7339 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 7340 } 7341 } 7342 } 7343 7344 if (!Subtarget->isThumb1Only()) { 7345 // (and x, (cmov -1, y, cond)) => (and.cond x, y) 7346 SDValue CAND = formConditionalOp(N, DAG, true); 7347 if (CAND.getNode()) 7348 return CAND; 7349 } 7350 7351 return SDValue(); 7352 } 7353 7354 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 7355 static SDValue PerformORCombine(SDNode *N, 7356 TargetLowering::DAGCombinerInfo &DCI, 7357 const ARMSubtarget *Subtarget) { 7358 // Attempt to use immediate-form VORR 7359 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 7360 DebugLoc dl = N->getDebugLoc(); 7361 EVT VT = N->getValueType(0); 7362 SelectionDAG &DAG = DCI.DAG; 7363 7364 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 7365 return SDValue(); 7366 7367 APInt SplatBits, SplatUndef; 7368 unsigned SplatBitSize; 7369 bool HasAnyUndefs; 7370 if (BVN && Subtarget->hasNEON() && 7371 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 7372 if (SplatBitSize <= 64) { 7373 EVT VorrVT; 7374 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 7375 SplatUndef.getZExtValue(), SplatBitSize, 7376 DAG, VorrVT, VT.is128BitVector(), 7377 OtherModImm); 7378 if (Val.getNode()) { 7379 SDValue Input = 7380 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 7381 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 7382 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 7383 } 7384 } 7385 } 7386 7387 if (!Subtarget->isThumb1Only()) { 7388 // (or x, (cmov 0, y, cond)) => (or.cond x, y) 7389 SDValue COR = formConditionalOp(N, DAG, true); 7390 if (COR.getNode()) 7391 return COR; 7392 } 7393 7394 SDValue N0 = N->getOperand(0); 7395 if (N0.getOpcode() != ISD::AND) 7396 return SDValue(); 7397 SDValue N1 = N->getOperand(1); 7398 7399 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 7400 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 7401 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 7402 APInt SplatUndef; 7403 unsigned SplatBitSize; 7404 bool HasAnyUndefs; 7405 7406 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 7407 APInt SplatBits0; 7408 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 7409 HasAnyUndefs) && !HasAnyUndefs) { 7410 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 7411 APInt SplatBits1; 7412 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 7413 HasAnyUndefs) && !HasAnyUndefs && 7414 SplatBits0 == ~SplatBits1) { 7415 // Canonicalize the vector type to make instruction selection simpler. 7416 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 7417 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 7418 N0->getOperand(1), N0->getOperand(0), 7419 N1->getOperand(0)); 7420 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 7421 } 7422 } 7423 } 7424 7425 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 7426 // reasonable. 7427 7428 // BFI is only available on V6T2+ 7429 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 7430 return SDValue(); 7431 7432 DebugLoc DL = N->getDebugLoc(); 7433 // 1) or (and A, mask), val => ARMbfi A, val, mask 7434 // iff (val & mask) == val 7435 // 7436 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 7437 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 7438 // && mask == ~mask2 7439 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 7440 // && ~mask == mask2 7441 // (i.e., copy a bitfield value into another bitfield of the same width) 7442 7443 if (VT != MVT::i32) 7444 return SDValue(); 7445 7446 SDValue N00 = N0.getOperand(0); 7447 7448 // The value and the mask need to be constants so we can verify this is 7449 // actually a bitfield set. If the mask is 0xffff, we can do better 7450 // via a movt instruction, so don't use BFI in that case. 7451 SDValue MaskOp = N0.getOperand(1); 7452 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 7453 if (!MaskC) 7454 return SDValue(); 7455 unsigned Mask = MaskC->getZExtValue(); 7456 if (Mask == 0xffff) 7457 return SDValue(); 7458 SDValue Res; 7459 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 7460 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 7461 if (N1C) { 7462 unsigned Val = N1C->getZExtValue(); 7463 if ((Val & ~Mask) != Val) 7464 return SDValue(); 7465 7466 if (ARM::isBitFieldInvertedMask(Mask)) { 7467 Val >>= CountTrailingZeros_32(~Mask); 7468 7469 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 7470 DAG.getConstant(Val, MVT::i32), 7471 DAG.getConstant(Mask, MVT::i32)); 7472 7473 // Do not add new nodes to DAG combiner worklist. 7474 DCI.CombineTo(N, Res, false); 7475 return SDValue(); 7476 } 7477 } else if (N1.getOpcode() == ISD::AND) { 7478 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 7479 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 7480 if (!N11C) 7481 return SDValue(); 7482 unsigned Mask2 = N11C->getZExtValue(); 7483 7484 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 7485 // as is to match. 7486 if (ARM::isBitFieldInvertedMask(Mask) && 7487 (Mask == ~Mask2)) { 7488 // The pack halfword instruction works better for masks that fit it, 7489 // so use that when it's available. 7490 if (Subtarget->hasT2ExtractPack() && 7491 (Mask == 0xffff || Mask == 0xffff0000)) 7492 return SDValue(); 7493 // 2a 7494 unsigned amt = CountTrailingZeros_32(Mask2); 7495 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 7496 DAG.getConstant(amt, MVT::i32)); 7497 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 7498 DAG.getConstant(Mask, MVT::i32)); 7499 // Do not add new nodes to DAG combiner worklist. 7500 DCI.CombineTo(N, Res, false); 7501 return SDValue(); 7502 } else if (ARM::isBitFieldInvertedMask(~Mask) && 7503 (~Mask == Mask2)) { 7504 // The pack halfword instruction works better for masks that fit it, 7505 // so use that when it's available. 7506 if (Subtarget->hasT2ExtractPack() && 7507 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 7508 return SDValue(); 7509 // 2b 7510 unsigned lsb = CountTrailingZeros_32(Mask); 7511 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 7512 DAG.getConstant(lsb, MVT::i32)); 7513 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 7514 DAG.getConstant(Mask2, MVT::i32)); 7515 // Do not add new nodes to DAG combiner worklist. 7516 DCI.CombineTo(N, Res, false); 7517 return SDValue(); 7518 } 7519 } 7520 7521 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 7522 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 7523 ARM::isBitFieldInvertedMask(~Mask)) { 7524 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 7525 // where lsb(mask) == #shamt and masked bits of B are known zero. 7526 SDValue ShAmt = N00.getOperand(1); 7527 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 7528 unsigned LSB = CountTrailingZeros_32(Mask); 7529 if (ShAmtC != LSB) 7530 return SDValue(); 7531 7532 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 7533 DAG.getConstant(~Mask, MVT::i32)); 7534 7535 // Do not add new nodes to DAG combiner worklist. 7536 DCI.CombineTo(N, Res, false); 7537 } 7538 7539 return SDValue(); 7540 } 7541 7542 static SDValue PerformXORCombine(SDNode *N, 7543 TargetLowering::DAGCombinerInfo &DCI, 7544 const ARMSubtarget *Subtarget) { 7545 EVT VT = N->getValueType(0); 7546 SelectionDAG &DAG = DCI.DAG; 7547 7548 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 7549 return SDValue(); 7550 7551 if (!Subtarget->isThumb1Only()) { 7552 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y) 7553 SDValue CXOR = formConditionalOp(N, DAG, true); 7554 if (CXOR.getNode()) 7555 return CXOR; 7556 } 7557 7558 return SDValue(); 7559 } 7560 7561 /// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 7562 /// the bits being cleared by the AND are not demanded by the BFI. 7563 static SDValue PerformBFICombine(SDNode *N, 7564 TargetLowering::DAGCombinerInfo &DCI) { 7565 SDValue N1 = N->getOperand(1); 7566 if (N1.getOpcode() == ISD::AND) { 7567 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 7568 if (!N11C) 7569 return SDValue(); 7570 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 7571 unsigned LSB = CountTrailingZeros_32(~InvMask); 7572 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB; 7573 unsigned Mask = (1 << Width)-1; 7574 unsigned Mask2 = N11C->getZExtValue(); 7575 if ((Mask & (~Mask2)) == 0) 7576 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0), 7577 N->getOperand(0), N1.getOperand(0), 7578 N->getOperand(2)); 7579 } 7580 return SDValue(); 7581 } 7582 7583 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 7584 /// ARMISD::VMOVRRD. 7585 static SDValue PerformVMOVRRDCombine(SDNode *N, 7586 TargetLowering::DAGCombinerInfo &DCI) { 7587 // vmovrrd(vmovdrr x, y) -> x,y 7588 SDValue InDouble = N->getOperand(0); 7589 if (InDouble.getOpcode() == ARMISD::VMOVDRR) 7590 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 7591 7592 // vmovrrd(load f64) -> (load i32), (load i32) 7593 SDNode *InNode = InDouble.getNode(); 7594 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 7595 InNode->getValueType(0) == MVT::f64 && 7596 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 7597 !cast<LoadSDNode>(InNode)->isVolatile()) { 7598 // TODO: Should this be done for non-FrameIndex operands? 7599 LoadSDNode *LD = cast<LoadSDNode>(InNode); 7600 7601 SelectionDAG &DAG = DCI.DAG; 7602 DebugLoc DL = LD->getDebugLoc(); 7603 SDValue BasePtr = LD->getBasePtr(); 7604 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, 7605 LD->getPointerInfo(), LD->isVolatile(), 7606 LD->isNonTemporal(), LD->isInvariant(), 7607 LD->getAlignment()); 7608 7609 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 7610 DAG.getConstant(4, MVT::i32)); 7611 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, 7612 LD->getPointerInfo(), LD->isVolatile(), 7613 LD->isNonTemporal(), LD->isInvariant(), 7614 std::min(4U, LD->getAlignment() / 2)); 7615 7616 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 7617 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 7618 DCI.RemoveFromWorklist(LD); 7619 DAG.DeleteNode(LD); 7620 return Result; 7621 } 7622 7623 return SDValue(); 7624 } 7625 7626 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 7627 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 7628 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 7629 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 7630 SDValue Op0 = N->getOperand(0); 7631 SDValue Op1 = N->getOperand(1); 7632 if (Op0.getOpcode() == ISD::BITCAST) 7633 Op0 = Op0.getOperand(0); 7634 if (Op1.getOpcode() == ISD::BITCAST) 7635 Op1 = Op1.getOperand(0); 7636 if (Op0.getOpcode() == ARMISD::VMOVRRD && 7637 Op0.getNode() == Op1.getNode() && 7638 Op0.getResNo() == 0 && Op1.getResNo() == 1) 7639 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), 7640 N->getValueType(0), Op0.getOperand(0)); 7641 return SDValue(); 7642 } 7643 7644 /// PerformSTORECombine - Target-specific dag combine xforms for 7645 /// ISD::STORE. 7646 static SDValue PerformSTORECombine(SDNode *N, 7647 TargetLowering::DAGCombinerInfo &DCI) { 7648 StoreSDNode *St = cast<StoreSDNode>(N); 7649 if (St->isVolatile()) 7650 return SDValue(); 7651 7652 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 7653 // pack all of the elements in one place. Next, store to memory in fewer 7654 // chunks. 7655 SDValue StVal = St->getValue(); 7656 EVT VT = StVal.getValueType(); 7657 if (St->isTruncatingStore() && VT.isVector()) { 7658 SelectionDAG &DAG = DCI.DAG; 7659 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7660 EVT StVT = St->getMemoryVT(); 7661 unsigned NumElems = VT.getVectorNumElements(); 7662 assert(StVT != VT && "Cannot truncate to the same type"); 7663 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits(); 7664 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits(); 7665 7666 // From, To sizes and ElemCount must be pow of two 7667 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 7668 7669 // We are going to use the original vector elt for storing. 7670 // Accumulated smaller vector elements must be a multiple of the store size. 7671 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 7672 7673 unsigned SizeRatio = FromEltSz / ToEltSz; 7674 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 7675 7676 // Create a type on which we perform the shuffle. 7677 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 7678 NumElems*SizeRatio); 7679 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 7680 7681 DebugLoc DL = St->getDebugLoc(); 7682 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 7683 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 7684 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio; 7685 7686 // Can't shuffle using an illegal type. 7687 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 7688 7689 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 7690 DAG.getUNDEF(WideVec.getValueType()), 7691 ShuffleVec.data()); 7692 // At this point all of the data is stored at the bottom of the 7693 // register. We now need to save it to mem. 7694 7695 // Find the largest store unit 7696 MVT StoreType = MVT::i8; 7697 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE; 7698 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) { 7699 MVT Tp = (MVT::SimpleValueType)tp; 7700 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 7701 StoreType = Tp; 7702 } 7703 // Didn't find a legal store type. 7704 if (!TLI.isTypeLegal(StoreType)) 7705 return SDValue(); 7706 7707 // Bitcast the original vector into a vector of store-size units 7708 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 7709 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 7710 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 7711 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 7712 SmallVector<SDValue, 8> Chains; 7713 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8, 7714 TLI.getPointerTy()); 7715 SDValue BasePtr = St->getBasePtr(); 7716 7717 // Perform one or more big stores into memory. 7718 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 7719 for (unsigned I = 0; I < E; I++) { 7720 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 7721 StoreType, ShuffWide, 7722 DAG.getIntPtrConstant(I)); 7723 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 7724 St->getPointerInfo(), St->isVolatile(), 7725 St->isNonTemporal(), St->getAlignment()); 7726 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 7727 Increment); 7728 Chains.push_back(Ch); 7729 } 7730 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0], 7731 Chains.size()); 7732 } 7733 7734 if (!ISD::isNormalStore(St)) 7735 return SDValue(); 7736 7737 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 7738 // ARM stores of arguments in the same cache line. 7739 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 7740 StVal.getNode()->hasOneUse()) { 7741 SelectionDAG &DAG = DCI.DAG; 7742 DebugLoc DL = St->getDebugLoc(); 7743 SDValue BasePtr = St->getBasePtr(); 7744 SDValue NewST1 = DAG.getStore(St->getChain(), DL, 7745 StVal.getNode()->getOperand(0), BasePtr, 7746 St->getPointerInfo(), St->isVolatile(), 7747 St->isNonTemporal(), St->getAlignment()); 7748 7749 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 7750 DAG.getConstant(4, MVT::i32)); 7751 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1), 7752 OffsetPtr, St->getPointerInfo(), St->isVolatile(), 7753 St->isNonTemporal(), 7754 std::min(4U, St->getAlignment() / 2)); 7755 } 7756 7757 if (StVal.getValueType() != MVT::i64 || 7758 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 7759 return SDValue(); 7760 7761 // Bitcast an i64 store extracted from a vector to f64. 7762 // Otherwise, the i64 value will be legalized to a pair of i32 values. 7763 SelectionDAG &DAG = DCI.DAG; 7764 DebugLoc dl = StVal.getDebugLoc(); 7765 SDValue IntVec = StVal.getOperand(0); 7766 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 7767 IntVec.getValueType().getVectorNumElements()); 7768 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 7769 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 7770 Vec, StVal.getOperand(1)); 7771 dl = N->getDebugLoc(); 7772 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 7773 // Make the DAGCombiner fold the bitcasts. 7774 DCI.AddToWorklist(Vec.getNode()); 7775 DCI.AddToWorklist(ExtElt.getNode()); 7776 DCI.AddToWorklist(V.getNode()); 7777 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 7778 St->getPointerInfo(), St->isVolatile(), 7779 St->isNonTemporal(), St->getAlignment(), 7780 St->getTBAAInfo()); 7781 } 7782 7783 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 7784 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 7785 /// i64 vector to have f64 elements, since the value can then be loaded 7786 /// directly into a VFP register. 7787 static bool hasNormalLoadOperand(SDNode *N) { 7788 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 7789 for (unsigned i = 0; i < NumElts; ++i) { 7790 SDNode *Elt = N->getOperand(i).getNode(); 7791 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 7792 return true; 7793 } 7794 return false; 7795 } 7796 7797 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 7798 /// ISD::BUILD_VECTOR. 7799 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 7800 TargetLowering::DAGCombinerInfo &DCI){ 7801 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 7802 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 7803 // into a pair of GPRs, which is fine when the value is used as a scalar, 7804 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 7805 SelectionDAG &DAG = DCI.DAG; 7806 if (N->getNumOperands() == 2) { 7807 SDValue RV = PerformVMOVDRRCombine(N, DAG); 7808 if (RV.getNode()) 7809 return RV; 7810 } 7811 7812 // Load i64 elements as f64 values so that type legalization does not split 7813 // them up into i32 values. 7814 EVT VT = N->getValueType(0); 7815 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 7816 return SDValue(); 7817 DebugLoc dl = N->getDebugLoc(); 7818 SmallVector<SDValue, 8> Ops; 7819 unsigned NumElts = VT.getVectorNumElements(); 7820 for (unsigned i = 0; i < NumElts; ++i) { 7821 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 7822 Ops.push_back(V); 7823 // Make the DAGCombiner fold the bitcast. 7824 DCI.AddToWorklist(V.getNode()); 7825 } 7826 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 7827 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts); 7828 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 7829 } 7830 7831 /// PerformInsertEltCombine - Target-specific dag combine xforms for 7832 /// ISD::INSERT_VECTOR_ELT. 7833 static SDValue PerformInsertEltCombine(SDNode *N, 7834 TargetLowering::DAGCombinerInfo &DCI) { 7835 // Bitcast an i64 load inserted into a vector to f64. 7836 // Otherwise, the i64 value will be legalized to a pair of i32 values. 7837 EVT VT = N->getValueType(0); 7838 SDNode *Elt = N->getOperand(1).getNode(); 7839 if (VT.getVectorElementType() != MVT::i64 || 7840 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 7841 return SDValue(); 7842 7843 SelectionDAG &DAG = DCI.DAG; 7844 DebugLoc dl = N->getDebugLoc(); 7845 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 7846 VT.getVectorNumElements()); 7847 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 7848 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 7849 // Make the DAGCombiner fold the bitcasts. 7850 DCI.AddToWorklist(Vec.getNode()); 7851 DCI.AddToWorklist(V.getNode()); 7852 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 7853 Vec, V, N->getOperand(2)); 7854 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 7855 } 7856 7857 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 7858 /// ISD::VECTOR_SHUFFLE. 7859 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 7860 // The LLVM shufflevector instruction does not require the shuffle mask 7861 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 7862 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 7863 // operands do not match the mask length, they are extended by concatenating 7864 // them with undef vectors. That is probably the right thing for other 7865 // targets, but for NEON it is better to concatenate two double-register 7866 // size vector operands into a single quad-register size vector. Do that 7867 // transformation here: 7868 // shuffle(concat(v1, undef), concat(v2, undef)) -> 7869 // shuffle(concat(v1, v2), undef) 7870 SDValue Op0 = N->getOperand(0); 7871 SDValue Op1 = N->getOperand(1); 7872 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 7873 Op1.getOpcode() != ISD::CONCAT_VECTORS || 7874 Op0.getNumOperands() != 2 || 7875 Op1.getNumOperands() != 2) 7876 return SDValue(); 7877 SDValue Concat0Op1 = Op0.getOperand(1); 7878 SDValue Concat1Op1 = Op1.getOperand(1); 7879 if (Concat0Op1.getOpcode() != ISD::UNDEF || 7880 Concat1Op1.getOpcode() != ISD::UNDEF) 7881 return SDValue(); 7882 // Skip the transformation if any of the types are illegal. 7883 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7884 EVT VT = N->getValueType(0); 7885 if (!TLI.isTypeLegal(VT) || 7886 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 7887 !TLI.isTypeLegal(Concat1Op1.getValueType())) 7888 return SDValue(); 7889 7890 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT, 7891 Op0.getOperand(0), Op1.getOperand(0)); 7892 // Translate the shuffle mask. 7893 SmallVector<int, 16> NewMask; 7894 unsigned NumElts = VT.getVectorNumElements(); 7895 unsigned HalfElts = NumElts/2; 7896 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 7897 for (unsigned n = 0; n < NumElts; ++n) { 7898 int MaskElt = SVN->getMaskElt(n); 7899 int NewElt = -1; 7900 if (MaskElt < (int)HalfElts) 7901 NewElt = MaskElt; 7902 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 7903 NewElt = HalfElts + MaskElt - NumElts; 7904 NewMask.push_back(NewElt); 7905 } 7906 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat, 7907 DAG.getUNDEF(VT), NewMask.data()); 7908 } 7909 7910 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and 7911 /// NEON load/store intrinsics to merge base address updates. 7912 static SDValue CombineBaseUpdate(SDNode *N, 7913 TargetLowering::DAGCombinerInfo &DCI) { 7914 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 7915 return SDValue(); 7916 7917 SelectionDAG &DAG = DCI.DAG; 7918 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 7919 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 7920 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1); 7921 SDValue Addr = N->getOperand(AddrOpIdx); 7922 7923 // Search for a use of the address operand that is an increment. 7924 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 7925 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 7926 SDNode *User = *UI; 7927 if (User->getOpcode() != ISD::ADD || 7928 UI.getUse().getResNo() != Addr.getResNo()) 7929 continue; 7930 7931 // Check that the add is independent of the load/store. Otherwise, folding 7932 // it would create a cycle. 7933 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 7934 continue; 7935 7936 // Find the new opcode for the updating load/store. 7937 bool isLoad = true; 7938 bool isLaneOp = false; 7939 unsigned NewOpc = 0; 7940 unsigned NumVecs = 0; 7941 if (isIntrinsic) { 7942 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 7943 switch (IntNo) { 7944 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 7945 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 7946 NumVecs = 1; break; 7947 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 7948 NumVecs = 2; break; 7949 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 7950 NumVecs = 3; break; 7951 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 7952 NumVecs = 4; break; 7953 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 7954 NumVecs = 2; isLaneOp = true; break; 7955 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 7956 NumVecs = 3; isLaneOp = true; break; 7957 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 7958 NumVecs = 4; isLaneOp = true; break; 7959 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 7960 NumVecs = 1; isLoad = false; break; 7961 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 7962 NumVecs = 2; isLoad = false; break; 7963 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 7964 NumVecs = 3; isLoad = false; break; 7965 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 7966 NumVecs = 4; isLoad = false; break; 7967 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 7968 NumVecs = 2; isLoad = false; isLaneOp = true; break; 7969 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 7970 NumVecs = 3; isLoad = false; isLaneOp = true; break; 7971 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 7972 NumVecs = 4; isLoad = false; isLaneOp = true; break; 7973 } 7974 } else { 7975 isLaneOp = true; 7976 switch (N->getOpcode()) { 7977 default: llvm_unreachable("unexpected opcode for Neon base update"); 7978 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 7979 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 7980 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 7981 } 7982 } 7983 7984 // Find the size of memory referenced by the load/store. 7985 EVT VecTy; 7986 if (isLoad) 7987 VecTy = N->getValueType(0); 7988 else 7989 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 7990 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 7991 if (isLaneOp) 7992 NumBytes /= VecTy.getVectorNumElements(); 7993 7994 // If the increment is a constant, it must match the memory ref size. 7995 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 7996 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 7997 uint64_t IncVal = CInc->getZExtValue(); 7998 if (IncVal != NumBytes) 7999 continue; 8000 } else if (NumBytes >= 3 * 16) { 8001 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 8002 // separate instructions that make it harder to use a non-constant update. 8003 continue; 8004 } 8005 8006 // Create the new updating load/store node. 8007 EVT Tys[6]; 8008 unsigned NumResultVecs = (isLoad ? NumVecs : 0); 8009 unsigned n; 8010 for (n = 0; n < NumResultVecs; ++n) 8011 Tys[n] = VecTy; 8012 Tys[n++] = MVT::i32; 8013 Tys[n] = MVT::Other; 8014 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2); 8015 SmallVector<SDValue, 8> Ops; 8016 Ops.push_back(N->getOperand(0)); // incoming chain 8017 Ops.push_back(N->getOperand(AddrOpIdx)); 8018 Ops.push_back(Inc); 8019 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) { 8020 Ops.push_back(N->getOperand(i)); 8021 } 8022 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N); 8023 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys, 8024 Ops.data(), Ops.size(), 8025 MemInt->getMemoryVT(), 8026 MemInt->getMemOperand()); 8027 8028 // Update the uses. 8029 std::vector<SDValue> NewResults; 8030 for (unsigned i = 0; i < NumResultVecs; ++i) { 8031 NewResults.push_back(SDValue(UpdN.getNode(), i)); 8032 } 8033 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 8034 DCI.CombineTo(N, NewResults); 8035 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 8036 8037 break; 8038 } 8039 return SDValue(); 8040 } 8041 8042 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 8043 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 8044 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 8045 /// return true. 8046 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 8047 SelectionDAG &DAG = DCI.DAG; 8048 EVT VT = N->getValueType(0); 8049 // vldN-dup instructions only support 64-bit vectors for N > 1. 8050 if (!VT.is64BitVector()) 8051 return false; 8052 8053 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 8054 SDNode *VLD = N->getOperand(0).getNode(); 8055 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 8056 return false; 8057 unsigned NumVecs = 0; 8058 unsigned NewOpc = 0; 8059 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 8060 if (IntNo == Intrinsic::arm_neon_vld2lane) { 8061 NumVecs = 2; 8062 NewOpc = ARMISD::VLD2DUP; 8063 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 8064 NumVecs = 3; 8065 NewOpc = ARMISD::VLD3DUP; 8066 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 8067 NumVecs = 4; 8068 NewOpc = ARMISD::VLD4DUP; 8069 } else { 8070 return false; 8071 } 8072 8073 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 8074 // numbers match the load. 8075 unsigned VLDLaneNo = 8076 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 8077 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 8078 UI != UE; ++UI) { 8079 // Ignore uses of the chain result. 8080 if (UI.getUse().getResNo() == NumVecs) 8081 continue; 8082 SDNode *User = *UI; 8083 if (User->getOpcode() != ARMISD::VDUPLANE || 8084 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 8085 return false; 8086 } 8087 8088 // Create the vldN-dup node. 8089 EVT Tys[5]; 8090 unsigned n; 8091 for (n = 0; n < NumVecs; ++n) 8092 Tys[n] = VT; 8093 Tys[n] = MVT::Other; 8094 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1); 8095 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 8096 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 8097 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys, 8098 Ops, 2, VLDMemInt->getMemoryVT(), 8099 VLDMemInt->getMemOperand()); 8100 8101 // Update the uses. 8102 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 8103 UI != UE; ++UI) { 8104 unsigned ResNo = UI.getUse().getResNo(); 8105 // Ignore uses of the chain result. 8106 if (ResNo == NumVecs) 8107 continue; 8108 SDNode *User = *UI; 8109 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 8110 } 8111 8112 // Now the vldN-lane intrinsic is dead except for its chain result. 8113 // Update uses of the chain. 8114 std::vector<SDValue> VLDDupResults; 8115 for (unsigned n = 0; n < NumVecs; ++n) 8116 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 8117 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 8118 DCI.CombineTo(VLD, VLDDupResults); 8119 8120 return true; 8121 } 8122 8123 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 8124 /// ARMISD::VDUPLANE. 8125 static SDValue PerformVDUPLANECombine(SDNode *N, 8126 TargetLowering::DAGCombinerInfo &DCI) { 8127 SDValue Op = N->getOperand(0); 8128 8129 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 8130 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 8131 if (CombineVLDDUP(N, DCI)) 8132 return SDValue(N, 0); 8133 8134 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 8135 // redundant. Ignore bit_converts for now; element sizes are checked below. 8136 while (Op.getOpcode() == ISD::BITCAST) 8137 Op = Op.getOperand(0); 8138 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 8139 return SDValue(); 8140 8141 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 8142 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits(); 8143 // The canonical VMOV for a zero vector uses a 32-bit element size. 8144 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 8145 unsigned EltBits; 8146 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 8147 EltSize = 8; 8148 EVT VT = N->getValueType(0); 8149 if (EltSize > VT.getVectorElementType().getSizeInBits()) 8150 return SDValue(); 8151 8152 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op); 8153 } 8154 8155 // isConstVecPow2 - Return true if each vector element is a power of 2, all 8156 // elements are the same constant, C, and Log2(C) ranges from 1 to 32. 8157 static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C) 8158 { 8159 integerPart cN; 8160 integerPart c0 = 0; 8161 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements(); 8162 I != E; I++) { 8163 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I)); 8164 if (!C) 8165 return false; 8166 8167 bool isExact; 8168 APFloat APF = C->getValueAPF(); 8169 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact) 8170 != APFloat::opOK || !isExact) 8171 return false; 8172 8173 c0 = (I == 0) ? cN : c0; 8174 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32) 8175 return false; 8176 } 8177 C = c0; 8178 return true; 8179 } 8180 8181 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 8182 /// can replace combinations of VMUL and VCVT (floating-point to integer) 8183 /// when the VMUL has a constant operand that is a power of 2. 8184 /// 8185 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 8186 /// vmul.f32 d16, d17, d16 8187 /// vcvt.s32.f32 d16, d16 8188 /// becomes: 8189 /// vcvt.s32.f32 d16, d16, #3 8190 static SDValue PerformVCVTCombine(SDNode *N, 8191 TargetLowering::DAGCombinerInfo &DCI, 8192 const ARMSubtarget *Subtarget) { 8193 SelectionDAG &DAG = DCI.DAG; 8194 SDValue Op = N->getOperand(0); 8195 8196 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() || 8197 Op.getOpcode() != ISD::FMUL) 8198 return SDValue(); 8199 8200 uint64_t C; 8201 SDValue N0 = Op->getOperand(0); 8202 SDValue ConstVec = Op->getOperand(1); 8203 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 8204 8205 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 8206 !isConstVecPow2(ConstVec, isSigned, C)) 8207 return SDValue(); 8208 8209 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 8210 Intrinsic::arm_neon_vcvtfp2fxu; 8211 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), 8212 N->getValueType(0), 8213 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0, 8214 DAG.getConstant(Log2_64(C), MVT::i32)); 8215 } 8216 8217 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 8218 /// can replace combinations of VCVT (integer to floating-point) and VDIV 8219 /// when the VDIV has a constant operand that is a power of 2. 8220 /// 8221 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 8222 /// vcvt.f32.s32 d16, d16 8223 /// vdiv.f32 d16, d17, d16 8224 /// becomes: 8225 /// vcvt.f32.s32 d16, d16, #3 8226 static SDValue PerformVDIVCombine(SDNode *N, 8227 TargetLowering::DAGCombinerInfo &DCI, 8228 const ARMSubtarget *Subtarget) { 8229 SelectionDAG &DAG = DCI.DAG; 8230 SDValue Op = N->getOperand(0); 8231 unsigned OpOpcode = Op.getNode()->getOpcode(); 8232 8233 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() || 8234 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 8235 return SDValue(); 8236 8237 uint64_t C; 8238 SDValue ConstVec = N->getOperand(1); 8239 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 8240 8241 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 8242 !isConstVecPow2(ConstVec, isSigned, C)) 8243 return SDValue(); 8244 8245 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 8246 Intrinsic::arm_neon_vcvtfxu2fp; 8247 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), 8248 Op.getValueType(), 8249 DAG.getConstant(IntrinsicOpcode, MVT::i32), 8250 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32)); 8251 } 8252 8253 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 8254 /// operand of a vector shift operation, where all the elements of the 8255 /// build_vector must have the same constant integer value. 8256 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 8257 // Ignore bit_converts. 8258 while (Op.getOpcode() == ISD::BITCAST) 8259 Op = Op.getOperand(0); 8260 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 8261 APInt SplatBits, SplatUndef; 8262 unsigned SplatBitSize; 8263 bool HasAnyUndefs; 8264 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 8265 HasAnyUndefs, ElementBits) || 8266 SplatBitSize > ElementBits) 8267 return false; 8268 Cnt = SplatBits.getSExtValue(); 8269 return true; 8270 } 8271 8272 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 8273 /// operand of a vector shift left operation. That value must be in the range: 8274 /// 0 <= Value < ElementBits for a left shift; or 8275 /// 0 <= Value <= ElementBits for a long left shift. 8276 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 8277 assert(VT.isVector() && "vector shift count is not a vector type"); 8278 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 8279 if (! getVShiftImm(Op, ElementBits, Cnt)) 8280 return false; 8281 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 8282 } 8283 8284 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 8285 /// operand of a vector shift right operation. For a shift opcode, the value 8286 /// is positive, but for an intrinsic the value count must be negative. The 8287 /// absolute value must be in the range: 8288 /// 1 <= |Value| <= ElementBits for a right shift; or 8289 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 8290 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 8291 int64_t &Cnt) { 8292 assert(VT.isVector() && "vector shift count is not a vector type"); 8293 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 8294 if (! getVShiftImm(Op, ElementBits, Cnt)) 8295 return false; 8296 if (isIntrinsic) 8297 Cnt = -Cnt; 8298 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 8299 } 8300 8301 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 8302 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 8303 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 8304 switch (IntNo) { 8305 default: 8306 // Don't do anything for most intrinsics. 8307 break; 8308 8309 // Vector shifts: check for immediate versions and lower them. 8310 // Note: This is done during DAG combining instead of DAG legalizing because 8311 // the build_vectors for 64-bit vector element shift counts are generally 8312 // not legal, and it is hard to see their values after they get legalized to 8313 // loads from a constant pool. 8314 case Intrinsic::arm_neon_vshifts: 8315 case Intrinsic::arm_neon_vshiftu: 8316 case Intrinsic::arm_neon_vshiftls: 8317 case Intrinsic::arm_neon_vshiftlu: 8318 case Intrinsic::arm_neon_vshiftn: 8319 case Intrinsic::arm_neon_vrshifts: 8320 case Intrinsic::arm_neon_vrshiftu: 8321 case Intrinsic::arm_neon_vrshiftn: 8322 case Intrinsic::arm_neon_vqshifts: 8323 case Intrinsic::arm_neon_vqshiftu: 8324 case Intrinsic::arm_neon_vqshiftsu: 8325 case Intrinsic::arm_neon_vqshiftns: 8326 case Intrinsic::arm_neon_vqshiftnu: 8327 case Intrinsic::arm_neon_vqshiftnsu: 8328 case Intrinsic::arm_neon_vqrshiftns: 8329 case Intrinsic::arm_neon_vqrshiftnu: 8330 case Intrinsic::arm_neon_vqrshiftnsu: { 8331 EVT VT = N->getOperand(1).getValueType(); 8332 int64_t Cnt; 8333 unsigned VShiftOpc = 0; 8334 8335 switch (IntNo) { 8336 case Intrinsic::arm_neon_vshifts: 8337 case Intrinsic::arm_neon_vshiftu: 8338 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 8339 VShiftOpc = ARMISD::VSHL; 8340 break; 8341 } 8342 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 8343 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 8344 ARMISD::VSHRs : ARMISD::VSHRu); 8345 break; 8346 } 8347 return SDValue(); 8348 8349 case Intrinsic::arm_neon_vshiftls: 8350 case Intrinsic::arm_neon_vshiftlu: 8351 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt)) 8352 break; 8353 llvm_unreachable("invalid shift count for vshll intrinsic"); 8354 8355 case Intrinsic::arm_neon_vrshifts: 8356 case Intrinsic::arm_neon_vrshiftu: 8357 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 8358 break; 8359 return SDValue(); 8360 8361 case Intrinsic::arm_neon_vqshifts: 8362 case Intrinsic::arm_neon_vqshiftu: 8363 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 8364 break; 8365 return SDValue(); 8366 8367 case Intrinsic::arm_neon_vqshiftsu: 8368 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 8369 break; 8370 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 8371 8372 case Intrinsic::arm_neon_vshiftn: 8373 case Intrinsic::arm_neon_vrshiftn: 8374 case Intrinsic::arm_neon_vqshiftns: 8375 case Intrinsic::arm_neon_vqshiftnu: 8376 case Intrinsic::arm_neon_vqshiftnsu: 8377 case Intrinsic::arm_neon_vqrshiftns: 8378 case Intrinsic::arm_neon_vqrshiftnu: 8379 case Intrinsic::arm_neon_vqrshiftnsu: 8380 // Narrowing shifts require an immediate right shift. 8381 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 8382 break; 8383 llvm_unreachable("invalid shift count for narrowing vector shift " 8384 "intrinsic"); 8385 8386 default: 8387 llvm_unreachable("unhandled vector shift"); 8388 } 8389 8390 switch (IntNo) { 8391 case Intrinsic::arm_neon_vshifts: 8392 case Intrinsic::arm_neon_vshiftu: 8393 // Opcode already set above. 8394 break; 8395 case Intrinsic::arm_neon_vshiftls: 8396 case Intrinsic::arm_neon_vshiftlu: 8397 if (Cnt == VT.getVectorElementType().getSizeInBits()) 8398 VShiftOpc = ARMISD::VSHLLi; 8399 else 8400 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ? 8401 ARMISD::VSHLLs : ARMISD::VSHLLu); 8402 break; 8403 case Intrinsic::arm_neon_vshiftn: 8404 VShiftOpc = ARMISD::VSHRN; break; 8405 case Intrinsic::arm_neon_vrshifts: 8406 VShiftOpc = ARMISD::VRSHRs; break; 8407 case Intrinsic::arm_neon_vrshiftu: 8408 VShiftOpc = ARMISD::VRSHRu; break; 8409 case Intrinsic::arm_neon_vrshiftn: 8410 VShiftOpc = ARMISD::VRSHRN; break; 8411 case Intrinsic::arm_neon_vqshifts: 8412 VShiftOpc = ARMISD::VQSHLs; break; 8413 case Intrinsic::arm_neon_vqshiftu: 8414 VShiftOpc = ARMISD::VQSHLu; break; 8415 case Intrinsic::arm_neon_vqshiftsu: 8416 VShiftOpc = ARMISD::VQSHLsu; break; 8417 case Intrinsic::arm_neon_vqshiftns: 8418 VShiftOpc = ARMISD::VQSHRNs; break; 8419 case Intrinsic::arm_neon_vqshiftnu: 8420 VShiftOpc = ARMISD::VQSHRNu; break; 8421 case Intrinsic::arm_neon_vqshiftnsu: 8422 VShiftOpc = ARMISD::VQSHRNsu; break; 8423 case Intrinsic::arm_neon_vqrshiftns: 8424 VShiftOpc = ARMISD::VQRSHRNs; break; 8425 case Intrinsic::arm_neon_vqrshiftnu: 8426 VShiftOpc = ARMISD::VQRSHRNu; break; 8427 case Intrinsic::arm_neon_vqrshiftnsu: 8428 VShiftOpc = ARMISD::VQRSHRNsu; break; 8429 } 8430 8431 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0), 8432 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32)); 8433 } 8434 8435 case Intrinsic::arm_neon_vshiftins: { 8436 EVT VT = N->getOperand(1).getValueType(); 8437 int64_t Cnt; 8438 unsigned VShiftOpc = 0; 8439 8440 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 8441 VShiftOpc = ARMISD::VSLI; 8442 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 8443 VShiftOpc = ARMISD::VSRI; 8444 else { 8445 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 8446 } 8447 8448 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0), 8449 N->getOperand(1), N->getOperand(2), 8450 DAG.getConstant(Cnt, MVT::i32)); 8451 } 8452 8453 case Intrinsic::arm_neon_vqrshifts: 8454 case Intrinsic::arm_neon_vqrshiftu: 8455 // No immediate versions of these to check for. 8456 break; 8457 } 8458 8459 return SDValue(); 8460 } 8461 8462 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 8463 /// lowers them. As with the vector shift intrinsics, this is done during DAG 8464 /// combining instead of DAG legalizing because the build_vectors for 64-bit 8465 /// vector element shift counts are generally not legal, and it is hard to see 8466 /// their values after they get legalized to loads from a constant pool. 8467 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 8468 const ARMSubtarget *ST) { 8469 EVT VT = N->getValueType(0); 8470 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 8471 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 8472 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 8473 SDValue N1 = N->getOperand(1); 8474 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 8475 SDValue N0 = N->getOperand(0); 8476 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 8477 DAG.MaskedValueIsZero(N0.getOperand(0), 8478 APInt::getHighBitsSet(32, 16))) 8479 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1); 8480 } 8481 } 8482 8483 // Nothing to be done for scalar shifts. 8484 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8485 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 8486 return SDValue(); 8487 8488 assert(ST->hasNEON() && "unexpected vector shift"); 8489 int64_t Cnt; 8490 8491 switch (N->getOpcode()) { 8492 default: llvm_unreachable("unexpected shift opcode"); 8493 8494 case ISD::SHL: 8495 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) 8496 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0), 8497 DAG.getConstant(Cnt, MVT::i32)); 8498 break; 8499 8500 case ISD::SRA: 8501 case ISD::SRL: 8502 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 8503 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 8504 ARMISD::VSHRs : ARMISD::VSHRu); 8505 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0), 8506 DAG.getConstant(Cnt, MVT::i32)); 8507 } 8508 } 8509 return SDValue(); 8510 } 8511 8512 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 8513 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 8514 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 8515 const ARMSubtarget *ST) { 8516 SDValue N0 = N->getOperand(0); 8517 8518 // Check for sign- and zero-extensions of vector extract operations of 8- 8519 // and 16-bit vector elements. NEON supports these directly. They are 8520 // handled during DAG combining because type legalization will promote them 8521 // to 32-bit types and it is messy to recognize the operations after that. 8522 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 8523 SDValue Vec = N0.getOperand(0); 8524 SDValue Lane = N0.getOperand(1); 8525 EVT VT = N->getValueType(0); 8526 EVT EltVT = N0.getValueType(); 8527 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8528 8529 if (VT == MVT::i32 && 8530 (EltVT == MVT::i8 || EltVT == MVT::i16) && 8531 TLI.isTypeLegal(Vec.getValueType()) && 8532 isa<ConstantSDNode>(Lane)) { 8533 8534 unsigned Opc = 0; 8535 switch (N->getOpcode()) { 8536 default: llvm_unreachable("unexpected opcode"); 8537 case ISD::SIGN_EXTEND: 8538 Opc = ARMISD::VGETLANEs; 8539 break; 8540 case ISD::ZERO_EXTEND: 8541 case ISD::ANY_EXTEND: 8542 Opc = ARMISD::VGETLANEu; 8543 break; 8544 } 8545 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane); 8546 } 8547 } 8548 8549 return SDValue(); 8550 } 8551 8552 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC 8553 /// to match f32 max/min patterns to use NEON vmax/vmin instructions. 8554 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG, 8555 const ARMSubtarget *ST) { 8556 // If the target supports NEON, try to use vmax/vmin instructions for f32 8557 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set, 8558 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is 8559 // a NaN; only do the transformation when it matches that behavior. 8560 8561 // For now only do this when using NEON for FP operations; if using VFP, it 8562 // is not obvious that the benefit outweighs the cost of switching to the 8563 // NEON pipeline. 8564 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() || 8565 N->getValueType(0) != MVT::f32) 8566 return SDValue(); 8567 8568 SDValue CondLHS = N->getOperand(0); 8569 SDValue CondRHS = N->getOperand(1); 8570 SDValue LHS = N->getOperand(2); 8571 SDValue RHS = N->getOperand(3); 8572 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 8573 8574 unsigned Opcode = 0; 8575 bool IsReversed; 8576 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) { 8577 IsReversed = false; // x CC y ? x : y 8578 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) { 8579 IsReversed = true ; // x CC y ? y : x 8580 } else { 8581 return SDValue(); 8582 } 8583 8584 bool IsUnordered; 8585 switch (CC) { 8586 default: break; 8587 case ISD::SETOLT: 8588 case ISD::SETOLE: 8589 case ISD::SETLT: 8590 case ISD::SETLE: 8591 case ISD::SETULT: 8592 case ISD::SETULE: 8593 // If LHS is NaN, an ordered comparison will be false and the result will 8594 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS 8595 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 8596 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE); 8597 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 8598 break; 8599 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin 8600 // will return -0, so vmin can only be used for unsafe math or if one of 8601 // the operands is known to be nonzero. 8602 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) && 8603 !DAG.getTarget().Options.UnsafeFPMath && 8604 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 8605 break; 8606 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN; 8607 break; 8608 8609 case ISD::SETOGT: 8610 case ISD::SETOGE: 8611 case ISD::SETGT: 8612 case ISD::SETGE: 8613 case ISD::SETUGT: 8614 case ISD::SETUGE: 8615 // If LHS is NaN, an ordered comparison will be false and the result will 8616 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS 8617 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 8618 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE); 8619 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 8620 break; 8621 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax 8622 // will return +0, so vmax can only be used for unsafe math or if one of 8623 // the operands is known to be nonzero. 8624 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) && 8625 !DAG.getTarget().Options.UnsafeFPMath && 8626 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 8627 break; 8628 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX; 8629 break; 8630 } 8631 8632 if (!Opcode) 8633 return SDValue(); 8634 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS); 8635 } 8636 8637 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 8638 SDValue 8639 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 8640 SDValue Cmp = N->getOperand(4); 8641 if (Cmp.getOpcode() != ARMISD::CMPZ) 8642 // Only looking at EQ and NE cases. 8643 return SDValue(); 8644 8645 EVT VT = N->getValueType(0); 8646 DebugLoc dl = N->getDebugLoc(); 8647 SDValue LHS = Cmp.getOperand(0); 8648 SDValue RHS = Cmp.getOperand(1); 8649 SDValue FalseVal = N->getOperand(0); 8650 SDValue TrueVal = N->getOperand(1); 8651 SDValue ARMcc = N->getOperand(2); 8652 ARMCC::CondCodes CC = 8653 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 8654 8655 // Simplify 8656 // mov r1, r0 8657 // cmp r1, x 8658 // mov r0, y 8659 // moveq r0, x 8660 // to 8661 // cmp r0, x 8662 // movne r0, y 8663 // 8664 // mov r1, r0 8665 // cmp r1, x 8666 // mov r0, x 8667 // movne r0, y 8668 // to 8669 // cmp r0, x 8670 // movne r0, y 8671 /// FIXME: Turn this into a target neutral optimization? 8672 SDValue Res; 8673 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 8674 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 8675 N->getOperand(3), Cmp); 8676 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 8677 SDValue ARMcc; 8678 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 8679 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 8680 N->getOperand(3), NewCmp); 8681 } 8682 8683 if (Res.getNode()) { 8684 APInt KnownZero, KnownOne; 8685 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne); 8686 // Capture demanded bits information that would be otherwise lost. 8687 if (KnownZero == 0xfffffffe) 8688 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 8689 DAG.getValueType(MVT::i1)); 8690 else if (KnownZero == 0xffffff00) 8691 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 8692 DAG.getValueType(MVT::i8)); 8693 else if (KnownZero == 0xffff0000) 8694 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 8695 DAG.getValueType(MVT::i16)); 8696 } 8697 8698 return Res; 8699 } 8700 8701 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 8702 DAGCombinerInfo &DCI) const { 8703 switch (N->getOpcode()) { 8704 default: break; 8705 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 8706 case ISD::SUB: return PerformSUBCombine(N, DCI); 8707 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 8708 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 8709 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 8710 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 8711 case ARMISD::BFI: return PerformBFICombine(N, DCI); 8712 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI); 8713 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 8714 case ISD::STORE: return PerformSTORECombine(N, DCI); 8715 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI); 8716 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 8717 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 8718 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 8719 case ISD::FP_TO_SINT: 8720 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget); 8721 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget); 8722 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 8723 case ISD::SHL: 8724 case ISD::SRA: 8725 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 8726 case ISD::SIGN_EXTEND: 8727 case ISD::ZERO_EXTEND: 8728 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 8729 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget); 8730 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 8731 case ARMISD::VLD2DUP: 8732 case ARMISD::VLD3DUP: 8733 case ARMISD::VLD4DUP: 8734 return CombineBaseUpdate(N, DCI); 8735 case ISD::INTRINSIC_VOID: 8736 case ISD::INTRINSIC_W_CHAIN: 8737 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 8738 case Intrinsic::arm_neon_vld1: 8739 case Intrinsic::arm_neon_vld2: 8740 case Intrinsic::arm_neon_vld3: 8741 case Intrinsic::arm_neon_vld4: 8742 case Intrinsic::arm_neon_vld2lane: 8743 case Intrinsic::arm_neon_vld3lane: 8744 case Intrinsic::arm_neon_vld4lane: 8745 case Intrinsic::arm_neon_vst1: 8746 case Intrinsic::arm_neon_vst2: 8747 case Intrinsic::arm_neon_vst3: 8748 case Intrinsic::arm_neon_vst4: 8749 case Intrinsic::arm_neon_vst2lane: 8750 case Intrinsic::arm_neon_vst3lane: 8751 case Intrinsic::arm_neon_vst4lane: 8752 return CombineBaseUpdate(N, DCI); 8753 default: break; 8754 } 8755 break; 8756 } 8757 return SDValue(); 8758 } 8759 8760 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 8761 EVT VT) const { 8762 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 8763 } 8764 8765 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const { 8766 if (!Subtarget->allowsUnalignedMem()) 8767 return false; 8768 8769 switch (VT.getSimpleVT().SimpleTy) { 8770 default: 8771 return false; 8772 case MVT::i8: 8773 case MVT::i16: 8774 case MVT::i32: 8775 return true; 8776 // FIXME: VLD1 etc with standard alignment is legal. 8777 } 8778 } 8779 8780 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 8781 unsigned AlignCheck) { 8782 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 8783 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 8784 } 8785 8786 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 8787 unsigned DstAlign, unsigned SrcAlign, 8788 bool IsZeroVal, 8789 bool MemcpyStrSrc, 8790 MachineFunction &MF) const { 8791 const Function *F = MF.getFunction(); 8792 8793 // See if we can use NEON instructions for this... 8794 if (IsZeroVal && 8795 !F->hasFnAttr(Attribute::NoImplicitFloat) && 8796 Subtarget->hasNEON()) { 8797 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) { 8798 return MVT::v4i32; 8799 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) { 8800 return MVT::v2i32; 8801 } 8802 } 8803 8804 // Lowering to i32/i16 if the size permits. 8805 if (Size >= 4) { 8806 return MVT::i32; 8807 } else if (Size >= 2) { 8808 return MVT::i16; 8809 } 8810 8811 // Let the target-independent logic figure it out. 8812 return MVT::Other; 8813 } 8814 8815 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 8816 if (V < 0) 8817 return false; 8818 8819 unsigned Scale = 1; 8820 switch (VT.getSimpleVT().SimpleTy) { 8821 default: return false; 8822 case MVT::i1: 8823 case MVT::i8: 8824 // Scale == 1; 8825 break; 8826 case MVT::i16: 8827 // Scale == 2; 8828 Scale = 2; 8829 break; 8830 case MVT::i32: 8831 // Scale == 4; 8832 Scale = 4; 8833 break; 8834 } 8835 8836 if ((V & (Scale - 1)) != 0) 8837 return false; 8838 V /= Scale; 8839 return V == (V & ((1LL << 5) - 1)); 8840 } 8841 8842 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 8843 const ARMSubtarget *Subtarget) { 8844 bool isNeg = false; 8845 if (V < 0) { 8846 isNeg = true; 8847 V = - V; 8848 } 8849 8850 switch (VT.getSimpleVT().SimpleTy) { 8851 default: return false; 8852 case MVT::i1: 8853 case MVT::i8: 8854 case MVT::i16: 8855 case MVT::i32: 8856 // + imm12 or - imm8 8857 if (isNeg) 8858 return V == (V & ((1LL << 8) - 1)); 8859 return V == (V & ((1LL << 12) - 1)); 8860 case MVT::f32: 8861 case MVT::f64: 8862 // Same as ARM mode. FIXME: NEON? 8863 if (!Subtarget->hasVFP2()) 8864 return false; 8865 if ((V & 3) != 0) 8866 return false; 8867 V >>= 2; 8868 return V == (V & ((1LL << 8) - 1)); 8869 } 8870 } 8871 8872 /// isLegalAddressImmediate - Return true if the integer value can be used 8873 /// as the offset of the target addressing mode for load / store of the 8874 /// given type. 8875 static bool isLegalAddressImmediate(int64_t V, EVT VT, 8876 const ARMSubtarget *Subtarget) { 8877 if (V == 0) 8878 return true; 8879 8880 if (!VT.isSimple()) 8881 return false; 8882 8883 if (Subtarget->isThumb1Only()) 8884 return isLegalT1AddressImmediate(V, VT); 8885 else if (Subtarget->isThumb2()) 8886 return isLegalT2AddressImmediate(V, VT, Subtarget); 8887 8888 // ARM mode. 8889 if (V < 0) 8890 V = - V; 8891 switch (VT.getSimpleVT().SimpleTy) { 8892 default: return false; 8893 case MVT::i1: 8894 case MVT::i8: 8895 case MVT::i32: 8896 // +- imm12 8897 return V == (V & ((1LL << 12) - 1)); 8898 case MVT::i16: 8899 // +- imm8 8900 return V == (V & ((1LL << 8) - 1)); 8901 case MVT::f32: 8902 case MVT::f64: 8903 if (!Subtarget->hasVFP2()) // FIXME: NEON? 8904 return false; 8905 if ((V & 3) != 0) 8906 return false; 8907 V >>= 2; 8908 return V == (V & ((1LL << 8) - 1)); 8909 } 8910 } 8911 8912 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 8913 EVT VT) const { 8914 int Scale = AM.Scale; 8915 if (Scale < 0) 8916 return false; 8917 8918 switch (VT.getSimpleVT().SimpleTy) { 8919 default: return false; 8920 case MVT::i1: 8921 case MVT::i8: 8922 case MVT::i16: 8923 case MVT::i32: 8924 if (Scale == 1) 8925 return true; 8926 // r + r << imm 8927 Scale = Scale & ~1; 8928 return Scale == 2 || Scale == 4 || Scale == 8; 8929 case MVT::i64: 8930 // r + r 8931 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 8932 return true; 8933 return false; 8934 case MVT::isVoid: 8935 // Note, we allow "void" uses (basically, uses that aren't loads or 8936 // stores), because arm allows folding a scale into many arithmetic 8937 // operations. This should be made more precise and revisited later. 8938 8939 // Allow r << imm, but the imm has to be a multiple of two. 8940 if (Scale & 1) return false; 8941 return isPowerOf2_32(Scale); 8942 } 8943 } 8944 8945 /// isLegalAddressingMode - Return true if the addressing mode represented 8946 /// by AM is legal for this target, for a load/store of the specified type. 8947 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, 8948 Type *Ty) const { 8949 EVT VT = getValueType(Ty, true); 8950 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 8951 return false; 8952 8953 // Can never fold addr of global into load/store. 8954 if (AM.BaseGV) 8955 return false; 8956 8957 switch (AM.Scale) { 8958 case 0: // no scale reg, must be "r+i" or "r", or "i". 8959 break; 8960 case 1: 8961 if (Subtarget->isThumb1Only()) 8962 return false; 8963 // FALL THROUGH. 8964 default: 8965 // ARM doesn't support any R+R*scale+imm addr modes. 8966 if (AM.BaseOffs) 8967 return false; 8968 8969 if (!VT.isSimple()) 8970 return false; 8971 8972 if (Subtarget->isThumb2()) 8973 return isLegalT2ScaledAddressingMode(AM, VT); 8974 8975 int Scale = AM.Scale; 8976 switch (VT.getSimpleVT().SimpleTy) { 8977 default: return false; 8978 case MVT::i1: 8979 case MVT::i8: 8980 case MVT::i32: 8981 if (Scale < 0) Scale = -Scale; 8982 if (Scale == 1) 8983 return true; 8984 // r + r << imm 8985 return isPowerOf2_32(Scale & ~1); 8986 case MVT::i16: 8987 case MVT::i64: 8988 // r + r 8989 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 8990 return true; 8991 return false; 8992 8993 case MVT::isVoid: 8994 // Note, we allow "void" uses (basically, uses that aren't loads or 8995 // stores), because arm allows folding a scale into many arithmetic 8996 // operations. This should be made more precise and revisited later. 8997 8998 // Allow r << imm, but the imm has to be a multiple of two. 8999 if (Scale & 1) return false; 9000 return isPowerOf2_32(Scale); 9001 } 9002 } 9003 return true; 9004 } 9005 9006 /// isLegalICmpImmediate - Return true if the specified immediate is legal 9007 /// icmp immediate, that is the target has icmp instructions which can compare 9008 /// a register against the immediate without having to materialize the 9009 /// immediate into a register. 9010 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 9011 // Thumb2 and ARM modes can use cmn for negative immediates. 9012 if (!Subtarget->isThumb()) 9013 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1; 9014 if (Subtarget->isThumb2()) 9015 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1; 9016 // Thumb1 doesn't have cmn, and only 8-bit immediates. 9017 return Imm >= 0 && Imm <= 255; 9018 } 9019 9020 /// isLegalAddImmediate - Return true if the specified immediate is legal 9021 /// add immediate, that is the target has add instructions which can add 9022 /// a register with the immediate without having to materialize the 9023 /// immediate into a register. 9024 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 9025 return ARM_AM::getSOImmVal(Imm) != -1; 9026 } 9027 9028 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 9029 bool isSEXTLoad, SDValue &Base, 9030 SDValue &Offset, bool &isInc, 9031 SelectionDAG &DAG) { 9032 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 9033 return false; 9034 9035 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 9036 // AddressingMode 3 9037 Base = Ptr->getOperand(0); 9038 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 9039 int RHSC = (int)RHS->getZExtValue(); 9040 if (RHSC < 0 && RHSC > -256) { 9041 assert(Ptr->getOpcode() == ISD::ADD); 9042 isInc = false; 9043 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 9044 return true; 9045 } 9046 } 9047 isInc = (Ptr->getOpcode() == ISD::ADD); 9048 Offset = Ptr->getOperand(1); 9049 return true; 9050 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 9051 // AddressingMode 2 9052 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 9053 int RHSC = (int)RHS->getZExtValue(); 9054 if (RHSC < 0 && RHSC > -0x1000) { 9055 assert(Ptr->getOpcode() == ISD::ADD); 9056 isInc = false; 9057 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 9058 Base = Ptr->getOperand(0); 9059 return true; 9060 } 9061 } 9062 9063 if (Ptr->getOpcode() == ISD::ADD) { 9064 isInc = true; 9065 ARM_AM::ShiftOpc ShOpcVal= 9066 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 9067 if (ShOpcVal != ARM_AM::no_shift) { 9068 Base = Ptr->getOperand(1); 9069 Offset = Ptr->getOperand(0); 9070 } else { 9071 Base = Ptr->getOperand(0); 9072 Offset = Ptr->getOperand(1); 9073 } 9074 return true; 9075 } 9076 9077 isInc = (Ptr->getOpcode() == ISD::ADD); 9078 Base = Ptr->getOperand(0); 9079 Offset = Ptr->getOperand(1); 9080 return true; 9081 } 9082 9083 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 9084 return false; 9085 } 9086 9087 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 9088 bool isSEXTLoad, SDValue &Base, 9089 SDValue &Offset, bool &isInc, 9090 SelectionDAG &DAG) { 9091 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 9092 return false; 9093 9094 Base = Ptr->getOperand(0); 9095 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 9096 int RHSC = (int)RHS->getZExtValue(); 9097 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 9098 assert(Ptr->getOpcode() == ISD::ADD); 9099 isInc = false; 9100 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 9101 return true; 9102 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 9103 isInc = Ptr->getOpcode() == ISD::ADD; 9104 Offset = DAG.getConstant(RHSC, RHS->getValueType(0)); 9105 return true; 9106 } 9107 } 9108 9109 return false; 9110 } 9111 9112 /// getPreIndexedAddressParts - returns true by value, base pointer and 9113 /// offset pointer and addressing mode by reference if the node's address 9114 /// can be legally represented as pre-indexed load / store address. 9115 bool 9116 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 9117 SDValue &Offset, 9118 ISD::MemIndexedMode &AM, 9119 SelectionDAG &DAG) const { 9120 if (Subtarget->isThumb1Only()) 9121 return false; 9122 9123 EVT VT; 9124 SDValue Ptr; 9125 bool isSEXTLoad = false; 9126 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 9127 Ptr = LD->getBasePtr(); 9128 VT = LD->getMemoryVT(); 9129 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 9130 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 9131 Ptr = ST->getBasePtr(); 9132 VT = ST->getMemoryVT(); 9133 } else 9134 return false; 9135 9136 bool isInc; 9137 bool isLegal = false; 9138 if (Subtarget->isThumb2()) 9139 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 9140 Offset, isInc, DAG); 9141 else 9142 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 9143 Offset, isInc, DAG); 9144 if (!isLegal) 9145 return false; 9146 9147 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 9148 return true; 9149 } 9150 9151 /// getPostIndexedAddressParts - returns true by value, base pointer and 9152 /// offset pointer and addressing mode by reference if this node can be 9153 /// combined with a load / store to form a post-indexed load / store. 9154 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 9155 SDValue &Base, 9156 SDValue &Offset, 9157 ISD::MemIndexedMode &AM, 9158 SelectionDAG &DAG) const { 9159 if (Subtarget->isThumb1Only()) 9160 return false; 9161 9162 EVT VT; 9163 SDValue Ptr; 9164 bool isSEXTLoad = false; 9165 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 9166 VT = LD->getMemoryVT(); 9167 Ptr = LD->getBasePtr(); 9168 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 9169 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 9170 VT = ST->getMemoryVT(); 9171 Ptr = ST->getBasePtr(); 9172 } else 9173 return false; 9174 9175 bool isInc; 9176 bool isLegal = false; 9177 if (Subtarget->isThumb2()) 9178 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 9179 isInc, DAG); 9180 else 9181 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 9182 isInc, DAG); 9183 if (!isLegal) 9184 return false; 9185 9186 if (Ptr != Base) { 9187 // Swap base ptr and offset to catch more post-index load / store when 9188 // it's legal. In Thumb2 mode, offset must be an immediate. 9189 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 9190 !Subtarget->isThumb2()) 9191 std::swap(Base, Offset); 9192 9193 // Post-indexed load / store update the base pointer. 9194 if (Ptr != Base) 9195 return false; 9196 } 9197 9198 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 9199 return true; 9200 } 9201 9202 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, 9203 APInt &KnownZero, 9204 APInt &KnownOne, 9205 const SelectionDAG &DAG, 9206 unsigned Depth) const { 9207 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); 9208 switch (Op.getOpcode()) { 9209 default: break; 9210 case ARMISD::CMOV: { 9211 // Bits are known zero/one if known on the LHS and RHS. 9212 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 9213 if (KnownZero == 0 && KnownOne == 0) return; 9214 9215 APInt KnownZeroRHS, KnownOneRHS; 9216 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1); 9217 KnownZero &= KnownZeroRHS; 9218 KnownOne &= KnownOneRHS; 9219 return; 9220 } 9221 } 9222 } 9223 9224 //===----------------------------------------------------------------------===// 9225 // ARM Inline Assembly Support 9226 //===----------------------------------------------------------------------===// 9227 9228 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 9229 // Looking for "rev" which is V6+. 9230 if (!Subtarget->hasV6Ops()) 9231 return false; 9232 9233 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 9234 std::string AsmStr = IA->getAsmString(); 9235 SmallVector<StringRef, 4> AsmPieces; 9236 SplitString(AsmStr, AsmPieces, ";\n"); 9237 9238 switch (AsmPieces.size()) { 9239 default: return false; 9240 case 1: 9241 AsmStr = AsmPieces[0]; 9242 AsmPieces.clear(); 9243 SplitString(AsmStr, AsmPieces, " \t,"); 9244 9245 // rev $0, $1 9246 if (AsmPieces.size() == 3 && 9247 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 9248 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 9249 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 9250 if (Ty && Ty->getBitWidth() == 32) 9251 return IntrinsicLowering::LowerToByteSwap(CI); 9252 } 9253 break; 9254 } 9255 9256 return false; 9257 } 9258 9259 /// getConstraintType - Given a constraint letter, return the type of 9260 /// constraint it is for this target. 9261 ARMTargetLowering::ConstraintType 9262 ARMTargetLowering::getConstraintType(const std::string &Constraint) const { 9263 if (Constraint.size() == 1) { 9264 switch (Constraint[0]) { 9265 default: break; 9266 case 'l': return C_RegisterClass; 9267 case 'w': return C_RegisterClass; 9268 case 'h': return C_RegisterClass; 9269 case 'x': return C_RegisterClass; 9270 case 't': return C_RegisterClass; 9271 case 'j': return C_Other; // Constant for movw. 9272 // An address with a single base register. Due to the way we 9273 // currently handle addresses it is the same as an 'r' memory constraint. 9274 case 'Q': return C_Memory; 9275 } 9276 } else if (Constraint.size() == 2) { 9277 switch (Constraint[0]) { 9278 default: break; 9279 // All 'U+' constraints are addresses. 9280 case 'U': return C_Memory; 9281 } 9282 } 9283 return TargetLowering::getConstraintType(Constraint); 9284 } 9285 9286 /// Examine constraint type and operand type and determine a weight value. 9287 /// This object must already have been set up with the operand type 9288 /// and the current alternative constraint selected. 9289 TargetLowering::ConstraintWeight 9290 ARMTargetLowering::getSingleConstraintMatchWeight( 9291 AsmOperandInfo &info, const char *constraint) const { 9292 ConstraintWeight weight = CW_Invalid; 9293 Value *CallOperandVal = info.CallOperandVal; 9294 // If we don't have a value, we can't do a match, 9295 // but allow it at the lowest weight. 9296 if (CallOperandVal == NULL) 9297 return CW_Default; 9298 Type *type = CallOperandVal->getType(); 9299 // Look at the constraint type. 9300 switch (*constraint) { 9301 default: 9302 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 9303 break; 9304 case 'l': 9305 if (type->isIntegerTy()) { 9306 if (Subtarget->isThumb()) 9307 weight = CW_SpecificReg; 9308 else 9309 weight = CW_Register; 9310 } 9311 break; 9312 case 'w': 9313 if (type->isFloatingPointTy()) 9314 weight = CW_Register; 9315 break; 9316 } 9317 return weight; 9318 } 9319 9320 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair; 9321 RCPair 9322 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 9323 EVT VT) const { 9324 if (Constraint.size() == 1) { 9325 // GCC ARM Constraint Letters 9326 switch (Constraint[0]) { 9327 case 'l': // Low regs or general regs. 9328 if (Subtarget->isThumb()) 9329 return RCPair(0U, &ARM::tGPRRegClass); 9330 return RCPair(0U, &ARM::GPRRegClass); 9331 case 'h': // High regs or no regs. 9332 if (Subtarget->isThumb()) 9333 return RCPair(0U, &ARM::hGPRRegClass); 9334 break; 9335 case 'r': 9336 return RCPair(0U, &ARM::GPRRegClass); 9337 case 'w': 9338 if (VT == MVT::f32) 9339 return RCPair(0U, &ARM::SPRRegClass); 9340 if (VT.getSizeInBits() == 64) 9341 return RCPair(0U, &ARM::DPRRegClass); 9342 if (VT.getSizeInBits() == 128) 9343 return RCPair(0U, &ARM::QPRRegClass); 9344 break; 9345 case 'x': 9346 if (VT == MVT::f32) 9347 return RCPair(0U, &ARM::SPR_8RegClass); 9348 if (VT.getSizeInBits() == 64) 9349 return RCPair(0U, &ARM::DPR_8RegClass); 9350 if (VT.getSizeInBits() == 128) 9351 return RCPair(0U, &ARM::QPR_8RegClass); 9352 break; 9353 case 't': 9354 if (VT == MVT::f32) 9355 return RCPair(0U, &ARM::SPRRegClass); 9356 break; 9357 } 9358 } 9359 if (StringRef("{cc}").equals_lower(Constraint)) 9360 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 9361 9362 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 9363 } 9364 9365 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 9366 /// vector. If it is invalid, don't add anything to Ops. 9367 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 9368 std::string &Constraint, 9369 std::vector<SDValue>&Ops, 9370 SelectionDAG &DAG) const { 9371 SDValue Result(0, 0); 9372 9373 // Currently only support length 1 constraints. 9374 if (Constraint.length() != 1) return; 9375 9376 char ConstraintLetter = Constraint[0]; 9377 switch (ConstraintLetter) { 9378 default: break; 9379 case 'j': 9380 case 'I': case 'J': case 'K': case 'L': 9381 case 'M': case 'N': case 'O': 9382 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 9383 if (!C) 9384 return; 9385 9386 int64_t CVal64 = C->getSExtValue(); 9387 int CVal = (int) CVal64; 9388 // None of these constraints allow values larger than 32 bits. Check 9389 // that the value fits in an int. 9390 if (CVal != CVal64) 9391 return; 9392 9393 switch (ConstraintLetter) { 9394 case 'j': 9395 // Constant suitable for movw, must be between 0 and 9396 // 65535. 9397 if (Subtarget->hasV6T2Ops()) 9398 if (CVal >= 0 && CVal <= 65535) 9399 break; 9400 return; 9401 case 'I': 9402 if (Subtarget->isThumb1Only()) { 9403 // This must be a constant between 0 and 255, for ADD 9404 // immediates. 9405 if (CVal >= 0 && CVal <= 255) 9406 break; 9407 } else if (Subtarget->isThumb2()) { 9408 // A constant that can be used as an immediate value in a 9409 // data-processing instruction. 9410 if (ARM_AM::getT2SOImmVal(CVal) != -1) 9411 break; 9412 } else { 9413 // A constant that can be used as an immediate value in a 9414 // data-processing instruction. 9415 if (ARM_AM::getSOImmVal(CVal) != -1) 9416 break; 9417 } 9418 return; 9419 9420 case 'J': 9421 if (Subtarget->isThumb()) { // FIXME thumb2 9422 // This must be a constant between -255 and -1, for negated ADD 9423 // immediates. This can be used in GCC with an "n" modifier that 9424 // prints the negated value, for use with SUB instructions. It is 9425 // not useful otherwise but is implemented for compatibility. 9426 if (CVal >= -255 && CVal <= -1) 9427 break; 9428 } else { 9429 // This must be a constant between -4095 and 4095. It is not clear 9430 // what this constraint is intended for. Implemented for 9431 // compatibility with GCC. 9432 if (CVal >= -4095 && CVal <= 4095) 9433 break; 9434 } 9435 return; 9436 9437 case 'K': 9438 if (Subtarget->isThumb1Only()) { 9439 // A 32-bit value where only one byte has a nonzero value. Exclude 9440 // zero to match GCC. This constraint is used by GCC internally for 9441 // constants that can be loaded with a move/shift combination. 9442 // It is not useful otherwise but is implemented for compatibility. 9443 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 9444 break; 9445 } else if (Subtarget->isThumb2()) { 9446 // A constant whose bitwise inverse can be used as an immediate 9447 // value in a data-processing instruction. This can be used in GCC 9448 // with a "B" modifier that prints the inverted value, for use with 9449 // BIC and MVN instructions. It is not useful otherwise but is 9450 // implemented for compatibility. 9451 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 9452 break; 9453 } else { 9454 // A constant whose bitwise inverse can be used as an immediate 9455 // value in a data-processing instruction. This can be used in GCC 9456 // with a "B" modifier that prints the inverted value, for use with 9457 // BIC and MVN instructions. It is not useful otherwise but is 9458 // implemented for compatibility. 9459 if (ARM_AM::getSOImmVal(~CVal) != -1) 9460 break; 9461 } 9462 return; 9463 9464 case 'L': 9465 if (Subtarget->isThumb1Only()) { 9466 // This must be a constant between -7 and 7, 9467 // for 3-operand ADD/SUB immediate instructions. 9468 if (CVal >= -7 && CVal < 7) 9469 break; 9470 } else if (Subtarget->isThumb2()) { 9471 // A constant whose negation can be used as an immediate value in a 9472 // data-processing instruction. This can be used in GCC with an "n" 9473 // modifier that prints the negated value, for use with SUB 9474 // instructions. It is not useful otherwise but is implemented for 9475 // compatibility. 9476 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 9477 break; 9478 } else { 9479 // A constant whose negation can be used as an immediate value in a 9480 // data-processing instruction. This can be used in GCC with an "n" 9481 // modifier that prints the negated value, for use with SUB 9482 // instructions. It is not useful otherwise but is implemented for 9483 // compatibility. 9484 if (ARM_AM::getSOImmVal(-CVal) != -1) 9485 break; 9486 } 9487 return; 9488 9489 case 'M': 9490 if (Subtarget->isThumb()) { // FIXME thumb2 9491 // This must be a multiple of 4 between 0 and 1020, for 9492 // ADD sp + immediate. 9493 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 9494 break; 9495 } else { 9496 // A power of two or a constant between 0 and 32. This is used in 9497 // GCC for the shift amount on shifted register operands, but it is 9498 // useful in general for any shift amounts. 9499 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 9500 break; 9501 } 9502 return; 9503 9504 case 'N': 9505 if (Subtarget->isThumb()) { // FIXME thumb2 9506 // This must be a constant between 0 and 31, for shift amounts. 9507 if (CVal >= 0 && CVal <= 31) 9508 break; 9509 } 9510 return; 9511 9512 case 'O': 9513 if (Subtarget->isThumb()) { // FIXME thumb2 9514 // This must be a multiple of 4 between -508 and 508, for 9515 // ADD/SUB sp = sp + immediate. 9516 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 9517 break; 9518 } 9519 return; 9520 } 9521 Result = DAG.getTargetConstant(CVal, Op.getValueType()); 9522 break; 9523 } 9524 9525 if (Result.getNode()) { 9526 Ops.push_back(Result); 9527 return; 9528 } 9529 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 9530 } 9531 9532 bool 9533 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 9534 // The ARM target isn't yet aware of offsets. 9535 return false; 9536 } 9537 9538 bool ARM::isBitFieldInvertedMask(unsigned v) { 9539 if (v == 0xffffffff) 9540 return 0; 9541 // there can be 1's on either or both "outsides", all the "inside" 9542 // bits must be 0's 9543 unsigned int lsb = 0, msb = 31; 9544 while (v & (1 << msb)) --msb; 9545 while (v & (1 << lsb)) ++lsb; 9546 for (unsigned int i = lsb; i <= msb; ++i) { 9547 if (v & (1 << i)) 9548 return 0; 9549 } 9550 return 1; 9551 } 9552 9553 /// isFPImmLegal - Returns true if the target can instruction select the 9554 /// specified FP immediate natively. If false, the legalizer will 9555 /// materialize the FP immediate as a load from a constant pool. 9556 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 9557 if (!Subtarget->hasVFP3()) 9558 return false; 9559 if (VT == MVT::f32) 9560 return ARM_AM::getFP32Imm(Imm) != -1; 9561 if (VT == MVT::f64) 9562 return ARM_AM::getFP64Imm(Imm) != -1; 9563 return false; 9564 } 9565 9566 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 9567 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 9568 /// specified in the intrinsic calls. 9569 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 9570 const CallInst &I, 9571 unsigned Intrinsic) const { 9572 switch (Intrinsic) { 9573 case Intrinsic::arm_neon_vld1: 9574 case Intrinsic::arm_neon_vld2: 9575 case Intrinsic::arm_neon_vld3: 9576 case Intrinsic::arm_neon_vld4: 9577 case Intrinsic::arm_neon_vld2lane: 9578 case Intrinsic::arm_neon_vld3lane: 9579 case Intrinsic::arm_neon_vld4lane: { 9580 Info.opc = ISD::INTRINSIC_W_CHAIN; 9581 // Conservatively set memVT to the entire set of vectors loaded. 9582 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8; 9583 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 9584 Info.ptrVal = I.getArgOperand(0); 9585 Info.offset = 0; 9586 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 9587 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 9588 Info.vol = false; // volatile loads with NEON intrinsics not supported 9589 Info.readMem = true; 9590 Info.writeMem = false; 9591 return true; 9592 } 9593 case Intrinsic::arm_neon_vst1: 9594 case Intrinsic::arm_neon_vst2: 9595 case Intrinsic::arm_neon_vst3: 9596 case Intrinsic::arm_neon_vst4: 9597 case Intrinsic::arm_neon_vst2lane: 9598 case Intrinsic::arm_neon_vst3lane: 9599 case Intrinsic::arm_neon_vst4lane: { 9600 Info.opc = ISD::INTRINSIC_VOID; 9601 // Conservatively set memVT to the entire set of vectors stored. 9602 unsigned NumElts = 0; 9603 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 9604 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 9605 if (!ArgTy->isVectorTy()) 9606 break; 9607 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8; 9608 } 9609 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 9610 Info.ptrVal = I.getArgOperand(0); 9611 Info.offset = 0; 9612 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 9613 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 9614 Info.vol = false; // volatile stores with NEON intrinsics not supported 9615 Info.readMem = false; 9616 Info.writeMem = true; 9617 return true; 9618 } 9619 case Intrinsic::arm_strexd: { 9620 Info.opc = ISD::INTRINSIC_W_CHAIN; 9621 Info.memVT = MVT::i64; 9622 Info.ptrVal = I.getArgOperand(2); 9623 Info.offset = 0; 9624 Info.align = 8; 9625 Info.vol = true; 9626 Info.readMem = false; 9627 Info.writeMem = true; 9628 return true; 9629 } 9630 case Intrinsic::arm_ldrexd: { 9631 Info.opc = ISD::INTRINSIC_W_CHAIN; 9632 Info.memVT = MVT::i64; 9633 Info.ptrVal = I.getArgOperand(0); 9634 Info.offset = 0; 9635 Info.align = 8; 9636 Info.vol = true; 9637 Info.readMem = true; 9638 Info.writeMem = false; 9639 return true; 9640 } 9641 default: 9642 break; 9643 } 9644 9645 return false; 9646 } 9647