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/IR/Type.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstBuilder.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Target/Mangler.h"
34 /// X86MCInstLower - This class is used to lower an MachineInstr into an MCInst.
35 class X86MCInstLower {
38 const MachineFunction &MF;
39 const TargetMachine &TM;
41 X86AsmPrinter &AsmPrinter;
43 X86MCInstLower(Mangler *mang, const MachineFunction &MF,
44 X86AsmPrinter &asmprinter);
46 void Lower(const MachineInstr *MI, MCInst &OutMI) const;
48 MCSymbol *GetSymbolFromOperand(const MachineOperand &MO) const;
49 MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
52 MachineModuleInfoMachO &getMachOMMI() const;
55 } // end anonymous namespace
57 X86MCInstLower::X86MCInstLower(Mangler *mang, const MachineFunction &mf,
58 X86AsmPrinter &asmprinter)
59 : Ctx(mf.getContext()), Mang(mang), MF(mf), TM(mf.getTarget()),
60 MAI(*TM.getMCAsmInfo()), AsmPrinter(asmprinter) {}
62 MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
63 return MF.getMMI().getObjFileInfo<MachineModuleInfoMachO>();
67 /// GetSymbolFromOperand - Lower an MO_GlobalAddress or MO_ExternalSymbol
68 /// operand to an MCSymbol.
69 MCSymbol *X86MCInstLower::
70 GetSymbolFromOperand(const MachineOperand &MO) const {
71 assert((MO.isGlobal() || MO.isSymbol() || MO.isMBB()) && "Isn't a symbol reference");
73 SmallString<128> Name;
76 const GlobalValue *GV = MO.getGlobal();
77 bool isImplicitlyPrivate = false;
78 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB ||
79 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
80 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
81 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
82 isImplicitlyPrivate = true;
84 Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
85 } else if (MO.isSymbol()) {
86 Name += MAI.getGlobalPrefix();
87 Name += MO.getSymbolName();
88 } else if (MO.isMBB()) {
89 Name += MO.getMBB()->getSymbol()->getName();
92 // If the target flags on the operand changes the name of the symbol, do that
93 // before we return the symbol.
94 switch (MO.getTargetFlags()) {
96 case X86II::MO_DLLIMPORT: {
97 // Handle dllimport linkage.
98 const char *Prefix = "__imp_";
99 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
102 case X86II::MO_DARWIN_NONLAZY:
103 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: {
104 Name += "$non_lazy_ptr";
105 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
107 MachineModuleInfoImpl::StubValueTy &StubSym =
108 getMachOMMI().getGVStubEntry(Sym);
109 if (StubSym.getPointer() == 0) {
110 assert(MO.isGlobal() && "Extern symbol not handled yet");
112 MachineModuleInfoImpl::
113 StubValueTy(Mang->getSymbol(MO.getGlobal()),
114 !MO.getGlobal()->hasInternalLinkage());
118 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: {
119 Name += "$non_lazy_ptr";
120 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
121 MachineModuleInfoImpl::StubValueTy &StubSym =
122 getMachOMMI().getHiddenGVStubEntry(Sym);
123 if (StubSym.getPointer() == 0) {
124 assert(MO.isGlobal() && "Extern symbol not handled yet");
126 MachineModuleInfoImpl::
127 StubValueTy(Mang->getSymbol(MO.getGlobal()),
128 !MO.getGlobal()->hasInternalLinkage());
132 case X86II::MO_DARWIN_STUB: {
134 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
135 MachineModuleInfoImpl::StubValueTy &StubSym =
136 getMachOMMI().getFnStubEntry(Sym);
137 if (StubSym.getPointer())
142 MachineModuleInfoImpl::
143 StubValueTy(Mang->getSymbol(MO.getGlobal()),
144 !MO.getGlobal()->hasInternalLinkage());
146 Name.erase(Name.end()-5, Name.end());
148 MachineModuleInfoImpl::
149 StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
155 return Ctx.GetOrCreateSymbol(Name.str());
158 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
159 MCSymbol *Sym) const {
160 // FIXME: We would like an efficient form for this, so we don't have to do a
161 // lot of extra uniquing.
162 const MCExpr *Expr = 0;
163 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
165 switch (MO.getTargetFlags()) {
166 default: llvm_unreachable("Unknown target flag on GV operand");
167 case X86II::MO_NO_FLAG: // No flag.
168 // These affect the name of the symbol, not any suffix.
169 case X86II::MO_DARWIN_NONLAZY:
170 case X86II::MO_DLLIMPORT:
171 case X86II::MO_DARWIN_STUB:
174 case X86II::MO_TLVP: RefKind = MCSymbolRefExpr::VK_TLVP; break;
175 case X86II::MO_TLVP_PIC_BASE:
176 Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_TLVP, Ctx);
177 // Subtract the pic base.
178 Expr = MCBinaryExpr::CreateSub(Expr,
179 MCSymbolRefExpr::Create(MF.getPICBaseSymbol(),
183 case X86II::MO_SECREL: RefKind = MCSymbolRefExpr::VK_SECREL; break;
184 case X86II::MO_TLSGD: RefKind = MCSymbolRefExpr::VK_TLSGD; break;
185 case X86II::MO_TLSLD: RefKind = MCSymbolRefExpr::VK_TLSLD; break;
186 case X86II::MO_TLSLDM: RefKind = MCSymbolRefExpr::VK_TLSLDM; break;
187 case X86II::MO_GOTTPOFF: RefKind = MCSymbolRefExpr::VK_GOTTPOFF; break;
188 case X86II::MO_INDNTPOFF: RefKind = MCSymbolRefExpr::VK_INDNTPOFF; break;
189 case X86II::MO_TPOFF: RefKind = MCSymbolRefExpr::VK_TPOFF; break;
190 case X86II::MO_DTPOFF: RefKind = MCSymbolRefExpr::VK_DTPOFF; break;
191 case X86II::MO_NTPOFF: RefKind = MCSymbolRefExpr::VK_NTPOFF; break;
192 case X86II::MO_GOTNTPOFF: RefKind = MCSymbolRefExpr::VK_GOTNTPOFF; break;
193 case X86II::MO_GOTPCREL: RefKind = MCSymbolRefExpr::VK_GOTPCREL; break;
194 case X86II::MO_GOT: RefKind = MCSymbolRefExpr::VK_GOT; break;
195 case X86II::MO_GOTOFF: RefKind = MCSymbolRefExpr::VK_GOTOFF; break;
196 case X86II::MO_PLT: RefKind = MCSymbolRefExpr::VK_PLT; break;
197 case X86II::MO_PIC_BASE_OFFSET:
198 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
199 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
200 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
201 // Subtract the pic base.
202 Expr = MCBinaryExpr::CreateSub(Expr,
203 MCSymbolRefExpr::Create(MF.getPICBaseSymbol(), Ctx),
205 if (MO.isJTI() && MAI.hasSetDirective()) {
206 // If .set directive is supported, use it to reduce the number of
207 // relocations the assembler will generate for differences between
208 // local labels. This is only safe when the symbols are in the same
209 // section so we are restricting it to jumptable references.
210 MCSymbol *Label = Ctx.CreateTempSymbol();
211 AsmPrinter.OutStreamer.EmitAssignment(Label, Expr);
212 Expr = MCSymbolRefExpr::Create(Label, Ctx);
218 Expr = MCSymbolRefExpr::Create(Sym, RefKind, Ctx);
220 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
221 Expr = MCBinaryExpr::CreateAdd(Expr,
222 MCConstantExpr::Create(MO.getOffset(), Ctx),
224 return MCOperand::CreateExpr(Expr);
229 static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
230 // Convert registers in the addr mode according to subreg64.
231 for (unsigned i = 0; i != 4; ++i) {
232 if (!MI->getOperand(OpNo+i).isReg()) continue;
234 unsigned Reg = MI->getOperand(OpNo+i).getReg();
235 // LEAs can use RIP-relative addressing, and RIP has no sub/super register.
236 if (Reg == 0 || Reg == X86::RIP) continue;
238 MI->getOperand(OpNo+i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
242 /// LowerUnaryToTwoAddr - R = setb -> R = sbb R, R
243 static void LowerUnaryToTwoAddr(MCInst &OutMI, unsigned NewOpc) {
244 OutMI.setOpcode(NewOpc);
245 OutMI.addOperand(OutMI.getOperand(0));
246 OutMI.addOperand(OutMI.getOperand(0));
249 /// \brief Simplify FOO $imm, %{al,ax,eax,rax} to FOO $imm, for instruction with
250 /// a short fixed-register form.
251 static void SimplifyShortImmForm(MCInst &Inst, unsigned Opcode) {
252 unsigned ImmOp = Inst.getNumOperands() - 1;
253 assert(Inst.getOperand(0).isReg() &&
254 (Inst.getOperand(ImmOp).isImm() || Inst.getOperand(ImmOp).isExpr()) &&
255 ((Inst.getNumOperands() == 3 && Inst.getOperand(1).isReg() &&
256 Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) ||
257 Inst.getNumOperands() == 2) && "Unexpected instruction!");
259 // Check whether the destination register can be fixed.
260 unsigned Reg = Inst.getOperand(0).getReg();
261 if (Reg != X86::AL && Reg != X86::AX && Reg != X86::EAX && Reg != X86::RAX)
264 // If so, rewrite the instruction.
265 MCOperand Saved = Inst.getOperand(ImmOp);
267 Inst.setOpcode(Opcode);
268 Inst.addOperand(Saved);
271 /// \brief Simplify things like MOV32rm to MOV32o32a.
272 static void SimplifyShortMoveForm(X86AsmPrinter &Printer, MCInst &Inst,
274 // Don't make these simplifications in 64-bit mode; other assemblers don't
275 // perform them because they make the code larger.
276 if (Printer.getSubtarget().is64Bit())
279 bool IsStore = Inst.getOperand(0).isReg() && Inst.getOperand(1).isReg();
280 unsigned AddrBase = IsStore;
281 unsigned RegOp = IsStore ? 0 : 5;
282 unsigned AddrOp = AddrBase + 3;
283 assert(Inst.getNumOperands() == 6 && Inst.getOperand(RegOp).isReg() &&
284 Inst.getOperand(AddrBase + 0).isReg() && // base
285 Inst.getOperand(AddrBase + 1).isImm() && // scale
286 Inst.getOperand(AddrBase + 2).isReg() && // index register
287 (Inst.getOperand(AddrOp).isExpr() || // address
288 Inst.getOperand(AddrOp).isImm())&&
289 Inst.getOperand(AddrBase + 4).isReg() && // segment
290 "Unexpected instruction!");
292 // Check whether the destination register can be fixed.
293 unsigned Reg = Inst.getOperand(RegOp).getReg();
294 if (Reg != X86::AL && Reg != X86::AX && Reg != X86::EAX && Reg != X86::RAX)
297 // Check whether this is an absolute address.
298 // FIXME: We know TLVP symbol refs aren't, but there should be a better way
300 bool Absolute = true;
301 if (Inst.getOperand(AddrOp).isExpr()) {
302 const MCExpr *MCE = Inst.getOperand(AddrOp).getExpr();
303 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(MCE))
304 if (SRE->getKind() == MCSymbolRefExpr::VK_TLVP)
309 (Inst.getOperand(AddrBase + 0).getReg() != 0 ||
310 Inst.getOperand(AddrBase + 2).getReg() != 0 ||
311 Inst.getOperand(AddrBase + 4).getReg() != 0 ||
312 Inst.getOperand(AddrBase + 1).getImm() != 1))
315 // If so, rewrite the instruction.
316 MCOperand Saved = Inst.getOperand(AddrOp);
318 Inst.setOpcode(Opcode);
319 Inst.addOperand(Saved);
322 void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
323 OutMI.setOpcode(MI->getOpcode());
325 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
326 const MachineOperand &MO = MI->getOperand(i);
329 switch (MO.getType()) {
332 llvm_unreachable("unknown operand type");
333 case MachineOperand::MO_Register:
334 // Ignore all implicit register operands.
335 if (MO.isImplicit()) continue;
336 MCOp = MCOperand::CreateReg(MO.getReg());
338 case MachineOperand::MO_Immediate:
339 MCOp = MCOperand::CreateImm(MO.getImm());
341 case MachineOperand::MO_MachineBasicBlock:
342 case MachineOperand::MO_GlobalAddress:
343 case MachineOperand::MO_ExternalSymbol:
344 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
346 case MachineOperand::MO_JumpTableIndex:
347 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()));
349 case MachineOperand::MO_ConstantPoolIndex:
350 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex()));
352 case MachineOperand::MO_BlockAddress:
353 MCOp = LowerSymbolOperand(MO,
354 AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress()));
356 case MachineOperand::MO_RegisterMask:
357 // Ignore call clobbers.
361 OutMI.addOperand(MCOp);
364 // Handle a few special cases to eliminate operand modifiers.
366 switch (OutMI.getOpcode()) {
367 case X86::LEA64_32r: // Handle 'subreg rewriting' for the lea64_32mem operand.
368 lower_lea64_32mem(&OutMI, 1);
373 // LEA should have a segment register, but it must be empty.
374 assert(OutMI.getNumOperands() == 1+X86::AddrNumOperands &&
375 "Unexpected # of LEA operands");
376 assert(OutMI.getOperand(1+X86::AddrSegmentReg).getReg() == 0 &&
377 "LEA has segment specified!");
379 case X86::MOV32r0: LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); break;
382 OutMI.setOpcode(X86::MOV32ri);
385 // Commute operands to get a smaller encoding by using VEX.R instead of VEX.B
386 // if one of the registers is extended, but other isn't.
388 case X86::VMOVAPDYrr:
390 case X86::VMOVAPSYrr:
392 case X86::VMOVDQAYrr:
394 case X86::VMOVDQUYrr:
396 case X86::VMOVUPDYrr:
398 case X86::VMOVUPSYrr: {
399 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(0).getReg()) &&
400 X86II::isX86_64ExtendedReg(OutMI.getOperand(1).getReg())) {
402 switch (OutMI.getOpcode()) {
403 default: llvm_unreachable("Invalid opcode");
404 case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
405 case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
406 case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
407 case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
408 case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
409 case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
410 case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
411 case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
412 case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
413 case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
414 case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
415 case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
417 OutMI.setOpcode(NewOpc);
422 case X86::VMOVSSrr: {
423 if (!X86II::isX86_64ExtendedReg(OutMI.getOperand(0).getReg()) &&
424 X86II::isX86_64ExtendedReg(OutMI.getOperand(2).getReg())) {
426 switch (OutMI.getOpcode()) {
427 default: llvm_unreachable("Invalid opcode");
428 case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
429 case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
431 OutMI.setOpcode(NewOpc);
436 // TAILJMPr64, CALL64r, CALL64pcrel32 - These instructions have register
437 // inputs modeled as normal uses instead of implicit uses. As such, truncate
438 // off all but the first operand (the callee). FIXME: Change isel.
439 case X86::TAILJMPr64:
441 case X86::CALL64pcrel32: {
442 unsigned Opcode = OutMI.getOpcode();
443 MCOperand Saved = OutMI.getOperand(0);
445 OutMI.setOpcode(Opcode);
446 OutMI.addOperand(Saved);
451 case X86::EH_RETURN64: {
453 OutMI.setOpcode(X86::RET);
457 // TAILJMPd, TAILJMPd64 - Lower to the correct jump instructions.
460 case X86::TAILJMPd64: {
462 switch (OutMI.getOpcode()) {
463 default: llvm_unreachable("Invalid opcode");
464 case X86::TAILJMPr: Opcode = X86::JMP32r; break;
466 case X86::TAILJMPd64: Opcode = X86::JMP_1; break;
469 MCOperand Saved = OutMI.getOperand(0);
471 OutMI.setOpcode(Opcode);
472 OutMI.addOperand(Saved);
476 // These are pseudo-ops for OR to help with the OR->ADD transformation. We do
477 // this with an ugly goto in case the resultant OR uses EAX and needs the
479 case X86::ADD16rr_DB: OutMI.setOpcode(X86::OR16rr); goto ReSimplify;
480 case X86::ADD32rr_DB: OutMI.setOpcode(X86::OR32rr); goto ReSimplify;
481 case X86::ADD64rr_DB: OutMI.setOpcode(X86::OR64rr); goto ReSimplify;
482 case X86::ADD16ri_DB: OutMI.setOpcode(X86::OR16ri); goto ReSimplify;
483 case X86::ADD32ri_DB: OutMI.setOpcode(X86::OR32ri); goto ReSimplify;
484 case X86::ADD64ri32_DB: OutMI.setOpcode(X86::OR64ri32); goto ReSimplify;
485 case X86::ADD16ri8_DB: OutMI.setOpcode(X86::OR16ri8); goto ReSimplify;
486 case X86::ADD32ri8_DB: OutMI.setOpcode(X86::OR32ri8); goto ReSimplify;
487 case X86::ADD64ri8_DB: OutMI.setOpcode(X86::OR64ri8); goto ReSimplify;
489 // The assembler backend wants to see branches in their small form and relax
490 // them to their large form. The JIT can only handle the large form because
491 // it does not do relaxation. For now, translate the large form to the
493 case X86::JMP_4: OutMI.setOpcode(X86::JMP_1); break;
494 case X86::JO_4: OutMI.setOpcode(X86::JO_1); break;
495 case X86::JNO_4: OutMI.setOpcode(X86::JNO_1); break;
496 case X86::JB_4: OutMI.setOpcode(X86::JB_1); break;
497 case X86::JAE_4: OutMI.setOpcode(X86::JAE_1); break;
498 case X86::JE_4: OutMI.setOpcode(X86::JE_1); break;
499 case X86::JNE_4: OutMI.setOpcode(X86::JNE_1); break;
500 case X86::JBE_4: OutMI.setOpcode(X86::JBE_1); break;
501 case X86::JA_4: OutMI.setOpcode(X86::JA_1); break;
502 case X86::JS_4: OutMI.setOpcode(X86::JS_1); break;
503 case X86::JNS_4: OutMI.setOpcode(X86::JNS_1); break;
504 case X86::JP_4: OutMI.setOpcode(X86::JP_1); break;
505 case X86::JNP_4: OutMI.setOpcode(X86::JNP_1); break;
506 case X86::JL_4: OutMI.setOpcode(X86::JL_1); break;
507 case X86::JGE_4: OutMI.setOpcode(X86::JGE_1); break;
508 case X86::JLE_4: OutMI.setOpcode(X86::JLE_1); break;
509 case X86::JG_4: OutMI.setOpcode(X86::JG_1); break;
511 // Atomic load and store require a separate pseudo-inst because Acquire
512 // implies mayStore and Release implies mayLoad; fix these to regular MOV
514 case X86::ACQUIRE_MOV8rm: OutMI.setOpcode(X86::MOV8rm); goto ReSimplify;
515 case X86::ACQUIRE_MOV16rm: OutMI.setOpcode(X86::MOV16rm); goto ReSimplify;
516 case X86::ACQUIRE_MOV32rm: OutMI.setOpcode(X86::MOV32rm); goto ReSimplify;
517 case X86::ACQUIRE_MOV64rm: OutMI.setOpcode(X86::MOV64rm); goto ReSimplify;
518 case X86::RELEASE_MOV8mr: OutMI.setOpcode(X86::MOV8mr); goto ReSimplify;
519 case X86::RELEASE_MOV16mr: OutMI.setOpcode(X86::MOV16mr); goto ReSimplify;
520 case X86::RELEASE_MOV32mr: OutMI.setOpcode(X86::MOV32mr); goto ReSimplify;
521 case X86::RELEASE_MOV64mr: OutMI.setOpcode(X86::MOV64mr); goto ReSimplify;
523 // We don't currently select the correct instruction form for instructions
524 // which have a short %eax, etc. form. Handle this by custom lowering, for
527 // Note, we are currently not handling the following instructions:
528 // MOV64ao8, MOV64o8a
529 // XCHG16ar, XCHG32ar, XCHG64ar
530 case X86::MOV8mr_NOREX:
531 case X86::MOV8mr: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV8ao8); break;
532 case X86::MOV8rm_NOREX:
533 case X86::MOV8rm: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV8o8a); break;
534 case X86::MOV16mr: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV16ao16); break;
535 case X86::MOV16rm: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV16o16a); break;
536 case X86::MOV32mr: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV32ao32); break;
537 case X86::MOV32rm: SimplifyShortMoveForm(AsmPrinter, OutMI, X86::MOV32o32a); break;
539 case X86::ADC8ri: SimplifyShortImmForm(OutMI, X86::ADC8i8); break;
540 case X86::ADC16ri: SimplifyShortImmForm(OutMI, X86::ADC16i16); break;
541 case X86::ADC32ri: SimplifyShortImmForm(OutMI, X86::ADC32i32); break;
542 case X86::ADC64ri32: SimplifyShortImmForm(OutMI, X86::ADC64i32); break;
543 case X86::ADD8ri: SimplifyShortImmForm(OutMI, X86::ADD8i8); break;
544 case X86::ADD16ri: SimplifyShortImmForm(OutMI, X86::ADD16i16); break;
545 case X86::ADD32ri: SimplifyShortImmForm(OutMI, X86::ADD32i32); break;
546 case X86::ADD64ri32: SimplifyShortImmForm(OutMI, X86::ADD64i32); break;
547 case X86::AND8ri: SimplifyShortImmForm(OutMI, X86::AND8i8); break;
548 case X86::AND16ri: SimplifyShortImmForm(OutMI, X86::AND16i16); break;
549 case X86::AND32ri: SimplifyShortImmForm(OutMI, X86::AND32i32); break;
550 case X86::AND64ri32: SimplifyShortImmForm(OutMI, X86::AND64i32); break;
551 case X86::CMP8ri: SimplifyShortImmForm(OutMI, X86::CMP8i8); break;
552 case X86::CMP16ri: SimplifyShortImmForm(OutMI, X86::CMP16i16); break;
553 case X86::CMP32ri: SimplifyShortImmForm(OutMI, X86::CMP32i32); break;
554 case X86::CMP64ri32: SimplifyShortImmForm(OutMI, X86::CMP64i32); break;
555 case X86::OR8ri: SimplifyShortImmForm(OutMI, X86::OR8i8); break;
556 case X86::OR16ri: SimplifyShortImmForm(OutMI, X86::OR16i16); break;
557 case X86::OR32ri: SimplifyShortImmForm(OutMI, X86::OR32i32); break;
558 case X86::OR64ri32: SimplifyShortImmForm(OutMI, X86::OR64i32); break;
559 case X86::SBB8ri: SimplifyShortImmForm(OutMI, X86::SBB8i8); break;
560 case X86::SBB16ri: SimplifyShortImmForm(OutMI, X86::SBB16i16); break;
561 case X86::SBB32ri: SimplifyShortImmForm(OutMI, X86::SBB32i32); break;
562 case X86::SBB64ri32: SimplifyShortImmForm(OutMI, X86::SBB64i32); break;
563 case X86::SUB8ri: SimplifyShortImmForm(OutMI, X86::SUB8i8); break;
564 case X86::SUB16ri: SimplifyShortImmForm(OutMI, X86::SUB16i16); break;
565 case X86::SUB32ri: SimplifyShortImmForm(OutMI, X86::SUB32i32); break;
566 case X86::SUB64ri32: SimplifyShortImmForm(OutMI, X86::SUB64i32); break;
567 case X86::TEST8ri: SimplifyShortImmForm(OutMI, X86::TEST8i8); break;
568 case X86::TEST16ri: SimplifyShortImmForm(OutMI, X86::TEST16i16); break;
569 case X86::TEST32ri: SimplifyShortImmForm(OutMI, X86::TEST32i32); break;
570 case X86::TEST64ri32: SimplifyShortImmForm(OutMI, X86::TEST64i32); break;
571 case X86::XOR8ri: SimplifyShortImmForm(OutMI, X86::XOR8i8); break;
572 case X86::XOR16ri: SimplifyShortImmForm(OutMI, X86::XOR16i16); break;
573 case X86::XOR32ri: SimplifyShortImmForm(OutMI, X86::XOR32i32); break;
574 case X86::XOR64ri32: SimplifyShortImmForm(OutMI, X86::XOR64i32); break;
576 case X86::MORESTACK_RET:
577 OutMI.setOpcode(X86::RET);
580 case X86::MORESTACK_RET_RESTORE_R10:
581 OutMI.setOpcode(X86::MOV64rr);
582 OutMI.addOperand(MCOperand::CreateReg(X86::R10));
583 OutMI.addOperand(MCOperand::CreateReg(X86::RAX));
585 AsmPrinter.OutStreamer.EmitInstruction(MCInstBuilder(X86::RET));
590 static void LowerTlsAddr(MCStreamer &OutStreamer,
591 X86MCInstLower &MCInstLowering,
592 const MachineInstr &MI) {
594 bool is64Bits = MI.getOpcode() == X86::TLS_addr64 ||
595 MI.getOpcode() == X86::TLS_base_addr64;
597 bool needsPadding = MI.getOpcode() == X86::TLS_addr64;
599 MCContext &context = OutStreamer.getContext();
602 OutStreamer.EmitInstruction(MCInstBuilder(X86::DATA16_PREFIX));
604 MCSymbolRefExpr::VariantKind SRVK;
605 switch (MI.getOpcode()) {
606 case X86::TLS_addr32:
607 case X86::TLS_addr64:
608 SRVK = MCSymbolRefExpr::VK_TLSGD;
610 case X86::TLS_base_addr32:
611 SRVK = MCSymbolRefExpr::VK_TLSLDM;
613 case X86::TLS_base_addr64:
614 SRVK = MCSymbolRefExpr::VK_TLSLD;
617 llvm_unreachable("unexpected opcode");
620 MCSymbol *sym = MCInstLowering.GetSymbolFromOperand(MI.getOperand(3));
621 const MCSymbolRefExpr *symRef = MCSymbolRefExpr::Create(sym, SRVK, context);
625 LEA.setOpcode(X86::LEA64r);
626 LEA.addOperand(MCOperand::CreateReg(X86::RDI)); // dest
627 LEA.addOperand(MCOperand::CreateReg(X86::RIP)); // base
628 LEA.addOperand(MCOperand::CreateImm(1)); // scale
629 LEA.addOperand(MCOperand::CreateReg(0)); // index
630 LEA.addOperand(MCOperand::CreateExpr(symRef)); // disp
631 LEA.addOperand(MCOperand::CreateReg(0)); // seg
632 } else if (SRVK == MCSymbolRefExpr::VK_TLSLDM) {
633 LEA.setOpcode(X86::LEA32r);
634 LEA.addOperand(MCOperand::CreateReg(X86::EAX)); // dest
635 LEA.addOperand(MCOperand::CreateReg(X86::EBX)); // base
636 LEA.addOperand(MCOperand::CreateImm(1)); // scale
637 LEA.addOperand(MCOperand::CreateReg(0)); // index
638 LEA.addOperand(MCOperand::CreateExpr(symRef)); // disp
639 LEA.addOperand(MCOperand::CreateReg(0)); // seg
641 LEA.setOpcode(X86::LEA32r);
642 LEA.addOperand(MCOperand::CreateReg(X86::EAX)); // dest
643 LEA.addOperand(MCOperand::CreateReg(0)); // base
644 LEA.addOperand(MCOperand::CreateImm(1)); // scale
645 LEA.addOperand(MCOperand::CreateReg(X86::EBX)); // index
646 LEA.addOperand(MCOperand::CreateExpr(symRef)); // disp
647 LEA.addOperand(MCOperand::CreateReg(0)); // seg
649 OutStreamer.EmitInstruction(LEA);
652 OutStreamer.EmitInstruction(MCInstBuilder(X86::DATA16_PREFIX));
653 OutStreamer.EmitInstruction(MCInstBuilder(X86::DATA16_PREFIX));
654 OutStreamer.EmitInstruction(MCInstBuilder(X86::REX64_PREFIX));
657 StringRef name = is64Bits ? "__tls_get_addr" : "___tls_get_addr";
658 MCSymbol *tlsGetAddr = context.GetOrCreateSymbol(name);
659 const MCSymbolRefExpr *tlsRef =
660 MCSymbolRefExpr::Create(tlsGetAddr,
661 MCSymbolRefExpr::VK_PLT,
664 OutStreamer.EmitInstruction(MCInstBuilder(is64Bits ? X86::CALL64pcrel32
669 void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
670 X86MCInstLower MCInstLowering(Mang, *MF, *this);
671 switch (MI->getOpcode()) {
672 case TargetOpcode::DBG_VALUE:
673 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
675 raw_string_ostream OS(TmpStr);
676 PrintDebugValueComment(MI, OS);
677 OutStreamer.EmitRawText(StringRef(OS.str()));
681 // Emit nothing here but a comment if we can.
682 case X86::Int_MemBarrier:
683 if (OutStreamer.hasRawTextSupport())
684 OutStreamer.EmitRawText(StringRef("\t#MEMBARRIER"));
689 case X86::EH_RETURN64: {
690 // Lower these as normal, but add some comments.
691 unsigned Reg = MI->getOperand(0).getReg();
692 OutStreamer.AddComment(StringRef("eh_return, addr: %") +
693 X86ATTInstPrinter::getRegisterName(Reg));
698 case X86::TAILJMPd64:
699 // Lower these as normal, but add some comments.
700 OutStreamer.AddComment("TAILCALL");
703 case X86::TLS_addr32:
704 case X86::TLS_addr64:
705 case X86::TLS_base_addr32:
706 case X86::TLS_base_addr64:
707 return LowerTlsAddr(OutStreamer, MCInstLowering, *MI);
709 case X86::MOVPC32r: {
710 // This is a pseudo op for a two instruction sequence with a label, which
717 MCSymbol *PICBase = MF->getPICBaseSymbol();
718 // FIXME: We would like an efficient form for this, so we don't have to do a
719 // lot of extra uniquing.
720 OutStreamer.EmitInstruction(MCInstBuilder(X86::CALLpcrel32)
721 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
724 OutStreamer.EmitLabel(PICBase);
727 OutStreamer.EmitInstruction(MCInstBuilder(X86::POP32r)
728 .addReg(MI->getOperand(0).getReg()));
733 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
734 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
737 // Okay, we have something like:
738 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
740 // For this, we want to print something like:
741 // MYGLOBAL + (. - PICBASE)
742 // However, we can't generate a ".", so just emit a new label here and refer
744 MCSymbol *DotSym = OutContext.CreateTempSymbol();
745 OutStreamer.EmitLabel(DotSym);
747 // Now that we have emitted the label, lower the complex operand expression.
748 MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2));
750 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
751 const MCExpr *PICBase =
752 MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), OutContext);
753 DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, OutContext);
755 DotExpr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym,OutContext),
756 DotExpr, OutContext);
758 OutStreamer.EmitInstruction(MCInstBuilder(X86::ADD32ri)
759 .addReg(MI->getOperand(0).getReg())
760 .addReg(MI->getOperand(1).getReg())
767 MCInstLowering.Lower(MI, TmpInst);
768 OutStreamer.EmitInstruction(TmpInst);