OSDN Git Service

2011-11-02 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-vect-stmts.c
index 7e45123..e91efc9 100644 (file)
@@ -1,7 +1,7 @@
 /* Statement Analysis and Transformation for Vectorization
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software
-   Foundation, Inc.
-   Contributed by Dorit Naishlos <dorit@il.ibm.com> 
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
+   Free Software Foundation, Inc.
+   Contributed by Dorit Naishlos <dorit@il.ibm.com>
    and Ira Rosen <irar@il.ibm.com>
 
 This file is part of GCC.
@@ -28,7 +28,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree.h"
 #include "target.h"
 #include "basic-block.h"
-#include "diagnostic.h"
+#include "tree-pretty-print.h"
+#include "gimple-pretty-print.h"
 #include "tree-flow.h"
 #include "tree-dump.h"
 #include "cfgloop.h"
@@ -36,11 +37,87 @@ along with GCC; see the file COPYING3.  If not see
 #include "expr.h"
 #include "recog.h"
 #include "optabs.h"
-#include "toplev.h"
+#include "diagnostic-core.h"
 #include "tree-vectorizer.h"
 #include "langhooks.h"
 
 
+/* Return a variable of type ELEM_TYPE[NELEMS].  */
+
+static tree
+create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
+{
+  return create_tmp_var (build_array_type_nelts (elem_type, nelems),
+                        "vect_array");
+}
+
+/* ARRAY is an array of vectors created by create_vector_array.
+   Return an SSA_NAME for the vector in index N.  The reference
+   is part of the vectorization of STMT and the vector is associated
+   with scalar destination SCALAR_DEST.  */
+
+static tree
+read_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
+                  tree array, unsigned HOST_WIDE_INT n)
+{
+  tree vect_type, vect, vect_name, array_ref;
+  gimple new_stmt;
+
+  gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
+  vect_type = TREE_TYPE (TREE_TYPE (array));
+  vect = vect_create_destination_var (scalar_dest, vect_type);
+  array_ref = build4 (ARRAY_REF, vect_type, array,
+                     build_int_cst (size_type_node, n),
+                     NULL_TREE, NULL_TREE);
+
+  new_stmt = gimple_build_assign (vect, array_ref);
+  vect_name = make_ssa_name (vect, new_stmt);
+  gimple_assign_set_lhs (new_stmt, vect_name);
+  vect_finish_stmt_generation (stmt, new_stmt, gsi);
+  mark_symbols_for_renaming (new_stmt);
+
+  return vect_name;
+}
+
+/* ARRAY is an array of vectors created by create_vector_array.
+   Emit code to store SSA_NAME VECT in index N of the array.
+   The store is part of the vectorization of STMT.  */
+
+static void
+write_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree vect,
+                   tree array, unsigned HOST_WIDE_INT n)
+{
+  tree array_ref;
+  gimple new_stmt;
+
+  array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
+                     build_int_cst (size_type_node, n),
+                     NULL_TREE, NULL_TREE);
+
+  new_stmt = gimple_build_assign (array_ref, vect);
+  vect_finish_stmt_generation (stmt, new_stmt, gsi);
+  mark_symbols_for_renaming (new_stmt);
+}
+
+/* PTR is a pointer to an array of type TYPE.  Return a representation
+   of *PTR.  The memory reference replaces those in FIRST_DR
+   (and its group).  */
+
+static tree
+create_array_ref (tree type, tree ptr, struct data_reference *first_dr)
+{
+  struct ptr_info_def *pi;
+  tree mem_ref, alias_ptr_type;
+
+  alias_ptr_type = reference_alias_ptr_type (DR_REF (first_dr));
+  mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
+  /* Arrays have the same alignment as their type.  */
+  pi = get_ptr_info (ptr);
+  pi->align = TYPE_ALIGN_UNIT (type);
+  pi->misalign = 0;
+  return mem_ref;
+}
+
 /* Utility functions used by vect_mark_stmts_to_be_vectorized.  */
 
 /* Function vect_mark_relevant.
@@ -49,33 +126,73 @@ along with GCC; see the file COPYING3.  If not see
 
 static void
 vect_mark_relevant (VEC(gimple,heap) **worklist, gimple stmt,
-                   enum vect_relevant relevant, bool live_p)
+                   enum vect_relevant relevant, bool live_p,
+                   bool used_in_pattern)
 {
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
   enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
   bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
+  gimple pattern_stmt;
 
   if (vect_print_dump_info (REPORT_DETAILS))
     fprintf (vect_dump, "mark relevant %d, live %d.", relevant, live_p);
 
+  /* If this stmt is an original stmt in a pattern, we might need to mark its
+     related pattern stmt instead of the original stmt.  However, such stmts
+     may have their own uses that are not in any pattern, in such cases the
+     stmt itself should be marked.  */
   if (STMT_VINFO_IN_PATTERN_P (stmt_info))
     {
-      gimple pattern_stmt;
+      bool found = false;
+      if (!used_in_pattern)
+        {
+          imm_use_iterator imm_iter;
+          use_operand_p use_p;
+          gimple use_stmt;
+          tree lhs;
 
-      /* This is the last stmt in a sequence that was detected as a 
-         pattern that can potentially be vectorized.  Don't mark the stmt
-         as relevant/live because it's not going to be vectorized.
-         Instead mark the pattern-stmt that replaces it.  */
+          if (is_gimple_assign (stmt))
+            lhs = gimple_assign_lhs (stmt);
+          else
+            lhs = gimple_call_lhs (stmt);
 
-      pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
+          /* This use is out of pattern use, if LHS has other uses that are
+             pattern uses, we should mark the stmt itself, and not the pattern
+             stmt.  */
+         if (TREE_CODE (lhs) == SSA_NAME)
+           FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
+             {
+               if (is_gimple_debug (USE_STMT (use_p)))
+                 continue;
+               use_stmt = USE_STMT (use_p);
+
+               if (vinfo_for_stmt (use_stmt)
+                   && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
+                 {
+                   found = true;
+                   break;
+                 }
+             }
+        }
 
-      if (vect_print_dump_info (REPORT_DETAILS))
-        fprintf (vect_dump, "last stmt in pattern. don't mark relevant/live.");
-      stmt_info = vinfo_for_stmt (pattern_stmt);
-      gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
-      save_relevant = STMT_VINFO_RELEVANT (stmt_info);
-      save_live_p = STMT_VINFO_LIVE_P (stmt_info);
-      stmt = pattern_stmt;
+      if (!found)
+        {
+          /* This is the last stmt in a sequence that was detected as a
+             pattern that can potentially be vectorized.  Don't mark the stmt
+             as relevant/live because it's not going to be vectorized.
+             Instead mark the pattern-stmt that replaces it.  */
+
+          pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
+
+          if (vect_print_dump_info (REPORT_DETAILS))
+            fprintf (vect_dump, "last stmt in pattern. don't mark"
+                                " relevant/live.");
+          stmt_info = vinfo_for_stmt (pattern_stmt);
+          gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
+          save_relevant = STMT_VINFO_RELEVANT (stmt_info);
+          save_live_p = STMT_VINFO_LIVE_P (stmt_info);
+          stmt = pattern_stmt;
+        }
     }
 
   STMT_VINFO_LIVE_P (stmt_info) |= live_p;
@@ -120,9 +237,9 @@ vect_stmt_relevant_p (gimple stmt, loop_vec_info loop_vinfo,
   *live_p = false;
 
   /* cond stmt other than loop exit cond.  */
-  if (is_ctrl_stmt (stmt) 
-      && STMT_VINFO_TYPE (vinfo_for_stmt (stmt)) 
-         != loop_exit_ctrl_vec_info_type) 
+  if (is_ctrl_stmt (stmt)
+      && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
+         != loop_exit_ctrl_vec_info_type)
     *relevant = vect_used_in_scope;
 
   /* changing memory.  */
@@ -162,9 +279,9 @@ vect_stmt_relevant_p (gimple stmt, loop_vec_info loop_vinfo,
 }
 
 
-/* Function exist_non_indexing_operands_for_use_p 
+/* Function exist_non_indexing_operands_for_use_p
 
-   USE is one of the uses attached to STMT. Check if USE is 
+   USE is one of the uses attached to STMT.  Check if USE is
    used in STMT for anything other than indexing an array.  */
 
 static bool
@@ -172,13 +289,13 @@ exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
 {
   tree operand;
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
-  /* USE corresponds to some operand in STMT. If there is no data
+
+  /* USE corresponds to some operand in STMT.  If there is no data
      reference in STMT, then any operand that corresponds to USE
      is not indexing an array.  */
   if (!STMT_VINFO_DATA_REF (stmt_info))
     return true;
+
   /* STMT has a data_ref. FORNOW this means that its of one of
      the following forms:
      -1- ARRAY_REF = var
@@ -186,19 +303,17 @@ exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
      (This should have been verified in analyze_data_refs).
 
      'var' in the second case corresponds to a def, not a use,
-     so USE cannot correspond to any operands that are not used 
+     so USE cannot correspond to any operands that are not used
      for array indexing.
 
      Therefore, all we need to check is if STMT falls into the
      first case, and whether var corresponds to USE.  */
-  if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
-    return false;
 
   if (!gimple_assign_copy_p (stmt))
     return false;
+  if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
+    return false;
   operand = gimple_assign_rhs1 (stmt);
-
   if (TREE_CODE (operand) != SSA_NAME)
     return false;
 
@@ -209,13 +324,13 @@ exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
 }
 
 
-/* 
+/*
    Function process_use.
 
    Inputs:
    - a USE in STMT in a loop represented by LOOP_VINFO
-   - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt 
-     that defined USE. This is done by calling mark_relevant and passing it
+   - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt
+     that defined USE.  This is done by calling mark_relevant and passing it
      the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
 
    Outputs:
@@ -225,17 +340,17 @@ exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
        STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
    Exceptions:
    - case 1: If USE is used only for address computations (e.g. array indexing),
-   which does not need to be directly vectorized, then the liveness/relevance 
+   which does not need to be directly vectorized, then the liveness/relevance
    of the respective DEF_STMT is left unchanged.
-   - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we 
-   skip DEF_STMT cause it had already been processed.  
+   - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
+   skip DEF_STMT cause it had already been processed.
    - case 3: If DEF_STMT and STMT are in different nests, then  "relevant" will
    be modified accordingly.
 
    Return true if everything is as expected. Return false otherwise.  */
 
 static bool
-process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p, 
+process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
             enum vect_relevant relevant, VEC(gimple,heap) **worklist)
 {
   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
@@ -246,13 +361,13 @@ process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
   gimple def_stmt;
   enum vect_def_type dt;
 
-  /* case 1: we are only interested in uses that need to be vectorized.  Uses 
+  /* case 1: we are only interested in uses that need to be vectorized.  Uses
      that are used for address computation are not considered relevant.  */
   if (!exist_non_indexing_operands_for_use_p (use, stmt))
      return true;
 
   if (!vect_is_simple_use (use, loop_vinfo, NULL, &def_stmt, &def, &dt))
-    { 
+    {
       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
         fprintf (vect_dump, "not vectorized: unsupported use in stmt.");
       return false;
@@ -269,10 +384,10 @@ process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
       return true;
     }
 
-  /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT). 
-     DEF_STMT must have already been processed, because this should be the 
-     only way that STMT, which is a reduction-phi, was put in the worklist, 
-     as there should be no other uses for DEF_STMT in the loop.  So we just 
+  /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
+     DEF_STMT must have already been processed, because this should be the
+     only way that STMT, which is a reduction-phi, was put in the worklist,
+     as there should be no other uses for DEF_STMT in the loop.  So we just
      check that everything is as expected, and we are done.  */
   dstmt_vinfo = vinfo_for_stmt (def_stmt);
   bb = gimple_bb (stmt);
@@ -287,7 +402,7 @@ process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
       if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
        dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
       gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
-      gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo) 
+      gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
                  || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
       return true;
     }
@@ -326,7 +441,7 @@ process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
 
        default:
          gcc_unreachable ();
-       }   
+       }
     }
 
   /* case 3b: inner-loop stmt defining an outer-loop stmt:
@@ -344,7 +459,7 @@ process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
       switch (relevant)
         {
         case vect_unused_in_scope:
-          relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def 
+          relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
             || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
                       vect_used_in_outer_by_reduction : vect_unused_in_scope;
           break;
@@ -362,7 +477,8 @@ process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
         }
     }
 
-  vect_mark_relevant (worklist, def_stmt, relevant, live_p);
+  vect_mark_relevant (worklist, def_stmt, relevant, live_p,
+                      is_pattern_stmt_p (stmt_vinfo));
   return true;
 }
 
@@ -410,7 +526,7 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
     {
       bb = bbs[i];
       for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
-       { 
+       {
          phi = gsi_stmt (si);
          if (vect_print_dump_info (REPORT_DETAILS))
            {
@@ -419,7 +535,7 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
            }
 
          if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
-           vect_mark_relevant (&worklist, phi, relevant, live_p);
+           vect_mark_relevant (&worklist, phi, relevant, live_p, false);
        }
       for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
        {
@@ -428,10 +544,10 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
            {
              fprintf (vect_dump, "init: stmt relevant? ");
              print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
-           } 
+           }
 
          if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
-            vect_mark_relevant (&worklist, stmt, relevant, live_p);
+            vect_mark_relevant (&worklist, stmt, relevant, live_p, false);
        }
     }
 
@@ -448,8 +564,8 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
           print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
        }
 
-      /* Examine the USEs of STMT. For each USE, mark the stmt that defines it 
-        (DEF_STMT) as relevant/irrelevant and live/dead according to the 
+      /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
+        (DEF_STMT) as relevant/irrelevant and live/dead according to the
         liveness and relevance properties of STMT.  */
       stmt_vinfo = vinfo_for_stmt (stmt);
       relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
@@ -465,9 +581,9 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
           live_p = false
           relevant = vect_used_by_reduction
         This is because we distinguish between two kinds of relevant stmts -
-        those that are used by a reduction computation, and those that are 
-        (also) used by a regular computation. This allows us later on to 
-        identify stmts that are used solely by a reduction, and therefore the 
+        those that are used by a reduction computation, and those that are
+        (also) used by a regular computation.  This allows us later on to
+        identify stmts that are used solely by a reduction, and therefore the
         order of the results that they produce does not have to be kept.  */
 
       def_type = STMT_VINFO_DEF_TYPE (stmt_vinfo);
@@ -494,9 +610,9 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
                  return false;
              }
 
-           live_p = false;     
+           live_p = false;
            break;
+
           case vect_nested_cycle:
             if (tmp_relevant != vect_unused_in_scope
                 && tmp_relevant != vect_used_in_outer_by_reduction
@@ -509,9 +625,9 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
                 return false;
               }
 
-            live_p = false; 
-            break; 
-      
+            live_p = false;
+            break;
+
           case vect_double_reduction_def:
             if (tmp_relevant != vect_unused_in_scope
                 && tmp_relevant != vect_used_by_reduction)
@@ -524,21 +640,71 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
               }
 
             live_p = false;
-            break; 
+            break;
 
           default:
             break;
         }
-      FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
-       {
-         tree op = USE_FROM_PTR (use_p);
-         if (!process_use (stmt, op, loop_vinfo, live_p, relevant, &worklist))
-           {
-             VEC_free (gimple, heap, worklist);
-             return false;
-           }
-       }
+
+      if (is_pattern_stmt_p (vinfo_for_stmt (stmt)))
+        {
+          /* Pattern statements are not inserted into the code, so
+             FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
+             have to scan the RHS or function arguments instead.  */
+          if (is_gimple_assign (stmt))
+            {
+             enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
+             tree op = gimple_assign_rhs1 (stmt);
+
+             i = 1;
+             if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
+               {
+                 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
+                                   live_p, relevant, &worklist)
+                     || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
+                                      live_p, relevant, &worklist))
+                   {
+                     VEC_free (gimple, heap, worklist);
+                     return false;
+                   }
+                 i = 2;
+               }
+             for (; i < gimple_num_ops (stmt); i++)
+                {
+                 op = gimple_op (stmt, i);
+                  if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
+                                    &worklist))
+                    {
+                      VEC_free (gimple, heap, worklist);
+                      return false;
+                    }
+                 }
+            }
+          else if (is_gimple_call (stmt))
+            {
+              for (i = 0; i < gimple_call_num_args (stmt); i++)
+                {
+                  tree arg = gimple_call_arg (stmt, i);
+                  if (!process_use (stmt, arg, loop_vinfo, live_p, relevant,
+                                    &worklist))
+                    {
+                      VEC_free (gimple, heap, worklist);
+                      return false;
+                    }
+                }
+            }
+        }
+      else
+        FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
+          {
+            tree op = USE_FROM_PTR (use_p);
+            if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
+                              &worklist))
+              {
+                VEC_free (gimple, heap, worklist);
+                return false;
+              }
+          }
     } /* while worklist */
 
   VEC_free (gimple, heap, worklist);
@@ -546,6 +712,21 @@ vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
 }
 
 
+/* Get cost by calling cost target builtin.  */
+
+static inline
+int vect_get_stmt_cost (enum vect_cost_for_stmt type_of_cost)
+{
+  tree dummy_type = NULL;
+  int dummy = 0;
+
+  return targetm.vectorize.builtin_vectorization_cost (type_of_cost,
+                                                       dummy_type, dummy);
+}
+
+
+/* Get cost for STMT.  */
+
 int
 cost_for_stmt (gimple stmt)
 {
@@ -554,9 +735,9 @@ cost_for_stmt (gimple stmt)
   switch (STMT_VINFO_TYPE (stmt_info))
   {
   case load_vec_info_type:
-    return TARG_SCALAR_LOAD_COST;
+    return vect_get_stmt_cost (scalar_load);
   case store_vec_info_type:
-    return TARG_SCALAR_STORE_COST;
+    return vect_get_stmt_cost (scalar_store);
   case op_vec_info_type:
   case condition_vec_info_type:
   case assignment_vec_info_type:
@@ -566,21 +747,21 @@ cost_for_stmt (gimple stmt)
   case type_demotion_vec_info_type:
   case type_conversion_vec_info_type:
   case call_vec_info_type:
-    return TARG_SCALAR_STMT_COST;
+    return vect_get_stmt_cost (scalar_stmt);
   case undef_vec_info_type:
   default:
     gcc_unreachable ();
   }
 }
 
-/* Function vect_model_simple_cost.  
+/* Function vect_model_simple_cost.
 
-   Models cost for simple operations, i.e. those that only emit ncopies of a 
+   Models cost for simple operations, i.e. those that only emit ncopies of a
    single op.  Right now, this does not account for multiple insns that could
    be generated for the single vector op.  We will handle that shortly.  */
 
 void
-vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies, 
+vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
                        enum vect_def_type *dt, slp_tree slp_node)
 {
   int i;
@@ -590,15 +771,15 @@ vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
   if (PURE_SLP_STMT (stmt_info))
     return;
 
-  inside_cost = ncopies * TARG_VEC_STMT_COST;
+  inside_cost = ncopies * vect_get_stmt_cost (vector_stmt); 
 
   /* FORNOW: Assuming maximum 2 args per stmts.  */
   for (i = 0; i < 2; i++)
     {
       if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
-       outside_cost += TARG_SCALAR_TO_VEC_COST
+       outside_cost += vect_get_stmt_cost (vector_stmt)
     }
-  
+
   if (vect_print_dump_info (REPORT_COST))
     fprintf (vect_dump, "vect_model_simple_cost: inside_cost = %d, "
              "outside_cost = %d .", inside_cost, outside_cost);
@@ -609,8 +790,8 @@ vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
 }
 
 
-/* Function vect_cost_strided_group_size 
+/* Function vect_cost_strided_group_size
+
    For strided load or store, return the group_size only if it is the first
    load or store of a group, else return 1.  This ensures that group size is
    only returned once per group.  */
@@ -618,10 +799,10 @@ vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
 static int
 vect_cost_strided_group_size (stmt_vec_info stmt_info)
 {
-  gimple first_stmt = DR_GROUP_FIRST_DR (stmt_info);
+  gimple first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
 
   if (first_stmt == STMT_VINFO_STMT (stmt_info))
-    return DR_GROUP_SIZE (stmt_info);
+    return GROUP_SIZE (stmt_info);
 
   return 1;
 }
@@ -633,33 +814,54 @@ vect_cost_strided_group_size (stmt_vec_info stmt_info)
    has the overhead of the strided access attributed to it.  */
 
 void
-vect_model_store_cost (stmt_vec_info stmt_info, int ncopies, 
-                      enum vect_def_type dt, slp_tree slp_node)
+vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
+                      bool store_lanes_p, enum vect_def_type dt,
+                      slp_tree slp_node)
 {
   int group_size;
-  int inside_cost = 0, outside_cost = 0;
+  unsigned int inside_cost = 0, outside_cost = 0;
+  struct data_reference *first_dr;
+  gimple first_stmt;
 
   /* The SLP costs were already calculated during SLP tree build.  */
   if (PURE_SLP_STMT (stmt_info))
     return;
 
   if (dt == vect_constant_def || dt == vect_external_def)
-    outside_cost = TARG_SCALAR_TO_VEC_COST;
+    outside_cost = vect_get_stmt_cost (scalar_to_vec); 
 
   /* Strided access?  */
-  if (DR_GROUP_FIRST_DR (stmt_info) && !slp_node) 
-    group_size = vect_cost_strided_group_size (stmt_info);
+  if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
+    {
+      if (slp_node)
+        {
+          first_stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (slp_node), 0);
+          group_size = 1;
+        }
+      else
+        {
+          first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
+          group_size = vect_cost_strided_group_size (stmt_info);
+        }
+
+      first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
+    }
   /* Not a strided access.  */
   else
-    group_size = 1;
+    {
+      group_size = 1;
+      first_dr = STMT_VINFO_DATA_REF (stmt_info);
+    }
 
-  /* Is this an access in a group of stores, which provide strided access?  
-     If so, add in the cost of the permutes.  */
-  if (group_size > 1) 
+  /* We assume that the cost of a single store-lanes instruction is
+     equivalent to the cost of GROUP_SIZE separate stores.  If a strided
+     access is instead being provided by a permute-and-store operation,
+     include the cost of the permutes.  */
+  if (!store_lanes_p && group_size > 1)
     {
       /* Uses a high and low interleave operation for each needed permute.  */
-      inside_cost = ncopies * exact_log2(group_size) * group_size 
-             * TARG_VEC_STMT_COST;
+      inside_cost = ncopies * exact_log2(group_size) * group_size
+        * vect_get_stmt_cost (vector_stmt);
 
       if (vect_print_dump_info (REPORT_COST))
         fprintf (vect_dump, "vect_model_store_cost: strided group_size = %d .",
@@ -668,7 +870,7 @@ vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
     }
 
   /* Costs of the stores.  */
-  inside_cost += ncopies * TARG_VEC_STORE_COST;
+  vect_get_store_cost (first_dr, ncopies, &inside_cost);
 
   if (vect_print_dump_info (REPORT_COST))
     fprintf (vect_dump, "vect_model_store_cost: inside_cost = %d, "
@@ -680,30 +882,72 @@ vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
 }
 
 
+/* Calculate cost of DR's memory access.  */
+void
+vect_get_store_cost (struct data_reference *dr, int ncopies,
+                     unsigned int *inside_cost)
+{
+  int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
+
+  switch (alignment_support_scheme)
+    {
+    case dr_aligned:
+      {
+        *inside_cost += ncopies * vect_get_stmt_cost (vector_store);
+
+        if (vect_print_dump_info (REPORT_COST))
+          fprintf (vect_dump, "vect_model_store_cost: aligned.");
+
+        break;
+      }
+
+    case dr_unaligned_supported:
+      {
+        gimple stmt = DR_STMT (dr);
+        stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
+        tree vectype = STMT_VINFO_VECTYPE (stmt_info);
+
+        /* Here, we assign an additional cost for the unaligned store.  */
+        *inside_cost += ncopies
+          * targetm.vectorize.builtin_vectorization_cost (unaligned_store,
+                                 vectype, DR_MISALIGNMENT (dr));
+
+        if (vect_print_dump_info (REPORT_COST))
+          fprintf (vect_dump, "vect_model_store_cost: unaligned supported by "
+                   "hardware.");
+
+        break;
+      }
+
+    default:
+      gcc_unreachable ();
+    }
+}
+
+
 /* Function vect_model_load_cost
 
    Models cost for loads.  In the case of strided accesses, the last access
    has the overhead of the strided access attributed to it.  Since unaligned
-   accesses are supported for loads, we also account for the costs of the 
+   accesses are supported for loads, we also account for the costs of the
    access scheme chosen.  */
 
 void
-vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, slp_tree slp_node)
-                
+vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, bool load_lanes_p,
+                     slp_tree slp_node)
 {
   int group_size;
-  int alignment_support_cheme;
   gimple first_stmt;
   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
-  int inside_cost = 0, outside_cost = 0;
+  unsigned int inside_cost = 0, outside_cost = 0;
 
   /* The SLP costs were already calculated during SLP tree build.  */
   if (PURE_SLP_STMT (stmt_info))
     return;
 
   /* Strided accesses?  */
-  first_stmt = DR_GROUP_FIRST_DR (stmt_info);
-  if (first_stmt && !slp_node)
+  first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
+  if (STMT_VINFO_STRIDED_ACCESS (stmt_info) && first_stmt && !slp_node)
     {
       group_size = vect_cost_strided_group_size (stmt_info);
       first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
@@ -715,28 +959,50 @@ vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, slp_tree slp_node)
       first_dr = dr;
     }
 
