001/* 002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.lir.alloc.lsra; 024 025import static com.oracle.graal.compiler.common.GraalOptions.*; 026 027import java.util.*; 028 029import jdk.internal.jvmci.code.*; 030import com.oracle.graal.debug.*; 031 032import com.oracle.graal.compiler.common.alloc.*; 033import com.oracle.graal.compiler.common.cfg.*; 034import com.oracle.graal.lir.*; 035import com.oracle.graal.lir.gen.*; 036import com.oracle.graal.lir.gen.LIRGeneratorTool.SpillMoveFactory; 037import com.oracle.graal.lir.phases.*; 038 039/** 040 * Phase 6: resolve data flow 041 * 042 * Insert moves at edges between blocks if intervals have been split. 043 */ 044public class LinearScanResolveDataFlowPhase extends AllocationPhase { 045 046 protected final LinearScan allocator; 047 048 protected LinearScanResolveDataFlowPhase(LinearScan allocator) { 049 this.allocator = allocator; 050 } 051 052 @Override 053 protected <B extends AbstractBlockBase<B>> void run(TargetDescription target, LIRGenerationResult lirGenRes, List<B> codeEmittingOrder, List<B> linearScanOrder, SpillMoveFactory spillMoveFactory, 054 RegisterAllocationConfig registerAllocationConfig) { 055 resolveDataFlow(); 056 } 057 058 protected void resolveCollectMappings(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> midBlock, MoveResolver moveResolver) { 059 assert moveResolver.checkEmpty(); 060 assert midBlock == null || 061 (midBlock.getPredecessorCount() == 1 && midBlock.getSuccessorCount() == 1 && midBlock.getPredecessors().get(0).equals(fromBlock) && midBlock.getSuccessors().get(0).equals( 062 toBlock)); 063 064 int toBlockFirstInstructionId = allocator.getFirstLirInstructionId(toBlock); 065 int fromBlockLastInstructionId = allocator.getLastLirInstructionId(fromBlock) + 1; 066 int numOperands = allocator.operandSize(); 067 BitSet liveAtEdge = allocator.getBlockData(toBlock).liveIn; 068 069 // visit all variables for which the liveAtEdge bit is set 070 for (int operandNum = liveAtEdge.nextSetBit(0); operandNum >= 0; operandNum = liveAtEdge.nextSetBit(operandNum + 1)) { 071 assert operandNum < numOperands : "live information set for not exisiting interval"; 072 assert allocator.getBlockData(fromBlock).liveOut.get(operandNum) && allocator.getBlockData(toBlock).liveIn.get(operandNum) : "interval not live at this edge"; 073 074 Interval fromInterval = allocator.splitChildAtOpId(allocator.intervalFor(operandNum), fromBlockLastInstructionId, LIRInstruction.OperandMode.DEF); 075 Interval toInterval = allocator.splitChildAtOpId(allocator.intervalFor(operandNum), toBlockFirstInstructionId, LIRInstruction.OperandMode.DEF); 076 077 if (fromInterval != toInterval && !fromInterval.location().equals(toInterval.location())) { 078 // need to insert move instruction 079 moveResolver.addMapping(fromInterval, toInterval); 080 } 081 } 082 } 083 084 void resolveFindInsertPos(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, MoveResolver moveResolver) { 085 if (fromBlock.getSuccessorCount() <= 1) { 086 if (Debug.isLogEnabled()) { 087 Debug.log("inserting moves at end of fromBlock B%d", fromBlock.getId()); 088 } 089 090 List<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(fromBlock); 091 LIRInstruction instr = instructions.get(instructions.size() - 1); 092 if (instr instanceof StandardOp.JumpOp) { 093 // insert moves before branch 094 moveResolver.setInsertPosition(instructions, instructions.size() - 1); 095 } else { 096 moveResolver.setInsertPosition(instructions, instructions.size()); 097 } 098 099 } else { 100 if (Debug.isLogEnabled()) { 101 Debug.log("inserting moves at beginning of toBlock B%d", toBlock.getId()); 102 } 103 104 if (DetailedAsserts.getValue()) { 105 assert allocator.getLIR().getLIRforBlock(fromBlock).get(0) instanceof StandardOp.LabelOp : "block does not start with a label"; 106 107 /* 108 * Because the number of predecessor edges matches the number of successor edges, 109 * blocks which are reached by switch statements may have be more than one 110 * predecessor but it will be guaranteed that all predecessors will be the same. 111 */ 112 for (AbstractBlockBase<?> predecessor : toBlock.getPredecessors()) { 113 assert fromBlock == predecessor : "all critical edges must be broken"; 114 } 115 } 116 117 moveResolver.setInsertPosition(allocator.getLIR().getLIRforBlock(toBlock), 1); 118 } 119 } 120 121 /** 122 * Inserts necessary moves (spilling or reloading) at edges between blocks for intervals that 123 * have been split. 124 */ 125 protected void resolveDataFlow() { 126 try (Indent indent = Debug.logAndIndent("resolve data flow")) { 127 128 MoveResolver moveResolver = allocator.createMoveResolver(); 129 BitSet blockCompleted = new BitSet(allocator.blockCount()); 130 131 optimizeEmptyBlocks(moveResolver, blockCompleted); 132 133 resolveDataFlow0(moveResolver, blockCompleted); 134 135 } 136 } 137 138 protected void optimizeEmptyBlocks(MoveResolver moveResolver, BitSet blockCompleted) { 139 for (AbstractBlockBase<?> block : allocator.sortedBlocks()) { 140 141 // check if block has only one predecessor and only one successor 142 if (block.getPredecessorCount() == 1 && block.getSuccessorCount() == 1) { 143 List<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block); 144 assert instructions.get(0) instanceof StandardOp.LabelOp : "block must start with label"; 145 assert instructions.get(instructions.size() - 1) instanceof StandardOp.JumpOp : "block with successor must end with unconditional jump"; 146 147 // check if block is empty (only label and branch) 148 if (instructions.size() == 2) { 149 AbstractBlockBase<?> pred = block.getPredecessors().iterator().next(); 150 AbstractBlockBase<?> sux = block.getSuccessors().iterator().next(); 151 152 // prevent optimization of two consecutive blocks 153 if (!blockCompleted.get(pred.getLinearScanNumber()) && !blockCompleted.get(sux.getLinearScanNumber())) { 154 if (Debug.isLogEnabled()) { 155 Debug.log(" optimizing empty block B%d (pred: B%d, sux: B%d)", block.getId(), pred.getId(), sux.getId()); 156 } 157 158 blockCompleted.set(block.getLinearScanNumber()); 159 160 /* 161 * Directly resolve between pred and sux (without looking at the empty block 162 * between). 163 */ 164 resolveCollectMappings(pred, sux, block, moveResolver); 165 if (moveResolver.hasMappings()) { 166 moveResolver.setInsertPosition(instructions, 1); 167 moveResolver.resolveAndAppendMoves(); 168 } 169 } 170 } 171 } 172 } 173 } 174 175 protected void resolveDataFlow0(MoveResolver moveResolver, BitSet blockCompleted) { 176 BitSet alreadyResolved = new BitSet(allocator.blockCount()); 177 for (AbstractBlockBase<?> fromBlock : allocator.sortedBlocks()) { 178 if (!blockCompleted.get(fromBlock.getLinearScanNumber())) { 179 alreadyResolved.clear(); 180 alreadyResolved.or(blockCompleted); 181 182 for (AbstractBlockBase<?> toBlock : fromBlock.getSuccessors()) { 183 184 /* 185 * Check for duplicate edges between the same blocks (can happen with switch 186 * blocks). 187 */ 188 if (!alreadyResolved.get(toBlock.getLinearScanNumber())) { 189 if (Debug.isLogEnabled()) { 190 Debug.log("processing edge between B%d and B%d", fromBlock.getId(), toBlock.getId()); 191 } 192 193 alreadyResolved.set(toBlock.getLinearScanNumber()); 194 195 // collect all intervals that have been split between 196 // fromBlock and toBlock 197 resolveCollectMappings(fromBlock, toBlock, null, moveResolver); 198 if (moveResolver.hasMappings()) { 199 resolveFindInsertPos(fromBlock, toBlock, moveResolver); 200 moveResolver.resolveAndAppendMoves(); 201 } 202 } 203 } 204 } 205 } 206 } 207 208}