silence incredibly braindead GCC 4 warning
[oota-llvm.git] / lib / Target / PowerPC / PPCBranchSelector.cpp
1 //===-- PowerPCBranchSelector.cpp - Emit long conditional branches-*- C++ -*-=//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #define DEBUG_TYPE "bsel"
19 #include "PowerPC.h"
20 #include "PowerPCInstrBuilder.h"
21 #include "PowerPCInstrInfo.h"
22 #include "PPC32InstrInfo.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/Support/Debug.h"
25 #include <map>
26 using namespace llvm;
27
28 namespace {
29   struct BSel : public MachineFunctionPass {
30     // OffsetMap - Mapping between BB and byte offset from start of function
31     std::map<MachineBasicBlock*, unsigned> OffsetMap;
32
33     /// bytesForOpcode - A convenience function for totalling up the number of
34     /// bytes in a basic block.
35     ///
36     static unsigned bytesForOpcode(unsigned opcode) {
37       switch (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.
42         return 12;
43       case PPC::IMPLICIT_DEF: // no asm emitted
44         return 0;
45       default:
46         return 4; // PowerPC instructions are all 4 bytes
47       }
48     }
49
50     virtual bool runOnMachineFunction(MachineFunction &Fn) {
51       // Running total of instructions encountered since beginning of function
52       unsigned ByteCount = 0;
53
54       // For each MBB, add its offset to the offset map, and count up its
55       // instructions
56       for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
57            ++MFI) {
58         MachineBasicBlock *MBB = MFI;
59         OffsetMap[MBB] = ByteCount;
60
61         for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
62              MBBI != EE; ++MBBI)
63           ByteCount += bytesForOpcode(MBBI->getOpcode());
64       }
65
66       // We're about to run over the MBB's again, so reset the ByteCount
67       ByteCount = 0;
68
69       // For each MBB, find the conditional branch pseudo instructions, and
70       // calculate the difference between the target MBB and the current ICount
71       // to decide whether or not to emit a short or long branch.
72       //
73       // short branch:
74       // bCC .L_TARGET_MBB
75       //
76       // long branch:
77       // bInverseCC $PC+8
78       // b .L_TARGET_MBB
79       // b .L_FALLTHROUGH_MBB
80
81       for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
82            ++MFI) {
83         MachineBasicBlock *MBB = MFI;
84
85         for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
86              MBBI != EE; ++MBBI) {
87           // We may end up deleting the MachineInstr that MBBI points to, so
88           // remember its opcode now so we can refer to it after calling erase()
89           unsigned OpcodeToReplace = MBBI->getOpcode();
90
91           if (OpcodeToReplace == PPC::COND_BRANCH) {
92             MachineBasicBlock::iterator MBBJ = MBBI;
93             ++MBBJ;
94             
95             // condbranch operands:
96             // 0. CR0 register
97             // 1. bc opcode
98             // 2. target MBB
99             // 3. fallthrough MBB
100             MachineBasicBlock *trueMBB =
101               MBBI->getOperand(2).getMachineBasicBlock();
102             MachineBasicBlock *falseMBB =
103               MBBI->getOperand(3).getMachineBasicBlock();
104
105             int Displacement = OffsetMap[trueMBB] - ByteCount;
106             unsigned Opcode = MBBI->getOperand(1).getImmedValue();
107             unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
108
109             if (Displacement >= -32768 && Displacement <= 32767) {
110               BuildMI(*MBB, MBBJ, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
111             } else {
112               BuildMI(*MBB, MBBJ, Inverted, 2).addReg(PPC::CR0).addSImm(8);
113               BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
114               BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(falseMBB);
115             }
116
117             // Erase the psuedo COND_BRANCH instruction, and then back up the
118             // iterator so that when the for loop increments it, we end up in
119             // the correct place rather than iterating off the end.
120             MBB->erase(MBBI);
121             MBBI = --MBBJ;
122           }
123           ByteCount += bytesForOpcode(OpcodeToReplace);
124         }
125       }
126
127       OffsetMap.clear();
128       return true;
129     }
130
131     virtual const char *getPassName() const {
132       return "PowerPC Branch Selection";
133     }
134   };
135 }
136
137 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
138 /// Pass
139 ///
140 FunctionPass *llvm::createPPCBranchSelectionPass() {
141   return new BSel();
142 }