LLVM  10.0.0svn
Instruction.h
Go to the documentation of this file.
1 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declaration of the Instruction class, which is the
10 // base class for all of the LLVM instructions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_INSTRUCTION_H
15 #define LLVM_IR_INSTRUCTION_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/ilist_node.h"
21 #include "llvm/IR/DebugLoc.h"
23 #include "llvm/IR/User.h"
24 #include "llvm/IR/Value.h"
25 #include "llvm/Support/Casting.h"
26 #include <algorithm>
27 #include <cassert>
28 #include <cstdint>
29 #include <utility>
30 
31 namespace llvm {
32 
33 class BasicBlock;
34 class FastMathFlags;
35 class MDNode;
36 class Module;
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 isUnaryOp() const { return isUnaryOp(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  bool isExceptionalTerminator() const {
136  return isExceptionalTerminator(getOpcode());
137  }
138  bool isIndirectTerminator() const {
139  return isIndirectTerminator(getOpcode());
140  }
141 
142  static const char* getOpcodeName(unsigned OpCode);
143 
144  static inline bool isTerminator(unsigned OpCode) {
145  return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
146  }
147 
148  static inline bool isUnaryOp(unsigned Opcode) {
149  return Opcode >= UnaryOpsBegin && Opcode < UnaryOpsEnd;
150  }
151  static inline bool isBinaryOp(unsigned Opcode) {
152  return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
153  }
154 
155  static inline bool isIntDivRem(unsigned Opcode) {
156  return Opcode == UDiv || Opcode == SDiv || Opcode == URem || Opcode == SRem;
157  }
158 
159  /// Determine if the Opcode is one of the shift instructions.
160  static inline bool isShift(unsigned Opcode) {
161  return Opcode >= Shl && Opcode <= AShr;
162  }
163 
164  /// Return true if this is a logical shift left or a logical shift right.
165  inline bool isLogicalShift() const {
166  return getOpcode() == Shl || getOpcode() == LShr;
167  }
168 
169  /// Return true if this is an arithmetic shift right.
170  inline bool isArithmeticShift() const {
171  return getOpcode() == AShr;
172  }
173 
174  /// Determine if the Opcode is and/or/xor.
175  static inline bool isBitwiseLogicOp(unsigned Opcode) {
176  return Opcode == And || Opcode == Or || Opcode == Xor;
177  }
178 
179  /// Return true if this is and/or/xor.
180  inline bool isBitwiseLogicOp() const {
181  return isBitwiseLogicOp(getOpcode());
182  }
183 
184  /// Determine if the OpCode is one of the CastInst instructions.
185  static inline bool isCast(unsigned OpCode) {
186  return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
187  }
188 
189  /// Determine if the OpCode is one of the FuncletPadInst instructions.
190  static inline bool isFuncletPad(unsigned OpCode) {
191  return OpCode >= FuncletPadOpsBegin && OpCode < FuncletPadOpsEnd;
192  }
193 
194  /// Returns true if the OpCode is a terminator related to exception handling.
195  static inline bool isExceptionalTerminator(unsigned OpCode) {
196  switch (OpCode) {
197  case Instruction::CatchSwitch:
198  case Instruction::CatchRet:
199  case Instruction::CleanupRet:
200  case Instruction::Invoke:
201  case Instruction::Resume:
202  return true;
203  default:
204  return false;
205  }
206  }
207 
208  /// Returns true if the OpCode is a terminator with indirect targets.
209  static inline bool isIndirectTerminator(unsigned OpCode) {
210  switch (OpCode) {
211  case Instruction::IndirectBr:
212  case Instruction::CallBr:
213  return true;
214  default:
215  return false;
216  }
217  }
218 
219  //===--------------------------------------------------------------------===//
220  // Metadata manipulation.
221  //===--------------------------------------------------------------------===//
222 
223  /// Return true if this instruction has any metadata attached to it.
224  bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); }
225 
226  /// Return true if this instruction has metadata attached to it other than a
227  /// debug location.
229  return hasMetadataHashEntry();
230  }
231 
232  /// Return true if this instruction has the given type of metadata attached.
233  bool hasMetadata(unsigned KindID) const {
234  return getMetadata(KindID) != nullptr;
235  }
236 
237  /// Return true if this instruction has the given type of metadata attached.
238  bool hasMetadata(StringRef Kind) const {
239  return getMetadata(Kind) != nullptr;
240  }
241 
242  /// Get the metadata of given kind attached to this Instruction.
243  /// If the metadata is not found then return null.
244  MDNode *getMetadata(unsigned KindID) const {
245  if (!hasMetadata()) return nullptr;
246  return getMetadataImpl(KindID);
247  }
248 
249  /// Get the metadata of given kind attached to this Instruction.
250  /// If the metadata is not found then return null.
252  if (!hasMetadata()) return nullptr;
253  return getMetadataImpl(Kind);
254  }
255 
256  /// Get all metadata attached to this Instruction. The first element of each
257  /// pair returned is the KindID, the second element is the metadata value.
258  /// This list is returned sorted by the KindID.
259  void
260  getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
261  if (hasMetadata())
262  getAllMetadataImpl(MDs);
263  }
264 
265  /// This does the same thing as getAllMetadata, except that it filters out the
266  /// debug location.
268  SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
270  getAllMetadataOtherThanDebugLocImpl(MDs);
271  }
272 
273  /// Fills the AAMDNodes structure with AA metadata from this instruction.
274  /// When Merge is true, the existing AA metadata is merged with that from this
275  /// instruction providing the most-general result.
276  void getAAMetadata(AAMDNodes &N, bool Merge = false) const;
277 
278  /// Set the metadata of the specified kind to the specified node. This updates
279  /// or replaces metadata if already present, or removes it if Node is null.
280  void setMetadata(unsigned KindID, MDNode *Node);
281  void setMetadata(StringRef Kind, MDNode *Node);
282 
283  /// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty,
284  /// specifies the list of meta data that needs to be copied. If \p WL is
285  /// empty, all meta data will be copied.
286  void copyMetadata(const Instruction &SrcInst,
288 
289  /// If the instruction has "branch_weights" MD_prof metadata and the MDNode
290  /// has three operands (including name string), swap the order of the
291  /// metadata.
292  void swapProfMetadata();
293 
294  /// Drop all unknown metadata except for debug locations.
295  /// @{
296  /// Passes are required to drop metadata they don't understand. This is a
297  /// convenience method for passes to do so.
298  void dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs);
300  return dropUnknownNonDebugMetadata(None);
301  }
302  void dropUnknownNonDebugMetadata(unsigned ID1) {
303  return dropUnknownNonDebugMetadata(makeArrayRef(ID1));
304  }
305  void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2) {
306  unsigned IDs[] = {ID1, ID2};
307  return dropUnknownNonDebugMetadata(IDs);
308  }
309  /// @}
310 
311  /// Sets the metadata on this instruction from the AAMDNodes structure.
312  void setAAMetadata(const AAMDNodes &N);
313 
314  /// Retrieve the raw weight values of a conditional branch or select.
315  /// Returns true on success with profile weights filled in.
316  /// Returns false if no metadata or invalid metadata was found.
317  bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const;
318 
319  /// Retrieve total raw weight values of a branch.
320  /// Returns true on success with profile total weights filled in.
321  /// Returns false if no metadata was found.
322  bool extractProfTotalWeight(uint64_t &TotalVal) const;
323 
324  /// Sets the branch_weights metadata to \p W for CallInst.
325  void setProfWeight(uint64_t W);
326 
327  /// Set the debug location information for this instruction.
328  void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
329 
330  /// Return the debug location for this node as a DebugLoc.
331  const DebugLoc &getDebugLoc() const { return DbgLoc; }
332 
333  /// Set or clear the nuw flag on this instruction, which must be an operator
334  /// which supports this flag. See LangRef.html for the meaning of this flag.
335  void setHasNoUnsignedWrap(bool b = true);
336 
337  /// Set or clear the nsw flag on this instruction, which must be an operator
338  /// which supports this flag. See LangRef.html for the meaning of this flag.
339  void setHasNoSignedWrap(bool b = true);
340 
341  /// Set or clear the exact flag on this instruction, which must be an operator
342  /// which supports this flag. See LangRef.html for the meaning of this flag.
343  void setIsExact(bool b = true);
344 
345  /// Determine whether the no unsigned wrap flag is set.
346  bool hasNoUnsignedWrap() const;
347 
348  /// Determine whether the no signed wrap flag is set.
349  bool hasNoSignedWrap() const;
350 
351  /// Drops flags that may cause this instruction to evaluate to poison despite
352  /// having non-poison inputs.
353  void dropPoisonGeneratingFlags();
354 
355  /// Determine whether the exact flag is set.
356  bool isExact() const;
357 
358  /// Set or clear all fast-math-flags on this instruction, which must be an
359  /// operator which supports this flag. See LangRef.html for the meaning of
360  /// this flag.
361  void setFast(bool B);
362 
363  /// Set or clear the reassociation flag on this instruction, which must be
364  /// an operator which supports this flag. See LangRef.html for the meaning of
365  /// this flag.
366  void setHasAllowReassoc(bool B);
367 
368  /// Set or clear the no-nans flag on this instruction, which must be an
369  /// operator which supports this flag. See LangRef.html for the meaning of
370  /// this flag.
371  void setHasNoNaNs(bool B);
372 
373  /// Set or clear the no-infs flag on this instruction, which must be an
374  /// operator which supports this flag. See LangRef.html for the meaning of
375  /// this flag.
376  void setHasNoInfs(bool B);
377 
378  /// Set or clear the no-signed-zeros flag on this instruction, which must be
379  /// an operator which supports this flag. See LangRef.html for the meaning of
380  /// this flag.
381  void setHasNoSignedZeros(bool B);
382 
383  /// Set or clear the allow-reciprocal flag on this instruction, which must be
384  /// an operator which supports this flag. See LangRef.html for the meaning of
385  /// this flag.
386  void setHasAllowReciprocal(bool B);
387 
388  /// Set or clear the approximate-math-functions flag on this instruction,
389  /// which must be an operator which supports this flag. See LangRef.html for
390  /// the meaning of this flag.
391  void setHasApproxFunc(bool B);
392 
393  /// Convenience function for setting multiple fast-math flags on this
394  /// instruction, which must be an operator which supports these flags. See
395  /// LangRef.html for the meaning of these flags.
396  void setFastMathFlags(FastMathFlags FMF);
397 
398  /// Convenience function for transferring all fast-math flag values to this
399  /// instruction, which must be an operator which supports these flags. See
400  /// LangRef.html for the meaning of these flags.
401  void copyFastMathFlags(FastMathFlags FMF);
402 
403  /// Determine whether all fast-math-flags are set.
404  bool isFast() const;
405 
406  /// Determine whether the allow-reassociation flag is set.
407  bool hasAllowReassoc() const;
408 
409  /// Determine whether the no-NaNs flag is set.
410  bool hasNoNaNs() const;
411 
412  /// Determine whether the no-infs flag is set.
413  bool hasNoInfs() const;
414 
415  /// Determine whether the no-signed-zeros flag is set.
416  bool hasNoSignedZeros() const;
417 
418  /// Determine whether the allow-reciprocal flag is set.
419  bool hasAllowReciprocal() const;
420 
421  /// Determine whether the allow-contract flag is set.
422  bool hasAllowContract() const;
423 
424  /// Determine whether the approximate-math-functions flag is set.
425  bool hasApproxFunc() const;
426 
427  /// Convenience function for getting all the fast-math flags, which must be an
428  /// operator which supports these flags. See LangRef.html for the meaning of
429  /// these flags.
430  FastMathFlags getFastMathFlags() const;
431 
432  /// Copy I's fast-math flags
433  void copyFastMathFlags(const Instruction *I);
434 
435  /// Convenience method to copy supported exact, fast-math, and (optionally)
436  /// wrapping flags from V to this instruction.
437  void copyIRFlags(const Value *V, bool IncludeWrapFlags = true);
438 
439  /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
440  /// V and this instruction.
441  void andIRFlags(const Value *V);
442 
443  /// Merge 2 debug locations and apply it to the Instruction. If the
444  /// instruction is a CallIns, we need to traverse the inline chain to find
445  /// the common scope. This is not efficient for N-way merging as each time
446  /// you merge 2 iterations, you need to rebuild the hashmap to find the
447  /// common scope. However, we still choose this API because:
448  /// 1) Simplicity: it takes 2 locations instead of a list of locations.
449  /// 2) In worst case, it increases the complexity from O(N*I) to
450  /// O(2*N*I), where N is # of Instructions to merge, and I is the
451  /// maximum level of inline stack. So it is still linear.
452  /// 3) Merging of call instructions should be extremely rare in real
453  /// applications, thus the N-way merging should be in code path.
454  /// The DebugLoc attached to this instruction will be overwritten by the
455  /// merged DebugLoc.
456  void applyMergedLocation(const DILocation *LocA, const DILocation *LocB);
457 
458 private:
459  /// Return true if we have an entry in the on-the-side metadata hash.
460  bool hasMetadataHashEntry() const {
461  return (getSubclassDataFromValue() & HasMetadataBit) != 0;
462  }
463 
464  // These are all implemented in Metadata.cpp.
465  MDNode *getMetadataImpl(unsigned KindID) const;
466  MDNode *getMetadataImpl(StringRef Kind) const;
467  void
468  getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
469  void getAllMetadataOtherThanDebugLocImpl(
470  SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
471  /// Clear all hashtable-based metadata from this instruction.
472  void clearMetadataHashEntries();
473 
474 public:
475  //===--------------------------------------------------------------------===//
476  // Predicates and helper methods.
477  //===--------------------------------------------------------------------===//
478 
479  /// Return true if the instruction is associative:
480  ///
481  /// Associative operators satisfy: x op (y op z) === (x op y) op z
482  ///
483  /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
484  ///
485  bool isAssociative() const LLVM_READONLY;
486  static bool isAssociative(unsigned Opcode) {
487  return Opcode == And || Opcode == Or || Opcode == Xor ||
488  Opcode == Add || Opcode == Mul;
489  }
490 
491  /// Return true if the instruction is commutative:
492  ///
493  /// Commutative operators satisfy: (x op y) === (y op x)
494  ///
495  /// In LLVM, these are the commutative operators, plus SetEQ and SetNE, when
496  /// applied to any type.
497  ///
498  bool isCommutative() const { return isCommutative(getOpcode()); }
499  static bool isCommutative(unsigned Opcode) {
500  switch (Opcode) {
501  case Add: case FAdd:
502  case Mul: case FMul:
503  case And: case Or: case Xor:
504  return true;
505  default:
506  return false;
507  }
508  }
509 
510  /// Return true if the instruction is idempotent:
511  ///
512  /// Idempotent operators satisfy: x op x === x
513  ///
514  /// In LLVM, the And and Or operators are idempotent.
515  ///
516  bool isIdempotent() const { return isIdempotent(getOpcode()); }
517  static bool isIdempotent(unsigned Opcode) {
518  return Opcode == And || Opcode == Or;
519  }
520 
521  /// Return true if the instruction is nilpotent:
522  ///
523  /// Nilpotent operators satisfy: x op x === Id,
524  ///
525  /// where Id is the identity for the operator, i.e. a constant such that
526  /// x op Id === x and Id op x === x for all x.
527  ///
528  /// In LLVM, the Xor operator is nilpotent.
529  ///
530  bool isNilpotent() const { return isNilpotent(getOpcode()); }
531  static bool isNilpotent(unsigned Opcode) {
532  return Opcode == Xor;
533  }
534 
535  /// Return true if this instruction may modify memory.
536  bool mayWriteToMemory() const;
537 
538  /// Return true if this instruction may read memory.
539  bool mayReadFromMemory() const;
540 
541  /// Return true if this instruction may read or write memory.
542  bool mayReadOrWriteMemory() const {
543  return mayReadFromMemory() || mayWriteToMemory();
544  }
545 
546  /// Return true if this instruction has an AtomicOrdering of unordered or
547  /// higher.
548  bool isAtomic() const;
549 
550  /// Return true if this atomic instruction loads from memory.
551  bool hasAtomicLoad() const;
552 
553  /// Return true if this atomic instruction stores to memory.
554  bool hasAtomicStore() const;
555 
556  /// Return true if this instruction may throw an exception.
557  bool mayThrow() const;
558 
559  /// Return true if this instruction behaves like a memory fence: it can load
560  /// or store to memory location without being given a memory location.
561  bool isFenceLike() const {
562  switch (getOpcode()) {
563  default:
564  return false;
565  // This list should be kept in sync with the list in mayWriteToMemory for
566  // all opcodes which don't have a memory location.
567  case Instruction::Fence:
568  case Instruction::CatchPad:
569  case Instruction::CatchRet:
570  case Instruction::Call:
571  case Instruction::Invoke:
572  return true;
573  }
574  }
575 
576  /// Return true if the instruction may have side effects.
577  ///
578  /// Note that this does not consider malloc and alloca to have side
579  /// effects because the newly allocated memory is completely invisible to
580  /// instructions which don't use the returned value. For cases where this
581  /// matters, isSafeToSpeculativelyExecute may be more appropriate.
582  bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); }
583 
584  /// Return true if the instruction can be removed if the result is unused.
585  ///
586  /// When constant folding some instructions cannot be removed even if their
587  /// results are unused. Specifically terminator instructions and calls that
588  /// may have side effects cannot be removed without semantically changing the
589  /// generated program.
590  bool isSafeToRemove() const;
591 
592  /// Return true if the instruction is a variety of EH-block.
593  bool isEHPad() const {
594  switch (getOpcode()) {
595  case Instruction::CatchSwitch:
596  case Instruction::CatchPad:
597  case Instruction::CleanupPad:
598  case Instruction::LandingPad:
599  return true;
600  default:
601  return false;
602  }
603  }
604 
605  /// Return true if the instruction is a llvm.lifetime.start or
606  /// llvm.lifetime.end marker.
607  bool isLifetimeStartOrEnd() const;
608 
609  /// Return a pointer to the next non-debug instruction in the same basic
610  /// block as 'this', or nullptr if no such instruction exists.
611  const Instruction *getNextNonDebugInstruction() const;
613  return const_cast<Instruction *>(
614  static_cast<const Instruction *>(this)->getNextNonDebugInstruction());
615  }
616 
617  /// Return a pointer to the previous non-debug instruction in the same basic
618  /// block as 'this', or nullptr if no such instruction exists.
619  const Instruction *getPrevNonDebugInstruction() const;
621  return const_cast<Instruction *>(
622  static_cast<const Instruction *>(this)->getPrevNonDebugInstruction());
623  }
624 
625  /// Create a copy of 'this' instruction that is identical in all ways except
626  /// the following:
627  /// * The instruction has no parent
628  /// * The instruction has no name
629  ///
630  Instruction *clone() const;
631 
632  /// Return true if the specified instruction is exactly identical to the
633  /// current one. This means that all operands match and any extra information
634  /// (e.g. load is volatile) agree.
635  bool isIdenticalTo(const Instruction *I) const;
636 
637  /// This is like isIdenticalTo, except that it ignores the
638  /// SubclassOptionalData flags, which may specify conditions under which the
639  /// instruction's result is undefined.
640  bool isIdenticalToWhenDefined(const Instruction *I) const;
641 
642  /// When checking for operation equivalence (using isSameOperationAs) it is
643  /// sometimes useful to ignore certain attributes.
645  /// Check for equivalence ignoring load/store alignment.
646  CompareIgnoringAlignment = 1<<0,
647  /// Check for equivalence treating a type and a vector of that type
648  /// as equivalent.
649  CompareUsingScalarTypes = 1<<1
650  };
651 
652  /// This function determines if the specified instruction executes the same
653  /// operation as the current one. This means that the opcodes, type, operand
654  /// types and any other factors affecting the operation must be the same. This
655  /// is similar to isIdenticalTo except the operands themselves don't have to
656  /// be identical.
657  /// @returns true if the specified instruction is the same operation as
658  /// the current one.
659  /// Determine if one instruction is the same operation as another.
660  bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const;
661 
662  /// Return true if there are any uses of this instruction in blocks other than
663  /// the specified block. Note that PHI nodes are considered to evaluate their
664  /// operands in the corresponding predecessor block.
665  bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
666 
667  /// Return the number of successors that this instruction has. The instruction
668  /// must be a terminator.
669  unsigned getNumSuccessors() const;
670 
671  /// Return the specified successor. This instruction must be a terminator.
672  BasicBlock *getSuccessor(unsigned Idx) const;
673 
674  /// Update the specified successor to point at the provided block. This
675  /// instruction must be a terminator.
676  void setSuccessor(unsigned Idx, BasicBlock *BB);
677 
678  /// Replace specified successor OldBB to point at the provided block.
679  /// This instruction must be a terminator.
680  void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB);
681 
682  /// Methods for support type inquiry through isa, cast, and dyn_cast:
683  static bool classof(const Value *V) {
684  return V->getValueID() >= Value::InstructionVal;
685  }
686 
687  //----------------------------------------------------------------------
688  // Exported enumerations.
689  //
690  enum TermOps { // These terminate basic blocks
691 #define FIRST_TERM_INST(N) TermOpsBegin = N,
692 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
693 #define LAST_TERM_INST(N) TermOpsEnd = N+1
694 #include "llvm/IR/Instruction.def"
695  };
696 
697  enum UnaryOps {
698 #define FIRST_UNARY_INST(N) UnaryOpsBegin = N,
699 #define HANDLE_UNARY_INST(N, OPC, CLASS) OPC = N,
700 #define LAST_UNARY_INST(N) UnaryOpsEnd = N+1
701 #include "llvm/IR/Instruction.def"
702  };
703 
704  enum BinaryOps {
705 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
706 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
707 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
708 #include "llvm/IR/Instruction.def"
709  };
710 
711  enum MemoryOps {
712 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
713 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
714 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
715 #include "llvm/IR/Instruction.def"
716  };
717 
718  enum CastOps {
719 #define FIRST_CAST_INST(N) CastOpsBegin = N,
720 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
721 #define LAST_CAST_INST(N) CastOpsEnd = N+1
722 #include "llvm/IR/Instruction.def"
723  };
724 
726 #define FIRST_FUNCLETPAD_INST(N) FuncletPadOpsBegin = N,
727 #define HANDLE_FUNCLETPAD_INST(N, OPC, CLASS) OPC = N,
728 #define LAST_FUNCLETPAD_INST(N) FuncletPadOpsEnd = N+1
729 #include "llvm/IR/Instruction.def"
730  };
731 
732  enum OtherOps {
733 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
734 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
735 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
736 #include "llvm/IR/Instruction.def"
737  };
738 
739 private:
741 
742  // Shadow Value::setValueSubclassData with a private forwarding method so that
743  // subclasses cannot accidentally use it.
744  void setValueSubclassData(unsigned short D) {
746  }
747 
748  unsigned short getSubclassDataFromValue() const {
750  }
751 
752  void setHasMetadataHashEntry(bool V) {
753  setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
754  (V ? HasMetadataBit : 0));
755  }
756 
757  void setParent(BasicBlock *P);
758 
759 protected:
760  // Instruction subclasses can stick up to 15 bits of stuff into the
761  // SubclassData field of instruction with these members.
762 
763  // Verify that only the low 15 bits are used.
764  void setInstructionSubclassData(unsigned short D) {
765  assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
766  setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
767  }
768 
770  return getSubclassDataFromValue() & ~HasMetadataBit;
771  }
772 
773  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
774  Instruction *InsertBefore = nullptr);
775  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
776  BasicBlock *InsertAtEnd);
777 
778 private:
779  /// Create a copy of this instruction.
780  Instruction *cloneImpl() const;
781 };
782 
784  V->deleteValue();
785 }
786 
787 } // end namespace llvm
788 
789 #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:267
unsigned short getSubclassDataFromValue() const
Definition: Value.h:714
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:484
bool isFuncletPad() const
Definition: Instruction.h:134
bool isFenceLike() const
Return true if this instruction behaves like a memory fence: it can load or store to memory location ...
Definition: Instruction.h:561
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned getSubclassDataFromInstruction() const
Definition: Instruction.h:769
Various leaf nodes.
Definition: ISDOpcodes.h:59
bool hasMetadataOtherThanDebugLoc() const
Return true if this instruction has metadata attached to it other than a debug location.
Definition: Instruction.h:228
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
bool isIdempotent() const
Return true if the instruction is idempotent:
Definition: Instruction.h:516
bool mayThrow(const MachineInstr &MI)
MDNode * getMetadata(StringRef Kind) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:251
bool isTerminator() const
Definition: Instruction.h:128
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:98
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:863
bool isArithmeticShift() const
Return true if this is an arithmetic shift right.
Definition: Instruction.h:170
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
Definition: Instruction.h:175
static bool isShift(unsigned Opcode)
Determine if the Opcode is one of the shift instructions.
Definition: Instruction.h:160
static bool isCommutative(unsigned Opcode)
Definition: Instruction.h:499
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
bool isNilpotent() const
Return true if the instruction is nilpotent:
Definition: Instruction.h:530
bool hasMetadata(unsigned KindID) const
Return true if this instruction has the given type of metadata attached.
Definition: Instruction.h:233
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:196
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
static bool isIndirectTerminator(unsigned OpCode)
Returns true if the OpCode is a terminator with indirect targets.
Definition: Instruction.h:209
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:224
void dropUnknownNonDebugMetadata(unsigned ID1)
Definition: Instruction.h:302
OperationEquivalenceFlags
When checking for operation equivalence (using isSameOperationAs) it is sometimes useful to ignore ce...
Definition: Instruction.h:644
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Definition: Instruction.h:180
void dropUnknownNonDebugMetadata()
Definition: Instruction.h:299
static bool isBinaryOp(unsigned Opcode)
Definition: Instruction.h:151
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:244
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:764
Instruction * getNextNonDebugInstruction()
Definition: Instruction.h:612
static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV)
bool hasMetadata(StringRef Kind) const
Return true if this instruction has the given type of metadata attached.
Definition: Instruction.h:238
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:258
Debug location.
static bool hasNoSignedWrap(BinaryOperator &I)
BasicBlock * getParent()
Definition: Instruction.h:67
static bool isTerminator(unsigned OpCode)
Definition: Instruction.h:144
Use delete by default for iplist and ilist.
Definition: ilist.h:40
#define P(N)
An ilist node that can access its parent list.
Definition: ilist_node.h:256
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:328
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
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:582
static bool isUnaryOp(unsigned Opcode)
Definition: Instruction.h:148
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Instruction.h:683
bool isBinaryOp() const
Definition: Instruction.h:130
static bool hasNoUnsignedWrap(BinaryOperator &I)
R600 Clause Merge
bool isExceptionalTerminator() const
Definition: Instruction.h:135
bool isCast() const
Definition: Instruction.h:133
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:643
static bool isCast(unsigned OpCode)
Determine if the OpCode is one of the CastInst instructions.
Definition: Instruction.h:185
void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2)
Definition: Instruction.h:305
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static void deleteNode(NodeTy *V)
Definition: ilist.h:41
bool isCommutative() const
Return true if the instruction is commutative:
Definition: Instruction.h:498
void setValueSubclassData(unsigned short D)
Definition: Value.h:715
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
Get all metadata attached to this Instruction.
Definition: Instruction.h:260
static bool isExceptionalTerminator(unsigned OpCode)
Returns true if the OpCode is a terminator related to exception handling.
Definition: Instruction.h:195
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:331
static bool isNilpotent(unsigned Opcode)
Definition: Instruction.h:531
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
#define LLVM_READONLY
Definition: Compiler.h:204
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
Definition: Instruction.h:165
static bool isFuncletPad(unsigned OpCode)
Determine if the OpCode is one of the FuncletPadInst instructions.
Definition: Instruction.h:190
bool isIndirectTerminator() const
Definition: Instruction.h:138
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
static bool isIntDivRem(unsigned Opcode)
Definition: Instruction.h:155
Instruction * getPrevNonDebugInstruction()
Definition: Instruction.h:620
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:593
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:159
bool isIntDivRem() const
Definition: Instruction.h:131
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static bool isIdempotent(unsigned Opcode)
Definition: Instruction.h:517
static bool isAssociative(const COFFSection &Section)
bool isUnaryOp() const
Definition: Instruction.h:129
static bool isAssociative(unsigned Opcode)
Definition: Instruction.h:486
const BasicBlock * getParent() const
Definition: Instruction.h:66
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
Definition: Instruction.h:542