Skip to content

Commit

Permalink
Optimize: merge local.set, local.get --> local.tee
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Mar 10, 2019
1 parent 86defc3 commit cd729d1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/de/inetsoftware/jwebassembly/module/CodeOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import java.util.List;

import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.VariableOperator;

/**
* Optimize the code of a single method/function through using of WebAssembly features without equivalent in Java.
*
Expand All @@ -35,6 +38,22 @@ public void optimze( List<WasmInstruction> instructions ) {
WasmInstruction instr = instructions.get( i );
switch( instr.getType() ) {
case Local:
// merge local.set, local.get --> local.tee
if( i == 0 ) {
continue;
}
if( instructions.get( i-1 ).getType() != Type.Local ) {
continue;
}
WasmLocalInstruction local1 = (WasmLocalInstruction)instructions.get( i-1 );
WasmLocalInstruction local2 = (WasmLocalInstruction)instr;
if( local1.getIndex() != local2.getIndex() ) {
continue;
}
if( local1.getOperator() == VariableOperator.set && local2.getOperator() == VariableOperator.get ) {
local1.setOperator( VariableOperator.tee );
instructions.remove( i );
}
break;
default:
}
Expand Down
19 changes: 19 additions & 0 deletions src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ Type getType() {
return Type.Local;
}

/**
* Get the operator
*
* @return the operator
*/
VariableOperator getOperator() {
return op;
}

/**
* Set the operator
*
* @param op
* the operator
*/
void setOperator( VariableOperator op ) {
this.op = op;
}

/**
* Get the number of the locals
*
Expand Down

0 comments on commit cd729d1

Please sign in to comment.