libjit-sys 0.2.1

Just-In-Time Compilation in Rust using LibJIT bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
 * jit-live.c - Liveness analysis for function bodies.
 *
 * Copyright (C) 2004  Southern Storm Software, Pty Ltd.
 *
 * This file is part of the libjit library.
 *
 * The libjit library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * The libjit library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the libjit library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "jit-internal.h"
#include <jit/jit-dump.h>

#define USE_FORWARD_PROPAGATION 1
#define USE_BACKWARD_PROPAGATION 1

/*
 * Compute liveness information for a basic block.
 */
static void
compute_liveness_for_block(jit_block_t block)
{
	jit_insn_iter_t iter;
	jit_insn_t insn;
	jit_value_t dest;
	jit_value_t value1;
	jit_value_t value2;
	int flags;

	/* Scan backwards to compute the liveness flags */
	jit_insn_iter_init_last(&iter, block);
	while((insn = jit_insn_iter_previous(&iter)) != 0)
	{
		/* Skip NOP instructions, which may have arguments left
		   over from when the instruction was replaced, but which
		   are not relevant to our liveness analysis */
		if(insn->opcode == JIT_OP_NOP)
		{
			continue;
		}

		/* Fetch the value parameters to this instruction */
		flags = insn->flags;
		if((flags & JIT_INSN_DEST_OTHER_FLAGS) == 0)
		{
			dest = insn->dest;
			if(dest && dest->is_constant)
			{
				dest = 0;
			}
		}
		else
		{
			dest = 0;
		}
		if((flags & JIT_INSN_VALUE1_OTHER_FLAGS) == 0)
		{
			value1 = insn->value1;
			if(value1 && value1->is_constant)
			{
				value1 = 0;
			}
		}
		else
		{
			value1 = 0;
		}
		if((flags & JIT_INSN_VALUE2_OTHER_FLAGS) == 0)
		{
			value2 = insn->value2;
			if(value2 && value2->is_constant)
			{
				value2 = 0;
			}
		}
		else
		{
			value2 = 0;
		}

		/* Record the liveness information in the instruction flags */
		flags &= ~JIT_INSN_LIVENESS_FLAGS;
		if(dest)
		{
			if(dest->live)
			{
				flags |= JIT_INSN_DEST_LIVE;
			}
			if(dest->next_use)
			{
				flags |= JIT_INSN_DEST_NEXT_USE;
			}
		}
		if(value1)
		{
			if(value1->live)
			{
				flags |= JIT_INSN_VALUE1_LIVE;
			}
			if(value1->next_use)
			{
				flags |= JIT_INSN_VALUE1_NEXT_USE;
			}
		}
		if(value2)
		{
			if(value2->live)
			{
				flags |= JIT_INSN_VALUE2_LIVE;
			}
			if(value2->next_use)
			{
				flags |= JIT_INSN_VALUE2_NEXT_USE;
			}
		}
		insn->flags = (short)flags;

		/* Set the destination to "not live, no next use" */
		if(dest)
		{
			if((flags & JIT_INSN_DEST_IS_VALUE) == 0)
			{
				if(!(dest->next_use) && !(dest->live))
				{
					/* There is no next use of this value and it is not
					   live on exit from the block.  So we can discard
					   the entire instruction as it will have no effect */
#ifdef _JIT_COMPILE_DEBUG
					printf("liveness analysis: optimize away instruction '");
					jit_dump_insn(stdout, block->func, insn);
					printf("'\n");
#endif
					insn->opcode = (short)JIT_OP_NOP;
					continue;
				}
				dest->live = 0;
				dest->next_use = 0;
			}
			else
			{
				/* The destination is actually a source value for this
				   instruction (e.g. JIT_OP_STORE_RELATIVE_*) */
				dest->live = 1;
				dest->next_use = 1;
			}
		}

		/* Set value1 and value2 to "live, next use" */
		if(value1)
		{
			value1->live = 1;
			value1->next_use = 1;
		}
		if(value2)
		{
			value2->live = 1;
			value2->next_use = 1;
		}
	}
}

