LLVM  10.0.0
MachineLoopUtils.cpp
Go to the documentation of this file.
1 //=- MachineLoopUtils.cpp - Functions for manipulating loops ----------------=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
14 using namespace llvm;
15 
16 namespace {
17 // MI's parent and BB are clones of each other. Find the equivalent copy of MI
18 // in BB.
19 MachineInstr &findEquivalentInstruction(MachineInstr &MI,
20  MachineBasicBlock *BB) {
21  MachineBasicBlock *PB = MI.getParent();
22  unsigned Offset = std::distance(PB->instr_begin(), MachineBasicBlock::instr_iterator(MI));
23  return *std::next(BB->instr_begin(), Offset);
24 }
25 } // namespace
26 
30  const TargetInstrInfo *TII) {
31  MachineFunction &MF = *Loop->getParent();
32  MachineBasicBlock *Preheader = *Loop->pred_begin();
33  if (Preheader == Loop)
34  Preheader = *std::next(Loop->pred_begin());
35  MachineBasicBlock *Exit = *Loop->succ_begin();
36  if (Exit == Loop)
37  Exit = *std::next(Loop->succ_begin());
38 
40  if (Direction == LPD_Front)
41  MF.insert(Loop->getIterator(), NewBB);
42  else
43  MF.insert(std::next(Loop->getIterator()), NewBB);
44 
45  // FIXME: Add DenseMapInfo trait for Register so we can use it as a key.
47  auto InsertPt = NewBB->end();
48  for (MachineInstr &MI : *Loop) {
49  MachineInstr *NewMI = MF.CloneMachineInstr(&MI);
50  NewBB->insert(InsertPt, NewMI);
51  for (MachineOperand &MO : NewMI->defs()) {
52  Register OrigR = MO.getReg();
53  if (OrigR.isPhysical())
54  continue;
55  Register &R = Remaps[OrigR];
56  R = MRI.createVirtualRegister(MRI.getRegClass(OrigR));
57  MO.setReg(R);
58 
59  if (Direction == LPD_Back) {
60  // Replace all uses outside the original loop with the new register.
61  // FIXME: is the use_iterator stable enough to mutate register uses
62  // while iterating?
64  for (auto &Use : MRI.use_operands(OrigR))
65  if (Use.getParent()->getParent() != Loop)
66  Uses.push_back(&Use);
67  for (auto *Use : Uses) {
68  MRI.constrainRegClass(R, MRI.getRegClass(Use->getReg()));
69  Use->setReg(R);
70  }
71  }
72  }
73  }
74 
75  for (auto I = NewBB->getFirstNonPHI(); I != NewBB->end(); ++I)
76  for (MachineOperand &MO : I->uses())
77  if (MO.isReg() && Remaps.count(MO.getReg()))
78  MO.setReg(Remaps[MO.getReg()]);
79 
80  for (auto I = NewBB->begin(); I->isPHI(); ++I) {
81  MachineInstr &MI = *I;
82  unsigned LoopRegIdx = 3, InitRegIdx = 1;
83  if (MI.getOperand(2).getMBB() != Preheader)
84  std::swap(LoopRegIdx, InitRegIdx);
85  MachineInstr &OrigPhi = findEquivalentInstruction(MI, Loop);
86  assert(OrigPhi.isPHI());
87  if (Direction == LPD_Front) {
88  // When peeling front, we are only left with the initial value from the
89  // preheader.
90  Register R = MI.getOperand(LoopRegIdx).getReg();
91  if (Remaps.count(R))
92  R = Remaps[R];
93  OrigPhi.getOperand(InitRegIdx).setReg(R);
94  MI.RemoveOperand(LoopRegIdx + 1);
95  MI.RemoveOperand(LoopRegIdx + 0);
96  } else {
97  // When peeling back, the initial value is the loop-carried value from
98  // the original loop.
99  Register LoopReg = OrigPhi.getOperand(LoopRegIdx).getReg();
100  MI.getOperand(LoopRegIdx).setReg(LoopReg);
101  MI.RemoveOperand(InitRegIdx + 1);
102  MI.RemoveOperand(InitRegIdx + 0);
103  }
104  }
105 
106  DebugLoc DL;
107  if (Direction == LPD_Front) {
108  Preheader->replaceSuccessor(Loop, NewBB);
109  NewBB->addSuccessor(Loop);
110  Loop->replacePhiUsesWith(Preheader, NewBB);
111  if (TII->removeBranch(*Preheader) > 0)
112  TII->insertBranch(*Preheader, NewBB, nullptr, {}, DL);
113  TII->removeBranch(*NewBB);
114  TII->insertBranch(*NewBB, Loop, nullptr, {}, DL);
115  } else {
116  Loop->replaceSuccessor(Exit, NewBB);
117  Exit->replacePhiUsesWith(Loop, NewBB);
118  NewBB->addSuccessor(Exit);
119 
120  MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
122  bool CanAnalyzeBr = !TII->analyzeBranch(*Loop, TBB, FBB, Cond);
123  (void)CanAnalyzeBr;
124  assert(CanAnalyzeBr && "Must be able to analyze the loop branch!");
125  TII->removeBranch(*Loop);
126  TII->insertBranch(*Loop, TBB == Exit ? NewBB : TBB,
127  FBB == Exit ? NewBB : FBB, Cond, DL);
128  if (TII->removeBranch(*NewBB) > 0)
129  TII->insertBranch(*NewBB, Exit, nullptr, {}, DL);
130  }
131 
132  return NewBB;
133 }
134 
137  Loop->getExitBlocks(ExitBlocks);
138 
139  for (auto *MBB : ExitBlocks)
140  if (MBB->isLiveIn(PhysReg))
141  return true;
142 
143  return false;
144 }
instr_iterator instr_begin()
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
virtual unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const
Insert branch code into the end of the specified MachineBasicBlock.
bool isRegLiveInExitBlocks(MachineLoop *Loop, int PhysReg)
Return true if PhysReg is live outside the loop, i.e.
A debug info location.
Definition: DebugLoc.h:33
Peel the first iteration of the loop.
bool isPHI() const
virtual unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const
Remove the branching code at the end of the specific MBB.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
const HexagonInstrInfo * TII
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
Direction
An enum for the direction of the loop.
Definition: LoopInfo.h:677
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:62
bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:95
void setReg(Register Reg)
Change the register this operand corresponds to.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
TargetInstrInfo - Interface to description of machine instruction set.
unsigned const MachineRegisterInfo * MRI
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
Definition: MachineInstr.h:514
self_iterator getIterator()
Definition: ilist_node.h:81
virtual bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e...
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
Create a new MachineInstr which is a copy of Orig, identical in all ways except the instruction has n...
iterator_range< use_iterator > use_operands(unsigned Reg) const
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Peel the last iteration of the loop.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:271
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:63
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
#define I(x, y, z)
Definition: MD5.cpp:58
iterator end()
Definition: DenseMap.h:82
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
Instructions::iterator instr_iterator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
IRTranslator LLVM IR MI
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:431
MachineBasicBlock * PeelSingleBlockLoop(LoopPeelDirection Direction, MachineBasicBlock *Loop, MachineRegisterInfo &MRI, const TargetInstrInfo *TII)
Peels a single block loop.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19