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 "llvm/Analysis/DebugInfo.h"
20 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.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 assert(MO.isSymbol());
59 Name += AsmPrinter.MAI->getGlobalPrefix();
60 Name += MO.getSymbolName();
62 const GlobalValue *GV = MO.getGlobal();
63 bool isImplicitlyPrivate = false;
64 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB ||
65 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
66 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
67 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
68 isImplicitlyPrivate = true;
70 Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
73 // If the target flags on the operand changes the name of the symbol, do that
74 // before we return the symbol.
75 switch (MO.getTargetFlags()) {
77 case X86II::MO_DLLIMPORT: {
78 // Handle dllimport linkage.
79 const char *Prefix = "__imp_";
80 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
83 case X86II::MO_DARWIN_NONLAZY:
84 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: {
85 Name += "$non_lazy_ptr";
86 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
88 MachineModuleInfoImpl::StubValueTy &StubSym =
89 getMachOMMI().getGVStubEntry(Sym);
90 if (StubSym.getPointer() == 0) {
91 assert(MO.isGlobal() && "Extern symbol not handled yet");
93 MachineModuleInfoImpl::
94 StubValueTy(AsmPrinter.Mang->getSymbol(MO.getGlobal()),
95 !MO.getGlobal()->hasInternalLinkage());
99 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: {
100 Name += "$non_lazy_ptr";
101 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
102 MachineModuleInfoImpl::StubValueTy &StubSym =
103 getMachOMMI().getHiddenGVStubEntry(Sym);
104 if (StubSym.getPointer() == 0) {
105 assert(MO.isGlobal() && "Extern symbol not handled yet");
107 MachineModuleInfoImpl::
108 StubValueTy(AsmPrinter.Mang->getSymbol(MO.getGlobal()),
109 !MO.getGlobal()->hasInternalLinkage());
113 case X86II::MO_DARWIN_STUB: {
115 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
116 MachineModuleInfoImpl::StubValueTy &StubSym =
117 getMachOMMI().getFnStubEntry(Sym);
118 if (StubSym.getPointer())
123 MachineModuleInfoImpl::
124 StubValueTy(AsmPrinter.Mang->getSymbol(MO.getGlobal()),
125 !MO.getGlobal()->hasInternalLinkage());
127 Name.erase(Name.end()-5, Name.end());
129 MachineModuleInfoImpl::
130 StubValueTy(Ctx.GetOrCreateSymbol(Name.str()), false);
136 return Ctx.GetOrCreateSymbol(Name.str());
139 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
140 MCSymbol *Sym) const {
141 // FIXME: We would like an efficient form for this, so we don't have to do a
142 // lot of extra uniquing.
143 const MCExpr *Expr = 0;
144 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
146 switch (MO.getTargetFlags()) {
147 default: llvm_unreachable("Unknown target flag on GV operand");
148 case X86II::MO_NO_FLAG: // No flag.
149 // These affect the name of the symbol, not any suffix.
150 case X86II::MO_DARWIN_NONLAZY:
151 case X86II::MO_DLLIMPORT:
152 case X86II::MO_DARWIN_STUB:
155 case X86II::MO_TLSGD: RefKind = MCSymbolRefExpr::VK_TLSGD; break;
156 case X86II::MO_GOTTPOFF: RefKind = MCSymbolRefExpr::VK_GOTTPOFF; break;
157 case X86II::MO_INDNTPOFF: RefKind = MCSymbolRefExpr::VK_INDNTPOFF; break;
158 case X86II::MO_TPOFF: RefKind = MCSymbolRefExpr::VK_TPOFF; break;
159 case X86II::MO_NTPOFF: RefKind = MCSymbolRefExpr::VK_NTPOFF; break;
160 case X86II::MO_GOTPCREL: RefKind = MCSymbolRefExpr::VK_GOTPCREL; break;
161 case X86II::MO_GOT: RefKind = MCSymbolRefExpr::VK_GOT; break;
162 case X86II::MO_GOTOFF: RefKind = MCSymbolRefExpr::VK_GOTOFF; break;
163 case X86II::MO_PLT: RefKind = MCSymbolRefExpr::VK_PLT; break;
164 case X86II::MO_PIC_BASE_OFFSET:
165 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
166 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
167 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
168 // Subtract the pic base.
169 Expr = MCBinaryExpr::CreateSub(Expr,
170 MCSymbolRefExpr::Create(GetPICBaseSymbol(), Ctx),
176 Expr = MCSymbolRefExpr::Create(Sym, RefKind, Ctx);
178 if (!MO.isJTI() && MO.getOffset())
179 Expr = MCBinaryExpr::CreateAdd(Expr,
180 MCConstantExpr::Create(MO.getOffset(), Ctx),
182 return MCOperand::CreateExpr(Expr);
187 static void lower_subreg32(MCInst *MI, unsigned OpNo) {
188 // Convert registers in the addr mode according to subreg32.
189 unsigned Reg = MI->getOperand(OpNo).getReg();
191 MI->getOperand(OpNo).setReg(getX86SubSuperRegister(Reg, MVT::i32));
194 static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
195 // Convert registers in the addr mode according to subreg64.
196 for (unsigned i = 0; i != 4; ++i) {
197 if (!MI->getOperand(OpNo+i).isReg()) continue;
199 unsigned Reg = MI->getOperand(OpNo+i).getReg();
200 if (Reg == 0) continue;
202 MI->getOperand(OpNo+i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
206 /// LowerSubReg32_Op0 - Things like MOVZX16rr8 -> MOVZX32rr8.
207 static void LowerSubReg32_Op0(MCInst &OutMI, unsigned NewOpc) {
208 OutMI.setOpcode(NewOpc);
209 lower_subreg32(&OutMI, 0);
211 /// LowerUnaryToTwoAddr - R = setb -> R = sbb R, R
212 static void LowerUnaryToTwoAddr(MCInst &OutMI, unsigned NewOpc) {
213 OutMI.setOpcode(NewOpc);
214 OutMI.addOperand(OutMI.getOperand(0));
215 OutMI.addOperand(OutMI.getOperand(0));
219 void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
220 OutMI.setOpcode(MI->getOpcode());
222 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
223 const MachineOperand &MO = MI->getOperand(i);
226 switch (MO.getType()) {
229 llvm_unreachable("unknown operand type");
230 case MachineOperand::MO_Register:
231 // Ignore all implicit register operands.
232 if (MO.isImplicit()) continue;
233 MCOp = MCOperand::CreateReg(MO.getReg());
235 case MachineOperand::MO_Immediate:
236 MCOp = MCOperand::CreateImm(MO.getImm());
238 case MachineOperand::MO_MachineBasicBlock:
239 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
240 MO.getMBB()->getSymbol(), Ctx));
242 case MachineOperand::MO_GlobalAddress:
243 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
245 case MachineOperand::MO_ExternalSymbol:
246 MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
248 case MachineOperand::MO_JumpTableIndex:
249 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()));
251 case MachineOperand::MO_ConstantPoolIndex:
252 MCOp = LowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex()));
254 case MachineOperand::MO_BlockAddress:
255 MCOp = LowerSymbolOperand(MO,
256 AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress()));
260 OutMI.addOperand(MCOp);
263 // Handle a few special cases to eliminate operand modifiers.
264 switch (OutMI.getOpcode()) {
265 case X86::LEA64_32r: // Handle 'subreg rewriting' for the lea64_32mem operand.
266 lower_lea64_32mem(&OutMI, 1);
268 case X86::MOVZX16rr8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr8); break;
269 case X86::MOVZX16rm8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm8); break;
270 case X86::MOVSX16rr8: LowerSubReg32_Op0(OutMI, X86::MOVSX32rr8); break;
271 case X86::MOVSX16rm8: LowerSubReg32_Op0(OutMI, X86::MOVSX32rm8); break;
272 case X86::MOVZX64rr32: LowerSubReg32_Op0(OutMI, X86::MOV32rr); break;
273 case X86::MOVZX64rm32: LowerSubReg32_Op0(OutMI, X86::MOV32rm); break;
274 case X86::MOV64ri64i32: LowerSubReg32_Op0(OutMI, X86::MOV32ri); break;
275 case X86::MOVZX64rr8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr8); break;
276 case X86::MOVZX64rm8: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm8); break;
277 case X86::MOVZX64rr16: LowerSubReg32_Op0(OutMI, X86::MOVZX32rr16); break;
278 case X86::MOVZX64rm16: LowerSubReg32_Op0(OutMI, X86::MOVZX32rm16); break;
279 case X86::SETB_C8r: LowerUnaryToTwoAddr(OutMI, X86::SBB8rr); break;
280 case X86::SETB_C16r: LowerUnaryToTwoAddr(OutMI, X86::SBB16rr); break;
281 case X86::SETB_C32r: LowerUnaryToTwoAddr(OutMI, X86::SBB32rr); break;
282 case X86::SETB_C64r: LowerUnaryToTwoAddr(OutMI, X86::SBB64rr); break;
283 case X86::MOV8r0: LowerUnaryToTwoAddr(OutMI, X86::XOR8rr); break;
284 case X86::MOV32r0: LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); break;
285 case X86::MMX_V_SET0: LowerUnaryToTwoAddr(OutMI, X86::MMX_PXORrr); break;
286 case X86::MMX_V_SETALLONES:
287 LowerUnaryToTwoAddr(OutMI, X86::MMX_PCMPEQDrr); break;
288 case X86::FsFLD0SS: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
289 case X86::FsFLD0SD: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
290 case X86::V_SET0PS: LowerUnaryToTwoAddr(OutMI, X86::XORPSrr); break;
291 case X86::V_SET0PD: LowerUnaryToTwoAddr(OutMI, X86::XORPDrr); break;
292 case X86::V_SET0PI: LowerUnaryToTwoAddr(OutMI, X86::PXORrr); break;
293 case X86::V_SETALLONES: LowerUnaryToTwoAddr(OutMI, X86::PCMPEQDrr); break;
296 LowerSubReg32_Op0(OutMI, X86::MOV32r0); // MOV16r0 -> MOV32r0
297 LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); // MOV32r0 -> XOR32rr
300 LowerSubReg32_Op0(OutMI, X86::MOV32r0); // MOV64r0 -> MOV32r0
301 LowerUnaryToTwoAddr(OutMI, X86::XOR32rr); // MOV32r0 -> XOR32rr
305 // The assembler backend wants to see branches in their small form and relax
306 // them to their large form. The JIT can only handle the large form because
307 // it does not do relaxation. For now, translate the large form to the
309 case X86::JMP_4: OutMI.setOpcode(X86::JMP_1); break;
310 case X86::JO_4: OutMI.setOpcode(X86::JO_1); break;
311 case X86::JNO_4: OutMI.setOpcode(X86::JNO_1); break;
312 case X86::JB_4: OutMI.setOpcode(X86::JB_1); break;
313 case X86::JAE_4: OutMI.setOpcode(X86::JAE_1); break;
314 case X86::JE_4: OutMI.setOpcode(X86::JE_1); break;
315 case X86::JNE_4: OutMI.setOpcode(X86::JNE_1); break;
316 case X86::JBE_4: OutMI.setOpcode(X86::JBE_1); break;
317 case X86::JA_4: OutMI.setOpcode(X86::JA_1); break;
318 case X86::JS_4: OutMI.setOpcode(X86::JS_1); break;
319 case X86::JNS_4: OutMI.setOpcode(X86::JNS_1); break;
320 case X86::JP_4: OutMI.setOpcode(X86::JP_1); break;
321 case X86::JNP_4: OutMI.setOpcode(X86::JNP_1); break;
322 case X86::JL_4: OutMI.setOpcode(X86::JL_1); break;
323 case X86::JGE_4: OutMI.setOpcode(X86::JGE_1); break;
324 case X86::JLE_4: OutMI.setOpcode(X86::JLE_1); break;
325 case X86::JG_4: OutMI.setOpcode(X86::JG_1); break;
329 void X86AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
331 // FIXME: if this is implemented for another target before it goes
332 // away completely, the common part should be moved into AsmPrinter.
333 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
334 unsigned NOps = MI->getNumOperands();
335 // cast away const; DIetc do not take const operands for some reason.
336 DIVariable V((MDNode*)(MI->getOperand(NOps-1).getMetadata()));
340 // Register or immediate value. Register 0 means undef.
341 assert(MI->getOperand(0).isReg() ||
342 MI->getOperand(0).isImm() ||
343 MI->getOperand(0).isFPImm());
344 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg() == 0) {
345 // Suppress offset in this case, it is not meaningful.
347 OutStreamer.AddBlankLine();
351 if (MI->getOperand(0).isFPImm()) {
352 // This is more naturally done in printOperand, but since the only use
353 // of such an operand is in this comment and that is temporary (and it's
354 // ugly), we prefer to keep this localized.
355 // The include of Type.h may be removable when this code is.
356 if (MI->getOperand(0).getFPImm()->getType()->isFloatTy() ||
357 MI->getOperand(0).getFPImm()->getType()->isDoubleTy())
358 MI->getOperand(0).print(O, &TM);
360 // There is no good way to print long double. Convert a copy to
361 // double. Ah well, it's only a comment.
363 APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
364 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
366 O << "(long double) " << APF.convertToDouble();
369 printOperand(MI, 0, O);
371 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg() == 0) {
372 // Suppress offset in this case, it is not meaningful.
374 OutStreamer.AddBlankLine();
377 // Frame address. Currently handles register +- offset only.
378 assert(MI->getOperand(0).isReg() && MI->getOperand(3).isImm());
379 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 3, O);
383 printOperand(MI, NOps-2, O);
387 void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
388 X86MCInstLower MCInstLowering(OutContext, Mang, *this);
389 switch (MI->getOpcode()) {
390 case TargetOpcode::DBG_VALUE:
391 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
393 raw_string_ostream OS(TmpStr);
394 PrintDebugValueComment(MI, OS);
395 OutStreamer.EmitRawText(StringRef(OS.str()));
399 case X86::MOVPC32r: {
401 // This is a pseudo op for a two instruction sequence with a label, which
408 MCSymbol *PICBase = MCInstLowering.GetPICBaseSymbol();
409 TmpInst.setOpcode(X86::CALLpcrel32);
410 // FIXME: We would like an efficient form for this, so we don't have to do a
411 // lot of extra uniquing.
412 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase,
414 OutStreamer.EmitInstruction(TmpInst);
417 OutStreamer.EmitLabel(PICBase);
420 TmpInst.setOpcode(X86::POP32r);
421 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
422 OutStreamer.EmitInstruction(TmpInst);
427 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
428 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
431 // Okay, we have something like:
432 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
434 // For this, we want to print something like:
435 // MYGLOBAL + (. - PICBASE)
436 // However, we can't generate a ".", so just emit a new label here and refer
438 MCSymbol *DotSym = OutContext.CreateTempSymbol();
439 OutStreamer.EmitLabel(DotSym);
441 // Now that we have emitted the label, lower the complex operand expression.
442 MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2));
444 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
445 const MCExpr *PICBase =
446 MCSymbolRefExpr::Create(MCInstLowering.GetPICBaseSymbol(), OutContext);
447 DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, OutContext);
449 DotExpr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym,OutContext),
450 DotExpr, OutContext);
453 TmpInst.setOpcode(X86::ADD32ri);
454 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
455 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
456 TmpInst.addOperand(MCOperand::CreateExpr(DotExpr));
457 OutStreamer.EmitInstruction(TmpInst);
463 MCInstLowering.Lower(MI, TmpInst);
465 OutStreamer.EmitInstruction(TmpInst);