diff --git a/docs/LoopTerminology.rst b/docs/LoopTerminology.rst index 991282e5051..e33a019da3c 100644 --- a/docs/LoopTerminology.rst +++ b/docs/LoopTerminology.rst @@ -43,6 +43,9 @@ Note that there are some important implications of this definition: * Any two loops are either fully disjoint (no intersecting blocks), or one must be a sub-loop of the other. +* Loops in a function form a forest. One implication of this fact + is that a loop either has no parent or a single parent. + A loop may have an arbitrary number of exits, both explicit (via control flow) and implicit (via throwing calls which transfer control out of the containing function). There is no special requirement on diff --git a/include/llvm/Analysis/LoopInfo.h b/include/llvm/Analysis/LoopInfo.h index a01045124c7..824488e9ead 100644 --- a/include/llvm/Analysis/LoopInfo.h +++ b/include/llvm/Analysis/LoopInfo.h @@ -103,6 +103,14 @@ public: return D; } BlockT *getHeader() const { return getBlocks().front(); } + /// Return the parent loop if it exists or nullptr for top + /// level loops. + + /// A loop is either top-level in a function (that is, it is not + /// contained in any other loop) or it is entirely enclosed in + /// some other loop. + /// If a loop is top-level, it has no parent, otherwise its + /// parent is the innermost loop in which it is enclosed. LoopT *getParentLoop() const { return ParentLoop; } /// This is a raw interface for bypassing addChildLoop.