blob: 36f68058f8f153906a0c9835607f931d8387a643 [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"
Misha Brukmanef8cf022004-07-27 18:33:06 +000022#include <map>
23using namespace llvm;
24
25namespace {
Chris Lattner26e385a2006-02-08 19:33:26 +000026 struct PPCBSel : public MachineFunctionPass {
Misha Brukmanef8cf022004-07-27 18:33:06 +000027 // OffsetMap - Mapping between BB and byte offset from start of function
28 std::map<MachineBasicBlock*, unsigned> OffsetMap;
29
Chris Lattner26e385a2006-02-08 19:33:26 +000030 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukmanef8cf022004-07-27 18:33:06 +000031
32 virtual const char *getPassName() const {
33 return "PowerPC Branch Selection";
34 }
35 };
36}
37
38/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
39/// Pass
40///
41FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner26e385a2006-02-08 19:33:26 +000042 return new PPCBSel();
Misha Brukmanef8cf022004-07-27 18:33:06 +000043}
Chris Lattner26e385a2006-02-08 19:33:26 +000044
45/// getNumBytesForInstruction - Return the number of bytes of code the specified
46/// instruction may be. This returns the maximum number of bytes.
47///
48static unsigned getNumBytesForInstruction(MachineInstr *MI) {
49 switch (MI->getOpcode()) {
50 case PPC::COND_BRANCH:
Nate Begemanbb01d4f2006-03-17 01:40:33 +000051 // while this will be 4 most of the time, if we emit 8 it is just a
Chris Lattner26e385a2006-02-08 19:33:26 +000052 // minor pessimization that saves us from having to worry about
53 // keeping the offsets up to date later when we emit long branch glue.
Nate Begemanbb01d4f2006-03-17 01:40:33 +000054 return 8;
Chris Lattnerd48ce272006-06-27 18:18:41 +000055 case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
56 case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
Chris Lattner26e385a2006-02-08 19:33:26 +000057 case PPC::IMPLICIT_DEF_F4: // no asm emitted
58 case PPC::IMPLICIT_DEF_F8: // no asm emitted
59 return 0;
60 case PPC::INLINEASM: // Inline Asm: Variable size.
61 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
62 if (MI->getOperand(i).isExternalSymbol()) {
63 const char *AsmStr = MI->getOperand(i).getSymbolName();
64 // Count the number of newline's in the asm string.
65 unsigned NumInstrs = 0;
66 for (; *AsmStr; ++AsmStr)
67 NumInstrs += *AsmStr == '\n';
68 return NumInstrs*4;
69 }
70 assert(0 && "INLINEASM didn't have format string??");
71 default:
72 return 4; // PowerPC instructions are all 4 bytes
73 }
74}
75
76
77bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
78 // Running total of instructions encountered since beginning of function
79 unsigned ByteCount = 0;
80
81 // For each MBB, add its offset to the offset map, and count up its
82 // instructions
83 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
84 ++MFI) {
85 MachineBasicBlock *MBB = MFI;
86 OffsetMap[MBB] = ByteCount;
87
88 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
89 MBBI != EE; ++MBBI)
90 ByteCount += getNumBytesForInstruction(MBBI);
91 }
92
93 // We're about to run over the MBB's again, so reset the ByteCount
94 ByteCount = 0;
95
96 // For each MBB, find the conditional branch pseudo instructions, and
97 // calculate the difference between the target MBB and the current ICount
98 // to decide whether or not to emit a short or long branch.
99 //
100 // short branch:
101 // bCC .L_TARGET_MBB
102 //
103 // long branch:
104 // bInverseCC $PC+8
105 // b .L_TARGET_MBB
Chris Lattner26e385a2006-02-08 19:33:26 +0000106 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
107 ++MFI) {
108 MachineBasicBlock *MBB = MFI;
109
110 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
111 MBBI != EE; ++MBBI) {
112 // We may end up deleting the MachineInstr that MBBI points to, so
113 // remember its opcode now so we can refer to it after calling erase()
114 unsigned ByteSize = getNumBytesForInstruction(MBBI);
115 if (MBBI->getOpcode() == PPC::COND_BRANCH) {
116 MachineBasicBlock::iterator MBBJ = MBBI;
117 ++MBBJ;
118
119 // condbranch operands:
120 // 0. CR0 register
121 // 1. bc opcode
122 // 2. target MBB
123 // 3. fallthrough MBB
124 MachineBasicBlock *trueMBB =
125 MBBI->getOperand(2).getMachineBasicBlock();
Chris Lattner26e385a2006-02-08 19:33:26 +0000126
127 int Displacement = OffsetMap[trueMBB] - ByteCount;
128 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
129 unsigned CRReg = MBBI->getOperand(0).getReg();
130 unsigned Inverted = PPCInstrInfo::invertPPCBranchOpcode(Opcode);
131
132 if (Displacement >= -32768 && Displacement <= 32767) {
133 BuildMI(*MBB, MBBJ, Opcode, 2).addReg(CRReg).addMBB(trueMBB);
134 } else {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000135 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(CRReg).addImm(8);
Chris Lattner26e385a2006-02-08 19:33:26 +0000136 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
Chris Lattner26e385a2006-02-08 19:33:26 +0000137 }
138
139 // Erase the psuedo COND_BRANCH instruction, and then back up the
140 // iterator so that when the for loop increments it, we end up in
141 // the correct place rather than iterating off the end.
142 MBB->erase(MBBI);
143 MBBI = --MBBJ;
144 }
145 ByteCount += ByteSize;
146 }
147 }
148
149 OffsetMap.clear();
150 return true;
151}
152