blob: 0c07d816218731da3c443680878dbc37703e2c65 [file] [log] [blame]
Misha Brukmanef8cf022004-07-27 18:33:06 +00001//===-- PowerPCBranchSelector.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
18#define DEBUG_TYPE "bsel"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000019#include "PPC.h"
Chris Lattnere80bf1b2005-10-14 23:45:43 +000020#include "PPCInstrBuilder.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000021#include "PPCInstrInfo.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Misha Brukmanef8cf022004-07-27 18:33:06 +000024#include <map>
25using namespace llvm;
26
27namespace {
28 struct BSel : public MachineFunctionPass {
29 // OffsetMap - Mapping between BB and byte offset from start of function
30 std::map<MachineBasicBlock*, unsigned> OffsetMap;
31
Misha Brukmanb4402432005-04-21 23:30:14 +000032 /// bytesForOpcode - A convenience function for totalling up the number of
Misha Brukmanef8cf022004-07-27 18:33:06 +000033 /// bytes in a basic block.
34 ///
35 static unsigned bytesForOpcode(unsigned opcode) {
36 switch (opcode) {
Misha Brukmandad438b2004-08-10 22:47:03 +000037 case PPC::COND_BRANCH:
Misha Brukmanef8cf022004-07-27 18:33:06 +000038 // while this will be 4 most of the time, if we emit 12 it is just a
Misha Brukmanb4402432005-04-21 23:30:14 +000039 // minor pessimization that saves us from having to worry about
Misha Brukmanef8cf022004-07-27 18:33:06 +000040 // keeping the offsets up to date later when we emit long branch glue.
41 return 12;
Chris Lattnera3fbdae2005-08-24 23:08:16 +000042 case PPC::IMPLICIT_DEF_GPR: // no asm emitted
Chris Lattnerd3eee1a2005-10-01 01:35:02 +000043 case PPC::IMPLICIT_DEF_F4: // no asm emitted
44 case PPC::IMPLICIT_DEF_F8: // no asm emitted
Misha Brukmanef8cf022004-07-27 18:33:06 +000045 return 0;
Misha Brukmanb4402432005-04-21 23:30:14 +000046 default:
Reid Spencer4fdd96c2005-06-18 17:37:34 +000047 break;
Misha Brukmanef8cf022004-07-27 18:33:06 +000048 }
Reid Spencer4fdd96c2005-06-18 17:37:34 +000049 return 4; // PowerPC instructions are all 4 bytes
Misha Brukmanef8cf022004-07-27 18:33:06 +000050 }
Misha Brukmanb4402432005-04-21 23:30:14 +000051
Misha Brukmanef8cf022004-07-27 18:33:06 +000052 virtual bool runOnMachineFunction(MachineFunction &Fn) {
53 // Running total of instructions encountered since beginning of function
54 unsigned ByteCount = 0;
55
56 // For each MBB, add its offset to the offset map, and count up its
57 // instructions
Misha Brukmanb4402432005-04-21 23:30:14 +000058 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
Misha Brukmanef8cf022004-07-27 18:33:06 +000059 ++MFI) {
60 MachineBasicBlock *MBB = MFI;
61 OffsetMap[MBB] = ByteCount;
62
63 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
64 MBBI != EE; ++MBBI)
65 ByteCount += bytesForOpcode(MBBI->getOpcode());
66 }
67
68 // We're about to run over the MBB's again, so reset the ByteCount
69 ByteCount = 0;
70
71 // For each MBB, find the conditional branch pseudo instructions, and
72 // calculate the difference between the target MBB and the current ICount
73 // to decide whether or not to emit a short or long branch.
74 //
75 // short branch:
76 // bCC .L_TARGET_MBB
77 //
78 // long branch:
79 // bInverseCC $PC+8
80 // b .L_TARGET_MBB
81 // b .L_FALLTHROUGH_MBB
82
Misha Brukmanb4402432005-04-21 23:30:14 +000083 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
Misha Brukmanef8cf022004-07-27 18:33:06 +000084 ++MFI) {
85 MachineBasicBlock *MBB = MFI;
Misha Brukmanb4402432005-04-21 23:30:14 +000086
Misha Brukmanef8cf022004-07-27 18:33:06 +000087 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
88 MBBI != EE; ++MBBI) {
Nate Begeman02e33b72005-06-12 23:50:33 +000089 // We may end up deleting the MachineInstr that MBBI points to, so
90 // remember its opcode now so we can refer to it after calling erase()
91 unsigned OpcodeToReplace = MBBI->getOpcode();
92
93 if (OpcodeToReplace == PPC::COND_BRANCH) {
94 MachineBasicBlock::iterator MBBJ = MBBI;
95 ++MBBJ;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000096
Misha Brukmanef8cf022004-07-27 18:33:06 +000097 // condbranch operands:
98 // 0. CR0 register
99 // 1. bc opcode
100 // 2. target MBB
101 // 3. fallthrough MBB
Misha Brukmanb4402432005-04-21 23:30:14 +0000102 MachineBasicBlock *trueMBB =
Misha Brukmanef8cf022004-07-27 18:33:06 +0000103 MBBI->getOperand(2).getMachineBasicBlock();
Misha Brukmanb4402432005-04-21 23:30:14 +0000104 MachineBasicBlock *falseMBB =
Misha Brukmanef8cf022004-07-27 18:33:06 +0000105 MBBI->getOperand(3).getMachineBasicBlock();
Misha Brukmanb4402432005-04-21 23:30:14 +0000106
Misha Brukmanef8cf022004-07-27 18:33:06 +0000107 int Displacement = OffsetMap[trueMBB] - ByteCount;
108 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
Chris Lattner29bfaa72005-08-26 23:41:27 +0000109 unsigned CRReg = MBBI->getOperand(0).getReg();
Misha Brukman06741822004-08-17 04:58:50 +0000110 unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
Misha Brukmanef8cf022004-07-27 18:33:06 +0000111
Misha Brukmanef8cf022004-07-27 18:33:06 +0000112 if (Displacement >= -32768 && Displacement <= 32767) {
Chris Lattner29bfaa72005-08-26 23:41:27 +0000113 BuildMI(*MBB, MBBJ, Opcode, 2).addReg(CRReg).addMBB(trueMBB);
Misha Brukmanef8cf022004-07-27 18:33:06 +0000114 } else {
Chris Lattner29bfaa72005-08-26 23:41:27 +0000115 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(CRReg).addSImm(8);
Nate Begeman02e33b72005-06-12 23:50:33 +0000116 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
117 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(falseMBB);
Misha Brukmanef8cf022004-07-27 18:33:06 +0000118 }
Nate Begeman02e33b72005-06-12 23:50:33 +0000119
120 // Erase the psuedo COND_BRANCH instruction, and then back up the
121 // iterator so that when the for loop increments it, we end up in
122 // the correct place rather than iterating off the end.
123 MBB->erase(MBBI);
124 MBBI = --MBBJ;
Misha Brukmanef8cf022004-07-27 18:33:06 +0000125 }
Nate Begeman02e33b72005-06-12 23:50:33 +0000126 ByteCount += bytesForOpcode(OpcodeToReplace);
Misha Brukmanef8cf022004-07-27 18:33:06 +0000127 }
128 }
129
130 OffsetMap.clear();
131 return true;
132 }
133
134 virtual const char *getPassName() const {
135 return "PowerPC Branch Selection";
136 }
137 };
138}
139
140/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
141/// Pass
142///
143FunctionPass *llvm::createPPCBranchSelectionPass() {
144 return new BSel();
145}