13a8b0f00e6d23ae57fee93f7f2c18fe8e6b45c3cChris Lattner//===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
10009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// This file contains the declaration of the Instruction class, which is the
11fab8c796f6754962f5732145248303e3a1f7b96bChris Lattner// base class for all of the LLVM instructions.
12009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
14009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_INSTRUCTION_H
16674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_INSTRUCTION_H
17009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/ADT/ArrayRef.h"
19fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohman#include "llvm/ADT/ilist_node.h"
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/DebugLoc.h"
210c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar#include "llvm/IR/SymbolTableListTraits.h"
220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/User.h"
237a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
261638b839090a35adcd5a4b4cc0a649352276e703Michael Ilsemanclass FastMathFlags;
2712ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext;
283990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattnerclass MDNode;
290c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainarclass BasicBlock;
3037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct AAMDNodes;
31333c40096561218bc3597cf153c0a3895274414cOwen Anderson
320c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainartemplate <>
33f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarstruct SymbolTableListSentinelTraits<Instruction>
34f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    : public ilist_half_embedded_sentinel_traits<Instruction> {};
350c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
36f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarclass Instruction : public User,
37f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                    public ilist_node_with_parent<Instruction, BasicBlock> {
38ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void operator=(const Instruction &) = delete;
39ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Instruction(const Instruction &) = delete;
40c5f24a2c2a16681f7816ac053c8f46a2692a3b7aJeff Cohen
41009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  BasicBlock *Parent;
4284e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner  DebugLoc DbgLoc;                         // 'dbg' Metadata cache.
43407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
44b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  enum {
45de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// This is a bit stored in the SubClassData field which indicates whether
46de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// this instruction has metadata attached to it or not.
47b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    HasMetadataBit = 1 << 15
48b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  };
49009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
50afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen  // Out of line virtual method, so the vtable, etc has a home.
510c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  ~Instruction() override;
52407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
53de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Specialize the methods defined in Value, as we know that an instruction
54de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// can only be used by other instructions.
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Instruction       *user_back()       { return cast<Instruction>(*user_begin());}
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const Instruction *user_back() const { return cast<Instruction>(*user_begin());}
57407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
58009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  inline const BasicBlock *getParent() const { return Parent; }
59009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  inline       BasicBlock *getParent()       { return Parent; }
6018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner
61de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return the module owning the function this instruction belongs to
624c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  /// or nullptr it the function does not have a module.
634c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  ///
644c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  /// Note: this is undefined behavior if the instruction does not have a
654c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  /// parent, or the parent basic block does not have a parent function.
664c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  const Module *getModule() const;
676948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Module *getModule();
6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
69de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return the function this instruction belongs to.
70f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  ///
71f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Note: it is undefined behavior to call this on an instruction not
72f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// currently inserted into a function.
73f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  const Function *getFunction() const;
74f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Function *getFunction();
75f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
76de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// This method unlinks 'this' from the containing basic block, but does not
77de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// delete it.
78b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner  void removeFromParent();
79b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner
80de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// This method unlinks 'this' from the containing basic block and deletes it.
81b92f50fe6091a7a12f54f9884529b1127b1a14e5Chris Lattner  ///
820c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  /// \returns an iterator pointing to the element after the erased one
83f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  SymbolTableList<Instruction>::iterator eraseFromParent();
84cfad5df977f257299063309fa34f3c24831093c4Chris Lattner
856948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// Insert an unlinked instruction into a basic block immediately before
866948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// the specified instruction.
8726bb50ab48c561adfd32d129e0eff0cbf0a04625Owen Anderson  void insertBefore(Instruction *InsertPos);
8826bb50ab48c561adfd32d129e0eff0cbf0a04625Owen Anderson
896948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// Insert an unlinked instruction into a basic block immediately after the
906948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  /// specified instruction.
913ff704fa2b67d6c857142218c5aca3058b6239fcChris Lattner  void insertAfter(Instruction *InsertPos);
923ff704fa2b67d6c857142218c5aca3058b6239fcChris Lattner
93de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Unlink this instruction from its current basic block and insert it into
94de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// the basic block that MovePos lives in, right before MovePos.
95287921d1889e101cb7f5cfa031a34ebe53a9a4a0Chris Lattner  void moveBefore(Instruction *MovePos);
96287921d1889e101cb7f5cfa031a34ebe53a9a4a0Chris Lattner
973990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  //===--------------------------------------------------------------------===//
983990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  // Subclass classification.
993990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  //===--------------------------------------------------------------------===//
100407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
101de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Returns a member of one of the enums like Instruction::Add.
102a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman  unsigned getOpcode() const { return getValueID() - InstructionVal; }
103407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
104555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner  const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
105555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner  bool isTerminator() const { return isTerminator(getOpcode()); }
106555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner  bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
107555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner  bool isShift() { return isShift(getOpcode()); }
108555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner  bool isCast() const { return isCast(getOpcode()); }
109f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  bool isFuncletPad() const { return isFuncletPad(getOpcode()); }
110407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
1115814008f4b77774c8563578e1562c9c24a6750c2Vikram S. Adve  static const char* getOpcodeName(unsigned OpCode);
112009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
113dac69c83c22a00d3f8de3bb4d62b9dbeb0a20cafReid Spencer  static inline bool isTerminator(unsigned OpCode) {
114dac69c83c22a00d3f8de3bb4d62b9dbeb0a20cafReid Spencer    return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
115dac69c83c22a00d3f8de3bb4d62b9dbeb0a20cafReid Spencer  }
116dac69c83c22a00d3f8de3bb4d62b9dbeb0a20cafReid Spencer
117555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner  static inline bool isBinaryOp(unsigned Opcode) {
118555c729a2f604027840b96d8e05f7d07d1f51053Chris Lattner    return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
119009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  }
120009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
121de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Determine if the Opcode is one of the shift instructions.
122832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer  static inline bool isShift(unsigned Opcode) {
123832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer    return Opcode >= Shl && Opcode <= AShr;
124832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer  }
125832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer
126de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this is a logical shift left or a logical shift right.
127cedbacffd67939d8e8f42e5506d458ddc187e575Dan Gohman  inline bool isLogicalShift() const {
128832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer    return getOpcode() == Shl || getOpcode() == LShr;
129832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer  }
130832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer
131de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this is an arithmetic shift right.
132cedbacffd67939d8e8f42e5506d458ddc187e575Dan Gohman  inline bool isArithmeticShift() const {
133832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer    return getOpcode() == AShr;
134832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer  }
135832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer
136de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Determine if the OpCode is one of the CastInst instructions.
1373da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  static inline bool isCast(unsigned OpCode) {
1383da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
1393da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  }
1403da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
141de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Determine if the OpCode is one of the FuncletPadInst instructions.
142f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static inline bool isFuncletPad(unsigned OpCode) {
143f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return OpCode >= FuncletPadOpsBegin && OpCode < FuncletPadOpsEnd;
144f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
145f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
1463990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  //===--------------------------------------------------------------------===//
1473990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  // Metadata manipulation.
1483990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  //===--------------------------------------------------------------------===//
149407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
150de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction has any metadata attached to it.
1510c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); }
152407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
153de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction has metadata attached to it other than a
154de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// debug location.
15561336ae001e07c6d68454b1494e45954d373fb51Chris Lattner  bool hasMetadataOtherThanDebugLoc() const {
15661336ae001e07c6d68454b1494e45954d373fb51Chris Lattner    return hasMetadataHashEntry();
1573990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  }
158407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
159de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Get the metadata of given kind attached to this Instruction.
1603990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  /// If the metadata is not found then return null.
1613990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  MDNode *getMetadata(unsigned KindID) const {
162dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!hasMetadata()) return nullptr;
1633990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner    return getMetadataImpl(KindID);
1643990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  }
165407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
166de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Get the metadata of given kind attached to this Instruction.
1673990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  /// If the metadata is not found then return null.
16885dadecbd664f60f0c7e4fbb44f083d43d01cfb7Benjamin Kramer  MDNode *getMetadata(StringRef Kind) const {
169dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!hasMetadata()) return nullptr;
1703990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner    return getMetadataImpl(Kind);
1713990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  }
172407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
173de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Get all metadata attached to this Instruction. The first element of each
174de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// pair returned is the KindID, the second element is the metadata value.
175de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// This list is returned sorted by the KindID.
17637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void
17737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1783990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner    if (hasMetadata())
1793990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner      getAllMetadataImpl(MDs);
1803990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  }
181407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
182de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// This does the same thing as getAllMetadata, except that it filters out the
183de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// debug location.
18437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void getAllMetadataOtherThanDebugLoc(
18537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
18661336ae001e07c6d68454b1494e45954d373fb51Chris Lattner    if (hasMetadataOtherThanDebugLoc())
18761336ae001e07c6d68454b1494e45954d373fb51Chris Lattner      getAllMetadataOtherThanDebugLocImpl(MDs);
18861336ae001e07c6d68454b1494e45954d373fb51Chris Lattner  }
189407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
190de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Fills the AAMDNodes structure with AA metadata from this instruction.
191de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// When Merge is true, the existing AA metadata is merged with that from this
192de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// instruction providing the most-general result.
19337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void getAAMetadata(AAMDNodes &N, bool Merge = false) const;
19437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
195de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Set the metadata of the specified kind to the specified node. This updates
196de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// or replaces metadata if already present, or removes it if Node is null.
1973990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  void setMetadata(unsigned KindID, MDNode *Node);
19885dadecbd664f60f0c7e4fbb44f083d43d01cfb7Benjamin Kramer  void setMetadata(StringRef Kind, MDNode *Node);
1993990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner
200f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Drop all unknown metadata except for debug locations.
201f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// @{
20236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Passes are required to drop metadata they don't understand. This is a
20336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// convenience method for passes to do so.
204f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs);
205f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void dropUnknownNonDebugMetadata() {
206f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return dropUnknownNonDebugMetadata(None);
20736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
208f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void dropUnknownNonDebugMetadata(unsigned ID1) {
209f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return dropUnknownNonDebugMetadata(makeArrayRef(ID1));
21036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
211f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2) {
21236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    unsigned IDs[] = {ID1, ID2};
213f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return dropUnknownNonDebugMetadata(IDs);
21436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
215f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// @}
21636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
217de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Sets the metadata on this instruction from the AAMDNodes structure.
21837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void setAAMetadata(const AAMDNodes &N);
21937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
220de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Retrieve the raw weight values of a conditional branch or select.
221de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Returns true on success with profile weights filled in.
222de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Returns false if no metadata or invalid metadata was found.
223de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal);
224de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
225de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Retrieve total raw weight values of a branch.
226de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Returns true on success with profile total weights filled in.
227de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Returns false if no metadata was found.
228de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool extractProfTotalWeight(uint64_t &TotalVal);
229de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
230de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Set the debug location information for this instruction.
231ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
232407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
233de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return the debug location for this node as a DebugLoc.
23484e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner  const DebugLoc &getDebugLoc() const { return DbgLoc; }
235407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
236de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Set or clear the nsw flag on this instruction, which must be an operator
237de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// which supports this flag. See LangRef.html for the meaning of this flag.
238de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void setHasNoUnsignedWrap(bool b = true);
239de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
240de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Set or clear the nsw flag on this instruction, which must be an operator
241de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// which supports this flag. See LangRef.html for the meaning of this flag.
242de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void setHasNoSignedWrap(bool b = true);
243de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
244de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Set or clear the exact flag on this instruction, which must be an operator
245de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// which supports this flag. See LangRef.html for the meaning of this flag.
246de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void setIsExact(bool b = true);
247de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
248de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Determine whether the no unsigned wrap flag is set.
249de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool hasNoUnsignedWrap() const;
250de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
251de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Determine whether the no signed wrap flag is set.
252de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool hasNoSignedWrap() const;
253de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
254de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Determine whether the exact flag is set.
255de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool isExact() const;
256de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
257125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Set or clear the unsafe-algebra flag on this instruction, which must be an
258125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// operator which supports this flag. See LangRef.html for the meaning of
259125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// this flag.
260125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  void setHasUnsafeAlgebra(bool B);
261125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
262125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Set or clear the no-nans flag on this instruction, which must be an
263125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// operator which supports this flag. See LangRef.html for the meaning of
264125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// this flag.
265125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  void setHasNoNaNs(bool B);
266125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
267125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Set or clear the no-infs flag on this instruction, which must be an
268125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// operator which supports this flag. See LangRef.html for the meaning of
269125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// this flag.
270125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  void setHasNoInfs(bool B);
271125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
272125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Set or clear the no-signed-zeros flag on this instruction, which must be
273125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// an operator which supports this flag. See LangRef.html for the meaning of
274125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// this flag.
275125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  void setHasNoSignedZeros(bool B);
276125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
277125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Set or clear the allow-reciprocal flag on this instruction, which must be
278125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// an operator which supports this flag. See LangRef.html for the meaning of
279125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// this flag.
280125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  void setHasAllowReciprocal(bool B);
281125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
28237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Convenience function for setting multiple fast-math flags on this
283125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// instruction, which must be an operator which supports these flags. See
28437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// LangRef.html for the meaning of these flags.
285125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  void setFastMathFlags(FastMathFlags FMF);
286125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
28737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Convenience function for transferring all fast-math flag values to this
28837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// instruction, which must be an operator which supports these flags. See
28937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// LangRef.html for the meaning of these flags.
29037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void copyFastMathFlags(FastMathFlags FMF);
29137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
292125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Determine whether the unsafe-algebra flag is set.
293125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  bool hasUnsafeAlgebra() const;
294125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
295125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Determine whether the no-NaNs flag is set.
296125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  bool hasNoNaNs() const;
297125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
298125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Determine whether the no-infs flag is set.
299125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  bool hasNoInfs() const;
300125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
301125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Determine whether the no-signed-zeros flag is set.
302125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  bool hasNoSignedZeros() const;
303125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
304125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Determine whether the allow-reciprocal flag is set.
305125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  bool hasAllowReciprocal() const;
306125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
307125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// Convenience function for getting all the fast-math flags, which must be an
308125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  /// operator which supports these flags. See LangRef.html for the meaning of
30937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// these flags.
310125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman  FastMathFlags getFastMathFlags() const;
311125fc7fefd4a198dd210cb43f5d8f1ba2c1e2dcfMichael Ilseman
3124b896dd613b1d85ee1b261ee470cb72fab24c282Michael Ilseman  /// Copy I's fast-math flags
3134b896dd613b1d85ee1b261ee470cb72fab24c282Michael Ilseman  void copyFastMathFlags(const Instruction *I);
3144b896dd613b1d85ee1b261ee470cb72fab24c282Michael Ilseman
315de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Convenience method to copy supported wrapping, exact, and fast-math flags
316de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// from V to this instruction.
317de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void copyIRFlags(const Value *V);
318de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
319de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
320de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// V and this instruction.
321de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void andIRFlags(const Value *V);
322de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
3233990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattnerprivate:
324de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if we have an entry in the on-the-side metadata hash.
325ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  bool hasMetadataHashEntry() const {
326ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner    return (getSubclassDataFromValue() & HasMetadataBit) != 0;
327ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  }
328407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
3293990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  // These are all implemented in Metadata.cpp.
3303990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  MDNode *getMetadataImpl(unsigned KindID) const;
33185dadecbd664f60f0c7e4fbb44f083d43d01cfb7Benjamin Kramer  MDNode *getMetadataImpl(StringRef Kind) const;
33237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void
33337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
33437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void getAllMetadataOtherThanDebugLocImpl(
33537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
336de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Clear all hashtable-based metadata from this instruction.
3374f1be4abba762f8a7b77d7622abaf1ed1a87b48bDan Gohman  void clearMetadataHashEntries();
3383990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattnerpublic:
3393990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  //===--------------------------------------------------------------------===//
3403990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  // Predicates and helper methods.
3413990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  //===--------------------------------------------------------------------===//
342407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
343407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
344de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the instruction is associative:
345f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  ///
346686e65fb65f97176e000c4d4cae82fc7b404ef9dMisha Brukman  ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
347f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  ///
3480d7ce5ffa4aa853b75e1015c62e27bd9f23ef73bDuncan Sands  /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
349f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  ///
3509b7f6f2de89a321f7eae5e942c8668cb50acfd1dShuxin Yang  bool isAssociative() const;
3510d7ce5ffa4aa853b75e1015c62e27bd9f23ef73bDuncan Sands  static bool isAssociative(unsigned op);
352f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner
353de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the instruction is commutative:
354f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  ///
355686e65fb65f97176e000c4d4cae82fc7b404ef9dMisha Brukman  ///   Commutative operators satisfy: (x op y) === (y op x)
356f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  ///
357f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
358f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  /// applied to any type.
359f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  ///
360f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  bool isCommutative() const { return isCommutative(getOpcode()); }
361f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner  static bool isCommutative(unsigned op);
362f2da7241f590aaae128ecce7732c6094084df2b6Chris Lattner
363de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the instruction is idempotent:
364c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
365c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///   Idempotent operators satisfy:  x op x === x
366c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
367c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  /// In LLVM, the And and Or operators are idempotent.
368c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
369c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  bool isIdempotent() const { return isIdempotent(getOpcode()); }
370c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  static bool isIdempotent(unsigned op);
371c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
372de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the instruction is nilpotent:
373c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
374c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///   Nilpotent operators satisfy:  x op x === Id,
375c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
376c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///   where Id is the identity for the operator, i.e. a constant such that
377c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///     x op Id === x and Id op x === x for all x.
378c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
379c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  /// In LLVM, the Xor operator is nilpotent.
380c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  ///
381c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  bool isNilpotent() const { return isNilpotent(getOpcode()); }
382c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  static bool isNilpotent(unsigned op);
383c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
384de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction may modify memory.
3857af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  bool mayWriteToMemory() const;
3867af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands
387de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction may read memory.
3887af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  bool mayReadFromMemory() const;
3897af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands
390de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction may read or write memory.
3916f3ba37ebb06de206c74d73c7c2b422cca28a16dEli Friedman  bool mayReadOrWriteMemory() const {
3926f3ba37ebb06de206c74d73c7c2b422cca28a16dEli Friedman    return mayReadFromMemory() || mayWriteToMemory();
3936f3ba37ebb06de206c74d73c7c2b422cca28a16dEli Friedman  }
3946f3ba37ebb06de206c74d73c7c2b422cca28a16dEli Friedman
395de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction has an AtomicOrdering of unordered or
396de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// higher.
39737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  bool isAtomic() const;
39837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
399de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if this instruction may throw an exception.
4007af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  bool mayThrow() const;
4017af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands
402de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the instruction may have side effects.
4037af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands  ///
4048b7d706c80afa7ad40acaab08f7406093a2851ffEli Friedman  /// Note that this does not consider malloc and alloca to have side
4058b7d706c80afa7ad40acaab08f7406093a2851ffEli Friedman  /// effects because the newly allocated memory is completely invisible to
406f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// instructions which don't use the returned value.  For cases where this
4078b7d706c80afa7ad40acaab08f7406093a2851ffEli Friedman  /// matters, isSafeToSpeculativelyExecute may be more appropriate.
408de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); }
4097af1c78b98d2df7d0ab9154461ca3d835706716eDuncan Sands
410de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the instruction is a variety of EH-block.
411f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  bool isEHPad() const {
412f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    switch (getOpcode()) {
413f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    case Instruction::CatchSwitch:
414f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    case Instruction::CatchPad:
415f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    case Instruction::CleanupPad:
416f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    case Instruction::LandingPad:
417f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      return true;
418f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    default:
419f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      return false;
420f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    }
421f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
422f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
423de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Create a copy of 'this' instruction that is identical in all ways except
424de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// the following:
425f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  ///   * The instruction has no parent
426f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  ///   * The instruction has no name
427f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  ///
428f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  Instruction *clone() const;
429407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
430de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if the specified instruction is exactly identical to the
431de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// current one. This means that all operands match and any extra information
432de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// (e.g. load is volatile) agree.
433f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  bool isIdenticalTo(const Instruction *I) const;
434407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
435de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// This is like isIdenticalTo, except that it ignores the
436de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// SubclassOptionalData flags, which specify conditions under which the
437de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// instruction's result is undefined.
438f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  bool isIdenticalToWhenDefined(const Instruction *I) const;
439ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel
440ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel  /// When checking for operation equivalence (using isSameOperationAs) it is
441ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel  /// sometimes useful to ignore certain attributes.
442ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel  enum OperationEquivalenceFlags {
443ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel    /// Check for equivalence ignoring load/store alignment.
444ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel    CompareIgnoringAlignment = 1<<0,
445ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel    /// Check for equivalence treating a type and a vector of that type
446ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel    /// as equivalent.
447ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel    CompareUsingScalarTypes = 1<<1
448ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel  };
449407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
450f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// This function determines if the specified instruction executes the same
451f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// operation as the current one. This means that the opcodes, type, operand
452f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// types and any other factors affecting the operation must be the same. This
453f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// is similar to isIdenticalTo except the operands themselves don't have to
454f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// be identical.
455f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// @returns true if the specified instruction is the same operation as
456f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// the current one.
457f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  /// @brief Determine if one instruction is the same operation as another.
458ec4e85e3364f50802f2007e4b1e23661d4610366Hal Finkel  bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const;
459407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
460de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Return true if there are any uses of this instruction in blocks other than
461de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// the specified block. Note that PHI nodes are considered to evaluate their
462de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// operands in the corresponding predecessor block.
463f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
464407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
465407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
46626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
467b00c582b6d40e6b9ff2d1ed4f5eaf7930e792aceChris Lattner  static inline bool classof(const Value *V) {
468a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() >= Value::InstructionVal;
4697295eb4ea3e3a81e697600cbca681674e4b35a20Chris Lattner  }
4709769ab22265b313171d201b5928688524a01bd87Misha Brukman
471009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  //----------------------------------------------------------------------
472f309880ad86114cda05037538c46123f6cda1a7eChris Lattner  // Exported enumerations.
473009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  //
474009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  enum TermOps {       // These terminate basic blocks
4750b16ae209a1d0876a7ea6800bb567d925443cba3Chris Lattner#define  FIRST_TERM_INST(N)             TermOpsBegin = N,
476f96315e985880178562ba43b51efbc0efc8f6c8aChris Lattner#define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
47782870e0b733fa72c759271c1c62cb5f07be2c4dbChris Lattner#define   LAST_TERM_INST(N)             TermOpsEnd = N+1
4780b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.def"
479009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  };
480009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
481009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  enum BinaryOps {
4820b16ae209a1d0876a7ea6800bb567d925443cba3Chris Lattner#define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
483f96315e985880178562ba43b51efbc0efc8f6c8aChris Lattner#define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
48482870e0b733fa72c759271c1c62cb5f07be2c4dbChris Lattner#define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
4850b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.def"
486009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  };
487009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
488009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  enum MemoryOps {
4890b16ae209a1d0876a7ea6800bb567d925443cba3Chris Lattner#define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
490f96315e985880178562ba43b51efbc0efc8f6c8aChris Lattner#define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
49182870e0b733fa72c759271c1c62cb5f07be2c4dbChris Lattner#define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
4920b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.def"
493009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  };
494009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
4953da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  enum CastOps {
4963da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer#define  FIRST_CAST_INST(N)             CastOpsBegin = N,
4973da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer#define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
4983da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer#define   LAST_CAST_INST(N)             CastOpsEnd = N+1
4990b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.def"
5003da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  };
5013da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
502f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  enum FuncletPadOps {
503f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#define  FIRST_FUNCLETPAD_INST(N)             FuncletPadOpsBegin = N,
504f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#define HANDLE_FUNCLETPAD_INST(N, OPC, CLASS) OPC = N,
505f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#define   LAST_FUNCLETPAD_INST(N)             FuncletPadOpsEnd = N+1
506f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include "llvm/IR/Instruction.def"
507f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  };
508f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
509009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  enum OtherOps {
5100b16ae209a1d0876a7ea6800bb567d925443cba3Chris Lattner#define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
511f96315e985880178562ba43b51efbc0efc8f6c8aChris Lattner#define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
51282870e0b733fa72c759271c1c62cb5f07be2c4dbChris Lattner#define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
5130b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.def"
514009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  };
515b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattnerprivate:
516b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  // Shadow Value::setValueSubclassData with a private forwarding method so that
517b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  // subclasses cannot accidentally use it.
518b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  void setValueSubclassData(unsigned short D) {
519b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    Value::setValueSubclassData(D);
520b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  }
521b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  unsigned short getSubclassDataFromValue() const {
522b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    return Value::getSubclassDataFromValue();
523b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  }
524407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
525ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  void setHasMetadataHashEntry(bool V) {
526b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
527b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner                         (V ? HasMetadataBit : 0));
528b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  }
529407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
530f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  friend class SymbolTableListTraits<Instruction>;
531b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  void setParent(BasicBlock *P);
532b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattnerprotected:
533b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  // Instruction subclasses can stick up to 15 bits of stuff into the
534b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  // SubclassData field of instruction with these members.
535407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
536b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  // Verify that only the low 15 bits are used.
537b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  void setInstructionSubclassData(unsigned short D) {
538b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
539b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
540b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  }
541407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
542b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  unsigned getSubclassDataFromInstruction() const {
543b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner    return getSubclassDataFromValue() & ~HasMetadataBit;
544b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner  }
545407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
546db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
547dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              Instruction *InsertBefore = nullptr);
548db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
549b2406d9895314cbc61183c2fb712cd1a2ddfe7e0Chris Lattner              BasicBlock *InsertAtEnd);
550407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
551f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarprivate:
552f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Create a copy of this instruction.
553f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Instruction *cloneImpl() const;
554009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
555009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
556e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner// Instruction* is only 4-byte aligned.
557e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattnertemplate<>
558e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattnerclass PointerLikeTypeTraits<Instruction*> {
559e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner  typedef Instruction* PT;
560e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattnerpublic:
561e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner  static inline void *getAsVoidPointer(PT P) { return P; }
562e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner  static inline PT getFromVoidPointer(void *P) {
563e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner    return static_cast<PT>(P);
564e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner  }
565e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner  enum { NumLowBitsAvailable = 2 };
566e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattner};
567407a6169b729c72c3a7ddb01b8454ab0b4f6897cMichael Ilseman
568d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
569d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
570009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
571