xdy 0.9.0

Complex RPG dice expression evaluator with histogram support.
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
//! # Common subexpression elimination
//!
//! Common subexpression elimination (CSE) is an optimization that eliminates
//! redundant computations, based on the observation that an expression that
//! has been previously computed previously can simply be reused. CSE requires
//! the function to be in static single assignment (SSA) form.

use std::collections::{HashMap, hash_map::Entry};

use crate::{
	Add, AddressingMode, CanAllocate, CanVisitInstructions as _, Div,
	DropHighest, DropLowest, Exp, Function, Instruction, InstructionVisitor,
	Mod, Mul, Neg, RegisterIndex, Return, RollCustomDice, RollRange,
	RollStandardDice, Sub, SumRollingRecord
};

use super::Optimizer;

////////////////////////////////////////////////////////////////////////////////
//                     Common subexpression elimination.                      //
////////////////////////////////////////////////////////////////////////////////

/// A common subexpression eliminator. The standard optimizer applies this
/// pass to a [function](Function) to eliminate common subexpressions. The
/// eliminator works by replacing eligible duplicate instructions with the
/// destination register of the original instruction. Note that dice
/// instructions cannot be eliminated, as they are not pure.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CommonSubexpressionEliminator
{
	/// The scrubbed original instructions, as a map from scrubbed instructions
	/// to their destination registers. Instruction scrubbing involves
	/// replacing its destination register with a sentinel value, so that the
	/// instruction can be compared for equality with another scrubbed
	/// instruction. When a common subexpression is found, the redundant
	/// instruction is replaced with the destination register of the original
	/// instruction.
	originals: HashMap<Instruction, RegisterIndex>,

	/// Replacements for registers, due to renumbering or instruction
	/// elimination, as a map from originals to replacements. The keys are
	/// relative to the original function, and the values are relative to the
	/// optimized function.
	replacements: HashMap<AddressingMode, AddressingMode>,

	/// The next register to allocate.
	next_register: RegisterIndex,

	/// The replacement instructions.
	instructions: Vec<Instruction>
}

impl Optimizer<()> for CommonSubexpressionEliminator
{
	fn optimize(mut self, mut function: Function) -> Result<Function, ()>
	{
		// Skip over the parameters and externals when initializing the register
		// allocator. Don't bother to install them in the replacement map, as
		// they cannot be altered.
		let start_register =
			function.parameters.len() + function.externals.len();
		loop
		{
			// Reset the optimizer state.
			self.originals.clear();
			self.replacements.clear();
			self.next_register = RegisterIndex(start_register);
			self.instructions.clear();
			// Visit each instruction in the function body.
			for instruction in &function.instructions
			{
				instruction.visit(&mut self).unwrap();
			}
			// Compare the old and new instructions. If they are the same, we
			// have reached a fixed point and can return the optimized function.
			if function.instructions == self.instructions
			{
				return Ok(function);
			}
			// Update the function to reflect the new instructions.
			function.register_count = self.next_register.0;
			function.instructions = self.instructions.clone();
		}
	}
}

impl InstructionVisitor<()> for CommonSubexpressionEliminator
{
	fn visit_roll_range(&mut self, range: &crate::RollRange) -> Result<(), ()>
	{
		// Range instructions are not pure, so they cannot be eliminated. The
		// rolling records are not affected by this optimization, so we don't
		// need to allocate replacements for them.
		self.emit(RollRange {
			dest: range.dest,
			start: self.replacement(range.start),
			end: self.replacement(range.end)
		});
		Ok(())
	}

	fn visit_roll_standard_dice(
		&mut self,
		roll: &RollStandardDice
	) -> Result<(), ()>
	{
		// Dice instructions are not pure, so they cannot be eliminated. The
		// rolling records are not affected by this optimization, so we don't
		// need to allocate replacements for them.
		self.emit(RollStandardDice {
			dest: roll.dest,
			count: self.replacement(roll.count),
			faces: self.replacement(roll.faces)
		});
		Ok(())
	}

	fn visit_roll_custom_dice(
		&mut self,
		roll: &RollCustomDice
	) -> Result<(), ()>
	{
		// Dice instructions are not pure, so they cannot be eliminated. The
		// rolling records are not affected by this optimization, so we don't
		// need to allocate replacements for them.
		self.emit(RollCustomDice {
			dest: roll.dest,
			count: self.replacement(roll.count),
			faces: roll.faces.clone()
		});
		Ok(())
	}

	fn visit_drop_lowest(&mut self, drop: &DropLowest) -> Result<(), ()>
	{
		// Dice instructions are not pure, so they cannot be eliminated. The
		// rolling records are not affected by this optimization, so we don't
		// need to allocate replacements for them.
		self.emit(DropLowest {
			dest: drop.dest,
			count: self.replacement(drop.count)
		});
		Ok(())
	}

	fn visit_drop_highest(&mut self, drop: &DropHighest) -> Result<(), ()>
	{
		// Dice instructions are not pure, so they cannot be eliminated. The
		// rolling records are not affected by this optimization, so we don't
		// need to allocate replacements for them.
		self.emit(DropHighest {
			dest: drop.dest,
			count: self.replacement(drop.count)
		});
		Ok(())
	}

