From 4a6c2a4b4f979af435752d8ef1a999d332977a0a Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Thu, 1 Nov 2012 09:37:49 +0000 Subject: [PATCH] Teach Type::getPointerAddressSpace to look through pointer vectors 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. llvm-svn: 167223 --- llvm/include/llvm/Instructions.h | 40 +++++++--------------------------------- llvm/include/llvm/Type.h | 4 +++- llvm/lib/VMCore/Type.cpp | 2 +- 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/llvm/include/llvm/Instructions.h b/llvm/include/llvm/Instructions.h index 40dbbaa..e3fd216 100644 --- a/llvm/include/llvm/Instructions.h +++ b/llvm/include/llvm/Instructions.h @@ -226,7 +226,7 @@ public: static unsigned getPointerOperandIndex() { return 0U; } unsigned getPointerAddressSpace() const { - return cast(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(getPointerOperand()->getType()) - ->getAddressSpace(); - if (getPointerOperand()->getType()->isVectorTy() - && cast(getPointerOperand()->getType())->isPointerTy()) - return cast(cast( - 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(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(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(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(getType())->getAddressSpace(); - if (getType()->isVectorTy() && - cast(getType())->getElementType()->isPointerTy()) - return cast( - cast(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(Ty)->getAddressSpace(); - if (Ty->isVectorTy() - && cast(Ty)->getElementType()->isPointerTy()) - return cast( - cast(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: diff --git a/llvm/include/llvm/Type.h b/llvm/include/llvm/Type.h index 10e14dd..def4575 100644 --- a/llvm/include/llvm/Type.h +++ b/llvm/include/llvm/Type.h @@ -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 diff --git a/llvm/lib/VMCore/Type.cpp b/llvm/lib/VMCore/Type.cpp index 1bbd2c6..1656ab2 100644 --- a/llvm/lib/VMCore/Type.cpp +++ b/llvm/lib/VMCore/Type.cpp @@ -215,7 +215,7 @@ unsigned Type::getVectorNumElements() const { } unsigned Type::getPointerAddressSpace() const { - return cast(this)->getAddressSpace(); + return cast(getScalarType())->getAddressSpace(); } -- 2.7.4