-  alignment_support_cheme = vect_supportable_dr_alignment (first_dr);
-
-  /* Is this an access in a group of loads providing strided access?  
-     If so, add in the cost of the permutes.  */
-  if (group_size > 1) 
+  /* We assume that the cost of a single load-lanes instruction is
+     equivalent to the cost of GROUP_SIZE separate loads.  If a strided
+     access is instead being provided by a load-and-permute operation,
+     include the cost of the permutes.  */
+  if (!load_lanes_p && group_size > 1)
     {
       /* Uses an even and odd extract operations for each needed permute.  */
       inside_cost = ncopies * exact_log2(group_size) * group_size
-       * TARG_VEC_STMT_COST;
+       * vect_get_stmt_cost (vector_stmt);
 
       if (vect_print_dump_info (REPORT_COST))
         fprintf (vect_dump, "vect_model_load_cost: strided group_size = %d .",
                  group_size);
-
     }
 
   /* The loads themselves.  */
-  switch (alignment_support_cheme)
+  vect_get_load_cost (first_dr, ncopies,
+         ((!STMT_VINFO_STRIDED_ACCESS (stmt_info)) || group_size > 1
+          || slp_node),
+         &inside_cost, &outside_cost);
+
+  if (vect_print_dump_info (REPORT_COST))
+    fprintf (vect_dump, "vect_model_load_cost: inside_cost = %d, "
+             "outside_cost = %d .", inside_cost, outside_cost);
+
+  /* Set the costs either in STMT_INFO or SLP_NODE (if exists).  */
+  stmt_vinfo_set_inside_of_loop_cost (stmt_info, slp_node, inside_cost);
+  stmt_vinfo_set_outside_of_loop_cost (stmt_info, slp_node, outside_cost);
+}
+
+
+/* Calculate cost of DR's memory access.  */
+void
+vect_get_load_cost (struct data_reference *dr, int ncopies,
+                    bool add_realign_cost, unsigned int *inside_cost,
+                    unsigned int *outside_cost)
+{
+  int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
+
+  switch (alignment_support_scheme)
     {
     case dr_aligned:
       {
-        inside_cost += ncopies * TARG_VEC_LOAD_COST;
+        *inside_cost += ncopies * vect_get_stmt_cost (vector_load); 
 
         if (vect_print_dump_info (REPORT_COST))
           fprintf (vect_dump, "vect_model_load_cost: aligned.");
@@ -745,9 +1011,14 @@ vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, slp_tree slp_node)
       }
     case dr_unaligned_supported:
       {
-        /* Here, we assign an additional cost for the unaligned load.  */
-        inside_cost += ncopies * TARG_VEC_UNALIGNED_LOAD_COST;
+        gimple stmt = DR_STMT (dr);
+        stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
+        tree vectype = STMT_VINFO_VECTYPE (stmt_info);
 
+        /* Here, we assign an additional cost for the unaligned load.  */
+        *inside_cost += ncopies
+          * targetm.vectorize.builtin_vectorization_cost (unaligned_load,
+                                           vectype, DR_MISALIGNMENT (dr));
         if (vect_print_dump_info (REPORT_COST))
           fprintf (vect_dump, "vect_model_load_cost: unaligned supported by "
                    "hardware.");
@@ -756,13 +1027,14 @@ vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, slp_tree slp_node)
       }
     case dr_explicit_realign:
       {
-        inside_cost += ncopies * (2*TARG_VEC_LOAD_COST + TARG_VEC_STMT_COST);
+        *inside_cost += ncopies * (2 * vect_get_stmt_cost (vector_load)
+           + vect_get_stmt_cost (vector_stmt));
 
         /* FIXME: If the misalignment remains fixed across the iterations of
            the containing loop, the following cost should be added to the
            outside costs.  */
         if (targetm.vectorize.builtin_mask_for_load)
-          inside_cost += TARG_VEC_STMT_COST;
+          *inside_cost += vect_get_stmt_cost (vector_stmt);
 
         break;
       }
@@ -773,44 +1045,36 @@ vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, slp_tree slp_node)
                    "pipelined.");
 
         /* Unaligned software pipeline has a load of an address, an initial
-           load, and possibly a mask operation to "prime" the loop. However,
+           load, and possibly a mask operation to "prime" the loop.  However,
            if this is an access in a group of loads, which provide strided
            access, then the above cost should only be considered for one
-           access in the group. Inside the loop, there is a load op
+           access in the group.  Inside the loop, there is a load op
            and a realignment op.  */
 
-        if ((!DR_GROUP_FIRST_DR (stmt_info)) || group_size > 1 || slp_node)
+        if (add_realign_cost)
           {
-            outside_cost = 2*TARG_VEC_STMT_COST;
+            *outside_cost = 2 * vect_get_stmt_cost (vector_stmt);
             if (targetm.vectorize.builtin_mask_for_load)
-              outside_cost += TARG_VEC_STMT_COST;
+              *outside_cost += vect_get_stmt_cost (vector_stmt);
           }
 
-        inside_cost += ncopies * (TARG_VEC_LOAD_COST + TARG_VEC_STMT_COST);
-
+        *inside_cost += ncopies * (vect_get_stmt_cost (vector_load)
+          + vect_get_stmt_cost (vector_stmt));
         break;
       }
 
     default:
       gcc_unreachable ();
     }
-  
-  if (vect_print_dump_info (REPORT_COST))
-    fprintf (vect_dump, "vect_model_load_cost: inside_cost = %d, "
-             "outside_cost = %d .", inside_cost, outside_cost);
-
-  /* Set the costs either in STMT_INFO or SLP_NODE (if exists).  */
-  stmt_vinfo_set_inside_of_loop_cost (stmt_info, slp_node, inside_cost);
-  stmt_vinfo_set_outside_of_loop_cost (stmt_info, slp_node, outside_cost);
 }
 
 
 /* Function vect_init_vector.
 
    Insert a new stmt (INIT_STMT) that initializes a new vector variable with
-   the vector elements of VECTOR_VAR. Place the initialization at BSI if it
-   is not NULL. Otherwise, place the initialization at the loop preheader.
-   Return the DEF of INIT_STMT. 
+   the vector elements of VECTOR_VAR.  Place the initialization at BSI if it
+   is not NULL.  Otherwise, place the initialization at the loop preheader.
+   Return the DEF of INIT_STMT.
    It will be used in the vectorization of STMT.  */
 
 tree
@@ -824,9 +1088,9 @@ vect_init_vector (gimple stmt, tree vector_var, tree vector_type,
   edge pe;
   tree new_temp;
   basic_block new_bb;
+
   new_var = vect_get_new_vect_var (vector_type, vect_simple_var, "cst_");
-  add_referenced_var (new_var); 
+  add_referenced_var (new_var);
   init_stmt = gimple_build_assign  (new_var, vector_var);
   new_temp = make_ssa_name (new_var, init_stmt);
   gimple_assign_set_lhs (init_stmt, new_temp);
@@ -836,14 +1100,14 @@ vect_init_vector (gimple stmt, tree vector_var, tree vector_type,
   else
     {
       loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
-      
+
       if (loop_vinfo)
         {
           struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
 
           if (nested_in_vect_loop_p (loop, stmt))
             loop = loop->inner;
-          
+
          pe = loop_preheader_edge (loop);
           new_bb = gsi_insert_on_edge_immediate (pe, init_stmt);
           gcc_assert (!new_bb);
@@ -874,7 +1138,7 @@ vect_init_vector (gimple stmt, tree vector_var, tree vector_type,
 
 /* Function vect_get_vec_def_for_operand.
 
-   OP is an operand in STMT. This function returns a (vector) def that will be
+   OP is an operand in STMT.  This function returns a (vector) def that will be
    used in the vectorized stmt for STMT.
 
    In the case that OP is an SSA_NAME which is defined in the loop, then
@@ -891,8 +1155,7 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
   gimple def_stmt;
   stmt_vec_info def_stmt_info = NULL;
   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
-  tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
-  unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
+  unsigned int nunits;
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
   tree vec_inv;
   tree vec_cst;
@@ -909,7 +1172,7 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
       print_generic_expr (vect_dump, op, TDF_SLIM);
     }
 
-  is_simple_use = vect_is_simple_use (op, loop_vinfo, NULL, &def_stmt, &def, 
+  is_simple_use = vect_is_simple_use (op, loop_vinfo, NULL, &def_stmt, &def,
                                       &dt);
   gcc_assert (is_simple_use);
   if (vect_print_dump_info (REPORT_DETAILS))
@@ -933,19 +1196,18 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
       {
        vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
        gcc_assert (vector_type);
+       nunits = TYPE_VECTOR_SUBPARTS (vector_type);
 
-       if (scalar_def) 
+       if (scalar_def)
          *scalar_def = op;
 
         /* Create 'vect_cst_ = {cst,cst,...,cst}'  */
         if (vect_print_dump_info (REPORT_DETAILS))
           fprintf (vect_dump, "Create vector_cst. nunits = %d", nunits);
 
-        for (i = nunits - 1; i >= 0; --i)
-          {
-            t = tree_cons (NULL_TREE, op, t);
-          }
-        vec_cst = build_vector (vector_type, t);
+        vec_cst = build_vector_from_val (vector_type,
+                                        fold_convert (TREE_TYPE (vector_type),
+                                                      op));
         return vect_init_vector (stmt, vec_cst, vector_type, NULL);
       }
 
@@ -956,7 +1218,7 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
        gcc_assert (vector_type);
        nunits = TYPE_VECTOR_SUBPARTS (vector_type);
 
-       if (scalar_def) 
+       if (scalar_def)
          *scalar_def = def;
 
         /* Create 'vec_inv = {inv,inv,..,inv}'  */
@@ -976,12 +1238,19 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
     /* Case 3: operand is defined inside the loop.  */
     case vect_internal_def:
       {
-       if (scalar_def) 
+       if (scalar_def)
          *scalar_def = NULL/* FIXME tuples: def_stmt*/;
 
         /* Get the def from the vectorized stmt.  */
         def_stmt_info = vinfo_for_stmt (def_stmt);
+
         vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
+        /* Get vectorized pattern statement.  */
+        if (!vec_stmt
+            && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
+            && !STMT_VINFO_RELEVANT (def_stmt_info))
+          vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
+                       STMT_VINFO_RELATED_STMT (def_stmt_info)));
         gcc_assert (vec_stmt);
        if (gimple_code (vec_stmt) == GIMPLE_PHI)
          vec_oprnd = PHI_RESULT (vec_stmt);
@@ -1000,7 +1269,7 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
        struct loop *loop;
 
        gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
-       loop = (gimple_bb (def_stmt))->loop_father; 
+       loop = (gimple_bb (def_stmt))->loop_father;
 
         /* Get the def before the loop  */
         op = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
@@ -1015,8 +1284,10 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
         /* Get the def from the vectorized stmt.  */
         def_stmt_info = vinfo_for_stmt (def_stmt);
         vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
-       gcc_assert (vec_stmt && gimple_code (vec_stmt) == GIMPLE_PHI);
-        vec_oprnd = PHI_RESULT (vec_stmt);
+       if (gimple_code (vec_stmt) == GIMPLE_PHI)
+         vec_oprnd = PHI_RESULT (vec_stmt);
+       else
+         vec_oprnd = gimple_get_lhs (vec_stmt);
         return vec_oprnd;
       }
 
@@ -1028,32 +1299,32 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
 
 /* Function vect_get_vec_def_for_stmt_copy
 
-   Return a vector-def for an operand. This function is used when the 
-   vectorized stmt to be created (by the caller to this function) is a "copy" 
-   created in case the vectorized result cannot fit in one vector, and several 
-   copies of the vector-stmt are required. In this case the vector-def is 
+   Return a vector-def for an operand.  This function is used when the
+   vectorized stmt to be created (by the caller to this function) is a "copy"
+   created in case the vectorized result cannot fit in one vector, and several
+   copies of the vector-stmt are required.  In this case the vector-def is
    retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
-   of the stmt that defines VEC_OPRND. 
+   of the stmt that defines VEC_OPRND.
    DT is the type of the vector def VEC_OPRND.
 
    Context:
         In case the vectorization factor (VF) is bigger than the number
    of elements that can fit in a vectype (nunits), we have to generate
-   more than one vector stmt to vectorize the scalar stmt. This situation
-   arises when there are multiple data-types operated upon in the loop; the 
+   more than one vector stmt to vectorize the scalar stmt.  This situation
+   arises when there are multiple data-types operated upon in the loop; the
    smallest data-type determines the VF, and as a result, when vectorizing
    stmts operating on wider types we need to create 'VF/nunits' "copies" of the
    vector stmt (each computing a vector of 'nunits' results, and together
-   computing 'VF' results in each iteration).  This function is called when 
+   computing 'VF' results in each iteration).  This function is called when
    vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
    which VF=16 and nunits=4, so the number of copies required is 4):
 
    scalar stmt:         vectorized into:        STMT_VINFO_RELATED_STMT
+
    S1: x = load         VS1.0:  vx.0 = memref0      VS1.1
                         VS1.1:  vx.1 = memref1      VS1.2
                         VS1.2:  vx.2 = memref2      VS1.3
-                        VS1.3:  vx.3 = memref3 
+                        VS1.3:  vx.3 = memref3
 
    S2: z = x + ...      VSnew.0:  vz0 = vx.0 + ...  VSnew.1
                         VSnew.1:  vz1 = vx.1 + ...  VSnew.2
@@ -1062,19 +1333,19 @@ vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
 
    The vectorization of S1 is explained in vectorizable_load.
    The vectorization of S2:
-        To create the first vector-stmt out of the 4 copies - VSnew.0 - 
-   the function 'vect_get_vec_def_for_operand' is called to 
-   get the relevant vector-def for each operand of S2. For operand x it
+        To create the first vector-stmt out of the 4 copies - VSnew.0 -
+   the function 'vect_get_vec_def_for_operand' is called to
+   get the relevant vector-def for each operand of S2.  For operand x it
    returns  the vector-def 'vx.0'.
 
-        To create the remaining copies of the vector-stmt (VSnew.j), this 
-   function is called to get the relevant vector-def for each operand.  It is 
-   obtained from the respective VS1.j stmt, which is recorded in the 
+        To create the remaining copies of the vector-stmt (VSnew.j), this
+   function is called to get the relevant vector-def for each operand.  It is
+   obtained from the respective VS1.j stmt, which is recorded in the
    STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
 
-        For example, to obtain the vector-def 'vx.1' in order to create the 
-   vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'. 
-   Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the 
+        For example, to obtain the vector-def 'vx.1' in order to create the
+   vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
+   Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
    STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
    and return its def ('vx.1').
    Overall, to create the above sequence this function will be called 3 times:
@@ -1107,11 +1378,11 @@ vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
 
 
 /* Get vectorized definitions for the operands to create a copy of an original
-   stmt. See vect_get_vec_def_for_stmt_copy() for details.  */
+   stmt.  See vect_get_vec_def_for_stmt_copy () for details.  */
 
 static void
-vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt, 
-                                VEC(tree,heap) **vec_oprnds0, 
+vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
+                                VEC(tree,heap) **vec_oprnds0,
                                 VEC(tree,heap) **vec_oprnds1)
 {
   tree vec_oprnd = VEC_pop (tree, *vec_oprnds0);
@@ -1128,7 +1399,8 @@ vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
 }
 
 
-/* Get vectorized definitions for OP0 and OP1, or SLP_NODE if it is not NULL.  */
+/* Get vectorized definitions for OP0 and OP1, or SLP_NODE if it is not
+   NULL.  */
 
 static void
 vect_get_vec_defs (tree op0, tree op1, gimple stmt,
@@ -1136,19 +1408,19 @@ vect_get_vec_defs (tree op0, tree op1, gimple stmt,
                   slp_tree slp_node)
 {
   if (slp_node)
-    vect_get_slp_defs (slp_node, vec_oprnds0, vec_oprnds1);
+    vect_get_slp_defs (op0, op1, slp_node, vec_oprnds0, vec_oprnds1, -1);
   else
     {
       tree vec_oprnd;
 
-      *vec_oprnds0 = VEC_alloc (tree, heap, 1);        
-      vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);      
+      *vec_oprnds0 = VEC_alloc (tree, heap, 1);
+      vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);
       VEC_quick_push (tree, *vec_oprnds0, vec_oprnd);
 
       if (op1)
        {
-         *vec_oprnds1 = VEC_alloc (tree, heap, 1);     
-         vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);      
+         *vec_oprnds1 = VEC_alloc (tree, heap, 1);
+         vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);
          VEC_quick_push (tree, *vec_oprnds1, vec_oprnd);
        }
     }
@@ -1171,7 +1443,7 @@ vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
 
   gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
 
-  set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo, 
+  set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo,
                                                    bb_vinfo));
 
   if (vect_print_dump_info (REPORT_DETAILS))
@@ -1180,7 +1452,7 @@ vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
       print_gimple_stmt (vect_dump, vec_stmt, 0, TDF_SLIM);
     }
 
-  gimple_set_location (vec_stmt, gimple_location (gsi_stmt (*gsi)));
+  gimple_set_location (vec_stmt, gimple_location (stmt));
 }
 
 /* Checks if CALL can be vectorized in type VECTYPE.  Returns
@@ -1191,7 +1463,6 @@ tree
 vectorizable_function (gimple call, tree vectype_out, tree vectype_in)
 {
   tree fndecl = gimple_call_fndecl (call);
-  enum built_in_function code;
 
   /* We only handle functions that do not read or clobber memory -- i.e.
      const or novops ones.  */
@@ -1203,15 +1474,14 @@ vectorizable_function (gimple call, tree vectype_out, tree vectype_in)
       || !DECL_BUILT_IN (fndecl))
     return NULL_TREE;
 
-  code = DECL_FUNCTION_CODE (fndecl);
-  return targetm.vectorize.builtin_vectorized_function (code, vectype_out,
+  return targetm.vectorize.builtin_vectorized_function (fndecl, vectype_out,
                                                        vectype_in);
 }
 
 /* Function vectorizable_call.
 
-   Check if STMT performs a function call that can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
+   Check if STMT performs a function call that can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
 
@@ -1227,18 +1497,20 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
   int nunits_in;
   int nunits_out;
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
-  tree fndecl, new_temp, def, rhs_type, lhs_type;
+  tree fndecl, new_temp, def, rhs_type;
   gimple def_stmt;
-  enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
+  enum vect_def_type dt[3]
+    = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
   gimple new_stmt = NULL;
   int ncopies, j;
   VEC(tree, heap) *vargs = NULL;
   enum { NARROW, NONE, WIDEN } modifier;
   size_t i, nargs;
+  tree lhs;
 
   /* FORNOW: unsupported in basic block SLP.  */
   gcc_assert (loop_vinfo);
-    
+
   if (!STMT_VINFO_RELEVANT_P (stmt_info))
     return false;
 
@@ -1256,50 +1528,77 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
   if (TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
     return false;
 
+  if (stmt_can_throw_internal (stmt))
+    return false;
+
+  vectype_out = STMT_VINFO_VECTYPE (stmt_info);
+
   /* Process function arguments.  */
   rhs_type = NULL_TREE;
+  vectype_in = NULL_TREE;
   nargs = gimple_call_num_args (stmt);
 
-  /* Bail out if the function has more than two arguments, we
-     do not have interesting builtin functions to vectorize with
-     more than two arguments.  No arguments is also not good.  */
-  if (nargs == 0 || nargs > 2)
+  /* Bail out if the function has more than three arguments, we do not have
+     interesting builtin functions to vectorize with more than two arguments
+     except for fma.  No arguments is also not good.  */
+  if (nargs == 0 || nargs > 3)
     return false;
 
   for (i = 0; i < nargs; i++)
     {
+      tree opvectype;
+
       op = gimple_call_arg (stmt, i);
 
       /* We can only handle calls with arguments of the same type.  */
       if (rhs_type
-         && rhs_type != TREE_TYPE (op))
+         && !types_compatible_p (rhs_type, TREE_TYPE (op)))
        {
          if (vect_print_dump_info (REPORT_DETAILS))
            fprintf (vect_dump, "argument types differ.");
          return false;
        }
-      rhs_type = TREE_TYPE (op);
+      if (!rhs_type)
+       rhs_type = TREE_TYPE (op);
 
-      if (!vect_is_simple_use (op, loop_vinfo, NULL, &def_stmt, &def, &dt[i]))
+      if (!vect_is_simple_use_1 (op, loop_vinfo, NULL,
+                                &def_stmt, &def, &dt[i], &opvectype))
        {
          if (vect_print_dump_info (REPORT_DETAILS))
            fprintf (vect_dump, "use not simple.");
          return false;
        }
-    }
 
-  vectype_in = get_vectype_for_scalar_type (rhs_type);
+      if (!vectype_in)
+       vectype_in = opvectype;
+      else if (opvectype
+              && opvectype != vectype_in)
+       {
+         if (vect_print_dump_info (REPORT_DETAILS))
+           fprintf (vect_dump, "argument vector types differ.");
+         return false;
+       }
+    }
+  /* If all arguments are external or constant defs use a vector type with
+     the same size as the output vector type.  */
   if (!vectype_in)
-    return false;
-  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
+    vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
+  if (vec_stmt)
+    gcc_assert (vectype_in);
+  if (!vectype_in)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "no vectype for scalar type ");
+          print_generic_expr (vect_dump, rhs_type, TDF_SLIM);
+        }
 
-  lhs_type = TREE_TYPE (gimple_call_lhs (stmt));
-  vectype_out = get_vectype_for_scalar_type (lhs_type);
-  if (!vectype_out)
-    return false;
-  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
+      return false;
+    }
 
   /* FORNOW */
+  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
+  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
   if (nunits_in == nunits_out / 2)
     modifier = NARROW;
   else if (nunits_out == nunits_in)
@@ -1345,7 +1644,7 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
   /** Transform.  **/
 
   if (vect_print_dump_info (REPORT_DETAILS))
-    fprintf (vect_dump, "transform operation.");
+    fprintf (vect_dump, "transform call.");
 
   /* Handle def.  */
   scalar_dest = gimple_call_lhs (stmt);
@@ -1384,6 +1683,7 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
          gimple_call_set_lhs (new_stmt, new_temp);
 
          vect_finish_stmt_generation (stmt, new_stmt, gsi);
+         mark_symbols_for_renaming (new_stmt);
 
          if (j == 0)
            STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
@@ -1416,7 +1716,7 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
                }
              else
                {
-                 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i);
+                 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
                  vec_oprnd0
                    = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
                  vec_oprnd1
@@ -1432,6 +1732,7 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
          gimple_call_set_lhs (new_stmt, new_temp);
 
          vect_finish_stmt_generation (stmt, new_stmt, gsi);
+         mark_symbols_for_renaming (new_stmt);
 
          if (j == 0)
            STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
@@ -1462,8 +1763,11 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
      rhs of the statement with something harmless.  */
 
   type = TREE_TYPE (scalar_dest);
-  new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
-                                 fold_convert (type, integer_zero_node));
+  if (is_pattern_stmt_p (stmt_info))
+    lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
+  else
+    lhs = gimple_call_lhs (stmt);
+  new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
   set_vinfo_for_stmt (new_stmt, stmt_info);
   set_vinfo_for_stmt (stmt, NULL);
   STMT_VINFO_STMT (stmt_info) = new_stmt;
@@ -1477,8 +1781,8 @@ vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
 /* Function vect_gen_widened_results_half
 
    Create a vector stmt whose code, type, number of arguments, and result
-   variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are 
-   VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
+   variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
+   VEC_OPRND0 and VEC_OPRND1.  The new vector stmt is to be inserted at BSI.
    In the case that CODE is a CALL_EXPR, this means that a call to DECL
    needs to be created (DECL is a function-decl of a target-builtin).
    STMT is the original scalar stmt that we are vectorizing.  */
@@ -1489,40 +1793,39 @@ vect_gen_widened_results_half (enum tree_code code,
                                tree vec_oprnd0, tree vec_oprnd1, int op_type,
                               tree vec_dest, gimple_stmt_iterator *gsi,
                               gimple stmt)
