| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.Lists; |
| 20 |
import com.google.common.collect.Maps; |
| 21 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 22 |
import com.google.javascript.jscomp.DataFlowAnalysis.BranchedFlowState; |
| 23 |
import com.google.javascript.jscomp.DataFlowAnalysis.BranchedForwardDataFlowAnalysis; |
| 24 |
import com.google.javascript.jscomp.DataFlowAnalysis.FlowState; |
| 25 |
import com.google.javascript.jscomp.DataFlowAnalysis.MaxIterationsExceededException; |
| 26 |
import com.google.javascript.jscomp.JoinOp.BinaryJoinOp; |
| 27 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 28 |
import com.google.javascript.jscomp.graph.GraphNode; |
| 29 |
import com.google.javascript.jscomp.graph.LatticeElement; |
| 30 |
|
| 31 |
import junit.framework.TestCase; |
| 32 |
|
| 33 |
import java.util.Comparator; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Map; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class DataFlowAnalysisTest extends TestCase { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
enum Operation { |
| 50 |
ADD("+"), SUB("-"), DIV("/"), MUL("*"); |
| 51 |
private final String stringRep; |
| 52 |
|
| 53 |
private Operation(String stringRep) { |
| 54 |
this.stringRep = stringRep; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public String toString() { |
| 59 |
return stringRep; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
abstract static class Value { |
| 67 |
|
| 68 |
boolean isNumber() { |
| 69 |
return this instanceof Number; |
| 70 |
} |
| 71 |
|
| 72 |
boolean isVariable() { |
| 73 |
return this instanceof Variable; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static class Variable extends Value { |
| 81 |
private String name; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
Variable(String n) { |
| 89 |
name = n; |
| 90 |
} |
| 91 |
|
| 92 |
String getName() { |
| 93 |
return name; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public boolean equals(Object other) { |
| 98 |
|
| 99 |
if (!(other instanceof Variable)) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
return ((Variable) other).name.equals(name); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public int hashCode() { |
| 107 |
return name.hashCode(); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public String toString() { |
| 112 |
return this.name; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static class Number extends Value { |
| 120 |
private int value; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
Number(int v) { |
| 128 |
value = v; |
| 129 |
} |
| 130 |
|
| 131 |
int getValue() { |
| 132 |
return value; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public String toString() { |
| 137 |
return "" + value; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public int hashCode() { |
| 142 |
return value; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
abstract static class Instruction { |
| 150 |
|
| 151 |
int order = 0; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
boolean isArithmetic() { |
| 159 |
return this instanceof ArithmeticInstruction; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
boolean isBranch() { |
| 168 |
return this instanceof BranchInstruction; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static class ArithmeticInstruction extends Instruction { |
| 180 |
private Operation operation; |
| 181 |
private Value operand1; |
| 182 |
private Value operand2; |
| 183 |
private Variable result; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
ArithmeticInstruction(Variable res, int op1, Operation o, int op2) { |
| 194 |
this(res, new Number(op1), o, new Number(op2)); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
ArithmeticInstruction(Variable res, Value op1, Operation o, int op2) { |
| 206 |
this(res, op1, o, new Number(op2)); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
ArithmeticInstruction(Variable res, int op1, Operation o, Value op2) { |
| 218 |
this(res, new Number(op1), o, op2); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
ArithmeticInstruction(Variable res, Value op1, Operation o, Value op2) { |
| 230 |
result = res; |
| 231 |
operand1 = op1; |
| 232 |
operand2 = op2; |
| 233 |
operation = o; |
| 234 |
} |
| 235 |
|
| 236 |
Operation getOperator() { |
| 237 |
return operation; |
| 238 |
} |
| 239 |
|
| 240 |
void setOperator(Operation op) { |
| 241 |
this.operation = op; |
| 242 |
} |
| 243 |
|
| 244 |
Value getOperand1() { |
| 245 |
return operand1; |
| 246 |
} |
| 247 |
|
| 248 |
void setOperand1(Value operand1) { |
| 249 |
this.operand1 = operand1; |
| 250 |
} |
| 251 |
|
| 252 |
Value getOperand2() { |
| 253 |
return operand2; |
| 254 |
} |
| 255 |
|
| 256 |
void setOperand2(Value operand2) { |
| 257 |
this.operand2 = operand2; |
| 258 |
} |
| 259 |
|
| 260 |
Variable getResult() { |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
void setResult(Variable result) { |
| 265 |
this.result = result; |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public String toString() { |
| 270 |
StringBuilder out = new StringBuilder(); |
| 271 |
out.append(result); |
| 272 |
out.append(" = "); |
| 273 |
out.append(operand1); |
| 274 |
out.append(operation); |
| 275 |
out.append(operand2); |
| 276 |
return out.toString(); |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public int hashCode() { |
| 281 |
return toString().hashCode(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public static ArithmeticInstruction |
| 286 |
newAssignNumberToVariableInstruction(Variable res, int num) { |
| 287 |
return new ArithmeticInstruction(res, num, Operation.ADD, 0); |
| 288 |
} |
| 289 |
|
| 290 |
public static ArithmeticInstruction |
| 291 |
newAssignVariableToVariableInstruction(Variable lhs, Variable rhs) { |
| 292 |
return new ArithmeticInstruction(lhs, rhs, Operation.ADD, 0); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static class BranchInstruction extends Instruction { |
| 299 |
private Value condition; |
| 300 |
|
| 301 |
BranchInstruction(Value cond) { |
| 302 |
condition = cond; |
| 303 |
} |
| 304 |
|
| 305 |
Value getCondition() { |
| 306 |
return condition; |
| 307 |
} |
| 308 |
|
| 309 |
void setCondition(Value condition) { |
| 310 |
this.condition = condition; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private static class ConstPropLatticeElement implements LatticeElement { |
| 334 |
private final Map<Variable, Integer> constMap; |
| 335 |
private final boolean isTop; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
ConstPropLatticeElement(boolean isTop) { |
| 343 |
this.isTop = isTop; |
| 344 |
this.constMap = Maps.newHashMap(); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
ConstPropLatticeElement() { |
| 351 |
this(false); |
| 352 |
} |
| 353 |
|
| 354 |
ConstPropLatticeElement(ConstPropLatticeElement other) { |
| 355 |
this.isTop = other.isTop; |
| 356 |
this.constMap = Maps.newHashMap(other.constMap); |
| 357 |
} |
| 358 |
|
| 359 |
@Override |
| 360 |
public String toString() { |
| 361 |
if (isTop) { |
| 362 |
return "TOP"; |
| 363 |
} |
| 364 |
StringBuilder out = new StringBuilder(); |
| 365 |
|
| 366 |
out.append("{"); |
| 367 |
for (Variable var : constMap.keySet()) { |
| 368 |
out.append(var); |
| 369 |
out.append("="); |
| 370 |
out.append(constMap.get(var)); |
| 371 |
out.append(" "); |
| 372 |
} |
| 373 |
out.append("}"); |
| 374 |
return out.toString(); |
| 375 |
} |
| 376 |
|
| 377 |
@Override |
| 378 |
public boolean equals(Object other) { |
| 379 |
if (other instanceof ConstPropLatticeElement) { |
| 380 |
ConstPropLatticeElement otherLattice = (ConstPropLatticeElement) other; |
| 381 |
return (this.isTop == otherLattice.isTop) && |
| 382 |
this.constMap.equals(otherLattice.constMap); |
| 383 |
} |
| 384 |
return false; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
private static class ConstPropJoinOp |
| 389 |
extends BinaryJoinOp<ConstPropLatticeElement> { |
| 390 |
|
| 391 |
@Override |
| 392 |
public ConstPropLatticeElement apply(ConstPropLatticeElement a, |
| 393 |
ConstPropLatticeElement b) { |
| 394 |
ConstPropLatticeElement result = new ConstPropLatticeElement(); |
| 395 |
|
| 396 |
if (a.isTop) { |
| 397 |
return new ConstPropLatticeElement(a); |
| 398 |
} |
| 399 |
if (b.isTop) { |
| 400 |
return new ConstPropLatticeElement(b); |
| 401 |
} |
| 402 |
|
| 403 |
for (Variable var : a.constMap.keySet()) { |
| 404 |
if (b.constMap.containsKey(var)) { |
| 405 |
Integer number = b.constMap.get(var); |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if (a.constMap.get(var).equals(number)) { |
| 410 |
result.constMap.put(var, number); |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
return result; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
static class DummyConstPropagation extends |
| 422 |
DataFlowAnalysis<Instruction, ConstPropLatticeElement> { |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
DummyConstPropagation(ControlFlowGraph<Instruction> targetCfg) { |
| 430 |
super(targetCfg, new ConstPropJoinOp()); |
| 431 |
} |
| 432 |
|
| 433 |
@Override |
| 434 |
boolean isForward() { |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
@Override |
| 439 |
ConstPropLatticeElement flowThrough(Instruction node, |
| 440 |
ConstPropLatticeElement input) { |
| 441 |
if (node.isBranch()) { |
| 442 |
return new ConstPropLatticeElement(input); |
| 443 |
} else { |
| 444 |
return flowThroughArithmeticInstruction((ArithmeticInstruction) node, |
| 445 |
input); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
@Override |
| 450 |
ConstPropLatticeElement createEntryLattice() { |
| 451 |
return new ConstPropLatticeElement(); |
| 452 |
} |
| 453 |
|
| 454 |
@Override |
| 455 |
ConstPropLatticeElement createInitialEstimateLattice() { |
| 456 |
return new ConstPropLatticeElement(true); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
static ConstPropLatticeElement flowThroughArithmeticInstruction( |
| 461 |
ArithmeticInstruction aInst, ConstPropLatticeElement input) { |
| 462 |
|
| 463 |
ConstPropLatticeElement out = new ConstPropLatticeElement(input); |
| 464 |
|
| 465 |
|
| 466 |
Integer leftConst = null; |
| 467 |
if (aInst.operand1.isNumber()) { |
| 468 |
leftConst = ((Number) aInst.operand1).value; |
| 469 |
} else { |
| 470 |
if (input.constMap.containsKey(aInst.operand1)) { |
| 471 |
leftConst = input.constMap.get(aInst.operand1); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
Integer rightConst = null; |
| 477 |
if (aInst.operand2.isNumber()) { |
| 478 |
rightConst = ((Number) aInst.operand2).value; |
| 479 |
} else { |
| 480 |
if (input.constMap.containsKey(aInst.operand2)) { |
| 481 |
rightConst = input.constMap.get(aInst.operand2); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
if (leftConst != null && rightConst != null) { |
| 487 |
Integer constResult = null; |
| 488 |
if (aInst.operation == Operation.ADD) { |
| 489 |
constResult = leftConst.intValue() + rightConst.intValue(); |
| 490 |
} else if (aInst.operation == Operation.SUB) { |
| 491 |
constResult = leftConst.intValue() - rightConst.intValue(); |
| 492 |
} else if (aInst.operation == Operation.MUL) { |
| 493 |
constResult = leftConst.intValue() * rightConst.intValue(); |
| 494 |
} else if (aInst.operation == Operation.DIV) { |
| 495 |
constResult = leftConst.intValue() / rightConst.intValue(); |
| 496 |
} |
| 497 |
|
| 498 |
out.constMap.put(aInst.result, constResult); |
| 499 |
} else { |
| 500 |
|
| 501 |
out.constMap.remove(aInst.result); |
| 502 |
} |
| 503 |
return out; |
| 504 |
} |
| 505 |
|
| 506 |
public void testSimpleIf() { |
| 507 |
|
| 508 |
Variable a = new Variable("a"); |
| 509 |
Variable b = new Variable("b"); |
| 510 |
Variable c = new Variable("c"); |
| 511 |
Instruction inst1 = new BranchInstruction(a); |
| 512 |
Instruction inst2 = newAssignNumberToVariableInstruction(b, 1); |
| 513 |
Instruction inst3 = newAssignNumberToVariableInstruction(b, 1); |
| 514 |
Instruction inst4 = newAssignVariableToVariableInstruction(c, b); |
| 515 |
ControlFlowGraph<Instruction> cfg = |
| 516 |
new ControlFlowGraph<Instruction>(inst1, true, true); |
| 517 |
GraphNode<Instruction, Branch> n1 = cfg.createNode(inst1); |
| 518 |
GraphNode<Instruction, Branch> n2 = cfg.createNode(inst2); |
| 519 |
GraphNode<Instruction, Branch> n3 = cfg.createNode(inst3); |
| 520 |
GraphNode<Instruction, Branch> n4 = cfg.createNode(inst4); |
| 521 |
cfg.connect(inst1, ControlFlowGraph.Branch.ON_FALSE, inst2); |
| 522 |
cfg.connect(inst1, ControlFlowGraph.Branch.ON_TRUE, inst3); |
| 523 |
cfg.connect(inst2, ControlFlowGraph.Branch.UNCOND, inst4); |
| 524 |
cfg.connect(inst3, ControlFlowGraph.Branch.UNCOND, inst4); |
| 525 |
|
| 526 |
DummyConstPropagation constProp = new DummyConstPropagation(cfg); |
| 527 |
constProp.analyze(); |
| 528 |
|
| 529 |
|
| 530 |
verifyInHas(n1, a, null); |
| 531 |
verifyInHas(n1, b, null); |
| 532 |
verifyInHas(n1, c, null); |
| 533 |
verifyOutHas(n1, a, null); |
| 534 |
verifyOutHas(n1, b, null); |
| 535 |
verifyOutHas(n1, c, null); |
| 536 |
|
| 537 |
|
| 538 |
verifyInHas(n2, a, null); |
| 539 |
verifyInHas(n2, b, null); |
| 540 |
verifyInHas(n2, c, null); |
| 541 |
verifyOutHas(n2, a, null); |
| 542 |
verifyOutHas(n2, b, 1); |
| 543 |
verifyOutHas(n2, c, null); |
| 544 |
|
| 545 |
|
| 546 |
verifyInHas(n3, a, null); |
| 547 |
verifyInHas(n3, b, null); |
| 548 |
verifyInHas(n3, c, null); |
| 549 |
verifyOutHas(n3, a, null); |
| 550 |
verifyOutHas(n3, b, 1); |
| 551 |
verifyOutHas(n3, c, null); |
| 552 |
|
| 553 |
|
| 554 |
verifyInHas(n4, a, null); |
| 555 |
verifyInHas(n4, b, 1); |
| 556 |
verifyInHas(n4, c, null); |
| 557 |
verifyOutHas(n4, a, null); |
| 558 |
|
| 559 |
verifyOutHas(n4, b, 1); |
| 560 |
verifyOutHas(n4, c, 1); |
| 561 |
} |
| 562 |
|
| 563 |
public void testSimpleLoop() { |
| 564 |
|
| 565 |
Variable a = new Variable("a"); |
| 566 |
Variable b = new Variable("b"); |
| 567 |
Variable c = new Variable("c"); |
| 568 |
Instruction inst1 = newAssignNumberToVariableInstruction(a, 0); |
| 569 |
Instruction inst2 = new ArithmeticInstruction(a, a, Operation.ADD, 1); |
| 570 |
Instruction inst3 = new BranchInstruction(b); |
| 571 |
Instruction inst4 = newAssignVariableToVariableInstruction(c, a); |
| 572 |
ControlFlowGraph<Instruction> cfg = |
| 573 |
new ControlFlowGraph<Instruction>(inst1, true, true); |
| 574 |
GraphNode<Instruction, Branch> n1 = cfg.createNode(inst1); |
| 575 |
GraphNode<Instruction, Branch> n2 = cfg.createNode(inst2); |
| 576 |
GraphNode<Instruction, Branch> n3 = cfg.createNode(inst3); |
| 577 |
GraphNode<Instruction, Branch> n4 = cfg.createNode(inst4); |
| 578 |
cfg.connect(inst1, ControlFlowGraph.Branch.UNCOND, inst2); |
| 579 |
cfg.connect(inst2, ControlFlowGraph.Branch.UNCOND, inst3); |
| 580 |
cfg.connect(inst3, ControlFlowGraph.Branch.ON_TRUE, inst2); |
| 581 |
cfg.connect(inst3, ControlFlowGraph.Branch.ON_FALSE, inst4); |
| 582 |
|
| 583 |
DummyConstPropagation constProp = new DummyConstPropagation(cfg); |
| 584 |
|
| 585 |
constProp.analyze(); |
| 586 |
|
| 587 |
|
| 588 |
verifyInHas(n1, a, null); |
| 589 |
verifyInHas(n1, b, null); |
| 590 |
verifyInHas(n1, c, null); |
| 591 |
verifyOutHas(n1, a, 0); |
| 592 |
verifyOutHas(n1, b, null); |
| 593 |
verifyOutHas(n1, c, null); |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
verifyInHas(n2, a, null); |
| 598 |
verifyInHas(n2, b, null); |
| 599 |
verifyInHas(n2, c, null); |
| 600 |
verifyOutHas(n2, a, null); |
| 601 |
verifyOutHas(n2, b, null); |
| 602 |
verifyOutHas(n2, c, null); |
| 603 |
|
| 604 |
verifyInHas(n3, a, null); |
| 605 |
verifyInHas(n3, b, null); |
| 606 |
verifyInHas(n3, c, null); |
| 607 |
verifyOutHas(n3, a, null); |
| 608 |
verifyOutHas(n3, b, null); |
| 609 |
verifyOutHas(n3, c, null); |
| 610 |
|
| 611 |
verifyInHas(n4, a, null); |
| 612 |
verifyInHas(n4, b, null); |
| 613 |
verifyInHas(n4, c, null); |
| 614 |
verifyOutHas(n4, a, null); |
| 615 |
verifyOutHas(n4, b, null); |
| 616 |
verifyOutHas(n4, c, null); |
| 617 |
} |
| 618 |
|
| 619 |
public void testLatticeArrayMinimizationWhenMidpointIsEven() { |
| 620 |
assertEquals(6, JoinOp.BinaryJoinOp.computeMidPoint(12)); |
| 621 |
} |
| 622 |
|
| 623 |
public void testLatticeArrayMinimizationWhenMidpointRoundsDown() { |
| 624 |
assertEquals(8, JoinOp.BinaryJoinOp.computeMidPoint(18)); |
| 625 |
} |
| 626 |
|
| 627 |
public void testLatticeArrayMinimizationWithTwoElements() { |
| 628 |
assertEquals(1, JoinOp.BinaryJoinOp.computeMidPoint(2)); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
static class BranchedDummyConstPropagation extends |
| 636 |
BranchedForwardDataFlowAnalysis<Instruction, ConstPropLatticeElement> { |
| 637 |
|
| 638 |
BranchedDummyConstPropagation(ControlFlowGraph<Instruction> targetCfg) { |
| 639 |
super(targetCfg, new ConstPropJoinOp()); |
| 640 |
} |
| 641 |
|
| 642 |
@Override |
| 643 |
ConstPropLatticeElement flowThrough(Instruction node, |
| 644 |
ConstPropLatticeElement input) { |
| 645 |
if (node.isArithmetic()) { |
| 646 |
return flowThroughArithmeticInstruction( |
| 647 |
(ArithmeticInstruction) node, input); |
| 648 |
} else { |
| 649 |
return new ConstPropLatticeElement(input); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
@Override |
| 654 |
List<ConstPropLatticeElement> branchedFlowThrough(Instruction node, |
| 655 |
ConstPropLatticeElement input) { |
| 656 |
List<ConstPropLatticeElement> result = Lists.newArrayList(); |
| 657 |
List<DiGraphEdge<Instruction, Branch>> outEdges = |
| 658 |
getCfg().getOutEdges(node); |
| 659 |
if (node.isArithmetic()) { |
| 660 |
assertTrue(outEdges.size() < 2); |
| 661 |
ConstPropLatticeElement aResult = flowThroughArithmeticInstruction( |
| 662 |
(ArithmeticInstruction) node, input); |
| 663 |
for (int i = 0; i < outEdges.size(); i++) { |
| 664 |
result.add(aResult); |
| 665 |
} |
| 666 |
} else { |
| 667 |
BranchInstruction branchInst = (BranchInstruction) node; |
| 668 |
for (DiGraphEdge<Instruction, Branch> branch : outEdges) { |
| 669 |
ConstPropLatticeElement edgeResult = |
| 670 |
new ConstPropLatticeElement(input); |
| 671 |
if (branch.getValue() == Branch.ON_FALSE && |
| 672 |
branchInst.getCondition().isVariable()) { |
| 673 |
edgeResult.constMap.put((Variable) branchInst.getCondition(), 0); |
| 674 |
} |
| 675 |
result.add(edgeResult); |
| 676 |
} |
| 677 |
} |
| 678 |
return result; |
| 679 |
} |
| 680 |
|
| 681 |
@Override |
| 682 |
ConstPropLatticeElement createEntryLattice() { |
| 683 |
return new ConstPropLatticeElement(); |
| 684 |
} |
| 685 |
|
| 686 |
@Override |
| 687 |
ConstPropLatticeElement createInitialEstimateLattice() { |
| 688 |
return new ConstPropLatticeElement(true); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
public void testBranchedSimpleIf() { |
| 693 |
|
| 694 |
Variable a = new Variable("a"); |
| 695 |
Variable b = new Variable("b"); |
| 696 |
Variable c = new Variable("c"); |
| 697 |
Instruction inst1 = new BranchInstruction(a); |
| 698 |
Instruction inst2 = newAssignNumberToVariableInstruction(a, 0); |
| 699 |
Instruction inst3 = newAssignNumberToVariableInstruction(b, 0); |
| 700 |
Instruction inst4 = newAssignVariableToVariableInstruction(c, b); |
| 701 |
ControlFlowGraph<Instruction> cfg = |
| 702 |
new ControlFlowGraph<Instruction>(inst1, true, true); |
| 703 |
GraphNode<Instruction, Branch> n1 = cfg.createNode(inst1); |
| 704 |
GraphNode<Instruction, Branch> n2 = cfg.createNode(inst2); |
| 705 |
GraphNode<Instruction, Branch> n3 = cfg.createNode(inst3); |
| 706 |
GraphNode<Instruction, Branch> n4 = cfg.createNode(inst4); |
| 707 |
cfg.connect(inst1, ControlFlowGraph.Branch.ON_TRUE, inst2); |
| 708 |
cfg.connect(inst1, ControlFlowGraph.Branch.ON_FALSE, inst3); |
| 709 |
cfg.connect(inst2, ControlFlowGraph.Branch.UNCOND, inst4); |
| 710 |
cfg.connect(inst3, ControlFlowGraph.Branch.UNCOND, inst4); |
| 711 |
|
| 712 |
BranchedDummyConstPropagation constProp = |
| 713 |
new BranchedDummyConstPropagation(cfg); |
| 714 |
constProp.analyze(); |
| 715 |
|
| 716 |
|
| 717 |
verifyBranchedInHas(n1, a, null); |
| 718 |
verifyBranchedInHas(n1, b, null); |
| 719 |
verifyBranchedInHas(n1, c, null); |
| 720 |
|
| 721 |
|
| 722 |
verifyBranchedInHas(n2, a, null); |
| 723 |
verifyBranchedInHas(n2, b, null); |
| 724 |
verifyBranchedInHas(n2, c, null); |
| 725 |
|
| 726 |
|
| 727 |
verifyBranchedInHas(n3, a, 0); |
| 728 |
verifyBranchedInHas(n3, b, null); |
| 729 |
verifyBranchedInHas(n3, c, null); |
| 730 |
|
| 731 |
|
| 732 |
verifyBranchedInHas(n4, a, 0); |
| 733 |
} |
| 734 |
|
| 735 |
public void testMaxIterationsExceededException() { |
| 736 |
final int MAX_STEP = 10; |
| 737 |
Variable a = new Variable("a"); |
| 738 |
Instruction inst1 = new ArithmeticInstruction(a, a, Operation.ADD, a); |
| 739 |
ControlFlowGraph<Instruction> cfg = |
| 740 |
new ControlFlowGraph<Instruction>(inst1, true, true) { |
| 741 |
@Override |
| 742 |
public Comparator<DiGraphNode<Instruction, Branch>> |
| 743 |
getOptionalNodeComparator(boolean isForward) { |
| 744 |
return new Comparator<DiGraphNode<Instruction, Branch>>() { |
| 745 |
@Override |
| 746 |
public int compare(DiGraphNode<Instruction, Branch> o1, |
| 747 |
DiGraphNode<Instruction, Branch> o2) { |
| 748 |
return o1.getValue().order - o2.getValue().order; |
| 749 |
} |
| 750 |
}; |
| 751 |
} |
| 752 |
}; |
| 753 |
cfg.createNode(inst1); |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
for (int i = 0; i < MAX_STEP + 1; i++) { |
| 758 |
Instruction inst2 = new ArithmeticInstruction(a, a, Operation.ADD, a); |
| 759 |
cfg.createNode(inst2); |
| 760 |
inst2.order = i + 1; |
| 761 |
cfg.connect(inst1, ControlFlowGraph.Branch.UNCOND, inst2); |
| 762 |
inst1 = inst2; |
| 763 |
} |
| 764 |
DummyConstPropagation constProp = new DummyConstPropagation(cfg); |
| 765 |
try { |
| 766 |
constProp.analyze(MAX_STEP); |
| 767 |
fail("Expected MaxIterationsExceededException to be thrown."); |
| 768 |
} catch (MaxIterationsExceededException e) { |
| 769 |
assertEquals(e.getMessage(), "Analysis did not terminate after " |
| 770 |
+ MAX_STEP + " iterations"); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
static void verifyInHas(GraphNode<Instruction, Branch> node, Variable var, |
| 775 |
Integer constant) { |
| 776 |
FlowState<ConstPropLatticeElement> fState = node.getAnnotation(); |
| 777 |
assertEquals(constant, fState.getIn().constMap.get(var)); |
| 778 |
} |
| 779 |
|
| 780 |
static void verifyOutHas(GraphNode<Instruction, Branch> node, Variable var, |
| 781 |
Integer constant) { |
| 782 |
FlowState<ConstPropLatticeElement> fState = node.getAnnotation(); |
| 783 |
assertEquals(constant, fState.getOut().constMap.get(var)); |
| 784 |
} |
| 785 |
|
| 786 |
static void verifyBranchedInHas(GraphNode<Instruction, Branch> node, |
| 787 |
Variable var, Integer constant) { |
| 788 |
BranchedFlowState<ConstPropLatticeElement> fState = node.getAnnotation(); |
| 789 |
assertEquals(constant, fState.getIn().constMap.get(var)); |
| 790 |
} |
| 791 |
} |