X86ISelLowering.cpp revision 362e98a5f5cf1919ac6435ff5b630b58ecd95fc5
1//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source 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 "X86MachineFunctionInfo.h" 19#include "X86TargetMachine.h" 20#include "llvm/CallingConv.h" 21#include "llvm/Constants.h" 22#include "llvm/DerivedTypes.h" 23#include "llvm/Function.h" 24#include "llvm/Intrinsics.h" 25#include "llvm/ADT/VectorExtras.h" 26#include "llvm/Analysis/ScalarEvolutionExpressions.h" 27#include "llvm/CodeGen/CallingConvLower.h" 28#include "llvm/CodeGen/MachineFrameInfo.h" 29#include "llvm/CodeGen/MachineFunction.h" 30#include "llvm/CodeGen/MachineInstrBuilder.h" 31#include "llvm/CodeGen/SelectionDAG.h" 32#include "llvm/CodeGen/SSARegMap.h" 33#include "llvm/Support/MathExtras.h" 34#include "llvm/Target/TargetOptions.h" 35#include "llvm/Support/CommandLine.h" 36#include "llvm/ADT/StringExtras.h" 37using namespace llvm; 38 39// FIXME: temporary. 40static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, 41 cl::desc("Enable fastcc on X86")); 42X86TargetLowering::X86TargetLowering(TargetMachine &TM) 43 : TargetLowering(TM) { 44 Subtarget = &TM.getSubtarget<X86Subtarget>(); 45 X86ScalarSSE = Subtarget->hasSSE2(); 46 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; 47 48 // Set up the TargetLowering object. 49 50 // X86 is weird, it always uses i8 for shift amounts and setcc results. 51 setShiftAmountType(MVT::i8); 52 setSetCCResultType(MVT::i8); 53 setSetCCResultContents(ZeroOrOneSetCCResult); 54 setSchedulingPreference(SchedulingForRegPressure); 55 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 56 setStackPointerRegisterToSaveRestore(X86StackPtr); 57 58 if (Subtarget->isTargetDarwin()) { 59 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. 60 setUseUnderscoreSetJmp(false); 61 setUseUnderscoreLongJmp(false); 62 } else if (Subtarget->isTargetMingw()) { 63 // MS runtime is weird: it exports _setjmp, but longjmp! 64 setUseUnderscoreSetJmp(true); 65 setUseUnderscoreLongJmp(false); 66 } else { 67 setUseUnderscoreSetJmp(true); 68 setUseUnderscoreLongJmp(true); 69 } 70 71 // Add legal addressing mode scale values. 72 addLegalAddressScale(8); 73 addLegalAddressScale(4); 74 addLegalAddressScale(2); 75 // Enter the ones which require both scale + index last. These are more 76 // expensive. 77 addLegalAddressScale(9); 78 addLegalAddressScale(5); 79 addLegalAddressScale(3); 80 81 // Set up the register classes. 82 addRegisterClass(MVT::i8, X86::GR8RegisterClass); 83 addRegisterClass(MVT::i16, X86::GR16RegisterClass); 84 addRegisterClass(MVT::i32, X86::GR32RegisterClass); 85 if (Subtarget->is64Bit()) 86 addRegisterClass(MVT::i64, X86::GR64RegisterClass); 87 88 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand); 89 90 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this 91 // operation. 92 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); 93 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); 94 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); 95 96 if (Subtarget->is64Bit()) { 97 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); 98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); 99 } else { 100 if (X86ScalarSSE) 101 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP. 102 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand); 103 else 104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); 105 } 106 107 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have 108 // this operation. 109 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); 110 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); 111 // SSE has no i16 to fp conversion, only i32 112 if (X86ScalarSSE) 113 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); 114 else { 115 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); 116 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); 117 } 118 119 if (!Subtarget->is64Bit()) { 120 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode. 121 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); 122 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); 123 } 124 125 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have 126 // this operation. 127 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); 128 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); 129 130 if (X86ScalarSSE) { 131 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); 132 } else { 133 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); 134 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); 135 } 136 137 // Handle FP_TO_UINT by promoting the destination to a larger signed 138 // conversion. 139 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); 140 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); 141 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); 142 143 if (Subtarget->is64Bit()) { 144 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); 145 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); 146 } else { 147 if (X86ScalarSSE && !Subtarget->hasSSE3()) 148 // Expand FP_TO_UINT into a select. 149 // FIXME: We would like to use a Custom expander here eventually to do 150 // the optimal thing for SSE vs. the default expansion in the legalizer. 151 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); 152 else 153 // With SSE3 we can use fisttpll to convert to a signed i64. 154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); 155 } 156 157 // TODO: when we have SSE, these could be more efficient, by using movd/movq. 158 if (!X86ScalarSSE) { 159 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); 160 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); 161 } 162 163 setOperationAction(ISD::BR_JT , MVT::Other, Expand); 164 setOperationAction(ISD::BRCOND , MVT::Other, Custom); 165 setOperationAction(ISD::BR_CC , MVT::Other, Expand); 166 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); 167 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); 168 if (Subtarget->is64Bit()) 169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand); 170 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); 171 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand); 172 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); 173 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); 174 setOperationAction(ISD::FREM , MVT::f64 , Expand); 175 176 setOperationAction(ISD::CTPOP , MVT::i8 , Expand); 177 setOperationAction(ISD::CTTZ , MVT::i8 , Expand); 178 setOperationAction(ISD::CTLZ , MVT::i8 , Expand); 179 setOperationAction(ISD::CTPOP , MVT::i16 , Expand); 180 setOperationAction(ISD::CTTZ , MVT::i16 , Expand); 181 setOperationAction(ISD::CTLZ , MVT::i16 , Expand); 182 setOperationAction(ISD::CTPOP , MVT::i32 , Expand); 183 setOperationAction(ISD::CTTZ , MVT::i32 , Expand); 184 setOperationAction(ISD::CTLZ , MVT::i32 , Expand); 185 if (Subtarget->is64Bit()) { 186 setOperationAction(ISD::CTPOP , MVT::i64 , Expand); 187 setOperationAction(ISD::CTTZ , MVT::i64 , Expand); 188 setOperationAction(ISD::CTLZ , MVT::i64 , Expand); 189 } 190 191 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); 192 setOperationAction(ISD::BSWAP , MVT::i16 , Expand); 193 194 // These should be promoted to a larger select which is supported. 195 setOperationAction(ISD::SELECT , MVT::i1 , Promote); 196 setOperationAction(ISD::SELECT , MVT::i8 , Promote); 197 // X86 wants to expand cmov itself. 198 setOperationAction(ISD::SELECT , MVT::i16 , Custom); 199 setOperationAction(ISD::SELECT , MVT::i32 , Custom); 200 setOperationAction(ISD::SELECT , MVT::f32 , Custom); 201 setOperationAction(ISD::SELECT , MVT::f64 , Custom); 202 setOperationAction(ISD::SETCC , MVT::i8 , Custom); 203 setOperationAction(ISD::SETCC , MVT::i16 , Custom); 204 setOperationAction(ISD::SETCC , MVT::i32 , Custom); 205 setOperationAction(ISD::SETCC , MVT::f32 , Custom); 206 setOperationAction(ISD::SETCC , MVT::f64 , Custom); 207 if (Subtarget->is64Bit()) { 208 setOperationAction(ISD::SELECT , MVT::i64 , Custom); 209 setOperationAction(ISD::SETCC , MVT::i64 , Custom); 210 } 211 // X86 ret instruction may pop stack. 212 setOperationAction(ISD::RET , MVT::Other, Custom); 213 // Darwin ABI issue. 214 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); 215 setOperationAction(ISD::JumpTable , MVT::i32 , Custom); 216 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); 217 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); 218 if (Subtarget->is64Bit()) { 219 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); 220 setOperationAction(ISD::JumpTable , MVT::i64 , Custom); 221 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); 222 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); 223 } 224 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) 225 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); 226 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); 227 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); 228 // X86 wants to expand memset / memcpy itself. 229 setOperationAction(ISD::MEMSET , MVT::Other, Custom); 230 setOperationAction(ISD::MEMCPY , MVT::Other, Custom); 231 232 // We don't have line number support yet. 233 setOperationAction(ISD::LOCATION, MVT::Other, Expand); 234 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); 235 // FIXME - use subtarget debug flags 236 if (!Subtarget->isTargetDarwin() && 237 !Subtarget->isTargetELF() && 238 !Subtarget->isTargetCygMing()) 239 setOperationAction(ISD::LABEL, MVT::Other, Expand); 240 241 // VASTART needs to be custom lowered to use the VarArgsFrameIndex 242 setOperationAction(ISD::VASTART , MVT::Other, Custom); 243 244 // Use the default implementation. 245 setOperationAction(ISD::VAARG , MVT::Other, Expand); 246 setOperationAction(ISD::VACOPY , MVT::Other, Expand); 247 setOperationAction(ISD::VAEND , MVT::Other, Expand); 248 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 249 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 250 if (Subtarget->is64Bit()) 251 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 252 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand); 253 254 if (X86ScalarSSE) { 255 // Set up the FP register classes. 256 addRegisterClass(MVT::f32, X86::FR32RegisterClass); 257 addRegisterClass(MVT::f64, X86::FR64RegisterClass); 258 259 // Use ANDPD to simulate FABS. 260 setOperationAction(ISD::FABS , MVT::f64, Custom); 261 setOperationAction(ISD::FABS , MVT::f32, Custom); 262 263 // Use XORP to simulate FNEG. 264 setOperationAction(ISD::FNEG , MVT::f64, Custom); 265 setOperationAction(ISD::FNEG , MVT::f32, Custom); 266 267 // Use ANDPD and ORPD to simulate FCOPYSIGN. 268 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 269 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 270 271 // We don't support sin/cos/fmod 272 setOperationAction(ISD::FSIN , MVT::f64, Expand); 273 setOperationAction(ISD::FCOS , MVT::f64, Expand); 274 setOperationAction(ISD::FREM , MVT::f64, Expand); 275 setOperationAction(ISD::FSIN , MVT::f32, Expand); 276 setOperationAction(ISD::FCOS , MVT::f32, Expand); 277 setOperationAction(ISD::FREM , MVT::f32, Expand); 278 279 // Expand FP immediates into loads from the stack, except for the special 280 // cases we handle. 281 setOperationAction(ISD::ConstantFP, MVT::f64, Expand); 282 setOperationAction(ISD::ConstantFP, MVT::f32, Expand); 283 addLegalFPImmediate(+0.0); // xorps / xorpd 284 } else { 285 // Set up the FP register classes. 286 addRegisterClass(MVT::f64, X86::RFPRegisterClass); 287 288 setOperationAction(ISD::UNDEF, MVT::f64, Expand); 289 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 290 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 291 292 if (!UnsafeFPMath) { 293 setOperationAction(ISD::FSIN , MVT::f64 , Expand); 294 setOperationAction(ISD::FCOS , MVT::f64 , Expand); 295 } 296 297 setOperationAction(ISD::ConstantFP, MVT::f64, Expand); 298 addLegalFPImmediate(+0.0); // FLD0 299 addLegalFPImmediate(+1.0); // FLD1 300 addLegalFPImmediate(-0.0); // FLD0/FCHS 301 addLegalFPImmediate(-1.0); // FLD1/FCHS 302 } 303 304 // First set operation action for all vector types to expand. Then we 305 // will selectively turn on ones that can be effectively codegen'd. 306 for (unsigned VT = (unsigned)MVT::Vector + 1; 307 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) { 308 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); 309 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); 310 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand); 311 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand); 312 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); 313 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand); 314 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand); 315 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand); 316 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand); 317 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand); 318 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand); 319 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand); 320 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand); 321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand); 322 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand); 323 } 324 325 if (Subtarget->hasMMX()) { 326 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); 327 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); 328 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); 329 330 // FIXME: add MMX packed arithmetics 331 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand); 332 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand); 333 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand); 334 } 335 336 if (Subtarget->hasSSE1()) { 337 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); 338 339 setOperationAction(ISD::FADD, MVT::v4f32, Legal); 340 setOperationAction(ISD::FSUB, MVT::v4f32, Legal); 341 setOperationAction(ISD::FMUL, MVT::v4f32, Legal); 342 setOperationAction(ISD::FDIV, MVT::v4f32, Legal); 343 setOperationAction(ISD::LOAD, MVT::v4f32, Legal); 344 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 345 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); 346 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); 347 setOperationAction(ISD::SELECT, MVT::v4f32, Custom); 348 } 349 350 if (Subtarget->hasSSE2()) { 351 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); 352 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); 353 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); 354 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); 355 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); 356 357 setOperationAction(ISD::ADD, MVT::v16i8, Legal); 358 setOperationAction(ISD::ADD, MVT::v8i16, Legal); 359 setOperationAction(ISD::ADD, MVT::v4i32, Legal); 360 setOperationAction(ISD::SUB, MVT::v16i8, Legal); 361 setOperationAction(ISD::SUB, MVT::v8i16, Legal); 362 setOperationAction(ISD::SUB, MVT::v4i32, Legal); 363 setOperationAction(ISD::MUL, MVT::v8i16, Legal); 364 setOperationAction(ISD::FADD, MVT::v2f64, Legal); 365 setOperationAction(ISD::FSUB, MVT::v2f64, Legal); 366 setOperationAction(ISD::FMUL, MVT::v2f64, Legal); 367 setOperationAction(ISD::FDIV, MVT::v2f64, Legal); 368 369 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); 370 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); 371 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); 372 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); 373 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones. 374 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 375 376 // Custom lower build_vector, vector_shuffle, and extract_vector_elt. 377 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { 378 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom); 379 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom); 380 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom); 381 } 382 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); 383 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); 384 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); 385 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); 386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); 387 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); 388 389 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. 390 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { 391 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote); 392 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64); 393 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote); 394 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64); 395 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote); 396 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64); 397 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote); 398 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64); 399 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote); 400 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64); 401 } 402 403 // Custom lower v2i64 and v2f64 selects. 404 setOperationAction(ISD::LOAD, MVT::v2f64, Legal); 405 setOperationAction(ISD::LOAD, MVT::v2i64, Legal); 406 setOperationAction(ISD::SELECT, MVT::v2f64, Custom); 407 setOperationAction(ISD::SELECT, MVT::v2i64, Custom); 408 } 409 410 // We want to custom lower some of our intrinsics. 411 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 412 413 // We have target-specific dag combine patterns for the following nodes: 414 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 415 setTargetDAGCombine(ISD::SELECT); 416 417 computeRegisterProperties(); 418 419 // FIXME: These should be based on subtarget info. Plus, the values should 420 // be smaller when we are in optimizing for size mode. 421 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores 422 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores 423 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores 424 allowUnalignedMemoryAccesses = true; // x86 supports it! 425} 426 427 428//===----------------------------------------------------------------------===// 429// Return Value Calling Convention Implementation 430//===----------------------------------------------------------------------===// 431 432/// GetRetValueLocs - If we are returning a set of values with the specified 433/// value types, determine the set of registers each one will land in. This 434/// sets one element of the ResultRegs array for each element in the VTs array. 435static void GetRetValueLocs(const MVT::ValueType *VTs, unsigned NumVTs, 436 unsigned *ResultRegs, 437 const X86Subtarget *Subtarget, 438 unsigned CC) { 439 if (NumVTs == 0) return; 440 441 if (NumVTs == 2) { 442 ResultRegs[0] = VTs[0] == MVT::i64 ? X86::RAX : X86::EAX; 443 ResultRegs[1] = VTs[1] == MVT::i64 ? X86::RDX : X86::EDX; 444 return; 445 } 446 447 // Otherwise, NumVTs is 1. 448 MVT::ValueType ArgVT = VTs[0]; 449 450 unsigned Reg; 451 switch (ArgVT) { 452 case MVT::i8: Reg = X86::AL; break; 453 case MVT::i16: Reg = X86::AX; break; 454 case MVT::i32: Reg = X86::EAX; break; 455 case MVT::i64: Reg = X86::RAX; break; 456 case MVT::f32: 457 case MVT::f64: 458 if (Subtarget->is64Bit()) 459 Reg = X86::XMM0; // FP values in X86-64 go in XMM0. 460 else if (CC == CallingConv::Fast && Subtarget->hasSSE2()) 461 Reg = X86::XMM0; // FP values in X86-32 with fastcc go in XMM0. 462 else 463 Reg = X86::ST0; // FP values in X86-32 go in ST0. 464 break; 465 default: 466 assert(MVT::isVector(ArgVT) && "Unknown return value type!"); 467 Reg = X86::XMM0; // Int/FP vector result -> XMM0. 468 break; 469 } 470 ResultRegs[0] = Reg; 471} 472 473/// LowerRET - Lower an ISD::RET node. 474SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) { 475 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args"); 476 477 // Support up returning up to two registers. 478 MVT::ValueType VTs[2]; 479 unsigned DestRegs[2]; 480 unsigned NumRegs = Op.getNumOperands() / 2; 481 assert(NumRegs <= 2 && "Can only return up to two regs!"); 482 483 for (unsigned i = 0; i != NumRegs; ++i) 484 VTs[i] = Op.getOperand(i*2+1).getValueType(); 485 486 // Determine which register each value should be copied into. 487 GetRetValueLocs(VTs, NumRegs, DestRegs, Subtarget, 488 DAG.getMachineFunction().getFunction()->getCallingConv()); 489 490 // If this is the first return lowered for this function, add the regs to the 491 // liveout set for the function. 492 if (DAG.getMachineFunction().liveout_empty()) { 493 for (unsigned i = 0; i != NumRegs; ++i) 494 DAG.getMachineFunction().addLiveOut(DestRegs[i]); 495 } 496 497 SDOperand Chain = Op.getOperand(0); 498 SDOperand Flag; 499 500 // Copy the result values into the output registers. 501 if (NumRegs != 1 || DestRegs[0] != X86::ST0) { 502 for (unsigned i = 0; i != NumRegs; ++i) { 503 Chain = DAG.getCopyToReg(Chain, DestRegs[i], Op.getOperand(i*2+1), Flag); 504 Flag = Chain.getValue(1); 505 } 506 } else { 507 // We need to handle a destination of ST0 specially, because it isn't really 508 // a register. 509 SDOperand Value = Op.getOperand(1); 510 511 // If this is an FP return with ScalarSSE, we need to move the value from 512 // an XMM register onto the fp-stack. 513 if (X86ScalarSSE) { 514 SDOperand MemLoc; 515 516 // If this is a load into a scalarsse value, don't store the loaded value 517 // back to the stack, only to reload it: just replace the scalar-sse load. 518 if (ISD::isNON_EXTLoad(Value.Val) && 519 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) { 520 Chain = Value.getOperand(0); 521 MemLoc = Value.getOperand(1); 522 } else { 523 // Spill the value to memory and reload it into top of stack. 524 unsigned Size = MVT::getSizeInBits(VTs[0])/8; 525 MachineFunction &MF = DAG.getMachineFunction(); 526 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); 527 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy()); 528 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0); 529 } 530 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other); 531 SDOperand Ops[] = { Chain, MemLoc, DAG.getValueType(VTs[0]) }; 532 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3); 533 Chain = Value.getValue(1); 534 } 535 536 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 537 SDOperand Ops[] = { Chain, Value }; 538 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2); 539 Flag = Chain.getValue(1); 540 } 541 542 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16); 543 if (Flag.Val) 544 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag); 545 else 546 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop); 547} 548 549 550/// LowerCallResult - Lower the result values of an ISD::CALL into the 551/// appropriate copies out of appropriate physical registers. This assumes that 552/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call 553/// being lowered. The returns a SDNode with the same number of values as the 554/// ISD::CALL. 555SDNode *X86TargetLowering:: 556LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall, 557 unsigned CallingConv, SelectionDAG &DAG) { 558 SmallVector<SDOperand, 8> ResultVals; 559 560 // We support returning up to two registers. 561 MVT::ValueType VTs[2]; 562 unsigned DestRegs[2]; 563 unsigned NumRegs = TheCall->getNumValues() - 1; 564 assert(NumRegs <= 2 && "Can only return up to two regs!"); 565 566 for (unsigned i = 0; i != NumRegs; ++i) 567 VTs[i] = TheCall->getValueType(i); 568 569 // Determine which register each value should be copied into. 570 GetRetValueLocs(VTs, NumRegs, DestRegs, Subtarget, CallingConv); 571 572 // Copy all of the result registers out of their specified physreg. 573 if (NumRegs != 1 || DestRegs[0] != X86::ST0) { 574 for (unsigned i = 0; i != NumRegs; ++i) { 575 Chain = DAG.getCopyFromReg(Chain, DestRegs[i], VTs[i], 576 InFlag).getValue(1); 577 InFlag = Chain.getValue(2); 578 ResultVals.push_back(Chain.getValue(0)); 579 } 580 } else { 581 // Copies from the FP stack are special, as ST0 isn't a valid register 582 // before the fp stackifier runs. 583 584 // Copy ST0 into an RFP register with FP_GET_RESULT. 585 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); 586 SDOperand GROps[] = { Chain, InFlag }; 587 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2); 588 Chain = RetVal.getValue(1); 589 InFlag = RetVal.getValue(2); 590 591 // If we are using ScalarSSE, store ST(0) to the stack and reload it into 592 // an XMM register. 593 if (X86ScalarSSE) { 594 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This 595 // shouldn't be necessary except that RFP cannot be live across 596 // multiple blocks. When stackifier is fixed, they can be uncoupled. 597 MachineFunction &MF = DAG.getMachineFunction(); 598 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); 599 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 600 SDOperand Ops[] = { 601 Chain, RetVal, StackSlot, DAG.getValueType(VTs[0]), InFlag 602 }; 603 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5); 604 RetVal = DAG.getLoad(VTs[0], Chain, StackSlot, NULL, 0); 605 Chain = RetVal.getValue(1); 606 } 607 608 if (VTs[0] == MVT::f32 && !X86ScalarSSE) 609 // FIXME: we would really like to remember that this FP_ROUND 610 // operation is okay to eliminate if we allow excess FP precision. 611 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); 612 ResultVals.push_back(RetVal); 613 } 614 615 // Merge everything together with a MERGE_VALUES node. 616 ResultVals.push_back(Chain); 617 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), 618 &ResultVals[0], ResultVals.size()).Val; 619} 620 621 622//===----------------------------------------------------------------------===// 623// C & StdCall Calling Convention implementation 624//===----------------------------------------------------------------------===// 625// StdCall calling convention seems to be standard for many Windows' API 626// routines and around. It differs from C calling convention just a little: 627// callee should clean up the stack, not caller. Symbols should be also 628// decorated in some fancy way :) It doesn't support any vector arguments. 629 630/// AddLiveIn - This helper function adds the specified physical register to the 631/// MachineFunction as a live in value. It also creates a corresponding virtual 632/// register for it. 633static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, 634 const TargetRegisterClass *RC) { 635 assert(RC->contains(PReg) && "Not the correct regclass!"); 636 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); 637 MF.addLiveIn(PReg, VReg); 638 return VReg; 639} 640 641/// HowToPassArgument - Returns how an formal argument of the specified type 642/// should be passed. If it is through stack, returns the size of the stack 643/// slot; if it is through integer or XMM register, returns the number of 644/// integer or XMM registers are needed. 645static void 646HowToPassCallArgument(MVT::ValueType ObjectVT, 647 bool ArgInReg, 648 unsigned NumIntRegs, unsigned NumXMMRegs, 649 unsigned MaxNumIntRegs, 650 unsigned &ObjSize, unsigned &ObjIntRegs, 651 unsigned &ObjXMMRegs) { 652 ObjSize = 0; 653 ObjIntRegs = 0; 654 ObjXMMRegs = 0; 655 656 if (MaxNumIntRegs>3) { 657 // We don't have too much registers on ia32! :) 658 MaxNumIntRegs = 3; 659 } 660 661 switch (ObjectVT) { 662 default: assert(0 && "Unhandled argument type!"); 663 case MVT::i8: 664 if (ArgInReg && (NumIntRegs < MaxNumIntRegs)) 665 ObjIntRegs = 1; 666 else 667 ObjSize = 1; 668 break; 669 case MVT::i16: 670 if (ArgInReg && (NumIntRegs < MaxNumIntRegs)) 671 ObjIntRegs = 1; 672 else 673 ObjSize = 2; 674 break; 675 case MVT::i32: 676 if (ArgInReg && (NumIntRegs < MaxNumIntRegs)) 677 ObjIntRegs = 1; 678 else 679 ObjSize = 4; 680 break; 681 case MVT::i64: 682 if (ArgInReg && (NumIntRegs+2 <= MaxNumIntRegs)) { 683 ObjIntRegs = 2; 684 } else if (ArgInReg && (NumIntRegs+1 <= MaxNumIntRegs)) { 685 ObjIntRegs = 1; 686 ObjSize = 4; 687 } else 688 ObjSize = 8; 689 case MVT::f32: 690 ObjSize = 4; 691 break; 692 case MVT::f64: 693 ObjSize = 8; 694 break; 695 case MVT::v16i8: 696 case MVT::v8i16: 697 case MVT::v4i32: 698 case MVT::v2i64: 699 case MVT::v4f32: 700 case MVT::v2f64: 701 if (NumXMMRegs < 4) 702 ObjXMMRegs = 1; 703 else 704 ObjSize = 16; 705 break; 706 } 707} 708 709SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG, 710 bool isStdCall) { 711 unsigned NumArgs = Op.Val->getNumValues() - 1; 712 MachineFunction &MF = DAG.getMachineFunction(); 713 MachineFrameInfo *MFI = MF.getFrameInfo(); 714 SDOperand Root = Op.getOperand(0); 715 SmallVector<SDOperand, 8> ArgValues; 716 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; 717 718 // Add DAG nodes to load the arguments... On entry to a function on the X86, 719 // the stack frame looks like this: 720 // 721 // [ESP] -- return address 722 // [ESP + 4] -- first argument (leftmost lexically) 723 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size 724 // ... 725 // 726 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot 727 unsigned NumSRetBytes= 0; // How much bytes on stack used for struct return 728 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. 729 unsigned NumIntRegs = 0; // Integer regs used for parameter passing 730 731 static const unsigned XMMArgRegs[] = { 732 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 733 }; 734 static const unsigned GPRArgRegs[][3] = { 735 { X86::AL, X86::DL, X86::CL }, 736 { X86::AX, X86::DX, X86::CX }, 737 { X86::EAX, X86::EDX, X86::ECX } 738 }; 739 static const TargetRegisterClass* GPRClasses[3] = { 740 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass 741 }; 742 743 // Handle regparm attribute 744 SmallVector<bool, 8> ArgInRegs(NumArgs, false); 745 SmallVector<bool, 8> SRetArgs(NumArgs, false); 746 if (!isVarArg) { 747 for (unsigned i = 0; i<NumArgs; ++i) { 748 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue(); 749 ArgInRegs[i] = (Flags >> 1) & 1; 750 SRetArgs[i] = (Flags >> 2) & 1; 751 } 752 } 753 754 for (unsigned i = 0; i < NumArgs; ++i) { 755 MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); 756 unsigned ArgIncrement = 4; 757 unsigned ObjSize = 0; 758 unsigned ObjXMMRegs = 0; 759 unsigned ObjIntRegs = 0; 760 unsigned Reg = 0; 761 SDOperand ArgValue; 762 763 HowToPassCallArgument(ObjectVT, 764 ArgInRegs[i], 765 NumIntRegs, NumXMMRegs, 3, 766 ObjSize, ObjIntRegs, ObjXMMRegs); 767 768 if (ObjSize > 4) 769 ArgIncrement = ObjSize; 770 771 if (ObjIntRegs || ObjXMMRegs) { 772 switch (ObjectVT) { 773 default: assert(0 && "Unhandled argument type!"); 774 case MVT::i8: 775 case MVT::i16: 776 case MVT::i32: { 777 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][NumIntRegs]; 778 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]); 779 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); 780 break; 781 } 782 case MVT::v16i8: 783 case MVT::v8i16: 784 case MVT::v4i32: 785 case MVT::v2i64: 786 case MVT::v4f32: 787 case MVT::v2f64: 788 assert(!isStdCall && "Unhandled argument type!"); 789 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass); 790 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); 791 break; 792 } 793 NumIntRegs += ObjIntRegs; 794 NumXMMRegs += ObjXMMRegs; 795 } 796 if (ObjSize) { 797 // XMM arguments have to be aligned on 16-byte boundary. 798 if (ObjSize == 16) 799 ArgOffset = ((ArgOffset + 15) / 16) * 16; 800 // Create the SelectionDAG nodes corresponding to a load from this 801 // parameter. 802 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); 803 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); 804 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0); 805 806 ArgOffset += ArgIncrement; // Move on to the next argument. 807 if (SRetArgs[i]) 808 NumSRetBytes += ArgIncrement; 809 } 810 811 ArgValues.push_back(ArgValue); 812 } 813 814 ArgValues.push_back(Root); 815 816 // If the function takes variable number of arguments, make a frame index for 817 // the start of the first vararg value... for expansion of llvm.va_start. 818 if (isVarArg) 819 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); 820 821 if (isStdCall && !isVarArg) { 822 BytesToPopOnReturn = ArgOffset; // Callee pops everything.. 823 BytesCallerReserves = 0; 824 } else { 825 BytesToPopOnReturn = NumSRetBytes; // Callee pops hidden struct pointer. 826 BytesCallerReserves = ArgOffset; 827 } 828 829 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only. 830 ReturnAddrIndex = 0; // No return address slot generated yet. 831 832 833 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn); 834 835 // Return the new list of results. 836 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), 837 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); 838} 839 840SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, 841 unsigned CC) { 842 SDOperand Chain = Op.getOperand(0); 843 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; 844 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; 845 SDOperand Callee = Op.getOperand(4); 846 unsigned NumOps = (Op.getNumOperands() - 5) / 2; 847 848 static const unsigned XMMArgRegs[] = { 849 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 850 }; 851 static const unsigned GPR32ArgRegs[] = { 852 X86::EAX, X86::EDX, X86::ECX 853 }; 854 855 // Count how many bytes are to be pushed on the stack. 856 unsigned NumBytes = 0; 857 // Keep track of the number of integer regs passed so far. 858 unsigned NumIntRegs = 0; 859 // Keep track of the number of XMM regs passed so far. 860 unsigned NumXMMRegs = 0; 861 // How much bytes on stack used for struct return 862 unsigned NumSRetBytes= 0; 863 864 // Handle regparm attribute 865 SmallVector<bool, 8> ArgInRegs(NumOps, false); 866 SmallVector<bool, 8> SRetArgs(NumOps, false); 867 for (unsigned i = 0; i<NumOps; ++i) { 868 unsigned Flags = 869 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue(); 870 ArgInRegs[i] = (Flags >> 1) & 1; 871 SRetArgs[i] = (Flags >> 2) & 1; 872 } 873 874 // Calculate stack frame size 875 for (unsigned i = 0; i != NumOps; ++i) { 876 SDOperand Arg = Op.getOperand(5+2*i); 877 unsigned ArgIncrement = 4; 878 unsigned ObjSize = 0; 879 unsigned ObjIntRegs = 0; 880 unsigned ObjXMMRegs = 0; 881 882 HowToPassCallArgument(Arg.getValueType(), 883 ArgInRegs[i], 884 NumIntRegs, NumXMMRegs, 3, 885 ObjSize, ObjIntRegs, ObjXMMRegs); 886 if (ObjSize > 4) 887 ArgIncrement = ObjSize; 888 889 NumIntRegs += ObjIntRegs; 890 NumXMMRegs += ObjXMMRegs; 891 if (ObjSize) { 892 // XMM arguments have to be aligned on 16-byte boundary. 893 if (ObjSize == 16) 894 NumBytes = ((NumBytes + 15) / 16) * 16; 895 NumBytes += ArgIncrement; 896 } 897 } 898 899 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); 900 901 // Arguments go on the stack in reverse order, as specified by the ABI. 902 unsigned ArgOffset = 0; 903 NumXMMRegs = 0; 904 NumIntRegs = 0; 905 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; 906 SmallVector<SDOperand, 8> MemOpChains; 907 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy()); 908 for (unsigned i = 0; i != NumOps; ++i) { 909 SDOperand Arg = Op.getOperand(5+2*i); 910 unsigned ArgIncrement = 4; 911 unsigned ObjSize = 0; 912 unsigned ObjIntRegs = 0; 913 unsigned ObjXMMRegs = 0; 914 915 HowToPassCallArgument(Arg.getValueType(), 916 ArgInRegs[i], 917 NumIntRegs, NumXMMRegs, 3, 918 ObjSize, ObjIntRegs, ObjXMMRegs); 919 920 if (ObjSize > 4) 921 ArgIncrement = ObjSize; 922 923 if (Arg.getValueType() == MVT::i8 || Arg.getValueType() == MVT::i16) { 924 // Promote the integer to 32 bits. If the input type is signed use a 925 // sign extend, otherwise use a zero extend. 926 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue(); 927 928 unsigned ExtOp = (Flags & 1) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 929 Arg = DAG.getNode(ExtOp, MVT::i32, Arg); 930 } 931 932 if (ObjIntRegs || ObjXMMRegs) { 933 switch (Arg.getValueType()) { 934 default: assert(0 && "Unhandled argument type!"); 935 case MVT::i32: 936 RegsToPass.push_back(std::make_pair(GPR32ArgRegs[NumIntRegs], Arg)); 937 break; 938 case MVT::v16i8: 939 case MVT::v8i16: 940 case MVT::v4i32: 941 case MVT::v2i64: 942 case MVT::v4f32: 943 case MVT::v2f64: 944 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); 945 break; 946 } 947 948 NumIntRegs += ObjIntRegs; 949 NumXMMRegs += ObjXMMRegs; 950 } 951 if (ObjSize) { 952 // XMM arguments have to be aligned on 16-byte boundary. 953 if (ObjSize == 16) 954 ArgOffset = ((ArgOffset + 15) / 16) * 16; 955 956 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); 957 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); 958 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); 959 960 ArgOffset += ArgIncrement; // Move on to the next argument. 961 if (SRetArgs[i]) 962 NumSRetBytes += ArgIncrement; 963 } 964 } 965 966 // Sanity check: we haven't seen NumSRetBytes > 4 967 assert((NumSRetBytes<=4) && 968 "Too much space for struct-return pointer requested"); 969 970 if (!MemOpChains.empty()) 971 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, 972 &MemOpChains[0], MemOpChains.size()); 973 974 // Build a sequence of copy-to-reg nodes chained together with token chain 975 // and flag operands which copy the outgoing args into registers. 976 SDOperand InFlag; 977 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 978 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, 979 InFlag); 980 InFlag = Chain.getValue(1); 981 } 982 983 // ELF / PIC requires GOT in the EBX register before function calls via PLT 984 // GOT pointer. 985 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 986 Subtarget->isPICStyleGOT()) { 987 Chain = DAG.getCopyToReg(Chain, X86::EBX, 988 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), 989 InFlag); 990 InFlag = Chain.getValue(1); 991 } 992 993 // If the callee is a GlobalAddress node (quite common, every direct call is) 994 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 995 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 996 // We should use extra load for direct calls to dllimported functions in 997 // non-JIT mode. 998 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), 999 getTargetMachine(), true)) 1000 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); 1001 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) 1002 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); 1003 1004 // Returns a chain & a flag for retval copy to use. 1005 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1006 SmallVector<SDOperand, 8> Ops; 1007 Ops.push_back(Chain); 1008 Ops.push_back(Callee); 1009 1010 // Add argument registers to the end of the list so that they are known live 1011 // into the call. 1012 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1013 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1014 RegsToPass[i].second.getValueType())); 1015 1016 // Add an implicit use GOT pointer in EBX. 1017 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 1018 Subtarget->isPICStyleGOT()) 1019 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); 1020 1021 if (InFlag.Val) 1022 Ops.push_back(InFlag); 1023 1024 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, 1025 NodeTys, &Ops[0], Ops.size()); 1026 InFlag = Chain.getValue(1); 1027 1028 // Create the CALLSEQ_END node. 1029 unsigned NumBytesForCalleeToPush = 0; 1030 1031 if (CC == CallingConv::X86_StdCall) { 1032 if (isVarArg) 1033 NumBytesForCalleeToPush = NumSRetBytes; 1034 else 1035 NumBytesForCalleeToPush = NumBytes; 1036 } else { 1037 // If this is is a call to a struct-return function, the callee 1038 // pops the hidden struct pointer, so we have to push it back. 1039 // This is common for Darwin/X86, Linux & Mingw32 targets. 1040 NumBytesForCalleeToPush = NumSRetBytes; 1041 } 1042 1043 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1044 Ops.clear(); 1045 Ops.push_back(Chain); 1046 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); 1047 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy())); 1048 Ops.push_back(InFlag); 1049 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); 1050 InFlag = Chain.getValue(1); 1051 1052 // Handle result values, copying them out of physregs into vregs that we 1053 // return. 1054 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); 1055} 1056 1057 1058//===----------------------------------------------------------------------===// 1059// X86-64 C Calling Convention implementation 1060//===----------------------------------------------------------------------===// 1061 1062 1063/// X86_64_CCC_AssignArgument - Implement the X86-64 C Calling Convention. 1064static void X86_64_CCC_AssignArgument(unsigned ValNo, 1065 MVT::ValueType ArgVT, unsigned ArgFlags, 1066 CCState &State, 1067 SmallVector<CCValAssign, 16> &Locs) { 1068 MVT::ValueType LocVT = ArgVT; 1069 CCValAssign::LocInfo LocInfo = CCValAssign::Full; 1070 1071 // Promote the integer to 32 bits. If the input type is signed use a 1072 // sign extend, otherwise use a zero extend. 1073 if (ArgVT == MVT::i8 || ArgVT == MVT::i16) { 1074 LocVT = MVT::i32; 1075 LocInfo = (ArgFlags & 1) ? CCValAssign::SExt : CCValAssign::ZExt; 1076 } 1077 1078 // If this is a 32-bit value, assign to a 32-bit register if any are 1079 // available. 1080 if (LocVT == MVT::i32) { 1081 static const unsigned GPR32ArgRegs[] = { 1082 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D 1083 }; 1084 if (unsigned Reg = State.AllocateReg(GPR32ArgRegs, 6)) { 1085 Locs.push_back(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo)); 1086 return; 1087 } 1088 } 1089 1090 // If this is a 64-bit value, assign to a 64-bit register if any are 1091 // available. 1092 if (LocVT == MVT::i64) { 1093 static const unsigned GPR64ArgRegs[] = { 1094 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 1095 }; 1096 if (unsigned Reg = State.AllocateReg(GPR64ArgRegs, 6)) { 1097 Locs.push_back(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo)); 1098 return; 1099 } 1100 } 1101 1102 // If this is a FP or vector type, assign to an XMM reg if any are 1103 // available. 1104 if (MVT::isVector(LocVT) || MVT::isFloatingPoint(LocVT)) { 1105 static const unsigned XMMArgRegs[] = { 1106 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 1107 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 1108 }; 1109 if (unsigned Reg = State.AllocateReg(XMMArgRegs, 8)) { 1110 Locs.push_back(CCValAssign::getReg(ValNo, ArgVT, Reg, LocVT, LocInfo)); 1111 return; 1112 } 1113 } 1114 1115 // Integer/FP values get stored in stack slots that are 8 bytes in size and 1116 // 8-byte aligned if there are no more registers to hold them. 1117 if (LocVT == MVT::i32 || LocVT == MVT::i64 || 1118 LocVT == MVT::f32 || LocVT == MVT::f64) { 1119 unsigned Offset = State.AllocateStack(8, 8); 1120 Locs.push_back(CCValAssign::getMem(ValNo, ArgVT, Offset, LocVT, LocInfo)); 1121 return; 1122 } 1123 1124 // Vectors get 16-byte stack slots that are 16-byte aligned. 1125 if (MVT::isVector(LocVT)) { 1126 unsigned Offset = State.AllocateStack(16, 16); 1127 Locs.push_back(CCValAssign::getMem(ValNo, ArgVT, Offset, LocVT, LocInfo)); 1128 return; 1129 } 1130 assert(0 && "Unknown argument type!"); 1131} 1132 1133 1134SDOperand 1135X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) { 1136 unsigned NumArgs = Op.Val->getNumValues() - 1; 1137 MachineFunction &MF = DAG.getMachineFunction(); 1138 MachineFrameInfo *MFI = MF.getFrameInfo(); 1139 SDOperand Root = Op.getOperand(0); 1140 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; 1141 1142 static const unsigned GPR64ArgRegs[] = { 1143 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 1144 }; 1145 static const unsigned XMMArgRegs[] = { 1146 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 1147 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 1148 }; 1149 1150 SmallVector<SDOperand, 8> ArgValues; 1151 1152 1153 CCState CCInfo(*getTargetMachine().getRegisterInfo()); 1154 SmallVector<CCValAssign, 16> ArgLocs; 1155 1156 for (unsigned i = 0; i != NumArgs; ++i) { 1157 MVT::ValueType ArgVT = Op.getValue(i).getValueType(); 1158 unsigned ArgFlags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue(); 1159 X86_64_CCC_AssignArgument(i, ArgVT, ArgFlags, CCInfo, ArgLocs); 1160 } 1161 1162 unsigned LastVal = ~0U; 1163 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1164 CCValAssign &VA = ArgLocs[i]; 1165 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later 1166 // places. 1167 assert(VA.getValNo() != LastVal && 1168 "Don't support value assigned to multiple locs yet"); 1169 LastVal = VA.getValNo(); 1170 1171 if (VA.isRegLoc()) { 1172 MVT::ValueType RegVT = VA.getLocVT(); 1173 TargetRegisterClass *RC; 1174 if (RegVT == MVT::i32) 1175 RC = X86::GR32RegisterClass; 1176 else if (RegVT == MVT::i64) 1177 RC = X86::GR64RegisterClass; 1178 else if (RegVT == MVT::f32) 1179 RC = X86::FR32RegisterClass; 1180 else if (RegVT == MVT::f64) 1181 RC = X86::FR64RegisterClass; 1182 else { 1183 assert(MVT::isVector(RegVT)); 1184 RC = X86::VR128RegisterClass; 1185 } 1186 1187 SDOperand ArgValue = DAG.getCopyFromReg(Root, VA.getLocReg(), RegVT); 1188 AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC); 1189 1190 // If this is an 8 or 16-bit value, it is really passed promoted to 32 1191 // bits. Insert an assert[sz]ext to capture this, then truncate to the 1192 // right size. 1193 if (VA.getLocInfo() == CCValAssign::SExt) 1194 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue, 1195 DAG.getValueType(VA.getValVT())); 1196 else if (VA.getLocInfo() == CCValAssign::ZExt) 1197 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue, 1198 DAG.getValueType(VA.getValVT())); 1199 1200 if (VA.getLocInfo() != CCValAssign::Full) 1201 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue); 1202 1203 ArgValues.push_back(ArgValue); 1204 } else { 1205 assert(VA.isMemLoc()); 1206 1207 // Create the nodes corresponding to a load from this parameter slot. 1208 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8, 1209 VA.getLocMemOffset()); 1210 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); 1211 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0)); 1212 } 1213 } 1214 1215 unsigned StackSize = CCInfo.getNextStackOffset(); 1216 1217 // If the function takes variable number of arguments, make a frame index for 1218 // the start of the first vararg value... for expansion of llvm.va_start. 1219 if (isVarArg) { 1220 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6); 1221 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); 1222 1223 // For X86-64, if there are vararg parameters that are passed via 1224 // registers, then we must store them to their spots on the stack so they 1225 // may be loaded by deferencing the result of va_next. 1226 VarArgsGPOffset = NumIntRegs * 8; 1227 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16; 1228 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); 1229 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16); 1230 1231 // Store the integer parameter registers. 1232 SmallVector<SDOperand, 8> MemOps; 1233 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); 1234 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, 1235 DAG.getConstant(VarArgsGPOffset, getPointerTy())); 1236 for (; NumIntRegs != 6; ++NumIntRegs) { 1237 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs], 1238 X86::GR64RegisterClass); 1239 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64); 1240 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); 1241 MemOps.push_back(Store); 1242 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, 1243 DAG.getConstant(8, getPointerTy())); 1244 } 1245 1246 // Now store the XMM (fp + vector) parameter registers. 1247 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, 1248 DAG.getConstant(VarArgsFPOffset, getPointerTy())); 1249 for (; NumXMMRegs != 8; ++NumXMMRegs) { 1250 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], 1251 X86::VR128RegisterClass); 1252 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32); 1253 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); 1254 MemOps.push_back(Store); 1255 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, 1256 DAG.getConstant(16, getPointerTy())); 1257 } 1258 if (!MemOps.empty()) 1259 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, 1260 &MemOps[0], MemOps.size()); 1261 } 1262 1263 ArgValues.push_back(Root); 1264 1265 ReturnAddrIndex = 0; // No return address slot generated yet. 1266 BytesToPopOnReturn = 0; // Callee pops nothing. 1267 BytesCallerReserves = StackSize; 1268 1269 // Return the new list of results. 1270 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), 1271 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); 1272} 1273 1274SDOperand 1275X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG, 1276 unsigned CC) { 1277 SDOperand Chain = Op.getOperand(0); 1278 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; 1279 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; 1280 SDOperand Callee = Op.getOperand(4); 1281 unsigned NumOps = (Op.getNumOperands() - 5) / 2; 1282 1283 CCState CCInfo(*getTargetMachine().getRegisterInfo()); 1284 SmallVector<CCValAssign, 16> ArgLocs; 1285 1286 for (unsigned i = 0; i != NumOps; ++i) { 1287 MVT::ValueType ArgVT = Op.getOperand(5+2*i).getValueType(); 1288 unsigned ArgFlags =cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue(); 1289 X86_64_CCC_AssignArgument(i, ArgVT, ArgFlags, CCInfo, ArgLocs); 1290 } 1291 1292 // Get a count of how many bytes are to be pushed on the stack. 1293 unsigned NumBytes = CCInfo.getNextStackOffset(); 1294 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); 1295 1296 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; 1297 SmallVector<SDOperand, 8> MemOpChains; 1298 1299 SDOperand StackPtr; 1300 1301 // Walk the register/memloc assignments, inserting copies/loads. 1302 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1303 CCValAssign &VA = ArgLocs[i]; 1304 SDOperand Arg = Op.getOperand(5+2*VA.getValNo()); 1305 1306 // Promote the value if needed. 1307 switch (VA.getLocInfo()) { 1308 default: assert(0 && "Unknown loc info!"); 1309 case CCValAssign::Full: break; 1310 case CCValAssign::SExt: 1311 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg); 1312 break; 1313 case CCValAssign::ZExt: 1314 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg); 1315 break; 1316 case CCValAssign::AExt: 1317 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg); 1318 break; 1319 } 1320 1321 if (VA.isRegLoc()) { 1322 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1323 } else { 1324 assert(VA.isMemLoc()); 1325 if (StackPtr.Val == 0) 1326 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy()); 1327 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy()); 1328 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); 1329 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); 1330 } 1331 } 1332 1333 if (!MemOpChains.empty()) 1334 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, 1335 &MemOpChains[0], MemOpChains.size()); 1336 1337 // Build a sequence of copy-to-reg nodes chained together with token chain 1338 // and flag operands which copy the outgoing args into registers. 1339 SDOperand InFlag; 1340 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1341 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, 1342 InFlag); 1343 InFlag = Chain.getValue(1); 1344 } 1345 1346 if (isVarArg) { 1347 // From AMD64 ABI document: 1348 // For calls that may call functions that use varargs or stdargs 1349 // (prototype-less calls or calls to functions containing ellipsis (...) in 1350 // the declaration) %al is used as hidden argument to specify the number 1351 // of SSE registers used. The contents of %al do not need to match exactly 1352 // the number of registers, but must be an ubound on the number of SSE 1353 // registers used and is in the range 0 - 8 inclusive. 1354 1355 // Count the number of XMM registers allocated. 1356 static const unsigned XMMArgRegs[] = { 1357 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 1358 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 1359 }; 1360 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); 1361 1362 Chain = DAG.getCopyToReg(Chain, X86::AL, 1363 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); 1364 InFlag = Chain.getValue(1); 1365 } 1366 1367 // If the callee is a GlobalAddress node (quite common, every direct call is) 1368 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 1369 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1370 // We should use extra load for direct calls to dllimported functions in 1371 // non-JIT mode. 1372 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), 1373 getTargetMachine(), true)) 1374 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); 1375 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) 1376 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); 1377 1378 // Returns a chain & a flag for retval copy to use. 1379 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1380 SmallVector<SDOperand, 8> Ops; 1381 Ops.push_back(Chain); 1382 Ops.push_back(Callee); 1383 1384 // Add argument registers to the end of the list so that they are known live 1385 // into the call. 1386 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1387 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1388 RegsToPass[i].second.getValueType())); 1389 1390 if (InFlag.Val) 1391 Ops.push_back(InFlag); 1392 1393 // FIXME: Do not generate X86ISD::TAILCALL for now. 1394 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, 1395 NodeTys, &Ops[0], Ops.size()); 1396 InFlag = Chain.getValue(1); 1397 1398 // Returns a flag for retval copy to use. 1399 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1400 Ops.clear(); 1401 Ops.push_back(Chain); 1402 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); 1403 Ops.push_back(DAG.getConstant(0, getPointerTy())); 1404 Ops.push_back(InFlag); 1405 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); 1406 InFlag = Chain.getValue(1); 1407 1408 // Handle result values, copying them out of physregs into vregs that we 1409 // return. 1410 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); 1411} 1412 1413//===----------------------------------------------------------------------===// 1414// Fast & FastCall Calling Convention implementation 1415//===----------------------------------------------------------------------===// 1416// 1417// The X86 'fast' calling convention passes up to two integer arguments in 1418// registers (an appropriate portion of EAX/EDX), passes arguments in C order, 1419// and requires that the callee pop its arguments off the stack (allowing proper 1420// tail calls), and has the same return value conventions as C calling convs. 1421// 1422// This calling convention always arranges for the callee pop value to be 8n+4 1423// bytes, which is needed for tail recursion elimination and stack alignment 1424// reasons. 1425// 1426// Note that this can be enhanced in the future to pass fp vals in registers 1427// (when we have a global fp allocator) and do other tricks. 1428// 1429//===----------------------------------------------------------------------===// 1430// The X86 'fastcall' calling convention passes up to two integer arguments in 1431// registers (an appropriate portion of ECX/EDX), passes arguments in C order, 1432// and requires that the callee pop its arguments off the stack (allowing proper 1433// tail calls), and has the same return value conventions as C calling convs. 1434// 1435// This calling convention always arranges for the callee pop value to be 8n+4 1436// bytes, which is needed for tail recursion elimination and stack alignment 1437// reasons. 1438SDOperand 1439X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG, 1440 bool isFastCall) { 1441 unsigned NumArgs = Op.Val->getNumValues()-1; 1442 MachineFunction &MF = DAG.getMachineFunction(); 1443 MachineFrameInfo *MFI = MF.getFrameInfo(); 1444 SDOperand Root = Op.getOperand(0); 1445 SmallVector<SDOperand, 8> ArgValues; 1446 1447 // Add DAG nodes to load the arguments... On entry to a function the stack 1448 // frame looks like this: 1449 // 1450 // [ESP] -- return address 1451 // [ESP + 4] -- first nonreg argument (leftmost lexically) 1452 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size 1453 // ... 1454 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot 1455 1456 // Keep track of the number of integer regs passed so far. This can be either 1457 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX 1458 // are both used). 1459 unsigned NumIntRegs = 0; 1460 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. 1461 1462 static const unsigned XMMArgRegs[] = { 1463 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 1464 }; 1465 1466 static const unsigned GPRArgRegs[][2][2] = { 1467 {{ X86::AL, X86::DL }, { X86::CL, X86::DL }}, 1468 {{ X86::AX, X86::DX }, { X86::CX, X86::DX }}, 1469 {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }} 1470 }; 1471 1472 static const TargetRegisterClass* GPRClasses[3] = { 1473 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass 1474 }; 1475 1476 unsigned GPRInd = (isFastCall ? 1 : 0); 1477 for (unsigned i = 0; i < NumArgs; ++i) { 1478 MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); 1479 unsigned ArgIncrement = 4; 1480 unsigned ObjSize = 0; 1481 unsigned ObjXMMRegs = 0; 1482 unsigned ObjIntRegs = 0; 1483 unsigned Reg = 0; 1484 SDOperand ArgValue; 1485 1486 HowToPassCallArgument(ObjectVT, 1487 true, // Use as much registers as possible 1488 NumIntRegs, NumXMMRegs, 1489 (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS), 1490 ObjSize, ObjIntRegs, ObjXMMRegs); 1491 1492 if (ObjSize > 4) 1493 ArgIncrement = ObjSize; 1494 1495 if (ObjIntRegs || ObjXMMRegs) { 1496 switch (ObjectVT) { 1497 default: assert(0 && "Unhandled argument type!"); 1498 case MVT::i8: 1499 case MVT::i16: 1500 case MVT::i32: { 1501 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][GPRInd][NumIntRegs]; 1502 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]); 1503 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); 1504 break; 1505 } 1506 case MVT::v16i8: 1507 case MVT::v8i16: 1508 case MVT::v4i32: 1509 case MVT::v2i64: 1510 case MVT::v4f32: 1511 case MVT::v2f64: { 1512 assert(!isFastCall && "Unhandled argument type!"); 1513 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass); 1514 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); 1515 break; 1516 } 1517 } 1518 NumIntRegs += ObjIntRegs; 1519 NumXMMRegs += ObjXMMRegs; 1520 } 1521 if (ObjSize) { 1522 // XMM arguments have to be aligned on 16-byte boundary. 1523 if (ObjSize == 16) 1524 ArgOffset = ((ArgOffset + 15) / 16) * 16; 1525 // Create the SelectionDAG nodes corresponding to a load from this 1526 // parameter. 1527 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); 1528 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); 1529 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0); 1530 1531 ArgOffset += ArgIncrement; // Move on to the next argument. 1532 } 1533 1534 ArgValues.push_back(ArgValue); 1535 } 1536 1537 ArgValues.push_back(Root); 1538 1539 // Make sure the instruction takes 8n+4 bytes to make sure the start of the 1540 // arguments and the arguments after the retaddr has been pushed are aligned. 1541 if ((ArgOffset & 7) == 0) 1542 ArgOffset += 4; 1543 1544 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. 1545 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only. 1546 ReturnAddrIndex = 0; // No return address slot generated yet. 1547 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. 1548 BytesCallerReserves = 0; 1549 1550 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn); 1551 1552 // Finally, inform the code generator which regs we return values in. 1553 switch (getValueType(MF.getFunction()->getReturnType())) { 1554 default: assert(0 && "Unknown type!"); 1555 case MVT::isVoid: break; 1556 case MVT::i1: 1557 case MVT::i8: 1558 case MVT::i16: 1559 case MVT::i32: 1560 MF.addLiveOut(X86::EAX); 1561 break; 1562 case MVT::i64: 1563 MF.addLiveOut(X86::EAX); 1564 MF.addLiveOut(X86::EDX); 1565 break; 1566 case MVT::f32: 1567 case MVT::f64: 1568 MF.addLiveOut(X86::ST0); 1569 break; 1570 case MVT::v16i8: 1571 case MVT::v8i16: 1572 case MVT::v4i32: 1573 case MVT::v2i64: 1574 case MVT::v4f32: 1575 case MVT::v2f64: 1576 assert(!isFastCall && "Unknown result type"); 1577 MF.addLiveOut(X86::XMM0); 1578 break; 1579 } 1580 1581 // Return the new list of results. 1582 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), 1583 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); 1584} 1585 1586SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG, 1587 unsigned CC) { 1588 SDOperand Chain = Op.getOperand(0); 1589 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; 1590 SDOperand Callee = Op.getOperand(4); 1591 unsigned NumOps = (Op.getNumOperands() - 5) / 2; 1592 1593 // Count how many bytes are to be pushed on the stack. 1594 unsigned NumBytes = 0; 1595 1596 // Keep track of the number of integer regs passed so far. This can be either 1597 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX 1598 // are both used). 1599 unsigned NumIntRegs = 0; 1600 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. 1601 1602 static const unsigned GPRArgRegs[][2][2] = { 1603 {{ X86::AL, X86::DL }, { X86::CL, X86::DL }}, 1604 {{ X86::AX, X86::DX }, { X86::CX, X86::DX }}, 1605 {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }} 1606 }; 1607 static const unsigned XMMArgRegs[] = { 1608 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 1609 }; 1610 1611 bool isFastCall = CC == CallingConv::X86_FastCall; 1612 unsigned GPRInd = isFastCall ? 1 : 0; 1613 for (unsigned i = 0; i != NumOps; ++i) { 1614 SDOperand Arg = Op.getOperand(5+2*i); 1615 1616 switch (Arg.getValueType()) { 1617 default: assert(0 && "Unknown value type!"); 1618 case MVT::i8: 1619 case MVT::i16: 1620 case MVT::i32: { 1621 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS); 1622 if (NumIntRegs < MaxNumIntRegs) { 1623 ++NumIntRegs; 1624 break; 1625 } 1626 } // Fall through 1627 case MVT::f32: 1628 NumBytes += 4; 1629 break; 1630 case MVT::f64: 1631 NumBytes += 8; 1632 break; 1633 case MVT::v16i8: 1634 case MVT::v8i16: 1635 case MVT::v4i32: 1636 case MVT::v2i64: 1637 case MVT::v4f32: 1638 case MVT::v2f64: 1639 assert(!isFastCall && "Unknown value type!"); 1640 if (NumXMMRegs < 4) 1641 NumXMMRegs++; 1642 else { 1643 // XMM arguments have to be aligned on 16-byte boundary. 1644 NumBytes = ((NumBytes + 15) / 16) * 16; 1645 NumBytes += 16; 1646 } 1647 break; 1648 } 1649 } 1650 1651 // Make sure the instruction takes 8n+4 bytes to make sure the start of the 1652 // arguments and the arguments after the retaddr has been pushed are aligned. 1653 if ((NumBytes & 7) == 0) 1654 NumBytes += 4; 1655 1656 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); 1657 1658 // Arguments go on the stack in reverse order, as specified by the ABI. 1659 unsigned ArgOffset = 0; 1660 NumIntRegs = 0; 1661 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; 1662 SmallVector<SDOperand, 8> MemOpChains; 1663 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy()); 1664 for (unsigned i = 0; i != NumOps; ++i) { 1665 SDOperand Arg = Op.getOperand(5+2*i); 1666 1667 switch (Arg.getValueType()) { 1668 default: assert(0 && "Unexpected ValueType for argument!"); 1669 case MVT::i8: 1670 case MVT::i16: 1671 case MVT::i32: { 1672 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS); 1673 if (NumIntRegs < MaxNumIntRegs) { 1674 unsigned RegToUse = 1675 GPRArgRegs[Arg.getValueType()-MVT::i8][GPRInd][NumIntRegs]; 1676 RegsToPass.push_back(std::make_pair(RegToUse, Arg)); 1677 ++NumIntRegs; 1678 break; 1679 } 1680 } // Fall through 1681 case MVT::f32: { 1682 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); 1683 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); 1684 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); 1685 ArgOffset += 4; 1686 break; 1687 } 1688 case MVT::f64: { 1689 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); 1690 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); 1691 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); 1692 ArgOffset += 8; 1693 break; 1694 } 1695 case MVT::v16i8: 1696 case MVT::v8i16: 1697 case MVT::v4i32: 1698 case MVT::v2i64: 1699 case MVT::v4f32: 1700 case MVT::v2f64: 1701 assert(!isFastCall && "Unexpected ValueType for argument!"); 1702 if (NumXMMRegs < 4) { 1703 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); 1704 NumXMMRegs++; 1705 } else { 1706 // XMM arguments have to be aligned on 16-byte boundary. 1707 ArgOffset = ((ArgOffset + 15) / 16) * 16; 1708 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); 1709 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); 1710 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); 1711 ArgOffset += 16; 1712 } 1713 break; 1714 } 1715 } 1716 1717 if (!MemOpChains.empty()) 1718 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, 1719 &MemOpChains[0], MemOpChains.size()); 1720 1721 // Build a sequence of copy-to-reg nodes chained together with token chain 1722 // and flag operands which copy the outgoing args into registers. 1723 SDOperand InFlag; 1724 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1725 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, 1726 InFlag); 1727 InFlag = Chain.getValue(1); 1728 } 1729 1730 // If the callee is a GlobalAddress node (quite common, every direct call is) 1731 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 1732 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1733 // We should use extra load for direct calls to dllimported functions in 1734 // non-JIT mode. 1735 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), 1736 getTargetMachine(), true)) 1737 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); 1738 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) 1739 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); 1740 1741 // ELF / PIC requires GOT in the EBX register before function calls via PLT 1742 // GOT pointer. 1743 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 1744 Subtarget->isPICStyleGOT()) { 1745 Chain = DAG.getCopyToReg(Chain, X86::EBX, 1746 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), 1747 InFlag); 1748 InFlag = Chain.getValue(1); 1749 } 1750 1751 // Returns a chain & a flag for retval copy to use. 1752 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1753 SmallVector<SDOperand, 8> Ops; 1754 Ops.push_back(Chain); 1755 Ops.push_back(Callee); 1756 1757 // Add argument registers to the end of the list so that they are known live 1758 // into the call. 1759 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1760 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1761 RegsToPass[i].second.getValueType())); 1762 1763 // Add an implicit use GOT pointer in EBX. 1764 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 1765 Subtarget->isPICStyleGOT()) 1766 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); 1767 1768 if (InFlag.Val) 1769 Ops.push_back(InFlag); 1770 1771 // FIXME: Do not generate X86ISD::TAILCALL for now. 1772 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, 1773 NodeTys, &Ops[0], Ops.size()); 1774 InFlag = Chain.getValue(1); 1775 1776 // Returns a flag for retval copy to use. 1777 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); 1778 Ops.clear(); 1779 Ops.push_back(Chain); 1780 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); 1781 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); 1782 Ops.push_back(InFlag); 1783 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); 1784 InFlag = Chain.getValue(1); 1785 1786 // Handle result values, copying them out of physregs into vregs that we 1787 // return. 1788 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); 1789} 1790 1791SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { 1792 if (ReturnAddrIndex == 0) { 1793 // Set up a frame object for the return address. 1794 MachineFunction &MF = DAG.getMachineFunction(); 1795 if (Subtarget->is64Bit()) 1796 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8); 1797 else 1798 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); 1799 } 1800 1801 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); 1802} 1803 1804 1805 1806/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86 1807/// specific condition code. It returns a false if it cannot do a direct 1808/// translation. X86CC is the translated CondCode. LHS/RHS are modified as 1809/// needed. 1810static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP, 1811 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS, 1812 SelectionDAG &DAG) { 1813 X86CC = X86::COND_INVALID; 1814 if (!isFP) { 1815 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { 1816 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { 1817 // X > -1 -> X == 0, jump !sign. 1818 RHS = DAG.getConstant(0, RHS.getValueType()); 1819 X86CC = X86::COND_NS; 1820 return true; 1821 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { 1822 // X < 0 -> X == 0, jump on sign. 1823 X86CC = X86::COND_S; 1824 return true; 1825 } 1826 } 1827 1828 switch (SetCCOpcode) { 1829 default: break; 1830 case ISD::SETEQ: X86CC = X86::COND_E; break; 1831 case ISD::SETGT: X86CC = X86::COND_G; break; 1832 case ISD::SETGE: X86CC = X86::COND_GE; break; 1833 case ISD::SETLT: X86CC = X86::COND_L; break; 1834 case ISD::SETLE: X86CC = X86::COND_LE; break; 1835 case ISD::SETNE: X86CC = X86::COND_NE; break; 1836 case ISD::SETULT: X86CC = X86::COND_B; break; 1837 case ISD::SETUGT: X86CC = X86::COND_A; break; 1838 case ISD::SETULE: X86CC = X86::COND_BE; break; 1839 case ISD::SETUGE: X86CC = X86::COND_AE; break; 1840 } 1841 } else { 1842 // On a floating point condition, the flags are set as follows: 1843 // ZF PF CF op 1844 // 0 | 0 | 0 | X > Y 1845 // 0 | 0 | 1 | X < Y 1846 // 1 | 0 | 0 | X == Y 1847 // 1 | 1 | 1 | unordered 1848 bool Flip = false; 1849 switch (SetCCOpcode) { 1850 default: break; 1851 case ISD::SETUEQ: 1852 case ISD::SETEQ: X86CC = X86::COND_E; break; 1853 case ISD::SETOLT: Flip = true; // Fallthrough 1854 case ISD::SETOGT: 1855 case ISD::SETGT: X86CC = X86::COND_A; break; 1856 case ISD::SETOLE: Flip = true; // Fallthrough 1857 case ISD::SETOGE: 1858 case ISD::SETGE: X86CC = X86::COND_AE; break; 1859 case ISD::SETUGT: Flip = true; // Fallthrough 1860 case ISD::SETULT: 1861 case ISD::SETLT: X86CC = X86::COND_B; break; 1862 case ISD::SETUGE: Flip = true; // Fallthrough 1863 case ISD::SETULE: 1864 case ISD::SETLE: X86CC = X86::COND_BE; break; 1865 case ISD::SETONE: 1866 case ISD::SETNE: X86CC = X86::COND_NE; break; 1867 case ISD::SETUO: X86CC = X86::COND_P; break; 1868 case ISD::SETO: X86CC = X86::COND_NP; break; 1869 } 1870 if (Flip) 1871 std::swap(LHS, RHS); 1872 } 1873 1874 return X86CC != X86::COND_INVALID; 1875} 1876 1877/// hasFPCMov - is there a floating point cmov for the specific X86 condition 1878/// code. Current x86 isa includes the following FP cmov instructions: 1879/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. 1880static bool hasFPCMov(unsigned X86CC) { 1881 switch (X86CC) { 1882 default: 1883 return false; 1884 case X86::COND_B: 1885 case X86::COND_BE: 1886 case X86::COND_E: 1887 case X86::COND_P: 1888 case X86::COND_A: 1889 case X86::COND_AE: 1890 case X86::COND_NE: 1891 case X86::COND_NP: 1892 return true; 1893 } 1894} 1895 1896/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return 1897/// true if Op is undef or if its value falls within the specified range (L, H]. 1898static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) { 1899 if (Op.getOpcode() == ISD::UNDEF) 1900 return true; 1901 1902 unsigned Val = cast<ConstantSDNode>(Op)->getValue(); 1903 return (Val >= Low && Val < Hi); 1904} 1905 1906/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return 1907/// true if Op is undef or if its value equal to the specified value. 1908static bool isUndefOrEqual(SDOperand Op, unsigned Val) { 1909 if (Op.getOpcode() == ISD::UNDEF) 1910 return true; 1911 return cast<ConstantSDNode>(Op)->getValue() == Val; 1912} 1913 1914/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand 1915/// specifies a shuffle of elements that is suitable for input to PSHUFD. 1916bool X86::isPSHUFDMask(SDNode *N) { 1917 assert(N->getOpcode() == ISD::BUILD_VECTOR); 1918 1919 if (N->getNumOperands() != 4) 1920 return false; 1921 1922 // Check if the value doesn't reference the second vector. 1923 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 1924 SDOperand Arg = N->getOperand(i); 1925 if (Arg.getOpcode() == ISD::UNDEF) continue; 1926 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 1927 if (cast<ConstantSDNode>(Arg)->getValue() >= 4) 1928 return false; 1929 } 1930 1931 return true; 1932} 1933 1934/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand 1935/// specifies a shuffle of elements that is suitable for input to PSHUFHW. 1936bool X86::isPSHUFHWMask(SDNode *N) { 1937 assert(N->getOpcode() == ISD::BUILD_VECTOR); 1938 1939 if (N->getNumOperands() != 8) 1940 return false; 1941 1942 // Lower quadword copied in order. 1943 for (unsigned i = 0; i != 4; ++i) { 1944 SDOperand Arg = N->getOperand(i); 1945 if (Arg.getOpcode() == ISD::UNDEF) continue; 1946 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 1947 if (cast<ConstantSDNode>(Arg)->getValue() != i) 1948 return false; 1949 } 1950 1951 // Upper quadword shuffled. 1952 for (unsigned i = 4; i != 8; ++i) { 1953 SDOperand Arg = N->getOperand(i); 1954 if (Arg.getOpcode() == ISD::UNDEF) continue; 1955 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 1956 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 1957 if (Val < 4 || Val > 7) 1958 return false; 1959 } 1960 1961 return true; 1962} 1963 1964/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand 1965/// specifies a shuffle of elements that is suitable for input to PSHUFLW. 1966bool X86::isPSHUFLWMask(SDNode *N) { 1967 assert(N->getOpcode() == ISD::BUILD_VECTOR); 1968 1969 if (N->getNumOperands() != 8) 1970 return false; 1971 1972 // Upper quadword copied in order. 1973 for (unsigned i = 4; i != 8; ++i) 1974 if (!isUndefOrEqual(N->getOperand(i), i)) 1975 return false; 1976 1977 // Lower quadword shuffled. 1978 for (unsigned i = 0; i != 4; ++i) 1979 if (!isUndefOrInRange(N->getOperand(i), 0, 4)) 1980 return false; 1981 1982 return true; 1983} 1984 1985/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand 1986/// specifies a shuffle of elements that is suitable for input to SHUFP*. 1987static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) { 1988 if (NumElems != 2 && NumElems != 4) return false; 1989 1990 unsigned Half = NumElems / 2; 1991 for (unsigned i = 0; i < Half; ++i) 1992 if (!isUndefOrInRange(Elems[i], 0, NumElems)) 1993 return false; 1994 for (unsigned i = Half; i < NumElems; ++i) 1995 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2)) 1996 return false; 1997 1998 return true; 1999} 2000 2001bool X86::isSHUFPMask(SDNode *N) { 2002 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2003 return ::isSHUFPMask(N->op_begin(), N->getNumOperands()); 2004} 2005 2006/// isCommutedSHUFP - Returns true if the shuffle mask is except 2007/// the reverse of what x86 shuffles want. x86 shuffles requires the lower 2008/// half elements to come from vector 1 (which would equal the dest.) and 2009/// the upper half to come from vector 2. 2010static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) { 2011 if (NumOps != 2 && NumOps != 4) return false; 2012 2013 unsigned Half = NumOps / 2; 2014 for (unsigned i = 0; i < Half; ++i) 2015 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2)) 2016 return false; 2017 for (unsigned i = Half; i < NumOps; ++i) 2018 if (!isUndefOrInRange(Ops[i], 0, NumOps)) 2019 return false; 2020 return true; 2021} 2022 2023static bool isCommutedSHUFP(SDNode *N) { 2024 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2025 return isCommutedSHUFP(N->op_begin(), N->getNumOperands()); 2026} 2027 2028/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand 2029/// specifies a shuffle of elements that is suitable for input to MOVHLPS. 2030bool X86::isMOVHLPSMask(SDNode *N) { 2031 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2032 2033 if (N->getNumOperands() != 4) 2034 return false; 2035 2036 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 2037 return isUndefOrEqual(N->getOperand(0), 6) && 2038 isUndefOrEqual(N->getOperand(1), 7) && 2039 isUndefOrEqual(N->getOperand(2), 2) && 2040 isUndefOrEqual(N->getOperand(3), 3); 2041} 2042 2043/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form 2044/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, 2045/// <2, 3, 2, 3> 2046bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) { 2047 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2048 2049 if (N->getNumOperands() != 4) 2050 return false; 2051 2052 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3 2053 return isUndefOrEqual(N->getOperand(0), 2) && 2054 isUndefOrEqual(N->getOperand(1), 3) && 2055 isUndefOrEqual(N->getOperand(2), 2) && 2056 isUndefOrEqual(N->getOperand(3), 3); 2057} 2058 2059/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand 2060/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. 2061bool X86::isMOVLPMask(SDNode *N) { 2062 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2063 2064 unsigned NumElems = N->getNumOperands(); 2065 if (NumElems != 2 && NumElems != 4) 2066 return false; 2067 2068 for (unsigned i = 0; i < NumElems/2; ++i) 2069 if (!isUndefOrEqual(N->getOperand(i), i + NumElems)) 2070 return false; 2071 2072 for (unsigned i = NumElems/2; i < NumElems; ++i) 2073 if (!isUndefOrEqual(N->getOperand(i), i)) 2074 return false; 2075 2076 return true; 2077} 2078 2079/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand 2080/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} 2081/// and MOVLHPS. 2082bool X86::isMOVHPMask(SDNode *N) { 2083 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2084 2085 unsigned NumElems = N->getNumOperands(); 2086 if (NumElems != 2 && NumElems != 4) 2087 return false; 2088 2089 for (unsigned i = 0; i < NumElems/2; ++i) 2090 if (!isUndefOrEqual(N->getOperand(i), i)) 2091 return false; 2092 2093 for (unsigned i = 0; i < NumElems/2; ++i) { 2094 SDOperand Arg = N->getOperand(i + NumElems/2); 2095 if (!isUndefOrEqual(Arg, i + NumElems)) 2096 return false; 2097 } 2098 2099 return true; 2100} 2101 2102/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand 2103/// specifies a shuffle of elements that is suitable for input to UNPCKL. 2104bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts, 2105 bool V2IsSplat = false) { 2106 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) 2107 return false; 2108 2109 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) { 2110 SDOperand BitI = Elts[i]; 2111 SDOperand BitI1 = Elts[i+1]; 2112 if (!isUndefOrEqual(BitI, j)) 2113 return false; 2114 if (V2IsSplat) { 2115 if (isUndefOrEqual(BitI1, NumElts)) 2116 return false; 2117 } else { 2118 if (!isUndefOrEqual(BitI1, j + NumElts)) 2119 return false; 2120 } 2121 } 2122 2123 return true; 2124} 2125 2126bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) { 2127 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2128 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat); 2129} 2130 2131/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand 2132/// specifies a shuffle of elements that is suitable for input to UNPCKH. 2133bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts, 2134 bool V2IsSplat = false) { 2135 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) 2136 return false; 2137 2138 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) { 2139 SDOperand BitI = Elts[i]; 2140 SDOperand BitI1 = Elts[i+1]; 2141 if (!isUndefOrEqual(BitI, j + NumElts/2)) 2142 return false; 2143 if (V2IsSplat) { 2144 if (isUndefOrEqual(BitI1, NumElts)) 2145 return false; 2146 } else { 2147 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts)) 2148 return false; 2149 } 2150 } 2151 2152 return true; 2153} 2154 2155bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) { 2156 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2157 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat); 2158} 2159 2160/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form 2161/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, 2162/// <0, 0, 1, 1> 2163bool X86::isUNPCKL_v_undef_Mask(SDNode *N) { 2164 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2165 2166 unsigned NumElems = N->getNumOperands(); 2167 if (NumElems != 4 && NumElems != 8 && NumElems != 16) 2168 return false; 2169 2170 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { 2171 SDOperand BitI = N->getOperand(i); 2172 SDOperand BitI1 = N->getOperand(i+1); 2173 2174 if (!isUndefOrEqual(BitI, j)) 2175 return false; 2176 if (!isUndefOrEqual(BitI1, j)) 2177 return false; 2178 } 2179 2180 return true; 2181} 2182 2183/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand 2184/// specifies a shuffle of elements that is suitable for input to MOVSS, 2185/// MOVSD, and MOVD, i.e. setting the lowest element. 2186static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) { 2187 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) 2188 return false; 2189 2190 if (!isUndefOrEqual(Elts[0], NumElts)) 2191 return false; 2192 2193 for (unsigned i = 1; i < NumElts; ++i) { 2194 if (!isUndefOrEqual(Elts[i], i)) 2195 return false; 2196 } 2197 2198 return true; 2199} 2200 2201bool X86::isMOVLMask(SDNode *N) { 2202 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2203 return ::isMOVLMask(N->op_begin(), N->getNumOperands()); 2204} 2205 2206/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse 2207/// of what x86 movss want. X86 movs requires the lowest element to be lowest 2208/// element of vector 2 and the other elements to come from vector 1 in order. 2209static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps, 2210 bool V2IsSplat = false, 2211 bool V2IsUndef = false) { 2212 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) 2213 return false; 2214 2215 if (!isUndefOrEqual(Ops[0], 0)) 2216 return false; 2217 2218 for (unsigned i = 1; i < NumOps; ++i) { 2219 SDOperand Arg = Ops[i]; 2220 if (!(isUndefOrEqual(Arg, i+NumOps) || 2221 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) || 2222 (V2IsSplat && isUndefOrEqual(Arg, NumOps)))) 2223 return false; 2224 } 2225 2226 return true; 2227} 2228 2229static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false, 2230 bool V2IsUndef = false) { 2231 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2232 return isCommutedMOVL(N->op_begin(), N->getNumOperands(), 2233 V2IsSplat, V2IsUndef); 2234} 2235 2236/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand 2237/// specifies a shuffle of elements that is suitable for input to MOVSHDUP. 2238bool X86::isMOVSHDUPMask(SDNode *N) { 2239 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2240 2241 if (N->getNumOperands() != 4) 2242 return false; 2243 2244 // Expect 1, 1, 3, 3 2245 for (unsigned i = 0; i < 2; ++i) { 2246 SDOperand Arg = N->getOperand(i); 2247 if (Arg.getOpcode() == ISD::UNDEF) continue; 2248 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2249 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2250 if (Val != 1) return false; 2251 } 2252 2253 bool HasHi = false; 2254 for (unsigned i = 2; i < 4; ++i) { 2255 SDOperand Arg = N->getOperand(i); 2256 if (Arg.getOpcode() == ISD::UNDEF) continue; 2257 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2258 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2259 if (Val != 3) return false; 2260 HasHi = true; 2261 } 2262 2263 // Don't use movshdup if it can be done with a shufps. 2264 return HasHi; 2265} 2266 2267/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand 2268/// specifies a shuffle of elements that is suitable for input to MOVSLDUP. 2269bool X86::isMOVSLDUPMask(SDNode *N) { 2270 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2271 2272 if (N->getNumOperands() != 4) 2273 return false; 2274 2275 // Expect 0, 0, 2, 2 2276 for (unsigned i = 0; i < 2; ++i) { 2277 SDOperand Arg = N->getOperand(i); 2278 if (Arg.getOpcode() == ISD::UNDEF) continue; 2279 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2280 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2281 if (Val != 0) return false; 2282 } 2283 2284 bool HasHi = false; 2285 for (unsigned i = 2; i < 4; ++i) { 2286 SDOperand Arg = N->getOperand(i); 2287 if (Arg.getOpcode() == ISD::UNDEF) continue; 2288 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2289 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2290 if (Val != 2) return false; 2291 HasHi = true; 2292 } 2293 2294 // Don't use movshdup if it can be done with a shufps. 2295 return HasHi; 2296} 2297 2298/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies 2299/// a splat of a single element. 2300static bool isSplatMask(SDNode *N) { 2301 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2302 2303 // This is a splat operation if each element of the permute is the same, and 2304 // if the value doesn't reference the second vector. 2305 unsigned NumElems = N->getNumOperands(); 2306 SDOperand ElementBase; 2307 unsigned i = 0; 2308 for (; i != NumElems; ++i) { 2309 SDOperand Elt = N->getOperand(i); 2310 if (isa<ConstantSDNode>(Elt)) { 2311 ElementBase = Elt; 2312 break; 2313 } 2314 } 2315 2316 if (!ElementBase.Val) 2317 return false; 2318 2319 for (; i != NumElems; ++i) { 2320 SDOperand Arg = N->getOperand(i); 2321 if (Arg.getOpcode() == ISD::UNDEF) continue; 2322 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2323 if (Arg != ElementBase) return false; 2324 } 2325 2326 // Make sure it is a splat of the first vector operand. 2327 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems; 2328} 2329 2330/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies 2331/// a splat of a single element and it's a 2 or 4 element mask. 2332bool X86::isSplatMask(SDNode *N) { 2333 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2334 2335 // We can only splat 64-bit, and 32-bit quantities with a single instruction. 2336 if (N->getNumOperands() != 4 && N->getNumOperands() != 2) 2337 return false; 2338 return ::isSplatMask(N); 2339} 2340 2341/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand 2342/// specifies a splat of zero element. 2343bool X86::isSplatLoMask(SDNode *N) { 2344 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2345 2346 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) 2347 if (!isUndefOrEqual(N->getOperand(i), 0)) 2348 return false; 2349 return true; 2350} 2351 2352/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle 2353/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* 2354/// instructions. 2355unsigned X86::getShuffleSHUFImmediate(SDNode *N) { 2356 unsigned NumOperands = N->getNumOperands(); 2357 unsigned Shift = (NumOperands == 4) ? 2 : 1; 2358 unsigned Mask = 0; 2359 for (unsigned i = 0; i < NumOperands; ++i) { 2360 unsigned Val = 0; 2361 SDOperand Arg = N->getOperand(NumOperands-i-1); 2362 if (Arg.getOpcode() != ISD::UNDEF) 2363 Val = cast<ConstantSDNode>(Arg)->getValue(); 2364 if (Val >= NumOperands) Val -= NumOperands; 2365 Mask |= Val; 2366 if (i != NumOperands - 1) 2367 Mask <<= Shift; 2368 } 2369 2370 return Mask; 2371} 2372 2373/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle 2374/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW 2375/// instructions. 2376unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { 2377 unsigned Mask = 0; 2378 // 8 nodes, but we only care about the last 4. 2379 for (unsigned i = 7; i >= 4; --i) { 2380 unsigned Val = 0; 2381 SDOperand Arg = N->getOperand(i); 2382 if (Arg.getOpcode() != ISD::UNDEF) 2383 Val = cast<ConstantSDNode>(Arg)->getValue(); 2384 Mask |= (Val - 4); 2385 if (i != 4) 2386 Mask <<= 2; 2387 } 2388 2389 return Mask; 2390} 2391 2392/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle 2393/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW 2394/// instructions. 2395unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { 2396 unsigned Mask = 0; 2397 // 8 nodes, but we only care about the first 4. 2398 for (int i = 3; i >= 0; --i) { 2399 unsigned Val = 0; 2400 SDOperand Arg = N->getOperand(i); 2401 if (Arg.getOpcode() != ISD::UNDEF) 2402 Val = cast<ConstantSDNode>(Arg)->getValue(); 2403 Mask |= Val; 2404 if (i != 0) 2405 Mask <<= 2; 2406 } 2407 2408 return Mask; 2409} 2410 2411/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand 2412/// specifies a 8 element shuffle that can be broken into a pair of 2413/// PSHUFHW and PSHUFLW. 2414static bool isPSHUFHW_PSHUFLWMask(SDNode *N) { 2415 assert(N->getOpcode() == ISD::BUILD_VECTOR); 2416 2417 if (N->getNumOperands() != 8) 2418 return false; 2419 2420 // Lower quadword shuffled. 2421 for (unsigned i = 0; i != 4; ++i) { 2422 SDOperand Arg = N->getOperand(i); 2423 if (Arg.getOpcode() == ISD::UNDEF) continue; 2424 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2425 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2426 if (Val > 4) 2427 return false; 2428 } 2429 2430 // Upper quadword shuffled. 2431 for (unsigned i = 4; i != 8; ++i) { 2432 SDOperand Arg = N->getOperand(i); 2433 if (Arg.getOpcode() == ISD::UNDEF) continue; 2434 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2435 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2436 if (Val < 4 || Val > 7) 2437 return false; 2438 } 2439 2440 return true; 2441} 2442 2443/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as 2444/// values in ther permute mask. 2445static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1, 2446 SDOperand &V2, SDOperand &Mask, 2447 SelectionDAG &DAG) { 2448 MVT::ValueType VT = Op.getValueType(); 2449 MVT::ValueType MaskVT = Mask.getValueType(); 2450 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT); 2451 unsigned NumElems = Mask.getNumOperands(); 2452 SmallVector<SDOperand, 8> MaskVec; 2453 2454 for (unsigned i = 0; i != NumElems; ++i) { 2455 SDOperand Arg = Mask.getOperand(i); 2456 if (Arg.getOpcode() == ISD::UNDEF) { 2457 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT)); 2458 continue; 2459 } 2460 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); 2461 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2462 if (Val < NumElems) 2463 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT)); 2464 else 2465 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT)); 2466 } 2467 2468 std::swap(V1, V2); 2469 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); 2470 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); 2471} 2472 2473/// ShouldXformToMOVHLPS - Return true if the node should be transformed to 2474/// match movhlps. The lower half elements should come from upper half of 2475/// V1 (and in order), and the upper half elements should come from the upper 2476/// half of V2 (and in order). 2477static bool ShouldXformToMOVHLPS(SDNode *Mask) { 2478 unsigned NumElems = Mask->getNumOperands(); 2479 if (NumElems != 4) 2480 return false; 2481 for (unsigned i = 0, e = 2; i != e; ++i) 2482 if (!isUndefOrEqual(Mask->getOperand(i), i+2)) 2483 return false; 2484 for (unsigned i = 2; i != 4; ++i) 2485 if (!isUndefOrEqual(Mask->getOperand(i), i+4)) 2486 return false; 2487 return true; 2488} 2489 2490/// isScalarLoadToVector - Returns true if the node is a scalar load that 2491/// is promoted to a vector. 2492static inline bool isScalarLoadToVector(SDNode *N) { 2493 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) { 2494 N = N->getOperand(0).Val; 2495 return ISD::isNON_EXTLoad(N); 2496 } 2497 return false; 2498} 2499 2500/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to 2501/// match movlp{s|d}. The lower half elements should come from lower half of 2502/// V1 (and in order), and the upper half elements should come from the upper 2503/// half of V2 (and in order). And since V1 will become the source of the 2504/// MOVLP, it must be either a vector load or a scalar load to vector. 2505static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) { 2506 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) 2507 return false; 2508 // Is V2 is a vector load, don't do this transformation. We will try to use 2509 // load folding shufps op. 2510 if (ISD::isNON_EXTLoad(V2)) 2511 return false; 2512 2513 unsigned NumElems = Mask->getNumOperands(); 2514 if (NumElems != 2 && NumElems != 4) 2515 return false; 2516 for (unsigned i = 0, e = NumElems/2; i != e; ++i) 2517 if (!isUndefOrEqual(Mask->getOperand(i), i)) 2518 return false; 2519 for (unsigned i = NumElems/2; i != NumElems; ++i) 2520 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems)) 2521 return false; 2522 return true; 2523} 2524 2525/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are 2526/// all the same. 2527static bool isSplatVector(SDNode *N) { 2528 if (N->getOpcode() != ISD::BUILD_VECTOR) 2529 return false; 2530 2531 SDOperand SplatValue = N->getOperand(0); 2532 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) 2533 if (N->getOperand(i) != SplatValue) 2534 return false; 2535 return true; 2536} 2537 2538/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved 2539/// to an undef. 2540static bool isUndefShuffle(SDNode *N) { 2541 if (N->getOpcode() != ISD::BUILD_VECTOR) 2542 return false; 2543 2544 SDOperand V1 = N->getOperand(0); 2545 SDOperand V2 = N->getOperand(1); 2546 SDOperand Mask = N->getOperand(2); 2547 unsigned NumElems = Mask.getNumOperands(); 2548 for (unsigned i = 0; i != NumElems; ++i) { 2549 SDOperand Arg = Mask.getOperand(i); 2550 if (Arg.getOpcode() != ISD::UNDEF) { 2551 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2552 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF) 2553 return false; 2554 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF) 2555 return false; 2556 } 2557 } 2558 return true; 2559} 2560 2561/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements 2562/// that point to V2 points to its first element. 2563static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) { 2564 assert(Mask.getOpcode() == ISD::BUILD_VECTOR); 2565 2566 bool Changed = false; 2567 SmallVector<SDOperand, 8> MaskVec; 2568 unsigned NumElems = Mask.getNumOperands(); 2569 for (unsigned i = 0; i != NumElems; ++i) { 2570 SDOperand Arg = Mask.getOperand(i); 2571 if (Arg.getOpcode() != ISD::UNDEF) { 2572 unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); 2573 if (Val > NumElems) { 2574 Arg = DAG.getConstant(NumElems, Arg.getValueType()); 2575 Changed = true; 2576 } 2577 } 2578 MaskVec.push_back(Arg); 2579 } 2580 2581 if (Changed) 2582 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), 2583 &MaskVec[0], MaskVec.size()); 2584 return Mask; 2585} 2586 2587/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd 2588/// operation of specified width. 2589static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) { 2590 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 2591 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); 2592 2593 SmallVector<SDOperand, 8> MaskVec; 2594 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT)); 2595 for (unsigned i = 1; i != NumElems; ++i) 2596 MaskVec.push_back(DAG.getConstant(i, BaseVT)); 2597 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); 2598} 2599 2600/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation 2601/// of specified width. 2602static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) { 2603 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 2604 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); 2605 SmallVector<SDOperand, 8> MaskVec; 2606 for (unsigned i = 0, e = NumElems/2; i != e; ++i) { 2607 MaskVec.push_back(DAG.getConstant(i, BaseVT)); 2608 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT)); 2609 } 2610 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); 2611} 2612 2613/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation 2614/// of specified width. 2615static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) { 2616 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 2617 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); 2618 unsigned Half = NumElems/2; 2619 SmallVector<SDOperand, 8> MaskVec; 2620 for (unsigned i = 0; i != Half; ++i) { 2621 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT)); 2622 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT)); 2623 } 2624 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); 2625} 2626 2627/// getZeroVector - Returns a vector of specified type with all zero elements. 2628/// 2629static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) { 2630 assert(MVT::isVector(VT) && "Expected a vector type"); 2631 unsigned NumElems = getVectorNumElements(VT); 2632 MVT::ValueType EVT = MVT::getVectorBaseType(VT); 2633 bool isFP = MVT::isFloatingPoint(EVT); 2634 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT); 2635 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero); 2636 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size()); 2637} 2638 2639/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32. 2640/// 2641static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) { 2642 SDOperand V1 = Op.getOperand(0); 2643 SDOperand Mask = Op.getOperand(2); 2644 MVT::ValueType VT = Op.getValueType(); 2645 unsigned NumElems = Mask.getNumOperands(); 2646 Mask = getUnpacklMask(NumElems, DAG); 2647 while (NumElems != 4) { 2648 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask); 2649 NumElems >>= 1; 2650 } 2651 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1); 2652 2653 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); 2654 Mask = getZeroVector(MaskVT, DAG); 2655 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, 2656 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask); 2657 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle); 2658} 2659 2660/// isZeroNode - Returns true if Elt is a constant zero or a floating point 2661/// constant +0.0. 2662static inline bool isZeroNode(SDOperand Elt) { 2663 return ((isa<ConstantSDNode>(Elt) && 2664 cast<ConstantSDNode>(Elt)->getValue() == 0) || 2665 (isa<ConstantFPSDNode>(Elt) && 2666 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0))); 2667} 2668 2669/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified 2670/// vector and zero or undef vector. 2671static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT, 2672 unsigned NumElems, unsigned Idx, 2673 bool isZero, SelectionDAG &DAG) { 2674 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT); 2675 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 2676 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT); 2677 SDOperand Zero = DAG.getConstant(0, EVT); 2678 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero); 2679 MaskVec[Idx] = DAG.getConstant(NumElems, EVT); 2680 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 2681 &MaskVec[0], MaskVec.size()); 2682 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); 2683} 2684 2685/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. 2686/// 2687static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros, 2688 unsigned NumNonZero, unsigned NumZero, 2689 SelectionDAG &DAG, TargetLowering &TLI) { 2690 if (NumNonZero > 8) 2691 return SDOperand(); 2692 2693 SDOperand V(0, 0); 2694 bool First = true; 2695 for (unsigned i = 0; i < 16; ++i) { 2696 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; 2697 if (ThisIsNonZero && First) { 2698 if (NumZero) 2699 V = getZeroVector(MVT::v8i16, DAG); 2700 else 2701 V = DAG.getNode(ISD::UNDEF, MVT::v8i16); 2702 First = false; 2703 } 2704 2705 if ((i & 1) != 0) { 2706 SDOperand ThisElt(0, 0), LastElt(0, 0); 2707 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; 2708 if (LastIsNonZero) { 2709 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1)); 2710 } 2711 if (ThisIsNonZero) { 2712 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i)); 2713 ThisElt = DAG.getNode(ISD::SHL, MVT::i16, 2714 ThisElt, DAG.getConstant(8, MVT::i8)); 2715 if (LastIsNonZero) 2716 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt); 2717 } else 2718 ThisElt = LastElt; 2719 2720 if (ThisElt.Val) 2721 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt, 2722 DAG.getConstant(i/2, TLI.getPointerTy())); 2723 } 2724 } 2725 2726 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V); 2727} 2728 2729/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16. 2730/// 2731static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros, 2732 unsigned NumNonZero, unsigned NumZero, 2733 SelectionDAG &DAG, TargetLowering &TLI) { 2734 if (NumNonZero > 4) 2735 return SDOperand(); 2736 2737 SDOperand V(0, 0); 2738 bool First = true; 2739 for (unsigned i = 0; i < 8; ++i) { 2740 bool isNonZero = (NonZeros & (1 << i)) != 0; 2741 if (isNonZero) { 2742 if (First) { 2743 if (NumZero) 2744 V = getZeroVector(MVT::v8i16, DAG); 2745 else 2746 V = DAG.getNode(ISD::UNDEF, MVT::v8i16); 2747 First = false; 2748 } 2749 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i), 2750 DAG.getConstant(i, TLI.getPointerTy())); 2751 } 2752 } 2753 2754 return V; 2755} 2756 2757SDOperand 2758X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) { 2759 // All zero's are handled with pxor. 2760 if (ISD::isBuildVectorAllZeros(Op.Val)) 2761 return Op; 2762 2763 // All one's are handled with pcmpeqd. 2764 if (ISD::isBuildVectorAllOnes(Op.Val)) 2765 return Op; 2766 2767 MVT::ValueType VT = Op.getValueType(); 2768 MVT::ValueType EVT = MVT::getVectorBaseType(VT); 2769 unsigned EVTBits = MVT::getSizeInBits(EVT); 2770 2771 unsigned NumElems = Op.getNumOperands(); 2772 unsigned NumZero = 0; 2773 unsigned NumNonZero = 0; 2774 unsigned NonZeros = 0; 2775 std::set<SDOperand> Values; 2776 for (unsigned i = 0; i < NumElems; ++i) { 2777 SDOperand Elt = Op.getOperand(i); 2778 if (Elt.getOpcode() != ISD::UNDEF) { 2779 Values.insert(Elt); 2780 if (isZeroNode(Elt)) 2781 NumZero++; 2782 else { 2783 NonZeros |= (1 << i); 2784 NumNonZero++; 2785 } 2786 } 2787 } 2788 2789 if (NumNonZero == 0) 2790 // Must be a mix of zero and undef. Return a zero vector. 2791 return getZeroVector(VT, DAG); 2792 2793 // Splat is obviously ok. Let legalizer expand it to a shuffle. 2794 if (Values.size() == 1) 2795 return SDOperand(); 2796 2797 // Special case for single non-zero element. 2798 if (NumNonZero == 1) { 2799 unsigned Idx = CountTrailingZeros_32(NonZeros); 2800 SDOperand Item = Op.getOperand(Idx); 2801 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item); 2802 if (Idx == 0) 2803 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. 2804 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx, 2805 NumZero > 0, DAG); 2806 2807 if (EVTBits == 32) { 2808 // Turn it into a shuffle of zero and zero-extended scalar to vector. 2809 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0, 2810 DAG); 2811 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 2812 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); 2813 SmallVector<SDOperand, 8> MaskVec; 2814 for (unsigned i = 0; i < NumElems; i++) 2815 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT)); 2816 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 2817 &MaskVec[0], MaskVec.size()); 2818 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item, 2819 DAG.getNode(ISD::UNDEF, VT), Mask); 2820 } 2821 } 2822 2823 // Let legalizer expand 2-wide build_vector's. 2824 if (EVTBits == 64) 2825 return SDOperand(); 2826 2827 // If element VT is < 32 bits, convert it to inserts into a zero vector. 2828 if (EVTBits == 8) { 2829 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, 2830 *this); 2831 if (V.Val) return V; 2832 } 2833 2834 if (EVTBits == 16) { 2835 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, 2836 *this); 2837 if (V.Val) return V; 2838 } 2839 2840 // If element VT is == 32 bits, turn it into a number of shuffles. 2841 SmallVector<SDOperand, 8> V; 2842 V.resize(NumElems); 2843 if (NumElems == 4 && NumZero > 0) { 2844 for (unsigned i = 0; i < 4; ++i) { 2845 bool isZero = !(NonZeros & (1 << i)); 2846 if (isZero) 2847 V[i] = getZeroVector(VT, DAG); 2848 else 2849 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); 2850 } 2851 2852 for (unsigned i = 0; i < 2; ++i) { 2853 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { 2854 default: break; 2855 case 0: 2856 V[i] = V[i*2]; // Must be a zero vector. 2857 break; 2858 case 1: 2859 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2], 2860 getMOVLMask(NumElems, DAG)); 2861 break; 2862 case 2: 2863 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], 2864 getMOVLMask(NumElems, DAG)); 2865 break; 2866 case 3: 2867 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], 2868 getUnpacklMask(NumElems, DAG)); 2869 break; 2870 } 2871 } 2872 2873 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd) 2874 // clears the upper bits. 2875 // FIXME: we can do the same for v4f32 case when we know both parts of 2876 // the lower half come from scalar_to_vector (loadf32). We should do 2877 // that in post legalizer dag combiner with target specific hooks. 2878 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0) 2879 return V[0]; 2880 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 2881 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT); 2882 SmallVector<SDOperand, 8> MaskVec; 2883 bool Reverse = (NonZeros & 0x3) == 2; 2884 for (unsigned i = 0; i < 2; ++i) 2885 if (Reverse) 2886 MaskVec.push_back(DAG.getConstant(1-i, EVT)); 2887 else 2888 MaskVec.push_back(DAG.getConstant(i, EVT)); 2889 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; 2890 for (unsigned i = 0; i < 2; ++i) 2891 if (Reverse) 2892 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT)); 2893 else 2894 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT)); 2895 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 2896 &MaskVec[0], MaskVec.size()); 2897 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask); 2898 } 2899 2900 if (Values.size() > 2) { 2901 // Expand into a number of unpckl*. 2902 // e.g. for v4f32 2903 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> 2904 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> 2905 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> 2906 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG); 2907 for (unsigned i = 0; i < NumElems; ++i) 2908 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); 2909 NumElems >>= 1; 2910 while (NumElems != 0) { 2911 for (unsigned i = 0; i < NumElems; ++i) 2912 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems], 2913 UnpckMask); 2914 NumElems >>= 1; 2915 } 2916 return V[0]; 2917 } 2918 2919 return SDOperand(); 2920} 2921 2922SDOperand 2923X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) { 2924 SDOperand V1 = Op.getOperand(0); 2925 SDOperand V2 = Op.getOperand(1); 2926 SDOperand PermMask = Op.getOperand(2); 2927 MVT::ValueType VT = Op.getValueType(); 2928 unsigned NumElems = PermMask.getNumOperands(); 2929 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; 2930 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; 2931 bool V1IsSplat = false; 2932 bool V2IsSplat = false; 2933 2934 if (isUndefShuffle(Op.Val)) 2935 return DAG.getNode(ISD::UNDEF, VT); 2936 2937 if (isSplatMask(PermMask.Val)) { 2938 if (NumElems <= 4) return Op; 2939 // Promote it to a v4i32 splat. 2940 return PromoteSplat(Op, DAG); 2941 } 2942 2943 if (X86::isMOVLMask(PermMask.Val)) 2944 return (V1IsUndef) ? V2 : Op; 2945 2946 if (X86::isMOVSHDUPMask(PermMask.Val) || 2947 X86::isMOVSLDUPMask(PermMask.Val) || 2948 X86::isMOVHLPSMask(PermMask.Val) || 2949 X86::isMOVHPMask(PermMask.Val) || 2950 X86::isMOVLPMask(PermMask.Val)) 2951 return Op; 2952 2953 if (ShouldXformToMOVHLPS(PermMask.Val) || 2954 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val)) 2955 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); 2956 2957 bool Commuted = false; 2958 V1IsSplat = isSplatVector(V1.Val); 2959 V2IsSplat = isSplatVector(V2.Val); 2960 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { 2961 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); 2962 std::swap(V1IsSplat, V2IsSplat); 2963 std::swap(V1IsUndef, V2IsUndef); 2964 Commuted = true; 2965 } 2966 2967 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) { 2968 if (V2IsUndef) return V1; 2969 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); 2970 if (V2IsSplat) { 2971 // V2 is a splat, so the mask may be malformed. That is, it may point 2972 // to any V2 element. The instruction selectior won't like this. Get 2973 // a corrected mask and commute to form a proper MOVS{S|D}. 2974 SDOperand NewMask = getMOVLMask(NumElems, DAG); 2975 if (NewMask.Val != PermMask.Val) 2976 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); 2977 } 2978 return Op; 2979 } 2980 2981 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || 2982 X86::isUNPCKLMask(PermMask.Val) || 2983 X86::isUNPCKHMask(PermMask.Val)) 2984 return Op; 2985 2986 if (V2IsSplat) { 2987 // Normalize mask so all entries that point to V2 points to its first 2988 // element then try to match unpck{h|l} again. If match, return a 2989 // new vector_shuffle with the corrected mask. 2990 SDOperand NewMask = NormalizeMask(PermMask, DAG); 2991 if (NewMask.Val != PermMask.Val) { 2992 if (X86::isUNPCKLMask(PermMask.Val, true)) { 2993 SDOperand NewMask = getUnpacklMask(NumElems, DAG); 2994 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); 2995 } else if (X86::isUNPCKHMask(PermMask.Val, true)) { 2996 SDOperand NewMask = getUnpackhMask(NumElems, DAG); 2997 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); 2998 } 2999 } 3000 } 3001 3002 // Normalize the node to match x86 shuffle ops if needed 3003 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val)) 3004 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); 3005 3006 if (Commuted) { 3007 // Commute is back and try unpck* again. 3008 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); 3009 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || 3010 X86::isUNPCKLMask(PermMask.Val) || 3011 X86::isUNPCKHMask(PermMask.Val)) 3012 return Op; 3013 } 3014 3015 // If VT is integer, try PSHUF* first, then SHUFP*. 3016 if (MVT::isInteger(VT)) { 3017 if (X86::isPSHUFDMask(PermMask.Val) || 3018 X86::isPSHUFHWMask(PermMask.Val) || 3019 X86::isPSHUFLWMask(PermMask.Val)) { 3020 if (V2.getOpcode() != ISD::UNDEF) 3021 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, 3022 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); 3023 return Op; 3024 } 3025 3026 if (X86::isSHUFPMask(PermMask.Val)) 3027 return Op; 3028 3029 // Handle v8i16 shuffle high / low shuffle node pair. 3030 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) { 3031 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); 3032 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); 3033 SmallVector<SDOperand, 8> MaskVec; 3034 for (unsigned i = 0; i != 4; ++i) 3035 MaskVec.push_back(PermMask.getOperand(i)); 3036 for (unsigned i = 4; i != 8; ++i) 3037 MaskVec.push_back(DAG.getConstant(i, BaseVT)); 3038 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3039 &MaskVec[0], MaskVec.size()); 3040 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); 3041 MaskVec.clear(); 3042 for (unsigned i = 0; i != 4; ++i) 3043 MaskVec.push_back(DAG.getConstant(i, BaseVT)); 3044 for (unsigned i = 4; i != 8; ++i) 3045 MaskVec.push_back(PermMask.getOperand(i)); 3046 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size()); 3047 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); 3048 } 3049 } else { 3050 // Floating point cases in the other order. 3051 if (X86::isSHUFPMask(PermMask.Val)) 3052 return Op; 3053 if (X86::isPSHUFDMask(PermMask.Val) || 3054 X86::isPSHUFHWMask(PermMask.Val) || 3055 X86::isPSHUFLWMask(PermMask.Val)) { 3056 if (V2.getOpcode() != ISD::UNDEF) 3057 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, 3058 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); 3059 return Op; 3060 } 3061 } 3062 3063 if (NumElems == 4) { 3064 MVT::ValueType MaskVT = PermMask.getValueType(); 3065 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); 3066 SmallVector<std::pair<int, int>, 8> Locs; 3067 Locs.reserve(NumElems); 3068 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); 3069 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); 3070 unsigned NumHi = 0; 3071 unsigned NumLo = 0; 3072 // If no more than two elements come from either vector. This can be 3073 // implemented with two shuffles. First shuffle gather the elements. 3074 // The second shuffle, which takes the first shuffle as both of its 3075 // vector operands, put the elements into the right order. 3076 for (unsigned i = 0; i != NumElems; ++i) { 3077 SDOperand Elt = PermMask.getOperand(i); 3078 if (Elt.getOpcode() == ISD::UNDEF) { 3079 Locs[i] = std::make_pair(-1, -1); 3080 } else { 3081 unsigned Val = cast<ConstantSDNode>(Elt)->getValue(); 3082 if (Val < NumElems) { 3083 Locs[i] = std::make_pair(0, NumLo); 3084 Mask1[NumLo] = Elt; 3085 NumLo++; 3086 } else { 3087 Locs[i] = std::make_pair(1, NumHi); 3088 if (2+NumHi < NumElems) 3089 Mask1[2+NumHi] = Elt; 3090 NumHi++; 3091 } 3092 } 3093 } 3094 if (NumLo <= 2 && NumHi <= 2) { 3095 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, 3096 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3097 &Mask1[0], Mask1.size())); 3098 for (unsigned i = 0; i != NumElems; ++i) { 3099 if (Locs[i].first == -1) 3100 continue; 3101 else { 3102 unsigned Idx = (i < NumElems/2) ? 0 : NumElems; 3103 Idx += Locs[i].first * (NumElems/2) + Locs[i].second; 3104 Mask2[i] = DAG.getConstant(Idx, MaskEVT); 3105 } 3106 } 3107 3108 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, 3109 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3110 &Mask2[0], Mask2.size())); 3111 } 3112 3113 // Break it into (shuffle shuffle_hi, shuffle_lo). 3114 Locs.clear(); 3115 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); 3116 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); 3117 SmallVector<SDOperand,8> *MaskPtr = &LoMask; 3118 unsigned MaskIdx = 0; 3119 unsigned LoIdx = 0; 3120 unsigned HiIdx = NumElems/2; 3121 for (unsigned i = 0; i != NumElems; ++i) { 3122 if (i == NumElems/2) { 3123 MaskPtr = &HiMask; 3124 MaskIdx = 1; 3125 LoIdx = 0; 3126 HiIdx = NumElems/2; 3127 } 3128 SDOperand Elt = PermMask.getOperand(i); 3129 if (Elt.getOpcode() == ISD::UNDEF) { 3130 Locs[i] = std::make_pair(-1, -1); 3131 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) { 3132 Locs[i] = std::make_pair(MaskIdx, LoIdx); 3133 (*MaskPtr)[LoIdx] = Elt; 3134 LoIdx++; 3135 } else { 3136 Locs[i] = std::make_pair(MaskIdx, HiIdx); 3137 (*MaskPtr)[HiIdx] = Elt; 3138 HiIdx++; 3139 } 3140 } 3141 3142 SDOperand LoShuffle = 3143 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, 3144 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3145 &LoMask[0], LoMask.size())); 3146 SDOperand HiShuffle = 3147 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, 3148 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3149 &HiMask[0], HiMask.size())); 3150 SmallVector<SDOperand, 8> MaskOps; 3151 for (unsigned i = 0; i != NumElems; ++i) { 3152 if (Locs[i].first == -1) { 3153 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); 3154 } else { 3155 unsigned Idx = Locs[i].first * NumElems + Locs[i].second; 3156 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT)); 3157 } 3158 } 3159 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle, 3160 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3161 &MaskOps[0], MaskOps.size())); 3162 } 3163 3164 return SDOperand(); 3165} 3166 3167SDOperand 3168X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { 3169 if (!isa<ConstantSDNode>(Op.getOperand(1))) 3170 return SDOperand(); 3171 3172 MVT::ValueType VT = Op.getValueType(); 3173 // TODO: handle v16i8. 3174 if (MVT::getSizeInBits(VT) == 16) { 3175 // Transform it so it match pextrw which produces a 32-bit result. 3176 MVT::ValueType EVT = (MVT::ValueType)(VT+1); 3177 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT, 3178 Op.getOperand(0), Op.getOperand(1)); 3179 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract, 3180 DAG.getValueType(VT)); 3181 return DAG.getNode(ISD::TRUNCATE, VT, Assert); 3182 } else if (MVT::getSizeInBits(VT) == 32) { 3183 SDOperand Vec = Op.getOperand(0); 3184 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); 3185 if (Idx == 0) 3186 return Op; 3187 // SHUFPS the element to the lowest double word, then movss. 3188 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); 3189 SmallVector<SDOperand, 8> IdxVec; 3190 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT))); 3191 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); 3192 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); 3193 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); 3194 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3195 &IdxVec[0], IdxVec.size()); 3196 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), 3197 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); 3198 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, 3199 DAG.getConstant(0, getPointerTy())); 3200 } else if (MVT::getSizeInBits(VT) == 64) { 3201 SDOperand Vec = Op.getOperand(0); 3202 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); 3203 if (Idx == 0) 3204 return Op; 3205 3206 // UNPCKHPD the element to the lowest double word, then movsd. 3207 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored 3208 // to a f64mem, the whole operation is folded into a single MOVHPDmr. 3209 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); 3210 SmallVector<SDOperand, 8> IdxVec; 3211 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT))); 3212 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); 3213 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3214 &IdxVec[0], IdxVec.size()); 3215 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), 3216 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); 3217 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, 3218 DAG.getConstant(0, getPointerTy())); 3219 } 3220 3221 return SDOperand(); 3222} 3223 3224SDOperand 3225X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { 3226 // Transform it so it match pinsrw which expects a 16-bit value in a GR32 3227 // as its second argument. 3228 MVT::ValueType VT = Op.getValueType(); 3229 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT); 3230 SDOperand N0 = Op.getOperand(0); 3231 SDOperand N1 = Op.getOperand(1); 3232 SDOperand N2 = Op.getOperand(2); 3233 if (MVT::getSizeInBits(BaseVT) == 16) { 3234 if (N1.getValueType() != MVT::i32) 3235 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1); 3236 if (N2.getValueType() != MVT::i32) 3237 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32); 3238 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2); 3239 } else if (MVT::getSizeInBits(BaseVT) == 32) { 3240 unsigned Idx = cast<ConstantSDNode>(N2)->getValue(); 3241 if (Idx == 0) { 3242 // Use a movss. 3243 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1); 3244 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); 3245 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); 3246 SmallVector<SDOperand, 8> MaskVec; 3247 MaskVec.push_back(DAG.getConstant(4, BaseVT)); 3248 for (unsigned i = 1; i <= 3; ++i) 3249 MaskVec.push_back(DAG.getConstant(i, BaseVT)); 3250 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1, 3251 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, 3252 &MaskVec[0], MaskVec.size())); 3253 } else { 3254 // Use two pinsrw instructions to insert a 32 bit value. 3255 Idx <<= 1; 3256 if (MVT::isFloatingPoint(N1.getValueType())) { 3257 if (ISD::isNON_EXTLoad(N1.Val)) { 3258 // Just load directly from f32mem to GR32. 3259 LoadSDNode *LD = cast<LoadSDNode>(N1); 3260 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(), 3261 LD->getSrcValue(), LD->getSrcValueOffset()); 3262 } else { 3263 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1); 3264 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1); 3265 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1, 3266 DAG.getConstant(0, getPointerTy())); 3267 } 3268 } 3269 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0); 3270 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, 3271 DAG.getConstant(Idx, getPointerTy())); 3272 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8)); 3273 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, 3274 DAG.getConstant(Idx+1, getPointerTy())); 3275 return DAG.getNode(ISD::BIT_CONVERT, VT, N0); 3276 } 3277 } 3278 3279 return SDOperand(); 3280} 3281 3282SDOperand 3283X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) { 3284 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0)); 3285 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt); 3286} 3287 3288// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 3289// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is 3290// one of the above mentioned nodes. It has to be wrapped because otherwise 3291// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 3292// be used to form addressing mode. These wrapped nodes will be selected 3293// into MOV32ri. 3294SDOperand 3295X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) { 3296 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 3297 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(), 3298 getPointerTy(), 3299 CP->getAlignment()); 3300 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); 3301 // With PIC, the address is actually $g + Offset. 3302 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 3303 !Subtarget->isPICStyleRIPRel()) { 3304 Result = DAG.getNode(ISD::ADD, getPointerTy(), 3305 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), 3306 Result); 3307 } 3308 3309 return Result; 3310} 3311 3312SDOperand 3313X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) { 3314 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 3315 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy()); 3316 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); 3317 // With PIC, the address is actually $g + Offset. 3318 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 3319 !Subtarget->isPICStyleRIPRel()) { 3320 Result = DAG.getNode(ISD::ADD, getPointerTy(), 3321 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), 3322 Result); 3323 } 3324 3325 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to 3326 // load the value at address GV, not the value of GV itself. This means that 3327 // the GlobalAddress must be in the base or index register of the address, not 3328 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call 3329 // The same applies for external symbols during PIC codegen 3330 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false)) 3331 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0); 3332 3333 return Result; 3334} 3335 3336SDOperand 3337X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) { 3338 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); 3339 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); 3340 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); 3341 // With PIC, the address is actually $g + Offset. 3342 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 3343 !Subtarget->isPICStyleRIPRel()) { 3344 Result = DAG.getNode(ISD::ADD, getPointerTy(), 3345 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), 3346 Result); 3347 } 3348 3349 return Result; 3350} 3351 3352SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) { 3353 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 3354 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); 3355 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); 3356 // With PIC, the address is actually $g + Offset. 3357 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && 3358 !Subtarget->isPICStyleRIPRel()) { 3359 Result = DAG.getNode(ISD::ADD, getPointerTy(), 3360 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), 3361 Result); 3362 } 3363 3364 return Result; 3365} 3366 3367SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) { 3368 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 && 3369 "Not an i64 shift!"); 3370 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; 3371 SDOperand ShOpLo = Op.getOperand(0); 3372 SDOperand ShOpHi = Op.getOperand(1); 3373 SDOperand ShAmt = Op.getOperand(2); 3374 SDOperand Tmp1 = isSRA ? 3375 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) : 3376 DAG.getConstant(0, MVT::i32); 3377 3378 SDOperand Tmp2, Tmp3; 3379 if (Op.getOpcode() == ISD::SHL_PARTS) { 3380 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt); 3381 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt); 3382 } else { 3383 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt); 3384 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt); 3385 } 3386 3387 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); 3388 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt, 3389 DAG.getConstant(32, MVT::i8)); 3390 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)}; 3391 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1); 3392 3393 SDOperand Hi, Lo; 3394 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8); 3395 3396 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag); 3397 SmallVector<SDOperand, 4> Ops; 3398 if (Op.getOpcode() == ISD::SHL_PARTS) { 3399 Ops.push_back(Tmp2); 3400 Ops.push_back(Tmp3); 3401 Ops.push_back(CC); 3402 Ops.push_back(InFlag); 3403 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); 3404 InFlag = Hi.getValue(1); 3405 3406 Ops.clear(); 3407 Ops.push_back(Tmp3); 3408 Ops.push_back(Tmp1); 3409 Ops.push_back(CC); 3410 Ops.push_back(InFlag); 3411 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); 3412 } else { 3413 Ops.push_back(Tmp2); 3414 Ops.push_back(Tmp3); 3415 Ops.push_back(CC); 3416 Ops.push_back(InFlag); 3417 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); 3418 InFlag = Lo.getValue(1); 3419 3420 Ops.clear(); 3421 Ops.push_back(Tmp3); 3422 Ops.push_back(Tmp1); 3423 Ops.push_back(CC); 3424 Ops.push_back(InFlag); 3425 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); 3426 } 3427 3428 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32); 3429 Ops.clear(); 3430 Ops.push_back(Lo); 3431 Ops.push_back(Hi); 3432 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size()); 3433} 3434 3435SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) { 3436 assert(Op.getOperand(0).getValueType() <= MVT::i64 && 3437 Op.getOperand(0).getValueType() >= MVT::i16 && 3438 "Unknown SINT_TO_FP to lower!"); 3439 3440 SDOperand Result; 3441 MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); 3442 unsigned Size = MVT::getSizeInBits(SrcVT)/8; 3443 MachineFunction &MF = DAG.getMachineFunction(); 3444 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); 3445 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 3446 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0), 3447 StackSlot, NULL, 0); 3448 3449 // Build the FILD 3450 SDVTList Tys; 3451 if (X86ScalarSSE) 3452 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); 3453 else 3454 Tys = DAG.getVTList(MVT::f64, MVT::Other); 3455 SmallVector<SDOperand, 8> Ops; 3456 Ops.push_back(Chain); 3457 Ops.push_back(StackSlot); 3458 Ops.push_back(DAG.getValueType(SrcVT)); 3459 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD, 3460 Tys, &Ops[0], Ops.size()); 3461 3462 if (X86ScalarSSE) { 3463 Chain = Result.getValue(1); 3464 SDOperand InFlag = Result.getValue(2); 3465 3466 // FIXME: Currently the FST is flagged to the FILD_FLAG. This 3467 // shouldn't be necessary except that RFP cannot be live across 3468 // multiple blocks. When stackifier is fixed, they can be uncoupled. 3469 MachineFunction &MF = DAG.getMachineFunction(); 3470 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); 3471 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 3472 Tys = DAG.getVTList(MVT::Other); 3473 SmallVector<SDOperand, 8> Ops; 3474 Ops.push_back(Chain); 3475 Ops.push_back(Result); 3476 Ops.push_back(StackSlot); 3477 Ops.push_back(DAG.getValueType(Op.getValueType())); 3478 Ops.push_back(InFlag); 3479 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size()); 3480 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0); 3481 } 3482 3483 return Result; 3484} 3485 3486SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) { 3487 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && 3488 "Unknown FP_TO_SINT to lower!"); 3489 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary 3490 // stack slot. 3491 MachineFunction &MF = DAG.getMachineFunction(); 3492 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; 3493 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); 3494 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 3495 3496 unsigned Opc; 3497 switch (Op.getValueType()) { 3498 default: assert(0 && "Invalid FP_TO_SINT to lower!"); 3499 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; 3500 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; 3501 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; 3502 } 3503 3504 SDOperand Chain = DAG.getEntryNode(); 3505 SDOperand Value = Op.getOperand(0); 3506 if (X86ScalarSSE) { 3507 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); 3508 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0); 3509 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other); 3510 SDOperand Ops[] = { 3511 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType()) 3512 }; 3513 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3); 3514 Chain = Value.getValue(1); 3515 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); 3516 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); 3517 } 3518 3519 // Build the FP_TO_INT*_IN_MEM 3520 SDOperand Ops[] = { Chain, Value, StackSlot }; 3521 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3); 3522 3523 // Load the result. 3524 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0); 3525} 3526 3527SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) { 3528 MVT::ValueType VT = Op.getValueType(); 3529 const Type *OpNTy = MVT::getTypeForValueType(VT); 3530 std::vector<Constant*> CV; 3531 if (VT == MVT::f64) { 3532 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)))); 3533 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3534 } else { 3535 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)))); 3536 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3537 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3538 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3539 } 3540 Constant *CS = ConstantStruct::get(CV); 3541 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); 3542 SDVTList Tys = DAG.getVTList(VT, MVT::Other); 3543 SmallVector<SDOperand, 3> Ops; 3544 Ops.push_back(DAG.getEntryNode()); 3545 Ops.push_back(CPIdx); 3546 Ops.push_back(DAG.getSrcValue(NULL)); 3547 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); 3548 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask); 3549} 3550 3551SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) { 3552 MVT::ValueType VT = Op.getValueType(); 3553 const Type *OpNTy = MVT::getTypeForValueType(VT); 3554 std::vector<Constant*> CV; 3555 if (VT == MVT::f64) { 3556 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63))); 3557 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3558 } else { 3559 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31))); 3560 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3561 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3562 CV.push_back(ConstantFP::get(OpNTy, 0.0)); 3563 } 3564 Constant *CS = ConstantStruct::get(CV); 3565 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); 3566 SDVTList Tys = DAG.getVTList(VT, MVT::Other); 3567 SmallVector<SDOperand, 3> Ops; 3568 Ops.push_back(DAG.getEntryNode()); 3569 Ops.push_back(CPIdx); 3570 Ops.push_back(DAG.getSrcValue(NULL)); 3571 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); 3572 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask); 3573} 3574 3575SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) { 3576 SDOperand Op0 = Op.getOperand(0); 3577 SDOperand Op1 = Op.getOperand(1); 3578 MVT::ValueType VT = Op.getValueType(); 3579 MVT::ValueType SrcVT = Op1.getValueType(); 3580 const Type *SrcTy = MVT::getTypeForValueType(SrcVT); 3581 3582 // If second operand is smaller, extend it first. 3583 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) { 3584 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1); 3585 SrcVT = VT; 3586 } 3587 3588 // First get the sign bit of second operand. 3589 std::vector<Constant*> CV; 3590 if (SrcVT == MVT::f64) { 3591 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63))); 3592 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3593 } else { 3594 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31))); 3595 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3596 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3597 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3598 } 3599 Constant *CS = ConstantStruct::get(CV); 3600 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); 3601 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other); 3602 SmallVector<SDOperand, 3> Ops; 3603 Ops.push_back(DAG.getEntryNode()); 3604 Ops.push_back(CPIdx); 3605 Ops.push_back(DAG.getSrcValue(NULL)); 3606 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); 3607 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1); 3608 3609 // Shift sign bit right or left if the two operands have different types. 3610 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) { 3611 // Op0 is MVT::f32, Op1 is MVT::f64. 3612 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit); 3613 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit, 3614 DAG.getConstant(32, MVT::i32)); 3615 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit); 3616 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit, 3617 DAG.getConstant(0, getPointerTy())); 3618 } 3619 3620 // Clear first operand sign bit. 3621 CV.clear(); 3622 if (VT == MVT::f64) { 3623 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63)))); 3624 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3625 } else { 3626 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31)))); 3627 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3628 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3629 CV.push_back(ConstantFP::get(SrcTy, 0.0)); 3630 } 3631 CS = ConstantStruct::get(CV); 3632 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); 3633 Tys = DAG.getVTList(VT, MVT::Other); 3634 Ops.clear(); 3635 Ops.push_back(DAG.getEntryNode()); 3636 Ops.push_back(CPIdx); 3637 Ops.push_back(DAG.getSrcValue(NULL)); 3638 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); 3639 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2); 3640 3641 // Or the value with the sign bit. 3642 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit); 3643} 3644 3645SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG, 3646 SDOperand Chain) { 3647 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); 3648 SDOperand Cond; 3649 SDOperand Op0 = Op.getOperand(0); 3650 SDOperand Op1 = Op.getOperand(1); 3651 SDOperand CC = Op.getOperand(2); 3652 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 3653 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); 3654 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag); 3655 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType()); 3656 unsigned X86CC; 3657 3658 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, 3659 Op0, Op1, DAG)) { 3660 SDOperand Ops1[] = { Chain, Op0, Op1 }; 3661 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1); 3662 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond }; 3663 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); 3664 } 3665 3666 assert(isFP && "Illegal integer SetCC!"); 3667 3668 SDOperand COps[] = { Chain, Op0, Op1 }; 3669 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1); 3670 3671 switch (SetCCOpcode) { 3672 default: assert(false && "Illegal floating point SetCC!"); 3673 case ISD::SETOEQ: { // !PF & ZF 3674 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond }; 3675 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2); 3676 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8), 3677 Tmp1.getValue(1) }; 3678 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); 3679 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2); 3680 } 3681 case ISD::SETUNE: { // PF | !ZF 3682 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond }; 3683 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2); 3684 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8), 3685 Tmp1.getValue(1) }; 3686 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); 3687 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2); 3688 } 3689 } 3690} 3691 3692SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) { 3693 bool addTest = true; 3694 SDOperand Chain = DAG.getEntryNode(); 3695 SDOperand Cond = Op.getOperand(0); 3696 SDOperand CC; 3697 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); 3698 3699 if (Cond.getOpcode() == ISD::SETCC) 3700 Cond = LowerSETCC(Cond, DAG, Chain); 3701 3702 if (Cond.getOpcode() == X86ISD::SETCC) { 3703 CC = Cond.getOperand(0); 3704 3705 // If condition flag is set by a X86ISD::CMP, then make a copy of it 3706 // (since flag operand cannot be shared). Use it as the condition setting 3707 // operand in place of the X86ISD::SETCC. 3708 // If the X86ISD::SETCC has more than one use, then perhaps it's better 3709 // to use a test instead of duplicating the X86ISD::CMP (for register 3710 // pressure reason)? 3711 SDOperand Cmp = Cond.getOperand(1); 3712 unsigned Opc = Cmp.getOpcode(); 3713 bool IllegalFPCMov = !X86ScalarSSE && 3714 MVT::isFloatingPoint(Op.getValueType()) && 3715 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()); 3716 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) && 3717 !IllegalFPCMov) { 3718 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) }; 3719 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3); 3720 addTest = false; 3721 } 3722 } 3723 3724 if (addTest) { 3725 CC = DAG.getConstant(X86::COND_NE, MVT::i8); 3726 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) }; 3727 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3); 3728 } 3729 3730 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag); 3731 SmallVector<SDOperand, 4> Ops; 3732 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if 3733 // condition is true. 3734 Ops.push_back(Op.getOperand(2)); 3735 Ops.push_back(Op.getOperand(1)); 3736 Ops.push_back(CC); 3737 Ops.push_back(Cond.getValue(1)); 3738 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); 3739} 3740 3741SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) { 3742 bool addTest = true; 3743 SDOperand Chain = Op.getOperand(0); 3744 SDOperand Cond = Op.getOperand(1); 3745 SDOperand Dest = Op.getOperand(2); 3746 SDOperand CC; 3747 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); 3748 3749 if (Cond.getOpcode() == ISD::SETCC) 3750 Cond = LowerSETCC(Cond, DAG, Chain); 3751 3752 if (Cond.getOpcode() == X86ISD::SETCC) { 3753 CC = Cond.getOperand(0); 3754 3755 // If condition flag is set by a X86ISD::CMP, then make a copy of it 3756 // (since flag operand cannot be shared). Use it as the condition setting 3757 // operand in place of the X86ISD::SETCC. 3758 // If the X86ISD::SETCC has more than one use, then perhaps it's better 3759 // to use a test instead of duplicating the X86ISD::CMP (for register 3760 // pressure reason)? 3761 SDOperand Cmp = Cond.getOperand(1); 3762 unsigned Opc = Cmp.getOpcode(); 3763 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) { 3764 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) }; 3765 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3); 3766 addTest = false; 3767 } 3768 } 3769 3770 if (addTest) { 3771 CC = DAG.getConstant(X86::COND_NE, MVT::i8); 3772 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) }; 3773 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3); 3774 } 3775 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), 3776 Cond, Op.getOperand(2), CC, Cond.getValue(1)); 3777} 3778 3779SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) { 3780 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue(); 3781 3782 if (Subtarget->is64Bit()) 3783 return LowerX86_64CCCCallTo(Op, DAG, CallingConv); 3784 else 3785 switch (CallingConv) { 3786 default: 3787 assert(0 && "Unsupported calling convention"); 3788 case CallingConv::Fast: 3789 if (EnableFastCC) 3790 return LowerFastCCCallTo(Op, DAG, CallingConv); 3791 // Falls through 3792 case CallingConv::C: 3793 case CallingConv::X86_StdCall: 3794 return LowerCCCCallTo(Op, DAG, CallingConv); 3795 case CallingConv::X86_FastCall: 3796 return LowerFastCCCallTo(Op, DAG, CallingConv); 3797 } 3798} 3799 3800SDOperand 3801X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { 3802 MachineFunction &MF = DAG.getMachineFunction(); 3803 const Function* Fn = MF.getFunction(); 3804 if (Fn->hasExternalLinkage() && 3805 Subtarget->isTargetCygMing() && 3806 Fn->getName() == "main") 3807 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true); 3808 3809 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); 3810 if (Subtarget->is64Bit()) 3811 return LowerX86_64CCCArguments(Op, DAG); 3812 else 3813 switch(CC) { 3814 default: 3815 assert(0 && "Unsupported calling convention"); 3816 case CallingConv::Fast: 3817 if (EnableFastCC) { 3818 return LowerFastCCArguments(Op, DAG); 3819 } 3820 // Falls through 3821 case CallingConv::C: 3822 return LowerCCCArguments(Op, DAG); 3823 case CallingConv::X86_StdCall: 3824 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall); 3825 return LowerCCCArguments(Op, DAG, true); 3826 case CallingConv::X86_FastCall: 3827 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall); 3828 return LowerFastCCArguments(Op, DAG, true); 3829 } 3830} 3831 3832SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) { 3833 SDOperand InFlag(0, 0); 3834 SDOperand Chain = Op.getOperand(0); 3835 unsigned Align = 3836 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); 3837 if (Align == 0) Align = 1; 3838 3839 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 3840 // If not DWORD aligned, call memset if size is less than the threshold. 3841 // It knows how to align to the right boundary first. 3842 if ((Align & 3) != 0 || 3843 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { 3844 MVT::ValueType IntPtr = getPointerTy(); 3845 const Type *IntPtrTy = getTargetData()->getIntPtrType(); 3846 TargetLowering::ArgListTy Args; 3847 TargetLowering::ArgListEntry Entry; 3848 Entry.Node = Op.getOperand(1); 3849 Entry.Ty = IntPtrTy; 3850 Entry.isSigned = false; 3851 Entry.isInReg = false; 3852 Entry.isSRet = false; 3853 Args.push_back(Entry); 3854 // Extend the unsigned i8 argument to be an int value for the call. 3855 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2)); 3856 Entry.Ty = IntPtrTy; 3857 Entry.isSigned = false; 3858 Entry.isInReg = false; 3859 Entry.isSRet = false; 3860 Args.push_back(Entry); 3861 Entry.Node = Op.getOperand(3); 3862 Args.push_back(Entry); 3863 std::pair<SDOperand,SDOperand> CallResult = 3864 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, 3865 DAG.getExternalSymbol("memset", IntPtr), Args, DAG); 3866 return CallResult.second; 3867 } 3868 3869 MVT::ValueType AVT; 3870 SDOperand Count; 3871 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 3872 unsigned BytesLeft = 0; 3873 bool TwoRepStos = false; 3874 if (ValC) { 3875 unsigned ValReg; 3876 uint64_t Val = ValC->getValue() & 255; 3877 3878 // If the value is a constant, then we can potentially use larger sets. 3879 switch (Align & 3) { 3880 case 2: // WORD aligned 3881 AVT = MVT::i16; 3882 ValReg = X86::AX; 3883 Val = (Val << 8) | Val; 3884 break; 3885 case 0: // DWORD aligned 3886 AVT = MVT::i32; 3887 ValReg = X86::EAX; 3888 Val = (Val << 8) | Val; 3889 Val = (Val << 16) | Val; 3890 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned 3891 AVT = MVT::i64; 3892 ValReg = X86::RAX; 3893 Val = (Val << 32) | Val; 3894 } 3895 break; 3896 default: // Byte aligned 3897 AVT = MVT::i8; 3898 ValReg = X86::AL; 3899 Count = Op.getOperand(3); 3900 break; 3901 } 3902 3903 if (AVT > MVT::i8) { 3904 if (I) { 3905 unsigned UBytes = MVT::getSizeInBits(AVT) / 8; 3906 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy()); 3907 BytesLeft = I->getValue() % UBytes; 3908 } else { 3909 assert(AVT >= MVT::i32 && 3910 "Do not use rep;stos if not at least DWORD aligned"); 3911 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), 3912 Op.getOperand(3), DAG.getConstant(2, MVT::i8)); 3913 TwoRepStos = true; 3914 } 3915 } 3916 3917 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT), 3918 InFlag); 3919 InFlag = Chain.getValue(1); 3920 } else { 3921 AVT = MVT::i8; 3922 Count = Op.getOperand(3); 3923 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag); 3924 InFlag = Chain.getValue(1); 3925 } 3926 3927 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, 3928 Count, InFlag); 3929 InFlag = Chain.getValue(1); 3930 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, 3931 Op.getOperand(1), InFlag); 3932 InFlag = Chain.getValue(1); 3933 3934 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 3935 SmallVector<SDOperand, 8> Ops; 3936 Ops.push_back(Chain); 3937 Ops.push_back(DAG.getValueType(AVT)); 3938 Ops.push_back(InFlag); 3939 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); 3940 3941 if (TwoRepStos) { 3942 InFlag = Chain.getValue(1); 3943 Count = Op.getOperand(3); 3944 MVT::ValueType CVT = Count.getValueType(); 3945 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, 3946 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); 3947 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, 3948 Left, InFlag); 3949 InFlag = Chain.getValue(1); 3950 Tys = DAG.getVTList(MVT::Other, MVT::Flag); 3951 Ops.clear(); 3952 Ops.push_back(Chain); 3953 Ops.push_back(DAG.getValueType(MVT::i8)); 3954 Ops.push_back(InFlag); 3955 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); 3956 } else if (BytesLeft) { 3957 // Issue stores for the last 1 - 7 bytes. 3958 SDOperand Value; 3959 unsigned Val = ValC->getValue() & 255; 3960 unsigned Offset = I->getValue() - BytesLeft; 3961 SDOperand DstAddr = Op.getOperand(1); 3962 MVT::ValueType AddrVT = DstAddr.getValueType(); 3963 if (BytesLeft >= 4) { 3964 Val = (Val << 8) | Val; 3965 Val = (Val << 16) | Val; 3966 Value = DAG.getConstant(Val, MVT::i32); 3967 Chain = DAG.getStore(Chain, Value, 3968 DAG.getNode(ISD::ADD, AddrVT, DstAddr, 3969 DAG.getConstant(Offset, AddrVT)), 3970 NULL, 0); 3971 BytesLeft -= 4; 3972 Offset += 4; 3973 } 3974 if (BytesLeft >= 2) { 3975 Value = DAG.getConstant((Val << 8) | Val, MVT::i16); 3976 Chain = DAG.getStore(Chain, Value, 3977 DAG.getNode(ISD::ADD, AddrVT, DstAddr, 3978 DAG.getConstant(Offset, AddrVT)), 3979 NULL, 0); 3980 BytesLeft -= 2; 3981 Offset += 2; 3982 } 3983 if (BytesLeft == 1) { 3984 Value = DAG.getConstant(Val, MVT::i8); 3985 Chain = DAG.getStore(Chain, Value, 3986 DAG.getNode(ISD::ADD, AddrVT, DstAddr, 3987 DAG.getConstant(Offset, AddrVT)), 3988 NULL, 0); 3989 } 3990 } 3991 3992 return Chain; 3993} 3994 3995SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) { 3996 SDOperand Chain = Op.getOperand(0); 3997 unsigned Align = 3998 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); 3999 if (Align == 0) Align = 1; 4000 4001 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 4002 // If not DWORD aligned, call memcpy if size is less than the threshold. 4003 // It knows how to align to the right boundary first. 4004 if ((Align & 3) != 0 || 4005 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { 4006 MVT::ValueType IntPtr = getPointerTy(); 4007 TargetLowering::ArgListTy Args; 4008 TargetLowering::ArgListEntry Entry; 4009 Entry.Ty = getTargetData()->getIntPtrType(); 4010 Entry.isSigned = false; 4011 Entry.isInReg = false; 4012 Entry.isSRet = false; 4013 Entry.Node = Op.getOperand(1); Args.push_back(Entry); 4014 Entry.Node = Op.getOperand(2); Args.push_back(Entry); 4015 Entry.Node = Op.getOperand(3); Args.push_back(Entry); 4016 std::pair<SDOperand,SDOperand> CallResult = 4017 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, 4018 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG); 4019 return CallResult.second; 4020 } 4021 4022 MVT::ValueType AVT; 4023 SDOperand Count; 4024 unsigned BytesLeft = 0; 4025 bool TwoRepMovs = false; 4026 switch (Align & 3) { 4027 case 2: // WORD aligned 4028 AVT = MVT::i16; 4029 break; 4030 case 0: // DWORD aligned 4031 AVT = MVT::i32; 4032 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned 4033 AVT = MVT::i64; 4034 break; 4035 default: // Byte aligned 4036 AVT = MVT::i8; 4037 Count = Op.getOperand(3); 4038 break; 4039 } 4040 4041 if (AVT > MVT::i8) { 4042 if (I) { 4043 unsigned UBytes = MVT::getSizeInBits(AVT) / 8; 4044 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy()); 4045 BytesLeft = I->getValue() % UBytes; 4046 } else { 4047 assert(AVT >= MVT::i32 && 4048 "Do not use rep;movs if not at least DWORD aligned"); 4049 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), 4050 Op.getOperand(3), DAG.getConstant(2, MVT::i8)); 4051 TwoRepMovs = true; 4052 } 4053 } 4054 4055 SDOperand InFlag(0, 0); 4056 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, 4057 Count, InFlag); 4058 InFlag = Chain.getValue(1); 4059 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, 4060 Op.getOperand(1), InFlag); 4061 InFlag = Chain.getValue(1); 4062 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI, 4063 Op.getOperand(2), InFlag); 4064 InFlag = Chain.getValue(1); 4065 4066 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 4067 SmallVector<SDOperand, 8> Ops; 4068 Ops.push_back(Chain); 4069 Ops.push_back(DAG.getValueType(AVT)); 4070 Ops.push_back(InFlag); 4071 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); 4072 4073 if (TwoRepMovs) { 4074 InFlag = Chain.getValue(1); 4075 Count = Op.getOperand(3); 4076 MVT::ValueType CVT = Count.getValueType(); 4077 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, 4078 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); 4079 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, 4080 Left, InFlag); 4081 InFlag = Chain.getValue(1); 4082 Tys = DAG.getVTList(MVT::Other, MVT::Flag); 4083 Ops.clear(); 4084 Ops.push_back(Chain); 4085 Ops.push_back(DAG.getValueType(MVT::i8)); 4086 Ops.push_back(InFlag); 4087 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); 4088 } else if (BytesLeft) { 4089 // Issue loads and stores for the last 1 - 7 bytes. 4090 unsigned Offset = I->getValue() - BytesLeft; 4091 SDOperand DstAddr = Op.getOperand(1); 4092 MVT::ValueType DstVT = DstAddr.getValueType(); 4093 SDOperand SrcAddr = Op.getOperand(2); 4094 MVT::ValueType SrcVT = SrcAddr.getValueType(); 4095 SDOperand Value; 4096 if (BytesLeft >= 4) { 4097 Value = DAG.getLoad(MVT::i32, Chain, 4098 DAG.getNode(ISD::ADD, SrcVT, SrcAddr, 4099 DAG.getConstant(Offset, SrcVT)), 4100 NULL, 0); 4101 Chain = Value.getValue(1); 4102 Chain = DAG.getStore(Chain, Value, 4103 DAG.getNode(ISD::ADD, DstVT, DstAddr, 4104 DAG.getConstant(Offset, DstVT)), 4105 NULL, 0); 4106 BytesLeft -= 4; 4107 Offset += 4; 4108 } 4109 if (BytesLeft >= 2) { 4110 Value = DAG.getLoad(MVT::i16, Chain, 4111 DAG.getNode(ISD::ADD, SrcVT, SrcAddr, 4112 DAG.getConstant(Offset, SrcVT)), 4113 NULL, 0); 4114 Chain = Value.getValue(1); 4115 Chain = DAG.getStore(Chain, Value, 4116 DAG.getNode(ISD::ADD, DstVT, DstAddr, 4117 DAG.getConstant(Offset, DstVT)), 4118 NULL, 0); 4119 BytesLeft -= 2; 4120 Offset += 2; 4121 } 4122 4123 if (BytesLeft == 1) { 4124 Value = DAG.getLoad(MVT::i8, Chain, 4125 DAG.getNode(ISD::ADD, SrcVT, SrcAddr, 4126 DAG.getConstant(Offset, SrcVT)), 4127 NULL, 0); 4128 Chain = Value.getValue(1); 4129 Chain = DAG.getStore(Chain, Value, 4130 DAG.getNode(ISD::ADD, DstVT, DstAddr, 4131 DAG.getConstant(Offset, DstVT)), 4132 NULL, 0); 4133 } 4134 } 4135 4136 return Chain; 4137} 4138 4139SDOperand 4140X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) { 4141 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); 4142 SDOperand TheOp = Op.getOperand(0); 4143 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1); 4144 if (Subtarget->is64Bit()) { 4145 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1)); 4146 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX, 4147 MVT::i64, Copy1.getValue(2)); 4148 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2, 4149 DAG.getConstant(32, MVT::i8)); 4150 SDOperand Ops[] = { 4151 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1) 4152 }; 4153 4154 Tys = DAG.getVTList(MVT::i64, MVT::Other); 4155 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2); 4156 } 4157 4158 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)); 4159 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX, 4160 MVT::i32, Copy1.getValue(2)); 4161 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) }; 4162 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 4163 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3); 4164} 4165 4166SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) { 4167 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2)); 4168 4169 if (!Subtarget->is64Bit()) { 4170 // vastart just stores the address of the VarArgsFrameIndex slot into the 4171 // memory location argument. 4172 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); 4173 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(), 4174 SV->getOffset()); 4175 } 4176 4177 // __va_list_tag: 4178 // gp_offset (0 - 6 * 8) 4179 // fp_offset (48 - 48 + 8 * 16) 4180 // overflow_arg_area (point to parameters coming in memory). 4181 // reg_save_area 4182 SmallVector<SDOperand, 8> MemOps; 4183 SDOperand FIN = Op.getOperand(1); 4184 // Store gp_offset 4185 SDOperand Store = DAG.getStore(Op.getOperand(0), 4186 DAG.getConstant(VarArgsGPOffset, MVT::i32), 4187 FIN, SV->getValue(), SV->getOffset()); 4188 MemOps.push_back(Store); 4189 4190 // Store fp_offset 4191 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, 4192 DAG.getConstant(4, getPointerTy())); 4193 Store = DAG.getStore(Op.getOperand(0), 4194 DAG.getConstant(VarArgsFPOffset, MVT::i32), 4195 FIN, SV->getValue(), SV->getOffset()); 4196 MemOps.push_back(Store); 4197 4198 // Store ptr to overflow_arg_area 4199 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, 4200 DAG.getConstant(4, getPointerTy())); 4201 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); 4202 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(), 4203 SV->getOffset()); 4204 MemOps.push_back(Store); 4205 4206 // Store ptr to reg_save_area. 4207 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, 4208 DAG.getConstant(8, getPointerTy())); 4209 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); 4210 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(), 4211 SV->getOffset()); 4212 MemOps.push_back(Store); 4213 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size()); 4214} 4215 4216SDOperand 4217X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) { 4218 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue(); 4219 switch (IntNo) { 4220 default: return SDOperand(); // Don't custom lower most intrinsics. 4221 // Comparison intrinsics. 4222 case Intrinsic::x86_sse_comieq_ss: 4223 case Intrinsic::x86_sse_comilt_ss: 4224 case Intrinsic::x86_sse_comile_ss: 4225 case Intrinsic::x86_sse_comigt_ss: 4226 case Intrinsic::x86_sse_comige_ss: 4227 case Intrinsic::x86_sse_comineq_ss: 4228 case Intrinsic::x86_sse_ucomieq_ss: 4229 case Intrinsic::x86_sse_ucomilt_ss: 4230 case Intrinsic::x86_sse_ucomile_ss: 4231 case Intrinsic::x86_sse_ucomigt_ss: 4232 case Intrinsic::x86_sse_ucomige_ss: 4233 case Intrinsic::x86_sse_ucomineq_ss: 4234 case Intrinsic::x86_sse2_comieq_sd: 4235 case Intrinsic::x86_sse2_comilt_sd: 4236 case Intrinsic::x86_sse2_comile_sd: 4237 case Intrinsic::x86_sse2_comigt_sd: 4238 case Intrinsic::x86_sse2_comige_sd: 4239 case Intrinsic::x86_sse2_comineq_sd: 4240 case Intrinsic::x86_sse2_ucomieq_sd: 4241 case Intrinsic::x86_sse2_ucomilt_sd: 4242 case Intrinsic::x86_sse2_ucomile_sd: 4243 case Intrinsic::x86_sse2_ucomigt_sd: 4244 case Intrinsic::x86_sse2_ucomige_sd: 4245 case Intrinsic::x86_sse2_ucomineq_sd: { 4246 unsigned Opc = 0; 4247 ISD::CondCode CC = ISD::SETCC_INVALID; 4248 switch (IntNo) { 4249 default: break; 4250 case Intrinsic::x86_sse_comieq_ss: 4251 case Intrinsic::x86_sse2_comieq_sd: 4252 Opc = X86ISD::COMI; 4253 CC = ISD::SETEQ; 4254 break; 4255 case Intrinsic::x86_sse_comilt_ss: 4256 case Intrinsic::x86_sse2_comilt_sd: 4257 Opc = X86ISD::COMI; 4258 CC = ISD::SETLT; 4259 break; 4260 case Intrinsic::x86_sse_comile_ss: 4261 case Intrinsic::x86_sse2_comile_sd: 4262 Opc = X86ISD::COMI; 4263 CC = ISD::SETLE; 4264 break; 4265 case Intrinsic::x86_sse_comigt_ss: 4266 case Intrinsic::x86_sse2_comigt_sd: 4267 Opc = X86ISD::COMI; 4268 CC = ISD::SETGT; 4269 break; 4270 case Intrinsic::x86_sse_comige_ss: 4271 case Intrinsic::x86_sse2_comige_sd: 4272 Opc = X86ISD::COMI; 4273 CC = ISD::SETGE; 4274 break; 4275 case Intrinsic::x86_sse_comineq_ss: 4276 case Intrinsic::x86_sse2_comineq_sd: 4277 Opc = X86ISD::COMI; 4278 CC = ISD::SETNE; 4279 break; 4280 case Intrinsic::x86_sse_ucomieq_ss: 4281 case Intrinsic::x86_sse2_ucomieq_sd: 4282 Opc = X86ISD::UCOMI; 4283 CC = ISD::SETEQ; 4284 break; 4285 case Intrinsic::x86_sse_ucomilt_ss: 4286 case Intrinsic::x86_sse2_ucomilt_sd: 4287 Opc = X86ISD::UCOMI; 4288 CC = ISD::SETLT; 4289 break; 4290 case Intrinsic::x86_sse_ucomile_ss: 4291 case Intrinsic::x86_sse2_ucomile_sd: 4292 Opc = X86ISD::UCOMI; 4293 CC = ISD::SETLE; 4294 break; 4295 case Intrinsic::x86_sse_ucomigt_ss: 4296 case Intrinsic::x86_sse2_ucomigt_sd: 4297 Opc = X86ISD::UCOMI; 4298 CC = ISD::SETGT; 4299 break; 4300 case Intrinsic::x86_sse_ucomige_ss: 4301 case Intrinsic::x86_sse2_ucomige_sd: 4302 Opc = X86ISD::UCOMI; 4303 CC = ISD::SETGE; 4304 break; 4305 case Intrinsic::x86_sse_ucomineq_ss: 4306 case Intrinsic::x86_sse2_ucomineq_sd: 4307 Opc = X86ISD::UCOMI; 4308 CC = ISD::SETNE; 4309 break; 4310 } 4311 4312 unsigned X86CC; 4313 SDOperand LHS = Op.getOperand(1); 4314 SDOperand RHS = Op.getOperand(2); 4315 translateX86CC(CC, true, X86CC, LHS, RHS, DAG); 4316 4317 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); 4318 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS }; 4319 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3); 4320 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag); 4321 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond }; 4322 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2); 4323 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC); 4324 } 4325 } 4326} 4327 4328SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) { 4329 // Depths > 0 not supported yet! 4330 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) 4331 return SDOperand(); 4332 4333 // Just load the return address 4334 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); 4335 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0); 4336} 4337 4338SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) { 4339 // Depths > 0 not supported yet! 4340 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) 4341 return SDOperand(); 4342 4343 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); 4344 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI, 4345 DAG.getConstant(4, getPointerTy())); 4346} 4347 4348/// LowerOperation - Provide custom lowering hooks for some operations. 4349/// 4350SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { 4351 switch (Op.getOpcode()) { 4352 default: assert(0 && "Should not custom lower this!"); 4353 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); 4354 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 4355 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 4356 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 4357 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); 4358 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 4359 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 4360 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); 4361 case ISD::SHL_PARTS: 4362 case ISD::SRA_PARTS: 4363 case ISD::SRL_PARTS: return LowerShift(Op, DAG); 4364 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); 4365 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); 4366 case ISD::FABS: return LowerFABS(Op, DAG); 4367 case ISD::FNEG: return LowerFNEG(Op, DAG); 4368 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 4369 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode()); 4370 case ISD::SELECT: return LowerSELECT(Op, DAG); 4371 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 4372 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 4373 case ISD::CALL: return LowerCALL(Op, DAG); 4374 case ISD::RET: return LowerRET(Op, DAG); 4375 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); 4376 case ISD::MEMSET: return LowerMEMSET(Op, DAG); 4377 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG); 4378 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG); 4379 case ISD::VASTART: return LowerVASTART(Op, DAG); 4380 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 4381 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 4382 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 4383 } 4384 return SDOperand(); 4385} 4386 4387const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { 4388 switch (Opcode) { 4389 default: return NULL; 4390 case X86ISD::SHLD: return "X86ISD::SHLD"; 4391 case X86ISD::SHRD: return "X86ISD::SHRD"; 4392 case X86ISD::FAND: return "X86ISD::FAND"; 4393 case X86ISD::FOR: return "X86ISD::FOR"; 4394 case X86ISD::FXOR: return "X86ISD::FXOR"; 4395 case X86ISD::FSRL: return "X86ISD::FSRL"; 4396 case X86ISD::FILD: return "X86ISD::FILD"; 4397 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; 4398 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; 4399 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; 4400 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM"; 4401 case X86ISD::FLD: return "X86ISD::FLD"; 4402 case X86ISD::FST: return "X86ISD::FST"; 4403 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT"; 4404 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; 4405 case X86ISD::CALL: return "X86ISD::CALL"; 4406 case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; 4407 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; 4408 case X86ISD::CMP: return "X86ISD::CMP"; 4409 case X86ISD::COMI: return "X86ISD::COMI"; 4410 case X86ISD::UCOMI: return "X86ISD::UCOMI"; 4411 case X86ISD::SETCC: return "X86ISD::SETCC"; 4412 case X86ISD::CMOV: return "X86ISD::CMOV"; 4413 case X86ISD::BRCOND: return "X86ISD::BRCOND"; 4414 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; 4415 case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; 4416 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; 4417 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK"; 4418 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA"; 4419 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; 4420 case X86ISD::Wrapper: return "X86ISD::Wrapper"; 4421 case X86ISD::S2VEC: return "X86ISD::S2VEC"; 4422 case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; 4423 case X86ISD::PINSRW: return "X86ISD::PINSRW"; 4424 case X86ISD::FMAX: return "X86ISD::FMAX"; 4425 case X86ISD::FMIN: return "X86ISD::FMIN"; 4426 } 4427} 4428 4429/// isLegalAddressImmediate - Return true if the integer value or 4430/// GlobalValue can be used as the offset of the target addressing mode. 4431bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const { 4432 // X86 allows a sign-extended 32-bit immediate field. 4433 return (V > -(1LL << 32) && V < (1LL << 32)-1); 4434} 4435 4436bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const { 4437 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement 4438 // field unless we are in small code model. 4439 if (Subtarget->is64Bit() && 4440 getTargetMachine().getCodeModel() != CodeModel::Small) 4441 return false; 4442 4443 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false)); 4444} 4445 4446/// isShuffleMaskLegal - Targets can use this to indicate that they only 4447/// support *some* VECTOR_SHUFFLE operations, those with specific masks. 4448/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 4449/// are assumed to be legal. 4450bool 4451X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const { 4452 // Only do shuffles on 128-bit vector types for now. 4453 if (MVT::getSizeInBits(VT) == 64) return false; 4454 return (Mask.Val->getNumOperands() <= 4 || 4455 isSplatMask(Mask.Val) || 4456 isPSHUFHW_PSHUFLWMask(Mask.Val) || 4457 X86::isUNPCKLMask(Mask.Val) || 4458 X86::isUNPCKL_v_undef_Mask(Mask.Val) || 4459 X86::isUNPCKHMask(Mask.Val)); 4460} 4461 4462bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps, 4463 MVT::ValueType EVT, 4464 SelectionDAG &DAG) const { 4465 unsigned NumElts = BVOps.size(); 4466 // Only do shuffles on 128-bit vector types for now. 4467 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false; 4468 if (NumElts == 2) return true; 4469 if (NumElts == 4) { 4470 return (isMOVLMask(&BVOps[0], 4) || 4471 isCommutedMOVL(&BVOps[0], 4, true) || 4472 isSHUFPMask(&BVOps[0], 4) || 4473 isCommutedSHUFP(&BVOps[0], 4)); 4474 } 4475 return false; 4476} 4477 4478//===----------------------------------------------------------------------===// 4479// X86 Scheduler Hooks 4480//===----------------------------------------------------------------------===// 4481 4482MachineBasicBlock * 4483X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, 4484 MachineBasicBlock *BB) { 4485 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 4486 switch (MI->getOpcode()) { 4487 default: assert(false && "Unexpected instr type to insert"); 4488 case X86::CMOV_FR32: 4489 case X86::CMOV_FR64: 4490 case X86::CMOV_V4F32: 4491 case X86::CMOV_V2F64: 4492 case X86::CMOV_V2I64: { 4493 // To "insert" a SELECT_CC instruction, we actually have to insert the 4494 // diamond control-flow pattern. The incoming instruction knows the 4495 // destination vreg to set, the condition code register to branch on, the 4496 // true/false values to select between, and a branch opcode to use. 4497 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 4498 ilist<MachineBasicBlock>::iterator It = BB; 4499 ++It; 4500 4501 // thisMBB: 4502 // ... 4503 // TrueVal = ... 4504 // cmpTY ccX, r1, r2 4505 // bCC copy1MBB 4506 // fallthrough --> copy0MBB 4507 MachineBasicBlock *thisMBB = BB; 4508 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); 4509 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); 4510 unsigned Opc = 4511 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); 4512 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB); 4513 MachineFunction *F = BB->getParent(); 4514 F->getBasicBlockList().insert(It, copy0MBB); 4515 F->getBasicBlockList().insert(It, sinkMBB); 4516 // Update machine-CFG edges by first adding all successors of the current 4517 // block to the new block which will contain the Phi node for the select. 4518 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), 4519 e = BB->succ_end(); i != e; ++i) 4520 sinkMBB->addSuccessor(*i); 4521 // Next, remove all successors of the current block, and add the true 4522 // and fallthrough blocks as its successors. 4523 while(!BB->succ_empty()) 4524 BB->removeSuccessor(BB->succ_begin()); 4525 BB->addSuccessor(copy0MBB); 4526 BB->addSuccessor(sinkMBB); 4527 4528 // copy0MBB: 4529 // %FalseValue = ... 4530 // # fallthrough to sinkMBB 4531 BB = copy0MBB; 4532 4533 // Update machine-CFG edges 4534 BB->addSuccessor(sinkMBB); 4535 4536 // sinkMBB: 4537 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 4538 // ... 4539 BB = sinkMBB; 4540 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg()) 4541 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 4542 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 4543 4544 delete MI; // The pseudo instruction is gone now. 4545 return BB; 4546 } 4547 4548 case X86::FP_TO_INT16_IN_MEM: 4549 case X86::FP_TO_INT32_IN_MEM: 4550 case X86::FP_TO_INT64_IN_MEM: { 4551 // Change the floating point control register to use "round towards zero" 4552 // mode when truncating to an integer value. 4553 MachineFunction *F = BB->getParent(); 4554 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); 4555 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx); 4556 4557 // Load the old value of the high byte of the control word... 4558 unsigned OldCW = 4559 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass); 4560 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx); 4561 4562 // Set the high part to be round to zero... 4563 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx) 4564 .addImm(0xC7F); 4565 4566 // Reload the modified control word now... 4567 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); 4568 4569 // Restore the memory image of control word to original value 4570 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx) 4571 .addReg(OldCW); 4572 4573 // Get the X86 opcode to use. 4574 unsigned Opc; 4575 switch (MI->getOpcode()) { 4576 default: assert(0 && "illegal opcode!"); 4577 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break; 4578 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break; 4579 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break; 4580 } 4581 4582 X86AddressMode AM; 4583 MachineOperand &Op = MI->getOperand(0); 4584 if (Op.isRegister()) { 4585 AM.BaseType = X86AddressMode::RegBase; 4586 AM.Base.Reg = Op.getReg(); 4587 } else { 4588 AM.BaseType = X86AddressMode::FrameIndexBase; 4589 AM.Base.FrameIndex = Op.getFrameIndex(); 4590 } 4591 Op = MI->getOperand(1); 4592 if (Op.isImmediate()) 4593 AM.Scale = Op.getImm(); 4594 Op = MI->getOperand(2); 4595 if (Op.isImmediate()) 4596 AM.IndexReg = Op.getImm(); 4597 Op = MI->getOperand(3); 4598 if (Op.isGlobalAddress()) { 4599 AM.GV = Op.getGlobal(); 4600 } else { 4601 AM.Disp = Op.getImm(); 4602 } 4603 addFullAddress(BuildMI(BB, TII->get(Opc)), AM) 4604 .addReg(MI->getOperand(4).getReg()); 4605 4606 // Reload the original control word now. 4607 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); 4608 4609 delete MI; // The pseudo instruction is gone now. 4610 return BB; 4611 } 4612 } 4613} 4614 4615//===----------------------------------------------------------------------===// 4616// X86 Optimization Hooks 4617//===----------------------------------------------------------------------===// 4618 4619void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, 4620 uint64_t Mask, 4621 uint64_t &KnownZero, 4622 uint64_t &KnownOne, 4623 unsigned Depth) const { 4624 unsigned Opc = Op.getOpcode(); 4625 assert((Opc >= ISD::BUILTIN_OP_END || 4626 Opc == ISD::INTRINSIC_WO_CHAIN || 4627 Opc == ISD::INTRINSIC_W_CHAIN || 4628 Opc == ISD::INTRINSIC_VOID) && 4629 "Should use MaskedValueIsZero if you don't know whether Op" 4630 " is a target node!"); 4631 4632 KnownZero = KnownOne = 0; // Don't know anything. 4633 switch (Opc) { 4634 default: break; 4635 case X86ISD::SETCC: 4636 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); 4637 break; 4638 } 4639} 4640 4641/// getShuffleScalarElt - Returns the scalar element that will make up the ith 4642/// element of the result of the vector shuffle. 4643static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) { 4644 MVT::ValueType VT = N->getValueType(0); 4645 SDOperand PermMask = N->getOperand(2); 4646 unsigned NumElems = PermMask.getNumOperands(); 4647 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1); 4648 i %= NumElems; 4649 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) { 4650 return (i == 0) 4651 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT)); 4652 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) { 4653 SDOperand Idx = PermMask.getOperand(i); 4654 if (Idx.getOpcode() == ISD::UNDEF) 4655 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT)); 4656 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG); 4657 } 4658 return SDOperand(); 4659} 4660 4661/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the 4662/// node is a GlobalAddress + an offset. 4663static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) { 4664 unsigned Opc = N->getOpcode(); 4665 if (Opc == X86ISD::Wrapper) { 4666 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) { 4667 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); 4668 return true; 4669 } 4670 } else if (Opc == ISD::ADD) { 4671 SDOperand N1 = N->getOperand(0); 4672 SDOperand N2 = N->getOperand(1); 4673 if (isGAPlusOffset(N1.Val, GA, Offset)) { 4674 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2); 4675 if (V) { 4676 Offset += V->getSignExtended(); 4677 return true; 4678 } 4679 } else if (isGAPlusOffset(N2.Val, GA, Offset)) { 4680 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1); 4681 if (V) { 4682 Offset += V->getSignExtended(); 4683 return true; 4684 } 4685 } 4686 } 4687 return false; 4688} 4689 4690/// isConsecutiveLoad - Returns true if N is loading from an address of Base 4691/// + Dist * Size. 4692static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size, 4693 MachineFrameInfo *MFI) { 4694 if (N->getOperand(0).Val != Base->getOperand(0).Val) 4695 return false; 4696 4697 SDOperand Loc = N->getOperand(1); 4698 SDOperand BaseLoc = Base->getOperand(1); 4699 if (Loc.getOpcode() == ISD::FrameIndex) { 4700 if (BaseLoc.getOpcode() != ISD::FrameIndex) 4701 return false; 4702 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex(); 4703 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 4704 int FS = MFI->getObjectSize(FI); 4705 int BFS = MFI->getObjectSize(BFI); 4706 if (FS != BFS || FS != Size) return false; 4707 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size); 4708 } else { 4709 GlobalValue *GV1 = NULL; 4710 GlobalValue *GV2 = NULL; 4711 int64_t Offset1 = 0; 4712 int64_t Offset2 = 0; 4713 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1); 4714 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2); 4715 if (isGA1 && isGA2 && GV1 == GV2) 4716 return Offset1 == (Offset2 + Dist*Size); 4717 } 4718 4719 return false; 4720} 4721 4722static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI, 4723 const X86Subtarget *Subtarget) { 4724 GlobalValue *GV; 4725 int64_t Offset; 4726 if (isGAPlusOffset(Base, GV, Offset)) 4727 return (GV->getAlignment() >= 16 && (Offset % 16) == 0); 4728 else { 4729 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!"); 4730 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex(); 4731 if (BFI < 0) 4732 // Fixed objects do not specify alignment, however the offsets are known. 4733 return ((Subtarget->getStackAlignment() % 16) == 0 && 4734 (MFI->getObjectOffset(BFI) % 16) == 0); 4735 else 4736 return MFI->getObjectAlignment(BFI) >= 16; 4737 } 4738 return false; 4739} 4740 4741 4742/// PerformShuffleCombine - Combine a vector_shuffle that is equal to 4743/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load 4744/// if the load addresses are consecutive, non-overlapping, and in the right 4745/// order. 4746static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, 4747 const X86Subtarget *Subtarget) { 4748 MachineFunction &MF = DAG.getMachineFunction(); 4749 MachineFrameInfo *MFI = MF.getFrameInfo(); 4750 MVT::ValueType VT = N->getValueType(0); 4751 MVT::ValueType EVT = MVT::getVectorBaseType(VT); 4752 SDOperand PermMask = N->getOperand(2); 4753 int NumElems = (int)PermMask.getNumOperands(); 4754 SDNode *Base = NULL; 4755 for (int i = 0; i < NumElems; ++i) { 4756 SDOperand Idx = PermMask.getOperand(i); 4757 if (Idx.getOpcode() == ISD::UNDEF) { 4758 if (!Base) return SDOperand(); 4759 } else { 4760 SDOperand Arg = 4761 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG); 4762 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val)) 4763 return SDOperand(); 4764 if (!Base) 4765 Base = Arg.Val; 4766 else if (!isConsecutiveLoad(Arg.Val, Base, 4767 i, MVT::getSizeInBits(EVT)/8,MFI)) 4768 return SDOperand(); 4769 } 4770 } 4771 4772 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget); 4773 if (isAlign16) { 4774 LoadSDNode *LD = cast<LoadSDNode>(Base); 4775 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(), 4776 LD->getSrcValueOffset()); 4777 } else { 4778 // Just use movups, it's shorter. 4779 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other); 4780 SmallVector<SDOperand, 3> Ops; 4781 Ops.push_back(Base->getOperand(0)); 4782 Ops.push_back(Base->getOperand(1)); 4783 Ops.push_back(Base->getOperand(2)); 4784 return DAG.getNode(ISD::BIT_CONVERT, VT, 4785 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size())); 4786 } 4787} 4788 4789/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. 4790static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, 4791 const X86Subtarget *Subtarget) { 4792 SDOperand Cond = N->getOperand(0); 4793 4794 // If we have SSE[12] support, try to form min/max nodes. 4795 if (Subtarget->hasSSE2() && 4796 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) { 4797 if (Cond.getOpcode() == ISD::SETCC) { 4798 // Get the LHS/RHS of the select. 4799 SDOperand LHS = N->getOperand(1); 4800 SDOperand RHS = N->getOperand(2); 4801 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 4802 4803 unsigned Opcode = 0; 4804 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { 4805 switch (CC) { 4806 default: break; 4807 case ISD::SETOLE: // (X <= Y) ? X : Y -> min 4808 case ISD::SETULE: 4809 case ISD::SETLE: 4810 if (!UnsafeFPMath) break; 4811 // FALL THROUGH. 4812 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min 4813 case ISD::SETLT: 4814 Opcode = X86ISD::FMIN; 4815 break; 4816 4817 case ISD::SETOGT: // (X > Y) ? X : Y -> max 4818 case ISD::SETUGT: 4819 case ISD::SETGT: 4820 if (!UnsafeFPMath) break; 4821 // FALL THROUGH. 4822 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max 4823 case ISD::SETGE: 4824 Opcode = X86ISD::FMAX; 4825 break; 4826 } 4827 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { 4828 switch (CC) { 4829 default: break; 4830 case ISD::SETOGT: // (X > Y) ? Y : X -> min 4831 case ISD::SETUGT: 4832 case ISD::SETGT: 4833 if (!UnsafeFPMath) break; 4834 // FALL THROUGH. 4835 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min 4836 case ISD::SETGE: 4837 Opcode = X86ISD::FMIN; 4838 break; 4839 4840 case ISD::SETOLE: // (X <= Y) ? Y : X -> max 4841 case ISD::SETULE: 4842 case ISD::SETLE: 4843 if (!UnsafeFPMath) break; 4844 // FALL THROUGH. 4845 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max 4846 case ISD::SETLT: 4847 Opcode = X86ISD::FMAX; 4848 break; 4849 } 4850 } 4851 4852 if (Opcode) 4853 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS); 4854 } 4855 4856 } 4857 4858 return SDOperand(); 4859} 4860 4861 4862SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N, 4863 DAGCombinerInfo &DCI) const { 4864 SelectionDAG &DAG = DCI.DAG; 4865 switch (N->getOpcode()) { 4866 default: break; 4867 case ISD::VECTOR_SHUFFLE: 4868 return PerformShuffleCombine(N, DAG, Subtarget); 4869 case ISD::SELECT: 4870 return PerformSELECTCombine(N, DAG, Subtarget); 4871 } 4872 4873 return SDOperand(); 4874} 4875 4876//===----------------------------------------------------------------------===// 4877// X86 Inline Assembly Support 4878//===----------------------------------------------------------------------===// 4879 4880/// getConstraintType - Given a constraint letter, return the type of 4881/// constraint it is for this target. 4882X86TargetLowering::ConstraintType 4883X86TargetLowering::getConstraintType(char ConstraintLetter) const { 4884 switch (ConstraintLetter) { 4885 case 'A': 4886 case 'r': 4887 case 'R': 4888 case 'l': 4889 case 'q': 4890 case 'Q': 4891 case 'x': 4892 case 'Y': 4893 return C_RegisterClass; 4894 default: return TargetLowering::getConstraintType(ConstraintLetter); 4895 } 4896} 4897 4898/// isOperandValidForConstraint - Return the specified operand (possibly 4899/// modified) if the specified SDOperand is valid for the specified target 4900/// constraint letter, otherwise return null. 4901SDOperand X86TargetLowering:: 4902isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) { 4903 switch (Constraint) { 4904 default: break; 4905 case 'i': 4906 // Literal immediates are always ok. 4907 if (isa<ConstantSDNode>(Op)) return Op; 4908 4909 // If we are in non-pic codegen mode, we allow the address of a global to 4910 // be used with 'i'. 4911 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) { 4912 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) 4913 return SDOperand(0, 0); 4914 4915 if (GA->getOpcode() != ISD::TargetGlobalAddress) 4916 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), 4917 GA->getOffset()); 4918 return Op; 4919 } 4920 4921 // Otherwise, not valid for this mode. 4922 return SDOperand(0, 0); 4923 } 4924 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG); 4925} 4926 4927 4928std::vector<unsigned> X86TargetLowering:: 4929getRegClassForInlineAsmConstraint(const std::string &Constraint, 4930 MVT::ValueType VT) const { 4931 if (Constraint.size() == 1) { 4932 // FIXME: not handling fp-stack yet! 4933 // FIXME: not handling MMX registers yet ('y' constraint). 4934 switch (Constraint[0]) { // GCC X86 Constraint Letters 4935 default: break; // Unknown constraint letter 4936 case 'A': // EAX/EDX 4937 if (VT == MVT::i32 || VT == MVT::i64) 4938 return make_vector<unsigned>(X86::EAX, X86::EDX, 0); 4939 break; 4940 case 'r': // GENERAL_REGS 4941 case 'R': // LEGACY_REGS 4942 if (VT == MVT::i64 && Subtarget->is64Bit()) 4943 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 4944 X86::RSI, X86::RDI, X86::RBP, X86::RSP, 4945 X86::R8, X86::R9, X86::R10, X86::R11, 4946 X86::R12, X86::R13, X86::R14, X86::R15, 0); 4947 if (VT == MVT::i32) 4948 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 4949 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0); 4950 else if (VT == MVT::i16) 4951 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 4952 X86::SI, X86::DI, X86::BP, X86::SP, 0); 4953 else if (VT == MVT::i8) 4954 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0); 4955 break; 4956 case 'l': // INDEX_REGS 4957 if (VT == MVT::i32) 4958 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 4959 X86::ESI, X86::EDI, X86::EBP, 0); 4960 else if (VT == MVT::i16) 4961 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 4962 X86::SI, X86::DI, X86::BP, 0); 4963 else if (VT == MVT::i8) 4964 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); 4965 break; 4966 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) 4967 case 'Q': // Q_REGS 4968 if (VT == MVT::i32) 4969 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); 4970 else if (VT == MVT::i16) 4971 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); 4972 else if (VT == MVT::i8) 4973 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); 4974 break; 4975 case 'x': // SSE_REGS if SSE1 allowed 4976 if (Subtarget->hasSSE1()) 4977 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 4978 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, 4979 0); 4980 return std::vector<unsigned>(); 4981 case 'Y': // SSE_REGS if SSE2 allowed 4982 if (Subtarget->hasSSE2()) 4983 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, 4984 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, 4985 0); 4986 return std::vector<unsigned>(); 4987 } 4988 } 4989 4990 return std::vector<unsigned>(); 4991} 4992 4993std::pair<unsigned, const TargetRegisterClass*> 4994X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 4995 MVT::ValueType VT) const { 4996 // Use the default implementation in TargetLowering to convert the register 4997 // constraint into a member of a register class. 4998 std::pair<unsigned, const TargetRegisterClass*> Res; 4999 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 5000 5001 // Not found as a standard register? 5002 if (Res.second == 0) { 5003 // GCC calls "st(0)" just plain "st". 5004 if (StringsEqualNoCase("{st}", Constraint)) { 5005 Res.first = X86::ST0; 5006 Res.second = X86::RSTRegisterClass; 5007 } 5008 5009 return Res; 5010 } 5011 5012 // Otherwise, check to see if this is a register class of the wrong value 5013 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to 5014 // turn into {ax},{dx}. 5015 if (Res.second->hasType(VT)) 5016 return Res; // Correct type already, nothing to do. 5017 5018 // All of the single-register GCC register classes map their values onto 5019 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we 5020 // really want an 8-bit or 32-bit register, map to the appropriate register 5021 // class and return the appropriate register. 5022 if (Res.second != X86::GR16RegisterClass) 5023 return Res; 5024 5025 if (VT == MVT::i8) { 5026 unsigned DestReg = 0; 5027 switch (Res.first) { 5028 default: break; 5029 case X86::AX: DestReg = X86::AL; break; 5030 case X86::DX: DestReg = X86::DL; break; 5031 case X86::CX: DestReg = X86::CL; break; 5032 case X86::BX: DestReg = X86::BL; break; 5033 } 5034 if (DestReg) { 5035 Res.first = DestReg; 5036 Res.second = Res.second = X86::GR8RegisterClass; 5037 } 5038 } else if (VT == MVT::i32) { 5039 unsigned DestReg = 0; 5040 switch (Res.first) { 5041 default: break; 5042 case X86::AX: DestReg = X86::EAX; break; 5043 case X86::DX: DestReg = X86::EDX; break; 5044 case X86::CX: DestReg = X86::ECX; break; 5045 case X86::BX: DestReg = X86::EBX; break; 5046 case X86::SI: DestReg = X86::ESI; break; 5047 case X86::DI: DestReg = X86::EDI; break; 5048 case X86::BP: DestReg = X86::EBP; break; 5049 case X86::SP: DestReg = X86::ESP; break; 5050 } 5051 if (DestReg) { 5052 Res.first = DestReg; 5053 Res.second = Res.second = X86::GR32RegisterClass; 5054 } 5055 } else if (VT == MVT::i64) { 5056 unsigned DestReg = 0; 5057 switch (Res.first) { 5058 default: break; 5059 case X86::AX: DestReg = X86::RAX; break; 5060 case X86::DX: DestReg = X86::RDX; break; 5061 case X86::CX: DestReg = X86::RCX; break; 5062 case X86::BX: DestReg = X86::RBX; break; 5063 case X86::SI: DestReg = X86::RSI; break; 5064 case X86::DI: DestReg = X86::RDI; break; 5065 case X86::BP: DestReg = X86::RBP; break; 5066 case X86::SP: DestReg = X86::RSP; break; 5067 } 5068 if (DestReg) { 5069 Res.first = DestReg; 5070 Res.second = Res.second = X86::GR64RegisterClass; 5071 } 5072 } 5073 5074 return Res; 5075} 5076