CACAO
Interval.hpp
Go to the documentation of this file.
1 /* src/vm/jit/loop/Interval.hpp
2 
3  Copyright (C) 1996-2012
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #ifndef _INTERVAL_HPP
26 #define _INTERVAL_HPP
27 
28 #include <limits>
29 #include <iostream>
30 
31 #include "vm/types.hpp"
32 #include "Scalar.hpp"
33 
34 /**
35  * An integer interval of the form
36  * constant_0 + instruction_0 .. constant_1 + instruction_1
37  * where both bounds are included.
38  */
39 class Interval
40 {
43 
44 public:
45 
46  static s4 min() { return Scalar::min(); }
47  static s4 max() { return Scalar::max(); }
48 
49  /**
50  * Creates the interval MIN .. MAX
51  */
53  : _lower(Scalar(min()))
54  , _upper(Scalar(max()))
55  {}
56 
57  explicit Interval(const Scalar& s)
58  : _lower(s)
59  , _upper(s)
60  {}
61 
62  Interval(const Scalar& lower, const Scalar& upper)
63  : _lower(lower)
64  , _upper(upper)
65  {}
66 
67  Scalar lower() const { return _lower; }
68  void lower(const Scalar& s) { _lower = s; }
69 
70  Scalar upper() const { return _upper; }
71  void upper(const Scalar& s) { _upper = s; }
72 
73  /**
74  * true if it can be proven that arrayVariable can be accessed at an index
75  * whose value lies within this interval.
76  */
77  bool isWithinBounds(s4 arrayVariable) const;
78 
79  /**
80  * Computes a superset of the intersection of this interval with the specified interval.
81  * The result is stored in this object.
82  */
83  void intersectWith(const Interval&);
84 
85  /**
86  * Computes a superset of the union of this interval with the specified interval.
87  * The result is stored in this object.
88  */
89  void unionWith(const Interval&);
90 
91  /**
92  * Tries to remove the specified scalar from this interval.
93  * This interval is only changed if the result can be represented as an interval.
94  */
95  void tryRemove(const Scalar&);
96 };
97 
98 inline bool Interval::isWithinBounds(s4 arrayVariable) const
99 {
100  return _lower.lower() >= 0 // index >= 0
101  && _upper.instruction().kind() == NumericInstruction::ARRAY_LENGTH // index <= ? + ?.length
102  && _upper.instruction().variable() == arrayVariable // index <= ? + arrayVariable.length
103  && _upper.constant() < 0; // index <= -1 + arrayVariable.length
104 }
105 
106 inline void Interval::intersectWith(const Interval& other)
107 {
110 }
111 
112 inline void Interval::unionWith(const Interval& other)
113 {
116 }
117 
118 inline void Interval::tryRemove(const Scalar& point)
119 {
120  // Remove point from interval if it is an endpoint.
121  if (point == _lower)
122  {
123  _lower.tryAdd(Scalar(1));
124  }
125  else if (point == _upper)
126  {
128  }
129 }
130 
131 // True if both endpoints are equal, otherwise false.
132 inline bool operator==(const Interval& a, const Interval& b)
133 {
134  return a.lower() == b.lower() && a.upper() == b.upper();
135 }
136 
137 // True if the intervals are not equal, otherwise false.
138 inline bool operator!=(const Interval& a, const Interval& b)
139 {
140  return a.lower() != b.lower() || a.upper() != b.upper();
141 }
142 
143 inline std::ostream& operator<<(std::ostream& out, const Interval& interval)
144 {
145  return out << '[' << interval.lower() << ',' << interval.upper() << ']';
146 }
147 
148 #endif
149 
150 /*
151  * These are local overrides for various environment variables in Emacs.
152  * Please do not remove this and leave it at the end of the file, where
153  * Emacs will automagically detect them.
154  * ---------------------------------------------------------------------
155  * Local variables:
156  * mode: c++
157  * indent-tabs-mode: t
158  * c-basic-offset: 4
159  * tab-width: 4
160  * End:
161  * vim:noexpandtab:sw=4:ts=4:
162  */
163 
void lowerBoundOfMinimumWith(const Scalar &)
Computes a lower bound of the minimum of this and the specified scalar.
Definition: Scalar.cpp:59
static s4 min()
Definition: Interval.hpp:46
Scalar _lower
Definition: Interval.hpp:41
void intersectWith(const Interval &)
Computes a superset of the intersection of this interval with the specified interval.
Definition: Interval.hpp:106
Scalar lower() const
Definition: Interval.hpp:67
static s4 min()
Definition: Scalar.hpp:47
Interval(const Scalar &lower, const Scalar &upper)
Definition: Interval.hpp:62
Interval()
Creates the interval MIN .
Definition: Interval.hpp:52
An integer interval of the form constant_0 + instruction_0 .
Definition: Interval.hpp:39
Scalar _upper
Definition: Interval.hpp:42
An integral value of the form Constant + NumericInstruction.
Definition: Scalar.hpp:40
void tryRemove(const Scalar &)
Tries to remove the specified scalar from this interval.
Definition: Interval.hpp:118
Scalar upper() const
Definition: Interval.hpp:70
void unionWith(const Interval &)
Computes a superset of the union of this interval with the specified interval.
Definition: Interval.hpp:112
bool operator!=(const ordered_list< T, Allocator > &lhs, const ordered_list< T, Allocator > &rhs)
inequality
static s4 max()
Definition: Scalar.hpp:48
bool isWithinBounds(s4 arrayVariable) const
true if it can be proven that arrayVariable can be accessed at an index whose value lies within this ...
Definition: Interval.hpp:98
int32_t s4
Definition: types.hpp:45
void upper(const Scalar &s)
Definition: Interval.hpp:71
OStream & operator<<(OStream &OS, const std::string &t)
Definition: OStream.hpp:459
void upperBoundOfMaximumWith(const Scalar &)
Computes an upper bound of the maximum of this and the specified scalar.
Definition: Scalar.cpp:30
bool operator==(const ordered_list< T, Allocator > &lhs, const ordered_list< T, Allocator > &rhs)
equality
NumericInstruction instruction() const
Definition: Scalar.hpp:87
bool tryAdd(const Scalar &)
Tries to add a scalar to this scalar.
Definition: Scalar.cpp:88
void upperBoundOfMinimumWith(const Scalar &)
Computes an upper bound of the minimum of this and the specified scalar.
Definition: Scalar.hpp:134
void lowerBoundOfMaximumWith(const Scalar &)
Computes a lower bound of the maximum of this and the specified scalar.
Definition: Scalar.hpp:140
OStream & out()
Definition: OStream.cpp:39
static s4 max()
Definition: Interval.hpp:47
bool trySubtract(const Scalar &)
Tries to subtract a scalar from this scalar.
Definition: Scalar.cpp:118
Interval(const Scalar &s)
Definition: Interval.hpp:57
s4 lower() const
Definition: Scalar.hpp:89
void lower(const Scalar &s)
Definition: Interval.hpp:68
s4 constant() const
Definition: Scalar.hpp:86