Coverage Report

Created: 2017-10-03 07:32

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