Skip to content

Commit 3415650

Browse files
Completed successfully example bra3 and bra2
1 parent f711ee3 commit 3415650

File tree

2 files changed

+275
-17
lines changed

2 files changed

+275
-17
lines changed

BranchRange.cpp

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ namespace
7777
// Get operands from binary operation
7878
Value *oper0 = operInst->getOperand(0);
7979
Value *oper1 = operInst->getOperand(1);
80+
unsigned operCode = operInst->getOpcode();
8081
std::pair<int, int> rangeRef(infMin, infMax);
8182

8283
if (ConstantInt *CI0 = dyn_cast<ConstantInt>(oper0))
@@ -87,7 +88,7 @@ namespace
8788
if (ConstantInt *CI1 = dyn_cast<ConstantInt>(oper1))
8889
{
8990
int constValue1 = CI1->getZExtValue();
90-
int totConst = constValue0 + constValue1;
91+
int totConst = binaryOperationResult(operCode, constValue0, constValue1);
9192
rangeRef.first = totConst;
9293
rangeRef.second = totConst;
9394
}
@@ -97,15 +98,15 @@ namespace
9798
std::map<Value *, std::pair<int, int>>::iterator valueRef = getValueReference(BB, oper1, &listRange);
9899
if (valueRef->second.first != infMin)
99100
{
100-
rangeRef.first = valueRef->second.first + constValue0;
101+
rangeRef.first = binaryOperationResult(operCode, constValue0, valueRef->second.first);
101102
}
102103
else
103104
{
104105
rangeRef.first = infMin;
105106
}
106107
if (valueRef->second.second != infMax)
107108
{
108-
rangeRef.second = valueRef->second.second + constValue0;
109+
rangeRef.second = binaryOperationResult(operCode, constValue0, valueRef->second.second);
109110
}
110111
else
111112
{
@@ -123,15 +124,15 @@ namespace
123124
std::map<Value *, std::pair<int, int>>::iterator valueRef = getValueReference(BB, oper0, &listRange);
124125
if (valueRef->second.first != infMin)
125126
{
126-
rangeRef.first = valueRef->second.first + constValue1;
127+
rangeRef.first = binaryOperationResult(operCode, valueRef->second.first, constValue1);
127128
}
128129
else
129130
{
130131
rangeRef.first = infMin;
131132
}
132133
if (valueRef->second.second != infMax)
133134
{
134-
rangeRef.second = valueRef->second.second + constValue1;
135+
rangeRef.second = binaryOperationResult(operCode, valueRef->second.second, constValue1);
135136
}
136137
else
137138
{
@@ -190,12 +191,8 @@ namespace
190191

191192
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper1))
192193
{
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);
199196
}
200197
}
201198
else
@@ -205,12 +202,8 @@ namespace
205202

206203
if (ConstantInt *CI = dyn_cast<ConstantInt>(oper0))
207204
{
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);
214207
}
215208
}
216209

