I hope to implement a feature to transform an IR with “while” or “for” loops to a new IR with no loop. Instead, I just want to use if-else statements in the new IR to implement the original semantics.
I can easily write a transform pass to handle the 1-level loop case, but for nested loops it seems a little harder.
If the inner loop can be flatten [1] (not sure if I use the right word), which means transforming the nested loop into 1-level loop, I think you can continue using your pass on it.
Thanks for your suggestion! Yes, I am trying to do this “nested flattening”. It seems that I need a post-dominator tree-based algorithm to flatten the nested loops from the innermost to the outermost, level by level.
Is there any feature already existed in LLVM tools? Or similar?
I am not sure I understand the problem statement. If the max iteration
count is statically known, any loop nest can be unrolled. Otherwise, I
don't see how one can remove such loops. Recursion maybe?
Polly does not implement loop flattening. That is, there's a "flatten
schedule" pass which would theoretically do this (if loop iteration
count is known), but it is not meant for production use.