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/Target/Mangler.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/Type.h"
33 const X86Subtarget &X86MCInstLower::getSubtarget() const {
34 return AsmPrinter.getSubtarget();
37 MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
38 assert(getSubtarget().isTargetDarwin() &&"Can only get MachO info on darwin");
39 return AsmPrinter.MMI->getObjFileInfo<MachineModuleInfoMachO>();
43 MCSymbol *X86MCInstLower::GetPICBaseSymbol() const {
44 const TargetLowering *TLI = AsmPrinter.TM.getTargetLowering();
45 return static_cast<const X86TargetLowering*>(TLI)->
46 getPICBaseSymbol(AsmPrinter.MF, Ctx);
49 /// GetSymbolFromOperand - Lower an MO_GlobalAddress or MO_ExternalSymbol
50 /// operand to an MCSymbol.
51 MCSymbol *X86MCInstLower::
52 GetSymbolFromOperand(const MachineOperand &MO) const {
53 assert((MO.isGlobal() || MO.isSymbol()) && "Isn't a symbol reference");
55 SmallString<128> Name;
58 bool isImplicitlyPrivate = false;
59 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB ||
60 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
61 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
62 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
63 isImplicitlyPrivate = true;
65 const GlobalValue *GV = MO.getGlobal();
66 Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
68 if (getSubtarget().isTargetCygMing()) {
69 X86COFFMachineModuleInfo &COFFMMI =
70 AsmPrinter.MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
71 COFFMMI.DecorateCygMingName(Name, GV, *AsmPrinter.TM.getTargetData());
74 assert(MO.isSymbol());
75 Name += AsmPrinter.MAI->getGlobalPrefix();
76 Name += MO.getSymbolName();
79 // If the target flags on the operand changes the name of the symbol, do that
80 // before we return the symbol.
81 switch (MO.getTargetFlags()) {
83 case X86II::MO_DLLIMPORT: {
84 // Handle dllimport linkage.
85 const char *Prefix = "__imp_";
86 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
89 case X86II::MO_DARWIN_NONLAZY:
90 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: {
91 Name += "$non_lazy_ptr";
92 MCSymbol *Sym = Ctx.GetOrCreateTemporarySymbol(Name.str());
94 MachineModuleInfoImpl::StubValueTy &StubSym =
95 getMachOMMI().getGVStubEntry(Sym);
96 if (StubSym.getPointer() == 0) {
97 assert(MO.isGlobal() && "Extern symbol not handled yet");
99 MachineModuleInfoImpl::
100 StubValueTy(AsmPrinter.GetGlobalValueSymbol(MO.getGlobal()),
101 !MO.getGlobal()->hasInternalLinkage());
105 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: {
106 Name += "$non_lazy_ptr";
107 MCSymbol *Sym = Ctx.GetOrCreateTemporarySymbol(Name.str());
108 MachineModuleInfoImpl::StubValueTy &StubSym =
109 getMachOMMI().getHiddenGVStubEntry(Sym);
110 if (StubSym.getPointer() == 0) {
111 assert(MO.isGlobal() && "Extern symbol not handled yet");
113 MachineModuleInfoImpl::
114 StubValueTy(AsmPrinter.GetGlobalValueSymbol(MO.getGlobal()),
115 !MO.getGlobal()->hasInternalLinkage());
119 case X86II::MO_DARWIN_STUB: {
121 MCSymbol *Sym = Ctx.GetOrCreateTemporarySymbol(Name.str());
122 MachineModuleInfoImpl::StubValueTy &StubSym =
123 getMachOMMI().getFnStubEntry(Sym);
124 if (StubSym.getPointer())
129 MachineModuleInfoImpl::
130 StubValueTy(AsmPrinter.GetGlobalValueSymbol(MO.getGlobal()),
131 !MO.getGlobal()->hasInternalLinkage());
133 Name.erase(Name.end()-5, Name.end());
135 MachineModuleInfoImpl::
136 StubValueTy(Ctx.GetOrCreateTemporarySymbol(Name.str()), false);
142 return Ctx.GetOrCreateSymbol(Name.str());
145 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
146 MCSymbol *Sym) const {
147 // FIXME: We would like an efficient form for this, so we don't have to do a
148 // lot of extra uniquing.
149 const MCExpr *Expr = 0;
150 X86MCTargetExpr::VariantKind RefKind = X86MCTargetExpr::Invalid;
152 switch (MO.getTargetFlags()) {
153 default: llvm_unreachable("Unknown target flag on GV operand");
154 case X86II::MO_NO_FLAG: // No flag.
155 // These affect the name of the symbol, not any suffix.
156 case X86II::MO_DARWIN_NONLAZY:
157 case X86II::MO_DLLIMPORT:
158 case X86II::MO_DARWIN_STUB:
161 case X86II::MO_TLSGD: RefKind = X86MCTargetExpr::TLSGD; break;
162 case X86II::MO_GOTTPOFF: RefKind = X86MCTargetExpr::GOTTPOFF; break;
163 case X86II::MO_INDNTPOFF: RefKind = X86MCTargetExpr::INDNTPOFF; break;
164 case X86II::MO_TPOFF: RefKind = X86MCTargetExpr::TPOFF; break;
165 case X86II::MO_NTPOFF: RefKind = X86MCTargetExpr::NTPOFF; break;
166 case X86II::MO_GOTPCREL: RefKind = X86MCTargetExpr::GOTPCREL; break;
167 case X86II::MO_GOT: RefKind = X86MCTargetExpr::GOT; break;
168 case X86II::MO_GOTOFF: RefKind = X86MCTargetExpr::GOTOFF; break;
169 case X86II::MO_PLT: RefKind = X86MCTargetExpr::PLT; break;
170 case X86II::MO_PIC_BASE_OFFSET:
171 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
172 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
173 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
174 // Subtract the pic base.
175 Expr = MCBinaryExpr::CreateSub(Expr,
176 MCSymbolRefExpr::Create(GetPICBaseSymbol(), Ctx),
182 if (RefKind == X86MCTargetExpr::Invalid)
183 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
185 Expr = X86MCTargetExpr::Create(Sym, RefKind, Ctx);
188 if (!MO.isJTI() && MO.getOffset())
189 Expr = MCBinaryExpr::CreateAdd(Expr,
190 MCConstantExpr::Create(MO.getOffset(), Ctx),
192 return MCOperand::CreateExpr(Expr);
197 static void lower_subreg32(MCInst *MI, unsigned OpNo) {
198 // Convert registers in the addr mode according to subreg32.
199 unsigned Reg = MI->getOperand(OpNo).getReg();
201 MI->getOperand(OpNo).setReg(getX86SubSuperRegister(Reg, MVT::i32));
204 static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
205 // Convert registers in the addr mode according to subreg64.
206 for (unsigned i = 0; i != 4; ++i) {
207 if (!MI->getOperand(OpNo+i).isReg()) continue;
209 unsigned Reg = MI->getOperand(OpNo+i).getReg();
210 if (Reg == 0) continue;
212 MI->getOperand(OpNo+i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
216 /// LowerSubReg32_Op0 - Things like MOVZX16rr8 -> MOVZX32rr8.
217 static void LowerSubReg32_Op0(MCInst &OutMI, unsigned NewOpc) {
218 OutMI.setOpcode(NewOpc);
219 lower_subreg32(&OutMI, 0);
221 /// LowerUnaryToTwoAddr - R = setb -> R = sbb R, R
222 static void LowerUnaryToTwoAddr(MCInst &OutMI, unsigned NewOpc) {
223 OutMI.setOpcode(NewOpc);
224 OutMI.addOperand(OutMI.getOperand(0));
225 OutMI.addOperand(OutMI.getOperand(0));
229 void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
230 OutMI.setOpcode(MI->getOpcode());
232 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
233 const MachineOperand &MO = MI->getOperand(i);
236 switch (MO.getType()) {
239 llvm_unreachable("unknown operand type");
240 case MachineOperand::MO_Register:
241 // Ignore all implicit register operands.
242 if (MO.isImplicit()) continue;
243 MCOp = MCOperand::CreateReg(MO.getReg());
245 case MachineOperand::MO_Immediate:
246 MCOp = MCOperand::CreateImm(MO.getImm());
248 case MachineOperand::MO_MachineBasicBlock:
249 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
250 MO.getMBB()->getSymbol(Ctx), Ctx));
252 case MachineOperand::MO_GlobalAddress:
253 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
255 case MachineOperand::MO_ExternalSymbol:
256 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
258 case MachineOperand::MO_JumpTableIndex:
259 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()));
261 case MachineOperand::MO_ConstantPoolIndex:
262 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex()));
264 case MachineOperand::MO_BlockAddress:
265 MCOp = LowerSymbolOperand(MO,
266 AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress()));
270 OutMI.addOperand(MCOp);
273 // Handle a few special cases to eliminate operand modifiers.
274 switch (OutMI.getOpcode()) {
275 case X86::LEA64_32r: // Handle 'subreg rewriting' for the lea64_32mem operand.
276 lower_lea64_32mem(&OutMI, 1);
278 case X86::MOVZX16rr8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr8); break;
279 case X86::MOVZX16rm8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm8); break;
280 case X86::MOVSX16rr8: LowerSubReg32_Op0(OutMI, X86::MOVSX32rr8); break;
281 case X86::MOVSX16rm8: LowerSubReg32_Op0(OutMI, X86::MOVSX32rm8); break;
282 case X86::MOVZX64rr32: LowerSubReg32_Op0(OutMI, X86::MOV32rr); break;
283 case X86::MOVZX64rm32: LowerSubReg32_Op0(OutMI, X86::MOV32rm); break;
284 case X86::MOV64ri64i32: LowerSubReg32_Op0(OutMI, X86::MOV32ri); break;
285 case X86::MOVZX64rr8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr8); break;
286 case X86::MOVZX64rm8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm8); break;
287 case X86::MOVZX64rr16: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr16); break;
288 case X86::MOVZX64rm16: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm16); break;
289 case X86::SETB_C8r: LowerUnaryToTwoAddr(OutMI, X86::SBB8rr); break;
290 case X86::SETB_C16r: LowerUnaryToTwoAddr(OutMI, X86::SBB16rr); break;
291 case X86::SETB_C32r: LowerUnaryToTwoAddr(OutMI, X86::SBB32rr); break;
292 case X86::SETB_C64r: LowerUnaryToTwoAddr(OutMI, X86::SBB64rr); break;
293 case X86::MOV8r0: LowerUnaryToTwoAddr(OutMI, X86::XOR8rr); break;
294 case X86::MOV32r0: LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); break;
295 case X86::MMX_V_SET0: LowerUnaryToTwoAddr(OutMI, X86::MMX_PXORrr); break;
296 case X86::MMX_V_SETALLONES:
297 LowerUnaryToTwoAddr(OutMI, X86::MMX_PCMPEQDrr); break;
298 case X86::FsFLD0SS: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
299 case X86::FsFLD0SD: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
300 case X86::V_SET0: LowerUnaryToTwoAddr(OutMI, X86::XORPSrr); break;
301 case X86::V_SETALLONES: LowerUnaryToTwoAddr(OutMI, X86::PCMPEQDrr); break;
304 LowerSubReg32_Op0(OutMI, X86::MOV32r0); // MOV16r0 -> MOV32r0
305 LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); // MOV32r0 -> XOR32rr
308 LowerSubReg32_Op0(OutMI, X86::MOV32r0); // MOV64r0 -> MOV32r0
309 LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); // MOV32r0 -> XOR32rr
316 void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
317 X86MCInstLower MCInstLowering(OutContext, Mang, *this);
318 switch (MI->getOpcode()) {
319 case TargetOpcode::DBG_VALUE: {
320 // FIXME: if this is implemented for another target before it goes
321 // away completely, the common part should be moved into AsmPrinter.
324 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
325 unsigned NOps = MI->getNumOperands();
326 // cast away const; DIetc do not take const operands for some reason.
327 DIVariable V((MDNode*)(MI->getOperand(NOps-1).getMetadata()));
331 // Register or immediate value. Register 0 means undef.
332 assert(MI->getOperand(0).getType()==MachineOperand::MO_Register ||
333 MI->getOperand(0).getType()==MachineOperand::MO_Immediate ||
334 MI->getOperand(0).getType()==MachineOperand::MO_FPImmediate);
335 if (MI->getOperand(0).getType()==MachineOperand::MO_Register &&
336 MI->getOperand(0).getReg()==0) {
337 // Suppress offset in this case, it is not meaningful.
339 OutStreamer.AddBlankLine();
341 } else if (MI->getOperand(0).getType()==MachineOperand::MO_FPImmediate) {
342 // This is more naturally done in printOperand, but since the only use
343 // of such an operand is in this comment and that is temporary (and it's
344 // ugly), we prefer to keep this localized.
345 // The include of Type.h may be removable when this code is.
346 if (MI->getOperand(0).getFPImm()->getType()->isFloatTy() ||
347 MI->getOperand(0).getFPImm()->getType()->isDoubleTy())
348 MI->getOperand(0).print(O, &TM);
350 // There is no good way to print long double. Convert a copy to
351 // double. Ah well, it's only a comment.
353 APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
354 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
356 O << "(long double) " << APF.convertToDouble();
361 if (MI->getOperand(0).getType()==MachineOperand::MO_Register &&
362 MI->getOperand(0).getReg()==0) {
363 // Suppress offset in this case, it is not meaningful.
365 OutStreamer.AddBlankLine();
368 // Frame address. Currently handles register +- offset only.
369 assert(MI->getOperand(0).getType()==MachineOperand::MO_Register);
370 assert(MI->getOperand(3).getType()==MachineOperand::MO_Immediate);
371 O << '['; printOperand(MI, 0); O << '+'; printOperand(MI, 3); O << ']';
374 printOperand(MI, NOps-2);
375 OutStreamer.AddBlankLine();
378 case X86::MOVPC32r: {
380 // This is a pseudo op for a two instruction sequence with a label, which
387 MCSymbol *PICBase = MCInstLowering.GetPICBaseSymbol();
388 TmpInst.setOpcode(X86::CALLpcrel32);
389 // FIXME: We would like an efficient form for this, so we don't have to do a
390 // lot of extra uniquing.
391 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase,
393 OutStreamer.EmitInstruction(TmpInst);
396 OutStreamer.EmitLabel(PICBase);
399 TmpInst.setOpcode(X86::POP32r);
400 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
401 OutStreamer.EmitInstruction(TmpInst);
406 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
407 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
410 // Okay, we have something like:
411 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
413 // For this, we want to print something like:
414 // MYGLOBAL + (. - PICBASE)
415 // However, we can't generate a ".", so just emit a new label here and refer
417 MCSymbol *DotSym = OutContext.GetOrCreateTemporarySymbol();
418 OutStreamer.EmitLabel(DotSym);
420 // Now that we have emitted the label, lower the complex operand expression.
421 MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2));
423 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
424 const MCExpr *PICBase =
425 MCSymbolRefExpr::Create(MCInstLowering.GetPICBaseSymbol(), OutContext);
426 DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, OutContext);
428 DotExpr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym,OutContext),
429 DotExpr, OutContext);
432 TmpInst.setOpcode(X86::ADD32ri);
433 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
434 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
435 TmpInst.addOperand(MCOperand::CreateExpr(DotExpr));
436 OutStreamer.EmitInstruction(TmpInst);
442 MCInstLowering.Lower(MI, TmpInst);
444 OutStreamer.EmitInstruction(TmpInst);