1 //===-- X86InstrInfo.cpp - X86 Instruction Information --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the X86 implementation of the TargetInstrInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "X86InstrInfo.h" 14 #include "X86.h" 15 #include "X86InstrBuilder.h" 16 #include "X86InstrFoldTables.h" 17 #include "X86MachineFunctionInfo.h" 18 #include "X86Subtarget.h" 19 #include "X86TargetMachine.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/Sequence.h" 22 #include "llvm/CodeGen/LiveIntervals.h" 23 #include "llvm/CodeGen/LivePhysRegs.h" 24 #include "llvm/CodeGen/LiveVariables.h" 25 #include "llvm/CodeGen/MachineConstantPool.h" 26 #include "llvm/CodeGen/MachineDominators.h" 27 #include "llvm/CodeGen/MachineFrameInfo.h" 28 #include "llvm/CodeGen/MachineInstrBuilder.h" 29 #include "llvm/CodeGen/MachineModuleInfo.h" 30 #include "llvm/CodeGen/MachineRegisterInfo.h" 31 #include "llvm/CodeGen/StackMaps.h" 32 #include "llvm/IR/DebugInfoMetadata.h" 33 #include "llvm/IR/DerivedTypes.h" 34 #include "llvm/IR/Function.h" 35 #include "llvm/MC/MCAsmInfo.h" 36 #include "llvm/MC/MCExpr.h" 37 #include "llvm/MC/MCInst.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/Debug.h" 40 #include "llvm/Support/ErrorHandling.h" 41 #include "llvm/Support/raw_ostream.h" 42 #include "llvm/Target/TargetOptions.h" 43 44 using namespace llvm; 45 46 #define DEBUG_TYPE "x86-instr-info" 47 48 #define GET_INSTRINFO_CTOR_DTOR 49 #include "X86GenInstrInfo.inc" 50 51 static cl::opt<bool> 52 NoFusing("disable-spill-fusing", 53 cl::desc("Disable fusing of spill code into instructions"), 54 cl::Hidden); 55 static cl::opt<bool> 56 PrintFailedFusing("print-failed-fuse-candidates", 57 cl::desc("Print instructions that the allocator wants to" 58 " fuse, but the X86 backend currently can't"), 59 cl::Hidden); 60 static cl::opt<bool> 61 ReMatPICStubLoad("remat-pic-stub-load", 62 cl::desc("Re-materialize load from stub in PIC mode"), 63 cl::init(false), cl::Hidden); 64 static cl::opt<unsigned> 65 PartialRegUpdateClearance("partial-reg-update-clearance", 66 cl::desc("Clearance between two register writes " 67 "for inserting XOR to avoid partial " 68 "register update"), 69 cl::init(64), cl::Hidden); 70 static cl::opt<unsigned> 71 UndefRegClearance("undef-reg-clearance", 72 cl::desc("How many idle instructions we would like before " 73 "certain undef register reads"), 74 cl::init(128), cl::Hidden); 75 76 77 // Pin the vtable to this file. 78 void X86InstrInfo::anchor() {} 79 80 X86InstrInfo::X86InstrInfo(X86Subtarget &STI) 81 : X86GenInstrInfo((STI.isTarget64BitLP64() ? X86::ADJCALLSTACKDOWN64 82 : X86::ADJCALLSTACKDOWN32), 83 (STI.isTarget64BitLP64() ? X86::ADJCALLSTACKUP64 84 : X86::ADJCALLSTACKUP32), 85 X86::CATCHRET, 86 (STI.is64Bit() ? X86::RET64 : X86::RET32)), 87 Subtarget(STI), RI(STI.getTargetTriple()) { 88 } 89 90 bool 91 X86InstrInfo::isCoalescableExtInstr(const MachineInstr &MI, 92 Register &SrcReg, Register &DstReg, 93 unsigned &SubIdx) const { 94 switch (MI.getOpcode()) { 95 default: break; 96 case X86::MOVSX16rr8: 97 case X86::MOVZX16rr8: 98 case X86::MOVSX32rr8: 99 case X86::MOVZX32rr8: 100 case X86::MOVSX64rr8: 101 if (!Subtarget.is64Bit()) 102 // It's not always legal to reference the low 8-bit of the larger 103 // register in 32-bit mode. 104 return false; 105 LLVM_FALLTHROUGH; 106 case X86::MOVSX32rr16: 107 case X86::MOVZX32rr16: 108 case X86::MOVSX64rr16: 109 case X86::MOVSX64rr32: { 110 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg()) 111 // Be conservative. 112 return false; 113 SrcReg = MI.getOperand(1).getReg(); 114 DstReg = MI.getOperand(0).getReg(); 115 switch (MI.getOpcode()) { 116 default: llvm_unreachable("Unreachable!"); 117 case X86::MOVSX16rr8: 118 case X86::MOVZX16rr8: 119 case X86::MOVSX32rr8: 120 case X86::MOVZX32rr8: 121 case X86::MOVSX64rr8: 122 SubIdx = X86::sub_8bit; 123 break; 124 case X86::MOVSX32rr16: 125 case X86::MOVZX32rr16: 126 case X86::MOVSX64rr16: 127 SubIdx = X86::sub_16bit; 128 break; 129 case X86::MOVSX64rr32: 130 SubIdx = X86::sub_32bit; 131 break; 132 } 133 return true; 134 } 135 } 136 return false; 137 } 138 139 bool X86InstrInfo::isDataInvariant(MachineInstr &MI) { 140 switch (MI.getOpcode()) { 141 default: 142 // By default, assume that the instruction is not data invariant. 143 return false; 144 145 // Some target-independent operations that trivially lower to data-invariant 146 // instructions. 147 case TargetOpcode::COPY: 148 case TargetOpcode::INSERT_SUBREG: 149 case TargetOpcode::SUBREG_TO_REG: 150 return true; 151 152 // On x86 it is believed that imul is constant time w.r.t. the loaded data. 153 // However, they set flags and are perhaps the most surprisingly constant 154 // time operations so we call them out here separately. 155 case X86::IMUL16rr: 156 case X86::IMUL16rri8: 157 case X86::IMUL16rri: 158 case X86::IMUL32rr: 159 case X86::IMUL32rri8: 160 case X86::IMUL32rri: 161 case X86::IMUL64rr: 162 case X86::IMUL64rri32: 163 case X86::IMUL64rri8: 164 165 // Bit scanning and counting instructions that are somewhat surprisingly 166 // constant time as they scan across bits and do other fairly complex 167 // operations like popcnt, but are believed to be constant time on x86. 168 // However, these set flags. 169 case X86::BSF16rr: 170 case X86::BSF32rr: 171 case X86::BSF64rr: 172 case X86::BSR16rr: 173 case X86::BSR32rr: 174 case X86::BSR64rr: 175 case X86::LZCNT16rr: 176 case X86::LZCNT32rr: 177 case X86::LZCNT64rr: 178 case X86::POPCNT16rr: 179 case X86::POPCNT32rr: 180 case X86::POPCNT64rr: 181 case X86::TZCNT16rr: 182 case X86::TZCNT32rr: 183 case X86::TZCNT64rr: 184 185 // Bit manipulation instructions are effectively combinations of basic 186 // arithmetic ops, and should still execute in constant time. These also 187 // set flags. 188 case X86::BLCFILL32rr: 189 case X86::BLCFILL64rr: 190 case X86::BLCI32rr: 191 case X86::BLCI64rr: 192 case X86::BLCIC32rr: 193 case X86::BLCIC64rr: 194 case X86::BLCMSK32rr: 195 case X86::BLCMSK64rr: 196 case X86::BLCS32rr: 197 case X86::BLCS64rr: 198 case X86::BLSFILL32rr: 199 case X86::BLSFILL64rr: 200 case X86::BLSI32rr: 201 case X86::BLSI64rr: 202 case X86::BLSIC32rr: 203 case X86::BLSIC64rr: 204 case X86::BLSMSK32rr: 205 case X86::BLSMSK64rr: 206 case X86::BLSR32rr: 207 case X86::BLSR64rr: 208 case X86::TZMSK32rr: 209 case X86::TZMSK64rr: 210 211 // Bit extracting and clearing instructions should execute in constant time, 212 // and set flags. 213 case X86::BEXTR32rr: 214 case X86::BEXTR64rr: 215 case X86::BEXTRI32ri: 216 case X86::BEXTRI64ri: 217 case X86::BZHI32rr: 218 case X86::BZHI64rr: 219 220 // Shift and rotate. 221 case X86::ROL8r1: 222 case X86::ROL16r1: 223 case X86::ROL32r1: 224 case X86::ROL64r1: 225 case X86::ROL8rCL: 226 case X86::ROL16rCL: 227 case X86::ROL32rCL: 228 case X86::ROL64rCL: 229 case X86::ROL8ri: 230 case X86::ROL16ri: 231 case X86::ROL32ri: 232 case X86::ROL64ri: 233 case X86::ROR8r1: 234 case X86::ROR16r1: 235 case X86::ROR32r1: 236 case X86::ROR64r1: 237 case X86::ROR8rCL: 238 case X86::ROR16rCL: 239 case X86::ROR32rCL: 240 case X86::ROR64rCL: 241 case X86::ROR8ri: 242 case X86::ROR16ri: 243 case X86::ROR32ri: 244 case X86::ROR64ri: 245 case X86::SAR8r1: 246 case X86::SAR16r1: 247 case X86::SAR32r1: 248 case X86::SAR64r1: 249 case X86::SAR8rCL: 250 case X86::SAR16rCL: 251 case X86::SAR32rCL: 252 case X86::SAR64rCL: 253 case X86::SAR8ri: 254 case X86::SAR16ri: 255 case X86::SAR32ri: 256 case X86::SAR64ri: 257 case X86::SHL8r1: 258 case X86::SHL16r1: 259 case X86::SHL32r1: 260 case X86::SHL64r1: 261 case X86::SHL8rCL: 262 case X86::SHL16rCL: 263 case X86::SHL32rCL: 264 case X86::SHL64rCL: 265 case X86::SHL8ri: 266 case X86::SHL16ri: 267 case X86::SHL32ri: 268 case X86::SHL64ri: 269 case X86::SHR8r1: 270 case X86::SHR16r1: 271 case X86::SHR32r1: 272 case X86::SHR64r1: 273 case X86::SHR8rCL: 274 case X86::SHR16rCL: 275 case X86::SHR32rCL: 276 case X86::SHR64rCL: 277 case X86::SHR8ri: 278 case X86::SHR16ri: 279 case X86::SHR32ri: 280 case X86::SHR64ri: 281 case X86::SHLD16rrCL: 282 case X86::SHLD32rrCL: 283 case X86::SHLD64rrCL: 284 case X86::SHLD16rri8: 285 case X86::SHLD32rri8: 286 case X86::SHLD64rri8: 287 case X86::SHRD16rrCL: 288 case X86::SHRD32rrCL: 289 case X86::SHRD64rrCL: 290 case X86::SHRD16rri8: 291 case X86::SHRD32rri8: 292 case X86::SHRD64rri8: 293 294 // Basic arithmetic is constant time on the input but does set flags. 295 case X86::ADC8rr: 296 case X86::ADC8ri: 297 case X86::ADC16rr: 298 case X86::ADC16ri: 299 case X86::ADC16ri8: 300 case X86::ADC32rr: 301 case X86::ADC32ri: 302 case X86::ADC32ri8: 303 case X86::ADC64rr: 304 case X86::ADC64ri8: 305 case X86::ADC64ri32: 306 case X86::ADD8rr: 307 case X86::ADD8ri: 308 case X86::ADD16rr: 309 case X86::ADD16ri: 310 case X86::ADD16ri8: 311 case X86::ADD32rr: 312 case X86::ADD32ri: 313 case X86::ADD32ri8: 314 case X86::ADD64rr: 315 case X86::ADD64ri8: 316 case X86::ADD64ri32: 317 case X86::AND8rr: 318 case X86::AND8ri: 319 case X86::AND16rr: 320 case X86::AND16ri: 321 case X86::AND16ri8: 322 case X86::AND32rr: 323 case X86::AND32ri: 324 case X86::AND32ri8: 325 case X86::AND64rr: 326 case X86::AND64ri8: 327 case X86::AND64ri32: 328 case X86::OR8rr: 329 case X86::OR8ri: 330 case X86::OR16rr: 331 case X86::OR16ri: 332 case X86::OR16ri8: 333 case X86::OR32rr: 334 case X86::OR32ri: 335 case X86::OR32ri8: 336 case X86::OR64rr: 337 case X86::OR64ri8: 338 case X86::OR64ri32: 339 case X86::SBB8rr: 340 case X86::SBB8ri: 341 case X86::SBB16rr: 342 case X86::SBB16ri: 343 case X86::SBB16ri8: 344 case X86::SBB32rr: 345 case X86::SBB32ri: 346 case X86::SBB32ri8: 347 case X86::SBB64rr: 348 case X86::SBB64ri8: 349 case X86::SBB64ri32: 350 case X86::SUB8rr: 351 case X86::SUB8ri: 352 case X86::SUB16rr: 353 case X86::SUB16ri: 354 case X86::SUB16ri8: 355 case X86::SUB32rr: 356 case X86::SUB32ri: 357 case X86::SUB32ri8: 358 case X86::SUB64rr: 359 case X86::SUB64ri8: 360 case X86::SUB64ri32: 361 case X86::XOR8rr: 362 case X86::XOR8ri: 363 case X86::XOR16rr: 364 case X86::XOR16ri: 365 case X86::XOR16ri8: 366 case X86::XOR32rr: 367 case X86::XOR32ri: 368 case X86::XOR32ri8: 369 case X86::XOR64rr: 370 case X86::XOR64ri8: 371 case X86::XOR64ri32: 372 // Arithmetic with just 32-bit and 64-bit variants and no immediates. 373 case X86::ADCX32rr: 374 case X86::ADCX64rr: 375 case X86::ADOX32rr: 376 case X86::ADOX64rr: 377 case X86::ANDN32rr: 378 case X86::ANDN64rr: 379 // Unary arithmetic operations. 380 case X86::DEC8r: 381 case X86::DEC16r: 382 case X86::DEC32r: 383 case X86::DEC64r: 384 case X86::INC8r: 385 case X86::INC16r: 386 case X86::INC32r: 387 case X86::INC64r: 388 case X86::NEG8r: 389 case X86::NEG16r: 390 case X86::NEG32r: 391 case X86::NEG64r: 392 393 // Unlike other arithmetic, NOT doesn't set EFLAGS. 394 case X86::NOT8r: 395 case X86::NOT16r: 396 case X86::NOT32r: 397 case X86::NOT64r: 398 399 // Various move instructions used to zero or sign extend things. Note that we 400 // intentionally don't support the _NOREX variants as we can't handle that 401 // register constraint anyways. 402 case X86::MOVSX16rr8: 403 case X86::MOVSX32rr8: 404 case X86::MOVSX32rr16: 405 case X86::MOVSX64rr8: 406 case X86::MOVSX64rr16: 407 case X86::MOVSX64rr32: 408 case X86::MOVZX16rr8: 409 case X86::MOVZX32rr8: 410 case X86::MOVZX32rr16: 411 case X86::MOVZX64rr8: 412 case X86::MOVZX64rr16: 413 case X86::MOV32rr: 414 415 // Arithmetic instructions that are both constant time and don't set flags. 416 case X86::RORX32ri: 417 case X86::RORX64ri: 418 case X86::SARX32rr: 419 case X86::SARX64rr: 420 case X86::SHLX32rr: 421 case X86::SHLX64rr: 422 case X86::SHRX32rr: 423 case X86::SHRX64rr: 424 425 // LEA doesn't actually access memory, and its arithmetic is constant time. 426 case X86::LEA16r: 427 case X86::LEA32r: 428 case X86::LEA64_32r: 429 case X86::LEA64r: 430 return true; 431 } 432 } 433 434 bool X86InstrInfo::isDataInvariantLoad(MachineInstr &MI) { 435 switch (MI.getOpcode()) { 436 default: 437 // By default, assume that the load will immediately leak. 438 return false; 439 440 // On x86 it is believed that imul is constant time w.r.t. the loaded data. 441 // However, they set flags and are perhaps the most surprisingly constant 442 // time operations so we call them out here separately. 443 case X86::IMUL16rm: 444 case X86::IMUL16rmi8: 445 case X86::IMUL16rmi: 446 case X86::IMUL32rm: 447 case X86::IMUL32rmi8: 448 case X86::IMUL32rmi: 449 case X86::IMUL64rm: 450 case X86::IMUL64rmi32: 451 case X86::IMUL64rmi8: 452 453 // Bit scanning and counting instructions that are somewhat surprisingly 454 // constant time as they scan across bits and do other fairly complex 455 // operations like popcnt, but are believed to be constant time on x86. 456 // However, these set flags. 457 case X86::BSF16rm: 458 case X86::BSF32rm: 459 case X86::BSF64rm: 460 case X86::BSR16rm: 461 case X86::BSR32rm: 462 case X86::BSR64rm: 463 case X86::LZCNT16rm: 464 case X86::LZCNT32rm: 465 case X86::LZCNT64rm: 466 case X86::POPCNT16rm: 467 case X86::POPCNT32rm: 468 case X86::POPCNT64rm: 469 case X86::TZCNT16rm: 470 case X86::TZCNT32rm: 471 case X86::TZCNT64rm: 472 473 // Bit manipulation instructions are effectively combinations of basic 474 // arithmetic ops, and should still execute in constant time. These also 475 // set flags. 476 case X86::BLCFILL32rm: 477 case X86::BLCFILL64rm: 478 case X86::BLCI32rm: 479 case X86::BLCI64rm: 480 case X86::BLCIC32rm: 481 case X86::BLCIC64rm: 482 case X86::BLCMSK32rm: 483 case X86::BLCMSK64rm: 484 case X86::BLCS32rm: 485 case X86::BLCS64rm: 486 case X86::BLSFILL32rm: 487 case X86::BLSFILL64rm: 488 case X86::BLSI32rm: 489 case X86::BLSI64rm: 490 case X86::BLSIC32rm: 491 case X86::BLSIC64rm: 492 case X86::BLSMSK32rm: 493 case X86::BLSMSK64rm: 494 case X86::BLSR32rm: 495 case X86::BLSR64rm: 496 case X86::TZMSK32rm: 497 case X86::TZMSK64rm: 498 499 // Bit extracting and clearing instructions should execute in constant time, 500 // and set flags. 501 case X86::BEXTR32rm: 502 case X86::BEXTR64rm: 503 case X86::BEXTRI32mi: 504 case X86::BEXTRI64mi: 505 case X86::BZHI32rm: 506 case X86::BZHI64rm: 507 508 // Basic arithmetic is constant time on the input but does set flags. 509 case X86::ADC8rm: 510 case X86::ADC16rm: 511 case X86::ADC32rm: 512 case X86::ADC64rm: 513 case X86::ADCX32rm: 514 case X86::ADCX64rm: 515 case X86::ADD8rm: 516 case X86::ADD16rm: 517 case X86::ADD32rm: 518 case X86::ADD64rm: 519 case X86::ADOX32rm: 520 case X86::ADOX64rm: 521 case X86::AND8rm: 522 case X86::AND16rm: 523 case X86::AND32rm: 524 case X86::AND64rm: 525 case X86::ANDN32rm: 526 case X86::ANDN64rm: 527 case X86::OR8rm: 528 case X86::OR16rm: 529 case X86::OR32rm: 530 case X86::OR64rm: 531 case X86::SBB8rm: 532 case X86::SBB16rm: 533 case X86::SBB32rm: 534 case X86::SBB64rm: 535 case X86::SUB8rm: 536 case X86::SUB16rm: 537 case X86::SUB32rm: 538 case X86::SUB64rm: 539 case X86::XOR8rm: 540 case X86::XOR16rm: 541 case X86::XOR32rm: 542 case X86::XOR64rm: 543 544 // Integer multiply w/o affecting flags is still believed to be constant 545 // time on x86. Called out separately as this is among the most surprising 546 // instructions to exhibit that behavior. 547 case X86::MULX32rm: 548 case X86::MULX64rm: 549 550 // Arithmetic instructions that are both constant time and don't set flags. 551 case X86::RORX32mi: 552 case X86::RORX64mi: 553 case X86::SARX32rm: 554 case X86::SARX64rm: 555 case X86::SHLX32rm: 556 case X86::SHLX64rm: 557 case X86::SHRX32rm: 558 case X86::SHRX64rm: 559 560 // Conversions are believed to be constant time and don't set flags. 561 case X86::CVTTSD2SI64rm: 562 case X86::VCVTTSD2SI64rm: 563 case X86::VCVTTSD2SI64Zrm: 564 case X86::CVTTSD2SIrm: 565 case X86::VCVTTSD2SIrm: 566 case X86::VCVTTSD2SIZrm: 567 case X86::CVTTSS2SI64rm: 568 case X86::VCVTTSS2SI64rm: 569 case X86::VCVTTSS2SI64Zrm: 570 case X86::CVTTSS2SIrm: 571 case X86::VCVTTSS2SIrm: 572 case X86::VCVTTSS2SIZrm: 573 case X86::CVTSI2SDrm: 574 case X86::VCVTSI2SDrm: 575 case X86::VCVTSI2SDZrm: 576 case X86::CVTSI2SSrm: 577 case X86::VCVTSI2SSrm: 578 case X86::VCVTSI2SSZrm: 579 case X86::CVTSI642SDrm: 580 case X86::VCVTSI642SDrm: 581 case X86::VCVTSI642SDZrm: 582 case X86::CVTSI642SSrm: 583 case X86::VCVTSI642SSrm: 584 case X86::VCVTSI642SSZrm: 585 case X86::CVTSS2SDrm: 586 case X86::VCVTSS2SDrm: 587 case X86::VCVTSS2SDZrm: 588 case X86::CVTSD2SSrm: 589 case X86::VCVTSD2SSrm: 590 case X86::VCVTSD2SSZrm: 591 // AVX512 added unsigned integer conversions. 592 case X86::VCVTTSD2USI64Zrm: 593 case X86::VCVTTSD2USIZrm: 594 case X86::VCVTTSS2USI64Zrm: 595 case X86::VCVTTSS2USIZrm: 596 case X86::VCVTUSI2SDZrm: 597 case X86::VCVTUSI642SDZrm: 598 case X86::VCVTUSI2SSZrm: 599 case X86::VCVTUSI642SSZrm: 600 601 // Loads to register don't set flags. 602 case X86::MOV8rm: 603 case X86::MOV8rm_NOREX: 604 case X86::MOV16rm: 605 case X86::MOV32rm: 606 case X86::MOV64rm: 607 case X86::MOVSX16rm8: 608 case X86::MOVSX32rm16: 609 case X86::MOVSX32rm8: 610 case X86::MOVSX32rm8_NOREX: 611 case X86::MOVSX64rm16: 612 case X86::MOVSX64rm32: 613 case X86::MOVSX64rm8: 614 case X86::MOVZX16rm8: 615 case X86::MOVZX32rm16: 616 case X86::MOVZX32rm8: 617 case X86::MOVZX32rm8_NOREX: 618 case X86::MOVZX64rm16: 619 case X86::MOVZX64rm8: 620 return true; 621 } 622 } 623 624 int X86InstrInfo::getSPAdjust(const MachineInstr &MI) const { 625 const MachineFunction *MF = MI.getParent()->getParent(); 626 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering(); 627 628 if (isFrameInstr(MI)) { 629 int SPAdj = alignTo(getFrameSize(MI), TFI->getStackAlign()); 630 SPAdj -= getFrameAdjustment(MI); 631 if (!isFrameSetup(MI)) 632 SPAdj = -SPAdj; 633 return SPAdj; 634 } 635 636 // To know whether a call adjusts the stack, we need information 637 // that is bound to the following ADJCALLSTACKUP pseudo. 638 // Look for the next ADJCALLSTACKUP that follows the call. 639 if (MI.isCall()) { 640 const MachineBasicBlock *MBB = MI.getParent(); 641 auto I = ++MachineBasicBlock::const_iterator(MI); 642 for (auto E = MBB->end(); I != E; ++I) { 643 if (I->getOpcode() == getCallFrameDestroyOpcode() || 644 I->isCall()) 645 break; 646 } 647 648 // If we could not find a frame destroy opcode, then it has already 649 // been simplified, so we don't care. 650 if (I->getOpcode() != getCallFrameDestroyOpcode()) 651 return 0; 652 653 return -(I->getOperand(1).getImm()); 654 } 655 656 // Currently handle only PUSHes we can reasonably expect to see 657 // in call sequences 658 switch (MI.getOpcode()) { 659 default: 660 return 0; 661 case X86::PUSH32i8: 662 case X86::PUSH32r: 663 case X86::PUSH32rmm: 664 case X86::PUSH32rmr: 665 case X86::PUSHi32: 666 return 4; 667 case X86::PUSH64i8: 668 case X86::PUSH64r: 669 case X86::PUSH64rmm: 670 case X86::PUSH64rmr: 671 case X86::PUSH64i32: 672 return 8; 673 } 674 } 675 676 /// Return true and the FrameIndex if the specified 677 /// operand and follow operands form a reference to the stack frame. 678 bool X86InstrInfo::isFrameOperand(const MachineInstr &MI, unsigned int Op, 679 int &FrameIndex) const { 680 if (MI.getOperand(Op + X86::AddrBaseReg).isFI() && 681 MI.getOperand(Op + X86::AddrScaleAmt).isImm() && 682 MI.getOperand(Op + X86::AddrIndexReg).isReg() && 683 MI.getOperand(Op + X86::AddrDisp).isImm() && 684 MI.getOperand(Op + X86::AddrScaleAmt).getImm() == 1 && 685 MI.getOperand(Op + X86::AddrIndexReg).getReg() == 0 && 686 MI.getOperand(Op + X86::AddrDisp).getImm() == 0) { 687 FrameIndex = MI.getOperand(Op + X86::AddrBaseReg).getIndex(); 688 return true; 689 } 690 return false; 691 } 692 693 static bool isFrameLoadOpcode(int Opcode, unsigned &MemBytes) { 694 switch (Opcode) { 695 default: 696 return false; 697 case X86::MOV8rm: 698 case X86::KMOVBkm: 699 MemBytes = 1; 700 return true; 701 case X86::MOV16rm: 702 case X86::KMOVWkm: 703 case X86::VMOVSHZrm: 704 case X86::VMOVSHZrm_alt: 705 MemBytes = 2; 706 return true; 707 case X86::MOV32rm: 708 case X86::MOVSSrm: 709 case X86::MOVSSrm_alt: 710 case X86::VMOVSSrm: 711 case X86::VMOVSSrm_alt: 712 case X86::VMOVSSZrm: 713 case X86::VMOVSSZrm_alt: 714 case X86::KMOVDkm: 715 MemBytes = 4; 716 return true; 717 case X86::MOV64rm: 718 case X86::LD_Fp64m: 719 case X86::MOVSDrm: 720 case X86::MOVSDrm_alt: 721 case X86::VMOVSDrm: 722 case X86::VMOVSDrm_alt: 723 case X86::VMOVSDZrm: 724 case X86::VMOVSDZrm_alt: 725 case X86::MMX_MOVD64rm: 726 case X86::MMX_MOVQ64rm: 727 case X86::KMOVQkm: 728 MemBytes = 8; 729 return true; 730 case X86::MOVAPSrm: 731 case X86::MOVUPSrm: 732 case X86::MOVAPDrm: 733 case X86::MOVUPDrm: 734 case X86::MOVDQArm: 735 case X86::MOVDQUrm: 736 case X86::VMOVAPSrm: 737 case X86::VMOVUPSrm: 738 case X86::VMOVAPDrm: 739 case X86::VMOVUPDrm: 740 case X86::VMOVDQArm: 741 case X86::VMOVDQUrm: 742 case X86::VMOVAPSZ128rm: 743 case X86::VMOVUPSZ128rm: 744 case X86::VMOVAPSZ128rm_NOVLX: 745 case X86::VMOVUPSZ128rm_NOVLX: 746 case X86::VMOVAPDZ128rm: 747 case X86::VMOVUPDZ128rm: 748 case X86::VMOVDQU8Z128rm: 749 case X86::VMOVDQU16Z128rm: 750 case X86::VMOVDQA32Z128rm: 751 case X86::VMOVDQU32Z128rm: 752 case X86::VMOVDQA64Z128rm: 753 case X86::VMOVDQU64Z128rm: 754 MemBytes = 16; 755 return true; 756 case X86::VMOVAPSYrm: 757 case X86::VMOVUPSYrm: 758 case X86::VMOVAPDYrm: 759 case X86::VMOVUPDYrm: 760 case X86::VMOVDQAYrm: 761 case X86::VMOVDQUYrm: 762 case X86::VMOVAPSZ256rm: 763 case X86::VMOVUPSZ256rm: 764 case X86::VMOVAPSZ256rm_NOVLX: 765 case X86::VMOVUPSZ256rm_NOVLX: 766 case X86::VMOVAPDZ256rm: 767 case X86::VMOVUPDZ256rm: 768 case X86::VMOVDQU8Z256rm: 769 case X86::VMOVDQU16Z256rm: 770 case X86::VMOVDQA32Z256rm: 771 case X86::VMOVDQU32Z256rm: 772 case X86::VMOVDQA64Z256rm: 773 case X86::VMOVDQU64Z256rm: 774 MemBytes = 32; 775 return true; 776 case X86::VMOVAPSZrm: 777 case X86::VMOVUPSZrm: 778 case X86::VMOVAPDZrm: 779 case X86::VMOVUPDZrm: 780 case X86::VMOVDQU8Zrm: 781 case X86::VMOVDQU16Zrm: 782 case X86::VMOVDQA32Zrm: 783 case X86::VMOVDQU32Zrm: 784 case X86::VMOVDQA64Zrm: 785 case X86::VMOVDQU64Zrm: 786 MemBytes = 64; 787 return true; 788 } 789 } 790 791 static bool isFrameStoreOpcode(int Opcode, unsigned &MemBytes) { 792 switch (Opcode) { 793 default: 794 return false; 795 case X86::MOV8mr: 796 case X86::KMOVBmk: 797 MemBytes = 1; 798 return true; 799 case X86::MOV16mr: 800 case X86::KMOVWmk: 801 case X86::VMOVSHZmr: 802 MemBytes = 2; 803 return true; 804 case X86::MOV32mr: 805 case X86::MOVSSmr: 806 case X86::VMOVSSmr: 807 case X86::VMOVSSZmr: 808 case X86::KMOVDmk: 809 MemBytes = 4; 810 return true; 811 case X86::MOV64mr: 812 case X86::ST_FpP64m: 813 case X86::MOVSDmr: 814 case X86::VMOVSDmr: 815 case X86::VMOVSDZmr: 816 case X86::MMX_MOVD64mr: 817 case X86::MMX_MOVQ64mr: 818 case X86::MMX_MOVNTQmr: 819 case X86::KMOVQmk: 820 MemBytes = 8; 821 return true; 822 case X86::MOVAPSmr: 823 case X86::MOVUPSmr: 824 case X86::MOVAPDmr: 825 case X86::MOVUPDmr: 826 case X86::MOVDQAmr: 827 case X86::MOVDQUmr: 828 case X86::VMOVAPSmr: 829 case X86::VMOVUPSmr: 830 case X86::VMOVAPDmr: 831 case X86::VMOVUPDmr: 832 case X86::VMOVDQAmr: 833 case X86::VMOVDQUmr: 834 case X86::VMOVUPSZ128mr: 835 case X86::VMOVAPSZ128mr: 836 case X86::VMOVUPSZ128mr_NOVLX: 837 case X86::VMOVAPSZ128mr_NOVLX: 838 case X86::VMOVUPDZ128mr: 839 case X86::VMOVAPDZ128mr: 840 case X86::VMOVDQA32Z128mr: 841 case X86::VMOVDQU32Z128mr: 842 case X86::VMOVDQA64Z128mr: 843 case X86::VMOVDQU64Z128mr: 844 case X86::VMOVDQU8Z128mr: 845 case X86::VMOVDQU16Z128mr: 846 MemBytes = 16; 847 return true; 848 case X86::VMOVUPSYmr: 849 case X86::VMOVAPSYmr: 850 case X86::VMOVUPDYmr: 851 case X86::VMOVAPDYmr: 852 case X86::VMOVDQUYmr: 853 case X86::VMOVDQAYmr: 854 case X86::VMOVUPSZ256mr: 855 case X86::VMOVAPSZ256mr: 856 case X86::VMOVUPSZ256mr_NOVLX: 857 case X86::VMOVAPSZ256mr_NOVLX: 858 case X86::VMOVUPDZ256mr: 859 case X86::VMOVAPDZ256mr: 860 case X86::VMOVDQU8Z256mr: 861 case X86::VMOVDQU16Z256mr: 862 case X86::VMOVDQA32Z256mr: 863 case X86::VMOVDQU32Z256mr: 864 case X86::VMOVDQA64Z256mr: 865 case X86::VMOVDQU64Z256mr: 866 MemBytes = 32; 867 return true; 868 case X86::VMOVUPSZmr: 869 case X86::VMOVAPSZmr: 870 case X86::VMOVUPDZmr: 871 case X86::VMOVAPDZmr: 872 case X86::VMOVDQU8Zmr: 873 case X86::VMOVDQU16Zmr: 874 case X86::VMOVDQA32Zmr: 875 case X86::VMOVDQU32Zmr: 876 case X86::VMOVDQA64Zmr: 877 case X86::VMOVDQU64Zmr: 878 MemBytes = 64; 879 return true; 880 } 881 return false; 882 } 883 884 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 885 int &FrameIndex) const { 886 unsigned Dummy; 887 return X86InstrInfo::isLoadFromStackSlot(MI, FrameIndex, Dummy); 888 } 889 890 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 891 int &FrameIndex, 892 unsigned &MemBytes) const { 893 if (isFrameLoadOpcode(MI.getOpcode(), MemBytes)) 894 if (MI.getOperand(0).getSubReg() == 0 && isFrameOperand(MI, 1, FrameIndex)) 895 return MI.getOperand(0).getReg(); 896 return 0; 897 } 898 899 unsigned X86InstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI, 900 int &FrameIndex) const { 901 unsigned Dummy; 902 if (isFrameLoadOpcode(MI.getOpcode(), Dummy)) { 903 unsigned Reg; 904 if ((Reg = isLoadFromStackSlot(MI, FrameIndex))) 905 return Reg; 906 // Check for post-frame index elimination operations 907 SmallVector<const MachineMemOperand *, 1> Accesses; 908 if (hasLoadFromStackSlot(MI, Accesses)) { 909 FrameIndex = 910 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue()) 911 ->getFrameIndex(); 912 return MI.getOperand(0).getReg(); 913 } 914 } 915 return 0; 916 } 917 918 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr &MI, 919 int &FrameIndex) const { 920 unsigned Dummy; 921 return X86InstrInfo::isStoreToStackSlot(MI, FrameIndex, Dummy); 922 } 923 924 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr &MI, 925 int &FrameIndex, 926 unsigned &MemBytes) const { 927 if (isFrameStoreOpcode(MI.getOpcode(), MemBytes)) 928 if (MI.getOperand(X86::AddrNumOperands).getSubReg() == 0 && 929 isFrameOperand(MI, 0, FrameIndex)) 930 return MI.getOperand(X86::AddrNumOperands).getReg(); 931 return 0; 932 } 933 934 unsigned X86InstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI, 935 int &FrameIndex) const { 936 unsigned Dummy; 937 if (isFrameStoreOpcode(MI.getOpcode(), Dummy)) { 938 unsigned Reg; 939 if ((Reg = isStoreToStackSlot(MI, FrameIndex))) 940 return Reg; 941 // Check for post-frame index elimination operations 942 SmallVector<const MachineMemOperand *, 1> Accesses; 943 if (hasStoreToStackSlot(MI, Accesses)) { 944 FrameIndex = 945 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue()) 946 ->getFrameIndex(); 947 return MI.getOperand(X86::AddrNumOperands).getReg(); 948 } 949 } 950 return 0; 951 } 952 953 /// Return true if register is PIC base; i.e.g defined by X86::MOVPC32r. 954 static bool regIsPICBase(Register BaseReg, const MachineRegisterInfo &MRI) { 955 // Don't waste compile time scanning use-def chains of physregs. 956 if (!BaseReg.isVirtual()) 957 return false; 958 bool isPICBase = false; 959 for (MachineRegisterInfo::def_instr_iterator I = MRI.def_instr_begin(BaseReg), 960 E = MRI.def_instr_end(); I != E; ++I) { 961 MachineInstr *DefMI = &*I; 962 if (DefMI->getOpcode() != X86::MOVPC32r) 963 return false; 964 assert(!isPICBase && "More than one PIC base?"); 965 isPICBase = true; 966 } 967 return isPICBase; 968 } 969 970 bool X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, 971 AAResults *AA) const { 972 switch (MI.getOpcode()) { 973 default: 974 // This function should only be called for opcodes with the ReMaterializable 975 // flag set. 976 llvm_unreachable("Unknown rematerializable operation!"); 977 break; 978 979 case X86::LOAD_STACK_GUARD: 980 case X86::AVX1_SETALLONES: 981 case X86::AVX2_SETALLONES: 982 case X86::AVX512_128_SET0: 983 case X86::AVX512_256_SET0: 984 case X86::AVX512_512_SET0: 985 case X86::AVX512_512_SETALLONES: 986 case X86::AVX512_FsFLD0SD: 987 case X86::AVX512_FsFLD0SH: 988 case X86::AVX512_FsFLD0SS: 989 case X86::AVX512_FsFLD0F128: 990 case X86::AVX_SET0: 991 case X86::FsFLD0SD: 992 case X86::FsFLD0SS: 993 case X86::FsFLD0F128: 994 case X86::KSET0D: 995 case X86::KSET0Q: 996 case X86::KSET0W: 997 case X86::KSET1D: 998 case X86::KSET1Q: 999 case X86::KSET1W: 1000 case X86::MMX_SET0: 1001 case X86::MOV32ImmSExti8: 1002 case X86::MOV32r0: 1003 case X86::MOV32r1: 1004 case X86::MOV32r_1: 1005 case X86::MOV32ri64: 1006 case X86::MOV64ImmSExti8: 1007 case X86::V_SET0: 1008 case X86::V_SETALLONES: 1009 case X86::MOV16ri: 1010 case X86::MOV32ri: 1011 case X86::MOV64ri: 1012 case X86::MOV64ri32: 1013 case X86::MOV8ri: 1014 case X86::PTILEZEROV: 1015 return true; 1016 1017 case X86::MOV8rm: 1018 case X86::MOV8rm_NOREX: 1019 case X86::MOV16rm: 1020 case X86::MOV32rm: 1021 case X86::MOV64rm: 1022 case X86::MOVSSrm: 1023 case X86::MOVSSrm_alt: 1024 case X86::MOVSDrm: 1025 case X86::MOVSDrm_alt: 1026 case X86::MOVAPSrm: 1027 case X86::MOVUPSrm: 1028 case X86::MOVAPDrm: 1029 case X86::MOVUPDrm: 1030 case X86::MOVDQArm: 1031 case X86::MOVDQUrm: 1032 case X86::VMOVSSrm: 1033 case X86::VMOVSSrm_alt: 1034 case X86::VMOVSDrm: 1035 case X86::VMOVSDrm_alt: 1036 case X86::VMOVAPSrm: 1037 case X86::VMOVUPSrm: 1038 case X86::VMOVAPDrm: 1039 case X86::VMOVUPDrm: 1040 case X86::VMOVDQArm: 1041 case X86::VMOVDQUrm: 1042 case X86::VMOVAPSYrm: 1043 case X86::VMOVUPSYrm: 1044 case X86::VMOVAPDYrm: 1045 case X86::VMOVUPDYrm: 1046 case X86::VMOVDQAYrm: 1047 case X86::VMOVDQUYrm: 1048 case X86::MMX_MOVD64rm: 1049 case X86::MMX_MOVQ64rm: 1050 // AVX-512 1051 case X86::VMOVSSZrm: 1052 case X86::VMOVSSZrm_alt: 1053 case X86::VMOVSDZrm: 1054 case X86::VMOVSDZrm_alt: 1055 case X86::VMOVSHZrm: 1056 case X86::VMOVSHZrm_alt: 1057 case X86::VMOVAPDZ128rm: 1058 case X86::VMOVAPDZ256rm: 1059 case X86::VMOVAPDZrm: 1060 case X86::VMOVAPSZ128rm: 1061 case X86::VMOVAPSZ256rm: 1062 case X86::VMOVAPSZ128rm_NOVLX: 1063 case X86::VMOVAPSZ256rm_NOVLX: 1064 case X86::VMOVAPSZrm: 1065 case X86::VMOVDQA32Z128rm: 1066 case X86::VMOVDQA32Z256rm: 1067 case X86::VMOVDQA32Zrm: 1068 case X86::VMOVDQA64Z128rm: 1069 case X86::VMOVDQA64Z256rm: 1070 case X86::VMOVDQA64Zrm: 1071 case X86::VMOVDQU16Z128rm: 1072 case X86::VMOVDQU16Z256rm: 1073 case X86::VMOVDQU16Zrm: 1074 case X86::VMOVDQU32Z128rm: 1075 case X86::VMOVDQU32Z256rm: 1076 case X86::VMOVDQU32Zrm: 1077 case X86::VMOVDQU64Z128rm: 1078 case X86::VMOVDQU64Z256rm: 1079 case X86::VMOVDQU64Zrm: 1080 case X86::VMOVDQU8Z128rm: 1081 case X86::VMOVDQU8Z256rm: 1082 case X86::VMOVDQU8Zrm: 1083 case X86::VMOVUPDZ128rm: 1084 case X86::VMOVUPDZ256rm: 1085 case X86::VMOVUPDZrm: 1086 case X86::VMOVUPSZ128rm: 1087 case X86::VMOVUPSZ256rm: 1088 case X86::VMOVUPSZ128rm_NOVLX: 1089 case X86::VMOVUPSZ256rm_NOVLX: 1090 case X86::VMOVUPSZrm: { 1091 // Loads from constant pools are trivially rematerializable. 1092 if (MI.getOperand(1 + X86::AddrBaseReg).isReg() && 1093 MI.getOperand(1 + X86::AddrScaleAmt).isImm() && 1094 MI.getOperand(1 + X86::AddrIndexReg).isReg() && 1095 MI.getOperand(1 + X86::AddrIndexReg).getReg() == 0 && 1096 MI.isDereferenceableInvariantLoad(AA)) { 1097 Register BaseReg = MI.getOperand(1 + X86::AddrBaseReg).getReg(); 1098 if (BaseReg == 0 || BaseReg == X86::RIP) 1099 return true; 1100 // Allow re-materialization of PIC load. 1101 if (!ReMatPICStubLoad && MI.getOperand(1 + X86::AddrDisp).isGlobal()) 1102 return false; 1103 const MachineFunction &MF = *MI.getParent()->getParent(); 1104 const MachineRegisterInfo &MRI = MF.getRegInfo(); 1105 return regIsPICBase(BaseReg, MRI); 1106 } 1107 return false; 1108 } 1109 1110 case X86::LEA32r: 1111 case X86::LEA64r: { 1112 if (MI.getOperand(1 + X86::AddrScaleAmt).isImm() && 1113 MI.getOperand(1 + X86::AddrIndexReg).isReg() && 1114 MI.getOperand(1 + X86::AddrIndexReg).getReg() == 0 && 1115 !MI.getOperand(1 + X86::AddrDisp).isReg()) { 1116 // lea fi#, lea GV, etc. are all rematerializable. 1117 if (!MI.getOperand(1 + X86::AddrBaseReg).isReg()) 1118 return true; 1119 Register BaseReg = MI.getOperand(1 + X86::AddrBaseReg).getReg(); 1120 if (BaseReg == 0) 1121 return true; 1122 // Allow re-materialization of lea PICBase + x. 1123 const MachineFunction &MF = *MI.getParent()->getParent(); 1124 const MachineRegisterInfo &MRI = MF.getRegInfo(); 1125 return regIsPICBase(BaseReg, MRI); 1126 } 1127 return false; 1128 } 1129 } 1130 } 1131 1132 void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB, 1133 MachineBasicBlock::iterator I, 1134 Register DestReg, unsigned SubIdx, 1135 const MachineInstr &Orig, 1136 const TargetRegisterInfo &TRI) const { 1137 bool ClobbersEFLAGS = Orig.modifiesRegister(X86::EFLAGS, &TRI); 1138 if (ClobbersEFLAGS && MBB.computeRegisterLiveness(&TRI, X86::EFLAGS, I) != 1139 MachineBasicBlock::LQR_Dead) { 1140 // The instruction clobbers EFLAGS. Re-materialize as MOV32ri to avoid side 1141 // effects. 1142 int Value; 1143 switch (Orig.getOpcode()) { 1144 case X86::MOV32r0: Value = 0; break; 1145 case X86::MOV32r1: Value = 1; break; 1146 case X86::MOV32r_1: Value = -1; break; 1147 default: 1148 llvm_unreachable("Unexpected instruction!"); 1149 } 1150 1151 const DebugLoc &DL = Orig.getDebugLoc(); 1152 BuildMI(MBB, I, DL, get(X86::MOV32ri)) 1153 .add(Orig.getOperand(0)) 1154 .addImm(Value); 1155 } else { 1156 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig); 1157 MBB.insert(I, MI); 1158 } 1159 1160 MachineInstr &NewMI = *std::prev(I); 1161 NewMI.substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI); 1162 } 1163 1164 /// True if MI has a condition code def, e.g. EFLAGS, that is not marked dead. 1165 bool X86InstrInfo::hasLiveCondCodeDef(MachineInstr &MI) const { 1166 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 1167 MachineOperand &MO = MI.getOperand(i); 1168 if (MO.isReg() && MO.isDef() && 1169 MO.getReg() == X86::EFLAGS && !MO.isDead()) { 1170 return true; 1171 } 1172 } 1173 return false; 1174 } 1175 1176 /// Check whether the shift count for a machine operand is non-zero. 1177 inline static unsigned getTruncatedShiftCount(const MachineInstr &MI, 1178 unsigned ShiftAmtOperandIdx) { 1179 // The shift count is six bits with the REX.W prefix and five bits without. 1180 unsigned ShiftCountMask = (MI.getDesc().TSFlags & X86II::REX_W) ? 63 : 31; 1181 unsigned Imm = MI.getOperand(ShiftAmtOperandIdx).getImm(); 1182 return Imm & ShiftCountMask; 1183 } 1184 1185 /// Check whether the given shift count is appropriate 1186 /// can be represented by a LEA instruction. 1187 inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt) { 1188 // Left shift instructions can be transformed into load-effective-address 1189 // instructions if we can encode them appropriately. 1190 // A LEA instruction utilizes a SIB byte to encode its scale factor. 1191 // The SIB.scale field is two bits wide which means that we can encode any 1192 // shift amount less than 4. 1193 return ShAmt < 4 && ShAmt > 0; 1194 } 1195 1196 bool X86InstrInfo::classifyLEAReg(MachineInstr &MI, const MachineOperand &Src, 1197 unsigned Opc, bool AllowSP, Register &NewSrc, 1198 bool &isKill, MachineOperand &ImplicitOp, 1199 LiveVariables *LV, LiveIntervals *LIS) const { 1200 MachineFunction &MF = *MI.getParent()->getParent(); 1201 const TargetRegisterClass *RC; 1202 if (AllowSP) { 1203 RC = Opc != X86::LEA32r ? &X86::GR64RegClass : &X86::GR32RegClass; 1204 } else { 1205 RC = Opc != X86::LEA32r ? 1206 &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass; 1207 } 1208 Register SrcReg = Src.getReg(); 1209 isKill = MI.killsRegister(SrcReg); 1210 1211 // For both LEA64 and LEA32 the register already has essentially the right 1212 // type (32-bit or 64-bit) we may just need to forbid SP. 1213 if (Opc != X86::LEA64_32r) { 1214 NewSrc = SrcReg; 1215 assert(!Src.isUndef() && "Undef op doesn't need optimization"); 1216 1217 if (NewSrc.isVirtual() && !MF.getRegInfo().constrainRegClass(NewSrc, RC)) 1218 return false; 1219 1220 return true; 1221 } 1222 1223 // This is for an LEA64_32r and incoming registers are 32-bit. One way or 1224 // another we need to add 64-bit registers to the final MI. 1225 if (SrcReg.isPhysical()) { 1226 ImplicitOp = Src; 1227 ImplicitOp.setImplicit(); 1228 1229 NewSrc = getX86SubSuperRegister(SrcReg, 64); 1230 assert(!Src.isUndef() && "Undef op doesn't need optimization"); 1231 } else { 1232 // Virtual register of the wrong class, we have to create a temporary 64-bit 1233 // vreg to feed into the LEA. 1234 NewSrc = MF.getRegInfo().createVirtualRegister(RC); 1235 MachineInstr *Copy = 1236 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(TargetOpcode::COPY)) 1237 .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit) 1238 .addReg(SrcReg, getKillRegState(isKill)); 1239 1240 // Which is obviously going to be dead after we're done with it. 1241 isKill = true; 1242 1243 if (LV) 1244 LV->replaceKillInstruction(SrcReg, MI, *Copy); 1245 1246 if (LIS) { 1247 SlotIndex CopyIdx = LIS->InsertMachineInstrInMaps(*Copy); 1248 SlotIndex Idx = LIS->getInstructionIndex(MI); 1249 LiveInterval &LI = LIS->getInterval(SrcReg); 1250 LiveRange::Segment *S = LI.getSegmentContaining(Idx); 1251 if (S->end.getBaseIndex() == Idx) 1252 S->end = CopyIdx.getRegSlot(); 1253 } 1254 } 1255 1256 // We've set all the parameters without issue. 1257 return true; 1258 } 1259 1260 MachineInstr *X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc, 1261 MachineInstr &MI, 1262 LiveVariables *LV, 1263 LiveIntervals *LIS, 1264 bool Is8BitOp) const { 1265 // We handle 8-bit adds and various 16-bit opcodes in the switch below. 1266 MachineBasicBlock &MBB = *MI.getParent(); 1267 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo(); 1268 assert((Is8BitOp || RegInfo.getTargetRegisterInfo()->getRegSizeInBits( 1269 *RegInfo.getRegClass(MI.getOperand(0).getReg())) == 16) && 1270 "Unexpected type for LEA transform"); 1271 1272 // TODO: For a 32-bit target, we need to adjust the LEA variables with 1273 // something like this: 1274 // Opcode = X86::LEA32r; 1275 // InRegLEA = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass); 1276 // OutRegLEA = 1277 // Is8BitOp ? RegInfo.createVirtualRegister(&X86::GR32ABCD_RegClass) 1278 // : RegInfo.createVirtualRegister(&X86::GR32RegClass); 1279 if (!Subtarget.is64Bit()) 1280 return nullptr; 1281 1282 unsigned Opcode = X86::LEA64_32r; 1283 Register InRegLEA = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass); 1284 Register OutRegLEA = RegInfo.createVirtualRegister(&X86::GR32RegClass); 1285 Register InRegLEA2; 1286 1287 // Build and insert into an implicit UNDEF value. This is OK because 1288 // we will be shifting and then extracting the lower 8/16-bits. 1289 // This has the potential to cause partial register stall. e.g. 1290 // movw (%rbp,%rcx,2), %dx 1291 // leal -65(%rdx), %esi 1292 // But testing has shown this *does* help performance in 64-bit mode (at 1293 // least on modern x86 machines). 1294 MachineBasicBlock::iterator MBBI = MI.getIterator(); 1295 Register Dest = MI.getOperand(0).getReg(); 1296 Register Src = MI.getOperand(1).getReg(); 1297 Register Src2; 1298 bool IsDead = MI.getOperand(0).isDead(); 1299 bool IsKill = MI.getOperand(1).isKill(); 1300 unsigned SubReg = Is8BitOp ? X86::sub_8bit : X86::sub_16bit; 1301 assert(!MI.getOperand(1).isUndef() && "Undef op doesn't need optimization"); 1302 MachineInstr *ImpDef = 1303 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(X86::IMPLICIT_DEF), InRegLEA); 1304 MachineInstr *InsMI = 1305 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(TargetOpcode::COPY)) 1306 .addReg(InRegLEA, RegState::Define, SubReg) 1307 .addReg(Src, getKillRegState(IsKill)); 1308 MachineInstr *ImpDef2 = nullptr; 1309 MachineInstr *InsMI2 = nullptr; 1310 1311 MachineInstrBuilder MIB = 1312 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(Opcode), OutRegLEA); 1313 switch (MIOpc) { 1314 default: llvm_unreachable("Unreachable!"); 1315 case X86::SHL8ri: 1316 case X86::SHL16ri: { 1317 unsigned ShAmt = MI.getOperand(2).getImm(); 1318 MIB.addReg(0).addImm(1ULL << ShAmt) 1319 .addReg(InRegLEA, RegState::Kill).addImm(0).addReg(0); 1320 break; 1321 } 1322 case X86::INC8r: 1323 case X86::INC16r: 1324 addRegOffset(MIB, InRegLEA, true, 1); 1325 break; 1326 case X86::DEC8r: 1327 case X86::DEC16r: 1328 addRegOffset(MIB, InRegLEA, true, -1); 1329 break; 1330 case X86::ADD8ri: 1331 case X86::ADD8ri_DB: 1332 case X86::ADD16ri: 1333 case X86::ADD16ri8: 1334 case X86::ADD16ri_DB: 1335 case X86::ADD16ri8_DB: 1336 addRegOffset(MIB, InRegLEA, true, MI.getOperand(2).getImm()); 1337 break; 1338 case X86::ADD8rr: 1339 case X86::ADD8rr_DB: 1340 case X86::ADD16rr: 1341 case X86::ADD16rr_DB: { 1342 Src2 = MI.getOperand(2).getReg(); 1343 bool IsKill2 = MI.getOperand(2).isKill(); 1344 assert(!MI.getOperand(2).isUndef() && "Undef op doesn't need optimization"); 1345 if (Src == Src2) { 1346 // ADD8rr/ADD16rr killed %reg1028, %reg1028 1347 // just a single insert_subreg. 1348 addRegReg(MIB, InRegLEA, true, InRegLEA, false); 1349 } else { 1350 if (Subtarget.is64Bit()) 1351 InRegLEA2 = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass); 1352 else 1353 InRegLEA2 = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass); 1354 // Build and insert into an implicit UNDEF value. This is OK because 1355 // we will be shifting and then extracting the lower 8/16-bits. 1356 ImpDef2 = BuildMI(MBB, &*MIB, MI.getDebugLoc(), get(X86::IMPLICIT_DEF), 1357 InRegLEA2); 1358 InsMI2 = BuildMI(MBB, &*MIB, MI.getDebugLoc(), get(TargetOpcode::COPY)) 1359 .addReg(InRegLEA2, RegState::Define, SubReg) 1360 .addReg(Src2, getKillRegState(IsKill2)); 1361 addRegReg(MIB, InRegLEA, true, InRegLEA2, true); 1362 } 1363 if (LV && IsKill2 && InsMI2) 1364 LV->replaceKillInstruction(Src2, MI, *InsMI2); 1365 break; 1366 } 1367 } 1368 1369 MachineInstr *NewMI = MIB; 1370 MachineInstr *ExtMI = 1371 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(TargetOpcode::COPY)) 1372 .addReg(Dest, RegState::Define | getDeadRegState(IsDead)) 1373 .addReg(OutRegLEA, RegState::Kill, SubReg); 1374 1375 if (LV) { 1376 // Update live variables. 1377 LV->getVarInfo(InRegLEA).Kills.push_back(NewMI); 1378 LV->getVarInfo(OutRegLEA).Kills.push_back(ExtMI); 1379 if (IsKill) 1380 LV->replaceKillInstruction(Src, MI, *InsMI); 1381 if (IsDead) 1382 LV->replaceKillInstruction(Dest, MI, *ExtMI); 1383 } 1384 1385 if (LIS) { 1386 LIS->InsertMachineInstrInMaps(*ImpDef); 1387 SlotIndex InsIdx = LIS->InsertMachineInstrInMaps(*InsMI); 1388 if (ImpDef2) 1389 LIS->InsertMachineInstrInMaps(*ImpDef2); 1390 SlotIndex Ins2Idx; 1391 if (InsMI2) 1392 Ins2Idx = LIS->InsertMachineInstrInMaps(*InsMI2); 1393 SlotIndex NewIdx = LIS->ReplaceMachineInstrInMaps(MI, *NewMI); 1394 SlotIndex ExtIdx = LIS->InsertMachineInstrInMaps(*ExtMI); 1395 LIS->getInterval(InRegLEA); 1396 LIS->getInterval(OutRegLEA); 1397 if (InRegLEA2) 1398 LIS->getInterval(InRegLEA2); 1399 1400 // Move the use of Src up to InsMI. 1401 LiveInterval &SrcLI = LIS->getInterval(Src); 1402 LiveRange::Segment *SrcSeg = SrcLI.getSegmentContaining(NewIdx); 1403 if (SrcSeg->end == NewIdx.getRegSlot()) 1404 SrcSeg->end = InsIdx.getRegSlot(); 1405 1406 if (InsMI2) { 1407 // Move the use of Src2 up to InsMI2. 1408 LiveInterval &Src2LI = LIS->getInterval(Src2); 1409 LiveRange::Segment *Src2Seg = Src2LI.getSegmentContaining(NewIdx); 1410 if (Src2Seg->end == NewIdx.getRegSlot()) 1411 Src2Seg->end = Ins2Idx.getRegSlot(); 1412 } 1413 1414 // Move the definition of Dest down to ExtMI. 1415 LiveInterval &DestLI = LIS->getInterval(Dest); 1416 LiveRange::Segment *DestSeg = 1417 DestLI.getSegmentContaining(NewIdx.getRegSlot()); 1418 assert(DestSeg->start == NewIdx.getRegSlot() && 1419 DestSeg->valno->def == NewIdx.getRegSlot()); 1420 DestSeg->start = ExtIdx.getRegSlot(); 1421 DestSeg->valno->def = ExtIdx.getRegSlot(); 1422 } 1423 1424 return ExtMI; 1425 } 1426 1427 /// This method must be implemented by targets that 1428 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target 1429 /// may be able to convert a two-address instruction into a true 1430 /// three-address instruction on demand. This allows the X86 target (for 1431 /// example) to convert ADD and SHL instructions into LEA instructions if they 1432 /// would require register copies due to two-addressness. 1433 /// 1434 /// This method returns a null pointer if the transformation cannot be 1435 /// performed, otherwise it returns the new instruction. 1436 /// 1437 MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr &MI, 1438 LiveVariables *LV, 1439 LiveIntervals *LIS) const { 1440 // The following opcodes also sets the condition code register(s). Only 1441 // convert them to equivalent lea if the condition code register def's 1442 // are dead! 1443 if (hasLiveCondCodeDef(MI)) 1444 return nullptr; 1445 1446 MachineFunction &MF = *MI.getParent()->getParent(); 1447 // All instructions input are two-addr instructions. Get the known operands. 1448 const MachineOperand &Dest = MI.getOperand(0); 1449 const MachineOperand &Src = MI.getOperand(1); 1450 1451 // Ideally, operations with undef should be folded before we get here, but we 1452 // can't guarantee it. Bail out because optimizing undefs is a waste of time. 1453 // Without this, we have to forward undef state to new register operands to 1454 // avoid machine verifier errors. 1455 if (Src.isUndef()) 1456 return nullptr; 1457 if (MI.getNumOperands() > 2) 1458 if (MI.getOperand(2).isReg() && MI.getOperand(2).isUndef()) 1459 return nullptr; 1460 1461 MachineInstr *NewMI = nullptr; 1462 Register SrcReg, SrcReg2; 1463 bool Is64Bit = Subtarget.is64Bit(); 1464 1465 bool Is8BitOp = false; 1466 unsigned MIOpc = MI.getOpcode(); 1467 switch (MIOpc) { 1468 default: llvm_unreachable("Unreachable!"); 1469 case X86::SHL64ri: { 1470 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!"); 1471 unsigned ShAmt = getTruncatedShiftCount(MI, 2); 1472 if (!isTruncatedShiftCountForLEA(ShAmt)) return nullptr; 1473 1474 // LEA can't handle RSP. 1475 if (Src.getReg().isVirtual() && !MF.getRegInfo().constrainRegClass( 1476 Src.getReg(), &X86::GR64_NOSPRegClass)) 1477 return nullptr; 1478 1479 NewMI = BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r)) 1480 .add(Dest) 1481 .addReg(0) 1482 .addImm(1ULL << ShAmt) 1483 .add(Src) 1484 .addImm(0) 1485 .addReg(0); 1486 break; 1487 } 1488 case X86::SHL32ri: { 1489 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!"); 1490 unsigned ShAmt = getTruncatedShiftCount(MI, 2); 1491 if (!isTruncatedShiftCountForLEA(ShAmt)) return nullptr; 1492 1493 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r; 1494 1495 // LEA can't handle ESP. 1496 bool isKill; 1497 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false); 1498 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, isKill, 1499 ImplicitOp, LV, LIS)) 1500 return nullptr; 1501 1502 MachineInstrBuilder MIB = 1503 BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1504 .add(Dest) 1505 .addReg(0) 1506 .addImm(1ULL << ShAmt) 1507 .addReg(SrcReg, getKillRegState(isKill)) 1508 .addImm(0) 1509 .addReg(0); 1510 if (ImplicitOp.getReg() != 0) 1511 MIB.add(ImplicitOp); 1512 NewMI = MIB; 1513 1514 break; 1515 } 1516 case X86::SHL8ri: 1517 Is8BitOp = true; 1518 LLVM_FALLTHROUGH; 1519 case X86::SHL16ri: { 1520 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!"); 1521 unsigned ShAmt = getTruncatedShiftCount(MI, 2); 1522 if (!isTruncatedShiftCountForLEA(ShAmt)) 1523 return nullptr; 1524 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp); 1525 } 1526 case X86::INC64r: 1527 case X86::INC32r: { 1528 assert(MI.getNumOperands() >= 2 && "Unknown inc instruction!"); 1529 unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r : 1530 (Is64Bit ? X86::LEA64_32r : X86::LEA32r); 1531 bool isKill; 1532 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false); 1533 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, isKill, 1534 ImplicitOp, LV, LIS)) 1535 return nullptr; 1536 1537 MachineInstrBuilder MIB = 1538 BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1539 .add(Dest) 1540 .addReg(SrcReg, getKillRegState(isKill)); 1541 if (ImplicitOp.getReg() != 0) 1542 MIB.add(ImplicitOp); 1543 1544 NewMI = addOffset(MIB, 1); 1545 break; 1546 } 1547 case X86::DEC64r: 1548 case X86::DEC32r: { 1549 assert(MI.getNumOperands() >= 2 && "Unknown dec instruction!"); 1550 unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r 1551 : (Is64Bit ? X86::LEA64_32r : X86::LEA32r); 1552 1553 bool isKill; 1554 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false); 1555 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, isKill, 1556 ImplicitOp, LV, LIS)) 1557 return nullptr; 1558 1559 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1560 .add(Dest) 1561 .addReg(SrcReg, getKillRegState(isKill)); 1562 if (ImplicitOp.getReg() != 0) 1563 MIB.add(ImplicitOp); 1564 1565 NewMI = addOffset(MIB, -1); 1566 1567 break; 1568 } 1569 case X86::DEC8r: 1570 case X86::INC8r: 1571 Is8BitOp = true; 1572 LLVM_FALLTHROUGH; 1573 case X86::DEC16r: 1574 case X86::INC16r: 1575 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp); 1576 case X86::ADD64rr: 1577 case X86::ADD64rr_DB: 1578 case X86::ADD32rr: 1579 case X86::ADD32rr_DB: { 1580 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!"); 1581 unsigned Opc; 1582 if (MIOpc == X86::ADD64rr || MIOpc == X86::ADD64rr_DB) 1583 Opc = X86::LEA64r; 1584 else 1585 Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r; 1586 1587 const MachineOperand &Src2 = MI.getOperand(2); 1588 bool isKill2; 1589 MachineOperand ImplicitOp2 = MachineOperand::CreateReg(0, false); 1590 if (!classifyLEAReg(MI, Src2, Opc, /*AllowSP=*/false, SrcReg2, isKill2, 1591 ImplicitOp2, LV, LIS)) 1592 return nullptr; 1593 1594 bool isKill; 1595 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false); 1596 if (Src.getReg() == Src2.getReg()) { 1597 // Don't call classify LEAReg a second time on the same register, in case 1598 // the first call inserted a COPY from Src2 and marked it as killed. 1599 isKill = isKill2; 1600 SrcReg = SrcReg2; 1601 } else { 1602 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, isKill, 1603 ImplicitOp, LV, LIS)) 1604 return nullptr; 1605 } 1606 1607 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc)).add(Dest); 1608 if (ImplicitOp.getReg() != 0) 1609 MIB.add(ImplicitOp); 1610 if (ImplicitOp2.getReg() != 0) 1611 MIB.add(ImplicitOp2); 1612 1613 NewMI = addRegReg(MIB, SrcReg, isKill, SrcReg2, isKill2); 1614 if (LV && Src2.isKill()) 1615 LV->replaceKillInstruction(SrcReg2, MI, *NewMI); 1616 break; 1617 } 1618 case X86::ADD8rr: 1619 case X86::ADD8rr_DB: 1620 Is8BitOp = true; 1621 LLVM_FALLTHROUGH; 1622 case X86::ADD16rr: 1623 case X86::ADD16rr_DB: 1624 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp); 1625 case X86::ADD64ri32: 1626 case X86::ADD64ri8: 1627 case X86::ADD64ri32_DB: 1628 case X86::ADD64ri8_DB: 1629 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!"); 1630 NewMI = addOffset( 1631 BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r)).add(Dest).add(Src), 1632 MI.getOperand(2)); 1633 break; 1634 case X86::ADD32ri: 1635 case X86::ADD32ri8: 1636 case X86::ADD32ri_DB: 1637 case X86::ADD32ri8_DB: { 1638 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!"); 1639 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r; 1640 1641 bool isKill; 1642 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false); 1643 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, isKill, 1644 ImplicitOp, LV, LIS)) 1645 return nullptr; 1646 1647 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1648 .add(Dest) 1649 .addReg(SrcReg, getKillRegState(isKill)); 1650 if (ImplicitOp.getReg() != 0) 1651 MIB.add(ImplicitOp); 1652 1653 NewMI = addOffset(MIB, MI.getOperand(2)); 1654 break; 1655 } 1656 case X86::ADD8ri: 1657 case X86::ADD8ri_DB: 1658 Is8BitOp = true; 1659 LLVM_FALLTHROUGH; 1660 case X86::ADD16ri: 1661 case X86::ADD16ri8: 1662 case X86::ADD16ri_DB: 1663 case X86::ADD16ri8_DB: 1664 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp); 1665 case X86::SUB8ri: 1666 case X86::SUB16ri8: 1667 case X86::SUB16ri: 1668 /// FIXME: Support these similar to ADD8ri/ADD16ri*. 1669 return nullptr; 1670 case X86::SUB32ri8: 1671 case X86::SUB32ri: { 1672 if (!MI.getOperand(2).isImm()) 1673 return nullptr; 1674 int64_t Imm = MI.getOperand(2).getImm(); 1675 if (!isInt<32>(-Imm)) 1676 return nullptr; 1677 1678 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!"); 1679 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r; 1680 1681 bool isKill; 1682 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false); 1683 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, isKill, 1684 ImplicitOp, LV, LIS)) 1685 return nullptr; 1686 1687 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1688 .add(Dest) 1689 .addReg(SrcReg, getKillRegState(isKill)); 1690 if (ImplicitOp.getReg() != 0) 1691 MIB.add(ImplicitOp); 1692 1693 NewMI = addOffset(MIB, -Imm); 1694 break; 1695 } 1696 1697 case X86::SUB64ri8: 1698 case X86::SUB64ri32: { 1699 if (!MI.getOperand(2).isImm()) 1700 return nullptr; 1701 int64_t Imm = MI.getOperand(2).getImm(); 1702 if (!isInt<32>(-Imm)) 1703 return nullptr; 1704 1705 assert(MI.getNumOperands() >= 3 && "Unknown sub instruction!"); 1706 1707 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), 1708 get(X86::LEA64r)).add(Dest).add(Src); 1709 NewMI = addOffset(MIB, -Imm); 1710 break; 1711 } 1712 1713 case X86::VMOVDQU8Z128rmk: 1714 case X86::VMOVDQU8Z256rmk: 1715 case X86::VMOVDQU8Zrmk: 1716 case X86::VMOVDQU16Z128rmk: 1717 case X86::VMOVDQU16Z256rmk: 1718 case X86::VMOVDQU16Zrmk: 1719 case X86::VMOVDQU32Z128rmk: case X86::VMOVDQA32Z128rmk: 1720 case X86::VMOVDQU32Z256rmk: case X86::VMOVDQA32Z256rmk: 1721 case X86::VMOVDQU32Zrmk: case X86::VMOVDQA32Zrmk: 1722 case X86::VMOVDQU64Z128rmk: case X86::VMOVDQA64Z128rmk: 1723 case X86::VMOVDQU64Z256rmk: case X86::VMOVDQA64Z256rmk: 1724 case X86::VMOVDQU64Zrmk: case X86::VMOVDQA64Zrmk: 1725 case X86::VMOVUPDZ128rmk: case X86::VMOVAPDZ128rmk: 1726 case X86::VMOVUPDZ256rmk: case X86::VMOVAPDZ256rmk: 1727 case X86::VMOVUPDZrmk: case X86::VMOVAPDZrmk: 1728 case X86::VMOVUPSZ128rmk: case X86::VMOVAPSZ128rmk: 1729 case X86::VMOVUPSZ256rmk: case X86::VMOVAPSZ256rmk: 1730 case X86::VMOVUPSZrmk: case X86::VMOVAPSZrmk: 1731 case X86::VBROADCASTSDZ256rmk: 1732 case X86::VBROADCASTSDZrmk: 1733 case X86::VBROADCASTSSZ128rmk: 1734 case X86::VBROADCASTSSZ256rmk: 1735 case X86::VBROADCASTSSZrmk: 1736 case X86::VPBROADCASTDZ128rmk: 1737 case X86::VPBROADCASTDZ256rmk: 1738 case X86::VPBROADCASTDZrmk: 1739 case X86::VPBROADCASTQZ128rmk: 1740 case X86::VPBROADCASTQZ256rmk: 1741 case X86::VPBROADCASTQZrmk: { 1742 unsigned Opc; 1743 switch (MIOpc) { 1744 default: llvm_unreachable("Unreachable!"); 1745 case X86::VMOVDQU8Z128rmk: Opc = X86::VPBLENDMBZ128rmk; break; 1746 case X86::VMOVDQU8Z256rmk: Opc = X86::VPBLENDMBZ256rmk; break; 1747 case X86::VMOVDQU8Zrmk: Opc = X86::VPBLENDMBZrmk; break; 1748 case X86::VMOVDQU16Z128rmk: Opc = X86::VPBLENDMWZ128rmk; break; 1749 case X86::VMOVDQU16Z256rmk: Opc = X86::VPBLENDMWZ256rmk; break; 1750 case X86::VMOVDQU16Zrmk: Opc = X86::VPBLENDMWZrmk; break; 1751 case X86::VMOVDQU32Z128rmk: Opc = X86::VPBLENDMDZ128rmk; break; 1752 case X86::VMOVDQU32Z256rmk: Opc = X86::VPBLENDMDZ256rmk; break; 1753 case X86::VMOVDQU32Zrmk: Opc = X86::VPBLENDMDZrmk; break; 1754 case X86::VMOVDQU64Z128rmk: Opc = X86::VPBLENDMQZ128rmk; break; 1755 case X86::VMOVDQU64Z256rmk: Opc = X86::VPBLENDMQZ256rmk; break; 1756 case X86::VMOVDQU64Zrmk: Opc = X86::VPBLENDMQZrmk; break; 1757 case X86::VMOVUPDZ128rmk: Opc = X86::VBLENDMPDZ128rmk; break; 1758 case X86::VMOVUPDZ256rmk: Opc = X86::VBLENDMPDZ256rmk; break; 1759 case X86::VMOVUPDZrmk: Opc = X86::VBLENDMPDZrmk; break; 1760 case X86::VMOVUPSZ128rmk: Opc = X86::VBLENDMPSZ128rmk; break; 1761 case X86::VMOVUPSZ256rmk: Opc = X86::VBLENDMPSZ256rmk; break; 1762 case X86::VMOVUPSZrmk: Opc = X86::VBLENDMPSZrmk; break; 1763 case X86::VMOVDQA32Z128rmk: Opc = X86::VPBLENDMDZ128rmk; break; 1764 case X86::VMOVDQA32Z256rmk: Opc = X86::VPBLENDMDZ256rmk; break; 1765 case X86::VMOVDQA32Zrmk: Opc = X86::VPBLENDMDZrmk; break; 1766 case X86::VMOVDQA64Z128rmk: Opc = X86::VPBLENDMQZ128rmk; break; 1767 case X86::VMOVDQA64Z256rmk: Opc = X86::VPBLENDMQZ256rmk; break; 1768 case X86::VMOVDQA64Zrmk: Opc = X86::VPBLENDMQZrmk; break; 1769 case X86::VMOVAPDZ128rmk: Opc = X86::VBLENDMPDZ128rmk; break; 1770 case X86::VMOVAPDZ256rmk: Opc = X86::VBLENDMPDZ256rmk; break; 1771 case X86::VMOVAPDZrmk: Opc = X86::VBLENDMPDZrmk; break; 1772 case X86::VMOVAPSZ128rmk: Opc = X86::VBLENDMPSZ128rmk; break; 1773 case X86::VMOVAPSZ256rmk: Opc = X86::VBLENDMPSZ256rmk; break; 1774 case X86::VMOVAPSZrmk: Opc = X86::VBLENDMPSZrmk; break; 1775 case X86::VBROADCASTSDZ256rmk: Opc = X86::VBLENDMPDZ256rmbk; break; 1776 case X86::VBROADCASTSDZrmk: Opc = X86::VBLENDMPDZrmbk; break; 1777 case X86::VBROADCASTSSZ128rmk: Opc = X86::VBLENDMPSZ128rmbk; break; 1778 case X86::VBROADCASTSSZ256rmk: Opc = X86::VBLENDMPSZ256rmbk; break; 1779 case X86::VBROADCASTSSZrmk: Opc = X86::VBLENDMPSZrmbk; break; 1780 case X86::VPBROADCASTDZ128rmk: Opc = X86::VPBLENDMDZ128rmbk; break; 1781 case X86::VPBROADCASTDZ256rmk: Opc = X86::VPBLENDMDZ256rmbk; break; 1782 case X86::VPBROADCASTDZrmk: Opc = X86::VPBLENDMDZrmbk; break; 1783 case X86::VPBROADCASTQZ128rmk: Opc = X86::VPBLENDMQZ128rmbk; break; 1784 case X86::VPBROADCASTQZ256rmk: Opc = X86::VPBLENDMQZ256rmbk; break; 1785 case X86::VPBROADCASTQZrmk: Opc = X86::VPBLENDMQZrmbk; break; 1786 } 1787 1788 NewMI = BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1789 .add(Dest) 1790 .add(MI.getOperand(2)) 1791 .add(Src) 1792 .add(MI.getOperand(3)) 1793 .add(MI.getOperand(4)) 1794 .add(MI.getOperand(5)) 1795 .add(MI.getOperand(6)) 1796 .add(MI.getOperand(7)); 1797 break; 1798 } 1799 1800 case X86::VMOVDQU8Z128rrk: 1801 case X86::VMOVDQU8Z256rrk: 1802 case X86::VMOVDQU8Zrrk: 1803 case X86::VMOVDQU16Z128rrk: 1804 case X86::VMOVDQU16Z256rrk: 1805 case X86::VMOVDQU16Zrrk: 1806 case X86::VMOVDQU32Z128rrk: case X86::VMOVDQA32Z128rrk: 1807 case X86::VMOVDQU32Z256rrk: case X86::VMOVDQA32Z256rrk: 1808 case X86::VMOVDQU32Zrrk: case X86::VMOVDQA32Zrrk: 1809 case X86::VMOVDQU64Z128rrk: case X86::VMOVDQA64Z128rrk: 1810 case X86::VMOVDQU64Z256rrk: case X86::VMOVDQA64Z256rrk: 1811 case X86::VMOVDQU64Zrrk: case X86::VMOVDQA64Zrrk: 1812 case X86::VMOVUPDZ128rrk: case X86::VMOVAPDZ128rrk: 1813 case X86::VMOVUPDZ256rrk: case X86::VMOVAPDZ256rrk: 1814 case X86::VMOVUPDZrrk: case X86::VMOVAPDZrrk: 1815 case X86::VMOVUPSZ128rrk: case X86::VMOVAPSZ128rrk: 1816 case X86::VMOVUPSZ256rrk: case X86::VMOVAPSZ256rrk: 1817 case X86::VMOVUPSZrrk: case X86::VMOVAPSZrrk: { 1818 unsigned Opc; 1819 switch (MIOpc) { 1820 default: llvm_unreachable("Unreachable!"); 1821 case X86::VMOVDQU8Z128rrk: Opc = X86::VPBLENDMBZ128rrk; break; 1822 case X86::VMOVDQU8Z256rrk: Opc = X86::VPBLENDMBZ256rrk; break; 1823 case X86::VMOVDQU8Zrrk: Opc = X86::VPBLENDMBZrrk; break; 1824 case X86::VMOVDQU16Z128rrk: Opc = X86::VPBLENDMWZ128rrk; break; 1825 case X86::VMOVDQU16Z256rrk: Opc = X86::VPBLENDMWZ256rrk; break; 1826 case X86::VMOVDQU16Zrrk: Opc = X86::VPBLENDMWZrrk; break; 1827 case X86::VMOVDQU32Z128rrk: Opc = X86::VPBLENDMDZ128rrk; break; 1828 case X86::VMOVDQU32Z256rrk: Opc = X86::VPBLENDMDZ256rrk; break; 1829 case X86::VMOVDQU32Zrrk: Opc = X86::VPBLENDMDZrrk; break; 1830 case X86::VMOVDQU64Z128rrk: Opc = X86::VPBLENDMQZ128rrk; break; 1831 case X86::VMOVDQU64Z256rrk: Opc = X86::VPBLENDMQZ256rrk; break; 1832 case X86::VMOVDQU64Zrrk: Opc = X86::VPBLENDMQZrrk; break; 1833 case X86::VMOVUPDZ128rrk: Opc = X86::VBLENDMPDZ128rrk; break; 1834 case X86::VMOVUPDZ256rrk: Opc = X86::VBLENDMPDZ256rrk; break; 1835 case X86::VMOVUPDZrrk: Opc = X86::VBLENDMPDZrrk; break; 1836 case X86::VMOVUPSZ128rrk: Opc = X86::VBLENDMPSZ128rrk; break; 1837 case X86::VMOVUPSZ256rrk: Opc = X86::VBLENDMPSZ256rrk; break; 1838 case X86::VMOVUPSZrrk: Opc = X86::VBLENDMPSZrrk; break; 1839 case X86::VMOVDQA32Z128rrk: Opc = X86::VPBLENDMDZ128rrk; break; 1840 case X86::VMOVDQA32Z256rrk: Opc = X86::VPBLENDMDZ256rrk; break; 1841 case X86::VMOVDQA32Zrrk: Opc = X86::VPBLENDMDZrrk; break; 1842 case X86::VMOVDQA64Z128rrk: Opc = X86::VPBLENDMQZ128rrk; break; 1843 case X86::VMOVDQA64Z256rrk: Opc = X86::VPBLENDMQZ256rrk; break; 1844 case X86::VMOVDQA64Zrrk: Opc = X86::VPBLENDMQZrrk; break; 1845 case X86::VMOVAPDZ128rrk: Opc = X86::VBLENDMPDZ128rrk; break; 1846 case X86::VMOVAPDZ256rrk: Opc = X86::VBLENDMPDZ256rrk; break; 1847 case X86::VMOVAPDZrrk: Opc = X86::VBLENDMPDZrrk; break; 1848 case X86::VMOVAPSZ128rrk: Opc = X86::VBLENDMPSZ128rrk; break; 1849 case X86::VMOVAPSZ256rrk: Opc = X86::VBLENDMPSZ256rrk; break; 1850 case X86::VMOVAPSZrrk: Opc = X86::VBLENDMPSZrrk; break; 1851 } 1852 1853 NewMI = BuildMI(MF, MI.getDebugLoc(), get(Opc)) 1854 .add(Dest) 1855 .add(MI.getOperand(2)) 1856 .add(Src) 1857 .add(MI.getOperand(3)); 1858 break; 1859 } 1860 } 1861 1862 if (!NewMI) return nullptr; 1863 1864 if (LV) { // Update live variables 1865 if (Src.isKill()) 1866 LV->replaceKillInstruction(Src.getReg(), MI, *NewMI); 1867 if (Dest.isDead()) 1868 LV->replaceKillInstruction(Dest.getReg(), MI, *NewMI); 1869 } 1870 1871 MachineBasicBlock &MBB = *MI.getParent(); 1872 MBB.insert(MI.getIterator(), NewMI); // Insert the new inst 1873 1874 if (LIS) { 1875 LIS->ReplaceMachineInstrInMaps(MI, *NewMI); 1876 if (SrcReg) 1877 LIS->getInterval(SrcReg); 1878 if (SrcReg2) 1879 LIS->getInterval(SrcReg2); 1880 } 1881 1882 return NewMI; 1883 } 1884 1885 /// This determines which of three possible cases of a three source commute 1886 /// the source indexes correspond to taking into account any mask operands. 1887 /// All prevents commuting a passthru operand. Returns -1 if the commute isn't 1888 /// possible. 1889 /// Case 0 - Possible to commute the first and second operands. 1890 /// Case 1 - Possible to commute the first and third operands. 1891 /// Case 2 - Possible to commute the second and third operands. 1892 static unsigned getThreeSrcCommuteCase(uint64_t TSFlags, unsigned SrcOpIdx1, 1893 unsigned SrcOpIdx2) { 1894 // Put the lowest index to SrcOpIdx1 to simplify the checks below. 1895 if (SrcOpIdx1 > SrcOpIdx2) 1896 std::swap(SrcOpIdx1, SrcOpIdx2); 1897 1898 unsigned Op1 = 1, Op2 = 2, Op3 = 3; 1899 if (X86II::isKMasked(TSFlags)) { 1900 Op2++; 1901 Op3++; 1902 } 1903 1904 if (SrcOpIdx1 == Op1 && SrcOpIdx2 == Op2) 1905 return 0; 1906 if (SrcOpIdx1 == Op1 && SrcOpIdx2 == Op3) 1907 return 1; 1908 if (SrcOpIdx1 == Op2 && SrcOpIdx2 == Op3) 1909 return 2; 1910 llvm_unreachable("Unknown three src commute case."); 1911 } 1912 1913 unsigned X86InstrInfo::getFMA3OpcodeToCommuteOperands( 1914 const MachineInstr &MI, unsigned SrcOpIdx1, unsigned SrcOpIdx2, 1915 const X86InstrFMA3Group &FMA3Group) const { 1916 1917 unsigned Opc = MI.getOpcode(); 1918 1919 // TODO: Commuting the 1st operand of FMA*_Int requires some additional 1920 // analysis. The commute optimization is legal only if all users of FMA*_Int 1921 // use only the lowest element of the FMA*_Int instruction. Such analysis are 1922 // not implemented yet. So, just return 0 in that case. 1923 // When such analysis are available this place will be the right place for 1924 // calling it. 1925 assert(!(FMA3Group.isIntrinsic() && (SrcOpIdx1 == 1 || SrcOpIdx2 == 1)) && 1926 "Intrinsic instructions can't commute operand 1"); 1927 1928 // Determine which case this commute is or if it can't be done. 1929 unsigned Case = getThreeSrcCommuteCase(MI.getDesc().TSFlags, SrcOpIdx1, 1930 SrcOpIdx2); 1931 assert(Case < 3 && "Unexpected case number!"); 1932 1933 // Define the FMA forms mapping array that helps to map input FMA form 1934 // to output FMA form to preserve the operation semantics after 1935 // commuting the operands. 1936 const unsigned Form132Index = 0; 1937 const unsigned Form213Index = 1; 1938 const unsigned Form231Index = 2; 1939 static const unsigned FormMapping[][3] = { 1940 // 0: SrcOpIdx1 == 1 && SrcOpIdx2 == 2; 1941 // FMA132 A, C, b; ==> FMA231 C, A, b; 1942 // FMA213 B, A, c; ==> FMA213 A, B, c; 1943 // FMA231 C, A, b; ==> FMA132 A, C, b; 1944 { Form231Index, Form213Index, Form132Index }, 1945 // 1: SrcOpIdx1 == 1 && SrcOpIdx2 == 3; 1946 // FMA132 A, c, B; ==> FMA132 B, c, A; 1947 // FMA213 B, a, C; ==> FMA231 C, a, B; 1948 // FMA231 C, a, B; ==> FMA213 B, a, C; 1949 { Form132Index, Form231Index, Form213Index }, 1950 // 2: SrcOpIdx1 == 2 && SrcOpIdx2 == 3; 1951 // FMA132 a, C, B; ==> FMA213 a, B, C; 1952 // FMA213 b, A, C; ==> FMA132 b, C, A; 1953 // FMA231 c, A, B; ==> FMA231 c, B, A; 1954 { Form213Index, Form132Index, Form231Index } 1955 }; 1956 1957 unsigned FMAForms[3]; 1958 FMAForms[0] = FMA3Group.get132Opcode(); 1959 FMAForms[1] = FMA3Group.get213Opcode(); 1960 FMAForms[2] = FMA3Group.get231Opcode(); 1961 unsigned FormIndex; 1962 for (FormIndex = 0; FormIndex < 3; FormIndex++) 1963 if (Opc == FMAForms[FormIndex]) 1964 break; 1965 1966 // Everything is ready, just adjust the FMA opcode and return it. 1967 FormIndex = FormMapping[Case][FormIndex]; 1968 return FMAForms[FormIndex]; 1969 } 1970 1971 static void commuteVPTERNLOG(MachineInstr &MI, unsigned SrcOpIdx1, 1972 unsigned SrcOpIdx2) { 1973 // Determine which case this commute is or if it can't be done. 1974 unsigned Case = getThreeSrcCommuteCase(MI.getDesc().TSFlags, SrcOpIdx1, 1975 SrcOpIdx2); 1976 assert(Case < 3 && "Unexpected case value!"); 1977 1978 // For each case we need to swap two pairs of bits in the final immediate. 1979 static const uint8_t SwapMasks[3][4] = { 1980 { 0x04, 0x10, 0x08, 0x20 }, // Swap bits 2/4 and 3/5. 1981 { 0x02, 0x10, 0x08, 0x40 }, // Swap bits 1/4 and 3/6. 1982 { 0x02, 0x04, 0x20, 0x40 }, // Swap bits 1/2 and 5/6. 1983 }; 1984 1985 uint8_t Imm = MI.getOperand(MI.getNumOperands()-1).getImm(); 1986 // Clear out the bits we are swapping. 1987 uint8_t NewImm = Imm & ~(SwapMasks[Case][0] | SwapMasks[Case][1] | 1988 SwapMasks[Case][2] | SwapMasks[Case][3]); 1989 // If the immediate had a bit of the pair set, then set the opposite bit. 1990 if (Imm & SwapMasks[Case][0]) NewImm |= SwapMasks[Case][1]; 1991 if (Imm & SwapMasks[Case][1]) NewImm |= SwapMasks[Case][0]; 1992 if (Imm & SwapMasks[Case][2]) NewImm |= SwapMasks[Case][3]; 1993 if (Imm & SwapMasks[Case][3]) NewImm |= SwapMasks[Case][2]; 1994 MI.getOperand(MI.getNumOperands()-1).setImm(NewImm); 1995 } 1996 1997 // Returns true if this is a VPERMI2 or VPERMT2 instruction that can be 1998 // commuted. 1999 static bool isCommutableVPERMV3Instruction(unsigned Opcode) { 2000 #define VPERM_CASES(Suffix) \ 2001 case X86::VPERMI2##Suffix##128rr: case X86::VPERMT2##Suffix##128rr: \ 2002 case X86::VPERMI2##Suffix##256rr: case X86::VPERMT2##Suffix##256rr: \ 2003 case X86::VPERMI2##Suffix##rr: case X86::VPERMT2##Suffix##rr: \ 2004 case X86::VPERMI2##Suffix##128rm: case X86::VPERMT2##Suffix##128rm: \ 2005 case X86::VPERMI2##Suffix##256rm: case X86::VPERMT2##Suffix##256rm: \ 2006 case X86::VPERMI2##Suffix##rm: case X86::VPERMT2##Suffix##rm: \ 2007 case X86::VPERMI2##Suffix##128rrkz: case X86::VPERMT2##Suffix##128rrkz: \ 2008 case X86::VPERMI2##Suffix##256rrkz: case X86::VPERMT2##Suffix##256rrkz: \ 2009 case X86::VPERMI2##Suffix##rrkz: case X86::VPERMT2##Suffix##rrkz: \ 2010 case X86::VPERMI2##Suffix##128rmkz: case X86::VPERMT2##Suffix##128rmkz: \ 2011 case X86::VPERMI2##Suffix##256rmkz: case X86::VPERMT2##Suffix##256rmkz: \ 2012 case X86::VPERMI2##Suffix##rmkz: case X86::VPERMT2##Suffix##rmkz: 2013 2014 #define VPERM_CASES_BROADCAST(Suffix) \ 2015 VPERM_CASES(Suffix) \ 2016 case X86::VPERMI2##Suffix##128rmb: case X86::VPERMT2##Suffix##128rmb: \ 2017 case X86::VPERMI2##Suffix##256rmb: case X86::VPERMT2##Suffix##256rmb: \ 2018 case X86::VPERMI2##Suffix##rmb: case X86::VPERMT2##Suffix##rmb: \ 2019 case X86::VPERMI2##Suffix##128rmbkz: case X86::VPERMT2##Suffix##128rmbkz: \ 2020 case X86::VPERMI2##Suffix##256rmbkz: case X86::VPERMT2##Suffix##256rmbkz: \ 2021 case X86::VPERMI2##Suffix##rmbkz: case X86::VPERMT2##Suffix##rmbkz: 2022 2023 switch (Opcode) { 2024 default: return false; 2025 VPERM_CASES(B) 2026 VPERM_CASES_BROADCAST(D) 2027 VPERM_CASES_BROADCAST(PD) 2028 VPERM_CASES_BROADCAST(PS) 2029 VPERM_CASES_BROADCAST(Q) 2030 VPERM_CASES(W) 2031 return true; 2032 } 2033 #undef VPERM_CASES_BROADCAST 2034 #undef VPERM_CASES 2035 } 2036 2037 // Returns commuted opcode for VPERMI2 and VPERMT2 instructions by switching 2038 // from the I opcode to the T opcode and vice versa. 2039 static unsigned getCommutedVPERMV3Opcode(unsigned Opcode) { 2040 #define VPERM_CASES(Orig, New) \ 2041 case X86::Orig##128rr: return X86::New##128rr; \ 2042 case X86::Orig##128rrkz: return X86::New##128rrkz; \ 2043 case X86::Orig##128rm: return X86::New##128rm; \ 2044 case X86::Orig##128rmkz: return X86::New##128rmkz; \ 2045 case X86::Orig##256rr: return X86::New##256rr; \ 2046 case X86::Orig##256rrkz: return X86::New##256rrkz; \ 2047 case X86::Orig##256rm: return X86::New##256rm; \ 2048 case X86::Orig##256rmkz: return X86::New##256rmkz; \ 2049 case X86::Orig##rr: return X86::New##rr; \ 2050 case X86::Orig##rrkz: return X86::New##rrkz; \ 2051 case X86::Orig##rm: return X86::New##rm; \ 2052 case X86::Orig##rmkz: return X86::New##rmkz; 2053 2054 #define VPERM_CASES_BROADCAST(Orig, New) \ 2055 VPERM_CASES(Orig, New) \ 2056 case X86::Orig##128rmb: return X86::New##128rmb; \ 2057 case X86::Orig##128rmbkz: return X86::New##128rmbkz; \ 2058 case X86::Orig##256rmb: return X86::New##256rmb; \ 2059 case X86::Orig##256rmbkz: return X86::New##256rmbkz; \ 2060 case X86::Orig##rmb: return X86::New##rmb; \ 2061 case X86::Orig##rmbkz: return X86::New##rmbkz; 2062 2063 switch (Opcode) { 2064 VPERM_CASES(VPERMI2B, VPERMT2B) 2065 VPERM_CASES_BROADCAST(VPERMI2D, VPERMT2D) 2066 VPERM_CASES_BROADCAST(VPERMI2PD, VPERMT2PD) 2067 VPERM_CASES_BROADCAST(VPERMI2PS, VPERMT2PS) 2068 VPERM_CASES_BROADCAST(VPERMI2Q, VPERMT2Q) 2069 VPERM_CASES(VPERMI2W, VPERMT2W) 2070 VPERM_CASES(VPERMT2B, VPERMI2B) 2071 VPERM_CASES_BROADCAST(VPERMT2D, VPERMI2D) 2072 VPERM_CASES_BROADCAST(VPERMT2PD, VPERMI2PD) 2073 VPERM_CASES_BROADCAST(VPERMT2PS, VPERMI2PS) 2074 VPERM_CASES_BROADCAST(VPERMT2Q, VPERMI2Q) 2075 VPERM_CASES(VPERMT2W, VPERMI2W) 2076 } 2077 2078 llvm_unreachable("Unreachable!"); 2079 #undef VPERM_CASES_BROADCAST 2080 #undef VPERM_CASES 2081 } 2082 2083 MachineInstr *X86InstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 2084 unsigned OpIdx1, 2085 unsigned OpIdx2) const { 2086 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & { 2087 if (NewMI) 2088 return *MI.getParent()->getParent()->CloneMachineInstr(&MI); 2089 return MI; 2090 }; 2091 2092 switch (MI.getOpcode()) { 2093 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I) 2094 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I) 2095 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I) 2096 case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I) 2097 case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I) 2098 case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I) 2099 unsigned Opc; 2100 unsigned Size; 2101 switch (MI.getOpcode()) { 2102 default: llvm_unreachable("Unreachable!"); 2103 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break; 2104 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break; 2105 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break; 2106 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break; 2107 case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break; 2108 case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break; 2109 } 2110 unsigned Amt = MI.getOperand(3).getImm(); 2111 auto &WorkingMI = cloneIfNew(MI); 2112 WorkingMI.setDesc(get(Opc)); 2113 WorkingMI.getOperand(3).setImm(Size - Amt); 2114 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2115 OpIdx1, OpIdx2); 2116 } 2117 case X86::PFSUBrr: 2118 case X86::PFSUBRrr: { 2119 // PFSUB x, y: x = x - y 2120 // PFSUBR x, y: x = y - x 2121 unsigned Opc = 2122 (X86::PFSUBRrr == MI.getOpcode() ? X86::PFSUBrr : X86::PFSUBRrr); 2123 auto &WorkingMI = cloneIfNew(MI); 2124 WorkingMI.setDesc(get(Opc)); 2125 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2126 OpIdx1, OpIdx2); 2127 } 2128 case X86::BLENDPDrri: 2129 case X86::BLENDPSrri: 2130 case X86::VBLENDPDrri: 2131 case X86::VBLENDPSrri: 2132 // If we're optimizing for size, try to use MOVSD/MOVSS. 2133 if (MI.getParent()->getParent()->getFunction().hasOptSize()) { 2134 unsigned Mask, Opc; 2135 switch (MI.getOpcode()) { 2136 default: llvm_unreachable("Unreachable!"); 2137 case X86::BLENDPDrri: Opc = X86::MOVSDrr; Mask = 0x03; break; 2138 case X86::BLENDPSrri: Opc = X86::MOVSSrr; Mask = 0x0F; break; 2139 case X86::VBLENDPDrri: Opc = X86::VMOVSDrr; Mask = 0x03; break; 2140 case X86::VBLENDPSrri: Opc = X86::VMOVSSrr; Mask = 0x0F; break; 2141 } 2142 if ((MI.getOperand(3).getImm() ^ Mask) == 1) { 2143 auto &WorkingMI = cloneIfNew(MI); 2144 WorkingMI.setDesc(get(Opc)); 2145 WorkingMI.RemoveOperand(3); 2146 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, 2147 /*NewMI=*/false, 2148 OpIdx1, OpIdx2); 2149 } 2150 } 2151 LLVM_FALLTHROUGH; 2152 case X86::PBLENDWrri: 2153 case X86::VBLENDPDYrri: 2154 case X86::VBLENDPSYrri: 2155 case X86::VPBLENDDrri: 2156 case X86::VPBLENDWrri: 2157 case X86::VPBLENDDYrri: 2158 case X86::VPBLENDWYrri:{ 2159 int8_t Mask; 2160 switch (MI.getOpcode()) { 2161 default: llvm_unreachable("Unreachable!"); 2162 case X86::BLENDPDrri: Mask = (int8_t)0x03; break; 2163 case X86::BLENDPSrri: Mask = (int8_t)0x0F; break; 2164 case X86::PBLENDWrri: Mask = (int8_t)0xFF; break; 2165 case X86::VBLENDPDrri: Mask = (int8_t)0x03; break; 2166 case X86::VBLENDPSrri: Mask = (int8_t)0x0F; break; 2167 case X86::VBLENDPDYrri: Mask = (int8_t)0x0F; break; 2168 case X86::VBLENDPSYrri: Mask = (int8_t)0xFF; break; 2169 case X86::VPBLENDDrri: Mask = (int8_t)0x0F; break; 2170 case X86::VPBLENDWrri: Mask = (int8_t)0xFF; break; 2171 case X86::VPBLENDDYrri: Mask = (int8_t)0xFF; break; 2172 case X86::VPBLENDWYrri: Mask = (int8_t)0xFF; break; 2173 } 2174 // Only the least significant bits of Imm are used. 2175 // Using int8_t to ensure it will be sign extended to the int64_t that 2176 // setImm takes in order to match isel behavior. 2177 int8_t Imm = MI.getOperand(3).getImm() & Mask; 2178 auto &WorkingMI = cloneIfNew(MI); 2179 WorkingMI.getOperand(3).setImm(Mask ^ Imm); 2180 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2181 OpIdx1, OpIdx2); 2182 } 2183 case X86::INSERTPSrr: 2184 case X86::VINSERTPSrr: 2185 case X86::VINSERTPSZrr: { 2186 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm(); 2187 unsigned ZMask = Imm & 15; 2188 unsigned DstIdx = (Imm >> 4) & 3; 2189 unsigned SrcIdx = (Imm >> 6) & 3; 2190 2191 // We can commute insertps if we zero 2 of the elements, the insertion is 2192 // "inline" and we don't override the insertion with a zero. 2193 if (DstIdx == SrcIdx && (ZMask & (1 << DstIdx)) == 0 && 2194 countPopulation(ZMask) == 2) { 2195 unsigned AltIdx = findFirstSet((ZMask | (1 << DstIdx)) ^ 15); 2196 assert(AltIdx < 4 && "Illegal insertion index"); 2197 unsigned AltImm = (AltIdx << 6) | (AltIdx << 4) | ZMask; 2198 auto &WorkingMI = cloneIfNew(MI); 2199 WorkingMI.getOperand(MI.getNumOperands() - 1).setImm(AltImm); 2200 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2201 OpIdx1, OpIdx2); 2202 } 2203 return nullptr; 2204 } 2205 case X86::MOVSDrr: 2206 case X86::MOVSSrr: 2207 case X86::VMOVSDrr: 2208 case X86::VMOVSSrr:{ 2209 // On SSE41 or later we can commute a MOVSS/MOVSD to a BLENDPS/BLENDPD. 2210 if (Subtarget.hasSSE41()) { 2211 unsigned Mask, Opc; 2212 switch (MI.getOpcode()) { 2213 default: llvm_unreachable("Unreachable!"); 2214 case X86::MOVSDrr: Opc = X86::BLENDPDrri; Mask = 0x02; break; 2215 case X86::MOVSSrr: Opc = X86::BLENDPSrri; Mask = 0x0E; break; 2216 case X86::VMOVSDrr: Opc = X86::VBLENDPDrri; Mask = 0x02; break; 2217 case X86::VMOVSSrr: Opc = X86::VBLENDPSrri; Mask = 0x0E; break; 2218 } 2219 2220 auto &WorkingMI = cloneIfNew(MI); 2221 WorkingMI.setDesc(get(Opc)); 2222 WorkingMI.addOperand(MachineOperand::CreateImm(Mask)); 2223 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2224 OpIdx1, OpIdx2); 2225 } 2226 2227 // Convert to SHUFPD. 2228 assert(MI.getOpcode() == X86::MOVSDrr && 2229 "Can only commute MOVSDrr without SSE4.1"); 2230 2231 auto &WorkingMI = cloneIfNew(MI); 2232 WorkingMI.setDesc(get(X86::SHUFPDrri)); 2233 WorkingMI.addOperand(MachineOperand::CreateImm(0x02)); 2234 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2235 OpIdx1, OpIdx2); 2236 } 2237 case X86::SHUFPDrri: { 2238 // Commute to MOVSD. 2239 assert(MI.getOperand(3).getImm() == 0x02 && "Unexpected immediate!"); 2240 auto &WorkingMI = cloneIfNew(MI); 2241 WorkingMI.setDesc(get(X86::MOVSDrr)); 2242 WorkingMI.RemoveOperand(3); 2243 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2244 OpIdx1, OpIdx2); 2245 } 2246 case X86::PCLMULQDQrr: 2247 case X86::VPCLMULQDQrr: 2248 case X86::VPCLMULQDQYrr: 2249 case X86::VPCLMULQDQZrr: 2250 case X86::VPCLMULQDQZ128rr: 2251 case X86::VPCLMULQDQZ256rr: { 2252 // SRC1 64bits = Imm[0] ? SRC1[127:64] : SRC1[63:0] 2253 // SRC2 64bits = Imm[4] ? SRC2[127:64] : SRC2[63:0] 2254 unsigned Imm = MI.getOperand(3).getImm(); 2255 unsigned Src1Hi = Imm & 0x01; 2256 unsigned Src2Hi = Imm & 0x10; 2257 auto &WorkingMI = cloneIfNew(MI); 2258 WorkingMI.getOperand(3).setImm((Src1Hi << 4) | (Src2Hi >> 4)); 2259 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2260 OpIdx1, OpIdx2); 2261 } 2262 case X86::VPCMPBZ128rri: case X86::VPCMPUBZ128rri: 2263 case X86::VPCMPBZ256rri: case X86::VPCMPUBZ256rri: 2264 case X86::VPCMPBZrri: case X86::VPCMPUBZrri: 2265 case X86::VPCMPDZ128rri: case X86::VPCMPUDZ128rri: 2266 case X86::VPCMPDZ256rri: case X86::VPCMPUDZ256rri: 2267 case X86::VPCMPDZrri: case X86::VPCMPUDZrri: 2268 case X86::VPCMPQZ128rri: case X86::VPCMPUQZ128rri: 2269 case X86::VPCMPQZ256rri: case X86::VPCMPUQZ256rri: 2270 case X86::VPCMPQZrri: case X86::VPCMPUQZrri: 2271 case X86::VPCMPWZ128rri: case X86::VPCMPUWZ128rri: 2272 case X86::VPCMPWZ256rri: case X86::VPCMPUWZ256rri: 2273 case X86::VPCMPWZrri: case X86::VPCMPUWZrri: 2274 case X86::VPCMPBZ128rrik: case X86::VPCMPUBZ128rrik: 2275 case X86::VPCMPBZ256rrik: case X86::VPCMPUBZ256rrik: 2276 case X86::VPCMPBZrrik: case X86::VPCMPUBZrrik: 2277 case X86::VPCMPDZ128rrik: case X86::VPCMPUDZ128rrik: 2278 case X86::VPCMPDZ256rrik: case X86::VPCMPUDZ256rrik: 2279 case X86::VPCMPDZrrik: case X86::VPCMPUDZrrik: 2280 case X86::VPCMPQZ128rrik: case X86::VPCMPUQZ128rrik: 2281 case X86::VPCMPQZ256rrik: case X86::VPCMPUQZ256rrik: 2282 case X86::VPCMPQZrrik: case X86::VPCMPUQZrrik: 2283 case X86::VPCMPWZ128rrik: case X86::VPCMPUWZ128rrik: 2284 case X86::VPCMPWZ256rrik: case X86::VPCMPUWZ256rrik: 2285 case X86::VPCMPWZrrik: case X86::VPCMPUWZrrik: { 2286 // Flip comparison mode immediate (if necessary). 2287 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm() & 0x7; 2288 Imm = X86::getSwappedVPCMPImm(Imm); 2289 auto &WorkingMI = cloneIfNew(MI); 2290 WorkingMI.getOperand(MI.getNumOperands() - 1).setImm(Imm); 2291 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2292 OpIdx1, OpIdx2); 2293 } 2294 case X86::VPCOMBri: case X86::VPCOMUBri: 2295 case X86::VPCOMDri: case X86::VPCOMUDri: 2296 case X86::VPCOMQri: case X86::VPCOMUQri: 2297 case X86::VPCOMWri: case X86::VPCOMUWri: { 2298 // Flip comparison mode immediate (if necessary). 2299 unsigned Imm = MI.getOperand(3).getImm() & 0x7; 2300 Imm = X86::getSwappedVPCOMImm(Imm); 2301 auto &WorkingMI = cloneIfNew(MI); 2302 WorkingMI.getOperand(3).setImm(Imm); 2303 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2304 OpIdx1, OpIdx2); 2305 } 2306 case X86::VCMPSDZrr: 2307 case X86::VCMPSSZrr: 2308 case X86::VCMPPDZrri: 2309 case X86::VCMPPSZrri: 2310 case X86::VCMPSHZrr: 2311 case X86::VCMPPHZrri: 2312 case X86::VCMPPHZ128rri: 2313 case X86::VCMPPHZ256rri: 2314 case X86::VCMPPDZ128rri: 2315 case X86::VCMPPSZ128rri: 2316 case X86::VCMPPDZ256rri: 2317 case X86::VCMPPSZ256rri: 2318 case X86::VCMPPDZrrik: 2319 case X86::VCMPPSZrrik: 2320 case X86::VCMPPDZ128rrik: 2321 case X86::VCMPPSZ128rrik: 2322 case X86::VCMPPDZ256rrik: 2323 case X86::VCMPPSZ256rrik: { 2324 unsigned Imm = 2325 MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 0x1f; 2326 Imm = X86::getSwappedVCMPImm(Imm); 2327 auto &WorkingMI = cloneIfNew(MI); 2328 WorkingMI.getOperand(MI.getNumExplicitOperands() - 1).setImm(Imm); 2329 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2330 OpIdx1, OpIdx2); 2331 } 2332 case X86::VPERM2F128rr: 2333 case X86::VPERM2I128rr: { 2334 // Flip permute source immediate. 2335 // Imm & 0x02: lo = if set, select Op1.lo/hi else Op0.lo/hi. 2336 // Imm & 0x20: hi = if set, select Op1.lo/hi else Op0.lo/hi. 2337 int8_t Imm = MI.getOperand(3).getImm() & 0xFF; 2338 auto &WorkingMI = cloneIfNew(MI); 2339 WorkingMI.getOperand(3).setImm(Imm ^ 0x22); 2340 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2341 OpIdx1, OpIdx2); 2342 } 2343 case X86::MOVHLPSrr: 2344 case X86::UNPCKHPDrr: 2345 case X86::VMOVHLPSrr: 2346 case X86::VUNPCKHPDrr: 2347 case X86::VMOVHLPSZrr: 2348 case X86::VUNPCKHPDZ128rr: { 2349 assert(Subtarget.hasSSE2() && "Commuting MOVHLP/UNPCKHPD requires SSE2!"); 2350 2351 unsigned Opc = MI.getOpcode(); 2352 switch (Opc) { 2353 default: llvm_unreachable("Unreachable!"); 2354 case X86::MOVHLPSrr: Opc = X86::UNPCKHPDrr; break; 2355 case X86::UNPCKHPDrr: Opc = X86::MOVHLPSrr; break; 2356 case X86::VMOVHLPSrr: Opc = X86::VUNPCKHPDrr; break; 2357 case X86::VUNPCKHPDrr: Opc = X86::VMOVHLPSrr; break; 2358 case X86::VMOVHLPSZrr: Opc = X86::VUNPCKHPDZ128rr; break; 2359 case X86::VUNPCKHPDZ128rr: Opc = X86::VMOVHLPSZrr; break; 2360 } 2361 auto &WorkingMI = cloneIfNew(MI); 2362 WorkingMI.setDesc(get(Opc)); 2363 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2364 OpIdx1, OpIdx2); 2365 } 2366 case X86::CMOV16rr: case X86::CMOV32rr: case X86::CMOV64rr: { 2367 auto &WorkingMI = cloneIfNew(MI); 2368 unsigned OpNo = MI.getDesc().getNumOperands() - 1; 2369 X86::CondCode CC = static_cast<X86::CondCode>(MI.getOperand(OpNo).getImm()); 2370 WorkingMI.getOperand(OpNo).setImm(X86::GetOppositeBranchCondition(CC)); 2371 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2372 OpIdx1, OpIdx2); 2373 } 2374 case X86::VPTERNLOGDZrri: case X86::VPTERNLOGDZrmi: 2375 case X86::VPTERNLOGDZ128rri: case X86::VPTERNLOGDZ128rmi: 2376 case X86::VPTERNLOGDZ256rri: case X86::VPTERNLOGDZ256rmi: 2377 case X86::VPTERNLOGQZrri: case X86::VPTERNLOGQZrmi: 2378 case X86::VPTERNLOGQZ128rri: case X86::VPTERNLOGQZ128rmi: 2379 case X86::VPTERNLOGQZ256rri: case X86::VPTERNLOGQZ256rmi: 2380 case X86::VPTERNLOGDZrrik: 2381 case X86::VPTERNLOGDZ128rrik: 2382 case X86::VPTERNLOGDZ256rrik: 2383 case X86::VPTERNLOGQZrrik: 2384 case X86::VPTERNLOGQZ128rrik: 2385 case X86::VPTERNLOGQZ256rrik: 2386 case X86::VPTERNLOGDZrrikz: case X86::VPTERNLOGDZrmikz: 2387 case X86::VPTERNLOGDZ128rrikz: case X86::VPTERNLOGDZ128rmikz: 2388 case X86::VPTERNLOGDZ256rrikz: case X86::VPTERNLOGDZ256rmikz: 2389 case X86::VPTERNLOGQZrrikz: case X86::VPTERNLOGQZrmikz: 2390 case X86::VPTERNLOGQZ128rrikz: case X86::VPTERNLOGQZ128rmikz: 2391 case X86::VPTERNLOGQZ256rrikz: case X86::VPTERNLOGQZ256rmikz: 2392 case X86::VPTERNLOGDZ128rmbi: 2393 case X86::VPTERNLOGDZ256rmbi: 2394 case X86::VPTERNLOGDZrmbi: 2395 case X86::VPTERNLOGQZ128rmbi: 2396 case X86::VPTERNLOGQZ256rmbi: 2397 case X86::VPTERNLOGQZrmbi: 2398 case X86::VPTERNLOGDZ128rmbikz: 2399 case X86::VPTERNLOGDZ256rmbikz: 2400 case X86::VPTERNLOGDZrmbikz: 2401 case X86::VPTERNLOGQZ128rmbikz: 2402 case X86::VPTERNLOGQZ256rmbikz: 2403 case X86::VPTERNLOGQZrmbikz: { 2404 auto &WorkingMI = cloneIfNew(MI); 2405 commuteVPTERNLOG(WorkingMI, OpIdx1, OpIdx2); 2406 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2407 OpIdx1, OpIdx2); 2408 } 2409 default: { 2410 if (isCommutableVPERMV3Instruction(MI.getOpcode())) { 2411 unsigned Opc = getCommutedVPERMV3Opcode(MI.getOpcode()); 2412 auto &WorkingMI = cloneIfNew(MI); 2413 WorkingMI.setDesc(get(Opc)); 2414 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2415 OpIdx1, OpIdx2); 2416 } 2417 2418 const X86InstrFMA3Group *FMA3Group = getFMA3Group(MI.getOpcode(), 2419 MI.getDesc().TSFlags); 2420 if (FMA3Group) { 2421 unsigned Opc = 2422 getFMA3OpcodeToCommuteOperands(MI, OpIdx1, OpIdx2, *FMA3Group); 2423 auto &WorkingMI = cloneIfNew(MI); 2424 WorkingMI.setDesc(get(Opc)); 2425 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, 2426 OpIdx1, OpIdx2); 2427 } 2428 2429 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); 2430 } 2431 } 2432 } 2433 2434 bool 2435 X86InstrInfo::findThreeSrcCommutedOpIndices(const MachineInstr &MI, 2436 unsigned &SrcOpIdx1, 2437 unsigned &SrcOpIdx2, 2438 bool IsIntrinsic) const { 2439 uint64_t TSFlags = MI.getDesc().TSFlags; 2440 2441 unsigned FirstCommutableVecOp = 1; 2442 unsigned LastCommutableVecOp = 3; 2443 unsigned KMaskOp = -1U; 2444 if (X86II::isKMasked(TSFlags)) { 2445 // For k-zero-masked operations it is Ok to commute the first vector 2446 // operand. Unless this is an intrinsic instruction. 2447 // For regular k-masked operations a conservative choice is done as the 2448 // elements of the first vector operand, for which the corresponding bit 2449 // in the k-mask operand is set to 0, are copied to the result of the 2450 // instruction. 2451 // TODO/FIXME: The commute still may be legal if it is known that the 2452 // k-mask operand is set to either all ones or all zeroes. 2453 // It is also Ok to commute the 1st operand if all users of MI use only 2454 // the elements enabled by the k-mask operand. For example, 2455 // v4 = VFMADD213PSZrk v1, k, v2, v3; // v1[i] = k[i] ? v2[i]*v1[i]+v3[i] 2456 // : v1[i]; 2457 // VMOVAPSZmrk <mem_addr>, k, v4; // this is the ONLY user of v4 -> 2458 // // Ok, to commute v1 in FMADD213PSZrk. 2459 2460 // The k-mask operand has index = 2 for masked and zero-masked operations. 2461 KMaskOp = 2; 2462 2463 // The operand with index = 1 is used as a source for those elements for 2464 // which the corresponding bit in the k-mask is set to 0. 2465 if (X86II::isKMergeMasked(TSFlags) || IsIntrinsic) 2466 FirstCommutableVecOp = 3; 2467 2468 LastCommutableVecOp++; 2469 } else if (IsIntrinsic) { 2470 // Commuting the first operand of an intrinsic instruction isn't possible 2471 // unless we can prove that only the lowest element of the result is used. 2472 FirstCommutableVecOp = 2; 2473 } 2474 2475 if (isMem(MI, LastCommutableVecOp)) 2476 LastCommutableVecOp--; 2477 2478 // Only the first RegOpsNum operands are commutable. 2479 // Also, the value 'CommuteAnyOperandIndex' is valid here as it means 2480 // that the operand is not specified/fixed. 2481 if (SrcOpIdx1 != CommuteAnyOperandIndex && 2482 (SrcOpIdx1 < FirstCommutableVecOp || SrcOpIdx1 > LastCommutableVecOp || 2483 SrcOpIdx1 == KMaskOp)) 2484 return false; 2485 if (SrcOpIdx2 != CommuteAnyOperandIndex && 2486 (SrcOpIdx2 < FirstCommutableVecOp || SrcOpIdx2 > LastCommutableVecOp || 2487 SrcOpIdx2 == KMaskOp)) 2488 return false; 2489 2490 // Look for two different register operands assumed to be commutable 2491 // regardless of the FMA opcode. The FMA opcode is adjusted later. 2492 if (SrcOpIdx1 == CommuteAnyOperandIndex || 2493 SrcOpIdx2 == CommuteAnyOperandIndex) { 2494 unsigned CommutableOpIdx2 = SrcOpIdx2; 2495 2496 // At least one of operands to be commuted is not specified and 2497 // this method is free to choose appropriate commutable operands. 2498 if (SrcOpIdx1 == SrcOpIdx2) 2499 // Both of operands are not fixed. By default set one of commutable 2500 // operands to the last register operand of the instruction. 2501 CommutableOpIdx2 = LastCommutableVecOp; 2502 else if (SrcOpIdx2 == CommuteAnyOperandIndex) 2503 // Only one of operands is not fixed. 2504 CommutableOpIdx2 = SrcOpIdx1; 2505 2506 // CommutableOpIdx2 is well defined now. Let's choose another commutable 2507 // operand and assign its index to CommutableOpIdx1. 2508 Register Op2Reg = MI.getOperand(CommutableOpIdx2).getReg(); 2509 2510 unsigned CommutableOpIdx1; 2511 for (CommutableOpIdx1 = LastCommutableVecOp; 2512 CommutableOpIdx1 >= FirstCommutableVecOp; CommutableOpIdx1--) { 2513 // Just ignore and skip the k-mask operand. 2514 if (CommutableOpIdx1 == KMaskOp) 2515 continue; 2516 2517 // The commuted operands must have different registers. 2518 // Otherwise, the commute transformation does not change anything and 2519 // is useless then. 2520 if (Op2Reg != MI.getOperand(CommutableOpIdx1).getReg()) 2521 break; 2522 } 2523 2524 // No appropriate commutable operands were found. 2525 if (CommutableOpIdx1 < FirstCommutableVecOp) 2526 return false; 2527 2528 // Assign the found pair of commutable indices to SrcOpIdx1 and SrcOpidx2 2529 // to return those values. 2530 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2531 CommutableOpIdx1, CommutableOpIdx2)) 2532 return false; 2533 } 2534 2535 return true; 2536 } 2537 2538 bool X86InstrInfo::findCommutedOpIndices(const MachineInstr &MI, 2539 unsigned &SrcOpIdx1, 2540 unsigned &SrcOpIdx2) const { 2541 const MCInstrDesc &Desc = MI.getDesc(); 2542 if (!Desc.isCommutable()) 2543 return false; 2544 2545 switch (MI.getOpcode()) { 2546 case X86::CMPSDrr: 2547 case X86::CMPSSrr: 2548 case X86::CMPPDrri: 2549 case X86::CMPPSrri: 2550 case X86::VCMPSDrr: 2551 case X86::VCMPSSrr: 2552 case X86::VCMPPDrri: 2553 case X86::VCMPPSrri: 2554 case X86::VCMPPDYrri: 2555 case X86::VCMPPSYrri: 2556 case X86::VCMPSDZrr: 2557 case X86::VCMPSSZrr: 2558 case X86::VCMPPDZrri: 2559 case X86::VCMPPSZrri: 2560 case X86::VCMPSHZrr: 2561 case X86::VCMPPHZrri: 2562 case X86::VCMPPHZ128rri: 2563 case X86::VCMPPHZ256rri: 2564 case X86::VCMPPDZ128rri: 2565 case X86::VCMPPSZ128rri: 2566 case X86::VCMPPDZ256rri: 2567 case X86::VCMPPSZ256rri: 2568 case X86::VCMPPDZrrik: 2569 case X86::VCMPPSZrrik: 2570 case X86::VCMPPDZ128rrik: 2571 case X86::VCMPPSZ128rrik: 2572 case X86::VCMPPDZ256rrik: 2573 case X86::VCMPPSZ256rrik: { 2574 unsigned OpOffset = X86II::isKMasked(Desc.TSFlags) ? 1 : 0; 2575 2576 // Float comparison can be safely commuted for 2577 // Ordered/Unordered/Equal/NotEqual tests 2578 unsigned Imm = MI.getOperand(3 + OpOffset).getImm() & 0x7; 2579 switch (Imm) { 2580 default: 2581 // EVEX versions can be commuted. 2582 if ((Desc.TSFlags & X86II::EncodingMask) == X86II::EVEX) 2583 break; 2584 return false; 2585 case 0x00: // EQUAL 2586 case 0x03: // UNORDERED 2587 case 0x04: // NOT EQUAL 2588 case 0x07: // ORDERED 2589 break; 2590 } 2591 2592 // The indices of the commutable operands are 1 and 2 (or 2 and 3 2593 // when masked). 2594 // Assign them to the returned operand indices here. 2595 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1 + OpOffset, 2596 2 + OpOffset); 2597 } 2598 case X86::MOVSSrr: 2599 // X86::MOVSDrr is always commutable. MOVSS is only commutable if we can 2600 // form sse4.1 blend. We assume VMOVSSrr/VMOVSDrr is always commutable since 2601 // AVX implies sse4.1. 2602 if (Subtarget.hasSSE41()) 2603 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 2604 return false; 2605 case X86::SHUFPDrri: 2606 // We can commute this to MOVSD. 2607 if (MI.getOperand(3).getImm() == 0x02) 2608 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 2609 return false; 2610 case X86::MOVHLPSrr: 2611 case X86::UNPCKHPDrr: 2612 case X86::VMOVHLPSrr: 2613 case X86::VUNPCKHPDrr: 2614 case X86::VMOVHLPSZrr: 2615 case X86::VUNPCKHPDZ128rr: 2616 if (Subtarget.hasSSE2()) 2617 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 2618 return false; 2619 case X86::VPTERNLOGDZrri: case X86::VPTERNLOGDZrmi: 2620 case X86::VPTERNLOGDZ128rri: case X86::VPTERNLOGDZ128rmi: 2621 case X86::VPTERNLOGDZ256rri: case X86::VPTERNLOGDZ256rmi: 2622 case X86::VPTERNLOGQZrri: case X86::VPTERNLOGQZrmi: 2623 case X86::VPTERNLOGQZ128rri: case X86::VPTERNLOGQZ128rmi: 2624 case X86::VPTERNLOGQZ256rri: case X86::VPTERNLOGQZ256rmi: 2625 case X86::VPTERNLOGDZrrik: 2626 case X86::VPTERNLOGDZ128rrik: 2627 case X86::VPTERNLOGDZ256rrik: 2628 case X86::VPTERNLOGQZrrik: 2629 case X86::VPTERNLOGQZ128rrik: 2630 case X86::VPTERNLOGQZ256rrik: 2631 case X86::VPTERNLOGDZrrikz: case X86::VPTERNLOGDZrmikz: 2632 case X86::VPTERNLOGDZ128rrikz: case X86::VPTERNLOGDZ128rmikz: 2633 case X86::VPTERNLOGDZ256rrikz: case X86::VPTERNLOGDZ256rmikz: 2634 case X86::VPTERNLOGQZrrikz: case X86::VPTERNLOGQZrmikz: 2635 case X86::VPTERNLOGQZ128rrikz: case X86::VPTERNLOGQZ128rmikz: 2636 case X86::VPTERNLOGQZ256rrikz: case X86::VPTERNLOGQZ256rmikz: 2637 case X86::VPTERNLOGDZ128rmbi: 2638 case X86::VPTERNLOGDZ256rmbi: 2639 case X86::VPTERNLOGDZrmbi: 2640 case X86::VPTERNLOGQZ128rmbi: 2641 case X86::VPTERNLOGQZ256rmbi: 2642 case X86::VPTERNLOGQZrmbi: 2643 case X86::VPTERNLOGDZ128rmbikz: 2644 case X86::VPTERNLOGDZ256rmbikz: 2645 case X86::VPTERNLOGDZrmbikz: 2646 case X86::VPTERNLOGQZ128rmbikz: 2647 case X86::VPTERNLOGQZ256rmbikz: 2648 case X86::VPTERNLOGQZrmbikz: 2649 return findThreeSrcCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 2650 case X86::VPDPWSSDYrr: 2651 case X86::VPDPWSSDrr: 2652 case X86::VPDPWSSDSYrr: 2653 case X86::VPDPWSSDSrr: 2654 case X86::VPDPWSSDZ128r: 2655 case X86::VPDPWSSDZ128rk: 2656 case X86::VPDPWSSDZ128rkz: 2657 case X86::VPDPWSSDZ256r: 2658 case X86::VPDPWSSDZ256rk: 2659 case X86::VPDPWSSDZ256rkz: 2660 case X86::VPDPWSSDZr: 2661 case X86::VPDPWSSDZrk: 2662 case X86::VPDPWSSDZrkz: 2663 case X86::VPDPWSSDSZ128r: 2664 case X86::VPDPWSSDSZ128rk: 2665 case X86::VPDPWSSDSZ128rkz: 2666 case X86::VPDPWSSDSZ256r: 2667 case X86::VPDPWSSDSZ256rk: 2668 case X86::VPDPWSSDSZ256rkz: 2669 case X86::VPDPWSSDSZr: 2670 case X86::VPDPWSSDSZrk: 2671 case X86::VPDPWSSDSZrkz: 2672 case X86::VPMADD52HUQZ128r: 2673 case X86::VPMADD52HUQZ128rk: 2674 case X86::VPMADD52HUQZ128rkz: 2675 case X86::VPMADD52HUQZ256r: 2676 case X86::VPMADD52HUQZ256rk: 2677 case X86::VPMADD52HUQZ256rkz: 2678 case X86::VPMADD52HUQZr: 2679 case X86::VPMADD52HUQZrk: 2680 case X86::VPMADD52HUQZrkz: 2681 case X86::VPMADD52LUQZ128r: 2682 case X86::VPMADD52LUQZ128rk: 2683 case X86::VPMADD52LUQZ128rkz: 2684 case X86::VPMADD52LUQZ256r: 2685 case X86::VPMADD52LUQZ256rk: 2686 case X86::VPMADD52LUQZ256rkz: 2687 case X86::VPMADD52LUQZr: 2688 case X86::VPMADD52LUQZrk: 2689 case X86::VPMADD52LUQZrkz: 2690 case X86::VFMADDCPHZr: 2691 case X86::VFMADDCPHZrk: 2692 case X86::VFMADDCPHZrkz: 2693 case X86::VFMADDCPHZ128r: 2694 case X86::VFMADDCPHZ128rk: 2695 case X86::VFMADDCPHZ128rkz: 2696 case X86::VFMADDCPHZ256r: 2697 case X86::VFMADDCPHZ256rk: 2698 case X86::VFMADDCPHZ256rkz: 2699 case X86::VFMADDCSHZr: 2700 case X86::VFMADDCSHZrk: 2701 case X86::VFMADDCSHZrkz: { 2702 unsigned CommutableOpIdx1 = 2; 2703 unsigned CommutableOpIdx2 = 3; 2704 if (X86II::isKMasked(Desc.TSFlags)) { 2705 // Skip the mask register. 2706 ++CommutableOpIdx1; 2707 ++CommutableOpIdx2; 2708 } 2709 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2710 CommutableOpIdx1, CommutableOpIdx2)) 2711 return false; 2712 if (!MI.getOperand(SrcOpIdx1).isReg() || 2713 !MI.getOperand(SrcOpIdx2).isReg()) 2714 // No idea. 2715 return false; 2716 return true; 2717 } 2718 2719 default: 2720 const X86InstrFMA3Group *FMA3Group = getFMA3Group(MI.getOpcode(), 2721 MI.getDesc().TSFlags); 2722 if (FMA3Group) 2723 return findThreeSrcCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2, 2724 FMA3Group->isIntrinsic()); 2725 2726 // Handled masked instructions since we need to skip over the mask input 2727 // and the preserved input. 2728 if (X86II::isKMasked(Desc.TSFlags)) { 2729 // First assume that the first input is the mask operand and skip past it. 2730 unsigned CommutableOpIdx1 = Desc.getNumDefs() + 1; 2731 unsigned CommutableOpIdx2 = Desc.getNumDefs() + 2; 2732 // Check if the first input is tied. If there isn't one then we only 2733 // need to skip the mask operand which we did above. 2734 if ((MI.getDesc().getOperandConstraint(Desc.getNumDefs(), 2735 MCOI::TIED_TO) != -1)) { 2736 // If this is zero masking instruction with a tied operand, we need to 2737 // move the first index back to the first input since this must 2738 // be a 3 input instruction and we want the first two non-mask inputs. 2739 // Otherwise this is a 2 input instruction with a preserved input and 2740 // mask, so we need to move the indices to skip one more input. 2741 if (X86II::isKMergeMasked(Desc.TSFlags)) { 2742 ++CommutableOpIdx1; 2743 ++CommutableOpIdx2; 2744 } else { 2745 --CommutableOpIdx1; 2746 } 2747 } 2748 2749 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2750 CommutableOpIdx1, CommutableOpIdx2)) 2751 return false; 2752 2753 if (!MI.getOperand(SrcOpIdx1).isReg() || 2754 !MI.getOperand(SrcOpIdx2).isReg()) 2755 // No idea. 2756 return false; 2757 return true; 2758 } 2759 2760 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); 2761 } 2762 return false; 2763 } 2764 2765 static bool isConvertibleLEA(MachineInstr *MI) { 2766 unsigned Opcode = MI->getOpcode(); 2767 if (Opcode != X86::LEA32r && Opcode != X86::LEA64r && 2768 Opcode != X86::LEA64_32r) 2769 return false; 2770 2771 const MachineOperand &Scale = MI->getOperand(1 + X86::AddrScaleAmt); 2772 const MachineOperand &Disp = MI->getOperand(1 + X86::AddrDisp); 2773 const MachineOperand &Segment = MI->getOperand(1 + X86::AddrSegmentReg); 2774 2775 if (Segment.getReg() != 0 || !Disp.isImm() || Disp.getImm() != 0 || 2776 Scale.getImm() > 1) 2777 return false; 2778 2779 return true; 2780 } 2781 2782 bool X86InstrInfo::hasCommutePreference(MachineInstr &MI, bool &Commute) const { 2783 // Currently we're interested in following sequence only. 2784 // r3 = lea r1, r2 2785 // r5 = add r3, r4 2786 // Both r3 and r4 are killed in add, we hope the add instruction has the 2787 // operand order 2788 // r5 = add r4, r3 2789 // So later in X86FixupLEAs the lea instruction can be rewritten as add. 2790 unsigned Opcode = MI.getOpcode(); 2791 if (Opcode != X86::ADD32rr && Opcode != X86::ADD64rr) 2792 return false; 2793 2794 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2795 Register Reg1 = MI.getOperand(1).getReg(); 2796 Register Reg2 = MI.getOperand(2).getReg(); 2797 2798 // Check if Reg1 comes from LEA in the same MBB. 2799 if (MachineInstr *Inst = MRI.getUniqueVRegDef(Reg1)) { 2800 if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) { 2801 Commute = true; 2802 return true; 2803 } 2804 } 2805 2806 // Check if Reg2 comes from LEA in the same MBB. 2807 if (MachineInstr *Inst = MRI.getUniqueVRegDef(Reg2)) { 2808 if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) { 2809 Commute = false; 2810 return true; 2811 } 2812 } 2813 2814 return false; 2815 } 2816 2817 X86::CondCode X86::getCondFromBranch(const MachineInstr &MI) { 2818 switch (MI.getOpcode()) { 2819 default: return X86::COND_INVALID; 2820 case X86::JCC_1: 2821 return static_cast<X86::CondCode>( 2822 MI.getOperand(MI.getDesc().getNumOperands() - 1).getImm()); 2823 } 2824 } 2825 2826 /// Return condition code of a SETCC opcode. 2827 X86::CondCode X86::getCondFromSETCC(const MachineInstr &MI) { 2828 switch (MI.getOpcode()) { 2829 default: return X86::COND_INVALID; 2830 case X86::SETCCr: case X86::SETCCm: 2831 return static_cast<X86::CondCode>( 2832 MI.getOperand(MI.getDesc().getNumOperands() - 1).getImm()); 2833 } 2834 } 2835 2836 /// Return condition code of a CMov opcode. 2837 X86::CondCode X86::getCondFromCMov(const MachineInstr &MI) { 2838 switch (MI.getOpcode()) { 2839 default: return X86::COND_INVALID; 2840 case X86::CMOV16rr: case X86::CMOV32rr: case X86::CMOV64rr: 2841 case X86::CMOV16rm: case X86::CMOV32rm: case X86::CMOV64rm: 2842 return static_cast<X86::CondCode>( 2843 MI.getOperand(MI.getDesc().getNumOperands() - 1).getImm()); 2844 } 2845 } 2846 2847 /// Return the inverse of the specified condition, 2848 /// e.g. turning COND_E to COND_NE. 2849 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) { 2850 switch (CC) { 2851 default: llvm_unreachable("Illegal condition code!"); 2852 case X86::COND_E: return X86::COND_NE; 2853 case X86::COND_NE: return X86::COND_E; 2854 case X86::COND_L: return X86::COND_GE; 2855 case X86::COND_LE: return X86::COND_G; 2856 case X86::COND_G: return X86::COND_LE; 2857 case X86::COND_GE: return X86::COND_L; 2858 case X86::COND_B: return X86::COND_AE; 2859 case X86::COND_BE: return X86::COND_A; 2860 case X86::COND_A: return X86::COND_BE; 2861 case X86::COND_AE: return X86::COND_B; 2862 case X86::COND_S: return X86::COND_NS; 2863 case X86::COND_NS: return X86::COND_S; 2864 case X86::COND_P: return X86::COND_NP; 2865 case X86::COND_NP: return X86::COND_P; 2866 case X86::COND_O: return X86::COND_NO; 2867 case X86::COND_NO: return X86::COND_O; 2868 case X86::COND_NE_OR_P: return X86::COND_E_AND_NP; 2869 case X86::COND_E_AND_NP: return X86::COND_NE_OR_P; 2870 } 2871 } 2872 2873 /// Assuming the flags are set by MI(a,b), return the condition code if we 2874 /// modify the instructions such that flags are set by MI(b,a). 2875 static X86::CondCode getSwappedCondition(X86::CondCode CC) { 2876 switch (CC) { 2877 default: return X86::COND_INVALID; 2878 case X86::COND_E: return X86::COND_E; 2879 case X86::COND_NE: return X86::COND_NE; 2880 case X86::COND_L: return X86::COND_G; 2881 case X86::COND_LE: return X86::COND_GE; 2882 case X86::COND_G: return X86::COND_L; 2883 case X86::COND_GE: return X86::COND_LE; 2884 case X86::COND_B: return X86::COND_A; 2885 case X86::COND_BE: return X86::COND_AE; 2886 case X86::COND_A: return X86::COND_B; 2887 case X86::COND_AE: return X86::COND_BE; 2888 } 2889 } 2890 2891 std::pair<X86::CondCode, bool> 2892 X86::getX86ConditionCode(CmpInst::Predicate Predicate) { 2893 X86::CondCode CC = X86::COND_INVALID; 2894 bool NeedSwap = false; 2895 switch (Predicate) { 2896 default: break; 2897 // Floating-point Predicates 2898 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break; 2899 case CmpInst::FCMP_OLT: NeedSwap = true; LLVM_FALLTHROUGH; 2900 case CmpInst::FCMP_OGT: CC = X86::COND_A; break; 2901 case CmpInst::FCMP_OLE: NeedSwap = true; LLVM_FALLTHROUGH; 2902 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break; 2903 case CmpInst::FCMP_UGT: NeedSwap = true; LLVM_FALLTHROUGH; 2904 case CmpInst::FCMP_ULT: CC = X86::COND_B; break; 2905 case CmpInst::FCMP_UGE: NeedSwap = true; LLVM_FALLTHROUGH; 2906 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break; 2907 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break; 2908 case CmpInst::FCMP_UNO: CC = X86::COND_P; break; 2909 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break; 2910 case CmpInst::FCMP_OEQ: LLVM_FALLTHROUGH; 2911 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break; 2912 2913 // Integer Predicates 2914 case CmpInst::ICMP_EQ: CC = X86::COND_E; break; 2915 case CmpInst::ICMP_NE: CC = X86::COND_NE; break; 2916 case CmpInst::ICMP_UGT: CC = X86::COND_A; break; 2917 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break; 2918 case CmpInst::ICMP_ULT: CC = X86::COND_B; break; 2919 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break; 2920 case CmpInst::ICMP_SGT: CC = X86::COND_G; break; 2921 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break; 2922 case CmpInst::ICMP_SLT: CC = X86::COND_L; break; 2923 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break; 2924 } 2925 2926 return std::make_pair(CC, NeedSwap); 2927 } 2928 2929 /// Return a cmov opcode for the given register size in bytes, and operand type. 2930 unsigned X86::getCMovOpcode(unsigned RegBytes, bool HasMemoryOperand) { 2931 switch(RegBytes) { 2932 default: llvm_unreachable("Illegal register size!"); 2933 case 2: return HasMemoryOperand ? X86::CMOV16rm : X86::CMOV16rr; 2934 case 4: return HasMemoryOperand ? X86::CMOV32rm : X86::CMOV32rr; 2935 case 8: return HasMemoryOperand ? X86::CMOV64rm : X86::CMOV64rr; 2936 } 2937 } 2938 2939 /// Get the VPCMP immediate for the given condition. 2940 unsigned X86::getVPCMPImmForCond(ISD::CondCode CC) { 2941 switch (CC) { 2942 default: llvm_unreachable("Unexpected SETCC condition"); 2943 case ISD::SETNE: return 4; 2944 case ISD::SETEQ: return 0; 2945 case ISD::SETULT: 2946 case ISD::SETLT: return 1; 2947 case ISD::SETUGT: 2948 case ISD::SETGT: return 6; 2949 case ISD::SETUGE: 2950 case ISD::SETGE: return 5; 2951 case ISD::SETULE: 2952 case ISD::SETLE: return 2; 2953 } 2954 } 2955 2956 /// Get the VPCMP immediate if the operands are swapped. 2957 unsigned X86::getSwappedVPCMPImm(unsigned Imm) { 2958 switch (Imm) { 2959 default: llvm_unreachable("Unreachable!"); 2960 case 0x01: Imm = 0x06; break; // LT -> NLE 2961 case 0x02: Imm = 0x05; break; // LE -> NLT 2962 case 0x05: Imm = 0x02; break; // NLT -> LE 2963 case 0x06: Imm = 0x01; break; // NLE -> LT 2964 case 0x00: // EQ 2965 case 0x03: // FALSE 2966 case 0x04: // NE 2967 case 0x07: // TRUE 2968 break; 2969 } 2970 2971 return Imm; 2972 } 2973 2974 /// Get the VPCOM immediate if the operands are swapped. 2975 unsigned X86::getSwappedVPCOMImm(unsigned Imm) { 2976 switch (Imm) { 2977 default: llvm_unreachable("Unreachable!"); 2978 case 0x00: Imm = 0x02; break; // LT -> GT 2979 case 0x01: Imm = 0x03; break; // LE -> GE 2980 case 0x02: Imm = 0x00; break; // GT -> LT 2981 case 0x03: Imm = 0x01; break; // GE -> LE 2982 case 0x04: // EQ 2983 case 0x05: // NE 2984 case 0x06: // FALSE 2985 case 0x07: // TRUE 2986 break; 2987 } 2988 2989 return Imm; 2990 } 2991 2992 /// Get the VCMP immediate if the operands are swapped. 2993 unsigned X86::getSwappedVCMPImm(unsigned Imm) { 2994 // Only need the lower 2 bits to distinquish. 2995 switch (Imm & 0x3) { 2996 default: llvm_unreachable("Unreachable!"); 2997 case 0x00: case 0x03: 2998 // EQ/NE/TRUE/FALSE/ORD/UNORD don't change immediate when commuted. 2999 break; 3000 case 0x01: case 0x02: 3001 // Need to toggle bits 3:0. Bit 4 stays the same. 3002 Imm ^= 0xf; 3003 break; 3004 } 3005 3006 return Imm; 3007 } 3008 3009 /// Return true if the Reg is X87 register. 3010 static bool isX87Reg(unsigned Reg) { 3011 return (Reg == X86::FPCW || Reg == X86::FPSW || 3012 (Reg >= X86::ST0 && Reg <= X86::ST7)); 3013 } 3014 3015 /// check if the instruction is X87 instruction 3016 bool X86::isX87Instruction(MachineInstr &MI) { 3017 for (const MachineOperand &MO : MI.operands()) { 3018 if (!MO.isReg()) 3019 continue; 3020 if (isX87Reg(MO.getReg())) 3021 return true; 3022 } 3023 return false; 3024 } 3025 3026 bool X86InstrInfo::isUnconditionalTailCall(const MachineInstr &MI) const { 3027 switch (MI.getOpcode()) { 3028 case X86::TCRETURNdi: 3029 case X86::TCRETURNri: 3030 case X86::TCRETURNmi: 3031 case X86::TCRETURNdi64: 3032 case X86::TCRETURNri64: 3033 case X86::TCRETURNmi64: 3034 return true; 3035 default: 3036 return false; 3037 } 3038 } 3039 3040 bool X86InstrInfo::canMakeTailCallConditional( 3041 SmallVectorImpl<MachineOperand> &BranchCond, 3042 const MachineInstr &TailCall) const { 3043 if (TailCall.getOpcode() != X86::TCRETURNdi && 3044 TailCall.getOpcode() != X86::TCRETURNdi64) { 3045 // Only direct calls can be done with a conditional branch. 3046 return false; 3047 } 3048 3049 const MachineFunction *MF = TailCall.getParent()->getParent(); 3050 if (Subtarget.isTargetWin64() && MF->hasWinCFI()) { 3051 // Conditional tail calls confuse the Win64 unwinder. 3052 return false; 3053 } 3054 3055 assert(BranchCond.size() == 1); 3056 if (BranchCond[0].getImm() > X86::LAST_VALID_COND) { 3057 // Can't make a conditional tail call with this condition. 3058 return false; 3059 } 3060 3061 const X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>(); 3062 if (X86FI->getTCReturnAddrDelta() != 0 || 3063 TailCall.getOperand(1).getImm() != 0) { 3064 // A conditional tail call cannot do any stack adjustment. 3065 return false; 3066 } 3067 3068 return true; 3069 } 3070 3071 void X86InstrInfo::replaceBranchWithTailCall( 3072 MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &BranchCond, 3073 const MachineInstr &TailCall) const { 3074 assert(canMakeTailCallConditional(BranchCond, TailCall)); 3075 3076 MachineBasicBlock::iterator I = MBB.end(); 3077 while (I != MBB.begin()) { 3078 --I; 3079 if (I->isDebugInstr()) 3080 continue; 3081 if (!I->isBranch()) 3082 assert(0 && "Can't find the branch to replace!"); 3083 3084 X86::CondCode CC = X86::getCondFromBranch(*I); 3085 assert(BranchCond.size() == 1); 3086 if (CC != BranchCond[0].getImm()) 3087 continue; 3088 3089 break; 3090 } 3091 3092 unsigned Opc = TailCall.getOpcode() == X86::TCRETURNdi ? X86::TCRETURNdicc 3093 : X86::TCRETURNdi64cc; 3094 3095 auto MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opc)); 3096 MIB->addOperand(TailCall.getOperand(0)); // Destination. 3097 MIB.addImm(0); // Stack offset (not used). 3098 MIB->addOperand(BranchCond[0]); // Condition. 3099 MIB.copyImplicitOps(TailCall); // Regmask and (imp-used) parameters. 3100 3101 // Add implicit uses and defs of all live regs potentially clobbered by the 3102 // call. This way they still appear live across the call. 3103 LivePhysRegs LiveRegs(getRegisterInfo()); 3104 LiveRegs.addLiveOuts(MBB); 3105 SmallVector<std::pair<MCPhysReg, const MachineOperand *>, 8> Clobbers; 3106 LiveRegs.stepForward(*MIB, Clobbers); 3107 for (const auto &C : Clobbers) { 3108 MIB.addReg(C.first, RegState::Implicit); 3109 MIB.addReg(C.first, RegState::Implicit | RegState::Define); 3110 } 3111 3112 I->eraseFromParent(); 3113 } 3114 3115 // Given a MBB and its TBB, find the FBB which was a fallthrough MBB (it may 3116 // not be a fallthrough MBB now due to layout changes). Return nullptr if the 3117 // fallthrough MBB cannot be identified. 3118 static MachineBasicBlock *getFallThroughMBB(MachineBasicBlock *MBB, 3119 MachineBasicBlock *TBB) { 3120 // Look for non-EHPad successors other than TBB. If we find exactly one, it 3121 // is the fallthrough MBB. If we find zero, then TBB is both the target MBB 3122 // and fallthrough MBB. If we find more than one, we cannot identify the 3123 // fallthrough MBB and should return nullptr. 3124 MachineBasicBlock *FallthroughBB = nullptr; 3125 for (MachineBasicBlock *Succ : MBB->successors()) { 3126 if (Succ->isEHPad() || (Succ == TBB && FallthroughBB)) 3127 continue; 3128 // Return a nullptr if we found more than one fallthrough successor. 3129 if (FallthroughBB && FallthroughBB != TBB) 3130 return nullptr; 3131 FallthroughBB = Succ; 3132 } 3133 return FallthroughBB; 3134 } 3135 3136 bool X86InstrInfo::AnalyzeBranchImpl( 3137 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, 3138 SmallVectorImpl<MachineOperand> &Cond, 3139 SmallVectorImpl<MachineInstr *> &CondBranches, bool AllowModify) const { 3140 3141 // Start from the bottom of the block and work up, examining the 3142 // terminator instructions. 3143 MachineBasicBlock::iterator I = MBB.end(); 3144 MachineBasicBlock::iterator UnCondBrIter = MBB.end(); 3145 while (I != MBB.begin()) { 3146 --I; 3147 if (I->isDebugInstr()) 3148 continue; 3149 3150 // Working from the bottom, when we see a non-terminator instruction, we're 3151 // done. 3152 if (!isUnpredicatedTerminator(*I)) 3153 break; 3154 3155 // A terminator that isn't a branch can't easily be handled by this 3156 // analysis. 3157 if (!I->isBranch()) 3158 return true; 3159 3160 // Handle unconditional branches. 3161 if (I->getOpcode() == X86::JMP_1) { 3162 UnCondBrIter = I; 3163 3164 if (!AllowModify) { 3165 TBB = I->getOperand(0).getMBB(); 3166 continue; 3167 } 3168 3169 // If the block has any instructions after a JMP, delete them. 3170 while (std::next(I) != MBB.end()) 3171 std::next(I)->eraseFromParent(); 3172 3173 Cond.clear(); 3174 FBB = nullptr; 3175 3176 // Delete the JMP if it's equivalent to a fall-through. 3177 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { 3178 TBB = nullptr; 3179 I->eraseFromParent(); 3180 I = MBB.end(); 3181 UnCondBrIter = MBB.end(); 3182 continue; 3183 } 3184 3185 // TBB is used to indicate the unconditional destination. 3186 TBB = I->getOperand(0).getMBB(); 3187 continue; 3188 } 3189 3190 // Handle conditional branches. 3191 X86::CondCode BranchCode = X86::getCondFromBranch(*I); 3192 if (BranchCode == X86::COND_INVALID) 3193 return true; // Can't handle indirect branch. 3194 3195 // In practice we should never have an undef eflags operand, if we do 3196 // abort here as we are not prepared to preserve the flag. 3197 if (I->findRegisterUseOperand(X86::EFLAGS)->isUndef()) 3198 return true; 3199 3200 // Working from the bottom, handle the first conditional branch. 3201 if (Cond.empty()) { 3202 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB(); 3203 if (AllowModify && UnCondBrIter != MBB.end() && 3204 MBB.isLayoutSuccessor(TargetBB)) { 3205 // If we can modify the code and it ends in something like: 3206 // 3207 // jCC L1 3208 // jmp L2 3209 // L1: 3210 // ... 3211 // L2: 3212 // 3213 // Then we can change this to: 3214 // 3215 // jnCC L2 3216 // L1: 3217 // ... 3218 // L2: 3219 // 3220 // Which is a bit more efficient. 3221 // We conditionally jump to the fall-through block. 3222 BranchCode = GetOppositeBranchCondition(BranchCode); 3223 MachineBasicBlock::iterator OldInst = I; 3224 3225 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JCC_1)) 3226 .addMBB(UnCondBrIter->getOperand(0).getMBB()) 3227 .addImm(BranchCode); 3228 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JMP_1)) 3229 .addMBB(TargetBB); 3230 3231 OldInst->eraseFromParent(); 3232 UnCondBrIter->eraseFromParent(); 3233 3234 // Restart the analysis. 3235 UnCondBrIter = MBB.end(); 3236 I = MBB.end(); 3237 continue; 3238 } 3239 3240 FBB = TBB; 3241 TBB = I->getOperand(0).getMBB(); 3242 Cond.push_back(MachineOperand::CreateImm(BranchCode)); 3243 CondBranches.push_back(&*I); 3244 continue; 3245 } 3246 3247 // Handle subsequent conditional branches. Only handle the case where all 3248 // conditional branches branch to the same destination and their condition 3249 // opcodes fit one of the special multi-branch idioms. 3250 assert(Cond.size() == 1); 3251 assert(TBB); 3252 3253 // If the conditions are the same, we can leave them alone. 3254 X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm(); 3255 auto NewTBB = I->getOperand(0).getMBB(); 3256 if (OldBranchCode == BranchCode && TBB == NewTBB) 3257 continue; 3258 3259 // If they differ, see if they fit one of the known patterns. Theoretically, 3260 // we could handle more patterns here, but we shouldn't expect to see them 3261 // if instruction selection has done a reasonable job. 3262 if (TBB == NewTBB && 3263 ((OldBranchCode == X86::COND_P && BranchCode == X86::COND_NE) || 3264 (OldBranchCode == X86::COND_NE && BranchCode == X86::COND_P))) { 3265 BranchCode = X86::COND_NE_OR_P; 3266 } else if ((OldBranchCode == X86::COND_NP && BranchCode == X86::COND_NE) || 3267 (OldBranchCode == X86::COND_E && BranchCode == X86::COND_P)) { 3268 if (NewTBB != (FBB ? FBB : getFallThroughMBB(&MBB, TBB))) 3269 return true; 3270 3271 // X86::COND_E_AND_NP usually has two different branch destinations. 3272 // 3273 // JP B1 3274 // JE B2 3275 // JMP B1 3276 // B1: 3277 // B2: 3278 // 3279 // Here this condition branches to B2 only if NP && E. It has another 3280 // equivalent form: 3281 // 3282 // JNE B1 3283 // JNP B2 3284 // JMP B1 3285 // B1: 3286 // B2: 3287 // 3288 // Similarly it branches to B2 only if E && NP. That is why this condition 3289 // is named with COND_E_AND_NP. 3290 BranchCode = X86::COND_E_AND_NP; 3291 } else 3292 return true; 3293 3294 // Update the MachineOperand. 3295 Cond[0].setImm(BranchCode); 3296 CondBranches.push_back(&*I); 3297 } 3298 3299 return false; 3300 } 3301 3302 bool X86InstrInfo::analyzeBranch(MachineBasicBlock &MBB, 3303 MachineBasicBlock *&TBB, 3304 MachineBasicBlock *&FBB, 3305 SmallVectorImpl<MachineOperand> &Cond, 3306 bool AllowModify) const { 3307 SmallVector<MachineInstr *, 4> CondBranches; 3308 return AnalyzeBranchImpl(MBB, TBB, FBB, Cond, CondBranches, AllowModify); 3309 } 3310 3311 bool X86InstrInfo::analyzeBranchPredicate(MachineBasicBlock &MBB, 3312 MachineBranchPredicate &MBP, 3313 bool AllowModify) const { 3314 using namespace std::placeholders; 3315 3316 SmallVector<MachineOperand, 4> Cond; 3317 SmallVector<MachineInstr *, 4> CondBranches; 3318 if (AnalyzeBranchImpl(MBB, MBP.TrueDest, MBP.FalseDest, Cond, CondBranches, 3319 AllowModify)) 3320 return true; 3321 3322 if (Cond.size() != 1) 3323 return true; 3324 3325 assert(MBP.TrueDest && "expected!"); 3326 3327 if (!MBP.FalseDest) 3328 MBP.FalseDest = MBB.getNextNode(); 3329 3330 const TargetRegisterInfo *TRI = &getRegisterInfo(); 3331 3332 MachineInstr *ConditionDef = nullptr; 3333 bool SingleUseCondition = true; 3334 3335 for (MachineInstr &MI : llvm::drop_begin(llvm::reverse(MBB))) { 3336 if (MI.modifiesRegister(X86::EFLAGS, TRI)) { 3337 ConditionDef = &MI; 3338 break; 3339 } 3340 3341 if (MI.readsRegister(X86::EFLAGS, TRI)) 3342 SingleUseCondition = false; 3343 } 3344 3345 if (!ConditionDef) 3346 return true; 3347 3348 if (SingleUseCondition) { 3349 for (auto *Succ : MBB.successors()) 3350 if (Succ->isLiveIn(X86::EFLAGS)) 3351 SingleUseCondition = false; 3352 } 3353 3354 MBP.ConditionDef = ConditionDef; 3355 MBP.SingleUseCondition = SingleUseCondition; 3356 3357 // Currently we only recognize the simple pattern: 3358 // 3359 // test %reg, %reg 3360 // je %label 3361 // 3362 const unsigned TestOpcode = 3363 Subtarget.is64Bit() ? X86::TEST64rr : X86::TEST32rr; 3364 3365 if (ConditionDef->getOpcode() == TestOpcode && 3366 ConditionDef->getNumOperands() == 3 && 3367 ConditionDef->getOperand(0).isIdenticalTo(ConditionDef->getOperand(1)) && 3368 (Cond[0].getImm() == X86::COND_NE || Cond[0].getImm() == X86::COND_E)) { 3369 MBP.LHS = ConditionDef->getOperand(0); 3370 MBP.RHS = MachineOperand::CreateImm(0); 3371 MBP.Predicate = Cond[0].getImm() == X86::COND_NE 3372 ? MachineBranchPredicate::PRED_NE 3373 : MachineBranchPredicate::PRED_EQ; 3374 return false; 3375 } 3376 3377 return true; 3378 } 3379 3380 unsigned X86InstrInfo::removeBranch(MachineBasicBlock &MBB, 3381 int *BytesRemoved) const { 3382 assert(!BytesRemoved && "code size not handled"); 3383 3384 MachineBasicBlock::iterator I = MBB.end(); 3385 unsigned Count = 0; 3386 3387 while (I != MBB.begin()) { 3388 --I; 3389 if (I->isDebugInstr()) 3390 continue; 3391 if (I->getOpcode() != X86::JMP_1 && 3392 X86::getCondFromBranch(*I) == X86::COND_INVALID) 3393 break; 3394 // Remove the branch. 3395 I->eraseFromParent(); 3396 I = MBB.end(); 3397 ++Count; 3398 } 3399 3400 return Count; 3401 } 3402 3403 unsigned X86InstrInfo::insertBranch(MachineBasicBlock &MBB, 3404 MachineBasicBlock *TBB, 3405 MachineBasicBlock *FBB, 3406 ArrayRef<MachineOperand> Cond, 3407 const DebugLoc &DL, 3408 int *BytesAdded) const { 3409 // Shouldn't be a fall through. 3410 assert(TBB && "insertBranch must not be told to insert a fallthrough"); 3411 assert((Cond.size() == 1 || Cond.size() == 0) && 3412 "X86 branch conditions have one component!"); 3413 assert(!BytesAdded && "code size not handled"); 3414 3415 if (Cond.empty()) { 3416 // Unconditional branch? 3417 assert(!FBB && "Unconditional branch with multiple successors!"); 3418 BuildMI(&MBB, DL, get(X86::JMP_1)).addMBB(TBB); 3419 return 1; 3420 } 3421 3422 // If FBB is null, it is implied to be a fall-through block. 3423 bool FallThru = FBB == nullptr; 3424 3425 // Conditional branch. 3426 unsigned Count = 0; 3427 X86::CondCode CC = (X86::CondCode)Cond[0].getImm(); 3428 switch (CC) { 3429 case X86::COND_NE_OR_P: 3430 // Synthesize NE_OR_P with two branches. 3431 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_NE); 3432 ++Count; 3433 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_P); 3434 ++Count; 3435 break; 3436 case X86::COND_E_AND_NP: 3437 // Use the next block of MBB as FBB if it is null. 3438 if (FBB == nullptr) { 3439 FBB = getFallThroughMBB(&MBB, TBB); 3440 assert(FBB && "MBB cannot be the last block in function when the false " 3441 "body is a fall-through."); 3442 } 3443 // Synthesize COND_E_AND_NP with two branches. 3444 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(FBB).addImm(X86::COND_NE); 3445 ++Count; 3446 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_NP); 3447 ++Count; 3448 break; 3449 default: { 3450 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(CC); 3451 ++Count; 3452 } 3453 } 3454 if (!FallThru) { 3455 // Two-way Conditional branch. Insert the second branch. 3456 BuildMI(&MBB, DL, get(X86::JMP_1)).addMBB(FBB); 3457 ++Count; 3458 } 3459 return Count; 3460 } 3461 3462 bool X86InstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 3463 ArrayRef<MachineOperand> Cond, 3464 Register DstReg, Register TrueReg, 3465 Register FalseReg, int &CondCycles, 3466 int &TrueCycles, int &FalseCycles) const { 3467 // Not all subtargets have cmov instructions. 3468 if (!Subtarget.hasCMov()) 3469 return false; 3470 if (Cond.size() != 1) 3471 return false; 3472 // We cannot do the composite conditions, at least not in SSA form. 3473 if ((X86::CondCode)Cond[0].getImm() > X86::LAST_VALID_COND) 3474 return false; 3475 3476 // Check register classes. 3477 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3478 const TargetRegisterClass *RC = 3479 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 3480 if (!RC) 3481 return false; 3482 3483 // We have cmov instructions for 16, 32, and 64 bit general purpose registers. 3484 if (X86::GR16RegClass.hasSubClassEq(RC) || 3485 X86::GR32RegClass.hasSubClassEq(RC) || 3486 X86::GR64RegClass.hasSubClassEq(RC)) { 3487 // This latency applies to Pentium M, Merom, Wolfdale, Nehalem, and Sandy 3488 // Bridge. Probably Ivy Bridge as well. 3489 CondCycles = 2; 3490 TrueCycles = 2; 3491 FalseCycles = 2; 3492 return true; 3493 } 3494 3495 // Can't do vectors. 3496 return false; 3497 } 3498 3499 void X86InstrInfo::insertSelect(MachineBasicBlock &MBB, 3500 MachineBasicBlock::iterator I, 3501 const DebugLoc &DL, Register DstReg, 3502 ArrayRef<MachineOperand> Cond, Register TrueReg, 3503 Register FalseReg) const { 3504 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3505 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 3506 const TargetRegisterClass &RC = *MRI.getRegClass(DstReg); 3507 assert(Cond.size() == 1 && "Invalid Cond array"); 3508 unsigned Opc = X86::getCMovOpcode(TRI.getRegSizeInBits(RC) / 8, 3509 false /*HasMemoryOperand*/); 3510 BuildMI(MBB, I, DL, get(Opc), DstReg) 3511 .addReg(FalseReg) 3512 .addReg(TrueReg) 3513 .addImm(Cond[0].getImm()); 3514 } 3515 3516 /// Test if the given register is a physical h register. 3517 static bool isHReg(unsigned Reg) { 3518 return X86::GR8_ABCD_HRegClass.contains(Reg); 3519 } 3520 3521 // Try and copy between VR128/VR64 and GR64 registers. 3522 static unsigned CopyToFromAsymmetricReg(unsigned DestReg, unsigned SrcReg, 3523 const X86Subtarget &Subtarget) { 3524 bool HasAVX = Subtarget.hasAVX(); 3525 bool HasAVX512 = Subtarget.hasAVX512(); 3526 3527 // SrcReg(MaskReg) -> DestReg(GR64) 3528 // SrcReg(MaskReg) -> DestReg(GR32) 3529 3530 // All KMASK RegClasses hold the same k registers, can be tested against anyone. 3531 if (X86::VK16RegClass.contains(SrcReg)) { 3532 if (X86::GR64RegClass.contains(DestReg)) { 3533 assert(Subtarget.hasBWI()); 3534 return X86::KMOVQrk; 3535 } 3536 if (X86::GR32RegClass.contains(DestReg)) 3537 return Subtarget.hasBWI() ? X86::KMOVDrk : X86::KMOVWrk; 3538 } 3539 3540 // SrcReg(GR64) -> DestReg(MaskReg) 3541 // SrcReg(GR32) -> DestReg(MaskReg) 3542 3543 // All KMASK RegClasses hold the same k registers, can be tested against anyone. 3544 if (X86::VK16RegClass.contains(DestReg)) { 3545 if (X86::GR64RegClass.contains(SrcReg)) { 3546 assert(Subtarget.hasBWI()); 3547 return X86::KMOVQkr; 3548 } 3549 if (X86::GR32RegClass.contains(SrcReg)) 3550 return Subtarget.hasBWI() ? X86::KMOVDkr : X86::KMOVWkr; 3551 } 3552 3553 3554 // SrcReg(VR128) -> DestReg(GR64) 3555 // SrcReg(VR64) -> DestReg(GR64) 3556 // SrcReg(GR64) -> DestReg(VR128) 3557 // SrcReg(GR64) -> DestReg(VR64) 3558 3559 if (X86::GR64RegClass.contains(DestReg)) { 3560 if (X86::VR128XRegClass.contains(SrcReg)) 3561 // Copy from a VR128 register to a GR64 register. 3562 return HasAVX512 ? X86::VMOVPQIto64Zrr : 3563 HasAVX ? X86::VMOVPQIto64rr : 3564 X86::MOVPQIto64rr; 3565 if (X86::VR64RegClass.contains(SrcReg)) 3566 // Copy from a VR64 register to a GR64 register. 3567 return X86::MMX_MOVD64from64rr; 3568 } else if (X86::GR64RegClass.contains(SrcReg)) { 3569 // Copy from a GR64 register to a VR128 register. 3570 if (X86::VR128XRegClass.contains(DestReg)) 3571 return HasAVX512 ? X86::VMOV64toPQIZrr : 3572 HasAVX ? X86::VMOV64toPQIrr : 3573 X86::MOV64toPQIrr; 3574 // Copy from a GR64 register to a VR64 register. 3575 if (X86::VR64RegClass.contains(DestReg)) 3576 return X86::MMX_MOVD64to64rr; 3577 } 3578 3579 // SrcReg(VR128) -> DestReg(GR32) 3580 // SrcReg(GR32) -> DestReg(VR128) 3581 3582 if (X86::GR32RegClass.contains(DestReg) && 3583 X86::VR128XRegClass.contains(SrcReg)) 3584 // Copy from a VR128 register to a GR32 register. 3585 return HasAVX512 ? X86::VMOVPDI2DIZrr : 3586 HasAVX ? X86::VMOVPDI2DIrr : 3587 X86::MOVPDI2DIrr; 3588 3589 if (X86::VR128XRegClass.contains(DestReg) && 3590 X86::GR32RegClass.contains(SrcReg)) 3591 // Copy from a VR128 register to a VR128 register. 3592 return HasAVX512 ? X86::VMOVDI2PDIZrr : 3593 HasAVX ? X86::VMOVDI2PDIrr : 3594 X86::MOVDI2PDIrr; 3595 return 0; 3596 } 3597 3598 void X86InstrInfo::copyPhysReg(MachineBasicBlock &MBB, 3599 MachineBasicBlock::iterator MI, 3600 const DebugLoc &DL, MCRegister DestReg, 3601 MCRegister SrcReg, bool KillSrc) const { 3602 // First deal with the normal symmetric copies. 3603 bool HasAVX = Subtarget.hasAVX(); 3604 bool HasVLX = Subtarget.hasVLX(); 3605 unsigned Opc = 0; 3606 if (X86::GR64RegClass.contains(DestReg, SrcReg)) 3607 Opc = X86::MOV64rr; 3608 else if (X86::GR32RegClass.contains(DestReg, SrcReg)) 3609 Opc = X86::MOV32rr; 3610 else if (X86::GR16RegClass.contains(DestReg, SrcReg)) 3611 Opc = X86::MOV16rr; 3612 else if (X86::GR8RegClass.contains(DestReg, SrcReg)) { 3613 // Copying to or from a physical H register on x86-64 requires a NOREX 3614 // move. Otherwise use a normal move. 3615 if ((isHReg(DestReg) || isHReg(SrcReg)) && 3616 Subtarget.is64Bit()) { 3617 Opc = X86::MOV8rr_NOREX; 3618 // Both operands must be encodable without an REX prefix. 3619 assert(X86::GR8_NOREXRegClass.contains(SrcReg, DestReg) && 3620 "8-bit H register can not be copied outside GR8_NOREX"); 3621 } else 3622 Opc = X86::MOV8rr; 3623 } 3624 else if (X86::VR64RegClass.contains(DestReg, SrcReg)) 3625 Opc = X86::MMX_MOVQ64rr; 3626 else if (X86::VR128XRegClass.contains(DestReg, SrcReg)) { 3627 if (HasVLX) 3628 Opc = X86::VMOVAPSZ128rr; 3629 else if (X86::VR128RegClass.contains(DestReg, SrcReg)) 3630 Opc = HasAVX ? X86::VMOVAPSrr : X86::MOVAPSrr; 3631 else { 3632 // If this an extended register and we don't have VLX we need to use a 3633 // 512-bit move. 3634 Opc = X86::VMOVAPSZrr; 3635 const TargetRegisterInfo *TRI = &getRegisterInfo(); 3636 DestReg = TRI->getMatchingSuperReg(DestReg, X86::sub_xmm, 3637 &X86::VR512RegClass); 3638 SrcReg = TRI->getMatchingSuperReg(SrcReg, X86::sub_xmm, 3639 &X86::VR512RegClass); 3640 } 3641 } else if (X86::VR256XRegClass.contains(DestReg, SrcReg)) { 3642 if (HasVLX) 3643 Opc = X86::VMOVAPSZ256rr; 3644 else if (X86::VR256RegClass.contains(DestReg, SrcReg)) 3645 Opc = X86::VMOVAPSYrr; 3646 else { 3647 // If this an extended register and we don't have VLX we need to use a 3648 // 512-bit move. 3649 Opc = X86::VMOVAPSZrr; 3650 const TargetRegisterInfo *TRI = &getRegisterInfo(); 3651 DestReg = TRI->getMatchingSuperReg(DestReg, X86::sub_ymm, 3652 &X86::VR512RegClass); 3653 SrcReg = TRI->getMatchingSuperReg(SrcReg, X86::sub_ymm, 3654 &X86::VR512RegClass); 3655 } 3656 } else if (X86::VR512RegClass.contains(DestReg, SrcReg)) 3657 Opc = X86::VMOVAPSZrr; 3658 // All KMASK RegClasses hold the same k registers, can be tested against anyone. 3659 else if (X86::VK16RegClass.contains(DestReg, SrcReg)) 3660 Opc = Subtarget.hasBWI() ? X86::KMOVQkk : X86::KMOVWkk; 3661 if (!Opc) 3662 Opc = CopyToFromAsymmetricReg(DestReg, SrcReg, Subtarget); 3663 3664 if (Opc) { 3665 BuildMI(MBB, MI, DL, get(Opc), DestReg) 3666 .addReg(SrcReg, getKillRegState(KillSrc)); 3667 return; 3668 } 3669 3670 if (SrcReg == X86::EFLAGS || DestReg == X86::EFLAGS) { 3671 // FIXME: We use a fatal error here because historically LLVM has tried 3672 // lower some of these physreg copies and we want to ensure we get 3673 // reasonable bug reports if someone encounters a case no other testing 3674 // found. This path should be removed after the LLVM 7 release. 3675 report_fatal_error("Unable to copy EFLAGS physical register!"); 3676 } 3677 3678 LLVM_DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg) << " to " 3679 << RI.getName(DestReg) << '\n'); 3680 report_fatal_error("Cannot emit physreg copy instruction"); 3681 } 3682 3683 Optional<DestSourcePair> 3684 X86InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const { 3685 if (MI.isMoveReg()) 3686 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 3687 return None; 3688 } 3689 3690 static unsigned getLoadStoreRegOpcode(Register Reg, 3691 const TargetRegisterClass *RC, 3692 bool IsStackAligned, 3693 const X86Subtarget &STI, bool load) { 3694 bool HasAVX = STI.hasAVX(); 3695 bool HasAVX512 = STI.hasAVX512(); 3696 bool HasVLX = STI.hasVLX(); 3697 3698 switch (STI.getRegisterInfo()->getSpillSize(*RC)) { 3699 default: 3700 llvm_unreachable("Unknown spill size"); 3701 case 1: 3702 assert(X86::GR8RegClass.hasSubClassEq(RC) && "Unknown 1-byte regclass"); 3703 if (STI.is64Bit()) 3704 // Copying to or from a physical H register on x86-64 requires a NOREX 3705 // move. Otherwise use a normal move. 3706 if (isHReg(Reg) || X86::GR8_ABCD_HRegClass.hasSubClassEq(RC)) 3707 return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX; 3708 return load ? X86::MOV8rm : X86::MOV8mr; 3709 case 2: 3710 if (X86::VK16RegClass.hasSubClassEq(RC)) 3711 return load ? X86::KMOVWkm : X86::KMOVWmk; 3712 if (X86::FR16XRegClass.hasSubClassEq(RC)) { 3713 assert(STI.hasFP16()); 3714 return load ? X86::VMOVSHZrm_alt : X86::VMOVSHZmr; 3715 } 3716 assert(X86::GR16RegClass.hasSubClassEq(RC) && "Unknown 2-byte regclass"); 3717 return load ? X86::MOV16rm : X86::MOV16mr; 3718 case 4: 3719 if (X86::GR32RegClass.hasSubClassEq(RC)) 3720 return load ? X86::MOV32rm : X86::MOV32mr; 3721 if (X86::FR32XRegClass.hasSubClassEq(RC)) 3722 return load ? 3723 (HasAVX512 ? X86::VMOVSSZrm_alt : 3724 HasAVX ? X86::VMOVSSrm_alt : 3725 X86::MOVSSrm_alt) : 3726 (HasAVX512 ? X86::VMOVSSZmr : 3727 HasAVX ? X86::VMOVSSmr : 3728 X86::MOVSSmr); 3729 if (X86::RFP32RegClass.hasSubClassEq(RC)) 3730 return load ? X86::LD_Fp32m : X86::ST_Fp32m; 3731 if (X86::VK32RegClass.hasSubClassEq(RC)) { 3732 assert(STI.hasBWI() && "KMOVD requires BWI"); 3733 return load ? X86::KMOVDkm : X86::KMOVDmk; 3734 } 3735 // All of these mask pair classes have the same spill size, the same kind 3736 // of kmov instructions can be used with all of them. 3737 if (X86::VK1PAIRRegClass.hasSubClassEq(RC) || 3738 X86::VK2PAIRRegClass.hasSubClassEq(RC) || 3739 X86::VK4PAIRRegClass.hasSubClassEq(RC) || 3740 X86::VK8PAIRRegClass.hasSubClassEq(RC) || 3741 X86::VK16PAIRRegClass.hasSubClassEq(RC)) 3742 return load ? X86::MASKPAIR16LOAD : X86::MASKPAIR16STORE; 3743 llvm_unreachable("Unknown 4-byte regclass"); 3744 case 8: 3745 if (X86::GR64RegClass.hasSubClassEq(RC)) 3746 return load ? X86::MOV64rm : X86::MOV64mr; 3747 if (X86::FR64XRegClass.hasSubClassEq(RC)) 3748 return load ? 3749 (HasAVX512 ? X86::VMOVSDZrm_alt : 3750 HasAVX ? X86::VMOVSDrm_alt : 3751 X86::MOVSDrm_alt) : 3752 (HasAVX512 ? X86::VMOVSDZmr : 3753 HasAVX ? X86::VMOVSDmr : 3754 X86::MOVSDmr); 3755 if (X86::VR64RegClass.hasSubClassEq(RC)) 3756 return load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr; 3757 if (X86::RFP64RegClass.hasSubClassEq(RC)) 3758 return load ? X86::LD_Fp64m : X86::ST_Fp64m; 3759 if (X86::VK64RegClass.hasSubClassEq(RC)) { 3760 assert(STI.hasBWI() && "KMOVQ requires BWI"); 3761 return load ? X86::KMOVQkm : X86::KMOVQmk; 3762 } 3763 llvm_unreachable("Unknown 8-byte regclass"); 3764 case 10: 3765 assert(X86::RFP80RegClass.hasSubClassEq(RC) && "Unknown 10-byte regclass"); 3766 return load ? X86::LD_Fp80m : X86::ST_FpP80m; 3767 case 16: { 3768 if (X86::VR128XRegClass.hasSubClassEq(RC)) { 3769 // If stack is realigned we can use aligned stores. 3770 if (IsStackAligned) 3771 return load ? 3772 (HasVLX ? X86::VMOVAPSZ128rm : 3773 HasAVX512 ? X86::VMOVAPSZ128rm_NOVLX : 3774 HasAVX ? X86::VMOVAPSrm : 3775 X86::MOVAPSrm): 3776 (HasVLX ? X86::VMOVAPSZ128mr : 3777 HasAVX512 ? X86::VMOVAPSZ128mr_NOVLX : 3778 HasAVX ? X86::VMOVAPSmr : 3779 X86::MOVAPSmr); 3780 else 3781 return load ? 3782 (HasVLX ? X86::VMOVUPSZ128rm : 3783 HasAVX512 ? X86::VMOVUPSZ128rm_NOVLX : 3784 HasAVX ? X86::VMOVUPSrm : 3785 X86::MOVUPSrm): 3786 (HasVLX ? X86::VMOVUPSZ128mr : 3787 HasAVX512 ? X86::VMOVUPSZ128mr_NOVLX : 3788 HasAVX ? X86::VMOVUPSmr : 3789 X86::MOVUPSmr); 3790 } 3791 llvm_unreachable("Unknown 16-byte regclass"); 3792 } 3793 case 32: 3794 assert(X86::VR256XRegClass.hasSubClassEq(RC) && "Unknown 32-byte regclass"); 3795 // If stack is realigned we can use aligned stores. 3796 if (IsStackAligned) 3797 return load ? 3798 (HasVLX ? X86::VMOVAPSZ256rm : 3799 HasAVX512 ? X86::VMOVAPSZ256rm_NOVLX : 3800 X86::VMOVAPSYrm) : 3801 (HasVLX ? X86::VMOVAPSZ256mr : 3802 HasAVX512 ? X86::VMOVAPSZ256mr_NOVLX : 3803 X86::VMOVAPSYmr); 3804 else 3805 return load ? 3806 (HasVLX ? X86::VMOVUPSZ256rm : 3807 HasAVX512 ? X86::VMOVUPSZ256rm_NOVLX : 3808 X86::VMOVUPSYrm) : 3809 (HasVLX ? X86::VMOVUPSZ256mr : 3810 HasAVX512 ? X86::VMOVUPSZ256mr_NOVLX : 3811 X86::VMOVUPSYmr); 3812 case 64: 3813 assert(X86::VR512RegClass.hasSubClassEq(RC) && "Unknown 64-byte regclass"); 3814 assert(STI.hasAVX512() && "Using 512-bit register requires AVX512"); 3815 if (IsStackAligned) 3816 return load ? X86::VMOVAPSZrm : X86::VMOVAPSZmr; 3817 else 3818 return load ? X86::VMOVUPSZrm : X86::VMOVUPSZmr; 3819 } 3820 } 3821 3822 Optional<ExtAddrMode> 3823 X86InstrInfo::getAddrModeFromMemoryOp(const MachineInstr &MemI, 3824 const TargetRegisterInfo *TRI) const { 3825 const MCInstrDesc &Desc = MemI.getDesc(); 3826 int MemRefBegin = X86II::getMemoryOperandNo(Desc.TSFlags); 3827 if (MemRefBegin < 0) 3828 return None; 3829 3830 MemRefBegin += X86II::getOperandBias(Desc); 3831 3832 auto &BaseOp = MemI.getOperand(MemRefBegin + X86::AddrBaseReg); 3833 if (!BaseOp.isReg()) // Can be an MO_FrameIndex 3834 return None; 3835 3836 const MachineOperand &DispMO = MemI.getOperand(MemRefBegin + X86::AddrDisp); 3837 // Displacement can be symbolic 3838 if (!DispMO.isImm()) 3839 return None; 3840 3841 ExtAddrMode AM; 3842 AM.BaseReg = BaseOp.getReg(); 3843 AM.ScaledReg = MemI.getOperand(MemRefBegin + X86::AddrIndexReg).getReg(); 3844 AM.Scale = MemI.getOperand(MemRefBegin + X86::AddrScaleAmt).getImm(); 3845 AM.Displacement = DispMO.getImm(); 3846 return AM; 3847 } 3848 3849 bool X86InstrInfo::getConstValDefinedInReg(const MachineInstr &MI, 3850 const Register Reg, 3851 int64_t &ImmVal) const { 3852 if (MI.getOpcode() != X86::MOV32ri && MI.getOpcode() != X86::MOV64ri) 3853 return false; 3854 // Mov Src can be a global address. 3855 if (!MI.getOperand(1).isImm() || MI.getOperand(0).getReg() != Reg) 3856 return false; 3857 ImmVal = MI.getOperand(1).getImm(); 3858 return true; 3859 } 3860 3861 bool X86InstrInfo::preservesZeroValueInReg( 3862 const MachineInstr *MI, const Register NullValueReg, 3863 const TargetRegisterInfo *TRI) const { 3864 if (!MI->modifiesRegister(NullValueReg, TRI)) 3865 return true; 3866 switch (MI->getOpcode()) { 3867 // Shift right/left of a null unto itself is still a null, i.e. rax = shl rax 3868 // X. 3869 case X86::SHR64ri: 3870 case X86::SHR32ri: 3871 case X86::SHL64ri: 3872 case X86::SHL32ri: 3873 assert(MI->getOperand(0).isDef() && MI->getOperand(1).isUse() && 3874 "expected for shift opcode!"); 3875 return MI->getOperand(0).getReg() == NullValueReg && 3876 MI->getOperand(1).getReg() == NullValueReg; 3877 // Zero extend of a sub-reg of NullValueReg into itself does not change the 3878 // null value. 3879 case X86::MOV32rr: 3880 return llvm::all_of(MI->operands(), [&](const MachineOperand &MO) { 3881 return TRI->isSubRegisterEq(NullValueReg, MO.getReg()); 3882 }); 3883 default: 3884 return false; 3885 } 3886 llvm_unreachable("Should be handled above!"); 3887 } 3888 3889 bool X86InstrInfo::getMemOperandsWithOffsetWidth( 3890 const MachineInstr &MemOp, SmallVectorImpl<const MachineOperand *> &BaseOps, 3891 int64_t &Offset, bool &OffsetIsScalable, unsigned &Width, 3892 const TargetRegisterInfo *TRI) const { 3893 const MCInstrDesc &Desc = MemOp.getDesc(); 3894 int MemRefBegin = X86II::getMemoryOperandNo(Desc.TSFlags); 3895 if (MemRefBegin < 0) 3896 return false; 3897 3898 MemRefBegin += X86II::getOperandBias(Desc); 3899 3900 const MachineOperand *BaseOp = 3901 &MemOp.getOperand(MemRefBegin + X86::AddrBaseReg); 3902 if (!BaseOp->isReg()) // Can be an MO_FrameIndex 3903 return false; 3904 3905 if (MemOp.getOperand(MemRefBegin + X86::AddrScaleAmt).getImm() != 1) 3906 return false; 3907 3908 if (MemOp.getOperand(MemRefBegin + X86::AddrIndexReg).getReg() != 3909 X86::NoRegister) 3910 return false; 3911 3912 const MachineOperand &DispMO = MemOp.getOperand(MemRefBegin + X86::AddrDisp); 3913 3914 // Displacement can be symbolic 3915 if (!DispMO.isImm()) 3916 return false; 3917 3918 Offset = DispMO.getImm(); 3919 3920 if (!BaseOp->isReg()) 3921 return false; 3922 3923 OffsetIsScalable = false; 3924 // FIXME: Relying on memoperands() may not be right thing to do here. Check 3925 // with X86 maintainers, and fix it accordingly. For now, it is ok, since 3926 // there is no use of `Width` for X86 back-end at the moment. 3927 Width = 3928 !MemOp.memoperands_empty() ? MemOp.memoperands().front()->getSize() : 0; 3929 BaseOps.push_back(BaseOp); 3930 return true; 3931 } 3932 3933 static unsigned getStoreRegOpcode(Register SrcReg, 3934 const TargetRegisterClass *RC, 3935 bool IsStackAligned, 3936 const X86Subtarget &STI) { 3937 return getLoadStoreRegOpcode(SrcReg, RC, IsStackAligned, STI, false); 3938 } 3939 3940 static unsigned getLoadRegOpcode(Register DestReg, 3941 const TargetRegisterClass *RC, 3942 bool IsStackAligned, const X86Subtarget &STI) { 3943 return getLoadStoreRegOpcode(DestReg, RC, IsStackAligned, STI, true); 3944 } 3945 3946 void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 3947 MachineBasicBlock::iterator MI, 3948 Register SrcReg, bool isKill, int FrameIdx, 3949 const TargetRegisterClass *RC, 3950 const TargetRegisterInfo *TRI) const { 3951 const MachineFunction &MF = *MBB.getParent(); 3952 const MachineFrameInfo &MFI = MF.getFrameInfo(); 3953 assert(MFI.getObjectSize(FrameIdx) >= TRI->getSpillSize(*RC) && 3954 "Stack slot too small for store"); 3955 if (RC->getID() == X86::TILERegClassID) { 3956 unsigned Opc = X86::TILESTORED; 3957 // tilestored %tmm, (%sp, %idx) 3958 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo(); 3959 Register VirtReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass); 3960 BuildMI(MBB, MI, DebugLoc(), get(X86::MOV64ri), VirtReg).addImm(64); 3961 MachineInstr *NewMI = 3962 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc)), FrameIdx) 3963 .addReg(SrcReg, getKillRegState(isKill)); 3964 MachineOperand &MO = NewMI->getOperand(2); 3965 MO.setReg(VirtReg); 3966 MO.setIsKill(true); 3967 } else { 3968 unsigned Alignment = std::max<uint32_t>(TRI->getSpillSize(*RC), 16); 3969 bool isAligned = 3970 (Subtarget.getFrameLowering()->getStackAlign() >= Alignment) || 3971 (RI.canRealignStack(MF) && !MFI.isFixedObjectIndex(FrameIdx)); 3972 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, Subtarget); 3973 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc)), FrameIdx) 3974 .addReg(SrcReg, getKillRegState(isKill)); 3975 } 3976 } 3977 3978 void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 3979 MachineBasicBlock::iterator MI, 3980 Register DestReg, int FrameIdx, 3981 const TargetRegisterClass *RC, 3982 const TargetRegisterInfo *TRI) const { 3983 if (RC->getID() == X86::TILERegClassID) { 3984 unsigned Opc = X86::TILELOADD; 3985 // tileloadd (%sp, %idx), %tmm 3986 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo(); 3987 Register VirtReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass); 3988 MachineInstr *NewMI = 3989 BuildMI(MBB, MI, DebugLoc(), get(X86::MOV64ri), VirtReg).addImm(64); 3990 NewMI = addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc), DestReg), 3991 FrameIdx); 3992 MachineOperand &MO = NewMI->getOperand(3); 3993 MO.setReg(VirtReg); 3994 MO.setIsKill(true); 3995 } else { 3996 const MachineFunction &MF = *MBB.getParent(); 3997 const MachineFrameInfo &MFI = MF.getFrameInfo(); 3998 unsigned Alignment = std::max<uint32_t>(TRI->getSpillSize(*RC), 16); 3999 bool isAligned = 4000 (Subtarget.getFrameLowering()->getStackAlign() >= Alignment) || 4001 (RI.canRealignStack(MF) && !MFI.isFixedObjectIndex(FrameIdx)); 4002 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, Subtarget); 4003 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc), DestReg), 4004 FrameIdx); 4005 } 4006 } 4007 4008 bool X86InstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 4009 Register &SrcReg2, int64_t &CmpMask, 4010 int64_t &CmpValue) const { 4011 switch (MI.getOpcode()) { 4012 default: break; 4013 case X86::CMP64ri32: 4014 case X86::CMP64ri8: 4015 case X86::CMP32ri: 4016 case X86::CMP32ri8: 4017 case X86::CMP16ri: 4018 case X86::CMP16ri8: 4019 case X86::CMP8ri: 4020 SrcReg = MI.getOperand(0).getReg(); 4021 SrcReg2 = 0; 4022 if (MI.getOperand(1).isImm()) { 4023 CmpMask = ~0; 4024 CmpValue = MI.getOperand(1).getImm(); 4025 } else { 4026 CmpMask = CmpValue = 0; 4027 } 4028 return true; 4029 // A SUB can be used to perform comparison. 4030 case X86::SUB64rm: 4031 case X86::SUB32rm: 4032 case X86::SUB16rm: 4033 case X86::SUB8rm: 4034 SrcReg = MI.getOperand(1).getReg(); 4035 SrcReg2 = 0; 4036 CmpMask = 0; 4037 CmpValue = 0; 4038 return true; 4039 case X86::SUB64rr: 4040 case X86::SUB32rr: 4041 case X86::SUB16rr: 4042 case X86::SUB8rr: 4043 SrcReg = MI.getOperand(1).getReg(); 4044 SrcReg2 = MI.getOperand(2).getReg(); 4045 CmpMask = 0; 4046 CmpValue = 0; 4047 return true; 4048 case X86::SUB64ri32: 4049 case X86::SUB64ri8: 4050 case X86::SUB32ri: 4051 case X86::SUB32ri8: 4052 case X86::SUB16ri: 4053 case X86::SUB16ri8: 4054 case X86::SUB8ri: 4055 SrcReg = MI.getOperand(1).getReg(); 4056 SrcReg2 = 0; 4057 if (MI.getOperand(2).isImm()) { 4058 CmpMask = ~0; 4059 CmpValue = MI.getOperand(2).getImm(); 4060 } else { 4061 CmpMask = CmpValue = 0; 4062 } 4063 return true; 4064 case X86::CMP64rr: 4065 case X86::CMP32rr: 4066 case X86::CMP16rr: 4067 case X86::CMP8rr: 4068 SrcReg = MI.getOperand(0).getReg(); 4069 SrcReg2 = MI.getOperand(1).getReg(); 4070 CmpMask = 0; 4071 CmpValue = 0; 4072 return true; 4073 case X86::TEST8rr: 4074 case X86::TEST16rr: 4075 case X86::TEST32rr: 4076 case X86::TEST64rr: 4077 SrcReg = MI.getOperand(0).getReg(); 4078 if (MI.getOperand(1).getReg() != SrcReg) 4079 return false; 4080 // Compare against zero. 4081 SrcReg2 = 0; 4082 CmpMask = ~0; 4083 CmpValue = 0; 4084 return true; 4085 } 4086 return false; 4087 } 4088 4089 bool X86InstrInfo::isRedundantFlagInstr(const MachineInstr &FlagI, 4090 Register SrcReg, Register SrcReg2, 4091 int64_t ImmMask, int64_t ImmValue, 4092 const MachineInstr &OI, bool *IsSwapped, 4093 int64_t *ImmDelta) const { 4094 switch (OI.getOpcode()) { 4095 case X86::CMP64rr: 4096 case X86::CMP32rr: 4097 case X86::CMP16rr: 4098 case X86::CMP8rr: 4099 case X86::SUB64rr: 4100 case X86::SUB32rr: 4101 case X86::SUB16rr: 4102 case X86::SUB8rr: { 4103 Register OISrcReg; 4104 Register OISrcReg2; 4105 int64_t OIMask; 4106 int64_t OIValue; 4107 if (!analyzeCompare(OI, OISrcReg, OISrcReg2, OIMask, OIValue) || 4108 OIMask != ImmMask || OIValue != ImmValue) 4109 return false; 4110 if (SrcReg == OISrcReg && SrcReg2 == OISrcReg2) { 4111 *IsSwapped = false; 4112 return true; 4113 } 4114 if (SrcReg == OISrcReg2 && SrcReg2 == OISrcReg) { 4115 *IsSwapped = true; 4116 return true; 4117 } 4118 return false; 4119 } 4120 case X86::CMP64ri32: 4121 case X86::CMP64ri8: 4122 case X86::CMP32ri: 4123 case X86::CMP32ri8: 4124 case X86::CMP16ri: 4125 case X86::CMP16ri8: 4126 case X86::CMP8ri: 4127 case X86::SUB64ri32: 4128 case X86::SUB64ri8: 4129 case X86::SUB32ri: 4130 case X86::SUB32ri8: 4131 case X86::SUB16ri: 4132 case X86::SUB16ri8: 4133 case X86::SUB8ri: 4134 case X86::TEST64rr: 4135 case X86::TEST32rr: 4136 case X86::TEST16rr: 4137 case X86::TEST8rr: { 4138 if (ImmMask != 0) { 4139 Register OISrcReg; 4140 Register OISrcReg2; 4141 int64_t OIMask; 4142 int64_t OIValue; 4143 if (analyzeCompare(OI, OISrcReg, OISrcReg2, OIMask, OIValue) && 4144 SrcReg == OISrcReg && ImmMask == OIMask) { 4145 if (OIValue == ImmValue) { 4146 *ImmDelta = 0; 4147 return true; 4148 } else if (static_cast<uint64_t>(ImmValue) == 4149 static_cast<uint64_t>(OIValue) - 1) { 4150 *ImmDelta = -1; 4151 return true; 4152 } else if (static_cast<uint64_t>(ImmValue) == 4153 static_cast<uint64_t>(OIValue) + 1) { 4154 *ImmDelta = 1; 4155 return true; 4156 } else { 4157 return false; 4158 } 4159 } 4160 } 4161 return FlagI.isIdenticalTo(OI); 4162 } 4163 default: 4164 return false; 4165 } 4166 } 4167 4168 /// Check whether the definition can be converted 4169 /// to remove a comparison against zero. 4170 inline static bool isDefConvertible(const MachineInstr &MI, bool &NoSignFlag, 4171 bool &ClearsOverflowFlag) { 4172 NoSignFlag = false; 4173 ClearsOverflowFlag = false; 4174 4175 switch (MI.getOpcode()) { 4176 default: return false; 4177 4178 // The shift instructions only modify ZF if their shift count is non-zero. 4179 // N.B.: The processor truncates the shift count depending on the encoding. 4180 case X86::SAR8ri: case X86::SAR16ri: case X86::SAR32ri:case X86::SAR64ri: 4181 case X86::SHR8ri: case X86::SHR16ri: case X86::SHR32ri:case X86::SHR64ri: 4182 return getTruncatedShiftCount(MI, 2) != 0; 4183 4184 // Some left shift instructions can be turned into LEA instructions but only 4185 // if their flags aren't used. Avoid transforming such instructions. 4186 case X86::SHL8ri: case X86::SHL16ri: case X86::SHL32ri:case X86::SHL64ri:{ 4187 unsigned ShAmt = getTruncatedShiftCount(MI, 2); 4188 if (isTruncatedShiftCountForLEA(ShAmt)) return false; 4189 return ShAmt != 0; 4190 } 4191 4192 case X86::SHRD16rri8:case X86::SHRD32rri8:case X86::SHRD64rri8: 4193 case X86::SHLD16rri8:case X86::SHLD32rri8:case X86::SHLD64rri8: 4194 return getTruncatedShiftCount(MI, 3) != 0; 4195 4196 case X86::SUB64ri32: case X86::SUB64ri8: case X86::SUB32ri: 4197 case X86::SUB32ri8: case X86::SUB16ri: case X86::SUB16ri8: 4198 case X86::SUB8ri: case X86::SUB64rr: case X86::SUB32rr: 4199 case X86::SUB16rr: case X86::SUB8rr: case X86::SUB64rm: 4200 case X86::SUB32rm: case X86::SUB16rm: case X86::SUB8rm: 4201 case X86::DEC64r: case X86::DEC32r: case X86::DEC16r: case X86::DEC8r: 4202 case X86::ADD64ri32: case X86::ADD64ri8: case X86::ADD32ri: 4203 case X86::ADD32ri8: case X86::ADD16ri: case X86::ADD16ri8: 4204 case X86::ADD8ri: case X86::ADD64rr: case X86::ADD32rr: 4205 case X86::ADD16rr: case X86::ADD8rr: case X86::ADD64rm: 4206 case X86::ADD32rm: case X86::ADD16rm: case X86::ADD8rm: 4207 case X86::INC64r: case X86::INC32r: case X86::INC16r: case X86::INC8r: 4208 case X86::ADC64ri32: case X86::ADC64ri8: case X86::ADC32ri: 4209 case X86::ADC32ri8: case X86::ADC16ri: case X86::ADC16ri8: 4210 case X86::ADC8ri: case X86::ADC64rr: case X86::ADC32rr: 4211 case X86::ADC16rr: case X86::ADC8rr: case X86::ADC64rm: 4212 case X86::ADC32rm: case X86::ADC16rm: case X86::ADC8rm: 4213 case X86::SBB64ri32: case X86::SBB64ri8: case X86::SBB32ri: 4214 case X86::SBB32ri8: case X86::SBB16ri: case X86::SBB16ri8: 4215 case X86::SBB8ri: case X86::SBB64rr: case X86::SBB32rr: 4216 case X86::SBB16rr: case X86::SBB8rr: case X86::SBB64rm: 4217 case X86::SBB32rm: case X86::SBB16rm: case X86::SBB8rm: 4218 case X86::NEG8r: case X86::NEG16r: case X86::NEG32r: case X86::NEG64r: 4219 case X86::SAR8r1: case X86::SAR16r1: case X86::SAR32r1:case X86::SAR64r1: 4220 case X86::SHR8r1: case X86::SHR16r1: case X86::SHR32r1:case X86::SHR64r1: 4221 case X86::SHL8r1: case X86::SHL16r1: case X86::SHL32r1:case X86::SHL64r1: 4222 case X86::LZCNT16rr: case X86::LZCNT16rm: 4223 case X86::LZCNT32rr: case X86::LZCNT32rm: 4224 case X86::LZCNT64rr: case X86::LZCNT64rm: 4225 case X86::POPCNT16rr:case X86::POPCNT16rm: 4226 case X86::POPCNT32rr:case X86::POPCNT32rm: 4227 case X86::POPCNT64rr:case X86::POPCNT64rm: 4228 case X86::TZCNT16rr: case X86::TZCNT16rm: 4229 case X86::TZCNT32rr: case X86::TZCNT32rm: 4230 case X86::TZCNT64rr: case X86::TZCNT64rm: 4231 return true; 4232 case X86::AND64ri32: case X86::AND64ri8: case X86::AND32ri: 4233 case X86::AND32ri8: case X86::AND16ri: case X86::AND16ri8: 4234 case X86::AND8ri: case X86::AND64rr: case X86::AND32rr: 4235 case X86::AND16rr: case X86::AND8rr: case X86::AND64rm: 4236 case X86::AND32rm: case X86::AND16rm: case X86::AND8rm: 4237 case X86::XOR64ri32: case X86::XOR64ri8: case X86::XOR32ri: 4238 case X86::XOR32ri8: case X86::XOR16ri: case X86::XOR16ri8: 4239 case X86::XOR8ri: case X86::XOR64rr: case X86::XOR32rr: 4240 case X86::XOR16rr: case X86::XOR8rr: case X86::XOR64rm: 4241 case X86::XOR32rm: case X86::XOR16rm: case X86::XOR8rm: 4242 case X86::OR64ri32: case X86::OR64ri8: case X86::OR32ri: 4243 case X86::OR32ri8: case X86::OR16ri: case X86::OR16ri8: 4244 case X86::OR8ri: case X86::OR64rr: case X86::OR32rr: 4245 case X86::OR16rr: case X86::OR8rr: case X86::OR64rm: 4246 case X86::OR32rm: case X86::OR16rm: case X86::OR8rm: 4247 case X86::ANDN32rr: case X86::ANDN32rm: 4248 case X86::ANDN64rr: case X86::ANDN64rm: 4249 case X86::BLSI32rr: case X86::BLSI32rm: 4250 case X86::BLSI64rr: case X86::BLSI64rm: 4251 case X86::BLSMSK32rr: case X86::BLSMSK32rm: 4252 case X86::BLSMSK64rr: case X86::BLSMSK64rm: 4253 case X86::BLSR32rr: case X86::BLSR32rm: 4254 case X86::BLSR64rr: case X86::BLSR64rm: 4255 case X86::BLCFILL32rr: case X86::BLCFILL32rm: 4256 case X86::BLCFILL64rr: case X86::BLCFILL64rm: 4257 case X86::BLCI32rr: case X86::BLCI32rm: 4258 case X86::BLCI64rr: case X86::BLCI64rm: 4259 case X86::BLCIC32rr: case X86::BLCIC32rm: 4260 case X86::BLCIC64rr: case X86::BLCIC64rm: 4261 case X86::BLCMSK32rr: case X86::BLCMSK32rm: 4262 case X86::BLCMSK64rr: case X86::BLCMSK64rm: 4263 case X86::BLCS32rr: case X86::BLCS32rm: 4264 case X86::BLCS64rr: case X86::BLCS64rm: 4265 case X86::BLSFILL32rr: case X86::BLSFILL32rm: 4266 case X86::BLSFILL64rr: case X86::BLSFILL64rm: 4267 case X86::BLSIC32rr: case X86::BLSIC32rm: 4268 case X86::BLSIC64rr: case X86::BLSIC64rm: 4269 case X86::BZHI32rr: case X86::BZHI32rm: 4270 case X86::BZHI64rr: case X86::BZHI64rm: 4271 case X86::T1MSKC32rr: case X86::T1MSKC32rm: 4272 case X86::T1MSKC64rr: case X86::T1MSKC64rm: 4273 case X86::TZMSK32rr: case X86::TZMSK32rm: 4274 case X86::TZMSK64rr: case X86::TZMSK64rm: 4275 // These instructions clear the overflow flag just like TEST. 4276 // FIXME: These are not the only instructions in this switch that clear the 4277 // overflow flag. 4278 ClearsOverflowFlag = true; 4279 return true; 4280 case X86::BEXTR32rr: case X86::BEXTR64rr: 4281 case X86::BEXTR32rm: case X86::BEXTR64rm: 4282 case X86::BEXTRI32ri: case X86::BEXTRI32mi: 4283 case X86::BEXTRI64ri: case X86::BEXTRI64mi: 4284 // BEXTR doesn't update the sign flag so we can't use it. It does clear 4285 // the overflow flag, but that's not useful without the sign flag. 4286 NoSignFlag = true; 4287 return true; 4288 } 4289 } 4290 4291 /// Check whether the use can be converted to remove a comparison against zero. 4292 static X86::CondCode isUseDefConvertible(const MachineInstr &MI) { 4293 switch (MI.getOpcode()) { 4294 default: return X86::COND_INVALID; 4295 case X86::NEG8r: 4296 case X86::NEG16r: 4297 case X86::NEG32r: 4298 case X86::NEG64r: 4299 return X86::COND_AE; 4300 case X86::LZCNT16rr: 4301 case X86::LZCNT32rr: 4302 case X86::LZCNT64rr: 4303 return X86::COND_B; 4304 case X86::POPCNT16rr: 4305 case X86::POPCNT32rr: 4306 case X86::POPCNT64rr: 4307 return X86::COND_E; 4308 case X86::TZCNT16rr: 4309 case X86::TZCNT32rr: 4310 case X86::TZCNT64rr: 4311 return X86::COND_B; 4312 case X86::BSF16rr: 4313 case X86::BSF32rr: 4314 case X86::BSF64rr: 4315 case X86::BSR16rr: 4316 case X86::BSR32rr: 4317 case X86::BSR64rr: 4318 return X86::COND_E; 4319 case X86::BLSI32rr: 4320 case X86::BLSI64rr: 4321 return X86::COND_AE; 4322 case X86::BLSR32rr: 4323 case X86::BLSR64rr: 4324 case X86::BLSMSK32rr: 4325 case X86::BLSMSK64rr: 4326 return X86::COND_B; 4327 // TODO: TBM instructions. 4328 } 4329 } 4330 4331 /// Check if there exists an earlier instruction that 4332 /// operates on the same source operands and sets flags in the same way as 4333 /// Compare; remove Compare if possible. 4334 bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, 4335 Register SrcReg2, int64_t CmpMask, 4336 int64_t CmpValue, 4337 const MachineRegisterInfo *MRI) const { 4338 // Check whether we can replace SUB with CMP. 4339 switch (CmpInstr.getOpcode()) { 4340 default: break; 4341 case X86::SUB64ri32: 4342 case X86::SUB64ri8: 4343 case X86::SUB32ri: 4344 case X86::SUB32ri8: 4345 case X86::SUB16ri: 4346 case X86::SUB16ri8: 4347 case X86::SUB8ri: 4348 case X86::SUB64rm: 4349 case X86::SUB32rm: 4350 case X86::SUB16rm: 4351 case X86::SUB8rm: 4352 case X86::SUB64rr: 4353 case X86::SUB32rr: 4354 case X86::SUB16rr: 4355 case X86::SUB8rr: { 4356 if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg())) 4357 return false; 4358 // There is no use of the destination register, we can replace SUB with CMP. 4359 unsigned NewOpcode = 0; 4360 switch (CmpInstr.getOpcode()) { 4361 default: llvm_unreachable("Unreachable!"); 4362 case X86::SUB64rm: NewOpcode = X86::CMP64rm; break; 4363 case X86::SUB32rm: NewOpcode = X86::CMP32rm; break; 4364 case X86::SUB16rm: NewOpcode = X86::CMP16rm; break; 4365 case X86::SUB8rm: NewOpcode = X86::CMP8rm; break; 4366 case X86::SUB64rr: NewOpcode = X86::CMP64rr; break; 4367 case X86::SUB32rr: NewOpcode = X86::CMP32rr; break; 4368 case X86::SUB16rr: NewOpcode = X86::CMP16rr; break; 4369 case X86::SUB8rr: NewOpcode = X86::CMP8rr; break; 4370 case X86::SUB64ri32: NewOpcode = X86::CMP64ri32; break; 4371 case X86::SUB64ri8: NewOpcode = X86::CMP64ri8; break; 4372 case X86::SUB32ri: NewOpcode = X86::CMP32ri; break; 4373 case X86::SUB32ri8: NewOpcode = X86::CMP32ri8; break; 4374 case X86::SUB16ri: NewOpcode = X86::CMP16ri; break; 4375 case X86::SUB16ri8: NewOpcode = X86::CMP16ri8; break; 4376 case X86::SUB8ri: NewOpcode = X86::CMP8ri; break; 4377 } 4378 CmpInstr.setDesc(get(NewOpcode)); 4379 CmpInstr.RemoveOperand(0); 4380 // Mutating this instruction invalidates any debug data associated with it. 4381 CmpInstr.dropDebugNumber(); 4382 // Fall through to optimize Cmp if Cmp is CMPrr or CMPri. 4383 if (NewOpcode == X86::CMP64rm || NewOpcode == X86::CMP32rm || 4384 NewOpcode == X86::CMP16rm || NewOpcode == X86::CMP8rm) 4385 return false; 4386 } 4387 } 4388 4389 // The following code tries to remove the comparison by re-using EFLAGS 4390 // from earlier instructions. 4391 4392 bool IsCmpZero = (CmpMask != 0 && CmpValue == 0); 4393 4394 // Transformation currently requires SSA values. 4395 if (SrcReg2.isPhysical()) 4396 return false; 4397 MachineInstr *SrcRegDef = MRI->getVRegDef(SrcReg); 4398 assert(SrcRegDef && "Must have a definition (SSA)"); 4399 4400 MachineInstr *MI = nullptr; 4401 MachineInstr *Sub = nullptr; 4402 MachineInstr *Movr0Inst = nullptr; 4403 bool NoSignFlag = false; 4404 bool ClearsOverflowFlag = false; 4405 bool ShouldUpdateCC = false; 4406 bool IsSwapped = false; 4407 X86::CondCode NewCC = X86::COND_INVALID; 4408 int64_t ImmDelta = 0; 4409 4410 // Search backward from CmpInstr for the next instruction defining EFLAGS. 4411 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4412 MachineBasicBlock &CmpMBB = *CmpInstr.getParent(); 4413 MachineBasicBlock::reverse_iterator From = 4414 std::next(MachineBasicBlock::reverse_iterator(CmpInstr)); 4415 for (MachineBasicBlock *MBB = &CmpMBB;;) { 4416 for (MachineInstr &Inst : make_range(From, MBB->rend())) { 4417 // Try to use EFLAGS from the instruction defining %SrcReg. Example: 4418 // %eax = addl ... 4419 // ... // EFLAGS not changed 4420 // testl %eax, %eax // <-- can be removed 4421 if (&Inst == SrcRegDef) { 4422 if (IsCmpZero && 4423 isDefConvertible(Inst, NoSignFlag, ClearsOverflowFlag)) { 4424 MI = &Inst; 4425 break; 4426 } 4427 // Cannot find other candidates before definition of SrcReg. 4428 return false; 4429 } 4430 4431 if (Inst.modifiesRegister(X86::EFLAGS, TRI)) { 4432 // Try to use EFLAGS produced by an instruction reading %SrcReg. 4433 // Example: 4434 // %eax = ... 4435 // ... 4436 // popcntl %eax 4437 // ... // EFLAGS not changed 4438 // testl %eax, %eax // <-- can be removed 4439 if (IsCmpZero) { 4440 NewCC = isUseDefConvertible(Inst); 4441 if (NewCC != X86::COND_INVALID && Inst.getOperand(1).isReg() && 4442 Inst.getOperand(1).getReg() == SrcReg) { 4443 ShouldUpdateCC = true; 4444 MI = &Inst; 4445 break; 4446 } 4447 } 4448 4449 // Try to use EFLAGS from an instruction with similar flag results. 4450 // Example: 4451 // sub x, y or cmp x, y 4452 // ... // EFLAGS not changed 4453 // cmp x, y // <-- can be removed 4454 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue, 4455 Inst, &IsSwapped, &ImmDelta)) { 4456 Sub = &Inst; 4457 break; 4458 } 4459 4460 // MOV32r0 is implemented with xor which clobbers condition code. It is 4461 // safe to move up, if the definition to EFLAGS is dead and earlier 4462 // instructions do not read or write EFLAGS. 4463 if (!Movr0Inst && Inst.getOpcode() == X86::MOV32r0 && 4464 Inst.registerDefIsDead(X86::EFLAGS, TRI)) { 4465 Movr0Inst = &Inst; 4466 continue; 4467 } 4468 4469 // Cannot do anything for any other EFLAG changes. 4470 return false; 4471 } 4472 } 4473 4474 if (MI || Sub) 4475 break; 4476 4477 // Reached begin of basic block. Continue in predecessor if there is 4478 // exactly one. 4479 if (MBB->pred_size() != 1) 4480 return false; 4481 MBB = *MBB->pred_begin(); 4482 From = MBB->rbegin(); 4483 } 4484 4485 // Scan forward from the instruction after CmpInstr for uses of EFLAGS. 4486 // It is safe to remove CmpInstr if EFLAGS is redefined or killed. 4487 // If we are done with the basic block, we need to check whether EFLAGS is 4488 // live-out. 4489 bool FlagsMayLiveOut = true; 4490 SmallVector<std::pair<MachineInstr*, X86::CondCode>, 4> OpsToUpdate; 4491 MachineBasicBlock::iterator AfterCmpInstr = 4492 std::next(MachineBasicBlock::iterator(CmpInstr)); 4493 for (MachineInstr &Instr : make_range(AfterCmpInstr, CmpMBB.end())) { 4494 bool ModifyEFLAGS = Instr.modifiesRegister(X86::EFLAGS, TRI); 4495 bool UseEFLAGS = Instr.readsRegister(X86::EFLAGS, TRI); 4496 // We should check the usage if this instruction uses and updates EFLAGS. 4497 if (!UseEFLAGS && ModifyEFLAGS) { 4498 // It is safe to remove CmpInstr if EFLAGS is updated again. 4499 FlagsMayLiveOut = false; 4500 break; 4501 } 4502 if (!UseEFLAGS && !ModifyEFLAGS) 4503 continue; 4504 4505 // EFLAGS is used by this instruction. 4506 X86::CondCode OldCC = X86::COND_INVALID; 4507 if (MI || IsSwapped || ImmDelta != 0) { 4508 // We decode the condition code from opcode. 4509 if (Instr.isBranch()) 4510 OldCC = X86::getCondFromBranch(Instr); 4511 else { 4512 OldCC = X86::getCondFromSETCC(Instr); 4513 if (OldCC == X86::COND_INVALID) 4514 OldCC = X86::getCondFromCMov(Instr); 4515 } 4516 if (OldCC == X86::COND_INVALID) return false; 4517 } 4518 X86::CondCode ReplacementCC = X86::COND_INVALID; 4519 if (MI) { 4520 switch (OldCC) { 4521 default: break; 4522 case X86::COND_A: case X86::COND_AE: 4523 case X86::COND_B: case X86::COND_BE: 4524 // CF is used, we can't perform this optimization. 4525 return false; 4526 case X86::COND_G: case X86::COND_GE: 4527 case X86::COND_L: case X86::COND_LE: 4528 case X86::COND_O: case X86::COND_NO: 4529 // If OF is used, the instruction needs to clear it like CmpZero does. 4530 if (!ClearsOverflowFlag) 4531 return false; 4532 break; 4533 case X86::COND_S: case X86::COND_NS: 4534 // If SF is used, but the instruction doesn't update the SF, then we 4535 // can't do the optimization. 4536 if (NoSignFlag) 4537 return false; 4538 break; 4539 } 4540 4541 // If we're updating the condition code check if we have to reverse the 4542 // condition. 4543 if (ShouldUpdateCC) 4544 switch (OldCC) { 4545 default: 4546 return false; 4547 case X86::COND_E: 4548 ReplacementCC = NewCC; 4549 break; 4550 case X86::COND_NE: 4551 ReplacementCC = GetOppositeBranchCondition(NewCC); 4552 break; 4553 } 4554 } else if (IsSwapped) { 4555 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code needs 4556 // to be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. 4557 // We swap the condition code and synthesize the new opcode. 4558 ReplacementCC = getSwappedCondition(OldCC); 4559 if (ReplacementCC == X86::COND_INVALID) return false; 4560 ShouldUpdateCC = true; 4561 } else if (ImmDelta != 0) { 4562 unsigned BitWidth = TRI->getRegSizeInBits(*MRI->getRegClass(SrcReg)); 4563 // Shift amount for min/max constants to adjust for 8/16/32 instruction 4564 // sizes. 4565 switch (OldCC) { 4566 case X86::COND_L: // x <s (C + 1) --> x <=s C 4567 if (ImmDelta != 1 || APInt::getSignedMinValue(BitWidth) == CmpValue) 4568 return false; 4569 ReplacementCC = X86::COND_LE; 4570 break; 4571 case X86::COND_B: // x <u (C + 1) --> x <=u C 4572 if (ImmDelta != 1 || CmpValue == 0) 4573 return false; 4574 ReplacementCC = X86::COND_BE; 4575 break; 4576 case X86::COND_GE: // x >=s (C + 1) --> x >s C 4577 if (ImmDelta != 1 || APInt::getSignedMinValue(BitWidth) == CmpValue) 4578 return false; 4579 ReplacementCC = X86::COND_G; 4580 break; 4581 case X86::COND_AE: // x >=u (C + 1) --> x >u C 4582 if (ImmDelta != 1 || CmpValue == 0) 4583 return false; 4584 ReplacementCC = X86::COND_A; 4585 break; 4586 case X86::COND_G: // x >s (C - 1) --> x >=s C 4587 if (ImmDelta != -1 || APInt::getSignedMaxValue(BitWidth) == CmpValue) 4588 return false; 4589 ReplacementCC = X86::COND_GE; 4590 break; 4591 case X86::COND_A: // x >u (C - 1) --> x >=u C 4592 if (ImmDelta != -1 || APInt::getMaxValue(BitWidth) == CmpValue) 4593 return false; 4594 ReplacementCC = X86::COND_AE; 4595 break; 4596 case X86::COND_LE: // x <=s (C - 1) --> x <s C 4597 if (ImmDelta != -1 || APInt::getSignedMaxValue(BitWidth) == CmpValue) 4598 return false; 4599 ReplacementCC = X86::COND_L; 4600 break; 4601 case X86::COND_BE: // x <=u (C - 1) --> x <u C 4602 if (ImmDelta != -1 || APInt::getMaxValue(BitWidth) == CmpValue) 4603 return false; 4604 ReplacementCC = X86::COND_B; 4605 break; 4606 default: 4607 return false; 4608 } 4609 ShouldUpdateCC = true; 4610 } 4611 4612 if (ShouldUpdateCC && ReplacementCC != OldCC) { 4613 // Push the MachineInstr to OpsToUpdate. 4614 // If it is safe to remove CmpInstr, the condition code of these 4615 // instructions will be modified. 4616 OpsToUpdate.push_back(std::make_pair(&Instr, ReplacementCC)); 4617 } 4618 if (ModifyEFLAGS || Instr.killsRegister(X86::EFLAGS, TRI)) { 4619 // It is safe to remove CmpInstr if EFLAGS is updated again or killed. 4620 FlagsMayLiveOut = false; 4621 break; 4622 } 4623 } 4624 4625 // If we have to update users but EFLAGS is live-out abort, since we cannot 4626 // easily find all of the users. 4627 if (ShouldUpdateCC && FlagsMayLiveOut) { 4628 for (MachineBasicBlock *Successor : CmpMBB.successors()) 4629 if (Successor->isLiveIn(X86::EFLAGS)) 4630 return false; 4631 } 4632 4633 // The instruction to be updated is either Sub or MI. 4634 assert((MI == nullptr || Sub == nullptr) && "Should not have Sub and MI set"); 4635 Sub = MI != nullptr ? MI : Sub; 4636 MachineBasicBlock *SubBB = Sub->getParent(); 4637 // Move Movr0Inst to the appropriate place before Sub. 4638 if (Movr0Inst) { 4639 // Only move within the same block so we don't accidentally move to a 4640 // block with higher execution frequency. 4641 if (&CmpMBB != SubBB) 4642 return false; 4643 // Look backwards until we find a def that doesn't use the current EFLAGS. 4644 MachineBasicBlock::reverse_iterator InsertI = Sub, 4645 InsertE = Sub->getParent()->rend(); 4646 for (; InsertI != InsertE; ++InsertI) { 4647 MachineInstr *Instr = &*InsertI; 4648 if (!Instr->readsRegister(X86::EFLAGS, TRI) && 4649 Instr->modifiesRegister(X86::EFLAGS, TRI)) { 4650 Movr0Inst->getParent()->remove(Movr0Inst); 4651 Instr->getParent()->insert(MachineBasicBlock::iterator(Instr), 4652 Movr0Inst); 4653 break; 4654 } 4655 } 4656 if (InsertI == InsertE) 4657 return false; 4658 } 4659 4660 // Make sure Sub instruction defines EFLAGS and mark the def live. 4661 MachineOperand *FlagDef = Sub->findRegisterDefOperand(X86::EFLAGS); 4662 assert(FlagDef && "Unable to locate a def EFLAGS operand"); 4663 FlagDef->setIsDead(false); 4664 4665 CmpInstr.eraseFromParent(); 4666 4667 // Modify the condition code of instructions in OpsToUpdate. 4668 for (auto &Op : OpsToUpdate) { 4669 Op.first->getOperand(Op.first->getDesc().getNumOperands() - 1) 4670 .setImm(Op.second); 4671 } 4672 // Add EFLAGS to block live-ins between CmpBB and block of flags producer. 4673 for (MachineBasicBlock *MBB = &CmpMBB; MBB != SubBB; 4674 MBB = *MBB->pred_begin()) { 4675 assert(MBB->pred_size() == 1 && "Expected exactly one predecessor"); 4676 if (!MBB->isLiveIn(X86::EFLAGS)) 4677 MBB->addLiveIn(X86::EFLAGS); 4678 } 4679 return true; 4680 } 4681 4682 /// Try to remove the load by folding it to a register 4683 /// operand at the use. We fold the load instructions if load defines a virtual 4684 /// register, the virtual register is used once in the same BB, and the 4685 /// instructions in-between do not load or store, and have no side effects. 4686 MachineInstr *X86InstrInfo::optimizeLoadInstr(MachineInstr &MI, 4687 const MachineRegisterInfo *MRI, 4688 Register &FoldAsLoadDefReg, 4689 MachineInstr *&DefMI) const { 4690 // Check whether we can move DefMI here. 4691 DefMI = MRI->getVRegDef(FoldAsLoadDefReg); 4692 assert(DefMI); 4693 bool SawStore = false; 4694 if (!DefMI->isSafeToMove(nullptr, SawStore)) 4695 return nullptr; 4696 4697 // Collect information about virtual register operands of MI. 4698 SmallVector<unsigned, 1> SrcOperandIds; 4699 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 4700 MachineOperand &MO = MI.getOperand(i); 4701 if (!MO.isReg()) 4702 continue; 4703 Register Reg = MO.getReg(); 4704 if (Reg != FoldAsLoadDefReg) 4705 continue; 4706 // Do not fold if we have a subreg use or a def. 4707 if (MO.getSubReg() || MO.isDef()) 4708 return nullptr; 4709 SrcOperandIds.push_back(i); 4710 } 4711 if (SrcOperandIds.empty()) 4712 return nullptr; 4713 4714 // Check whether we can fold the def into SrcOperandId. 4715 if (MachineInstr *FoldMI = foldMemoryOperand(MI, SrcOperandIds, *DefMI)) { 4716 FoldAsLoadDefReg = 0; 4717 return FoldMI; 4718 } 4719 4720 return nullptr; 4721 } 4722 4723 /// Expand a single-def pseudo instruction to a two-addr 4724 /// instruction with two undef reads of the register being defined. 4725 /// This is used for mapping: 4726 /// %xmm4 = V_SET0 4727 /// to: 4728 /// %xmm4 = PXORrr undef %xmm4, undef %xmm4 4729 /// 4730 static bool Expand2AddrUndef(MachineInstrBuilder &MIB, 4731 const MCInstrDesc &Desc) { 4732 assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction."); 4733 Register Reg = MIB.getReg(0); 4734 MIB->setDesc(Desc); 4735 4736 // MachineInstr::addOperand() will insert explicit operands before any 4737 // implicit operands. 4738 MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef); 4739 // But we don't trust that. 4740 assert(MIB.getReg(1) == Reg && 4741 MIB.getReg(2) == Reg && "Misplaced operand"); 4742 return true; 4743 } 4744 4745 /// Expand a single-def pseudo instruction to a two-addr 4746 /// instruction with two %k0 reads. 4747 /// This is used for mapping: 4748 /// %k4 = K_SET1 4749 /// to: 4750 /// %k4 = KXNORrr %k0, %k0 4751 static bool Expand2AddrKreg(MachineInstrBuilder &MIB, const MCInstrDesc &Desc, 4752 Register Reg) { 4753 assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction."); 4754 MIB->setDesc(Desc); 4755 MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef); 4756 return true; 4757 } 4758 4759 static bool expandMOV32r1(MachineInstrBuilder &MIB, const TargetInstrInfo &TII, 4760 bool MinusOne) { 4761 MachineBasicBlock &MBB = *MIB->getParent(); 4762 const DebugLoc &DL = MIB->getDebugLoc(); 4763 Register Reg = MIB.getReg(0); 4764 4765 // Insert the XOR. 4766 BuildMI(MBB, MIB.getInstr(), DL, TII.get(X86::XOR32rr), Reg) 4767 .addReg(Reg, RegState::Undef) 4768 .addReg(Reg, RegState::Undef); 4769 4770 // Turn the pseudo into an INC or DEC. 4771 MIB->setDesc(TII.get(MinusOne ? X86::DEC32r : X86::INC32r)); 4772 MIB.addReg(Reg); 4773 4774 return true; 4775 } 4776 4777 static bool ExpandMOVImmSExti8(MachineInstrBuilder &MIB, 4778 const TargetInstrInfo &TII, 4779 const X86Subtarget &Subtarget) { 4780 MachineBasicBlock &MBB = *MIB->getParent(); 4781 const DebugLoc &DL = MIB->getDebugLoc(); 4782 int64_t Imm = MIB->getOperand(1).getImm(); 4783 assert(Imm != 0 && "Using push/pop for 0 is not efficient."); 4784 MachineBasicBlock::iterator I = MIB.getInstr(); 4785 4786 int StackAdjustment; 4787 4788 if (Subtarget.is64Bit()) { 4789 assert(MIB->getOpcode() == X86::MOV64ImmSExti8 || 4790 MIB->getOpcode() == X86::MOV32ImmSExti8); 4791 4792 // Can't use push/pop lowering if the function might write to the red zone. 4793 X86MachineFunctionInfo *X86FI = 4794 MBB.getParent()->getInfo<X86MachineFunctionInfo>(); 4795 if (X86FI->getUsesRedZone()) { 4796 MIB->setDesc(TII.get(MIB->getOpcode() == 4797 X86::MOV32ImmSExti8 ? X86::MOV32ri : X86::MOV64ri)); 4798 return true; 4799 } 4800 4801 // 64-bit mode doesn't have 32-bit push/pop, so use 64-bit operations and 4802 // widen the register if necessary. 4803 StackAdjustment = 8; 4804 BuildMI(MBB, I, DL, TII.get(X86::PUSH64i8)).addImm(Imm); 4805 MIB->setDesc(TII.get(X86::POP64r)); 4806 MIB->getOperand(0) 4807 .setReg(getX86SubSuperRegister(MIB.getReg(0), 64)); 4808 } else { 4809 assert(MIB->getOpcode() == X86::MOV32ImmSExti8); 4810 StackAdjustment = 4; 4811 BuildMI(MBB, I, DL, TII.get(X86::PUSH32i8)).addImm(Imm); 4812 MIB->setDesc(TII.get(X86::POP32r)); 4813 } 4814 MIB->RemoveOperand(1); 4815 MIB->addImplicitDefUseOperands(*MBB.getParent()); 4816 4817 // Build CFI if necessary. 4818 MachineFunction &MF = *MBB.getParent(); 4819 const X86FrameLowering *TFL = Subtarget.getFrameLowering(); 4820 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); 4821 bool NeedsDwarfCFI = !IsWin64Prologue && MF.needsFrameMoves(); 4822 bool EmitCFI = !TFL->hasFP(MF) && NeedsDwarfCFI; 4823 if (EmitCFI) { 4824 TFL->BuildCFI(MBB, I, DL, 4825 MCCFIInstruction::createAdjustCfaOffset(nullptr, StackAdjustment)); 4826 TFL->BuildCFI(MBB, std::next(I), DL, 4827 MCCFIInstruction::createAdjustCfaOffset(nullptr, -StackAdjustment)); 4828 } 4829 4830 return true; 4831 } 4832 4833 // LoadStackGuard has so far only been implemented for 64-bit MachO. Different 4834 // code sequence is needed for other targets. 4835 static void expandLoadStackGuard(MachineInstrBuilder &MIB, 4836 const TargetInstrInfo &TII) { 4837 MachineBasicBlock &MBB = *MIB->getParent(); 4838 const DebugLoc &DL = MIB->getDebugLoc(); 4839 Register Reg = MIB.getReg(0); 4840 const GlobalValue *GV = 4841 cast<GlobalValue>((*MIB->memoperands_begin())->getValue()); 4842 auto Flags = MachineMemOperand::MOLoad | 4843 MachineMemOperand::MODereferenceable | 4844 MachineMemOperand::MOInvariant; 4845 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand( 4846 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 8, Align(8)); 4847 MachineBasicBlock::iterator I = MIB.getInstr(); 4848 4849 BuildMI(MBB, I, DL, TII.get(X86::MOV64rm), Reg).addReg(X86::RIP).addImm(1) 4850 .addReg(0).addGlobalAddress(GV, 0, X86II::MO_GOTPCREL).addReg(0) 4851 .addMemOperand(MMO); 4852 MIB->setDebugLoc(DL); 4853 MIB->setDesc(TII.get(X86::MOV64rm)); 4854 MIB.addReg(Reg, RegState::Kill).addImm(1).addReg(0).addImm(0).addReg(0); 4855 } 4856 4857 static bool expandXorFP(MachineInstrBuilder &MIB, const TargetInstrInfo &TII) { 4858 MachineBasicBlock &MBB = *MIB->getParent(); 4859 MachineFunction &MF = *MBB.getParent(); 4860 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 4861 const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); 4862 unsigned XorOp = 4863 MIB->getOpcode() == X86::XOR64_FP ? X86::XOR64rr : X86::XOR32rr; 4864 MIB->setDesc(TII.get(XorOp)); 4865 MIB.addReg(TRI->getFrameRegister(MF), RegState::Undef); 4866 return true; 4867 } 4868 4869 // This is used to handle spills for 128/256-bit registers when we have AVX512, 4870 // but not VLX. If it uses an extended register we need to use an instruction 4871 // that loads the lower 128/256-bit, but is available with only AVX512F. 4872 static bool expandNOVLXLoad(MachineInstrBuilder &MIB, 4873 const TargetRegisterInfo *TRI, 4874 const MCInstrDesc &LoadDesc, 4875 const MCInstrDesc &BroadcastDesc, 4876 unsigned SubIdx) { 4877 Register DestReg = MIB.getReg(0); 4878 // Check if DestReg is XMM16-31 or YMM16-31. 4879 if (TRI->getEncodingValue(DestReg) < 16) { 4880 // We can use a normal VEX encoded load. 4881 MIB->setDesc(LoadDesc); 4882 } else { 4883 // Use a 128/256-bit VBROADCAST instruction. 4884 MIB->setDesc(BroadcastDesc); 4885 // Change the destination to a 512-bit register. 4886 DestReg = TRI->getMatchingSuperReg(DestReg, SubIdx, &X86::VR512RegClass); 4887 MIB->getOperand(0).setReg(DestReg); 4888 } 4889 return true; 4890 } 4891 4892 // This is used to handle spills for 128/256-bit registers when we have AVX512, 4893 // but not VLX. If it uses an extended register we need to use an instruction 4894 // that stores the lower 128/256-bit, but is available with only AVX512F. 4895 static bool expandNOVLXStore(MachineInstrBuilder &MIB, 4896 const TargetRegisterInfo *TRI, 4897 const MCInstrDesc &StoreDesc, 4898 const MCInstrDesc &ExtractDesc, 4899 unsigned SubIdx) { 4900 Register SrcReg = MIB.getReg(X86::AddrNumOperands); 4901 // Check if DestReg is XMM16-31 or YMM16-31. 4902 if (TRI->getEncodingValue(SrcReg) < 16) { 4903 // We can use a normal VEX encoded store. 4904 MIB->setDesc(StoreDesc); 4905 } else { 4906 // Use a VEXTRACTF instruction. 4907 MIB->setDesc(ExtractDesc); 4908 // Change the destination to a 512-bit register. 4909 SrcReg = TRI->getMatchingSuperReg(SrcReg, SubIdx, &X86::VR512RegClass); 4910 MIB->getOperand(X86::AddrNumOperands).setReg(SrcReg); 4911 MIB.addImm(0x0); // Append immediate to extract from the lower bits. 4912 } 4913 4914 return true; 4915 } 4916 4917 static bool expandSHXDROT(MachineInstrBuilder &MIB, const MCInstrDesc &Desc) { 4918 MIB->setDesc(Desc); 4919 int64_t ShiftAmt = MIB->getOperand(2).getImm(); 4920 // Temporarily remove the immediate so we can add another source register. 4921 MIB->RemoveOperand(2); 4922 // Add the register. Don't copy the kill flag if there is one. 4923 MIB.addReg(MIB.getReg(1), 4924 getUndefRegState(MIB->getOperand(1).isUndef())); 4925 // Add back the immediate. 4926 MIB.addImm(ShiftAmt); 4927 return true; 4928 } 4929 4930 bool X86InstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 4931 bool HasAVX = Subtarget.hasAVX(); 4932 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI); 4933 switch (MI.getOpcode()) { 4934 case X86::MOV32r0: 4935 return Expand2AddrUndef(MIB, get(X86::XOR32rr)); 4936 case X86::MOV32r1: 4937 return expandMOV32r1(MIB, *this, /*MinusOne=*/ false); 4938 case X86::MOV32r_1: 4939 return expandMOV32r1(MIB, *this, /*MinusOne=*/ true); 4940 case X86::MOV32ImmSExti8: 4941 case X86::MOV64ImmSExti8: 4942 return ExpandMOVImmSExti8(MIB, *this, Subtarget); 4943 case X86::SETB_C32r: 4944 return Expand2AddrUndef(MIB, get(X86::SBB32rr)); 4945 case X86::SETB_C64r: 4946 return Expand2AddrUndef(MIB, get(X86::SBB64rr)); 4947 case X86::MMX_SET0: 4948 return Expand2AddrUndef(MIB, get(X86::MMX_PXORirr)); 4949 case X86::V_SET0: 4950 case X86::FsFLD0SS: 4951 case X86::FsFLD0SD: 4952 case X86::FsFLD0F128: 4953 return Expand2AddrUndef(MIB, get(HasAVX ? X86::VXORPSrr : X86::XORPSrr)); 4954 case X86::AVX_SET0: { 4955 assert(HasAVX && "AVX not supported"); 4956 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4957 Register SrcReg = MIB.getReg(0); 4958 Register XReg = TRI->getSubReg(SrcReg, X86::sub_xmm); 4959 MIB->getOperand(0).setReg(XReg); 4960 Expand2AddrUndef(MIB, get(X86::VXORPSrr)); 4961 MIB.addReg(SrcReg, RegState::ImplicitDefine); 4962 return true; 4963 } 4964 case X86::AVX512_128_SET0: 4965 case X86::AVX512_FsFLD0SH: 4966 case X86::AVX512_FsFLD0SS: 4967 case X86::AVX512_FsFLD0SD: 4968 case X86::AVX512_FsFLD0F128: { 4969 bool HasVLX = Subtarget.hasVLX(); 4970 Register SrcReg = MIB.getReg(0); 4971 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4972 if (HasVLX || TRI->getEncodingValue(SrcReg) < 16) 4973 return Expand2AddrUndef(MIB, 4974 get(HasVLX ? X86::VPXORDZ128rr : X86::VXORPSrr)); 4975 // Extended register without VLX. Use a larger XOR. 4976 SrcReg = 4977 TRI->getMatchingSuperReg(SrcReg, X86::sub_xmm, &X86::VR512RegClass); 4978 MIB->getOperand(0).setReg(SrcReg); 4979 return Expand2AddrUndef(MIB, get(X86::VPXORDZrr)); 4980 } 4981 case X86::AVX512_256_SET0: 4982 case X86::AVX512_512_SET0: { 4983 bool HasVLX = Subtarget.hasVLX(); 4984 Register SrcReg = MIB.getReg(0); 4985 const TargetRegisterInfo *TRI = &getRegisterInfo(); 4986 if (HasVLX || TRI->getEncodingValue(SrcReg) < 16) { 4987 Register XReg = TRI->getSubReg(SrcReg, X86::sub_xmm); 4988 MIB->getOperand(0).setReg(XReg); 4989 Expand2AddrUndef(MIB, 4990 get(HasVLX ? X86::VPXORDZ128rr : X86::VXORPSrr)); 4991 MIB.addReg(SrcReg, RegState::ImplicitDefine); 4992 return true; 4993 } 4994 if (MI.getOpcode() == X86::AVX512_256_SET0) { 4995 // No VLX so we must reference a zmm. 4996 unsigned ZReg = 4997 TRI->getMatchingSuperReg(SrcReg, X86::sub_ymm, &X86::VR512RegClass); 4998 MIB->getOperand(0).setReg(ZReg); 4999 } 5000 return Expand2AddrUndef(MIB, get(X86::VPXORDZrr)); 5001 } 5002 case X86::V_SETALLONES: 5003 return Expand2AddrUndef(MIB, get(HasAVX ? X86::VPCMPEQDrr : X86::PCMPEQDrr)); 5004 case X86::AVX2_SETALLONES: 5005 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDYrr)); 5006 case X86::AVX1_SETALLONES: { 5007 Register Reg = MIB.getReg(0); 5008 // VCMPPSYrri with an immediate 0xf should produce VCMPTRUEPS. 5009 MIB->setDesc(get(X86::VCMPPSYrri)); 5010 MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef).addImm(0xf); 5011 return true; 5012 } 5013 case X86::AVX512_512_SETALLONES: { 5014 Register Reg = MIB.getReg(0); 5015 MIB->setDesc(get(X86::VPTERNLOGDZrri)); 5016 // VPTERNLOGD needs 3 register inputs and an immediate. 5017 // 0xff will return 1s for any input. 5018 MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef) 5019 .addReg(Reg, RegState::Undef).addImm(0xff); 5020 return true; 5021 } 5022 case X86::AVX512_512_SEXT_MASK_32: 5023 case X86::AVX512_512_SEXT_MASK_64: { 5024 Register Reg = MIB.getReg(0); 5025 Register MaskReg = MIB.getReg(1); 5026 unsigned MaskState = getRegState(MIB->getOperand(1)); 5027 unsigned Opc = (MI.getOpcode() == X86::AVX512_512_SEXT_MASK_64) ? 5028 X86::VPTERNLOGQZrrikz : X86::VPTERNLOGDZrrikz; 5029 MI.RemoveOperand(1); 5030 MIB->setDesc(get(Opc)); 5031 // VPTERNLOG needs 3 register inputs and an immediate. 5032 // 0xff will return 1s for any input. 5033 MIB.addReg(Reg, RegState::Undef).addReg(MaskReg, MaskState) 5034 .addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef).addImm(0xff); 5035 return true; 5036 } 5037 case X86::VMOVAPSZ128rm_NOVLX: 5038 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVAPSrm), 5039 get(X86::VBROADCASTF32X4rm), X86::sub_xmm); 5040 case X86::VMOVUPSZ128rm_NOVLX: 5041 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVUPSrm), 5042 get(X86::VBROADCASTF32X4rm), X86::sub_xmm); 5043 case X86::VMOVAPSZ256rm_NOVLX: 5044 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVAPSYrm), 5045 get(X86::VBROADCASTF64X4rm), X86::sub_ymm); 5046 case X86::VMOVUPSZ256rm_NOVLX: 5047 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVUPSYrm), 5048 get(X86::VBROADCASTF64X4rm), X86::sub_ymm); 5049 case X86::VMOVAPSZ128mr_NOVLX: 5050 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVAPSmr), 5051 get(X86::VEXTRACTF32x4Zmr), X86::sub_xmm); 5052 case X86::VMOVUPSZ128mr_NOVLX: 5053 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVUPSmr), 5054 get(X86::VEXTRACTF32x4Zmr), X86::sub_xmm); 5055 case X86::VMOVAPSZ256mr_NOVLX: 5056 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVAPSYmr), 5057 get(X86::VEXTRACTF64x4Zmr), X86::sub_ymm); 5058 case X86::VMOVUPSZ256mr_NOVLX: 5059 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVUPSYmr), 5060 get(X86::VEXTRACTF64x4Zmr), X86::sub_ymm); 5061 case X86::MOV32ri64: { 5062 Register Reg = MIB.getReg(0); 5063 Register Reg32 = RI.getSubReg(Reg, X86::sub_32bit); 5064 MI.setDesc(get(X86::MOV32ri)); 5065 MIB->getOperand(0).setReg(Reg32); 5066 MIB.addReg(Reg, RegState::ImplicitDefine); 5067 return true; 5068 } 5069 5070 // KNL does not recognize dependency-breaking idioms for mask registers, 5071 // so kxnor %k1, %k1, %k2 has a RAW dependence on %k1. 5072 // Using %k0 as the undef input register is a performance heuristic based 5073 // on the assumption that %k0 is used less frequently than the other mask 5074 // registers, since it is not usable as a write mask. 5075 // FIXME: A more advanced approach would be to choose the best input mask 5076 // register based on context. 5077 case X86::KSET0W: return Expand2AddrKreg(MIB, get(X86::KXORWrr), X86::K0); 5078 case X86::KSET0D: return Expand2AddrKreg(MIB, get(X86::KXORDrr), X86::K0); 5079 case X86::KSET0Q: return Expand2AddrKreg(MIB, get(X86::KXORQrr), X86::K0); 5080 case X86::KSET1W: return Expand2AddrKreg(MIB, get(X86::KXNORWrr), X86::K0); 5081 case X86::KSET1D: return Expand2AddrKreg(MIB, get(X86::KXNORDrr), X86::K0); 5082 case X86::KSET1Q: return Expand2AddrKreg(MIB, get(X86::KXNORQrr), X86::K0); 5083 case TargetOpcode::LOAD_STACK_GUARD: 5084 expandLoadStackGuard(MIB, *this); 5085 return true; 5086 case X86::XOR64_FP: 5087 case X86::XOR32_FP: 5088 return expandXorFP(MIB, *this); 5089 case X86::SHLDROT32ri: return expandSHXDROT(MIB, get(X86::SHLD32rri8)); 5090 case X86::SHLDROT64ri: return expandSHXDROT(MIB, get(X86::SHLD64rri8)); 5091 case X86::SHRDROT32ri: return expandSHXDROT(MIB, get(X86::SHRD32rri8)); 5092 case X86::SHRDROT64ri: return expandSHXDROT(MIB, get(X86::SHRD64rri8)); 5093 case X86::ADD8rr_DB: MIB->setDesc(get(X86::OR8rr)); break; 5094 case X86::ADD16rr_DB: MIB->setDesc(get(X86::OR16rr)); break; 5095 case X86::ADD32rr_DB: MIB->setDesc(get(X86::OR32rr)); break; 5096 case X86::ADD64rr_DB: MIB->setDesc(get(X86::OR64rr)); break; 5097 case X86::ADD8ri_DB: MIB->setDesc(get(X86::OR8ri)); break; 5098 case X86::ADD16ri_DB: MIB->setDesc(get(X86::OR16ri)); break; 5099 case X86::ADD32ri_DB: MIB->setDesc(get(X86::OR32ri)); break; 5100 case X86::ADD64ri32_DB: MIB->setDesc(get(X86::OR64ri32)); break; 5101 case X86::ADD16ri8_DB: MIB->setDesc(get(X86::OR16ri8)); break; 5102 case X86::ADD32ri8_DB: MIB->setDesc(get(X86::OR32ri8)); break; 5103 case X86::ADD64ri8_DB: MIB->setDesc(get(X86::OR64ri8)); break; 5104 } 5105 return false; 5106 } 5107 5108 /// Return true for all instructions that only update 5109 /// the first 32 or 64-bits of the destination register and leave the rest 5110 /// unmodified. This can be used to avoid folding loads if the instructions 5111 /// only update part of the destination register, and the non-updated part is 5112 /// not needed. e.g. cvtss2sd, sqrtss. Unfolding the load from these 5113 /// instructions breaks the partial register dependency and it can improve 5114 /// performance. e.g.: 5115 /// 5116 /// movss (%rdi), %xmm0 5117 /// cvtss2sd %xmm0, %xmm0 5118 /// 5119 /// Instead of 5120 /// cvtss2sd (%rdi), %xmm0 5121 /// 5122 /// FIXME: This should be turned into a TSFlags. 5123 /// 5124 static bool hasPartialRegUpdate(unsigned Opcode, 5125 const X86Subtarget &Subtarget, 5126 bool ForLoadFold = false) { 5127 switch (Opcode) { 5128 case X86::CVTSI2SSrr: 5129 case X86::CVTSI2SSrm: 5130 case X86::CVTSI642SSrr: 5131 case X86::CVTSI642SSrm: 5132 case X86::CVTSI2SDrr: 5133 case X86::CVTSI2SDrm: 5134 case X86::CVTSI642SDrr: 5135 case X86::CVTSI642SDrm: 5136 // Load folding won't effect the undef register update since the input is 5137 // a GPR. 5138 return !ForLoadFold; 5139 case X86::CVTSD2SSrr: 5140 case X86::CVTSD2SSrm: 5141 case X86::CVTSS2SDrr: 5142 case X86::CVTSS2SDrm: 5143 case X86::MOVHPDrm: 5144 case X86::MOVHPSrm: 5145 case X86::MOVLPDrm: 5146 case X86::MOVLPSrm: 5147 case X86::RCPSSr: 5148 case X86::RCPSSm: 5149 case X86::RCPSSr_Int: 5150 case X86::RCPSSm_Int: 5151 case X86::ROUNDSDr: 5152 case X86::ROUNDSDm: 5153 case X86::ROUNDSSr: 5154 case X86::ROUNDSSm: 5155 case X86::RSQRTSSr: 5156 case X86::RSQRTSSm: 5157 case X86::RSQRTSSr_Int: 5158 case X86::RSQRTSSm_Int: 5159 case X86::SQRTSSr: 5160 case X86::SQRTSSm: 5161 case X86::SQRTSSr_Int: 5162 case X86::SQRTSSm_Int: 5163 case X86::SQRTSDr: 5164 case X86::SQRTSDm: 5165 case X86::SQRTSDr_Int: 5166 case X86::SQRTSDm_Int: 5167 return true; 5168 // GPR 5169 case X86::POPCNT32rm: 5170 case X86::POPCNT32rr: 5171 case X86::POPCNT64rm: 5172 case X86::POPCNT64rr: 5173 return Subtarget.hasPOPCNTFalseDeps(); 5174 case X86::LZCNT32rm: 5175 case X86::LZCNT32rr: 5176 case X86::LZCNT64rm: 5177 case X86::LZCNT64rr: 5178 case X86::TZCNT32rm: 5179 case X86::TZCNT32rr: 5180 case X86::TZCNT64rm: 5181 case X86::TZCNT64rr: 5182 return Subtarget.hasLZCNTFalseDeps(); 5183 } 5184 5185 return false; 5186 } 5187 5188 /// Inform the BreakFalseDeps pass how many idle 5189 /// instructions we would like before a partial register update. 5190 unsigned X86InstrInfo::getPartialRegUpdateClearance( 5191 const MachineInstr &MI, unsigned OpNum, 5192 const TargetRegisterInfo *TRI) const { 5193 if (OpNum != 0 || !hasPartialRegUpdate(MI.getOpcode(), Subtarget)) 5194 return 0; 5195 5196 // If MI is marked as reading Reg, the partial register update is wanted. 5197 const MachineOperand &MO = MI.getOperand(0); 5198 Register Reg = MO.getReg(); 5199 if (Reg.isVirtual()) { 5200 if (MO.readsReg() || MI.readsVirtualRegister(Reg)) 5201 return 0; 5202 } else { 5203 if (MI.readsRegister(Reg, TRI)) 5204 return 0; 5205 } 5206 5207 // If any instructions in the clearance range are reading Reg, insert a 5208 // dependency breaking instruction, which is inexpensive and is likely to 5209 // be hidden in other instruction's cycles. 5210 return PartialRegUpdateClearance; 5211 } 5212 5213 // Return true for any instruction the copies the high bits of the first source 5214 // operand into the unused high bits of the destination operand. 5215 // Also returns true for instructions that have two inputs where one may 5216 // be undef and we want it to use the same register as the other input. 5217 static bool hasUndefRegUpdate(unsigned Opcode, unsigned OpNum, 5218 bool ForLoadFold = false) { 5219 // Set the OpNum parameter to the first source operand. 5220 switch (Opcode) { 5221 case X86::MMX_PUNPCKHBWirr: 5222 case X86::MMX_PUNPCKHWDirr: 5223 case X86::MMX_PUNPCKHDQirr: 5224 case X86::MMX_PUNPCKLBWirr: 5225 case X86::MMX_PUNPCKLWDirr: 5226 case X86::MMX_PUNPCKLDQirr: 5227 case X86::MOVHLPSrr: 5228 case X86::PACKSSWBrr: 5229 case X86::PACKUSWBrr: 5230 case X86::PACKSSDWrr: 5231 case X86::PACKUSDWrr: 5232 case X86::PUNPCKHBWrr: 5233 case X86::PUNPCKLBWrr: 5234 case X86::PUNPCKHWDrr: 5235 case X86::PUNPCKLWDrr: 5236 case X86::PUNPCKHDQrr: 5237 case X86::PUNPCKLDQrr: 5238 case X86::PUNPCKHQDQrr: 5239 case X86::PUNPCKLQDQrr: 5240 case X86::SHUFPDrri: 5241 case X86::SHUFPSrri: 5242 // These instructions are sometimes used with an undef first or second 5243 // source. Return true here so BreakFalseDeps will assign this source to the 5244 // same register as the first source to avoid a false dependency. 5245 // Operand 1 of these instructions is tied so they're separate from their 5246 // VEX counterparts. 5247 return OpNum == 2 && !ForLoadFold; 5248 5249 case X86::VMOVLHPSrr: 5250 case X86::VMOVLHPSZrr: 5251 case X86::VPACKSSWBrr: 5252 case X86::VPACKUSWBrr: 5253 case X86::VPACKSSDWrr: 5254 case X86::VPACKUSDWrr: 5255 case X86::VPACKSSWBZ128rr: 5256 case X86::VPACKUSWBZ128rr: 5257 case X86::VPACKSSDWZ128rr: 5258 case X86::VPACKUSDWZ128rr: 5259 case X86::VPERM2F128rr: 5260 case X86::VPERM2I128rr: 5261 case X86::VSHUFF32X4Z256rri: 5262 case X86::VSHUFF32X4Zrri: 5263 case X86::VSHUFF64X2Z256rri: 5264 case X86::VSHUFF64X2Zrri: 5265 case X86::VSHUFI32X4Z256rri: 5266 case X86::VSHUFI32X4Zrri: 5267 case X86::VSHUFI64X2Z256rri: 5268 case X86::VSHUFI64X2Zrri: 5269 case X86::VPUNPCKHBWrr: 5270 case X86::VPUNPCKLBWrr: 5271 case X86::VPUNPCKHBWYrr: 5272 case X86::VPUNPCKLBWYrr: 5273 case X86::VPUNPCKHBWZ128rr: 5274 case X86::VPUNPCKLBWZ128rr: 5275 case X86::VPUNPCKHBWZ256rr: 5276 case X86::VPUNPCKLBWZ256rr: 5277 case X86::VPUNPCKHBWZrr: 5278 case X86::VPUNPCKLBWZrr: 5279 case X86::VPUNPCKHWDrr: 5280 case X86::VPUNPCKLWDrr: 5281 case X86::VPUNPCKHWDYrr: 5282 case X86::VPUNPCKLWDYrr: 5283 case X86::VPUNPCKHWDZ128rr: 5284 case X86::VPUNPCKLWDZ128rr: 5285 case X86::VPUNPCKHWDZ256rr: 5286 case X86::VPUNPCKLWDZ256rr: 5287 case X86::VPUNPCKHWDZrr: 5288 case X86::VPUNPCKLWDZrr: 5289 case X86::VPUNPCKHDQrr: 5290 case X86::VPUNPCKLDQrr: 5291 case X86::VPUNPCKHDQYrr: 5292 case X86::VPUNPCKLDQYrr: 5293 case X86::VPUNPCKHDQZ128rr: 5294 case X86::VPUNPCKLDQZ128rr: 5295 case X86::VPUNPCKHDQZ256rr: 5296 case X86::VPUNPCKLDQZ256rr: 5297 case X86::VPUNPCKHDQZrr: 5298 case X86::VPUNPCKLDQZrr: 5299 case X86::VPUNPCKHQDQrr: 5300 case X86::VPUNPCKLQDQrr: 5301 case X86::VPUNPCKHQDQYrr: 5302 case X86::VPUNPCKLQDQYrr: 5303 case X86::VPUNPCKHQDQZ128rr: 5304 case X86::VPUNPCKLQDQZ128rr: 5305 case X86::VPUNPCKHQDQZ256rr: 5306 case X86::VPUNPCKLQDQZ256rr: 5307 case X86::VPUNPCKHQDQZrr: 5308 case X86::VPUNPCKLQDQZrr: 5309 // These instructions are sometimes used with an undef first or second 5310 // source. Return true here so BreakFalseDeps will assign this source to the 5311 // same register as the first source to avoid a false dependency. 5312 return (OpNum == 1 || OpNum == 2) && !ForLoadFold; 5313 5314 case X86::VCVTSI2SSrr: 5315 case X86::VCVTSI2SSrm: 5316 case X86::VCVTSI2SSrr_Int: 5317 case X86::VCVTSI2SSrm_Int: 5318 case X86::VCVTSI642SSrr: 5319 case X86::VCVTSI642SSrm: 5320 case X86::VCVTSI642SSrr_Int: 5321 case X86::VCVTSI642SSrm_Int: 5322 case X86::VCVTSI2SDrr: 5323 case X86::VCVTSI2SDrm: 5324 case X86::VCVTSI2SDrr_Int: 5325 case X86::VCVTSI2SDrm_Int: 5326 case X86::VCVTSI642SDrr: 5327 case X86::VCVTSI642SDrm: 5328 case X86::VCVTSI642SDrr_Int: 5329 case X86::VCVTSI642SDrm_Int: 5330 // AVX-512 5331 case X86::VCVTSI2SSZrr: 5332 case X86::VCVTSI2SSZrm: 5333 case X86::VCVTSI2SSZrr_Int: 5334 case X86::VCVTSI2SSZrrb_Int: 5335 case X86::VCVTSI2SSZrm_Int: 5336 case X86::VCVTSI642SSZrr: 5337 case X86::VCVTSI642SSZrm: 5338 case X86::VCVTSI642SSZrr_Int: 5339 case X86::VCVTSI642SSZrrb_Int: 5340 case X86::VCVTSI642SSZrm_Int: 5341 case X86::VCVTSI2SDZrr: 5342 case X86::VCVTSI2SDZrm: 5343 case X86::VCVTSI2SDZrr_Int: 5344 case X86::VCVTSI2SDZrm_Int: 5345 case X86::VCVTSI642SDZrr: 5346 case X86::VCVTSI642SDZrm: 5347 case X86::VCVTSI642SDZrr_Int: 5348 case X86::VCVTSI642SDZrrb_Int: 5349 case X86::VCVTSI642SDZrm_Int: 5350 case X86::VCVTUSI2SSZrr: 5351 case X86::VCVTUSI2SSZrm: 5352 case X86::VCVTUSI2SSZrr_Int: 5353 case X86::VCVTUSI2SSZrrb_Int: 5354 case X86::VCVTUSI2SSZrm_Int: 5355 case X86::VCVTUSI642SSZrr: 5356 case X86::VCVTUSI642SSZrm: 5357 case X86::VCVTUSI642SSZrr_Int: 5358 case X86::VCVTUSI642SSZrrb_Int: 5359 case X86::VCVTUSI642SSZrm_Int: 5360 case X86::VCVTUSI2SDZrr: 5361 case X86::VCVTUSI2SDZrm: 5362 case X86::VCVTUSI2SDZrr_Int: 5363 case X86::VCVTUSI2SDZrm_Int: 5364 case X86::VCVTUSI642SDZrr: 5365 case X86::VCVTUSI642SDZrm: 5366 case X86::VCVTUSI642SDZrr_Int: 5367 case X86::VCVTUSI642SDZrrb_Int: 5368 case X86::VCVTUSI642SDZrm_Int: 5369 case X86::VCVTSI2SHZrr: 5370 case X86::VCVTSI2SHZrm: 5371 case X86::VCVTSI2SHZrr_Int: 5372 case X86::VCVTSI2SHZrrb_Int: 5373 case X86::VCVTSI2SHZrm_Int: 5374 case X86::VCVTSI642SHZrr: 5375 case X86::VCVTSI642SHZrm: 5376 case X86::VCVTSI642SHZrr_Int: 5377 case X86::VCVTSI642SHZrrb_Int: 5378 case X86::VCVTSI642SHZrm_Int: 5379 case X86::VCVTUSI2SHZrr: 5380 case X86::VCVTUSI2SHZrm: 5381 case X86::VCVTUSI2SHZrr_Int: 5382 case X86::VCVTUSI2SHZrrb_Int: 5383 case X86::VCVTUSI2SHZrm_Int: 5384 case X86::VCVTUSI642SHZrr: 5385 case X86::VCVTUSI642SHZrm: 5386 case X86::VCVTUSI642SHZrr_Int: 5387 case X86::VCVTUSI642SHZrrb_Int: 5388 case X86::VCVTUSI642SHZrm_Int: 5389 // Load folding won't effect the undef register update since the input is 5390 // a GPR. 5391 return OpNum == 1 && !ForLoadFold; 5392 case X86::VCVTSD2SSrr: 5393 case X86::VCVTSD2SSrm: 5394 case X86::VCVTSD2SSrr_Int: 5395 case X86::VCVTSD2SSrm_Int: 5396 case X86::VCVTSS2SDrr: 5397 case X86::VCVTSS2SDrm: 5398 case X86::VCVTSS2SDrr_Int: 5399 case X86::VCVTSS2SDrm_Int: 5400 case X86::VRCPSSr: 5401 case X86::VRCPSSr_Int: 5402 case X86::VRCPSSm: 5403 case X86::VRCPSSm_Int: 5404 case X86::VROUNDSDr: 5405 case X86::VROUNDSDm: 5406 case X86::VROUNDSDr_Int: 5407 case X86::VROUNDSDm_Int: 5408 case X86::VROUNDSSr: 5409 case X86::VROUNDSSm: 5410 case X86::VROUNDSSr_Int: 5411 case X86::VROUNDSSm_Int: 5412 case X86::VRSQRTSSr: 5413 case X86::VRSQRTSSr_Int: 5414 case X86::VRSQRTSSm: 5415 case X86::VRSQRTSSm_Int: 5416 case X86::VSQRTSSr: 5417 case X86::VSQRTSSr_Int: 5418 case X86::VSQRTSSm: 5419 case X86::VSQRTSSm_Int: 5420 case X86::VSQRTSDr: 5421 case X86::VSQRTSDr_Int: 5422 case X86::VSQRTSDm: 5423 case X86::VSQRTSDm_Int: 5424 // AVX-512 5425 case X86::VCVTSD2SSZrr: 5426 case X86::VCVTSD2SSZrr_Int: 5427 case X86::VCVTSD2SSZrrb_Int: 5428 case X86::VCVTSD2SSZrm: 5429 case X86::VCVTSD2SSZrm_Int: 5430 case X86::VCVTSS2SDZrr: 5431 case X86::VCVTSS2SDZrr_Int: 5432 case X86::VCVTSS2SDZrrb_Int: 5433 case X86::VCVTSS2SDZrm: 5434 case X86::VCVTSS2SDZrm_Int: 5435 case X86::VGETEXPSDZr: 5436 case X86::VGETEXPSDZrb: 5437 case X86::VGETEXPSDZm: 5438 case X86::VGETEXPSSZr: 5439 case X86::VGETEXPSSZrb: 5440 case X86::VGETEXPSSZm: 5441 case X86::VGETMANTSDZrri: 5442 case X86::VGETMANTSDZrrib: 5443 case X86::VGETMANTSDZrmi: 5444 case X86::VGETMANTSSZrri: 5445 case X86::VGETMANTSSZrrib: 5446 case X86::VGETMANTSSZrmi: 5447 case X86::VRNDSCALESDZr: 5448 case X86::VRNDSCALESDZr_Int: 5449 case X86::VRNDSCALESDZrb_Int: 5450 case X86::VRNDSCALESDZm: 5451 case X86::VRNDSCALESDZm_Int: 5452 case X86::VRNDSCALESSZr: 5453 case X86::VRNDSCALESSZr_Int: 5454 case X86::VRNDSCALESSZrb_Int: 5455 case X86::VRNDSCALESSZm: 5456 case X86::VRNDSCALESSZm_Int: 5457 case X86::VRCP14SDZrr: 5458 case X86::VRCP14SDZrm: 5459 case X86::VRCP14SSZrr: 5460 case X86::VRCP14SSZrm: 5461 case X86::VRCPSHZrr: 5462 case X86::VRCPSHZrm: 5463 case X86::VRSQRTSHZrr: 5464 case X86::VRSQRTSHZrm: 5465 case X86::VREDUCESHZrmi: 5466 case X86::VREDUCESHZrri: 5467 case X86::VREDUCESHZrrib: 5468 case X86::VGETEXPSHZr: 5469 case X86::VGETEXPSHZrb: 5470 case X86::VGETEXPSHZm: 5471 case X86::VGETMANTSHZrri: 5472 case X86::VGETMANTSHZrrib: 5473 case X86::VGETMANTSHZrmi: 5474 case X86::VRNDSCALESHZr: 5475 case X86::VRNDSCALESHZr_Int: 5476 case X86::VRNDSCALESHZrb_Int: 5477 case X86::VRNDSCALESHZm: 5478 case X86::VRNDSCALESHZm_Int: 5479 case X86::VSQRTSHZr: 5480 case X86::VSQRTSHZr_Int: 5481 case X86::VSQRTSHZrb_Int: 5482 case X86::VSQRTSHZm: 5483 case X86::VSQRTSHZm_Int: 5484 case X86::VRCP28SDZr: 5485 case X86::VRCP28SDZrb: 5486 case X86::VRCP28SDZm: 5487 case X86::VRCP28SSZr: 5488 case X86::VRCP28SSZrb: 5489 case X86::VRCP28SSZm: 5490 case X86::VREDUCESSZrmi: 5491 case X86::VREDUCESSZrri: 5492 case X86::VREDUCESSZrrib: 5493 case X86::VRSQRT14SDZrr: 5494 case X86::VRSQRT14SDZrm: 5495 case X86::VRSQRT14SSZrr: 5496 case X86::VRSQRT14SSZrm: 5497 case X86::VRSQRT28SDZr: 5498 case X86::VRSQRT28SDZrb: 5499 case X86::VRSQRT28SDZm: 5500 case X86::VRSQRT28SSZr: 5501 case X86::VRSQRT28SSZrb: 5502 case X86::VRSQRT28SSZm: 5503 case X86::VSQRTSSZr: 5504 case X86::VSQRTSSZr_Int: 5505 case X86::VSQRTSSZrb_Int: 5506 case X86::VSQRTSSZm: 5507 case X86::VSQRTSSZm_Int: 5508 case X86::VSQRTSDZr: 5509 case X86::VSQRTSDZr_Int: 5510 case X86::VSQRTSDZrb_Int: 5511 case X86::VSQRTSDZm: 5512 case X86::VSQRTSDZm_Int: 5513 case X86::VCVTSD2SHZrr: 5514 case X86::VCVTSD2SHZrr_Int: 5515 case X86::VCVTSD2SHZrrb_Int: 5516 case X86::VCVTSD2SHZrm: 5517 case X86::VCVTSD2SHZrm_Int: 5518 case X86::VCVTSS2SHZrr: 5519 case X86::VCVTSS2SHZrr_Int: 5520 case X86::VCVTSS2SHZrrb_Int: 5521 case X86::VCVTSS2SHZrm: 5522 case X86::VCVTSS2SHZrm_Int: 5523 case X86::VCVTSH2SDZrr: 5524 case X86::VCVTSH2SDZrr_Int: 5525 case X86::VCVTSH2SDZrrb_Int: 5526 case X86::VCVTSH2SDZrm: 5527 case X86::VCVTSH2SDZrm_Int: 5528 case X86::VCVTSH2SSZrr: 5529 case X86::VCVTSH2SSZrr_Int: 5530 case X86::VCVTSH2SSZrrb_Int: 5531 case X86::VCVTSH2SSZrm: 5532 case X86::VCVTSH2SSZrm_Int: 5533 return OpNum == 1; 5534 case X86::VMOVSSZrrk: 5535 case X86::VMOVSDZrrk: 5536 return OpNum == 3 && !ForLoadFold; 5537 case X86::VMOVSSZrrkz: 5538 case X86::VMOVSDZrrkz: 5539 return OpNum == 2 && !ForLoadFold; 5540 } 5541 5542 return false; 5543 } 5544 5545 /// Inform the BreakFalseDeps pass how many idle instructions we would like 5546 /// before certain undef register reads. 5547 /// 5548 /// This catches the VCVTSI2SD family of instructions: 5549 /// 5550 /// vcvtsi2sdq %rax, undef %xmm0, %xmm14 5551 /// 5552 /// We should to be careful *not* to catch VXOR idioms which are presumably 5553 /// handled specially in the pipeline: 5554 /// 5555 /// vxorps undef %xmm1, undef %xmm1, %xmm1 5556 /// 5557 /// Like getPartialRegUpdateClearance, this makes a strong assumption that the 5558 /// high bits that are passed-through are not live. 5559 unsigned 5560 X86InstrInfo::getUndefRegClearance(const MachineInstr &MI, unsigned OpNum, 5561 const TargetRegisterInfo *TRI) const { 5562 const MachineOperand &MO = MI.getOperand(OpNum); 5563 if (Register::isPhysicalRegister(MO.getReg()) && 5564 hasUndefRegUpdate(MI.getOpcode(), OpNum)) 5565 return UndefRegClearance; 5566 5567 return 0; 5568 } 5569 5570 void X86InstrInfo::breakPartialRegDependency( 5571 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const { 5572 Register Reg = MI.getOperand(OpNum).getReg(); 5573 // If MI kills this register, the false dependence is already broken. 5574 if (MI.killsRegister(Reg, TRI)) 5575 return; 5576 5577 if (X86::VR128RegClass.contains(Reg)) { 5578 // These instructions are all floating point domain, so xorps is the best 5579 // choice. 5580 unsigned Opc = Subtarget.hasAVX() ? X86::VXORPSrr : X86::XORPSrr; 5581 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(Opc), Reg) 5582 .addReg(Reg, RegState::Undef) 5583 .addReg(Reg, RegState::Undef); 5584 MI.addRegisterKilled(Reg, TRI, true); 5585 } else if (X86::VR256RegClass.contains(Reg)) { 5586 // Use vxorps to clear the full ymm register. 5587 // It wants to read and write the xmm sub-register. 5588 Register XReg = TRI->getSubReg(Reg, X86::sub_xmm); 5589 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VXORPSrr), XReg) 5590 .addReg(XReg, RegState::Undef) 5591 .addReg(XReg, RegState::Undef) 5592 .addReg(Reg, RegState::ImplicitDefine); 5593 MI.addRegisterKilled(Reg, TRI, true); 5594 } else if (X86::GR64RegClass.contains(Reg)) { 5595 // Using XOR32rr because it has shorter encoding and zeros up the upper bits 5596 // as well. 5597 Register XReg = TRI->getSubReg(Reg, X86::sub_32bit); 5598 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::XOR32rr), XReg) 5599 .addReg(XReg, RegState::Undef) 5600 .addReg(XReg, RegState::Undef) 5601 .addReg(Reg, RegState::ImplicitDefine); 5602 MI.addRegisterKilled(Reg, TRI, true); 5603 } else if (X86::GR32RegClass.contains(Reg)) { 5604 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::XOR32rr), Reg) 5605 .addReg(Reg, RegState::Undef) 5606 .addReg(Reg, RegState::Undef); 5607 MI.addRegisterKilled(Reg, TRI, true); 5608 } 5609 } 5610 5611 static void addOperands(MachineInstrBuilder &MIB, ArrayRef<MachineOperand> MOs, 5612 int PtrOffset = 0) { 5613 unsigned NumAddrOps = MOs.size(); 5614 5615 if (NumAddrOps < 4) { 5616 // FrameIndex only - add an immediate offset (whether its zero or not). 5617 for (unsigned i = 0; i != NumAddrOps; ++i) 5618 MIB.add(MOs[i]); 5619 addOffset(MIB, PtrOffset); 5620 } else { 5621 // General Memory Addressing - we need to add any offset to an existing 5622 // offset. 5623 assert(MOs.size() == 5 && "Unexpected memory operand list length"); 5624 for (unsigned i = 0; i != NumAddrOps; ++i) { 5625 const MachineOperand &MO = MOs[i]; 5626 if (i == 3 && PtrOffset != 0) { 5627 MIB.addDisp(MO, PtrOffset); 5628 } else { 5629 MIB.add(MO); 5630 } 5631 } 5632 } 5633 } 5634 5635 static void updateOperandRegConstraints(MachineFunction &MF, 5636 MachineInstr &NewMI, 5637 const TargetInstrInfo &TII) { 5638 MachineRegisterInfo &MRI = MF.getRegInfo(); 5639 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 5640 5641 for (int Idx : llvm::seq<int>(0, NewMI.getNumOperands())) { 5642 MachineOperand &MO = NewMI.getOperand(Idx); 5643 // We only need to update constraints on virtual register operands. 5644 if (!MO.isReg()) 5645 continue; 5646 Register Reg = MO.getReg(); 5647 if (!Reg.isVirtual()) 5648 continue; 5649 5650 auto *NewRC = MRI.constrainRegClass( 5651 Reg, TII.getRegClass(NewMI.getDesc(), Idx, &TRI, MF)); 5652 if (!NewRC) { 5653 LLVM_DEBUG( 5654 dbgs() << "WARNING: Unable to update register constraint for operand " 5655 << Idx << " of instruction:\n"; 5656 NewMI.dump(); dbgs() << "\n"); 5657 } 5658 } 5659 } 5660 5661 static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode, 5662 ArrayRef<MachineOperand> MOs, 5663 MachineBasicBlock::iterator InsertPt, 5664 MachineInstr &MI, 5665 const TargetInstrInfo &TII) { 5666 // Create the base instruction with the memory operand as the first part. 5667 // Omit the implicit operands, something BuildMI can't do. 5668 MachineInstr *NewMI = 5669 MF.CreateMachineInstr(TII.get(Opcode), MI.getDebugLoc(), true); 5670 MachineInstrBuilder MIB(MF, NewMI); 5671 addOperands(MIB, MOs); 5672 5673 // Loop over the rest of the ri operands, converting them over. 5674 unsigned NumOps = MI.getDesc().getNumOperands() - 2; 5675 for (unsigned i = 0; i != NumOps; ++i) { 5676 MachineOperand &MO = MI.getOperand(i + 2); 5677 MIB.add(MO); 5678 } 5679 for (unsigned i = NumOps + 2, e = MI.getNumOperands(); i != e; ++i) { 5680 MachineOperand &MO = MI.getOperand(i); 5681 MIB.add(MO); 5682 } 5683 5684 updateOperandRegConstraints(MF, *NewMI, TII); 5685 5686 MachineBasicBlock *MBB = InsertPt->getParent(); 5687 MBB->insert(InsertPt, NewMI); 5688 5689 return MIB; 5690 } 5691 5692 static MachineInstr *FuseInst(MachineFunction &MF, unsigned Opcode, 5693 unsigned OpNo, ArrayRef<MachineOperand> MOs, 5694 MachineBasicBlock::iterator InsertPt, 5695 MachineInstr &MI, const TargetInstrInfo &TII, 5696 int PtrOffset = 0) { 5697 // Omit the implicit operands, something BuildMI can't do. 5698 MachineInstr *NewMI = 5699 MF.CreateMachineInstr(TII.get(Opcode), MI.getDebugLoc(), true); 5700 MachineInstrBuilder MIB(MF, NewMI); 5701 5702 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 5703 MachineOperand &MO = MI.getOperand(i); 5704 if (i == OpNo) { 5705 assert(MO.isReg() && "Expected to fold into reg operand!"); 5706 addOperands(MIB, MOs, PtrOffset); 5707 } else { 5708 MIB.add(MO); 5709 } 5710 } 5711 5712 updateOperandRegConstraints(MF, *NewMI, TII); 5713 5714 // Copy the NoFPExcept flag from the instruction we're fusing. 5715 if (MI.getFlag(MachineInstr::MIFlag::NoFPExcept)) 5716 NewMI->setFlag(MachineInstr::MIFlag::NoFPExcept); 5717 5718 MachineBasicBlock *MBB = InsertPt->getParent(); 5719 MBB->insert(InsertPt, NewMI); 5720 5721 return MIB; 5722 } 5723 5724 static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode, 5725 ArrayRef<MachineOperand> MOs, 5726 MachineBasicBlock::iterator InsertPt, 5727 MachineInstr &MI) { 5728 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt, 5729 MI.getDebugLoc(), TII.get(Opcode)); 5730 addOperands(MIB, MOs); 5731 return MIB.addImm(0); 5732 } 5733 5734 MachineInstr *X86InstrInfo::foldMemoryOperandCustom( 5735 MachineFunction &MF, MachineInstr &MI, unsigned OpNum, 5736 ArrayRef<MachineOperand> MOs, MachineBasicBlock::iterator InsertPt, 5737 unsigned Size, Align Alignment) const { 5738 switch (MI.getOpcode()) { 5739 case X86::INSERTPSrr: 5740 case X86::VINSERTPSrr: 5741 case X86::VINSERTPSZrr: 5742 // Attempt to convert the load of inserted vector into a fold load 5743 // of a single float. 5744 if (OpNum == 2) { 5745 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm(); 5746 unsigned ZMask = Imm & 15; 5747 unsigned DstIdx = (Imm >> 4) & 3; 5748 unsigned SrcIdx = (Imm >> 6) & 3; 5749 5750 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 5751 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum, &RI, MF); 5752 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8; 5753 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment >= Align(4)) { 5754 int PtrOffset = SrcIdx * 4; 5755 unsigned NewImm = (DstIdx << 4) | ZMask; 5756 unsigned NewOpCode = 5757 (MI.getOpcode() == X86::VINSERTPSZrr) ? X86::VINSERTPSZrm : 5758 (MI.getOpcode() == X86::VINSERTPSrr) ? X86::VINSERTPSrm : 5759 X86::INSERTPSrm; 5760 MachineInstr *NewMI = 5761 FuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, PtrOffset); 5762 NewMI->getOperand(NewMI->getNumOperands() - 1).setImm(NewImm); 5763 return NewMI; 5764 } 5765 } 5766 break; 5767 case X86::MOVHLPSrr: 5768 case X86::VMOVHLPSrr: 5769 case X86::VMOVHLPSZrr: 5770 // Move the upper 64-bits of the second operand to the lower 64-bits. 5771 // To fold the load, adjust the pointer to the upper and use (V)MOVLPS. 5772 // TODO: In most cases AVX doesn't have a 8-byte alignment requirement. 5773 if (OpNum == 2) { 5774 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 5775 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum, &RI, MF); 5776 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8; 5777 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment >= Align(8)) { 5778 unsigned NewOpCode = 5779 (MI.getOpcode() == X86::VMOVHLPSZrr) ? X86::VMOVLPSZ128rm : 5780 (MI.getOpcode() == X86::VMOVHLPSrr) ? X86::VMOVLPSrm : 5781 X86::MOVLPSrm; 5782 MachineInstr *NewMI = 5783 FuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, 8); 5784 return NewMI; 5785 } 5786 } 5787 break; 5788 case X86::UNPCKLPDrr: 5789 // If we won't be able to fold this to the memory form of UNPCKL, use 5790 // MOVHPD instead. Done as custom because we can't have this in the load 5791 // table twice. 5792 if (OpNum == 2) { 5793 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 5794 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum, &RI, MF); 5795 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8; 5796 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment < Align(16)) { 5797 MachineInstr *NewMI = 5798 FuseInst(MF, X86::MOVHPDrm, OpNum, MOs, InsertPt, MI, *this); 5799 return NewMI; 5800 } 5801 } 5802 break; 5803 } 5804 5805 return nullptr; 5806 } 5807 5808 static bool shouldPreventUndefRegUpdateMemFold(MachineFunction &MF, 5809 MachineInstr &MI) { 5810 if (!hasUndefRegUpdate(MI.getOpcode(), 1, /*ForLoadFold*/true) || 5811 !MI.getOperand(1).isReg()) 5812 return false; 5813 5814 // The are two cases we need to handle depending on where in the pipeline 5815 // the folding attempt is being made. 5816 // -Register has the undef flag set. 5817 // -Register is produced by the IMPLICIT_DEF instruction. 5818 5819 if (MI.getOperand(1).isUndef()) 5820 return true; 5821 5822 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 5823 MachineInstr *VRegDef = RegInfo.getUniqueVRegDef(MI.getOperand(1).getReg()); 5824 return VRegDef && VRegDef->isImplicitDef(); 5825 } 5826 5827 MachineInstr *X86InstrInfo::foldMemoryOperandImpl( 5828 MachineFunction &MF, MachineInstr &MI, unsigned OpNum, 5829 ArrayRef<MachineOperand> MOs, MachineBasicBlock::iterator InsertPt, 5830 unsigned Size, Align Alignment, bool AllowCommute) const { 5831 bool isSlowTwoMemOps = Subtarget.slowTwoMemOps(); 5832 bool isTwoAddrFold = false; 5833 5834 // For CPUs that favor the register form of a call or push, 5835 // do not fold loads into calls or pushes, unless optimizing for size 5836 // aggressively. 5837 if (isSlowTwoMemOps && !MF.getFunction().hasMinSize() && 5838 (MI.getOpcode() == X86::CALL32r || MI.getOpcode() == X86::CALL64r || 5839 MI.getOpcode() == X86::PUSH16r || MI.getOpcode() == X86::PUSH32r || 5840 MI.getOpcode() == X86::PUSH64r)) 5841 return nullptr; 5842 5843 // Avoid partial and undef register update stalls unless optimizing for size. 5844 if (!MF.getFunction().hasOptSize() && 5845 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/true) || 5846 shouldPreventUndefRegUpdateMemFold(MF, MI))) 5847 return nullptr; 5848 5849 unsigned NumOps = MI.getDesc().getNumOperands(); 5850 bool isTwoAddr = 5851 NumOps > 1 && MI.getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1; 5852 5853 // FIXME: AsmPrinter doesn't know how to handle 5854 // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding. 5855 if (MI.getOpcode() == X86::ADD32ri && 5856 MI.getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS) 5857 return nullptr; 5858 5859 // GOTTPOFF relocation loads can only be folded into add instructions. 5860 // FIXME: Need to exclude other relocations that only support specific 5861 // instructions. 5862 if (MOs.size() == X86::AddrNumOperands && 5863 MOs[X86::AddrDisp].getTargetFlags() == X86II::MO_GOTTPOFF && 5864 MI.getOpcode() != X86::ADD64rr) 5865 return nullptr; 5866 5867 MachineInstr *NewMI = nullptr; 5868 5869 // Attempt to fold any custom cases we have. 5870 if (MachineInstr *CustomMI = foldMemoryOperandCustom( 5871 MF, MI, OpNum, MOs, InsertPt, Size, Alignment)) 5872 return CustomMI; 5873 5874 const X86MemoryFoldTableEntry *I = nullptr; 5875 5876 // Folding a memory location into the two-address part of a two-address 5877 // instruction is different than folding it other places. It requires 5878 // replacing the *two* registers with the memory location. 5879 if (isTwoAddr && NumOps >= 2 && OpNum < 2 && MI.getOperand(0).isReg() && 5880 MI.getOperand(1).isReg() && 5881 MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) { 5882 I = lookupTwoAddrFoldTable(MI.getOpcode()); 5883 isTwoAddrFold = true; 5884 } else { 5885 if (OpNum == 0) { 5886 if (MI.getOpcode() == X86::MOV32r0) { 5887 NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, InsertPt, MI); 5888 if (NewMI) 5889 return NewMI; 5890 } 5891 } 5892 5893 I = lookupFoldTable(MI.getOpcode(), OpNum); 5894 } 5895 5896 if (I != nullptr) { 5897 unsigned Opcode = I->DstOp; 5898 bool FoldedLoad = 5899 isTwoAddrFold || (OpNum == 0 && I->Flags & TB_FOLDED_LOAD) || OpNum > 0; 5900 bool FoldedStore = 5901 isTwoAddrFold || (OpNum == 0 && I->Flags & TB_FOLDED_STORE); 5902 MaybeAlign MinAlign = 5903 decodeMaybeAlign((I->Flags & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT); 5904 if (MinAlign && Alignment < *MinAlign) 5905 return nullptr; 5906 bool NarrowToMOV32rm = false; 5907 if (Size) { 5908 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 5909 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum, 5910 &RI, MF); 5911 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8; 5912 // Check if it's safe to fold the load. If the size of the object is 5913 // narrower than the load width, then it's not. 5914 // FIXME: Allow scalar intrinsic instructions like ADDSSrm_Int. 5915 if (FoldedLoad && Size < RCSize) { 5916 // If this is a 64-bit load, but the spill slot is 32, then we can do 5917 // a 32-bit load which is implicitly zero-extended. This likely is 5918 // due to live interval analysis remat'ing a load from stack slot. 5919 if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4) 5920 return nullptr; 5921 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg()) 5922 return nullptr; 5923 Opcode = X86::MOV32rm; 5924 NarrowToMOV32rm = true; 5925 } 5926 // For stores, make sure the size of the object is equal to the size of 5927 // the store. If the object is larger, the extra bits would be garbage. If 5928 // the object is smaller we might overwrite another object or fault. 5929 if (FoldedStore && Size != RCSize) 5930 return nullptr; 5931 } 5932 5933 if (isTwoAddrFold) 5934 NewMI = FuseTwoAddrInst(MF, Opcode, MOs, InsertPt, MI, *this); 5935 else 5936 NewMI = FuseInst(MF, Opcode, OpNum, MOs, InsertPt, MI, *this); 5937 5938 if (NarrowToMOV32rm) { 5939 // If this is the special case where we use a MOV32rm to load a 32-bit 5940 // value and zero-extend the top bits. Change the destination register 5941 // to a 32-bit one. 5942 Register DstReg = NewMI->getOperand(0).getReg(); 5943 if (DstReg.isPhysical()) 5944 NewMI->getOperand(0).setReg(RI.getSubReg(DstReg, X86::sub_32bit)); 5945 else 5946 NewMI->getOperand(0).setSubReg(X86::sub_32bit); 5947 } 5948 return NewMI; 5949 } 5950 5951 // If the instruction and target operand are commutable, commute the 5952 // instruction and try again. 5953 if (AllowCommute) { 5954 unsigned CommuteOpIdx1 = OpNum, CommuteOpIdx2 = CommuteAnyOperandIndex; 5955 if (findCommutedOpIndices(MI, CommuteOpIdx1, CommuteOpIdx2)) { 5956 bool HasDef = MI.getDesc().getNumDefs(); 5957 Register Reg0 = HasDef ? MI.getOperand(0).getReg() : Register(); 5958 Register Reg1 = MI.getOperand(CommuteOpIdx1).getReg(); 5959 Register Reg2 = MI.getOperand(CommuteOpIdx2).getReg(); 5960 bool Tied1 = 5961 0 == MI.getDesc().getOperandConstraint(CommuteOpIdx1, MCOI::TIED_TO); 5962 bool Tied2 = 5963 0 == MI.getDesc().getOperandConstraint(CommuteOpIdx2, MCOI::TIED_TO); 5964 5965 // If either of the commutable operands are tied to the destination 5966 // then we can not commute + fold. 5967 if ((HasDef && Reg0 == Reg1 && Tied1) || 5968 (HasDef && Reg0 == Reg2 && Tied2)) 5969 return nullptr; 5970 5971 MachineInstr *CommutedMI = 5972 commuteInstruction(MI, false, CommuteOpIdx1, CommuteOpIdx2); 5973 if (!CommutedMI) { 5974 // Unable to commute. 5975 return nullptr; 5976 } 5977 if (CommutedMI != &MI) { 5978 // New instruction. We can't fold from this. 5979 CommutedMI->eraseFromParent(); 5980 return nullptr; 5981 } 5982 5983 // Attempt to fold with the commuted version of the instruction. 5984 NewMI = foldMemoryOperandImpl(MF, MI, CommuteOpIdx2, MOs, InsertPt, Size, 5985 Alignment, /*AllowCommute=*/false); 5986 if (NewMI) 5987 return NewMI; 5988 5989 // Folding failed again - undo the commute before returning. 5990 MachineInstr *UncommutedMI = 5991 commuteInstruction(MI, false, CommuteOpIdx1, CommuteOpIdx2); 5992 if (!UncommutedMI) { 5993 // Unable to commute. 5994 return nullptr; 5995 } 5996 if (UncommutedMI != &MI) { 5997 // New instruction. It doesn't need to be kept. 5998 UncommutedMI->eraseFromParent(); 5999 return nullptr; 6000 } 6001 6002 // Return here to prevent duplicate fuse failure report. 6003 return nullptr; 6004 } 6005 } 6006 6007 // No fusion 6008 if (PrintFailedFusing && !MI.isCopy()) 6009 dbgs() << "We failed to fuse operand " << OpNum << " in " << MI; 6010 return nullptr; 6011 } 6012 6013 MachineInstr * 6014 X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, 6015 ArrayRef<unsigned> Ops, 6016 MachineBasicBlock::iterator InsertPt, 6017 int FrameIndex, LiveIntervals *LIS, 6018 VirtRegMap *VRM) const { 6019 // Check switch flag 6020 if (NoFusing) 6021 return nullptr; 6022 6023 // Avoid partial and undef register update stalls unless optimizing for size. 6024 if (!MF.getFunction().hasOptSize() && 6025 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/true) || 6026 shouldPreventUndefRegUpdateMemFold(MF, MI))) 6027 return nullptr; 6028 6029 // Don't fold subreg spills, or reloads that use a high subreg. 6030 for (auto Op : Ops) { 6031 MachineOperand &MO = MI.getOperand(Op); 6032 auto SubReg = MO.getSubReg(); 6033 if (SubReg && (MO.isDef() || SubReg == X86::sub_8bit_hi)) 6034 return nullptr; 6035 } 6036 6037 const MachineFrameInfo &MFI = MF.getFrameInfo(); 6038 unsigned Size = MFI.getObjectSize(FrameIndex); 6039 Align Alignment = MFI.getObjectAlign(FrameIndex); 6040 // If the function stack isn't realigned we don't want to fold instructions 6041 // that need increased alignment. 6042 if (!RI.hasStackRealignment(MF)) 6043 Alignment = 6044 std::min(Alignment, Subtarget.getFrameLowering()->getStackAlign()); 6045 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 6046 unsigned NewOpc = 0; 6047 unsigned RCSize = 0; 6048 switch (MI.getOpcode()) { 6049 default: return nullptr; 6050 case X86::TEST8rr: NewOpc = X86::CMP8ri; RCSize = 1; break; 6051 case X86::TEST16rr: NewOpc = X86::CMP16ri8; RCSize = 2; break; 6052 case X86::TEST32rr: NewOpc = X86::CMP32ri8; RCSize = 4; break; 6053 case X86::TEST64rr: NewOpc = X86::CMP64ri8; RCSize = 8; break; 6054 } 6055 // Check if it's safe to fold the load. If the size of the object is 6056 // narrower than the load width, then it's not. 6057 if (Size < RCSize) 6058 return nullptr; 6059 // Change to CMPXXri r, 0 first. 6060 MI.setDesc(get(NewOpc)); 6061 MI.getOperand(1).ChangeToImmediate(0); 6062 } else if (Ops.size() != 1) 6063 return nullptr; 6064 6065 return foldMemoryOperandImpl(MF, MI, Ops[0], 6066 MachineOperand::CreateFI(FrameIndex), InsertPt, 6067 Size, Alignment, /*AllowCommute=*/true); 6068 } 6069 6070 /// Check if \p LoadMI is a partial register load that we can't fold into \p MI 6071 /// because the latter uses contents that wouldn't be defined in the folded 6072 /// version. For instance, this transformation isn't legal: 6073 /// movss (%rdi), %xmm0 6074 /// addps %xmm0, %xmm0 6075 /// -> 6076 /// addps (%rdi), %xmm0 6077 /// 6078 /// But this one is: 6079 /// movss (%rdi), %xmm0 6080 /// addss %xmm0, %xmm0 6081 /// -> 6082 /// addss (%rdi), %xmm0 6083 /// 6084 static bool isNonFoldablePartialRegisterLoad(const MachineInstr &LoadMI, 6085 const MachineInstr &UserMI, 6086 const MachineFunction &MF) { 6087 unsigned Opc = LoadMI.getOpcode(); 6088 unsigned UserOpc = UserMI.getOpcode(); 6089 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 6090 const TargetRegisterClass *RC = 6091 MF.getRegInfo().getRegClass(LoadMI.getOperand(0).getReg()); 6092 unsigned RegSize = TRI.getRegSizeInBits(*RC); 6093 6094 if ((Opc == X86::MOVSSrm || Opc == X86::VMOVSSrm || Opc == X86::VMOVSSZrm || 6095 Opc == X86::MOVSSrm_alt || Opc == X86::VMOVSSrm_alt || 6096 Opc == X86::VMOVSSZrm_alt) && 6097 RegSize > 32) { 6098 // These instructions only load 32 bits, we can't fold them if the 6099 // destination register is wider than 32 bits (4 bytes), and its user 6100 // instruction isn't scalar (SS). 6101 switch (UserOpc) { 6102 case X86::CVTSS2SDrr_Int: 6103 case X86::VCVTSS2SDrr_Int: 6104 case X86::VCVTSS2SDZrr_Int: 6105 case X86::VCVTSS2SDZrr_Intk: 6106 case X86::VCVTSS2SDZrr_Intkz: 6107 case X86::CVTSS2SIrr_Int: case X86::CVTSS2SI64rr_Int: 6108 case X86::VCVTSS2SIrr_Int: case X86::VCVTSS2SI64rr_Int: 6109 case X86::VCVTSS2SIZrr_Int: case X86::VCVTSS2SI64Zrr_Int: 6110 case X86::CVTTSS2SIrr_Int: case X86::CVTTSS2SI64rr_Int: 6111 case X86::VCVTTSS2SIrr_Int: case X86::VCVTTSS2SI64rr_Int: 6112 case X86::VCVTTSS2SIZrr_Int: case X86::VCVTTSS2SI64Zrr_Int: 6113 case X86::VCVTSS2USIZrr_Int: case X86::VCVTSS2USI64Zrr_Int: 6114 case X86::VCVTTSS2USIZrr_Int: case X86::VCVTTSS2USI64Zrr_Int: 6115 case X86::RCPSSr_Int: case X86::VRCPSSr_Int: 6116 case X86::RSQRTSSr_Int: case X86::VRSQRTSSr_Int: 6117 case X86::ROUNDSSr_Int: case X86::VROUNDSSr_Int: 6118 case X86::COMISSrr_Int: case X86::VCOMISSrr_Int: case X86::VCOMISSZrr_Int: 6119 case X86::UCOMISSrr_Int:case X86::VUCOMISSrr_Int:case X86::VUCOMISSZrr_Int: 6120 case X86::ADDSSrr_Int: case X86::VADDSSrr_Int: case X86::VADDSSZrr_Int: 6121 case X86::CMPSSrr_Int: case X86::VCMPSSrr_Int: case X86::VCMPSSZrr_Int: 6122 case X86::DIVSSrr_Int: case X86::VDIVSSrr_Int: case X86::VDIVSSZrr_Int: 6123 case X86::MAXSSrr_Int: case X86::VMAXSSrr_Int: case X86::VMAXSSZrr_Int: 6124 case X86::MINSSrr_Int: case X86::VMINSSrr_Int: case X86::VMINSSZrr_Int: 6125 case X86::MULSSrr_Int: case X86::VMULSSrr_Int: case X86::VMULSSZrr_Int: 6126 case X86::SQRTSSr_Int: case X86::VSQRTSSr_Int: case X86::VSQRTSSZr_Int: 6127 case X86::SUBSSrr_Int: case X86::VSUBSSrr_Int: case X86::VSUBSSZrr_Int: 6128 case X86::VADDSSZrr_Intk: case X86::VADDSSZrr_Intkz: 6129 case X86::VCMPSSZrr_Intk: 6130 case X86::VDIVSSZrr_Intk: case X86::VDIVSSZrr_Intkz: 6131 case X86::VMAXSSZrr_Intk: case X86::VMAXSSZrr_Intkz: 6132 case X86::VMINSSZrr_Intk: case X86::VMINSSZrr_Intkz: 6133 case X86::VMULSSZrr_Intk: case X86::VMULSSZrr_Intkz: 6134 case X86::VSQRTSSZr_Intk: case X86::VSQRTSSZr_Intkz: 6135 case X86::VSUBSSZrr_Intk: case X86::VSUBSSZrr_Intkz: 6136 case X86::VFMADDSS4rr_Int: case X86::VFNMADDSS4rr_Int: 6137 case X86::VFMSUBSS4rr_Int: case X86::VFNMSUBSS4rr_Int: 6138 case X86::VFMADD132SSr_Int: case X86::VFNMADD132SSr_Int: 6139 case X86::VFMADD213SSr_Int: case X86::VFNMADD213SSr_Int: 6140 case X86::VFMADD231SSr_Int: case X86::VFNMADD231SSr_Int: 6141 case X86::VFMSUB132SSr_Int: case X86::VFNMSUB132SSr_Int: 6142 case X86::VFMSUB213SSr_Int: case X86::VFNMSUB213SSr_Int: 6143 case X86::VFMSUB231SSr_Int: case X86::VFNMSUB231SSr_Int: 6144 case X86::VFMADD132SSZr_Int: case X86::VFNMADD132SSZr_Int: 6145 case X86::VFMADD213SSZr_Int: case X86::VFNMADD213SSZr_Int: 6146 case X86::VFMADD231SSZr_Int: case X86::VFNMADD231SSZr_Int: 6147 case X86::VFMSUB132SSZr_Int: case X86::VFNMSUB132SSZr_Int: 6148 case X86::VFMSUB213SSZr_Int: case X86::VFNMSUB213SSZr_Int: 6149 case X86::VFMSUB231SSZr_Int: case X86::VFNMSUB231SSZr_Int: 6150 case X86::VFMADD132SSZr_Intk: case X86::VFNMADD132SSZr_Intk: 6151 case X86::VFMADD213SSZr_Intk: case X86::VFNMADD213SSZr_Intk: 6152 case X86::VFMADD231SSZr_Intk: case X86::VFNMADD231SSZr_Intk: 6153 case X86::VFMSUB132SSZr_Intk: case X86::VFNMSUB132SSZr_Intk: 6154 case X86::VFMSUB213SSZr_Intk: case X86::VFNMSUB213SSZr_Intk: 6155 case X86::VFMSUB231SSZr_Intk: case X86::VFNMSUB231SSZr_Intk: 6156 case X86::VFMADD132SSZr_Intkz: case X86::VFNMADD132SSZr_Intkz: 6157 case X86::VFMADD213SSZr_Intkz: case X86::VFNMADD213SSZr_Intkz: 6158 case X86::VFMADD231SSZr_Intkz: case X86::VFNMADD231SSZr_Intkz: 6159 case X86::VFMSUB132SSZr_Intkz: case X86::VFNMSUB132SSZr_Intkz: 6160 case X86::VFMSUB213SSZr_Intkz: case X86::VFNMSUB213SSZr_Intkz: 6161 case X86::VFMSUB231SSZr_Intkz: case X86::VFNMSUB231SSZr_Intkz: 6162 case X86::VFIXUPIMMSSZrri: 6163 case X86::VFIXUPIMMSSZrrik: 6164 case X86::VFIXUPIMMSSZrrikz: 6165 case X86::VFPCLASSSSZrr: 6166 case X86::VFPCLASSSSZrrk: 6167 case X86::VGETEXPSSZr: 6168 case X86::VGETEXPSSZrk: 6169 case X86::VGETEXPSSZrkz: 6170 case X86::VGETMANTSSZrri: 6171 case X86::VGETMANTSSZrrik: 6172 case X86::VGETMANTSSZrrikz: 6173 case X86::VRANGESSZrri: 6174 case X86::VRANGESSZrrik: 6175 case X86::VRANGESSZrrikz: 6176 case X86::VRCP14SSZrr: 6177 case X86::VRCP14SSZrrk: 6178 case X86::VRCP14SSZrrkz: 6179 case X86::VRCP28SSZr: 6180 case X86::VRCP28SSZrk: 6181 case X86::VRCP28SSZrkz: 6182 case X86::VREDUCESSZrri: 6183 case X86::VREDUCESSZrrik: 6184 case X86::VREDUCESSZrrikz: 6185 case X86::VRNDSCALESSZr_Int: 6186 case X86::VRNDSCALESSZr_Intk: 6187 case X86::VRNDSCALESSZr_Intkz: 6188 case X86::VRSQRT14SSZrr: 6189 case X86::VRSQRT14SSZrrk: 6190 case X86::VRSQRT14SSZrrkz: 6191 case X86::VRSQRT28SSZr: 6192 case X86::VRSQRT28SSZrk: 6193 case X86::VRSQRT28SSZrkz: 6194 case X86::VSCALEFSSZrr: 6195 case X86::VSCALEFSSZrrk: 6196 case X86::VSCALEFSSZrrkz: 6197 return false; 6198 default: 6199 return true; 6200 } 6201 } 6202 6203 if ((Opc == X86::MOVSDrm || Opc == X86::VMOVSDrm || Opc == X86::VMOVSDZrm || 6204 Opc == X86::MOVSDrm_alt || Opc == X86::VMOVSDrm_alt || 6205 Opc == X86::VMOVSDZrm_alt) && 6206 RegSize > 64) { 6207 // These instructions only load 64 bits, we can't fold them if the 6208 // destination register is wider than 64 bits (8 bytes), and its user 6209 // instruction isn't scalar (SD). 6210 switch (UserOpc) { 6211 case X86::CVTSD2SSrr_Int: 6212 case X86::VCVTSD2SSrr_Int: 6213 case X86::VCVTSD2SSZrr_Int: 6214 case X86::VCVTSD2SSZrr_Intk: 6215 case X86::VCVTSD2SSZrr_Intkz: 6216 case X86::CVTSD2SIrr_Int: case X86::CVTSD2SI64rr_Int: 6217 case X86::VCVTSD2SIrr_Int: case X86::VCVTSD2SI64rr_Int: 6218 case X86::VCVTSD2SIZrr_Int: case X86::VCVTSD2SI64Zrr_Int: 6219 case X86::CVTTSD2SIrr_Int: case X86::CVTTSD2SI64rr_Int: 6220 case X86::VCVTTSD2SIrr_Int: case X86::VCVTTSD2SI64rr_Int: 6221 case X86::VCVTTSD2SIZrr_Int: case X86::VCVTTSD2SI64Zrr_Int: 6222 case X86::VCVTSD2USIZrr_Int: case X86::VCVTSD2USI64Zrr_Int: 6223 case X86::VCVTTSD2USIZrr_Int: case X86::VCVTTSD2USI64Zrr_Int: 6224 case X86::ROUNDSDr_Int: case X86::VROUNDSDr_Int: 6225 case X86::COMISDrr_Int: case X86::VCOMISDrr_Int: case X86::VCOMISDZrr_Int: 6226 case X86::UCOMISDrr_Int:case X86::VUCOMISDrr_Int:case X86::VUCOMISDZrr_Int: 6227 case X86::ADDSDrr_Int: case X86::VADDSDrr_Int: case X86::VADDSDZrr_Int: 6228 case X86::CMPSDrr_Int: case X86::VCMPSDrr_Int: case X86::VCMPSDZrr_Int: 6229 case X86::DIVSDrr_Int: case X86::VDIVSDrr_Int: case X86::VDIVSDZrr_Int: 6230 case X86::MAXSDrr_Int: case X86::VMAXSDrr_Int: case X86::VMAXSDZrr_Int: 6231 case X86::MINSDrr_Int: case X86::VMINSDrr_Int: case X86::VMINSDZrr_Int: 6232 case X86::MULSDrr_Int: case X86::VMULSDrr_Int: case X86::VMULSDZrr_Int: 6233 case X86::SQRTSDr_Int: case X86::VSQRTSDr_Int: case X86::VSQRTSDZr_Int: 6234 case X86::SUBSDrr_Int: case X86::VSUBSDrr_Int: case X86::VSUBSDZrr_Int: 6235 case X86::VADDSDZrr_Intk: case X86::VADDSDZrr_Intkz: 6236 case X86::VCMPSDZrr_Intk: 6237 case X86::VDIVSDZrr_Intk: case X86::VDIVSDZrr_Intkz: 6238 case X86::VMAXSDZrr_Intk: case X86::VMAXSDZrr_Intkz: 6239 case X86::VMINSDZrr_Intk: case X86::VMINSDZrr_Intkz: 6240 case X86::VMULSDZrr_Intk: case X86::VMULSDZrr_Intkz: 6241 case X86::VSQRTSDZr_Intk: case X86::VSQRTSDZr_Intkz: 6242 case X86::VSUBSDZrr_Intk: case X86::VSUBSDZrr_Intkz: 6243 case X86::VFMADDSD4rr_Int: case X86::VFNMADDSD4rr_Int: 6244 case X86::VFMSUBSD4rr_Int: case X86::VFNMSUBSD4rr_Int: 6245 case X86::VFMADD132SDr_Int: case X86::VFNMADD132SDr_Int: 6246 case X86::VFMADD213SDr_Int: case X86::VFNMADD213SDr_Int: 6247 case X86::VFMADD231SDr_Int: case X86::VFNMADD231SDr_Int: 6248 case X86::VFMSUB132SDr_Int: case X86::VFNMSUB132SDr_Int: 6249 case X86::VFMSUB213SDr_Int: case X86::VFNMSUB213SDr_Int: 6250 case X86::VFMSUB231SDr_Int: case X86::VFNMSUB231SDr_Int: 6251 case X86::VFMADD132SDZr_Int: case X86::VFNMADD132SDZr_Int: 6252 case X86::VFMADD213SDZr_Int: case X86::VFNMADD213SDZr_Int: 6253 case X86::VFMADD231SDZr_Int: case X86::VFNMADD231SDZr_Int: 6254 case X86::VFMSUB132SDZr_Int: case X86::VFNMSUB132SDZr_Int: 6255 case X86::VFMSUB213SDZr_Int: case X86::VFNMSUB213SDZr_Int: 6256 case X86::VFMSUB231SDZr_Int: case X86::VFNMSUB231SDZr_Int: 6257 case X86::VFMADD132SDZr_Intk: case X86::VFNMADD132SDZr_Intk: 6258 case X86::VFMADD213SDZr_Intk: case X86::VFNMADD213SDZr_Intk: 6259 case X86::VFMADD231SDZr_Intk: case X86::VFNMADD231SDZr_Intk: 6260 case X86::VFMSUB132SDZr_Intk: case X86::VFNMSUB132SDZr_Intk: 6261 case X86::VFMSUB213SDZr_Intk: case X86::VFNMSUB213SDZr_Intk: 6262 case X86::VFMSUB231SDZr_Intk: case X86::VFNMSUB231SDZr_Intk: 6263 case X86::VFMADD132SDZr_Intkz: case X86::VFNMADD132SDZr_Intkz: 6264 case X86::VFMADD213SDZr_Intkz: case X86::VFNMADD213SDZr_Intkz: 6265 case X86::VFMADD231SDZr_Intkz: case X86::VFNMADD231SDZr_Intkz: 6266 case X86::VFMSUB132SDZr_Intkz: case X86::VFNMSUB132SDZr_Intkz: 6267 case X86::VFMSUB213SDZr_Intkz: case X86::VFNMSUB213SDZr_Intkz: 6268 case X86::VFMSUB231SDZr_Intkz: case X86::VFNMSUB231SDZr_Intkz: 6269 case X86::VFIXUPIMMSDZrri: 6270 case X86::VFIXUPIMMSDZrrik: 6271 case X86::VFIXUPIMMSDZrrikz: 6272 case X86::VFPCLASSSDZrr: 6273 case X86::VFPCLASSSDZrrk: 6274 case X86::VGETEXPSDZr: 6275 case X86::VGETEXPSDZrk: 6276 case X86::VGETEXPSDZrkz: 6277 case X86::VGETMANTSDZrri: 6278 case X86::VGETMANTSDZrrik: 6279 case X86::VGETMANTSDZrrikz: 6280 case X86::VRANGESDZrri: 6281 case X86::VRANGESDZrrik: 6282 case X86::VRANGESDZrrikz: 6283 case X86::VRCP14SDZrr: 6284 case X86::VRCP14SDZrrk: 6285 case X86::VRCP14SDZrrkz: 6286 case X86::VRCP28SDZr: 6287 case X86::VRCP28SDZrk: 6288 case X86::VRCP28SDZrkz: 6289 case X86::VREDUCESDZrri: 6290 case X86::VREDUCESDZrrik: 6291 case X86::VREDUCESDZrrikz: 6292 case X86::VRNDSCALESDZr_Int: 6293 case X86::VRNDSCALESDZr_Intk: 6294 case X86::VRNDSCALESDZr_Intkz: 6295 case X86::VRSQRT14SDZrr: 6296 case X86::VRSQRT14SDZrrk: 6297 case X86::VRSQRT14SDZrrkz: 6298 case X86::VRSQRT28SDZr: 6299 case X86::VRSQRT28SDZrk: 6300 case X86::VRSQRT28SDZrkz: 6301 case X86::VSCALEFSDZrr: 6302 case X86::VSCALEFSDZrrk: 6303 case X86::VSCALEFSDZrrkz: 6304 return false; 6305 default: 6306 return true; 6307 } 6308 } 6309 6310 if ((Opc == X86::VMOVSHZrm || Opc == X86::VMOVSHZrm_alt) && RegSize > 16) { 6311 // These instructions only load 16 bits, we can't fold them if the 6312 // destination register is wider than 16 bits (2 bytes), and its user 6313 // instruction isn't scalar (SH). 6314 switch (UserOpc) { 6315 case X86::VADDSHZrr_Int: 6316 case X86::VCMPSHZrr_Int: 6317 case X86::VDIVSHZrr_Int: 6318 case X86::VMAXSHZrr_Int: 6319 case X86::VMINSHZrr_Int: 6320 case X86::VMULSHZrr_Int: 6321 case X86::VSUBSHZrr_Int: 6322 case X86::VADDSHZrr_Intk: case X86::VADDSHZrr_Intkz: 6323 case X86::VCMPSHZrr_Intk: 6324 case X86::VDIVSHZrr_Intk: case X86::VDIVSHZrr_Intkz: 6325 case X86::VMAXSHZrr_Intk: case X86::VMAXSHZrr_Intkz: 6326 case X86::VMINSHZrr_Intk: case X86::VMINSHZrr_Intkz: 6327 case X86::VMULSHZrr_Intk: case X86::VMULSHZrr_Intkz: 6328 case X86::VSUBSHZrr_Intk: case X86::VSUBSHZrr_Intkz: 6329 case X86::VFMADD132SHZr_Int: case X86::VFNMADD132SHZr_Int: 6330 case X86::VFMADD213SHZr_Int: case X86::VFNMADD213SHZr_Int: 6331 case X86::VFMADD231SHZr_Int: case X86::VFNMADD231SHZr_Int: 6332 case X86::VFMSUB132SHZr_Int: case X86::VFNMSUB132SHZr_Int: 6333 case X86::VFMSUB213SHZr_Int: case X86::VFNMSUB213SHZr_Int: 6334 case X86::VFMSUB231SHZr_Int: case X86::VFNMSUB231SHZr_Int: 6335 case X86::VFMADD132SHZr_Intk: case X86::VFNMADD132SHZr_Intk: 6336 case X86::VFMADD213SHZr_Intk: case X86::VFNMADD213SHZr_Intk: 6337 case X86::VFMADD231SHZr_Intk: case X86::VFNMADD231SHZr_Intk: 6338 case X86::VFMSUB132SHZr_Intk: case X86::VFNMSUB132SHZr_Intk: 6339 case X86::VFMSUB213SHZr_Intk: case X86::VFNMSUB213SHZr_Intk: 6340 case X86::VFMSUB231SHZr_Intk: case X86::VFNMSUB231SHZr_Intk: 6341 case X86::VFMADD132SHZr_Intkz: case X86::VFNMADD132SHZr_Intkz: 6342 case X86::VFMADD213SHZr_Intkz: case X86::VFNMADD213SHZr_Intkz: 6343 case X86::VFMADD231SHZr_Intkz: case X86::VFNMADD231SHZr_Intkz: 6344 case X86::VFMSUB132SHZr_Intkz: case X86::VFNMSUB132SHZr_Intkz: 6345 case X86::VFMSUB213SHZr_Intkz: case X86::VFNMSUB213SHZr_Intkz: 6346 case X86::VFMSUB231SHZr_Intkz: case X86::VFNMSUB231SHZr_Intkz: 6347 return false; 6348 default: 6349 return true; 6350 } 6351 } 6352 6353 return false; 6354 } 6355 6356 MachineInstr *X86InstrInfo::foldMemoryOperandImpl( 6357 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 6358 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI, 6359 LiveIntervals *LIS) const { 6360 6361 // TODO: Support the case where LoadMI loads a wide register, but MI 6362 // only uses a subreg. 6363 for (auto Op : Ops) { 6364 if (MI.getOperand(Op).getSubReg()) 6365 return nullptr; 6366 } 6367 6368 // If loading from a FrameIndex, fold directly from the FrameIndex. 6369 unsigned NumOps = LoadMI.getDesc().getNumOperands(); 6370 int FrameIndex; 6371 if (isLoadFromStackSlot(LoadMI, FrameIndex)) { 6372 if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF)) 6373 return nullptr; 6374 return foldMemoryOperandImpl(MF, MI, Ops, InsertPt, FrameIndex, LIS); 6375 } 6376 6377 // Check switch flag 6378 if (NoFusing) return nullptr; 6379 6380 // Avoid partial and undef register update stalls unless optimizing for size. 6381 if (!MF.getFunction().hasOptSize() && 6382 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/true) || 6383 shouldPreventUndefRegUpdateMemFold(MF, MI))) 6384 return nullptr; 6385 6386 // Determine the alignment of the load. 6387 Align Alignment; 6388 if (LoadMI.hasOneMemOperand()) 6389 Alignment = (*LoadMI.memoperands_begin())->getAlign(); 6390 else 6391 switch (LoadMI.getOpcode()) { 6392 case X86::AVX512_512_SET0: 6393 case X86::AVX512_512_SETALLONES: 6394 Alignment = Align(64); 6395 break; 6396 case X86::AVX2_SETALLONES: 6397 case X86::AVX1_SETALLONES: 6398 case X86::AVX_SET0: 6399 case X86::AVX512_256_SET0: 6400 Alignment = Align(32); 6401 break; 6402 case X86::V_SET0: 6403 case X86::V_SETALLONES: 6404 case X86::AVX512_128_SET0: 6405 case X86::FsFLD0F128: 6406 case X86::AVX512_FsFLD0F128: 6407 Alignment = Align(16); 6408 break; 6409 case X86::MMX_SET0: 6410 case X86::FsFLD0SD: 6411 case X86::AVX512_FsFLD0SD: 6412 Alignment = Align(8); 6413 break; 6414 case X86::FsFLD0SS: 6415 case X86::AVX512_FsFLD0SS: 6416 Alignment = Align(4); 6417 break; 6418 case X86::AVX512_FsFLD0SH: 6419 Alignment = Align(2); 6420 break; 6421 default: 6422 return nullptr; 6423 } 6424 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 6425 unsigned NewOpc = 0; 6426 switch (MI.getOpcode()) { 6427 default: return nullptr; 6428 case X86::TEST8rr: NewOpc = X86::CMP8ri; break; 6429 case X86::TEST16rr: NewOpc = X86::CMP16ri8; break; 6430 case X86::TEST32rr: NewOpc = X86::CMP32ri8; break; 6431 case X86::TEST64rr: NewOpc = X86::CMP64ri8; break; 6432 } 6433 // Change to CMPXXri r, 0 first. 6434 MI.setDesc(get(NewOpc)); 6435 MI.getOperand(1).ChangeToImmediate(0); 6436 } else if (Ops.size() != 1) 6437 return nullptr; 6438 6439 // Make sure the subregisters match. 6440 // Otherwise we risk changing the size of the load. 6441 if (LoadMI.getOperand(0).getSubReg() != MI.getOperand(Ops[0]).getSubReg()) 6442 return nullptr; 6443 6444 SmallVector<MachineOperand,X86::AddrNumOperands> MOs; 6445 switch (LoadMI.getOpcode()) { 6446 case X86::MMX_SET0: 6447 case X86::V_SET0: 6448 case X86::V_SETALLONES: 6449 case X86::AVX2_SETALLONES: 6450 case X86::AVX1_SETALLONES: 6451 case X86::AVX_SET0: 6452 case X86::AVX512_128_SET0: 6453 case X86::AVX512_256_SET0: 6454 case X86::AVX512_512_SET0: 6455 case X86::AVX512_512_SETALLONES: 6456 case X86::AVX512_FsFLD0SH: 6457 case X86::FsFLD0SD: 6458 case X86::AVX512_FsFLD0SD: 6459 case X86::FsFLD0SS: 6460 case X86::AVX512_FsFLD0SS: 6461 case X86::FsFLD0F128: 6462 case X86::AVX512_FsFLD0F128: { 6463 // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure. 6464 // Create a constant-pool entry and operands to load from it. 6465 6466 // Medium and large mode can't fold loads this way. 6467 if (MF.getTarget().getCodeModel() != CodeModel::Small && 6468 MF.getTarget().getCodeModel() != CodeModel::Kernel) 6469 return nullptr; 6470 6471 // x86-32 PIC requires a PIC base register for constant pools. 6472 unsigned PICBase = 0; 6473 // Since we're using Small or Kernel code model, we can always use 6474 // RIP-relative addressing for a smaller encoding. 6475 if (Subtarget.is64Bit()) { 6476 PICBase = X86::RIP; 6477 } else if (MF.getTarget().isPositionIndependent()) { 6478 // FIXME: PICBase = getGlobalBaseReg(&MF); 6479 // This doesn't work for several reasons. 6480 // 1. GlobalBaseReg may have been spilled. 6481 // 2. It may not be live at MI. 6482 return nullptr; 6483 } 6484 6485 // Create a constant-pool entry. 6486 MachineConstantPool &MCP = *MF.getConstantPool(); 6487 Type *Ty; 6488 unsigned Opc = LoadMI.getOpcode(); 6489 if (Opc == X86::FsFLD0SS || Opc == X86::AVX512_FsFLD0SS) 6490 Ty = Type::getFloatTy(MF.getFunction().getContext()); 6491 else if (Opc == X86::FsFLD0SD || Opc == X86::AVX512_FsFLD0SD) 6492 Ty = Type::getDoubleTy(MF.getFunction().getContext()); 6493 else if (Opc == X86::FsFLD0F128 || Opc == X86::AVX512_FsFLD0F128) 6494 Ty = Type::getFP128Ty(MF.getFunction().getContext()); 6495 else if (Opc == X86::AVX512_FsFLD0SH) 6496 Ty = Type::getHalfTy(MF.getFunction().getContext()); 6497 else if (Opc == X86::AVX512_512_SET0 || Opc == X86::AVX512_512_SETALLONES) 6498 Ty = FixedVectorType::get(Type::getInt32Ty(MF.getFunction().getContext()), 6499 16); 6500 else if (Opc == X86::AVX2_SETALLONES || Opc == X86::AVX_SET0 || 6501 Opc == X86::AVX512_256_SET0 || Opc == X86::AVX1_SETALLONES) 6502 Ty = FixedVectorType::get(Type::getInt32Ty(MF.getFunction().getContext()), 6503 8); 6504 else if (Opc == X86::MMX_SET0) 6505 Ty = FixedVectorType::get(Type::getInt32Ty(MF.getFunction().getContext()), 6506 2); 6507 else 6508 Ty = FixedVectorType::get(Type::getInt32Ty(MF.getFunction().getContext()), 6509 4); 6510 6511 bool IsAllOnes = (Opc == X86::V_SETALLONES || Opc == X86::AVX2_SETALLONES || 6512 Opc == X86::AVX512_512_SETALLONES || 6513 Opc == X86::AVX1_SETALLONES); 6514 const Constant *C = IsAllOnes ? Constant::getAllOnesValue(Ty) : 6515 Constant::getNullValue(Ty); 6516 unsigned CPI = MCP.getConstantPoolIndex(C, Alignment); 6517 6518 // Create operands to load from the constant pool entry. 6519 MOs.push_back(MachineOperand::CreateReg(PICBase, false)); 6520 MOs.push_back(MachineOperand::CreateImm(1)); 6521 MOs.push_back(MachineOperand::CreateReg(0, false)); 6522 MOs.push_back(MachineOperand::CreateCPI(CPI, 0)); 6523 MOs.push_back(MachineOperand::CreateReg(0, false)); 6524 break; 6525 } 6526 default: { 6527 if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF)) 6528 return nullptr; 6529 6530 // Folding a normal load. Just copy the load's address operands. 6531 MOs.append(LoadMI.operands_begin() + NumOps - X86::AddrNumOperands, 6532 LoadMI.operands_begin() + NumOps); 6533 break; 6534 } 6535 } 6536 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, InsertPt, 6537 /*Size=*/0, Alignment, /*AllowCommute=*/true); 6538 } 6539 6540 static SmallVector<MachineMemOperand *, 2> 6541 extractLoadMMOs(ArrayRef<MachineMemOperand *> MMOs, MachineFunction &MF) { 6542 SmallVector<MachineMemOperand *, 2> LoadMMOs; 6543 6544 for (MachineMemOperand *MMO : MMOs) { 6545 if (!MMO->isLoad()) 6546 continue; 6547 6548 if (!MMO->isStore()) { 6549 // Reuse the MMO. 6550 LoadMMOs.push_back(MMO); 6551 } else { 6552 // Clone the MMO and unset the store flag. 6553 LoadMMOs.push_back(MF.getMachineMemOperand( 6554 MMO, MMO->getFlags() & ~MachineMemOperand::MOStore)); 6555 } 6556 } 6557 6558 return LoadMMOs; 6559 } 6560 6561 static SmallVector<MachineMemOperand *, 2> 6562 extractStoreMMOs(ArrayRef<MachineMemOperand *> MMOs, MachineFunction &MF) { 6563 SmallVector<MachineMemOperand *, 2> StoreMMOs; 6564 6565 for (MachineMemOperand *MMO : MMOs) { 6566 if (!MMO->isStore()) 6567 continue; 6568 6569 if (!MMO->isLoad()) { 6570 // Reuse the MMO. 6571 StoreMMOs.push_back(MMO); 6572 } else { 6573 // Clone the MMO and unset the load flag. 6574 StoreMMOs.push_back(MF.getMachineMemOperand( 6575 MMO, MMO->getFlags() & ~MachineMemOperand::MOLoad)); 6576 } 6577 } 6578 6579 return StoreMMOs; 6580 } 6581 6582 static unsigned getBroadcastOpcode(const X86MemoryFoldTableEntry *I, 6583 const TargetRegisterClass *RC, 6584 const X86Subtarget &STI) { 6585 assert(STI.hasAVX512() && "Expected at least AVX512!"); 6586 unsigned SpillSize = STI.getRegisterInfo()->getSpillSize(*RC); 6587 assert((SpillSize == 64 || STI.hasVLX()) && 6588 "Can't broadcast less than 64 bytes without AVX512VL!"); 6589 6590 switch (I->Flags & TB_BCAST_MASK) { 6591 default: llvm_unreachable("Unexpected broadcast type!"); 6592 case TB_BCAST_D: 6593 switch (SpillSize) { 6594 default: llvm_unreachable("Unknown spill size"); 6595 case 16: return X86::VPBROADCASTDZ128rm; 6596 case 32: return X86::VPBROADCASTDZ256rm; 6597 case 64: return X86::VPBROADCASTDZrm; 6598 } 6599 break; 6600 case TB_BCAST_Q: 6601 switch (SpillSize) { 6602 default: llvm_unreachable("Unknown spill size"); 6603 case 16: return X86::VPBROADCASTQZ128rm; 6604 case 32: return X86::VPBROADCASTQZ256rm; 6605 case 64: return X86::VPBROADCASTQZrm; 6606 } 6607 break; 6608 case TB_BCAST_SS: 6609 switch (SpillSize) { 6610 default: llvm_unreachable("Unknown spill size"); 6611 case 16: return X86::VBROADCASTSSZ128rm; 6612 case 32: return X86::VBROADCASTSSZ256rm; 6613 case 64: return X86::VBROADCASTSSZrm; 6614 } 6615 break; 6616 case TB_BCAST_SD: 6617 switch (SpillSize) { 6618 default: llvm_unreachable("Unknown spill size"); 6619 case 16: return X86::VMOVDDUPZ128rm; 6620 case 32: return X86::VBROADCASTSDZ256rm; 6621 case 64: return X86::VBROADCASTSDZrm; 6622 } 6623 break; 6624 } 6625 } 6626 6627 bool X86InstrInfo::unfoldMemoryOperand( 6628 MachineFunction &MF, MachineInstr &MI, unsigned Reg, bool UnfoldLoad, 6629 bool UnfoldStore, SmallVectorImpl<MachineInstr *> &NewMIs) const { 6630 const X86MemoryFoldTableEntry *I = lookupUnfoldTable(MI.getOpcode()); 6631 if (I == nullptr) 6632 return false; 6633 unsigned Opc = I->DstOp; 6634 unsigned Index = I->Flags & TB_INDEX_MASK; 6635 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD; 6636 bool FoldedStore = I->Flags & TB_FOLDED_STORE; 6637 bool FoldedBCast = I->Flags & TB_FOLDED_BCAST; 6638 if (UnfoldLoad && !FoldedLoad) 6639 return false; 6640 UnfoldLoad &= FoldedLoad; 6641 if (UnfoldStore && !FoldedStore) 6642 return false; 6643 UnfoldStore &= FoldedStore; 6644 6645 const MCInstrDesc &MCID = get(Opc); 6646 6647 const TargetRegisterClass *RC = getRegClass(MCID, Index, &RI, MF); 6648 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 6649 // TODO: Check if 32-byte or greater accesses are slow too? 6650 if (!MI.hasOneMemOperand() && RC == &X86::VR128RegClass && 6651 Subtarget.isUnalignedMem16Slow()) 6652 // Without memoperands, loadRegFromAddr and storeRegToStackSlot will 6653 // conservatively assume the address is unaligned. That's bad for 6654 // performance. 6655 return false; 6656 SmallVector<MachineOperand, X86::AddrNumOperands> AddrOps; 6657 SmallVector<MachineOperand,2> BeforeOps; 6658 SmallVector<MachineOperand,2> AfterOps; 6659 SmallVector<MachineOperand,4> ImpOps; 6660 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 6661 MachineOperand &Op = MI.getOperand(i); 6662 if (i >= Index && i < Index + X86::AddrNumOperands) 6663 AddrOps.push_back(Op); 6664 else if (Op.isReg() && Op.isImplicit()) 6665 ImpOps.push_back(Op); 6666 else if (i < Index) 6667 BeforeOps.push_back(Op); 6668 else if (i > Index) 6669 AfterOps.push_back(Op); 6670 } 6671 6672 // Emit the load or broadcast instruction. 6673 if (UnfoldLoad) { 6674 auto MMOs = extractLoadMMOs(MI.memoperands(), MF); 6675 6676 unsigned Opc; 6677 if (FoldedBCast) { 6678 Opc = getBroadcastOpcode(I, RC, Subtarget); 6679 } else { 6680 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16); 6681 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment; 6682 Opc = getLoadRegOpcode(Reg, RC, isAligned, Subtarget); 6683 } 6684 6685 DebugLoc DL; 6686 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), Reg); 6687 for (unsigned i = 0, e = AddrOps.size(); i != e; ++i) 6688 MIB.add(AddrOps[i]); 6689 MIB.setMemRefs(MMOs); 6690 NewMIs.push_back(MIB); 6691 6692 if (UnfoldStore) { 6693 // Address operands cannot be marked isKill. 6694 for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) { 6695 MachineOperand &MO = NewMIs[0]->getOperand(i); 6696 if (MO.isReg()) 6697 MO.setIsKill(false); 6698 } 6699 } 6700 } 6701 6702 // Emit the data processing instruction. 6703 MachineInstr *DataMI = MF.CreateMachineInstr(MCID, MI.getDebugLoc(), true); 6704 MachineInstrBuilder MIB(MF, DataMI); 6705 6706 if (FoldedStore) 6707 MIB.addReg(Reg, RegState::Define); 6708 for (MachineOperand &BeforeOp : BeforeOps) 6709 MIB.add(BeforeOp); 6710 if (FoldedLoad) 6711 MIB.addReg(Reg); 6712 for (MachineOperand &AfterOp : AfterOps) 6713 MIB.add(AfterOp); 6714 for (MachineOperand &ImpOp : ImpOps) { 6715 MIB.addReg(ImpOp.getReg(), 6716 getDefRegState(ImpOp.isDef()) | 6717 RegState::Implicit | 6718 getKillRegState(ImpOp.isKill()) | 6719 getDeadRegState(ImpOp.isDead()) | 6720 getUndefRegState(ImpOp.isUndef())); 6721 } 6722 // Change CMP32ri r, 0 back to TEST32rr r, r, etc. 6723 switch (DataMI->getOpcode()) { 6724 default: break; 6725 case X86::CMP64ri32: 6726 case X86::CMP64ri8: 6727 case X86::CMP32ri: 6728 case X86::CMP32ri8: 6729 case X86::CMP16ri: 6730 case X86::CMP16ri8: 6731 case X86::CMP8ri: { 6732 MachineOperand &MO0 = DataMI->getOperand(0); 6733 MachineOperand &MO1 = DataMI->getOperand(1); 6734 if (MO1.isImm() && MO1.getImm() == 0) { 6735 unsigned NewOpc; 6736 switch (DataMI->getOpcode()) { 6737 default: llvm_unreachable("Unreachable!"); 6738 case X86::CMP64ri8: 6739 case X86::CMP64ri32: NewOpc = X86::TEST64rr; break; 6740 case X86::CMP32ri8: 6741 case X86::CMP32ri: NewOpc = X86::TEST32rr; break; 6742 case X86::CMP16ri8: 6743 case X86::CMP16ri: NewOpc = X86::TEST16rr; break; 6744 case X86::CMP8ri: NewOpc = X86::TEST8rr; break; 6745 } 6746 DataMI->setDesc(get(NewOpc)); 6747 MO1.ChangeToRegister(MO0.getReg(), false); 6748 } 6749 } 6750 } 6751 NewMIs.push_back(DataMI); 6752 6753 // Emit the store instruction. 6754 if (UnfoldStore) { 6755 const TargetRegisterClass *DstRC = getRegClass(MCID, 0, &RI, MF); 6756 auto MMOs = extractStoreMMOs(MI.memoperands(), MF); 6757 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*DstRC), 16); 6758 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment; 6759 unsigned Opc = getStoreRegOpcode(Reg, DstRC, isAligned, Subtarget); 6760 DebugLoc DL; 6761 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc)); 6762 for (unsigned i = 0, e = AddrOps.size(); i != e; ++i) 6763 MIB.add(AddrOps[i]); 6764 MIB.addReg(Reg, RegState::Kill); 6765 MIB.setMemRefs(MMOs); 6766 NewMIs.push_back(MIB); 6767 } 6768 6769 return true; 6770 } 6771 6772 bool 6773 X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N, 6774 SmallVectorImpl<SDNode*> &NewNodes) const { 6775 if (!N->isMachineOpcode()) 6776 return false; 6777 6778 const X86MemoryFoldTableEntry *I = lookupUnfoldTable(N->getMachineOpcode()); 6779 if (I == nullptr) 6780 return false; 6781 unsigned Opc = I->DstOp; 6782 unsigned Index = I->Flags & TB_INDEX_MASK; 6783 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD; 6784 bool FoldedStore = I->Flags & TB_FOLDED_STORE; 6785 bool FoldedBCast = I->Flags & TB_FOLDED_BCAST; 6786 const MCInstrDesc &MCID = get(Opc); 6787 MachineFunction &MF = DAG.getMachineFunction(); 6788 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 6789 const TargetRegisterClass *RC = getRegClass(MCID, Index, &RI, MF); 6790 unsigned NumDefs = MCID.NumDefs; 6791 std::vector<SDValue> AddrOps; 6792 std::vector<SDValue> BeforeOps; 6793 std::vector<SDValue> AfterOps; 6794 SDLoc dl(N); 6795 unsigned NumOps = N->getNumOperands(); 6796 for (unsigned i = 0; i != NumOps-1; ++i) { 6797 SDValue Op = N->getOperand(i); 6798 if (i >= Index-NumDefs && i < Index-NumDefs + X86::AddrNumOperands) 6799 AddrOps.push_back(Op); 6800 else if (i < Index-NumDefs) 6801 BeforeOps.push_back(Op); 6802 else if (i > Index-NumDefs) 6803 AfterOps.push_back(Op); 6804 } 6805 SDValue Chain = N->getOperand(NumOps-1); 6806 AddrOps.push_back(Chain); 6807 6808 // Emit the load instruction. 6809 SDNode *Load = nullptr; 6810 if (FoldedLoad) { 6811 EVT VT = *TRI.legalclasstypes_begin(*RC); 6812 auto MMOs = extractLoadMMOs(cast<MachineSDNode>(N)->memoperands(), MF); 6813 if (MMOs.empty() && RC == &X86::VR128RegClass && 6814 Subtarget.isUnalignedMem16Slow()) 6815 // Do not introduce a slow unaligned load. 6816 return false; 6817 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte 6818 // memory access is slow above. 6819 6820 unsigned Opc; 6821 if (FoldedBCast) { 6822 Opc = getBroadcastOpcode(I, RC, Subtarget); 6823 } else { 6824 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16); 6825 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment; 6826 Opc = getLoadRegOpcode(0, RC, isAligned, Subtarget); 6827 } 6828 6829 Load = DAG.getMachineNode(Opc, dl, VT, MVT::Other, AddrOps); 6830 NewNodes.push_back(Load); 6831 6832 // Preserve memory reference information. 6833 DAG.setNodeMemRefs(cast<MachineSDNode>(Load), MMOs); 6834 } 6835 6836 // Emit the data processing instruction. 6837 std::vector<EVT> VTs; 6838 const TargetRegisterClass *DstRC = nullptr; 6839 if (MCID.getNumDefs() > 0) { 6840 DstRC = getRegClass(MCID, 0, &RI, MF); 6841 VTs.push_back(*TRI.legalclasstypes_begin(*DstRC)); 6842 } 6843 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { 6844 EVT VT = N->getValueType(i); 6845 if (VT != MVT::Other && i >= (unsigned)MCID.getNumDefs()) 6846 VTs.push_back(VT); 6847 } 6848 if (Load) 6849 BeforeOps.push_back(SDValue(Load, 0)); 6850 llvm::append_range(BeforeOps, AfterOps); 6851 // Change CMP32ri r, 0 back to TEST32rr r, r, etc. 6852 switch (Opc) { 6853 default: break; 6854 case X86::CMP64ri32: 6855 case X86::CMP64ri8: 6856 case X86::CMP32ri: 6857 case X86::CMP32ri8: 6858 case X86::CMP16ri: 6859 case X86::CMP16ri8: 6860 case X86::CMP8ri: 6861 if (isNullConstant(BeforeOps[1])) { 6862 switch (Opc) { 6863 default: llvm_unreachable("Unreachable!"); 6864 case X86::CMP64ri8: 6865 case X86::CMP64ri32: Opc = X86::TEST64rr; break; 6866 case X86::CMP32ri8: 6867 case X86::CMP32ri: Opc = X86::TEST32rr; break; 6868 case X86::CMP16ri8: 6869 case X86::CMP16ri: Opc = X86::TEST16rr; break; 6870 case X86::CMP8ri: Opc = X86::TEST8rr; break; 6871 } 6872 BeforeOps[1] = BeforeOps[0]; 6873 } 6874 } 6875 SDNode *NewNode= DAG.getMachineNode(Opc, dl, VTs, BeforeOps); 6876 NewNodes.push_back(NewNode); 6877 6878 // Emit the store instruction. 6879 if (FoldedStore) { 6880 AddrOps.pop_back(); 6881 AddrOps.push_back(SDValue(NewNode, 0)); 6882 AddrOps.push_back(Chain); 6883 auto MMOs = extractStoreMMOs(cast<MachineSDNode>(N)->memoperands(), MF); 6884 if (MMOs.empty() && RC == &X86::VR128RegClass && 6885 Subtarget.isUnalignedMem16Slow()) 6886 // Do not introduce a slow unaligned store. 6887 return false; 6888 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte 6889 // memory access is slow above. 6890 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16); 6891 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment; 6892 SDNode *Store = 6893 DAG.getMachineNode(getStoreRegOpcode(0, DstRC, isAligned, Subtarget), 6894 dl, MVT::Other, AddrOps); 6895 NewNodes.push_back(Store); 6896 6897 // Preserve memory reference information. 6898 DAG.setNodeMemRefs(cast<MachineSDNode>(Store), MMOs); 6899 } 6900 6901 return true; 6902 } 6903 6904 unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc, 6905 bool UnfoldLoad, bool UnfoldStore, 6906 unsigned *LoadRegIndex) const { 6907 const X86MemoryFoldTableEntry *I = lookupUnfoldTable(Opc); 6908 if (I == nullptr) 6909 return 0; 6910 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD; 6911 bool FoldedStore = I->Flags & TB_FOLDED_STORE; 6912 if (UnfoldLoad && !FoldedLoad) 6913 return 0; 6914 if (UnfoldStore && !FoldedStore) 6915 return 0; 6916 if (LoadRegIndex) 6917 *LoadRegIndex = I->Flags & TB_INDEX_MASK; 6918 return I->DstOp; 6919 } 6920 6921 bool 6922 X86InstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, 6923 int64_t &Offset1, int64_t &Offset2) const { 6924 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode()) 6925 return false; 6926 unsigned Opc1 = Load1->getMachineOpcode(); 6927 unsigned Opc2 = Load2->getMachineOpcode(); 6928 switch (Opc1) { 6929 default: return false; 6930 case X86::MOV8rm: 6931 case X86::MOV16rm: 6932 case X86::MOV32rm: 6933 case X86::MOV64rm: 6934 case X86::LD_Fp32m: 6935 case X86::LD_Fp64m: 6936 case X86::LD_Fp80m: 6937 case X86::MOVSSrm: 6938 case X86::MOVSSrm_alt: 6939 case X86::MOVSDrm: 6940 case X86::MOVSDrm_alt: 6941 case X86::MMX_MOVD64rm: 6942 case X86::MMX_MOVQ64rm: 6943 case X86::MOVAPSrm: 6944 case X86::MOVUPSrm: 6945 case X86::MOVAPDrm: 6946 case X86::MOVUPDrm: 6947 case X86::MOVDQArm: 6948 case X86::MOVDQUrm: 6949 // AVX load instructions 6950 case X86::VMOVSSrm: 6951 case X86::VMOVSSrm_alt: 6952 case X86::VMOVSDrm: 6953 case X86::VMOVSDrm_alt: 6954 case X86::VMOVAPSrm: 6955 case X86::VMOVUPSrm: 6956 case X86::VMOVAPDrm: 6957 case X86::VMOVUPDrm: 6958 case X86::VMOVDQArm: 6959 case X86::VMOVDQUrm: 6960 case X86::VMOVAPSYrm: 6961 case X86::VMOVUPSYrm: 6962 case X86::VMOVAPDYrm: 6963 case X86::VMOVUPDYrm: 6964 case X86::VMOVDQAYrm: 6965 case X86::VMOVDQUYrm: 6966 // AVX512 load instructions 6967 case X86::VMOVSSZrm: 6968 case X86::VMOVSSZrm_alt: 6969 case X86::VMOVSDZrm: 6970 case X86::VMOVSDZrm_alt: 6971 case X86::VMOVAPSZ128rm: 6972 case X86::VMOVUPSZ128rm: 6973 case X86::VMOVAPSZ128rm_NOVLX: 6974 case X86::VMOVUPSZ128rm_NOVLX: 6975 case X86::VMOVAPDZ128rm: 6976 case X86::VMOVUPDZ128rm: 6977 case X86::VMOVDQU8Z128rm: 6978 case X86::VMOVDQU16Z128rm: 6979 case X86::VMOVDQA32Z128rm: 6980 case X86::VMOVDQU32Z128rm: 6981 case X86::VMOVDQA64Z128rm: 6982 case X86::VMOVDQU64Z128rm: 6983 case X86::VMOVAPSZ256rm: 6984 case X86::VMOVUPSZ256rm: 6985 case X86::VMOVAPSZ256rm_NOVLX: 6986 case X86::VMOVUPSZ256rm_NOVLX: 6987 case X86::VMOVAPDZ256rm: 6988 case X86::VMOVUPDZ256rm: 6989 case X86::VMOVDQU8Z256rm: 6990 case X86::VMOVDQU16Z256rm: 6991 case X86::VMOVDQA32Z256rm: 6992 case X86::VMOVDQU32Z256rm: 6993 case X86::VMOVDQA64Z256rm: 6994 case X86::VMOVDQU64Z256rm: 6995 case X86::VMOVAPSZrm: 6996 case X86::VMOVUPSZrm: 6997 case X86::VMOVAPDZrm: 6998 case X86::VMOVUPDZrm: 6999 case X86::VMOVDQU8Zrm: 7000 case X86::VMOVDQU16Zrm: 7001 case X86::VMOVDQA32Zrm: 7002 case X86::VMOVDQU32Zrm: 7003 case X86::VMOVDQA64Zrm: 7004 case X86::VMOVDQU64Zrm: 7005 case X86::KMOVBkm: 7006 case X86::KMOVWkm: 7007 case X86::KMOVDkm: 7008 case X86::KMOVQkm: 7009 break; 7010 } 7011 switch (Opc2) { 7012 default: return false; 7013 case X86::MOV8rm: 7014 case X86::MOV16rm: 7015 case X86::MOV32rm: 7016 case X86::MOV64rm: 7017 case X86::LD_Fp32m: 7018 case X86::LD_Fp64m: 7019 case X86::LD_Fp80m: 7020 case X86::MOVSSrm: 7021 case X86::MOVSSrm_alt: 7022 case X86::MOVSDrm: 7023 case X86::MOVSDrm_alt: 7024 case X86::MMX_MOVD64rm: 7025 case X86::MMX_MOVQ64rm: 7026 case X86::MOVAPSrm: 7027 case X86::MOVUPSrm: 7028 case X86::MOVAPDrm: 7029 case X86::MOVUPDrm: 7030 case X86::MOVDQArm: 7031 case X86::MOVDQUrm: 7032 // AVX load instructions 7033 case X86::VMOVSSrm: 7034 case X86::VMOVSSrm_alt: 7035 case X86::VMOVSDrm: 7036 case X86::VMOVSDrm_alt: 7037 case X86::VMOVAPSrm: 7038 case X86::VMOVUPSrm: 7039 case X86::VMOVAPDrm: 7040 case X86::VMOVUPDrm: 7041 case X86::VMOVDQArm: 7042 case X86::VMOVDQUrm: 7043 case X86::VMOVAPSYrm: 7044 case X86::VMOVUPSYrm: 7045 case X86::VMOVAPDYrm: 7046 case X86::VMOVUPDYrm: 7047 case X86::VMOVDQAYrm: 7048 case X86::VMOVDQUYrm: 7049 // AVX512 load instructions 7050 case X86::VMOVSSZrm: 7051 case X86::VMOVSSZrm_alt: 7052 case X86::VMOVSDZrm: 7053 case X86::VMOVSDZrm_alt: 7054 case X86::VMOVAPSZ128rm: 7055 case X86::VMOVUPSZ128rm: 7056 case X86::VMOVAPSZ128rm_NOVLX: 7057 case X86::VMOVUPSZ128rm_NOVLX: 7058 case X86::VMOVAPDZ128rm: 7059 case X86::VMOVUPDZ128rm: 7060 case X86::VMOVDQU8Z128rm: 7061 case X86::VMOVDQU16Z128rm: 7062 case X86::VMOVDQA32Z128rm: 7063 case X86::VMOVDQU32Z128rm: 7064 case X86::VMOVDQA64Z128rm: 7065 case X86::VMOVDQU64Z128rm: 7066 case X86::VMOVAPSZ256rm: 7067 case X86::VMOVUPSZ256rm: 7068 case X86::VMOVAPSZ256rm_NOVLX: 7069 case X86::VMOVUPSZ256rm_NOVLX: 7070 case X86::VMOVAPDZ256rm: 7071 case X86::VMOVUPDZ256rm: 7072 case X86::VMOVDQU8Z256rm: 7073 case X86::VMOVDQU16Z256rm: 7074 case X86::VMOVDQA32Z256rm: 7075 case X86::VMOVDQU32Z256rm: 7076 case X86::VMOVDQA64Z256rm: 7077 case X86::VMOVDQU64Z256rm: 7078 case X86::VMOVAPSZrm: 7079 case X86::VMOVUPSZrm: 7080 case X86::VMOVAPDZrm: 7081 case X86::VMOVUPDZrm: 7082 case X86::VMOVDQU8Zrm: 7083 case X86::VMOVDQU16Zrm: 7084 case X86::VMOVDQA32Zrm: 7085 case X86::VMOVDQU32Zrm: 7086 case X86::VMOVDQA64Zrm: 7087 case X86::VMOVDQU64Zrm: 7088 case X86::KMOVBkm: 7089 case X86::KMOVWkm: 7090 case X86::KMOVDkm: 7091 case X86::KMOVQkm: 7092 break; 7093 } 7094 7095 // Lambda to check if both the loads have the same value for an operand index. 7096 auto HasSameOp = [&](int I) { 7097 return Load1->getOperand(I) == Load2->getOperand(I); 7098 }; 7099 7100 // All operands except the displacement should match. 7101 if (!HasSameOp(X86::AddrBaseReg) || !HasSameOp(X86::AddrScaleAmt) || 7102 !HasSameOp(X86::AddrIndexReg) || !HasSameOp(X86::AddrSegmentReg)) 7103 return false; 7104 7105 // Chain Operand must be the same. 7106 if (!HasSameOp(5)) 7107 return false; 7108 7109 // Now let's examine if the displacements are constants. 7110 auto Disp1 = dyn_cast<ConstantSDNode>(Load1->getOperand(X86::AddrDisp)); 7111 auto Disp2 = dyn_cast<ConstantSDNode>(Load2->getOperand(X86::AddrDisp)); 7112 if (!Disp1 || !Disp2) 7113 return false; 7114 7115 Offset1 = Disp1->getSExtValue(); 7116 Offset2 = Disp2->getSExtValue(); 7117 return true; 7118 } 7119 7120 bool X86InstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, 7121 int64_t Offset1, int64_t Offset2, 7122 unsigned NumLoads) const { 7123 assert(Offset2 > Offset1); 7124 if ((Offset2 - Offset1) / 8 > 64) 7125 return false; 7126 7127 unsigned Opc1 = Load1->getMachineOpcode(); 7128 unsigned Opc2 = Load2->getMachineOpcode(); 7129 if (Opc1 != Opc2) 7130 return false; // FIXME: overly conservative? 7131 7132 switch (Opc1) { 7133 default: break; 7134 case X86::LD_Fp32m: 7135 case X86::LD_Fp64m: 7136 case X86::LD_Fp80m: 7137 case X86::MMX_MOVD64rm: 7138 case X86::MMX_MOVQ64rm: 7139 return false; 7140 } 7141 7142 EVT VT = Load1->getValueType(0); 7143 switch (VT.getSimpleVT().SimpleTy) { 7144 default: 7145 // XMM registers. In 64-bit mode we can be a bit more aggressive since we 7146 // have 16 of them to play with. 7147 if (Subtarget.is64Bit()) { 7148 if (NumLoads >= 3) 7149 return false; 7150 } else if (NumLoads) { 7151 return false; 7152 } 7153 break; 7154 case MVT::i8: 7155 case MVT::i16: 7156 case MVT::i32: 7157 case MVT::i64: 7158 case MVT::f32: 7159 case MVT::f64: 7160 if (NumLoads) 7161 return false; 7162 break; 7163 } 7164 7165 return true; 7166 } 7167 7168 bool X86InstrInfo::isSchedulingBoundary(const MachineInstr &MI, 7169 const MachineBasicBlock *MBB, 7170 const MachineFunction &MF) const { 7171 7172 // ENDBR instructions should not be scheduled around. 7173 unsigned Opcode = MI.getOpcode(); 7174 if (Opcode == X86::ENDBR64 || Opcode == X86::ENDBR32 || 7175 Opcode == X86::LDTILECFG) 7176 return true; 7177 7178 return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF); 7179 } 7180 7181 bool X86InstrInfo:: 7182 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 7183 assert(Cond.size() == 1 && "Invalid X86 branch condition!"); 7184 X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm()); 7185 Cond[0].setImm(GetOppositeBranchCondition(CC)); 7186 return false; 7187 } 7188 7189 bool X86InstrInfo:: 7190 isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const { 7191 // FIXME: Return false for x87 stack register classes for now. We can't 7192 // allow any loads of these registers before FpGet_ST0_80. 7193 return !(RC == &X86::CCRRegClass || RC == &X86::DFCCRRegClass || 7194 RC == &X86::RFP32RegClass || RC == &X86::RFP64RegClass || 7195 RC == &X86::RFP80RegClass); 7196 } 7197 7198 /// Return a virtual register initialized with the 7199 /// the global base register value. Output instructions required to 7200 /// initialize the register in the function entry block, if necessary. 7201 /// 7202 /// TODO: Eliminate this and move the code to X86MachineFunctionInfo. 7203 /// 7204 unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const { 7205 assert((!Subtarget.is64Bit() || 7206 MF->getTarget().getCodeModel() == CodeModel::Medium || 7207 MF->getTarget().getCodeModel() == CodeModel::Large) && 7208 "X86-64 PIC uses RIP relative addressing"); 7209 7210 X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>(); 7211 Register GlobalBaseReg = X86FI->getGlobalBaseReg(); 7212 if (GlobalBaseReg != 0) 7213 return GlobalBaseReg; 7214 7215 // Create the register. The code to initialize it is inserted 7216 // later, by the CGBR pass (below). 7217 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 7218 GlobalBaseReg = RegInfo.createVirtualRegister( 7219 Subtarget.is64Bit() ? &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass); 7220 X86FI->setGlobalBaseReg(GlobalBaseReg); 7221 return GlobalBaseReg; 7222 } 7223 7224 // These are the replaceable SSE instructions. Some of these have Int variants 7225 // that we don't include here. We don't want to replace instructions selected 7226 // by intrinsics. 7227 static const uint16_t ReplaceableInstrs[][3] = { 7228 //PackedSingle PackedDouble PackedInt 7229 { X86::MOVAPSmr, X86::MOVAPDmr, X86::MOVDQAmr }, 7230 { X86::MOVAPSrm, X86::MOVAPDrm, X86::MOVDQArm }, 7231 { X86::MOVAPSrr, X86::MOVAPDrr, X86::MOVDQArr }, 7232 { X86::MOVUPSmr, X86::MOVUPDmr, X86::MOVDQUmr }, 7233 { X86::MOVUPSrm, X86::MOVUPDrm, X86::MOVDQUrm }, 7234 { X86::MOVLPSmr, X86::MOVLPDmr, X86::MOVPQI2QImr }, 7235 { X86::MOVSDmr, X86::MOVSDmr, X86::MOVPQI2QImr }, 7236 { X86::MOVSSmr, X86::MOVSSmr, X86::MOVPDI2DImr }, 7237 { X86::MOVSDrm, X86::MOVSDrm, X86::MOVQI2PQIrm }, 7238 { X86::MOVSDrm_alt,X86::MOVSDrm_alt,X86::MOVQI2PQIrm }, 7239 { X86::MOVSSrm, X86::MOVSSrm, X86::MOVDI2PDIrm }, 7240 { X86::MOVSSrm_alt,X86::MOVSSrm_alt,X86::MOVDI2PDIrm }, 7241 { X86::MOVNTPSmr, X86::MOVNTPDmr, X86::MOVNTDQmr }, 7242 { X86::ANDNPSrm, X86::ANDNPDrm, X86::PANDNrm }, 7243 { X86::ANDNPSrr, X86::ANDNPDrr, X86::PANDNrr }, 7244 { X86::ANDPSrm, X86::ANDPDrm, X86::PANDrm }, 7245 { X86::ANDPSrr, X86::ANDPDrr, X86::PANDrr }, 7246 { X86::ORPSrm, X86::ORPDrm, X86::PORrm }, 7247 { X86::ORPSrr, X86::ORPDrr, X86::PORrr }, 7248 { X86::XORPSrm, X86::XORPDrm, X86::PXORrm }, 7249 { X86::XORPSrr, X86::XORPDrr, X86::PXORrr }, 7250 { X86::UNPCKLPDrm, X86::UNPCKLPDrm, X86::PUNPCKLQDQrm }, 7251 { X86::MOVLHPSrr, X86::UNPCKLPDrr, X86::PUNPCKLQDQrr }, 7252 { X86::UNPCKHPDrm, X86::UNPCKHPDrm, X86::PUNPCKHQDQrm }, 7253 { X86::UNPCKHPDrr, X86::UNPCKHPDrr, X86::PUNPCKHQDQrr }, 7254 { X86::UNPCKLPSrm, X86::UNPCKLPSrm, X86::PUNPCKLDQrm }, 7255 { X86::UNPCKLPSrr, X86::UNPCKLPSrr, X86::PUNPCKLDQrr }, 7256 { X86::UNPCKHPSrm, X86::UNPCKHPSrm, X86::PUNPCKHDQrm }, 7257 { X86::UNPCKHPSrr, X86::UNPCKHPSrr, X86::PUNPCKHDQrr }, 7258 { X86::EXTRACTPSmr, X86::EXTRACTPSmr, X86::PEXTRDmr }, 7259 { X86::EXTRACTPSrr, X86::EXTRACTPSrr, X86::PEXTRDrr }, 7260 // AVX 128-bit support 7261 { X86::VMOVAPSmr, X86::VMOVAPDmr, X86::VMOVDQAmr }, 7262 { X86::VMOVAPSrm, X86::VMOVAPDrm, X86::VMOVDQArm }, 7263 { X86::VMOVAPSrr, X86::VMOVAPDrr, X86::VMOVDQArr }, 7264 { X86::VMOVUPSmr, X86::VMOVUPDmr, X86::VMOVDQUmr }, 7265 { X86::VMOVUPSrm, X86::VMOVUPDrm, X86::VMOVDQUrm }, 7266 { X86::VMOVLPSmr, X86::VMOVLPDmr, X86::VMOVPQI2QImr }, 7267 { X86::VMOVSDmr, X86::VMOVSDmr, X86::VMOVPQI2QImr }, 7268 { X86::VMOVSSmr, X86::VMOVSSmr, X86::VMOVPDI2DImr }, 7269 { X86::VMOVSDrm, X86::VMOVSDrm, X86::VMOVQI2PQIrm }, 7270 { X86::VMOVSDrm_alt,X86::VMOVSDrm_alt,X86::VMOVQI2PQIrm }, 7271 { X86::VMOVSSrm, X86::VMOVSSrm, X86::VMOVDI2PDIrm }, 7272 { X86::VMOVSSrm_alt,X86::VMOVSSrm_alt,X86::VMOVDI2PDIrm }, 7273 { X86::VMOVNTPSmr, X86::VMOVNTPDmr, X86::VMOVNTDQmr }, 7274 { X86::VANDNPSrm, X86::VANDNPDrm, X86::VPANDNrm }, 7275 { X86::VANDNPSrr, X86::VANDNPDrr, X86::VPANDNrr }, 7276 { X86::VANDPSrm, X86::VANDPDrm, X86::VPANDrm }, 7277 { X86::VANDPSrr, X86::VANDPDrr, X86::VPANDrr }, 7278 { X86::VORPSrm, X86::VORPDrm, X86::VPORrm }, 7279 { X86::VORPSrr, X86::VORPDrr, X86::VPORrr }, 7280 { X86::VXORPSrm, X86::VXORPDrm, X86::VPXORrm }, 7281 { X86::VXORPSrr, X86::VXORPDrr, X86::VPXORrr }, 7282 { X86::VUNPCKLPDrm, X86::VUNPCKLPDrm, X86::VPUNPCKLQDQrm }, 7283 { X86::VMOVLHPSrr, X86::VUNPCKLPDrr, X86::VPUNPCKLQDQrr }, 7284 { X86::VUNPCKHPDrm, X86::VUNPCKHPDrm, X86::VPUNPCKHQDQrm }, 7285 { X86::VUNPCKHPDrr, X86::VUNPCKHPDrr, X86::VPUNPCKHQDQrr }, 7286 { X86::VUNPCKLPSrm, X86::VUNPCKLPSrm, X86::VPUNPCKLDQrm }, 7287 { X86::VUNPCKLPSrr, X86::VUNPCKLPSrr, X86::VPUNPCKLDQrr }, 7288 { X86::VUNPCKHPSrm, X86::VUNPCKHPSrm, X86::VPUNPCKHDQrm }, 7289 { X86::VUNPCKHPSrr, X86::VUNPCKHPSrr, X86::VPUNPCKHDQrr }, 7290 { X86::VEXTRACTPSmr, X86::VEXTRACTPSmr, X86::VPEXTRDmr }, 7291 { X86::VEXTRACTPSrr, X86::VEXTRACTPSrr, X86::VPEXTRDrr }, 7292 // AVX 256-bit support 7293 { X86::VMOVAPSYmr, X86::VMOVAPDYmr, X86::VMOVDQAYmr }, 7294 { X86::VMOVAPSYrm, X86::VMOVAPDYrm, X86::VMOVDQAYrm }, 7295 { X86::VMOVAPSYrr, X86::VMOVAPDYrr, X86::VMOVDQAYrr }, 7296 { X86::VMOVUPSYmr, X86::VMOVUPDYmr, X86::VMOVDQUYmr }, 7297 { X86::VMOVUPSYrm, X86::VMOVUPDYrm, X86::VMOVDQUYrm }, 7298 { X86::VMOVNTPSYmr, X86::VMOVNTPDYmr, X86::VMOVNTDQYmr }, 7299 { X86::VPERMPSYrm, X86::VPERMPSYrm, X86::VPERMDYrm }, 7300 { X86::VPERMPSYrr, X86::VPERMPSYrr, X86::VPERMDYrr }, 7301 { X86::VPERMPDYmi, X86::VPERMPDYmi, X86::VPERMQYmi }, 7302 { X86::VPERMPDYri, X86::VPERMPDYri, X86::VPERMQYri }, 7303 // AVX512 support 7304 { X86::VMOVLPSZ128mr, X86::VMOVLPDZ128mr, X86::VMOVPQI2QIZmr }, 7305 { X86::VMOVNTPSZ128mr, X86::VMOVNTPDZ128mr, X86::VMOVNTDQZ128mr }, 7306 { X86::VMOVNTPSZ256mr, X86::VMOVNTPDZ256mr, X86::VMOVNTDQZ256mr }, 7307 { X86::VMOVNTPSZmr, X86::VMOVNTPDZmr, X86::VMOVNTDQZmr }, 7308 { X86::VMOVSDZmr, X86::VMOVSDZmr, X86::VMOVPQI2QIZmr }, 7309 { X86::VMOVSSZmr, X86::VMOVSSZmr, X86::VMOVPDI2DIZmr }, 7310 { X86::VMOVSDZrm, X86::VMOVSDZrm, X86::VMOVQI2PQIZrm }, 7311 { X86::VMOVSDZrm_alt, X86::VMOVSDZrm_alt, X86::VMOVQI2PQIZrm }, 7312 { X86::VMOVSSZrm, X86::VMOVSSZrm, X86::VMOVDI2PDIZrm }, 7313 { X86::VMOVSSZrm_alt, X86::VMOVSSZrm_alt, X86::VMOVDI2PDIZrm }, 7314 { X86::VBROADCASTSSZ128rr,X86::VBROADCASTSSZ128rr,X86::VPBROADCASTDZ128rr }, 7315 { X86::VBROADCASTSSZ128rm,X86::VBROADCASTSSZ128rm,X86::VPBROADCASTDZ128rm }, 7316 { X86::VBROADCASTSSZ256rr,X86::VBROADCASTSSZ256rr,X86::VPBROADCASTDZ256rr }, 7317 { X86::VBROADCASTSSZ256rm,X86::VBROADCASTSSZ256rm,X86::VPBROADCASTDZ256rm }, 7318 { X86::VBROADCASTSSZrr, X86::VBROADCASTSSZrr, X86::VPBROADCASTDZrr }, 7319 { X86::VBROADCASTSSZrm, X86::VBROADCASTSSZrm, X86::VPBROADCASTDZrm }, 7320 { X86::VMOVDDUPZ128rr, X86::VMOVDDUPZ128rr, X86::VPBROADCASTQZ128rr }, 7321 { X86::VMOVDDUPZ128rm, X86::VMOVDDUPZ128rm, X86::VPBROADCASTQZ128rm }, 7322 { X86::VBROADCASTSDZ256rr,X86::VBROADCASTSDZ256rr,X86::VPBROADCASTQZ256rr }, 7323 { X86::VBROADCASTSDZ256rm,X86::VBROADCASTSDZ256rm,X86::VPBROADCASTQZ256rm }, 7324 { X86::VBROADCASTSDZrr, X86::VBROADCASTSDZrr, X86::VPBROADCASTQZrr }, 7325 { X86::VBROADCASTSDZrm, X86::VBROADCASTSDZrm, X86::VPBROADCASTQZrm }, 7326 { X86::VINSERTF32x4Zrr, X86::VINSERTF32x4Zrr, X86::VINSERTI32x4Zrr }, 7327 { X86::VINSERTF32x4Zrm, X86::VINSERTF32x4Zrm, X86::VINSERTI32x4Zrm }, 7328 { X86::VINSERTF32x8Zrr, X86::VINSERTF32x8Zrr, X86::VINSERTI32x8Zrr }, 7329 { X86::VINSERTF32x8Zrm, X86::VINSERTF32x8Zrm, X86::VINSERTI32x8Zrm }, 7330 { X86::VINSERTF64x2Zrr, X86::VINSERTF64x2Zrr, X86::VINSERTI64x2Zrr }, 7331 { X86::VINSERTF64x2Zrm, X86::VINSERTF64x2Zrm, X86::VINSERTI64x2Zrm }, 7332 { X86::VINSERTF64x4Zrr, X86::VINSERTF64x4Zrr, X86::VINSERTI64x4Zrr }, 7333 { X86::VINSERTF64x4Zrm, X86::VINSERTF64x4Zrm, X86::VINSERTI64x4Zrm }, 7334 { X86::VINSERTF32x4Z256rr,X86::VINSERTF32x4Z256rr,X86::VINSERTI32x4Z256rr }, 7335 { X86::VINSERTF32x4Z256rm,X86::VINSERTF32x4Z256rm,X86::VINSERTI32x4Z256rm }, 7336 { X86::VINSERTF64x2Z256rr,X86::VINSERTF64x2Z256rr,X86::VINSERTI64x2Z256rr }, 7337 { X86::VINSERTF64x2Z256rm,X86::VINSERTF64x2Z256rm,X86::VINSERTI64x2Z256rm }, 7338 { X86::VEXTRACTF32x4Zrr, X86::VEXTRACTF32x4Zrr, X86::VEXTRACTI32x4Zrr }, 7339 { X86::VEXTRACTF32x4Zmr, X86::VEXTRACTF32x4Zmr, X86::VEXTRACTI32x4Zmr }, 7340 { X86::VEXTRACTF32x8Zrr, X86::VEXTRACTF32x8Zrr, X86::VEXTRACTI32x8Zrr }, 7341 { X86::VEXTRACTF32x8Zmr, X86::VEXTRACTF32x8Zmr, X86::VEXTRACTI32x8Zmr }, 7342 { X86::VEXTRACTF64x2Zrr, X86::VEXTRACTF64x2Zrr, X86::VEXTRACTI64x2Zrr }, 7343 { X86::VEXTRACTF64x2Zmr, X86::VEXTRACTF64x2Zmr, X86::VEXTRACTI64x2Zmr }, 7344 { X86::VEXTRACTF64x4Zrr, X86::VEXTRACTF64x4Zrr, X86::VEXTRACTI64x4Zrr }, 7345 { X86::VEXTRACTF64x4Zmr, X86::VEXTRACTF64x4Zmr, X86::VEXTRACTI64x4Zmr }, 7346 { X86::VEXTRACTF32x4Z256rr,X86::VEXTRACTF32x4Z256rr,X86::VEXTRACTI32x4Z256rr }, 7347 { X86::VEXTRACTF32x4Z256mr,X86::VEXTRACTF32x4Z256mr,X86::VEXTRACTI32x4Z256mr }, 7348 { X86::VEXTRACTF64x2Z256rr,X86::VEXTRACTF64x2Z256rr,X86::VEXTRACTI64x2Z256rr }, 7349 { X86::VEXTRACTF64x2Z256mr,X86::VEXTRACTF64x2Z256mr,X86::VEXTRACTI64x2Z256mr }, 7350 { X86::VPERMILPSmi, X86::VPERMILPSmi, X86::VPSHUFDmi }, 7351 { X86::VPERMILPSri, X86::VPERMILPSri, X86::VPSHUFDri }, 7352 { X86::VPERMILPSZ128mi, X86::VPERMILPSZ128mi, X86::VPSHUFDZ128mi }, 7353 { X86::VPERMILPSZ128ri, X86::VPERMILPSZ128ri, X86::VPSHUFDZ128ri }, 7354 { X86::VPERMILPSZ256mi, X86::VPERMILPSZ256mi, X86::VPSHUFDZ256mi }, 7355 { X86::VPERMILPSZ256ri, X86::VPERMILPSZ256ri, X86::VPSHUFDZ256ri }, 7356 { X86::VPERMILPSZmi, X86::VPERMILPSZmi, X86::VPSHUFDZmi }, 7357 { X86::VPERMILPSZri, X86::VPERMILPSZri, X86::VPSHUFDZri }, 7358 { X86::VPERMPSZ256rm, X86::VPERMPSZ256rm, X86::VPERMDZ256rm }, 7359 { X86::VPERMPSZ256rr, X86::VPERMPSZ256rr, X86::VPERMDZ256rr }, 7360 { X86::VPERMPDZ256mi, X86::VPERMPDZ256mi, X86::VPERMQZ256mi }, 7361 { X86::VPERMPDZ256ri, X86::VPERMPDZ256ri, X86::VPERMQZ256ri }, 7362 { X86::VPERMPDZ256rm, X86::VPERMPDZ256rm, X86::VPERMQZ256rm }, 7363 { X86::VPERMPDZ256rr, X86::VPERMPDZ256rr, X86::VPERMQZ256rr }, 7364 { X86::VPERMPSZrm, X86::VPERMPSZrm, X86::VPERMDZrm }, 7365 { X86::VPERMPSZrr, X86::VPERMPSZrr, X86::VPERMDZrr }, 7366 { X86::VPERMPDZmi, X86::VPERMPDZmi, X86::VPERMQZmi }, 7367 { X86::VPERMPDZri, X86::VPERMPDZri, X86::VPERMQZri }, 7368 { X86::VPERMPDZrm, X86::VPERMPDZrm, X86::VPERMQZrm }, 7369 { X86::VPERMPDZrr, X86::VPERMPDZrr, X86::VPERMQZrr }, 7370 { X86::VUNPCKLPDZ256rm, X86::VUNPCKLPDZ256rm, X86::VPUNPCKLQDQZ256rm }, 7371 { X86::VUNPCKLPDZ256rr, X86::VUNPCKLPDZ256rr, X86::VPUNPCKLQDQZ256rr }, 7372 { X86::VUNPCKHPDZ256rm, X86::VUNPCKHPDZ256rm, X86::VPUNPCKHQDQZ256rm }, 7373 { X86::VUNPCKHPDZ256rr, X86::VUNPCKHPDZ256rr, X86::VPUNPCKHQDQZ256rr }, 7374 { X86::VUNPCKLPSZ256rm, X86::VUNPCKLPSZ256rm, X86::VPUNPCKLDQZ256rm }, 7375 { X86::VUNPCKLPSZ256rr, X86::VUNPCKLPSZ256rr, X86::VPUNPCKLDQZ256rr }, 7376 { X86::VUNPCKHPSZ256rm, X86::VUNPCKHPSZ256rm, X86::VPUNPCKHDQZ256rm }, 7377 { X86::VUNPCKHPSZ256rr, X86::VUNPCKHPSZ256rr, X86::VPUNPCKHDQZ256rr }, 7378 { X86::VUNPCKLPDZ128rm, X86::VUNPCKLPDZ128rm, X86::VPUNPCKLQDQZ128rm }, 7379 { X86::VMOVLHPSZrr, X86::VUNPCKLPDZ128rr, X86::VPUNPCKLQDQZ128rr }, 7380 { X86::VUNPCKHPDZ128rm, X86::VUNPCKHPDZ128rm, X86::VPUNPCKHQDQZ128rm }, 7381 { X86::VUNPCKHPDZ128rr, X86::VUNPCKHPDZ128rr, X86::VPUNPCKHQDQZ128rr }, 7382 { X86::VUNPCKLPSZ128rm, X86::VUNPCKLPSZ128rm, X86::VPUNPCKLDQZ128rm }, 7383 { X86::VUNPCKLPSZ128rr, X86::VUNPCKLPSZ128rr, X86::VPUNPCKLDQZ128rr }, 7384 { X86::VUNPCKHPSZ128rm, X86::VUNPCKHPSZ128rm, X86::VPUNPCKHDQZ128rm }, 7385 { X86::VUNPCKHPSZ128rr, X86::VUNPCKHPSZ128rr, X86::VPUNPCKHDQZ128rr }, 7386 { X86::VUNPCKLPDZrm, X86::VUNPCKLPDZrm, X86::VPUNPCKLQDQZrm }, 7387 { X86::VUNPCKLPDZrr, X86::VUNPCKLPDZrr, X86::VPUNPCKLQDQZrr }, 7388 { X86::VUNPCKHPDZrm, X86::VUNPCKHPDZrm, X86::VPUNPCKHQDQZrm }, 7389 { X86::VUNPCKHPDZrr, X86::VUNPCKHPDZrr, X86::VPUNPCKHQDQZrr }, 7390 { X86::VUNPCKLPSZrm, X86::VUNPCKLPSZrm, X86::VPUNPCKLDQZrm }, 7391 { X86::VUNPCKLPSZrr, X86::VUNPCKLPSZrr, X86::VPUNPCKLDQZrr }, 7392 { X86::VUNPCKHPSZrm, X86::VUNPCKHPSZrm, X86::VPUNPCKHDQZrm }, 7393 { X86::VUNPCKHPSZrr, X86::VUNPCKHPSZrr, X86::VPUNPCKHDQZrr }, 7394 { X86::VEXTRACTPSZmr, X86::VEXTRACTPSZmr, X86::VPEXTRDZmr }, 7395 { X86::VEXTRACTPSZrr, X86::VEXTRACTPSZrr, X86::VPEXTRDZrr }, 7396 }; 7397 7398 static const uint16_t ReplaceableInstrsAVX2[][3] = { 7399 //PackedSingle PackedDouble PackedInt 7400 { X86::VANDNPSYrm, X86::VANDNPDYrm, X86::VPANDNYrm }, 7401 { X86::VANDNPSYrr, X86::VANDNPDYrr, X86::VPANDNYrr }, 7402 { X86::VANDPSYrm, X86::VANDPDYrm, X86::VPANDYrm }, 7403 { X86::VANDPSYrr, X86::VANDPDYrr, X86::VPANDYrr }, 7404 { X86::VORPSYrm, X86::VORPDYrm, X86::VPORYrm }, 7405 { X86::VORPSYrr, X86::VORPDYrr, X86::VPORYrr }, 7406 { X86::VXORPSYrm, X86::VXORPDYrm, X86::VPXORYrm }, 7407 { X86::VXORPSYrr, X86::VXORPDYrr, X86::VPXORYrr }, 7408 { X86::VPERM2F128rm, X86::VPERM2F128rm, X86::VPERM2I128rm }, 7409 { X86::VPERM2F128rr, X86::VPERM2F128rr, X86::VPERM2I128rr }, 7410 { X86::VBROADCASTSSrm, X86::VBROADCASTSSrm, X86::VPBROADCASTDrm}, 7411 { X86::VBROADCASTSSrr, X86::VBROADCASTSSrr, X86::VPBROADCASTDrr}, 7412 { X86::VMOVDDUPrm, X86::VMOVDDUPrm, X86::VPBROADCASTQrm}, 7413 { X86::VMOVDDUPrr, X86::VMOVDDUPrr, X86::VPBROADCASTQrr}, 7414 { X86::VBROADCASTSSYrr, X86::VBROADCASTSSYrr, X86::VPBROADCASTDYrr}, 7415 { X86::VBROADCASTSSYrm, X86::VBROADCASTSSYrm, X86::VPBROADCASTDYrm}, 7416 { X86::VBROADCASTSDYrr, X86::VBROADCASTSDYrr, X86::VPBROADCASTQYrr}, 7417 { X86::VBROADCASTSDYrm, X86::VBROADCASTSDYrm, X86::VPBROADCASTQYrm}, 7418 { X86::VBROADCASTF128, X86::VBROADCASTF128, X86::VBROADCASTI128 }, 7419 { X86::VBLENDPSYrri, X86::VBLENDPSYrri, X86::VPBLENDDYrri }, 7420 { X86::VBLENDPSYrmi, X86::VBLENDPSYrmi, X86::VPBLENDDYrmi }, 7421 { X86::VPERMILPSYmi, X86::VPERMILPSYmi, X86::VPSHUFDYmi }, 7422 { X86::VPERMILPSYri, X86::VPERMILPSYri, X86::VPSHUFDYri }, 7423 { X86::VUNPCKLPDYrm, X86::VUNPCKLPDYrm, X86::VPUNPCKLQDQYrm }, 7424 { X86::VUNPCKLPDYrr, X86::VUNPCKLPDYrr, X86::VPUNPCKLQDQYrr }, 7425 { X86::VUNPCKHPDYrm, X86::VUNPCKHPDYrm, X86::VPUNPCKHQDQYrm }, 7426 { X86::VUNPCKHPDYrr, X86::VUNPCKHPDYrr, X86::VPUNPCKHQDQYrr }, 7427 { X86::VUNPCKLPSYrm, X86::VUNPCKLPSYrm, X86::VPUNPCKLDQYrm }, 7428 { X86::VUNPCKLPSYrr, X86::VUNPCKLPSYrr, X86::VPUNPCKLDQYrr }, 7429 { X86::VUNPCKHPSYrm, X86::VUNPCKHPSYrm, X86::VPUNPCKHDQYrm }, 7430 { X86::VUNPCKHPSYrr, X86::VUNPCKHPSYrr, X86::VPUNPCKHDQYrr }, 7431 }; 7432 7433 static const uint16_t ReplaceableInstrsFP[][3] = { 7434 //PackedSingle PackedDouble 7435 { X86::MOVLPSrm, X86::MOVLPDrm, X86::INSTRUCTION_LIST_END }, 7436 { X86::MOVHPSrm, X86::MOVHPDrm, X86::INSTRUCTION_LIST_END }, 7437 { X86::MOVHPSmr, X86::MOVHPDmr, X86::INSTRUCTION_LIST_END }, 7438 { X86::VMOVLPSrm, X86::VMOVLPDrm, X86::INSTRUCTION_LIST_END }, 7439 { X86::VMOVHPSrm, X86::VMOVHPDrm, X86::INSTRUCTION_LIST_END }, 7440 { X86::VMOVHPSmr, X86::VMOVHPDmr, X86::INSTRUCTION_LIST_END }, 7441 { X86::VMOVLPSZ128rm, X86::VMOVLPDZ128rm, X86::INSTRUCTION_LIST_END }, 7442 { X86::VMOVHPSZ128rm, X86::VMOVHPDZ128rm, X86::INSTRUCTION_LIST_END }, 7443 { X86::VMOVHPSZ128mr, X86::VMOVHPDZ128mr, X86::INSTRUCTION_LIST_END }, 7444 }; 7445 7446 static const uint16_t ReplaceableInstrsAVX2InsertExtract[][3] = { 7447 //PackedSingle PackedDouble PackedInt 7448 { X86::VEXTRACTF128mr, X86::VEXTRACTF128mr, X86::VEXTRACTI128mr }, 7449 { X86::VEXTRACTF128rr, X86::VEXTRACTF128rr, X86::VEXTRACTI128rr }, 7450 { X86::VINSERTF128rm, X86::VINSERTF128rm, X86::VINSERTI128rm }, 7451 { X86::VINSERTF128rr, X86::VINSERTF128rr, X86::VINSERTI128rr }, 7452 }; 7453 7454 static const uint16_t ReplaceableInstrsAVX512[][4] = { 7455 // Two integer columns for 64-bit and 32-bit elements. 7456 //PackedSingle PackedDouble PackedInt PackedInt 7457 { X86::VMOVAPSZ128mr, X86::VMOVAPDZ128mr, X86::VMOVDQA64Z128mr, X86::VMOVDQA32Z128mr }, 7458 { X86::VMOVAPSZ128rm, X86::VMOVAPDZ128rm, X86::VMOVDQA64Z128rm, X86::VMOVDQA32Z128rm }, 7459 { X86::VMOVAPSZ128rr, X86::VMOVAPDZ128rr, X86::VMOVDQA64Z128rr, X86::VMOVDQA32Z128rr }, 7460 { X86::VMOVUPSZ128mr, X86::VMOVUPDZ128mr, X86::VMOVDQU64Z128mr, X86::VMOVDQU32Z128mr }, 7461 { X86::VMOVUPSZ128rm, X86::VMOVUPDZ128rm, X86::VMOVDQU64Z128rm, X86::VMOVDQU32Z128rm }, 7462 { X86::VMOVAPSZ256mr, X86::VMOVAPDZ256mr, X86::VMOVDQA64Z256mr, X86::VMOVDQA32Z256mr }, 7463 { X86::VMOVAPSZ256rm, X86::VMOVAPDZ256rm, X86::VMOVDQA64Z256rm, X86::VMOVDQA32Z256rm }, 7464 { X86::VMOVAPSZ256rr, X86::VMOVAPDZ256rr, X86::VMOVDQA64Z256rr, X86::VMOVDQA32Z256rr }, 7465 { X86::VMOVUPSZ256mr, X86::VMOVUPDZ256mr, X86::VMOVDQU64Z256mr, X86::VMOVDQU32Z256mr }, 7466 { X86::VMOVUPSZ256rm, X86::VMOVUPDZ256rm, X86::VMOVDQU64Z256rm, X86::VMOVDQU32Z256rm }, 7467 { X86::VMOVAPSZmr, X86::VMOVAPDZmr, X86::VMOVDQA64Zmr, X86::VMOVDQA32Zmr }, 7468 { X86::VMOVAPSZrm, X86::VMOVAPDZrm, X86::VMOVDQA64Zrm, X86::VMOVDQA32Zrm }, 7469 { X86::VMOVAPSZrr, X86::VMOVAPDZrr, X86::VMOVDQA64Zrr, X86::VMOVDQA32Zrr }, 7470 { X86::VMOVUPSZmr, X86::VMOVUPDZmr, X86::VMOVDQU64Zmr, X86::VMOVDQU32Zmr }, 7471 { X86::VMOVUPSZrm, X86::VMOVUPDZrm, X86::VMOVDQU64Zrm, X86::VMOVDQU32Zrm }, 7472 }; 7473 7474 static const uint16_t ReplaceableInstrsAVX512DQ[][4] = { 7475 // Two integer columns for 64-bit and 32-bit elements. 7476 //PackedSingle PackedDouble PackedInt PackedInt 7477 { X86::VANDNPSZ128rm, X86::VANDNPDZ128rm, X86::VPANDNQZ128rm, X86::VPANDNDZ128rm }, 7478 { X86::VANDNPSZ128rr, X86::VANDNPDZ128rr, X86::VPANDNQZ128rr, X86::VPANDNDZ128rr }, 7479 { X86::VANDPSZ128rm, X86::VANDPDZ128rm, X86::VPANDQZ128rm, X86::VPANDDZ128rm }, 7480 { X86::VANDPSZ128rr, X86::VANDPDZ128rr, X86::VPANDQZ128rr, X86::VPANDDZ128rr }, 7481 { X86::VORPSZ128rm, X86::VORPDZ128rm, X86::VPORQZ128rm, X86::VPORDZ128rm }, 7482 { X86::VORPSZ128rr, X86::VORPDZ128rr, X86::VPORQZ128rr, X86::VPORDZ128rr }, 7483 { X86::VXORPSZ128rm, X86::VXORPDZ128rm, X86::VPXORQZ128rm, X86::VPXORDZ128rm }, 7484 { X86::VXORPSZ128rr, X86::VXORPDZ128rr, X86::VPXORQZ128rr, X86::VPXORDZ128rr }, 7485 { X86::VANDNPSZ256rm, X86::VANDNPDZ256rm, X86::VPANDNQZ256rm, X86::VPANDNDZ256rm }, 7486 { X86::VANDNPSZ256rr, X86::VANDNPDZ256rr, X86::VPANDNQZ256rr, X86::VPANDNDZ256rr }, 7487 { X86::VANDPSZ256rm, X86::VANDPDZ256rm, X86::VPANDQZ256rm, X86::VPANDDZ256rm }, 7488 { X86::VANDPSZ256rr, X86::VANDPDZ256rr, X86::VPANDQZ256rr, X86::VPANDDZ256rr }, 7489 { X86::VORPSZ256rm, X86::VORPDZ256rm, X86::VPORQZ256rm, X86::VPORDZ256rm }, 7490 { X86::VORPSZ256rr, X86::VORPDZ256rr, X86::VPORQZ256rr, X86::VPORDZ256rr }, 7491 { X86::VXORPSZ256rm, X86::VXORPDZ256rm, X86::VPXORQZ256rm, X86::VPXORDZ256rm }, 7492 { X86::VXORPSZ256rr, X86::VXORPDZ256rr, X86::VPXORQZ256rr, X86::VPXORDZ256rr }, 7493 { X86::VANDNPSZrm, X86::VANDNPDZrm, X86::VPANDNQZrm, X86::VPANDNDZrm }, 7494 { X86::VANDNPSZrr, X86::VANDNPDZrr, X86::VPANDNQZrr, X86::VPANDNDZrr }, 7495 { X86::VANDPSZrm, X86::VANDPDZrm, X86::VPANDQZrm, X86::VPANDDZrm }, 7496 { X86::VANDPSZrr, X86::VANDPDZrr, X86::VPANDQZrr, X86::VPANDDZrr }, 7497 { X86::VORPSZrm, X86::VORPDZrm, X86::VPORQZrm, X86::VPORDZrm }, 7498 { X86::VORPSZrr, X86::VORPDZrr, X86::VPORQZrr, X86::VPORDZrr }, 7499 { X86::VXORPSZrm, X86::VXORPDZrm, X86::VPXORQZrm, X86::VPXORDZrm }, 7500 { X86::VXORPSZrr, X86::VXORPDZrr, X86::VPXORQZrr, X86::VPXORDZrr }, 7501 }; 7502 7503 static const uint16_t ReplaceableInstrsAVX512DQMasked[][4] = { 7504 // Two integer columns for 64-bit and 32-bit elements. 7505 //PackedSingle PackedDouble 7506 //PackedInt PackedInt 7507 { X86::VANDNPSZ128rmk, X86::VANDNPDZ128rmk, 7508 X86::VPANDNQZ128rmk, X86::VPANDNDZ128rmk }, 7509 { X86::VANDNPSZ128rmkz, X86::VANDNPDZ128rmkz, 7510 X86::VPANDNQZ128rmkz, X86::VPANDNDZ128rmkz }, 7511 { X86::VANDNPSZ128rrk, X86::VANDNPDZ128rrk, 7512 X86::VPANDNQZ128rrk, X86::VPANDNDZ128rrk }, 7513 { X86::VANDNPSZ128rrkz, X86::VANDNPDZ128rrkz, 7514 X86::VPANDNQZ128rrkz, X86::VPANDNDZ128rrkz }, 7515 { X86::VANDPSZ128rmk, X86::VANDPDZ128rmk, 7516 X86::VPANDQZ128rmk, X86::VPANDDZ128rmk }, 7517 { X86::VANDPSZ128rmkz, X86::VANDPDZ128rmkz, 7518 X86::VPANDQZ128rmkz, X86::VPANDDZ128rmkz }, 7519 { X86::VANDPSZ128rrk, X86::VANDPDZ128rrk, 7520 X86::VPANDQZ128rrk, X86::VPANDDZ128rrk }, 7521 { X86::VANDPSZ128rrkz, X86::VANDPDZ128rrkz, 7522 X86::VPANDQZ128rrkz, X86::VPANDDZ128rrkz }, 7523 { X86::VORPSZ128rmk, X86::VORPDZ128rmk, 7524 X86::VPORQZ128rmk, X86::VPORDZ128rmk }, 7525 { X86::VORPSZ128rmkz, X86::VORPDZ128rmkz, 7526 X86::VPORQZ128rmkz, X86::VPORDZ128rmkz }, 7527 { X86::VORPSZ128rrk, X86::VORPDZ128rrk, 7528 X86::VPORQZ128rrk, X86::VPORDZ128rrk }, 7529 { X86::VORPSZ128rrkz, X86::VORPDZ128rrkz, 7530 X86::VPORQZ128rrkz, X86::VPORDZ128rrkz }, 7531 { X86::VXORPSZ128rmk, X86::VXORPDZ128rmk, 7532 X86::VPXORQZ128rmk, X86::VPXORDZ128rmk }, 7533 { X86::VXORPSZ128rmkz, X86::VXORPDZ128rmkz, 7534 X86::VPXORQZ128rmkz, X86::VPXORDZ128rmkz }, 7535 { X86::VXORPSZ128rrk, X86::VXORPDZ128rrk, 7536 X86::VPXORQZ128rrk, X86::VPXORDZ128rrk }, 7537 { X86::VXORPSZ128rrkz, X86::VXORPDZ128rrkz, 7538 X86::VPXORQZ128rrkz, X86::VPXORDZ128rrkz }, 7539 { X86::VANDNPSZ256rmk, X86::VANDNPDZ256rmk, 7540 X86::VPANDNQZ256rmk, X86::VPANDNDZ256rmk }, 7541 { X86::VANDNPSZ256rmkz, X86::VANDNPDZ256rmkz, 7542 X86::VPANDNQZ256rmkz, X86::VPANDNDZ256rmkz }, 7543 { X86::VANDNPSZ256rrk, X86::VANDNPDZ256rrk, 7544 X86::VPANDNQZ256rrk, X86::VPANDNDZ256rrk }, 7545 { X86::VANDNPSZ256rrkz, X86::VANDNPDZ256rrkz, 7546 X86::VPANDNQZ256rrkz, X86::VPANDNDZ256rrkz }, 7547 { X86::VANDPSZ256rmk, X86::VANDPDZ256rmk, 7548 X86::VPANDQZ256rmk, X86::VPANDDZ256rmk }, 7549 { X86::VANDPSZ256rmkz, X86::VANDPDZ256rmkz, 7550 X86::VPANDQZ256rmkz, X86::VPANDDZ256rmkz }, 7551 { X86::VANDPSZ256rrk, X86::VANDPDZ256rrk, 7552 X86::VPANDQZ256rrk, X86::VPANDDZ256rrk }, 7553 { X86::VANDPSZ256rrkz, X86::VANDPDZ256rrkz, 7554 X86::VPANDQZ256rrkz, X86::VPANDDZ256rrkz }, 7555 { X86::VORPSZ256rmk, X86::VORPDZ256rmk, 7556 X86::VPORQZ256rmk, X86::VPORDZ256rmk }, 7557 { X86::VORPSZ256rmkz, X86::VORPDZ256rmkz, 7558 X86::VPORQZ256rmkz, X86::VPORDZ256rmkz }, 7559 { X86::VORPSZ256rrk, X86::VORPDZ256rrk, 7560 X86::VPORQZ256rrk, X86::VPORDZ256rrk }, 7561 { X86::VORPSZ256rrkz, X86::VORPDZ256rrkz, 7562 X86::VPORQZ256rrkz, X86::VPORDZ256rrkz }, 7563 { X86::VXORPSZ256rmk, X86::VXORPDZ256rmk, 7564 X86::VPXORQZ256rmk, X86::VPXORDZ256rmk }, 7565 { X86::VXORPSZ256rmkz, X86::VXORPDZ256rmkz, 7566 X86::VPXORQZ256rmkz, X86::VPXORDZ256rmkz }, 7567 { X86::VXORPSZ256rrk, X86::VXORPDZ256rrk, 7568 X86::VPXORQZ256rrk, X86::VPXORDZ256rrk }, 7569 { X86::VXORPSZ256rrkz, X86::VXORPDZ256rrkz, 7570 X86::VPXORQZ256rrkz, X86::VPXORDZ256rrkz }, 7571 { X86::VANDNPSZrmk, X86::VANDNPDZrmk, 7572 X86::VPANDNQZrmk, X86::VPANDNDZrmk }, 7573 { X86::VANDNPSZrmkz, X86::VANDNPDZrmkz, 7574 X86::VPANDNQZrmkz, X86::VPANDNDZrmkz }, 7575 { X86::VANDNPSZrrk, X86::VANDNPDZrrk, 7576 X86::VPANDNQZrrk, X86::VPANDNDZrrk }, 7577 { X86::VANDNPSZrrkz, X86::VANDNPDZrrkz, 7578 X86::VPANDNQZrrkz, X86::VPANDNDZrrkz }, 7579 { X86::VANDPSZrmk, X86::VANDPDZrmk, 7580 X86::VPANDQZrmk, X86::VPANDDZrmk }, 7581 { X86::VANDPSZrmkz, X86::VANDPDZrmkz, 7582 X86::VPANDQZrmkz, X86::VPANDDZrmkz }, 7583 { X86::VANDPSZrrk, X86::VANDPDZrrk, 7584 X86::VPANDQZrrk, X86::VPANDDZrrk }, 7585 { X86::VANDPSZrrkz, X86::VANDPDZrrkz, 7586 X86::VPANDQZrrkz, X86::VPANDDZrrkz }, 7587 { X86::VORPSZrmk, X86::VORPDZrmk, 7588 X86::VPORQZrmk, X86::VPORDZrmk }, 7589 { X86::VORPSZrmkz, X86::VORPDZrmkz, 7590 X86::VPORQZrmkz, X86::VPORDZrmkz }, 7591 { X86::VORPSZrrk, X86::VORPDZrrk, 7592 X86::VPORQZrrk, X86::VPORDZrrk }, 7593 { X86::VORPSZrrkz, X86::VORPDZrrkz, 7594 X86::VPORQZrrkz, X86::VPORDZrrkz }, 7595 { X86::VXORPSZrmk, X86::VXORPDZrmk, 7596 X86::VPXORQZrmk, X86::VPXORDZrmk }, 7597 { X86::VXORPSZrmkz, X86::VXORPDZrmkz, 7598 X86::VPXORQZrmkz, X86::VPXORDZrmkz }, 7599 { X86::VXORPSZrrk, X86::VXORPDZrrk, 7600 X86::VPXORQZrrk, X86::VPXORDZrrk }, 7601 { X86::VXORPSZrrkz, X86::VXORPDZrrkz, 7602 X86::VPXORQZrrkz, X86::VPXORDZrrkz }, 7603 // Broadcast loads can be handled the same as masked operations to avoid 7604 // changing element size. 7605 { X86::VANDNPSZ128rmb, X86::VANDNPDZ128rmb, 7606 X86::VPANDNQZ128rmb, X86::VPANDNDZ128rmb }, 7607 { X86::VANDPSZ128rmb, X86::VANDPDZ128rmb, 7608 X86::VPANDQZ128rmb, X86::VPANDDZ128rmb }, 7609 { X86::VORPSZ128rmb, X86::VORPDZ128rmb, 7610 X86::VPORQZ128rmb, X86::VPORDZ128rmb }, 7611 { X86::VXORPSZ128rmb, X86::VXORPDZ128rmb, 7612 X86::VPXORQZ128rmb, X86::VPXORDZ128rmb }, 7613 { X86::VANDNPSZ256rmb, X86::VANDNPDZ256rmb, 7614 X86::VPANDNQZ256rmb, X86::VPANDNDZ256rmb }, 7615 { X86::VANDPSZ256rmb, X86::VANDPDZ256rmb, 7616 X86::VPANDQZ256rmb, X86::VPANDDZ256rmb }, 7617 { X86::VORPSZ256rmb, X86::VORPDZ256rmb, 7618 X86::VPORQZ256rmb, X86::VPORDZ256rmb }, 7619 { X86::VXORPSZ256rmb, X86::VXORPDZ256rmb, 7620 X86::VPXORQZ256rmb, X86::VPXORDZ256rmb }, 7621 { X86::VANDNPSZrmb, X86::VANDNPDZrmb, 7622 X86::VPANDNQZrmb, X86::VPANDNDZrmb }, 7623 { X86::VANDPSZrmb, X86::VANDPDZrmb, 7624 X86::VPANDQZrmb, X86::VPANDDZrmb }, 7625 { X86::VANDPSZrmb, X86::VANDPDZrmb, 7626 X86::VPANDQZrmb, X86::VPANDDZrmb }, 7627 { X86::VORPSZrmb, X86::VORPDZrmb, 7628 X86::VPORQZrmb, X86::VPORDZrmb }, 7629 { X86::VXORPSZrmb, X86::VXORPDZrmb, 7630 X86::VPXORQZrmb, X86::VPXORDZrmb }, 7631 { X86::VANDNPSZ128rmbk, X86::VANDNPDZ128rmbk, 7632 X86::VPANDNQZ128rmbk, X86::VPANDNDZ128rmbk }, 7633 { X86::VANDPSZ128rmbk, X86::VANDPDZ128rmbk, 7634 X86::VPANDQZ128rmbk, X86::VPANDDZ128rmbk }, 7635 { X86::VORPSZ128rmbk, X86::VORPDZ128rmbk, 7636 X86::VPORQZ128rmbk, X86::VPORDZ128rmbk }, 7637 { X86::VXORPSZ128rmbk, X86::VXORPDZ128rmbk, 7638 X86::VPXORQZ128rmbk, X86::VPXORDZ128rmbk }, 7639 { X86::VANDNPSZ256rmbk, X86::VANDNPDZ256rmbk, 7640 X86::VPANDNQZ256rmbk, X86::VPANDNDZ256rmbk }, 7641 { X86::VANDPSZ256rmbk, X86::VANDPDZ256rmbk, 7642 X86::VPANDQZ256rmbk, X86::VPANDDZ256rmbk }, 7643 { X86::VORPSZ256rmbk, X86::VORPDZ256rmbk, 7644 X86::VPORQZ256rmbk, X86::VPORDZ256rmbk }, 7645 { X86::VXORPSZ256rmbk, X86::VXORPDZ256rmbk, 7646 X86::VPXORQZ256rmbk, X86::VPXORDZ256rmbk }, 7647 { X86::VANDNPSZrmbk, X86::VANDNPDZrmbk, 7648 X86::VPANDNQZrmbk, X86::VPANDNDZrmbk }, 7649 { X86::VANDPSZrmbk, X86::VANDPDZrmbk, 7650 X86::VPANDQZrmbk, X86::VPANDDZrmbk }, 7651 { X86::VANDPSZrmbk, X86::VANDPDZrmbk, 7652 X86::VPANDQZrmbk, X86::VPANDDZrmbk }, 7653 { X86::VORPSZrmbk, X86::VORPDZrmbk, 7654 X86::VPORQZrmbk, X86::VPORDZrmbk }, 7655 { X86::VXORPSZrmbk, X86::VXORPDZrmbk, 7656 X86::VPXORQZrmbk, X86::VPXORDZrmbk }, 7657 { X86::VANDNPSZ128rmbkz,X86::VANDNPDZ128rmbkz, 7658 X86::VPANDNQZ128rmbkz,X86::VPANDNDZ128rmbkz}, 7659 { X86::VANDPSZ128rmbkz, X86::VANDPDZ128rmbkz, 7660 X86::VPANDQZ128rmbkz, X86::VPANDDZ128rmbkz }, 7661 { X86::VORPSZ128rmbkz, X86::VORPDZ128rmbkz, 7662 X86::VPORQZ128rmbkz, X86::VPORDZ128rmbkz }, 7663 { X86::VXORPSZ128rmbkz, X86::VXORPDZ128rmbkz, 7664 X86::VPXORQZ128rmbkz, X86::VPXORDZ128rmbkz }, 7665 { X86::VANDNPSZ256rmbkz,X86::VANDNPDZ256rmbkz, 7666 X86::VPANDNQZ256rmbkz,X86::VPANDNDZ256rmbkz}, 7667 { X86::VANDPSZ256rmbkz, X86::VANDPDZ256rmbkz, 7668 X86::VPANDQZ256rmbkz, X86::VPANDDZ256rmbkz }, 7669 { X86::VORPSZ256rmbkz, X86::VORPDZ256rmbkz, 7670 X86::VPORQZ256rmbkz, X86::VPORDZ256rmbkz }, 7671 { X86::VXORPSZ256rmbkz, X86::VXORPDZ256rmbkz, 7672 X86::VPXORQZ256rmbkz, X86::VPXORDZ256rmbkz }, 7673 { X86::VANDNPSZrmbkz, X86::VANDNPDZrmbkz, 7674 X86::VPANDNQZrmbkz, X86::VPANDNDZrmbkz }, 7675 { X86::VANDPSZrmbkz, X86::VANDPDZrmbkz, 7676 X86::VPANDQZrmbkz, X86::VPANDDZrmbkz }, 7677 { X86::VANDPSZrmbkz, X86::VANDPDZrmbkz, 7678 X86::VPANDQZrmbkz, X86::VPANDDZrmbkz }, 7679 { X86::VORPSZrmbkz, X86::VORPDZrmbkz, 7680 X86::VPORQZrmbkz, X86::VPORDZrmbkz }, 7681 { X86::VXORPSZrmbkz, X86::VXORPDZrmbkz, 7682 X86::VPXORQZrmbkz, X86::VPXORDZrmbkz }, 7683 }; 7684 7685 // NOTE: These should only be used by the custom domain methods. 7686 static const uint16_t ReplaceableBlendInstrs[][3] = { 7687 //PackedSingle PackedDouble PackedInt 7688 { X86::BLENDPSrmi, X86::BLENDPDrmi, X86::PBLENDWrmi }, 7689 { X86::BLENDPSrri, X86::BLENDPDrri, X86::PBLENDWrri }, 7690 { X86::VBLENDPSrmi, X86::VBLENDPDrmi, X86::VPBLENDWrmi }, 7691 { X86::VBLENDPSrri, X86::VBLENDPDrri, X86::VPBLENDWrri }, 7692 { X86::VBLENDPSYrmi, X86::VBLENDPDYrmi, X86::VPBLENDWYrmi }, 7693 { X86::VBLENDPSYrri, X86::VBLENDPDYrri, X86::VPBLENDWYrri }, 7694 }; 7695 static const uint16_t ReplaceableBlendAVX2Instrs[][3] = { 7696 //PackedSingle PackedDouble PackedInt 7697 { X86::VBLENDPSrmi, X86::VBLENDPDrmi, X86::VPBLENDDrmi }, 7698 { X86::VBLENDPSrri, X86::VBLENDPDrri, X86::VPBLENDDrri }, 7699 { X86::VBLENDPSYrmi, X86::VBLENDPDYrmi, X86::VPBLENDDYrmi }, 7700 { X86::VBLENDPSYrri, X86::VBLENDPDYrri, X86::VPBLENDDYrri }, 7701 }; 7702 7703 // Special table for changing EVEX logic instructions to VEX. 7704 // TODO: Should we run EVEX->VEX earlier? 7705 static const uint16_t ReplaceableCustomAVX512LogicInstrs[][4] = { 7706 // Two integer columns for 64-bit and 32-bit elements. 7707 //PackedSingle PackedDouble PackedInt PackedInt 7708 { X86::VANDNPSrm, X86::VANDNPDrm, X86::VPANDNQZ128rm, X86::VPANDNDZ128rm }, 7709 { X86::VANDNPSrr, X86::VANDNPDrr, X86::VPANDNQZ128rr, X86::VPANDNDZ128rr }, 7710 { X86::VANDPSrm, X86::VANDPDrm, X86::VPANDQZ128rm, X86::VPANDDZ128rm }, 7711 { X86::VANDPSrr, X86::VANDPDrr, X86::VPANDQZ128rr, X86::VPANDDZ128rr }, 7712 { X86::VORPSrm, X86::VORPDrm, X86::VPORQZ128rm, X86::VPORDZ128rm }, 7713 { X86::VORPSrr, X86::VORPDrr, X86::VPORQZ128rr, X86::VPORDZ128rr }, 7714 { X86::VXORPSrm, X86::VXORPDrm, X86::VPXORQZ128rm, X86::VPXORDZ128rm }, 7715 { X86::VXORPSrr, X86::VXORPDrr, X86::VPXORQZ128rr, X86::VPXORDZ128rr }, 7716 { X86::VANDNPSYrm, X86::VANDNPDYrm, X86::VPANDNQZ256rm, X86::VPANDNDZ256rm }, 7717 { X86::VANDNPSYrr, X86::VANDNPDYrr, X86::VPANDNQZ256rr, X86::VPANDNDZ256rr }, 7718 { X86::VANDPSYrm, X86::VANDPDYrm, X86::VPANDQZ256rm, X86::VPANDDZ256rm }, 7719 { X86::VANDPSYrr, X86::VANDPDYrr, X86::VPANDQZ256rr, X86::VPANDDZ256rr }, 7720 { X86::VORPSYrm, X86::VORPDYrm, X86::VPORQZ256rm, X86::VPORDZ256rm }, 7721 { X86::VORPSYrr, X86::VORPDYrr, X86::VPORQZ256rr, X86::VPORDZ256rr }, 7722 { X86::VXORPSYrm, X86::VXORPDYrm, X86::VPXORQZ256rm, X86::VPXORDZ256rm }, 7723 { X86::VXORPSYrr, X86::VXORPDYrr, X86::VPXORQZ256rr, X86::VPXORDZ256rr }, 7724 }; 7725 7726 // FIXME: Some shuffle and unpack instructions have equivalents in different 7727 // domains, but they require a bit more work than just switching opcodes. 7728 7729 static const uint16_t *lookup(unsigned opcode, unsigned domain, 7730 ArrayRef<uint16_t[3]> Table) { 7731 for (const uint16_t (&Row)[3] : Table) 7732 if (Row[domain-1] == opcode) 7733 return Row; 7734 return nullptr; 7735 } 7736 7737 static const uint16_t *lookupAVX512(unsigned opcode, unsigned domain, 7738 ArrayRef<uint16_t[4]> Table) { 7739 // If this is the integer domain make sure to check both integer columns. 7740 for (const uint16_t (&Row)[4] : Table) 7741 if (Row[domain-1] == opcode || (domain == 3 && Row[3] == opcode)) 7742 return Row; 7743 return nullptr; 7744 } 7745 7746 // Helper to attempt to widen/narrow blend masks. 7747 static bool AdjustBlendMask(unsigned OldMask, unsigned OldWidth, 7748 unsigned NewWidth, unsigned *pNewMask = nullptr) { 7749 assert(((OldWidth % NewWidth) == 0 || (NewWidth % OldWidth) == 0) && 7750 "Illegal blend mask scale"); 7751 unsigned NewMask = 0; 7752 7753 if ((OldWidth % NewWidth) == 0) { 7754 unsigned Scale = OldWidth / NewWidth; 7755 unsigned SubMask = (1u << Scale) - 1; 7756 for (unsigned i = 0; i != NewWidth; ++i) { 7757 unsigned Sub = (OldMask >> (i * Scale)) & SubMask; 7758 if (Sub == SubMask) 7759 NewMask |= (1u << i); 7760 else if (Sub != 0x0) 7761 return false; 7762 } 7763 } else { 7764 unsigned Scale = NewWidth / OldWidth; 7765 unsigned SubMask = (1u << Scale) - 1; 7766 for (unsigned i = 0; i != OldWidth; ++i) { 7767 if (OldMask & (1 << i)) { 7768 NewMask |= (SubMask << (i * Scale)); 7769 } 7770 } 7771 } 7772 7773 if (pNewMask) 7774 *pNewMask = NewMask; 7775 return true; 7776 } 7777 7778 uint16_t X86InstrInfo::getExecutionDomainCustom(const MachineInstr &MI) const { 7779 unsigned Opcode = MI.getOpcode(); 7780 unsigned NumOperands = MI.getDesc().getNumOperands(); 7781 7782 auto GetBlendDomains = [&](unsigned ImmWidth, bool Is256) { 7783 uint16_t validDomains = 0; 7784 if (MI.getOperand(NumOperands - 1).isImm()) { 7785 unsigned Imm = MI.getOperand(NumOperands - 1).getImm(); 7786 if (AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4)) 7787 validDomains |= 0x2; // PackedSingle 7788 if (AdjustBlendMask(Imm, ImmWidth, Is256 ? 4 : 2)) 7789 validDomains |= 0x4; // PackedDouble 7790 if (!Is256 || Subtarget.hasAVX2()) 7791 validDomains |= 0x8; // PackedInt 7792 } 7793 return validDomains; 7794 }; 7795 7796 switch (Opcode) { 7797 case X86::BLENDPDrmi: 7798 case X86::BLENDPDrri: 7799 case X86::VBLENDPDrmi: 7800 case X86::VBLENDPDrri: 7801 return GetBlendDomains(2, false); 7802 case X86::VBLENDPDYrmi: 7803 case X86::VBLENDPDYrri: 7804 return GetBlendDomains(4, true); 7805 case X86::BLENDPSrmi: 7806 case X86::BLENDPSrri: 7807 case X86::VBLENDPSrmi: 7808 case X86::VBLENDPSrri: 7809 case X86::VPBLENDDrmi: 7810 case X86::VPBLENDDrri: 7811 return GetBlendDomains(4, false); 7812 case X86::VBLENDPSYrmi: 7813 case X86::VBLENDPSYrri: 7814 case X86::VPBLENDDYrmi: 7815 case X86::VPBLENDDYrri: 7816 return GetBlendDomains(8, true); 7817 case X86::PBLENDWrmi: 7818 case X86::PBLENDWrri: 7819 case X86::VPBLENDWrmi: 7820 case X86::VPBLENDWrri: 7821 // Treat VPBLENDWY as a 128-bit vector as it repeats the lo/hi masks. 7822 case X86::VPBLENDWYrmi: 7823 case X86::VPBLENDWYrri: 7824 return GetBlendDomains(8, false); 7825 case X86::VPANDDZ128rr: case X86::VPANDDZ128rm: 7826 case X86::VPANDDZ256rr: case X86::VPANDDZ256rm: 7827 case X86::VPANDQZ128rr: case X86::VPANDQZ128rm: 7828 case X86::VPANDQZ256rr: case X86::VPANDQZ256rm: 7829 case X86::VPANDNDZ128rr: case X86::VPANDNDZ128rm: 7830 case X86::VPANDNDZ256rr: case X86::VPANDNDZ256rm: 7831 case X86::VPANDNQZ128rr: case X86::VPANDNQZ128rm: 7832 case X86::VPANDNQZ256rr: case X86::VPANDNQZ256rm: 7833 case X86::VPORDZ128rr: case X86::VPORDZ128rm: 7834 case X86::VPORDZ256rr: case X86::VPORDZ256rm: 7835 case X86::VPORQZ128rr: case X86::VPORQZ128rm: 7836 case X86::VPORQZ256rr: case X86::VPORQZ256rm: 7837 case X86::VPXORDZ128rr: case X86::VPXORDZ128rm: 7838 case X86::VPXORDZ256rr: case X86::VPXORDZ256rm: 7839 case X86::VPXORQZ128rr: case X86::VPXORQZ128rm: 7840 case X86::VPXORQZ256rr: case X86::VPXORQZ256rm: 7841 // If we don't have DQI see if we can still switch from an EVEX integer 7842 // instruction to a VEX floating point instruction. 7843 if (Subtarget.hasDQI()) 7844 return 0; 7845 7846 if (RI.getEncodingValue(MI.getOperand(0).getReg()) >= 16) 7847 return 0; 7848 if (RI.getEncodingValue(MI.getOperand(1).getReg()) >= 16) 7849 return 0; 7850 // Register forms will have 3 operands. Memory form will have more. 7851 if (NumOperands == 3 && 7852 RI.getEncodingValue(MI.getOperand(2).getReg()) >= 16) 7853 return 0; 7854 7855 // All domains are valid. 7856 return 0xe; 7857 case X86::MOVHLPSrr: 7858 // We can swap domains when both inputs are the same register. 7859 // FIXME: This doesn't catch all the cases we would like. If the input 7860 // register isn't KILLed by the instruction, the two address instruction 7861 // pass puts a COPY on one input. The other input uses the original 7862 // register. This prevents the same physical register from being used by 7863 // both inputs. 7864 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg() && 7865 MI.getOperand(0).getSubReg() == 0 && 7866 MI.getOperand(1).getSubReg() == 0 && 7867 MI.getOperand(2).getSubReg() == 0) 7868 return 0x6; 7869 return 0; 7870 case X86::SHUFPDrri: 7871 return 0x6; 7872 } 7873 return 0; 7874 } 7875 7876 bool X86InstrInfo::setExecutionDomainCustom(MachineInstr &MI, 7877 unsigned Domain) const { 7878 assert(Domain > 0 && Domain < 4 && "Invalid execution domain"); 7879 uint16_t dom = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3; 7880 assert(dom && "Not an SSE instruction"); 7881 7882 unsigned Opcode = MI.getOpcode(); 7883 unsigned NumOperands = MI.getDesc().getNumOperands(); 7884 7885 auto SetBlendDomain = [&](unsigned ImmWidth, bool Is256) { 7886 if (MI.getOperand(NumOperands - 1).isImm()) { 7887 unsigned Imm = MI.getOperand(NumOperands - 1).getImm() & 255; 7888 Imm = (ImmWidth == 16 ? ((Imm << 8) | Imm) : Imm); 7889 unsigned NewImm = Imm; 7890 7891 const uint16_t *table = lookup(Opcode, dom, ReplaceableBlendInstrs); 7892 if (!table) 7893 table = lookup(Opcode, dom, ReplaceableBlendAVX2Instrs); 7894 7895 if (Domain == 1) { // PackedSingle 7896 AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4, &NewImm); 7897 } else if (Domain == 2) { // PackedDouble 7898 AdjustBlendMask(Imm, ImmWidth, Is256 ? 4 : 2, &NewImm); 7899 } else if (Domain == 3) { // PackedInt 7900 if (Subtarget.hasAVX2()) { 7901 // If we are already VPBLENDW use that, else use VPBLENDD. 7902 if ((ImmWidth / (Is256 ? 2 : 1)) != 8) { 7903 table = lookup(Opcode, dom, ReplaceableBlendAVX2Instrs); 7904 AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4, &NewImm); 7905 } 7906 } else { 7907 assert(!Is256 && "128-bit vector expected"); 7908 AdjustBlendMask(Imm, ImmWidth, 8, &NewImm); 7909 } 7910 } 7911 7912 assert(table && table[Domain - 1] && "Unknown domain op"); 7913 MI.setDesc(get(table[Domain - 1])); 7914 MI.getOperand(NumOperands - 1).setImm(NewImm & 255); 7915 } 7916 return true; 7917 }; 7918 7919 switch (Opcode) { 7920 case X86::BLENDPDrmi: 7921 case X86::BLENDPDrri: 7922 case X86::VBLENDPDrmi: 7923 case X86::VBLENDPDrri: 7924 return SetBlendDomain(2, false); 7925 case X86::VBLENDPDYrmi: 7926 case X86::VBLENDPDYrri: 7927 return SetBlendDomain(4, true); 7928 case X86::BLENDPSrmi: 7929 case X86::BLENDPSrri: 7930 case X86::VBLENDPSrmi: 7931 case X86::VBLENDPSrri: 7932 case X86::VPBLENDDrmi: 7933 case X86::VPBLENDDrri: 7934 return SetBlendDomain(4, false); 7935 case X86::VBLENDPSYrmi: 7936 case X86::VBLENDPSYrri: 7937 case X86::VPBLENDDYrmi: 7938 case X86::VPBLENDDYrri: 7939 return SetBlendDomain(8, true); 7940 case X86::PBLENDWrmi: 7941 case X86::PBLENDWrri: 7942 case X86::VPBLENDWrmi: 7943 case X86::VPBLENDWrri: 7944 return SetBlendDomain(8, false); 7945 case X86::VPBLENDWYrmi: 7946 case X86::VPBLENDWYrri: 7947 return SetBlendDomain(16, true); 7948 case X86::VPANDDZ128rr: case X86::VPANDDZ128rm: 7949 case X86::VPANDDZ256rr: case X86::VPANDDZ256rm: 7950 case X86::VPANDQZ128rr: case X86::VPANDQZ128rm: 7951 case X86::VPANDQZ256rr: case X86::VPANDQZ256rm: 7952 case X86::VPANDNDZ128rr: case X86::VPANDNDZ128rm: 7953 case X86::VPANDNDZ256rr: case X86::VPANDNDZ256rm: 7954 case X86::VPANDNQZ128rr: case X86::VPANDNQZ128rm: 7955 case X86::VPANDNQZ256rr: case X86::VPANDNQZ256rm: 7956 case X86::VPORDZ128rr: case X86::VPORDZ128rm: 7957 case X86::VPORDZ256rr: case X86::VPORDZ256rm: 7958 case X86::VPORQZ128rr: case X86::VPORQZ128rm: 7959 case X86::VPORQZ256rr: case X86::VPORQZ256rm: 7960 case X86::VPXORDZ128rr: case X86::VPXORDZ128rm: 7961 case X86::VPXORDZ256rr: case X86::VPXORDZ256rm: 7962 case X86::VPXORQZ128rr: case X86::VPXORQZ128rm: 7963 case X86::VPXORQZ256rr: case X86::VPXORQZ256rm: { 7964 // Without DQI, convert EVEX instructions to VEX instructions. 7965 if (Subtarget.hasDQI()) 7966 return false; 7967 7968 const uint16_t *table = lookupAVX512(MI.getOpcode(), dom, 7969 ReplaceableCustomAVX512LogicInstrs); 7970 assert(table && "Instruction not found in table?"); 7971 // Don't change integer Q instructions to D instructions and 7972 // use D intructions if we started with a PS instruction. 7973 if (Domain == 3 && (dom == 1 || table[3] == MI.getOpcode())) 7974 Domain = 4; 7975 MI.setDesc(get(table[Domain - 1])); 7976 return true; 7977 } 7978 case X86::UNPCKHPDrr: 7979 case X86::MOVHLPSrr: 7980 // We just need to commute the instruction which will switch the domains. 7981 if (Domain != dom && Domain != 3 && 7982 MI.getOperand(1).getReg() == MI.getOperand(2).getReg() && 7983 MI.getOperand(0).getSubReg() == 0 && 7984 MI.getOperand(1).getSubReg() == 0 && 7985 MI.getOperand(2).getSubReg() == 0) { 7986 commuteInstruction(MI, false); 7987 return true; 7988 } 7989 // We must always return true for MOVHLPSrr. 7990 if (Opcode == X86::MOVHLPSrr) 7991 return true; 7992 break; 7993 case X86::SHUFPDrri: { 7994 if (Domain == 1) { 7995 unsigned Imm = MI.getOperand(3).getImm(); 7996 unsigned NewImm = 0x44; 7997 if (Imm & 1) NewImm |= 0x0a; 7998 if (Imm & 2) NewImm |= 0xa0; 7999 MI.getOperand(3).setImm(NewImm); 8000 MI.setDesc(get(X86::SHUFPSrri)); 8001 } 8002 return true; 8003 } 8004 } 8005 return false; 8006 } 8007 8008 std::pair<uint16_t, uint16_t> 8009 X86InstrInfo::getExecutionDomain(const MachineInstr &MI) const { 8010 uint16_t domain = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3; 8011 unsigned opcode = MI.getOpcode(); 8012 uint16_t validDomains = 0; 8013 if (domain) { 8014 // Attempt to match for custom instructions. 8015 validDomains = getExecutionDomainCustom(MI); 8016 if (validDomains) 8017 return std::make_pair(domain, validDomains); 8018 8019 if (lookup(opcode, domain, ReplaceableInstrs)) { 8020 validDomains = 0xe; 8021 } else if (lookup(opcode, domain, ReplaceableInstrsAVX2)) { 8022 validDomains = Subtarget.hasAVX2() ? 0xe : 0x6; 8023 } else if (lookup(opcode, domain, ReplaceableInstrsFP)) { 8024 validDomains = 0x6; 8025 } else if (lookup(opcode, domain, ReplaceableInstrsAVX2InsertExtract)) { 8026 // Insert/extract instructions should only effect domain if AVX2 8027 // is enabled. 8028 if (!Subtarget.hasAVX2()) 8029 return std::make_pair(0, 0); 8030 validDomains = 0xe; 8031 } else if (lookupAVX512(opcode, domain, ReplaceableInstrsAVX512)) { 8032 validDomains = 0xe; 8033 } else if (Subtarget.hasDQI() && lookupAVX512(opcode, domain, 8034 ReplaceableInstrsAVX512DQ)) { 8035 validDomains = 0xe; 8036 } else if (Subtarget.hasDQI()) { 8037 if (const uint16_t *table = lookupAVX512(opcode, domain, 8038 ReplaceableInstrsAVX512DQMasked)) { 8039 if (domain == 1 || (domain == 3 && table[3] == opcode)) 8040 validDomains = 0xa; 8041 else 8042 validDomains = 0xc; 8043 } 8044 } 8045 } 8046 return std::make_pair(domain, validDomains); 8047 } 8048 8049 void X86InstrInfo::setExecutionDomain(MachineInstr &MI, unsigned Domain) const { 8050 assert(Domain>0 && Domain<4 && "Invalid execution domain"); 8051 uint16_t dom = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3; 8052 assert(dom && "Not an SSE instruction"); 8053 8054 // Attempt to match for custom instructions. 8055 if (setExecutionDomainCustom(MI, Domain)) 8056 return; 8057 8058 const uint16_t *table = lookup(MI.getOpcode(), dom, ReplaceableInstrs); 8059 if (!table) { // try the other table 8060 assert((Subtarget.hasAVX2() || Domain < 3) && 8061 "256-bit vector operations only available in AVX2"); 8062 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsAVX2); 8063 } 8064 if (!table) { // try the FP table 8065 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsFP); 8066 assert((!table || Domain < 3) && 8067 "Can only select PackedSingle or PackedDouble"); 8068 } 8069 if (!table) { // try the other table 8070 assert(Subtarget.hasAVX2() && 8071 "256-bit insert/extract only available in AVX2"); 8072 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsAVX2InsertExtract); 8073 } 8074 if (!table) { // try the AVX512 table 8075 assert(Subtarget.hasAVX512() && "Requires AVX-512"); 8076 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512); 8077 // Don't change integer Q instructions to D instructions. 8078 if (table && Domain == 3 && table[3] == MI.getOpcode()) 8079 Domain = 4; 8080 } 8081 if (!table) { // try the AVX512DQ table 8082 assert((Subtarget.hasDQI() || Domain >= 3) && "Requires AVX-512DQ"); 8083 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512DQ); 8084 // Don't change integer Q instructions to D instructions and 8085 // use D instructions if we started with a PS instruction. 8086 if (table && Domain == 3 && (dom == 1 || table[3] == MI.getOpcode())) 8087 Domain = 4; 8088 } 8089 if (!table) { // try the AVX512DQMasked table 8090 assert((Subtarget.hasDQI() || Domain >= 3) && "Requires AVX-512DQ"); 8091 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512DQMasked); 8092 if (table && Domain == 3 && (dom == 1 || table[3] == MI.getOpcode())) 8093 Domain = 4; 8094 } 8095 assert(table && "Cannot change domain"); 8096 MI.setDesc(get(table[Domain - 1])); 8097 } 8098 8099 /// Return the noop instruction to use for a noop. 8100 MCInst X86InstrInfo::getNop() const { 8101 MCInst Nop; 8102 Nop.setOpcode(X86::NOOP); 8103 return Nop; 8104 } 8105 8106 bool X86InstrInfo::isHighLatencyDef(int opc) const { 8107 switch (opc) { 8108 default: return false; 8109 case X86::DIVPDrm: 8110 case X86::DIVPDrr: 8111 case X86::DIVPSrm: 8112 case X86::DIVPSrr: 8113 case X86::DIVSDrm: 8114 case X86::DIVSDrm_Int: 8115 case X86::DIVSDrr: 8116 case X86::DIVSDrr_Int: 8117 case X86::DIVSSrm: 8118 case X86::DIVSSrm_Int: 8119 case X86::DIVSSrr: 8120 case X86::DIVSSrr_Int: 8121 case X86::SQRTPDm: 8122 case X86::SQRTPDr: 8123 case X86::SQRTPSm: 8124 case X86::SQRTPSr: 8125 case X86::SQRTSDm: 8126 case X86::SQRTSDm_Int: 8127 case X86::SQRTSDr: 8128 case X86::SQRTSDr_Int: 8129 case X86::SQRTSSm: 8130 case X86::SQRTSSm_Int: 8131 case X86::SQRTSSr: 8132 case X86::SQRTSSr_Int: 8133 // AVX instructions with high latency 8134 case X86::VDIVPDrm: 8135 case X86::VDIVPDrr: 8136 case X86::VDIVPDYrm: 8137 case X86::VDIVPDYrr: 8138 case X86::VDIVPSrm: 8139 case X86::VDIVPSrr: 8140 case X86::VDIVPSYrm: 8141 case X86::VDIVPSYrr: 8142 case X86::VDIVSDrm: 8143 case X86::VDIVSDrm_Int: 8144 case X86::VDIVSDrr: 8145 case X86::VDIVSDrr_Int: 8146 case X86::VDIVSSrm: 8147 case X86::VDIVSSrm_Int: 8148 case X86::VDIVSSrr: 8149 case X86::VDIVSSrr_Int: 8150 case X86::VSQRTPDm: 8151 case X86::VSQRTPDr: 8152 case X86::VSQRTPDYm: 8153 case X86::VSQRTPDYr: 8154 case X86::VSQRTPSm: 8155 case X86::VSQRTPSr: 8156 case X86::VSQRTPSYm: 8157 case X86::VSQRTPSYr: 8158 case X86::VSQRTSDm: 8159 case X86::VSQRTSDm_Int: 8160 case X86::VSQRTSDr: 8161 case X86::VSQRTSDr_Int: 8162 case X86::VSQRTSSm: 8163 case X86::VSQRTSSm_Int: 8164 case X86::VSQRTSSr: 8165 case X86::VSQRTSSr_Int: 8166 // AVX512 instructions with high latency 8167 case X86::VDIVPDZ128rm: 8168 case X86::VDIVPDZ128rmb: 8169 case X86::VDIVPDZ128rmbk: 8170 case X86::VDIVPDZ128rmbkz: 8171 case X86::VDIVPDZ128rmk: 8172 case X86::VDIVPDZ128rmkz: 8173 case X86::VDIVPDZ128rr: 8174 case X86::VDIVPDZ128rrk: 8175 case X86::VDIVPDZ128rrkz: 8176 case X86::VDIVPDZ256rm: 8177 case X86::VDIVPDZ256rmb: 8178 case X86::VDIVPDZ256rmbk: 8179 case X86::VDIVPDZ256rmbkz: 8180 case X86::VDIVPDZ256rmk: 8181 case X86::VDIVPDZ256rmkz: 8182 case X86::VDIVPDZ256rr: 8183 case X86::VDIVPDZ256rrk: 8184 case X86::VDIVPDZ256rrkz: 8185 case X86::VDIVPDZrrb: 8186 case X86::VDIVPDZrrbk: 8187 case X86::VDIVPDZrrbkz: 8188 case X86::VDIVPDZrm: 8189 case X86::VDIVPDZrmb: 8190 case X86::VDIVPDZrmbk: 8191 case X86::VDIVPDZrmbkz: 8192 case X86::VDIVPDZrmk: 8193 case X86::VDIVPDZrmkz: 8194 case X86::VDIVPDZrr: 8195 case X86::VDIVPDZrrk: 8196 case X86::VDIVPDZrrkz: 8197 case X86::VDIVPSZ128rm: 8198 case X86::VDIVPSZ128rmb: 8199 case X86::VDIVPSZ128rmbk: 8200 case X86::VDIVPSZ128rmbkz: 8201 case X86::VDIVPSZ128rmk: 8202 case X86::VDIVPSZ128rmkz: 8203 case X86::VDIVPSZ128rr: 8204 case X86::VDIVPSZ128rrk: 8205 case X86::VDIVPSZ128rrkz: 8206 case X86::VDIVPSZ256rm: 8207 case X86::VDIVPSZ256rmb: 8208 case X86::VDIVPSZ256rmbk: 8209 case X86::VDIVPSZ256rmbkz: 8210 case X86::VDIVPSZ256rmk: 8211 case X86::VDIVPSZ256rmkz: 8212 case X86::VDIVPSZ256rr: 8213 case X86::VDIVPSZ256rrk: 8214 case X86::VDIVPSZ256rrkz: 8215 case X86::VDIVPSZrrb: 8216 case X86::VDIVPSZrrbk: 8217 case X86::VDIVPSZrrbkz: 8218 case X86::VDIVPSZrm: 8219 case X86::VDIVPSZrmb: 8220 case X86::VDIVPSZrmbk: 8221 case X86::VDIVPSZrmbkz: 8222 case X86::VDIVPSZrmk: 8223 case X86::VDIVPSZrmkz: 8224 case X86::VDIVPSZrr: 8225 case X86::VDIVPSZrrk: 8226 case X86::VDIVPSZrrkz: 8227 case X86::VDIVSDZrm: 8228 case X86::VDIVSDZrr: 8229 case X86::VDIVSDZrm_Int: 8230 case X86::VDIVSDZrm_Intk: 8231 case X86::VDIVSDZrm_Intkz: 8232 case X86::VDIVSDZrr_Int: 8233 case X86::VDIVSDZrr_Intk: 8234 case X86::VDIVSDZrr_Intkz: 8235 case X86::VDIVSDZrrb_Int: 8236 case X86::VDIVSDZrrb_Intk: 8237 case X86::VDIVSDZrrb_Intkz: 8238 case X86::VDIVSSZrm: 8239 case X86::VDIVSSZrr: 8240 case X86::VDIVSSZrm_Int: 8241 case X86::VDIVSSZrm_Intk: 8242 case X86::VDIVSSZrm_Intkz: 8243 case X86::VDIVSSZrr_Int: 8244 case X86::VDIVSSZrr_Intk: 8245 case X86::VDIVSSZrr_Intkz: 8246 case X86::VDIVSSZrrb_Int: 8247 case X86::VDIVSSZrrb_Intk: 8248 case X86::VDIVSSZrrb_Intkz: 8249 case X86::VSQRTPDZ128m: 8250 case X86::VSQRTPDZ128mb: 8251 case X86::VSQRTPDZ128mbk: 8252 case X86::VSQRTPDZ128mbkz: 8253 case X86::VSQRTPDZ128mk: 8254 case X86::VSQRTPDZ128mkz: 8255 case X86::VSQRTPDZ128r: 8256 case X86::VSQRTPDZ128rk: 8257 case X86::VSQRTPDZ128rkz: 8258 case X86::VSQRTPDZ256m: 8259 case X86::VSQRTPDZ256mb: 8260 case X86::VSQRTPDZ256mbk: 8261 case X86::VSQRTPDZ256mbkz: 8262 case X86::VSQRTPDZ256mk: 8263 case X86::VSQRTPDZ256mkz: 8264 case X86::VSQRTPDZ256r: 8265 case X86::VSQRTPDZ256rk: 8266 case X86::VSQRTPDZ256rkz: 8267 case X86::VSQRTPDZm: 8268 case X86::VSQRTPDZmb: 8269 case X86::VSQRTPDZmbk: 8270 case X86::VSQRTPDZmbkz: 8271 case X86::VSQRTPDZmk: 8272 case X86::VSQRTPDZmkz: 8273 case X86::VSQRTPDZr: 8274 case X86::VSQRTPDZrb: 8275 case X86::VSQRTPDZrbk: 8276 case X86::VSQRTPDZrbkz: 8277 case X86::VSQRTPDZrk: 8278 case X86::VSQRTPDZrkz: 8279 case X86::VSQRTPSZ128m: 8280 case X86::VSQRTPSZ128mb: 8281 case X86::VSQRTPSZ128mbk: 8282 case X86::VSQRTPSZ128mbkz: 8283 case X86::VSQRTPSZ128mk: 8284 case X86::VSQRTPSZ128mkz: 8285 case X86::VSQRTPSZ128r: 8286 case X86::VSQRTPSZ128rk: 8287 case X86::VSQRTPSZ128rkz: 8288 case X86::VSQRTPSZ256m: 8289 case X86::VSQRTPSZ256mb: 8290 case X86::VSQRTPSZ256mbk: 8291 case X86::VSQRTPSZ256mbkz: 8292 case X86::VSQRTPSZ256mk: 8293 case X86::VSQRTPSZ256mkz: 8294 case X86::VSQRTPSZ256r: 8295 case X86::VSQRTPSZ256rk: 8296 case X86::VSQRTPSZ256rkz: 8297 case X86::VSQRTPSZm: 8298 case X86::VSQRTPSZmb: 8299 case X86::VSQRTPSZmbk: 8300 case X86::VSQRTPSZmbkz: 8301 case X86::VSQRTPSZmk: 8302 case X86::VSQRTPSZmkz: 8303 case X86::VSQRTPSZr: 8304 case X86::VSQRTPSZrb: 8305 case X86::VSQRTPSZrbk: 8306 case X86::VSQRTPSZrbkz: 8307 case X86::VSQRTPSZrk: 8308 case X86::VSQRTPSZrkz: 8309 case X86::VSQRTSDZm: 8310 case X86::VSQRTSDZm_Int: 8311 case X86::VSQRTSDZm_Intk: 8312 case X86::VSQRTSDZm_Intkz: 8313 case X86::VSQRTSDZr: 8314 case X86::VSQRTSDZr_Int: 8315 case X86::VSQRTSDZr_Intk: 8316 case X86::VSQRTSDZr_Intkz: 8317 case X86::VSQRTSDZrb_Int: 8318 case X86::VSQRTSDZrb_Intk: 8319 case X86::VSQRTSDZrb_Intkz: 8320 case X86::VSQRTSSZm: 8321 case X86::VSQRTSSZm_Int: 8322 case X86::VSQRTSSZm_Intk: 8323 case X86::VSQRTSSZm_Intkz: 8324 case X86::VSQRTSSZr: 8325 case X86::VSQRTSSZr_Int: 8326 case X86::VSQRTSSZr_Intk: 8327 case X86::VSQRTSSZr_Intkz: 8328 case X86::VSQRTSSZrb_Int: 8329 case X86::VSQRTSSZrb_Intk: 8330 case X86::VSQRTSSZrb_Intkz: 8331 8332 case X86::VGATHERDPDYrm: 8333 case X86::VGATHERDPDZ128rm: 8334 case X86::VGATHERDPDZ256rm: 8335 case X86::VGATHERDPDZrm: 8336 case X86::VGATHERDPDrm: 8337 case X86::VGATHERDPSYrm: 8338 case X86::VGATHERDPSZ128rm: 8339 case X86::VGATHERDPSZ256rm: 8340 case X86::VGATHERDPSZrm: 8341 case X86::VGATHERDPSrm: 8342 case X86::VGATHERPF0DPDm: 8343 case X86::VGATHERPF0DPSm: 8344 case X86::VGATHERPF0QPDm: 8345 case X86::VGATHERPF0QPSm: 8346 case X86::VGATHERPF1DPDm: 8347 case X86::VGATHERPF1DPSm: 8348 case X86::VGATHERPF1QPDm: 8349 case X86::VGATHERPF1QPSm: 8350 case X86::VGATHERQPDYrm: 8351 case X86::VGATHERQPDZ128rm: 8352 case X86::VGATHERQPDZ256rm: 8353 case X86::VGATHERQPDZrm: 8354 case X86::VGATHERQPDrm: 8355 case X86::VGATHERQPSYrm: 8356 case X86::VGATHERQPSZ128rm: 8357 case X86::VGATHERQPSZ256rm: 8358 case X86::VGATHERQPSZrm: 8359 case X86::VGATHERQPSrm: 8360 case X86::VPGATHERDDYrm: 8361 case X86::VPGATHERDDZ128rm: 8362 case X86::VPGATHERDDZ256rm: 8363 case X86::VPGATHERDDZrm: 8364 case X86::VPGATHERDDrm: 8365 case X86::VPGATHERDQYrm: 8366 case X86::VPGATHERDQZ128rm: 8367 case X86::VPGATHERDQZ256rm: 8368 case X86::VPGATHERDQZrm: 8369 case X86::VPGATHERDQrm: 8370 case X86::VPGATHERQDYrm: 8371 case X86::VPGATHERQDZ128rm: 8372 case X86::VPGATHERQDZ256rm: 8373 case X86::VPGATHERQDZrm: 8374 case X86::VPGATHERQDrm: 8375 case X86::VPGATHERQQYrm: 8376 case X86::VPGATHERQQZ128rm: 8377 case X86::VPGATHERQQZ256rm: 8378 case X86::VPGATHERQQZrm: 8379 case X86::VPGATHERQQrm: 8380 case X86::VSCATTERDPDZ128mr: 8381 case X86::VSCATTERDPDZ256mr: 8382 case X86::VSCATTERDPDZmr: 8383 case X86::VSCATTERDPSZ128mr: 8384 case X86::VSCATTERDPSZ256mr: 8385 case X86::VSCATTERDPSZmr: 8386 case X86::VSCATTERPF0DPDm: 8387 case X86::VSCATTERPF0DPSm: 8388 case X86::VSCATTERPF0QPDm: 8389 case X86::VSCATTERPF0QPSm: 8390 case X86::VSCATTERPF1DPDm: 8391 case X86::VSCATTERPF1DPSm: 8392 case X86::VSCATTERPF1QPDm: 8393 case X86::VSCATTERPF1QPSm: 8394 case X86::VSCATTERQPDZ128mr: 8395 case X86::VSCATTERQPDZ256mr: 8396 case X86::VSCATTERQPDZmr: 8397 case X86::VSCATTERQPSZ128mr: 8398 case X86::VSCATTERQPSZ256mr: 8399 case X86::VSCATTERQPSZmr: 8400 case X86::VPSCATTERDDZ128mr: 8401 case X86::VPSCATTERDDZ256mr: 8402 case X86::VPSCATTERDDZmr: 8403 case X86::VPSCATTERDQZ128mr: 8404 case X86::VPSCATTERDQZ256mr: 8405 case X86::VPSCATTERDQZmr: 8406 case X86::VPSCATTERQDZ128mr: 8407 case X86::VPSCATTERQDZ256mr: 8408 case X86::VPSCATTERQDZmr: 8409 case X86::VPSCATTERQQZ128mr: 8410 case X86::VPSCATTERQQZ256mr: 8411 case X86::VPSCATTERQQZmr: 8412 return true; 8413 } 8414 } 8415 8416 bool X86InstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel, 8417 const MachineRegisterInfo *MRI, 8418 const MachineInstr &DefMI, 8419 unsigned DefIdx, 8420 const MachineInstr &UseMI, 8421 unsigned UseIdx) const { 8422 return isHighLatencyDef(DefMI.getOpcode()); 8423 } 8424 8425 bool X86InstrInfo::hasReassociableOperands(const MachineInstr &Inst, 8426 const MachineBasicBlock *MBB) const { 8427 assert(Inst.getNumExplicitOperands() == 3 && Inst.getNumExplicitDefs() == 1 && 8428 Inst.getNumDefs() <= 2 && "Reassociation needs binary operators"); 8429 8430 // Integer binary math/logic instructions have a third source operand: 8431 // the EFLAGS register. That operand must be both defined here and never 8432 // used; ie, it must be dead. If the EFLAGS operand is live, then we can 8433 // not change anything because rearranging the operands could affect other 8434 // instructions that depend on the exact status flags (zero, sign, etc.) 8435 // that are set by using these particular operands with this operation. 8436 const MachineOperand *FlagDef = Inst.findRegisterDefOperand(X86::EFLAGS); 8437 assert((Inst.getNumDefs() == 1 || FlagDef) && 8438 "Implicit def isn't flags?"); 8439 if (FlagDef && !FlagDef->isDead()) 8440 return false; 8441 8442 return TargetInstrInfo::hasReassociableOperands(Inst, MBB); 8443 } 8444 8445 // TODO: There are many more machine instruction opcodes to match: 8446 // 1. Other data types (integer, vectors) 8447 // 2. Other math / logic operations (xor, or) 8448 // 3. Other forms of the same operation (intrinsics and other variants) 8449 bool X86InstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const { 8450 switch (Inst.getOpcode()) { 8451 case X86::AND8rr: 8452 case X86::AND16rr: 8453 case X86::AND32rr: 8454 case X86::AND64rr: 8455 case X86::OR8rr: 8456 case X86::OR16rr: 8457 case X86::OR32rr: 8458 case X86::OR64rr: 8459 case X86::XOR8rr: 8460 case X86::XOR16rr: 8461 case X86::XOR32rr: 8462 case X86::XOR64rr: 8463 case X86::IMUL16rr: 8464 case X86::IMUL32rr: 8465 case X86::IMUL64rr: 8466 case X86::PANDrr: 8467 case X86::PORrr: 8468 case X86::PXORrr: 8469 case X86::ANDPDrr: 8470 case X86::ANDPSrr: 8471 case X86::ORPDrr: 8472 case X86::ORPSrr: 8473 case X86::XORPDrr: 8474 case X86::XORPSrr: 8475 case X86::PADDBrr: 8476 case X86::PADDWrr: 8477 case X86::PADDDrr: 8478 case X86::PADDQrr: 8479 case X86::PMULLWrr: 8480 case X86::PMULLDrr: 8481 case X86::PMAXSBrr: 8482 case X86::PMAXSDrr: 8483 case X86::PMAXSWrr: 8484 case X86::PMAXUBrr: 8485 case X86::PMAXUDrr: 8486 case X86::PMAXUWrr: 8487 case X86::PMINSBrr: 8488 case X86::PMINSDrr: 8489 case X86::PMINSWrr: 8490 case X86::PMINUBrr: 8491 case X86::PMINUDrr: 8492 case X86::PMINUWrr: 8493 case X86::VPANDrr: 8494 case X86::VPANDYrr: 8495 case X86::VPANDDZ128rr: 8496 case X86::VPANDDZ256rr: 8497 case X86::VPANDDZrr: 8498 case X86::VPANDQZ128rr: 8499 case X86::VPANDQZ256rr: 8500 case X86::VPANDQZrr: 8501 case X86::VPORrr: 8502 case X86::VPORYrr: 8503 case X86::VPORDZ128rr: 8504 case X86::VPORDZ256rr: 8505 case X86::VPORDZrr: 8506 case X86::VPORQZ128rr: 8507 case X86::VPORQZ256rr: 8508 case X86::VPORQZrr: 8509 case X86::VPXORrr: 8510 case X86::VPXORYrr: 8511 case X86::VPXORDZ128rr: 8512 case X86::VPXORDZ256rr: 8513 case X86::VPXORDZrr: 8514 case X86::VPXORQZ128rr: 8515 case X86::VPXORQZ256rr: 8516 case X86::VPXORQZrr: 8517 case X86::VANDPDrr: 8518 case X86::VANDPSrr: 8519 case X86::VANDPDYrr: 8520 case X86::VANDPSYrr: 8521 case X86::VANDPDZ128rr: 8522 case X86::VANDPSZ128rr: 8523 case X86::VANDPDZ256rr: 8524 case X86::VANDPSZ256rr: 8525 case X86::VANDPDZrr: 8526 case X86::VANDPSZrr: 8527 case X86::VORPDrr: 8528 case X86::VORPSrr: 8529 case X86::VORPDYrr: 8530 case X86::VORPSYrr: 8531 case X86::VORPDZ128rr: 8532 case X86::VORPSZ128rr: 8533 case X86::VORPDZ256rr: 8534 case X86::VORPSZ256rr: 8535 case X86::VORPDZrr: 8536 case X86::VORPSZrr: 8537 case X86::VXORPDrr: 8538 case X86::VXORPSrr: 8539 case X86::VXORPDYrr: 8540 case X86::VXORPSYrr: 8541 case X86::VXORPDZ128rr: 8542 case X86::VXORPSZ128rr: 8543 case X86::VXORPDZ256rr: 8544 case X86::VXORPSZ256rr: 8545 case X86::VXORPDZrr: 8546 case X86::VXORPSZrr: 8547 case X86::KADDBrr: 8548 case X86::KADDWrr: 8549 case X86::KADDDrr: 8550 case X86::KADDQrr: 8551 case X86::KANDBrr: 8552 case X86::KANDWrr: 8553 case X86::KANDDrr: 8554 case X86::KANDQrr: 8555 case X86::KORBrr: 8556 case X86::KORWrr: 8557 case X86::KORDrr: 8558 case X86::KORQrr: 8559 case X86::KXORBrr: 8560 case X86::KXORWrr: 8561 case X86::KXORDrr: 8562 case X86::KXORQrr: 8563 case X86::VPADDBrr: 8564 case X86::VPADDWrr: 8565 case X86::VPADDDrr: 8566 case X86::VPADDQrr: 8567 case X86::VPADDBYrr: 8568 case X86::VPADDWYrr: 8569 case X86::VPADDDYrr: 8570 case X86::VPADDQYrr: 8571 case X86::VPADDBZ128rr: 8572 case X86::VPADDWZ128rr: 8573 case X86::VPADDDZ128rr: 8574 case X86::VPADDQZ128rr: 8575 case X86::VPADDBZ256rr: 8576 case X86::VPADDWZ256rr: 8577 case X86::VPADDDZ256rr: 8578 case X86::VPADDQZ256rr: 8579 case X86::VPADDBZrr: 8580 case X86::VPADDWZrr: 8581 case X86::VPADDDZrr: 8582 case X86::VPADDQZrr: 8583 case X86::VPMULLWrr: 8584 case X86::VPMULLWYrr: 8585 case X86::VPMULLWZ128rr: 8586 case X86::VPMULLWZ256rr: 8587 case X86::VPMULLWZrr: 8588 case X86::VPMULLDrr: 8589 case X86::VPMULLDYrr: 8590 case X86::VPMULLDZ128rr: 8591 case X86::VPMULLDZ256rr: 8592 case X86::VPMULLDZrr: 8593 case X86::VPMULLQZ128rr: 8594 case X86::VPMULLQZ256rr: 8595 case X86::VPMULLQZrr: 8596 case X86::VPMAXSBrr: 8597 case X86::VPMAXSBYrr: 8598 case X86::VPMAXSBZ128rr: 8599 case X86::VPMAXSBZ256rr: 8600 case X86::VPMAXSBZrr: 8601 case X86::VPMAXSDrr: 8602 case X86::VPMAXSDYrr: 8603 case X86::VPMAXSDZ128rr: 8604 case X86::VPMAXSDZ256rr: 8605 case X86::VPMAXSDZrr: 8606 case X86::VPMAXSQZ128rr: 8607 case X86::VPMAXSQZ256rr: 8608 case X86::VPMAXSQZrr: 8609 case X86::VPMAXSWrr: 8610 case X86::VPMAXSWYrr: 8611 case X86::VPMAXSWZ128rr: 8612 case X86::VPMAXSWZ256rr: 8613 case X86::VPMAXSWZrr: 8614 case X86::VPMAXUBrr: 8615 case X86::VPMAXUBYrr: 8616 case X86::VPMAXUBZ128rr: 8617 case X86::VPMAXUBZ256rr: 8618 case X86::VPMAXUBZrr: 8619 case X86::VPMAXUDrr: 8620 case X86::VPMAXUDYrr: 8621 case X86::VPMAXUDZ128rr: 8622 case X86::VPMAXUDZ256rr: 8623 case X86::VPMAXUDZrr: 8624 case X86::VPMAXUQZ128rr: 8625 case X86::VPMAXUQZ256rr: 8626 case X86::VPMAXUQZrr: 8627 case X86::VPMAXUWrr: 8628 case X86::VPMAXUWYrr: 8629 case X86::VPMAXUWZ128rr: 8630 case X86::VPMAXUWZ256rr: 8631 case X86::VPMAXUWZrr: 8632 case X86::VPMINSBrr: 8633 case X86::VPMINSBYrr: 8634 case X86::VPMINSBZ128rr: 8635 case X86::VPMINSBZ256rr: 8636 case X86::VPMINSBZrr: 8637 case X86::VPMINSDrr: 8638 case X86::VPMINSDYrr: 8639 case X86::VPMINSDZ128rr: 8640 case X86::VPMINSDZ256rr: 8641 case X86::VPMINSDZrr: 8642 case X86::VPMINSQZ128rr: 8643 case X86::VPMINSQZ256rr: 8644 case X86::VPMINSQZrr: 8645 case X86::VPMINSWrr: 8646 case X86::VPMINSWYrr: 8647 case X86::VPMINSWZ128rr: 8648 case X86::VPMINSWZ256rr: 8649 case X86::VPMINSWZrr: 8650 case X86::VPMINUBrr: 8651 case X86::VPMINUBYrr: 8652 case X86::VPMINUBZ128rr: 8653 case X86::VPMINUBZ256rr: 8654 case X86::VPMINUBZrr: 8655 case X86::VPMINUDrr: 8656 case X86::VPMINUDYrr: 8657 case X86::VPMINUDZ128rr: 8658 case X86::VPMINUDZ256rr: 8659 case X86::VPMINUDZrr: 8660 case X86::VPMINUQZ128rr: 8661 case X86::VPMINUQZ256rr: 8662 case X86::VPMINUQZrr: 8663 case X86::VPMINUWrr: 8664 case X86::VPMINUWYrr: 8665 case X86::VPMINUWZ128rr: 8666 case X86::VPMINUWZ256rr: 8667 case X86::VPMINUWZrr: 8668 // Normal min/max instructions are not commutative because of NaN and signed 8669 // zero semantics, but these are. Thus, there's no need to check for global 8670 // relaxed math; the instructions themselves have the properties we need. 8671 case X86::MAXCPDrr: 8672 case X86::MAXCPSrr: 8673 case X86::MAXCSDrr: 8674 case X86::MAXCSSrr: 8675 case X86::MINCPDrr: 8676 case X86::MINCPSrr: 8677 case X86::MINCSDrr: 8678 case X86::MINCSSrr: 8679 case X86::VMAXCPDrr: 8680 case X86::VMAXCPSrr: 8681 case X86::VMAXCPDYrr: 8682 case X86::VMAXCPSYrr: 8683 case X86::VMAXCPDZ128rr: 8684 case X86::VMAXCPSZ128rr: 8685 case X86::VMAXCPDZ256rr: 8686 case X86::VMAXCPSZ256rr: 8687 case X86::VMAXCPDZrr: 8688 case X86::VMAXCPSZrr: 8689 case X86::VMAXCSDrr: 8690 case X86::VMAXCSSrr: 8691 case X86::VMAXCSDZrr: 8692 case X86::VMAXCSSZrr: 8693 case X86::VMINCPDrr: 8694 case X86::VMINCPSrr: 8695 case X86::VMINCPDYrr: 8696 case X86::VMINCPSYrr: 8697 case X86::VMINCPDZ128rr: 8698 case X86::VMINCPSZ128rr: 8699 case X86::VMINCPDZ256rr: 8700 case X86::VMINCPSZ256rr: 8701 case X86::VMINCPDZrr: 8702 case X86::VMINCPSZrr: 8703 case X86::VMINCSDrr: 8704 case X86::VMINCSSrr: 8705 case X86::VMINCSDZrr: 8706 case X86::VMINCSSZrr: 8707 case X86::VMAXCPHZ128rr: 8708 case X86::VMAXCPHZ256rr: 8709 case X86::VMAXCPHZrr: 8710 case X86::VMAXCSHZrr: 8711 case X86::VMINCPHZ128rr: 8712 case X86::VMINCPHZ256rr: 8713 case X86::VMINCPHZrr: 8714 case X86::VMINCSHZrr: 8715 return true; 8716 case X86::ADDPDrr: 8717 case X86::ADDPSrr: 8718 case X86::ADDSDrr: 8719 case X86::ADDSSrr: 8720 case X86::MULPDrr: 8721 case X86::MULPSrr: 8722 case X86::MULSDrr: 8723 case X86::MULSSrr: 8724 case X86::VADDPDrr: 8725 case X86::VADDPSrr: 8726 case X86::VADDPDYrr: 8727 case X86::VADDPSYrr: 8728 case X86::VADDPDZ128rr: 8729 case X86::VADDPSZ128rr: 8730 case X86::VADDPDZ256rr: 8731 case X86::VADDPSZ256rr: 8732 case X86::VADDPDZrr: 8733 case X86::VADDPSZrr: 8734 case X86::VADDSDrr: 8735 case X86::VADDSSrr: 8736 case X86::VADDSDZrr: 8737 case X86::VADDSSZrr: 8738 case X86::VMULPDrr: 8739 case X86::VMULPSrr: 8740 case X86::VMULPDYrr: 8741 case X86::VMULPSYrr: 8742 case X86::VMULPDZ128rr: 8743 case X86::VMULPSZ128rr: 8744 case X86::VMULPDZ256rr: 8745 case X86::VMULPSZ256rr: 8746 case X86::VMULPDZrr: 8747 case X86::VMULPSZrr: 8748 case X86::VMULSDrr: 8749 case X86::VMULSSrr: 8750 case X86::VMULSDZrr: 8751 case X86::VMULSSZrr: 8752 case X86::VADDPHZ128rr: 8753 case X86::VADDPHZ256rr: 8754 case X86::VADDPHZrr: 8755 case X86::VADDSHZrr: 8756 case X86::VMULPHZ128rr: 8757 case X86::VMULPHZ256rr: 8758 case X86::VMULPHZrr: 8759 case X86::VMULSHZrr: 8760 return Inst.getFlag(MachineInstr::MIFlag::FmReassoc) && 8761 Inst.getFlag(MachineInstr::MIFlag::FmNsz); 8762 default: 8763 return false; 8764 } 8765 } 8766 8767 /// If \p DescribedReg overlaps with the MOVrr instruction's destination 8768 /// register then, if possible, describe the value in terms of the source 8769 /// register. 8770 static Optional<ParamLoadedValue> 8771 describeMOVrrLoadedValue(const MachineInstr &MI, Register DescribedReg, 8772 const TargetRegisterInfo *TRI) { 8773 Register DestReg = MI.getOperand(0).getReg(); 8774 Register SrcReg = MI.getOperand(1).getReg(); 8775 8776 auto Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {}); 8777 8778 // If the described register is the destination, just return the source. 8779 if (DestReg == DescribedReg) 8780 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr); 8781 8782 // If the described register is a sub-register of the destination register, 8783 // then pick out the source register's corresponding sub-register. 8784 if (unsigned SubRegIdx = TRI->getSubRegIndex(DestReg, DescribedReg)) { 8785 Register SrcSubReg = TRI->getSubReg(SrcReg, SubRegIdx); 8786 return ParamLoadedValue(MachineOperand::CreateReg(SrcSubReg, false), Expr); 8787 } 8788 8789 // The remaining case to consider is when the described register is a 8790 // super-register of the destination register. MOV8rr and MOV16rr does not 8791 // write to any of the other bytes in the register, meaning that we'd have to 8792 // describe the value using a combination of the source register and the 8793 // non-overlapping bits in the described register, which is not currently 8794 // possible. 8795 if (MI.getOpcode() == X86::MOV8rr || MI.getOpcode() == X86::MOV16rr || 8796 !TRI->isSuperRegister(DestReg, DescribedReg)) 8797 return None; 8798 8799 assert(MI.getOpcode() == X86::MOV32rr && "Unexpected super-register case"); 8800 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr); 8801 } 8802 8803 Optional<ParamLoadedValue> 8804 X86InstrInfo::describeLoadedValue(const MachineInstr &MI, Register Reg) const { 8805 const MachineOperand *Op = nullptr; 8806 DIExpression *Expr = nullptr; 8807 8808 const TargetRegisterInfo *TRI = &getRegisterInfo(); 8809 8810 switch (MI.getOpcode()) { 8811 case X86::LEA32r: 8812 case X86::LEA64r: 8813 case X86::LEA64_32r: { 8814 // We may need to describe a 64-bit parameter with a 32-bit LEA. 8815 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg)) 8816 return None; 8817 8818 // Operand 4 could be global address. For now we do not support 8819 // such situation. 8820 if (!MI.getOperand(4).isImm() || !MI.getOperand(2).isImm()) 8821 return None; 8822 8823 const MachineOperand &Op1 = MI.getOperand(1); 8824 const MachineOperand &Op2 = MI.getOperand(3); 8825 assert(Op2.isReg() && (Op2.getReg() == X86::NoRegister || 8826 Register::isPhysicalRegister(Op2.getReg()))); 8827 8828 // Omit situations like: 8829 // %rsi = lea %rsi, 4, ... 8830 if ((Op1.isReg() && Op1.getReg() == MI.getOperand(0).getReg()) || 8831 Op2.getReg() == MI.getOperand(0).getReg()) 8832 return None; 8833 else if ((Op1.isReg() && Op1.getReg() != X86::NoRegister && 8834 TRI->regsOverlap(Op1.getReg(), MI.getOperand(0).getReg())) || 8835 (Op2.getReg() != X86::NoRegister && 8836 TRI->regsOverlap(Op2.getReg(), MI.getOperand(0).getReg()))) 8837 return None; 8838 8839 int64_t Coef = MI.getOperand(2).getImm(); 8840 int64_t Offset = MI.getOperand(4).getImm(); 8841 SmallVector<uint64_t, 8> Ops; 8842 8843 if ((Op1.isReg() && Op1.getReg() != X86::NoRegister)) { 8844 Op = &Op1; 8845 } else if (Op1.isFI()) 8846 Op = &Op1; 8847 8848 if (Op && Op->isReg() && Op->getReg() == Op2.getReg() && Coef > 0) { 8849 Ops.push_back(dwarf::DW_OP_constu); 8850 Ops.push_back(Coef + 1); 8851 Ops.push_back(dwarf::DW_OP_mul); 8852 } else { 8853 if (Op && Op2.getReg() != X86::NoRegister) { 8854 int dwarfReg = TRI->getDwarfRegNum(Op2.getReg(), false); 8855 if (dwarfReg < 0) 8856 return None; 8857 else if (dwarfReg < 32) { 8858 Ops.push_back(dwarf::DW_OP_breg0 + dwarfReg); 8859 Ops.push_back(0); 8860 } else { 8861 Ops.push_back(dwarf::DW_OP_bregx); 8862 Ops.push_back(dwarfReg); 8863 Ops.push_back(0); 8864 } 8865 } else if (!Op) { 8866 assert(Op2.getReg() != X86::NoRegister); 8867 Op = &Op2; 8868 } 8869 8870 if (Coef > 1) { 8871 assert(Op2.getReg() != X86::NoRegister); 8872 Ops.push_back(dwarf::DW_OP_constu); 8873 Ops.push_back(Coef); 8874 Ops.push_back(dwarf::DW_OP_mul); 8875 } 8876 8877 if (((Op1.isReg() && Op1.getReg() != X86::NoRegister) || Op1.isFI()) && 8878 Op2.getReg() != X86::NoRegister) { 8879 Ops.push_back(dwarf::DW_OP_plus); 8880 } 8881 } 8882 8883 DIExpression::appendOffset(Ops, Offset); 8884 Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), Ops); 8885 8886 return ParamLoadedValue(*Op, Expr);; 8887 } 8888 case X86::MOV8ri: 8889 case X86::MOV16ri: 8890 // TODO: Handle MOV8ri and MOV16ri. 8891 return None; 8892 case X86::MOV32ri: 8893 case X86::MOV64ri: 8894 case X86::MOV64ri32: 8895 // MOV32ri may be used for producing zero-extended 32-bit immediates in 8896 // 64-bit parameters, so we need to consider super-registers. 8897 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg)) 8898 return None; 8899 return ParamLoadedValue(MI.getOperand(1), Expr); 8900 case X86::MOV8rr: 8901 case X86::MOV16rr: 8902 case X86::MOV32rr: 8903 case X86::MOV64rr: 8904 return describeMOVrrLoadedValue(MI, Reg, TRI); 8905 case X86::XOR32rr: { 8906 // 64-bit parameters are zero-materialized using XOR32rr, so also consider 8907 // super-registers. 8908 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg)) 8909 return None; 8910 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) 8911 return ParamLoadedValue(MachineOperand::CreateImm(0), Expr); 8912 return None; 8913 } 8914 case X86::MOVSX64rr32: { 8915 // We may need to describe the lower 32 bits of the MOVSX; for example, in 8916 // cases like this: 8917 // 8918 // $ebx = [...] 8919 // $rdi = MOVSX64rr32 $ebx 8920 // $esi = MOV32rr $edi 8921 if (!TRI->isSubRegisterEq(MI.getOperand(0).getReg(), Reg)) 8922 return None; 8923 8924 Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {}); 8925 8926 // If the described register is the destination register we need to 8927 // sign-extend the source register from 32 bits. The other case we handle 8928 // is when the described register is the 32-bit sub-register of the 8929 // destination register, in case we just need to return the source 8930 // register. 8931 if (Reg == MI.getOperand(0).getReg()) 8932 Expr = DIExpression::appendExt(Expr, 32, 64, true); 8933 else 8934 assert(X86MCRegisterClasses[X86::GR32RegClassID].contains(Reg) && 8935 "Unhandled sub-register case for MOVSX64rr32"); 8936 8937 return ParamLoadedValue(MI.getOperand(1), Expr); 8938 } 8939 default: 8940 assert(!MI.isMoveImmediate() && "Unexpected MoveImm instruction"); 8941 return TargetInstrInfo::describeLoadedValue(MI, Reg); 8942 } 8943 } 8944 8945 /// This is an architecture-specific helper function of reassociateOps. 8946 /// Set special operand attributes for new instructions after reassociation. 8947 void X86InstrInfo::setSpecialOperandAttr(MachineInstr &OldMI1, 8948 MachineInstr &OldMI2, 8949 MachineInstr &NewMI1, 8950 MachineInstr &NewMI2) const { 8951 // Propagate FP flags from the original instructions. 8952 // But clear poison-generating flags because those may not be valid now. 8953 // TODO: There should be a helper function for copying only fast-math-flags. 8954 uint16_t IntersectedFlags = OldMI1.getFlags() & OldMI2.getFlags(); 8955 NewMI1.setFlags(IntersectedFlags); 8956 NewMI1.clearFlag(MachineInstr::MIFlag::NoSWrap); 8957 NewMI1.clearFlag(MachineInstr::MIFlag::NoUWrap); 8958 NewMI1.clearFlag(MachineInstr::MIFlag::IsExact); 8959 8960 NewMI2.setFlags(IntersectedFlags); 8961 NewMI2.clearFlag(MachineInstr::MIFlag::NoSWrap); 8962 NewMI2.clearFlag(MachineInstr::MIFlag::NoUWrap); 8963 NewMI2.clearFlag(MachineInstr::MIFlag::IsExact); 8964 8965 // Integer instructions may define an implicit EFLAGS dest register operand. 8966 MachineOperand *OldFlagDef1 = OldMI1.findRegisterDefOperand(X86::EFLAGS); 8967 MachineOperand *OldFlagDef2 = OldMI2.findRegisterDefOperand(X86::EFLAGS); 8968 8969 assert(!OldFlagDef1 == !OldFlagDef2 && 8970 "Unexpected instruction type for reassociation"); 8971 8972 if (!OldFlagDef1 || !OldFlagDef2) 8973 return; 8974 8975 assert(OldFlagDef1->isDead() && OldFlagDef2->isDead() && 8976 "Must have dead EFLAGS operand in reassociable instruction"); 8977 8978 MachineOperand *NewFlagDef1 = NewMI1.findRegisterDefOperand(X86::EFLAGS); 8979 MachineOperand *NewFlagDef2 = NewMI2.findRegisterDefOperand(X86::EFLAGS); 8980 8981 assert(NewFlagDef1 && NewFlagDef2 && 8982 "Unexpected operand in reassociable instruction"); 8983 8984 // Mark the new EFLAGS operands as dead to be helpful to subsequent iterations 8985 // of this pass or other passes. The EFLAGS operands must be dead in these new 8986 // instructions because the EFLAGS operands in the original instructions must 8987 // be dead in order for reassociation to occur. 8988 NewFlagDef1->setIsDead(); 8989 NewFlagDef2->setIsDead(); 8990 } 8991 8992 std::pair<unsigned, unsigned> 8993 X86InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 8994 return std::make_pair(TF, 0u); 8995 } 8996 8997 ArrayRef<std::pair<unsigned, const char *>> 8998 X86InstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 8999 using namespace X86II; 9000 static const std::pair<unsigned, const char *> TargetFlags[] = { 9001 {MO_GOT_ABSOLUTE_ADDRESS, "x86-got-absolute-address"}, 9002 {MO_PIC_BASE_OFFSET, "x86-pic-base-offset"}, 9003 {MO_GOT, "x86-got"}, 9004 {MO_GOTOFF, "x86-gotoff"}, 9005 {MO_GOTPCREL, "x86-gotpcrel"}, 9006 {MO_GOTPCREL_NORELAX, "x86-gotpcrel-norelax"}, 9007 {MO_PLT, "x86-plt"}, 9008 {MO_TLSGD, "x86-tlsgd"}, 9009 {MO_TLSLD, "x86-tlsld"}, 9010 {MO_TLSLDM, "x86-tlsldm"}, 9011 {MO_GOTTPOFF, "x86-gottpoff"}, 9012 {MO_INDNTPOFF, "x86-indntpoff"}, 9013 {MO_TPOFF, "x86-tpoff"}, 9014 {MO_DTPOFF, "x86-dtpoff"}, 9015 {MO_NTPOFF, "x86-ntpoff"}, 9016 {MO_GOTNTPOFF, "x86-gotntpoff"}, 9017 {MO_DLLIMPORT, "x86-dllimport"}, 9018 {MO_DARWIN_NONLAZY, "x86-darwin-nonlazy"}, 9019 {MO_DARWIN_NONLAZY_PIC_BASE, "x86-darwin-nonlazy-pic-base"}, 9020 {MO_TLVP, "x86-tlvp"}, 9021 {MO_TLVP_PIC_BASE, "x86-tlvp-pic-base"}, 9022 {MO_SECREL, "x86-secrel"}, 9023 {MO_COFFSTUB, "x86-coffstub"}}; 9024 return makeArrayRef(TargetFlags); 9025 } 9026 9027 namespace { 9028 /// Create Global Base Reg pass. This initializes the PIC 9029 /// global base register for x86-32. 9030 struct CGBR : public MachineFunctionPass { 9031 static char ID; 9032 CGBR() : MachineFunctionPass(ID) {} 9033 9034 bool runOnMachineFunction(MachineFunction &MF) override { 9035 const X86TargetMachine *TM = 9036 static_cast<const X86TargetMachine *>(&MF.getTarget()); 9037 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>(); 9038 9039 // Don't do anything in the 64-bit small and kernel code models. They use 9040 // RIP-relative addressing for everything. 9041 if (STI.is64Bit() && (TM->getCodeModel() == CodeModel::Small || 9042 TM->getCodeModel() == CodeModel::Kernel)) 9043 return false; 9044 9045 // Only emit a global base reg in PIC mode. 9046 if (!TM->isPositionIndependent()) 9047 return false; 9048 9049 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 9050 Register GlobalBaseReg = X86FI->getGlobalBaseReg(); 9051 9052 // If we didn't need a GlobalBaseReg, don't insert code. 9053 if (GlobalBaseReg == 0) 9054 return false; 9055 9056 // Insert the set of GlobalBaseReg into the first MBB of the function 9057 MachineBasicBlock &FirstMBB = MF.front(); 9058 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 9059 DebugLoc DL = FirstMBB.findDebugLoc(MBBI); 9060 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 9061 const X86InstrInfo *TII = STI.getInstrInfo(); 9062 9063 Register PC; 9064 if (STI.isPICStyleGOT()) 9065 PC = RegInfo.createVirtualRegister(&X86::GR32RegClass); 9066 else 9067 PC = GlobalBaseReg; 9068 9069 if (STI.is64Bit()) { 9070 if (TM->getCodeModel() == CodeModel::Medium) { 9071 // In the medium code model, use a RIP-relative LEA to materialize the 9072 // GOT. 9073 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::LEA64r), PC) 9074 .addReg(X86::RIP) 9075 .addImm(0) 9076 .addReg(0) 9077 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_") 9078 .addReg(0); 9079 } else if (TM->getCodeModel() == CodeModel::Large) { 9080 // In the large code model, we are aiming for this code, though the 9081 // register allocation may vary: 9082 // leaq .LN$pb(%rip), %rax 9083 // movq $_GLOBAL_OFFSET_TABLE_ - .LN$pb, %rcx 9084 // addq %rcx, %rax 9085 // RAX now holds address of _GLOBAL_OFFSET_TABLE_. 9086 Register PBReg = RegInfo.createVirtualRegister(&X86::GR64RegClass); 9087 Register GOTReg = RegInfo.createVirtualRegister(&X86::GR64RegClass); 9088 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::LEA64r), PBReg) 9089 .addReg(X86::RIP) 9090 .addImm(0) 9091 .addReg(0) 9092 .addSym(MF.getPICBaseSymbol()) 9093 .addReg(0); 9094 std::prev(MBBI)->setPreInstrSymbol(MF, MF.getPICBaseSymbol()); 9095 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOV64ri), GOTReg) 9096 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_", 9097 X86II::MO_PIC_BASE_OFFSET); 9098 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD64rr), PC) 9099 .addReg(PBReg, RegState::Kill) 9100 .addReg(GOTReg, RegState::Kill); 9101 } else { 9102 llvm_unreachable("unexpected code model"); 9103 } 9104 } else { 9105 // Operand of MovePCtoStack is completely ignored by asm printer. It's 9106 // only used in JIT code emission as displacement to pc. 9107 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0); 9108 9109 // If we're using vanilla 'GOT' PIC style, we should use relative 9110 // addressing not to pc, but to _GLOBAL_OFFSET_TABLE_ external. 9111 if (STI.isPICStyleGOT()) { 9112 // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel], 9113 // %some_register 9114 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg) 9115 .addReg(PC) 9116 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_", 9117 X86II::MO_GOT_ABSOLUTE_ADDRESS); 9118 } 9119 } 9120 9121 return true; 9122 } 9123 9124 StringRef getPassName() const override { 9125 return "X86 PIC Global Base Reg Initialization"; 9126 } 9127 9128 void getAnalysisUsage(AnalysisUsage &AU) const override { 9129 AU.setPreservesCFG(); 9130 MachineFunctionPass::getAnalysisUsage(AU); 9131 } 9132 }; 9133 } // namespace 9134 9135 char CGBR::ID = 0; 9136 FunctionPass* 9137 llvm::createX86GlobalBaseRegPass() { return new CGBR(); } 9138 9139 namespace { 9140 struct LDTLSCleanup : public MachineFunctionPass { 9141 static char ID; 9142 LDTLSCleanup() : MachineFunctionPass(ID) {} 9143 9144 bool runOnMachineFunction(MachineFunction &MF) override { 9145 if (skipFunction(MF.getFunction())) 9146 return false; 9147 9148 X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>(); 9149 if (MFI->getNumLocalDynamicTLSAccesses() < 2) { 9150 // No point folding accesses if there isn't at least two. 9151 return false; 9152 } 9153 9154 MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>(); 9155 return VisitNode(DT->getRootNode(), 0); 9156 } 9157 9158 // Visit the dominator subtree rooted at Node in pre-order. 9159 // If TLSBaseAddrReg is non-null, then use that to replace any 9160 // TLS_base_addr instructions. Otherwise, create the register 9161 // when the first such instruction is seen, and then use it 9162 // as we encounter more instructions. 9163 bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) { 9164 MachineBasicBlock *BB = Node->getBlock(); 9165 bool Changed = false; 9166 9167 // Traverse the current block. 9168 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; 9169 ++I) { 9170 switch (I->getOpcode()) { 9171 case X86::TLS_base_addr32: 9172 case X86::TLS_base_addr64: 9173 if (TLSBaseAddrReg) 9174 I = ReplaceTLSBaseAddrCall(*I, TLSBaseAddrReg); 9175 else 9176 I = SetRegister(*I, &TLSBaseAddrReg); 9177 Changed = true; 9178 break; 9179 default: 9180 break; 9181 } 9182 } 9183 9184 // Visit the children of this block in the dominator tree. 9185 for (auto I = Node->begin(), E = Node->end(); I != E; ++I) { 9186 Changed |= VisitNode(*I, TLSBaseAddrReg); 9187 } 9188 9189 return Changed; 9190 } 9191 9192 // Replace the TLS_base_addr instruction I with a copy from 9193 // TLSBaseAddrReg, returning the new instruction. 9194 MachineInstr *ReplaceTLSBaseAddrCall(MachineInstr &I, 9195 unsigned TLSBaseAddrReg) { 9196 MachineFunction *MF = I.getParent()->getParent(); 9197 const X86Subtarget &STI = MF->getSubtarget<X86Subtarget>(); 9198 const bool is64Bit = STI.is64Bit(); 9199 const X86InstrInfo *TII = STI.getInstrInfo(); 9200 9201 // Insert a Copy from TLSBaseAddrReg to RAX/EAX. 9202 MachineInstr *Copy = 9203 BuildMI(*I.getParent(), I, I.getDebugLoc(), 9204 TII->get(TargetOpcode::COPY), is64Bit ? X86::RAX : X86::EAX) 9205 .addReg(TLSBaseAddrReg); 9206 9207 // Erase the TLS_base_addr instruction. 9208 I.eraseFromParent(); 9209 9210 return Copy; 9211 } 9212 9213 // Create a virtual register in *TLSBaseAddrReg, and populate it by 9214 // inserting a copy instruction after I. Returns the new instruction. 9215 MachineInstr *SetRegister(MachineInstr &I, unsigned *TLSBaseAddrReg) { 9216 MachineFunction *MF = I.getParent()->getParent(); 9217 const X86Subtarget &STI = MF->getSubtarget<X86Subtarget>(); 9218 const bool is64Bit = STI.is64Bit(); 9219 const X86InstrInfo *TII = STI.getInstrInfo(); 9220 9221 // Create a virtual register for the TLS base address. 9222 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 9223 *TLSBaseAddrReg = RegInfo.createVirtualRegister(is64Bit 9224 ? &X86::GR64RegClass 9225 : &X86::GR32RegClass); 9226 9227 // Insert a copy from RAX/EAX to TLSBaseAddrReg. 9228 MachineInstr *Next = I.getNextNode(); 9229 MachineInstr *Copy = 9230 BuildMI(*I.getParent(), Next, I.getDebugLoc(), 9231 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg) 9232 .addReg(is64Bit ? X86::RAX : X86::EAX); 9233 9234 return Copy; 9235 } 9236 9237 StringRef getPassName() const override { 9238 return "Local Dynamic TLS Access Clean-up"; 9239 } 9240 9241 void getAnalysisUsage(AnalysisUsage &AU) const override { 9242 AU.setPreservesCFG(); 9243 AU.addRequired<MachineDominatorTree>(); 9244 MachineFunctionPass::getAnalysisUsage(AU); 9245 } 9246 }; 9247 } 9248 9249 char LDTLSCleanup::ID = 0; 9250 FunctionPass* 9251 llvm::createCleanupLocalDynamicTLSPass() { return new LDTLSCleanup(); } 9252 9253 /// Constants defining how certain sequences should be outlined. 9254 /// 9255 /// \p MachineOutlinerDefault implies that the function is called with a call 9256 /// instruction, and a return must be emitted for the outlined function frame. 9257 /// 9258 /// That is, 9259 /// 9260 /// I1 OUTLINED_FUNCTION: 9261 /// I2 --> call OUTLINED_FUNCTION I1 9262 /// I3 I2 9263 /// I3 9264 /// ret 9265 /// 9266 /// * Call construction overhead: 1 (call instruction) 9267 /// * Frame construction overhead: 1 (return instruction) 9268 /// 9269 /// \p MachineOutlinerTailCall implies that the function is being tail called. 9270 /// A jump is emitted instead of a call, and the return is already present in 9271 /// the outlined sequence. That is, 9272 /// 9273 /// I1 OUTLINED_FUNCTION: 9274 /// I2 --> jmp OUTLINED_FUNCTION I1 9275 /// ret I2 9276 /// ret 9277 /// 9278 /// * Call construction overhead: 1 (jump instruction) 9279 /// * Frame construction overhead: 0 (don't need to return) 9280 /// 9281 enum MachineOutlinerClass { 9282 MachineOutlinerDefault, 9283 MachineOutlinerTailCall 9284 }; 9285 9286 outliner::OutlinedFunction X86InstrInfo::getOutliningCandidateInfo( 9287 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const { 9288 unsigned SequenceSize = 9289 std::accumulate(RepeatedSequenceLocs[0].front(), 9290 std::next(RepeatedSequenceLocs[0].back()), 0, 9291 [](unsigned Sum, const MachineInstr &MI) { 9292 // FIXME: x86 doesn't implement getInstSizeInBytes, so 9293 // we can't tell the cost. Just assume each instruction 9294 // is one byte. 9295 if (MI.isDebugInstr() || MI.isKill()) 9296 return Sum; 9297 return Sum + 1; 9298 }); 9299 9300 // We check to see if CFI Instructions are present, and if they are 9301 // we find the number of CFI Instructions in the candidates. 9302 unsigned CFICount = 0; 9303 MachineBasicBlock::iterator MBBI = RepeatedSequenceLocs[0].front(); 9304 for (unsigned Loc = RepeatedSequenceLocs[0].getStartIdx(); 9305 Loc < RepeatedSequenceLocs[0].getEndIdx() + 1; Loc++) { 9306 if (MBBI->isCFIInstruction()) 9307 CFICount++; 9308 MBBI++; 9309 } 9310 9311 // We compare the number of found CFI Instructions to the number of CFI 9312 // instructions in the parent function for each candidate. We must check this 9313 // since if we outline one of the CFI instructions in a function, we have to 9314 // outline them all for correctness. If we do not, the address offsets will be 9315 // incorrect between the two sections of the program. 9316 for (outliner::Candidate &C : RepeatedSequenceLocs) { 9317 std::vector<MCCFIInstruction> CFIInstructions = 9318 C.getMF()->getFrameInstructions(); 9319 9320 if (CFICount > 0 && CFICount != CFIInstructions.size()) 9321 return outliner::OutlinedFunction(); 9322 } 9323 9324 // FIXME: Use real size in bytes for call and ret instructions. 9325 if (RepeatedSequenceLocs[0].back()->isTerminator()) { 9326 for (outliner::Candidate &C : RepeatedSequenceLocs) 9327 C.setCallInfo(MachineOutlinerTailCall, 1); 9328 9329 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize, 9330 0, // Number of bytes to emit frame. 9331 MachineOutlinerTailCall // Type of frame. 9332 ); 9333 } 9334 9335 if (CFICount > 0) 9336 return outliner::OutlinedFunction(); 9337 9338 for (outliner::Candidate &C : RepeatedSequenceLocs) 9339 C.setCallInfo(MachineOutlinerDefault, 1); 9340 9341 return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize, 1, 9342 MachineOutlinerDefault); 9343 } 9344 9345 bool X86InstrInfo::isFunctionSafeToOutlineFrom(MachineFunction &MF, 9346 bool OutlineFromLinkOnceODRs) const { 9347 const Function &F = MF.getFunction(); 9348 9349 // Does the function use a red zone? If it does, then we can't risk messing 9350 // with the stack. 9351 if (Subtarget.getFrameLowering()->has128ByteRedZone(MF)) { 9352 // It could have a red zone. If it does, then we don't want to touch it. 9353 const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 9354 if (!X86FI || X86FI->getUsesRedZone()) 9355 return false; 9356 } 9357 9358 // If we *don't* want to outline from things that could potentially be deduped 9359 // then return false. 9360 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage()) 9361 return false; 9362 9363 // This function is viable for outlining, so return true. 9364 return true; 9365 } 9366 9367 outliner::InstrType 9368 X86InstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const { 9369 MachineInstr &MI = *MIT; 9370 // Don't allow debug values to impact outlining type. 9371 if (MI.isDebugInstr() || MI.isIndirectDebugValue()) 9372 return outliner::InstrType::Invisible; 9373 9374 // At this point, KILL instructions don't really tell us much so we can go 9375 // ahead and skip over them. 9376 if (MI.isKill()) 9377 return outliner::InstrType::Invisible; 9378 9379 // Is this a tail call? If yes, we can outline as a tail call. 9380 if (isTailCall(MI)) 9381 return outliner::InstrType::Legal; 9382 9383 // Is this the terminator of a basic block? 9384 if (MI.isTerminator() || MI.isReturn()) { 9385 9386 // Does its parent have any successors in its MachineFunction? 9387 if (MI.getParent()->succ_empty()) 9388 return outliner::InstrType::Legal; 9389 9390 // It does, so we can't tail call it. 9391 return outliner::InstrType::Illegal; 9392 } 9393 9394 // Don't outline anything that modifies or reads from the stack pointer. 9395 // 9396 // FIXME: There are instructions which are being manually built without 9397 // explicit uses/defs so we also have to check the MCInstrDesc. We should be 9398 // able to remove the extra checks once those are fixed up. For example, 9399 // sometimes we might get something like %rax = POP64r 1. This won't be 9400 // caught by modifiesRegister or readsRegister even though the instruction 9401 // really ought to be formed so that modifiesRegister/readsRegister would 9402 // catch it. 9403 if (MI.modifiesRegister(X86::RSP, &RI) || MI.readsRegister(X86::RSP, &RI) || 9404 MI.getDesc().hasImplicitUseOfPhysReg(X86::RSP) || 9405 MI.getDesc().hasImplicitDefOfPhysReg(X86::RSP)) 9406 return outliner::InstrType::Illegal; 9407 9408 // Outlined calls change the instruction pointer, so don't read from it. 9409 if (MI.readsRegister(X86::RIP, &RI) || 9410 MI.getDesc().hasImplicitUseOfPhysReg(X86::RIP) || 9411 MI.getDesc().hasImplicitDefOfPhysReg(X86::RIP)) 9412 return outliner::InstrType::Illegal; 9413 9414 // Positions can't safely be outlined. 9415 if (MI.isPosition()) 9416 return outliner::InstrType::Illegal; 9417 9418 // Make sure none of the operands of this instruction do anything tricky. 9419 for (const MachineOperand &MOP : MI.operands()) 9420 if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() || 9421 MOP.isTargetIndex()) 9422 return outliner::InstrType::Illegal; 9423 9424 return outliner::InstrType::Legal; 9425 } 9426 9427 void X86InstrInfo::buildOutlinedFrame(MachineBasicBlock &MBB, 9428 MachineFunction &MF, 9429 const outliner::OutlinedFunction &OF) 9430 const { 9431 // If we're a tail call, we already have a return, so don't do anything. 9432 if (OF.FrameConstructionID == MachineOutlinerTailCall) 9433 return; 9434 9435 // We're a normal call, so our sequence doesn't have a return instruction. 9436 // Add it in. 9437 MachineInstr *retq = BuildMI(MF, DebugLoc(), get(X86::RET64)); 9438 MBB.insert(MBB.end(), retq); 9439 } 9440 9441 MachineBasicBlock::iterator 9442 X86InstrInfo::insertOutlinedCall(Module &M, MachineBasicBlock &MBB, 9443 MachineBasicBlock::iterator &It, 9444 MachineFunction &MF, 9445 const outliner::Candidate &C) const { 9446 // Is it a tail call? 9447 if (C.CallConstructionID == MachineOutlinerTailCall) { 9448 // Yes, just insert a JMP. 9449 It = MBB.insert(It, 9450 BuildMI(MF, DebugLoc(), get(X86::TAILJMPd64)) 9451 .addGlobalAddress(M.getNamedValue(MF.getName()))); 9452 } else { 9453 // No, insert a call. 9454 It = MBB.insert(It, 9455 BuildMI(MF, DebugLoc(), get(X86::CALL64pcrel32)) 9456 .addGlobalAddress(M.getNamedValue(MF.getName()))); 9457 } 9458 9459 return It; 9460 } 9461 9462 #define GET_INSTRINFO_HELPERS 9463 #include "X86GenInstrInfo.inc" 9464