[llvm-commits] [llvm] r44858 - in /llvm/trunk: include/llvm/Bitcode/LLVMBitCodes.h include/llvm/DerivedTypes.h include/llvm/GlobalVariable.h include/llvm/Instructions.h lib/AsmParser/LLLexer.cpp lib/AsmParser/llvmAsmParser.y lib/Bitcode/Reader/BitcodeReader.cpp lib/Bitcode/Writer/BitcodeWriter.cpp lib/VMCore/AsmWriter.cpp lib/VMCore/Constants.cpp lib/VMCore/Globals.cpp lib/VMCore/Instructions.cpp lib/VMCore/Type.cpp test/Assembler/2007-12-11-AddressSpaces.ll tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Christopher Lamb christopher.lamb at gmail.com
Tue Dec 11 00:59:06 PST 2007


Author: clamb
Date: Tue Dec 11 02:59:05 2007
New Revision: 44858

URL: http://llvm.org/viewvc/llvm-project?rev=44858&view=rev
Log:
Implement address space attribute for LLVM pointer types. Address spaces are 
regions of memory that have a target specific relationship, as described in the 
Embedded C Technical Report. 

This also implements the 2007-12-11-AddressSpaces test, 
which demonstrates how address space attributes can be used in LLVM IR.

In addition, this patch changes the bitcode signature for stores (in a backwards 
compatible manner), such that the pointer type, rather than the pointee type, is 
encoded. This permits type information in the pointer (e.g. address space) to be 
preserved for stores.

LangRef updates are forthcoming.

Added:
    llvm/trunk/test/Assembler/2007-12-11-AddressSpaces.ll
Modified:
    llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h
    llvm/trunk/include/llvm/DerivedTypes.h
    llvm/trunk/include/llvm/GlobalVariable.h
    llvm/trunk/include/llvm/Instructions.h
    llvm/trunk/lib/AsmParser/LLLexer.cpp
    llvm/trunk/lib/AsmParser/llvmAsmParser.y
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/VMCore/AsmWriter.cpp
    llvm/trunk/lib/VMCore/Constants.cpp
    llvm/trunk/lib/VMCore/Globals.cpp
    llvm/trunk/lib/VMCore/Instructions.cpp
    llvm/trunk/lib/VMCore/Type.cpp
    llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Modified: llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h (original)
+++ llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h Tue Dec 11 02:59:05 2007
@@ -46,7 +46,7 @@
     MODULE_CODE_SECTIONNAME = 5,    // SECTIONNAME: [strchr x N]
     MODULE_CODE_DEPLIB      = 6,    // DEPLIB:      [strchr x N]
 
-    // GLOBALVAR: [type, isconst, initid, 
+    // GLOBALVAR: [pointer type, isconst, initid, 
     //             linkage, alignment, section, visibility, threadlocal]
     MODULE_CODE_GLOBALVAR   = 7,
 
@@ -194,9 +194,13 @@
     FUNC_CODE_INST_FREE        = 18, // FREE:       [opty, op]
     FUNC_CODE_INST_ALLOCA      = 19, // ALLOCA:     [instty, op, align]
     FUNC_CODE_INST_LOAD        = 20, // LOAD:       [opty, op, align, vol]
-    FUNC_CODE_INST_STORE       = 21, // STORE:      [ptrty,val,ptr, align, vol]
+    FUNC_CODE_INST_STORE       = 21, // STORE:      [valty,val,ptr, align, vol]
     FUNC_CODE_INST_CALL        = 22, // CALL:       [attr, fnty, fnid, args...]
-    FUNC_CODE_INST_VAARG       = 23  // VAARG:      [valistty, valist, instty]
+    FUNC_CODE_INST_VAARG       = 23, // VAARG:      [valistty, valist, instty]
+    // This store code encodes the pointer type, rather than the value type
+    // this is so information only available in the pointer type (e.g. address
+    // spaces) is retained.
+    FUNC_CODE_INST_STORE2      = 24 // STORE:      [ptrty,ptr,val, align, vol]
   };
 } // End bitc namespace
 } // End llvm namespace