-{ 
+{
   gimple new_stmt;
-  tree new_temp; 
-  /* Generate half of the widened result:  */ 
-  if (code == CALL_EXPR) 
-    {  
-      /* Target specific support  */ 
+  tree new_temp;
+
+  /* Generate half of the widened result:  */
+  if (code == CALL_EXPR)
+    {
+      /* Target specific support  */
       if (op_type == binary_op)
        new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
       else
        new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
       new_temp = make_ssa_name (vec_dest, new_stmt);
       gimple_call_set_lhs (new_stmt, new_temp);
-    } 
-  else 
+    }
+  else
     {
-      /* Generic support */ 
-      gcc_assert (op_type == TREE_CODE_LENGTH (code)); 
+      /* Generic support */
+      gcc_assert (op_type == TREE_CODE_LENGTH (code));
       if (op_type != binary_op)
        vec_oprnd1 = NULL;
       new_stmt = gimple_build_assign_with_ops (code, vec_dest, vec_oprnd0,
                                               vec_oprnd1);
       new_temp = make_ssa_name (vec_dest, new_stmt);
       gimple_assign_set_lhs (new_stmt, new_temp);
-    } 
+    }
   vect_finish_stmt_generation (stmt, new_stmt, gsi);
 
   return new_stmt;
 }
 
-
-/* Check if STMT performs a conversion operation, that can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
+/* Check if STMT performs a conversion operation, that can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
 
@@ -1548,14 +1851,11 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
   int nunits_out;
   tree vectype_out, vectype_in;
   int ncopies, j;
-  tree expr;
-  tree rhs_type, lhs_type;
-  tree builtin_decl;
+  tree rhs_type;
   enum { NARROW, NONE, WIDEN } modifier;
   int i;
   VEC(tree,heap) *vec_oprnds0 = NULL;
   tree vop0;
-  tree integral_type;
   VEC(tree,heap) *dummy = NULL;
   int dummy_int;
 
@@ -1563,7 +1863,7 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
 
   /* FORNOW: unsupported in basic block SLP.  */
   gcc_assert (loop_vinfo);
-  
+
   if (!STMT_VINFO_RELEVANT_P (stmt_info))
     return false;
 
@@ -1581,21 +1881,39 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
     return false;
 
   /* Check types of lhs and rhs.  */
+  scalar_dest = gimple_assign_lhs (stmt);
+  vectype_out = STMT_VINFO_VECTYPE (stmt_info);
+
   op0 = gimple_assign_rhs1 (stmt);
   rhs_type = TREE_TYPE (op0);
-  vectype_in = get_vectype_for_scalar_type (rhs_type);
+  /* Check the operands of the operation.  */
+  if (!vect_is_simple_use_1 (op0, loop_vinfo, NULL,
+                            &def_stmt, &def, &dt[0], &vectype_in))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+       fprintf (vect_dump, "use not simple.");
+      return false;
+    }
+  /* If op0 is an external or constant defs use a vector type of
+     the same size as the output vector type.  */
   if (!vectype_in)
-    return false;
-  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
+    vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
+  if (vec_stmt)
+    gcc_assert (vectype_in);
+  if (!vectype_in)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "no vectype for scalar type ");
+          print_generic_expr (vect_dump, rhs_type, TDF_SLIM);
+        }
 
-  scalar_dest = gimple_assign_lhs (stmt);
-  lhs_type = TREE_TYPE (scalar_dest);
-  vectype_out = get_vectype_for_scalar_type (lhs_type);
-  if (!vectype_out)
-    return false;
-  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
+      return false;
+    }
 
   /* FORNOW */
+  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
+  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
   if (nunits_in == nunits_out / 2)
     modifier = NARROW;
   else if (nunits_out == nunits_in)
@@ -1605,48 +1923,32 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
   else
     return false;
 
-  if (modifier == NONE)
-    gcc_assert (STMT_VINFO_VECTYPE (stmt_info) == vectype_out);
-
-  /* Bail out if the types are both integral or non-integral.  */
-  if ((INTEGRAL_TYPE_P (rhs_type) && INTEGRAL_TYPE_P (lhs_type))
-      || (!INTEGRAL_TYPE_P (rhs_type) && !INTEGRAL_TYPE_P (lhs_type)))
-    return false;
-
-  integral_type = INTEGRAL_TYPE_P (rhs_type) ? vectype_in : vectype_out;
-
   if (modifier == NARROW)
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
   else
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
 
-  /* FORNOW: SLP with multiple types is not supported. The SLP analysis verifies
-     this, so we can safely override NCOPIES with 1 here.  */
-  if (slp_node)
+  /* Multiple types in SLP are handled by creating the appropriate number of
+     vectorized stmts for each SLP node.  Hence, NCOPIES is always 1 in
+     case of SLP.  */
+  if (slp_node || PURE_SLP_STMT (stmt_info))
     ncopies = 1;
-  
+
   /* Sanity check: make sure that at least one copy of the vectorized stmt
      needs to be generated.  */
   gcc_assert (ncopies >= 1);
 
-  /* Check the operands of the operation.  */
-  if (!vect_is_simple_use (op0, loop_vinfo, NULL, &def_stmt, &def, &dt[0]))
-    {
-      if (vect_print_dump_info (REPORT_DETAILS))
-       fprintf (vect_dump, "use not simple.");
-      return false;
-    }
-
   /* Supportable by target?  */
   if ((modifier == NONE
-       && !targetm.vectorize.builtin_conversion (code, integral_type))
+       && !supportable_convert_operation (code, vectype_out, vectype_in, &decl1, &code1))
       || (modifier == WIDEN
-         && !supportable_widening_operation (code, stmt, vectype_in,
+         && !supportable_widening_operation (code, stmt,
+                                             vectype_out, vectype_in,
                                              &decl1, &decl2,
                                              &code1, &code2,
                                               &dummy_int, &dummy))
       || (modifier == NARROW
-         && !supportable_narrowing_operation (code, stmt, vectype_in,
+         && !supportable_narrowing_operation (code, vectype_out, vectype_in,
                                               &code1, &dummy_int, &dummy)))
     {
       if (vect_print_dump_info (REPORT_DETAILS))
@@ -1656,10 +1958,9 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
 
   if (modifier != NONE)
     {
-      STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
       /* FORNOW: SLP not supported.  */
       if (STMT_SLP_TYPE (stmt_info))
-       return false;      
+       return false;
     }
 
   if (!vec_stmt)               /* transformation not required.  */
@@ -1685,25 +1986,35 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
       for (j = 0; j < ncopies; j++)
        {
          if (j == 0)
-           vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node); 
+           vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node);
          else
            vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
 
-         builtin_decl =
-           targetm.vectorize.builtin_conversion (code, integral_type);
-         for (i = 0; VEC_iterate (tree, vec_oprnds0, i, vop0); i++)
-           { 
-             /* Arguments are ready. create the new vector stmt.  */
-             new_stmt = gimple_build_call (builtin_decl, 1, vop0);
-             new_temp = make_ssa_name (vec_dest, new_stmt);
-             gimple_call_set_lhs (new_stmt, new_temp);
-             vect_finish_stmt_generation (stmt, new_stmt, gsi);
-             if (slp_node)
-               VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
-           }
-
-         if (j == 0)
-           STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
+         FOR_EACH_VEC_ELT (tree, vec_oprnds0, i, vop0)
+          {
+            /* Arguments are ready, create the new vector stmt.  */
+            if (code1 == CALL_EXPR)
+              {
+                new_stmt = gimple_build_call (decl1, 1, vop0);
+                new_temp = make_ssa_name (vec_dest, new_stmt);
+                gimple_call_set_lhs (new_stmt, new_temp);
+              }
+            else
+              {
+                gcc_assert (TREE_CODE_LENGTH (code) == unary_op);
+                new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0,
+                                                        NULL);
+                new_temp = make_ssa_name (vec_dest, new_stmt);
+                gimple_assign_set_lhs (new_stmt, new_temp);
+              }
+
+            vect_finish_stmt_generation (stmt, new_stmt, gsi);
+            if (slp_node)
+              VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
+          }
+
+         if (j == 0)
+           STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
          else
            STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
          prev_stmt_info = vinfo_for_stmt (new_stmt);
@@ -1722,11 +2033,9 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
          else
            vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
 
-         STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
-
          /* Generate first half of the widened result:  */
          new_stmt
-           = vect_gen_widened_results_half (code1, decl1, 
+           = vect_gen_widened_results_half (code1, decl1,
                                             vec_oprnd0, vec_oprnd1,
                                             unary_op, vec_dest, gsi, stmt);
          if (j == 0)
@@ -1764,8 +2073,7 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
              vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
            }
 
-         /* Arguments are ready. Create the new vector stmt.  */
-         expr = build2 (code1, vectype_out, vec_oprnd0, vec_oprnd1);
+         /* Arguments are ready.  Create the new vector stmt.  */
          new_stmt = gimple_build_assign_with_ops (code1, vec_dest, vec_oprnd0,
                                                   vec_oprnd1);
          new_temp = make_ssa_name (vec_dest, new_stmt);
@@ -1784,14 +2092,16 @@ vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
     }
 
   if (vec_oprnds0)
-    VEC_free (tree, heap, vec_oprnds0); 
+    VEC_free (tree, heap, vec_oprnds0);
 
   return true;
 }
+
+
 /* Function vectorizable_assignment.
 
-   Check if STMT performs an assignment (copy) that can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
+   Check if STMT performs an assignment (copy) that can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
 
@@ -1809,24 +2119,26 @@ vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
   tree def;
   gimple def_stmt;
   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
-  int nunits = TYPE_VECTOR_SUBPARTS (vectype);
+  unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
   int ncopies;
-  int i;
+  int i, j;
   VEC(tree,heap) *vec_oprnds = NULL;
   tree vop;
   bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
+  gimple new_stmt = NULL;
+  stmt_vec_info prev_stmt_info = NULL;
+  enum tree_code code;
+  tree vectype_in;
 
   /* Multiple types in SLP are handled by creating the appropriate number of
      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
      case of SLP.  */
-  if (slp_node)
+  if (slp_node || PURE_SLP_STMT (stmt_info))
     ncopies = 1;
   else
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
 
   gcc_assert (ncopies >= 1);
-  if (ncopies > 1)
-    return false; /* FORNOW */
 
   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
     return false;
@@ -1842,19 +2154,54 @@ vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
   if (TREE_CODE (scalar_dest) != SSA_NAME)
     return false;
 
+  code = gimple_assign_rhs_code (stmt);
   if (gimple_assign_single_p (stmt)
-      || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
+      || code == PAREN_EXPR
+      || CONVERT_EXPR_CODE_P (code))
     op = gimple_assign_rhs1 (stmt);
   else
     return false;
 
-  if (!vect_is_simple_use (op, loop_vinfo, bb_vinfo, &def_stmt, &def, &dt[0]))
+  if (code == VIEW_CONVERT_EXPR)
+    op = TREE_OPERAND (op, 0);
+
+  if (!vect_is_simple_use_1 (op, loop_vinfo, bb_vinfo,
+                            &def_stmt, &def, &dt[0], &vectype_in))
     {
       if (vect_print_dump_info (REPORT_DETAILS))
         fprintf (vect_dump, "use not simple.");
       return false;
     }
 
+  /* We can handle NOP_EXPR conversions that do not change the number
+     of elements or the vector size.  */
+  if ((CONVERT_EXPR_CODE_P (code)
+       || code == VIEW_CONVERT_EXPR)
+      && (!vectype_in
+         || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
+         || (GET_MODE_SIZE (TYPE_MODE (vectype))
+             != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
+    return false;
+
+  /* We do not handle bit-precision changes.  */
+  if ((CONVERT_EXPR_CODE_P (code)
+       || code == VIEW_CONVERT_EXPR)
+      && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
+      && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
+          != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
+         || ((TYPE_PRECISION (TREE_TYPE (op))
+              != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
+      /* But a conversion that does not change the bit-pattern is ok.  */
+      && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
+           > TYPE_PRECISION (TREE_TYPE (op)))
+          && TYPE_UNSIGNED (TREE_TYPE (op))))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "type conversion to/from bit-precision "
+                "unsupported.");
+      return false;
+    }
+
   if (!vec_stmt) /* transformation not required.  */
     {
       STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
@@ -1872,47 +2219,101 @@ vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
   vec_dest = vect_create_destination_var (scalar_dest, vectype);
 
   /* Handle use.  */
-  vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node);
-
-  /* Arguments are ready. create the new vector stmt.  */
-  for (i = 0; VEC_iterate (tree, vec_oprnds, i, vop); i++)
+  for (j = 0; j < ncopies; j++)
     {
-      *vec_stmt = gimple_build_assign (vec_dest, vop);
-      new_temp = make_ssa_name (vec_dest, *vec_stmt);
-      gimple_assign_set_lhs (*vec_stmt, new_temp);
-      vect_finish_stmt_generation (stmt, *vec_stmt, gsi);
-      STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt;
+      /* Handle uses.  */
+      if (j == 0)
+        vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node);
+      else
+        vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
+
+      /* Arguments are ready. create the new vector stmt.  */
+      FOR_EACH_VEC_ELT (tree, vec_oprnds, i, vop)
+       {
+        if (CONVERT_EXPR_CODE_P (code)
+            || code == VIEW_CONVERT_EXPR)
+          vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
+         new_stmt = gimple_build_assign (vec_dest, vop);
+         new_temp = make_ssa_name (vec_dest, new_stmt);
+         gimple_assign_set_lhs (new_stmt, new_temp);
+         vect_finish_stmt_generation (stmt, new_stmt, gsi);
+         if (slp_node)
+           VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
+       }
 
       if (slp_node)
-       VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), *vec_stmt);
-   }
-  
-  VEC_free (tree, heap, vec_oprnds);       
+        continue;
+
+      if (j == 0)
+        STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
+      else
+        STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
+
+      prev_stmt_info = vinfo_for_stmt (new_stmt);
+    }
+
+  VEC_free (tree, heap, vec_oprnds);
   return true;
 }
 
-/* Function vectorizable_operation.
 
-   Check if STMT performs a binary or unary operation that can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
+/* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
+   either as shift by a scalar or by a vector.  */
+
+bool
+vect_supportable_shift (enum tree_code code, tree scalar_type)
+{
+
+  enum machine_mode vec_mode;
+  optab optab;
+  int icode;
+  tree vectype;
+
+  vectype = get_vectype_for_scalar_type (scalar_type);
+  if (!vectype)
+    return false;
+
+  optab = optab_for_tree_code (code, vectype, optab_scalar);
+  if (!optab
+      || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
+    {
+      optab = optab_for_tree_code (code, vectype, optab_vector);
+      if (!optab
+          || (optab_handler (optab, TYPE_MODE (vectype))
+                      == CODE_FOR_nothing))
+        return false;
+    }
+
+  vec_mode = TYPE_MODE (vectype);
+  icode = (int) optab_handler (optab, vec_mode);
+  if (icode == CODE_FOR_nothing)
+    return false;
+
+  return true;
+}
+
+
+/* Function vectorizable_shift.
+
+   Check if STMT performs a shift operation that can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
 
 static bool
-vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
-                       gimple *vec_stmt, slp_tree slp_node)
+vectorizable_shift (gimple stmt, gimple_stmt_iterator *gsi,
+                    gimple *vec_stmt, slp_tree slp_node)
 {
   tree vec_dest;
   tree scalar_dest;
   tree op0, op1 = NULL;
   tree vec_oprnd1 = NULL_TREE;
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
-  tree vectype = STMT_VINFO_VECTYPE (stmt_info);
+  tree vectype;
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
   enum tree_code code;
   enum machine_mode vec_mode;
   tree new_temp;
-  int op_type;
   optab optab;
   int icode;
   enum machine_mode optab_op2_mode;
@@ -1921,35 +2322,378 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
   gimple new_stmt = NULL;
   stmt_vec_info prev_stmt_info;
-  int nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
+  int nunits_in;
   int nunits_out;
   tree vectype_out;
+  tree op1_vectype;
   int ncopies;
   int j, i;
-  VEC(tree,heap) *vec_oprnds0 = NULL, *vec_oprnds1 = NULL;
+  VEC (tree, heap) *vec_oprnds0 = NULL, *vec_oprnds1 = NULL;
   tree vop0, vop1;
   unsigned int k;
-  bool shift_p = false;
-  bool scalar_shift_arg = false;
+  bool scalar_shift_arg = true;
   bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
   int vf;
 
+  if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
+    return false;
+
+  if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
+    return false;
+
+  /* Is STMT a vectorizable binary/unary operation?   */
+  if (!is_gimple_assign (stmt))
+    return false;
+
+  if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
+    return false;
+
+  code = gimple_assign_rhs_code (stmt);
+
+  if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
+      || code == RROTATE_EXPR))
+    return false;
+
+  scalar_dest = gimple_assign_lhs (stmt);
+  vectype_out = STMT_VINFO_VECTYPE (stmt_info);
+  if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
+      != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "bit-precision shifts not supported.");
+      return false;
+    }
+
+  op0 = gimple_assign_rhs1 (stmt);
+  if (!vect_is_simple_use_1 (op0, loop_vinfo, bb_vinfo,
+                             &def_stmt, &def, &dt[0], &vectype))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "use not simple.");
+      return false;
+    }
+  /* If op0 is an external or constant def use a vector type with
+     the same size as the output vector type.  */
+  if (!vectype)
+    vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
+  if (vec_stmt)
+    gcc_assert (vectype);
+  if (!vectype)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "no vectype for scalar type ");
+          print_generic_expr (vect_dump, TREE_TYPE (op0), TDF_SLIM);
+        }
+
+      return false;
+    }
+
+  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
+  nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
+  if (nunits_out != nunits_in)
+    return false;
+
+  op1 = gimple_assign_rhs2 (stmt);
+  if (!vect_is_simple_use_1 (op1, loop_vinfo, bb_vinfo, &def_stmt, &def,
+                            &dt[1], &op1_vectype))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "use not simple.");
+      return false;
+    }
+
   if (loop_vinfo)
     vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
   else
-    /* FORNOW: multiple types are not supported in basic block SLP.  */
-    vf = nunits_in;
+    vf = 1;
 
   /* Multiple types in SLP are handled by creating the appropriate number of
-     vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
+     vectorized stmts for each SLP node.  Hence, NCOPIES is always 1 in
      case of SLP.  */
-  if (slp_node)
+  if (slp_node || PURE_SLP_STMT (stmt_info))
     ncopies = 1;
   else
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
 
   gcc_assert (ncopies >= 1);
 
+  /* Determine whether the shift amount is a vector, or scalar.  If the
+     shift/rotate amount is a vector, use the vector/vector shift optabs.  */
+
+  if (dt[1] == vect_internal_def && !slp_node)
+    scalar_shift_arg = false;
+  else if (dt[1] == vect_constant_def
+          || dt[1] == vect_external_def
+          || dt[1] == vect_internal_def)
+    {
+      /* In SLP, need to check whether the shift count is the same,
+        in loops if it is a constant or invariant, it is always
+        a scalar shift.  */
+      if (slp_node)
+       {
+         VEC (gimple, heap) *stmts = SLP_TREE_SCALAR_STMTS (slp_node);
+         gimple slpstmt;
+
+         FOR_EACH_VEC_ELT (gimple, stmts, k, slpstmt)
+           if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
+             scalar_shift_arg = false;
+       }
+    }
+  else
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+       fprintf (vect_dump, "operand mode requires invariant argument.");
+      return false;
+    }
+
+  /* Vector shifted by vector.  */
+  if (!scalar_shift_arg)
+    {
+      optab = optab_for_tree_code (code, vectype, optab_vector);
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "vector/vector shift/rotate found.");
+      if (!op1_vectype)
+       op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
+      if (op1_vectype == NULL_TREE
+         || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
+       {
+         if (vect_print_dump_info (REPORT_DETAILS))
+           fprintf (vect_dump, "unusable type for last operand in"
+                               " vector/vector shift/rotate.");
+         return false;
+       }
+    }
+  /* See if the machine has a vector shifted by scalar insn and if not
+     then see if it has a vector shifted by vector insn.  */
+  else
+    {
+      optab = optab_for_tree_code (code, vectype, optab_scalar);
+      if (optab
+          && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
+        {
+          if (vect_print_dump_info (REPORT_DETAILS))
+            fprintf (vect_dump, "vector/scalar shift/rotate found.");
+        }
+      else
+        {
+          optab = optab_for_tree_code (code, vectype, optab_vector);
+          if (optab
+               && (optab_handler (optab, TYPE_MODE (vectype))
+                      != CODE_FOR_nothing))
+            {
+             scalar_shift_arg = false;
+
+              if (vect_print_dump_info (REPORT_DETAILS))
+                fprintf (vect_dump, "vector/vector shift/rotate found.");
+
+              /* Unlike the other binary operators, shifts/rotates have
+                 the rhs being int, instead of the same type as the lhs,
+                 so make sure the scalar is the right type if we are
+                dealing with vectors of long long/long/short/char.  */
+              if (dt[1] == vect_constant_def)
+                op1 = fold_convert (TREE_TYPE (vectype), op1);
+             else if (!useless_type_conversion_p (TREE_TYPE (vectype),
+                                                  TREE_TYPE (op1)))
+               {
+                 if (slp_node
+                     && TYPE_MODE (TREE_TYPE (vectype))
+                        != TYPE_MODE (TREE_TYPE (op1)))
+                   {
+                     if (vect_print_dump_info (REPORT_DETAILS))
+                     fprintf (vect_dump, "unusable type for last operand in"
+                                         " vector/vector shift/rotate.");
+                       return false;
+                   }
+                 if (vec_stmt && !slp_node)
+                   {
+                     op1 = fold_convert (TREE_TYPE (vectype), op1);
+                     op1 = vect_init_vector (stmt, op1,
+                                             TREE_TYPE (vectype), NULL);
+                   }
+               }
+            }
+        }
+    }
+
+  /* Supportable by target?  */
+  if (!optab)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "no optab.");
+      return false;
+    }
+  vec_mode = TYPE_MODE (vectype);
+  icode = (int) optab_handler (optab, vec_mode);
+  if (icode == CODE_FOR_nothing)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "op not supported by target.");
+      /* Check only during analysis.  */
+      if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
+          || (vf < vect_min_worthwhile_factor (code)
+              && !vec_stmt))
+        return false;
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "proceeding using word mode.");
+    }
+
+  /* Worthwhile without SIMD support?  Check only during analysis.  */
+  if (!VECTOR_MODE_P (TYPE_MODE (vectype))
+      && vf < vect_min_worthwhile_factor (code)
+      && !vec_stmt)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "not worthwhile without SIMD support.");
+      return false;
+    }
+
+  if (!vec_stmt) /* transformation not required.  */
+    {
+      STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "=== vectorizable_shift ===");
+      vect_model_simple_cost (stmt_info, ncopies, dt, NULL);
+      return true;
+    }
+
+  /** Transform.  **/
+
+  if (vect_print_dump_info (REPORT_DETAILS))
+    fprintf (vect_dump, "transform binary/unary operation.");
+
+  /* Handle def.  */
+  vec_dest = vect_create_destination_var (scalar_dest, vectype);
+
+  /* Allocate VECs for vector operands.  In case of SLP, vector operands are
+     created in the previous stages of the recursion, so no allocation is
+     needed, except for the case of shift with scalar shift argument.  In that
+     case we store the scalar operand in VEC_OPRNDS1 for every vector stmt to
+     be created to vectorize the SLP group, i.e., SLP_NODE->VEC_STMTS_SIZE.
+     In case of loop-based vectorization we allocate VECs of size 1.  We
+     allocate VEC_OPRNDS1 only in case of binary operation.  */
+  if (!slp_node)
+    {
+      vec_oprnds0 = VEC_alloc (tree, heap, 1);
+      vec_oprnds1 = VEC_alloc (tree, heap, 1);
+    }
+  else if (scalar_shift_arg)
+    vec_oprnds1 = VEC_alloc (tree, heap, slp_node->vec_stmts_size);
+
+  prev_stmt_info = NULL;
+  for (j = 0; j < ncopies; j++)
+    {
+      /* Handle uses.  */
+      if (j == 0)
+        {
+          if (scalar_shift_arg)
+            {
+              /* Vector shl and shr insn patterns can be defined with scalar
+                 operand 2 (shift operand).  In this case, use constant or loop
+                 invariant op1 directly, without extending it to vector mode
+                 first.  */
+              optab_op2_mode = insn_data[icode].operand[2].mode;
+              if (!VECTOR_MODE_P (optab_op2_mode))
+                {
+                  if (vect_print_dump_info (REPORT_DETAILS))
+                    fprintf (vect_dump, "operand 1 using scalar mode.");
+                  vec_oprnd1 = op1;
+                  VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
+                  if (slp_node)
+                    {
+                      /* Store vec_oprnd1 for every vector stmt to be created
+                         for SLP_NODE.  We check during the analysis that all
+                         the shift arguments are the same.
+                         TODO: Allow different constants for different vector
+                         stmts generated for an SLP instance.  */
+                      for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
+                        VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
+                    }
+                }
+            }
+
+          /* vec_oprnd1 is available if operand 1 should be of a scalar-type
+             (a special case for certain kind of vector shifts); otherwise,
+             operand 1 should be of a vector type (the usual case).  */
+          if (vec_oprnd1)
+            vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
+                               slp_node);
+          else
+            vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
+                               slp_node);
+        }
+      else
+        vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
+
+      /* Arguments are ready.  Create the new vector stmt.  */
+      FOR_EACH_VEC_ELT (tree, vec_oprnds0, i, vop0)
+        {
+          vop1 = VEC_index (tree, vec_oprnds1, i);
+          new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
+          new_temp = make_ssa_name (vec_dest, new_stmt);
+          gimple_assign_set_lhs (new_stmt, new_temp);
+          vect_finish_stmt_generation (stmt, new_stmt, gsi);
+          if (slp_node)
+            VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
+        }
+
+      if (slp_node)
+        continue;
+
+      if (j == 0)
+        STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
+      else
+        STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
+      prev_stmt_info = vinfo_for_stmt (new_stmt);
+    }
+
+  VEC_free (tree, heap, vec_oprnds0);
+  VEC_free (tree, heap, vec_oprnds1);
+
+  return true;
+}
+
+
+/* Function vectorizable_operation.
+
+   Check if STMT performs a binary, unary or ternary operation that can
+   be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
+   stmt to replace it, put it in VEC_STMT, and insert it at BSI.
+   Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
+
+static bool
+vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
+                       gimple *vec_stmt, slp_tree slp_node)
+{
+  tree vec_dest;
+  tree scalar_dest;
+  tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
+  stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
+  tree vectype;
+  loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
+  enum tree_code code;
+  enum machine_mode vec_mode;
+  tree new_temp;
+  int op_type;
+  optab optab;
+  int icode;
+  tree def;
+  gimple def_stmt;
+  enum vect_def_type dt[3]
+    = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
+  gimple new_stmt = NULL;
+  stmt_vec_info prev_stmt_info;
+  int nunits_in;
+  int nunits_out;
+  tree vectype_out;
+  int ncopies;
+  int j, i;
+  VEC(tree,heap) *vec_oprnds0 = NULL, *vec_oprnds1 = NULL, *vec_oprnds2 = NULL;
+  tree vop0, vop1, vop2;
+  bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
+  int vf;
+
   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
     return false;
 
@@ -1963,14 +2707,6 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
     return false;
 
-  scalar_dest = gimple_assign_lhs (stmt);
-  vectype_out = get_vectype_for_scalar_type (TREE_TYPE (scalar_dest));
-  if (!vectype_out)
-    return false;
-  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
-  if (nunits_out != nunits_in)
-    return false;
-
   code = gimple_assign_rhs_code (stmt);
 
   /* For pointer addition, we should use the normal plus for
@@ -1980,91 +2716,105 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
 
   /* Support only unary or binary operations.  */
   op_type = TREE_CODE_LENGTH (code);
