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