]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/BasicBlock.h
Merge ^/head r317971 through r318379.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / BasicBlock.h
1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- 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 BasicBlock class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IR_BASICBLOCK_H
15 #define LLVM_IR_BASICBLOCK_H
16
17 #include "llvm/ADT/ilist.h"
18 #include "llvm/ADT/ilist_node.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/SymbolTableListTraits.h"
22 #include "llvm/IR/Value.h"
23 #include "llvm/Support/CBindingWrapping.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm-c/Types.h"
26 #include <cassert>
27 #include <cstddef>
28
29 namespace llvm {
30
31 class CallInst;
32 class Function;
33 class LandingPadInst;
34 class LLVMContext;
35 class Module;
36 class TerminatorInst;
37 class ValueSymbolTable;
38
39 /// \brief LLVM Basic Block Representation
40 ///
41 /// This represents a single basic block in LLVM. A basic block is simply a
42 /// container of instructions that execute sequentially. Basic blocks are Values
43 /// because they are referenced by instructions such as branches and switch
44 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
45 /// represents a label to which a branch can jump.
46 ///
47 /// A well formed basic block is formed of a list of non-terminating
48 /// instructions followed by a single TerminatorInst instruction.
49 /// TerminatorInst's may not occur in the middle of basic blocks, and must
50 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
51 /// occur because it may be useful in the intermediate stage of constructing or
52 /// modifying a program. However, the verifier will ensure that basic blocks
53 /// are "well formed".
54 class BasicBlock : public Value, // Basic blocks are data objects also
55                    public ilist_node_with_parent<BasicBlock, Function> {
56 public:
57   using InstListType = SymbolTableList<Instruction>;
58
59 private:
60   friend class BlockAddress;
61   friend class SymbolTableListTraits<BasicBlock>;
62
63   InstListType InstList;
64   Function *Parent;
65
66   void setParent(Function *parent);
67
68   /// \brief Constructor.
69   ///
70   /// If the function parameter is specified, the basic block is automatically
71   /// inserted at either the end of the function (if InsertBefore is null), or
72   /// before the specified basic block.
73   explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
74                       Function *Parent = nullptr,
75                       BasicBlock *InsertBefore = nullptr);
76
77 public:
78   BasicBlock(const BasicBlock &) = delete;
79   BasicBlock &operator=(const BasicBlock &) = delete;
80   ~BasicBlock() override;
81
82   /// \brief Get the context in which this basic block lives.
83   LLVMContext &getContext() const;
84
85   /// Instruction iterators...
86   using iterator = InstListType::iterator;
87   using const_iterator = InstListType::const_iterator;
88   using reverse_iterator = InstListType::reverse_iterator;
89   using const_reverse_iterator = InstListType::const_reverse_iterator;
90
91   /// \brief Creates a new BasicBlock.
92   ///
93   /// If the Parent parameter is specified, the basic block is automatically
94   /// inserted at either the end of the function (if InsertBefore is 0), or
95   /// before the specified basic block.
96   static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
97                             Function *Parent = nullptr,
98                             BasicBlock *InsertBefore = nullptr) {
99     return new BasicBlock(Context, Name, Parent, InsertBefore);
100   }
101
102   /// \brief Return the enclosing method, or null if none.
103   const Function *getParent() const { return Parent; }
104         Function *getParent()       { return Parent; }
105
106   /// \brief Return the module owning the function this basic block belongs to,
107   /// or nullptr it the function does not have a module.
108   ///
109   /// Note: this is undefined behavior if the block does not have a parent.
110   const Module *getModule() const;
111   Module *getModule() {
112     return const_cast<Module *>(
113                             static_cast<const BasicBlock *>(this)->getModule());
114   }
115
116   /// \brief Returns the terminator instruction if the block is well formed or
117   /// null if the block is not well formed.
118   const TerminatorInst *getTerminator() const LLVM_READONLY;
119   TerminatorInst *getTerminator() {
120     return const_cast<TerminatorInst *>(
121                         static_cast<const BasicBlock *>(this)->getTerminator());
122   }
123
124   /// \brief Returns the call instruction calling @llvm.experimental.deoptimize
125   /// prior to the terminating return instruction of this basic block, if such a
126   /// call is present.  Otherwise, returns null.
127   const CallInst *getTerminatingDeoptimizeCall() const;
128   CallInst *getTerminatingDeoptimizeCall() {
129     return const_cast<CallInst *>(
130          static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall());
131   }
132
133   /// \brief Returns the call instruction marked 'musttail' prior to the
134   /// terminating return instruction of this basic block, if such a call is
135   /// present.  Otherwise, returns null.
136   const CallInst *getTerminatingMustTailCall() const;
137   CallInst *getTerminatingMustTailCall() {
138     return const_cast<CallInst *>(
139            static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall());
140   }
141
142   /// \brief Returns a pointer to the first instruction in this block that is
143   /// not a PHINode instruction.
144   ///
145   /// When adding instructions to the beginning of the basic block, they should
146   /// be added before the returned value, not before the first instruction,
147   /// which might be PHI. Returns 0 is there's no non-PHI instruction.
148   const Instruction* getFirstNonPHI() const;
149   Instruction* getFirstNonPHI() {
150     return const_cast<Instruction *>(
151                        static_cast<const BasicBlock *>(this)->getFirstNonPHI());
152   }
153
154   /// \brief Returns a pointer to the first instruction in this block that is not
155   /// a PHINode or a debug intrinsic.
156   const Instruction* getFirstNonPHIOrDbg() const;
157   Instruction* getFirstNonPHIOrDbg() {
158     return const_cast<Instruction *>(
159                   static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg());
160   }
161
162   /// \brief Returns a pointer to the first instruction in this block that is not
163   /// a PHINode, a debug intrinsic, or a lifetime intrinsic.
164   const Instruction* getFirstNonPHIOrDbgOrLifetime() const;
165   Instruction* getFirstNonPHIOrDbgOrLifetime() {
166     return const_cast<Instruction *>(
167         static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime());
168   }
169
170   /// \brief Returns an iterator to the first instruction in this block that is
171   /// suitable for inserting a non-PHI instruction.
172   ///
173   /// In particular, it skips all PHIs and LandingPad instructions.
174   const_iterator getFirstInsertionPt() const;
175   iterator getFirstInsertionPt() {
176     return static_cast<const BasicBlock *>(this)
177                                           ->getFirstInsertionPt().getNonConst();
178   }
179
180   /// \brief Unlink 'this' from the containing function, but do not delete it.
181   void removeFromParent();
182
183   /// \brief Unlink 'this' from the containing function and delete it.
184   ///
185   // \returns an iterator pointing to the element after the erased one.
186   SymbolTableList<BasicBlock>::iterator eraseFromParent();
187
188   /// \brief Unlink this basic block from its current function and insert it
189   /// into the function that \p MovePos lives in, right before \p MovePos.
190   void moveBefore(BasicBlock *MovePos);
191
192   /// \brief Unlink this basic block from its current function and insert it
193   /// right after \p MovePos in the function \p MovePos lives in.
194   void moveAfter(BasicBlock *MovePos);
195
196   /// \brief Insert unlinked basic block into a function.
197   ///
198   /// Inserts an unlinked basic block into \c Parent.  If \c InsertBefore is
199   /// provided, inserts before that basic block, otherwise inserts at the end.
200   ///
201   /// \pre \a getParent() is \c nullptr.
202   void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
203
204   /// \brief Return the predecessor of this block if it has a single predecessor
205   /// block. Otherwise return a null pointer.
206   const BasicBlock *getSinglePredecessor() const;
207   BasicBlock *getSinglePredecessor() {
208     return const_cast<BasicBlock *>(
209                  static_cast<const BasicBlock *>(this)->getSinglePredecessor());
210   }
211
212   /// \brief Return the predecessor of this block if it has a unique predecessor
213   /// block. Otherwise return a null pointer.
214   ///
215   /// Note that unique predecessor doesn't mean single edge, there can be
216   /// multiple edges from the unique predecessor to this block (for example a
217   /// switch statement with multiple cases having the same destination).
218   const BasicBlock *getUniquePredecessor() const;
219   BasicBlock *getUniquePredecessor() {
220     return const_cast<BasicBlock *>(
221                  static_cast<const BasicBlock *>(this)->getUniquePredecessor());
222   }
223
224   /// \brief Return the successor of this block if it has a single successor.
225   /// Otherwise return a null pointer.
226   ///
227   /// This method is analogous to getSinglePredecessor above.
228   const BasicBlock *getSingleSuccessor() const;
229   BasicBlock *getSingleSuccessor() {
230     return const_cast<BasicBlock *>(
231                    static_cast<const BasicBlock *>(this)->getSingleSuccessor());
232   }
233
234   /// \brief Return the successor of this block if it has a unique successor.
235   /// Otherwise return a null pointer.
236   ///
237   /// This method is analogous to getUniquePredecessor above.
238   const BasicBlock *getUniqueSuccessor() const;
239   BasicBlock *getUniqueSuccessor() {
240     return const_cast<BasicBlock *>(
241                    static_cast<const BasicBlock *>(this)->getUniqueSuccessor());
242   }
243
244   //===--------------------------------------------------------------------===//
245   /// Instruction iterator methods
246   ///
247   inline iterator                begin()       { return InstList.begin(); }
248   inline const_iterator          begin() const { return InstList.begin(); }
249   inline iterator                end  ()       { return InstList.end();   }
250   inline const_iterator          end  () const { return InstList.end();   }
251
252   inline reverse_iterator        rbegin()       { return InstList.rbegin(); }
253   inline const_reverse_iterator  rbegin() const { return InstList.rbegin(); }
254   inline reverse_iterator        rend  ()       { return InstList.rend();   }
255   inline const_reverse_iterator  rend  () const { return InstList.rend();   }
256
257   inline size_t                   size() const { return InstList.size();  }
258   inline bool                    empty() const { return InstList.empty(); }
259   inline const Instruction      &front() const { return InstList.front(); }
260   inline       Instruction      &front()       { return InstList.front(); }
261   inline const Instruction       &back() const { return InstList.back();  }
262   inline       Instruction       &back()       { return InstList.back();  }
263
264   /// \brief Return the underlying instruction list container.
265   ///
266   /// Currently you need to access the underlying instruction list container
267   /// directly if you want to modify it.
268   const InstListType &getInstList() const { return InstList; }
269         InstListType &getInstList()       { return InstList; }
270
271   /// \brief Returns a pointer to a member of the instruction list.
272   static InstListType BasicBlock::*getSublistAccess(Instruction*) {
273     return &BasicBlock::InstList;
274   }
275
276   /// \brief Returns a pointer to the symbol table if one exists.
277   ValueSymbolTable *getValueSymbolTable();
278
279   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
280   static inline bool classof(const Value *V) {
281     return V->getValueID() == Value::BasicBlockVal;
282   }
283
284   /// \brief Cause all subinstructions to "let go" of all the references that
285   /// said subinstructions are maintaining.
286   ///
287   /// This allows one to 'delete' a whole class at a time, even though there may
288   /// be circular references... first all references are dropped, and all use
289   /// counts go to zero.  Then everything is delete'd for real.  Note that no
290   /// operations are valid on an object that has "dropped all references",
291   /// except operator delete.
292   void dropAllReferences();
293
294   /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer
295   /// able to reach it.
296   ///
297   /// This is actually not used to update the Predecessor list, but is actually
298   /// used to update the PHI nodes that reside in the block.  Note that this
299   /// should be called while the predecessor still refers to this block.
300   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
301
302   bool canSplitPredecessors() const;
303
304   /// \brief Split the basic block into two basic blocks at the specified
305   /// instruction.
306   ///
307   /// Note that all instructions BEFORE the specified iterator stay as part of
308   /// the original basic block, an unconditional branch is added to the original
309   /// BB, and the rest of the instructions in the BB are moved to the new BB,
310   /// including the old terminator.  The newly formed BasicBlock is returned.
311   /// This function invalidates the specified iterator.
312   ///
313   /// Note that this only works on well formed basic blocks (must have a
314   /// terminator), and 'I' must not be the end of instruction list (which would
315   /// cause a degenerate basic block to be formed, having a terminator inside of
316   /// the basic block).
317   ///
318   /// Also note that this doesn't preserve any passes. To split blocks while
319   /// keeping loop information consistent, use the SplitBlock utility function.
320   BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
321   BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "") {
322     return splitBasicBlock(I->getIterator(), BBName);
323   }
324
325   /// \brief Returns true if there are any uses of this basic block other than
326   /// direct branches, switches, etc. to it.
327   bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
328
329   /// \brief Update all phi nodes in this basic block's successors to refer to
330   /// basic block \p New instead of to it.
331   void replaceSuccessorsPhiUsesWith(BasicBlock *New);
332
333   /// \brief Return true if this basic block is an exception handling block.
334   bool isEHPad() const { return getFirstNonPHI()->isEHPad(); }
335
336   /// \brief Return true if this basic block is a landing pad.
337   ///
338   /// Being a ``landing pad'' means that the basic block is the destination of
339   /// the 'unwind' edge of an invoke instruction.
340   bool isLandingPad() const;
341
342   /// \brief Return the landingpad instruction associated with the landing pad.
343   const LandingPadInst *getLandingPadInst() const;
344   LandingPadInst *getLandingPadInst() {
345     return const_cast<LandingPadInst *>(
346                     static_cast<const BasicBlock *>(this)->getLandingPadInst());
347   }
348
349 private:
350   /// \brief Increment the internal refcount of the number of BlockAddresses
351   /// referencing this BasicBlock by \p Amt.
352   ///
353   /// This is almost always 0, sometimes one possibly, but almost never 2, and
354   /// inconceivably 3 or more.
355   void AdjustBlockAddressRefCount(int Amt) {
356     setValueSubclassData(getSubclassDataFromValue()+Amt);
357     assert((int)(signed char)getSubclassDataFromValue() >= 0 &&
358            "Refcount wrap-around");
359   }
360
361   /// \brief Shadow Value::setValueSubclassData with a private forwarding method
362   /// so that any future subclasses cannot accidentally use it.
363   void setValueSubclassData(unsigned short D) {
364     Value::setValueSubclassData(D);
365   }
366 };
367
368 // Create wrappers for C Binding types (see CBindingWrapping.h).
369 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef)
370
371 } // end namespace llvm
372
373 #endif // LLVM_IR_BASICBLOCK_H