-  if (op_type != unary_op && op_type != binary_op)
+  if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
     {
       if (vect_print_dump_info (REPORT_DETAILS))
-       fprintf (vect_dump, "num. args = %d (not unary/binary op).", op_type);
+       fprintf (vect_dump, "num. args = %d (not unary/binary/ternary op).",
+                op_type);
       return false;
     }
 
-  op0 = gimple_assign_rhs1 (stmt);
-  if (!vect_is_simple_use (op0, loop_vinfo, bb_vinfo, &def_stmt, &def, &dt[0]))
+  scalar_dest = gimple_assign_lhs (stmt);
+  vectype_out = STMT_VINFO_VECTYPE (stmt_info);
+
+  /* Most operations cannot handle bit-precision types without extra
+     truncations.  */
+  if ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
+       != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
+      /* Exception are bitwise binary operations.  */
+      && code != BIT_IOR_EXPR
+      && code != BIT_XOR_EXPR
+      && code != BIT_AND_EXPR)
     {
       if (vect_print_dump_info (REPORT_DETAILS))
-        fprintf (vect_dump, "use not simple.");
+        fprintf (vect_dump, "bit-precision arithmetic not supported.");
       return false;
     }
 
-  if (op_type == binary_op)
+  op0 = gimple_assign_rhs1 (stmt);
+  if (!vect_is_simple_use_1 (op0, loop_vinfo, bb_vinfo,
+                            &def_stmt, &def, &dt[0], &vectype))
     {
-      op1 = gimple_assign_rhs2 (stmt);
-      if (!vect_is_simple_use (op1, loop_vinfo, bb_vinfo, &def_stmt, &def, 
-                               &dt[1]))
-       {
-         if (vect_print_dump_info (REPORT_DETAILS))
-           fprintf (vect_dump, "use not simple.");
-         return false;
-       }
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "use not simple.");
+      return false;
     }
-
-  /* If this is a shift/rotate, determine whether the shift amount is a vector,
-     or scalar.  If the shift/rotate amount is a vector, use the vector/vector
-     shift optabs.  */
-  if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
-      || code == RROTATE_EXPR)
+  /* If op0 is an external or constant def use a vector type with
+     the same size as the output vector type.  */
+  if (!vectype)
+    vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
+  if (vec_stmt)
+    gcc_assert (vectype);
+  if (!vectype)
     {
-      shift_p = true;
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "no vectype for scalar type ");
+          print_generic_expr (vect_dump, TREE_TYPE (op0), TDF_SLIM);
+        }
 
-      /* vector shifted by vector */
-      if (dt[1] == vect_internal_def)
-       {
-         optab = optab_for_tree_code (code, vectype, optab_vector);
-         if (vect_print_dump_info (REPORT_DETAILS))
-           fprintf (vect_dump, "vector/vector shift/rotate found.");
-       }
+      return false;
+    }
 
-      /* See if the machine has a vector shifted by scalar insn and if not
-        then see if it has a vector shifted by vector insn */
-      else if (dt[1] == vect_constant_def || dt[1] == vect_external_def)
-       {
-         optab = optab_for_tree_code (code, vectype, optab_scalar);
-         if (optab
-             && (optab_handler (optab, TYPE_MODE (vectype))->insn_code
-                 != CODE_FOR_nothing))
-           {
-             scalar_shift_arg = true;
-             if (vect_print_dump_info (REPORT_DETAILS))
-               fprintf (vect_dump, "vector/scalar shift/rotate found.");
-           }
-         else
-           {
-             optab = optab_for_tree_code (code, vectype, optab_vector);
-             if (optab
-                 && (optab_handler (optab, TYPE_MODE (vectype))->insn_code
-                     != CODE_FOR_nothing))
-               {
-                 if (vect_print_dump_info (REPORT_DETAILS))
-                   fprintf (vect_dump, "vector/vector shift/rotate found.");
-
-                 /* Unlike the other binary operators, shifts/rotates have
-                    the rhs being int, instead of the same type as the lhs,
-                    so make sure the scalar is the right type if we are
-                    dealing with vectors of short/char.  */
-                 if (dt[1] == vect_constant_def)
-                   op1 = fold_convert (TREE_TYPE (vectype), op1);
-               }
-           }
+  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
+  nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
+  if (nunits_out != nunits_in)
+    return false;
+
+  if (op_type == binary_op || op_type == ternary_op)
+    {
+      op1 = gimple_assign_rhs2 (stmt);
+      if (!vect_is_simple_use (op1, loop_vinfo, bb_vinfo, &def_stmt, &def,
+                               &dt[1]))
+       {
+         if (vect_print_dump_info (REPORT_DETAILS))
+           fprintf (vect_dump, "use not simple.");
+         return false;
        }
-
-      else
+    }
+  if (op_type == ternary_op)
+    {
+      op2 = gimple_assign_rhs3 (stmt);
+      if (!vect_is_simple_use (op2, loop_vinfo, bb_vinfo, &def_stmt, &def,
+                               &dt[2]))
        {
          if (vect_print_dump_info (REPORT_DETAILS))
-           fprintf (vect_dump, "operand mode requires invariant argument.");
+           fprintf (vect_dump, "use not simple.");
          return false;
        }
     }
+
+  if (loop_vinfo)
+    vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
+  else
+    vf = 1;
+
+  /* Multiple types in SLP are handled by creating the appropriate number of
+     vectorized stmts for each SLP node.  Hence, NCOPIES is always 1 in
+     case of SLP.  */
+  if (slp_node || PURE_SLP_STMT (stmt_info))
+    ncopies = 1;
   else
-    optab = optab_for_tree_code (code, vectype, optab_default);
+    ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
+
+  gcc_assert (ncopies >= 1);
+
+  /* Shifts are handled in vectorizable_shift ().  */
+  if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
+      || code == RROTATE_EXPR)
+   return false;
+
+  optab = optab_for_tree_code (code, vectype, optab_default);
 
   /* Supportable by target?  */
   if (!optab)
@@ -2074,7 +2824,7 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
       return false;
     }
   vec_mode = TYPE_MODE (vectype);
-  icode = (int) optab_handler (optab, vec_mode)->insn_code;
+  icode = (int) optab_handler (optab, vec_mode);
   if (icode == CODE_FOR_nothing)
     {
       if (vect_print_dump_info (REPORT_DETAILS))
@@ -2088,7 +2838,7 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
        fprintf (vect_dump, "proceeding using word mode.");
     }
 
-  /* Worthwhile without SIMD support? Check only during analysis.  */
+  /* Worthwhile without SIMD support?  Check only during analysis.  */
   if (!VECTOR_MODE_P (TYPE_MODE (vectype))
       && vf < vect_min_worthwhile_factor (code)
       && !vec_stmt)
@@ -2115,32 +2865,32 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
   /* Handle def.  */
   vec_dest = vect_create_destination_var (scalar_dest, vectype);
 
-  /* Allocate VECs for vector operands. In case of SLP, vector operands are 
+  /* Allocate VECs for vector operands.  In case of SLP, vector operands are
      created in the previous stages of the recursion, so no allocation is
-     needed, except for the case of shift with scalar shift argument. In that
+     needed, except for the case of shift with scalar shift argument.  In that
      case we store the scalar operand in VEC_OPRNDS1 for every vector stmt to
      be created to vectorize the SLP group, i.e., SLP_NODE->VEC_STMTS_SIZE.
-     In case of loop-based vectorization we allocate VECs of size 1. We 
-     allocate VEC_OPRNDS1 only in case of binary operation.  */ 
+     In case of loop-based vectorization we allocate VECs of size 1.  We
+     allocate VEC_OPRNDS1 only in case of binary operation.  */
   if (!slp_node)
     {
       vec_oprnds0 = VEC_alloc (tree, heap, 1);
-      if (op_type == binary_op)
+      if (op_type == binary_op || op_type == ternary_op)
         vec_oprnds1 = VEC_alloc (tree, heap, 1);
+      if (op_type == ternary_op)
+        vec_oprnds2 = VEC_alloc (tree, heap, 1);
     }
-  else if (scalar_shift_arg)
-    vec_oprnds1 = VEC_alloc (tree, heap, slp_node->vec_stmts_size);  
 
   /* In case the vectorization factor (VF) is bigger than the number
      of elements that we can fit in a vectype (nunits), we have to generate
      more than one vector stmt - i.e - we need to "unroll" the
-     vector stmt by a factor VF/nunits. In doing so, we record a pointer
+     vector stmt by a factor VF/nunits.  In doing so, we record a pointer
      from one copy of the vector stmt to the next, in the field
-     STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
+     STMT_VINFO_RELATED_STMT.  This is necessary in order to allow following
      stages to find the correct vector defs to be used when vectorizing
-     stmts that use the defs of the current stmt. The example below illustrates
-     the vectorization process when VF=16 and nunits=4 (i.e - we need to create
-     4 vectorized stmts):
+     stmts that use the defs of the current stmt.  The example below
+     illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
+     we need to create 4 vectorized stmts):
 
      before vectorization:
                                 RELATED_STMT    VEC_STMT
@@ -2159,18 +2909,18 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
 
      step2: vectorize stmt S2 (done here):
         To vectorize stmt S2 we first need to find the relevant vector
-        def for the first operand 'x'. This is, as usual, obtained from
+        def for the first operand 'x'.  This is, as usual, obtained from
         the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
-        that defines 'x' (S1). This way we find the stmt VS1_0, and the
-        relevant vector def 'vx0'. Having found 'vx0' we can generate
+        that defines 'x' (S1).  This way we find the stmt VS1_0, and the
+        relevant vector def 'vx0'.  Having found 'vx0' we can generate
         the vector stmt VS2_0, and as usual, record it in the
         STMT_VINFO_VEC_STMT of stmt S2.
         When creating the second copy (VS2_1), we obtain the relevant vector
         def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
-        stmt VS1_0. This way we find the stmt VS1_1 and the relevant
-        vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
+        stmt VS1_0.  This way we find the stmt VS1_1 and the relevant
+        vector def 'vx1'.  Using 'vx1' we create stmt VS2_1 and record a
         pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
-        Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
+        Similarly when creating stmts VS2_2 and VS2_3.  This is the resulting
         chain of stmts and pointers:
                                 RELATED_STMT    VEC_STMT
         VS1_0:  vx0 = memref0   VS1_1           -
@@ -2190,51 +2940,40 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
       /* Handle uses.  */
       if (j == 0)
        {
-         if (op_type == binary_op && scalar_shift_arg)
-           {
-             /* Vector shl and shr insn patterns can be defined with scalar 
-                operand 2 (shift operand). In this case, use constant or loop 
-                invariant op1 directly, without extending it to vector mode 
-                first.  */
-             optab_op2_mode = insn_data[icode].operand[2].mode;
-             if (!VECTOR_MODE_P (optab_op2_mode))
-               {
-                 if (vect_print_dump_info (REPORT_DETAILS))
-                   fprintf (vect_dump, "operand 1 using scalar mode.");
-                 vec_oprnd1 = op1;
-                 VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
-                 if (slp_node)
-                   {
-                     /* Store vec_oprnd1 for every vector stmt to be created
-                        for SLP_NODE. We check during the analysis that all the
-                         shift arguments are the same.  
-                        TODO: Allow different constants for different vector 
-                        stmts generated for an SLP instance.  */          
-                     for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
-                       VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
-                   }
-               }
-           }
-        
-          /* vec_oprnd1 is available if operand 1 should be of a scalar-type 
-             (a special case for certain kind of vector shifts); otherwise, 
-             operand 1 should be of a vector type (the usual case).  */
-         if (op_type == binary_op && !vec_oprnd1)
-           vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1, 
+         if (op_type == binary_op || op_type == ternary_op)
+           vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
                               slp_node);
          else
-           vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL, 
+           vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
                               slp_node);
+         if (op_type == ternary_op)
+           {
+             vec_oprnds2 = VEC_alloc (tree, heap, 1);
+             VEC_quick_push (tree, vec_oprnds2,
+                             vect_get_vec_def_for_operand (op2, stmt, NULL));
+           }
        }
       else
-       vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
+       {
+         vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
+         if (op_type == ternary_op)
+           {
+             tree vec_oprnd = VEC_pop (tree, vec_oprnds2);
+             VEC_quick_push (tree, vec_oprnds2,
+                             vect_get_vec_def_for_stmt_copy (dt[2],
+                                                             vec_oprnd));
+           }
+       }
 
-      /* Arguments are ready. Create the new vector stmt.  */
-      for (i = 0; VEC_iterate (tree, vec_oprnds0, i, vop0); i++)
+      /* Arguments are ready.  Create the new vector stmt.  */
+      FOR_EACH_VEC_ELT (tree, vec_oprnds0, i, vop0)
         {
-         vop1 = ((op_type == binary_op)
-                 ? VEC_index (tree, vec_oprnds1, i) : NULL);
-         new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
+         vop1 = ((op_type == binary_op || op_type == ternary_op)
+                 ? VEC_index (tree, vec_oprnds1, i) : NULL_TREE);
+         vop2 = ((op_type == ternary_op)
+                 ? VEC_index (tree, vec_oprnds2, i) : NULL_TREE);
+         new_stmt = gimple_build_assign_with_ops3 (code, vec_dest,
+                                                   vop0, vop1, vop2);
          new_temp = make_ssa_name (vec_dest, new_stmt);
          gimple_assign_set_lhs (new_stmt, new_temp);
          vect_finish_stmt_generation (stmt, new_stmt, gsi);
@@ -2255,20 +2994,22 @@ vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
   VEC_free (tree, heap, vec_oprnds0);
   if (vec_oprnds1)
     VEC_free (tree, heap, vec_oprnds1);
+  if (vec_oprnds2)
+    VEC_free (tree, heap, vec_oprnds2);
 
   return true;
 }
 
 
-/* Get vectorized definitions for loop-based vectorization. For the first
-   operand we call vect_get_vec_def_for_operand() (with OPRND containing 
-   scalar operand), and for the rest we get a copy with 
+/* Get vectorized definitions for loop-based vectorization.  For the first
+   operand we call vect_get_vec_def_for_operand() (with OPRND containing
+   scalar operand), and for the rest we get a copy with
    vect_get_vec_def_for_stmt_copy() using the previous vector definition
    (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
    The vectors are collected into VEC_OPRNDS.  */
 
 static void
-vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt, 
+vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
                           VEC (tree, heap) **vec_oprnds, int multi_step_cvt)
 {
   tree vec_oprnd;
@@ -2276,7 +3017,7 @@ vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
   /* Get first vector operand.  */
   /* All the vector operands except the very first one (that is scalar oprnd)
      are stmt copies.  */
-  if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)  
+  if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
     vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt, NULL);
   else
     vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
@@ -2286,18 +3027,18 @@ vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
   /* Get second vector operand.  */
   vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
   VEC_quick_push (tree, *vec_oprnds, vec_oprnd);
-    
+
   *oprnd = vec_oprnd;
 
-  /* For conversion in multiple steps, continue to get operands 
+  /* For conversion in multiple steps, continue to get operands
      recursively.  */
   if (multi_step_cvt)
-    vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds,  multi_step_cvt - 1); 
+    vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds,  multi_step_cvt - 1);
 }
 
 
 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
-   For multi-step conversions store the resulting vectors and call the function 
+   For multi-step conversions store the resulting vectors and call the function
    recursively.  */
 
 static void
@@ -2313,7 +3054,7 @@ vect_create_vectorized_demotion_stmts (VEC (tree, heap) **vec_oprnds,
   gimple new_stmt;
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
 
-  vec_dest = VEC_pop (tree, vec_dsts); 
+  vec_dest = VEC_pop (tree, vec_dsts);
 
   for (i = 0; i < VEC_length (tree, *vec_oprnds); i += 2)
     {
@@ -2327,10 +3068,10 @@ vect_create_vectorized_demotion_stmts (VEC (tree, heap) **vec_oprnds,
 
       if (multi_step_cvt)
         /* Store the resulting vector for next recursive call.  */
-        VEC_replace (tree, *vec_oprnds, i/2, new_tmp);      
+        VEC_replace (tree, *vec_oprnds, i/2, new_tmp);
       else
         {
-          /* This is the last step of the conversion sequence. Store the 
+          /* This is the last step of the conversion sequence. Store the
              vectors in SLP_NODE or in vector info of the scalar statement
              (or in STMT_VINFO_RELATED_STMT chain).  */
           if (slp_node)
@@ -2348,7 +3089,7 @@ vect_create_vectorized_demotion_stmts (VEC (tree, heap) **vec_oprnds,
     }
 
   /* For multi-step demotion operations we first generate demotion operations
-     from the source type to the intermediate types, and then combine the 
+     from the source type to the intermediate types, and then combine the
      results (stored in VEC_OPRNDS) in demotion operation to the destination
      type.  */
   if (multi_step_cvt)
@@ -2356,7 +3097,7 @@ vect_create_vectorized_demotion_stmts (VEC (tree, heap) **vec_oprnds,
       /* At each level of recursion we have have of the operands we had at the
          previous level.  */
       VEC_truncate (tree, *vec_oprnds, (i+1)/2);
-      vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1, 
+      vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
                                              stmt, vec_dsts, gsi, slp_node,
                                              code, prev_stmt_info);
     }
@@ -2395,11 +3136,9 @@ vectorizable_type_demotion (gimple stmt, gimple_stmt_iterator *gsi,
   VEC (tree, heap) *vec_oprnds0 = NULL;
   VEC (tree, heap) *vec_dsts = NULL, *interm_types = NULL, *tmp_vec_dsts = NULL;
   tree last_oprnd, intermediate_type;
+  bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
 
-  /* FORNOW: not supported by basic block SLP vectorization.  */
-  gcc_assert (loop_vinfo);
-
-  if (!STMT_VINFO_RELEVANT_P (stmt_info))
+  if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
     return false;
 
   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
@@ -2416,51 +3155,71 @@ vectorizable_type_demotion (gimple stmt, gimple_stmt_iterator *gsi,
   if (!CONVERT_EXPR_CODE_P (code))
     return false;
 
+  scalar_dest = gimple_assign_lhs (stmt);
+  vectype_out = STMT_VINFO_VECTYPE (stmt_info);
+
+  /* Check the operands of the operation.  */
   op0 = gimple_assign_rhs1 (stmt);
-  vectype_in = get_vectype_for_scalar_type (TREE_TYPE (op0));
-  if (!vectype_in)
+  if (! ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
+         && INTEGRAL_TYPE_P (TREE_TYPE (op0)))
+        || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (scalar_dest))
+            && SCALAR_FLOAT_TYPE_P (TREE_TYPE (op0)))))
     return false;
-  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
 
-  scalar_dest = gimple_assign_lhs (stmt);
-  vectype_out = get_vectype_for_scalar_type (TREE_TYPE (scalar_dest));
-  if (!vectype_out)
-    return false;
+  if (INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
+      && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
+          != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
+         || ((TYPE_PRECISION (TREE_TYPE (op0))
+              != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op0)))))))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "type demotion to/from bit-precision unsupported.");
+      return false;
+    }
+
+  if (!vect_is_simple_use_1 (op0, loop_vinfo, bb_vinfo,
+                            &def_stmt, &def, &dt[0], &vectype_in))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "use not simple.");
+      return false;
+    }
+  /* If op0 is an external def use a vector type with the
+     same size as the output vector type if possible.  */
+  if (!vectype_in)
+    vectype_in = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
+  if (vec_stmt)
+    gcc_assert (vectype_in);
+  if (!vectype_in)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "no vectype for scalar type ");
+          print_generic_expr (vect_dump, TREE_TYPE (op0), TDF_SLIM);
+        }
+
+      return false;
+    }
+
+  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
   if (nunits_in >= nunits_out)
     return false;
 
   /* Multiple types in SLP are handled by creating the appropriate number of
-     vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
+     vectorized stmts for each SLP node.  Hence, NCOPIES is always 1 in
      case of SLP.  */
-  if (slp_node)
+  if (slp_node || PURE_SLP_STMT (stmt_info))
     ncopies = 1;
   else
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
   gcc_assert (ncopies >= 1);
 
-  if (! ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
-         && INTEGRAL_TYPE_P (TREE_TYPE (op0)))
-        || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (scalar_dest))
-            && SCALAR_FLOAT_TYPE_P (TREE_TYPE (op0))
-            && CONVERT_EXPR_CODE_P (code))))
-    return false;
-
-  /* Check the operands of the operation.  */
-  if (!vect_is_simple_use (op0, loop_vinfo, NULL, &def_stmt, &def, &dt[0]))
-    {
-      if (vect_print_dump_info (REPORT_DETAILS))
-        fprintf (vect_dump, "use not simple.");
-      return false;
-    }
-
   /* Supportable by target?  */
-  if (!supportable_narrowing_operation (code, stmt, vectype_in, &code1,
-                                        &multi_step_cvt, &interm_types))
+  if (!supportable_narrowing_operation (code, vectype_out, vectype_in,
+                                       &code1, &multi_step_cvt, &interm_types))
     return false;
 
