Index: llvm/trunk/docs/LangRef.rst =================================================================== --- llvm/trunk/docs/LangRef.rst +++ llvm/trunk/docs/LangRef.rst @@ -4589,6 +4589,25 @@ !2 = !{ i8 0, i8 2, i8 3, i8 6 } !3 = !{ i8 -2, i8 0, i8 3, i8 6 } +'``absolute_symbol``' Metadata +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``absolute_symbol`` metadata may be attached to a global variable +declaration. It marks the declaration as a reference to an absolute symbol, +which causes the backend to use absolute relocations for the symbol even +in position independent code, and expresses the possible ranges that the +global variable's *address* (not its value) is in, in the same format as +``range`` metadata. + +Example: + +.. code-block:: llvm + + @a = external global i8, !absolute_symbol !0 ; Absolute symbol in range [0,256) + + ... + !0 = !{ i64 0, i64 256 } + '``unpredictable``' Metadata ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Index: llvm/trunk/include/llvm/IR/GlobalValue.h =================================================================== --- llvm/trunk/include/llvm/IR/GlobalValue.h +++ llvm/trunk/include/llvm/IR/GlobalValue.h @@ -33,6 +33,7 @@ namespace llvm { class Comdat; +class ConstantRange; class Error; class GlobalObject; class Module; @@ -511,6 +512,13 @@ } GlobalObject *getBaseObject(); + /// Returns whether this is a reference to an absolute symbol. + bool isAbsoluteSymbolRef() const; + + /// If this is an absolute symbol reference, returns the range of the symbol, + /// otherwise returns None. + Optional getAbsoluteSymbolRange() const; + /// This method unlinks 'this' from the containing module, but does not delete /// it. virtual void removeFromParent() = 0; Index: llvm/trunk/include/llvm/IR/LLVMContext.h =================================================================== --- llvm/trunk/include/llvm/IR/LLVMContext.h +++ llvm/trunk/include/llvm/IR/LLVMContext.h @@ -77,6 +77,7 @@ MD_loop = 18, // "llvm.loop" MD_type = 19, // "type" MD_section_prefix = 20, // "section_prefix" + MD_absolute_symbol = 21, // "absolute_symbol" }; /// Known operand bundle tag IDs, which always have the same value. All Index: llvm/trunk/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/trunk/lib/Analysis/ValueTracking.cpp +++ llvm/trunk/lib/Analysis/ValueTracking.cpp @@ -3345,11 +3345,11 @@ if (const Argument *A = dyn_cast(V)) return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr(); - // A global variable in address space 0 is non null unless extern weak. - // Other address spaces may have null as a valid address for a global, - // so we can't assume anything. + // A global variable in address space 0 is non null unless extern weak + // or an absolute symbol reference. Other address spaces may have null as a + // valid address for a global, so we can't assume anything. if (const GlobalValue *GV = dyn_cast(V)) - return !GV->hasExternalWeakLinkage() && + return !GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && GV->getType()->getAddressSpace() == 0; // A Load tagged with nonnull metadata is never null. Index: llvm/trunk/lib/IR/Globals.cpp =================================================================== --- llvm/trunk/lib/IR/Globals.cpp +++ llvm/trunk/lib/IR/Globals.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Triple.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/ConstantRange.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalValue.h" @@ -222,6 +223,26 @@ return nullptr; } +bool GlobalValue::isAbsoluteSymbolRef() const { + auto *GO = dyn_cast(this); + if (!GO) + return false; + + return GO->getMetadata(LLVMContext::MD_absolute_symbol); +} + +Optional GlobalValue::getAbsoluteSymbolRange() const { + auto *GO = dyn_cast(this); + if (!GO) + return None; + + MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol); + if (!MD) + return None; + + return getConstantRangeFromMetadata(*MD); +} + //===----------------------------------------------------------------------===// // GlobalVariable Implementation //===----------------------------------------------------------------------===// Index: llvm/trunk/lib/IR/LLVMContext.cpp =================================================================== --- llvm/trunk/lib/IR/LLVMContext.cpp +++ llvm/trunk/lib/IR/LLVMContext.cpp @@ -57,6 +57,7 @@ {MD_loop, "llvm.loop"}, {MD_type, "type"}, {MD_section_prefix, "section_prefix"}, + {MD_absolute_symbol, "absolute_symbol"}, }; for (auto &MDKind : MDKinds) { Index: llvm/trunk/lib/Target/X86/X86ISelDAGToDAG.cpp =================================================================== --- llvm/trunk/lib/Target/X86/X86ISelDAGToDAG.cpp +++ llvm/trunk/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -24,6 +24,7 @@ #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/SelectionDAGISel.h" +#include "llvm/IR/ConstantRange.h" #include "llvm/IR/Function.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Intrinsics.h" @@ -1571,7 +1572,15 @@ return false; Imm = N; - return TM.getCodeModel() == CodeModel::Small; + if (N->getOpcode() != ISD::TargetGlobalAddress) + return TM.getCodeModel() == CodeModel::Small; + + Optional CR = + cast(N)->getGlobal()->getAbsoluteSymbolRange(); + if (!CR) + return TM.getCodeModel() == CodeModel::Small; + + return CR->getUnsignedMax().ult(1ull << 32); } bool X86DAGToDAGISel::selectLEA64_32Addr(SDValue N, SDValue &Base, @@ -1710,10 +1719,39 @@ return true; } + // Keep track of the original value type and whether this value was + // truncated. If we see a truncation from pointer type to VT that truncates + // bits that are known to be zero, we can use a narrow reference. + EVT VT = N.getValueType(); + bool WasTruncated = false; + if (N.getOpcode() == ISD::TRUNCATE) { + WasTruncated = true; + N = N.getOperand(0); + } + if (N.getOpcode() != X86ISD::Wrapper) return false; - Op = N.getOperand(0); + // We can only use non-GlobalValues as immediates if they were not truncated, + // as we do not have any range information. If we have a GlobalValue and the + // address was not truncated, we can select it as an operand directly. + unsigned Opc = N.getOperand(0)->getOpcode(); + if (Opc != ISD::TargetGlobalAddress || !WasTruncated) { + Op = N.getOperand(0); + // We can only select the operand directly if we didn't have to look past a + // truncate. + return !WasTruncated; + } + + // Check that the global's range fits into VT. + auto *GA = cast(N.getOperand(0)); + Optional CR = GA->getGlobal()->getAbsoluteSymbolRange(); + if (!CR || CR->getUnsignedMax().uge(1ull << VT.getSizeInBits())) + return false; + + // Okay, we can use a narrow reference. + Op = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(N), VT, + GA->getOffset(), GA->getTargetFlags()); return true; } Index: llvm/trunk/lib/Target/X86/X86ISelLowering.h =================================================================== --- llvm/trunk/lib/Target/X86/X86ISelLowering.h +++ llvm/trunk/lib/Target/X86/X86ISelLowering.h @@ -1115,7 +1115,7 @@ SDValue InsertBitToMaskVector(SDValue Op, SelectionDAG &DAG) const; SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const; - unsigned getGlobalWrapperKind() const; + unsigned getGlobalWrapperKind(const GlobalValue *GV = nullptr) const; SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const; SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const; SDValue LowerGlobalAddress(const GlobalValue *GV, const SDLoc &dl, Index: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp =================================================================== --- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp +++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp @@ -13676,7 +13676,11 @@ } // Returns the appropriate wrapper opcode for a global reference. -unsigned X86TargetLowering::getGlobalWrapperKind() const { +unsigned X86TargetLowering::getGlobalWrapperKind(const GlobalValue *GV) const { + // References to absolute symbols are never PC-relative. + if (GV && GV->isAbsoluteSymbolRef()) + return X86ISD::Wrapper; + CodeModel::Model M = getTargetMachine().getCodeModel(); if (Subtarget.isPICStyleRIPRel() && (M == CodeModel::Small || M == CodeModel::Kernel)) @@ -13805,7 +13809,7 @@ Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, OpFlags); } - Result = DAG.getNode(getGlobalWrapperKind(), dl, PtrVT, Result); + Result = DAG.getNode(getGlobalWrapperKind(GV), dl, PtrVT, Result); // With PIC, the address is actually $g + Offset. if (isGlobalRelativeToPICBase(OpFlags)) { Index: llvm/trunk/lib/Target/X86/X86InstrInfo.td =================================================================== --- llvm/trunk/lib/Target/X86/X86InstrInfo.td +++ llvm/trunk/lib/Target/X86/X86InstrInfo.td @@ -948,10 +948,10 @@ // Eventually, it would be nice to allow ConstantHoisting to merge constants // globally for potentially added savings. // -def imm8_su : PatLeaf<(i8 imm), [{ +def imm8_su : PatLeaf<(i8 relocImm), [{ return !shouldAvoidImmediateInstFormsForSize(N); }]>; -def imm16_su : PatLeaf<(i16 imm), [{ +def imm16_su : PatLeaf<(i16 relocImm), [{ return !shouldAvoidImmediateInstFormsForSize(N); }]>; def imm32_su : PatLeaf<(i32 relocImm), [{ Index: llvm/trunk/lib/Target/X86/X86InstrShiftRotate.td =================================================================== --- llvm/trunk/lib/Target/X86/X86InstrShiftRotate.td +++ llvm/trunk/lib/Target/X86/X86InstrShiftRotate.td @@ -591,19 +591,20 @@ def ROR8ri : Ii8<0xC0, MRM1r, (outs GR8 :$dst), (ins GR8 :$src1, u8imm:$src2), "ror{b}\t{$src2, $dst|$dst, $src2}", - [(set GR8:$dst, (rotr GR8:$src1, (i8 imm:$src2)))], IIC_SR>; + [(set GR8:$dst, (rotr GR8:$src1, (i8 relocImm:$src2)))], + IIC_SR>; def ROR16ri : Ii8<0xC1, MRM1r, (outs GR16:$dst), (ins GR16:$src1, u8imm:$src2), "ror{w}\t{$src2, $dst|$dst, $src2}", - [(set GR16:$dst, (rotr GR16:$src1, (i8 imm:$src2)))], + [(set GR16:$dst, (rotr GR16:$src1, (i8 relocImm:$src2)))], IIC_SR>, OpSize16; def ROR32ri : Ii8<0xC1, MRM1r, (outs GR32:$dst), (ins GR32:$src1, u8imm:$src2), "ror{l}\t{$src2, $dst|$dst, $src2}", - [(set GR32:$dst, (rotr GR32:$src1, (i8 imm:$src2)))], + [(set GR32:$dst, (rotr GR32:$src1, (i8 relocImm:$src2)))], IIC_SR>, OpSize32; def ROR64ri : RIi8<0xC1, MRM1r, (outs GR64:$dst), (ins GR64:$src1, u8imm:$src2), "ror{q}\t{$src2, $dst|$dst, $src2}", - [(set GR64:$dst, (rotr GR64:$src1, (i8 imm:$src2)))], + [(set GR64:$dst, (rotr GR64:$src1, (i8 relocImm:$src2)))], IIC_SR>; // Rotate by 1 Index: llvm/trunk/lib/Target/X86/X86Subtarget.cpp =================================================================== --- llvm/trunk/lib/Target/X86/X86Subtarget.cpp +++ llvm/trunk/lib/Target/X86/X86Subtarget.cpp @@ -92,6 +92,10 @@ if (TM.getCodeModel() == CodeModel::Large) return X86II::MO_NO_FLAG; + // Absolute symbols can be referenced directly. + if (GV && GV->isAbsoluteSymbolRef()) + return X86II::MO_NO_FLAG; + if (TM.shouldAssumeDSOLocal(M, GV)) return classifyLocalReference(GV); Index: llvm/trunk/test/CodeGen/X86/absolute-bit-mask.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/absolute-bit-mask.ll +++ llvm/trunk/test/CodeGen/X86/absolute-bit-mask.ll @@ -0,0 +1,61 @@ +; RUN: llc < %s | FileCheck %s +; RUN: llc -relocation-model=pic < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@bit_mask8 = external hidden global i8, !absolute_symbol !0 +@bit_mask32 = external hidden global i8, !absolute_symbol !1 +@bit_mask64 = external hidden global i8, !absolute_symbol !2 + +declare void @f() + +define void @foo8(i8* %ptr) { + %load = load i8, i8* %ptr + ; CHECK: testb $bit_mask8, (%rdi) + %and = and i8 %load, ptrtoint (i8* @bit_mask8 to i8) + %icmp = icmp eq i8 %and, 0 + br i1 %icmp, label %t, label %f + +t: + call void @f() + ret void + +f: + ret void +} + +define void @foo32(i32* %ptr) { + %load = load i32, i32* %ptr + ; CHECK: testl $bit_mask32, (%rdi) + %and = and i32 %load, ptrtoint (i8* @bit_mask32 to i32) + %icmp = icmp eq i32 %and, 0 + br i1 %icmp, label %t, label %f + +t: + call void @f() + ret void + +f: + ret void +} + +define void @foo64(i64* %ptr) { + %load = load i64, i64* %ptr + ; CHECK: movabsq $bit_mask64, %rax + ; CHECK: testq (%rdi), %rax + %and = and i64 %load, ptrtoint (i8* @bit_mask64 to i64) + %icmp = icmp eq i64 %and, 0 + br i1 %icmp, label %t, label %f + +t: + call void @f() + ret void + +f: + ret void +} + +!0 = !{i64 0, i64 256} +!1 = !{i64 0, i64 4294967296} +!2 = !{i64 -1, i64 -1} Index: llvm/trunk/test/CodeGen/X86/absolute-bt.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/absolute-bt.ll +++ llvm/trunk/test/CodeGen/X86/absolute-bt.ll @@ -0,0 +1,51 @@ +; RUN: llc < %s | FileCheck %s +; RUN: llc -relocation-model=pic < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@bit_mask8 = external hidden global i8, !absolute_symbol !0 +@bit_mask32 = external hidden global i8, !absolute_symbol !1 +@bit_mask64 = external hidden global i8, !absolute_symbol !2 + +declare void @f() + +define void @foo32(i32* %ptr) { + %load = load i32, i32* %ptr + %and = and i32 %load, 31 + %shl = shl i32 1, %and + %and2 = and i32 %shl, ptrtoint (i8* @bit_mask32 to i32) + ; CHECK: movl $bit_mask32, %eax + ; CHECK: btl %ecx, %eax + %icmp = icmp eq i32 %and2, 0 + br i1 %icmp, label %t, label %f + +t: + call void @f() + ret void + +f: + ret void +} + +define void @foo64(i64* %ptr) { + %load = load i64, i64* %ptr + %and = and i64 %load, 63 + %shl = shl i64 1, %and + %and2 = and i64 %shl, ptrtoint (i8* @bit_mask64 to i64) + ; CHECK: movabsq $bit_mask64, %rax + ; CHECK: btq %rcx, %rax + %icmp = icmp eq i64 %and2, 0 + br i1 %icmp, label %t, label %f + +t: + call void @f() + ret void + +f: + ret void +} + +!0 = !{i64 0, i64 256} +!1 = !{i64 0, i64 4294967296} +!2 = !{i64 -1, i64 -1} Index: llvm/trunk/test/CodeGen/X86/absolute-constant.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/absolute-constant.ll +++ llvm/trunk/test/CodeGen/X86/absolute-constant.ll @@ -0,0 +1,28 @@ +; RUN: llc < %s | FileCheck %s +; RUN: llc -relocation-model=pic < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@foo = external global i8, align 1, !absolute_symbol !0 + +define void @bar(i8* %x) { +entry: + %0 = load i8, i8* %x, align 1 + %conv = sext i8 %0 to i32 + ; CHECK: testb $foo, (%rdi) + %and = and i32 %conv, sext (i8 ptrtoint (i8* @foo to i8) to i32) + %tobool = icmp eq i32 %and, 0 + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %entry + tail call void (...) @xf() + br label %if.end + +if.end: ; preds = %entry, %if.then + ret void +} + +declare void @xf(...) + +!0 = !{i32 0, i32 256} Index: llvm/trunk/test/CodeGen/X86/absolute-rotate.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/absolute-rotate.ll +++ llvm/trunk/test/CodeGen/X86/absolute-rotate.ll @@ -0,0 +1,27 @@ +; RUN: llc < %s | FileCheck %s +; RUN: llc -relocation-model=pic < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@align = external hidden global i8, !absolute_symbol !0 + +declare void @f() + +define void @foo(i64 %val) { + %shr = lshr i64 %val, zext (i8 ptrtoint (i8* @align to i8) to i64) + %shl = shl i64 %val, zext (i8 sub (i8 64, i8 ptrtoint (i8* @align to i8)) to i64) + ; CHECK: rorq $align, %rdi + %ror = or i64 %shr, %shl + %cmp = icmp ult i64 %ror, 109 + br i1 %cmp, label %t, label %f + +t: + call void @f() + ret void + +f: + ret void +} + +!0 = !{i64 0, i64 256} Index: llvm/trunk/test/Transforms/FunctionAttrs/nonnull-global.ll =================================================================== --- llvm/trunk/test/Transforms/FunctionAttrs/nonnull-global.ll +++ llvm/trunk/test/Transforms/FunctionAttrs/nonnull-global.ll @@ -0,0 +1,10 @@ +; RUN: opt -S -functionattrs %s | FileCheck %s + +@a = external global i8, !absolute_symbol !0 + +; CHECK-NOT: define nonnull +define i8* @foo() { + ret i8* @a +} + +!0 = !{i64 0, i64 256}