LLVM:getUniqueExitBlock() 的用法

LLVM: usage of getUniqueExitBlock()

如何在我的 llvm pass 中使用 LoopInfo.h 中的函数 Loops::getUniqueExitBlock()
我不明白,如何为特定循环调用此函数。

getUniqueExitBlock() 的文档指出:

getUniqueExitBlock - If getUniqueExitBlocks would return exactly one block, return that block.

Otherwise return null.

这告诉我们 getUniqueExitBlock() 是一种检查循环是否只有一个出口块的便捷方法。如果是,则 return 它 - 否则 return 为空。

假设您有办法获取 Loop 个对象,您可以通过执行类似

的操作来使用此函数
auto loop = get_loop() // you have to write this part
auto exit = loop->getUniqueExitBlock() // calls the function
if(exit != nullptr) {
    // exit points to the block that the loop jumps to when it exits
} else {
    // exit is null - this means that the loop either has more than one exit block or no exit blocks. deal with this case
}

可以找到所有 LLVM API 的文档 here. Sometimes the API isn't documented so well, as is the case with the new Attribute API. In this case, it's useful to look through the code itself for examples of the function being used within the LLVM codebase itself. For example, the documentation for getUniqueExitBlock() tells us that it's referenced internally by llvm::UnrollRuntimeLoopProlog()。这个例子可以帮助您了解如何正确使用 getUniqueExitBlock().