private static TACBaseBlocks AllOptimization(List <TACInstruction> Instructions) { if (TACOptimizersAllBlock.Count == 0) { var r = new TACBaseBlocks(Instructions); r.GenBaseBlocks(); return(r); } List <TACInstruction> previos; previos = Instructions; int AllOptimizationCount = 0; do { TACOptimizersAllBlock[AllOptimizationCount].Instructions = previos.Copy(); TACOptimizersAllBlock[AllOptimizationCount].Run(); if (previos.SequenceEqual(TACOptimizersAllBlock[AllOptimizationCount].Instructions)) { AllOptimizationCount++; } else { previos = TACOptimizersAllBlock[AllOptimizationCount].Instructions; AllOptimizationCount = 0; GlobalOptimizationCount = 0; } } while (AllOptimizationCount < TACOptimizersAllBlock.Count); ++GlobalOptimizationCount; var res = new TACBaseBlocks(previos); res.GenBaseBlocks(); return(res); }
public void OneBlock2() { var TAC = GenerateTAC( @" { x = 1 + y; t = 1 + y; y = 2; z = 1 + y; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new AvailableExpressionsOptimizer(); optimizer.Run(cfg, blocks.blocks); var actual = blocks.blocks.Select(b => b.ToString().Trim()); var expected = new List <string>() { "x = 1 + y\n" + "t = x\n" + "y = 2\n" + "z = 1 + y" }; Assert.AreEqual(actual, expected); }
public void OneBlockWithLabelTest2() { var TAC = GenerateTAC( @" { int x; x = 14; 2: y = 2 * (a + b) - c; x = x + x; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); Assert.AreEqual(blocks.blocks.Count, 1); var expected = new List <List <string> >() { new List <string>() { "x = 14", "2", "#t0 = a + b", "#t1 = 2 * #t0", "#t2 = #t1 - c", "y = #t2", "x = x + x" } }; var actual = blocks.blocks.Select(block => block.Instructions.Select(instr => instr.ToString().Trim())).ToList(); Assert.AreEqual(expected, actual); }
public void Simple() { var TAC = GenerateTAC( @" { a = 3; b = 3; c = a + b; a = d; e = a; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new ConstantPropagationIter(); optimizer.Cfg = cfg; optimizer.Instructions = TAC.Instructions; optimizer.Blocks = blocks.blocks; optimizer.Run(); var actual = optimizer.Instructions.Select(i => i.ToString().Trim()).ToList(); var expected = new List <string>() { "a = 3", "b = 3", "c = 3 + 3", "a = d", "e = a" }; Assert.AreEqual(expected, actual); }
private static TACBaseBlocks IterAlgoOptimizations(TACBaseBlocks blocks) { var prevInstructions = blocks.BlockMerging(); int AllOptimizationCount = 0; TACBaseBlocks prevBlocks; do { IterAlgoOptimizers[AllOptimizationCount].Instructions = prevInstructions.Copy(); BasicBlock.clearIndexCounter(); prevBlocks = new TACBaseBlocks(IterAlgoOptimizers[AllOptimizationCount].Instructions); prevBlocks.GenBaseBlocks(); IterAlgoOptimizers[AllOptimizationCount].Blocks = prevBlocks.blocks; var cfg = new ControlFlowGraph(prevBlocks.blocks); IterAlgoOptimizers[AllOptimizationCount].Cfg = cfg; IterAlgoOptimizers[AllOptimizationCount].Run(); if (prevInstructions.SequenceEqual(IterAlgoOptimizers[AllOptimizationCount].Instructions)) { AllOptimizationCount++; } else { prevInstructions = IterAlgoOptimizers[AllOptimizationCount].Instructions; AllOptimizationCount = 0; GlobalOptimizationCount = 0; } } while (AllOptimizationCount < IterAlgoOptimizers.Count); ++GlobalOptimizationCount; return(prevBlocks); }
public void MergingBlocksTest1() { var TAC = GenerateTAC( @" { int x; x = 14; y = 2 * (a + b) - c; x = x + x; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); Assert.AreEqual(blocks.blocks.Count, 1); var expected = new List <string>() { "x = 14", "#t0 = a + b", "#t1 = 2 * #t0", "#t2 = #t1 - c", "y = #t2", "x = x + x" }; var actual = blocks.BlockMerging().Select(instructions => instructions.ToString().Trim()); Assert.AreEqual(expected, actual); }
protected ControlFlowGraph GenerateCFG(string sourceCode) { Scanner scanner = new Scanner(); scanner.SetSource(sourceCode, 0); Parser parser = new Parser(scanner); parser.Parse(); var parentFiller = new FillParentsVisitor(); parser.root.Visit(parentFiller); var TACGenerator = new TACGenerationVisitor(); parser.root.Visit(TACGenerator); var blocks = new TACBaseBlocks(TACGenerator.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); return(cfg); }
public void ManyBlockTest2() { var TAC = GenerateTAC( @" { i = m - 1; j = n; a = u1; 2: i = i + 1; j = j - 1; if a { a = u2; } i = u3; if b { goto 2; } } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new ReachingDefinitionOptimizer(cfg); var vectorOptimizer = new ReachingDefinitionVectorOptimizer(cfg); CheckEquality(optimizer, vectorOptimizer); }
public void ManyBlockTest1() { var TAC = GenerateTAC( @" { n = 4; c = n + 5; if a + 3 > 4 * 2 { a = a + 3; } else { b = 5; } s = 8; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new ReachingDefinitionOptimizer(cfg); var vectorOptimizer = new ReachingDefinitionVectorOptimizer(cfg); CheckEquality(optimizer, vectorOptimizer); }
public void ManyBlockWithFakeLabelTest2() { var TAC = GenerateTAC( @" { int x; x = 14; y = 2 * (a + b) - c; x = x + x; if x == 2 { 4: y = 1; b = 1; } } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); Assert.AreEqual(blocks.blocks.Count, 4); var expected = new List <List <string> >() { new List <string>() { "x = 14", "#t0 = a + b", "#t1 = 2 * #t0", "#t2 = #t1 - c", "y = #t2", "x = x + x", "#t3 = x == 2", "if #t3 goto #L0" }, new List <string>() { "goto #L1" }, new List <string>() { "#L0", "4", "y = 1", "b = 1" }, new List <string>() { "#L1" } }; var actual = blocks.blocks.Select(block => block.Instructions.Select(instr => instr.ToString().Trim())).ToList(); Assert.AreEqual(expected, actual); }
protected (List <BasicBlock> basicBlocks, InOutInfo inOutInfo) GenGraphAndGetInOutInfo(string program) { var TAC = GenerateTAC(program); var TACBlocks = new TACBaseBlocks(TAC.Instructions); TACBlocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(TACBlocks.blocks); var inOutInfo = new SampleClassIterAlgoForTransferFunc().Execute(cfg); return(TACBlocks.blocks, inOutInfo); }
public NaturalLoop GetLoopFromBlocksCode(params string[] blocksCode) { var result = new NaturalLoop(null, null); foreach (var code in blocksCode) { var block = GenerateTAC(code); var TACBlock = new TACBaseBlocks(block.Instructions); TACBlock.GenBaseBlocks(); result.AddBlock(TACBlock.blocks[0]); } return(result); }
public List <NaturalLoop> GetLoops(string sourceCode) { var TAC = GenerateTAC(sourceCode); var TACBlocks = new TACBaseBlocks(TAC.Instructions); TACBlocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(TACBlocks.blocks); var dt = new DominatorsTree(cfg); dt.GenDominatorsTree(); var loops = TACUtils.GetNaturalLoops(cfg, dt.dominators); return(loops); }
public void OneBlockTest1() { var TAC = GenerateTAC( @" { x = z + y; u = x; t = z + y; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new ReachingDefinitionOptimizer(cfg); var vectorOptimizer = new ReachingDefinitionVectorOptimizer(cfg); CheckEquality(optimizer, vectorOptimizer); }
public void GenericPropagation2() { var TAC = GenerateTAC( @" { b = 3; if a { goto 1; } b = 2; 1: r = b; } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new ConstantPropagationIter(); optimizer.Cfg = cfg; optimizer.Instructions = TAC.Instructions; optimizer.Blocks = blocks.blocks; optimizer.Run(); var actual = optimizer.Instructions.Select(i => i.ToString().Trim()).ToList(); var expected = new List <string>() { "b = 3", "if a goto #L0", "goto #L1", "#L0", "goto 1", "#L1", "b = 2", "1", "r = b" }; Assert.AreEqual(expected, actual); }
public static TACBaseBlocks Optimize(Parser parser, bool debugInfo = false) { var TACGenerator = new TACGenerationVisitor(); parser.root.Visit(TACGenerator); var TACBlocks = new TACBaseBlocks(TACGenerator.Instructions); TACBlocks.GenBaseBlocks(); TACBaseBlocks result = TACBlocks; var i = 1; do { var oneBlockOptimizations = OptimizeBlock(result); if (debugInfo) { Console.WriteLine("===============TAC EachBlockOpt: Stage {0}===============", i.ToString()); PrintInstructions(oneBlockOptimizations); ++i; } result = AllOptimization(oneBlockOptimizations); if (debugInfo) { Console.WriteLine("===============TAC AllBlocksOpt: Stage {0}===============", i.ToString()); PrintInstructions(result.BlockMerging()); ++i; } result = IterAlgoOptimizations(result); if (debugInfo) { Console.WriteLine("=================TAC IterAlgs: Stage {0}=================", i.ToString()); PrintInstructions(result.BlockMerging()); ++i; } } while (GlobalOptimizationCount < 3); return(result); }
public void GlobalManyBlocksTest() { var TAC = GenerateTAC( @" { for i=1..10 { for j=1..10 { a = 0; } } for i=1..10 { a = 1; } } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); Assert.AreEqual(blocks.blocks.Count, 10); var expected = new List <List <string> >() { new List <string>() { "i = 1" }, new List <string>() { "#L0\t#t0 = i > 10", "if #t0 goto #L1" }, new List <string>() { "j = 1" }, new List <string>() { "#L2\t#t1 = j > 10", "if #t1 goto #L3" }, new List <string>() { "a = 0", "j = j + 1", "goto #L2" }, new List <string>() { "#L3", "i = i + 1", "goto #L0" }, new List <string>() { "#L1", "i = 1" }, new List <string>() { "#L4\t#t2 = i > 10", "if #t2 goto #L5" }, new List <string>() { "a = 1", "i = i + 1", "goto #L4" }, new List <string>() { "#L5" }, }; var actual = blocks.blocks.Select(block => block.Instructions.Select(instr => instr.ToString().Trim())).ToList(); Assert.AreEqual(expected, actual); var expectedMerg = new List <string>() { "i = 1", "#L0\t#t0 = i > 10", "if #t0 goto #L1", "j = 1", "#L2\t#t1 = j > 10", "if #t1 goto #L3", "a = 0", "j = j + 1", "goto #L2", "#L3", "i = i + 1", "goto #L0", "#L1", "i = 1", "#L4\t#t2 = i > 10", "if #t2 goto #L5", "a = 1", "i = i + 1", "goto #L4", "#L5" }; var actualMerg = blocks.BlockMerging().Select(instructions => instructions.ToString().Trim()); Assert.AreEqual(expectedMerg, actualMerg); }
public void ManyBlocks() { var TAC = GenerateTAC( @" { 1: a = b + c; d = b + c; b = b + c; t = b + c; a = d * e; if x { goto 1; } 3: o = d * e; a = d * e; if y { goto 3; } } "); var blocks = new TACBaseBlocks(TAC.Instructions); blocks.GenBaseBlocks(); var cfg = new ControlFlowGraph(blocks.blocks); var optimizer = new AvailableExpressionsOptimizer(); optimizer.Run(cfg, blocks.blocks); var actual = blocks.blocks.Select(b => b.ToString().Trim()); var expected = new List <string>() { "1\n" + "a = b + c\n" + "d = a\n" + "b = a\n" + "t = b + c\n" + "a = d * e\n" + "if x goto #L0", "goto #L1", "#L0\n" + "goto 1", "#L1", "3\n" + "o = a\n" + "a = a\n" + "if y goto #L2", "goto #L3", "#L2\n" + "goto 3", "#L3" }; Assert.AreEqual(actual, expected); }