'How to run my analysis pass on optimized ir

I write a driver to run pass in my code.


void MyPassManager::addNewFuncPassByCode(string PassCode,
                                           FunctionPassManager &FPM, FunctionAnalysisManager &FAM){
#define GET_NEW_FUNC_PASS
#define FUNCPASS(CODE, CLASS)                 \
              if(PassCode == CODE)              \
                register##CLASS##Pass(FPM, FAM);
#include "PassDef.inc"
#undef FUNCPASS
#undef GET_NEW_FUNC_PASS
}

void MyPassDriver(){
    //...
    PassBuilder PB;

    LoopAnalysisManager LAM;
    FunctionAnalysisManager FAM;
    CGSCCAnalysisManager CGAM;
    ModuleAnalysisManager MAM;
    AAManager AA;
    if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) {
        spdlog::warn("parseAAPipeline error: {} ", toString(std::move(Err)));
        return;
    }
    PB.registerModuleAnalyses(MAM);
    PB.registerCGSCCAnalyses(CGAM);
    PB.registerFunctionAnalyses(FAM);
    PB.registerLoopAnalyses(LAM);
    PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);

    ModulePassManager MPM;
    FunctionPassManager FPM;

    FAM.registerPass([&] { return std::move(AA); });

    Triple ModuleTriple(M.getTargetTriple());
    TargetLibraryInfoImpl TLII(ModuleTriple);
    FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });

    addNewFuncPassByCode(code, FPM, FAM);

    MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));


    MPM.run(M, MAM);
}

I register pass by FunctionPassManager.addPass(...)

void registerRedundantCondPass(llvm::FunctionPassManager &FPM, llvm::FunctionAnalysisManager &FAM) {
    spdlog::debug("registerRedundantCondPass called here!");
    FPM.addPass(SROA());
    // Catch trivial redundancies
    FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
    // My custom analysis pass to detect some pattern
    FPM.addPass(MyCustomPass());
}

I find that my analysis pass sometimes does not run on optimized ir(here is --sroa --early-cse-mem), but it sometimes can run on optimized ir, I am confused about it.

for example if I use opt --sroa -early-cse-mem, the block in IR will be optimized like:

if.then:                                          ; preds = %entry
  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)), !dbg !984
  br i1 true, label %if.then5, label %if.end, !dbg !986

my analysis pass sometimes seems like still run on the block without optimization:

if.then:                                          ; preds = %entry
  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)), !dbg !984
  %elem2 = getelementptr inbounds %struct.MMM, %struct.MMM* %M1, i32 0, i32 2, !dbg !986
  %2 = load i32, i32* %elem2, align 8, !dbg !986
  %elem3 = getelementptr inbounds %struct.MMM, %struct.MMM* %M2, i32 0, i32 2, !dbg !988
  %3 = load i32, i32* %elem3, align 8, !dbg !988
  %cmp4 = icmp eq i32 %2, %3, !dbg !989
  br i1 %cmp4, label %if.then5, label %if.end, !dbg !990

Is there anything wrong with my driver , or I can't just use FPM.addPass to determine the sequence and dependency of my analysis pass?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source