-  STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
-
   if (!vec_stmt) /* transformation not required.  */
     {
       STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
@@ -2475,25 +3234,25 @@ vectorizable_type_demotion (gimple stmt, gimple_stmt_iterator *gsi,
     fprintf (vect_dump, "transform type demotion operation. ncopies = %d.",
             ncopies);
 
-  /* In case of multi-step demotion, we first generate demotion operations to 
-     the intermediate types, and then from that types to the final one. 
+  /* In case of multi-step demotion, we first generate demotion operations to
+     the intermediate types, and then from that types to the final one.
      We create vector destinations for the intermediate type (TYPES) received
-     from supportable_narrowing_operation, and store them in the correct order 
+     from supportable_narrowing_operation, and store them in the correct order
      for future use in vect_create_vectorized_demotion_stmts().  */
   if (multi_step_cvt)
     vec_dsts = VEC_alloc (tree, heap, multi_step_cvt + 1);
   else
     vec_dsts = VEC_alloc (tree, heap, 1);
+
   vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
   VEC_quick_push (tree, vec_dsts, vec_dest);
 
   if (multi_step_cvt)
     {
-      for (i = VEC_length (tree, interm_types) - 1; 
+      for (i = VEC_length (tree, interm_types) - 1;
            VEC_iterate (tree, interm_types, i, intermediate_type); i--)
         {
-          vec_dest = vect_create_destination_var (scalar_dest, 
+          vec_dest = vect_create_destination_var (scalar_dest,
                                                   intermediate_type);
           VEC_quick_push (tree, vec_dsts, vec_dest);
         }
@@ -2509,21 +3268,21 @@ vectorizable_type_demotion (gimple stmt, gimple_stmt_iterator *gsi,
     {
       /* Handle uses.  */
       if (slp_node)
-        vect_get_slp_defs (slp_node, &vec_oprnds0, NULL); 
+        vect_get_slp_defs (op0, NULL_TREE, slp_node, &vec_oprnds0, NULL, -1);
       else
         {
           VEC_free (tree, heap, vec_oprnds0);
           vec_oprnds0 = VEC_alloc (tree, heap,
                         (multi_step_cvt ? vect_pow2 (multi_step_cvt) * 2 : 2));
-          vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,  
+          vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
                                     vect_pow2 (multi_step_cvt) - 1);
         }
 
-      /* Arguments are ready. Create the new vector stmts.  */
+      /* Arguments are ready.  Create the new vector stmts.  */
       tmp_vec_dsts = VEC_copy (tree, heap, vec_dsts);
-      vect_create_vectorized_demotion_stmts (&vec_oprnds0,  
+      vect_create_vectorized_demotion_stmts (&vec_oprnds0,
                                              multi_step_cvt, stmt, tmp_vec_dsts,
-                                             gsi, slp_node, code1, 
+                                             gsi, slp_node, code1,
                                              &prev_stmt_info);
     }
 
@@ -2538,7 +3297,7 @@ vectorizable_type_demotion (gimple stmt, gimple_stmt_iterator *gsi,
 
 
 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
-   and VEC_OPRNDS1 (for binary operations). For multi-step conversions store 
+   and VEC_OPRNDS1 (for binary operations).  For multi-step conversions store
    the resulting vectors and call the function recursively.  */
 
 static void
@@ -2548,7 +3307,7 @@ vect_create_vectorized_promotion_stmts (VEC (tree, heap) **vec_oprnds0,
                                         VEC (tree, heap) *vec_dsts,
                                         gimple_stmt_iterator *gsi,
                                         slp_tree slp_node, enum tree_code code1,
-                                        enum tree_code code2, tree decl1, 
+                                        enum tree_code code2, tree decl1,
                                         tree decl2, int op_type,
                                         stmt_vec_info *prev_stmt_info)
 {
@@ -2561,7 +3320,7 @@ vect_create_vectorized_promotion_stmts (VEC (tree, heap) **vec_oprnds0,
   vec_dest = VEC_pop (tree, vec_dsts);
   vec_tmp = VEC_alloc (tree, heap, VEC_length (tree, *vec_oprnds0) * 2);
 
-  for (i = 0; VEC_iterate (tree, *vec_oprnds0, i, vop0); i++)
+  FOR_EACH_VEC_ELT (tree, *vec_oprnds0, i, vop0)
     {
       if (op_type == binary_op)
         vop1 = VEC_index (tree, *vec_oprnds1, i);
@@ -2569,7 +3328,7 @@ vect_create_vectorized_promotion_stmts (VEC (tree, heap) **vec_oprnds0,
         vop1 = NULL_TREE;
 
       /* Generate the two halves of promotion operation.  */
-      new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,  
+      new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
                                                  op_type, vec_dest, gsi, stmt);
       new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
                                                  op_type, vec_dest, gsi, stmt);
@@ -2614,20 +3373,21 @@ vect_create_vectorized_promotion_stmts (VEC (tree, heap) **vec_oprnds0,
 
   if (multi_step_cvt)
     {
-      /* For multi-step promotion operation we first generate we call the 
-         function recurcively for every stage. We start from the input type,
+      /* For multi-step promotion operation we first generate we call the
+         function recurcively for every stage.  We start from the input type,
          create promotion operations to the intermediate types, and then
          create promotions to the output type.  */
       *vec_oprnds0 = VEC_copy (tree, heap, vec_tmp);
-      VEC_free (tree, heap, vec_tmp);
       vect_create_vectorized_promotion_stmts (vec_oprnds0, vec_oprnds1,
                                               multi_step_cvt - 1, stmt,
                                               vec_dsts, gsi, slp_node, code1,
                                               code2, decl2, decl2, op_type,
                                               prev_stmt_info);
     }
+
+  VEC_free (tree, heap, vec_tmp);
 }
-    
+
 
 /* Function vectorizable_type_promotion
 
@@ -2649,7 +3409,7 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
   enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
   tree decl1 = NULL_TREE, decl2 = NULL_TREE;
-  int op_type; 
+  int op_type;
   tree def;
   gimple def_stmt;
   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
@@ -2664,11 +3424,10 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
   int multi_step_cvt = 0;
   VEC (tree, heap) *vec_oprnds0 = NULL, *vec_oprnds1 = NULL;
   VEC (tree, heap) *vec_dsts = NULL, *interm_types = NULL, *tmp_vec_dsts = NULL;
-  
-  /* FORNOW: not supported by basic block SLP vectorization.  */
-  gcc_assert (loop_vinfo);
-    
-  if (!STMT_VINFO_RELEVANT_P (stmt_info))
+  bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
+  unsigned int k;
+
+  if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
     return false;
 
   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
@@ -2683,33 +3442,15 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
 
   code = gimple_assign_rhs_code (stmt);
   if (!CONVERT_EXPR_CODE_P (code)
-      && code != WIDEN_MULT_EXPR)
-    return false;
-
-  op0 = gimple_assign_rhs1 (stmt);
-  vectype_in = get_vectype_for_scalar_type (TREE_TYPE (op0));
-  if (!vectype_in)
+      && code != WIDEN_MULT_EXPR
+      && code != WIDEN_LSHIFT_EXPR)
     return false;
-  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
 
   scalar_dest = gimple_assign_lhs (stmt);
-  vectype_out = get_vectype_for_scalar_type (TREE_TYPE (scalar_dest));
-  if (!vectype_out)
-    return false;
-  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
-  if (nunits_in <= nunits_out)
-    return false;
-
-  /* Multiple types in SLP are handled by creating the appropriate number of
-     vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
-     case of SLP.  */
-  if (slp_node)
-    ncopies = 1;
-  else
-    ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
-
-  gcc_assert (ncopies >= 1);
+  vectype_out = STMT_VINFO_VECTYPE (stmt_info);
 
+  /* Check the operands of the operation.  */
+  op0 = gimple_assign_rhs1 (stmt);
   if (! ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
          && INTEGRAL_TYPE_P (TREE_TYPE (op0)))
         || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (scalar_dest))
@@ -2717,8 +3458,20 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
             && CONVERT_EXPR_CODE_P (code))))
     return false;
 
-  /* Check the operands of the operation.  */
-  if (!vect_is_simple_use (op0, loop_vinfo, NULL, &def_stmt, &def, &dt[0]))
+  if (INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
+      && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
+          != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
+         || ((TYPE_PRECISION (TREE_TYPE (op0))
+              != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op0)))))))
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "type promotion to/from bit-precision "
+                "unsupported.");
+      return false;
+    }
+
+  if (!vect_is_simple_use_1 (op0, loop_vinfo, bb_vinfo,
+                            &def_stmt, &def, &dt[0], &vectype_in))
     {
       if (vect_print_dump_info (REPORT_DETAILS))
        fprintf (vect_dump, "use not simple.");
@@ -2728,17 +3481,63 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
   op_type = TREE_CODE_LENGTH (code);
   if (op_type == binary_op)
     {
+      bool ok;
+
       op1 = gimple_assign_rhs2 (stmt);
-      if (!vect_is_simple_use (op1, loop_vinfo, NULL, &def_stmt, &def, &dt[1]))
+      if (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR)
         {
-         if (vect_print_dump_info (REPORT_DETAILS))
-           fprintf (vect_dump, "use not simple.");
-          return false;
+         /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
+            OP1.  */
+          if (CONSTANT_CLASS_P (op0))
+            ok = vect_is_simple_use_1 (op1, loop_vinfo, NULL,
+                             &def_stmt, &def, &dt[1], &vectype_in);
+          else
+            ok = vect_is_simple_use (op1, loop_vinfo, NULL, &def_stmt, &def,
+                                     &dt[1]);
+
+          if (!ok)
+            {
+             if (vect_print_dump_info (REPORT_DETAILS))
+               fprintf (vect_dump, "use not simple.");
+              return false;
+            }
+        }        
+    }
+
+  /* If op0 is an external or constant def use a vector type with
+     the same size as the output vector type.  */
+  if (!vectype_in)
+    vectype_in = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
+  if (vec_stmt)
+    gcc_assert (vectype_in);
+  if (!vectype_in)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "no vectype for scalar type ");
+          print_generic_expr (vect_dump, TREE_TYPE (op0), TDF_SLIM);
         }
+
+      return false;
     }
 
+  nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
+  nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
+  if (nunits_in <= nunits_out)
+    return false;
+
+  /* Multiple types in SLP are handled by creating the appropriate number of
+     vectorized stmts for each SLP node.  Hence, NCOPIES is always 1 in
+     case of SLP.  */
+  if (slp_node || PURE_SLP_STMT (stmt_info))
+    ncopies = 1;
+  else
+    ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
+
+  gcc_assert (ncopies >= 1);
+
   /* Supportable by target?  */
-  if (!supportable_widening_operation (code, stmt, vectype_in,
+  if (!supportable_widening_operation (code, stmt, vectype_out, vectype_in,
                                       &decl1, &decl2, &code1, &code2,
                                        &multi_step_cvt, &interm_types))
     return false;
@@ -2747,8 +3546,6 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
      architecture.  */
   gcc_assert (!(multi_step_cvt && op_type == binary_op));
 
-  STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
-
   if (!vec_stmt) /* transformation not required.  */
     {
       STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
@@ -2764,11 +3561,19 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
     fprintf (vect_dump, "transform type promotion operation. ncopies = %d.",
                         ncopies);
 
+  if (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR)
+    {
+      if (CONSTANT_CLASS_P (op0))
+       op0 = fold_convert (TREE_TYPE (op1), op0);
+      else if (CONSTANT_CLASS_P (op1))
+       op1 = fold_convert (TREE_TYPE (op0), op1);
+    }
+
   /* Handle def.  */
-  /* In case of multi-step promotion, we first generate promotion operations 
+  /* In case of multi-step promotion, we first generate promotion operations
      to the intermediate types, and then from that types to the final one.
-     We store vector destination in VEC_DSTS in the correct order for 
-     recursive creation of promotion operations in 
+     We store vector destination in VEC_DSTS in the correct order for
+     recursive creation of promotion operations in
      vect_create_vectorized_promotion_stmts(). Vector destinations are created
      according to TYPES recieved from supportable_widening_operation().   */
   if (multi_step_cvt)
@@ -2789,14 +3594,16 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
           VEC_quick_push (tree, vec_dsts, vec_dest);
         }
     }
-  
+
   if (!slp_node)
     {
-      vec_oprnds0 = VEC_alloc (tree, heap, 
+      vec_oprnds0 = VEC_alloc (tree, heap,
                             (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
       if (op_type == binary_op)
         vec_oprnds1 = VEC_alloc (tree, heap, 1);
     }
+  else if (code == WIDEN_LSHIFT_EXPR)
+    vec_oprnds1 = VEC_alloc (tree, heap, slp_node->vec_stmts_size);
 
   /* In case the vectorization factor (VF) is bigger than the number
      of elements that we can fit in a vectype (nunits), we have to generate
@@ -2810,14 +3617,33 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
       if (j == 0)
         {
           if (slp_node)
-              vect_get_slp_defs (slp_node, &vec_oprnds0, &vec_oprnds1);
-          else
+           {
+             if (code == WIDEN_LSHIFT_EXPR)
+                {
+                  vec_oprnd1 = op1;
+                 /* Store vec_oprnd1 for every vector stmt to be created
+                    for SLP_NODE.  We check during the analysis that all
+                    the shift arguments are the same.  */
+                  for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
+                    VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
+
+                 vect_get_slp_defs (op0, NULL_TREE, slp_node, &vec_oprnds0, NULL,
+                                    -1);
+                }
+              else
+                vect_get_slp_defs (op0, op1, slp_node, &vec_oprnds0,
+                                   &vec_oprnds1, -1);
+           }
+         else
             {
               vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
               VEC_quick_push (tree, vec_oprnds0, vec_oprnd0);
               if (op_type == binary_op)
                 {
-                  vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt, NULL);
+                  if (code == WIDEN_LSHIFT_EXPR)
+                    vec_oprnd1 = op1;
+                  else
+                    vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt, NULL);
                   VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
                 }
             }
@@ -2828,15 +3654,18 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
           VEC_replace (tree, vec_oprnds0, 0, vec_oprnd0);
           if (op_type == binary_op)
             {
-              vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd1);
+              if (code == WIDEN_LSHIFT_EXPR)
+                vec_oprnd1 = op1;
+              else
+                vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd1);
               VEC_replace (tree, vec_oprnds1, 0, vec_oprnd1);
             }
         }
 
-      /* Arguments are ready. Create the new vector stmts.  */
+      /* Arguments are ready.  Create the new vector stmts.  */
       tmp_vec_dsts = VEC_copy (tree, heap, vec_dsts);
       vect_create_vectorized_promotion_stmts (&vec_oprnds0, &vec_oprnds1,
-                                              multi_step_cvt, stmt, 
+                                              multi_step_cvt, stmt,
                                               tmp_vec_dsts,
                                               gsi, slp_node, code1, code2,
                                               decl1, decl2, op_type,
@@ -2856,9 +3685,9 @@ vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
 
 /* Function vectorizable_store.
 
-   Check if STMT defines a non scalar data-ref (array/pointer/structure) that 
-   can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
+   Check if STMT defines a non scalar data-ref (array/pointer/structure) that
+   can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
 
@@ -2873,6 +3702,7 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
+  tree elem_type;
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
   struct loop *loop = NULL;
   enum machine_mode vec_mode;
@@ -2888,14 +3718,15 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   int j;
   gimple next_stmt, first_stmt = NULL;
   bool strided_store = false;
+  bool store_lanes_p = false;
   unsigned int group_size, i;
   VEC(tree,heap) *dr_chain = NULL, *oprnds = NULL, *result_chain = NULL;
   bool inv_p;
   VEC(tree,heap) *vec_oprnds = NULL;
   bool slp = (slp_node != NULL);
-  stmt_vec_info first_stmt_vinfo;
   unsigned int vec_num;
   bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
+  tree aggr_type;
 
   if (loop_vinfo)
     loop = LOOP_VINFO_LOOP (loop_vinfo);
@@ -2903,7 +3734,7 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   /* Multiple types in SLP are handled by creating the appropriate number of
      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
      case of SLP.  */
-  if (slp)
+  if (slp || PURE_SLP_STMT (stmt_info))
     ncopies = 1;
   else
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
@@ -2930,11 +3761,15 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
     return false;
 
   scalar_dest = gimple_assign_lhs (stmt);
+  if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
+      && is_pattern_stmt_p (stmt_info))
+    scalar_dest = TREE_OPERAND (scalar_dest, 0);
   if (TREE_CODE (scalar_dest) != ARRAY_REF
       && TREE_CODE (scalar_dest) != INDIRECT_REF
       && TREE_CODE (scalar_dest) != COMPONENT_REF
       && TREE_CODE (scalar_dest) != IMAGPART_EXPR
-      && TREE_CODE (scalar_dest) != REALPART_EXPR)
+      && TREE_CODE (scalar_dest) != REALPART_EXPR
+      && TREE_CODE (scalar_dest) != MEM_REF)
     return false;
 
   gcc_assert (gimple_assign_single_p (stmt));
@@ -2946,49 +3781,54 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
       return false;
     }
 
-  /* The scalar rhs type needs to be trivially convertible to the vector
-     component type.  This should always be the case.  */
-  if (!useless_type_conversion_p (TREE_TYPE (vectype), TREE_TYPE (op)))
-    {      
-      if (vect_print_dump_info (REPORT_DETAILS))
-        fprintf (vect_dump, "???  operands of different types");
-      return false;
-    }
-
+  elem_type = TREE_TYPE (vectype);
   vec_mode = TYPE_MODE (vectype);
+
   /* FORNOW. In some cases can vectorize even if data-type not supported
      (e.g. - array initialization with 0).  */
-  if (optab_handler (mov_optab, (int)vec_mode)->insn_code == CODE_FOR_nothing)
+  if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
     return false;
 
   if (!STMT_VINFO_DATA_REF (stmt_info))
     return false;
 
+  if (tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0)
+    {
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "negative step for store.");
+      return false;
+    }
+
   if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
     {
       strided_store = true;
-      first_stmt = DR_GROUP_FIRST_DR (stmt_info);
-      if (!vect_strided_store_supported (vectype)
-         && !PURE_SLP_STMT (stmt_info) && !slp)
-       return false;
-     
+      first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
+      if (!slp && !PURE_SLP_STMT (stmt_info))
+       {
+         group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
+         if (vect_store_lanes_supported (vectype, group_size))
+           store_lanes_p = true;
+         else if (!vect_strided_store_supported (vectype, group_size))
+           return false;
+       }
+
       if (first_stmt == stmt)
        {
           /* STMT is the leader of the group. Check the operands of all the
              stmts of the group.  */
-          next_stmt = DR_GROUP_NEXT_DR (stmt_info);
+          next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
           while (next_stmt)
             {
              gcc_assert (gimple_assign_single_p (next_stmt));
              op = gimple_assign_rhs1 (next_stmt);
-              if (!vect_is_simple_use (op, loop_vinfo, bb_vinfo, &def_stmt, 
+              if (!vect_is_simple_use (op, loop_vinfo, bb_vinfo, &def_stmt,
                                        &def, &dt))
                 {
                   if (vect_print_dump_info (REPORT_DETAILS))
                     fprintf (vect_dump, "use not simple.");
                   return false;
                 }
-              next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
+              next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
             }
         }
     }
@@ -2996,7 +3836,7 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   if (!vec_stmt) /* transformation not required.  */
     {
       STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
-      vect_model_store_cost (stmt_info, ncopies, dt, NULL);
+      vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt, NULL);
       return true;
     }
 
@@ -3005,17 +3845,17 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   if (strided_store)
     {
       first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
-      group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
+      group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
 
-      DR_GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
+      GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
 
       /* FORNOW */
       gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
 
       /* We vectorize all the stmts of the interleaving group when we
         reach the last stmt in the group.  */
-      if (DR_GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt)) 
-         < DR_GROUP_SIZE (vinfo_for_stmt (first_stmt))
+      if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
+         < GROUP_SIZE (vinfo_for_stmt (first_stmt))
          && !slp)
        {
          *vec_stmt = NULL;
@@ -3023,35 +3863,49 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
        }
 
       if (slp)
-       strided_store = false;
-
-      /* VEC_NUM is the number of vect stmts to be created for this group.  */
-      if (slp)
-       vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
+        {
+          strided_store = false;
+          /* VEC_NUM is the number of vect stmts to be created for this 
+             group.  */
+          vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
+          first_stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (slp_node), 0); 
+          first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
+        } 
       else
+        /* VEC_NUM is the number of vect stmts to be created for this 
+           group.  */
        vec_num = group_size;
     }
-  else 
+  else
     {
       first_stmt = stmt;
       first_dr = dr;
       group_size = vec_num = 1;
-      first_stmt_vinfo = stmt_info;
     }
-  
+
   if (vect_print_dump_info (REPORT_DETAILS))
     fprintf (vect_dump, "transform store. ncopies = %d",ncopies);
 
   dr_chain = VEC_alloc (tree, heap, group_size);
   oprnds = VEC_alloc (tree, heap, group_size);
 
-  alignment_support_scheme = vect_supportable_dr_alignment (first_dr);
+  alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
   gcc_assert (alignment_support_scheme);
+  /* Targets with store-lane instructions must not require explicit
+     realignment.  */
+  gcc_assert (!store_lanes_p
+             || alignment_support_scheme == dr_aligned
+             || alignment_support_scheme == dr_unaligned_supported);
+
+  if (store_lanes_p)
+    aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
+  else
+    aggr_type = vectype;
 
   /* In case the vectorization factor (VF) is bigger than the number
      of elements that we can fit in a vectype (nunits), we have to generate
      more than one vector stmt - i.e - we need to "unroll" the
-     vector stmt by a factor VF/nunits.  For more details see documentation in 
+     vector stmt by a factor VF/nunits.  For more details see documentation in
      vect_get_vec_def_for_copy_stmt.  */
 
   /* In case of interleaving (non-unit strided access):
@@ -3075,16 +3929,16 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
         VS5: vx5 = VEC_INTERLEAVE_HIGH_EXPR < vx0, vx3 >
         VS6: vx6 = VEC_INTERLEAVE_LOW_EXPR < vx0, vx3 >
        ...
-       
+
      And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
      (the order of the data-refs in the output of vect_permute_store_chain
      corresponds to the order of scalar stmts in the interleaving chain - see
      the documentation of vect_permute_store_chain()).
 
      In case of both multiple types and interleaving, above vector stores and
-     permutation stmts are created for every copy. The result vector stmts are
+     permutation stmts are created for every copy.  The result vector stmts are
      put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
-     STMT_VINFO_RELATED_STMT for the next copies.     
+     STMT_VINFO_RELATED_STMT for the next copies.
   */
 
   prev_stmt_info = NULL;
@@ -3098,52 +3952,53 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
           if (slp)
             {
              /* Get vectorized arguments for SLP_NODE.  */
-              vect_get_slp_defs (slp_node, &vec_oprnds, NULL);
+              vect_get_slp_defs (NULL_TREE, NULL_TREE, slp_node, &vec_oprnds,
+                                 NULL, -1);
 
               vec_oprnd = VEC_index (tree, vec_oprnds, 0);
             }
           else
             {
-             /* For interleaved stores we collect vectorized defs for all the 
-                stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then 
-                used as an input to vect_permute_store_chain(), and OPRNDS as 
+             /* For interleaved stores we collect vectorized defs for all the
+                stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
+                used as an input to vect_permute_store_chain(), and OPRNDS as
                 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
 
                 If the store is not strided, GROUP_SIZE is 1, and DR_CHAIN and
                 OPRNDS are of size 1.  */
-             next_stmt = first_stmt;     
+             next_stmt = first_stmt;
              for (i = 0; i < group_size; i++)
                {
-                 /* Since gaps are not supported for interleaved stores, 
-                    GROUP_SIZE is the exact number of stmts in the chain. 
-                    Therefore, NEXT_STMT can't be NULL_TREE.  In case that 
-                    there is no interleaving, GROUP_SIZE is 1, and only one 
+                 /* Since gaps are not supported for interleaved stores,
+                    GROUP_SIZE is the exact number of stmts in the chain.
+                    Therefore, NEXT_STMT can't be NULL_TREE.  In case that
+                    there is no interleaving, GROUP_SIZE is 1, and only one
                     iteration of the loop will be executed.  */
                  gcc_assert (next_stmt
                              && gimple_assign_single_p (next_stmt));
                  op = gimple_assign_rhs1 (next_stmt);
 
-                 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt, 
+                 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt,
                                                            NULL);
-                 VEC_quick_push(tree, dr_chain, vec_oprnd); 
-                 VEC_quick_push(tree, oprnds, vec_oprnd); 
-                 next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
+                 VEC_quick_push(tree, dr_chain, vec_oprnd);
+                 VEC_quick_push(tree, oprnds, vec_oprnd);
+                 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
                }
            }
 
          /* We should have catched mismatched types earlier.  */
          gcc_assert (useless_type_conversion_p (vectype,
                                                 TREE_TYPE (vec_oprnd)));
-         dataref_ptr = vect_create_data_ref_ptr (first_stmt, NULL, NULL_TREE, 
-                                                 &dummy, &ptr_incr, false, 
-                                                 &inv_p);
+         dataref_ptr = vect_create_data_ref_ptr (first_stmt, aggr_type, NULL,
+                                                 NULL_TREE, &dummy, gsi,
+                                                 &ptr_incr, false, &inv_p);
          gcc_assert (bb_vinfo || !inv_p);
        }
-      else 
+      else
        {
-         /* For interleaved stores we created vectorized defs for all the 
-            defs stored in OPRNDS in the previous iteration (previous copy). 
-            DR_CHAIN is then used as an input to vect_permute_store_chain(), 
+         /* For interleaved stores we created vectorized defs for all the
+            defs stored in OPRNDS in the previous iteration (previous copy).
+            DR_CHAIN is then used as an input to vect_permute_store_chain(),
             and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
             next copy.
             If the store is not strided, GROUP_SIZE is 1, and DR_CHAIN and
@@ -3151,88 +4006,184 @@ vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
          for (i = 0; i < group_size; i++)
            {
              op = VEC_index (tree, oprnds, i);
-             vect_is_simple_use (op, loop_vinfo, bb_vinfo, &def_stmt, &def, 
+             vect_is_simple_use (op, loop_vinfo, bb_vinfo, &def_stmt, &def,
                                  &dt);
-             vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op); 
+             vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
              VEC_replace(tree, dr_chain, i, vec_oprnd);
              VEC_replace(tree, oprnds, i, vec_oprnd);
            }
-         dataref_ptr = 
-               bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt, NULL_TREE);
+         dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
+                                        TYPE_SIZE_UNIT (aggr_type));
        }
 
-      if (strided_store)
+      if (store_lanes_p)
        {
-         result_chain = VEC_alloc (tree, heap, group_size);     
-         /* Permute.  */
-         if (!vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
-                                        &result_chain))
-           return false;
+         tree vec_array;
+
+         /* Combine all the vectors into an array.  */
+         vec_array = create_vector_array (vectype, vec_num);
+         for (i = 0; i < vec_num; i++)
+           {
+             vec_oprnd = VEC_index (tree, dr_chain, i);
+             write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
+           }
+
+         /* Emit:
+              MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY).  */
+         data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
+         new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
+         gimple_call_set_lhs (new_stmt, data_ref);
+         vect_finish_stmt_generation (stmt, new_stmt, gsi);
+         mark_symbols_for_renaming (new_stmt);
        }
