blob: 10efb9882a5e4da8eab6fcdc335a986260b4ede4 [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"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000022#include "llvm/Support/Compiler.h"
Chris Lattnereea52d32006-10-13 17:56:02 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetAsmInfo.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000025#include <map>
26using namespace llvm;
27
28namespace {
Chris Lattner95255282006-06-28 23:17:24 +000029 struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass {
Chris Lattnereea52d32006-10-13 17:56:02 +000030 /// OffsetMap - Mapping between BB and byte offset from start of function.
31 /// TODO: replace this with a vector, using the MBB idx as the key.
Misha Brukman999d9cf2004-07-27 18:33:06 +000032 std::map<MachineBasicBlock*, unsigned> OffsetMap;
33
Chris Lattner6f4a0722006-02-08 19:33:26 +000034 virtual bool runOnMachineFunction(MachineFunction &Fn);
Misha Brukman999d9cf2004-07-27 18:33:06 +000035
36 virtual const char *getPassName() const {
37 return "PowerPC Branch Selection";
38 }
39 };
40}
41
42/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
43/// Pass
44///
45FunctionPass *llvm::createPPCBranchSelectionPass() {
Chris Lattner6f4a0722006-02-08 19:33:26 +000046 return new PPCBSel();
Misha Brukman999d9cf2004-07-27 18:33:06 +000047}
Chris Lattner6f4a0722006-02-08 19:33:26 +000048
49/// getNumBytesForInstruction - Return the number of bytes of code the specified
50/// instruction may be. This returns the maximum number of bytes.
51///
52static unsigned getNumBytesForInstruction(MachineInstr *MI) {
53 switch (MI->getOpcode()) {
54 case PPC::COND_BRANCH:
Nate Begeman81e80972006-03-17 01:40:33 +000055 // while this will be 4 most of the time, if we emit 8 it is just a
Chris Lattner6f4a0722006-02-08 19:33:26 +000056 // minor pessimization that saves us from having to worry about
57 // keeping the offsets up to date later when we emit long branch glue.
Nate Begeman81e80972006-03-17 01:40:33 +000058 return 8;
Chris Lattner563ecfb2006-06-27 18:18:41 +000059 case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
60 case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
Chris Lattnereea52d32006-10-13 17:56:02 +000061 case PPC::IMPLICIT_DEF_F4: // no asm emitted
62 case PPC::IMPLICIT_DEF_F8: // no asm emitted
Chris Lattner6f4a0722006-02-08 19:33:26 +000063 return 0;
Chris Lattnereea52d32006-10-13 17:56:02 +000064 case PPC::INLINEASM: { // Inline Asm: Variable size.
65 MachineFunction *MF = MI->getParent()->getParent();
66 const char *AsmStr = MI->getOperand(0).getSymbolName();
67 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
68 }
Chris Lattner6f4a0722006-02-08 19:33:26 +000069 default:
70 return 4; // PowerPC instructions are all 4 bytes
71 }
72}
73
74
75bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
76 // Running total of instructions encountered since beginning of function
77 unsigned ByteCount = 0;
78
79 // For each MBB, add its offset to the offset map, and count up its
80 // instructions
81 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
82 ++MFI) {
83 MachineBasicBlock *MBB = MFI;
84 OffsetMap[MBB] = ByteCount;
85
86 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
87 MBBI != EE; ++MBBI)
88 ByteCount += getNumBytesForInstruction(MBBI);
89 }
90
91 // We're about to run over the MBB's again, so reset the ByteCount
92 ByteCount = 0;
93
94 // For each MBB, find the conditional branch pseudo instructions, and
95 // calculate the difference between the target MBB and the current ICount
96 // to decide whether or not to emit a short or long branch.
97 //
98 // short branch:
99 // bCC .L_TARGET_MBB
100 //
101 // long branch:
102 // bInverseCC $PC+8
103 // b .L_TARGET_MBB
Chris Lattner6f4a0722006-02-08 19:33:26 +0000104 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
105 ++MFI) {
106 MachineBasicBlock *MBB = MFI;
107
108 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
109 MBBI != EE; ++MBBI) {
110 // We may end up deleting the MachineInstr that MBBI points to, so
111 // remember its opcode now so we can refer to it after calling erase()
112 unsigned ByteSize = getNumBytesForInstruction(MBBI);
113 if (MBBI->getOpcode() == PPC::COND_BRANCH) {
114 MachineBasicBlock::iterator MBBJ = MBBI;
115 ++MBBJ;
116
117 // condbranch operands:
118 // 0. CR0 register
119 // 1. bc opcode
120 // 2. target MBB
121 // 3. fallthrough MBB
122 MachineBasicBlock *trueMBB =
123 MBBI->getOperand(2).getMachineBasicBlock();
Chris Lattner6f4a0722006-02-08 19:33:26 +0000124
125 int Displacement = OffsetMap[trueMBB] - ByteCount;
126 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
127 unsigned CRReg = MBBI->getOperand(0).getReg();
128 unsigned Inverted = PPCInstrInfo::invertPPCBranchOpcode(Opcode);
129
130 if (Displacement >= -32768 && Displacement <= 32767) {
131 BuildMI(*MBB, MBBJ, Opcode, 2).addReg(CRReg).addMBB(trueMBB);
132 } else {
Evan Cheng1822ffb2006-08-25 23:29:06 +0000133 // Long branch, skip next branch instruction (i.e. $PC+8).
Evan Chengb9ca9262006-08-25 21:54:44 +0000134 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(CRReg).addImm(2);
Chris Lattner6f4a0722006-02-08 19:33:26 +0000135 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
Chris Lattner6f4a0722006-02-08 19:33:26 +0000136 }
137
138 // Erase the psuedo COND_BRANCH instruction, and then back up the
139 // iterator so that when the for loop increments it, we end up in
140 // the correct place rather than iterating off the end.
141 MBB->erase(MBBI);
142 MBBI = --MBBJ;
143 }
144 ByteCount += ByteSize;
145 }
146 }
147
148 OffsetMap.clear();
149 return true;
150}
151