Refactor TLS code and add some tests. The tests and expected results are:
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 27 Feb 2009 13:37:18 +0000 (13:37 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 27 Feb 2009 13:37:18 +0000 (13:37 +0000)
 pic |  declaration | linkage  | visibility |

!pic |  declaration | external | default    | tls1.ll     tls2.ll     | local exec
 pic |  declaration | external | default    | tls1-pic.ll tls2-pic.ll | general dynamic
!pic | !declaration | external | default    | tls3.ll     tls4.ll     | initial exec
 pic | !declaration | external | default    | tls3-pic.ll tls4-pic.ll | general dynamic

!pic |  declaration | external | hidden     | tls7.ll     tls8.ll     | local exec
 pic |  declaration | external | hidden     | X                       | local dynamic
!pic | !declaration | external | hidden     | tls9.ll     tls10.ll    | local exec
 pic | !declaration | external | hidden     | X                       | local dynamic

!pic |  declaration | internal | default    | tls5.ll     tls6.ll     | local exec
 pic |  declaration | internal | default    | X                       | local dynamic

The ones marked with an X have not been implemented since local dynamic is not implemented.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65632 91177308-0d34-0410-b5e6-96231b3b80d8

18 files changed:
include/llvm/Target/TargetLowering.h
lib/CodeGen/SelectionDAG/TargetLowering.cpp
lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp
lib/Target/X86/X86ISelLowering.cpp
test/CodeGen/X86/tls1-pic.ll [new file with mode: 0644]
test/CodeGen/X86/tls1.ll
test/CodeGen/X86/tls10.ll [new file with mode: 0644]
test/CodeGen/X86/tls2-pic.ll [new file with mode: 0644]
test/CodeGen/X86/tls2.ll
test/CodeGen/X86/tls3-pic.ll [new file with mode: 0644]
test/CodeGen/X86/tls3.ll [new file with mode: 0644]
test/CodeGen/X86/tls4-pic.ll [new file with mode: 0644]
test/CodeGen/X86/tls4.ll [new file with mode: 0644]
test/CodeGen/X86/tls5.ll [new file with mode: 0644]
test/CodeGen/X86/tls6.ll [new file with mode: 0644]
test/CodeGen/X86/tls7.ll [new file with mode: 0644]
test/CodeGen/X86/tls8.ll [new file with mode: 0644]
test/CodeGen/X86/tls9.ll [new file with mode: 0644]

index 2247b3625ed753063ba4564e66aa0a9cc6b1321b..8ec7f7a75f14e776932d4b0aac88cf406bf4b55f 100644 (file)
@@ -30,6 +30,7 @@
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/CodeGen/DebugLoc.h"
+#include "llvm/Target/TargetMachine.h"
 #include <climits>
 #include <map>
 #include <vector>
@@ -54,6 +55,18 @@ namespace llvm {
   class TargetSubtarget;
   class Value;
 
+  // FIXME: should this be here?
+  namespace TLSModel {
+    enum Model {
+      GeneralDynamic,
+      LocalDynamic,
+      InitialExec,
+      LocalExec
+    };
+  }
+  TLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc);
+
+
 //===----------------------------------------------------------------------===//
 /// TargetLowering - This class defines information used to lower LLVM code to
 /// legal SelectionDAG operators that the target instruction selector can accept
index 3f56febafa302429ab488c71f970214a9e5bf5dc..e92932b186f2c46d543cd769781ba9ed7d2719f8 100644 (file)
 #include "llvm/Support/MathExtras.h"
 using namespace llvm;
 
+namespace llvm {
+TLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc) {
+  bool isLocal = GV->hasLocalLinkage();
+  bool isDeclaration = GV->isDeclaration();
+  // FIXME: what should we do for protected and internal visibility?
+  // For variables, is internal different from hidden?
+  bool isHidden = GV->hasHiddenVisibility();
+
+  if (reloc == Reloc::PIC_) {
+    if (isLocal || isHidden)
+      return TLSModel::LocalDynamic;
+    else
+      return TLSModel::GeneralDynamic;
+  } else {
+    if (!isDeclaration || isHidden)
+      return TLSModel::LocalExec;
+    else
+      return TLSModel::InitialExec;
+  }
+}
+}
+
 /// InitLibcallNames - Set default libcall names.
 ///
 static void InitLibcallNames(const char **Names) {
index ca1d7385ade3af695777ee7ce4664ad4e898200d..e0bd6a3e34487bc61d741c148e65bd6e88e7e7e9 100644 (file)
@@ -439,13 +439,30 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
     printOffset(MO.getOffset());
 
     if (isThreadLocal) {
-      if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
-        O << "@TLSGD"; // general dynamic TLS model
-      else
-        if (GV->isDeclaration())
-          O << "@INDNTPOFF"; // initial exec TLS model
+      TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
+      switch (model) {
+      case TLSModel::GeneralDynamic:
+        O << "@TLSGD";
+        break;
+      case TLSModel::LocalDynamic:
+        // O << "@TLSLD"; // local dynamic not implemented
+       O << "@TLSGD";
+        break;
+      case TLSModel::InitialExec:
+        if (Subtarget->is64Bit())
+          O << "@TLSGD"; // 64 bit intial exec not implemented
         else
-          O << "@NTPOFF"; // local exec TLS model
+          O << "@INDNTPOFF";
+        break;
+      case TLSModel::LocalExec:
+        if (Subtarget->is64Bit())
+          O << "@TLSGD"; // 64 bit local exec not implemented
+        else
+         O << "@NTPOFF";
+        break;
+      default:
+        assert (0 && "Unknown TLS model");
+      }
     } else if (isMemOp) {
       if (shouldPrintGOT(TM, Subtarget)) {
         if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
index d18c283c4fba01b9d7976adb6811a2834799169c..c3a29b2a43a18b52abb99aabef5a25fafe945e68 100644 (file)
@@ -4795,7 +4795,7 @@ LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
 // "local exec" model.
 static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
-                                     const MVT PtrVT) {
+                                   const MVT PtrVT, TLSModel::Model model) {
   DebugLoc dl = GA->getDebugLoc();
   // Get the Thread Pointer
   SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER,
@@ -4807,7 +4807,7 @@ static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
                                              GA->getOffset());
   SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
 
-  if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
+  if (model == TLSModel::InitialExec)
     Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
                          PseudoSourceValue::getGOT(), 0);
 
@@ -4823,15 +4823,31 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
   assert(Subtarget->isTargetELF() &&
          "TLS not implemented for non-ELF targets");
   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
-  // If the relocation model is PIC, use the "General Dynamic" TLS Model,
-  // otherwise use the "Local Exec"TLS Model
+  GlobalValue *GV = GA->getGlobal();
+  TLSModel::Model model =
+    getTLSModel (GV, getTargetMachine().getRelocationModel());
   if (Subtarget->is64Bit()) {
-    return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
+    switch (model) {
+    case TLSModel::GeneralDynamic:
+    case TLSModel::LocalDynamic: // not implemented
+    case TLSModel::InitialExec:  // not implemented
+    case TLSModel::LocalExec:    // not implemented
+      return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
+    default:
+      assert (0 && "Unknown TLS model");
+    }
   } else {
-    if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
+    switch (model) {
+    case TLSModel::GeneralDynamic:
+    case TLSModel::LocalDynamic: // not implemented
       return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
-    else
-      return LowerToTLSExecModel(GA, DAG, getPointerTy());
+
+    case TLSModel::InitialExec:
+    case TLSModel::LocalExec:
+      return LowerToTLSExecModel(GA, DAG, getPointerTy(), model);
+    default:
+      assert (0 && "Unknown TLS model");
+    }
   }
 }
 
diff --git a/test/CodeGen/X86/tls1-pic.ll b/test/CodeGen/X86/tls1-pic.ll
new file mode 100644 (file)
index 0000000..0674f79
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic > %t
+; RUN: grep {leal      i@TLSGD(,%ebx,1), %eax} %t
+; RUN: grep {call      ___tls_get_addr@PLT} %t
+
+@i = thread_local global i32 15
+
+define i32 @f() {
+entry:
+       %tmp1 = load i32* @i
+       ret i32 %tmp1
+}
index c4025034bced7fa3b249bf1e06260fb4e7e852d1..ccff7180eb00134d0a409a401c55f5d0a66be65c 100644 (file)
@@ -1,19 +1,10 @@
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu | \
-; RUN:     grep {movl  %gs:i@NTPOFF, %eax}
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu | \
-; RUN:     grep {leal  i@NTPOFF(%eax), %eax}
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic | \
-; RUN:     grep {leal  i@TLSGD(,%ebx,1), %eax}
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:i@NTPOFF, %eax} %t
 
-@i = thread_local global i32 15                ; <i32*> [#uses=2]
+@i = thread_local global i32 15
 
 define i32 @f() {
 entry:
-       %tmp1 = load i32* @i            ; <i32> [#uses=1]
+       %tmp1 = load i32* @i
        ret i32 %tmp1
 }
-
-define i32* @g() {
-entry:
-       ret i32* @i
-}
diff --git a/test/CodeGen/X86/tls10.ll b/test/CodeGen/X86/tls10.ll
new file mode 100644 (file)
index 0000000..a4f2fb1
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
+
+@i = external hidden thread_local global i32
+
+define i32* @f() {
+entry:
+       ret i32* @i
+}
diff --git a/test/CodeGen/X86/tls2-pic.ll b/test/CodeGen/X86/tls2-pic.ll
new file mode 100644 (file)
index 0000000..0ecb673
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic > %t
+; RUN: grep {leal      i@TLSGD(,%ebx,1), %eax} %t
+; RUN: grep {call      ___tls_get_addr@PLT} %t
+
+@i = thread_local global i32 15
+
+define i32* @f() {
+entry:
+       ret i32* @i
+}
index c9b73fba8c7d5ce2ba75ca3da08c9a3d3ddd8306..fb57ae1a3917073ac35ec4864e0a8b3b29600ae1 100644 (file)
@@ -1,19 +1,10 @@
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu | \
-; RUN:     grep {movl  %gs:(%eax), %eax}
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu | \
-; RUN:     grep {addl  i@INDNTPOFF, %eax}
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic | \
-; RUN:     grep {leal  i@TLSGD(,%ebx,1), %eax}
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
 
-@i = external thread_local global i32          ; <i32*> [#uses=2]
+@i = thread_local global i32 15
 
-define i32 @f() {
-entry:
-       %tmp1 = load i32* @i            ; <i32> [#uses=1]
-       ret i32 %tmp1
-}
-
-define i32* @g() {
+define i32* @f() {
 entry:
        ret i32* @i
 }
diff --git a/test/CodeGen/X86/tls3-pic.ll b/test/CodeGen/X86/tls3-pic.ll
new file mode 100644 (file)
index 0000000..80fad48
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic  > %t
+; RUN: grep {leal      i@TLSGD(,%ebx,1), %eax} %t
+; RUN: grep {call      ___tls_get_addr@PLT} %t
+
+@i = external thread_local global i32          ; <i32*> [#uses=2]
+
+define i32 @f() {
+entry:
+       %tmp1 = load i32* @i            ; <i32> [#uses=1]
+       ret i32 %tmp1
+}
diff --git a/test/CodeGen/X86/tls3.ll b/test/CodeGen/X86/tls3.ll
new file mode 100644 (file)
index 0000000..396e790
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      i@INDNTPOFF, %eax} %t
+; RUN: grep {movl      %gs:(%eax), %eax} %t
+
+@i = external thread_local global i32          ; <i32*> [#uses=2]
+
+define i32 @f() {
+entry:
+       %tmp1 = load i32* @i            ; <i32> [#uses=1]
+       ret i32 %tmp1
+}
diff --git a/test/CodeGen/X86/tls4-pic.ll b/test/CodeGen/X86/tls4-pic.ll
new file mode 100644 (file)
index 0000000..135d746
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic > %t
+; RUN: grep {leal      i@TLSGD(,%ebx,1), %eax} %t
+; RUN: grep {call      ___tls_get_addr@PLT} %t
+
+@i = external thread_local global i32          ; <i32*> [#uses=2]
+
+define i32* @f() {
+entry:
+       ret i32* @i
+}
diff --git a/test/CodeGen/X86/tls4.ll b/test/CodeGen/X86/tls4.ll
new file mode 100644 (file)
index 0000000..3146699
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {addl      i@INDNTPOFF, %eax} %t
+
+@i = external thread_local global i32          ; <i32*> [#uses=2]
+
+define i32* @f() {
+entry:
+       ret i32* @i
+}
diff --git a/test/CodeGen/X86/tls5.ll b/test/CodeGen/X86/tls5.ll
new file mode 100644 (file)
index 0000000..b32f5fc
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:i@NTPOFF, %eax} %t
+
+@i = internal thread_local global i32 15
+
+define i32 @f() {
+entry:
+       %tmp1 = load i32* @i
+       ret i32 %tmp1
+}
diff --git a/test/CodeGen/X86/tls6.ll b/test/CodeGen/X86/tls6.ll
new file mode 100644 (file)
index 0000000..e0bcade
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
+
+@i = internal thread_local global i32 15
+
+define i32* @f() {
+entry:
+       ret i32* @i
+}
diff --git a/test/CodeGen/X86/tls7.ll b/test/CodeGen/X86/tls7.ll
new file mode 100644 (file)
index 0000000..eafa5c2
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:i@NTPOFF, %eax} %t
+
+@i = hidden thread_local global i32 15
+
+define i32 @f() {
+entry:
+       %tmp1 = load i32* @i
+       ret i32 %tmp1
+}
diff --git a/test/CodeGen/X86/tls8.ll b/test/CodeGen/X86/tls8.ll
new file mode 100644 (file)
index 0000000..4971fd2
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
+
+@i = hidden thread_local global i32 15
+
+define i32* @f() {
+entry:
+       ret i32* @i
+}
diff --git a/test/CodeGen/X86/tls9.ll b/test/CodeGen/X86/tls9.ll
new file mode 100644 (file)
index 0000000..31dffcc
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:i@NTPOFF, %eax} %t
+
+@i = external hidden thread_local global i32
+
+define i32 @f() {
+entry:
+       %tmp1 = load i32* @i
+       ret i32 %tmp1
+}