@@ -440,6 +433,96 @@ namespace
440433
range0.second == infMax ? range1.second : range1.second == infMax ? range0.second : std::max(range0.second, range1.second));
441434
}
442435

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+
443526
// Check if given BasicBlock is already visited in listRange
444527
bool isAlreadyVisited(BasicBlock *next, std::map<BasicBlock *, std::map<Value *, std::pair<int, int>>> *listRange)
445528
{

src/bra/bra2/bra2-result.txt

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
--- entry ---
2+
@Br-Simple
3+
entry to while.cond
4+
ADDED TO WORKLIST while.cond (sameRanges=1, wasVisited=0, isUpdated=0)
5+
6+
7+
--- while.cond ---
8+
@Phi: k.0 (, add2)
9+
ONE REFERENCE MISSING
10+
11+
@Cmp
12+
13+
@Br-Complex
14+
k.0 < 100
15+
Successor0: (-2147483648, 99)
16+
Successor1: (100, 2147483647)
17+
NEW VISITED ADDED: while.body
18+
NEW VISITED ADDED: while.end
19+
ADDED TO WORKLIST while.body (sameRanges=1, wasVisited=1, isUpdated=1)
20+
ADDED TO WORKLIST while.end (sameRanges=1, wasVisited=1, isUpdated=1)
21+
22+
23+
--- while.body ---
24+
@Cmp
25+
26+
@Br-Complex
27+
0 < k.0
28+
Successor0: (1, 99)
29+
Successor1: (-2147483648, 0)
30+
NEW VISITED ADDED: if.then
31+
NEW VISITED ADDED: if.end
32+
ADDED TO WORKLIST if.then (sameRanges=1, wasVisited=1, isUpdated=1)
33+
ADDED TO WORKLIST if.end (sameRanges=1, wasVisited=1, isUpdated=1)
34+
35+
36+
--- while.end ---
37+
38+
39+
--- if.then ---
40+
@Operation
41+
NEW REFERENCE VALUE: add (1, 1) into if.then
42+
43+
@Operation
44+
NEW REFERENCE VALUE: sub (0, 98) into if.then
45+
46+
@Br-Simple
47+
if.then to if.end
48+
49+
What about k.0?
50+
DIFFERENT? -> k.0: if.then (1, 99) - if.end (-2147483648, 0)
51+
YES! -> (1, 99)
52+
53+
What about add?
54+
NOW ADDED: add
55+
56+
What about sub?
57+
NOW ADDED: sub
58+
59+
60+
--- if.end ---
61+
@Operation
62+
NEW REFERENCE VALUE: add2 (2, 100) into if.end
63+
64+
@Br-Simple
65+
if.end to while.cond
66+
67+
What about k.0?
68+
NOW ADDED: k.0
69+
70+
What about add?
71+
NOW ADDED: add
72+
73+
What about sub?
74+
NOW ADDED: sub
75+
76+
What about add2?
77+
NOW ADDED: add2
78+
ADDED TO WORKLIST while.cond (sameRanges=0, wasVisited=1, isUpdated=0)
79+
80+
81+
--- while.cond ---
82+
@Phi: k.0 (, add2)
83+
New Range: (0, 100)
84+
85+
@Cmp
86+
87+
@Br-Complex
88+
k.0 < 100
89+
Successor0: (0, 99)
90+
Successor1: (100, 100)
91+
CHECK RANGE of k.0: (-2147483648, 99) to (0, 99)
92+
RANGE CHANGED: k.0 from while.body
93+
CHECK RANGE of k.0: (100, 2147483647) to (100, 100)
94+
RANGE CHANGED: k.0 from while.end
95+
ADDED TO WORKLIST while.body (sameRanges=1, wasVisited=1, isUpdated=1)
96+
ADDED TO WORKLIST while.end (sameRanges=1, wasVisited=1, isUpdated=1)
97+
98+
99+
--- while.body ---
100+
@Cmp
101+
102+
@Br-Complex
103+
0 < k.0
104+
Successor0: (1, 99)
105+
Successor1: (0, 0)
106+
CHECK RANGE of k.0: (1, 99) to (1, 99)
107+
CHECK RANGE of k.0: (1, 99) to (0, 0)
108+
RANGE CHANGED: k.0 from if.end
109+
ADDED TO WORKLIST if.end (sameRanges=1, wasVisited=1, isUpdated=1)
110+
111+
112+
--- while.end ---
113+
114+
115+
--- if.end ---
116+
@Operation
117+
NEW REFERENCE VALUE: add2 (1, 100) into if.end
118+
119+
@Br-Simple
120+
if.end to while.cond
121+
122+
What about k.0?
123+
DIFFERENT? -> k.0: if.end (0, 99) - while.cond (0, 100)
124+
YES! -> (0, 100)
125+
126+
What about add?
127+
DIFFERENT? -> add: if.end (1, 1) - while.cond (1, 1)
128+
129+
What about sub?
130+
DIFFERENT? -> sub: if.end (0, 98) - while.cond (0, 98)
131+
132+
What about add2?
133+
DIFFERENT? -> add2: if.end (1, 100) - while.cond (2, 100)
134+
YES! -> (1, 100)
135+
ADDED TO WORKLIST while.cond (sameRanges=0, wasVisited=1, isUpdated=0)
136+
137+
138+
--- while.cond ---
139+
@Phi: k.0 (, add2)
140+
New Range: (0, 100)
141+
142+
@Cmp
143+
144+
@Br-Complex
145+
k.0 < 100
146+
Successor0: (0, 99)
147+
Successor1: (100, 100)
148+
CHECK RANGE of k.0: (0, 99) to (0, 99)
149+
CHECK RANGE of k.0: (100, 100) to (100, 100)
150+
151+
--- VALUE-RANGES ---
152+
BB: entry
153+
154+
BB: while.cond
155+
k.0(0, 100) = 101 is 7bits
156+
add(1, 1) = 1 is 1bits
157+
sub(0, 98) = 99 is 7bits
158+
add2(1, 100) = 100 is 7bits
159+
160+
BB: if.end
161+
k.0(0, 99) = 100 is 7bits
162+
add(1, 1) = 1 is 1bits
163+
sub(0, 98) = 99 is 7bits
164+
add2(1, 100) = 100 is 7bits
165+
166+
BB: while.body
167+
k.0(0, 99) = 100 is 7bits
168+
169+
BB: while.end
170+
k.0(100, 100) = 1 is 1bits
171+
172+
BB: if.then
173+
k.0(1, 99) = 99 is 7bits
174+
add(1, 1) = 1 is 1bits
175+
sub(0, 98) = 99 is 7bits

0 commit comments

Comments
 (0)