1 //===-- HexagonCFGOptimizer.cpp - CFG optimizations -----------------------===//
2 // The LLVM Compiler Infrastructure
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
10 #include "HexagonMachineFunctionInfo.h"
11 #include "HexagonSubtarget.h"
12 #include "HexagonTargetMachine.h"
13 #include "llvm/CodeGen/MachineDominators.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/CodeGen/MachineLoopInfo.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
27 #define DEBUG_TYPE "hexagon_cfg"
30 FunctionPass *createHexagonCFGOptimizer();
31 void initializeHexagonCFGOptimizerPass(PassRegistry&);
37 class HexagonCFGOptimizer : public MachineFunctionPass {
40 void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *);
41 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;
51 MachineFunctionProperties getRequiredProperties() const override {
52 return MachineFunctionProperties().set(
53 MachineFunctionProperties::Property::NoVRegs);
58 char HexagonCFGOptimizer::ID = 0;
60 static bool IsConditionalBranch(int 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:
76 static bool IsUnconditionalJump(int Opc) {
77 return (Opc == Hexagon::J2_jump);
80 void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
81 MachineInstr &MI, MachineBasicBlock *NewTarget) {
82 const TargetInstrInfo *TII =
83 MI.getParent()->getParent()->getSubtarget().getInstrInfo();
85 switch (MI.getOpcode()) {
86 case Hexagon::J2_jumpt:
87 NewOpcode = Hexagon::J2_jumpf;
90 case Hexagon::J2_jumpf:
91 NewOpcode = Hexagon::J2_jumpt;
94 case Hexagon::J2_jumptnewpt:
95 NewOpcode = Hexagon::J2_jumpfnewpt;
98 case Hexagon::J2_jumpfnewpt:
99 NewOpcode = Hexagon::J2_jumptnewpt;
103 llvm_unreachable("Cannot handle this case");
106 MI.setDesc(TII->get(NewOpcode));
107 MI.getOperand(1).setMBB(NewTarget);
110 bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) {
111 if (MBB->canFallThrough())
113 for (MachineBasicBlock *PB : MBB->predecessors())
114 if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough())
119 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
120 if (skipFunction(*Fn.getFunction()))
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;
128 // Traverse the basic block.
129 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
130 if (MII != MBB->end()) {
131 MachineInstr &MI = *MII;
132 int Opc = MI.getOpcode();
133 if (IsConditionalBranch(Opc)) {
136 // (Case 1) Transform the code if the following condition occurs:
137 // BB1: if (p0) jump BB3
138 // ...falls-through to BB2 ...
140 // ...next block in layout is BB3...
143 // Transform this to:
144 // BB1: if (!p0) jump BB4
148 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
149 // BB1: if (p0) jump BB3
150 // ...falls-through to BB2 ...
152 // ...other basic blocks ...
154 // ...not a fall-thru
158 // Transform this to:
159 // BB1: if (!p0) jump BB4
164 unsigned NumSuccs = MBB->succ_size();
165 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
166 MachineBasicBlock* FirstSucc = *SI;
167 MachineBasicBlock* SecondSucc = *(++SI);
168 MachineBasicBlock* LayoutSucc = nullptr;
169 MachineBasicBlock* JumpAroundTarget = nullptr;
171 if (MBB->isLayoutSuccessor(FirstSucc)) {
172 LayoutSucc = FirstSucc;
173 JumpAroundTarget = SecondSucc;
174 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
175 LayoutSucc = SecondSucc;
176 JumpAroundTarget = FirstSucc;
178 // Odd case...cannot handle.
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();
189 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
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;
208 if (case1 || case2) {
209 InvertAndChangeJumpTarget(MI, UncondTarget);
210 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
212 // Remove the unconditional branch in LayoutSucc.
213 LayoutSucc->erase(LayoutSucc->begin());
214 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
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);
227 // Correct live-in information. Is used by post-RA scheduler
228 // The live-in to LayoutSucc is now all values live-in to
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);
251 //===----------------------------------------------------------------------===//
252 // Public Constructor Functions
253 //===----------------------------------------------------------------------===//
255 INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
258 FunctionPass *llvm::createHexagonCFGOptimizer() {
259 return new HexagonCFGOptimizer();