1 //===-- X86ISelLowering.cpp - X86 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 X86 uses to lower LLVM code into a 11 // selection DAG. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "X86.h" 16 #include "X86InstrBuilder.h" 17 #include "X86ISelLowering.h" 18 #include "X86TargetMachine.h" 19 #include "llvm/CallingConv.h" 20 #include "llvm/Constants.h" 21 #include "llvm/DerivedTypes.h" 22 #include "llvm/GlobalAlias.h" 23 #include "llvm/GlobalVariable.h" 24 #include "llvm/Function.h" 25 #include "llvm/Instructions.h" 26 #include "llvm/Intrinsics.h" 27 #include "llvm/LLVMContext.h" 28 #include "llvm/ADT/BitVector.h" 29 #include "llvm/ADT/VectorExtras.h" 30 #include "llvm/CodeGen/MachineFrameInfo.h" 31 #include "llvm/CodeGen/MachineFunction.h" 32 #include "llvm/CodeGen/MachineInstrBuilder.h" 33 #include "llvm/CodeGen/MachineModuleInfo.h" 34 #include "llvm/CodeGen/MachineRegisterInfo.h" 35 #include "llvm/CodeGen/PseudoSourceValue.h" 36 #include "llvm/Support/MathExtras.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Target/TargetLoweringObjectFile.h" 40 #include "llvm/Target/TargetOptions.h" 41 #include "llvm/ADT/SmallSet.h" 42 #include "llvm/ADT/StringExtras.h" 43 #include "llvm/Support/CommandLine.h" 44 #include "llvm/Support/raw_ostream.h" 45 using namespace llvm; 46 47 static cl::opt<bool> 48 DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX")); 49 50 // Forward declarations. 51 static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, 52 SDValue V2); 53 54 static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) { 55 switch (TM.getSubtarget<X86Subtarget>().TargetType) { 56 default: llvm_unreachable("unknown subtarget type"); 57 case X86Subtarget::isDarwin: 58 return new TargetLoweringObjectFileMachO(); 59 case X86Subtarget::isELF: 60 return new TargetLoweringObjectFileELF(); 61 case X86Subtarget::isMingw: 62 case X86Subtarget::isCygwin: 63 case X86Subtarget::isWindows: 64 return new TargetLoweringObjectFileCOFF(); 65 } 66 67 } 68 69 X86TargetLowering::X86TargetLowering(X86TargetMachine &TM) 70 : TargetLowering(TM, createTLOF(TM)) { 71 Subtarget = &TM.getSubtarget<X86Subtarget>(); 72 X86ScalarSSEf64 = Subtarget->hasSSE2(); 73 X86ScalarSSEf32 = Subtarget->hasSSE1(); 74 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; 75 76 RegInfo = TM.getRegisterInfo(); 77 TD = getTargetData(); 78 79 // Set up the TargetLowering object. 80 81 // X86 is weird, it always uses i8 for shift amounts and setcc results. 82 setShiftAmountType(MVT::i8); 83 setBooleanContents(ZeroOrOneBooleanContent); 84 setSchedulingPreference(SchedulingForRegPressure); 85 setStackPointerRegisterToSaveRestore(X86StackPtr); 86 87 if (Subtarget->isTargetDarwin()) { 88 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. 89 setUseUnderscoreSetJmp(false); 90 setUseUnderscoreLongJmp(false); 91 } else if (Subtarget->isTargetMingw()) { 92 // MS runtime is weird: it exports _setjmp, but longjmp! 93 setUseUnderscoreSetJmp(true); 94 setUseUnderscoreLongJmp(false); 95 } else { 96 setUseUnderscoreSetJmp(true); 97 setUseUnderscoreLongJmp(true); 98 } 99 100 // Set up the register classes. 101 addRegisterClass(MVT::i8, X86::GR8RegisterClass); 102 addRegisterClass(MVT::i16, X86::GR16RegisterClass); 103 addRegisterClass(MVT::i32, X86::GR32RegisterClass); 104 if (Subtarget->is64Bit()) 105 addRegisterClass(MVT::i64, X86::GR64RegisterClass); 106 107 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 108 109 // We don't accept any truncstore of integer registers. 110 setTruncStoreAction(MVT::i64, MVT::i32, Expand); 111 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 112 setTruncStoreAction(MVT::i64, MVT::i8 , Expand); 113 setTruncStoreAction(MVT::i32, MVT::i16, Expand); 114 setTruncStoreAction(MVT::i32, MVT::i8 , Expand); 115 setTruncStoreAction(MVT::i16, MVT::i8, Expand); 116 117 // SETOEQ and SETUNE require checking two conditions. 118 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand); 119 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand); 120 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand); 121 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand); 122 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand); 123 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand); 124 125 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this 126 // operation. 127 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); 128 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); 129 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); 130 131 if (Subtarget->is64Bit()) { 132 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); 133 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); 134 } else if (!UseSoftFloat) { 135 if (X86ScalarSSEf64) { 136 // We have an impenetrably clever algorithm for ui64->double only. 137 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom); 138 } 139 // We have an algorithm for SSE2, and we turn this into a 64-bit 140 // FILD for other targets. 141 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom); 142 } 143 144 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have 145 // this operation. 146 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); 147 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); 148 149 if (!UseSoftFloat) { 150 // SSE has no i16 to fp conversion, only i32 151 if (X86ScalarSSEf32) { 152 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); 153 // f32 and f64 cases are Legal, f80 case is not 154 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); 155 } else { 156 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); 157 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); 158 } 159 } else { 160 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); 161 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote); 162 } 163 164 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 165 // are Legal, f80 is custom lowered. 166 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); 167 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); 168 169 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have 170 // this operation. 171 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); 172 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); 173 174 if (X86ScalarSSEf32) { 175 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); 176 // f32 and f64 cases are Legal, f80 case is not 177 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); 178 } else { 179 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); 180 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); 181 } 182 183 // Handle FP_TO_UINT by promoting the destination to a larger signed 184 // conversion. 185 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); 186 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); 187 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); 188 189 if (Subtarget->is64Bit()) { 190 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); 191 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); 192 } else if (!UseSoftFloat) { 193 if (X86ScalarSSEf32 && !Subtarget->hasSSE3()) 194 // Expand FP_TO_UINT into a select. 195 // FIXME: We would like to use a Custom expander here eventually to do 196 // the optimal thing for SSE vs. the default expansion in the legalizer. 197 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); 198 else 199 // With SSE3 we can use fisttpll to convert to a signed i64; without 200 // SSE, we're stuck with a fistpll. 201 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom); 202 } 203 204 // TODO: when we have SSE, these could be more efficient, by using movd/movq. 205 if (!X86ScalarSSEf64) { 206 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); 207 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); 208 } 209 210 // Scalar integer divide and remainder are lowered to use operations that 211 // produce two results, to match the available instructions. This exposes 212 // the two-result form to trivial CSE, which is able to combine x/y and x%y 213 // into a single instruction. 214 // 215 // Scalar integer multiply-high is also lowered to use two-result 216 // operations, to match the available instructions. However, plain multiply 217 // (low) operations are left as Legal, as there are single-result 218 // instructions for this in x86. Using the two-result multiply instructions 219 // when both high and low results are needed must be arranged by dagcombine. 220 setOperationAction(ISD::MULHS , MVT::i8 , Expand); 221 setOperationAction(ISD::MULHU , MVT::i8 , Expand); 222 setOperationAction(ISD::SDIV , MVT::i8 , Expand); 223 setOperationAction(ISD::UDIV , MVT::i8 , Expand); 224 setOperationAction(ISD::SREM , MVT::i8 , Expand); 225 setOperationAction(ISD::UREM , MVT::i8 , Expand); 226 setOperationAction(ISD::MULHS , MVT::i16 , Expand); 227 setOperationAction(ISD::MULHU , MVT::i16 , Expand); 228 setOperationAction(ISD::SDIV , MVT::i16 , Expand); 229 setOperationAction(ISD::UDIV , MVT::i16 , Expand); 230 setOperationAction(ISD::SREM , MVT::i16 , Expand); 231 setOperationAction(ISD::UREM , MVT::i16 , Expand); 232 setOperationAction(ISD::MULHS , MVT::i32 , Expand); 233 setOperationAction(ISD::MULHU , MVT::i32 , Expand); 234 setOperationAction(ISD::SDIV , MVT::i32 , Expand); 235 setOperationAction(ISD::UDIV , MVT::i32 , Expand); 236 setOperationAction(ISD::SREM , MVT::i32 , Expand); 237 setOperationAction(ISD::UREM , MVT::i32 , Expand); 238 setOperationAction(ISD::MULHS , MVT::i64 , Expand); 239 setOperationAction(ISD::MULHU , MVT::i64 , Expand); 240 setOperationAction(ISD::SDIV , MVT::i64 , Expand); 241 setOperationAction(ISD::UDIV , MVT::i64 , Expand); 242 setOperationAction(ISD::SREM , MVT::i64 , Expand); 243 setOperationAction(ISD::UREM , MVT::i64 , Expand); 244 245 setOperationAction(ISD::BR_JT , MVT::Other, Expand); 246 setOperationAction(ISD::BRCOND , MVT::Other, Custom); 247 setOperationAction(ISD::BR_CC , MVT::Other, Expand); 248 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); 249 if (Subtarget->is64Bit()) 250 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); 251 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); 252 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); 253 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); 254 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); 255 setOperationAction(ISD::FREM , MVT::f32 , Expand); 256 setOperationAction(ISD::FREM , MVT::f64 , Expand); 257 setOperationAction(ISD::FREM , MVT::f80 , Expand); 258 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom); 259 260 setOperationAction(ISD::CTPOP , MVT::i8 , Expand); 261 setOperationAction(ISD::CTTZ , MVT::i8 , Custom); 262 setOperationAction(ISD::CTLZ , MVT::i8 , Custom); 263 setOperationAction(ISD::CTPOP , MVT::i16 , Expand); 264 setOperationAction(ISD::CTTZ , MVT::i16 , Custom); 265 setOperationAction(ISD::CTLZ , MVT::i16 , Custom); 266 setOperationAction(ISD::CTPOP , MVT::i32 , Expand); 267 setOperationAction(ISD::CTTZ , MVT::i32 , Custom); 268 setOperationAction(ISD::CTLZ , MVT::i32 , Custom); 269 if (Subtarget->is64Bit()) { 270 setOperationAction(ISD::CTPOP , MVT::i64 , Expand); 271 setOperationAction(ISD::CTTZ , MVT::i64 , Custom); 272 setOperationAction(ISD::CTLZ , MVT::i64 , Custom); 273 } 274 275 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); 276 setOperationAction(ISD::BSWAP , MVT::i16 , Expand); 277 278 // These should be promoted to a larger select which is supported. 279 setOperationAction(ISD::SELECT , MVT::i1 , Promote); 280 setOperationAction(ISD::SELECT , MVT::i8 , Promote); 281 // X86 wants to expand cmov itself. 282 setOperationAction(ISD::SELECT , MVT::i16 , Custom); 283 setOperationAction(ISD::SELECT , MVT::i32 , Custom); 284 setOperationAction(ISD::SELECT , MVT::f32 , Custom); 285 setOperationAction(ISD::SELECT , MVT::f64 , Custom); 286 setOperationAction(ISD::SELECT , MVT::f80 , Custom); 287 setOperationAction(ISD::SETCC , MVT::i8 , Custom); 288 setOperationAction(ISD::SETCC , MVT::i16 , Custom); 289 setOperationAction(ISD::SETCC , MVT::i32 , Custom); 290 setOperationAction(ISD::SETCC , MVT::f32 , Custom); 291 setOperationAction(ISD::SETCC , MVT::f64 , Custom); 292 setOperationAction(ISD::SETCC , MVT::f80 , Custom); 293 if (Subtarget->is64Bit()) { 294 setOperationAction(ISD::SELECT , MVT::i64 , Custom); 295 setOperationAction(ISD::SETCC , MVT::i64 , Custom); 296 } 297 // X86 ret instruction may pop stack. 298 setOperationAction(ISD::RET , MVT::Other, Custom); 299 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); 300 301 // Darwin ABI issue. 302 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); 303 setOperationAction(ISD::JumpTable , MVT::i32 , Custom); 304 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); 305 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom); 306 if (Subtarget->is64Bit()) 307 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 308 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); 309 if (Subtarget->is64Bit()) { 310 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); 311 setOperationAction(ISD::JumpTable , MVT::i64 , Custom); 312 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); 313 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); 314 } 315 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) 316 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); 317 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); 318 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); 319 if (Subtarget->is64Bit()) { 320 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom); 321 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom); 322 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom); 323 } 324 325 if (Subtarget->hasSSE1()) 326 setOperationAction(ISD::PREFETCH , MVT::Other, Legal); 327 328 if (!Subtarget->hasSSE2()) 329 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand); 330 331 // Expand certain atomics 332 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom); 333 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom); 334 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 335 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 336 337 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom); 338 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom); 339 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); 340 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); 341 342 if (!Subtarget->is64Bit()) { 343 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); 344 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); 345 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); 346 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); 347 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); 348 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom); 349 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); 350 } 351 352 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion. 353 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand); 354 // FIXME - use subtarget debug flags 355 if (!Subtarget->isTargetDarwin() && 356 !Subtarget->isTargetELF() && 357 !Subtarget->isTargetCygMing()) { 358 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand); 359 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); 360 } 361 362 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand); 363 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand); 364 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); 365 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); 366 if (Subtarget->is64Bit()) { 367 setExceptionPointerRegister(X86::RAX); 368 setExceptionSelectorRegister(X86::RDX); 369 } else { 370 setExceptionPointerRegister(X86::EAX); 371 setExceptionSelectorRegister(X86::EDX); 372 } 373 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); 374 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom); 375 376 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom); 377 378 setOperationAction(ISD::TRAP, MVT::Other, Legal); 379 380 // VASTART needs to be custom lowered to use the VarArgsFrameIndex 381 setOperationAction(ISD::VASTART , MVT::Other, Custom); 382 setOperationAction(ISD::VAEND , MVT::Other, Expand); 383 if (Subtarget->is64Bit()) { 384 setOperationAction(ISD::VAARG , MVT::Other, Custom); 385 setOperationAction(ISD::VACOPY , MVT::Other, Custom); 386 } else { 387 setOperationAction(ISD::VAARG , MVT::Other, Expand); 388 setOperationAction(ISD::VACOPY , MVT::Other, Expand); 389 } 390 391 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 392 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 393 if (Subtarget->is64Bit()) 394 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 395 if (Subtarget->isTargetCygMing()) 396 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 397 else 398 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 399 400 if (!UseSoftFloat && X86ScalarSSEf64) { 401 // f32 and f64 use SSE. 402 // Set up the FP register classes. 403 addRegisterClass(MVT::f32, X86::FR32RegisterClass); 404 addRegisterClass(MVT::f64, X86::FR64RegisterClass); 405 406 // Use ANDPD to simulate FABS. 407 setOperationAction(ISD::FABS , MVT::f64, Custom); 408 setOperationAction(ISD::FABS , MVT::f32, Custom); 409 410 // Use XORP to simulate FNEG. 411 setOperationAction(ISD::FNEG , MVT::f64, Custom); 412 setOperationAction(ISD::FNEG , MVT::f32, Custom); 413 414 // Use ANDPD and ORPD to simulate FCOPYSIGN. 415 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 416 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 417 418 // We don't support sin/cos/fmod 419 setOperationAction(ISD::FSIN , MVT::f64, Expand); 420 setOperationAction(ISD::FCOS , MVT::f64, Expand); 421 setOperationAction(ISD::FSIN , MVT::f32, Expand); 422 setOperationAction(ISD::FCOS , MVT::f32, Expand); 423 424 // Expand FP immediates into loads from the stack, except for the special 425 // cases we handle. 426 addLegalFPImmediate(APFloat(+0.0)); // xorpd 427 addLegalFPImmediate(APFloat(+0.0f)); // xorps 428 } else if (!UseSoftFloat && X86ScalarSSEf32) { 429 // Use SSE for f32, x87 for f64. 430 // Set up the FP register classes. 431 addRegisterClass(MVT::f32, X86::FR32RegisterClass); 432 addRegisterClass(MVT::f64, X86::RFP64RegisterClass); 433 434 // Use ANDPS to simulate FABS. 435 setOperationAction(ISD::FABS , MVT::f32, Custom); 436 437 // Use XORP to simulate FNEG. 438 setOperationAction(ISD::FNEG , MVT::f32, Custom); 439 440 setOperationAction(ISD::UNDEF, MVT::f64, Expand); 441 442 // Use ANDPS and ORPS to simulate FCOPYSIGN. 443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 445 446 // We don't support sin/cos/fmod 447 setOperationAction(ISD::FSIN , MVT::f32, Expand); 448 setOperationAction(ISD::FCOS , MVT::f32, Expand); 449 450 // Special cases we handle for FP constants. 451 addLegalFPImmediate(APFloat(+0.0f)); // xorps 452 addLegalFPImmediate(APFloat(+0.0)); // FLD0 453 addLegalFPImmediate(APFloat(+1.0)); // FLD1 454 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS 455 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS 456 457 if (!UnsafeFPMath) { 458 setOperationAction(ISD::FSIN , MVT::f64 , Expand); 459 setOperationAction(ISD::FCOS , MVT::f64 , Expand); 460 } 461 } else if (!UseSoftFloat) { 462 // f32 and f64 in x87. 463 // Set up the FP register classes. 464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass); 465 addRegisterClass(MVT::f32, X86::RFP32RegisterClass); 466 467 setOperationAction(ISD::UNDEF, MVT::f64, Expand); 468 setOperationAction(ISD::UNDEF, MVT::f32, Expand); 469 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 470 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 471 472 if (!UnsafeFPMath) { 473 setOperationAction(ISD::FSIN , MVT::f64 , Expand); 474 setOperationAction(ISD::FCOS , MVT::f64 , Expand); 475 } 476 addLegalFPImmediate(APFloat(+0.0)); // FLD0 477 addLegalFPImmediate(APFloat(+1.0)); // FLD1 478 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS 479 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS 480 addLegalFPImmediate(APFloat(+0.0f)); // FLD0 481 addLegalFPImmediate(APFloat(+1.0f)); // FLD1 482 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS 483 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS 484 } 485 486 // Long double always uses X87. 487 if (!UseSoftFloat) { 488 addRegisterClass(MVT::f80, X86::RFP80RegisterClass); 489 setOperationAction(ISD::UNDEF, MVT::f80, Expand); 490 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand); 491 { 492 bool ignored; 493 APFloat TmpFlt(+0.0); 494 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, 495 &ignored); 496 addLegalFPImmediate(TmpFlt); // FLD0 497 TmpFlt.changeSign(); 498 addLegalFPImmediate(TmpFlt); // FLD0/FCHS 499 APFloat TmpFlt2(+1.0); 500 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, 501 &ignored); 502 addLegalFPImmediate(TmpFlt2); // FLD1 503 TmpFlt2.changeSign(); 504 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS 505 } 506 507 if (!UnsafeFPMath) { 508 setOperationAction(ISD::FSIN , MVT::f80 , Expand); 509 setOperationAction(ISD::FCOS , MVT::f80 , Expand); 510 } 511 } 512 513 // Always use a library call for pow. 514 setOperationAction(ISD::FPOW , MVT::f32 , Expand); 515 setOperationAction(ISD::FPOW , MVT::f64 , Expand); 516 setOperationAction(ISD::FPOW , MVT::f80 , Expand); 517 518 setOperationAction(ISD::FLOG, MVT::f80, Expand); 519 setOperationAction(ISD::FLOG2, MVT::f80, Expand); 520 setOperationAction(ISD::FLOG10, MVT::f80, Expand); 521 setOperationAction(ISD::FEXP, MVT::f80, Expand); 522 setOperationAction(ISD::FEXP2, MVT::f80, Expand); 523 524 // First set operation action for all vector types to either promote 525 // (for widening) or expand (for scalarization). Then we will selectively 526 // turn on ones that can be effectively codegen'd. 527 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 528 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { 529 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand); 530 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand); 531 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand); 532 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand); 533 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand); 534 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand); 535 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand); 536 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand); 537 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand); 538 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand); 539 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand); 540 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand); 541 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand); 542 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand); 543 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand); 544 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand); 545 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand); 546 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand); 547 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand); 548 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand); 549 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand); 550 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand); 551 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand); 552 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand); 553 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand); 554 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand); 555 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand); 556 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand); 557 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand); 558 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand); 559 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand); 560 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand); 561 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand); 562 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand); 563 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand); 564 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand); 565 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand); 566 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand); 567 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand); 568 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand); 569 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand); 570 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand); 571 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand); 572 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand); 573 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand); 574 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand); 575 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand); 576 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand); 577 } 578 579 // FIXME: In order to prevent SSE instructions being expanded to MMX ones 580 // with -msoft-float, disable use of MMX as well. 581 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) { 582 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); 583 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); 584 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); 585 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass); 586 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass); 587 588 setOperationAction(ISD::ADD, MVT::v8i8, Legal); 589 setOperationAction(ISD::ADD, MVT::v4i16, Legal); 590 setOperationAction(ISD::ADD, MVT::v2i32, Legal); 591 setOperationAction(ISD::ADD, MVT::v1i64, Legal); 592 593 setOperationAction(ISD::SUB, MVT::v8i8, Legal); 594 setOperationAction(ISD::SUB, MVT::v4i16, Legal); 595 setOperationAction(ISD::SUB, MVT::v2i32, Legal); 596 setOperationAction(ISD::SUB, MVT::v1i64, Legal); 597 598 setOperationAction(ISD::MULHS, MVT::v4i16, Legal); 599 setOperationAction(ISD::MUL, MVT::v4i16, Legal); 600 601 setOperationAction(ISD::AND, MVT::v8i8, Promote); 602 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64); 603 setOperationAction(ISD::AND, MVT::v4i16, Promote); 604 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64); 605 setOperationAction(ISD::AND, MVT::v2i32, Promote); 606 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64); 607 setOperationAction(ISD::AND, MVT::v1i64, Legal); 608 609 setOperationAction(ISD::OR, MVT::v8i8, Promote); 610 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64); 611 setOperationAction(ISD::OR, MVT::v4i16, Promote); 612 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64); 613 setOperationAction(ISD::OR, MVT::v2i32, Promote); 614 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64); 615 setOperationAction(ISD::OR, MVT::v1i64, Legal); 616 617 setOperationAction(ISD::XOR, MVT::v8i8, Promote); 618 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64); 619 setOperationAction(ISD::XOR, MVT::v4i16, Promote); 620 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64); 621 setOperationAction(ISD::XOR, MVT::v2i32, Promote); 622 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64); 623 setOperationAction(ISD::XOR, MVT::v1i64, Legal); 624 625 setOperationAction(ISD::LOAD, MVT::v8i8, Promote); 626 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64); 627 setOperationAction(ISD::LOAD, MVT::v4i16, Promote); 628 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64); 629 setOperationAction(ISD::LOAD, MVT::v2i32, Promote); 630 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64); 631 setOperationAction(ISD::LOAD, MVT::v2f32, Promote); 632 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64); 633 setOperationAction(ISD::LOAD, MVT::v1i64, Legal); 634 635 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom); 636 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); 637 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom); 638 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom); 639 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom); 640 641 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom); 642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); 643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom); 644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom); 645 646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom); 647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom); 648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom); 649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom); 650 651 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); 652 653 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand); 654 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand); 655 setOperationAction(ISD::SELECT, MVT::v8i8, Promote); 656 setOperationAction(ISD::SELECT, MVT::v4i16, Promote); 657 setOperationAction(ISD::SELECT, MVT::v2i32, Promote); 658 setOperationAction(ISD::SELECT, MVT::v1i64, Custom); 659 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom); 660 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom); 661 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom); 662 } 663 664 if (!UseSoftFloat && Subtarget->hasSSE1()) { 665 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); 666 667 setOperationAction(ISD::FADD, MVT::v4f32, Legal); 668 setOperationAction(ISD::FSUB, MVT::v4f32, Legal); 669 setOperationAction(ISD::FMUL, MVT::v4f32, Legal); 670 setOperationAction(ISD::FDIV, MVT::v4f32, Legal); 671 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); 672 setOperationAction(ISD::FNEG, MVT::v4f32, Custom); 673 setOperationAction(ISD::LOAD, MVT::v4f32, Legal); 674 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); 676 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); 677 setOperationAction(ISD::SELECT, MVT::v4f32, Custom); 678 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom); 679 } 680 681 if (!UseSoftFloat && Subtarget->hasSSE2()) { 682 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); 683 684 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM 685 // registers cannot be used even for integer operations. 686 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); 687 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); 688 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); 689 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); 690 691 setOperationAction(ISD::ADD, MVT::v16i8, Legal); 692 setOperationAction(ISD::ADD, MVT::v8i16, Legal); 693 setOperationAction(ISD::ADD, MVT::v4i32, Legal); 694 setOperationAction(ISD::ADD, MVT::v2i64, Legal); 695 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 696 setOperationAction(ISD::SUB, MVT::v16i8, Legal); 697 setOperationAction(ISD::SUB, MVT::v8i16, Legal); 698 setOperationAction(ISD::SUB, MVT::v4i32, Legal); 699 setOperationAction(ISD::SUB, MVT::v2i64, Legal); 700 setOperationAction(ISD::MUL, MVT::v8i16, Legal); 701 setOperationAction(ISD::FADD, MVT::v2f64, Legal); 702 setOperationAction(ISD::FSUB, MVT::v2f64, Legal); 703 setOperationAction(ISD::FMUL, MVT::v2f64, Legal); 704 setOperationAction(ISD::FDIV, MVT::v2f64, Legal); 705 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); 706 setOperationAction(ISD::FNEG, MVT::v2f64, Custom); 707 708 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom); 709 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom); 710 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom); 711 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom); 712 713 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); 714 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); 715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); 716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); 717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 718 719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt. 720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) { 721 MVT VT = (MVT::SimpleValueType)i; 722 // Do not attempt to custom lower non-power-of-2 vectors 723 if (!isPowerOf2_32(VT.getVectorNumElements())) 724 continue; 725 // Do not attempt to custom lower non-128-bit vectors 726 if (!VT.is128BitVector()) 727 continue; 728 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 729 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 731 } 732 733 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); 734 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); 735 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); 736 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); 737 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); 738 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); 739 740 if (Subtarget->is64Bit()) { 741 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom); 742 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); 743 } 744 745 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. 746 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) { 747 MVT VT = (MVT::SimpleValueType)i; 748 749 // Do not attempt to promote non-128-bit vectors 750 if (!VT.is128BitVector()) { 751 continue; 752 } 753 setOperationAction(ISD::AND, VT, Promote); 754 AddPromotedToType (ISD::AND, VT, MVT::v2i64); 755 setOperationAction(ISD::OR, VT, Promote); 756 AddPromotedToType (ISD::OR, VT, MVT::v2i64); 757 setOperationAction(ISD::XOR, VT, Promote); 758 AddPromotedToType (ISD::XOR, VT, MVT::v2i64); 759 setOperationAction(ISD::LOAD, VT, Promote); 760 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64); 761 setOperationAction(ISD::SELECT, VT, Promote); 762 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64); 763 } 764 765 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 766 767 // Custom lower v2i64 and v2f64 selects. 768 setOperationAction(ISD::LOAD, MVT::v2f64, Legal); 769 setOperationAction(ISD::LOAD, MVT::v2i64, Legal); 770 setOperationAction(ISD::SELECT, MVT::v2f64, Custom); 771 setOperationAction(ISD::SELECT, MVT::v2i64, Custom); 772 773 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); 774 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); 775 if (!DisableMMX && Subtarget->hasMMX()) { 776 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom); 777 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom); 778 } 779 } 780 781 if (Subtarget->hasSSE41()) { 782 // FIXME: Do we need to handle scalar-to-vector here? 783 setOperationAction(ISD::MUL, MVT::v4i32, Legal); 784 785 // i8 and i16 vectors are custom , because the source register and source 786 // source memory operand types are not the same width. f32 vectors are 787 // custom since the immediate controlling the insert encodes additional 788 // information. 789 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom); 790 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); 791 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); 792 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 793 794 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom); 795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom); 796 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom); 797 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); 798 799 if (Subtarget->is64Bit()) { 800 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal); 801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal); 802 } 803 } 804 805 if (Subtarget->hasSSE42()) { 806 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom); 807 } 808 809 if (!UseSoftFloat && Subtarget->hasAVX()) { 810 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass); 811 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass); 812 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass); 813 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass); 814 815 setOperationAction(ISD::LOAD, MVT::v8f32, Legal); 816 setOperationAction(ISD::LOAD, MVT::v8i32, Legal); 817 setOperationAction(ISD::LOAD, MVT::v4f64, Legal); 818 setOperationAction(ISD::LOAD, MVT::v4i64, Legal); 819 setOperationAction(ISD::FADD, MVT::v8f32, Legal); 820 setOperationAction(ISD::FSUB, MVT::v8f32, Legal); 821 setOperationAction(ISD::FMUL, MVT::v8f32, Legal); 822 setOperationAction(ISD::FDIV, MVT::v8f32, Legal); 823 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal); 824 setOperationAction(ISD::FNEG, MVT::v8f32, Custom); 825 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom); 826 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom); 827 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom); 828 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom); 829 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom); 830 831 // Operations to consider commented out -v16i16 v32i8 832 //setOperationAction(ISD::ADD, MVT::v16i16, Legal); 833 setOperationAction(ISD::ADD, MVT::v8i32, Custom); 834 setOperationAction(ISD::ADD, MVT::v4i64, Custom); 835 //setOperationAction(ISD::SUB, MVT::v32i8, Legal); 836 //setOperationAction(ISD::SUB, MVT::v16i16, Legal); 837 setOperationAction(ISD::SUB, MVT::v8i32, Custom); 838 setOperationAction(ISD::SUB, MVT::v4i64, Custom); 839 //setOperationAction(ISD::MUL, MVT::v16i16, Legal); 840 setOperationAction(ISD::FADD, MVT::v4f64, Legal); 841 setOperationAction(ISD::FSUB, MVT::v4f64, Legal); 842 setOperationAction(ISD::FMUL, MVT::v4f64, Legal); 843 setOperationAction(ISD::FDIV, MVT::v4f64, Legal); 844 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal); 845 setOperationAction(ISD::FNEG, MVT::v4f64, Custom); 846 847 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom); 848 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom); 849 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom); 850 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom); 851 852 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom); 853 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom); 854 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom); 855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom); 856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom); 857 858 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom); 859 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom); 860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom); 861 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom); 862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom); 863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom); 864 865 #if 0 866 // Not sure we want to do this since there are no 256-bit integer 867 // operations in AVX 868 869 // Custom lower build_vector, vector_shuffle, and extract_vector_elt. 870 // This includes 256-bit vectors 871 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) { 872 MVT VT = (MVT::SimpleValueType)i; 873 874 // Do not attempt to custom lower non-power-of-2 vectors 875 if (!isPowerOf2_32(VT.getVectorNumElements())) 876 continue; 877 878 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 879 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 881 } 882 883 if (Subtarget->is64Bit()) { 884 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom); 885 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom); 886 } 887 #endif 888 889 #if 0 890 // Not sure we want to do this since there are no 256-bit integer 891 // operations in AVX 892 893 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64. 894 // Including 256-bit vectors 895 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) { 896 MVT VT = (MVT::SimpleValueType)i; 897 898 if (!VT.is256BitVector()) { 899 continue; 900 } 901 setOperationAction(ISD::AND, VT, Promote); 902 AddPromotedToType (ISD::AND, VT, MVT::v4i64); 903 setOperationAction(ISD::OR, VT, Promote); 904 AddPromotedToType (ISD::OR, VT, MVT::v4i64); 905 setOperationAction(ISD::XOR, VT, Promote); 906 AddPromotedToType (ISD::XOR, VT, MVT::v4i64); 907 setOperationAction(ISD::LOAD, VT, Promote); 908 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64); 909 setOperationAction(ISD::SELECT, VT, Promote); 910 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64); 911 } 912 913 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 914 #endif 915 } 916 917 // We want to custom lower some of our intrinsics. 918 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 919 920 // Add/Sub/Mul with overflow operations are custom lowered. 921 setOperationAction(ISD::SADDO, MVT::i32, Custom); 922 setOperationAction(ISD::SADDO, MVT::i64, Custom); 923 setOperationAction(ISD::UADDO, MVT::i32, Custom); 924 setOperationAction(ISD::UADDO, MVT::i64, Custom); 925 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 926 setOperationAction(ISD::SSUBO, MVT::i64, Custom); 927 setOperationAction(ISD::USUBO, MVT::i32, Custom); 928 setOperationAction(ISD::USUBO, MVT::i64, Custom); 929 setOperationAction(ISD::SMULO, MVT::i32, Custom); 930 setOperationAction(ISD::SMULO, MVT::i64, Custom); 931 932 if (!Subtarget->is64Bit()) { 933 // These libcalls are not available in 32-bit. 934 setLibcallName(RTLIB::SHL_I128, 0); 935 setLibcallName(RTLIB::SRL_I128, 0); 936 setLibcallName(RTLIB::SRA_I128, 0); 937 } 938 939 // We have target-specific dag combine patterns for the following nodes: 940 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 941 setTargetDAGCombine(ISD::BUILD_VECTOR); 942 setTargetDAGCombine(ISD::SELECT); 943 setTargetDAGCombine(ISD::SHL); 944 setTargetDAGCombine(ISD::SRA); 945 setTargetDAGCombine(ISD::SRL); 946 setTargetDAGCombine(ISD::STORE); 947 setTargetDAGCombine(ISD::MEMBARRIER); 948 if (Subtarget->is64Bit()) 949 setTargetDAGCombine(ISD::MUL); 950 951 computeRegisterProperties(); 952 953 // FIXME: These should be based on subtarget info. Plus, the values should 954 // be smaller when we are in optimizing for size mode. 955 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores 956 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores 957 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores 958 allowUnalignedMemoryAccesses = true; // x86 supports it! 959 setPrefLoopAlignment(16); 960 benefitFromCodePlacementOpt = true; 961 } 962 963 964 MVT X86TargetLowering::getSetCCResultType(MVT VT) const { 965 return MVT::i8; 966 } 967 968 969 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine 970 /// the desired ByVal argument alignment. 971 static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) { 972 if (MaxAlign == 16) 973 return; 974 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { 975 if (VTy->getBitWidth() == 128) 976 MaxAlign = 16; 977 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 978 unsigned EltAlign = 0; 979 getMaxByValAlign(ATy->getElementType(), EltAlign); 980 if (EltAlign > MaxAlign) 981 MaxAlign = EltAlign; 982 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { 983 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 984 unsigned EltAlign = 0; 985 getMaxByValAlign(STy->getElementType(i), EltAlign); 986 if (EltAlign > MaxAlign) 987 MaxAlign = EltAlign; 988 if (MaxAlign == 16) 989 break; 990 } 991 } 992 return; 993 } 994 995 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate 996 /// function arguments in the caller parameter area. For X86, aggregates 997 /// that contain SSE vectors are placed at 16-byte boundaries while the rest 998 /// are at 4-byte boundaries. 999 unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const { 1000 if (Subtarget->is64Bit()) { 1001 // Max of 8 and alignment of type. 1002 unsigned TyAlign = TD->getABITypeAlignment(Ty); 1003 if (TyAlign > 8) 1004 return TyAlign; 1005 return 8; 1006 } 1007 1008 unsigned Align = 4; 1009 if (Subtarget->hasSSE1()) 1010 getMaxByValAlign(Ty, Align); 1011 return Align; 1012 } 1013 1014 /// getOptimalMemOpType - Returns the target specific optimal type for load 1015 /// and store operations as a result of memset, memcpy, and memmove 1016 /// lowering. It returns MVT::iAny if SelectionDAG should be responsible for 1017 /// determining it. 1018 MVT 1019 X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align, 1020 bool isSrcConst, bool isSrcStr, 1021 SelectionDAG &DAG) const { 1022 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like 1023 // linux. This is because the stack realignment code can't handle certain 1024 // cases like PR2962. This should be removed when PR2962 is fixed. 1025 const Function *F = DAG.getMachineFunction().getFunction(); 1026 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat); 1027 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) { 1028 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16) 1029 return MVT::v4i32; 1030 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16) 1031 return MVT::v4f32; 1032 } 1033 if (Subtarget->is64Bit() && Size >= 8) 1034 return MVT::i64; 1035 return MVT::i32; 1036 } 1037 1038 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC 1039 /// jumptable. 1040 SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table, 1041 SelectionDAG &DAG) const { 1042 if (usesGlobalOffsetTable()) 1043 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy()); 1044 if (!Subtarget->is64Bit()) 1045 // This doesn't have DebugLoc associated with it, but is not really the 1046 // same as a Register. 1047 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(), 1048 getPointerTy()); 1049 return Table; 1050 } 1051 1052 /// getFunctionAlignment - Return the Log2 alignment of this function. 1053 unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const { 1054 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4; 1055 } 1056 1057 //===----------------------------------------------------------------------===// 1058 // Return Value Calling Convention Implementation 1059 //===----------------------------------------------------------------------===// 1060 1061 #include "X86GenCallingConv.inc" 1062 1063 /// LowerRET - Lower an ISD::RET node. 1064 SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) { 1065 DebugLoc dl = Op.getDebugLoc(); 1066 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args"); 1067 1068 SmallVector<CCValAssign, 16> RVLocs; 1069 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); 1070 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); 1071 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext()); 1072 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86); 1073 1074 // If this is the first return lowered for this function, add the regs to the 1075 // liveout set for the function. 1076 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { 1077 for (unsigned i = 0; i != RVLocs.size(); ++i) 1078 if (RVLocs[i].isRegLoc()) 1079 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); 1080 } 1081 SDValue Chain = Op.getOperand(0); 1082 1083 // Handle tail call return. 1084 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL); 1085 if (Chain.getOpcode() == X86ISD::TAILCALL) { 1086 SDValue TailCall = Chain; 1087 SDValue TargetAddress = TailCall.getOperand(1); 1088 SDValue StackAdjustment = TailCall.getOperand(2); 1089 assert(((TargetAddress.getOpcode() == ISD::Register && 1090 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX || 1091 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R11)) || 1092 TargetAddress.getOpcode() == ISD::TargetExternalSymbol || 1093 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) && 1094 "Expecting an global address, external symbol, or register"); 1095 assert(StackAdjustment.getOpcode() == ISD::Constant && 1096 "Expecting a const value"); 1097 1098 SmallVector<SDValue,8> Operands; 1099 Operands.push_back(Chain.getOperand(0)); 1100 Operands.push_back(TargetAddress); 1101 Operands.push_back(StackAdjustment); 1102 // Copy registers used by the call. Last operand is a flag so it is not 1103 // copied. 1104 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) { 1105 Operands.push_back(Chain.getOperand(i)); 1106 } 1107 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0], 1108 Operands.size()); 1109 } 1110 1111 // Regular return. 1112 SDValue Flag; 1113 1114 SmallVector<SDValue, 6> RetOps; 1115 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 1116 // Operand #1 = Bytes To Pop 1117 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16)); 1118 1119 // Copy the result values into the output registers. 1120 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1121 CCValAssign &VA = RVLocs[i]; 1122 assert(VA.isRegLoc() && "Can only return in registers!"); 1123 SDValue ValToCopy = Op.getOperand(i*2+1); 1124 1125 // Returns in ST0/ST1 are handled specially: these are pushed as operands to 1126 // the RET instruction and handled by the FP Stackifier. 1127 if (VA.getLocReg() == X86::ST0 || 1128 VA.getLocReg() == X86::ST1) { 1129 // If this is a copy from an xmm register to ST(0), use an FPExtend to 1130 // change the value to the FP stack register class. 1131 if (isScalarFPTypeInSSEReg(VA.getValVT())) 1132 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy); 1133 RetOps.push_back(ValToCopy); 1134 // Don't emit a copytoreg. 1135 continue; 1136 } 1137 1138 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64 1139 // which is returned in RAX / RDX. 1140 if (Subtarget->is64Bit()) { 1141 MVT ValVT = ValToCopy.getValueType(); 1142 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) { 1143 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy); 1144 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) 1145 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy); 1146 } 1147 } 1148 1149 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag); 1150 Flag = Chain.getValue(1); 1151 } 1152 1153 // The x86-64 ABI for returning structs by value requires that we copy 1154 // the sret argument into %rax for the return. We saved the argument into 1155 // a virtual register in the entry block, so now we copy the value out 1156 // and into %rax. 1157 if (Subtarget->is64Bit() && 1158 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { 1159 MachineFunction &MF = DAG.getMachineFunction(); 1160 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); 1161 unsigned Reg = FuncInfo->getSRetReturnReg(); 1162 if (!Reg) { 1163 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64)); 1164 FuncInfo->setSRetReturnReg(Reg); 1165 } 1166 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy()); 1167 1168 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag); 1169 Flag = Chain.getValue(1); 1170 } 1171 1172 RetOps[0] = Chain; // Update chain. 1173 1174 // Add the flag if we have it. 1175 if (Flag.getNode()) 1176 RetOps.push_back(Flag); 1177 1178 return DAG.getNode(X86ISD::RET_FLAG, dl, 1179 MVT::Other, &RetOps[0], RetOps.size()); 1180 } 1181 1182 1183 /// LowerCallResult - Lower the result values of an ISD::CALL into the 1184 /// appropriate copies out of appropriate physical registers. This assumes that 1185 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call 1186 /// being lowered. The returns a SDNode with the same number of values as the 1187 /// ISD::CALL. 1188 SDNode *X86TargetLowering:: 1189 LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall, 1190 unsigned CallingConv, SelectionDAG &DAG) { 1191 1192 DebugLoc dl = TheCall->getDebugLoc(); 1193 // Assign locations to each value returned by this call. 1194 SmallVector<CCValAssign, 16> RVLocs; 1195 bool isVarArg = TheCall->isVarArg(); 1196 bool Is64Bit = Subtarget->is64Bit(); 1197 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), 1198 RVLocs, *DAG.getContext()); 1199 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86); 1200 1201 SmallVector<SDValue, 8> ResultVals; 1202 1203 // Copy all of the result registers out of their specified physreg. 1204 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1205 CCValAssign &VA = RVLocs[i]; 1206 MVT CopyVT = VA.getValVT(); 1207 1208 // If this is x86-64, and we disabled SSE, we can't return FP values 1209 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) && 1210 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) { 1211 llvm_report_error("SSE register return with SSE disabled"); 1212 } 1213 1214 // If this is a call to a function that returns an fp value on the floating 1215 // point stack, but where we prefer to use the value in xmm registers, copy 1216 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg. 1217 if ((VA.getLocReg() == X86::ST0 || 1218 VA.getLocReg() == X86::ST1) && 1219 isScalarFPTypeInSSEReg(VA.getValVT())) { 1220 CopyVT = MVT::f80; 1221 } 1222 1223 SDValue Val; 1224 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) { 1225 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64. 1226 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) { 1227 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), 1228 MVT::v2i64, InFlag).getValue(1); 1229 Val = Chain.getValue(0); 1230 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, 1231 Val, DAG.getConstant(0, MVT::i64)); 1232 } else { 1233 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), 1234 MVT::i64, InFlag).getValue(1); 1235 Val = Chain.getValue(0); 1236 } 1237 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val); 1238 } else { 1239 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), 1240 CopyVT, InFlag).getValue(1); 1241 Val = Chain.getValue(0); 1242 } 1243 InFlag = Chain.getValue(2); 1244 1245 if (CopyVT != VA.getValVT()) { 1246 // Round the F80 the right size, which also moves to the appropriate xmm 1247 // register. 1248 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val, 1249 // This truncation won't change the value. 1250 DAG.getIntPtrConstant(1)); 1251 } 1252 1253 ResultVals.push_back(Val); 1254 } 1255 1256 // Merge everything together with a MERGE_VALUES node. 1257 ResultVals.push_back(Chain); 1258 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(), 1259 &ResultVals[0], ResultVals.size()).getNode(); 1260 } 1261 1262 1263 //===----------------------------------------------------------------------===// 1264 // C & StdCall & Fast Calling Convention implementation 1265 //===----------------------------------------------------------------------===// 1266 // StdCall calling convention seems to be standard for many Windows' API 1267 // routines and around. It differs from C calling convention just a little: 1268 // callee should clean up the stack, not caller. Symbols should be also 1269 // decorated in some fancy way :) It doesn't support any vector arguments. 1270 // For info on fast calling convention see Fast Calling Convention (tail call) 1271 // implementation LowerX86_32FastCCCallTo. 1272 1273 /// CallIsStructReturn - Determines whether a CALL node uses struct return 1274 /// semantics. 1275 static bool CallIsStructReturn(CallSDNode *TheCall) { 1276 unsigned NumOps = TheCall->getNumArgs(); 1277 if (!NumOps) 1278 return false; 1279 1280 return TheCall->getArgFlags(0).isSRet(); 1281 } 1282 1283 /// ArgsAreStructReturn - Determines whether a function uses struct 1284 /// return semantics. 1285 static bool ArgsAreStructReturn(SDValue Op) { 1286 unsigned NumArgs = Op.getNode()->getNumValues() - 1; 1287 if (!NumArgs) 1288 return false; 1289 1290 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet(); 1291 } 1292 1293 /// IsCalleePop - Determines whether the callee is required to pop its 1294 /// own arguments. Callee pop is necessary to support tail calls. 1295 bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) { 1296 if (IsVarArg) 1297 return false; 1298 1299 switch (CallingConv) { 1300 default: 1301 return false; 1302 case CallingConv::X86_StdCall: 1303 return !Subtarget->is64Bit(); 1304 case CallingConv::X86_FastCall: 1305 return !Subtarget->is64Bit(); 1306 case CallingConv::Fast: 1307 return PerformTailCallOpt; 1308 } 1309 } 1310 1311 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the 1312 /// given CallingConvention value. 1313 CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const { 1314 if (Subtarget->is64Bit()) { 1315 if (Subtarget->isTargetWin64()) 1316 return CC_X86_Win64_C; 1317 else 1318 return CC_X86_64_C; 1319 } 1320 1321 if (CC == CallingConv::X86_FastCall) 1322 return CC_X86_32_FastCall; 1323 else if (CC == CallingConv::Fast) 1324 return CC_X86_32_FastCC; 1325 else 1326 return CC_X86_32_C; 1327 } 1328 1329 /// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to 1330 /// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node. 1331 NameDecorationStyle 1332 X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) { 1333 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 1334 if (CC == CallingConv::X86_FastCall) 1335 return FastCall; 1336 else if (CC == CallingConv::X86_StdCall) 1337 return StdCall; 1338 return None; 1339 } 1340 1341 1342 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified 1343 /// by "Src" to address "Dst" with size and alignment information specified by 1344 /// the specific parameter attribute. The copy will be passed as a byval 1345 /// function parameter. 1346 static SDValue 1347 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, 1348 ISD::ArgFlagsTy Flags, SelectionDAG &DAG, 1349 DebugLoc dl) { 1350 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32); 1351 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), 1352 /*AlwaysInline=*/true, NULL, 0, NULL, 0); 1353 } 1354 1355 SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG, 1356 const CCValAssign &VA, 1357 MachineFrameInfo *MFI, 1358 unsigned CC, 1359 SDValue Root, unsigned i) { 1360 // Create the nodes corresponding to a load from this parameter slot. 1361 ISD::ArgFlagsTy Flags = 1362 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags(); 1363 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt; 1364 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal(); 1365 1366 // FIXME: For now, all byval parameter objects are marked mutable. This can be 1367 // changed with more analysis. 1368 // In case of tail call optimization mark all arguments mutable. Since they 1369 // could be overwritten by lowering of arguments in case of a tail call. 1370 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, 1371 VA.getLocMemOffset(), isImmutable); 1372 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 1373 if (Flags.isByVal()) 1374 return FIN; 1375 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN, 1376 PseudoSourceValue::getFixedStack(FI), 0); 1377 } 1378 1379 SDValue 1380 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) { 1381 MachineFunction &MF = DAG.getMachineFunction(); 1382 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); 1383 DebugLoc dl = Op.getDebugLoc(); 1384 1385 const Function* Fn = MF.getFunction(); 1386 if (Fn->hasExternalLinkage() && 1387 Subtarget->isTargetCygMing() && 1388 Fn->getName() == "main") 1389 FuncInfo->setForceFramePointer(true); 1390 1391 // Decorate the function name. 1392 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op)); 1393 1394 MachineFrameInfo *MFI = MF.getFrameInfo(); 1395 SDValue Root = Op.getOperand(0); 1396 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0; 1397 unsigned CC = MF.getFunction()->getCallingConv(); 1398 bool Is64Bit = Subtarget->is64Bit(); 1399 bool IsWin64 = Subtarget->isTargetWin64(); 1400 1401 assert(!(isVarArg && CC == CallingConv::Fast) && 1402 "Var args not supported with calling convention fastcc"); 1403 1404 // Assign locations to all of the incoming arguments. 1405 SmallVector<CCValAssign, 16> ArgLocs; 1406 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext()); 1407 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC)); 1408 1409 SmallVector<SDValue, 8> ArgValues; 1410 unsigned LastVal = ~0U; 1411 SDValue ArgValue; 1412 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1413 CCValAssign &VA = ArgLocs[i]; 1414 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later 1415 // places. 1416 assert(VA.getValNo() != LastVal && 1417 "Don't support value assigned to multiple locs yet"); 1418 LastVal = VA.getValNo(); 1419 1420 if (VA.isRegLoc()) { 1421 MVT RegVT = VA.getLocVT(); 1422 TargetRegisterClass *RC = NULL; 1423 if (RegVT == MVT::i32) 1424 RC = X86::GR32RegisterClass; 1425 else if (Is64Bit && RegVT == MVT::i64) 1426 RC = X86::GR64RegisterClass; 1427 else if (RegVT == MVT::f32) 1428 RC = X86::FR32RegisterClass; 1429 else if (RegVT == MVT::f64) 1430 RC = X86::FR64RegisterClass; 1431 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128) 1432 RC = X86::VR128RegisterClass; 1433 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64) 1434 RC = X86::VR64RegisterClass; 1435 else 1436 llvm_unreachable("Unknown argument type!"); 1437 1438 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 1439 ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT); 1440 1441 // If this is an 8 or 16-bit value, it is really passed promoted to 32 1442 // bits. Insert an assert[sz]ext to capture this, then truncate to the 1443 // right size. 1444 if (VA.getLocInfo() == CCValAssign::SExt) 1445 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 1446 DAG.getValueType(VA.getValVT())); 1447 else if (VA.getLocInfo() == CCValAssign::ZExt) 1448 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 1449 DAG.getValueType(VA.getValVT())); 1450 else if (VA.getLocInfo() == CCValAssign::BCvt) 1451 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue); 1452 1453 if (VA.isExtInLoc()) { 1454 // Handle MMX values passed in XMM regs. 1455 if (RegVT.isVector()) { 1456 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, 1457 ArgValue, DAG.getConstant(0, MVT::i64)); 1458 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue); 1459 } else 1460 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 1461 } 1462 } else { 1463 assert(VA.isMemLoc()); 1464 ArgValue = LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i); 1465 } 1466 1467 // If value is passed via pointer - do a load. 1468 if (VA.getLocInfo() == CCValAssign::Indirect) 1469 ArgValue = DAG.getLoad(VA.getValVT(), dl, Root, ArgValue, NULL, 0); 1470 1471 ArgValues.push_back(ArgValue); 1472 } 1473 1474 // The x86-64 ABI for returning structs by value requires that we copy 1475 // the sret argument into %rax for the return. Save the argument into 1476 // a virtual register so that we can access it from the return points. 1477 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) { 1478 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); 1479 unsigned Reg = FuncInfo->getSRetReturnReg(); 1480 if (!Reg) { 1481 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64)); 1482 FuncInfo->setSRetReturnReg(Reg); 1483 } 1484 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]); 1485 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root); 1486 } 1487 1488 unsigned StackSize = CCInfo.getNextStackOffset(); 1489 // align stack specially for tail calls 1490 if (PerformTailCallOpt && CC == CallingConv::Fast) 1491 StackSize = GetAlignedArgumentStackSize(StackSize, DAG); 1492 1493 // If the function takes variable number of arguments, make a frame index for 1494 // the start of the first vararg value... for expansion of llvm.va_start. 1495 if (isVarArg) { 1496 if (Is64Bit || CC != CallingConv::X86_FastCall) { 1497 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); 1498 } 1499 if (Is64Bit) { 1500 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0; 1501 1502 // FIXME: We should really autogenerate these arrays 1503 static const unsigned GPR64ArgRegsWin64[] = { 1504 X86::RCX, X86::RDX, X86::R8, X86::R9 1505 }; 1506 static const unsigned XMMArgRegsWin64[] = { 1507 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 1508 }; 1509 static const unsigned GPR64ArgRegs64Bit[] = { 1510 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 1511 }; 1512 static const unsigned XMMArgRegs64Bit[] = { 1513 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 1514 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 1515 }; 1516 const unsigned *GPR64ArgRegs, *XMMArgRegs; 1517 1518 if (IsWin64) { 1519 TotalNumIntRegs = 4; TotalNumXMMRegs = 4; 1520 GPR64ArgRegs = GPR64ArgRegsWin64; 1521 XMMArgRegs = XMMArgRegsWin64; 1522 } else { 1523 TotalNumIntRegs = 6; TotalNumXMMRegs = 8; 1524 GPR64ArgRegs = GPR64ArgRegs64Bit; 1525 XMMArgRegs = XMMArgRegs64Bit; 1526 } 1527 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 1528 TotalNumIntRegs); 1529 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 1530 TotalNumXMMRegs); 1531 1532 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat); 1533 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) && 1534 "SSE register cannot be used when SSE is disabled!"); 1535 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) && 1536 "SSE register cannot be used when SSE is disabled!"); 1537 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1()) 1538 // Kernel mode asks for SSE to be disabled, so don't push them 1539 // on the stack. 1540 TotalNumXMMRegs = 0; 1541 1542 // For X86-64, if there are vararg parameters that are passed via 1543 // registers, then we must store them to their spots on the stack so they 1544 // may be loaded by deferencing the result of va_next. 1545 VarArgsGPOffset = NumIntRegs * 8; 1546 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16; 1547 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 + 1548 TotalNumXMMRegs * 16, 16); 1549 1550 // Store the integer parameter registers. 1551 SmallVector<SDValue, 8> MemOps; 1552 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); 1553 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, 1554 DAG.getIntPtrConstant(VarArgsGPOffset)); 1555 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) { 1556 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs], 1557 X86::GR64RegisterClass); 1558 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64); 1559 SDValue Store = 1560 DAG.getStore(Val.getValue(1), dl, Val, FIN, 1561 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0); 1562 MemOps.push_back(Store); 1563 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 1564 DAG.getIntPtrConstant(8)); 1565 } 1566 1567 // Now store the XMM (fp + vector) parameter registers. 1568 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, 1569 DAG.getIntPtrConstant(VarArgsFPOffset)); 1570 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) { 1571 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs], 1572 X86::VR128RegisterClass); 1573 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32); 1574 SDValue Store = 1575 DAG.getStore(Val.getValue(1), dl, Val, FIN, 1576 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0); 1577 MemOps.push_back(Store); 1578 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 1579 DAG.getIntPtrConstant(16)); 1580 } 1581 if (!MemOps.empty()) 1582 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1583 &MemOps[0], MemOps.size()); 1584 } 1585 } 1586 1587 ArgValues.push_back(Root); 1588 1589 // Some CCs need callee pop. 1590 if (IsCalleePop(isVarArg, CC)) { 1591 BytesToPopOnReturn = StackSize; // Callee pops everything. 1592 BytesCallerReserves = 0; 1593 } else { 1594 BytesToPopOnReturn = 0; // Callee pops nothing. 1595 // If this is an sret function, the return should pop the hidden pointer. 1596 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op)) 1597 BytesToPopOnReturn = 4; 1598 BytesCallerReserves = StackSize; 1599 } 1600 1601 if (!Is64Bit) { 1602 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only. 1603 if (CC == CallingConv::X86_FastCall) 1604 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. 1605 } 1606 1607 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); 1608 1609 // Return the new list of results. 1610 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(), 1611 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo()); 1612 } 1613 1614 SDValue 1615 X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG, 1616 const SDValue &StackPtr, 1617 const CCValAssign &VA, 1618 SDValue Chain, 1619 SDValue Arg, ISD::ArgFlagsTy Flags) { 1620 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0); 1621 DebugLoc dl = TheCall->getDebugLoc(); 1622 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset(); 1623 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); 1624 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1625 if (Flags.isByVal()) { 1626 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl); 1627 } 1628 return DAG.getStore(Chain, dl, Arg, PtrOff, 1629 PseudoSourceValue::getStack(), LocMemOffset); 1630 } 1631 1632 /// EmitTailCallLoadRetAddr - Emit a load of return address if tail call 1633 /// optimization is performed and it is required. 1634 SDValue 1635 X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG, 1636 SDValue &OutRetAddr, 1637 SDValue Chain, 1638 bool IsTailCall, 1639 bool Is64Bit, 1640 int FPDiff, 1641 DebugLoc dl) { 1642 if (!IsTailCall || FPDiff==0) return Chain; 1643 1644 // Adjust the Return address stack slot. 1645 MVT VT = getPointerTy(); 1646 OutRetAddr = getReturnAddressFrameIndex(DAG); 1647 1648 // Load the "old" Return address. 1649 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0); 1650 return SDValue(OutRetAddr.getNode(), 1); 1651 } 1652 1653 /// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call 1654 /// optimization is performed and it is required (FPDiff!=0). 1655 static SDValue 1656 EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF, 1657 SDValue Chain, SDValue RetAddrFrIdx, 1658 bool Is64Bit, int FPDiff, DebugLoc dl) { 1659 // Store the return address to the appropriate stack slot. 1660 if (!FPDiff) return Chain; 1661 // Calculate the new stack slot for the return address. 1662 int SlotSize = Is64Bit ? 8 : 4; 1663 int NewReturnAddrFI = 1664 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize); 1665 MVT VT = Is64Bit ? MVT::i64 : MVT::i32; 1666 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT); 1667 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx, 1668 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0); 1669 return Chain; 1670 } 1671 1672 SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) { 1673 MachineFunction &MF = DAG.getMachineFunction(); 1674 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode()); 1675 SDValue Chain = TheCall->getChain(); 1676 unsigned CC = TheCall->getCallingConv(); 1677 bool isVarArg = TheCall->isVarArg(); 1678 bool IsTailCall = TheCall->isTailCall() && 1679 CC == CallingConv::Fast && PerformTailCallOpt; 1680 SDValue Callee = TheCall->getCallee(); 1681 bool Is64Bit = Subtarget->is64Bit(); 1682 bool IsStructRet = CallIsStructReturn(TheCall); 1683 DebugLoc dl = TheCall->getDebugLoc(); 1684 1685 assert(!(isVarArg && CC == CallingConv::Fast) && 1686 "Var args not supported with calling convention fastcc"); 1687 1688 // Analyze operands of the call, assigning locations to each operand. 1689 SmallVector<CCValAssign, 16> ArgLocs; 1690 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext()); 1691 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC)); 1692 1693 // Get a count of how many bytes are to be pushed on the stack. 1694 unsigned NumBytes = CCInfo.getNextStackOffset(); 1695 if (PerformTailCallOpt && CC == CallingConv::Fast) 1696 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG); 1697 1698 int FPDiff = 0; 1699 if (IsTailCall) { 1700 // Lower arguments at fp - stackoffset + fpdiff. 1701 unsigned NumBytesCallerPushed = 1702 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn(); 1703 FPDiff = NumBytesCallerPushed - NumBytes; 1704 1705 // Set the delta of movement of the returnaddr stackslot. 1706 // But only set if delta is greater than previous delta. 1707 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta())) 1708 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff); 1709 } 1710 1711 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); 1712 1713 SDValue RetAddrFrIdx; 1714 // Load return adress for tail calls. 1715 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit, 1716 FPDiff, dl); 1717 1718 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 1719 SmallVector<SDValue, 8> MemOpChains; 1720 SDValue StackPtr; 1721 1722 // Walk the register/memloc assignments, inserting copies/loads. In the case 1723 // of tail call optimization arguments are handle later. 1724 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1725 CCValAssign &VA = ArgLocs[i]; 1726 MVT RegVT = VA.getLocVT(); 1727 SDValue Arg = TheCall->getArg(i); 1728 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); 1729 bool isByVal = Flags.isByVal(); 1730 1731 // Promote the value if needed. 1732 switch (VA.getLocInfo()) { 1733 default: llvm_unreachable("Unknown loc info!"); 1734 case CCValAssign::Full: break; 1735 case CCValAssign::SExt: 1736 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg); 1737 break; 1738 case CCValAssign::ZExt: 1739 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg); 1740 break; 1741 case CCValAssign::AExt: 1742 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) { 1743 // Special case: passing MMX values in XMM registers. 1744 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg); 1745 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg); 1746 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg); 1747 } else 1748 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg); 1749 break; 1750 case CCValAssign::BCvt: 1751 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg); 1752 break; 1753 case CCValAssign::Indirect: { 1754 // Store the argument. 1755 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); 1756 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); 1757 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot, 1758 PseudoSourceValue::getFixedStack(FI), 0); 1759 Arg = SpillSlot; 1760 break; 1761 } 1762 } 1763 1764 if (VA.isRegLoc()) { 1765 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1766 } else { 1767 if (!IsTailCall || (IsTailCall && isByVal)) { 1768 assert(VA.isMemLoc()); 1769 if (StackPtr.getNode() == 0) 1770 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy()); 1771 1772 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA, 1773 Chain, Arg, Flags)); 1774 } 1775 } 1776 } 1777 1778 if (!MemOpChains.empty()) 1779 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1780 &MemOpChains[0], MemOpChains.size()); 1781 1782 // Build a sequence of copy-to-reg nodes chained together with token chain 1783 // and flag operands which copy the outgoing args into registers. 1784 SDValue InFlag; 1785 // Tail call byval lowering might overwrite argument registers so in case of 1786 // tail call optimization the copies to registers are lowered later. 1787 if (!IsTailCall) 1788 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1789 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1790 RegsToPass[i].second, InFlag); 1791 InFlag = Chain.getValue(1); 1792 } 1793 1794 1795 if (Subtarget->isPICStyleGOT()) { 1796 // ELF / PIC requires GOT in the EBX register before function calls via PLT 1797 // GOT pointer. 1798 if (!IsTailCall) { 1799 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX, 1800 DAG.getNode(X86ISD::GlobalBaseReg, 1801 DebugLoc::getUnknownLoc(), 1802 getPointerTy()), 1803 InFlag); 1804 InFlag = Chain.getValue(1); 1805 } else { 1806 // If we are tail calling and generating PIC/GOT style code load the 1807 // address of the callee into ECX. The value in ecx is used as target of 1808 // the tail jump. This is done to circumvent the ebx/callee-saved problem 1809 // for tail calls on PIC/GOT architectures. Normally we would just put the 1810 // address of GOT into ebx and then call target@PLT. But for tail calls 1811 // ebx would be restored (since ebx is callee saved) before jumping to the 1812 // target@PLT. 1813 1814 // Note: The actual moving to ECX is done further down. 1815 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); 1816 if (G && !G->getGlobal()->hasHiddenVisibility() && 1817 !G->getGlobal()->hasProtectedVisibility()) 1818 Callee = LowerGlobalAddress(Callee, DAG); 1819 else if (isa<ExternalSymbolSDNode>(Callee)) 1820 Callee = LowerExternalSymbol(Callee, DAG); 1821 } 1822 } 1823 1824 if (Is64Bit && isVarArg) { 1825 // From AMD64 ABI document: 1826 // For calls that may call functions that use varargs or stdargs 1827 // (prototype-less calls or calls to functions containing ellipsis (...) in 1828 // the declaration) %al is used as hidden argument to specify the number 1829 // of SSE registers used. The contents of %al do not need to match exactly 1830 // the number of registers, but must be an ubound on the number of SSE 1831 // registers used and is in the range 0 - 8 inclusive. 1832 1833 // FIXME: Verify this on Win64 1834 // Count the number of XMM registers allocated. 1835 static const unsigned XMMArgRegs[] = { 1836 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 1837 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 1838 }; 1839 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); 1840 assert((Subtarget->hasSSE1() || !NumXMMRegs) 1841 && "SSE registers cannot be used when SSE is disabled"); 1842 1843 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, 1844 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); 1845 InFlag = Chain.getValue(1); 1846 } 1847 1848 1849 // For tail calls lower the arguments to the 'real' stack slot. 1850 if (IsTailCall) { 1851 SmallVector<SDValue, 8> MemOpChains2; 1852 SDValue FIN; 1853 int FI = 0; 1854 // Do not flag preceeding copytoreg stuff together with the following stuff. 1855 InFlag = SDValue(); 1856 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1857 CCValAssign &VA = ArgLocs[i]; 1858 if (!VA.isRegLoc()) { 1859 assert(VA.isMemLoc()); 1860 SDValue Arg = TheCall->getArg(i); 1861 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); 1862 // Create frame index. 1863 int32_t Offset = VA.getLocMemOffset()+FPDiff; 1864 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8; 1865 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset); 1866 FIN = DAG.getFrameIndex(FI, getPointerTy()); 1867 1868 if (Flags.isByVal()) { 1869 // Copy relative to framepointer. 1870 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset()); 1871 if (StackPtr.getNode() == 0) 1872 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, 1873 getPointerTy()); 1874 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source); 1875 1876 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain, 1877 Flags, DAG, dl)); 1878 } else { 1879 // Store relative to framepointer. 1880 MemOpChains2.push_back( 1881 DAG.getStore(Chain, dl, Arg, FIN, 1882 PseudoSourceValue::getFixedStack(FI), 0)); 1883 } 1884 } 1885 } 1886 1887 if (!MemOpChains2.empty()) 1888 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1889 &MemOpChains2[0], MemOpChains2.size()); 1890 1891 // Copy arguments to their registers. 1892 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1893 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1894 RegsToPass[i].second, InFlag); 1895 InFlag = Chain.getValue(1); 1896 } 1897 InFlag =SDValue(); 1898 1899 // Store the return address to the appropriate stack slot. 1900 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit, 1901 FPDiff, dl); 1902 } 1903 1904 // If the callee is a GlobalAddress node (quite common, every direct call is) 1905 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 1906 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1907 // We should use extra load for direct calls to dllimported functions in 1908 // non-JIT mode. 1909 GlobalValue *GV = G->getGlobal(); 1910 if (!GV->hasDLLImportLinkage()) { 1911 unsigned char OpFlags = 0; 1912 1913 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to 1914 // external symbols most go through the PLT in PIC mode. If the symbol 1915 // has hidden or protected visibility, or if it is static or local, then 1916 // we don't need to use the PLT - we can directly call it. 1917 if (Subtarget->isTargetELF() && 1918 getTargetMachine().getRelocationModel() == Reloc::PIC_ && 1919 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) { 1920 OpFlags = X86II::MO_PLT; 1921 } else if (Subtarget->isPICStyleStubAny() && 1922 (GV->isDeclaration() || GV->isWeakForLinker()) && 1923 Subtarget->getDarwinVers() < 9) { 1924 // PC-relative references to external symbols should go through $stub, 1925 // unless we're building with the leopard linker or later, which 1926 // automatically synthesizes these stubs. 1927 OpFlags = X86II::MO_DARWIN_STUB; 1928 } 1929 1930 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(), 1931 G->getOffset(), OpFlags); 1932 } 1933 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1934 unsigned char OpFlags = 0; 1935 1936 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external 1937 // symbols should go through the PLT. 1938 if (Subtarget->isTargetELF() && 1939 getTargetMachine().getRelocationModel() == Reloc::PIC_) { 1940 OpFlags = X86II::MO_PLT; 1941 } else if (Subtarget->isPICStyleStubAny() && 1942 Subtarget->getDarwinVers() < 9) { 1943 // PC-relative references to external symbols should go through $stub, 1944 // unless we're building with the leopard linker or later, which 1945 // automatically synthesizes these stubs. 1946 OpFlags = X86II::MO_DARWIN_STUB; 1947 } 1948 1949 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(), 1950 OpFlags); 1951 } else if (IsTailCall) { 1952 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX; 1953 1954 Chain = DAG.getCopyToReg(Chain, dl, 1955 DAG.getRegister(Opc, getPointerTy()), 1956 Callee,InFlag); 1957 Callee = DAG.getRegister(Opc, getPointerTy()); 1958 // Add register as live out. 1959 MF.getRegInfo().addLiveOut(Opc); 1960 } 1961 1962 // Returns a chain & a flag for retval copy to use. 1963 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1964 SmallVector<SDValue, 8> Ops; 1965 1966 if (IsTailCall) { 1967 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1968 DAG.getIntPtrConstant(0, true), InFlag); 1969 InFlag = Chain.getValue(1); 1970 1971 // Returns a chain & a flag for retval copy to use. 1972 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1973 Ops.clear(); 1974 } 1975 1976 Ops.push_back(Chain); 1977 Ops.push_back(Callee); 1978 1979 if (IsTailCall) 1980 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32)); 1981 1982 // Add argument registers to the end of the list so that they are known live 1983 // into the call. 1984 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1985 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1986 RegsToPass[i].second.getValueType())); 1987 1988 // Add an implicit use GOT pointer in EBX. 1989 if (!IsTailCall && Subtarget->isPICStyleGOT()) 1990 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); 1991 1992 // Add an implicit use of AL for x86 vararg functions. 1993 if (Is64Bit && isVarArg) 1994 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8)); 1995 1996 if (InFlag.getNode()) 1997 Ops.push_back(InFlag); 1998 1999 if (IsTailCall) { 2000 assert(InFlag.getNode() && 2001 "Flag must be set. Depend on flag being set in LowerRET"); 2002 Chain = DAG.getNode(X86ISD::TAILCALL, dl, 2003 TheCall->getVTList(), &Ops[0], Ops.size()); 2004 2005 return SDValue(Chain.getNode(), Op.getResNo()); 2006 } 2007 2008 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); 2009 InFlag = Chain.getValue(1); 2010 2011 // Create the CALLSEQ_END node. 2012 unsigned NumBytesForCalleeToPush; 2013 if (IsCalleePop(isVarArg, CC)) 2014 NumBytesForCalleeToPush = NumBytes; // Callee pops everything 2015 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet) 2016 // If this is is a call to a struct-return function, the callee 2017 // pops the hidden struct pointer, so we have to push it back. 2018 // This is common for Darwin/X86, Linux & Mingw32 targets. 2019 NumBytesForCalleeToPush = 4; 2020 else 2021 NumBytesForCalleeToPush = 0; // Callee pops nothing. 2022 2023 // Returns a flag for retval copy to use. 2024 Chain = DAG.getCALLSEQ_END(Chain, 2025 DAG.getIntPtrConstant(NumBytes, true), 2026 DAG.getIntPtrConstant(NumBytesForCalleeToPush, 2027 true), 2028 InFlag); 2029 InFlag = Chain.getValue(1); 2030 2031 // Handle result values, copying them out of physregs into vregs that we 2032 // return. 2033 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), 2034 Op.getResNo()); 2035 } 2036 2037 2038 //===----------------------------------------------------------------------===// 2039 // Fast Calling Convention (tail call) implementation 2040 //===----------------------------------------------------------------------===// 2041 2042 // Like std call, callee cleans arguments, convention except that ECX is 2043 // reserved for storing the tail called function address. Only 2 registers are 2044 // free for argument passing (inreg). Tail call optimization is performed 2045 // provided: 2046 // * tailcallopt is enabled 2047 // * caller/callee are fastcc 2048 // On X86_64 architecture with GOT-style position independent code only local 2049 // (within module) calls are supported at the moment. 2050 // To keep the stack aligned according to platform abi the function 2051 // GetAlignedArgumentStackSize ensures that argument delta is always multiples 2052 // of stack alignment. (Dynamic linkers need this - darwin's dyld for example) 2053 // If a tail called function callee has more arguments than the caller the 2054 // caller needs to make sure that there is room to move the RETADDR to. This is 2055 // achieved by reserving an area the size of the argument delta right after the 2056 // original REtADDR, but before the saved framepointer or the spilled registers 2057 // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4) 2058 // stack layout: 2059 // arg1 2060 // arg2 2061 // RETADDR 2062 // [ new RETADDR 2063 // move area ] 2064 // (possible EBP) 2065 // ESI 2066 // EDI 2067 // local1 .. 2068 2069 /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned 2070 /// for a 16 byte align requirement. 2071 unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize, 2072 SelectionDAG& DAG) { 2073 MachineFunction &MF = DAG.getMachineFunction(); 2074 const TargetMachine &TM = MF.getTarget(); 2075 const TargetFrameInfo &TFI = *TM.getFrameInfo(); 2076 unsigned StackAlignment = TFI.getStackAlignment(); 2077 uint64_t AlignMask = StackAlignment - 1; 2078 int64_t Offset = StackSize; 2079 uint64_t SlotSize = TD->getPointerSize(); 2080 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) { 2081 // Number smaller than 12 so just add the difference. 2082 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask)); 2083 } else { 2084 // Mask out lower bits, add stackalignment once plus the 12 bytes. 2085 Offset = ((~AlignMask) & Offset) + StackAlignment + 2086 (StackAlignment-SlotSize); 2087 } 2088 return Offset; 2089 } 2090 2091 /// IsEligibleForTailCallElimination - Check to see whether the next instruction 2092 /// following the call is a return. A function is eligible if caller/callee 2093 /// calling conventions match, currently only fastcc supports tail calls, and 2094 /// the function CALL is immediatly followed by a RET. 2095 bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall, 2096 SDValue Ret, 2097 SelectionDAG& DAG) const { 2098 if (!PerformTailCallOpt) 2099 return false; 2100 2101 if (CheckTailCallReturnConstraints(TheCall, Ret)) { 2102 unsigned CallerCC = 2103 DAG.getMachineFunction().getFunction()->getCallingConv(); 2104 unsigned CalleeCC = TheCall->getCallingConv(); 2105 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) 2106 return true; 2107 } 2108 2109 return false; 2110 } 2111 2112 FastISel * 2113 X86TargetLowering::createFastISel(MachineFunction &mf, 2114 MachineModuleInfo *mmo, 2115 DwarfWriter *dw, 2116 DenseMap<const Value *, unsigned> &vm, 2117 DenseMap<const BasicBlock *, 2118 MachineBasicBlock *> &bm, 2119 DenseMap<const AllocaInst *, int> &am 2120 #ifndef NDEBUG 2121 , SmallSet<Instruction*, 8> &cil 2122 #endif 2123 ) { 2124 return X86::createFastISel(mf, mmo, dw, vm, bm, am 2125 #ifndef NDEBUG 2126 , cil 2127 #endif 2128 ); 2129 } 2130 2131 2132 //===----------------------------------------------------------------------===// 2133 // Other Lowering Hooks 2134 //===----------------------------------------------------------------------===// 2135 2136 2137 SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { 2138 MachineFunction &MF = DAG.getMachineFunction(); 2139 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); 2140 int ReturnAddrIndex = FuncInfo->getRAIndex(); 2141 2142 if (ReturnAddrIndex == 0) { 2143 // Set up a frame object for the return address. 2144 uint64_t SlotSize = TD->getPointerSize(); 2145 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize); 2146 FuncInfo->setRAIndex(ReturnAddrIndex); 2147 } 2148 2149 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); 2150 } 2151 2152 2153 /// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86 2154 /// specific condition code, returning the condition code and the LHS/RHS of the 2155 /// comparison to make. 2156 static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP, 2157 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) { 2158 if (!isFP) { 2159 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { 2160 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { 2161 // X > -1 -> X == 0, jump !sign. 2162 RHS = DAG.getConstant(0, RHS.getValueType()); 2163 return X86::COND_NS; 2164 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { 2165 // X < 0 -> X == 0, jump on sign. 2166 return X86::COND_S; 2167 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) { 2168 // X < 1 -> X <= 0 2169 RHS = DAG.getConstant(0, RHS.getValueType()); 2170 return X86::COND_LE; 2171 } 2172 } 2173 2174 switch (SetCCOpcode) { 2175 default: llvm_unreachable("Invalid integer condition!"); 2176 case ISD::SETEQ: return X86::COND_E; 2177 case ISD::SETGT: return X86::COND_G; 2178 case ISD::SETGE: return X86::COND_GE; 2179 case ISD::SETLT: return X86::COND_L; 2180 case ISD::SETLE: return X86::COND_LE; 2181 case ISD::SETNE: return X86::COND_NE; 2182 case ISD::SETULT: return X86::COND_B; 2183 case ISD::SETUGT: return X86::COND_A; 2184 case ISD::SETULE: return X86::COND_BE; 2185 case ISD::SETUGE: return X86::COND_AE; 2186 } 2187 } 2188 2189 // First determine if it is required or is profitable to flip the operands. 2190 2191 // If LHS is a foldable load, but RHS is not, flip the condition. 2192 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) && 2193 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) { 2194 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode); 2195 std::swap(LHS, RHS); 2196 } 2197 2198 switch (SetCCOpcode) { 2199 default: break; 2200 case ISD::SETOLT: 2201 case ISD::SETOLE: 2202 case ISD::SETUGT: 2203 case ISD::SETUGE: 2204 std::swap(LHS, RHS); 2205 break; 2206 } 2207 2208 // On a floating point condition, the flags are set as follows: 2209 // ZF PF CF op 2210 // 0 | 0 | 0 | X > Y 2211 // 0 | 0 | 1 | X < Y 2212 // 1 | 0 | 0 | X == Y 2213 // 1 | 1 | 1 | unordered 2214 switch (SetCCOpcode) { 2215 default: llvm_unreachable("Condcode should be pre-legalized away"); 2216 case ISD::SETUEQ: 2217 case ISD::SETEQ: return X86::COND_E; 2218 case ISD::SETOLT: // flipped 2219 case ISD::SETOGT: 2220 case ISD::SETGT: return X86::COND_A; 2221 case ISD::SETOLE: // flipped 2222 case ISD::SETOGE: 2223 case ISD::SETGE: return X86::COND_AE; 2224 case ISD::SETUGT: // flipped 2225 case ISD::SETULT: 2226 case ISD::SETLT: return X86::COND_B; 2227 case ISD::SETUGE: // flipped 2228 case ISD::SETULE: 2229 case ISD::SETLE: return X86::COND_BE; 2230 case ISD::SETONE: 2231 case ISD::SETNE: return X86::COND_NE; 2232 case ISD::SETUO: return X86::COND_P; 2233 case ISD::SETO: return X86::COND_NP; 2234 } 2235 } 2236 2237 /// hasFPCMov - is there a floating point cmov for the specific X86 condition 2238 /// code. Current x86 isa includes the following FP cmov instructions: 2239 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. 2240 static bool hasFPCMov(unsigned X86CC) { 2241 switch (X86CC) { 2242 default: 2243 return false; 2244 case X86::COND_B: 2245 case X86::COND_BE: 2246 case X86::COND_E: 2247 case X86::COND_P: 2248 case X86::COND_A: 2249 case X86::COND_AE: 2250 case X86::COND_NE: 2251 case X86::COND_NP: 2252 return true; 2253 } 2254 } 2255 2256 /// isUndefOrInRange - Return true if Val is undef or if its value falls within 2257 /// the specified range (L, H]. 2258 static bool isUndefOrInRange(int Val, int Low, int Hi) { 2259 return (Val < 0) || (Val >= Low && Val < Hi); 2260 } 2261 2262 /// isUndefOrEqual - Val is either less than zero (undef) or equal to the 2263 /// specified value. 2264 static bool isUndefOrEqual(int Val, int CmpVal) { 2265 if (Val < 0 || Val == CmpVal) 2266 return true; 2267 return false; 2268 } 2269 2270 /// isPSHUFDMask - Return true if the node specifies a shuffle of elements that 2271 /// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference 2272 /// the second operand. 2273 static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) { 2274 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16) 2275 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4); 2276 if (VT == MVT::v2f64 || VT == MVT::v2i64) 2277 return (Mask[0] < 2 && Mask[1] < 2); 2278 return false; 2279 } 2280 2281 bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) { 2282 SmallVector<int, 8> M; 2283 N->getMask(M); 2284 return ::isPSHUFDMask(M, N->getValueType(0)); 2285 } 2286 2287 /// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that 2288 /// is suitable for input to PSHUFHW. 2289 static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) { 2290 if (VT != MVT::v8i16) 2291 return false; 2292 2293 // Lower quadword copied in order or undef. 2294 for (int i = 0; i != 4; ++i) 2295 if (Mask[i] >= 0 && Mask[i] != i) 2296 return false; 2297 2298 // Upper quadword shuffled. 2299 for (int i = 4; i != 8; ++i) 2300 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7)) 2301 return false; 2302 2303 return true; 2304 } 2305 2306 bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) { 2307 SmallVector<int, 8> M; 2308 N->getMask(M); 2309 return ::isPSHUFHWMask(M, N->getValueType(0)); 2310 } 2311 2312 /// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that 2313 /// is suitable for input to PSHUFLW. 2314 static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) { 2315 if (VT != MVT::v8i16) 2316 return false; 2317 2318 // Upper quadword copied in order. 2319 for (int i = 4; i != 8; ++i) 2320 if (Mask[i] >= 0 && Mask[i] != i) 2321 return false; 2322 2323 // Lower quadword shuffled. 2324 for (int i = 0; i != 4; ++i) 2325 if (Mask[i] >= 4) 2326 return false; 2327 2328 return true; 2329 } 2330 2331 bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) { 2332 SmallVector<int, 8> M; 2333 N->getMask(M); 2334 return ::isPSHUFLWMask(M, N->getValueType(0)); 2335 } 2336 2337 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand 2338 /// specifies a shuffle of elements that is suitable for input to SHUFP*. 2339 static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) { 2340 int NumElems = VT.getVectorNumElements(); 2341 if (NumElems != 2 && NumElems != 4) 2342 return false; 2343 2344 int Half = NumElems / 2; 2345 for (int i = 0; i < Half; ++i) 2346 if (!isUndefOrInRange(Mask[i], 0, NumElems)) 2347 return false; 2348 for (int i = Half; i < NumElems; ++i) 2349 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2)) 2350 return false; 2351 2352 return true; 2353 } 2354 2355 bool X86::isSHUFPMask(ShuffleVectorSDNode *N) { 2356 SmallVector<int, 8> M; 2357 N->getMask(M); 2358 return ::isSHUFPMask(M, N->getValueType(0)); 2359 } 2360 2361 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly 2362 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower 2363 /// half elements to come from vector 1 (which would equal the dest.) and 2364 /// the upper half to come from vector 2. 2365 static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) { 2366 int NumElems = VT.getVectorNumElements(); 2367 2368 if (NumElems != 2 && NumElems != 4) 2369 return false; 2370 2371 int Half = NumElems / 2; 2372 for (int i = 0; i < Half; ++i) 2373 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2)) 2374 return false; 2375 for (int i = Half; i < NumElems; ++i) 2376 if (!isUndefOrInRange(Mask[i], 0, NumElems)) 2377 return false; 2378 return true; 2379 } 2380 2381 static bool isCommutedSHUFP(ShuffleVectorSDNode *N) { 2382 SmallVector<int, 8> M; 2383 N->getMask(M); 2384 return isCommutedSHUFPMask(M, N->getValueType(0)); 2385 } 2386 2387 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand 2388 /// specifies a shuffle of elements that is suitable for input to MOVHLPS. 2389 bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) { 2390 if (N->getValueType(0).getVectorNumElements() != 4) 2391 return false; 2392 2393 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 2394 return isUndefOrEqual(N->getMaskElt(0), 6) && 2395 isUndefOrEqual(N->getMaskElt(1), 7) && 2396 isUndefOrEqual(N->getMaskElt(2), 2) && 2397 isUndefOrEqual(N->getMaskElt(3), 3); 2398 } 2399 2400 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand 2401 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. 2402 bool X86::isMOVLPMask(ShuffleVectorSDNode *N) { 2403 unsigned NumElems = N->getValueType(0).getVectorNumElements(); 2404 2405 if (NumElems != 2 && NumElems != 4) 2406 return false; 2407 2408 for (unsigned i = 0; i < NumElems/2; ++i) 2409 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems)) 2410 return false; 2411 2412 for (unsigned i = NumElems/2; i < NumElems; ++i) 2413 if (!isUndefOrEqual(N->getMaskElt(i), i)) 2414 return false; 2415 2416 return true; 2417 } 2418 2419 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand 2420 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} 2421 /// and MOVLHPS. 2422 bool X86::isMOVHPMask(ShuffleVectorSDNode *N) { 2423 unsigned NumElems = N->getValueType(0).getVectorNumElements(); 2424 2425 if (NumElems != 2 && NumElems != 4) 2426 return false; 2427 2428 for (unsigned i = 0; i < NumElems/2; ++i) 2429 if (!isUndefOrEqual(N->getMaskElt(i), i)) 2430 return false; 2431 2432 for (unsigned i = 0; i < NumElems/2; ++i) 2433 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems)) 2434 return false; 2435 2436 return true; 2437 } 2438 2439 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form 2440 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, 2441 /// <2, 3, 2, 3> 2442 bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) { 2443 unsigned NumElems = N->getValueType(0).getVectorNumElements(); 2444 2445 if (NumElems != 4) 2446 return false; 2447 2448 return isUndefOrEqual(N->getMaskElt(0), 2) && 2449 isUndefOrEqual(N->getMaskElt(1), 3) && 2450 isUndefOrEqual(N->getMaskElt(2), 2) && 2451 isUndefOrEqual(N->getMaskElt(3), 3); 2452 } 2453 2454 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand 2455 /// specifies a shuffle of elements that is suitable for input to UNPCKL. 2456 static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT, 2457 bool V2IsSplat = false) { 2458 int NumElts = VT.getVectorNumElements(); 2459 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) 2460 return false; 2461 2462 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) { 2463 int BitI = Mask[i]; 2464 int BitI1 = Mask[i+1]; 2465 if (!isUndefOrEqual(BitI, j)) 2466 return false; 2467 if (V2IsSplat) { 2468 if (!isUndefOrEqual(BitI1, NumElts)) 2469 return false; 2470 } else { 2471 if (!isUndefOrEqual(BitI1, j + NumElts)) 2472 return false; 2473 } 2474 } 2475 return true; 2476 } 2477 2478 bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) { 2479 SmallVector<int, 8> M; 2480 N->getMask(M); 2481 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat); 2482 } 2483 2484 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand 2485 /// specifies a shuffle of elements that is suitable for input to UNPCKH. 2486 static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT, 2487 bool V2IsSplat = false) { 2488 int NumElts = VT.getVectorNumElements(); 2489 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) 2490 return false; 2491 2492 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) { 2493 int BitI = Mask[i]; 2494 int BitI1 = Mask[i+1]; 2495 if (!isUndefOrEqual(BitI, j + NumElts/2)) 2496 return false; 2497 if (V2IsSplat) { 2498 if (isUndefOrEqual(BitI1, NumElts)) 2499 return false; 2500 } else { 2501 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts)) 2502 return false; 2503 } 2504 } 2505 return true; 2506 } 2507 2508 bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) { 2509 SmallVector<int, 8> M; 2510 N->getMask(M); 2511 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat); 2512 } 2513 2514 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form 2515 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, 2516 /// <0, 0, 1, 1> 2517 static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) { 2518 int NumElems = VT.getVectorNumElements(); 2519 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) 2520 return false; 2521 2522 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) { 2523 int BitI = Mask[i]; 2524 int BitI1 = Mask[i+1]; 2525 if (!isUndefOrEqual(BitI, j)) 2526 return false; 2527 if (!isUndefOrEqual(BitI1, j)) 2528 return false; 2529 } 2530 return true; 2531 } 2532 2533 bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) { 2534 SmallVector<int, 8> M; 2535 N->getMask(M); 2536 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0)); 2537 } 2538 2539 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form 2540 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef, 2541 /// <2, 2, 3, 3> 2542 static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) { 2543 int NumElems = VT.getVectorNumElements(); 2544 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) 2545 return false; 2546 2547 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) { 2548 int BitI = Mask[i]; 2549 int BitI1 = Mask[i+1]; 2550 if (!isUndefOrEqual(BitI, j)) 2551 return false; 2552 if (!isUndefOrEqual(BitI1, j)) 2553 return false; 2554 } 2555 return true; 2556 } 2557 2558 bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) { 2559 SmallVector<int, 8> M; 2560 N->getMask(M); 2561 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0)); 2562 } 2563 2564 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand 2565 /// specifies a shuffle of elements that is suitable for input to MOVSS, 2566 /// MOVSD, and MOVD, i.e. setting the lowest element. 2567 static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) { 2568 if (VT.getVectorElementType().getSizeInBits() < 32) 2569 return false; 2570 2571 int NumElts = VT.getVectorNumElements(); 2572 2573 if (!isUndefOrEqual(Mask[0], NumElts)) 2574 return false; 2575 2576 for (int i = 1; i < NumElts; ++i) 2577 if (!isUndefOrEqual(Mask[i], i)) 2578 return false; 2579 2580 return true; 2581 } 2582 2583 bool X86::isMOVLMask(ShuffleVectorSDNode *N) { 2584 SmallVector<int, 8> M; 2585 N->getMask(M); 2586 return ::isMOVLMask(M, N->getValueType(0)); 2587 } 2588 2589 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse 2590 /// of what x86 movss want. X86 movs requires the lowest element to be lowest 2591 /// element of vector 2 and the other elements to come from vector 1 in order. 2592 static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT, 2593 bool V2IsSplat = false, bool V2IsUndef = false) { 2594 int NumOps = VT.getVectorNumElements(); 2595 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) 2596 return false; 2597 2598 if (!isUndefOrEqual(Mask[0], 0)) 2599 return false; 2600 2601 for (int i = 1; i < NumOps; ++i) 2602 if (!(isUndefOrEqual(Mask[i], i+NumOps) || 2603 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) || 2604 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps)))) 2605 return false; 2606 2607 return true; 2608 } 2609 2610 static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false, 2611 bool V2IsUndef = false) { 2612 SmallVector<int, 8> M; 2613 N->getMask(M); 2614 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef); 2615 } 2616 2617 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand 2618 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. 2619 bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) { 2620 if (N->getValueType(0).getVectorNumElements() != 4) 2621 return false; 2622 2623 // Expect 1, 1, 3, 3 2624 for (unsigned i = 0; i < 2; ++i) { 2625 int Elt = N->getMaskElt(i); 2626 if (Elt >= 0 && Elt != 1) 2627 return false; 2628 } 2629 2630 bool HasHi = false; 2631 for (unsigned i = 2; i < 4; ++i) { 2632 int Elt = N->getMaskElt(i); 2633 if (Elt >= 0 && Elt != 3) 2634 return false; 2635 if (Elt == 3) 2636 HasHi = true; 2637 } 2638 // Don't use movshdup if it can be done with a shufps. 2639 // FIXME: verify that matching u, u, 3, 3 is what we want. 2640 return HasHi; 2641 } 2642 2643 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand 2644 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. 2645 bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) { 2646 if (N->getValueType(0).getVectorNumElements() != 4) 2647 return false; 2648 2649 // Expect 0, 0, 2, 2 2650 for (unsigned i = 0; i < 2; ++i) 2651 if (N->getMaskElt(i) > 0) 2652 return false; 2653 2654 bool HasHi = false; 2655 for (unsigned i = 2; i < 4; ++i) { 2656 int Elt = N->getMaskElt(i); 2657 if (Elt >= 0 && Elt != 2) 2658 return false; 2659 if (Elt == 2) 2660 HasHi = true; 2661 } 2662 // Don't use movsldup if it can be done with a shufps. 2663 return HasHi; 2664 } 2665 2666 /// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand 2667 /// specifies a shuffle of elements that is suitable for input to MOVDDUP. 2668 bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) { 2669 int e = N->getValueType(0).getVectorNumElements() / 2; 2670 2671 for (int i = 0; i < e; ++i) 2672 if (!isUndefOrEqual(N->getMaskElt(i), i)) 2673 return false; 2674 for (int i = 0; i < e; ++i) 2675 if (!isUndefOrEqual(N->getMaskElt(e+i), i)) 2676 return false; 2677 return true; 2678 } 2679 2680 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle 2681 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* 2682 /// instructions. 2683 unsigned X86::getShuffleSHUFImmediate(SDNode *N) { 2684 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 2685 int NumOperands = SVOp->getValueType(0).getVectorNumElements(); 2686 2687 unsigned Shift = (NumOperands == 4) ? 2 : 1; 2688 unsigned Mask = 0; 2689 for (int i = 0; i < NumOperands; ++i) { 2690 int Val = SVOp->getMaskElt(NumOperands-i-1); 2691 if (Val < 0) Val = 0; 2692 if (Val >= NumOperands) Val -= NumOperands; 2693 Mask |= Val; 2694 if (i != NumOperands - 1) 2695 Mask <<= Shift; 2696 } 2697 return Mask; 2698 } 2699 2700 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle 2701 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW 2702 /// instructions. 2703 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { 2704 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 2705 unsigned Mask = 0; 2706 // 8 nodes, but we only care about the last 4. 2707 for (unsigned i = 7; i >= 4; --i) { 2708 int Val = SVOp->getMaskElt(i); 2709 if (Val >= 0) 2710 Mask |= (Val - 4); 2711 if (i != 4) 2712 Mask <<= 2; 2713 } 2714 return Mask; 2715 } 2716 2717 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle 2718 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW 2719 /// instructions. 2720 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { 2721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 2722 unsigned Mask = 0; 2723 // 8 nodes, but we only care about the first 4. 2724 for (int i = 3; i >= 0; --i) { 2725 int Val = SVOp->getMaskElt(i); 2726 if (Val >= 0) 2727 Mask |= Val; 2728 if (i != 0) 2729 Mask <<= 2; 2730 } 2731 return Mask; 2732 } 2733 2734 /// isZeroNode - Returns true if Elt is a constant zero or a floating point 2735 /// constant +0.0. 2736 bool X86::isZeroNode(SDValue Elt) { 2737 return ((isa<ConstantSDNode>(Elt) && 2738 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) || 2739 (isa<ConstantFPSDNode>(Elt) && 2740 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); 2741 } 2742 2743 /// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in 2744 /// their permute mask. 2745 static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp, 2746 SelectionDAG &DAG) { 2747 MVT VT = SVOp->getValueType(0); 2748 unsigned NumElems = VT.getVectorNumElements(); 2749 SmallVector<int, 8> MaskVec; 2750 2751 for (unsigned i = 0; i != NumElems; ++i) { 2752 int idx = SVOp->getMaskElt(i); 2753 if (idx < 0) 2754 MaskVec.push_back(idx); 2755 else if (idx < (int)NumElems) 2756 MaskVec.push_back(idx + NumElems); 2757 else 2758 MaskVec.push_back(idx - NumElems); 2759 } 2760 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1), 2761 SVOp->getOperand(0), &MaskVec[0]); 2762 } 2763 2764 /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming 2765 /// the two vector operands have swapped position. 2766 static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) { 2767 unsigned NumElems = VT.getVectorNumElements(); 2768 for (unsigned i = 0; i != NumElems; ++i) { 2769 int idx = Mask[i]; 2770 if (idx < 0) 2771 continue; 2772 else if (idx < (int)NumElems) 2773 Mask[i] = idx + NumElems; 2774 else 2775 Mask[i] = idx - NumElems; 2776 } 2777 } 2778 2779 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to 2780 /// match movhlps. The lower half elements should come from upper half of 2781 /// V1 (and in order), and the upper half elements should come from the upper 2782 /// half of V2 (and in order). 2783 static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) { 2784 if (Op->getValueType(0).getVectorNumElements() != 4) 2785 return false; 2786 for (unsigned i = 0, e = 2; i != e; ++i) 2787 if (!isUndefOrEqual(Op->getMaskElt(i), i+2)) 2788 return false; 2789 for (unsigned i = 2; i != 4; ++i) 2790 if (!isUndefOrEqual(Op->getMaskElt(i), i+4)) 2791 return false; 2792 return true; 2793 } 2794 2795 /// isScalarLoadToVector - Returns true if the node is a scalar load that 2796 /// is promoted to a vector. It also returns the LoadSDNode by reference if 2797 /// required. 2798 static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) { 2799 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR) 2800 return false; 2801 N = N->getOperand(0).getNode(); 2802 if (!ISD::isNON_EXTLoad(N)) 2803 return false; 2804 if (LD) 2805 *LD = cast<LoadSDNode>(N); 2806 return true; 2807 } 2808 2809 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to 2810 /// match movlp{s|d}. The lower half elements should come from lower half of 2811 /// V1 (and in order), and the upper half elements should come from the upper 2812 /// half of V2 (and in order). And since V1 will become the source of the 2813 /// MOVLP, it must be either a vector load or a scalar load to vector. 2814 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, 2815 ShuffleVectorSDNode *Op) { 2816 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) 2817 return false; 2818 // Is V2 is a vector load, don't do this transformation. We will try to use 2819 // load folding shufps op. 2820 if (ISD::isNON_EXTLoad(V2)) 2821 return false; 2822 2823 unsigned NumElems = Op->getValueType(0).getVectorNumElements(); 2824 2825 if (NumElems != 2 && NumElems != 4) 2826 return false; 2827 for (unsigned i = 0, e = NumElems/2; i != e; ++i) 2828 if (!isUndefOrEqual(Op->getMaskElt(i), i)) 2829 return false; 2830 for (unsigned i = NumElems/2; i != NumElems; ++i) 2831 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems)) 2832 return false; 2833 return true; 2834 } 2835 2836 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are 2837 /// all the same. 2838 static bool isSplatVector(SDNode *N) { 2839 if (N->getOpcode() != ISD::BUILD_VECTOR) 2840 return false; 2841 2842 SDValue SplatValue = N->getOperand(0); 2843 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) 2844 if (N->getOperand(i) != SplatValue) 2845 return false; 2846 return true; 2847 } 2848 2849 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved 2850 /// to an zero vector. 2851 /// FIXME: move to dag combiner / method on ShuffleVectorSDNode 2852 static bool isZeroShuffle(ShuffleVectorSDNode *N) { 2853 SDValue V1 = N->getOperand(0); 2854 SDValue V2 = N->getOperand(1); 2855 unsigned NumElems = N->getValueType(0).getVectorNumElements(); 2856 for (unsigned i = 0; i != NumElems; ++i) { 2857 int Idx = N->getMaskElt(i); 2858 if (Idx >= (int)NumElems) { 2859 unsigned Opc = V2.getOpcode(); 2860 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode())) 2861 continue; 2862 if (Opc != ISD::BUILD_VECTOR || 2863 !X86::isZeroNode(V2.getOperand(Idx-NumElems))) 2864 return false; 2865 } else if (Idx >= 0) { 2866 unsigned Opc = V1.getOpcode(); 2867 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode())) 2868 continue; 2869 if (Opc != ISD::BUILD_VECTOR || 2870 !X86::isZeroNode(V1.getOperand(Idx))) 2871 return false; 2872 } 2873 } 2874 return true; 2875 } 2876 2877 /// getZeroVector - Returns a vector of specified type with all zero elements. 2878 /// 2879 static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG, 2880 DebugLoc dl) { 2881 assert(VT.isVector() && "Expected a vector type"); 2882 2883 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest 2884 // type. This ensures they get CSE'd. 2885 SDValue Vec; 2886 if (VT.getSizeInBits() == 64) { // MMX 2887 SDValue Cst = DAG.getTargetConstant(0, MVT::i32); 2888 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst); 2889 } else if (HasSSE2) { // SSE2 2890 SDValue Cst = DAG.getTargetConstant(0, MVT::i32); 2891 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); 2892 } else { // SSE1 2893 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32); 2894 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst); 2895 } 2896 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec); 2897 } 2898 2899 /// getOnesVector - Returns a vector of specified type with all bits set. 2900 /// 2901 static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) { 2902 assert(VT.isVector() && "Expected a vector type"); 2903 2904 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest 2905 // type. This ensures they get CSE'd. 2906 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32); 2907 SDValue Vec; 2908 if (VT.getSizeInBits() == 64) // MMX 2909 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst); 2910 else // SSE 2911 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); 2912 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec); 2913 } 2914 2915 2916 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements 2917 /// that point to V2 points to its first element. 2918 static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { 2919 MVT VT = SVOp->getValueType(0); 2920 unsigned NumElems = VT.getVectorNumElements(); 2921 2922 bool Changed = false; 2923 SmallVector<int, 8> MaskVec; 2924 SVOp->getMask(MaskVec); 2925 2926 for (unsigned i = 0; i != NumElems; ++i) { 2927 if (MaskVec[i] > (int)NumElems) { 2928 MaskVec[i] = NumElems; 2929 Changed = true; 2930 } 2931 } 2932 if (Changed) 2933 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0), 2934 SVOp->getOperand(1), &MaskVec[0]); 2935 return SDValue(SVOp, 0); 2936 } 2937 2938 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd 2939 /// operation of specified width. 2940 static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, 2941 SDValue V2) { 2942 unsigned NumElems = VT.getVectorNumElements(); 2943 SmallVector<int, 8> Mask; 2944 Mask.push_back(NumElems); 2945 for (unsigned i = 1; i != NumElems; ++i) 2946 Mask.push_back(i); 2947 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); 2948 } 2949 2950 /// getUnpackl - Returns a vector_shuffle node for an unpackl operation. 2951 static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, 2952 SDValue V2) { 2953 unsigned NumElems = VT.getVectorNumElements(); 2954 SmallVector<int, 8> Mask; 2955 for (unsigned i = 0, e = NumElems/2; i != e; ++i) { 2956 Mask.push_back(i); 2957 Mask.push_back(i + NumElems); 2958 } 2959 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); 2960 } 2961 2962 /// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation. 2963 static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, 2964 SDValue V2) { 2965 unsigned NumElems = VT.getVectorNumElements(); 2966 unsigned Half = NumElems/2; 2967 SmallVector<int, 8> Mask; 2968 for (unsigned i = 0; i != Half; ++i) { 2969 Mask.push_back(i + Half); 2970 Mask.push_back(i + NumElems + Half); 2971 } 2972 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); 2973 } 2974 2975 /// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32. 2976 static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG, 2977 bool HasSSE2) { 2978 if (SV->getValueType(0).getVectorNumElements() <= 4) 2979 return SDValue(SV, 0); 2980 2981 MVT PVT = MVT::v4f32; 2982 MVT VT = SV->getValueType(0); 2983 DebugLoc dl = SV->getDebugLoc(); 2984 SDValue V1 = SV->getOperand(0); 2985 int NumElems = VT.getVectorNumElements(); 2986 int EltNo = SV->getSplatIndex(); 2987 2988 // unpack elements to the correct location 2989 while (NumElems > 4) { 2990 if (EltNo < NumElems/2) { 2991 V1 = getUnpackl(DAG, dl, VT, V1, V1); 2992 } else { 2993 V1 = getUnpackh(DAG, dl, VT, V1, V1); 2994 EltNo -= NumElems/2; 2995 } 2996 NumElems >>= 1; 2997 } 2998 2999 // Perform the splat. 3000 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo }; 3001 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1); 3002 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]); 3003 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1); 3004 } 3005 3006 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified 3007 /// vector of zero or undef vector. This produces a shuffle where the low 3008 /// element of V2 is swizzled into the zero/undef vector, landing at element 3009 /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3). 3010 static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx, 3011 bool isZero, bool HasSSE2, 3012 SelectionDAG &DAG) { 3013 MVT VT = V2.getValueType(); 3014 SDValue V1 = isZero 3015 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT); 3016 unsigned NumElems = VT.getVectorNumElements(); 3017 SmallVector<int, 16> MaskVec; 3018 for (unsigned i = 0; i != NumElems; ++i) 3019 // If this is the insertion idx, put the low elt of V2 here. 3020 MaskVec.push_back(i == Idx ? NumElems : i); 3021 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]); 3022 } 3023 3024 /// getNumOfConsecutiveZeros - Return the number of elements in a result of 3025 /// a shuffle that is zero. 3026 static 3027 unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems, 3028 bool Low, SelectionDAG &DAG) { 3029 unsigned NumZeros = 0; 3030 for (int i = 0; i < NumElems; ++i) { 3031 unsigned Index = Low ? i : NumElems-i-1; 3032 int Idx = SVOp->getMaskElt(Index); 3033 if (Idx < 0) { 3034 ++NumZeros; 3035 continue; 3036 } 3037 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index); 3038 if (Elt.getNode() && X86::isZeroNode(Elt)) 3039 ++NumZeros; 3040 else 3041 break; 3042 } 3043 return NumZeros; 3044 } 3045 3046 /// isVectorShift - Returns true if the shuffle can be implemented as a 3047 /// logical left or right shift of a vector. 3048 /// FIXME: split into pslldqi, psrldqi, palignr variants. 3049 static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG, 3050 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) { 3051 int NumElems = SVOp->getValueType(0).getVectorNumElements(); 3052 3053 isLeft = true; 3054 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG); 3055 if (!NumZeros) { 3056 isLeft = false; 3057 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG); 3058 if (!NumZeros) 3059 return false; 3060 } 3061 bool SeenV1 = false; 3062 bool SeenV2 = false; 3063 for (int i = NumZeros; i < NumElems; ++i) { 3064 int Val = isLeft ? (i - NumZeros) : i; 3065 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros)); 3066 if (Idx < 0) 3067 continue; 3068 if (Idx < NumElems) 3069 SeenV1 = true; 3070 else { 3071 Idx -= NumElems; 3072 SeenV2 = true; 3073 } 3074 if (Idx != Val) 3075 return false; 3076 } 3077 if (SeenV1 && SeenV2) 3078 return false; 3079 3080 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1); 3081 ShAmt = NumZeros; 3082 return true; 3083 } 3084 3085 3086 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. 3087 /// 3088 static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros, 3089 unsigned NumNonZero, unsigned NumZero, 3090 SelectionDAG &DAG, TargetLowering &TLI) { 3091 if (NumNonZero > 8) 3092 return SDValue(); 3093 3094 DebugLoc dl = Op.getDebugLoc(); 3095 SDValue V(0, 0); 3096 bool First = true; 3097 for (unsigned i = 0; i < 16; ++i) { 3098 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; 3099 if (ThisIsNonZero && First) { 3100 if (NumZero) 3101 V = getZeroVector(MVT::v8i16, true, DAG, dl); 3102 else 3103 V = DAG.getUNDEF(MVT::v8i16); 3104 First = false; 3105 } 3106 3107 if ((i & 1) != 0) { 3108 SDValue ThisElt(0, 0), LastElt(0, 0); 3109 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; 3110 if (LastIsNonZero) { 3111 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl, 3112 MVT::i16, Op.getOperand(i-1)); 3113 } 3114 if (ThisIsNonZero) { 3115 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i)); 3116 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16, 3117 ThisElt, DAG.getConstant(8, MVT::i8)); 3118 if (LastIsNonZero) 3119 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt); 3120 } else 3121 ThisElt = LastElt; 3122 3123 if (ThisElt.getNode()) 3124 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt, 3125 DAG.getIntPtrConstant(i/2)); 3126 } 3127 } 3128 3129 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V); 3130 } 3131 3132 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16. 3133 /// 3134 static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros, 3135 unsigned NumNonZero, unsigned NumZero, 3136 SelectionDAG &DAG, TargetLowering &TLI) { 3137 if (NumNonZero > 4) 3138 return SDValue(); 3139 3140 DebugLoc dl = Op.getDebugLoc(); 3141 SDValue V(0, 0); 3142 bool First = true; 3143 for (unsigned i = 0; i < 8; ++i) { 3144 bool isNonZero = (NonZeros & (1 << i)) != 0; 3145 if (isNonZero) { 3146 if (First) { 3147 if (NumZero) 3148 V = getZeroVector(MVT::v8i16, true, DAG, dl); 3149 else 3150 V = DAG.getUNDEF(MVT::v8i16); 3151 First = false; 3152 } 3153 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, 3154 MVT::v8i16, V, Op.getOperand(i), 3155 DAG.getIntPtrConstant(i)); 3156 } 3157 } 3158 3159 return V; 3160 } 3161 3162 /// getVShift - Return a vector logical shift node. 3163 /// 3164 static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp, 3165 unsigned NumBits, SelectionDAG &DAG, 3166 const TargetLowering &TLI, DebugLoc dl) { 3167 bool isMMX = VT.getSizeInBits() == 64; 3168 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64; 3169 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL; 3170 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp); 3171 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, 3172 DAG.getNode(Opc, dl, ShVT, SrcOp, 3173 DAG.getConstant(NumBits, TLI.getShiftAmountTy()))); 3174 } 3175 3176 SDValue 3177 X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) { 3178 DebugLoc dl = Op.getDebugLoc(); 3179 // All zero's are handled with pxor, all one's are handled with pcmpeqd. 3180 if (ISD::isBuildVectorAllZeros(Op.getNode()) 3181 || ISD::isBuildVectorAllOnes(Op.getNode())) { 3182 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to 3183 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are 3184 // eliminated on x86-32 hosts. 3185 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32) 3186 return Op; 3187 3188 if (ISD::isBuildVectorAllOnes(Op.getNode())) 3189 return getOnesVector(Op.getValueType(), DAG, dl); 3190 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl); 3191 } 3192 3193 MVT VT = Op.getValueType(); 3194 MVT EVT = VT.getVectorElementType(); 3195 unsigned EVTBits = EVT.getSizeInBits(); 3196 3197 unsigned NumElems = Op.getNumOperands(); 3198 unsigned NumZero = 0; 3199 unsigned NumNonZero = 0; 3200 unsigned NonZeros = 0; 3201 bool IsAllConstants = true; 3202 SmallSet<SDValue, 8> Values; 3203 for (unsigned i = 0; i < NumElems; ++i) { 3204 SDValue Elt = Op.getOperand(i); 3205 if (Elt.getOpcode() == ISD::UNDEF) 3206 continue; 3207 Values.insert(Elt); 3208 if (Elt.getOpcode() != ISD::Constant && 3209 Elt.getOpcode() != ISD::ConstantFP) 3210 IsAllConstants = false; 3211 if (X86::isZeroNode(Elt)) 3212 NumZero++; 3213 else { 3214 NonZeros |= (1 << i); 3215 NumNonZero++; 3216 } 3217 } 3218 3219 if (NumNonZero == 0) { 3220 // All undef vector. Return an UNDEF. All zero vectors were handled above. 3221 return DAG.getUNDEF(VT); 3222 } 3223 3224 // Special case for single non-zero, non-undef, element. 3225 if (NumNonZero == 1) { 3226 unsigned Idx = CountTrailingZeros_32(NonZeros); 3227 SDValue Item = Op.getOperand(Idx); 3228 3229 // If this is an insertion of an i64 value on x86-32, and if the top bits of 3230 // the value are obviously zero, truncate the value to i32 and do the 3231 // insertion that way. Only do this if the value is non-constant or if the 3232 // value is a constant being inserted into element 0. It is cheaper to do 3233 // a constant pool load than it is to do a movd + shuffle. 3234 if (EVT == MVT::i64 && !Subtarget->is64Bit() && 3235 (!IsAllConstants || Idx == 0)) { 3236 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) { 3237 // Handle MMX and SSE both. 3238 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32; 3239 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2; 3240 3241 // Truncate the value (which may itself be a constant) to i32, and 3242 // convert it to a vector with movd (S2V+shuffle to zero extend). 3243 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item); 3244 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item); 3245 Item = getShuffleVectorZeroOrUndef(Item, 0, true, 3246 Subtarget->hasSSE2(), DAG); 3247 3248 // Now we have our 32-bit value zero extended in the low element of 3249 // a vector. If Idx != 0, swizzle it into place. 3250 if (Idx != 0) { 3251 SmallVector<int, 4> Mask; 3252 Mask.push_back(Idx); 3253 for (unsigned i = 1; i != VecElts; ++i) 3254 Mask.push_back(i); 3255 Item = DAG.getVectorShuffle(VecVT, dl, Item, 3256 DAG.getUNDEF(Item.getValueType()), 3257 &Mask[0]); 3258 } 3259 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item); 3260 } 3261 } 3262 3263 // If we have a constant or non-constant insertion into the low element of 3264 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into 3265 // the rest of the elements. This will be matched as movd/movq/movss/movsd 3266 // depending on what the source datatype is. 3267 if (Idx == 0) { 3268 if (NumZero == 0) { 3269 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); 3270 } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 || 3271 (EVT == MVT::i64 && Subtarget->is64Bit())) { 3272 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); 3273 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. 3274 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(), 3275 DAG); 3276 } else if (EVT == MVT::i16 || EVT == MVT::i8) { 3277 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item); 3278 MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32; 3279 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item); 3280 Item = getShuffleVectorZeroOrUndef(Item, 0, true, 3281 Subtarget->hasSSE2(), DAG); 3282 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item); 3283 } 3284 } 3285 3286 // Is it a vector logical left shift? 3287 if (NumElems == 2 && Idx == 1 && 3288 X86::isZeroNode(Op.getOperand(0)) && 3289 !X86::isZeroNode(Op.getOperand(1))) { 3290 unsigned NumBits = VT.getSizeInBits(); 3291 return getVShift(true, VT, 3292 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 3293 VT, Op.getOperand(1)), 3294 NumBits/2, DAG, *this, dl); 3295 } 3296 3297 if (IsAllConstants) // Otherwise, it's better to do a constpool load. 3298 return SDValue(); 3299 3300 // Otherwise, if this is a vector with i32 or f32 elements, and the element 3301 // is a non-constant being inserted into an element other than the low one, 3302 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka 3303 // movd/movss) to move this into the low element, then shuffle it into 3304 // place. 3305 if (EVTBits == 32) { 3306 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); 3307 3308 // Turn it into a shuffle of zero and zero-extended scalar to vector. 3309 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, 3310 Subtarget->hasSSE2(), DAG); 3311 SmallVector<int, 8> MaskVec; 3312 for (unsigned i = 0; i < NumElems; i++) 3313 MaskVec.push_back(i == Idx ? 0 : 1); 3314 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]); 3315 } 3316 } 3317 3318 // Splat is obviously ok. Let legalizer expand it to a shuffle. 3319 if (Values.size() == 1) 3320 return SDValue(); 3321 3322 // A vector full of immediates; various special cases are already 3323 // handled, so this is best done with a single constant-pool load. 3324 if (IsAllConstants) 3325 return SDValue(); 3326 3327 // Let legalizer expand 2-wide build_vectors. 3328 if (EVTBits == 64) { 3329 if (NumNonZero == 1) { 3330 // One half is zero or undef. 3331 unsigned Idx = CountTrailingZeros_32(NonZeros); 3332 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, 3333 Op.getOperand(Idx)); 3334 return getShuffleVectorZeroOrUndef(V2, Idx, true, 3335 Subtarget->hasSSE2(), DAG); 3336 } 3337 return SDValue(); 3338 } 3339 3340 // If element VT is < 32 bits, convert it to inserts into a zero vector. 3341 if (EVTBits == 8 && NumElems == 16) { 3342 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, 3343 *this); 3344 if (V.getNode()) return V; 3345 } 3346 3347 if (EVTBits == 16 && NumElems == 8) { 3348 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, 3349 *this); 3350 if (V.getNode()) return V; 3351 } 3352 3353 // If element VT is == 32 bits, turn it into a number of shuffles. 3354 SmallVector<SDValue, 8> V; 3355 V.resize(NumElems); 3356 if (NumElems == 4 && NumZero > 0) { 3357 for (unsigned i = 0; i < 4; ++i) { 3358 bool isZero = !(NonZeros & (1 << i)); 3359 if (isZero) 3360 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl); 3361 else 3362 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); 3363 } 3364 3365 for (unsigned i = 0; i < 2; ++i) { 3366 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { 3367 default: break; 3368 case 0: 3369 V[i] = V[i*2]; // Must be a zero vector. 3370 break; 3371 case 1: 3372 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]); 3373 break; 3374 case 2: 3375 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]); 3376 break; 3377 case 3: 3378 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]); 3379 break; 3380 } 3381 } 3382 3383 SmallVector<int, 8> MaskVec; 3384 bool Reverse = (NonZeros & 0x3) == 2; 3385 for (unsigned i = 0; i < 2; ++i) 3386 MaskVec.push_back(Reverse ? 1-i : i); 3387 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; 3388 for (unsigned i = 0; i < 2; ++i) 3389 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems); 3390 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]); 3391 } 3392 3393 if (Values.size() > 2) { 3394 // If we have SSE 4.1, Expand into a number of inserts unless the number of 3395 // values to be inserted is equal to the number of elements, in which case 3396 // use the unpack code below in the hopes of matching the consecutive elts 3397 // load merge pattern for shuffles. 3398 // FIXME: We could probably just check that here directly. 3399 if (Values.size() < NumElems && VT.getSizeInBits() == 128 && 3400 getSubtarget()->hasSSE41()) { 3401 V[0] = DAG.getUNDEF(VT); 3402 for (unsigned i = 0; i < NumElems; ++i) 3403 if (Op.getOperand(i).getOpcode() != ISD::UNDEF) 3404 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0], 3405 Op.getOperand(i), DAG.getIntPtrConstant(i)); 3406 return V[0]; 3407 } 3408 // Expand into a number of unpckl*. 3409 // e.g. for v4f32 3410 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> 3411 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> 3412 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> 3413 for (unsigned i = 0; i < NumElems; ++i) 3414 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); 3415 NumElems >>= 1; 3416 while (NumElems != 0) { 3417 for (unsigned i = 0; i < NumElems; ++i) 3418 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]); 3419 NumElems >>= 1; 3420 } 3421 return V[0]; 3422 } 3423 3424 return SDValue(); 3425 } 3426 3427 // v8i16 shuffles - Prefer shuffles in the following order: 3428 // 1. [all] pshuflw, pshufhw, optional move 3429 // 2. [ssse3] 1 x pshufb 3430 // 3. [ssse3] 2 x pshufb + 1 x por 3431 // 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw) 3432 static 3433 SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp, 3434 SelectionDAG &DAG, X86TargetLowering &TLI) { 3435 SDValue V1 = SVOp->getOperand(0); 3436 SDValue V2 = SVOp->getOperand(1); 3437 DebugLoc dl = SVOp->getDebugLoc(); 3438 SmallVector<int, 8> MaskVals; 3439 3440 // Determine if more than 1 of the words in each of the low and high quadwords 3441 // of the result come from the same quadword of one of the two inputs. Undef 3442 // mask values count as coming from any quadword, for better codegen. 3443 SmallVector<unsigned, 4> LoQuad(4); 3444 SmallVector<unsigned, 4> HiQuad(4); 3445 BitVector InputQuads(4); 3446 for (unsigned i = 0; i < 8; ++i) { 3447 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad; 3448 int EltIdx = SVOp->getMaskElt(i); 3449 MaskVals.push_back(EltIdx); 3450 if (EltIdx < 0) { 3451 ++Quad[0]; 3452 ++Quad[1]; 3453 ++Quad[2]; 3454 ++Quad[3]; 3455 continue; 3456 } 3457 ++Quad[EltIdx / 4]; 3458 InputQuads.set(EltIdx / 4); 3459 } 3460 3461 int BestLoQuad = -1; 3462 unsigned MaxQuad = 1; 3463 for (unsigned i = 0; i < 4; ++i) { 3464 if (LoQuad[i] > MaxQuad) { 3465 BestLoQuad = i; 3466 MaxQuad = LoQuad[i]; 3467 } 3468 } 3469 3470 int BestHiQuad = -1; 3471 MaxQuad = 1; 3472 for (unsigned i = 0; i < 4; ++i) { 3473 if (HiQuad[i] > MaxQuad) { 3474 BestHiQuad = i; 3475 MaxQuad = HiQuad[i]; 3476 } 3477 } 3478 3479 // For SSSE3, If all 8 words of the result come from only 1 quadword of each 3480 // of the two input vectors, shuffle them into one input vector so only a 3481 // single pshufb instruction is necessary. If There are more than 2 input 3482 // quads, disable the next transformation since it does not help SSSE3. 3483 bool V1Used = InputQuads[0] || InputQuads[1]; 3484 bool V2Used = InputQuads[2] || InputQuads[3]; 3485 if (TLI.getSubtarget()->hasSSSE3()) { 3486 if (InputQuads.count() == 2 && V1Used && V2Used) { 3487 BestLoQuad = InputQuads.find_first(); 3488 BestHiQuad = InputQuads.find_next(BestLoQuad); 3489 } 3490 if (InputQuads.count() > 2) { 3491 BestLoQuad = -1; 3492 BestHiQuad = -1; 3493 } 3494 } 3495 3496 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update 3497 // the shuffle mask. If a quad is scored as -1, that means that it contains 3498 // words from all 4 input quadwords. 3499 SDValue NewV; 3500 if (BestLoQuad >= 0 || BestHiQuad >= 0) { 3501 SmallVector<int, 8> MaskV; 3502 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad); 3503 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad); 3504 NewV = DAG.getVectorShuffle(MVT::v2i64, dl, 3505 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1), 3506 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]); 3507 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV); 3508 3509 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the 3510 // source words for the shuffle, to aid later transformations. 3511 bool AllWordsInNewV = true; 3512 bool InOrder[2] = { true, true }; 3513 for (unsigned i = 0; i != 8; ++i) { 3514 int idx = MaskVals[i]; 3515 if (idx != (int)i) 3516 InOrder[i/4] = false; 3517 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad) 3518 continue; 3519 AllWordsInNewV = false; 3520 break; 3521 } 3522 3523 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV; 3524 if (AllWordsInNewV) { 3525 for (int i = 0; i != 8; ++i) { 3526 int idx = MaskVals[i]; 3527 if (idx < 0) 3528 continue; 3529 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4; 3530 if ((idx != i) && idx < 4) 3531 pshufhw = false; 3532 if ((idx != i) && idx > 3) 3533 pshuflw = false; 3534 } 3535 V1 = NewV; 3536 V2Used = false; 3537 BestLoQuad = 0; 3538 BestHiQuad = 1; 3539 } 3540 3541 // If we've eliminated the use of V2, and the new mask is a pshuflw or 3542 // pshufhw, that's as cheap as it gets. Return the new shuffle. 3543 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) { 3544 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV, 3545 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]); 3546 } 3547 } 3548 3549 // If we have SSSE3, and all words of the result are from 1 input vector, 3550 // case 2 is generated, otherwise case 3 is generated. If no SSSE3 3551 // is present, fall back to case 4. 3552 if (TLI.getSubtarget()->hasSSSE3()) { 3553 SmallVector<SDValue,16> pshufbMask; 3554 3555 // If we have elements from both input vectors, set the high bit of the 3556 // shuffle mask element to zero out elements that come from V2 in the V1 3557 // mask, and elements that come from V1 in the V2 mask, so that the two 3558 // results can be OR'd together. 3559 bool TwoInputs = V1Used && V2Used; 3560 for (unsigned i = 0; i != 8; ++i) { 3561 int EltIdx = MaskVals[i] * 2; 3562 if (TwoInputs && (EltIdx >= 16)) { 3563 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); 3564 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); 3565 continue; 3566 } 3567 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); 3568 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8)); 3569 } 3570 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1); 3571 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, 3572 DAG.getNode(ISD::BUILD_VECTOR, dl, 3573 MVT::v16i8, &pshufbMask[0], 16)); 3574 if (!TwoInputs) 3575 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); 3576 3577 // Calculate the shuffle mask for the second input, shuffle it, and 3578 // OR it with the first shuffled input. 3579 pshufbMask.clear(); 3580 for (unsigned i = 0; i != 8; ++i) { 3581 int EltIdx = MaskVals[i] * 2; 3582 if (EltIdx < 16) { 3583 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); 3584 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); 3585 continue; 3586 } 3587 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8)); 3588 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8)); 3589 } 3590 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2); 3591 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, 3592 DAG.getNode(ISD::BUILD_VECTOR, dl, 3593 MVT::v16i8, &pshufbMask[0], 16)); 3594 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); 3595 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); 3596 } 3597 3598 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order, 3599 // and update MaskVals with new element order. 3600 BitVector InOrder(8); 3601 if (BestLoQuad >= 0) { 3602 SmallVector<int, 8> MaskV; 3603 for (int i = 0; i != 4; ++i) { 3604 int idx = MaskVals[i]; 3605 if (idx < 0) { 3606 MaskV.push_back(-1); 3607 InOrder.set(i); 3608 } else if ((idx / 4) == BestLoQuad) { 3609 MaskV.push_back(idx & 3); 3610 InOrder.set(i); 3611 } else { 3612 MaskV.push_back(-1); 3613 } 3614 } 3615 for (unsigned i = 4; i != 8; ++i) 3616 MaskV.push_back(i); 3617 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), 3618 &MaskV[0]); 3619 } 3620 3621 // If BestHi >= 0, generate a pshufhw to put the high elements in order, 3622 // and update MaskVals with the new element order. 3623 if (BestHiQuad >= 0) { 3624 SmallVector<int, 8> MaskV; 3625 for (unsigned i = 0; i != 4; ++i) 3626 MaskV.push_back(i); 3627 for (unsigned i = 4; i != 8; ++i) { 3628 int idx = MaskVals[i]; 3629 if (idx < 0) { 3630 MaskV.push_back(-1); 3631 InOrder.set(i); 3632 } else if ((idx / 4) == BestHiQuad) { 3633 MaskV.push_back((idx & 3) + 4); 3634 InOrder.set(i); 3635 } else { 3636 MaskV.push_back(-1); 3637 } 3638 } 3639 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), 3640 &MaskV[0]); 3641 } 3642 3643 // In case BestHi & BestLo were both -1, which means each quadword has a word 3644 // from each of the four input quadwords, calculate the InOrder bitvector now 3645 // before falling through to the insert/extract cleanup. 3646 if (BestLoQuad == -1 && BestHiQuad == -1) { 3647 NewV = V1; 3648 for (int i = 0; i != 8; ++i) 3649 if (MaskVals[i] < 0 || MaskVals[i] == i) 3650 InOrder.set(i); 3651 } 3652 3653 // The other elements are put in the right place using pextrw and pinsrw. 3654 for (unsigned i = 0; i != 8; ++i) { 3655 if (InOrder[i]) 3656 continue; 3657 int EltIdx = MaskVals[i]; 3658 if (EltIdx < 0) 3659 continue; 3660 SDValue ExtOp = (EltIdx < 8) 3661 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1, 3662 DAG.getIntPtrConstant(EltIdx)) 3663 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2, 3664 DAG.getIntPtrConstant(EltIdx - 8)); 3665 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp, 3666 DAG.getIntPtrConstant(i)); 3667 } 3668 return NewV; 3669 } 3670 3671 // v16i8 shuffles - Prefer shuffles in the following order: 3672 // 1. [ssse3] 1 x pshufb 3673 // 2. [ssse3] 2 x pshufb + 1 x por 3674 // 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw 3675 static 3676 SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp, 3677 SelectionDAG &DAG, X86TargetLowering &TLI) { 3678 SDValue V1 = SVOp->getOperand(0); 3679 SDValue V2 = SVOp->getOperand(1); 3680 DebugLoc dl = SVOp->getDebugLoc(); 3681 SmallVector<int, 16> MaskVals; 3682 SVOp->getMask(MaskVals); 3683 3684 // If we have SSSE3, case 1 is generated when all result bytes come from 3685 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is 3686 // present, fall back to case 3. 3687 // FIXME: kill V2Only once shuffles are canonizalized by getNode. 3688 bool V1Only = true; 3689 bool V2Only = true; 3690 for (unsigned i = 0; i < 16; ++i) { 3691 int EltIdx = MaskVals[i]; 3692 if (EltIdx < 0) 3693 continue; 3694 if (EltIdx < 16) 3695 V2Only = false; 3696 else 3697 V1Only = false; 3698 } 3699 3700 // If SSSE3, use 1 pshufb instruction per vector with elements in the result. 3701 if (TLI.getSubtarget()->hasSSSE3()) { 3702 SmallVector<SDValue,16> pshufbMask; 3703 3704 // If all result elements are from one input vector, then only translate 3705 // undef mask values to 0x80 (zero out result) in the pshufb mask. 3706 // 3707 // Otherwise, we have elements from both input vectors, and must zero out 3708 // elements that come from V2 in the first mask, and V1 in the second mask 3709 // so that we can OR them together. 3710 bool TwoInputs = !(V1Only || V2Only); 3711 for (unsigned i = 0; i != 16; ++i) { 3712 int EltIdx = MaskVals[i]; 3713 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) { 3714 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); 3715 continue; 3716 } 3717 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); 3718 } 3719 // If all the elements are from V2, assign it to V1 and return after 3720 // building the first pshufb. 3721 if (V2Only) 3722 V1 = V2; 3723 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, 3724 DAG.getNode(ISD::BUILD_VECTOR, dl, 3725 MVT::v16i8, &pshufbMask[0], 16)); 3726 if (!TwoInputs) 3727 return V1; 3728 3729 // Calculate the shuffle mask for the second input, shuffle it, and 3730 // OR it with the first shuffled input. 3731 pshufbMask.clear(); 3732 for (unsigned i = 0; i != 16; ++i) { 3733 int EltIdx = MaskVals[i]; 3734 if (EltIdx < 16) { 3735 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); 3736 continue; 3737 } 3738 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8)); 3739 } 3740 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, 3741 DAG.getNode(ISD::BUILD_VECTOR, dl, 3742 MVT::v16i8, &pshufbMask[0], 16)); 3743 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); 3744 } 3745 3746 // No SSSE3 - Calculate in place words and then fix all out of place words 3747 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from 3748 // the 16 different words that comprise the two doublequadword input vectors. 3749 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); 3750 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2); 3751 SDValue NewV = V2Only ? V2 : V1; 3752 for (int i = 0; i != 8; ++i) { 3753 int Elt0 = MaskVals[i*2]; 3754 int Elt1 = MaskVals[i*2+1]; 3755 3756 // This word of the result is all undef, skip it. 3757 if (Elt0 < 0 && Elt1 < 0) 3758 continue; 3759 3760 // This word of the result is already in the correct place, skip it. 3761 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1)) 3762 continue; 3763 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17)) 3764 continue; 3765 3766 SDValue Elt0Src = Elt0 < 16 ? V1 : V2; 3767 SDValue Elt1Src = Elt1 < 16 ? V1 : V2; 3768 SDValue InsElt; 3769 3770 // If Elt0 and Elt1 are defined, are consecutive, and can be load 3771 // using a single extract together, load it and store it. 3772 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) { 3773 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, 3774 DAG.getIntPtrConstant(Elt1 / 2)); 3775 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, 3776 DAG.getIntPtrConstant(i)); 3777 continue; 3778 } 3779 3780 // If Elt1 is defined, extract it from the appropriate source. If the 3781 // source byte is not also odd, shift the extracted word left 8 bits 3782 // otherwise clear the bottom 8 bits if we need to do an or. 3783 if (Elt1 >= 0) { 3784 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, 3785 DAG.getIntPtrConstant(Elt1 / 2)); 3786 if ((Elt1 & 1) == 0) 3787 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt, 3788 DAG.getConstant(8, TLI.getShiftAmountTy())); 3789 else if (Elt0 >= 0) 3790 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt, 3791 DAG.getConstant(0xFF00, MVT::i16)); 3792 } 3793 // If Elt0 is defined, extract it from the appropriate source. If the 3794 // source byte is not also even, shift the extracted word right 8 bits. If 3795 // Elt1 was also defined, OR the extracted values together before 3796 // inserting them in the result. 3797 if (Elt0 >= 0) { 3798 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, 3799 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2)); 3800 if ((Elt0 & 1) != 0) 3801 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0, 3802 DAG.getConstant(8, TLI.getShiftAmountTy())); 3803 else if (Elt1 >= 0) 3804 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0, 3805 DAG.getConstant(0x00FF, MVT::i16)); 3806 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0) 3807 : InsElt0; 3808 } 3809 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, 3810 DAG.getIntPtrConstant(i)); 3811 } 3812 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV); 3813 } 3814 3815 /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide 3816 /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be 3817 /// done when every pair / quad of shuffle mask elements point to elements in 3818 /// the right sequence. e.g. 3819 /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15> 3820 static 3821 SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp, 3822 SelectionDAG &DAG, 3823 TargetLowering &TLI, DebugLoc dl) { 3824 MVT VT = SVOp->getValueType(0); 3825 SDValue V1 = SVOp->getOperand(0); 3826 SDValue V2 = SVOp->getOperand(1); 3827 unsigned NumElems = VT.getVectorNumElements(); 3828 unsigned NewWidth = (NumElems == 4) ? 2 : 4; 3829 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth); 3830 MVT MaskEltVT = MaskVT.getVectorElementType(); 3831 MVT NewVT = MaskVT; 3832 switch (VT.getSimpleVT()) { 3833 default: assert(false && "Unexpected!"); 3834 case MVT::v4f32: NewVT = MVT::v2f64; break; 3835 case MVT::v4i32: NewVT = MVT::v2i64; break; 3836 case MVT::v8i16: NewVT = MVT::v4i32; break; 3837 case MVT::v16i8: NewVT = MVT::v4i32; break; 3838 } 3839 3840 if (NewWidth == 2) { 3841 if (VT.isInteger()) 3842 NewVT = MVT::v2i64; 3843 else 3844 NewVT = MVT::v2f64; 3845 } 3846 int Scale = NumElems / NewWidth; 3847 SmallVector<int, 8> MaskVec; 3848 for (unsigned i = 0; i < NumElems; i += Scale) { 3849 int StartIdx = -1; 3850 for (int j = 0; j < Scale; ++j) { 3851 int EltIdx = SVOp->getMaskElt(i+j); 3852 if (EltIdx < 0) 3853 continue; 3854 if (StartIdx == -1) 3855 StartIdx = EltIdx - (EltIdx % Scale); 3856 if (EltIdx != StartIdx + j) 3857 return SDValue(); 3858 } 3859 if (StartIdx == -1) 3860 MaskVec.push_back(-1); 3861 else 3862 MaskVec.push_back(StartIdx / Scale); 3863 } 3864 3865 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1); 3866 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2); 3867 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]); 3868 } 3869 3870 /// getVZextMovL - Return a zero-extending vector move low node. 3871 /// 3872 static SDValue getVZextMovL(MVT VT, MVT OpVT, 3873 SDValue SrcOp, SelectionDAG &DAG, 3874 const X86Subtarget *Subtarget, DebugLoc dl) { 3875 if (VT == MVT::v2f64 || VT == MVT::v4f32) { 3876 LoadSDNode *LD = NULL; 3877 if (!isScalarLoadToVector(SrcOp.getNode(), &LD)) 3878 LD = dyn_cast<LoadSDNode>(SrcOp); 3879 if (!LD) { 3880 // movssrr and movsdrr do not clear top bits. Try to use movd, movq 3881 // instead. 3882 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32; 3883 if ((EVT != MVT::i64 || Subtarget->is64Bit()) && 3884 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR && 3885 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT && 3886 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) { 3887 // PR2108 3888 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32; 3889 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, 3890 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, 3891 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 3892 OpVT, 3893 SrcOp.getOperand(0) 3894 .getOperand(0)))); 3895 } 3896 } 3897 } 3898 3899 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, 3900 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, 3901 DAG.getNode(ISD::BIT_CONVERT, dl, 3902 OpVT, SrcOp))); 3903 } 3904 3905 /// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of 3906 /// shuffles. 3907 static SDValue 3908 LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { 3909 SDValue V1 = SVOp->getOperand(0); 3910 SDValue V2 = SVOp->getOperand(1); 3911 DebugLoc dl = SVOp->getDebugLoc(); 3912 MVT VT = SVOp->getValueType(0); 3913 3914 SmallVector<std::pair<int, int>, 8> Locs; 3915 Locs.resize(4); 3916 SmallVector<int, 8> Mask1(4U, -1); 3917 SmallVector<int, 8> PermMask; 3918 SVOp->getMask(PermMask); 3919 3920 unsigned NumHi = 0; 3921 unsigned NumLo = 0; 3922 for (unsigned i = 0; i != 4; ++i) { 3923 int Idx = PermMask[i]; 3924 if (Idx < 0) { 3925 Locs[i] = std::make_pair(-1, -1); 3926 } else { 3927 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!"); 3928 if (Idx < 4) { 3929 Locs[i] = std::make_pair(0, NumLo); 3930 Mask1[NumLo] = Idx; 3931 NumLo++; 3932 } else { 3933 Locs[i] = std::make_pair(1, NumHi); 3934 if (2+NumHi < 4) 3935 Mask1[2+NumHi] = Idx; 3936 NumHi++; 3937 } 3938 } 3939 } 3940 3941 if (NumLo <= 2 && NumHi <= 2) { 3942 // If no more than two elements come from either vector. This can be 3943 // implemented with two shuffles. First shuffle gather the elements. 3944 // The second shuffle, which takes the first shuffle as both of its 3945 // vector operands, put the elements into the right order. 3946 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); 3947 3948 SmallVector<int, 8> Mask2(4U, -1); 3949 3950 for (unsigned i = 0; i != 4; ++i) { 3951 if (Locs[i].first == -1) 3952 continue; 3953 else { 3954 unsigned Idx = (i < 2) ? 0 : 4; 3955 Idx += Locs[i].first * 2 + Locs[i].second; 3956 Mask2[i] = Idx; 3957 } 3958 } 3959 3960 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]); 3961 } else if (NumLo == 3 || NumHi == 3) { 3962 // Otherwise, we must have three elements from one vector, call it X, and 3963 // one element from the other, call it Y. First, use a shufps to build an 3964 // intermediate vector with the one element from Y and the element from X 3965 // that will be in the same half in the final destination (the indexes don't 3966 // matter). Then, use a shufps to build the final vector, taking the half 3967 // containing the element from Y from the intermediate, and the other half 3968 // from X. 3969 if (NumHi == 3) { 3970 // Normalize it so the 3 elements come from V1. 3971 CommuteVectorShuffleMask(PermMask, VT); 3972 std::swap(V1, V2); 3973 } 3974 3975 // Find the element from V2. 3976 unsigned HiIndex; 3977 for (HiIndex = 0; HiIndex < 3; ++HiIndex) { 3978 int Val = PermMask[HiIndex]; 3979 if (Val < 0) 3980 continue; 3981 if (Val >= 4) 3982 break; 3983 } 3984 3985 Mask1[0] = PermMask[HiIndex]; 3986 Mask1[1] = -1; 3987 Mask1[2] = PermMask[HiIndex^1]; 3988 Mask1[3] = -1; 3989 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); 3990 3991 if (HiIndex >= 2) { 3992 Mask1[0] = PermMask[0]; 3993 Mask1[1] = PermMask[1]; 3994 Mask1[2] = HiIndex & 1 ? 6 : 4; 3995 Mask1[3] = HiIndex & 1 ? 4 : 6; 3996 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); 3997 } else { 3998 Mask1[0] = HiIndex & 1 ? 2 : 0; 3999 Mask1[1] = HiIndex & 1 ? 0 : 2; 4000 Mask1[2] = PermMask[2]; 4001 Mask1[3] = PermMask[3]; 4002 if (Mask1[2] >= 0) 4003 Mask1[2] += 4; 4004 if (Mask1[3] >= 0) 4005 Mask1[3] += 4; 4006 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]); 4007 } 4008 } 4009 4010 // Break it into (shuffle shuffle_hi, shuffle_lo). 4011 Locs.clear(); 4012 SmallVector<int,8> LoMask(4U, -1); 4013 SmallVector<int,8> HiMask(4U, -1); 4014 4015 SmallVector<int,8> *MaskPtr = &LoMask; 4016 unsigned MaskIdx = 0; 4017 unsigned LoIdx = 0; 4018 unsigned HiIdx = 2; 4019 for (unsigned i = 0; i != 4; ++i) { 4020 if (i == 2) { 4021 MaskPtr = &HiMask; 4022 MaskIdx = 1; 4023 LoIdx = 0; 4024 HiIdx = 2; 4025 } 4026 int Idx = PermMask[i]; 4027 if (Idx < 0) { 4028 Locs[i] = std::make_pair(-1, -1); 4029 } else if (Idx < 4) { 4030 Locs[i] = std::make_pair(MaskIdx, LoIdx); 4031 (*MaskPtr)[LoIdx] = Idx; 4032 LoIdx++; 4033 } else { 4034 Locs[i] = std::make_pair(MaskIdx, HiIdx); 4035 (*MaskPtr)[HiIdx] = Idx; 4036 HiIdx++; 4037 } 4038 } 4039 4040 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]); 4041 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]); 4042 SmallVector<int, 8> MaskOps; 4043 for (unsigned i = 0; i != 4; ++i) { 4044 if (Locs[i].first == -1) { 4045 MaskOps.push_back(-1); 4046 } else { 4047 unsigned Idx = Locs[i].first * 4 + Locs[i].second; 4048 MaskOps.push_back(Idx); 4049 } 4050 } 4051 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]); 4052 } 4053 4054 SDValue 4055 X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 4056 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); 4057 SDValue V1 = Op.getOperand(0); 4058 SDValue V2 = Op.getOperand(1); 4059 MVT VT = Op.getValueType(); 4060 DebugLoc dl = Op.getDebugLoc(); 4061 unsigned NumElems = VT.getVectorNumElements(); 4062 bool isMMX = VT.getSizeInBits() == 64; 4063 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; 4064 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; 4065 bool V1IsSplat = false; 4066 bool V2IsSplat = false; 4067 4068 if (isZeroShuffle(SVOp)) 4069 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl); 4070 4071 // Promote splats to v4f32. 4072 if (SVOp->isSplat()) { 4073 if (isMMX || NumElems < 4) 4074 return Op; 4075 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2()); 4076 } 4077 4078 // If the shuffle can be profitably rewritten as a narrower shuffle, then 4079 // do it! 4080 if (VT == MVT::v8i16 || VT == MVT::v16i8) { 4081 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); 4082 if (NewOp.getNode()) 4083 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, 4084 LowerVECTOR_SHUFFLE(NewOp, DAG)); 4085 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) { 4086 // FIXME: Figure out a cleaner way to do this. 4087 // Try to make use of movq to zero out the top part. 4088 if (ISD::isBuildVectorAllZeros(V2.getNode())) { 4089 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); 4090 if (NewOp.getNode()) { 4091 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false)) 4092 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0), 4093 DAG, Subtarget, dl); 4094 } 4095 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) { 4096 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); 4097 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp))) 4098 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1), 4099 DAG, Subtarget, dl); 4100 } 4101 } 4102 4103 if (X86::isPSHUFDMask(SVOp)) 4104 return Op; 4105 4106 // Check if this can be converted into a logical shift. 4107 bool isLeft = false; 4108 unsigned ShAmt = 0; 4109 SDValue ShVal; 4110 bool isShift = getSubtarget()->hasSSE2() && 4111 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt); 4112 if (isShift && ShVal.hasOneUse()) { 4113 // If the shifted value has multiple uses, it may be cheaper to use 4114 // v_set0 + movlhps or movhlps, etc. 4115 MVT EVT = VT.getVectorElementType(); 4116 ShAmt *= EVT.getSizeInBits(); 4117 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); 4118 } 4119 4120 if (X86::isMOVLMask(SVOp)) { 4121 if (V1IsUndef) 4122 return V2; 4123 if (ISD::isBuildVectorAllZeros(V1.getNode())) 4124 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl); 4125 if (!isMMX) 4126 return Op; 4127 } 4128 4129 // FIXME: fold these into legal mask. 4130 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) || 4131 X86::isMOVSLDUPMask(SVOp) || 4132 X86::isMOVHLPSMask(SVOp) || 4133 X86::isMOVHPMask(SVOp) || 4134 X86::isMOVLPMask(SVOp))) 4135 return Op; 4136 4137 if (ShouldXformToMOVHLPS(SVOp) || 4138 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp)) 4139 return CommuteVectorShuffle(SVOp, DAG); 4140 4141 if (isShift) { 4142 // No better options. Use a vshl / vsrl. 4143 MVT EVT = VT.getVectorElementType(); 4144 ShAmt *= EVT.getSizeInBits(); 4145 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); 4146 } 4147 4148 bool Commuted = false; 4149 // FIXME: This should also accept a bitcast of a splat? Be careful, not 4150 // 1,1,1,1 -> v8i16 though. 4151 V1IsSplat = isSplatVector(V1.getNode()); 4152 V2IsSplat = isSplatVector(V2.getNode()); 4153 4154 // Canonicalize the splat or undef, if present, to be on the RHS. 4155 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { 4156 Op = CommuteVectorShuffle(SVOp, DAG); 4157 SVOp = cast<ShuffleVectorSDNode>(Op); 4158 V1 = SVOp->getOperand(0); 4159 V2 = SVOp->getOperand(1); 4160 std::swap(V1IsSplat, V2IsSplat); 4161 std::swap(V1IsUndef, V2IsUndef); 4162 Commuted = true; 4163 } 4164 4165 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) { 4166 // Shuffling low element of v1 into undef, just return v1. 4167 if (V2IsUndef) 4168 return V1; 4169 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which 4170 // the instruction selector will not match, so get a canonical MOVL with 4171 // swapped operands to undo the commute. 4172 return getMOVL(DAG, dl, VT, V2, V1); 4173 } 4174 4175 if (X86::isUNPCKL_v_undef_Mask(SVOp) || 4176 X86::isUNPCKH_v_undef_Mask(SVOp) || 4177 X86::isUNPCKLMask(SVOp) || 4178 X86::isUNPCKHMask(SVOp)) 4179 return Op; 4180 4181 if (V2IsSplat) { 4182 // Normalize mask so all entries that point to V2 points to its first 4183 // element then try to match unpck{h|l} again. If match, return a 4184 // new vector_shuffle with the corrected mask. 4185 SDValue NewMask = NormalizeMask(SVOp, DAG); 4186 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask); 4187 if (NSVOp != SVOp) { 4188 if (X86::isUNPCKLMask(NSVOp, true)) { 4189 return NewMask; 4190 } else if (X86::isUNPCKHMask(NSVOp, true)) { 4191 return NewMask; 4192 } 4193 } 4194 } 4195 4196 if (Commuted) { 4197 // Commute is back and try unpck* again. 4198 // FIXME: this seems wrong. 4199 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG); 4200 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp); 4201 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) || 4202 X86::isUNPCKH_v_undef_Mask(NewSVOp) || 4203 X86::isUNPCKLMask(NewSVOp) || 4204 X86::isUNPCKHMask(NewSVOp)) 4205 return NewOp; 4206 } 4207 4208 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle. 4209 4210 // Normalize the node to match x86 shuffle ops if needed 4211 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp)) 4212 return CommuteVectorShuffle(SVOp, DAG); 4213 4214 // Check for legal shuffle and return? 4215 SmallVector<int, 16> PermMask; 4216 SVOp->getMask(PermMask); 4217 if (isShuffleMaskLegal(PermMask, VT)) 4218 return Op; 4219 4220 // Handle v8i16 specifically since SSE can do byte extraction and insertion. 4221 if (VT == MVT::v8i16) { 4222 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this); 4223 if (NewOp.getNode()) 4224 return NewOp; 4225 } 4226 4227 if (VT == MVT::v16i8) { 4228 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this); 4229 if (NewOp.getNode()) 4230 return NewOp; 4231 } 4232 4233 // Handle all 4 wide cases with a number of shuffles except for MMX. 4234 if (NumElems == 4 && !isMMX) 4235 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG); 4236 4237 return SDValue(); 4238 } 4239 4240 SDValue 4241 X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, 4242 SelectionDAG &DAG) { 4243 MVT VT = Op.getValueType(); 4244 DebugLoc dl = Op.getDebugLoc(); 4245 if (VT.getSizeInBits() == 8) { 4246 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32, 4247 Op.getOperand(0), Op.getOperand(1)); 4248 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, 4249 DAG.getValueType(VT)); 4250 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); 4251 } else if (VT.getSizeInBits() == 16) { 4252 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4253 // If Idx is 0, it's cheaper to do a move instead of a pextrw. 4254 if (Idx == 0) 4255 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, 4256 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, 4257 DAG.getNode(ISD::BIT_CONVERT, dl, 4258 MVT::v4i32, 4259 Op.getOperand(0)), 4260 Op.getOperand(1))); 4261 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32, 4262 Op.getOperand(0), Op.getOperand(1)); 4263 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, 4264 DAG.getValueType(VT)); 4265 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); 4266 } else if (VT == MVT::f32) { 4267 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy 4268 // the result back to FR32 register. It's only worth matching if the 4269 // result has a single use which is a store or a bitcast to i32. And in 4270 // the case of a store, it's not worth it if the index is a constant 0, 4271 // because a MOVSSmr can be used instead, which is smaller and faster. 4272 if (!Op.hasOneUse()) 4273 return SDValue(); 4274 SDNode *User = *Op.getNode()->use_begin(); 4275 if ((User->getOpcode() != ISD::STORE || 4276 (isa<ConstantSDNode>(Op.getOperand(1)) && 4277 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) && 4278 (User->getOpcode() != ISD::BIT_CONVERT || 4279 User->getValueType(0) != MVT::i32)) 4280 return SDValue(); 4281 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, 4282 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, 4283 Op.getOperand(0)), 4284 Op.getOperand(1)); 4285 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract); 4286 } else if (VT == MVT::i32) { 4287 // ExtractPS works with constant index. 4288 if (isa<ConstantSDNode>(Op.getOperand(1))) 4289 return Op; 4290 } 4291 return SDValue(); 4292 } 4293 4294 4295 SDValue 4296 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 4297 if (!isa<ConstantSDNode>(Op.getOperand(1))) 4298 return SDValue(); 4299 4300 if (Subtarget->hasSSE41()) { 4301 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG); 4302 if (Res.getNode()) 4303 return Res; 4304 } 4305 4306 MVT VT = Op.getValueType(); 4307 DebugLoc dl = Op.getDebugLoc(); 4308 // TODO: handle v16i8. 4309 if (VT.getSizeInBits() == 16) { 4310 SDValue Vec = Op.getOperand(0); 4311 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4312 if (Idx == 0) 4313 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, 4314 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, 4315 DAG.getNode(ISD::BIT_CONVERT, dl, 4316 MVT::v4i32, Vec), 4317 Op.getOperand(1))); 4318 // Transform it so it match pextrw which produces a 32-bit result. 4319 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1); 4320 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT, 4321 Op.getOperand(0), Op.getOperand(1)); 4322 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract, 4323 DAG.getValueType(VT)); 4324 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); 4325 } else if (VT.getSizeInBits() == 32) { 4326 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4327 if (Idx == 0) 4328 return Op; 4329 4330 // SHUFPS the element to the lowest double word, then movss. 4331 int Mask[4] = { Idx, -1, -1, -1 }; 4332 MVT VVT = Op.getOperand(0).getValueType(); 4333 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), 4334 DAG.getUNDEF(VVT), Mask); 4335 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, 4336 DAG.getIntPtrConstant(0)); 4337 } else if (VT.getSizeInBits() == 64) { 4338 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b 4339 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught 4340 // to match extract_elt for f64. 4341 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4342 if (Idx == 0) 4343 return Op; 4344 4345 // UNPCKHPD the element to the lowest double word, then movsd. 4346 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored 4347 // to a f64mem, the whole operation is folded into a single MOVHPDmr. 4348 int Mask[2] = { 1, -1 }; 4349 MVT VVT = Op.getOperand(0).getValueType(); 4350 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), 4351 DAG.getUNDEF(VVT), Mask); 4352 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, 4353 DAG.getIntPtrConstant(0)); 4354 } 4355 4356 return SDValue(); 4357 } 4358 4359 SDValue 4360 X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){ 4361 MVT VT = Op.getValueType(); 4362 MVT EVT = VT.getVectorElementType(); 4363 DebugLoc dl = Op.getDebugLoc(); 4364 4365 SDValue N0 = Op.getOperand(0); 4366 SDValue N1 = Op.getOperand(1); 4367 SDValue N2 = Op.getOperand(2); 4368 4369 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) && 4370 isa<ConstantSDNode>(N2)) { 4371 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB 4372 : X86ISD::PINSRW; 4373 // Transform it so it match pinsr{b,w} which expects a GR32 as its second 4374 // argument. 4375 if (N1.getValueType() != MVT::i32) 4376 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); 4377 if (N2.getValueType() != MVT::i32) 4378 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); 4379 return DAG.getNode(Opc, dl, VT, N0, N1, N2); 4380 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) { 4381 // Bits [7:6] of the constant are the source select. This will always be 4382 // zero here. The DAG Combiner may combine an extract_elt index into these 4383 // bits. For example (insert (extract, 3), 2) could be matched by putting 4384 // the '3' into bits [7:6] of X86ISD::INSERTPS. 4385 // Bits [5:4] of the constant are the destination select. This is the 4386 // value of the incoming immediate. 4387 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may 4388 // combine either bitwise AND or insert of float 0.0 to set these bits. 4389 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4); 4390 // Create this as a scalar to vector.. 4391 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1); 4392 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2); 4393 } else if (EVT == MVT::i32 && isa<ConstantSDNode>(N2)) { 4394 // PINSR* works with constant index. 4395 return Op; 4396 } 4397 return SDValue(); 4398 } 4399 4400 SDValue 4401 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 4402 MVT VT = Op.getValueType(); 4403 MVT EVT = VT.getVectorElementType(); 4404 4405 if (Subtarget->hasSSE41()) 4406 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG); 4407 4408 if (EVT == MVT::i8) 4409 return SDValue(); 4410 4411 DebugLoc dl = Op.getDebugLoc(); 4412 SDValue N0 = Op.getOperand(0); 4413 SDValue N1 = Op.getOperand(1); 4414 SDValue N2 = Op.getOperand(2); 4415 4416 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) { 4417 // Transform it so it match pinsrw which expects a 16-bit value in a GR32 4418 // as its second argument. 4419 if (N1.getValueType() != MVT::i32) 4420 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); 4421 if (N2.getValueType() != MVT::i32) 4422 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); 4423 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2); 4424 } 4425 return SDValue(); 4426 } 4427 4428 SDValue 4429 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) { 4430 DebugLoc dl = Op.getDebugLoc(); 4431 if (Op.getValueType() == MVT::v2f32) 4432 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32, 4433 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32, 4434 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, 4435 Op.getOperand(0)))); 4436 4437 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64) 4438 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0)); 4439 4440 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0)); 4441 MVT VT = MVT::v2i32; 4442 switch (Op.getValueType().getSimpleVT()) { 4443 default: break; 4444 case MVT::v16i8: 4445 case MVT::v8i16: 4446 VT = MVT::v4i32; 4447 break; 4448 } 4449 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), 4450 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt)); 4451 } 4452 4453 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 4454 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is 4455 // one of the above mentioned nodes. It has to be wrapped because otherwise 4456 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 4457 // be used to form addressing mode. These wrapped nodes will be selected 4458 // into MOV32ri. 4459 SDValue 4460 X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 4461 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 4462 4463 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the 4464 // global base reg. 4465 unsigned char OpFlag = 0; 4466 unsigned WrapperKind = X86ISD::Wrapper; 4467 4468 if (Subtarget->isPICStyleRIPRel() && 4469 getTargetMachine().getCodeModel() == CodeModel::Small) 4470 WrapperKind = X86ISD::WrapperRIP; 4471 else if (Subtarget->isPICStyleGOT()) 4472 OpFlag = X86II::MO_GOTOFF; 4473 else if (Subtarget->isPICStyleStubPIC()) 4474 OpFlag = X86II::MO_PIC_BASE_OFFSET; 4475 4476 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(), 4477 CP->getAlignment(), 4478 CP->getOffset(), OpFlag); 4479 DebugLoc DL = CP->getDebugLoc(); 4480 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); 4481 // With PIC, the address is actually $g + Offset. 4482 if (OpFlag) { 4483 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), 4484 DAG.getNode(X86ISD::GlobalBaseReg, 4485 DebugLoc::getUnknownLoc(), getPointerTy()), 4486 Result); 4487 } 4488 4489 return Result; 4490 } 4491 4492 SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) { 4493 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 4494 4495 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the 4496 // global base reg. 4497 unsigned char OpFlag = 0; 4498 unsigned WrapperKind = X86ISD::Wrapper; 4499 4500 if (Subtarget->isPICStyleRIPRel() && 4501 getTargetMachine().getCodeModel() == CodeModel::Small) 4502 WrapperKind = X86ISD::WrapperRIP; 4503 else if (Subtarget->isPICStyleGOT()) 4504 OpFlag = X86II::MO_GOTOFF; 4505 else if (Subtarget->isPICStyleStubPIC()) 4506 OpFlag = X86II::MO_PIC_BASE_OFFSET; 4507 4508 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(), 4509 OpFlag); 4510 DebugLoc DL = JT->getDebugLoc(); 4511 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); 4512 4513 // With PIC, the address is actually $g + Offset. 4514 if (OpFlag) { 4515 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), 4516 DAG.getNode(X86ISD::GlobalBaseReg, 4517 DebugLoc::getUnknownLoc(), getPointerTy()), 4518 Result); 4519 } 4520 4521 return Result; 4522 } 4523 4524 SDValue 4525 X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) { 4526 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); 4527 4528 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the 4529 // global base reg. 4530 unsigned char OpFlag = 0; 4531 unsigned WrapperKind = X86ISD::Wrapper; 4532 if (Subtarget->isPICStyleRIPRel() && 4533 getTargetMachine().getCodeModel() == CodeModel::Small) 4534 WrapperKind = X86ISD::WrapperRIP; 4535 else if (Subtarget->isPICStyleGOT()) 4536 OpFlag = X86II::MO_GOTOFF; 4537 else if (Subtarget->isPICStyleStubPIC()) 4538 OpFlag = X86II::MO_PIC_BASE_OFFSET; 4539 4540 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag); 4541 4542 DebugLoc DL = Op.getDebugLoc(); 4543 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); 4544 4545 4546 // With PIC, the address is actually $g + Offset. 4547 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 4548 !Subtarget->is64Bit()) { 4549 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), 4550 DAG.getNode(X86ISD::GlobalBaseReg, 4551 DebugLoc::getUnknownLoc(), 4552 getPointerTy()), 4553 Result); 4554 } 4555 4556 return Result; 4557 } 4558 4559 SDValue 4560 X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl, 4561 int64_t Offset, 4562 SelectionDAG &DAG) const { 4563 // Create the TargetGlobalAddress node, folding in the constant 4564 // offset if it is legal. 4565 unsigned char OpFlags = 4566 Subtarget->ClassifyGlobalReference(GV, getTargetMachine()); 4567 SDValue Result; 4568 if (OpFlags == X86II::MO_NO_FLAG && isInt32(Offset)) { 4569 // A direct static reference to a global. 4570 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset); 4571 Offset = 0; 4572 } else { 4573 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags); 4574 } 4575 4576 if (Subtarget->isPICStyleRIPRel() && 4577 getTargetMachine().getCodeModel() == CodeModel::Small) 4578 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result); 4579 else 4580 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); 4581 4582 // With PIC, the address is actually $g + Offset. 4583 if (isGlobalRelativeToPICBase(OpFlags)) { 4584 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), 4585 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()), 4586 Result); 4587 } 4588 4589 // For globals that require a load from a stub to get the address, emit the 4590 // load. 4591 if (isGlobalStubReference(OpFlags)) 4592 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result, 4593 PseudoSourceValue::getGOT(), 0); 4594 4595 // If there was a non-zero offset that we didn't fold, create an explicit 4596 // addition for it. 4597 if (Offset != 0) 4598 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result, 4599 DAG.getConstant(Offset, getPointerTy())); 4600 4601 return Result; 4602 } 4603 4604 SDValue 4605 X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) { 4606 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 4607 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); 4608 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG); 4609 } 4610 4611 static SDValue 4612 GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA, 4613 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg, 4614 unsigned char OperandFlags) { 4615 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 4616 DebugLoc dl = GA->getDebugLoc(); 4617 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), 4618 GA->getValueType(0), 4619 GA->getOffset(), 4620 OperandFlags); 4621 if (InFlag) { 4622 SDValue Ops[] = { Chain, TGA, *InFlag }; 4623 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3); 4624 } else { 4625 SDValue Ops[] = { Chain, TGA }; 4626 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2); 4627 } 4628 SDValue Flag = Chain.getValue(1); 4629 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag); 4630 } 4631 4632 // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit 4633 static SDValue 4634 LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG, 4635 const MVT PtrVT) { 4636 SDValue InFlag; 4637 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better 4638 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, 4639 DAG.getNode(X86ISD::GlobalBaseReg, 4640 DebugLoc::getUnknownLoc(), 4641 PtrVT), InFlag); 4642 InFlag = Chain.getValue(1); 4643 4644 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD); 4645 } 4646 4647 // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit 4648 static SDValue 4649 LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG, 4650 const MVT PtrVT) { 4651 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, 4652 X86::RAX, X86II::MO_TLSGD); 4653 } 4654 4655 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or 4656 // "local exec" model. 4657 static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, 4658 const MVT PtrVT, TLSModel::Model model, 4659 bool is64Bit) { 4660 DebugLoc dl = GA->getDebugLoc(); 4661 // Get the Thread Pointer 4662 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress, 4663 DebugLoc::getUnknownLoc(), PtrVT, 4664 DAG.getRegister(is64Bit? X86::FS : X86::GS, 4665 MVT::i32)); 4666 4667 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base, 4668 NULL, 0); 4669 4670 unsigned char OperandFlags = 0; 4671 // Most TLS accesses are not RIP relative, even on x86-64. One exception is 4672 // initialexec. 4673 unsigned WrapperKind = X86ISD::Wrapper; 4674 if (model == TLSModel::LocalExec) { 4675 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF; 4676 } else if (is64Bit) { 4677 assert(model == TLSModel::InitialExec); 4678 OperandFlags = X86II::MO_GOTTPOFF; 4679 WrapperKind = X86ISD::WrapperRIP; 4680 } else { 4681 assert(model == TLSModel::InitialExec); 4682 OperandFlags = X86II::MO_INDNTPOFF; 4683 } 4684 4685 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial 4686 // exec) 4687 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), 4688 GA->getOffset(), OperandFlags); 4689 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA); 4690 4691 if (model == TLSModel::InitialExec) 4692 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset, 4693 PseudoSourceValue::getGOT(), 0); 4694 4695 // The address of the thread local variable is the add of the thread 4696 // pointer with the offset of the variable. 4697 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 4698 } 4699 4700 SDValue 4701 X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) { 4702 // TODO: implement the "local dynamic" model 4703 // TODO: implement the "initial exec"model for pic executables 4704 assert(Subtarget->isTargetELF() && 4705 "TLS not implemented for non-ELF targets"); 4706 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 4707 const GlobalValue *GV = GA->getGlobal(); 4708 4709 // If GV is an alias then use the aliasee for determining 4710 // thread-localness. 4711 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 4712 GV = GA->resolveAliasedGlobal(false); 4713 4714 TLSModel::Model model = getTLSModel(GV, 4715 getTargetMachine().getRelocationModel()); 4716 4717 switch (model) { 4718 case TLSModel::GeneralDynamic: 4719 case TLSModel::LocalDynamic: // not implemented 4720 if (Subtarget->is64Bit()) 4721 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy()); 4722 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy()); 4723 4724 case TLSModel::InitialExec: 4725 case TLSModel::LocalExec: 4726 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, 4727 Subtarget->is64Bit()); 4728 } 4729 4730 llvm_unreachable("Unreachable"); 4731 return SDValue(); 4732 } 4733 4734 4735 /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and 4736 /// take a 2 x i32 value to shift plus a shift amount. 4737 SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) { 4738 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4739 MVT VT = Op.getValueType(); 4740 unsigned VTBits = VT.getSizeInBits(); 4741 DebugLoc dl = Op.getDebugLoc(); 4742 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; 4743 SDValue ShOpLo = Op.getOperand(0); 4744 SDValue ShOpHi = Op.getOperand(1); 4745 SDValue ShAmt = Op.getOperand(2); 4746 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi, 4747 DAG.getConstant(VTBits - 1, MVT::i8)) 4748 : DAG.getConstant(0, VT); 4749 4750 SDValue Tmp2, Tmp3; 4751 if (Op.getOpcode() == ISD::SHL_PARTS) { 4752 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt); 4753 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 4754 } else { 4755 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt); 4756 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt); 4757 } 4758 4759 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt, 4760 DAG.getConstant(VTBits, MVT::i8)); 4761 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT, 4762 AndNode, DAG.getConstant(0, MVT::i8)); 4763 4764 SDValue Hi, Lo; 4765 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8); 4766 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond }; 4767 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond }; 4768 4769 if (Op.getOpcode() == ISD::SHL_PARTS) { 4770 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); 4771 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); 4772 } else { 4773 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); 4774 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); 4775 } 4776 4777 SDValue Ops[2] = { Lo, Hi }; 4778 return DAG.getMergeValues(Ops, 2, dl); 4779 } 4780 4781 SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 4782 MVT SrcVT = Op.getOperand(0).getValueType(); 4783 4784 if (SrcVT.isVector()) { 4785 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) { 4786 return Op; 4787 } 4788 return SDValue(); 4789 } 4790 4791 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 && 4792 "Unknown SINT_TO_FP to lower!"); 4793 4794 // These are really Legal; return the operand so the caller accepts it as 4795 // Legal. 4796 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType())) 4797 return Op; 4798 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) && 4799 Subtarget->is64Bit()) { 4800 return Op; 4801 } 4802 4803 DebugLoc dl = Op.getDebugLoc(); 4804 unsigned Size = SrcVT.getSizeInBits()/8; 4805 MachineFunction &MF = DAG.getMachineFunction(); 4806 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); 4807 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 4808 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), 4809 StackSlot, 4810 PseudoSourceValue::getFixedStack(SSFI), 0); 4811 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG); 4812 } 4813 4814 SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain, 4815 SDValue StackSlot, 4816 SelectionDAG &DAG) { 4817 // Build the FILD 4818 DebugLoc dl = Op.getDebugLoc(); 4819 SDVTList Tys; 4820 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType()); 4821 if (useSSE) 4822 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); 4823 else 4824 Tys = DAG.getVTList(Op.getValueType(), MVT::Other); 4825 SmallVector<SDValue, 8> Ops; 4826 Ops.push_back(Chain); 4827 Ops.push_back(StackSlot); 4828 Ops.push_back(DAG.getValueType(SrcVT)); 4829 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl, 4830 Tys, &Ops[0], Ops.size()); 4831 4832 if (useSSE) { 4833 Chain = Result.getValue(1); 4834 SDValue InFlag = Result.getValue(2); 4835 4836 // FIXME: Currently the FST is flagged to the FILD_FLAG. This 4837 // shouldn't be necessary except that RFP cannot be live across 4838 // multiple blocks. When stackifier is fixed, they can be uncoupled. 4839 MachineFunction &MF = DAG.getMachineFunction(); 4840 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); 4841 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 4842 Tys = DAG.getVTList(MVT::Other); 4843 SmallVector<SDValue, 8> Ops; 4844 Ops.push_back(Chain); 4845 Ops.push_back(Result); 4846 Ops.push_back(StackSlot); 4847 Ops.push_back(DAG.getValueType(Op.getValueType())); 4848 Ops.push_back(InFlag); 4849 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size()); 4850 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot, 4851 PseudoSourceValue::getFixedStack(SSFI), 0); 4852 } 4853 4854 return Result; 4855 } 4856 4857 // LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion. 4858 SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) { 4859 // This algorithm is not obvious. Here it is in C code, more or less: 4860 /* 4861 double uint64_to_double( uint32_t hi, uint32_t lo ) { 4862 static const __m128i exp = { 0x4330000045300000ULL, 0 }; 4863 static const __m128d bias = { 0x1.0p84, 0x1.0p52 }; 4864 4865 // Copy ints to xmm registers. 4866 __m128i xh = _mm_cvtsi32_si128( hi ); 4867 __m128i xl = _mm_cvtsi32_si128( lo ); 4868 4869 // Combine into low half of a single xmm register. 4870 __m128i x = _mm_unpacklo_epi32( xh, xl ); 4871 __m128d d; 4872 double sd; 4873 4874 // Merge in appropriate exponents to give the integer bits the right 4875 // magnitude. 4876 x = _mm_unpacklo_epi32( x, exp ); 4877 4878 // Subtract away the biases to deal with the IEEE-754 double precision 4879 // implicit 1. 4880 d = _mm_sub_pd( (__m128d) x, bias ); 4881 4882 // All conversions up to here are exact. The correctly rounded result is 4883 // calculated using the current rounding mode using the following 4884 // horizontal add. 4885 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) ); 4886 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this 4887 // store doesn't really need to be here (except 4888 // maybe to zero the other double) 4889 return sd; 4890 } 4891 */ 4892 4893 DebugLoc dl = Op.getDebugLoc(); 4894 LLVMContext *Context = DAG.getContext(); 4895 4896 // Build some magic constants. 4897 std::vector<Constant*> CV0; 4898 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000))); 4899 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000))); 4900 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0))); 4901 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0))); 4902 Constant *C0 = ConstantVector::get(CV0); 4903 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16); 4904 4905 std::vector<Constant*> CV1; 4906 CV1.push_back( 4907 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL)))); 4908 CV1.push_back( 4909 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL)))); 4910 Constant *C1 = ConstantVector::get(CV1); 4911 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16); 4912 4913 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, 4914 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4915 Op.getOperand(0), 4916 DAG.getIntPtrConstant(1))); 4917 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, 4918 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4919 Op.getOperand(0), 4920 DAG.getIntPtrConstant(0))); 4921 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2); 4922 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0, 4923 PseudoSourceValue::getConstantPool(), 0, 4924 false, 16); 4925 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0); 4926 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2); 4927 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1, 4928 PseudoSourceValue::getConstantPool(), 0, 4929 false, 16); 4930 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1); 4931 4932 // Add the halves; easiest way is to swap them into another reg first. 4933 int ShufMask[2] = { 1, -1 }; 4934 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub, 4935 DAG.getUNDEF(MVT::v2f64), ShufMask); 4936 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub); 4937 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add, 4938 DAG.getIntPtrConstant(0)); 4939 } 4940 4941 // LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion. 4942 SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) { 4943 DebugLoc dl = Op.getDebugLoc(); 4944 // FP constant to bias correct the final result. 4945 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), 4946 MVT::f64); 4947 4948 // Load the 32-bit value into an XMM register. 4949 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, 4950 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 4951 Op.getOperand(0), 4952 DAG.getIntPtrConstant(0))); 4953 4954 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 4955 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load), 4956 DAG.getIntPtrConstant(0)); 4957 4958 // Or the load with the bias. 4959 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, 4960 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, 4961 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 4962 MVT::v2f64, Load)), 4963 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, 4964 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 4965 MVT::v2f64, Bias))); 4966 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 4967 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or), 4968 DAG.getIntPtrConstant(0)); 4969 4970 // Subtract the bias. 4971 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias); 4972 4973 // Handle final rounding. 4974 MVT DestVT = Op.getValueType(); 4975 4976 if (DestVT.bitsLT(MVT::f64)) { 4977 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, 4978 DAG.getIntPtrConstant(0)); 4979 } else if (DestVT.bitsGT(MVT::f64)) { 4980 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); 4981 } 4982 4983 // Handle final rounding. 4984 return Sub; 4985 } 4986 4987 SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 4988 SDValue N0 = Op.getOperand(0); 4989 DebugLoc dl = Op.getDebugLoc(); 4990 4991 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't 4992 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform 4993 // the optimization here. 4994 if (DAG.SignBitIsZero(N0)) 4995 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0); 4996 4997 MVT SrcVT = N0.getValueType(); 4998 if (SrcVT == MVT::i64) { 4999 // We only handle SSE2 f64 target here; caller can expand the rest. 5000 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64) 5001 return SDValue(); 5002 5003 return LowerUINT_TO_FP_i64(Op, DAG); 5004 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) { 5005 return LowerUINT_TO_FP_i32(Op, DAG); 5006 } 5007 5008 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!"); 5009 5010 // Make a 64-bit buffer, and use it to build an FILD. 5011 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64); 5012 SDValue WordOff = DAG.getConstant(4, getPointerTy()); 5013 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl, 5014 getPointerTy(), StackSlot, WordOff); 5015 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), 5016 StackSlot, NULL, 0); 5017 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32), 5018 OffsetSlot, NULL, 0); 5019 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG); 5020 } 5021 5022 std::pair<SDValue,SDValue> X86TargetLowering:: 5023 FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) { 5024 DebugLoc dl = Op.getDebugLoc(); 5025 5026 MVT DstTy = Op.getValueType(); 5027 5028 if (!IsSigned) { 5029 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT"); 5030 DstTy = MVT::i64; 5031 } 5032 5033 assert(DstTy.getSimpleVT() <= MVT::i64 && 5034 DstTy.getSimpleVT() >= MVT::i16 && 5035 "Unknown FP_TO_SINT to lower!"); 5036 5037 // These are really Legal. 5038 if (DstTy == MVT::i32 && 5039 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) 5040 return std::make_pair(SDValue(), SDValue()); 5041 if (Subtarget->is64Bit() && 5042 DstTy == MVT::i64 && 5043 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) 5044 return std::make_pair(SDValue(), SDValue()); 5045 5046 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary 5047 // stack slot. 5048 MachineFunction &MF = DAG.getMachineFunction(); 5049 unsigned MemSize = DstTy.getSizeInBits()/8; 5050 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); 5051 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 5052 5053 unsigned Opc; 5054 switch (DstTy.getSimpleVT()) { 5055 default: llvm_unreachable("Invalid FP_TO_SINT to lower!"); 5056 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; 5057 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; 5058 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; 5059 } 5060 5061 SDValue Chain = DAG.getEntryNode(); 5062 SDValue Value = Op.getOperand(0); 5063 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) { 5064 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!"); 5065 Chain = DAG.getStore(Chain, dl, Value, StackSlot, 5066 PseudoSourceValue::getFixedStack(SSFI), 0); 5067 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other); 5068 SDValue Ops[] = { 5069 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType()) 5070 }; 5071 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3); 5072 Chain = Value.getValue(1); 5073 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); 5074 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 5075 } 5076 5077 // Build the FP_TO_INT*_IN_MEM 5078 SDValue Ops[] = { Chain, Value, StackSlot }; 5079 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3); 5080 5081 return std::make_pair(FIST, StackSlot); 5082 } 5083 5084 SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) { 5085 if (Op.getValueType().isVector()) { 5086 if (Op.getValueType() == MVT::v2i32 && 5087 Op.getOperand(0).getValueType() == MVT::v2f64) { 5088 return Op; 5089 } 5090 return SDValue(); 5091 } 5092 5093 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true); 5094 SDValue FIST = Vals.first, StackSlot = Vals.second; 5095 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal. 5096 if (FIST.getNode() == 0) return Op; 5097 5098 // Load the result. 5099 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(), 5100 FIST, StackSlot, NULL, 0); 5101 } 5102 5103 SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) { 5104 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false); 5105 SDValue FIST = Vals.first, StackSlot = Vals.second; 5106 assert(FIST.getNode() && "Unexpected failure"); 5107 5108 // Load the result. 5109 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(), 5110 FIST, StackSlot, NULL, 0); 5111 } 5112 5113 SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) { 5114 LLVMContext *Context = DAG.getContext(); 5115 DebugLoc dl = Op.getDebugLoc(); 5116 MVT VT = Op.getValueType(); 5117 MVT EltVT = VT; 5118 if (VT.isVector()) 5119 EltVT = VT.getVectorElementType(); 5120 std::vector<Constant*> CV; 5121 if (EltVT == MVT::f64) { 5122 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))); 5123 CV.push_back(C); 5124 CV.push_back(C); 5125 } else { 5126 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))); 5127 CV.push_back(C); 5128 CV.push_back(C); 5129 CV.push_back(C); 5130 CV.push_back(C); 5131 } 5132 Constant *C = ConstantVector::get(CV); 5133 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); 5134 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, 5135 PseudoSourceValue::getConstantPool(), 0, 5136 false, 16); 5137 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask); 5138 } 5139 5140 SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) { 5141 LLVMContext *Context = DAG.getContext(); 5142 DebugLoc dl = Op.getDebugLoc(); 5143 MVT VT = Op.getValueType(); 5144 MVT EltVT = VT; 5145 unsigned EltNum = 1; 5146 if (VT.isVector()) { 5147 EltVT = VT.getVectorElementType(); 5148 EltNum = VT.getVectorNumElements(); 5149 } 5150 std::vector<Constant*> CV; 5151 if (EltVT == MVT::f64) { 5152 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))); 5153 CV.push_back(C); 5154 CV.push_back(C); 5155 } else { 5156 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))); 5157 CV.push_back(C); 5158 CV.push_back(C); 5159 CV.push_back(C); 5160 CV.push_back(C); 5161 } 5162 Constant *C = ConstantVector::get(CV); 5163 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); 5164 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, 5165 PseudoSourceValue::getConstantPool(), 0, 5166 false, 16); 5167 if (VT.isVector()) { 5168 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, 5169 DAG.getNode(ISD::XOR, dl, MVT::v2i64, 5170 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, 5171 Op.getOperand(0)), 5172 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask))); 5173 } else { 5174 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask); 5175 } 5176 } 5177 5178 SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) { 5179 LLVMContext *Context = DAG.getContext(); 5180 SDValue Op0 = Op.getOperand(0); 5181 SDValue Op1 = Op.getOperand(1); 5182 DebugLoc dl = Op.getDebugLoc(); 5183 MVT VT = Op.getValueType(); 5184 MVT SrcVT = Op1.getValueType(); 5185 5186 // If second operand is smaller, extend it first. 5187 if (SrcVT.bitsLT(VT)) { 5188 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1); 5189 SrcVT = VT; 5190 } 5191 // And if it is bigger, shrink it first. 5192 if (SrcVT.bitsGT(VT)) { 5193 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1)); 5194 SrcVT = VT; 5195 } 5196 5197 // At this point the operands and the result should have the same 5198 // type, and that won't be f80 since that is not custom lowered. 5199 5200 // First get the sign bit of second operand. 5201 std::vector<Constant*> CV; 5202 if (SrcVT == MVT::f64) { 5203 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)))); 5204 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0)))); 5205 } else { 5206 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)))); 5207 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); 5208 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); 5209 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); 5210 } 5211 Constant *C = ConstantVector::get(CV); 5212 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); 5213 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx, 5214 PseudoSourceValue::getConstantPool(), 0, 5215 false, 16); 5216 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1); 5217 5218 // Shift sign bit right or left if the two operands have different types. 5219 if (SrcVT.bitsGT(VT)) { 5220 // Op0 is MVT::f32, Op1 is MVT::f64. 5221 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit); 5222 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit, 5223 DAG.getConstant(32, MVT::i32)); 5224 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit); 5225 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit, 5226 DAG.getIntPtrConstant(0)); 5227 } 5228 5229 // Clear first operand sign bit. 5230 CV.clear(); 5231 if (VT == MVT::f64) { 5232 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))))); 5233 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0)))); 5234 } else { 5235 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))))); 5236 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); 5237 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); 5238 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); 5239 } 5240 C = ConstantVector::get(CV); 5241 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); 5242 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, 5243 PseudoSourceValue::getConstantPool(), 0, 5244 false, 16); 5245 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2); 5246 5247 // Or the value with the sign bit. 5248 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit); 5249 } 5250 5251 /// Emit nodes that will be selected as "test Op0,Op0", or something 5252 /// equivalent. 5253 SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC, 5254 SelectionDAG &DAG) { 5255 DebugLoc dl = Op.getDebugLoc(); 5256 5257 // CF and OF aren't always set the way we want. Determine which 5258 // of these we need. 5259 bool NeedCF = false; 5260 bool NeedOF = false; 5261 switch (X86CC) { 5262 case X86::COND_A: case X86::COND_AE: 5263 case X86::COND_B: case X86::COND_BE: 5264 NeedCF = true; 5265 break; 5266 case X86::COND_G: case X86::COND_GE: 5267 case X86::COND_L: case X86::COND_LE: 5268 case X86::COND_O: case X86::COND_NO: 5269 NeedOF = true; 5270 break; 5271 default: break; 5272 } 5273 5274 // See if we can use the EFLAGS value from the operand instead of 5275 // doing a separate TEST. TEST always sets OF and CF to 0, so unless 5276 // we prove that the arithmetic won't overflow, we can't use OF or CF. 5277 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) { 5278 unsigned Opcode = 0; 5279 unsigned NumOperands = 0; 5280 switch (Op.getNode()->getOpcode()) { 5281 case ISD::ADD: 5282 // Due to an isel shortcoming, be conservative if this add is likely to 5283 // be selected as part of a load-modify-store instruction. When the root 5284 // node in a match is a store, isel doesn't know how to remap non-chain 5285 // non-flag uses of other nodes in the match, such as the ADD in this 5286 // case. This leads to the ADD being left around and reselected, with 5287 // the result being two adds in the output. 5288 for (SDNode::use_iterator UI = Op.getNode()->use_begin(), 5289 UE = Op.getNode()->use_end(); UI != UE; ++UI) 5290 if (UI->getOpcode() == ISD::STORE) 5291 goto default_case; 5292 if (ConstantSDNode *C = 5293 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) { 5294 // An add of one will be selected as an INC. 5295 if (C->getAPIntValue() == 1) { 5296 Opcode = X86ISD::INC; 5297 NumOperands = 1; 5298 break; 5299 } 5300 // An add of negative one (subtract of one) will be selected as a DEC. 5301 if (C->getAPIntValue().isAllOnesValue()) { 5302 Opcode = X86ISD::DEC; 5303 NumOperands = 1; 5304 break; 5305 } 5306 } 5307 // Otherwise use a regular EFLAGS-setting add. 5308 Opcode = X86ISD::ADD; 5309 NumOperands = 2; 5310 break; 5311 case ISD::SUB: 5312 // Due to the ISEL shortcoming noted above, be conservative if this sub is 5313 // likely to be selected as part of a load-modify-store instruction. 5314 for (SDNode::use_iterator UI = Op.getNode()->use_begin(), 5315 UE = Op.getNode()->use_end(); UI != UE; ++UI) 5316 if (UI->getOpcode() == ISD::STORE) 5317 goto default_case; 5318 // Otherwise use a regular EFLAGS-setting sub. 5319 Opcode = X86ISD::SUB; 5320 NumOperands = 2; 5321 break; 5322 case X86ISD::ADD: 5323 case X86ISD::SUB: 5324 case X86ISD::INC: 5325 case X86ISD::DEC: 5326 return SDValue(Op.getNode(), 1); 5327 default: 5328 default_case: 5329 break; 5330 } 5331 if (Opcode != 0) { 5332 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 5333 SmallVector<SDValue, 4> Ops; 5334 for (unsigned i = 0; i != NumOperands; ++i) 5335 Ops.push_back(Op.getOperand(i)); 5336 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands); 5337 DAG.ReplaceAllUsesWith(Op, New); 5338 return SDValue(New.getNode(), 1); 5339 } 5340 } 5341 5342 // Otherwise just emit a CMP with 0, which is the TEST pattern. 5343 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, 5344 DAG.getConstant(0, Op.getValueType())); 5345 } 5346 5347 /// Emit nodes that will be selected as "cmp Op0,Op1", or something 5348 /// equivalent. 5349 SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC, 5350 SelectionDAG &DAG) { 5351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1)) 5352 if (C->getAPIntValue() == 0) 5353 return EmitTest(Op0, X86CC, DAG); 5354 5355 DebugLoc dl = Op0.getDebugLoc(); 5356 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1); 5357 } 5358 5359 SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) { 5360 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); 5361 SDValue Op0 = Op.getOperand(0); 5362 SDValue Op1 = Op.getOperand(1); 5363 DebugLoc dl = Op.getDebugLoc(); 5364 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 5365 5366 // Lower (X & (1 << N)) == 0 to BT(X, N). 5367 // Lower ((X >>u N) & 1) != 0 to BT(X, N). 5368 // Lower ((X >>s N) & 1) != 0 to BT(X, N). 5369 if (Op0.getOpcode() == ISD::AND && 5370 Op0.hasOneUse() && 5371 Op1.getOpcode() == ISD::Constant && 5372 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 && 5373 (CC == ISD::SETEQ || CC == ISD::SETNE)) { 5374 SDValue LHS, RHS; 5375 if (Op0.getOperand(1).getOpcode() == ISD::SHL) { 5376 if (ConstantSDNode *Op010C = 5377 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0))) 5378 if (Op010C->getZExtValue() == 1) { 5379 LHS = Op0.getOperand(0); 5380 RHS = Op0.getOperand(1).getOperand(1); 5381 } 5382 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) { 5383 if (ConstantSDNode *Op000C = 5384 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0))) 5385 if (Op000C->getZExtValue() == 1) { 5386 LHS = Op0.getOperand(1); 5387 RHS = Op0.getOperand(0).getOperand(1); 5388 } 5389 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) { 5390 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1)); 5391 SDValue AndLHS = Op0.getOperand(0); 5392 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) { 5393 LHS = AndLHS.getOperand(0); 5394 RHS = AndLHS.getOperand(1); 5395 } 5396 } 5397 5398 if (LHS.getNode()) { 5399 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT 5400 // instruction. Since the shift amount is in-range-or-undefined, we know 5401 // that doing a bittest on the i16 value is ok. We extend to i32 because 5402 // the encoding for the i16 version is larger than the i32 version. 5403 if (LHS.getValueType() == MVT::i8) 5404 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS); 5405 5406 // If the operand types disagree, extend the shift amount to match. Since 5407 // BT ignores high bits (like shifts) we can use anyextend. 5408 if (LHS.getValueType() != RHS.getValueType()) 5409 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS); 5410 5411 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS); 5412 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B; 5413 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, 5414 DAG.getConstant(Cond, MVT::i8), BT); 5415 } 5416 } 5417 5418 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint(); 5419 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG); 5420 5421 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG); 5422 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, 5423 DAG.getConstant(X86CC, MVT::i8), Cond); 5424 } 5425 5426 SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 5427 SDValue Cond; 5428 SDValue Op0 = Op.getOperand(0); 5429 SDValue Op1 = Op.getOperand(1); 5430 SDValue CC = Op.getOperand(2); 5431 MVT VT = Op.getValueType(); 5432 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 5433 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint(); 5434 DebugLoc dl = Op.getDebugLoc(); 5435 5436 if (isFP) { 5437 unsigned SSECC = 8; 5438 MVT VT0 = Op0.getValueType(); 5439 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64); 5440 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD; 5441 bool Swap = false; 5442 5443 switch (SetCCOpcode) { 5444 default: break; 5445 case ISD::SETOEQ: 5446 case ISD::SETEQ: SSECC = 0; break; 5447 case ISD::SETOGT: 5448 case ISD::SETGT: Swap = true; // Fallthrough 5449 case ISD::SETLT: 5450 case ISD::SETOLT: SSECC = 1; break; 5451 case ISD::SETOGE: 5452 case ISD::SETGE: Swap = true; // Fallthrough 5453 case ISD::SETLE: 5454 case ISD::SETOLE: SSECC = 2; break; 5455 case ISD::SETUO: SSECC = 3; break; 5456 case ISD::SETUNE: 5457 case ISD::SETNE: SSECC = 4; break; 5458 case ISD::SETULE: Swap = true; 5459 case ISD::SETUGE: SSECC = 5; break; 5460 case ISD::SETULT: Swap = true; 5461 case ISD::SETUGT: SSECC = 6; break; 5462 case ISD::SETO: SSECC = 7; break; 5463 } 5464 if (Swap) 5465 std::swap(Op0, Op1); 5466 5467 // In the two special cases we can't handle, emit two comparisons. 5468 if (SSECC == 8) { 5469 if (SetCCOpcode == ISD::SETUEQ) { 5470 SDValue UNORD, EQ; 5471 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8)); 5472 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8)); 5473 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ); 5474 } 5475 else if (SetCCOpcode == ISD::SETONE) { 5476 SDValue ORD, NEQ; 5477 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8)); 5478 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8)); 5479 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ); 5480 } 5481 llvm_unreachable("Illegal FP comparison"); 5482 } 5483 // Handle all other FP comparisons here. 5484 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8)); 5485 } 5486 5487 // We are handling one of the integer comparisons here. Since SSE only has 5488 // GT and EQ comparisons for integer, swapping operands and multiple 5489 // operations may be required for some comparisons. 5490 unsigned Opc = 0, EQOpc = 0, GTOpc = 0; 5491 bool Swap = false, Invert = false, FlipSigns = false; 5492 5493 switch (VT.getSimpleVT()) { 5494 default: break; 5495 case MVT::v8i8: 5496 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break; 5497 case MVT::v4i16: 5498 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break; 5499 case MVT::v2i32: 5500 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break; 5501 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break; 5502 } 5503 5504 switch (SetCCOpcode) { 5505 default: break; 5506 case ISD::SETNE: Invert = true; 5507 case ISD::SETEQ: Opc = EQOpc; break; 5508 case ISD::SETLT: Swap = true; 5509 case ISD::SETGT: Opc = GTOpc; break; 5510 case ISD::SETGE: Swap = true; 5511 case ISD::SETLE: Opc = GTOpc; Invert = true; break; 5512 case ISD::SETULT: Swap = true; 5513 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break; 5514 case ISD::SETUGE: Swap = true; 5515 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break; 5516 } 5517 if (Swap) 5518 std::swap(Op0, Op1); 5519 5520 // Since SSE has no unsigned integer comparisons, we need to flip the sign 5521 // bits of the inputs before performing those operations. 5522 if (FlipSigns) { 5523 MVT EltVT = VT.getVectorElementType(); 5524 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), 5525 EltVT); 5526 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit); 5527 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0], 5528 SignBits.size()); 5529 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec); 5530 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec); 5531 } 5532 5533 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 5534 5535 // If the logical-not of the result is required, perform that now. 5536 if (Invert) 5537 Result = DAG.getNOT(dl, Result, VT); 5538 5539 return Result; 5540 } 5541 5542 // isX86LogicalCmp - Return true if opcode is a X86 logical comparison. 5543 static bool isX86LogicalCmp(SDValue Op) { 5544 unsigned Opc = Op.getNode()->getOpcode(); 5545 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) 5546 return true; 5547 if (Op.getResNo() == 1 && 5548 (Opc == X86ISD::ADD || 5549 Opc == X86ISD::SUB || 5550 Opc == X86ISD::SMUL || 5551 Opc == X86ISD::UMUL || 5552 Opc == X86ISD::INC || 5553 Opc == X86ISD::DEC)) 5554 return true; 5555 5556 return false; 5557 } 5558 5559 SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) { 5560 bool addTest = true; 5561 SDValue Cond = Op.getOperand(0); 5562 DebugLoc dl = Op.getDebugLoc(); 5563 SDValue CC; 5564 5565 if (Cond.getOpcode() == ISD::SETCC) 5566 Cond = LowerSETCC(Cond, DAG); 5567 5568 // If condition flag is set by a X86ISD::CMP, then use it as the condition 5569 // setting operand in place of the X86ISD::SETCC. 5570 if (Cond.getOpcode() == X86ISD::SETCC) { 5571 CC = Cond.getOperand(0); 5572 5573 SDValue Cmp = Cond.getOperand(1); 5574 unsigned Opc = Cmp.getOpcode(); 5575 MVT VT = Op.getValueType(); 5576 5577 bool IllegalFPCMov = false; 5578 if (VT.isFloatingPoint() && !VT.isVector() && 5579 !isScalarFPTypeInSSEReg(VT)) // FPStack? 5580 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue()); 5581 5582 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) || 5583 Opc == X86ISD::BT) { // FIXME 5584 Cond = Cmp; 5585 addTest = false; 5586 } 5587 } 5588 5589 if (addTest) { 5590 CC = DAG.getConstant(X86::COND_NE, MVT::i8); 5591 Cond = EmitTest(Cond, X86::COND_NE, DAG); 5592 } 5593 5594 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag); 5595 SmallVector<SDValue, 4> Ops; 5596 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if 5597 // condition is true. 5598 Ops.push_back(Op.getOperand(2)); 5599 Ops.push_back(Op.getOperand(1)); 5600 Ops.push_back(CC); 5601 Ops.push_back(Cond); 5602 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size()); 5603 } 5604 5605 // isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or 5606 // ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart 5607 // from the AND / OR. 5608 static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) { 5609 Opc = Op.getOpcode(); 5610 if (Opc != ISD::OR && Opc != ISD::AND) 5611 return false; 5612 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC && 5613 Op.getOperand(0).hasOneUse() && 5614 Op.getOperand(1).getOpcode() == X86ISD::SETCC && 5615 Op.getOperand(1).hasOneUse()); 5616 } 5617 5618 // isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and 5619 // 1 and that the SETCC node has a single use. 5620 static bool isXor1OfSetCC(SDValue Op) { 5621 if (Op.getOpcode() != ISD::XOR) 5622 return false; 5623 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 5624 if (N1C && N1C->getAPIntValue() == 1) { 5625 return Op.getOperand(0).getOpcode() == X86ISD::SETCC && 5626 Op.getOperand(0).hasOneUse(); 5627 } 5628 return false; 5629 } 5630 5631 SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) { 5632 bool addTest = true; 5633 SDValue Chain = Op.getOperand(0); 5634 SDValue Cond = Op.getOperand(1); 5635 SDValue Dest = Op.getOperand(2); 5636 DebugLoc dl = Op.getDebugLoc(); 5637 SDValue CC; 5638 5639 if (Cond.getOpcode() == ISD::SETCC) 5640 Cond = LowerSETCC(Cond, DAG); 5641 #if 0 5642 // FIXME: LowerXALUO doesn't handle these!! 5643 else if (Cond.getOpcode() == X86ISD::ADD || 5644 Cond.getOpcode() == X86ISD::SUB || 5645 Cond.getOpcode() == X86ISD::SMUL || 5646 Cond.getOpcode() == X86ISD::UMUL) 5647 Cond = LowerXALUO(Cond, DAG); 5648 #endif 5649 5650 // If condition flag is set by a X86ISD::CMP, then use it as the condition 5651 // setting operand in place of the X86ISD::SETCC. 5652 if (Cond.getOpcode() == X86ISD::SETCC) { 5653 CC = Cond.getOperand(0); 5654 5655 SDValue Cmp = Cond.getOperand(1); 5656 unsigned Opc = Cmp.getOpcode(); 5657 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp?? 5658 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) { 5659 Cond = Cmp; 5660 addTest = false; 5661 } else { 5662 switch (cast<ConstantSDNode>(CC)->getZExtValue()) { 5663 default: break; 5664 case X86::COND_O: 5665 case X86::COND_B: 5666 // These can only come from an arithmetic instruction with overflow, 5667 // e.g. SADDO, UADDO. 5668 Cond = Cond.getNode()->getOperand(1); 5669 addTest = false; 5670 break; 5671 } 5672 } 5673 } else { 5674 unsigned CondOpc; 5675 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) { 5676 SDValue Cmp = Cond.getOperand(0).getOperand(1); 5677 if (CondOpc == ISD::OR) { 5678 // Also, recognize the pattern generated by an FCMP_UNE. We can emit 5679 // two branches instead of an explicit OR instruction with a 5680 // separate test. 5681 if (Cmp == Cond.getOperand(1).getOperand(1) && 5682 isX86LogicalCmp(Cmp)) { 5683 CC = Cond.getOperand(0).getOperand(0); 5684 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), 5685 Chain, Dest, CC, Cmp); 5686 CC = Cond.getOperand(1).getOperand(0); 5687 Cond = Cmp; 5688 addTest = false; 5689 } 5690 } else { // ISD::AND 5691 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit 5692 // two branches instead of an explicit AND instruction with a 5693 // separate test. However, we only do this if this block doesn't 5694 // have a fall-through edge, because this requires an explicit 5695 // jmp when the condition is false. 5696 if (Cmp == Cond.getOperand(1).getOperand(1) && 5697 isX86LogicalCmp(Cmp) && 5698 Op.getNode()->hasOneUse()) { 5699 X86::CondCode CCode = 5700 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); 5701 CCode = X86::GetOppositeBranchCondition(CCode); 5702 CC = DAG.getConstant(CCode, MVT::i8); 5703 SDValue User = SDValue(*Op.getNode()->use_begin(), 0); 5704 // Look for an unconditional branch following this conditional branch. 5705 // We need this because we need to reverse the successors in order 5706 // to implement FCMP_OEQ. 5707 if (User.getOpcode() == ISD::BR) { 5708 SDValue FalseBB = User.getOperand(1); 5709 SDValue NewBR = 5710 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest); 5711 assert(NewBR == User); 5712 Dest = FalseBB; 5713 5714 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), 5715 Chain, Dest, CC, Cmp); 5716 X86::CondCode CCode = 5717 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0); 5718 CCode = X86::GetOppositeBranchCondition(CCode); 5719 CC = DAG.getConstant(CCode, MVT::i8); 5720 Cond = Cmp; 5721 addTest = false; 5722 } 5723 } 5724 } 5725 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) { 5726 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition. 5727 // It should be transformed during dag combiner except when the condition 5728 // is set by a arithmetics with overflow node. 5729 X86::CondCode CCode = 5730 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); 5731 CCode = X86::GetOppositeBranchCondition(CCode); 5732 CC = DAG.getConstant(CCode, MVT::i8); 5733 Cond = Cond.getOperand(0).getOperand(1); 5734 addTest = false; 5735 } 5736 } 5737 5738 if (addTest) { 5739 CC = DAG.getConstant(X86::COND_NE, MVT::i8); 5740 Cond = EmitTest(Cond, X86::COND_NE, DAG); 5741 } 5742 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), 5743 Chain, Dest, CC, Cond); 5744 } 5745 5746 5747 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. 5748 // Calls to _alloca is needed to probe the stack when allocating more than 4k 5749 // bytes in one go. Touching the stack at 4K increments is necessary to ensure 5750 // that the guard pages used by the OS virtual memory manager are allocated in 5751 // correct sequence. 5752 SDValue 5753 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, 5754 SelectionDAG &DAG) { 5755 assert(Subtarget->isTargetCygMing() && 5756 "This should be used only on Cygwin/Mingw targets"); 5757 DebugLoc dl = Op.getDebugLoc(); 5758 5759 // Get the inputs. 5760 SDValue Chain = Op.getOperand(0); 5761 SDValue Size = Op.getOperand(1); 5762 // FIXME: Ensure alignment here 5763 5764 SDValue Flag; 5765 5766 MVT IntPtr = getPointerTy(); 5767 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; 5768 5769 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true)); 5770 5771 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag); 5772 Flag = Chain.getValue(1); 5773 5774 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 5775 SDValue Ops[] = { Chain, 5776 DAG.getTargetExternalSymbol("_alloca", IntPtr), 5777 DAG.getRegister(X86::EAX, IntPtr), 5778 DAG.getRegister(X86StackPtr, SPTy), 5779 Flag }; 5780 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5); 5781 Flag = Chain.getValue(1); 5782 5783 Chain = DAG.getCALLSEQ_END(Chain, 5784 DAG.getIntPtrConstant(0, true), 5785 DAG.getIntPtrConstant(0, true), 5786 Flag); 5787 5788 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1); 5789 5790 SDValue Ops1[2] = { Chain.getValue(0), Chain }; 5791 return DAG.getMergeValues(Ops1, 2, dl); 5792 } 5793 5794 SDValue 5795 X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl, 5796 SDValue Chain, 5797 SDValue Dst, SDValue Src, 5798 SDValue Size, unsigned Align, 5799 const Value *DstSV, 5800 uint64_t DstSVOff) { 5801 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 5802 5803 // If not DWORD aligned or size is more than the threshold, call the library. 5804 // The libc version is likely to be faster for these cases. It can use the 5805 // address value and run time information about the CPU. 5806 if ((Align & 3) != 0 || 5807 !ConstantSize || 5808 ConstantSize->getZExtValue() > 5809 getSubtarget()->getMaxInlineSizeThreshold()) { 5810 SDValue InFlag(0, 0); 5811 5812 // Check to see if there is a specialized entry-point for memory zeroing. 5813 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src); 5814 5815 if (const char *bzeroEntry = V && 5816 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) { 5817 MVT IntPtr = getPointerTy(); 5818 const Type *IntPtrTy = TD->getIntPtrType(); 5819 TargetLowering::ArgListTy Args; 5820 TargetLowering::ArgListEntry Entry; 5821 Entry.Node = Dst; 5822 Entry.Ty = IntPtrTy; 5823 Args.push_back(Entry); 5824 Entry.Node = Size; 5825 Args.push_back(Entry); 5826 std::pair<SDValue,SDValue> CallResult = 5827 LowerCallTo(Chain, Type::VoidTy, false, false, false, false, 5828 0, CallingConv::C, false, 5829 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl); 5830 return CallResult.second; 5831 } 5832 5833 // Otherwise have the target-independent code call memset. 5834 return SDValue(); 5835 } 5836 5837 uint64_t SizeVal = ConstantSize->getZExtValue(); 5838 SDValue InFlag(0, 0); 5839 MVT AVT; 5840 SDValue Count; 5841 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src); 5842 unsigned BytesLeft = 0; 5843 bool TwoRepStos = false; 5844 if (ValC) { 5845 unsigned ValReg; 5846 uint64_t Val = ValC->getZExtValue() & 255; 5847 5848 // If the value is a constant, then we can potentially use larger sets. 5849 switch (Align & 3) { 5850 case 2: // WORD aligned 5851 AVT = MVT::i16; 5852 ValReg = X86::AX; 5853 Val = (Val << 8) | Val; 5854 break; 5855 case 0: // DWORD aligned 5856 AVT = MVT::i32; 5857 ValReg = X86::EAX; 5858 Val = (Val << 8) | Val; 5859 Val = (Val << 16) | Val; 5860 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned 5861 AVT = MVT::i64; 5862 ValReg = X86::RAX; 5863 Val = (Val << 32) | Val; 5864 } 5865 break; 5866 default: // Byte aligned 5867 AVT = MVT::i8; 5868 ValReg = X86::AL; 5869 Count = DAG.getIntPtrConstant(SizeVal); 5870 break; 5871 } 5872 5873 if (AVT.bitsGT(MVT::i8)) { 5874 unsigned UBytes = AVT.getSizeInBits() / 8; 5875 Count = DAG.getIntPtrConstant(SizeVal / UBytes); 5876 BytesLeft = SizeVal % UBytes; 5877 } 5878 5879 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT), 5880 InFlag); 5881 InFlag = Chain.getValue(1); 5882 } else { 5883 AVT = MVT::i8; 5884 Count = DAG.getIntPtrConstant(SizeVal); 5885 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag); 5886 InFlag = Chain.getValue(1); 5887 } 5888 5889 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : 5890 X86::ECX, 5891 Count, InFlag); 5892 InFlag = Chain.getValue(1); 5893 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : 5894 X86::EDI, 5895 Dst, InFlag); 5896 InFlag = Chain.getValue(1); 5897 5898 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 5899 SmallVector<SDValue, 8> Ops; 5900 Ops.push_back(Chain); 5901 Ops.push_back(DAG.getValueType(AVT)); 5902 Ops.push_back(InFlag); 5903 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size()); 5904 5905 if (TwoRepStos) { 5906 InFlag = Chain.getValue(1); 5907 Count = Size; 5908 MVT CVT = Count.getValueType(); 5909 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count, 5910 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); 5911 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX : 5912 X86::ECX, 5913 Left, InFlag); 5914 InFlag = Chain.getValue(1); 5915 Tys = DAG.getVTList(MVT::Other, MVT::Flag); 5916 Ops.clear(); 5917 Ops.push_back(Chain); 5918 Ops.push_back(DAG.getValueType(MVT::i8)); 5919 Ops.push_back(InFlag); 5920 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size()); 5921 } else if (BytesLeft) { 5922 // Handle the last 1 - 7 bytes. 5923 unsigned Offset = SizeVal - BytesLeft; 5924 MVT AddrVT = Dst.getValueType(); 5925 MVT SizeVT = Size.getValueType(); 5926 5927 Chain = DAG.getMemset(Chain, dl, 5928 DAG.getNode(ISD::ADD, dl, AddrVT, Dst, 5929 DAG.getConstant(Offset, AddrVT)), 5930 Src, 5931 DAG.getConstant(BytesLeft, SizeVT), 5932 Align, DstSV, DstSVOff + Offset); 5933 } 5934 5935 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain. 5936 return Chain; 5937 } 5938 5939 SDValue 5940 X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl, 5941 SDValue Chain, SDValue Dst, SDValue Src, 5942 SDValue Size, unsigned Align, 5943 bool AlwaysInline, 5944 const Value *DstSV, uint64_t DstSVOff, 5945 const Value *SrcSV, uint64_t SrcSVOff) { 5946 // This requires the copy size to be a constant, preferrably 5947 // within a subtarget-specific limit. 5948 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); 5949 if (!ConstantSize) 5950 return SDValue(); 5951 uint64_t SizeVal = ConstantSize->getZExtValue(); 5952 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold()) 5953 return SDValue(); 5954 5955 /// If not DWORD aligned, call the library. 5956 if ((Align & 3) != 0) 5957 return SDValue(); 5958 5959 // DWORD aligned 5960 MVT AVT = MVT::i32; 5961 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned 5962 AVT = MVT::i64; 5963 5964 unsigned UBytes = AVT.getSizeInBits() / 8; 5965 unsigned CountVal = SizeVal / UBytes; 5966 SDValue Count = DAG.getIntPtrConstant(CountVal); 5967 unsigned BytesLeft = SizeVal % UBytes; 5968 5969 SDValue InFlag(0, 0); 5970 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : 5971 X86::ECX, 5972 Count, InFlag); 5973 InFlag = Chain.getValue(1); 5974 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : 5975 X86::EDI, 5976 Dst, InFlag); 5977 InFlag = Chain.getValue(1); 5978 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI : 5979 X86::ESI, 5980 Src, InFlag); 5981 InFlag = Chain.getValue(1); 5982 5983 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 5984 SmallVector<SDValue, 8> Ops; 5985 Ops.push_back(Chain); 5986 Ops.push_back(DAG.getValueType(AVT)); 5987 Ops.push_back(InFlag); 5988 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size()); 5989 5990 SmallVector<SDValue, 4> Results; 5991 Results.push_back(RepMovs); 5992 if (BytesLeft) { 5993 // Handle the last 1 - 7 bytes. 5994 unsigned Offset = SizeVal - BytesLeft; 5995 MVT DstVT = Dst.getValueType(); 5996 MVT SrcVT = Src.getValueType(); 5997 MVT SizeVT = Size.getValueType(); 5998 Results.push_back(DAG.getMemcpy(Chain, dl, 5999 DAG.getNode(ISD::ADD, dl, DstVT, Dst, 6000 DAG.getConstant(Offset, DstVT)), 6001 DAG.getNode(ISD::ADD, dl, SrcVT, Src, 6002 DAG.getConstant(Offset, SrcVT)), 6003 DAG.getConstant(BytesLeft, SizeVT), 6004 Align, AlwaysInline, 6005 DstSV, DstSVOff + Offset, 6006 SrcSV, SrcSVOff + Offset)); 6007 } 6008 6009 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 6010 &Results[0], Results.size()); 6011 } 6012 6013 SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) { 6014 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 6015 DebugLoc dl = Op.getDebugLoc(); 6016 6017 if (!Subtarget->is64Bit()) { 6018 // vastart just stores the address of the VarArgsFrameIndex slot into the 6019 // memory location argument. 6020 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); 6021 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0); 6022 } 6023 6024 // __va_list_tag: 6025 // gp_offset (0 - 6 * 8) 6026 // fp_offset (48 - 48 + 8 * 16) 6027 // overflow_arg_area (point to parameters coming in memory). 6028 // reg_save_area 6029 SmallVector<SDValue, 8> MemOps; 6030 SDValue FIN = Op.getOperand(1); 6031 // Store gp_offset 6032 SDValue Store = DAG.getStore(Op.getOperand(0), dl, 6033 DAG.getConstant(VarArgsGPOffset, MVT::i32), 6034 FIN, SV, 0); 6035 MemOps.push_back(Store); 6036 6037 // Store fp_offset 6038 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), 6039 FIN, DAG.getIntPtrConstant(4)); 6040 Store = DAG.getStore(Op.getOperand(0), dl, 6041 DAG.getConstant(VarArgsFPOffset, MVT::i32), 6042 FIN, SV, 0); 6043 MemOps.push_back(Store); 6044 6045 // Store ptr to overflow_arg_area 6046 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), 6047 FIN, DAG.getIntPtrConstant(4)); 6048 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); 6049 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0); 6050 MemOps.push_back(Store); 6051 6052 // Store ptr to reg_save_area. 6053 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), 6054 FIN, DAG.getIntPtrConstant(8)); 6055 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); 6056 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0); 6057 MemOps.push_back(Store); 6058 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 6059 &MemOps[0], MemOps.size()); 6060 } 6061 6062 SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) { 6063 // X86-64 va_list is a struct { i32, i32, i8*, i8* }. 6064 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!"); 6065 SDValue Chain = Op.getOperand(0); 6066 SDValue SrcPtr = Op.getOperand(1); 6067 SDValue SrcSV = Op.getOperand(2); 6068 6069 llvm_report_error("VAArgInst is not yet implemented for x86-64!"); 6070 return SDValue(); 6071 } 6072 6073 SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) { 6074 // X86-64 va_list is a struct { i32, i32, i8*, i8* }. 6075 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!"); 6076 SDValue Chain = Op.getOperand(0); 6077 SDValue DstPtr = Op.getOperand(1); 6078 SDValue SrcPtr = Op.getOperand(2); 6079 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); 6080 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 6081 DebugLoc dl = Op.getDebugLoc(); 6082 6083 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr, 6084 DAG.getIntPtrConstant(24), 8, false, 6085 DstSV, 0, SrcSV, 0); 6086 } 6087 6088 SDValue 6089 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) { 6090 DebugLoc dl = Op.getDebugLoc(); 6091 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6092 switch (IntNo) { 6093 default: return SDValue(); // Don't custom lower most intrinsics. 6094 // Comparison intrinsics. 6095 case Intrinsic::x86_sse_comieq_ss: 6096 case Intrinsic::x86_sse_comilt_ss: 6097 case Intrinsic::x86_sse_comile_ss: 6098 case Intrinsic::x86_sse_comigt_ss: 6099 case Intrinsic::x86_sse_comige_ss: 6100 case Intrinsic::x86_sse_comineq_ss: 6101 case Intrinsic::x86_sse_ucomieq_ss: 6102 case Intrinsic::x86_sse_ucomilt_ss: 6103 case Intrinsic::x86_sse_ucomile_ss: 6104 case Intrinsic::x86_sse_ucomigt_ss: 6105 case Intrinsic::x86_sse_ucomige_ss: 6106 case Intrinsic::x86_sse_ucomineq_ss: 6107 case Intrinsic::x86_sse2_comieq_sd: 6108 case Intrinsic::x86_sse2_comilt_sd: 6109 case Intrinsic::x86_sse2_comile_sd: 6110 case Intrinsic::x86_sse2_comigt_sd: 6111 case Intrinsic::x86_sse2_comige_sd: 6112 case Intrinsic::x86_sse2_comineq_sd: 6113 case Intrinsic::x86_sse2_ucomieq_sd: 6114 case Intrinsic::x86_sse2_ucomilt_sd: 6115 case Intrinsic::x86_sse2_ucomile_sd: 6116 case Intrinsic::x86_sse2_ucomigt_sd: 6117 case Intrinsic::x86_sse2_ucomige_sd: 6118 case Intrinsic::x86_sse2_ucomineq_sd: { 6119 unsigned Opc = 0; 6120 ISD::CondCode CC = ISD::SETCC_INVALID; 6121 switch (IntNo) { 6122 default: break; 6123 case Intrinsic::x86_sse_comieq_ss: 6124 case Intrinsic::x86_sse2_comieq_sd: 6125 Opc = X86ISD::COMI; 6126 CC = ISD::SETEQ; 6127 break; 6128 case Intrinsic::x86_sse_comilt_ss: 6129 case Intrinsic::x86_sse2_comilt_sd: 6130 Opc = X86ISD::COMI; 6131 CC = ISD::SETLT; 6132 break; 6133 case Intrinsic::x86_sse_comile_ss: 6134 case Intrinsic::x86_sse2_comile_sd: 6135 Opc = X86ISD::COMI; 6136 CC = ISD::SETLE; 6137 break; 6138 case Intrinsic::x86_sse_comigt_ss: 6139 case Intrinsic::x86_sse2_comigt_sd: 6140 Opc = X86ISD::COMI; 6141 CC = ISD::SETGT; 6142 break; 6143 case Intrinsic::x86_sse_comige_ss: 6144 case Intrinsic::x86_sse2_comige_sd: 6145 Opc = X86ISD::COMI; 6146 CC = ISD::SETGE; 6147 break; 6148 case Intrinsic::x86_sse_comineq_ss: 6149 case Intrinsic::x86_sse2_comineq_sd: 6150 Opc = X86ISD::COMI; 6151 CC = ISD::SETNE; 6152 break; 6153 case Intrinsic::x86_sse_ucomieq_ss: 6154 case Intrinsic::x86_sse2_ucomieq_sd: 6155 Opc = X86ISD::UCOMI; 6156 CC = ISD::SETEQ; 6157 break; 6158 case Intrinsic::x86_sse_ucomilt_ss: 6159 case Intrinsic::x86_sse2_ucomilt_sd: 6160 Opc = X86ISD::UCOMI; 6161 CC = ISD::SETLT; 6162 break; 6163 case Intrinsic::x86_sse_ucomile_ss: 6164 case Intrinsic::x86_sse2_ucomile_sd: 6165 Opc = X86ISD::UCOMI; 6166 CC = ISD::SETLE; 6167 break; 6168 case Intrinsic::x86_sse_ucomigt_ss: 6169 case Intrinsic::x86_sse2_ucomigt_sd: 6170 Opc = X86ISD::UCOMI; 6171 CC = ISD::SETGT; 6172 break; 6173 case Intrinsic::x86_sse_ucomige_ss: 6174 case Intrinsic::x86_sse2_ucomige_sd: 6175 Opc = X86ISD::UCOMI; 6176 CC = ISD::SETGE; 6177 break; 6178 case Intrinsic::x86_sse_ucomineq_ss: 6179 case Intrinsic::x86_sse2_ucomineq_sd: 6180 Opc = X86ISD::UCOMI; 6181 CC = ISD::SETNE; 6182 break; 6183 } 6184 6185 SDValue LHS = Op.getOperand(1); 6186 SDValue RHS = Op.getOperand(2); 6187 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG); 6188 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS); 6189 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, 6190 DAG.getConstant(X86CC, MVT::i8), Cond); 6191 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); 6192 } 6193 // ptest intrinsics. The intrinsic these come from are designed to return 6194 // an integer value, not just an instruction so lower it to the ptest 6195 // pattern and a setcc for the result. 6196 case Intrinsic::x86_sse41_ptestz: 6197 case Intrinsic::x86_sse41_ptestc: 6198 case Intrinsic::x86_sse41_ptestnzc:{ 6199 unsigned X86CC = 0; 6200 switch (IntNo) { 6201 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering."); 6202 case Intrinsic::x86_sse41_ptestz: 6203 // ZF = 1 6204 X86CC = X86::COND_E; 6205 break; 6206 case Intrinsic::x86_sse41_ptestc: 6207 // CF = 1 6208 X86CC = X86::COND_B; 6209 break; 6210 case Intrinsic::x86_sse41_ptestnzc: 6211 // ZF and CF = 0 6212 X86CC = X86::COND_A; 6213 break; 6214 } 6215 6216 SDValue LHS = Op.getOperand(1); 6217 SDValue RHS = Op.getOperand(2); 6218 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS); 6219 SDValue CC = DAG.getConstant(X86CC, MVT::i8); 6220 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test); 6221 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); 6222 } 6223 6224 // Fix vector shift instructions where the last operand is a non-immediate 6225 // i32 value. 6226 case Intrinsic::x86_sse2_pslli_w: 6227 case Intrinsic::x86_sse2_pslli_d: 6228 case Intrinsic::x86_sse2_pslli_q: 6229 case Intrinsic::x86_sse2_psrli_w: 6230 case Intrinsic::x86_sse2_psrli_d: 6231 case Intrinsic::x86_sse2_psrli_q: 6232 case Intrinsic::x86_sse2_psrai_w: 6233 case Intrinsic::x86_sse2_psrai_d: 6234 case Intrinsic::x86_mmx_pslli_w: 6235 case Intrinsic::x86_mmx_pslli_d: 6236 case Intrinsic::x86_mmx_pslli_q: 6237 case Intrinsic::x86_mmx_psrli_w: 6238 case Intrinsic::x86_mmx_psrli_d: 6239 case Intrinsic::x86_mmx_psrli_q: 6240 case Intrinsic::x86_mmx_psrai_w: 6241 case Intrinsic::x86_mmx_psrai_d: { 6242 SDValue ShAmt = Op.getOperand(2); 6243 if (isa<ConstantSDNode>(ShAmt)) 6244 return SDValue(); 6245 6246 unsigned NewIntNo = 0; 6247 MVT ShAmtVT = MVT::v4i32; 6248 switch (IntNo) { 6249 case Intrinsic::x86_sse2_pslli_w: 6250 NewIntNo = Intrinsic::x86_sse2_psll_w; 6251 break; 6252 case Intrinsic::x86_sse2_pslli_d: 6253 NewIntNo = Intrinsic::x86_sse2_psll_d; 6254 break; 6255 case Intrinsic::x86_sse2_pslli_q: 6256 NewIntNo = Intrinsic::x86_sse2_psll_q; 6257 break; 6258 case Intrinsic::x86_sse2_psrli_w: 6259 NewIntNo = Intrinsic::x86_sse2_psrl_w; 6260 break; 6261 case Intrinsic::x86_sse2_psrli_d: 6262 NewIntNo = Intrinsic::x86_sse2_psrl_d; 6263 break; 6264 case Intrinsic::x86_sse2_psrli_q: 6265 NewIntNo = Intrinsic::x86_sse2_psrl_q; 6266 break; 6267 case Intrinsic::x86_sse2_psrai_w: 6268 NewIntNo = Intrinsic::x86_sse2_psra_w; 6269 break; 6270 case Intrinsic::x86_sse2_psrai_d: 6271 NewIntNo = Intrinsic::x86_sse2_psra_d; 6272 break; 6273 default: { 6274 ShAmtVT = MVT::v2i32; 6275 switch (IntNo) { 6276 case Intrinsic::x86_mmx_pslli_w: 6277 NewIntNo = Intrinsic::x86_mmx_psll_w; 6278 break; 6279 case Intrinsic::x86_mmx_pslli_d: 6280 NewIntNo = Intrinsic::x86_mmx_psll_d; 6281 break; 6282 case Intrinsic::x86_mmx_pslli_q: 6283 NewIntNo = Intrinsic::x86_mmx_psll_q; 6284 break; 6285 case Intrinsic::x86_mmx_psrli_w: 6286 NewIntNo = Intrinsic::x86_mmx_psrl_w; 6287 break; 6288 case Intrinsic::x86_mmx_psrli_d: 6289 NewIntNo = Intrinsic::x86_mmx_psrl_d; 6290 break; 6291 case Intrinsic::x86_mmx_psrli_q: 6292 NewIntNo = Intrinsic::x86_mmx_psrl_q; 6293 break; 6294 case Intrinsic::x86_mmx_psrai_w: 6295 NewIntNo = Intrinsic::x86_mmx_psra_w; 6296 break; 6297 case Intrinsic::x86_mmx_psrai_d: 6298 NewIntNo = Intrinsic::x86_mmx_psra_d; 6299 break; 6300 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. 6301 } 6302 break; 6303 } 6304 } 6305 MVT VT = Op.getValueType(); 6306 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, 6307 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt)); 6308 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6309 DAG.getConstant(NewIntNo, MVT::i32), 6310 Op.getOperand(1), ShAmt); 6311 } 6312 } 6313 } 6314 6315 SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) { 6316 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6317 DebugLoc dl = Op.getDebugLoc(); 6318 6319 if (Depth > 0) { 6320 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 6321 SDValue Offset = 6322 DAG.getConstant(TD->getPointerSize(), 6323 Subtarget->is64Bit() ? MVT::i64 : MVT::i32); 6324 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), 6325 DAG.getNode(ISD::ADD, dl, getPointerTy(), 6326 FrameAddr, Offset), 6327 NULL, 0); 6328 } 6329 6330 // Just load the return address. 6331 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); 6332 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), 6333 RetAddrFI, NULL, 0); 6334 } 6335 6336 SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) { 6337 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 6338 MFI->setFrameAddressIsTaken(true); 6339 MVT VT = Op.getValueType(); 6340 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful 6341 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6342 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP; 6343 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 6344 while (Depth--) 6345 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0); 6346 return FrameAddr; 6347 } 6348 6349 SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op, 6350 SelectionDAG &DAG) { 6351 return DAG.getIntPtrConstant(2*TD->getPointerSize()); 6352 } 6353 6354 SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) 6355 { 6356 MachineFunction &MF = DAG.getMachineFunction(); 6357 SDValue Chain = Op.getOperand(0); 6358 SDValue Offset = Op.getOperand(1); 6359 SDValue Handler = Op.getOperand(2); 6360 DebugLoc dl = Op.getDebugLoc(); 6361 6362 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP, 6363 getPointerTy()); 6364 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX); 6365 6366 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame, 6367 DAG.getIntPtrConstant(-TD->getPointerSize())); 6368 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset); 6369 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0); 6370 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr); 6371 MF.getRegInfo().addLiveOut(StoreAddrReg); 6372 6373 return DAG.getNode(X86ISD::EH_RETURN, dl, 6374 MVT::Other, 6375 Chain, DAG.getRegister(StoreAddrReg, getPointerTy())); 6376 } 6377 6378 SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op, 6379 SelectionDAG &DAG) { 6380 SDValue Root = Op.getOperand(0); 6381 SDValue Trmp = Op.getOperand(1); // trampoline 6382 SDValue FPtr = Op.getOperand(2); // nested function 6383 SDValue Nest = Op.getOperand(3); // 'nest' parameter value 6384 DebugLoc dl = Op.getDebugLoc(); 6385 6386 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 6387 6388 const X86InstrInfo *TII = 6389 ((X86TargetMachine&)getTargetMachine()).getInstrInfo(); 6390 6391 if (Subtarget->is64Bit()) { 6392 SDValue OutChains[6]; 6393 6394 // Large code-model. 6395 6396 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r); 6397 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri); 6398 6399 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10); 6400 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11); 6401 6402 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix 6403 6404 // Load the pointer to the nested function into R11. 6405 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11 6406 SDValue Addr = Trmp; 6407 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), 6408 Addr, TrmpAddr, 0); 6409 6410 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, 6411 DAG.getConstant(2, MVT::i64)); 6412 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2); 6413 6414 // Load the 'nest' parameter value into R10. 6415 // R10 is specified in X86CallingConv.td 6416 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10 6417 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, 6418 DAG.getConstant(10, MVT::i64)); 6419 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), 6420 Addr, TrmpAddr, 10); 6421 6422 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, 6423 DAG.getConstant(12, MVT::i64)); 6424 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2); 6425 6426 // Jump to the nested function. 6427 OpCode = (JMP64r << 8) | REX_WB; // jmpq *... 6428 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, 6429 DAG.getConstant(20, MVT::i64)); 6430 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), 6431 Addr, TrmpAddr, 20); 6432 6433 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11 6434 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, 6435 DAG.getConstant(22, MVT::i64)); 6436 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr, 6437 TrmpAddr, 22); 6438 6439 SDValue Ops[] = 6440 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) }; 6441 return DAG.getMergeValues(Ops, 2, dl); 6442 } else { 6443 const Function *Func = 6444 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); 6445 unsigned CC = Func->getCallingConv(); 6446 unsigned NestReg; 6447 6448 switch (CC) { 6449 default: 6450 llvm_unreachable("Unsupported calling convention"); 6451 case CallingConv::C: 6452 case CallingConv::X86_StdCall: { 6453 // Pass 'nest' parameter in ECX. 6454 // Must be kept in sync with X86CallingConv.td 6455 NestReg = X86::ECX; 6456 6457 // Check that ECX wasn't needed by an 'inreg' parameter. 6458 const FunctionType *FTy = Func->getFunctionType(); 6459 const AttrListPtr &Attrs = Func->getAttributes(); 6460 6461 if (!Attrs.isEmpty() && !Func->isVarArg()) { 6462 unsigned InRegCount = 0; 6463 unsigned Idx = 1; 6464 6465 for (FunctionType::param_iterator I = FTy->param_begin(), 6466 E = FTy->param_end(); I != E; ++I, ++Idx) 6467 if (Attrs.paramHasAttr(Idx, Attribute::InReg)) 6468 // FIXME: should only count parameters that are lowered to integers. 6469 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32; 6470 6471 if (InRegCount > 2) { 6472 llvm_report_error("Nest register in use - reduce number of inreg parameters!"); 6473 } 6474 } 6475 break; 6476 } 6477 case CallingConv::X86_FastCall: 6478 case CallingConv::Fast: 6479 // Pass 'nest' parameter in EAX. 6480 // Must be kept in sync with X86CallingConv.td 6481 NestReg = X86::EAX; 6482 break; 6483 } 6484 6485 SDValue OutChains[4]; 6486 SDValue Addr, Disp; 6487 6488 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, 6489 DAG.getConstant(10, MVT::i32)); 6490 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr); 6491 6492 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri); 6493 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg); 6494 OutChains[0] = DAG.getStore(Root, dl, 6495 DAG.getConstant(MOV32ri|N86Reg, MVT::i8), 6496 Trmp, TrmpAddr, 0); 6497 6498 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, 6499 DAG.getConstant(1, MVT::i32)); 6500 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1); 6501 6502 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP); 6503 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, 6504 DAG.getConstant(5, MVT::i32)); 6505 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr, 6506 TrmpAddr, 5, false, 1); 6507 6508 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, 6509 DAG.getConstant(6, MVT::i32)); 6510 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1); 6511 6512 SDValue Ops[] = 6513 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) }; 6514 return DAG.getMergeValues(Ops, 2, dl); 6515 } 6516 } 6517 6518 SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) { 6519 /* 6520 The rounding mode is in bits 11:10 of FPSR, and has the following 6521 settings: 6522 00 Round to nearest 6523 01 Round to -inf 6524 10 Round to +inf 6525 11 Round to 0 6526 6527 FLT_ROUNDS, on the other hand, expects the following: 6528 -1 Undefined 6529 0 Round to 0 6530 1 Round to nearest 6531 2 Round to +inf 6532 3 Round to -inf 6533 6534 To perform the conversion, we do: 6535 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3) 6536 */ 6537 6538 MachineFunction &MF = DAG.getMachineFunction(); 6539 const TargetMachine &TM = MF.getTarget(); 6540 const TargetFrameInfo &TFI = *TM.getFrameInfo(); 6541 unsigned StackAlignment = TFI.getStackAlignment(); 6542 MVT VT = Op.getValueType(); 6543 DebugLoc dl = Op.getDebugLoc(); 6544 6545 // Save FP Control Word to stack slot 6546 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment); 6547 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 6548 6549 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other, 6550 DAG.getEntryNode(), StackSlot); 6551 6552 // Load FP Control Word from stack slot 6553 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0); 6554 6555 // Transform as necessary 6556 SDValue CWD1 = 6557 DAG.getNode(ISD::SRL, dl, MVT::i16, 6558 DAG.getNode(ISD::AND, dl, MVT::i16, 6559 CWD, DAG.getConstant(0x800, MVT::i16)), 6560 DAG.getConstant(11, MVT::i8)); 6561 SDValue CWD2 = 6562 DAG.getNode(ISD::SRL, dl, MVT::i16, 6563 DAG.getNode(ISD::AND, dl, MVT::i16, 6564 CWD, DAG.getConstant(0x400, MVT::i16)), 6565 DAG.getConstant(9, MVT::i8)); 6566 6567 SDValue RetVal = 6568 DAG.getNode(ISD::AND, dl, MVT::i16, 6569 DAG.getNode(ISD::ADD, dl, MVT::i16, 6570 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2), 6571 DAG.getConstant(1, MVT::i16)), 6572 DAG.getConstant(3, MVT::i16)); 6573 6574 6575 return DAG.getNode((VT.getSizeInBits() < 16 ? 6576 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); 6577 } 6578 6579 SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) { 6580 MVT VT = Op.getValueType(); 6581 MVT OpVT = VT; 6582 unsigned NumBits = VT.getSizeInBits(); 6583 DebugLoc dl = Op.getDebugLoc(); 6584 6585 Op = Op.getOperand(0); 6586 if (VT == MVT::i8) { 6587 // Zero extend to i32 since there is not an i8 bsr. 6588 OpVT = MVT::i32; 6589 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); 6590 } 6591 6592 // Issue a bsr (scan bits in reverse) which also sets EFLAGS. 6593 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); 6594 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op); 6595 6596 // If src is zero (i.e. bsr sets ZF), returns NumBits. 6597 SmallVector<SDValue, 4> Ops; 6598 Ops.push_back(Op); 6599 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT)); 6600 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); 6601 Ops.push_back(Op.getValue(1)); 6602 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4); 6603 6604 // Finally xor with NumBits-1. 6605 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT)); 6606 6607 if (VT == MVT::i8) 6608 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); 6609 return Op; 6610 } 6611 6612 SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) { 6613 MVT VT = Op.getValueType(); 6614 MVT OpVT = VT; 6615 unsigned NumBits = VT.getSizeInBits(); 6616 DebugLoc dl = Op.getDebugLoc(); 6617 6618 Op = Op.getOperand(0); 6619 if (VT == MVT::i8) { 6620 OpVT = MVT::i32; 6621 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); 6622 } 6623 6624 // Issue a bsf (scan bits forward) which also sets EFLAGS. 6625 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); 6626 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op); 6627 6628 // If src is zero (i.e. bsf sets ZF), returns NumBits. 6629 SmallVector<SDValue, 4> Ops; 6630 Ops.push_back(Op); 6631 Ops.push_back(DAG.getConstant(NumBits, OpVT)); 6632 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); 6633 Ops.push_back(Op.getValue(1)); 6634 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4); 6635 6636 if (VT == MVT::i8) 6637 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); 6638 return Op; 6639 } 6640 6641 SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) { 6642 MVT VT = Op.getValueType(); 6643 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply"); 6644 DebugLoc dl = Op.getDebugLoc(); 6645 6646 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32); 6647 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32); 6648 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b ); 6649 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi ); 6650 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b ); 6651 // 6652 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 ); 6653 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 ); 6654 // return AloBlo + AloBhi + AhiBlo; 6655 6656 SDValue A = Op.getOperand(0); 6657 SDValue B = Op.getOperand(1); 6658 6659 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6660 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), 6661 A, DAG.getConstant(32, MVT::i32)); 6662 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6663 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), 6664 B, DAG.getConstant(32, MVT::i32)); 6665 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6666 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), 6667 A, B); 6668 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6669 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), 6670 A, Bhi); 6671 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6672 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), 6673 Ahi, B); 6674 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6675 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), 6676 AloBhi, DAG.getConstant(32, MVT::i32)); 6677 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 6678 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), 6679 AhiBlo, DAG.getConstant(32, MVT::i32)); 6680 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi); 6681 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo); 6682 return Res; 6683 } 6684 6685 6686 SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) { 6687 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus 6688 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering 6689 // looks for this combo and may remove the "setcc" instruction if the "setcc" 6690 // has only one use. 6691 SDNode *N = Op.getNode(); 6692 SDValue LHS = N->getOperand(0); 6693 SDValue RHS = N->getOperand(1); 6694 unsigned BaseOp = 0; 6695 unsigned Cond = 0; 6696 DebugLoc dl = Op.getDebugLoc(); 6697 6698 switch (Op.getOpcode()) { 6699 default: llvm_unreachable("Unknown ovf instruction!"); 6700 case ISD::SADDO: 6701 // A subtract of one will be selected as a INC. Note that INC doesn't 6702 // set CF, so we can't do this for UADDO. 6703 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 6704 if (C->getAPIntValue() == 1) { 6705 BaseOp = X86ISD::INC; 6706 Cond = X86::COND_O; 6707 break; 6708 } 6709 BaseOp = X86ISD::ADD; 6710 Cond = X86::COND_O; 6711 break; 6712 case ISD::UADDO: 6713 BaseOp = X86ISD::ADD; 6714 Cond = X86::COND_B; 6715 break; 6716 case ISD::SSUBO: 6717 // A subtract of one will be selected as a DEC. Note that DEC doesn't 6718 // set CF, so we can't do this for USUBO. 6719 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 6720 if (C->getAPIntValue() == 1) { 6721 BaseOp = X86ISD::DEC; 6722 Cond = X86::COND_O; 6723 break; 6724 } 6725 BaseOp = X86ISD::SUB; 6726 Cond = X86::COND_O; 6727 break; 6728 case ISD::USUBO: 6729 BaseOp = X86ISD::SUB; 6730 Cond = X86::COND_B; 6731 break; 6732 case ISD::SMULO: 6733 BaseOp = X86ISD::SMUL; 6734 Cond = X86::COND_O; 6735 break; 6736 case ISD::UMULO: 6737 BaseOp = X86ISD::UMUL; 6738 Cond = X86::COND_B; 6739 break; 6740 } 6741 6742 // Also sets EFLAGS. 6743 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32); 6744 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS); 6745 6746 SDValue SetCC = 6747 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1), 6748 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1)); 6749 6750 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC); 6751 return Sum; 6752 } 6753 6754 SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) { 6755 MVT T = Op.getValueType(); 6756 DebugLoc dl = Op.getDebugLoc(); 6757 unsigned Reg = 0; 6758 unsigned size = 0; 6759 switch(T.getSimpleVT()) { 6760 default: 6761 assert(false && "Invalid value type!"); 6762 case MVT::i8: Reg = X86::AL; size = 1; break; 6763 case MVT::i16: Reg = X86::AX; size = 2; break; 6764 case MVT::i32: Reg = X86::EAX; size = 4; break; 6765 case MVT::i64: 6766 assert(Subtarget->is64Bit() && "Node not type legal!"); 6767 Reg = X86::RAX; size = 8; 6768 break; 6769 } 6770 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg, 6771 Op.getOperand(2), SDValue()); 6772 SDValue Ops[] = { cpIn.getValue(0), 6773 Op.getOperand(1), 6774 Op.getOperand(3), 6775 DAG.getTargetConstant(size, MVT::i8), 6776 cpIn.getValue(1) }; 6777 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 6778 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5); 6779 SDValue cpOut = 6780 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1)); 6781 return cpOut; 6782 } 6783 6784 SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op, 6785 SelectionDAG &DAG) { 6786 assert(Subtarget->is64Bit() && "Result not type legalized?"); 6787 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 6788 SDValue TheChain = Op.getOperand(0); 6789 DebugLoc dl = Op.getDebugLoc(); 6790 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); 6791 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1)); 6792 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64, 6793 rax.getValue(2)); 6794 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx, 6795 DAG.getConstant(32, MVT::i8)); 6796 SDValue Ops[] = { 6797 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp), 6798 rdx.getValue(1) 6799 }; 6800 return DAG.getMergeValues(Ops, 2, dl); 6801 } 6802 6803 SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) { 6804 SDNode *Node = Op.getNode(); 6805 DebugLoc dl = Node->getDebugLoc(); 6806 MVT T = Node->getValueType(0); 6807 SDValue negOp = DAG.getNode(ISD::SUB, dl, T, 6808 DAG.getConstant(0, T), Node->getOperand(2)); 6809 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl, 6810 cast<AtomicSDNode>(Node)->getMemoryVT(), 6811 Node->getOperand(0), 6812 Node->getOperand(1), negOp, 6813 cast<AtomicSDNode>(Node)->getSrcValue(), 6814 cast<AtomicSDNode>(Node)->getAlignment()); 6815 } 6816 6817 /// LowerOperation - Provide custom lowering hooks for some operations. 6818 /// 6819 SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { 6820 switch (Op.getOpcode()) { 6821 default: llvm_unreachable("Should not custom lower this!"); 6822 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG); 6823 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG); 6824 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); 6825 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 6826 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 6827 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 6828 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); 6829 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 6830 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 6831 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 6832 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); 6833 case ISD::SHL_PARTS: 6834 case ISD::SRA_PARTS: 6835 case ISD::SRL_PARTS: return LowerShift(Op, DAG); 6836 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); 6837 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); 6838 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); 6839 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG); 6840 case ISD::FABS: return LowerFABS(Op, DAG); 6841 case ISD::FNEG: return LowerFNEG(Op, DAG); 6842 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 6843 case ISD::SETCC: return LowerSETCC(Op, DAG); 6844 case ISD::VSETCC: return LowerVSETCC(Op, DAG); 6845 case ISD::SELECT: return LowerSELECT(Op, DAG); 6846 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 6847 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 6848 case ISD::CALL: return LowerCALL(Op, DAG); 6849 case ISD::RET: return LowerRET(Op, DAG); 6850 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); 6851 case ISD::VASTART: return LowerVASTART(Op, DAG); 6852 case ISD::VAARG: return LowerVAARG(Op, DAG); 6853 case ISD::VACOPY: return LowerVACOPY(Op, DAG); 6854 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 6855 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 6856 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 6857 case ISD::FRAME_TO_ARGS_OFFSET: 6858 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); 6859 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); 6860 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); 6861 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG); 6862 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 6863 case ISD::CTLZ: return LowerCTLZ(Op, DAG); 6864 case ISD::CTTZ: return LowerCTTZ(Op, DAG); 6865 case ISD::MUL: return LowerMUL_V2I64(Op, DAG); 6866 case ISD::SADDO: 6867 case ISD::UADDO: 6868 case ISD::SSUBO: 6869 case ISD::USUBO: 6870 case ISD::SMULO: 6871 case ISD::UMULO: return LowerXALUO(Op, DAG); 6872 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG); 6873 } 6874 } 6875 6876 void X86TargetLowering:: 6877 ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results, 6878 SelectionDAG &DAG, unsigned NewOp) { 6879 MVT T = Node->getValueType(0); 6880 DebugLoc dl = Node->getDebugLoc(); 6881 assert (T == MVT::i64 && "Only know how to expand i64 atomics"); 6882 6883 SDValue Chain = Node->getOperand(0); 6884 SDValue In1 = Node->getOperand(1); 6885 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 6886 Node->getOperand(2), DAG.getIntPtrConstant(0)); 6887 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, 6888 Node->getOperand(2), DAG.getIntPtrConstant(1)); 6889 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't 6890 // have a MemOperand. Pass the info through as a normal operand. 6891 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand()); 6892 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI }; 6893 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 6894 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5); 6895 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)}; 6896 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); 6897 Results.push_back(Result.getValue(2)); 6898 } 6899 6900 /// ReplaceNodeResults - Replace a node with an illegal result type 6901 /// with a new node built out of custom code. 6902 void X86TargetLowering::ReplaceNodeResults(SDNode *N, 6903 SmallVectorImpl<SDValue>&Results, 6904 SelectionDAG &DAG) { 6905 DebugLoc dl = N->getDebugLoc(); 6906 switch (N->getOpcode()) { 6907 default: 6908 assert(false && "Do not know how to custom type legalize this operation!"); 6909 return; 6910 case ISD::FP_TO_SINT: { 6911 std::pair<SDValue,SDValue> Vals = 6912 FP_TO_INTHelper(SDValue(N, 0), DAG, true); 6913 SDValue FIST = Vals.first, StackSlot = Vals.second; 6914 if (FIST.getNode() != 0) { 6915 MVT VT = N->getValueType(0); 6916 // Return a load from the stack slot. 6917 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0)); 6918 } 6919 return; 6920 } 6921 case ISD::READCYCLECOUNTER: { 6922 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 6923 SDValue TheChain = N->getOperand(0); 6924 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); 6925 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32, 6926 rd.getValue(1)); 6927 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32, 6928 eax.getValue(2)); 6929 // Use a buildpair to merge the two 32-bit values into a 64-bit one. 6930 SDValue Ops[] = { eax, edx }; 6931 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2)); 6932 Results.push_back(edx.getValue(1)); 6933 return; 6934 } 6935 case ISD::ATOMIC_CMP_SWAP: { 6936 MVT T = N->getValueType(0); 6937 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap"); 6938 SDValue cpInL, cpInH; 6939 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2), 6940 DAG.getConstant(0, MVT::i32)); 6941 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2), 6942 DAG.getConstant(1, MVT::i32)); 6943 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue()); 6944 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH, 6945 cpInL.getValue(1)); 6946 SDValue swapInL, swapInH; 6947 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3), 6948 DAG.getConstant(0, MVT::i32)); 6949 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3), 6950 DAG.getConstant(1, MVT::i32)); 6951 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL, 6952 cpInH.getValue(1)); 6953 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH, 6954 swapInL.getValue(1)); 6955 SDValue Ops[] = { swapInH.getValue(0), 6956 N->getOperand(1), 6957 swapInH.getValue(1) }; 6958 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 6959 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3); 6960 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX, 6961 MVT::i32, Result.getValue(1)); 6962 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX, 6963 MVT::i32, cpOutL.getValue(2)); 6964 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)}; 6965 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); 6966 Results.push_back(cpOutH.getValue(1)); 6967 return; 6968 } 6969 case ISD::ATOMIC_LOAD_ADD: 6970 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG); 6971 return; 6972 case ISD::ATOMIC_LOAD_AND: 6973 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG); 6974 return; 6975 case ISD::ATOMIC_LOAD_NAND: 6976 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG); 6977 return; 6978 case ISD::ATOMIC_LOAD_OR: 6979 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG); 6980 return; 6981 case ISD::ATOMIC_LOAD_SUB: 6982 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG); 6983 return; 6984 case ISD::ATOMIC_LOAD_XOR: 6985 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG); 6986 return; 6987 case ISD::ATOMIC_SWAP: 6988 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG); 6989 return; 6990 } 6991 } 6992 6993 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { 6994 switch (Opcode) { 6995 default: return NULL; 6996 case X86ISD::BSF: return "X86ISD::BSF"; 6997 case X86ISD::BSR: return "X86ISD::BSR"; 6998 case X86ISD::SHLD: return "X86ISD::SHLD"; 6999 case X86ISD::SHRD: return "X86ISD::SHRD"; 7000 case X86ISD::FAND: return "X86ISD::FAND"; 7001 case X86ISD::FOR: return "X86ISD::FOR"; 7002 case X86ISD::FXOR: return "X86ISD::FXOR"; 7003 case X86ISD::FSRL: return "X86ISD::FSRL"; 7004 case X86ISD::FILD: return "X86ISD::FILD"; 7005 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; 7006 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; 7007 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; 7008 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM"; 7009 case X86ISD::FLD: return "X86ISD::FLD"; 7010 case X86ISD::FST: return "X86ISD::FST"; 7011 case X86ISD::CALL: return "X86ISD::CALL"; 7012 case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; 7013 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; 7014 case X86ISD::BT: return "X86ISD::BT"; 7015 case X86ISD::CMP: return "X86ISD::CMP"; 7016 case X86ISD::COMI: return "X86ISD::COMI"; 7017 case X86ISD::UCOMI: return "X86ISD::UCOMI"; 7018 case X86ISD::SETCC: return "X86ISD::SETCC"; 7019 case X86ISD::CMOV: return "X86ISD::CMOV"; 7020 case X86ISD::BRCOND: return "X86ISD::BRCOND"; 7021 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; 7022 case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; 7023 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; 7024 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; 7025 case X86ISD::Wrapper: return "X86ISD::Wrapper"; 7026 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP"; 7027 case X86ISD::PEXTRB: return "X86ISD::PEXTRB"; 7028 case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; 7029 case X86ISD::INSERTPS: return "X86ISD::INSERTPS"; 7030 case X86ISD::PINSRB: return "X86ISD::PINSRB"; 7031 case X86ISD::PINSRW: return "X86ISD::PINSRW"; 7032 case X86ISD::PSHUFB: return "X86ISD::PSHUFB"; 7033 case X86ISD::FMAX: return "X86ISD::FMAX"; 7034 case X86ISD::FMIN: return "X86ISD::FMIN"; 7035 case X86ISD::FRSQRT: return "X86ISD::FRSQRT"; 7036 case X86ISD::FRCP: return "X86ISD::FRCP"; 7037 case X86ISD::TLSADDR: return "X86ISD::TLSADDR"; 7038 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress"; 7039 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN"; 7040 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN"; 7041 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m"; 7042 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG"; 7043 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG"; 7044 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG"; 7045 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG"; 7046 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG"; 7047 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG"; 7048 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG"; 7049 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG"; 7050 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL"; 7051 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD"; 7052 case X86ISD::VSHL: return "X86ISD::VSHL"; 7053 case X86ISD::VSRL: return "X86ISD::VSRL"; 7054 case X86ISD::CMPPD: return "X86ISD::CMPPD"; 7055 case X86ISD::CMPPS: return "X86ISD::CMPPS"; 7056 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB"; 7057 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW"; 7058 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD"; 7059 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ"; 7060 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB"; 7061 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW"; 7062 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD"; 7063 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ"; 7064 case X86ISD::ADD: return "X86ISD::ADD"; 7065 case X86ISD::SUB: return "X86ISD::SUB"; 7066 case X86ISD::SMUL: return "X86ISD::SMUL"; 7067 case X86ISD::UMUL: return "X86ISD::UMUL"; 7068 case X86ISD::INC: return "X86ISD::INC"; 7069 case X86ISD::DEC: return "X86ISD::DEC"; 7070 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM"; 7071 case X86ISD::PTEST: return "X86ISD::PTEST"; 7072 } 7073 } 7074 7075 // isLegalAddressingMode - Return true if the addressing mode represented 7076 // by AM is legal for this target, for a load/store of the specified type. 7077 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, 7078 const Type *Ty) const { 7079 // X86 supports extremely general addressing modes. 7080 7081 // X86 allows a sign-extended 32-bit immediate field as a displacement. 7082 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1) 7083 return false; 7084 7085 if (AM.BaseGV) { 7086 unsigned GVFlags = 7087 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine()); 7088 7089 // If a reference to this global requires an extra load, we can't fold it. 7090 if (isGlobalStubReference(GVFlags)) 7091 return false; 7092 7093 // If BaseGV requires a register for the PIC base, we cannot also have a 7094 // BaseReg specified. 7095 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags)) 7096 return false; 7097 7098 // X86-64 only supports addr of globals in small code model. 7099 if (Subtarget->is64Bit()) { 7100 if (getTargetMachine().getCodeModel() != CodeModel::Small) 7101 return false; 7102 // If lower 4G is not available, then we must use rip-relative addressing. 7103 if (AM.BaseOffs || AM.Scale > 1) 7104 return false; 7105 } 7106 } 7107 7108 switch (AM.Scale) { 7109 case 0: 7110 case 1: 7111 case 2: 7112 case 4: 7113 case 8: 7114 // These scales always work. 7115 break; 7116 case 3: 7117 case 5: 7118 case 9: 7119 // These scales are formed with basereg+scalereg. Only accept if there is 7120 // no basereg yet. 7121 if (AM.HasBaseReg) 7122 return false; 7123 break; 7124 default: // Other stuff never works. 7125 return false; 7126 } 7127 7128 return true; 7129 } 7130 7131 7132 bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const { 7133 if (!Ty1->isInteger() || !Ty2->isInteger()) 7134 return false; 7135 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); 7136 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); 7137 if (NumBits1 <= NumBits2) 7138 return false; 7139 return Subtarget->is64Bit() || NumBits1 < 64; 7140 } 7141 7142 bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const { 7143 if (!VT1.isInteger() || !VT2.isInteger()) 7144 return false; 7145 unsigned NumBits1 = VT1.getSizeInBits(); 7146 unsigned NumBits2 = VT2.getSizeInBits(); 7147 if (NumBits1 <= NumBits2) 7148 return false; 7149 return Subtarget->is64Bit() || NumBits1 < 64; 7150 } 7151 7152 bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const { 7153 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. 7154 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit(); 7155 } 7156 7157 bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const { 7158 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. 7159 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit(); 7160 } 7161 7162 bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const { 7163 // i16 instructions are longer (0x66 prefix) and potentially slower. 7164 return !(VT1 == MVT::i32 && VT2 == MVT::i16); 7165 } 7166 7167 /// isShuffleMaskLegal - Targets can use this to indicate that they only 7168 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 7169 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 7170 /// are assumed to be legal. 7171 bool 7172 X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 7173 MVT VT) const { 7174 // Only do shuffles on 128-bit vector types for now. 7175 if (VT.getSizeInBits() == 64) 7176 return false; 7177 7178 // FIXME: pshufb, blends, palignr, shifts. 7179 return (VT.getVectorNumElements() == 2 || 7180 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 7181 isMOVLMask(M, VT) || 7182 isSHUFPMask(M, VT) || 7183 isPSHUFDMask(M, VT) || 7184 isPSHUFHWMask(M, VT) || 7185 isPSHUFLWMask(M, VT) || 7186 isUNPCKLMask(M, VT) || 7187 isUNPCKHMask(M, VT) || 7188 isUNPCKL_v_undef_Mask(M, VT) || 7189 isUNPCKH_v_undef_Mask(M, VT)); 7190 } 7191 7192 bool 7193 X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask, 7194 MVT VT) const { 7195 unsigned NumElts = VT.getVectorNumElements(); 7196 // FIXME: This collection of masks seems suspect. 7197 if (NumElts == 2) 7198 return true; 7199 if (NumElts == 4 && VT.getSizeInBits() == 128) { 7200 return (isMOVLMask(Mask, VT) || 7201 isCommutedMOVLMask(Mask, VT, true) || 7202 isSHUFPMask(Mask, VT) || 7203 isCommutedSHUFPMask(Mask, VT)); 7204 } 7205 return false; 7206 } 7207 7208 //===----------------------------------------------------------------------===// 7209 // X86 Scheduler Hooks 7210 //===----------------------------------------------------------------------===// 7211 7212 // private utility function 7213 MachineBasicBlock * 7214 X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr, 7215 MachineBasicBlock *MBB, 7216 unsigned regOpc, 7217 unsigned immOpc, 7218 unsigned LoadOpc, 7219 unsigned CXchgOpc, 7220 unsigned copyOpc, 7221 unsigned notOpc, 7222 unsigned EAXreg, 7223 TargetRegisterClass *RC, 7224 bool invSrc) const { 7225 // For the atomic bitwise operator, we generate 7226 // thisMBB: 7227 // newMBB: 7228 // ld t1 = [bitinstr.addr] 7229 // op t2 = t1, [bitinstr.val] 7230 // mov EAX = t1 7231 // lcs dest = [bitinstr.addr], t2 [EAX is implicit] 7232 // bz newMBB 7233 // fallthrough -->nextMBB 7234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 7235 const BasicBlock *LLVM_BB = MBB->getBasicBlock(); 7236 MachineFunction::iterator MBBIter = MBB; 7237 ++MBBIter; 7238 7239 /// First build the CFG 7240 MachineFunction *F = MBB->getParent(); 7241 MachineBasicBlock *thisMBB = MBB; 7242 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); 7243 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); 7244 F->insert(MBBIter, newMBB); 7245 F->insert(MBBIter, nextMBB); 7246 7247 // Move all successors to thisMBB to nextMBB 7248 nextMBB->transferSuccessors(thisMBB); 7249 7250 // Update thisMBB to fall through to newMBB 7251 thisMBB->addSuccessor(newMBB); 7252 7253 // newMBB jumps to itself and fall through to nextMBB 7254 newMBB->addSuccessor(nextMBB); 7255 newMBB->addSuccessor(newMBB); 7256 7257 // Insert instructions into newMBB based on incoming instruction 7258 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 && 7259 "unexpected number of operands"); 7260 DebugLoc dl = bInstr->getDebugLoc(); 7261 MachineOperand& destOper = bInstr->getOperand(0); 7262 MachineOperand* argOpers[2 + X86AddrNumOperands]; 7263 int numArgs = bInstr->getNumOperands() - 1; 7264 for (int i=0; i < numArgs; ++i) 7265 argOpers[i] = &bInstr->getOperand(i+1); 7266 7267 // x86 address has 4 operands: base, index, scale, and displacement 7268 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] 7269 int valArgIndx = lastAddrIndx + 1; 7270 7271 unsigned t1 = F->getRegInfo().createVirtualRegister(RC); 7272 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1); 7273 for (int i=0; i <= lastAddrIndx; ++i) 7274 (*MIB).addOperand(*argOpers[i]); 7275 7276 unsigned tt = F->getRegInfo().createVirtualRegister(RC); 7277 if (invSrc) { 7278 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1); 7279 } 7280 else 7281 tt = t1; 7282 7283 unsigned t2 = F->getRegInfo().createVirtualRegister(RC); 7284 assert((argOpers[valArgIndx]->isReg() || 7285 argOpers[valArgIndx]->isImm()) && 7286 "invalid operand"); 7287 if (argOpers[valArgIndx]->isReg()) 7288 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2); 7289 else 7290 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2); 7291 MIB.addReg(tt); 7292 (*MIB).addOperand(*argOpers[valArgIndx]); 7293 7294 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg); 7295 MIB.addReg(t1); 7296 7297 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc)); 7298 for (int i=0; i <= lastAddrIndx; ++i) 7299 (*MIB).addOperand(*argOpers[i]); 7300 MIB.addReg(t2); 7301 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand"); 7302 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin()); 7303 7304 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg()); 7305 MIB.addReg(EAXreg); 7306 7307 // insert branch 7308 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); 7309 7310 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now. 7311 return nextMBB; 7312 } 7313 7314 // private utility function: 64 bit atomics on 32 bit host. 7315 MachineBasicBlock * 7316 X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr, 7317 MachineBasicBlock *MBB, 7318 unsigned regOpcL, 7319 unsigned regOpcH, 7320 unsigned immOpcL, 7321 unsigned immOpcH, 7322 bool invSrc) const { 7323 // For the atomic bitwise operator, we generate 7324 // thisMBB (instructions are in pairs, except cmpxchg8b) 7325 // ld t1,t2 = [bitinstr.addr] 7326 // newMBB: 7327 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4) 7328 // op t5, t6 <- out1, out2, [bitinstr.val] 7329 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val]) 7330 // mov ECX, EBX <- t5, t6 7331 // mov EAX, EDX <- t1, t2 7332 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit] 7333 // mov t3, t4 <- EAX, EDX 7334 // bz newMBB 7335 // result in out1, out2 7336 // fallthrough -->nextMBB 7337 7338 const TargetRegisterClass *RC = X86::GR32RegisterClass; 7339 const unsigned LoadOpc = X86::MOV32rm; 7340 const unsigned copyOpc = X86::MOV32rr; 7341 const unsigned NotOpc = X86::NOT32r; 7342 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 7343 const BasicBlock *LLVM_BB = MBB->getBasicBlock(); 7344 MachineFunction::iterator MBBIter = MBB; 7345 ++MBBIter; 7346 7347 /// First build the CFG 7348 MachineFunction *F = MBB->getParent(); 7349 MachineBasicBlock *thisMBB = MBB; 7350 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); 7351 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); 7352 F->insert(MBBIter, newMBB); 7353 F->insert(MBBIter, nextMBB); 7354 7355 // Move all successors to thisMBB to nextMBB 7356 nextMBB->transferSuccessors(thisMBB); 7357 7358 // Update thisMBB to fall through to newMBB 7359 thisMBB->addSuccessor(newMBB); 7360 7361 // newMBB jumps to itself and fall through to nextMBB 7362 newMBB->addSuccessor(nextMBB); 7363 newMBB->addSuccessor(newMBB); 7364 7365 DebugLoc dl = bInstr->getDebugLoc(); 7366 // Insert instructions into newMBB based on incoming instruction 7367 // There are 8 "real" operands plus 9 implicit def/uses, ignored here. 7368 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 && 7369 "unexpected number of operands"); 7370 MachineOperand& dest1Oper = bInstr->getOperand(0); 7371 MachineOperand& dest2Oper = bInstr->getOperand(1); 7372 MachineOperand* argOpers[2 + X86AddrNumOperands]; 7373 for (int i=0; i < 2 + X86AddrNumOperands; ++i) 7374 argOpers[i] = &bInstr->getOperand(i+2); 7375 7376 // x86 address has 4 operands: base, index, scale, and displacement 7377 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] 7378 7379 unsigned t1 = F->getRegInfo().createVirtualRegister(RC); 7380 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1); 7381 for (int i=0; i <= lastAddrIndx; ++i) 7382 (*MIB).addOperand(*argOpers[i]); 7383 unsigned t2 = F->getRegInfo().createVirtualRegister(RC); 7384 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2); 7385 // add 4 to displacement. 7386 for (int i=0; i <= lastAddrIndx-2; ++i) 7387 (*MIB).addOperand(*argOpers[i]); 7388 MachineOperand newOp3 = *(argOpers[3]); 7389 if (newOp3.isImm()) 7390 newOp3.setImm(newOp3.getImm()+4); 7391 else 7392 newOp3.setOffset(newOp3.getOffset()+4); 7393 (*MIB).addOperand(newOp3); 7394 (*MIB).addOperand(*argOpers[lastAddrIndx]); 7395 7396 // t3/4 are defined later, at the bottom of the loop 7397 unsigned t3 = F->getRegInfo().createVirtualRegister(RC); 7398 unsigned t4 = F->getRegInfo().createVirtualRegister(RC); 7399 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg()) 7400 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB); 7401 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg()) 7402 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB); 7403 7404 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC); 7405 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC); 7406 if (invSrc) { 7407 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1); 7408 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2); 7409 } else { 7410 tt1 = t1; 7411 tt2 = t2; 7412 } 7413 7414 int valArgIndx = lastAddrIndx + 1; 7415 assert((argOpers[valArgIndx]->isReg() || 7416 argOpers[valArgIndx]->isImm()) && 7417 "invalid operand"); 7418 unsigned t5 = F->getRegInfo().createVirtualRegister(RC); 7419 unsigned t6 = F->getRegInfo().createVirtualRegister(RC); 7420 if (argOpers[valArgIndx]->isReg()) 7421 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5); 7422 else 7423 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5); 7424 if (regOpcL != X86::MOV32rr) 7425 MIB.addReg(tt1); 7426 (*MIB).addOperand(*argOpers[valArgIndx]); 7427 assert(argOpers[valArgIndx + 1]->isReg() == 7428 argOpers[valArgIndx]->isReg()); 7429 assert(argOpers[valArgIndx + 1]->isImm() == 7430 argOpers[valArgIndx]->isImm()); 7431 if (argOpers[valArgIndx + 1]->isReg()) 7432 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6); 7433 else 7434 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6); 7435 if (regOpcH != X86::MOV32rr) 7436 MIB.addReg(tt2); 7437 (*MIB).addOperand(*argOpers[valArgIndx + 1]); 7438 7439 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX); 7440 MIB.addReg(t1); 7441 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX); 7442 MIB.addReg(t2); 7443 7444 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX); 7445 MIB.addReg(t5); 7446 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX); 7447 MIB.addReg(t6); 7448 7449 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B)); 7450 for (int i=0; i <= lastAddrIndx; ++i) 7451 (*MIB).addOperand(*argOpers[i]); 7452 7453 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand"); 7454 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin()); 7455 7456 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3); 7457 MIB.addReg(X86::EAX); 7458 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4); 7459 MIB.addReg(X86::EDX); 7460 7461 // insert branch 7462 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); 7463 7464 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now. 7465 return nextMBB; 7466 } 7467 7468 // private utility function 7469 MachineBasicBlock * 7470 X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr, 7471 MachineBasicBlock *MBB, 7472 unsigned cmovOpc) const { 7473 // For the atomic min/max operator, we generate 7474 // thisMBB: 7475 // newMBB: 7476 // ld t1 = [min/max.addr] 7477 // mov t2 = [min/max.val] 7478 // cmp t1, t2 7479 // cmov[cond] t2 = t1 7480 // mov EAX = t1 7481 // lcs dest = [bitinstr.addr], t2 [EAX is implicit] 7482 // bz newMBB 7483 // fallthrough -->nextMBB 7484 // 7485 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 7486 const BasicBlock *LLVM_BB = MBB->getBasicBlock(); 7487 MachineFunction::iterator MBBIter = MBB; 7488 ++MBBIter; 7489 7490 /// First build the CFG 7491 MachineFunction *F = MBB->getParent(); 7492 MachineBasicBlock *thisMBB = MBB; 7493 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); 7494 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); 7495 F->insert(MBBIter, newMBB); 7496 F->insert(MBBIter, nextMBB); 7497 7498 // Move all successors to thisMBB to nextMBB 7499 nextMBB->transferSuccessors(thisMBB); 7500 7501 // Update thisMBB to fall through to newMBB 7502 thisMBB->addSuccessor(newMBB); 7503 7504 // newMBB jumps to newMBB and fall through to nextMBB 7505 newMBB->addSuccessor(nextMBB); 7506 newMBB->addSuccessor(newMBB); 7507 7508 DebugLoc dl = mInstr->getDebugLoc(); 7509 // Insert instructions into newMBB based on incoming instruction 7510 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 && 7511 "unexpected number of operands"); 7512 MachineOperand& destOper = mInstr->getOperand(0); 7513 MachineOperand* argOpers[2 + X86AddrNumOperands]; 7514 int numArgs = mInstr->getNumOperands() - 1; 7515 for (int i=0; i < numArgs; ++i) 7516 argOpers[i] = &mInstr->getOperand(i+1); 7517 7518 // x86 address has 4 operands: base, index, scale, and displacement 7519 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] 7520 int valArgIndx = lastAddrIndx + 1; 7521 7522 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); 7523 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1); 7524 for (int i=0; i <= lastAddrIndx; ++i) 7525 (*MIB).addOperand(*argOpers[i]); 7526 7527 // We only support register and immediate values 7528 assert((argOpers[valArgIndx]->isReg() || 7529 argOpers[valArgIndx]->isImm()) && 7530 "invalid operand"); 7531 7532 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); 7533 if (argOpers[valArgIndx]->isReg()) 7534 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2); 7535 else 7536 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2); 7537 (*MIB).addOperand(*argOpers[valArgIndx]); 7538 7539 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX); 7540 MIB.addReg(t1); 7541 7542 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr)); 7543 MIB.addReg(t1); 7544 MIB.addReg(t2); 7545 7546 // Generate movc 7547 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); 7548 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3); 7549 MIB.addReg(t2); 7550 MIB.addReg(t1); 7551 7552 // Cmp and exchange if none has modified the memory location 7553 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32)); 7554 for (int i=0; i <= lastAddrIndx; ++i) 7555 (*MIB).addOperand(*argOpers[i]); 7556 MIB.addReg(t3); 7557 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand"); 7558 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin()); 7559 7560 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg()); 7561 MIB.addReg(X86::EAX); 7562 7563 // insert branch 7564 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); 7565 7566 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now. 7567 return nextMBB; 7568 } 7569 7570 7571 MachineBasicBlock * 7572 X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 7573 MachineBasicBlock *BB) const { 7574 DebugLoc dl = MI->getDebugLoc(); 7575 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 7576 switch (MI->getOpcode()) { 7577 default: assert(false && "Unexpected instr type to insert"); 7578 case X86::CMOV_V1I64: 7579 case X86::CMOV_FR32: 7580 case X86::CMOV_FR64: 7581 case X86::CMOV_V4F32: 7582 case X86::CMOV_V2F64: 7583 case X86::CMOV_V2I64: { 7584 // To "insert" a SELECT_CC instruction, we actually have to insert the 7585 // diamond control-flow pattern. The incoming instruction knows the 7586 // destination vreg to set, the condition code register to branch on, the 7587 // true/false values to select between, and a branch opcode to use. 7588 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7589 MachineFunction::iterator It = BB; 7590 ++It; 7591 7592 // thisMBB: 7593 // ... 7594 // TrueVal = ... 7595 // cmpTY ccX, r1, r2 7596 // bCC copy1MBB 7597 // fallthrough --> copy0MBB 7598 MachineBasicBlock *thisMBB = BB; 7599 MachineFunction *F = BB->getParent(); 7600 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 7601 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 7602 unsigned Opc = 7603 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); 7604 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB); 7605 F->insert(It, copy0MBB); 7606 F->insert(It, sinkMBB); 7607 // Update machine-CFG edges by transferring all successors of the current 7608 // block to the new block which will contain the Phi node for the select. 7609 sinkMBB->transferSuccessors(BB); 7610 7611 // Add the true and fallthrough blocks as its successors. 7612 BB->addSuccessor(copy0MBB); 7613 BB->addSuccessor(sinkMBB); 7614 7615 // copy0MBB: 7616 // %FalseValue = ... 7617 // # fallthrough to sinkMBB 7618 BB = copy0MBB; 7619 7620 // Update machine-CFG edges 7621 BB->addSuccessor(sinkMBB); 7622 7623 // sinkMBB: 7624 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 7625 // ... 7626 BB = sinkMBB; 7627 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg()) 7628 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 7629 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 7630 7631 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. 7632 return BB; 7633 } 7634 7635 case X86::FP32_TO_INT16_IN_MEM: 7636 case X86::FP32_TO_INT32_IN_MEM: 7637 case X86::FP32_TO_INT64_IN_MEM: 7638 case X86::FP64_TO_INT16_IN_MEM: 7639 case X86::FP64_TO_INT32_IN_MEM: 7640 case X86::FP64_TO_INT64_IN_MEM: 7641 case X86::FP80_TO_INT16_IN_MEM: 7642 case X86::FP80_TO_INT32_IN_MEM: 7643 case X86::FP80_TO_INT64_IN_MEM: { 7644 // Change the floating point control register to use "round towards zero" 7645 // mode when truncating to an integer value. 7646 MachineFunction *F = BB->getParent(); 7647 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); 7648 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx); 7649 7650 // Load the old value of the high byte of the control word... 7651 unsigned OldCW = 7652 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass); 7653 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW), 7654 CWFrameIdx); 7655 7656 // Set the high part to be round to zero... 7657 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx) 7658 .addImm(0xC7F); 7659 7660 // Reload the modified control word now... 7661 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx); 7662 7663 // Restore the memory image of control word to original value 7664 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx) 7665 .addReg(OldCW); 7666 7667 // Get the X86 opcode to use. 7668 unsigned Opc; 7669 switch (MI->getOpcode()) { 7670 default: llvm_unreachable("illegal opcode!"); 7671 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; 7672 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; 7673 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; 7674 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; 7675 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; 7676 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; 7677 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; 7678 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; 7679 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; 7680 } 7681 7682 X86AddressMode AM; 7683 MachineOperand &Op = MI->getOperand(0); 7684 if (Op.isReg()) { 7685 AM.BaseType = X86AddressMode::RegBase; 7686 AM.Base.Reg = Op.getReg(); 7687 } else { 7688 AM.BaseType = X86AddressMode::FrameIndexBase; 7689 AM.Base.FrameIndex = Op.getIndex(); 7690 } 7691 Op = MI->getOperand(1); 7692 if (Op.isImm()) 7693 AM.Scale = Op.getImm(); 7694 Op = MI->getOperand(2); 7695 if (Op.isImm()) 7696 AM.IndexReg = Op.getImm(); 7697 Op = MI->getOperand(3); 7698 if (Op.isGlobal()) { 7699 AM.GV = Op.getGlobal(); 7700 } else { 7701 AM.Disp = Op.getImm(); 7702 } 7703 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM) 7704 .addReg(MI->getOperand(X86AddrNumOperands).getReg()); 7705 7706 // Reload the original control word now. 7707 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx); 7708 7709 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. 7710 return BB; 7711 } 7712 case X86::ATOMAND32: 7713 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr, 7714 X86::AND32ri, X86::MOV32rm, 7715 X86::LCMPXCHG32, X86::MOV32rr, 7716 X86::NOT32r, X86::EAX, 7717 X86::GR32RegisterClass); 7718 case X86::ATOMOR32: 7719 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr, 7720 X86::OR32ri, X86::MOV32rm, 7721 X86::LCMPXCHG32, X86::MOV32rr, 7722 X86::NOT32r, X86::EAX, 7723 X86::GR32RegisterClass); 7724 case X86::ATOMXOR32: 7725 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr, 7726 X86::XOR32ri, X86::MOV32rm, 7727 X86::LCMPXCHG32, X86::MOV32rr, 7728 X86::NOT32r, X86::EAX, 7729 X86::GR32RegisterClass); 7730 case X86::ATOMNAND32: 7731 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr, 7732 X86::AND32ri, X86::MOV32rm, 7733 X86::LCMPXCHG32, X86::MOV32rr, 7734 X86::NOT32r, X86::EAX, 7735 X86::GR32RegisterClass, true); 7736 case X86::ATOMMIN32: 7737 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr); 7738 case X86::ATOMMAX32: 7739 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr); 7740 case X86::ATOMUMIN32: 7741 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr); 7742 case X86::ATOMUMAX32: 7743 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr); 7744 7745 case X86::ATOMAND16: 7746 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr, 7747 X86::AND16ri, X86::MOV16rm, 7748 X86::LCMPXCHG16, X86::MOV16rr, 7749 X86::NOT16r, X86::AX, 7750 X86::GR16RegisterClass); 7751 case X86::ATOMOR16: 7752 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr, 7753 X86::OR16ri, X86::MOV16rm, 7754 X86::LCMPXCHG16, X86::MOV16rr, 7755 X86::NOT16r, X86::AX, 7756 X86::GR16RegisterClass); 7757 case X86::ATOMXOR16: 7758 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr, 7759 X86::XOR16ri, X86::MOV16rm, 7760 X86::LCMPXCHG16, X86::MOV16rr, 7761 X86::NOT16r, X86::AX, 7762 X86::GR16RegisterClass); 7763 case X86::ATOMNAND16: 7764 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr, 7765 X86::AND16ri, X86::MOV16rm, 7766 X86::LCMPXCHG16, X86::MOV16rr, 7767 X86::NOT16r, X86::AX, 7768 X86::GR16RegisterClass, true); 7769 case X86::ATOMMIN16: 7770 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr); 7771 case X86::ATOMMAX16: 7772 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr); 7773 case X86::ATOMUMIN16: 7774 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr); 7775 case X86::ATOMUMAX16: 7776 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr); 7777 7778 case X86::ATOMAND8: 7779 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr, 7780 X86::AND8ri, X86::MOV8rm, 7781 X86::LCMPXCHG8, X86::MOV8rr, 7782 X86::NOT8r, X86::AL, 7783 X86::GR8RegisterClass); 7784 case X86::ATOMOR8: 7785 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr, 7786 X86::OR8ri, X86::MOV8rm, 7787 X86::LCMPXCHG8, X86::MOV8rr, 7788 X86::NOT8r, X86::AL, 7789 X86::GR8RegisterClass); 7790 case X86::ATOMXOR8: 7791 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr, 7792 X86::XOR8ri, X86::MOV8rm, 7793 X86::LCMPXCHG8, X86::MOV8rr, 7794 X86::NOT8r, X86::AL, 7795 X86::GR8RegisterClass); 7796 case X86::ATOMNAND8: 7797 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr, 7798 X86::AND8ri, X86::MOV8rm, 7799 X86::LCMPXCHG8, X86::MOV8rr, 7800 X86::NOT8r, X86::AL, 7801 X86::GR8RegisterClass, true); 7802 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way. 7803 // This group is for 64-bit host. 7804 case X86::ATOMAND64: 7805 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr, 7806 X86::AND64ri32, X86::MOV64rm, 7807 X86::LCMPXCHG64, X86::MOV64rr, 7808 X86::NOT64r, X86::RAX, 7809 X86::GR64RegisterClass); 7810 case X86::ATOMOR64: 7811 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr, 7812 X86::OR64ri32, X86::MOV64rm, 7813 X86::LCMPXCHG64, X86::MOV64rr, 7814 X86::NOT64r, X86::RAX, 7815 X86::GR64RegisterClass); 7816 case X86::ATOMXOR64: 7817 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr, 7818 X86::XOR64ri32, X86::MOV64rm, 7819 X86::LCMPXCHG64, X86::MOV64rr, 7820 X86::NOT64r, X86::RAX, 7821 X86::GR64RegisterClass); 7822 case X86::ATOMNAND64: 7823 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr, 7824 X86::AND64ri32, X86::MOV64rm, 7825 X86::LCMPXCHG64, X86::MOV64rr, 7826 X86::NOT64r, X86::RAX, 7827 X86::GR64RegisterClass, true); 7828 case X86::ATOMMIN64: 7829 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr); 7830 case X86::ATOMMAX64: 7831 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr); 7832 case X86::ATOMUMIN64: 7833 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr); 7834 case X86::ATOMUMAX64: 7835 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr); 7836 7837 // This group does 64-bit operations on a 32-bit host. 7838 case X86::ATOMAND6432: 7839 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7840 X86::AND32rr, X86::AND32rr, 7841 X86::AND32ri, X86::AND32ri, 7842 false); 7843 case X86::ATOMOR6432: 7844 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7845 X86::OR32rr, X86::OR32rr, 7846 X86::OR32ri, X86::OR32ri, 7847 false); 7848 case X86::ATOMXOR6432: 7849 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7850 X86::XOR32rr, X86::XOR32rr, 7851 X86::XOR32ri, X86::XOR32ri, 7852 false); 7853 case X86::ATOMNAND6432: 7854 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7855 X86::AND32rr, X86::AND32rr, 7856 X86::AND32ri, X86::AND32ri, 7857 true); 7858 case X86::ATOMADD6432: 7859 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7860 X86::ADD32rr, X86::ADC32rr, 7861 X86::ADD32ri, X86::ADC32ri, 7862 false); 7863 case X86::ATOMSUB6432: 7864 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7865 X86::SUB32rr, X86::SBB32rr, 7866 X86::SUB32ri, X86::SBB32ri, 7867 false); 7868 case X86::ATOMSWAP6432: 7869 return EmitAtomicBit6432WithCustomInserter(MI, BB, 7870 X86::MOV32rr, X86::MOV32rr, 7871 X86::MOV32ri, X86::MOV32ri, 7872 false); 7873 } 7874 } 7875 7876 //===----------------------------------------------------------------------===// 7877 // X86 Optimization Hooks 7878 //===----------------------------------------------------------------------===// 7879 7880 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, 7881 const APInt &Mask, 7882 APInt &KnownZero, 7883 APInt &KnownOne, 7884 const SelectionDAG &DAG, 7885 unsigned Depth) const { 7886 unsigned Opc = Op.getOpcode(); 7887 assert((Opc >= ISD::BUILTIN_OP_END || 7888 Opc == ISD::INTRINSIC_WO_CHAIN || 7889 Opc == ISD::INTRINSIC_W_CHAIN || 7890 Opc == ISD::INTRINSIC_VOID) && 7891 "Should use MaskedValueIsZero if you don't know whether Op" 7892 " is a target node!"); 7893 7894 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything. 7895 switch (Opc) { 7896 default: break; 7897 case X86ISD::ADD: 7898 case X86ISD::SUB: 7899 case X86ISD::SMUL: 7900 case X86ISD::UMUL: 7901 case X86ISD::INC: 7902 case X86ISD::DEC: 7903 // These nodes' second result is a boolean. 7904 if (Op.getResNo() == 0) 7905 break; 7906 // Fallthrough 7907 case X86ISD::SETCC: 7908 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(), 7909 Mask.getBitWidth() - 1); 7910 break; 7911 } 7912 } 7913 7914 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the 7915 /// node is a GlobalAddress + offset. 7916 bool X86TargetLowering::isGAPlusOffset(SDNode *N, 7917 GlobalValue* &GA, int64_t &Offset) const{ 7918 if (N->getOpcode() == X86ISD::Wrapper) { 7919 if (isa<GlobalAddressSDNode>(N->getOperand(0))) { 7920 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); 7921 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset(); 7922 return true; 7923 } 7924 } 7925 return TargetLowering::isGAPlusOffset(N, GA, Offset); 7926 } 7927 7928 static bool isBaseAlignmentOfN(unsigned N, SDNode *Base, 7929 const TargetLowering &TLI) { 7930 GlobalValue *GV; 7931 int64_t Offset = 0; 7932 if (TLI.isGAPlusOffset(Base, GV, Offset)) 7933 return (GV->getAlignment() >= N && (Offset % N) == 0); 7934 // DAG combine handles the stack object case. 7935 return false; 7936 } 7937 7938 static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems, 7939 MVT EVT, LoadSDNode *&LDBase, 7940 unsigned &LastLoadedElt, 7941 SelectionDAG &DAG, MachineFrameInfo *MFI, 7942 const TargetLowering &TLI) { 7943 LDBase = NULL; 7944 LastLoadedElt = -1U; 7945 for (unsigned i = 0; i < NumElems; ++i) { 7946 if (N->getMaskElt(i) < 0) { 7947 if (!LDBase) 7948 return false; 7949 continue; 7950 } 7951 7952 SDValue Elt = DAG.getShuffleScalarElt(N, i); 7953 if (!Elt.getNode() || 7954 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode()))) 7955 return false; 7956 if (!LDBase) { 7957 if (Elt.getNode()->getOpcode() == ISD::UNDEF) 7958 return false; 7959 LDBase = cast<LoadSDNode>(Elt.getNode()); 7960 LastLoadedElt = i; 7961 continue; 7962 } 7963 if (Elt.getOpcode() == ISD::UNDEF) 7964 continue; 7965 7966 LoadSDNode *LD = cast<LoadSDNode>(Elt); 7967 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI)) 7968 return false; 7969 LastLoadedElt = i; 7970 } 7971 return true; 7972 } 7973 7974 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to 7975 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load 7976 /// if the load addresses are consecutive, non-overlapping, and in the right 7977 /// order. In the case of v2i64, it will see if it can rewrite the 7978 /// shuffle to be an appropriate build vector so it can take advantage of 7979 // performBuildVectorCombine. 7980 static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, 7981 const TargetLowering &TLI) { 7982 DebugLoc dl = N->getDebugLoc(); 7983 MVT VT = N->getValueType(0); 7984 MVT EVT = VT.getVectorElementType(); 7985 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 7986 unsigned NumElems = VT.getVectorNumElements(); 7987 7988 if (VT.getSizeInBits() != 128) 7989 return SDValue(); 7990 7991 // Try to combine a vector_shuffle into a 128-bit load. 7992 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 7993 LoadSDNode *LD = NULL; 7994 unsigned LastLoadedElt; 7995 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG, 7996 MFI, TLI)) 7997 return SDValue(); 7998 7999 if (LastLoadedElt == NumElems - 1) { 8000 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI)) 8001 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), 8002 LD->getSrcValue(), LD->getSrcValueOffset(), 8003 LD->isVolatile()); 8004 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), 8005 LD->getSrcValue(), LD->getSrcValueOffset(), 8006 LD->isVolatile(), LD->getAlignment()); 8007 } else if (NumElems == 4 && LastLoadedElt == 1) { 8008 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other); 8009 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() }; 8010 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2); 8011 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode); 8012 } 8013 return SDValue(); 8014 } 8015 8016 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. 8017 static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, 8018 const X86Subtarget *Subtarget) { 8019 DebugLoc DL = N->getDebugLoc(); 8020 SDValue Cond = N->getOperand(0); 8021 // Get the LHS/RHS of the select. 8022 SDValue LHS = N->getOperand(1); 8023 SDValue RHS = N->getOperand(2); 8024 8025 // If we have SSE[12] support, try to form min/max nodes. 8026 if (Subtarget->hasSSE2() && 8027 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) && 8028 Cond.getOpcode() == ISD::SETCC) { 8029 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 8030 8031 unsigned Opcode = 0; 8032 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { 8033 switch (CC) { 8034 default: break; 8035 case ISD::SETOLE: // (X <= Y) ? X : Y -> min 8036 case ISD::SETULE: 8037 case ISD::SETLE: 8038 if (!UnsafeFPMath) break; 8039 // FALL THROUGH. 8040 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min 8041 case ISD::SETLT: 8042 Opcode = X86ISD::FMIN; 8043 break; 8044 8045 case ISD::SETOGT: // (X > Y) ? X : Y -> max 8046 case ISD::SETUGT: 8047 case ISD::SETGT: 8048 if (!UnsafeFPMath) break; 8049 // FALL THROUGH. 8050 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max 8051 case ISD::SETGE: 8052 Opcode = X86ISD::FMAX; 8053 break; 8054 } 8055 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { 8056 switch (CC) { 8057 default: break; 8058 case ISD::SETOGT: // (X > Y) ? Y : X -> min 8059 case ISD::SETUGT: 8060 case ISD::SETGT: 8061 if (!UnsafeFPMath) break; 8062 // FALL THROUGH. 8063 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min 8064 case ISD::SETGE: 8065 Opcode = X86ISD::FMIN; 8066 break; 8067 8068 case ISD::SETOLE: // (X <= Y) ? Y : X -> max 8069 case ISD::SETULE: 8070 case ISD::SETLE: 8071 if (!UnsafeFPMath) break; 8072 // FALL THROUGH. 8073 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max 8074 case ISD::SETLT: 8075 Opcode = X86ISD::FMAX; 8076 break; 8077 } 8078 } 8079 8080 if (Opcode) 8081 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS); 8082 } 8083 8084 // If this is a select between two integer constants, try to do some 8085 // optimizations. 8086 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) { 8087 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS)) 8088 // Don't do this for crazy integer types. 8089 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) { 8090 // If this is efficiently invertible, canonicalize the LHSC/RHSC values 8091 // so that TrueC (the true value) is larger than FalseC. 8092 bool NeedsCondInvert = false; 8093 8094 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) && 8095 // Efficiently invertible. 8096 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible. 8097 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible. 8098 isa<ConstantSDNode>(Cond.getOperand(1))))) { 8099 NeedsCondInvert = true; 8100 std::swap(TrueC, FalseC); 8101 } 8102 8103 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0. 8104 if (FalseC->getAPIntValue() == 0 && 8105 TrueC->getAPIntValue().isPowerOf2()) { 8106 if (NeedsCondInvert) // Invert the condition if needed. 8107 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, 8108 DAG.getConstant(1, Cond.getValueType())); 8109 8110 // Zero extend the condition if needed. 8111 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond); 8112 8113 unsigned ShAmt = TrueC->getAPIntValue().logBase2(); 8114 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond, 8115 DAG.getConstant(ShAmt, MVT::i8)); 8116 } 8117 8118 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. 8119 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { 8120 if (NeedsCondInvert) // Invert the condition if needed. 8121 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, 8122 DAG.getConstant(1, Cond.getValueType())); 8123 8124 // Zero extend the condition if needed. 8125 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, 8126 FalseC->getValueType(0), Cond); 8127 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, 8128 SDValue(FalseC, 0)); 8129 } 8130 8131 // Optimize cases that will turn into an LEA instruction. This requires 8132 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). 8133 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { 8134 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); 8135 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; 8136 8137 bool isFastMultiplier = false; 8138 if (Diff < 10) { 8139 switch ((unsigned char)Diff) { 8140 default: break; 8141 case 1: // result = add base, cond 8142 case 2: // result = lea base( , cond*2) 8143 case 3: // result = lea base(cond, cond*2) 8144 case 4: // result = lea base( , cond*4) 8145 case 5: // result = lea base(cond, cond*4) 8146 case 8: // result = lea base( , cond*8) 8147 case 9: // result = lea base(cond, cond*8) 8148 isFastMultiplier = true; 8149 break; 8150 } 8151 } 8152 8153 if (isFastMultiplier) { 8154 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); 8155 if (NeedsCondInvert) // Invert the condition if needed. 8156 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, 8157 DAG.getConstant(1, Cond.getValueType())); 8158 8159 // Zero extend the condition if needed. 8160 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), 8161 Cond); 8162 // Scale the condition by the difference. 8163 if (Diff != 1) 8164 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, 8165 DAG.getConstant(Diff, Cond.getValueType())); 8166 8167 // Add the base if non-zero. 8168 if (FalseC->getAPIntValue() != 0) 8169 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, 8170 SDValue(FalseC, 0)); 8171 return Cond; 8172 } 8173 } 8174 } 8175 } 8176 8177 return SDValue(); 8178 } 8179 8180 /// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL] 8181 static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG, 8182 TargetLowering::DAGCombinerInfo &DCI) { 8183 DebugLoc DL = N->getDebugLoc(); 8184 8185 // If the flag operand isn't dead, don't touch this CMOV. 8186 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty()) 8187 return SDValue(); 8188 8189 // If this is a select between two integer constants, try to do some 8190 // optimizations. Note that the operands are ordered the opposite of SELECT 8191 // operands. 8192 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 8193 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) { 8194 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is 8195 // larger than FalseC (the false value). 8196 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2); 8197 8198 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) { 8199 CC = X86::GetOppositeBranchCondition(CC); 8200 std::swap(TrueC, FalseC); 8201 } 8202 8203 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0. 8204 // This is efficient for any integer data type (including i8/i16) and 8205 // shift amount. 8206 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) { 8207 SDValue Cond = N->getOperand(3); 8208 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, 8209 DAG.getConstant(CC, MVT::i8), Cond); 8210 8211 // Zero extend the condition if needed. 8212 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond); 8213 8214 unsigned ShAmt = TrueC->getAPIntValue().logBase2(); 8215 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond, 8216 DAG.getConstant(ShAmt, MVT::i8)); 8217 if (N->getNumValues() == 2) // Dead flag value? 8218 return DCI.CombineTo(N, Cond, SDValue()); 8219 return Cond; 8220 } 8221 8222 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient 8223 // for any integer data type, including i8/i16. 8224 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { 8225 SDValue Cond = N->getOperand(3); 8226 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, 8227 DAG.getConstant(CC, MVT::i8), Cond); 8228 8229 // Zero extend the condition if needed. 8230 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, 8231 FalseC->getValueType(0), Cond); 8232 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, 8233 SDValue(FalseC, 0)); 8234 8235 if (N->getNumValues() == 2) // Dead flag value? 8236 return DCI.CombineTo(N, Cond, SDValue()); 8237 return Cond; 8238 } 8239 8240 // Optimize cases that will turn into an LEA instruction. This requires 8241 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). 8242 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { 8243 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); 8244 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; 8245 8246 bool isFastMultiplier = false; 8247 if (Diff < 10) { 8248 switch ((unsigned char)Diff) { 8249 default: break; 8250 case 1: // result = add base, cond 8251 case 2: // result = lea base( , cond*2) 8252 case 3: // result = lea base(cond, cond*2) 8253 case 4: // result = lea base( , cond*4) 8254 case 5: // result = lea base(cond, cond*4) 8255 case 8: // result = lea base( , cond*8) 8256 case 9: // result = lea base(cond, cond*8) 8257 isFastMultiplier = true; 8258 break; 8259 } 8260 } 8261 8262 if (isFastMultiplier) { 8263 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); 8264 SDValue Cond = N->getOperand(3); 8265 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, 8266 DAG.getConstant(CC, MVT::i8), Cond); 8267 // Zero extend the condition if needed. 8268 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), 8269 Cond); 8270 // Scale the condition by the difference. 8271 if (Diff != 1) 8272 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, 8273 DAG.getConstant(Diff, Cond.getValueType())); 8274 8275 // Add the base if non-zero. 8276 if (FalseC->getAPIntValue() != 0) 8277 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, 8278 SDValue(FalseC, 0)); 8279 if (N->getNumValues() == 2) // Dead flag value? 8280 return DCI.CombineTo(N, Cond, SDValue()); 8281 return Cond; 8282 } 8283 } 8284 } 8285 } 8286 return SDValue(); 8287 } 8288 8289 8290 /// PerformMulCombine - Optimize a single multiply with constant into two 8291 /// in order to implement it with two cheaper instructions, e.g. 8292 /// LEA + SHL, LEA + LEA. 8293 static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG, 8294 TargetLowering::DAGCombinerInfo &DCI) { 8295 if (DAG.getMachineFunction(). 8296 getFunction()->hasFnAttr(Attribute::OptimizeForSize)) 8297 return SDValue(); 8298 8299 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 8300 return SDValue(); 8301 8302 MVT VT = N->getValueType(0); 8303 if (VT != MVT::i64) 8304 return SDValue(); 8305 8306 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8307 if (!C) 8308 return SDValue(); 8309 uint64_t MulAmt = C->getZExtValue(); 8310 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9) 8311 return SDValue(); 8312 8313 uint64_t MulAmt1 = 0; 8314 uint64_t MulAmt2 = 0; 8315 if ((MulAmt % 9) == 0) { 8316 MulAmt1 = 9; 8317 MulAmt2 = MulAmt / 9; 8318 } else if ((MulAmt % 5) == 0) { 8319 MulAmt1 = 5; 8320 MulAmt2 = MulAmt / 5; 8321 } else if ((MulAmt % 3) == 0) { 8322 MulAmt1 = 3; 8323 MulAmt2 = MulAmt / 3; 8324 } 8325 if (MulAmt2 && 8326 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){ 8327 DebugLoc DL = N->getDebugLoc(); 8328 8329 if (isPowerOf2_64(MulAmt2) && 8330 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD)) 8331 // If second multiplifer is pow2, issue it first. We want the multiply by 8332 // 3, 5, or 9 to be folded into the addressing mode unless the lone use 8333 // is an add. 8334 std::swap(MulAmt1, MulAmt2); 8335 8336 SDValue NewMul; 8337 if (isPowerOf2_64(MulAmt1)) 8338 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), 8339 DAG.getConstant(Log2_64(MulAmt1), MVT::i8)); 8340 else 8341 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), 8342 DAG.getConstant(MulAmt1, VT)); 8343 8344 if (isPowerOf2_64(MulAmt2)) 8345 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul, 8346 DAG.getConstant(Log2_64(MulAmt2), MVT::i8)); 8347 else 8348 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul, 8349 DAG.getConstant(MulAmt2, VT)); 8350 8351 // Do not add new nodes to DAG combiner worklist. 8352 DCI.CombineTo(N, NewMul, false); 8353 } 8354 return SDValue(); 8355 } 8356 8357 8358 /// PerformShiftCombine - Transforms vector shift nodes to use vector shifts 8359 /// when possible. 8360 static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG, 8361 const X86Subtarget *Subtarget) { 8362 // On X86 with SSE2 support, we can transform this to a vector shift if 8363 // all elements are shifted by the same amount. We can't do this in legalize 8364 // because the a constant vector is typically transformed to a constant pool 8365 // so we have no knowledge of the shift amount. 8366 if (!Subtarget->hasSSE2()) 8367 return SDValue(); 8368 8369 MVT VT = N->getValueType(0); 8370 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16) 8371 return SDValue(); 8372 8373 SDValue ShAmtOp = N->getOperand(1); 8374 MVT EltVT = VT.getVectorElementType(); 8375 DebugLoc DL = N->getDebugLoc(); 8376 SDValue BaseShAmt; 8377 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) { 8378 unsigned NumElts = VT.getVectorNumElements(); 8379 unsigned i = 0; 8380 for (; i != NumElts; ++i) { 8381 SDValue Arg = ShAmtOp.getOperand(i); 8382 if (Arg.getOpcode() == ISD::UNDEF) continue; 8383 BaseShAmt = Arg; 8384 break; 8385 } 8386 for (; i != NumElts; ++i) { 8387 SDValue Arg = ShAmtOp.getOperand(i); 8388 if (Arg.getOpcode() == ISD::UNDEF) continue; 8389 if (Arg != BaseShAmt) { 8390 return SDValue(); 8391 } 8392 } 8393 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE && 8394 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) { 8395 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp, 8396 DAG.getIntPtrConstant(0)); 8397 } else 8398 return SDValue(); 8399 8400 if (EltVT.bitsGT(MVT::i32)) 8401 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt); 8402 else if (EltVT.bitsLT(MVT::i32)) 8403 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt); 8404 8405 // The shift amount is identical so we can do a vector shift. 8406 SDValue ValOp = N->getOperand(0); 8407 switch (N->getOpcode()) { 8408 default: 8409 llvm_unreachable("Unknown shift opcode!"); 8410 break; 8411 case ISD::SHL: 8412 if (VT == MVT::v2i64) 8413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8414 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), 8415 ValOp, BaseShAmt); 8416 if (VT == MVT::v4i32) 8417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8418 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32), 8419 ValOp, BaseShAmt); 8420 if (VT == MVT::v8i16) 8421 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8422 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), 8423 ValOp, BaseShAmt); 8424 break; 8425 case ISD::SRA: 8426 if (VT == MVT::v4i32) 8427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8428 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32), 8429 ValOp, BaseShAmt); 8430 if (VT == MVT::v8i16) 8431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8432 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32), 8433 ValOp, BaseShAmt); 8434 break; 8435 case ISD::SRL: 8436 if (VT == MVT::v2i64) 8437 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8438 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), 8439 ValOp, BaseShAmt); 8440 if (VT == MVT::v4i32) 8441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8442 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32), 8443 ValOp, BaseShAmt); 8444 if (VT == MVT::v8i16) 8445 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, 8446 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32), 8447 ValOp, BaseShAmt); 8448 break; 8449 } 8450 return SDValue(); 8451 } 8452 8453 /// PerformSTORECombine - Do target-specific dag combines on STORE nodes. 8454 static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG, 8455 const X86Subtarget *Subtarget) { 8456 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering 8457 // the FP state in cases where an emms may be missing. 8458 // A preferable solution to the general problem is to figure out the right 8459 // places to insert EMMS. This qualifies as a quick hack. 8460 8461 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode. 8462 StoreSDNode *St = cast<StoreSDNode>(N); 8463 MVT VT = St->getValue().getValueType(); 8464 if (VT.getSizeInBits() != 64) 8465 return SDValue(); 8466 8467 const Function *F = DAG.getMachineFunction().getFunction(); 8468 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat); 8469 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps 8470 && Subtarget->hasSSE2(); 8471 if ((VT.isVector() || 8472 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) && 8473 isa<LoadSDNode>(St->getValue()) && 8474 !cast<LoadSDNode>(St->getValue())->isVolatile() && 8475 St->getChain().hasOneUse() && !St->isVolatile()) { 8476 SDNode* LdVal = St->getValue().getNode(); 8477 LoadSDNode *Ld = 0; 8478 int TokenFactorIndex = -1; 8479 SmallVector<SDValue, 8> Ops; 8480 SDNode* ChainVal = St->getChain().getNode(); 8481 // Must be a store of a load. We currently handle two cases: the load 8482 // is a direct child, and it's under an intervening TokenFactor. It is 8483 // possible to dig deeper under nested TokenFactors. 8484 if (ChainVal == LdVal) 8485 Ld = cast<LoadSDNode>(St->getChain()); 8486 else if (St->getValue().hasOneUse() && 8487 ChainVal->getOpcode() == ISD::TokenFactor) { 8488 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) { 8489 if (ChainVal->getOperand(i).getNode() == LdVal) { 8490 TokenFactorIndex = i; 8491 Ld = cast<LoadSDNode>(St->getValue()); 8492 } else 8493 Ops.push_back(ChainVal->getOperand(i)); 8494 } 8495 } 8496 8497 if (!Ld || !ISD::isNormalLoad(Ld)) 8498 return SDValue(); 8499 8500 // If this is not the MMX case, i.e. we are just turning i64 load/store 8501 // into f64 load/store, avoid the transformation if there are multiple 8502 // uses of the loaded value. 8503 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0)) 8504 return SDValue(); 8505 8506 DebugLoc LdDL = Ld->getDebugLoc(); 8507 DebugLoc StDL = N->getDebugLoc(); 8508 // If we are a 64-bit capable x86, lower to a single movq load/store pair. 8509 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store 8510 // pair instead. 8511 if (Subtarget->is64Bit() || F64IsLegal) { 8512 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64; 8513 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), 8514 Ld->getBasePtr(), Ld->getSrcValue(), 8515 Ld->getSrcValueOffset(), Ld->isVolatile(), 8516 Ld->getAlignment()); 8517 SDValue NewChain = NewLd.getValue(1); 8518 if (TokenFactorIndex != -1) { 8519 Ops.push_back(NewChain); 8520 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], 8521 Ops.size()); 8522 } 8523 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(), 8524 St->getSrcValue(), St->getSrcValueOffset(), 8525 St->isVolatile(), St->getAlignment()); 8526 } 8527 8528 // Otherwise, lower to two pairs of 32-bit loads / stores. 8529 SDValue LoAddr = Ld->getBasePtr(); 8530 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr, 8531 DAG.getConstant(4, MVT::i32)); 8532 8533 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr, 8534 Ld->getSrcValue(), Ld->getSrcValueOffset(), 8535 Ld->isVolatile(), Ld->getAlignment()); 8536 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr, 8537 Ld->getSrcValue(), Ld->getSrcValueOffset()+4, 8538 Ld->isVolatile(), 8539 MinAlign(Ld->getAlignment(), 4)); 8540 8541 SDValue NewChain = LoLd.getValue(1); 8542 if (TokenFactorIndex != -1) { 8543 Ops.push_back(LoLd); 8544 Ops.push_back(HiLd); 8545 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], 8546 Ops.size()); 8547 } 8548 8549 LoAddr = St->getBasePtr(); 8550 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr, 8551 DAG.getConstant(4, MVT::i32)); 8552 8553 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr, 8554 St->getSrcValue(), St->getSrcValueOffset(), 8555 St->isVolatile(), St->getAlignment()); 8556 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr, 8557 St->getSrcValue(), 8558 St->getSrcValueOffset() + 4, 8559 St->isVolatile(), 8560 MinAlign(St->getAlignment(), 4)); 8561 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt); 8562 } 8563 return SDValue(); 8564 } 8565 8566 /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and 8567 /// X86ISD::FXOR nodes. 8568 static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) { 8569 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR); 8570 // F[X]OR(0.0, x) -> x 8571 // F[X]OR(x, 0.0) -> x 8572 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) 8573 if (C->getValueAPF().isPosZero()) 8574 return N->getOperand(1); 8575 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) 8576 if (C->getValueAPF().isPosZero()) 8577 return N->getOperand(0); 8578 return SDValue(); 8579 } 8580 8581 /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes. 8582 static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) { 8583 // FAND(0.0, x) -> 0.0 8584 // FAND(x, 0.0) -> 0.0 8585 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) 8586 if (C->getValueAPF().isPosZero()) 8587 return N->getOperand(0); 8588 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) 8589 if (C->getValueAPF().isPosZero()) 8590 return N->getOperand(1); 8591 return SDValue(); 8592 } 8593 8594 static SDValue PerformBTCombine(SDNode *N, 8595 SelectionDAG &DAG, 8596 TargetLowering::DAGCombinerInfo &DCI) { 8597 // BT ignores high bits in the bit index operand. 8598 SDValue Op1 = N->getOperand(1); 8599 if (Op1.hasOneUse()) { 8600 unsigned BitWidth = Op1.getValueSizeInBits(); 8601 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth)); 8602 APInt KnownZero, KnownOne; 8603 TargetLowering::TargetLoweringOpt TLO(DAG); 8604 TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8605 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) || 8606 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO)) 8607 DCI.CommitTargetLoweringOpt(TLO); 8608 } 8609 return SDValue(); 8610 } 8611 8612 static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) { 8613 SDValue Op = N->getOperand(0); 8614 if (Op.getOpcode() == ISD::BIT_CONVERT) 8615 Op = Op.getOperand(0); 8616 MVT VT = N->getValueType(0), OpVT = Op.getValueType(); 8617 if (Op.getOpcode() == X86ISD::VZEXT_LOAD && 8618 VT.getVectorElementType().getSizeInBits() == 8619 OpVT.getVectorElementType().getSizeInBits()) { 8620 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op); 8621 } 8622 return SDValue(); 8623 } 8624 8625 // On X86 and X86-64, atomic operations are lowered to locked instructions. 8626 // Locked instructions, in turn, have implicit fence semantics (all memory 8627 // operations are flushed before issuing the locked instruction, and the 8628 // are not buffered), so we can fold away the common pattern of 8629 // fence-atomic-fence. 8630 static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) { 8631 SDValue atomic = N->getOperand(0); 8632 switch (atomic.getOpcode()) { 8633 case ISD::ATOMIC_CMP_SWAP: 8634 case ISD::ATOMIC_SWAP: 8635 case ISD::ATOMIC_LOAD_ADD: 8636 case ISD::ATOMIC_LOAD_SUB: 8637 case ISD::ATOMIC_LOAD_AND: 8638 case ISD::ATOMIC_LOAD_OR: 8639 case ISD::ATOMIC_LOAD_XOR: 8640 case ISD::ATOMIC_LOAD_NAND: 8641 case ISD::ATOMIC_LOAD_MIN: 8642 case ISD::ATOMIC_LOAD_MAX: 8643 case ISD::ATOMIC_LOAD_UMIN: 8644 case ISD::ATOMIC_LOAD_UMAX: 8645 break; 8646 default: 8647 return SDValue(); 8648 } 8649 8650 SDValue fence = atomic.getOperand(0); 8651 if (fence.getOpcode() != ISD::MEMBARRIER) 8652 return SDValue(); 8653 8654 switch (atomic.getOpcode()) { 8655 case ISD::ATOMIC_CMP_SWAP: 8656 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0), 8657 atomic.getOperand(1), atomic.getOperand(2), 8658 atomic.getOperand(3)); 8659 case ISD::ATOMIC_SWAP: 8660 case ISD::ATOMIC_LOAD_ADD: 8661 case ISD::ATOMIC_LOAD_SUB: 8662 case ISD::ATOMIC_LOAD_AND: 8663 case ISD::ATOMIC_LOAD_OR: 8664 case ISD::ATOMIC_LOAD_XOR: 8665 case ISD::ATOMIC_LOAD_NAND: 8666 case ISD::ATOMIC_LOAD_MIN: 8667 case ISD::ATOMIC_LOAD_MAX: 8668 case ISD::ATOMIC_LOAD_UMIN: 8669 case ISD::ATOMIC_LOAD_UMAX: 8670 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0), 8671 atomic.getOperand(1), atomic.getOperand(2)); 8672 default: 8673 return SDValue(); 8674 } 8675 } 8676 8677 SDValue X86TargetLowering::PerformDAGCombine(SDNode *N, 8678 DAGCombinerInfo &DCI) const { 8679 SelectionDAG &DAG = DCI.DAG; 8680 switch (N->getOpcode()) { 8681 default: break; 8682 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this); 8683 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget); 8684 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI); 8685 case ISD::MUL: return PerformMulCombine(N, DAG, DCI); 8686 case ISD::SHL: 8687 case ISD::SRA: 8688 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget); 8689 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget); 8690 case X86ISD::FXOR: 8691 case X86ISD::FOR: return PerformFORCombine(N, DAG); 8692 case X86ISD::FAND: return PerformFANDCombine(N, DAG); 8693 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI); 8694 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG); 8695 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG); 8696 } 8697 8698 return SDValue(); 8699 } 8700 8701 //===----------------------------------------------------------------------===// 8702 // X86 Inline Assembly Support 8703 //===----------------------------------------------------------------------===// 8704 8705 static bool LowerToBSwap(CallInst *CI) { 8706 // FIXME: this should verify that we are targetting a 486 or better. If not, 8707 // we will turn this bswap into something that will be lowered to logical ops 8708 // instead of emitting the bswap asm. For now, we don't support 486 or lower 8709 // so don't worry about this. 8710 8711 // Verify this is a simple bswap. 8712 if (CI->getNumOperands() != 2 || 8713 CI->getType() != CI->getOperand(1)->getType() || 8714 !CI->getType()->isInteger()) 8715 return false; 8716 8717 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 8718 if (!Ty || Ty->getBitWidth() % 16 != 0) 8719 return false; 8720 8721 // Okay, we can do this xform, do so now. 8722 const Type *Tys[] = { Ty }; 8723 Module *M = CI->getParent()->getParent()->getParent(); 8724 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); 8725 8726 Value *Op = CI->getOperand(1); 8727 Op = CallInst::Create(Int, Op, CI->getName(), CI); 8728 8729 CI->replaceAllUsesWith(Op); 8730 CI->eraseFromParent(); 8731 return true; 8732 } 8733 8734 bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const { 8735 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 8736 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); 8737 8738 std::string AsmStr = IA->getAsmString(); 8739 8740 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a" 8741 std::vector<std::string> AsmPieces; 8742 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator? 8743 8744 switch (AsmPieces.size()) { 8745 default: return false; 8746 case 1: 8747 AsmStr = AsmPieces[0]; 8748 AsmPieces.clear(); 8749 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace. 8750 8751 // bswap $0 8752 if (AsmPieces.size() == 2 && 8753 (AsmPieces[0] == "bswap" || 8754 AsmPieces[0] == "bswapq" || 8755 AsmPieces[0] == "bswapl") && 8756 (AsmPieces[1] == "$0" || 8757 AsmPieces[1] == "${0:q}")) { 8758 // No need to check constraints, nothing other than the equivalent of 8759 // "=r,0" would be valid here. 8760 return LowerToBSwap(CI); 8761 } 8762 // rorw $$8, ${0:w} --> llvm.bswap.i16 8763 if (CI->getType() == Type::Int16Ty && 8764 AsmPieces.size() == 3 && 8765 AsmPieces[0] == "rorw" && 8766 AsmPieces[1] == "$$8," && 8767 AsmPieces[2] == "${0:w}" && 8768 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") { 8769 return LowerToBSwap(CI); 8770 } 8771 break; 8772 case 3: 8773 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 && 8774 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && 8775 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { 8776 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 8777 std::vector<std::string> Words; 8778 SplitString(AsmPieces[0], Words, " \t"); 8779 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") { 8780 Words.clear(); 8781 SplitString(AsmPieces[1], Words, " \t"); 8782 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") { 8783 Words.clear(); 8784 SplitString(AsmPieces[2], Words, " \t,"); 8785 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" && 8786 Words[2] == "%edx") { 8787 return LowerToBSwap(CI); 8788 } 8789 } 8790 } 8791 } 8792 break; 8793 } 8794 return false; 8795 } 8796 8797 8798 8799 /// getConstraintType - Given a constraint letter, return the type of 8800 /// constraint it is for this target. 8801 X86TargetLowering::ConstraintType 8802 X86TargetLowering::getConstraintType(const std::string &Constraint) const { 8803 if (Constraint.size() == 1) { 8804 switch (Constraint[0]) { 8805 case 'A': 8806 return C_Register; 8807 case 'f': 8808 case 'r': 8809 case 'R': 8810 case 'l': 8811 case 'q': 8812 case 'Q': 8813 case 'x': 8814 case 'y': 8815 case 'Y': 8816 return C_RegisterClass; 8817 case 'e': 8818 case 'Z': 8819 return C_Other; 8820 default: 8821 break; 8822 } 8823 } 8824 return TargetLowering::getConstraintType(Constraint); 8825 } 8826 8827 /// LowerXConstraint - try to replace an X constraint, which matches anything, 8828 /// with another that has more specific requirements based on the type of the 8829 /// corresponding operand. 8830 const char *X86TargetLowering:: 8831 LowerXConstraint(MVT ConstraintVT) const { 8832 // FP X constraints get lowered to SSE1/2 registers if available, otherwise 8833 // 'f' like normal targets. 8834 if (ConstraintVT.isFloatingPoint()) { 8835 if (Subtarget->hasSSE2()) 8836 return "Y"; 8837 if (Subtarget->hasSSE1()) 8838 return "x"; 8839 } 8840 8841 return TargetLowering::LowerXConstraint(ConstraintVT); 8842 } 8843 8844 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 8845 /// vector. If it is invalid, don't add anything to Ops. 8846 void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op, 8847 char Constraint, 8848 bool hasMemory, 8849 std::vector<SDValue>&Ops, 8850 SelectionDAG &DAG) const { 8851 SDValue Result(0, 0); 8852 8853 switch (Constraint) { 8854 default: break; 8855 case 'I': 8856 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 8857 if (C->getZExtValue() <= 31) { 8858 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); 8859 break; 8860 } 8861 } 8862 return; 8863 case 'J': 8864 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 8865 if (C->getZExtValue() <= 63) { 8866 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); 8867 break; 8868 } 8869 } 8870 return; 8871 case 'K': 8872 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 8873 if ((int8_t)C->getSExtValue() == C->getSExtValue()) { 8874 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); 8875 break; 8876 } 8877 } 8878 return; 8879 case 'N': 8880 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 8881 if (C->getZExtValue() <= 255) { 8882 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); 8883 break; 8884 } 8885 } 8886 return; 8887 case 'e': { 8888 // 32-bit signed value 8889 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 8890 const ConstantInt *CI = C->getConstantIntValue(); 8891 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) { 8892 // Widen to 64 bits here to get it sign extended. 8893 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64); 8894 break; 8895 } 8896 // FIXME gcc accepts some relocatable values here too, but only in certain 8897 // memory models; it's complicated. 8898 } 8899 return; 8900 } 8901 case 'Z': { 8902 // 32-bit unsigned value 8903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 8904 const ConstantInt *CI = C->getConstantIntValue(); 8905 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) { 8906 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); 8907 break; 8908 } 8909 } 8910 // FIXME gcc accepts some relocatable values here too, but only in certain 8911 // memory models; it's complicated. 8912 return; 8913 } 8914 case 'i': { 8915 // Literal immediates are always ok. 8916 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { 8917 // Widen to 64 bits here to get it sign extended. 8918 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64); 8919 break; 8920 } 8921 8922 // If we are in non-pic codegen mode, we allow the address of a global (with 8923 // an optional displacement) to be used with 'i'. 8924 GlobalAddressSDNode *GA = 0; 8925 int64_t Offset = 0; 8926 8927 // Match either (GA), (GA+C), (GA+C1+C2), etc. 8928 while (1) { 8929 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) { 8930 Offset += GA->getOffset(); 8931 break; 8932 } else if (Op.getOpcode() == ISD::ADD) { 8933 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 8934 Offset += C->getZExtValue(); 8935 Op = Op.getOperand(0); 8936 continue; 8937 } 8938 } else if (Op.getOpcode() == ISD::SUB) { 8939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 8940 Offset += -C->getZExtValue(); 8941 Op = Op.getOperand(0); 8942 continue; 8943 } 8944 } 8945 8946 // Otherwise, this isn't something we can handle, reject it. 8947 return; 8948 } 8949 8950 GlobalValue *GV = GA->getGlobal(); 8951 // If we require an extra load to get this address, as in PIC mode, we 8952 // can't accept it. 8953 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV, 8954 getTargetMachine()))) 8955 return; 8956 8957 if (hasMemory) 8958 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG); 8959 else 8960 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset); 8961 Result = Op; 8962 break; 8963 } 8964 } 8965 8966 if (Result.getNode()) { 8967 Ops.push_back(Result); 8968 return; 8969 } 8970 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory, 8971 Ops, DAG); 8972 } 8973 8974 std::vector<unsigned> X86TargetLowering:: 8975 getRegClassForInlineAsmConstraint(const std::string &Constraint, 8976 MVT VT) const { 8977 if (Constraint.size() == 1) { 8978 // FIXME: not handling fp-stack yet! 8979 switch (Constraint[0]) { // GCC X86 Constraint Letters 8980 default: break; // Unknown constraint letter 8981 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode. 8982 if (Subtarget->is64Bit()) { 8983 if (VT == MVT::i32) 8984 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 8985 X86::ESI, X86::EDI, X86::R8D, X86::R9D, 8986 X86::R10D,X86::R11D,X86::R12D, 8987 X86::R13D,X86::R14D,X86::R15D, 8988 X86::EBP, X86::ESP, 0); 8989 else if (VT == MVT::i16) 8990 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 8991 X86::SI, X86::DI, X86::R8W,X86::R9W, 8992 X86::R10W,X86::R11W,X86::R12W, 8993 X86::R13W,X86::R14W,X86::R15W, 8994 X86::BP, X86::SP, 0); 8995 else if (VT == MVT::i8) 8996 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 8997 X86::SIL, X86::DIL, X86::R8B,X86::R9B, 8998 X86::R10B,X86::R11B,X86::R12B, 8999 X86::R13B,X86::R14B,X86::R15B, 9000 X86::BPL, X86::SPL, 0); 9001 9002 else if (VT == MVT::i64) 9003 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 9004 X86::RSI, X86::RDI, X86::R8, X86::R9, 9005 X86::R10, X86::R11, X86::R12, 9006 X86::R13, X86::R14, X86::R15, 9007 X86::RBP, X86::RSP, 0); 9008 9009 break; 9010 } 9011 // 32-bit fallthrough 9012 case 'Q': // Q_REGS 9013 if (VT == MVT::i32) 9014 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); 9015 else if (VT == MVT::i16) 9016 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); 9017 else if (VT == MVT::i8) 9018 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0); 9019 else if (VT == MVT::i64) 9020 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0); 9021 break; 9022 } 9023 } 9024 9025 return std::vector<unsigned>(); 9026 } 9027 9028 std::pair<unsigned, const TargetRegisterClass*> 9029 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 9030 MVT VT) const { 9031 // First, see if this is a constraint that directly corresponds to an LLVM 9032 // register class. 9033 if (Constraint.size() == 1) { 9034 // GCC Constraint Letters 9035 switch (Constraint[0]) { 9036 default: break; 9037 case 'r': // GENERAL_REGS 9038 case 'R': // LEGACY_REGS 9039 case 'l': // INDEX_REGS 9040 if (VT == MVT::i8) 9041 return std::make_pair(0U, X86::GR8RegisterClass); 9042 if (VT == MVT::i16) 9043 return std::make_pair(0U, X86::GR16RegisterClass); 9044 if (VT == MVT::i32 || !Subtarget->is64Bit()) 9045 return std::make_pair(0U, X86::GR32RegisterClass); 9046 return std::make_pair(0U, X86::GR64RegisterClass); 9047 case 'f': // FP Stack registers. 9048 // If SSE is enabled for this VT, use f80 to ensure the isel moves the 9049 // value to the correct fpstack register class. 9050 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT)) 9051 return std::make_pair(0U, X86::RFP32RegisterClass); 9052 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT)) 9053 return std::make_pair(0U, X86::RFP64RegisterClass); 9054 return std::make_pair(0U, X86::RFP80RegisterClass); 9055 case 'y': // MMX_REGS if MMX allowed. 9056 if (!Subtarget->hasMMX()) break; 9057 return std::make_pair(0U, X86::VR64RegisterClass); 9058 case 'Y': // SSE_REGS if SSE2 allowed 9059 if (!Subtarget->hasSSE2()) break; 9060 // FALL THROUGH. 9061 case 'x': // SSE_REGS if SSE1 allowed 9062 if (!Subtarget->hasSSE1()) break; 9063 9064 switch (VT.getSimpleVT()) { 9065 default: break; 9066 // Scalar SSE types. 9067 case MVT::f32: 9068 case MVT::i32: 9069 return std::make_pair(0U, X86::FR32RegisterClass); 9070 case MVT::f64: 9071 case MVT::i64: 9072 return std::make_pair(0U, X86::FR64RegisterClass); 9073 // Vector types. 9074 case MVT::v16i8: 9075 case MVT::v8i16: 9076 case MVT::v4i32: 9077 case MVT::v2i64: 9078 case MVT::v4f32: 9079 case MVT::v2f64: 9080 return std::make_pair(0U, X86::VR128RegisterClass); 9081 } 9082 break; 9083 } 9084 } 9085 9086 // Use the default implementation in TargetLowering to convert the register 9087 // constraint into a member of a register class. 9088 std::pair<unsigned, const TargetRegisterClass*> Res; 9089 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 9090 9091 // Not found as a standard register? 9092 if (Res.second == 0) { 9093 // GCC calls "st(0)" just plain "st". 9094 if (StringsEqualNoCase("{st}", Constraint)) { 9095 Res.first = X86::ST0; 9096 Res.second = X86::RFP80RegisterClass; 9097 } 9098 // 'A' means EAX + EDX. 9099 if (Constraint == "A") { 9100 Res.first = X86::EAX; 9101 Res.second = X86::GR32_ADRegisterClass; 9102 } 9103 return Res; 9104 } 9105 9106 // Otherwise, check to see if this is a register class of the wrong value 9107 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to 9108 // turn into {ax},{dx}. 9109 if (Res.second->hasType(VT)) 9110 return Res; // Correct type already, nothing to do. 9111 9112 // All of the single-register GCC register classes map their values onto 9113 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we 9114 // really want an 8-bit or 32-bit register, map to the appropriate register 9115 // class and return the appropriate register. 9116 if (Res.second == X86::GR16RegisterClass) { 9117 if (VT == MVT::i8) { 9118 unsigned DestReg = 0; 9119 switch (Res.first) { 9120 default: break; 9121 case X86::AX: DestReg = X86::AL; break; 9122 case X86::DX: DestReg = X86::DL; break; 9123 case X86::CX: DestReg = X86::CL; break; 9124 case X86::BX: DestReg = X86::BL; break; 9125 } 9126 if (DestReg) { 9127 Res.first = DestReg; 9128 Res.second = X86::GR8RegisterClass; 9129 } 9130 } else if (VT == MVT::i32) { 9131 unsigned DestReg = 0; 9132 switch (Res.first) { 9133 default: break; 9134 case X86::AX: DestReg = X86::EAX; break; 9135 case X86::DX: DestReg = X86::EDX; break; 9136 case X86::CX: DestReg = X86::ECX; break; 9137 case X86::BX: DestReg = X86::EBX; break; 9138 case X86::SI: DestReg = X86::ESI; break; 9139 case X86::DI: DestReg = X86::EDI; break; 9140 case X86::BP: DestReg = X86::EBP; break; 9141 case X86::SP: DestReg = X86::ESP; break; 9142 } 9143 if (DestReg) { 9144 Res.first = DestReg; 9145 Res.second = X86::GR32RegisterClass; 9146 } 9147 } else if (VT == MVT::i64) { 9148 unsigned DestReg = 0; 9149 switch (Res.first) { 9150 default: break; 9151 case X86::AX: DestReg = X86::RAX; break; 9152 case X86::DX: DestReg = X86::RDX; break; 9153 case X86::CX: DestReg = X86::RCX; break; 9154 case X86::BX: DestReg = X86::RBX; break; 9155 case X86::SI: DestReg = X86::RSI; break; 9156 case X86::DI: DestReg = X86::RDI; break; 9157 case X86::BP: DestReg = X86::RBP; break; 9158 case X86::SP: DestReg = X86::RSP; break; 9159 } 9160 if (DestReg) { 9161 Res.first = DestReg; 9162 Res.second = X86::GR64RegisterClass; 9163 } 9164 } 9165 } else if (Res.second == X86::FR32RegisterClass || 9166 Res.second == X86::FR64RegisterClass || 9167 Res.second == X86::VR128RegisterClass) { 9168 // Handle references to XMM physical registers that got mapped into the 9169 // wrong class. This can happen with constraints like {xmm0} where the 9170 // target independent register mapper will just pick the first match it can 9171 // find, ignoring the required type. 9172 if (VT == MVT::f32) 9173 Res.second = X86::FR32RegisterClass; 9174 else if (VT == MVT::f64) 9175 Res.second = X86::FR64RegisterClass; 9176 else if (X86::VR128RegisterClass->hasType(VT)) 9177 Res.second = X86::VR128RegisterClass; 9178 } 9179 9180 return Res; 9181 } 9182 9183 //===----------------------------------------------------------------------===// 9184 // X86 Widen vector type 9185 //===----------------------------------------------------------------------===// 9186 9187 /// getWidenVectorType: given a vector type, returns the type to widen 9188 /// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself. 9189 /// If there is no vector type that we want to widen to, returns MVT::Other 9190 /// When and where to widen is target dependent based on the cost of 9191 /// scalarizing vs using the wider vector type. 9192 9193 MVT X86TargetLowering::getWidenVectorType(MVT VT) const { 9194 assert(VT.isVector()); 9195 if (isTypeLegal(VT)) 9196 return VT; 9197 9198 // TODO: In computeRegisterProperty, we can compute the list of legal vector 9199 // type based on element type. This would speed up our search (though 9200 // it may not be worth it since the size of the list is relatively 9201 // small). 9202 MVT EltVT = VT.getVectorElementType(); 9203 unsigned NElts = VT.getVectorNumElements(); 9204 9205 // On X86, it make sense to widen any vector wider than 1 9206 if (NElts <= 1) 9207 return MVT::Other; 9208 9209 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE; 9210 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) { 9211 MVT SVT = (MVT::SimpleValueType)nVT; 9212 9213 if (isTypeLegal(SVT) && 9214 SVT.getVectorElementType() == EltVT && 9215 SVT.getVectorNumElements() > NElts) 9216 return SVT; 9217 } 9218 return MVT::Other; 9219 } 9220