1 //===-- PowerPCBranchSelector.cpp - Emit long conditional branches-*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that scans a machine function to determine which
11 // 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.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "bsel"
20 #include "PowerPCInstrBuilder.h"
21 #include "PowerPCInstrInfo.h"
22 #include "PPC32InstrInfo.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/Support/Debug.h"
29 struct BSel : public MachineFunctionPass {
30 // OffsetMap - Mapping between BB and byte offset from start of function
31 std::map<MachineBasicBlock*, unsigned> OffsetMap;
33 /// bytesForOpcode - A convenience function for totalling up the number of
34 /// bytes in a basic block.
36 static unsigned bytesForOpcode(unsigned opcode) {
38 case PPC::COND_BRANCH:
39 // while this will be 4 most of the time, if we emit 12 it is just a
40 // minor pessimization that saves us from having to worry about
41 // keeping the offsets up to date later when we emit long branch glue.
43 case PPC::IMPLICIT_DEF_GPR: // no asm emitted
44 case PPC::IMPLICIT_DEF_FP: // no asm emitted
49 return 4; // PowerPC instructions are all 4 bytes
52 virtual bool runOnMachineFunction(MachineFunction &Fn) {
53 // Running total of instructions encountered since beginning of function
54 unsigned ByteCount = 0;
56 // For each MBB, add its offset to the offset map, and count up its
58 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
60 MachineBasicBlock *MBB = MFI;
61 OffsetMap[MBB] = ByteCount;
63 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
65 ByteCount += bytesForOpcode(MBBI->getOpcode());
68 // We're about to run over the MBB's again, so reset the ByteCount
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.
81 // b .L_FALLTHROUGH_MBB
83 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
85 MachineBasicBlock *MBB = MFI;
87 for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
89 // 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();
93 if (OpcodeToReplace == PPC::COND_BRANCH) {
94 MachineBasicBlock::iterator MBBJ = MBBI;
97 // condbranch operands:
101 // 3. fallthrough MBB
102 MachineBasicBlock *trueMBB =
103 MBBI->getOperand(2).getMachineBasicBlock();
104 MachineBasicBlock *falseMBB =
105 MBBI->getOperand(3).getMachineBasicBlock();
107 int Displacement = OffsetMap[trueMBB] - ByteCount;
108 unsigned Opcode = MBBI->getOperand(1).getImmedValue();
109 unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
111 if (Displacement >= -32768 && Displacement <= 32767) {
112 BuildMI(*MBB, MBBJ, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
114 BuildMI(*MBB, MBBJ, Inverted, 2).addReg(PPC::CR0).addSImm(8);
115 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
116 BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(falseMBB);
119 // Erase the psuedo COND_BRANCH instruction, and then back up the
120 // iterator so that when the for loop increments it, we end up in
121 // the correct place rather than iterating off the end.
125 ByteCount += bytesForOpcode(OpcodeToReplace);
133 virtual const char *getPassName() const {
134 return "PowerPC Branch Selection";
139 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
142 FunctionPass *llvm::createPPCBranchSelectionPass() {