/
DefUse.java
189 lines (166 loc) · 5.02 KB
/
DefUse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.ssa;
import java.util.ArrayList;
import java.util.Iterator;
import com.ibm.wala.util.collections.EmptyIterator;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.intset.IntIterator;
import com.ibm.wala.util.intset.IntSet;
import com.ibm.wala.util.intset.IntSetUtil;
import com.ibm.wala.util.intset.MutableIntSet;
/**
* An object which represent Def-Use information for an SSA {@link IR}
*/
public class DefUse {
static final boolean DEBUG = false;
/**
* A mapping from integer (value number) -> {@link SSAInstruction} that defines the value
*/
final private SSAInstruction[] defs;
/**
* A mapping from integer (value number) -> bit vector holding integers representing instructions that use the value number
*/
final private MutableIntSet[] uses;
/**
* A Mapping from integer -> Instruction
*/
final protected ArrayList<SSAInstruction> allInstructions = new ArrayList<SSAInstruction>();
/**
* prevent the IR from being collected while this is live.
*/
private final IR ir;
/**
* @param ir an IR in SSA form.
* @throws IllegalArgumentException if ir is null
*/
public DefUse(final IR ir) {
if (ir == null) {
throw new IllegalArgumentException("ir is null");
}
this.ir = ir;
// set up mapping from integer -> instruction
initAllInstructions();
defs = new SSAInstruction[getMaxValueNumber() + 1];
uses = new MutableIntSet[getMaxValueNumber() + 1];
if (DEBUG) {
System.err.println(("DefUse: defs.length " + defs.length));
}
Iterator it = allInstructions.iterator();
for (int i = 0; i < allInstructions.size(); i++) {
SSAInstruction s = (SSAInstruction) it.next();
if (s == null) {
continue;
}
for (int j = 0; j < getNumberOfDefs(s); j++) {
defs[getDef(s, j)] = s;
}
for (int j = 0; j < getNumberOfUses(s); j++) {
int use = getUse(s, j);
try {
if (use != -1) {
if (uses[use] == null) {
uses[use] = IntSetUtil.make();
}
uses[use].add(i);
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new InternalError("unexpected value number " + use);
}
}
}
}
/**
* @return the maximum value number in a particular IR
*/
protected int getMaxValueNumber() {
return ir.getSymbolTable().getMaxValueNumber();
}
/**
* Initialize the allInstructions field with every {@link SSAInstruction} in the ir.
*/
protected void initAllInstructions() {
for (Iterator<SSAInstruction> it = ir.iterateAllInstructions(); it.hasNext();) {
allInstructions.add(it.next());
}
}
/**
* What is the ith value number defined by instruction s?
*/
protected int getDef(SSAInstruction s, int i) {
return s.getDef(i);
}
/**
* What is the ith value number used by instruction s?
*/
protected int getUse(SSAInstruction s, int i) {
return s.getUse(i);
}
/**
* How many value numbers does instruction s def?
*/
protected int getNumberOfDefs(SSAInstruction s) {
return s.getNumberOfDefs();
}
/**
* How many value numbers does instruction s use?
*/
protected int getNumberOfUses(SSAInstruction s) {
return s.getNumberOfUses();
}
/**
* @return the {@link SSAInstruction} that defines the variable with value number v.
*/
public SSAInstruction getDef(int v) {
return (v < defs.length) ? defs[v] : null;
}
/**
* Return all uses of the variable with the given value number
*/
public Iterator<SSAInstruction> getUses(int v) {
if (uses[v] == null) {
return EmptyIterator.instance();
} else {
return new UseIterator(uses[v]);
}
}
/**
* return an {@link Iterator} of all instructions that use any of a set of variables
*/
private class UseIterator implements Iterator<SSAInstruction> {
final IntIterator it;
/**
* @param uses the set of value numbers whose uses this object iterates over
*/
UseIterator(IntSet uses) {
it = uses.intIterator();
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public SSAInstruction next() {
return allInstructions.get(it.next());
}
@Override
public void remove() {
Assertions.UNREACHABLE();
}
}
/**
* @param v a value number
* @return the number of uses of the variable with the given value number
*/
public int getNumberOfUses(int v) {
return uses[v] == null ? 0 : uses[v].size();
}
}