diff --git a/gcc/config/riscv/riscv-vsetvl.cc b/gcc/config/riscv/riscv-vsetvl.cc
index c73a84cb6bd..f8b708c248a 100644
--- a/gcc/config/riscv/riscv-vsetvl.cc
+++ b/gcc/config/riscv/riscv-vsetvl.cc
@@ -2721,6 +2721,7 @@ public:
   }
 };
 
+
 const pass_data pass_data_vsetvl = {
   RTL_PASS,	 /* type */
   "vsetvl",	 /* name */
@@ -2736,54 +2737,8 @@ const pass_data pass_data_vsetvl = {
 class pass_vsetvl : public rtl_opt_pass
 {
 private:
-  vector_infos_manager *m_vector_manager;
-
-  const vector_insn_info &get_vector_info (const rtx_insn *) const;
-  const vector_insn_info &get_vector_info (const insn_info *) const;
-  const vector_block_info &get_block_info (const basic_block) const;
-  const vector_block_info &get_block_info (const bb_info *) const;
-  vector_block_info &get_block_info (const basic_block);
-  vector_block_info &get_block_info (const bb_info *);
-  void update_vector_info (const insn_info *, const vector_insn_info &);
-  void update_block_info (int, profile_probability, const vector_insn_info &);
-
-  void simple_vsetvl (void) const;
-  void lazy_vsetvl (void);
-
-  /* Phase 1.  */
-  void compute_local_backward_infos (const bb_info *);
-
-  /* Phase 2.  */
-  bool need_vsetvl (const vector_insn_info &, const vector_insn_info &) const;
-  void transfer_before (vector_insn_info &, insn_info *) const;
-  void transfer_after (vector_insn_info &, insn_info *) const;
-  void emit_local_forward_vsetvls (const bb_info *);
-
-  /* Phase 3.  */
-  bool earliest_fusion (void);
-  void vsetvl_fusion (void);
-
-  /* Phase 4.  */
-  void prune_expressions (void);
-  void compute_local_properties (void);
-  bool can_refine_vsetvl_p (const basic_block, const vector_insn_info &) const;
-  void refine_vsetvls (void) const;
-  void cleanup_vsetvls (void);
-  bool commit_vsetvls (void);
-  void pre_vsetvl (void);
-
-  /* Phase 5.  */
-  rtx_insn *get_vsetvl_at_end (const bb_info *, vector_insn_info *) const;
-  void local_eliminate_vsetvl_insn (const bb_info *) const;
-  bool global_eliminate_vsetvl_insn (const bb_info *) const;
-  void ssa_post_optimization (void) const;
-
-  /* Phase 6.  */
-  void df_post_optimization (void) const;
-
-  void init (void);
-  void done (void);
-  void compute_probabilities (void);
+  void simple_vsetvl ();
+  void lazy_vsetvl ();
 
 public:
   pass_vsetvl (gcc::context *ctxt) : rtl_opt_pass (pass_data_vsetvl, ctxt) {}
@@ -2793,69 +2748,11 @@ public:
   virtual unsigned int execute (function *) final override;
 }; // class pass_vsetvl
 
-const vector_insn_info &
-pass_vsetvl::get_vector_info (const rtx_insn *i) const
-{
-  return m_vector_manager->vector_insn_infos[INSN_UID (i)];
-}
-
-const vector_insn_info &
-pass_vsetvl::get_vector_info (const insn_info *i) const
-{
-  return m_vector_manager->vector_insn_infos[i->uid ()];
-}
-
-const vector_block_info &
-pass_vsetvl::get_block_info (const basic_block bb) const
-{
-  return m_vector_manager->vector_block_infos[bb->index];
-}
-
-const vector_block_info &
-pass_vsetvl::get_block_info (const bb_info *bb) const
-{
-  return m_vector_manager->vector_block_infos[bb->index ()];
-}
-
-vector_block_info &
-pass_vsetvl::get_block_info (const basic_block bb)
-{
-  return m_vector_manager->vector_block_infos[bb->index];
-}
-
-vector_block_info &
-pass_vsetvl::get_block_info (const bb_info *bb)
-{
-  return m_vector_manager->vector_block_infos[bb->index ()];
-}
-
-void
-pass_vsetvl::update_vector_info (const insn_info *i,
-				 const vector_insn_info &new_info)
-{
-  m_vector_manager->vector_insn_infos[i->uid ()] = new_info;
-}
-
-void
-pass_vsetvl::update_block_info (int index, profile_probability prob,
-				const vector_insn_info &new_info)
-{
-  m_vector_manager->vector_block_infos[index].probability = prob;
-  if (m_vector_manager->vector_block_infos[index].local_dem
-      == m_vector_manager->vector_block_infos[index].reaching_out)
-    m_vector_manager->vector_block_infos[index].local_dem = new_info;
-  m_vector_manager->vector_block_infos[index].reaching_out = new_info;
-}
-
-/* Simple m_vsetvl_insert vsetvl for optimize == 0.  */
 void
-pass_vsetvl::simple_vsetvl (void) const
+pass_vsetvl::simple_vsetvl ()
 {
   if (dump_file)
-    fprintf (dump_file,
-	     "\nEntering Simple VSETVL PASS and Handling %d basic blocks for "
-	     "function:%s\n",
-	     n_basic_blocks_for_fn (cfun), function_name (cfun));
+    fprintf (dump_file, "\nEntering Simple VSETVL PASS\n");
 
   basic_block cfg_bb;
   rtx_insn *rinsn;
@@ -2867,14 +2764,102 @@ pass_vsetvl::simple_vsetvl (void) const
 	    continue;
 	  if (has_vtype_op (rinsn))
 	    {
-	      const auto info = get_vector_info (rinsn);
-	      emit_vsetvl_insn (VSETVL_DISCARD_RESULT, EMIT_BEFORE, info,
-				NULL_RTX, rinsn);
+	      const auto &info = vsetvl_info (rinsn);
+	      rtx pat = info.get_vsetvl_pat ();
+	      emit_insn_before (pat, rinsn);
+	      if (dump_file)
+		{
+		  fprintf (dump_file, "  Insert vsetvl insn before insn %d:\n",
+			   INSN_UID (rinsn));
+		  print_rtl_single (dump_file, PREV_INSN (rinsn));
+		}
 	    }
 	}
     }
 }
 
