blob: e7fce56b20988dad56d42c6951dcd7d75090ffed [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman999d9cf2004-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 Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman999d9cf2004-07-27 18:33:06 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb5f662f2005-04-21 23:30:14 +000010// This file contains a pass that scans a machine function to determine which
Misha Brukman999d9cf2004-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 Lattner26689592005-10-14 23:51:18 +000018#include "PPC.h"
Chris Lattner26bd0d42005-10-14 23:45:43 +000019#include "PPCInstrBuilder.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000020#include "PPCInstrInfo.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000022#include <map>
23using namespace llvm;
24
25namespace {
Chris Lattner6f4a0722006-02-08 19:33:26 +000026 struct PPCBSel : public MachineFunctionPass {
Misha Brukman999d9cf2004-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 Lattner6f4a0722006-02-08 19:33:26 +000030 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukman999d9cf2004-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 Lattner6f4a0722006-02-08 19:33:26 +000042 return new PPCBSel();
Misha Brukman999d9cf2004-07-27 18:33:06 +000043}
Chris Lattner6f4a0722006-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:
51 // while this will be 4 most of the time, if we emit 12 it is just a
52 // minor pessimization that saves us from having to worry about
53 // keeping the offsets up to date later when we emit long branch glue.
54 return 12;
55 case PPC::IMPLICIT_DEF_GPR: // no asm emitted
56 case PPC::IMPLICIT_DEF_F4: // no asm emitted
57 case PPC::IMPLICIT_DEF_F8: // no asm emitted
58 return 0;
59 case PPC::INLINEASM: // Inline Asm: Variable size.
60 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
61 if (MI->getOperand(i).isExternalSymbol()) {
62 const char *AsmStr = MI->getOperand(i).getSymbolName();
63 // Count the number of newline's in the asm string.
64 unsigned NumInstrs = 0;
65 for (; *AsmStr; ++AsmStr)
66 NumInstrs += *AsmStr == '\n';
67 return NumInstrs*4;
68 }
69 assert(0 && "INLINEASM didn't have format string??");
70 default:
71 return 4; // PowerPC instructions are all 4 bytes
72 }
73}
74
75
76bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
77 // Running total of instructions encountered since beginning of function
78 unsigned ByteCount = 0;
79
80 // For each MBB, add its offset to the offset map, and count up its
81 // instructions
82 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
83 ++MFI) {
84 MachineBasicBlock *MBB = MFI;
85 OffsetMap[MBB] = ByteCount;
86
87 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
88 MBBI != EE; ++MBBI)
89 ByteCount += getNumBytesForInstruction(MBBI);
90 }
91
92 // We're about to run over the MBB's again, so reset the ByteCount
93 ByteCount = 0;
94
95 // For each MBB, find the conditional branch pseudo instructions, and
96 // calculate the difference between the target MBB and the current ICount
97 // to decide whether or not to emit a short or long branch.
98 //
99 // short branch:
100 // bCC .L_TARGET_MBB
101 //
102 // long branch:
103 // bInverseCC $PC+8
104 // b .L_TARGET_MBB
105 // b .L_FALLTHROUGH_MBB
106 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();
126 MachineBasicBlock *falseMBB =
127 MBBI->getOperand(3).getMachineBasicBlock();
128
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 {
137 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(CRReg).addSImm(8);
138 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
139 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(falseMBB);
140 }
141
142 // Erase the psuedo COND_BRANCH instruction, and then back up the
143 // iterator so that when the for loop increments it, we end up in
144 // the correct place rather than iterating off the end.
145 MBB->erase(MBBI);
146 MBBI = --MBBJ;
147 }
148 ByteCount += ByteSize;
149 }
150 }
151
152 OffsetMap.clear();
153 return true;
154}
155