Index: llvm/docs/LangRef.rst =================================================================== --- llvm/docs/LangRef.rst +++ llvm/docs/LangRef.rst @@ -4557,7 +4557,7 @@ '``range``' Metadata ^^^^^^^^^^^^^^^^^^^^ -``range`` metadata may be attached only to ``load``, ``call`` and ``invoke`` of +``range`` metadata may be attached to ``load``, ``call`` and ``invoke`` of integer types. It expresses the possible ranges the loaded value or the value returned by the called function at this call site is in. The ranges are represented with a flattened list of integers. The loaded value or the value @@ -4574,6 +4574,12 @@ In addition, the pairs must be in signed order of the lower bound and they must be non-contiguous. +``range`` may also 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. + Examples: .. code-block:: llvm @@ -4583,11 +4589,15 @@ %c = call i8 @foo(), !range !2 ; Can only be 0, 1, 3, 4 or 5 %d = invoke i8 @bar() to label %cont unwind label %lpad, !range !3 ; Can only be -2, -1, 3, 4 or 5 + + @e = external global i8, !range !4 ; Can only have an address in range [0,256) + ... !0 = !{ i8 0, i8 2 } !1 = !{ i8 255, i8 2 } !2 = !{ i8 0, i8 2, i8 3, i8 6 } !3 = !{ i8 -2, i8 0, i8 3, i8 6 } + !4 = !{ i64 0, i64 256 } '``unpredictable``' Metadata ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Index: llvm/include/llvm/IR/GlobalValue.h =================================================================== --- llvm/include/llvm/IR/GlobalValue.h +++ llvm/include/llvm/IR/GlobalValue.h @@ -25,6 +25,7 @@ namespace llvm { class Comdat; +class ConstantRange; class Error; class GlobalObject; class PointerType; @@ -502,6 +503,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/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -3346,11 +3346,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/lib/IR/Globals.cpp =================================================================== --- llvm/lib/IR/Globals.cpp +++ llvm/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_range); +} + +Optional GlobalValue::getAbsoluteSymbolRange() const { + auto *GO = dyn_cast(this); + if (!GO) + return None; + + MDNode *MD = GO->getMetadata(LLVMContext::MD_range); + if (!MD) + return None; + + return getConstantRangeFromMetadata(*MD); +} + //===----------------------------------------------------------------------===// // GlobalVariable Implementation //===----------------------------------------------------------------------===// Index: llvm/lib/Target/X86/X86ISelDAGToDAG.cpp =================================================================== --- llvm/lib/Target/X86/X86ISelDAGToDAG.cpp +++ llvm/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" @@ -1573,7 +1574,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, @@ -1712,19 +1721,40 @@ 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; + // 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::TargetConstantPool || Opc == ISD::TargetJumpTable || - Opc == ISD::TargetExternalSymbol || Opc == ISD::TargetGlobalAddress || - Opc == ISD::TargetGlobalTLSAddress || Opc == ISD::MCSymbol || - Opc == ISD::TargetBlockAddress) { + if (Opc != ISD::TargetGlobalAddress || !WasTruncated) { Op = N.getOperand(0); - return true; + // We can only select the operand directly if we didn't have to look past a + // truncate. + return !WasTruncated; } - return false; + // 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; } bool X86DAGToDAGISel::tryFoldLoad(SDNode *P, SDValue N, Index: llvm/lib/Target/X86/X86ISelLowering.h =================================================================== --- llvm/lib/Target/X86/X86ISelLowering.h +++ llvm/lib/Target/X86/X86ISelLowering.h @@ -1117,7 +1117,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/lib/Target/X86/X86ISelLowering.cpp =================================================================== --- llvm/lib/Target/X86/X86ISelLowering.cpp +++ llvm/lib/Target/X86/X86ISelLowering.cpp @@ -13193,7 +13193,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)) @@ -13322,7 +13326,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/lib/Target/X86/X86InstrInfo.td =================================================================== --- llvm/lib/Target/X86/X86InstrInfo.td +++ llvm/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/lib/Target/X86/X86InstrShiftRotate.td =================================================================== --- llvm/lib/Target/X86/X86InstrShiftRotate.td +++ llvm/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/lib/Target/X86/X86Subtarget.cpp =================================================================== --- llvm/lib/Target/X86/X86Subtarget.cpp +++ llvm/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/test/CodeGen/X86/absolute-bit-mask.ll =================================================================== --- /dev/null +++ llvm/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, !range !0 +@bit_mask32 = external hidden global i8, !range !1 +@bit_mask64 = external hidden global i8, !range !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/test/CodeGen/X86/absolute-bt.ll =================================================================== --- /dev/null +++ llvm/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, !range !0 +@bit_mask32 = external hidden global i8, !range !1 +@bit_mask64 = external hidden global i8, !range !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/test/CodeGen/X86/absolute-constant.ll =================================================================== --- /dev/null +++ llvm/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, !range !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/test/CodeGen/X86/absolute-rotate.ll =================================================================== --- /dev/null +++ llvm/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, !range !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/test/Transforms/FunctionAttrs/nonnull-global.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/FunctionAttrs/nonnull-global.ll @@ -0,0 +1,10 @@ +; RUN: opt -S -functionattrs %s | FileCheck %s + +@a = external global i8, !range !0 + +; CHECK-NOT: define nonnull +define i8* @foo() { + ret i8* @a +} + +!0 = !{i64 0, i64 256}