LLVM  5.0.0
HexagonCFGOptimizer.cpp
Go to the documentation of this file.
1 //===-- HexagonCFGOptimizer.cpp - CFG optimizations -----------------------===//
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Hexagon.h"
11 #include "HexagonSubtarget.h"
12 #include "HexagonTargetMachine.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/Debug.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "hexagon_cfg"
28 
29 namespace llvm {
32 }
33 
34 
35 namespace {
36 
37 class HexagonCFGOptimizer : public MachineFunctionPass {
38 
39 private:
40  void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *);
41  bool isOnFallThroughPath(MachineBasicBlock *MBB);
42 
43 public:
44  static char ID;
45  HexagonCFGOptimizer() : MachineFunctionPass(ID) {
47  }
48 
49  StringRef getPassName() const override { return "Hexagon CFG Optimizer"; }
50  bool runOnMachineFunction(MachineFunction &Fn) override;
51  MachineFunctionProperties getRequiredProperties() const override {
54  }
55 };
56 
57 
59 
60 static bool IsConditionalBranch(int Opc) {
61  switch (Opc) {
62  case Hexagon::J2_jumpt:
63  case Hexagon::J2_jumptpt:
64  case Hexagon::J2_jumpf:
65  case Hexagon::J2_jumpfpt:
66  case Hexagon::J2_jumptnew:
67  case Hexagon::J2_jumpfnew:
68  case Hexagon::J2_jumptnewpt:
69  case Hexagon::J2_jumpfnewpt:
70  return true;
71  }
72  return false;
73 }
74 
75 
76 static bool IsUnconditionalJump(int Opc) {
77  return (Opc == Hexagon::J2_jump);
78 }
79 
80 void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
81  MachineInstr &MI, MachineBasicBlock *NewTarget) {
82  const TargetInstrInfo *TII =
84  int NewOpcode = 0;
85  switch (MI.getOpcode()) {
86  case Hexagon::J2_jumpt:
87  NewOpcode = Hexagon::J2_jumpf;
88  break;
89 
90  case Hexagon::J2_jumpf:
91  NewOpcode = Hexagon::J2_jumpt;
92  break;
93 
94  case Hexagon::J2_jumptnewpt:
95  NewOpcode = Hexagon::J2_jumpfnewpt;
96  break;
97 
98  case Hexagon::J2_jumpfnewpt:
99  NewOpcode = Hexagon::J2_jumptnewpt;
100  break;
101 
102  default:
103  llvm_unreachable("Cannot handle this case");
104  }
105 
106  MI.setDesc(TII->get(NewOpcode));
107  MI.getOperand(1).setMBB(NewTarget);
108 }
109 
110 bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) {
111  if (MBB->canFallThrough())
112  return true;
113  for (MachineBasicBlock *PB : MBB->predecessors())
114  if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough())
115  return true;
116  return false;
117 }
118 
119 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
120  if (skipFunction(*Fn.getFunction()))
121  return false;
122 
123  // Loop over all of the basic blocks.
124  for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
125  MBBb != MBBe; ++MBBb) {
126  MachineBasicBlock *MBB = &*MBBb;
127 
128  // Traverse the basic block.
130  if (MII != MBB->end()) {
131  MachineInstr &MI = *MII;
132  int Opc = MI.getOpcode();
133  if (IsConditionalBranch(Opc)) {
134 
135  //
136  // (Case 1) Transform the code if the following condition occurs:
137  // BB1: if (p0) jump BB3
138  // ...falls-through to BB2 ...
139  // BB2: jump BB4
140  // ...next block in layout is BB3...
141  // BB3: ...
142  //
143  // Transform this to:
144  // BB1: if (!p0) jump BB4
145  // Remove BB2
146  // BB3: ...
147  //
148  // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
149  // BB1: if (p0) jump BB3
150  // ...falls-through to BB2 ...
151  // BB2: jump BB4
152  // ...other basic blocks ...
153  // BB4:
154  // ...not a fall-thru
155  // BB3: ...
156  // jump BB4
157  //
158  // Transform this to:
159  // BB1: if (!p0) jump BB4
160  // Remove BB2
161  // BB3: ...
162  // BB4: ...
163  //
164  unsigned NumSuccs = MBB->succ_size();
166  MachineBasicBlock* FirstSucc = *SI;
167  MachineBasicBlock* SecondSucc = *(++SI);
168  MachineBasicBlock* LayoutSucc = nullptr;
169  MachineBasicBlock* JumpAroundTarget = nullptr;
170 
171  if (MBB->isLayoutSuccessor(FirstSucc)) {
172  LayoutSucc = FirstSucc;
173  JumpAroundTarget = SecondSucc;
174  } else if (MBB->isLayoutSuccessor(SecondSucc)) {
175  LayoutSucc = SecondSucc;
176  JumpAroundTarget = FirstSucc;
177  } else {
178  // Odd case...cannot handle.
179  }
180 
181  // The target of the unconditional branch must be JumpAroundTarget.
182  // TODO: If not, we should not invert the unconditional branch.
183  MachineBasicBlock* CondBranchTarget = nullptr;
184  if (MI.getOpcode() == Hexagon::J2_jumpt ||
185  MI.getOpcode() == Hexagon::J2_jumpf) {
186  CondBranchTarget = MI.getOperand(1).getMBB();
187  }
188 
189  if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
190  continue;
191  }
192 
193  if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
194  // Ensure that BB2 has one instruction -- an unconditional jump.
195  if ((LayoutSucc->size() == 1) &&
196  IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
197  assert(JumpAroundTarget && "jump target is needed to process second basic block");
198  MachineBasicBlock* UncondTarget =
199  LayoutSucc->front().getOperand(0).getMBB();
200  // Check if the layout successor of BB2 is BB3.
201  bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
202  bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
203  JumpAroundTarget->size() >= 1 &&
204  IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
205  JumpAroundTarget->pred_size() == 1 &&
206  JumpAroundTarget->succ_size() == 1;
207 
208  if (case1 || case2) {
209  InvertAndChangeJumpTarget(MI, UncondTarget);
210  MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
211 
212  // Remove the unconditional branch in LayoutSucc.
213  LayoutSucc->erase(LayoutSucc->begin());
214  LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
215 
216  // This code performs the conversion for case 2, which moves
217  // the block to the fall-thru case (BB3 in the code above).
218  if (case2 && !case1) {
219  JumpAroundTarget->moveAfter(LayoutSucc);
220  // only move a block if it doesn't have a fall-thru. otherwise
221  // the CFG will be incorrect.
222  if (!isOnFallThroughPath(UncondTarget))
223  UncondTarget->moveAfter(JumpAroundTarget);
224  }
225 
226  //
227  // Correct live-in information. Is used by post-RA scheduler
228  // The live-in to LayoutSucc is now all values live-in to
229  // JumpAroundTarget.
230  //
231  std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
232  LayoutSucc->livein_begin(), LayoutSucc->livein_end());
233  std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
234  JumpAroundTarget->livein_begin(),
235  JumpAroundTarget->livein_end());
236  for (const auto &OrigLI : OrigLiveIn)
237  LayoutSucc->removeLiveIn(OrigLI.PhysReg);
238  for (const auto &NewLI : NewLiveIn)
239  LayoutSucc->addLiveIn(NewLI);
240  }
241  }
242  }
243  }
244  }
245  }
246  return true;
247 }
248 }
249 
250 
251 //===----------------------------------------------------------------------===//
252 // Public Constructor Functions
253 //===----------------------------------------------------------------------===//
254 
255 INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
256  false, false)
257 
259  return new HexagonCFGOptimizer();
260 }
unsigned succ_size() const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MachineBasicBlock * getMBB() const
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void moveAfter(MachineBasicBlock *NewBefore)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
TargetInstrInfo - Interface to description of machine instruction set.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
livein_iterator livein_begin() const
livein_iterator livein_end() const
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
void setMBB(MachineBasicBlock *MBB)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
iterator_range< pred_iterator > predecessors()
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
Iterator for intrusive lists based on ilist_node.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
llvm_unreachable("Unknown RegBank!")
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:59
INITIALIZE_PASS(HexagonCFGOptimizer,"hexagon-cfg","Hexagon CFG Optimizer", false, false) FunctionPass *llvm
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
void initializeHexagonCFGOptimizerPass(PassRegistry &)
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:40
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
unsigned pred_size() const
std::vector< MachineBasicBlock * >::iterator succ_iterator
Properties which a MachineFunction may have at a given point in time.
FunctionPass * createHexagonCFGOptimizer()