diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -54789,9 +54789,15 @@ // In any sort of PIC mode addresses need to be computed at runtime by // adding in a register or some sort of table lookup. These can't - // be used as immediates. - if (Subtarget.isPICStyleGOT() || Subtarget.isPICStyleStubPIC()) - return; + // be used as immediates. BlockAddresses referring to labels in the local + // function don't need such lookup. + if (Subtarget.isPICStyleGOT() || Subtarget.isPICStyleStubPIC()) { + if (auto *BA = dyn_cast(Op)) { + if (BA->getBlockAddress()->getFunction() != &DAG.getMachineFunction().getFunction()) + return; + } else + return; + } // If we are in non-pic codegen mode, we allow the address of a global (with // an optional displacement) to be used with 'i'. diff --git a/llvm/test/CodeGen/X86/asm-block-labels-pic.ll b/llvm/test/CodeGen/X86/asm-block-labels-pic.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/asm-block-labels-pic.ll @@ -0,0 +1,28 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc --relocation-model=pic %s -o - | FileCheck %s +target triple = "i386-unknown-linux-gnu" + +; The intent of this test is to ensure that we handle blockaddress' correctly +; with "i" constraints for -m32 -fPIC. + +define void @x() { +; CHECK-LABEL: x: +; CHECK: # %bb.0: +; CHECK-NEXT: #APP +; CHECK-NEXT: # .Ltmp0 +; CHECK-EMPTY: +; CHECK-NEXT: #NO_APP +; CHECK-NEXT: # %bb.2: # %return +; CHECK-NEXT: retl +; CHECK-NEXT: .Ltmp0: # Block address taken +; CHECK-NEXT: .LBB0_1: # %overflow +; CHECK-NEXT: retl + callbr void asm "# ${0:l}\0A", "i"(i8* blockaddress(@x, %overflow)) + to label %return [label %overflow] + +overflow: + br label %return + +return: + ret void +} diff --git a/llvm/test/CodeGen/X86/asm-block-labels-pic2.ll b/llvm/test/CodeGen/X86/asm-block-labels-pic2.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/asm-block-labels-pic2.ll @@ -0,0 +1,19 @@ +; RUN: not llc --relocation-model=pic %s -o - 2>&1 | FileCheck %s +target triple = "i386-unknown-linux-gnu" + +; The intent of this test is to ensure that we handle blockaddress' correctly +; with "i" constraints for -m32 -fPIC. + +define void @x() { +entry: + br label %overflow +overflow: + ret void +} + +; Test unusual case of blockaddress from @x in @y's asm. +; CHECK: error: invalid operand for inline asm constraint 'i' +define void @y() { + call void asm "# ${0:l}\0A", "i"(i8* blockaddress(@x, %overflow)) + ret void +}