+/* Lazy vsetvl insertion for optimize > 0. */
+void
+pass_vsetvl::lazy_vsetvl ()
+{
+  if (dump_file)
+    fprintf (dump_file, "\nEntering Lazy VSETVL PASS\n\n");
+
+  pre_vsetvl pre = pre_vsetvl ();
+
+  if (dump_file)
+    fprintf (dump_file, "\nPhase 1: Fuse local vsetvl infos.\n\n");
+  pre.fuse_local_vsetvl_info ();
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    pre.dump (dump_file, "phase 1");
+
+  /* Phase 2:  Fuse header and footer vsetvl infos between basic blocks.  */
+  if (dump_file)
+    fprintf (dump_file, "\nPhase 2: Lift up vsetvl info.\n\n");
+  bool changed;
+  int fused_count = 0;
+  do
+    {
+      if (dump_file)
+	fprintf (dump_file, "  Try lift up %d.\n\n", fused_count);
+      changed = pre.earliest_fuse_vsetvl_info ();
+      fused_count += 1;
+  } while (changed);
+
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    pre.dump (dump_file, "phase 2");
+
+  /* Phase 3: Reducing redundant vsetvl infos using LCM.  */
+  if (dump_file)
+    fprintf (dump_file, "\nPhase 3: Reduce global vsetvl infos.\n\n");
+  pre.pre_global_vsetvl_info ();
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    pre.dump (dump_file, "phase 3");
+
+  /* Phase 4: Insert, modify and remove vsetvl insns.  */
+  if (dump_file)
+    fprintf (dump_file,
+	     "\nPhase 4: Insert, modify and remove vsetvl insns.\n\n");
+  pre.emit_vsetvl ();
+
+  /* Phase 5: Cleaup */
+  if (dump_file)
+    fprintf (dump_file, "\nPhase 5: Cleaup\n\n");
+  pre.cleaup ();
+
+  pre.finish ();
+}
+
+/* Main entry point for this pass.  */
+unsigned int
+pass_vsetvl::execute (function *)
+{
+  if (n_basic_blocks_for_fn (cfun) <= 0)
+    return 0;
+
+  /* The RVV instruction may change after split which is not a stable
+     instruction. We need to split it here to avoid potential issue
+     since the VSETVL PASS is insert before split PASS.  */
+  split_all_insns ();
+
+  /* Early return for there is no vector instructions.  */
+  if (!has_vector_insn (cfun))
+    return 0;
+
+  if (!optimize)
+    simple_vsetvl ();
+  else
+    lazy_vsetvl ();
+
+  return 0;
+}
+
+rtl_opt_pass *
+make_pass_vsetvl (gcc::context *ctxt)
+{
+  return new pass_vsetvl (ctxt);
+}
+
 /* Compute demanded information by backward data-flow analysis.  */
 void
 pass_vsetvl::compute_local_backward_infos (const bb_info *bb)