Modified: llvm/trunk/include/llvm/DerivedTypes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DerivedTypes.h?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/include/llvm/DerivedTypes.h (original)
+++ llvm/trunk/include/llvm/DerivedTypes.h Tue Dec 11 02:59:05 2007
@@ -363,12 +363,17 @@
 ///
 class PointerType : public SequentialType {
   friend class TypeMap<PointerValType, PointerType>;
+  unsigned AddressSpace;
+  
   PointerType(const PointerType &);                   // Do not implement
   const PointerType &operator=(const PointerType &);  // Do not implement
-  explicit PointerType(const Type *ElType);
+  explicit PointerType(const Type *ElType, unsigned AddrSpace);
 public:
   /// PointerType::get - This is the only way to construct a new pointer type.
-  static PointerType *get(const Type *ElementType);
+  static PointerType *get(const Type *ElementType, unsigned AddressSpace = 0);
+  
+  /// @brief Return the address space of the Pointer type.
+  inline unsigned getAddressSpace() const { return AddressSpace; }
 
   // Implement the AbstractTypeUser interface.
   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);

Modified: llvm/trunk/include/llvm/GlobalVariable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/GlobalVariable.h?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/include/llvm/GlobalVariable.h (original)
+++ llvm/trunk/include/llvm/GlobalVariable.h Tue Dec 11 02:59:05 2007
@@ -50,12 +50,14 @@
   /// automatically inserted into the end of the specified modules global list.
   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
                  Constant *Initializer = 0, const std::string &Name = "",
-                 Module *Parent = 0, bool ThreadLocal = false);
+                 Module *Parent = 0, bool ThreadLocal = false, 
+                 unsigned AddressSpace = 0);
   /// GlobalVariable ctor - This creates a global and inserts it before the
   /// specified other global.
   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
                  Constant *Initializer, const std::string &Name,
-                 GlobalVariable *InsertBefore, bool ThreadLocal = false);
+                 GlobalVariable *InsertBefore, bool ThreadLocal = false, 
+                 unsigned AddressSpace = 0);
   
   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
   /// the global variable is defined in some other translation unit, and is thus

Modified: llvm/trunk/include/llvm/Instructions.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Instructions.h?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Instructions.h (original)
+++ llvm/trunk/include/llvm/Instructions.h Tue Dec 11 02:59:05 2007
@@ -455,7 +455,8 @@
                     Instruction *InsertBefore =0)
       : Instruction(PointerType::get(
                       checkType(getIndexedType(Ptr->getType(),
-                                               IdxBegin, IdxEnd, true))),
+                                               IdxBegin, IdxEnd, true)),
+                      cast<PointerType>(Ptr->getType())->getAddressSpace()),
                     GetElementPtr, 0, 0, InsertBefore) {
     init(Ptr, IdxBegin, IdxEnd, Name,
          typename std::iterator_traits<InputIterator>::iterator_category());
@@ -465,7 +466,8 @@
                     const std::string &Name, BasicBlock *InsertAtEnd)
       : Instruction(PointerType::get(
                       checkType(getIndexedType(Ptr->getType(),
-                                               IdxBegin, IdxEnd, true))),
+                                               IdxBegin, IdxEnd, true)),
+                      cast<PointerType>(Ptr->getType())->getAddressSpace()),
                     GetElementPtr, 0, 0, InsertAtEnd) {
     init(Ptr, IdxBegin, IdxEnd, Name,
          typename std::iterator_traits<InputIterator>::iterator_category());

Modified: llvm/trunk/lib/AsmParser/LLLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLLexer.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/AsmParser/LLLexer.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLLexer.cpp Tue Dec 11 02:59:05 2007
@@ -463,6 +463,7 @@
   KEYWORD("datalayout", DATALAYOUT);
   KEYWORD("volatile", VOLATILE);
   KEYWORD("align", ALIGN);
+  KEYWORD("addrspace", ADDRSPACE);
   KEYWORD("section", SECTION);
   KEYWORD("alias", ALIAS);
   KEYWORD("module", MODULE);

Modified: llvm/trunk/lib/AsmParser/llvmAsmParser.y
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/llvmAsmParser.y?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/AsmParser/llvmAsmParser.y (original)
+++ llvm/trunk/lib/AsmParser/llvmAsmParser.y Tue Dec 11 02:59:05 2007
@@ -491,7 +491,8 @@
    if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
      V = new Function(FTy, GlobalValue::ExternalLinkage);
    else
-     V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
+     V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
+                            (Module*)0, false, PTy->getAddressSpace());
    break;
   }
   default:
