Skip to content

Commit ceebabd

Browse files
authored
Merge pull request #197 from nevillegrech/simplify_global
Various optimizations at the local and global analysis levels
2 parents 0e8cbe3 + 0de2587 commit ceebabd

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

logic/functions.dl

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ PossibleReturnAddressWithPos(targetSetter, retCtx, retBlock, retTarget, pos) :-
107107
postTrans.Statement_Block(targetSetStatement, targetSetter),
108108
postTrans.BasicBlock_Tail(targetSetter, jumpStmt),
109109
postTrans.JUMP(jumpStmt),
110-
postTrans.BeforeLocalStackContents(jumpStmt, pos, targetVariable).
110+
postTrans.BlockOutLocalStackContents(targetSetter, pos, targetVariable).
111111

112112
/**
113113
This pattern is prominent in 0.8.x code:
@@ -140,7 +140,7 @@ PossibleReturnAddressWithPos(as(next, Block), retCtx, retBlock, retTarget, pos)
140140
postTrans.StaticBlockJumpTarget(targetSetter, next),
141141
postTrans.BasicBlock_Tail(as(next, Block), jumpStmt),
142142
postTrans.JUMP(jumpStmt),
143-
postTrans.BeforeLocalStackContents(jumpStmt, pos, stackIndex),
143+
postTrans.BlockOutLocalStackContents(as(next, Block), pos, stackIndex),
144144
// filter out cases where the function is called directly
145145
!postTrans.IsJumpBlockPrivateFunctionCall(_, as(next, Block)).
146146

@@ -833,6 +833,11 @@ IRLocalStackContents(irstmt, n, varOrStackIndex) :-
833833
Statement_IRStatement(stmt, _, irstmt).
834834

835835

836+
.decl IRBlockOutLocalStackContents(irBlock: IRBlock, n: StackIndex, varOrStackIndex: VariableOrStackIndex)
837+
IRBlockOutLocalStackContents(irBlock, n, varOrStackIndex) :-
838+
postTrans.BlockOutLocalStackContents(block, n, varOrStackIndex),
839+
Block_IRBlock(block, _, irBlock).
840+
836841
/***********
837842
* Discovery of arguments accepted by a function
838843
***********/
@@ -1154,28 +1159,40 @@ BeforeFunctionCallFunctionalBlockOutputContents(block, index, realVariable) :-
11541159
CheckIsStackIndex(stackIndex).
11551160

11561161

1157-
// TODO, check case for function call
1162+
// uses of the jump/call stmt
11581163
FunctionalStatement_Uses_Local(stmt, varOrStackIndex, n) :-
11591164
IRFunctionCall(block, func),
11601165
IRBasicBlock_Tail(block, stmt),
11611166
IRStatement_Opcode(stmt, opcode),
11621167
OpcodePopWords(opcode, m),
1163-
//// REVIEW
1164-
// PossibleNumberOfFunctionArguments(func, n_in),
1168+
NumberOfFunctionArguments(func, _),
1169+
IRBeforeLocalStackContents(stmt, n, varOrStackIndex), n < m.
1170+
1171+
// function args
1172+
FunctionalStatement_Uses_Local(stmt, varOrStackIndex, n + m) :-
1173+
IRFunctionCall(block, func),
1174+
IRBasicBlock_Tail(block, stmt),
1175+
IRStatement_Opcode(stmt, opcode),
1176+
OpcodePopWords(opcode, m),
11651177
NumberOfFunctionArguments(func, n_in),
1166-
IsStackIndexLessThan(n, m+n_in),
1167-
IRBeforeLocalStackContents(stmt, n, varOrStackIndex).
1178+
IRBlockOutLocalStackContents(block, n, varOrStackIndex), n < n_in.
11681179

11691180
// TODO, check case for function return
11701181
FunctionalStatement_Uses_Local(stmt, varOrStackIndex, n) :-
1171-
//// REVIEW
1182+
NumberOfFunctionReturnArguments(func, _),
1183+
IRFunction_Return(func, retBlock),
1184+
IRBasicBlock_Tail(retBlock, stmt),
1185+
IRStatement_Opcode(stmt, opcode),
1186+
OpcodePopWords(opcode, m),
1187+
IRBeforeLocalStackContents(stmt, n, varOrStackIndex), n < m.
1188+
1189+
FunctionalStatement_Uses_Local(stmt, varOrStackIndex, n + m) :-
11721190
NumberOfFunctionReturnArguments(func, n_out),
11731191
IRFunction_Return(func, retBlock),
11741192
IRBasicBlock_Tail(retBlock, stmt),
11751193
IRStatement_Opcode(stmt, opcode),
11761194
OpcodePopWords(opcode, m),
1177-
IsStackIndexLessThan(n, m+n_out),
1178-
IRBeforeLocalStackContents(stmt, n, varOrStackIndex).
1195+
IRBlockOutLocalStackContents(retBlock, n, varOrStackIndex), n < n_out.
11791196

11801197
FunctionalStatement_Uses_Local(irstmt, varOrStackIndex, n) :-
11811198
postTrans.Statement_Uses_Local(stmt, varOrStackIndex, n),

logic/global_components.dl

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,18 @@
132132
BlockOutputContents(callerCtx, caller, index, variable),
133133
BlockEdge(callerCtx, caller, calleeCtx, callee).
134134
.plan 1:(2,1)
135-
136-
// Copy the stack variables untouched by the callee basic block
137-
BlockOutputContents(calleeCtx, callee, index+delta, variable) :-
138-
BlockInputContents(calleeCtx, callee, index, variable),
139-
BlockPopDelta(callee, calleePopLen),
140-
BlockStackDelta(callee, delta),
141-
index >= calleePopLen,
142-
index+delta <= MAX_STACK_HEIGHT.
143135

144136
// Handle the proper variables pushed by this basic block
145137
BlockOutputContents(ctx, block, index, as(var, Variable)) :-
146138
ReachableContext(ctx, block),
147-
BasicBlock_Tail(block, stmt),
148-
LocalStackContents(stmt, index, var),
139+
BlockOutLocalStackContents(block, index, var),
149140
CheckIsVariable(var).
150141

151-
152-
// Handle the stackIndexes pushed by this basic block
142+
// Handle the indexes: stack contents pushed by previous blocks
153143
BlockOutputContents(ctx, block, index, as(realVariable, Variable)) :-
154144
BlockInputContents(ctx, block, stackIndex, realVariable),
155145
CheckIsStackIndex(stackIndex),
156-
BasicBlock_Tail(block, stmt),
157-
LocalStackContents(stmt, index, stackIndex).
158-
146+
BlockOutLocalStackContents(block, index, stackIndex).
159147

160148

161149
BlockJumpTarget(ctx, block, var) :-

logic/local_components.dl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,12 @@ IsStackIndexLessThan(n, maximum) :- n = range(0, maximum, 1).
268268

269269
// What is the difference in stack height after the execution of this `statement`
270270
.decl StatementStackDelta(statement: Statement, delta: number)
271-
271+
272+
// block-level stack behavior predicate: After the executin of block, stack `index` will hold
273+
// `variableOrIndex` which is either a variable pushed by the block or a reference to a stackindex at its beginning
274+
.decl BlockOutLocalStackContents(block: Block, index: StackIndex, variableOrIndex: VariableOrStackIndex)
275+
DEBUG_OUTPUT(BlockOutLocalStackContents)
276+
272277
ImmediateBlockJumpTarget(block, as(var, Variable)) :-
273278
BasicBlock_Tail(block, stmt),
274279
IsJump(stmt),
@@ -349,8 +354,7 @@ IsStackIndexLessThan(n, maximum) :- n = range(0, maximum, 1).
349354
LocalStackContents(stmt, m+1, other) :-
350355
BeforeLocalStackContents(stmt, m, other),
351356
Statement_Opcode(stmt, opcode),
352-
DUPN(opcode, n),
353-
IsStackIndexLessThan(m, n).
357+
DUPN(opcode, n), m < n.
354358

355359
/**
356360
Optimization: before local stack contents for swaps
@@ -372,7 +376,7 @@ IsStackIndexLessThan(n, maximum) :- n = range(0, maximum, 1).
372376

373377
LocalStackContents(stmt, m, other) :-
374378
BeforeSwapLocalStackContents(stmt, n, m, other),
375-
IsStackIndexLessThan(m, n), m > 0.
379+
m < n, m > 0.
376380

377381
/*
378382
***********
@@ -398,7 +402,7 @@ IsStackIndexLessThan(n, maximum) :- n = range(0, maximum, 1).
398402
IsBasicBlockHead(stmt),
399403
Statement_Block(stmt, block),
400404
BlockPopDelta(block, delta),
401-
IsStackIndexLessThan(stackIndex, delta + 10).
405+
IsStackIndexLessThan(stackIndex, delta + 2).
402406

403407
/*
404408
***********
@@ -442,13 +446,22 @@ IsStackIndexLessThan(n, maximum) :- n = range(0, maximum, 1).
442446
BasicBlock_Tail(block, stmt),
443447
StatementPopDelta(stmt, delta).
444448

449+
BlockOutLocalStackContents(block, index, varOrIndex):-
450+
BasicBlock_Tail(block, tail),
451+
LocalStackContents(tail, index, varOrIndex).
452+
453+
BlockOutLocalStackContents(block, outIndex, as(outIndex - stackDelta, StackIndex)):-
454+
BlockPopDelta(block, popDelta),
455+
BlockStackDelta(block, stackDelta),
456+
outIndex = range(popDelta + stackDelta + 1, MAX_STACK_HEIGHT + 1, 1).
457+
445458
.decl Statement_Uses_Local(stmt:Statement, var:VariableOrStackIndex, n:StackIndex)
446459

447460
Statement_Uses_Local(stmt, varOrStackIndex, n) :-
448461
Statement_Opcode(stmt, opcode),
449462
!TACNOP(stmt),
450463
OpcodePopWords(opcode, m),
451-
IsStackIndexLessThan(n, m),
464+
n < m,
452465
BeforeLocalStackContents(stmt, n, varOrStackIndex).
453466

454467
// Identify low-level blocks that will always REVERT

logic/types_defs.dl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#endif
1515

1616
#define CheckIsVariable(v) ((v) < 0)
17-
#define CheckIsStackIndex(v) ((v) >= 0, (v) < MAX_STACK_HEIGHT)
17+
#define CheckIsStackIndex(v) ((v) >= 0, (v) <= MAX_STACK_HEIGHT)
1818

1919

2020
// The following 4 constants as also defined in src/common.py

0 commit comments

Comments
 (0)