aboutsummaryrefslogtreecommitdiff
path: root/examples/opt_test.zc
blob: c58128380fd0079b861ddc5f293a74c3e3b516df (plain) (blame)
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
// a tour of the compiler's optimization hints. the program is valid and runs;
// the hints only show up in the editor (or the lsp cli), never in a normal build.
// each function below triggers one of them.

fn sink(.d p) { free(d*p); return 0; }

// a variable that is written and never read
fn dead() {
	let unused = 5;
	return 1;
}

// never reassigned, so it could be a const
fn constant() {
	let width = 40;
	return width + 2;
}

// multiplying by a power of two is a shift
fn shifty(n) { return n * 8; }

// a number with no name
fn magic(n) { return n + 1234; }

// new inside a loop allocates every pass
fn looping() {
	let i = 0;
	while (i < 3) {
		let buf = new(10);
		buf[0] = i;
		sink(d*buf);
		i++;
	}
	return i;
}

// a small buffer that never leaves the function can live on the stack
fn scratch() {
	let buf = new(8);
	buf[0] = 7;
	return buf[0];
}

// a consuming call takes the object after its allocation
fn handoff() {
	let tmp = new(4);
	sink(d*tmp);
	return 0;
}

fn main() {
	put_n(dead() + constant() + shifty(2) + magic(0) + looping() + scratch() + handoff()); put_c(10);
	return 0;
}