1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
16 // A simple struct to represent (write) effects. A write is represented as a
17 // modification of type bounds (e.g. of a variable).
19 // An effect can either be definite, if the write is known to have taken place,
20 // or 'possible', if it was optional. The difference is relevant when composing
23 // There are two ways to compose effects: sequentially (they happen one after
24 // the other) or alternatively (either one or the other happens). A definite
25 // effect cancels out any previous effect upon sequencing. A possible effect
26 // merges into a previous effect, i.e., type bounds are merged. Alternative
27 // composition always merges bounds. It yields a possible effect if at least
28 // one was only possible.
30 enum Modality { POSSIBLE, DEFINITE };
35 Effect() : modality(DEFINITE) {}
36 Effect(Bounds b, Modality m = DEFINITE) : modality(m), bounds(b) {}
38 // The unknown effect.
39 static Effect Unknown(Zone* zone) {
40 return Effect(Bounds::Unbounded(zone), POSSIBLE);
43 static Effect Forget(Zone* zone) {
44 return Effect(Bounds::Unbounded(zone), DEFINITE);
47 // Sequential composition, as in 'e1; e2'.
48 static Effect Seq(Effect e1, Effect e2, Zone* zone) {
49 if (e2.modality == DEFINITE) return e2;
50 return Effect(Bounds::Either(e1.bounds, e2.bounds, zone), e1.modality);
53 // Alternative composition, as in 'cond ? e1 : e2'.
54 static Effect Alt(Effect e1, Effect e2, Zone* zone) {
56 Bounds::Either(e1.bounds, e2.bounds, zone),
57 e1.modality == POSSIBLE ? POSSIBLE : e2.modality);
62 // Classes encapsulating sets of effects on variables.
64 // Effects maps variables to effects and supports sequential and alternative
67 // NestedEffects is an incremental representation that supports persistence
68 // through functional extension. It represents the map as an adjoin of a list
69 // of maps, whose tail can be shared.
71 // Both classes provide similar interfaces, implemented in parts through the
72 // EffectsMixin below (using sandwich style, to work around the style guide's
75 // We also (ab)use Effects/NestedEffects as a representation for abstract
76 // store typings. In that case, only definite effects are of interest.
78 template<class Var, class Base, class Effects>
79 class EffectsMixin: public Base {
81 explicit EffectsMixin(Zone* zone) : Base(zone) {}
83 Effect Lookup(Var var) {
85 return this->Find(var, &locator)
86 ? locator.value() : Effect::Unknown(Base::zone());
89 Bounds LookupBounds(Var var) {
90 Effect effect = Lookup(var);
91 return effect.modality == Effect::DEFINITE
92 ? effect.bounds : Bounds::Unbounded(Base::zone());
95 // Sequential composition.
96 void Seq(Var var, Effect effect) {
98 if (!this->Insert(var, &locator)) {
99 effect = Effect::Seq(locator.value(), effect, Base::zone());
101 locator.set_value(effect);
104 void Seq(Effects that) {
105 SeqMerger<EffectsMixin> merge = { *this };
106 that.ForEach(&merge);
109 // Alternative composition.
110 void Alt(Var var, Effect effect) {
112 if (!this->Insert(var, &locator)) {
113 effect = Effect::Alt(locator.value(), effect, Base::zone());
115 locator.set_value(effect);
118 void Alt(Effects that) {
119 AltWeakener<EffectsMixin> weaken = { *this, that };
120 this->ForEach(&weaken);
121 AltMerger<EffectsMixin> merge = { *this };
122 that.ForEach(&merge);
127 Overrider override = {
128 Effect::Forget(Base::zone()), Effects(Base::zone()) };
129 this->ForEach(&override);
130 Seq(override.effects);
134 typedef typename Base::Locator Locator;
138 void Call(Var var, Effect effect) { self.Seq(var, effect); }
144 void Call(Var var, Effect effect) { self.Alt(var, effect); }
150 void Call(Var var, Effect effect) {
151 if (effect.modality == Effect::DEFINITE && !other.Contains(var)) {
152 effect.modality = Effect::POSSIBLE;
154 self.Insert(var, &locator);
155 locator.set_value(effect);
163 void Call(Var var, Effect effect) { effects.Seq(var, new_effect); }
170 template<class Var, Var kNoVar> class Effects;
171 template<class Var, Var kNoVar> class NestedEffectsBase;
173 template<class Var, Var kNoVar>
176 explicit EffectsBase(Zone* zone) : map_(new(zone) Mapping(zone)) {}
178 bool IsEmpty() { return map_->is_empty(); }
181 friend class NestedEffectsBase<Var, kNoVar>;
183 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >;
185 Zone* zone() { return map_->allocator().zone(); }
187 struct SplayTreeConfig {
189 typedef Effect Value;
190 static const Var kNoKey = kNoVar;
191 static Effect NoValue() { return Effect(); }
192 static int Compare(int x, int y) { return y - x; }
194 typedef ZoneSplayTree<SplayTreeConfig> Mapping;
195 typedef typename Mapping::Locator Locator;
197 bool Contains(Var var) {
198 ASSERT(var != kNoVar);
199 return map_->Contains(var);
201 bool Find(Var var, Locator* locator) {
202 ASSERT(var != kNoVar);
203 return map_->Find(var, locator);
205 bool Insert(Var var, Locator* locator) {
206 ASSERT(var != kNoVar);
207 return map_->Insert(var, locator);
210 template<class Callback>
211 void ForEach(Callback* callback) {
212 return map_->ForEach(callback);
219 template<class Var, Var kNoVar>
220 const Var EffectsBase<Var, kNoVar>::SplayTreeConfig::kNoKey;
222 template<class Var, Var kNoVar>
223 class Effects: public
224 EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > {
226 explicit Effects(Zone* zone)
227 : EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >(zone)
232 template<class Var, Var kNoVar>
233 class NestedEffectsBase {
235 explicit NestedEffectsBase(Zone* zone) : node_(new(zone) Node(zone)) {}
237 template<class Callback>
238 void ForEach(Callback* callback) {
239 if (node_->previous) NestedEffectsBase(node_->previous).ForEach(callback);
240 node_->effects.ForEach(callback);
243 Effects<Var, kNoVar> Top() { return node_->effects; }
246 for (Node* node = node_; node != NULL; node = node->previous) {
247 if (!node->effects.IsEmpty()) return false;
253 typedef typename EffectsBase<Var, kNoVar>::Locator Locator;
255 Zone* zone() { return node_->zone; }
257 void push() { node_ = new(node_->zone) Node(node_->zone, node_); }
258 void pop() { node_ = node_->previous; }
259 bool is_empty() { return node_ == NULL; }
261 bool Contains(Var var) {
262 ASSERT(var != kNoVar);
263 for (Node* node = node_; node != NULL; node = node->previous) {
264 if (node->effects.Contains(var)) return true;
269 bool Find(Var var, Locator* locator) {
270 ASSERT(var != kNoVar);
271 for (Node* node = node_; node != NULL; node = node->previous) {
272 if (node->effects.Find(var, locator)) return true;
277 bool Insert(Var var, Locator* locator);
280 struct Node: ZoneObject {
282 Effects<Var, kNoVar> effects;
284 explicit Node(Zone* zone, Node* previous = NULL)
285 : zone(zone), effects(zone), previous(previous) {}
288 explicit NestedEffectsBase(Node* node) : node_(node) {}
294 template<class Var, Var kNoVar>
295 bool NestedEffectsBase<Var, kNoVar>::Insert(Var var, Locator* locator) {
296 ASSERT(var != kNoVar);
297 if (!node_->effects.Insert(var, locator)) return false;
299 for (Node* node = node_->previous; node != NULL; node = node->previous) {
300 if (node->effects.Find(var, &shadowed)) {
301 // Initialize with shadowed entry.
302 locator->set_value(shadowed.value());
310 template<class Var, Var kNoVar>
311 class NestedEffects: public
312 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > {
314 explicit NestedEffects(Zone* zone) :
315 EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >(
318 // Create an extension of the current effect set. The current set should not
319 // be modified while the extension is in use.
320 NestedEffects Push() {
321 NestedEffects result = *this;
326 NestedEffects Pop() {
327 NestedEffects result = *this;
329 ASSERT(!this->is_empty());
334 } } // namespace v8::internal
336 #endif // V8_EFFECTS_H_