Move the GlobalBaseReg field out of X86ISelDAGToDAG.cpp
and X86FastISel.cpp into X86MachineFunction.h, so that it
can be shared, instead of having each selector keep track
of its own.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56825 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/X86/X86InstrInfo.cpp b/lib/Target/X86/X86InstrInfo.cpp
index c275418..cac35b1 100644
--- a/lib/Target/X86/X86InstrInfo.cpp
+++ b/lib/Target/X86/X86InstrInfo.cpp
@@ -2947,10 +2947,19 @@
   return Size;
 }
 
-/// initializeGlobalBaseReg - Output the instructions required to put the
-/// base address to use for accessing globals into a register.
+/// getGlobalBaseReg - Return a virtual register initialized with the
+/// the global base register value. Output instructions required to
+/// initialize the register in the function entry block, if necessary.
 ///
-unsigned X86InstrInfo::initializeGlobalBaseReg(MachineFunction *MF) const {
+unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
+  assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
+         "X86-64 PIC uses RIP relative addressing");
+
+  X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
+  unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
+  if (GlobalBaseReg != 0)
+    return GlobalBaseReg;
+
   // Insert the set of GlobalBaseReg into the first MBB of the function
   MachineBasicBlock &FirstMBB = MF->front();
   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
@@ -2966,12 +2975,14 @@
   // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
   if (TM.getRelocationModel() == Reloc::PIC_ &&
       TM.getSubtarget<X86Subtarget>().isPICStyleGOT()) {
-    unsigned GlobalBaseReg =
+    GlobalBaseReg =
       RegInfo.createVirtualRegister(X86::GR32RegisterClass);
     BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
       .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
-    return GlobalBaseReg;
+  } else {
+    GlobalBaseReg = PC;
   }
 
-  return PC;
+  X86FI->setGlobalBaseReg(GlobalBaseReg);
+  return GlobalBaseReg;
 }