/// <summary>
        /// Replace reads from variables which are never assigned with a constant `0` (the default value for unassigned vars)
        /// </summary>
        /// <param name="cfg"></param>
        /// <returns></returns>
        public static IControlFlowGraph ReplaceUnassignedReads(this IControlFlowGraph cfg)
        {
            var v = new FindAssignedVariables();

            foreach (var vertex in cfg.Vertices)
            {
                v.Visit(vertex);
            }

            return(cfg.VisitBlocks(() => new ReplaceUnassignedReads(new HashSet <VariableName>(v.Names))));
        }
Example #2
0
        [NotNull] public static IReadOnlyDictionary <VariableName, BaseExpression> FindConstants([NotNull] this IControlFlowGraph cfg, ISingleStaticAssignmentTable ssa)
        {
            var constants = new Dictionary <VariableName, BaseExpression>();
            var count     = -1;

            // Keep finding more constants until no more are found
            while (count != constants.Count)
            {
                count = constants.Count;
                cfg.VisitBlocks(() => new FindConstantVariables(constants, ssa));
            }

            return(constants);
        }
Example #3
0
        /// <summary>
        /// Find variables which are guaranteed to be `0` or `1`
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="ssa"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        [NotNull] public static ISet <VariableName> FindBooleanVariables([NotNull] this IControlFlowGraph cfg, ISingleStaticAssignmentTable ssa, ITypeAssignments types)
        {
            var booleans = new HashSet <VariableName>();

            // Keep finding more constants until no more are found
            var count = -1;

            while (count != booleans.Count)
            {
                count = booleans.Count;
                cfg.VisitBlocks(() => new FindBooleanVariables(booleans, ssa, types));
            }

            var result = new HashSet <VariableName>(booleans.Where(n => !n.IsExternal));

            return(result);
        }
Example #4
0
        /// <summary>
        /// Replace expressions which result in a constant with the result
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="ssa"></param>
        /// <returns></returns>
        [NotNull] public static IControlFlowGraph FoldConstants([NotNull] this IControlFlowGraph cfg, ISingleStaticAssignmentTable ssa)
        {
            // Keep finding and replacing constants until nothing is found
            cfg = cfg.Fixpoint(c => {
                // Find variables which are assigned a value which is not tainted by external reads
                var constants = c.FindConstants(ssa);

                // Replace reads of a constant variable with the expression assigned to that variable
                c = c.VisitBlocks(() => new ReplaceConstantSubexpressions(constants));

                return(c);
            });

            // Replace constant subexpressions with their value
            cfg = cfg.VisitBlocks(() => new ConstantFoldingVisitor(true));

            return(cfg);
        }
 /// <summary>
 /// Replace inc/dec operations on numbers with a+=1 and a-=1
 /// </summary>
 /// <param name="cfg"></param>
 /// <param name="types"></param>
 /// <returns></returns>
 public static IControlFlowGraph SimplifyModificationExpressions(this IControlFlowGraph cfg, ITypeAssignments types)
 {
     return(cfg.VisitBlocks(() => new SimplifyModify(types)));
 }
 public static IControlFlowGraph RecomposeModify(this IControlFlowGraph cfg)
 {
     return(cfg.VisitBlocks(() => new RecomposeModifyIR()));
 }