diff --git a/src/main/java/dk/camelot64/kickc/Compiler.java b/src/main/java/dk/camelot64/kickc/Compiler.java index 6440f0256..a2fac8fe0 100644 --- a/src/main/java/dk/camelot64/kickc/Compiler.java +++ b/src/main/java/dk/camelot64/kickc/Compiler.java @@ -249,6 +249,7 @@ public class Compiler { optimizations.add(new Pass2ConditionalAndOrRewriting(program)); optimizations.add(new Pass2ConstantRValueConsolidation(program)); optimizations.add(new Pass2ConstantIdentification(program)); + optimizations.add(new Pass2ConstantValues(program)); optimizations.add(new PassNStatementIndices(program)); optimizations.add(new PassNVariableReferenceInfos(program)); //optimizations.add(new Pass2ConstantAdditionElimination(program)); @@ -308,6 +309,7 @@ public class Compiler { constantOptimizations.add(new Pass2IdenticalPhiElimination(program)); constantOptimizations.add(new Pass2ConstantRValueConsolidation(program)); constantOptimizations.add(new Pass2ConstantIdentification(program)); + constantOptimizations.add(new Pass2ConstantValues(program)); constantOptimizations.add(new Pass2ConstantAdditionElimination(program)); constantOptimizations.add(new Pass2ConstantSimplification(program)); constantOptimizations.add(new Pass2ConstantCastSimplification(program)); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantValues.java b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantValues.java new file mode 100644 index 000000000..e81c7b10b --- /dev/null +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantValues.java @@ -0,0 +1,36 @@ +package dk.camelot64.kickc.passes; + +import dk.camelot64.kickc.model.Program; +import dk.camelot64.kickc.model.iterator.ProgramValueIterator; +import dk.camelot64.kickc.model.values.*; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Compiler Pass identifying constants values + */ +public class Pass2ConstantValues extends Pass2SsaOptimization { + + public Pass2ConstantValues(Program program) { + super(program); + } + + @Override + public boolean step() { + AtomicBoolean modified = new AtomicBoolean(false); + + ProgramValueIterator.execute(getProgram().getGraph(), (programValue, currentStmt, stmtIt, currentBlock) -> { + Value value = programValue.get(); + if((value instanceof RValue) && !(value instanceof ConstantValue)) { + ConstantValue constant = Pass2ConstantIdentification.getConstant((RValue) value); + if(constant!=null) { + programValue.set(constant); + getLog().append("Constant value identified " + value+ " in "+ (currentStmt==null?"":currentStmt.toString(getProgram(), false))); + modified.set(true); + } + } + }); + return modified.get(); + } + +}