diff --git a/gcc/bitmap.cc b/gcc/bitmap.cc
index 20de562caac07d2716ff96a1a61ded3ed8dc32c1..d1d0324b633e7a118a53e6861bee97bf8ba8bf91 100644
--- a/gcc/bitmap.cc
+++ b/gcc/bitmap.cc
@@ -1217,12 +1217,12 @@ bitmap_single_bit_set_p (const_bitmap a)
 
 
 /* Return the bit number of the first set bit in the bitmap.  The
-   bitmap must be non-empty.  */
+   bitmap must be non-empty.  When CLEAR is true it clears the bit.  */
 
-unsigned
-bitmap_first_set_bit (const_bitmap a)
+static unsigned
+bitmap_first_set_bit_worker (bitmap a, bool clear)
 {
-  const bitmap_element *elt = a->first;
+  bitmap_element *elt = a->first;
   unsigned bit_no;
   BITMAP_WORD word;
   unsigned ix;
@@ -1269,9 +1269,42 @@ bitmap_first_set_bit (const_bitmap a)
 
  gcc_checking_assert (word & 1);
 #endif
+
+ if (clear)
+   {
+     elt->bits[ix] &= ~((BITMAP_WORD) 1 << (bit_no % BITMAP_WORD_BITS));
+     /* If we cleared the entire word, free up the element.  */
+     if (!elt->bits[ix]
+	 && bitmap_element_zerop (elt))
+       {
+	 if (!a->tree_form)
+	   bitmap_list_unlink_element (a, elt);
+	 else
+	   bitmap_tree_unlink_element (a, elt);
+       }
+   }
+
  return bit_no;
 }
 
+/* Return the bit number of the first set bit in the bitmap.  The
+   bitmap must be non-empty.  */
+
+unsigned
+bitmap_first_set_bit (const_bitmap a)
+{
+  return bitmap_first_set_bit_worker (const_cast<bitmap> (a), false);
+}
+
+/* Return and clear the bit number of the first set bit in the bitmap.  The
+   bitmap must be non-empty.  */
+
+unsigned
+bitmap_clear_first_set_bit (bitmap a)
+{
+  return bitmap_first_set_bit_worker (a, true);
+}
+
 /* Return the bit number of the first set bit in the bitmap.  The
    bitmap must be non-empty.  */
 
diff --git a/gcc/bitmap.h b/gcc/bitmap.h
index 43337d2e9d9ed8c1be2c230221f2c8e7204d9f8f..5432f386dbd4509bcf2bc4780f7dba8cc43e56fd 100644
--- a/gcc/bitmap.h
+++ b/gcc/bitmap.h
@@ -110,6 +110,7 @@ along with GCC; see the file COPYING3.  If not see
 
      * clear			: bitmap_clear
      * smallest_member		: bitmap_first_set_bit
+     * pop_smallest		: bitmap_clear_first_set_bit
      * choose_one		: (not implemented, but could be
 				   in constant time)
 
@@ -133,6 +134,7 @@ along with GCC; see the file COPYING3.  If not see
    amortized time with O(E) worst-case behavior.
 
      * smallest_member
+     * pop_smallest
      * largest_member
      * set_size
      * member_p
@@ -501,6 +503,7 @@ extern void debug (const bitmap_head &ref);
 extern void debug (const bitmap_head *ptr);
 
 extern unsigned bitmap_first_set_bit (const_bitmap);
+extern unsigned bitmap_clear_first_set_bit (bitmap);
 extern unsigned bitmap_last_set_bit (const_bitmap);
 
 /* Compute bitmap hash (for purposes of hashing etc.)  */
