xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430BranchSelector.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- MSP430BranchSelector.cpp - Emit long conditional branches ---------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains a pass that scans a machine function to determine which
10*0b57cec5SDimitry Andric // conditional branches need more than 10 bits of displacement to reach their
11*0b57cec5SDimitry Andric // target basic block.  It does this in two passes; a calculation of basic block
12*0b57cec5SDimitry Andric // positions pass, and a branch pseudo op to machine branch opcode pass.  This
13*0b57cec5SDimitry Andric // pass should be run last, just before the assembly printer.
14*0b57cec5SDimitry Andric //
15*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric #include "MSP430.h"
18*0b57cec5SDimitry Andric #include "MSP430InstrInfo.h"
19*0b57cec5SDimitry Andric #include "MSP430Subtarget.h"
20*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
23*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
24*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
25*0b57cec5SDimitry Andric using namespace llvm;
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric #define DEBUG_TYPE "msp430-branch-select"
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric static cl::opt<bool>
30*0b57cec5SDimitry Andric     BranchSelectEnabled("msp430-branch-select", cl::Hidden, cl::init(true),
31*0b57cec5SDimitry Andric                         cl::desc("Expand out of range branches"));
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric STATISTIC(NumSplit, "Number of machine basic blocks split");
34*0b57cec5SDimitry Andric STATISTIC(NumExpanded, "Number of branches expanded to long format");
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric namespace {
37*0b57cec5SDimitry Andric class MSP430BSel : public MachineFunctionPass {
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric   typedef SmallVector<int, 16> OffsetVector;
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric   MachineFunction *MF;
42*0b57cec5SDimitry Andric   const MSP430InstrInfo *TII;
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   unsigned measureFunction(OffsetVector &BlockOffsets,
45*0b57cec5SDimitry Andric                            MachineBasicBlock *FromBB = nullptr);
46*0b57cec5SDimitry Andric   bool expandBranches(OffsetVector &BlockOffsets);
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric public:
49*0b57cec5SDimitry Andric   static char ID;
50*0b57cec5SDimitry Andric   MSP430BSel() : MachineFunctionPass(ID) {}
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
55*0b57cec5SDimitry Andric     return MachineFunctionProperties().set(
56*0b57cec5SDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
57*0b57cec5SDimitry Andric   }
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric   StringRef getPassName() const override { return "MSP430 Branch Selector"; }
60*0b57cec5SDimitry Andric };
61*0b57cec5SDimitry Andric char MSP430BSel::ID = 0;
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric static bool isInRage(int DistanceInBytes) {
65*0b57cec5SDimitry Andric   // According to CC430 Family User's Guide, Section 4.5.1.3, branch
66*0b57cec5SDimitry Andric   // instructions have the signed 10-bit word offset field, so first we need to
67*0b57cec5SDimitry Andric   // convert the distance from bytes to words, then check if it fits in 10-bit
68*0b57cec5SDimitry Andric   // signed integer.
69*0b57cec5SDimitry Andric   const int WordSize = 2;
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   assert((DistanceInBytes % WordSize == 0) &&
72*0b57cec5SDimitry Andric          "Branch offset should be word aligned!");
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric   int Words = DistanceInBytes / WordSize;
75*0b57cec5SDimitry Andric   return isInt<10>(Words);
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric /// Measure each basic block, fill the BlockOffsets, and return the size of
79*0b57cec5SDimitry Andric /// the function, starting with BB
80*0b57cec5SDimitry Andric unsigned MSP430BSel::measureFunction(OffsetVector &BlockOffsets,
81*0b57cec5SDimitry Andric                                      MachineBasicBlock *FromBB) {
82*0b57cec5SDimitry Andric   // Give the blocks of the function a dense, in-order, numbering.
83*0b57cec5SDimitry Andric   MF->RenumberBlocks(FromBB);
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric   MachineFunction::iterator Begin;
86*0b57cec5SDimitry Andric   if (FromBB == nullptr) {
87*0b57cec5SDimitry Andric     Begin = MF->begin();
88*0b57cec5SDimitry Andric   } else {
89*0b57cec5SDimitry Andric     Begin = FromBB->getIterator();
90*0b57cec5SDimitry Andric   }
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   BlockOffsets.resize(MF->getNumBlockIDs());
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric   unsigned TotalSize = BlockOffsets[Begin->getNumber()];
95*0b57cec5SDimitry Andric   for (auto &MBB : make_range(Begin, MF->end())) {
96*0b57cec5SDimitry Andric     BlockOffsets[MBB.getNumber()] = TotalSize;
97*0b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
98*0b57cec5SDimitry Andric       TotalSize += TII->getInstSizeInBytes(MI);
99*0b57cec5SDimitry Andric     }
100*0b57cec5SDimitry Andric   }
101*0b57cec5SDimitry Andric   return TotalSize;
102*0b57cec5SDimitry Andric }
103*0b57cec5SDimitry Andric 
104*0b57cec5SDimitry Andric /// Do expand branches and split the basic blocks if necessary.
105*0b57cec5SDimitry Andric /// Returns true if made any change.
106*0b57cec5SDimitry Andric bool MSP430BSel::expandBranches(OffsetVector &BlockOffsets) {
107*0b57cec5SDimitry Andric   // For each conditional branch, if the offset to its destination is larger
108*0b57cec5SDimitry Andric   // than the offset field allows, transform it into a long branch sequence
109*0b57cec5SDimitry Andric   // like this:
110*0b57cec5SDimitry Andric   //   short branch:
111*0b57cec5SDimitry Andric   //     bCC MBB
112*0b57cec5SDimitry Andric   //   long branch:
113*0b57cec5SDimitry Andric   //     b!CC $PC+6
114*0b57cec5SDimitry Andric   //     b MBB
115*0b57cec5SDimitry Andric   //
116*0b57cec5SDimitry Andric   bool MadeChange = false;
117*0b57cec5SDimitry Andric   for (auto MBB = MF->begin(), E = MF->end(); MBB != E; ++MBB) {
118*0b57cec5SDimitry Andric     unsigned MBBStartOffset = 0;
119*0b57cec5SDimitry Andric     for (auto MI = MBB->begin(), EE = MBB->end(); MI != EE; ++MI) {
120*0b57cec5SDimitry Andric       MBBStartOffset += TII->getInstSizeInBytes(*MI);
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric       // If this instruction is not a short branch then skip it.
123*0b57cec5SDimitry Andric       if (MI->getOpcode() != MSP430::JCC && MI->getOpcode() != MSP430::JMP) {
124*0b57cec5SDimitry Andric         continue;
125*0b57cec5SDimitry Andric       }
126*0b57cec5SDimitry Andric 
127*0b57cec5SDimitry Andric       MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
128*0b57cec5SDimitry Andric       // Determine the distance from the current branch to the destination
129*0b57cec5SDimitry Andric       // block. MBBStartOffset already includes the size of the current branch
130*0b57cec5SDimitry Andric       // instruction.
131*0b57cec5SDimitry Andric       int BlockDistance =
132*0b57cec5SDimitry Andric           BlockOffsets[DestBB->getNumber()] - BlockOffsets[MBB->getNumber()];
133*0b57cec5SDimitry Andric       int BranchDistance = BlockDistance - MBBStartOffset;
134*0b57cec5SDimitry Andric 
135*0b57cec5SDimitry Andric       // If this branch is in range, ignore it.
136*0b57cec5SDimitry Andric       if (isInRage(BranchDistance)) {
137*0b57cec5SDimitry Andric         continue;
138*0b57cec5SDimitry Andric       }
139*0b57cec5SDimitry Andric 
140*0b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "  Found a branch that needs expanding, "
141*0b57cec5SDimitry Andric                         << printMBBReference(*DestBB) << ", Distance "
142*0b57cec5SDimitry Andric                         << BranchDistance << "\n");
143*0b57cec5SDimitry Andric 
144*0b57cec5SDimitry Andric       // If JCC is not the last instruction we need to split the MBB.
145*0b57cec5SDimitry Andric       if (MI->getOpcode() == MSP430::JCC && std::next(MI) != EE) {
146*0b57cec5SDimitry Andric 
147*0b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "  Found a basic block that needs to be split, "
148*0b57cec5SDimitry Andric                           << printMBBReference(*MBB) << "\n");
149*0b57cec5SDimitry Andric 
150*0b57cec5SDimitry Andric         // Create a new basic block.
151*0b57cec5SDimitry Andric         MachineBasicBlock *NewBB =
152*0b57cec5SDimitry Andric             MF->CreateMachineBasicBlock(MBB->getBasicBlock());
153*0b57cec5SDimitry Andric         MF->insert(std::next(MBB), NewBB);
154*0b57cec5SDimitry Andric 
155*0b57cec5SDimitry Andric         // Splice the instructions following MI over to the NewBB.
156*0b57cec5SDimitry Andric         NewBB->splice(NewBB->end(), &*MBB, std::next(MI), MBB->end());
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric         // Update the successor lists.
159*0b57cec5SDimitry Andric         for (MachineBasicBlock *Succ : MBB->successors()) {
160*0b57cec5SDimitry Andric           if (Succ == DestBB) {
161*0b57cec5SDimitry Andric             continue;
162*0b57cec5SDimitry Andric           }
163*0b57cec5SDimitry Andric           MBB->replaceSuccessor(Succ, NewBB);
164*0b57cec5SDimitry Andric           NewBB->addSuccessor(Succ);
165*0b57cec5SDimitry Andric         }
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric         // We introduced a new MBB so all following blocks should be numbered
168*0b57cec5SDimitry Andric         // and measured again.
169*0b57cec5SDimitry Andric         measureFunction(BlockOffsets, &*MBB);
170*0b57cec5SDimitry Andric 
171*0b57cec5SDimitry Andric         ++NumSplit;
172*0b57cec5SDimitry Andric 
173*0b57cec5SDimitry Andric         // It may be not necessary to start all over at this point, but it's
174*0b57cec5SDimitry Andric         // safer do this anyway.
175*0b57cec5SDimitry Andric         return true;
176*0b57cec5SDimitry Andric       }
177*0b57cec5SDimitry Andric 
178*0b57cec5SDimitry Andric       MachineInstr &OldBranch = *MI;
179*0b57cec5SDimitry Andric       DebugLoc dl = OldBranch.getDebugLoc();
180*0b57cec5SDimitry Andric       int InstrSizeDiff = -TII->getInstSizeInBytes(OldBranch);
181*0b57cec5SDimitry Andric 
182*0b57cec5SDimitry Andric       if (MI->getOpcode() == MSP430::JCC) {
183*0b57cec5SDimitry Andric         MachineBasicBlock *NextMBB = &*std::next(MBB);
184*0b57cec5SDimitry Andric         assert(MBB->isSuccessor(NextMBB) &&
185*0b57cec5SDimitry Andric                "This block must have a layout successor!");
186*0b57cec5SDimitry Andric 
187*0b57cec5SDimitry Andric         // The BCC operands are:
188*0b57cec5SDimitry Andric         // 0. Target MBB
189*0b57cec5SDimitry Andric         // 1. MSP430 branch predicate
190*0b57cec5SDimitry Andric         SmallVector<MachineOperand, 1> Cond;
191*0b57cec5SDimitry Andric         Cond.push_back(MI->getOperand(1));
192*0b57cec5SDimitry Andric 
193*0b57cec5SDimitry Andric         // Jump over the long branch on the opposite condition
194*0b57cec5SDimitry Andric         TII->reverseBranchCondition(Cond);
195*0b57cec5SDimitry Andric         MI = BuildMI(*MBB, MI, dl, TII->get(MSP430::JCC))
196*0b57cec5SDimitry Andric                  .addMBB(NextMBB)
197*0b57cec5SDimitry Andric                  .add(Cond[0]);
198*0b57cec5SDimitry Andric         InstrSizeDiff += TII->getInstSizeInBytes(*MI);
199*0b57cec5SDimitry Andric         ++MI;
200*0b57cec5SDimitry Andric       }
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric       // Unconditional branch to the real destination.
203*0b57cec5SDimitry Andric       MI = BuildMI(*MBB, MI, dl, TII->get(MSP430::Bi)).addMBB(DestBB);
204*0b57cec5SDimitry Andric       InstrSizeDiff += TII->getInstSizeInBytes(*MI);
205*0b57cec5SDimitry Andric 
206*0b57cec5SDimitry Andric       // Remove the old branch from the function.
207*0b57cec5SDimitry Andric       OldBranch.eraseFromParent();
208*0b57cec5SDimitry Andric 
209*0b57cec5SDimitry Andric       // The size of a new instruction is different from the old one, so we need
210*0b57cec5SDimitry Andric       // to correct all block offsets.
211*0b57cec5SDimitry Andric       for (int i = MBB->getNumber() + 1, e = BlockOffsets.size(); i < e; ++i) {
212*0b57cec5SDimitry Andric         BlockOffsets[i] += InstrSizeDiff;
213*0b57cec5SDimitry Andric       }
214*0b57cec5SDimitry Andric       MBBStartOffset += InstrSizeDiff;
215*0b57cec5SDimitry Andric 
216*0b57cec5SDimitry Andric       ++NumExpanded;
217*0b57cec5SDimitry Andric       MadeChange = true;
218*0b57cec5SDimitry Andric     }
219*0b57cec5SDimitry Andric   }
220*0b57cec5SDimitry Andric   return MadeChange;
221*0b57cec5SDimitry Andric }
222*0b57cec5SDimitry Andric 
223*0b57cec5SDimitry Andric bool MSP430BSel::runOnMachineFunction(MachineFunction &mf) {
224*0b57cec5SDimitry Andric   MF = &mf;
225*0b57cec5SDimitry Andric   TII = static_cast<const MSP430InstrInfo *>(MF->getSubtarget().getInstrInfo());
226*0b57cec5SDimitry Andric 
227*0b57cec5SDimitry Andric   // If the pass is disabled, just bail early.
228*0b57cec5SDimitry Andric   if (!BranchSelectEnabled)
229*0b57cec5SDimitry Andric     return false;
230*0b57cec5SDimitry Andric 
231*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\n********** " << getPassName() << " **********\n");
232*0b57cec5SDimitry Andric 
233*0b57cec5SDimitry Andric   // BlockOffsets - Contains the distance from the beginning of the function to
234*0b57cec5SDimitry Andric   // the beginning of each basic block.
235*0b57cec5SDimitry Andric   OffsetVector BlockOffsets;
236*0b57cec5SDimitry Andric 
237*0b57cec5SDimitry Andric   unsigned FunctionSize = measureFunction(BlockOffsets);
238*0b57cec5SDimitry Andric   // If the entire function is smaller than the displacement of a branch field,
239*0b57cec5SDimitry Andric   // we know we don't need to expand any branches in this
240*0b57cec5SDimitry Andric   // function. This is a common case.
241*0b57cec5SDimitry Andric   if (isInRage(FunctionSize)) {
242*0b57cec5SDimitry Andric     return false;
243*0b57cec5SDimitry Andric   }
244*0b57cec5SDimitry Andric 
245*0b57cec5SDimitry Andric   // Iteratively expand branches until we reach a fixed point.
246*0b57cec5SDimitry Andric   bool MadeChange = false;
247*0b57cec5SDimitry Andric   while (expandBranches(BlockOffsets))
248*0b57cec5SDimitry Andric     MadeChange = true;
249*0b57cec5SDimitry Andric 
250*0b57cec5SDimitry Andric   return MadeChange;
251*0b57cec5SDimitry Andric }
252*0b57cec5SDimitry Andric 
253*0b57cec5SDimitry Andric /// Returns an instance of the Branch Selection Pass
254*0b57cec5SDimitry Andric FunctionPass *llvm::createMSP430BranchSelectionPass() {
255*0b57cec5SDimitry Andric   return new MSP430BSel();
256*0b57cec5SDimitry Andric }
257