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 "ARM.h" 17 #include "ARMCallingConv.h" 18 #include "ARMConstantPoolValue.h" 19 #include "ARMISelLowering.h" 20 #include "ARMMachineFunctionInfo.h" 21 #include "ARMPerfectShuffle.h" 22 #include "ARMRegisterInfo.h" 23 #include "ARMSubtarget.h" 24 #include "ARMTargetMachine.h" 25 #include "ARMTargetObjectFile.h" 26 #include "MCTargetDesc/ARMAddressingModes.h" 27 #include "llvm/CallingConv.h" 28 #include "llvm/Constants.h" 29 #include "llvm/Function.h" 30 #include "llvm/GlobalValue.h" 31 #include "llvm/Instruction.h" 32 #include "llvm/Instructions.h" 33 #include "llvm/Intrinsics.h" 34 #include "llvm/Type.h" 35 #include "llvm/CodeGen/CallingConvLower.h" 36 #include "llvm/CodeGen/IntrinsicLowering.h" 37 #include "llvm/CodeGen/MachineBasicBlock.h" 38 #include "llvm/CodeGen/MachineFrameInfo.h" 39 #include "llvm/CodeGen/MachineFunction.h" 40 #include "llvm/CodeGen/MachineInstrBuilder.h" 41 #include "llvm/CodeGen/MachineModuleInfo.h" 42 #include "llvm/CodeGen/MachineRegisterInfo.h" 43 #include "llvm/CodeGen/SelectionDAG.h" 44 #include "llvm/MC/MCSectionMachO.h" 45 #include "llvm/Target/TargetOptions.h" 46 #include "llvm/ADT/VectorExtras.h" 47 #include "llvm/ADT/StringExtras.h" 48 #include "llvm/ADT/Statistic.h" 49 #include "llvm/Support/CommandLine.h" 50 #include "llvm/Support/ErrorHandling.h" 51 #include "llvm/Support/MathExtras.h" 52 #include "llvm/Support/raw_ostream.h" 53 #include <sstream> 54 using namespace llvm; 55 56 STATISTIC(NumTailCalls, "Number of tail calls"); 57 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 58 59 // This option should go away when tail calls fully work. 60 static cl::opt<bool> 61 EnableARMTailCalls("arm-tail-calls", cl::Hidden, 62 cl::desc("Generate tail calls (TEMPORARY OPTION)."), 63 cl::init(false)); 64 65 cl::opt<bool> 66 EnableARMLongCalls("arm-long-calls", cl::Hidden, 67 cl::desc("Generate calls via indirect call instructions"), 68 cl::init(false)); 69 70 static cl::opt<bool> 71 ARMInterworking("arm-interworking", cl::Hidden, 72 cl::desc("Enable / disable ARM interworking (for debugging only)"), 73 cl::init(true)); 74 75 namespace { 76 class ARMCCState : public CCState { 77 public: 78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, 79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs, 80 LLVMContext &C, ParmContext PC) 81 : CCState(CC, isVarArg, MF, TM, locs, C) { 82 assert(((PC == Call) || (PC == Prologue)) && 83 "ARMCCState users must specify whether their context is call" 84 "or prologue generation."); 85 CallOrPrologue = PC; 86 } 87 }; 88 } 89 90 // The APCS parameter registers. 91 static const unsigned GPRArgRegs[] = { 92 ARM::R0, ARM::R1, ARM::R2, ARM::R3 93 }; 94 95 void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT, 96 EVT PromotedBitwiseVT) { 97 if (VT != PromotedLdStVT) { 98 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote); 99 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(), 100 PromotedLdStVT.getSimpleVT()); 101 102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote); 103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(), 104 PromotedLdStVT.getSimpleVT()); 105 } 106 107 EVT ElemTy = VT.getVectorElementType(); 108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64) 109 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom); 110 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom); 111 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom); 112 if (ElemTy == MVT::i32) { 113 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom); 114 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom); 115 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom); 116 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom); 117 } else { 118 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand); 119 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand); 120 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand); 121 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand); 122 } 123 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom); 124 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom); 125 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal); 126 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal); 127 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand); 128 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand); 129 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand); 130 if (VT.isInteger()) { 131 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom); 132 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom); 133 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom); 134 } 135 136 // Promote all bit-wise operations. 137 if (VT.isInteger() && VT != PromotedBitwiseVT) { 138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote); 139 AddPromotedToType (ISD::AND, VT.getSimpleVT(), 140 PromotedBitwiseVT.getSimpleVT()); 141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote); 142 AddPromotedToType (ISD::OR, VT.getSimpleVT(), 143 PromotedBitwiseVT.getSimpleVT()); 144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote); 145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(), 146 PromotedBitwiseVT.getSimpleVT()); 147 } 148 149 // Neon does not support vector divide/remainder operations. 150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand); 151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand); 152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand); 153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand); 154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand); 155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand); 156 } 157 158 void ARMTargetLowering::addDRTypeForNEON(EVT VT) { 159 addRegisterClass(VT, ARM::DPRRegisterClass); 160 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 161 } 162 163 void ARMTargetLowering::addQRTypeForNEON(EVT VT) { 164 addRegisterClass(VT, ARM::QPRRegisterClass); 165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 166 } 167 168 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) { 169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin()) 170 return new TargetLoweringObjectFileMachO(); 171 172 return new ARMElfTargetObjectFile(); 173 } 174 175 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM) 176 : TargetLowering(TM, createTLOF(TM)) { 177 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 178 RegInfo = TM.getRegisterInfo(); 179 Itins = TM.getInstrItineraryData(); 180 181 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 182 183 if (Subtarget->isTargetDarwin()) { 184 // Uses VFP for Thumb libfuncs if available. 185 if (Subtarget->isThumb() && Subtarget->hasVFP2()) { 186 // Single-precision floating-point arithmetic. 187 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp"); 188 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp"); 189 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp"); 190 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp"); 191 192 // Double-precision floating-point arithmetic. 193 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp"); 194 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp"); 195 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp"); 196 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp"); 197 198 // Single-precision comparisons. 199 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp"); 200 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp"); 201 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp"); 202 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp"); 203 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp"); 204 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp"); 205 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp"); 206 setLibcallName(RTLIB::O_F32, "__unordsf2vfp"); 207 208 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 209 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); 210 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 211 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 212 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 213 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 214 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 215 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 216 217 // Double-precision comparisons. 218 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp"); 219 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp"); 220 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp"); 221 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp"); 222 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp"); 223 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp"); 224 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp"); 225 setLibcallName(RTLIB::O_F64, "__unorddf2vfp"); 226 227 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 228 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE); 229 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 230 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 231 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 232 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 233 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 234 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 235 236 // Floating-point to integer conversions. 237 // i64 conversions are done via library routines even when generating VFP 238 // instructions, so use the same ones. 239 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp"); 240 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp"); 241 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp"); 242 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp"); 243 244 // Conversions between floating types. 245 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp"); 246 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp"); 247 248 // Integer to floating-point conversions. 249 // i64 conversions are done via library routines even when generating VFP 250 // instructions, so use the same ones. 251 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 252 // e.g., __floatunsidf vs. __floatunssidfvfp. 253 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp"); 254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp"); 255 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp"); 256 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp"); 257 } 258 } 259 260 // These libcalls are not available in 32-bit. 261 setLibcallName(RTLIB::SHL_I128, 0); 262 setLibcallName(RTLIB::SRL_I128, 0); 263 setLibcallName(RTLIB::SRA_I128, 0); 264 265 if (Subtarget->isAAPCS_ABI()) { 266 // Double-precision floating-point arithmetic helper functions 267 // RTABI chapter 4.1.2, Table 2 268 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd"); 269 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv"); 270 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul"); 271 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub"); 272 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS); 273 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS); 274 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS); 275 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS); 276 277 // Double-precision floating-point comparison helper functions 278 // RTABI chapter 4.1.2, Table 3 279 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq"); 280 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 281 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq"); 282 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ); 283 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt"); 284 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 285 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple"); 286 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 287 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge"); 288 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 289 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt"); 290 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 291 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun"); 292 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 293 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun"); 294 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 295 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS); 296 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS); 297 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS); 298 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS); 299 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS); 300 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS); 301 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS); 302 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS); 303 304 // Single-precision floating-point arithmetic helper functions 305 // RTABI chapter 4.1.2, Table 4 306 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd"); 307 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv"); 308 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul"); 309 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub"); 310 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS); 311 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS); 312 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS); 313 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS); 314 315 // Single-precision floating-point comparison helper functions 316 // RTABI chapter 4.1.2, Table 5 317 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq"); 318 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 319 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq"); 320 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ); 321 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt"); 322 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 323 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple"); 324 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 325 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge"); 326 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 327 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt"); 328 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 329 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun"); 330 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 331 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun"); 332 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 333 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS); 334 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS); 335 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS); 336 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS); 337 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS); 338 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS); 339 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS); 340 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS); 341 342 // Floating-point to integer conversions. 343 // RTABI chapter 4.1.2, Table 6 344 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz"); 345 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz"); 346 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz"); 347 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz"); 348 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz"); 349 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz"); 350 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz"); 351 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz"); 352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS); 353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS); 354 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS); 355 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS); 356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS); 357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS); 358 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS); 359 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS); 360 361 // Conversions between floating types. 362 // RTABI chapter 4.1.2, Table 7 363 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f"); 364 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d"); 365 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS); 366 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS); 367 368 // Integer to floating-point conversions. 369 // RTABI chapter 4.1.2, Table 8 370 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d"); 371 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d"); 372 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d"); 373 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d"); 374 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f"); 375 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f"); 376 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f"); 377 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f"); 378 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS); 379 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS); 380 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS); 381 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS); 382 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS); 383 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS); 384 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS); 385 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS); 386 387 // Long long helper functions 388 // RTABI chapter 4.2, Table 9 389 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul"); 390 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod"); 391 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod"); 392 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl"); 393 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr"); 394 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr"); 395 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS); 396 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS); 397 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS); 398 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS); 399 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS); 400 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS); 401 402 // Integer division functions 403 // RTABI chapter 4.3.1 404 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv"); 405 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv"); 406 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv"); 407 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv"); 408 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv"); 409 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv"); 410 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS); 411 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS); 412 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS); 413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS); 414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS); 415 setLibcallCallingConv(RTLIB::UDIV_I32, 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 } 423 424 // Use divmod compiler-rt calls for iOS 5.0 and later. 425 if (Subtarget->getTargetTriple().getOS() == Triple::IOS && 426 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) { 427 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 428 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 429 } 430 431 if (Subtarget->isThumb1Only()) 432 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass); 433 else 434 addRegisterClass(MVT::i32, ARM::GPRRegisterClass); 435 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 436 !Subtarget->isThumb1Only()) { 437 addRegisterClass(MVT::f32, ARM::SPRRegisterClass); 438 if (!Subtarget->isFPOnlySP()) 439 addRegisterClass(MVT::f64, ARM::DPRRegisterClass); 440 441 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 442 } 443 444 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 445 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { 446 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 447 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT) 448 setTruncStoreAction((MVT::SimpleValueType)VT, 449 (MVT::SimpleValueType)InnerVT, Expand); 450 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand); 451 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand); 452 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand); 453 } 454 455 if (Subtarget->hasNEON()) { 456 addDRTypeForNEON(MVT::v2f32); 457 addDRTypeForNEON(MVT::v8i8); 458 addDRTypeForNEON(MVT::v4i16); 459 addDRTypeForNEON(MVT::v2i32); 460 addDRTypeForNEON(MVT::v1i64); 461 462 addQRTypeForNEON(MVT::v4f32); 463 addQRTypeForNEON(MVT::v2f64); 464 addQRTypeForNEON(MVT::v16i8); 465 addQRTypeForNEON(MVT::v8i16); 466 addQRTypeForNEON(MVT::v4i32); 467 addQRTypeForNEON(MVT::v2i64); 468 469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 470 // neither Neon nor VFP support any arithmetic operations on it. 471 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 472 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 473 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 474 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 475 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 476 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 477 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 478 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 479 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 480 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 481 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 482 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 483 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand); 484 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 485 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 486 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 487 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 488 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 489 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 490 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 491 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 492 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 493 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 494 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 495 496 // Neon does not support some operations on v1i64 and v2i64 types. 497 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 498 // Custom handling for some quad-vector types to detect VMULL. 499 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 500 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 501 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 502 // Custom handling for some vector types to avoid expensive expansions 503 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 504 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 505 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 506 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 507 setOperationAction(ISD::SETCC, MVT::v1i64, Expand); 508 setOperationAction(ISD::SETCC, MVT::v2i64, Expand); 509 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 510 // a destination type that is wider than the source. 511 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 512 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 513 514 setTargetDAGCombine(ISD::INTRINSIC_VOID); 515 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 516 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 517 setTargetDAGCombine(ISD::SHL); 518 setTargetDAGCombine(ISD::SRL); 519 setTargetDAGCombine(ISD::SRA); 520 setTargetDAGCombine(ISD::SIGN_EXTEND); 521 setTargetDAGCombine(ISD::ZERO_EXTEND); 522 setTargetDAGCombine(ISD::ANY_EXTEND); 523 setTargetDAGCombine(ISD::SELECT_CC); 524 setTargetDAGCombine(ISD::BUILD_VECTOR); 525 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 526 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 527 setTargetDAGCombine(ISD::STORE); 528 setTargetDAGCombine(ISD::FP_TO_SINT); 529 setTargetDAGCombine(ISD::FP_TO_UINT); 530 setTargetDAGCombine(ISD::FDIV); 531 532 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand); 533 } 534 535 computeRegisterProperties(); 536 537 // ARM does not have f32 extending load. 538 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 539 540 // ARM does not have i1 sign extending load. 541 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 542 543 // ARM supports all 4 flavors of integer indexed load / store. 544 if (!Subtarget->isThumb1Only()) { 545 for (unsigned im = (unsigned)ISD::PRE_INC; 546 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 547 setIndexedLoadAction(im, MVT::i1, Legal); 548 setIndexedLoadAction(im, MVT::i8, Legal); 549 setIndexedLoadAction(im, MVT::i16, Legal); 550 setIndexedLoadAction(im, MVT::i32, Legal); 551 setIndexedStoreAction(im, MVT::i1, Legal); 552 setIndexedStoreAction(im, MVT::i8, Legal); 553 setIndexedStoreAction(im, MVT::i16, Legal); 554 setIndexedStoreAction(im, MVT::i32, Legal); 555 } 556 } 557 558 // i64 operation support. 559 setOperationAction(ISD::MUL, MVT::i64, Expand); 560 setOperationAction(ISD::MULHU, MVT::i32, Expand); 561 if (Subtarget->isThumb1Only()) { 562 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 563 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 564 } 565 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 566 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP())) 567 setOperationAction(ISD::MULHS, MVT::i32, Expand); 568 569 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 570 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 571 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 572 setOperationAction(ISD::SRL, MVT::i64, Custom); 573 setOperationAction(ISD::SRA, MVT::i64, Custom); 574 575 if (!Subtarget->isThumb1Only()) { 576 // FIXME: We should do this for Thumb1 as well. 577 setOperationAction(ISD::ADDC, MVT::i32, Custom); 578 setOperationAction(ISD::ADDE, MVT::i32, Custom); 579 setOperationAction(ISD::SUBC, MVT::i32, Custom); 580 setOperationAction(ISD::SUBE, MVT::i32, Custom); 581 } 582 583 // ARM does not have ROTL. 584 setOperationAction(ISD::ROTL, MVT::i32, Expand); 585 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 586 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 587 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 588 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 589 590 // Only ARMv6 has BSWAP. 591 if (!Subtarget->hasV6Ops()) 592 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 593 594 // These are expanded into libcalls. 595 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) { 596 // v7M has a hardware divider 597 setOperationAction(ISD::SDIV, MVT::i32, Expand); 598 setOperationAction(ISD::UDIV, MVT::i32, Expand); 599 } 600 setOperationAction(ISD::SREM, MVT::i32, Expand); 601 setOperationAction(ISD::UREM, MVT::i32, Expand); 602 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 603 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 604 605 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 606 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 607 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom); 608 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 609 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 610 611 setOperationAction(ISD::TRAP, MVT::Other, Legal); 612 613 // Use the default implementation. 614 setOperationAction(ISD::VASTART, MVT::Other, Custom); 615 setOperationAction(ISD::VAARG, MVT::Other, Expand); 616 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 617 setOperationAction(ISD::VAEND, MVT::Other, Expand); 618 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 619 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 620 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); 621 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); 622 setExceptionPointerRegister(ARM::R0); 623 setExceptionSelectorRegister(ARM::R1); 624 625 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 626 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 627 // the default expansion. 628 // FIXME: This should be checking for v6k, not just v6. 629 if (Subtarget->hasDataBarrier() || 630 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) { 631 // membarrier needs custom lowering; the rest are legal and handled 632 // normally. 633 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom); 634 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 635 // Custom lowering for 64-bit ops 636 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); 637 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); 638 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); 639 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); 640 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); 641 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); 642 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 643 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc. 644 setInsertFencesForAtomic(true); 645 } else { 646 // Set them all for expansion, which will force libcalls. 647 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand); 648 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand); 649 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 650 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 651 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 652 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 653 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 654 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 655 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 656 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 657 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 658 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 659 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 660 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 661 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 662 // Unordered/Monotonic case. 663 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 664 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 665 // Since the libcalls include locking, fold in the fences 666 setShouldFoldAtomicFences(true); 667 } 668 669 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 670 671 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 672 if (!Subtarget->hasV6Ops()) { 673 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 674 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 675 } 676 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 677 678 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 679 !Subtarget->isThumb1Only()) { 680 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 681 // iff target supports vfp2. 682 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 683 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 684 } 685 686 // We want to custom lower some of our intrinsics. 687 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 688 if (Subtarget->isTargetDarwin()) { 689 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 690 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 691 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 692 } 693 694 setOperationAction(ISD::SETCC, MVT::i32, Expand); 695 setOperationAction(ISD::SETCC, MVT::f32, Expand); 696 setOperationAction(ISD::SETCC, MVT::f64, Expand); 697 setOperationAction(ISD::SELECT, MVT::i32, Custom); 698 setOperationAction(ISD::SELECT, MVT::f32, Custom); 699 setOperationAction(ISD::SELECT, MVT::f64, Custom); 700 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 701 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 702 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 703 704 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 705 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 706 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 707 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 708 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 709 710 // We don't support sin/cos/fmod/copysign/pow 711 setOperationAction(ISD::FSIN, MVT::f64, Expand); 712 setOperationAction(ISD::FSIN, MVT::f32, Expand); 713 setOperationAction(ISD::FCOS, MVT::f32, Expand); 714 setOperationAction(ISD::FCOS, MVT::f64, Expand); 715 setOperationAction(ISD::FREM, MVT::f64, Expand); 716 setOperationAction(ISD::FREM, MVT::f32, Expand); 717 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 718 !Subtarget->isThumb1Only()) { 719 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 720 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 721 } 722 setOperationAction(ISD::FPOW, MVT::f64, Expand); 723 setOperationAction(ISD::FPOW, MVT::f32, Expand); 724 725 setOperationAction(ISD::FMA, MVT::f64, Expand); 726 setOperationAction(ISD::FMA, MVT::f32, Expand); 727 728 // Various VFP goodness 729 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) { 730 // int <-> fp are custom expanded into bit_convert + ARMISD ops. 731 if (Subtarget->hasVFP2()) { 732 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 733 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 734 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 735 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 736 } 737 // Special handling for half-precision FP. 738 if (!Subtarget->hasFP16()) { 739 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand); 740 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand); 741 } 742 } 743 744 // We have target-specific dag combine patterns for the following nodes: 745 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 746 setTargetDAGCombine(ISD::ADD); 747 setTargetDAGCombine(ISD::SUB); 748 setTargetDAGCombine(ISD::MUL); 749 750 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) 751 setTargetDAGCombine(ISD::OR); 752 if (Subtarget->hasNEON()) 753 setTargetDAGCombine(ISD::AND); 754 755 setStackPointerRegisterToSaveRestore(ARM::SP); 756 757 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() || 758 !Subtarget->hasVFP2()) 759 setSchedulingPreference(Sched::RegPressure); 760 else 761 setSchedulingPreference(Sched::Hybrid); 762 763 //// temporary - rewrite interface to use type 764 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1; 765 maxStoresPerMemset = 16; 766 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4; 767 768 // On ARM arguments smaller than 4 bytes are extended, so all arguments 769 // are at least 4 bytes aligned. 770 setMinStackArgumentAlignment(4); 771 772 benefitFromCodePlacementOpt = true; 773 774 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 775 } 776 777 // FIXME: It might make sense to define the representative register class as the 778 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 779 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 780 // SPR's representative would be DPR_VFP2. This should work well if register 781 // pressure tracking were modified such that a register use would increment the 782 // pressure of the register class's representative and all of it's super 783 // classes' representatives transitively. We have not implemented this because 784 // of the difficulty prior to coalescing of modeling operand register classes 785 // due to the common occurrence of cross class copies and subregister insertions 786 // and extractions. 787 std::pair<const TargetRegisterClass*, uint8_t> 788 ARMTargetLowering::findRepresentativeClass(EVT VT) const{ 789 const TargetRegisterClass *RRC = 0; 790 uint8_t Cost = 1; 791 switch (VT.getSimpleVT().SimpleTy) { 792 default: 793 return TargetLowering::findRepresentativeClass(VT); 794 // Use DPR as representative register class for all floating point 795 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 796 // the cost is 1 for both f32 and f64. 797 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 798 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 799 RRC = ARM::DPRRegisterClass; 800 // When NEON is used for SP, only half of the register file is available 801 // because operations that define both SP and DP results will be constrained 802 // to the VFP2 class (D0-D15). We currently model this constraint prior to 803 // coalescing by double-counting the SP regs. See the FIXME above. 804 if (Subtarget->useNEONForSinglePrecisionFP()) 805 Cost = 2; 806 break; 807 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 808 case MVT::v4f32: case MVT::v2f64: 809 RRC = ARM::DPRRegisterClass; 810 Cost = 2; 811 break; 812 case MVT::v4i64: 813 RRC = ARM::DPRRegisterClass; 814 Cost = 4; 815 break; 816 case MVT::v8i64: 817 RRC = ARM::DPRRegisterClass; 818 Cost = 8; 819 break; 820 } 821 return std::make_pair(RRC, Cost); 822 } 823 824 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 825 switch (Opcode) { 826 default: return 0; 827 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 828 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN"; 829 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 830 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 831 case ARMISD::CALL: return "ARMISD::CALL"; 832 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 833 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 834 case ARMISD::tCALL: return "ARMISD::tCALL"; 835 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 836 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 837 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 838 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 839 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 840 case ARMISD::CMP: return "ARMISD::CMP"; 841 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 842 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 843 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 844 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 845 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 846 case ARMISD::CMOV: return "ARMISD::CMOV"; 847 848 case ARMISD::RBIT: return "ARMISD::RBIT"; 849 850 case ARMISD::FTOSI: return "ARMISD::FTOSI"; 851 case ARMISD::FTOUI: return "ARMISD::FTOUI"; 852 case ARMISD::SITOF: return "ARMISD::SITOF"; 853 case ARMISD::UITOF: return "ARMISD::UITOF"; 854 855 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 856 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 857 case ARMISD::RRX: return "ARMISD::RRX"; 858 859 case ARMISD::ADDC: return "ARMISD::ADDC"; 860 case ARMISD::ADDE: return "ARMISD::ADDE"; 861 case ARMISD::SUBC: return "ARMISD::SUBC"; 862 case ARMISD::SUBE: return "ARMISD::SUBE"; 863 864 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 865 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 866 867 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 868 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP"; 869 870 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 871 872 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 873 874 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 875 876 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER"; 877 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 878 879 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 880 881 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 882 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 883 case ARMISD::VCGE: return "ARMISD::VCGE"; 884 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 885 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 886 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 887 case ARMISD::VCGT: return "ARMISD::VCGT"; 888 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 889 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 890 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 891 case ARMISD::VTST: return "ARMISD::VTST"; 892 893 case ARMISD::VSHL: return "ARMISD::VSHL"; 894 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 895 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 896 case ARMISD::VSHLLs: return "ARMISD::VSHLLs"; 897 case ARMISD::VSHLLu: return "ARMISD::VSHLLu"; 898 case ARMISD::VSHLLi: return "ARMISD::VSHLLi"; 899 case ARMISD::VSHRN: return "ARMISD::VSHRN"; 900 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 901 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 902 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 903 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 904 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 905 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 906 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 907 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 908 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 909 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 910 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 911 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 912 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 913 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 914 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 915 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 916 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 917 case ARMISD::VDUP: return "ARMISD::VDUP"; 918 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 919 case ARMISD::VEXT: return "ARMISD::VEXT"; 920 case ARMISD::VREV64: return "ARMISD::VREV64"; 921 case ARMISD::VREV32: return "ARMISD::VREV32"; 922 case ARMISD::VREV16: return "ARMISD::VREV16"; 923 case ARMISD::VZIP: return "ARMISD::VZIP"; 924 case ARMISD::VUZP: return "ARMISD::VUZP"; 925 case ARMISD::VTRN: return "ARMISD::VTRN"; 926 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 927 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 928 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 929 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 930 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 931 case ARMISD::FMAX: return "ARMISD::FMAX"; 932 case ARMISD::FMIN: return "ARMISD::FMIN"; 933 case ARMISD::BFI: return "ARMISD::BFI"; 934 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 935 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 936 case ARMISD::VBSL: return "ARMISD::VBSL"; 937 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 938 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 939 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 940 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 941 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 942 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 943 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 944 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 945 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 946 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 947 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 948 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 949 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 950 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 951 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 952 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 953 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 954 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 955 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 956 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 957 } 958 } 959 960 EVT ARMTargetLowering::getSetCCResultType(EVT VT) const { 961 if (!VT.isVector()) return getPointerTy(); 962 return VT.changeVectorElementTypeToInteger(); 963 } 964 965 /// getRegClassFor - Return the register class that should be used for the 966 /// specified value type. 967 TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const { 968 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 969 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 970 // load / store 4 to 8 consecutive D registers. 971 if (Subtarget->hasNEON()) { 972 if (VT == MVT::v4i64) 973 return ARM::QQPRRegisterClass; 974 else if (VT == MVT::v8i64) 975 return ARM::QQQQPRRegisterClass; 976 } 977 return TargetLowering::getRegClassFor(VT); 978 } 979 980 // Create a fast isel object. 981 FastISel * 982 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const { 983 return ARM::createFastISel(funcInfo); 984 } 985 986 /// getMaximalGlobalOffset - Returns the maximal possible offset which can 987 /// be used for loads / stores from the global. 988 unsigned ARMTargetLowering::getMaximalGlobalOffset() const { 989 return (Subtarget->isThumb1Only() ? 127 : 4095); 990 } 991 992 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 993 unsigned NumVals = N->getNumValues(); 994 if (!NumVals) 995 return Sched::RegPressure; 996 997 for (unsigned i = 0; i != NumVals; ++i) { 998 EVT VT = N->getValueType(i); 999 if (VT == MVT::Glue || VT == MVT::Other) 1000 continue; 1001 if (VT.isFloatingPoint() || VT.isVector()) 1002 return Sched::ILP; 1003 } 1004 1005 if (!N->isMachineOpcode()) 1006 return Sched::RegPressure; 1007 1008 // Load are scheduled for latency even if there instruction itinerary 1009 // is not available. 1010 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1011 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1012 1013 if (MCID.getNumDefs() == 0) 1014 return Sched::RegPressure; 1015 if (!Itins->isEmpty() && 1016 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1017 return Sched::ILP; 1018 1019 return Sched::RegPressure; 1020 } 1021 1022 //===----------------------------------------------------------------------===// 1023 // Lowering Code 1024 //===----------------------------------------------------------------------===// 1025 1026 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1027 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1028 switch (CC) { 1029 default: llvm_unreachable("Unknown condition code!"); 1030 case ISD::SETNE: return ARMCC::NE; 1031 case ISD::SETEQ: return ARMCC::EQ; 1032 case ISD::SETGT: return ARMCC::GT; 1033 case ISD::SETGE: return ARMCC::GE; 1034 case ISD::SETLT: return ARMCC::LT; 1035 case ISD::SETLE: return ARMCC::LE; 1036 case ISD::SETUGT: return ARMCC::HI; 1037 case ISD::SETUGE: return ARMCC::HS; 1038 case ISD::SETULT: return ARMCC::LO; 1039 case ISD::SETULE: return ARMCC::LS; 1040 } 1041 } 1042 1043 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1044 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1045 ARMCC::CondCodes &CondCode2) { 1046 CondCode2 = ARMCC::AL; 1047 switch (CC) { 1048 default: llvm_unreachable("Unknown FP condition!"); 1049 case ISD::SETEQ: 1050 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1051 case ISD::SETGT: 1052 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1053 case ISD::SETGE: 1054 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1055 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1056 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1057 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1058 case ISD::SETO: CondCode = ARMCC::VC; break; 1059 case ISD::SETUO: CondCode = ARMCC::VS; break; 1060 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1061 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1062 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1063 case ISD::SETLT: 1064 case ISD::SETULT: CondCode = ARMCC::LT; break; 1065 case ISD::SETLE: 1066 case ISD::SETULE: CondCode = ARMCC::LE; break; 1067 case ISD::SETNE: 1068 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1069 } 1070 } 1071 1072 //===----------------------------------------------------------------------===// 1073 // Calling Convention Implementation 1074 //===----------------------------------------------------------------------===// 1075 1076 #include "ARMGenCallingConv.inc" 1077 1078 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the 1079 /// given CallingConvention value. 1080 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1081 bool Return, 1082 bool isVarArg) const { 1083 switch (CC) { 1084 default: 1085 llvm_unreachable("Unsupported calling convention"); 1086 case CallingConv::Fast: 1087 if (Subtarget->hasVFP2() && !isVarArg) { 1088 if (!Subtarget->isAAPCS_ABI()) 1089 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1090 // For AAPCS ABI targets, just use VFP variant of the calling convention. 1091 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1092 } 1093 // Fallthrough 1094 case CallingConv::C: { 1095 // Use target triple & subtarget features to do actual dispatch. 1096 if (!Subtarget->isAAPCS_ABI()) 1097 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1098 else if (Subtarget->hasVFP2() && 1099 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1100 !isVarArg) 1101 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1102 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1103 } 1104 case CallingConv::ARM_AAPCS_VFP: 1105 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1106 case CallingConv::ARM_AAPCS: 1107 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1108 case CallingConv::ARM_APCS: 1109 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1110 } 1111 } 1112 1113 /// LowerCallResult - Lower the result values of a call into the 1114 /// appropriate copies out of appropriate physical registers. 1115 SDValue 1116 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1117 CallingConv::ID CallConv, bool isVarArg, 1118 const SmallVectorImpl<ISD::InputArg> &Ins, 1119 DebugLoc dl, SelectionDAG &DAG, 1120 SmallVectorImpl<SDValue> &InVals) const { 1121 1122 // Assign locations to each value returned by this call. 1123 SmallVector<CCValAssign, 16> RVLocs; 1124 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1125 getTargetMachine(), RVLocs, *DAG.getContext(), Call); 1126 CCInfo.AnalyzeCallResult(Ins, 1127 CCAssignFnForNode(CallConv, /* Return*/ true, 1128 isVarArg)); 1129 1130 // Copy all of the result registers out of their specified physreg. 1131 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1132 CCValAssign VA = RVLocs[i]; 1133 1134 SDValue Val; 1135 if (VA.needsCustom()) { 1136 // Handle f64 or half of a v2f64. 1137 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1138 InFlag); 1139 Chain = Lo.getValue(1); 1140 InFlag = Lo.getValue(2); 1141 VA = RVLocs[++i]; // skip ahead to next loc 1142 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1143 InFlag); 1144 Chain = Hi.getValue(1); 1145 InFlag = Hi.getValue(2); 1146 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1147 1148 if (VA.getLocVT() == MVT::v2f64) { 1149 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1150 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1151 DAG.getConstant(0, MVT::i32)); 1152 1153 VA = RVLocs[++i]; // skip ahead to next loc 1154 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1155 Chain = Lo.getValue(1); 1156 InFlag = Lo.getValue(2); 1157 VA = RVLocs[++i]; // skip ahead to next loc 1158 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1159 Chain = Hi.getValue(1); 1160 InFlag = Hi.getValue(2); 1161 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1162 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1163 DAG.getConstant(1, MVT::i32)); 1164 } 1165 } else { 1166 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1167 InFlag); 1168 Chain = Val.getValue(1); 1169 InFlag = Val.getValue(2); 1170 } 1171 1172 switch (VA.getLocInfo()) { 1173 default: llvm_unreachable("Unknown loc info!"); 1174 case CCValAssign::Full: break; 1175 case CCValAssign::BCvt: 1176 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1177 break; 1178 } 1179 1180 InVals.push_back(Val); 1181 } 1182 1183 return Chain; 1184 } 1185 1186 /// LowerMemOpCallTo - Store the argument to the stack. 1187 SDValue 1188 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, 1189 SDValue StackPtr, SDValue Arg, 1190 DebugLoc dl, SelectionDAG &DAG, 1191 const CCValAssign &VA, 1192 ISD::ArgFlagsTy Flags) const { 1193 unsigned LocMemOffset = VA.getLocMemOffset(); 1194 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); 1195 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1196 return DAG.getStore(Chain, dl, Arg, PtrOff, 1197 MachinePointerInfo::getStack(LocMemOffset), 1198 false, false, 0); 1199 } 1200 1201 void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG, 1202 SDValue Chain, SDValue &Arg, 1203 RegsToPassVector &RegsToPass, 1204 CCValAssign &VA, CCValAssign &NextVA, 1205 SDValue &StackPtr, 1206 SmallVector<SDValue, 8> &MemOpChains, 1207 ISD::ArgFlagsTy Flags) const { 1208 1209 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1210 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1211 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd)); 1212 1213 if (NextVA.isRegLoc()) 1214 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1))); 1215 else { 1216 assert(NextVA.isMemLoc()); 1217 if (StackPtr.getNode() == 0) 1218 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1219 1220 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1), 1221 dl, DAG, NextVA, 1222 Flags)); 1223 } 1224 } 1225 1226 /// LowerCall - Lowering a call into a callseq_start <- 1227 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1228 /// nodes. 1229 SDValue 1230 ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee, 1231 CallingConv::ID CallConv, bool isVarArg, 1232 bool &isTailCall, 1233 const SmallVectorImpl<ISD::OutputArg> &Outs, 1234 const SmallVectorImpl<SDValue> &OutVals, 1235 const SmallVectorImpl<ISD::InputArg> &Ins, 1236 DebugLoc dl, SelectionDAG &DAG, 1237 SmallVectorImpl<SDValue> &InVals) const { 1238 MachineFunction &MF = DAG.getMachineFunction(); 1239 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1240 bool IsSibCall = false; 1241 // Disable tail calls if they're not supported. 1242 if (!EnableARMTailCalls && !Subtarget->supportsTailCall()) 1243 isTailCall = false; 1244 if (isTailCall) { 1245 // Check if it's really possible to do a tail call. 1246 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1247 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(), 1248 Outs, OutVals, Ins, DAG); 1249 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1250 // detected sibcalls. 1251 if (isTailCall) { 1252 ++NumTailCalls; 1253 IsSibCall = true; 1254 } 1255 } 1256 1257 // Analyze operands of the call, assigning locations to each operand. 1258 SmallVector<CCValAssign, 16> ArgLocs; 1259 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1260 getTargetMachine(), ArgLocs, *DAG.getContext(), Call); 1261 CCInfo.AnalyzeCallOperands(Outs, 1262 CCAssignFnForNode(CallConv, /* Return*/ false, 1263 isVarArg)); 1264 1265 // Get a count of how many bytes are to be pushed on the stack. 1266 unsigned NumBytes = CCInfo.getNextStackOffset(); 1267 1268 // For tail calls, memory operands are available in our caller's stack. 1269 if (IsSibCall) 1270 NumBytes = 0; 1271 1272 // Adjust the stack pointer for the new arguments... 1273 // These operations are automatically eliminated by the prolog/epilog pass 1274 if (!IsSibCall) 1275 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); 1276 1277 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1278 1279 RegsToPassVector RegsToPass; 1280 SmallVector<SDValue, 8> MemOpChains; 1281 1282 // Walk the register/memloc assignments, inserting copies/loads. In the case 1283 // of tail call optimization, arguments are handled later. 1284 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1285 i != e; 1286 ++i, ++realArgIdx) { 1287 CCValAssign &VA = ArgLocs[i]; 1288 SDValue Arg = OutVals[realArgIdx]; 1289 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1290 bool isByVal = Flags.isByVal(); 1291 1292 // Promote the value if needed. 1293 switch (VA.getLocInfo()) { 1294 default: llvm_unreachable("Unknown loc info!"); 1295 case CCValAssign::Full: break; 1296 case CCValAssign::SExt: 1297 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1298 break; 1299 case CCValAssign::ZExt: 1300 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1301 break; 1302 case CCValAssign::AExt: 1303 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1304 break; 1305 case CCValAssign::BCvt: 1306 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1307 break; 1308 } 1309 1310 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1311 if (VA.needsCustom()) { 1312 if (VA.getLocVT() == MVT::v2f64) { 1313 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1314 DAG.getConstant(0, MVT::i32)); 1315 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1316 DAG.getConstant(1, MVT::i32)); 1317 1318 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1319 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1320 1321 VA = ArgLocs[++i]; // skip ahead to next loc 1322 if (VA.isRegLoc()) { 1323 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1324 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1325 } else { 1326 assert(VA.isMemLoc()); 1327 1328 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1329 dl, DAG, VA, Flags)); 1330 } 1331 } else { 1332 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1333 StackPtr, MemOpChains, Flags); 1334 } 1335 } else if (VA.isRegLoc()) { 1336 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1337 } else if (isByVal) { 1338 assert(VA.isMemLoc()); 1339 unsigned offset = 0; 1340 1341 // True if this byval aggregate will be split between registers 1342 // and memory. 1343 if (CCInfo.isFirstByValRegValid()) { 1344 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 1345 unsigned int i, j; 1346 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) { 1347 SDValue Const = DAG.getConstant(4*i, MVT::i32); 1348 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1349 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1350 MachinePointerInfo(), 1351 false, false, false, 0); 1352 MemOpChains.push_back(Load.getValue(1)); 1353 RegsToPass.push_back(std::make_pair(j, Load)); 1354 } 1355 offset = ARM::R4 - CCInfo.getFirstByValReg(); 1356 CCInfo.clearFirstByValReg(); 1357 } 1358 1359 unsigned LocMemOffset = VA.getLocMemOffset(); 1360 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset); 1361 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, 1362 StkPtrOff); 1363 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset); 1364 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset); 1365 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, 1366 MVT::i32); 1367 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, 1368 Flags.getByValAlign(), 1369 /*isVolatile=*/false, 1370 /*AlwaysInline=*/false, 1371 MachinePointerInfo(0), 1372 MachinePointerInfo(0))); 1373 1374 } else if (!IsSibCall) { 1375 assert(VA.isMemLoc()); 1376 1377 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1378 dl, DAG, VA, Flags)); 1379 } 1380 } 1381 1382 if (!MemOpChains.empty()) 1383 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1384 &MemOpChains[0], MemOpChains.size()); 1385 1386 // Build a sequence of copy-to-reg nodes chained together with token chain 1387 // and flag operands which copy the outgoing args into the appropriate regs. 1388 SDValue InFlag; 1389 // Tail call byval lowering might overwrite argument registers so in case of 1390 // tail call optimization the copies to registers are lowered later. 1391 if (!isTailCall) 1392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1394 RegsToPass[i].second, InFlag); 1395 InFlag = Chain.getValue(1); 1396 } 1397 1398 // For tail calls lower the arguments to the 'real' stack slot. 1399 if (isTailCall) { 1400 // Force all the incoming stack arguments to be loaded from the stack 1401 // before any new outgoing arguments are stored to the stack, because the 1402 // outgoing stack slots may alias the incoming argument stack slots, and 1403 // the alias isn't otherwise explicit. This is slightly more conservative 1404 // than necessary, because it means that each store effectively depends 1405 // on every argument instead of just those arguments it would clobber. 1406 1407 // Do not flag preceding copytoreg stuff together with the following stuff. 1408 InFlag = SDValue(); 1409 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1410 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1411 RegsToPass[i].second, InFlag); 1412 InFlag = Chain.getValue(1); 1413 } 1414 InFlag =SDValue(); 1415 } 1416 1417 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1418 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1419 // node so that legalize doesn't hack it. 1420 bool isDirect = false; 1421 bool isARMFunc = false; 1422 bool isLocalARMFunc = false; 1423 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1424 1425 if (EnableARMLongCalls) { 1426 assert (getTargetMachine().getRelocationModel() == Reloc::Static 1427 && "long-calls with non-static relocation model!"); 1428 // Handle a global address or an external symbol. If it's not one of 1429 // those, the target's already in a register, so we don't need to do 1430 // anything extra. 1431 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1432 const GlobalValue *GV = G->getGlobal(); 1433 // Create a constant pool entry for the callee address 1434 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1435 ARMConstantPoolValue *CPV = 1436 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 1437 1438 // Get the address of the callee into a register 1439 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1440 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1441 Callee = DAG.getLoad(getPointerTy(), dl, 1442 DAG.getEntryNode(), CPAddr, 1443 MachinePointerInfo::getConstantPool(), 1444 false, false, false, 0); 1445 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 1446 const char *Sym = S->getSymbol(); 1447 1448 // Create a constant pool entry for the callee address 1449 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1450 ARMConstantPoolValue *CPV = 1451 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1452 ARMPCLabelIndex, 0); 1453 // Get the address of the callee into a register 1454 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1455 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1456 Callee = DAG.getLoad(getPointerTy(), dl, 1457 DAG.getEntryNode(), CPAddr, 1458 MachinePointerInfo::getConstantPool(), 1459 false, false, false, 0); 1460 } 1461 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1462 const GlobalValue *GV = G->getGlobal(); 1463 isDirect = true; 1464 bool isExt = GV->isDeclaration() || GV->isWeakForLinker(); 1465 bool isStub = (isExt && Subtarget->isTargetDarwin()) && 1466 getTargetMachine().getRelocationModel() != Reloc::Static; 1467 isARMFunc = !Subtarget->isThumb() || isStub; 1468 // ARM call to a local ARM function is predicable. 1469 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking); 1470 // tBX takes a register source operand. 1471 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1472 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1473 ARMConstantPoolValue *CPV = 1474 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4); 1475 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1476 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1477 Callee = DAG.getLoad(getPointerTy(), dl, 1478 DAG.getEntryNode(), CPAddr, 1479 MachinePointerInfo::getConstantPool(), 1480 false, false, false, 0); 1481 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1482 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1483 getPointerTy(), Callee, PICLabel); 1484 } else { 1485 // On ELF targets for PIC code, direct calls should go through the PLT 1486 unsigned OpFlags = 0; 1487 if (Subtarget->isTargetELF() && 1488 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1489 OpFlags = ARMII::MO_PLT; 1490 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags); 1491 } 1492 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1493 isDirect = true; 1494 bool isStub = Subtarget->isTargetDarwin() && 1495 getTargetMachine().getRelocationModel() != Reloc::Static; 1496 isARMFunc = !Subtarget->isThumb() || isStub; 1497 // tBX takes a register source operand. 1498 const char *Sym = S->getSymbol(); 1499 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1500 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1501 ARMConstantPoolValue *CPV = 1502 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1503 ARMPCLabelIndex, 4); 1504 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1505 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1506 Callee = DAG.getLoad(getPointerTy(), dl, 1507 DAG.getEntryNode(), CPAddr, 1508 MachinePointerInfo::getConstantPool(), 1509 false, false, false, 0); 1510 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1511 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1512 getPointerTy(), Callee, PICLabel); 1513 } else { 1514 unsigned OpFlags = 0; 1515 // On ELF targets for PIC code, direct calls should go through the PLT 1516 if (Subtarget->isTargetELF() && 1517 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1518 OpFlags = ARMII::MO_PLT; 1519 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags); 1520 } 1521 } 1522 1523 // FIXME: handle tail calls differently. 1524 unsigned CallOpc; 1525 if (Subtarget->isThumb()) { 1526 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 1527 CallOpc = ARMISD::CALL_NOLINK; 1528 else 1529 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; 1530 } else { 1531 CallOpc = (isDirect || Subtarget->hasV5TOps()) 1532 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL) 1533 : ARMISD::CALL_NOLINK; 1534 } 1535 1536 std::vector<SDValue> Ops; 1537 Ops.push_back(Chain); 1538 Ops.push_back(Callee); 1539 1540 // Add argument registers to the end of the list so that they are known live 1541 // into the call. 1542 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1543 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1544 RegsToPass[i].second.getValueType())); 1545 1546 if (InFlag.getNode()) 1547 Ops.push_back(InFlag); 1548 1549 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1550 if (isTailCall) 1551 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size()); 1552 1553 // Returns a chain and a flag for retval copy to use. 1554 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size()); 1555 InFlag = Chain.getValue(1); 1556 1557 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1558 DAG.getIntPtrConstant(0, true), InFlag); 1559 if (!Ins.empty()) 1560 InFlag = Chain.getValue(1); 1561 1562 // Handle result values, copying them out of physregs into vregs that we 1563 // return. 1564 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, 1565 dl, DAG, InVals); 1566 } 1567 1568 /// HandleByVal - Every parameter *after* a byval parameter is passed 1569 /// on the stack. Remember the next parameter register to allocate, 1570 /// and then confiscate the rest of the parameter registers to insure 1571 /// this. 1572 void 1573 llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const { 1574 unsigned reg = State->AllocateReg(GPRArgRegs, 4); 1575 assert((State->getCallOrPrologue() == Prologue || 1576 State->getCallOrPrologue() == Call) && 1577 "unhandled ParmContext"); 1578 if ((!State->isFirstByValRegValid()) && 1579 (ARM::R0 <= reg) && (reg <= ARM::R3)) { 1580 State->setFirstByValReg(reg); 1581 // At a call site, a byval parameter that is split between 1582 // registers and memory needs its size truncated here. In a 1583 // function prologue, such byval parameters are reassembled in 1584 // memory, and are not truncated. 1585 if (State->getCallOrPrologue() == Call) { 1586 unsigned excess = 4 * (ARM::R4 - reg); 1587 assert(size >= excess && "expected larger existing stack allocation"); 1588 size -= excess; 1589 } 1590 } 1591 // Confiscate any remaining parameter registers to preclude their 1592 // assignment to subsequent parameters. 1593 while (State->AllocateReg(GPRArgRegs, 4)) 1594 ; 1595 } 1596 1597 /// MatchingStackOffset - Return true if the given stack call argument is 1598 /// already available in the same position (relatively) of the caller's 1599 /// incoming argument stack. 1600 static 1601 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 1602 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, 1603 const ARMInstrInfo *TII) { 1604 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; 1605 int FI = INT_MAX; 1606 if (Arg.getOpcode() == ISD::CopyFromReg) { 1607 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 1608 if (!TargetRegisterInfo::isVirtualRegister(VR)) 1609 return false; 1610 MachineInstr *Def = MRI->getVRegDef(VR); 1611 if (!Def) 1612 return false; 1613 if (!Flags.isByVal()) { 1614 if (!TII->isLoadFromStackSlot(Def, FI)) 1615 return false; 1616 } else { 1617 return false; 1618 } 1619 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 1620 if (Flags.isByVal()) 1621 // ByVal argument is passed in as a pointer but it's now being 1622 // dereferenced. e.g. 1623 // define @foo(%struct.X* %A) { 1624 // tail call @bar(%struct.X* byval %A) 1625 // } 1626 return false; 1627 SDValue Ptr = Ld->getBasePtr(); 1628 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 1629 if (!FINode) 1630 return false; 1631 FI = FINode->getIndex(); 1632 } else 1633 return false; 1634 1635 assert(FI != INT_MAX); 1636 if (!MFI->isFixedObjectIndex(FI)) 1637 return false; 1638 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); 1639 } 1640 1641 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 1642 /// for tail call optimization. Targets which want to do tail call 1643 /// optimization should implement this function. 1644 bool 1645 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 1646 CallingConv::ID CalleeCC, 1647 bool isVarArg, 1648 bool isCalleeStructRet, 1649 bool isCallerStructRet, 1650 const SmallVectorImpl<ISD::OutputArg> &Outs, 1651 const SmallVectorImpl<SDValue> &OutVals, 1652 const SmallVectorImpl<ISD::InputArg> &Ins, 1653 SelectionDAG& DAG) const { 1654 const Function *CallerF = DAG.getMachineFunction().getFunction(); 1655 CallingConv::ID CallerCC = CallerF->getCallingConv(); 1656 bool CCMatch = CallerCC == CalleeCC; 1657 1658 // Look for obvious safe cases to perform tail call optimization that do not 1659 // require ABI changes. This is what gcc calls sibcall. 1660 1661 // Do not sibcall optimize vararg calls unless the call site is not passing 1662 // any arguments. 1663 if (isVarArg && !Outs.empty()) 1664 return false; 1665 1666 // Also avoid sibcall optimization if either caller or callee uses struct 1667 // return semantics. 1668 if (isCalleeStructRet || isCallerStructRet) 1669 return false; 1670 1671 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo:: 1672 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as 1673 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation 1674 // support in the assembler and linker to be used. This would need to be 1675 // fixed to fully support tail calls in Thumb1. 1676 // 1677 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take 1678 // LR. This means if we need to reload LR, it takes an extra instructions, 1679 // which outweighs the value of the tail call; but here we don't know yet 1680 // whether LR is going to be used. Probably the right approach is to 1681 // generate the tail call here and turn it back into CALL/RET in 1682 // emitEpilogue if LR is used. 1683 1684 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls, 1685 // but we need to make sure there are enough registers; the only valid 1686 // registers are the 4 used for parameters. We don't currently do this 1687 // case. 1688 if (Subtarget->isThumb1Only()) 1689 return false; 1690 1691 // If the calling conventions do not match, then we'd better make sure the 1692 // results are returned in the same way as what the caller expects. 1693 if (!CCMatch) { 1694 SmallVector<CCValAssign, 16> RVLocs1; 1695 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), 1696 getTargetMachine(), RVLocs1, *DAG.getContext(), Call); 1697 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg)); 1698 1699 SmallVector<CCValAssign, 16> RVLocs2; 1700 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), 1701 getTargetMachine(), RVLocs2, *DAG.getContext(), Call); 1702 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg)); 1703 1704 if (RVLocs1.size() != RVLocs2.size()) 1705 return false; 1706 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 1707 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 1708 return false; 1709 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 1710 return false; 1711 if (RVLocs1[i].isRegLoc()) { 1712 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 1713 return false; 1714 } else { 1715 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 1716 return false; 1717 } 1718 } 1719 } 1720 1721 // If the callee takes no arguments then go on to check the results of the 1722 // call. 1723 if (!Outs.empty()) { 1724 // Check if stack adjustment is needed. For now, do not do this if any 1725 // argument is passed on the stack. 1726 SmallVector<CCValAssign, 16> ArgLocs; 1727 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), 1728 getTargetMachine(), ArgLocs, *DAG.getContext(), Call); 1729 CCInfo.AnalyzeCallOperands(Outs, 1730 CCAssignFnForNode(CalleeCC, false, isVarArg)); 1731 if (CCInfo.getNextStackOffset()) { 1732 MachineFunction &MF = DAG.getMachineFunction(); 1733 1734 // Check if the arguments are already laid out in the right way as 1735 // the caller's fixed stack objects. 1736 MachineFrameInfo *MFI = MF.getFrameInfo(); 1737 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 1738 const ARMInstrInfo *TII = 1739 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo(); 1740 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1741 i != e; 1742 ++i, ++realArgIdx) { 1743 CCValAssign &VA = ArgLocs[i]; 1744 EVT RegVT = VA.getLocVT(); 1745 SDValue Arg = OutVals[realArgIdx]; 1746 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1747 if (VA.getLocInfo() == CCValAssign::Indirect) 1748 return false; 1749 if (VA.needsCustom()) { 1750 // f64 and vector types are split into multiple registers or 1751 // register/stack-slot combinations. The types will not match 1752 // the registers; give up on memory f64 refs until we figure 1753 // out what to do about this. 1754 if (!VA.isRegLoc()) 1755 return false; 1756 if (!ArgLocs[++i].isRegLoc()) 1757 return false; 1758 if (RegVT == MVT::v2f64) { 1759 if (!ArgLocs[++i].isRegLoc()) 1760 return false; 1761 if (!ArgLocs[++i].isRegLoc()) 1762 return false; 1763 } 1764 } else if (!VA.isRegLoc()) { 1765 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 1766 MFI, MRI, TII)) 1767 return false; 1768 } 1769 } 1770 } 1771 } 1772 1773 return true; 1774 } 1775 1776 SDValue 1777 ARMTargetLowering::LowerReturn(SDValue Chain, 1778 CallingConv::ID CallConv, bool isVarArg, 1779 const SmallVectorImpl<ISD::OutputArg> &Outs, 1780 const SmallVectorImpl<SDValue> &OutVals, 1781 DebugLoc dl, SelectionDAG &DAG) const { 1782 1783 // CCValAssign - represent the assignment of the return value to a location. 1784 SmallVector<CCValAssign, 16> RVLocs; 1785 1786 // CCState - Info about the registers and stack slots. 1787 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 1788 getTargetMachine(), RVLocs, *DAG.getContext(), Call); 1789 1790 // Analyze outgoing return values. 1791 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true, 1792 isVarArg)); 1793 1794 // If this is the first return lowered for this function, add 1795 // the regs to the liveout set for the function. 1796 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { 1797 for (unsigned i = 0; i != RVLocs.size(); ++i) 1798 if (RVLocs[i].isRegLoc()) 1799 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); 1800 } 1801 1802 SDValue Flag; 1803 1804 // Copy the result values into the output registers. 1805 for (unsigned i = 0, realRVLocIdx = 0; 1806 i != RVLocs.size(); 1807 ++i, ++realRVLocIdx) { 1808 CCValAssign &VA = RVLocs[i]; 1809 assert(VA.isRegLoc() && "Can only return in registers!"); 1810 1811 SDValue Arg = OutVals[realRVLocIdx]; 1812 1813 switch (VA.getLocInfo()) { 1814 default: llvm_unreachable("Unknown loc info!"); 1815 case CCValAssign::Full: break; 1816 case CCValAssign::BCvt: 1817 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1818 break; 1819 } 1820 1821 if (VA.needsCustom()) { 1822 if (VA.getLocVT() == MVT::v2f64) { 1823 // Extract the first half and return it in two registers. 1824 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1825 DAG.getConstant(0, MVT::i32)); 1826 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 1827 DAG.getVTList(MVT::i32, MVT::i32), Half); 1828 1829 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag); 1830 Flag = Chain.getValue(1); 1831 VA = RVLocs[++i]; // skip ahead to next loc 1832 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 1833 HalfGPRs.getValue(1), Flag); 1834 Flag = Chain.getValue(1); 1835 VA = RVLocs[++i]; // skip ahead to next loc 1836 1837 // Extract the 2nd half and fall through to handle it as an f64 value. 1838 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1839 DAG.getConstant(1, MVT::i32)); 1840 } 1841 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 1842 // available. 1843 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1844 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1); 1845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag); 1846 Flag = Chain.getValue(1); 1847 VA = RVLocs[++i]; // skip ahead to next loc 1848 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1), 1849 Flag); 1850 } else 1851 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 1852 1853 // Guarantee that all emitted copies are 1854 // stuck together, avoiding something bad. 1855 Flag = Chain.getValue(1); 1856 } 1857 1858 SDValue result; 1859 if (Flag.getNode()) 1860 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag); 1861 else // Return Void 1862 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain); 1863 1864 return result; 1865 } 1866 1867 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const { 1868 if (N->getNumValues() != 1) 1869 return false; 1870 if (!N->hasNUsesOfValue(1, 0)) 1871 return false; 1872 1873 unsigned NumCopies = 0; 1874 SDNode* Copies[2]; 1875 SDNode *Use = *N->use_begin(); 1876 if (Use->getOpcode() == ISD::CopyToReg) { 1877 Copies[NumCopies++] = Use; 1878 } else if (Use->getOpcode() == ARMISD::VMOVRRD) { 1879 // f64 returned in a pair of GPRs. 1880 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end(); 1881 UI != UE; ++UI) { 1882 if (UI->getOpcode() != ISD::CopyToReg) 1883 return false; 1884 Copies[UI.getUse().getResNo()] = *UI; 1885 ++NumCopies; 1886 } 1887 } else if (Use->getOpcode() == ISD::BITCAST) { 1888 // f32 returned in a single GPR. 1889 if (!Use->hasNUsesOfValue(1, 0)) 1890 return false; 1891 Use = *Use->use_begin(); 1892 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0)) 1893 return false; 1894 Copies[NumCopies++] = Use; 1895 } else { 1896 return false; 1897 } 1898 1899 if (NumCopies != 1 && NumCopies != 2) 1900 return false; 1901 1902 bool HasRet = false; 1903 for (unsigned i = 0; i < NumCopies; ++i) { 1904 SDNode *Copy = Copies[i]; 1905 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 1906 UI != UE; ++UI) { 1907 if (UI->getOpcode() == ISD::CopyToReg) { 1908 SDNode *Use = *UI; 1909 if (Use == Copies[0] || Use == Copies[1]) 1910 continue; 1911 return false; 1912 } 1913 if (UI->getOpcode() != ARMISD::RET_FLAG) 1914 return false; 1915 HasRet = true; 1916 } 1917 } 1918 1919 return HasRet; 1920 } 1921 1922 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 1923 if (!EnableARMTailCalls) 1924 return false; 1925 1926 if (!CI->isTailCall()) 1927 return false; 1928 1929 return !Subtarget->isThumb1Only(); 1930 } 1931 1932 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 1933 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 1934 // one of the above mentioned nodes. It has to be wrapped because otherwise 1935 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 1936 // be used to form addressing mode. These wrapped nodes will be selected 1937 // into MOVi. 1938 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 1939 EVT PtrVT = Op.getValueType(); 1940 // FIXME there is no actual debug info here 1941 DebugLoc dl = Op.getDebugLoc(); 1942 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 1943 SDValue Res; 1944 if (CP->isMachineConstantPoolEntry()) 1945 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 1946 CP->getAlignment()); 1947 else 1948 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 1949 CP->getAlignment()); 1950 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 1951 } 1952 1953 unsigned ARMTargetLowering::getJumpTableEncoding() const { 1954 return MachineJumpTableInfo::EK_Inline; 1955 } 1956 1957 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 1958 SelectionDAG &DAG) const { 1959 MachineFunction &MF = DAG.getMachineFunction(); 1960 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1961 unsigned ARMPCLabelIndex = 0; 1962 DebugLoc DL = Op.getDebugLoc(); 1963 EVT PtrVT = getPointerTy(); 1964 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 1965 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 1966 SDValue CPAddr; 1967 if (RelocM == Reloc::Static) { 1968 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 1969 } else { 1970 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 1971 ARMPCLabelIndex = AFI->createPICLabelUId(); 1972 ARMConstantPoolValue *CPV = 1973 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 1974 ARMCP::CPBlockAddress, PCAdj); 1975 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 1976 } 1977 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 1978 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr, 1979 MachinePointerInfo::getConstantPool(), 1980 false, false, false, 0); 1981 if (RelocM == Reloc::Static) 1982 return Result; 1983 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1984 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 1985 } 1986 1987 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 1988 SDValue 1989 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 1990 SelectionDAG &DAG) const { 1991 DebugLoc dl = GA->getDebugLoc(); 1992 EVT PtrVT = getPointerTy(); 1993 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 1994 MachineFunction &MF = DAG.getMachineFunction(); 1995 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1996 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1997 ARMConstantPoolValue *CPV = 1998 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 1999 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2000 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2001 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2002 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, 2003 MachinePointerInfo::getConstantPool(), 2004 false, false, false, 0); 2005 SDValue Chain = Argument.getValue(1); 2006 2007 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2008 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2009 2010 // call __tls_get_addr. 2011 ArgListTy Args; 2012 ArgListEntry Entry; 2013 Entry.Node = Argument; 2014 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2015 Args.push_back(Entry); 2016 // FIXME: is there useful debug info available here? 2017 std::pair<SDValue, SDValue> CallResult = 2018 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()), 2019 false, false, false, false, 2020 0, CallingConv::C, false, /*isReturnValueUsed=*/true, 2021 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl); 2022 return CallResult.first; 2023 } 2024 2025 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2026 // "local exec" model. 2027 SDValue 2028 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2029 SelectionDAG &DAG) const { 2030 const GlobalValue *GV = GA->getGlobal(); 2031 DebugLoc dl = GA->getDebugLoc(); 2032 SDValue Offset; 2033 SDValue Chain = DAG.getEntryNode(); 2034 EVT PtrVT = getPointerTy(); 2035 // Get the Thread Pointer 2036 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2037 2038 if (GV->isDeclaration()) { 2039 MachineFunction &MF = DAG.getMachineFunction(); 2040 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2041 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2042 // Initial exec model. 2043 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2044 ARMConstantPoolValue *CPV = 2045 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2046 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2047 true); 2048 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2049 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2050 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2051 MachinePointerInfo::getConstantPool(), 2052 false, false, false, 0); 2053 Chain = Offset.getValue(1); 2054 2055 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2056 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2057 2058 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2059 MachinePointerInfo::getConstantPool(), 2060 false, false, false, 0); 2061 } else { 2062 // local exec model 2063 ARMConstantPoolValue *CPV = 2064 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2065 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2066 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2067 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2068 MachinePointerInfo::getConstantPool(), 2069 false, false, false, 0); 2070 } 2071 2072 // The address of the thread local variable is the add of the thread 2073 // pointer with the offset of the variable. 2074 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2075 } 2076 2077 SDValue 2078 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2079 // TODO: implement the "local dynamic" model 2080 assert(Subtarget->isTargetELF() && 2081 "TLS not implemented for non-ELF targets"); 2082 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2083 // If the relocation model is PIC, use the "General Dynamic" TLS Model, 2084 // otherwise use the "Local Exec" TLS Model 2085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) 2086 return LowerToTLSGeneralDynamicModel(GA, DAG); 2087 else 2088 return LowerToTLSExecModels(GA, DAG); 2089 } 2090 2091 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 2092 SelectionDAG &DAG) const { 2093 EVT PtrVT = getPointerTy(); 2094 DebugLoc dl = Op.getDebugLoc(); 2095 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2096 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2097 if (RelocM == Reloc::PIC_) { 2098 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility(); 2099 ARMConstantPoolValue *CPV = 2100 ARMConstantPoolConstant::Create(GV, 2101 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT); 2102 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2103 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2104 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 2105 CPAddr, 2106 MachinePointerInfo::getConstantPool(), 2107 false, false, false, 0); 2108 SDValue Chain = Result.getValue(1); 2109 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); 2110 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT); 2111 if (!UseGOTOFF) 2112 Result = DAG.getLoad(PtrVT, dl, Chain, Result, 2113 MachinePointerInfo::getGOT(), 2114 false, false, false, 0); 2115 return Result; 2116 } 2117 2118 // If we have T2 ops, we can materialize the address directly via movt/movw 2119 // pair. This is always cheaper. 2120 if (Subtarget->useMovt()) { 2121 ++NumMovwMovt; 2122 // FIXME: Once remat is capable of dealing with instructions with register 2123 // operands, expand this into two nodes. 2124 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2125 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2126 } else { 2127 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2128 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2129 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2130 MachinePointerInfo::getConstantPool(), 2131 false, false, false, 0); 2132 } 2133 } 2134 2135 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 2136 SelectionDAG &DAG) const { 2137 EVT PtrVT = getPointerTy(); 2138 DebugLoc dl = Op.getDebugLoc(); 2139 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2140 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2141 MachineFunction &MF = DAG.getMachineFunction(); 2142 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2143 2144 // FIXME: Enable this for static codegen when tool issues are fixed. 2145 if (Subtarget->useMovt() && RelocM != Reloc::Static) { 2146 ++NumMovwMovt; 2147 // FIXME: Once remat is capable of dealing with instructions with register 2148 // operands, expand this into two nodes. 2149 if (RelocM == Reloc::Static) 2150 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2151 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2152 2153 unsigned Wrapper = (RelocM == Reloc::PIC_) 2154 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN; 2155 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, 2156 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2157 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2158 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 2159 MachinePointerInfo::getGOT(), 2160 false, false, false, 0); 2161 return Result; 2162 } 2163 2164 unsigned ARMPCLabelIndex = 0; 2165 SDValue CPAddr; 2166 if (RelocM == Reloc::Static) { 2167 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2168 } else { 2169 ARMPCLabelIndex = AFI->createPICLabelUId(); 2170 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8); 2171 ARMConstantPoolValue *CPV = 2172 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 2173 PCAdj); 2174 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2175 } 2176 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2177 2178 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2179 MachinePointerInfo::getConstantPool(), 2180 false, false, false, 0); 2181 SDValue Chain = Result.getValue(1); 2182 2183 if (RelocM == Reloc::PIC_) { 2184 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2185 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2186 } 2187 2188 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2189 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(), 2190 false, false, false, 0); 2191 2192 return Result; 2193 } 2194 2195 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, 2196 SelectionDAG &DAG) const { 2197 assert(Subtarget->isTargetELF() && 2198 "GLOBAL OFFSET TABLE not implemented for non-ELF targets"); 2199 MachineFunction &MF = DAG.getMachineFunction(); 2200 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2201 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2202 EVT PtrVT = getPointerTy(); 2203 DebugLoc dl = Op.getDebugLoc(); 2204 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2205 ARMConstantPoolValue *CPV = 2206 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_", 2207 ARMPCLabelIndex, PCAdj); 2208 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2209 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2210 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2211 MachinePointerInfo::getConstantPool(), 2212 false, false, false, 0); 2213 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2214 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2215 } 2216 2217 SDValue 2218 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 2219 DebugLoc dl = Op.getDebugLoc(); 2220 SDValue Val = DAG.getConstant(0, MVT::i32); 2221 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 2222 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 2223 Op.getOperand(1), Val); 2224 } 2225 2226 SDValue 2227 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 2228 DebugLoc dl = Op.getDebugLoc(); 2229 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 2230 Op.getOperand(1), DAG.getConstant(0, MVT::i32)); 2231 } 2232 2233 SDValue 2234 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 2235 const ARMSubtarget *Subtarget) const { 2236 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2237 DebugLoc dl = Op.getDebugLoc(); 2238 switch (IntNo) { 2239 default: return SDValue(); // Don't custom lower most intrinsics. 2240 case Intrinsic::arm_thread_pointer: { 2241 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2242 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2243 } 2244 case Intrinsic::eh_sjlj_lsda: { 2245 MachineFunction &MF = DAG.getMachineFunction(); 2246 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2247 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2248 EVT PtrVT = getPointerTy(); 2249 DebugLoc dl = Op.getDebugLoc(); 2250 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2251 SDValue CPAddr; 2252 unsigned PCAdj = (RelocM != Reloc::PIC_) 2253 ? 0 : (Subtarget->isThumb() ? 4 : 8); 2254 ARMConstantPoolValue *CPV = 2255 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex, 2256 ARMCP::CPLSDA, PCAdj); 2257 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2258 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2259 SDValue Result = 2260 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2261 MachinePointerInfo::getConstantPool(), 2262 false, false, false, 0); 2263 2264 if (RelocM == Reloc::PIC_) { 2265 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2266 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2267 } 2268 return Result; 2269 } 2270 case Intrinsic::arm_neon_vmulls: 2271 case Intrinsic::arm_neon_vmullu: { 2272 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 2273 ? ARMISD::VMULLs : ARMISD::VMULLu; 2274 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(), 2275 Op.getOperand(1), Op.getOperand(2)); 2276 } 2277 } 2278 } 2279 2280 static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG, 2281 const ARMSubtarget *Subtarget) { 2282 DebugLoc dl = Op.getDebugLoc(); 2283 if (!Subtarget->hasDataBarrier()) { 2284 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2285 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2286 // here. 2287 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2288 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!"); 2289 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2290 DAG.getConstant(0, MVT::i32)); 2291 } 2292 2293 SDValue Op5 = Op.getOperand(5); 2294 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0; 2295 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2296 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 2297 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0); 2298 2299 ARM_MB::MemBOpt DMBOpt; 2300 if (isDeviceBarrier) 2301 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY; 2302 else 2303 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH; 2304 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0), 2305 DAG.getConstant(DMBOpt, MVT::i32)); 2306 } 2307 2308 2309 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 2310 const ARMSubtarget *Subtarget) { 2311 // FIXME: handle "fence singlethread" more efficiently. 2312 DebugLoc dl = Op.getDebugLoc(); 2313 if (!Subtarget->hasDataBarrier()) { 2314 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2315 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2316 // here. 2317 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2318 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!"); 2319 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2320 DAG.getConstant(0, MVT::i32)); 2321 } 2322 2323 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0), 2324 DAG.getConstant(ARM_MB::ISH, MVT::i32)); 2325 } 2326 2327 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 2328 const ARMSubtarget *Subtarget) { 2329 // ARM pre v5TE and Thumb1 does not have preload instructions. 2330 if (!(Subtarget->isThumb2() || 2331 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 2332 // Just preserve the chain. 2333 return Op.getOperand(0); 2334 2335 DebugLoc dl = Op.getDebugLoc(); 2336 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 2337 if (!isRead && 2338 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 2339 // ARMv7 with MP extension has PLDW. 2340 return Op.getOperand(0); 2341 2342 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 2343 if (Subtarget->isThumb()) { 2344 // Invert the bits. 2345 isRead = ~isRead & 1; 2346 isData = ~isData & 1; 2347 } 2348 2349 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 2350 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32), 2351 DAG.getConstant(isData, MVT::i32)); 2352 } 2353 2354 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 2355 MachineFunction &MF = DAG.getMachineFunction(); 2356 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 2357 2358 // vastart just stores the address of the VarArgsFrameIndex slot into the 2359 // memory location argument. 2360 DebugLoc dl = Op.getDebugLoc(); 2361 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2362 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2363 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2364 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2365 MachinePointerInfo(SV), false, false, 0); 2366 } 2367 2368 SDValue 2369 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA, 2370 SDValue &Root, SelectionDAG &DAG, 2371 DebugLoc dl) const { 2372 MachineFunction &MF = DAG.getMachineFunction(); 2373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2374 2375 TargetRegisterClass *RC; 2376 if (AFI->isThumb1OnlyFunction()) 2377 RC = ARM::tGPRRegisterClass; 2378 else 2379 RC = ARM::GPRRegisterClass; 2380 2381 // Transform the arguments stored in physical registers into virtual ones. 2382 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2383 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2384 2385 SDValue ArgValue2; 2386 if (NextVA.isMemLoc()) { 2387 MachineFrameInfo *MFI = MF.getFrameInfo(); 2388 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true); 2389 2390 // Create load node to retrieve arguments from the stack. 2391 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2392 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, 2393 MachinePointerInfo::getFixedStack(FI), 2394 false, false, false, 0); 2395 } else { 2396 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 2397 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2398 } 2399 2400 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 2401 } 2402 2403 void 2404 ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF, 2405 unsigned &VARegSize, unsigned &VARegSaveSize) 2406 const { 2407 unsigned NumGPRs; 2408 if (CCInfo.isFirstByValRegValid()) 2409 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg(); 2410 else { 2411 unsigned int firstUnalloced; 2412 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs, 2413 sizeof(GPRArgRegs) / 2414 sizeof(GPRArgRegs[0])); 2415 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0; 2416 } 2417 2418 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment(); 2419 VARegSize = NumGPRs * 4; 2420 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1); 2421 } 2422 2423 // The remaining GPRs hold either the beginning of variable-argument 2424 // data, or the beginning of an aggregate passed by value (usuall 2425 // byval). Either way, we allocate stack slots adjacent to the data 2426 // provided by our caller, and store the unallocated registers there. 2427 // If this is a variadic function, the va_list pointer will begin with 2428 // these values; otherwise, this reassembles a (byval) structure that 2429 // was split between registers and memory. 2430 void 2431 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 2432 DebugLoc dl, SDValue &Chain, 2433 unsigned ArgOffset) const { 2434 MachineFunction &MF = DAG.getMachineFunction(); 2435 MachineFrameInfo *MFI = MF.getFrameInfo(); 2436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2437 unsigned firstRegToSaveIndex; 2438 if (CCInfo.isFirstByValRegValid()) 2439 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0; 2440 else { 2441 firstRegToSaveIndex = CCInfo.getFirstUnallocated 2442 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0])); 2443 } 2444 2445 unsigned VARegSize, VARegSaveSize; 2446 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize); 2447 if (VARegSaveSize) { 2448 // If this function is vararg, store any remaining integer argument regs 2449 // to their spots on the stack so that they may be loaded by deferencing 2450 // the result of va_next. 2451 AFI->setVarArgsRegSaveSize(VARegSaveSize); 2452 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize, 2453 ArgOffset + VARegSaveSize 2454 - VARegSize, 2455 false)); 2456 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(), 2457 getPointerTy()); 2458 2459 SmallVector<SDValue, 4> MemOps; 2460 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) { 2461 TargetRegisterClass *RC; 2462 if (AFI->isThumb1OnlyFunction()) 2463 RC = ARM::tGPRRegisterClass; 2464 else 2465 RC = ARM::GPRRegisterClass; 2466 2467 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC); 2468 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 2469 SDValue Store = 2470 DAG.getStore(Val.getValue(1), dl, Val, FIN, 2471 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()), 2472 false, false, 0); 2473 MemOps.push_back(Store); 2474 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 2475 DAG.getConstant(4, getPointerTy())); 2476 } 2477 if (!MemOps.empty()) 2478 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 2479 &MemOps[0], MemOps.size()); 2480 } else 2481 // This will point to the next argument passed via stack. 2482 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true)); 2483 } 2484 2485 SDValue 2486 ARMTargetLowering::LowerFormalArguments(SDValue Chain, 2487 CallingConv::ID CallConv, bool isVarArg, 2488 const SmallVectorImpl<ISD::InputArg> 2489 &Ins, 2490 DebugLoc dl, SelectionDAG &DAG, 2491 SmallVectorImpl<SDValue> &InVals) 2492 const { 2493 MachineFunction &MF = DAG.getMachineFunction(); 2494 MachineFrameInfo *MFI = MF.getFrameInfo(); 2495 2496 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2497 2498 // Assign locations to all of the incoming arguments. 2499 SmallVector<CCValAssign, 16> ArgLocs; 2500 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 2501 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue); 2502 CCInfo.AnalyzeFormalArguments(Ins, 2503 CCAssignFnForNode(CallConv, /* Return*/ false, 2504 isVarArg)); 2505 2506 SmallVector<SDValue, 16> ArgValues; 2507 int lastInsIndex = -1; 2508 2509 SDValue ArgValue; 2510 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2511 CCValAssign &VA = ArgLocs[i]; 2512 2513 // Arguments stored in registers. 2514 if (VA.isRegLoc()) { 2515 EVT RegVT = VA.getLocVT(); 2516 2517 if (VA.needsCustom()) { 2518 // f64 and vector types are split up into multiple registers or 2519 // combinations of registers and stack slots. 2520 if (VA.getLocVT() == MVT::v2f64) { 2521 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 2522 Chain, DAG, dl); 2523 VA = ArgLocs[++i]; // skip ahead to next loc 2524 SDValue ArgValue2; 2525 if (VA.isMemLoc()) { 2526 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true); 2527 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2528 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 2529 MachinePointerInfo::getFixedStack(FI), 2530 false, false, false, 0); 2531 } else { 2532 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 2533 Chain, DAG, dl); 2534 } 2535 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 2536 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 2537 ArgValue, ArgValue1, DAG.getIntPtrConstant(0)); 2538 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 2539 ArgValue, ArgValue2, DAG.getIntPtrConstant(1)); 2540 } else 2541 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 2542 2543 } else { 2544 TargetRegisterClass *RC; 2545 2546 if (RegVT == MVT::f32) 2547 RC = ARM::SPRRegisterClass; 2548 else if (RegVT == MVT::f64) 2549 RC = ARM::DPRRegisterClass; 2550 else if (RegVT == MVT::v2f64) 2551 RC = ARM::QPRRegisterClass; 2552 else if (RegVT == MVT::i32) 2553 RC = (AFI->isThumb1OnlyFunction() ? 2554 ARM::tGPRRegisterClass : ARM::GPRRegisterClass); 2555 else 2556 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 2557 2558 // Transform the arguments in physical registers into virtual ones. 2559 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2560 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 2561 } 2562 2563 // If this is an 8 or 16-bit value, it is really passed promoted 2564 // to 32 bits. Insert an assert[sz]ext to capture this, then 2565 // truncate to the right size. 2566 switch (VA.getLocInfo()) { 2567 default: llvm_unreachable("Unknown loc info!"); 2568 case CCValAssign::Full: break; 2569 case CCValAssign::BCvt: 2570 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 2571 break; 2572 case CCValAssign::SExt: 2573 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 2574 DAG.getValueType(VA.getValVT())); 2575 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 2576 break; 2577 case CCValAssign::ZExt: 2578 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 2579 DAG.getValueType(VA.getValVT())); 2580 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 2581 break; 2582 } 2583 2584 InVals.push_back(ArgValue); 2585 2586 } else { // VA.isRegLoc() 2587 2588 // sanity check 2589 assert(VA.isMemLoc()); 2590 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 2591 2592 int index = ArgLocs[i].getValNo(); 2593 2594 // Some Ins[] entries become multiple ArgLoc[] entries. 2595 // Process them only once. 2596 if (index != lastInsIndex) 2597 { 2598 ISD::ArgFlagsTy Flags = Ins[index].Flags; 2599 // FIXME: For now, all byval parameter objects are marked mutable. 2600 // This can be changed with more analysis. 2601 // In case of tail call optimization mark all arguments mutable. 2602 // Since they could be overwritten by lowering of arguments in case of 2603 // a tail call. 2604 if (Flags.isByVal()) { 2605 unsigned VARegSize, VARegSaveSize; 2606 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize); 2607 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0); 2608 unsigned Bytes = Flags.getByValSize() - VARegSize; 2609 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects. 2610 int FI = MFI->CreateFixedObject(Bytes, 2611 VA.getLocMemOffset(), false); 2612 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy())); 2613 } else { 2614 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 2615 VA.getLocMemOffset(), true); 2616 2617 // Create load nodes to retrieve arguments from the stack. 2618 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2619 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 2620 MachinePointerInfo::getFixedStack(FI), 2621 false, false, false, 0)); 2622 } 2623 lastInsIndex = index; 2624 } 2625 } 2626 } 2627 2628 // varargs 2629 if (isVarArg) 2630 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset()); 2631 2632 return Chain; 2633 } 2634 2635 /// isFloatingPointZero - Return true if this is +0.0. 2636 static bool isFloatingPointZero(SDValue Op) { 2637 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 2638 return CFP->getValueAPF().isPosZero(); 2639 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 2640 // Maybe this has already been legalized into the constant pool? 2641 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 2642 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 2643 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 2644 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 2645 return CFP->getValueAPF().isPosZero(); 2646 } 2647 } 2648 return false; 2649 } 2650 2651 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 2652 /// the given operands. 2653 SDValue 2654 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 2655 SDValue &ARMcc, SelectionDAG &DAG, 2656 DebugLoc dl) const { 2657 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 2658 unsigned C = RHSC->getZExtValue(); 2659 if (!isLegalICmpImmediate(C)) { 2660 // Constant does not fit, try adjusting it by one? 2661 switch (CC) { 2662 default: break; 2663 case ISD::SETLT: 2664 case ISD::SETGE: 2665 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 2666 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 2667 RHS = DAG.getConstant(C-1, MVT::i32); 2668 } 2669 break; 2670 case ISD::SETULT: 2671 case ISD::SETUGE: 2672 if (C != 0 && isLegalICmpImmediate(C-1)) { 2673 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 2674 RHS = DAG.getConstant(C-1, MVT::i32); 2675 } 2676 break; 2677 case ISD::SETLE: 2678 case ISD::SETGT: 2679 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 2680 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 2681 RHS = DAG.getConstant(C+1, MVT::i32); 2682 } 2683 break; 2684 case ISD::SETULE: 2685 case ISD::SETUGT: 2686 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 2687 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 2688 RHS = DAG.getConstant(C+1, MVT::i32); 2689 } 2690 break; 2691 } 2692 } 2693 } 2694 2695 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 2696 ARMISD::NodeType CompareType; 2697 switch (CondCode) { 2698 default: 2699 CompareType = ARMISD::CMP; 2700 break; 2701 case ARMCC::EQ: 2702 case ARMCC::NE: 2703 // Uses only Z Flag 2704 CompareType = ARMISD::CMPZ; 2705 break; 2706 } 2707 ARMcc = DAG.getConstant(CondCode, MVT::i32); 2708 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 2709 } 2710 2711 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 2712 SDValue 2713 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG, 2714 DebugLoc dl) const { 2715 SDValue Cmp; 2716 if (!isFloatingPointZero(RHS)) 2717 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS); 2718 else 2719 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS); 2720 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 2721 } 2722 2723 /// duplicateCmp - Glue values can have only one use, so this function 2724 /// duplicates a comparison node. 2725 SDValue 2726 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 2727 unsigned Opc = Cmp.getOpcode(); 2728 DebugLoc DL = Cmp.getDebugLoc(); 2729 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 2730 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 2731 2732 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 2733 Cmp = Cmp.getOperand(0); 2734 Opc = Cmp.getOpcode(); 2735 if (Opc == ARMISD::CMPFP) 2736 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 2737 else { 2738 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 2739 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 2740 } 2741 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 2742 } 2743 2744 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 2745 SDValue Cond = Op.getOperand(0); 2746 SDValue SelectTrue = Op.getOperand(1); 2747 SDValue SelectFalse = Op.getOperand(2); 2748 DebugLoc dl = Op.getDebugLoc(); 2749 2750 // Convert: 2751 // 2752 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 2753 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 2754 // 2755 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 2756 const ConstantSDNode *CMOVTrue = 2757 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 2758 const ConstantSDNode *CMOVFalse = 2759 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 2760 2761 if (CMOVTrue && CMOVFalse) { 2762 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 2763 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 2764 2765 SDValue True; 2766 SDValue False; 2767 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 2768 True = SelectTrue; 2769 False = SelectFalse; 2770 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 2771 True = SelectFalse; 2772 False = SelectTrue; 2773 } 2774 2775 if (True.getNode() && False.getNode()) { 2776 EVT VT = Op.getValueType(); 2777 SDValue ARMcc = Cond.getOperand(2); 2778 SDValue CCR = Cond.getOperand(3); 2779 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 2780 assert(True.getValueType() == VT); 2781 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp); 2782 } 2783 } 2784 } 2785 2786 return DAG.getSelectCC(dl, Cond, 2787 DAG.getConstant(0, Cond.getValueType()), 2788 SelectTrue, SelectFalse, ISD::SETNE); 2789 } 2790 2791 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 2792 EVT VT = Op.getValueType(); 2793 SDValue LHS = Op.getOperand(0); 2794 SDValue RHS = Op.getOperand(1); 2795 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 2796 SDValue TrueVal = Op.getOperand(2); 2797 SDValue FalseVal = Op.getOperand(3); 2798 DebugLoc dl = Op.getDebugLoc(); 2799 2800 if (LHS.getValueType() == MVT::i32) { 2801 SDValue ARMcc; 2802 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2803 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 2804 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp); 2805 } 2806 2807 ARMCC::CondCodes CondCode, CondCode2; 2808 FPCCToARMCC(CC, CondCode, CondCode2); 2809 2810 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 2811 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 2812 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2813 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 2814 ARMcc, CCR, Cmp); 2815 if (CondCode2 != ARMCC::AL) { 2816 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32); 2817 // FIXME: Needs another CMP because flag can have but one use. 2818 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 2819 Result = DAG.getNode(ARMISD::CMOV, dl, VT, 2820 Result, TrueVal, ARMcc2, CCR, Cmp2); 2821 } 2822 return Result; 2823 } 2824 2825 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 2826 /// to morph to an integer compare sequence. 2827 static bool canChangeToInt(SDValue Op, bool &SeenZero, 2828 const ARMSubtarget *Subtarget) { 2829 SDNode *N = Op.getNode(); 2830 if (!N->hasOneUse()) 2831 // Otherwise it requires moving the value from fp to integer registers. 2832 return false; 2833 if (!N->getNumValues()) 2834 return false; 2835 EVT VT = Op.getValueType(); 2836 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 2837 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 2838 // vmrs are very slow, e.g. cortex-a8. 2839 return false; 2840 2841 if (isFloatingPointZero(Op)) { 2842 SeenZero = true; 2843 return true; 2844 } 2845 return ISD::isNormalLoad(N); 2846 } 2847 2848 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 2849 if (isFloatingPointZero(Op)) 2850 return DAG.getConstant(0, MVT::i32); 2851 2852 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 2853 return DAG.getLoad(MVT::i32, Op.getDebugLoc(), 2854 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(), 2855 Ld->isVolatile(), Ld->isNonTemporal(), 2856 Ld->isInvariant(), Ld->getAlignment()); 2857 2858 llvm_unreachable("Unknown VFP cmp argument!"); 2859 } 2860 2861 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 2862 SDValue &RetVal1, SDValue &RetVal2) { 2863 if (isFloatingPointZero(Op)) { 2864 RetVal1 = DAG.getConstant(0, MVT::i32); 2865 RetVal2 = DAG.getConstant(0, MVT::i32); 2866 return; 2867 } 2868 2869 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 2870 SDValue Ptr = Ld->getBasePtr(); 2871 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(), 2872 Ld->getChain(), Ptr, 2873 Ld->getPointerInfo(), 2874 Ld->isVolatile(), Ld->isNonTemporal(), 2875 Ld->isInvariant(), Ld->getAlignment()); 2876 2877 EVT PtrType = Ptr.getValueType(); 2878 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 2879 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(), 2880 PtrType, Ptr, DAG.getConstant(4, PtrType)); 2881 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(), 2882 Ld->getChain(), NewPtr, 2883 Ld->getPointerInfo().getWithOffset(4), 2884 Ld->isVolatile(), Ld->isNonTemporal(), 2885 Ld->isInvariant(), NewAlign); 2886 return; 2887 } 2888 2889 llvm_unreachable("Unknown VFP cmp argument!"); 2890 } 2891 2892 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 2893 /// f32 and even f64 comparisons to integer ones. 2894 SDValue 2895 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 2896 SDValue Chain = Op.getOperand(0); 2897 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 2898 SDValue LHS = Op.getOperand(2); 2899 SDValue RHS = Op.getOperand(3); 2900 SDValue Dest = Op.getOperand(4); 2901 DebugLoc dl = Op.getDebugLoc(); 2902 2903 bool SeenZero = false; 2904 if (canChangeToInt(LHS, SeenZero, Subtarget) && 2905 canChangeToInt(RHS, SeenZero, Subtarget) && 2906 // If one of the operand is zero, it's safe to ignore the NaN case since 2907 // we only care about equality comparisons. 2908 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) { 2909 // If unsafe fp math optimization is enabled and there are no other uses of 2910 // the CMP operands, and the condition code is EQ or NE, we can optimize it 2911 // to an integer comparison. 2912 if (CC == ISD::SETOEQ) 2913 CC = ISD::SETEQ; 2914 else if (CC == ISD::SETUNE) 2915 CC = ISD::SETNE; 2916 2917 SDValue ARMcc; 2918 if (LHS.getValueType() == MVT::f32) { 2919 LHS = bitcastf32Toi32(LHS, DAG); 2920 RHS = bitcastf32Toi32(RHS, DAG); 2921 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 2922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2923 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 2924 Chain, Dest, ARMcc, CCR, Cmp); 2925 } 2926 2927 SDValue LHS1, LHS2; 2928 SDValue RHS1, RHS2; 2929 expandf64Toi32(LHS, DAG, LHS1, LHS2); 2930 expandf64Toi32(RHS, DAG, RHS1, RHS2); 2931 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 2932 ARMcc = DAG.getConstant(CondCode, MVT::i32); 2933 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 2934 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 2935 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7); 2936 } 2937 2938 return SDValue(); 2939 } 2940 2941 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 2942 SDValue Chain = Op.getOperand(0); 2943 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 2944 SDValue LHS = Op.getOperand(2); 2945 SDValue RHS = Op.getOperand(3); 2946 SDValue Dest = Op.getOperand(4); 2947 DebugLoc dl = Op.getDebugLoc(); 2948 2949 if (LHS.getValueType() == MVT::i32) { 2950 SDValue ARMcc; 2951 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 2952 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2953 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 2954 Chain, Dest, ARMcc, CCR, Cmp); 2955 } 2956 2957 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 2958 2959 if (getTargetMachine().Options.UnsafeFPMath && 2960 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 2961 CC == ISD::SETNE || CC == ISD::SETUNE)) { 2962 SDValue Result = OptimizeVFPBrcond(Op, DAG); 2963 if (Result.getNode()) 2964 return Result; 2965 } 2966 2967 ARMCC::CondCodes CondCode, CondCode2; 2968 FPCCToARMCC(CC, CondCode, CondCode2); 2969 2970 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 2971 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 2972 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 2973 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 2974 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 2975 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5); 2976 if (CondCode2 != ARMCC::AL) { 2977 ARMcc = DAG.getConstant(CondCode2, MVT::i32); 2978 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 2979 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5); 2980 } 2981 return Res; 2982 } 2983 2984 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 2985 SDValue Chain = Op.getOperand(0); 2986 SDValue Table = Op.getOperand(1); 2987 SDValue Index = Op.getOperand(2); 2988 DebugLoc dl = Op.getDebugLoc(); 2989 2990 EVT PTy = getPointerTy(); 2991 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 2992 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 2993 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy); 2994 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 2995 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId); 2996 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy)); 2997 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 2998 if (Subtarget->isThumb2()) { 2999 // Thumb2 uses a two-level jump. That is, it jumps into the jump table 3000 // which does another jump to the destination. This also makes it easier 3001 // to translate it to TBB / TBH later. 3002 // FIXME: This might not work if the function is extremely large. 3003 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 3004 Addr, Op.getOperand(2), JTI, UId); 3005 } 3006 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 3007 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 3008 MachinePointerInfo::getJumpTable(), 3009 false, false, false, 0); 3010 Chain = Addr.getValue(1); 3011 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table); 3012 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3013 } else { 3014 Addr = DAG.getLoad(PTy, dl, Chain, Addr, 3015 MachinePointerInfo::getJumpTable(), 3016 false, false, false, 0); 3017 Chain = Addr.getValue(1); 3018 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3019 } 3020 } 3021 3022 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3023 EVT VT = Op.getValueType(); 3024 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering"); 3025 3026 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 3027 return Op; 3028 return DAG.UnrollVectorOp(Op.getNode()); 3029 } 3030 3031 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3032 EVT VT = Op.getValueType(); 3033 if (VT.isVector()) 3034 return LowerVectorFP_TO_INT(Op, DAG); 3035 3036 DebugLoc dl = Op.getDebugLoc(); 3037 unsigned Opc; 3038 3039 switch (Op.getOpcode()) { 3040 default: 3041 assert(0 && "Invalid opcode!"); 3042 case ISD::FP_TO_SINT: 3043 Opc = ARMISD::FTOSI; 3044 break; 3045 case ISD::FP_TO_UINT: 3046 Opc = ARMISD::FTOUI; 3047 break; 3048 } 3049 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0)); 3050 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 3051 } 3052 3053 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3054 EVT VT = Op.getValueType(); 3055 DebugLoc dl = Op.getDebugLoc(); 3056 3057 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 3058 if (VT.getVectorElementType() == MVT::f32) 3059 return Op; 3060 return DAG.UnrollVectorOp(Op.getNode()); 3061 } 3062 3063 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 3064 "Invalid type for custom lowering!"); 3065 if (VT != MVT::v4f32) 3066 return DAG.UnrollVectorOp(Op.getNode()); 3067 3068 unsigned CastOpc; 3069 unsigned Opc; 3070 switch (Op.getOpcode()) { 3071 default: 3072 assert(0 && "Invalid opcode!"); 3073 case ISD::SINT_TO_FP: 3074 CastOpc = ISD::SIGN_EXTEND; 3075 Opc = ISD::SINT_TO_FP; 3076 break; 3077 case ISD::UINT_TO_FP: 3078 CastOpc = ISD::ZERO_EXTEND; 3079 Opc = ISD::UINT_TO_FP; 3080 break; 3081 } 3082 3083 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 3084 return DAG.getNode(Opc, dl, VT, Op); 3085 } 3086 3087 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3088 EVT VT = Op.getValueType(); 3089 if (VT.isVector()) 3090 return LowerVectorINT_TO_FP(Op, DAG); 3091 3092 DebugLoc dl = Op.getDebugLoc(); 3093 unsigned Opc; 3094 3095 switch (Op.getOpcode()) { 3096 default: 3097 assert(0 && "Invalid opcode!"); 3098 case ISD::SINT_TO_FP: 3099 Opc = ARMISD::SITOF; 3100 break; 3101 case ISD::UINT_TO_FP: 3102 Opc = ARMISD::UITOF; 3103 break; 3104 } 3105 3106 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0)); 3107 return DAG.getNode(Opc, dl, VT, Op); 3108 } 3109 3110 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 3111 // Implement fcopysign with a fabs and a conditional fneg. 3112 SDValue Tmp0 = Op.getOperand(0); 3113 SDValue Tmp1 = Op.getOperand(1); 3114 DebugLoc dl = Op.getDebugLoc(); 3115 EVT VT = Op.getValueType(); 3116 EVT SrcVT = Tmp1.getValueType(); 3117 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 3118 Tmp0.getOpcode() == ARMISD::VMOVDRR; 3119 bool UseNEON = !InGPR && Subtarget->hasNEON(); 3120 3121 if (UseNEON) { 3122 // Use VBSL to copy the sign bit. 3123 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 3124 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 3125 DAG.getTargetConstant(EncodedVal, MVT::i32)); 3126 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 3127 if (VT == MVT::f64) 3128 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3129 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 3130 DAG.getConstant(32, MVT::i32)); 3131 else /*if (VT == MVT::f32)*/ 3132 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 3133 if (SrcVT == MVT::f32) { 3134 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 3135 if (VT == MVT::f64) 3136 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3137 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 3138 DAG.getConstant(32, MVT::i32)); 3139 } else if (VT == MVT::f32) 3140 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 3141 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 3142 DAG.getConstant(32, MVT::i32)); 3143 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 3144 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 3145 3146 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 3147 MVT::i32); 3148 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 3149 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 3150 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 3151 3152 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 3153 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 3154 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 3155 if (VT == MVT::f32) { 3156 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 3157 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 3158 DAG.getConstant(0, MVT::i32)); 3159 } else { 3160 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 3161 } 3162 3163 return Res; 3164 } 3165 3166 // Bitcast operand 1 to i32. 3167 if (SrcVT == MVT::f64) 3168 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 3169 &Tmp1, 1).getValue(1); 3170 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 3171 3172 // Or in the signbit with integer operations. 3173 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32); 3174 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32); 3175 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 3176 if (VT == MVT::f32) { 3177 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 3178 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 3179 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 3180 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 3181 } 3182 3183 // f64: Or the high part with signbit and then combine two parts. 3184 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 3185 &Tmp0, 1); 3186 SDValue Lo = Tmp0.getValue(0); 3187 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 3188 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 3189 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 3190 } 3191 3192 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 3193 MachineFunction &MF = DAG.getMachineFunction(); 3194 MachineFrameInfo *MFI = MF.getFrameInfo(); 3195 MFI->setReturnAddressIsTaken(true); 3196 3197 EVT VT = Op.getValueType(); 3198 DebugLoc dl = Op.getDebugLoc(); 3199 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3200 if (Depth) { 3201 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 3202 SDValue Offset = DAG.getConstant(4, MVT::i32); 3203 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 3204 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 3205 MachinePointerInfo(), false, false, false, 0); 3206 } 3207 3208 // Return LR, which contains the return address. Mark it an implicit live-in. 3209 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 3210 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 3211 } 3212 3213 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 3214 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 3215 MFI->setFrameAddressIsTaken(true); 3216 3217 EVT VT = Op.getValueType(); 3218 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful 3219 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3220 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin()) 3221 ? ARM::R7 : ARM::R11; 3222 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 3223 while (Depth--) 3224 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 3225 MachinePointerInfo(), 3226 false, false, false, 0); 3227 return FrameAddr; 3228 } 3229 3230 /// ExpandBITCAST - If the target supports VFP, this function is called to 3231 /// expand a bit convert where either the source or destination type is i64 to 3232 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 3233 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 3234 /// vectors), since the legalizer won't know what to do with that. 3235 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 3236 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3237 DebugLoc dl = N->getDebugLoc(); 3238 SDValue Op = N->getOperand(0); 3239 3240 // This function is only supposed to be called for i64 types, either as the 3241 // source or destination of the bit convert. 3242 EVT SrcVT = Op.getValueType(); 3243 EVT DstVT = N->getValueType(0); 3244 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 3245 "ExpandBITCAST called for non-i64 type"); 3246 3247 // Turn i64->f64 into VMOVDRR. 3248 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 3249 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 3250 DAG.getConstant(0, MVT::i32)); 3251 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 3252 DAG.getConstant(1, MVT::i32)); 3253 return DAG.getNode(ISD::BITCAST, dl, DstVT, 3254 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 3255 } 3256 3257 // Turn f64->i64 into VMOVRRD. 3258 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 3259 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 3260 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1); 3261 // Merge the pieces into a single i64 value. 3262 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 3263 } 3264 3265 return SDValue(); 3266 } 3267 3268 /// getZeroVector - Returns a vector of specified type with all zero elements. 3269 /// Zero vectors are used to represent vector negation and in those cases 3270 /// will be implemented with the NEON VNEG instruction. However, VNEG does 3271 /// not support i64 elements, so sometimes the zero vectors will need to be 3272 /// explicitly constructed. Regardless, use a canonical VMOV to create the 3273 /// zero vector. 3274 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) { 3275 assert(VT.isVector() && "Expected a vector type"); 3276 // The canonical modified immediate encoding of a zero vector is....0! 3277 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32); 3278 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 3279 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 3280 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 3281 } 3282 3283 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 3284 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 3285 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 3286 SelectionDAG &DAG) const { 3287 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 3288 EVT VT = Op.getValueType(); 3289 unsigned VTBits = VT.getSizeInBits(); 3290 DebugLoc dl = Op.getDebugLoc(); 3291 SDValue ShOpLo = Op.getOperand(0); 3292 SDValue ShOpHi = Op.getOperand(1); 3293 SDValue ShAmt = Op.getOperand(2); 3294 SDValue ARMcc; 3295 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 3296 3297 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 3298 3299 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 3300 DAG.getConstant(VTBits, MVT::i32), ShAmt); 3301 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 3302 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 3303 DAG.getConstant(VTBits, MVT::i32)); 3304 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 3305 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 3306 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 3307 3308 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3309 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 3310 ARMcc, DAG, dl); 3311 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 3312 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, 3313 CCR, Cmp); 3314 3315 SDValue Ops[2] = { Lo, Hi }; 3316 return DAG.getMergeValues(Ops, 2, dl); 3317 } 3318 3319 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 3320 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 3321 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 3322 SelectionDAG &DAG) const { 3323 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 3324 EVT VT = Op.getValueType(); 3325 unsigned VTBits = VT.getSizeInBits(); 3326 DebugLoc dl = Op.getDebugLoc(); 3327 SDValue ShOpLo = Op.getOperand(0); 3328 SDValue ShOpHi = Op.getOperand(1); 3329 SDValue ShAmt = Op.getOperand(2); 3330 SDValue ARMcc; 3331 3332 assert(Op.getOpcode() == ISD::SHL_PARTS); 3333 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 3334 DAG.getConstant(VTBits, MVT::i32), ShAmt); 3335 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 3336 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 3337 DAG.getConstant(VTBits, MVT::i32)); 3338 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 3339 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 3340 3341 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 3342 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3343 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 3344 ARMcc, DAG, dl); 3345 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 3346 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc, 3347 CCR, Cmp); 3348 3349 SDValue Ops[2] = { Lo, Hi }; 3350 return DAG.getMergeValues(Ops, 2, dl); 3351 } 3352 3353 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 3354 SelectionDAG &DAG) const { 3355 // The rounding mode is in bits 23:22 of the FPSCR. 3356 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 3357 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 3358 // so that the shift + and get folded into a bitfield extract. 3359 DebugLoc dl = Op.getDebugLoc(); 3360 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 3361 DAG.getConstant(Intrinsic::arm_get_fpscr, 3362 MVT::i32)); 3363 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 3364 DAG.getConstant(1U << 22, MVT::i32)); 3365 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 3366 DAG.getConstant(22, MVT::i32)); 3367 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 3368 DAG.getConstant(3, MVT::i32)); 3369 } 3370 3371 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 3372 const ARMSubtarget *ST) { 3373 EVT VT = N->getValueType(0); 3374 DebugLoc dl = N->getDebugLoc(); 3375 3376 if (!ST->hasV6T2Ops()) 3377 return SDValue(); 3378 3379 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0)); 3380 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 3381 } 3382 3383 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 3384 const ARMSubtarget *ST) { 3385 EVT VT = N->getValueType(0); 3386 DebugLoc dl = N->getDebugLoc(); 3387 3388 if (!VT.isVector()) 3389 return SDValue(); 3390 3391 // Lower vector shifts on NEON to use VSHL. 3392 assert(ST->hasNEON() && "unexpected vector shift"); 3393 3394 // Left shifts translate directly to the vshiftu intrinsic. 3395 if (N->getOpcode() == ISD::SHL) 3396 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 3397 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32), 3398 N->getOperand(0), N->getOperand(1)); 3399 3400 assert((N->getOpcode() == ISD::SRA || 3401 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 3402 3403 // NEON uses the same intrinsics for both left and right shifts. For 3404 // right shifts, the shift amounts are negative, so negate the vector of 3405 // shift amounts. 3406 EVT ShiftVT = N->getOperand(1).getValueType(); 3407 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 3408 getZeroVector(ShiftVT, DAG, dl), 3409 N->getOperand(1)); 3410 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 3411 Intrinsic::arm_neon_vshifts : 3412 Intrinsic::arm_neon_vshiftu); 3413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 3414 DAG.getConstant(vshiftInt, MVT::i32), 3415 N->getOperand(0), NegatedCount); 3416 } 3417 3418 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 3419 const ARMSubtarget *ST) { 3420 EVT VT = N->getValueType(0); 3421 DebugLoc dl = N->getDebugLoc(); 3422 3423 // We can get here for a node like i32 = ISD::SHL i32, i64 3424 if (VT != MVT::i64) 3425 return SDValue(); 3426 3427 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 3428 "Unknown shift to lower!"); 3429 3430 // We only lower SRA, SRL of 1 here, all others use generic lowering. 3431 if (!isa<ConstantSDNode>(N->getOperand(1)) || 3432 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1) 3433 return SDValue(); 3434 3435 // If we are in thumb mode, we don't have RRX. 3436 if (ST->isThumb1Only()) return SDValue(); 3437 3438 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 3439 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 3440 DAG.getConstant(0, MVT::i32)); 3441 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 3442 DAG.getConstant(1, MVT::i32)); 3443 3444 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 3445 // captures the result into a carry flag. 3446 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 3447 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1); 3448 3449 // The low part is an ARMISD::RRX operand, which shifts the carry in. 3450 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 3451 3452 // Merge the pieces into a single i64 value. 3453 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 3454 } 3455 3456 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 3457 SDValue TmpOp0, TmpOp1; 3458 bool Invert = false; 3459 bool Swap = false; 3460 unsigned Opc = 0; 3461 3462 SDValue Op0 = Op.getOperand(0); 3463 SDValue Op1 = Op.getOperand(1); 3464 SDValue CC = Op.getOperand(2); 3465 EVT VT = Op.getValueType(); 3466 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 3467 DebugLoc dl = Op.getDebugLoc(); 3468 3469 if (Op.getOperand(1).getValueType().isFloatingPoint()) { 3470 switch (SetCCOpcode) { 3471 default: llvm_unreachable("Illegal FP comparison"); break; 3472 case ISD::SETUNE: 3473 case ISD::SETNE: Invert = true; // Fallthrough 3474 case ISD::SETOEQ: 3475 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 3476 case ISD::SETOLT: 3477 case ISD::SETLT: Swap = true; // Fallthrough 3478 case ISD::SETOGT: 3479 case ISD::SETGT: Opc = ARMISD::VCGT; break; 3480 case ISD::SETOLE: 3481 case ISD::SETLE: Swap = true; // Fallthrough 3482 case ISD::SETOGE: 3483 case ISD::SETGE: Opc = ARMISD::VCGE; break; 3484 case ISD::SETUGE: Swap = true; // Fallthrough 3485 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 3486 case ISD::SETUGT: Swap = true; // Fallthrough 3487 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 3488 case ISD::SETUEQ: Invert = true; // Fallthrough 3489 case ISD::SETONE: 3490 // Expand this to (OLT | OGT). 3491 TmpOp0 = Op0; 3492 TmpOp1 = Op1; 3493 Opc = ISD::OR; 3494 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 3495 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1); 3496 break; 3497 case ISD::SETUO: Invert = true; // Fallthrough 3498 case ISD::SETO: 3499 // Expand this to (OLT | OGE). 3500 TmpOp0 = Op0; 3501 TmpOp1 = Op1; 3502 Opc = ISD::OR; 3503 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 3504 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1); 3505 break; 3506 } 3507 } else { 3508 // Integer comparisons. 3509 switch (SetCCOpcode) { 3510 default: llvm_unreachable("Illegal integer comparison"); break; 3511 case ISD::SETNE: Invert = true; 3512 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 3513 case ISD::SETLT: Swap = true; 3514 case ISD::SETGT: Opc = ARMISD::VCGT; break; 3515 case ISD::SETLE: Swap = true; 3516 case ISD::SETGE: Opc = ARMISD::VCGE; break; 3517 case ISD::SETULT: Swap = true; 3518 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 3519 case ISD::SETULE: Swap = true; 3520 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 3521 } 3522 3523 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 3524 if (Opc == ARMISD::VCEQ) { 3525 3526 SDValue AndOp; 3527 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 3528 AndOp = Op0; 3529 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 3530 AndOp = Op1; 3531 3532 // Ignore bitconvert. 3533 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 3534 AndOp = AndOp.getOperand(0); 3535 3536 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 3537 Opc = ARMISD::VTST; 3538 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0)); 3539 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1)); 3540 Invert = !Invert; 3541 } 3542 } 3543 } 3544 3545 if (Swap) 3546 std::swap(Op0, Op1); 3547 3548 // If one of the operands is a constant vector zero, attempt to fold the 3549 // comparison to a specialized compare-against-zero form. 3550 SDValue SingleOp; 3551 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 3552 SingleOp = Op0; 3553 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 3554 if (Opc == ARMISD::VCGE) 3555 Opc = ARMISD::VCLEZ; 3556 else if (Opc == ARMISD::VCGT) 3557 Opc = ARMISD::VCLTZ; 3558 SingleOp = Op1; 3559 } 3560 3561 SDValue Result; 3562 if (SingleOp.getNode()) { 3563 switch (Opc) { 3564 case ARMISD::VCEQ: 3565 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break; 3566 case ARMISD::VCGE: 3567 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break; 3568 case ARMISD::VCLEZ: 3569 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break; 3570 case ARMISD::VCGT: 3571 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break; 3572 case ARMISD::VCLTZ: 3573 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break; 3574 default: 3575 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 3576 } 3577 } else { 3578 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 3579 } 3580 3581 if (Invert) 3582 Result = DAG.getNOT(dl, Result, VT); 3583 3584 return Result; 3585 } 3586 3587 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 3588 /// valid vector constant for a NEON instruction with a "modified immediate" 3589 /// operand (e.g., VMOV). If so, return the encoded value. 3590 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 3591 unsigned SplatBitSize, SelectionDAG &DAG, 3592 EVT &VT, bool is128Bits, NEONModImmType type) { 3593 unsigned OpCmode, Imm; 3594 3595 // SplatBitSize is set to the smallest size that splats the vector, so a 3596 // zero vector will always have SplatBitSize == 8. However, NEON modified 3597 // immediate instructions others than VMOV do not support the 8-bit encoding 3598 // of a zero vector, and the default encoding of zero is supposed to be the 3599 // 32-bit version. 3600 if (SplatBits == 0) 3601 SplatBitSize = 32; 3602 3603 switch (SplatBitSize) { 3604 case 8: 3605 if (type != VMOVModImm) 3606 return SDValue(); 3607 // Any 1-byte value is OK. Op=0, Cmode=1110. 3608 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 3609 OpCmode = 0xe; 3610 Imm = SplatBits; 3611 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 3612 break; 3613 3614 case 16: 3615 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 3616 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 3617 if ((SplatBits & ~0xff) == 0) { 3618 // Value = 0x00nn: Op=x, Cmode=100x. 3619 OpCmode = 0x8; 3620 Imm = SplatBits; 3621 break; 3622 } 3623 if ((SplatBits & ~0xff00) == 0) { 3624 // Value = 0xnn00: Op=x, Cmode=101x. 3625 OpCmode = 0xa; 3626 Imm = SplatBits >> 8; 3627 break; 3628 } 3629 return SDValue(); 3630 3631 case 32: 3632 // NEON's 32-bit VMOV supports splat values where: 3633 // * only one byte is nonzero, or 3634 // * the least significant byte is 0xff and the second byte is nonzero, or 3635 // * the least significant 2 bytes are 0xff and the third is nonzero. 3636 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 3637 if ((SplatBits & ~0xff) == 0) { 3638 // Value = 0x000000nn: Op=x, Cmode=000x. 3639 OpCmode = 0; 3640 Imm = SplatBits; 3641 break; 3642 } 3643 if ((SplatBits & ~0xff00) == 0) { 3644 // Value = 0x0000nn00: Op=x, Cmode=001x. 3645 OpCmode = 0x2; 3646 Imm = SplatBits >> 8; 3647 break; 3648 } 3649 if ((SplatBits & ~0xff0000) == 0) { 3650 // Value = 0x00nn0000: Op=x, Cmode=010x. 3651 OpCmode = 0x4; 3652 Imm = SplatBits >> 16; 3653 break; 3654 } 3655 if ((SplatBits & ~0xff000000) == 0) { 3656 // Value = 0xnn000000: Op=x, Cmode=011x. 3657 OpCmode = 0x6; 3658 Imm = SplatBits >> 24; 3659 break; 3660 } 3661 3662 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 3663 if (type == OtherModImm) return SDValue(); 3664 3665 if ((SplatBits & ~0xffff) == 0 && 3666 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 3667 // Value = 0x0000nnff: Op=x, Cmode=1100. 3668 OpCmode = 0xc; 3669 Imm = SplatBits >> 8; 3670 SplatBits |= 0xff; 3671 break; 3672 } 3673 3674 if ((SplatBits & ~0xffffff) == 0 && 3675 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 3676 // Value = 0x00nnffff: Op=x, Cmode=1101. 3677 OpCmode = 0xd; 3678 Imm = SplatBits >> 16; 3679 SplatBits |= 0xffff; 3680 break; 3681 } 3682 3683 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 3684 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 3685 // VMOV.I32. A (very) minor optimization would be to replicate the value 3686 // and fall through here to test for a valid 64-bit splat. But, then the 3687 // caller would also need to check and handle the change in size. 3688 return SDValue(); 3689 3690 case 64: { 3691 if (type != VMOVModImm) 3692 return SDValue(); 3693 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 3694 uint64_t BitMask = 0xff; 3695 uint64_t Val = 0; 3696 unsigned ImmMask = 1; 3697 Imm = 0; 3698 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 3699 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 3700 Val |= BitMask; 3701 Imm |= ImmMask; 3702 } else if ((SplatBits & BitMask) != 0) { 3703 return SDValue(); 3704 } 3705 BitMask <<= 8; 3706 ImmMask <<= 1; 3707 } 3708 // Op=1, Cmode=1110. 3709 OpCmode = 0x1e; 3710 SplatBits = Val; 3711 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 3712 break; 3713 } 3714 3715 default: 3716 llvm_unreachable("unexpected size for isNEONModifiedImm"); 3717 return SDValue(); 3718 } 3719 3720 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 3721 return DAG.getTargetConstant(EncodedVal, MVT::i32); 3722 } 3723 3724 static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT, 3725 bool &ReverseVEXT, unsigned &Imm) { 3726 unsigned NumElts = VT.getVectorNumElements(); 3727 ReverseVEXT = false; 3728 3729 // Assume that the first shuffle index is not UNDEF. Fail if it is. 3730 if (M[0] < 0) 3731 return false; 3732 3733 Imm = M[0]; 3734 3735 // If this is a VEXT shuffle, the immediate value is the index of the first 3736 // element. The other shuffle indices must be the successive elements after 3737 // the first one. 3738 unsigned ExpectedElt = Imm; 3739 for (unsigned i = 1; i < NumElts; ++i) { 3740 // Increment the expected index. If it wraps around, it may still be 3741 // a VEXT but the source vectors must be swapped. 3742 ExpectedElt += 1; 3743 if (ExpectedElt == NumElts * 2) { 3744 ExpectedElt = 0; 3745 ReverseVEXT = true; 3746 } 3747 3748 if (M[i] < 0) continue; // ignore UNDEF indices 3749 if (ExpectedElt != static_cast<unsigned>(M[i])) 3750 return false; 3751 } 3752 3753 // Adjust the index value if the source operands will be swapped. 3754 if (ReverseVEXT) 3755 Imm -= NumElts; 3756 3757 return true; 3758 } 3759 3760 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 3761 /// instruction with the specified blocksize. (The order of the elements 3762 /// within each block of the vector is reversed.) 3763 static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT, 3764 unsigned BlockSize) { 3765 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 3766 "Only possible block sizes for VREV are: 16, 32, 64"); 3767 3768 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3769 if (EltSz == 64) 3770 return false; 3771 3772 unsigned NumElts = VT.getVectorNumElements(); 3773 unsigned BlockElts = M[0] + 1; 3774 // If the first shuffle index is UNDEF, be optimistic. 3775 if (M[0] < 0) 3776 BlockElts = BlockSize / EltSz; 3777 3778 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 3779 return false; 3780 3781 for (unsigned i = 0; i < NumElts; ++i) { 3782 if (M[i] < 0) continue; // ignore UNDEF indices 3783 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 3784 return false; 3785 } 3786 3787 return true; 3788 } 3789 3790 static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) { 3791 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 3792 // range, then 0 is placed into the resulting vector. So pretty much any mask 3793 // of 8 elements can work here. 3794 return VT == MVT::v8i8 && M.size() == 8; 3795 } 3796 3797 static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT, 3798 unsigned &WhichResult) { 3799 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3800 if (EltSz == 64) 3801 return false; 3802 3803 unsigned NumElts = VT.getVectorNumElements(); 3804 WhichResult = (M[0] == 0 ? 0 : 1); 3805 for (unsigned i = 0; i < NumElts; i += 2) { 3806 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 3807 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult)) 3808 return false; 3809 } 3810 return true; 3811 } 3812 3813 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 3814 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 3815 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 3816 static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT, 3817 unsigned &WhichResult) { 3818 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3819 if (EltSz == 64) 3820 return false; 3821 3822 unsigned NumElts = VT.getVectorNumElements(); 3823 WhichResult = (M[0] == 0 ? 0 : 1); 3824 for (unsigned i = 0; i < NumElts; i += 2) { 3825 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 3826 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult)) 3827 return false; 3828 } 3829 return true; 3830 } 3831 3832 static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT, 3833 unsigned &WhichResult) { 3834 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3835 if (EltSz == 64) 3836 return false; 3837 3838 unsigned NumElts = VT.getVectorNumElements(); 3839 WhichResult = (M[0] == 0 ? 0 : 1); 3840 for (unsigned i = 0; i != NumElts; ++i) { 3841 if (M[i] < 0) continue; // ignore UNDEF indices 3842 if ((unsigned) M[i] != 2 * i + WhichResult) 3843 return false; 3844 } 3845 3846 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 3847 if (VT.is64BitVector() && EltSz == 32) 3848 return false; 3849 3850 return true; 3851 } 3852 3853 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 3854 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 3855 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 3856 static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT, 3857 unsigned &WhichResult) { 3858 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3859 if (EltSz == 64) 3860 return false; 3861 3862 unsigned Half = VT.getVectorNumElements() / 2; 3863 WhichResult = (M[0] == 0 ? 0 : 1); 3864 for (unsigned j = 0; j != 2; ++j) { 3865 unsigned Idx = WhichResult; 3866 for (unsigned i = 0; i != Half; ++i) { 3867 int MIdx = M[i + j * Half]; 3868 if (MIdx >= 0 && (unsigned) MIdx != Idx) 3869 return false; 3870 Idx += 2; 3871 } 3872 } 3873 3874 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 3875 if (VT.is64BitVector() && EltSz == 32) 3876 return false; 3877 3878 return true; 3879 } 3880 3881 static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT, 3882 unsigned &WhichResult) { 3883 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3884 if (EltSz == 64) 3885 return false; 3886 3887 unsigned NumElts = VT.getVectorNumElements(); 3888 WhichResult = (M[0] == 0 ? 0 : 1); 3889 unsigned Idx = WhichResult * NumElts / 2; 3890 for (unsigned i = 0; i != NumElts; i += 2) { 3891 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 3892 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts)) 3893 return false; 3894 Idx += 1; 3895 } 3896 3897 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 3898 if (VT.is64BitVector() && EltSz == 32) 3899 return false; 3900 3901 return true; 3902 } 3903 3904 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 3905 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 3906 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 3907 static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT, 3908 unsigned &WhichResult) { 3909 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 3910 if (EltSz == 64) 3911 return false; 3912 3913 unsigned NumElts = VT.getVectorNumElements(); 3914 WhichResult = (M[0] == 0 ? 0 : 1); 3915 unsigned Idx = WhichResult * NumElts / 2; 3916 for (unsigned i = 0; i != NumElts; i += 2) { 3917 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 3918 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx)) 3919 return false; 3920 Idx += 1; 3921 } 3922 3923 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 3924 if (VT.is64BitVector() && EltSz == 32) 3925 return false; 3926 3927 return true; 3928 } 3929 3930 // If N is an integer constant that can be moved into a register in one 3931 // instruction, return an SDValue of such a constant (will become a MOV 3932 // instruction). Otherwise return null. 3933 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 3934 const ARMSubtarget *ST, DebugLoc dl) { 3935 uint64_t Val; 3936 if (!isa<ConstantSDNode>(N)) 3937 return SDValue(); 3938 Val = cast<ConstantSDNode>(N)->getZExtValue(); 3939 3940 if (ST->isThumb1Only()) { 3941 if (Val <= 255 || ~Val <= 255) 3942 return DAG.getConstant(Val, MVT::i32); 3943 } else { 3944 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 3945 return DAG.getConstant(Val, MVT::i32); 3946 } 3947 return SDValue(); 3948 } 3949 3950 // If this is a case we can't handle, return null and let the default 3951 // expansion code take care of it. 3952 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 3953 const ARMSubtarget *ST) const { 3954 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 3955 DebugLoc dl = Op.getDebugLoc(); 3956 EVT VT = Op.getValueType(); 3957 3958 APInt SplatBits, SplatUndef; 3959 unsigned SplatBitSize; 3960 bool HasAnyUndefs; 3961 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 3962 if (SplatBitSize <= 64) { 3963 // Check if an immediate VMOV works. 3964 EVT VmovVT; 3965 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 3966 SplatUndef.getZExtValue(), SplatBitSize, 3967 DAG, VmovVT, VT.is128BitVector(), 3968 VMOVModImm); 3969 if (Val.getNode()) { 3970 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 3971 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 3972 } 3973 3974 // Try an immediate VMVN. 3975 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 3976 Val = isNEONModifiedImm(NegatedImm, 3977 SplatUndef.getZExtValue(), SplatBitSize, 3978 DAG, VmovVT, VT.is128BitVector(), 3979 VMVNModImm); 3980 if (Val.getNode()) { 3981 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 3982 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 3983 } 3984 3985 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 3986 if (VT == MVT::v2f32 || VT == MVT::v4f32) { 3987 ConstantFPSDNode *C = cast<ConstantFPSDNode>(Op.getOperand(0)); 3988 int ImmVal = ARM_AM::getFP32Imm(C->getValueAPF()); 3989 if (ImmVal != -1) { 3990 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32); 3991 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 3992 } 3993 } 3994 } 3995 } 3996 3997 // Scan through the operands to see if only one value is used. 3998 unsigned NumElts = VT.getVectorNumElements(); 3999 bool isOnlyLowElement = true; 4000 bool usesOnlyOneValue = true; 4001 bool isConstant = true; 4002 SDValue Value; 4003 for (unsigned i = 0; i < NumElts; ++i) { 4004 SDValue V = Op.getOperand(i); 4005 if (V.getOpcode() == ISD::UNDEF) 4006 continue; 4007 if (i > 0) 4008 isOnlyLowElement = false; 4009 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 4010 isConstant = false; 4011 4012 if (!Value.getNode()) 4013 Value = V; 4014 else if (V != Value) 4015 usesOnlyOneValue = false; 4016 } 4017 4018 if (!Value.getNode()) 4019 return DAG.getUNDEF(VT); 4020 4021 if (isOnlyLowElement) 4022 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 4023 4024 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4025 4026 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 4027 // i32 and try again. 4028 if (usesOnlyOneValue && EltSize <= 32) { 4029 if (!isConstant) 4030 return DAG.getNode(ARMISD::VDUP, dl, VT, Value); 4031 if (VT.getVectorElementType().isFloatingPoint()) { 4032 SmallVector<SDValue, 8> Ops; 4033 for (unsigned i = 0; i < NumElts; ++i) 4034 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 4035 Op.getOperand(i))); 4036 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 4037 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts); 4038 Val = LowerBUILD_VECTOR(Val, DAG, ST); 4039 if (Val.getNode()) 4040 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4041 } 4042 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 4043 if (Val.getNode()) 4044 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 4045 } 4046 4047 // If all elements are constants and the case above didn't get hit, fall back 4048 // to the default expansion, which will generate a load from the constant 4049 // pool. 4050 if (isConstant) 4051 return SDValue(); 4052 4053 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 4054 if (NumElts >= 4) { 4055 SDValue shuffle = ReconstructShuffle(Op, DAG); 4056 if (shuffle != SDValue()) 4057 return shuffle; 4058 } 4059 4060 // Vectors with 32- or 64-bit elements can be built by directly assigning 4061 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 4062 // will be legalized. 4063 if (EltSize >= 32) { 4064 // Do the expansion with floating-point types, since that is what the VFP 4065 // registers are defined to use, and since i64 is not legal. 4066 EVT EltVT = EVT::getFloatingPointVT(EltSize); 4067 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 4068 SmallVector<SDValue, 8> Ops; 4069 for (unsigned i = 0; i < NumElts; ++i) 4070 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 4071 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts); 4072 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4073 } 4074 4075 return SDValue(); 4076 } 4077 4078 // Gather data to see if the operation can be modelled as a 4079 // shuffle in combination with VEXTs. 4080 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 4081 SelectionDAG &DAG) const { 4082 DebugLoc dl = Op.getDebugLoc(); 4083 EVT VT = Op.getValueType(); 4084 unsigned NumElts = VT.getVectorNumElements(); 4085 4086 SmallVector<SDValue, 2> SourceVecs; 4087 SmallVector<unsigned, 2> MinElts; 4088 SmallVector<unsigned, 2> MaxElts; 4089 4090 for (unsigned i = 0; i < NumElts; ++i) { 4091 SDValue V = Op.getOperand(i); 4092 if (V.getOpcode() == ISD::UNDEF) 4093 continue; 4094 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 4095 // A shuffle can only come from building a vector from various 4096 // elements of other vectors. 4097 return SDValue(); 4098 } else if (V.getOperand(0).getValueType().getVectorElementType() != 4099 VT.getVectorElementType()) { 4100 // This code doesn't know how to handle shuffles where the vector 4101 // element types do not match (this happens because type legalization 4102 // promotes the return type of EXTRACT_VECTOR_ELT). 4103 // FIXME: It might be appropriate to extend this code to handle 4104 // mismatched types. 4105 return SDValue(); 4106 } 4107 4108 // Record this extraction against the appropriate vector if possible... 4109 SDValue SourceVec = V.getOperand(0); 4110 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 4111 bool FoundSource = false; 4112 for (unsigned j = 0; j < SourceVecs.size(); ++j) { 4113 if (SourceVecs[j] == SourceVec) { 4114 if (MinElts[j] > EltNo) 4115 MinElts[j] = EltNo; 4116 if (MaxElts[j] < EltNo) 4117 MaxElts[j] = EltNo; 4118 FoundSource = true; 4119 break; 4120 } 4121 } 4122 4123 // Or record a new source if not... 4124 if (!FoundSource) { 4125 SourceVecs.push_back(SourceVec); 4126 MinElts.push_back(EltNo); 4127 MaxElts.push_back(EltNo); 4128 } 4129 } 4130 4131 // Currently only do something sane when at most two source vectors 4132 // involved. 4133 if (SourceVecs.size() > 2) 4134 return SDValue(); 4135 4136 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) }; 4137 int VEXTOffsets[2] = {0, 0}; 4138 4139 // This loop extracts the usage patterns of the source vectors 4140 // and prepares appropriate SDValues for a shuffle if possible. 4141 for (unsigned i = 0; i < SourceVecs.size(); ++i) { 4142 if (SourceVecs[i].getValueType() == VT) { 4143 // No VEXT necessary 4144 ShuffleSrcs[i] = SourceVecs[i]; 4145 VEXTOffsets[i] = 0; 4146 continue; 4147 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) { 4148 // It probably isn't worth padding out a smaller vector just to 4149 // break it down again in a shuffle. 4150 return SDValue(); 4151 } 4152 4153 // Since only 64-bit and 128-bit vectors are legal on ARM and 4154 // we've eliminated the other cases... 4155 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts && 4156 "unexpected vector sizes in ReconstructShuffle"); 4157 4158 if (MaxElts[i] - MinElts[i] >= NumElts) { 4159 // Span too large for a VEXT to cope 4160 return SDValue(); 4161 } 4162 4163 if (MinElts[i] >= NumElts) { 4164 // The extraction can just take the second half 4165 VEXTOffsets[i] = NumElts; 4166 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4167 SourceVecs[i], 4168 DAG.getIntPtrConstant(NumElts)); 4169 } else if (MaxElts[i] < NumElts) { 4170 // The extraction can just take the first half 4171 VEXTOffsets[i] = 0; 4172 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4173 SourceVecs[i], 4174 DAG.getIntPtrConstant(0)); 4175 } else { 4176 // An actual VEXT is needed 4177 VEXTOffsets[i] = MinElts[i]; 4178 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4179 SourceVecs[i], 4180 DAG.getIntPtrConstant(0)); 4181 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 4182 SourceVecs[i], 4183 DAG.getIntPtrConstant(NumElts)); 4184 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2, 4185 DAG.getConstant(VEXTOffsets[i], MVT::i32)); 4186 } 4187 } 4188 4189 SmallVector<int, 8> Mask; 4190 4191 for (unsigned i = 0; i < NumElts; ++i) { 4192 SDValue Entry = Op.getOperand(i); 4193 if (Entry.getOpcode() == ISD::UNDEF) { 4194 Mask.push_back(-1); 4195 continue; 4196 } 4197 4198 SDValue ExtractVec = Entry.getOperand(0); 4199 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i) 4200 .getOperand(1))->getSExtValue(); 4201 if (ExtractVec == SourceVecs[0]) { 4202 Mask.push_back(ExtractElt - VEXTOffsets[0]); 4203 } else { 4204 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]); 4205 } 4206 } 4207 4208 // Final check before we try to produce nonsense... 4209 if (isShuffleMaskLegal(Mask, VT)) 4210 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], 4211 &Mask[0]); 4212 4213 return SDValue(); 4214 } 4215 4216 /// isShuffleMaskLegal - Targets can use this to indicate that they only 4217 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 4218 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 4219 /// are assumed to be legal. 4220 bool 4221 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 4222 EVT VT) const { 4223 if (VT.getVectorNumElements() == 4 && 4224 (VT.is128BitVector() || VT.is64BitVector())) { 4225 unsigned PFIndexes[4]; 4226 for (unsigned i = 0; i != 4; ++i) { 4227 if (M[i] < 0) 4228 PFIndexes[i] = 8; 4229 else 4230 PFIndexes[i] = M[i]; 4231 } 4232 4233 // Compute the index in the perfect shuffle table. 4234 unsigned PFTableIndex = 4235 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 4236 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 4237 unsigned Cost = (PFEntry >> 30); 4238 4239 if (Cost <= 4) 4240 return true; 4241 } 4242 4243 bool ReverseVEXT; 4244 unsigned Imm, WhichResult; 4245 4246 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4247 return (EltSize >= 32 || 4248 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 4249 isVREVMask(M, VT, 64) || 4250 isVREVMask(M, VT, 32) || 4251 isVREVMask(M, VT, 16) || 4252 isVEXTMask(M, VT, ReverseVEXT, Imm) || 4253 isVTBLMask(M, VT) || 4254 isVTRNMask(M, VT, WhichResult) || 4255 isVUZPMask(M, VT, WhichResult) || 4256 isVZIPMask(M, VT, WhichResult) || 4257 isVTRN_v_undef_Mask(M, VT, WhichResult) || 4258 isVUZP_v_undef_Mask(M, VT, WhichResult) || 4259 isVZIP_v_undef_Mask(M, VT, WhichResult)); 4260 } 4261 4262 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 4263 /// the specified operations to build the shuffle. 4264 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 4265 SDValue RHS, SelectionDAG &DAG, 4266 DebugLoc dl) { 4267 unsigned OpNum = (PFEntry >> 26) & 0x0F; 4268 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 4269 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 4270 4271 enum { 4272 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 4273 OP_VREV, 4274 OP_VDUP0, 4275 OP_VDUP1, 4276 OP_VDUP2, 4277 OP_VDUP3, 4278 OP_VEXT1, 4279 OP_VEXT2, 4280 OP_VEXT3, 4281 OP_VUZPL, // VUZP, left result 4282 OP_VUZPR, // VUZP, right result 4283 OP_VZIPL, // VZIP, left result 4284 OP_VZIPR, // VZIP, right result 4285 OP_VTRNL, // VTRN, left result 4286 OP_VTRNR // VTRN, right result 4287 }; 4288 4289 if (OpNum == OP_COPY) { 4290 if (LHSID == (1*9+2)*9+3) return LHS; 4291 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 4292 return RHS; 4293 } 4294 4295 SDValue OpLHS, OpRHS; 4296 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 4297 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 4298 EVT VT = OpLHS.getValueType(); 4299 4300 switch (OpNum) { 4301 default: llvm_unreachable("Unknown shuffle opcode!"); 4302 case OP_VREV: 4303 // VREV divides the vector in half and swaps within the half. 4304 if (VT.getVectorElementType() == MVT::i32 || 4305 VT.getVectorElementType() == MVT::f32) 4306 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 4307 // vrev <4 x i16> -> VREV32 4308 if (VT.getVectorElementType() == MVT::i16) 4309 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 4310 // vrev <4 x i8> -> VREV16 4311 assert(VT.getVectorElementType() == MVT::i8); 4312 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 4313 case OP_VDUP0: 4314 case OP_VDUP1: 4315 case OP_VDUP2: 4316 case OP_VDUP3: 4317 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 4318 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32)); 4319 case OP_VEXT1: 4320 case OP_VEXT2: 4321 case OP_VEXT3: 4322 return DAG.getNode(ARMISD::VEXT, dl, VT, 4323 OpLHS, OpRHS, 4324 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32)); 4325 case OP_VUZPL: 4326 case OP_VUZPR: 4327 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 4328 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 4329 case OP_VZIPL: 4330 case OP_VZIPR: 4331 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 4332 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 4333 case OP_VTRNL: 4334 case OP_VTRNR: 4335 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 4336 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 4337 } 4338 } 4339 4340 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 4341 SmallVectorImpl<int> &ShuffleMask, 4342 SelectionDAG &DAG) { 4343 // Check to see if we can use the VTBL instruction. 4344 SDValue V1 = Op.getOperand(0); 4345 SDValue V2 = Op.getOperand(1); 4346 DebugLoc DL = Op.getDebugLoc(); 4347 4348 SmallVector<SDValue, 8> VTBLMask; 4349 for (SmallVectorImpl<int>::iterator 4350 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 4351 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32)); 4352 4353 if (V2.getNode()->getOpcode() == ISD::UNDEF) 4354 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 4355 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, 4356 &VTBLMask[0], 8)); 4357 4358 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 4359 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, 4360 &VTBLMask[0], 8)); 4361 } 4362 4363 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 4364 SDValue V1 = Op.getOperand(0); 4365 SDValue V2 = Op.getOperand(1); 4366 DebugLoc dl = Op.getDebugLoc(); 4367 EVT VT = Op.getValueType(); 4368 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 4369 SmallVector<int, 8> ShuffleMask; 4370 4371 // Convert shuffles that are directly supported on NEON to target-specific 4372 // DAG nodes, instead of keeping them as shuffles and matching them again 4373 // during code selection. This is more efficient and avoids the possibility 4374 // of inconsistencies between legalization and selection. 4375 // FIXME: floating-point vectors should be canonicalized to integer vectors 4376 // of the same time so that they get CSEd properly. 4377 SVN->getMask(ShuffleMask); 4378 4379 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4380 if (EltSize <= 32) { 4381 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 4382 int Lane = SVN->getSplatIndex(); 4383 // If this is undef splat, generate it via "just" vdup, if possible. 4384 if (Lane == -1) Lane = 0; 4385 4386 // Test if V1 is a SCALAR_TO_VECTOR. 4387 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 4388 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 4389 } 4390 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 4391 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 4392 // reaches it). 4393 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 4394 !isa<ConstantSDNode>(V1.getOperand(0))) { 4395 bool IsScalarToVector = true; 4396 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 4397 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) { 4398 IsScalarToVector = false; 4399 break; 4400 } 4401 if (IsScalarToVector) 4402 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 4403 } 4404 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 4405 DAG.getConstant(Lane, MVT::i32)); 4406 } 4407 4408 bool ReverseVEXT; 4409 unsigned Imm; 4410 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 4411 if (ReverseVEXT) 4412 std::swap(V1, V2); 4413 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 4414 DAG.getConstant(Imm, MVT::i32)); 4415 } 4416 4417 if (isVREVMask(ShuffleMask, VT, 64)) 4418 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 4419 if (isVREVMask(ShuffleMask, VT, 32)) 4420 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 4421 if (isVREVMask(ShuffleMask, VT, 16)) 4422 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 4423 4424 // Check for Neon shuffles that modify both input vectors in place. 4425 // If both results are used, i.e., if there are two shuffles with the same 4426 // source operands and with masks corresponding to both results of one of 4427 // these operations, DAG memoization will ensure that a single node is 4428 // used for both shuffles. 4429 unsigned WhichResult; 4430 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 4431 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 4432 V1, V2).getValue(WhichResult); 4433 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 4434 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 4435 V1, V2).getValue(WhichResult); 4436 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 4437 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 4438 V1, V2).getValue(WhichResult); 4439 4440 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 4441 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 4442 V1, V1).getValue(WhichResult); 4443 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 4444 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 4445 V1, V1).getValue(WhichResult); 4446 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 4447 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 4448 V1, V1).getValue(WhichResult); 4449 } 4450 4451 // If the shuffle is not directly supported and it has 4 elements, use 4452 // the PerfectShuffle-generated table to synthesize it from other shuffles. 4453 unsigned NumElts = VT.getVectorNumElements(); 4454 if (NumElts == 4) { 4455 unsigned PFIndexes[4]; 4456 for (unsigned i = 0; i != 4; ++i) { 4457 if (ShuffleMask[i] < 0) 4458 PFIndexes[i] = 8; 4459 else 4460 PFIndexes[i] = ShuffleMask[i]; 4461 } 4462 4463 // Compute the index in the perfect shuffle table. 4464 unsigned PFTableIndex = 4465 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 4466 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 4467 unsigned Cost = (PFEntry >> 30); 4468 4469 if (Cost <= 4) 4470 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 4471 } 4472 4473 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 4474 if (EltSize >= 32) { 4475 // Do the expansion with floating-point types, since that is what the VFP 4476 // registers are defined to use, and since i64 is not legal. 4477 EVT EltVT = EVT::getFloatingPointVT(EltSize); 4478 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 4479 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 4480 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 4481 SmallVector<SDValue, 8> Ops; 4482 for (unsigned i = 0; i < NumElts; ++i) { 4483 if (ShuffleMask[i] < 0) 4484 Ops.push_back(DAG.getUNDEF(EltVT)); 4485 else 4486 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 4487 ShuffleMask[i] < (int)NumElts ? V1 : V2, 4488 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 4489 MVT::i32))); 4490 } 4491 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts); 4492 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 4493 } 4494 4495 if (VT == MVT::v8i8) { 4496 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG); 4497 if (NewOp.getNode()) 4498 return NewOp; 4499 } 4500 4501 return SDValue(); 4502 } 4503 4504 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 4505 // INSERT_VECTOR_ELT is legal only for immediate indexes. 4506 SDValue Lane = Op.getOperand(2); 4507 if (!isa<ConstantSDNode>(Lane)) 4508 return SDValue(); 4509 4510 return Op; 4511 } 4512 4513 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 4514 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 4515 SDValue Lane = Op.getOperand(1); 4516 if (!isa<ConstantSDNode>(Lane)) 4517 return SDValue(); 4518 4519 SDValue Vec = Op.getOperand(0); 4520 if (Op.getValueType() == MVT::i32 && 4521 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) { 4522 DebugLoc dl = Op.getDebugLoc(); 4523 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 4524 } 4525 4526 return Op; 4527 } 4528 4529 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 4530 // The only time a CONCAT_VECTORS operation can have legal types is when 4531 // two 64-bit vectors are concatenated to a 128-bit vector. 4532 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 4533 "unexpected CONCAT_VECTORS"); 4534 DebugLoc dl = Op.getDebugLoc(); 4535 SDValue Val = DAG.getUNDEF(MVT::v2f64); 4536 SDValue Op0 = Op.getOperand(0); 4537 SDValue Op1 = Op.getOperand(1); 4538 if (Op0.getOpcode() != ISD::UNDEF) 4539 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 4540 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 4541 DAG.getIntPtrConstant(0)); 4542 if (Op1.getOpcode() != ISD::UNDEF) 4543 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 4544 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 4545 DAG.getIntPtrConstant(1)); 4546 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 4547 } 4548 4549 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 4550 /// element has been zero/sign-extended, depending on the isSigned parameter, 4551 /// from an integer type half its size. 4552 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 4553 bool isSigned) { 4554 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 4555 EVT VT = N->getValueType(0); 4556 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 4557 SDNode *BVN = N->getOperand(0).getNode(); 4558 if (BVN->getValueType(0) != MVT::v4i32 || 4559 BVN->getOpcode() != ISD::BUILD_VECTOR) 4560 return false; 4561 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 4562 unsigned HiElt = 1 - LoElt; 4563 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 4564 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 4565 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 4566 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 4567 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 4568 return false; 4569 if (isSigned) { 4570 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 4571 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 4572 return true; 4573 } else { 4574 if (Hi0->isNullValue() && Hi1->isNullValue()) 4575 return true; 4576 } 4577 return false; 4578 } 4579 4580 if (N->getOpcode() != ISD::BUILD_VECTOR) 4581 return false; 4582 4583 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 4584 SDNode *Elt = N->getOperand(i).getNode(); 4585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 4586 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 4587 unsigned HalfSize = EltSize / 2; 4588 if (isSigned) { 4589 if (!isIntN(HalfSize, C->getSExtValue())) 4590 return false; 4591 } else { 4592 if (!isUIntN(HalfSize, C->getZExtValue())) 4593 return false; 4594 } 4595 continue; 4596 } 4597 return false; 4598 } 4599 4600 return true; 4601 } 4602 4603 /// isSignExtended - Check if a node is a vector value that is sign-extended 4604 /// or a constant BUILD_VECTOR with sign-extended elements. 4605 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 4606 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 4607 return true; 4608 if (isExtendedBUILD_VECTOR(N, DAG, true)) 4609 return true; 4610 return false; 4611 } 4612 4613 /// isZeroExtended - Check if a node is a vector value that is zero-extended 4614 /// or a constant BUILD_VECTOR with zero-extended elements. 4615 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 4616 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 4617 return true; 4618 if (isExtendedBUILD_VECTOR(N, DAG, false)) 4619 return true; 4620 return false; 4621 } 4622 4623 /// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending 4624 /// load, or BUILD_VECTOR with extended elements, return the unextended value. 4625 static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) { 4626 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 4627 return N->getOperand(0); 4628 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) 4629 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(), 4630 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(), 4631 LD->isNonTemporal(), LD->isInvariant(), 4632 LD->getAlignment()); 4633 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 4634 // have been legalized as a BITCAST from v4i32. 4635 if (N->getOpcode() == ISD::BITCAST) { 4636 SDNode *BVN = N->getOperand(0).getNode(); 4637 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 4638 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 4639 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 4640 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32, 4641 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2)); 4642 } 4643 // Construct a new BUILD_VECTOR with elements truncated to half the size. 4644 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 4645 EVT VT = N->getValueType(0); 4646 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 4647 unsigned NumElts = VT.getVectorNumElements(); 4648 MVT TruncVT = MVT::getIntegerVT(EltSize); 4649 SmallVector<SDValue, 8> Ops; 4650 for (unsigned i = 0; i != NumElts; ++i) { 4651 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 4652 const APInt &CInt = C->getAPIntValue(); 4653 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT)); 4654 } 4655 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), 4656 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts); 4657 } 4658 4659 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 4660 unsigned Opcode = N->getOpcode(); 4661 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 4662 SDNode *N0 = N->getOperand(0).getNode(); 4663 SDNode *N1 = N->getOperand(1).getNode(); 4664 return N0->hasOneUse() && N1->hasOneUse() && 4665 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 4666 } 4667 return false; 4668 } 4669 4670 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 4671 unsigned Opcode = N->getOpcode(); 4672 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 4673 SDNode *N0 = N->getOperand(0).getNode(); 4674 SDNode *N1 = N->getOperand(1).getNode(); 4675 return N0->hasOneUse() && N1->hasOneUse() && 4676 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 4677 } 4678 return false; 4679 } 4680 4681 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 4682 // Multiplications are only custom-lowered for 128-bit vectors so that 4683 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 4684 EVT VT = Op.getValueType(); 4685 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL"); 4686 SDNode *N0 = Op.getOperand(0).getNode(); 4687 SDNode *N1 = Op.getOperand(1).getNode(); 4688 unsigned NewOpc = 0; 4689 bool isMLA = false; 4690 bool isN0SExt = isSignExtended(N0, DAG); 4691 bool isN1SExt = isSignExtended(N1, DAG); 4692 if (isN0SExt && isN1SExt) 4693 NewOpc = ARMISD::VMULLs; 4694 else { 4695 bool isN0ZExt = isZeroExtended(N0, DAG); 4696 bool isN1ZExt = isZeroExtended(N1, DAG); 4697 if (isN0ZExt && isN1ZExt) 4698 NewOpc = ARMISD::VMULLu; 4699 else if (isN1SExt || isN1ZExt) { 4700 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 4701 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 4702 if (isN1SExt && isAddSubSExt(N0, DAG)) { 4703 NewOpc = ARMISD::VMULLs; 4704 isMLA = true; 4705 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 4706 NewOpc = ARMISD::VMULLu; 4707 isMLA = true; 4708 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 4709 std::swap(N0, N1); 4710 NewOpc = ARMISD::VMULLu; 4711 isMLA = true; 4712 } 4713 } 4714 4715 if (!NewOpc) { 4716 if (VT == MVT::v2i64) 4717 // Fall through to expand this. It is not legal. 4718 return SDValue(); 4719 else 4720 // Other vector multiplications are legal. 4721 return Op; 4722 } 4723 } 4724 4725 // Legalize to a VMULL instruction. 4726 DebugLoc DL = Op.getDebugLoc(); 4727 SDValue Op0; 4728 SDValue Op1 = SkipExtension(N1, DAG); 4729 if (!isMLA) { 4730 Op0 = SkipExtension(N0, DAG); 4731 assert(Op0.getValueType().is64BitVector() && 4732 Op1.getValueType().is64BitVector() && 4733 "unexpected types for extended operands to VMULL"); 4734 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 4735 } 4736 4737 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 4738 // isel lowering to take advantage of no-stall back to back vmul + vmla. 4739 // vmull q0, d4, d6 4740 // vmlal q0, d5, d6 4741 // is faster than 4742 // vaddl q0, d4, d5 4743 // vmovl q1, d6 4744 // vmul q0, q0, q1 4745 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG); 4746 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG); 4747 EVT Op1VT = Op1.getValueType(); 4748 return DAG.getNode(N0->getOpcode(), DL, VT, 4749 DAG.getNode(NewOpc, DL, VT, 4750 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 4751 DAG.getNode(NewOpc, DL, VT, 4752 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 4753 } 4754 4755 static SDValue 4756 LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) { 4757 // Convert to float 4758 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 4759 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 4760 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 4761 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 4762 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 4763 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 4764 // Get reciprocal estimate. 4765 // float4 recip = vrecpeq_f32(yf); 4766 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4767 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y); 4768 // Because char has a smaller range than uchar, we can actually get away 4769 // without any newton steps. This requires that we use a weird bias 4770 // of 0xb000, however (again, this has been exhaustively tested). 4771 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 4772 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 4773 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 4774 Y = DAG.getConstant(0xb000, MVT::i32); 4775 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y); 4776 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 4777 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 4778 // Convert back to short. 4779 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 4780 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 4781 return X; 4782 } 4783 4784 static SDValue 4785 LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) { 4786 SDValue N2; 4787 // Convert to float. 4788 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 4789 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 4790 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 4791 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 4792 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 4793 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 4794 4795 // Use reciprocal estimate and one refinement step. 4796 // float4 recip = vrecpeq_f32(yf); 4797 // recip *= vrecpsq_f32(yf, recip); 4798 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4799 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1); 4800 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4801 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 4802 N1, N2); 4803 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 4804 // Because short has a smaller range than ushort, we can actually get away 4805 // with only a single newton step. This requires that we use a weird bias 4806 // of 89, however (again, this has been exhaustively tested). 4807 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 4808 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 4809 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 4810 N1 = DAG.getConstant(0x89, MVT::i32); 4811 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 4812 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 4813 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 4814 // Convert back to integer and return. 4815 // return vmovn_s32(vcvt_s32_f32(result)); 4816 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 4817 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 4818 return N0; 4819 } 4820 4821 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 4822 EVT VT = Op.getValueType(); 4823 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 4824 "unexpected type for custom-lowering ISD::SDIV"); 4825 4826 DebugLoc dl = Op.getDebugLoc(); 4827 SDValue N0 = Op.getOperand(0); 4828 SDValue N1 = Op.getOperand(1); 4829 SDValue N2, N3; 4830 4831 if (VT == MVT::v8i8) { 4832 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 4833 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 4834 4835 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 4836 DAG.getIntPtrConstant(4)); 4837 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 4838 DAG.getIntPtrConstant(4)); 4839 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 4840 DAG.getIntPtrConstant(0)); 4841 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 4842 DAG.getIntPtrConstant(0)); 4843 4844 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 4845 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 4846 4847 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 4848 N0 = LowerCONCAT_VECTORS(N0, DAG); 4849 4850 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 4851 return N0; 4852 } 4853 return LowerSDIV_v4i16(N0, N1, dl, DAG); 4854 } 4855 4856 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 4857 EVT VT = Op.getValueType(); 4858 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 4859 "unexpected type for custom-lowering ISD::UDIV"); 4860 4861 DebugLoc dl = Op.getDebugLoc(); 4862 SDValue N0 = Op.getOperand(0); 4863 SDValue N1 = Op.getOperand(1); 4864 SDValue N2, N3; 4865 4866 if (VT == MVT::v8i8) { 4867 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 4868 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 4869 4870 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 4871 DAG.getIntPtrConstant(4)); 4872 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 4873 DAG.getIntPtrConstant(4)); 4874 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 4875 DAG.getIntPtrConstant(0)); 4876 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 4877 DAG.getIntPtrConstant(0)); 4878 4879 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 4880 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 4881 4882 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 4883 N0 = LowerCONCAT_VECTORS(N0, DAG); 4884 4885 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 4886 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32), 4887 N0); 4888 return N0; 4889 } 4890 4891 // v4i16 sdiv ... Convert to float. 4892 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 4893 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 4894 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 4895 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 4896 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 4897 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 4898 4899 // Use reciprocal estimate and two refinement steps. 4900 // float4 recip = vrecpeq_f32(yf); 4901 // recip *= vrecpsq_f32(yf, recip); 4902 // recip *= vrecpsq_f32(yf, recip); 4903 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4904 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1); 4905 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4906 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 4907 BN1, N2); 4908 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 4909 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 4910 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 4911 BN1, N2); 4912 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 4913 // Simply multiplying by the reciprocal estimate can leave us a few ulps 4914 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 4915 // and that it will never cause us to return an answer too large). 4916 // float4 result = as_float4(as_int4(xf*recip) + 2); 4917 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 4918 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 4919 N1 = DAG.getConstant(2, MVT::i32); 4920 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 4921 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 4922 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 4923 // Convert back to integer and return. 4924 // return vmovn_u32(vcvt_s32_f32(result)); 4925 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 4926 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 4927 return N0; 4928 } 4929 4930 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 4931 EVT VT = Op.getNode()->getValueType(0); 4932 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 4933 4934 unsigned Opc; 4935 bool ExtraOp = false; 4936 switch (Op.getOpcode()) { 4937 default: assert(0 && "Invalid code"); 4938 case ISD::ADDC: Opc = ARMISD::ADDC; break; 4939 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 4940 case ISD::SUBC: Opc = ARMISD::SUBC; break; 4941 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 4942 } 4943 4944 if (!ExtraOp) 4945 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0), 4946 Op.getOperand(1)); 4947 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0), 4948 Op.getOperand(1), Op.getOperand(2)); 4949 } 4950 4951 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 4952 // Monotonic load/store is legal for all targets 4953 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic) 4954 return Op; 4955 4956 // Aquire/Release load/store is not legal for targets without a 4957 // dmb or equivalent available. 4958 return SDValue(); 4959 } 4960 4961 4962 static void 4963 ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results, 4964 SelectionDAG &DAG, unsigned NewOp) { 4965 DebugLoc dl = Node->getDebugLoc(); 4966 assert (Node->getValueType(0) == MVT::i64 && 4967 "Only know how to expand i64 atomics"); 4968 4969 SmallVector<SDValue, 6> Ops; 4970 Ops.push_back(Node->getOperand(0)); // Chain 4971 Ops.push_back(Node->getOperand(1)); // Ptr 4972 // Low part of Val1 4973 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4974 Node->getOperand(2), DAG.getIntPtrConstant(0))); 4975 // High part of Val1 4976 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4977 Node->getOperand(2), DAG.getIntPtrConstant(1))); 4978 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) { 4979 // High part of Val1 4980 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4981 Node->getOperand(3), DAG.getIntPtrConstant(0))); 4982 // High part of Val2 4983 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4984 Node->getOperand(3), DAG.getIntPtrConstant(1))); 4985 } 4986 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 4987 SDValue Result = 4988 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64, 4989 cast<MemSDNode>(Node)->getMemOperand()); 4990 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) }; 4991 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); 4992 Results.push_back(Result.getValue(2)); 4993 } 4994 4995 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 4996 switch (Op.getOpcode()) { 4997 default: llvm_unreachable("Don't know how to custom lower this!"); 4998 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 4999 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 5000 case ISD::GlobalAddress: 5001 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) : 5002 LowerGlobalAddressELF(Op, DAG); 5003 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 5004 case ISD::SELECT: return LowerSELECT(Op, DAG); 5005 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 5006 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 5007 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 5008 case ISD::VASTART: return LowerVASTART(Op, DAG); 5009 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget); 5010 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 5011 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 5012 case ISD::SINT_TO_FP: 5013 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 5014 case ISD::FP_TO_SINT: 5015 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 5016 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 5017 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 5018 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 5019 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG); 5020 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 5021 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 5022 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 5023 Subtarget); 5024 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 5025 case ISD::SHL: 5026 case ISD::SRL: 5027 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 5028 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 5029 case ISD::SRL_PARTS: 5030 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 5031 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 5032 case ISD::SETCC: return LowerVSETCC(Op, DAG); 5033 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 5034 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 5035 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 5036 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 5037 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 5038 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 5039 case ISD::MUL: return LowerMUL(Op, DAG); 5040 case ISD::SDIV: return LowerSDIV(Op, DAG); 5041 case ISD::UDIV: return LowerUDIV(Op, DAG); 5042 case ISD::ADDC: 5043 case ISD::ADDE: 5044 case ISD::SUBC: 5045 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 5046 case ISD::ATOMIC_LOAD: 5047 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 5048 } 5049 return SDValue(); 5050 } 5051 5052 /// ReplaceNodeResults - Replace the results of node with an illegal result 5053 /// type with new values built out of custom code. 5054 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 5055 SmallVectorImpl<SDValue>&Results, 5056 SelectionDAG &DAG) const { 5057 SDValue Res; 5058 switch (N->getOpcode()) { 5059 default: 5060 llvm_unreachable("Don't know how to custom expand this!"); 5061 break; 5062 case ISD::BITCAST: 5063 Res = ExpandBITCAST(N, DAG); 5064 break; 5065 case ISD::SRL: 5066 case ISD::SRA: 5067 Res = Expand64BitShift(N, DAG, Subtarget); 5068 break; 5069 case ISD::ATOMIC_LOAD_ADD: 5070 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG); 5071 return; 5072 case ISD::ATOMIC_LOAD_AND: 5073 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG); 5074 return; 5075 case ISD::ATOMIC_LOAD_NAND: 5076 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG); 5077 return; 5078 case ISD::ATOMIC_LOAD_OR: 5079 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG); 5080 return; 5081 case ISD::ATOMIC_LOAD_SUB: 5082 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG); 5083 return; 5084 case ISD::ATOMIC_LOAD_XOR: 5085 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG); 5086 return; 5087 case ISD::ATOMIC_SWAP: 5088 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG); 5089 return; 5090 case ISD::ATOMIC_CMP_SWAP: 5091 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG); 5092 return; 5093 } 5094 if (Res.getNode()) 5095 Results.push_back(Res); 5096 } 5097 5098 //===----------------------------------------------------------------------===// 5099 // ARM Scheduler Hooks 5100 //===----------------------------------------------------------------------===// 5101 5102 MachineBasicBlock * 5103 ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI, 5104 MachineBasicBlock *BB, 5105 unsigned Size) const { 5106 unsigned dest = MI->getOperand(0).getReg(); 5107 unsigned ptr = MI->getOperand(1).getReg(); 5108 unsigned oldval = MI->getOperand(2).getReg(); 5109 unsigned newval = MI->getOperand(3).getReg(); 5110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5111 DebugLoc dl = MI->getDebugLoc(); 5112 bool isThumb2 = Subtarget->isThumb2(); 5113 5114 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5115 unsigned scratch = 5116 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass 5117 : ARM::GPRRegisterClass); 5118 5119 if (isThumb2) { 5120 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass); 5121 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass); 5122 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass); 5123 } 5124 5125 unsigned ldrOpc, strOpc; 5126 switch (Size) { 5127 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 5128 case 1: 5129 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 5130 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 5131 break; 5132 case 2: 5133 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 5134 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 5135 break; 5136 case 4: 5137 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 5138 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 5139 break; 5140 } 5141 5142 MachineFunction *MF = BB->getParent(); 5143 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5144 MachineFunction::iterator It = BB; 5145 ++It; // insert the new blocks after the current block 5146 5147 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); 5148 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); 5149 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5150 MF->insert(It, loop1MBB); 5151 MF->insert(It, loop2MBB); 5152 MF->insert(It, exitMBB); 5153 5154 // Transfer the remainder of BB and its successor edges to exitMBB. 5155 exitMBB->splice(exitMBB->begin(), BB, 5156 llvm::next(MachineBasicBlock::iterator(MI)), 5157 BB->end()); 5158 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5159 5160 // thisMBB: 5161 // ... 5162 // fallthrough --> loop1MBB 5163 BB->addSuccessor(loop1MBB); 5164 5165 // loop1MBB: 5166 // ldrex dest, [ptr] 5167 // cmp dest, oldval 5168 // bne exitMBB 5169 BB = loop1MBB; 5170 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 5171 if (ldrOpc == ARM::t2LDREX) 5172 MIB.addImm(0); 5173 AddDefaultPred(MIB); 5174 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 5175 .addReg(dest).addReg(oldval)); 5176 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5177 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5178 BB->addSuccessor(loop2MBB); 5179 BB->addSuccessor(exitMBB); 5180 5181 // loop2MBB: 5182 // strex scratch, newval, [ptr] 5183 // cmp scratch, #0 5184 // bne loop1MBB 5185 BB = loop2MBB; 5186 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr); 5187 if (strOpc == ARM::t2STREX) 5188 MIB.addImm(0); 5189 AddDefaultPred(MIB); 5190 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5191 .addReg(scratch).addImm(0)); 5192 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5193 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5194 BB->addSuccessor(loop1MBB); 5195 BB->addSuccessor(exitMBB); 5196 5197 // exitMBB: 5198 // ... 5199 BB = exitMBB; 5200 5201 MI->eraseFromParent(); // The instruction is gone now. 5202 5203 return BB; 5204 } 5205 5206 MachineBasicBlock * 5207 ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, 5208 unsigned Size, unsigned BinOpcode) const { 5209 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 5210 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5211 5212 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5213 MachineFunction *MF = BB->getParent(); 5214 MachineFunction::iterator It = BB; 5215 ++It; 5216 5217 unsigned dest = MI->getOperand(0).getReg(); 5218 unsigned ptr = MI->getOperand(1).getReg(); 5219 unsigned incr = MI->getOperand(2).getReg(); 5220 DebugLoc dl = MI->getDebugLoc(); 5221 bool isThumb2 = Subtarget->isThumb2(); 5222 5223 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5224 if (isThumb2) { 5225 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass); 5226 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass); 5227 } 5228 5229 unsigned ldrOpc, strOpc; 5230 switch (Size) { 5231 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 5232 case 1: 5233 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 5234 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 5235 break; 5236 case 2: 5237 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 5238 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 5239 break; 5240 case 4: 5241 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 5242 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 5243 break; 5244 } 5245 5246 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5247 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5248 MF->insert(It, loopMBB); 5249 MF->insert(It, exitMBB); 5250 5251 // Transfer the remainder of BB and its successor edges to exitMBB. 5252 exitMBB->splice(exitMBB->begin(), BB, 5253 llvm::next(MachineBasicBlock::iterator(MI)), 5254 BB->end()); 5255 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5256 5257 TargetRegisterClass *TRC = 5258 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass; 5259 unsigned scratch = MRI.createVirtualRegister(TRC); 5260 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC); 5261 5262 // thisMBB: 5263 // ... 5264 // fallthrough --> loopMBB 5265 BB->addSuccessor(loopMBB); 5266 5267 // loopMBB: 5268 // ldrex dest, ptr 5269 // <binop> scratch2, dest, incr 5270 // strex scratch, scratch2, ptr 5271 // cmp scratch, #0 5272 // bne- loopMBB 5273 // fallthrough --> exitMBB 5274 BB = loopMBB; 5275 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 5276 if (ldrOpc == ARM::t2LDREX) 5277 MIB.addImm(0); 5278 AddDefaultPred(MIB); 5279 if (BinOpcode) { 5280 // operand order needs to go the other way for NAND 5281 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr) 5282 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2). 5283 addReg(incr).addReg(dest)).addReg(0); 5284 else 5285 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2). 5286 addReg(dest).addReg(incr)).addReg(0); 5287 } 5288 5289 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr); 5290 if (strOpc == ARM::t2STREX) 5291 MIB.addImm(0); 5292 AddDefaultPred(MIB); 5293 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5294 .addReg(scratch).addImm(0)); 5295 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5296 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5297 5298 BB->addSuccessor(loopMBB); 5299 BB->addSuccessor(exitMBB); 5300 5301 // exitMBB: 5302 // ... 5303 BB = exitMBB; 5304 5305 MI->eraseFromParent(); // The instruction is gone now. 5306 5307 return BB; 5308 } 5309 5310 MachineBasicBlock * 5311 ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI, 5312 MachineBasicBlock *BB, 5313 unsigned Size, 5314 bool signExtend, 5315 ARMCC::CondCodes Cond) const { 5316 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5317 5318 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5319 MachineFunction *MF = BB->getParent(); 5320 MachineFunction::iterator It = BB; 5321 ++It; 5322 5323 unsigned dest = MI->getOperand(0).getReg(); 5324 unsigned ptr = MI->getOperand(1).getReg(); 5325 unsigned incr = MI->getOperand(2).getReg(); 5326 unsigned oldval = dest; 5327 DebugLoc dl = MI->getDebugLoc(); 5328 bool isThumb2 = Subtarget->isThumb2(); 5329 5330 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5331 if (isThumb2) { 5332 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass); 5333 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass); 5334 } 5335 5336 unsigned ldrOpc, strOpc, extendOpc; 5337 switch (Size) { 5338 default: llvm_unreachable("unsupported size for AtomicCmpSwap!"); 5339 case 1: 5340 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB; 5341 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB; 5342 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB; 5343 break; 5344 case 2: 5345 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH; 5346 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH; 5347 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH; 5348 break; 5349 case 4: 5350 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX; 5351 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX; 5352 extendOpc = 0; 5353 break; 5354 } 5355 5356 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5357 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5358 MF->insert(It, loopMBB); 5359 MF->insert(It, exitMBB); 5360 5361 // Transfer the remainder of BB and its successor edges to exitMBB. 5362 exitMBB->splice(exitMBB->begin(), BB, 5363 llvm::next(MachineBasicBlock::iterator(MI)), 5364 BB->end()); 5365 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5366 5367 TargetRegisterClass *TRC = 5368 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass; 5369 unsigned scratch = MRI.createVirtualRegister(TRC); 5370 unsigned scratch2 = MRI.createVirtualRegister(TRC); 5371 5372 // thisMBB: 5373 // ... 5374 // fallthrough --> loopMBB 5375 BB->addSuccessor(loopMBB); 5376 5377 // loopMBB: 5378 // ldrex dest, ptr 5379 // (sign extend dest, if required) 5380 // cmp dest, incr 5381 // cmov.cond scratch2, dest, incr 5382 // strex scratch, scratch2, ptr 5383 // cmp scratch, #0 5384 // bne- loopMBB 5385 // fallthrough --> exitMBB 5386 BB = loopMBB; 5387 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr); 5388 if (ldrOpc == ARM::t2LDREX) 5389 MIB.addImm(0); 5390 AddDefaultPred(MIB); 5391 5392 // Sign extend the value, if necessary. 5393 if (signExtend && extendOpc) { 5394 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass); 5395 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval) 5396 .addReg(dest) 5397 .addImm(0)); 5398 } 5399 5400 // Build compare and cmov instructions. 5401 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 5402 .addReg(oldval).addReg(incr)); 5403 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2) 5404 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR); 5405 5406 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr); 5407 if (strOpc == ARM::t2STREX) 5408 MIB.addImm(0); 5409 AddDefaultPred(MIB); 5410 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5411 .addReg(scratch).addImm(0)); 5412 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5413 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5414 5415 BB->addSuccessor(loopMBB); 5416 BB->addSuccessor(exitMBB); 5417 5418 // exitMBB: 5419 // ... 5420 BB = exitMBB; 5421 5422 MI->eraseFromParent(); // The instruction is gone now. 5423 5424 return BB; 5425 } 5426 5427 MachineBasicBlock * 5428 ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB, 5429 unsigned Op1, unsigned Op2, 5430 bool NeedsCarry, bool IsCmpxchg) const { 5431 // This also handles ATOMIC_SWAP, indicated by Op1==0. 5432 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5433 5434 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 5435 MachineFunction *MF = BB->getParent(); 5436 MachineFunction::iterator It = BB; 5437 ++It; 5438 5439 unsigned destlo = MI->getOperand(0).getReg(); 5440 unsigned desthi = MI->getOperand(1).getReg(); 5441 unsigned ptr = MI->getOperand(2).getReg(); 5442 unsigned vallo = MI->getOperand(3).getReg(); 5443 unsigned valhi = MI->getOperand(4).getReg(); 5444 DebugLoc dl = MI->getDebugLoc(); 5445 bool isThumb2 = Subtarget->isThumb2(); 5446 5447 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 5448 if (isThumb2) { 5449 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass); 5450 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass); 5451 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass); 5452 } 5453 5454 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD; 5455 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD; 5456 5457 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5458 MachineBasicBlock *contBB = 0, *cont2BB = 0; 5459 if (IsCmpxchg) { 5460 contBB = MF->CreateMachineBasicBlock(LLVM_BB); 5461 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB); 5462 } 5463 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 5464 MF->insert(It, loopMBB); 5465 if (IsCmpxchg) { 5466 MF->insert(It, contBB); 5467 MF->insert(It, cont2BB); 5468 } 5469 MF->insert(It, exitMBB); 5470 5471 // Transfer the remainder of BB and its successor edges to exitMBB. 5472 exitMBB->splice(exitMBB->begin(), BB, 5473 llvm::next(MachineBasicBlock::iterator(MI)), 5474 BB->end()); 5475 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 5476 5477 TargetRegisterClass *TRC = 5478 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass; 5479 unsigned storesuccess = MRI.createVirtualRegister(TRC); 5480 5481 // thisMBB: 5482 // ... 5483 // fallthrough --> loopMBB 5484 BB->addSuccessor(loopMBB); 5485 5486 // loopMBB: 5487 // ldrexd r2, r3, ptr 5488 // <binopa> r0, r2, incr 5489 // <binopb> r1, r3, incr 5490 // strexd storesuccess, r0, r1, ptr 5491 // cmp storesuccess, #0 5492 // bne- loopMBB 5493 // fallthrough --> exitMBB 5494 // 5495 // Note that the registers are explicitly specified because there is not any 5496 // way to force the register allocator to allocate a register pair. 5497 // 5498 // FIXME: The hardcoded registers are not necessary for Thumb2, but we 5499 // need to properly enforce the restriction that the two output registers 5500 // for ldrexd must be different. 5501 BB = loopMBB; 5502 // Load 5503 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc)) 5504 .addReg(ARM::R2, RegState::Define) 5505 .addReg(ARM::R3, RegState::Define).addReg(ptr)); 5506 // Copy r2/r3 into dest. (This copy will normally be coalesced.) 5507 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2); 5508 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3); 5509 5510 if (IsCmpxchg) { 5511 // Add early exit 5512 for (unsigned i = 0; i < 2; i++) { 5513 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : 5514 ARM::CMPrr)) 5515 .addReg(i == 0 ? destlo : desthi) 5516 .addReg(i == 0 ? vallo : valhi)); 5517 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5518 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5519 BB->addSuccessor(exitMBB); 5520 BB->addSuccessor(i == 0 ? contBB : cont2BB); 5521 BB = (i == 0 ? contBB : cont2BB); 5522 } 5523 5524 // Copy to physregs for strexd 5525 unsigned setlo = MI->getOperand(5).getReg(); 5526 unsigned sethi = MI->getOperand(6).getReg(); 5527 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo); 5528 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi); 5529 } else if (Op1) { 5530 // Perform binary operation 5531 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0) 5532 .addReg(destlo).addReg(vallo)) 5533 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry)); 5534 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1) 5535 .addReg(desthi).addReg(valhi)).addReg(0); 5536 } else { 5537 // Copy to physregs for strexd 5538 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo); 5539 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi); 5540 } 5541 5542 // Store 5543 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess) 5544 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr)); 5545 // Cmp+jump 5546 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 5547 .addReg(storesuccess).addImm(0)); 5548 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 5549 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 5550 5551 BB->addSuccessor(loopMBB); 5552 BB->addSuccessor(exitMBB); 5553 5554 // exitMBB: 5555 // ... 5556 BB = exitMBB; 5557 5558 MI->eraseFromParent(); // The instruction is gone now. 5559 5560 return BB; 5561 } 5562 5563 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 5564 /// registers the function context. 5565 void ARMTargetLowering:: 5566 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB, 5567 MachineBasicBlock *DispatchBB, int FI) const { 5568 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5569 DebugLoc dl = MI->getDebugLoc(); 5570 MachineFunction *MF = MBB->getParent(); 5571 MachineRegisterInfo *MRI = &MF->getRegInfo(); 5572 MachineConstantPool *MCP = MF->getConstantPool(); 5573 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 5574 const Function *F = MF->getFunction(); 5575 5576 bool isThumb = Subtarget->isThumb(); 5577 bool isThumb2 = Subtarget->isThumb2(); 5578 5579 unsigned PCLabelId = AFI->createPICLabelUId(); 5580 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 5581 ARMConstantPoolValue *CPV = 5582 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj); 5583 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 5584 5585 const TargetRegisterClass *TRC = 5586 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass; 5587 5588 // Grab constant pool and fixed stack memory operands. 5589 MachineMemOperand *CPMMO = 5590 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(), 5591 MachineMemOperand::MOLoad, 4, 4); 5592 5593 MachineMemOperand *FIMMOSt = 5594 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 5595 MachineMemOperand::MOStore, 4, 4); 5596 5597 // Load the address of the dispatch MBB into the jump buffer. 5598 if (isThumb2) { 5599 // Incoming value: jbuf 5600 // ldr.n r5, LCPI1_1 5601 // orr r5, r5, #1 5602 // add r5, pc 5603 // str r5, [$jbuf, #+4] ; &jbuf[1] 5604 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5605 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 5606 .addConstantPoolIndex(CPI) 5607 .addMemOperand(CPMMO)); 5608 // Set the low bit because of thumb mode. 5609 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5610 AddDefaultCC( 5611 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 5612 .addReg(NewVReg1, RegState::Kill) 5613 .addImm(0x01))); 5614 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5615 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 5616 .addReg(NewVReg2, RegState::Kill) 5617 .addImm(PCLabelId); 5618 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 5619 .addReg(NewVReg3, RegState::Kill) 5620 .addFrameIndex(FI) 5621 .addImm(36) // &jbuf[1] :: pc 5622 .addMemOperand(FIMMOSt)); 5623 } else if (isThumb) { 5624 // Incoming value: jbuf 5625 // ldr.n r1, LCPI1_4 5626 // add r1, pc 5627 // mov r2, #1 5628 // orrs r1, r2 5629 // add r2, $jbuf, #+4 ; &jbuf[1] 5630 // str r1, [r2] 5631 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5632 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 5633 .addConstantPoolIndex(CPI) 5634 .addMemOperand(CPMMO)); 5635 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5636 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 5637 .addReg(NewVReg1, RegState::Kill) 5638 .addImm(PCLabelId); 5639 // Set the low bit because of thumb mode. 5640 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5641 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 5642 .addReg(ARM::CPSR, RegState::Define) 5643 .addImm(1)); 5644 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 5645 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 5646 .addReg(ARM::CPSR, RegState::Define) 5647 .addReg(NewVReg2, RegState::Kill) 5648 .addReg(NewVReg3, RegState::Kill)); 5649 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 5650 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5) 5651 .addFrameIndex(FI) 5652 .addImm(36)); // &jbuf[1] :: pc 5653 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 5654 .addReg(NewVReg4, RegState::Kill) 5655 .addReg(NewVReg5, RegState::Kill) 5656 .addImm(0) 5657 .addMemOperand(FIMMOSt)); 5658 } else { 5659 // Incoming value: jbuf 5660 // ldr r1, LCPI1_1 5661 // add r1, pc, r1 5662 // str r1, [$jbuf, #+4] ; &jbuf[1] 5663 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5664 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 5665 .addConstantPoolIndex(CPI) 5666 .addImm(0) 5667 .addMemOperand(CPMMO)); 5668 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5669 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 5670 .addReg(NewVReg1, RegState::Kill) 5671 .addImm(PCLabelId)); 5672 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 5673 .addReg(NewVReg2, RegState::Kill) 5674 .addFrameIndex(FI) 5675 .addImm(36) // &jbuf[1] :: pc 5676 .addMemOperand(FIMMOSt)); 5677 } 5678 } 5679 5680 MachineBasicBlock *ARMTargetLowering:: 5681 EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const { 5682 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 5683 DebugLoc dl = MI->getDebugLoc(); 5684 MachineFunction *MF = MBB->getParent(); 5685 MachineRegisterInfo *MRI = &MF->getRegInfo(); 5686 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 5687 MachineFrameInfo *MFI = MF->getFrameInfo(); 5688 int FI = MFI->getFunctionContextIndex(); 5689 5690 const TargetRegisterClass *TRC = 5691 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass; 5692 5693 // Get a mapping of the call site numbers to all of the landing pads they're 5694 // associated with. 5695 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad; 5696 unsigned MaxCSNum = 0; 5697 MachineModuleInfo &MMI = MF->getMMI(); 5698 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) { 5699 if (!BB->isLandingPad()) continue; 5700 5701 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 5702 // pad. 5703 for (MachineBasicBlock::iterator 5704 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 5705 if (!II->isEHLabel()) continue; 5706 5707 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 5708 if (!MMI.hasCallSiteLandingPad(Sym)) continue; 5709 5710 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym); 5711 for (SmallVectorImpl<unsigned>::iterator 5712 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 5713 CSI != CSE; ++CSI) { 5714 CallSiteNumToLPad[*CSI].push_back(BB); 5715 MaxCSNum = std::max(MaxCSNum, *CSI); 5716 } 5717 break; 5718 } 5719 } 5720 5721 // Get an ordered list of the machine basic blocks for the jump table. 5722 std::vector<MachineBasicBlock*> LPadList; 5723 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs; 5724 LPadList.reserve(CallSiteNumToLPad.size()); 5725 for (unsigned I = 1; I <= MaxCSNum; ++I) { 5726 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 5727 for (SmallVectorImpl<MachineBasicBlock*>::iterator 5728 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 5729 LPadList.push_back(*II); 5730 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 5731 } 5732 } 5733 5734 assert(!LPadList.empty() && 5735 "No landing pad destinations for the dispatch jump table!"); 5736 5737 // Create the jump table and associated information. 5738 MachineJumpTableInfo *JTI = 5739 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 5740 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 5741 unsigned UId = AFI->createJumpTableUId(); 5742 5743 // Create the MBBs for the dispatch code. 5744 5745 // Shove the dispatch's address into the return slot in the function context. 5746 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 5747 DispatchBB->setIsLandingPad(); 5748 5749 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 5750 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP)); 5751 DispatchBB->addSuccessor(TrapBB); 5752 5753 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 5754 DispatchBB->addSuccessor(DispContBB); 5755 5756 // Insert and MBBs. 5757 MF->insert(MF->end(), DispatchBB); 5758 MF->insert(MF->end(), DispContBB); 5759 MF->insert(MF->end(), TrapBB); 5760 5761 // Insert code into the entry block that creates and registers the function 5762 // context. 5763 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 5764 5765 MachineMemOperand *FIMMOLd = 5766 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 5767 MachineMemOperand::MOLoad | 5768 MachineMemOperand::MOVolatile, 4, 4); 5769 5770 BuildMI(DispatchBB, dl, TII->get(ARM::eh_sjlj_dispatchsetup)); 5771 5772 unsigned NumLPads = LPadList.size(); 5773 if (Subtarget->isThumb2()) { 5774 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5775 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 5776 .addFrameIndex(FI) 5777 .addImm(4) 5778 .addMemOperand(FIMMOLd)); 5779 5780 if (NumLPads < 256) { 5781 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 5782 .addReg(NewVReg1) 5783 .addImm(LPadList.size())); 5784 } else { 5785 unsigned VReg1 = MRI->createVirtualRegister(TRC); 5786 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 5787 .addImm(NumLPads & 0xFFFF)); 5788 5789 unsigned VReg2 = VReg1; 5790 if ((NumLPads & 0xFFFF0000) != 0) { 5791 VReg2 = MRI->createVirtualRegister(TRC); 5792 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 5793 .addReg(VReg1) 5794 .addImm(NumLPads >> 16)); 5795 } 5796 5797 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 5798 .addReg(NewVReg1) 5799 .addReg(VReg2)); 5800 } 5801 5802 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 5803 .addMBB(TrapBB) 5804 .addImm(ARMCC::HI) 5805 .addReg(ARM::CPSR); 5806 5807 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5808 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3) 5809 .addJumpTableIndex(MJTI) 5810 .addImm(UId)); 5811 5812 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 5813 AddDefaultCC( 5814 AddDefaultPred( 5815 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 5816 .addReg(NewVReg3, RegState::Kill) 5817 .addReg(NewVReg1) 5818 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 5819 5820 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 5821 .addReg(NewVReg4, RegState::Kill) 5822 .addReg(NewVReg1) 5823 .addJumpTableIndex(MJTI) 5824 .addImm(UId); 5825 } else if (Subtarget->isThumb()) { 5826 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5827 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 5828 .addFrameIndex(FI) 5829 .addImm(1) 5830 .addMemOperand(FIMMOLd)); 5831 5832 if (NumLPads < 256) { 5833 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 5834 .addReg(NewVReg1) 5835 .addImm(NumLPads)); 5836 } else { 5837 MachineConstantPool *ConstantPool = MF->getConstantPool(); 5838 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 5839 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 5840 5841 // MachineConstantPool wants an explicit alignment. 5842 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty); 5843 if (Align == 0) 5844 Align = getTargetData()->getTypeAllocSize(C->getType()); 5845 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 5846 5847 unsigned VReg1 = MRI->createVirtualRegister(TRC); 5848 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 5849 .addReg(VReg1, RegState::Define) 5850 .addConstantPoolIndex(Idx)); 5851 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 5852 .addReg(NewVReg1) 5853 .addReg(VReg1)); 5854 } 5855 5856 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 5857 .addMBB(TrapBB) 5858 .addImm(ARMCC::HI) 5859 .addReg(ARM::CPSR); 5860 5861 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 5862 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 5863 .addReg(ARM::CPSR, RegState::Define) 5864 .addReg(NewVReg1) 5865 .addImm(2)); 5866 5867 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5868 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 5869 .addJumpTableIndex(MJTI) 5870 .addImm(UId)); 5871 5872 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 5873 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 5874 .addReg(ARM::CPSR, RegState::Define) 5875 .addReg(NewVReg2, RegState::Kill) 5876 .addReg(NewVReg3)); 5877 5878 MachineMemOperand *JTMMOLd = 5879 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 5880 MachineMemOperand::MOLoad, 4, 4); 5881 5882 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 5883 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 5884 .addReg(NewVReg4, RegState::Kill) 5885 .addImm(0) 5886 .addMemOperand(JTMMOLd)); 5887 5888 unsigned NewVReg6 = MRI->createVirtualRegister(TRC); 5889 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 5890 .addReg(ARM::CPSR, RegState::Define) 5891 .addReg(NewVReg5, RegState::Kill) 5892 .addReg(NewVReg3)); 5893 5894 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 5895 .addReg(NewVReg6, RegState::Kill) 5896 .addJumpTableIndex(MJTI) 5897 .addImm(UId); 5898 } else { 5899 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 5900 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 5901 .addFrameIndex(FI) 5902 .addImm(4) 5903 .addMemOperand(FIMMOLd)); 5904 5905 if (NumLPads < 256) { 5906 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 5907 .addReg(NewVReg1) 5908 .addImm(NumLPads)); 5909 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 5910 unsigned VReg1 = MRI->createVirtualRegister(TRC); 5911 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 5912 .addImm(NumLPads & 0xFFFF)); 5913 5914 unsigned VReg2 = VReg1; 5915 if ((NumLPads & 0xFFFF0000) != 0) { 5916 VReg2 = MRI->createVirtualRegister(TRC); 5917 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 5918 .addReg(VReg1) 5919 .addImm(NumLPads >> 16)); 5920 } 5921 5922 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 5923 .addReg(NewVReg1) 5924 .addReg(VReg2)); 5925 } else { 5926 MachineConstantPool *ConstantPool = MF->getConstantPool(); 5927 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 5928 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 5929 5930 // MachineConstantPool wants an explicit alignment. 5931 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty); 5932 if (Align == 0) 5933 Align = getTargetData()->getTypeAllocSize(C->getType()); 5934 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 5935 5936 unsigned VReg1 = MRI->createVirtualRegister(TRC); 5937 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 5938 .addReg(VReg1, RegState::Define) 5939 .addConstantPoolIndex(Idx) 5940 .addImm(0)); 5941 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 5942 .addReg(NewVReg1) 5943 .addReg(VReg1, RegState::Kill)); 5944 } 5945 5946 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 5947 .addMBB(TrapBB) 5948 .addImm(ARMCC::HI) 5949 .addReg(ARM::CPSR); 5950 5951 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 5952 AddDefaultCC( 5953 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 5954 .addReg(NewVReg1) 5955 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 5956 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 5957 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 5958 .addJumpTableIndex(MJTI) 5959 .addImm(UId)); 5960 5961 MachineMemOperand *JTMMOLd = 5962 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 5963 MachineMemOperand::MOLoad, 4, 4); 5964 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 5965 AddDefaultPred( 5966 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 5967 .addReg(NewVReg3, RegState::Kill) 5968 .addReg(NewVReg4) 5969 .addImm(0) 5970 .addMemOperand(JTMMOLd)); 5971 5972 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 5973 .addReg(NewVReg5, RegState::Kill) 5974 .addReg(NewVReg4) 5975 .addJumpTableIndex(MJTI) 5976 .addImm(UId); 5977 } 5978 5979 // Add the jump table entries as successors to the MBB. 5980 MachineBasicBlock *PrevMBB = 0; 5981 for (std::vector<MachineBasicBlock*>::iterator 5982 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 5983 MachineBasicBlock *CurMBB = *I; 5984 if (PrevMBB != CurMBB) 5985 DispContBB->addSuccessor(CurMBB); 5986 PrevMBB = CurMBB; 5987 } 5988 5989 // N.B. the order the invoke BBs are processed in doesn't matter here. 5990 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 5991 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 5992 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF); 5993 SmallVector<MachineBasicBlock*, 64> MBBLPads; 5994 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator 5995 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) { 5996 MachineBasicBlock *BB = *I; 5997 5998 // Remove the landing pad successor from the invoke block and replace it 5999 // with the new dispatch block. 6000 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 6001 BB->succ_end()); 6002 while (!Successors.empty()) { 6003 MachineBasicBlock *SMBB = Successors.pop_back_val(); 6004 if (SMBB->isLandingPad()) { 6005 BB->removeSuccessor(SMBB); 6006 MBBLPads.push_back(SMBB); 6007 } 6008 } 6009 6010 BB->addSuccessor(DispatchBB); 6011 6012 // Find the invoke call and mark all of the callee-saved registers as 6013 // 'implicit defined' so that they're spilled. This prevents code from 6014 // moving instructions to before the EH block, where they will never be 6015 // executed. 6016 for (MachineBasicBlock::reverse_iterator 6017 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 6018 if (!II->isCall()) continue; 6019 6020 DenseMap<unsigned, bool> DefRegs; 6021 for (MachineInstr::mop_iterator 6022 OI = II->operands_begin(), OE = II->operands_end(); 6023 OI != OE; ++OI) { 6024 if (!OI->isReg()) continue; 6025 DefRegs[OI->getReg()] = true; 6026 } 6027 6028 MachineInstrBuilder MIB(&*II); 6029 6030 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 6031 unsigned Reg = SavedRegs[i]; 6032 if (Subtarget->isThumb2() && 6033 !ARM::tGPRRegisterClass->contains(Reg) && 6034 !ARM::hGPRRegisterClass->contains(Reg)) 6035 continue; 6036 else if (Subtarget->isThumb1Only() && 6037 !ARM::tGPRRegisterClass->contains(Reg)) 6038 continue; 6039 else if (!Subtarget->isThumb() && 6040 !ARM::GPRRegisterClass->contains(Reg)) 6041 continue; 6042 if (!DefRegs[Reg]) 6043 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 6044 } 6045 6046 break; 6047 } 6048 } 6049 6050 // Mark all former landing pads as non-landing pads. The dispatch is the only 6051 // landing pad now. 6052 for (SmallVectorImpl<MachineBasicBlock*>::iterator 6053 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 6054 (*I)->setIsLandingPad(false); 6055 6056 // The instruction is gone now. 6057 MI->eraseFromParent(); 6058 6059 return MBB; 6060 } 6061 6062 static 6063 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 6064 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 6065 E = MBB->succ_end(); I != E; ++I) 6066 if (*I != Succ) 6067 return *I; 6068 llvm_unreachable("Expecting a BB with two successors!"); 6069 } 6070 6071 MachineBasicBlock * 6072 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 6073 MachineBasicBlock *BB) const { 6074 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 6075 DebugLoc dl = MI->getDebugLoc(); 6076 bool isThumb2 = Subtarget->isThumb2(); 6077 switch (MI->getOpcode()) { 6078 default: { 6079 MI->dump(); 6080 llvm_unreachable("Unexpected instr type to insert"); 6081 } 6082 // The Thumb2 pre-indexed stores have the same MI operands, they just 6083 // define them differently in the .td files from the isel patterns, so 6084 // they need pseudos. 6085 case ARM::t2STR_preidx: 6086 MI->setDesc(TII->get(ARM::t2STR_PRE)); 6087 return BB; 6088 case ARM::t2STRB_preidx: 6089 MI->setDesc(TII->get(ARM::t2STRB_PRE)); 6090 return BB; 6091 case ARM::t2STRH_preidx: 6092 MI->setDesc(TII->get(ARM::t2STRH_PRE)); 6093 return BB; 6094 6095 case ARM::STRi_preidx: 6096 case ARM::STRBi_preidx: { 6097 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ? 6098 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM; 6099 // Decode the offset. 6100 unsigned Offset = MI->getOperand(4).getImm(); 6101 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 6102 Offset = ARM_AM::getAM2Offset(Offset); 6103 if (isSub) 6104 Offset = -Offset; 6105 6106 MachineMemOperand *MMO = *MI->memoperands_begin(); 6107 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 6108 .addOperand(MI->getOperand(0)) // Rn_wb 6109 .addOperand(MI->getOperand(1)) // Rt 6110 .addOperand(MI->getOperand(2)) // Rn 6111 .addImm(Offset) // offset (skip GPR==zero_reg) 6112 .addOperand(MI->getOperand(5)) // pred 6113 .addOperand(MI->getOperand(6)) 6114 .addMemOperand(MMO); 6115 MI->eraseFromParent(); 6116 return BB; 6117 } 6118 case ARM::STRr_preidx: 6119 case ARM::STRBr_preidx: 6120 case ARM::STRH_preidx: { 6121 unsigned NewOpc; 6122 switch (MI->getOpcode()) { 6123 default: llvm_unreachable("unexpected opcode!"); 6124 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 6125 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 6126 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 6127 } 6128 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 6129 for (unsigned i = 0; i < MI->getNumOperands(); ++i) 6130 MIB.addOperand(MI->getOperand(i)); 6131 MI->eraseFromParent(); 6132 return BB; 6133 } 6134 case ARM::ATOMIC_LOAD_ADD_I8: 6135 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 6136 case ARM::ATOMIC_LOAD_ADD_I16: 6137 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 6138 case ARM::ATOMIC_LOAD_ADD_I32: 6139 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr); 6140 6141 case ARM::ATOMIC_LOAD_AND_I8: 6142 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6143 case ARM::ATOMIC_LOAD_AND_I16: 6144 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6145 case ARM::ATOMIC_LOAD_AND_I32: 6146 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6147 6148 case ARM::ATOMIC_LOAD_OR_I8: 6149 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6150 case ARM::ATOMIC_LOAD_OR_I16: 6151 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6152 case ARM::ATOMIC_LOAD_OR_I32: 6153 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6154 6155 case ARM::ATOMIC_LOAD_XOR_I8: 6156 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6157 case ARM::ATOMIC_LOAD_XOR_I16: 6158 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6159 case ARM::ATOMIC_LOAD_XOR_I32: 6160 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6161 6162 case ARM::ATOMIC_LOAD_NAND_I8: 6163 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 6164 case ARM::ATOMIC_LOAD_NAND_I16: 6165 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 6166 case ARM::ATOMIC_LOAD_NAND_I32: 6167 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr); 6168 6169 case ARM::ATOMIC_LOAD_SUB_I8: 6170 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 6171 case ARM::ATOMIC_LOAD_SUB_I16: 6172 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 6173 case ARM::ATOMIC_LOAD_SUB_I32: 6174 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr); 6175 6176 case ARM::ATOMIC_LOAD_MIN_I8: 6177 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT); 6178 case ARM::ATOMIC_LOAD_MIN_I16: 6179 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT); 6180 case ARM::ATOMIC_LOAD_MIN_I32: 6181 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT); 6182 6183 case ARM::ATOMIC_LOAD_MAX_I8: 6184 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT); 6185 case ARM::ATOMIC_LOAD_MAX_I16: 6186 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT); 6187 case ARM::ATOMIC_LOAD_MAX_I32: 6188 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT); 6189 6190 case ARM::ATOMIC_LOAD_UMIN_I8: 6191 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO); 6192 case ARM::ATOMIC_LOAD_UMIN_I16: 6193 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO); 6194 case ARM::ATOMIC_LOAD_UMIN_I32: 6195 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO); 6196 6197 case ARM::ATOMIC_LOAD_UMAX_I8: 6198 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI); 6199 case ARM::ATOMIC_LOAD_UMAX_I16: 6200 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI); 6201 case ARM::ATOMIC_LOAD_UMAX_I32: 6202 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI); 6203 6204 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0); 6205 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0); 6206 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0); 6207 6208 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1); 6209 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2); 6210 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4); 6211 6212 6213 case ARM::ATOMADD6432: 6214 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr, 6215 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr, 6216 /*NeedsCarry*/ true); 6217 case ARM::ATOMSUB6432: 6218 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 6219 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 6220 /*NeedsCarry*/ true); 6221 case ARM::ATOMOR6432: 6222 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr, 6223 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr); 6224 case ARM::ATOMXOR6432: 6225 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr, 6226 isThumb2 ? ARM::t2EORrr : ARM::EORrr); 6227 case ARM::ATOMAND6432: 6228 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr, 6229 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr); 6230 case ARM::ATOMSWAP6432: 6231 return EmitAtomicBinary64(MI, BB, 0, 0, false); 6232 case ARM::ATOMCMPXCHG6432: 6233 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr, 6234 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, 6235 /*NeedsCarry*/ false, /*IsCmpxchg*/true); 6236 6237 case ARM::tMOVCCr_pseudo: { 6238 // To "insert" a SELECT_CC instruction, we actually have to insert the 6239 // diamond control-flow pattern. The incoming instruction knows the 6240 // destination vreg to set, the condition code register to branch on, the 6241 // true/false values to select between, and a branch opcode to use. 6242 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6243 MachineFunction::iterator It = BB; 6244 ++It; 6245 6246 // thisMBB: 6247 // ... 6248 // TrueVal = ... 6249 // cmpTY ccX, r1, r2 6250 // bCC copy1MBB 6251 // fallthrough --> copy0MBB 6252 MachineBasicBlock *thisMBB = BB; 6253 MachineFunction *F = BB->getParent(); 6254 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 6255 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 6256 F->insert(It, copy0MBB); 6257 F->insert(It, sinkMBB); 6258 6259 // Transfer the remainder of BB and its successor edges to sinkMBB. 6260 sinkMBB->splice(sinkMBB->begin(), BB, 6261 llvm::next(MachineBasicBlock::iterator(MI)), 6262 BB->end()); 6263 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 6264 6265 BB->addSuccessor(copy0MBB); 6266 BB->addSuccessor(sinkMBB); 6267 6268 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB) 6269 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); 6270 6271 // copy0MBB: 6272 // %FalseValue = ... 6273 // # fallthrough to sinkMBB 6274 BB = copy0MBB; 6275 6276 // Update machine-CFG edges 6277 BB->addSuccessor(sinkMBB); 6278 6279 // sinkMBB: 6280 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 6281 // ... 6282 BB = sinkMBB; 6283 BuildMI(*BB, BB->begin(), dl, 6284 TII->get(ARM::PHI), MI->getOperand(0).getReg()) 6285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 6286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 6287 6288 MI->eraseFromParent(); // The pseudo instruction is gone now. 6289 return BB; 6290 } 6291 6292 case ARM::BCCi64: 6293 case ARM::BCCZi64: { 6294 // If there is an unconditional branch to the other successor, remove it. 6295 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end()); 6296 6297 // Compare both parts that make up the double comparison separately for 6298 // equality. 6299 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64; 6300 6301 unsigned LHS1 = MI->getOperand(1).getReg(); 6302 unsigned LHS2 = MI->getOperand(2).getReg(); 6303 if (RHSisZero) { 6304 AddDefaultPred(BuildMI(BB, dl, 6305 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6306 .addReg(LHS1).addImm(0)); 6307 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 6308 .addReg(LHS2).addImm(0) 6309 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 6310 } else { 6311 unsigned RHS1 = MI->getOperand(3).getReg(); 6312 unsigned RHS2 = MI->getOperand(4).getReg(); 6313 AddDefaultPred(BuildMI(BB, dl, 6314 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 6315 .addReg(LHS1).addReg(RHS1)); 6316 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 6317 .addReg(LHS2).addReg(RHS2) 6318 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 6319 } 6320 6321 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB(); 6322 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 6323 if (MI->getOperand(0).getImm() == ARMCC::NE) 6324 std::swap(destMBB, exitMBB); 6325 6326 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 6327 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 6328 if (isThumb2) 6329 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB)); 6330 else 6331 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 6332 6333 MI->eraseFromParent(); // The pseudo instruction is gone now. 6334 return BB; 6335 } 6336 6337 case ARM::Int_eh_sjlj_setjmp: 6338 case ARM::Int_eh_sjlj_setjmp_nofp: 6339 case ARM::tInt_eh_sjlj_setjmp: 6340 case ARM::t2Int_eh_sjlj_setjmp: 6341 case ARM::t2Int_eh_sjlj_setjmp_nofp: 6342 EmitSjLjDispatchBlock(MI, BB); 6343 return BB; 6344 6345 case ARM::ABS: 6346 case ARM::t2ABS: { 6347 // To insert an ABS instruction, we have to insert the 6348 // diamond control-flow pattern. The incoming instruction knows the 6349 // source vreg to test against 0, the destination vreg to set, 6350 // the condition code register to branch on, the 6351 // true/false values to select between, and a branch opcode to use. 6352 // It transforms 6353 // V1 = ABS V0 6354 // into 6355 // V2 = MOVS V0 6356 // BCC (branch to SinkBB if V0 >= 0) 6357 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 6358 // SinkBB: V1 = PHI(V2, V3) 6359 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 6360 MachineFunction::iterator BBI = BB; 6361 ++BBI; 6362 MachineFunction *Fn = BB->getParent(); 6363 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 6364 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 6365 Fn->insert(BBI, RSBBB); 6366 Fn->insert(BBI, SinkBB); 6367 6368 unsigned int ABSSrcReg = MI->getOperand(1).getReg(); 6369 unsigned int ABSDstReg = MI->getOperand(0).getReg(); 6370 bool isThumb2 = Subtarget->isThumb2(); 6371 MachineRegisterInfo &MRI = Fn->getRegInfo(); 6372 // In Thumb mode S must not be specified if source register is the SP or 6373 // PC and if destination register is the SP, so restrict register class 6374 unsigned NewMovDstReg = MRI.createVirtualRegister( 6375 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass); 6376 unsigned NewRsbDstReg = MRI.createVirtualRegister( 6377 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass); 6378 6379 // Transfer the remainder of BB and its successor edges to sinkMBB. 6380 SinkBB->splice(SinkBB->begin(), BB, 6381 llvm::next(MachineBasicBlock::iterator(MI)), 6382 BB->end()); 6383 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 6384 6385 BB->addSuccessor(RSBBB); 6386 BB->addSuccessor(SinkBB); 6387 6388 // fall through to SinkMBB 6389 RSBBB->addSuccessor(SinkBB); 6390 6391 // insert a movs at the end of BB 6392 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr), 6393 NewMovDstReg) 6394 .addReg(ABSSrcReg, RegState::Kill) 6395 .addImm((unsigned)ARMCC::AL).addReg(0) 6396 .addReg(ARM::CPSR, RegState::Define); 6397 6398 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 6399 BuildMI(BB, dl, 6400 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 6401 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 6402 6403 // insert rsbri in RSBBB 6404 // Note: BCC and rsbri will be converted into predicated rsbmi 6405 // by if-conversion pass 6406 BuildMI(*RSBBB, RSBBB->begin(), dl, 6407 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 6408 .addReg(NewMovDstReg, RegState::Kill) 6409 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 6410 6411 // insert PHI in SinkBB, 6412 // reuse ABSDstReg to not change uses of ABS instruction 6413 BuildMI(*SinkBB, SinkBB->begin(), dl, 6414 TII->get(ARM::PHI), ABSDstReg) 6415 .addReg(NewRsbDstReg).addMBB(RSBBB) 6416 .addReg(NewMovDstReg).addMBB(BB); 6417 6418 // remove ABS instruction 6419 MI->eraseFromParent(); 6420 6421 // return last added BB 6422 return SinkBB; 6423 } 6424 } 6425 } 6426 6427 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 6428 SDNode *Node) const { 6429 if (!MI->hasPostISelHook()) { 6430 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) && 6431 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'"); 6432 return; 6433 } 6434 6435 const MCInstrDesc *MCID = &MI->getDesc(); 6436 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 6437 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 6438 // operand is still set to noreg. If needed, set the optional operand's 6439 // register to CPSR, and remove the redundant implicit def. 6440 // 6441 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>). 6442 6443 // Rename pseudo opcodes. 6444 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode()); 6445 if (NewOpc) { 6446 const ARMBaseInstrInfo *TII = 6447 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo()); 6448 MCID = &TII->get(NewOpc); 6449 6450 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 && 6451 "converted opcode should be the same except for cc_out"); 6452 6453 MI->setDesc(*MCID); 6454 6455 // Add the optional cc_out operand 6456 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 6457 } 6458 unsigned ccOutIdx = MCID->getNumOperands() - 1; 6459 6460 // Any ARM instruction that sets the 's' bit should specify an optional 6461 // "cc_out" operand in the last operand position. 6462 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 6463 assert(!NewOpc && "Optional cc_out operand required"); 6464 return; 6465 } 6466 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 6467 // since we already have an optional CPSR def. 6468 bool definesCPSR = false; 6469 bool deadCPSR = false; 6470 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands(); 6471 i != e; ++i) { 6472 const MachineOperand &MO = MI->getOperand(i); 6473 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 6474 definesCPSR = true; 6475 if (MO.isDead()) 6476 deadCPSR = true; 6477 MI->RemoveOperand(i); 6478 break; 6479 } 6480 } 6481 if (!definesCPSR) { 6482 assert(!NewOpc && "Optional cc_out operand required"); 6483 return; 6484 } 6485 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 6486 if (deadCPSR) { 6487 assert(!MI->getOperand(ccOutIdx).getReg() && 6488 "expect uninitialized optional cc_out operand"); 6489 return; 6490 } 6491 6492 // If this instruction was defined with an optional CPSR def and its dag node 6493 // had a live implicit CPSR def, then activate the optional CPSR def. 6494 MachineOperand &MO = MI->getOperand(ccOutIdx); 6495 MO.setReg(ARM::CPSR); 6496 MO.setIsDef(true); 6497 } 6498 6499 //===----------------------------------------------------------------------===// 6500 // ARM Optimization Hooks 6501 //===----------------------------------------------------------------------===// 6502 6503 static 6504 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 6505 TargetLowering::DAGCombinerInfo &DCI) { 6506 SelectionDAG &DAG = DCI.DAG; 6507 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6508 EVT VT = N->getValueType(0); 6509 unsigned Opc = N->getOpcode(); 6510 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC; 6511 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1); 6512 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2); 6513 ISD::CondCode CC = ISD::SETCC_INVALID; 6514 6515 if (isSlctCC) { 6516 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get(); 6517 } else { 6518 SDValue CCOp = Slct.getOperand(0); 6519 if (CCOp.getOpcode() == ISD::SETCC) 6520 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get(); 6521 } 6522 6523 bool DoXform = false; 6524 bool InvCC = false; 6525 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) && 6526 "Bad input!"); 6527 6528 if (LHS.getOpcode() == ISD::Constant && 6529 cast<ConstantSDNode>(LHS)->isNullValue()) { 6530 DoXform = true; 6531 } else if (CC != ISD::SETCC_INVALID && 6532 RHS.getOpcode() == ISD::Constant && 6533 cast<ConstantSDNode>(RHS)->isNullValue()) { 6534 std::swap(LHS, RHS); 6535 SDValue Op0 = Slct.getOperand(0); 6536 EVT OpVT = isSlctCC ? Op0.getValueType() : 6537 Op0.getOperand(0).getValueType(); 6538 bool isInt = OpVT.isInteger(); 6539 CC = ISD::getSetCCInverse(CC, isInt); 6540 6541 if (!TLI.isCondCodeLegal(CC, OpVT)) 6542 return SDValue(); // Inverse operator isn't legal. 6543 6544 DoXform = true; 6545 InvCC = true; 6546 } 6547 6548 if (DoXform) { 6549 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS); 6550 if (isSlctCC) 6551 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result, 6552 Slct.getOperand(0), Slct.getOperand(1), CC); 6553 SDValue CCOp = Slct.getOperand(0); 6554 if (InvCC) 6555 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(), 6556 CCOp.getOperand(0), CCOp.getOperand(1), CC); 6557 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT, 6558 CCOp, OtherOp, Result); 6559 } 6560 return SDValue(); 6561 } 6562 6563 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction 6564 // (only after legalization). 6565 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1, 6566 TargetLowering::DAGCombinerInfo &DCI, 6567 const ARMSubtarget *Subtarget) { 6568 6569 // Only perform optimization if after legalize, and if NEON is available. We 6570 // also expected both operands to be BUILD_VECTORs. 6571 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 6572 || N0.getOpcode() != ISD::BUILD_VECTOR 6573 || N1.getOpcode() != ISD::BUILD_VECTOR) 6574 return SDValue(); 6575 6576 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 6577 EVT VT = N->getValueType(0); 6578 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 6579 return SDValue(); 6580 6581 // Check that the vector operands are of the right form. 6582 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 6583 // operands, where N is the size of the formed vector. 6584 // Each EXTRACT_VECTOR should have the same input vector and odd or even 6585 // index such that we have a pair wise add pattern. 6586 6587 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 6588 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 6589 return SDValue(); 6590 SDValue Vec = N0->getOperand(0)->getOperand(0); 6591 SDNode *V = Vec.getNode(); 6592 unsigned nextIndex = 0; 6593 6594 // For each operands to the ADD which are BUILD_VECTORs, 6595 // check to see if each of their operands are an EXTRACT_VECTOR with 6596 // the same vector and appropriate index. 6597 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 6598 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 6599 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 6600 6601 SDValue ExtVec0 = N0->getOperand(i); 6602 SDValue ExtVec1 = N1->getOperand(i); 6603 6604 // First operand is the vector, verify its the same. 6605 if (V != ExtVec0->getOperand(0).getNode() || 6606 V != ExtVec1->getOperand(0).getNode()) 6607 return SDValue(); 6608 6609 // Second is the constant, verify its correct. 6610 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 6611 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 6612 6613 // For the constant, we want to see all the even or all the odd. 6614 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 6615 || C1->getZExtValue() != nextIndex+1) 6616 return SDValue(); 6617 6618 // Increment index. 6619 nextIndex+=2; 6620 } else 6621 return SDValue(); 6622 } 6623 6624 // Create VPADDL node. 6625 SelectionDAG &DAG = DCI.DAG; 6626 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6627 6628 // Build operand list. 6629 SmallVector<SDValue, 8> Ops; 6630 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, 6631 TLI.getPointerTy())); 6632 6633 // Input is the vector. 6634 Ops.push_back(Vec); 6635 6636 // Get widened type and narrowed type. 6637 MVT widenType; 6638 unsigned numElem = VT.getVectorNumElements(); 6639 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) { 6640 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 6641 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 6642 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 6643 default: 6644 assert(0 && "Invalid vector element type for padd optimization."); 6645 } 6646 6647 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), 6648 widenType, &Ops[0], Ops.size()); 6649 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp); 6650 } 6651 6652 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 6653 /// operands N0 and N1. This is a helper for PerformADDCombine that is 6654 /// called with the default operands, and if that fails, with commuted 6655 /// operands. 6656 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 6657 TargetLowering::DAGCombinerInfo &DCI, 6658 const ARMSubtarget *Subtarget){ 6659 6660 // Attempt to create vpaddl for this add. 6661 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget); 6662 if (Result.getNode()) 6663 return Result; 6664 6665 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 6666 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) { 6667 SDValue Result = combineSelectAndUse(N, N0, N1, DCI); 6668 if (Result.getNode()) return Result; 6669 } 6670 return SDValue(); 6671 } 6672 6673 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 6674 /// 6675 static SDValue PerformADDCombine(SDNode *N, 6676 TargetLowering::DAGCombinerInfo &DCI, 6677 const ARMSubtarget *Subtarget) { 6678 SDValue N0 = N->getOperand(0); 6679 SDValue N1 = N->getOperand(1); 6680 6681 // First try with the default operand order. 6682 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget); 6683 if (Result.getNode()) 6684 return Result; 6685 6686 // If that didn't work, try again with the operands commuted. 6687 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 6688 } 6689 6690 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 6691 /// 6692 static SDValue PerformSUBCombine(SDNode *N, 6693 TargetLowering::DAGCombinerInfo &DCI) { 6694 SDValue N0 = N->getOperand(0); 6695 SDValue N1 = N->getOperand(1); 6696 6697 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 6698 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) { 6699 SDValue Result = combineSelectAndUse(N, N1, N0, DCI); 6700 if (Result.getNode()) return Result; 6701 } 6702 6703 return SDValue(); 6704 } 6705 6706 /// PerformVMULCombine 6707 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 6708 /// special multiplier accumulator forwarding. 6709 /// vmul d3, d0, d2 6710 /// vmla d3, d1, d2 6711 /// is faster than 6712 /// vadd d3, d0, d1 6713 /// vmul d3, d3, d2 6714 static SDValue PerformVMULCombine(SDNode *N, 6715 TargetLowering::DAGCombinerInfo &DCI, 6716 const ARMSubtarget *Subtarget) { 6717 if (!Subtarget->hasVMLxForwarding()) 6718 return SDValue(); 6719 6720 SelectionDAG &DAG = DCI.DAG; 6721 SDValue N0 = N->getOperand(0); 6722 SDValue N1 = N->getOperand(1); 6723 unsigned Opcode = N0.getOpcode(); 6724 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 6725 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 6726 Opcode = N1.getOpcode(); 6727 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 6728 Opcode != ISD::FADD && Opcode != ISD::FSUB) 6729 return SDValue(); 6730 std::swap(N0, N1); 6731 } 6732 6733 EVT VT = N->getValueType(0); 6734 DebugLoc DL = N->getDebugLoc(); 6735 SDValue N00 = N0->getOperand(0); 6736 SDValue N01 = N0->getOperand(1); 6737 return DAG.getNode(Opcode, DL, VT, 6738 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 6739 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 6740 } 6741 6742 static SDValue PerformMULCombine(SDNode *N, 6743 TargetLowering::DAGCombinerInfo &DCI, 6744 const ARMSubtarget *Subtarget) { 6745 SelectionDAG &DAG = DCI.DAG; 6746 6747 if (Subtarget->isThumb1Only()) 6748 return SDValue(); 6749 6750 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 6751 return SDValue(); 6752 6753 EVT VT = N->getValueType(0); 6754 if (VT.is64BitVector() || VT.is128BitVector()) 6755 return PerformVMULCombine(N, DCI, Subtarget); 6756 if (VT != MVT::i32) 6757 return SDValue(); 6758 6759 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 6760 if (!C) 6761 return SDValue(); 6762 6763 uint64_t MulAmt = C->getZExtValue(); 6764 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt); 6765 ShiftAmt = ShiftAmt & (32 - 1); 6766 SDValue V = N->getOperand(0); 6767 DebugLoc DL = N->getDebugLoc(); 6768 6769 SDValue Res; 6770 MulAmt >>= ShiftAmt; 6771 if (isPowerOf2_32(MulAmt - 1)) { 6772 // (mul x, 2^N + 1) => (add (shl x, N), x) 6773 Res = DAG.getNode(ISD::ADD, DL, VT, 6774 V, DAG.getNode(ISD::SHL, DL, VT, 6775 V, DAG.getConstant(Log2_32(MulAmt-1), 6776 MVT::i32))); 6777 } else if (isPowerOf2_32(MulAmt + 1)) { 6778 // (mul x, 2^N - 1) => (sub (shl x, N), x) 6779 Res = DAG.getNode(ISD::SUB, DL, VT, 6780 DAG.getNode(ISD::SHL, DL, VT, 6781 V, DAG.getConstant(Log2_32(MulAmt+1), 6782 MVT::i32)), 6783 V); 6784 } else 6785 return SDValue(); 6786 6787 if (ShiftAmt != 0) 6788 Res = DAG.getNode(ISD::SHL, DL, VT, Res, 6789 DAG.getConstant(ShiftAmt, MVT::i32)); 6790 6791 // Do not add new nodes to DAG combiner worklist. 6792 DCI.CombineTo(N, Res, false); 6793 return SDValue(); 6794 } 6795 6796 static SDValue PerformANDCombine(SDNode *N, 6797 TargetLowering::DAGCombinerInfo &DCI) { 6798 6799 // Attempt to use immediate-form VBIC 6800 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 6801 DebugLoc dl = N->getDebugLoc(); 6802 EVT VT = N->getValueType(0); 6803 SelectionDAG &DAG = DCI.DAG; 6804 6805 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 6806 return SDValue(); 6807 6808 APInt SplatBits, SplatUndef; 6809 unsigned SplatBitSize; 6810 bool HasAnyUndefs; 6811 if (BVN && 6812 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 6813 if (SplatBitSize <= 64) { 6814 EVT VbicVT; 6815 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 6816 SplatUndef.getZExtValue(), SplatBitSize, 6817 DAG, VbicVT, VT.is128BitVector(), 6818 OtherModImm); 6819 if (Val.getNode()) { 6820 SDValue Input = 6821 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 6822 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 6823 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 6824 } 6825 } 6826 } 6827 6828 return SDValue(); 6829 } 6830 6831 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 6832 static SDValue PerformORCombine(SDNode *N, 6833 TargetLowering::DAGCombinerInfo &DCI, 6834 const ARMSubtarget *Subtarget) { 6835 // Attempt to use immediate-form VORR 6836 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 6837 DebugLoc dl = N->getDebugLoc(); 6838 EVT VT = N->getValueType(0); 6839 SelectionDAG &DAG = DCI.DAG; 6840 6841 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 6842 return SDValue(); 6843 6844 APInt SplatBits, SplatUndef; 6845 unsigned SplatBitSize; 6846 bool HasAnyUndefs; 6847 if (BVN && Subtarget->hasNEON() && 6848 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 6849 if (SplatBitSize <= 64) { 6850 EVT VorrVT; 6851 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 6852 SplatUndef.getZExtValue(), SplatBitSize, 6853 DAG, VorrVT, VT.is128BitVector(), 6854 OtherModImm); 6855 if (Val.getNode()) { 6856 SDValue Input = 6857 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 6858 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 6859 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 6860 } 6861 } 6862 } 6863 6864 SDValue N0 = N->getOperand(0); 6865 if (N0.getOpcode() != ISD::AND) 6866 return SDValue(); 6867 SDValue N1 = N->getOperand(1); 6868 6869 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 6870 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 6871 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 6872 APInt SplatUndef; 6873 unsigned SplatBitSize; 6874 bool HasAnyUndefs; 6875 6876 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 6877 APInt SplatBits0; 6878 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 6879 HasAnyUndefs) && !HasAnyUndefs) { 6880 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 6881 APInt SplatBits1; 6882 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 6883 HasAnyUndefs) && !HasAnyUndefs && 6884 SplatBits0 == ~SplatBits1) { 6885 // Canonicalize the vector type to make instruction selection simpler. 6886 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 6887 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 6888 N0->getOperand(1), N0->getOperand(0), 6889 N1->getOperand(0)); 6890 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 6891 } 6892 } 6893 } 6894 6895 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 6896 // reasonable. 6897 6898 // BFI is only available on V6T2+ 6899 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 6900 return SDValue(); 6901 6902 DebugLoc DL = N->getDebugLoc(); 6903 // 1) or (and A, mask), val => ARMbfi A, val, mask 6904 // iff (val & mask) == val 6905 // 6906 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 6907 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 6908 // && mask == ~mask2 6909 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 6910 // && ~mask == mask2 6911 // (i.e., copy a bitfield value into another bitfield of the same width) 6912 6913 if (VT != MVT::i32) 6914 return SDValue(); 6915 6916 SDValue N00 = N0.getOperand(0); 6917 6918 // The value and the mask need to be constants so we can verify this is 6919 // actually a bitfield set. If the mask is 0xffff, we can do better 6920 // via a movt instruction, so don't use BFI in that case. 6921 SDValue MaskOp = N0.getOperand(1); 6922 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 6923 if (!MaskC) 6924 return SDValue(); 6925 unsigned Mask = MaskC->getZExtValue(); 6926 if (Mask == 0xffff) 6927 return SDValue(); 6928 SDValue Res; 6929 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 6930 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 6931 if (N1C) { 6932 unsigned Val = N1C->getZExtValue(); 6933 if ((Val & ~Mask) != Val) 6934 return SDValue(); 6935 6936 if (ARM::isBitFieldInvertedMask(Mask)) { 6937 Val >>= CountTrailingZeros_32(~Mask); 6938 6939 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 6940 DAG.getConstant(Val, MVT::i32), 6941 DAG.getConstant(Mask, MVT::i32)); 6942 6943 // Do not add new nodes to DAG combiner worklist. 6944 DCI.CombineTo(N, Res, false); 6945 return SDValue(); 6946 } 6947 } else if (N1.getOpcode() == ISD::AND) { 6948 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 6949 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 6950 if (!N11C) 6951 return SDValue(); 6952 unsigned Mask2 = N11C->getZExtValue(); 6953 6954 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 6955 // as is to match. 6956 if (ARM::isBitFieldInvertedMask(Mask) && 6957 (Mask == ~Mask2)) { 6958 // The pack halfword instruction works better for masks that fit it, 6959 // so use that when it's available. 6960 if (Subtarget->hasT2ExtractPack() && 6961 (Mask == 0xffff || Mask == 0xffff0000)) 6962 return SDValue(); 6963 // 2a 6964 unsigned amt = CountTrailingZeros_32(Mask2); 6965 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 6966 DAG.getConstant(amt, MVT::i32)); 6967 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 6968 DAG.getConstant(Mask, MVT::i32)); 6969 // Do not add new nodes to DAG combiner worklist. 6970 DCI.CombineTo(N, Res, false); 6971 return SDValue(); 6972 } else if (ARM::isBitFieldInvertedMask(~Mask) && 6973 (~Mask == Mask2)) { 6974 // The pack halfword instruction works better for masks that fit it, 6975 // so use that when it's available. 6976 if (Subtarget->hasT2ExtractPack() && 6977 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 6978 return SDValue(); 6979 // 2b 6980 unsigned lsb = CountTrailingZeros_32(Mask); 6981 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 6982 DAG.getConstant(lsb, MVT::i32)); 6983 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 6984 DAG.getConstant(Mask2, MVT::i32)); 6985 // Do not add new nodes to DAG combiner worklist. 6986 DCI.CombineTo(N, Res, false); 6987 return SDValue(); 6988 } 6989 } 6990 6991 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 6992 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 6993 ARM::isBitFieldInvertedMask(~Mask)) { 6994 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 6995 // where lsb(mask) == #shamt and masked bits of B are known zero. 6996 SDValue ShAmt = N00.getOperand(1); 6997 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 6998 unsigned LSB = CountTrailingZeros_32(Mask); 6999 if (ShAmtC != LSB) 7000 return SDValue(); 7001 7002 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 7003 DAG.getConstant(~Mask, MVT::i32)); 7004 7005 // Do not add new nodes to DAG combiner worklist. 7006 DCI.CombineTo(N, Res, false); 7007 } 7008 7009 return SDValue(); 7010 } 7011 7012 /// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 7013 /// the bits being cleared by the AND are not demanded by the BFI. 7014 static SDValue PerformBFICombine(SDNode *N, 7015 TargetLowering::DAGCombinerInfo &DCI) { 7016 SDValue N1 = N->getOperand(1); 7017 if (N1.getOpcode() == ISD::AND) { 7018 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 7019 if (!N11C) 7020 return SDValue(); 7021 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 7022 unsigned LSB = CountTrailingZeros_32(~InvMask); 7023 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB; 7024 unsigned Mask = (1 << Width)-1; 7025 unsigned Mask2 = N11C->getZExtValue(); 7026 if ((Mask & (~Mask2)) == 0) 7027 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0), 7028 N->getOperand(0), N1.getOperand(0), 7029 N->getOperand(2)); 7030 } 7031 return SDValue(); 7032 } 7033 7034 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 7035 /// ARMISD::VMOVRRD. 7036 static SDValue PerformVMOVRRDCombine(SDNode *N, 7037 TargetLowering::DAGCombinerInfo &DCI) { 7038 // vmovrrd(vmovdrr x, y) -> x,y 7039 SDValue InDouble = N->getOperand(0); 7040 if (InDouble.getOpcode() == ARMISD::VMOVDRR) 7041 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 7042 7043 // vmovrrd(load f64) -> (load i32), (load i32) 7044 SDNode *InNode = InDouble.getNode(); 7045 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 7046 InNode->getValueType(0) == MVT::f64 && 7047 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 7048 !cast<LoadSDNode>(InNode)->isVolatile()) { 7049 // TODO: Should this be done for non-FrameIndex operands? 7050 LoadSDNode *LD = cast<LoadSDNode>(InNode); 7051 7052 SelectionDAG &DAG = DCI.DAG; 7053 DebugLoc DL = LD->getDebugLoc(); 7054 SDValue BasePtr = LD->getBasePtr(); 7055 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, 7056 LD->getPointerInfo(), LD->isVolatile(), 7057 LD->isNonTemporal(), LD->isInvariant(), 7058 LD->getAlignment()); 7059 7060 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 7061 DAG.getConstant(4, MVT::i32)); 7062 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, 7063 LD->getPointerInfo(), LD->isVolatile(), 7064 LD->isNonTemporal(), LD->isInvariant(), 7065 std::min(4U, LD->getAlignment() / 2)); 7066 7067 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 7068 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 7069 DCI.RemoveFromWorklist(LD); 7070 DAG.DeleteNode(LD); 7071 return Result; 7072 } 7073 7074 return SDValue(); 7075 } 7076 7077 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 7078 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 7079 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 7080 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 7081 SDValue Op0 = N->getOperand(0); 7082 SDValue Op1 = N->getOperand(1); 7083 if (Op0.getOpcode() == ISD::BITCAST) 7084 Op0 = Op0.getOperand(0); 7085 if (Op1.getOpcode() == ISD::BITCAST) 7086 Op1 = Op1.getOperand(0); 7087 if (Op0.getOpcode() == ARMISD::VMOVRRD && 7088 Op0.getNode() == Op1.getNode() && 7089 Op0.getResNo() == 0 && Op1.getResNo() == 1) 7090 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), 7091 N->getValueType(0), Op0.getOperand(0)); 7092 return SDValue(); 7093 } 7094 7095 /// PerformSTORECombine - Target-specific dag combine xforms for 7096 /// ISD::STORE. 7097 static SDValue PerformSTORECombine(SDNode *N, 7098 TargetLowering::DAGCombinerInfo &DCI) { 7099 // Bitcast an i64 store extracted from a vector to f64. 7100 // Otherwise, the i64 value will be legalized to a pair of i32 values. 7101 StoreSDNode *St = cast<StoreSDNode>(N); 7102 SDValue StVal = St->getValue(); 7103 if (!ISD::isNormalStore(St) || St->isVolatile()) 7104 return SDValue(); 7105 7106 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 7107 StVal.getNode()->hasOneUse() && !St->isVolatile()) { 7108 SelectionDAG &DAG = DCI.DAG; 7109 DebugLoc DL = St->getDebugLoc(); 7110 SDValue BasePtr = St->getBasePtr(); 7111 SDValue NewST1 = DAG.getStore(St->getChain(), DL, 7112 StVal.getNode()->getOperand(0), BasePtr, 7113 St->getPointerInfo(), St->isVolatile(), 7114 St->isNonTemporal(), St->getAlignment()); 7115 7116 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 7117 DAG.getConstant(4, MVT::i32)); 7118 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1), 7119 OffsetPtr, St->getPointerInfo(), St->isVolatile(), 7120 St->isNonTemporal(), 7121 std::min(4U, St->getAlignment() / 2)); 7122 } 7123 7124 if (StVal.getValueType() != MVT::i64 || 7125 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 7126 return SDValue(); 7127 7128 SelectionDAG &DAG = DCI.DAG; 7129 DebugLoc dl = StVal.getDebugLoc(); 7130 SDValue IntVec = StVal.getOperand(0); 7131 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 7132 IntVec.getValueType().getVectorNumElements()); 7133 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 7134 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 7135 Vec, StVal.getOperand(1)); 7136 dl = N->getDebugLoc(); 7137 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 7138 // Make the DAGCombiner fold the bitcasts. 7139 DCI.AddToWorklist(Vec.getNode()); 7140 DCI.AddToWorklist(ExtElt.getNode()); 7141 DCI.AddToWorklist(V.getNode()); 7142 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 7143 St->getPointerInfo(), St->isVolatile(), 7144 St->isNonTemporal(), St->getAlignment(), 7145 St->getTBAAInfo()); 7146 } 7147 7148 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 7149 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 7150 /// i64 vector to have f64 elements, since the value can then be loaded 7151 /// directly into a VFP register. 7152 static bool hasNormalLoadOperand(SDNode *N) { 7153 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 7154 for (unsigned i = 0; i < NumElts; ++i) { 7155 SDNode *Elt = N->getOperand(i).getNode(); 7156 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 7157 return true; 7158 } 7159 return false; 7160 } 7161 7162 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 7163 /// ISD::BUILD_VECTOR. 7164 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 7165 TargetLowering::DAGCombinerInfo &DCI){ 7166 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 7167 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 7168 // into a pair of GPRs, which is fine when the value is used as a scalar, 7169 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 7170 SelectionDAG &DAG = DCI.DAG; 7171 if (N->getNumOperands() == 2) { 7172 SDValue RV = PerformVMOVDRRCombine(N, DAG); 7173 if (RV.getNode()) 7174 return RV; 7175 } 7176 7177 // Load i64 elements as f64 values so that type legalization does not split 7178 // them up into i32 values. 7179 EVT VT = N->getValueType(0); 7180 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 7181 return SDValue(); 7182 DebugLoc dl = N->getDebugLoc(); 7183 SmallVector<SDValue, 8> Ops; 7184 unsigned NumElts = VT.getVectorNumElements(); 7185 for (unsigned i = 0; i < NumElts; ++i) { 7186 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 7187 Ops.push_back(V); 7188 // Make the DAGCombiner fold the bitcast. 7189 DCI.AddToWorklist(V.getNode()); 7190 } 7191 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 7192 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts); 7193 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 7194 } 7195 7196 /// PerformInsertEltCombine - Target-specific dag combine xforms for 7197 /// ISD::INSERT_VECTOR_ELT. 7198 static SDValue PerformInsertEltCombine(SDNode *N, 7199 TargetLowering::DAGCombinerInfo &DCI) { 7200 // Bitcast an i64 load inserted into a vector to f64. 7201 // Otherwise, the i64 value will be legalized to a pair of i32 values. 7202 EVT VT = N->getValueType(0); 7203 SDNode *Elt = N->getOperand(1).getNode(); 7204 if (VT.getVectorElementType() != MVT::i64 || 7205 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 7206 return SDValue(); 7207 7208 SelectionDAG &DAG = DCI.DAG; 7209 DebugLoc dl = N->getDebugLoc(); 7210 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 7211 VT.getVectorNumElements()); 7212 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 7213 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 7214 // Make the DAGCombiner fold the bitcasts. 7215 DCI.AddToWorklist(Vec.getNode()); 7216 DCI.AddToWorklist(V.getNode()); 7217 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 7218 Vec, V, N->getOperand(2)); 7219 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 7220 } 7221 7222 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 7223 /// ISD::VECTOR_SHUFFLE. 7224 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 7225 // The LLVM shufflevector instruction does not require the shuffle mask 7226 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 7227 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 7228 // operands do not match the mask length, they are extended by concatenating 7229 // them with undef vectors. That is probably the right thing for other 7230 // targets, but for NEON it is better to concatenate two double-register 7231 // size vector operands into a single quad-register size vector. Do that 7232 // transformation here: 7233 // shuffle(concat(v1, undef), concat(v2, undef)) -> 7234 // shuffle(concat(v1, v2), undef) 7235 SDValue Op0 = N->getOperand(0); 7236 SDValue Op1 = N->getOperand(1); 7237 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 7238 Op1.getOpcode() != ISD::CONCAT_VECTORS || 7239 Op0.getNumOperands() != 2 || 7240 Op1.getNumOperands() != 2) 7241 return SDValue(); 7242 SDValue Concat0Op1 = Op0.getOperand(1); 7243 SDValue Concat1Op1 = Op1.getOperand(1); 7244 if (Concat0Op1.getOpcode() != ISD::UNDEF || 7245 Concat1Op1.getOpcode() != ISD::UNDEF) 7246 return SDValue(); 7247 // Skip the transformation if any of the types are illegal. 7248 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7249 EVT VT = N->getValueType(0); 7250 if (!TLI.isTypeLegal(VT) || 7251 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 7252 !TLI.isTypeLegal(Concat1Op1.getValueType())) 7253 return SDValue(); 7254 7255 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT, 7256 Op0.getOperand(0), Op1.getOperand(0)); 7257 // Translate the shuffle mask. 7258 SmallVector<int, 16> NewMask; 7259 unsigned NumElts = VT.getVectorNumElements(); 7260 unsigned HalfElts = NumElts/2; 7261 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 7262 for (unsigned n = 0; n < NumElts; ++n) { 7263 int MaskElt = SVN->getMaskElt(n); 7264 int NewElt = -1; 7265 if (MaskElt < (int)HalfElts) 7266 NewElt = MaskElt; 7267 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 7268 NewElt = HalfElts + MaskElt - NumElts; 7269 NewMask.push_back(NewElt); 7270 } 7271 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat, 7272 DAG.getUNDEF(VT), NewMask.data()); 7273 } 7274 7275 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and 7276 /// NEON load/store intrinsics to merge base address updates. 7277 static SDValue CombineBaseUpdate(SDNode *N, 7278 TargetLowering::DAGCombinerInfo &DCI) { 7279 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 7280 return SDValue(); 7281 7282 SelectionDAG &DAG = DCI.DAG; 7283 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 7284 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 7285 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1); 7286 SDValue Addr = N->getOperand(AddrOpIdx); 7287 7288 // Search for a use of the address operand that is an increment. 7289 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 7290 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 7291 SDNode *User = *UI; 7292 if (User->getOpcode() != ISD::ADD || 7293 UI.getUse().getResNo() != Addr.getResNo()) 7294 continue; 7295 7296 // Check that the add is independent of the load/store. Otherwise, folding 7297 // it would create a cycle. 7298 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 7299 continue; 7300 7301 // Find the new opcode for the updating load/store. 7302 bool isLoad = true; 7303 bool isLaneOp = false; 7304 unsigned NewOpc = 0; 7305 unsigned NumVecs = 0; 7306 if (isIntrinsic) { 7307 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 7308 switch (IntNo) { 7309 default: assert(0 && "unexpected intrinsic for Neon base update"); 7310 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 7311 NumVecs = 1; break; 7312 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 7313 NumVecs = 2; break; 7314 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 7315 NumVecs = 3; break; 7316 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 7317 NumVecs = 4; break; 7318 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 7319 NumVecs = 2; isLaneOp = true; break; 7320 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 7321 NumVecs = 3; isLaneOp = true; break; 7322 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 7323 NumVecs = 4; isLaneOp = true; break; 7324 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 7325 NumVecs = 1; isLoad = false; break; 7326 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 7327 NumVecs = 2; isLoad = false; break; 7328 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 7329 NumVecs = 3; isLoad = false; break; 7330 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 7331 NumVecs = 4; isLoad = false; break; 7332 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 7333 NumVecs = 2; isLoad = false; isLaneOp = true; break; 7334 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 7335 NumVecs = 3; isLoad = false; isLaneOp = true; break; 7336 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 7337 NumVecs = 4; isLoad = false; isLaneOp = true; break; 7338 } 7339 } else { 7340 isLaneOp = true; 7341 switch (N->getOpcode()) { 7342 default: assert(0 && "unexpected opcode for Neon base update"); 7343 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 7344 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 7345 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 7346 } 7347 } 7348 7349 // Find the size of memory referenced by the load/store. 7350 EVT VecTy; 7351 if (isLoad) 7352 VecTy = N->getValueType(0); 7353 else 7354 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 7355 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 7356 if (isLaneOp) 7357 NumBytes /= VecTy.getVectorNumElements(); 7358 7359 // If the increment is a constant, it must match the memory ref size. 7360 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 7361 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 7362 uint64_t IncVal = CInc->getZExtValue(); 7363 if (IncVal != NumBytes) 7364 continue; 7365 } else if (NumBytes >= 3 * 16) { 7366 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 7367 // separate instructions that make it harder to use a non-constant update. 7368 continue; 7369 } 7370 7371 // Create the new updating load/store node. 7372 EVT Tys[6]; 7373 unsigned NumResultVecs = (isLoad ? NumVecs : 0); 7374 unsigned n; 7375 for (n = 0; n < NumResultVecs; ++n) 7376 Tys[n] = VecTy; 7377 Tys[n++] = MVT::i32; 7378 Tys[n] = MVT::Other; 7379 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2); 7380 SmallVector<SDValue, 8> Ops; 7381 Ops.push_back(N->getOperand(0)); // incoming chain 7382 Ops.push_back(N->getOperand(AddrOpIdx)); 7383 Ops.push_back(Inc); 7384 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) { 7385 Ops.push_back(N->getOperand(i)); 7386 } 7387 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N); 7388 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys, 7389 Ops.data(), Ops.size(), 7390 MemInt->getMemoryVT(), 7391 MemInt->getMemOperand()); 7392 7393 // Update the uses. 7394 std::vector<SDValue> NewResults; 7395 for (unsigned i = 0; i < NumResultVecs; ++i) { 7396 NewResults.push_back(SDValue(UpdN.getNode(), i)); 7397 } 7398 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 7399 DCI.CombineTo(N, NewResults); 7400 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 7401 7402 break; 7403 } 7404 return SDValue(); 7405 } 7406 7407 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 7408 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 7409 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 7410 /// return true. 7411 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 7412 SelectionDAG &DAG = DCI.DAG; 7413 EVT VT = N->getValueType(0); 7414 // vldN-dup instructions only support 64-bit vectors for N > 1. 7415 if (!VT.is64BitVector()) 7416 return false; 7417 7418 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 7419 SDNode *VLD = N->getOperand(0).getNode(); 7420 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 7421 return false; 7422 unsigned NumVecs = 0; 7423 unsigned NewOpc = 0; 7424 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 7425 if (IntNo == Intrinsic::arm_neon_vld2lane) { 7426 NumVecs = 2; 7427 NewOpc = ARMISD::VLD2DUP; 7428 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 7429 NumVecs = 3; 7430 NewOpc = ARMISD::VLD3DUP; 7431 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 7432 NumVecs = 4; 7433 NewOpc = ARMISD::VLD4DUP; 7434 } else { 7435 return false; 7436 } 7437 7438 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 7439 // numbers match the load. 7440 unsigned VLDLaneNo = 7441 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 7442 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 7443 UI != UE; ++UI) { 7444 // Ignore uses of the chain result. 7445 if (UI.getUse().getResNo() == NumVecs) 7446 continue; 7447 SDNode *User = *UI; 7448 if (User->getOpcode() != ARMISD::VDUPLANE || 7449 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 7450 return false; 7451 } 7452 7453 // Create the vldN-dup node. 7454 EVT Tys[5]; 7455 unsigned n; 7456 for (n = 0; n < NumVecs; ++n) 7457 Tys[n] = VT; 7458 Tys[n] = MVT::Other; 7459 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1); 7460 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 7461 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 7462 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys, 7463 Ops, 2, VLDMemInt->getMemoryVT(), 7464 VLDMemInt->getMemOperand()); 7465 7466 // Update the uses. 7467 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 7468 UI != UE; ++UI) { 7469 unsigned ResNo = UI.getUse().getResNo(); 7470 // Ignore uses of the chain result. 7471 if (ResNo == NumVecs) 7472 continue; 7473 SDNode *User = *UI; 7474 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 7475 } 7476 7477 // Now the vldN-lane intrinsic is dead except for its chain result. 7478 // Update uses of the chain. 7479 std::vector<SDValue> VLDDupResults; 7480 for (unsigned n = 0; n < NumVecs; ++n) 7481 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 7482 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 7483 DCI.CombineTo(VLD, VLDDupResults); 7484 7485 return true; 7486 } 7487 7488 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 7489 /// ARMISD::VDUPLANE. 7490 static SDValue PerformVDUPLANECombine(SDNode *N, 7491 TargetLowering::DAGCombinerInfo &DCI) { 7492 SDValue Op = N->getOperand(0); 7493 7494 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 7495 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 7496 if (CombineVLDDUP(N, DCI)) 7497 return SDValue(N, 0); 7498 7499 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 7500 // redundant. Ignore bit_converts for now; element sizes are checked below. 7501 while (Op.getOpcode() == ISD::BITCAST) 7502 Op = Op.getOperand(0); 7503 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 7504 return SDValue(); 7505 7506 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 7507 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits(); 7508 // The canonical VMOV for a zero vector uses a 32-bit element size. 7509 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 7510 unsigned EltBits; 7511 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 7512 EltSize = 8; 7513 EVT VT = N->getValueType(0); 7514 if (EltSize > VT.getVectorElementType().getSizeInBits()) 7515 return SDValue(); 7516 7517 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op); 7518 } 7519 7520 // isConstVecPow2 - Return true if each vector element is a power of 2, all 7521 // elements are the same constant, C, and Log2(C) ranges from 1 to 32. 7522 static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C) 7523 { 7524 integerPart cN; 7525 integerPart c0 = 0; 7526 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements(); 7527 I != E; I++) { 7528 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I)); 7529 if (!C) 7530 return false; 7531 7532 bool isExact; 7533 APFloat APF = C->getValueAPF(); 7534 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact) 7535 != APFloat::opOK || !isExact) 7536 return false; 7537 7538 c0 = (I == 0) ? cN : c0; 7539 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32) 7540 return false; 7541 } 7542 C = c0; 7543 return true; 7544 } 7545 7546 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 7547 /// can replace combinations of VMUL and VCVT (floating-point to integer) 7548 /// when the VMUL has a constant operand that is a power of 2. 7549 /// 7550 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 7551 /// vmul.f32 d16, d17, d16 7552 /// vcvt.s32.f32 d16, d16 7553 /// becomes: 7554 /// vcvt.s32.f32 d16, d16, #3 7555 static SDValue PerformVCVTCombine(SDNode *N, 7556 TargetLowering::DAGCombinerInfo &DCI, 7557 const ARMSubtarget *Subtarget) { 7558 SelectionDAG &DAG = DCI.DAG; 7559 SDValue Op = N->getOperand(0); 7560 7561 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() || 7562 Op.getOpcode() != ISD::FMUL) 7563 return SDValue(); 7564 7565 uint64_t C; 7566 SDValue N0 = Op->getOperand(0); 7567 SDValue ConstVec = Op->getOperand(1); 7568 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 7569 7570 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 7571 !isConstVecPow2(ConstVec, isSigned, C)) 7572 return SDValue(); 7573 7574 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 7575 Intrinsic::arm_neon_vcvtfp2fxu; 7576 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), 7577 N->getValueType(0), 7578 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0, 7579 DAG.getConstant(Log2_64(C), MVT::i32)); 7580 } 7581 7582 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 7583 /// can replace combinations of VCVT (integer to floating-point) and VDIV 7584 /// when the VDIV has a constant operand that is a power of 2. 7585 /// 7586 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 7587 /// vcvt.f32.s32 d16, d16 7588 /// vdiv.f32 d16, d17, d16 7589 /// becomes: 7590 /// vcvt.f32.s32 d16, d16, #3 7591 static SDValue PerformVDIVCombine(SDNode *N, 7592 TargetLowering::DAGCombinerInfo &DCI, 7593 const ARMSubtarget *Subtarget) { 7594 SelectionDAG &DAG = DCI.DAG; 7595 SDValue Op = N->getOperand(0); 7596 unsigned OpOpcode = Op.getNode()->getOpcode(); 7597 7598 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() || 7599 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 7600 return SDValue(); 7601 7602 uint64_t C; 7603 SDValue ConstVec = N->getOperand(1); 7604 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 7605 7606 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 7607 !isConstVecPow2(ConstVec, isSigned, C)) 7608 return SDValue(); 7609 7610 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 7611 Intrinsic::arm_neon_vcvtfxu2fp; 7612 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), 7613 Op.getValueType(), 7614 DAG.getConstant(IntrinsicOpcode, MVT::i32), 7615 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32)); 7616 } 7617 7618 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 7619 /// operand of a vector shift operation, where all the elements of the 7620 /// build_vector must have the same constant integer value. 7621 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 7622 // Ignore bit_converts. 7623 while (Op.getOpcode() == ISD::BITCAST) 7624 Op = Op.getOperand(0); 7625 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 7626 APInt SplatBits, SplatUndef; 7627 unsigned SplatBitSize; 7628 bool HasAnyUndefs; 7629 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 7630 HasAnyUndefs, ElementBits) || 7631 SplatBitSize > ElementBits) 7632 return false; 7633 Cnt = SplatBits.getSExtValue(); 7634 return true; 7635 } 7636 7637 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 7638 /// operand of a vector shift left operation. That value must be in the range: 7639 /// 0 <= Value < ElementBits for a left shift; or 7640 /// 0 <= Value <= ElementBits for a long left shift. 7641 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 7642 assert(VT.isVector() && "vector shift count is not a vector type"); 7643 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 7644 if (! getVShiftImm(Op, ElementBits, Cnt)) 7645 return false; 7646 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 7647 } 7648 7649 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 7650 /// operand of a vector shift right operation. For a shift opcode, the value 7651 /// is positive, but for an intrinsic the value count must be negative. The 7652 /// absolute value must be in the range: 7653 /// 1 <= |Value| <= ElementBits for a right shift; or 7654 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 7655 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 7656 int64_t &Cnt) { 7657 assert(VT.isVector() && "vector shift count is not a vector type"); 7658 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 7659 if (! getVShiftImm(Op, ElementBits, Cnt)) 7660 return false; 7661 if (isIntrinsic) 7662 Cnt = -Cnt; 7663 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 7664 } 7665 7666 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 7667 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 7668 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 7669 switch (IntNo) { 7670 default: 7671 // Don't do anything for most intrinsics. 7672 break; 7673 7674 // Vector shifts: check for immediate versions and lower them. 7675 // Note: This is done during DAG combining instead of DAG legalizing because 7676 // the build_vectors for 64-bit vector element shift counts are generally 7677 // not legal, and it is hard to see their values after they get legalized to 7678 // loads from a constant pool. 7679 case Intrinsic::arm_neon_vshifts: 7680 case Intrinsic::arm_neon_vshiftu: 7681 case Intrinsic::arm_neon_vshiftls: 7682 case Intrinsic::arm_neon_vshiftlu: 7683 case Intrinsic::arm_neon_vshiftn: 7684 case Intrinsic::arm_neon_vrshifts: 7685 case Intrinsic::arm_neon_vrshiftu: 7686 case Intrinsic::arm_neon_vrshiftn: 7687 case Intrinsic::arm_neon_vqshifts: 7688 case Intrinsic::arm_neon_vqshiftu: 7689 case Intrinsic::arm_neon_vqshiftsu: 7690 case Intrinsic::arm_neon_vqshiftns: 7691 case Intrinsic::arm_neon_vqshiftnu: 7692 case Intrinsic::arm_neon_vqshiftnsu: 7693 case Intrinsic::arm_neon_vqrshiftns: 7694 case Intrinsic::arm_neon_vqrshiftnu: 7695 case Intrinsic::arm_neon_vqrshiftnsu: { 7696 EVT VT = N->getOperand(1).getValueType(); 7697 int64_t Cnt; 7698 unsigned VShiftOpc = 0; 7699 7700 switch (IntNo) { 7701 case Intrinsic::arm_neon_vshifts: 7702 case Intrinsic::arm_neon_vshiftu: 7703 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 7704 VShiftOpc = ARMISD::VSHL; 7705 break; 7706 } 7707 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 7708 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 7709 ARMISD::VSHRs : ARMISD::VSHRu); 7710 break; 7711 } 7712 return SDValue(); 7713 7714 case Intrinsic::arm_neon_vshiftls: 7715 case Intrinsic::arm_neon_vshiftlu: 7716 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt)) 7717 break; 7718 llvm_unreachable("invalid shift count for vshll intrinsic"); 7719 7720 case Intrinsic::arm_neon_vrshifts: 7721 case Intrinsic::arm_neon_vrshiftu: 7722 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 7723 break; 7724 return SDValue(); 7725 7726 case Intrinsic::arm_neon_vqshifts: 7727 case Intrinsic::arm_neon_vqshiftu: 7728 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 7729 break; 7730 return SDValue(); 7731 7732 case Intrinsic::arm_neon_vqshiftsu: 7733 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 7734 break; 7735 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 7736 7737 case Intrinsic::arm_neon_vshiftn: 7738 case Intrinsic::arm_neon_vrshiftn: 7739 case Intrinsic::arm_neon_vqshiftns: 7740 case Intrinsic::arm_neon_vqshiftnu: 7741 case Intrinsic::arm_neon_vqshiftnsu: 7742 case Intrinsic::arm_neon_vqrshiftns: 7743 case Intrinsic::arm_neon_vqrshiftnu: 7744 case Intrinsic::arm_neon_vqrshiftnsu: 7745 // Narrowing shifts require an immediate right shift. 7746 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 7747 break; 7748 llvm_unreachable("invalid shift count for narrowing vector shift " 7749 "intrinsic"); 7750 7751 default: 7752 llvm_unreachable("unhandled vector shift"); 7753 } 7754 7755 switch (IntNo) { 7756 case Intrinsic::arm_neon_vshifts: 7757 case Intrinsic::arm_neon_vshiftu: 7758 // Opcode already set above. 7759 break; 7760 case Intrinsic::arm_neon_vshiftls: 7761 case Intrinsic::arm_neon_vshiftlu: 7762 if (Cnt == VT.getVectorElementType().getSizeInBits()) 7763 VShiftOpc = ARMISD::VSHLLi; 7764 else 7765 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ? 7766 ARMISD::VSHLLs : ARMISD::VSHLLu); 7767 break; 7768 case Intrinsic::arm_neon_vshiftn: 7769 VShiftOpc = ARMISD::VSHRN; break; 7770 case Intrinsic::arm_neon_vrshifts: 7771 VShiftOpc = ARMISD::VRSHRs; break; 7772 case Intrinsic::arm_neon_vrshiftu: 7773 VShiftOpc = ARMISD::VRSHRu; break; 7774 case Intrinsic::arm_neon_vrshiftn: 7775 VShiftOpc = ARMISD::VRSHRN; break; 7776 case Intrinsic::arm_neon_vqshifts: 7777 VShiftOpc = ARMISD::VQSHLs; break; 7778 case Intrinsic::arm_neon_vqshiftu: 7779 VShiftOpc = ARMISD::VQSHLu; break; 7780 case Intrinsic::arm_neon_vqshiftsu: 7781 VShiftOpc = ARMISD::VQSHLsu; break; 7782 case Intrinsic::arm_neon_vqshiftns: 7783 VShiftOpc = ARMISD::VQSHRNs; break; 7784 case Intrinsic::arm_neon_vqshiftnu: 7785 VShiftOpc = ARMISD::VQSHRNu; break; 7786 case Intrinsic::arm_neon_vqshiftnsu: 7787 VShiftOpc = ARMISD::VQSHRNsu; break; 7788 case Intrinsic::arm_neon_vqrshiftns: 7789 VShiftOpc = ARMISD::VQRSHRNs; break; 7790 case Intrinsic::arm_neon_vqrshiftnu: 7791 VShiftOpc = ARMISD::VQRSHRNu; break; 7792 case Intrinsic::arm_neon_vqrshiftnsu: 7793 VShiftOpc = ARMISD::VQRSHRNsu; break; 7794 } 7795 7796 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0), 7797 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32)); 7798 } 7799 7800 case Intrinsic::arm_neon_vshiftins: { 7801 EVT VT = N->getOperand(1).getValueType(); 7802 int64_t Cnt; 7803 unsigned VShiftOpc = 0; 7804 7805 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 7806 VShiftOpc = ARMISD::VSLI; 7807 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 7808 VShiftOpc = ARMISD::VSRI; 7809 else { 7810 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 7811 } 7812 7813 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0), 7814 N->getOperand(1), N->getOperand(2), 7815 DAG.getConstant(Cnt, MVT::i32)); 7816 } 7817 7818 case Intrinsic::arm_neon_vqrshifts: 7819 case Intrinsic::arm_neon_vqrshiftu: 7820 // No immediate versions of these to check for. 7821 break; 7822 } 7823 7824 return SDValue(); 7825 } 7826 7827 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 7828 /// lowers them. As with the vector shift intrinsics, this is done during DAG 7829 /// combining instead of DAG legalizing because the build_vectors for 64-bit 7830 /// vector element shift counts are generally not legal, and it is hard to see 7831 /// their values after they get legalized to loads from a constant pool. 7832 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 7833 const ARMSubtarget *ST) { 7834 EVT VT = N->getValueType(0); 7835 7836 // Nothing to be done for scalar shifts. 7837 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7838 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 7839 return SDValue(); 7840 7841 assert(ST->hasNEON() && "unexpected vector shift"); 7842 int64_t Cnt; 7843 7844 switch (N->getOpcode()) { 7845 default: llvm_unreachable("unexpected shift opcode"); 7846 7847 case ISD::SHL: 7848 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) 7849 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0), 7850 DAG.getConstant(Cnt, MVT::i32)); 7851 break; 7852 7853 case ISD::SRA: 7854 case ISD::SRL: 7855 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 7856 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 7857 ARMISD::VSHRs : ARMISD::VSHRu); 7858 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0), 7859 DAG.getConstant(Cnt, MVT::i32)); 7860 } 7861 } 7862 return SDValue(); 7863 } 7864 7865 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 7866 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 7867 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 7868 const ARMSubtarget *ST) { 7869 SDValue N0 = N->getOperand(0); 7870 7871 // Check for sign- and zero-extensions of vector extract operations of 8- 7872 // and 16-bit vector elements. NEON supports these directly. They are 7873 // handled during DAG combining because type legalization will promote them 7874 // to 32-bit types and it is messy to recognize the operations after that. 7875 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 7876 SDValue Vec = N0.getOperand(0); 7877 SDValue Lane = N0.getOperand(1); 7878 EVT VT = N->getValueType(0); 7879 EVT EltVT = N0.getValueType(); 7880 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7881 7882 if (VT == MVT::i32 && 7883 (EltVT == MVT::i8 || EltVT == MVT::i16) && 7884 TLI.isTypeLegal(Vec.getValueType()) && 7885 isa<ConstantSDNode>(Lane)) { 7886 7887 unsigned Opc = 0; 7888 switch (N->getOpcode()) { 7889 default: llvm_unreachable("unexpected opcode"); 7890 case ISD::SIGN_EXTEND: 7891 Opc = ARMISD::VGETLANEs; 7892 break; 7893 case ISD::ZERO_EXTEND: 7894 case ISD::ANY_EXTEND: 7895 Opc = ARMISD::VGETLANEu; 7896 break; 7897 } 7898 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane); 7899 } 7900 } 7901 7902 return SDValue(); 7903 } 7904 7905 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC 7906 /// to match f32 max/min patterns to use NEON vmax/vmin instructions. 7907 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG, 7908 const ARMSubtarget *ST) { 7909 // If the target supports NEON, try to use vmax/vmin instructions for f32 7910 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set, 7911 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is 7912 // a NaN; only do the transformation when it matches that behavior. 7913 7914 // For now only do this when using NEON for FP operations; if using VFP, it 7915 // is not obvious that the benefit outweighs the cost of switching to the 7916 // NEON pipeline. 7917 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() || 7918 N->getValueType(0) != MVT::f32) 7919 return SDValue(); 7920 7921 SDValue CondLHS = N->getOperand(0); 7922 SDValue CondRHS = N->getOperand(1); 7923 SDValue LHS = N->getOperand(2); 7924 SDValue RHS = N->getOperand(3); 7925 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 7926 7927 unsigned Opcode = 0; 7928 bool IsReversed; 7929 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) { 7930 IsReversed = false; // x CC y ? x : y 7931 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) { 7932 IsReversed = true ; // x CC y ? y : x 7933 } else { 7934 return SDValue(); 7935 } 7936 7937 bool IsUnordered; 7938 switch (CC) { 7939 default: break; 7940 case ISD::SETOLT: 7941 case ISD::SETOLE: 7942 case ISD::SETLT: 7943 case ISD::SETLE: 7944 case ISD::SETULT: 7945 case ISD::SETULE: 7946 // If LHS is NaN, an ordered comparison will be false and the result will 7947 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS 7948 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 7949 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE); 7950 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 7951 break; 7952 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin 7953 // will return -0, so vmin can only be used for unsafe math or if one of 7954 // the operands is known to be nonzero. 7955 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) && 7956 !DAG.getTarget().Options.UnsafeFPMath && 7957 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 7958 break; 7959 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN; 7960 break; 7961 7962 case ISD::SETOGT: 7963 case ISD::SETOGE: 7964 case ISD::SETGT: 7965 case ISD::SETGE: 7966 case ISD::SETUGT: 7967 case ISD::SETUGE: 7968 // If LHS is NaN, an ordered comparison will be false and the result will 7969 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS 7970 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 7971 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE); 7972 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 7973 break; 7974 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax 7975 // will return +0, so vmax can only be used for unsafe math or if one of 7976 // the operands is known to be nonzero. 7977 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) && 7978 !DAG.getTarget().Options.UnsafeFPMath && 7979 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 7980 break; 7981 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX; 7982 break; 7983 } 7984 7985 if (!Opcode) 7986 return SDValue(); 7987 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS); 7988 } 7989 7990 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 7991 SDValue 7992 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 7993 SDValue Cmp = N->getOperand(4); 7994 if (Cmp.getOpcode() != ARMISD::CMPZ) 7995 // Only looking at EQ and NE cases. 7996 return SDValue(); 7997 7998 EVT VT = N->getValueType(0); 7999 DebugLoc dl = N->getDebugLoc(); 8000 SDValue LHS = Cmp.getOperand(0); 8001 SDValue RHS = Cmp.getOperand(1); 8002 SDValue FalseVal = N->getOperand(0); 8003 SDValue TrueVal = N->getOperand(1); 8004 SDValue ARMcc = N->getOperand(2); 8005 ARMCC::CondCodes CC = 8006 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 8007 8008 // Simplify 8009 // mov r1, r0 8010 // cmp r1, x 8011 // mov r0, y 8012 // moveq r0, x 8013 // to 8014 // cmp r0, x 8015 // movne r0, y 8016 // 8017 // mov r1, r0 8018 // cmp r1, x 8019 // mov r0, x 8020 // movne r0, y 8021 // to 8022 // cmp r0, x 8023 // movne r0, y 8024 /// FIXME: Turn this into a target neutral optimization? 8025 SDValue Res; 8026 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 8027 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 8028 N->getOperand(3), Cmp); 8029 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 8030 SDValue ARMcc; 8031 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 8032 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 8033 N->getOperand(3), NewCmp); 8034 } 8035 8036 if (Res.getNode()) { 8037 APInt KnownZero, KnownOne; 8038 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()); 8039 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne); 8040 // Capture demanded bits information that would be otherwise lost. 8041 if (KnownZero == 0xfffffffe) 8042 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 8043 DAG.getValueType(MVT::i1)); 8044 else if (KnownZero == 0xffffff00) 8045 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 8046 DAG.getValueType(MVT::i8)); 8047 else if (KnownZero == 0xffff0000) 8048 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 8049 DAG.getValueType(MVT::i16)); 8050 } 8051 8052 return Res; 8053 } 8054 8055 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 8056 DAGCombinerInfo &DCI) const { 8057 switch (N->getOpcode()) { 8058 default: break; 8059 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 8060 case ISD::SUB: return PerformSUBCombine(N, DCI); 8061 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 8062 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 8063 case ISD::AND: return PerformANDCombine(N, DCI); 8064 case ARMISD::BFI: return PerformBFICombine(N, DCI); 8065 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI); 8066 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 8067 case ISD::STORE: return PerformSTORECombine(N, DCI); 8068 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI); 8069 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 8070 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 8071 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 8072 case ISD::FP_TO_SINT: 8073 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget); 8074 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget); 8075 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 8076 case ISD::SHL: 8077 case ISD::SRA: 8078 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 8079 case ISD::SIGN_EXTEND: 8080 case ISD::ZERO_EXTEND: 8081 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 8082 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget); 8083 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 8084 case ARMISD::VLD2DUP: 8085 case ARMISD::VLD3DUP: 8086 case ARMISD::VLD4DUP: 8087 return CombineBaseUpdate(N, DCI); 8088 case ISD::INTRINSIC_VOID: 8089 case ISD::INTRINSIC_W_CHAIN: 8090 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 8091 case Intrinsic::arm_neon_vld1: 8092 case Intrinsic::arm_neon_vld2: 8093 case Intrinsic::arm_neon_vld3: 8094 case Intrinsic::arm_neon_vld4: 8095 case Intrinsic::arm_neon_vld2lane: 8096 case Intrinsic::arm_neon_vld3lane: 8097 case Intrinsic::arm_neon_vld4lane: 8098 case Intrinsic::arm_neon_vst1: 8099 case Intrinsic::arm_neon_vst2: 8100 case Intrinsic::arm_neon_vst3: 8101 case Intrinsic::arm_neon_vst4: 8102 case Intrinsic::arm_neon_vst2lane: 8103 case Intrinsic::arm_neon_vst3lane: 8104 case Intrinsic::arm_neon_vst4lane: 8105 return CombineBaseUpdate(N, DCI); 8106 default: break; 8107 } 8108 break; 8109 } 8110 return SDValue(); 8111 } 8112 8113 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 8114 EVT VT) const { 8115 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 8116 } 8117 8118 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const { 8119 if (!Subtarget->allowsUnalignedMem()) 8120 return false; 8121 8122 switch (VT.getSimpleVT().SimpleTy) { 8123 default: 8124 return false; 8125 case MVT::i8: 8126 case MVT::i16: 8127 case MVT::i32: 8128 return true; 8129 // FIXME: VLD1 etc with standard alignment is legal. 8130 } 8131 } 8132 8133 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 8134 unsigned AlignCheck) { 8135 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 8136 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 8137 } 8138 8139 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 8140 unsigned DstAlign, unsigned SrcAlign, 8141 bool IsZeroVal, 8142 bool MemcpyStrSrc, 8143 MachineFunction &MF) const { 8144 const Function *F = MF.getFunction(); 8145 8146 // See if we can use NEON instructions for this... 8147 if (IsZeroVal && 8148 !F->hasFnAttr(Attribute::NoImplicitFloat) && 8149 Subtarget->hasNEON()) { 8150 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) { 8151 return MVT::v4i32; 8152 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) { 8153 return MVT::v2i32; 8154 } 8155 } 8156 8157 // Lowering to i32/i16 if the size permits. 8158 if (Size >= 4) { 8159 return MVT::i32; 8160 } else if (Size >= 2) { 8161 return MVT::i16; 8162 } 8163 8164 // Let the target-independent logic figure it out. 8165 return MVT::Other; 8166 } 8167 8168 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 8169 if (V < 0) 8170 return false; 8171 8172 unsigned Scale = 1; 8173 switch (VT.getSimpleVT().SimpleTy) { 8174 default: return false; 8175 case MVT::i1: 8176 case MVT::i8: 8177 // Scale == 1; 8178 break; 8179 case MVT::i16: 8180 // Scale == 2; 8181 Scale = 2; 8182 break; 8183 case MVT::i32: 8184 // Scale == 4; 8185 Scale = 4; 8186 break; 8187 } 8188 8189 if ((V & (Scale - 1)) != 0) 8190 return false; 8191 V /= Scale; 8192 return V == (V & ((1LL << 5) - 1)); 8193 } 8194 8195 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 8196 const ARMSubtarget *Subtarget) { 8197 bool isNeg = false; 8198 if (V < 0) { 8199 isNeg = true; 8200 V = - V; 8201 } 8202 8203 switch (VT.getSimpleVT().SimpleTy) { 8204 default: return false; 8205 case MVT::i1: 8206 case MVT::i8: 8207 case MVT::i16: 8208 case MVT::i32: 8209 // + imm12 or - imm8 8210 if (isNeg) 8211 return V == (V & ((1LL << 8) - 1)); 8212 return V == (V & ((1LL << 12) - 1)); 8213 case MVT::f32: 8214 case MVT::f64: 8215 // Same as ARM mode. FIXME: NEON? 8216 if (!Subtarget->hasVFP2()) 8217 return false; 8218 if ((V & 3) != 0) 8219 return false; 8220 V >>= 2; 8221 return V == (V & ((1LL << 8) - 1)); 8222 } 8223 } 8224 8225 /// isLegalAddressImmediate - Return true if the integer value can be used 8226 /// as the offset of the target addressing mode for load / store of the 8227 /// given type. 8228 static bool isLegalAddressImmediate(int64_t V, EVT VT, 8229 const ARMSubtarget *Subtarget) { 8230 if (V == 0) 8231 return true; 8232 8233 if (!VT.isSimple()) 8234 return false; 8235 8236 if (Subtarget->isThumb1Only()) 8237 return isLegalT1AddressImmediate(V, VT); 8238 else if (Subtarget->isThumb2()) 8239 return isLegalT2AddressImmediate(V, VT, Subtarget); 8240 8241 // ARM mode. 8242 if (V < 0) 8243 V = - V; 8244 switch (VT.getSimpleVT().SimpleTy) { 8245 default: return false; 8246 case MVT::i1: 8247 case MVT::i8: 8248 case MVT::i32: 8249 // +- imm12 8250 return V == (V & ((1LL << 12) - 1)); 8251 case MVT::i16: 8252 // +- imm8 8253 return V == (V & ((1LL << 8) - 1)); 8254 case MVT::f32: 8255 case MVT::f64: 8256 if (!Subtarget->hasVFP2()) // FIXME: NEON? 8257 return false; 8258 if ((V & 3) != 0) 8259 return false; 8260 V >>= 2; 8261 return V == (V & ((1LL << 8) - 1)); 8262 } 8263 } 8264 8265 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 8266 EVT VT) const { 8267 int Scale = AM.Scale; 8268 if (Scale < 0) 8269 return false; 8270 8271 switch (VT.getSimpleVT().SimpleTy) { 8272 default: return false; 8273 case MVT::i1: 8274 case MVT::i8: 8275 case MVT::i16: 8276 case MVT::i32: 8277 if (Scale == 1) 8278 return true; 8279 // r + r << imm 8280 Scale = Scale & ~1; 8281 return Scale == 2 || Scale == 4 || Scale == 8; 8282 case MVT::i64: 8283 // r + r 8284 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 8285 return true; 8286 return false; 8287 case MVT::isVoid: 8288 // Note, we allow "void" uses (basically, uses that aren't loads or 8289 // stores), because arm allows folding a scale into many arithmetic 8290 // operations. This should be made more precise and revisited later. 8291 8292 // Allow r << imm, but the imm has to be a multiple of two. 8293 if (Scale & 1) return false; 8294 return isPowerOf2_32(Scale); 8295 } 8296 } 8297 8298 /// isLegalAddressingMode - Return true if the addressing mode represented 8299 /// by AM is legal for this target, for a load/store of the specified type. 8300 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, 8301 Type *Ty) const { 8302 EVT VT = getValueType(Ty, true); 8303 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 8304 return false; 8305 8306 // Can never fold addr of global into load/store. 8307 if (AM.BaseGV) 8308 return false; 8309 8310 switch (AM.Scale) { 8311 case 0: // no scale reg, must be "r+i" or "r", or "i". 8312 break; 8313 case 1: 8314 if (Subtarget->isThumb1Only()) 8315 return false; 8316 // FALL THROUGH. 8317 default: 8318 // ARM doesn't support any R+R*scale+imm addr modes. 8319 if (AM.BaseOffs) 8320 return false; 8321 8322 if (!VT.isSimple()) 8323 return false; 8324 8325 if (Subtarget->isThumb2()) 8326 return isLegalT2ScaledAddressingMode(AM, VT); 8327 8328 int Scale = AM.Scale; 8329 switch (VT.getSimpleVT().SimpleTy) { 8330 default: return false; 8331 case MVT::i1: 8332 case MVT::i8: 8333 case MVT::i32: 8334 if (Scale < 0) Scale = -Scale; 8335 if (Scale == 1) 8336 return true; 8337 // r + r << imm 8338 return isPowerOf2_32(Scale & ~1); 8339 case MVT::i16: 8340 case MVT::i64: 8341 // r + r 8342 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 8343 return true; 8344 return false; 8345 8346 case MVT::isVoid: 8347 // Note, we allow "void" uses (basically, uses that aren't loads or 8348 // stores), because arm allows folding a scale into many arithmetic 8349 // operations. This should be made more precise and revisited later. 8350 8351 // Allow r << imm, but the imm has to be a multiple of two. 8352 if (Scale & 1) return false; 8353 return isPowerOf2_32(Scale); 8354 } 8355 break; 8356 } 8357 return true; 8358 } 8359 8360 /// isLegalICmpImmediate - Return true if the specified immediate is legal 8361 /// icmp immediate, that is the target has icmp instructions which can compare 8362 /// a register against the immediate without having to materialize the 8363 /// immediate into a register. 8364 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 8365 if (!Subtarget->isThumb()) 8366 return ARM_AM::getSOImmVal(Imm) != -1; 8367 if (Subtarget->isThumb2()) 8368 return ARM_AM::getT2SOImmVal(Imm) != -1; 8369 return Imm >= 0 && Imm <= 255; 8370 } 8371 8372 /// isLegalAddImmediate - Return true if the specified immediate is legal 8373 /// add immediate, that is the target has add instructions which can add 8374 /// a register with the immediate without having to materialize the 8375 /// immediate into a register. 8376 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 8377 return ARM_AM::getSOImmVal(Imm) != -1; 8378 } 8379 8380 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 8381 bool isSEXTLoad, SDValue &Base, 8382 SDValue &Offset, bool &isInc, 8383 SelectionDAG &DAG) { 8384 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 8385 return false; 8386 8387 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 8388 // AddressingMode 3 8389 Base = Ptr->getOperand(0); 8390 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 8391 int RHSC = (int)RHS->getZExtValue(); 8392 if (RHSC < 0 && RHSC > -256) { 8393 assert(Ptr->getOpcode() == ISD::ADD); 8394 isInc = false; 8395 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 8396 return true; 8397 } 8398 } 8399 isInc = (Ptr->getOpcode() == ISD::ADD); 8400 Offset = Ptr->getOperand(1); 8401 return true; 8402 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 8403 // AddressingMode 2 8404 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 8405 int RHSC = (int)RHS->getZExtValue(); 8406 if (RHSC < 0 && RHSC > -0x1000) { 8407 assert(Ptr->getOpcode() == ISD::ADD); 8408 isInc = false; 8409 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 8410 Base = Ptr->getOperand(0); 8411 return true; 8412 } 8413 } 8414 8415 if (Ptr->getOpcode() == ISD::ADD) { 8416 isInc = true; 8417 ARM_AM::ShiftOpc ShOpcVal= 8418 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 8419 if (ShOpcVal != ARM_AM::no_shift) { 8420 Base = Ptr->getOperand(1); 8421 Offset = Ptr->getOperand(0); 8422 } else { 8423 Base = Ptr->getOperand(0); 8424 Offset = Ptr->getOperand(1); 8425 } 8426 return true; 8427 } 8428 8429 isInc = (Ptr->getOpcode() == ISD::ADD); 8430 Base = Ptr->getOperand(0); 8431 Offset = Ptr->getOperand(1); 8432 return true; 8433 } 8434 8435 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 8436 return false; 8437 } 8438 8439 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 8440 bool isSEXTLoad, SDValue &Base, 8441 SDValue &Offset, bool &isInc, 8442 SelectionDAG &DAG) { 8443 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 8444 return false; 8445 8446 Base = Ptr->getOperand(0); 8447 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 8448 int RHSC = (int)RHS->getZExtValue(); 8449 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 8450 assert(Ptr->getOpcode() == ISD::ADD); 8451 isInc = false; 8452 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 8453 return true; 8454 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 8455 isInc = Ptr->getOpcode() == ISD::ADD; 8456 Offset = DAG.getConstant(RHSC, RHS->getValueType(0)); 8457 return true; 8458 } 8459 } 8460 8461 return false; 8462 } 8463 8464 /// getPreIndexedAddressParts - returns true by value, base pointer and 8465 /// offset pointer and addressing mode by reference if the node's address 8466 /// can be legally represented as pre-indexed load / store address. 8467 bool 8468 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 8469 SDValue &Offset, 8470 ISD::MemIndexedMode &AM, 8471 SelectionDAG &DAG) const { 8472 if (Subtarget->isThumb1Only()) 8473 return false; 8474 8475 EVT VT; 8476 SDValue Ptr; 8477 bool isSEXTLoad = false; 8478 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 8479 Ptr = LD->getBasePtr(); 8480 VT = LD->getMemoryVT(); 8481 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 8482 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 8483 Ptr = ST->getBasePtr(); 8484 VT = ST->getMemoryVT(); 8485 } else 8486 return false; 8487 8488 bool isInc; 8489 bool isLegal = false; 8490 if (Subtarget->isThumb2()) 8491 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 8492 Offset, isInc, DAG); 8493 else 8494 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 8495 Offset, isInc, DAG); 8496 if (!isLegal) 8497 return false; 8498 8499 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 8500 return true; 8501 } 8502 8503 /// getPostIndexedAddressParts - returns true by value, base pointer and 8504 /// offset pointer and addressing mode by reference if this node can be 8505 /// combined with a load / store to form a post-indexed load / store. 8506 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 8507 SDValue &Base, 8508 SDValue &Offset, 8509 ISD::MemIndexedMode &AM, 8510 SelectionDAG &DAG) const { 8511 if (Subtarget->isThumb1Only()) 8512 return false; 8513 8514 EVT VT; 8515 SDValue Ptr; 8516 bool isSEXTLoad = false; 8517 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 8518 VT = LD->getMemoryVT(); 8519 Ptr = LD->getBasePtr(); 8520 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 8521 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 8522 VT = ST->getMemoryVT(); 8523 Ptr = ST->getBasePtr(); 8524 } else 8525 return false; 8526 8527 bool isInc; 8528 bool isLegal = false; 8529 if (Subtarget->isThumb2()) 8530 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 8531 isInc, DAG); 8532 else 8533 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 8534 isInc, DAG); 8535 if (!isLegal) 8536 return false; 8537 8538 if (Ptr != Base) { 8539 // Swap base ptr and offset to catch more post-index load / store when 8540 // it's legal. In Thumb2 mode, offset must be an immediate. 8541 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 8542 !Subtarget->isThumb2()) 8543 std::swap(Base, Offset); 8544 8545 // Post-indexed load / store update the base pointer. 8546 if (Ptr != Base) 8547 return false; 8548 } 8549 8550 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 8551 return true; 8552 } 8553 8554 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, 8555 const APInt &Mask, 8556 APInt &KnownZero, 8557 APInt &KnownOne, 8558 const SelectionDAG &DAG, 8559 unsigned Depth) const { 8560 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); 8561 switch (Op.getOpcode()) { 8562 default: break; 8563 case ARMISD::CMOV: { 8564 // Bits are known zero/one if known on the LHS and RHS. 8565 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); 8566 if (KnownZero == 0 && KnownOne == 0) return; 8567 8568 APInt KnownZeroRHS, KnownOneRHS; 8569 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, 8570 KnownZeroRHS, KnownOneRHS, Depth+1); 8571 KnownZero &= KnownZeroRHS; 8572 KnownOne &= KnownOneRHS; 8573 return; 8574 } 8575 } 8576 } 8577 8578 //===----------------------------------------------------------------------===// 8579 // ARM Inline Assembly Support 8580 //===----------------------------------------------------------------------===// 8581 8582 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 8583 // Looking for "rev" which is V6+. 8584 if (!Subtarget->hasV6Ops()) 8585 return false; 8586 8587 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 8588 std::string AsmStr = IA->getAsmString(); 8589 SmallVector<StringRef, 4> AsmPieces; 8590 SplitString(AsmStr, AsmPieces, ";\n"); 8591 8592 switch (AsmPieces.size()) { 8593 default: return false; 8594 case 1: 8595 AsmStr = AsmPieces[0]; 8596 AsmPieces.clear(); 8597 SplitString(AsmStr, AsmPieces, " \t,"); 8598 8599 // rev $0, $1 8600 if (AsmPieces.size() == 3 && 8601 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 8602 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 8603 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 8604 if (Ty && Ty->getBitWidth() == 32) 8605 return IntrinsicLowering::LowerToByteSwap(CI); 8606 } 8607 break; 8608 } 8609 8610 return false; 8611 } 8612 8613 /// getConstraintType - Given a constraint letter, return the type of 8614 /// constraint it is for this target. 8615 ARMTargetLowering::ConstraintType 8616 ARMTargetLowering::getConstraintType(const std::string &Constraint) const { 8617 if (Constraint.size() == 1) { 8618 switch (Constraint[0]) { 8619 default: break; 8620 case 'l': return C_RegisterClass; 8621 case 'w': return C_RegisterClass; 8622 case 'h': return C_RegisterClass; 8623 case 'x': return C_RegisterClass; 8624 case 't': return C_RegisterClass; 8625 case 'j': return C_Other; // Constant for movw. 8626 // An address with a single base register. Due to the way we 8627 // currently handle addresses it is the same as an 'r' memory constraint. 8628 case 'Q': return C_Memory; 8629 } 8630 } else if (Constraint.size() == 2) { 8631 switch (Constraint[0]) { 8632 default: break; 8633 // All 'U+' constraints are addresses. 8634 case 'U': return C_Memory; 8635 } 8636 } 8637 return TargetLowering::getConstraintType(Constraint); 8638 } 8639 8640 /// Examine constraint type and operand type and determine a weight value. 8641 /// This object must already have been set up with the operand type 8642 /// and the current alternative constraint selected. 8643 TargetLowering::ConstraintWeight 8644 ARMTargetLowering::getSingleConstraintMatchWeight( 8645 AsmOperandInfo &info, const char *constraint) const { 8646 ConstraintWeight weight = CW_Invalid; 8647 Value *CallOperandVal = info.CallOperandVal; 8648 // If we don't have a value, we can't do a match, 8649 // but allow it at the lowest weight. 8650 if (CallOperandVal == NULL) 8651 return CW_Default; 8652 Type *type = CallOperandVal->getType(); 8653 // Look at the constraint type. 8654 switch (*constraint) { 8655 default: 8656 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 8657 break; 8658 case 'l': 8659 if (type->isIntegerTy()) { 8660 if (Subtarget->isThumb()) 8661 weight = CW_SpecificReg; 8662 else 8663 weight = CW_Register; 8664 } 8665 break; 8666 case 'w': 8667 if (type->isFloatingPointTy()) 8668 weight = CW_Register; 8669 break; 8670 } 8671 return weight; 8672 } 8673 8674 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair; 8675 RCPair 8676 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 8677 EVT VT) const { 8678 if (Constraint.size() == 1) { 8679 // GCC ARM Constraint Letters 8680 switch (Constraint[0]) { 8681 case 'l': // Low regs or general regs. 8682 if (Subtarget->isThumb()) 8683 return RCPair(0U, ARM::tGPRRegisterClass); 8684 else 8685 return RCPair(0U, ARM::GPRRegisterClass); 8686 case 'h': // High regs or no regs. 8687 if (Subtarget->isThumb()) 8688 return RCPair(0U, ARM::hGPRRegisterClass); 8689 break; 8690 case 'r': 8691 return RCPair(0U, ARM::GPRRegisterClass); 8692 case 'w': 8693 if (VT == MVT::f32) 8694 return RCPair(0U, ARM::SPRRegisterClass); 8695 if (VT.getSizeInBits() == 64) 8696 return RCPair(0U, ARM::DPRRegisterClass); 8697 if (VT.getSizeInBits() == 128) 8698 return RCPair(0U, ARM::QPRRegisterClass); 8699 break; 8700 case 'x': 8701 if (VT == MVT::f32) 8702 return RCPair(0U, ARM::SPR_8RegisterClass); 8703 if (VT.getSizeInBits() == 64) 8704 return RCPair(0U, ARM::DPR_8RegisterClass); 8705 if (VT.getSizeInBits() == 128) 8706 return RCPair(0U, ARM::QPR_8RegisterClass); 8707 break; 8708 case 't': 8709 if (VT == MVT::f32) 8710 return RCPair(0U, ARM::SPRRegisterClass); 8711 break; 8712 } 8713 } 8714 if (StringRef("{cc}").equals_lower(Constraint)) 8715 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass); 8716 8717 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 8718 } 8719 8720 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 8721 /// vector. If it is invalid, don't add anything to Ops. 8722 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 8723 std::string &Constraint, 8724 std::vector<SDValue>&Ops, 8725 SelectionDAG &DAG) const { 8726 SDValue Result(0, 0); 8727 8728 // Currently only support length 1 constraints. 8729 if (Constraint.length() != 1) return; 8730 8731 char ConstraintLetter = Constraint[0]; 8732 switch (ConstraintLetter) { 8733 default: break; 8734 case 'j': 8735 case 'I': case 'J': case 'K': case 'L': 8736 case 'M': case 'N': case 'O': 8737 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 8738 if (!C) 8739 return; 8740 8741 int64_t CVal64 = C->getSExtValue(); 8742 int CVal = (int) CVal64; 8743 // None of these constraints allow values larger than 32 bits. Check 8744 // that the value fits in an int. 8745 if (CVal != CVal64) 8746 return; 8747 8748 switch (ConstraintLetter) { 8749 case 'j': 8750 // Constant suitable for movw, must be between 0 and 8751 // 65535. 8752 if (Subtarget->hasV6T2Ops()) 8753 if (CVal >= 0 && CVal <= 65535) 8754 break; 8755 return; 8756 case 'I': 8757 if (Subtarget->isThumb1Only()) { 8758 // This must be a constant between 0 and 255, for ADD 8759 // immediates. 8760 if (CVal >= 0 && CVal <= 255) 8761 break; 8762 } else if (Subtarget->isThumb2()) { 8763 // A constant that can be used as an immediate value in a 8764 // data-processing instruction. 8765 if (ARM_AM::getT2SOImmVal(CVal) != -1) 8766 break; 8767 } else { 8768 // A constant that can be used as an immediate value in a 8769 // data-processing instruction. 8770 if (ARM_AM::getSOImmVal(CVal) != -1) 8771 break; 8772 } 8773 return; 8774 8775 case 'J': 8776 if (Subtarget->isThumb()) { // FIXME thumb2 8777 // This must be a constant between -255 and -1, for negated ADD 8778 // immediates. This can be used in GCC with an "n" modifier that 8779 // prints the negated value, for use with SUB instructions. It is 8780 // not useful otherwise but is implemented for compatibility. 8781 if (CVal >= -255 && CVal <= -1) 8782 break; 8783 } else { 8784 // This must be a constant between -4095 and 4095. It is not clear 8785 // what this constraint is intended for. Implemented for 8786 // compatibility with GCC. 8787 if (CVal >= -4095 && CVal <= 4095) 8788 break; 8789 } 8790 return; 8791 8792 case 'K': 8793 if (Subtarget->isThumb1Only()) { 8794 // A 32-bit value where only one byte has a nonzero value. Exclude 8795 // zero to match GCC. This constraint is used by GCC internally for 8796 // constants that can be loaded with a move/shift combination. 8797 // It is not useful otherwise but is implemented for compatibility. 8798 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 8799 break; 8800 } else if (Subtarget->isThumb2()) { 8801 // A constant whose bitwise inverse can be used as an immediate 8802 // value in a data-processing instruction. This can be used in GCC 8803 // with a "B" modifier that prints the inverted value, for use with 8804 // BIC and MVN instructions. It is not useful otherwise but is 8805 // implemented for compatibility. 8806 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 8807 break; 8808 } else { 8809 // A constant whose bitwise inverse can be used as an immediate 8810 // value in a data-processing instruction. This can be used in GCC 8811 // with a "B" modifier that prints the inverted value, for use with 8812 // BIC and MVN instructions. It is not useful otherwise but is 8813 // implemented for compatibility. 8814 if (ARM_AM::getSOImmVal(~CVal) != -1) 8815 break; 8816 } 8817 return; 8818 8819 case 'L': 8820 if (Subtarget->isThumb1Only()) { 8821 // This must be a constant between -7 and 7, 8822 // for 3-operand ADD/SUB immediate instructions. 8823 if (CVal >= -7 && CVal < 7) 8824 break; 8825 } else if (Subtarget->isThumb2()) { 8826 // A constant whose negation can be used as an immediate value in a 8827 // data-processing instruction. This can be used in GCC with an "n" 8828 // modifier that prints the negated value, for use with SUB 8829 // instructions. It is not useful otherwise but is implemented for 8830 // compatibility. 8831 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 8832 break; 8833 } else { 8834 // A constant whose negation can be used as an immediate value in a 8835 // data-processing instruction. This can be used in GCC with an "n" 8836 // modifier that prints the negated value, for use with SUB 8837 // instructions. It is not useful otherwise but is implemented for 8838 // compatibility. 8839 if (ARM_AM::getSOImmVal(-CVal) != -1) 8840 break; 8841 } 8842 return; 8843 8844 case 'M': 8845 if (Subtarget->isThumb()) { // FIXME thumb2 8846 // This must be a multiple of 4 between 0 and 1020, for 8847 // ADD sp + immediate. 8848 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 8849 break; 8850 } else { 8851 // A power of two or a constant between 0 and 32. This is used in 8852 // GCC for the shift amount on shifted register operands, but it is 8853 // useful in general for any shift amounts. 8854 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 8855 break; 8856 } 8857 return; 8858 8859 case 'N': 8860 if (Subtarget->isThumb()) { // FIXME thumb2 8861 // This must be a constant between 0 and 31, for shift amounts. 8862 if (CVal >= 0 && CVal <= 31) 8863 break; 8864 } 8865 return; 8866 8867 case 'O': 8868 if (Subtarget->isThumb()) { // FIXME thumb2 8869 // This must be a multiple of 4 between -508 and 508, for 8870 // ADD/SUB sp = sp + immediate. 8871 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 8872 break; 8873 } 8874 return; 8875 } 8876 Result = DAG.getTargetConstant(CVal, Op.getValueType()); 8877 break; 8878 } 8879 8880 if (Result.getNode()) { 8881 Ops.push_back(Result); 8882 return; 8883 } 8884 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 8885 } 8886 8887 bool 8888 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 8889 // The ARM target isn't yet aware of offsets. 8890 return false; 8891 } 8892 8893 bool ARM::isBitFieldInvertedMask(unsigned v) { 8894 if (v == 0xffffffff) 8895 return 0; 8896 // there can be 1's on either or both "outsides", all the "inside" 8897 // bits must be 0's 8898 unsigned int lsb = 0, msb = 31; 8899 while (v & (1 << msb)) --msb; 8900 while (v & (1 << lsb)) ++lsb; 8901 for (unsigned int i = lsb; i <= msb; ++i) { 8902 if (v & (1 << i)) 8903 return 0; 8904 } 8905 return 1; 8906 } 8907 8908 /// isFPImmLegal - Returns true if the target can instruction select the 8909 /// specified FP immediate natively. If false, the legalizer will 8910 /// materialize the FP immediate as a load from a constant pool. 8911 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 8912 if (!Subtarget->hasVFP3()) 8913 return false; 8914 if (VT == MVT::f32) 8915 return ARM_AM::getFP32Imm(Imm) != -1; 8916 if (VT == MVT::f64) 8917 return ARM_AM::getFP64Imm(Imm) != -1; 8918 return false; 8919 } 8920 8921 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 8922 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 8923 /// specified in the intrinsic calls. 8924 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 8925 const CallInst &I, 8926 unsigned Intrinsic) const { 8927 switch (Intrinsic) { 8928 case Intrinsic::arm_neon_vld1: 8929 case Intrinsic::arm_neon_vld2: 8930 case Intrinsic::arm_neon_vld3: 8931 case Intrinsic::arm_neon_vld4: 8932 case Intrinsic::arm_neon_vld2lane: 8933 case Intrinsic::arm_neon_vld3lane: 8934 case Intrinsic::arm_neon_vld4lane: { 8935 Info.opc = ISD::INTRINSIC_W_CHAIN; 8936 // Conservatively set memVT to the entire set of vectors loaded. 8937 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8; 8938 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 8939 Info.ptrVal = I.getArgOperand(0); 8940 Info.offset = 0; 8941 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 8942 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 8943 Info.vol = false; // volatile loads with NEON intrinsics not supported 8944 Info.readMem = true; 8945 Info.writeMem = false; 8946 return true; 8947 } 8948 case Intrinsic::arm_neon_vst1: 8949 case Intrinsic::arm_neon_vst2: 8950 case Intrinsic::arm_neon_vst3: 8951 case Intrinsic::arm_neon_vst4: 8952 case Intrinsic::arm_neon_vst2lane: 8953 case Intrinsic::arm_neon_vst3lane: 8954 case Intrinsic::arm_neon_vst4lane: { 8955 Info.opc = ISD::INTRINSIC_VOID; 8956 // Conservatively set memVT to the entire set of vectors stored. 8957 unsigned NumElts = 0; 8958 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 8959 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 8960 if (!ArgTy->isVectorTy()) 8961 break; 8962 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8; 8963 } 8964 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 8965 Info.ptrVal = I.getArgOperand(0); 8966 Info.offset = 0; 8967 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 8968 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 8969 Info.vol = false; // volatile stores with NEON intrinsics not supported 8970 Info.readMem = false; 8971 Info.writeMem = true; 8972 return true; 8973 } 8974 case Intrinsic::arm_strexd: { 8975 Info.opc = ISD::INTRINSIC_W_CHAIN; 8976 Info.memVT = MVT::i64; 8977 Info.ptrVal = I.getArgOperand(2); 8978 Info.offset = 0; 8979 Info.align = 8; 8980 Info.vol = true; 8981 Info.readMem = false; 8982 Info.writeMem = true; 8983 return true; 8984 } 8985 case Intrinsic::arm_ldrexd: { 8986 Info.opc = ISD::INTRINSIC_W_CHAIN; 8987 Info.memVT = MVT::i64; 8988 Info.ptrVal = I.getArgOperand(0); 8989 Info.offset = 0; 8990 Info.align = 8; 8991 Info.vol = true; 8992 Info.readMem = true; 8993 Info.writeMem = false; 8994 return true; 8995 } 8996 default: 8997 break; 8998 } 8999 9000 return false; 9001 } 9002