Skip to content

Commit 39709a3

Browse files
Added support for sub opcode instructions
1 parent ffc8203 commit 39709a3

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

Hpps.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ 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' and 'toValue'
28-
// from = to + toValue
27+
// 'from' is unknown, it depends on 'to', 'toValue', and 'toOps'
28+
// from = to toOps('+' or '-') toValue
2929
std::vector<Value *> from;
3030
std::vector<Value *> to;
3131
std::vector<int> toValue;
@@ -55,12 +55,13 @@ namespace
5555
// 'toValue' -> Operand1 int value
5656
if (auto *operInst = dyn_cast<BinaryOperator>(&I))
5757
{
58-
Value *oper0 = operInst->getOperand(0);
58+
// Value *oper0 = operInst->getOperand(0);
5959
Value *oper1 = operInst->getOperand(1);
6060

61+
// Store opcode ('+' or '-') to find real value range
6162
if (operInst->getOpcode() == Instruction::Add)
6263
{
63-
errs() << " -Stored:" << operInst->getName() << "\n";
64+
errs() << " -Stored(Add):" << operInst->getName() << "\n";
6465

6566
// Store reference of variable still to find range
6667
from.push_back(operInst);
@@ -76,6 +77,19 @@ namespace
7677
}
7778
else if (operInst->getOpcode() == Instruction::Sub)
7879
{
80+
errs() << " -Stored(Sub):" << operInst->getName() << "\n";
81+
82+
// Store reference of variable still to find range
83+
from.push_back(operInst);
84+
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
92+
loadRef.pop_back();
7993
}
8094
}
8195

@@ -126,27 +140,36 @@ namespace
126140

127141
// Print references
128142
errs() << "\nREFERENCES:\n";
129-
for (auto i = 0; i < from.size(); ++i)
143+
for (unsigned i = 0; i < from.size(); ++i)
130144
{
131145
errs() << to.at(i)->getName() << "+" << toValue.at(i) << "<-" << from.at(i)->getName() << "\n";
132146

133147
// Search variable reference inside already found ranges
134-
for (int v = 0; v < ranged.size(); ++v)
148+
for (unsigned v = 0; v < ranged.size(); ++v)
135149
{
136150
// When range found, add a new found range
137151
if (to.at(i) == ranged.at(v))
138152
{
139153
errs() << "-FOUND:" << ranged.at(v)->getName() << "\n";
140-
minRange.push_back(minRange.at(v) + toValue.at(i));
141-
maxRange.push_back(minRange.at(v) + toValue.at(i));
154+
if (toOps.at(i) == Instruction::Add)
155+
{
156+
minRange.push_back(minRange.at(v) + toValue.at(i));
157+
maxRange.push_back(minRange.at(v) + toValue.at(i));
158+
}
159+
else if (toOps.at(i) == Instruction::Sub)
160+
{
161+
minRange.push_back(minRange.at(v) - toValue.at(i));
162+
maxRange.push_back(minRange.at(v) - toValue.at(i));
163+
}
164+
142165
ranged.push_back(from.at(i));
143166
}
144167
}
145168
}
146169

147170
// Print value range computed
148-
errs() << "\nVALUE RANGES:\n";
149-
for (auto i = 0; i < minRange.size(); ++i)
171+
errs() << "\nVALUE RANGES\n";
172+
for (unsigned i = 0; i < minRange.size(); ++i)
150173
{
151174
errs() << ranged.at(i)->getName() << "(" << minRange.at(i) << ", " << maxRange.at(i) << ")\n";
152175
}

0 commit comments

Comments
 (0)