001/*
002 * Copyright (c) 2009, 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.java;
024
025import com.oracle.graal.java.BciBlockMapping.BciBlock;
026
027public final class SmallLocalLiveness extends LocalLiveness {
028    /*
029     * local n is represented by the bit accessible as (1 << n)
030     */
031
032    private final long[] localsLiveIn;
033    private final long[] localsLiveOut;
034    private final long[] localsLiveGen;
035    private final long[] localsLiveKill;
036    private final long[] localsChangedInLoop;
037    private final int maxLocals;
038
039    public SmallLocalLiveness(BciBlock[] blocks, int maxLocals, int loopCount) {
040        super(blocks);
041        this.maxLocals = maxLocals;
042        int blockSize = blocks.length;
043        localsLiveIn = new long[blockSize];
044        localsLiveOut = new long[blockSize];
045        localsLiveGen = new long[blockSize];
046        localsLiveKill = new long[blockSize];
047        localsChangedInLoop = new long[loopCount];
048    }
049
050    private String debugString(long value) {
051        StringBuilder str = new StringBuilder("{");
052        long current = value;
053        for (int i = 0; i < maxLocals; i++) {
054            if ((current & 1L) == 1L) {
055                if (str.length() > 1) {
056                    str.append(", ");
057                }
058                str.append(i);
059            }
060            current >>= 1;
061        }
062        return str.append('}').toString();
063    }
064
065    @Override
066    protected String debugLiveIn(int blockID) {
067        return debugString(localsLiveIn[blockID]);
068    }
069
070    @Override
071    protected String debugLiveOut(int blockID) {
072        return debugString(localsLiveOut[blockID]);
073    }
074
075    @Override
076    protected String debugLiveGen(int blockID) {
077        return debugString(localsLiveGen[blockID]);
078    }
079
080    @Override
081    protected String debugLiveKill(int blockID) {
082        return debugString(localsLiveKill[blockID]);
083    }
084
085    @Override
086    protected int liveOutCardinality(int blockID) {
087        return Long.bitCount(localsLiveOut[blockID]);
088    }
089
090    @Override
091    protected void propagateLiveness(int blockID, int successorID) {
092        localsLiveOut[blockID] |= localsLiveIn[successorID];
093    }
094
095    @Override
096    protected void updateLiveness(int blockID) {
097        localsLiveIn[blockID] = (localsLiveOut[blockID] & ~localsLiveKill[blockID]) | localsLiveGen[blockID];
098    }
099
100    @Override
101    protected void loadOne(int blockID, int local) {
102        long bit = 1L << local;
103        if ((localsLiveKill[blockID] & bit) == 0L) {
104            localsLiveGen[blockID] |= bit;
105        }
106    }
107
108    @Override
109    protected void storeOne(int blockID, int local) {
110        long bit = 1L << local;
111        if ((localsLiveGen[blockID] & bit) == 0L) {
112            localsLiveKill[blockID] |= bit;
113        }
114
115        BciBlock block = blocks[blockID];
116        long tmp = block.loops;
117        int pos = 0;
118        while (tmp != 0) {
119            if ((tmp & 1L) == 1L) {
120                this.localsChangedInLoop[pos] |= bit;
121            }
122            tmp >>= 1;
123            ++pos;
124        }
125    }
126
127    @Override
128    public boolean localIsLiveIn(BciBlock block, int local) {
129        int blockID = block.getId();
130        return blockID >= Integer.MAX_VALUE ? false : (localsLiveIn[blockID] & (1L << local)) != 0L;
131    }
132
133    @Override
134    public boolean localIsLiveOut(BciBlock block, int local) {
135        int blockID = block.getId();
136        return blockID >= Integer.MAX_VALUE ? false : (localsLiveOut[blockID] & (1L << local)) != 0L;
137    }
138
139    @Override
140    public boolean localIsChangedInLoop(int loopId, int local) {
141        return (localsChangedInLoop[loopId] & (1L << local)) != 0L;
142    }
143}