Index: clang/lib/CodeGen/CGVTables.cpp =================================================================== --- clang/lib/CodeGen/CGVTables.cpp +++ clang/lib/CodeGen/CGVTables.cpp @@ -410,14 +410,14 @@ // FIXME: re-use FnInfo in this computation. llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk); - llvm::GlobalValue *Entry; + llvm::GlobalPointer *Entry; // Strip off a bitcast if we got one back. if (llvm::ConstantExpr *CE = dyn_cast(C)) { assert(CE->getOpcode() == llvm::Instruction::BitCast); - Entry = cast(CE->getOperand(0)); + Entry = cast(CE->getOperand(0)); } else { - Entry = cast(C); + Entry = cast(C); } // There's already a declaration with the same name, check if it has the same @@ -432,7 +432,7 @@ // Remove the name from the old thunk function and get a new thunk. OldThunkFn->setName(StringRef()); - Entry = cast(CGM.GetAddrOfThunk(GD, Thunk)); + Entry = cast(CGM.GetAddrOfThunk(GD, Thunk)); // If needed, replace the old thunk with a bitcast. if (!OldThunkFn->use_empty()) { Index: clang/lib/CodeGen/CodeGenModule.h =================================================================== --- clang/lib/CodeGen/CodeGenModule.h +++ clang/lib/CodeGen/CodeGenModule.h @@ -379,8 +379,8 @@ /// Map used to track internal linkage functions declared within /// extern "C" regions. - typedef llvm::MapVector StaticExternCMap; + typedef llvm::MapVector + StaticExternCMap; StaticExternCMap StaticExternCValues; /// \brief thread_local variables defined or used in this TU. @@ -859,7 +859,7 @@ /// extern "C" linkage specification, prepare to emit an alias for it /// to the expected name. template - void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV); + void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalObject *GO); /// Add a global to a list to be added to the llvm.used metadata. void addUsedGlobal(llvm::GlobalValue *GV); Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -1884,7 +1884,7 @@ } if ((isa(Entry) || isa(Entry)) && - (Entry->getType()->getElementType() == Ty)) { + (cast(Entry)->getType()->getElementType() == Ty)) { return Entry; } @@ -1932,8 +1932,7 @@ Entry->removeDeadConstantUsers(); } - llvm::Constant *BC = llvm::ConstantExpr::getBitCast( - F, Entry->getType()->getElementType()->getPointerTo()); + llvm::Constant *BC = llvm::ConstantExpr::getBitCast(F, Entry->getType()); addGlobalValReplacement(Entry, BC); } @@ -2126,8 +2125,9 @@ } // Make sure the result is of the correct type. - if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) - return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); + if (auto *GP = dyn_cast(Entry)) + if (GP->getType()->getAddressSpace() != Ty->getAddressSpace()) + return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); // (If global is requested for a definition, we always need to create a new // global, not just return a bitcast.) @@ -2346,7 +2346,7 @@ template void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, - llvm::GlobalValue *GV) { + llvm::GlobalObject *GO) { if (!getLangOpts().CPlusPlus) return; @@ -2369,7 +2369,7 @@ // specification. Make a note of that so we can give it the "expected" // mangled name if nothing else is using that name. std::pair R = - StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); + StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GO)); // If we have multiple internal linkage entities with the same name // in extern "C" regions, none of them gets that name. @@ -2905,7 +2905,7 @@ llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); // Get or create the prototype for the function. - if (!GV || (GV->getType()->getElementType() != Ty)) + if (!GV || (cast(GV)->getType()->getElementType() != Ty)) GV = cast(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/true, /*IsForDefinition=*/true)); @@ -4130,7 +4130,7 @@ return; for (auto &I : StaticExternCValues) { IdentifierInfo *Name = I.first; - llvm::GlobalValue *Val = I.second; + llvm::GlobalObject *Val = I.second; if (Val && !getModule().getNamedValue(Name->getName())) addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); } Index: clang/lib/CodeGen/ItaniumCXXABI.cpp =================================================================== --- clang/lib/CodeGen/ItaniumCXXABI.cpp +++ clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -1528,7 +1528,7 @@ llvm::Constant * ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base, const CXXRecordDecl *VTableClass) { - llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits()); + llvm::GlobalVariable *VTable = getAddrOfVTable(VTableClass, CharUnits()); // Find the appropriate vtable within the vtable group. uint64_t AddressPoint = CGM.getItaniumVTableContext() @@ -3483,7 +3483,7 @@ if (Entry && !Entry->isDeclaration()) return; - auto *Aliasee = cast(CGM.GetAddrOfGlobal(TargetDecl)); + auto *Aliasee = cast(CGM.GetAddrOfGlobal(TargetDecl)); // Create the alias with no name. auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee); Index: llvm/include/llvm/CodeGen/SelectionDAGNodes.h =================================================================== --- llvm/include/llvm/CodeGen/SelectionDAGNodes.h +++ llvm/include/llvm/CodeGen/SelectionDAGNodes.h @@ -1431,8 +1431,6 @@ const GlobalValue *getGlobal() const { return TheGlobal; } int64_t getOffset() const { return Offset; } unsigned char getTargetFlags() const { return TargetFlags; } - // Return the address space this GlobalAddress belongs to. - unsigned getAddressSpace() const; static bool classof(const SDNode *N) { return N->getOpcode() == ISD::GlobalAddress || Index: llvm/include/llvm/IR/GlobalAlias.h =================================================================== --- llvm/include/llvm/IR/GlobalAlias.h +++ llvm/include/llvm/IR/GlobalAlias.h @@ -50,14 +50,14 @@ // The module is taken from the Aliasee. static GlobalAlias *create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, - GlobalValue *Aliasee); + GlobalPointer *Aliasee); // Type, Parent and AddressSpace taken from the Aliasee. static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name, - GlobalValue *Aliasee); + GlobalPointer *Aliasee); // Linkage, Type, Parent and AddressSpace taken from the Aliasee. - static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee); + static GlobalAlias *create(const Twine &Name, GlobalPointer *Aliasee); /// removeFromParent - This method unlinks 'this' from the containing module, /// but does not delete it. Index: llvm/include/llvm/IR/GlobalIndirectSymbol.h =================================================================== --- llvm/include/llvm/IR/GlobalIndirectSymbol.h +++ llvm/include/llvm/IR/GlobalIndirectSymbol.h @@ -16,12 +16,12 @@ #ifndef LLVM_IR_GLOBALINDIRECTSYMBOL_H #define LLVM_IR_GLOBALINDIRECTSYMBOL_H -#include "llvm/IR/GlobalValue.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/OperandTraits.h" namespace llvm { -class GlobalIndirectSymbol : public GlobalValue { +class GlobalIndirectSymbol : public GlobalPointer { void operator=(const GlobalIndirectSymbol &) = delete; GlobalIndirectSymbol(const GlobalIndirectSymbol &) = delete; Index: llvm/include/llvm/IR/GlobalObject.h =================================================================== --- llvm/include/llvm/IR/GlobalObject.h +++ llvm/include/llvm/IR/GlobalObject.h @@ -16,7 +16,7 @@ #define LLVM_IR_GLOBALOBJECT_H #include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/GlobalValue.h" +#include "llvm/IR/GlobalPointer.h" namespace llvm { class Comdat; @@ -24,14 +24,14 @@ class Metadata; class Module; -class GlobalObject : public GlobalValue { +class GlobalObject : public GlobalPointer { GlobalObject(const GlobalObject &) = delete; protected: GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace = 0) - : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace), + : GlobalPointer(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace), ObjComdat(nullptr) { setGlobalValueSubClassData(0); } Index: llvm/include/llvm/IR/GlobalPointer.h =================================================================== --- /dev/null +++ llvm/include/llvm/IR/GlobalPointer.h @@ -0,0 +1,40 @@ +//===-- llvm/IR/GlobalPointer.h - Global pointer values ---------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// GlobalPointer is the common base class of all globally definable pointer +// values. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_IR_GLOBALPOINTER_H +#define LLVM_IR_GLOBALPOINTER_H + +#include "llvm/IR/GlobalValue.h" + +namespace llvm { + +class GlobalPointer : public GlobalValue { +protected: + GlobalPointer(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, + LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace) + : GlobalValue(PointerType::get(Ty, AddressSpace), VTy, Ops, NumOps, + Linkage, Name), ValueType(Ty) {} + + Type *ValueType; + +public: + /// GlobalPointers are always pointers. + PointerType *getType() const { return cast(User::getType()); } + + Type *getValueType() const { return ValueType; } +}; + +} // namespace llvm + +#endif Index: llvm/include/llvm/IR/GlobalValue.h =================================================================== --- llvm/include/llvm/IR/GlobalValue.h +++ llvm/include/llvm/IR/GlobalValue.h @@ -68,16 +68,15 @@ protected: GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, - LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace) - : Constant(PointerType::get(Ty, AddressSpace), VTy, Ops, NumOps), - ValueType(Ty), Linkage(Linkage), Visibility(DefaultVisibility), + LinkageTypes Linkage, const Twine &Name) + : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage), + Visibility(DefaultVisibility), UnnamedAddrVal(unsigned(UnnamedAddr::None)), DllStorageClass(DefaultStorageClass), ThreadLocal(NotThreadLocal), IntID((Intrinsic::ID)0U), Parent(nullptr) { setName(Name); } - Type *ValueType; // All bitfields use unsigned as the underlying type so that MSVC will pack // them. unsigned Linkage : 4; // The linkage of this global @@ -232,11 +231,6 @@ bool hasSection() const { return !getSection().empty(); } StringRef getSection() const; - /// Global values are always pointers. - PointerType *getType() const { return cast(User::getType()); } - - Type *getValueType() const { return ValueType; } - static LinkageTypes getLinkOnceLinkage(bool ODR) { return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; } Index: llvm/include/llvm/LTO/LTO.h =================================================================== --- llvm/include/llvm/LTO/LTO.h +++ llvm/include/llvm/LTO/LTO.h @@ -21,6 +21,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/CodeGen/Analysis.h" #include "llvm/IR/DiagnosticInfo.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/LTO/Config.h" #include "llvm/Linker/IRMover.h" @@ -198,7 +199,7 @@ if (!GV) return 0; return GV->getParent()->getDataLayout().getTypeAllocSize( - GV->getType()->getElementType()); + cast(GV)->getValueType()); } unsigned getCommonAlignment() const { assert(Flags & object::BasicSymbolRef::SF_Common); Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -28,6 +28,7 @@ #include "llvm/IR/Dominators.h" #include "llvm/IR/GetElementPtrTypeIterator.h" #include "llvm/IR/GlobalAlias.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" @@ -3346,12 +3347,12 @@ 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. + // A global pointer 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. - if (const GlobalValue *GV = dyn_cast(V)) - return !GV->hasExternalWeakLinkage() && - GV->getType()->getAddressSpace() == 0; + if (const auto *GP = dyn_cast(V)) + return !GP->hasExternalWeakLinkage() && + GP->getType()->getAddressSpace() == 0; // A Load tagged with nonnull metadata is never null. if (const LoadInst *LI = dyn_cast(V)) Index: llvm/lib/AsmParser/LLParser.cpp =================================================================== --- llvm/lib/AsmParser/LLParser.cpp +++ llvm/lib/AsmParser/LLParser.cpp @@ -29,6 +29,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/GlobalIFunc.h" #include "llvm/IR/GlobalObject.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/InlineAsm.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" @@ -911,7 +912,8 @@ Name, nullptr, GlobalVariable::NotThreadLocal, AddrSpace); } else { - if (GVal->getValueType() != Ty) + if (!isa(GVal) || + cast(GVal)->getValueType() != Ty) return Error(TyLoc, "forward reference and definition of global have different types"); Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -1134,7 +1134,7 @@ std::map GCMap; unsigned MaxAlignment = 0; unsigned MaxGlobalType = 0; - for (const GlobalValue &GV : M.globals()) { + for (const GlobalVariable &GV : M.globals()) { MaxAlignment = std::max(MaxAlignment, GV.getAlignment()); MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType())); if (GV.hasSection()) { Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -7075,11 +7075,6 @@ } } -// getAddressSpace - Return the address space this GlobalAddress belongs to. -unsigned GlobalAddressSDNode::getAddressSpace() const { - return getGlobal()->getType()->getAddressSpace(); -} - Type *ConstantPoolSDNode::getType() const { if (isMachineConstantPoolEntry()) Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -21,6 +21,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" @@ -433,7 +434,8 @@ const TargetMachine &TM) const { // We may only use a PLT-relative relocation to refer to unnamed_addr // functions. - if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) + if (!LHS->hasGlobalUnnamedAddr() || !isa(LHS) || + !cast(LHS)->getValueType()->isFunctionTy()) return nullptr; // Basic sanity checks. @@ -1140,7 +1142,8 @@ getMangler().getNameWithPrefix(OS, GV, false); } - if (!GV->getValueType()->isFunctionTy()) { + if (!isa(GV) || + !cast(GV)->getValueType()->isFunctionTy()) { if (TT.isKnownWindowsMSVCEnvironment()) OS << ",DATA"; else Index: llvm/lib/IR/Globals.cpp =================================================================== --- llvm/lib/IR/Globals.cpp +++ llvm/lib/IR/Globals.cpp @@ -17,6 +17,7 @@ #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/GlobalAlias.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Module.h" @@ -320,7 +321,7 @@ GlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Symbol) - : GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) { + : GlobalPointer(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) { Op<0>() = Symbol; } @@ -352,18 +353,18 @@ GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, - GlobalValue *Aliasee) { + GlobalPointer *Aliasee) { return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); } GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, - GlobalValue *Aliasee) { + GlobalPointer *Aliasee) { PointerType *PTy = Aliasee->getType(); return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, Aliasee); } -GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { +GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalPointer *Aliasee) { return create(Aliasee->getLinkage(), Name, Aliasee); } Index: llvm/lib/Linker/IRMover.cpp =================================================================== --- llvm/lib/Linker/IRMover.cpp +++ llvm/lib/Linker/IRMover.cpp @@ -16,6 +16,7 @@ #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/GVMaterializer.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/TypeFinder.h" #include "llvm/Support/Error.h" @@ -631,11 +632,11 @@ NewGV = copyGlobalAliasProto(cast(SGV)); else NewGV = new GlobalVariable( - DstM, TypeMap.get(SGV->getValueType()), + DstM, TypeMap.get(cast(SGV)->getValueType()), /*isConstant*/ false, GlobalValue::ExternalLinkage, /*init*/ nullptr, SGV->getName(), /*insertbefore*/ nullptr, SGV->getThreadLocalMode(), - SGV->getType()->getAddressSpace()); + cast(SGV)->getType()->getAddressSpace()); } if (ForDefinition) @@ -668,7 +669,7 @@ /// types 'Foo' but one got renamed when the module was loaded into the same /// LLVMContext. void IRLinker::computeTypeMapping() { - for (GlobalValue &SGV : SrcM->globals()) { + for (GlobalVariable &SGV : SrcM->globals()) { GlobalValue *DGV = getLinkedToGlobal(&SGV); if (!DGV) continue; @@ -679,7 +680,7 @@ } // Unify the element type of appending arrays. - ArrayType *DAT = cast(DGV->getValueType()); + ArrayType *DAT = cast(cast(DGV)->getValueType()); ArrayType *SAT = cast(SGV.getValueType()); TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); } Index: llvm/lib/Linker/LinkModules.cpp =================================================================== --- llvm/lib/Linker/LinkModules.cpp +++ llvm/lib/Linker/LinkModules.cpp @@ -16,6 +16,7 @@ #include "llvm/ADT/SetVector.h" #include "llvm/ADT/StringSet.h" #include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/LLVMContext.h" #include "llvm/Linker/Linker.h" #include "llvm/Support/Error.h" @@ -317,8 +318,10 @@ } const DataLayout &DL = Dest.getParent()->getDataLayout(); - uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType()); - uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType()); + uint64_t DestSize = + DL.getTypeAllocSize(cast(Dest).getValueType()); + uint64_t SrcSize = + DL.getTypeAllocSize(cast(Src).getValueType()); LinkFromSrc = SrcSize > DestSize; return false; } Index: llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp =================================================================== --- llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp +++ llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp @@ -16,6 +16,7 @@ #include "llvm/ADT/APSInt.h" #include "llvm/CodeGen/SelectionDAGISel.h" #include "llvm/IR/Function.h" // To access function attributes. +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Intrinsics.h" #include "llvm/Support/Debug.h" @@ -696,8 +697,9 @@ const GlobalValue *GV = GAN->getGlobal(); unsigned Alignment = GV->getAlignment(); - Type *Ty = GV->getValueType(); - if (Alignment == 0 && Ty->isSized()) + Type *Ty = isa(GV) ? cast(GV)->getValueType() + : nullptr; + if (Alignment == 0 && Ty && Ty->isSized()) Alignment = DL.getABITypeAlignment(Ty); if (Alignment >= Size) Index: llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp +++ llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp @@ -28,6 +28,7 @@ #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/DiagnosticInfo.h" +#include "llvm/IR/GlobalPointer.h" #include "SIInstrInfo.h" using namespace llvm; @@ -845,21 +846,22 @@ const DataLayout &DL = DAG.getDataLayout(); GlobalAddressSDNode *G = cast(Op); - const GlobalValue *GV = G->getGlobal(); - switch (G->getAddressSpace()) { - case AMDGPUAS::LOCAL_ADDRESS: { - // XXX: What does the value of G->getOffset() mean? - assert(G->getOffset() == 0 && - "Do not know what to do with an non-zero offset"); + if (const auto *GP = dyn_cast(G->getGlobal())) { + switch (GP->getType()->getAddressSpace()) { + case AMDGPUAS::LOCAL_ADDRESS: { + // XXX: What does the value of G->getOffset() mean? + assert(G->getOffset() == 0 && + "Do not know what to do with an non-zero offset"); - // TODO: We could emit code to handle the initialization somewhere. - if (hasDefinedInitializer(GV)) - break; + // TODO: We could emit code to handle the initialization somewhere. + if (hasDefinedInitializer(GP)) + break; - unsigned Offset = MFI->allocateLDSGlobal(DL, *GV); - return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType()); - } + unsigned Offset = MFI->allocateLDSGlobal(DL, *GP); + return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType()); + } + } } const Function &Fn = *DAG.getMachineFunction().getFunction(); Index: llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.h =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.h +++ llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.h @@ -15,10 +15,12 @@ namespace llvm { +class GlobalPointer; + class AMDGPUMachineFunction : public MachineFunctionInfo { /// A map to keep track of local memory objects and their offsets within the /// local memory space. - SmallDenseMap LocalMemoryObjects; + SmallDenseMap LocalMemoryObjects; uint64_t KernArgSize; unsigned MaxKernArgAlign; @@ -66,7 +68,7 @@ return IsKernel; } - unsigned allocateLDSGlobal(const DataLayout &DL, const GlobalValue &GV); + unsigned allocateLDSGlobal(const DataLayout &DL, const GlobalPointer &GP); }; } Index: llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.cpp +++ llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.cpp @@ -9,6 +9,7 @@ #include "AMDGPUMachineFunction.h" #include "AMDGPUSubtarget.h" +#include "llvm/IR/GlobalPointer.h" using namespace llvm; @@ -26,14 +27,14 @@ } unsigned AMDGPUMachineFunction::allocateLDSGlobal(const DataLayout &DL, - const GlobalValue &GV) { - auto Entry = LocalMemoryObjects.insert(std::make_pair(&GV, 0)); + const GlobalPointer &GP) { + auto Entry = LocalMemoryObjects.insert(std::make_pair(&GP, 0)); if (!Entry.second) return Entry.first->second; - unsigned Align = GV.getAlignment(); + unsigned Align = GP.getAlignment(); if (Align == 0) - Align = DL.getABITypeAlignment(GV.getValueType()); + Align = DL.getABITypeAlignment(GP.getValueType()); /// TODO: We should sort these to minimize wasted space due to alignment /// padding. Currently the padding is decided by the first encountered use @@ -41,7 +42,7 @@ unsigned Offset = LDSSize = alignTo(LDSSize, Align); Entry.first->second = Offset; - LDSSize += DL.getTypeAllocSize(GV.getValueType()); + LDSSize += DL.getTypeAllocSize(GP.getValueType()); return Offset; } Index: llvm/lib/Target/AMDGPU/R600ISelLowering.cpp =================================================================== --- llvm/lib/Target/AMDGPU/R600ISelLowering.cpp +++ llvm/lib/Target/AMDGPU/R600ISelLowering.cpp @@ -673,7 +673,7 @@ SelectionDAG &DAG) const { GlobalAddressSDNode *GSD = cast(Op); - if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) + if (!AMDGPU::isReadOnlySegment(GSD->getGlobal())) return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); const DataLayout &DL = DAG.getDataLayout(); Index: llvm/lib/Target/AMDGPU/SIISelLowering.cpp =================================================================== --- llvm/lib/Target/AMDGPU/SIISelLowering.cpp +++ llvm/lib/Target/AMDGPU/SIISelLowering.cpp @@ -21,6 +21,7 @@ #include "AMDGPU.h" #include "AMDGPUIntrinsicInfo.h" #include "AMDGPUSubtarget.h" +#include "Utils/AMDGPUBaseInfo.h" #include "SIISelLowering.h" #include "SIInstrInfo.h" #include "SIMachineFunctionInfo.h" @@ -1819,13 +1820,12 @@ bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const { const Triple &TT = getTargetMachine().getTargetTriple(); - return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS && + return AMDGPU::isReadOnlySegment(GV) && AMDGPU::shouldEmitConstantsToTextSection(TT); } bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const { - return (GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS || - GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) && + return (AMDGPU::isGlobalSegment(GV) || AMDGPU::isReadOnlySegment(GV)) && !shouldEmitFixup(GV) && !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV); } @@ -2021,8 +2021,8 @@ bool SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { // We can fold offsets for anything that doesn't require a GOT relocation. - return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS || - GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) && + return (AMDGPU::isGlobalSegment(GA->getGlobal()) || + AMDGPU::isReadOnlySegment(GA->getGlobal())) && !shouldEmitGOTReloc(GA->getGlobal()); } @@ -2072,8 +2072,8 @@ SelectionDAG &DAG) const { GlobalAddressSDNode *GSD = cast(Op); - if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS && - GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS) + if (!AMDGPU::isReadOnlySegment(GSD->getGlobal()) && + !AMDGPU::isGlobalSegment(GSD->getGlobal())) return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); SDLoc DL(GSD); Index: llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp =================================================================== --- llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp +++ llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp @@ -11,6 +11,7 @@ #include "SIDefines.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalValue.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCInstrInfo.h" @@ -153,15 +154,21 @@ } bool isGroupSegment(const GlobalValue *GV) { - return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS; + return isa(GV) && + cast(GV)->getType()->getAddressSpace() == + AMDGPUAS::LOCAL_ADDRESS; } bool isGlobalSegment(const GlobalValue *GV) { - return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS; + return isa(GV) && + cast(GV)->getType()->getAddressSpace() == + AMDGPUAS::GLOBAL_ADDRESS; } bool isReadOnlySegment(const GlobalValue *GV) { - return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS; + return isa(GV) && + cast(GV)->getType()->getAddressSpace() == + AMDGPUAS::CONSTANT_ADDRESS; } bool shouldEmitConstantsToTextSection(const Triple &TT) { Index: llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp =================================================================== --- llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -33,6 +33,7 @@ #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" @@ -1755,7 +1756,8 @@ } if (const GlobalValue *GVar = dyn_cast(CPV)) { bool IsNonGenericPointer = false; - if (GVar->getType()->getAddressSpace() != 0) { + if (isa(GVar) && + cast(GVar)->getType()->getAddressSpace() != 0) { IsNonGenericPointer = true; } if (EmitGeneric && !isa(CPV) && !IsNonGenericPointer) { Index: llvm/lib/Target/PowerPC/PPCISelLowering.cpp =================================================================== --- llvm/lib/Target/PowerPC/PPCISelLowering.cpp +++ llvm/lib/Target/PowerPC/PPCISelLowering.cpp @@ -35,6 +35,7 @@ #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/Intrinsics.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" @@ -4340,7 +4341,11 @@ Callee.getOpcode() == ISD::TargetGlobalTLSAddress) return false; - return G->getGlobal()->getValueType()->isFunctionTy(); + auto *GP = dyn_cast(G->getGlobal()); + if (!GP) + return false; + + return GP->getValueType()->isFunctionTy(); } return false; Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -28,6 +28,7 @@ #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/CodeGen/MachineInstr.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" @@ -247,10 +248,10 @@ } const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) { - if (const GlobalValue *GV = dyn_cast(CV)) - if (GV->getValueType()->isFunctionTy()) + if (const auto *GP = dyn_cast(CV)) + if (GP->getValueType()->isFunctionTy()) return MCSymbolRefExpr::create( - getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext); + getSymbol(GP), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext); return AsmPrinter::lowerConstant(CV); } Index: llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -25,6 +25,7 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/Intrinsics.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" @@ -628,7 +629,8 @@ EVT VT = Op.getValueType(); assert(GA->getTargetFlags() == 0 && "Unexpected target flags on generic GlobalAddressSDNode"); - if (GA->getAddressSpace() != 0) + if (!isa(GA->getGlobal()) || + cast(GA->getGlobal())->getType()->getAddressSpace() != 0) fail(DL, DAG, "WebAssembly only expects the 0 address space"); return DAG.getNode( WebAssemblyISD::Wrapper, DL, VT, Index: llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -18,6 +18,7 @@ #include "llvm/CodeGen/AsmPrinter.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" @@ -94,12 +95,16 @@ llvm_unreachable("unknown floating point immediate type"); break; } - case MachineOperand::MO_GlobalAddress: + case MachineOperand::MO_GlobalAddress: { assert(MO.getTargetFlags() == 0 && "WebAssembly does not use target flags on GlobalAddresses"); + bool IsFunction = false; + if (auto *GP = dyn_cast(MO.getGlobal())) + IsFunction = GP->getValueType()->isFunctionTy(); MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), - MO.getGlobal()->getValueType()->isFunctionTy()); + IsFunction); break; + } case MachineOperand::MO_ExternalSymbol: // The target flag indicates whether this is a symbol for a // variable or a function. Index: llvm/lib/Target/XCore/XCoreISelLowering.cpp =================================================================== --- llvm/lib/Target/XCore/XCoreISelLowering.cpp +++ llvm/lib/Target/XCore/XCoreISelLowering.cpp @@ -30,6 +30,7 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalAlias.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Intrinsics.h" #include "llvm/Support/Debug.h" @@ -254,8 +255,9 @@ // FIXME there is no actual debug info here SDLoc dl(GA); - if (GV->getValueType()->isFunctionTy()) - return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA); + if (auto *GP = dyn_cast(GV)) + if (GP->getValueType()->isFunctionTy()) + return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA); const auto *GVar = dyn_cast(GV); if ((GV->hasSection() && GV->getSection().startswith(".cp.")) || @@ -269,7 +271,11 @@ if (XTL.getTargetMachine().getCodeModel() == CodeModel::Small) return true; - Type *ObjType = GV->getValueType(); + auto *GP = dyn_cast(GV); + if (!GP) + return false; + + Type *ObjType = GP->getValueType(); if (!ObjType->isSized()) return false; Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -1364,7 +1364,7 @@ // Insert a store of null into each global. for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { - Type *ValTy = cast(FieldGlobals[i])->getValueType(); + Type *ValTy = cast(FieldGlobals[i])->getValueType(); Constant *Null = Constant::getNullValue(ValTy); new StoreInst(Null, FieldGlobals[i], SI); } Index: llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp +++ llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp @@ -132,7 +132,7 @@ namespace { -StringRef GetGlobalTypeString(const GlobalValue &G) { +StringRef GetGlobalTypeString(const GlobalAlias &G) { // Types of GlobalVariables are always pointer types. Type *GType = G.getValueType(); // For now we support blacklisting struct types only. Index: llvm/tools/llvm-nm/llvm-nm.cpp =================================================================== --- llvm/tools/llvm-nm/llvm-nm.cpp +++ llvm/tools/llvm-nm/llvm-nm.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalAlias.h" +#include "llvm/IR/GlobalPointer.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" @@ -878,9 +879,11 @@ } static char getSymbolNMTypeChar(const GlobalValue &GV) { + if (!isa(GV)) + return 'a'; // FIXME: should we print 'b'? At the IR level we cannot be sure if this // will be in bss or not, but we could approximate. - return GV.getValueType()->isFunctionTy() ? 't' : 'd'; + return cast(GV).getValueType()->isFunctionTy() ? 't' : 'd'; } static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {