Implement 64-bit undef, sub, shl/shr, srem/urem
[oota-llvm.git] / lib / Target / PowerPC / PPCBranchSelector.cpp
1 //===-- PPCBranchSelector.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 #include "PPC.h"
19 #include "PPCInstrBuilder.h"
20 #include "PPCInstrInfo.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include <map>
23 using namespace llvm;
24
25 namespace {
26   struct PPCBSel : public MachineFunctionPass {
27     // OffsetMap - Mapping between BB and byte offset from start of function
28     std::map<MachineBasicBlock*, unsigned> OffsetMap;
29
30     virtual bool runOnMachineFunction(MachineFunction &Fn);
31
32     virtual const char *getPassName() const {
33       return "PowerPC Branch Selection";
34     }
35   };
36 }
37
38 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
39 /// Pass
40 ///
41 FunctionPass *llvm::createPPCBranchSelectionPass() {
42   return new PPCBSel();
43 }
44
45 /// getNumBytesForInstruction - Return the number of bytes of code the specified
46 /// instruction may be.  This returns the maximum number of bytes.
47 ///
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.
54     return 8;
55   case PPC::IMPLICIT_DEF_GPRC: // no asm emitted
56   case PPC::IMPLICIT_DEF_G8RC: // no asm emitted
57   case PPC::IMPLICIT_DEF_F4: // no asm emitted
58   case PPC::IMPLICIT_DEF_F8: // no asm emitted
59     return 0;
60   case PPC::INLINEASM:    // Inline Asm: Variable size.
61     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
62       if (MI->getOperand(i).isExternalSymbol()) {
63         const char *AsmStr = MI->getOperand(i).getSymbolName();
64         // Count the number of newline's in the asm string.
65         unsigned NumInstrs = 0;
66         for (; *AsmStr; ++AsmStr)
67           NumInstrs += *AsmStr == '\n';
68         return NumInstrs*4;
69       }
70     assert(0 && "INLINEASM didn't have format string??");
71   default:
72     return 4; // PowerPC instructions are all 4 bytes
73   }
74 }
75
76
77 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
78   // Running total of instructions encountered since beginning of function
79   unsigned ByteCount = 0;
80   
81   // For each MBB, add its offset to the offset map, and count up its
82   // instructions
83   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
84        ++MFI) {
85     MachineBasicBlock *MBB = MFI;
86     OffsetMap[MBB] = ByteCount;
87     
88     for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
89          MBBI != EE; ++MBBI)
90       ByteCount += getNumBytesForInstruction(MBBI);
91   }
92   
93   // We're about to run over the MBB's again, so reset the ByteCount
94   ByteCount = 0;
95   
96   // For each MBB, find the conditional branch pseudo instructions, and
97   // calculate the difference between the target MBB and the current ICount
98   // to decide whether or not to emit a short or long branch.
99   //
100   // short branch:
101   // bCC .L_TARGET_MBB
102   //
103   // long branch:
104   // bInverseCC $PC+8
105   // b .L_TARGET_MBB
106   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
107        ++MFI) {
108     MachineBasicBlock *MBB = MFI;
109     
110     for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
111          MBBI != EE; ++MBBI) {
112       // We may end up deleting the MachineInstr that MBBI points to, so
113       // remember its opcode now so we can refer to it after calling erase()
114       unsigned ByteSize = getNumBytesForInstruction(MBBI);
115       if (MBBI->getOpcode() == PPC::COND_BRANCH) {
116         MachineBasicBlock::iterator MBBJ = MBBI;
117         ++MBBJ;
118         
119         // condbranch operands:
120         // 0. CR0 register
121         // 1. bc opcode
122         // 2. target MBB
123         // 3. fallthrough MBB
124         MachineBasicBlock *trueMBB =
125           MBBI->getOperand(2).getMachineBasicBlock();
126         
127         int Displacement = OffsetMap[trueMBB] - ByteCount;
128         unsigned Opcode = MBBI->getOperand(1).getImmedValue();
129         unsigned CRReg = MBBI->getOperand(0).getReg();
130         unsigned Inverted = PPCInstrInfo::invertPPCBranchOpcode(Opcode);
131         
132         if (Displacement >= -32768 && Displacement <= 32767) {
133           BuildMI(*MBB, MBBJ, Opcode, 2).addReg(CRReg).addMBB(trueMBB);
134         } else {
135           BuildMI(*MBB, MBBJ, Inverted, 2).addReg(CRReg).addImm(8);
136           BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
137         }
138         
139         // Erase the psuedo COND_BRANCH instruction, and then back up the
140         // iterator so that when the for loop increments it, we end up in
141         // the correct place rather than iterating off the end.
142         MBB->erase(MBBI);
143         MBBI = --MBBJ;
144       }
145       ByteCount += ByteSize;
146     }
147   }
148   
149   OffsetMap.clear();
150   return true;
151 }
152