blob: fdf1dd338a7cbb413e2060caa977f42656631d68 [file] [log] [blame]
Misha Brukman999d9cf2004-07-27 18:33:06 +00001//===-- PowerPCBranchSelector.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
18#define DEBUG_TYPE "bsel"
19#include "PowerPC.h"
20#include "PowerPCInstrBuilder.h"
21#include "PowerPCInstrInfo.h"
Misha Brukman363dd072004-08-17 04:58:50 +000022#include "PPC32InstrInfo.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/Debug.h"
Misha Brukman999d9cf2004-07-27 18:33:06 +000025#include <map>
26using namespace llvm;
27
28namespace {
29 struct BSel : public MachineFunctionPass {
30 // OffsetMap - Mapping between BB and byte offset from start of function
31 std::map<MachineBasicBlock*, unsigned> OffsetMap;
32
Misha Brukmanb5f662f2005-04-21 23:30:14 +000033 /// bytesForOpcode - A convenience function for totalling up the number of
Misha Brukman999d9cf2004-07-27 18:33:06 +000034 /// bytes in a basic block.
35 ///
36 static unsigned bytesForOpcode(unsigned opcode) {
37 switch (opcode) {
Misha Brukman5b570812004-08-10 22:47:03 +000038 case PPC::COND_BRANCH:
Misha Brukman999d9cf2004-07-27 18:33:06 +000039 // while this will be 4 most of the time, if we emit 12 it is just a
Misha Brukmanb5f662f2005-04-21 23:30:14 +000040 // minor pessimization that saves us from having to worry about
Misha Brukman999d9cf2004-07-27 18:33:06 +000041 // keeping the offsets up to date later when we emit long branch glue.
42 return 12;
Misha Brukman5b570812004-08-10 22:47:03 +000043 case PPC::IMPLICIT_DEF: // no asm emitted
Misha Brukman999d9cf2004-07-27 18:33:06 +000044 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +000045 default:
Reid Spencer4b828e62005-06-18 17:37:34 +000046 break;
Misha Brukman999d9cf2004-07-27 18:33:06 +000047 }
Reid Spencer4b828e62005-06-18 17:37:34 +000048 return 4; // PowerPC instructions are all 4 bytes
Misha Brukman999d9cf2004-07-27 18:33:06 +000049 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +000050
Misha Brukman999d9cf2004-07-27 18:33:06 +000051 virtual bool runOnMachineFunction(MachineFunction &Fn) {
52 // Running total of instructions encountered since beginning of function
53 unsigned ByteCount = 0;
54
55 // For each MBB, add its offset to the offset map, and count up its
56 // instructions
Misha Brukmanb5f662f2005-04-21 23:30:14 +000057 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
Misha Brukman999d9cf2004-07-27 18:33:06 +000058 ++MFI) {
59 MachineBasicBlock *MBB = MFI;
60 OffsetMap[MBB] = ByteCount;
61
62 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
63 MBBI != EE; ++MBBI)
64 ByteCount += bytesForOpcode(MBBI->getOpcode());
65 }
66
67 // We're about to run over the MBB's again, so reset the ByteCount
68 ByteCount = 0;
69
70 // For each MBB, find the conditional branch pseudo instructions, and
71 // calculate the difference between the target MBB and the current ICount
72 // to decide whether or not to emit a short or long branch.
73 //
74 // short branch:
75 // bCC .L_TARGET_MBB
76 //
77 // long branch:
78 // bInverseCC $PC+8
79 // b .L_TARGET_MBB
80 // b .L_FALLTHROUGH_MBB
81
Misha Brukmanb5f662f2005-04-21 23:30:14 +000082 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
Misha Brukman999d9cf2004-07-27 18:33:06 +000083 ++MFI) {
84 MachineBasicBlock *MBB = MFI;
Misha Brukmanb5f662f2005-04-21 23:30:14 +000085
Misha Brukman999d9cf2004-07-27 18:33:06 +000086 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
87 MBBI != EE; ++MBBI) {
Nate Begemanac609dd2005-06-12 23:50:33 +000088 // We may end up deleting the MachineInstr that MBBI points to, so
89 // remember its opcode now so we can refer to it after calling erase()
90 unsigned OpcodeToReplace = MBBI->getOpcode();
91
92 if (OpcodeToReplace == PPC::COND_BRANCH) {
93 MachineBasicBlock::iterator MBBJ = MBBI;
94 ++MBBJ;
95
Misha Brukman999d9cf2004-07-27 18:33:06 +000096 // condbranch operands:
97 // 0. CR0 register
98 // 1. bc opcode
99 // 2. target MBB
100 // 3. fallthrough MBB
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000101 MachineBasicBlock *trueMBB =
Misha Brukman999d9cf2004-07-27 18:33:06 +0000102 MBBI->getOperand(2).getMachineBasicBlock();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000103 MachineBasicBlock *falseMBB =
Misha Brukman999d9cf2004-07-27 18:33:06 +0000104 MBBI->getOperand(3).getMachineBasicBlock();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000105
Misha Brukman999d9cf2004-07-27 18:33:06 +0000106 int Displacement = OffsetMap[trueMBB] - ByteCount;
107 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
Misha Brukman363dd072004-08-17 04:58:50 +0000108 unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000109
Misha Brukman999d9cf2004-07-27 18:33:06 +0000110 if (Displacement >= -32768 && Displacement <= 32767) {
Nate Begemanac609dd2005-06-12 23:50:33 +0000111 BuildMI(*MBB, MBBJ, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000112 } else {
Nate Begemanac609dd2005-06-12 23:50:33 +0000113 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(PPC::CR0).addSImm(8);
114 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
115 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(falseMBB);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000116 }
Nate Begemanac609dd2005-06-12 23:50:33 +0000117
118 // Erase the psuedo COND_BRANCH instruction, and then back up the
119 // iterator so that when the for loop increments it, we end up in
120 // the correct place rather than iterating off the end.
121 MBB->erase(MBBI);
122 MBBI = --MBBJ;
Misha Brukman999d9cf2004-07-27 18:33:06 +0000123 }
Nate Begemanac609dd2005-06-12 23:50:33 +0000124 ByteCount += bytesForOpcode(OpcodeToReplace);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000125 }
126 }
127
128 OffsetMap.clear();
129 return true;
130 }
131
132 virtual const char *getPassName() const {
133 return "PowerPC Branch Selection";
134 }
135 };
136}
137
138/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
139/// Pass
140///
141FunctionPass *llvm::createPPCBranchSelectionPass() {
142 return new BSel();
143}