1 //===-- PPCBranchSelector.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 //===----------------------------------------------------------------------===//
19 #include "PPCInstrBuilder.h"
20 #include "PPCInstrInfo.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
26 struct PPCBSel
: public MachineFunctionPass
{
27 // OffsetMap - Mapping between BB and byte offset from start of function
28 std::map
<MachineBasicBlock
*, unsigned> OffsetMap
;
30 virtual bool runOnMachineFunction(MachineFunction
&Fn
);
32 virtual const char *getPassName() const {
33 return "PowerPC Branch Selection";
38 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
41 FunctionPass
*llvm::createPPCBranchSelectionPass() {
45 /// getNumBytesForInstruction - Return the number of bytes of code the specified
46 /// instruction may be. This returns the maximum number of bytes.
48 static unsigned getNumBytesForInstruction(MachineInstr
*MI
) {
49 switch (MI
->getOpcode()) {
50 case PPC::COND_BRANCH
:
51 // while this will be 4 most of the time, if we emit 8 it is just a
52 // minor pessimization that saves us from having to worry about
53 // keeping the offsets up to date later when we emit long branch glue.
55 case PPC::IMPLICIT_DEF_GPR
: // no asm emitted
56 case PPC::IMPLICIT_DEF_F4
: // no asm emitted
57 case PPC::IMPLICIT_DEF_F8
: // no asm emitted
59 case PPC::INLINEASM
: // Inline Asm: Variable size.
60 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
)
61 if (MI
->getOperand(i
).isExternalSymbol()) {
62 const char *AsmStr
= MI
->getOperand(i
).getSymbolName();
63 // Count the number of newline's in the asm string.
64 unsigned NumInstrs
= 0;
65 for (; *AsmStr
; ++AsmStr
)
66 NumInstrs
+= *AsmStr
== '\n';
69 assert(0 && "INLINEASM didn't have format string??");
71 return 4; // PowerPC instructions are all 4 bytes
76 bool PPCBSel::runOnMachineFunction(MachineFunction
&Fn
) {
77 // Running total of instructions encountered since beginning of function
78 unsigned ByteCount
= 0;
80 // For each MBB, add its offset to the offset map, and count up its
82 for (MachineFunction::iterator MFI
= Fn
.begin(), E
= Fn
.end(); MFI
!= E
;
84 MachineBasicBlock
*MBB
= MFI
;
85 OffsetMap
[MBB
] = ByteCount
;
87 for (MachineBasicBlock::iterator MBBI
= MBB
->begin(), EE
= MBB
->end();
89 ByteCount
+= getNumBytesForInstruction(MBBI
);
92 // We're about to run over the MBB's again, so reset the ByteCount
95 // For each MBB, find the conditional branch pseudo instructions, and
96 // calculate the difference between the target MBB and the current ICount
97 // to decide whether or not to emit a short or long branch.
105 for (MachineFunction::iterator MFI
= Fn
.begin(), E
= Fn
.end(); MFI
!= E
;
107 MachineBasicBlock
*MBB
= MFI
;
109 for (MachineBasicBlock::iterator MBBI
= MBB
->begin(), EE
= MBB
->end();
110 MBBI
!= EE
; ++MBBI
) {
111 // We may end up deleting the MachineInstr that MBBI points to, so
112 // remember its opcode now so we can refer to it after calling erase()
113 unsigned ByteSize
= getNumBytesForInstruction(MBBI
);
114 if (MBBI
->getOpcode() == PPC::COND_BRANCH
) {
115 MachineBasicBlock::iterator MBBJ
= MBBI
;
118 // condbranch operands:
122 // 3. fallthrough MBB
123 MachineBasicBlock
*trueMBB
=
124 MBBI
->getOperand(2).getMachineBasicBlock();
126 int Displacement
= OffsetMap
[trueMBB
] - ByteCount
;
127 unsigned Opcode
= MBBI
->getOperand(1).getImmedValue();
128 unsigned CRReg
= MBBI
->getOperand(0).getReg();
129 unsigned Inverted
= PPCInstrInfo::invertPPCBranchOpcode(Opcode
);
131 if (Displacement
>= -32768 && Displacement
<= 32767) {
132 BuildMI(*MBB
, MBBJ
, Opcode
, 2).addReg(CRReg
).addMBB(trueMBB
);
134 BuildMI(*MBB
, MBBJ
, Inverted
, 2).addReg(CRReg
).addSImm(8);
135 BuildMI(*MBB
, MBBJ
, PPC::B
, 1).addMBB(trueMBB
);
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.
144 ByteCount
+= ByteSize
;