@@ -722,13 +723,14 @@
                     GlobalValue::LinkageTypes Linkage,
                     GlobalValue::VisibilityTypes Visibility,
                     bool isConstantGlobal, const Type *Ty,
-                    Constant *Initializer, bool IsThreadLocal) {
+                    Constant *Initializer, bool IsThreadLocal,
+                    unsigned AddressSpace = 0) {
   if (isa<FunctionType>(Ty)) {
     GenerateError("Cannot declare global vars of function type");
     return 0;
   }
 
-  const PointerType *PTy = PointerType::get(Ty);
+  const PointerType *PTy = PointerType::get(Ty, AddressSpace);
 
   std::string Name;
   if (NameStr) {
@@ -780,7 +782,7 @@
   // Otherwise there is no existing GV to use, create one now.
   GlobalVariable *GV =
     new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
-                       CurModule.CurrentModule, IsThreadLocal);
+                       CurModule.CurrentModule, IsThreadLocal, AddressSpace);
   GV->setVisibility(Visibility);
   InsertValue(GV, CurModule.Values);
   return GV;
@@ -1054,7 +1056,7 @@
 %token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
 %token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
 %token DLLIMPORT DLLEXPORT EXTERN_WEAK
-%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
+%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
 %token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
 %token DATALAYOUT
@@ -1268,6 +1270,7 @@
 };
 
 
+
 SectionString : SECTION STRINGCONSTANT {
   for (unsigned i = 0, e = $2->length(); i != e; ++i)
     if ((*$2)[i] == '"' || (*$2)[i] == '\\')
@@ -1320,6 +1323,13 @@
     delete $1;
     CHECK_FOR_ERROR
   }
+  | Types ADDRSPACE '(' EUINT64VAL ')' '*' {             // Pointer type?
+    if (*$1 == Type::LabelTy)
+      GEN_ERROR("Cannot form a pointer to a basic block");
+    $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $4)));
+    delete $1;
+    CHECK_FOR_ERROR
+  }
   | SymbolicValueRef {            // Named types are also simple types...
     const Type* tmp = getTypeVal($1);
     CHECK_FOR_ERROR
@@ -2073,6 +2083,17 @@
   } GlobalVarAttributes {
     CurGV = 0;
   }
+  | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
+    ADDRSPACE '(' EUINT64VAL ')' { 
+    /* "Externally Visible" Linkage with address space qualifier */
+    if ($5 == 0) 
+      GEN_ERROR("Global value initializer is not a constant");
+    CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
+                                $2, $4, $5->getType(), $5, $3, $8);
+    CHECK_FOR_ERROR
+  } GlobalVarAttributes {
+    CurGV = 0;
+  }
   | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
     ConstVal {
     if ($6 == 0) 

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Tue Dec 11 02:59:05 2007
@@ -323,11 +323,16 @@
       
       ResultTy = IntegerType::get(Record[0]);
       break;
-    case bitc::TYPE_CODE_POINTER:   // POINTER: [pointee type]
+    case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 
+                                    //          [pointee type, address space]
       if (Record.size() < 1)
         return Error("Invalid POINTER type record");
-      ResultTy = PointerType::get(getTypeByID(Record[0], true));
+      unsigned AddressSpace = 0;
+      if (Record.size() == 2)
+        AddressSpace = Record[1];
+      ResultTy = PointerType::get(getTypeByID(Record[0], true), AddressSpace);
       break;
+    }
     case bitc::TYPE_CODE_FUNCTION: {
       // FIXME: attrid is dead, remove it in LLVM 3.0
       // FUNCTION: [vararg, attrid, retty, paramty x N]
@@ -982,7 +987,7 @@
       CollectorTable.push_back(S);
       break;
     }
-    // GLOBALVAR: [type, isconst, initid, 
+    // GLOBALVAR: [pointer type, isconst, initid,
     //             linkage, alignment, section, visibility, threadlocal]
     case bitc::MODULE_CODE_GLOBALVAR: {
       if (Record.size() < 6)
@@ -990,6 +995,7 @@
       const Type *Ty = getTypeByID(Record[0]);
       if (!isa<PointerType>(Ty))
         return Error("Global not a pointer type!");
+      unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
       Ty = cast<PointerType>(Ty)->getElementType();
       
       bool isConstant = Record[1];
@@ -1009,7 +1015,8 @@
         isThreadLocal = Record[7];
 
       GlobalVariable *NewGV =
-        new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule);
+        new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule, 
+                           isThreadLocal, AddressSpace);
       NewGV->setAlignment(Alignment);
       if (!Section.empty())
         NewGV->setSection(Section);
