FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LLVM-Examples/LLVMIRModify/LLVMCFGIRMod.cpp at master · codersguild/LLVM-Examples · GitHub
codersguild
/
LLVM-Examples
Public
forked from
lahiri-phdworks/LLVM-Examples
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LLVM-Examples
/
LLVMIRModify
/
LLVMCFGIRMod.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
123 lines (105 loc) · 3.86 KB
Breadcrumbs
LLVM-Examples
/
LLVMIRModify
/
LLVMCFGIRMod.cpp
Copy path
File metadata and controls
123 lines (105 loc) · 3.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#
include
"
llvm/Analysis/CFG.h
"
#
include
"
llvm/Analysis/DomTreeUpdater.h
"
#
include
"
llvm/Analysis/PostDominators.h
"
#
include
"
llvm/IR/IRBuilder.h
"
#
include
"
llvm/IR/InstIterator.h
"
#
include
"
llvm/IR/InstrTypes.h
"
#
include
"
llvm/IR/Instructions.h
"
#
include
"
llvm/IR/LLVMContext.h
"
#
include
"
llvm/IR/Module.h
"
#
include
"
llvm/IR/PassManager.h
"
#
include
"
llvm/Passes/PassBuilder.h
"
#
include
"
llvm/Passes/PassPlugin.h
"
#
include
"
llvm/Support/raw_ostream.h
"
#
include
"
llvm/Transforms/Utils/BasicBlockUtils.h
"
//
only needed for printing
#
include
<
assert.h
>
#
include
<
iostream
>
using
namespace
llvm
;
namespace
{
struct
ModifyBuildCFG
:
public
PassInfoMixin
<ModifyBuildCFG> {
PreservedAnalyses
run
(Function &F, FunctionAnalysisManager &
FAM
) {
if
(F.
getName
() !=
"
main
"
)
return
PreservedAnalyses::all
();
//
What precursors/pre-passes are needed. Define.
LoopInfo &getLI =
FAM
.
getResult
<llvm::LoopAnalysis>(F);
DominatorTree &
DT
=
FAM
.
getResult
<DominatorTreeAnalysis>(F);
PostDominatorTree &
PDT
=
FAM
.
getResult
<PostDominatorTreeAnalysis>(F);
/*
*
* @brief Loop Info Analysis
*/
if
(getLI.
empty
())
std::cout <<
"
No Loops !
\n\n
"
;
else
getLI.
print
(
errs
());
/*
*
* @brief Print Dominator Tree to llvm::errs()
*/
DT
.
print
(
errs
());
//
What we intend do do in the pass starts here.
int
found =
0
;
LLVMContext &Ctx = F.
getContext
();
Instruction *binop =
nullptr
;
ReversePostOrderTraversal<Function *>
RPOT
(&F);
for
(
auto
*
BB
:
RPOT
) {
errs
() <<
"
\t\t
Basic Block :
"
<<
BB
->
getName
().
str
() <<
"
\n
"
;
for
(
auto
&I : *
BB
) {
errs
() <<
"
\t
Operation :
"
<< I.
getOpcodeName
() <<
"
\n
"
;
//
Find the first "Add" Instruction.
if
(
strcmp
(I.
getOpcodeName
(),
"
add
"
) ==
0
) {
binop = &I;
found =
1
;
break
;
}
}
if
(found ==
1
)
break
;
}
//
Use the operands of a binary operator to
//
get the operands of Compare Not Equal Instruction.
if
(binop !=
NULL
) {
IRBuilder<>
Builder
(binop);
Value *lhs = binop->
getOperand
(
0
);
Value *rhs = binop->
getOperand
(
1
);
Value *xpv = Builder.
CreateAlloca
(
llvm::Type::getInt32Ty
(Ctx),
nullptr
,
"
new_alloca_1
"
);
Value *xpv2 = Builder.
CreateAlloca
(
llvm::Type::getInt32Ty
(Ctx),
nullptr
,
"
new_alloca_2
"
);
Value *add1 = Builder.
CreateAdd
(lhs, rhs);
Value *mul = Builder.
CreateMul
(lhs, rhs);
//
Modify CFG to add compare and source/target basic blocks.
Value *icmp1 = Builder.
CreateICmpEQ
(add1, mul,
"
equals_compare
"
);
Instruction *ThenTerm, *ElseTerm;
SplitBlockAndInsertIfThenElse
(icmp1, binop, &ThenTerm, &ElseTerm,
nullptr
);
Builder.
SetInsertPoint
(ThenTerm);
Value *xp1 = Builder.
CreateStore
(add1, xpv);
Builder.
SetInsertPoint
(ElseTerm);
Value *xp2 = Builder.
CreateStore
(mul, xpv2);
}
ReversePostOrderTraversal<Function *>
RPOT1
(&F);
for
(
auto
*
BB
:
RPOT1
) {
errs
() <<
"
\t\t
Basic Block :
"
<<
BB
->
getName
().
str
() <<
"
\n
"
;
}
//
Preserve previous analysis in the pass. Like Dominator information.
//
THe pass we made does not modify that.
return
PreservedAnalyses::none
();
}
};
}
//
namespace
//
Registering the pass.
extern
"
C
"
::llvm::PassPluginLibraryInfo
LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo
() {
return
{
LLVM_PLUGIN_API_VERSION
,
"
ModifyBuildCFG
"
,
"
v0.1
"
,
[](PassBuilder &
PB
) {
PB
.
registerPipelineParsingCallback
(
[](StringRef Name, FunctionPassManager &
FPM
,
ArrayRef<PassBuilder::PipelineElement>) {
if
(Name ==
"
modifyncfg
"
) {
FPM
.
addPass
(
ModifyBuildCFG
());
return
true
;
}
return
false
;
});
}};
}
Back
|
FazBrowse Home
|
New Git URL