Index: lib/Target/NVPTX/NVPTXAsmPrinter.h =================================================================== --- lib/Target/NVPTX/NVPTXAsmPrinter.h +++ lib/Target/NVPTX/NVPTXAsmPrinter.h @@ -86,6 +86,14 @@ std::vector buffer; // the buffer SmallVector symbolPosInBuffer; SmallVector Symbols; + // SymbolsBeforeStripping[i] is the original form of Symbols[i] before + // stripping pointer casts, i.e., + // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts(). + // + // We need to keep these values because AggBuffer::print decides whether to + // emit a "generic()" cast for Symbols[i] depending on the address space of + // SymbolsBeforeStripping[i]. + SmallVector SymbolsBeforeStripping; unsigned curpos; raw_ostream &O; NVPTXAsmPrinter &AP; @@ -119,9 +127,10 @@ } return curpos; } - void addSymbol(const Value *GVar) { + void addSymbol(const Value *GVar, const Value *GVarBeforePointerCasts) { symbolPosInBuffer.push_back(curpos); Symbols.push_back(GVar); + SymbolsBeforeStripping.push_back(GVarBeforePointerCasts); numSymbols++; } void print() { @@ -145,10 +154,11 @@ O << ", "; if (pos == nextSymbolPos) { const Value *v = Symbols[nSym]; + const Value *v0 = SymbolsBeforeStripping[nSym]; if (const GlobalValue *GVar = dyn_cast(v)) { MCSymbol *Name = AP.getSymbol(GVar); - PointerType *PTy = dyn_cast(GVar->getType()); - bool IsNonGenericPointer = false; + PointerType *PTy = dyn_cast(v0->getType()); + bool IsNonGenericPointer = false; // Is v0 a non-generic pointer? if (PTy && PTy->getAddressSpace() != 0) { IsNonGenericPointer = true; } Index: lib/Target/NVPTX/NVPTXAsmPrinter.cpp =================================================================== --- lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -1179,7 +1179,7 @@ if ((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) || (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) { const Constant *Initializer = GVar->getInitializer(); - // 'undef' is treated as there is no value spefied. + // 'undef' is treated as there is no value specified. if (!Initializer->isNullValue() && !isa(Initializer)) { O << " = "; printScalarConstant(Initializer, O); @@ -1788,7 +1788,7 @@ } if (Cexpr->getOpcode() == Instruction::PtrToInt) { Value *v = Cexpr->getOperand(0)->stripPointerCasts(); - aggBuffer->addSymbol(v); + aggBuffer->addSymbol(v, Cexpr->getOperand(0)); aggBuffer->addZeros(4); break; } @@ -1810,7 +1810,7 @@ } if (Cexpr->getOpcode() == Instruction::PtrToInt) { Value *v = Cexpr->getOperand(0)->stripPointerCasts(); - aggBuffer->addSymbol(v); + aggBuffer->addSymbol(v, Cexpr->getOperand(0)); aggBuffer->addZeros(8); break; } @@ -1839,10 +1839,10 @@ } case Type::PointerTyID: { if (const GlobalValue *GVar = dyn_cast(CPV)) { - aggBuffer->addSymbol(GVar); + aggBuffer->addSymbol(GVar, GVar); } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) { const Value *v = Cexpr->stripPointerCasts(); - aggBuffer->addSymbol(v); + aggBuffer->addSymbol(v, Cexpr); } unsigned int s = TD->getTypeAllocSize(CPV->getType()); aggBuffer->addZeros(s); Index: test/CodeGen/NVPTX/addrspacecast-gvar.ll =================================================================== --- test/CodeGen/NVPTX/addrspacecast-gvar.ll +++ test/CodeGen/NVPTX/addrspacecast-gvar.ll @@ -3,7 +3,9 @@ ; CHECK: .visible .global .align 4 .u32 g = 42; ; CHECK: .visible .global .align 4 .u32 g2 = generic(g); ; CHECK: .visible .global .align 4 .u32 g3 = g; +; CHECK: .visible .global .align 8 .u32 g4[2] = {0, generic(g)}; @g = addrspace(1) global i32 42 @g2 = addrspace(1) global i32* addrspacecast (i32 addrspace(1)* @g to i32*) @g3 = addrspace(1) global i32 addrspace(1)* @g +@g4 = constant {i32*, i32*} {i32* null, i32* addrspacecast (i32 addrspace(1)* @g to i32*)}