JumpTable support! What this represents is working asm and jit support for
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAGISel.h
1 //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SelectionDAGISel class, which is used as the common
11 // base class for SelectionDAG-based instruction selectors.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_ISEL_H
16 #define LLVM_CODEGEN_SELECTIONDAG_ISEL_H
17
18 #include "llvm/Pass.h"
19 #include "llvm/Constant.h"
20 #include "llvm/CodeGen/SelectionDAGNodes.h"
21 #include <set>
22
23 namespace llvm {
24   class SelectionDAG;
25   class SelectionDAGLowering;
26   class SDOperand;
27   class SSARegMap;
28   class MachineBasicBlock;
29   class MachineFunction;
30   class MachineInstr;
31   class TargetLowering;
32   class FunctionLoweringInfo;
33   class HazardRecognizer;
34
35 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
36 /// pattern-matching instruction selectors.
37 class SelectionDAGISel : public FunctionPass {
38 public:
39   TargetLowering &TLI;
40   SSARegMap *RegMap;
41   SelectionDAG *CurDAG;
42   MachineBasicBlock *BB;
43
44   SelectionDAGISel(TargetLowering &tli) : TLI(tli), JT(0,0,0) {}
45
46   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
47
48   virtual bool runOnFunction(Function &Fn);
49
50   unsigned MakeReg(MVT::ValueType VT);
51
52   virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
53   virtual void InstructionSelectBasicBlock(SelectionDAG &SD) = 0;
54
55   /// SelectInlineAsmMemoryOperand - Select the specified address as a target
56   /// addressing mode, according to the specified constraint code.  If this does
57   /// not match or is not implemented, return true.  The resultant operands
58   /// (which will appear in the machine instruction) should be added to the
59   /// OutOps vector.
60   virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
61                                             char ConstraintCode,
62                                             std::vector<SDOperand> &OutOps,
63                                             SelectionDAG &DAG) {
64     return true;
65   }
66   
67   /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
68   /// to use for this target when scheduling the DAG.
69   virtual HazardRecognizer *CreateTargetHazardRecognizer();
70   
71   /// CaseBlock - This structure is used to communicate between SDLowering and
72   /// SDISel for the code generation of additional basic blocks needed by multi-
73   /// case switch statements.
74   struct CaseBlock {
75     CaseBlock(ISD::CondCode cc, Value *s, Constant *c, MachineBasicBlock *lhs,
76               MachineBasicBlock *rhs, MachineBasicBlock *me) : 
77     CC(cc), SwitchV(s), CaseC(c), LHSBB(lhs), RHSBB(rhs), ThisBB(me) {}
78     // CC - the condition code to use for the case block's setcc node
79     ISD::CondCode CC;
80     // SwitchV - the value to be switched on, 'foo' in switch(foo)
81     Value *SwitchV;
82     // CaseC - the constant the setcc node will compare against SwitchV
83     Constant *CaseC;
84     // LHSBB - the block to branch to if the setcc is true
85     MachineBasicBlock *LHSBB;
86     // RHSBB - the block to branch to if the setcc is false
87     MachineBasicBlock *RHSBB;
88     // ThisBB - the blcok into which to emit the code for the setcc and branches
89     MachineBasicBlock *ThisBB;
90   };
91   struct JumpTable {
92     JumpTable(unsigned R, unsigned J, MachineBasicBlock *me) : Reg(R), JTI(J),
93               MBB(me) {}
94     // Reg - the virtual register containing the index of the jump table entry
95     // to jump to.
96     unsigned Reg;
97     // JTI - the JumpTableIndex for this jump table in the function.
98     unsigned JTI;
99     // MBB - the MBB into which to emit the code for the indirect jump.
100     MachineBasicBlock *MBB;
101     // SuccMBBs - a vector of unique successor MBBs used for updating CFG info
102     // and PHI nodes.
103     std::set<MachineBasicBlock*> SuccMBBs;
104   };
105   
106 protected:
107   /// Pick a safe ordering and emit instructions for each target node in the
108   /// graph.
109   void ScheduleAndEmitDAG(SelectionDAG &DAG);
110   
111   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
112   /// by tblgen.  Others should not call it.
113   void SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops,
114                                      SelectionDAG &DAG);
115   
116 private:
117   SDOperand CopyValueToVirtualRegister(SelectionDAGLowering &SDL,
118                                        Value *V, unsigned Reg);
119   void SelectBasicBlock(BasicBlock *BB, MachineFunction &MF,
120                         FunctionLoweringInfo &FuncInfo);
121
122   void BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
123            std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
124                          FunctionLoweringInfo &FuncInfo);
125   void CodeGenAndEmitDAG(SelectionDAG &DAG);
126   void LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
127                       std::vector<SDOperand> &UnorderedChains);
128
129   /// SwitchCases - Vector of CaseBlock structures used to communicate
130   /// SwitchInst code generation information.
131   std::vector<CaseBlock> SwitchCases;
132
133   /// JT - Record which holds necessary information for emitting a jump table
134   JumpTable JT;
135 };
136
137 }
138
139 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */