Skip to content

Commit 8a68918

Browse files
Added both variable add/sub handler (a = b + c)
Not tested, it may not compile!
1 parent 07c18cb commit 8a68918

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

Hpps.cpp

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ namespace
2424
bool runOnFunction(Function &Func) override
2525
{
2626
// Keep reference of value still left to find the ranges
27-
// 'from' is unknown, it depends on 'to', 'toValue', and 'toOps'
28-
// from = to toOps('+' or '-') toValue
27+
// 'from' is unknown, it depends on 'to', 'toValue', and 'toOps' ('+' or '-')
28+
// from = to toOps toValue (a = b + 1)
29+
// from = toValue toOps to (a = 1 + b)
30+
// from = to toOps to2 (a = b + c)
2931
std::vector<Value *> from;
3032
std::vector<Value *> to;
3133
std::vector<int> toValue;
34+
std::vector<Value *> to2;
3235
std::vector<unsigned> toOps;
3336

3437
// Reference of variabiles for which range has been found
@@ -48,80 +51,85 @@ namespace
4851
// Print instruction
4952
errs() << I << "\n";
5053

54+
// When instruction is load, store reference to its variables
55+
if (auto *loadInst = dyn_cast<LoadInst>(&I))
56+
{
57+
for (Use &U : loadInst->operands())
58+
{
59+
Value *v = U.get();
60+
loadRef.push_back(v);
61+
errs() << " -Load:" << v->getName() << "\n";
62+
}
63+
}
64+
5165
// When instruction is Add or Sub
52-
// Save operand0 (variable) and operand1 (int)
53-
// 'to' -> Previous load instruction value
54-
// 'from' -> Current assigned variable value
55-
// 'toValue' -> Operand1 int value
66+
// Save operand0 (reference/constant) and operand1 (reference/constant)
5667
if (auto *operInst = dyn_cast<BinaryOperator>(&I))
5768
{
58-
// Value *oper0 = operInst->getOperand(0);
69+
Value *oper0 = operInst->getOperand(0);
5970
Value *oper1 = operInst->getOperand(1);
6071

61-
// Store opcode ('+' or '-') to find real value range
62-
if (operInst->getOpcode() == Instruction::Add)
63-
{
64-
errs() << " -Stored(Add):" << operInst->getName() << "\n";
72+
// Store reference of variable still to find range
73+
from.push_back(operInst);
74+
toOps.push_back(operInst->getOpcode()); // '+' ot '-'
6575

66-
// Store reference of variable still to find range
67-
from.push_back(operInst);
68-
to.push_back(loadRef.at(0));
69-
toOps.push_back(operInst->getOpcode());
70-
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper1))
71-
{
72-
toValue.push_back(CI->getZExtValue());
73-
}
76+
// Store constants/references
77+
errs() << " -Stored:" << operInst->getName() << "\n";
7478

75-
// Remove previous used load instruction value
79+
// a = b + 1 (variable + constant)
80+
// Get reference from previous load instruction
81+
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper1))
82+
{
83+
toValue.push_back(CI->getZExtValue());
84+
to.push_back(loadRef.at(0));
7685
loadRef.pop_back();
7786
}
78-
else if (operInst->getOpcode() == Instruction::Sub)
79-
{
80-
errs() << " -Stored(Sub):" << operInst->getName() << "\n";
8187

82-
// Store reference of variable still to find range
83-
from.push_back(operInst);
88+
// a = 1 + b (constant + variable)
89+
// Get reference from previous load instruction
90+
else if (ConstantInt *CI = dyn_cast<ConstantInt>(oper0))
91+
{
92+
toValue.push_back(CI->getZExtValue());
8493
to.push_back(loadRef.at(0));
85-
toOps.push_back(operInst->getOpcode());
86-
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper1))
87-
{
88-
toValue.push_back(CI->getZExtValue());
89-
}
90-
91-
// Remove previous used load instruction value
9294
loadRef.pop_back();
9395
}
94-
}
9596

96-
// When instruction is load, store reference to its variable
97-
if (auto *loadInst = dyn_cast<LoadInst>(&I))
98-
{
99-
for (Use &U : loadInst->operands())
97+
// a = b + c (variable + variable)
98+
// Get reference from two previous load instructions
99+
else
100100
{
101-
Value *v = U.get();
102-
loadRef.push_back(v);
101+
// TODO: It may be that there are x2 'to', create a struct to handle them both
102+
to.push_back(loadRef.at(0));
103+
to.push_back(loadRef.at(1));
104+
105+
// Remove previous used load instruction value
106+
loadRef.pop_back();
107+
loadRef.pop_back();
103108
}
104109
}
105110

106111
// When instruction is store
107112
if (auto *strInst = dyn_cast<StoreInst>(&I))
108113
{
109114
// Get operand0 (value) and operand1 (assigned)
115+
// operand1 = operand0
110116
Value *oper0 = strInst->getOperand(0);
111117
Value *oper1 = strInst->getOperand(1);
112118

113119
// Store reference to variable still to find range
120+
// a = b
114121
if (oper0->hasName())
115122
{
116123
errs() << " -Ref:" << oper0->getName() << "\n";
117-
from.push_back(oper1);
118-
to.push_back(oper0);
124+
from.push_back(oper1); // a
125+
to.push_back(oper0); // b
119126

120-
toValue.push_back(0);
121-
toOps.push_back(Instruction::Add);
127+
toValue.push_back(0); // Placeholder
128+
toOps.push_back(Instruction::Add); // Placeholder
122129
}
123130

124-
// Store constant range value
131+
// Store constant range value (Value-Range Found!)
132+
// a = 1
125133
else
126134
{
127135
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper0))
@@ -138,7 +146,7 @@ namespace
138146
}
139147
}
140148

141-
// Print references
149+
// Resolve references
142150
errs() << "\nREFERENCES:\n";
143151
for (unsigned i = 0; i < from.size(); ++i)
144152
{

0 commit comments

Comments
 (0)