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