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
//! # Commutation
//!
//! Commutative operations can reorder their operands without changing the
//! meaning or result. Both addition and multiplication are commutative, so
//! reordering the operands can create additional opportunities for folding
//! constants and reducing strength. This pass reorders the operands of
//! commutative operations so that immediates appear before registers. The
//! constant commuter requires the function to be in static single assignment
//! (SSA) form.

use std::{
	cmp::{max, min},
	collections::{BTreeSet, HashMap}
};

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

////////////////////////////////////////////////////////////////////////////////
//                                Commutation.                                //
////////////////////////////////////////////////////////////////////////////////

/// A commuter that shuffles the immediate operands of commutative instructions
/// to the front in order to simplify constant folding and strength reduction.
/// The standard optimizer applies this pass to a [function](crate::Function)
/// before folding constants or reducing strength.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstantCommuter<'inst>
{
	/// The instructions to organize.
	instructions: &'inst [Instruction],

	/// The dependency analyzer.
	analyzer: DependencyAnalyzer<'inst>,

	/// An arrangement of the instructions of a function body into commutative
	/// groups. Each instruction is branded with a group number, and all
	/// instructions sharing the same group number are commutative with respect
	/// to each other.
	groups: HashMap<Instruction, usize>,

	/// The next group number to assign.
	next_group: usize,

	/// Replacement instructions, indexed by original instructions.
	replacements: HashMap<Instruction, Instruction>
}

impl<'inst> ConstantCommuter<'inst>
{
	/// Construct a new instruction organizer.
	///
	/// # Parameters
	/// - `instructions`: The instructions to organize.
	///
	/// # Returns
	/// The new instruction organizer.
	pub fn commute(instructions: &'inst [Instruction]) -> Vec<Instruction>
	{
		let mut commuter = Self {
			instructions,
			analyzer: DependencyAnalyzer::analyze(instructions),
			groups: HashMap::new(),
			next_group: 0,
			replacements: HashMap::new()
		};
		// Visit each instruction in the function body to organize them into
		// commutative groups.
		for instruction in instructions
		{
			instruction.visit(&mut commuter).unwrap();
		}
		// For each commutative group, rewrite the instructions to shuffle their
		// immediate operands to the front of the group. Use the first
		// instruction of a group to classify the type of the group.
		let groups = commuter.groups.values().copied().collect::<BTreeSet<_>>();
		for group in groups
		{
			let representative = commuter
				.instructions
				.iter()
				.find(|inst| commuter.groups.get(inst) == Some(&group))
				.unwrap();
			match representative
			{
				Instruction::DropLowest(_) => commuter
					.rewrite_commutative_group(group, |dest, srcs| {
						DropLowest {
							dest: dest.try_into().unwrap(),
							count: srcs[0]
						}
					}),
				Instruction::DropHighest(_) => commuter
					.rewrite_commutative_group(group, |dest, srcs| {
						DropHighest {
							dest: dest.try_into().unwrap(),
							count: srcs[0]
						}
					}),
				Instruction::Add(_) =>
				{
					commuter.rewrite_commutative_group(group, |dest, srcs| {
						Add {
							dest: dest.try_into().unwrap(),
							op1: srcs[0],
							op2: srcs[1]
						}
					})
				},
				Instruction::Sub(_) => commuter
					.rewrite_nearly_commutative_group(
						group,
						|dest, srcs| Add {
							dest: dest.try_into().unwrap(),
							op1: srcs[0],
							op2: srcs[1]
						},
						|dest, srcs| Sub {
							dest: dest.try_into().unwrap(),
							op1: srcs[0],
							op2: srcs[1]
						}
					),
				Instruction::Mul(_) =>
				{
					commuter.rewrite_commutative_group(group, |dest, srcs| {
						Mul {
							dest: dest.try_into().unwrap(),
							op1: srcs[0],
							op2: srcs[1]
						}
					})
				},
				Instruction::Div(_) => commuter
					.rewrite_nearly_commutative_group(
						group,
						|dest, srcs| Mul {
							dest: dest.try_into().unwrap(),
							op1: srcs[0],
							op2: srcs[1]
						},
						|dest, srcs| Div {
							dest: dest.try_into().unwrap(),
							op1: srcs[0],
							op2: srcs[1]
						}
					),
				_ =>
				{}
			}
		}
		// Answer the replacement function body.
		instructions
			.iter()
			.map(|inst| commuter.replacements.get(inst).unwrap_or(inst))
			.cloned()
			.collect()
	}
}

impl InstructionVisitor<()> for ConstantCommuter<'_>
{
	fn visit_roll_range(&mut self, _inst: &RollRange) -> Result<(), ()>
	{
		Ok(())
	}

	fn visit_roll_standard_dice(
		&mut self,
		_inst: &RollStandardDice
	) -> Result<(), ()>
	{
		Ok(())
	}

	fn visit_roll_custom_dice(
		&mut self,
		_inst: &RollCustomDice
	) -> Result<(), ()>
	{
		Ok(())
	}

