1/*
2 * Copyright (c) 2015, 2015, 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.alloc.lsra;
24
25import static jdk.vm.ci.code.ValueUtil.isRegister;
26
27import org.graalvm.compiler.lir.alloc.lsra.Interval.UsePosList;
28import org.graalvm.compiler.lir.debug.IntervalDumper;
29
30import jdk.vm.ci.meta.AllocatableValue;
31import jdk.vm.ci.meta.Value;
32
33class LinearScanIntervalDumper implements IntervalDumper {
34    private final Interval[] intervals;
35
36    LinearScanIntervalDumper(Interval[] intervals) {
37        this.intervals = intervals;
38    }
39
40    @Override
41    public void visitIntervals(IntervalVisitor visitor) {
42        for (Interval interval : intervals) {
43            if (interval != null) {
44                printInterval(interval, visitor);
45            }
46        }
47    }
48
49    private static void printInterval(Interval interval, IntervalVisitor visitor) {
50        Value hint = interval.locationHint(false) != null ? interval.locationHint(false).operand : null;
51        AllocatableValue operand = interval.operand;
52        String type = isRegister(operand) ? "fixed" : operand.getValueKind().getPlatformKind().toString();
53        visitor.visitIntervalStart(interval.splitParent().operand, operand, interval.location(), hint, type);
54
55        // print ranges
56        Range cur = interval.first();
57        while (!cur.isEndMarker()) {
58            visitor.visitRange(cur.from, cur.to);
59            cur = cur.next;
60            assert cur != null : "range list not closed with range sentinel";
61        }
62
63        // print use positions
64        int prev = -1;
65        UsePosList usePosList = interval.usePosList();
66        for (int i = usePosList.size() - 1; i >= 0; --i) {
67            assert prev < usePosList.usePos(i) : "use positions not sorted";
68            visitor.visitUsePos(usePosList.usePos(i), usePosList.registerPriority(i));
69            prev = usePosList.usePos(i);
70        }
71
72        visitor.visitIntervalEnd(interval.spillState());
73    }
74
75}
76