| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 1 | //===- HexagonCFGOptimizer.cpp - CFG optimizations ------------------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 9 | #include "Hexagon.h" |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 11 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
| 12 | #include "llvm/CodeGen/MachineFunction.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
| 15 | #include "llvm/CodeGen/MachineOperand.h" |
| David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 20 | #include <cassert> |
| 21 | #include <vector> |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "hexagon_cfg" |
| 26 | |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 27 | namespace llvm { |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 28 | |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 29 | FunctionPass *createHexagonCFGOptimizer(); |
| 30 | void initializeHexagonCFGOptimizerPass(PassRegistry&); |
| 31 | |
| 32 | } // end namespace llvm |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 33 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | class HexagonCFGOptimizer : public MachineFunctionPass { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 37 | private: |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 38 | void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *); |
| Krzysztof Parzyszek | 0b3acbb | 2017-04-28 21:54:11 +0000 | [diff] [blame] | 39 | bool isOnFallThroughPath(MachineBasicBlock *MBB); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 40 | |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 41 | public: |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 42 | static char ID; |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 43 | |
| Eric Christopher | 5c3376a | 2015-02-02 18:46:27 +0000 | [diff] [blame] | 44 | HexagonCFGOptimizer() : MachineFunctionPass(ID) { |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 45 | initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry()); |
| 46 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 47 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 48 | StringRef getPassName() const override { return "Hexagon CFG Optimizer"; } |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 49 | bool runOnMachineFunction(MachineFunction &Fn) override; |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 50 | |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 51 | MachineFunctionProperties getRequiredProperties() const override { |
| 52 | return MachineFunctionProperties().set( |
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 53 | MachineFunctionProperties::Property::NoVRegs); |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 54 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 57 | } // end anonymous namespace |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 58 | |
| 59 | char HexagonCFGOptimizer::ID = 0; |
| 60 | |
| 61 | static bool IsConditionalBranch(int Opc) { |
| Krzysztof Parzyszek | a243adf | 2016-08-19 14:14:09 +0000 | [diff] [blame] | 62 | switch (Opc) { |
| 63 | case Hexagon::J2_jumpt: |
| 64 | case Hexagon::J2_jumptpt: |
| 65 | case Hexagon::J2_jumpf: |
| 66 | case Hexagon::J2_jumpfpt: |
| 67 | case Hexagon::J2_jumptnew: |
| 68 | case Hexagon::J2_jumpfnew: |
| 69 | case Hexagon::J2_jumptnewpt: |
| 70 | case Hexagon::J2_jumpfnewpt: |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 76 | static bool IsUnconditionalJump(int Opc) { |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 77 | return (Opc == Hexagon::J2_jump); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 80 | void HexagonCFGOptimizer::InvertAndChangeJumpTarget( |
| 81 | MachineInstr &MI, MachineBasicBlock *NewTarget) { |
| Eric Christopher | 5c3376a | 2015-02-02 18:46:27 +0000 | [diff] [blame] | 82 | const TargetInstrInfo *TII = |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 83 | MI.getParent()->getParent()->getSubtarget().getInstrInfo(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 84 | int NewOpcode = 0; |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 85 | switch (MI.getOpcode()) { |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 86 | case Hexagon::J2_jumpt: |
| 87 | NewOpcode = Hexagon::J2_jumpf; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 88 | break; |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 89 | case Hexagon::J2_jumpf: |
| 90 | NewOpcode = Hexagon::J2_jumpt; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 91 | break; |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 92 | case Hexagon::J2_jumptnewpt: |
| 93 | NewOpcode = Hexagon::J2_jumpfnewpt; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 94 | break; |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 95 | case Hexagon::J2_jumpfnewpt: |
| 96 | NewOpcode = Hexagon::J2_jumptnewpt; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 97 | break; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 98 | default: |
| Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 99 | llvm_unreachable("Cannot handle this case"); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 102 | MI.setDesc(TII->get(NewOpcode)); |
| 103 | MI.getOperand(1).setMBB(NewTarget); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| Krzysztof Parzyszek | 0b3acbb | 2017-04-28 21:54:11 +0000 | [diff] [blame] | 106 | bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) { |
| 107 | if (MBB->canFallThrough()) |
| 108 | return true; |
| 109 | for (MachineBasicBlock *PB : MBB->predecessors()) |
| 110 | if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough()) |
| 111 | return true; |
| 112 | return false; |
| 113 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 114 | |
| 115 | bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 116 | if (skipFunction(Fn.getFunction())) |
| Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 117 | return false; |
| 118 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 119 | // Loop over all of the basic blocks. |
| 120 | for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end(); |
| 121 | MBBb != MBBe; ++MBBb) { |
| Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 122 | MachineBasicBlock *MBB = &*MBBb; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 123 | |
| 124 | // Traverse the basic block. |
| 125 | MachineBasicBlock::iterator MII = MBB->getFirstTerminator(); |
| 126 | if (MII != MBB->end()) { |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 127 | MachineInstr &MI = *MII; |
| 128 | int Opc = MI.getOpcode(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 129 | if (IsConditionalBranch(Opc)) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 130 | // (Case 1) Transform the code if the following condition occurs: |
| 131 | // BB1: if (p0) jump BB3 |
| 132 | // ...falls-through to BB2 ... |
| 133 | // BB2: jump BB4 |
| 134 | // ...next block in layout is BB3... |
| 135 | // BB3: ... |
| 136 | // |
| 137 | // Transform this to: |
| 138 | // BB1: if (!p0) jump BB4 |
| 139 | // Remove BB2 |
| 140 | // BB3: ... |
| 141 | // |
| 142 | // (Case 2) A variation occurs when BB3 contains a JMP to BB4: |
| 143 | // BB1: if (p0) jump BB3 |
| 144 | // ...falls-through to BB2 ... |
| 145 | // BB2: jump BB4 |
| 146 | // ...other basic blocks ... |
| 147 | // BB4: |
| 148 | // ...not a fall-thru |
| 149 | // BB3: ... |
| 150 | // jump BB4 |
| 151 | // |
| 152 | // Transform this to: |
| 153 | // BB1: if (!p0) jump BB4 |
| 154 | // Remove BB2 |
| 155 | // BB3: ... |
| 156 | // BB4: ... |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 157 | unsigned NumSuccs = MBB->succ_size(); |
| 158 | MachineBasicBlock::succ_iterator SI = MBB->succ_begin(); |
| 159 | MachineBasicBlock* FirstSucc = *SI; |
| 160 | MachineBasicBlock* SecondSucc = *(++SI); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 161 | MachineBasicBlock* LayoutSucc = nullptr; |
| 162 | MachineBasicBlock* JumpAroundTarget = nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 163 | |
| 164 | if (MBB->isLayoutSuccessor(FirstSucc)) { |
| 165 | LayoutSucc = FirstSucc; |
| 166 | JumpAroundTarget = SecondSucc; |
| 167 | } else if (MBB->isLayoutSuccessor(SecondSucc)) { |
| 168 | LayoutSucc = SecondSucc; |
| 169 | JumpAroundTarget = FirstSucc; |
| 170 | } else { |
| 171 | // Odd case...cannot handle. |
| 172 | } |
| 173 | |
| 174 | // The target of the unconditional branch must be JumpAroundTarget. |
| 175 | // TODO: If not, we should not invert the unconditional branch. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 176 | MachineBasicBlock* CondBranchTarget = nullptr; |
| Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 177 | if (MI.getOpcode() == Hexagon::J2_jumpt || |
| 178 | MI.getOpcode() == Hexagon::J2_jumpf) { |
| 179 | CondBranchTarget = MI.getOperand(1).getMBB(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 187 | // Ensure that BB2 has one instruction -- an unconditional jump. |
| 188 | if ((LayoutSucc->size() == 1) && |
| 189 | IsUnconditionalJump(LayoutSucc->front().getOpcode())) { |
| Krzysztof Parzyszek | 8975743 | 2016-05-05 22:00:44 +0000 | [diff] [blame] | 190 | assert(JumpAroundTarget && "jump target is needed to process second basic block"); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 191 | MachineBasicBlock* UncondTarget = |
| 192 | LayoutSucc->front().getOperand(0).getMBB(); |
| 193 | // Check if the layout successor of BB2 is BB3. |
| 194 | bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget); |
| 195 | bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) && |
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 196 | !JumpAroundTarget->empty() && |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 197 | IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) && |
| 198 | JumpAroundTarget->pred_size() == 1 && |
| 199 | JumpAroundTarget->succ_size() == 1; |
| 200 | |
| 201 | if (case1 || case2) { |
| 202 | InvertAndChangeJumpTarget(MI, UncondTarget); |
| Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 203 | MBB->replaceSuccessor(JumpAroundTarget, UncondTarget); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 204 | |
| 205 | // Remove the unconditional branch in LayoutSucc. |
| 206 | LayoutSucc->erase(LayoutSucc->begin()); |
| Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 207 | LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 208 | |
| 209 | // This code performs the conversion for case 2, which moves |
| 210 | // the block to the fall-thru case (BB3 in the code above). |
| 211 | if (case2 && !case1) { |
| 212 | JumpAroundTarget->moveAfter(LayoutSucc); |
| 213 | // only move a block if it doesn't have a fall-thru. otherwise |
| 214 | // the CFG will be incorrect. |
| Krzysztof Parzyszek | 0b3acbb | 2017-04-28 21:54:11 +0000 | [diff] [blame] | 215 | if (!isOnFallThroughPath(UncondTarget)) |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 216 | UncondTarget->moveAfter(JumpAroundTarget); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 219 | // Correct live-in information. Is used by post-RA scheduler |
| 220 | // The live-in to LayoutSucc is now all values live-in to |
| 221 | // JumpAroundTarget. |
| Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 222 | std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn( |
| 223 | LayoutSucc->livein_begin(), LayoutSucc->livein_end()); |
| 224 | std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn( |
| 225 | JumpAroundTarget->livein_begin(), |
| 226 | JumpAroundTarget->livein_end()); |
| 227 | for (const auto &OrigLI : OrigLiveIn) |
| 228 | LayoutSucc->removeLiveIn(OrigLI.PhysReg); |
| 229 | for (const auto &NewLI : NewLiveIn) |
| 230 | LayoutSucc->addLiveIn(NewLI); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | return true; |
| 238 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 239 | |
| 240 | //===----------------------------------------------------------------------===// |
| 241 | // Public Constructor Functions |
| 242 | //===----------------------------------------------------------------------===// |
| 243 | |
| Chandler Carruth | d474144 | 2016-06-03 10:13:29 +0000 | [diff] [blame] | 244 | INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer", |
| 245 | false, false) |
| Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 246 | |
| Eric Christopher | 5c3376a | 2015-02-02 18:46:27 +0000 | [diff] [blame] | 247 | FunctionPass *llvm::createHexagonCFGOptimizer() { |
| 248 | return new HexagonCFGOptimizer(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 249 | } |