Extracting Single-entry single-exit Regions into functions

Hi,
I want to write a pass that pulls single-entry a single-exit (SESE) region into a function.
The idea is similar to extracting a loop in LoopExtractor.cpp.

Basically, for each region that meet our criteria,

  1. Find all basic blocks in that regions
  2. Call llvm::ExtractCodeRegion() to replace that region by a call to the extracted function.
  3. Delete the region from the RegionPassManager queue
  4. Update the RegionInfo to reflect the change

To find all basic blocks (step 1), I use Region::block_iterator().
However, after changing the CFG, that block_iterator does not seem to work anymore.

Should I add a list of basic blocks for each Region like what people did with Loop?

Thank you.