	fn visit_sum_rolling_record(
		&mut self,
		sum: &SumRollingRecord
	) -> Result<(), ()>
	{
		// The compiler currently cannot produce duplicate SumRollingRecord
		// instructions, so this is future-proofing only.
		if let (dest, true) = self.canonicalize(*sum)
		{
			// This is the original instruction, so we need to emit it after
			// replacing the destination register.
			self.emit(SumRollingRecord { dest, src: sum.src });
		}
		Ok(())
	}

	fn visit_add(&mut self, inst: &Add) -> Result<(), ()>
	{
		self.canonicalize_binary_op(
			*inst,
			|| (inst.op1, inst.op2),
			|dest, op1, op2| Add { dest, op1, op2 }
		)
	}

	fn visit_sub(&mut self, inst: &Sub) -> Result<(), ()>
	{
		self.canonicalize_binary_op(
			*inst,
			|| (inst.op1, inst.op2),
			|dest, op1, op2| Sub { dest, op1, op2 }
		)
	}

	fn visit_mul(&mut self, inst: &Mul) -> Result<(), ()>
	{
		self.canonicalize_binary_op(
			*inst,
			|| (inst.op1, inst.op2),
			|dest, op1, op2| Mul { dest, op1, op2 }
		)
	}

	fn visit_div(&mut self, inst: &Div) -> Result<(), ()>
	{
		self.canonicalize_binary_op(
			*inst,
			|| (inst.op1, inst.op2),
			|dest, op1, op2| Div { dest, op1, op2 }
		)
	}

	fn visit_mod(&mut self, inst: &Mod) -> Result<(), ()>
	{
		self.canonicalize_binary_op(
			*inst,
			|| (inst.op1, inst.op2),
			|dest, op1, op2| Mod { dest, op1, op2 }
		)
	}

	fn visit_exp(&mut self, inst: &Exp) -> Result<(), ()>
	{
		self.canonicalize_binary_op(
			*inst,
			|| (inst.op1, inst.op2),
			|dest, op1, op2| Exp { dest, op1, op2 }
		)
	}

	fn visit_neg(&mut self, inst: &Neg) -> Result<(), ()>
	{
		if let (dest, true) = self.canonicalize(*inst)
		{
			self.emit(Neg {
				dest,
				op: self.replacement(inst.op)
			});
		}
		Ok(())
	}

	fn visit_return(&mut self, inst: &Return) -> Result<(), ()>
	{
		self.emit(Return {
			src: self.replacement(inst.src)
		});
		Ok(())
	}
}

impl CommonSubexpressionEliminator
{
	/// Look up the replacement of the specified operand, falling back to the
	/// operand itself if no replacement is found.
	///
	/// # Parameters
	/// - `op`: The operand to look up.
	///
	/// # Returns
	/// The replacement for the operand.
	#[inline]
	fn replacement(&self, op: impl Into<AddressingMode>) -> AddressingMode
	{
		let op: AddressingMode = op.into();
		*self.replacements.get(&op).unwrap_or(&op)
	}

	/// Canonicalize the specified instruction.
	///
	/// # Parameters
	/// - `inst`: The instruction to canonicalize.
	///
	/// # Returns
	/// A 2-tuple comprising (1) the canonical destination register for the
	/// instruction and (2) whether the instruction holds the first occurrence
	/// of the canonical destination register.
	fn canonicalize(
		&mut self,
		inst: impl Into<Instruction>
	) -> (RegisterIndex, bool)
	{
		let inst = inst.into();
		match self.originals.entry(inst.scrub())
		{
			Entry::Occupied(entry) =>
			{
				let dest = *entry.get();
				self.replacements
					.insert(inst.destination().unwrap(), dest.into());
				(dest, false)
			},
			Entry::Vacant(entry) =>
			{
				let dest = self.next_register.allocate();
				self.replacements
					.insert(inst.destination().unwrap(), dest.into());
				(*entry.insert(dest), true)
			}
		}
	}

	/// Canonicalize the specified binary operation, emitting a new
	/// instruction if this is the first occurrence of the canonical
	/// destination register.
	///
	/// # Type Parameters
	/// - `I`: The type of the new instruction to emit.
	///
	/// # Parameters
	/// - `inst`: The instruction to canonicalize.
	/// - `extractor`: A closure that extracts the two source operands from the
	///   instruction.
	/// - `constructor`: A closure that constructs a new instruction with the
	///   given canonical destination register and source operands.
	fn canonicalize_binary_op<I>(
		&mut self,
		inst: impl Into<Instruction>,
		extractor: impl Fn() -> (AddressingMode, AddressingMode),
		constructor: impl Fn(RegisterIndex, AddressingMode, AddressingMode) -> I
	) -> Result<(), ()>
	where
		I: Into<Instruction>
	{
		if let (dest, true) = self.canonicalize(inst)
		{
			// This is the original instruction, so we need to emit it after
			// replacing the destination register.
			let (op1, op2) = extractor();
			self.instructions.push(
				constructor(
					dest,
					self.replacements.get(&op1).copied().unwrap_or(op1),
					self.replacements.get(&op2).copied().unwrap_or(op2)
				)
				.into()
			);
		}
		Ok(())
	}