+      else
+       {
+         new_stmt = NULL;
+         if (strided_store)
+           {
+             result_chain = VEC_alloc (tree, heap, group_size);
+             /* Permute.  */
+             vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
+                                       &result_chain);
+           }
+
+         next_stmt = first_stmt;
+         for (i = 0; i < vec_num; i++)
+           {
+             struct ptr_info_def *pi;
+
+             if (i > 0)
+               /* Bump the vector pointer.  */
+               dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
+                                              stmt, NULL_TREE);
+
+             if (slp)
+               vec_oprnd = VEC_index (tree, vec_oprnds, i);
+             else if (strided_store)
+               /* For strided stores vectorized defs are interleaved in
+                  vect_permute_store_chain().  */
+               vec_oprnd = VEC_index (tree, result_chain, i);
+
+             data_ref = build2 (MEM_REF, TREE_TYPE (vec_oprnd), dataref_ptr,
+                                build_int_cst (reference_alias_ptr_type
+                                               (DR_REF (first_dr)), 0));
+             pi = get_ptr_info (dataref_ptr);
+             pi->align = TYPE_ALIGN_UNIT (vectype);
+             if (aligned_access_p (first_dr))
+               pi->misalign = 0;
+             else if (DR_MISALIGNMENT (first_dr) == -1)
+               {
+                 TREE_TYPE (data_ref)
+                   = build_aligned_type (TREE_TYPE (data_ref),
+                                         TYPE_ALIGN (elem_type));
+                 pi->align = TYPE_ALIGN_UNIT (elem_type);
+                 pi->misalign = 0;
+               }
+             else
+               {
+                 TREE_TYPE (data_ref)
+                   = build_aligned_type (TREE_TYPE (data_ref),
+                                         TYPE_ALIGN (elem_type));
+                 pi->misalign = DR_MISALIGNMENT (first_dr);
+               }
+
+             /* Arguments are ready.  Create the new vector stmt.  */
+             new_stmt = gimple_build_assign (data_ref, vec_oprnd);
+             vect_finish_stmt_generation (stmt, new_stmt, gsi);
+             mark_symbols_for_renaming (new_stmt);
+
+             if (slp)
+               continue;
 
-      next_stmt = first_stmt;
-      for (i = 0; i < vec_num; i++)
+             next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
+             if (!next_stmt)
+               break;
+           }
+       }
+      if (!slp)
        {
-         if (i > 0)
-           /* Bump the vector pointer.  */
-           dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
-                                          NULL_TREE);
-
-         if (slp)
-           vec_oprnd = VEC_index (tree, vec_oprnds, i);
-         else if (strided_store)
-           /* For strided stores vectorized defs are interleaved in 
-              vect_permute_store_chain().  */
-           vec_oprnd = VEC_index (tree, result_chain, i);
-
-          if (aligned_access_p (first_dr))
-            data_ref = build_fold_indirect_ref (dataref_ptr);
-          else
-          {
-            int mis = DR_MISALIGNMENT (first_dr);
-            tree tmis = (mis == -1 ? size_zero_node : size_int (mis));
-            tmis = size_binop (MULT_EXPR, tmis, size_int (BITS_PER_UNIT));
-            data_ref = build2 (MISALIGNED_INDIRECT_REF, vectype, dataref_ptr, tmis);
-           }
+         if (j == 0)
+           STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
+         else
+           STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
+         prev_stmt_info = vinfo_for_stmt (new_stmt);
+       }
+    }
 
-         /* If accesses through a pointer to vectype do not alias the original
-            memory reference we have a problem.  This should never happen.  */
-         gcc_assert (alias_sets_conflict_p (get_alias_set (data_ref),
-                     get_alias_set (gimple_assign_lhs (stmt))));
+  VEC_free (tree, heap, dr_chain);
+  VEC_free (tree, heap, oprnds);
+  if (result_chain)
+    VEC_free (tree, heap, result_chain);
+  if (vec_oprnds)
+    VEC_free (tree, heap, vec_oprnds);
 
-         /* Arguments are ready. Create the new vector stmt.  */
-         new_stmt = gimple_build_assign (data_ref, vec_oprnd);
-         vect_finish_stmt_generation (stmt, new_stmt, gsi);
-         mark_symbols_for_renaming (new_stmt);
+  return true;
+}
+
+/* Given a vector type VECTYPE returns a builtin DECL to be used
+   for vector permutation and returns the mask that implements
+   reversal of the vector elements.  If that is impossible to do,
+   returns NULL.  */
+
+static tree
+perm_mask_for_reverse (tree vectype)
+{
+  tree mask_elt_type, mask_type, mask_vec;
+  int i, nunits;
+  unsigned char *sel;
+
+  nunits = TYPE_VECTOR_SUBPARTS (vectype);
+  sel = XALLOCAVEC (unsigned char, nunits);
+
+  for (i = 0; i < nunits; ++i)
+    sel[i] = nunits - 1 - i;
+
+  if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
+    return NULL;
+
+  mask_elt_type
+    = lang_hooks.types.type_for_size
+    (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (vectype))), 1);
+  mask_type = get_vectype_for_scalar_type (mask_elt_type);
+
+  mask_vec = NULL;
+  for (i = 0; i < nunits; i++)
+    mask_vec = tree_cons (NULL, build_int_cst (mask_elt_type, i), mask_vec);
+  mask_vec = build_vector (mask_type, mask_vec);
+
+  return mask_vec;
+}
+
+/* Given a vector variable X, that was generated for the scalar LHS of
+   STMT, generate instructions to reverse the vector elements of X,
+   insert them a *GSI and return the permuted vector variable.  */
 
-          if (slp)
-            continue;
-         
-          if (j == 0)
-            STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt =  new_stmt;
-         else
-           STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
+static tree
+reverse_vec_elements (tree x, gimple stmt, gimple_stmt_iterator *gsi)
+{
+  tree vectype = TREE_TYPE (x);
+  tree mask_vec, perm_dest, data_ref;
+  gimple perm_stmt;
 
-         prev_stmt_info = vinfo_for_stmt (new_stmt);
-         next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
-         if (!next_stmt)
-           break;
-       }
-    }
+  mask_vec = perm_mask_for_reverse (vectype);
 
-  VEC_free (tree, heap, dr_chain);  
-  VEC_free (tree, heap, oprnds);  
-  if (result_chain)
-    VEC_free (tree, heap, result_chain);  
+  perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
 
-  return true;
+  /* Generate the permute statement.  */
+  perm_stmt = gimple_build_assign_with_ops3 (VEC_PERM_EXPR, perm_dest,
+                                            x, x, mask_vec);
+  data_ref = make_ssa_name (perm_dest, perm_stmt);
+  gimple_set_lhs (perm_stmt, data_ref);
+  vect_finish_stmt_generation (stmt, perm_stmt, gsi);
+
+  return data_ref;
 }
 
 /* vectorizable_load.
 
-   Check if STMT reads a non scalar data-ref (array/pointer/structure) that 
-   can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
+   Check if STMT reads a non scalar data-ref (array/pointer/structure) that
+   can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
 
@@ -3244,15 +4195,16 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   tree vec_dest = NULL;
   tree data_ref = NULL;
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
-  stmt_vec_info prev_stmt_info; 
+  stmt_vec_info prev_stmt_info;
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
   struct loop *loop = NULL;
   struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
   bool nested_in_vect_loop = false;
   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
+  tree elem_type;
   tree new_temp;
-  int mode;
+  enum machine_mode mode;
   gimple new_stmt = NULL;
   tree dummy;
   enum dr_alignment_support alignment_support_scheme;
@@ -3267,9 +4219,10 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   gimple phi = NULL;
   VEC(tree,heap) *dr_chain = NULL;
   bool strided_load = false;
+  bool load_lanes_p = false;
   gimple first_stmt;
-  tree scalar_type;
   bool inv_p;
+  bool negative;
   bool compute_in_loop = false;
   struct loop *at_loop;
   int vec_num;
@@ -3278,6 +4231,7 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   enum tree_code code;
   bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
   int vf;
+  tree aggr_type;
 
   if (loop_vinfo)
     {
@@ -3286,13 +4240,12 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
       vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
     }
   else
-    /* FORNOW: multiple types are not supported in basic block SLP.  */
-    vf = nunits;
+    vf = 1;
 
   /* Multiple types in SLP are handled by creating the appropriate number of
-     vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
+     vectorized stmts for each SLP node.  Hence, NCOPIES is always 1 in
      case of SLP.  */
-  if (slp)
+  if (slp || PURE_SLP_STMT (stmt_info))
     ncopies = 1;
   else
     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
@@ -3326,30 +4279,31 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
       && code != INDIRECT_REF
       && code != COMPONENT_REF
       && code != IMAGPART_EXPR
-      && code != REALPART_EXPR)
+      && code != REALPART_EXPR
+      && code != MEM_REF
+      && TREE_CODE_CLASS (code) != tcc_declaration)
     return false;
 
   if (!STMT_VINFO_DATA_REF (stmt_info))
     return false;
 
-  scalar_type = TREE_TYPE (DR_REF (dr));
-  mode = (int) TYPE_MODE (vectype);
-
-  /* FORNOW. In some cases can vectorize even if data-type not supported
-    (e.g. - data copies).  */
-  if (optab_handler (mov_optab, mode)->insn_code == CODE_FOR_nothing)
+  negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
+  if (negative && ncopies > 1)
     {
       if (vect_print_dump_info (REPORT_DETAILS))
-       fprintf (vect_dump, "Aligned load, but unsupported type.");
+        fprintf (vect_dump, "multiple types with negative step.");
       return false;
     }
 
-  /* The vector component type needs to be trivially convertible to the
-     scalar lhs.  This should always be the case.  */
-  if (!useless_type_conversion_p (TREE_TYPE (scalar_dest), TREE_TYPE (vectype)))
-    {      
+  elem_type = TREE_TYPE (vectype);
+  mode = TYPE_MODE (vectype);
+
+  /* FORNOW. In some cases can vectorize even if data-type not supported
+    (e.g. - data copies).  */
+  if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
+    {
       if (vect_print_dump_info (REPORT_DETAILS))
-        fprintf (vect_dump, "???  operands of different types");
+       fprintf (vect_dump, "Aligned load, but unsupported type.");
       return false;
     }
 
@@ -3360,27 +4314,56 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
       /* FORNOW */
       gcc_assert (! nested_in_vect_loop);
 
-      /* Check if interleaving is supported.  */
-      if (!vect_strided_load_supported (vectype)
-         && !PURE_SLP_STMT (stmt_info) && !slp)
-       return false;
+      first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
+      if (!slp && !PURE_SLP_STMT (stmt_info))
+       {
+         group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
+         if (vect_load_lanes_supported (vectype, group_size))
+           load_lanes_p = true;
+         else if (!vect_strided_load_supported (vectype, group_size))
+           return false;
+       }
+    }
+
+  if (negative)
+    {
+      gcc_assert (!strided_load);
+      alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
+      if (alignment_support_scheme != dr_aligned
+         && alignment_support_scheme != dr_unaligned_supported)
+       {
+         if (vect_print_dump_info (REPORT_DETAILS))
+           fprintf (vect_dump, "negative step but alignment required.");
+         return false;
+       }
+      if (!perm_mask_for_reverse (vectype))
+       {
+         if (vect_print_dump_info (REPORT_DETAILS))
+           fprintf (vect_dump, "negative step and reversing not supported.");
+         return false;
+       }
     }
 
   if (!vec_stmt) /* transformation not required.  */
     {
       STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
-      vect_model_load_cost (stmt_info, ncopies, NULL);
+      vect_model_load_cost (stmt_info, ncopies, load_lanes_p, NULL);
       return true;
     }
 
   if (vect_print_dump_info (REPORT_DETAILS))
-    fprintf (vect_dump, "transform load.");
+    fprintf (vect_dump, "transform load. ncopies = %d", ncopies);
 
   /** Transform.  **/
 
   if (strided_load)
     {
-      first_stmt = DR_GROUP_FIRST_DR (stmt_info);
+      first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
+      if (slp
+          && !SLP_INSTANCE_LOAD_PERMUTATION (slp_node_instance)
+         && first_stmt != VEC_index (gimple, SLP_TREE_SCALAR_STMTS (slp_node), 0))
+        first_stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (slp_node), 0);
+
       /* Check if the chain of loads is already vectorized.  */
       if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt)))
        {
@@ -3388,7 +4371,7 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
          return true;
        }
       first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
-      group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
+      group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
 
       /* VEC_NUM is the number of vect stmts to be created for this group.  */
       if (slp)
@@ -3400,8 +4383,6 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
        }
       else
        vec_num = group_size;
-
-      dr_chain = VEC_alloc (tree, heap, vec_num);
     }
   else
     {
@@ -3410,19 +4391,24 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
       group_size = vec_num = 1;
     }
 
-  alignment_support_scheme = vect_supportable_dr_alignment (first_dr);
+  alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
   gcc_assert (alignment_support_scheme);
+  /* Targets with load-lane instructions must not require explicit
+     realignment.  */
+  gcc_assert (!load_lanes_p
+             || alignment_support_scheme == dr_aligned
+             || alignment_support_scheme == dr_unaligned_supported);
 
   /* In case the vectorization factor (VF) is bigger than the number
      of elements that we can fit in a vectype (nunits), we have to generate
      more than one vector stmt - i.e - we need to "unroll" the
-     vector stmt by a factor VF/nunits. In doing so, we record a pointer
+     vector stmt by a factor VF/nunits.  In doing so, we record a pointer
      from one copy of the vector stmt to the next, in the field
-     STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
+     STMT_VINFO_RELATED_STMT.  This is necessary in order to allow following
      stages to find the correct vector defs to be used when vectorizing
-     stmts that use the defs of the current stmt. The example below illustrates
-     the vectorization process when VF=16 and nunits=4 (i.e - we need to create
-     4 vectorized stmts):
+     stmts that use the defs of the current stmt.  The example below
+     illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
+     need to create 4 vectorized stmts):
 
      before vectorization:
                                 RELATED_STMT    VEC_STMT
@@ -3434,7 +4420,7 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
         pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
         Next, we create the vector stmt VS1_1, and record a pointer to
         it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
-        Similarly, for VS1_2 and VS1_3. This is the resulting chain of
+        Similarly, for VS1_2 and VS1_3.  This is the resulting chain of
         stmts and pointers:
                                 RELATED_STMT    VEC_STMT
         VS1_0:  vx0 = memref0   VS1_1           -
@@ -3444,8 +4430,8 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
         S1:     x = load        -               VS1_0
         S2:     z = x + 1       -               -
 
-     See in documentation in vect_get_vec_def_for_stmt_copy for how the 
-     information we recorded in RELATED_STMT field is used to vectorize 
+     See in documentation in vect_get_vec_def_for_stmt_copy for how the
+     information we recorded in RELATED_STMT field is used to vectorize
      stmt S2.  */
 
   /* In case of interleaving (non-unit strided access):
@@ -3455,7 +4441,7 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
      S3:  x1 = &base + 1
      S4:  x3 = &base + 3
 
-     Vectorized loads are created in the order of memory accesses 
+     Vectorized loads are created in the order of memory accesses
      starting from the access of the first stmt of the chain:
 
      VS1: vx0 = &base
@@ -3476,10 +4462,10 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
      The generation of permutation stmts and recording them in
      STMT_VINFO_VEC_STMT is done in vect_transform_strided_load().
 
-     In case of both multiple types and interleaving, the vector loads and 
-     permutation stmts above are created for every copy. The result vector stmts
-     are put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
-     STMT_VINFO_RELATED_STMT for the next copies.  */
+     In case of both multiple types and interleaving, the vector loads and
+     permutation stmts above are created for every copy.  The result vector
+     stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
+     corresponding STMT_VINFO_RELATED_STMT for the next copies.  */
 
   /* If the data reference is aligned (dr_aligned) or potentially unaligned
      on a target that supports unaligned accesses (dr_unaligned_supported)
@@ -3493,7 +4479,7 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
          }
 
      Otherwise, the data reference is potentially unaligned on a target that
-     does not support unaligned accesses (dr_explicit_realign_optimized) - 
+     does not support unaligned accesses (dr_explicit_realign_optimized) -
      then generate the following code, in which the data in each iteration is
      obtained by two vector loads, one from the previous iteration, and one
      from the current iteration:
@@ -3512,7 +4498,7 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
 
   /* If the misalignment remains the same throughout the execution of the
      loop, we can create the init_addr and permutation mask at the loop
-     preheader. Otherwise, it needs to be created inside the loop.
+     preheader.  Otherwise, it needs to be created inside the loop.
      This can only occur when vectorizing memory accesses in the inner-loop
      nested within an outer-loop that is being vectorized.  */
 
@@ -3540,155 +4526,241 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
   else
     at_loop = loop;
 
+  if (negative)
+    offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
+
+  if (load_lanes_p)
+    aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
+  else
+    aggr_type = vectype;
+
   prev_stmt_info = NULL;
   for (j = 0; j < ncopies; j++)
-    { 
-      /* 1. Create the vector pointer update chain.  */
+    {
+      /* 1. Create the vector or array pointer update chain.  */
       if (j == 0)
-        dataref_ptr = vect_create_data_ref_ptr (first_stmt,
-                                               at_loop, offset, 
-                                               &dummy, &ptr_incr, false, 
-                                               &inv_p);
+        dataref_ptr = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
+                                               offset, &dummy, gsi,
+                                               &ptr_incr, false, &inv_p);
       else
-        dataref_ptr = 
-               bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt, NULL_TREE);
+        dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
+                                      TYPE_SIZE_UNIT (aggr_type));
 
-      for (i = 0; i < vec_num; i++)
+      if (strided_load || slp_perm)
+       dr_chain = VEC_alloc (tree, heap, vec_num);
+
+      if (load_lanes_p)
        {
-         if (i > 0)
-           dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
-                                          NULL_TREE);
+         tree vec_array;
 
-         /* 2. Create the vector-load in the loop.  */
-         switch (alignment_support_scheme)
-           {
-           case dr_aligned:
-             gcc_assert (aligned_access_p (first_dr));
-             data_ref = build_fold_indirect_ref (dataref_ptr);
-             break;
-           case dr_unaligned_supported:
-             {
-               int mis = DR_MISALIGNMENT (first_dr);
-               tree tmis = (mis == -1 ? size_zero_node : size_int (mis));
+         vec_array = create_vector_array (vectype, vec_num);
 
-               tmis = size_binop (MULT_EXPR, tmis, size_int(BITS_PER_UNIT));
-               data_ref =
-                 build2 (MISALIGNED_INDIRECT_REF, vectype, dataref_ptr, tmis);
-               break;
-             }
-           case dr_explicit_realign:
-             {
-               tree ptr, bump;
-               tree vs_minus_1 = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
-
-               if (compute_in_loop)
-                 msq = vect_setup_realignment (first_stmt, gsi,
-                                               &realignment_token,
-                                               dr_explicit_realign, 
-                                               dataref_ptr, NULL);
-
-               data_ref = build1 (ALIGN_INDIRECT_REF, vectype, dataref_ptr);
-               vec_dest = vect_create_destination_var (scalar_dest, vectype);
-               new_stmt = gimple_build_assign (vec_dest, data_ref);
-               new_temp = make_ssa_name (vec_dest, new_stmt);
-               gimple_assign_set_lhs (new_stmt, new_temp);
-               gimple_set_vdef (new_stmt, gimple_vdef (stmt));
-               gimple_set_vuse (new_stmt, gimple_vuse (stmt));
-               vect_finish_stmt_generation (stmt, new_stmt, gsi);
-               msq = new_temp;
-
-               bump = size_binop (MULT_EXPR, vs_minus_1,
-                                  TYPE_SIZE_UNIT (scalar_type));
-               ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
-               data_ref = build1 (ALIGN_INDIRECT_REF, vectype, ptr);
-               break;
-             }
-           case dr_explicit_realign_optimized:
-             data_ref = build1 (ALIGN_INDIRECT_REF, vectype, dataref_ptr);
-             break;
-           default:
-             gcc_unreachable ();
-           }
-         /* If accesses through a pointer to vectype do not alias the original
-            memory reference we have a problem.  This should never happen. */
-         gcc_assert (alias_sets_conflict_p (get_alias_set (data_ref),
-                     get_alias_set (gimple_assign_rhs1 (stmt))));
-         vec_dest = vect_create_destination_var (scalar_dest, vectype);
-         new_stmt = gimple_build_assign (vec_dest, data_ref);
-         new_temp = make_ssa_name (vec_dest, new_stmt);
-         gimple_assign_set_lhs (new_stmt, new_temp);
+         /* Emit:
+              VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]).  */
+         data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
+         new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
+         gimple_call_set_lhs (new_stmt, vec_array);
          vect_finish_stmt_generation (stmt, new_stmt, gsi);
          mark_symbols_for_renaming (new_stmt);
 
