@@ -543,7 +543,8 @@ namespace {
543543
544544class TypePrinting {
545545public:
546- TypePrinting (const Module *M = nullptr ) : DeferredM(M) {}
546+ TypePrinting (const Module *M = nullptr )
547+ : M(M), TypesIncorporated(M == nullptr ) {}
547548
548549 TypePrinting (const TypePrinting &) = delete ;
549550 TypePrinting &operator =(const TypePrinting &) = delete ;
@@ -564,7 +565,8 @@ class TypePrinting {
564565 void incorporateTypes ();
565566
566567 // / A module to process lazily when needed. Set to nullptr as soon as used.
567- const Module *DeferredM;
568+ const Module *M;
569+ bool TypesIncorporated;
568570
569571 TypeFinder NamedTypes;
570572
@@ -605,11 +607,11 @@ bool TypePrinting::empty() {
605607}
606608
607609void TypePrinting::incorporateTypes () {
608- if (!DeferredM )
610+ if (TypesIncorporated )
609611 return ;
610612
611- NamedTypes.run (*DeferredM , false );
612- DeferredM = nullptr ;
613+ NamedTypes.run (*M , false );
614+ TypesIncorporated = true ;
613615
614616 // The list of struct types we got back includes all the struct types, split
615617 // the unnamed ones out to a numbering and remove the anonymous structs.
@@ -630,6 +632,19 @@ void TypePrinting::incorporateTypes() {
630632 NamedTypes.erase (NextToUse, NamedTypes.end ());
631633}
632634
635+ static void printAddressSpace (const Module *M, unsigned AS, raw_ostream &OS,
636+ bool ForcePrint = false ) {
637+ if (AS == 0 && !ForcePrint)
638+ return ;
639+ OS << " addrspace(" ;
640+ StringRef ASName = M ? M->getTargetTriple ().getAddressSpaceName (AS) : " " ;
641+ if (!ASName.empty ())
642+ OS << " \" " << ASName << " \" " ;
643+ else
644+ OS << AS;
645+ OS << " )" ;
646+ }
647+
633648// / Write the specified type to the specified raw_ostream, making use of type
634649// / names or up references to shorten the type name where possible.
635650void TypePrinting::print (Type *Ty, raw_ostream &OS) {
@@ -686,8 +701,7 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
686701 case Type::PointerTyID: {
687702 PointerType *PTy = cast<PointerType>(Ty);
688703 OS << " ptr" ;
689- if (unsigned AddressSpace = PTy->getAddressSpace ())
690- OS << " addrspace(" << AddressSpace << ' )' ;
704+ printAddressSpace (M, PTy->getAddressSpace (), OS);
691705 return ;
692706 }
693707 case Type::ArrayTyID: {
@@ -3894,12 +3908,12 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
38943908 printThreadLocalModel (GV->getThreadLocalMode (), Out);
38953909 StringRef UA = getUnnamedAddrEncoding (GV->getUnnamedAddr ());
38963910 if (!UA.empty ())
3897- Out << UA << ' ' ;
3911+ Out << UA;
38983912
38993913 if (unsigned AddressSpace = GV->getType ()->getAddressSpace ())
3900- Out << " addrspace( " << AddressSpace << " ) " ;
3914+ printAddressSpace (GV-> getParent (), AddressSpace, Out) ;
39013915 if (GV->isExternallyInitialized ()) Out << " externally_initialized " ;
3902- Out << (GV->isConstant () ? " constant " : " global " );
3916+ Out << (GV->isConstant () ? " constant " : " global " );
39033917 TypePrinter.print (GV->getValueType (), Out);
39043918
39053919 if (GV->hasInitializer ()) {
@@ -4172,9 +4186,9 @@ void AssemblyWriter::printFunction(const Function *F) {
41724186 // a module with a non-zero program address space or if there is no valid
41734187 // Module* so that the file can be parsed without the datalayout string.
41744188 const Module *Mod = F->getParent ();
4175- if (F-> getAddressSpace () != 0 || !Mod ||
4176- Mod->getDataLayout ().getProgramAddressSpace () != 0 )
4177- Out << " addrspace( " << F->getAddressSpace () << " ) " ;
4189+ bool ForcePrintAddressSpace =
4190+ ! Mod || Mod ->getDataLayout ().getProgramAddressSpace () != 0 ;
4191+ printAddressSpace (Mod, F->getAddressSpace (), Out, ForcePrintAddressSpace) ;
41784192 if (Attrs.hasFnAttrs ())
41794193 Out << " #" << Machine.getAttributeGroupSlot (Attrs.getFnAttrs ());
41804194 if (F->hasSection ()) {
@@ -4356,17 +4370,13 @@ static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
43564370 return ;
43574371 }
43584372 unsigned CallAddrSpace = Operand->getType ()->getPointerAddressSpace ();
4359- bool PrintAddrSpace = CallAddrSpace != 0 ;
4360- if (!PrintAddrSpace) {
4361- const Module *Mod = getModuleFromVal (I);
4362- // We also print it if it is zero but not equal to the program address space
4363- // or if we can't find a valid Module* to make it possible to parse
4364- // the resulting file even without a datalayout string.
4365- if (!Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 )
4366- PrintAddrSpace = true ;
4367- }
4368- if (PrintAddrSpace)
4369- Out << " addrspace(" << CallAddrSpace << " )" ;
4373+ const Module *Mod = getModuleFromVal (I);
4374+ // We also print it if it is zero but not equal to the program address space
4375+ // or if we can't find a valid Module* to make it possible to parse
4376+ // the resulting file even without a datalayout string.
4377+ bool ForcePrintAddrSpace =
4378+ !Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 ;
4379+ printAddressSpace (Mod, CallAddrSpace, Out, ForcePrintAddrSpace);
43704380}
43714381
43724382// This member is called for each Instruction in a function..
@@ -4734,8 +4744,10 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
47344744 }
47354745
47364746 unsigned AddrSpace = AI->getAddressSpace ();
4737- if (AddrSpace != 0 )
4738- Out << " , addrspace(" << AddrSpace << ' )' ;
4747+ if (AddrSpace != 0 ) {
4748+ Out << " ," ;
4749+ printAddressSpace (AI->getModule (), AddrSpace, Out);
4750+ }
47394751 } else if (isa<CastInst>(I)) {
47404752 if (Operand) {
47414753 Out << ' ' ;
0 commit comments