Represent runtime preemption in the IR.

Currently we do not represent runtime preemption in the IR, which has several
drawbacks:

  1) The semantics of GlobalValues differ depending on the object file format
     you are targeting (as well as the relocation-model and -fPIE value).
  2) We have no way of disabling inlining of run time interposable functions,
     since in the IR we only know if a function is link-time interposable.
     Because of this llvm cannot support elf-interposition semantics.
  3) In LTO builds of executables we will have extra knowledge that a symbol
     resolved to a local definition and can't be preemptable, but have no way to
     propagate that knowledge through the compiler.

This patch adds preemptability specifiers to the IR with the following meaning:

dso_local --> means the compiler may assume the symbol will resolve to a
 definition within the current linkage unit and the symbol may be accessed
 directly even if the definition is not within this compilation unit.

dso_preemptable --> means that the compiler must assume the GlobalValue may be
replaced with a definition from outside the current linkage unit at runtime.

To ease transitioning dso_preemptable is treated as a 'default' in that
low-level codegen will still do the same checks it did previously to see if a
symbol should be accessed indirectly. Eventually when IR producers emit the
specifiers on all Globalvalues we can change dso_preemptable to mean 'always
access indirectly', and remove the current logic.

Differential Revision: https://reviews.llvm.org/D20217