@@ -1485,7 +1492,20 @@
       I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
       break;
     }
+    case bitc::FUNC_CODE_INST_STORE2: { // STORE2:[ptrty, ptr, val, align, vol]
+      unsigned OpNum = 0;
+      Value *Val, *Ptr;
+      if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
+          getValue(Record, OpNum, 
+                    cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
+          OpNum+2 != Record.size())
+        return Error("Invalid STORE record");
+      
+      I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
+      break;
+    }
     case bitc::FUNC_CODE_INST_STORE: { // STORE:[val, valty, ptr, align, vol]
+      // FIXME: Legacy form of store instruction. Should be removed in LLVM 3.0.
       unsigned OpNum = 0;
       Value *Val, *Ptr;
       if (getValueTypePair(Record, OpNum, NextValueNo, Val) ||

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Tue Dec 11 02:59:05 2007
@@ -197,10 +197,14 @@
       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
       break;
     case Type::PointerTyID:
-      // POINTER: [pointee type]
+      const PointerType *PTy = cast<PointerType>(T);
+      // POINTER: [pointee type] or [pointee type, address space]
       Code = bitc::TYPE_CODE_POINTER;
-      TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
-      AbbrevToUse = PtrAbbrev;
+      TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
+      if (unsigned AddressSpace = PTy->getAddressSpace())
+        TypeVals.push_back(AddressSpace);
+      else
+        AbbrevToUse = PtrAbbrev;
       break;
 
     case Type::FunctionTyID: {
@@ -829,9 +833,9 @@
     Vals.push_back(cast<LoadInst>(I).isVolatile());
     break;
   case Instruction::Store:
-    Code = bitc::FUNC_CODE_INST_STORE;
-    PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // val.
-    Vals.push_back(VE.getValueID(I.getOperand(1)));       // ptr.
+    Code = bitc::FUNC_CODE_INST_STORE2;
+    PushValueAndType(I.getOperand(1), InstID, Vals, VE);  // ptrty + ptr
+    Vals.push_back(VE.getValueID(I.getOperand(0)));       // val.
     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
     Vals.push_back(cast<StoreInst>(I).isVolatile());
     break;

Modified: llvm/trunk/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/AsmWriter.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/trunk/lib/VMCore/AsmWriter.cpp Tue Dec 11 02:59:05 2007
@@ -334,11 +334,15 @@
       Result += '>';
     break;
   }
-  case Type::PointerTyID:
-    calcTypeName(cast<PointerType>(Ty)->getElementType(),
+  case Type::PointerTyID: {
+    const PointerType *PTy = cast<PointerType>(Ty);
+    calcTypeName(PTy->getElementType(),
                           TypeStack, TypeNames, Result);
+    if (unsigned AddressSpace = PTy->getAddressSpace())
+      Result += " addrspace(" + utostr(AddressSpace) + ")";
     Result += "*";
     break;
+  }
   case Type::ArrayTyID: {
     const ArrayType *ATy = cast<ArrayType>(Ty);
     Result += "[" + utostr(ATy->getNumElements()) + " x ";
@@ -951,6 +955,9 @@
     writeOperand(GV->getInitializer(), false);
   }
 
+  if (unsigned AddressSpace = GV->getType()->getAddressSpace())
+    Out << " addrspace(" << AddressSpace << ") ";
+    
   if (GV->hasSection())
     Out << ", section \"" << GV->getSection() << '"';
   if (GV->getAlignment())

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Tue Dec 11 02:59:05 2007
@@ -1860,7 +1860,8 @@
   const Type *Ty = 
     GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx, true);
   assert(Ty && "GEP indices invalid!");
-  return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
+  unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
+  return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
 }
 
 Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,

Modified: llvm/trunk/lib/VMCore/Globals.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Globals.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Globals.cpp (original)
+++ llvm/trunk/lib/VMCore/Globals.cpp Tue Dec 11 02:59:05 2007
@@ -85,8 +85,9 @@
 
 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
                                Constant *InitVal, const std::string &Name,
-                               Module *ParentModule, bool ThreadLocal)
-  : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
+                               Module *ParentModule, bool ThreadLocal, 
+                               unsigned AddressSpace)
+  : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
                 &Initializer, InitVal != 0, Link, Name),
     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
   if (InitVal) {
@@ -105,8 +106,9 @@
 
 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
                                Constant *InitVal, const std::string &Name,
-                               GlobalVariable *Before, bool ThreadLocal)
-  : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
+                               GlobalVariable *Before, bool ThreadLocal,
+                               unsigned AddressSpace)
+  : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
                 &Initializer, InitVal != 0, Link, Name), 
     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
   if (InitVal) {

Modified: llvm/trunk/lib/VMCore/Instructions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Instructions.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Instructions.cpp (original)
+++ llvm/trunk/lib/VMCore/Instructions.cpp Tue Dec 11 02:59:05 2007
@@ -937,7 +937,8 @@
 
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                                      const std::string &Name, Instruction *InBe)
-  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
+  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
+                      cast<PointerType>(Ptr->getType())->getAddressSpace()),
                 GetElementPtr, 0, 0, InBe) {
   init(Ptr, Idx);
   setName(Name);
@@ -945,7 +946,8 @@
 
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                                      const std::string &Name, BasicBlock *IAE)
-  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
+  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
+                      cast<PointerType>(Ptr->getType())->getAddressSpace()),
                 GetElementPtr, 0, 0, IAE) {
   init(Ptr, Idx);
   setName(Name);

Modified: llvm/trunk/lib/VMCore/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Type.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Type.cpp (original)
+++ llvm/trunk/lib/VMCore/Type.cpp Tue Dec 11 02:59:05 2007
@@ -338,7 +338,10 @@
   }
   case Type::PointerTyID: {
     const PointerType *PTy = cast<PointerType>(Ty);
-    Result = getTypeDescription(PTy->getElementType(), TypeStack) + " *";
+    Result = getTypeDescription(PTy->getElementType(), TypeStack);
+    if (unsigned AddressSpace = PTy->getAddressSpace())
+      Result += " addrspace(" + utostr(AddressSpace) + ")";
+    Result += " *";
     break;
   }
   case Type::ArrayTyID: {
@@ -492,7 +495,9 @@
 }
 
 
-PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
+PointerType::PointerType(const Type *E, unsigned AddrSpace)
+  : SequentialType(PointerTyID, E) {
+  AddressSpace = AddrSpace;
   // Calculate whether or not this type is abstract
   setAbstract(E->isAbstract());
 }
@@ -634,8 +639,9 @@
     const IntegerType *ITy2 = cast<IntegerType>(Ty2);
     return ITy->getBitWidth() == ITy2->getBitWidth();
   } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
-    return TypesEqual(PTy->getElementType(),
-                      cast<PointerType>(Ty2)->getElementType(), EqTypes);
+    const PointerType *PTy2 = cast<PointerType>(Ty2);
+    return PTy->getAddressSpace() == PTy2->getAddressSpace() &&
+           TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
   } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
     const StructType *STy2 = cast<StructType>(Ty2);
     if (STy->getNumElements() != STy2->getNumElements()) return false;
@@ -756,6 +762,9 @@
     case Type::StructTyID:
       HashVal ^= cast<StructType>(SubTy)->getNumElements();
       break;
+    case Type::PointerTyID:
+      HashVal ^= cast<PointerType>(SubTy)->getAddressSpace();
+      break;
     }
   }
   return HashVal ? HashVal : 1;  // Do not return zero unless opaque subty.
@@ -1251,11 +1260,12 @@
 namespace llvm {
 class PointerValType {
   const Type *ValTy;
+  unsigned AddressSpace;
 public:
-  PointerValType(const Type *val) : ValTy(val) {}
+  PointerValType(const Type *val, unsigned as) : ValTy(val), AddressSpace(as) {}
 
   static PointerValType get(const PointerType *PT) {
-    return PointerValType(PT->getElementType());
+    return PointerValType(PT->getElementType(), PT->getAddressSpace());
   }
 
   static unsigned hashTypeStructure(const PointerType *PT) {
@@ -1263,25 +1273,26 @@
   }
 
   bool operator<(const PointerValType &MTV) const {
-    return ValTy < MTV.ValTy;
+    if (AddressSpace < MTV.AddressSpace) return true;
+    return AddressSpace == MTV.AddressSpace && ValTy < MTV.ValTy;
   }
 };
 }
 
 static ManagedStatic<TypeMap<PointerValType, PointerType> > PointerTypes;
 
