chunhualiao / freeCompilerCamp

Goal: a website to automatically train and certify compiler researchers and developers
10 stars 1 forks source link

Add more details on llvm pass tutorial for `runOnFunction` #91

Closed aneesh-joshi closed 5 years ago

aneesh-joshi commented 5 years ago

It would be great if there was an explanation of what is going on in: https://github.com/chunhualiao/freeCompilerCamp/blob/master/compiler-classroom/_posts/2019-10-20-llvm-pass.markdown

In Section A.3

    bool runOnFunction(Function &F) override {
      errs() << "*";
      errs().write_escaped(F.getName()) << '\n';
      for (auto& B : F) {
        for (auto& I : B) {
          if(isa<CallInst>(&I)) {
            ImmutableCallSite CS(&I);
            if (const Function *F = CS.getCalledFunction()) {
              errs() << " |-" << F->getName() << "\n";
            }
          }
        }
      }
      return false;
    }
aneesh-joshi commented 5 years ago

Something like:

for (auto& B : F) { // for each basic block in the Function
        for (auto& I : B) { // for each Instruction in the Basic Block
          if(isa<CallInst>(&I)) { // if the instruction is a call instruction
            ImmutableCallSite CS(&I); // NOTSURE what this does
            if (const Function *F = CS.getCalledFunction()) {// NOTSURE
              errs() << " |-" << F->getName() << "\n";
            }
          }
almishra commented 5 years ago

Commit in - dd2f7f739fe17b6960c710f37573de39a2cc5592