In the general case, this is impossible because BBs don't form a DAG. A topological order is only defined for a DAG - a graph without cycles; BBs within a function may form cycles (loops, etc).
Your best approximation IMHO is to decompose the BB graph into SCCs (Strongly Connected Components). LLVM already has tools to do that: see include/llvm/ADT/SCCIterator.h
, and also tools/opt/PrintSCC.cpp
.
The latter, in fact, will already print the SCCs of a function in reverse topological order, invoked like this:
$ opt -print-cfg-sccs <bitcode file>
Update (16-Sep-2013): See also this blog post.