| Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- HexagonCFGOptimizer.cpp - CFG optimizations -----------------------===// |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 | |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 9 | #include "Hexagon.h" |
| Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 10 | #include "HexagonMachineFunctionInfo.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "HexagonSubtarget.h" |
| 12 | #include "HexagonTargetMachine.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineDominators.h" |
| 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Passes.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/Debug.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetRegisterInfo.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "hexagon_cfg" |
| 29 | |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 30 | namespace llvm { |
| 31 | void initializeHexagonCFGOptimizerPass(PassRegistry&); |
| 32 | } |
| 33 | |
| 34 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | class HexagonCFGOptimizer : public MachineFunctionPass { |
| 38 | |
| 39 | private: |
| Krzysztof Parzyszek | d500747 | 2013-05-06 18:38:37 +0000 | [diff] [blame] | 40 | const HexagonTargetMachine& QTM; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 41 | const HexagonSubtarget &QST; |
| 42 | |
| 43 | void InvertAndChangeJumpTarget(MachineInstr*, MachineBasicBlock*); |
| 44 | |
| 45 | public: |
| 46 | static char ID; |
| Krzysztof Parzyszek | d500747 | 2013-05-06 18:38:37 +0000 | [diff] [blame] | 47 | HexagonCFGOptimizer(const HexagonTargetMachine& TM) |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 48 | : MachineFunctionPass(ID), QTM(TM), QST(*TM.getSubtargetImpl()) { |
| 49 | initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry()); |
| 50 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 51 | |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 52 | const char *getPassName() const override { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 53 | return "Hexagon CFG Optimizer"; |
| 54 | } |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 55 | bool runOnMachineFunction(MachineFunction &Fn) override; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | |
| 59 | char HexagonCFGOptimizer::ID = 0; |
| 60 | |
| 61 | static bool IsConditionalBranch(int Opc) { |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 62 | return (Opc == Hexagon::J2_jumpt) || (Opc == Hexagon::J2_jumpf) |
| 63 | || (Opc == Hexagon::J2_jumptnewpt) || (Opc == Hexagon::J2_jumpfnewpt); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | static bool IsUnconditionalJump(int Opc) { |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 68 | return (Opc == Hexagon::J2_jump); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | void |
| 73 | HexagonCFGOptimizer::InvertAndChangeJumpTarget(MachineInstr* MI, |
| 74 | MachineBasicBlock* NewTarget) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 75 | const HexagonInstrInfo *QII = QTM.getSubtargetImpl()->getInstrInfo(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 76 | int NewOpcode = 0; |
| 77 | switch(MI->getOpcode()) { |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 78 | case Hexagon::J2_jumpt: |
| 79 | NewOpcode = Hexagon::J2_jumpf; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 80 | break; |
| 81 | |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 82 | case Hexagon::J2_jumpf: |
| 83 | NewOpcode = Hexagon::J2_jumpt; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 84 | break; |
| 85 | |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 86 | case Hexagon::J2_jumptnewpt: |
| 87 | NewOpcode = Hexagon::J2_jumpfnewpt; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 88 | break; |
| 89 | |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 90 | case Hexagon::J2_jumpfnewpt: |
| 91 | NewOpcode = Hexagon::J2_jumptnewpt; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 92 | break; |
| 93 | |
| 94 | default: |
| Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 95 | llvm_unreachable("Cannot handle this case"); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | MI->setDesc(QII->get(NewOpcode)); |
| 99 | MI->getOperand(1).setMBB(NewTarget); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) { |
| 104 | |
| 105 | // Loop over all of the basic blocks. |
| 106 | for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end(); |
| 107 | MBBb != MBBe; ++MBBb) { |
| 108 | MachineBasicBlock* MBB = MBBb; |
| 109 | |
| 110 | // Traverse the basic block. |
| 111 | MachineBasicBlock::iterator MII = MBB->getFirstTerminator(); |
| 112 | if (MII != MBB->end()) { |
| 113 | MachineInstr *MI = MII; |
| 114 | int Opc = MI->getOpcode(); |
| 115 | if (IsConditionalBranch(Opc)) { |
| 116 | |
| 117 | // |
| 118 | // (Case 1) Transform the code if the following condition occurs: |
| 119 | // BB1: if (p0) jump BB3 |
| 120 | // ...falls-through to BB2 ... |
| 121 | // BB2: jump BB4 |
| 122 | // ...next block in layout is BB3... |
| 123 | // BB3: ... |
| 124 | // |
| 125 | // Transform this to: |
| 126 | // BB1: if (!p0) jump BB4 |
| 127 | // Remove BB2 |
| 128 | // BB3: ... |
| 129 | // |
| 130 | // (Case 2) A variation occurs when BB3 contains a JMP to BB4: |
| 131 | // BB1: if (p0) jump BB3 |
| 132 | // ...falls-through to BB2 ... |
| 133 | // BB2: jump BB4 |
| 134 | // ...other basic blocks ... |
| 135 | // BB4: |
| 136 | // ...not a fall-thru |
| 137 | // BB3: ... |
| 138 | // jump BB4 |
| 139 | // |
| 140 | // Transform this to: |
| 141 | // BB1: if (!p0) jump BB4 |
| 142 | // Remove BB2 |
| 143 | // BB3: ... |
| 144 | // BB4: ... |
| 145 | // |
| 146 | unsigned NumSuccs = MBB->succ_size(); |
| 147 | MachineBasicBlock::succ_iterator SI = MBB->succ_begin(); |
| 148 | MachineBasicBlock* FirstSucc = *SI; |
| 149 | MachineBasicBlock* SecondSucc = *(++SI); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 150 | MachineBasicBlock* LayoutSucc = nullptr; |
| 151 | MachineBasicBlock* JumpAroundTarget = nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 152 | |
| 153 | if (MBB->isLayoutSuccessor(FirstSucc)) { |
| 154 | LayoutSucc = FirstSucc; |
| 155 | JumpAroundTarget = SecondSucc; |
| 156 | } else if (MBB->isLayoutSuccessor(SecondSucc)) { |
| 157 | LayoutSucc = SecondSucc; |
| 158 | JumpAroundTarget = FirstSucc; |
| 159 | } else { |
| 160 | // Odd case...cannot handle. |
| 161 | } |
| 162 | |
| 163 | // The target of the unconditional branch must be JumpAroundTarget. |
| 164 | // TODO: If not, we should not invert the unconditional branch. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 165 | MachineBasicBlock* CondBranchTarget = nullptr; |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 166 | if ((MI->getOpcode() == Hexagon::J2_jumpt) || |
| 167 | (MI->getOpcode() == Hexagon::J2_jumpf)) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 168 | CondBranchTarget = MI->getOperand(1).getMBB(); |
| 169 | } |
| 170 | |
| 171 | if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) { |
| 176 | |
| 177 | // Ensure that BB2 has one instruction -- an unconditional jump. |
| 178 | if ((LayoutSucc->size() == 1) && |
| 179 | IsUnconditionalJump(LayoutSucc->front().getOpcode())) { |
| 180 | MachineBasicBlock* UncondTarget = |
| 181 | LayoutSucc->front().getOperand(0).getMBB(); |
| 182 | // Check if the layout successor of BB2 is BB3. |
| 183 | bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget); |
| 184 | bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) && |
| 185 | JumpAroundTarget->size() >= 1 && |
| 186 | IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) && |
| 187 | JumpAroundTarget->pred_size() == 1 && |
| 188 | JumpAroundTarget->succ_size() == 1; |
| 189 | |
| 190 | if (case1 || case2) { |
| 191 | InvertAndChangeJumpTarget(MI, UncondTarget); |
| 192 | MBB->removeSuccessor(JumpAroundTarget); |
| 193 | MBB->addSuccessor(UncondTarget); |
| 194 | |
| 195 | // Remove the unconditional branch in LayoutSucc. |
| 196 | LayoutSucc->erase(LayoutSucc->begin()); |
| 197 | LayoutSucc->removeSuccessor(UncondTarget); |
| 198 | LayoutSucc->addSuccessor(JumpAroundTarget); |
| 199 | |
| 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); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // |
| 212 | // Correct live-in information. Is used by post-RA scheduler |
| 213 | // The live-in to LayoutSucc is now all values live-in to |
| 214 | // JumpAroundTarget. |
| 215 | // |
| 216 | std::vector<unsigned> OrigLiveIn(LayoutSucc->livein_begin(), |
| 217 | LayoutSucc->livein_end()); |
| 218 | std::vector<unsigned> NewLiveIn(JumpAroundTarget->livein_begin(), |
| 219 | JumpAroundTarget->livein_end()); |
| 220 | for (unsigned i = 0; i < OrigLiveIn.size(); ++i) { |
| 221 | LayoutSucc->removeLiveIn(OrigLiveIn[i]); |
| 222 | } |
| 223 | for (unsigned i = 0; i < NewLiveIn.size(); ++i) { |
| 224 | LayoutSucc->addLiveIn(NewLiveIn[i]); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | // Public Constructor Functions |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 241 | static void initializePassOnce(PassRegistry &Registry) { |
| 242 | PassInfo *PI = new PassInfo("Hexagon CFG Optimizer", "hexagon-cfg", |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 243 | &HexagonCFGOptimizer::ID, nullptr, false, false); |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 244 | Registry.registerPass(*PI, true); |
| 245 | } |
| 246 | |
| 247 | void llvm::initializeHexagonCFGOptimizerPass(PassRegistry &Registry) { |
| 248 | CALL_ONCE_INITIALIZATION(initializePassOnce) |
| 249 | } |
| 250 | |
| Krzysztof Parzyszek | d500747 | 2013-05-06 18:38:37 +0000 | [diff] [blame] | 251 | FunctionPass *llvm::createHexagonCFGOptimizer(const HexagonTargetMachine &TM) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 252 | return new HexagonCFGOptimizer(TM); |
| 253 | } |