Construct ArrayRef<const T*> from vector<T>
authorPete Cooper <peter_cooper@apple.com>
Wed, 13 May 2015 22:43:09 +0000 (22:43 +0000)
committerPete Cooper <peter_cooper@apple.com>
Wed, 13 May 2015 22:43:09 +0000 (22:43 +0000)
ArrayRef already has a SFINAE constructor which can construct ArrayRef<const T*> from ArrayRef<T*>.

This adds methods to do the same directly from SmallVector and std::vector.  This avoids an intermediate step through the use of makeArrayRef.

Also update the users of this in LICM and SROA to remove the now unnecessary makeArrayRef call.

Reviewed by David Blaikie.

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

include/llvm/ADT/ArrayRef.h
lib/Transforms/Scalar/LICM.cpp
lib/Transforms/Scalar/SROA.cpp

index f7b055e3a060b6cc0543e23f6b112644fcc52361..1b2bdffc8335d7cbff37c2b1a13d9496b6c37b80 100644 (file)
@@ -96,6 +96,25 @@ namespace llvm {
                  std::is_convertible<U *const *, T const *>::value>::type* = 0)
       : Data(A.data()), Length(A.size()) {}
 
+    /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
+    /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
+    /// whenever we copy-construct an ArrayRef.
+    template<typename U, typename DummyT>
+    /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<U*, DummyT> &Vec,
+                          typename std::enable_if<
+                              std::is_convertible<U *const *,
+                                                  T const *>::value>::type* = 0)
+      : Data(Vec.data()), Length(Vec.size()) {
+    }
+
+    /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
+    /// to ensure that only vectors of pointers can be converted.
+    template<typename U, typename A>
+    ArrayRef(const std::vector<U *, A> &Vec,
+             typename std::enable_if<
+                 std::is_convertible<U *const *, T const *>::value>::type* = 0)
+      : Data(Vec.data()), Length(Vec.size()) {}
+
     /// @}
     /// @name Simple Operations
     /// @{
index 1ac15fa8905bac9355591d45615a280ab65c028b..3d385c04a90a153925f374785422890122c75805 100644 (file)
@@ -929,7 +929,7 @@ bool llvm::promoteLoopAccessesToScalars(AliasSet &AS,
   // We use the SSAUpdater interface to insert phi nodes as required.
   SmallVector<PHINode*, 16> NewPHIs;
   SSAUpdater SSA(&NewPHIs);
-  LoopPromoter Promoter(SomePtr, makeArrayRef(LoopUses), SSA,
+  LoopPromoter Promoter(SomePtr, LoopUses, SSA,
                         PointerMustAliases, ExitBlocks,
                         InsertPts, PIC, *CurAST, *LI, DL, Alignment, AATags);
 
index 2e7858077596d5ca8a1ed98f2c136e9b182bd3ce..056dd11b5ab329e6a03e6e5f4b7f11e0ac7877b1 100644 (file)
@@ -4419,7 +4419,7 @@ bool SROA::promoteAllocas(Function &F) {
       DeadInsts.push_back(I);
       enqueueUsersInWorklist(*I, Worklist, Visited);
     }
-    AllocaPromoter(makeArrayRef(Insts), SSA, *AI, DIB).run(Insts);
+    AllocaPromoter(Insts, SSA, *AI, DIB).run(Insts);
     while (!DeadInsts.empty())
       DeadInsts.pop_back_val()->eraseFromParent();
     AI->eraseFromParent();