@@ -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,20 @@ void TypePrinting::incorporateTypes() {
630632 NamedTypes.erase (NextToUse, NamedTypes.end ());
631633}
632634
635+ static void printAddressSpace (const Module *M, unsigned AS, raw_ostream &OS,
636+ StringRef Prefix = " " , StringRef Suffix = " " ,
637+ bool ForcePrint = false ) {
638+ if (AS == 0 && !ForcePrint)
639+ return ;
640+ OS << Prefix << " addrspace(" ;
641+ StringRef ASName = M ? M->getTargetTriple ().getAddressSpaceName (AS) : " " ;
642+ if (!ASName.empty ())
643+ OS << " \" " << ASName << " \" " ;
644+ else
645+ OS << AS;
646+ OS << " )" << Suffix;
647+ }
648+
633649// / Write the specified type to the specified raw_ostream, making use of type
634650// / names or up references to shorten the type name where possible.
635651void TypePrinting::print (Type *Ty, raw_ostream &OS) {
@@ -686,8 +702,7 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
686702 case Type::PointerTyID: {
687703 PointerType *PTy = cast<PointerType>(Ty);
688704 OS << " ptr" ;
689- if (unsigned AddressSpace = PTy->getAddressSpace ())
690- OS << " addrspace(" << AddressSpace << ' )' ;
705+ printAddressSpace (M, PTy->getAddressSpace (), OS);
691706 return ;
692707 }
693708 case Type::ArrayTyID: {
@@ -3896,10 +3911,10 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
38963911 printThreadLocalModel (GV->getThreadLocalMode (), Out);
38973912 StringRef UA = getUnnamedAddrEncoding (GV->getUnnamedAddr ());
38983913 if (!UA.empty ())
3899- Out << UA << ' ' ;
3914+ Out << UA << ' ' ;
39003915
3901- if ( unsigned AddressSpace = GV->getType ()->getAddressSpace ())
3902- Out << " addrspace( " << AddressSpace << " ) " ;
3916+ printAddressSpace (GV-> getParent (), GV->getType ()->getAddressSpace (), Out,
3917+ /* Prefix= */ " " , /* Suffix= */ " " ) ;
39033918 if (GV->isExternallyInitialized ()) Out << " externally_initialized " ;
39043919 Out << (GV->isConstant () ? " constant " : " global " );
39053920 TypePrinter.print (GV->getValueType (), Out);
@@ -4174,9 +4189,10 @@ void AssemblyWriter::printFunction(const Function *F) {
41744189 // a module with a non-zero program address space or if there is no valid
41754190 // Module* so that the file can be parsed without the datalayout string.
41764191 const Module *Mod = F->getParent ();
4177- if (F->getAddressSpace () != 0 || !Mod ||
4178- Mod->getDataLayout ().getProgramAddressSpace () != 0 )
4179- Out << " addrspace(" << F->getAddressSpace () << " )" ;
4192+ bool ForcePrintAddressSpace =
4193+ !Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 ;
4194+ printAddressSpace (Mod, F->getAddressSpace (), Out, /* Prefix=*/ " " ,
4195+ /* Suffix=*/ " " , ForcePrintAddressSpace);
41804196 if (Attrs.hasFnAttrs ())
41814197 Out << " #" << Machine.getAttributeGroupSlot (Attrs.getFnAttrs ());
41824198 if (F->hasSection ()) {
@@ -4358,17 +4374,14 @@ static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
43584374 return ;
43594375 }
43604376 unsigned CallAddrSpace = Operand->getType ()->getPointerAddressSpace ();
4361- bool PrintAddrSpace = CallAddrSpace != 0 ;
4362- if (!PrintAddrSpace) {
4363- const Module *Mod = getModuleFromVal (I);
4364- // We also print it if it is zero but not equal to the program address space
4365- // or if we can't find a valid Module* to make it possible to parse
4366- // the resulting file even without a datalayout string.
4367- if (!Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 )
4368- PrintAddrSpace = true ;
4369- }
4370- if (PrintAddrSpace)
4371- Out << " addrspace(" << CallAddrSpace << " )" ;
4377+ const Module *Mod = getModuleFromVal (I);
4378+ // We also print it if it is zero but not equal to the program address space
4379+ // or if we can't find a valid Module* to make it possible to parse
4380+ // the resulting file even without a datalayout string.
4381+ bool ForcePrintAddrSpace =
4382+ !Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 ;
4383+ printAddressSpace (Mod, CallAddrSpace, Out, /* Prefix=*/ " " , /* Suffix=*/ " " ,
4384+ ForcePrintAddrSpace);
43724385}
43734386
43744387// This member is called for each Instruction in a function..
@@ -4735,9 +4748,8 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
47354748 Out << " , align " << A->value ();
47364749 }
47374750
4738- unsigned AddrSpace = AI->getAddressSpace ();
4739- if (AddrSpace != 0 )
4740- Out << " , addrspace(" << AddrSpace << ' )' ;
4751+ printAddressSpace (AI->getModule (), AI->getAddressSpace (), Out,
4752+ /* Prefix=*/ " , " );
47414753 } else if (isa<CastInst>(I)) {
47424754 if (Operand) {
47434755 Out << ' ' ;
0 commit comments