1 //===-- X86MCInstLower.cpp - Convert X86 MachineInstr to an MCInst --------===// 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 code to lower X86 MachineInstrs to their corresponding 10 // MCInst records. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MCTargetDesc/X86ATTInstPrinter.h" 15 #include "MCTargetDesc/X86BaseInfo.h" 16 #include "MCTargetDesc/X86InstComments.h" 17 #include "MCTargetDesc/X86ShuffleDecode.h" 18 #include "MCTargetDesc/X86TargetStreamer.h" 19 #include "X86AsmPrinter.h" 20 #include "X86RegisterInfo.h" 21 #include "X86ShuffleDecodeConstantPool.h" 22 #include "X86Subtarget.h" 23 #include "llvm/ADT/Optional.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/ADT/iterator_range.h" 26 #include "llvm/CodeGen/MachineConstantPool.h" 27 #include "llvm/CodeGen/MachineFunction.h" 28 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 29 #include "llvm/CodeGen/MachineOperand.h" 30 #include "llvm/CodeGen/StackMaps.h" 31 #include "llvm/IR/DataLayout.h" 32 #include "llvm/IR/GlobalValue.h" 33 #include "llvm/IR/Mangler.h" 34 #include "llvm/MC/MCAsmInfo.h" 35 #include "llvm/MC/MCCodeEmitter.h" 36 #include "llvm/MC/MCContext.h" 37 #include "llvm/MC/MCExpr.h" 38 #include "llvm/MC/MCFixup.h" 39 #include "llvm/MC/MCInst.h" 40 #include "llvm/MC/MCInstBuilder.h" 41 #include "llvm/MC/MCSection.h" 42 #include "llvm/MC/MCSectionELF.h" 43 #include "llvm/MC/MCStreamer.h" 44 #include "llvm/MC/MCSymbol.h" 45 #include "llvm/MC/MCSymbolELF.h" 46 #include "llvm/Target/TargetLoweringObjectFile.h" 47 #include "llvm/Target/TargetMachine.h" 48 49 using namespace llvm; 50 51 namespace { 52 53 /// X86MCInstLower - This class is used to lower an MachineInstr into an MCInst. 54 class X86MCInstLower { 55 MCContext &Ctx; 56 const MachineFunction &MF; 57 const TargetMachine &TM; 58 const MCAsmInfo &MAI; 59 X86AsmPrinter &AsmPrinter; 60 61 public: 62 X86MCInstLower(const MachineFunction &MF, X86AsmPrinter &asmprinter); 63 64 Optional<MCOperand> LowerMachineOperand(const MachineInstr *MI, 65 const MachineOperand &MO) const; 66 void Lower(const MachineInstr *MI, MCInst &OutMI) const; 67 68 MCSymbol *GetSymbolFromOperand(const MachineOperand &MO) const; 69 MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const; 70 71 private: 72 MachineModuleInfoMachO &getMachOMMI() const; 73 }; 74 75 } // end anonymous namespace 76 77 /// A RAII helper which defines a region of instructions which can't have 78 /// padding added between them for correctness. 79 struct NoAutoPaddingScope { 80 MCStreamer &OS; 81 const bool OldAllowAutoPadding; 82 NoAutoPaddingScope(MCStreamer &OS) 83 : OS(OS), OldAllowAutoPadding(OS.getAllowAutoPadding()) { 84 changeAndComment(false); 85 } 86 ~NoAutoPaddingScope() { changeAndComment(OldAllowAutoPadding); } 87 void changeAndComment(bool b) { 88 if (b == OS.getAllowAutoPadding()) 89 return; 90 OS.setAllowAutoPadding(b); 91 if (b) 92 OS.emitRawComment("autopadding"); 93 else 94 OS.emitRawComment("noautopadding"); 95 } 96 }; 97 98 // Emit a minimal sequence of nops spanning NumBytes bytes. 99 static void emitX86Nops(MCStreamer &OS, unsigned NumBytes, 100 const X86Subtarget *Subtarget); 101 102 void X86AsmPrinter::StackMapShadowTracker::count(MCInst &Inst, 103 const MCSubtargetInfo &STI, 104 MCCodeEmitter *CodeEmitter) { 105 if (InShadow) { 106 SmallString<256> Code; 107 SmallVector<MCFixup, 4> Fixups; 108 raw_svector_ostream VecOS(Code); 109 CodeEmitter->encodeInstruction(Inst, VecOS, Fixups, STI); 110 CurrentShadowSize += Code.size(); 111 if (CurrentShadowSize >= RequiredShadowSize) 112 InShadow = false; // The shadow is big enough. Stop counting. 113 } 114 } 115 116 void X86AsmPrinter::StackMapShadowTracker::emitShadowPadding( 117 MCStreamer &OutStreamer, const MCSubtargetInfo &STI) { 118 if (InShadow && CurrentShadowSize < RequiredShadowSize) { 119 InShadow = false; 120 emitX86Nops(OutStreamer, RequiredShadowSize - CurrentShadowSize, 121 &MF->getSubtarget<X86Subtarget>()); 122 } 123 } 124 125 void X86AsmPrinter::EmitAndCountInstruction(MCInst &Inst) { 126 OutStreamer->emitInstruction(Inst, getSubtargetInfo()); 127 SMShadowTracker.count(Inst, getSubtargetInfo(), CodeEmitter.get()); 128 } 129 130 X86MCInstLower::X86MCInstLower(const MachineFunction &mf, 131 X86AsmPrinter &asmprinter) 132 : Ctx(mf.getContext()), MF(mf), TM(mf.getTarget()), MAI(*TM.getMCAsmInfo()), 133 AsmPrinter(asmprinter) {} 134 135 MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const { 136 return MF.getMMI().getObjFileInfo<MachineModuleInfoMachO>(); 137 } 138 139 /// GetSymbolFromOperand - Lower an MO_GlobalAddress or MO_ExternalSymbol 140 /// operand to an MCSymbol. 141 MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO) const { 142 const Triple &TT = TM.getTargetTriple(); 143 if (MO.isGlobal() && TT.isOSBinFormatELF()) 144 return AsmPrinter.getSymbolPreferLocal(*MO.getGlobal()); 145 146 const DataLayout &DL = MF.getDataLayout(); 147 assert((MO.isGlobal() || MO.isSymbol() || MO.isMBB()) && 148 "Isn't a symbol reference"); 149 150 MCSymbol *Sym = nullptr; 151 SmallString<128> Name; 152 StringRef Suffix; 153 154 switch (MO.getTargetFlags()) { 155 case X86II::MO_DLLIMPORT: 156 // Handle dllimport linkage. 157 Name += "__imp_"; 158 break; 159 case X86II::MO_COFFSTUB: 160 Name += ".refptr."; 161 break; 162 case X86II::MO_DARWIN_NONLAZY: 163 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: 164 Suffix = "$non_lazy_ptr"; 165 break; 166 } 167 168 if (!Suffix.empty()) 169 Name += DL.getPrivateGlobalPrefix(); 170 171 if (MO.isGlobal()) { 172 const GlobalValue *GV = MO.getGlobal(); 173 AsmPrinter.getNameWithPrefix(Name, GV); 174 } else if (MO.isSymbol()) { 175 Mangler::getNameWithPrefix(Name, MO.getSymbolName(), DL); 176 } else if (MO.isMBB()) { 177 assert(Suffix.empty()); 178 Sym = MO.getMBB()->getSymbol(); 179 } 180 181 Name += Suffix; 182 if (!Sym) 183 Sym = Ctx.getOrCreateSymbol(Name); 184 185 // If the target flags on the operand changes the name of the symbol, do that 186 // before we return the symbol. 187 switch (MO.getTargetFlags()) { 188 default: 189 break; 190 case X86II::MO_COFFSTUB: { 191 MachineModuleInfoCOFF &MMICOFF = 192 MF.getMMI().getObjFileInfo<MachineModuleInfoCOFF>(); 193 MachineModuleInfoImpl::StubValueTy &StubSym = MMICOFF.getGVStubEntry(Sym); 194 if (!StubSym.getPointer()) { 195 assert(MO.isGlobal() && "Extern symbol not handled yet"); 196 StubSym = MachineModuleInfoImpl::StubValueTy( 197 AsmPrinter.getSymbol(MO.getGlobal()), true); 198 } 199 break; 200 } 201 case X86II::MO_DARWIN_NONLAZY: 202 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: { 203 MachineModuleInfoImpl::StubValueTy &StubSym = 204 getMachOMMI().getGVStubEntry(Sym); 205 if (!StubSym.getPointer()) { 206 assert(MO.isGlobal() && "Extern symbol not handled yet"); 207 StubSym = MachineModuleInfoImpl::StubValueTy( 208 AsmPrinter.getSymbol(MO.getGlobal()), 209 !MO.getGlobal()->hasInternalLinkage()); 210 } 211 break; 212 } 213 } 214 215 return Sym; 216 } 217 218 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO, 219 MCSymbol *Sym) const { 220 // FIXME: We would like an efficient form for this, so we don't have to do a 221 // lot of extra uniquing. 222 const MCExpr *Expr = nullptr; 223 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None; 224 225 switch (MO.getTargetFlags()) { 226 default: 227 llvm_unreachable("Unknown target flag on GV operand"); 228 case X86II::MO_NO_FLAG: // No flag. 229 // These affect the name of the symbol, not any suffix. 230 case X86II::MO_DARWIN_NONLAZY: 231 case X86II::MO_DLLIMPORT: 232 case X86II::MO_COFFSTUB: 233 break; 234 235 case X86II::MO_TLVP: 236 RefKind = MCSymbolRefExpr::VK_TLVP; 237 break; 238 case X86II::MO_TLVP_PIC_BASE: 239 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_TLVP, Ctx); 240 // Subtract the pic base. 241 Expr = MCBinaryExpr::createSub( 242 Expr, MCSymbolRefExpr::create(MF.getPICBaseSymbol(), Ctx), Ctx); 243 break; 244 case X86II::MO_SECREL: 245 RefKind = MCSymbolRefExpr::VK_SECREL; 246 break; 247 case X86II::MO_TLSGD: 248 RefKind = MCSymbolRefExpr::VK_TLSGD; 249 break; 250 case X86II::MO_TLSLD: 251 RefKind = MCSymbolRefExpr::VK_TLSLD; 252 break; 253 case X86II::MO_TLSLDM: 254 RefKind = MCSymbolRefExpr::VK_TLSLDM; 255 break; 256 case X86II::MO_GOTTPOFF: 257 RefKind = MCSymbolRefExpr::VK_GOTTPOFF; 258 break; 259 case X86II::MO_INDNTPOFF: 260 RefKind = MCSymbolRefExpr::VK_INDNTPOFF; 261 break; 262 case X86II::MO_TPOFF: 263 RefKind = MCSymbolRefExpr::VK_TPOFF; 264 break; 265 case X86II::MO_DTPOFF: 266 RefKind = MCSymbolRefExpr::VK_DTPOFF; 267 break; 268 case X86II::MO_NTPOFF: 269 RefKind = MCSymbolRefExpr::VK_NTPOFF; 270 break; 271 case X86II::MO_GOTNTPOFF: 272 RefKind = MCSymbolRefExpr::VK_GOTNTPOFF; 273 break; 274 case X86II::MO_GOTPCREL: 275 RefKind = MCSymbolRefExpr::VK_GOTPCREL; 276 break; 277 case X86II::MO_GOT: 278 RefKind = MCSymbolRefExpr::VK_GOT; 279 break; 280 case X86II::MO_GOTOFF: 281 RefKind = MCSymbolRefExpr::VK_GOTOFF; 282 break; 283 case X86II::MO_PLT: 284 RefKind = MCSymbolRefExpr::VK_PLT; 285 break; 286 case X86II::MO_ABS8: 287 RefKind = MCSymbolRefExpr::VK_X86_ABS8; 288 break; 289 case X86II::MO_PIC_BASE_OFFSET: 290 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: 291 Expr = MCSymbolRefExpr::create(Sym, Ctx); 292 // Subtract the pic base. 293 Expr = MCBinaryExpr::createSub( 294 Expr, MCSymbolRefExpr::create(MF.getPICBaseSymbol(), Ctx), Ctx); 295 if (MO.isJTI()) { 296 assert(MAI.doesSetDirectiveSuppressReloc()); 297 // If .set directive is supported, use it to reduce the number of 298 // relocations the assembler will generate for differences between 299 // local labels. This is only safe when the symbols are in the same 300 // section so we are restricting it to jumptable references. 301 MCSymbol *Label = Ctx.createTempSymbol(); 302 AsmPrinter.OutStreamer->emitAssignment(Label, Expr); 303 Expr = MCSymbolRefExpr::create(Label, Ctx); 304 } 305 break; 306 } 307 308 if (!Expr) 309 Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx); 310 311 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset()) 312 Expr = MCBinaryExpr::createAdd( 313 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx); 314 return MCOperand::createExpr(Expr); 315 } 316 317 /// Simplify FOO $imm, %{al,ax,eax,rax} to FOO $imm, for instruction with 318 /// a short fixed-register form. 319 static void SimplifyShortImmForm(MCInst &Inst, unsigned Opcode) { 320 unsigned ImmOp = Inst.getNumOperands() - 1; 321 assert(Inst.getOperand(0).isReg() && 322 (Inst.getOperand(ImmOp).isImm() || Inst.getOperand(ImmOp).isExpr()) && 323 ((Inst.getNumOperands() == 3 && Inst.getOperand(1).isReg() && 324 Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) || 325 Inst.getNumOperands() == 2) && 326 "Unexpected instruction!"); 327 328 // Check whether the destination register can be fixed. 329 unsigned Reg = Inst.getOperand(0).getReg(); 330 if (Reg != X86::AL && Reg != X86::AX && Reg != X86::EAX && Reg != X86::RAX) 331 return; 332 333 // If so, rewrite the instruction. 334 MCOperand Saved = Inst.getOperand(ImmOp); 335 Inst = MCInst(); 336 Inst.setOpcode(Opcode); 337 Inst.addOperand(Saved); 338 } 339 340 /// If a movsx instruction has a shorter encoding for the used register 341 /// simplify the instruction to use it instead. 342 static void SimplifyMOVSX(MCInst &Inst) { 343 unsigned NewOpcode = 0; 344 unsigned Op0 = Inst.getOperand(0).getReg(), Op1 = Inst.getOperand(1).getReg(); 345 switch (Inst.getOpcode()) { 346 default: 347 llvm_unreachable("Unexpected instruction!"); 348 case X86::MOVSX16rr8: // movsbw %al, %ax --> cbtw 349 if (Op0 == X86::AX && Op1 == X86::AL) 350 NewOpcode = X86::CBW; 351 break; 352 case X86::MOVSX32rr16: // movswl %ax, %eax --> cwtl 353 if (Op0 == X86::EAX && Op1 == X86::AX) 354 NewOpcode = X86::CWDE; 355 break; 356 case X86::MOVSX64rr32: // movslq %eax, %rax --> cltq 357 if (Op0 == X86::RAX && Op1 == X86::EAX) 358 NewOpcode = X86::CDQE; 359 break; 360 } 361 362 if (NewOpcode != 0) { 363 Inst = MCInst(); 364 Inst.setOpcode(NewOpcode); 365 } 366 } 367 368 /// Simplify things like MOV32rm to MOV32o32a. 369 static void SimplifyShortMoveForm(X86AsmPrinter &Printer, MCInst &Inst, 370 unsigned Opcode) { 371 // Don't make these simplifications in 64-bit mode; other assemblers don't 372 // perform them because they make the code larger. 373 if (Printer.getSubtarget().is64Bit()) 374 return; 375 376 bool IsStore = Inst.getOperand(0).isReg() && Inst.getOperand(1).isReg(); 377 unsigned AddrBase = IsStore; 378 unsigned RegOp = IsStore ? 0 : 5; 379 unsigned AddrOp = AddrBase + 3; 380 assert( 381 Inst.getNumOperands() == 6 && Inst.getOperand(RegOp).isReg() && 382 Inst.getOperand(AddrBase + X86::AddrBaseReg).isReg() && 383 Inst.getOperand(AddrBase + X86::AddrScaleAmt).isImm() && 384 Inst.getOperand(AddrBase + X86::AddrIndexReg).isReg() && 385 Inst.getOperand(AddrBase + X86::AddrSegmentReg).isReg() && 386 (Inst.getOperand(AddrOp).isExpr() || Inst.getOperand(AddrOp).isImm()) && 387 "Unexpected instruction!"); 388 389 // Check whether the destination register can be fixed. 390 unsigned Reg = Inst.getOperand(RegOp).getReg(); 391 if (Reg != X86::AL && Reg != X86::AX && Reg != X86::EAX && Reg != X86::RAX) 392 return; 393 394 // Check whether this is an absolute address. 395 // FIXME: We know TLVP symbol refs aren't, but there should be a better way 396 // to do this here. 397 bool Absolute = true; 398 if (Inst.getOperand(AddrOp).isExpr()) { 399 const MCExpr *MCE = Inst.getOperand(AddrOp).getExpr(); 400 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(MCE)) 401 if (SRE->getKind() == MCSymbolRefExpr::VK_TLVP) 402 Absolute = false; 403 } 404 405 if (Absolute && 406 (Inst.getOperand(AddrBase + X86::AddrBaseReg).getReg() != 0 || 407 Inst.getOperand(AddrBase + X86::AddrScaleAmt).getImm() != 1 || 408 Inst.getOperand(AddrBase + X86::AddrIndexReg).getReg() != 0)) 409 return; 410 411 // If so, rewrite the instruction. 412 MCOperand Saved = Inst.getOperand(AddrOp); 413 MCOperand Seg = Inst.getOperand(AddrBase + X86::AddrSegmentReg); 414 Inst = MCInst(); 415 Inst.setOpcode(Opcode); 416 Inst.addOperand(Saved); 417 Inst.addOperand(Seg); 418 } 419 420 static unsigned getRetOpcode(const X86Subtarget &Subtarget) { 421 return Subtarget.is64Bit() ? X86::RETQ : X86::RETL; 422 } 423 424 Optional<MCOperand> 425 X86MCInstLower::LowerMachineOperand(const MachineInstr *MI, 426 const MachineOperand &MO) const { 427 switch (MO.getType()) { 428 default: 429 MI->print(errs()); 430 llvm_unreachable("unknown operand type"); 431 case MachineOperand::MO_Register: 432 // Ignore all implicit register operands. 433 if (MO.isImplicit()) 434 return None; 435 return MCOperand::createReg(MO.getReg()); 436 case MachineOperand::MO_Immediate: 437 return MCOperand::createImm(MO.getImm()); 438 case MachineOperand::MO_MachineBasicBlock: 439 case MachineOperand::MO_GlobalAddress: 440 case MachineOperand::MO_ExternalSymbol: 441 return LowerSymbolOperand(MO, GetSymbolFromOperand(MO)); 442 case MachineOperand::MO_MCSymbol: 443 return LowerSymbolOperand(MO, MO.getMCSymbol()); 444 case MachineOperand::MO_JumpTableIndex: 445 return LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex())); 446 case MachineOperand::MO_ConstantPoolIndex: 447 return LowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex())); 448 case MachineOperand::MO_BlockAddress: 449 return LowerSymbolOperand( 450 MO, AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress())); 451 case MachineOperand::MO_RegisterMask: 452 // Ignore call clobbers. 453 return None; 454 } 455 } 456 457 // Replace TAILJMP opcodes with their equivalent opcodes that have encoding 458 // information. 459 static unsigned convertTailJumpOpcode(unsigned Opcode) { 460 switch (Opcode) { 461 case X86::TAILJMPr: 462 Opcode = X86::JMP32r; 463 break; 464 case X86::TAILJMPm: 465 Opcode = X86::JMP32m; 466 break; 467 case X86::TAILJMPr64: 468 Opcode = X86::JMP64r; 469 break; 470 case X86::TAILJMPm64: 471 Opcode = X86::JMP64m; 472 break; 473 case X86::TAILJMPr64_REX: 474 Opcode = X86::JMP64r_REX; 475 break; 476 case X86::TAILJMPm64_REX: 477 Opcode = X86::JMP64m_REX; 478 break; 479 case X86::TAILJMPd: 480 case X86::TAILJMPd64: 481 Opcode = X86::JMP_1; 482 break; 483 case X86::TAILJMPd_CC: 484 case X86::TAILJMPd64_CC: 485 Opcode = X86::JCC_1; 486 break; 487 } 488 489 return Opcode; 490 } 491 492 void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { 493 OutMI.setOpcode(MI->getOpcode()); 494 495 for (const MachineOperand &MO : MI->operands()) 496 if (auto MaybeMCOp = LowerMachineOperand(MI, MO)) 497 OutMI.addOperand(MaybeMCOp.getValue()); 498 499 // Handle a few special cases to eliminate operand modifiers. 500 switch (OutMI.getOpcode()) { 501 case X86::LEA64_32r: 502 case X86::LEA64r: 503 case X86::LEA16r: 504 case X86::LEA32r: 505 // LEA should have a segment register, but it must be empty. 506 assert(OutMI.getNumOperands() == 1 + X86::AddrNumOperands && 507 "Unexpected # of LEA operands"); 508 assert(OutMI.getOperand(1 + X86::AddrSegmentReg).getReg() == 0 && 509 "LEA has segment specified!"); 510 break; 511 512 case X86::MULX32Hrr: 513 case X86::MULX32Hrm: 514 case X86::MULX64Hrr: 515 case X86::MULX64Hrm: { 516 // Turn into regular MULX by duplicating the destination. 517 unsigned NewOpc; 518 switch (OutMI.getOpcode()) { 519 default: llvm_unreachable("Invalid opcode"); 520 case X86::MULX32Hrr: NewOpc = X86::MULX32rr; break; 521 case X86::MULX32Hrm: NewOpc = X86::MULX32rm; break; 522 case X86::MULX64Hrr: NewOpc = X86::MULX64rr; break; 523 case X86::MULX64Hrm: NewOpc = X86::MULX64rm; break; 524 } 525 OutMI.setOpcode(NewOpc); 526 // Duplicate the destination. 527 unsigned DestReg = OutMI.getOperand(0).getReg(); 528 OutMI.insert(OutMI.begin(), MCOperand::createReg(DestReg)); 529 break; 530 } 531 532 // Commute operands to get a smaller encoding by using VEX.R instead of VEX.B 533 // if one of the registers is extended, but other isn't. 534 case X86::VMOVZPQILo2PQIrr: 535 case X86::VMOVAPDrr: 536 case X86::VMOVAPDYrr: 537 case X86::VMOVAPSrr: 538 case X86::VMOVAPSYrr: 539 case X86::VMOVDQArr: 540 case X86::VMOVDQAYrr: 541 case X86::VMOVDQUrr: 542 case X86::VMOVDQUYrr: 543 case X86::VMOVUPDrr: 544 case X86::VMOVUPDYrr: 545 case X86::VMOVUPSrr: 546 case X86::VMOVUPSYrr: { 547 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(0).getReg()) && 548 X86II::isX86_64ExtendedReg(OutMI.getOperand(1).getReg())) { 549 unsigned NewOpc; 550 switch (OutMI.getOpcode()) { 551 default: llvm_unreachable("Invalid opcode"); 552 case X86::VMOVZPQILo2PQIrr: NewOpc = X86::VMOVPQI2QIrr; break; 553 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break; 554 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break; 555 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break; 556 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break; 557 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break; 558 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break; 559 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break; 560 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break; 561 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break; 562 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break; 563 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break; 564 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break; 565 } 566 OutMI.setOpcode(NewOpc); 567 } 568 break; 569 } 570 case X86::VMOVSDrr: 571 case X86::VMOVSSrr: { 572 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(0).getReg()) && 573 X86II::isX86_64ExtendedReg(OutMI.getOperand(2).getReg())) { 574 unsigned NewOpc; 575 switch (OutMI.getOpcode()) { 576 default: llvm_unreachable("Invalid opcode"); 577 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break; 578 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break; 579 } 580 OutMI.setOpcode(NewOpc); 581 } 582 break; 583 } 584 585 case X86::VPCMPBZ128rmi: case X86::VPCMPBZ128rmik: 586 case X86::VPCMPBZ128rri: case X86::VPCMPBZ128rrik: 587 case X86::VPCMPBZ256rmi: case X86::VPCMPBZ256rmik: 588 case X86::VPCMPBZ256rri: case X86::VPCMPBZ256rrik: 589 case X86::VPCMPBZrmi: case X86::VPCMPBZrmik: 590 case X86::VPCMPBZrri: case X86::VPCMPBZrrik: 591 case X86::VPCMPDZ128rmi: case X86::VPCMPDZ128rmik: 592 case X86::VPCMPDZ128rmib: case X86::VPCMPDZ128rmibk: 593 case X86::VPCMPDZ128rri: case X86::VPCMPDZ128rrik: 594 case X86::VPCMPDZ256rmi: case X86::VPCMPDZ256rmik: 595 case X86::VPCMPDZ256rmib: case X86::VPCMPDZ256rmibk: 596 case X86::VPCMPDZ256rri: case X86::VPCMPDZ256rrik: 597 case X86::VPCMPDZrmi: case X86::VPCMPDZrmik: 598 case X86::VPCMPDZrmib: case X86::VPCMPDZrmibk: 599 case X86::VPCMPDZrri: case X86::VPCMPDZrrik: 600 case X86::VPCMPQZ128rmi: case X86::VPCMPQZ128rmik: 601 case X86::VPCMPQZ128rmib: case X86::VPCMPQZ128rmibk: 602 case X86::VPCMPQZ128rri: case X86::VPCMPQZ128rrik: 603 case X86::VPCMPQZ256rmi: case X86::VPCMPQZ256rmik: 604 case X86::VPCMPQZ256rmib: case X86::VPCMPQZ256rmibk: 605 case X86::VPCMPQZ256rri: case X86::VPCMPQZ256rrik: 606 case X86::VPCMPQZrmi: case X86::VPCMPQZrmik: 607 case X86::VPCMPQZrmib: case X86::VPCMPQZrmibk: 608 case X86::VPCMPQZrri: case X86::VPCMPQZrrik: 609 case X86::VPCMPWZ128rmi: case X86::VPCMPWZ128rmik: 610 case X86::VPCMPWZ128rri: case X86::VPCMPWZ128rrik: 611 case X86::VPCMPWZ256rmi: case X86::VPCMPWZ256rmik: 612 case X86::VPCMPWZ256rri: case X86::VPCMPWZ256rrik: 613 case X86::VPCMPWZrmi: case X86::VPCMPWZrmik: 614 case X86::VPCMPWZrri: case X86::VPCMPWZrrik: { 615 // Turn immediate 0 into the VPCMPEQ instruction. 616 if (OutMI.getOperand(OutMI.getNumOperands() - 1).getImm() == 0) { 617 unsigned NewOpc; 618 switch (OutMI.getOpcode()) { 619 default: llvm_unreachable("Invalid opcode"); 620 case X86::VPCMPBZ128rmi: NewOpc = X86::VPCMPEQBZ128rm; break; 621 case X86::VPCMPBZ128rmik: NewOpc = X86::VPCMPEQBZ128rmk; break; 622 case X86::VPCMPBZ128rri: NewOpc = X86::VPCMPEQBZ128rr; break; 623 case X86::VPCMPBZ128rrik: NewOpc = X86::VPCMPEQBZ128rrk; break; 624 case X86::VPCMPBZ256rmi: NewOpc = X86::VPCMPEQBZ256rm; break; 625 case X86::VPCMPBZ256rmik: NewOpc = X86::VPCMPEQBZ256rmk; break; 626 case X86::VPCMPBZ256rri: NewOpc = X86::VPCMPEQBZ256rr; break; 627 case X86::VPCMPBZ256rrik: NewOpc = X86::VPCMPEQBZ256rrk; break; 628 case X86::VPCMPBZrmi: NewOpc = X86::VPCMPEQBZrm; break; 629 case X86::VPCMPBZrmik: NewOpc = X86::VPCMPEQBZrmk; break; 630 case X86::VPCMPBZrri: NewOpc = X86::VPCMPEQBZrr; break; 631 case X86::VPCMPBZrrik: NewOpc = X86::VPCMPEQBZrrk; break; 632 case X86::VPCMPDZ128rmi: NewOpc = X86::VPCMPEQDZ128rm; break; 633 case X86::VPCMPDZ128rmib: NewOpc = X86::VPCMPEQDZ128rmb; break; 634 case X86::VPCMPDZ128rmibk: NewOpc = X86::VPCMPEQDZ128rmbk; break; 635 case X86::VPCMPDZ128rmik: NewOpc = X86::VPCMPEQDZ128rmk; break; 636 case X86::VPCMPDZ128rri: NewOpc = X86::VPCMPEQDZ128rr; break; 637 case X86::VPCMPDZ128rrik: NewOpc = X86::VPCMPEQDZ128rrk; break; 638 case X86::VPCMPDZ256rmi: NewOpc = X86::VPCMPEQDZ256rm; break; 639 case X86::VPCMPDZ256rmib: NewOpc = X86::VPCMPEQDZ256rmb; break; 640 case X86::VPCMPDZ256rmibk: NewOpc = X86::VPCMPEQDZ256rmbk; break; 641 case X86::VPCMPDZ256rmik: NewOpc = X86::VPCMPEQDZ256rmk; break; 642 case X86::VPCMPDZ256rri: NewOpc = X86::VPCMPEQDZ256rr; break; 643 case X86::VPCMPDZ256rrik: NewOpc = X86::VPCMPEQDZ256rrk; break; 644 case X86::VPCMPDZrmi: NewOpc = X86::VPCMPEQDZrm; break; 645 case X86::VPCMPDZrmib: NewOpc = X86::VPCMPEQDZrmb; break; 646 case X86::VPCMPDZrmibk: NewOpc = X86::VPCMPEQDZrmbk; break; 647 case X86::VPCMPDZrmik: NewOpc = X86::VPCMPEQDZrmk; break; 648 case X86::VPCMPDZrri: NewOpc = X86::VPCMPEQDZrr; break; 649 case X86::VPCMPDZrrik: NewOpc = X86::VPCMPEQDZrrk; break; 650 case X86::VPCMPQZ128rmi: NewOpc = X86::VPCMPEQQZ128rm; break; 651 case X86::VPCMPQZ128rmib: NewOpc = X86::VPCMPEQQZ128rmb; break; 652 case X86::VPCMPQZ128rmibk: NewOpc = X86::VPCMPEQQZ128rmbk; break; 653 case X86::VPCMPQZ128rmik: NewOpc = X86::VPCMPEQQZ128rmk; break; 654 case X86::VPCMPQZ128rri: NewOpc = X86::VPCMPEQQZ128rr; break; 655 case X86::VPCMPQZ128rrik: NewOpc = X86::VPCMPEQQZ128rrk; break; 656 case X86::VPCMPQZ256rmi: NewOpc = X86::VPCMPEQQZ256rm; break; 657 case X86::VPCMPQZ256rmib: NewOpc = X86::VPCMPEQQZ256rmb; break; 658 case X86::VPCMPQZ256rmibk: NewOpc = X86::VPCMPEQQZ256rmbk; break; 659 case X86::VPCMPQZ256rmik: NewOpc = X86::VPCMPEQQZ256rmk; break; 660 case X86::VPCMPQZ256rri: NewOpc = X86::VPCMPEQQZ256rr; break; 661 case X86::VPCMPQZ256rrik: NewOpc = X86::VPCMPEQQZ256rrk; break; 662 case X86::VPCMPQZrmi: NewOpc = X86::VPCMPEQQZrm; break; 663 case X86::VPCMPQZrmib: NewOpc = X86::VPCMPEQQZrmb; break; 664 case X86::VPCMPQZrmibk: NewOpc = X86::VPCMPEQQZrmbk; break; 665 case X86::VPCMPQZrmik: NewOpc = X86::VPCMPEQQZrmk; break; 666 case X86::VPCMPQZrri: NewOpc = X86::VPCMPEQQZrr; break; 667 case X86::VPCMPQZrrik: NewOpc = X86::VPCMPEQQZrrk; break; 668 case X86::VPCMPWZ128rmi: NewOpc = X86::VPCMPEQWZ128rm; break; 669 case X86::VPCMPWZ128rmik: NewOpc = X86::VPCMPEQWZ128rmk; break; 670 case X86::VPCMPWZ128rri: NewOpc = X86::VPCMPEQWZ128rr; break; 671 case X86::VPCMPWZ128rrik: NewOpc = X86::VPCMPEQWZ128rrk; break; 672 case X86::VPCMPWZ256rmi: NewOpc = X86::VPCMPEQWZ256rm; break; 673 case X86::VPCMPWZ256rmik: NewOpc = X86::VPCMPEQWZ256rmk; break; 674 case X86::VPCMPWZ256rri: NewOpc = X86::VPCMPEQWZ256rr; break; 675 case X86::VPCMPWZ256rrik: NewOpc = X86::VPCMPEQWZ256rrk; break; 676 case X86::VPCMPWZrmi: NewOpc = X86::VPCMPEQWZrm; break; 677 case X86::VPCMPWZrmik: NewOpc = X86::VPCMPEQWZrmk; break; 678 case X86::VPCMPWZrri: NewOpc = X86::VPCMPEQWZrr; break; 679 case X86::VPCMPWZrrik: NewOpc = X86::VPCMPEQWZrrk; break; 680 } 681 682 OutMI.setOpcode(NewOpc); 683 OutMI.erase(&OutMI.getOperand(OutMI.getNumOperands() - 1)); 684 break; 685 } 686 687 // Turn immediate 6 into the VPCMPGT instruction. 688 if (OutMI.getOperand(OutMI.getNumOperands() - 1).getImm() == 6) { 689 unsigned NewOpc; 690 switch (OutMI.getOpcode()) { 691 default: llvm_unreachable("Invalid opcode"); 692 case X86::VPCMPBZ128rmi: NewOpc = X86::VPCMPGTBZ128rm; break; 693 case X86::VPCMPBZ128rmik: NewOpc = X86::VPCMPGTBZ128rmk; break; 694 case X86::VPCMPBZ128rri: NewOpc = X86::VPCMPGTBZ128rr; break; 695 case X86::VPCMPBZ128rrik: NewOpc = X86::VPCMPGTBZ128rrk; break; 696 case X86::VPCMPBZ256rmi: NewOpc = X86::VPCMPGTBZ256rm; break; 697 case X86::VPCMPBZ256rmik: NewOpc = X86::VPCMPGTBZ256rmk; break; 698 case X86::VPCMPBZ256rri: NewOpc = X86::VPCMPGTBZ256rr; break; 699 case X86::VPCMPBZ256rrik: NewOpc = X86::VPCMPGTBZ256rrk; break; 700 case X86::VPCMPBZrmi: NewOpc = X86::VPCMPGTBZrm; break; 701 case X86::VPCMPBZrmik: NewOpc = X86::VPCMPGTBZrmk; break; 702 case X86::VPCMPBZrri: NewOpc = X86::VPCMPGTBZrr; break; 703 case X86::VPCMPBZrrik: NewOpc = X86::VPCMPGTBZrrk; break; 704 case X86::VPCMPDZ128rmi: NewOpc = X86::VPCMPGTDZ128rm; break; 705 case X86::VPCMPDZ128rmib: NewOpc = X86::VPCMPGTDZ128rmb; break; 706 case X86::VPCMPDZ128rmibk: NewOpc = X86::VPCMPGTDZ128rmbk; break; 707 case X86::VPCMPDZ128rmik: NewOpc = X86::VPCMPGTDZ128rmk; break; 708 case X86::VPCMPDZ128rri: NewOpc = X86::VPCMPGTDZ128rr; break; 709 case X86::VPCMPDZ128rrik: NewOpc = X86::VPCMPGTDZ128rrk; break; 710 case X86::VPCMPDZ256rmi: NewOpc = X86::VPCMPGTDZ256rm; break; 711 case X86::VPCMPDZ256rmib: NewOpc = X86::VPCMPGTDZ256rmb; break; 712 case X86::VPCMPDZ256rmibk: NewOpc = X86::VPCMPGTDZ256rmbk; break; 713 case X86::VPCMPDZ256rmik: NewOpc = X86::VPCMPGTDZ256rmk; break; 714 case X86::VPCMPDZ256rri: NewOpc = X86::VPCMPGTDZ256rr; break; 715 case X86::VPCMPDZ256rrik: NewOpc = X86::VPCMPGTDZ256rrk; break; 716 case X86::VPCMPDZrmi: NewOpc = X86::VPCMPGTDZrm; break; 717 case X86::VPCMPDZrmib: NewOpc = X86::VPCMPGTDZrmb; break; 718 case X86::VPCMPDZrmibk: NewOpc = X86::VPCMPGTDZrmbk; break; 719 case X86::VPCMPDZrmik: NewOpc = X86::VPCMPGTDZrmk; break; 720 case X86::VPCMPDZrri: NewOpc = X86::VPCMPGTDZrr; break; 721 case X86::VPCMPDZrrik: NewOpc = X86::VPCMPGTDZrrk; break; 722 case X86::VPCMPQZ128rmi: NewOpc = X86::VPCMPGTQZ128rm; break; 723 case X86::VPCMPQZ128rmib: NewOpc = X86::VPCMPGTQZ128rmb; break; 724 case X86::VPCMPQZ128rmibk: NewOpc = X86::VPCMPGTQZ128rmbk; break; 725 case X86::VPCMPQZ128rmik: NewOpc = X86::VPCMPGTQZ128rmk; break; 726 case X86::VPCMPQZ128rri: NewOpc = X86::VPCMPGTQZ128rr; break; 727 case X86::VPCMPQZ128rrik: NewOpc = X86::VPCMPGTQZ128rrk; break; 728 case X86::VPCMPQZ256rmi: NewOpc = X86::VPCMPGTQZ256rm; break; 729 case X86::VPCMPQZ256rmib: NewOpc = X86::VPCMPGTQZ256rmb; break; 730 case X86::VPCMPQZ256rmibk: NewOpc = X86::VPCMPGTQZ256rmbk; break; 731 case X86::VPCMPQZ256rmik: NewOpc = X86::VPCMPGTQZ256rmk; break; 732 case X86::VPCMPQZ256rri: NewOpc = X86::VPCMPGTQZ256rr; break; 733 case X86::VPCMPQZ256rrik: NewOpc = X86::VPCMPGTQZ256rrk; break; 734 case X86::VPCMPQZrmi: NewOpc = X86::VPCMPGTQZrm; break; 735 case X86::VPCMPQZrmib: NewOpc = X86::VPCMPGTQZrmb; break; 736 case X86::VPCMPQZrmibk: NewOpc = X86::VPCMPGTQZrmbk; break; 737 case X86::VPCMPQZrmik: NewOpc = X86::VPCMPGTQZrmk; break; 738 case X86::VPCMPQZrri: NewOpc = X86::VPCMPGTQZrr; break; 739 case X86::VPCMPQZrrik: NewOpc = X86::VPCMPGTQZrrk; break; 740 case X86::VPCMPWZ128rmi: NewOpc = X86::VPCMPGTWZ128rm; break; 741 case X86::VPCMPWZ128rmik: NewOpc = X86::VPCMPGTWZ128rmk; break; 742 case X86::VPCMPWZ128rri: NewOpc = X86::VPCMPGTWZ128rr; break; 743 case X86::VPCMPWZ128rrik: NewOpc = X86::VPCMPGTWZ128rrk; break; 744 case X86::VPCMPWZ256rmi: NewOpc = X86::VPCMPGTWZ256rm; break; 745 case X86::VPCMPWZ256rmik: NewOpc = X86::VPCMPGTWZ256rmk; break; 746 case X86::VPCMPWZ256rri: NewOpc = X86::VPCMPGTWZ256rr; break; 747 case X86::VPCMPWZ256rrik: NewOpc = X86::VPCMPGTWZ256rrk; break; 748 case X86::VPCMPWZrmi: NewOpc = X86::VPCMPGTWZrm; break; 749 case X86::VPCMPWZrmik: NewOpc = X86::VPCMPGTWZrmk; break; 750 case X86::VPCMPWZrri: NewOpc = X86::VPCMPGTWZrr; break; 751 case X86::VPCMPWZrrik: NewOpc = X86::VPCMPGTWZrrk; break; 752 } 753 754 OutMI.setOpcode(NewOpc); 755 OutMI.erase(&OutMI.getOperand(OutMI.getNumOperands() - 1)); 756 break; 757 } 758 759 break; 760 } 761 762 // CALL64r, CALL64pcrel32 - These instructions used to have 763 // register inputs modeled as normal uses instead of implicit uses. As such, 764 // they we used to truncate off all but the first operand (the callee). This 765 // issue seems to have been fixed at some point. This assert verifies that. 766 case X86::CALL64r: 767 case X86::CALL64pcrel32: 768 assert(OutMI.getNumOperands() == 1 && "Unexpected number of operands!"); 769 break; 770 771 case X86::EH_RETURN: 772 case X86::EH_RETURN64: { 773 OutMI = MCInst(); 774 OutMI.setOpcode(getRetOpcode(AsmPrinter.getSubtarget())); 775 break; 776 } 777 778 case X86::CLEANUPRET: { 779 // Replace CLEANUPRET with the appropriate RET. 780 OutMI = MCInst(); 781 OutMI.setOpcode(getRetOpcode(AsmPrinter.getSubtarget())); 782 break; 783 } 784 785 case X86::CATCHRET: { 786 // Replace CATCHRET with the appropriate RET. 787 const X86Subtarget &Subtarget = AsmPrinter.getSubtarget(); 788 unsigned ReturnReg = Subtarget.is64Bit() ? X86::RAX : X86::EAX; 789 OutMI = MCInst(); 790 OutMI.setOpcode(getRetOpcode(Subtarget)); 791 OutMI.addOperand(MCOperand::createReg(ReturnReg)); 792 break; 793 } 794 795 // TAILJMPd, TAILJMPd64, TailJMPd_cc - Lower to the correct jump 796 // instruction. 797 case X86::TAILJMPr: 798 case X86::TAILJMPr64: 799 case X86::TAILJMPr64_REX: 800 case X86::TAILJMPd: 801 case X86::TAILJMPd64: 802 assert(OutMI.getNumOperands() == 1 && "Unexpected number of operands!"); 803 OutMI.setOpcode(convertTailJumpOpcode(OutMI.getOpcode())); 804 break; 805 806 case X86::TAILJMPd_CC: 807 case X86::TAILJMPd64_CC: 808 assert(OutMI.getNumOperands() == 2 && "Unexpected number of operands!"); 809 OutMI.setOpcode(convertTailJumpOpcode(OutMI.getOpcode())); 810 break; 811 812 case X86::TAILJMPm: 813 case X86::TAILJMPm64: 814 case X86::TAILJMPm64_REX: 815 assert(OutMI.getNumOperands() == X86::AddrNumOperands && 816 "Unexpected number of operands!"); 817 OutMI.setOpcode(convertTailJumpOpcode(OutMI.getOpcode())); 818 break; 819 820 case X86::DEC16r: 821 case X86::DEC32r: 822 case X86::INC16r: 823 case X86::INC32r: 824 // If we aren't in 64-bit mode we can use the 1-byte inc/dec instructions. 825 if (!AsmPrinter.getSubtarget().is64Bit()) { 826 unsigned Opcode; 827 switch (OutMI.getOpcode()) { 828 default: llvm_unreachable("Invalid opcode"); 829 case X86::DEC16r: Opcode = X86::DEC16r_alt; break; 830 case X86::DEC32r: Opcode = X86::DEC32r_alt; break; 831 case X86::INC16r: Opcode = X86::INC16r_alt; break; 832 case X86::INC32r: Opcode = X86::INC32r_alt; break; 833 } 834 OutMI.setOpcode(Opcode); 835 } 836 break; 837 838 // We don't currently select the correct instruction form for instructions 839 // which have a short %eax, etc. form. Handle this by custom lowering, for 840 // now. 841 // 842 // Note, we are currently not handling the following instructions: 843 // MOV64ao8, MOV64o8a 844 // XCHG16ar, XCHG32ar, XCHG64ar 845 case X86::MOV8mr_NOREX: 846 case X86::MOV8mr: 847 case X86::MOV8rm_NOREX: 848 case X86::MOV8rm: 849 case X86::MOV16mr: 850 case X86::MOV16rm: 851 case X86::MOV32mr: 852 case X86::MOV32rm: { 853 unsigned NewOpc; 854 switch (OutMI.getOpcode()) { 855 default: llvm_unreachable("Invalid opcode"); 856 case X86::MOV8mr_NOREX: 857 case X86::MOV8mr: NewOpc = X86::MOV8o32a; break; 858 case X86::MOV8rm_NOREX: 859 case X86::MOV8rm: NewOpc = X86::MOV8ao32; break; 860 case X86::MOV16mr: NewOpc = X86::MOV16o32a; break; 861 case X86::MOV16rm: NewOpc = X86::MOV16ao32; break; 862 case X86::MOV32mr: NewOpc = X86::MOV32o32a; break; 863 case X86::MOV32rm: NewOpc = X86::MOV32ao32; break; 864 } 865 SimplifyShortMoveForm(AsmPrinter, OutMI, NewOpc); 866 break; 867 } 868 869 case X86::ADC8ri: case X86::ADC16ri: case X86::ADC32ri: case X86::ADC64ri32: 870 case X86::ADD8ri: case X86::ADD16ri: case X86::ADD32ri: case X86::ADD64ri32: 871 case X86::AND8ri: case X86::AND16ri: case X86::AND32ri: case X86::AND64ri32: 872 case X86::CMP8ri: case X86::CMP16ri: case X86::CMP32ri: case X86::CMP64ri32: 873 case X86::OR8ri: case X86::OR16ri: case X86::OR32ri: case X86::OR64ri32: 874 case X86::SBB8ri: case X86::SBB16ri: case X86::SBB32ri: case X86::SBB64ri32: 875 case X86::SUB8ri: case X86::SUB16ri: case X86::SUB32ri: case X86::SUB64ri32: 876 case X86::TEST8ri:case X86::TEST16ri:case X86::TEST32ri:case X86::TEST64ri32: 877 case X86::XOR8ri: case X86::XOR16ri: case X86::XOR32ri: case X86::XOR64ri32: { 878 unsigned NewOpc; 879 switch (OutMI.getOpcode()) { 880 default: llvm_unreachable("Invalid opcode"); 881 case X86::ADC8ri: NewOpc = X86::ADC8i8; break; 882 case X86::ADC16ri: NewOpc = X86::ADC16i16; break; 883 case X86::ADC32ri: NewOpc = X86::ADC32i32; break; 884 case X86::ADC64ri32: NewOpc = X86::ADC64i32; break; 885 case X86::ADD8ri: NewOpc = X86::ADD8i8; break; 886 case X86::ADD16ri: NewOpc = X86::ADD16i16; break; 887 case X86::ADD32ri: NewOpc = X86::ADD32i32; break; 888 case X86::ADD64ri32: NewOpc = X86::ADD64i32; break; 889 case X86::AND8ri: NewOpc = X86::AND8i8; break; 890 case X86::AND16ri: NewOpc = X86::AND16i16; break; 891 case X86::AND32ri: NewOpc = X86::AND32i32; break; 892 case X86::AND64ri32: NewOpc = X86::AND64i32; break; 893 case X86::CMP8ri: NewOpc = X86::CMP8i8; break; 894 case X86::CMP16ri: NewOpc = X86::CMP16i16; break; 895 case X86::CMP32ri: NewOpc = X86::CMP32i32; break; 896 case X86::CMP64ri32: NewOpc = X86::CMP64i32; break; 897 case X86::OR8ri: NewOpc = X86::OR8i8; break; 898 case X86::OR16ri: NewOpc = X86::OR16i16; break; 899 case X86::OR32ri: NewOpc = X86::OR32i32; break; 900 case X86::OR64ri32: NewOpc = X86::OR64i32; break; 901 case X86::SBB8ri: NewOpc = X86::SBB8i8; break; 902 case X86::SBB16ri: NewOpc = X86::SBB16i16; break; 903 case X86::SBB32ri: NewOpc = X86::SBB32i32; break; 904 case X86::SBB64ri32: NewOpc = X86::SBB64i32; break; 905 case X86::SUB8ri: NewOpc = X86::SUB8i8; break; 906 case X86::SUB16ri: NewOpc = X86::SUB16i16; break; 907 case X86::SUB32ri: NewOpc = X86::SUB32i32; break; 908 case X86::SUB64ri32: NewOpc = X86::SUB64i32; break; 909 case X86::TEST8ri: NewOpc = X86::TEST8i8; break; 910 case X86::TEST16ri: NewOpc = X86::TEST16i16; break; 911 case X86::TEST32ri: NewOpc = X86::TEST32i32; break; 912 case X86::TEST64ri32: NewOpc = X86::TEST64i32; break; 913 case X86::XOR8ri: NewOpc = X86::XOR8i8; break; 914 case X86::XOR16ri: NewOpc = X86::XOR16i16; break; 915 case X86::XOR32ri: NewOpc = X86::XOR32i32; break; 916 case X86::XOR64ri32: NewOpc = X86::XOR64i32; break; 917 } 918 SimplifyShortImmForm(OutMI, NewOpc); 919 break; 920 } 921 922 // Try to shrink some forms of movsx. 923 case X86::MOVSX16rr8: 924 case X86::MOVSX32rr16: 925 case X86::MOVSX64rr32: 926 SimplifyMOVSX(OutMI); 927 break; 928 929 case X86::VCMPPDrri: 930 case X86::VCMPPDYrri: 931 case X86::VCMPPSrri: 932 case X86::VCMPPSYrri: 933 case X86::VCMPSDrr: 934 case X86::VCMPSSrr: { 935 // Swap the operands if it will enable a 2 byte VEX encoding. 936 // FIXME: Change the immediate to improve opportunities? 937 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(1).getReg()) && 938 X86II::isX86_64ExtendedReg(OutMI.getOperand(2).getReg())) { 939 unsigned Imm = MI->getOperand(3).getImm() & 0x7; 940 switch (Imm) { 941 default: break; 942 case 0x00: // EQUAL 943 case 0x03: // UNORDERED 944 case 0x04: // NOT EQUAL 945 case 0x07: // ORDERED 946 std::swap(OutMI.getOperand(1), OutMI.getOperand(2)); 947 break; 948 } 949 } 950 break; 951 } 952 953 case X86::VMOVHLPSrr: 954 case X86::VUNPCKHPDrr: 955 // These are not truly commutable so hide them from the default case. 956 break; 957 958 default: { 959 // If the instruction is a commutable arithmetic instruction we might be 960 // able to commute the operands to get a 2 byte VEX prefix. 961 uint64_t TSFlags = MI->getDesc().TSFlags; 962 if (MI->getDesc().isCommutable() && 963 (TSFlags & X86II::EncodingMask) == X86II::VEX && 964 (TSFlags & X86II::OpMapMask) == X86II::TB && 965 (TSFlags & X86II::FormMask) == X86II::MRMSrcReg && 966 !(TSFlags & X86II::VEX_W) && (TSFlags & X86II::VEX_4V) && 967 OutMI.getNumOperands() == 3) { 968 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(1).getReg()) && 969 X86II::isX86_64ExtendedReg(OutMI.getOperand(2).getReg())) 970 std::swap(OutMI.getOperand(1), OutMI.getOperand(2)); 971 } 972 break; 973 } 974 } 975 } 976 977 void X86AsmPrinter::LowerTlsAddr(X86MCInstLower &MCInstLowering, 978 const MachineInstr &MI) { 979 NoAutoPaddingScope NoPadScope(*OutStreamer); 980 bool Is64Bits = MI.getOpcode() == X86::TLS_addr64 || 981 MI.getOpcode() == X86::TLS_base_addr64; 982 MCContext &Ctx = OutStreamer->getContext(); 983 984 MCSymbolRefExpr::VariantKind SRVK; 985 switch (MI.getOpcode()) { 986 case X86::TLS_addr32: 987 case X86::TLS_addr64: 988 SRVK = MCSymbolRefExpr::VK_TLSGD; 989 break; 990 case X86::TLS_base_addr32: 991 SRVK = MCSymbolRefExpr::VK_TLSLDM; 992 break; 993 case X86::TLS_base_addr64: 994 SRVK = MCSymbolRefExpr::VK_TLSLD; 995 break; 996 default: 997 llvm_unreachable("unexpected opcode"); 998 } 999 1000 const MCSymbolRefExpr *Sym = MCSymbolRefExpr::create( 1001 MCInstLowering.GetSymbolFromOperand(MI.getOperand(3)), SRVK, Ctx); 1002 1003 // As of binutils 2.32, ld has a bogus TLS relaxation error when the GD/LD 1004 // code sequence using R_X86_64_GOTPCREL (instead of R_X86_64_GOTPCRELX) is 1005 // attempted to be relaxed to IE/LE (binutils PR24784). Work around the bug by 1006 // only using GOT when GOTPCRELX is enabled. 1007 // TODO Delete the workaround when GOTPCRELX becomes commonplace. 1008 bool UseGot = MMI->getModule()->getRtLibUseGOT() && 1009 Ctx.getAsmInfo()->canRelaxRelocations(); 1010 1011 if (Is64Bits) { 1012 bool NeedsPadding = SRVK == MCSymbolRefExpr::VK_TLSGD; 1013 if (NeedsPadding) 1014 EmitAndCountInstruction(MCInstBuilder(X86::DATA16_PREFIX)); 1015 EmitAndCountInstruction(MCInstBuilder(X86::LEA64r) 1016 .addReg(X86::RDI) 1017 .addReg(X86::RIP) 1018 .addImm(1) 1019 .addReg(0) 1020 .addExpr(Sym) 1021 .addReg(0)); 1022 const MCSymbol *TlsGetAddr = Ctx.getOrCreateSymbol("__tls_get_addr"); 1023 if (NeedsPadding) { 1024 if (!UseGot) 1025 EmitAndCountInstruction(MCInstBuilder(X86::DATA16_PREFIX)); 1026 EmitAndCountInstruction(MCInstBuilder(X86::DATA16_PREFIX)); 1027 EmitAndCountInstruction(MCInstBuilder(X86::REX64_PREFIX)); 1028 } 1029 if (UseGot) { 1030 const MCExpr *Expr = MCSymbolRefExpr::create( 1031 TlsGetAddr, MCSymbolRefExpr::VK_GOTPCREL, Ctx); 1032 EmitAndCountInstruction(MCInstBuilder(X86::CALL64m) 1033 .addReg(X86::RIP) 1034 .addImm(1) 1035 .addReg(0) 1036 .addExpr(Expr) 1037 .addReg(0)); 1038 } else { 1039 EmitAndCountInstruction( 1040 MCInstBuilder(X86::CALL64pcrel32) 1041 .addExpr(MCSymbolRefExpr::create(TlsGetAddr, 1042 MCSymbolRefExpr::VK_PLT, Ctx))); 1043 } 1044 } else { 1045 if (SRVK == MCSymbolRefExpr::VK_TLSGD && !UseGot) { 1046 EmitAndCountInstruction(MCInstBuilder(X86::LEA32r) 1047 .addReg(X86::EAX) 1048 .addReg(0) 1049 .addImm(1) 1050 .addReg(X86::EBX) 1051 .addExpr(Sym) 1052 .addReg(0)); 1053 } else { 1054 EmitAndCountInstruction(MCInstBuilder(X86::LEA32r) 1055 .addReg(X86::EAX) 1056 .addReg(X86::EBX) 1057 .addImm(1) 1058 .addReg(0) 1059 .addExpr(Sym) 1060 .addReg(0)); 1061 } 1062 1063 const MCSymbol *TlsGetAddr = Ctx.getOrCreateSymbol("___tls_get_addr"); 1064 if (UseGot) { 1065 const MCExpr *Expr = 1066 MCSymbolRefExpr::create(TlsGetAddr, MCSymbolRefExpr::VK_GOT, Ctx); 1067 EmitAndCountInstruction(MCInstBuilder(X86::CALL32m) 1068 .addReg(X86::EBX) 1069 .addImm(1) 1070 .addReg(0) 1071 .addExpr(Expr) 1072 .addReg(0)); 1073 } else { 1074 EmitAndCountInstruction( 1075 MCInstBuilder(X86::CALLpcrel32) 1076 .addExpr(MCSymbolRefExpr::create(TlsGetAddr, 1077 MCSymbolRefExpr::VK_PLT, Ctx))); 1078 } 1079 } 1080 } 1081 1082 /// Emit the largest nop instruction smaller than or equal to \p NumBytes 1083 /// bytes. Return the size of nop emitted. 1084 static unsigned emitNop(MCStreamer &OS, unsigned NumBytes, 1085 const X86Subtarget *Subtarget) { 1086 // Determine the longest nop which can be efficiently decoded for the given 1087 // target cpu. 15-bytes is the longest single NOP instruction, but some 1088 // platforms can't decode the longest forms efficiently. 1089 unsigned MaxNopLength = 1; 1090 if (Subtarget->is64Bit()) { 1091 // FIXME: We can use NOOPL on 32-bit targets with FeatureNOPL, but the 1092 // IndexReg/BaseReg below need to be updated. 1093 if (Subtarget->hasFeature(X86::FeatureFast7ByteNOP)) 1094 MaxNopLength = 7; 1095 else if (Subtarget->hasFeature(X86::FeatureFast15ByteNOP)) 1096 MaxNopLength = 15; 1097 else if (Subtarget->hasFeature(X86::FeatureFast11ByteNOP)) 1098 MaxNopLength = 11; 1099 else 1100 MaxNopLength = 10; 1101 } if (Subtarget->is32Bit()) 1102 MaxNopLength = 2; 1103 1104 // Cap a single nop emission at the profitable value for the target 1105 NumBytes = std::min(NumBytes, MaxNopLength); 1106 1107 unsigned NopSize; 1108 unsigned Opc, BaseReg, ScaleVal, IndexReg, Displacement, SegmentReg; 1109 IndexReg = Displacement = SegmentReg = 0; 1110 BaseReg = X86::RAX; 1111 ScaleVal = 1; 1112 switch (NumBytes) { 1113 case 0: 1114 llvm_unreachable("Zero nops?"); 1115 break; 1116 case 1: 1117 NopSize = 1; 1118 Opc = X86::NOOP; 1119 break; 1120 case 2: 1121 NopSize = 2; 1122 Opc = X86::XCHG16ar; 1123 break; 1124 case 3: 1125 NopSize = 3; 1126 Opc = X86::NOOPL; 1127 break; 1128 case 4: 1129 NopSize = 4; 1130 Opc = X86::NOOPL; 1131 Displacement = 8; 1132 break; 1133 case 5: 1134 NopSize = 5; 1135 Opc = X86::NOOPL; 1136 Displacement = 8; 1137 IndexReg = X86::RAX; 1138 break; 1139 case 6: 1140 NopSize = 6; 1141 Opc = X86::NOOPW; 1142 Displacement = 8; 1143 IndexReg = X86::RAX; 1144 break; 1145 case 7: 1146 NopSize = 7; 1147 Opc = X86::NOOPL; 1148 Displacement = 512; 1149 break; 1150 case 8: 1151 NopSize = 8; 1152 Opc = X86::NOOPL; 1153 Displacement = 512; 1154 IndexReg = X86::RAX; 1155 break; 1156 case 9: 1157 NopSize = 9; 1158 Opc = X86::NOOPW; 1159 Displacement = 512; 1160 IndexReg = X86::RAX; 1161 break; 1162 default: 1163 NopSize = 10; 1164 Opc = X86::NOOPW; 1165 Displacement = 512; 1166 IndexReg = X86::RAX; 1167 SegmentReg = X86::CS; 1168 break; 1169 } 1170 1171 unsigned NumPrefixes = std::min(NumBytes - NopSize, 5U); 1172 NopSize += NumPrefixes; 1173 for (unsigned i = 0; i != NumPrefixes; ++i) 1174 OS.emitBytes("\x66"); 1175 1176 switch (Opc) { 1177 default: llvm_unreachable("Unexpected opcode"); 1178 case X86::NOOP: 1179 OS.emitInstruction(MCInstBuilder(Opc), *Subtarget); 1180 break; 1181 case X86::XCHG16ar: 1182 OS.emitInstruction(MCInstBuilder(Opc).addReg(X86::AX).addReg(X86::AX), 1183 *Subtarget); 1184 break; 1185 case X86::NOOPL: 1186 case X86::NOOPW: 1187 OS.emitInstruction(MCInstBuilder(Opc) 1188 .addReg(BaseReg) 1189 .addImm(ScaleVal) 1190 .addReg(IndexReg) 1191 .addImm(Displacement) 1192 .addReg(SegmentReg), 1193 *Subtarget); 1194 break; 1195 } 1196 assert(NopSize <= NumBytes && "We overemitted?"); 1197 return NopSize; 1198 } 1199 1200 /// Emit the optimal amount of multi-byte nops on X86. 1201 static void emitX86Nops(MCStreamer &OS, unsigned NumBytes, 1202 const X86Subtarget *Subtarget) { 1203 unsigned NopsToEmit = NumBytes; 1204 (void)NopsToEmit; 1205 while (NumBytes) { 1206 NumBytes -= emitNop(OS, NumBytes, Subtarget); 1207 assert(NopsToEmit >= NumBytes && "Emitted more than I asked for!"); 1208 } 1209 } 1210 1211 void X86AsmPrinter::LowerSTATEPOINT(const MachineInstr &MI, 1212 X86MCInstLower &MCIL) { 1213 assert(Subtarget->is64Bit() && "Statepoint currently only supports X86-64"); 1214 1215 NoAutoPaddingScope NoPadScope(*OutStreamer); 1216 1217 StatepointOpers SOpers(&MI); 1218 if (unsigned PatchBytes = SOpers.getNumPatchBytes()) { 1219 emitX86Nops(*OutStreamer, PatchBytes, Subtarget); 1220 } else { 1221 // Lower call target and choose correct opcode 1222 const MachineOperand &CallTarget = SOpers.getCallTarget(); 1223 MCOperand CallTargetMCOp; 1224 unsigned CallOpcode; 1225 switch (CallTarget.getType()) { 1226 case MachineOperand::MO_GlobalAddress: 1227 case MachineOperand::MO_ExternalSymbol: 1228 CallTargetMCOp = MCIL.LowerSymbolOperand( 1229 CallTarget, MCIL.GetSymbolFromOperand(CallTarget)); 1230 CallOpcode = X86::CALL64pcrel32; 1231 // Currently, we only support relative addressing with statepoints. 1232 // Otherwise, we'll need a scratch register to hold the target 1233 // address. You'll fail asserts during load & relocation if this 1234 // symbol is to far away. (TODO: support non-relative addressing) 1235 break; 1236 case MachineOperand::MO_Immediate: 1237 CallTargetMCOp = MCOperand::createImm(CallTarget.getImm()); 1238 CallOpcode = X86::CALL64pcrel32; 1239 // Currently, we only support relative addressing with statepoints. 1240 // Otherwise, we'll need a scratch register to hold the target 1241 // immediate. You'll fail asserts during load & relocation if this 1242 // address is to far away. (TODO: support non-relative addressing) 1243 break; 1244 case MachineOperand::MO_Register: 1245 // FIXME: Add retpoline support and remove this. 1246 if (Subtarget->useIndirectThunkCalls()) 1247 report_fatal_error("Lowering register statepoints with thunks not " 1248 "yet implemented."); 1249 CallTargetMCOp = MCOperand::createReg(CallTarget.getReg()); 1250 CallOpcode = X86::CALL64r; 1251 break; 1252 default: 1253 llvm_unreachable("Unsupported operand type in statepoint call target"); 1254 break; 1255 } 1256 1257 // Emit call 1258 MCInst CallInst; 1259 CallInst.setOpcode(CallOpcode); 1260 CallInst.addOperand(CallTargetMCOp); 1261 OutStreamer->emitInstruction(CallInst, getSubtargetInfo()); 1262 } 1263 1264 // Record our statepoint node in the same section used by STACKMAP 1265 // and PATCHPOINT 1266 auto &Ctx = OutStreamer->getContext(); 1267 MCSymbol *MILabel = Ctx.createTempSymbol(); 1268 OutStreamer->emitLabel(MILabel); 1269 SM.recordStatepoint(*MILabel, MI); 1270 } 1271 1272 void X86AsmPrinter::LowerFAULTING_OP(const MachineInstr &FaultingMI, 1273 X86MCInstLower &MCIL) { 1274 // FAULTING_LOAD_OP <def>, <faltinf type>, <MBB handler>, 1275 // <opcode>, <operands> 1276 1277 NoAutoPaddingScope NoPadScope(*OutStreamer); 1278 1279 Register DefRegister = FaultingMI.getOperand(0).getReg(); 1280 FaultMaps::FaultKind FK = 1281 static_cast<FaultMaps::FaultKind>(FaultingMI.getOperand(1).getImm()); 1282 MCSymbol *HandlerLabel = FaultingMI.getOperand(2).getMBB()->getSymbol(); 1283 unsigned Opcode = FaultingMI.getOperand(3).getImm(); 1284 unsigned OperandsBeginIdx = 4; 1285 1286 auto &Ctx = OutStreamer->getContext(); 1287 MCSymbol *FaultingLabel = Ctx.createTempSymbol(); 1288 OutStreamer->emitLabel(FaultingLabel); 1289 1290 assert(FK < FaultMaps::FaultKindMax && "Invalid Faulting Kind!"); 1291 FM.recordFaultingOp(FK, FaultingLabel, HandlerLabel); 1292 1293 MCInst MI; 1294 MI.setOpcode(Opcode); 1295 1296 if (DefRegister != X86::NoRegister) 1297 MI.addOperand(MCOperand::createReg(DefRegister)); 1298 1299 for (auto I = FaultingMI.operands_begin() + OperandsBeginIdx, 1300 E = FaultingMI.operands_end(); 1301 I != E; ++I) 1302 if (auto MaybeOperand = MCIL.LowerMachineOperand(&FaultingMI, *I)) 1303 MI.addOperand(MaybeOperand.getValue()); 1304 1305 OutStreamer->AddComment("on-fault: " + HandlerLabel->getName()); 1306 OutStreamer->emitInstruction(MI, getSubtargetInfo()); 1307 } 1308 1309 void X86AsmPrinter::LowerFENTRY_CALL(const MachineInstr &MI, 1310 X86MCInstLower &MCIL) { 1311 bool Is64Bits = Subtarget->is64Bit(); 1312 MCContext &Ctx = OutStreamer->getContext(); 1313 MCSymbol *fentry = Ctx.getOrCreateSymbol("__fentry__"); 1314 const MCSymbolRefExpr *Op = 1315 MCSymbolRefExpr::create(fentry, MCSymbolRefExpr::VK_None, Ctx); 1316 1317 EmitAndCountInstruction( 1318 MCInstBuilder(Is64Bits ? X86::CALL64pcrel32 : X86::CALLpcrel32) 1319 .addExpr(Op)); 1320 } 1321 1322 void X86AsmPrinter::LowerPATCHABLE_OP(const MachineInstr &MI, 1323 X86MCInstLower &MCIL) { 1324 // PATCHABLE_OP minsize, opcode, operands 1325 1326 NoAutoPaddingScope NoPadScope(*OutStreamer); 1327 1328 unsigned MinSize = MI.getOperand(0).getImm(); 1329 unsigned Opcode = MI.getOperand(1).getImm(); 1330 1331 MCInst MCI; 1332 MCI.setOpcode(Opcode); 1333 for (auto &MO : make_range(MI.operands_begin() + 2, MI.operands_end())) 1334 if (auto MaybeOperand = MCIL.LowerMachineOperand(&MI, MO)) 1335 MCI.addOperand(MaybeOperand.getValue()); 1336 1337 SmallString<256> Code; 1338 SmallVector<MCFixup, 4> Fixups; 1339 raw_svector_ostream VecOS(Code); 1340 CodeEmitter->encodeInstruction(MCI, VecOS, Fixups, getSubtargetInfo()); 1341 1342 if (Code.size() < MinSize) { 1343 if (MinSize == 2 && Subtarget->is32Bit() && 1344 Subtarget->isTargetWindowsMSVC() && 1345 (Subtarget->getCPU().empty() || Subtarget->getCPU() == "pentium3")) { 1346 // For compatibilty reasons, when targetting MSVC, is is important to 1347 // generate a 'legacy' NOP in the form of a 8B FF MOV EDI, EDI. Some tools 1348 // rely specifically on this pattern to be able to patch a function. 1349 // This is only for 32-bit targets, when using /arch:IA32 or /arch:SSE. 1350 OutStreamer->emitInstruction( 1351 MCInstBuilder(X86::MOV32rr_REV).addReg(X86::EDI).addReg(X86::EDI), 1352 *Subtarget); 1353 } else if (MinSize == 2 && Opcode == X86::PUSH64r) { 1354 // This is an optimization that lets us get away without emitting a nop in 1355 // many cases. 1356 // 1357 // NB! In some cases the encoding for PUSH64r (e.g. PUSH64r %r9) takes two 1358 // bytes too, so the check on MinSize is important. 1359 MCI.setOpcode(X86::PUSH64rmr); 1360 } else { 1361 unsigned NopSize = emitNop(*OutStreamer, MinSize, Subtarget); 1362 assert(NopSize == MinSize && "Could not implement MinSize!"); 1363 (void)NopSize; 1364 } 1365 } 1366 1367 OutStreamer->emitInstruction(MCI, getSubtargetInfo()); 1368 } 1369 1370 // Lower a stackmap of the form: 1371 // <id>, <shadowBytes>, ... 1372 void X86AsmPrinter::LowerSTACKMAP(const MachineInstr &MI) { 1373 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo()); 1374 1375 auto &Ctx = OutStreamer->getContext(); 1376 MCSymbol *MILabel = Ctx.createTempSymbol(); 1377 OutStreamer->emitLabel(MILabel); 1378 1379 SM.recordStackMap(*MILabel, MI); 1380 unsigned NumShadowBytes = MI.getOperand(1).getImm(); 1381 SMShadowTracker.reset(NumShadowBytes); 1382 } 1383 1384 // Lower a patchpoint of the form: 1385 // [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ... 1386 void X86AsmPrinter::LowerPATCHPOINT(const MachineInstr &MI, 1387 X86MCInstLower &MCIL) { 1388 assert(Subtarget->is64Bit() && "Patchpoint currently only supports X86-64"); 1389 1390 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo()); 1391 1392 NoAutoPaddingScope NoPadScope(*OutStreamer); 1393 1394 auto &Ctx = OutStreamer->getContext(); 1395 MCSymbol *MILabel = Ctx.createTempSymbol(); 1396 OutStreamer->emitLabel(MILabel); 1397 SM.recordPatchPoint(*MILabel, MI); 1398 1399 PatchPointOpers opers(&MI); 1400 unsigned ScratchIdx = opers.getNextScratchIdx(); 1401 unsigned EncodedBytes = 0; 1402 const MachineOperand &CalleeMO = opers.getCallTarget(); 1403 1404 // Check for null target. If target is non-null (i.e. is non-zero or is 1405 // symbolic) then emit a call. 1406 if (!(CalleeMO.isImm() && !CalleeMO.getImm())) { 1407 MCOperand CalleeMCOp; 1408 switch (CalleeMO.getType()) { 1409 default: 1410 /// FIXME: Add a verifier check for bad callee types. 1411 llvm_unreachable("Unrecognized callee operand type."); 1412 case MachineOperand::MO_Immediate: 1413 if (CalleeMO.getImm()) 1414 CalleeMCOp = MCOperand::createImm(CalleeMO.getImm()); 1415 break; 1416 case MachineOperand::MO_ExternalSymbol: 1417 case MachineOperand::MO_GlobalAddress: 1418 CalleeMCOp = MCIL.LowerSymbolOperand(CalleeMO, 1419 MCIL.GetSymbolFromOperand(CalleeMO)); 1420 break; 1421 } 1422 1423 // Emit MOV to materialize the target address and the CALL to target. 1424 // This is encoded with 12-13 bytes, depending on which register is used. 1425 Register ScratchReg = MI.getOperand(ScratchIdx).getReg(); 1426 if (X86II::isX86_64ExtendedReg(ScratchReg)) 1427 EncodedBytes = 13; 1428 else 1429 EncodedBytes = 12; 1430 1431 EmitAndCountInstruction( 1432 MCInstBuilder(X86::MOV64ri).addReg(ScratchReg).addOperand(CalleeMCOp)); 1433 // FIXME: Add retpoline support and remove this. 1434 if (Subtarget->useIndirectThunkCalls()) 1435 report_fatal_error( 1436 "Lowering patchpoint with thunks not yet implemented."); 1437 EmitAndCountInstruction(MCInstBuilder(X86::CALL64r).addReg(ScratchReg)); 1438 } 1439 1440 // Emit padding. 1441 unsigned NumBytes = opers.getNumPatchBytes(); 1442 assert(NumBytes >= EncodedBytes && 1443 "Patchpoint can't request size less than the length of a call."); 1444 1445 emitX86Nops(*OutStreamer, NumBytes - EncodedBytes, Subtarget); 1446 } 1447 1448 void X86AsmPrinter::LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, 1449 X86MCInstLower &MCIL) { 1450 assert(Subtarget->is64Bit() && "XRay custom events only supports X86-64"); 1451 1452 NoAutoPaddingScope NoPadScope(*OutStreamer); 1453 1454 // We want to emit the following pattern, which follows the x86 calling 1455 // convention to prepare for the trampoline call to be patched in. 1456 // 1457 // .p2align 1, ... 1458 // .Lxray_event_sled_N: 1459 // jmp +N // jump across the instrumentation sled 1460 // ... // set up arguments in register 1461 // callq __xray_CustomEvent@plt // force dependency to symbol 1462 // ... 1463 // <jump here> 1464 // 1465 // After patching, it would look something like: 1466 // 1467 // nopw (2-byte nop) 1468 // ... 1469 // callq __xrayCustomEvent // already lowered 1470 // ... 1471 // 1472 // --- 1473 // First we emit the label and the jump. 1474 auto CurSled = OutContext.createTempSymbol("xray_event_sled_", true); 1475 OutStreamer->AddComment("# XRay Custom Event Log"); 1476 OutStreamer->emitCodeAlignment(2); 1477 OutStreamer->emitLabel(CurSled); 1478 1479 // Use a two-byte `jmp`. This version of JMP takes an 8-bit relative offset as 1480 // an operand (computed as an offset from the jmp instruction). 1481 // FIXME: Find another less hacky way do force the relative jump. 1482 OutStreamer->emitBinaryData("\xeb\x0f"); 1483 1484 // The default C calling convention will place two arguments into %rcx and 1485 // %rdx -- so we only work with those. 1486 const Register DestRegs[] = {X86::RDI, X86::RSI}; 1487 bool UsedMask[] = {false, false}; 1488 // Filled out in loop. 1489 Register SrcRegs[] = {0, 0}; 1490 1491 // Then we put the operands in the %rdi and %rsi registers. We spill the 1492 // values in the register before we clobber them, and mark them as used in 1493 // UsedMask. In case the arguments are already in the correct register, we use 1494 // emit nops appropriately sized to keep the sled the same size in every 1495 // situation. 1496 for (unsigned I = 0; I < MI.getNumOperands(); ++I) 1497 if (auto Op = MCIL.LowerMachineOperand(&MI, MI.getOperand(I))) { 1498 assert(Op->isReg() && "Only support arguments in registers"); 1499 SrcRegs[I] = getX86SubSuperRegister(Op->getReg(), 64); 1500 if (SrcRegs[I] != DestRegs[I]) { 1501 UsedMask[I] = true; 1502 EmitAndCountInstruction( 1503 MCInstBuilder(X86::PUSH64r).addReg(DestRegs[I])); 1504 } else { 1505 emitX86Nops(*OutStreamer, 4, Subtarget); 1506 } 1507 } 1508 1509 // Now that the register values are stashed, mov arguments into place. 1510 // FIXME: This doesn't work if one of the later SrcRegs is equal to an 1511 // earlier DestReg. We will have already overwritten over the register before 1512 // we can copy from it. 1513 for (unsigned I = 0; I < MI.getNumOperands(); ++I) 1514 if (SrcRegs[I] != DestRegs[I]) 1515 EmitAndCountInstruction( 1516 MCInstBuilder(X86::MOV64rr).addReg(DestRegs[I]).addReg(SrcRegs[I])); 1517 1518 // We emit a hard dependency on the __xray_CustomEvent symbol, which is the 1519 // name of the trampoline to be implemented by the XRay runtime. 1520 auto TSym = OutContext.getOrCreateSymbol("__xray_CustomEvent"); 1521 MachineOperand TOp = MachineOperand::CreateMCSymbol(TSym); 1522 if (isPositionIndependent()) 1523 TOp.setTargetFlags(X86II::MO_PLT); 1524 1525 // Emit the call instruction. 1526 EmitAndCountInstruction(MCInstBuilder(X86::CALL64pcrel32) 1527 .addOperand(MCIL.LowerSymbolOperand(TOp, TSym))); 1528 1529 // Restore caller-saved and used registers. 1530 for (unsigned I = sizeof UsedMask; I-- > 0;) 1531 if (UsedMask[I]) 1532 EmitAndCountInstruction(MCInstBuilder(X86::POP64r).addReg(DestRegs[I])); 1533 else 1534 emitX86Nops(*OutStreamer, 1, Subtarget); 1535 1536 OutStreamer->AddComment("xray custom event end."); 1537 1538 // Record the sled version. Version 0 of this sled was spelled differently, so 1539 // we let the runtime handle the different offsets we're using. Version 2 1540 // changed the absolute address to a PC-relative address. 1541 recordSled(CurSled, MI, SledKind::CUSTOM_EVENT, 2); 1542 } 1543 1544 void X86AsmPrinter::LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr &MI, 1545 X86MCInstLower &MCIL) { 1546 assert(Subtarget->is64Bit() && "XRay typed events only supports X86-64"); 1547 1548 NoAutoPaddingScope NoPadScope(*OutStreamer); 1549 1550 // We want to emit the following pattern, which follows the x86 calling 1551 // convention to prepare for the trampoline call to be patched in. 1552 // 1553 // .p2align 1, ... 1554 // .Lxray_event_sled_N: 1555 // jmp +N // jump across the instrumentation sled 1556 // ... // set up arguments in register 1557 // callq __xray_TypedEvent@plt // force dependency to symbol 1558 // ... 1559 // <jump here> 1560 // 1561 // After patching, it would look something like: 1562 // 1563 // nopw (2-byte nop) 1564 // ... 1565 // callq __xrayTypedEvent // already lowered 1566 // ... 1567 // 1568 // --- 1569 // First we emit the label and the jump. 1570 auto CurSled = OutContext.createTempSymbol("xray_typed_event_sled_", true); 1571 OutStreamer->AddComment("# XRay Typed Event Log"); 1572 OutStreamer->emitCodeAlignment(2); 1573 OutStreamer->emitLabel(CurSled); 1574 1575 // Use a two-byte `jmp`. This version of JMP takes an 8-bit relative offset as 1576 // an operand (computed as an offset from the jmp instruction). 1577 // FIXME: Find another less hacky way do force the relative jump. 1578 OutStreamer->emitBinaryData("\xeb\x14"); 1579 1580 // An x86-64 convention may place three arguments into %rcx, %rdx, and R8, 1581 // so we'll work with those. Or we may be called via SystemV, in which case 1582 // we don't have to do any translation. 1583 const Register DestRegs[] = {X86::RDI, X86::RSI, X86::RDX}; 1584 bool UsedMask[] = {false, false, false}; 1585 1586 // Will fill out src regs in the loop. 1587 Register SrcRegs[] = {0, 0, 0}; 1588 1589 // Then we put the operands in the SystemV registers. We spill the values in 1590 // the registers before we clobber them, and mark them as used in UsedMask. 1591 // In case the arguments are already in the correct register, we emit nops 1592 // appropriately sized to keep the sled the same size in every situation. 1593 for (unsigned I = 0; I < MI.getNumOperands(); ++I) 1594 if (auto Op = MCIL.LowerMachineOperand(&MI, MI.getOperand(I))) { 1595 // TODO: Is register only support adequate? 1596 assert(Op->isReg() && "Only supports arguments in registers"); 1597 SrcRegs[I] = getX86SubSuperRegister(Op->getReg(), 64); 1598 if (SrcRegs[I] != DestRegs[I]) { 1599 UsedMask[I] = true; 1600 EmitAndCountInstruction( 1601 MCInstBuilder(X86::PUSH64r).addReg(DestRegs[I])); 1602 } else { 1603 emitX86Nops(*OutStreamer, 4, Subtarget); 1604 } 1605 } 1606 1607 // In the above loop we only stash all of the destination registers or emit 1608 // nops if the arguments are already in the right place. Doing the actually 1609 // moving is postponed until after all the registers are stashed so nothing 1610 // is clobbers. We've already added nops to account for the size of mov and 1611 // push if the register is in the right place, so we only have to worry about 1612 // emitting movs. 1613 // FIXME: This doesn't work if one of the later SrcRegs is equal to an 1614 // earlier DestReg. We will have already overwritten over the register before 1615 // we can copy from it. 1616 for (unsigned I = 0; I < MI.getNumOperands(); ++I) 1617 if (UsedMask[I]) 1618 EmitAndCountInstruction( 1619 MCInstBuilder(X86::MOV64rr).addReg(DestRegs[I]).addReg(SrcRegs[I])); 1620 1621 // We emit a hard dependency on the __xray_TypedEvent symbol, which is the 1622 // name of the trampoline to be implemented by the XRay runtime. 1623 auto TSym = OutContext.getOrCreateSymbol("__xray_TypedEvent"); 1624 MachineOperand TOp = MachineOperand::CreateMCSymbol(TSym); 1625 if (isPositionIndependent()) 1626 TOp.setTargetFlags(X86II::MO_PLT); 1627 1628 // Emit the call instruction. 1629 EmitAndCountInstruction(MCInstBuilder(X86::CALL64pcrel32) 1630 .addOperand(MCIL.LowerSymbolOperand(TOp, TSym))); 1631 1632 // Restore caller-saved and used registers. 1633 for (unsigned I = sizeof UsedMask; I-- > 0;) 1634 if (UsedMask[I]) 1635 EmitAndCountInstruction(MCInstBuilder(X86::POP64r).addReg(DestRegs[I])); 1636 else 1637 emitX86Nops(*OutStreamer, 1, Subtarget); 1638 1639 OutStreamer->AddComment("xray typed event end."); 1640 1641 // Record the sled version. 1642 recordSled(CurSled, MI, SledKind::TYPED_EVENT, 2); 1643 } 1644 1645 void X86AsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI, 1646 X86MCInstLower &MCIL) { 1647 1648 NoAutoPaddingScope NoPadScope(*OutStreamer); 1649 1650 const Function &F = MF->getFunction(); 1651 if (F.hasFnAttribute("patchable-function-entry")) { 1652 unsigned Num; 1653 if (F.getFnAttribute("patchable-function-entry") 1654 .getValueAsString() 1655 .getAsInteger(10, Num)) 1656 return; 1657 emitX86Nops(*OutStreamer, Num, Subtarget); 1658 return; 1659 } 1660 // We want to emit the following pattern: 1661 // 1662 // .p2align 1, ... 1663 // .Lxray_sled_N: 1664 // jmp .tmpN 1665 // # 9 bytes worth of noops 1666 // 1667 // We need the 9 bytes because at runtime, we'd be patching over the full 11 1668 // bytes with the following pattern: 1669 // 1670 // mov %r10, <function id, 32-bit> // 6 bytes 1671 // call <relative offset, 32-bits> // 5 bytes 1672 // 1673 auto CurSled = OutContext.createTempSymbol("xray_sled_", true); 1674 OutStreamer->emitCodeAlignment(2); 1675 OutStreamer->emitLabel(CurSled); 1676 1677 // Use a two-byte `jmp`. This version of JMP takes an 8-bit relative offset as 1678 // an operand (computed as an offset from the jmp instruction). 1679 // FIXME: Find another less hacky way do force the relative jump. 1680 OutStreamer->emitBytes("\xeb\x09"); 1681 emitX86Nops(*OutStreamer, 9, Subtarget); 1682 recordSled(CurSled, MI, SledKind::FUNCTION_ENTER, 2); 1683 } 1684 1685 void X86AsmPrinter::LowerPATCHABLE_RET(const MachineInstr &MI, 1686 X86MCInstLower &MCIL) { 1687 NoAutoPaddingScope NoPadScope(*OutStreamer); 1688 1689 // Since PATCHABLE_RET takes the opcode of the return statement as an 1690 // argument, we use that to emit the correct form of the RET that we want. 1691 // i.e. when we see this: 1692 // 1693 // PATCHABLE_RET X86::RET ... 1694 // 1695 // We should emit the RET followed by sleds. 1696 // 1697 // .p2align 1, ... 1698 // .Lxray_sled_N: 1699 // ret # or equivalent instruction 1700 // # 10 bytes worth of noops 1701 // 1702 // This just makes sure that the alignment for the next instruction is 2. 1703 auto CurSled = OutContext.createTempSymbol("xray_sled_", true); 1704 OutStreamer->emitCodeAlignment(2); 1705 OutStreamer->emitLabel(CurSled); 1706 unsigned OpCode = MI.getOperand(0).getImm(); 1707 MCInst Ret; 1708 Ret.setOpcode(OpCode); 1709 for (auto &MO : make_range(MI.operands_begin() + 1, MI.operands_end())) 1710 if (auto MaybeOperand = MCIL.LowerMachineOperand(&MI, MO)) 1711 Ret.addOperand(MaybeOperand.getValue()); 1712 OutStreamer->emitInstruction(Ret, getSubtargetInfo()); 1713 emitX86Nops(*OutStreamer, 10, Subtarget); 1714 recordSled(CurSled, MI, SledKind::FUNCTION_EXIT, 2); 1715 } 1716 1717 void X86AsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, 1718 X86MCInstLower &MCIL) { 1719 NoAutoPaddingScope NoPadScope(*OutStreamer); 1720 1721 // Like PATCHABLE_RET, we have the actual instruction in the operands to this 1722 // instruction so we lower that particular instruction and its operands. 1723 // Unlike PATCHABLE_RET though, we put the sled before the JMP, much like how 1724 // we do it for PATCHABLE_FUNCTION_ENTER. The sled should be very similar to 1725 // the PATCHABLE_FUNCTION_ENTER case, followed by the lowering of the actual 1726 // tail call much like how we have it in PATCHABLE_RET. 1727 auto CurSled = OutContext.createTempSymbol("xray_sled_", true); 1728 OutStreamer->emitCodeAlignment(2); 1729 OutStreamer->emitLabel(CurSled); 1730 auto Target = OutContext.createTempSymbol(); 1731 1732 // Use a two-byte `jmp`. This version of JMP takes an 8-bit relative offset as 1733 // an operand (computed as an offset from the jmp instruction). 1734 // FIXME: Find another less hacky way do force the relative jump. 1735 OutStreamer->emitBytes("\xeb\x09"); 1736 emitX86Nops(*OutStreamer, 9, Subtarget); 1737 OutStreamer->emitLabel(Target); 1738 recordSled(CurSled, MI, SledKind::TAIL_CALL, 2); 1739 1740 unsigned OpCode = MI.getOperand(0).getImm(); 1741 OpCode = convertTailJumpOpcode(OpCode); 1742 MCInst TC; 1743 TC.setOpcode(OpCode); 1744 1745 // Before emitting the instruction, add a comment to indicate that this is 1746 // indeed a tail call. 1747 OutStreamer->AddComment("TAILCALL"); 1748 for (auto &MO : make_range(MI.operands_begin() + 1, MI.operands_end())) 1749 if (auto MaybeOperand = MCIL.LowerMachineOperand(&MI, MO)) 1750 TC.addOperand(MaybeOperand.getValue()); 1751 OutStreamer->emitInstruction(TC, getSubtargetInfo()); 1752 } 1753 1754 // Returns instruction preceding MBBI in MachineFunction. 1755 // If MBBI is the first instruction of the first basic block, returns null. 1756 static MachineBasicBlock::const_iterator 1757 PrevCrossBBInst(MachineBasicBlock::const_iterator MBBI) { 1758 const MachineBasicBlock *MBB = MBBI->getParent(); 1759 while (MBBI == MBB->begin()) { 1760 if (MBB == &MBB->getParent()->front()) 1761 return MachineBasicBlock::const_iterator(); 1762 MBB = MBB->getPrevNode(); 1763 MBBI = MBB->end(); 1764 } 1765 --MBBI; 1766 return MBBI; 1767 } 1768 1769 static const Constant *getConstantFromPool(const MachineInstr &MI, 1770 const MachineOperand &Op) { 1771 if (!Op.isCPI() || Op.getOffset() != 0) 1772 return nullptr; 1773 1774 ArrayRef<MachineConstantPoolEntry> Constants = 1775 MI.getParent()->getParent()->getConstantPool()->getConstants(); 1776 const MachineConstantPoolEntry &ConstantEntry = Constants[Op.getIndex()]; 1777 1778 // Bail if this is a machine constant pool entry, we won't be able to dig out 1779 // anything useful. 1780 if (ConstantEntry.isMachineConstantPoolEntry()) 1781 return nullptr; 1782 1783 const Constant *C = ConstantEntry.Val.ConstVal; 1784 assert((!C || ConstantEntry.getType() == C->getType()) && 1785 "Expected a constant of the same type!"); 1786 return C; 1787 } 1788 1789 static std::string getShuffleComment(const MachineInstr *MI, unsigned SrcOp1Idx, 1790 unsigned SrcOp2Idx, ArrayRef<int> Mask) { 1791 std::string Comment; 1792 1793 // Compute the name for a register. This is really goofy because we have 1794 // multiple instruction printers that could (in theory) use different 1795 // names. Fortunately most people use the ATT style (outside of Windows) 1796 // and they actually agree on register naming here. Ultimately, this is 1797 // a comment, and so its OK if it isn't perfect. 1798 auto GetRegisterName = [](unsigned RegNum) -> StringRef { 1799 return X86ATTInstPrinter::getRegisterName(RegNum); 1800 }; 1801 1802 const MachineOperand &DstOp = MI->getOperand(0); 1803 const MachineOperand &SrcOp1 = MI->getOperand(SrcOp1Idx); 1804 const MachineOperand &SrcOp2 = MI->getOperand(SrcOp2Idx); 1805 1806 StringRef DstName = DstOp.isReg() ? GetRegisterName(DstOp.getReg()) : "mem"; 1807 StringRef Src1Name = 1808 SrcOp1.isReg() ? GetRegisterName(SrcOp1.getReg()) : "mem"; 1809 StringRef Src2Name = 1810 SrcOp2.isReg() ? GetRegisterName(SrcOp2.getReg()) : "mem"; 1811 1812 // One source operand, fix the mask to print all elements in one span. 1813 SmallVector<int, 8> ShuffleMask(Mask.begin(), Mask.end()); 1814 if (Src1Name == Src2Name) 1815 for (int i = 0, e = ShuffleMask.size(); i != e; ++i) 1816 if (ShuffleMask[i] >= e) 1817 ShuffleMask[i] -= e; 1818 1819 raw_string_ostream CS(Comment); 1820 CS << DstName; 1821 1822 // Handle AVX512 MASK/MASXZ write mask comments. 1823 // MASK: zmmX {%kY} 1824 // MASKZ: zmmX {%kY} {z} 1825 if (SrcOp1Idx > 1) { 1826 assert((SrcOp1Idx == 2 || SrcOp1Idx == 3) && "Unexpected writemask"); 1827 1828 const MachineOperand &WriteMaskOp = MI->getOperand(SrcOp1Idx - 1); 1829 if (WriteMaskOp.isReg()) { 1830 CS << " {%" << GetRegisterName(WriteMaskOp.getReg()) << "}"; 1831 1832 if (SrcOp1Idx == 2) { 1833 CS << " {z}"; 1834 } 1835 } 1836 } 1837 1838 CS << " = "; 1839 1840 for (int i = 0, e = ShuffleMask.size(); i != e; ++i) { 1841 if (i != 0) 1842 CS << ","; 1843 if (ShuffleMask[i] == SM_SentinelZero) { 1844 CS << "zero"; 1845 continue; 1846 } 1847 1848 // Otherwise, it must come from src1 or src2. Print the span of elements 1849 // that comes from this src. 1850 bool isSrc1 = ShuffleMask[i] < (int)e; 1851 CS << (isSrc1 ? Src1Name : Src2Name) << '['; 1852 1853 bool IsFirst = true; 1854 while (i != e && ShuffleMask[i] != SM_SentinelZero && 1855 (ShuffleMask[i] < (int)e) == isSrc1) { 1856 if (!IsFirst) 1857 CS << ','; 1858 else 1859 IsFirst = false; 1860 if (ShuffleMask[i] == SM_SentinelUndef) 1861 CS << "u"; 1862 else 1863 CS << ShuffleMask[i] % (int)e; 1864 ++i; 1865 } 1866 CS << ']'; 1867 --i; // For loop increments element #. 1868 } 1869 CS.flush(); 1870 1871 return Comment; 1872 } 1873 1874 static void printConstant(const APInt &Val, raw_ostream &CS) { 1875 if (Val.getBitWidth() <= 64) { 1876 CS << Val.getZExtValue(); 1877 } else { 1878 // print multi-word constant as (w0,w1) 1879 CS << "("; 1880 for (int i = 0, N = Val.getNumWords(); i < N; ++i) { 1881 if (i > 0) 1882 CS << ","; 1883 CS << Val.getRawData()[i]; 1884 } 1885 CS << ")"; 1886 } 1887 } 1888 1889 static void printConstant(const APFloat &Flt, raw_ostream &CS) { 1890 SmallString<32> Str; 1891 // Force scientific notation to distinquish from integers. 1892 Flt.toString(Str, 0, 0); 1893 CS << Str; 1894 } 1895 1896 static void printConstant(const Constant *COp, raw_ostream &CS) { 1897 if (isa<UndefValue>(COp)) { 1898 CS << "u"; 1899 } else if (auto *CI = dyn_cast<ConstantInt>(COp)) { 1900 printConstant(CI->getValue(), CS); 1901 } else if (auto *CF = dyn_cast<ConstantFP>(COp)) { 1902 printConstant(CF->getValueAPF(), CS); 1903 } else { 1904 CS << "?"; 1905 } 1906 } 1907 1908 void X86AsmPrinter::EmitSEHInstruction(const MachineInstr *MI) { 1909 assert(MF->hasWinCFI() && "SEH_ instruction in function without WinCFI?"); 1910 assert(getSubtarget().isOSWindows() && "SEH_ instruction Windows only"); 1911 1912 // Use the .cv_fpo directives if we're emitting CodeView on 32-bit x86. 1913 if (EmitFPOData) { 1914 X86TargetStreamer *XTS = 1915 static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()); 1916 switch (MI->getOpcode()) { 1917 case X86::SEH_PushReg: 1918 XTS->emitFPOPushReg(MI->getOperand(0).getImm()); 1919 break; 1920 case X86::SEH_StackAlloc: 1921 XTS->emitFPOStackAlloc(MI->getOperand(0).getImm()); 1922 break; 1923 case X86::SEH_StackAlign: 1924 XTS->emitFPOStackAlign(MI->getOperand(0).getImm()); 1925 break; 1926 case X86::SEH_SetFrame: 1927 assert(MI->getOperand(1).getImm() == 0 && 1928 ".cv_fpo_setframe takes no offset"); 1929 XTS->emitFPOSetFrame(MI->getOperand(0).getImm()); 1930 break; 1931 case X86::SEH_EndPrologue: 1932 XTS->emitFPOEndPrologue(); 1933 break; 1934 case X86::SEH_SaveReg: 1935 case X86::SEH_SaveXMM: 1936 case X86::SEH_PushFrame: 1937 llvm_unreachable("SEH_ directive incompatible with FPO"); 1938 break; 1939 default: 1940 llvm_unreachable("expected SEH_ instruction"); 1941 } 1942 return; 1943 } 1944 1945 // Otherwise, use the .seh_ directives for all other Windows platforms. 1946 switch (MI->getOpcode()) { 1947 case X86::SEH_PushReg: 1948 OutStreamer->EmitWinCFIPushReg(MI->getOperand(0).getImm()); 1949 break; 1950 1951 case X86::SEH_SaveReg: 1952 OutStreamer->EmitWinCFISaveReg(MI->getOperand(0).getImm(), 1953 MI->getOperand(1).getImm()); 1954 break; 1955 1956 case X86::SEH_SaveXMM: 1957 OutStreamer->EmitWinCFISaveXMM(MI->getOperand(0).getImm(), 1958 MI->getOperand(1).getImm()); 1959 break; 1960 1961 case X86::SEH_StackAlloc: 1962 OutStreamer->EmitWinCFIAllocStack(MI->getOperand(0).getImm()); 1963 break; 1964 1965 case X86::SEH_SetFrame: 1966 OutStreamer->EmitWinCFISetFrame(MI->getOperand(0).getImm(), 1967 MI->getOperand(1).getImm()); 1968 break; 1969 1970 case X86::SEH_PushFrame: 1971 OutStreamer->EmitWinCFIPushFrame(MI->getOperand(0).getImm()); 1972 break; 1973 1974 case X86::SEH_EndPrologue: 1975 OutStreamer->EmitWinCFIEndProlog(); 1976 break; 1977 1978 default: 1979 llvm_unreachable("expected SEH_ instruction"); 1980 } 1981 } 1982 1983 static unsigned getRegisterWidth(const MCOperandInfo &Info) { 1984 if (Info.RegClass == X86::VR128RegClassID || 1985 Info.RegClass == X86::VR128XRegClassID) 1986 return 128; 1987 if (Info.RegClass == X86::VR256RegClassID || 1988 Info.RegClass == X86::VR256XRegClassID) 1989 return 256; 1990 if (Info.RegClass == X86::VR512RegClassID) 1991 return 512; 1992 llvm_unreachable("Unknown register class!"); 1993 } 1994 1995 static void addConstantComments(const MachineInstr *MI, 1996 MCStreamer &OutStreamer) { 1997 switch (MI->getOpcode()) { 1998 // Lower PSHUFB and VPERMILP normally but add a comment if we can find 1999 // a constant shuffle mask. We won't be able to do this at the MC layer 2000 // because the mask isn't an immediate. 2001 case X86::PSHUFBrm: 2002 case X86::VPSHUFBrm: 2003 case X86::VPSHUFBYrm: 2004 case X86::VPSHUFBZ128rm: 2005 case X86::VPSHUFBZ128rmk: 2006 case X86::VPSHUFBZ128rmkz: 2007 case X86::VPSHUFBZ256rm: 2008 case X86::VPSHUFBZ256rmk: 2009 case X86::VPSHUFBZ256rmkz: 2010 case X86::VPSHUFBZrm: 2011 case X86::VPSHUFBZrmk: 2012 case X86::VPSHUFBZrmkz: { 2013 unsigned SrcIdx = 1; 2014 if (X86II::isKMasked(MI->getDesc().TSFlags)) { 2015 // Skip mask operand. 2016 ++SrcIdx; 2017 if (X86II::isKMergeMasked(MI->getDesc().TSFlags)) { 2018 // Skip passthru operand. 2019 ++SrcIdx; 2020 } 2021 } 2022 unsigned MaskIdx = SrcIdx + 1 + X86::AddrDisp; 2023 2024 assert(MI->getNumOperands() >= (SrcIdx + 1 + X86::AddrNumOperands) && 2025 "Unexpected number of operands!"); 2026 2027 const MachineOperand &MaskOp = MI->getOperand(MaskIdx); 2028 if (auto *C = getConstantFromPool(*MI, MaskOp)) { 2029 unsigned Width = getRegisterWidth(MI->getDesc().OpInfo[0]); 2030 SmallVector<int, 64> Mask; 2031 DecodePSHUFBMask(C, Width, Mask); 2032 if (!Mask.empty()) 2033 OutStreamer.AddComment(getShuffleComment(MI, SrcIdx, SrcIdx, Mask)); 2034 } 2035 break; 2036 } 2037 2038 case X86::VPERMILPSrm: 2039 case X86::VPERMILPSYrm: 2040 case X86::VPERMILPSZ128rm: 2041 case X86::VPERMILPSZ128rmk: 2042 case X86::VPERMILPSZ128rmkz: 2043 case X86::VPERMILPSZ256rm: 2044 case X86::VPERMILPSZ256rmk: 2045 case X86::VPERMILPSZ256rmkz: 2046 case X86::VPERMILPSZrm: 2047 case X86::VPERMILPSZrmk: 2048 case X86::VPERMILPSZrmkz: 2049 case X86::VPERMILPDrm: 2050 case X86::VPERMILPDYrm: 2051 case X86::VPERMILPDZ128rm: 2052 case X86::VPERMILPDZ128rmk: 2053 case X86::VPERMILPDZ128rmkz: 2054 case X86::VPERMILPDZ256rm: 2055 case X86::VPERMILPDZ256rmk: 2056 case X86::VPERMILPDZ256rmkz: 2057 case X86::VPERMILPDZrm: 2058 case X86::VPERMILPDZrmk: 2059 case X86::VPERMILPDZrmkz: { 2060 unsigned ElSize; 2061 switch (MI->getOpcode()) { 2062 default: llvm_unreachable("Invalid opcode"); 2063 case X86::VPERMILPSrm: 2064 case X86::VPERMILPSYrm: 2065 case X86::VPERMILPSZ128rm: 2066 case X86::VPERMILPSZ256rm: 2067 case X86::VPERMILPSZrm: 2068 case X86::VPERMILPSZ128rmkz: 2069 case X86::VPERMILPSZ256rmkz: 2070 case X86::VPERMILPSZrmkz: 2071 case X86::VPERMILPSZ128rmk: 2072 case X86::VPERMILPSZ256rmk: 2073 case X86::VPERMILPSZrmk: 2074 ElSize = 32; 2075 break; 2076 case X86::VPERMILPDrm: 2077 case X86::VPERMILPDYrm: 2078 case X86::VPERMILPDZ128rm: 2079 case X86::VPERMILPDZ256rm: 2080 case X86::VPERMILPDZrm: 2081 case X86::VPERMILPDZ128rmkz: 2082 case X86::VPERMILPDZ256rmkz: 2083 case X86::VPERMILPDZrmkz: 2084 case X86::VPERMILPDZ128rmk: 2085 case X86::VPERMILPDZ256rmk: 2086 case X86::VPERMILPDZrmk: 2087 ElSize = 64; 2088 break; 2089 } 2090 2091 unsigned SrcIdx = 1; 2092 if (X86II::isKMasked(MI->getDesc().TSFlags)) { 2093 // Skip mask operand. 2094 ++SrcIdx; 2095 if (X86II::isKMergeMasked(MI->getDesc().TSFlags)) { 2096 // Skip passthru operand. 2097 ++SrcIdx; 2098 } 2099 } 2100 unsigned MaskIdx = SrcIdx + 1 + X86::AddrDisp; 2101 2102 assert(MI->getNumOperands() >= (SrcIdx + 1 + X86::AddrNumOperands) && 2103 "Unexpected number of operands!"); 2104 2105 const MachineOperand &MaskOp = MI->getOperand(MaskIdx); 2106 if (auto *C = getConstantFromPool(*MI, MaskOp)) { 2107 unsigned Width = getRegisterWidth(MI->getDesc().OpInfo[0]); 2108 SmallVector<int, 16> Mask; 2109 DecodeVPERMILPMask(C, ElSize, Width, Mask); 2110 if (!Mask.empty()) 2111 OutStreamer.AddComment(getShuffleComment(MI, SrcIdx, SrcIdx, Mask)); 2112 } 2113 break; 2114 } 2115 2116 case X86::VPERMIL2PDrm: 2117 case X86::VPERMIL2PSrm: 2118 case X86::VPERMIL2PDYrm: 2119 case X86::VPERMIL2PSYrm: { 2120 assert(MI->getNumOperands() >= (3 + X86::AddrNumOperands + 1) && 2121 "Unexpected number of operands!"); 2122 2123 const MachineOperand &CtrlOp = MI->getOperand(MI->getNumOperands() - 1); 2124 if (!CtrlOp.isImm()) 2125 break; 2126 2127 unsigned ElSize; 2128 switch (MI->getOpcode()) { 2129 default: llvm_unreachable("Invalid opcode"); 2130 case X86::VPERMIL2PSrm: case X86::VPERMIL2PSYrm: ElSize = 32; break; 2131 case X86::VPERMIL2PDrm: case X86::VPERMIL2PDYrm: ElSize = 64; break; 2132 } 2133 2134 const MachineOperand &MaskOp = MI->getOperand(3 + X86::AddrDisp); 2135 if (auto *C = getConstantFromPool(*MI, MaskOp)) { 2136 unsigned Width = getRegisterWidth(MI->getDesc().OpInfo[0]); 2137 SmallVector<int, 16> Mask; 2138 DecodeVPERMIL2PMask(C, (unsigned)CtrlOp.getImm(), ElSize, Width, Mask); 2139 if (!Mask.empty()) 2140 OutStreamer.AddComment(getShuffleComment(MI, 1, 2, Mask)); 2141 } 2142 break; 2143 } 2144 2145 case X86::VPPERMrrm: { 2146 assert(MI->getNumOperands() >= (3 + X86::AddrNumOperands) && 2147 "Unexpected number of operands!"); 2148 2149 const MachineOperand &MaskOp = MI->getOperand(3 + X86::AddrDisp); 2150 if (auto *C = getConstantFromPool(*MI, MaskOp)) { 2151 unsigned Width = getRegisterWidth(MI->getDesc().OpInfo[0]); 2152 SmallVector<int, 16> Mask; 2153 DecodeVPPERMMask(C, Width, Mask); 2154 if (!Mask.empty()) 2155 OutStreamer.AddComment(getShuffleComment(MI, 1, 2, Mask)); 2156 } 2157 break; 2158 } 2159 2160 case X86::MMX_MOVQ64rm: { 2161 assert(MI->getNumOperands() == (1 + X86::AddrNumOperands) && 2162 "Unexpected number of operands!"); 2163 if (auto *C = getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) { 2164 std::string Comment; 2165 raw_string_ostream CS(Comment); 2166 const MachineOperand &DstOp = MI->getOperand(0); 2167 CS << X86ATTInstPrinter::getRegisterName(DstOp.getReg()) << " = "; 2168 if (auto *CF = dyn_cast<ConstantFP>(C)) { 2169 CS << "0x" << CF->getValueAPF().bitcastToAPInt().toString(16, false); 2170 OutStreamer.AddComment(CS.str()); 2171 } 2172 } 2173 break; 2174 } 2175 2176 #define MOV_CASE(Prefix, Suffix) \ 2177 case X86::Prefix##MOVAPD##Suffix##rm: \ 2178 case X86::Prefix##MOVAPS##Suffix##rm: \ 2179 case X86::Prefix##MOVUPD##Suffix##rm: \ 2180 case X86::Prefix##MOVUPS##Suffix##rm: \ 2181 case X86::Prefix##MOVDQA##Suffix##rm: \ 2182 case X86::Prefix##MOVDQU##Suffix##rm: 2183 2184 #define MOV_AVX512_CASE(Suffix) \ 2185 case X86::VMOVDQA64##Suffix##rm: \ 2186 case X86::VMOVDQA32##Suffix##rm: \ 2187 case X86::VMOVDQU64##Suffix##rm: \ 2188 case X86::VMOVDQU32##Suffix##rm: \ 2189 case X86::VMOVDQU16##Suffix##rm: \ 2190 case X86::VMOVDQU8##Suffix##rm: \ 2191 case X86::VMOVAPS##Suffix##rm: \ 2192 case X86::VMOVAPD##Suffix##rm: \ 2193 case X86::VMOVUPS##Suffix##rm: \ 2194 case X86::VMOVUPD##Suffix##rm: 2195 2196 #define CASE_ALL_MOV_RM() \ 2197 MOV_CASE(, ) /* SSE */ \ 2198 MOV_CASE(V, ) /* AVX-128 */ \ 2199 MOV_CASE(V, Y) /* AVX-256 */ \ 2200 MOV_AVX512_CASE(Z) \ 2201 MOV_AVX512_CASE(Z256) \ 2202 MOV_AVX512_CASE(Z128) 2203 2204 // For loads from a constant pool to a vector register, print the constant 2205 // loaded. 2206 CASE_ALL_MOV_RM() 2207 case X86::VBROADCASTF128: 2208 case X86::VBROADCASTI128: 2209 case X86::VBROADCASTF32X4Z256rm: 2210 case X86::VBROADCASTF32X4rm: 2211 case X86::VBROADCASTF32X8rm: 2212 case X86::VBROADCASTF64X2Z128rm: 2213 case X86::VBROADCASTF64X2rm: 2214 case X86::VBROADCASTF64X4rm: 2215 case X86::VBROADCASTI32X4Z256rm: 2216 case X86::VBROADCASTI32X4rm: 2217 case X86::VBROADCASTI32X8rm: 2218 case X86::VBROADCASTI64X2Z128rm: 2219 case X86::VBROADCASTI64X2rm: 2220 case X86::VBROADCASTI64X4rm: 2221 assert(MI->getNumOperands() >= (1 + X86::AddrNumOperands) && 2222 "Unexpected number of operands!"); 2223 if (auto *C = getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) { 2224 int NumLanes = 1; 2225 // Override NumLanes for the broadcast instructions. 2226 switch (MI->getOpcode()) { 2227 case X86::VBROADCASTF128: NumLanes = 2; break; 2228 case X86::VBROADCASTI128: NumLanes = 2; break; 2229 case X86::VBROADCASTF32X4Z256rm: NumLanes = 2; break; 2230 case X86::VBROADCASTF32X4rm: NumLanes = 4; break; 2231 case X86::VBROADCASTF32X8rm: NumLanes = 2; break; 2232 case X86::VBROADCASTF64X2Z128rm: NumLanes = 2; break; 2233 case X86::VBROADCASTF64X2rm: NumLanes = 4; break; 2234 case X86::VBROADCASTF64X4rm: NumLanes = 2; break; 2235 case X86::VBROADCASTI32X4Z256rm: NumLanes = 2; break; 2236 case X86::VBROADCASTI32X4rm: NumLanes = 4; break; 2237 case X86::VBROADCASTI32X8rm: NumLanes = 2; break; 2238 case X86::VBROADCASTI64X2Z128rm: NumLanes = 2; break; 2239 case X86::VBROADCASTI64X2rm: NumLanes = 4; break; 2240 case X86::VBROADCASTI64X4rm: NumLanes = 2; break; 2241 } 2242 2243 std::string Comment; 2244 raw_string_ostream CS(Comment); 2245 const MachineOperand &DstOp = MI->getOperand(0); 2246 CS << X86ATTInstPrinter::getRegisterName(DstOp.getReg()) << " = "; 2247 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) { 2248 CS << "["; 2249 for (int l = 0; l != NumLanes; ++l) { 2250 for (int i = 0, NumElements = CDS->getNumElements(); i < NumElements; 2251 ++i) { 2252 if (i != 0 || l != 0) 2253 CS << ","; 2254 if (CDS->getElementType()->isIntegerTy()) 2255 printConstant(CDS->getElementAsAPInt(i), CS); 2256 else if (CDS->getElementType()->isHalfTy() || 2257 CDS->getElementType()->isFloatTy() || 2258 CDS->getElementType()->isDoubleTy()) 2259 printConstant(CDS->getElementAsAPFloat(i), CS); 2260 else 2261 CS << "?"; 2262 } 2263 } 2264 CS << "]"; 2265 OutStreamer.AddComment(CS.str()); 2266 } else if (auto *CV = dyn_cast<ConstantVector>(C)) { 2267 CS << "<"; 2268 for (int l = 0; l != NumLanes; ++l) { 2269 for (int i = 0, NumOperands = CV->getNumOperands(); i < NumOperands; 2270 ++i) { 2271 if (i != 0 || l != 0) 2272 CS << ","; 2273 printConstant(CV->getOperand(i), CS); 2274 } 2275 } 2276 CS << ">"; 2277 OutStreamer.AddComment(CS.str()); 2278 } 2279 } 2280 break; 2281 2282 case X86::MOVDDUPrm: 2283 case X86::VMOVDDUPrm: 2284 case X86::VMOVDDUPZ128rm: 2285 case X86::VBROADCASTSSrm: 2286 case X86::VBROADCASTSSYrm: 2287 case X86::VBROADCASTSSZ128rm: 2288 case X86::VBROADCASTSSZ256rm: 2289 case X86::VBROADCASTSSZrm: 2290 case X86::VBROADCASTSDYrm: 2291 case X86::VBROADCASTSDZ256rm: 2292 case X86::VBROADCASTSDZrm: 2293 case X86::VPBROADCASTBrm: 2294 case X86::VPBROADCASTBYrm: 2295 case X86::VPBROADCASTBZ128rm: 2296 case X86::VPBROADCASTBZ256rm: 2297 case X86::VPBROADCASTBZrm: 2298 case X86::VPBROADCASTDrm: 2299 case X86::VPBROADCASTDYrm: 2300 case X86::VPBROADCASTDZ128rm: 2301 case X86::VPBROADCASTDZ256rm: 2302 case X86::VPBROADCASTDZrm: 2303 case X86::VPBROADCASTQrm: 2304 case X86::VPBROADCASTQYrm: 2305 case X86::VPBROADCASTQZ128rm: 2306 case X86::VPBROADCASTQZ256rm: 2307 case X86::VPBROADCASTQZrm: 2308 case X86::VPBROADCASTWrm: 2309 case X86::VPBROADCASTWYrm: 2310 case X86::VPBROADCASTWZ128rm: 2311 case X86::VPBROADCASTWZ256rm: 2312 case X86::VPBROADCASTWZrm: 2313 assert(MI->getNumOperands() >= (1 + X86::AddrNumOperands) && 2314 "Unexpected number of operands!"); 2315 if (auto *C = getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) { 2316 int NumElts; 2317 switch (MI->getOpcode()) { 2318 default: llvm_unreachable("Invalid opcode"); 2319 case X86::MOVDDUPrm: NumElts = 2; break; 2320 case X86::VMOVDDUPrm: NumElts = 2; break; 2321 case X86::VMOVDDUPZ128rm: NumElts = 2; break; 2322 case X86::VBROADCASTSSrm: NumElts = 4; break; 2323 case X86::VBROADCASTSSYrm: NumElts = 8; break; 2324 case X86::VBROADCASTSSZ128rm: NumElts = 4; break; 2325 case X86::VBROADCASTSSZ256rm: NumElts = 8; break; 2326 case X86::VBROADCASTSSZrm: NumElts = 16; break; 2327 case X86::VBROADCASTSDYrm: NumElts = 4; break; 2328 case X86::VBROADCASTSDZ256rm: NumElts = 4; break; 2329 case X86::VBROADCASTSDZrm: NumElts = 8; break; 2330 case X86::VPBROADCASTBrm: NumElts = 16; break; 2331 case X86::VPBROADCASTBYrm: NumElts = 32; break; 2332 case X86::VPBROADCASTBZ128rm: NumElts = 16; break; 2333 case X86::VPBROADCASTBZ256rm: NumElts = 32; break; 2334 case X86::VPBROADCASTBZrm: NumElts = 64; break; 2335 case X86::VPBROADCASTDrm: NumElts = 4; break; 2336 case X86::VPBROADCASTDYrm: NumElts = 8; break; 2337 case X86::VPBROADCASTDZ128rm: NumElts = 4; break; 2338 case X86::VPBROADCASTDZ256rm: NumElts = 8; break; 2339 case X86::VPBROADCASTDZrm: NumElts = 16; break; 2340 case X86::VPBROADCASTQrm: NumElts = 2; break; 2341 case X86::VPBROADCASTQYrm: NumElts = 4; break; 2342 case X86::VPBROADCASTQZ128rm: NumElts = 2; break; 2343 case X86::VPBROADCASTQZ256rm: NumElts = 4; break; 2344 case X86::VPBROADCASTQZrm: NumElts = 8; break; 2345 case X86::VPBROADCASTWrm: NumElts = 8; break; 2346 case X86::VPBROADCASTWYrm: NumElts = 16; break; 2347 case X86::VPBROADCASTWZ128rm: NumElts = 8; break; 2348 case X86::VPBROADCASTWZ256rm: NumElts = 16; break; 2349 case X86::VPBROADCASTWZrm: NumElts = 32; break; 2350 } 2351 2352 std::string Comment; 2353 raw_string_ostream CS(Comment); 2354 const MachineOperand &DstOp = MI->getOperand(0); 2355 CS << X86ATTInstPrinter::getRegisterName(DstOp.getReg()) << " = "; 2356 CS << "["; 2357 for (int i = 0; i != NumElts; ++i) { 2358 if (i != 0) 2359 CS << ","; 2360 printConstant(C, CS); 2361 } 2362 CS << "]"; 2363 OutStreamer.AddComment(CS.str()); 2364 } 2365 } 2366 } 2367 2368 void X86AsmPrinter::emitInstruction(const MachineInstr *MI) { 2369 X86MCInstLower MCInstLowering(*MF, *this); 2370 const X86RegisterInfo *RI = 2371 MF->getSubtarget<X86Subtarget>().getRegisterInfo(); 2372 2373 // Add a comment about EVEX-2-VEX compression for AVX-512 instrs that 2374 // are compressed from EVEX encoding to VEX encoding. 2375 if (TM.Options.MCOptions.ShowMCEncoding) { 2376 if (MI->getAsmPrinterFlags() & X86::AC_EVEX_2_VEX) 2377 OutStreamer->AddComment("EVEX TO VEX Compression ", false); 2378 } 2379 2380 // Add comments for values loaded from constant pool. 2381 if (OutStreamer->isVerboseAsm()) 2382 addConstantComments(MI, *OutStreamer); 2383 2384 switch (MI->getOpcode()) { 2385 case TargetOpcode::DBG_VALUE: 2386 llvm_unreachable("Should be handled target independently"); 2387 2388 // Emit nothing here but a comment if we can. 2389 case X86::Int_MemBarrier: 2390 OutStreamer->emitRawComment("MEMBARRIER"); 2391 return; 2392 2393 case X86::EH_RETURN: 2394 case X86::EH_RETURN64: { 2395 // Lower these as normal, but add some comments. 2396 Register Reg = MI->getOperand(0).getReg(); 2397 OutStreamer->AddComment(StringRef("eh_return, addr: %") + 2398 X86ATTInstPrinter::getRegisterName(Reg)); 2399 break; 2400 } 2401 case X86::CLEANUPRET: { 2402 // Lower these as normal, but add some comments. 2403 OutStreamer->AddComment("CLEANUPRET"); 2404 break; 2405 } 2406 2407 case X86::CATCHRET: { 2408 // Lower these as normal, but add some comments. 2409 OutStreamer->AddComment("CATCHRET"); 2410 break; 2411 } 2412 2413 case X86::ENDBR32: 2414 case X86::ENDBR64: { 2415 // CurrentPatchableFunctionEntrySym can be CurrentFnBegin only for 2416 // -fpatchable-function-entry=N,0. The entry MBB is guaranteed to be 2417 // non-empty. If MI is the initial ENDBR, place the 2418 // __patchable_function_entries label after ENDBR. 2419 if (CurrentPatchableFunctionEntrySym && 2420 CurrentPatchableFunctionEntrySym == CurrentFnBegin && 2421 MI == &MF->front().front()) { 2422 MCInst Inst; 2423 MCInstLowering.Lower(MI, Inst); 2424 EmitAndCountInstruction(Inst); 2425 CurrentPatchableFunctionEntrySym = createTempSymbol("patch"); 2426 OutStreamer->emitLabel(CurrentPatchableFunctionEntrySym); 2427 return; 2428 } 2429 break; 2430 } 2431 2432 case X86::TAILJMPr: 2433 case X86::TAILJMPm: 2434 case X86::TAILJMPd: 2435 case X86::TAILJMPd_CC: 2436 case X86::TAILJMPr64: 2437 case X86::TAILJMPm64: 2438 case X86::TAILJMPd64: 2439 case X86::TAILJMPd64_CC: 2440 case X86::TAILJMPr64_REX: 2441 case X86::TAILJMPm64_REX: 2442 // Lower these as normal, but add some comments. 2443 OutStreamer->AddComment("TAILCALL"); 2444 break; 2445 2446 case X86::TLS_addr32: 2447 case X86::TLS_addr64: 2448 case X86::TLS_base_addr32: 2449 case X86::TLS_base_addr64: 2450 return LowerTlsAddr(MCInstLowering, *MI); 2451 2452 case X86::MOVPC32r: { 2453 // This is a pseudo op for a two instruction sequence with a label, which 2454 // looks like: 2455 // call "L1$pb" 2456 // "L1$pb": 2457 // popl %esi 2458 2459 // Emit the call. 2460 MCSymbol *PICBase = MF->getPICBaseSymbol(); 2461 // FIXME: We would like an efficient form for this, so we don't have to do a 2462 // lot of extra uniquing. 2463 EmitAndCountInstruction( 2464 MCInstBuilder(X86::CALLpcrel32) 2465 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext))); 2466 2467 const X86FrameLowering *FrameLowering = 2468 MF->getSubtarget<X86Subtarget>().getFrameLowering(); 2469 bool hasFP = FrameLowering->hasFP(*MF); 2470 2471 // TODO: This is needed only if we require precise CFA. 2472 bool HasActiveDwarfFrame = OutStreamer->getNumFrameInfos() && 2473 !OutStreamer->getDwarfFrameInfos().back().End; 2474 2475 int stackGrowth = -RI->getSlotSize(); 2476 2477 if (HasActiveDwarfFrame && !hasFP) { 2478 OutStreamer->emitCFIAdjustCfaOffset(-stackGrowth); 2479 } 2480 2481 // Emit the label. 2482 OutStreamer->emitLabel(PICBase); 2483 2484 // popl $reg 2485 EmitAndCountInstruction( 2486 MCInstBuilder(X86::POP32r).addReg(MI->getOperand(0).getReg())); 2487 2488 if (HasActiveDwarfFrame && !hasFP) { 2489 OutStreamer->emitCFIAdjustCfaOffset(stackGrowth); 2490 } 2491 return; 2492 } 2493 2494 case X86::ADD32ri: { 2495 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri. 2496 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS) 2497 break; 2498 2499 // Okay, we have something like: 2500 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL) 2501 2502 // For this, we want to print something like: 2503 // MYGLOBAL + (. - PICBASE) 2504 // However, we can't generate a ".", so just emit a new label here and refer 2505 // to it. 2506 MCSymbol *DotSym = OutContext.createTempSymbol(); 2507 OutStreamer->emitLabel(DotSym); 2508 2509 // Now that we have emitted the label, lower the complex operand expression. 2510 MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2)); 2511 2512 const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext); 2513 const MCExpr *PICBase = 2514 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), OutContext); 2515 DotExpr = MCBinaryExpr::createSub(DotExpr, PICBase, OutContext); 2516 2517 DotExpr = MCBinaryExpr::createAdd( 2518 MCSymbolRefExpr::create(OpSym, OutContext), DotExpr, OutContext); 2519 2520 EmitAndCountInstruction(MCInstBuilder(X86::ADD32ri) 2521 .addReg(MI->getOperand(0).getReg()) 2522 .addReg(MI->getOperand(1).getReg()) 2523 .addExpr(DotExpr)); 2524 return; 2525 } 2526 case TargetOpcode::STATEPOINT: 2527 return LowerSTATEPOINT(*MI, MCInstLowering); 2528 2529 case TargetOpcode::FAULTING_OP: 2530 return LowerFAULTING_OP(*MI, MCInstLowering); 2531 2532 case TargetOpcode::FENTRY_CALL: 2533 return LowerFENTRY_CALL(*MI, MCInstLowering); 2534 2535 case TargetOpcode::PATCHABLE_OP: 2536 return LowerPATCHABLE_OP(*MI, MCInstLowering); 2537 2538 case TargetOpcode::STACKMAP: 2539 return LowerSTACKMAP(*MI); 2540 2541 case TargetOpcode::PATCHPOINT: 2542 return LowerPATCHPOINT(*MI, MCInstLowering); 2543 2544 case TargetOpcode::PATCHABLE_FUNCTION_ENTER: 2545 return LowerPATCHABLE_FUNCTION_ENTER(*MI, MCInstLowering); 2546 2547 case TargetOpcode::PATCHABLE_RET: 2548 return LowerPATCHABLE_RET(*MI, MCInstLowering); 2549 2550 case TargetOpcode::PATCHABLE_TAIL_CALL: 2551 return LowerPATCHABLE_TAIL_CALL(*MI, MCInstLowering); 2552 2553 case TargetOpcode::PATCHABLE_EVENT_CALL: 2554 return LowerPATCHABLE_EVENT_CALL(*MI, MCInstLowering); 2555 2556 case TargetOpcode::PATCHABLE_TYPED_EVENT_CALL: 2557 return LowerPATCHABLE_TYPED_EVENT_CALL(*MI, MCInstLowering); 2558 2559 case X86::MORESTACK_RET: 2560 EmitAndCountInstruction(MCInstBuilder(getRetOpcode(*Subtarget))); 2561 return; 2562 2563 case X86::MORESTACK_RET_RESTORE_R10: 2564 // Return, then restore R10. 2565 EmitAndCountInstruction(MCInstBuilder(getRetOpcode(*Subtarget))); 2566 EmitAndCountInstruction( 2567 MCInstBuilder(X86::MOV64rr).addReg(X86::R10).addReg(X86::RAX)); 2568 return; 2569 2570 case X86::SEH_PushReg: 2571 case X86::SEH_SaveReg: 2572 case X86::SEH_SaveXMM: 2573 case X86::SEH_StackAlloc: 2574 case X86::SEH_StackAlign: 2575 case X86::SEH_SetFrame: 2576 case X86::SEH_PushFrame: 2577 case X86::SEH_EndPrologue: 2578 EmitSEHInstruction(MI); 2579 return; 2580 2581 case X86::SEH_Epilogue: { 2582 assert(MF->hasWinCFI() && "SEH_ instruction in function without WinCFI?"); 2583 MachineBasicBlock::const_iterator MBBI(MI); 2584 // Check if preceded by a call and emit nop if so. 2585 for (MBBI = PrevCrossBBInst(MBBI); 2586 MBBI != MachineBasicBlock::const_iterator(); 2587 MBBI = PrevCrossBBInst(MBBI)) { 2588 // Conservatively assume that pseudo instructions don't emit code and keep 2589 // looking for a call. We may emit an unnecessary nop in some cases. 2590 if (!MBBI->isPseudo()) { 2591 if (MBBI->isCall()) 2592 EmitAndCountInstruction(MCInstBuilder(X86::NOOP)); 2593 break; 2594 } 2595 } 2596 return; 2597 } 2598 } 2599 2600 MCInst TmpInst; 2601 MCInstLowering.Lower(MI, TmpInst); 2602 2603 // Stackmap shadows cannot include branch targets, so we can count the bytes 2604 // in a call towards the shadow, but must ensure that the no thread returns 2605 // in to the stackmap shadow. The only way to achieve this is if the call 2606 // is at the end of the shadow. 2607 if (MI->isCall()) { 2608 // Count then size of the call towards the shadow 2609 SMShadowTracker.count(TmpInst, getSubtargetInfo(), CodeEmitter.get()); 2610 // Then flush the shadow so that we fill with nops before the call, not 2611 // after it. 2612 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo()); 2613 // Then emit the call 2614 OutStreamer->emitInstruction(TmpInst, getSubtargetInfo()); 2615 return; 2616 } 2617 2618 EmitAndCountInstruction(TmpInst); 2619 } 2620