First step of switch lowering refactoring: perform worklist-driven
[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/SelectionDAG.h"
21 #include "llvm/CodeGen/SelectionDAGNodes.h"
22
23 namespace llvm {
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   std::vector<SDNode*> TopOrder;
43   unsigned DAGSize;
44
45   explicit SelectionDAGISel(TargetLowering &tli) : TLI(tli), DAGSize(0) {}
46   
47   TargetLowering &getTargetLowering() { return TLI; }
48
49   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50
51   virtual bool runOnFunction(Function &Fn);
52
53   unsigned MakeReg(MVT::ValueType VT);
54
55   virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
56   virtual void InstructionSelectBasicBlock(SelectionDAG &SD) = 0;
57   virtual void SelectRootInit() {
58     DAGSize = CurDAG->AssignTopologicalOrder(TopOrder);
59   }
60
61   /// SelectInlineAsmMemoryOperand - Select the specified address as a target
62   /// addressing mode, according to the specified constraint code.  If this does
63   /// not match or is not implemented, return true.  The resultant operands
64   /// (which will appear in the machine instruction) should be added to the
65   /// OutOps vector.
66   virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
67                                             char ConstraintCode,
68                                             std::vector<SDOperand> &OutOps,
69                                             SelectionDAG &DAG) {
70     return true;
71   }
72
73   /// CanBeFoldedBy - Returns true if the specific operand node N of U can be
74   /// folded during instruction selection that starts at Root?
75   virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) { return true;}
76   
77   /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
78   /// to use for this target when scheduling the DAG.
79   virtual HazardRecognizer *CreateTargetHazardRecognizer();
80   
81   /// CaseBlock - This structure is used to communicate between SDLowering and
82   /// SDISel for the code generation of additional basic blocks needed by multi-
83   /// case switch statements.
84   struct CaseBlock {
85     CaseBlock(ISD::CondCode cc, Value *cmplhs, Value *cmprhs, 
86               MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
87               MachineBasicBlock *me)
88       : CC(cc), CmpLHS(cmplhs), CmpRHS(cmprhs),
89         TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {}
90     // CC - the condition code to use for the case block's setcc node
91     ISD::CondCode CC;
92     // CmpLHS/CmpRHS - The LHS/RHS of the comparison to emit.
93     Value *CmpLHS, *CmpRHS;
94     // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
95     MachineBasicBlock *TrueBB, *FalseBB;
96     // ThisBB - the block into which to emit the code for the setcc and branches
97     MachineBasicBlock *ThisBB;
98   };
99   struct JumpTable {
100     JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
101               MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {};
102     
103     /// Reg - the virtual register containing the index of the jump table entry
104     //. to jump to.
105     unsigned Reg;
106     /// JTI - the JumpTableIndex for this jump table in the function.
107     unsigned JTI;
108     /// MBB - the MBB into which to emit the code for the indirect jump.
109     MachineBasicBlock *MBB;
110     /// Default - the MBB of the default bb, which is a successor of the range
111     /// check MBB.  This is when updating PHI nodes in successors.
112     MachineBasicBlock *Default;
113   };
114   struct JumpTableHeader {
115     JumpTableHeader(uint64_t F, uint64_t L, Value* SV, MachineBasicBlock* H,
116                     bool E = false):
117       First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {};
118     uint64_t First;
119     uint64_t Last;
120     Value *SValue;
121     MachineBasicBlock *HeaderBB;
122     bool Emitted;
123   };
124   typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
125  
126 protected:
127   /// Pick a safe ordering and emit instructions for each target node in the
128   /// graph.
129   void ScheduleAndEmitDAG(SelectionDAG &DAG);
130   
131   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
132   /// by tblgen.  Others should not call it.
133   void SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops,
134                                      SelectionDAG &DAG);
135
136   // Calls to these predicates are generated by tblgen.
137   bool CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, int64_t DesiredMaskS);  
138   bool CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, int64_t DesiredMaskS);  
139   
140 private:
141   void SelectBasicBlock(BasicBlock *BB, MachineFunction &MF,
142                         FunctionLoweringInfo &FuncInfo);
143
144   void BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
145            std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
146                          FunctionLoweringInfo &FuncInfo);
147   void CodeGenAndEmitDAG(SelectionDAG &DAG);
148   void LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
149                       std::vector<SDOperand> &UnorderedChains);
150
151   /// SwitchCases - Vector of CaseBlock structures used to communicate
152   /// SwitchInst code generation information.
153   std::vector<CaseBlock> SwitchCases;
154
155   /// JTCases - Vector of JumpTable structures which holds necessary information
156   /// for emitting a jump tables during SwitchInst code generation.
157   std::vector<JumpTableBlock> JTCases;
158 };
159
160 }
161
162 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */