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