LLVM LoopPass检测出站边缘

时间:2018-06-21 12:33:20

标签: loops llvm

我有一个简单的LLVM LoopPass,试图检查是否 循环可以在条件以外的其他地方退出。 假定不涉及任何子循环。 我正在使用的文件main.c有四个循环,第一个和最后一个循环为“好”,第二个和第三个循环为“不良”。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *foo(char *s)
{
    while (*s) { s++;                              }
    while (*s) { s++; if (*s == 'a') { break;    } }
    while (*s) { s++; if (*s == 'a') { goto TTT; } }
    while (*s) { s++; if (*s == 'a') { continue; } }
TTT:
    return NULL;
}

int main(int argc, char **argv)
{
    char *s = malloc(10);
    foo(s);
}

这是我写的LoopPass:

struct StaticAnalyzer : public LoopPass
{
    static char ID;
    StaticAnalyzer():LoopPass(ID){}

    virtual bool runOnLoop(Loop *loop, LPPassManager &LPM)
    {
        int numExits=0;
        for (auto it = loop->block_begin(); it != loop->block_end(); it++)
        {
            if (loop->isLoopExiting(*it)) { numExits++; }
        }
        if (numExits > 1) errs() << "BAD \n";
        else              errs() << "GOOD\n";

        return false;
    }
};

运行附带的通行证时,我得到正确的结果:

GOOD
BAD
BAD
GOOD

但是我想知道是否有更简单的方法?谢谢!

0 个答案:

没有答案