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