1 //===-- X86MCInstLower.cpp - Convert X86 MachineInstr to an MCInst --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower X86 MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
15 #include "X86AsmPrinter.h"
16 #include "InstPrinter/X86ATTInstPrinter.h"
17 #include "X86COFFMachineModuleInfo.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
20 #include "llvm/CodeGen/StackMaps.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstBuilder.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Target/Mangler.h"
35 /// X86MCInstLower - This class is used to lower an MachineInstr into an MCInst.
36 class X86MCInstLower {
38 const MachineFunction &MF;
39 const TargetMachine &TM;
41 X86AsmPrinter &AsmPrinter;
43 X86MCInstLower(const MachineFunction &MF, X86AsmPrinter &asmprinter);
45 void Lower(const MachineInstr *MI, MCInst &OutMI) const;
47 MCSymbol *GetSymbolFromOperand(const MachineOperand &MO) const;
48 MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
51 MachineModuleInfoMachO &getMachOMMI() const;
52 Mangler *getMang() const {
53 return AsmPrinter.Mang;
57 } // end anonymous namespace
59 X86MCInstLower::X86MCInstLower(const MachineFunction &mf,
60 X86AsmPrinter &asmprinter)
61 : Ctx(mf.getContext()), MF(mf), TM(mf.getTarget()),
62 MAI(*TM.getMCAsmInfo()), AsmPrinter(asmprinter) {}
64 MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
65 return MF.getMMI().getObjFileInfo<MachineModuleInfoMachO>();
69 /// GetSymbolFromOperand - Lower an MO_GlobalAddress or MO_ExternalSymbol
70 /// operand to an MCSymbol.
71 MCSymbol *X86MCInstLower::
72 GetSymbolFromOperand(const MachineOperand &MO) const {
73 assert((MO.isGlobal() || MO.isSymbol() || MO.isMBB()) && "Isn't a symbol reference");
75 SmallString<128> Name;
78 const GlobalValue *GV = MO.getGlobal();
79 bool isImplicitlyPrivate = false;
80 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB ||
81 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
82 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
83 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
84 isImplicitlyPrivate = true;
86 getMang()->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
87 } else if (MO.isSymbol()) {
88 Name += MAI.getGlobalPrefix();
89 Name += MO.getSymbolName();
90 } else if (MO.isMBB()) {
91 Name += MO.getMBB()->getSymbol()->getName();
94 // If the target flags on the operand changes the name of the symbol, do that
95 // before we return the symbol.
96 switch (MO.getTargetFlags()) {
98 case X86II::MO_DLLIMPORT: {
99 // Handle dllimport linkage.
100 const char *Prefix = "__imp_";
101 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
104 case X86II::MO_DARWIN_NONLAZY:
105 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: {
106 Name += "$non_lazy_ptr";
107 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
109 MachineModuleInfoImpl::StubValueTy &StubSym =
110 getMachOMMI().getGVStubEntry(Sym);
111 if (StubSym.getPointer() == 0) {
112 assert(MO.isGlobal() && "Extern symbol not handled yet");
114 MachineModuleInfoImpl::
115 StubValueTy(AsmPrinter.getSymbol(MO.getGlobal()),
116 !MO.getGlobal()->hasInternalLinkage());
120 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: {
121 Name += "$non_lazy_ptr";
122 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
123 MachineModuleInfoImpl::StubValueTy &StubSym =
124 getMachOMMI().getHiddenGVStubEntry(Sym);
125 if (StubSym.getPointer() == 0) {
126 assert(MO.isGlobal() && "Extern symbol not handled yet");
128 MachineModuleInfoImpl::
129 StubValueTy(AsmPrinter.getSymbol(MO.getGlobal()),
130 !MO.getGlobal()->hasInternalLinkage());
134 case X86II::MO_DARWIN_STUB: {
136 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
137 MachineModuleInfoImpl::StubValueTy &StubSym =
138 getMachOMMI().getFnStubEntry(Sym);
139 if (StubSym.getPointer())
144 MachineModuleInfoImpl::
145 StubValueTy(AsmPrinter.getSymbol(MO.getGlobal()),
146 !MO.getGlobal()->hasInternalLinkage());
148 Name.erase(Name.end()-5, Name.end());
150 MachineModuleInfoImpl::
151 StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
157 return Ctx.GetOrCreateSymbol(Name.str());
160 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
161 MCSymbol *Sym) const {
162 // FIXME: We would like an efficient form for this, so we don't have to do a
163 // lot of extra uniquing.
164 const MCExpr *Expr = 0;
165 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
167 switch (MO.getTargetFlags()) {
168 default: llvm_unreachable("Unknown target flag on GV operand");
169 case X86II::MO_NO_FLAG: // No flag.
170 // These affect the name of the symbol, not any suffix.
171 case X86II::MO_DARWIN_NONLAZY:
172 case X86II::MO_DLLIMPORT:
173 case X86II::MO_DARWIN_STUB:
176 case X86II::MO_TLVP: RefKind = MCSymbolRefExpr::VK_TLVP; break;
177 case X86II::MO_TLVP_PIC_BASE:
178 Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_TLVP, Ctx);
179 // Subtract the pic base.
180 Expr = MCBinaryExpr::CreateSub(Expr,
181 MCSymbolRefExpr::Create(MF.getPICBaseSymbol(),
185 case X86II::MO_SECREL: RefKind = MCSymbolRefExpr::VK_SECREL; break;
186 case X86II::MO_TLSGD: RefKind = MCSymbolRefExpr::VK_TLSGD; break;
187 case X86II::MO_TLSLD: RefKind = MCSymbolRefExpr::VK_TLSLD; break;
188 case X86II::MO_TLSLDM: RefKind = MCSymbolRefExpr::VK_TLSLDM; break;
189 case X86II::MO_GOTTPOFF: RefKind = MCSymbolRefExpr::VK_GOTTPOFF; break;
190 case X86II::MO_INDNTPOFF: RefKind = MCSymbolRefExpr::VK_INDNTPOFF; break;
191 case X86II::MO_TPOFF: RefKind = MCSymbolRefExpr::VK_TPOFF; break;
192 case X86II::MO_DTPOFF: RefKind = MCSymbolRefExpr::VK_DTPOFF; break;
193 case X86II::MO_NTPOFF: RefKind = MCSymbolRefExpr::VK_NTPOFF; break;
194 case X86II::MO_GOTNTPOFF: RefKind = MCSymbolRefExpr::VK_GOTNTPOFF; break;
195 case X86II::MO_GOTPCREL: RefKind = MCSymbolRefExpr::VK_GOTPCREL; break;
196 case X86II::MO_GOT: RefKind = MCSymbolRefExpr::VK_GOT; break;
197 case X86II::MO_GOTOFF: RefKind = MCSymbolRefExpr::VK_GOTOFF; break;
198 case X86II::MO_PLT: RefKind = MCSymbolRefExpr::VK_PLT; break;
199 case X86II::MO_PIC_BASE_OFFSET:
200 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
201 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
202 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
203 // Subtract the pic base.
204 Expr = MCBinaryExpr::CreateSub(Expr,
205 MCSymbolRefExpr::Create(MF.getPICBaseSymbol(), Ctx),
207 if (MO.isJTI() && MAI.hasSetDirective()) {
208 // If .set directive is supported, use it to reduce the number of
209 // relocations the assembler will generate for differences between
210 // local labels. This is only safe when the symbols are in the same
211 // section so we are restricting it to jumptable references.
212 MCSymbol *Label = Ctx.CreateTempSymbol();
213 AsmPrinter.OutStreamer.EmitAssignment(Label, Expr);
214 Expr = MCSymbolRefExpr::Create(Label, Ctx);
220 Expr = MCSymbolRefExpr::Create(Sym, RefKind, Ctx);
222 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
223 Expr = MCBinaryExpr::CreateAdd(Expr,
224 MCConstantExpr::Create(MO.getOffset(), Ctx),
226 return MCOperand::CreateExpr(Expr);
230 /// LowerUnaryToTwoAddr - R = setb -> R = sbb R, R
231 static void LowerUnaryToTwoAddr(MCInst &OutMI, unsigned NewOpc) {
232 OutMI.setOpcode(NewOpc);
233 OutMI.addOperand(OutMI.getOperand(0));
234 OutMI.addOperand(OutMI.getOperand(0));
237 /// \brief Simplify FOO $imm, %{al,ax,eax,rax} to FOO $imm, for instruction with
238 /// a short fixed-register form.
239 static void SimplifyShortImmForm(MCInst &Inst, unsigned Opcode) {
240 unsigned ImmOp = Inst.getNumOperands() - 1;
241 assert(Inst.getOperand(0).isReg() &&
242 (Inst.getOperand(ImmOp).isImm() || Inst.getOperand(ImmOp).isExpr()) &&
243 ((Inst.getNumOperands() == 3 && Inst.getOperand(1).isReg() &&
244 Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) ||
245 Inst.getNumOperands() == 2) && "Unexpected instruction!");
247 // Check whether the destination register can be fixed.
248 unsigned Reg = Inst.getOperand(0).getReg();
249 if (Reg != X86::AL && Reg != X86::AX && Reg != X86::EAX && Reg != X86::RAX)
252 // If so, rewrite the instruction.
253 MCOperand Saved = Inst.getOperand(ImmOp);
255 Inst.setOpcode(Opcode);
256 Inst.addOperand(Saved);
259 /// \brief If a movsx instruction has a shorter encoding for the used register
260 /// simplify the instruction to use it instead.
261 static void SimplifyMOVSX(MCInst &Inst) {
262 unsigned NewOpcode = 0;
263 unsigned Op0 = Inst.getOperand(0).getReg(), Op1 = Inst.getOperand(1).getReg();
264 switch (Inst.getOpcode()) {
266 llvm_unreachable("Unexpected instruction!");
267 case X86::MOVSX16rr8: // movsbw %al, %ax --> cbtw
268 if (Op0 == X86::AX && Op1 == X86::AL)
269 NewOpcode = X86::CBW;
271 case X86::MOVSX32rr16: // movswl %ax, %eax --> cwtl
272 if (Op0 == X86::EAX && Op1 == X86::AX)
273 NewOpcode = X86::CWDE;
275 case X86::MOVSX64rr32: // movslq %eax, %rax --> cltq
276 if (Op0 == X86::RAX && Op1 == X86::EAX)
277 NewOpcode = X86::CDQE;
281 if (NewOpcode != 0) {
283 Inst.setOpcode(NewOpcode);
287 /// \brief Simplify things like MOV32rm to MOV32o32a.
288 static void SimplifyShortMoveForm(X86AsmPrinter &Printer, MCInst &Inst,
290 // Don't make these simplifications in 64-bit mode; other assemblers don't
291 // perform them because they make the code larger.
292 if (Printer.getSubtarget().is64Bit())
295 bool IsStore = Inst.getOperand(0).isReg() && Inst.getOperand(1).isReg();
296 unsigned AddrBase = IsStore;
297 unsigned RegOp = IsStore ? 0 : 5;
298 unsigned AddrOp = AddrBase + 3;
299 assert(Inst.getNumOperands() == 6 && Inst.getOperand(RegOp).isReg() &&
300 Inst.getOperand(AddrBase + 0).isReg() && // base
301 Inst.getOperand(AddrBase + 1).isImm() && // scale
302 Inst.getOperand(AddrBase + 2).isReg() && // index register
303 (Inst.getOperand(AddrOp).isExpr() || // address
304 Inst.getOperand(AddrOp).isImm())&&
305 Inst.getOperand(AddrBase + 4).isReg() && // segment
306 "Unexpected instruction!");
308 // Check whether the destination register can be fixed.
309 unsigned Reg = Inst.getOperand(RegOp).getReg();
310 if (Reg != X86::AL && Reg != X86::AX && Reg != X86::EAX && Reg != X86::RAX)
313 // Check whether this is an absolute address.
314 // FIXME: We know TLVP symbol refs aren't, but there should be a better way
316 bool Absolute = true;
317 if (Inst.getOperand(AddrOp).isExpr()) {
318 const MCExpr *MCE = Inst.getOperand(AddrOp).getExpr();
319 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(MCE))
320 if (SRE->getKind() == MCSymbolRefExpr::VK_TLVP)
325 (Inst.getOperand(AddrBase + 0).getReg() != 0 ||
326 Inst.getOperand(AddrBase + 2).getReg() != 0 ||
327 Inst.getOperand(AddrBase + 4).getReg() != 0 ||
328 Inst.getOperand(AddrBase + 1).getImm() != 1))
331 // If so, rewrite the instruction.
332 MCOperand Saved = Inst.getOperand(AddrOp);
334 Inst.setOpcode(Opcode);
335 Inst.addOperand(Saved);
338 void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
339 OutMI.setOpcode(MI->getOpcode());
341 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
342 const MachineOperand &MO = MI->getOperand(i);
345 switch (MO.getType()) {
348 llvm_unreachable("unknown operand type");
349 case MachineOperand::MO_Register:
350 // Ignore all implicit register operands.
351 if (MO.isImplicit()) continue;
352 MCOp = MCOperand::CreateReg(MO.getReg());
354 case MachineOperand::MO_Immediate:
355 MCOp = MCOperand::CreateImm(MO.getImm());
357 case MachineOperand::MO_MachineBasicBlock:
358 case MachineOperand::MO_GlobalAddress:
359 case MachineOperand::MO_ExternalSymbol:
360 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
362 case MachineOperand::MO_JumpTableIndex:
363 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()));
365 case MachineOperand::MO_ConstantPoolIndex:
366 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex()));
368 case MachineOperand::MO_BlockAddress:
369 MCOp = LowerSymbolOperand(MO,
370 AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress()));
372 case MachineOperand::MO_RegisterMask:
373 // Ignore call clobbers.
377 OutMI.addOperand(MCOp);
380 // Handle a few special cases to eliminate operand modifiers.
382 switch (OutMI.getOpcode()) {
387 // LEA should have a segment register, but it must be empty.
388 assert(OutMI.getNumOperands() == 1+X86::AddrNumOperands &&
389 "Unexpected # of LEA operands");
390 assert(OutMI.getOperand(1+X86::AddrSegmentReg).getReg() == 0 &&
391 "LEA has segment specified!");
393 case X86::MOV32r0: LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); break;
396 OutMI.setOpcode(X86::MOV32ri);
399 // Commute operands to get a smaller encoding by using VEX.R instead of VEX.B
400 // if one of the registers is extended, but other isn't.
402 case X86::VMOVAPDYrr:
404 case X86::VMOVAPSYrr:
406 case X86::VMOVDQAYrr:
408 case X86::VMOVDQUYrr:
410 case X86::VMOVUPDYrr:
412 case X86::VMOVUPSYrr: {
413 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(0).getReg()) &&
414 X86II::isX86_64ExtendedReg(OutMI.getOperand(1).getReg())) {
416 switch (OutMI.getOpcode()) {
417 default: llvm_unreachable("Invalid opcode");
418 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
419 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
420 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
421 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
422 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
423 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
424 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
425 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
426 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
427 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
428 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
429 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
431 OutMI.setOpcode(NewOpc);
436 case X86::VMOVSSrr: {
437 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(0).getReg()) &&
438 X86II::isX86_64ExtendedReg(OutMI.getOperand(2).getReg())) {
440 switch (OutMI.getOpcode()) {
441 default: llvm_unreachable("Invalid opcode");
442 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
443 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
445 OutMI.setOpcode(NewOpc);
450 // TAILJMPr64, CALL64r, CALL64pcrel32 - These instructions have register
451 // inputs modeled as normal uses instead of implicit uses. As such, truncate
452 // off all but the first operand (the callee). FIXME: Change isel.
453 case X86::TAILJMPr64:
455 case X86::CALL64pcrel32: {
456 unsigned Opcode = OutMI.getOpcode();
457 MCOperand Saved = OutMI.getOperand(0);
459 OutMI.setOpcode(Opcode);
460 OutMI.addOperand(Saved);
465 case X86::EH_RETURN64: {
467 OutMI.setOpcode(X86::RET);
471 // TAILJMPd, TAILJMPd64 - Lower to the correct jump instructions.
474 case X86::TAILJMPd64: {
476 switch (OutMI.getOpcode()) {
477 default: llvm_unreachable("Invalid opcode");
478 case X86::TAILJMPr: Opcode = X86::JMP32r; break;
480 case X86::TAILJMPd64: Opcode = X86::JMP_1; break;
483 MCOperand Saved = OutMI.getOperand(0);
485 OutMI.setOpcode(Opcode);
486 OutMI.addOperand(Saved);
490 // These are pseudo-ops for OR to help with the OR->ADD transformation. We do
491 // this with an ugly goto in case the resultant OR uses EAX and needs the
493 case X86::ADD16rr_DB: OutMI.setOpcode(X86::OR16rr); goto ReSimplify;
494 case X86::ADD32rr_DB: OutMI.setOpcode(X86::OR32rr); goto ReSimplify;
495 case X86::ADD64rr_DB: OutMI.setOpcode(X86::OR64rr); goto ReSimplify;
496 case X86::ADD16ri_DB: OutMI.setOpcode(X86::OR16ri); goto ReSimplify;
497 case X86::ADD32ri_DB: OutMI.setOpcode(X86::OR32ri); goto ReSimplify;
498 case X86::ADD64ri32_DB: OutMI.setOpcode(X86::OR64ri32); goto ReSimplify;
499 case X86::ADD16ri8_DB: OutMI.setOpcode(X86::OR16ri8); goto ReSimplify;
500 case X86::ADD32ri8_DB: OutMI.setOpcode(X86::OR32ri8); goto ReSimplify;
501 case X86::ADD64ri8_DB: OutMI.setOpcode(X86::OR64ri8); goto ReSimplify;
503 // The assembler backend wants to see branches in their small form and relax
504 // them to their large form. The JIT can only handle the large form because
505 // it does not do relaxation. For now, translate the large form to the
507 case X86::JMP_4: OutMI.setOpcode(X86::JMP_1); break;
508 case X86::JO_4: OutMI.setOpcode(X86::JO_1); break;
509 case X86::JNO_4: OutMI.setOpcode(X86::JNO_1); break;
510 case X86::JB_4: OutMI.setOpcode(X86::JB_1); break;
511 case X86::JAE_4: OutMI.setOpcode(X86::JAE_1); break;
512 case X86::JE_4: OutMI.setOpcode(X86::JE_1); break;
513 case X86::JNE_4: OutMI.setOpcode(X86::JNE_1); break;
514 case X86::JBE_4: OutMI.setOpcode(X86::JBE_1); break;
515 case X86::JA_4: OutMI.setOpcode(X86::JA_1); break;
516 case X86::JS_4: OutMI.setOpcode(X86::JS_1); break;
517 case X86::JNS_4: OutMI.setOpcode(X86::JNS_1); break;
518 case X86::JP_4: OutMI.setOpcode(X86::JP_1); break;
519 case X86::JNP_4: OutMI.setOpcode(X86::JNP_1); break;
520 case X86::JL_4: OutMI.setOpcode(X86::JL_1); break;
521 case X86::JGE_4: OutMI.setOpcode(X86::JGE_1); break;
522 case X86::JLE_4: OutMI.setOpcode(X86::JLE_1); break;
523 case X86::JG_4: OutMI.setOpcode(X86::JG_1); break;
525 // Atomic load and store require a separate pseudo-inst because Acquire
526 // implies mayStore and Release implies mayLoad; fix these to regular MOV
528 case X86::ACQUIRE_MOV8rm: OutMI.setOpcode(X86::MOV8rm); goto ReSimplify;
529 case X86::ACQUIRE_MOV16rm: OutMI.setOpcode(X86::MOV16rm); goto ReSimplify;
530 case X86::ACQUIRE_MOV32rm: OutMI.setOpcode(X86::MOV32rm); goto ReSimplify;
531 case X86::ACQUIRE_MOV64rm: OutMI.setOpcode(X86::MOV64rm); goto ReSimplify;
532 case X86::RELEASE_MOV8mr: OutMI.setOpcode(X86::MOV8mr); goto ReSimplify;
533 case X86::RELEASE_MOV16mr: OutMI.setOpcode(X86::MOV16mr); goto ReSimplify;
534 case X86::RELEASE_MOV32mr: OutMI.setOpcode(X86::MOV32mr); goto ReSimplify;
535 case X86::RELEASE_MOV64mr: OutMI.setOpcode(X86::MOV64mr); goto ReSimplify;
537 // We don't currently select the correct instruction form for instructions
538 // which have a short %eax, etc. form. Handle this by custom lowering, for
541 // Note, we are currently not handling the following instructions:
542 // MOV64ao8, MOV64o8a
543 // XCHG16ar, XCHG32ar, XCHG64ar
544 case X86::MOV8mr_NOREX:
545 case X86::MOV8mr: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV8ao8); break;
546 case X86::MOV8rm_NOREX:
547 case X86::MOV8rm: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV8o8a); break;
548 case X86::MOV16mr: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV16ao16); break;
549 case X86::MOV16rm: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV16o16a); break;
550 case X86::MOV32mr: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV32ao32); break;
551 case X86::MOV32rm: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV32o32a); break;
553 case X86::ADC8ri: SimplifyShortImmForm(OutMI, X86::ADC8i8); break;
554 case X86::ADC16ri: SimplifyShortImmForm(OutMI, X86::ADC16i16); break;
555 case X86::ADC32ri: SimplifyShortImmForm(OutMI, X86::ADC32i32); break;
556 case X86::ADC64ri32: SimplifyShortImmForm(OutMI, X86::ADC64i32); break;
557 case X86::ADD8ri: SimplifyShortImmForm(OutMI, X86::ADD8i8); break;
558 case X86::ADD16ri: SimplifyShortImmForm(OutMI, X86::ADD16i16); break;
559 case X86::ADD32ri: SimplifyShortImmForm(OutMI, X86::ADD32i32); break;
560 case X86::ADD64ri32: SimplifyShortImmForm(OutMI, X86::ADD64i32); break;
561 case X86::AND8ri: SimplifyShortImmForm(OutMI, X86::AND8i8); break;
562 case X86::AND16ri: SimplifyShortImmForm(OutMI, X86::AND16i16); break;
563 case X86::AND32ri: SimplifyShortImmForm(OutMI, X86::AND32i32); break;
564 case X86::AND64ri32: SimplifyShortImmForm(OutMI, X86::AND64i32); break;
565 case X86::CMP8ri: SimplifyShortImmForm(OutMI, X86::CMP8i8); break;
566 case X86::CMP16ri: SimplifyShortImmForm(OutMI, X86::CMP16i16); break;
567 case X86::CMP32ri: SimplifyShortImmForm(OutMI, X86::CMP32i32); break;
568 case X86::CMP64ri32: SimplifyShortImmForm(OutMI, X86::CMP64i32); break;
569 case X86::OR8ri: SimplifyShortImmForm(OutMI, X86::OR8i8); break;
570 case X86::OR16ri: SimplifyShortImmForm(OutMI, X86::OR16i16); break;
571 case X86::OR32ri: SimplifyShortImmForm(OutMI, X86::OR32i32); break;
572 case X86::OR64ri32: SimplifyShortImmForm(OutMI, X86::OR64i32); break;
573 case X86::SBB8ri: SimplifyShortImmForm(OutMI, X86::SBB8i8); break;
574 case X86::SBB16ri: SimplifyShortImmForm(OutMI, X86::SBB16i16); break;
575 case X86::SBB32ri: SimplifyShortImmForm(OutMI, X86::SBB32i32); break;
576 case X86::SBB64ri32: SimplifyShortImmForm(OutMI, X86::SBB64i32); break;
577 case X86::SUB8ri: SimplifyShortImmForm(OutMI, X86::SUB8i8); break;
578 case X86::SUB16ri: SimplifyShortImmForm(OutMI, X86::SUB16i16); break;
579 case X86::SUB32ri: SimplifyShortImmForm(OutMI, X86::SUB32i32); break;
580 case X86::SUB64ri32: SimplifyShortImmForm(OutMI, X86::SUB64i32); break;
581 case X86::TEST8ri: SimplifyShortImmForm(OutMI, X86::TEST8i8); break;
582 case X86::TEST16ri: SimplifyShortImmForm(OutMI, X86::TEST16i16); break;
583 case X86::TEST32ri: SimplifyShortImmForm(OutMI, X86::TEST32i32); break;
584 case X86::TEST64ri32: SimplifyShortImmForm(OutMI, X86::TEST64i32); break;
585 case X86::XOR8ri: SimplifyShortImmForm(OutMI, X86::XOR8i8); break;
586 case X86::XOR16ri: SimplifyShortImmForm(OutMI, X86::XOR16i16); break;
587 case X86::XOR32ri: SimplifyShortImmForm(OutMI, X86::XOR32i32); break;
588 case X86::XOR64ri32: SimplifyShortImmForm(OutMI, X86::XOR64i32); break;
590 // Try to shrink some forms of movsx.
591 case X86::MOVSX16rr8:
592 case X86::MOVSX32rr16:
593 case X86::MOVSX64rr32:
594 SimplifyMOVSX(OutMI);
597 case X86::MORESTACK_RET:
598 OutMI.setOpcode(X86::RET);
601 case X86::MORESTACK_RET_RESTORE_R10:
602 OutMI.setOpcode(X86::MOV64rr);
603 OutMI.addOperand(MCOperand::CreateReg(X86::R10));
604 OutMI.addOperand(MCOperand::CreateReg(X86::RAX));
606 AsmPrinter.OutStreamer.EmitInstruction(MCInstBuilder(X86::RET));
611 static void LowerTlsAddr(MCStreamer &OutStreamer,
612 X86MCInstLower &MCInstLowering,
613 const MachineInstr &MI) {
615 bool is64Bits = MI.getOpcode() == X86::TLS_addr64 ||
616 MI.getOpcode() == X86::TLS_base_addr64;
618 bool needsPadding = MI.getOpcode() == X86::TLS_addr64;
620 MCContext &context = OutStreamer.getContext();
623 OutStreamer.EmitInstruction(MCInstBuilder(X86::DATA16_PREFIX));
625 MCSymbolRefExpr::VariantKind SRVK;
626 switch (MI.getOpcode()) {
627 case X86::TLS_addr32:
628 case X86::TLS_addr64:
629 SRVK = MCSymbolRefExpr::VK_TLSGD;
631 case X86::TLS_base_addr32:
632 SRVK = MCSymbolRefExpr::VK_TLSLDM;
634 case X86::TLS_base_addr64:
635 SRVK = MCSymbolRefExpr::VK_TLSLD;
638 llvm_unreachable("unexpected opcode");
641 MCSymbol *sym = MCInstLowering.GetSymbolFromOperand(MI.getOperand(3));
642 const MCSymbolRefExpr *symRef = MCSymbolRefExpr::Create(sym, SRVK, context);
646 LEA.setOpcode(X86::LEA64r);
647 LEA.addOperand(MCOperand::CreateReg(X86::RDI)); // dest
648 LEA.addOperand(MCOperand::CreateReg(X86::RIP)); // base
649 LEA.addOperand(MCOperand::CreateImm(1)); // scale
650 LEA.addOperand(MCOperand::CreateReg(0)); // index
651 LEA.addOperand(MCOperand::CreateExpr(symRef)); // disp
652 LEA.addOperand(MCOperand::CreateReg(0)); // seg
653 } else if (SRVK == MCSymbolRefExpr::VK_TLSLDM) {
654 LEA.setOpcode(X86::LEA32r);
655 LEA.addOperand(MCOperand::CreateReg(X86::EAX)); // dest
656 LEA.addOperand(MCOperand::CreateReg(X86::EBX)); // base
657 LEA.addOperand(MCOperand::CreateImm(1)); // scale
658 LEA.addOperand(MCOperand::CreateReg(0)); // index
659 LEA.addOperand(MCOperand::CreateExpr(symRef)); // disp
660 LEA.addOperand(MCOperand::CreateReg(0)); // seg
662 LEA.setOpcode(X86::LEA32r);
663 LEA.addOperand(MCOperand::CreateReg(X86::EAX)); // dest
664 LEA.addOperand(MCOperand::CreateReg(0)); // base
665 LEA.addOperand(MCOperand::CreateImm(1)); // scale
666 LEA.addOperand(MCOperand::CreateReg(X86::EBX)); // index
667 LEA.addOperand(MCOperand::CreateExpr(symRef)); // disp
668 LEA.addOperand(MCOperand::CreateReg(0)); // seg
670 OutStreamer.EmitInstruction(LEA);
673 OutStreamer.EmitInstruction(MCInstBuilder(X86::DATA16_PREFIX));
674 OutStreamer.EmitInstruction(MCInstBuilder(X86::DATA16_PREFIX));
675 OutStreamer.EmitInstruction(MCInstBuilder(X86::REX64_PREFIX));
678 StringRef name = is64Bits ? "__tls_get_addr" : "___tls_get_addr";
679 MCSymbol *tlsGetAddr = context.GetOrCreateSymbol(name);
680 const MCSymbolRefExpr *tlsRef =
681 MCSymbolRefExpr::Create(tlsGetAddr,
682 MCSymbolRefExpr::VK_PLT,
685 OutStreamer.EmitInstruction(MCInstBuilder(is64Bits ? X86::CALL64pcrel32
690 static std::pair<StackMaps::Location, MachineInstr::const_mop_iterator>
691 parseMemoryOperand(StackMaps::Location::LocationType LocTy,
692 MachineInstr::const_mop_iterator MOI,
693 MachineInstr::const_mop_iterator MOE) {
695 typedef StackMaps::Location Location;
697 assert(std::distance(MOI, MOE) >= 5 && "Too few operands to encode mem op.");
699 const MachineOperand &Base = *MOI;
700 const MachineOperand &Scale = *(++MOI);
701 const MachineOperand &Index = *(++MOI);
702 const MachineOperand &Disp = *(++MOI);
703 const MachineOperand &ZeroReg = *(++MOI);
705 // Sanity check for supported operand format.
706 assert(Base.isReg() &&
707 Scale.isImm() && Scale.getImm() == 1 &&
708 Index.isReg() && Index.getReg() == 0 &&
709 Disp.isImm() && ZeroReg.isReg() && (ZeroReg.getReg() == 0) &&
710 "Unsupported x86 memory operand sequence.");
715 return std::make_pair(
716 Location(LocTy, Base.getReg(), Disp.getImm()), ++MOI);
719 std::pair<StackMaps::Location, MachineInstr::const_mop_iterator>
720 X86AsmPrinter::stackmapOperandParser(MachineInstr::const_mop_iterator MOI,
721 MachineInstr::const_mop_iterator MOE) {
723 typedef StackMaps::Location Location;
725 const MachineOperand &MOP = *MOI;
726 assert(!MOP.isRegMask() && (!MOP.isReg() || !MOP.isImplicit()) &&
727 "Register mask and implicit operands should not be processed.");
730 switch (MOP.getImm()) {
731 default: llvm_unreachable("Unrecognized operand type.");
732 case StackMaps::DirectMemRefOp:
733 return parseMemoryOperand(StackMaps::Location::Direct,
734 llvm::next(MOI), MOE);
735 case StackMaps::IndirectMemRefOp:
736 return parseMemoryOperand(StackMaps::Location::Indirect,
737 llvm::next(MOI), MOE);
738 case StackMaps::ConstantOp: {
740 assert(MOI->isImm() && "Expected constant operand.");
741 int64_t Imm = MOI->getImm();
742 return std::make_pair(Location(Location::Constant, 0, Imm), ++MOI);
747 // Otherwise this is a reg operand.
748 assert(MOP.isReg() && "Expected register operand here.");
749 assert(TargetRegisterInfo::isPhysicalRegister(MOP.getReg()) &&
750 "Virtreg operands should have been rewritten before now.");
751 return std::make_pair(Location(Location::Register, MOP.getReg(), 0), ++MOI);
754 static MachineInstr::const_mop_iterator
755 getStackMapEndMOP(MachineInstr::const_mop_iterator MOI,
756 MachineInstr::const_mop_iterator MOE) {
757 for (; MOI != MOE; ++MOI)
758 if (MOI->isRegMask() || (MOI->isReg() && MOI->isImplicit()))
764 static void LowerSTACKMAP(MCStreamer &OutStreamer,
765 X86MCInstLower &MCInstLowering,
767 const MachineInstr &MI)
769 int64_t ID = MI.getOperand(0).getImm();
770 unsigned NumNOPBytes = MI.getOperand(1).getImm();
772 assert((int32_t)ID == ID && "Stack maps hold 32-bit IDs");
773 SM.recordStackMap(MI, ID, llvm::next(MI.operands_begin(), 2),
774 getStackMapEndMOP(MI.operands_begin(), MI.operands_end()));
776 for (unsigned i = 0; i < NumNOPBytes; ++i)
777 OutStreamer.EmitInstruction(MCInstBuilder(X86::NOOP));
780 static void LowerPATCHPOINT(MCStreamer &OutStreamer,
781 X86MCInstLower &MCInstLowering,
783 const MachineInstr &MI) {
784 bool hasDef = MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
785 !MI.getOperand(0).isImplicit();
786 unsigned StartIdx = hasDef ? 1 : 0;
788 unsigned StartIdx2 = 0, e = MI.getNumOperands();
789 while (StartIdx2 < e && MI.getOperand(StartIdx2).isReg() &&
790 MI.getOperand(StartIdx2).isDef() &&
791 !MI.getOperand(StartIdx2).isImplicit())
794 assert(StartIdx == StartIdx2 &&
795 "Unexpected additonal definition in Patchpoint intrinsic.");
798 int64_t ID = MI.getOperand(StartIdx).getImm();
799 assert((int32_t)ID == ID && "Stack maps hold 32-bit IDs");
801 // Get the number of arguments participating in the call. This number was
802 // adjusted during call lowering by subtracting stack args.
803 bool isAnyRegCC = MI.getOperand(StartIdx + 4).getImm() == CallingConv::AnyReg;
804 assert(((hasDef && isAnyRegCC) || !hasDef) &&
805 "Only Patchpoints with AnyReg calling convention may have a result");
806 int64_t StackMapIdx = isAnyRegCC ? StartIdx + 5 :
807 StartIdx + 5 + MI.getOperand(StartIdx + 3).getImm();
808 assert(StackMapIdx <= MI.getNumOperands() &&
809 "Patchpoint intrinsic dropped arguments.");
811 SM.recordStackMap(MI, ID, llvm::next(MI.operands_begin(), StackMapIdx),
812 getStackMapEndMOP(MI.operands_begin(), MI.operands_end()),
813 isAnyRegCC && hasDef);
815 // Emit call. We need to know how many bytes we encoded here.
816 unsigned EncodedBytes = 2;
817 OutStreamer.EmitInstruction(MCInstBuilder(X86::CALL64r)
818 .addReg(MI.getOperand(StartIdx + 2).getReg()));
821 unsigned NumNOPBytes = MI.getOperand(StartIdx + 1).getImm();
822 assert(NumNOPBytes >= EncodedBytes &&
823 "Patchpoint can't request size less than the length of a call.");
825 for (unsigned i = EncodedBytes; i < NumNOPBytes; ++i)
826 OutStreamer.EmitInstruction(MCInstBuilder(X86::NOOP));
829 void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
830 X86MCInstLower MCInstLowering(*MF, *this);
831 switch (MI->getOpcode()) {
832 case TargetOpcode::DBG_VALUE:
833 llvm_unreachable("Should be handled target independently");
835 // Emit nothing here but a comment if we can.
836 case X86::Int_MemBarrier:
837 if (OutStreamer.hasRawTextSupport())
838 OutStreamer.EmitRawText(StringRef("\t#MEMBARRIER"));
843 case X86::EH_RETURN64: {
844 // Lower these as normal, but add some comments.
845 unsigned Reg = MI->getOperand(0).getReg();
846 OutStreamer.AddComment(StringRef("eh_return, addr: %") +
847 X86ATTInstPrinter::getRegisterName(Reg));
852 case X86::TAILJMPd64:
853 // Lower these as normal, but add some comments.
854 OutStreamer.AddComment("TAILCALL");
857 case X86::TLS_addr32:
858 case X86::TLS_addr64:
859 case X86::TLS_base_addr32:
860 case X86::TLS_base_addr64:
861 return LowerTlsAddr(OutStreamer, MCInstLowering, *MI);
863 case X86::MOVPC32r: {
864 // This is a pseudo op for a two instruction sequence with a label, which
871 MCSymbol *PICBase = MF->getPICBaseSymbol();
872 // FIXME: We would like an efficient form for this, so we don't have to do a
873 // lot of extra uniquing.
874 OutStreamer.EmitInstruction(MCInstBuilder(X86::CALLpcrel32)
875 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
878 OutStreamer.EmitLabel(PICBase);
881 OutStreamer.EmitInstruction(MCInstBuilder(X86::POP32r)
882 .addReg(MI->getOperand(0).getReg()));
887 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
888 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
891 // Okay, we have something like:
892 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
894 // For this, we want to print something like:
895 // MYGLOBAL + (. - PICBASE)
896 // However, we can't generate a ".", so just emit a new label here and refer
898 MCSymbol *DotSym = OutContext.CreateTempSymbol();
899 OutStreamer.EmitLabel(DotSym);
901 // Now that we have emitted the label, lower the complex operand expression.
902 MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2));
904 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
905 const MCExpr *PICBase =
906 MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), OutContext);
907 DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, OutContext);
909 DotExpr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym,OutContext),
910 DotExpr, OutContext);
912 OutStreamer.EmitInstruction(MCInstBuilder(X86::ADD32ri)
913 .addReg(MI->getOperand(0).getReg())
914 .addReg(MI->getOperand(1).getReg())
919 case TargetOpcode::STACKMAP:
920 return LowerSTACKMAP(OutStreamer, MCInstLowering, SM, *MI);
922 case TargetOpcode::PATCHPOINT:
923 return LowerPATCHPOINT(OutStreamer, MCInstLowering, SM, *MI);
927 MCInstLowering.Lower(MI, TmpInst);
928 OutStreamer.EmitInstruction(TmpInst);