#if defined(USE_FORWARD_PROPAGATION) || defined(USE_BACKWARD_PROPAGATION)
/*
 * Check if the instruction is eligible for copy propagation.
 */
static int
is_copy_insn(jit_insn_t insn)
{
	jit_type_t dtype;
	jit_type_t vtype;

	if (!insn || !insn->dest || !insn->value1)
	{
		return 0;
	}

	switch(insn->opcode)
	{
	case JIT_OP_COPY_INT:
		/* Currently JIT_INSN_COPY_INT is used not only for int-to-int
		   copying but for byte-to-int and short-to-int copying too
		   (see jit_insn_convert). Propagation of byte and short values
		   to instructions that expect ints might confuse them. */
		dtype = jit_type_normalize(insn->dest->type);
		vtype = jit_type_normalize(insn->value1->type);
		if(dtype != vtype)
		{
			/* signed/unsigned int conversion should be safe */
			if((dtype->kind == JIT_TYPE_INT || dtype->kind == JIT_TYPE_UINT)
			   && (vtype->kind == JIT_TYPE_INT || vtype->kind == JIT_TYPE_UINT))
			{
				return 1;
			}
			return 0;
		}
		return 1;

	case JIT_OP_COPY_LOAD_SBYTE:
	case JIT_OP_COPY_LOAD_UBYTE:
	case JIT_OP_COPY_LOAD_SHORT:
	case JIT_OP_COPY_LOAD_USHORT:
	case JIT_OP_COPY_LONG:
	case JIT_OP_COPY_FLOAT32:
	case JIT_OP_COPY_FLOAT64:
	case JIT_OP_COPY_NFLOAT:
	case JIT_OP_COPY_STRUCT:
	case JIT_OP_COPY_STORE_BYTE:
	case JIT_OP_COPY_STORE_SHORT:
		return 1;
	}

	return 0;
}
#endif

#if USE_FORWARD_PROPAGATION
/*
 * Perform simple copy propagation within basic block. Replaces instructions
 * that look like this:
 *
 * i) t = x
 * ...
 * j) y = op(t)
 *
 * with the folowing:
 *
 * i) t = x
 * ...
 * j) y = op(x)
 *
 * If "t" is not used after the instruction "j" then further liveness analysis
 * may replace the instruction "i" with a noop:
 *
 * i) noop
 * ...
 * j) y = op(x)
 *
 * The propagation stops as soon as either "t" or "x" are changed (used as a
 * dest in a different instruction).
 */
