/// <summary>
        /// Can we combine? Yes, if and only if the same guy and same variables!
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="optimize"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
        {
            if (statement.GetType() != typeof(StatementRecordPairValues))
            {
                return(false);
            }
            var other = statement as StatementRecordPairValues;

            if (other._index.RawValue != _index.RawValue)
            {
                return(false);
            }

            var isTheSame = _savers.Zip(other._savers, (f, s) => f.indexValue.RawValue == s.indexValue.RawValue && f.mapRecord.Type == s.mapRecord.Type).All(b => b);

            if (!isTheSame)
            {
                return(false);
            }

            // Now we can do them all.
            foreach (var saver in _savers.Zip(other._savers, (f, s) => Tuple.Create(f, s)))
            {
                optimize.TryRenameVarialbeOneLevelUp(saver.Item2.mapRecord.RawValue, saver.Item1.mapRecord);
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Attempt to combine two record statements. We are a bit different, we have
        /// an object we depend on - which is record... and that has to be the same. The
        /// other one we need to propagate.
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentException("statement");
            }

            if (opt == null)
            {
                throw new ArgumentNullException("opt");
            }

            var asRecord = statement as StatementRecordIndicies;

            if (asRecord == null)
            {
                return(false);
            }

            if (_intToRecord.RawValue != asRecord._intToRecord.RawValue)
            {
                return(false);
            }

            //
            // Since int to record is the same, we do a rename and move on!
            //

            return(opt.TryRenameVarialbeOneLevelUp(asRecord._storageArray.RawValue, _storageArray));
        }
Example #3
0
        /// <summary>
        /// Try to combine two of these guys
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public override bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentNullException();
            }
            if (this == statement)
            {
                throw new ArgumentException("Can't comebine with self!");
            }

            var otherLoop = statement as StatementLoopOverGood;

            if (otherLoop == null)
            {
                return(false);
            }

            // Are they the same? _index is independent and we can alter it.
            if (otherLoop._indexIsGood.RawValue == _indexIsGood.RawValue &&
                otherLoop._indiciesToCheck.RawValue == _indiciesToCheck.RawValue)
            {
                if (!(opt.TryRenameVarialbeOneLevelUp(otherLoop._index.RawValue, _index)))
                {
                    return(false);
                }

                Combine(otherLoop, opt);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// Combine these statements.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="optimize"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            var other = statement as StatementRecordValue;

            if (other == null)
            {
                return(false);
            }

            if (other._recordOnlyFirstValue != _recordOnlyFirstValue)
            {
                return(false);
            }

            if (other._savers.Count != _savers.Count)
            {
                return(false);
            }

            foreach (var o in other._savers)
            {
                var ms = _savers.Any(s => s.Item2.RawValue == o.Item2.RawValue);
                if (!ms)
                {
                    return(false);
                }
            }

            if (optimize == null)
            {
                throw new ArgumentNullException("optimize");
            }

            foreach (var o in other._savers)
            {
                var ms = _savers.Where(s => s.Item2.RawValue == o.Item2.RawValue).First();
                optimize.TryRenameVarialbeOneLevelUp(o.Item1.RawValue, ms.Item1);
            }
            optimize.TryRenameVarialbeOneLevelUp(other._valueWasSeen.RawValue, _valueWasSeen);

            return(true);
        }
Example #5
0
            public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
            {
                var other = statement as CombineTestStatement;

                if (other == null)
                {
                    return(false);
                }
                return(optimize.TryRenameVarialbeOneLevelUp(other.vdecl2.ParameterName, vdecl2));
            }
