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