1 //===-- HexagonCFGOptimizer.cpp - CFG optimizations -----------------------===// 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 9 #include "Hexagon.h" 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" 24 25 using namespace llvm; 26 27 #define DEBUG_TYPE "hexagon_cfg" 28 29 namespace llvm { 30 FunctionPass *createHexagonCFGOptimizer(); 31 void initializeHexagonCFGOptimizerPass(PassRegistry&); 32 } 33 34 35 namespace { 36 37 class HexagonCFGOptimizer : public MachineFunctionPass { 38 39 private: 40 void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *); 41 bool isOnFallThroughPath(MachineBasicBlock *MBB); 42 43 public: 44 static char ID; 45 HexagonCFGOptimizer() : MachineFunctionPass(ID) { 46 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry()); 47 } 48 49 StringRef getPassName() const override { return "Hexagon CFG Optimizer"; } 50 bool runOnMachineFunction(MachineFunction &Fn) override; 51 MachineFunctionProperties getRequiredProperties() const override { 52 return MachineFunctionProperties().set( 53 MachineFunctionProperties::Property::NoVRegs); 54 } 55 }; 56 57 58 char HexagonCFGOptimizer::ID = 0; 59 60 static bool IsConditionalBranch(int Opc) { 61 switch (Opc) { 62 case Hexagon::J2_jumpt: 63 case Hexagon::J2_jumptpt: 64 case Hexagon::J2_jumpf: 65 case Hexagon::J2_jumpfpt: 66 case Hexagon::J2_jumptnew: 67 case Hexagon::J2_jumpfnew: 68 case Hexagon::J2_jumptnewpt: 69 case Hexagon::J2_jumpfnewpt: 70 return true; 71 } 72 return false; 73 } 74 75 76 static bool IsUnconditionalJump(int Opc) { 77 return (Opc == Hexagon::J2_jump); 78 } 79 80 void HexagonCFGOptimizer::InvertAndChangeJumpTarget( 81 MachineInstr &MI, MachineBasicBlock *NewTarget) { 82 const TargetInstrInfo *TII = 83 MI.getParent()->getParent()->getSubtarget().getInstrInfo(); 84 int NewOpcode = 0; 85 switch (MI.getOpcode()) { 86 case Hexagon::J2_jumpt: 87 NewOpcode = Hexagon::J2_jumpf; 88 break; 89 90 case Hexagon::J2_jumpf: 91 NewOpcode = Hexagon::J2_jumpt; 92 break; 93 94 case Hexagon::J2_jumptnewpt: 95 NewOpcode = Hexagon::J2_jumpfnewpt; 96 break; 97 98 case Hexagon::J2_jumpfnewpt: 99 NewOpcode = Hexagon::J2_jumptnewpt; 100 break; 101 102 default: 103 llvm_unreachable("Cannot handle this case"); 104 } 105 106 MI.setDesc(TII->get(NewOpcode)); 107 MI.getOperand(1).setMBB(NewTarget); 108 } 109 110 bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) { 111 if (MBB->canFallThrough()) 112 return true; 113 for (MachineBasicBlock *PB : MBB->predecessors()) 114 if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough()) 115 return true; 116 return false; 117 } 118 119 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) { 120 if (skipFunction(*Fn.getFunction())) 121 return false; 122 123 // Loop over all of the basic blocks. 124 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end(); 125 MBBb != MBBe; ++MBBb) { 126 MachineBasicBlock *MBB = &*MBBb; 127 128 // Traverse the basic block. 129 MachineBasicBlock::iterator MII = MBB->getFirstTerminator(); 130 if (MII != MBB->end()) { 131 MachineInstr &MI = *MII; 132 int Opc = MI.getOpcode(); 133 if (IsConditionalBranch(Opc)) { 134 135 // 136 // (Case 1) Transform the code if the following condition occurs: 137 // BB1: if (p0) jump BB3 138 // ...falls-through to BB2 ... 139 // BB2: jump BB4 140 // ...next block in layout is BB3... 141 // BB3: ... 142 // 143 // Transform this to: 144 // BB1: if (!p0) jump BB4 145 // Remove BB2 146 // BB3: ... 147 // 148 // (Case 2) A variation occurs when BB3 contains a JMP to BB4: 149 // BB1: if (p0) jump BB3 150 // ...falls-through to BB2 ... 151 // BB2: jump BB4 152 // ...other basic blocks ... 153 // BB4: 154 // ...not a fall-thru 155 // BB3: ... 156 // jump BB4 157 // 158 // Transform this to: 159 // BB1: if (!p0) jump BB4 160 // Remove BB2 161 // BB3: ... 162 // BB4: ... 163 // 164 unsigned NumSuccs = MBB->succ_size(); 165 MachineBasicBlock::succ_iterator SI = MBB->succ_begin(); 166 MachineBasicBlock* FirstSucc = *SI; 167 MachineBasicBlock* SecondSucc = *(++SI); 168 MachineBasicBlock* LayoutSucc = nullptr; 169 MachineBasicBlock* JumpAroundTarget = nullptr; 170 171 if (MBB->isLayoutSuccessor(FirstSucc)) { 172 LayoutSucc = FirstSucc; 173 JumpAroundTarget = SecondSucc; 174 } else if (MBB->isLayoutSuccessor(SecondSucc)) { 175 LayoutSucc = SecondSucc; 176 JumpAroundTarget = FirstSucc; 177 } else { 178 // Odd case...cannot handle. 179 } 180 181 // The target of the unconditional branch must be JumpAroundTarget. 182 // TODO: If not, we should not invert the unconditional branch. 183 MachineBasicBlock* CondBranchTarget = nullptr; 184 if (MI.getOpcode() == Hexagon::J2_jumpt || 185 MI.getOpcode() == Hexagon::J2_jumpf) { 186 CondBranchTarget = MI.getOperand(1).getMBB(); 187 } 188 189 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) { 190 continue; 191 } 192 193 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) { 194 // Ensure that BB2 has one instruction -- an unconditional jump. 195 if ((LayoutSucc->size() == 1) && 196 IsUnconditionalJump(LayoutSucc->front().getOpcode())) { 197 assert(JumpAroundTarget && "jump target is needed to process second basic block"); 198 MachineBasicBlock* UncondTarget = 199 LayoutSucc->front().getOperand(0).getMBB(); 200 // Check if the layout successor of BB2 is BB3. 201 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget); 202 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) && 203 JumpAroundTarget->size() >= 1 && 204 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) && 205 JumpAroundTarget->pred_size() == 1 && 206 JumpAroundTarget->succ_size() == 1; 207 208 if (case1 || case2) { 209 InvertAndChangeJumpTarget(MI, UncondTarget); 210 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget); 211 212 // Remove the unconditional branch in LayoutSucc. 213 LayoutSucc->erase(LayoutSucc->begin()); 214 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget); 215 216 // This code performs the conversion for case 2, which moves 217 // the block to the fall-thru case (BB3 in the code above). 218 if (case2 && !case1) { 219 JumpAroundTarget->moveAfter(LayoutSucc); 220 // only move a block if it doesn't have a fall-thru. otherwise 221 // the CFG will be incorrect. 222 if (!isOnFallThroughPath(UncondTarget)) 223 UncondTarget->moveAfter(JumpAroundTarget); 224 } 225 226 // 227 // Correct live-in information. Is used by post-RA scheduler 228 // The live-in to LayoutSucc is now all values live-in to 229 // JumpAroundTarget. 230 // 231 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn( 232 LayoutSucc->livein_begin(), LayoutSucc->livein_end()); 233 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn( 234 JumpAroundTarget->livein_begin(), 235 JumpAroundTarget->livein_end()); 236 for (const auto &OrigLI : OrigLiveIn) 237 LayoutSucc->removeLiveIn(OrigLI.PhysReg); 238 for (const auto &NewLI : NewLiveIn) 239 LayoutSucc->addLiveIn(NewLI); 240 } 241 } 242 } 243 } 244 } 245 } 246 return true; 247 } 248 } 249 250 251 //===----------------------------------------------------------------------===// 252 // Public Constructor Functions 253 //===----------------------------------------------------------------------===// 254 255 INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer", 256 false, false) 257 258 FunctionPass *llvm::createHexagonCFGOptimizer() { 259 return new HexagonCFGOptimizer(); 260 } 261