diff --git a/gcc/config/riscv/riscv-vsetvl.cc b/gcc/config/riscv/riscv-vsetvl.cc
index 90477f331d7c52f0b089621abdbade68e31c9cd6..ed5a2b58ab04ec4c98fcfcc241d6c0ee7f75f3d8 100644
--- a/gcc/config/riscv/riscv-vsetvl.cc
+++ b/gcc/config/riscv/riscv-vsetvl.cc
@@ -579,6 +579,8 @@ extract_single_source (set_info *set)
   if (!set->insn ()->is_phi ())
     return nullptr;
   hash_set<set_info *> sets = get_all_sets (set, true, false, true);
+  if (sets.is_empty ())
+    return nullptr;
 
   insn_info *first_insn = (*sets.begin ())->insn ();
   if (first_insn->is_artificial ())
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/avl_use_bug-1.c b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/avl_use_bug-1.c
new file mode 100644
index 0000000000000000000000000000000000000000..330221c2d7b4ad42a6c94ff5edb1c2b9a97f5f02
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/avl_use_bug-1.c
@@ -0,0 +1,39 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv_zvl256b -mabi=lp64 --param=riscv-autovec-lmul=m8 --param=riscv-autovec-preference=fixed-vlmax -O2" } */
+
+struct a_struct
+{
+  unsigned char a_character;
+};
+
+struct a_struct an_array[5];
+struct a_struct *a_ptr;
+int yabba = 1;
+
+int
+f (a, b)
+     unsigned char a;
+     unsigned long b;
+{
+  long i, j, p, q, r, s;
+
+  if (b != (unsigned long) 0)
+    {
+      if (yabba)
+	return -1;
+      s = 4000000 / b;
+      for (i = 0; i < 11; i++)
+	{
+	  for (j = 0; j < 256; j++)
+	    {
+	      if (((p - s < 0) ? -s : 0) < (( q - s < 0) ? -s : q))
+		r = i;
+	    }
+	}
+    }
+
+  if (yabba)
+    return 0;
+  a_ptr = &an_array[a];
+  a_ptr->a_character = (unsigned char) r;
+}