1 | //===- llvm/BasicBlock.h - Represent a basic block in the VM ----*- 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 BasicBlock class. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_IR_BASICBLOCK_H |
14 | #define LLVM_IR_BASICBLOCK_H |
15 | |
16 | #include "llvm-c/Types.h" |
17 | #include "llvm/ADT/Twine.h" |
18 | #include "llvm/ADT/ilist.h" |
19 | #include "llvm/ADT/ilist_node.h" |
20 | #include "llvm/ADT/iterator.h" |
21 | #include "llvm/ADT/iterator_range.h" |
22 | #include "llvm/IR/Instruction.h" |
23 | #include "llvm/IR/SymbolTableListTraits.h" |
24 | #include "llvm/IR/Value.h" |
25 | #include "llvm/Support/CBindingWrapping.h" |
26 | #include "llvm/Support/Casting.h" |
27 | #include "llvm/Support/Compiler.h" |
28 | #include <cassert> |
29 | #include <cstddef> |
30 | #include <iterator> |
31 | |
32 | namespace llvm { |
33 | |
34 | class AssemblyAnnotationWriter; |
35 | class CallInst; |
36 | class Function; |
37 | class LandingPadInst; |
38 | class LLVMContext; |
39 | class Module; |
40 | class PHINode; |
41 | class ValueSymbolTable; |
42 | |
43 | /// LLVM Basic Block Representation |
44 | /// |
45 | /// This represents a single basic block in LLVM. A basic block is simply a |
46 | /// container of instructions that execute sequentially. Basic blocks are Values |
47 | /// because they are referenced by instructions such as branches and switch |
48 | /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block |
49 | /// represents a label to which a branch can jump. |
50 | /// |
51 | /// A well formed basic block is formed of a list of non-terminating |
52 | /// instructions followed by a single terminator instruction. Terminator |
53 | /// instructions may not occur in the middle of basic blocks, and must terminate |
54 | /// the blocks. The BasicBlock class allows malformed basic blocks to occur |
55 | /// because it may be useful in the intermediate stage of constructing or |
56 | /// modifying a program. However, the verifier will ensure that basic blocks are |
57 | /// "well formed". |
58 | class BasicBlock final : public Value, // Basic blocks are data objects also |
59 | public ilist_node_with_parent<BasicBlock, Function> { |
60 | public: |
61 | using InstListType = SymbolTableList<Instruction>; |
62 | |
63 | private: |
64 | friend class BlockAddress; |
65 | friend class SymbolTableListTraits<BasicBlock>; |
66 | |
67 | InstListType InstList; |
68 | Function *Parent; |
69 | |
70 | void setParent(Function *parent); |
71 | |
72 | /// Constructor. |
73 | /// |
74 | /// If the function parameter is specified, the basic block is automatically |
75 | /// inserted at either the end of the function (if InsertBefore is null), or |
76 | /// before the specified basic block. |
77 | explicit BasicBlock(LLVMContext &C, const Twine &Name = "" , |
78 | Function *Parent = nullptr, |
79 | BasicBlock *InsertBefore = nullptr); |
80 | |
81 | public: |
82 | BasicBlock(const BasicBlock &) = delete; |
83 | BasicBlock &operator=(const BasicBlock &) = delete; |
84 | ~BasicBlock(); |
85 | |
86 | /// Get the context in which this basic block lives. |
87 | LLVMContext &getContext() const; |
88 | |
89 | /// Instruction iterators... |
90 | using iterator = InstListType::iterator; |
91 | using const_iterator = InstListType::const_iterator; |
92 | using reverse_iterator = InstListType::reverse_iterator; |
93 | using const_reverse_iterator = InstListType::const_reverse_iterator; |
94 | |
95 | /// Creates a new BasicBlock. |
96 | /// |
97 | /// If the Parent parameter is specified, the basic block is automatically |
98 | /// inserted at either the end of the function (if InsertBefore is 0), or |
99 | /// before the specified basic block. |
100 | static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "" , |
101 | Function *Parent = nullptr, |
102 | BasicBlock *InsertBefore = nullptr) { |
103 | return new BasicBlock(Context, Name, Parent, InsertBefore); |
104 | } |
105 | |
106 | /// Return the enclosing method, or null if none. |
107 | const Function *getParent() const { return Parent; } |
108 | Function *getParent() { return Parent; } |
109 | |
110 | /// Return the module owning the function this basic block belongs to, or |
111 | /// nullptr if the function does not have a module. |
112 | /// |
113 | /// Note: this is undefined behavior if the block does not have a parent. |
114 | const Module *getModule() const; |
115 | Module *getModule() { |
116 | return const_cast<Module *>( |
117 | static_cast<const BasicBlock *>(this)->getModule()); |
118 | } |
119 | |
120 | /// Returns the terminator instruction if the block is well formed or null |
121 | /// if the block is not well formed. |
122 | const Instruction *getTerminator() const LLVM_READONLY; |
123 | Instruction *getTerminator() { |
124 | return const_cast<Instruction *>( |
125 | static_cast<const BasicBlock *>(this)->getTerminator()); |
126 | } |
127 | |
128 | /// Returns the call instruction calling \@llvm.experimental.deoptimize |
129 | /// prior to the terminating return instruction of this basic block, if such |
130 | /// a call is present. Otherwise, returns null. |
131 | const CallInst *getTerminatingDeoptimizeCall() const; |
132 | CallInst *getTerminatingDeoptimizeCall() { |
133 | return const_cast<CallInst *>( |
134 | static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall()); |
135 | } |
136 | |
137 | /// Returns the call instruction calling \@llvm.experimental.deoptimize |
138 | /// that is present either in current basic block or in block that is a unique |
139 | /// successor to current block, if such call is present. Otherwise, returns null. |
140 | const CallInst *getPostdominatingDeoptimizeCall() const; |
141 | CallInst *getPostdominatingDeoptimizeCall() { |
142 | return const_cast<CallInst *>( |
143 | static_cast<const BasicBlock *>(this)->getPostdominatingDeoptimizeCall()); |
144 | } |
145 | |
146 | /// Returns the call instruction marked 'musttail' prior to the terminating |
147 | /// return instruction of this basic block, if such a call is present. |
148 | /// Otherwise, returns null. |
149 | const CallInst *getTerminatingMustTailCall() const; |
150 | CallInst *getTerminatingMustTailCall() { |
151 | return const_cast<CallInst *>( |
152 | static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall()); |
153 | } |
154 | |
155 | /// Returns a pointer to the first instruction in this block that is not a |
156 | /// PHINode instruction. |
157 | /// |
158 | /// When adding instructions to the beginning of the basic block, they should |
159 | /// be added before the returned value, not before the first instruction, |
160 | /// which might be PHI. Returns 0 is there's no non-PHI instruction. |
161 | const Instruction* getFirstNonPHI() const; |
162 | Instruction* getFirstNonPHI() { |
163 | return const_cast<Instruction *>( |
164 | static_cast<const BasicBlock *>(this)->getFirstNonPHI()); |
165 | } |
166 | |
167 | /// Returns a pointer to the first instruction in this block that is not a |
168 | /// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp |
169 | /// is true. |
170 | const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = false) const; |
171 | Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = false) { |
172 | return const_cast<Instruction *>( |
173 | static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg( |
174 | SkipPseudoOp)); |
175 | } |
176 | |
177 | /// Returns a pointer to the first instruction in this block that is not a |
178 | /// PHINode, a debug intrinsic, or a lifetime intrinsic, or any pseudo |
179 | /// operation if \c SkipPseudoOp is true. |
180 | const Instruction * |
181 | getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = false) const; |
182 | Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = false) { |
183 | return const_cast<Instruction *>( |
184 | static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime( |
185 | SkipPseudoOp)); |
186 | } |
187 | |
188 | /// Returns an iterator to the first instruction in this block that is |
189 | /// suitable for inserting a non-PHI instruction. |
190 | /// |
191 | /// In particular, it skips all PHIs and LandingPad instructions. |
192 | const_iterator getFirstInsertionPt() const; |
193 | iterator getFirstInsertionPt() { |
194 | return static_cast<const BasicBlock *>(this) |
195 | ->getFirstInsertionPt().getNonConst(); |
196 | } |
197 | |
198 | /// Return a const iterator range over the instructions in the block, skipping |
199 | /// any debug instructions. Skip any pseudo operations as well if \c |
200 | /// SkipPseudoOp is true. |
201 | iterator_range<filter_iterator<BasicBlock::const_iterator, |
202 | std::function<bool(const Instruction &)>>> |
203 | instructionsWithoutDebug(bool SkipPseudoOp = false) const; |
204 | |
205 | /// Return an iterator range over the instructions in the block, skipping any |
206 | /// debug instructions. Skip and any pseudo operations as well if \c |
207 | /// SkipPseudoOp is true. |
208 | iterator_range< |
209 | filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>> |
210 | instructionsWithoutDebug(bool SkipPseudoOp = false); |
211 | |
212 | /// Return the size of the basic block ignoring debug instructions |
213 | filter_iterator<BasicBlock::const_iterator, |
214 | std::function<bool(const Instruction &)>>::difference_type |
215 | sizeWithoutDebug() const; |
216 | |
217 | /// Unlink 'this' from the containing function, but do not delete it. |
218 | void removeFromParent(); |
219 | |
220 | /// Unlink 'this' from the containing function and delete it. |
221 | /// |
222 | // \returns an iterator pointing to the element after the erased one. |
223 | SymbolTableList<BasicBlock>::iterator eraseFromParent(); |
224 | |
225 | /// Unlink this basic block from its current function and insert it into |
226 | /// the function that \p MovePos lives in, right before \p MovePos. |
227 | void moveBefore(BasicBlock *MovePos); |
228 | |
229 | /// Unlink this basic block from its current function and insert it |
230 | /// right after \p MovePos in the function \p MovePos lives in. |
231 | void moveAfter(BasicBlock *MovePos); |
232 | |
233 | /// Insert unlinked basic block into a function. |
234 | /// |
235 | /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is |
236 | /// provided, inserts before that basic block, otherwise inserts at the end. |
237 | /// |
238 | /// \pre \a getParent() is \c nullptr. |
239 | void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); |
240 | |
241 | /// Return the predecessor of this block if it has a single predecessor |
242 | /// block. Otherwise return a null pointer. |
243 | const BasicBlock *getSinglePredecessor() const; |
244 | BasicBlock *getSinglePredecessor() { |
245 | return const_cast<BasicBlock *>( |
246 | static_cast<const BasicBlock *>(this)->getSinglePredecessor()); |
247 | } |
248 | |
249 | /// Return the predecessor of this block if it has a unique predecessor |
250 | /// block. Otherwise return a null pointer. |
251 | /// |
252 | /// Note that unique predecessor doesn't mean single edge, there can be |
253 | /// multiple edges from the unique predecessor to this block (for example a |
254 | /// switch statement with multiple cases having the same destination). |
255 | const BasicBlock *getUniquePredecessor() const; |
256 | BasicBlock *getUniquePredecessor() { |
257 | return const_cast<BasicBlock *>( |
258 | static_cast<const BasicBlock *>(this)->getUniquePredecessor()); |
259 | } |
260 | |
261 | /// Return true if this block has exactly N predecessors. |
262 | bool hasNPredecessors(unsigned N) const; |
263 | |
264 | /// Return true if this block has N predecessors or more. |
265 | bool hasNPredecessorsOrMore(unsigned N) const; |
266 | |
267 | /// Return the successor of this block if it has a single successor. |
268 | /// Otherwise return a null pointer. |
269 | /// |
270 | /// This method is analogous to getSinglePredecessor above. |
271 | const BasicBlock *getSingleSuccessor() const; |
272 | BasicBlock *getSingleSuccessor() { |
273 | return const_cast<BasicBlock *>( |
274 | static_cast<const BasicBlock *>(this)->getSingleSuccessor()); |
275 | } |
276 | |
277 | /// Return the successor of this block if it has a unique successor. |
278 | /// Otherwise return a null pointer. |
279 | /// |
280 | /// This method is analogous to getUniquePredecessor above. |
281 | const BasicBlock *getUniqueSuccessor() const; |
282 | BasicBlock *getUniqueSuccessor() { |
283 | return const_cast<BasicBlock *>( |
284 | static_cast<const BasicBlock *>(this)->getUniqueSuccessor()); |
285 | } |
286 | |
287 | /// Print the basic block to an output stream with an optional |
288 | /// AssemblyAnnotationWriter. |
289 | void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr, |
290 | bool ShouldPreserveUseListOrder = false, |
291 | bool IsForDebug = false) const; |
292 | |
293 | //===--------------------------------------------------------------------===// |
294 | /// Instruction iterator methods |
295 | /// |
296 | inline iterator begin() { return InstList.begin(); } |
297 | inline const_iterator begin() const { return InstList.begin(); } |
298 | inline iterator end () { return InstList.end(); } |
299 | inline const_iterator end () const { return InstList.end(); } |
300 | |
301 | inline reverse_iterator rbegin() { return InstList.rbegin(); } |
302 | inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } |
303 | inline reverse_iterator rend () { return InstList.rend(); } |
304 | inline const_reverse_iterator rend () const { return InstList.rend(); } |
305 | |
306 | inline size_t size() const { return InstList.size(); } |
307 | inline bool empty() const { return InstList.empty(); } |
308 | inline const Instruction &front() const { return InstList.front(); } |
309 | inline Instruction &front() { return InstList.front(); } |
310 | inline const Instruction &back() const { return InstList.back(); } |
311 | inline Instruction &back() { return InstList.back(); } |
312 | |
313 | /// Iterator to walk just the phi nodes in the basic block. |
314 | template <typename PHINodeT = PHINode, typename BBIteratorT = iterator> |
315 | class phi_iterator_impl |
316 | : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>, |
317 | std::forward_iterator_tag, PHINodeT> { |
318 | friend BasicBlock; |
319 | |
320 | PHINodeT *PN; |
321 | |
322 | phi_iterator_impl(PHINodeT *PN) : PN(PN) {} |
323 | |
324 | public: |
325 | // Allow default construction to build variables, but this doesn't build |
326 | // a useful iterator. |
327 | phi_iterator_impl() = default; |
328 | |
329 | // Allow conversion between instantiations where valid. |
330 | template <typename PHINodeU, typename BBIteratorU, |
331 | typename = std::enable_if_t< |
332 | std::is_convertible<PHINodeU *, PHINodeT *>::value>> |
333 | phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg) |
334 | : PN(Arg.PN) {} |
335 | |
336 | bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; } |
337 | |
338 | PHINodeT &operator*() const { return *PN; } |
339 | |
340 | using phi_iterator_impl::iterator_facade_base::operator++; |
341 | phi_iterator_impl &operator++() { |
342 | assert(PN && "Cannot increment the end iterator!" ); |
343 | PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN))); |
344 | return *this; |
345 | } |
346 | }; |
347 | using phi_iterator = phi_iterator_impl<>; |
348 | using const_phi_iterator = |
349 | phi_iterator_impl<const PHINode, BasicBlock::const_iterator>; |
350 | |
351 | /// Returns a range that iterates over the phis in the basic block. |
352 | /// |
353 | /// Note that this cannot be used with basic blocks that have no terminator. |
354 | iterator_range<const_phi_iterator> phis() const { |
355 | return const_cast<BasicBlock *>(this)->phis(); |
356 | } |
357 | iterator_range<phi_iterator> phis(); |
358 | |
359 | /// Return the underlying instruction list container. |
360 | /// |
361 | /// Currently you need to access the underlying instruction list container |
362 | /// directly if you want to modify it. |
363 | const InstListType &getInstList() const { return InstList; } |
364 | InstListType &getInstList() { return InstList; } |
365 | |
366 | /// Returns a pointer to a member of the instruction list. |
367 | static InstListType BasicBlock::*getSublistAccess(Instruction*) { |
368 | return &BasicBlock::InstList; |
369 | } |
370 | |
371 | /// Returns a pointer to the symbol table if one exists. |
372 | ValueSymbolTable *getValueSymbolTable(); |
373 | |
374 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
375 | static bool classof(const Value *V) { |
376 | return V->getValueID() == Value::BasicBlockVal; |
377 | } |
378 | |
379 | /// Cause all subinstructions to "let go" of all the references that said |
380 | /// subinstructions are maintaining. |
381 | /// |
382 | /// This allows one to 'delete' a whole class at a time, even though there may |
383 | /// be circular references... first all references are dropped, and all use |
384 | /// counts go to zero. Then everything is delete'd for real. Note that no |
385 | /// operations are valid on an object that has "dropped all references", |
386 | /// except operator delete. |
387 | void dropAllReferences(); |
388 | |
389 | /// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred. |
390 | /// Note that this function does not actually remove the predecessor. |
391 | /// |
392 | /// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with |
393 | /// zero or one incoming values, and don't simplify PHIs with all incoming |
394 | /// values the same. |
395 | void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false); |
396 | |
397 | bool canSplitPredecessors() const; |
398 | |
399 | /// Split the basic block into two basic blocks at the specified instruction. |
400 | /// |
401 | /// If \p Before is true, splitBasicBlockBefore handles the |
402 | /// block splitting. Otherwise, execution proceeds as described below. |
403 | /// |
404 | /// Note that all instructions BEFORE the specified iterator |
405 | /// stay as part of the original basic block, an unconditional branch is added |
406 | /// to the original BB, and the rest of the instructions in the BB are moved |
407 | /// to the new BB, including the old terminator. The newly formed basic block |
408 | /// is returned. This function invalidates the specified iterator. |
409 | /// |
410 | /// Note that this only works on well formed basic blocks (must have a |
411 | /// terminator), and \p 'I' must not be the end of instruction list (which |
412 | /// would cause a degenerate basic block to be formed, having a terminator |
413 | /// inside of the basic block). |
414 | /// |
415 | /// Also note that this doesn't preserve any passes. To split blocks while |
416 | /// keeping loop information consistent, use the SplitBlock utility function. |
417 | BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "" , |
418 | bool Before = false); |
419 | BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "" , |
420 | bool Before = false) { |
421 | return splitBasicBlock(I->getIterator(), BBName, Before); |
422 | } |
423 | |
424 | /// Split the basic block into two basic blocks at the specified instruction |
425 | /// and insert the new basic blocks as the predecessor of the current block. |
426 | /// |
427 | /// This function ensures all instructions AFTER and including the specified |
428 | /// iterator \p I are part of the original basic block. All Instructions |
429 | /// BEFORE the iterator \p I are moved to the new BB and an unconditional |
430 | /// branch is added to the new BB. The new basic block is returned. |
431 | /// |
432 | /// Note that this only works on well formed basic blocks (must have a |
433 | /// terminator), and \p 'I' must not be the end of instruction list (which |
434 | /// would cause a degenerate basic block to be formed, having a terminator |
435 | /// inside of the basic block). \p 'I' cannot be a iterator for a PHINode |
436 | /// with multiple incoming blocks. |
437 | /// |
438 | /// Also note that this doesn't preserve any passes. To split blocks while |
439 | /// keeping loop information consistent, use the SplitBlockBefore utility |
440 | /// function. |
441 | BasicBlock *splitBasicBlockBefore(iterator I, const Twine &BBName = "" ); |
442 | BasicBlock *splitBasicBlockBefore(Instruction *I, const Twine &BBName = "" ) { |
443 | return splitBasicBlockBefore(I->getIterator(), BBName); |
444 | } |
445 | |
446 | /// Returns true if there are any uses of this basic block other than |
447 | /// direct branches, switches, etc. to it. |
448 | bool hasAddressTaken() const { |
449 | return getBasicBlockBits().BlockAddressRefCount != 0; |
450 | } |
451 | |
452 | /// Update all phi nodes in this basic block to refer to basic block \p New |
453 | /// instead of basic block \p Old. |
454 | void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New); |
455 | |
456 | /// Update all phi nodes in this basic block's successors to refer to basic |
457 | /// block \p New instead of basic block \p Old. |
458 | void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New); |
459 | |
460 | /// Update all phi nodes in this basic block's successors to refer to basic |
461 | /// block \p New instead of to it. |
462 | void replaceSuccessorsPhiUsesWith(BasicBlock *New); |
463 | |
464 | /// Return true if this basic block is an exception handling block. |
465 | bool isEHPad() const { return getFirstNonPHI()->isEHPad(); } |
466 | |
467 | /// Return true if this basic block is a landing pad. |
468 | /// |
469 | /// Being a ``landing pad'' means that the basic block is the destination of |
470 | /// the 'unwind' edge of an invoke instruction. |
471 | bool isLandingPad() const; |
472 | |
473 | /// Return the landingpad instruction associated with the landing pad. |
474 | const LandingPadInst *getLandingPadInst() const; |
475 | LandingPadInst *getLandingPadInst() { |
476 | return const_cast<LandingPadInst *>( |
477 | static_cast<const BasicBlock *>(this)->getLandingPadInst()); |
478 | } |
479 | |
480 | /// Return true if it is legal to hoist instructions into this block. |
481 | bool isLegalToHoistInto() const; |
482 | |
483 | Optional<uint64_t> () const; |
484 | |
485 | /// Returns true if the Order field of child Instructions is valid. |
486 | bool isInstrOrderValid() const { |
487 | return getBasicBlockBits().InstrOrderValid; |
488 | } |
489 | |
490 | /// Mark instruction ordering invalid. Done on every instruction insert. |
491 | void invalidateOrders() { |
492 | validateInstrOrdering(); |
493 | BasicBlockBits Bits = getBasicBlockBits(); |
494 | Bits.InstrOrderValid = false; |
495 | setBasicBlockBits(Bits); |
496 | } |
497 | |
498 | /// Renumber instructions and mark the ordering as valid. |
499 | void renumberInstructions(); |
500 | |
501 | /// Asserts that instruction order numbers are marked invalid, or that they |
502 | /// are in ascending order. This is constant time if the ordering is invalid, |
503 | /// and linear in the number of instructions if the ordering is valid. Callers |
504 | /// should be careful not to call this in ways that make common operations |
505 | /// O(n^2). For example, it takes O(n) time to assign order numbers to |
506 | /// instructions, so the order should be validated no more than once after |
507 | /// each ordering to ensure that transforms have the same algorithmic |
508 | /// complexity when asserts are enabled as when they are disabled. |
509 | void validateInstrOrdering() const; |
510 | |
511 | private: |
512 | #if defined(_AIX) && (!defined(__GNUC__) || defined(__ibmxl__)) |
513 | // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words |
514 | // and give the `pack` pragma push semantics. |
515 | #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)") |
516 | #define END_TWO_BYTE_PACK() _Pragma("pack(pop)") |
517 | #else |
518 | #define BEGIN_TWO_BYTE_PACK() |
519 | #define END_TWO_BYTE_PACK() |
520 | #endif |
521 | |
522 | BEGIN_TWO_BYTE_PACK() |
523 | /// Bitfield to help interpret the bits in Value::SubclassData. |
524 | struct BasicBlockBits { |
525 | unsigned short BlockAddressRefCount : 15; |
526 | unsigned short InstrOrderValid : 1; |
527 | }; |
528 | END_TWO_BYTE_PACK() |
529 | |
530 | #undef BEGIN_TWO_BYTE_PACK |
531 | #undef END_TWO_BYTE_PACK |
532 | |
533 | /// Safely reinterpret the subclass data bits to a more useful form. |
534 | BasicBlockBits getBasicBlockBits() const { |
535 | static_assert(sizeof(BasicBlockBits) == sizeof(unsigned short), |
536 | "too many bits for Value::SubclassData" ); |
537 | unsigned short ValueData = getSubclassDataFromValue(); |
538 | BasicBlockBits AsBits; |
539 | memcpy(&AsBits, &ValueData, sizeof(AsBits)); |
540 | return AsBits; |
541 | } |
542 | |
543 | /// Reinterpret our subclass bits and store them back into Value. |
544 | void setBasicBlockBits(BasicBlockBits AsBits) { |
545 | unsigned short D; |
546 | memcpy(&D, &AsBits, sizeof(D)); |
547 | Value::setValueSubclassData(D); |
548 | } |
549 | |
550 | /// Increment the internal refcount of the number of BlockAddresses |
551 | /// referencing this BasicBlock by \p Amt. |
552 | /// |
553 | /// This is almost always 0, sometimes one possibly, but almost never 2, and |
554 | /// inconceivably 3 or more. |
555 | void AdjustBlockAddressRefCount(int Amt) { |
556 | BasicBlockBits Bits = getBasicBlockBits(); |
557 | Bits.BlockAddressRefCount += Amt; |
558 | setBasicBlockBits(Bits); |
559 | assert(Bits.BlockAddressRefCount < 255 && "Refcount wrap-around" ); |
560 | } |
561 | |
562 | /// Shadow Value::setValueSubclassData with a private forwarding method so |
563 | /// that any future subclasses cannot accidentally use it. |
564 | void setValueSubclassData(unsigned short D) { |
565 | Value::setValueSubclassData(D); |
566 | } |
567 | }; |
568 | |
569 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
570 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) |
571 | |
572 | /// Advance \p It while it points to a debug instruction and return the result. |
573 | /// This assumes that \p It is not at the end of a block. |
574 | BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It); |
575 | |
576 | #ifdef NDEBUG |
577 | /// In release builds, this is a no-op. For !NDEBUG builds, the checks are |
578 | /// implemented in the .cpp file to avoid circular header deps. |
579 | inline void BasicBlock::validateInstrOrdering() const {} |
580 | #endif |
581 | |
582 | } // end namespace llvm |
583 | |
584 | #endif // LLVM_IR_BASICBLOCK_H |
585 | |