1 //===- HexagonCFGOptimizer.cpp - CFG optimizations ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/CodeGen/MachineBasicBlock.h"
12 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/MachineOperand.h"
17 #include "llvm/CodeGen/TargetInstrInfo.h"
18 #include "llvm/CodeGen/TargetSubtargetInfo.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/ErrorHandling.h"
26 #define DEBUG_TYPE "hexagon_cfg"
30 FunctionPass *createHexagonCFGOptimizer();
31 void initializeHexagonCFGOptimizerPass(PassRegistry&);
33 } // end namespace llvm
37 class HexagonCFGOptimizer : public MachineFunctionPass {
39 void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *);
40 bool isOnFallThroughPath(MachineBasicBlock *MBB);
45 HexagonCFGOptimizer() : MachineFunctionPass(ID) {
46 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
49 StringRef getPassName() const override { return "Hexagon CFG Optimizer"; }
50 bool runOnMachineFunction(MachineFunction &Fn) override;
52 MachineFunctionProperties getRequiredProperties() const override {
53 return MachineFunctionProperties().set(
54 MachineFunctionProperties::Property::NoVRegs);
58 } // end anonymous namespace
60 char HexagonCFGOptimizer::ID = 0;
62 static bool IsConditionalBranch(int Opc) {
64 case Hexagon::J2_jumpt:
65 case Hexagon::J2_jumptpt:
66 case Hexagon::J2_jumpf:
67 case Hexagon::J2_jumpfpt:
68 case Hexagon::J2_jumptnew:
69 case Hexagon::J2_jumpfnew:
70 case Hexagon::J2_jumptnewpt:
71 case Hexagon::J2_jumpfnewpt:
77 static bool IsUnconditionalJump(int Opc) {
78 return (Opc == Hexagon::J2_jump);
81 void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
82 MachineInstr &MI, MachineBasicBlock *NewTarget) {
83 const TargetInstrInfo *TII =
84 MI.getParent()->getParent()->getSubtarget().getInstrInfo();
86 switch (MI.getOpcode()) {
87 case Hexagon::J2_jumpt:
88 NewOpcode = Hexagon::J2_jumpf;
90 case Hexagon::J2_jumpf:
91 NewOpcode = Hexagon::J2_jumpt;
93 case Hexagon::J2_jumptnewpt:
94 NewOpcode = Hexagon::J2_jumpfnewpt;
96 case Hexagon::J2_jumpfnewpt:
97 NewOpcode = Hexagon::J2_jumptnewpt;
100 llvm_unreachable("Cannot handle this case");
103 MI.setDesc(TII->get(NewOpcode));
104 MI.getOperand(1).setMBB(NewTarget);
107 bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) {
108 if (MBB->canFallThrough())
110 for (MachineBasicBlock *PB : MBB->predecessors())
111 if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough())
116 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
117 if (skipFunction(Fn.getFunction()))
120 // Loop over all of the basic blocks.
121 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
122 MBBb != MBBe; ++MBBb) {
123 MachineBasicBlock *MBB = &*MBBb;
125 // Traverse the basic block.
126 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
127 if (MII != MBB->end()) {
128 MachineInstr &MI = *MII;
129 int Opc = MI.getOpcode();
130 if (IsConditionalBranch(Opc)) {
131 // (Case 1) Transform the code if the following condition occurs:
132 // BB1: if (p0) jump BB3
133 // ...falls-through to BB2 ...
135 // ...next block in layout is BB3...
138 // Transform this to:
139 // BB1: if (!p0) jump BB4
143 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
144 // BB1: if (p0) jump BB3
145 // ...falls-through to BB2 ...
147 // ...other basic blocks ...
149 // ...not a fall-thru
153 // Transform this to:
154 // BB1: if (!p0) jump BB4
158 unsigned NumSuccs = MBB->succ_size();
159 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
160 MachineBasicBlock* FirstSucc = *SI;
161 MachineBasicBlock* SecondSucc = *(++SI);
162 MachineBasicBlock* LayoutSucc = nullptr;
163 MachineBasicBlock* JumpAroundTarget = nullptr;
165 if (MBB->isLayoutSuccessor(FirstSucc)) {
166 LayoutSucc = FirstSucc;
167 JumpAroundTarget = SecondSucc;
168 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
169 LayoutSucc = SecondSucc;
170 JumpAroundTarget = FirstSucc;
172 // Odd case...cannot handle.
175 // The target of the unconditional branch must be JumpAroundTarget.
176 // TODO: If not, we should not invert the unconditional branch.
177 MachineBasicBlock* CondBranchTarget = nullptr;
178 if (MI.getOpcode() == Hexagon::J2_jumpt ||
179 MI.getOpcode() == Hexagon::J2_jumpf) {
180 CondBranchTarget = MI.getOperand(1).getMBB();
183 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
187 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
188 // Ensure that BB2 has one instruction -- an unconditional jump.
189 if ((LayoutSucc->size() == 1) &&
190 IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
191 assert(JumpAroundTarget && "jump target is needed to process second basic block");
192 MachineBasicBlock* UncondTarget =
193 LayoutSucc->front().getOperand(0).getMBB();
194 // Check if the layout successor of BB2 is BB3.
195 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
196 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
197 !JumpAroundTarget->empty() &&
198 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
199 JumpAroundTarget->pred_size() == 1 &&
200 JumpAroundTarget->succ_size() == 1;
202 if (case1 || case2) {
203 InvertAndChangeJumpTarget(MI, UncondTarget);
204 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
206 // Remove the unconditional branch in LayoutSucc.
207 LayoutSucc->erase(LayoutSucc->begin());
208 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
210 // This code performs the conversion for case 2, which moves
211 // the block to the fall-thru case (BB3 in the code above).
212 if (case2 && !case1) {
213 JumpAroundTarget->moveAfter(LayoutSucc);
214 // only move a block if it doesn't have a fall-thru. otherwise
215 // the CFG will be incorrect.
216 if (!isOnFallThroughPath(UncondTarget))
217 UncondTarget->moveAfter(JumpAroundTarget);
220 // Correct live-in information. Is used by post-RA scheduler
221 // The live-in to LayoutSucc is now all values live-in to
223 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
224 LayoutSucc->livein_begin(), LayoutSucc->livein_end());
225 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
226 JumpAroundTarget->livein_begin(),
227 JumpAroundTarget->livein_end());
228 for (const auto &OrigLI : OrigLiveIn)
229 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
230 for (const auto &NewLI : NewLiveIn)
231 LayoutSucc->addLiveIn(NewLI);
241 //===----------------------------------------------------------------------===//
242 // Public Constructor Functions
243 //===----------------------------------------------------------------------===//
245 INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
248 FunctionPass *llvm::createHexagonCFGOptimizer() {
249 return new HexagonCFGOptimizer();