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.core.gen;
26
27 import static jdk.vm.ci.code.ValueUtil.asRegister;
28 import static jdk.vm.ci.code.ValueUtil.isLegal;
29 import static jdk.vm.ci.code.ValueUtil.isRegister;
30 import static org.graalvm.compiler.core.common.GraalOptions.MatchExpressions;
31 import static org.graalvm.compiler.debug.DebugOptions.LogVerbose;
32 import static org.graalvm.compiler.lir.LIR.verifyBlock;
33
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
37
38 import jdk.internal.vm.compiler.collections.EconomicMap;
39 import jdk.internal.vm.compiler.collections.UnmodifiableMapCursor;
40 import org.graalvm.compiler.core.common.LIRKind;
41 import org.graalvm.compiler.core.common.calc.Condition;
42 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
43 import org.graalvm.compiler.core.common.cfg.BlockMap;
44 import org.graalvm.compiler.core.common.type.Stamp;
45 import org.graalvm.compiler.core.match.ComplexMatchValue;
46 import org.graalvm.compiler.core.match.MatchPattern;
47 import org.graalvm.compiler.core.match.MatchRuleRegistry;
48 import org.graalvm.compiler.core.match.MatchStatement;
49 import org.graalvm.compiler.debug.DebugContext;
295 * new Variable.
296 */
297 value = gen.emitMove(value);
298 } else if (node.isConstant() && !gen.getSpillMoveFactory().allowConstantToStackMove(node.asConstant()) && !LIRKind.isValue(value)) {
299 /*
300 * Some constants are not allowed as inputs for PHIs in certain backends. Explicitly
301 * create a copy of this value to force it into a register. The new variable is only
302 * used in the PHI.
303 */
304 Variable result = gen.newVariable(value.getValueKind());
305 gen.emitMove(result, value);
306 value = result;
307 }
308 values.add(value);
309 }
310 return values.toArray(new Value[values.size()]);
311 }
312
313 public void doBlockPrologue(@SuppressWarnings("unused") Block block, @SuppressWarnings("unused") OptionValues options) {
314
315 }
316
317 @Override
318 @SuppressWarnings("try")
319 public void doBlock(Block block, StructuredGraph graph, BlockMap<List<Node>> blockMap) {
320
321 OptionValues options = graph.getOptions();
322 try (BlockScope blockScope = gen.getBlockScope(block)) {
323 setSourcePosition(null);
324
325 if (block == gen.getResult().getLIR().getControlFlowGraph().getStartBlock()) {
326 assert block.getPredecessorCount() == 0;
327 emitPrologue(graph);
328 } else {
329 assert block.getPredecessorCount() > 0;
330 // create phi-in value array
331 AbstractBeginNode begin = block.getBeginNode();
332 if (begin instanceof AbstractMergeNode) {
333 AbstractMergeNode merge = (AbstractMergeNode) begin;
334 LabelOp label = (LabelOp) gen.getResult().getLIR().getLIRforBlock(block).get(0);
355 ValueNode valueNode = (ValueNode) node;
356 if (trace) {
357 TTY.println("LIRGen for " + valueNode);
358 }
359 Value operand = getOperand(valueNode);
360 if (operand == null) {
361 if (!peephole(valueNode)) {
362 try {
363 doRoot(valueNode);
364 } catch (GraalError e) {
365 throw GraalGraphError.transformAndAddContext(e, valueNode);
366 } catch (Throwable e) {
367 throw new GraalGraphError(e).addContext(valueNode);
368 }
369 }
370 } else if (ComplexMatchValue.INTERIOR_MATCH.equals(operand)) {
371 // Doesn't need to be evaluated
372 debug.log("interior match for %s", valueNode);
373 } else if (operand instanceof ComplexMatchValue) {
374 debug.log("complex match for %s", valueNode);
375 ComplexMatchValue match = (ComplexMatchValue) operand;
376 operand = match.evaluate(this);
377 if (operand != null) {
378 setResult(valueNode, operand);
379 }
380 } else {
381 // There can be cases in which the result of an instruction is already set
382 // before by other instructions.
383 }
384 }
385 }
386
387 if (!gen.hasBlockEnd(block)) {
388 NodeIterable<Node> successors = block.getEndNode().successors();
389 assert successors.count() == block.getSuccessorCount();
390 if (block.getSuccessorCount() != 1) {
391 /*
392 * If we have more than one successor, we cannot just use the first one. Since
393 * successors are unordered, this would be a random choice.
394 */
|
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.core.gen;
26
27 import static jdk.vm.ci.code.ValueUtil.asRegister;
28 import static jdk.vm.ci.code.ValueUtil.isLegal;
29 import static jdk.vm.ci.code.ValueUtil.isRegister;
30 import static org.graalvm.compiler.core.common.SpeculativeExecutionAttacksMitigations.AllTargets;
31 import static org.graalvm.compiler.core.common.SpeculativeExecutionAttacksMitigations.Options.MitigateSpeculativeExecutionAttacks;
32 import static org.graalvm.compiler.core.common.GraalOptions.MatchExpressions;
33 import static org.graalvm.compiler.debug.DebugOptions.LogVerbose;
34 import static org.graalvm.compiler.lir.LIR.verifyBlock;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
39
40 import jdk.internal.vm.compiler.collections.EconomicMap;
41 import jdk.internal.vm.compiler.collections.UnmodifiableMapCursor;
42 import org.graalvm.compiler.core.common.LIRKind;
43 import org.graalvm.compiler.core.common.calc.Condition;
44 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
45 import org.graalvm.compiler.core.common.cfg.BlockMap;
46 import org.graalvm.compiler.core.common.type.Stamp;
47 import org.graalvm.compiler.core.match.ComplexMatchValue;
48 import org.graalvm.compiler.core.match.MatchPattern;
49 import org.graalvm.compiler.core.match.MatchRuleRegistry;
50 import org.graalvm.compiler.core.match.MatchStatement;
51 import org.graalvm.compiler.debug.DebugContext;
297 * new Variable.
298 */
299 value = gen.emitMove(value);
300 } else if (node.isConstant() && !gen.getSpillMoveFactory().allowConstantToStackMove(node.asConstant()) && !LIRKind.isValue(value)) {
301 /*
302 * Some constants are not allowed as inputs for PHIs in certain backends. Explicitly
303 * create a copy of this value to force it into a register. The new variable is only
304 * used in the PHI.
305 */
306 Variable result = gen.newVariable(value.getValueKind());
307 gen.emitMove(result, value);
308 value = result;
309 }
310 values.add(value);
311 }
312 return values.toArray(new Value[values.size()]);
313 }
314
315 public void doBlockPrologue(@SuppressWarnings("unused") Block block, @SuppressWarnings("unused") OptionValues options) {
316
317 if (MitigateSpeculativeExecutionAttacks.getValue(options) == AllTargets) {
318 boolean hasControlSplitPredecessor = false;
319 for (Block b : block.getPredecessors()) {
320 if (b.getSuccessorCount() > 1) {
321 hasControlSplitPredecessor = true;
322 break;
323 }
324 }
325 boolean isStartBlock = block.getPredecessorCount() == 0;
326 if (hasControlSplitPredecessor || isStartBlock) {
327 getLIRGeneratorTool().emitSpeculationFence();
328 }
329 }
330 }
331
332 @Override
333 @SuppressWarnings("try")
334 public void doBlock(Block block, StructuredGraph graph, BlockMap<List<Node>> blockMap) {
335
336 OptionValues options = graph.getOptions();
337 try (BlockScope blockScope = gen.getBlockScope(block)) {
338 setSourcePosition(null);
339
340 if (block == gen.getResult().getLIR().getControlFlowGraph().getStartBlock()) {
341 assert block.getPredecessorCount() == 0;
342 emitPrologue(graph);
343 } else {
344 assert block.getPredecessorCount() > 0;
345 // create phi-in value array
346 AbstractBeginNode begin = block.getBeginNode();
347 if (begin instanceof AbstractMergeNode) {
348 AbstractMergeNode merge = (AbstractMergeNode) begin;
349 LabelOp label = (LabelOp) gen.getResult().getLIR().getLIRforBlock(block).get(0);
370 ValueNode valueNode = (ValueNode) node;
371 if (trace) {
372 TTY.println("LIRGen for " + valueNode);
373 }
374 Value operand = getOperand(valueNode);
375 if (operand == null) {
376 if (!peephole(valueNode)) {
377 try {
378 doRoot(valueNode);
379 } catch (GraalError e) {
380 throw GraalGraphError.transformAndAddContext(e, valueNode);
381 } catch (Throwable e) {
382 throw new GraalGraphError(e).addContext(valueNode);
383 }
384 }
385 } else if (ComplexMatchValue.INTERIOR_MATCH.equals(operand)) {
386 // Doesn't need to be evaluated
387 debug.log("interior match for %s", valueNode);
388 } else if (operand instanceof ComplexMatchValue) {
389 debug.log("complex match for %s", valueNode);
390 // Set current position to the position of the root matched node.
391 setSourcePosition(node.getNodeSourcePosition());
392 ComplexMatchValue match = (ComplexMatchValue) operand;
393 operand = match.evaluate(this);
394 if (operand != null) {
395 setResult(valueNode, operand);
396 }
397 } else {
398 // There can be cases in which the result of an instruction is already set
399 // before by other instructions.
400 }
401 }
402 }
403
404 if (!gen.hasBlockEnd(block)) {
405 NodeIterable<Node> successors = block.getEndNode().successors();
406 assert successors.count() == block.getSuccessorCount();
407 if (block.getSuccessorCount() != 1) {
408 /*
409 * If we have more than one successor, we cannot just use the first one. Since
410 * successors are unordered, this would be a random choice.
411 */
|