Skip to content

Commit

Permalink
add a unit test for a problematic optimization pattern to prevent it …
Browse files Browse the repository at this point in the history
…from happening again
  • Loading branch information
Santiago Ontañón authored and Santiago Ontañón committed Sep 6, 2022
1 parent 4ad6514 commit fa20fb1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/main/java/workers/pattopt/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,16 @@ public int getSpaceSaving(PatternMatch match, CodeBase code)
for(CPUOpPattern pat:replacement) {
if (!pat.isWildcard()) {
CPUOp ipat = pat.instantiate(match, this, config);
int irepSize = ipat.sizeInBytes();
int n = 1;
if (pat.repetitionVariable != null) {
n = match.variables.get(pat.repetitionVariable).evaluateToInteger(null, code, true);
if (ipat == null) {
config.error("An optimization pattern ("+this.name+") has generated ilegal code!");
} else {
int irepSize = ipat.sizeInBytes();
int n = 1;
if (pat.repetitionVariable != null) {
n = match.variables.get(pat.repetitionVariable).evaluateToInteger(null, code, true);
}
replacementSize += n * irepSize;
}
replacementSize += n * irepSize;
}
}
int spaceSaving = patternSize - replacementSize;
Expand All @@ -289,7 +293,12 @@ public int[] getTimeSaving(PatternMatch match, CodeBase code)
int replacementTime[] = {0,0};
for(CPUOpPattern pat:pattern) {
if (!pat.isWildcard()) {
int tmp[] = pat.instantiate(match, this, config).timing();
CPUOp ipat = pat.instantiate(match, this, config);
if (ipat == null) {
config.error("An optimization pattern ("+this.name+") has generated ilegal code!");
continue;
}
int tmp[] = ipat.timing();
int n = 1;
if (pat.repetitionVariable != null) {
n = match.variables.get(pat.repetitionVariable).evaluateToInteger(null, code, true);
Expand All @@ -304,7 +313,12 @@ public int[] getTimeSaving(PatternMatch match, CodeBase code)
}
for(CPUOpPattern pat:replacement) {
if (!pat.isWildcard()) {
int tmp[] = pat.instantiate(match, this, config).timing();
CPUOp ipat = pat.instantiate(match, this, config);
if (ipat == null) {
config.error("An optimization pattern ("+this.name+") has generated ilegal code!");
continue;
}
int tmp[] = ipat.timing();
int n = 1;
if (pat.repetitionVariable != null) {
n = match.variables.get(pat.repetitionVariable).evaluateToInteger(null, code, true);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/data/pbo-patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pattern: Remove unused ld ?reg,?any
0: ld ?reg,?any
replacement:
constraints:
notIn(?reg,I)
regsNotUsedAfter(0,?reg)

pattern: Replace ld a,?const with xor a
Expand Down Expand Up @@ -1928,6 +1929,7 @@ replacement:
1: *
2: ld ?reg2,?const1
constraints:
notIn(?reg2,I)
regsNotUsed(1,?reg1)
regsNotUsedAfter(2,?reg1)

Expand Down
1 change: 1 addition & 0 deletions src/test/java/test/PatternBasedOptimizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public PatternBasedOptimizerTest() {
@Test public void test73() throws Exception { test("data/potests/test73.asm", null, null, "speed", 10, 74, 74, "data/potests/test73-expected.asm"); }
@Test public void test74() throws Exception { test("data/potests/test74.asm", null, null, "speed", 3, 28, 28, "data/potests/test74-expected.asm"); }
@Test public void test75() throws Exception { test("data/potests/test75.asm", "sdcc", null, "speed", 1, 3, 3, "data/potests/test75-expected.asm"); }
@Test public void test76() throws Exception { test("data/potests/test76.asm", null, null, "size", 0, 0, 0, null); }


private void test(String inputFile, String dialect, String cpu, String target,
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/data/potests/test76.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
org #4000

ld a, #fe
ld i, a

0 comments on commit fa20fb1

Please sign in to comment.