-
Notifications
You must be signed in to change notification settings - Fork 36
/
MIR.h
executable file
·13062 lines (11153 loc) · 373 KB
/
MIR.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Everything needed to build actual MIR instructions: the actual opcodes and
* instructions, the instruction interface, and use chains.
*/
#ifndef jit_MIR_h
#define jit_MIR_h
#include "mozilla/Array.h"
#include "mozilla/DebugOnly.h"
#include "builtin/SIMD.h"
#include "jit/AtomicOp.h"
#include "jit/FixedList.h"
#include "jit/InlineList.h"
#include "jit/JitAllocPolicy.h"
#include "jit/MacroAssembler.h"
#include "jit/MOpcodes.h"
#include "jit/TypedObjectPrediction.h"
#include "jit/TypePolicy.h"
#include "vm/ArrayObject.h"
#include "vm/ScopeObject.h"
#include "vm/TypedArrayCommon.h"
#include "vm/UnboxedObject.h"
// Undo windows.h damage on Win64
#undef MemoryBarrier
namespace js {
class StringObject;
namespace jit {
class BaselineInspector;
class Range;
static inline
MIRType MIRTypeFromValue(const js::Value& vp)
{
if (vp.isDouble())
return MIRType_Double;
if (vp.isMagic()) {
switch (vp.whyMagic()) {
case JS_OPTIMIZED_ARGUMENTS:
return MIRType_MagicOptimizedArguments;
case JS_OPTIMIZED_OUT:
return MIRType_MagicOptimizedOut;
case JS_ELEMENTS_HOLE:
return MIRType_MagicHole;
case JS_IS_CONSTRUCTING:
return MIRType_MagicIsConstructing;
case JS_UNINITIALIZED_LEXICAL:
return MIRType_MagicUninitializedLexical;
default:
MOZ_ASSERT(!"Unexpected magic constant");
}
}
return MIRTypeFromValueType(vp.extractNonDoubleType());
}
#define MIR_FLAG_LIST(_) \
_(InWorklist) \
_(EmittedAtUses) \
_(Commutative) \
_(Movable) /* Allow passes like LICM to move this instruction */ \
_(Lowered) /* (Debug only) has a virtual register */ \
_(Guard) /* Not removable if uses == 0 */ \
\
/* Flag an instruction to be considered as a Guard if the instructions
* bails out on some inputs.
*
* Some optimizations can replace an instruction, and leave its operands
* unused. When the type information of the operand got used as a
* predicate of the transformation, then we have to flag the operands as
* GuardRangeBailouts.
*
* This flag prevents further optimization of instructions, which
* might remove the run-time checks (bailout conditions) used as a
* predicate of the previous transformation.
*/ \
_(GuardRangeBailouts) \
\
/* Keep the flagged instruction in resume points and do not substitute this
* instruction by an UndefinedValue. This might be used by call inlining
* when a function argument is not used by the inlined instructions.
*/ \
_(ImplicitlyUsed) \
\
/* The instruction has been marked dead for lazy removal from resume
* points.
*/ \
_(Unused) \
\
/* When a branch is removed, the uses of multiple instructions are removed.
* The removal of branches is based on hypotheses. These hypotheses might
* fail, in which case we need to bailout from the current code.
*
* When we implement a destructive optimization, we need to consider the
* failing cases, and consider the fact that we might resume the execution
* into a branch which was removed from the compiler. As such, a
* destructive optimization need to take into acount removed branches.
*
* In order to let destructive optimizations know about removed branches, we
* have to annotate instructions with the UseRemoved flag. This flag
* annotates instruction which were used in removed branches.
*/ \
_(UseRemoved) \
\
/* Marks if the current instruction should go to the bailout paths instead
* of producing code as part of the control flow. This flag can only be set
* on instructions which are only used by ResumePoint or by other flagged
* instructions.
*/ \
_(RecoveredOnBailout) \
\
/* Some instructions might represent an object, but the memory of these
* objects might be incomplete if we have not recovered all the stores which
* were supposed to happen before. This flag is used to annotate
* instructions which might return a pointer to a memory area which is not
* yet fully initialized. This flag is used to ensure that stores are
* executed before returning the value.
*/ \
_(IncompleteObject) \
\
/* The current instruction got discarded from the MIR Graph. This is useful
* when we want to iterate over resume points and instructions, while
* handling instructions which are discarded without reporting to the
* iterator.
*/ \
_(Discarded)
class MDefinition;
class MInstruction;
class MBasicBlock;
class MNode;
class MUse;
class MPhi;
class MIRGraph;
class MResumePoint;
class MControlInstruction;
// Represents a use of a node.
class MUse : public TempObject, public InlineListNode<MUse>
{
// Grant access to setProducerUnchecked.
friend class MDefinition;
friend class MPhi;
MDefinition* producer_; // MDefinition that is being used.
MNode* consumer_; // The node that is using this operand.
// Low-level unchecked edit method for replaceAllUsesWith and
// MPhi::removeOperand. This doesn't update use lists!
// replaceAllUsesWith and MPhi::removeOperand do that manually.
void setProducerUnchecked(MDefinition* producer) {
MOZ_ASSERT(consumer_);
MOZ_ASSERT(producer_);
MOZ_ASSERT(producer);
producer_ = producer;
}
public:
// Default constructor for use in vectors.
MUse()
: producer_(nullptr), consumer_(nullptr)
{ }
// Move constructor for use in vectors. When an MUse is moved, it stays
// in its containing use list.
MUse(MUse&& other)
: InlineListNode<MUse>(mozilla::Move(other)),
producer_(other.producer_), consumer_(other.consumer_)
{ }
// Construct an MUse initialized with |producer| and |consumer|.
MUse(MDefinition* producer, MNode* consumer)
{
initUnchecked(producer, consumer);
}
// Set this use, which was previously clear.
inline void init(MDefinition* producer, MNode* consumer);
// Like init, but works even when the use contains uninitialized data.
inline void initUnchecked(MDefinition* producer, MNode* consumer);
// Like initUnchecked, but set the producer to nullptr.
inline void initUncheckedWithoutProducer(MNode* consumer);
// Set this use, which was not previously clear.
inline void replaceProducer(MDefinition* producer);
// Clear this use.
inline void releaseProducer();
MDefinition* producer() const {
MOZ_ASSERT(producer_ != nullptr);
return producer_;
}
bool hasProducer() const {
return producer_ != nullptr;
}
MNode* consumer() const {
MOZ_ASSERT(consumer_ != nullptr);
return consumer_;
}
#ifdef DEBUG
// Return the operand index of this MUse in its consumer. This is DEBUG-only
// as normal code should instead to call indexOf on the casted consumer
// directly, to allow it to be devirtualized and inlined.
size_t index() const;
#endif
};
typedef InlineList<MUse>::iterator MUseIterator;
// A node is an entry in the MIR graph. It has two kinds:
// MInstruction: an instruction which appears in the IR stream.
// MResumePoint: a list of instructions that correspond to the state of the
// interpreter/Baseline stack.
//
// Nodes can hold references to MDefinitions. Each MDefinition has a list of
// nodes holding such a reference (its use chain).
class MNode : public TempObject
{
protected:
MBasicBlock* block_; // Containing basic block.
public:
enum Kind {
Definition,
ResumePoint
};
MNode()
: block_(nullptr)
{ }
explicit MNode(MBasicBlock* block)
: block_(block)
{ }
virtual Kind kind() const = 0;
// Returns the definition at a given operand.
virtual MDefinition* getOperand(size_t index) const = 0;
virtual size_t numOperands() const = 0;
virtual size_t indexOf(const MUse* u) const = 0;
bool isDefinition() const {
return kind() == Definition;
}
bool isResumePoint() const {
return kind() == ResumePoint;
}
MBasicBlock* block() const {
return block_;
}
MBasicBlock *caller() const;
// Sets an already set operand, updating use information. If you're looking
// for setOperand, this is probably what you want.
virtual void replaceOperand(size_t index, MDefinition* operand) = 0;
// Resets the operand to an uninitialized state, breaking the link
// with the previous operand's producer.
void releaseOperand(size_t index) {
getUseFor(index)->releaseProducer();
}
bool hasOperand(size_t index) const {
return getUseFor(index)->hasProducer();
}
inline MDefinition* toDefinition();
inline MResumePoint* toResumePoint();
virtual bool writeRecoverData(CompactBufferWriter& writer) const;
virtual void dump(FILE* fp) const = 0;
virtual void dump() const = 0;
protected:
// Need visibility on getUseFor to avoid O(n^2) complexity.
friend void AssertBasicGraphCoherency(MIRGraph& graph);
// Gets the MUse corresponding to given operand.
virtual MUse* getUseFor(size_t index) = 0;
virtual const MUse* getUseFor(size_t index) const = 0;
};
class AliasSet {
private:
uint32_t flags_;
public:
enum Flag {
None_ = 0,
ObjectFields = 1 << 0, // shape, class, slots, length etc.
Element = 1 << 1, // A Value member of obj->elements or
// a typed object.
UnboxedElement = 1 << 2, // An unboxed scalar or reference member of
// a typed array, typed object, or unboxed
// object.
DynamicSlot = 1 << 3, // A Value member of obj->slots.
FixedSlot = 1 << 4, // A Value member of obj->fixedSlots().
DOMProperty = 1 << 5, // A DOM property
FrameArgument = 1 << 6, // An argument kept on the stack frame
AsmJSGlobalVar = 1 << 7, // An asm.js global var
AsmJSHeap = 1 << 8, // An asm.js heap load
TypedArrayLength = 1 << 9,// A typed array's length
Last = TypedArrayLength,
Any = Last | (Last - 1),
NumCategories = 10,
// Indicates load or store.
Store_ = 1 << 31
};
static_assert((1 << NumCategories) - 1 == Any,
"NumCategories must include all flags present in Any");
explicit AliasSet(uint32_t flags)
: flags_(flags)
{
}
public:
inline bool isNone() const {
return flags_ == None_;
}
uint32_t flags() const {
return flags_ & Any;
}
inline bool isStore() const {
return !!(flags_ & Store_);
}
inline bool isLoad() const {
return !isStore() && !isNone();
}
inline AliasSet operator |(const AliasSet& other) const {
return AliasSet(flags_ | other.flags_);
}
inline AliasSet operator&(const AliasSet& other) const {
return AliasSet(flags_ & other.flags_);
}
static AliasSet None() {
return AliasSet(None_);
}
static AliasSet Load(uint32_t flags) {
MOZ_ASSERT(flags && !(flags & Store_));
return AliasSet(flags);
}
static AliasSet Store(uint32_t flags) {
MOZ_ASSERT(flags && !(flags & Store_));
return AliasSet(flags | Store_);
}
};
// An MDefinition is an SSA name.
class MDefinition : public MNode
{
friend class MBasicBlock;
public:
enum Opcode {
# define DEFINE_OPCODES(op) Op_##op,
MIR_OPCODE_LIST(DEFINE_OPCODES)
# undef DEFINE_OPCODES
Op_Invalid
};
private:
InlineList<MUse> uses_; // Use chain.
uint32_t id_; // Instruction ID, which after block re-ordering
// is sorted within a basic block.
uint32_t flags_; // Bit flags.
Range* range_; // Any computed range for this def.
MIRType resultType_; // Representation of result type.
TemporaryTypeSet* resultTypeSet_; // Optional refinement of the result type.
union {
MInstruction* dependency_; // Implicit dependency (store, call, etc.) of this instruction.
// Used by alias analysis, GVN and LICM.
uint32_t virtualRegister_; // Used by lowering to map definitions to virtual registers.
};
// Track bailouts by storing the current pc in MIR instruction. Also used
// for profiling and keeping track of what the last known pc was.
const BytecodeSite* trackedSite_;
private:
enum Flag {
None = 0,
# define DEFINE_FLAG(flag) flag,
MIR_FLAG_LIST(DEFINE_FLAG)
# undef DEFINE_FLAG
Total
};
bool hasFlags(uint32_t flags) const {
return (flags_ & flags) == flags;
}
void removeFlags(uint32_t flags) {
flags_ &= ~flags;
}
void setFlags(uint32_t flags) {
flags_ |= flags;
}
protected:
virtual void setBlock(MBasicBlock* block) {
block_ = block;
}
static HashNumber addU32ToHash(HashNumber hash, uint32_t data);
public:
MDefinition()
: id_(0),
flags_(0),
range_(nullptr),
resultType_(MIRType_None),
resultTypeSet_(nullptr),
dependency_(nullptr),
trackedSite_(nullptr)
{ }
// Copying a definition leaves the list of uses and the block empty.
explicit MDefinition(const MDefinition& other)
: id_(0),
flags_(other.flags_),
range_(other.range_),
resultType_(other.resultType_),
resultTypeSet_(other.resultTypeSet_),
dependency_(other.dependency_),
trackedSite_(other.trackedSite_)
{ }
virtual Opcode op() const = 0;
virtual const char* opName() const = 0;
virtual void accept(MDefinitionVisitor* visitor) = 0;
void printName(FILE* fp) const;
static void PrintOpcodeName(FILE* fp, Opcode op);
virtual void printOpcode(FILE* fp) const;
void dump(FILE* fp) const override;
void dump() const override;
void dumpLocation(FILE* fp) const;
void dumpLocation() const;
// For LICM.
virtual bool neverHoist() const { return false; }
// Also for LICM. Test whether this definition is likely to be a call, which
// would clobber all or many of the floating-point registers, such that
// hoisting floating-point constants out of containing loops isn't likely to
// be worthwhile.
virtual bool possiblyCalls() const { return false; }
void setTrackedSite(const BytecodeSite* site) {
MOZ_ASSERT(site);
trackedSite_ = site;
}
const BytecodeSite* trackedSite() const {
return trackedSite_;
}
jsbytecode* trackedPc() const {
return trackedSite_ ? trackedSite_->pc() : nullptr;
}
InlineScriptTree* trackedTree() const {
return trackedSite_ ? trackedSite_->tree() : nullptr;
}
TrackedOptimizations* trackedOptimizations() const {
return trackedSite_ && trackedSite_->hasOptimizations()
? trackedSite_->optimizations()
: nullptr;
}
JSScript* profilerLeaveScript() const {
return trackedTree()->outermostCaller()->script();
}
jsbytecode* profilerLeavePc() const {
// If this is in a top-level function, use the pc directly.
if (trackedTree()->isOutermostCaller())
return trackedPc();
// Walk up the InlineScriptTree chain to find the top-most callPC
InlineScriptTree* curTree = trackedTree();
InlineScriptTree* callerTree = curTree->caller();
while (!callerTree->isOutermostCaller()) {
curTree = callerTree;
callerTree = curTree->caller();
}
// Return the callPc of the topmost inlined script.
return curTree->callerPc();
}
// Return the range of this value, *before* any bailout checks. Contrast
// this with the type() method, and the Range constructor which takes an
// MDefinition*, which describe the value *after* any bailout checks.
//
// Warning: Range analysis is removing the bit-operations such as '| 0' at
// the end of the transformations. Using this function to analyse any
// operands after the truncate phase of the range analysis will lead to
// errors. Instead, one should define the collectRangeInfoPreTrunc() to set
// the right set of flags which are dependent on the range of the inputs.
Range* range() const {
MOZ_ASSERT(type() != MIRType_None);
return range_;
}
void setRange(Range* range) {
MOZ_ASSERT(type() != MIRType_None);
range_ = range;
}
virtual HashNumber valueHash() const;
virtual bool congruentTo(const MDefinition* ins) const {
return false;
}
bool congruentIfOperandsEqual(const MDefinition* ins) const;
virtual MDefinition* foldsTo(TempAllocator& alloc);
virtual void analyzeEdgeCasesForward();
virtual void analyzeEdgeCasesBackward();
// When a floating-point value is used by nodes which would prefer to
// recieve integer inputs, we may be able to help by computing our result
// into an integer directly.
//
// A value can be truncated in 4 differents ways:
// 1. Ignore Infinities (x / 0 --> 0).
// 2. Ignore overflow (INT_MIN / -1 == (INT_MAX + 1) --> INT_MIN)
// 3. Ignore negative zeros. (-0 --> 0)
// 4. Ignore remainder. (3 / 4 --> 0)
//
// Indirect truncation is used to represent that we are interested in the
// truncated result, but only if it can safely flow into operations which
// are computed modulo 2^32, such as (2) and (3). Infinities are not safe,
// as they would have absorbed other math operations. Remainders are not
// safe, as fractions can be scaled up by multiplication.
//
// Division is a particularly interesting node here because it covers all 4
// cases even when its own operands are integers.
//
// Note that these enum values are ordered from least value-modifying to
// most value-modifying, and code relies on this ordering.
enum TruncateKind {
// No correction.
NoTruncate = 0,
// An integer is desired, but we can't skip bailout checks.
TruncateAfterBailouts = 1,
// The value will be truncated after some arithmetic (see above).
IndirectTruncate = 2,
// Direct and infallible truncation to int32.
Truncate = 3
};
// |needTruncation| records the truncation kind of the results, such that it
// can be used to truncate the operands of this instruction. If
// |needTruncation| function returns true, then the |truncate| function is
// called on the same instruction to mutate the instruction, such as
// updating the return type, the range and the specialization of the
// instruction.
virtual bool needTruncation(TruncateKind kind);
virtual void truncate();
// Determine what kind of truncate this node prefers for the operand at the
// given index.
virtual TruncateKind operandTruncateKind(size_t index) const;
// Compute an absolute or symbolic range for the value of this node.
virtual void computeRange(TempAllocator& alloc) {
}
// Collect information from the pre-truncated ranges.
virtual void collectRangeInfoPreTrunc() {
}
MNode::Kind kind() const override {
return MNode::Definition;
}
uint32_t id() const {
MOZ_ASSERT(block_);
return id_;
}
void setId(uint32_t id) {
id_ = id;
}
#define FLAG_ACCESSOR(flag) \
bool is##flag() const {\
return hasFlags(1 << flag);\
}\
void set##flag() {\
MOZ_ASSERT(!hasFlags(1 << flag));\
setFlags(1 << flag);\
}\
void setNot##flag() {\
MOZ_ASSERT(hasFlags(1 << flag));\
removeFlags(1 << flag);\
}\
void set##flag##Unchecked() {\
setFlags(1 << flag);\
} \
void setNot##flag##Unchecked() {\
removeFlags(1 << flag);\
}
MIR_FLAG_LIST(FLAG_ACCESSOR)
#undef FLAG_ACCESSOR
// Return the type of this value. This may be speculative, and enforced
// dynamically with the use of bailout checks. If all the bailout checks
// pass, the value will have this type.
//
// Unless this is an MUrsh that has bailouts disabled, which, as a special
// case, may return a value in (INT32_MAX,UINT32_MAX] even when its type()
// is MIRType_Int32.
MIRType type() const {
return resultType_;
}
TemporaryTypeSet* resultTypeSet() const {
return resultTypeSet_;
}
bool emptyResultTypeSet() const;
bool mightBeType(MIRType type) const {
MOZ_ASSERT(type != MIRType_Value);
MOZ_ASSERT(type != MIRType_ObjectOrNull);
if (type == this->type())
return true;
if (this->type() == MIRType_ObjectOrNull)
return type == MIRType_Object || type == MIRType_Null;
if (this->type() == MIRType_Value)
return !resultTypeSet() || resultTypeSet()->mightBeMIRType(type);
return false;
}
bool mightBeMagicType() const;
// Float32 specialization operations (see big comment in IonAnalysis before the Float32
// specialization algorithm).
virtual bool isFloat32Commutative() const { return false; }
virtual bool canProduceFloat32() const { return false; }
virtual bool canConsumeFloat32(MUse* use) const { return false; }
virtual void trySpecializeFloat32(TempAllocator& alloc) {}
#ifdef DEBUG
// Used during the pass that checks that Float32 flow into valid MDefinitions
virtual bool isConsistentFloat32Use(MUse* use) const {
return type() == MIRType_Float32 || canConsumeFloat32(use);
}
#endif
// Returns the beginning of this definition's use chain.
MUseIterator usesBegin() const {
return uses_.begin();
}
// Returns the end of this definition's use chain.
MUseIterator usesEnd() const {
return uses_.end();
}
bool canEmitAtUses() const {
return !isEmittedAtUses();
}
// Removes a use at the given position
void removeUse(MUse* use) {
uses_.remove(use);
}
#ifdef DEBUG
// Number of uses of this instruction. This function is only available
// in DEBUG mode since it requires traversing the list. Most users should
// use hasUses() or hasOneUse() instead.
size_t useCount() const;
// Number of uses of this instruction (only counting MDefinitions, ignoring
// MResumePoints). This function is only available in DEBUG mode since it
// requires traversing the list. Most users should use hasUses() or
// hasOneUse() instead.
size_t defUseCount() const;
#endif
// Test whether this MDefinition has exactly one use.
bool hasOneUse() const;
// Test whether this MDefinition has exactly one use.
// (only counting MDefinitions, ignoring MResumePoints)
bool hasOneDefUse() const;
// Test whether this MDefinition has at least one use.
// (only counting MDefinitions, ignoring MResumePoints)
bool hasDefUses() const;
// Test whether this MDefinition has at least one non-recovered use.
// (only counting MDefinitions, ignoring MResumePoints)
bool hasLiveDefUses() const;
bool hasUses() const {
return !uses_.empty();
}
void addUse(MUse* use) {
MOZ_ASSERT(use->producer() == this);
uses_.pushFront(use);
}
void addUseUnchecked(MUse* use) {
MOZ_ASSERT(use->producer() == this);
uses_.pushFrontUnchecked(use);
}
void replaceUse(MUse* old, MUse* now) {
MOZ_ASSERT(now->producer() == this);
uses_.replace(old, now);
}
// Replace the current instruction by a dominating instruction |dom| in all
// uses of the current instruction.
void replaceAllUsesWith(MDefinition* dom);
// Like replaceAllUsesWith, but doesn't set UseRemoved on |this|'s operands.
void justReplaceAllUsesWith(MDefinition* dom);
// Replace the current instruction by an optimized-out constant in all uses
// of the current instruction. Note, that optimized-out constant should not
// be observed, and thus they should not flow in any computation.
void optimizeOutAllUses(TempAllocator& alloc);
// Mark this instruction as having replaced all uses of ins, as during GVN,
// returning false if the replacement should not be performed. For use when
// GVN eliminates instructions which are not equivalent to one another.
virtual bool updateForReplacement(MDefinition* ins) {
return true;
}
void setVirtualRegister(uint32_t vreg) {
virtualRegister_ = vreg;
#ifdef DEBUG
setLoweredUnchecked();
#endif
}
uint32_t virtualRegister() const {
MOZ_ASSERT(isLowered());
return virtualRegister_;
}
public:
// Opcode testing and casts.
template<typename MIRType> bool is() const {
return op() == MIRType::classOpcode;
}
template<typename MIRType> MIRType* to() {
MOZ_ASSERT(this->is<MIRType>());
return static_cast<MIRType*>(this);
}
template<typename MIRType> const MIRType* to() const {
MOZ_ASSERT(this->is<MIRType>());
return static_cast<const MIRType*>(this);
}
# define OPCODE_CASTS(opcode) \
bool is##opcode() const { \
return this->is<M##opcode>(); \
} \
M##opcode* to##opcode() { \
return this->to<M##opcode>(); \
} \
const M##opcode* to##opcode() const { \
return this->to<M##opcode>(); \
}
MIR_OPCODE_LIST(OPCODE_CASTS)
# undef OPCODE_CASTS
bool isConstantValue() {
return isConstant() || (isBox() && getOperand(0)->isConstant());
}
const Value& constantValue();
const Value* constantVp();
bool constantToBoolean();
inline MInstruction* toInstruction();
inline const MInstruction* toInstruction() const;
bool isInstruction() const {
return !isPhi();
}
virtual bool isControlInstruction() const {
return false;
}
inline MControlInstruction* toControlInstruction();
void setResultType(MIRType type) {
resultType_ = type;
}
void setResultTypeSet(TemporaryTypeSet* types) {
resultTypeSet_ = types;
}
MInstruction* dependency() const {
return dependency_;
}
void setDependency(MInstruction* dependency) {
dependency_ = dependency;
}
virtual AliasSet getAliasSet() const {
// Instructions are effectful by default.
return AliasSet::Store(AliasSet::Any);
}
bool isEffectful() const {
return getAliasSet().isStore();
}
#ifdef DEBUG
virtual bool needsResumePoint() const {
// Return whether this instruction should have its own resume point.
return isEffectful();
}
#endif
virtual bool mightAlias(const MDefinition* store) const {
// Return whether this load may depend on the specified store, given
// that the alias sets intersect. This may be refined to exclude
// possible aliasing in cases where alias set flags are too imprecise.
MOZ_ASSERT(!isEffectful() && store->isEffectful());
MOZ_ASSERT(getAliasSet().flags() & store->getAliasSet().flags());
return true;
}
virtual bool canRecoverOnBailout() const {
return false;
}
};
// An MUseDefIterator walks over uses in a definition, skipping any use that is
// not a definition. Items from the use list must not be deleted during
// iteration.
class MUseDefIterator
{
MDefinition* def_;
MUseIterator current_;
MUseIterator search(MUseIterator start) {
MUseIterator i(start);
for (; i != def_->usesEnd(); i++) {
if (i->consumer()->isDefinition())
return i;
}
return def_->usesEnd();
}
public:
explicit MUseDefIterator(MDefinition* def)
: def_(def),
current_(search(def->usesBegin()))
{ }
operator bool() const {
return current_ != def_->usesEnd();
}
MUseDefIterator operator ++() {
MOZ_ASSERT(current_ != def_->usesEnd());
++current_;
current_ = search(current_);
return *this;
}
MUseDefIterator operator ++(int) {
MUseDefIterator old(*this);
operator++();
return old;
}
MUse* use() const {
return *current_;
}
MDefinition* def() const {
return current_->consumer()->toDefinition();
}
};
typedef Vector<MDefinition*, 8, JitAllocPolicy> MDefinitionVector;
typedef Vector<MInstruction*, 6, JitAllocPolicy> MInstructionVector;
// An instruction is an SSA name that is inserted into a basic block's IR
// stream.
class MInstruction
: public MDefinition,
public InlineListNode<MInstruction>
{
MResumePoint* resumePoint_;
public:
MInstruction()
: resumePoint_(nullptr)
{ }
// Copying an instruction leaves the block and resume point as empty.
explicit MInstruction(const MInstruction& other)
: MDefinition(other),
resumePoint_(nullptr)
{ }
// Convenient function used for replacing a load by the value of the store
// if the types are match, and boxing the value if they do not match.
//
// Note: There is no need for such function in AsmJS functions as they do
// not use any MIRType_Value.
MDefinition* foldsToStoredValue(TempAllocator& alloc, MDefinition* loaded);
void setResumePoint(MResumePoint* resumePoint);
// Used to transfer the resume point to the rewritten instruction.
void stealResumePoint(MInstruction* ins);
void moveResumePointAsEntry();
void clearResumePoint();
MResumePoint* resumePoint() const {
return resumePoint_;
}
// For instructions which can be cloned with new inputs, with all other
// information being the same. clone() implementations do not need to worry
// about cloning generic MInstruction/MDefinition state like flags and
// resume points.
virtual bool canClone() const {
return false;
}
virtual MInstruction* clone(TempAllocator& alloc, const MDefinitionVector& inputs) const {
MOZ_CRASH();
}
// Instructions needing to hook into type analysis should return a
// TypePolicy.
virtual TypePolicy* typePolicy() = 0;
virtual MIRType typePolicySpecialization() = 0;
};
#define INSTRUCTION_HEADER_WITHOUT_TYPEPOLICY(opcode) \
static const Opcode classOpcode = MDefinition::Op_##opcode; \
Opcode op() const override { \
return classOpcode; \
} \
const char* opName() const override { \
return #opcode; \
} \
void accept(MDefinitionVisitor* visitor) override { \
visitor->visit##opcode(this); \
}
#define INSTRUCTION_HEADER(opcode) \
INSTRUCTION_HEADER_WITHOUT_TYPEPOLICY(opcode) \
virtual TypePolicy* typePolicy() override; \
virtual MIRType typePolicySpecialization() override;
#define ALLOW_CLONE(typename) \
bool canClone() const override { \
return true; \
} \
MInstruction* clone(TempAllocator& alloc, \
const MDefinitionVector& inputs) const override { \
MInstruction* res = new(alloc) typename(*this); \
for (size_t i = 0; i < numOperands(); i++) \
res->replaceOperand(i, inputs[i]); \
return res; \
}
template <size_t Arity>
class MAryInstruction : public MInstruction
{
mozilla::Array<MUse, Arity> operands_;
protected:
MUse* getUseFor(size_t index) final override {
return &operands_[index];
}
const MUse* getUseFor(size_t index) const final override {
return &operands_[index];
}
void initOperand(size_t index, MDefinition* operand) {
operands_[index].init(operand, this);
}
public:
MDefinition* getOperand(size_t index) const final override {
return operands_[index].producer();
}
size_t numOperands() const final override {
return Arity;
}
size_t indexOf(const MUse* u) const final override {
MOZ_ASSERT(u >= &operands_[0]);
MOZ_ASSERT(u <= &operands_[numOperands() - 1]);
return u - &operands_[0];
}