llvm-svn: 316668
diff --git a/llvm/test/CodeGen/PowerPC/preemption.ll b/llvm/test/CodeGen/PowerPC/preemption.ll
new file mode 100644
index 0000000..5652f6d
--- /dev/null
+++ b/llvm/test/CodeGen/PowerPC/preemption.ll
@@ -0,0 +1,301 @@
+; RUN: llc -mtriple powerpc64le-unkown-gnu-linux  < %s |  FileCheck %s
+; RUN: llc -mtriple powerpc64le-unkown-gnu-linux -relocation-model=static \
+; RUN: < %s |  FileCheck --check-prefix=STATIC %s
+; RUN: llc -mtriple powerpc64le-unkown-gnu-linux -relocation-model=pic \
+; RUN: < %s |  FileCheck %s
+
+; globals
+
+@strong_default = global i32 55
+define i32* @get_strong_default() #0 {
+  ret i32* @strong_default
+
+; STATIC-LABEL: @get_strong_default
+; STATIC: addis 3, 2, strong_default@toc@ha
+; STATIC: addi 3, 3, strong_default@toc@l
+; STATIC: blr
+
+; CHECK-LABEL: @get_strong_default
+; CHECK: addis 3, 2, .LC0@toc@ha
+; CHECK: ld 3, .LC0@toc@l(3)
+; CHECK: blr
+}
+
+@weak_default = weak global i32 55
+define i32* @get_weak_default() #0 {
+  ret i32* @weak_default
+
+; STATIC-LABEL: @get_weak_default
+; STATIC: addis 3, 2, weak_default@toc@ha
+; STATIC: addi 3, 3, weak_default@toc@l
+; STATIC: blr
+
+; CHECK-LABEL: @get_weak_default
+; CHECK: addis 3, 2, .LC1@toc@ha
+; CHECK: ld 3, .LC1@toc@l(3)
+; CHECK: blr
+}
+
+@external_default_global = external global i32
+define i32* @get_external_default_global() {
+  ret i32* @external_default_global
+
+; STATIC-LABEL: @get_external_default_global
+; STATIC: addis 3, 2, .LC0@toc@ha
+; STATIC: ld 3, .LC0@toc@l(3)
+; STATIC: blr
+
+; CHECK-LABEL: @get_external_default_global
+; CHECK: addis 3, 2, .LC2@toc@ha
+; CHECK: ld 3, .LC2@toc@l(3)
+; CHECK: blr
+}
+
+
+@strong_local_global = dso_local global i32 55
+define i32* @get_strong_local_global() {
+  ret i32* @strong_local_global
+
+; STATIC-LABEL: @get_strong_local_global
+; STATIC:       addis 3, 2, strong_local_global@toc@ha
+; STATIC:       addi 3, 3, strong_local_global@toc@l
+; STATIC:       blr
+
+; CHECK-LABEL: @get_strong_local_global
+; CHECK:       addis 3, 2, strong_local_global@toc@ha
+; CHECK:       addi 3, 3, strong_local_global@toc@l
+; CHECK:       blr
+}
+
+@weak_local_global = weak dso_local global i32 42
+define i32* @get_weak_local_global() {
+  ret i32* @weak_local_global
+
+; STATIC-LABEL: @get_weak_local_global
+; STATIC:       addis 3, 2, weak_local_global@toc@ha
+; STATIC:       addi 3, 3, weak_local_global@toc@l
+; STATIC:       blr
+
+; CHECK-LABEL: @get_weak_local_global
+; CHECK:       addis 3, 2, weak_local_global@toc@ha
+; CHECK:       addi 3, 3, weak_local_global@toc@l
+; CHECK:       blr
+}
+
+@external_local_global = external dso_local global i32
+define i32* @get_external_local_global() {
+  ret i32* @external_local_global
+; STATIC-LABEL: @get_external_local_global
+; STATIC:       addis 3, 2, external_local_global@toc@ha
+; STATIC:       addi 3, 3, external_local_global@toc@l
+; STATIC:       blr
+
+; CHECK-LABEL: @get_external_local_global
+; CHECK:       addis 3, 2, external_local_global@toc@ha
+; CHECK:       addi 3, 3, external_local_global@toc@l
+; CHECK:       blr
+}
+
+@strong_preemptable_global = dso_preemptable global i32 42
+define i32* @get_strong_preemptable_global() {
+  ret i32* @strong_preemptable_global
+
+; STATIC-LABEL: @get_strong_preemptable_global
+; STATIC: addis 3, 2, strong_preemptable_global@toc@ha
+; STATIC: addi 3, 3, strong_preemptable_global@toc@l
+; STATIC: blr
+
+; CHECK-LABEL: @get_strong_preemptable_global
+; CHECK: addis 3, 2, .LC3@toc@ha
+; CHECK: ld 3, .LC3@toc@l(3)
+; CHECK: blr
+}
+
+@weak_preemptable_global = weak dso_preemptable global i32 42
+define i32* @get_weak_preemptable_global() {
+  ret i32* @weak_preemptable_global
+
+; STATIC-LABEL: @get_weak_preemptable_global
+; STATIC: addis 3, 2, weak_preemptable_global@toc@ha
+; STATIC: addi 3, 3, weak_preemptable_global@toc@l
+; STATIC: blr
+
+; CHECK-LABEL: @get_weak_preemptable_global
+; CHECK: addis 3, 2, .LC4@toc@ha
+; CHECK: ld 3, .LC4@toc@l(3)
+; CHECK: blr
+}
+
+@external_preemptable_global = external dso_preemptable global i32
+define i32* @get_external_preemptable_global() {
+  ret i32* @external_preemptable_global
+
+; STATIC-LABEL: @get_external_preemptable_global
+; STATIC: addis 3, 2, .LC1@toc@ha
+; STATIC: ld 3, .LC1@toc@l(3)
+; STATIC: blr
+
+; CHECK-LABEL: @get_external_preemptable_global
+; CHECK: addis 3, 2, .LC5@toc@ha
+; CHECK: ld 3, .LC5@toc@l(3)
+; CHECK: blr
+}
+
+; functions
+define signext i32 @strong_default_function(i32 %i) {
+  ret i32 %i
+}
+define signext i32 @strong_default_function_caller(i32 %i) {
+  %call = notail call signext i32 @strong_default_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @strong_default_function_caller
+; STATIC:       bl strong_default_function
+; STATIC-NOT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @strong_default_function_caller
+; CHECK:        bl strong_default_function
+; CHECK-NEXT:   nop
+; CHECK:        blr
+}
+
+define weak signext i32 @weak_default_function(i32 %i) {
+  ret i32 %i
+}
+define signext i32 @weak_default_function_caller(i32 %i) {
+  %call = notail call signext i32 @weak_default_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @weak_default_function_caller
+; STATIC:       bl weak_default_function
+; STATIC-NOT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @weak_default_function_caller
+; CHECK:        bl weak_default_function
+; CHECK-NEXT:   nop
+; CHECK:        blr
+}
+
+
+declare i32 @external_default_function(i32 %i)
+define i32 @external_default_function_caller(i32 %i) {
+  %call = notail call signext i32  @external_default_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @external_default_function_caller
+; STATIC:       bl external_default_function
+; STATIC-NEXT:  nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @external_default_function_caller
+; CHECK:        bl external_default_function
+; CHECK-NEXT:   nop
+; CHECK:        blr
+}
+
+define dso_local signext i32 @strong_local_function(i32 %i) {
+  ret i32 %i
+}
+define signext i32 @strong_local_function_caller(i32 %i) {
+  %call = notail call signext i32 @strong_local_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @strong_local_function_caller
+; STATIC:       bl strong_local_function
+; STATIC-NOT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @strong_local_function_caller
+; CHECK:        bl strong_local_function
+; CHECK-NOT:    nop
+; CHECK:        blr
+}
+
+define weak dso_local signext i32 @weak_local_function(i32 %i) {
+  ret i32 %i
+}
+define signext i32 @weak_local_function_caller(i32 %i) {
+  %call = notail call signext i32 @weak_local_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @weak_local_function_caller
+; STATIC:       bl weak_local_function
+; STATIC-NOT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @weak_local_function_caller
+; CHECK:        bl weak_local_function
+; CHECK-NOT:    nop
+; CHECK:        blr
+}
+
+declare dso_local i32 @external_local_function(i32 %i)
+define i32 @external_local_function_caller(i32 %i) {
+  %call = notail call signext i32  @external_local_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @external_local_function_caller
+; STATIC:       bl external_local_function
+; STATIC-NOT:  nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @external_local_function_caller
+; CHECK:        bl external_local_function
+; CHECK-NOT:    nop
+; CHECK:        blr
+}
+
+define dso_preemptable signext i32 @strong_preemptable_function(i32 %i) {
+  ret i32 %i
+}
+define signext i32 @strong_preemptable_function_caller(i32 %i) {
+  %call = notail call signext i32 @strong_preemptable_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @strong_preemptable_function_caller
+; STATIC:       bl strong_preemptable_function
+; STATIC-NOT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @strong_preemptable_function_caller
+; CHECK:        bl strong_preemptable_function
+; CHECK-NEXT:   nop
+; CHECK:        blr
+}
+
+define weak dso_preemptable signext i32 @weak_preemptable_function(i32 %i) {
+  ret i32 %i
+}
+define signext i32 @weak_preemptable_function_caller(i32 %i) {
+  %call = notail call signext i32 @weak_preemptable_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @weak_preemptable_function_caller
+; STATIC:       bl weak_preemptable_function
+; STATIC-NOT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @weak_preemptable_function_caller
+; CHECK:        bl weak_preemptable_function
+; CHECK-NEXT:   nop
+; CHECK:        blr
+}
+
+declare dso_preemptable i32 @external_preemptable_function(i32 %i)
+define i32 @external_preemptable_function_caller(i32 %i) {
+  %call = notail call signext i32  @external_preemptable_function(i32 signext %i)
+  ret i32 %call
+
+; STATIC-LABEL: @external_preemptable_function_caller
+; STATIC:       bl external_preemptable_function
+; STATIC-NEXT:   nop
+; STATIC:       blr
+
+; CHECK-LABEL:  @external_preemptable_function_caller
+; CHECK:        bl external_preemptable_function
+; CHECK-NEXT:    nop
+; CHECK:        blr
+}
+
diff --git a/llvm/test/CodeGen/X86/darwin-preemption.ll b/llvm/test/CodeGen/X86/darwin-preemption.ll
new file mode 100644
index 0000000..9df0389
--- /dev/null
+++ b/llvm/test/CodeGen/X86/darwin-preemption.ll
@@ -0,0 +1,251 @@
+; RUN: llc -mtriple x86_64-apple-darwin \
+; RUN:     -relocation-model=static          < %s | FileCheck %s
+; RUN: llc -mtriple x86_64-apple-darwin \
+; RUN:     -relocation-model=pic             < %s | FileCheck %s
+; RUN: llc -mtriple x86_64-apple-darwin \
+; RUN:     -relocation-model=dynamic-no-pic  < %s | FileCheck %s
+
+; 32 bits
+
+; RUN: llc -mtriple i386-apple-darwin \
+; RUN:    -relocation-model=static < %s | FileCheck --check-prefix=DARWIN32_S %s
+; RUN: llc -mtriple i386-apple-darwin \
+; RUN:     -relocation-model=pic     < %s | FileCheck --check-prefix=DARWIN32 %s
+; RUN: llc -mtriple i386-apple-darwin \
+; RUN:   -relocation-model=dynamic-no-pic < %s | \
+; RUN:   FileCheck --check-prefix=DARWIN32_DNP %s
+
+; globals
+
+@strong_default_global = global i32 42
+define i32* @get_strong_default_global() {
+  ret i32* @strong_default_global
+}
+; CHECK: leaq _strong_default_global(%rip), %rax
+; DARWIN32: leal _strong_default_global-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_default_global, %eax
+; DARWIN32_DNP: movl $_strong_default_global, %eax
+
+@weak_default_global = weak global i32 42
+define i32* @get_weak_default_global() {
+  ret i32* @weak_default_global
+}
+; CHECK: movq _weak_default_global@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_weak_default_global$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_default_global, %eax
+; DARWIN32_DNP: movl L_weak_default_global$non_lazy_ptr, %eax
+
+@external_default_global = external global i32
+define i32* @get_external_default_global() {
+  ret i32* @external_default_global
+}
+; CHECK: movq _external_default_global@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_external_default_global$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_external_default_global, %eax
+; DARWIN32_DNP: movl L_external_default_global$non_lazy_ptr, %eax
+
+@strong_local_global = dso_local global i32 42
+define i32* @get_strong_local_global() {
+  ret i32* @strong_local_global
+}
+; CHECK: leaq _strong_local_global(%rip), %rax
+; DARWIN32: leal _strong_local_global-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_local_global, %eax
+; DARWIN32_DNP: movl $_strong_local_global, %eax
+
+@weak_local_global = weak dso_local global i32 42
+define i32* @get_weak_local_global() {
+  ret i32* @weak_local_global
+}
+; CHECK: leaq _weak_local_global(%rip), %rax
+; DARWIN32: leal _weak_local_global-L{{.}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_local_global, %eax
+; DARWIN32_DNP: movl $_weak_local_global, %eax
+
+@external_local_global = external dso_local global i32
+define i32* @get_external_local_global() {
+  ret i32* @external_local_global
+}
+; CHECK: leaq _external_local_global(%rip), %rax
+; DARWIN32: movl L_external_local_global$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_external_local_global, %eax
+; DARWIN32_DNP: movl $_external_local_global, %eax
+
+@strong_preemptable_global = dso_preemptable global i32 42
+define i32* @get_strong_preemptable_global() {
+  ret i32* @strong_preemptable_global
+}
+; CHECK: leaq _strong_preemptable_global(%rip), %rax
+; DARWIN32: leal _strong_preemptable_global-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_preemptable_global, %eax
+; DARWIN32_DNP: movl $_strong_preemptable_global, %eax
+
+@weak_preemptable_global = weak dso_preemptable global i32 42
+define i32* @get_weak_preemptable_global() {
+  ret i32* @weak_preemptable_global
+}
+; CHECK: movq _weak_preemptable_global@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_weak_preemptable_global$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_preemptable_global, %eax
+; DARWIN32_DNP: movl L_weak_preemptable_global$non_lazy_ptr, %eax
+
+@external_preemptable_global = external dso_preemptable global i32
+define i32* @get_external_preemptable_global() {
+  ret i32* @external_preemptable_global
+}
+; CHECK: movq _external_preemptable_global@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_external_preemptable_global$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_external_preemptable_global, %eax
+; DARWIN32_DNP: movl L_external_preemptable_global$non_lazy_ptr, %eax
+
+; aliases
+@aliasee = global i32 42
+
+@strong_default_alias = alias i32, i32* @aliasee
+define i32* @get_strong_default_alias() {
+  ret i32* @strong_default_alias
+}
+; CHECK: leaq _strong_default_alias(%rip), %rax
+; DARWIN32: leal _strong_default_alias-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_default_alias, %eax
+; DARWIN32_DNP: movl $_strong_default_alias, %eax
+
+@weak_default_alias = weak alias i32, i32* @aliasee
+define i32* @get_weak_default_alias() {
+  ret i32* @weak_default_alias
+}
+; CHECK: movq _weak_default_alias@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_weak_default_alias$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_default_alias, %eax
+; DARWIN32_DNP: movl L_weak_default_alias$non_lazy_ptr, %eax
+
+@strong_local_alias = dso_local alias i32, i32* @aliasee
+define i32* @get_strong_local_alias() {
+  ret i32* @strong_local_alias
+}
+; CHECK: leaq _strong_local_alias(%rip), %rax
+; DARWIN32: leal _strong_local_alias-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_local_alias, %eax
+; DARWIN32_DNP: movl $_strong_local_alias, %eax
+
+@weak_local_alias = weak dso_local alias i32, i32* @aliasee
+define i32* @get_weak_local_alias() {
+  ret i32* @weak_local_alias
+}
+; CHECK: leaq _weak_local_alias(%rip), %rax
+; DARWIN32: leal _weak_local_alias-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_local_alias, %eax
+; DARWIN32_DNP: movl $_weak_local_alias, %eax
+
+@strong_preemptable_alias = dso_preemptable alias i32, i32* @aliasee
+define i32* @get_strong_preemptable_alias() {
+  ret i32* @strong_preemptable_alias
+}
+; CHECK: leaq _strong_preemptable_alias(%rip), %rax
+; DARWIN32: leal _strong_preemptable_alias-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_preemptable_alias, %eax
+; DARWIN32_DNP: movl $_strong_preemptable_alias, %eax
+
+@weak_preemptable_alias = weak dso_preemptable alias i32, i32* @aliasee
+define i32* @get_weak_preemptable_alias() {
+  ret i32* @weak_preemptable_alias
+}
+; CHECK: movq _weak_preemptable_alias@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_weak_preemptable_alias$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_preemptable_alias, %eax
+; DARWIN32_DNP: movl L_weak_preemptable_alias$non_lazy_ptr, %eax
+
+; functions
+
+define void @strong_default_function() {
+  ret void
+}
+define void()* @get_strong_default_function() {
+  ret void()* @strong_default_function
+}
+; CHECK: leaq _strong_default_function(%rip), %rax
+; DARWIN32: leal _strong_default_function-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_default_function, %eax
+; DARWIN32_DNP: movl $_strong_default_function, %eax
+
+define weak void @weak_default_function() {
+  ret void
+}
+define void()* @get_weak_default_function() {
+  ret void()* @weak_default_function
+}
+; CHECK: movq _weak_default_function@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_weak_default_function$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_default_function, %eax
+; DARWIN32_DNP: movl L_weak_default_function$non_lazy_ptr, %eax
+
+declare void @external_default_function()
+define void()* @get_external_default_function() {
+  ret void()* @external_default_function
+}
+; CHECK: movq _external_default_function@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_external_default_function$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_external_default_function, %eax
+; DARWIN32_DNP: movl L_external_default_function$non_lazy_ptr, %eax
+
+define dso_local void @strong_local_function() {
+  ret void
+}
+define void()* @get_strong_local_function() {
+  ret void()* @strong_local_function
+}
+; CHECK: leaq _strong_local_function(%rip), %rax
+; DARWIN32: leal _strong_local_function-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_local_function, %eax
+; DARWIN32_DNP: movl $_strong_local_function, %eax
+
+define weak dso_local void @weak_local_function() {
+  ret void
+}
+define void()* @get_weak_local_function() {
+  ret void()* @weak_local_function
+}
+; CHECK: leaq _weak_local_function(%rip), %rax
+; DARWIN32: leal _weak_local_function-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_local_function, %eax
+; DARWIN32_DNP: movl $_weak_local_function, %eax
+
+declare dso_local void @external_local_function()
+define void()* @get_external_local_function() {
+  ret void()* @external_local_function
+}
+; CHECK: leaq _external_local_function(%rip), %rax
+; DARWIN32: movl L_external_local_function$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_external_local_function, %eax
+; DARWIN32_DNP: movl $_external_local_function, %eax
+
+define dso_preemptable void @strong_preemptable_function() {
+  ret void
+}
+define void()* @get_strong_preemptable_function() {
+  ret void()* @strong_preemptable_function
+}
+; CHECK: leaq _strong_preemptable_function(%rip), %rax
+; DARWIN32: leal _strong_preemptable_function-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_strong_preemptable_function, %eax
+; DARWIN32_DNP: movl $_strong_preemptable_function, %eax
+
+define weak dso_preemptable void @weak_preemptable_function() {
+  ret void
+}
+define void()* @get_weak_preemptable_function() {
+  ret void()* @weak_preemptable_function
+}
+; CHECK: movq _weak_preemptable_function@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_weak_preemptable_function$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_weak_preemptable_function, %eax
+; DARWIN32_DNP: movl L_weak_preemptable_function$non_lazy_ptr, %eax
+
+declare dso_preemptable void @external_preemptable_function()
+define void()* @get_external_preemptable_function() {
+  ret void()* @external_preemptable_function
+}
+; CHECK: movq _external_preemptable_function@GOTPCREL(%rip), %rax
+; DARWIN32: movl L_external_preemptable_function$non_lazy_ptr-L{{.*}}$pb(%eax), %eax
+; DARWIN32_S: movl $_external_preemptable_function, %eax
+; DARWIN32_DNP: movl L_external_preemptable_function$non_lazy_ptr, %eax
diff --git a/llvm/test/CodeGen/X86/linux-preemption.ll b/llvm/test/CodeGen/X86/linux-preemption.ll
new file mode 100644
index 0000000..ab1ac2f2
--- /dev/null
+++ b/llvm/test/CodeGen/X86/linux-preemption.ll
@@ -0,0 +1,225 @@
+; RUN: llc -mtriple x86_64-pc-linux \
+; RUN:     -relocation-model=static  < %s | FileCheck --check-prefix=STATIC %s
+; RUN: llc -mtriple x86_64-pc-linux \
+; RUN:     -relocation-model=pic             < %s | FileCheck %s
+; RUN: llc -mtriple x86_64-pc-linux \
+; RUN:     -relocation-model=dynamic-no-pic  < %s | FileCheck %s
+
+; 32 bits
+
+; RUN: llc -mtriple i386-pc-linux \
+; RUN:     -relocation-model=pic     < %s | FileCheck --check-prefix=CHECK32 %s
+
+; globals
+
+@strong_default_global = global i32 42
+define i32* @get_strong_default_global() {
+  ret i32* @strong_default_global
+}
+; CHECK: movq strong_default_global@GOTPCREL(%rip), %rax
+; STATIC: movl $strong_default_global, %eax
+; CHECK32: movl strong_default_global@GOT(%eax), %eax
+
+@weak_default_global = weak global i32 42
+define i32* @get_weak_default_global() {
+  ret i32* @weak_default_global
+}
+; CHECK: movq weak_default_global@GOTPCREL(%rip), %rax
+; STATIC: movl $weak_default_global, %eax
+; CHECK32: movl weak_default_global@GOT(%eax), %eax
+
+@external_default_global = external global i32
+define i32* @get_external_default_global() {
+  ret i32* @external_default_global
+}
+; CHECK: movq external_default_global@GOTPCREL(%rip), %rax
+; STATIC: movl $external_default_global, %eax
+; CHECK32: movl external_default_global@GOT(%eax), %eax
+
+@strong_local_global = dso_local global i32 42
+define i32* @get_strong_local_global() {
+  ret i32* @strong_local_global
+}
+; CHECK: leaq strong_local_global(%rip), %rax
+; STATIC: movl $strong_local_global, %eax
+; CHECK32: leal strong_local_global@GOTOFF(%eax), %eax
+
+@weak_local_global = weak dso_local global i32 42
+define i32* @get_weak_local_global() {
+  ret i32* @weak_local_global
+}
+; CHECK: leaq weak_local_global(%rip), %rax
+; STATIC: movl $weak_local_global, %eax
+; CHECK32: leal weak_local_global@GOTOFF(%eax), %eax
+
+@external_local_global = external dso_local global i32
+define i32* @get_external_local_global() {
+  ret i32* @external_local_global
+}
+; CHECK: leaq external_local_global(%rip), %rax
+; STATIC: movl $external_local_global, %eax
+; CHECK32: leal external_local_global@GOTOFF(%eax), %eax
+
+
+@strong_preemptable_global = dso_preemptable global i32 42
+define i32* @get_strong_preemptable_global() {
+  ret i32* @strong_preemptable_global
+}
+; CHECK: movq strong_preemptable_global@GOTPCREL(%rip), %rax
+; STATIC: movl $strong_preemptable_global, %eax
+; CHECK32: movl strong_preemptable_global@GOT(%eax), %eax
+
+@weak_preemptable_global = weak dso_preemptable global i32 42
+define i32* @get_weak_preemptable_global() {
+  ret i32* @weak_preemptable_global
+}
+; CHECK ;ADD_LABEL_BACK;  movq weak_preemptable_global@GOTPCREL(%rip), %rax
+; STATIC ;ADD_LABEL_BACK; movq weak_preemptable_global@GOTPCREL, %rax
+; CHECK32 ;ADD_LABEL_BACK; movl weak_preemptable_global@GOT(%eax), %eax
+
+@external_preemptable_global = external dso_preemptable global i32
+define i32* @get_external_preemptable_global() {
+  ret i32* @external_preemptable_global
+}
+; CHECK: movq external_preemptable_global@GOTPCREL(%rip), %rax
+; STATIC: movl $external_preemptable_global, %eax
+; CHECK32: movl external_preemptable_global@GOT(%eax), %eax
+
+; aliases
+@aliasee = global i32 42
+
+@strong_default_alias = alias i32, i32* @aliasee
+define i32* @get_strong_default_alias() {
+  ret i32* @strong_default_alias
+}
+; CHECK: movq strong_default_alias@GOTPCREL(%rip), %rax
+; STATIC: movl $strong_default_alias, %eax
+; CHECK32: movl strong_default_alias@GOT(%eax), %eax
+
+@weak_default_alias = weak alias i32, i32* @aliasee
+define i32* @get_weak_default_alias() {
+  ret i32* @weak_default_alias
+}
+; CHECK: movq weak_default_alias@GOTPCREL(%rip), %rax
+; STATIC: movl $weak_default_alias, %eax
+; CHECK32: movl weak_default_alias@GOT(%eax), %eax
+
+@strong_local_alias = dso_local alias i32, i32* @aliasee
+define i32* @get_strong_local_alias() {
+  ret i32* @strong_local_alias
+}
+; CHECK: leaq strong_local_alias(%rip), %rax
+; STATIC: movl $strong_local_alias, %eax
+; CHECK32: leal strong_local_alias@GOTOFF(%eax), %eax
+
+@weak_local_alias = weak dso_local alias i32, i32* @aliasee
+define i32* @get_weak_local_alias() {
+  ret i32* @weak_local_alias
+}
+; CHECK: leaq weak_local_alias(%rip), %rax
+; STATIC: movl $weak_local_alias, %eax
+; CHECK32: leal weak_local_alias@GOTOFF(%eax), %eax
+
+
+@strong_preemptable_alias = dso_preemptable alias i32, i32* @aliasee
+define i32* @get_strong_preemptable_alias() {
+  ret i32* @strong_preemptable_alias
+}
+; CHECK: movq strong_preemptable_alias@GOTPCREL(%rip), %rax
+; STATIC: movl $strong_preemptable_alias, %eax
+; CHECK32: movl strong_preemptable_alias@GOT(%eax), %eax
+
+@weak_preemptable_alias = weak dso_preemptable alias i32, i32* @aliasee
+define i32* @get_weak_preemptable_alias() {
+  ret i32* @weak_preemptable_alias
+}
+; CHECK: movq weak_preemptable_alias@GOTPCREL(%rip), %rax
+; STATIC: movl $weak_preemptable_alias, %eax
+; CHECK32: movl weak_preemptable_alias@GOT(%eax), %eax
+
+; functions
+
+define void @strong_default_function() {
+  ret void
+}
+define void()* @get_strong_default_function() {
+  ret void()* @strong_default_function
+}
+; CHECK: movq strong_default_function@GOTPCREL(%rip), %rax
+; STATIC: movl $strong_default_function, %eax
+; CHECK32: movl strong_default_function@GOT(%eax), %eax
+
+define weak void @weak_default_function() {
+  ret void
+}
+define void()* @get_weak_default_function() {
+  ret void()* @weak_default_function
+}
+; CHECK: movq weak_default_function@GOTPCREL(%rip), %rax
+; STATIC: movl $weak_default_function, %eax
+; CHECK32: movl weak_default_function@GOT(%eax), %eax
+
+declare void @external_default_function()
+define void()* @get_external_default_function() {
+  ret void()* @external_default_function
+}
+; CHECK: movq external_default_function@GOTPCREL(%rip), %rax
+; STATIC: movl $external_default_function, %eax
+; CHECK32: movl external_default_function@GOT(%eax), %eax
+
+define dso_local void @strong_local_function() {
+  ret void
+}
+define void()* @get_strong_local_function() {
+  ret void()* @strong_local_function
+}
+; CHECK: leaq strong_local_function(%rip), %rax
+; STATIC: movl $strong_local_function, %eax
+; CHECK32: leal strong_local_function@GOTOFF(%eax), %eax
+
+define weak dso_local void @weak_local_function() {
+  ret void
+}
+define void()* @get_weak_local_function() {
+  ret void()* @weak_local_function
+}
+; CHECK: leaq weak_local_function(%rip), %rax
+; STATIC: movl $weak_local_function, %eax
+; CHECK32: leal weak_local_function@GOTOFF(%eax), %eax
+
+declare dso_local void @external_local_function()
+define void()* @get_external_local_function() {
+  ret void()* @external_local_function
+}
+; CHECK: leaq external_local_function(%rip), %rax
+; STATIC: movl $external_local_function, %eax
+; CHECK32: leal external_local_function@GOTOFF(%eax), %eax
+
+
+define dso_preemptable void @strong_preemptable_function() {
+  ret void
+}
+define void()* @get_strong_preemptable_function() {
+  ret void()* @strong_preemptable_function
+}
+; CHECK: movq strong_preemptable_function@GOTPCREL(%rip), %rax
+; STATIC: movl $strong_preemptable_function, %eax
+; CHECK32: movl strong_preemptable_function@GOT(%eax), %eax
+
+define weak dso_preemptable void @weak_preemptable_function() {
+  ret void
+}
+define void()* @get_weak_preemptable_function() {
+  ret void()* @weak_preemptable_function
+}
+; CHECK: movq weak_preemptable_function@GOTPCREL(%rip), %rax
+; STATIC: movl $weak_preemptable_function, %eax
+; CHECK32: movl weak_preemptable_function@GOT(%eax), %eax
+
+declare dso_preemptable void @external_preemptable_function()
+define void()* @get_external_preemptable_function() {
+  ret void()* @external_preemptable_function
+}
+; CHECK: movq external_preemptable_function@GOTPCREL(%rip), %rax
+; STATIC: movl $external_preemptable_function, %eax
+; CHECK32: movl external_preemptable_function@GOT(%eax), %eax
diff --git a/llvm/test/CodeGen/X86/win32-preemption.ll b/llvm/test/CodeGen/X86/win32-preemption.ll
new file mode 100644
index 0000000..accabac
--- /dev/null
+++ b/llvm/test/CodeGen/X86/win32-preemption.ll
@@ -0,0 +1,236 @@
+; RUN: llc -mtriple x86_64-pc-win32 \
+; RUN:     -relocation-model=static  < %s | FileCheck --check-prefix=COFF_S %s
+; RUN: llc -mtriple x86_64-pc-win32 \
+; RUN:     -relocation-model=pic     < %s | FileCheck --check-prefix=COFF %s
+; RUN: llc -mtriple x86_64-pc-win32 \
+; RUN:  -relocation-model=dynamic-no-pic < %s | FileCheck --check-prefix=COFF %s
+
+
+; 32 bits
+
+; RUN: llc -mtriple i386-pc-win32 \
+; RUN:    -relocation-model=static  < %s | FileCheck --check-prefix=COFF32 %s
+; RUN: llc -mtriple i386-pc-win32 \
+; RUN:     -relocation-model=pic     < %s | FileCheck --check-prefix=COFF32 %s
+; RUN: llc -mtriple i386-pc-win32 \
+; RUN:   -relocation-model=dynamic-no-pic < %s | \
+; RUN:   FileCheck --check-prefix=COFF32 %s
+
+; globals
+
+@strong_default_global = global i32 42
+define i32* @get_strong_default_global() {
+  ret i32* @strong_default_global
+}
+; COFF: leaq strong_default_global(%rip), %rax
+; COFF_S: movl $strong_default_global, %eax
+; COFF32: movl $_strong_default_global, %eax
+
+@weak_default_global = weak global i32 42
+define i32* @get_weak_default_global() {
+  ret i32* @weak_default_global
+}
+; COFF: leaq weak_default_global(%rip), %rax
+; COFF_S: movl $weak_default_global, %eax
+; COFF32: movl $_weak_default_global, %eax
+
+@external_default_global = external global i32
+define i32* @get_external_default_global() {
+  ret i32* @external_default_global
+}
+; COFF: leaq external_default_global(%rip), %rax
+; COFF_S: movl $external_default_global, %eax
+; COFF32: movl $_external_default_global, %eax
+
+
+@strong_local_global = dso_local global i32 42
+define i32* @get_strong_local_global() {
+  ret i32* @strong_local_global
+}
+; COFF: leaq strong_local_global(%rip), %rax
+; COFF_S: movl $strong_local_global, %eax
+; COFF32: movl $_strong_local_global, %eax
+
+@weak_local_global = weak dso_local global i32 42
+define i32* @get_weak_local_global() {
+  ret i32* @weak_local_global
+}
+; COFF: leaq weak_local_global(%rip), %rax
+; COFF_S: movl $weak_local_global, %eax
+; COFF32: movl $_weak_local_global, %eax
+
+@external_local_global = external dso_local global i32
+define i32* @get_external_local_global() {
+  ret i32* @external_local_global
+}
+; COFF: leaq external_local_global(%rip), %rax
+; COFF_S: movl $external_local_global, %eax
+; COFF32: movl $_external_local_global, %eax
+
+
+@strong_preemptable_global = dso_preemptable global i32 42
+define i32* @get_strong_preemptable_global() {
+  ret i32* @strong_preemptable_global
+}
+; COFF: leaq strong_preemptable_global(%rip), %rax
+; COFF_S: movl $strong_preemptable_global, %eax
+; COFF32: movl $_strong_preemptable_global, %eax
+
+@weak_preemptable_global = weak dso_preemptable global i32 42
+define i32* @get_weak_preemptable_global() {
+  ret i32* @weak_preemptable_global
+}
+; COFF: leaq weak_preemptable_global(%rip), %rax
+; COFF_S: movl $weak_preemptable_global, %eax
+; COFF32: movl $_weak_preemptable_global, %eax
+
+@external_preemptable_global = external dso_preemptable global i32
+define i32* @get_external_preemptable_global() {
+  ret i32* @external_preemptable_global
+}
+; COFF: leaq external_preemptable_global(%rip), %rax
+; COFF_S: movl $external_preemptable_global, %eax
+; COFF32: movl $_external_preemptable_global, %eax
+
+
+; aliases
+@aliasee = global i32 42
+
+@strong_default_alias = alias i32, i32* @aliasee
+define i32* @get_strong_default_alias() {
+  ret i32* @strong_default_alias
+}
+; COFF: leaq strong_default_alias(%rip), %rax
+; COFF_S: movl $strong_default_alias, %eax
+; COFF32: movl $_strong_default_alias, %eax
+
+@weak_default_alias = weak alias i32, i32* @aliasee
+define i32* @get_weak_default_alias() {
+  ret i32* @weak_default_alias
+}
+; COFF: leaq weak_default_alias(%rip), %rax
+; COFF_S: movl $weak_default_alias, %eax
+; COFF32: movl $_weak_default_alias, %eax
+
+
+@strong_local_alias = dso_local alias i32, i32* @aliasee
+define i32* @get_strong_local_alias() {
+  ret i32* @strong_local_alias
+}
+; COFF: leaq strong_local_alias(%rip), %rax
+; COFF_S: movl $strong_local_alias, %eax
+; COFF32: movl $_strong_local_alias, %eax
+
+@weak_local_alias = weak dso_local alias i32, i32* @aliasee
+define i32* @get_weak_local_alias() {
+  ret i32* @weak_local_alias
+}
+; COFF: leaq weak_local_alias(%rip), %rax
+; COFF_S: movl $weak_local_alias, %eax
+; COFF32: movl $_weak_local_alias, %eax
+
+
+@strong_preemptable_alias = dso_preemptable alias i32, i32* @aliasee
+define i32* @get_strong_preemptable_alias() {
+  ret i32* @strong_preemptable_alias
+}
+; COFF: leaq strong_preemptable_alias(%rip), %rax
+; COFF_S: movl $strong_preemptable_alias, %eax
+; COFF32: movl $_strong_preemptable_alias, %eax
+
+@weak_preemptable_alias = weak dso_preemptable alias i32, i32* @aliasee
+define i32* @get_weak_preemptable_alias() {
+  ret i32* @weak_preemptable_alias
+}
+; COFF: leaq weak_preemptable_alias(%rip), %rax
+; COFF_S: movl $weak_preemptable_alias, %eax
+; COFF32: movl $_weak_preemptable_alias, %eax
+
+
+; functions
+
+define void @strong_default_function() {
+  ret void
+}
+define void()* @get_strong_default_function() {
+  ret void()* @strong_default_function
+}
+; COFF: leaq strong_default_function(%rip), %rax
+; COFF_S: movl $strong_default_function, %eax
+; COFF32: movl $_strong_default_function, %eax
+
+define weak void @weak_default_function() {
+  ret void
+}
+define void()* @get_weak_default_function() {
+  ret void()* @weak_default_function
+}
+; COFF: leaq weak_default_function(%rip), %rax
+; COFF_S: movl $weak_default_function, %eax
+; COFF32: movl $_weak_default_function, %eax
+
+declare void @external_default_function()
+define void()* @get_external_default_function() {
+  ret void()* @external_default_function
+}
+; COFF: leaq external_default_function(%rip), %rax
+; COFF_S: movl $external_default_function, %eax
+; COFF32: movl $_external_default_function, %eax
+
+
+define dso_local void @strong_local_function() {
+  ret void
+}
+define void()* @get_strong_local_function() {
+  ret void()* @strong_local_function
+}
+; COFF: leaq strong_local_function(%rip), %rax
+; COFF_S: movl $strong_local_function, %eax
+; COFF32: movl $_strong_local_function, %eax
+
+define weak dso_local void @weak_local_function() {
+  ret void
+}
+define void()* @get_weak_local_function() {
+  ret void()* @weak_local_function
+}
+; COFF: leaq weak_local_function(%rip), %rax
+; COFF_S: movl $weak_local_function, %eax
+; COFF32: movl $_weak_local_function, %eax
+
+declare dso_local void @external_local_function()
+define void()* @get_external_local_function() {
+  ret void()* @external_local_function
+}
+; COFF: leaq external_local_function(%rip), %rax
+; COFF_S: movl $external_local_function, %eax
+; COFF32: movl $_external_local_function, %eax
+
+
+define dso_preemptable void @strong_preemptable_function() {
+  ret void
+}
+define void()* @get_strong_preemptable_function() {
+  ret void()* @strong_preemptable_function
+}
+; COFF: leaq strong_preemptable_function(%rip), %rax
+; COFF_S: movl $strong_preemptable_function, %eax
+; COFF32: movl $_strong_preemptable_function, %eax
+
+define weak dso_preemptable void @weak_preemptable_function() {
+  ret void
+}
+define void()* @get_weak_preemptable_function() {
+  ret void()* @weak_preemptable_function
+}
+; COFF: leaq weak_preemptable_function(%rip), %rax
+; COFF_S: movl $weak_preemptable_function, %eax
+; COFF32: movl $_weak_preemptable_function, %eax
+
+declare dso_preemptable void @external_preemptable_function()
+define void()* @get_external_preemptable_function() {
+  ret void()* @external_preemptable_function
+}
+; COFF: leaq external_preemptable_function(%rip), %rax
+; COFF_S: movl $external_preemptable_function, %eax
+; COFF32: movl $_external_preemptable_function, %eax