No subject

Larry D'Anna larry at elder-gods.org
Mon Jun 2 12:14:30 PDT 2014


>From a2c3b1d88ce4e240f0e23fee38314c04500b3523 Mon Sep 17 00:00:00 2001
Message-Id: <a2c3b1d88ce4e240f0e23fee38314c04500b3523.1401671195.git.larry at elder-gods.org>
From: Larry D'Anna <larry at elder-gods.org>
Date: Sun, 1 Jun 2014 18:34:49 -0400
Subject: [PATCH] X86: lower MO_GOT_ABSOLUTE_ADDRESS in LowerSymbolOperand like
 all the other MO_ types.

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.  A less ugly hack is to let EmitInstruction take care of
the label, but lower the operand in LowerSymbolOperand as normal, using a label
passed in as an argument.

This also allows us to remove two related FIXMEs for checks that prevented
instructions with this kind of operand from getting folded.
---
 lib/Target/X86/X86InstrInfo.cpp   | 10 -----
 lib/Target/X86/X86MCInstLower.cpp | 79 ++++++++++++++++++++-------------------
 2 files changed, 40 insertions(+), 49 deletions(-)

diff --git a/lib/Target/X86/X86InstrInfo.cpp b/lib/Target/X86/X86InstrInfo.cpp
index 6993577..bacd1e0 100644
--- a/lib/Target/X86/X86InstrInfo.cpp
+++ b/lib/Target/X86/X86InstrInfo.cpp
@@ -4089,12 +4089,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
@@ -4536,10 +4530,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..6688b77 100644
--- a/lib/Target/X86/X86MCInstLower.cpp
+++ b/lib/Target/X86/X86MCInstLower.cpp
@@ -43,10 +43,12 @@ class X86MCInstLower {
 public:
   X86MCInstLower(const MachineFunction &MF, X86AsmPrinter &asmprinter);
 
-  void Lower(const MachineInstr *MI, MCInst &OutMI) const;
+  void Lower(const MachineInstr *MI, MCInst &OutMI,
+             MCSymbol *DotSym = NULL) const;
 
   MCSymbol *GetSymbolFromOperand(const MachineOperand &MO) const;
-  MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
+  MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
+                               MCSymbol *DotSym = NULL) const;
 
 private:
   MachineModuleInfoMachO &getMachOMMI() const;
@@ -165,7 +167,8 @@ GetSymbolFromOperand(const MachineOperand &MO) const {
 }
 
 MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
-                                             MCSymbol *Sym) const {
+                                             MCSymbol *Sym,
+                                             MCSymbol *DotSym) const {
   // FIXME: We would like an efficient form for this, so we don't have to do a
   // lot of extra uniquing.
   const MCExpr *Expr = nullptr;
@@ -221,6 +224,25 @@ MCOperand X86MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
       Expr = MCSymbolRefExpr::Create(Label, Ctx);
     }
     break;
+
+  case X86II::MO_GOT_ABSOLUTE_ADDRESS: {
+    assert(DotSym != NULL);
+
+    // 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)
+
+    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 +363,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, MCInst &OutMI,
+                           MCSymbol *DotSym) const {
   OutMI.setOpcode(MI->getOpcode());
 
   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
@@ -363,7 +386,7 @@ void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
     case MachineOperand::MO_MachineBasicBlock:
     case MachineOperand::MO_GlobalAddress:
     case MachineOperand::MO_ExternalSymbol:
-      MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
+      MCOp = LowerSymbolOperand(MO, GetSymbolFromOperand(MO), DotSym);
       break;
     case MachineOperand::MO_JumpTableIndex:
       MCOp = LowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()));
@@ -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,18 @@ void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
     return;
   }
 
+  // Pre-emit a label to the current instruction if one of the operands is going
+  // to need it.
+  MCSymbol *DotSym = NULL;
+  for (unsigned i = 0; i < MI->getNumOperands(); i++) {
+    if (MI->getOperand(i).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS) {
+      DotSym = OutContext.CreateTempSymbol();
+      OutStreamer.EmitLabel(DotSym);
+      break;
+    }
+  }
+
   MCInst TmpInst;
-  MCInstLowering.Lower(MI, TmpInst);
+  MCInstLowering.Lower(MI, TmpInst, DotSym);
   EmitToStreamer(OutStreamer, TmpInst);
 }
-- 
1.8.3.2




More information about the llvm-commits mailing list