Skip to content

Commit 84798f9

Browse files
committed
Add new -Oorder-trampolines optimization.
This attempts to order trampolines as per the instruction ordering. When combined with -Ojump-peephole, many jumps can be eliminated, which may result in a nice performance boost.
1 parent 16a6e89 commit 84798f9

4 files changed

Lines changed: 55 additions & 30 deletions

File tree

src/e9patch/e9alloc.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ RB_GENERATE_STATIC(Tree, Node, entry, compare);
9090

9191
#define FLAG_LB 0x1
9292
#define FLAG_UB 0x2
93-
#define FLAG_SAME_PAGE 0x4
93+
#define FLAG_RIGHT 0x4
94+
#define FLAG_SAME_PAGE 0x8
9495

9596
#define flag_set(flags, flag, val) \
9697
((val)? (flags) | (flag): (flags) & ~(flag))
@@ -118,7 +119,8 @@ static Node *alloc()
118119
static Node *node(Node *parent, intptr_t lb, intptr_t ub, size_t size,
119120
uint32_t flags)
120121
{
121-
bool alloc_left = ((flags & FLAG_LB) != 0 || (flags & FLAG_UB) == 0);
122+
bool alloc_left = ((flags & FLAG_RIGHT) != 0? false:
123+
((flags & FLAG_LB) != 0 || (flags & FLAG_UB) == 0));
122124
intptr_t LB, UB;
123125
if (alloc_left)
124126
{
@@ -260,7 +262,12 @@ const Alloc *allocate(Allocator &allocator, intptr_t lb, intptr_t ub,
260262
size_t size = (size_t)r;
261263
ub += size;
262264
uint32_t flags = (same_page? FLAG_SAME_PAGE: 0);
263-
Node *n = insert(allocator.tree.root, lb, ub, size, flags);
265+
Node *n = nullptr;
266+
const intptr_t target = 0x70000000;
267+
if (option_Oorder_trampolines && ub > target)
268+
n = insert(allocator.tree.root, lb, target, size, flags | FLAG_RIGHT);
269+
if (n == nullptr)
270+
n = insert(allocator.tree.root, lb, ub, size, flags);
264271
if (n == nullptr)
265272
return nullptr;
266273
if (allocator.tree.root == nullptr)

src/e9patch/e9patch.cpp

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool option_tactic_backward_T3 = true;
4040
unsigned option_Ojump_elim = 0;
4141
unsigned option_Ojump_elim_size = 64;
4242
bool option_Ojump_peephole = true;
43+
bool option_Oorder_trampolines = false;
4344
bool option_Oscratch_stack = false;
4445
intptr_t option_mem_lb = INTPTR_MIN;
4546
intptr_t option_mem_ub = INTPTR_MAX;
@@ -120,33 +121,6 @@ void debugImpl(const char *msg, ...)
120121
putc('\n', stderr);
121122
}
122123

123-
/*
124-
* Options.
125-
*/
126-
enum Option
127-
{
128-
OPTION_DEBUG,
129-
OPTION_HELP,
130-
OPTION_INPUT,
131-
OPTION_MEM_LB,
132-
OPTION_MEM_MAPPING_SIZE,
133-
OPTION_MEM_MULTI_PAGE,
134-
OPTION_MEM_UB,
135-
OPTION_OJUMP_ELIM,
136-
OPTION_OJUMP_ELIM_SIZE,
137-
OPTION_OJUMP_PEEPHOLE,
138-
OPTION_OSCRATCH_STACK,
139-
OPTION_OUTPUT,
140-
OPTION_STATIC_LOADER,
141-
OPTION_TACTIC_B1,
142-
OPTION_TACTIC_B2,
143-
OPTION_TACTIC_T1,
144-
OPTION_TACTIC_T2,
145-
OPTION_TACTIC_T3,
146-
OPTION_TACTIC_BACKWARD_T3,
147-
OPTION_TRAP_ALL,
148-
};
149-
150124
/*
151125
* Parse an integer from an optarg.
152126
*/
@@ -208,6 +182,12 @@ static void usage(FILE *stream, const char *progname)
208182
"\t\tEnables [disables] jump-from-trampoline peephole optimization.\n"
209183
"\t\tDefault: true (enabled)\n"
210184
"\n"
185+
"\t-Oorder-trampolines[=false]\n"
186+
"\t\tEnables [disables] the ordering of trampolines with respect\n"
187+
"\t\tto the original instruction ordering (as much as is possible).\n"
188+
"\t\tThis can boost -Ojump-peephole.\n"
189+
"\t\tDefault: false (disabled)\n"
190+
"\n"
211191
"\t-Oscratch-stack[=false]\n"
212192
"\t\tAllow the stack to be used as scratch space. This allows\n"
213193
"\t\tfaster code to be emitted, but may break transparency.\n"
@@ -271,6 +251,34 @@ static void usage(FILE *stream, const char *progname)
271251
"\n", progname, PAGE_SIZE, PAGE_SIZE);
272252
}
273253

