1 //===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 pseudo 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"
20 #include "MCTargetDesc/PPCPredicates.h"
21 #include "PPCInstrBuilder.h"
22 #include "PPCInstrInfo.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Target/TargetMachine.h"
29 STATISTIC(NumExpanded, "Number of branches expanded to long format");
32 void initializePPCBSelPass(PassRegistry&);
36 struct PPCBSel : public MachineFunctionPass {
38 PPCBSel() : MachineFunctionPass(ID) {
39 initializePPCBSelPass(*PassRegistry::getPassRegistry());
42 /// BlockSizes - The sizes of the basic blocks in the function.
43 std::vector<unsigned> BlockSizes;
45 virtual bool runOnMachineFunction(MachineFunction &Fn);
47 virtual const char *getPassName() const {
48 return "PowerPC Branch Selector";
54 INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
57 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
60 FunctionPass *llvm::createPPCBranchSelectionPass() {
64 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
65 const PPCInstrInfo *TII =
66 static_cast<const PPCInstrInfo*>(Fn.getTarget().getInstrInfo());
67 // Give the blocks of the function a dense, in-order, numbering.
69 BlockSizes.resize(Fn.getNumBlockIDs());
71 // Measure each MBB and compute a size for the entire function.
72 unsigned FuncSize = 0;
73 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
75 MachineBasicBlock *MBB = MFI;
77 unsigned BlockSize = 0;
78 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
80 BlockSize += TII->GetInstSizeInBytes(MBBI);
82 BlockSizes[MBB->getNumber()] = BlockSize;
83 FuncSize += BlockSize;
86 // If the entire function is smaller than the displacement of a branch field,
87 // we know we don't need to shrink any branches in this function. This is a
89 if (FuncSize < (1 << 15)) {
94 // For each conditional branch, if the offset to its destination is larger
95 // than the offset field allows, transform it into a long branch sequence
103 bool MadeChange = true;
104 bool EverMadeChange = false;
106 // Iteratively expand branches until we reach a fixed point.
109 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
111 MachineBasicBlock &MBB = *MFI;
112 unsigned MBBStartOffset = 0;
113 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
115 MachineBasicBlock *Dest = 0;
116 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
117 Dest = I->getOperand(2).getMBB();
118 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
119 !I->getOperand(1).isImm())
120 Dest = I->getOperand(1).getMBB();
121 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
122 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) &&
123 !I->getOperand(0).isImm())
124 Dest = I->getOperand(0).getMBB();
127 MBBStartOffset += TII->GetInstSizeInBytes(I);
131 // Determine the offset from the current branch to the destination
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;
140 for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
141 BranchSize += BlockSizes[i];
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;
147 for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
148 BranchSize += BlockSizes[i];
151 // If this branch is in range, ignore it.
152 if (isInt<16>(BranchSize)) {
157 // Otherwise, we have to expand it to a long branch.
158 MachineInstr *OldBranch = I;
159 DebugLoc dl = OldBranch->getDebugLoc();
161 if (I->getOpcode() == PPC::BCC) {
162 // The BCC operands are:
163 // 0. PPC branch predicate
166 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
167 unsigned CRReg = I->getOperand(1).getReg();
169 // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
170 BuildMI(MBB, I, dl, TII->get(PPC::BCC))
171 .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
172 } else if (I->getOpcode() == PPC::BC) {
173 unsigned CRBit = I->getOperand(0).getReg();
174 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
175 } else if (I->getOpcode() == PPC::BCn) {
176 unsigned CRBit = I->getOperand(0).getReg();
177 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
178 } else if (I->getOpcode() == PPC::BDNZ) {
179 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
180 } else if (I->getOpcode() == PPC::BDNZ8) {
181 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
182 } else if (I->getOpcode() == PPC::BDZ) {
183 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
184 } else if (I->getOpcode() == PPC::BDZ8) {
185 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
187 llvm_unreachable("Unhandled branch type!");
190 // Uncond branch to the real destination.
191 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
193 // Remove the old branch from the function.
194 OldBranch->eraseFromParent();
196 // Remember that this instruction is 8-bytes, increase the size of the
197 // block by 4, remember to iterate.
198 BlockSizes[MBB.getNumber()] += 4;
204 EverMadeChange |= MadeChange;