diff --git a/gcc/df-core.cc b/gcc/df-core.cc
index 38f69ac57431381ac569b3761bb36a6ab84f3a74..3286ffda2ce82f0e0b5a5d5cbcb79fa2c7fad360 100644
--- a/gcc/df-core.cc
+++ b/gcc/df-core.cc
@@ -1040,8 +1040,7 @@ df_worklist_dataflow_doublequeue (struct dataflow *dataflow,
 
       do
 	{
-	  unsigned index = bitmap_first_set_bit (worklist);
-	  bitmap_clear_bit (worklist, index);
+	  unsigned index = bitmap_clear_first_set_bit (worklist);
 
 	  unsigned bb_index;
 	  dcount++;
diff --git a/gcc/graphite-scop-detection.cc b/gcc/graphite-scop-detection.cc
index f976451949d4d0dfa372c58ba4fca9776ec6dffd..99551990e5445f583439d43920e6d4a1a94f5c88 100644
--- a/gcc/graphite-scop-detection.cc
+++ b/gcc/graphite-scop-detection.cc
@@ -469,8 +469,7 @@ scop_detection::merge_sese (sese_l first, sese_l second) const
      its border it acts more like a visited bitmap.  */
   do
     {
-      int index = bitmap_first_set_bit (worklist);
-      bitmap_clear_bit (worklist, index);
+      int index = bitmap_clear_first_set_bit (worklist);
       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, index);
       edge_iterator ei;
       edge e;
diff --git a/gcc/sanopt.cc b/gcc/sanopt.cc
index 85489739507e43fc3b8fe463f14cc1f7a6ee9e96..ce8393b951872e571aed77b6a8a6806f21607a9c 100644
--- a/gcc/sanopt.cc
+++ b/gcc/sanopt.cc
@@ -1012,8 +1012,7 @@ sanitize_asan_mark_unpoison (void)
   /* 2) Propagate the information to all reachable blocks.  */
   while (!bitmap_empty_p (worklist))
     {
-      unsigned i = bitmap_first_set_bit (worklist);
-      bitmap_clear_bit (worklist, i);
+      unsigned i = bitmap_clear_first_set_bit (worklist);
       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
       gcc_assert (bb);
 
@@ -1109,8 +1108,7 @@ sanitize_asan_mark_poison (void)
   /* 2) Propagate the information to all definitions blocks.  */
   while (!bitmap_empty_p (worklist))
     {
-      unsigned i = bitmap_first_set_bit (worklist);
-      bitmap_clear_bit (worklist, i);
+      unsigned i = bitmap_clear_first_set_bit (worklist);
       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
       gcc_assert (bb);
 
diff --git a/gcc/tree-cfgcleanup.cc b/gcc/tree-cfgcleanup.cc
index 64ff16fc45bab5a91e6b1e05cb4651c3aede28b0..42b253121220251c630fb27a564af51ed59698fb 100644
--- a/gcc/tree-cfgcleanup.cc
+++ b/gcc/tree-cfgcleanup.cc
@@ -1133,8 +1133,7 @@ cleanup_tree_cfg_noloop (unsigned ssa_update_flags)
   /* Now process the altered blocks, as long as any are available.  */
   while (!bitmap_empty_p (cfgcleanup_altered_bbs))
     {
-      unsigned i = bitmap_first_set_bit (cfgcleanup_altered_bbs);
-      bitmap_clear_bit (cfgcleanup_altered_bbs, i);
+      unsigned i = bitmap_clear_first_set_bit (cfgcleanup_altered_bbs);
       if (i < NUM_FIXED_BLOCKS)
 	continue;
 
diff --git a/gcc/tree-into-ssa.cc b/gcc/tree-into-ssa.cc
index 2e3229904565117faa8d2581beceaa6a552c5ea6..5cfe7c516cc95b9d9f6c38cc7a9ad31826cb0803 100644
--- a/gcc/tree-into-ssa.cc
+++ b/gcc/tree-into-ssa.cc
@@ -2348,8 +2348,7 @@ rewrite_blocks (basic_block entry, enum rewrite_mode what)
 	}
       while (!bitmap_empty_p (worklist))
 	{
-	  int idx = bitmap_first_set_bit (worklist);
-	  bitmap_clear_bit (worklist, idx);
+	  int idx = bitmap_clear_first_set_bit (worklist);
 	  basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
 	  bb->flags |= in_region;
 	  extra_rgn.safe_push (bb);
diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc
index 0ae998f86f9839ba5a95c5fc1a22cd2f12b5cdad..bda780876f3afd3a69351699f7a58c423ed997ed 100644
--- a/gcc/tree-ssa-dce.cc
+++ b/gcc/tree-ssa-dce.cc
@@ -2102,8 +2102,7 @@ simple_dce_from_worklist (bitmap worklist)
   while (! bitmap_empty_p (worklist))
     {
       /* Pop item.  */
-      unsigned i = bitmap_first_set_bit (worklist);
-      bitmap_clear_bit (worklist, i);
+      unsigned i = bitmap_clear_first_set_bit (worklist);
 
       tree def = ssa_name (i);
       /* Removed by somebody else or still in use.  */
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 9692911e31b2567f112aa97f5a7e2e94112f5b4b..7fa2a154e840c115fd225e6848fef951c49186f0 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -8491,8 +8491,7 @@ do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
       bitmap_set_bit (worklist, 0);
       while (!bitmap_empty_p (worklist))
 	{
-	  int idx = bitmap_first_set_bit (worklist);
-	  bitmap_clear_bit (worklist, idx);
+	  int idx = bitmap_clear_first_set_bit (worklist);
 	  basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
 	  gcc_assert ((bb->flags & BB_EXECUTABLE)
 		      && !rpo_state[idx].visited);