Exemple #1
0
        public static bool FindSimpleWhile()
        {
            List <int> order = graph.getOrder(true);

            for (int i = 0; i < order.Count(); i++)
            {
                int        idx     = order[i];
                List <int> childs  = graph.getChildren(idx);
                List <int> parents = graph.getParents(idx);
                if (childs.Count == 2 && childs.Contains(idx))
                {
                    for (int j = 0; j < graph.edges.Count; j++)
                    {
                        if (graph.edges[j].start == idx &&
                            graph.edges[j].end == idx)
                        {
                            graph.edges.RemoveAt(j);
                            break;
                        }
                    }
                    ABSBlock abs = new ABSBlock(ABSBlock.absType.SIMPLEWHILE);
                    abs.data = graph.nodes[idx].data;
                    graph.nodes[idx].data = abs;
                    LogReduction("Simple While Reduction: " + idx);
                    return(true);
                }
            }
            return(false);
        }
Exemple #2
0
        public static string PrintAcSESEBlock(ABSBlock block, int tabs)
        {
            StringBuilder sb = new StringBuilder();
            string        t  = "";

            for (int i = 0; i < tabs; i++)
            {
                t += "\t";
            }
            ACSESERegion region = ((ACSESERegion)block.data);
            Graph        gr     = region.graph;
            List <int>   order  = gr.getTopogicalOrder();

            for (int i = 0; i < order.Count - 1; i++)
            {
                int idx = order[i];
                if (region.codeNodes.Contains(idx))
                {
                    List <List <int> > paths = gr.getAllReachingPaths(idx);
                    StringBuilder      exp   = new StringBuilder();
                    exp.Append(t + "if(");
                    if (paths.Count == 1)
                    {
                        exp.Append(ConditionPathToExpression(paths[0], region));
                        exp.AppendLine(")");
                    }
                    else
                    {
                        StringBuilder sb2 = new StringBuilder();
                        foreach (List <int> path in paths)
                        {
                            if (sb2.Length != 0)
                            {
                                sb2.AppendLine(" || ");
                                sb2.Append(t + "   ");
                            }
                            sb2.Append("(" + ConditionPathToExpression(path, region) + ")");
                        }
                        exp.AppendLine(sb2.ToString());
                        exp.AppendLine(t + "  )");
                    }
                    exp.AppendLine(t + "{");
                    exp.Append(PrintBlock(gr.nodes[idx].data, tabs + 1));
                    exp.AppendLine(t + "}");
                    sb.Append(exp.ToString());
                }
            }
            sb.Append(PrintBlock(region.graph.nodes[order[order.Count - 1]].data, tabs));
            return(sb.ToString());
        }
