- Added option -relocation-model to set relocation model. Valid values include static, pic,
dynamic-no-pic, and default.
PPC and x86 default is dynamic-no-pic for Darwin, pic for others.
- Removed options -enable-pic and -ppc-static.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26315 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/X86/X86ATTAsmPrinter.cpp b/lib/Target/X86/X86ATTAsmPrinter.cpp
index 9b43873..f1774fd 100755
--- a/lib/Target/X86/X86ATTAsmPrinter.cpp
+++ b/lib/Target/X86/X86ATTAsmPrinter.cpp
@@ -119,7 +119,7 @@
     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
     if (!isMemOp && !isCallOp) O << '$';
     // Darwin block shameless ripped from PPCAsmPrinter.cpp
-    if (forDarwin) {
+    if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
       GlobalValue *GV = MO.getGlobal();
       std::string Name = Mang->getValueName(GV);
       // Link-once, External, or Weakly-linked global variables need
@@ -133,7 +133,7 @@
         } else {
           GVStubs.insert(Name);
           O << "L" << Name << "$non_lazy_ptr";
-          if (PICEnabled)
+          if (TM.getRelocationModel() == Reloc::PIC)
             O << "-\"L" << getFunctionNumber() << "$pb\"";
         }
       } else {
@@ -150,14 +150,14 @@
   }
   case MachineOperand::MO_ExternalSymbol: {
     bool isCallOp = Modifier && !strcmp(Modifier, "call");
-    bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
-    if (isCallOp && forDarwin) {
-      std::string Name(GlobalPrefix); Name += MO.getSymbolName();
+    if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
+      std::string Name(GlobalPrefix);
+      Name += MO.getSymbolName();
       FnStubs.insert(Name);
       O << "L" << Name << "$stub";
       return;
     }
-    if (!isMemOp && !isCallOp) O << '$';
+    if (!isCallOp) O << '$';
     O << GlobalPrefix << MO.getSymbolName();
     return;
   }
@@ -198,7 +198,7 @@
   } else if (BaseReg.isConstantPoolIndex()) {
     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
       << BaseReg.getConstantPoolIndex();
-    if (forDarwin && PICEnabled)
+    if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
       O << "-\"L" << getFunctionNumber() << "$pb\"";
     if (DispSpec.getImmedValue())
       O << "+" << DispSpec.getImmedValue();
diff --git a/lib/Target/X86/X86CodeEmitter.cpp b/lib/Target/X86/X86CodeEmitter.cpp
index 0c0ed53..772f1ab 100644
--- a/lib/Target/X86/X86CodeEmitter.cpp
+++ b/lib/Target/X86/X86CodeEmitter.cpp
@@ -77,6 +77,9 @@
 }
 
 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
+  assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
+          MF.getTarget().getRelocationModel() != Reloc::Static) &&
+         "JIT relocation model must be set to static or default!");
   II = ((X86TargetMachine&)MF.getTarget()).getInstrInfo();
 
   MCE.startFunction(MF);
