1//===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- 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#include "llvm/CodeGen/MachineBasicBlock.h"
15#include "llvm/BasicBlock.h"
16#include "llvm/CodeGen/LiveVariables.h"
17#include "llvm/CodeGen/MachineDominators.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineLoopInfo.h"
20#include "llvm/CodeGen/SlotIndexes.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/Target/TargetRegisterInfo.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Assembly/Writer.h"
28#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/SmallPtrSet.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/LeakDetector.h"
32#include "llvm/Support/raw_ostream.h"
33#include <algorithm>
34using namespace llvm;
35
36MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
37  : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
38    AddressTaken(false) {
39  Insts.Parent = this;
40}
41
42MachineBasicBlock::~MachineBasicBlock() {
43  LeakDetector::removeGarbageObject(this);
44}
45
46/// getSymbol - Return the MCSymbol for this basic block.
47///
48MCSymbol *MachineBasicBlock::getSymbol() const {
49  const MachineFunction *MF = getParent();
50  MCContext &Ctx = MF->getContext();
51  const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix();
52  return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" +
53                               Twine(MF->getFunctionNumber()) + "_" +
54                               Twine(getNumber()));
55}
56
57
58raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
59  MBB.print(OS);
60  return OS;
61}
62
63/// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
64/// parent pointer of the MBB, the MBB numbering, and any instructions in the
65/// MBB to be on the right operand list for registers.
66///
67/// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
68/// gets the next available unique MBB number. If it is removed from a
69/// MachineFunction, it goes back to being #-1.
70void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
71  MachineFunction &MF = *N->getParent();
72  N->Number = MF.addToMBBNumbering(N);
73
74  // Make sure the instructions have their operands in the reginfo lists.
75  MachineRegisterInfo &RegInfo = MF.getRegInfo();
76  for (MachineBasicBlock::instr_iterator
77         I = N->instr_begin(), E = N->instr_end(); I != E; ++I)
78    I->AddRegOperandsToUseLists(RegInfo);
79
80  LeakDetector::removeGarbageObject(N);
81}
82
83void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
84  N->getParent()->removeFromMBBNumbering(N->Number);
85  N->Number = -1;
86  LeakDetector::addGarbageObject(N);
87}
88
89
90/// addNodeToList (MI) - When we add an instruction to a basic block
91/// list, we update its parent pointer and add its operands from reg use/def
92/// lists if appropriate.
93void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
94  assert(N->getParent() == 0 && "machine instruction already in a basic block");
95  N->setParent(Parent);
96
97  // Add the instruction's register operands to their corresponding
98  // use/def lists.
99  MachineFunction *MF = Parent->getParent();
100  N->AddRegOperandsToUseLists(MF->getRegInfo());
101
102  LeakDetector::removeGarbageObject(N);
103}
104
105/// removeNodeFromList (MI) - When we remove an instruction from a basic block
106/// list, we update its parent pointer and remove its operands from reg use/def
107/// lists if appropriate.
108void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
109  assert(N->getParent() != 0 && "machine instruction not in a basic block");
110
111  // Remove from the use/def lists.
112  N->RemoveRegOperandsFromUseLists();
113
114  N->setParent(0);
115
116  LeakDetector::addGarbageObject(N);
117}
118
119/// transferNodesFromList (MI) - When moving a range of instructions from one
120/// MBB list to another, we need to update the parent pointers and the use/def
121/// lists.
122void ilist_traits<MachineInstr>::
123transferNodesFromList(ilist_traits<MachineInstr> &fromList,
124                      ilist_iterator<MachineInstr> first,
125                      ilist_iterator<MachineInstr> last) {
126  assert(Parent->getParent() == fromList.Parent->getParent() &&
127        "MachineInstr parent mismatch!");
128
129  // Splice within the same MBB -> no change.
130  if (Parent == fromList.Parent) return;
131
132  // If splicing between two blocks within the same function, just update the
133  // parent pointers.
134  for (; first != last; ++first)
135    first->setParent(Parent);
136}
137
138void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
139  assert(!MI->getParent() && "MI is still in a block!");
140  Parent->getParent()->DeleteMachineInstr(MI);
141}
142
143MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
144  instr_iterator I = instr_begin(), E = instr_end();
145  while (I != E && I->isPHI())
146    ++I;
147  assert(!I->isInsideBundle() && "First non-phi MI cannot be inside a bundle!");
148  return I;
149}
150
151MachineBasicBlock::iterator
152MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
153  iterator E = end();
154  while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue()))
155    ++I;
156  // FIXME: This needs to change if we wish to bundle labels / dbg_values
157  // inside the bundle.
158  assert(!I->isInsideBundle() &&
159         "First non-phi / non-label instruction is inside a bundle!");
160  return I;
161}
162
163MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
164  iterator B = begin(), E = end(), I = E;
165  while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
166    ; /*noop */
167  while (I != E && !I->isTerminator())
168    ++I;
169  return I;
170}
171
172MachineBasicBlock::const_iterator
173MachineBasicBlock::getFirstTerminator() const {
174  const_iterator B = begin(), E = end(), I = E;
175  while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
176    ; /*noop */
177  while (I != E && !I->isTerminator())
178    ++I;
179  return I;
180}
181
182MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
183  instr_iterator B = instr_begin(), E = instr_end(), I = E;
184  while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
185    ; /*noop */
186  while (I != E && !I->isTerminator())
187    ++I;
188  return I;
189}
190
191MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
192  // Skip over end-of-block dbg_value instructions.
193  instr_iterator B = instr_begin(), I = instr_end();
194  while (I != B) {
195    --I;
196    // Return instruction that starts a bundle.
197    if (I->isDebugValue() || I->isInsideBundle())
198      continue;
199    return I;
200  }
201  // The block is all debug values.
202  return end();
203}
204
205MachineBasicBlock::const_iterator
206MachineBasicBlock::getLastNonDebugInstr() const {
207  // Skip over end-of-block dbg_value instructions.
208  const_instr_iterator B = instr_begin(), I = instr_end();
209  while (I != B) {
210    --I;
211    // Return instruction that starts a bundle.
212    if (I->isDebugValue() || I->isInsideBundle())
213      continue;
214    return I;
215  }
216  // The block is all debug values.
217  return end();
218}
219
220const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const {
221  // A block with a landing pad successor only has one other successor.
222  if (succ_size() > 2)
223    return 0;
224  for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
225    if ((*I)->isLandingPad())
226      return *I;
227  return 0;
228}
229
230void MachineBasicBlock::dump() const {
231  print(dbgs());
232}
233
234StringRef MachineBasicBlock::getName() const {
235  if (const BasicBlock *LBB = getBasicBlock())
236    return LBB->getName();
237  else
238    return "(null)";
239}
240
241/// Return a hopefully unique identifier for this block.
242std::string MachineBasicBlock::getFullName() const {
243  std::string Name;
244  if (getParent())
245    Name = (getParent()->getFunction()->getName() + ":").str();
246  if (getBasicBlock())
247    Name += getBasicBlock()->getName();
248  else
249    Name += (Twine("BB") + Twine(getNumber())).str();
250  return Name;
251}
252
253void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const {
254  const MachineFunction *MF = getParent();
255  if (!MF) {
256    OS << "Can't print out MachineBasicBlock because parent MachineFunction"
257       << " is null\n";
258    return;
259  }
260
261  if (Indexes)
262    OS << Indexes->getMBBStartIdx(this) << '\t';
263
264  OS << "BB#" << getNumber() << ": ";
265
266  const char *Comma = "";
267  if (const BasicBlock *LBB = getBasicBlock()) {
268    OS << Comma << "derived from LLVM BB ";
269    WriteAsOperand(OS, LBB, /*PrintType=*/false);
270    Comma = ", ";
271  }
272  if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
273  if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
274  if (Alignment) {
275    OS << Comma << "Align " << Alignment << " (" << (1u << Alignment)
276       << " bytes)";
277    Comma = ", ";
278  }
279
280  OS << '\n';
281
282  const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
283  if (!livein_empty()) {
284    if (Indexes) OS << '\t';
285    OS << "    Live Ins:";
286    for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
287      OS << ' ' << PrintReg(*I, TRI);
288    OS << '\n';
289  }
290  // Print the preds of this block according to the CFG.
291  if (!pred_empty()) {
292    if (Indexes) OS << '\t';
293    OS << "    Predecessors according to CFG:";
294    for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
295      OS << " BB#" << (*PI)->getNumber();
296    OS << '\n';
297  }
298
299  for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) {
300    if (Indexes) {
301      if (Indexes->hasIndex(I))
302        OS << Indexes->getInstructionIndex(I);
303      OS << '\t';
304    }
305    OS << '\t';
306    if (I->isInsideBundle())
307      OS << "  * ";
308    I->print(OS, &getParent()->getTarget());
309  }
310
311  // Print the successors of this block according to the CFG.
312  if (!succ_empty()) {
313    if (Indexes) OS << '\t';
314    OS << "    Successors according to CFG:";
315    for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
316      OS << " BB#" << (*SI)->getNumber();
317    OS << '\n';
318  }
319}
320
321void MachineBasicBlock::removeLiveIn(unsigned Reg) {
322  std::vector<unsigned>::iterator I =
323    std::find(LiveIns.begin(), LiveIns.end(), Reg);
324  if (I != LiveIns.end())
325    LiveIns.erase(I);
326}
327
328bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
329  livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
330  return I != livein_end();
331}
332
333void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
334  getParent()->splice(NewAfter, this);
335}
336
337void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
338  MachineFunction::iterator BBI = NewBefore;
339  getParent()->splice(++BBI, this);
340}
341
342void MachineBasicBlock::updateTerminator() {
343  const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
344  // A block with no successors has no concerns with fall-through edges.
345  if (this->succ_empty()) return;
346
347  MachineBasicBlock *TBB = 0, *FBB = 0;
348  SmallVector<MachineOperand, 4> Cond;
349  DebugLoc dl;  // FIXME: this is nowhere
350  bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond);
351  (void) B;
352  assert(!B && "UpdateTerminators requires analyzable predecessors!");
353  if (Cond.empty()) {
354    if (TBB) {
355      // The block has an unconditional branch. If its successor is now
356      // its layout successor, delete the branch.
357      if (isLayoutSuccessor(TBB))
358        TII->RemoveBranch(*this);
359    } else {
360      // The block has an unconditional fallthrough. If its successor is not
361      // its layout successor, insert a branch. First we have to locate the
362      // only non-landing-pad successor, as that is the fallthrough block.
363      for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
364        if ((*SI)->isLandingPad())
365          continue;
366        assert(!TBB && "Found more than one non-landing-pad successor!");
367        TBB = *SI;
368      }
369
370      // If there is no non-landing-pad successor, the block has no
371      // fall-through edges to be concerned with.
372      if (!TBB)
373        return;
374
375      // Finally update the unconditional successor to be reached via a branch
376      // if it would not be reached by fallthrough.
377      if (!isLayoutSuccessor(TBB))
378        TII->InsertBranch(*this, TBB, 0, Cond, dl);
379    }
380  } else {
381    if (FBB) {
382      // The block has a non-fallthrough conditional branch. If one of its
383      // successors is its layout successor, rewrite it to a fallthrough
384      // conditional branch.
385      if (isLayoutSuccessor(TBB)) {
386        if (TII->ReverseBranchCondition(Cond))
387          return;
388        TII->RemoveBranch(*this);
389        TII->InsertBranch(*this, FBB, 0, Cond, dl);
390      } else if (isLayoutSuccessor(FBB)) {
391        TII->RemoveBranch(*this);
392        TII->InsertBranch(*this, TBB, 0, Cond, dl);
393      }
394    } else {
395      // Walk through the successors and find the successor which is not
396      // a landing pad and is not the conditional branch destination (in TBB)
397      // as the fallthrough successor.
398      MachineBasicBlock *FallthroughBB = 0;
399      for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
400        if ((*SI)->isLandingPad() || *SI == TBB)
401          continue;
402        assert(!FallthroughBB && "Found more than one fallthrough successor.");
403        FallthroughBB = *SI;
404      }
405      if (!FallthroughBB && canFallThrough()) {
406        // We fallthrough to the same basic block as the conditional jump
407        // targets. Remove the conditional jump, leaving unconditional
408        // fallthrough.
409        // FIXME: This does not seem like a reasonable pattern to support, but it
410        // has been seen in the wild coming out of degenerate ARM test cases.
411        TII->RemoveBranch(*this);
412
413        // Finally update the unconditional successor to be reached via a branch
414        // if it would not be reached by fallthrough.
415        if (!isLayoutSuccessor(TBB))
416          TII->InsertBranch(*this, TBB, 0, Cond, dl);
417        return;
418      }
419
420      // The block has a fallthrough conditional branch.
421      if (isLayoutSuccessor(TBB)) {
422        if (TII->ReverseBranchCondition(Cond)) {
423          // We can't reverse the condition, add an unconditional branch.
424          Cond.clear();
425          TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
426          return;
427        }
428        TII->RemoveBranch(*this);
429        TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
430      } else if (!isLayoutSuccessor(FallthroughBB)) {
431        TII->RemoveBranch(*this);
432        TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl);
433      }
434    }
435  }
436}
437
438void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) {
439
440  // If we see non-zero value for the first time it means we actually use Weight
441  // list, so we fill all Weights with 0's.
442  if (weight != 0 && Weights.empty())
443    Weights.resize(Successors.size());
444
445  if (weight != 0 || !Weights.empty())
446    Weights.push_back(weight);
447
448   Successors.push_back(succ);
449   succ->addPredecessor(this);
450 }
451
452void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
453  succ->removePredecessor(this);
454  succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
455  assert(I != Successors.end() && "Not a current successor!");
456
457  // If Weight list is empty it means we don't use it (disabled optimization).
458  if (!Weights.empty()) {
459    weight_iterator WI = getWeightIterator(I);
460    Weights.erase(WI);
461  }
462
463  Successors.erase(I);
464}
465
466MachineBasicBlock::succ_iterator
467MachineBasicBlock::removeSuccessor(succ_iterator I) {
468  assert(I != Successors.end() && "Not a current successor!");
469
470  // If Weight list is empty it means we don't use it (disabled optimization).
471  if (!Weights.empty()) {
472    weight_iterator WI = getWeightIterator(I);
473    Weights.erase(WI);
474  }
475
476  (*I)->removePredecessor(this);
477  return Successors.erase(I);
478}
479
480void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
481                                         MachineBasicBlock *New) {
482  uint32_t weight = 0;
483  succ_iterator SI = std::find(Successors.begin(), Successors.end(), Old);
484
485  // If Weight list is empty it means we don't use it (disabled optimization).
486  if (!Weights.empty()) {
487    weight_iterator WI = getWeightIterator(SI);
488    weight = *WI;
489  }
490
491  // Update the successor information.
492  removeSuccessor(SI);
493  addSuccessor(New, weight);
494}
495
496void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
497  Predecessors.push_back(pred);
498}
499
500void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
501  pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred);
502  assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
503  Predecessors.erase(I);
504}
505
506void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
507  if (this == fromMBB)
508    return;
509
510  while (!fromMBB->succ_empty()) {
511    MachineBasicBlock *Succ = *fromMBB->succ_begin();
512    uint32_t weight = 0;
513
514
515    // If Weight list is empty it means we don't use it (disabled optimization).
516    if (!fromMBB->Weights.empty())
517      weight = *fromMBB->Weights.begin();
518
519    addSuccessor(Succ, weight);
520    fromMBB->removeSuccessor(Succ);
521  }
522}
523
524void
525MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) {
526  if (this == fromMBB)
527    return;
528
529  while (!fromMBB->succ_empty()) {
530    MachineBasicBlock *Succ = *fromMBB->succ_begin();
531    addSuccessor(Succ);
532    fromMBB->removeSuccessor(Succ);
533
534    // Fix up any PHI nodes in the successor.
535    for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(),
536           ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI)
537      for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) {
538        MachineOperand &MO = MI->getOperand(i);
539        if (MO.getMBB() == fromMBB)
540          MO.setMBB(this);
541      }
542  }
543}
544
545bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
546  const_succ_iterator I = std::find(Successors.begin(), Successors.end(), MBB);
547  return I != Successors.end();
548}
549
550bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
551  MachineFunction::const_iterator I(this);
552  return llvm::next(I) == MachineFunction::const_iterator(MBB);
553}
554
555bool MachineBasicBlock::canFallThrough() {
556  MachineFunction::iterator Fallthrough = this;
557  ++Fallthrough;
558  // If FallthroughBlock is off the end of the function, it can't fall through.
559  if (Fallthrough == getParent()->end())
560    return false;
561
562  // If FallthroughBlock isn't a successor, no fallthrough is possible.
563  if (!isSuccessor(Fallthrough))
564    return false;
565
566  // Analyze the branches, if any, at the end of the block.
567  MachineBasicBlock *TBB = 0, *FBB = 0;
568  SmallVector<MachineOperand, 4> Cond;
569  const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
570  if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) {
571    // If we couldn't analyze the branch, examine the last instruction.
572    // If the block doesn't end in a known control barrier, assume fallthrough
573    // is possible. The isPredicated check is needed because this code can be
574    // called during IfConversion, where an instruction which is normally a
575    // Barrier is predicated and thus no longer an actual control barrier.
576    return empty() || !back().isBarrier() || TII->isPredicated(&back());
577  }
578
579  // If there is no branch, control always falls through.
580  if (TBB == 0) return true;
581
582  // If there is some explicit branch to the fallthrough block, it can obviously
583  // reach, even though the branch should get folded to fall through implicitly.
584  if (MachineFunction::iterator(TBB) == Fallthrough ||
585      MachineFunction::iterator(FBB) == Fallthrough)
586    return true;
587
588  // If it's an unconditional branch to some block not the fall through, it
589  // doesn't fall through.
590  if (Cond.empty()) return false;
591
592  // Otherwise, if it is conditional and has no explicit false block, it falls
593  // through.
594  return FBB == 0;
595}
596
597MachineBasicBlock *
598MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) {
599  MachineFunction *MF = getParent();
600  DebugLoc dl;  // FIXME: this is nowhere
601
602  // We may need to update this's terminator, but we can't do that if
603  // AnalyzeBranch fails. If this uses a jump table, we won't touch it.
604  const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
605  MachineBasicBlock *TBB = 0, *FBB = 0;
606  SmallVector<MachineOperand, 4> Cond;
607  if (TII->AnalyzeBranch(*this, TBB, FBB, Cond))
608    return NULL;
609
610  // Avoid bugpoint weirdness: A block may end with a conditional branch but
611  // jumps to the same MBB is either case. We have duplicate CFG edges in that
612  // case that we can't handle. Since this never happens in properly optimized
613  // code, just skip those edges.
614  if (TBB && TBB == FBB) {
615    DEBUG(dbgs() << "Won't split critical edge after degenerate BB#"
616                 << getNumber() << '\n');
617    return NULL;
618  }
619
620  MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
621  MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB);
622  DEBUG(dbgs() << "Splitting critical edge:"
623        " BB#" << getNumber()
624        << " -- BB#" << NMBB->getNumber()
625        << " -- BB#" << Succ->getNumber() << '\n');
626
627  // On some targets like Mips, branches may kill virtual registers. Make sure
628  // that LiveVariables is properly updated after updateTerminator replaces the
629  // terminators.
630  LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>();
631
632  // Collect a list of virtual registers killed by the terminators.
633  SmallVector<unsigned, 4> KilledRegs;
634  if (LV)
635    for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
636         I != E; ++I) {
637      MachineInstr *MI = I;
638      for (MachineInstr::mop_iterator OI = MI->operands_begin(),
639           OE = MI->operands_end(); OI != OE; ++OI) {
640        if (!OI->isReg() || OI->getReg() == 0 ||
641            !OI->isUse() || !OI->isKill() || OI->isUndef())
642          continue;
643        unsigned Reg = OI->getReg();
644        if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
645            LV->getVarInfo(Reg).removeKill(MI)) {
646          KilledRegs.push_back(Reg);
647          DEBUG(dbgs() << "Removing terminator kill: " << *MI);
648          OI->setIsKill(false);
649        }
650      }
651    }
652
653  ReplaceUsesOfBlockWith(Succ, NMBB);
654  updateTerminator();
655
656  // Insert unconditional "jump Succ" instruction in NMBB if necessary.
657  NMBB->addSuccessor(Succ);
658  if (!NMBB->isLayoutSuccessor(Succ)) {
659    Cond.clear();
660    MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl);
661  }
662
663  // Fix PHI nodes in Succ so they refer to NMBB instead of this
664  for (MachineBasicBlock::instr_iterator
665         i = Succ->instr_begin(),e = Succ->instr_end();
666       i != e && i->isPHI(); ++i)
667    for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
668      if (i->getOperand(ni+1).getMBB() == this)
669        i->getOperand(ni+1).setMBB(NMBB);
670
671  // Inherit live-ins from the successor
672  for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(),
673	 E = Succ->livein_end(); I != E; ++I)
674    NMBB->addLiveIn(*I);
675
676  // Update LiveVariables.
677  const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
678  if (LV) {
679    // Restore kills of virtual registers that were killed by the terminators.
680    while (!KilledRegs.empty()) {
681      unsigned Reg = KilledRegs.pop_back_val();
682      for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) {
683        if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false))
684          continue;
685        if (TargetRegisterInfo::isVirtualRegister(Reg))
686          LV->getVarInfo(Reg).Kills.push_back(I);
687        DEBUG(dbgs() << "Restored terminator kill: " << *I);
688        break;
689      }
690    }
691    // Update relevant live-through information.
692    LV->addNewBlock(NMBB, this, Succ);
693  }
694
695  if (MachineDominatorTree *MDT =
696      P->getAnalysisIfAvailable<MachineDominatorTree>()) {
697    // Update dominator information.
698    MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ);
699
700    bool IsNewIDom = true;
701    for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end();
702         PI != E; ++PI) {
703      MachineBasicBlock *PredBB = *PI;
704      if (PredBB == NMBB)
705        continue;
706      if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) {
707        IsNewIDom = false;
708        break;
709      }
710    }
711
712    // We know "this" dominates the newly created basic block.
713    MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this);
714
715    // If all the other predecessors of "Succ" are dominated by "Succ" itself
716    // then the new block is the new immediate dominator of "Succ". Otherwise,
717    // the new block doesn't dominate anything.
718    if (IsNewIDom)
719      MDT->changeImmediateDominator(SucccDTNode, NewDTNode);
720  }
721
722  if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>())
723    if (MachineLoop *TIL = MLI->getLoopFor(this)) {
724      // If one or the other blocks were not in a loop, the new block is not
725      // either, and thus LI doesn't need to be updated.
726      if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
727        if (TIL == DestLoop) {
728          // Both in the same loop, the NMBB joins loop.
729          DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
730        } else if (TIL->contains(DestLoop)) {
731          // Edge from an outer loop to an inner loop.  Add to the outer loop.
732          TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
733        } else if (DestLoop->contains(TIL)) {
734          // Edge from an inner loop to an outer loop.  Add to the outer loop.
735          DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
736        } else {
737          // Edge from two loops with no containment relation.  Because these
738          // are natural loops, we know that the destination block must be the
739          // header of its loop (adding a branch into a loop elsewhere would
740          // create an irreducible loop).
741          assert(DestLoop->getHeader() == Succ &&
742                 "Should not create irreducible loops!");
743          if (MachineLoop *P = DestLoop->getParentLoop())
744            P->addBasicBlockToLoop(NMBB, MLI->getBase());
745        }
746      }
747    }
748
749  return NMBB;
750}
751
752MachineBasicBlock::iterator
753MachineBasicBlock::erase(MachineBasicBlock::iterator I) {
754  if (I->isBundle()) {
755    MachineBasicBlock::iterator E = llvm::next(I);
756    return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
757  }
758
759  return Insts.erase(I.getInstrIterator());
760}
761
762MachineInstr *MachineBasicBlock::remove(MachineInstr *I) {
763  if (I->isBundle()) {
764    instr_iterator MII = llvm::next(I);
765    iterator E = end();
766    while (MII != E && MII->isInsideBundle()) {
767      MachineInstr *MI = &*MII++;
768      Insts.remove(MI);
769    }
770  }
771
772  return Insts.remove(I);
773}
774
775void MachineBasicBlock::splice(MachineBasicBlock::iterator where,
776                               MachineBasicBlock *Other,
777                               MachineBasicBlock::iterator From) {
778  if (From->isBundle()) {
779    MachineBasicBlock::iterator To = llvm::next(From);
780    Insts.splice(where.getInstrIterator(), Other->Insts,
781                 From.getInstrIterator(), To.getInstrIterator());
782    return;
783  }
784
785  Insts.splice(where.getInstrIterator(), Other->Insts, From.getInstrIterator());
786}
787
788/// removeFromParent - This method unlinks 'this' from the containing function,
789/// and returns it, but does not delete it.
790MachineBasicBlock *MachineBasicBlock::removeFromParent() {
791  assert(getParent() && "Not embedded in a function!");
792  getParent()->remove(this);
793  return this;
794}
795
796
797/// eraseFromParent - This method unlinks 'this' from the containing function,
798/// and deletes it.
799void MachineBasicBlock::eraseFromParent() {
800  assert(getParent() && "Not embedded in a function!");
801  getParent()->erase(this);
802}
803
804
805/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
806/// 'Old', change the code and CFG so that it branches to 'New' instead.
807void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
808                                               MachineBasicBlock *New) {
809  assert(Old != New && "Cannot replace self with self!");
810
811  MachineBasicBlock::instr_iterator I = instr_end();
812  while (I != instr_begin()) {
813    --I;
814    if (!I->isTerminator()) break;
815
816    // Scan the operands of this machine instruction, replacing any uses of Old
817    // with New.
818    for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
819      if (I->getOperand(i).isMBB() &&
820          I->getOperand(i).getMBB() == Old)
821        I->getOperand(i).setMBB(New);
822  }
823
824  // Update the successor information.
825  replaceSuccessor(Old, New);
826}
827
828/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
829/// CFG to be inserted.  If we have proven that MBB can only branch to DestA and
830/// DestB, remove any other MBB successors from the CFG.  DestA and DestB can be
831/// null.
832///
833/// Besides DestA and DestB, retain other edges leading to LandingPads
834/// (currently there can be only one; we don't check or require that here).
835/// Note it is possible that DestA and/or DestB are LandingPads.
836bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
837                                             MachineBasicBlock *DestB,
838                                             bool isCond) {
839  // The values of DestA and DestB frequently come from a call to the
840  // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial
841  // values from there.
842  //
843  // 1. If both DestA and DestB are null, then the block ends with no branches
844  //    (it falls through to its successor).
845  // 2. If DestA is set, DestB is null, and isCond is false, then the block ends
846  //    with only an unconditional branch.
847  // 3. If DestA is set, DestB is null, and isCond is true, then the block ends
848  //    with a conditional branch that falls through to a successor (DestB).
849  // 4. If DestA and DestB is set and isCond is true, then the block ends with a
850  //    conditional branch followed by an unconditional branch. DestA is the
851  //    'true' destination and DestB is the 'false' destination.
852
853  bool Changed = false;
854
855  MachineFunction::iterator FallThru =
856    llvm::next(MachineFunction::iterator(this));
857
858  if (DestA == 0 && DestB == 0) {
859    // Block falls through to successor.
860    DestA = FallThru;
861    DestB = FallThru;
862  } else if (DestA != 0 && DestB == 0) {
863    if (isCond)
864      // Block ends in conditional jump that falls through to successor.
865      DestB = FallThru;
866  } else {
867    assert(DestA && DestB && isCond &&
868           "CFG in a bad state. Cannot correct CFG edges");
869  }
870
871  // Remove superfluous edges. I.e., those which aren't destinations of this
872  // basic block, duplicate edges, or landing pads.
873  SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs;
874  MachineBasicBlock::succ_iterator SI = succ_begin();
875  while (SI != succ_end()) {
876    const MachineBasicBlock *MBB = *SI;
877    if (!SeenMBBs.insert(MBB) ||
878        (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) {
879      // This is a superfluous edge, remove it.
880      SI = removeSuccessor(SI);
881      Changed = true;
882    } else {
883      ++SI;
884    }
885  }
886
887  return Changed;
888}
889
890/// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
891/// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
892DebugLoc
893MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
894  DebugLoc DL;
895  instr_iterator E = instr_end();
896  if (MBBI == E)
897    return DL;
898
899  // Skip debug declarations, we don't want a DebugLoc from them.
900  while (MBBI != E && MBBI->isDebugValue())
901    MBBI++;
902  if (MBBI != E)
903    DL = MBBI->getDebugLoc();
904  return DL;
905}
906
907/// getSuccWeight - Return weight of the edge from this block to MBB.
908///
909uint32_t MachineBasicBlock::getSuccWeight(const MachineBasicBlock *succ) const {
910  if (Weights.empty())
911    return 0;
912
913  const_succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
914  return *getWeightIterator(I);
915}
916
917/// getWeightIterator - Return wight iterator corresonding to the I successor
918/// iterator
919MachineBasicBlock::weight_iterator MachineBasicBlock::
920getWeightIterator(MachineBasicBlock::succ_iterator I) {
921  assert(Weights.size() == Successors.size() && "Async weight list!");
922  size_t index = std::distance(Successors.begin(), I);
923  assert(index < Weights.size() && "Not a current successor!");
924  return Weights.begin() + index;
925}
926
927/// getWeightIterator - Return wight iterator corresonding to the I successor
928/// iterator
929MachineBasicBlock::const_weight_iterator MachineBasicBlock::
930getWeightIterator(MachineBasicBlock::const_succ_iterator I) const {
931  assert(Weights.size() == Successors.size() && "Async weight list!");
932  const size_t index = std::distance(Successors.begin(), I);
933  assert(index < Weights.size() && "Not a current successor!");
934  return Weights.begin() + index;
935}
936
937void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
938                          bool t) {
939  OS << "BB#" << MBB->getNumber();
940}
941
942