static int
forward_propagation(jit_block_t block)
{
	int optimized;
	jit_insn_iter_t iter, iter2;
	jit_insn_t insn, insn2;
	jit_value_t dest, value;
	int flags2;

	optimized = 0;

	jit_insn_iter_init(&iter, block);
	while((insn = jit_insn_iter_next(&iter)) != 0)
	{
		if(!is_copy_insn(insn))
		{
			continue;
		}

		dest = insn->dest;
		value = insn->value1;

		/* Discard copy to itself */
		if(dest == value)
		{
#ifdef _JIT_COMPILE_DEBUG
			printf("forward copy propagation: optimize away copy to itself in '");
			jit_dump_insn(stdout, block->func, insn);
			printf("'\n");
#endif
			insn->opcode = (short)JIT_OP_NOP;
			optimized = 1;
			continue;
		}

		/* Not smart enough to tell when it is safe to optimize copying
		   to a value that is used in other basic blocks or may be
		   aliased. */
		if(!dest->is_temporary)
		{
			continue;
		}
		if(dest->is_addressable || dest->is_volatile)
		{
			continue;
		}
		if(value->is_addressable || value->is_volatile)
		{
			continue;
		}

		iter2 = iter;
		while((insn2 = jit_insn_iter_next(&iter2)) != 0)
		{
			/* Skip NOP instructions, which may have arguments left
			   over from when the instruction was replaced, but which
			   are not relevant to our analysis */
			if(insn->opcode == JIT_OP_NOP)
			{
				continue;
			}

			flags2 = insn2->flags;
			if((flags2 & JIT_INSN_DEST_OTHER_FLAGS) == 0)
			{
				if((flags2 & JIT_INSN_DEST_IS_VALUE) == 0)
				{
					if(insn2->dest == dest || insn2->dest == value)
					{
						break;
					}
				}
				else if(insn2->dest == dest)
				{
#ifdef _JIT_COMPILE_DEBUG
					printf("forward copy propagation: in '");
					jit_dump_insn(stdout, block->func, insn2);
					printf("' replace ");
					jit_dump_value(stdout, block->func, insn2->dest, 0);
					printf(" with ");
					jit_dump_value(stdout, block->func, value, 0);
					printf("'\n");
#endif
					insn2->dest = value;
					optimized = 1;
				}
			}
			if((flags2 & JIT_INSN_VALUE1_OTHER_FLAGS) == 0)
			{
				if(insn2->value1 == dest)
				{
#ifdef _JIT_COMPILE_DEBUG
					printf("forward copy propagation: in '");
					jit_dump_insn(stdout, block->func, insn2);
					printf("' replace ");
					jit_dump_value(stdout, block->func, insn2->value1, 0);
					printf(" with ");
					jit_dump_value(stdout, block->func, value, 0);
					printf("'\n");
#endif
					insn2->value1 = value;
					optimized = 1;
				}
			}
			if((flags2 & JIT_INSN_VALUE2_OTHER_FLAGS) == 0)
			{
				if(insn2->value2 == dest)
				{
#ifdef _JIT_COMPILE_DEBUG
					printf("forward copy propagation: in '");
					jit_dump_insn(stdout, block->func, insn2);
					printf("' replace ");
					jit_dump_value(stdout, block->func, insn2->value2, 0);
					printf(" with ");
					jit_dump_value(stdout, block->func, value, 0);
					printf("'\n");
#endif
					insn2->value2 = value;
					optimized = 1;
				}
			}
		}
	}

	return optimized;
}
#endif

#ifdef USE_BACKWARD_PROPAGATION
/*
 * Perform simple copy propagation within basic block for the case when a
 * temporary value is stored to another value. This replaces instructions
 * that look like this:
 *
 * i) t = op(x)
 * ...
 * j) y = t
 *
 * with the following
 *
 * i) y = op(x)
 * ...
 * j) noop
 *
 * This is only allowed if "t" is used only in the instructions "i" and "j"
 * and "y" is not used between "i" and "j" (but can be used after "j").
 */
