Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- MSP430BranchSelector.cpp - Emit long conditional branches ---------===// |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a pass that scans a machine function to determine which |
| 11 | // conditional branches need more than 10 bits of displacement to reach their |
| 12 | // target basic block. It does this in two passes; a calculation of basic block |
Gabor Greif | 21fed66 | 2010-08-23 20:30:51 +0000 | [diff] [blame] | 13 | // positions pass, and a branch pseudo op to machine branch opcode pass. This |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 14 | // pass should be run last, just before the assembly printer. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 18 | #include "MSP430.h" |
| 19 | #include "MSP430InstrInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 20 | #include "MSP430Subtarget.h" |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "msp430-branch-select" |
| 29 | |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 30 | STATISTIC(NumExpanded, "Number of branches expanded to long format"); |
| 31 | |
| 32 | namespace { |
| 33 | struct MSP430BSel : public MachineFunctionPass { |
| 34 | static char ID; |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 35 | MSP430BSel() : MachineFunctionPass(ID) {} |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 36 | |
| 37 | /// BlockSizes - The sizes of the basic blocks in the function. |
| 38 | std::vector<unsigned> BlockSizes; |
| 39 | |
Craig Topper | 6f9e59e | 2014-04-29 07:58:09 +0000 | [diff] [blame] | 40 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 41 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 42 | MachineFunctionProperties getRequiredProperties() const override { |
| 43 | return MachineFunctionProperties().set( |
| 44 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 45 | } |
| 46 | |
Craig Topper | 6f9e59e | 2014-04-29 07:58:09 +0000 | [diff] [blame] | 47 | const char *getPassName() const override { |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 48 | return "MSP430 Branch Selector"; |
| 49 | } |
| 50 | }; |
| 51 | char MSP430BSel::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 52 | } |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 53 | |
| 54 | /// createMSP430BranchSelectionPass - returns an instance of the Branch |
| 55 | /// Selection Pass |
| 56 | /// |
| 57 | FunctionPass *llvm::createMSP430BranchSelectionPass() { |
| 58 | return new MSP430BSel(); |
| 59 | } |
| 60 | |
| 61 | bool MSP430BSel::runOnMachineFunction(MachineFunction &Fn) { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 62 | const MSP430InstrInfo *TII = |
| 63 | static_cast<const MSP430InstrInfo *>(Fn.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 64 | // Give the blocks of the function a dense, in-order, numbering. |
| 65 | Fn.RenumberBlocks(); |
| 66 | BlockSizes.resize(Fn.getNumBlockIDs()); |
| 67 | |
| 68 | // Measure each MBB and compute a size for the entire function. |
| 69 | unsigned FuncSize = 0; |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 70 | for (MachineBasicBlock &MBB : Fn) { |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 71 | unsigned BlockSize = 0; |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 72 | for (MachineInstr &MI : MBB) |
| 73 | BlockSize += TII->GetInstSizeInBytes(MI); |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 74 | |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 75 | BlockSizes[MBB.getNumber()] = BlockSize; |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 76 | FuncSize += BlockSize; |
| 77 | } |
| 78 | |
| 79 | // If the entire function is smaller than the displacement of a branch field, |
| 80 | // we know we don't need to shrink any branches in this function. This is a |
| 81 | // common case. |
| 82 | if (FuncSize < (1 << 9)) { |
| 83 | BlockSizes.clear(); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // For each conditional branch, if the offset to its destination is larger |
| 88 | // than the offset field allows, transform it into a long branch sequence |
| 89 | // like this: |
| 90 | // short branch: |
| 91 | // bCC MBB |
| 92 | // long branch: |
| 93 | // b!CC $PC+6 |
| 94 | // b MBB |
| 95 | // |
| 96 | bool MadeChange = true; |
| 97 | bool EverMadeChange = false; |
| 98 | while (MadeChange) { |
| 99 | // Iteratively expand branches until we reach a fixed point. |
| 100 | MadeChange = false; |
| 101 | |
| 102 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 103 | ++MFI) { |
| 104 | MachineBasicBlock &MBB = *MFI; |
| 105 | unsigned MBBStartOffset = 0; |
| 106 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 107 | I != E; ++I) { |
| 108 | if ((I->getOpcode() != MSP430::JCC || I->getOperand(0).isImm()) && |
| 109 | I->getOpcode() != MSP430::JMP) { |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 110 | MBBStartOffset += TII->GetInstSizeInBytes(*I); |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // Determine the offset from the current branch to the destination |
| 115 | // block. |
| 116 | MachineBasicBlock *Dest = I->getOperand(0).getMBB(); |
| 117 | |
| 118 | int BranchSize; |
| 119 | if (Dest->getNumber() <= MBB.getNumber()) { |
| 120 | // If this is a backwards branch, the delta is the offset from the |
| 121 | // start of this block to this branch, plus the sizes of all blocks |
| 122 | // from this block to the dest. |
| 123 | BranchSize = MBBStartOffset; |
| 124 | |
| 125 | for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i) |
| 126 | BranchSize += BlockSizes[i]; |
| 127 | } else { |
| 128 | // Otherwise, add the size of the blocks between this block and the |
| 129 | // dest to the number of bytes left in this block. |
| 130 | BranchSize = -MBBStartOffset; |
| 131 | |
| 132 | for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i) |
| 133 | BranchSize += BlockSizes[i]; |
| 134 | } |
| 135 | |
| 136 | // If this branch is in range, ignore it. |
| 137 | if (isInt<10>(BranchSize)) { |
| 138 | MBBStartOffset += 2; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | // Otherwise, we have to expand it to a long branch. |
| 143 | unsigned NewSize; |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 144 | MachineInstr &OldBranch = *I; |
| 145 | DebugLoc dl = OldBranch.getDebugLoc(); |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 146 | |
| 147 | if (I->getOpcode() == MSP430::JMP) { |
| 148 | NewSize = 4; |
| 149 | } else { |
| 150 | // The BCC operands are: |
| 151 | // 0. MSP430 branch predicate |
| 152 | // 1. Target MBB |
| 153 | SmallVector<MachineOperand, 1> Cond; |
| 154 | Cond.push_back(I->getOperand(1)); |
| 155 | |
| 156 | // Jump over the uncond branch inst (i.e. $+6) on opposite condition. |
| 157 | TII->ReverseBranchCondition(Cond); |
| 158 | BuildMI(MBB, I, dl, TII->get(MSP430::JCC)) |
| 159 | .addImm(4).addOperand(Cond[0]); |
| 160 | |
| 161 | NewSize = 6; |
| 162 | } |
| 163 | // Uncond branch to the real destination. |
Anton Korobeynikov | 319d71f | 2010-05-01 12:28:21 +0000 | [diff] [blame] | 164 | I = BuildMI(MBB, I, dl, TII->get(MSP430::Bi)).addMBB(Dest); |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 165 | |
| 166 | // Remove the old branch from the function. |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 167 | OldBranch.eraseFromParent(); |
Anton Korobeynikov | ce52fd5 | 2010-01-15 21:19:05 +0000 | [diff] [blame] | 168 | |
| 169 | // Remember that this instruction is NewSize bytes, increase the size of the |
| 170 | // block by NewSize-2, remember to iterate. |
| 171 | BlockSizes[MBB.getNumber()] += NewSize-2; |
| 172 | MBBStartOffset += NewSize; |
| 173 | |
| 174 | ++NumExpanded; |
| 175 | MadeChange = true; |
| 176 | } |
| 177 | } |
| 178 | EverMadeChange |= MadeChange; |
| 179 | } |
| 180 | |
| 181 | BlockSizes.clear(); |
| 182 | return true; |
| 183 | } |