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