Example #6
0
        /// <summary>
        /// Try to combine two of these statements. The key is what we are trying to minimize or maximize.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            var other = statement as StatementMinMaxTest;

            if (other == null)
            {
                return(false);
            }

            if (CompareOperator != other.CompareOperator ||
                exprToMinOrMaximize.RawValue != other.exprToMinOrMaximize.RawValue)
            {
                return(false);
            }

            //
            // Everything else is dependent - so we can just rename it.
            //

            var cando = opt.TryRenameVarialbeOneLevelUp(other.MaxMinVariable.ParameterName, MaxMinVariable);

            if (!cando)
            {
                return(false);
            }
            cando = opt.TryRenameVarialbeOneLevelUp(other.vIsFilled.ParameterName, vIsFilled);
            if (!cando)
            {
                throw new InvalidOperationException("Unable to rename second variable in a chain for Min/Max operator!");
            }

            return(true);
        }
        /// <summary>
        /// Attempt to combine this statement. This is a little tricky. As it could be
        /// that the value we are accumulating is all that is different. In that case,
        /// we might be able to do the combination.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            var otherAssign = statement as StatementAggregate;

            if (otherAssign == null)
            {
                return(false);
            }

            if (opt == null)
            {
                throw new ArgumentNullException("opt");
            }

            //
            // Simple case: everything is the same
            //

            if (ResultVariable.ParameterName == otherAssign.ResultVariable.ParameterName &&
                Expression.RawValue == otherAssign.Expression.RawValue)
            {
                return(true);
            }

            //
            // Next, see if we rename the accumulator everything would be identical
            //

            string tempRaw = Expression.RawValue.ReplaceVariableNames(ResultVariable.ParameterName, otherAssign.ResultVariable.ParameterName);

            if (tempRaw == otherAssign.Expression.RawValue)
            {
                // In order for this to work, we have to attempt to rename the variable that the other
                // guy owns. Since this variable is declared outside here, we have to call up in order
                // to have it run. Note that in this call it will call down into here to do the rename!

                return(opt.TryRenameVarialbeOneLevelUp(otherAssign.ResultVariable.ParameterName, ResultVariable));
            }

            //
            // There is nothing else we can do to figure out if this is the same, I"m afraid!
            //

            return(false);
        }
        /// <summary>
        /// We can combine if our predicates look the same.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="optimize"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            var other = statement as StatementAnyAllDetector;

            if (other == null)
            {
                return(false);
            }

            if (Predicate == null)
            {
                if (other.Predicate != null)
                {
                    return(false);
                }
            }
            else
            {
                if (other.Predicate == null)
                {
                    return(false);
                }

                if (other.Predicate.RawValue != Predicate.RawValue ||
                    other.ResultValueToBe != ResultValueToBe)
                {
                    return(false);
                }
            }

            //
            // As long as nothing crazy is going on with result, then we
            // can definitely combine these two!
            //

            if (optimize == null)
            {
                throw new ArgumentNullException("optimize");
            }

            return(optimize.TryRenameVarialbeOneLevelUp(other.Result.RawValue, Result));
        }
Example #9
0
        /// <summary>
        /// Attempt to combine two of these statements. We can do this iff the source expression that
        /// we are testing is the same. Then the two indicies need to be renamed (it is assumed that we have total
        /// control - as you can see from the generated code above).
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public override bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            var other = statement as StatementCheckLoopPairwise;

            if (other == null)
            {
                return(false);
            }

            if (_indciesToInspect.ParameterName != other._indciesToInspect.ParameterName)
            {
                return(false);
            }

            //
            // Rename the various guys. Note that index1 and index2 are declared by us. So it is only our sub-blocks that have to deal with
            // that. So we do a "local" renaming.
            //

            var rename = opt.TryRenameVarialbeOneLevelUp(other._whatIsGood.RawValue, _whatIsGood);

            if (!rename)
            {
                return(false);
            }
            other.RenameVariable(other._index1.RawValue, _index1.RawValue);
            other.RenameVariable(other._index2.RawValue, _index2.RawValue);

            //
            // Combine the sub-blocks now
            //

            Combine(other as StatementInlineBlockBase, opt);

            return(true);
        }
        /// <summary>
        /// We don't have the code to do the combination yet, so we have to bail!
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public override bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            if (opt == null)
            {
                throw new ArgumentNullException("opt");
            }

            var other = statement as StatementIfOnCount;

            if (other == null)
            {
                return(false);
            }

            var issame = Comparison == other.Comparison &&
                         Limit.RawValue == other.Limit.RawValue;

            if (!issame)
            {
                return(false);
            }

            //
            // Now we have to rename the right, just in case...
            //

            if (!opt.TryRenameVarialbeOneLevelUp(other.Counter.RawValue, Counter))
            {
                return(false);
            }

            Combine(other, opt);

            return(true);
        }
