aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/IntermediateRepresentation/Operation.cs
blob: bc3a71b31a771c8ccdb9bbd0d98a3b9ef79d7cde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace ARMeilleure.IntermediateRepresentation
{
    unsafe struct Operation : IEquatable<Operation>, IIntrusiveListNode<Operation>
    {
        internal struct Data
        {
            public ushort Instruction;
            public ushort Intrinsic;
            public ushort SourcesCount;
            public ushort DestinationsCount;
            public Operation ListPrevious;
            public Operation ListNext;
            public Operand* Destinations;
            public Operand* Sources;
        }

        private Data* _data;

        public readonly Instruction Instruction
        {
            get => (Instruction)_data->Instruction;
            private set => _data->Instruction = (ushort)value;
        }

        public readonly Intrinsic Intrinsic
        {
            get => (Intrinsic)_data->Intrinsic;
            private set => _data->Intrinsic = (ushort)value;
        }

        public readonly Operation ListPrevious
        {
            get => _data->ListPrevious;
            set => _data->ListPrevious = value;
        }

        public readonly Operation ListNext
        {
            get => _data->ListNext;
            set => _data->ListNext = value;
        }

        public readonly Operand Destination
        {
            get => _data->DestinationsCount != 0 ? GetDestination(0) : default;
            set => SetDestination(value);
        }

        public readonly int DestinationsCount => _data->DestinationsCount;
        public readonly int SourcesCount => _data->SourcesCount;

        internal readonly Span<Operand> DestinationsUnsafe => new(_data->Destinations, _data->DestinationsCount);
        internal readonly Span<Operand> SourcesUnsafe => new(_data->Sources, _data->SourcesCount);

        public readonly PhiOperation AsPhi()
        {
            Debug.Assert(Instruction == Instruction.Phi);

            return new PhiOperation(this);
        }

        public readonly Operand GetDestination(int index)
        {
            return DestinationsUnsafe[index];
        }

        public readonly Operand GetSource(int index)
        {
            return SourcesUnsafe[index];
        }

        public readonly void SetDestination(int index, Operand dest)
        {
            ref Operand curDest = ref DestinationsUnsafe[index];

            RemoveAssignment(curDest);
            AddAssignment(dest);

            curDest = dest;
        }

        public readonly void SetSource(int index, Operand src)
        {
            ref Operand curSrc = ref SourcesUnsafe[index];

            RemoveUse(curSrc);
            AddUse(src);

            curSrc = src;
        }

        private readonly void RemoveOldDestinations()
        {
            for (int i = 0; i < _data->DestinationsCount; i++)
            {
                RemoveAssignment(_data->Destinations[i]);
            }
        }

        public readonly void SetDestination(Operand dest)
        {
            RemoveOldDestinations();

            if (dest == default)
            {
                _data->DestinationsCount = 0;
            }
            else
            {
                EnsureCapacity(ref _data->Destinations, ref _data->DestinationsCount, 1);

                _data->Destinations[0] = dest;

                AddAssignment(dest);
            }
        }

        public readonly void SetDestinations(Operand[] dests)
        {
            RemoveOldDestinations();

            EnsureCapacity(ref _data->Destinations, ref _data->DestinationsCount, dests.Length);

            for (int index = 0; index < dests.Length; index++)
            {
                Operand newOp = dests[index];

                _data->Destinations[index] = newOp;

                AddAssignment(newOp);
            }
        }

        private readonly void RemoveOldSources()
        {
            for (int index = 0; index < _data->SourcesCount; index++)
            {
                RemoveUse(_data->Sources[index]);
            }
        }

        public readonly void SetSource(Operand src)
        {
            RemoveOldSources();

            if (src == default)
            {
                _data->SourcesCount = 0;
            }
            else
            {
                EnsureCapacity(ref _data->Sources, ref _data->SourcesCount, 1);

                _data->Sources[0] = src;

                AddUse(src);
            }
        }

        public readonly void SetSources(Operand[] srcs)
        {
            RemoveOldSources();

            EnsureCapacity(ref _data->Sources, ref _data->SourcesCount, srcs.Length);

            for (int index = 0; index < srcs.Length; index++)
            {
                Operand newOp = srcs[index];

                _data->Sources[index] = newOp;

                AddUse(newOp);
            }
        }

        public void TurnIntoCopy(Operand source)
        {
            Instruction = Instruction.Copy;

            SetSource(source);
        }

        private readonly void AddAssignment(Operand op)
        {
            if (op != default)
            {
                op.AddAssignment(this);
            }
        }

        private readonly void RemoveAssignment(Operand op)
        {
            if (op != default)
            {
                op.RemoveAssignment(this);
            }
        }

        private readonly void AddUse(Operand op)
        {
            if (op != default)
            {
                op.AddUse(this);
            }
        }

        private readonly void RemoveUse(Operand op)
        {
            if (op != default)
            {
                op.RemoveUse(this);
            }
        }

        public readonly bool Equals(Operation operation)
        {
            return operation._data == _data;
        }

        public readonly override bool Equals(object obj)
        {
            return obj is Operation operation && Equals(operation);
        }

        public readonly override int GetHashCode()
        {
            return HashCode.Combine((IntPtr)_data);
        }

        public static bool operator ==(Operation a, Operation b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(Operation a, Operation b)
        {
            return !a.Equals(b);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static void EnsureCapacity(ref Operand* list, ref ushort capacity, int newCapacity)
        {
            if (newCapacity > ushort.MaxValue)
            {
                ThrowOverflow(newCapacity);
            }
            // We only need to allocate a new buffer if we're increasing the size.
            else if (newCapacity > capacity)
            {
                list = Allocators.References.Allocate<Operand>((uint)newCapacity);
            }

            capacity = (ushort)newCapacity;
        }

        private static void ThrowOverflow(int count) =>
            throw new OverflowException($"Exceeded maximum size for Source or Destinations. Required {count}.");

        public static class Factory
        {
            private static Operation Make(Instruction inst, int destCount, int srcCount)
            {
                Data* data = Allocators.Operations.Allocate<Data>();
                *data = default;

                Operation result = new()
                {
                    _data = data,
                    Instruction = inst,
                };

                EnsureCapacity(ref result._data->Destinations, ref result._data->DestinationsCount, destCount);
                EnsureCapacity(ref result._data->Sources, ref result._data->SourcesCount, srcCount);

                result.DestinationsUnsafe.Clear();
                result.SourcesUnsafe.Clear();

                return result;
            }

            public static Operation Operation(Instruction inst, Operand dest)
            {
                Operation result = Make(inst, 0, 0);
                result.SetDestination(dest);
                return result;
            }

            public static Operation Operation(Instruction inst, Operand dest, Operand src0)
            {
                Operation result = Make(inst, 0, 1);
                result.SetDestination(dest);
                result.SetSource(0, src0);
                return result;
            }

            public static Operation Operation(Instruction inst, Operand dest, Operand src0, Operand src1)
            {
                Operation result = Make(inst, 0, 2);
                result.SetDestination(dest);
                result.SetSource(0, src0);
                result.SetSource(1, src1);
                return result;
            }

            public static Operation Operation(Instruction inst, Operand dest, Operand src0, Operand src1, Operand src2)
            {
                Operation result = Make(inst, 0, 3);
                result.SetDestination(dest);
                result.SetSource(0, src0);
                result.SetSource(1, src1);
                result.SetSource(2, src2);
                return result;
            }

            public static Operation Operation(Instruction inst, Operand dest, int srcCount)
            {
                Operation result = Make(inst, 0, srcCount);
                result.SetDestination(dest);
                return result;
            }

            public static Operation Operation(Instruction inst, Operand dest, Operand[] srcs)
            {
                Operation result = Make(inst, 0, srcs.Length);

                result.SetDestination(dest);

                for (int index = 0; index < srcs.Length; index++)
                {
                    result.SetSource(index, srcs[index]);
                }

                return result;
            }

            public static Operation Operation(Intrinsic intrin, Operand dest, params Operand[] srcs)
            {
                Operation result = Make(Instruction.Extended, 0, srcs.Length);

                result.Intrinsic = intrin;
                result.SetDestination(dest);

                for (int index = 0; index < srcs.Length; index++)
                {
                    result.SetSource(index, srcs[index]);
                }

                return result;
            }

            public static Operation Operation(Instruction inst, Operand[] dests, Operand[] srcs)
            {
                Operation result = Make(inst, dests.Length, srcs.Length);

                for (int index = 0; index < dests.Length; index++)
                {
                    result.SetDestination(index, dests[index]);
                }

                for (int index = 0; index < srcs.Length; index++)
                {
                    result.SetSource(index, srcs[index]);
                }

                return result;
            }

            public static Operation PhiOperation(Operand dest, int srcCount)
            {
                return Operation(Instruction.Phi, dest, srcCount * 2);
            }
        }
    }
}