Remove the TargetMachine forwards for TargetSubtargetInfo based
[oota-llvm.git] / lib / Target / PowerPC / PPCBranchSelector.cpp
1 //===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
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 pseudo op to machine branch opcode pass.  This
14 // pass should be run last, just before the assembly printer.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "PPC.h"
19 #include "MCTargetDesc/PPCPredicates.h"
20 #include "PPCInstrBuilder.h"
21 #include "PPCInstrInfo.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetSubtargetInfo.h"
27 using namespace llvm;
28
29 #define DEBUG_TYPE "ppc-branch-select"
30
31 STATISTIC(NumExpanded, "Number of branches expanded to long format");
32
33 namespace llvm {
34   void initializePPCBSelPass(PassRegistry&);
35 }
36
37 namespace {
38   struct PPCBSel : public MachineFunctionPass {
39     static char ID;
40     PPCBSel() : MachineFunctionPass(ID) {
41       initializePPCBSelPass(*PassRegistry::getPassRegistry());
42     }
43
44     /// BlockSizes - The sizes of the basic blocks in the function.
45     std::vector<unsigned> BlockSizes;
46
47     bool runOnMachineFunction(MachineFunction &Fn) override;
48
49     const char *getPassName() const override {
50       return "PowerPC Branch Selector";
51     }
52   };
53   char PPCBSel::ID = 0;
54 }
55
56 INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
57                 false, false)
58
59 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
60 /// Pass
61 ///
62 FunctionPass *llvm::createPPCBranchSelectionPass() {
63   return new PPCBSel();
64 }
65
66 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
67   const PPCInstrInfo *TII = static_cast<const PPCInstrInfo *>(
68       Fn.getTarget().getSubtargetImpl()->getInstrInfo());
69   // Give the blocks of the function a dense, in-order, numbering.
70   Fn.RenumberBlocks();
71   BlockSizes.resize(Fn.getNumBlockIDs());
72
73   // Measure each MBB and compute a size for the entire function.
74   unsigned FuncSize = 0;
75   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
76        ++MFI) {
77     MachineBasicBlock *MBB = MFI;
78
79     unsigned BlockSize = 0;
80     for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
81          MBBI != EE; ++MBBI)
82       BlockSize += TII->GetInstSizeInBytes(MBBI);
83     
84     BlockSizes[MBB->getNumber()] = BlockSize;
85     FuncSize += BlockSize;
86   }
87   
88   // If the entire function is smaller than the displacement of a branch field,
89   // we know we don't need to shrink any branches in this function.  This is a
90   // common case.
91   if (FuncSize < (1 << 15)) {
92     BlockSizes.clear();
93     return false;
94   }
95   
96   // For each conditional branch, if the offset to its destination is larger
97   // than the offset field allows, transform it into a long branch sequence
98   // like this:
99   //   short branch:
100   //     bCC MBB
101   //   long branch:
102   //     b!CC $PC+8
103   //     b MBB
104   //
105   bool MadeChange = true;
106   bool EverMadeChange = false;
107   while (MadeChange) {
108     // Iteratively expand branches until we reach a fixed point.
109     MadeChange = false;
110   
111     for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
112          ++MFI) {
113       MachineBasicBlock &MBB = *MFI;
114       unsigned MBBStartOffset = 0;
115       for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
116            I != E; ++I) {
117         MachineBasicBlock *Dest = nullptr;
118         if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
119           Dest = I->getOperand(2).getMBB();
120         else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
121                  !I->getOperand(1).isImm())
122           Dest = I->getOperand(1).getMBB();
123         else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
124                   I->getOpcode() == PPC::BDZ8  || I->getOpcode() == PPC::BDZ) &&
125                  !I->getOperand(0).isImm())
126           Dest = I->getOperand(0).getMBB();
127
128         if (!Dest) {
129           MBBStartOffset += TII->GetInstSizeInBytes(I);
130           continue;
131         }
132         
133         // Determine the offset from the current branch to the destination
134         // block.
135         int BranchSize;
136         if (Dest->getNumber() <= MBB.getNumber()) {
137           // If this is a backwards branch, the delta is the offset from the
138           // start of this block to this branch, plus the sizes of all blocks
139           // from this block to the dest.
140           BranchSize = MBBStartOffset;
141           
142           for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
143             BranchSize += BlockSizes[i];
144         } else {
145           // Otherwise, add the size of the blocks between this block and the
146           // dest to the number of bytes left in this block.
147           BranchSize = -MBBStartOffset;
148
149           for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
150             BranchSize += BlockSizes[i];
151         }
152
153         // If this branch is in range, ignore it.
154         if (isInt<16>(BranchSize)) {
155           MBBStartOffset += 4;
156           continue;
157         }
158
159         // Otherwise, we have to expand it to a long branch.
160         MachineInstr *OldBranch = I;
161         DebugLoc dl = OldBranch->getDebugLoc();
162  
163         if (I->getOpcode() == PPC::BCC) {
164           // The BCC operands are:
165           // 0. PPC branch predicate
166           // 1. CR register
167           // 2. Target MBB
168           PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
169           unsigned CRReg = I->getOperand(1).getReg();
170        
171           // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
172           BuildMI(MBB, I, dl, TII->get(PPC::BCC))
173             .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
174         } else if (I->getOpcode() == PPC::BC) {
175           unsigned CRBit = I->getOperand(0).getReg();
176           BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
177         } else if (I->getOpcode() == PPC::BCn) {
178           unsigned CRBit = I->getOperand(0).getReg();
179           BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
180         } else if (I->getOpcode() == PPC::BDNZ) {
181           BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
182         } else if (I->getOpcode() == PPC::BDNZ8) {
183           BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
184         } else if (I->getOpcode() == PPC::BDZ) {
185           BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
186         } else if (I->getOpcode() == PPC::BDZ8) {
187           BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
188         } else {
189            llvm_unreachable("Unhandled branch type!");
190         }
191         
192         // Uncond branch to the real destination.
193         I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
194
195         // Remove the old branch from the function.
196         OldBranch->eraseFromParent();
197         
198         // Remember that this instruction is 8-bytes, increase the size of the
199         // block by 4, remember to iterate.
200         BlockSizes[MBB.getNumber()] += 4;
201         MBBStartOffset += 8;
202         ++NumExpanded;
203         MadeChange = true;
204       }
205     }
206     EverMadeChange |= MadeChange;
207   }
208   
209   BlockSizes.clear();
210   return true;
211 }
212