blob: 123edb781d9640dc3cc68bb6c87aac83f8b6374a [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"
Misha Brukmanef8cf022004-07-27 18:33:06 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner4dc4f302006-10-13 17:56:02 +000022#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner96d73862006-11-16 18:13:49 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/Support/Compiler.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000026#include <map>
27using namespace llvm;
28
Chris Lattner96d73862006-11-16 18:13:49 +000029static Statistic<> NumExpanded("ppc-branch-select",
30 "Num branches expanded to long format");
31
Misha Brukmanef8cf022004-07-27 18:33:06 +000032namespace {
Chris Lattner996795b2006-06-28 23:17:24 +000033 struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass {
Chris Lattner4dc4f302006-10-13 17:56:02 +000034 /// OffsetMap - Mapping between BB and byte offset from start of function.
35 /// TODO: replace this with a vector, using the MBB idx as the key.
Misha Brukmanef8cf022004-07-27 18:33:06 +000036 std::map<MachineBasicBlock*, unsigned> OffsetMap;
37
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 {
41 return "PowerPC Branch Selection";
42 }
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()) {
58 case PPC::COND_BRANCH:
Nate Begemanbb01d4f2006-03-17 01:40:33 +000059 // while this will be 4 most of the time, if we emit 8 it is just a
Chris Lattner26e385a2006-02-08 19:33:26 +000060 // minor pessimization that saves us from having to worry about
61 // keeping the offsets up to date later when we emit long branch glue.
Nate Begemanbb01d4f2006-03-17 01:40:33 +000062 return 8;
Chris Lattnerd48ce272006-06-27 18:18:41 +000063 case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
64 case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
Chris Lattner4dc4f302006-10-13 17:56:02 +000065 case PPC::IMPLICIT_DEF_F4: // no asm emitted
66 case PPC::IMPLICIT_DEF_F8: // no asm emitted
Chris Lattner26e385a2006-02-08 19:33:26 +000067 return 0;
Chris Lattner4dc4f302006-10-13 17:56:02 +000068 case PPC::INLINEASM: { // Inline Asm: Variable size.
69 MachineFunction *MF = MI->getParent()->getParent();
70 const char *AsmStr = MI->getOperand(0).getSymbolName();
71 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
72 }
Chris Lattner26e385a2006-02-08 19:33:26 +000073 default:
74 return 4; // PowerPC instructions are all 4 bytes
75 }
76}
77
78
79bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
80 // Running total of instructions encountered since beginning of function
81 unsigned ByteCount = 0;
82
83 // For each MBB, add its offset to the offset map, and count up its
84 // instructions
85 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
86 ++MFI) {
87 MachineBasicBlock *MBB = MFI;
88 OffsetMap[MBB] = ByteCount;
89
90 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
91 MBBI != EE; ++MBBI)
92 ByteCount += getNumBytesForInstruction(MBBI);
93 }
94
95 // We're about to run over the MBB's again, so reset the ByteCount
96 ByteCount = 0;
97
98 // For each MBB, find the conditional branch pseudo instructions, and
99 // calculate the difference between the target MBB and the current ICount
100 // to decide whether or not to emit a short or long branch.
101 //
102 // short branch:
103 // bCC .L_TARGET_MBB
104 //
105 // long branch:
106 // bInverseCC $PC+8
107 // b .L_TARGET_MBB
Chris Lattner26e385a2006-02-08 19:33:26 +0000108 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
109 ++MFI) {
110 MachineBasicBlock *MBB = MFI;
111
112 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
113 MBBI != EE; ++MBBI) {
114 // We may end up deleting the MachineInstr that MBBI points to, so
115 // remember its opcode now so we can refer to it after calling erase()
116 unsigned ByteSize = getNumBytesForInstruction(MBBI);
117 if (MBBI->getOpcode() == PPC::COND_BRANCH) {
118 MachineBasicBlock::iterator MBBJ = MBBI;
119 ++MBBJ;
120
121 // condbranch operands:
122 // 0. CR0 register
123 // 1. bc opcode
124 // 2. target MBB
125 // 3. fallthrough MBB
126 MachineBasicBlock *trueMBB =
127 MBBI->getOperand(2).getMachineBasicBlock();
Chris Lattner26e385a2006-02-08 19:33:26 +0000128
129 int Displacement = OffsetMap[trueMBB] - ByteCount;
130 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
131 unsigned CRReg = MBBI->getOperand(0).getReg();
132 unsigned Inverted = PPCInstrInfo::invertPPCBranchOpcode(Opcode);
133
134 if (Displacement >= -32768 && Displacement <= 32767) {
135 BuildMI(*MBB, MBBJ, Opcode, 2).addReg(CRReg).addMBB(trueMBB);
136 } else {
Evan Cheng1b200572006-08-25 23:29:06 +0000137 // Long branch, skip next branch instruction (i.e. $PC+8).
Chris Lattner96d73862006-11-16 18:13:49 +0000138 ++NumExpanded;
Evan Chengd7572fb2006-08-25 21:54:44 +0000139 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(CRReg).addImm(2);
Chris Lattner26e385a2006-02-08 19:33:26 +0000140 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
Chris Lattner26e385a2006-02-08 19:33:26 +0000141 }
142
143 // Erase the psuedo COND_BRANCH instruction, and then back up the
144 // iterator so that when the for loop increments it, we end up in
145 // the correct place rather than iterating off the end.
146 MBB->erase(MBBI);
147 MBBI = --MBBJ;
148 }
149 ByteCount += ByteSize;
150 }
151 }
152
153 OffsetMap.clear();
154 return true;
155}
156