Now that the ISel is available, it's possible to create a default instruction
[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
22 namespace llvm {
23   class SelectionDAG;
24   class SelectionDAGLowering;
25   class SDOperand;
26   class SSARegMap;
27   class MachineBasicBlock;
28   class MachineFunction;
29   class MachineInstr;
30   class TargetLowering;
31   class FunctionLoweringInfo;
32   class HazardRecognizer;
33
34 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
35 /// pattern-matching instruction selectors.
36 class SelectionDAGISel : public FunctionPass {
37 public:
38   TargetLowering &TLI;
39   SSARegMap *RegMap;
40   SelectionDAG *CurDAG;
41   MachineBasicBlock *BB;
42
43   SelectionDAGISel(TargetLowering &tli) : TLI(tli), JT(0,0,0,0) {}
44   
45   TargetLowering &getTargetLowering() { return TLI; }
46
47   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
48
49   virtual bool runOnFunction(Function &Fn);
50
51   unsigned MakeReg(MVT::ValueType VT);
52
53   virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
54   virtual void InstructionSelectBasicBlock(SelectionDAG &SD) = 0;
55
56   /// SelectInlineAsmMemoryOperand - Select the specified address as a target
57   /// addressing mode, according to the specified constraint code.  If this does
58   /// not match or is not implemented, return true.  The resultant operands
59   /// (which will appear in the machine instruction) should be added to the
60   /// OutOps vector.
61   virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
62                                             char ConstraintCode,
63                                             std::vector<SDOperand> &OutOps,
64                                             SelectionDAG &DAG) {
65     return true;
66   }
67
68   /// CanBeFoldedBy - Returns true if the specific operand node N of U can be
69   /// folded during instruction selection?
70   virtual bool CanBeFoldedBy(SDNode *N, SDNode *U) { return true; }
71   
72   /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
73   /// to use for this target when scheduling the DAG.
74   virtual HazardRecognizer *CreateTargetHazardRecognizer();
75   
76   /// CaseBlock - This structure is used to communicate between SDLowering and
77   /// SDISel for the code generation of additional basic blocks needed by multi-
78   /// case switch statements.
79   struct CaseBlock {
80     CaseBlock(ISD::CondCode cc, Value *s, Constant *c, MachineBasicBlock *lhs,
81               MachineBasicBlock *rhs, MachineBasicBlock *me) : 
82     CC(cc), SwitchV(s), CaseC(c), LHSBB(lhs), RHSBB(rhs), ThisBB(me) {}
83     // CC - the condition code to use for the case block's setcc node
84     ISD::CondCode CC;
85     // SwitchV - the value to be switched on, 'foo' in switch(foo)
86     Value *SwitchV;
87     // CaseC - the constant the setcc node will compare against SwitchV
88     Constant *CaseC;
89     // LHSBB - the block to branch to if the setcc is true
90     MachineBasicBlock *LHSBB;
91     // RHSBB - the block to branch to if the setcc is false
92     MachineBasicBlock *RHSBB;
93     // ThisBB - the blcok into which to emit the code for the setcc and branches
94     MachineBasicBlock *ThisBB;
95   };
96   struct JumpTable {
97     JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
98               MachineBasicBlock *D) : Reg(R), JTI(J), MBB(M), Default(D) {}
99     // Reg - the virtual register containing the index of the jump table entry
100     // to jump to.
101     unsigned Reg;
102     // JTI - the JumpTableIndex for this jump table in the function.
103     unsigned JTI;
104     // MBB - the MBB into which to emit the code for the indirect jump.
105     MachineBasicBlock *MBB;
106     // Default - the MBB of the default bb, which is a successor of the range
107     // check MBB.  This is when updating PHI nodes in successors.
108     MachineBasicBlock *Default;
109   };
110   
111 protected:
112   /// Pick a safe ordering and emit instructions for each target node in the
113   /// graph.
114   void ScheduleAndEmitDAG(SelectionDAG &DAG);
115   
116   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
117   /// by tblgen.  Others should not call it.
118   void SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops,
119                                      SelectionDAG &DAG);
120
121 private:
122   SDOperand CopyValueToVirtualRegister(SelectionDAGLowering &SDL,
123                                        Value *V, unsigned Reg);
124   void SelectBasicBlock(BasicBlock *BB, MachineFunction &MF,
125                         FunctionLoweringInfo &FuncInfo);
126
127   void BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
128            std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
129                          FunctionLoweringInfo &FuncInfo);
130   void CodeGenAndEmitDAG(SelectionDAG &DAG);
131   void LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
132                       std::vector<SDOperand> &UnorderedChains);
133
134   /// SwitchCases - Vector of CaseBlock structures used to communicate
135   /// SwitchInst code generation information.
136   std::vector<CaseBlock> SwitchCases;
137
138   /// JT - Record which holds necessary information for emitting a jump table
139   JumpTable JT;
140 };
141
142 }
143
144 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */