Index: lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- lib/Target/X86/X86ISelLowering.cpp	(Revision 145790)
+++ lib/Target/X86/X86ISelLowering.cpp	(Arbeitskopie)
@@ -7571,6 +7571,41 @@
     unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
     return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
                               Chain.getValue(1));
+  } else if (Subtarget->isTargetWindows()) {
+    // Just use the implicit TLS architecture
+    // Need to generate someting similar to:
+    //   mov     rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage from TEB
+    //   mov     ecx, dword [rel _tls_index]: Load index (from C runtime)
+    //   mov     rcx, qword [rdx+rcx*8]
+    //   mov     eax, .tls$:tlsvar
+    //   [rax+rcx] contains the address
+    // Windows 64bit: gs:0x58
+    // Windows 32bit: fs:0x2C
+
+    // If GV is an alias then use the aliasee for determining
+    // thread-localness.
+    if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
+      GV = GA->resolveAliasedGlobal(false);
+    DebugLoc dl = GA->getDebugLoc();
+
+    // Get the Thread Pointer, which is %fs:0x2C (32-bit) or %gs:0x58 (64-bit).
+    Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
+                                                         Subtarget->is64Bit() ? 256 : 257));
+
+    SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
+                                        DAG.getIntPtrConstant(Subtarget->is64Bit() ? 0x58 : 0x2C),
+                                        MachinePointerInfo(Ptr),
+                                        false, false, false, 0);
+
+	// Get the offset of start of .tls section
+    SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
+                                             GA->getValueType(0),
+                                             GA->getOffset(), X86II::MO_TLSOFF);
+    SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
+
+    // The address of the thread local variable is the add of the thread
+    // pointer with the offset of the variable.
+    return DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, Offset);
   }
 
   assert(false &&
Index: lib/Target/X86/X86MCInstLower.cpp
===================================================================
--- lib/Target/X86/X86MCInstLower.cpp	(Revision 145790)
+++ lib/Target/X86/X86MCInstLower.cpp	(Arbeitskopie)
@@ -154,6 +154,7 @@
                                                            Ctx),
                                    Ctx);
     break;
+  case X86II::MO_TLSOFF:    RefKind = MCSymbolRefExpr::VK_TLSOFF; break;
   case X86II::MO_TLSGD:     RefKind = MCSymbolRefExpr::VK_TLSGD; break;
   case X86II::MO_GOTTPOFF:  RefKind = MCSymbolRefExpr::VK_GOTTPOFF; break;
   case X86II::MO_INDNTPOFF: RefKind = MCSymbolRefExpr::VK_INDNTPOFF; break;
Index: lib/Target/X86/MCTargetDesc/X86BaseInfo.h
===================================================================
--- lib/Target/X86/MCTargetDesc/X86BaseInfo.h	(Revision 145790)
+++ lib/Target/X86/MCTargetDesc/X86BaseInfo.h	(Arbeitskopie)
@@ -164,7 +164,13 @@
     /// is some TLS offset from the picbase.
     ///
     /// This is the 32-bit TLS offset for Darwin TLS in PIC mode.
-    MO_TLVP_PIC_BASE
+    MO_TLVP_PIC_BASE,
+
+    /// MO_TLSOFF - On a symbol operand this indicates that the immediate is
+    /// some TLS offset.
+    ///
+    /// This is the TLS offset for the COFF/Windows TLS mechanism.
+    MO_TLSOFF
   };
 
   enum {
Index: include/llvm/MC/MCExpr.h
===================================================================
--- include/llvm/MC/MCExpr.h	(Revision 145790)
+++ include/llvm/MC/MCExpr.h	(Arbeitskopie)
@@ -162,6 +162,7 @@
     VK_TPOFF,
     VK_DTPOFF,
     VK_TLVP,      // Mach-O thread local variable relocation
+    VK_TLSOFF,
     // FIXME: We'd really like to use the generic Kinds listed above for these.
     VK_ARM_PLT,   // ARM-style PLT references. i.e., (PLT) instead of @PLT
     VK_ARM_TLSGD, //   ditto for TLSGD, GOT, GOTOFF, TPOFF and GOTTPOFF
