001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.lir2mir;
014
015import org.jikesrvm.compilers.opt.ir.GenericPhysicalRegisterTools;
016import org.jikesrvm.compilers.opt.ir.GenericRegisterPool;
017import org.jikesrvm.compilers.opt.ir.IR;
018import org.jikesrvm.compilers.opt.ir.Instruction;
019import org.jikesrvm.compilers.opt.ir.operand.AddressConstantOperand;
020import org.jikesrvm.compilers.opt.ir.operand.ConditionOperand;
021import org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand;
022import org.jikesrvm.compilers.opt.ir.operand.LongConstantOperand;
023import org.jikesrvm.compilers.opt.ir.operand.Operand;
024import org.jikesrvm.compilers.opt.ir.operand.RegisterOperand;
025import org.jikesrvm.util.Bits;
026import org.vmmagic.unboxed.Address;
027
028/**
029 * Contains BURS helper functions common to all platforms.
030 */
031public abstract class BURS_Common_Helpers extends GenericPhysicalRegisterTools {
032
033  /** Infinite cost for a rule */
034  protected static final int INFINITE = 0x7fff;
035
036  /**
037   * The BURS object
038   */
039  protected final BURS burs;
040
041  /**
042   * The register pool of the IR being processed
043   */
044  protected final GenericRegisterPool regpool;
045
046  protected BURS_Common_Helpers(BURS b) {
047    burs = b;
048    regpool = b.ir.regpool;
049  }
050
051  @Override
052  public final IR getIR() {
053    return burs.ir;
054  }
055
056  protected final void EMIT(Instruction s) {
057    burs.append(s);
058  }
059
060  // returns the given operand as a register
061  protected static RegisterOperand R(Operand op) {
062    return op.asRegister();
063  }
064
065  // returns the given operand as an address constant
066  protected static AddressConstantOperand AC(Operand op) {
067    return op.asAddressConstant();
068  }
069
070  // returns the given operand as an integer constant
071  protected static IntConstantOperand IC(Operand op) {
072    return op.asIntConstant();
073  }
074
075  // returns the given operand as a long constant
076  protected static LongConstantOperand LC(Operand op) {
077    return op.asLongConstant();
078  }
079
080  // returns the integer value of the given operand
081  protected static int IV(Operand op) {
082    return IC(op).value;
083  }
084
085  // returns the long value of the given operand
086  protected static long LV(Operand op) {
087    return LC(op).value;
088  }
089
090  // returns the Address value of the given operand
091  protected static Address AV(Operand op) {
092    return AC(op).value;
093  }
094
095  // is a == 0?
096  protected static boolean ZERO(Operand a) {
097    return (IV(a) == 0);
098  }
099
100  // is a == 1?
101  protected static boolean ONE(Operand a) {
102    return (IV(a) == 1);
103  }
104
105  // is a == -1?
106  protected static boolean MINUSONE(Operand a) {
107    return (IV(a) == -1);
108  }
109
110  protected static int FITS(Operand op, int numBits, int trueCost) {
111    return FITS(op, numBits, trueCost, INFINITE);
112  }
113
114  protected static int FITS(Operand op, int numBits, int trueCost, int falseCost) {
115    if (op.isIntConstant() && Bits.fits(IV(op), numBits)) {
116      return trueCost;
117    } else if (op.isAddressConstant() && Bits.fits(AV(op), numBits)) {
118      return trueCost;
119    } else {
120      return falseCost;
121    }
122  }
123
124  protected static int isZERO(int x, int trueCost) {
125    return isZERO(x, trueCost, INFINITE);
126  }
127
128  protected static int isZERO(int x, int trueCost, int falseCost) {
129    return x == 0 ? trueCost : falseCost;
130  }
131
132  protected static int isONE(int x, int trueCost) {
133    return isONE(x, trueCost, INFINITE);
134  }
135
136  protected static int isONE(int x, int trueCost, int falseCost) {
137    return x == 1 ? trueCost : falseCost;
138  }
139
140  // helper functions for condition operands
141  protected static boolean EQ_NE(ConditionOperand c) {
142    int cond = c.value;
143    return ((cond == ConditionOperand.EQUAL) || (cond == ConditionOperand.NOT_EQUAL));
144  }
145
146  protected static boolean EQ_LT_LE(ConditionOperand c) {
147    int cond = c.value;
148    return ((cond == ConditionOperand.EQUAL) ||
149            (cond == ConditionOperand.LESS) ||
150            (cond == ConditionOperand.LESS_EQUAL));
151  }
152
153  protected static boolean EQ_GT_GE(ConditionOperand c) {
154    int cond = c.value;
155    return ((cond == ConditionOperand.EQUAL) ||
156            (cond == ConditionOperand.GREATER) ||
157            (cond == ConditionOperand.GREATER_EQUAL));
158  }
159
160  /* node accessors */
161  protected static Instruction P(AbstractBURS_TreeNode p) {
162    return p.getInstruction();
163  }
164
165  protected static Instruction PL(AbstractBURS_TreeNode p) {
166    return p.child1.getInstruction();
167  }
168
169  protected static Instruction PLL(AbstractBURS_TreeNode p) {
170    return p.child1.child1.getInstruction();
171  }
172
173  protected static Instruction PLLL(AbstractBURS_TreeNode p) {
174    return p.child1.child1.child1.getInstruction();
175  }
176
177  protected static Instruction PLLLL(AbstractBURS_TreeNode p) {
178    return p.child1.child1.child1.child1.getInstruction();
179  }
180
181  protected static Instruction PLLLLLL(AbstractBURS_TreeNode p) {
182    return p.child1.child1.child1.child1.child1.child1.getInstruction();
183  }
184
185  protected static Instruction PLLLLLLL(AbstractBURS_TreeNode p) {
186    return p.child1.child1.child1.child1.child1.child1.child1.getInstruction();
187  }
188
189  protected static Instruction PLLLRL(AbstractBURS_TreeNode p) {
190    return p.child1.child1.child1.child2.child1.getInstruction();
191  }
192
193  protected static Instruction PLLLRLL(AbstractBURS_TreeNode p) {
194    return p.child1.child1.child1.child2.child1.child1.getInstruction();
195  }
196
197  protected static Instruction PLLLRLLL(AbstractBURS_TreeNode p) {
198    return p.child1.child1.child1.child2.child1.child1.child1.getInstruction();
199  }
200
201  protected static Instruction PLLRLLL(AbstractBURS_TreeNode p) {
202    return p.child1.child1.child2.child1.child1.child1.getInstruction();
203  }
204
205  protected static Instruction PLLR(AbstractBURS_TreeNode p) {
206    return p.child1.child1.child2.getInstruction();
207  }
208
209  protected static Instruction PLLRL(AbstractBURS_TreeNode p) {
210    return p.child1.child1.child2.child1.getInstruction();
211  }
212
213  protected static Instruction PLLRLL(AbstractBURS_TreeNode p) {
214    return p.child1.child1.child2.child1.child1.getInstruction();
215  }
216
217  protected static Instruction PLLRLLR(AbstractBURS_TreeNode p) {
218    return p.child1.child1.child2.child1.child1.child2.getInstruction();
219  }
220
221  protected static Instruction PLR(AbstractBURS_TreeNode p) {
222    return p.child1.child2.getInstruction();
223  }
224
225  protected static Instruction PLRL(AbstractBURS_TreeNode p) {
226    return p.child1.child2.child1.getInstruction();
227  }
228
229  protected static Instruction PLRLL(AbstractBURS_TreeNode p) {
230    return p.child1.child2.child1.child1.getInstruction();
231  }
232
233  protected static Instruction PLRLLRL(AbstractBURS_TreeNode p) {
234    return p.child1.child2.child1.child1.child2.child1.getInstruction();
235  }
236
237  protected static Instruction PLRR(AbstractBURS_TreeNode p) {
238    return p.child1.child2.child2.getInstruction();
239  }
240
241  protected static Instruction PR(AbstractBURS_TreeNode p) {
242    return p.child2.getInstruction();
243  }
244
245  protected static Instruction PRL(AbstractBURS_TreeNode p) {
246    return p.child2.child1.getInstruction();
247  }
248
249  protected static Instruction PRLL(AbstractBURS_TreeNode p) {
250    return p.child2.child1.child1.getInstruction();
251  }
252
253  protected static Instruction PRLLL(AbstractBURS_TreeNode p) {
254    return p.child2.child1.child1.child1.getInstruction();
255  }
256
257  protected static Instruction PRLLLL(AbstractBURS_TreeNode p) {
258    return p.child2.child1.child1.child1.child1.getInstruction();
259  }
260
261  protected static Instruction PRLLR(AbstractBURS_TreeNode p) {
262    return p.child2.child1.child1.child2.getInstruction();
263  }
264
265  protected static Instruction PRLLRLLL(AbstractBURS_TreeNode p) {
266    return p.child2.child1.child1.child2.child1.child1.child1.getInstruction();
267  }
268
269  protected static Instruction PRLR(AbstractBURS_TreeNode p) {
270    return p.child2.child1.child2.getInstruction();
271  }
272
273  protected static Instruction PRLRL(AbstractBURS_TreeNode p) {
274    return p.child2.child1.child2.child1.getInstruction();
275  }
276
277  protected static Instruction PRR(AbstractBURS_TreeNode p) {
278    return p.child2.child2.getInstruction();
279  }
280
281  protected static Instruction PRRL(AbstractBURS_TreeNode p) {
282    return p.child2.child2.child1.getInstruction();
283  }
284
285  protected static Instruction PRRR(AbstractBURS_TreeNode p) {
286    return p.child2.child2.child2.getInstruction();
287  }
288
289  protected static int V(AbstractBURS_TreeNode p) {
290    return ((BURS_IntConstantTreeNode) p).value;
291  }
292
293  protected static int VL(AbstractBURS_TreeNode p) {
294    return ((BURS_IntConstantTreeNode) p.child1).value;
295  }
296
297  protected static int VLL(AbstractBURS_TreeNode p) {
298    return ((BURS_IntConstantTreeNode) p.child1.child1).value;
299  }
300
301  protected static int VLLL(AbstractBURS_TreeNode p) {
302    return ((BURS_IntConstantTreeNode) p.child1.child1.child1).value;
303  }
304
305  protected static int VLLLL(AbstractBURS_TreeNode p) {
306    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child1).value;
307  }
308
309  protected static int VLLLLLR(AbstractBURS_TreeNode p) {
310    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child1.child1.child2).value;
311  }
312
313  protected static int VLLLLLLR(AbstractBURS_TreeNode p) {
314    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child1.child1.child1.child2).value;
315  }
316
317  protected static int VLLLLLLLR(AbstractBURS_TreeNode p) {
318    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child1.child1.child1.child1.child2).value;
319  }
320
321  protected static int VLLLR(AbstractBURS_TreeNode p) {
322    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child2).value;
323  }
324
325  protected static int VLLLLR(AbstractBURS_TreeNode p) {
326    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child1.child2).value;
327  }
328
329  protected static int VLLLRLLLR(AbstractBURS_TreeNode p) {
330    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child2.child1.child1.child1.child2).value;
331  }
332
333  protected static int VLLLRLLR(AbstractBURS_TreeNode p) {
334    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child2.child1.child1.child2).value;
335  }
336
337  protected static int VLLLRLR(AbstractBURS_TreeNode p) {
338    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child2.child1.child2).value;
339  }
340
341  protected static int VLLLRR(AbstractBURS_TreeNode p) {
342    return ((BURS_IntConstantTreeNode) p.child1.child1.child1.child2.child2).value;
343  }
344
345  protected static int VLLR(AbstractBURS_TreeNode p) {
346    return ((BURS_IntConstantTreeNode) p.child1.child1.child2).value;
347  }
348
349  protected static int VLLRLLRR(AbstractBURS_TreeNode p) {
350    return ((BURS_IntConstantTreeNode) p.child1.child1.child2.child1.child1.child2.child2).value;
351  }
352
353  protected static int VLLRLR(AbstractBURS_TreeNode p) {
354    return ((BURS_IntConstantTreeNode) p.child1.child1.child2.child1.child2).value;
355  }
356
357  protected static int VLLRLLLR(AbstractBURS_TreeNode p) {
358    return ((BURS_IntConstantTreeNode) p.child1.child1.child2.child1.child1.child1.child2).value;
359  }
360
361  protected static int VLLRLLR(AbstractBURS_TreeNode p) {
362    return ((BURS_IntConstantTreeNode) p.child1.child1.child2.child1.child1.child2).value;
363  }
364
365  protected static int VLLRR(AbstractBURS_TreeNode p) {
366    return ((BURS_IntConstantTreeNode) p.child1.child1.child2.child2).value;
367  }
368
369  protected static int VLR(AbstractBURS_TreeNode p) {
370    return ((BURS_IntConstantTreeNode) p.child1.child2).value;
371  }
372
373  protected static int VLRLR(AbstractBURS_TreeNode p) {
374    return ((BURS_IntConstantTreeNode) p.child1.child2.child1.child2).value;
375  }
376
377  protected static int VLRL(AbstractBURS_TreeNode p) {
378    return ((BURS_IntConstantTreeNode) p.child1.child2.child1).value;
379  }
380
381  protected static int VLRR(AbstractBURS_TreeNode p) {
382    return ((BURS_IntConstantTreeNode) p.child1.child2.child2).value;
383  }
384
385  protected static int VLRLL(AbstractBURS_TreeNode p) {
386    return ((BURS_IntConstantTreeNode) p.child1.child2.child1.child1).value;
387  }
388
389  protected static int VLRLLRR(AbstractBURS_TreeNode p) {
390    return ((BURS_IntConstantTreeNode) p.child1.child2.child1.child1.child2.child2).value;
391  }
392
393  protected static int VLRRR(AbstractBURS_TreeNode p) {
394    return ((BURS_IntConstantTreeNode) p.child1.child2.child2.child2).value;
395  }
396
397  protected static int VR(AbstractBURS_TreeNode p) {
398    return ((BURS_IntConstantTreeNode) p.child2).value;
399  }
400
401  protected static int VRL(AbstractBURS_TreeNode p) {
402    return ((BURS_IntConstantTreeNode) p.child2.child1).value;
403  }
404
405  protected static int VRLLR(AbstractBURS_TreeNode p) {
406    return ((BURS_IntConstantTreeNode) p.child2.child1.child1.child2).value;
407  }
408
409  protected static int VRLLLR(AbstractBURS_TreeNode p) {
410    return ((BURS_IntConstantTreeNode) p.child2.child1.child1.child1.child2).value;
411  }
412
413  protected static int VRLLLLR(AbstractBURS_TreeNode p) {
414    return ((BURS_IntConstantTreeNode) p.child2.child1.child1.child1.child1.child2).value;
415  }
416
417  protected static int VRLLRLLLR(AbstractBURS_TreeNode p) {
418    return ((BURS_IntConstantTreeNode) p.child2.child1.child1.child2.child1.child1.child1.child2).value;
419  }
420
421  protected static int VRLLRLLR(AbstractBURS_TreeNode p) {
422    return ((BURS_IntConstantTreeNode) p.child2.child1.child1.child2.child1.child1.child2).value;
423  }
424
425  protected static int VRLLRR(AbstractBURS_TreeNode p) {
426    return ((BURS_IntConstantTreeNode) p.child2.child1.child1.child2.child2).value;
427  }
428
429  protected static int VRLRLR(AbstractBURS_TreeNode p) {
430    return ((BURS_IntConstantTreeNode) p.child2.child1.child2.child1.child2).value;
431  }
432
433  protected static int VRLRR(AbstractBURS_TreeNode p) {
434    return ((BURS_IntConstantTreeNode) p.child2.child1.child2.child2).value;
435  }
436
437  protected static int VRLL(AbstractBURS_TreeNode p) {
438    return ((BURS_IntConstantTreeNode) p.child2.child1.child1).value;
439  }
440
441  protected static int VRLR(AbstractBURS_TreeNode p) {
442    return ((BURS_IntConstantTreeNode) p.child2.child1.child2).value;
443  }
444
445  protected static int VRR(AbstractBURS_TreeNode p) {
446    return ((BURS_IntConstantTreeNode) p.child2.child2).value;
447  }
448
449  protected static int VRRL(AbstractBURS_TreeNode p) {
450    return ((BURS_IntConstantTreeNode) p.child2.child2.child1).value;
451  }
452
453  protected static int VRRLR(AbstractBURS_TreeNode p) {
454    return ((BURS_IntConstantTreeNode) p.child2.child2.child1.child2).value;
455  }
456
457  protected static int VRRR(AbstractBURS_TreeNode p) {
458    return ((BURS_IntConstantTreeNode) p.child2.child2.child2).value;
459  }
460}