	fn visit_drop_lowest(&mut self, _inst: &DropLowest) -> Result<(), ()>
	{
		Ok(())
	}

	fn visit_drop_highest(&mut self, _inst: &DropHighest) -> Result<(), ()>
	{
		Ok(())
	}

	fn visit_sum_rolling_record(
		&mut self,
		_inst: &SumRollingRecord
	) -> Result<(), ()>
	{
		Ok(())
	}

	fn visit_add(&mut self, inst: &Add) -> Result<(), ()>
	{
		self.organize(*inst, |inst| matches!(inst, Instruction::Add(_)))
	}

	fn visit_sub(&mut self, inst: &Sub) -> Result<(), ()>
	{
		// Subtraction is not commutative, but a chain of subtractions can be
		// rewritten as a single subtraction with the leading operand and the
		// sum of the subtrahends.
		self.organize(*inst, |inst| matches!(inst, Instruction::Sub(_)))
	}

	fn visit_mul(&mut self, inst: &Mul) -> Result<(), ()>
	{
		self.organize(*inst, |inst| matches!(inst, Instruction::Mul(_)))
	}

	fn visit_div(&mut self, inst: &Div) -> Result<(), ()>
	{
		// Division is not commutative, but a chain of divisions can be
		// rewritten as a single division with the leading operand and the
		// product of the divisors.
		self.organize(*inst, |inst| matches!(inst, Instruction::Div(_)))
	}

	fn visit_mod(&mut self, _inst: &Mod) -> Result<(), ()> { Ok(()) }

	fn visit_exp(&mut self, _inst: &Exp) -> Result<(), ()> { Ok(()) }

	fn visit_neg(&mut self, _inst: &Neg) -> Result<(), ()> { Ok(()) }

	fn visit_return(&mut self, _inst: &Return) -> Result<(), ()> { Ok(()) }
}

impl ConstantCommuter<'_>
{
	/// Place the specified commutative instruction into a group, using the
	/// supplied filter to recognize other instructions of the same type.
	///
	/// # Parameters
	/// - `inst`: The instruction to organize.
	/// - `filter`: A function that answers `true` if the instruction is of the
	///   same type as the one being organized.
	fn organize(
		&mut self,
		inst: impl Into<Instruction>,
		filter: impl Fn(&Instruction) -> bool
	) -> Result<(), ()>
	{
		let inst = inst.into();
		// Obtain our group, creating it if necessary.
		let group = self.group_id(&inst);
		// Merge each reader's group with ours if the reader is the same type of
		// instruction as us. This reduces the total number of expensive merges.
		for reader in self
			.analyzer
			.readers()
			.get(&inst.destination().unwrap())
			.unwrap()
			.clone()
		{
			let reader = &self.instructions[reader.0];
			if filter(reader)
			{
				let reader_group = self.group_id(&reader.clone());
				self.merge_group_ids(group, reader_group);
			}
		}
		Ok(())
	}

	/// Answer the identifier of the group to which the specified instruction
	/// belongs, creating a new group and branding the instruction if
	/// necessary.
	///
	/// # Parameters
	/// - `inst`: The instruction.
	///
	/// # Returns
	/// The requested group identifier.
	fn group_id(&mut self, inst: &Instruction) -> usize
	{
		match self.groups.get(inst)
		{
			Some(index) => *index,
			None =>
			{
				let index = self.next_group;
				self.groups.insert(inst.clone(), index);
				self.next_group += 1;
				index
			}
		}
	}

	/// Merge two groups of commutative instructions.
	///
	/// # Parameters
	/// - `first`: The first group identifier.
	/// - `second`: The second group identifier.
	fn merge_group_ids(&mut self, first: usize, second: usize)
	{
		if first != second
		{
			// We prefer to keep the lower group number.
			let min = min(first, second);
			let max = max(first, second);
			// For each instruction in the second group, move it to the first
			// group.
			self.groups.iter_mut().for_each(|(_, group)| {
				if *group == max
				{
					*group = min;
				}
			});
			assert!(self.groups.values().all(|&group| group != max));
		}
	}

	/// Answer the instructions in the specified group, sorted by destination
	/// register.
	///
	/// # Parameters
	/// - `group`: The group identifier.
	///
	/// # Returns
	/// The instruction group.
	fn group(&self, group: usize) -> Vec<Instruction>
	{
		let mut instructions = self
			.groups
			.iter()
			.filter_map(|(inst, g)| if *g == group { Some(inst) } else { None })
			.map(|inst| self.replacements.get(inst).unwrap_or(inst).clone())
			.collect::<Vec<_>>();
		instructions.sort_by_key(Instruction::destination);
		instructions
	}

