Skip to content

Commit efd469a

Browse files
committed
for P1, optimize short branches forward to the return label
1 parent 0001dcc commit efd469a

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

backends/asm/assemble_ir.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -945,13 +945,6 @@ PrintCompressCondJump(struct flexbuf *fb, int cond, Operand *dst)
945945
flexbuf_addstr(fb, "\n");
946946
}
947947

948-
// LMM jumps +- this amount are turned into add/sub of the pc
949-
// pick a conservative value
950-
// 127 would be the absolute maximum here
951-
#define MAX_REL_JUMP_OFFSET_LMM 100
952-
// On P2 conditional jumps have a range +/- 256
953-
#define MAX_REL_JUMP_OFFSET_P2 200
954-
955948
/* convert IR list into assembly language */
956949
static int didPub = 0;
957950

@@ -1230,10 +1223,14 @@ DoAssembleIR(struct flexbuf *fb, IR *ir, Module *P)
12301223
PrintCond(fb, ir->cond);
12311224
// if we know the destination we may be able to optimize
12321225
// the branch
1233-
if (ir->aux && gl_lmm_kind == LMM_KIND_ORIG && !(ir->flags & (FLAG_KEEP_INSTR|FLAG_JMPTABLE_INSTR))) {
1226+
if ((ir->flags & FLAG_RET_BRANCH) || (ir->aux && gl_lmm_kind == LMM_KIND_ORIG && !(ir->flags & (FLAG_KEEP_INSTR|FLAG_JMPTABLE_INSTR))) ) {
12341227
int offset;
1235-
dest = (IR *)ir->aux;
1236-
offset = dest->addr - ir->addr;
1228+
if (ir->aux) {
1229+
dest = (IR *)ir->aux;
1230+
offset = dest->addr - ir->addr;
1231+
} else {
1232+
offset = 1; /* fake, but we know it's a short forward branch */
1233+
}
12371234
if ( offset > 0 && offset < MAX_REL_JUMP_OFFSET_LMM) {
12381235
flexbuf_printf(fb, "add\t__pc, #4*(");
12391236
PrintOperand(fb, ir->dst);

backends/asm/optimize_ir.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,7 @@ OptimizeAddSub(IRList *irl)
33073307
// forward or backward
33083308
//
33093309
static void
3310-
AssignTemporaryAddresses(IRList *irl)
3310+
AssignTemporaryAddresses(IRList *irl, Operand *retlabel)
33113311
{
33123312
IR *ir;
33133313
unsigned addr = 0;
@@ -3323,6 +3323,16 @@ AssignTemporaryAddresses(IRList *irl)
33233323
ir->aux = NULL;
33243324
}
33253325
}
3326+
/* on P1, flag short branches to the return */
3327+
if (retlabel && !gl_p2) {
3328+
for (ir = irl->head; ir; ir = ir->next) {
3329+
if (IsJump(ir) && ir->dst == retlabel
3330+
&& (addr - ir->addr < MAX_REL_JUMP_OFFSET_LMM))
3331+
{
3332+
ir->flags |= FLAG_RET_BRANCH;
3333+
}
3334+
}
3335+
}
33263336
}
33273337

33283338
//
@@ -5348,7 +5358,7 @@ OptimizeIRLocal(IRList *irl, Function *f)
53485358
again:
53495359
do {
53505360
change = 0;
5351-
AssignTemporaryAddresses(irl);
5361+
AssignTemporaryAddresses(irl, FuncData(f)->asmreturnlabel);
53525362
if (flags & OPT_BASIC_REGS) {
53535363
OPT_PASS(CheckLabelUsage(irl));
53545364
OPT_PASS(OptimizeReadWrite(irl));

backends/asm/outasm.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Spin to Pasm converter
3-
* Copyright 2016-2024 Total Spectrum Software Inc.
3+
* Copyright 2016-2025 Total Spectrum Software Inc.
44
* PASM output routines
55
*/
66

@@ -11,6 +11,13 @@
1111
#include "becommon.h"
1212
#include "util/sha256.h"
1313

14+
// LMM jumps +- this amount are turned into add/sub of the pc
15+
// pick a conservative value
16+
// 127 would be the absolute maximum here
17+
#define MAX_REL_JUMP_OFFSET_LMM 100
18+
// On P2 conditional jumps have a range +/- 256
19+
#define MAX_REL_JUMP_OFFSET_P2 200
20+
1421
// functions for producing local identifiers
1522
const char *IdentifierLocalName(Function *func, const char *name);
1623
const char *IdentifierModuleName(Module *P, const char *name);

instr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ enum flags {
295295
FLAG_USER_FCACHE = 0x20000,
296296

297297
// rest of the bits are used by the optimizer
298-
FLAG_LABEL_USED = 0x100000,
299-
FLAG_INSTR_NEW = 0x200000,
298+
FLAG_LABEL_USED = 0x100000,
299+
FLAG_INSTR_NEW = 0x200000,
300+
FLAG_RET_BRANCH = 0x400000, /* for a short forward branch to return */
300301
FLAG_OPTIMIZER = 0xFFF00000,
301302
};
302303

0 commit comments

Comments
 (0)