1/*
2 * Copyright (c) 2016, 2016, 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 */
23package org.graalvm.compiler.lir.ssi;
24
25import java.util.BitSet;
26
27import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
28import org.graalvm.compiler.lir.alloc.lsra.LinearScanLifetimeAnalysisPhase;
29import org.graalvm.compiler.lir.gen.LIRGenerationResult;
30import org.graalvm.compiler.lir.phases.AllocationPhase;
31import org.graalvm.compiler.lir.ssa.SSAUtil;
32import org.graalvm.compiler.options.Option;
33import org.graalvm.compiler.options.OptionType;
34import org.graalvm.compiler.options.StableOptionValue;
35
36import jdk.vm.ci.code.TargetDescription;
37import jdk.vm.ci.common.JVMCIError;
38
39/**
40 * Constructs {@linkplain SSIUtil SSI LIR} using a liveness analysis.
41 *
42 * Implementation derived from {@link LinearScanLifetimeAnalysisPhase}.
43 *
44 * @see SSIUtil
45 */
46public final class SSIConstructionPhase extends AllocationPhase {
47
48    static class Options {
49
50        //@formatter:off
51        @Option(help = "Use fast SSI builder.", type = OptionType.Debug)
52        public static final StableOptionValue<Boolean> TraceRAFastSSIBuilder = new StableOptionValue<>(true);
53        //@formatter:on
54    }
55
56    @Override
57    protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
58        assert SSAUtil.verifySSAForm(lirGenRes.getLIR());
59        if (Options.TraceRAFastSSIBuilder.getValue()) {
60            FastSSIBuilder fastSSIBuilder = new FastSSIBuilder(lirGenRes.getLIR());
61            fastSSIBuilder.build();
62            fastSSIBuilder.finish();
63        } else {
64            SSIBuilder ssiBuilder = new SSIBuilder(lirGenRes.getLIR());
65            ssiBuilder.build();
66            ssiBuilder.finish();
67        }
68    }
69
70    static void check(AbstractBlockBase<?>[] blocks, SSIBuilderBase liveSets1, SSIBuilderBase liveSets2) {
71        for (AbstractBlockBase<?> block : blocks) {
72            check(block, liveSets1.getLiveIn(block), liveSets2.getLiveIn(block));
73            check(block, liveSets1.getLiveOut(block), liveSets2.getLiveOut(block));
74        }
75    }
76
77    private static void check(AbstractBlockBase<?> block, BitSet liveIn1, BitSet liveIn2) {
78        if (!liveIn1.equals(liveIn2)) {
79            throw JVMCIError.shouldNotReachHere(String.format("%s LiveSet differ: %s vs %s", block, liveIn1, liveIn2));
80        }
81    }
82}
83