blob: 5eaf38f01886af35b05342a50f35f0dbc2d1fad4 [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;
45 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +000046 default:
Misha Brukman999d9cf2004-07-27 18:33:06 +000047 return 4; // PowerPC instructions are all 4 bytes
48 break;
49 }
50 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +000051
Misha Brukman999d9cf2004-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 Brukmanb5f662f2005-04-21 23:30:14 +000058 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
Misha Brukman999d9cf2004-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 Brukmanb5f662f2005-04-21 23:30:14 +000083 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
Misha Brukman999d9cf2004-07-27 18:33:06 +000084 ++MFI) {
85 MachineBasicBlock *MBB = MFI;
Misha Brukmanb5f662f2005-04-21 23:30:14 +000086
Misha Brukman999d9cf2004-07-27 18:33:06 +000087 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
88 MBBI != EE; ++MBBI) {
Misha Brukman5b570812004-08-10 22:47:03 +000089 if (MBBI->getOpcode() == PPC::COND_BRANCH) {
Misha Brukman999d9cf2004-07-27 18:33:06 +000090 // condbranch operands:
91 // 0. CR0 register
92 // 1. bc opcode
93 // 2. target MBB
94 // 3. fallthrough MBB
Misha Brukmanb5f662f2005-04-21 23:30:14 +000095 MachineBasicBlock *trueMBB =
Misha Brukman999d9cf2004-07-27 18:33:06 +000096 MBBI->getOperand(2).getMachineBasicBlock();
Misha Brukmanb5f662f2005-04-21 23:30:14 +000097 MachineBasicBlock *falseMBB =
Misha Brukman999d9cf2004-07-27 18:33:06 +000098 MBBI->getOperand(3).getMachineBasicBlock();
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Misha Brukman999d9cf2004-07-27 18:33:06 +0000100 int Displacement = OffsetMap[trueMBB] - ByteCount;
101 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
Misha Brukman363dd072004-08-17 04:58:50 +0000102 unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000103
104 MachineInstr *MI = MBBI;
105 if (Displacement >= -32768 && Displacement <= 32767) {
Misha Brukman5b570812004-08-10 22:47:03 +0000106 BuildMI(*MBB, MBBI, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000107 } else {
Misha Brukman5b570812004-08-10 22:47:03 +0000108 BuildMI(*MBB, MBBI, Inverted, 2).addReg(PPC::CR0).addSImm(8);
109 BuildMI(*MBB, MBBI, PPC::B, 1).addMBB(trueMBB);
110 BuildMI(*MBB, MBBI, PPC::B, 1).addMBB(falseMBB);
Misha Brukman999d9cf2004-07-27 18:33:06 +0000111 }
112 MBB->erase(MI);
113 }
114 ByteCount += bytesForOpcode(MBBI->getOpcode());
115 }
116 }
117
118 OffsetMap.clear();
119 return true;
120 }
121
122 virtual const char *getPassName() const {
123 return "PowerPC Branch Selection";
124 }
125 };
126}
127
128/// createPPCBranchSelectionPass - returns an instance of the Branch Selection
129/// Pass
130///
131FunctionPass *llvm::createPPCBranchSelectionPass() {
132 return new BSel();
133}