-         /* 3. Handle explicit realignment if necessary/supported. Create in
-               loop: vec_dest = realign_load (msq, lsq, realignment_token)  */
-         if (alignment_support_scheme == dr_explicit_realign_optimized
-             || alignment_support_scheme == dr_explicit_realign)
+         /* Extract each vector into an SSA_NAME.  */
+         for (i = 0; i < vec_num; i++)
+           {
+             new_temp = read_vector_array (stmt, gsi, scalar_dest,
+                                           vec_array, i);
+             VEC_quick_push (tree, dr_chain, new_temp);
+           }
+
+         /* Record the mapping between SSA_NAMEs and statements.  */
+         vect_record_strided_load_vectors (stmt, dr_chain);
+       }
+      else
+       {
+         for (i = 0; i < vec_num; i++)
            {
-             tree tmp;
+             if (i > 0)
+               dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
+                                              stmt, NULL_TREE);
 
-             lsq = gimple_assign_lhs (new_stmt);
-             if (!realignment_token)
-               realignment_token = dataref_ptr;
+             /* 2. Create the vector-load in the loop.  */
+             switch (alignment_support_scheme)
+               {
+               case dr_aligned:
+               case dr_unaligned_supported:
+                 {
+                   struct ptr_info_def *pi;
+                   data_ref
+                     = build2 (MEM_REF, vectype, dataref_ptr,
+                               build_int_cst (reference_alias_ptr_type
+                                              (DR_REF (first_dr)), 0));
+                   pi = get_ptr_info (dataref_ptr);
+                   pi->align = TYPE_ALIGN_UNIT (vectype);
+                   if (alignment_support_scheme == dr_aligned)
+                     {
+                       gcc_assert (aligned_access_p (first_dr));
+                       pi->misalign = 0;
+                     }
+                   else if (DR_MISALIGNMENT (first_dr) == -1)
+                     {
+                       TREE_TYPE (data_ref)
+                         = build_aligned_type (TREE_TYPE (data_ref),
+                                               TYPE_ALIGN (elem_type));
+                       pi->align = TYPE_ALIGN_UNIT (elem_type);
+                       pi->misalign = 0;
+                     }
+                   else
+                     {
+                       TREE_TYPE (data_ref)
+                         = build_aligned_type (TREE_TYPE (data_ref),
+                                               TYPE_ALIGN (elem_type));
+                       pi->misalign = DR_MISALIGNMENT (first_dr);
+                     }
+                   break;
+                 }
+               case dr_explicit_realign:
+                 {
+                   tree ptr, bump;
+                   tree vs_minus_1;
+
+                   vs_minus_1 = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
+
+                   if (compute_in_loop)
+                     msq = vect_setup_realignment (first_stmt, gsi,
+                                                   &realignment_token,
+                                                   dr_explicit_realign,
+                                                   dataref_ptr, NULL);
+
+                   new_stmt = gimple_build_assign_with_ops
+                                (BIT_AND_EXPR, NULL_TREE, dataref_ptr,
+                                 build_int_cst
+                                 (TREE_TYPE (dataref_ptr),
+                                  -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
+                   ptr = make_ssa_name (SSA_NAME_VAR (dataref_ptr), new_stmt);
+                   gimple_assign_set_lhs (new_stmt, ptr);
+                   vect_finish_stmt_generation (stmt, new_stmt, gsi);
+                   data_ref
+                     = build2 (MEM_REF, vectype, ptr,
+                               build_int_cst (reference_alias_ptr_type
+                                                (DR_REF (first_dr)), 0));
+                   vec_dest = vect_create_destination_var (scalar_dest,
+                                                           vectype);
+                   new_stmt = gimple_build_assign (vec_dest, data_ref);
+                   new_temp = make_ssa_name (vec_dest, new_stmt);
+                   gimple_assign_set_lhs (new_stmt, new_temp);
+                   gimple_set_vdef (new_stmt, gimple_vdef (stmt));
+                   gimple_set_vuse (new_stmt, gimple_vuse (stmt));
+                   vect_finish_stmt_generation (stmt, new_stmt, gsi);
+                   msq = new_temp;
+
+                   bump = size_binop (MULT_EXPR, vs_minus_1,
+                                      TYPE_SIZE_UNIT (elem_type));
+                   ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
+                   new_stmt = gimple_build_assign_with_ops
+                                (BIT_AND_EXPR, NULL_TREE, ptr,
+                                 build_int_cst
+                                 (TREE_TYPE (ptr),
+                                  -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
+                   ptr = make_ssa_name (SSA_NAME_VAR (dataref_ptr), new_stmt);
+                   gimple_assign_set_lhs (new_stmt, ptr);
+                   vect_finish_stmt_generation (stmt, new_stmt, gsi);
+                   data_ref
+                     = build2 (MEM_REF, vectype, ptr,
+                               build_int_cst (reference_alias_ptr_type
+                                                (DR_REF (first_dr)), 0));
+                   break;
+                 }
+               case dr_explicit_realign_optimized:
+                 new_stmt = gimple_build_assign_with_ops
+                              (BIT_AND_EXPR, NULL_TREE, dataref_ptr,
+                               build_int_cst
+                                 (TREE_TYPE (dataref_ptr),
+                                  -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
+                 new_temp = make_ssa_name (SSA_NAME_VAR (dataref_ptr),
+                                           new_stmt);
+                 gimple_assign_set_lhs (new_stmt, new_temp);
+                 vect_finish_stmt_generation (stmt, new_stmt, gsi);
+                 data_ref
+                   = build2 (MEM_REF, vectype, new_temp,
+                             build_int_cst (reference_alias_ptr_type
+                                              (DR_REF (first_dr)), 0));
+                 break;
+               default:
+                 gcc_unreachable ();
+               }
              vec_dest = vect_create_destination_var (scalar_dest, vectype);
-             tmp = build3 (REALIGN_LOAD_EXPR, vectype, msq, lsq,
-                           realignment_token);
-             new_stmt = gimple_build_assign (vec_dest, tmp);
+             new_stmt = gimple_build_assign (vec_dest, data_ref);
              new_temp = make_ssa_name (vec_dest, new_stmt);
              gimple_assign_set_lhs (new_stmt, new_temp);
              vect_finish_stmt_generation (stmt, new_stmt, gsi);
+             mark_symbols_for_renaming (new_stmt);
 
-             if (alignment_support_scheme == dr_explicit_realign_optimized)
+             /* 3. Handle explicit realignment if necessary/supported.
+                Create in loop:
+                  vec_dest = realign_load (msq, lsq, realignment_token)  */
+             if (alignment_support_scheme == dr_explicit_realign_optimized
+                 || alignment_support_scheme == dr_explicit_realign)
                {
-                 gcc_assert (phi);
-                 if (i == vec_num - 1 && j == ncopies - 1)
-                   add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
-                                UNKNOWN_LOCATION);
-                 msq = lsq;
+                 lsq = gimple_assign_lhs (new_stmt);
+                 if (!realignment_token)
+                   realignment_token = dataref_ptr;
+                 vec_dest = vect_create_destination_var (scalar_dest, vectype);
+                 new_stmt
+                   = gimple_build_assign_with_ops3 (REALIGN_LOAD_EXPR,
+                                                    vec_dest, msq, lsq,
+                                                    realignment_token);
+                 new_temp = make_ssa_name (vec_dest, new_stmt);
+                 gimple_assign_set_lhs (new_stmt, new_temp);
+                 vect_finish_stmt_generation (stmt, new_stmt, gsi);
+
+                 if (alignment_support_scheme == dr_explicit_realign_optimized)
+                   {
+                     gcc_assert (phi);
+                     if (i == vec_num - 1 && j == ncopies - 1)
+                       add_phi_arg (phi, lsq,
+                                    loop_latch_edge (containing_loop),
+                                    UNKNOWN_LOCATION);
+                     msq = lsq;
+                   }
                }
-           }
 
-         /* 4. Handle invariant-load.  */
-         if (inv_p && !bb_vinfo)
-           {
-             gcc_assert (!strided_load);
-             gcc_assert (nested_in_vect_loop_p (loop, stmt));
-             if (j == 0)
+             /* 4. Handle invariant-load.  */
+             if (inv_p && !bb_vinfo)
                {
-                 int k;
-                 tree t = NULL_TREE;
-                 tree vec_inv, bitpos, bitsize = TYPE_SIZE (scalar_type);
-
-                 /* CHECKME: bitpos depends on endianess?  */
-                 bitpos = bitsize_zero_node;
-                 vec_inv = build3 (BIT_FIELD_REF, scalar_type, new_temp, 
-                                   bitsize, bitpos);
-                 vec_dest = 
-                       vect_create_destination_var (scalar_dest, NULL_TREE);
-                 new_stmt = gimple_build_assign (vec_dest, vec_inv);
-                  new_temp = make_ssa_name (vec_dest, new_stmt);
-                 gimple_assign_set_lhs (new_stmt, new_temp);
-                 vect_finish_stmt_generation (stmt, new_stmt, gsi);
+                 tree tem, vec_inv;
+                 gimple_stmt_iterator gsi2 = *gsi;
+                 gcc_assert (!strided_load);
+                 gsi_next (&gsi2);
+                 tem = scalar_dest;
+                 if (!useless_type_conversion_p (TREE_TYPE (vectype),
+                                                 TREE_TYPE (tem)))
+                   {
+                     tem = fold_convert (TREE_TYPE (vectype), tem);
+                     tem = force_gimple_operand_gsi (&gsi2, tem, true,
+                                                     NULL_TREE, true,
+                                                     GSI_SAME_STMT);
+                   }
+                 vec_inv = build_vector_from_val (vectype, tem);
+                 new_temp = vect_init_vector (stmt, vec_inv,
+                                              vectype, &gsi2);
+                 new_stmt = SSA_NAME_DEF_STMT (new_temp);
+               }
 
-                 for (k = nunits - 1; k >= 0; --k)
-                   t = tree_cons (NULL_TREE, new_temp, t);
-                 /* FIXME: use build_constructor directly.  */
-                 vec_inv = build_constructor_from_list (vectype, t);
-                 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
+             if (negative)
+               {
+                 new_temp = reverse_vec_elements (new_temp, stmt, gsi);
                  new_stmt = SSA_NAME_DEF_STMT (new_temp);
                }
-             else
-               gcc_unreachable (); /* FORNOW. */
-           }
 
-         /* Collect vector loads and later create their permutation in
-            vect_transform_strided_load ().  */
-          if (strided_load || slp_perm)
-            VEC_quick_push (tree, dr_chain, new_temp);
+             /* Collect vector loads and later create their permutation in
+                vect_transform_strided_load ().  */
+             if (strided_load || slp_perm)
+               VEC_quick_push (tree, dr_chain, new_temp);
 
-         /* Store vector loads in the corresponding SLP_NODE.  */
-         if (slp && !slp_perm)
-           VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
+             /* Store vector loads in the corresponding SLP_NODE.  */
+             if (slp && !slp_perm)
+               VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node),
+                               new_stmt);
+           }
        }
 
       if (slp && !slp_perm)
@@ -3707,12 +4779,9 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
         {
           if (strided_load)
            {
-             if (!vect_transform_strided_load (stmt, dr_chain, group_size, gsi))
-               return false;     
-
+             if (!load_lanes_p)
+               vect_transform_strided_load (stmt, dr_chain, group_size, gsi);
              *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
-              VEC_free (tree, heap, dr_chain);
-             dr_chain = VEC_alloc (tree, heap, group_size);
            }
           else
            {
@@ -3723,29 +4792,32 @@ vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
              prev_stmt_info = vinfo_for_stmt (new_stmt);
            }
         }
+      if (dr_chain)
+       VEC_free (tree, heap, dr_chain);
     }
 
-  if (dr_chain)
-    VEC_free (tree, heap, dr_chain);
-
   return true;
 }
 
 /* Function vect_is_simple_cond.
-  
+
    Input:
    LOOP - the loop that is being vectorized.
    COND - Condition that is checked for simple use.
 
+   Output:
+   *COMP_VECTYPE - the vector type for the comparison.
+
    Returns whether a COND can be vectorized.  Checks whether
    condition operands are supportable using vec_is_simple_use.  */
 
 static bool
-vect_is_simple_cond (tree cond, loop_vec_info loop_vinfo)
+vect_is_simple_cond (tree cond, loop_vec_info loop_vinfo, tree *comp_vectype)
 {
   tree lhs, rhs;
   tree def;
   enum vect_def_type dt;
+  tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
 
   if (!COMPARISON_CLASS_P (cond))
     return false;
@@ -3756,8 +4828,8 @@ vect_is_simple_cond (tree cond, loop_vec_info loop_vinfo)
   if (TREE_CODE (lhs) == SSA_NAME)
     {
       gimple lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
-      if (!vect_is_simple_use (lhs, loop_vinfo, NULL, &lhs_def_stmt, &def, 
-                               &dt))
+      if (!vect_is_simple_use_1 (lhs, loop_vinfo, NULL, &lhs_def_stmt, &def,
+                                &dt, &vectype1))
        return false;
     }
   else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
@@ -3767,22 +4839,23 @@ vect_is_simple_cond (tree cond, loop_vec_info loop_vinfo)
   if (TREE_CODE (rhs) == SSA_NAME)
     {
       gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
-      if (!vect_is_simple_use (rhs, loop_vinfo, NULL, &rhs_def_stmt, &def, 
-                               &dt))
+      if (!vect_is_simple_use_1 (rhs, loop_vinfo, NULL, &rhs_def_stmt, &def,
+                                &dt, &vectype2))
        return false;
     }
   else if (TREE_CODE (rhs) != INTEGER_CST  && TREE_CODE (rhs) != REAL_CST
           && TREE_CODE (rhs) != FIXED_CST)
     return false;
 
+  *comp_vectype = vectype1 ? vectype1 : vectype2;
   return true;
 }
 
 /* vectorizable_condition.
 
-   Check if STMT is conditional modify expression that can be vectorized. 
-   If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
-   stmt using VEC_COND_EXPR  to replace it, put it in VEC_STMT, and insert it 
+   Check if STMT is conditional modify expression that can be vectorized.
+   If VEC_STMT is also passed, vectorize the STMT: create a vectorized
+   stmt using VEC_COND_EXPR  to replace it, put it in VEC_STMT, and insert it
    at GSI.
 
    When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
@@ -3797,26 +4870,32 @@ vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
 {
   tree scalar_dest = NULL_TREE;
   tree vec_dest = NULL_TREE;
-  tree op = NULL_TREE;
   tree cond_expr, then_clause, else_clause;
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
-  tree vec_cond_lhs, vec_cond_rhs, vec_then_clause, vec_else_clause;
+  tree comp_vectype;
+  tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
+  tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
   tree vec_compare, vec_cond_expr;
   tree new_temp;
   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
-  enum machine_mode vec_mode;
   tree def;
-  enum vect_def_type dt;
+  enum vect_def_type dt, dts[4];
   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
   int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
   enum tree_code code;
+  stmt_vec_info prev_stmt_info = NULL;
+  int j;
 
   /* FORNOW: unsupported in basic block SLP.  */
   gcc_assert (loop_vinfo);
-    
+
+  /* FORNOW: SLP not supported.  */
+  if (STMT_SLP_TYPE (stmt_info))
+    return false;
+
   gcc_assert (ncopies >= 1);
-  if (ncopies > 1)
+  if (reduc_index && ncopies > 1)
     return false; /* FORNOW */
 
   if (!STMT_VINFO_RELEVANT_P (stmt_info))
@@ -3827,12 +4906,8 @@ vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
            && reduc_def))
     return false;
 
-  /* FORNOW: SLP not supported.  */
-  if (STMT_SLP_TYPE (stmt_info))
-    return false;
-
   /* FORNOW: not yet supported.  */
-  if (STMT_VINFO_LIVE_P (stmt_info)) 
+  if (STMT_VINFO_LIVE_P (stmt_info))
     {
       if (vect_print_dump_info (REPORT_DETAILS))
         fprintf (vect_dump, "value used after loop.");
@@ -3848,28 +4923,22 @@ vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
   if (code != COND_EXPR)
     return false;
 
-  gcc_assert (gimple_assign_single_p (stmt));
-  op = gimple_assign_rhs1 (stmt);
-  cond_expr = TREE_OPERAND (op, 0);
-  then_clause = TREE_OPERAND (op, 1);
-  else_clause = TREE_OPERAND (op, 2);
-
-  if (!vect_is_simple_cond (cond_expr, loop_vinfo))
-    return false;
+  cond_expr = gimple_assign_rhs1 (stmt);
+  then_clause = gimple_assign_rhs2 (stmt);
+  else_clause = gimple_assign_rhs3 (stmt);
 
-  /* We do not handle two different vector types for the condition
-     and the values.  */
-  if (TREE_TYPE (TREE_OPERAND (cond_expr, 0)) != TREE_TYPE (vectype))
+  if (!vect_is_simple_cond (cond_expr, loop_vinfo, &comp_vectype)
+      || !comp_vectype)
     return false;
 
   if (TREE_CODE (then_clause) == SSA_NAME)
     {
       gimple then_def_stmt = SSA_NAME_DEF_STMT (then_clause);
-      if (!vect_is_simple_use (then_clause, loop_vinfo, NULL, 
+      if (!vect_is_simple_use (then_clause, loop_vinfo, NULL,
                               &then_def_stmt, &def, &dt))
        return false;
     }
-  else if (TREE_CODE (then_clause) != INTEGER_CST 
+  else if (TREE_CODE (then_clause) != INTEGER_CST
           && TREE_CODE (then_clause) != REAL_CST
           && TREE_CODE (then_clause) != FIXED_CST)
     return false;
@@ -3881,18 +4950,15 @@ vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
                               &else_def_stmt, &def, &dt))
        return false;
     }
-  else if (TREE_CODE (else_clause) != INTEGER_CST 
+  else if (TREE_CODE (else_clause) != INTEGER_CST
           && TREE_CODE (else_clause) != REAL_CST
           && TREE_CODE (else_clause) != FIXED_CST)
     return false;
 
-
-  vec_mode = TYPE_MODE (vectype);
-
-  if (!vec_stmt) 
+  if (!vec_stmt)
     {
       STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
-      return expand_vec_cond_expr_p (TREE_TYPE (op), vec_mode);
+      return expand_vec_cond_expr_p (vectype, comp_vectype);
     }
 
   /* Transform */
@@ -3902,30 +4968,69 @@ vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
   vec_dest = vect_create_destination_var (scalar_dest, vectype);
 
   /* Handle cond expr.  */
-  vec_cond_lhs = 
-    vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0), stmt, NULL);
-  vec_cond_rhs = 
-    vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1), stmt, NULL);
-  if (reduc_index == 1)
-    vec_then_clause = reduc_def;
-  else
-    vec_then_clause = vect_get_vec_def_for_operand (then_clause, stmt, NULL);
-  if (reduc_index == 2)
-    vec_else_clause = reduc_def;
-  else
-    vec_else_clause = vect_get_vec_def_for_operand (else_clause, stmt, NULL);
-
-  /* Arguments are ready. Create the new vector stmt.  */
-  vec_compare = build2 (TREE_CODE (cond_expr), vectype, 
-                       vec_cond_lhs, vec_cond_rhs);
-  vec_cond_expr = build3 (VEC_COND_EXPR, vectype, 
-                         vec_compare, vec_then_clause, vec_else_clause);
-
-  *vec_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
-  new_temp = make_ssa_name (vec_dest, *vec_stmt);
-  gimple_assign_set_lhs (*vec_stmt, new_temp);
-  vect_finish_stmt_generation (stmt, *vec_stmt, gsi);
-  
+  for (j = 0; j < ncopies; j++)
+    {
+      gimple new_stmt;
+      if (j == 0)
+       {
+         gimple gtemp;
+         vec_cond_lhs =
+             vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
+                                           stmt, NULL);
+         vect_is_simple_use (TREE_OPERAND (cond_expr, 0), loop_vinfo,
+                             NULL, &gtemp, &def, &dts[0]);
+         vec_cond_rhs =
+             vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
+                                           stmt, NULL);
+         vect_is_simple_use (TREE_OPERAND (cond_expr, 1), loop_vinfo,
+                             NULL, &gtemp, &def, &dts[1]);
+         if (reduc_index == 1)
+           vec_then_clause = reduc_def;
+         else
+           {
+             vec_then_clause = vect_get_vec_def_for_operand (then_clause,
+                                                             stmt, NULL);
+             vect_is_simple_use (then_clause, loop_vinfo,
+                                 NULL, &gtemp, &def, &dts[2]);
+           }
+         if (reduc_index == 2)
+           vec_else_clause = reduc_def;
+         else
+           {
+             vec_else_clause = vect_get_vec_def_for_operand (else_clause,
+                                                             stmt, NULL);
+             vect_is_simple_use (else_clause, loop_vinfo,
+                                 NULL, &gtemp, &def, &dts[3]);
+           }
+       }
+      else
+       {
+         vec_cond_lhs = vect_get_vec_def_for_stmt_copy (dts[0], vec_cond_lhs);
+         vec_cond_rhs = vect_get_vec_def_for_stmt_copy (dts[1], vec_cond_rhs);
+         vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
+                                                           vec_then_clause);
+         vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
+                                                           vec_else_clause);
+       }
+
+      /* Arguments are ready.  Create the new vector stmt.  */
+      vec_compare = build2 (TREE_CODE (cond_expr), vectype,
+                           vec_cond_lhs, vec_cond_rhs);
+      vec_cond_expr = build3 (VEC_COND_EXPR, vectype,
+                             vec_compare, vec_then_clause, vec_else_clause);
+
+      new_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
+      new_temp = make_ssa_name (vec_dest, new_stmt);
+      gimple_assign_set_lhs (new_stmt, new_temp);
+      vect_finish_stmt_generation (stmt, new_stmt, gsi);
+      if (j == 0)
+        STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
+      else
+        STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
+
+      prev_stmt_info = vinfo_for_stmt (new_stmt);
+    }
+
   return true;
 }
 
@@ -3937,10 +5042,10 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
 {
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
   bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
-  enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);  
+  enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
   bool ok;
-  HOST_WIDE_INT dummy;
   tree scalar_type, vectype;
+  gimple pattern_stmt, pattern_def_stmt;
 
   if (vect_print_dump_info (REPORT_DETAILS))
     {
@@ -3949,29 +5054,83 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
     }
 
   if (gimple_has_volatile_ops (stmt))
-    {    
+    {
       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
         fprintf (vect_dump, "not vectorized: stmt has volatile operands");
 
       return false;
     }
-  /* Skip stmts that do not need to be vectorized. In loops this is expected 
+
+  /* Skip stmts that do not need to be vectorized. In loops this is expected
      to include:
      - the COND_EXPR which is the loop exit condition
      - any LABEL_EXPRs in the loop
-     - computations that are used only for array indexing or loop control.  
+     - computations that are used only for array indexing or loop control.
      In basic blocks we only analyze statements that are a part of some SLP
-     instance, therefore, all the statements are relevant.  */
+     instance, therefore, all the statements are relevant.
 
-  if (!STMT_VINFO_RELEVANT_P (stmt_info) 
+     Pattern statement need to be analyzed instead of the original statement
+     if the original statement is not relevant.  Otherwise, we analyze both
+     statements.  */
+
+  pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
+  if (!STMT_VINFO_RELEVANT_P (stmt_info)
       && !STMT_VINFO_LIVE_P (stmt_info))
     {
-      if (vect_print_dump_info (REPORT_DETAILS))
-        fprintf (vect_dump, "irrelevant.");
+      if (STMT_VINFO_IN_PATTERN_P (stmt_info)
+          && pattern_stmt
+          && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
+              || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
+        {
+          /* Analyze PATTERN_STMT instead of the original stmt.  */
+          stmt = pattern_stmt;
+          stmt_info = vinfo_for_stmt (pattern_stmt);
+          if (vect_print_dump_info (REPORT_DETAILS))
+            {
+              fprintf (vect_dump, "==> examining pattern statement: ");
+              print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
+            }
+        }
+      else
+        {
+          if (vect_print_dump_info (REPORT_DETAILS))
+            fprintf (vect_dump, "irrelevant.");
 
-      return true;
+          return true;
+        }
     }
+  else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
+           && pattern_stmt
+           && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
+               || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
+    {
+      /* Analyze PATTERN_STMT too.  */
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "==> examining pattern statement: ");
+          print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
+        }
+
+      if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
+        return false;
+   }
+
+  if (is_pattern_stmt_p (stmt_info)
+      && (pattern_def_stmt = STMT_VINFO_PATTERN_DEF_STMT (stmt_info))
+      && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
+          || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt))))
+    {
+      /* Analyze def stmt of STMT if it's a pattern stmt.  */
+      if (vect_print_dump_info (REPORT_DETAILS))
+        {
+          fprintf (vect_dump, "==> examining pattern def statement: ");
+          print_gimple_stmt (vect_dump, pattern_def_stmt, 0, TDF_SLIM);
+        }
+
+      if (!vect_analyze_stmt (pattern_def_stmt, need_to_vectorize, node))
+        return false;
+   }
+
 
   switch (STMT_VINFO_DEF_TYPE (stmt_info))
     {
@@ -3997,7 +5156,7 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
     {
       gcc_assert (PURE_SLP_STMT (stmt_info));
 
-      scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
+      scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
       if (vect_print_dump_info (REPORT_DETAILS))
         {
           fprintf (vect_dump, "get vectype for scalar type:  ");
@@ -4032,27 +5191,31 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
     }
 
    ok = true;
-   if (!bb_vinfo 
+   if (!bb_vinfo
        && (STMT_VINFO_RELEVANT_P (stmt_info)
            || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
       ok = (vectorizable_type_promotion (stmt, NULL, NULL, NULL)
             || vectorizable_type_demotion (stmt, NULL, NULL, NULL)
             || vectorizable_conversion (stmt, NULL, NULL, NULL)
+            || vectorizable_shift (stmt, NULL, NULL, NULL)
             || vectorizable_operation (stmt, NULL, NULL, NULL)
             || vectorizable_assignment (stmt, NULL, NULL, NULL)
             || vectorizable_load (stmt, NULL, NULL, NULL, NULL)
             || vectorizable_call (stmt, NULL, NULL)
             || vectorizable_store (stmt, NULL, NULL, NULL)
-            || vectorizable_reduction (stmt, NULL, NULL)
+            || vectorizable_reduction (stmt, NULL, NULL, NULL)
             || vectorizable_condition (stmt, NULL, NULL, NULL, 0));
     else
       {
         if (bb_vinfo)
-          ok = (vectorizable_operation (stmt, NULL, NULL, node)
+          ok = (vectorizable_type_promotion (stmt, NULL, NULL, node)
+                || vectorizable_type_demotion (stmt, NULL, NULL, node)
+               || vectorizable_shift (stmt, NULL, NULL, node)
+                || vectorizable_operation (stmt, NULL, NULL, node)
                 || vectorizable_assignment (stmt, NULL, NULL, node)
                 || vectorizable_load (stmt, NULL, NULL, node, NULL)
                 || vectorizable_store (stmt, NULL, NULL, node));
-      }            
+      }
 
   if (!ok)
     {
@@ -4062,7 +5225,7 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
           fprintf (vect_dump, "supported: ");
           print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
         }
-    
+
       return false;
     }
 
@@ -4083,31 +5246,10 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
           fprintf (vect_dump, "supported: ");
           print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
         }
-   
+
        return false;
     }
 
-  if (!PURE_SLP_STMT (stmt_info))
-    {
-      /* Groups of strided accesses whose size is not a power of 2 are not 
-         vectorizable yet using loop-vectorization. Therefore, if this stmt 
-        feeds non-SLP-able stmts (i.e., this stmt has to be both SLPed and 
-        loop-based vectorized), the loop cannot be vectorized.  */
-      if (STMT_VINFO_STRIDED_ACCESS (stmt_info)
-          && exact_log2 (DR_GROUP_SIZE (vinfo_for_stmt (
-                                        DR_GROUP_FIRST_DR (stmt_info)))) == -1)
-        {
-          if (vect_print_dump_info (REPORT_DETAILS))
-            {
-              fprintf (vect_dump, "not vectorized: the size of group "
-                                  "of strided accesses is not a power of 2");
-              print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
-            }
-
-          return false;
-        }
-    }
-     
   return true;
 }
 
@@ -4118,13 +5260,12 @@ vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
 
 bool
 vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
-                    bool *strided_store, slp_tree slp_node, 
+                    bool *strided_store, slp_tree slp_node,
                      slp_instance slp_node_instance)
 {
   bool is_store = false;
   gimple vec_stmt = NULL;
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
-  gimple orig_stmt_in_pattern;
   bool done;
 
   switch (STMT_VINFO_TYPE (stmt_info))
@@ -4150,6 +5291,11 @@ vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
       gcc_assert (done);
       break;
 
+    case shift_vec_info_type:
+      done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
+      gcc_assert (done);
+      break;
+
     case op_vec_info_type:
       done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
       gcc_assert (done);
@@ -4161,7 +5307,7 @@ vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
       break;
 
     case load_vec_info_type:
-      done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node, 
+      done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
                                 slp_node_instance);
       gcc_assert (done);
       break;
@@ -4172,7 +5318,7 @@ vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
       if (STMT_VINFO_STRIDED_ACCESS (stmt_info) && !slp_node)
        {
          /* In case of interleaving, the whole chain is vectorized when the
-            last store in the chain is reached. Store stmts before the last
+            last store in the chain is reached.  Store stmts before the last
             one are skipped, and there vec_stmt_info shouldn't be freed
             meanwhile.  */
          *strided_store = true;
@@ -4192,11 +5338,11 @@ vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
     case call_vec_info_type:
       gcc_assert (!slp_node);
       done = vectorizable_call (stmt, gsi, &vec_stmt);
+      stmt = gsi_stmt (*gsi);
       break;
 
     case reduc_vec_info_type:
-      gcc_assert (!slp_node);
-      done = vectorizable_reduction (stmt, gsi, &vec_stmt);
+      done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
       gcc_assert (done);
       break;
 
@@ -4217,7 +5363,7 @@ vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
                                 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
       && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
       && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
-          || STMT_VINFO_RELEVANT (stmt_info) == 
+          || STMT_VINFO_RELEVANT (stmt_info) ==
                                            vect_used_in_outer_by_reduction))
     {
       struct loop *innerloop = LOOP_VINFO_LOOP (
@@ -4258,30 +5404,13 @@ vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
     }
 
   if (vec_stmt)
-    {
-      STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
-      orig_stmt_in_pattern = STMT_VINFO_RELATED_STMT (stmt_info);
-      if (orig_stmt_in_pattern)
-       {
-         stmt_vec_info stmt_vinfo = vinfo_for_stmt (orig_stmt_in_pattern);
-         /* STMT was inserted by the vectorizer to replace a computation idiom.
-            ORIG_STMT_IN_PATTERN is a stmt in the original sequence that 
-            computed this idiom.  We need to record a pointer to VEC_STMT in 
-            the stmt_info of ORIG_STMT_IN_PATTERN.  See more details in the 
-            documentation of vect_pattern_recog.  */
-         if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
-           {
-             gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo) == stmt);
-             STMT_VINFO_VEC_STMT (stmt_vinfo) = vec_stmt;
-           }
-       }
-    }
+    STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
 
-  return is_store; 
+  return is_store;
 }
 
 
-/* Remove a group of stores (for SLP or interleaving), free their 
+/* Remove a group of stores (for SLP or interleaving), free their
    stmt_vec_info.  */
 
 void
@@ -4296,7 +5425,7 @@ vect_remove_stores (gimple first_stmt)
       /* Free the attached stmt_vec_info and remove the stmt.  */
       next_si = gsi_for_stmt (next);
       gsi_remove (&next_si, true);
-      tmp = DR_GROUP_NEXT_DR (vinfo_for_stmt (next));
+      tmp = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
       free_stmt_vec_info (next);
       next = tmp;
     }
@@ -4308,7 +5437,7 @@ vect_remove_stores (gimple first_stmt)
    Create and initialize a new stmt_vec_info struct for STMT.  */
 
 stmt_vec_info
-new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo, 
+new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
                    bb_vec_info bb_vinfo)
 {
   stmt_vec_info res;
@@ -4322,8 +5451,10 @@ new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
   STMT_VINFO_LIVE_P (res) = false;
   STMT_VINFO_VECTYPE (res) = NULL;
   STMT_VINFO_VEC_STMT (res) = NULL;
+  STMT_VINFO_VECTORIZABLE (res) = true;
   STMT_VINFO_IN_PATTERN_P (res) = false;
   STMT_VINFO_RELATED_STMT (res) = NULL;
+  STMT_VINFO_PATTERN_DEF_STMT (res) = NULL;
   STMT_VINFO_DATA_REF (res) = NULL;
 
   STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
@@ -4342,13 +5473,13 @@ new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
   STMT_VINFO_INSIDE_OF_LOOP_COST (res) = 0;
   STMT_VINFO_OUTSIDE_OF_LOOP_COST (res) = 0;
   STMT_SLP_TYPE (res) = loop_vect;
-  DR_GROUP_FIRST_DR (res) = NULL;
-  DR_GROUP_NEXT_DR (res) = NULL;
-  DR_GROUP_SIZE (res) = 0;
-  DR_GROUP_STORE_COUNT (res) = 0;
-  DR_GROUP_GAP (res) = 0;
-  DR_GROUP_SAME_DR_STMT (res) = NULL;
-  DR_GROUP_READ_WRITE_DEPENDENCE (res) = false;
+  GROUP_FIRST_ELEMENT (res) = NULL;
+  GROUP_NEXT_ELEMENT (res) = NULL;
+  GROUP_SIZE (res) = 0;
+  GROUP_STORE_COUNT (res) = 0;
+  GROUP_GAP (res) = 0;
+  GROUP_SAME_DR_STMT (res) = NULL;
+  GROUP_READ_WRITE_DEPENDENCE (res) = false;
 
   return res;
 }
@@ -4390,25 +5521,59 @@ free_stmt_vec_info (gimple stmt)
 }
 
 
-/* Function get_vectype_for_scalar_type.
+/* Function get_vectype_for_scalar_type_and_size.
 
-   Returns the vector type corresponding to SCALAR_TYPE as supported
+   Returns the vector type corresponding to SCALAR_TYPE  and SIZE as supported
    by the target.  */
 
-tree
-get_vectype_for_scalar_type (tree scalar_type)
+static tree
+get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
 {
   enum machine_mode inner_mode = TYPE_MODE (scalar_type);
-  int nbytes = GET_MODE_SIZE (inner_mode);
+  enum machine_mode simd_mode;
+  unsigned int nbytes = GET_MODE_SIZE (inner_mode);
   int nunits;
   tree vectype;
 
-  if (nbytes == 0 || nbytes >= UNITS_PER_SIMD_WORD (inner_mode))
+  if (nbytes == 0)
+    return NULL_TREE;
+
+  /* We can't build a vector type of elements with alignment bigger than
+     their size.  */
+  if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
+    return NULL_TREE;
+
+  /* For vector types of elements whose mode precision doesn't
+     match their types precision we use a element type of mode
+     precision.  The vectorization routines will have to make sure
+     they support the proper result truncation/extension.  */
+  if (INTEGRAL_TYPE_P (scalar_type)
+      && GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type))
+    scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
+                                                 TYPE_UNSIGNED (scalar_type));
+
+  if (GET_MODE_CLASS (inner_mode) != MODE_INT
+      && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
     return NULL_TREE;
 
-  /* FORNOW: Only a single vector size per mode (UNITS_PER_SIMD_WORD)
-     is expected.  */
-  nunits = UNITS_PER_SIMD_WORD (inner_mode) / nbytes;
+  /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
+     When the component mode passes the above test simply use a type
+     corresponding to that mode.  The theory is that any use that
+     would cause problems with this will disable vectorization anyway.  */
+  if (!SCALAR_FLOAT_TYPE_P (scalar_type)
+      && !INTEGRAL_TYPE_P (scalar_type)
+      && !POINTER_TYPE_P (scalar_type))
+    scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
+
+  /* If no size was supplied use the mode the target prefers.   Otherwise
+     lookup a vector mode of the specified size.  */
+  if (size == 0)
+    simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
+  else
+    simd_mode = mode_for_vector (inner_mode, size / nbytes);
+  nunits = GET_MODE_SIZE (simd_mode) / nbytes;
+  if (nunits <= 1)
+    return NULL_TREE;
 
   vectype = build_vector_type (scalar_type, nunits);
   if (vect_print_dump_info (REPORT_DETAILS))
@@ -4437,6 +5602,37 @@ get_vectype_for_scalar_type (tree scalar_type)
   return vectype;
 }
 
+unsigned int current_vector_size;
+
+/* Function get_vectype_for_scalar_type.
+
+   Returns the vector type corresponding to SCALAR_TYPE as supported
+   by the target.  */
+
+tree
+get_vectype_for_scalar_type (tree scalar_type)
+{
+  tree vectype;
+  vectype = get_vectype_for_scalar_type_and_size (scalar_type,
+                                                 current_vector_size);
+  if (vectype
+      && current_vector_size == 0)
+    current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
+  return vectype;
+}
+
+/* Function get_same_sized_vectype
+
+   Returns a vector type corresponding to SCALAR_TYPE of size
+   VECTOR_TYPE if supported by the target.  */
+
+tree
+get_same_sized_vectype (tree scalar_type, tree vector_type)
+{
+  return get_vectype_for_scalar_type_and_size
+          (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
+}
+
 /* Function vect_is_simple_use.
 
    Input:
@@ -4446,40 +5642,40 @@ get_vectype_for_scalar_type (tree scalar_type)
    DEF - the defining stmt in case OPERAND is an SSA_NAME.
 
    Returns whether a stmt with OPERAND can be vectorized.
-   For loops, supportable operands are constants, loop invariants, and operands 
-   that are defined by the current iteration of the loop. Unsupportable 
-   operands are those that are defined by a previous iteration of the loop (as 
+   For loops, supportable operands are constants, loop invariants, and operands
+   that are defined by the current iteration of the loop.  Unsupportable
+   operands are those that are defined by a previous iteration of the loop (as
    is the case in reduction/induction computations).
    For basic blocks, supportable operands are constants and bb invariants.
    For now, operands defined outside the basic block are not supported.  */
 
 bool
-vect_is_simple_use (tree operand, loop_vec_info loop_vinfo, 
+vect_is_simple_use (tree operand, loop_vec_info loop_vinfo,
                     bb_vec_info bb_vinfo, gimple *def_stmt,
                    tree *def, enum vect_def_type *dt)
-{ 
+{
   basic_block bb;
   stmt_vec_info stmt_vinfo;
   struct loop *loop = NULL;
-  
+
   if (loop_vinfo)
     loop = LOOP_VINFO_LOOP (loop_vinfo);
 
   *def_stmt = NULL;
   *def = NULL_TREE;
-  
+
   if (vect_print_dump_info (REPORT_DETAILS))
     {
       fprintf (vect_dump, "vect_is_simple_use: operand ");
       print_generic_expr (vect_dump, operand, TDF_SLIM);
     }
-    
+
   if (TREE_CODE (operand) == INTEGER_CST || TREE_CODE (operand) == REAL_CST)
     {
       *dt = vect_constant_def;
       return true;
     }
-    
+
   if (is_gimple_min_invariant (operand))
     {
       *def = operand;
@@ -4493,14 +5689,14 @@ vect_is_simple_use (tree operand, loop_vec_info loop_vinfo,
         fprintf (vect_dump, "non-associatable copy.");
       operand = TREE_OPERAND (operand, 0);
     }
-    
+
   if (TREE_CODE (operand) != SSA_NAME)
     {
       if (vect_print_dump_info (REPORT_DETAILS))
         fprintf (vect_dump, "not ssa-name.");
       return false;
     }
-    
+
   *def_stmt = SSA_NAME_DEF_STMT (operand);
   if (*def_stmt == NULL)
     {
@@ -4528,7 +5724,7 @@ vect_is_simple_use (tree operand, loop_vec_info loop_vinfo,
 
   if ((loop && !flow_bb_inside_loop_p (loop, bb))
       || (!loop && bb != BB_VINFO_BB (bb_vinfo))
-      || (!loop && gimple_code (*def_stmt) == GIMPLE_PHI)) 
+      || (!loop && gimple_code (*def_stmt) == GIMPLE_PHI))
     *dt = vect_external_def;
   else
     {
@@ -4570,32 +5766,80 @@ vect_is_simple_use (tree operand, loop_vec_info loop_vinfo,
   return true;
 }
 
+/* Function vect_is_simple_use_1.
+
+   Same as vect_is_simple_use_1 but also determines the vector operand
+   type of OPERAND and stores it to *VECTYPE.  If the definition of
+   OPERAND is vect_uninitialized_def, vect_constant_def or
+   vect_external_def *VECTYPE will be set to NULL_TREE and the caller
+   is responsible to compute the best suited vector type for the
+   scalar operand.  */
+
+bool
+vect_is_simple_use_1 (tree operand, loop_vec_info loop_vinfo,
+                     bb_vec_info bb_vinfo, gimple *def_stmt,
+                     tree *def, enum vect_def_type *dt, tree *vectype)
+{
+  if (!vect_is_simple_use (operand, loop_vinfo, bb_vinfo, def_stmt, def, dt))
+    return false;
+
+  /* Now get a vector type if the def is internal, otherwise supply
+     NULL_TREE and leave it up to the caller to figure out a proper
+     type for the use stmt.  */
+  if (*dt == vect_internal_def
+      || *dt == vect_induction_def
+      || *dt == vect_reduction_def
+      || *dt == vect_double_reduction_def
+      || *dt == vect_nested_cycle)
+    {
+      stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
+
+      if (STMT_VINFO_IN_PATTERN_P (stmt_info)
+          && !STMT_VINFO_RELEVANT (stmt_info)
+          && !STMT_VINFO_LIVE_P (stmt_info))
+       stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
+
+      *vectype = STMT_VINFO_VECTYPE (stmt_info);
+      gcc_assert (*vectype != NULL_TREE);
+    }
+  else if (*dt == vect_uninitialized_def
+          || *dt == vect_constant_def
+          || *dt == vect_external_def)
+    *vectype = NULL_TREE;
+  else
+    gcc_unreachable ();
+
+  return true;
+}
+
 
 /* Function supportable_widening_operation
 
-   Check whether an operation represented by the code CODE is a 
-   widening operation that is supported by the target platform in 
-   vector form (i.e., when operating on arguments of type VECTYPE).
-    
+   Check whether an operation represented by the code CODE is a
+   widening operation that is supported by the target platform in
+   vector form (i.e., when operating on arguments of type VECTYPE_IN
+   producing a result of type VECTYPE_OUT).
+
    Widening operations we currently support are NOP (CONVERT), FLOAT
    and WIDEN_MULT.  This function checks if these operations are supported
    by the target platform either directly (via vector tree-codes), or via
    target builtins.
 
    Output:
-   - CODE1 and CODE2 are codes of vector operations to be used when 
-   vectorizing the operation, if available. 
+   - CODE1 and CODE2 are codes of vector operations to be used when
+   vectorizing the operation, if available.
    - DECL1 and DECL2 are decls of target builtin functions to be used
-   when vectorizing the operation, if available. In this case,
-   CODE1 and CODE2 are CALL_EXPR.  
+   when vectorizing the operation, if available.  In this case,
+   CODE1 and CODE2 are CALL_EXPR.
    - MULTI_STEP_CVT determines the number of required intermediate steps in
    case of multi-step conversion (like char->short->int - in that case
    MULTI_STEP_CVT will be 1).
-   - INTERM_TYPES contains the intermediate type required to perform the 
-   widening operation (short in the above example).  */   
+   - INTERM_TYPES contains the intermediate type required to perform the
+   widening operation (short in the above example).  */
 
 bool
-supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
+supportable_widening_operation (enum tree_code code, gimple stmt,
+                               tree vectype_out, tree vectype_in,
                                 tree *decl1, tree *decl2,
                                 enum tree_code *code1, enum tree_code *code2,
                                 int *multi_step_cvt,
@@ -4603,36 +5847,40 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
 {
   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
   loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
-  struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
+  struct loop *vect_loop = NULL;
   bool ordered_p;
   enum machine_mode vec_mode;
   enum insn_code icode1, icode2;
   optab optab1, optab2;
-  tree type = gimple_expr_type (stmt);
-  tree wide_vectype = get_vectype_for_scalar_type (type);
+  tree vectype = vectype_in;
+  tree wide_vectype = vectype_out;
   enum tree_code c1, c2;
 
+  if (loop_info)
+    vect_loop = LOOP_VINFO_LOOP (loop_info);
+
   /* The result of a vectorized widening operation usually requires two vectors
-     (because the widened results do not fit int one vector). The generated 
-     vector results would normally be expected to be generated in the same 
+     (because the widened results do not fit int one vector). The generated
+     vector results would normally be expected to be generated in the same
      order as in the original scalar computation, i.e. if 8 results are
      generated in each vector iteration, they are to be organized as follows:
-        vect1: [res1,res2,res3,res4], vect2: [res5,res6,res7,res8]. 
+        vect1: [res1,res2,res3,res4], vect2: [res5,res6,res7,res8].
 
-     However, in the special case that the result of the widening operation is 
+     However, in the special case that the result of the widening operation is
      used in a reduction computation only, the order doesn't matter (because
-     when vectorizing a reduction we change the order of the computation). 
+     when vectorizing a reduction we change the order of the computation).
      Some targets can take advantage of this and generate more efficient code.
      For example, targets like Altivec, that support widen_mult using a sequence
      of {mult_even,mult_odd} generate the following vectors:
         vect1: [res1,res3,res5,res7], vect2: [res2,res4,res6,res8].
 
      When vectorizing outer-loops, we execute the inner-loop sequentially
-     (each vectorized inner-loop iteration contributes to VF outer-loop 
-     iterations in parallel). We therefore don't allow to change the order 
+     (each vectorized inner-loop iteration contributes to VF outer-loop
+     iterations in parallel).  We therefore don't allow to change the order
      of the computation in the inner-loop during outer-loop vectorization.  */
 
-   if (STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
+   if (vect_loop
+       && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
        && !nested_in_vect_loop_p (vect_loop, stmt))
      ordered_p = false;
    else
@@ -4669,6 +5917,19 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
         }
       break;
 
+    case WIDEN_LSHIFT_EXPR:
+      if (BYTES_BIG_ENDIAN)
+        {
+          c1 = VEC_WIDEN_LSHIFT_HI_EXPR;
+          c2 = VEC_WIDEN_LSHIFT_LO_EXPR;
+        }
+      else
+        {
+          c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
+          c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
+        }
+      break;
+
     CASE_CONVERT:
       if (BYTES_BIG_ENDIAN)
         {
@@ -4708,8 +5969,8 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
   if (code == FIX_TRUNC_EXPR)
     {
       /* The signedness is determined from output operand.  */
-      optab1 = optab_for_tree_code (c1, type, optab_default);
-      optab2 = optab_for_tree_code (c2, type, optab_default);
+      optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
+      optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
     }
   else
     {
@@ -4721,12 +5982,11 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
     return false;
 
   vec_mode = TYPE_MODE (vectype);
-  if ((icode1 = optab_handler (optab1, vec_mode)->insn_code) == CODE_FOR_nothing
-       || (icode2 = optab_handler (optab2, vec_mode)->insn_code)
-                                                       == CODE_FOR_nothing)
+  if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
+       || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
     return false;
 
-  /* Check if it's a multi-step conversion that can be done using intermediate 
+  /* Check if it's a multi-step conversion that can be done using intermediate
      types.  */
   if (insn_data[icode1].operand[0].mode != TYPE_MODE (wide_vectype)
        || insn_data[icode2].operand[0].mode != TYPE_MODE (wide_vectype))
@@ -4738,13 +5998,14 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
 
       if (!CONVERT_EXPR_CODE_P (code))
         return false;
-      
+
       *code1 = c1;
       *code2 = c2;
-    
+
       /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
-         intermediate  steps in promotion sequence. We try MAX_INTERM_CVT_STEPS
-         to get to NARROW_VECTYPE, and fail if we do not.  */
+         intermediate steps in promotion sequence.  We try
+         MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
+         not.  */
       *interm_types = VEC_alloc (tree, heap, MAX_INTERM_CVT_STEPS);
       for (i = 0; i < 3; i++)
         {
@@ -4755,16 +6016,16 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
           optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
 
           if (!optab3 || !optab4
-              || (icode1 = optab1->handlers[(int) prev_mode].insn_code)
-                                                        == CODE_FOR_nothing
+              || ((icode1 = optab_handler (optab1, prev_mode))
+                 == CODE_FOR_nothing)
               || insn_data[icode1].operand[0].mode != intermediate_mode
-              || (icode2 = optab2->handlers[(int) prev_mode].insn_code)
-                                                        == CODE_FOR_nothing
+              || ((icode2 = optab_handler (optab2, prev_mode))
+                 == CODE_FOR_nothing)
               || insn_data[icode2].operand[0].mode != intermediate_mode
-              || (icode1 = optab3->handlers[(int) intermediate_mode].insn_code) 
-                                                        == CODE_FOR_nothing
-              || (icode2 = optab4->handlers[(int) intermediate_mode].insn_code)
-                                                        == CODE_FOR_nothing)
+              || ((icode1 = optab_handler (optab3, intermediate_mode))
+                 == CODE_FOR_nothing)
+              || ((icode2 = optab_handler (optab4, intermediate_mode))
+                 == CODE_FOR_nothing))
             return false;
 
           VEC_quick_push (tree, *interm_types, intermediate_type);
@@ -4789,34 +6050,35 @@ supportable_widening_operation (enum tree_code code, gimple stmt, tree vectype,
 
 /* Function supportable_narrowing_operation
 
-   Check whether an operation represented by the code CODE is a 
-   narrowing operation that is supported by the target platform in 
-   vector form (i.e., when operating on arguments of type VECTYPE).
-    
+   Check whether an operation represented by the code CODE is a
+   narrowing operation that is supported by the target platform in
+   vector form (i.e., when operating on arguments of type VECTYPE_IN
+   and producing a result of type VECTYPE_OUT).
+
    Narrowing operations we currently support are NOP (CONVERT) and
-   FIX_TRUNC. This function checks if these operations are supported by
+   FIX_TRUNC.  This function checks if these operations are supported by
    the target platform directly via vector tree-codes.
 
    Output:
-   - CODE1 is the code of a vector operation to be used when 
-   vectorizing the operation, if available. 
+   - CODE1 is the code of a vector operation to be used when
+   vectorizing the operation, if available.
    - MULTI_STEP_CVT determines the number of required intermediate steps in
    case of multi-step conversion (like int->short->char - in that case
    MULTI_STEP_CVT will be 1).
    - INTERM_TYPES contains the intermediate type required to perform the
-   narrowing operation (short in the above example).   */ 
+   narrowing operation (short in the above example).   */
 
 bool
 supportable_narrowing_operation (enum tree_code code,
-                                const_gimple stmt, tree vectype,
+                                tree vectype_out, tree vectype_in,
                                 enum tree_code *code1, int *multi_step_cvt,
                                  VEC (tree, heap) **interm_types)
 {
   enum machine_mode vec_mode;
   enum insn_code icode1;
   optab optab1, interm_optab;
-  tree type = gimple_expr_type (stmt);
-  tree narrow_vectype = get_vectype_for_scalar_type (type);
+  tree vectype = vectype_in;
+  tree narrow_vectype = vectype_out;
   enum tree_code c1;
   tree intermediate_type, prev_type;
   int i;
@@ -4842,7 +6104,7 @@ supportable_narrowing_operation (enum tree_code code,
 
   if (code == FIX_TRUNC_EXPR)
     /* The signedness is determined from output operand.  */
-    optab1 = optab_for_tree_code (c1, type, optab_default);
+    optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
   else
     optab1 = optab_for_tree_code (c1, vectype, optab_default);
 
@@ -4850,8 +6112,7 @@ supportable_narrowing_operation (enum tree_code code,
     return false;
 
   vec_mode = TYPE_MODE (vectype);
-  if ((icode1 = optab_handler (optab1, vec_mode)->insn_code) 
-       == CODE_FOR_nothing)
+  if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
     return false;
 
   /* Check if it's a multi-step conversion that can be done using intermediate
@@ -4863,23 +6124,23 @@ supportable_narrowing_operation (enum tree_code code,
       *code1 = c1;
       prev_type = vectype;
       /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
-         intermediate  steps in promotion sequence. We try MAX_INTERM_CVT_STEPS
-         to get to NARROW_VECTYPE, and fail if we do not.  */
+         intermediate steps in promotion sequence.  We try
+         MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
+         not.  */
       *interm_types = VEC_alloc (tree, heap, MAX_INTERM_CVT_STEPS);
       for (i = 0; i < 3; i++)
         {
           intermediate_mode = insn_data[icode1].operand[0].mode;
           intermediate_type = lang_hooks.types.type_for_mode (intermediate_mode,
                                                      TYPE_UNSIGNED (prev_type));
-          interm_optab = optab_for_tree_code (c1, intermediate_type, 
+          interm_optab = optab_for_tree_code (c1, intermediate_type,
                                               optab_default);
-          if (!interm_optab  
-              || (icode1 = optab1->handlers[(int) prev_mode].insn_code)
-                                                        == CODE_FOR_nothing
+          if (!interm_optab
+              || ((icode1 = optab_handler (optab1, prev_mode))
+                 == CODE_FOR_nothing)
               || insn_data[icode1].operand[0].mode != intermediate_mode
-              || (icode1 
-                  = interm_optab->handlers[(int) intermediate_mode].insn_code)
-                 == CODE_FOR_nothing)
+              || ((icode1 = optab_handler (interm_optab, intermediate_mode))
+                 == CODE_FOR_nothing))
             return false;
 
           VEC_quick_push (tree, *interm_types, intermediate_type);