Convert remaining X-Form and Pseudo instructions over to asm writer
[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/CodeGen/MachineFunction.h"
25 #include "llvm/Support/Debug.h"
26 #include <map>
27 using namespace llvm;
28
29 namespace {
30   struct BSel : public MachineFunctionPass {
31     // OffsetMap - Mapping between BB and byte offset from start of function
32     std::map<MachineBasicBlock*, unsigned> OffsetMap;
33
34     /// bytesForOpcode - A convenience function for totalling up the number of 
35     /// bytes in a basic block.
36     ///
37     static unsigned bytesForOpcode(unsigned opcode) {
38       switch (opcode) {
39       case PPC::COND_BRANCH:
40         // while this will be 4 most of the time, if we emit 12 it is just a
41         // minor pessimization that saves us from having to worry about 
42         // keeping the offsets up to date later when we emit long branch glue.
43         return 12;
44       case PPC::IMPLICIT_DEF: // no asm emitted
45         return 0;
46         break;
47       default: 
48         return 4; // PowerPC instructions are all 4 bytes
49         break;
50       }
51     }
52     
53     virtual bool runOnMachineFunction(MachineFunction &Fn) {
54       // Running total of instructions encountered since beginning of function
55       unsigned ByteCount = 0;
56
57       // For each MBB, add its offset to the offset map, and count up its
58       // instructions
59       for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; 
60            ++MFI) {
61         MachineBasicBlock *MBB = MFI;
62         OffsetMap[MBB] = ByteCount;
63
64         for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
65              MBBI != EE; ++MBBI)
66           ByteCount += bytesForOpcode(MBBI->getOpcode());
67       }
68
69       // We're about to run over the MBB's again, so reset the ByteCount
70       ByteCount = 0;
71
72       // For each MBB, find the conditional branch pseudo instructions, and
73       // calculate the difference between the target MBB and the current ICount
74       // to decide whether or not to emit a short or long branch.
75       //
76       // short branch:
77       // bCC .L_TARGET_MBB
78       //
79       // long branch:
80       // bInverseCC $PC+8
81       // b .L_TARGET_MBB
82       // b .L_FALLTHROUGH_MBB
83
84       for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; 
85            ++MFI) {
86         MachineBasicBlock *MBB = MFI;
87         
88         for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
89              MBBI != EE; ++MBBI) {
90           if (MBBI->getOpcode() == PPC::COND_BRANCH) {
91             // condbranch operands:
92             // 0. CR0 register
93             // 1. bc opcode
94             // 2. target MBB
95             // 3. fallthrough MBB
96             MachineBasicBlock *trueMBB = 
97               MBBI->getOperand(2).getMachineBasicBlock();
98             MachineBasicBlock *falseMBB = 
99               MBBI->getOperand(3).getMachineBasicBlock();
100             
101             int Displacement = OffsetMap[trueMBB] - ByteCount;
102             unsigned Opcode = MBBI->getOperand(1).getImmedValue();
103             unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
104
105             MachineInstr *MI = MBBI;
106             if (Displacement >= -32768 && Displacement <= 32767) {
107               BuildMI(*MBB, MBBI, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
108             } else {
109               BuildMI(*MBB, MBBI, Inverted, 2).addReg(PPC::CR0).addSImm(8);
110               BuildMI(*MBB, MBBI, PPC::B, 1).addMBB(trueMBB);
111               BuildMI(*MBB, MBBI, PPC::B, 1).addMBB(falseMBB);
112             }
113             MBB->erase(MI);
114           }
115           ByteCount += bytesForOpcode(MBBI->getOpcode());
116         }
117       }
118
119       OffsetMap.clear();
120       return true;
121     }
122
123     virtual const char *getPassName() const {
124       return "PowerPC Branch Selection";
125     }
126   };
127 }
128
129 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
130 /// Pass
131 ///
132 FunctionPass *llvm::createPPCBranchSelectionPass() {
133   return new BSel();
134 }