Exemple #3
0
        public static bool FindIfThen()
        {
            List <int> order = graph.getOrder(true);

            for (int i = 0; i < order.Count(); i++)
            {
                int idxHead = order[i];
                int idxThen, idxAfter;
                idxThen = idxAfter = -1;
                List <int> childs = graph.getChildren(idxHead);
                if (childs.Count == 2)
                {
                    List <int> parents0 = graph.getParents(childs[0]);
                    List <int> parents1 = graph.getParents(childs[1]);
                    if (parents0.Count == 1)
                    {
                        List <int> tmp = graph.getChildren(childs[0]);
                        if (tmp.Count == 1 && tmp[0] == childs[1])
                        {
                            idxThen  = childs[0];
                            idxAfter = childs[1];
                        }
                    }
                    else if (parents1.Count == 1)
                    {
                        List <int> tmp = graph.getChildren(childs[1]);
                        if (tmp.Count == 1 && tmp[0] == childs[0])
                        {
                            idxThen  = childs[1];
                            idxAfter = childs[0];
                        }
                    }
                    if (idxAfter != -1 && idxThen != -1 && idxAfter != idxHead)
                    {
                        ABSBlock      abs = new ABSBlock(ABSBlock.absType.IFTHEN);
                        List <object> tmp = new List <object>();
                        tmp.Add(graph.nodes[idxHead].data);
                        tmp.Add(graph.nodes[idxThen].data);
                        abs.data = tmp;
                        graph.nodes[idxHead].data = abs;
                        graph.removeNode(idxThen);
                        LogReduction("If-Then Reduction: " + idxHead + "<-" + idxThen);
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #4
0
        public static bool FindACSESERegions()
        {
            List <List <int> > regions = graph.getAllSESERegions();

            for (int i = 0; i < regions.Count; i++)
            {
                bool containsOther = false;
                for (int j = 0; j < regions.Count; j++)
                {
                    if (i != j && Helper.ListContained(regions[j], regions[i]))
                    {
                        containsOther = true;
                        break;
                    }
                }
                if (containsOther)
                {
                    continue;
                }
                Graph gr = graph.getCutOut(regions[i]);
                if (gr.isCyclic())
                {
                    continue;
                }
                gr.nodes[0] = new Node(gr.nodes[0].data, gr.nodes[0].srcId);
                ABSBlock abs = new ABSBlock(ABSBlock.absType.ACSESEREGION);
                abs.data = new ACSESERegion(gr);
                graph.nodes[regions[i][0]].data = abs;
                int first = regions[i][0];
                int last  = regions[i][regions[i].Count - 1];
                if (graph.getChildren(last).Count > 0)
                {
                    graph.edges.Add(new Edge(regions[i][0], graph.getChildren(last)[0], "branch"));
                }
                regions[i].RemoveAt(0);
                regions[i].Sort();
                regions[i].Reverse();
                foreach (int j in regions[i])
                {
                    graph.removeNode(j);
                }
                LogReduction("ACSESE Region Reduction: " + first + "<-" + last);
                return(true);
            }
            return(false);
        }
Exemple #5
0
        public static bool FindSequences()
        {
            List <int> order = graph.getOrder(true);

            for (int i = 0; i < order.Count(); i++)
            {
                int        idx    = order[i];
                List <int> childs = graph.getChildren(idx);
                if (childs.Count == 1 && childs[0] != idx)
                {
                    int        idx2    = childs[0];
                    List <int> parents = graph.getParents(idx2);
                    List <int> childs2 = graph.getChildren(idx2);
                    if (parents.Count == 1 && childs2.Count < 2)
                    {
                        if (childs2.Count == 1 && (childs2[0] == idx || childs2[0] == idx2))
                        {
                            continue;
                        }
                        ABSBlock      abs = new ABSBlock(ABSBlock.absType.SEQUENCE);
                        List <object> tmp = new List <object>();
                        tmp.Add(graph.nodes[idx].data);
                        tmp.Add(graph.nodes[idx2].data);
                        abs.data = tmp;
                        if (childs2.Count == 1)
                        {
                            foreach (Edge e in graph.edges)
                            {
                                if (e.start == idx2)
                                {
                                    e.start = idx;
                                }
                            }
                        }
                        graph.nodes[idx].data = abs;
                        graph.removeNode(idx2);
                        LogReduction("Sequence Reduction: " + idx + "<-" + idx2);
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #6
0
        public static bool FindSimpleLoops()
        {
            List <List <int> > scc    = graph.getAllStronglyConnectedComponents();
            List <Loop>        gLoops = new List <Loop>();

            foreach (List <int> component in scc)
            {
                if (component.Count > 1)
                {
                    List <List <int> > loops = graph.getAllSimpleLoops(component);
                    foreach (List <int> loop in loops)
                    {
                        gLoops.Add(graph.getLoopInformation(loop));
                    }
                }
            }
            List <int> order = graph.getOrder(true);

            for (int i = 0; i < order.Count(); i++)
            {
                int idxHead = order[i];
                foreach (Loop loop in gLoops)
                {
                    if (loop.header.index == idxHead && !loop.isMultiExit && loop.body.Count == 1 && !loop.body[0].hasOutsideEntry)
                    {
                        int      idxTail = loop.body[0].index;
                        ABSBlock abs;
                        if (loop.header.isBreak)
                        {
                            List <int> childs = graph.getChildren(idxTail);
                            if (childs.Count != 1)
                            {
                                ABSBlock abs2 = new ABSBlock(ABSBlock.absType.BREAK);
                                foreach (int child in childs)
                                {
                                    if (child != idxHead)
                                    {
                                        graph.removeEdge(idxTail, child);
                                    }
                                }
                                abs2.data = graph.nodes[idxTail].data;
                                graph.nodes[idxTail].data = abs2;
                                LogReduction("Break Reduction: " + idxTail);
                            }
                            abs = new ABSBlock(ABSBlock.absType.WHILE);
                            LogReduction("While Reduction: " + idxHead + "<-" + idxTail);
                        }
                        else
                        {
                            List <int> childs = graph.getChildren(idxTail);
                            if (childs.Count != 2)
                            {
                                continue;
                            }
                            foreach (int child in childs)
                            {
                                if (child != idxHead)
                                {
                                    graph.edges.Add(new Edge(idxHead, child, "branch"));
                                }
                            }
                            abs = new ABSBlock(ABSBlock.absType.DOWHILE);
                            LogReduction("Do-While Reduction: " + idxHead + "<-" + idxTail);
                        }
                        List <object> tmp = new List <object>();
                        tmp.Add(graph.nodes[idxHead].data);
                        tmp.Add(graph.nodes[idxTail].data);
                        abs.data = tmp;
                        graph.nodes[idxHead].data = abs;
                        graph.removeNode(idxTail);
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #7
0
        public static bool FindLoopParts()
        {
            List <List <int> > scc    = graph.getAllStronglyConnectedComponents();
            List <Loop>        gLoops = new List <Loop>();

            foreach (List <int> component in scc)
            {
                if (component.Count > 1)
                {
                    List <List <int> > loops = graph.getAllSimpleLoops(component);
                    foreach (List <int> loop in loops)
                    {
                        gLoops.Add(graph.getLoopInformation(loop));
                    }
                }
            }
            List <int> order = graph.getOrder(true);

            for (int i = 0; i < order.Count(); i++)
            {
                int idx = order[i];
                foreach (Loop loop in gLoops)
                {
                    if (!loop.isMultiExit)
                    {
                        foreach (LoopPart part in loop.body)
                        {
                            if (part.index == idx)
                            {
                                if (!part.hasOutsideEntry && !part.isTail && !part.isHeader && part.isContinue && !part.isBreak)
                                {
                                    List <int> childs = graph.getChildren(idx);
                                    if (childs.Count != 2)
                                    {
                                        break;
                                    }
                                    for (int j = 0; j < graph.edges.Count; j++)
                                    {
                                        if (graph.edges[j].start == idx &&
                                            graph.edges[j].end == loop.header.index)
                                        {
                                            graph.edges.RemoveAt(j);
                                            break;
                                        }
                                    }
                                    ABSBlock abs = new ABSBlock(ABSBlock.absType.CONTINUE);
                                    abs.data = graph.nodes[idx].data;
                                    graph.nodes[idx].data = abs;
                                    LogReduction("Continue Reduction: " + idx);
                                    return(true);
                                }
                                if (!part.hasOutsideEntry && !part.isTail && !part.isHeader && !part.isContinue && part.isBreak)
                                {
                                    List <int> childs = graph.getChildren(idx);
                                    if (childs.Count != 2)
                                    {
                                        break;
                                    }
                                    bool isFirst = false;
                                    foreach (LoopPart part2 in loop.body)
                                    {
                                        if (part2.index == childs[0])
                                        {
                                            isFirst = true;
                                            break;
                                        }
                                    }
                                    if (isFirst)
                                    {
                                        for (int j = 0; j < graph.edges.Count; j++)
                                        {
                                            if (graph.edges[j].start == idx &&
                                                graph.edges[j].end == childs[1])
                                            {
                                                graph.edges.RemoveAt(j);
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        for (int j = 0; j < graph.edges.Count; j++)
                                        {
                                            if (graph.edges[j].start == idx &&
                                                graph.edges[j].end == childs[0])
                                            {
                                                graph.edges.RemoveAt(j);
                                                break;
                                            }
                                        }
                                    }
                                    ABSBlock abs = new ABSBlock(ABSBlock.absType.BREAK);
                                    abs.data = graph.nodes[idx].data;
                                    graph.nodes[idx].data = abs;
                                    LogReduction("Break Reduction: " + idx);
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #8
0
        public static bool FindIfThenElse()
        {
            List <int> order = graph.getOrder(true);

            for (int i = 0; i < order.Count(); i++)
            {
                int idxHead = order[i];
                int idxThen, idxElse, idxAfter;
                idxThen = idxElse = idxAfter = -1;
                List <int> childs = graph.getChildren(idxHead);
                if (childs.Count == 2)
                {
                    List <int> parents0 = graph.getParents(childs[0]);
                    List <int> parents1 = graph.getParents(childs[1]);
                    List <int> childs0  = graph.getChildren(childs[0]);
                    List <int> childs1  = graph.getChildren(childs[1]);
                    if (parents0.Count == 1 && parents1.Count == 1 &&
                        childs0.Count == 1 && childs1.Count == 1 &&
                        childs0[0] == childs1[0])
                    {
                        foreach (Edge e in graph.edges)
                        {
                            if (e.start == idxHead && e.end == childs[0])
                            {
                                if (e.text == "true")
                                {
                                    idxThen  = childs[0];
                                    idxElse  = childs[1];
                                    idxAfter = childs0[0];
                                }
                                else if (e.text == "false")
                                {
                                    idxThen  = childs[1];
                                    idxElse  = childs[0];
                                    idxAfter = childs0[0];
                                }
                                break;
                            }
                        }
                        if (idxAfter != -1 && idxThen != -1 && idxElse != -1 && idxAfter != idxHead)
                        {
                            ABSBlock      abs = new ABSBlock(ABSBlock.absType.IFTHENELSE);
                            List <object> tmp = new List <object>();
                            tmp.Add(graph.nodes[idxHead].data);
                            tmp.Add(graph.nodes[idxThen].data);
                            tmp.Add(graph.nodes[idxElse].data);
                            abs.data = tmp;
                            graph.nodes[idxHead].data = abs;
                            graph.edges.Add(new Edge(idxHead, idxAfter, "branch"));
                            if (idxThen > idxElse)
                            {
                                graph.removeNode(idxThen);
                                graph.removeNode(idxElse);
                            }
                            else
                            {
                                graph.removeNode(idxElse);
                                graph.removeNode(idxThen);
                            }
                            LogReduction("If-Then-Else Reduction: " + idxHead + "<-" + idxThen + ", " + idxHead + "<-" + idxElse);
                            return(true);
                        }
                    }
                    if (parents0.Count == 1 && parents1.Count == 1 &&
                        childs0.Count == 0 && childs1.Count == 0)
                    {
                        foreach (Edge e in graph.edges)
                        {
                            if (e.start == idxHead && e.end == childs[0])
                            {
                                if (e.text == "true")
                                {
                                    idxThen = childs[0];
                                    idxElse = childs[1];
                                }
                                else if (e.text == "false")
                                {
                                    idxThen = childs[1];
                                    idxElse = childs[0];
                                }
                                break;
                            }
                        }
                        if (idxThen != -1 && idxElse != -1)
                        {
                            ABSBlock      abs = new ABSBlock(ABSBlock.absType.IFTHENELSE);
                            List <object> tmp = new List <object>();
                            tmp.Add(graph.nodes[idxHead].data);
                            tmp.Add(graph.nodes[idxThen].data);
                            tmp.Add(graph.nodes[idxElse].data);
                            abs.data = tmp;
                            graph.nodes[idxHead].data = abs;
                            if (idxThen > idxElse)
                            {
                                graph.removeNode(idxThen);
                                graph.removeNode(idxElse);
                            }
                            else
                            {
                                graph.removeNode(idxElse);
                                graph.removeNode(idxThen);
                            }
                            LogReduction("If-Then-Else Reduction: " + idxHead + "<-" + idxThen + ", " + idxHead + "<-" + idxElse);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemple #9
0
        public static string PrintAbsBlock(ABSBlock block, int tabs)
        {
            StringBuilder sb = new StringBuilder();
            List <object> list;
            string        t = "";

            for (int i = 0; i < tabs; i++)
            {
                t += "\t";
            }
            switch (block.type)
            {
            case ABSBlock.absType.SEQUENCE:
                list = (List <object>)block.data;
                sb.Append(PrintBlock(list[0], tabs));
                sb.Append(PrintBlock(list[1], tabs));
                break;

            case ABSBlock.absType.SIMPLEWHILE:
                sb.AppendLine(t + "while(" + PrintCodeBlock((CodeBlock)block.data) + ");");
                break;

            case ABSBlock.absType.BREAK:
                sb.AppendLine(t + "if(" + PrintCodeBlock((CodeBlock)block.data) + ") break;");
                break;

            case ABSBlock.absType.CONTINUE:
                sb.AppendLine(t + "if(" + PrintCodeBlock((CodeBlock)block.data) + ") continue;");
                break;

            case ABSBlock.absType.IFTHEN:
                list = (List <object>)block.data;
                sb.AppendLine(t + "if(!" + PrintCodeBlock((CodeBlock)list[0]) + ")");
                sb.AppendLine(t + "{");
                sb.Append(PrintBlock(list[1], tabs + 1));
                sb.AppendLine(t + "}");
                break;

            case ABSBlock.absType.IFTHENELSE:
                list = (List <object>)block.data;
                sb.AppendLine(t + "if(" + PrintCodeBlock((CodeBlock)list[0]) + ")");
                sb.AppendLine(t + "{");
                sb.Append(PrintBlock(list[1], tabs + 1));
                sb.AppendLine(t + "}");
                sb.AppendLine(t + "else");
                sb.AppendLine(t + "{");
                sb.Append(PrintBlock(list[2], tabs + 1));
                sb.AppendLine(t + "}");
                break;

            case ABSBlock.absType.WHILE:
                list = (List <object>)block.data;
                sb.AppendLine(t + "while(" + PrintCodeBlock((CodeBlock)list[0]) + ")");
                sb.AppendLine(t + "{");
                sb.Append(PrintBlock(list[1], tabs + 1));
                sb.AppendLine(t + "}");
                break;

            case ABSBlock.absType.DOWHILE:
                list = (List <object>)block.data;
                sb.AppendLine(t + "do");
                sb.AppendLine(t + "{");
                sb.Append(PrintBlock(list[0], tabs + 1));
                sb.AppendLine(t + "}");
                sb.AppendLine(t + "while(" + PrintCodeBlock((CodeBlock)list[1]) + ")");
                break;

            case ABSBlock.absType.ACSESEREGION:
                sb.Append(PrintAcSESEBlock(block, tabs));
                break;

            default:
                break;
            }
            return(sb.ToString());
        }