]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/Instruction.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / Instruction.h
1 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Instruction class, which is the
11 // base class for all of the LLVM instructions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_INSTRUCTION_H
16 #define LLVM_IR_INSTRUCTION_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/ilist_node.h"
22 #include "llvm/IR/DebugLoc.h"
23 #include "llvm/IR/SymbolTableListTraits.h"
24 #include "llvm/IR/User.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <cstdint>
30 #include <utility>
31
32 namespace llvm {
33
34 class BasicBlock;
35 class FastMathFlags;
36 class MDNode;
37 class Module;
38 struct AAMDNodes;
39
40 template <> struct ilist_alloc_traits<Instruction> {
41   static inline void deleteNode(Instruction *V);
42 };
43
44 class Instruction : public User,
45                     public ilist_node_with_parent<Instruction, BasicBlock> {
46   BasicBlock *Parent;
47   DebugLoc DbgLoc;                         // 'dbg' Metadata cache.
48
49   enum {
50     /// This is a bit stored in the SubClassData field which indicates whether
51     /// this instruction has metadata attached to it or not.
52     HasMetadataBit = 1 << 15
53   };
54
55 protected:
56   ~Instruction(); // Use deleteValue() to delete a generic Instruction.
57
58 public:
59   Instruction(const Instruction &) = delete;
60   Instruction &operator=(const Instruction &) = delete;
61
62   /// Specialize the methods defined in Value, as we know that an instruction
63   /// can only be used by other instructions.
64   Instruction       *user_back()       { return cast<Instruction>(*user_begin());}
65   const Instruction *user_back() const { return cast<Instruction>(*user_begin());}
66
67   inline const BasicBlock *getParent() const { return Parent; }
68   inline       BasicBlock *getParent()       { return Parent; }
69
70   /// Return the module owning the function this instruction belongs to
71   /// or nullptr it the function does not have a module.
72   ///
73   /// Note: this is undefined behavior if the instruction does not have a
74   /// parent, or the parent basic block does not have a parent function.
75   const Module *getModule() const;
76   Module *getModule() {
77     return const_cast<Module *>(
78                            static_cast<const Instruction *>(this)->getModule());
79   }
80
81   /// Return the function this instruction belongs to.
82   ///
83   /// Note: it is undefined behavior to call this on an instruction not
84   /// currently inserted into a function.
85   const Function *getFunction() const;
86   Function *getFunction() {
87     return const_cast<Function *>(
88                          static_cast<const Instruction *>(this)->getFunction());
89   }
90
91   /// This method unlinks 'this' from the containing basic block, but does not
92   /// delete it.
93   void removeFromParent();
94
95   /// This method unlinks 'this' from the containing basic block and deletes it.
96   ///
97   /// \returns an iterator pointing to the element after the erased one
98   SymbolTableList<Instruction>::iterator eraseFromParent();
99
100   /// Insert an unlinked instruction into a basic block immediately before
101   /// the specified instruction.
102   void insertBefore(Instruction *InsertPos);
103
104   /// Insert an unlinked instruction into a basic block immediately after the
105   /// specified instruction.
106   void insertAfter(Instruction *InsertPos);
107
108   /// Unlink this instruction from its current basic block and insert it into
109   /// the basic block that MovePos lives in, right before MovePos.
110   void moveBefore(Instruction *MovePos);
111
112   /// Unlink this instruction and insert into BB before I.
113   ///
114   /// \pre I is a valid iterator into BB.
115   void moveBefore(BasicBlock &BB, SymbolTableList<Instruction>::iterator I);
116
117   /// Unlink this instruction from its current basic block and insert it into
118   /// the basic block that MovePos lives in, right after MovePos.
119   void moveAfter(Instruction *MovePos);
120
121   //===--------------------------------------------------------------------===//
122   // Subclass classification.
123   //===--------------------------------------------------------------------===//
124
125   /// Returns a member of one of the enums like Instruction::Add.
126   unsigned getOpcode() const { return getValueID() - InstructionVal; }
127
128   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
129   bool isTerminator() const { return isTerminator(getOpcode()); }
130   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
131   bool isIntDivRem() const { return isIntDivRem(getOpcode()); }
132   bool isShift() { return isShift(getOpcode()); }
133   bool isCast() const { return isCast(getOpcode()); }
134   bool isFuncletPad() const { return isFuncletPad(getOpcode()); }
135
136   static const char* getOpcodeName(unsigned OpCode);
137
138   static inline bool isTerminator(unsigned OpCode) {
139     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
140   }
141
142   static inline bool isBinaryOp(unsigned Opcode) {
143     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
144   }
145
146   static inline bool isIntDivRem(unsigned Opcode) {
147     return Opcode == UDiv || Opcode == SDiv || Opcode == URem || Opcode == SRem;
148   }
149
150   /// Determine if the Opcode is one of the shift instructions.
151   static inline bool isShift(unsigned Opcode) {
152     return Opcode >= Shl && Opcode <= AShr;
153   }
154
155   /// Return true if this is a logical shift left or a logical shift right.
156   inline bool isLogicalShift() const {
157     return getOpcode() == Shl || getOpcode() == LShr;
158   }
159
160   /// Return true if this is an arithmetic shift right.
161   inline bool isArithmeticShift() const {
162     return getOpcode() == AShr;
163   }
164
165   /// Determine if the Opcode is and/or/xor.
166   static inline bool isBitwiseLogicOp(unsigned Opcode) {
167     return Opcode == And || Opcode == Or || Opcode == Xor;
168   }
169
170   /// Return true if this is and/or/xor.
171   inline bool isBitwiseLogicOp() const {
172     return isBitwiseLogicOp(getOpcode());
173   }
174
175   /// Determine if the OpCode is one of the CastInst instructions.
176   static inline bool isCast(unsigned OpCode) {
177     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
178   }
179
180   /// Determine if the OpCode is one of the FuncletPadInst instructions.
181   static inline bool isFuncletPad(unsigned OpCode) {
182     return OpCode >= FuncletPadOpsBegin && OpCode < FuncletPadOpsEnd;
183   }
184
185   //===--------------------------------------------------------------------===//
186   // Metadata manipulation.
187   //===--------------------------------------------------------------------===//
188
189   /// Return true if this instruction has any metadata attached to it.
190   bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); }
191
192   /// Return true if this instruction has metadata attached to it other than a
193   /// debug location.
194   bool hasMetadataOtherThanDebugLoc() const {
195     return hasMetadataHashEntry();
196   }
197
198   /// Get the metadata of given kind attached to this Instruction.
199   /// If the metadata is not found then return null.
200   MDNode *getMetadata(unsigned KindID) const {
201     if (!hasMetadata()) return nullptr;
202     return getMetadataImpl(KindID);
203   }
204
205   /// Get the metadata of given kind attached to this Instruction.
206   /// If the metadata is not found then return null.
207   MDNode *getMetadata(StringRef Kind) const {
208     if (!hasMetadata()) return nullptr;
209     return getMetadataImpl(Kind);
210   }
211
212   /// Get all metadata attached to this Instruction. The first element of each
213   /// pair returned is the KindID, the second element is the metadata value.
214   /// This list is returned sorted by the KindID.
215   void
216   getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
217     if (hasMetadata())
218       getAllMetadataImpl(MDs);
219   }
220
221   /// This does the same thing as getAllMetadata, except that it filters out the
222   /// debug location.
223   void getAllMetadataOtherThanDebugLoc(
224       SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
225     if (hasMetadataOtherThanDebugLoc())
226       getAllMetadataOtherThanDebugLocImpl(MDs);
227   }
228
229   /// Fills the AAMDNodes structure with AA metadata from this instruction.
230   /// When Merge is true, the existing AA metadata is merged with that from this
231   /// instruction providing the most-general result.
232   void getAAMetadata(AAMDNodes &N, bool Merge = false) const;
233
234   /// Set the metadata of the specified kind to the specified node. This updates
235   /// or replaces metadata if already present, or removes it if Node is null.
236   void setMetadata(unsigned KindID, MDNode *Node);
237   void setMetadata(StringRef Kind, MDNode *Node);
238
239   /// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty,
240   /// specifies the list of meta data that needs to be copied. If \p WL is
241   /// empty, all meta data will be copied.
242   void copyMetadata(const Instruction &SrcInst,
243                     ArrayRef<unsigned> WL = ArrayRef<unsigned>());
244
245   /// If the instruction has "branch_weights" MD_prof metadata and the MDNode
246   /// has three operands (including name string), swap the order of the
247   /// metadata.
248   void swapProfMetadata();
249
250   /// Drop all unknown metadata except for debug locations.
251   /// @{
252   /// Passes are required to drop metadata they don't understand. This is a
253   /// convenience method for passes to do so.
254   void dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs);
255   void dropUnknownNonDebugMetadata() {
256     return dropUnknownNonDebugMetadata(None);
257   }
258   void dropUnknownNonDebugMetadata(unsigned ID1) {
259     return dropUnknownNonDebugMetadata(makeArrayRef(ID1));
260   }
261   void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2) {
262     unsigned IDs[] = {ID1, ID2};
263     return dropUnknownNonDebugMetadata(IDs);
264   }
265   /// @}
266
267   /// Sets the metadata on this instruction from the AAMDNodes structure.
268   void setAAMetadata(const AAMDNodes &N);
269
270   /// Retrieve the raw weight values of a conditional branch or select.
271   /// Returns true on success with profile weights filled in.
272   /// Returns false if no metadata or invalid metadata was found.
273   bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const;
274
275   /// Retrieve total raw weight values of a branch.
276   /// Returns true on success with profile total weights filled in.
277   /// Returns false if no metadata was found.
278   bool extractProfTotalWeight(uint64_t &TotalVal) const;
279
280   /// Updates branch_weights metadata by scaling it by \p S / \p T.
281   void updateProfWeight(uint64_t S, uint64_t T);
282
283   /// Sets the branch_weights metadata to \p W for CallInst.
284   void setProfWeight(uint64_t W);
285
286   /// Set the debug location information for this instruction.
287   void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
288
289   /// Return the debug location for this node as a DebugLoc.
290   const DebugLoc &getDebugLoc() const { return DbgLoc; }
291
292   /// Set or clear the nuw flag on this instruction, which must be an operator
293   /// which supports this flag. See LangRef.html for the meaning of this flag.
294   void setHasNoUnsignedWrap(bool b = true);
295
296   /// Set or clear the nsw flag on this instruction, which must be an operator
297   /// which supports this flag. See LangRef.html for the meaning of this flag.
298   void setHasNoSignedWrap(bool b = true);
299
300   /// Set or clear the exact flag on this instruction, which must be an operator
301   /// which supports this flag. See LangRef.html for the meaning of this flag.
302   void setIsExact(bool b = true);
303
304   /// Determine whether the no unsigned wrap flag is set.
305   bool hasNoUnsignedWrap() const;
306
307   /// Determine whether the no signed wrap flag is set.
308   bool hasNoSignedWrap() const;
309
310   /// Drops flags that may cause this instruction to evaluate to poison despite
311   /// having non-poison inputs.
312   void dropPoisonGeneratingFlags();
313
314   /// Determine whether the exact flag is set.
315   bool isExact() const;
316
317   /// Set or clear all fast-math-flags on this instruction, which must be an
318   /// operator which supports this flag. See LangRef.html for the meaning of
319   /// this flag.
320   void setFast(bool B);
321
322   /// Set or clear the reassociation flag on this instruction, which must be
323   /// an operator which supports this flag. See LangRef.html for the meaning of
324   /// this flag.
325   void setHasAllowReassoc(bool B);
326
327   /// Set or clear the no-nans flag on this instruction, which must be an
328   /// operator which supports this flag. See LangRef.html for the meaning of
329   /// this flag.
330   void setHasNoNaNs(bool B);
331
332   /// Set or clear the no-infs flag on this instruction, which must be an
333   /// operator which supports this flag. See LangRef.html for the meaning of
334   /// this flag.
335   void setHasNoInfs(bool B);
336
337   /// Set or clear the no-signed-zeros flag on this instruction, which must be
338   /// an operator which supports this flag. See LangRef.html for the meaning of
339   /// this flag.
340   void setHasNoSignedZeros(bool B);
341
342   /// Set or clear the allow-reciprocal flag on this instruction, which must be
343   /// an operator which supports this flag. See LangRef.html for the meaning of
344   /// this flag.
345   void setHasAllowReciprocal(bool B);
346
347   /// Set or clear the approximate-math-functions flag on this instruction,
348   /// which must be an operator which supports this flag. See LangRef.html for
349   /// the meaning of this flag.
350   void setHasApproxFunc(bool B);
351
352   /// Convenience function for setting multiple fast-math flags on this
353   /// instruction, which must be an operator which supports these flags. See
354   /// LangRef.html for the meaning of these flags.
355   void setFastMathFlags(FastMathFlags FMF);
356
357   /// Convenience function for transferring all fast-math flag values to this
358   /// instruction, which must be an operator which supports these flags. See
359   /// LangRef.html for the meaning of these flags.
360   void copyFastMathFlags(FastMathFlags FMF);
361
362   /// Determine whether all fast-math-flags are set.
363   bool isFast() const;
364
365   /// Determine whether the allow-reassociation flag is set.
366   bool hasAllowReassoc() const;
367
368   /// Determine whether the no-NaNs flag is set.
369   bool hasNoNaNs() const;
370
371   /// Determine whether the no-infs flag is set.
372   bool hasNoInfs() const;
373
374   /// Determine whether the no-signed-zeros flag is set.
375   bool hasNoSignedZeros() const;
376
377   /// Determine whether the allow-reciprocal flag is set.
378   bool hasAllowReciprocal() const;
379
380   /// Determine whether the allow-contract flag is set.
381   bool hasAllowContract() const;
382
383   /// Determine whether the approximate-math-functions flag is set.
384   bool hasApproxFunc() const;
385
386   /// Convenience function for getting all the fast-math flags, which must be an
387   /// operator which supports these flags. See LangRef.html for the meaning of
388   /// these flags.
389   FastMathFlags getFastMathFlags() const;
390
391   /// Copy I's fast-math flags
392   void copyFastMathFlags(const Instruction *I);
393
394   /// Convenience method to copy supported exact, fast-math, and (optionally)
395   /// wrapping flags from V to this instruction.
396   void copyIRFlags(const Value *V, bool IncludeWrapFlags = true);
397
398   /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
399   /// V and this instruction.
400   void andIRFlags(const Value *V);
401
402   /// Merge 2 debug locations and apply it to the Instruction. If the
403   /// instruction is a CallIns, we need to traverse the inline chain to find
404   /// the common scope. This is not efficient for N-way merging as each time
405   /// you merge 2 iterations, you need to rebuild the hashmap to find the
406   /// common scope. However, we still choose this API because:
407   ///  1) Simplicity: it takes 2 locations instead of a list of locations.
408   ///  2) In worst case, it increases the complexity from O(N*I) to
409   ///     O(2*N*I), where N is # of Instructions to merge, and I is the
410   ///     maximum level of inline stack. So it is still linear.
411   ///  3) Merging of call instructions should be extremely rare in real
412   ///     applications, thus the N-way merging should be in code path.
413   /// The DebugLoc attached to this instruction will be overwritten by the
414   /// merged DebugLoc.
415   void applyMergedLocation(const DILocation *LocA, const DILocation *LocB);
416
417 private:
418   /// Return true if we have an entry in the on-the-side metadata hash.
419   bool hasMetadataHashEntry() const {
420     return (getSubclassDataFromValue() & HasMetadataBit) != 0;
421   }
422
423   // These are all implemented in Metadata.cpp.
424   MDNode *getMetadataImpl(unsigned KindID) const;
425   MDNode *getMetadataImpl(StringRef Kind) const;
426   void
427   getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
428   void getAllMetadataOtherThanDebugLocImpl(
429       SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
430   /// Clear all hashtable-based metadata from this instruction.
431   void clearMetadataHashEntries();
432
433 public:
434   //===--------------------------------------------------------------------===//
435   // Predicates and helper methods.
436   //===--------------------------------------------------------------------===//
437
438   /// Return true if the instruction is associative:
439   ///
440   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
441   ///
442   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
443   ///
444   bool isAssociative() const LLVM_READONLY;
445   static bool isAssociative(unsigned Opcode) {
446     return Opcode == And || Opcode == Or || Opcode == Xor ||
447            Opcode == Add || Opcode == Mul;
448   }
449
450   /// Return true if the instruction is commutative:
451   ///
452   ///   Commutative operators satisfy: (x op y) === (y op x)
453   ///
454   /// In LLVM, these are the commutative operators, plus SetEQ and SetNE, when
455   /// applied to any type.
456   ///
457   bool isCommutative() const { return isCommutative(getOpcode()); }
458   static bool isCommutative(unsigned Opcode) {
459     switch (Opcode) {
460     case Add: case FAdd:
461     case Mul: case FMul:
462     case And: case Or: case Xor:
463       return true;
464     default:
465       return false;
466   }
467   }
468
469   /// Return true if the instruction is idempotent:
470   ///
471   ///   Idempotent operators satisfy:  x op x === x
472   ///
473   /// In LLVM, the And and Or operators are idempotent.
474   ///
475   bool isIdempotent() const { return isIdempotent(getOpcode()); }
476   static bool isIdempotent(unsigned Opcode) {
477     return Opcode == And || Opcode == Or;
478   }
479
480   /// Return true if the instruction is nilpotent:
481   ///
482   ///   Nilpotent operators satisfy:  x op x === Id,
483   ///
484   ///   where Id is the identity for the operator, i.e. a constant such that
485   ///     x op Id === x and Id op x === x for all x.
486   ///
487   /// In LLVM, the Xor operator is nilpotent.
488   ///
489   bool isNilpotent() const { return isNilpotent(getOpcode()); }
490   static bool isNilpotent(unsigned Opcode) {
491     return Opcode == Xor;
492   }
493
494   /// Return true if this instruction may modify memory.
495   bool mayWriteToMemory() const;
496
497   /// Return true if this instruction may read memory.
498   bool mayReadFromMemory() const;
499
500   /// Return true if this instruction may read or write memory.
501   bool mayReadOrWriteMemory() const {
502     return mayReadFromMemory() || mayWriteToMemory();
503   }
504
505   /// Return true if this instruction has an AtomicOrdering of unordered or
506   /// higher.
507   bool isAtomic() const;
508
509   /// Return true if this atomic instruction loads from memory.
510   bool hasAtomicLoad() const;
511
512   /// Return true if this atomic instruction stores to memory.
513   bool hasAtomicStore() const;
514
515   /// Return true if this instruction may throw an exception.
516   bool mayThrow() const;
517
518   /// Return true if this instruction behaves like a memory fence: it can load
519   /// or store to memory location without being given a memory location.
520   bool isFenceLike() const {
521     switch (getOpcode()) {
522     default:
523       return false;
524     // This list should be kept in sync with the list in mayWriteToMemory for
525     // all opcodes which don't have a memory location.
526     case Instruction::Fence:
527     case Instruction::CatchPad:
528     case Instruction::CatchRet:
529     case Instruction::Call:
530     case Instruction::Invoke:
531       return true;
532     }
533   }
534
535   /// Return true if the instruction may have side effects.
536   ///
537   /// Note that this does not consider malloc and alloca to have side
538   /// effects because the newly allocated memory is completely invisible to
539   /// instructions which don't use the returned value.  For cases where this
540   /// matters, isSafeToSpeculativelyExecute may be more appropriate.
541   bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); }
542
543   /// Return true if the instruction can be removed if the result is unused.
544   ///
545   /// When constant folding some instructions cannot be removed even if their
546   /// results are unused. Specifically terminator instructions and calls that
547   /// may have side effects cannot be removed without semantically changing the
548   /// generated program.
549   bool isSafeToRemove() const;
550
551   /// Return true if the instruction is a variety of EH-block.
552   bool isEHPad() const {
553     switch (getOpcode()) {
554     case Instruction::CatchSwitch:
555     case Instruction::CatchPad:
556     case Instruction::CleanupPad:
557     case Instruction::LandingPad:
558       return true;
559     default:
560       return false;
561     }
562   }
563
564   /// Return a pointer to the next non-debug instruction in the same basic
565   /// block as 'this', or nullptr if no such instruction exists.
566   const Instruction *getNextNonDebugInstruction() const;
567   Instruction *getNextNonDebugInstruction() {
568     return const_cast<Instruction *>(
569         static_cast<const Instruction *>(this)->getNextNonDebugInstruction());
570   }
571
572   /// Create a copy of 'this' instruction that is identical in all ways except
573   /// the following:
574   ///   * The instruction has no parent
575   ///   * The instruction has no name
576   ///
577   Instruction *clone() const;
578
579   /// Return true if the specified instruction is exactly identical to the
580   /// current one. This means that all operands match and any extra information
581   /// (e.g. load is volatile) agree.
582   bool isIdenticalTo(const Instruction *I) const;
583
584   /// This is like isIdenticalTo, except that it ignores the
585   /// SubclassOptionalData flags, which may specify conditions under which the
586   /// instruction's result is undefined.
587   bool isIdenticalToWhenDefined(const Instruction *I) const;
588
589   /// When checking for operation equivalence (using isSameOperationAs) it is
590   /// sometimes useful to ignore certain attributes.
591   enum OperationEquivalenceFlags {
592     /// Check for equivalence ignoring load/store alignment.
593     CompareIgnoringAlignment = 1<<0,
594     /// Check for equivalence treating a type and a vector of that type
595     /// as equivalent.
596     CompareUsingScalarTypes = 1<<1
597   };
598
599   /// This function determines if the specified instruction executes the same
600   /// operation as the current one. This means that the opcodes, type, operand
601   /// types and any other factors affecting the operation must be the same. This
602   /// is similar to isIdenticalTo except the operands themselves don't have to
603   /// be identical.
604   /// @returns true if the specified instruction is the same operation as
605   /// the current one.
606   /// Determine if one instruction is the same operation as another.
607   bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const;
608
609   /// Return true if there are any uses of this instruction in blocks other than
610   /// the specified block. Note that PHI nodes are considered to evaluate their
611   /// operands in the corresponding predecessor block.
612   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
613
614
615   /// Methods for support type inquiry through isa, cast, and dyn_cast:
616   static bool classof(const Value *V) {
617     return V->getValueID() >= Value::InstructionVal;
618   }
619
620   //----------------------------------------------------------------------
621   // Exported enumerations.
622   //
623   enum TermOps {       // These terminate basic blocks
624 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
625 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
626 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
627 #include "llvm/IR/Instruction.def"
628   };
629
630   enum BinaryOps {
631 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
632 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
633 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
634 #include "llvm/IR/Instruction.def"
635   };
636
637   enum MemoryOps {
638 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
639 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
640 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
641 #include "llvm/IR/Instruction.def"
642   };
643
644   enum CastOps {
645 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
646 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
647 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
648 #include "llvm/IR/Instruction.def"
649   };
650
651   enum FuncletPadOps {
652 #define  FIRST_FUNCLETPAD_INST(N)             FuncletPadOpsBegin = N,
653 #define HANDLE_FUNCLETPAD_INST(N, OPC, CLASS) OPC = N,
654 #define   LAST_FUNCLETPAD_INST(N)             FuncletPadOpsEnd = N+1
655 #include "llvm/IR/Instruction.def"
656   };
657
658   enum OtherOps {
659 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
660 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
661 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
662 #include "llvm/IR/Instruction.def"
663   };
664
665 private:
666   friend class SymbolTableListTraits<Instruction>;
667
668   // Shadow Value::setValueSubclassData with a private forwarding method so that
669   // subclasses cannot accidentally use it.
670   void setValueSubclassData(unsigned short D) {
671     Value::setValueSubclassData(D);
672   }
673
674   unsigned short getSubclassDataFromValue() const {
675     return Value::getSubclassDataFromValue();
676   }
677
678   void setHasMetadataHashEntry(bool V) {
679     setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
680                          (V ? HasMetadataBit : 0));
681   }
682
683   void setParent(BasicBlock *P);
684
685 protected:
686   // Instruction subclasses can stick up to 15 bits of stuff into the
687   // SubclassData field of instruction with these members.
688
689   // Verify that only the low 15 bits are used.
690   void setInstructionSubclassData(unsigned short D) {
691     assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
692     setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
693   }
694
695   unsigned getSubclassDataFromInstruction() const {
696     return getSubclassDataFromValue() & ~HasMetadataBit;
697   }
698
699   Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
700               Instruction *InsertBefore = nullptr);
701   Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
702               BasicBlock *InsertAtEnd);
703
704 private:
705   /// Create a copy of this instruction.
706   Instruction *cloneImpl() const;
707 };
708
709 inline void ilist_alloc_traits<Instruction>::deleteNode(Instruction *V) {
710   V->deleteValue();
711 }
712
713 } // end namespace llvm
714
715 #endif // LLVM_IR_INSTRUCTION_H