[llvm] r178147 - Cleanup the simplify_type implementation.

Eli Friedman eli.friedman at gmail.com
Mon Jul 15 20:34:05 PDT 2013


On Wed, Mar 27, 2013 at 9:43 AM, Rafael Espindola
<rafael.espindola at gmail.com> wrote:
> Author: rafael
> Date: Wed Mar 27 11:43:11 2013
> New Revision: 178147
>
> URL: http://llvm.org/viewvc/llvm-project?rev=178147&view=rev
> Log:
> Cleanup the simplify_type implementation.
>
> As far as simplify_type is concerned, there are 3 kinds of smart pointers:
>
> * const correct: A 'const MyPtr<int> &' produces a 'const int*'. A
> 'MyPtr<int> &' produces a 'int *'.
> * always const: Even a 'MyPtr<int> &' produces a 'const int*'.
> * no const: Even a 'const MyPtr<int> &' produces a 'int*'.
>
> This patch then does the following:
>
> * Removes the unused specializations. Since they are unused, it is hard
> to know which kind should be implemented.
> * Make sure we don't drop const.
> * Fix the default forwarding so that const correct pointer only need
> one specialization.
> * Simplifies the existing specializations.
>
> Modified:
>     llvm/trunk/include/llvm/ADT/IntrusiveRefCntPtr.h
>     llvm/trunk/include/llvm/ADT/Optional.h
>     llvm/trunk/include/llvm/ADT/ilist.h
>     llvm/trunk/include/llvm/CodeGen/SelectionDAGNodes.h
>     llvm/trunk/include/llvm/IR/Use.h
>     llvm/trunk/include/llvm/IR/User.h
>     llvm/trunk/include/llvm/Support/Casting.h
>     llvm/trunk/include/llvm/Support/ValueHandle.h
>     llvm/trunk/include/llvm/Support/type_traits.h
>
> Modified: llvm/trunk/include/llvm/ADT/IntrusiveRefCntPtr.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/IntrusiveRefCntPtr.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/IntrusiveRefCntPtr.h (original)
> +++ llvm/trunk/include/llvm/ADT/IntrusiveRefCntPtr.h Wed Mar 27 11:43:11 2013
> @@ -226,13 +226,13 @@ namespace llvm {
>
>    template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
>      typedef T* SimpleType;
> -    static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
> +    static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T>& Val) {
>        return Val.getPtr();
>      }
>    };
>
>    template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
> -    typedef T* SimpleType;
> +    typedef /*const*/ T* SimpleType;
>      static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
>        return Val.getPtr();
>      }
>
> Modified: llvm/trunk/include/llvm/ADT/Optional.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Optional.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/Optional.h (original)
> +++ llvm/trunk/include/llvm/ADT/Optional.h Wed Mar 27 11:43:11 2013
> @@ -128,20 +128,6 @@ public:
>  #endif
>  };
>
> -template<typename T> struct simplify_type;
> -
> -template <typename T>
> -struct simplify_type<const Optional<T> > {
> -  typedef const T* SimpleType;
> -  static SimpleType getSimplifiedValue(const Optional<T> &Val) {
> -    return Val.getPointer();
> -  }
> -};
> -
> -template <typename T>
> -struct simplify_type<Optional<T> >
> -  : public simplify_type<const Optional<T> > {};
> -
>  template <typename T> struct isPodLike;
>  template <typename T> struct isPodLike<Optional<T> > {
>    // An Optional<T> is pod-like if T is.
>
> Modified: llvm/trunk/include/llvm/ADT/ilist.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ilist.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/ilist.h (original)
> +++ llvm/trunk/include/llvm/ADT/ilist.h Wed Mar 27 11:43:11 2013
> @@ -274,12 +274,12 @@ template<typename From> struct simplify_
>  template<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > {
>    typedef NodeTy* SimpleType;
>
> -  static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
> +  static SimpleType getSimplifiedValue(ilist_iterator<NodeTy> &Node) {
>      return &*Node;
>    }
>  };
>  template<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > {
> -  typedef NodeTy* SimpleType;
> +  typedef /*const*/ NodeTy* SimpleType;
>
>    static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
>      return &*Node;
>
> Modified: llvm/trunk/include/llvm/CodeGen/SelectionDAGNodes.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SelectionDAGNodes.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/CodeGen/SelectionDAGNodes.h (original)
> +++ llvm/trunk/include/llvm/CodeGen/SelectionDAGNodes.h Wed Mar 27 11:43:11 2013
> @@ -196,14 +196,14 @@ template <> struct isPodLike<SDValue> {
>  /// SDValues as if they were SDNode*'s.
>  template<> struct simplify_type<SDValue> {
>    typedef SDNode* SimpleType;
> -  static SimpleType getSimplifiedValue(const SDValue &Val) {
> -    return static_cast<SimpleType>(Val.getNode());
> +  static SimpleType getSimplifiedValue(SDValue &Val) {
> +    return Val.getNode();
>    }
>  };
>  template<> struct simplify_type<const SDValue> {
> -  typedef SDNode* SimpleType;
> +  typedef /*const*/ SDNode* SimpleType;
>    static SimpleType getSimplifiedValue(const SDValue &Val) {
> -    return static_cast<SimpleType>(Val.getNode());
> +    return Val.getNode();
>    }
>  };
>
> @@ -295,14 +295,8 @@ private:
>  /// SDValues as if they were SDNode*'s.
>  template<> struct simplify_type<SDUse> {
>    typedef SDNode* SimpleType;
> -  static SimpleType getSimplifiedValue(const SDUse &Val) {
> -    return static_cast<SimpleType>(Val.getNode());
> -  }
> -};
> -template<> struct simplify_type<const SDUse> {
> -  typedef SDNode* SimpleType;
> -  static SimpleType getSimplifiedValue(const SDUse &Val) {
> -    return static_cast<SimpleType>(Val.getNode());
> +  static SimpleType getSimplifiedValue(SDUse &Val) {
> +    return Val.getNode();
>    }
>  };
>
>
> Modified: llvm/trunk/include/llvm/IR/Use.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Use.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/IR/Use.h (original)
> +++ llvm/trunk/include/llvm/IR/Use.h Wed Mar 27 11:43:11 2013
> @@ -149,14 +149,14 @@ private:
>  // casting operators.
>  template<> struct simplify_type<Use> {
>    typedef Value* SimpleType;
> -  static SimpleType getSimplifiedValue(const Use &Val) {
> -    return static_cast<SimpleType>(Val.get());
> +  static SimpleType getSimplifiedValue(Use &Val) {
> +    return Val.get();
>    }
>  };
>  template<> struct simplify_type<const Use> {
> -  typedef Value* SimpleType;
> +  typedef /*const*/ Value* SimpleType;
>    static SimpleType getSimplifiedValue(const Use &Val) {
> -    return static_cast<SimpleType>(Val.get());
> +    return Val.get();
>    }
>  };
>
>
> Modified: llvm/trunk/include/llvm/IR/User.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/User.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/IR/User.h (original)
> +++ llvm/trunk/include/llvm/IR/User.h Wed Mar 27 11:43:11 2013
> @@ -183,27 +183,17 @@ public:
>
>  template<> struct simplify_type<User::op_iterator> {
>    typedef Value* SimpleType;
> -
> -  static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
> -    return static_cast<SimpleType>(Val->get());
> +  static SimpleType getSimplifiedValue(User::op_iterator &Val) {
> +    return Val->get();
>    }
>  };
> -
> -template<> struct simplify_type<const User::op_iterator>
> -  : public simplify_type<User::op_iterator> {};
> -
>  template<> struct simplify_type<User::const_op_iterator> {
> -  typedef Value* SimpleType;
> -
> -  static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
> -    return static_cast<SimpleType>(Val->get());
> +  typedef /*const*/ Value* SimpleType;
> +  static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
> +    return Val->get();
>    }
>  };
>
> -template<> struct simplify_type<const User::const_op_iterator>
> -  : public simplify_type<User::const_op_iterator> {};
> -
> -
>  // value_use_iterator::getOperandNo - Requires the definition of the User class.
>  template<typename UserTy>
>  unsigned value_use_iterator<UserTy>::getOperandNo() const {
>
> Modified: llvm/trunk/include/llvm/Support/Casting.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Casting.h?rev=178147&r1=178146&r2=178147&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Support/Casting.h (original)
> +++ llvm/trunk/include/llvm/Support/Casting.h Wed Mar 27 11:43:11 2013
> @@ -36,9 +36,13 @@ template<typename From> struct simplify_
>  };
>
>  template<typename From> struct simplify_type<const From> {
> -  typedef const From SimpleType;
> -  static SimpleType &getSimplifiedValue(const From &Val) {
> -    return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
> +  typedef typename simplify_type<From>::SimpleType NonConstSimpleType;
> +  typedef typename add_const_past_pointer<NonConstSimpleType>::type
> +    SimpleType;
> +  typedef typename add_lvalue_reference_if_not_pointer<SimpleType>::type
> +    RetType;
> +  static RetType getSimplifiedValue(const From& Val) {
> +    return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val));
>    }
>  };
>
> @@ -81,6 +85,13 @@ template <typename To, typename From> st
>    }
>  };
>
> +template <typename To, typename From> struct isa_impl_cl<To, From*const> {
> +  static inline bool doit(const From *Val) {
> +    assert(Val && "isa<> used on a null pointer");
> +    return isa_impl<To, From>::doit(*Val);
> +  }
> +};
> +
>  template <typename To, typename From> struct isa_impl_cl<To, const From*> {
>    static inline bool doit(const From *Val) {
>      assert(Val && "isa<> used on a null pointer");
> @@ -102,7 +113,7 @@ struct isa_impl_wrap {
>    static bool doit(const From &Val) {
>      return isa_impl_wrap<To, SimpleFrom,
>        typename simplify_type<SimpleFrom>::SimpleType>::doit(
> -                          simplify_type<From>::getSimplifiedValue(Val));
> +                          simplify_type<const From>::getSimplifiedValue(Val));
>    }
>  };
>
> @@ -121,7 +132,8 @@ struct isa_impl_wrap<To, FromTy, FromTy>
>  //
>  template <class X, class Y>
>  inline bool isa(const Y &Val) {
> -  return isa_impl_wrap<X, Y, typename simplify_type<Y>::SimpleType>::doit(Val);
> +  return isa_impl_wrap<X, const Y,
> +                       typename simplify_type<const Y>::SimpleType>::doit(Val);
>  }
>
>  //===----------------------------------------------------------------------===//
> @@ -178,7 +190,7 @@ struct cast_retty {
>  //
>  template<class To, class From, class SimpleFrom> struct cast_convert_val {
>    // This is not a simple type, use the template to simplify it...
> -  static typename cast_retty<To, From>::ret_type doit(const From &Val) {
> +  static typename cast_retty<To, From>::ret_type doit(From &Val) {
>      return cast_convert_val<To, SimpleFrom,
>        typename simplify_type<SimpleFrom>::SimpleType>::doit(
>                            simplify_type<From>::getSimplifiedValue(Val));
> @@ -204,20 +216,14 @@ template<class To, class FromTy> struct
>  //  cast<Instruction>(myVal)->getParent()
>  //
>  template <class X, class Y>
> -inline typename enable_if_c<
> -  !is_same<Y, typename simplify_type<Y>::SimpleType>::value,
> -  typename cast_retty<X, Y>::ret_type
> ->::type cast(const Y &Val) {
> +inline typename cast_retty<X, const Y>::ret_type cast(const Y &Val) {

The extra enable_ifs were intentionally added in r175819 to avoid the
accidental usage of unsafe casts.  Is there some justification for why
they are no longer necessary?

(Sorry about digging up old commits... I just happened to notice this now.)

-Eli



More information about the llvm-commits mailing list