static int
backward_propagation(jit_block_t block)
{
	int optimized;
	jit_insn_iter_t iter, iter2;
	jit_insn_t insn, insn2;
	jit_value_t dest, value;
	int flags2;

	optimized = 0;

	jit_insn_iter_init_last(&iter, block);
	while((insn = jit_insn_iter_previous(&iter)) != 0)
	{
		if(!is_copy_insn(insn))
		{
			continue;
		}

		dest = insn->dest;
		value = insn->value1;

		/* Discard copy to itself */
		if(dest == value)
		{
#ifdef _JIT_COMPILE_DEBUG
			printf("backward copy propagation: optimize away copy to itself in '");
			jit_dump_insn(stdout, block->func, insn);
			printf("'\n");
#endif
			insn->opcode = (short)JIT_OP_NOP;
			optimized = 1;
			continue;
		}

		/* "value" is used afterwards so we cannot eliminate it here */
		if((insn->flags & (JIT_INSN_VALUE1_LIVE | JIT_INSN_VALUE1_NEXT_USE)) != 0)
		{
			continue;
		}

		if(dest->is_addressable || dest->is_volatile)
		{
			continue;
		}
		if(value->is_addressable || value->is_volatile)
		{
			continue;
		}

		iter2 = iter;
		while((insn2 = jit_insn_iter_previous(&iter2)) != 0)
		{
			/* Skip NOP instructions, which may have arguments left
			   over from when the instruction was replaced, but which
			   are not relevant to our analysis */
			if(insn->opcode == JIT_OP_NOP)
			{
				continue;
			}

			flags2 = insn2->flags;
			if((flags2 & JIT_INSN_DEST_OTHER_FLAGS) == 0)
			{
				if(insn2->dest == dest)
				{
					break;
				}
				if(insn2->dest == value)
				{
					if((flags2 & JIT_INSN_DEST_IS_VALUE) == 0)
					{
#ifdef _JIT_COMPILE_DEBUG
						printf("backward copy propagation: in '");
						jit_dump_insn(stdout, block->func, insn2);
						printf("' replace ");
						jit_dump_value(stdout, block->func, insn2->dest, 0);
						printf(" with ");
						jit_dump_value(stdout, block->func, dest, 0);
						printf(" and optimize away '");
						jit_dump_insn(stdout, block->func, insn);
						printf("'\n");
#endif
						insn->opcode = (short)JIT_OP_NOP;
						insn2->dest = dest;
						optimized = 1;
					}
					break;
				}
			}
			if((flags2 & JIT_INSN_VALUE1_OTHER_FLAGS) == 0)
			{
				if(insn2->value1 == dest || insn2->value1 == value)
				{
					break;
				}
			}
			if((flags2 & JIT_INSN_VALUE2_OTHER_FLAGS) == 0)
			{
				if(insn2->value2 == dest || insn2->value1 == value)
				{
					break;
				}
			}
		}
	}

	return optimized;
}
#endif

/* Reset value liveness flags. */
static void
reset_value_liveness(jit_value_t value)
{
	if(value)
	{
		if (!value->is_constant && !value->is_temporary)
		{
			value->live = 1;
		}
		else
		{
			value->live = 0;
		}
		value->next_use = 0;
	}
}

/*
 * Re-scan the block to reset the liveness flags on all non-temporaries
 * because we need them in the original state for the next block.
 */
static void
reset_liveness_flags(jit_block_t block, int reset_all)
{
	jit_insn_iter_t iter;
	jit_insn_t insn;
	int flags;

	jit_insn_iter_init(&iter, block);
	while((insn = jit_insn_iter_next(&iter)) != 0)
	{
		flags = insn->flags;
		if((flags & JIT_INSN_DEST_OTHER_FLAGS) == 0)
		{
			reset_value_liveness(insn->dest);
		}
		if((flags & JIT_INSN_VALUE1_OTHER_FLAGS) == 0)
		{
			reset_value_liveness(insn->value1);
		}
		if((flags & JIT_INSN_VALUE2_OTHER_FLAGS) == 0)
		{
			reset_value_liveness(insn->value2);
		}
		if(reset_all)
		{
			flags &= ~(JIT_INSN_DEST_LIVE | JIT_INSN_DEST_NEXT_USE
				   |JIT_INSN_VALUE1_LIVE | JIT_INSN_VALUE1_NEXT_USE
				   |JIT_INSN_VALUE2_LIVE | JIT_INSN_VALUE2_NEXT_USE);
		}
	}
}

void _jit_function_compute_liveness(jit_function_t func)
{
	jit_block_t block = func->builder->entry_block;
	while(block != 0)
	{
#ifdef USE_FORWARD_PROPAGATION
		/* Perform forward copy propagation for the block */
		forward_propagation(block);
#endif

		/* Reset the liveness flags for the next block */
		reset_liveness_flags(block, 0);

		/* Compute the liveness flags for the block */
		compute_liveness_for_block(block);

#ifdef USE_BACKWARD_PROPAGATION
		/* Perform backward copy propagation for the block */
		if(backward_propagation(block))
		{
			/* Reset the liveness flags and compute them again */
			reset_liveness_flags(block, 1);
			compute_liveness_for_block(block);
		}
#endif

		/* Move on to the next block in the function */
		block = block->next;
	}
}