-PointerType *PointerType::get(const Type *ValueType) {
+PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) {
   assert(ValueType && "Can't get a pointer to <null> type!");
   assert(ValueType != Type::VoidTy &&
          "Pointer to void is not valid, use sbyte* instead!");
   assert(ValueType != Type::LabelTy && "Pointer to label is not valid!");
-  PointerValType PVT(ValueType);
+  PointerValType PVT(ValueType, AddressSpace);
 
   PointerType *PT = PointerTypes->get(PVT);
   if (PT) return PT;
 
   // Value not found.  Derive a new type!
-  PointerTypes->add(PVT, PT = new PointerType(ValueType));
+  PointerTypes->add(PVT, PT = new PointerType(ValueType, AddressSpace));
 
 #ifdef DEBUG_MERGE_TYPES
   DOUT << "Derived new type: " << *PT << "\n";

Added: llvm/trunk/test/Assembler/2007-12-11-AddressSpaces.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/2007-12-11-AddressSpaces.ll?rev=44858&view=auto

==============================================================================
--- llvm/trunk/test/Assembler/2007-12-11-AddressSpaces.ll (added)
+++ llvm/trunk/test/Assembler/2007-12-11-AddressSpaces.ll Tue Dec 11 02:59:05 2007
@@ -0,0 +1,25 @@
+; RUN: llvm-as < %s | llvm-dis |& grep {addrspace(33)} | count 7
+; RUN: llvm-as < %s | llvm-dis |& grep {addrspace(42)} | count 2
+; RUN: llvm-as < %s | llvm-dis |& grep {addrspace(66)} | count 2
+; RUN: llvm-as < %s | llvm-dis |& grep {addrspace(11)} | count 6
+; RUN: llvm-as < %s | llvm-dis |& grep {addrspace(22)} | count 5
+
+	%struct.mystruct = type { i32, i32 addrspace(33)*, i32, i32 addrspace(33)* }
+ at input = global %struct.mystruct zeroinitializer addrspace(42) 		; <%struct.mystruct addrspace(42)*> [#uses=1]
+ at output = global %struct.mystruct zeroinitializer addrspace(66) 		; <%struct.mystruct addrspace(66)*> [#uses=1]
+ at y = global i32 addrspace(11)* addrspace(22)* null addrspace(33) 		; <i32 addrspace(11)* addrspace(22)* addrspace(33)*> [#uses=1]
+
+define void @foo() {
+entry:
+	%tmp1 = load i32 addrspace(33)* addrspace(42)* getelementptr (%struct.mystruct addrspace(42)* @input, i32 0, i32 3), align 4		; <i32 addrspace(33)*> [#uses=1]
+	store i32 addrspace(33)* %tmp1, i32 addrspace(33)* addrspace(66)* getelementptr (%struct.mystruct addrspace(66)* @output, i32 0, i32 1), align 4
+	ret void
+}
+
+define i32 addrspace(11)* @bar(i32 addrspace(11)* addrspace(22)* addrspace(33)* %x) {
+entry:
+	%tmp1 = load i32 addrspace(11)* addrspace(22)* addrspace(33)* @y, align 4		; <i32 addrspace(11)* addrspace(22)*> [#uses=2]
+	store i32 addrspace(11)* addrspace(22)* %tmp1, i32 addrspace(11)* addrspace(22)* addrspace(33)* %x, align 4
+	%tmp5 = load i32 addrspace(11)* addrspace(22)* %tmp1, align 4		; <i32 addrspace(11)*> [#uses=1]
+	ret i32 addrspace(11)* %tmp5
+}

Modified: llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp?rev=44858&r1=44857&r2=44858&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Tue Dec 11 02:59:05 2007
@@ -197,6 +197,7 @@
     case bitc::FUNC_CODE_INST_STORE:       return "INST_STORE";
     case bitc::FUNC_CODE_INST_CALL:        return "INST_CALL";
     case bitc::FUNC_CODE_INST_VAARG:       return "INST_VAARG";
+    case bitc::FUNC_CODE_INST_STORE2:      return "INST_STORE2";
     }
   case bitc::TYPE_SYMTAB_BLOCK_ID:
     switch (CodeID) {





More information about the llvm-commits mailing list