Package org.jruby.ir.operands
Class ImmutableLiteral<T>
java.lang.Object
org.jruby.ir.operands.Operand
org.jruby.ir.operands.ImmutableLiteral<T>
- Direct Known Subclasses:
Bignum,Boolean,Complex,Fixnum,Float,FrozenString,Nil,NullBlock,Rational,Regexp,Symbol,SymbolProc,UnboxedBoolean,UnboxedFixnum,UnboxedFloat
Operands extending this type can make a reasonable assumption of
immutability. In Ruby, almost nothing is truly immutable (set_instance_var)
but for the sake of our compiler, we can assume the basic behavior will
continue to work unchanged (the value of an instance of fixnum 3 will remain 3).
Knowing that we have a literal which will not change can be used for
optimizations like constant propagation.
ENEBO: This cachedObject thing obviously cannot be used from multiple
threads without some safety being added. This probably also is not the
fastest code, but it is very simple. It would be really nice to make this
side-effect free as well, but this is difficult without adding a level of
indirection or pre-caching each value we encounter during construction.
-
Field Summary
FieldsFields inherited from class org.jruby.ir.operands.Operand
EMPTY_ARRAY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAppend the list of variables used in this operand to the input list -- force every operand to implement this because a missing implementation can cause bad failures.cachedObject(ThreadContext context) Returns the cached object.booleanCan we replace every use of a variable 'v' that contains the value of this operand with the operand itself? This takes importance when there are at least two uses of 'v' within this scope.abstract TcreateCacheObject(ThreadContext context) Implementing class is responsible for constructing the cached value.booleanDo we know the value of this operand at compile-time? If we do then it may be possible to constant propagate (one case: We also know it is also an ImmutableLiteral).booleanisCached()Has this object already been cached?retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) retrieve the live value represented by this immutable literal.Methods inherited from class org.jruby.ir.operands.Operand
encode, getOperandType, getSimplifiedOperand, getValue, isFalseyImmediate, isTruthyImmediate, visit
-
Field Details
-
cachedObject
-
-
Constructor Details
-
ImmutableLiteral
public ImmutableLiteral()
-
-
Method Details
-
hasKnownValue
public boolean hasKnownValue()Description copied from class:OperandDo we know the value of this operand at compile-time? If we do then it may be possible to constant propagate (one case: We also know it is also an ImmutableLiteral).- Overrides:
hasKnownValuein classOperand- Returns:
- true if a known compile-time value.
-
canCopyPropagate
public boolean canCopyPropagate()Description copied from class:OperandCan we replace every use of a variable 'v' that contains the value of this operand with the operand itself? This takes importance when there are at least two uses of 'v' within this scope. Ex: v = [1,2,3]; x = v; y = v In this case, we cannot replace the occurrences of 'v' because we would then get x = [1,2,3]; y = [1,2,3] which would then result in two different array objects being constructed instead of a single one.- Overrides:
canCopyPropagatein classOperand- Returns:
- true if it is safe to copy-propagate the operand.
-
addUsedVariables
Description copied from class:OperandAppend the list of variables used in this operand to the input list -- force every operand to implement this because a missing implementation can cause bad failures.- Specified by:
addUsedVariablesin classOperand
-
cloneForInlining
- Specified by:
cloneForInliningin classOperand
-
createCacheObject
Implementing class is responsible for constructing the cached value. -
cachedObject
Returns the cached object. If not then it asks class to create an object to cache. -
isCached
public boolean isCached()Has this object already been cached? -
retrieve
public T retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) retrieve the live value represented by this immutable literal. An interesting property of knowing something cannot change at compile time is that all information necessary to construct it is also known at compile time. We don't pre-create these since we don't want to assume the cost of constructing literals which may never be used.
-