@@ -97,7 +100,6 @@
 }
 
 void Emitter::emitBasicBlock(const MachineBasicBlock &MBB) {
-  assert(!PICEnabled && "CodeEmitter does not support PIC!");
   if (uint64_t Addr = MCE.getCurrentPCValue())
     BasicBlockAddrs[&MBB] = Addr;
 
diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp
index 77fc52b..105e6ce 100644
--- a/lib/Target/X86/X86ISelLowering.cpp
+++ b/lib/Target/X86/X86ISelLowering.cpp
@@ -1836,7 +1836,7 @@
     if (getTargetMachine().
         getSubtarget<X86Subtarget>().isTargetDarwin()) {
       // With PIC, the address is actually $g + Offset.
-      if (PICEnabled)
+      if (getTargetMachine().getRelocationModel() == Reloc::PIC)
         Result = DAG.getNode(ISD::ADD, getPointerTy(),
                 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);    
     }
@@ -1851,7 +1851,7 @@
       GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
       SDOperand Addr = DAG.getTargetGlobalAddress(GV, getPointerTy());
       // With PIC, the address is actually $g + Offset.
-      if (PICEnabled)
+      if (getTargetMachine().getRelocationModel() == Reloc::PIC)
         Addr = DAG.getNode(ISD::ADD, getPointerTy(),
                       DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Addr);
 
@@ -1859,8 +1859,9 @@
       // the value at address GV, not the value of GV itself.  This means that
       // the GlobalAddress must be in the base or index register of the address,
       // not the GV offset field.
-      if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
-          (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
+      if (getTargetMachine().getRelocationModel() != Reloc::Static &&
+          (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
+           (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
         Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
                              Addr, DAG.getSrcValue(NULL));
     }
diff --git a/lib/Target/X86/X86IntelAsmPrinter.cpp b/lib/Target/X86/X86IntelAsmPrinter.cpp
index 90fe3ac..1a5aba1 100755
--- a/lib/Target/X86/X86IntelAsmPrinter.cpp
+++ b/lib/Target/X86/X86IntelAsmPrinter.cpp
@@ -113,7 +113,7 @@
     bool isCallOp = Modifier && !strcmp(Modifier, "call");
     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
     if (!isMemOp && !isCallOp) O << "OFFSET ";
-    if (forDarwin) {
+    if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
       GlobalValue *GV = MO.getGlobal();
       std::string Name = Mang->getValueName(GV);
       if (!isMemOp && !isCallOp) O << '$';
@@ -128,7 +128,7 @@
         } else {
           GVStubs.insert(Name);
           O << "L" << Name << "$non_lazy_ptr";
-          if (PICEnabled)
+          if (TM.getRelocationModel() == Reloc::PIC)
             O << "-\"L" << getFunctionNumber() << "$pb\"";
         }
       } else {
@@ -145,13 +145,14 @@
   }
   case MachineOperand::MO_ExternalSymbol: {
     bool isCallOp = Modifier && !strcmp(Modifier, "call");
-    bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
-    if (isCallOp && forDarwin) {
-      std::string Name(GlobalPrefix); Name += MO.getSymbolName();
+    if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
+      std::string Name(GlobalPrefix);
+      Name += MO.getSymbolName();
       FnStubs.insert(Name);
       O << "L" << Name << "$stub";
       return;
     }
+    if (!isCallOp) O << "OFFSET ";
     O << GlobalPrefix << MO.getSymbolName();
     return;
   }
@@ -177,7 +178,7 @@
   } else if (BaseReg.isConstantPoolIndex()) {
     O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
       << BaseReg.getConstantPoolIndex();
-    if (forDarwin && PICEnabled)
+    if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
       O << "-\"L" << getFunctionNumber() << "$pb\"";
 
     if (IndexReg.getReg()) {
diff --git a/lib/Target/X86/X86TargetMachine.cpp b/lib/Target/X86/X86TargetMachine.cpp
index f24e1b0..8adce31 100644
--- a/lib/Target/X86/X86TargetMachine.cpp
+++ b/lib/Target/X86/X86TargetMachine.cpp
@@ -76,6 +76,11 @@
     FrameInfo(TargetFrameInfo::StackGrowsDown,
               Subtarget.getStackAlignment(), -4),
     JITInfo(*this) {
+  if (getRelocationModel() == Reloc::Default)
+    if (Subtarget.isTargetDarwin())
+      setRelocationModel(Reloc::DynamicNoPIC);
+    else
+      setRelocationModel(Reloc::PIC);
 }
 
 
@@ -149,8 +154,8 @@
 /// not supported for this target.
 ///
 void X86JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
-  // The JIT does not support or need PIC.
-  PICEnabled = false;
+  // The JIT should use static relocation model.
+  TM.setRelocationModel(Reloc::Static);
 
   // FIXME: Implement efficient support for garbage collection intrinsics.
   PM.add(createLowerGCPass());