1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.ssa;
18
19import com.android.dx.util.BitIntSet;
20import com.android.dx.util.IntSet;
21import com.android.dx.util.ListIntSet;
22
23
24/**
25 * Makes int sets for various parts of the optimizer.
26 */
27public final class SetFactory {
28
29    /**
30     * BitIntSet/ListIntSet threshold for dominance frontier sets. These
31     * sets are kept per basic block until phi placement and tend to be,
32     * like the CFG itself, very sparse at large sizes.
33     *
34     * A value of 3072 here is somewhere around 1.125mb of total bitset size.
35     */
36    private static final int DOMFRONT_SET_THRESHOLD_SIZE = 3072;
37
38    /**
39     * BitIntSet/ListIntSet threshold for interference graph sets. These
40     * sets are kept per register until register allocation is done.
41     *
42     * A value of 3072 here is somewhere around 1.125mb of total bitset size.
43     */
44    private static final int INTERFERENCE_SET_THRESHOLD_SIZE = 3072;
45
46    /**
47     * BitIntSet/ListIntSet threshold for the live in/out sets kept by
48     * {@link SsaBasicBlock}. These are sets of SSA registers kept per basic
49     * block during register allocation.
50     *
51     * The total size of a bitset for this would be the count of blocks
52     * times the size of registers. The threshold value here is merely
53     * the register count, which is typically on the order of the block
54     * count as well.
55     */
56    private static final int LIVENESS_SET_THRESHOLD_SIZE = 3072;
57
58
59    /**
60     * Make IntSet for the dominance-frontier sets.
61     *
62     * @param szBlocks {@code >=0;} count of basic blocks in method
63     * @return {@code non-null;} appropriate set
64     */
65    /*package*/ static IntSet makeDomFrontSet(int szBlocks) {
66        return szBlocks <= DOMFRONT_SET_THRESHOLD_SIZE
67                ? new BitIntSet(szBlocks)
68                : new ListIntSet();
69    }
70
71    /**
72     * Make IntSet for the interference graph sets. Public because
73     * InterferenceGraph is in another package.
74     *
75     * @param countRegs {@code >=0;} count of SSA registers used in method
76     * @return {@code non-null;} appropriate set
77     */
78    public static IntSet makeInterferenceSet(int countRegs) {
79        return countRegs <= INTERFERENCE_SET_THRESHOLD_SIZE
80                ? new BitIntSet(countRegs)
81                : new ListIntSet();
82    }
83
84    /**
85     * Make IntSet for register live in/out sets.
86     *
87     * @param countRegs {@code >=0;} count of SSA registers used in method
88     * @return {@code non-null;} appropriate set
89     */
90    /*package*/ static IntSet makeLivenessSet(int countRegs) {
91        return countRegs <= LIVENESS_SET_THRESHOLD_SIZE
92                ? new BitIntSet(countRegs)
93                : new ListIntSet();
94    }
95}
96