	/// Emit the specified instruction.
	///
	/// # Parameters
	/// - `inst`: The instruction to emit.
	#[inline]
	fn emit(&mut self, inst: impl Into<Instruction>)
	{
		self.instructions.push(inst.into());
	}
}

/// Provides the capability to scrub the destination register of an
/// [instruction](Instruction).
trait CanScrub
{
	/// Scrub the destination register of the instruction, so that it can be
	/// compared for equality with another instruction for the purpose of
	/// identifying common subexpressions.
	///
	/// # Returns
	/// A scrubbed variant of the receiver.
	fn scrub(&self) -> Self;
}

impl CanScrub for RollRange
{
	fn scrub(&self) -> Self
	{
		// The destination is a rolling record, so it unaffected.
		RollRange {
			dest: self.dest,
			start: self.start,
			end: self.end
		}
	}
}

impl CanScrub for RollStandardDice
{
	fn scrub(&self) -> Self
	{
		// The destination is a rolling record, so it unaffected.
		RollStandardDice {
			dest: self.dest,
			count: self.count,
			faces: self.faces
		}
	}
}

impl CanScrub for RollCustomDice
{
	fn scrub(&self) -> Self
	{
		// The destination is a rolling record, so it unaffected.
		RollCustomDice {
			dest: self.dest,
			count: self.count,
			faces: self.faces.clone()
		}
	}
}

impl CanScrub for DropLowest
{
	fn scrub(&self) -> Self
	{
		// The destination is a rolling record, so it unaffected.
		DropLowest {
			dest: self.dest,
			count: self.count
		}
	}
}

impl CanScrub for DropHighest
{
	fn scrub(&self) -> Self
	{
		// The destination is a rolling record, so it unaffected.
		DropHighest {
			dest: self.dest,
			count: self.count
		}
	}
}

impl CanScrub for SumRollingRecord
{
	fn scrub(&self) -> Self
	{
		SumRollingRecord {
			dest: SCRUBBED_DEST,
			src: self.src
		}
	}
}

impl CanScrub for Add
{
	fn scrub(&self) -> Self
	{
		Add {
			dest: SCRUBBED_DEST,
			op1: self.op1,
			op2: self.op2
		}
	}
}

impl CanScrub for Sub
{
	fn scrub(&self) -> Self
	{
		Sub {
			dest: SCRUBBED_DEST,
			op1: self.op1,
			op2: self.op2
		}
	}
}

impl CanScrub for Mul
{
	fn scrub(&self) -> Self
	{
		Mul {
			dest: SCRUBBED_DEST,
			op1: self.op1,
			op2: self.op2
		}
	}
}

impl CanScrub for Div
{
	fn scrub(&self) -> Self
	{
		Div {
			dest: SCRUBBED_DEST,
			op1: self.op1,
			op2: self.op2
		}
	}
}

impl CanScrub for Mod
{
	fn scrub(&self) -> Self
	{
		Mod {
			dest: SCRUBBED_DEST,
			op1: self.op1,
			op2: self.op2
		}
	}
}

impl CanScrub for Exp
{
	fn scrub(&self) -> Self
	{
		Exp {
			dest: SCRUBBED_DEST,
			op1: self.op1,
			op2: self.op2
		}
	}
}

impl CanScrub for Neg
{
	fn scrub(&self) -> Self
	{
		Neg {
			dest: SCRUBBED_DEST,
			op: self.op
		}
	}
}

impl CanScrub for Return
{
	fn scrub(&self) -> Self { Return { src: self.src } }
}

impl CanScrub for Instruction
{
	fn scrub(&self) -> Self
	{
		match self
		{
			Instruction::RollRange(inst) => inst.scrub().into(),
			Instruction::RollStandardDice(inst) => inst.scrub().into(),
			Instruction::RollCustomDice(inst) => inst.scrub().into(),
			Instruction::DropLowest(inst) => inst.scrub().into(),
			Instruction::DropHighest(inst) => inst.scrub().into(),
			Instruction::SumRollingRecord(inst) => inst.scrub().into(),
			Instruction::Add(inst) => inst.scrub().into(),
			Instruction::Sub(inst) => inst.scrub().into(),
			Instruction::Mul(inst) => inst.scrub().into(),
			Instruction::Div(inst) => inst.scrub().into(),
			Instruction::Mod(inst) => inst.scrub().into(),
			Instruction::Exp(inst) => inst.scrub().into(),
			Instruction::Neg(inst) => inst.scrub().into(),
			Instruction::Return(inst) => inst.scrub().into()
		}
	}
}

/// The sentinel value used to indicate that a register index has been scrubbed.
/// The value is in-band, and so very technically a valid register index, but
/// reality dictates that no real function will have over two billion registers.
const SCRUBBED_DEST: RegisterIndex = RegisterIndex(usize::MAX);