Hanle i8 returns
[llvm/msp430.git] / lib / Target / PowerPC / PPCBranchSelector.cpp
blobb95a502d918712ba230657f00e91714ccfcc139a
1 //===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=//
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 16 bits of displacement to reach their
12 // target basic block. It does this in two passes; a calculation of basic block
13 // positions pass, and a branch psuedo op to machine branch opcode pass. This
14 // pass should be run last, just before the assembly printer.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "ppc-branch-select"
19 #include "PPC.h"
20 #include "PPCInstrBuilder.h"
21 #include "PPCInstrInfo.h"
22 #include "PPCPredicates.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/MathExtras.h"
28 using namespace llvm;
30 STATISTIC(NumExpanded, "Number of branches expanded to long format");
32 namespace {
33 struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass {
34 static char ID;
35 PPCBSel() : MachineFunctionPass(&ID) {}
37 /// BlockSizes - The sizes of the basic blocks in the function.
38 std::vector<unsigned> BlockSizes;
40 virtual bool runOnMachineFunction(MachineFunction &Fn);
42 virtual const char *getPassName() const {
43 return "PowerPC Branch Selector";
46 char PPCBSel::ID = 0;
49 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
50 /// Pass
51 ///
52 FunctionPass *llvm::createPPCBranchSelectionPass() {
53 return new PPCBSel();
56 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
57 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
58 // Give the blocks of the function a dense, in-order, numbering.
59 Fn.RenumberBlocks();
60 BlockSizes.resize(Fn.getNumBlockIDs());
62 // Measure each MBB and compute a size for the entire function.
63 unsigned FuncSize = 0;
64 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
65 ++MFI) {
66 MachineBasicBlock *MBB = MFI;
68 unsigned BlockSize = 0;
69 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
70 MBBI != EE; ++MBBI)
71 BlockSize += TII->GetInstSizeInBytes(MBBI);
73 BlockSizes[MBB->getNumber()] = BlockSize;
74 FuncSize += BlockSize;
77 // If the entire function is smaller than the displacement of a branch field,
78 // we know we don't need to shrink any branches in this function. This is a
79 // common case.
80 if (FuncSize < (1 << 15)) {
81 BlockSizes.clear();
82 return false;
85 // For each conditional branch, if the offset to its destination is larger
86 // than the offset field allows, transform it into a long branch sequence
87 // like this:
88 // short branch:
89 // bCC MBB
90 // long branch:
91 // b!CC $PC+8
92 // b MBB
94 bool MadeChange = true;
95 bool EverMadeChange = false;
96 while (MadeChange) {
97 // Iteratively expand branches until we reach a fixed point.
98 MadeChange = false;
100 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
101 ++MFI) {
102 MachineBasicBlock &MBB = *MFI;
103 unsigned MBBStartOffset = 0;
104 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
105 I != E; ++I) {
106 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
107 MBBStartOffset += TII->GetInstSizeInBytes(I);
108 continue;
111 // Determine the offset from the current branch to the destination
112 // block.
113 MachineBasicBlock *Dest = I->getOperand(2).getMBB();
115 int BranchSize;
116 if (Dest->getNumber() <= MBB.getNumber()) {
117 // If this is a backwards branch, the delta is the offset from the
118 // start of this block to this branch, plus the sizes of all blocks
119 // from this block to the dest.
120 BranchSize = MBBStartOffset;
122 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
123 BranchSize += BlockSizes[i];
124 } else {
125 // Otherwise, add the size of the blocks between this block and the
126 // dest to the number of bytes left in this block.
127 BranchSize = -MBBStartOffset;
129 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
130 BranchSize += BlockSizes[i];
133 // If this branch is in range, ignore it.
134 if (isInt16(BranchSize)) {
135 MBBStartOffset += 4;
136 continue;
139 // Otherwise, we have to expand it to a long branch.
140 // The BCC operands are:
141 // 0. PPC branch predicate
142 // 1. CR register
143 // 2. Target MBB
144 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
145 unsigned CRReg = I->getOperand(1).getReg();
147 MachineInstr *OldBranch = I;
148 DebugLoc dl = OldBranch->getDebugLoc();
150 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
151 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
152 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
154 // Uncond branch to the real destination.
155 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
157 // Remove the old branch from the function.
158 OldBranch->eraseFromParent();
160 // Remember that this instruction is 8-bytes, increase the size of the
161 // block by 4, remember to iterate.
162 BlockSizes[MBB.getNumber()] += 4;
163 MBBStartOffset += 8;
164 ++NumExpanded;
165 MadeChange = true;
168 EverMadeChange |= MadeChange;
171 BlockSizes.clear();
172 return true;