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.regalloc;
014
015/**
016 * Implements a basic live interval (no holes), which is a pair
017 * <pre>
018 *   begin    - the starting point of the interval
019 *   end      - the ending point of the interval
020 * </pre>
021 *
022 * <p> Begin and end are numbers given to each instruction by a numbering pass.
023 */
024class BasicInterval {
025
026  /**
027   * DFN of the beginning instruction of this interval
028   */
029  protected final int begin;
030  /**
031   * DFN of the last instruction of this interval
032   */
033  protected int end;
034
035  BasicInterval(int begin, int end) {
036    this.begin = begin;
037    this.end = end;
038  }
039
040  /**
041   * @return the DFN signifying the beginning of this basic interval
042   */
043  final int getBegin() {
044    return begin;
045  }
046
047  /**
048   * @return the DFN signifying the end of this basic interval
049   */
050  final int getEnd() {
051    return end;
052  }
053
054  /**
055   * Extends a live interval to a new endpoint.
056   *
057   * @param newEnd the new end point
058   */
059  final void setEnd(int newEnd) {
060    end = newEnd;
061  }
062
063  final boolean startsAfter(int dfn) {
064    return begin > dfn;
065  }
066
067  final boolean startsBefore(int dfn) {
068    return begin < dfn;
069  }
070
071  final boolean endsBefore(int dfn) {
072    return end < dfn;
073  }
074
075  final boolean endsAfter(int dfn) {
076    return end > dfn;
077  }
078
079  final boolean contains(int dfn) {
080    return begin <= dfn && end >= dfn;
081  }
082
083  final boolean startsBefore(BasicInterval i) {
084    return begin < i.begin;
085  }
086
087  final boolean endsAfter(BasicInterval i) {
088    return end > i.end;
089  }
090
091  final boolean sameRange(BasicInterval i) {
092    return begin == i.begin && end == i.end;
093  }
094
095  final boolean intersects(BasicInterval i) {
096    int iBegin = i.getBegin();
097    int iEnd = i.getEnd();
098    return !(endsBefore(iBegin + 1) || startsAfter(iEnd - 1));
099  }
100
101  @Override
102  public int hashCode() {
103    final int prime = 31;
104    int result = 1;
105    result = prime * result + begin;
106    result = prime * result + end;
107    return result;
108  }
109
110  @Override
111  public boolean equals(Object obj) {
112    if (this == obj)
113      return true;
114    if (obj == null)
115      return false;
116    if (getClass() != obj.getClass())
117      return false;
118    BasicInterval other = (BasicInterval) obj;
119    return sameRange(other);
120  }
121
122  @Override
123  public String toString() {
124    String s = "[ " + begin + ", " + end + " ] ";
125    return s;
126  }
127}