Skip to content

Commit 7bece4c

Browse files
committed
Handle for loops with constant indices but byte or word variables
1 parent 8056b86 commit 7bece4c

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Version 6.6.2
22
- Added warning (-Warray-index) for constant array indices out of bounds
33
- Allow warnings and optimizations to be turned off with a `no-` prefix (so `warn(no-init-vars)` is like `warn(!init-vars)`).
4+
- Added some peephole optimizations to remove unnecessary sign extension
45

56
Version 6.6.1
67
- Changed type of `dat` array in C++ output to `unsigned char`

Test/Expect/stest305.pasm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub main
2+
coginit(0, @entry, 0)
3+
dat
4+
org 0
5+
entry
6+
7+
_blah
8+
rdword _var01, arg01
9+
and _var01, #7
10+
wrword _var01, arg01
11+
_blah_ret
12+
ret
13+
14+
COG_BSS_START
15+
fit 496
16+
org COG_BSS_START
17+
_var01
18+
res 1
19+
arg01
20+
res 1
21+
fit 496

Test/stest305.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void blah(short *x) {
2+
*x &= 7;
3+
}

loops.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,9 +1030,25 @@ CheckSimpleIncrementLoop(AST *stmt)
10301030
} else {
10311031
return;
10321032
}
1033-
if (!AstMatchName(updateVar, condtest->left))
1034-
return;
1035-
1033+
if (!AstMatchName(updateVar, condtest->left)) {
1034+
// special case sign extension
1035+
bool isOK = false;
1036+
if (condtest->left && condtest->left->kind == AST_OPERATOR && (condtest->left->d.ival == K_SIGNEXTEND || condtest->left->d.ival == K_ZEROEXTEND) ) {
1037+
if (IsConstExpr(condtest->left->right) && AstMatchName(updateVar, condtest->left->left) && IsConstExpr(finalValue)) {
1038+
int32_t finalVal = EvalConstExpr(finalValue);
1039+
int32_t extendVal = EvalConstExpr(condtest->left->right);
1040+
extendVal = 1<<(extendVal & 0x1f);
1041+
if (condtest->left->d.ival == K_SIGNEXTEND) {
1042+
extendVal = extendVal >> 1;
1043+
}
1044+
isOK = (finalVal >= 0) && (finalVal < extendVal);
1045+
}
1046+
}
1047+
if (!isOK) {
1048+
return;
1049+
}
1050+
}
1051+
10361052
/* check that the update is i++, and the variable is not used anywhere else */
10371053
while (update->kind == AST_SEQUENCE) {
10381054
if (AstUsesName(update->right, updateVar)) {

0 commit comments

Comments
 (0)