@@ -77,6 +77,7 @@ namespace
77
77
// Get operands from binary operation
78
78
Value *oper0 = operInst->getOperand (0 );
79
79
Value *oper1 = operInst->getOperand (1 );
80
+ unsigned operCode = operInst->getOpcode ();
80
81
std::pair<int , int > rangeRef (infMin, infMax);
81
82
82
83
if (ConstantInt *CI0 = dyn_cast<ConstantInt>(oper0))
@@ -87,7 +88,7 @@ namespace
87
88
if (ConstantInt *CI1 = dyn_cast<ConstantInt>(oper1))
88
89
{
89
90
int constValue1 = CI1->getZExtValue ();
90
- int totConst = constValue0 + constValue1;
91
+ int totConst = binaryOperationResult (operCode, constValue0, constValue1) ;
91
92
rangeRef.first = totConst;
92
93
rangeRef.second = totConst;
93
94
}
@@ -97,15 +98,15 @@ namespace
97
98
std::map<Value *, std::pair<int , int >>::iterator valueRef = getValueReference (BB, oper1, &listRange);
98
99
if (valueRef->second .first != infMin)
99
100
{
100
- rangeRef.first = valueRef->second .first + constValue0 ;
101
+ rangeRef.first = binaryOperationResult (operCode, constValue0, valueRef->second .first ) ;
101
102
}
102
103
else
103
104
{
104
105
rangeRef.first = infMin;
105
106
}
106
107
if (valueRef->second .second != infMax)
107
108
{
108
- rangeRef.second = valueRef->second .second + constValue0 ;
109
+ rangeRef.second = binaryOperationResult (operCode, constValue0, valueRef->second .second ) ;
109
110
}
110
111
else
111
112
{
@@ -123,15 +124,15 @@ namespace
123
124
std::map<Value *, std::pair<int , int >>::iterator valueRef = getValueReference (BB, oper0, &listRange);
124
125
if (valueRef->second .first != infMin)
125
126
{
126
- rangeRef.first = valueRef->second .first + constValue1;
127
+ rangeRef.first = binaryOperationResult (operCode, valueRef->second .first , constValue1) ;
127
128
}
128
129
else
129
130
{
130
131
rangeRef.first = infMin;
131
132
}
132
133
if (valueRef->second .second != infMax)
133
134
{
134
- rangeRef.second = valueRef->second .second + constValue1;
135
+ rangeRef.second = binaryOperationResult (operCode, valueRef->second .second , constValue1) ;
135
136
}
136
137
else
137
138
{
@@ -190,12 +191,8 @@ namespace
190
191
191
192
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper1))
192
193
{
193
- if (pred == ICmpInst::ICMP_SLT)
194
- {
195
- errs () << oper->getName () << " < " << CI->getZExtValue () << " \n " ;
196
- rangeSuccessor0.second = CI->getZExtValue () - 1 ;
197
- rangeSuccessor1.first = CI->getZExtValue ();
198
- }
194
+ // Change range of successors based on reference, constant value, and predicate
195
+ computeCmpRange (true , pred, oper, CI->getZExtValue (), &rangeSuccessor0, &rangeSuccessor1);
199
196
}
200
197
}
201
198
else
@@ -205,12 +202,8 @@ namespace
205
202
206
203
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper0))
207
204
{
208
- if (pred == ICmpInst::ICMP_SLT)
209
- {
210
- errs () << CI->getZExtValue () << " < " << oper->getName () << " \n " ;
211
- rangeSuccessor0.first = CI->getZExtValue () + 1 ;
212
- rangeSuccessor1.second = CI->getZExtValue ();
213
- }
205
+ // Change range of successors based on reference, constant value, and predicate
206
+ computeCmpRange (false , pred, oper, CI->getZExtValue (), &rangeSuccessor0, &rangeSuccessor1);
214
207
}
215
208
}
216
209
@@ -440,6 +433,96 @@ namespace
440
433
range0.second == infMax ? range1.second : range1.second == infMax ? range0.second : std::max (range0.second , range1.second ));
441
434
}
442
435
436
+ // Compute binary operation (+ or -) result
437
+ int binaryOperationResult (unsigned binOper, int value1, int value2)
438
+ {
439
+ if (binOper == Instruction::Add)
440
+ {
441
+ return value1 + value2;
442
+ }
443
+ else if (binOper == Instruction::Sub)
444
+ {
445
+ return value1 - value2;
446
+ }
447
+
448
+ return value1 + value2;
449
+ }
450
+
451
+ // Computes ranges from CMP instruction (<, >, <=, >=)
452
+ void computeCmpRange (bool isRefOper0, ICmpInst::Predicate pred, Value *oper, int cmpValue, std::pair<int , int > *rangeSuccessor0, std::pair<int , int > *rangeSuccessor1)
453
+ {
454
+ if (pred == ICmpInst::ICMP_SLT)
455
+ {
456
+ // a < 1
457
+ if (isRefOper0)
458
+ {
459
+ errs () << oper->getName () << " < " << cmpValue << " \n " ;
460
+ rangeSuccessor0->second = cmpValue - 1 ;
461
+ rangeSuccessor1->first = cmpValue;
462
+ }
463
+ // 1 < a
464
+ else
465
+ {
466
+ errs () << cmpValue << " < " << oper->getName () << " \n " ;
467
+ rangeSuccessor0->first = cmpValue + 1 ;
468
+ rangeSuccessor1->second = cmpValue;
469
+ }
470
+ }
471
+ else if (pred == ICmpInst::ICMP_SLE)
472
+ {
473
+ // a <= 1
474
+ if (isRefOper0)
475
+ {
476
+ errs () << oper->getName () << " <= " << cmpValue << " \n " ;
477
+ rangeSuccessor0->second = cmpValue;
478
+ rangeSuccessor1->first = cmpValue + 1 ;
479
+ }
480
+ // 1 <= a
481
+ else
482
+ {
483
+ errs () << cmpValue << " <= " << oper->getName () << " \n " ;
484
+ rangeSuccessor0->first = cmpValue;
485
+ rangeSuccessor1->second = cmpValue - 1 ;
486
+ }
487
+ }
488
+ else if (pred == ICmpInst::ICMP_SGT)
489
+ {
490
+ // TODO
491
+ // // a > 1
492
+ // if (isRefOper0)
493
+ // {
494
+ // errs() << oper->getName() << " > " << cmpValue << "\n";
495
+ // rangeSuccessor0->second = cmpValue - 1;
496
+ // rangeSuccessor1->first = cmpValue;
497
+ // }
498
+ // // 1 > a
499
+ // else
500
+ // {
501
+ // errs() << cmpValue << " > " << oper->getName() << "\n";
502
+ // rangeSuccessor0->first = cmpValue + 1;
503
+ // rangeSuccessor1->second = cmpValue;
504
+ // }
505
+ }
506
+ else if (pred == ICmpInst::ICMP_SGE)
507
+ {
508
+ // TODO
509
+ // // a >= 1
510
+ // if (isRefOper0)
511
+ // {
512
+ // errs() << oper->getName() << " >= " << cmpValue << "\n";
513
+ // rangeSuccessor0->second = cmpValue - 1;
514
+ // rangeSuccessor1->first = cmpValue;
515
+ // }
516
+ // // 1 >= a
517
+ // else
518
+ // {
519
+ // errs() << cmpValue << " >= " << oper->getName() << "\n";
520
+ // rangeSuccessor0->first = cmpValue + 1;
521
+ // rangeSuccessor1->second = cmpValue;
522
+ // }
523
+ }
524
+ }
525
+
443
526
// Check if given BasicBlock is already visited in listRange
444
527
bool isAlreadyVisited (BasicBlock *next, std::map<BasicBlock *, std::map<Value *, std::pair<int , int >>> *listRange)
445
528
{
0 commit comments