blob: f3130b6e12880212a02cadc7aff6f108c1cd7091 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/CodeGen/MachineBasicBlock.h -------------------------*- 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// Collect the sequence of machine instructions for a basic block.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16
17#include "llvm/ADT/GraphTraits.h"
18#include "llvm/ADT/ilist.h"
19#include "llvm/ADT/ilist_node.h"
20#include "llvm/ADT/iterator_range.h"
21#include "llvm/ADT/simple_ilist.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineInstrBundleIterator.h"
24#include "llvm/IR/DebugLoc.h"
25#include "llvm/MC/LaneBitmask.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/Support/BranchProbability.h"
28#include "llvm/Support/Printable.h"
29#include <cassert>
30#include <cstdint>
31#include <functional>
32#include <iterator>
33#include <string>
34#include <vector>
35
36namespace llvm {
37
38class BasicBlock;
39class MachineFunction;
40class MCSymbol;
41class ModuleSlotTracker;
42class Pass;
43class SlotIndexes;
44class StringRef;
45class raw_ostream;
46class TargetRegisterClass;
47class TargetRegisterInfo;
48
49template <> struct ilist_traits<MachineInstr> {
50private:
51 friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
52
53 MachineBasicBlock *Parent;
54
55 using instr_iterator =
56 simple_ilist<MachineInstr, ilist_sentinel_tracking<true>>::iterator;
57
58public:
59 void addNodeToList(MachineInstr *N);
60 void removeNodeFromList(MachineInstr *N);
61 void transferNodesFromList(ilist_traits &OldList, instr_iterator First,
62 instr_iterator Last);
63 void deleteNode(MachineInstr *MI);
64};
65
66class MachineBasicBlock
67 : public ilist_node_with_parent<MachineBasicBlock, MachineFunction> {
68public:
69 /// Pair of physical register and lane mask.
70 /// This is not simply a std::pair typedef because the members should be named
71 /// clearly as they both have an integer type.
72 struct RegisterMaskPair {
73 public:
74 MCPhysReg PhysReg;
75 LaneBitmask LaneMask;
76
77 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
78 : PhysReg(PhysReg), LaneMask(LaneMask) {}
79 };
80
81private:
82 using Instructions = ilist<MachineInstr, ilist_sentinel_tracking<true>>;
83
84 Instructions Insts;
85 const BasicBlock *BB;
86 int Number;
87 MachineFunction *xParent;
88
89 /// Keep track of the predecessor / successor basic blocks.
90 std::vector<MachineBasicBlock *> Predecessors;
91 std::vector<MachineBasicBlock *> Successors;
92
93 /// Keep track of the probabilities to the successors. This vector has the
94 /// same order as Successors, or it is empty if we don't use it (disable
95 /// optimization).
96 std::vector<BranchProbability> Probs;
97 using probability_iterator = std::vector<BranchProbability>::iterator;
98 using const_probability_iterator =
99 std::vector<BranchProbability>::const_iterator;
100
101 Optional<uint64_t> IrrLoopHeaderWeight;
102
103 /// Keep track of the physical registers that are livein of the basicblock.
104 using LiveInVector = std::vector<RegisterMaskPair>;
105 LiveInVector LiveIns;
106
107 /// Alignment of the basic block. Zero if the basic block does not need to be
108 /// aligned. The alignment is specified as log2(bytes).
109 unsigned Alignment = 0;
110
111 /// Indicate that this basic block is entered via an exception handler.
112 bool IsEHPad = false;
113
114 /// Indicate that this basic block is potentially the target of an indirect
115 /// branch.
116 bool AddressTaken = false;
117
118 /// Indicate that this basic block is the entry block of an EH funclet.
119 bool IsEHFuncletEntry = false;
120
121 /// Indicate that this basic block is the entry block of a cleanup funclet.
122 bool IsCleanupFuncletEntry = false;
123
124 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
125 /// is only computed once and is cached.
126 mutable MCSymbol *CachedMCSymbol = nullptr;
127
128 // Intrusive list support
129 MachineBasicBlock() = default;
130
131 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
132
133 ~MachineBasicBlock();
134
135 // MachineBasicBlocks are allocated and owned by MachineFunction.
136 friend class MachineFunction;
137
138public:
139 /// Return the LLVM basic block that this instance corresponded to originally.
140 /// Note that this may be NULL if this instance does not correspond directly
141 /// to an LLVM basic block.
142 const BasicBlock *getBasicBlock() const { return BB; }
143
144 /// Return the name of the corresponding LLVM basic block, or an empty string.
145 StringRef getName() const;
146
147 /// Return a formatted string to identify this block and its parent function.
148 std::string getFullName() const;
149
150 /// Test whether this block is potentially the target of an indirect branch.
151 bool hasAddressTaken() const { return AddressTaken; }
152
153 /// Set this block to reflect that it potentially is the target of an indirect
154 /// branch.
155 void setHasAddressTaken() { AddressTaken = true; }
156
157 /// Return the MachineFunction containing this basic block.
158 const MachineFunction *getParent() const { return xParent; }
159 MachineFunction *getParent() { return xParent; }
160
161 using instr_iterator = Instructions::iterator;
162 using const_instr_iterator = Instructions::const_iterator;
163 using reverse_instr_iterator = Instructions::reverse_iterator;
164 using const_reverse_instr_iterator = Instructions::const_reverse_iterator;
165
166 using iterator = MachineInstrBundleIterator<MachineInstr>;
167 using const_iterator = MachineInstrBundleIterator<const MachineInstr>;
168 using reverse_iterator = MachineInstrBundleIterator<MachineInstr, true>;
169 using const_reverse_iterator =
170 MachineInstrBundleIterator<const MachineInstr, true>;
171
172 unsigned size() const { return (unsigned)Insts.size(); }
173 bool empty() const { return Insts.empty(); }
174
175 MachineInstr &instr_front() { return Insts.front(); }
176 MachineInstr &instr_back() { return Insts.back(); }
177 const MachineInstr &instr_front() const { return Insts.front(); }
178 const MachineInstr &instr_back() const { return Insts.back(); }
179
180 MachineInstr &front() { return Insts.front(); }
181 MachineInstr &back() { return *--end(); }
182 const MachineInstr &front() const { return Insts.front(); }
183 const MachineInstr &back() const { return *--end(); }
184
185 instr_iterator instr_begin() { return Insts.begin(); }
186 const_instr_iterator instr_begin() const { return Insts.begin(); }
187 instr_iterator instr_end() { return Insts.end(); }
188 const_instr_iterator instr_end() const { return Insts.end(); }
189 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
190 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
191 reverse_instr_iterator instr_rend () { return Insts.rend(); }
192 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
193
194 using instr_range = iterator_range<instr_iterator>;
195 using const_instr_range = iterator_range<const_instr_iterator>;
196 instr_range instrs() { return instr_range(instr_begin(), instr_end()); }
197 const_instr_range instrs() const {
198 return const_instr_range(instr_begin(), instr_end());
199 }
200
201 iterator begin() { return instr_begin(); }
202 const_iterator begin() const { return instr_begin(); }
203 iterator end () { return instr_end(); }
204 const_iterator end () const { return instr_end(); }
205 reverse_iterator rbegin() {
206 return reverse_iterator::getAtBundleBegin(instr_rbegin());
207 }
208 const_reverse_iterator rbegin() const {
209 return const_reverse_iterator::getAtBundleBegin(instr_rbegin());
210 }
211 reverse_iterator rend() { return reverse_iterator(instr_rend()); }
212 const_reverse_iterator rend() const {
213 return const_reverse_iterator(instr_rend());
214 }
215
216 /// Support for MachineInstr::getNextNode().
217 static Instructions MachineBasicBlock::*getSublistAccess(MachineInstr *) {
218 return &MachineBasicBlock::Insts;
219 }
220
221 inline iterator_range<iterator> terminators() {
222 return make_range(getFirstTerminator(), end());
223 }
224 inline iterator_range<const_iterator> terminators() const {
225 return make_range(getFirstTerminator(), end());
226 }
227
228 /// Returns a range that iterates over the phis in the basic block.
229 inline iterator_range<iterator> phis() {
230 return make_range(begin(), getFirstNonPHI());
231 }
232 inline iterator_range<const_iterator> phis() const {
233 return const_cast<MachineBasicBlock *>(this)->phis();
234 }
235
236 // Machine-CFG iterators
237 using pred_iterator = std::vector<MachineBasicBlock *>::iterator;
238 using const_pred_iterator = std::vector<MachineBasicBlock *>::const_iterator;
239 using succ_iterator = std::vector<MachineBasicBlock *>::iterator;
240 using const_succ_iterator = std::vector<MachineBasicBlock *>::const_iterator;
241 using pred_reverse_iterator =
242 std::vector<MachineBasicBlock *>::reverse_iterator;
243 using const_pred_reverse_iterator =
244 std::vector<MachineBasicBlock *>::const_reverse_iterator;
245 using succ_reverse_iterator =
246 std::vector<MachineBasicBlock *>::reverse_iterator;
247 using const_succ_reverse_iterator =
248 std::vector<MachineBasicBlock *>::const_reverse_iterator;
249 pred_iterator pred_begin() { return Predecessors.begin(); }
250 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
251 pred_iterator pred_end() { return Predecessors.end(); }
252 const_pred_iterator pred_end() const { return Predecessors.end(); }
253 pred_reverse_iterator pred_rbegin()
254 { return Predecessors.rbegin();}
255 const_pred_reverse_iterator pred_rbegin() const
256 { return Predecessors.rbegin();}
257 pred_reverse_iterator pred_rend()
258 { return Predecessors.rend(); }
259 const_pred_reverse_iterator pred_rend() const
260 { return Predecessors.rend(); }
261 unsigned pred_size() const {
262 return (unsigned)Predecessors.size();
263 }
264 bool pred_empty() const { return Predecessors.empty(); }
265 succ_iterator succ_begin() { return Successors.begin(); }
266 const_succ_iterator succ_begin() const { return Successors.begin(); }
267 succ_iterator succ_end() { return Successors.end(); }
268 const_succ_iterator succ_end() const { return Successors.end(); }
269 succ_reverse_iterator succ_rbegin()
270 { return Successors.rbegin(); }
271 const_succ_reverse_iterator succ_rbegin() const
272 { return Successors.rbegin(); }
273 succ_reverse_iterator succ_rend()
274 { return Successors.rend(); }
275 const_succ_reverse_iterator succ_rend() const
276 { return Successors.rend(); }
277 unsigned succ_size() const {
278 return (unsigned)Successors.size();
279 }
280 bool succ_empty() const { return Successors.empty(); }
281
282 inline iterator_range<pred_iterator> predecessors() {
283 return make_range(pred_begin(), pred_end());
284 }
285 inline iterator_range<const_pred_iterator> predecessors() const {
286 return make_range(pred_begin(), pred_end());
287 }
288 inline iterator_range<succ_iterator> successors() {
289 return make_range(succ_begin(), succ_end());
290 }
291 inline iterator_range<const_succ_iterator> successors() const {
292 return make_range(succ_begin(), succ_end());
293 }
294
295 // LiveIn management methods.
296
297 /// Adds the specified register as a live in. Note that it is an error to add
298 /// the same register to the same set more than once unless the intention is
299 /// to call sortUniqueLiveIns after all registers are added.
300 void addLiveIn(MCPhysReg PhysReg,
301 LaneBitmask LaneMask = LaneBitmask::getAll()) {
302 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
303 }
304 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
305 LiveIns.push_back(RegMaskPair);
306 }
307
308 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
309 /// this than repeatedly calling isLiveIn before calling addLiveIn for every
310 /// LiveIn insertion.
311 void sortUniqueLiveIns();
312
313 /// Clear live in list.
314 void clearLiveIns();
315
316 /// Add PhysReg as live in to this block, and ensure that there is a copy of
317 /// PhysReg to a virtual register of class RC. Return the virtual register
318 /// that is a copy of the live in PhysReg.
319 unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
320
321 /// Remove the specified register from the live in set.
322 void removeLiveIn(MCPhysReg Reg,
323 LaneBitmask LaneMask = LaneBitmask::getAll());
324
325 /// Return true if the specified register is in the live in set.
326 bool isLiveIn(MCPhysReg Reg,
327 LaneBitmask LaneMask = LaneBitmask::getAll()) const;
328
329 // Iteration support for live in sets. These sets are kept in sorted
330 // order by their register number.
331 using livein_iterator = LiveInVector::const_iterator;
332#ifndef NDEBUG
333 /// Unlike livein_begin, this method does not check that the liveness
334 /// information is accurate. Still for debug purposes it may be useful
335 /// to have iterators that won't assert if the liveness information
336 /// is not current.
337 livein_iterator livein_begin_dbg() const { return LiveIns.begin(); }
338 iterator_range<livein_iterator> liveins_dbg() const {
339 return make_range(livein_begin_dbg(), livein_end());
340 }
341#endif
342 livein_iterator livein_begin() const;
343 livein_iterator livein_end() const { return LiveIns.end(); }
344 bool livein_empty() const { return LiveIns.empty(); }
345 iterator_range<livein_iterator> liveins() const {
346 return make_range(livein_begin(), livein_end());
347 }
348
349 /// Remove entry from the livein set and return iterator to the next.
350 livein_iterator removeLiveIn(livein_iterator I);
351
352 /// Get the clobber mask for the start of this basic block. Funclets use this
353 /// to prevent register allocation across funclet transitions.
354 const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const;
355
356 /// Get the clobber mask for the end of the basic block.
357 /// \see getBeginClobberMask()
358 const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const;
359
360 /// Return alignment of the basic block. The alignment is specified as
361 /// log2(bytes).
362 unsigned getAlignment() const { return Alignment; }
363
364 /// Set alignment of the basic block. The alignment is specified as
365 /// log2(bytes).
366 void setAlignment(unsigned Align) { Alignment = Align; }
367
368 /// Returns true if the block is a landing pad. That is this basic block is
369 /// entered via an exception handler.
370 bool isEHPad() const { return IsEHPad; }
371
372 /// Indicates the block is a landing pad. That is this basic block is entered
373 /// via an exception handler.
374 void setIsEHPad(bool V = true) { IsEHPad = V; }
375
376 bool hasEHPadSuccessor() const;
377
378 /// Returns true if this is the entry block of an EH funclet.
379 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
380
381 /// Indicates if this is the entry block of an EH funclet.
382 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
383
384 /// Returns true if this is the entry block of a cleanup funclet.
385 bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
386
387 /// Indicates if this is the entry block of a cleanup funclet.
388 void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
389
390 /// Returns true if it is legal to hoist instructions into this block.
391 bool isLegalToHoistInto() const;
392
393 // Code Layout methods.
394
395 /// Move 'this' block before or after the specified block. This only moves
396 /// the block, it does not modify the CFG or adjust potential fall-throughs at
397 /// the end of the block.
398 void moveBefore(MachineBasicBlock *NewAfter);
399 void moveAfter(MachineBasicBlock *NewBefore);
400
401 /// Update the terminator instructions in block to account for changes to the
402 /// layout. If the block previously used a fallthrough, it may now need a
403 /// branch, and if it previously used branching it may now be able to use a
404 /// fallthrough.
405 void updateTerminator();
406
407 // Machine-CFG mutators
408
409 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
410 /// of Succ is automatically updated. PROB parameter is stored in
411 /// Probabilities list. The default probability is set as unknown. Mixing
412 /// known and unknown probabilities in successor list is not allowed. When all
413 /// successors have unknown probabilities, 1 / N is returned as the
414 /// probability for each successor, where N is the number of successors.
415 ///
416 /// Note that duplicate Machine CFG edges are not allowed.
417 void addSuccessor(MachineBasicBlock *Succ,
418 BranchProbability Prob = BranchProbability::getUnknown());
419
420 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
421 /// of Succ is automatically updated. The probability is not provided because
422 /// BPI is not available (e.g. -O0 is used), in which case edge probabilities
423 /// won't be used. Using this interface can save some space.
424 void addSuccessorWithoutProb(MachineBasicBlock *Succ);
425
426 /// Set successor probability of a given iterator.
427 void setSuccProbability(succ_iterator I, BranchProbability Prob);
428
429 /// Normalize probabilities of all successors so that the sum of them becomes
430 /// one. This is usually done when the current update on this MBB is done, and
431 /// the sum of its successors' probabilities is not guaranteed to be one. The
432 /// user is responsible for the correct use of this function.
433 /// MBB::removeSuccessor() has an option to do this automatically.
434 void normalizeSuccProbs() {
435 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
436 }
437
438 /// Validate successors' probabilities and check if the sum of them is
439 /// approximate one. This only works in DEBUG mode.
440 void validateSuccProbs() const;
441
442 /// Remove successor from the successors list of this MachineBasicBlock. The
443 /// Predecessors list of Succ is automatically updated.
444 /// If NormalizeSuccProbs is true, then normalize successors' probabilities
445 /// after the successor is removed.
446 void removeSuccessor(MachineBasicBlock *Succ,
447 bool NormalizeSuccProbs = false);
448
449 /// Remove specified successor from the successors list of this
450 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
451 /// If NormalizeSuccProbs is true, then normalize successors' probabilities
452 /// after the successor is removed.
453 /// Return the iterator to the element after the one removed.
454 succ_iterator removeSuccessor(succ_iterator I,
455 bool NormalizeSuccProbs = false);
456
457 /// Replace successor OLD with NEW and update probability info.
458 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
459
460 /// Copy a successor (and any probability info) from original block to this
461 /// block's. Uses an iterator into the original blocks successors.
462 ///
463 /// This is useful when doing a partial clone of successors. Afterward, the
464 /// probabilities may need to be normalized.
465 void copySuccessor(MachineBasicBlock *Orig, succ_iterator I);
466
467 /// Transfers all the successors from MBB to this machine basic block (i.e.,
468 /// copies all the successors FromMBB and remove all the successors from
469 /// FromMBB).
470 void transferSuccessors(MachineBasicBlock *FromMBB);
471
472 /// Transfers all the successors, as in transferSuccessors, and update PHI
473 /// operands in the successor blocks which refer to FromMBB to refer to this.
474 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
475
476 /// Return true if any of the successors have probabilities attached to them.
477 bool hasSuccessorProbabilities() const { return !Probs.empty(); }
478
479 /// Return true if the specified MBB is a predecessor of this block.
480 bool isPredecessor(const MachineBasicBlock *MBB) const;
481
482 /// Return true if the specified MBB is a successor of this block.
483 bool isSuccessor(const MachineBasicBlock *MBB) const;
484
485 /// Return true if the specified MBB will be emitted immediately after this
486 /// block, such that if this block exits by falling through, control will
487 /// transfer to the specified MBB. Note that MBB need not be a successor at
488 /// all, for example if this block ends with an unconditional branch to some
489 /// other block.
490 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
491
492 /// Return the fallthrough block if the block can implicitly
493 /// transfer control to the block after it by falling off the end of
494 /// it. This should return null if it can reach the block after
495 /// it, but it uses an explicit branch to do so (e.g., a table
496 /// jump). Non-null return is a conservative answer.
497 MachineBasicBlock *getFallThrough();
498
499 /// Return true if the block can implicitly transfer control to the
500 /// block after it by falling off the end of it. This should return
501 /// false if it can reach the block after it, but it uses an
502 /// explicit branch to do so (e.g., a table jump). True is a
503 /// conservative answer.
504 bool canFallThrough();
505
506 /// Returns a pointer to the first instruction in this block that is not a
507 /// PHINode instruction. When adding instructions to the beginning of the
508 /// basic block, they should be added before the returned value, not before
509 /// the first instruction, which might be PHI.
510 /// Returns end() is there's no non-PHI instruction.
511 iterator getFirstNonPHI();
512
513 /// Return the first instruction in MBB after I that is not a PHI or a label.
514 /// This is the correct point to insert lowered copies at the beginning of a
515 /// basic block that must be before any debugging information.
516 iterator SkipPHIsAndLabels(iterator I);
517
518 /// Return the first instruction in MBB after I that is not a PHI, label or
519 /// debug. This is the correct point to insert copies at the beginning of a
520 /// basic block.
521 iterator SkipPHIsLabelsAndDebug(iterator I);
522
523 /// Returns an iterator to the first terminator instruction of this basic
524 /// block. If a terminator does not exist, it returns end().
525 iterator getFirstTerminator();
526 const_iterator getFirstTerminator() const {
527 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
528 }
529
530 /// Same getFirstTerminator but it ignores bundles and return an
531 /// instr_iterator instead.
532 instr_iterator getFirstInstrTerminator();
533
534 /// Returns an iterator to the first non-debug instruction in the basic block,
535 /// or end().
536 iterator getFirstNonDebugInstr();
537 const_iterator getFirstNonDebugInstr() const {
538 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
539 }
540
541 /// Returns an iterator to the last non-debug instruction in the basic block,
542 /// or end().
543 iterator getLastNonDebugInstr();
544 const_iterator getLastNonDebugInstr() const {
545 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
546 }
547
548 /// Convenience function that returns true if the block ends in a return
549 /// instruction.
550 bool isReturnBlock() const {
551 return !empty() && back().isReturn();
552 }
553
554 /// Split the critical edge from this block to the given successor block, and
555 /// return the newly created block, or null if splitting is not possible.
556 ///
557 /// This function updates LiveVariables, MachineDominatorTree, and
558 /// MachineLoopInfo, as applicable.
559 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P);
560
561 /// Check if the edge between this block and the given successor \p
562 /// Succ, can be split. If this returns true a subsequent call to
563 /// SplitCriticalEdge is guaranteed to return a valid basic block if
564 /// no changes occurred in the meantime.
565 bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const;
566
567 void pop_front() { Insts.pop_front(); }
568 void pop_back() { Insts.pop_back(); }
569 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
570
571 /// Insert MI into the instruction list before I, possibly inside a bundle.
572 ///
573 /// If the insertion point is inside a bundle, MI will be added to the bundle,
574 /// otherwise MI will not be added to any bundle. That means this function
575 /// alone can't be used to prepend or append instructions to bundles. See
576 /// MIBundleBuilder::insert() for a more reliable way of doing that.
577 instr_iterator insert(instr_iterator I, MachineInstr *M);
578
579 /// Insert a range of instructions into the instruction list before I.
580 template<typename IT>
581 void insert(iterator I, IT S, IT E) {
582 assert((I == end() || I->getParent() == this) &&
583 "iterator points outside of basic block");
584 Insts.insert(I.getInstrIterator(), S, E);
585 }
586
587 /// Insert MI into the instruction list before I.
588 iterator insert(iterator I, MachineInstr *MI) {
589 assert((I == end() || I->getParent() == this) &&
590 "iterator points outside of basic block");
591 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
592 "Cannot insert instruction with bundle flags");
593 return Insts.insert(I.getInstrIterator(), MI);
594 }
595
596 /// Insert MI into the instruction list after I.
597 iterator insertAfter(iterator I, MachineInstr *MI) {
598 assert((I == end() || I->getParent() == this) &&
599 "iterator points outside of basic block");
600 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
601 "Cannot insert instruction with bundle flags");
602 return Insts.insertAfter(I.getInstrIterator(), MI);
603 }
604
605 /// Remove an instruction from the instruction list and delete it.
606 ///
607 /// If the instruction is part of a bundle, the other instructions in the
608 /// bundle will still be bundled after removing the single instruction.
609 instr_iterator erase(instr_iterator I);
610
611 /// Remove an instruction from the instruction list and delete it.
612 ///
613 /// If the instruction is part of a bundle, the other instructions in the
614 /// bundle will still be bundled after removing the single instruction.
615 instr_iterator erase_instr(MachineInstr *I) {
616 return erase(instr_iterator(I));
617 }
618
619 /// Remove a range of instructions from the instruction list and delete them.
620 iterator erase(iterator I, iterator E) {
621 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
622 }
623
624 /// Remove an instruction or bundle from the instruction list and delete it.
625 ///
626 /// If I points to a bundle of instructions, they are all erased.
627 iterator erase(iterator I) {
628 return erase(I, std::next(I));
629 }
630
631 /// Remove an instruction from the instruction list and delete it.
632 ///
633 /// If I is the head of a bundle of instructions, the whole bundle will be
634 /// erased.
635 iterator erase(MachineInstr *I) {
636 return erase(iterator(I));
637 }
638
639 /// Remove the unbundled instruction from the instruction list without
640 /// deleting it.
641 ///
642 /// This function can not be used to remove bundled instructions, use
643 /// remove_instr to remove individual instructions from a bundle.
644 MachineInstr *remove(MachineInstr *I) {
645 assert(!I->isBundled() && "Cannot remove bundled instructions");
646 return Insts.remove(instr_iterator(I));
647 }
648
649 /// Remove the possibly bundled instruction from the instruction list
650 /// without deleting it.
651 ///
652 /// If the instruction is part of a bundle, the other instructions in the
653 /// bundle will still be bundled after removing the single instruction.
654 MachineInstr *remove_instr(MachineInstr *I);
655
656 void clear() {
657 Insts.clear();
658 }
659
660 /// Take an instruction from MBB 'Other' at the position From, and insert it
661 /// into this MBB right before 'Where'.
662 ///
663 /// If From points to a bundle of instructions, the whole bundle is moved.
664 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
665 // The range splice() doesn't allow noop moves, but this one does.
666 if (Where != From)
667 splice(Where, Other, From, std::next(From));
668 }
669
670 /// Take a block of instructions from MBB 'Other' in the range [From, To),
671 /// and insert them into this MBB right before 'Where'.
672 ///
673 /// The instruction at 'Where' must not be included in the range of
674 /// instructions to move.
675 void splice(iterator Where, MachineBasicBlock *Other,
676 iterator From, iterator To) {
677 Insts.splice(Where.getInstrIterator(), Other->Insts,
678 From.getInstrIterator(), To.getInstrIterator());
679 }
680
681 /// This method unlinks 'this' from the containing function, and returns it,
682 /// but does not delete it.
683 MachineBasicBlock *removeFromParent();
684
685 /// This method unlinks 'this' from the containing function and deletes it.
686 void eraseFromParent();
687
688 /// Given a machine basic block that branched to 'Old', change the code and
689 /// CFG so that it branches to 'New' instead.
690 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
691
692 /// Various pieces of code can cause excess edges in the CFG to be inserted.
693 /// If we have proven that MBB can only branch to DestA and DestB, remove any
694 /// other MBB successors from the CFG. DestA and DestB can be null. Besides
695 /// DestA and DestB, retain other edges leading to LandingPads (currently
696 /// there can be only one; we don't check or require that here). Note it is
697 /// possible that DestA and/or DestB are LandingPads.
698 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
699 MachineBasicBlock *DestB,
700 bool IsCond);
701
702 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
703 /// instructions. Return UnknownLoc if there is none.
704 DebugLoc findDebugLoc(instr_iterator MBBI);
705 DebugLoc findDebugLoc(iterator MBBI) {
706 return findDebugLoc(MBBI.getInstrIterator());
707 }
708
709 /// Find the previous valid DebugLoc preceding MBBI, skipping and DBG_VALUE
710 /// instructions. Return UnknownLoc if there is none.
711 DebugLoc findPrevDebugLoc(instr_iterator MBBI);
712 DebugLoc findPrevDebugLoc(iterator MBBI) {
713 return findPrevDebugLoc(MBBI.getInstrIterator());
714 }
715
716 /// Find and return the merged DebugLoc of the branch instructions of the
717 /// block. Return UnknownLoc if there is none.
718 DebugLoc findBranchDebugLoc();
719
720 /// Possible outcome of a register liveness query to computeRegisterLiveness()
721 enum LivenessQueryResult {
722 LQR_Live, ///< Register is known to be (at least partially) live.
723 LQR_Dead, ///< Register is known to be fully dead.
724 LQR_Unknown ///< Register liveness not decidable from local neighborhood.
725 };
726
727 /// Return whether (physical) register \p Reg has been defined and not
728 /// killed as of just before \p Before.
729 ///
730 /// Search is localised to a neighborhood of \p Neighborhood instructions
731 /// before (searching for defs or kills) and \p Neighborhood instructions
732 /// after (searching just for defs) \p Before.
733 ///
734 /// \p Reg must be a physical register.
735 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
736 unsigned Reg,
737 const_iterator Before,
738 unsigned Neighborhood = 10) const;
739
740 // Debugging methods.
741 void dump() const;
742 void print(raw_ostream &OS, const SlotIndexes * = nullptr,
743 bool IsStandalone = true) const;
744 void print(raw_ostream &OS, ModuleSlotTracker &MST,
745 const SlotIndexes * = nullptr, bool IsStandalone = true) const;
746
747 // Printing method used by LoopInfo.
748 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
749
750 /// MachineBasicBlocks are uniquely numbered at the function level, unless
751 /// they're not in a MachineFunction yet, in which case this will return -1.
752 int getNumber() const { return Number; }
753 void setNumber(int N) { Number = N; }
754
755 /// Return the MCSymbol for this basic block.
756 MCSymbol *getSymbol() const;
757
758 Optional<uint64_t> getIrrLoopHeaderWeight() const {
759 return IrrLoopHeaderWeight;
760 }
761
762 void setIrrLoopHeaderWeight(uint64_t Weight) {
763 IrrLoopHeaderWeight = Weight;
764 }
765
766private:
767 /// Return probability iterator corresponding to the I successor iterator.
768 probability_iterator getProbabilityIterator(succ_iterator I);
769 const_probability_iterator
770 getProbabilityIterator(const_succ_iterator I) const;
771
772 friend class MachineBranchProbabilityInfo;
773 friend class MIPrinter;
774
775 /// Return probability of the edge from this block to MBB. This method should
776 /// NOT be called directly, but by using getEdgeProbability method from
777 /// MachineBranchProbabilityInfo class.
778 BranchProbability getSuccProbability(const_succ_iterator Succ) const;
779
780 // Methods used to maintain doubly linked list of blocks...
781 friend struct ilist_callback_traits<MachineBasicBlock>;
782
783 // Machine-CFG mutators
784
785 /// Add Pred as a predecessor of this MachineBasicBlock. Don't do this
786 /// unless you know what you're doing, because it doesn't update Pred's
787 /// successors list. Use Pred->addSuccessor instead.
788 void addPredecessor(MachineBasicBlock *Pred);
789
790 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
791 /// unless you know what you're doing, because it doesn't update Pred's
792 /// successors list. Use Pred->removeSuccessor instead.
793 void removePredecessor(MachineBasicBlock *Pred);
794};
795
796raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
797
798/// Prints a machine basic block reference.
799///
800/// The format is:
801/// %bb.5 - a machine basic block with MBB.getNumber() == 5.
802///
803/// Usage: OS << printMBBReference(MBB) << '\n';
804Printable printMBBReference(const MachineBasicBlock &MBB);
805
806// This is useful when building IndexedMaps keyed on basic block pointers.
807struct MBB2NumberFunctor {
808 using argument_type = const MachineBasicBlock *;
809 unsigned operator()(const MachineBasicBlock *MBB) const {
810 return MBB->getNumber();
811 }
812};
813
814//===--------------------------------------------------------------------===//
815// GraphTraits specializations for machine basic block graphs (machine-CFGs)
816//===--------------------------------------------------------------------===//
817
818// Provide specializations of GraphTraits to be able to treat a
819// MachineFunction as a graph of MachineBasicBlocks.
820//
821
822template <> struct GraphTraits<MachineBasicBlock *> {
823 using NodeRef = MachineBasicBlock *;
824 using ChildIteratorType = MachineBasicBlock::succ_iterator;
825
826 static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; }
827 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
828 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
829};
830
831template <> struct GraphTraits<const MachineBasicBlock *> {
832 using NodeRef = const MachineBasicBlock *;
833 using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
834
835 static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; }
836 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
837 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
838};
839
840// Provide specializations of GraphTraits to be able to treat a
841// MachineFunction as a graph of MachineBasicBlocks and to walk it
842// in inverse order. Inverse order for a function is considered
843// to be when traversing the predecessor edges of a MBB
844// instead of the successor edges.
845//
846template <> struct GraphTraits<Inverse<MachineBasicBlock*>> {
847 using NodeRef = MachineBasicBlock *;
848 using ChildIteratorType = MachineBasicBlock::pred_iterator;
849
850 static NodeRef getEntryNode(Inverse<MachineBasicBlock *> G) {
851 return G.Graph;
852 }
853
854 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
855 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
856};
857
858template <> struct GraphTraits<Inverse<const MachineBasicBlock*>> {
859 using NodeRef = const MachineBasicBlock *;
860 using ChildIteratorType = MachineBasicBlock::const_pred_iterator;
861
862 static NodeRef getEntryNode(Inverse<const MachineBasicBlock *> G) {
863 return G.Graph;
864 }
865
866 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
867 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
868};
869
870/// MachineInstrSpan provides an interface to get an iteration range
871/// containing the instruction it was initialized with, along with all
872/// those instructions inserted prior to or following that instruction
873/// at some point after the MachineInstrSpan is constructed.
874class MachineInstrSpan {
875 MachineBasicBlock &MBB;
876 MachineBasicBlock::iterator I, B, E;
877
878public:
879 MachineInstrSpan(MachineBasicBlock::iterator I)
880 : MBB(*I->getParent()),
881 I(I),
882 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
883 E(std::next(I)) {}
884
885 MachineBasicBlock::iterator begin() {
886 return B == MBB.end() ? MBB.begin() : std::next(B);
887 }
888 MachineBasicBlock::iterator end() { return E; }
889 bool empty() { return begin() == end(); }
890
891 MachineBasicBlock::iterator getInitial() { return I; }
892};
893
894/// Increment \p It until it points to a non-debug instruction or to \p End
895/// and return the resulting iterator. This function should only be used
896/// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
897/// const_instr_iterator} and the respective reverse iterators.
898template<typename IterT>
899inline IterT skipDebugInstructionsForward(IterT It, IterT End) {
900 while (It != End && It->isDebugValue())
901 It++;
902 return It;
903}
904
905/// Decrement \p It until it points to a non-debug instruction or to \p Begin
906/// and return the resulting iterator. This function should only be used
907/// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
908/// const_instr_iterator} and the respective reverse iterators.
909template<class IterT>
910inline IterT skipDebugInstructionsBackward(IterT It, IterT Begin) {
911 while (It != Begin && It->isDebugValue())
912 It--;
913 return It;
914}
915
916} // end namespace llvm
917
918#endif // LLVM_CODEGEN_MACHINEBASICBLOCK_H