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/Debug.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
| 23 | #include "llvm/Target/TargetRegisterInfo.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "hexagon_cfg" |
| 28 | |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 29 | namespace llvm { |
Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 30 | FunctionPass *createHexagonCFGOptimizer(); |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 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: |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 40 | void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 41 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 42 | public: |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 43 | static char ID; |
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 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 48 | const char *getPassName() const override { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 49 | return "Hexagon CFG Optimizer"; |
| 50 | } |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 51 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 52 | MachineFunctionProperties getRequiredProperties() const override { |
| 53 | return MachineFunctionProperties().set( |
| 54 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 55 | } |
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 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 71 | void HexagonCFGOptimizer::InvertAndChangeJumpTarget( |
| 72 | MachineInstr &MI, MachineBasicBlock *NewTarget) { |
Eric Christopher | 5c3376a | 2015-02-02 18:46:27 +0000 | [diff] [blame] | 73 | const TargetInstrInfo *TII = |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 74 | MI.getParent()->getParent()->getSubtarget().getInstrInfo(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 75 | int NewOpcode = 0; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 76 | switch (MI.getOpcode()) { |
Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 77 | case Hexagon::J2_jumpt: |
| 78 | NewOpcode = Hexagon::J2_jumpf; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 79 | break; |
| 80 | |
Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 81 | case Hexagon::J2_jumpf: |
| 82 | NewOpcode = Hexagon::J2_jumpt; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 83 | break; |
| 84 | |
Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 85 | case Hexagon::J2_jumptnewpt: |
| 86 | NewOpcode = Hexagon::J2_jumpfnewpt; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 87 | break; |
| 88 | |
Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 89 | case Hexagon::J2_jumpfnewpt: |
| 90 | NewOpcode = Hexagon::J2_jumptnewpt; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 91 | break; |
| 92 | |
| 93 | default: |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 94 | llvm_unreachable("Cannot handle this case"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 97 | MI.setDesc(TII->get(NewOpcode)); |
| 98 | MI.getOperand(1).setMBB(NewTarget); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) { |
Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 103 | if (skipFunction(*Fn.getFunction())) |
| 104 | return false; |
| 105 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 106 | // Loop over all of the basic blocks. |
| 107 | for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end(); |
| 108 | MBBb != MBBe; ++MBBb) { |
Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 109 | MachineBasicBlock *MBB = &*MBBb; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 110 | |
| 111 | // Traverse the basic block. |
| 112 | MachineBasicBlock::iterator MII = MBB->getFirstTerminator(); |
| 113 | if (MII != MBB->end()) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 114 | MachineInstr &MI = *MII; |
| 115 | int Opc = MI.getOpcode(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 116 | if (IsConditionalBranch(Opc)) { |
| 117 | |
| 118 | // |
| 119 | // (Case 1) Transform the code if the following condition occurs: |
| 120 | // BB1: if (p0) jump BB3 |
| 121 | // ...falls-through to BB2 ... |
| 122 | // BB2: jump BB4 |
| 123 | // ...next block in layout is BB3... |
| 124 | // BB3: ... |
| 125 | // |
| 126 | // Transform this to: |
| 127 | // BB1: if (!p0) jump BB4 |
| 128 | // Remove BB2 |
| 129 | // BB3: ... |
| 130 | // |
| 131 | // (Case 2) A variation occurs when BB3 contains a JMP to BB4: |
| 132 | // BB1: if (p0) jump BB3 |
| 133 | // ...falls-through to BB2 ... |
| 134 | // BB2: jump BB4 |
| 135 | // ...other basic blocks ... |
| 136 | // BB4: |
| 137 | // ...not a fall-thru |
| 138 | // BB3: ... |
| 139 | // jump BB4 |
| 140 | // |
| 141 | // Transform this to: |
| 142 | // BB1: if (!p0) jump BB4 |
| 143 | // Remove BB2 |
| 144 | // BB3: ... |
| 145 | // BB4: ... |
| 146 | // |
| 147 | unsigned NumSuccs = MBB->succ_size(); |
| 148 | MachineBasicBlock::succ_iterator SI = MBB->succ_begin(); |
| 149 | MachineBasicBlock* FirstSucc = *SI; |
| 150 | MachineBasicBlock* SecondSucc = *(++SI); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 151 | MachineBasicBlock* LayoutSucc = nullptr; |
| 152 | MachineBasicBlock* JumpAroundTarget = nullptr; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 153 | |
| 154 | if (MBB->isLayoutSuccessor(FirstSucc)) { |
| 155 | LayoutSucc = FirstSucc; |
| 156 | JumpAroundTarget = SecondSucc; |
| 157 | } else if (MBB->isLayoutSuccessor(SecondSucc)) { |
| 158 | LayoutSucc = SecondSucc; |
| 159 | JumpAroundTarget = FirstSucc; |
| 160 | } else { |
| 161 | // Odd case...cannot handle. |
| 162 | } |
| 163 | |
| 164 | // The target of the unconditional branch must be JumpAroundTarget. |
| 165 | // TODO: If not, we should not invert the unconditional branch. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 166 | MachineBasicBlock* CondBranchTarget = nullptr; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 167 | if (MI.getOpcode() == Hexagon::J2_jumpt || |
| 168 | MI.getOpcode() == Hexagon::J2_jumpf) { |
| 169 | CondBranchTarget = MI.getOperand(1).getMBB(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) { |
| 177 | |
| 178 | // Ensure that BB2 has one instruction -- an unconditional jump. |
| 179 | if ((LayoutSucc->size() == 1) && |
| 180 | IsUnconditionalJump(LayoutSucc->front().getOpcode())) { |
Krzysztof Parzyszek | 8975743 | 2016-05-05 22:00:44 +0000 | [diff] [blame] | 181 | assert(JumpAroundTarget && "jump target is needed to process second basic block"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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; |
| 191 | |
| 192 | if (case1 || case2) { |
| 193 | InvertAndChangeJumpTarget(MI, UncondTarget); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 194 | MBB->replaceSuccessor(JumpAroundTarget, UncondTarget); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 195 | |
| 196 | // Remove the unconditional branch in LayoutSucc. |
| 197 | LayoutSucc->erase(LayoutSucc->begin()); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 198 | LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 | // |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 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); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | return true; |
| 232 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 233 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 234 | |
| 235 | |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | // Public Constructor Functions |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | |
Chandler Carruth | d474144 | 2016-06-03 10:13:29 +0000 | [diff] [blame] | 240 | INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer", |
| 241 | false, false) |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 242 | |
Eric Christopher | 5c3376a | 2015-02-02 18:46:27 +0000 | [diff] [blame] | 243 | FunctionPass *llvm::createHexagonCFGOptimizer() { |
| 244 | return new HexagonCFGOptimizer(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 245 | } |