-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_const_value_from_store.cpp
More file actions
59 lines (50 loc) · 2.1 KB
/
Copy pathget_const_value_from_store.cpp
File metadata and controls
59 lines (50 loc) · 2.1 KB
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
//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements two versions of the LLVM "Hello World" pass described
// in docs/WritingAnLLVMPass.html
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
using namespace llvm;
#define DEBUG_TYPE "demo"
STATISTIC(HelloCounter, "Counts number of functions greeted");
namespace {
// Hello - The first implementation, without getAnalysisUsage.
struct Demo : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Demo() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
// int number_of_ins = 0;
++HelloCounter;
errs() << "Demo: ";
errs().write_escaped(F.getName()) << '\n';
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
errs() << " opcode of the instruction is : " << I.getOpcodeName()<< " ";
errs() << "Variable name : "<< I.getName()<<"\n"; // This gets the variable name
if (StoreInst *Store = dyn_cast<StoreInst>(&I)) // Checks if the instruction is a store inst
{
Value *StoredValue = Store->getValueOperand(); // gets the Operand value
if (ConstantInt *ConstantValue = dyn_cast<ConstantInt>(StoredValue)) { // Initially, the operand value was in hex, but we need to change in integer
errs() << "Constant value in store instruction: "<< ConstantValue->getValue() << "\n"; // This gives the constant Integer value
}}
}
}
return false;
}
};
} // namespace
char Demo::ID = 0;
static RegisterPass<Demo> X("demo", "This is a Demo pass");