	/// Rewrite the instructions in a commutative group such that the immediate
	/// operands are read first and all registers are read in ascending order.
	/// Hoisting the immediate operands to the front of a chain of commutative
	/// instructions makes them more amenable to other optimizations. Do not
	/// emit the instructions, just populate the replacement map.
	///
	/// # Type Parameters
	/// - `I`: The type of instruction to rewrite.
	///
	/// # Parameters
	/// - `group`: The group of instructions to rewrite.
	/// - `constructor`: A function that constructs a new instruction of the
	///   appropriate type from the destination and source operands of the
	///   original instruction.
	fn rewrite_commutative_group<I>(
		&mut self,
		group: usize,
		constructor: impl Fn(AddressingMode, &[AddressingMode]) -> I
	) where
		I: Into<Instruction>
	{
		// Collect all instructions within the specified commutative group,
		// using any replacements that have already been established. Extract
		// their operands and sort them in descending order, such that
		// immediates are at the front and registers are at the back. Ensure
		// that registers are written before they are read by preserving
		// ascending order. Vectors can only efficiently pop from the back, so
		// we reverse the order of the operands before iterating over them.
		let instructions = self.group(group);
		let mut ops = instructions
			.iter()
			.flat_map(Instruction::sources)
			.collect::<Vec<_>>();
		ops.sort();
		ops.reverse();
		let arity = ops.len() / instructions.len();
		instructions.iter().for_each(|inst| {
			let ops =
				(0..arity).map(|_| ops.pop().unwrap()).collect::<Vec<_>>();
			let new_inst =
				constructor(inst.destination().unwrap(), &ops).into();
			self.replacements.insert((*inst).clone(), new_inst);
		});
	}

	/// Rewrite the instructions in a nearly commutative group such that the
	/// commutative immediate operands are read first and all commutative
	/// registers are read in ascending order. Hoisting the immediate operands
	/// to the front of a chain of nearly commutative instructions makes them
	/// more amenable to other optimizations. Do not emit the instructions, just
	/// populate the replacement map. The first instruction in the group does
	/// not commute with the rest, so we handle it specially and emit it last.
	///
	/// # Type Parameters
	/// - `C`: The type of commutative instruction to rewrite.
	/// - `F`: The type of final instruction to rewrite.
	///
	/// # Parameters
	/// - `group`: The group of instructions to rewrite.
	/// - `commutative_constructor`: A function that constructs a new
	///   commutative instruction of the appropriate type from the destination
	///   and source operands of the original instruction.
	/// - `final_constructor`: A function that constructs a new terminal
	///   instruction of the appropriate type from the destination and source
	///   operands of the original instruction.
	fn rewrite_nearly_commutative_group<C, F>(
		&mut self,
		group: usize,
		commutative_constructor: impl Fn(AddressingMode, &[AddressingMode]) -> C,
		final_constructor: impl Fn(AddressingMode, &[AddressingMode]) -> F
	) where
		C: Into<Instruction>,
		F: Into<Instruction>
	{
		// Collect all instructions within the nearly commutative group. If
		// there's only a single element in the group, then there's nothing to
		// do.
		let mut instructions = self.group(group);
		if instructions.len() > 1
		{
			// Extract all of the destination registers from the complete chain,
			// including the terminal instruction. They are already sorted.
			let mut targets = instructions
				.iter()
				.flat_map(Instruction::destination)
				.collect::<Vec<_>>();
			// Extract all of the operands from the complete chain.
			let mut ops = instructions
				.iter()
				.flat_map(Instruction::sources)
				.collect::<Vec<_>>();
			// The very first operand is not commutative with the rest, so we
			// handle it specially. We'll pop it off the front of the list and
			// re-inject it when we emit the final non-commutative instruction.
			let first_op = ops.remove(0);
			// The next to last operand is the destination of the last
			// commutative instruction. We swap it with the last operand so that
			// we can pop it off the back of the list. We sort the remaining
			// operands, which are all part of the commutative chain.
			let len = ops.len();
			ops.swap(len - 2, len - 1);
			let commutative_result = ops.pop().unwrap();
			ops.sort();
			// The first instruction is non-commutative, and needs to be emitted
			// last. We remove it from the list of instructions, rewrite it with
			// the correct operands, and save it to emit at the end.
			let first_inst = instructions.remove(0);
			let terminal = final_constructor(
				targets.pop().unwrap(),
				&[first_op, commutative_result]
			)
			.into();
			// Reverse the remaining operands and destinations prior to
			// traversing the commutative instructions.
			ops.reverse();
			targets.reverse();
			// Rewrite the commutative instructions in the chain, now that
			// everything is in the correct order for further optimization.
			let arity = ops.len() / instructions.len();
			let mut previous = first_inst.clone();
			instructions.iter().for_each(|inst| {
				let dest = targets.pop().unwrap();
				let ops =
					(0..arity).map(|_| ops.pop().unwrap()).collect::<Vec<_>>();
				let new_inst = commutative_constructor(dest, &ops).into();
				self.replacements.insert(previous.clone(), new_inst);
				previous = inst.clone();
			});
			self.replacements.insert(previous, terminal);
		}
	}
}