254+
/*
255+
* Options.
256+
*/
257+
enum Option
258+
{
259+
OPTION_DEBUG,
260+
OPTION_HELP,
261+
OPTION_INPUT,
262+
OPTION_MEM_LB,
263+
OPTION_MEM_MAPPING_SIZE,
264+
OPTION_MEM_MULTI_PAGE,
265+
OPTION_MEM_UB,
266+
OPTION_OJUMP_ELIM,
267+
OPTION_OJUMP_ELIM_SIZE,
268+
OPTION_OJUMP_PEEPHOLE,
269+
OPTION_OORDER_TRAMPOLINES,
270+
OPTION_OSCRATCH_STACK,
271+
OPTION_OUTPUT,
272+
OPTION_STATIC_LOADER,
273+
OPTION_TACTIC_B1,
274+
OPTION_TACTIC_B2,
275+
OPTION_TACTIC_T1,
276+
OPTION_TACTIC_T2,
277+
OPTION_TACTIC_T3,
278+
OPTION_TACTIC_BACKWARD_T3,
279+
OPTION_TRAP_ALL,
280+
};
281+
274282
/*
275283
* Parse options.
276284
*/
@@ -283,6 +291,7 @@ void parseOptions(int argc, char * const argv[], bool api)
283291
{"Ojump-elim", req_arg, nullptr, OPTION_OJUMP_ELIM},
284292
{"Ojump-elim-size", req_arg, nullptr, OPTION_OJUMP_ELIM_SIZE},
285293
{"Ojump-peephole", opt_arg, nullptr, OPTION_OJUMP_PEEPHOLE},
294+
{"Oorder-trampolines", opt_arg, nullptr, OPTION_OORDER_TRAMPOLINES},
286295
{"Oscratch-stack", opt_arg, nullptr, OPTION_OSCRATCH_STACK},
287296
{"debug", no_arg, nullptr, OPTION_DEBUG},
288297
{"help", no_arg, nullptr, OPTION_HELP},
@@ -347,6 +356,10 @@ void parseOptions(int argc, char * const argv[], bool api)
347356
option_Ojump_peephole =
348357
parseBoolOptArg("-Ojump-peephole", optarg);
349358
break;
359+
case OPTION_OORDER_TRAMPOLINES:
360+
option_Oorder_trampolines =
361+
parseBoolOptArg("-Oorder-trampolines", optarg);
362+
break;
350363
case OPTION_OSCRATCH_STACK:
351364
option_Oscratch_stack =
352365
parseBoolOptArg("-Oscratch-stack", optarg);

src/e9patch/e9patch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ extern bool option_debug;
365365
extern unsigned option_Ojump_elim;
366366
extern unsigned option_Ojump_elim_size;
367367
extern bool option_Ojump_peephole;
368+
extern bool option_Oorder_trampolines;
368369
extern bool option_Oscratch_stack;
369370
extern bool option_tactic_B1;
370371
extern bool option_tactic_B2;

src/e9tool/e9tool.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,23 +2333,27 @@ int main(int argc, char **argv)
23332333
options.push_back("-Ojump-elim=0");
23342334
options.push_back("-Ojump-elim-size=0");
23352335
options.push_back("-Ojump-peephole=false");
2336+
options.push_back("-Oorder-trampolines=false");
23362337
options.push_back("-Oscratch-stack=false");
23372338
break;
23382339
default: case 1:
23392340
options.push_back("-Ojump-elim=0");
23402341
options.push_back("-Ojump-elim-size=0");
2342+
options.push_back("-Oorder-trampolines=false");
23412343
options.push_back("-Ojump-peephole=true");
23422344
options.push_back("-Oscratch-stack=true");
23432345
break;
23442346
case 2:
23452347
options.push_back("-Ojump-elim=32");
23462348
options.push_back("-Ojump-elim-size=64");
2349+
options.push_back("-Oorder-trampolines=true");
23472350
options.push_back("-Ojump-peephole=true");
23482351
options.push_back("-Oscratch-stack=true");
23492352
break;
23502353
case 3:
23512354
options.push_back("-Ojump-elim=64");
23522355
options.push_back("-Ojump-elim-size=512");
2356+
options.push_back("-Oorder-trampolines=true");
23532357
options.push_back("-Ojump-peephole=true");
23542358
options.push_back("-Oscratch-stack=true");
23552359
break;

0 commit comments

Comments
 (0)