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 "X86MCInstLower.h"
16 #include "X86AsmPrinter.h"
17 #include "X86COFFMachineModuleInfo.h"
18 #include "X86MCAsmInfo.h"
19 #include "X86MCTargetExpr.h"
20 #include "llvm/Analysis/DebugInfo.h"
21 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/ADT/SmallString.h"
30 #include "llvm/Type.h"
34 const X86Subtarget &X86MCInstLower::getSubtarget() const {
35 return AsmPrinter.getSubtarget();
38 MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
39 assert(getSubtarget().isTargetDarwin() &&"Can only get MachO info on darwin");
40 return AsmPrinter.MMI->getObjFileInfo<MachineModuleInfoMachO>();
44 MCSymbol *X86MCInstLower::GetPICBaseSymbol() const {
45 const TargetLowering *TLI = AsmPrinter.TM.getTargetLowering();
46 return static_cast<const X86TargetLowering*>(TLI)->
47 getPICBaseSymbol(AsmPrinter.MF, Ctx);
50 /// GetSymbolFromOperand - Lower an MO_GlobalAddress or MO_ExternalSymbol
51 /// operand to an MCSymbol.
52 MCSymbol *X86MCInstLower::
53 GetSymbolFromOperand(const MachineOperand &MO) const {
54 assert((MO.isGlobal() || MO.isSymbol()) && "Isn't a symbol reference");
56 SmallString<128> Name;
59 assert(MO.isSymbol());
60 Name += AsmPrinter.MAI->getGlobalPrefix();
61 Name += MO.getSymbolName();
63 const GlobalValue *GV = MO.getGlobal();
64 bool isImplicitlyPrivate = false;
65 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB ||
66 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
67 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
68 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
69 isImplicitlyPrivate = true;
71 Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
74 // If the target flags on the operand changes the name of the symbol, do that
75 // before we return the symbol.
76 switch (MO.getTargetFlags()) {
78 case X86II::MO_DLLIMPORT: {
79 // Handle dllimport linkage.
80 const char *Prefix = "__imp_";
81 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
84 case X86II::MO_DARWIN_NONLAZY:
85 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: {
86 Name += "$non_lazy_ptr";
87 MCSymbol *Sym = Ctx.GetOrCreateTemporarySymbol(Name.str());
89 MachineModuleInfoImpl::StubValueTy &StubSym =
90 getMachOMMI().getGVStubEntry(Sym);
91 if (StubSym.getPointer() == 0) {
92 assert(MO.isGlobal() && "Extern symbol not handled yet");
94 MachineModuleInfoImpl::
95 StubValueTy(AsmPrinter.Mang->getSymbol(MO.getGlobal()),
96 !MO.getGlobal()->hasInternalLinkage());
100 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: {
101 Name += "$non_lazy_ptr";
102 MCSymbol *Sym = Ctx.GetOrCreateTemporarySymbol(Name.str());
103 MachineModuleInfoImpl::StubValueTy &StubSym =
104 getMachOMMI().getHiddenGVStubEntry(Sym);
105 if (StubSym.getPointer() == 0) {
106 assert(MO.isGlobal() && "Extern symbol not handled yet");
108 MachineModuleInfoImpl::
109 StubValueTy(AsmPrinter.Mang->getSymbol(MO.getGlobal()),
110 !MO.getGlobal()->hasInternalLinkage());
114 case X86II::MO_DARWIN_STUB: {
116 MCSymbol *Sym = Ctx.GetOrCreateTemporarySymbol(Name.str());
117 MachineModuleInfoImpl::StubValueTy &StubSym =
118 getMachOMMI().getFnStubEntry(Sym);
119 if (StubSym.getPointer())
124 MachineModuleInfoImpl::
125 StubValueTy(AsmPrinter.Mang->getSymbol(MO.getGlobal()),
126 !MO.getGlobal()->hasInternalLinkage());
128 Name.erase(Name.end()-5, Name.end());
130 MachineModuleInfoImpl::
131 StubValueTy(Ctx.GetOrCreateTemporarySymbol(Name.str()), false);
137 return Ctx.GetOrCreateSymbol(Name.str());
140 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
141 MCSymbol *Sym) const {
142 // FIXME: We would like an efficient form for this, so we don't have to do a
143 // lot of extra uniquing.
144 const MCExpr *Expr = 0;
145 X86MCTargetExpr::VariantKind RefKind = X86MCTargetExpr::Invalid;
147 switch (MO.getTargetFlags()) {
148 default: llvm_unreachable("Unknown target flag on GV operand");
149 case X86II::MO_NO_FLAG: // No flag.
150 // These affect the name of the symbol, not any suffix.
151 case X86II::MO_DARWIN_NONLAZY:
152 case X86II::MO_DLLIMPORT:
153 case X86II::MO_DARWIN_STUB:
156 case X86II::MO_TLSGD: RefKind = X86MCTargetExpr::TLSGD; break;
157 case X86II::MO_GOTTPOFF: RefKind = X86MCTargetExpr::GOTTPOFF; break;
158 case X86II::MO_INDNTPOFF: RefKind = X86MCTargetExpr::INDNTPOFF; break;
159 case X86II::MO_TPOFF: RefKind = X86MCTargetExpr::TPOFF; break;
160 case X86II::MO_NTPOFF: RefKind = X86MCTargetExpr::NTPOFF; break;
161 case X86II::MO_GOTPCREL: RefKind = X86MCTargetExpr::GOTPCREL; break;
162 case X86II::MO_GOT: RefKind = X86MCTargetExpr::GOT; break;
163 case X86II::MO_GOTOFF: RefKind = X86MCTargetExpr::GOTOFF; break;
164 case X86II::MO_PLT: RefKind = X86MCTargetExpr::PLT; break;
165 case X86II::MO_PIC_BASE_OFFSET:
166 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
167 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
168 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
169 // Subtract the pic base.
170 Expr = MCBinaryExpr::CreateSub(Expr,
171 MCSymbolRefExpr::Create(GetPICBaseSymbol(), Ctx),
177 if (RefKind == X86MCTargetExpr::Invalid)
178 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
180 Expr = X86MCTargetExpr::Create(Sym, RefKind, Ctx);
183 if (!MO.isJTI() && MO.getOffset())
184 Expr = MCBinaryExpr::CreateAdd(Expr,
185 MCConstantExpr::Create(MO.getOffset(), Ctx),
187 return MCOperand::CreateExpr(Expr);
192 static void lower_subreg32(MCInst *MI, unsigned OpNo) {
193 // Convert registers in the addr mode according to subreg32.
194 unsigned Reg = MI->getOperand(OpNo).getReg();
196 MI->getOperand(OpNo).setReg(getX86SubSuperRegister(Reg, MVT::i32));
199 static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
200 // Convert registers in the addr mode according to subreg64.
201 for (unsigned i = 0; i != 4; ++i) {
202 if (!MI->getOperand(OpNo+i).isReg()) continue;
204 unsigned Reg = MI->getOperand(OpNo+i).getReg();
205 if (Reg == 0) continue;
207 MI->getOperand(OpNo+i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
211 /// LowerSubReg32_Op0 - Things like MOVZX16rr8 -> MOVZX32rr8.
212 static void LowerSubReg32_Op0(MCInst &OutMI, unsigned NewOpc) {
213 OutMI.setOpcode(NewOpc);
214 lower_subreg32(&OutMI, 0);
216 /// LowerUnaryToTwoAddr - R = setb -> R = sbb R, R
217 static void LowerUnaryToTwoAddr(MCInst &OutMI, unsigned NewOpc) {
218 OutMI.setOpcode(NewOpc);
219 OutMI.addOperand(OutMI.getOperand(0));
220 OutMI.addOperand(OutMI.getOperand(0));
224 void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
225 OutMI.setOpcode(MI->getOpcode());
227 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
228 const MachineOperand &MO = MI->getOperand(i);
231 switch (MO.getType()) {
234 llvm_unreachable("unknown operand type");
235 case MachineOperand::MO_Register:
236 // Ignore all implicit register operands.
237 if (MO.isImplicit()) continue;
238 MCOp = MCOperand::CreateReg(MO.getReg());
240 case MachineOperand::MO_Immediate:
241 MCOp = MCOperand::CreateImm(MO.getImm());
243 case MachineOperand::MO_MachineBasicBlock:
244 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
245 MO.getMBB()->getSymbol(), Ctx));
247 case MachineOperand::MO_GlobalAddress:
248 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
250 case MachineOperand::MO_ExternalSymbol:
251 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
253 case MachineOperand::MO_JumpTableIndex:
254 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()));
256 case MachineOperand::MO_ConstantPoolIndex:
257 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex()));
259 case MachineOperand::MO_BlockAddress:
260 MCOp = LowerSymbolOperand(MO,
261 AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress()));
265 OutMI.addOperand(MCOp);
268 // Handle a few special cases to eliminate operand modifiers.
269 switch (OutMI.getOpcode()) {
270 case X86::LEA64_32r: // Handle 'subreg rewriting' for the lea64_32mem operand.
271 lower_lea64_32mem(&OutMI, 1);
273 case X86::MOVZX16rr8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr8); break;
274 case X86::MOVZX16rm8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm8); break;
275 case X86::MOVSX16rr8: LowerSubReg32_Op0(OutMI, X86::MOVSX32rr8); break;
276 case X86::MOVSX16rm8: LowerSubReg32_Op0(OutMI, X86::MOVSX32rm8); break;
277 case X86::MOVZX64rr32: LowerSubReg32_Op0(OutMI, X86::MOV32rr); break;
278 case X86::MOVZX64rm32: LowerSubReg32_Op0(OutMI, X86::MOV32rm); break;
279 case X86::MOV64ri64i32: LowerSubReg32_Op0(OutMI, X86::MOV32ri); break;
280 case X86::MOVZX64rr8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr8); break;
281 case X86::MOVZX64rm8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm8); break;
282 case X86::MOVZX64rr16: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr16); break;
283 case X86::MOVZX64rm16: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm16); break;
284 case X86::SETB_C8r: LowerUnaryToTwoAddr(OutMI, X86::SBB8rr); break;
285 case X86::SETB_C16r: LowerUnaryToTwoAddr(OutMI, X86::SBB16rr); break;
286 case X86::SETB_C32r: LowerUnaryToTwoAddr(OutMI, X86::SBB32rr); break;
287 case X86::SETB_C64r: LowerUnaryToTwoAddr(OutMI, X86::SBB64rr); break;
288 case X86::MOV8r0: LowerUnaryToTwoAddr(OutMI, X86::XOR8rr); break;
289 case X86::MOV32r0: LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); break;
290 case X86::MMX_V_SET0: LowerUnaryToTwoAddr(OutMI, X86::MMX_PXORrr); break;
291 case X86::MMX_V_SETALLONES:
292 LowerUnaryToTwoAddr(OutMI, X86::MMX_PCMPEQDrr); break;
293 case X86::FsFLD0SS: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
294 case X86::FsFLD0SD: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
295 case X86::V_SET0: LowerUnaryToTwoAddr(OutMI, X86::XORPSrr); break;
296 case X86::V_SETALLONES: LowerUnaryToTwoAddr(OutMI, X86::PCMPEQDrr); break;
299 LowerSubReg32_Op0(OutMI, X86::MOV32r0); // MOV16r0 -> MOV32r0
300 LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); // MOV32r0 -> XOR32rr
303 LowerSubReg32_Op0(OutMI, X86::MOV32r0); // MOV64r0 -> MOV32r0
304 LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); // MOV32r0 -> XOR32rr
308 // The assembler backend wants to see branches in their small form and relax
309 // them to their large form. The JIT can only handle the large form because
310 // it does not do relaxation. For now, translate the large form to the
312 case X86::JMP_4: OutMI.setOpcode(X86::JMP_1); break;
313 case X86::JO_4: OutMI.setOpcode(X86::JO_1); break;
314 case X86::JNO_4: OutMI.setOpcode(X86::JNO_1); break;
315 case X86::JB_4: OutMI.setOpcode(X86::JB_1); break;
316 case X86::JAE_4: OutMI.setOpcode(X86::JAE_1); break;
317 case X86::JE_4: OutMI.setOpcode(X86::JE_1); break;
318 case X86::JNE_4: OutMI.setOpcode(X86::JNE_1); break;
319 case X86::JBE_4: OutMI.setOpcode(X86::JBE_1); break;
320 case X86::JA_4: OutMI.setOpcode(X86::JA_1); break;
321 case X86::JS_4: OutMI.setOpcode(X86::JS_1); break;
322 case X86::JNS_4: OutMI.setOpcode(X86::JNS_1); break;
323 case X86::JP_4: OutMI.setOpcode(X86::JP_1); break;
324 case X86::JNP_4: OutMI.setOpcode(X86::JNP_1); break;
325 case X86::JL_4: OutMI.setOpcode(X86::JL_1); break;
326 case X86::JGE_4: OutMI.setOpcode(X86::JGE_1); break;
327 case X86::JLE_4: OutMI.setOpcode(X86::JLE_1); break;
328 case X86::JG_4: OutMI.setOpcode(X86::JG_1); break;
334 void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
335 X86MCInstLower MCInstLowering(OutContext, Mang, *this);
336 switch (MI->getOpcode()) {
337 case TargetOpcode::DBG_VALUE: {
338 // FIXME: if this is implemented for another target before it goes
339 // away completely, the common part should be moved into AsmPrinter.
342 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
343 unsigned NOps = MI->getNumOperands();
344 // cast away const; DIetc do not take const operands for some reason.
345 DIVariable V((MDNode*)(MI->getOperand(NOps-1).getMetadata()));
349 // Register or immediate value. Register 0 means undef.
350 assert(MI->getOperand(0).getType()==MachineOperand::MO_Register ||
351 MI->getOperand(0).getType()==MachineOperand::MO_Immediate ||
352 MI->getOperand(0).getType()==MachineOperand::MO_FPImmediate);
353 if (MI->getOperand(0).getType()==MachineOperand::MO_Register &&
354 MI->getOperand(0).getReg()==0) {
355 // Suppress offset in this case, it is not meaningful.
357 OutStreamer.AddBlankLine();
359 } else if (MI->getOperand(0).getType()==MachineOperand::MO_FPImmediate) {
360 // This is more naturally done in printOperand, but since the only use
361 // of such an operand is in this comment and that is temporary (and it's
362 // ugly), we prefer to keep this localized.
363 // The include of Type.h may be removable when this code is.
364 if (MI->getOperand(0).getFPImm()->getType()->isFloatTy() ||
365 MI->getOperand(0).getFPImm()->getType()->isDoubleTy())
366 MI->getOperand(0).print(O, &TM);
368 // There is no good way to print long double. Convert a copy to
369 // double. Ah well, it's only a comment.
371 APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
372 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
374 O << "(long double) " << APF.convertToDouble();
379 if (MI->getOperand(0).getType()==MachineOperand::MO_Register &&
380 MI->getOperand(0).getReg()==0) {
381 // Suppress offset in this case, it is not meaningful.
383 OutStreamer.AddBlankLine();
386 // Frame address. Currently handles register +- offset only.
387 assert(MI->getOperand(0).getType()==MachineOperand::MO_Register);
388 assert(MI->getOperand(3).getType()==MachineOperand::MO_Immediate);
389 O << '['; printOperand(MI, 0); O << '+'; printOperand(MI, 3); O << ']';
392 printOperand(MI, NOps-2);
393 OutStreamer.AddBlankLine();
396 case X86::MOVPC32r: {
398 // This is a pseudo op for a two instruction sequence with a label, which
405 MCSymbol *PICBase = MCInstLowering.GetPICBaseSymbol();
406 TmpInst.setOpcode(X86::CALLpcrel32);
407 // FIXME: We would like an efficient form for this, so we don't have to do a
408 // lot of extra uniquing.
409 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase,
411 OutStreamer.EmitInstruction(TmpInst);
414 OutStreamer.EmitLabel(PICBase);
417 TmpInst.setOpcode(X86::POP32r);
418 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
419 OutStreamer.EmitInstruction(TmpInst);
424 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
425 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
428 // Okay, we have something like:
429 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
431 // For this, we want to print something like:
432 // MYGLOBAL + (. - PICBASE)
433 // However, we can't generate a ".", so just emit a new label here and refer
435 MCSymbol *DotSym = OutContext.GetOrCreateTemporarySymbol();
436 OutStreamer.EmitLabel(DotSym);
438 // Now that we have emitted the label, lower the complex operand expression.
439 MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2));
441 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
442 const MCExpr *PICBase =
443 MCSymbolRefExpr::Create(MCInstLowering.GetPICBaseSymbol(), OutContext);
444 DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, OutContext);
446 DotExpr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym,OutContext),
447 DotExpr, OutContext);
450 TmpInst.setOpcode(X86::ADD32ri);
451 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
452 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
453 TmpInst.addOperand(MCOperand::CreateExpr(DotExpr));
454 OutStreamer.EmitInstruction(TmpInst);
460 MCInstLowering.Lower(MI, TmpInst);
462 OutStreamer.EmitInstruction(TmpInst);