blob: e8274f3b9e531e29814405b7a1cfa8c0b4b359c8 [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 Lattner1ef9cd42006-12-19 22:59:26 +000018#define DEBUG_TYPE "ppc-branch-select"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000019#include "PPC.h"
Chris Lattnere80bf1b2005-10-14 23:45:43 +000020#include "PPCInstrBuilder.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000021#include "PPCInstrInfo.h"
Chris Lattner8c6a41e2006-11-17 22:10:59 +000022#include "PPCPredicates.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner4dc4f302006-10-13 17:56:02 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner96d73862006-11-16 18:13:49 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/Support/Compiler.h"
Chris Lattner542dfd52006-11-18 00:32:03 +000028#include "llvm/Support/MathExtras.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000029using namespace llvm;
30
Chris Lattner1ef9cd42006-12-19 22:59:26 +000031STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattner96d73862006-11-16 18:13:49 +000032
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) {
Evan Cheng20350c42006-11-27 23:37:22 +000076 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
Chris Lattner542dfd52006-11-18 00:32:03 +000077 // Give the blocks of the function a dense, in-order, numbering.
78 Fn.RenumberBlocks();
79 BlockSizes.resize(Fn.getNumBlockIDs());
80
81 // Measure each MBB and compute a size for the entire function.
82 unsigned FuncSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000083 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
84 ++MFI) {
85 MachineBasicBlock *MBB = MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +000086
87 unsigned BlockSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000088 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
89 MBBI != EE; ++MBBI)
Chris Lattner542dfd52006-11-18 00:32:03 +000090 BlockSize += getNumBytesForInstruction(MBBI);
Chris Lattner26e385a2006-02-08 19:33:26 +000091
Chris Lattner542dfd52006-11-18 00:32:03 +000092 BlockSizes[MBB->getNumber()] = BlockSize;
93 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +000094 }
95
Chris Lattner542dfd52006-11-18 00:32:03 +000096 // If the entire function is smaller than the displacement of a branch field,
97 // we know we don't need to shrink any branches in this function. This is a
98 // common case.
99 if (FuncSize < (1 << 15)) {
100 BlockSizes.clear();
101 return false;
102 }
103
104 // For each conditional branch, if the offset to its destination is larger
105 // than the offset field allows, transform it into a long branch sequence
106 // like this:
107 // short branch:
108 // bCC MBB
109 // long branch:
110 // b!CC $PC+8
111 // b MBB
112 //
113 bool MadeChange = true;
114 bool EverMadeChange = false;
115 while (MadeChange) {
116 // Iteratively expand branches until we reach a fixed point.
117 MadeChange = false;
118
119 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
120 ++MFI) {
121 MachineBasicBlock &MBB = *MFI;
122 unsigned MBBStartOffset = 0;
123 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
124 I != E; ++I) {
125 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
126 MBBStartOffset += getNumBytesForInstruction(I);
127 continue;
128 }
129
130 // Determine the offset from the current branch to the destination
131 // block.
132 MachineBasicBlock *Dest = I->getOperand(2).getMachineBasicBlock();
133
134 int BranchSize;
135 if (Dest->getNumber() <= MBB.getNumber()) {
136 // If this is a backwards branch, the delta is the offset from the
137 // start of this block to this branch, plus the sizes of all blocks
138 // from this block to the dest.
139 BranchSize = MBBStartOffset;
140
141 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
142 BranchSize += BlockSizes[i];
143 } else {
144 // Otherwise, add the size of the blocks between this block and the
145 // dest to the number of bytes left in this block.
146 BranchSize = -MBBStartOffset;
147
148 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
149 BranchSize += BlockSizes[i];
150 }
151
152 // If this branch is in range, ignore it.
153 if (isInt16(BranchSize)) {
154 MBBStartOffset += 4;
155 continue;
156 }
157
158 // Otherwise, we have to expand it to a long branch.
159 // The BCC operands are:
160 // 0. PPC branch predicate
161 // 1. CR register
162 // 2. Target MBB
163 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
164 unsigned CRReg = I->getOperand(1).getReg();
165
166 MachineInstr *OldBranch = I;
167
168 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
Evan Cheng20350c42006-11-27 23:37:22 +0000169 BuildMI(MBB, I, TII->get(PPC::BCC))
Chris Lattner542dfd52006-11-18 00:32:03 +0000170 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
171
172 // Uncond branch to the real destination.
Evan Cheng20350c42006-11-27 23:37:22 +0000173 I = BuildMI(MBB, I, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner542dfd52006-11-18 00:32:03 +0000174
175 // Remove the old branch from the function.
176 OldBranch->eraseFromParent();
177
178 // Remember that this instruction is 8-bytes, increase the size of the
179 // block by 4, remember to iterate.
180 BlockSizes[MBB.getNumber()] += 4;
181 MBBStartOffset += 8;
182 ++NumExpanded;
183 MadeChange = true;
184 }
185 }
186 EverMadeChange |= MadeChange;
187 }
188
189 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000190 return true;
191}
192