SSA Range Cache
The ssa_cache class provides a general-purpose table to store and query ranges associated with SSA names. It implements the range_query interface and is suitable for use anywhere in the compiler that needs a table of value ranges indexed by SSA names.
Basic Usage
To use an ssa_cache, create an instance and populate it as ranges become known. The following API methods are available:
set_range(tree name, const vrange &r)
- Stores the given range for the SSA name, replacing any existing value.
get_range(vrange &r, tree name) const
Retrieves the stored range for the SSA name and writes it into r. Returns false if no value is cached.
has_range(tree name) const
Returns true if a range is currently stored for the given SSA name.
merge_range(tree name, const vrange &r)
Performs a union of the new range with the existing range in the cache. Returns true *only* if this causes the cached value to change.
clear_range(tree name)
- Removes the cached value for the given SSA name.
clear()
Clears all entries in the cache. In ssa_cache, this is a full vector traversal and may be expensive. In contrast, ssa_lazy_cache implements a more efficient version that clears only active entries via its bitmap.
All stored ranges are internally managed using a vrange_allocator, ensuring efficient memory reuse across queries.
Lazy Variant
The ssa_lazy_cache class inherits from ssa_cache and introduces sparse storage via an additional bitmap. It avoids zeroing the vector memory across all SSA names, and instead tracks active ssa-names via the bitmap.
This is particularly useful in situations when
- Only a small fraction of SSA names will participate in analysis, it uses a bitmap to track which SSA names have active entries.
- There is a need to frequently clear the cache. clear () is significantly more efficient.
- Low startup overhead is desired. The normal ssa_cache clears the entire vector at constructor time,
The API is the same, but it provides an extra entry point:
empty_p ()
Return true if the cache is currently empty.
Integration with `range_query`
Both ssa_cache and ssa_lazy_cache inherit from the range_query interface. This allows them to be passed to any part of the compiler infrastructure that expects a range_query—for example, routines that perform range-based folding or simplification.
By implementing range_of_expr(), the cache can be queried as a first-class source of value ranges for SSA names, making it easy to plug into expression evaluators or propagation engines without needing custom glue code.
Summary
Feature |
ssa_cache |
ssa_lazy_cache |
Storage |
Dense (vector) |
Sparse (bitmap + vector) |
Setup cost |
Higher upfront |
Lower for sparse use |
clear() cost |
High (full scan) |
Low (bitmap-driven) |
Best for |
Most SSA names used |
Few SSA names used |
Memory usage |
Higher |
Lower in sparse cases |
Both classes provide consistent, fast range lookup and modification, and integrate smoothly with the broader Ranger infrastructure via the range_query interface.