public bool Optimize(ThreeAddressCode tac)
        {
            var isOptimized       = false;
            var directAssignments = new Dictionary <string, string>();

            var blocks = new BasicBlocks();

            blocks.SplitTACode(tac);

            foreach (var block in blocks)
            {
                var currentNode = block.First;
                while (currentNode != null)
                {
                    if (currentNode.Value is TacAssignmentNode assignmentNode)
                    {
                        if (assignmentNode.Operation == null)
                        {
                            if (directAssignments.ContainsKey(assignmentNode.LeftPartIdentifier))
                            {
                                directAssignments.Remove(assignmentNode.LeftPartIdentifier);
                            }

                            if (!int.TryParse(assignmentNode.FirstOperand, out int firstOpValue))
                            {
                                var id      = assignmentNode.LeftPartIdentifier;
                                var firstOp = assignmentNode.FirstOperand;
                                directAssignments.Add(id, firstOp);
                            }
                        }

                        if (directAssignments.ContainsKey(assignmentNode.FirstOperand))
                        {
                            assignmentNode.FirstOperand = directAssignments[assignmentNode.FirstOperand];
                            isOptimized = true;
                        }

                        if (assignmentNode.SecondOperand != null && directAssignments.ContainsKey(assignmentNode.SecondOperand))
                        {
                            assignmentNode.SecondOperand = directAssignments[assignmentNode.SecondOperand];
                            isOptimized = true;
                        }

                        if (directAssignments.ContainsKey(assignmentNode.LeftPartIdentifier) &&
                            assignmentNode.SecondOperand != null && assignmentNode.FirstOperand != null)
                        {
                            directAssignments.Remove(assignmentNode.LeftPartIdentifier);
                        }
                    }

                    currentNode = currentNode.Next;
                }
            }

            return(isOptimized);
        }
Example #2
0
        public bool Optimize(ThreeAddressCode tac)
        {
            var blocks = new BasicBlocks();

            blocks.SplitTACode(tac);

            var nodesByExpression = new Dictionary <string, LinkedListNode <TacNode> >();
            var isOptimized       = false;

            foreach (var block in blocks)
            {
                nodesByExpression.Clear();
                var currentNode = block.First;
                while (currentNode != null)
                {
                    if (currentNode.Value is TacAssignmentNode assignment &&
                        assignment.Operation != null &&
                        !int.TryParse(assignment.FirstOperand, out _) &&
                        !int.TryParse(assignment.SecondOperand, out _))
                    {
                        var expression = RightPartToString(assignment);
                        if (!nodesByExpression.ContainsKey(expression))
                        {
                            nodesByExpression.Add(expression, currentNode);
                        }
                        else
                        {
                            var node           = nodesByExpression[expression];
                            var assignmentNode = node.Value as TacAssignmentNode;
                            if (IsPossibleToOptimize(node, currentNode,
                                                     assignmentNode.FirstOperand, assignmentNode.SecondOperand))
                            {
                                assignment.FirstOperand  = assignmentNode.LeftPartIdentifier;
                                assignment.Operation     = null;
                                assignment.SecondOperand = null;
                                isOptimized = true;
                            }
                            else
                            {
                                nodesByExpression[expression] = currentNode;
                            }
                        }
                    }

                    currentNode = currentNode.Next;
                }
            }

            return(isOptimized);
        }