Example #11
0
        /// <summary>
        /// Try to combine two assign statements. Since this will be for totally
        /// trivial cases, this should be "easy" - only when they are the same.
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            if (opt == null)
            {
                throw new ArgumentNullException("opt");
            }

            var otherAssign = statement as StatementAssign;

            if (otherAssign == null)
            {
                return(false);
            }

            if (Expression.RawValue != otherAssign.Expression.RawValue)
            {
                return(false);
            }

            // If the statements are identical, then we can combine by default without having to do any
            // further work.

            if (otherAssign.ResultVariable.RawValue == ResultVariable.RawValue)
            {
                return(true);
            }

            // If we have declared, then we are sole owner - so we can force the change. Otherwise, we
            // need to let the infrastructure figure out where the declared is and change it from there.

            return(opt.TryRenameVarialbeOneLevelUp(otherAssign.ResultVariable.RawValue, ResultVariable));
        }
        /// <summary>
        /// Attempt to combine two of these statements. We can do this iff the source expression that
        /// we are testing is the same. Then the two indicies need to be renamed (it is assumed that we have total
        /// control - as you can see from the generated code above).
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public override bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            var other = statement as StatementCheckLoopPairwise;
            if (other == null)
                return false;

            if (_indciesToInspect.ParameterName != other._indciesToInspect.ParameterName)
                return false;

            //
            // Rename the various guys. Note that index1 and index2 are declared by us. So it is only our sub-blocks that have to deal with
            // that. So we do a "local" renaming.
            //

            var rename = opt.TryRenameVarialbeOneLevelUp(other._whatIsGood.RawValue, _whatIsGood);
            if (!rename)
                return false;
            other.RenameVariable(other._index1.RawValue, _index1.RawValue);
            other.RenameVariable(other._index2.RawValue, _index2.RawValue);

            //
            // Combine the sub-blocks now
            //

            Combine(other as StatementInlineBlockBase, opt);

            return true;
        }
 public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
 {
     var other = statement as CombineTestStatement;
     if (other == null)
         return false;
     return optimize.TryRenameVarialbeOneLevelUp(other.vdecl2.ParameterName, vdecl2);
 }
        /// <summary>
        /// We can combine if our predicates look the same.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="optimize"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            var other = statement as StatementAnyAllDetector;
            if (other == null)
                return false;

            if (Predicate == null)
            {
                if (other.Predicate != null)
                    return false;
            }
            else
            {
                if (other.Predicate == null)
                    return false;

                if (other.Predicate.RawValue != Predicate.RawValue
                    || other.ResultValueToBe != ResultValueToBe)
                    return false;
            }

            //
            // As long as nothing crazy is going on with result, then we
            // can definitely combine these two!
            //

            if (optimize == null)
                throw new ArgumentNullException("optimize");

            return optimize.TryRenameVarialbeOneLevelUp(other.Result.RawValue, Result);
        }
        /// <summary>
                 /// Try to combine two of these guys
                 /// </summary>
                 /// <param name="statement"></param>
                 /// <returns></returns>
        public override bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentNullException();
            if (this == statement)
                throw new ArgumentException("Can't comebine with self!");

            var otherLoop = statement as StatementLoopOverGood;
            if (otherLoop == null)
                return false;

            // Are they the same? _index is independent and we can alter it.
            if (otherLoop._indexIsGood.RawValue == _indexIsGood.RawValue
                && otherLoop._indiciesToCheck.RawValue == _indiciesToCheck.RawValue)
            {
                if (!(opt.TryRenameVarialbeOneLevelUp(otherLoop._index.RawValue, _index)))
                    return false;

                Combine(otherLoop, opt);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Try to combine two of these statements. The key is what we are trying to minimize or maximize.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            var other = statement as StatementMinMaxTest;
            if (other == null)
                return false;

            if (CompareOperator != other.CompareOperator
                || exprToMinOrMaximize.RawValue != other.exprToMinOrMaximize.RawValue)
                return false;

            //
            // Everything else is dependent - so we can just rename it.
            //

            var cando = opt.TryRenameVarialbeOneLevelUp(other.MaxMinVariable.ParameterName, MaxMinVariable);
            if (!cando)
                return false;
            cando = opt.TryRenameVarialbeOneLevelUp(other.vIsFilled.ParameterName, vIsFilled);
            if (!cando)
                throw new InvalidOperationException("Unable to rename second variable in a chain for Min/Max operator!");

            return true;
        }
        /// <summary>
        /// Attempt to combine this statement. This is a little tricky. As it could be
        /// that the value we are accumulating is all that is different. In that case,
        /// we might be able to do the combination.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            var otherAssign = statement as StatementAggregate;
            if (otherAssign == null)
                return false;

            if (opt == null)
                throw new ArgumentNullException("opt");

            //
            // Simple case: everything is the same
            //

            if (ResultVariable.ParameterName == otherAssign.ResultVariable.ParameterName
                && Expression.RawValue == otherAssign.Expression.RawValue)
                return true;

            //
            // Next, see if we rename the accumulator everything would be identical
            //

            string tempRaw = Expression.RawValue.ReplaceVariableNames(ResultVariable.ParameterName, otherAssign.ResultVariable.ParameterName);
            if (tempRaw == otherAssign.Expression.RawValue)
            {
                // In order for this to work, we have to attempt to rename the variable that the other
                // guy owns. Since this variable is declared outside here, we have to call up in order
                // to have it run. Note that in this call it will call down into here to do the rename!

                return opt.TryRenameVarialbeOneLevelUp(otherAssign.ResultVariable.ParameterName, ResultVariable);
            }

            //
            // There is nothing else we can do to figure out if this is the same, I"m afraid!
            //

            return false;
        }
