LLVM入门与CSCD70的Assignments

llvm

Tutorial2 introduction to LLVM Ⅱ

操作基本指令依靠 instruction class, basic block class or BasicBlockUtils class。

User-Use-Value

我们如何确保所有引用(i.e. uses)会被正确的更新?User-Use-Value relationship 用来解决这个问题。

LLVM 有一个很重要的基类:Value。几乎所有的 object types 都是继承于此。

Assignment1

Function Information

FunctionInfoPass 中的 run 函数的 todo 部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
outs() << "CSCD70 Function Information Pass"
<< "\n";

/// @todo(CSCD70) Please complete this method.
for (auto iter = M.begin(); iter != M.end(); ++iter) {
Function &func = *iter;
outs() << "Function name: " << func.getName() << "\n";
outs() << "Number of Arguments: " << func.arg_size() << "\n";
outs() << "Number of Calls: " << func.getNumUses() << "\n";
outs() << "Number OF BBs: " << func.size() << "\n";
int funcInstrSize = 0;
for (BasicBlock &BB : func) {
funcInstrSize += BB.size();
}
outs() << "Number of Instructions: " << funcInstrSize << "\n\n";
}

return PreservedAnalyses::all();
}
1
2
$ clang -O0 -emit-llvm -c ./test/Loop.c -o ./test/Loop.bc
$ opt -load-pass-plugin=./libFunctionInfo.so -passes=function-info ./test/Loop.bc -o ./test/LoopFunctionInfo.bc

参考文章

  1. 代码优化与LLVM IR pass | Kiprey’s Blog
  2. eternalsakura/sakura_llvm_opt: 15-745 note (github.com)
  3. About — LLVM 17.0.0git documentation
  4. UofT-EcoSystem/CSCD70: CSCD70 Compiler Optimization (github.com)