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 //===----------------------------------------------------------------------===//
9 #define DEBUG_TYPE "hexagon_cfg"
11 #include "HexagonMachineFunctionInfo.h"
12 #include "HexagonSubtarget.h"
13 #include "HexagonTargetMachine.h"
14 #include "llvm/CodeGen/MachineDominators.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineLoopInfo.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
30 void initializeHexagonCFGOptimizerPass(PassRegistry&);
36 class HexagonCFGOptimizer : public MachineFunctionPass {
39 const HexagonTargetMachine& QTM;
40 const HexagonSubtarget &QST;
42 void InvertAndChangeJumpTarget(MachineInstr*, MachineBasicBlock*);
46 HexagonCFGOptimizer(const HexagonTargetMachine& TM)
47 : MachineFunctionPass(ID), QTM(TM), QST(*TM.getSubtargetImpl()) {
48 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
51 const char *getPassName() const {
52 return "Hexagon CFG Optimizer";
54 bool runOnMachineFunction(MachineFunction &Fn);
58 char HexagonCFGOptimizer::ID = 0;
60 static bool IsConditionalBranch(int Opc) {
61 return (Opc == Hexagon::JMP_t) || (Opc == Hexagon::JMP_f)
62 || (Opc == Hexagon::JMP_tnew_t) || (Opc == Hexagon::JMP_fnew_t);
66 static bool IsUnconditionalJump(int Opc) {
67 return (Opc == Hexagon::JMP);
72 HexagonCFGOptimizer::InvertAndChangeJumpTarget(MachineInstr* MI,
73 MachineBasicBlock* NewTarget) {
74 const HexagonInstrInfo *QII = QTM.getInstrInfo();
76 switch(MI->getOpcode()) {
78 NewOpcode = Hexagon::JMP_f;
82 NewOpcode = Hexagon::JMP_t;
85 case Hexagon::JMP_tnew_t:
86 NewOpcode = Hexagon::JMP_fnew_t;
89 case Hexagon::JMP_fnew_t:
90 NewOpcode = Hexagon::JMP_tnew_t;
94 llvm_unreachable("Cannot handle this case");
97 MI->setDesc(QII->get(NewOpcode));
98 MI->getOperand(1).setMBB(NewTarget);
102 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
104 // Loop over all of the basic blocks.
105 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
106 MBBb != MBBe; ++MBBb) {
107 MachineBasicBlock* MBB = MBBb;
109 // Traverse the basic block.
110 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
111 if (MII != MBB->end()) {
112 MachineInstr *MI = MII;
113 int Opc = MI->getOpcode();
114 if (IsConditionalBranch(Opc)) {
117 // (Case 1) Transform the code if the following condition occurs:
118 // BB1: if (p0) jump BB3
119 // ...falls-through to BB2 ...
121 // ...next block in layout is BB3...
124 // Transform this to:
125 // BB1: if (!p0) jump BB4
129 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
130 // BB1: if (p0) jump BB3
131 // ...falls-through to BB2 ...
133 // ...other basic blocks ...
135 // ...not a fall-thru
139 // Transform this to:
140 // BB1: if (!p0) jump BB4
145 unsigned NumSuccs = MBB->succ_size();
146 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
147 MachineBasicBlock* FirstSucc = *SI;
148 MachineBasicBlock* SecondSucc = *(++SI);
149 MachineBasicBlock* LayoutSucc = NULL;
150 MachineBasicBlock* JumpAroundTarget = NULL;
152 if (MBB->isLayoutSuccessor(FirstSucc)) {
153 LayoutSucc = FirstSucc;
154 JumpAroundTarget = SecondSucc;
155 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
156 LayoutSucc = SecondSucc;
157 JumpAroundTarget = FirstSucc;
159 // Odd case...cannot handle.
162 // The target of the unconditional branch must be JumpAroundTarget.
163 // TODO: If not, we should not invert the unconditional branch.
164 MachineBasicBlock* CondBranchTarget = NULL;
165 if ((MI->getOpcode() == Hexagon::JMP_t) ||
166 (MI->getOpcode() == Hexagon::JMP_f)) {
167 CondBranchTarget = MI->getOperand(1).getMBB();
170 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
174 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
176 // Ensure that BB2 has one instruction -- an unconditional jump.
177 if ((LayoutSucc->size() == 1) &&
178 IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
179 MachineBasicBlock* UncondTarget =
180 LayoutSucc->front().getOperand(0).getMBB();
181 // Check if the layout successor of BB2 is BB3.
182 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
183 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
184 JumpAroundTarget->size() >= 1 &&
185 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
186 JumpAroundTarget->pred_size() == 1 &&
187 JumpAroundTarget->succ_size() == 1;
189 if (case1 || case2) {
190 InvertAndChangeJumpTarget(MI, UncondTarget);
191 MBB->removeSuccessor(JumpAroundTarget);
192 MBB->addSuccessor(UncondTarget);
194 // Remove the unconditional branch in LayoutSucc.
195 LayoutSucc->erase(LayoutSucc->begin());
196 LayoutSucc->removeSuccessor(UncondTarget);
197 LayoutSucc->addSuccessor(JumpAroundTarget);
199 // This code performs the conversion for case 2, which moves
200 // the block to the fall-thru case (BB3 in the code above).
201 if (case2 && !case1) {
202 JumpAroundTarget->moveAfter(LayoutSucc);
203 // only move a block if it doesn't have a fall-thru. otherwise
204 // the CFG will be incorrect.
205 if (!UncondTarget->canFallThrough()) {
206 UncondTarget->moveAfter(JumpAroundTarget);
211 // Correct live-in information. Is used by post-RA scheduler
212 // The live-in to LayoutSucc is now all values live-in to
215 std::vector<unsigned> OrigLiveIn(LayoutSucc->livein_begin(),
216 LayoutSucc->livein_end());
217 std::vector<unsigned> NewLiveIn(JumpAroundTarget->livein_begin(),
218 JumpAroundTarget->livein_end());
219 for (unsigned i = 0; i < OrigLiveIn.size(); ++i) {
220 LayoutSucc->removeLiveIn(OrigLiveIn[i]);
222 for (unsigned i = 0; i < NewLiveIn.size(); ++i) {
223 LayoutSucc->addLiveIn(NewLiveIn[i]);
236 //===----------------------------------------------------------------------===//
237 // Public Constructor Functions
238 //===----------------------------------------------------------------------===//
240 static void initializePassOnce(PassRegistry &Registry) {
241 PassInfo *PI = new PassInfo("Hexagon CFG Optimizer", "hexagon-cfg",
242 &HexagonCFGOptimizer::ID, 0, false, false);
243 Registry.registerPass(*PI, true);
246 void llvm::initializeHexagonCFGOptimizerPass(PassRegistry &Registry) {
247 CALL_ONCE_INITIALIZATION(initializePassOnce)
250 FunctionPass *llvm::createHexagonCFGOptimizer(const HexagonTargetMachine &TM) {
251 return new HexagonCFGOptimizer(TM);