comparison graal/GraalCompiler/src/com/sun/c1x/graph/GraphBuilder.java @ 2676:e0e89714e2f1

Merge
author Maxine VM
date Fri, 13 May 2011 14:03:03 -0700
parents bcd20d26d52d 6ab73784566a
children ed36daed4c43 7ed72769d51a
comparison
equal deleted inserted replaced
2675:bcd20d26d52d 2676:e0e89714e2f1
109 109
110 private Value rootMethodSynchronizedObject; 110 private Value rootMethodSynchronizedObject;
111 111
112 private final Graph graph; 112 private final Graph graph;
113 113
114 private final List<BlockBegin> newExceptionHandlers = new ArrayList<BlockBegin>();
115
114 /** 116 /**
115 * Creates a new, initialized, {@code GraphBuilder} instance for a given compilation. 117 * Creates a new, initialized, {@code GraphBuilder} instance for a given compilation.
116 * 118 *
117 * @param compilation the compilation 119 * @param compilation the compilation
118 * @param ir the IR to build the graph into 120 * @param ir the IR to build the graph into
146 } 148 }
147 149
148 // 1. create the start block 150 // 1. create the start block
149 ir.startBlock = new BlockBegin(0, ir.nextBlockNumber(), graph); 151 ir.startBlock = new BlockBegin(0, ir.nextBlockNumber(), graph);
150 BlockBegin startBlock = ir.startBlock; 152 BlockBegin startBlock = ir.startBlock;
151 graph.root().setStart(startBlock); 153 // graph.root().setStart(startBlock);
152 154
153 // 2. compute the block map, setup exception handlers and get the entrypoint(s) 155 // 2. compute the block map, setup exception handlers and get the entrypoint(s)
154 BlockMap blockMap = compilation.getBlockMap(rootMethod); 156 BlockMap blockMap = compilation.getBlockMap(rootMethod);
155 157
156 blockList = new BlockBegin[rootMethod.code().length]; 158 blockList = new BlockBegin[rootMethod.code().length];
315 int phiOperand = entry.addExceptionState(curState); 317 int phiOperand = entry.addExceptionState(curState);
316 318
317 // add entry to the list of exception handlers of this block 319 // add entry to the list of exception handlers of this block
318 curBlock.addExceptionHandler(entry); 320 curBlock.addExceptionHandler(entry);
319 321
320 // add back-edge from exception handler entry to this block 322 newExceptionHandlers.add(entry);
321 if (!entry.blockPredecessors().contains(curBlock)) {
322 entry.addPredecessor(curBlock);
323 }
324 323
325 // clone exception handler 324 // clone exception handler
326 ExceptionHandler newHandler = new ExceptionHandler(handler); 325 ExceptionHandler newHandler = new ExceptionHandler(handler);
327 newHandler.setPhiOperand(phiOperand); 326 newHandler.setPhiOperand(phiOperand);
328 exceptionHandlers.add(newHandler); 327 exceptionHandlers.add(newHandler);
521 private void ifNode(Value x, Condition cond, Value y, FrameState stateBefore) { 520 private void ifNode(Value x, Condition cond, Value y, FrameState stateBefore) {
522 BlockBegin tsucc = blockAt(stream().readBranchDest()); 521 BlockBegin tsucc = blockAt(stream().readBranchDest());
523 BlockBegin fsucc = blockAt(stream().nextBCI()); 522 BlockBegin fsucc = blockAt(stream().nextBCI());
524 int bci = stream().currentBCI(); 523 int bci = stream().currentBCI();
525 boolean isSafepoint = !noSafepoints() && (tsucc.bci() <= bci || fsucc.bci() <= bci); 524 boolean isSafepoint = !noSafepoints() && (tsucc.bci() <= bci || fsucc.bci() <= bci);
526 append(new If(x, cond, y, tsucc, fsucc, isSafepoint ? stateBefore : null, isSafepoint, graph)); 525 if (isSafepoint) {
526 append(new If(x, cond, y, tsucc, fsucc, stateBefore, isSafepoint, graph));
527 } else {
528 append(new If(x, cond, y, tsucc, fsucc, null, isSafepoint, graph));
529 stateBefore.delete();
530 }
527 } 531 }
528 532
529 private void genIfZero(Condition cond) { 533 private void genIfZero(Condition cond) {
530 Value y = appendConstant(CiConstant.INT_0); 534 Value y = appendConstant(CiConstant.INT_0);
531 FrameState stateBefore = frameState.create(bci()); 535 FrameState stateBefore = frameState.create(bci());
556 560
557 private void genCheckCast() { 561 private void genCheckCast() {
558 int cpi = stream().readCPI(); 562 int cpi = stream().readCPI();
559 RiType type = constantPool().lookupType(cpi, CHECKCAST); 563 RiType type = constantPool().lookupType(cpi, CHECKCAST);
560 boolean isInitialized = type.isResolved(); 564 boolean isInitialized = type.isResolved();
561 Value typeInstruction = genTypeOrDeopt(RiType.Representation.ObjectHub, type, isInitialized, cpi, frameState.create(bci())); 565 Value typeInstruction = genTypeOrDeopt(RiType.Representation.ObjectHub, type, isInitialized, cpi, frameState);
562 CheckCast c = new CheckCast(type, typeInstruction, frameState.apop(), graph); 566 CheckCast c = new CheckCast(type, typeInstruction, frameState.apop(), graph);
563 frameState.apush(append(c)); 567 frameState.apush(append(c));
564 } 568 }
565 569
566 private void genInstanceOf() { 570 private void genInstanceOf() {
567 int cpi = stream().readCPI(); 571 int cpi = stream().readCPI();
568 RiType type = constantPool().lookupType(cpi, INSTANCEOF); 572 RiType type = constantPool().lookupType(cpi, INSTANCEOF);
569 boolean isInitialized = type.isResolved(); 573 boolean isInitialized = type.isResolved();
570 //System.out.println("instanceof : type.isResolved() = " + type.isResolved() + "; type.isInitialized() = " + type.isInitialized()); 574 //System.out.println("instanceof : type.isResolved() = " + type.isResolved() + "; type.isInitialized() = " + type.isInitialized());
571 Value typeInstruction = genTypeOrDeopt(RiType.Representation.ObjectHub, type, isInitialized, cpi, frameState.create(bci())); 575 Value typeInstruction = genTypeOrDeopt(RiType.Representation.ObjectHub, type, isInitialized, cpi, frameState);
572 InstanceOf i = new InstanceOf(type, typeInstruction, frameState.apop(), graph); 576 Instruction result;
573 frameState.ipush(append(i)); 577 Value object = frameState.apop();
578 if (typeInstruction != null) {
579 result = new InstanceOf(type, typeInstruction, object, graph);
580 } else {
581 result = Constant.forInt(0, graph);
582 }
583 frameState.ipush(append(result));
574 } 584 }
575 585
576 void genNewInstance(int cpi) { 586 void genNewInstance(int cpi) {
577 RiType type = constantPool().lookupType(cpi, NEW); 587 RiType type = constantPool().lookupType(cpi, NEW);
578 FrameState stateBefore = null; 588 FrameState stateBefore = null;
622 // Must copy the state here, because the field holder must still be on the stack. 632 // Must copy the state here, because the field holder must still be on the stack.
623 FrameState stateBefore = null; 633 FrameState stateBefore = null;
624 if (!field.isResolved()) { 634 if (!field.isResolved()) {
625 stateBefore = frameState.create(bci()); 635 stateBefore = frameState.create(bci());
626 } 636 }
627 LoadField load = new LoadField(frameState.apop(), field, graph); 637 LoadField load = new LoadField(frameState.apop(), field, stateBefore, graph);
628 appendOptimizedLoadField(field.kind(), load); 638 appendOptimizedLoadField(field.kind(), load);
629 load.setStateBefore(stateBefore);
630 } 639 }
631 640
632 private void genPutField(int cpi, RiField field) { 641 private void genPutField(int cpi, RiField field) {
633 // Must copy the state here, because the field holder must still be on the stack. 642 // Must copy the state here, because the field holder must still be on the stack.
634 FrameState stateBefore = null; 643 FrameState stateBefore = null;
643 652
644 private void genGetStatic(int cpi, RiField field) { 653 private void genGetStatic(int cpi, RiField field) {
645 RiType holder = field.holder(); 654 RiType holder = field.holder();
646 boolean isInitialized = field.isResolved(); 655 boolean isInitialized = field.isResolved();
647 CiConstant constantValue = null; 656 CiConstant constantValue = null;
648 FrameState stateBefore = frameState.create(bci());
649 if (isInitialized) { 657 if (isInitialized) {
650 constantValue = field.constantValue(null); 658 constantValue = field.constantValue(null);
651 } 659 }
652 if (constantValue != null) { 660 if (constantValue != null) {
653 frameState.push(constantValue.kind.stackKind(), appendConstant(constantValue)); 661 frameState.push(constantValue.kind.stackKind(), appendConstant(constantValue));
654 } else { 662 } else {
655 Value container = genTypeOrDeopt(RiType.Representation.StaticFields, holder, isInitialized, cpi, stateBefore); 663 FrameState stateBefore = frameState.create(bci());
656 LoadField load = new LoadField(container, field, graph); 664 Value container = genTypeOrDeopt(RiType.Representation.StaticFields, holder, isInitialized, cpi, frameState);
665 if (container == null) {
666 container = Constant.forObject(null, graph);
667 }
668 LoadField load = new LoadField(container, field, stateBefore, graph);
657 appendOptimizedLoadField(field.kind(), load); 669 appendOptimizedLoadField(field.kind(), load);
658 load.setStateBefore(stateBefore);
659 } 670 }
660 } 671 }
661 672
662 private void genPutStatic(int cpi, RiField field) { 673 private void genPutStatic(int cpi, RiField field) {
663 RiType holder = field.holder(); 674 RiType holder = field.holder();
664 FrameState stateBefore = frameState.create(bci()); 675 FrameState stateBefore = frameState.create(bci());
665 Value container = genTypeOrDeopt(RiType.Representation.StaticFields, holder, field.isResolved(), cpi, stateBefore); 676 Value container = genTypeOrDeopt(RiType.Representation.StaticFields, holder, field.isResolved(), cpi, frameState);
666 Value value = frameState.pop(field.kind().stackKind()); 677 Value value = frameState.pop(field.kind().stackKind());
667 StoreField store = new StoreField(container, field, value, graph); 678 if (container != null) {
668 appendOptimizedStoreField(store); 679 StoreField store = new StoreField(container, field, value, graph);
669 if (!field.isResolved()) { 680 appendOptimizedStoreField(store);
670 store.setStateBefore(stateBefore); 681 if (!field.isResolved()) {
671 } 682 store.setStateBefore(stateBefore);
672 } 683 }
673 684 }
674 private Value genTypeOrDeopt(RiType.Representation representation, RiType holder, boolean initialized, int cpi, FrameState stateBefore) { 685 }
675 Value holderInstr; 686
687 private Value genTypeOrDeopt(RiType.Representation representation, RiType holder, boolean initialized, int cpi, FrameStateAccess stateBefore) {
676 if (initialized) { 688 if (initialized) {
677 holderInstr = appendConstant(holder.getEncoding(representation)); 689 return appendConstant(holder.getEncoding(representation));
678 } else { 690 } else {
679 append(new Deoptimize(graph, stateBefore)); 691 append(new Deoptimize(graph, stateBefore.duplicate(bci())));
680 holderInstr = append(Constant.forObject(null, graph)); 692 return null;
681 } 693 }
682 return holderInstr;
683 } 694 }
684 695
685 private void appendOptimizedStoreField(StoreField store) { 696 private void appendOptimizedStoreField(StoreField store) {
686 append(store); 697 append(store);
687 } 698 }
697 boolean isInitialized = target.isResolved() && holder.isInitialized(); 708 boolean isInitialized = target.isResolved() && holder.isInitialized();
698 if (!isInitialized && C1XOptions.ResolveClassBeforeStaticInvoke) { 709 if (!isInitialized && C1XOptions.ResolveClassBeforeStaticInvoke) {
699 // Re-use the same resolution code as for accessing a static field. Even though 710 // Re-use the same resolution code as for accessing a static field. Even though
700 // the result of resolution is not used by the invocation (only the side effect 711 // the result of resolution is not used by the invocation (only the side effect
701 // of initialization is required), it can be commoned with static field accesses. 712 // of initialization is required), it can be commoned with static field accesses.
702 genTypeOrDeopt(RiType.Representation.StaticFields, holder, isInitialized, cpi, frameState.create(bci())); 713 genTypeOrDeopt(RiType.Representation.StaticFields, holder, isInitialized, cpi, frameState);
703 } 714 }
704 Value[] args = frameState.popArguments(target.signature().argumentSlots(false)); 715 Value[] args = frameState.popArguments(target.signature().argumentSlots(false));
705 appendInvoke(INVOKESTATIC, target, args, cpi, constantPool); 716 appendInvoke(INVOKESTATIC, target, args, cpi, constantPool);
706 } 717 }
707 718
1107 // NOTE that inlining may have changed the block we are parsing 1118 // NOTE that inlining may have changed the block we are parsing
1108 assert end != null : "end should exist after iterating over bytecodes"; 1119 assert end != null : "end should exist after iterating over bytecodes";
1109 FrameState stateAtEnd = frameState.create(bci()); 1120 FrameState stateAtEnd = frameState.create(bci());
1110 end.setStateAfter(stateAtEnd); 1121 end.setStateAfter(stateAtEnd);
1111 curBlock.setEnd(end); 1122 curBlock.setEnd(end);
1123
1124 for (BlockBegin entry : newExceptionHandlers) {
1125 // add back-edge from exception handler entry to this block
1126 if (!entry.blockPredecessors().contains(curBlock.end())) {
1127 entry.addPredecessor(curBlock.end());
1128 }
1129 }
1130 newExceptionHandlers.clear();
1131
1112 // propagate the state 1132 // propagate the state
1113 for (BlockBegin succ : end.blockSuccessors()) { 1133 for (BlockBegin succ : end.blockSuccessors()) {
1114 assert succ.blockPredecessors().contains(curBlock); 1134 assert succ.blockPredecessors().contains(curBlock.end());
1115 succ.mergeOrClone(stateAtEnd, method()); 1135 succ.mergeOrClone(stateAtEnd, method());
1116 addToWorkList(succ); 1136 addToWorkList(succ);
1117 } 1137 }
1118 return end; 1138 return end;
1119 } 1139 }