Teach Type::getPointerAddressSpace to look through pointer vectors
authorChandler Carruth <chandlerc@gmail.com>
Thu, 1 Nov 2012 09:37:49 +0000 (09:37 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Thu, 1 Nov 2012 09:37:49 +0000 (09:37 +0000)
politely and document this feature.

This simple API extension then allows us to write all of the
Instructions' address space query methods much more simply. No
functionality change intended here.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167223 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Instructions.h
include/llvm/Type.h
lib/VMCore/Type.cpp

index 40dbbaabe69a45a010dcbe57c5151826fc7d1714..e3fd2161c828c5371343167470a197d5769dc56f 100644 (file)
@@ -226,7 +226,7 @@ public:
   static unsigned getPointerOperandIndex() { return 0U; }
 
   unsigned getPointerAddressSpace() const {
-    return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
+    return getPointerOperand()->getType()->getPointerAddressSpace();
   }
 
 
@@ -348,16 +348,7 @@ public:
   static unsigned getPointerOperandIndex() { return 1U; }
 
   unsigned getPointerAddressSpace() const {
-    if (getPointerOperand()->getType()->isPointerTy())
-      return cast<PointerType>(getPointerOperand()->getType())
-        ->getAddressSpace();
-    if (getPointerOperand()->getType()->isVectorTy()
-        && cast<VectorType>(getPointerOperand()->getType())->isPointerTy())
-      return cast<PointerType>(cast<VectorType>(
-            getPointerOperand()->getType())->getElementType())
-        ->getAddressSpace();
-    llvm_unreachable("Only a vector of pointers or pointers can be used!");
-    return 0;
+    return getPointerOperand()->getType()->getPointerAddressSpace();
   }
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -527,7 +518,7 @@ public:
   const Value *getNewValOperand() const { return getOperand(2); }
   
   unsigned getPointerAddressSpace() const {
-    return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
+    return getPointerOperand()->getType()->getPointerAddressSpace();
   }
   
   // Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -670,7 +661,7 @@ public:
   const Value *getValOperand() const { return getOperand(1); }
 
   unsigned getPointerAddressSpace() const {
-    return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
+    return getPointerOperand()->getType()->getPointerAddressSpace();
   }
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -801,7 +792,7 @@ public:
   }
 
   unsigned getPointerAddressSpace() const {
-    return cast<PointerType>(getPointerOperandType())->getAddressSpace();
+    return getPointerOperand()->getType()->getPointerAddressSpace();
   }
 
   /// getPointerOperandType - Method to return the pointer operand as a
@@ -3616,15 +3607,7 @@ public:
 
   /// @brief return the address space of the pointer.
   unsigned getAddressSpace() const {
-    if (getType()->isPointerTy()) 
-      return cast<PointerType>(getType())->getAddressSpace();
-    if (getType()->isVectorTy() &&
-        cast<VectorType>(getType())->getElementType()->isPointerTy())
-      return cast<PointerType>(
-          cast<VectorType>(getType())->getElementType())
-        ->getAddressSpace();
-    llvm_unreachable("Must be a pointer or a vector of pointers.");
-    return 0;
+    return getType()->getPointerAddressSpace();
   }
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -3665,16 +3648,7 @@ public:
 
   /// @brief return the address space of the pointer.
   unsigned getPointerAddressSpace() const {
-    Type *Ty = getOperand(0)->getType();
-    if (Ty->isPointerTy())
-      return cast<PointerType>(Ty)->getAddressSpace();
-    if (Ty->isVectorTy()
-        && cast<VectorType>(Ty)->getElementType()->isPointerTy())
-      return cast<PointerType>(
-          cast<VectorType>(Ty)->getElementType())
-        ->getAddressSpace();
-    llvm_unreachable("Must be a pointer or a vector of pointers.");
-    return 0;
+    return getOperand(0)->getType()->getPointerAddressSpace();
   }
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
index 10e14dd8dea0ee9ceff8d85499f265b4319253ea..def45750dd71672a4577b354d4e3c4209a18e352 100644 (file)
@@ -346,8 +346,10 @@ public:
   unsigned getVectorNumElements() const;
   Type *getVectorElementType() const { return getSequentialElementType(); }
 
-  unsigned getPointerAddressSpace() const;
   Type *getPointerElementType() const { return getSequentialElementType(); }
+
+  /// \brief Get the address space of this pointer or pointer vector type.
+  unsigned getPointerAddressSpace() const;
   
   //===--------------------------------------------------------------------===//
   // Static members exported by the Type class itself.  Useful for getting
index 1bbd2c6cf0b554adcde37e5cb6e892583ccc2d34..1656ab2cab3a8b86bc4bdb0ef834fd017df339b3 100644 (file)
@@ -215,7 +215,7 @@ unsigned Type::getVectorNumElements() const {
 }
 
 unsigned Type::getPointerAddressSpace() const {
-  return cast<PointerType>(this)->getAddressSpace();
+  return cast<PointerType>(getScalarType())->getAddressSpace();
 }