1 package Analysis.Locality;
5 import IR.MethodDescriptor;
8 public class GenerateConversions {
9 LocalityAnalysis locality;
12 /** Warning: This class modifies the code in place. */
14 public GenerateConversions(LocalityAnalysis la, State state) {
20 private void doConversion() {
21 Set<LocalityBinding> bindings=locality.getLocalityBindings();
22 Iterator<LocalityBinding> bindit=bindings.iterator();
23 while(bindit.hasNext()) {
24 LocalityBinding lb=bindit.next();
25 //Don't need to do conversion if it is already atomic
33 /* At the end of an atomic block, we need to convert any global
34 * references that will be used again into OID's. */
36 private void converttoOid(LocalityBinding lb) {
37 Hashtable<FlatNode, Integer> atomictab=locality.getAtomic(lb);
38 Hashtable<FlatNode, Hashtable<TempDescriptor, Integer>> temptab=locality.getNodeTempInfo(lb);
39 MethodDescriptor md=lb.getMethod();
40 FlatMethod fm=state.getMethodFlat(md);
41 Hashtable<FlatNode, Set<TempNodePair>> nodetotnpair=new Hashtable<FlatNode, Set<TempNodePair>>();
42 Hashtable<FlatNode, Set<TempDescriptor>> nodetoconvs=new Hashtable<FlatNode, Set<TempDescriptor>>();
43 Hashtable<FlatNode, Set<TempDescriptor>> nodetoconvs2=new Hashtable<FlatNode, Set<TempDescriptor>>();
45 Set<FlatNode> toprocess=fm.getNodeSet();
47 while(!toprocess.isEmpty()) {
48 FlatNode fn=toprocess.iterator().next();
50 boolean isatomic=atomictab.get(fn).intValue()>0;
52 Hashtable<TempDescriptor, Integer> nodetemptab=temptab.get(fn);
54 List<TempDescriptor> reads=Arrays.asList(fn.readsTemps());
55 List<TempDescriptor> writes=Arrays.asList(fn.writesTemps());
57 if (!isatomic&&fn.kind()==FKind.FlatAtomicExitNode
58 &&!nodetoconvs.containsKey(fn)) {
59 nodetoconvs.put(fn, new HashSet<TempDescriptor>());
60 nodetoconvs2.put(fn, new HashSet<TempDescriptor>());
63 HashSet<TempNodePair> tempset=new HashSet<TempNodePair>();
65 for(int i=0; i<fn.numPrev(); i++) {
66 FlatNode fnprev=fn.getPrev(i);
67 if (!nodetotnpair.containsKey(fnprev))
70 Set<TempNodePair> prevset=nodetotnpair.get(fnprev);
71 for(Iterator<TempNodePair> it=prevset.iterator(); it.hasNext();) {
72 TempNodePair tnp=it.next();
73 if (fn.kind()==FKind.FlatGlobalConvNode&&
74 ((FlatGlobalConvNode)fn).getLocality()!=lb) {
79 if (reads.contains(tnp.getTemp())&&tnp.getNode()!=null) {
80 //Value actually is read...
81 nodetoconvs.get(tnp.getNode()).add(tnp.getTemp());
84 if (writes.contains(tnp.getTemp())) //value overwritten
86 if (!isatomic&&fn.kind()==FKind.FlatAtomicExitNode) {
87 //Create new node and tag it with this exit
88 if (tnp.getNode()==null) {
89 TempNodePair tnp2=new TempNodePair(tnp.getTemp());
92 nodetoconvs2.get(fn).add(tnp.getTemp()); //have to hide cached copies from gc -- add unused converted temps
100 /* If this is in an atomic block, record temps that
103 /* NOTE: If this compiler is changed to maintain
104 * OID/Ptr's in variables, then we need to use all
105 * global temps that could be read and not just the
106 * ones converted by globalconvnode*/
108 if (fn.kind()!=FKind.FlatGlobalConvNode||
109 ((FlatGlobalConvNode)fn).getLocality()==lb) {
110 /*If globalconvnode, make sure we have the right
112 for(Iterator<TempDescriptor> writeit=writes.iterator(); writeit.hasNext();) {
113 TempDescriptor wrtmp=writeit.next();
114 if (state.SINGLETM) {
115 if (wrtmp.getType().isPtr()&&
116 (nodetemptab.get(wrtmp)!=LocalityAnalysis.SCRATCH)) {
117 TempNodePair tnp=new TempNodePair(wrtmp);
120 } else if (nodetemptab.get(wrtmp)==LocalityAnalysis.GLOBAL) {
121 TempNodePair tnp=new TempNodePair(wrtmp);
127 if (!nodetotnpair.containsKey(fn)||!nodetotnpair.get(fn).equals(tempset)) {
128 //changes to set, so enqueue next nodes
129 nodetotnpair.put(fn, tempset); //update set
130 for(int i=0; i<fn.numNext(); i++) {
131 toprocess.add(fn.getNext(i));
135 //Place Convert to Oid nodes
136 toprocess=fm.getNodeSet();
137 for(Iterator<FlatNode> it=toprocess.iterator(); it.hasNext();) {
138 FlatNode fn=it.next();
139 if (atomictab.get(fn).intValue()==0&&fn.numPrev()>0&&
140 atomictab.get(fn.getPrev(0)).intValue()>0) {
142 assert(fn.kind()==FKind.FlatAtomicExitNode);
143 //insert calls here...
144 Set<TempDescriptor> tempset=nodetoconvs2.get(fn);
145 for(Iterator<TempDescriptor> tempit=tempset.iterator(); tempit.hasNext();) {
146 TempDescriptor tmpd=tempit.next();
147 FlatGlobalConvNode fgcn=new FlatGlobalConvNode(tmpd, lb, false, nodetoconvs.get(fn).contains(tmpd));
148 fgcn.setAtomicEnter(((FlatAtomicExitNode)fn).getAtomicEnter());
149 atomictab.put(fgcn, new Integer(1));
150 temptab.put(fgcn, (Hashtable<TempDescriptor, Integer>)temptab.get(fn).clone());
152 FlatNode[] prevarray=new FlatNode[fn.numPrev()];
153 for(int i=0; i<fn.numPrev(); i++) {
154 FlatNode fnprev=fn.getPrev(i);
157 for(int i=0; i<prevarray.length; i++) {
158 for(int j=0; j<prevarray[i].numNext(); j++) {
159 if (prevarray[i].getNext(j)==fn) {
160 //found index, change node
161 prevarray[i].setNext(j, fgcn);
172 /* At the beginning of an atomic block, we need to convert any
173 * OID's that will be used in the atomic block to pointers */
175 private void converttoPtr(LocalityBinding lb) {
176 Hashtable<FlatNode, Set<TempDescriptor>> nodetotranstemps=new Hashtable<FlatNode, Set<TempDescriptor>>();
177 Hashtable<FlatNode, Integer> atomictab=locality.getAtomic(lb);
178 Hashtable<FlatNode, Hashtable<TempDescriptor, Integer>> temptab=locality.getNodeTempInfo(lb);
179 MethodDescriptor md=lb.getMethod();
180 FlatMethod fm=state.getMethodFlat(md);
181 Set<FlatNode> toprocess=fm.getNodeSet();
183 while(!toprocess.isEmpty()) {
184 FlatNode fn=toprocess.iterator().next();
185 toprocess.remove(fn);
187 if (atomictab.get(fn).intValue()>0) {
188 //build set of transaction temps use by next nodes
189 HashSet<TempDescriptor> transtemps=new HashSet<TempDescriptor>();
190 for(int i=0; i<fn.numNext(); i++) {
191 FlatNode fnnext=fn.getNext(i);
192 if (nodetotranstemps.containsKey(fnnext))
193 transtemps.addAll(nodetotranstemps.get(fnnext));
195 //subtract out the ones we write to
196 transtemps.removeAll(Arrays.asList(fn.writesTemps()));
197 //add in the globals we read from
198 Hashtable<TempDescriptor, Integer> pretemptab=locality.getNodePreTempInfo(lb, fn);
199 if (state.SINGLETM) {
200 TempDescriptor [] readtemps=fn.readsTemps();
201 for(int i=0; i<readtemps.length; i++) {
202 TempDescriptor tmp=readtemps[i];
203 if (tmp.getType().isPtr()&&
204 pretemptab.get(tmp).intValue()!=LocalityAnalysis.SCRATCH) {
209 TempDescriptor [] readtemps=fn.readsTemps();
210 for(int i=0; i<readtemps.length; i++) {
211 TempDescriptor tmp=readtemps[i];
212 if (pretemptab.get(tmp).intValue()==LocalityAnalysis.GLOBAL) {
217 if (!nodetotranstemps.containsKey(fn)||!nodetotranstemps.get(fn).equals(transtemps)) {
218 nodetotranstemps.put(fn, transtemps);
219 for(int i=0; i<fn.numPrev(); i++)
220 toprocess.add(fn.getPrev(i));
224 toprocess=fm.getNodeSet();
225 for(Iterator<FlatNode> it=toprocess.iterator(); it.hasNext();) {
226 FlatNode fn=it.next();
227 if (atomictab.get(fn).intValue()>0&&
228 atomictab.get(fn.getPrev(0)).intValue()==0) {
230 assert(fn.kind()==FKind.FlatAtomicEnterNode);
232 //insert calls here...
233 Set<TempDescriptor> tempset=nodetotranstemps.get(fn);
234 for(Iterator<TempDescriptor> tempit=tempset.iterator(); tempit.hasNext();) {
235 FlatGlobalConvNode fgcn=new FlatGlobalConvNode(tempit.next(), lb, true);
236 fgcn.setAtomicEnter((FlatAtomicEnterNode)fn);
237 atomictab.put(fgcn, atomictab.get(fn));
238 temptab.put(fgcn, (Hashtable<TempDescriptor, Integer>)temptab.get(fn).clone());
239 fgcn.addNext(fn.getNext(0));