rucc_ir/opcode.rs
1//! The instruction set.
2//!
3//! Design: `spec/08-ir.md` section 8.3.
4//!
5//! The set is small enough to enumerate and it is closed. Adding an opcode is a spec change,
6//! because the verifier, the printer, the parser, the rewrite rules and the lowering all have
7//! to learn it, and an opcode that only half of them know about is a silent miscompilation
8//! waiting for the right input.
9//!
10//! Two things are deliberately absent. There is no `getelementptr`: pointer arithmetic is
11//! [`Opcode::PtrAdd`] over a byte offset the frontend computed, because C never needs the
12//! multi-index form and its absence removes a well known source of complexity. And there is no
13//! `phi`: values arriving at a block are the block's parameters, passed by the branch, so
14//! there is no operand list positionally tied to a predecessor list kept somewhere else.
15
16use std::fmt;
17
18/// One instruction of the IR.
19///
20/// The names are the textual form exactly, so [`Opcode::name`] and [`Opcode::from_name`] are
21/// what the printer and the parser use, and neither carries a table of its own that could
22/// drift from this one.
23///
24/// The enum is not `non_exhaustive`, deliberately. The set is closed, so a pass that matches
25/// on every opcode should stop compiling when one is added rather than fall into a wildcard
26/// arm that quietly does the wrong thing.
27#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
28pub enum Opcode {
29 // Constants. A constant is an instruction rather than an operand kind, so that every
30 // operand is a value and every value has one definition, which is what makes the
31 // dominance check in the verifier a single rule rather than a rule with exceptions.
32 /// An integer constant, `iconst.i32 7`.
33 IConst,
34 /// A floating point constant, `fconst.f64 0x1.8p+1`.
35 FConst,
36 /// A vector constant with every lane the same, `splat.i8x16 0`.
37 Splat,
38 /// The address of a global or a function, `global_addr @counter`.
39 GlobalAddr,
40 /// The address of a block in this function, `block_addr block3`.
41 ///
42 /// The one instruction that names a block without being a branch, which is what GNU's
43 /// `&&label` is. Where it goes is [`Opcode::IndirectBr`], and the two are only useful
44 /// together: an address on its own is a number that nothing can do anything with.
45 BlockAddr,
46
47 // Arithmetic.
48 /// Integer addition.
49 Add,
50 /// Integer subtraction.
51 Sub,
52 /// Integer multiplication.
53 Mul,
54 /// Signed division.
55 SDiv,
56 /// Unsigned division.
57 UDiv,
58 /// Signed remainder, with the sign of the dividend.
59 SRem,
60 /// Unsigned remainder.
61 URem,
62 /// Bitwise and.
63 And,
64 /// Bitwise or.
65 Or,
66 /// Bitwise exclusive or.
67 Xor,
68 /// Shift left.
69 Shl,
70 /// Logical shift right, shifting in zeroes.
71 LShr,
72 /// Arithmetic shift right, shifting in the sign bit.
73 AShr,
74 /// Floating point addition.
75 FAdd,
76 /// Floating point subtraction.
77 FSub,
78 /// Floating point multiplication.
79 FMul,
80 /// Floating point division.
81 FDiv,
82 /// Floating point remainder.
83 FRem,
84 /// Floating point negation, which flips the sign bit and is not `0 - x`.
85 FNeg,
86 /// Fused multiply-add, rounded once.
87 Fma,
88
89 // Comparison.
90 /// Integer comparison, producing `i1` or a vector of `i1`.
91 ICmp,
92 /// Floating point comparison, producing `i1` or a vector of `i1`.
93 FCmp,
94
95 // Selection.
96 /// One of two values, chosen by a bit. `select c, a, b` is `a` when `c` is one.
97 ///
98 /// This is what control flow becomes when it stops being control flow.
99 /// `spec/optimizer/22-phiopt-and-if-conversion.md` section 22.2 makes it the lowering target
100 /// for a diamond whose two arms compute a value, and the reason it is an opcode rather than a
101 /// pattern is that it is the form the rule set is written against: `select(c, a, a) -> a` and
102 /// `select(c, 1, 0) -> zext(c)` are ordinary rules once the shape has a name.
103 ///
104 /// Both arms are evaluated, which is the whole point and also the whole danger. Whatever
105 /// produces one of these owes the argument that evaluating the arm that is not chosen is
106 /// harmless, and section 22.6 is the list of ways that argument goes wrong.
107 Select,
108
109 // Conversion.
110 /// Narrows an integer, discarding the high bits.
111 Trunc,
112 /// Widens an integer, copying the sign bit.
113 SExt,
114 /// Widens an integer, filling with zeroes.
115 ZExt,
116 /// Narrows a floating point value.
117 FPTrunc,
118 /// Widens a floating point value.
119 FPExt,
120 /// Floating point to signed integer.
121 FPToSI,
122 /// Floating point to unsigned integer.
123 FPToUI,
124 /// Signed integer to floating point.
125 SIToFP,
126 /// Unsigned integer to floating point.
127 UIToFP,
128 /// An address to an integer of the same width.
129 PtrToInt,
130 /// An integer to an address.
131 IntToPtr,
132 /// A reinterpretation of the same bits at the same width.
133 Bitcast,
134
135 // Memory.
136 /// Memory as the function found it, which is where a memory SSA chain starts.
137 ///
138 /// It produces one `mem` and takes nothing, and it belongs at the top of the entry block.
139 /// GCC calls the same thing the default definition of `.MEM` and LLVM calls it
140 /// `liveOnEntry`. It exists as an instruction rather than as a parameter of the entry block
141 /// because the entry block's parameters are the function's parameters and the verifier
142 /// checks them against the signature, and memory is not an argument anybody passed.
143 MemEntry,
144 /// A stack slot. In the entry block, or marked dynamic for a variable length array.
145 Alloca,
146 /// A read.
147 Load,
148 /// A write, producing no value.
149 Store,
150 /// Address arithmetic: an address and a byte offset.
151 PtrAdd,
152 /// A copy of a known size between addresses that do not overlap.
153 Memcpy,
154 /// A copy of a known size between addresses that may overlap.
155 Memmove,
156 /// A fill of a known size with one byte.
157 Memset,
158 /// An atomic read.
159 AtomicLoad,
160 /// An atomic write.
161 AtomicStore,
162 /// An atomic read-modify-write, carrying which operation in [`RmwOp`](crate::RmwOp).
163 AtomicRmw,
164 /// An atomic compare and exchange, producing the old value and whether it succeeded.
165 Cmpxchg,
166 /// A memory barrier.
167 Fence,
168
169 // Memory safety. Design: `spec/safe-memory/06-instrumentation.md` section 6.2.2. None of
170 // these is emitted unless `-fsafety` asked for it, and a function compiled without it
171 // contains not one of them.
172 /// The capability of a pointer value, taken from the pointer's provenance.
173 ///
174 /// One operand, the pointer, and one capability out. This is the general question and the other
175 /// producers are all the special cases of it that have a cheap answer, which is why it is the
176 /// one that always works and the one to reach for last. A pointer whose provenance the compiler
177 /// still holds should have got its capability from wherever that provenance came from: from the
178 /// allocation site, from the aux slot beside it in memory, from the frame its caller wrote, or
179 /// from the derivation that narrowed it. One of these is what is left when none of those did.
180 ///
181 /// So it has two lowerings and which one it gets is not a property of the instruction. A pointer
182 /// the lowering can trace back to an allocation site gets the cheap answer, which is a subtract
183 /// and a load off the header the allocator wrote. Anything else gets the plane walk, which is
184 /// the expensive answer and the only one that is always available, and comes back marked as
185 /// recovered so the summary counts it as the weakening it is.
186 ///
187 /// An interior pointer is always the second of those, and that is the answer to the question
188 /// document 05 section 5.2.3 leaves open rather than a gap in it. The header is behind the
189 /// payload so that finding it is a subtract by a constant, which is true of a pointer to the
190 /// base of an object and false of a pointer to the middle of one, and the lowering asks for the
191 /// base by construction rather than by checking.
192 CapOf,
193 /// The capability in the auxiliary slot beside a stored pointer, read back.
194 ///
195 /// A pointer written to memory and read again has to bring its capability with it, and where
196 /// the capability lives is document 05's question rather than this one's. What this says is
197 /// that a capability comes back from an address.
198 ///
199 /// Three operands: the capability of the object the word is in, the address of the word, and
200 /// the pointer that was loaded from it. The first is what says where the slot is, since the
201 /// aux is in front of the object and the arithmetic is over the object's own base and extent,
202 /// and the third is what the slot's two numbers are relative to. Both of those are facts about
203 /// the representation document 05 section 5.2.2 chose, and an instrumented load has both
204 /// values in hand already, since the capability is the one its bounds check used and the
205 /// pointer is what the load produced.
206 CapLoad,
207 /// The other half of [`Opcode::CapLoad`], writing one into the slot beside a pointer.
208 ///
209 /// Four operands: the capability of the object the word is in, the address of the word, the
210 /// pointer being stored there, and that pointer's capability. The first three are the same
211 /// three [`Opcode::CapLoad`] takes and for the same reasons, and the fourth is what is being
212 /// written down.
213 CapStore,
214 /// The slots beside a run of copied words, carried over from the run they were copied from.
215 ///
216 /// Three operands, for the reason [`Opcode::MetaTypeCopy`] has three: the destination, the
217 /// source, and how many bytes moved. A copy of a structure moves whatever pointers are in it
218 /// and the capability of each of those lives in the slot beside it, so a copy that moved the
219 /// bytes and left the slots alone would leave every pointer in the destination described by
220 /// whatever was there before, which is nothing at best and another instance's answer at worst.
221 ///
222 /// No capability operand, for the reason the other two copies name no node. What each
223 /// destination slot ends up saying is whatever the slot beside the word it came from said, and
224 /// the only place that is written down is the aux over the source.
225 CapCopy,
226 /// The capability that permits nothing, which is what a null pointer has.
227 CapNull,
228 /// A capability narrowed to a sub-object of what it covered.
229 ///
230 /// Only under `-fsafety-subobject`. Narrowing is what catches an overflow from one member of
231 /// a struct into the next, and it is separate because C code that walks off the end of a
232 /// member on purpose exists and a project has to be able to say so.
233 CapNarrow,
234 /// The capability for an address that arrived from outside, recovered from the planes.
235 CapRecover,
236 /// How many bytes from an address on the capability covers, asking for no more than a limit.
237 ///
238 /// Three operands, the capability, the address and how many bytes the asker wants, and one
239 /// integer result that is never more than that limit. It is what
240 /// `spec/safe-memory/07-check-elimination.md` section 7.4 needs to split a loop: the checked
241 /// part and the unchecked part are divided at `min(n, extent / sizeof(T))`, and the extent is
242 /// the half of that a compiler cannot work out on its own.
243 ///
244 /// The limit is an operand because under milestone S1 answering means walking the lifetime
245 /// plane, and a walk that stops at the number of bytes the loop was going to read anyway is
246 /// bounded by the work the loop is already doing. An answer smaller than the truth costs
247 /// iterations in the checked half and is never wrong, which is what makes stopping early
248 /// allowed. Once a capability carries its own bounds, which is milestone S2, this is a
249 /// subtraction on the capability and the limit is one `min`.
250 CapExtent,
251 /// How many bytes below an address on the capability covers, asking for no more than a limit.
252 ///
253 /// The mirror of [`Opcode::CapExtent`], with the same three operands and the same kind of
254 /// answer. What it counts is the bytes ending at the address rather than the bytes starting
255 /// there, so an answer of `n` says that `[addr - n, addr)` belongs to one thing. The address
256 /// itself is one past what is asked about, which is what a walk from high to low needs: the
257 /// question is about where the walk ends up, and where it ends up is below where it began.
258 ///
259 /// The ownership asked about is the byte below the address rather than the byte at it, since
260 /// the address may be one past the end of the object and the object is what the question is
261 /// about. Everything else, the limit operand and why an answer short of the truth is allowed,
262 /// is [`Opcode::CapExtent`]'s.
263 CapExtentBack,
264 /// The capabilities of a call's pointer arguments, handed over beside the arguments.
265 ///
266 /// Document 05 section 5.3 is the whole of why this is an instruction of its own rather than
267 /// more operands on the call. An instrumented function's calling convention is unchanged, so a
268 /// pointer argument goes in the register it always went in and `sizeof(void *)` is still eight,
269 /// which is what lets an object this compiler built link against one nobody instrumented. The
270 /// capability therefore travels out of band, in a small per thread frame the caller writes and
271 /// the callee reads, indexed by argument position.
272 ///
273 /// One operand per pointer argument, in the order the call passes them, and no operand for the
274 /// arguments that are not pointers. A call handing over more pointers than the frame has room
275 /// for describes the ones that fit and the callee recovers the rest, which is a weakening the
276 /// summary counts rather than a refusal. How many fit is the runtime's number and
277 /// `rucc_safety::frame` is where the compiler keeps it, so nothing here is a bound on the
278 /// operand count.
279 ///
280 /// It goes immediately in front of the call it is about, which is how the two are tied
281 /// together, the same way [`Opcode::MetaRelease`] is tied to the atomic it goes in front of.
282 CapPublish,
283 /// There is no frame for this call, which is what a callee that might not be instrumented gets.
284 ///
285 /// The other half of [`Opcode::CapPublish`] and not the absence of one. Publishing to a callee
286 /// that never reads the frame leaves it in place for whatever that callee calls back into, and
287 /// a callback entered from uninstrumented code holding somebody else's capabilities is worse
288 /// than one entered holding none. So a call the compiler cannot say reads frames says there is
289 /// nothing rather than saying nothing at all, and document 10 section 10.8's callback recovers
290 /// its arguments the way an entry from outside always does.
291 CapClear,
292 /// The capability of a pointer parameter, out of the frame the caller published.
293 ///
294 /// The reading end of [`Opcode::CapPublish`], in the callee rather than in the caller. Two
295 /// operands, the parameter itself and which position it is in the call, and one capability out.
296 ///
297 /// The parameter is an operand because it is the answer when there is no frame. A function
298 /// entered from code this build never compiled finds nothing published, and so does one whose
299 /// caller could not vouch for it and said `cap_clear`, and in both cases the capability has to be
300 /// worked out from the pointer the way [`Opcode::CapRecover`] does. That is the expensive answer
301 /// the frame exists to avoid, it is available whatever the caller did, and it is counted as the
302 /// weakening it is.
303 ///
304 /// The position is an operand rather than a payload for the same reason [`Opcode::CapNarrow`]'s
305 /// offset and length are: it is a number the runtime is handed. A position the frame does not
306 /// reach is not an error either, since it is the recovery again, so nothing here has to know how
307 /// many capabilities a frame holds.
308 CapArg,
309 /// The capability of the pointer this function is returning, left where its caller reads.
310 ///
311 /// One operand, the capability, and it goes immediately in front of the `ret` it is about, which
312 /// is the tie [`Opcode::CapPublish`] has with its call. A function that returns from several
313 /// places has one of these in front of each of them, because what is being said is about the
314 /// value leaving by that particular one.
315 ///
316 /// This is the only thing in the design that writes into a frame somebody else made. It is
317 /// allowed to because the frame is the caller's stack and the caller is waiting for this
318 /// function to return, and it is needed because a returned pointer is the one value that crosses
319 /// a call in the other direction.
320 CapYield,
321 /// The capability of the pointer a call gave back, out of the frame the call was published with.
322 ///
323 /// The reading end of [`Opcode::CapYield`], in the caller. One operand, the pointer that came
324 /// back, and one capability out. The operand is there for the reason [`Opcode::CapArg`]'s is: a
325 /// callee that wrote nothing leaves the bottom capability where the caller put it, and the
326 /// bottom one is the signal to work the answer out from the pointer instead.
327 ///
328 /// It goes immediately after the call it is about, and that call has to be one a `cap_publish`
329 /// goes in front of. A call that says there is no frame has no frame for a callee to have
330 /// written into, so there would be nothing for this to read.
331 CapResult,
332 /// An access is within its capability's bounds, aligned, and permitted.
333 ///
334 /// The size and the alignment are the access's, and they are in the memory payload rather
335 /// than in operands because they are what the front end knew and not what the program
336 /// computed.
337 ///
338 /// A third operand overrides how many bytes are asked about, and it exists for the one check
339 /// the front end did not write. `spec/safe-memory/07-check-elimination.md` section 7.4 replaces
340 /// the checks in a loop that runs `n` times with one check over `n * sizeof(T)` bytes, and that
341 /// is a length the program computes rather than one anybody knew when the access was parsed. The
342 /// payload still holds the alignment and the type information of the access the check came from,
343 /// and its size becomes the size of one of them rather than the size of the question.
344 CheckBounds,
345 /// The capability's provenance is still live.
346 CheckLive,
347 /// The access agrees with the type plane, which is the effective type rule of C 6.5.
348 CheckType,
349 /// The bytes the access reads have been written.
350 CheckInit,
351 /// A pointer derived from another stays inside the capability the first one had.
352 ///
353 /// Three operands, because the answer is about the new pointer and the question is about
354 /// the old one's capability.
355 CheckDeriv,
356 /// The metadata this access is about to consult has not been changed under it.
357 ///
358 /// Judgement J9, which document 09 section 9.5 specifies and which document 04 section 4.5
359 /// keeps out of J1 for the reason the `restrict` checks are kept out: it is a statement about
360 /// two operations rather than about one. The payload holds the size of the access, because the
361 /// question is whether any granule of the range this touches carries a write by another thread.
362 CheckRace,
363 /// This read did not reach a byte another `restrict` pointer of the same block wrote.
364 ///
365 /// Judgement J8, which document 09 section 9.6 specifies and which document 04 section 4.6
366 /// keeps out of J1 because it is a statement about a pair of accesses rather than about one.
367 /// The payload holds the size of the access and the two numbers saying which pointer it went
368 /// through, and those are the same two [`crate::Restrict`] carries on the access itself.
369 ///
370 /// Read and write are two opcodes rather than one with a flag on it, because every bit of
371 /// [`crate::Flags`] is spoken for and because the operand would then be a constant the program
372 /// does not compute, which is the thing [`Opcode::CheckBounds`] says belongs in the payload.
373 CheckRestrictRead,
374 /// The same about a write, which is the half of the pair that makes the other half a violation.
375 ///
376 /// Two accesses that both only read are not a violation of anything, so what the scope records
377 /// is which of them wrote and the check refuses a pair only when at least one did.
378 CheckRestrictWrite,
379 /// The capability this free goes through still names the instance that owns the address.
380 ///
381 /// Judgement J6, and the same two operands [`Opcode::CheckLive`] has for the same reason: a
382 /// capability and the pointer it is about, with no payload, because how many bytes are at the
383 /// address is not something a free asks. What separates it from the lifetime check is where it
384 /// goes and what it is about. This one sits in front of a call that ends a storage instance
385 /// rather than in front of an access, and the question it decides is whether the pointer being
386 /// handed over still names the instance it was made for.
387 ///
388 /// It exists because that question has no other way of being asked. `free` takes an address and
389 /// nothing else, so a second free of a block the allocator has already handed back out looks
390 /// like an ordinary free to the allocator and releases somebody else's live object.
391 /// tamnd/rucc#492 is that, and the version the capability carries is what tells the two apart.
392 ///
393 /// Nothing discharges one. A bounds fact says nothing about it and a lifetime fact about an
394 /// access says nothing about a free, so it stays where the pass put it and the cost is one
395 /// check per free rather than one per access.
396 CheckFree,
397 /// A storage instance begins here, over a range, with a class.
398 ///
399 /// Judgement J4. This is the `alloca` for an automatic instance and the allocator's report
400 /// for an allocated one, and the range is a pointer and a length in registers rather than a
401 /// payload, because the length of a variable length array is not known when the instruction
402 /// is written down.
403 MetaBegin,
404 /// A storage instance ends here, which is judgement J5.
405 ///
406 /// Every capability for it fails from this point on and keeps failing after the address is
407 /// handed out again, which is what makes the check a use after free check rather than a use
408 /// after reallocation one.
409 MetaEnd,
410 /// The effective type of a range is now this one.
411 MetaType,
412 /// The effective types of a range are now the ones the range it was copied from had.
413 ///
414 /// Three operands, because a copy has two ranges and one length: the destination, the source,
415 /// and how many bytes moved. A `memcpy` does not store through a type, so there is no type to
416 /// name here and naming one would be wrong: C 6.5 says the copied bytes keep the effective type
417 /// they had, whatever that was, and the only place that is written down is the plane over the
418 /// source.
419 MetaTypeCopy,
420 /// The bytes of a range are now initialized.
421 MetaInit,
422 /// The bytes of a range are now initialized wherever the range they were copied from was.
423 ///
424 /// Three operands, for the reason [`Opcode::MetaTypeCopy`] has three. A copy does not write
425 /// values of its own, so whether a destination byte holds anything is whether the byte it came
426 /// from did, and the only place that is written down is the plane over the source. That is what
427 /// makes a structure filled member by member and then copied whole still have padding nothing
428 /// wrote, which is the infoleak the plane is for.
429 MetaInitCopy,
430 /// This thread wrote a range, at whatever step of its own counting it has reached.
431 ///
432 /// The epoch plane's only write, from `spec/safe-memory/09-type-init-and-races.md` section 9.5.
433 /// Two operands like the other plane writes, and the range is a pointer shaped slot rather than
434 /// whatever the access covered: the plane holds one stamp per eight bytes because that is what
435 /// a pointer comes in, and a granule two threads share is one holding no pointer.
436 ///
437 /// It carries no thread and no count. Which thread is running and how far it has counted are
438 /// both facts about the moment the program reaches this, so the runtime reads them and nothing
439 /// here could name them.
440 MetaEpoch,
441 /// Everything this thread has done so far is published at the atomic object named here.
442 ///
443 /// One half of a synchronization edge in the sense of section 9.5, and the half that goes in
444 /// front of the atomic that carries it. The operand is the object's address, because that is
445 /// the key whoever takes the other end will look the clock up under, and there is no payload:
446 /// which thread is publishing and how far it has counted are facts about the moment the program
447 /// reaches this, the same way they are for [`Opcode::MetaEpoch`].
448 ///
449 /// This exists because an atomic is not a call. Every other edge the monitor knows about is a
450 /// `pthread` function and is interposed, and a C11 release store is a machine instruction with
451 /// nothing to interpose, so the compiler is the only thing that can say the edge was there.
452 MetaRelease,
453 /// Everything published at the atomic object named here is now ordered before this thread.
454 ///
455 /// The other half of [`Opcode::MetaRelease`], and it goes after the atomic rather than in front
456 /// of it, because the ordering it takes is the ordering the atomic just read.
457 ///
458 /// A missing edge is the one kind of missing instrumentation in the whole safety pass that
459 /// costs a false report rather than a missed one: two threads that really were ordered by an
460 /// edge nobody recorded look concurrent, and a race is reported against a program doing nothing
461 /// wrong. That is why the edges go in before the race check is ever on by default.
462 MetaAcquire,
463 /// Everything this thread has done so far is published at no object in particular.
464 ///
465 /// What a release fence is, and the reason it cannot reuse [`Opcode::MetaRelease`]: a fence
466 /// orders against every other thread rather than against one object, so there is no address to
467 /// key it on and it takes no operands at all. The relaxed atomic that usually sits beside it in
468 /// the source is not the key either, because the fence orders everything, not that one word.
469 ///
470 /// The runtime pays for that with one cell shared by every fence in the program, which orders
471 /// more pairs of threads than the program really ordered. That direction is safe. A thread put
472 /// further ahead than it needed to be reports fewer races, never a wrong one.
473 MetaFenceRelease,
474 /// Everything published at any release fence is now ordered before this thread.
475 ///
476 /// The other half of [`Opcode::MetaFenceRelease`], after the fence rather than in front of it,
477 /// for the same reason [`Opcode::MetaAcquire`] goes after its atomic.
478 MetaFenceAcquire,
479 /// A range leaves the monitor's authority, or comes back, which is judgement J7.
480 MetaTransfer,
481 /// A declared exemption starts here, with the reason it was declared.
482 ///
483 /// Not an optimization hint. Everything between this and its `safe_region_end` is code the
484 /// monitor is told not to judge, so the reason it carries is a trust set entry, and
485 /// `spec/safe-memory/10-boundaries.md` section 10.2 counts them per build precisely so that
486 /// a reviewer can read what a binary's guarantee rests on.
487 SafeRegionBegin,
488 /// The end of the region the last `safe_region_begin` opened.
489 SafeRegionEnd,
490 /// A block that declares `restrict` pointers begins here, over a slot to keep its record in.
491 ///
492 /// The operand is the storage the record lives in, which is the block's own stack slot, and the
493 /// payload says how large it is. The clique the block was given is [`crate::Restrict::clique`]
494 /// of the payload and how many pointers it declares is [`crate::Restrict::base`], which is the
495 /// one place that field counts bases rather than naming one.
496 ///
497 /// A marker rather than something the front end could fold into the accesses, because the
498 /// promise is about the block's dynamic extent: a function called twice has made the promise
499 /// twice, and what the second call reached says nothing about the first.
500 RestrictEnter,
501 /// The block the last `restrict_enter` opened ends here.
502 ///
503 /// The operand is the same slot, so that the record can be unlinked from whatever encloses it
504 /// without the runtime having to keep a list of its own.
505 RestrictLeave,
506
507 // Control. Every one of these is a terminator.
508 /// An unconditional branch, `jump block1(%a, %b)`.
509 Jump,
510 /// A two-way branch on an `i1`.
511 BrIf,
512 /// A multi-way branch on an integer, with a default.
513 Switch,
514 /// A branch to an address, `indirect_br %0, block1, block2`.
515 ///
516 /// The targets are every block control can arrive at, which is what makes the edges of a
517 /// computed `goto` ordinary edges: nothing else in the compiler has to know that the
518 /// address decides which one it is. A target that is not listed is a branch that does not
519 /// happen, so a frontend that leaves one out has made a promise on the program's behalf.
520 IndirectBr,
521 /// A return, with the values the signature says.
522 Return,
523 /// A place control cannot reach, which the frontend emits after a `noreturn` call.
524 Unreachable,
525
526 // Calls.
527 /// A call to a named function.
528 Call,
529 /// A call through an address, carrying the signature it is called with.
530 CallIndirect,
531 /// A call in tail position that reuses the frame, which is a terminator.
532 TailCall,
533
534 // Intrinsics, which is the closed part. The open part is `TargetIntrinsic`.
535 /// Count leading zeroes.
536 Ctlz,
537 /// Count trailing zeroes.
538 Cttz,
539 /// Count set bits.
540 Ctpop,
541 /// Reverse the bytes.
542 Bswap,
543 /// Reverse the bits.
544 Bitreverse,
545 /// Signed addition, producing the result and whether it overflowed.
546 SAddOverflow,
547 /// Unsigned addition, producing the result and whether it overflowed.
548 UAddOverflow,
549 /// Signed subtraction, producing the result and whether it overflowed.
550 SSubOverflow,
551 /// Unsigned subtraction, producing the result and whether it overflowed.
552 USubOverflow,
553 /// Signed multiplication, producing the result and whether it overflowed.
554 SMulOverflow,
555 /// Unsigned multiplication, producing the result and whether it overflowed.
556 UMulOverflow,
557 /// `__builtin_expect`, which is the value with a hint attached.
558 Expect,
559 /// `__builtin_unreachable` as a hint on a path, distinct from the terminator.
560 UnreachableHint,
561 /// `__builtin_trap`, which stops the program where it stands.
562 ///
563 /// Not a terminator, for the reason `unreachable_hint` is not one: what ends a block here is
564 /// control going somewhere, and this goes nowhere at all. The block it is in goes on being
565 /// lowered and whatever follows it is written and never run, which costs a few bytes nothing
566 /// reaches and keeps every pass that walks a block from needing a second shape for it.
567 Trap,
568 /// `__builtin_prefetch`.
569 Prefetch,
570 /// `__builtin_frame_address`.
571 FrameAddress,
572 /// `__builtin_return_address`.
573 ReturnAddress,
574 /// `__builtin_thread_pointer`, the address of the storage the running thread has.
575 ///
576 /// It takes nothing and answers a pointer. Unlike the two above it there is no walk to do and
577 /// no frame to have kept: the machine holds the address in a place of its own, so this is one
578 /// instruction on every target that has the builtin at all.
579 ThreadPointer,
580 /// The start of a variable argument list.
581 VaStart,
582 /// One argument off a variable argument list, which moves the list on as it reads it. Two
583 /// of these on one list are two arguments and never one argument read twice, so whatever
584 /// decides which instructions may be folded together has to leave these alone.
585 VaArg,
586 /// One argument off a variable argument list, when that argument is an object rather than a
587 /// value, which is what a `struct` or a `union` read out of one is.
588 ///
589 /// It answers the address of the object rather than the object, because an aggregate is not
590 /// a value and there is nothing for one result to be. Where the object arrives in registers
591 /// there is no address until something makes one, so what this asks of a target is a place
592 /// to put the registers and the address of that place, which is the copy every psABI's own
593 /// description of the algorithm makes. It moves the list on for the reason [`Opcode::VaArg`]
594 /// does.
595 VaObject,
596 /// The end of a variable argument list.
597 VaEnd,
598 /// A copy of a variable argument list.
599 VaCopy,
600 /// The stack pointer, saved before a variable length array.
601 StackSave,
602 /// The stack pointer, restored after one.
603 StackRestore,
604 /// The marker a `setjmp` leaves, which pins everything live across it.
605 ///
606 /// One operand, the buffer, and one result, which is the `int` the save answers with: zero
607 /// where control went past it and one where control came back to it. That is the one
608 /// instruction here whose value depends on how control reached it, and it is one instruction
609 /// rather than a branch and a block because the edge a `longjmp` travels is not in this
610 /// function's control flow graph. It is written into the buffer and taken at run time, so a
611 /// pass that walked the edges would find a block nothing reaches and take it away.
612 SetjmpMarker,
613 /// The marker a `longjmp` leaves.
614 ///
615 /// One operand, the buffer, and no result, and not a terminator either, for the reason above:
616 /// where control goes is not a block of this function. What follows it is written and never
617 /// reached.
618 LongjmpMarker,
619 /// A target-specific intrinsic, named rather than enumerated, for the vector builtins.
620 TargetIntrinsic,
621
622 /// Inline assembly. A terminator when it has labels, which is `asm goto`.
623 InlineAsm,
624}
625
626impl Opcode {
627 /// The textual form, which is also what the parser reads.
628 #[must_use]
629 pub const fn name(self) -> &'static str {
630 match self {
631 Self::IConst => "iconst",
632 Self::FConst => "fconst",
633 Self::Splat => "splat",
634 Self::GlobalAddr => "global_addr",
635 Self::BlockAddr => "block_addr",
636 Self::Add => "add",
637 Self::Sub => "sub",
638 Self::Mul => "mul",
639 Self::SDiv => "sdiv",
640 Self::UDiv => "udiv",
641 Self::SRem => "srem",
642 Self::URem => "urem",
643 Self::And => "and",
644 Self::Or => "or",
645 Self::Xor => "xor",
646 Self::Shl => "shl",
647 Self::LShr => "lshr",
648 Self::AShr => "ashr",
649 Self::FAdd => "fadd",
650 Self::FSub => "fsub",
651 Self::FMul => "fmul",
652 Self::FDiv => "fdiv",
653 Self::FRem => "frem",
654 Self::FNeg => "fneg",
655 Self::Fma => "fma",
656 Self::ICmp => "icmp",
657 Self::FCmp => "fcmp",
658 Self::Select => "select",
659 Self::Trunc => "trunc",
660 Self::SExt => "sext",
661 Self::ZExt => "zext",
662 Self::FPTrunc => "fptrunc",
663 Self::FPExt => "fpext",
664 Self::FPToSI => "fptosi",
665 Self::FPToUI => "fptoui",
666 Self::SIToFP => "sitofp",
667 Self::UIToFP => "uitofp",
668 Self::PtrToInt => "ptrtoint",
669 Self::IntToPtr => "inttoptr",
670 Self::Bitcast => "bitcast",
671 Self::MemEntry => "mem_entry",
672 Self::Alloca => "alloca",
673 Self::Load => "load",
674 Self::Store => "store",
675 Self::PtrAdd => "ptr_add",
676 Self::Memcpy => "memcpy",
677 Self::Memmove => "memmove",
678 Self::Memset => "memset",
679 Self::AtomicLoad => "atomic_load",
680 Self::AtomicStore => "atomic_store",
681 Self::AtomicRmw => "atomic_rmw",
682 Self::Cmpxchg => "cmpxchg",
683 Self::Fence => "fence",
684 Self::CapOf => "cap_of",
685 Self::CapLoad => "cap_load",
686 Self::CapStore => "cap_store",
687 Self::CapCopy => "cap_copy",
688 Self::CapNull => "cap_null",
689 Self::CapNarrow => "cap_narrow",
690 Self::CapRecover => "cap_recover",
691 Self::CapExtent => "cap_extent",
692 Self::CapExtentBack => "cap_extent_back",
693 Self::CapPublish => "cap_publish",
694 Self::CapClear => "cap_clear",
695 Self::CapArg => "cap_arg",
696 Self::CapYield => "cap_yield",
697 Self::CapResult => "cap_result",
698 Self::CheckBounds => "check_bounds",
699 Self::CheckLive => "check_live",
700 Self::CheckType => "check_type",
701 Self::CheckInit => "check_init",
702 Self::CheckDeriv => "check_deriv",
703 Self::CheckRace => "check_race",
704 Self::CheckRestrictRead => "check_restrict_read",
705 Self::CheckRestrictWrite => "check_restrict_write",
706 Self::CheckFree => "check_free",
707 Self::MetaBegin => "meta_begin",
708 Self::MetaEnd => "meta_end",
709 Self::MetaType => "meta_type",
710 Self::MetaTypeCopy => "meta_type_copy",
711 Self::MetaInit => "meta_init",
712 Self::MetaInitCopy => "meta_init_copy",
713 Self::MetaEpoch => "meta_epoch",
714 Self::MetaRelease => "meta_release",
715 Self::MetaAcquire => "meta_acquire",
716 Self::MetaFenceRelease => "meta_fence_release",
717 Self::MetaFenceAcquire => "meta_fence_acquire",
718 Self::MetaTransfer => "meta_transfer",
719 Self::SafeRegionBegin => "safe_region_begin",
720 Self::SafeRegionEnd => "safe_region_end",
721 Self::RestrictEnter => "restrict_enter",
722 Self::RestrictLeave => "restrict_leave",
723 Self::Jump => "jump",
724 Self::BrIf => "br_if",
725 Self::Switch => "switch",
726 Self::IndirectBr => "indirect_br",
727 Self::Return => "return",
728 Self::Unreachable => "unreachable",
729 Self::Call => "call",
730 Self::CallIndirect => "call_indirect",
731 Self::TailCall => "tail_call",
732 Self::Ctlz => "ctlz",
733 Self::Cttz => "cttz",
734 Self::Ctpop => "ctpop",
735 Self::Bswap => "bswap",
736 Self::Bitreverse => "bitreverse",
737 Self::SAddOverflow => "sadd_overflow",
738 Self::UAddOverflow => "uadd_overflow",
739 Self::SSubOverflow => "ssub_overflow",
740 Self::USubOverflow => "usub_overflow",
741 Self::SMulOverflow => "smul_overflow",
742 Self::UMulOverflow => "umul_overflow",
743 Self::Expect => "expect",
744 Self::UnreachableHint => "unreachable_hint",
745 Self::Trap => "trap",
746 Self::Prefetch => "prefetch",
747 Self::FrameAddress => "frame_address",
748 Self::ReturnAddress => "return_address",
749 Self::ThreadPointer => "thread_pointer",
750 Self::VaStart => "va_start",
751 Self::VaArg => "va_arg",
752 Self::VaObject => "va_object",
753 Self::VaEnd => "va_end",
754 Self::VaCopy => "va_copy",
755 Self::StackSave => "stacksave",
756 Self::StackRestore => "stackrestore",
757 Self::SetjmpMarker => "setjmp_marker",
758 Self::LongjmpMarker => "longjmp_marker",
759 Self::TargetIntrinsic => "target_intrinsic",
760 Self::InlineAsm => "inline_asm",
761 }
762 }
763
764 /// Every opcode, in the order they are declared.
765 ///
766 /// The parser walks this rather than holding a second table, because a second table is a
767 /// table that can disagree with the first one.
768 pub fn all() -> impl Iterator<Item = Self> {
769 ALL.iter().copied()
770 }
771
772 /// The opcode with that name, if there is one.
773 #[must_use]
774 pub fn from_name(name: &str) -> Option<Self> {
775 ALL.iter().copied().find(|op| op.name() == name)
776 }
777
778 /// Whether this ends a block.
779 ///
780 /// [`Opcode::InlineAsm`] is not here and is the one instruction whose answer depends on
781 /// the instruction rather than on the opcode: `asm goto` has successors and everything
782 /// else does not. Ask the instruction, not the opcode.
783 #[must_use]
784 pub const fn is_terminator(self) -> bool {
785 matches!(
786 self,
787 Self::Jump
788 | Self::BrIf
789 | Self::Switch
790 | Self::IndirectBr
791 | Self::Return
792 | Self::Unreachable
793 | Self::TailCall
794 )
795 }
796
797 /// Whether the operands can be swapped without changing the result.
798 ///
799 /// The floating point cases are commutative even under the strictest rounding, because
800 /// swapping the operands of an addition does not change which of them is a NaN, and the
801 /// sign of a NaN result is not something we promise anything about either way.
802 #[must_use]
803 pub const fn is_commutative(self) -> bool {
804 matches!(
805 self,
806 Self::Add
807 | Self::Mul
808 | Self::And
809 | Self::Or
810 | Self::Xor
811 | Self::FAdd
812 | Self::FMul
813 | Self::SAddOverflow
814 | Self::UAddOverflow
815 | Self::SMulOverflow
816 | Self::UMulOverflow
817 )
818 }
819
820 /// Whether this reads or writes memory, or has an effect the optimizer has to preserve.
821 ///
822 /// An instruction that answers no can be deleted when nothing uses its result, moved
823 /// across a call, and merged with another one computing the same thing. Everything else
824 /// has to be argued about individually, so the conservative answer is the true one here
825 /// and the list of exceptions is the part that is checked.
826 #[must_use]
827 pub const fn has_effects(self) -> bool {
828 !matches!(
829 self,
830 Self::IConst
831 | Self::FConst
832 | Self::Splat
833 | Self::GlobalAddr
834 | Self::BlockAddr
835 | Self::Add
836 | Self::Sub
837 | Self::Mul
838 | Self::SDiv
839 | Self::UDiv
840 | Self::SRem
841 | Self::URem
842 | Self::And
843 | Self::Or
844 | Self::Xor
845 | Self::Shl
846 | Self::LShr
847 | Self::AShr
848 | Self::FAdd
849 | Self::FSub
850 | Self::FMul
851 | Self::FDiv
852 | Self::FRem
853 | Self::FNeg
854 | Self::Fma
855 | Self::ICmp
856 | Self::FCmp
857 | Self::Select
858 | Self::Trunc
859 | Self::SExt
860 | Self::ZExt
861 | Self::FPTrunc
862 | Self::FPExt
863 | Self::FPToSI
864 | Self::FPToUI
865 | Self::SIToFP
866 | Self::UIToFP
867 | Self::PtrToInt
868 | Self::IntToPtr
869 | Self::Bitcast
870 | Self::PtrAdd
871 | Self::Ctlz
872 | Self::Cttz
873 | Self::Ctpop
874 | Self::Bswap
875 | Self::Bitreverse
876 | Self::SAddOverflow
877 | Self::UAddOverflow
878 | Self::SSubOverflow
879 | Self::USubOverflow
880 | Self::SMulOverflow
881 | Self::UMulOverflow
882 | Self::Expect
883 | Self::FrameAddress
884 | Self::ReturnAddress
885 // The same address for as long as the thread runs, and a thread cannot change
886 // which one it is part way through a function, so two of these in one function
887 // are the same value and either may be moved to where the other is.
888 | Self::ThreadPointer
889 | Self::MemEntry
890 // Three of the capability instructions are arithmetic on a pointer's
891 // provenance and touch nothing. The other four do: `cap_load`, `cap_store` and
892 // `cap_copy` are an access, and `cap_recover` reads the planes.
893 | Self::CapOf
894 | Self::CapNull
895 | Self::CapNarrow
896 )
897 }
898
899 /// Whether an instruction with this opcode touches memory.
900 ///
901 /// This is what decides whether it takes a memory operand once memory SSA is built, per
902 /// document 09 of `spec/optimizer`. It is written as the exceptions to touching memory
903 /// rather than as a list of what does, for the reason document 08.6 gives about the escape
904 /// analysis: an opcode added later has to end up on the conservative side by default, and a
905 /// list of what touches memory would silently leave a new one out.
906 ///
907 /// `mem_entry` answers no. It produces memory rather than touching it, which is the whole
908 /// of what it is for.
909 #[must_use]
910 pub const fn touches_memory(self) -> bool {
911 if !self.has_effects() {
912 return false;
913 }
914 !matches!(
915 self,
916 // Fresh storage nothing could have been reading, and the pointer that names it.
917 Self::Alloca
918 // The stack pointer, which is a register and not memory. Putting it back is a
919 // different matter and is below, because it takes storage away.
920 | Self::StackSave
921 // Control, which goes somewhere rather than touching anything. A tail call is
922 // not here, because it is a call.
923 | Self::Jump
924 | Self::BrIf
925 | Self::Switch
926 | Self::IndirectBr
927 | Self::Return
928 | Self::Unreachable
929 | Self::UnreachableHint
930 )
931 }
932
933 /// Whether an instruction with this opcode writes memory, and so produces a new version of
934 /// it rather than only reading the version it was given.
935 ///
936 /// Everything that touches memory writes it except the ones that plainly do not. A `fence`
937 /// writes nothing and is still a write here, because document 09.5 says an atomic or a
938 /// barrier is a definition nothing walks past, and giving it one is how that is expressed
939 /// in a representation whose only ordering is the memory chain.
940 ///
941 /// The checks read the planes and change nothing, which
942 /// `spec/safe-memory/06-instrumentation.md` section 6.2.4 states as the word `readonly`. A
943 /// check that trapped is a program that stopped and there is no version of memory after it
944 /// for anything to observe, so the trap costs nothing here. What it does cost is that a
945 /// check may not be moved across a plane write, and that is the memory chain saying so
946 /// rather than this.
947 #[must_use]
948 pub const fn writes_memory(self) -> bool {
949 self.touches_memory()
950 && !matches!(
951 self,
952 Self::Load
953 | Self::AtomicLoad
954 | Self::Prefetch
955 | Self::CapLoad
956 | Self::CapRecover
957 | Self::CapArg
958 | Self::CapResult
959 | Self::CapExtent
960 | Self::CapExtentBack
961 | Self::CheckBounds
962 | Self::CheckLive
963 | Self::CheckType
964 | Self::CheckInit
965 | Self::CheckDeriv
966 | Self::CheckRace
967 | Self::CheckFree
968 )
969 }
970
971 /// Whether the only memory this touches is the safety planes.
972 ///
973 /// The planes are storage the runtime keeps for itself, one entry per range of program bytes,
974 /// laid out by `spec/safe-memory/05-representation.md` section 5.2. What matters here is that
975 /// no name in the program reaches one. A plane write and a program store can never be the same
976 /// byte, and neither can a plane read and a program load, so an alias oracle that knows this
977 /// answers no to every pair with one of each.
978 ///
979 /// The aux plane sits in the same allocation as the object rather than in a map of its own, so
980 /// an address far enough outside an object does land in somebody's plane. That is an access
981 /// outside the bounds of the thing it was derived from, which is the access every check in this
982 /// list exists to refuse, and it is undefined before it is refused. An optimizer is entitled to
983 /// the assumption that the program does not make one, and this is that assumption and not a
984 /// second one.
985 ///
986 /// Nothing above can work that out by looking at the access, which is why this is here. The
987 /// address operand of one of these is a locator and not the memory it touches: `meta_init %p`
988 /// writes the entry the plane keeps for `%p` and does not write `%p`, so an oracle reading the
989 /// operand the ordinary way sees a write to exactly the bytes a load of `%p` wants.
990 ///
991 /// A list of what does rather than the exceptions to it, which is the other way round from
992 /// [`Opcode::touches_memory`] and for the same reason: an opcode added later and left out of
993 /// this one is an opcode the oracle says nothing about, which costs a missed optimization,
994 /// and an opcode added later that lands in here without anybody reading what it does would be
995 /// a wrong answer about memory in the safety pass of all places.
996 ///
997 /// Three families are deliberately not here even though their names look like they belong.
998 /// The capability instructions other than the two extent queries go through a slot in the
999 /// caller's frame, which is a local like any other. The `restrict` markers take the block's own
1000 /// stack slot as an operand and write their record into it. The synchronization edges say
1001 /// something about the ordering of program memory rather than only about a plane, and a wrong
1002 /// answer there is a false report rather than a missed one.
1003 #[must_use]
1004 pub const fn touches_only_planes(self) -> bool {
1005 matches!(
1006 self,
1007 Self::CheckBounds
1008 | Self::CheckLive
1009 | Self::CheckType
1010 | Self::CheckInit
1011 | Self::CheckDeriv
1012 | Self::CheckRace
1013 | Self::CheckFree
1014 | Self::CapExtent
1015 | Self::CapExtentBack
1016 | Self::MetaBegin
1017 | Self::MetaEnd
1018 | Self::MetaType
1019 | Self::MetaTypeCopy
1020 | Self::MetaInit
1021 | Self::MetaInitCopy
1022 | Self::MetaEpoch
1023 | Self::MetaTransfer
1024 )
1025 }
1026
1027 /// How many values this produces, for the opcodes where the count is fixed.
1028 ///
1029 /// `None` means the count comes from somewhere else: a call takes it from its signature,
1030 /// and inline assembly takes it from its output constraints. A tail call is not one of
1031 /// them, because whatever it returns goes straight out of the function and there is no
1032 /// instruction after it to use anything.
1033 #[must_use]
1034 pub const fn results(self) -> Option<u8> {
1035 match self {
1036 Self::Call | Self::CallIndirect | Self::InlineAsm => None,
1037 Self::Cmpxchg
1038 | Self::SAddOverflow
1039 | Self::UAddOverflow
1040 | Self::SSubOverflow
1041 | Self::USubOverflow
1042 | Self::SMulOverflow
1043 | Self::UMulOverflow => Some(2),
1044 Self::Store
1045 | Self::Memcpy
1046 | Self::Memmove
1047 | Self::Memset
1048 | Self::AtomicStore
1049 | Self::Fence
1050 | Self::Prefetch
1051 | Self::VaStart
1052 | Self::VaEnd
1053 | Self::VaCopy
1054 | Self::StackRestore
1055 | Self::UnreachableHint
1056 | Self::Trap
1057 | Self::LongjmpMarker
1058 | Self::CapStore
1059 | Self::CapCopy
1060 | Self::CapPublish
1061 | Self::CapClear
1062 | Self::CapYield
1063 | Self::CheckBounds
1064 | Self::CheckLive
1065 | Self::CheckType
1066 | Self::CheckInit
1067 | Self::CheckDeriv
1068 | Self::CheckRace
1069 | Self::CheckRestrictRead
1070 | Self::CheckRestrictWrite
1071 | Self::CheckFree
1072 | Self::MetaBegin
1073 | Self::MetaEnd
1074 | Self::MetaType
1075 | Self::MetaTypeCopy
1076 | Self::MetaInit
1077 | Self::MetaInitCopy
1078 | Self::MetaEpoch
1079 | Self::MetaRelease
1080 | Self::MetaAcquire
1081 | Self::MetaFenceRelease
1082 | Self::MetaFenceAcquire
1083 | Self::MetaTransfer
1084 | Self::SafeRegionBegin
1085 | Self::SafeRegionEnd
1086 | Self::RestrictEnter
1087 | Self::RestrictLeave => Some(0),
1088 _ if self.is_terminator() => Some(0),
1089 _ => Some(1),
1090 }
1091 }
1092
1093 /// Whether an instruction with this opcode produces a capability.
1094 ///
1095 /// Seven of the fourteen `cap` instructions. The other seven consume one instead, or none at
1096 /// all: `cap_store` writes one beside a pointer, `cap_copy` moves a run of them from beside one
1097 /// set of words to beside another, `cap_extent` and `cap_extent_back` ask one a
1098 /// question about itself and answer with a number, `cap_publish` hands a call's worth of them
1099 /// to a callee, `cap_yield` leaves one where the caller of this function will look for it, and
1100 /// `cap_clear` takes no operands because saying there is no frame is not a statement about any
1101 /// capability. The reason this is a question about the opcode rather than
1102 /// about the result type is that the verifier asks it the other way round: it walks the results
1103 /// looking for a `cap` and needs to know whether the instruction under it was entitled to make
1104 /// one.
1105 #[must_use]
1106 pub const fn makes_capability(self) -> bool {
1107 matches!(
1108 self,
1109 Self::CapOf
1110 | Self::CapLoad
1111 | Self::CapNull
1112 | Self::CapNarrow
1113 | Self::CapRecover
1114 | Self::CapArg
1115 | Self::CapResult
1116 )
1117 }
1118
1119 /// Which operand of a capability producer names the pointer the capability is about.
1120 ///
1121 /// Five of the seven [`Opcode::makes_capability`] lists, and they all mean the same thing by it:
1122 /// the capability describes the object that pointer is in. Where they differ is only in how the
1123 /// answer was arrived at, which is a walk of the lifetime plane for `cap_recover`, a read of the
1124 /// slot beside the word for `cap_load`, a read of the caller's frame for `cap_arg`, a read of
1125 /// the frame the caller published for `cap_result`, and whatever the back end has at hand for
1126 /// `cap_of`.
1127 ///
1128 /// That is worth stating as one question because the optimizer asks it. A rule that discharges a
1129 /// check by knowing which pointer the check's capability is about has no business caring which
1130 /// producer supplied it, and while `cap_of` was the only one anything emitted, asking for the
1131 /// opcode by name and taking operand zero was the same question. It stopped being the same
1132 /// question when tamnd/rucc#1241 started emitting the cheap producers, and a rule that still
1133 /// asked by name would quietly discharge less the better the code got.
1134 ///
1135 /// The two that answer nothing are the two that are not about a pointer at all. A `cap_narrow`
1136 /// is about another capability and a `cap_null` is about nothing by construction.
1137 ///
1138 /// `cap_result` was in that group and did not belong there. The reasoning was that it is about a
1139 /// pointer the callee returned, which sounds like a value this function has only as the call's
1140 /// own result, and the opcode carries that pointer as operand zero for the same reason
1141 /// `cap_arg` carries one: a callee that wrote nothing leaves the bottom capability in the slot,
1142 /// and the pointer is what the runtime falls back to working the answer out from. So the
1143 /// operand was there the whole time and this said there was none. It is the same mistake the
1144 /// paragraph above is about, made once more in the place that exists to stop it, which is
1145 /// exactly how much care this question wants: every producer that names a pointer has to be
1146 /// here, and the way to tell is to read the opcode's operands rather than its purpose.
1147 #[must_use]
1148 pub const fn capability_names(self) -> Option<usize> {
1149 match self {
1150 Self::CapOf | Self::CapRecover | Self::CapArg | Self::CapResult => Some(0),
1151 // The third, because the first two are the container's capability and the address of
1152 // the word, and the pointer this one is about is the value that came out of the word.
1153 Self::CapLoad => Some(2),
1154 _ => None,
1155 }
1156 }
1157
1158 /// Which payload an instruction with this opcode carries.
1159 ///
1160 /// The printer reads the payload it finds and does not need this. The parser has only the
1161 /// opcode when it reaches the operands, so this is where the two of them agree on what
1162 /// comes after them. An instruction carrying a payload of some other kind prints as text
1163 /// the parser cannot read back, which is why the verifier checks it against
1164 /// [`Extra::kind`](crate::Extra::kind) rather than leaving it to be found later.
1165 #[must_use]
1166 pub const fn extra_kind(self) -> ExtraKind {
1167 match self {
1168 Self::IConst | Self::FConst | Self::Splat => ExtraKind::Imm,
1169 Self::GlobalAddr | Self::TargetIntrinsic => ExtraKind::Symbol,
1170 Self::ICmp => ExtraKind::IntPred,
1171 Self::FCmp => ExtraKind::FloatPred,
1172 Self::Alloca
1173 | Self::Load
1174 | Self::Store
1175 | Self::Memcpy
1176 | Self::Memmove
1177 | Self::Memset
1178 | Self::AtomicLoad
1179 | Self::AtomicStore
1180 | Self::Cmpxchg
1181 // Four of the checks are about a run of bytes and the payload is where the size
1182 // of that run is, along with the alignment `check_bounds` wants and the aliasing
1183 // node `check_type` compares against. The other two ask a question about a
1184 // pointer and not about a range, so they carry nothing.
1185 | Self::CheckBounds
1186 | Self::CheckType
1187 | Self::CheckInit
1188 | Self::CheckRace
1189 // The two `restrict` checks and the marker that opens their scope. The first two carry
1190 // the size of the access and the two numbers saying which pointer it went through, and
1191 // the third carries the size of the slot and the numbers describing the scope itself.
1192 | Self::CheckRestrictRead
1193 | Self::CheckRestrictWrite
1194 | Self::RestrictEnter => ExtraKind::Mem,
1195 // The plane writes. What each one needs beyond the range is different, and the range
1196 // itself is operands, since the length of a variable length array is a value.
1197 Self::MetaBegin => ExtraKind::Class,
1198 Self::MetaTransfer => ExtraKind::Owner,
1199 Self::MetaType => ExtraKind::Node,
1200 Self::SafeRegionBegin => ExtraKind::Reason,
1201 Self::VaObject => ExtraKind::VaObject,
1202 Self::AtomicRmw => ExtraKind::Rmw,
1203 Self::Fence => ExtraKind::Order,
1204 Self::Prefetch => ExtraKind::Prefetch,
1205 // How far up the chain of frames to walk, which is a number written in the instruction
1206 // and never a value. The builtins these came from take a constant and nothing else, for
1207 // the reason `prefetch` takes one: the instructions this becomes are a walk of that
1208 // length, and a length not known until the program runs has nothing to walk.
1209 Self::FrameAddress | Self::ReturnAddress => ExtraKind::Depth,
1210 Self::Jump | Self::BrIf | Self::BlockAddr | Self::IndirectBr => ExtraKind::Targets,
1211 Self::Switch => ExtraKind::Switch,
1212 Self::Call | Self::CallIndirect | Self::TailCall => ExtraKind::Call,
1213 Self::InlineAsm => ExtraKind::Asm,
1214 _ => ExtraKind::None,
1215 }
1216 }
1217}
1218
1219/// Which of [`Extra`](crate::Extra)'s shapes an instruction carries.
1220///
1221/// The same list of names, without any of the payloads, so that a question about an opcode can
1222/// be answered without an instruction to look at.
1223#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1224pub enum ExtraKind {
1225 /// Nothing.
1226 None,
1227 /// A constant.
1228 Imm,
1229 /// A name.
1230 Symbol,
1231 /// An integer comparison predicate.
1232 IntPred,
1233 /// A floating point comparison predicate.
1234 FloatPred,
1235 /// An access.
1236 Mem,
1237 /// An atomic read-modify-write.
1238 Rmw,
1239 /// A barrier's ordering.
1240 Order,
1241 /// What a prefetch is a hint about.
1242 Prefetch,
1243 /// How many frames up to walk.
1244 Depth,
1245 /// Branch targets.
1246 Targets,
1247 /// A call.
1248 Call,
1249 /// A `switch`.
1250 Switch,
1251 /// Inline assembly.
1252 Asm,
1253 /// An object read off a variable argument list.
1254 VaObject,
1255 /// What kind of storage an instance is.
1256 Class,
1257 /// Who a range of memory went to.
1258 Owner,
1259 /// A metadata node.
1260 Node,
1261 /// Why a declared exemption is there.
1262 Reason,
1263}
1264
1265impl ExtraKind {
1266 /// What it is, in words, for a message that names two of them and has to read as English.
1267 #[must_use]
1268 pub const fn name(self) -> &'static str {
1269 match self {
1270 Self::None => "nothing",
1271 Self::Imm => "a constant",
1272 Self::Symbol => "a name",
1273 Self::IntPred => "an integer comparison",
1274 Self::FloatPred => "a floating point comparison",
1275 Self::Mem => "an access",
1276 Self::Rmw => "a read-modify-write",
1277 Self::Order => "an ordering",
1278 Self::Prefetch => "a prefetch hint",
1279 Self::Depth => "a depth",
1280 Self::Targets => "branch targets",
1281 Self::Call => "a call",
1282 Self::Switch => "a switch",
1283 Self::Asm => "inline assembly",
1284 Self::VaObject => "an object off a variable argument list",
1285 Self::Class => "a storage class",
1286 Self::Owner => "an owner",
1287 Self::Node => "a metadata node",
1288 Self::Reason => "a reason",
1289 }
1290 }
1291}
1292
1293impl fmt::Display for Opcode {
1294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1295 f.write_str(self.name())
1296 }
1297}
1298
1299/// Every opcode, which is what [`Opcode::all`] hands out.
1300///
1301/// This is written out rather than derived, and the test below is what keeps it complete: it
1302/// checks the count against [`Opcode::InlineAsm`], the last variant, so a new opcode that is
1303/// not added here fails the build rather than going quietly missing from the parser.
1304static ALL: &[Opcode] = &[
1305 Opcode::IConst,
1306 Opcode::FConst,
1307 Opcode::Splat,
1308 Opcode::GlobalAddr,
1309 Opcode::BlockAddr,
1310 Opcode::Add,
1311 Opcode::Sub,
1312 Opcode::Mul,
1313 Opcode::SDiv,
1314 Opcode::UDiv,
1315 Opcode::SRem,
1316 Opcode::URem,
1317 Opcode::And,
1318 Opcode::Or,
1319 Opcode::Xor,
1320 Opcode::Shl,
1321 Opcode::LShr,
1322 Opcode::AShr,
1323 Opcode::FAdd,
1324 Opcode::FSub,
1325 Opcode::FMul,
1326 Opcode::FDiv,
1327 Opcode::FRem,
1328 Opcode::FNeg,
1329 Opcode::Fma,
1330 Opcode::ICmp,
1331 Opcode::FCmp,
1332 Opcode::Select,
1333 Opcode::Trunc,
1334 Opcode::SExt,
1335 Opcode::ZExt,
1336 Opcode::FPTrunc,
1337 Opcode::FPExt,
1338 Opcode::FPToSI,
1339 Opcode::FPToUI,
1340 Opcode::SIToFP,
1341 Opcode::UIToFP,
1342 Opcode::PtrToInt,
1343 Opcode::IntToPtr,
1344 Opcode::Bitcast,
1345 Opcode::MemEntry,
1346 Opcode::Alloca,
1347 Opcode::Load,
1348 Opcode::Store,
1349 Opcode::PtrAdd,
1350 Opcode::Memcpy,
1351 Opcode::Memmove,
1352 Opcode::Memset,
1353 Opcode::AtomicLoad,
1354 Opcode::AtomicStore,
1355 Opcode::AtomicRmw,
1356 Opcode::Cmpxchg,
1357 Opcode::Fence,
1358 Opcode::CapOf,
1359 Opcode::CapLoad,
1360 Opcode::CapStore,
1361 Opcode::CapCopy,
1362 Opcode::CapNull,
1363 Opcode::CapNarrow,
1364 Opcode::CapRecover,
1365 Opcode::CapExtent,
1366 Opcode::CapExtentBack,
1367 Opcode::CapPublish,
1368 Opcode::CapClear,
1369 Opcode::CapArg,
1370 Opcode::CapYield,
1371 Opcode::CapResult,
1372 Opcode::CheckBounds,
1373 Opcode::CheckLive,
1374 Opcode::CheckType,
1375 Opcode::CheckInit,
1376 Opcode::CheckDeriv,
1377 Opcode::CheckRace,
1378 Opcode::CheckRestrictRead,
1379 Opcode::CheckRestrictWrite,
1380 Opcode::CheckFree,
1381 Opcode::MetaBegin,
1382 Opcode::MetaEnd,
1383 Opcode::MetaType,
1384 Opcode::MetaTypeCopy,
1385 Opcode::MetaInit,
1386 Opcode::MetaInitCopy,
1387 Opcode::MetaEpoch,
1388 Opcode::MetaRelease,
1389 Opcode::MetaAcquire,
1390 Opcode::MetaFenceRelease,
1391 Opcode::MetaFenceAcquire,
1392 Opcode::MetaTransfer,
1393 Opcode::SafeRegionBegin,
1394 Opcode::SafeRegionEnd,
1395 Opcode::RestrictEnter,
1396 Opcode::RestrictLeave,
1397 Opcode::Jump,
1398 Opcode::BrIf,
1399 Opcode::Switch,
1400 Opcode::IndirectBr,
1401 Opcode::Return,
1402 Opcode::Unreachable,
1403 Opcode::Call,
1404 Opcode::CallIndirect,
1405 Opcode::TailCall,
1406 Opcode::Ctlz,
1407 Opcode::Cttz,
1408 Opcode::Ctpop,
1409 Opcode::Bswap,
1410 Opcode::Bitreverse,
1411 Opcode::SAddOverflow,
1412 Opcode::UAddOverflow,
1413 Opcode::SSubOverflow,
1414 Opcode::USubOverflow,
1415 Opcode::SMulOverflow,
1416 Opcode::UMulOverflow,
1417 Opcode::Expect,
1418 Opcode::UnreachableHint,
1419 Opcode::Trap,
1420 Opcode::Prefetch,
1421 Opcode::FrameAddress,
1422 Opcode::ReturnAddress,
1423 Opcode::ThreadPointer,
1424 Opcode::VaStart,
1425 Opcode::VaArg,
1426 Opcode::VaObject,
1427 Opcode::VaEnd,
1428 Opcode::VaCopy,
1429 Opcode::StackSave,
1430 Opcode::StackRestore,
1431 Opcode::SetjmpMarker,
1432 Opcode::LongjmpMarker,
1433 Opcode::TargetIntrinsic,
1434 Opcode::InlineAsm,
1435];
1436
1437/// The ten integer comparisons.
1438///
1439/// Signedness is on the predicate rather than on the type, for the same reason it is on
1440/// `sdiv` and `udiv`: the type space is halved and the operation says what it means.
1441#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1442pub enum IntPred {
1443 /// Equal.
1444 Eq,
1445 /// Not equal.
1446 Ne,
1447 /// Signed less than.
1448 Slt,
1449 /// Signed less than or equal.
1450 Sle,
1451 /// Signed greater than.
1452 Sgt,
1453 /// Signed greater than or equal.
1454 Sge,
1455 /// Unsigned less than.
1456 Ult,
1457 /// Unsigned less than or equal.
1458 Ule,
1459 /// Unsigned greater than.
1460 Ugt,
1461 /// Unsigned greater than or equal.
1462 Uge,
1463}
1464
1465impl IntPred {
1466 /// The textual form.
1467 #[must_use]
1468 pub const fn name(self) -> &'static str {
1469 match self {
1470 Self::Eq => "eq",
1471 Self::Ne => "ne",
1472 Self::Slt => "slt",
1473 Self::Sle => "sle",
1474 Self::Sgt => "sgt",
1475 Self::Sge => "sge",
1476 Self::Ult => "ult",
1477 Self::Ule => "ule",
1478 Self::Ugt => "ugt",
1479 Self::Uge => "uge",
1480 }
1481 }
1482
1483 /// The predicate with that name, if there is one.
1484 #[must_use]
1485 pub fn from_name(name: &str) -> Option<Self> {
1486 Self::all().find(|pred| pred.name() == name)
1487 }
1488
1489 /// Every predicate.
1490 pub fn all() -> impl Iterator<Item = Self> {
1491 [
1492 Self::Eq,
1493 Self::Ne,
1494 Self::Slt,
1495 Self::Sle,
1496 Self::Sgt,
1497 Self::Sge,
1498 Self::Ult,
1499 Self::Ule,
1500 Self::Ugt,
1501 Self::Uge,
1502 ]
1503 .into_iter()
1504 }
1505
1506 /// The predicate that holds exactly when this one does not.
1507 #[must_use]
1508 pub const fn inverse(self) -> Self {
1509 match self {
1510 Self::Eq => Self::Ne,
1511 Self::Ne => Self::Eq,
1512 Self::Slt => Self::Sge,
1513 Self::Sge => Self::Slt,
1514 Self::Sle => Self::Sgt,
1515 Self::Sgt => Self::Sle,
1516 Self::Ult => Self::Uge,
1517 Self::Uge => Self::Ult,
1518 Self::Ule => Self::Ugt,
1519 Self::Ugt => Self::Ule,
1520 }
1521 }
1522
1523 /// The predicate that holds when the operands are given the other way round.
1524 #[must_use]
1525 pub const fn swapped(self) -> Self {
1526 match self {
1527 Self::Eq => Self::Eq,
1528 Self::Ne => Self::Ne,
1529 Self::Slt => Self::Sgt,
1530 Self::Sgt => Self::Slt,
1531 Self::Sle => Self::Sge,
1532 Self::Sge => Self::Sle,
1533 Self::Ult => Self::Ugt,
1534 Self::Ugt => Self::Ult,
1535 Self::Ule => Self::Uge,
1536 Self::Uge => Self::Ule,
1537 }
1538 }
1539
1540 /// Whether this reads its operands as signed. Equality reads them as neither.
1541 #[must_use]
1542 pub const fn is_signed(self) -> bool {
1543 matches!(self, Self::Slt | Self::Sle | Self::Sgt | Self::Sge)
1544 }
1545
1546 /// The predicate that asks the same question with the bits read as unsigned.
1547 ///
1548 /// Each ordering has a counterpart the other way round and equality is the same question at
1549 /// both readings, so every predicate has one and nothing here is a refusal. What it is for is
1550 /// operands known not to be negative: the two readings agree on those, so a signed comparison
1551 /// of two of them is the unsigned comparison of them, and the unsigned one is the one that
1552 /// still holds when the same values are looked at in fewer bits.
1553 #[must_use]
1554 pub const fn unsigned(self) -> Self {
1555 match self {
1556 Self::Slt => Self::Ult,
1557 Self::Sle => Self::Ule,
1558 Self::Sgt => Self::Ugt,
1559 Self::Sge => Self::Uge,
1560 other => other,
1561 }
1562 }
1563}
1564
1565impl fmt::Display for IntPred {
1566 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1567 f.write_str(self.name())
1568 }
1569}
1570
1571/// The floating point comparisons, ordered and unordered.
1572///
1573/// An ordered predicate is false if either operand is a NaN, and an unordered one is true. C's
1574/// `<` is `olt` and C's `!=` is `une`, which is the whole of why both families are here.
1575#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1576pub enum FloatPred {
1577 /// Always false.
1578 False,
1579 /// Ordered and equal.
1580 Oeq,
1581 /// Ordered and greater than.
1582 Ogt,
1583 /// Ordered and greater than or equal.
1584 Oge,
1585 /// Ordered and less than.
1586 Olt,
1587 /// Ordered and less than or equal.
1588 Ole,
1589 /// Ordered and not equal.
1590 One,
1591 /// Ordered, which is to say neither operand is a NaN.
1592 Ord,
1593 /// Unordered, which is to say one of them is.
1594 Uno,
1595 /// Unordered or equal.
1596 Ueq,
1597 /// Unordered or greater than.
1598 Ugt,
1599 /// Unordered or greater than or equal.
1600 Uge,
1601 /// Unordered or less than.
1602 Ult,
1603 /// Unordered or less than or equal.
1604 Ule,
1605 /// Unordered or not equal.
1606 Une,
1607 /// Always true.
1608 True,
1609}
1610
1611impl FloatPred {
1612 /// The textual form.
1613 #[must_use]
1614 pub const fn name(self) -> &'static str {
1615 match self {
1616 Self::False => "false",
1617 Self::Oeq => "oeq",
1618 Self::Ogt => "ogt",
1619 Self::Oge => "oge",
1620 Self::Olt => "olt",
1621 Self::Ole => "ole",
1622 Self::One => "one",
1623 Self::Ord => "ord",
1624 Self::Uno => "uno",
1625 Self::Ueq => "ueq",
1626 Self::Ugt => "ugt",
1627 Self::Uge => "uge",
1628 Self::Ult => "ult",
1629 Self::Ule => "ule",
1630 Self::Une => "une",
1631 Self::True => "true",
1632 }
1633 }
1634
1635 /// The predicate with that name, if there is one.
1636 #[must_use]
1637 pub fn from_name(name: &str) -> Option<Self> {
1638 Self::all().find(|pred| pred.name() == name)
1639 }
1640
1641 /// Every predicate.
1642 pub fn all() -> impl Iterator<Item = Self> {
1643 [
1644 Self::False,
1645 Self::Oeq,
1646 Self::Ogt,
1647 Self::Oge,
1648 Self::Olt,
1649 Self::Ole,
1650 Self::One,
1651 Self::Ord,
1652 Self::Uno,
1653 Self::Ueq,
1654 Self::Ugt,
1655 Self::Uge,
1656 Self::Ult,
1657 Self::Ule,
1658 Self::Une,
1659 Self::True,
1660 ]
1661 .into_iter()
1662 }
1663
1664 /// The predicate that holds exactly when this one does not.
1665 #[must_use]
1666 pub const fn inverse(self) -> Self {
1667 match self {
1668 Self::False => Self::True,
1669 Self::Oeq => Self::Une,
1670 Self::Ogt => Self::Ule,
1671 Self::Oge => Self::Ult,
1672 Self::Olt => Self::Uge,
1673 Self::Ole => Self::Ugt,
1674 Self::One => Self::Ueq,
1675 Self::Ord => Self::Uno,
1676 Self::Uno => Self::Ord,
1677 Self::Ueq => Self::One,
1678 Self::Ugt => Self::Ole,
1679 Self::Uge => Self::Olt,
1680 Self::Ult => Self::Oge,
1681 Self::Ule => Self::Ogt,
1682 Self::Une => Self::Oeq,
1683 Self::True => Self::False,
1684 }
1685 }
1686
1687 /// The predicate that holds when the operands are given the other way round.
1688 #[must_use]
1689 pub const fn swapped(self) -> Self {
1690 match self {
1691 Self::Ogt => Self::Olt,
1692 Self::Olt => Self::Ogt,
1693 Self::Oge => Self::Ole,
1694 Self::Ole => Self::Oge,
1695 Self::Ugt => Self::Ult,
1696 Self::Ult => Self::Ugt,
1697 Self::Uge => Self::Ule,
1698 Self::Ule => Self::Uge,
1699 same => same,
1700 }
1701 }
1702
1703 /// Whether this is false when either operand is a NaN.
1704 ///
1705 /// [`FloatPred::False`] and [`FloatPred::True`] are neither ordered nor unordered, since
1706 /// they do not look at their operands at all, and both answer no here.
1707 #[must_use]
1708 pub const fn is_ordered(self) -> bool {
1709 matches!(
1710 self,
1711 Self::Oeq | Self::Ogt | Self::Oge | Self::Olt | Self::Ole | Self::One | Self::Ord
1712 )
1713 }
1714}
1715
1716impl fmt::Display for FloatPred {
1717 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1718 f.write_str(self.name())
1719 }
1720}
1721
1722#[cfg(test)]
1723mod tests {
1724 use super::*;
1725
1726 #[test]
1727 fn every_opcode_is_in_the_table() {
1728 // `InlineAsm` is the last variant, so its discriminant plus one is how many there are.
1729 // A new opcode declared after it moves this number, and a new opcode declared before
1730 // it and not added to `ALL` moves the length, so either mistake fails here.
1731 assert_eq!(ALL.len(), Opcode::InlineAsm as usize + 1);
1732 for (position, &op) in ALL.iter().enumerate() {
1733 assert_eq!(op as usize, position, "{op} is out of order in ALL");
1734 }
1735 }
1736
1737 #[test]
1738 fn every_opcode_name_is_one_word_the_reader_can_take() {
1739 // The textual form keeps the dot for the type suffix and the flags, so an opcode with a
1740 // dot in it reads back as a shorter opcode with a suffix that is not a type. The safety
1741 // instructions are spelled `cap_of` and not `cap.of` for this reason, and the
1742 // specification says so at `spec/safe-memory/06-instrumentation.md` section 6.2.2.
1743 for opcode in Opcode::all() {
1744 let name = opcode.name();
1745 assert!(!name.is_empty(), "an opcode with no name");
1746 assert!(
1747 name.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_'),
1748 "{name} is not one word"
1749 );
1750 }
1751 }
1752
1753 #[test]
1754 fn every_opcode_has_its_own_name_and_finds_it_again() {
1755 let mut names: Vec<&str> = Opcode::all().map(Opcode::name).collect();
1756 let total = names.len();
1757 names.sort_unstable();
1758 names.dedup();
1759 assert_eq!(names.len(), total, "two opcodes share a name");
1760 for op in Opcode::all() {
1761 assert_eq!(Opcode::from_name(op.name()), Some(op));
1762 }
1763 assert_eq!(Opcode::from_name("phi"), None);
1764 assert_eq!(Opcode::from_name("getelementptr"), None);
1765 assert_eq!(Opcode::from_name(""), None);
1766 }
1767
1768 #[test]
1769 fn the_terminators_are_the_ones_control_leaves_by() {
1770 let terminators: Vec<&str> =
1771 Opcode::all().filter(|op| op.is_terminator()).map(Opcode::name).collect();
1772 assert_eq!(
1773 terminators,
1774 ["jump", "br_if", "switch", "indirect_br", "return", "unreachable", "tail_call"]
1775 );
1776 }
1777
1778 #[test]
1779 fn a_terminator_produces_nothing() {
1780 for op in Opcode::all().filter(|op| op.is_terminator()) {
1781 assert_eq!(op.results(), Some(0), "{op}");
1782 }
1783 }
1784
1785 #[test]
1786 fn the_pair_producing_opcodes_are_the_ones_with_a_flag_beside_the_value() {
1787 let pairs: Vec<&str> =
1788 Opcode::all().filter(|op| op.results() == Some(2)).map(Opcode::name).collect();
1789 assert_eq!(
1790 pairs,
1791 [
1792 "cmpxchg",
1793 "sadd_overflow",
1794 "uadd_overflow",
1795 "ssub_overflow",
1796 "usub_overflow",
1797 "smul_overflow",
1798 "umul_overflow"
1799 ]
1800 );
1801 }
1802
1803 #[test]
1804 fn the_capability_instructions_are_the_ones_that_make_a_capability() {
1805 let makers: Vec<Opcode> = Opcode::all().filter(|op| op.makes_capability()).collect();
1806 assert_eq!(
1807 makers,
1808 vec![
1809 Opcode::CapOf,
1810 Opcode::CapLoad,
1811 Opcode::CapNull,
1812 Opcode::CapNarrow,
1813 Opcode::CapRecover,
1814 Opcode::CapArg,
1815 Opcode::CapResult
1816 ]
1817 );
1818 // The other three read a capability rather than making one. `cap_store` writes it out and
1819 // produces nothing at all, and the two extent queries answer with a number.
1820 assert!(!Opcode::CapStore.makes_capability());
1821 assert_eq!(Opcode::CapStore.results(), Some(0));
1822 assert!(!Opcode::CapExtent.makes_capability());
1823 assert_eq!(Opcode::CapExtent.results(), Some(1));
1824 assert!(!Opcode::CapExtentBack.makes_capability());
1825 assert_eq!(Opcode::CapExtentBack.results(), Some(1));
1826 for opcode in makers {
1827 assert_eq!(opcode.results(), Some(1), "{}", opcode.name());
1828 }
1829 }
1830
1831 #[test]
1832 fn a_producer_says_which_of_its_operands_is_the_pointer_it_is_about() {
1833 // Five of the seven, and the one that is not operand zero is the one whose first two
1834 // operands are the container and the word rather than the value that came out of it.
1835 assert_eq!(Opcode::CapOf.capability_names(), Some(0));
1836 assert_eq!(Opcode::CapRecover.capability_names(), Some(0));
1837 assert_eq!(Opcode::CapArg.capability_names(), Some(0));
1838 assert_eq!(Opcode::CapResult.capability_names(), Some(0));
1839 assert_eq!(Opcode::CapLoad.capability_names(), Some(2));
1840
1841 // The two that are about something other than a pointer this function has an operand for.
1842 assert_eq!(Opcode::CapNarrow.capability_names(), None);
1843 assert_eq!(Opcode::CapNull.capability_names(), None);
1844
1845 // Nothing that is not a producer answers, since the question is what a capability describes
1846 // and those have no capability to describe anything with.
1847 assert_eq!(Opcode::CapStore.capability_names(), None);
1848 assert_eq!(Opcode::Load.capability_names(), None);
1849 assert_eq!(Opcode::CheckLive.capability_names(), None);
1850 }
1851
1852 #[test]
1853 fn a_check_reads_the_planes_and_writes_nothing() {
1854 let checks = [
1855 Opcode::CheckBounds,
1856 Opcode::CheckLive,
1857 Opcode::CheckType,
1858 Opcode::CheckInit,
1859 Opcode::CheckDeriv,
1860 Opcode::CheckRace,
1861 Opcode::CheckFree,
1862 ];
1863 for opcode in checks {
1864 let name = opcode.name();
1865 // It traps, so it stays where it was put and nothing deletes it for having no
1866 // result. It reads a plane, so it takes a memory operand. It writes nothing, so
1867 // the access after it reads the version the check was given.
1868 assert!(opcode.has_effects(), "{name}");
1869 assert!(opcode.touches_memory(), "{name}");
1870 assert!(!opcode.writes_memory(), "{name}");
1871 assert_eq!(opcode.results(), Some(0), "{name}");
1872 }
1873 }
1874
1875 #[test]
1876 fn a_restrict_check_writes_memory_because_it_records_what_it_saw() {
1877 // The one place the sentence above does not hold. Every other check reads a plane and
1878 // leaves it alone, so the optimizer may hoist one out of a loop or keep the later of two
1879 // identical ones. These record the range they were asked about into the block's own slot,
1880 // so a check that ran twice saw two accesses and a check that was hoisted saw one, and
1881 // either rewrite changes what the next one answers. Saying they write memory is how the
1882 // memory chain refuses both.
1883 let recording = [
1884 Opcode::CheckRestrictRead,
1885 Opcode::CheckRestrictWrite,
1886 Opcode::RestrictEnter,
1887 Opcode::RestrictLeave,
1888 ];
1889 for opcode in recording {
1890 let name = opcode.name();
1891 assert!(opcode.has_effects(), "{name}");
1892 assert!(opcode.touches_memory(), "{name}");
1893 assert!(opcode.writes_memory(), "{name}");
1894 assert_eq!(opcode.results(), Some(0), "{name}");
1895 }
1896 }
1897
1898 #[test]
1899 fn the_capability_instructions_that_touch_memory_are_the_five_that_have_to() {
1900 // `cap_load` and `cap_store` are an access to the slot beside a pointer, and `cap_recover`
1901 // and the two extent queries read the planes. The other three are arithmetic on a
1902 // provenance the program already had, so the optimizer may treat them as it treats
1903 // `ptr_add`.
1904 assert!(!Opcode::CapOf.has_effects());
1905 assert!(!Opcode::CapNull.has_effects());
1906 assert!(!Opcode::CapNarrow.has_effects());
1907 assert!(Opcode::CapLoad.touches_memory() && !Opcode::CapLoad.writes_memory());
1908 assert!(Opcode::CapRecover.touches_memory() && !Opcode::CapRecover.writes_memory());
1909 assert!(Opcode::CapExtent.touches_memory() && !Opcode::CapExtent.writes_memory());
1910 assert!(Opcode::CapExtentBack.touches_memory() && !Opcode::CapExtentBack.writes_memory());
1911 assert!(Opcode::CapStore.writes_memory());
1912 }
1913
1914 #[test]
1915 fn what_only_touches_a_plane_touches_memory_and_is_not_an_access() {
1916 // Two halves. Everything in the list is on the memory chain, because an instruction the
1917 // chain does not carry is one the walk never sees and saying anything about it would be
1918 // saying it about nothing. And everything in the list comes from the safety lowering,
1919 // because the planes are the lowering's own storage and an opcode from somewhere else
1920 // claiming to touch only them is the claim being made about the wrong memory.
1921 for opcode in Opcode::all() {
1922 if !opcode.touches_only_planes() {
1923 continue;
1924 }
1925 let name = opcode.name();
1926 assert!(opcode.touches_memory(), "{name}");
1927 let instrumentation = name.starts_with("check_")
1928 || name.starts_with("meta_")
1929 || name.starts_with("cap_extent");
1930 assert!(instrumentation, "{name}");
1931 }
1932 for opcode in [Opcode::MetaInit, Opcode::MetaType, Opcode::CheckBounds, Opcode::CapExtent] {
1933 assert!(opcode.touches_only_planes(), "{opcode}");
1934 }
1935 }
1936
1937 #[test]
1938 fn what_goes_through_a_frame_slot_is_not_a_plane_access() {
1939 // The three families the list leaves out on purpose, and the reason is the same each time:
1940 // an operand of one of these is memory the program has a name for. The capability
1941 // instructions other than the two extent queries go through a slot in the frame, and the
1942 // `restrict` markers write their record into the block's own slot. The synchronization
1943 // edges are left out for a different reason, which is that they say something about the
1944 // ordering of program memory and not only about a plane.
1945 let outside = [
1946 Opcode::CapLoad,
1947 Opcode::CapStore,
1948 Opcode::CapCopy,
1949 Opcode::CapRecover,
1950 Opcode::CheckRestrictRead,
1951 Opcode::CheckRestrictWrite,
1952 Opcode::RestrictEnter,
1953 Opcode::RestrictLeave,
1954 Opcode::MetaRelease,
1955 Opcode::MetaAcquire,
1956 Opcode::MetaFenceRelease,
1957 Opcode::MetaFenceAcquire,
1958 ];
1959 for opcode in outside {
1960 assert!(!opcode.touches_only_planes(), "{opcode}");
1961 }
1962 // And nothing ordinary is in it either, since a store answering yes would be the whole
1963 // optimizer told that program memory is unreachable.
1964 for opcode in [Opcode::Load, Opcode::Store, Opcode::Call, Opcode::Memcpy, Opcode::Fence] {
1965 assert!(!opcode.touches_only_planes(), "{opcode}");
1966 }
1967 }
1968
1969 #[test]
1970 fn memory_has_effects_and_arithmetic_does_not() {
1971 for op in [Opcode::Load, Opcode::Store, Opcode::Call, Opcode::Alloca, Opcode::Fence] {
1972 assert!(op.has_effects(), "{op}");
1973 }
1974 for op in [Opcode::Add, Opcode::FDiv, Opcode::ICmp, Opcode::PtrAdd, Opcode::IConst] {
1975 assert!(!op.has_effects(), "{op}");
1976 }
1977 }
1978
1979 #[test]
1980 fn commuting_is_only_claimed_where_it_holds() {
1981 assert!(Opcode::Add.is_commutative());
1982 assert!(Opcode::FAdd.is_commutative());
1983 assert!(!Opcode::Sub.is_commutative());
1984 assert!(!Opcode::FDiv.is_commutative());
1985 assert!(!Opcode::Shl.is_commutative());
1986 }
1987
1988 #[test]
1989 fn an_integer_predicate_inverts_and_swaps_back_to_itself() {
1990 for pred in IntPred::all() {
1991 assert_eq!(pred.inverse().inverse(), pred);
1992 assert_eq!(pred.swapped().swapped(), pred);
1993 assert_eq!(IntPred::from_name(pred.name()), Some(pred));
1994 }
1995 assert_eq!(IntPred::Slt.inverse(), IntPred::Sge);
1996 assert_eq!(IntPred::Slt.swapped(), IntPred::Sgt);
1997 assert_eq!(IntPred::from_name("lt"), None);
1998 }
1999
2000 #[test]
2001 fn an_integer_predicate_has_an_unsigned_counterpart_that_asks_the_same_way_round() {
2002 for pred in IntPred::all() {
2003 let unsigned = pred.unsigned();
2004 assert!(!unsigned.is_signed(), "{pred}");
2005 assert_eq!(unsigned.unsigned(), unsigned, "{pred}");
2006 // The same way round, so inverting or swapping either first gives the same answer.
2007 assert_eq!(pred.inverse().unsigned(), unsigned.inverse(), "{pred}");
2008 assert_eq!(pred.swapped().unsigned(), unsigned.swapped(), "{pred}");
2009 }
2010 assert_eq!(IntPred::Slt.unsigned(), IntPred::Ult);
2011 assert_eq!(IntPred::Sge.unsigned(), IntPred::Uge);
2012 // Equality is the same question at both readings, so it is already its own counterpart.
2013 assert_eq!(IntPred::Eq.unsigned(), IntPred::Eq);
2014 assert_eq!(IntPred::Ne.unsigned(), IntPred::Ne);
2015 }
2016
2017 #[test]
2018 fn a_floating_predicate_inverts_across_the_ordered_line() {
2019 for pred in FloatPred::all() {
2020 assert_eq!(pred.inverse().inverse(), pred);
2021 assert_eq!(pred.swapped().swapped(), pred);
2022 assert_eq!(FloatPred::from_name(pred.name()), Some(pred));
2023 }
2024 // Inverting has to cross the line, because the negation of an ordered comparison is
2025 // true when an operand is a NaN. This is where `!(a < b)` stops being `a >= b`. The
2026 // two constants are outside it: neither of them looks at its operands.
2027 for pred in FloatPred::all().filter(|p| !matches!(p, FloatPred::False | FloatPred::True)) {
2028 assert_ne!(pred.is_ordered(), pred.inverse().is_ordered(), "{pred}");
2029 }
2030 assert_eq!(FloatPred::Olt.inverse(), FloatPred::Uge);
2031 assert_eq!(FloatPred::Olt.swapped(), FloatPred::Ogt);
2032 }
2033
2034 #[test]
2035 fn swapping_a_predicate_keeps_it_ordered_or_unordered() {
2036 for pred in FloatPred::all() {
2037 assert_eq!(pred.is_ordered(), pred.swapped().is_ordered(), "{pred}");
2038 }
2039 for pred in IntPred::all() {
2040 assert_eq!(pred.is_signed(), pred.swapped().is_signed(), "{pred}");
2041 }
2042 }
2043
2044 #[test]
2045 fn no_two_predicates_share_a_name_within_their_family() {
2046 for names in [
2047 IntPred::all().map(IntPred::name).collect::<Vec<_>>(),
2048 FloatPred::all().map(FloatPred::name).collect::<Vec<_>>(),
2049 ] {
2050 let total = names.len();
2051 let mut names = names;
2052 names.sort_unstable();
2053 names.dedup();
2054 assert_eq!(names.len(), total);
2055 }
2056 }
2057}