OK, so O(n^3) is probably not good even for debugging. I'll rewrite it with the same algorithm as topological sort, as topological sort can detect a cycle in a graph. That is much cheaper than this naive algorithm. http://llvm-reviews.chandlerc.com/D1980