blob: 99115752f717782234206edd26e2de4077dc9b83 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmanef8cf022004-07-27 18:33:06 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Gabor Greif21fed662010-08-23 20:30:51 +000013// positions pass, and a branch pseudo op to machine branch opcode pass. This
Misha Brukmanef8cf022004-07-27 18:33:06 +000014// 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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "MCTargetDesc/PPCPredicates.h"
Chris Lattnere80bf1b2005-10-14 23:45:43 +000021#include "PPCInstrBuilder.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000022#include "PPCInstrInfo.h"
Chris Lattner96d73862006-11-16 18:13:49 +000023#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner542dfd52006-11-18 00:32:03 +000025#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetMachine.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000027using namespace llvm;
28
Chris Lattner1ef9cd42006-12-19 22:59:26 +000029STATISTIC(NumExpanded, "Number of branches expanded to long format");
Chris Lattner96d73862006-11-16 18:13:49 +000030
Misha Brukmanef8cf022004-07-27 18:33:06 +000031namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000032 struct PPCBSel : public MachineFunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000033 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000034 PPCBSel() : MachineFunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000035
Chris Lattner542dfd52006-11-18 00:32:03 +000036 /// BlockSizes - The sizes of the basic blocks in the function.
37 std::vector<unsigned> BlockSizes;
Misha Brukmanef8cf022004-07-27 18:33:06 +000038
Chris Lattner26e385a2006-02-08 19:33:26 +000039 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukmanef8cf022004-07-27 18:33:06 +000040
41 virtual const char *getPassName() const {
Chris Lattner542dfd52006-11-18 00:32:03 +000042 return "PowerPC Branch Selector";
Misha Brukmanef8cf022004-07-27 18:33:06 +000043 }
44 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000045 char PPCBSel::ID = 0;
Misha Brukmanef8cf022004-07-27 18:33:06 +000046}
47
48/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
49/// Pass
50///
51FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000052 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000053}
Chris Lattner26e385a2006-02-08 19:33:26 +000054
Chris Lattner26e385a2006-02-08 19:33:26 +000055bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
Gabor Greif4ad727172010-07-23 13:28:47 +000056 const PPCInstrInfo *TII =
57 static_cast<const PPCInstrInfo*>(Fn.getTarget().getInstrInfo());
Chris Lattner542dfd52006-11-18 00:32:03 +000058 // Give the blocks of the function a dense, in-order, numbering.
59 Fn.RenumberBlocks();
60 BlockSizes.resize(Fn.getNumBlockIDs());
61
62 // Measure each MBB and compute a size for the entire function.
63 unsigned FuncSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000064 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
65 ++MFI) {
66 MachineBasicBlock *MBB = MFI;
Chris Lattner542dfd52006-11-18 00:32:03 +000067
68 unsigned BlockSize = 0;
Chris Lattner26e385a2006-02-08 19:33:26 +000069 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
70 MBBI != EE; ++MBBI)
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +000071 BlockSize += TII->GetInstSizeInBytes(MBBI);
Chris Lattner26e385a2006-02-08 19:33:26 +000072
Chris Lattner542dfd52006-11-18 00:32:03 +000073 BlockSizes[MBB->getNumber()] = BlockSize;
74 FuncSize += BlockSize;
Chris Lattner26e385a2006-02-08 19:33:26 +000075 }
76
Chris Lattner542dfd52006-11-18 00:32:03 +000077 // 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;
83 }
84
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
93 //
94 bool MadeChange = true;
95 bool EverMadeChange = false;
96 while (MadeChange) {
97 // Iteratively expand branches until we reach a fixed point.
98 MadeChange = false;
99
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) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000106 if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000107 MBBStartOffset += TII->GetInstSizeInBytes(I);
Chris Lattner542dfd52006-11-18 00:32:03 +0000108 continue;
109 }
110
111 // Determine the offset from the current branch to the destination
112 // block.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000113 MachineBasicBlock *Dest = I->getOperand(2).getMBB();
Chris Lattner542dfd52006-11-18 00:32:03 +0000114
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;
121
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;
128
129 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
130 BranchSize += BlockSizes[i];
131 }
132
133 // If this branch is in range, ignore it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000134 if (isInt<16>(BranchSize)) {
Chris Lattner542dfd52006-11-18 00:32:03 +0000135 MBBStartOffset += 4;
136 continue;
137 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000138
Chris Lattner542dfd52006-11-18 00:32:03 +0000139 // Otherwise, we have to expand it to a long branch.
Chris Lattner542dfd52006-11-18 00:32:03 +0000140 MachineInstr *OldBranch = I;
Dale Johannesene9f623e2009-02-13 02:27:39 +0000141 DebugLoc dl = OldBranch->getDebugLoc();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000142
143 if (I->getOpcode() == PPC::BCC) {
144 // The BCC operands are:
145 // 0. PPC branch predicate
146 // 1. CR register
147 // 2. Target MBB
148 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
149 unsigned CRReg = I->getOperand(1).getReg();
150
151 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
152 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
153 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
154 } else if (I->getOpcode() == PPC::BDNZ) {
155 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
156 } else if (I->getOpcode() == PPC::BDNZ8) {
157 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
158 } else if (I->getOpcode() == PPC::BDZ) {
159 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
160 } else if (I->getOpcode() == PPC::BDZ8) {
161 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
162 } else {
163 llvm_unreachable("Unhandled branch type!");
164 }
Chris Lattner542dfd52006-11-18 00:32:03 +0000165
166 // Uncond branch to the real destination.
Dale Johannesene9f623e2009-02-13 02:27:39 +0000167 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
Chris Lattner542dfd52006-11-18 00:32:03 +0000168
169 // Remove the old branch from the function.
170 OldBranch->eraseFromParent();
171
172 // Remember that this instruction is 8-bytes, increase the size of the
173 // block by 4, remember to iterate.
174 BlockSizes[MBB.getNumber()] += 4;
175 MBBStartOffset += 8;
176 ++NumExpanded;
177 MadeChange = true;
178 }
179 }
180 EverMadeChange |= MadeChange;
181 }
182
183 BlockSizes.clear();
Chris Lattner26e385a2006-02-08 19:33:26 +0000184 return true;
185}
186