blob: f3d31243583d466aa13f51b37f2a664749de2f31 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmanef8cf022004-07-27 18:33:06 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Baegeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmanef8cf022004-07-27 18:33:06 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb4402432005-04-21 23:30:14 +000010// This file contains a pass that scans a machine function to determine which
Misha Brukmanef8cf022004-07-27 18:33:06 +000011// 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.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000018#include "PPC.h"
Chris Lattnere80bf1b2005-10-14 23:45:43 +000019#include "PPCInstrBuilder.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000020#include "PPCInstrInfo.h"
Chris Lattner8c6a41e2006-11-17 22:10:59 +000021#include "PPCPredicates.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner4dc4f302006-10-13 17:56:02 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner96d73862006-11-16 18:13:49 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/Support/Compiler.h"
Chris Lattner542dfd52006-11-18 00:32:03 +000027#include "llvm/Support/MathExtras.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000028using namespace llvm;
29
Chris Lattner96d73862006-11-16 18:13:49 +000030static Statistic<> NumExpanded("ppc-branch-select",
31 "Num branches expanded to long format");
32
Misha Brukmanef8cf022004-07-27 18:33:06 +000033namespace {
Chris Lattner996795b2006-06-28 23:17:24 +000034 struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass {
Chris Lattner542dfd52006-11-18 00:32:03 +000035 /// BlockSizes - The sizes of the basic blocks in the function.
36 std::vector<unsigned> BlockSizes;
Misha Brukmanef8cf022004-07-27 18:33:06 +000037
Chris Lattner26e385a2006-02-08 19:33:26 +000038 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukmanef8cf022004-07-27 18:33:06 +000039
40 virtual const char *getPassName() const {
Chris Lattner542dfd52006-11-18 00:32:03 +000041 return "PowerPC Branch Selector";
Misha Brukmanef8cf022004-07-27 18:33:06 +000042 }
43 };
44}
45
46/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
47/// Pass
48///
49FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000050 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000051}
Chris Lattner26e385a2006-02-08 19:33:26 +000052
53/// getNumBytesForInstruction - Return the number of bytes of code the specified
54/// instruction may be. This returns the maximum number of bytes.
55///
56static unsigned getNumBytesForInstruction(MachineInstr *MI) {
57 switch (MI->getOpcode()) {
Chris Lattnerd48ce272006-06-27 18:18:41 +000058 case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
59 case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
Chris Lattner4dc4f302006-10-13 17:56:02 +000060 case PPC::IMPLICIT_DEF_F4: // no asm emitted
61 case PPC::IMPLICIT_DEF_F8: // no asm emitted
Chris Lattnera7152882006-11-16 23:49:52 +000062 case PPC::IMPLICIT_DEF_VRRC: // no asm emitted
Chris Lattner26e385a2006-02-08 19:33:26 +000063 return 0;
Chris Lattner4dc4f302006-10-13 17:56:02 +000064 case PPC::INLINEASM: { // Inline Asm: Variable size.
65 MachineFunction *MF = MI->getParent()->getParent();
66 const char *AsmStr = MI->getOperand(0).getSymbolName();
67 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
68 }
Chris Lattner26e385a2006-02-08 19:33:26 +000069 default:
70 return 4; // PowerPC instructions are all 4 bytes
71 }
72}
73
74
75bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Chris Lattner542dfd52006-11-18 00:32:03 +000076 // Give the blocks of the function a dense, in-order, numbering.
77 Fn.RenumberBlocks();
78 BlockSizes.resize(Fn.getNumBlockIDs());
79
80 // Measure each MBB and compute a size for the entire function.
81 unsigned FuncSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000082 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
83 ++MFI) {
84 MachineBasicBlock *MBB = MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +000085
86 unsigned BlockSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000087 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
88 MBBI != EE; ++MBBI)
Chris Lattner542dfd52006-11-18 00:32:03 +000089 BlockSize += getNumBytesForInstruction(MBBI);
Chris Lattner26e385a2006-02-08 19:33:26 +000090
Chris Lattner542dfd52006-11-18 00:32:03 +000091 BlockSizes[MBB->getNumber()] = BlockSize;
92 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +000093 }
94
Chris Lattner542dfd52006-11-18 00:32:03 +000095 // If the entire function is smaller than the displacement of a branch field,
96 // we know we don't need to shrink any branches in this function. This is a
97 // common case.
98 if (FuncSize < (1 << 15)) {
99 BlockSizes.clear();
100 return false;
101 }
102
103 // For each conditional branch, if the offset to its destination is larger
104 // than the offset field allows, transform it into a long branch sequence
105 // like this:
106 // short branch:
107 // bCC MBB
108 // long branch:
109 // b!CC $PC+8
110 // b MBB
111 //
112 bool MadeChange = true;
113 bool EverMadeChange = false;
114 while (MadeChange) {
115 // Iteratively expand branches until we reach a fixed point.
116 MadeChange = false;
117
118 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
119 ++MFI) {
120 MachineBasicBlock &MBB = *MFI;
121 unsigned MBBStartOffset = 0;
122 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
123 I != E; ++I) {
124 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
125 MBBStartOffset += getNumBytesForInstruction(I);
126 continue;
127 }
128
129 // Determine the offset from the current branch to the destination
130 // block.
131 MachineBasicBlock *Dest = I->getOperand(2).getMachineBasicBlock();
132
133 int BranchSize;
134 if (Dest->getNumber() <= MBB.getNumber()) {
135 // If this is a backwards branch, the delta is the offset from the
136 // start of this block to this branch, plus the sizes of all blocks
137 // from this block to the dest.
138 BranchSize = MBBStartOffset;
139
140 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
141 BranchSize += BlockSizes[i];
142 } else {
143 // Otherwise, add the size of the blocks between this block and the
144 // dest to the number of bytes left in this block.
145 BranchSize = -MBBStartOffset;
146
147 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
148 BranchSize += BlockSizes[i];
149 }
150
151 // If this branch is in range, ignore it.
152 if (isInt16(BranchSize)) {
153 MBBStartOffset += 4;
154 continue;
155 }
156
157 // Otherwise, we have to expand it to a long branch.
158 // The BCC operands are:
159 // 0. PPC branch predicate
160 // 1. CR register
161 // 2. Target MBB
162 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
163 unsigned CRReg = I->getOperand(1).getReg();
164
165 MachineInstr *OldBranch = I;
166
167 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
168 BuildMI(MBB, I, PPC::BCC, 3)
169 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
170
171 // Uncond branch to the real destination.
172 I = BuildMI(MBB, I, PPC::B, 1).addMBB(Dest);
173
174 // Remove the old branch from the function.
175 OldBranch->eraseFromParent();
176
177 // Remember that this instruction is 8-bytes, increase the size of the
178 // block by 4, remember to iterate.
179 BlockSizes[MBB.getNumber()] += 4;
180 MBBStartOffset += 8;
181 ++NumExpanded;
182 MadeChange = true;
183 }
184 }
185 EverMadeChange |= MadeChange;
186 }
187
188 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000189 return true;
190}
191