arichardson created this revision.
arichardson added reviewers: aykevl, dylanmckay, arsenm, bjope, theraven, 
jrtc27.
Herald added subscribers: cfe-commits, hiraditya, mgorny, wdng.
Herald added a reviewer: ctetreau.
Herald added a project: clang.

APIs such as PointerType::getUnqual/Type::getPointerTo() can result in
pointers being created in address space zero even though this is not
correct for all targets. We have been bitten by this issue many times our
CHERI fork: we use address space 200 for both globals and functions.
Creating a pointer in address space zero will generally result instruction
selection failures or in some cases code being generated that triggers
traps at run time. To avoid these problems, I've remove the many instances
of `unsigned AS = 0` function parameters to ensure that pointers get created
in the right address space.

Keeping these changes out-of-tree results in compilation failures almost
every time I do an upstream merge. It would be very beneficial for us to
have these changes upstreamed, and should help targets such as AVR than
use a non-zero program address space.
I've seen some recent commits for AVR that fix the same AS=0 issues that
we have out-of-tree (e.g. 215dc2e203341f7d1edc4c4a191b048af4ace43d 
<https://reviews.llvm.org/rG215dc2e203341f7d1edc4c4a191b048af4ace43d>).
I think making such bugs a compilation failure should be beneficial for
everyone even if it means typing a few more characters to get a pointer type.

This change allows for gradual migration: we can add a single CMake line
to each directory that should no longer compile with implicit address
space zero: `add_definitions(-DLLVM_NO_IMPLICIT_ADDRESS_SPACE=1)`. In this
patch I've added the definition to lib/IR and I will follow up with further
cleanups.

Depends on D70947 <https://reviews.llvm.org/D70947>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78491

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.h
  llvm/include/llvm/Analysis/TargetTransformInfo.h
  llvm/include/llvm/CodeGen/MachineMemOperand.h
  llvm/include/llvm/CodeGen/TargetLowering.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/IR/DataLayout.h
  llvm/include/llvm/IR/DerivedTypes.h
  llvm/include/llvm/IR/Function.h
  llvm/include/llvm/IR/GlobalObject.h
  llvm/include/llvm/IR/IRBuilder.h
  llvm/include/llvm/IR/Type.h
  llvm/lib/IR/AutoUpgrade.cpp
  llvm/lib/IR/CMakeLists.txt
  llvm/lib/IR/Constants.cpp
  llvm/lib/IR/ConstantsContext.h
  llvm/lib/IR/Core.cpp
  llvm/lib/IR/Function.cpp
  llvm/lib/IR/InlineAsm.cpp
  llvm/lib/IR/Instructions.cpp
  llvm/lib/IR/Mangler.cpp

Index: llvm/lib/IR/Mangler.cpp
===================================================================
--- llvm/lib/IR/Mangler.cpp
+++ llvm/lib/IR/Mangler.cpp
@@ -101,7 +101,7 @@
     if (AI->hasByValOrInAllocaAttr())
       Ty = cast<PointerType>(Ty)->getElementType();
     // Size should be aligned to pointer size.
-    unsigned PtrSize = DL.getPointerSize();
+    unsigned PtrSize = DL.getPointerSize(0);
     ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
   }
 
Index: llvm/lib/IR/Instructions.cpp
===================================================================
--- llvm/lib/IR/Instructions.cpp
+++ llvm/lib/IR/Instructions.cpp
@@ -616,12 +616,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) {
@@ -712,7 +714,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;
Index: llvm/lib/IR/InlineAsm.cpp
===================================================================
--- llvm/lib/IR/InlineAsm.cpp
+++ llvm/lib/IR/InlineAsm.cpp
@@ -30,7 +30,8 @@
 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, FTy->getPointerAddressSpace()),
+            Value::InlineAsmVal),
       AsmString(asmString), Constraints(constraints), FTy(FTy),
       HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
       Dialect(asmDialect) {
@@ -45,7 +46,8 @@
   InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
                        isAlignStack, asmDialect);
   LLVMContextImpl *pImpl = FTy->getContext().pImpl;
-  return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
+  return pImpl->InlineAsms.getOrCreate(
+      PointerType::get(FTy, FTy->getPointerAddressSpace()), Key);
 }
 
 void InlineAsm::destroyConstant() {
Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -1048,7 +1048,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()];
@@ -1056,7 +1056,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()];
Index: llvm/lib/IR/Core.cpp
===================================================================
--- llvm/lib/IR/Core.cpp
+++ llvm/lib/IR/Core.cpp
@@ -3634,12 +3634,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) {
Index: llvm/lib/IR/ConstantsContext.h
===================================================================
--- llvm/lib/IR/ConstantsContext.h
+++ llvm/lib/IR/ConstantsContext.h
@@ -458,7 +458,7 @@
   using TypeClass = ConstantInfo<InlineAsm>::TypeClass;
 
   InlineAsm *create(TypeClass *Ty) const {
-    assert(PointerType::getUnqual(FTy) == Ty);
+    assert(PointerType::get(FTy, FTy->getPointerAddressSpace()) == Ty);
     return new InlineAsm(FTy, std::string(AsmString), std::string(Constraints),
                          HasSideEffects, IsAlignStack, AsmDialect);
   }
Index: llvm/lib/IR/Constants.cpp
===================================================================
--- llvm/lib/IR/Constants.cpp
+++ llvm/lib/IR/Constants.cpp
@@ -1586,8 +1586,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);
@@ -2044,9 +2044,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) {
@@ -2075,9 +2074,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,
Index: llvm/lib/IR/CMakeLists.txt
===================================================================
--- llvm/lib/IR/CMakeLists.txt
+++ llvm/lib/IR/CMakeLists.txt
@@ -63,3 +63,5 @@
   DEPENDS
   intrinsics_gen
   )
+
+add_definitions(-DLLVM_NO_IMPLICIT_ADDRESS_SPACE=1)
Index: llvm/lib/IR/AutoUpgrade.cpp
===================================================================
--- llvm/lib/IR/AutoUpgrade.cpp
+++ llvm/lib/IR/AutoUpgrade.cpp
@@ -879,7 +879,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<Constant *> NewCtors(N);
@@ -887,12 +887,13 @@
     auto Ctor = cast<Constant>(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() /
@@ -1714,7 +1717,8 @@
       // Nontemporal (unaligned) store of the 0'th element of the float/double
       // vector.
       Type *SrcEltTy = cast<VectorType>(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");
@@ -1739,9 +1743,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));
@@ -1759,9 +1765,11 @@
       Type *NewVecTy = VectorType::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.
@@ -1775,9 +1783,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.
@@ -2179,8 +2189,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());
@@ -2194,8 +2206,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());
@@ -2289,8 +2303,10 @@
       auto *VecTy = cast<VectorType>(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);
@@ -2336,8 +2352,10 @@
       Type *EltTy = cast<VectorType>(CI->getType())->getElementType();
       unsigned NumSrcElts = 128 / EltTy->getPrimitiveSizeInBits();
       Type *VT = VectorType::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(
@@ -3083,7 +3101,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));
@@ -3461,8 +3481,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);
@@ -3683,8 +3705,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);
Index: llvm/include/llvm/IR/Type.h
===================================================================
--- llvm/include/llvm/IR/Type.h
+++ llvm/include/llvm/IR/Type.h
@@ -26,6 +26,17 @@
 #include <cstdint>
 #include <iterator>
 
+// 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<class GraphType> struct GraphTraits;
@@ -421,23 +432,27 @@
   // 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 *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 *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
Index: llvm/include/llvm/IR/IRBuilder.h
===================================================================
--- llvm/include/llvm/IR/IRBuilder.h
+++ llvm/include/llvm/IR/IRBuilder.h
@@ -375,8 +375,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) {
@@ -482,12 +487,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);
   }
 
@@ -1916,8 +1922,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};
Index: llvm/include/llvm/IR/GlobalObject.h
===================================================================
--- llvm/include/llvm/IR/GlobalObject.h
+++ 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);
Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -871,7 +871,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<Function> : public HungoffOperandTraits<3> {};
Index: llvm/include/llvm/IR/DerivedTypes.h
===================================================================
--- llvm/include/llvm/IR/DerivedTypes.h
+++ llvm/include/llvm/IR/DerivedTypes.h
@@ -547,11 +547,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; }
 
Index: llvm/include/llvm/IR/DataLayout.h
===================================================================
--- llvm/include/llvm/IR/DataLayout.h
+++ llvm/include/llvm/IR/DataLayout.h
@@ -353,17 +353,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;
@@ -394,7 +394,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;
   }
 
@@ -538,7 +538,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.
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===================================================================
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -397,7 +397,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
Index: llvm/include/llvm/CodeGen/TargetLowering.h
===================================================================
--- llvm/include/llvm/CodeGen/TargetLowering.h
+++ llvm/include/llvm/CodeGen/TargetLowering.h
@@ -329,14 +329,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));
   }
 
@@ -1561,7 +1562,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;
@@ -1569,7 +1570,7 @@
 
   /// LLT handling variant.
   virtual bool allowsMisalignedMemoryAccesses(
-      LLT, unsigned AddrSpace = 0, unsigned Align = 1,
+      LLT, LLVM_DEFAULT_AS_PARAM(AddrSpace), unsigned Align = 1,
       MachineMemOperand::Flags Flags = MachineMemOperand::MONone,
       bool * /*Fast*/ = nullptr) const {
     return false;
@@ -1581,7 +1582,7 @@
   /// (as defined by the target).
   bool allowsMemoryAccessForAlignment(
       LLVMContext &Context, const DataLayout &DL, EVT VT,
-      unsigned AddrSpace = 0, unsigned Alignment = 1,
+      LLVM_DEFAULT_AS_PARAM(AddrSpace), unsigned Alignment = 1,
       MachineMemOperand::Flags Flags = MachineMemOperand::MONone,
       bool *Fast = nullptr) const;
 
@@ -1600,7 +1601,7 @@
   /// target).
   virtual bool
   allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, EVT VT,
-                     unsigned AddrSpace = 0, unsigned Alignment = 1,
+                     LLVM_DEFAULT_AS_PARAM(AddrSpace), unsigned Alignment = 1,
                      MachineMemOperand::Flags Flags = MachineMemOperand::MONone,
                      bool *Fast = nullptr) const;
 
@@ -2306,7 +2307,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;
Index: llvm/include/llvm/CodeGen/MachineMemOperand.h
===================================================================
--- llvm/include/llvm/CodeGen/MachineMemOperand.h
+++ llvm/include/llvm/CodeGen/MachineMemOperand.h
@@ -58,7 +58,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) {}
 
Index: llvm/include/llvm/Analysis/TargetTransformInfo.h
===================================================================
--- llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -606,7 +606,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
@@ -702,7 +702,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;
 
Index: clang/lib/CodeGen/CGOpenMPRuntime.h
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.h
+++ clang/lib/CodeGen/CGOpenMPRuntime.h
@@ -764,9 +764,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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to