1 salcianu 1.1 // WriteBarriers.java, created Sat Apr 12 15:50:32 2003 by salcianu
  2 salcianu 1.1 // Copyright (C) 2003 Alexandru Salcianu <salcianu@MIT.EDU>
  3 salcianu 1.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 salcianu 1.1 package harpoon.Main;
  5 salcianu 1.1 
  6 salcianu 1.1 import harpoon.ClassFile.CachingCodeFactory;
  7 salcianu 1.1 
  8 salcianu 1.1 import harpoon.Analysis.Quads.QuadClassHierarchy;
  9 salcianu 1.1 import harpoon.Analysis.PreciseGC.WriteBarrierPrePass;
 10 salcianu 1.1 import harpoon.Analysis.PreciseGC.WriteBarrierStats;
 11 salcianu 1.1 import harpoon.Analysis.PreciseGC.WriteBarrierTreePass;
 12 salcianu 1.1 
 13 salcianu 1.1 import harpoon.Util.Options.Option;
 14 salcianu 1.1 
 15 salcianu 1.1 import java.util.List;
 16 salcianu 1.1 import java.util.LinkedList;
 17 salcianu 1.1 import java.io.FileOutputStream;
 18 salcianu 1.1 import java.io.PrintStream;
 19 salcianu 1.1 
 20 salcianu 1.1 /**
 21 salcianu 1.1  * <code>WriteBarriers</code>
 22 salcianu 1.1  * 
 23 salcianu 1.1  * @author  Alexandru Salcianu <salcianu@MIT.EDU>
 24 salcianu 1.1  * @version $Id: WriteBarriers.java,v 1.1 2003/04/17 00:19:29 salcianu Exp $
 25 salcianu 1.1  */
 26 salcianu 1.1 public abstract class WriteBarriers {
 27 salcianu 1.1     
 28 salcianu 1.1     static boolean DYNAMICWBS = false;
 29 salcianu 1.1     static boolean WRITEBARRIERS = false;
 30 salcianu 1.1     static boolean WB_STATISTICS = false;
 31 salcianu 1.1     static WriteBarrierStats writeBarrierStats = null;
 32 salcianu 1.1 
 33 salcianu 1.1     private static int WB_TRANSFORMS = 0; // no transformations by default
 34 salcianu 1.1     static int wbOptLevel = 0; // no removals by default
 35 salcianu 1.1     private static PrintStream wbps = null;
 36 salcianu 1.1 
 37 salcianu 1.1     
 38 salcianu 1.1     private static List/*<Option>*/ getOptions() {
 39 salcianu 1.1         List/*<Option>*/ opts = new LinkedList/*<Option>*/();
 40 salcianu 1.1         
 41 salcianu 1.1         opts.add(new Option("w", "", "<wbOptLevel>",
 42 salcianu 1.1                             "Add write barriers (KKZ)") {
 43 salcianu 1.1             public void action() {
 44 salcianu 1.1                 WRITEBARRIERS = true;
 45 salcianu 1.1                 assert DYNAMICWBS == false :
 46 salcianu 1.1                     "Only one of options -d and -w may be selected.";
 47 salcianu 1.1                 String arg = getOptionalArg(0);
 48 salcianu 1.1                 if (arg != null) {
 49 salcianu 1.1                     try {
 50 salcianu 1.1                         wbOptLevel = Integer.parseInt(arg);
 51 salcianu 1.1                         assert wbOptLevel >= 0 && wbOptLevel <= 6;
 52 salcianu 1.1                     } catch (Exception e) {
 53 salcianu 1.1                         System.err.println(e);
 54 salcianu 1.1                         System.exit(1);
 55 salcianu 1.1                     }
 56 salcianu 1.1                 }
 57 salcianu 1.1             }
 58 salcianu 1.1         });
 59 salcianu 1.1         
 60 salcianu 1.1         opts.add(new Option("d", "", "<wbOptLevel>",
 61 salcianu 1.1                             "Dynamic write barriers (KKZ)") {
 62 salcianu 1.1             public void action() {
 63 salcianu 1.1                 DYNAMICWBS = true;
 64 salcianu 1.1                 assert WRITEBARRIERS == false :
 65 salcianu 1.1                     "Only one of options -d and -w may be selected.";
 66 salcianu 1.1                 String arg = getOptionalArg(0);
 67 salcianu 1.1                 if (arg != null) {
 68 salcianu 1.1                     try {
 69 salcianu 1.1                         wbOptLevel = Integer.parseInt(arg);
 70 salcianu 1.1                         assert wbOptLevel == 0 || wbOptLevel == 1;
 71 salcianu 1.1                     } catch (Exception e) {
 72 salcianu 1.1                         System.err.println(e);
 73 salcianu 1.1                         System.exit(1);
 74 salcianu 1.1                     }
 75 salcianu 1.1                 }
 76 salcianu 1.1             }
 77 salcianu 1.1         });
 78 salcianu 1.1 
 79 salcianu 1.1         opts.add(new Option("x", "", "<outputFile>",
 80 salcianu 1.1                             "Compile with write barrier statistics (KKZ)") {
 81 salcianu 1.1             public void action() {
 82 salcianu 1.1                 WB_STATISTICS = true;
 83 salcianu 1.1                 String arg = getOptionalArg(0);
 84 salcianu 1.1                 if (arg != null) {
 85 salcianu 1.1                     try {
 86 salcianu 1.1                         wbps = 
 87 salcianu 1.1                             new PrintStream(new FileOutputStream(arg, true));
 88 salcianu 1.1                         System.out.println
 89 salcianu 1.1                             ("Writing write barrier list to file "+arg);
 90 salcianu 1.1                     } catch (Exception e) {
 91 salcianu 1.1                         System.err.println(e);
 92 salcianu 1.1                         System.exit(1);
 93 salcianu 1.1                     }
 94 salcianu 1.1                 }
 95 salcianu 1.1             }
 96 salcianu 1.1         });
 97 salcianu 1.1 
 98 salcianu 1.1         opts.add(new Option("y", "", "<WBTransfLevel>",
 99 salcianu 1.1                             "Compile with transformations (KKZ)") {
100 salcianu 1.1             public void action() {
101 salcianu 1.1                 String arg = getOptionalArg(0);
102 salcianu 1.1                 if (arg != null) {
103 salcianu 1.1                     // select transformation level
104 salcianu 1.1                     WB_TRANSFORMS = Integer.parseInt(arg);
105 salcianu 1.1                     assert WB_TRANSFORMS >= 0 && WB_TRANSFORMS <= 3;
106 salcianu 1.1                 } else {
107 salcianu 1.1                     // both transformations on
108 salcianu 1.1                     WB_TRANSFORMS = 3;
109 salcianu 1.1                 }
110 salcianu 1.1             }
111 salcianu 1.1         });
112 salcianu 1.1 
113 salcianu 1.1         return opts;
114 salcianu 1.1     }
115 salcianu 1.1     
116 salcianu 1.1 
117 salcianu 1.1     public static class WBQuadPass extends CompilerStageEZ {
118 salcianu 1.1         public WBQuadPass() { super("write-barrier[-removal].quad-pass"); }
119 salcianu 1.1         public boolean enabled() { return WRITEBARRIERS; }
120 salcianu 1.1 
121 salcianu 1.1         public List/*<Option>*/ getOptions() {
122 salcianu 1.1             return WriteBarriers.getOptions();
123 salcianu 1.1         }
124 salcianu 1.1 
125 salcianu 1.1         public void real_action() {
126 salcianu 1.1             System.out.println("Using write barriers for generational gc.");
127 salcianu 1.1             if (wbOptLevel != 0)
128 salcianu 1.1                 System.out.println
129 salcianu 1.1                     ("Removing write barriers at optimization level " +
130 salcianu 1.1                      wbOptLevel+".");
131 salcianu 1.1             String rName = frame.getRuntime().resourcePath
132 salcianu 1.1                 ("writebarrier-safe.properties");
133 salcianu 1.1             System.out.println(rName);
134 salcianu 1.1             if (WB_TRANSFORMS == 2 || WB_TRANSFORMS == 3) {
135 salcianu 1.1                 // transform recursive constructors
136 salcianu 1.1                 hcf = new harpoon.Analysis.PreciseGC.RCTransformer
137 salcianu 1.1                     (hcf, classHierarchy, linker).codeFactory();
138 salcianu 1.1                 // re-generate class hierarchy to handle modified methods
139 salcianu 1.1                 classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
140 salcianu 1.1             }
141 salcianu 1.1             if (WB_TRANSFORMS == 1 || WB_TRANSFORMS == 3) {
142 salcianu 1.1                 // perform allocation hoisting
143 salcianu 1.1                 System.out.println("Performing allocation hoisting.");
144 salcianu 1.1                 hcf = new CachingCodeFactory(hcf);
145 salcianu 1.1                 hcf = new harpoon.Analysis.PreciseGC.AllocationHoisting
146 salcianu 1.1                     (hcf, classHierarchy, linker, rName, 
147 salcianu 1.1                      (wbOptLevel != 0) ? wbOptLevel : 2).codeFactory();
148 salcianu 1.1                 // re-generate class hierarchy to handle modified methods
149 salcianu 1.1                 classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
150 salcianu 1.1             }
151 salcianu 1.1             hcf = new CachingCodeFactory(hcf);
152 salcianu 1.1             hcf = (new harpoon.Analysis.PreciseGC.WriteBarrierQuadPass
153 salcianu 1.1                    (classHierarchy, hcf, linker, rName, wbOptLevel)).
154 salcianu 1.1                 codeFactory();
155 salcianu 1.1             // re-generate class hierarchy to handle added calls
156 salcianu 1.1             classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
157 salcianu 1.1         }
158 salcianu 1.1     };
159 salcianu 1.1 
160 salcianu 1.1 
161 salcianu 1.1 
162 salcianu 1.1     public static class DynamicWBQuadPass extends CompilerStageEZ {
163 salcianu 1.1         public DynamicWBQuadPass() { super("dynamic-write-barrier.quad-pass");}
164 salcianu 1.1         public boolean enabled() { return DYNAMICWBS; }
165 salcianu 1.1 
166 salcianu 1.1         public void real_action() {
167 salcianu 1.1             System.out.println
168 salcianu 1.1                 ("Using dynamic write barriers for generational gc.");
169 salcianu 1.1             if (wbOptLevel == 1)
170 salcianu 1.1                 System.out.println("Optimistically removing write barriers.");
171 salcianu 1.1             harpoon.Analysis.PreciseGC.DynamicWBQuadPass dynamicWB = null;
172 salcianu 1.1             if (wbOptLevel == 1 || WB_STATISTICS) {
173 salcianu 1.1                 dynamicWB = new harpoon.Analysis.PreciseGC.DynamicWBQuadPass
174 salcianu 1.1                     (hcf, linker);
175 salcianu 1.1                 hcf = dynamicWB.codeFactory();
176 salcianu 1.1             }
177 salcianu 1.1             hcf = (wbOptLevel == 0) ?
178 salcianu 1.1                 (new harpoon.Analysis.PreciseGC.WriteBarrierInserter
179 salcianu 1.1                  (hcf, linker)).codeFactory() :
180 salcianu 1.1                 (new harpoon.Analysis.PreciseGC.WriteBarrierInserter
181 salcianu 1.1                  (hcf, linker, dynamicWB)).codeFactory();
182 salcianu 1.1             // re-generate class hierarchy to handle added calls
183 salcianu 1.1             classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
184 salcianu 1.1         }
185 salcianu 1.1     };
186 salcianu 1.1 
187 salcianu 1.1 
188 salcianu 1.1     public static class WBDynamicWBTreePass extends CompilerStageEZ {
189 salcianu 1.1         public WBDynamicWBTreePass() { super("wb-dynamicwb.tree-pass"); }
190 salcianu 1.1         public boolean enabled() { return WRITEBARRIERS || DYNAMICWBS; }
191 salcianu 1.1 
192 salcianu 1.1         public void real_action() {
193 salcianu 1.1             // run constant propagation
194 salcianu 1.1             hcf = new harpoon.Analysis.Tree.ConstantPropagation(hcf).
195 salcianu 1.1                 codeFactory();
196 salcianu 1.1             // remove write barriers for assignment to constants
197 salcianu 1.1             hcf = harpoon.Analysis.PreciseGC.WriteBarrierConstElim.codeFactory
198 salcianu 1.1                 (frame, hcf, linker);
199 salcianu 1.1             if (WB_STATISTICS) {
200 salcianu 1.1                 System.out.println("Compiling for write barrier statistics.");
201 salcianu 1.1                 if (wbps != null)
202 salcianu 1.1                     wbps.println("\nWRITE BARRIER LIST FOR " + 
203 salcianu 1.1                                  SAMain.className);
204 salcianu 1.1                 writeBarrierStats = 
205 salcianu 1.1                     new harpoon.Analysis.PreciseGC.WriteBarrierStats
206 salcianu 1.1                     (frame, hcf, classHierarchy, wbps, linker);
207 salcianu 1.1                 hcf = writeBarrierStats.codeFactory();
208 salcianu 1.1             } else {
209 salcianu 1.1                 hcf = harpoon.Analysis.PreciseGC.WriteBarrierTreePass.
210 salcianu 1.1                     codeFactory(hcf, frame, classHierarchy, linker);
211 salcianu 1.1             }
212 salcianu 1.1         }
213 salcianu 1.1     };
214 salcianu 1.1     
215 salcianu 1.1 }