Coverage Report

Created: 2018-07-18 22:01

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