1 /*
2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 
25 package org.graalvm.compiler.lir.alloc.lsra.ssa;
26 
27 import static jdk.vm.ci.code.ValueUtil.isRegister;
28 import static org.graalvm.compiler.lir.LIRValueUtil.asConstant;
29 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
30 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
31 
32 import java.util.ArrayList;
33 
34 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
35 import org.graalvm.compiler.debug.CounterKey;
36 import org.graalvm.compiler.debug.DebugContext;
37 import org.graalvm.compiler.lir.LIRInstruction;
38 import org.graalvm.compiler.lir.alloc.lsra.Interval;
39 import org.graalvm.compiler.lir.alloc.lsra.LinearScan;
40 import org.graalvm.compiler.lir.alloc.lsra.LinearScanResolveDataFlowPhase;
41 import org.graalvm.compiler.lir.alloc.lsra.MoveResolver;
42 import org.graalvm.compiler.lir.ssa.SSAUtil;
43 import org.graalvm.compiler.lir.ssa.SSAUtil.PhiValueVisitor;
44 
45 import jdk.vm.ci.meta.Value;
46 
47 class SSALinearScanResolveDataFlowPhase extends LinearScanResolveDataFlowPhase {
48 
49     private static final CounterKey numPhiResolutionMoves = DebugContext.counter("SSA LSRA[numPhiResolutionMoves]");
50     private static final CounterKey numStackToStackMoves = DebugContext.counter("SSA LSRA[numStackToStackMoves]");
51 
SSALinearScanResolveDataFlowPhase(LinearScan allocator)52     SSALinearScanResolveDataFlowPhase(LinearScan allocator) {
53         super(allocator);
54     }
55 
56     @Override
resolveCollectMappings(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> midBlock, MoveResolver moveResolver)57     protected void resolveCollectMappings(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> midBlock, MoveResolver moveResolver) {
58         super.resolveCollectMappings(fromBlock, toBlock, midBlock, moveResolver);
59 
60         if (toBlock.getPredecessorCount() > 1) {
61             int toBlockFirstInstructionId = allocator.getFirstLirInstructionId(toBlock);
62             int fromBlockLastInstructionId = allocator.getLastLirInstructionId(fromBlock) + 1;
63 
64             AbstractBlockBase<?> phiOutBlock = midBlock != null ? midBlock : fromBlock;
65             ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(phiOutBlock);
66             int phiOutIdx = SSAUtil.phiOutIndex(allocator.getLIR(), phiOutBlock);
67             int phiOutId = midBlock != null ? fromBlockLastInstructionId : instructions.get(phiOutIdx).id();
68             assert phiOutId >= 0;
69 
70             PhiValueVisitor visitor = new PhiValueVisitor() {
71 
72                 @Override
73                 public void visit(Value phiIn, Value phiOut) {
74                     assert !isRegister(phiOut) : "phiOut is a register: " + phiOut;
75                     assert !isRegister(phiIn) : "phiIn is a register: " + phiIn;
76                     Interval toInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiIn), toBlockFirstInstructionId, LIRInstruction.OperandMode.DEF);
77                     DebugContext debug = allocator.getDebug();
78                     if (isConstantValue(phiOut)) {
79                         numPhiResolutionMoves.increment(debug);
80                         moveResolver.addMapping(asConstant(phiOut), toInterval);
81                     } else {
82                         Interval fromInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiOut), phiOutId, LIRInstruction.OperandMode.DEF);
83                         if (fromInterval != toInterval && !fromInterval.location().equals(toInterval.location())) {
84                             numPhiResolutionMoves.increment(debug);
85                             if (!(isStackSlotValue(toInterval.location()) && isStackSlotValue(fromInterval.location()))) {
86                                 moveResolver.addMapping(fromInterval, toInterval);
87                             } else {
88                                 numStackToStackMoves.increment(debug);
89                                 moveResolver.addMapping(fromInterval, toInterval);
90                             }
91                         }
92                     }
93                 }
94             };
95 
96             SSAUtil.forEachPhiValuePair(allocator.getLIR(), toBlock, phiOutBlock, visitor);
97             SSAUtil.removePhiOut(allocator.getLIR(), phiOutBlock);
98         }
99     }
100 
101 }
102