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 *);
44 HexagonCFGOptimizer() : MachineFunctionPass(ID) {
45 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
48 const char *getPassName() const override {
49 return "Hexagon CFG Optimizer";
51 bool runOnMachineFunction(MachineFunction &Fn) override;
52 MachineFunctionProperties getRequiredProperties() const override {
53 return MachineFunctionProperties().set(
54 MachineFunctionProperties::Property::AllVRegsAllocated);
59 char HexagonCFGOptimizer::ID = 0;
61 static bool IsConditionalBranch(int Opc) {
62 return (Opc == Hexagon::J2_jumpt) || (Opc == Hexagon::J2_jumpf)
63 || (Opc == Hexagon::J2_jumptnewpt) || (Opc == Hexagon::J2_jumpfnewpt);
67 static bool IsUnconditionalJump(int Opc) {
68 return (Opc == Hexagon::J2_jump);
71 void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
72 MachineInstr &MI, MachineBasicBlock *NewTarget) {
73 const TargetInstrInfo *TII =
74 MI.getParent()->getParent()->getSubtarget().getInstrInfo();
76 switch (MI.getOpcode()) {
77 case Hexagon::J2_jumpt:
78 NewOpcode = Hexagon::J2_jumpf;
81 case Hexagon::J2_jumpf:
82 NewOpcode = Hexagon::J2_jumpt;
85 case Hexagon::J2_jumptnewpt:
86 NewOpcode = Hexagon::J2_jumpfnewpt;
89 case Hexagon::J2_jumpfnewpt:
90 NewOpcode = Hexagon::J2_jumptnewpt;
94 llvm_unreachable("Cannot handle this case");
97 MI.setDesc(TII->get(NewOpcode));
98 MI.getOperand(1).setMBB(NewTarget);
102 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
103 if (skipFunction(*Fn.getFunction()))
106 // Loop over all of the basic blocks.
107 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
108 MBBb != MBBe; ++MBBb) {
109 MachineBasicBlock *MBB = &*MBBb;
111 // Traverse the basic block.
112 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
113 if (MII != MBB->end()) {
114 MachineInstr &MI = *MII;
115 int Opc = MI.getOpcode();
116 if (IsConditionalBranch(Opc)) {
119 // (Case 1) Transform the code if the following condition occurs:
120 // BB1: if (p0) jump BB3
121 // ...falls-through to BB2 ...
123 // ...next block in layout is BB3...
126 // Transform this to:
127 // BB1: if (!p0) jump BB4
131 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
132 // BB1: if (p0) jump BB3
133 // ...falls-through to BB2 ...
135 // ...other basic blocks ...
137 // ...not a fall-thru
141 // Transform this to:
142 // BB1: if (!p0) jump BB4
147 unsigned NumSuccs = MBB->succ_size();
148 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
149 MachineBasicBlock* FirstSucc = *SI;
150 MachineBasicBlock* SecondSucc = *(++SI);
151 MachineBasicBlock* LayoutSucc = nullptr;
152 MachineBasicBlock* JumpAroundTarget = nullptr;
154 if (MBB->isLayoutSuccessor(FirstSucc)) {
155 LayoutSucc = FirstSucc;
156 JumpAroundTarget = SecondSucc;
157 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
158 LayoutSucc = SecondSucc;
159 JumpAroundTarget = FirstSucc;
161 // Odd case...cannot handle.
164 // The target of the unconditional branch must be JumpAroundTarget.
165 // TODO: If not, we should not invert the unconditional branch.
166 MachineBasicBlock* CondBranchTarget = nullptr;
167 if (MI.getOpcode() == Hexagon::J2_jumpt ||
168 MI.getOpcode() == Hexagon::J2_jumpf) {
169 CondBranchTarget = MI.getOperand(1).getMBB();
172 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
176 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
178 // Ensure that BB2 has one instruction -- an unconditional jump.
179 if ((LayoutSucc->size() == 1) &&
180 IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
181 assert(JumpAroundTarget && "jump target is needed to process second basic block");
182 MachineBasicBlock* UncondTarget =
183 LayoutSucc->front().getOperand(0).getMBB();
184 // Check if the layout successor of BB2 is BB3.
185 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
186 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
187 JumpAroundTarget->size() >= 1 &&
188 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
189 JumpAroundTarget->pred_size() == 1 &&
190 JumpAroundTarget->succ_size() == 1;
192 if (case1 || case2) {
193 InvertAndChangeJumpTarget(MI, UncondTarget);
194 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
196 // Remove the unconditional branch in LayoutSucc.
197 LayoutSucc->erase(LayoutSucc->begin());
198 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
200 // This code performs the conversion for case 2, which moves
201 // the block to the fall-thru case (BB3 in the code above).
202 if (case2 && !case1) {
203 JumpAroundTarget->moveAfter(LayoutSucc);
204 // only move a block if it doesn't have a fall-thru. otherwise
205 // the CFG will be incorrect.
206 if (!UncondTarget->canFallThrough()) {
207 UncondTarget->moveAfter(JumpAroundTarget);
212 // Correct live-in information. Is used by post-RA scheduler
213 // The live-in to LayoutSucc is now all values live-in to
216 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
217 LayoutSucc->livein_begin(), LayoutSucc->livein_end());
218 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
219 JumpAroundTarget->livein_begin(),
220 JumpAroundTarget->livein_end());
221 for (const auto &OrigLI : OrigLiveIn)
222 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
223 for (const auto &NewLI : NewLiveIn)
224 LayoutSucc->addLiveIn(NewLI);
236 //===----------------------------------------------------------------------===//
237 // Public Constructor Functions
238 //===----------------------------------------------------------------------===//
240 INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
243 FunctionPass *llvm::createHexagonCFGOptimizer() {
244 return new HexagonCFGOptimizer();