add two helper methods.
[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   SelectionDAGISel(TargetLowering &tli) : TLI(tli), DAGSize(0), JT(0,0,0,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?
75   virtual bool CanBeFoldedBy(SDNode *N, SDNode *U) { 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 *s, Constant *c, MachineBasicBlock *lhs,
86               MachineBasicBlock *rhs, MachineBasicBlock *me) : 
87     CC(cc), SwitchV(s), CaseC(c), LHSBB(lhs), RHSBB(rhs), ThisBB(me) {}
88     // CC - the condition code to use for the case block's setcc node
89     ISD::CondCode CC;
90     // SwitchV - the value to be switched on, 'foo' in switch(foo)
91     Value *SwitchV;
92     // CaseC - the constant the setcc node will compare against SwitchV
93     Constant *CaseC;
94     // LHSBB - the block to branch to if the setcc is true
95     MachineBasicBlock *LHSBB;
96     // RHSBB - the block to branch to if the setcc is false
97     MachineBasicBlock *RHSBB;
98     // ThisBB - the blcok into which to emit the code for the setcc and branches
99     MachineBasicBlock *ThisBB;
100   };
101   struct JumpTable {
102     JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
103               MachineBasicBlock *D) : Reg(R), JTI(J), MBB(M), Default(D) {}
104     // Reg - the virtual register containing the index of the jump table entry
105     // to jump to.
106     unsigned Reg;
107     // JTI - the JumpTableIndex for this jump table in the function.
108     unsigned JTI;
109     // MBB - the MBB into which to emit the code for the indirect jump.
110     MachineBasicBlock *MBB;
111     // Default - the MBB of the default bb, which is a successor of the range
112     // check MBB.  This is when updating PHI nodes in successors.
113     MachineBasicBlock *Default;
114   };
115   
116 protected:
117   /// Pick a safe ordering and emit instructions for each target node in the
118   /// graph.
119   void ScheduleAndEmitDAG(SelectionDAG &DAG);
120   
121   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
122   /// by tblgen.  Others should not call it.
123   void SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops,
124                                      SelectionDAG &DAG);
125
126   // Calls to these predicates are generated by tblgen.
127   bool CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, int64_t DesiredMaskS);  
128   bool CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, int64_t DesiredMaskS);  
129   
130 private:
131   void SplitCritEdgesForPHIConstants(BasicBlock *BB);
132   SDOperand CopyValueToVirtualRegister(SelectionDAGLowering &SDL,
133                                        Value *V, unsigned Reg);
134   void SelectBasicBlock(BasicBlock *BB, MachineFunction &MF,
135                         FunctionLoweringInfo &FuncInfo);
136
137   void BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
138            std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
139                          FunctionLoweringInfo &FuncInfo);
140   void CodeGenAndEmitDAG(SelectionDAG &DAG);
141   void LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
142                       std::vector<SDOperand> &UnorderedChains);
143
144   /// SwitchCases - Vector of CaseBlock structures used to communicate
145   /// SwitchInst code generation information.
146   std::vector<CaseBlock> SwitchCases;
147
148   /// JT - Record which holds necessary information for emitting a jump table
149   JumpTable JT;
150 };
151
152 }
153
154 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */