Index: llvm/include/llvm/IR/GlobalValue.h =================================================================== --- llvm/include/llvm/IR/GlobalValue.h +++ llvm/include/llvm/IR/GlobalValue.h @@ -26,6 +26,7 @@ namespace llvm { class Comdat; +class ConstantRange; class PointerType; class Module; @@ -492,6 +493,13 @@ // increased. bool canIncreaseAlignment() const; + /// 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/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" @@ -213,6 +214,26 @@ return true; } +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,30 @@ return true; } + 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; 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; + return !WasTruncated; } - return false; + auto *GA = cast(N.getOperand(0)); + Optional CR = GA->getGlobal()->getAbsoluteSymbolRange(); + if (!CR || CR->getUnsignedMax().uge(1ull << VT.getSizeInBits())) + return false; + + 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 @@ -13057,7 +13057,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)) @@ -13186,7 +13190,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 @@ -947,10 +947,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}