-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello-llvm.cpp
64 lines (61 loc) · 1.93 KB
/
hello-llvm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//===----------------------------------------------------------------------===//
//
// Philipp Schubert
//
// Copyright (c) 2021
// GaZAR UG (haftungsbeschränkt)
// Bielefeld, Germany
// philipp@gazar.eu
//
//===----------------------------------------------------------------------===//
#include <memory>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
int main(int Argc, char **Argv) {
if (Argc != 2) {
llvm::outs() << "usage: <prog> <IR file>\n";
return 1;
}
// Parse an LLVM IR file
llvm::SMDiagnostic Diag;
llvm::LLVMContext Ctx;
std::unique_ptr<llvm::Module> M =
llvm::parseIRFile(Argv[1], Diag, Ctx); // NOLINT
// Check if the module is valid
bool BrokenDbgInfo = false;
if (llvm::verifyModule(*M, &llvm::errs(), &BrokenDbgInfo)) {
llvm::errs() << "error: invalid module\n";
return 1;
}
if (BrokenDbgInfo) {
llvm::errs() << "caution: debug info is broken\n";
}
// Iterate the IR and analyze the functions, basic blocks or individual
// instructions.
for (const auto &F : *M) {
llvm::outs() << "found function " << F.getName() << '\n';
for (const auto &BB : F) {
for (const auto &I : BB) {
if (const auto *Alloca = llvm::dyn_cast<llvm::AllocaInst>(&I)) {
llvm::outs() << "found alloca inst: " << *Alloca << '\n';
}
if (const auto *CallSite = llvm::dyn_cast<llvm::CallBase>(&I)) {
if (const auto *Callee = CallSite->getCalledFunction()) {
llvm::outs() << "found callee: " << Callee->getName() << '\n';
} else {
llvm::outs() << "found indirect callsite: " << *CallSite << '\n';
}
}
}
}
}
return 0;
}