More smarter temp locals

This commit is contained in:
dcodeIO 2017-12-12 09:38:20 +01:00
parent f75b962c74
commit c8680b1e77
5 changed files with 631 additions and 709 deletions

View File

@ -858,12 +858,12 @@ export class Compiler extends DiagnosticEmitter {
this.disallowContinue = true;
// introduce a local for evaluating the condition (exactly once)
const local: Local = this.currentFunction.addLocal(Type.i32);
const tempLocal: Local = this.currentFunction.getTempLocal(Type.i32);
let i: i32, k: i32 = statement.cases.length;
// prepend initializer to inner block
const breaks: ExpressionRef[] = new Array(1 + k);
breaks[0] = this.module.createSetLocal(local.index, this.compileExpression(statement.expression, Type.i32)); // initializer
breaks[0] = this.module.createSetLocal(tempLocal.index, this.compileExpression(statement.expression, Type.i32)); // initializer
// make one br_if per (possibly dynamic) labeled case (binaryen optimizes to br_table where possible)
let breakIndex: i32 = 1;
@ -873,7 +873,7 @@ export class Compiler extends DiagnosticEmitter {
if (case_.label) {
breaks[breakIndex++] = this.module.createBreak("case" + i.toString(10) + "|" + context,
this.module.createBinary(BinaryOp.EqI32,
this.module.createGetLocal(local.index, NativeType.I32),
this.module.createGetLocal(tempLocal.index, NativeType.I32),
this.compileExpression(case_.label, Type.i32)
)
);
@ -881,6 +881,8 @@ export class Compiler extends DiagnosticEmitter {
defaultIndex = i;
}
this.currentFunction.freeTempLocal(tempLocal);
// otherwise br to default respectively out of the switch if there is no default case
breaks[breakIndex] = this.module.createBreak((defaultIndex >= 0
? "case" + defaultIndex.toString(10)
@ -1495,7 +1497,7 @@ export class Compiler extends DiagnosticEmitter {
// );
// otherwise use a temporary local for the intermediate value
tempLocal = this.currentFunction.addLocal(this.currentType);
tempLocal = this.currentFunction.getAndFreeTempLocal(this.currentType);
condition = this.module.createTeeLocal(tempLocal.index, left);
return this.module.createIf(
this.currentType.isLongInteger
@ -1528,7 +1530,7 @@ export class Compiler extends DiagnosticEmitter {
// );
// otherwise use a temporary local for the intermediate value
tempLocal = this.currentFunction.addLocal(this.currentType);
tempLocal = this.currentFunction.getAndFreeTempLocal(this.currentType);
condition = this.module.createTeeLocal(tempLocal.index, left);
return this.module.createIf(
this.currentType.isLongInteger
@ -1848,7 +1850,7 @@ export class Compiler extends DiagnosticEmitter {
const getValue: ExpressionRef = this.compileExpression(expression.expression, contextualType, contextualType == Type.void ? ConversionKind.NONE : ConversionKind.IMPLICIT);
// use a temp local for the intermediate value
const tempLocal: Local = this.currentFunction.addLocal(this.currentType);
const tempLocal: Local = this.currentFunction.getTempLocal(this.currentType);
let op: BinaryOp;
let nativeType: NativeType;
@ -1886,6 +1888,7 @@ export class Compiler extends DiagnosticEmitter {
// NOTE: can't preemptively tee_local the return value on the stack because binaryen expects
// this to be well-formed. becomes a tee_local when optimizing, though.
this.currentType = tempLocal.type;
this.currentFunction.freeTempLocal(tempLocal);
return this.module.createBlock(null, [
this.module.createSetLocal(tempLocal.index, getValue), // +++ this.module.createTeeLocal(tempLocal.index, getValue),
setValue,

View File

@ -11,20 +11,8 @@
(func $start (; 0 ;) (type $v)
(local $0 i32)
(local $1 f64)
(local $2 i32)
(local $3 f64)
(local $4 i32)
(local $5 i32)
(local $6 f64)
(local $7 f64)
(local $8 i32)
(local $9 i32)
(local $10 i64)
(local $11 i64)
(local $12 f32)
(local $13 f32)
(local $14 f64)
(local $15 f64)
(local $2 i64)
(local $3 f32)
(drop
(if (result i32)
(tee_local $0
@ -48,68 +36,68 @@
)
(drop
(if (result i32)
(tee_local $2
(tee_local $0
(i32.const 1)
)
(get_local $2)
(get_local $0)
(unreachable)
)
)
(drop
(if (result f64)
(f64.ne
(tee_local $3
(tee_local $1
(f64.const 1)
)
(f64.const 0)
)
(get_local $3)
(get_local $1)
(unreachable)
)
)
(drop
(if (result i32)
(tee_local $5
(tee_local $0
(if (result i32)
(tee_local $4
(tee_local $0
(i32.const 1)
)
(i32.const 2)
(get_local $4)
(get_local $0)
)
)
(get_local $5)
(get_local $0)
(unreachable)
)
)
(drop
(if (result f64)
(f64.ne
(tee_local $7
(tee_local $1
(if (result f64)
(f64.ne
(tee_local $6
(tee_local $1
(f64.const 1)
)
(f64.const 0)
)
(f64.const 2)
(get_local $6)
(get_local $1)
)
)
(f64.const 0)
)
(get_local $7)
(get_local $1)
(unreachable)
)
)
(set_global $logical/i
(if (result i32)
(tee_local $8
(tee_local $0
(i32.const 1)
)
(i32.const 2)
(get_local $8)
(get_local $0)
)
)
(if
@ -123,10 +111,10 @@
)
(set_global $logical/i
(if (result i32)
(tee_local $9
(tee_local $0
(i32.const 0)
)
(get_local $9)
(get_local $0)
(i32.const 1)
)
)
@ -142,13 +130,13 @@
(set_global $logical/I
(if (result i64)
(i64.ne
(tee_local $10
(tee_local $2
(i64.const 1)
)
(i64.const 0)
)
(i64.const 2)
(get_local $10)
(get_local $2)
)
)
(if
@ -163,12 +151,12 @@
(set_global $logical/I
(if (result i64)
(i64.ne
(tee_local $11
(tee_local $2
(i64.const 0)
)
(i64.const 0)
)
(get_local $11)
(get_local $2)
(i64.const 1)
)
)
@ -184,13 +172,13 @@
(set_global $logical/f
(if (result f32)
(f32.ne
(tee_local $12
(tee_local $3
(f32.const 1)
)
(f32.const 0)
)
(f32.const 2)
(get_local $12)
(get_local $3)
)
)
(if
@ -205,12 +193,12 @@
(set_global $logical/f
(if (result f32)
(f32.ne
(tee_local $13
(tee_local $3
(f32.const 0)
)
(f32.const 0)
)
(get_local $13)
(get_local $3)
(f32.const 1)
)
)
@ -226,13 +214,13 @@
(set_global $logical/F
(if (result f64)
(f64.ne
(tee_local $14
(tee_local $1
(f64.const 1)
)
(f64.const 0)
)
(f64.const 2)
(get_local $14)
(get_local $1)
)
)
(if
@ -247,12 +235,12 @@
(set_global $logical/F
(if (result f64)
(f64.ne
(tee_local $15
(tee_local $1
(f64.const 0)
)
(f64.const 0)
)
(get_local $15)
(get_local $1)
(f64.const 1)
)
)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -10,21 +10,9 @@
(start $start)
(func $start (; 0 ;) (type $v)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i64)
(local $5 i64)
(local $6 i64)
(local $7 i64)
(local $8 f32)
(local $9 f32)
(local $10 f32)
(local $11 f32)
(local $12 f64)
(local $13 f64)
(local $14 f64)
(local $15 f64)
(local $1 i64)
(local $2 f32)
(local $3 f64)
(drop
(i32.const 1)
)
@ -107,16 +95,16 @@
)
(drop
(block (result i32)
(set_local $1
(set_local $0
(get_global $unary/i)
)
(set_global $unary/i
(i32.sub
(get_local $1)
(get_local $0)
(i32.const 1)
)
)
(get_local $1)
(get_local $0)
)
)
(set_global $unary/i
@ -183,30 +171,30 @@
)
(set_global $unary/i
(block (result i32)
(set_local $2
(set_local $0
(get_global $unary/i)
)
(set_global $unary/i
(i32.add
(get_local $2)
(get_local $0)
(i32.const 1)
)
)
(get_local $2)
(get_local $0)
)
)
(set_global $unary/i
(block (result i32)
(set_local $3
(set_local $0
(get_global $unary/i)
)
(set_global $unary/i
(i32.sub
(get_local $3)
(get_local $0)
(i32.const 1)
)
)
(get_local $3)
(get_local $0)
)
)
(drop
@ -243,30 +231,30 @@
)
(drop
(block (result i64)
(set_local $4
(set_local $1
(get_global $unary/I)
)
(set_global $unary/I
(i64.add
(get_local $4)
(get_local $1)
(i64.const 1)
)
)
(get_local $4)
(get_local $1)
)
)
(drop
(block (result i64)
(set_local $5
(set_local $1
(get_global $unary/I)
)
(set_global $unary/I
(i64.sub
(get_local $5)
(get_local $1)
(i64.const 1)
)
)
(get_local $5)
(get_local $1)
)
)
(set_global $unary/I
@ -337,30 +325,30 @@
)
(set_global $unary/I
(block (result i64)
(set_local $6
(set_local $1
(get_global $unary/I)
)
(set_global $unary/I
(i64.add
(get_local $6)
(get_local $1)
(i64.const 1)
)
)
(get_local $6)
(get_local $1)
)
)
(set_global $unary/I
(block (result i64)
(set_local $7
(set_local $1
(get_global $unary/I)
)
(set_global $unary/I
(i64.sub
(get_local $7)
(get_local $1)
(i64.const 1)
)
)
(get_local $7)
(get_local $1)
)
)
(drop
@ -391,30 +379,30 @@
)
(drop
(block (result f32)
(set_local $8
(set_local $2
(get_global $unary/f)
)
(set_global $unary/f
(f32.add
(get_local $8)
(get_local $2)
(f32.const 1)
)
)
(get_local $8)
(get_local $2)
)
)
(drop
(block (result f32)
(set_local $9
(set_local $2
(get_global $unary/f)
)
(set_global $unary/f
(f32.sub
(get_local $9)
(get_local $2)
(f32.const 1)
)
)
(get_local $9)
(get_local $2)
)
)
(set_global $unary/f
@ -469,30 +457,30 @@
)
(set_global $unary/f
(block (result f32)
(set_local $10
(set_local $2
(get_global $unary/f)
)
(set_global $unary/f
(f32.add
(get_local $10)
(get_local $2)
(f32.const 1)
)
)
(get_local $10)
(get_local $2)
)
)
(set_global $unary/f
(block (result f32)
(set_local $11
(set_local $2
(get_global $unary/f)
)
(set_global $unary/f
(f32.sub
(get_local $11)
(get_local $2)
(f32.const 1)
)
)
(get_local $11)
(get_local $2)
)
)
(drop
@ -523,30 +511,30 @@
)
(drop
(block (result f64)
(set_local $12
(set_local $3
(get_global $unary/F)
)
(set_global $unary/F
(f64.add
(get_local $12)
(get_local $3)
(f64.const 1)
)
)
(get_local $12)
(get_local $3)
)
)
(drop
(block (result f64)
(set_local $13
(set_local $3
(get_global $unary/F)
)
(set_global $unary/F
(f64.sub
(get_local $13)
(get_local $3)
(f64.const 1)
)
)
(get_local $13)
(get_local $3)
)
)
(set_global $unary/F
@ -605,30 +593,30 @@
)
(set_global $unary/F
(block (result f64)
(set_local $14
(set_local $3
(get_global $unary/F)
)
(set_global $unary/F
(f64.add
(get_local $14)
(get_local $3)
(f64.const 1)
)
)
(get_local $14)
(get_local $3)
)
)
(set_global $unary/F
(block (result f64)
(set_local $15
(set_local $3
(get_global $unary/F)
)
(set_global $unary/F
(f64.sub
(get_local $15)
(get_local $3)
(f64.const 1)
)
)
(get_local $15)
(get_local $3)
)
)
)