blob: ffcf22216d4f1d98adcb265f6259ec0575b183c3 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- MSP430BranchSelector.cpp - Emit long conditional branches ---------===//
Anton Korobeynikovce52fd52010-01-15 21:19:05 +00002//
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 Greif21fed662010-08-23 20:30:51 +000013// positions pass, and a branch pseudo op to machine branch opcode pass. This
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000014// pass should be run last, just before the assembly printer.
15//
16//===----------------------------------------------------------------------===//
17
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000018#include "MSP430.h"
19#include "MSP430InstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000020#include "MSP430Subtarget.h"
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000024#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000026using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "msp430-branch-select"
29
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000030STATISTIC(NumExpanded, "Number of branches expanded to long format");
31
32namespace {
33 struct MSP430BSel : public MachineFunctionPass {
34 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000035 MSP430BSel() : MachineFunctionPass(ID) {}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000036
37 /// BlockSizes - The sizes of the basic blocks in the function.
38 std::vector<unsigned> BlockSizes;
39
Craig Topper6f9e59e2014-04-29 07:58:09 +000040 bool runOnMachineFunction(MachineFunction &Fn) override;
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000041
Craig Topper6f9e59e2014-04-29 07:58:09 +000042 const char *getPassName() const override {
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000043 return "MSP430 Branch Selector";
44 }
45 };
46 char MSP430BSel::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000047}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000048
49/// createMSP430BranchSelectionPass - returns an instance of the Branch
50/// Selection Pass
51///
52FunctionPass *llvm::createMSP430BranchSelectionPass() {
53 return new MSP430BSel();
54}
55
56bool MSP430BSel::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopherfc6de422014-08-05 02:39:49 +000057 const MSP430InstrInfo *TII =
58 static_cast<const MSP430InstrInfo *>(Fn.getSubtarget().getInstrInfo());
Anton Korobeynikovce52fd52010-01-15 21:19:05 +000059 // Give the blocks of the function a dense, in-order, numbering.
60 Fn.RenumberBlocks();
61 BlockSizes.resize(Fn.getNumBlockIDs());
62
63 // Measure each MBB and compute a size for the entire function.
64 unsigned FuncSize = 0;
65 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
66 ++MFI) {
67 MachineBasicBlock *MBB = MFI;
68
69 unsigned BlockSize = 0;
70 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
71 MBBI != EE; ++MBBI)
72 BlockSize += TII->GetInstSizeInBytes(MBBI);
73
74 BlockSizes[MBB->getNumber()] = BlockSize;
75 FuncSize += BlockSize;
76 }
77
78 // If the entire function is smaller than the displacement of a branch field,
79 // we know we don't need to shrink any branches in this function. This is a
80 // common case.
81 if (FuncSize < (1 << 9)) {
82 BlockSizes.clear();
83 return false;
84 }
85
86 // For each conditional branch, if the offset to its destination is larger
87 // than the offset field allows, transform it into a long branch sequence
88 // like this:
89 // short branch:
90 // bCC MBB
91 // long branch:
92 // b!CC $PC+6
93 // b MBB
94 //
95 bool MadeChange = true;
96 bool EverMadeChange = false;
97 while (MadeChange) {
98 // Iteratively expand branches until we reach a fixed point.
99 MadeChange = false;
100
101 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
102 ++MFI) {
103 MachineBasicBlock &MBB = *MFI;
104 unsigned MBBStartOffset = 0;
105 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
106 I != E; ++I) {
107 if ((I->getOpcode() != MSP430::JCC || I->getOperand(0).isImm()) &&
108 I->getOpcode() != MSP430::JMP) {
109 MBBStartOffset += TII->GetInstSizeInBytes(I);
110 continue;
111 }
112
113 // Determine the offset from the current branch to the destination
114 // block.
115 MachineBasicBlock *Dest = I->getOperand(0).getMBB();
116
117 int BranchSize;
118 if (Dest->getNumber() <= MBB.getNumber()) {
119 // If this is a backwards branch, the delta is the offset from the
120 // start of this block to this branch, plus the sizes of all blocks
121 // from this block to the dest.
122 BranchSize = MBBStartOffset;
123
124 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
125 BranchSize += BlockSizes[i];
126 } else {
127 // Otherwise, add the size of the blocks between this block and the
128 // dest to the number of bytes left in this block.
129 BranchSize = -MBBStartOffset;
130
131 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
132 BranchSize += BlockSizes[i];
133 }
134
135 // If this branch is in range, ignore it.
136 if (isInt<10>(BranchSize)) {
137 MBBStartOffset += 2;
138 continue;
139 }
140
141 // Otherwise, we have to expand it to a long branch.
142 unsigned NewSize;
143 MachineInstr *OldBranch = I;
144 DebugLoc dl = OldBranch->getDebugLoc();
145
146 if (I->getOpcode() == MSP430::JMP) {
147 NewSize = 4;
148 } else {
149 // The BCC operands are:
150 // 0. MSP430 branch predicate
151 // 1. Target MBB
152 SmallVector<MachineOperand, 1> Cond;
153 Cond.push_back(I->getOperand(1));
154
155 // Jump over the uncond branch inst (i.e. $+6) on opposite condition.
156 TII->ReverseBranchCondition(Cond);
157 BuildMI(MBB, I, dl, TII->get(MSP430::JCC))
158 .addImm(4).addOperand(Cond[0]);
159
160 NewSize = 6;
161 }
162 // Uncond branch to the real destination.
Anton Korobeynikov319d71f2010-05-01 12:28:21 +0000163 I = BuildMI(MBB, I, dl, TII->get(MSP430::Bi)).addMBB(Dest);
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000164
165 // Remove the old branch from the function.
166 OldBranch->eraseFromParent();
167
168 // Remember that this instruction is NewSize bytes, increase the size of the
169 // block by NewSize-2, remember to iterate.
170 BlockSizes[MBB.getNumber()] += NewSize-2;
171 MBBStartOffset += NewSize;
172
173 ++NumExpanded;
174 MadeChange = true;
175 }
176 }
177 EverMadeChange |= MadeChange;
178 }
179
180 BlockSizes.clear();
181 return true;
182}