diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h --- a/clang/lib/CodeGen/CGOpenMPRuntime.h +++ b/clang/lib/CodeGen/CGOpenMPRuntime.h @@ -786,9 +786,9 @@ /// \param Ty Type of the global variable. If it is exist already the type /// must be the same. /// \param Name Name of the variable. - llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty, - const llvm::Twine &Name, - unsigned AddressSpace = 0); + llvm::Constant * + getOrCreateInternalVariable(llvm::Type *Ty, const llvm::Twine &Name, + LLVM_DEFAULT_AS_PARAM(AddressSpace)); /// Set of threadprivate variables with the generated initializer. llvm::StringSet<> ThreadPrivateWithDefinition; diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h --- a/llvm/include/llvm/Analysis/TargetTransformInfo.h +++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h @@ -675,7 +675,7 @@ /// TODO: Handle pre/postinc as well. int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, - unsigned AddrSpace = 0) const; + LLVM_DEFAULT_AS_PARAM(AddrSpace)) const; /// Return true if the loop strength reduce pass should make /// Instruction* based TTI queries to isLegalAddressingMode(). This is @@ -778,7 +778,7 @@ /// Determine if the target supports unaligned memory accesses. bool allowsMisalignedMemoryAccesses(LLVMContext &Context, unsigned BitWidth, - unsigned AddressSpace = 0, + LLVM_DEFAULT_AS_PARAM(AddressSpace), unsigned Alignment = 1, bool *Fast = nullptr) const; diff --git a/llvm/include/llvm/CodeGen/MachineMemOperand.h b/llvm/include/llvm/CodeGen/MachineMemOperand.h --- a/llvm/include/llvm/CodeGen/MachineMemOperand.h +++ b/llvm/include/llvm/CodeGen/MachineMemOperand.h @@ -59,7 +59,8 @@ AddrSpace = v ? v->getAddressSpace() : 0; } - explicit MachinePointerInfo(unsigned AddressSpace = 0, int64_t offset = 0) + explicit MachinePointerInfo(LLVM_DEFAULT_AS_PARAM(AddressSpace), + int64_t offset = 0) : V((const Value *)nullptr), Offset(offset), StackID(0), AddrSpace(AddressSpace) {} diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h --- a/llvm/include/llvm/CodeGen/TargetLowering.h +++ b/llvm/include/llvm/CodeGen/TargetLowering.h @@ -339,14 +339,15 @@ /// Return the pointer type for the given address space, defaults to /// the pointer type from the data layout. /// FIXME: The default needs to be removed once all the code is updated. - virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS = 0) const { + virtual MVT getPointerTy(const DataLayout &DL, + LLVM_DEFAULT_AS_PARAM(AS)) const { return MVT::getIntegerVT(DL.getPointerSizeInBits(AS)); } /// Return the in-memory pointer type for the given address space, defaults to /// the pointer type from the data layout. FIXME: The default needs to be /// removed once all the code is updated. - MVT getPointerMemTy(const DataLayout &DL, uint32_t AS = 0) const { + MVT getPointerMemTy(const DataLayout &DL, LLVM_DEFAULT_AS_PARAM(AS)) const { return MVT::getIntegerVT(DL.getPointerSizeInBits(AS)); } @@ -1575,7 +1576,7 @@ /// helps to ensure that such replacements don't generate code that causes an /// alignment error (trap) on the target machine. virtual bool allowsMisalignedMemoryAccesses( - EVT, unsigned AddrSpace = 0, unsigned Align = 1, + EVT, LLVM_DEFAULT_AS_PARAM(AddrSpace), unsigned Align = 1, MachineMemOperand::Flags Flags = MachineMemOperand::MONone, bool * /*Fast*/ = nullptr) const { return false; @@ -1583,7 +1584,7 @@ /// LLT handling variant. virtual bool allowsMisalignedMemoryAccesses( - LLT, unsigned AddrSpace = 0, Align Alignment = Align(1), + LLT, LLVM_DEFAULT_AS_PARAM(AddrSpace), Align Alignment = Align(1), MachineMemOperand::Flags Flags = MachineMemOperand::MONone, bool * /*Fast*/ = nullptr) const { return false; @@ -1595,7 +1596,7 @@ /// (as defined by the target). bool allowsMemoryAccessForAlignment( LLVMContext &Context, const DataLayout &DL, EVT VT, - unsigned AddrSpace = 0, Align Alignment = Align(1), + LLVM_DEFAULT_AS_PARAM(AddrSpace), Align Alignment = Align(1), MachineMemOperand::Flags Flags = MachineMemOperand::MONone, bool *Fast = nullptr) const; @@ -1614,7 +1615,8 @@ /// target). virtual bool allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, EVT VT, - unsigned AddrSpace = 0, Align Alignment = Align(1), + LLVM_DEFAULT_AS_PARAM(AddrSpace), + Align Alignment = Align(1), MachineMemOperand::Flags Flags = MachineMemOperand::MONone, bool *Fast = nullptr) const; @@ -2322,7 +2324,7 @@ /// TODO: Handle pre/postinc as well. /// TODO: Remove default argument virtual int getScalingFactorCost(const DataLayout &DL, const AddrMode &AM, - Type *Ty, unsigned AS = 0) const { + Type *Ty, LLVM_DEFAULT_AS_PARAM(AS)) const { // Default: assume that any scaling factor used in a legal AM is free. if (isLegalAddressingMode(DL, AM, Ty, AS)) return 0; diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h --- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -481,7 +481,7 @@ /// must be the same. /// \param Name Name of the variable. Constant *getOrCreateOMPInternalVariable(Type *Ty, const Twine &Name, - unsigned AddressSpace = 0); + LLVM_DEFAULT_AS_PARAM(AddressSpace)); /// Returns corresponding lock object for the specified critical region /// name. If the lock object does not exist it is created, otherwise the diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -357,17 +357,17 @@ } /// Layout pointer alignment - Align getPointerABIAlignment(unsigned AS) const; + Align getPointerABIAlignment(LLVM_DEFAULT_AS_PARAM(AS)) const; /// Return target's alignment for stack-based pointers /// FIXME: The defaults need to be removed once all of /// the backends/clients are updated. - Align getPointerPrefAlignment(unsigned AS = 0) const; + Align getPointerPrefAlignment(LLVM_DEFAULT_AS_PARAM(AS)) const; /// Layout pointer size /// FIXME: The defaults need to be removed once all of /// the backends/clients are updated. - unsigned getPointerSize(unsigned AS = 0) const; + unsigned getPointerSize(LLVM_DEFAULT_AS_PARAM(AS)) const; /// Returns the maximum pointer size over all address spaces. unsigned getMaxPointerSize() const; @@ -398,7 +398,7 @@ /// Layout pointer size, in bits /// FIXME: The defaults need to be removed once all of /// the backends/clients are updated. - unsigned getPointerSizeInBits(unsigned AS = 0) const { + unsigned getPointerSizeInBits(LLVM_DEFAULT_AS_PARAM(AS)) const { return getPointerSize(AS) * 8; } @@ -542,7 +542,8 @@ /// Returns an integer type with size at least as big as that of a /// pointer in the given address space. - IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const; + IntegerType *getIntPtrType(LLVMContext &C, + LLVM_DEFAULT_AS_PARAM(AddressSpace)) const; /// Returns an integer (vector of integer) type with size at least as /// big as that of a pointer of the given pointer (vector of pointer) type. diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h --- a/llvm/include/llvm/IR/DerivedTypes.h +++ b/llvm/include/llvm/IR/DerivedTypes.h @@ -672,11 +672,13 @@ /// address space. static PointerType *get(Type *ElementType, unsigned AddressSpace); +#ifndef LLVM_NO_IMPLICIT_ADDRESS_SPACE /// This constructs a pointer to an object of the specified type in the /// generic address space (address space zero). static PointerType *getUnqual(Type *ElementType) { return PointerType::get(ElementType, 0); } +#endif Type *getElementType() const { return PointeeTy; } diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -893,7 +893,7 @@ /// Null pointer access in non-zero address space is not considered undefined. /// Return value: false => null pointer dereference is undefined. /// Return value: true => null pointer dereference is not undefined. -bool NullPointerIsDefined(const Function *F, unsigned AS = 0); +bool NullPointerIsDefined(const Function *F, LLVM_DEFAULT_AS_PARAM(AS)); template <> struct OperandTraits : public HungoffOperandTraits<3> {}; diff --git a/llvm/include/llvm/IR/GlobalObject.h b/llvm/include/llvm/IR/GlobalObject.h --- a/llvm/include/llvm/IR/GlobalObject.h +++ b/llvm/include/llvm/IR/GlobalObject.h @@ -45,7 +45,7 @@ protected: GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name, - unsigned AddressSpace = 0) + LLVM_DEFAULT_AS_PARAM(AddressSpace)) : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace), ObjComdat(nullptr) { setGlobalValueSubClassData(0); diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -386,8 +386,13 @@ /// filled in with the null terminated string value specified. The new global /// variable will be marked mergable with any others of the same contents. If /// Name is specified, it is the name of the global variable created. +#ifdef LLVM_NO_IMPLICIT_ADDRESS_SPACE + GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name, + unsigned AddressSpace); +#else GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "", unsigned AddressSpace = 0); +#endif /// Get a constant value representing either true or false. ConstantInt *getInt1(bool V) { @@ -498,12 +503,13 @@ } /// Fetch the type representing a pointer to an 8-bit integer value. - PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { + PointerType *getInt8PtrTy(LLVM_DEFAULT_AS_PARAM(AddrSpace)) { return Type::getInt8PtrTy(Context, AddrSpace); } /// Fetch the type representing a pointer to an integer value. - IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { + IntegerType *getIntPtrTy(const DataLayout &DL, + LLVM_DEFAULT_AS_PARAM(AddrSpace)) { return DL.getIntPtrType(Context, AddrSpace); } @@ -1934,8 +1940,13 @@ /// Same as CreateGlobalString, but return a pointer with "i8*" type /// instead of a pointer to array of i8. +#ifdef LLVM_NO_IMPLICIT_ADDRESS_SPACE + Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name, + unsigned AddressSpace) { +#else Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", unsigned AddressSpace = 0) { +#endif GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace); Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); Constant *Indices[] = {Zero, Zero}; diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h --- a/llvm/include/llvm/IR/Type.h +++ b/llvm/include/llvm/IR/Type.h @@ -26,6 +26,17 @@ #include #include +// LLVM_NO_IMPLICIT_ADDRESS_SPACE can be defined to gradually migrate files +// away from relying on the default value zero for address spaces. +// This is helpful to avoid subtle bugs in targets that use a non-zero +// address space for global variables or functions. +#if !defined(LLVM_NO_IMPLICIT_ADDRESS_SPACE) || \ + LLVM_NO_IMPLICIT_ADDRESS_SPACE == 0 +#define LLVM_DEFAULT_AS_PARAM(name) unsigned name = 0 +#else +#define LLVM_DEFAULT_AS_PARAM(name) unsigned name +#endif + namespace llvm { template struct GraphTraits; @@ -432,24 +443,28 @@ // Convenience methods for getting pointer types with one of the above builtin // types as pointee. // - static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getBFloatPtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); - static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); - static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); + static PointerType *getHalfPtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getBFloatPtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getFloatPtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getDoublePtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getX86_FP80PtrTy(LLVMContext &C, + LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getFP128PtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getPPC_FP128PtrTy(LLVMContext &C, + LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getX86_MMXPtrTy(LLVMContext &C, + LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, + LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getInt1PtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getInt8PtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getInt16PtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getInt32PtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); + static PointerType *getInt64PtrTy(LLVMContext &C, LLVM_DEFAULT_AS_PARAM(AS)); /// Return a pointer to the current type. This is equivalent to /// PointerType::get(Foo, AddrSpace). - PointerType *getPointerTo(unsigned AddrSpace = 0) const; + PointerType *getPointerTo(LLVM_DEFAULT_AS_PARAM(AddrSpace)) const; private: /// Derived types like structures and arrays are sized iff all of the members diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp --- a/llvm/lib/IR/AutoUpgrade.cpp +++ b/llvm/lib/IR/AutoUpgrade.cpp @@ -880,7 +880,7 @@ LLVMContext &C = GV->getContext(); IRBuilder<> IRB(C); auto EltTy = StructType::get(STy->getElementType(0), STy->getElementType(1), - IRB.getInt8PtrTy()); + IRB.getInt8PtrTy(GV->getAddressSpace())); Constant *Init = GV->getInitializer(); unsigned N = Init->getNumOperands(); std::vector NewCtors(N); @@ -888,12 +888,13 @@ auto Ctor = cast(Init->getOperand(i)); NewCtors[i] = ConstantStruct::get( EltTy, Ctor->getAggregateElement(0u), Ctor->getAggregateElement(1), - Constant::getNullValue(IRB.getInt8PtrTy())); + Constant::getNullValue(IRB.getInt8PtrTy(GV->getAddressSpace()))); } Constant *NewInit = ConstantArray::get(ArrayType::get(EltTy, N), NewCtors); return new GlobalVariable(NewInit->getType(), false, GV->getLinkage(), - NewInit, GV->getName()); + NewInit, GV->getName(), GV->getThreadLocalMode(), + GV->getAddressSpace()); } // Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them @@ -1244,8 +1245,9 @@ Value *Ptr, Value *Data, Value *Mask, bool Aligned) { // Cast the pointer to the right type. - Ptr = Builder.CreateBitCast(Ptr, - llvm::PointerType::getUnqual(Data->getType())); + Ptr = Builder.CreateBitCast( + Ptr, llvm::PointerType::get(Data->getType(), + Ptr->getType()->getPointerAddressSpace())); const Align Alignment = Aligned ? Align(Data->getType()->getPrimitiveSizeInBits().getFixedSize() / 8) @@ -1267,7 +1269,8 @@ bool Aligned) { Type *ValTy = Passthru->getType(); // Cast the pointer to the right type. - Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(ValTy)); + Ptr = Builder.CreateBitCast( + Ptr, ValTy->getPointerTo(Ptr->getType()->getPointerAddressSpace())); const Align Alignment = Aligned ? Align(Passthru->getType()->getPrimitiveSizeInBits().getFixedSize() / @@ -1716,7 +1719,8 @@ // Nontemporal (unaligned) store of the 0'th element of the float/double // vector. Type *SrcEltTy = cast(Arg1->getType())->getElementType(); - PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy); + PointerType *EltPtrTy = + SrcEltTy->getPointerTo(Arg0->getType()->getPointerAddressSpace()); Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast"); Value *Extract = Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement"); @@ -1741,9 +1745,11 @@ Value *Arg1 = CI->getArgOperand(1); // Convert the type of the pointer to a pointer to the stored type. - Value *BC = Builder.CreateBitCast(Arg0, - PointerType::getUnqual(Arg1->getType()), - "cast"); + Value *BC = + Builder.CreateBitCast(Arg0, + Arg1->getType()->getPointerTo( + Arg0->getType()->getPointerAddressSpace()), + "cast"); StoreInst *SI = Builder.CreateAlignedStore( Arg1, BC, Align(Arg1->getType()->getPrimitiveSizeInBits().getFixedSize() / 8)); @@ -1761,9 +1767,11 @@ auto *NewVecTy = FixedVectorType::get(Type::getInt64Ty(C), 2); Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0); - Value *BC = Builder.CreateBitCast(Arg0, - PointerType::getUnqual(Elt->getType()), - "cast"); + Value *BC = + Builder.CreateBitCast(Arg0, + Elt->getType()->getPointerTo( + Arg0->getType()->getPointerAddressSpace()), + "cast"); Builder.CreateAlignedStore(Elt, BC, Align(1)); // Remove intrinsic. @@ -1777,9 +1785,11 @@ Value *Arg0 = CI->getArgOperand(0); Value *Arg1 = CI->getArgOperand(1); - Arg0 = Builder.CreateBitCast(Arg0, - PointerType::getUnqual(Arg1->getType()), - "cast"); + Arg0 = Builder.CreateBitCast( + Arg0, + PointerType::get(Arg1->getType(), + Arg0->getType()->getPointerAddressSpace()), + "cast"); Builder.CreateAlignedStore(Arg1, Arg0, Align(1)); // Remove intrinsic. @@ -2181,8 +2191,10 @@ Type *PtrTy = ResultTy->getElementType(); // Cast the pointer to element type. - Value *Ptr = Builder.CreateBitCast(CI->getOperand(0), - llvm::PointerType::getUnqual(PtrTy)); + Value *Ptr = Builder.CreateBitCast( + CI->getOperand(0), + llvm::PointerType::get( + PtrTy, CI->getOperand(0)->getType()->getPointerAddressSpace())); Value *MaskVec = getX86MaskVec(Builder, CI->getArgOperand(2), ResultTy->getNumElements()); @@ -2196,8 +2208,10 @@ Type *PtrTy = ResultTy->getElementType(); // Cast the pointer to element type. - Value *Ptr = Builder.CreateBitCast(CI->getOperand(0), - llvm::PointerType::getUnqual(PtrTy)); + Value *Ptr = Builder.CreateBitCast( + CI->getOperand(0), + llvm::PointerType::get( + PtrTy, CI->getOperand(0)->getType()->getPointerAddressSpace())); Value *MaskVec = getX86MaskVec(Builder, CI->getArgOperand(2), ResultTy->getNumElements()); @@ -2291,8 +2305,10 @@ auto *VecTy = cast(CI->getType()); Type *EltTy = VecTy->getElementType(); unsigned EltNum = VecTy->getNumElements(); - Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0), - EltTy->getPointerTo()); + auto PtrArg = CI->getArgOperand(0); + Value *Cast = Builder.CreateBitCast( + PtrArg, + EltTy->getPointerTo(PtrArg->getType()->getPointerAddressSpace())); Value *Load = Builder.CreateLoad(EltTy, Cast); Type *I32Ty = Type::getInt32Ty(C); Rep = UndefValue::get(VecTy); @@ -2338,8 +2354,10 @@ Type *EltTy = cast(CI->getType())->getElementType(); unsigned NumSrcElts = 128 / EltTy->getPrimitiveSizeInBits(); auto *VT = FixedVectorType::get(EltTy, NumSrcElts); - Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0), - PointerType::getUnqual(VT)); + Value *Op = Builder.CreatePointerCast( + CI->getArgOperand(0), + PointerType::get( + VT, CI->getOperand(0)->getType()->getPointerAddressSpace())); Value *Load = Builder.CreateAlignedLoad(VT, Op, Align(1)); if (NumSrcElts == 2) Rep = Builder.CreateShuffleVector( @@ -3085,7 +3103,9 @@ // Convert the type of the pointer to a pointer to the stored type. Value *BC = Builder.CreateBitCast( - Ptr, PointerType::getUnqual(CI->getType()), "cast"); + Ptr, + CI->getType()->getPointerTo(Ptr->getType()->getPointerAddressSpace()), + "cast"); LoadInst *LI = Builder.CreateAlignedLoad( CI->getType(), BC, Align(CI->getType()->getPrimitiveSizeInBits().getFixedSize() / 8)); @@ -3463,8 +3483,10 @@ // Extract the second result and store it. Value *Data = Builder.CreateExtractValue(NewCall, 1); // Cast the pointer to the right type. - Value *Ptr = Builder.CreateBitCast(CI->getArgOperand(3), - llvm::PointerType::getUnqual(Data->getType())); + Value *Ptr = Builder.CreateBitCast( + CI->getArgOperand(3), + Data->getType()->getPointerTo( + CI->getArgOperand(3)->getType()->getPointerAddressSpace())); Builder.CreateAlignedStore(Data, Ptr, Align(1)); // Replace the original call result with the first result of the new call. Value *CF = Builder.CreateExtractValue(NewCall, 0); @@ -3685,8 +3707,10 @@ // Extract the second result and store it. Value *Data = Builder.CreateExtractValue(NewCall, 1); // Cast the pointer to the right type. - Value *Ptr = Builder.CreateBitCast(CI->getArgOperand(0), - llvm::PointerType::getUnqual(Data->getType())); + Value *Ptr = Builder.CreateBitCast( + CI->getArgOperand(0), + Data->getType()->getPointerTo( + CI->getArgOperand(0)->getType()->getPointerAddressSpace())); Builder.CreateAlignedStore(Data, Ptr, Align(1)); // Replace the original call result with the first result of the new call. Value *TSC = Builder.CreateExtractValue(NewCall, 0); diff --git a/llvm/lib/IR/CMakeLists.txt b/llvm/lib/IR/CMakeLists.txt --- a/llvm/lib/IR/CMakeLists.txt +++ b/llvm/lib/IR/CMakeLists.txt @@ -63,3 +63,5 @@ DEPENDS intrinsics_gen ) + +add_definitions(-DLLVM_NO_IMPLICIT_ADDRESS_SPACE=1) diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -1692,8 +1692,8 @@ } BlockAddress::BlockAddress(Function *F, BasicBlock *BB) -: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal, - &Op<0>(), 2) { + : Constant(Type::getInt8PtrTy(F->getContext(), F->getAddressSpace()), + Value::BlockAddressVal, &Op<0>(), 2) { setOperand(0, F); setOperand(1, BB); BB->AdjustBlockAddressRefCount(1); @@ -2150,9 +2150,8 @@ // Note that a non-inbounds gep is used, as null isn't within any object. Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); Constant *GEP = getGetElementPtr( - Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); - return getPtrToInt(GEP, - Type::getInt64Ty(Ty->getContext())); + Ty, Constant::getNullValue(PointerType::get(Ty, 0)), GEPIdx); + return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext())); } Constant *ConstantExpr::getAlignOf(Type* Ty) { @@ -2181,9 +2180,8 @@ FieldNo }; Constant *GEP = getGetElementPtr( - Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); - return getPtrToInt(GEP, - Type::getInt64Ty(Ty->getContext())); + Ty, Constant::getNullValue(PointerType::get(Ty, 0)), GEPIdx); + return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext())); } Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1, diff --git a/llvm/lib/IR/ConstantsContext.h b/llvm/lib/IR/ConstantsContext.h --- a/llvm/lib/IR/ConstantsContext.h +++ b/llvm/lib/IR/ConstantsContext.h @@ -500,7 +500,7 @@ using TypeClass = ConstantInfo::TypeClass; InlineAsm *create(TypeClass *Ty) const { - assert(PointerType::getUnqual(FTy) == Ty); + assert(PointerType::get(FTy, 0) == Ty); return new InlineAsm(FTy, std::string(AsmString), std::string(Constraints), HasSideEffects, IsAlignStack, AsmDialect); } diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -3644,12 +3644,12 @@ LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, const char *Name) { - return wrap(unwrap(B)->CreateGlobalString(Str, Name)); + return wrap(unwrap(B)->CreateGlobalString(Str, Name, 0)); } LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, const char *Name) { - return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); + return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name, 0)); } LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -1125,7 +1125,7 @@ } case IITDescriptor::PtrToArgument: { Type *Ty = Tys[D.getArgumentNumber()]; - return PointerType::getUnqual(Ty); + return PointerType::get(Ty, 0); } case IITDescriptor::PtrToElt: { Type *Ty = Tys[D.getArgumentNumber()]; @@ -1133,7 +1133,7 @@ if (!VTy) llvm_unreachable("Expected an argument of Vector Type"); Type *EltTy = VTy->getElementType(); - return PointerType::getUnqual(EltTy); + return PointerType::get(EltTy, 0); } case IITDescriptor::VecElementArgument: { Type *Ty = Tys[D.getArgumentNumber()]; diff --git a/llvm/lib/IR/InlineAsm.cpp b/llvm/lib/IR/InlineAsm.cpp --- a/llvm/lib/IR/InlineAsm.cpp +++ b/llvm/lib/IR/InlineAsm.cpp @@ -27,10 +27,12 @@ using namespace llvm; +// Note: InlineAsm is currently always in address space zero +// TODO: should we require it to be DL.getProgramAddressSpace()? InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString, const std::string &constraints, bool hasSideEffects, bool isAlignStack, AsmDialect asmDialect) - : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal), + : Value(PointerType::get(FTy, 0), Value::InlineAsmVal), AsmString(asmString), Constraints(constraints), FTy(FTy), HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack), Dialect(asmDialect) { @@ -45,7 +47,9 @@ InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects, isAlignStack, asmDialect); LLVMContextImpl *pImpl = FTy->getContext().pImpl; - return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key); + // Note: InlineAsm is currently always in address space zero + // TODO: should we require it to be DL.getProgramAddressSpace()? + return pImpl->InlineAsms.getOrCreate(PointerType::get(FTy, 0), Key); } void InlineAsm::destroyConstant() { diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -621,12 +621,14 @@ // Create the call to Malloc. BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; Module *M = BB->getParent()->getParent(); - Type *BPTy = Type::getInt8PtrTy(BB->getContext()); + Type *BPTy = Type::getInt8PtrTy( + BB->getContext(), M->getDataLayout().getDefaultGlobalsAddressSpace()); FunctionCallee MallocFunc = MallocF; if (!MallocFunc) // prototype malloc as "void *malloc(size_t)" MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); - PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); + PointerType *AllocPtrType = AllocTy->getPointerTo( + MallocFunc.getFunctionType()->getReturnType()->getPointerAddressSpace()); CallInst *MCall = nullptr; Instruction *Result = nullptr; if (InsertBefore) { @@ -717,7 +719,8 @@ Module *M = BB->getParent()->getParent(); Type *VoidTy = Type::getVoidTy(M->getContext()); - Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); + Type *IntPtrTy = Type::getInt8PtrTy( + M->getContext(), Source->getType()->getPointerAddressSpace()); // prototype free as "void free(void*)" FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); CallInst *Result = nullptr; diff --git a/llvm/lib/IR/Mangler.cpp b/llvm/lib/IR/Mangler.cpp --- a/llvm/lib/IR/Mangler.cpp +++ b/llvm/lib/IR/Mangler.cpp @@ -95,7 +95,7 @@ // Calculate arguments size total. unsigned ArgWords = 0; - const unsigned PtrSize = DL.getPointerSize(); + const unsigned PtrSize = DL.getPointerSize(0); for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI) {