Change the Address Space of a Type of a Function Argument

Hi so I want to change from

define dso_loca void @_Z5emptyPi(ptr noundef %0) ...
define dso_loca void @_Z5emptyPi(ptr addrspace(1) noundef %0) ...
Where the ptr has an explicit addrspace(1),
Currently I am using

  for (Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
         ii != ee; ++ii) {
        Type *ArgType = ii->getType();
        // Value * v = dynamic_cast<Value*>(ii);
        if (ArgType->isPointerTy()) {
           Type *NewPtrTy = PointerType::getWithSamePointeeType(
            cast<PointerType>(ArgType), DL.getDefaultGlobalsAddressSpace());
            // v->mutateType(NewPtrTy);


This give me the error “Argument value does not match function argument type!”
So it seems I have to also change the llvm:FunctionType for the param, but the iterators are llvm::Type* const* which I don’t think it is possible to change Type or address space cast.
What is the best way to do this?


You’ll need to create a new FunctionType with the desired argument types, not mutate the one that’s there as that would affect any other function that happens to have the same type.