Example #18
0
        /// <summary>
        /// Try to combine two assign statements. Since this will be for totally
        /// trivial cases, this should be "easy" - only when they are the same.
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            if (opt == null)
                throw new ArgumentNullException("opt");

            var otherAssign = statement as StatementAssign;
            if (otherAssign == null)
                return false;

            if (Expression.RawValue != otherAssign.Expression.RawValue)
                return false;

            // If the statements are identical, then we can combine by default without having to do any
            // further work.

            if (otherAssign.ResultVariable.RawValue == ResultVariable.RawValue)
                return true;

            // If we have declared, then we are sole owner - so we can force the change. Otherwise, we
            // need to let the infrastructure figure out where the declared is and change it from there.

            return opt.TryRenameVarialbeOneLevelUp(otherAssign.ResultVariable.RawValue, ResultVariable);
        }
        /// <summary>
        /// Combine these statements.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="optimize"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            var other = statement as StatementRecordValue;
            if (other == null)
                return false;

            if (other._recordOnlyFirstValue != _recordOnlyFirstValue)
                return false;

            if (other._savers.Count != _savers.Count)
                return false;

            foreach (var o in other._savers)
            {
                var ms = _savers.Any(s => s.Item2.RawValue == o.Item2.RawValue);
                if (!ms)
                    return false;
            }

            if (optimize == null)
                throw new ArgumentNullException("optimize");

            foreach (var o in other._savers)
            {
                var ms = _savers.Where(s => s.Item2.RawValue == o.Item2.RawValue).First();
                optimize.TryRenameVarialbeOneLevelUp(o.Item1.RawValue, ms.Item1);
            }
            optimize.TryRenameVarialbeOneLevelUp(other._valueWasSeen.RawValue, _valueWasSeen);

            return true;
        }
        /// <summary>
        /// Attempt to combine two record statements. We are a bit different, we have 
        /// an object we depend on - which is record... and that has to be the same. The
        /// other one we need to propagate.
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentException("statement");

            if (opt == null)
                throw new ArgumentNullException("opt");

            var asRecord = statement as StatementRecordIndicies;
            if (asRecord == null)
                return false;

            if (_intToRecord.RawValue != asRecord._intToRecord.RawValue)
                return false;

            //
            // Since int to record is the same, we do a rename and move on!
            //

            return opt.TryRenameVarialbeOneLevelUp(asRecord._storageArray.RawValue, _storageArray);
        }
        /// <summary>
        /// We don't have the code to do the combination yet, so we have to bail!
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        public override bool TryCombineStatement(IStatement statement, ICodeOptimizationService opt)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            if (opt == null)
                throw new ArgumentNullException("opt");

            var other = statement as StatementIfOnCount;
            if (other == null)
                return false;

            var issame = Comparison == other.Comparison
                && Limit.RawValue == other.Limit.RawValue;

            if (!issame)
                return false;

            //
            // Now we have to rename the right, just in case...
            //

            if (!opt.TryRenameVarialbeOneLevelUp(other.Counter.RawValue, Counter))
                return false;

            Combine(other, opt);

            return true;
        }
        /// <summary>
        /// Can we combine? Yes, if and only if the same guy and same variables!
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="optimize"></param>
        /// <returns></returns>
        public bool TryCombineStatement(IStatement statement, ICodeOptimizationService optimize)
        {
            if (statement.GetType() != typeof(StatementRecordPairValues))
                return false;
            var other = statement as StatementRecordPairValues;
            if (other._index.RawValue != _index.RawValue)
                return false;

            var isTheSame = _savers.Zip(other._savers, (f, s) => f.indexValue.RawValue == s.indexValue.RawValue && f.mapRecord.Type == s.mapRecord.Type).All(b => b);
            if (!isTheSame)
            {
                return false;
            }

            // Now we can do them all.
            foreach (var saver in _savers.Zip(other._savers, (f, s) => Tuple.Create(f, s)))
            {
                optimize.TryRenameVarialbeOneLevelUp(saver.Item2.mapRecord.RawValue, saver.Item1.mapRecord);
            }

            return true;
        }