How to write a custom intermodular pass in LLVM?

后端 未结 3 532
遇见更好的自我
遇见更好的自我 2020-12-31 11:33

I\'ve written a standard Analysis pass in LLVM, by extending the FunctionPass class. Everything seems to make sense.

Now what I\'d like to do is write a couple of in

3条回答
  •  青春惊慌失措
    2020-12-31 11:58

    This can be done using a module pass. Below is my code, and if you need help running it you can look here.

    bar.c

    int your_fun(int arg2) {
        int x = arg2;
        return x+2;
    }
    

    Skeleton.cpp

    #include "llvm/Pass.h"
    #include "llvm/IR/Module.h"
    #include "llvm/Support/raw_ostream.h"
    #include "llvm/IR/LegacyPassManager.h"
    #include "llvm/Transforms/IPO/PassManagerBuilder.h"
    using namespace llvm;
    
    namespace {
      struct SkeletonPass : public ModulePass {
        static char ID;
        SkeletonPass() : ModulePass(ID) {}
    
        virtual bool runOnModule(Module &M) {
            for (auto& F : M) {
                errs() << "\tFunction: " << F.getName() << "\n";
    
                for (auto& BB : F) {
                    errs() << "\t\tBasic Block: " << BB.getName() << "\n";
    
                    for (auto& I : BB) {
                        errs() << "\t\t\tInstruction: " << I.getOpcodeName() << "\n";
                    }
                }
            }
    
            return false;
        }
      };
    }
    
    char SkeletonPass::ID = 0;
    
    // Automatically enable the pass.
    // http://adriansampson.net/blog/clangpass.html
    static void registerSkeletonPass(const PassManagerBuilder &,
                             legacy::PassManagerBase &PM) {
      PM.add(new SkeletonPass());
    }
    
    static RegisterStandardPasses RegisterMyPass(PassManagerBuilder::EP_ModuleOptimizerEarly,
                                                    registerSkeletonPass);
    
    static RegisterStandardPasses RegisterMyPass1(PassManagerBuilder::EP_EnabledOnOptLevel0,
                                                    registerSkeletonPass);
    

    Output:

    | => clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.so foo.c bar.c
    Module: foo.c!
            Function: my_fun!
                Basicblock: entry!
                Instruction: alloca
                Instruction: alloca
                Instruction: store
                Instruction: load
                Instruction: store
                Instruction: load
                Instruction: add
                Instruction: ret
            Function: main!
                Basicblock: entry!
                Instruction: alloca
                Instruction: alloca
                Instruction: alloca
                Instruction: alloca
                Instruction: alloca
                Instruction: store
                Instruction: store
                Instruction: store
                Instruction: store
                Instruction: store
                Instruction: load
                Instruction: icmp
                Instruction: br
                Basicblock: if.then!
                Instruction: load
                Instruction: store
                Instruction: br
                Basicblock: if.else!
                Instruction: load
                Instruction: call
                Instruction: store
                Instruction: br
                Basicblock: if.end!
                Instruction: load
                Instruction: ret
    Module: bar.c!
            Function: your_fun!
                Basicblock: entry!
                Instruction: alloca
                Instruction: alloca
                Instruction: store
                Instruction: load
                Instruction: store
                Instruction: load
                Instruction: add
                Instruction: ret
    

    Output: If you include header file linking to bar.c

    Module: foo.c!
            Function: your_fun!
                Basicblock: entry!
                Instruction: alloca
                Instruction: alloca
                Instruction: store
                Instruction: load
                Instruction: store
                Instruction: load
                Instruction: add
                Instruction: ret
            Function: my_fun!
                Basicblock: entry!
                Instruction: alloca
                Instruction: alloca
                Instruction: store
                Instruction: load
                Instruction: store
                Instruction: load
                Instruction: add
                Instruction: ret
            Function: main!
                Basicblock: entry!
                Instruction: alloca
                Instruction: alloca
                Instruction: alloca
                Instruction: alloca
                Instruction: alloca
                Instruction: store
                Instruction: store
                Instruction: store
                Instruction: store
                Instruction: store
                Instruction: load
                Instruction: icmp
                Instruction: br
                Basicblock: if.then!
                Instruction: load
                Instruction: store
                Instruction: br
                Basicblock: if.else!
                Instruction: load
                Instruction: call
                Instruction: store
                Instruction: load
                Instruction: call
                Instruction: store
                Instruction: br
                Basicblock: if.end!
                Instruction: load
                Instruction: ret
    

提交回复
热议问题