[PATCH] X86: lower MO_GOT_ABSOLUTE_ADDRESS in LowerSymbolOperand like all the other MO_ types.

Larry D'Anna larry at elder-gods.org
Mon Jun 9 21:14:02 PDT 2014


It seems the only reason this was done in an instruction-specific hack in
EmitInstruction was because that's where we're able to emit a label for the
current instruction.

Refactored X86MCInstLower::Lower to pass the instruction to the MCStreamer
directly instead of returning it to caller.  This allows
X86MCInstLower::LowerSymbolOperand to emit the label it needs in order to lower
MO_GOT_ABSOLUTE_ADDRESS.

This also allows us to remove two related FIXMEs for checks that prevented
instructions with this kind of operand from getting folded.
---

 PATCH v2 changes: moved EmitLabel into LowerSymbolOperand so we do not need to
    scan operands for MO_GOT_ABSOLUTE_ADDRESS.
  
 lib/Target/X86/X86InstrInfo.cpp   | 10 ------
 lib/Target/X86/X86MCInstLower.cpp | 64 ++++++++++++++++-----------------------
 2 files changed, 26 insertions(+), 48 deletions(-)

diff --git a/lib/Target/X86/X86InstrInfo.cpp b/lib/Target/X86/X86InstrInfo.cpp
index 03bd544..95a9055 100644
--- a/lib/Target/X86/X86InstrInfo.cpp
+++ b/lib/Target/X86/X86InstrInfo.cpp
@@ -4090,12 +4090,6 @@ X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
   bool isTwoAddr = NumOps > 1 &&
     MI->getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1;
 
-  // FIXME: AsmPrinter doesn't know how to handle
-  // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
-  if (MI->getOpcode() == X86::ADD32ri &&
-      MI->getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
-    return nullptr;
-
   MachineInstr *NewMI = nullptr;
   // Folding a memory location into the two-address part of a two-address
   // instruction is different than folding it other places.  It requires
@@ -4537,10 +4531,6 @@ bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
     case X86::TEST64rr:
       return true;
     case X86::ADD32ri:
-      // FIXME: AsmPrinter doesn't know how to handle
-      // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
-      if (MI->getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
-        return false;
       break;
     }
   }
diff --git a/lib/Target/X86/X86MCInstLower.cpp b/lib/Target/X86/X86MCInstLower.cpp
index 0190080..cde2f54 100644
--- a/lib/Target/X86/X86MCInstLower.cpp
+++ b/lib/Target/X86/X86MCInstLower.cpp
@@ -43,7 +43,7 @@ class X86MCInstLower {
 public:
   X86MCInstLower(const MachineFunction &MF, X86AsmPrinter &asmprinter);
 
-  void Lower(const MachineInstr *MI, MCInst &OutMI) const;
+  void Lower(const MachineInstr *MI) const;
 
   MCSymbol *GetSymbolFromOperand(const MachineOperand &MO) const;
   MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
@@ -221,6 +221,26 @@ MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
       Expr = MCSymbolRefExpr::Create(Label, Ctx);
     }
     break;
+
+  case X86II::MO_GOT_ABSOLUTE_ADDRESS: {
+
+    // Okay, we have something like:
+    //  EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@_GLOBAL_OFFSET_TABLE_)
+
+    // For this, we want to print something like:
+    //   _GLOBAL_OFFSET_TABLE_ + (. - PICBASE)
+
+    MCSymbol *DotSym = Ctx.CreateTempSymbol();
+    AsmPrinter.OutStreamer.EmitLabel(DotSym);
+
+    Expr = MCSymbolRefExpr::Create(DotSym, Ctx);
+    const MCExpr *PICBase =
+      MCSymbolRefExpr::Create(MF.getPICBaseSymbol(), Ctx);
+    Expr = MCBinaryExpr::CreateSub(Expr, PICBase, Ctx);
+    Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym,Ctx),
+                                   Expr, Ctx);
+    break;
+  }
   }
 
   if (!Expr)
@@ -341,7 +361,8 @@ static unsigned getRetOpcode(const X86Subtarget &Subtarget)
 	return Subtarget.is64Bit() ? X86::RETQ : X86::RETL;
 }
 
-void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
+void X86MCInstLower::Lower(const MachineInstr *MI) const {
+  MCInst OutMI;
   OutMI.setOpcode(MI->getOpcode());
 
   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
@@ -599,6 +620,8 @@ ReSimplify:
     SimplifyMOVSX(OutMI);
     break;
   }
+
+  AsmPrinter.EmitToStreamer(AsmPrinter.OutStreamer, OutMI);
 }
 
 static void LowerTlsAddr(MCStreamer &OutStreamer,
@@ -833,39 +856,6 @@ void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
     return;
   }
 
-  case X86::ADD32ri: {
-    // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
-    if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
-      break;
-
-    // Okay, we have something like:
-    //  EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
-
-    // For this, we want to print something like:
-    //   MYGLOBAL + (. - PICBASE)
-    // However, we can't generate a ".", so just emit a new label here and refer
-    // to it.
-    MCSymbol *DotSym = OutContext.CreateTempSymbol();
-    OutStreamer.EmitLabel(DotSym);
-
-    // Now that we have emitted the label, lower the complex operand expression.
-    MCSymbol *OpSym = MCInstLowering.GetSymbolFromOperand(MI->getOperand(2));
-
-    const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
-    const MCExpr *PICBase =
-      MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), OutContext);
-    DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, OutContext);
-
-    DotExpr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym,OutContext),
-                                      DotExpr, OutContext);
-
-    EmitToStreamer(OutStreamer, MCInstBuilder(X86::ADD32ri)
-      .addReg(MI->getOperand(0).getReg())
-      .addReg(MI->getOperand(1).getReg())
-      .addExpr(DotExpr));
-    return;
-  }
-
   case TargetOpcode::STACKMAP:
     return LowerSTACKMAP(OutStreamer, SM, *MI, Subtarget->is64Bit(), getSubtargetInfo());
 
@@ -885,7 +875,5 @@ void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
     return;
   }
 
-  MCInst TmpInst;
-  MCInstLowering.Lower(MI, TmpInst);
-  EmitToStreamer(OutStreamer, TmpInst);
+  MCInstLowering.Lower(MI);
 }
-- 
1.8.3.2




More information about the llvm-commits mailing list