LegalizeTypes support for EXTRACT_VECTOR_ELT. The
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypes.h
1 //===-- LegalizeTypes.h - Definition of the DAG Type Legalizer class ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the DAGTypeLegalizer class.  This is a private interface
11 // shared between the code that implements the SelectionDAG::LegalizeTypes
12 // method.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SELECTIONDAG_LEGALIZETYPES_H
17 #define SELECTIONDAG_LEGALIZETYPES_H
18
19 #define DEBUG_TYPE "legalize-types"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25
26 namespace llvm {
27
28 //===----------------------------------------------------------------------===//
29 /// DAGTypeLegalizer - This takes an arbitrary SelectionDAG as input and
30 /// hacks on it until the target machine can handle it.  This involves
31 /// eliminating value sizes the machine cannot handle (promoting small sizes to
32 /// large sizes or splitting up large values into small values) as well as
33 /// eliminating operations the machine cannot handle.
34 ///
35 /// This code also does a small amount of optimization and recognition of idioms
36 /// as part of its processing.  For example, if a target does not support a
37 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
38 /// will attempt merge setcc and brc instructions into brcc's.
39 ///
40 class VISIBILITY_HIDDEN DAGTypeLegalizer {
41   TargetLowering &TLI;
42   SelectionDAG &DAG;
43 public:
44   // NodeIDFlags - This pass uses the NodeID on the SDNodes to hold information
45   // about the state of the node.  The enum has all the values.
46   enum NodeIDFlags {
47     /// ReadyToProcess - All operands have been processed, so this node is ready
48     /// to be handled.
49     ReadyToProcess = 0,
50     
51     /// NewNode - This is a new node that was created in the process of
52     /// legalizing some other node.
53     NewNode = -1,
54     
55     /// Processed - This is a node that has already been processed.
56     Processed = -2
57     
58     // 1+ - This is a node which has this many unlegalized operands.
59   };
60 private:
61   enum LegalizeAction {
62     Legal,      // The target natively supports this type.
63     Promote,    // This type should be executed in a larger type.
64     Expand,     // This type should be split into two types of half the size.
65     Scalarize,  // Replace this one-element vector type with its element type.
66     Split       // This vector type should be split into smaller vectors.
67   };
68
69   /// ValueTypeActions - This is a bitvector that contains two bits for each
70   /// simple value type, where the two bits correspond to the LegalizeAction
71   /// enum from TargetLowering.  This can be queried with "getTypeAction(VT)".
72   TargetLowering::ValueTypeActionImpl ValueTypeActions;
73   
74   /// getTypeAction - Return how we should legalize values of this type, either
75   /// it is already legal, or we need to promote it to a larger integer type, or
76   /// we need to expand it into multiple registers of a smaller integer type, or
77   /// we need to scalarize a one-element vector type into the element type, or
78   /// we need to split a vector type into smaller vector types.
79   LegalizeAction getTypeAction(MVT::ValueType VT) const {
80     switch (ValueTypeActions.getTypeAction(VT)) {
81     default:
82       assert(false && "Unknown legalize action!");
83     case TargetLowering::Legal:
84       return Legal;
85     case TargetLowering::Promote:
86       return Promote;
87     case TargetLowering::Expand:
88       // Expand can mean 1) split integer in half 2) scalarize single-element
89       // vector 3) split vector in two.
90       if (!MVT::isVector(VT))
91         return Expand;
92       else if (MVT::getVectorNumElements(VT) == 1)
93         return Scalarize;
94       else
95         return Split;
96     }
97   }
98
99   /// isTypeLegal - Return true if this type is legal on this target.
100   bool isTypeLegal(MVT::ValueType VT) const {
101     return ValueTypeActions.getTypeAction(VT) == TargetLowering::Legal;
102   }
103
104   /// PromotedNodes - For nodes that are below legal width, this map indicates
105   /// what promoted value to use.
106   DenseMap<SDOperand, SDOperand> PromotedNodes;
107   
108   /// ExpandedNodes - For nodes that need to be expanded this map indicates
109   /// which operands are the expanded version of the input.
110   DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
111
112   /// ScalarizedNodes - For nodes that are <1 x ty>, this map indicates the
113   /// scalar value of type 'ty' to use.
114   DenseMap<SDOperand, SDOperand> ScalarizedNodes;
115
116   /// SplitNodes - For nodes that need to be split this map indicates
117   /// which operands are the expanded version of the input.
118   DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
119   
120   /// ReplacedNodes - For nodes that have been replaced with another,
121   /// indicates the replacement node to use.
122   DenseMap<SDOperand, SDOperand> ReplacedNodes;
123
124   /// Worklist - This defines a worklist of nodes to process.  In order to be
125   /// pushed onto this worklist, all operands of a node must have already been
126   /// processed.
127   SmallVector<SDNode*, 128> Worklist;
128   
129 public:
130   explicit DAGTypeLegalizer(SelectionDAG &dag)
131     : TLI(dag.getTargetLoweringInfo()), DAG(dag),
132     ValueTypeActions(TLI.getValueTypeActions()) {
133     assert(MVT::LAST_VALUETYPE <= 32 &&
134            "Too many value types for ValueTypeActions to hold!");
135   }      
136   
137   void run();
138   
139   /// ReanalyzeNode - Recompute the NodeID and correct processed operands
140   /// for the specified node, adding it to the worklist if ready.
141   void ReanalyzeNode(SDNode *N) {
142     N->setNodeId(NewNode);
143     AnalyzeNewNode(N);
144   }
145
146 private:
147   void AnalyzeNewNode(SDNode *&N);
148
149   void ReplaceValueWith(SDOperand From, SDOperand To);
150   void ReplaceNodeWith(SDNode *From, SDNode *To);
151
152   void RemapNode(SDOperand &N);
153
154   // Common routines.
155   SDOperand CreateStackStoreLoad(SDOperand Op, MVT::ValueType DestVT);
156   SDOperand HandleMemIntrinsic(SDNode *N);
157   void SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
158
159   //===--------------------------------------------------------------------===//
160   // Promotion Support: LegalizeTypesPromote.cpp
161   //===--------------------------------------------------------------------===//
162   
163   SDOperand GetPromotedOp(SDOperand Op) {
164     SDOperand &PromotedOp = PromotedNodes[Op];
165     RemapNode(PromotedOp);
166     assert(PromotedOp.Val && "Operand wasn't promoted?");
167     return PromotedOp;
168   }
169   void SetPromotedOp(SDOperand Op, SDOperand Result);
170   
171   /// GetPromotedZExtOp - Get a promoted operand and zero extend it to the final
172   /// size.
173   SDOperand GetPromotedZExtOp(SDOperand Op) {
174     MVT::ValueType OldVT = Op.getValueType();
175     Op = GetPromotedOp(Op);
176     return DAG.getZeroExtendInReg(Op, OldVT);
177   }    
178     
179   // Result Promotion.
180   void PromoteResult(SDNode *N, unsigned ResNo);
181   SDOperand PromoteResult_BIT_CONVERT(SDNode *N);
182   SDOperand PromoteResult_BUILD_PAIR(SDNode *N);
183   SDOperand PromoteResult_Constant(SDNode *N);
184   SDOperand PromoteResult_CTLZ(SDNode *N);
185   SDOperand PromoteResult_CTPOP(SDNode *N);
186   SDOperand PromoteResult_CTTZ(SDNode *N);
187   SDOperand PromoteResult_EXTRACT_VECTOR_ELT(SDNode *N);
188   SDOperand PromoteResult_FP_ROUND(SDNode *N);
189   SDOperand PromoteResult_FP_TO_XINT(SDNode *N);
190   SDOperand PromoteResult_INT_EXTEND(SDNode *N);
191   SDOperand PromoteResult_LOAD(LoadSDNode *N);
192   SDOperand PromoteResult_SDIV(SDNode *N);
193   SDOperand PromoteResult_SELECT   (SDNode *N);
194   SDOperand PromoteResult_SELECT_CC(SDNode *N);
195   SDOperand PromoteResult_SETCC(SDNode *N);
196   SDOperand PromoteResult_SHL(SDNode *N);
197   SDOperand PromoteResult_SimpleIntBinOp(SDNode *N);
198   SDOperand PromoteResult_SRA(SDNode *N);
199   SDOperand PromoteResult_SRL(SDNode *N);
200   SDOperand PromoteResult_TRUNCATE(SDNode *N);
201   SDOperand PromoteResult_UDIV(SDNode *N);
202   SDOperand PromoteResult_UNDEF(SDNode *N);
203
204   // Operand Promotion.
205   bool PromoteOperand(SDNode *N, unsigned OperandNo);
206   SDOperand PromoteOperand_ANY_EXTEND(SDNode *N);
207   SDOperand PromoteOperand_BUILD_PAIR(SDNode *N);
208   SDOperand PromoteOperand_BR_CC(SDNode *N, unsigned OpNo);
209   SDOperand PromoteOperand_BRCOND(SDNode *N, unsigned OpNo);
210   SDOperand PromoteOperand_BUILD_VECTOR(SDNode *N);
211   SDOperand PromoteOperand_FP_EXTEND(SDNode *N);
212   SDOperand PromoteOperand_FP_ROUND(SDNode *N);
213   SDOperand PromoteOperand_INT_TO_FP(SDNode *N);
214   SDOperand PromoteOperand_INSERT_VECTOR_ELT(SDNode *N, unsigned OpNo);
215   SDOperand PromoteOperand_MEMBARRIER(SDNode *N);
216   SDOperand PromoteOperand_RET(SDNode *N, unsigned OpNo);
217   SDOperand PromoteOperand_SELECT(SDNode *N, unsigned OpNo);
218   SDOperand PromoteOperand_SETCC(SDNode *N, unsigned OpNo);
219   SDOperand PromoteOperand_SIGN_EXTEND(SDNode *N);
220   SDOperand PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo);
221   SDOperand PromoteOperand_TRUNCATE(SDNode *N);
222   SDOperand PromoteOperand_ZERO_EXTEND(SDNode *N);
223
224   void PromoteSetCCOperands(SDOperand &LHS,SDOperand &RHS, ISD::CondCode Code);
225
226   //===--------------------------------------------------------------------===//
227   // Expansion Support: LegalizeTypesExpand.cpp
228   //===--------------------------------------------------------------------===//
229   
230   void GetExpandedOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
231   void SetExpandedOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
232     
233   // Result Expansion.
234   void ExpandResult(SDNode *N, unsigned ResNo);
235   void ExpandResult_ANY_EXTEND (SDNode *N, SDOperand &Lo, SDOperand &Hi);
236   void ExpandResult_AssertZext (SDNode *N, SDOperand &Lo, SDOperand &Hi);
237   void ExpandResult_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
238   void ExpandResult_BUILD_PAIR (SDNode *N, SDOperand &Lo, SDOperand &Hi);
239   void ExpandResult_Constant   (SDNode *N, SDOperand &Lo, SDOperand &Hi);
240   void ExpandResult_CTLZ       (SDNode *N, SDOperand &Lo, SDOperand &Hi);
241   void ExpandResult_CTPOP      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
242   void ExpandResult_CTTZ       (SDNode *N, SDOperand &Lo, SDOperand &Hi);
243   void ExpandResult_EXTRACT_VECTOR_ELT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
244   void ExpandResult_LOAD       (LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
245   void ExpandResult_MERGE_VALUES(SDNode *N, SDOperand &Lo, SDOperand &Hi);
246   void ExpandResult_SIGN_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
247   void ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi);
248   void ExpandResult_TRUNCATE   (SDNode *N, SDOperand &Lo, SDOperand &Hi);
249   void ExpandResult_UNDEF      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
250   void ExpandResult_ZERO_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
251
252   void ExpandResult_Logical    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
253   void ExpandResult_BSWAP      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
254   void ExpandResult_ADDSUB     (SDNode *N, SDOperand &Lo, SDOperand &Hi);
255   void ExpandResult_ADDSUBC    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
256   void ExpandResult_ADDSUBE    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
257   void ExpandResult_SELECT     (SDNode *N, SDOperand &Lo, SDOperand &Hi);
258   void ExpandResult_SELECT_CC  (SDNode *N, SDOperand &Lo, SDOperand &Hi);
259   void ExpandResult_MUL        (SDNode *N, SDOperand &Lo, SDOperand &Hi);
260   void ExpandResult_Shift      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
261   
262   void ExpandShiftByConstant(SDNode *N, unsigned Amt, 
263                              SDOperand &Lo, SDOperand &Hi);
264   bool ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi);
265
266   // Operand Expansion.
267   bool ExpandOperand(SDNode *N, unsigned OperandNo);
268   SDOperand ExpandOperand_BIT_CONVERT(SDNode *N);
269   SDOperand ExpandOperand_BR_CC(SDNode *N);
270   SDOperand ExpandOperand_BUILD_VECTOR(SDNode *N);
271   SDOperand ExpandOperand_EXTRACT_ELEMENT(SDNode *N);
272   SDOperand ExpandOperand_SETCC(SDNode *N);
273   SDOperand ExpandOperand_SINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
274   SDOperand ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo);
275   SDOperand ExpandOperand_TRUNCATE(SDNode *N);
276   SDOperand ExpandOperand_UINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
277
278   void ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
279                            ISD::CondCode &CCCode);
280   
281   //===--------------------------------------------------------------------===//
282   // Scalarization Support: LegalizeTypesScalarize.cpp
283   //===--------------------------------------------------------------------===//
284   
285   SDOperand GetScalarizedOp(SDOperand Op) {
286     SDOperand &ScalarOp = ScalarizedNodes[Op];
287     RemapNode(ScalarOp);
288     assert(ScalarOp.Val && "Operand wasn't scalarized?");
289     return ScalarOp;
290   }
291   void SetScalarizedOp(SDOperand Op, SDOperand Result);
292     
293   // Result Vector Scalarization: <1 x ty> -> ty.
294   void ScalarizeResult(SDNode *N, unsigned OpNo);
295   SDOperand ScalarizeRes_BinOp(SDNode *N);
296   SDOperand ScalarizeRes_UnaryOp(SDNode *N);
297
298   SDOperand ScalarizeRes_BIT_CONVERT(SDNode *N);
299   SDOperand ScalarizeRes_FPOWI(SDNode *N);
300   SDOperand ScalarizeRes_INSERT_VECTOR_ELT(SDNode *N);
301   SDOperand ScalarizeRes_LOAD(LoadSDNode *N);
302   SDOperand ScalarizeRes_SELECT(SDNode *N);
303   SDOperand ScalarizeRes_UNDEF(SDNode *N);
304   SDOperand ScalarizeRes_VECTOR_SHUFFLE(SDNode *N);
305
306   // Operand Vector Scalarization: <1 x ty> -> ty.
307   bool ScalarizeOperand(SDNode *N, unsigned OpNo);
308   SDOperand ScalarizeOp_BIT_CONVERT(SDNode *N);
309   SDOperand ScalarizeOp_EXTRACT_VECTOR_ELT(SDNode *N);
310   SDOperand ScalarizeOp_STORE(StoreSDNode *N, unsigned OpNo);
311
312   //===--------------------------------------------------------------------===//
313   // Vector Splitting Support: LegalizeTypesSplit.cpp
314   //===--------------------------------------------------------------------===//
315   
316   void GetSplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
317   void SetSplitOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
318   
319   // Result Vector Splitting: <128 x ty> -> 2 x <64 x ty>.
320   void SplitResult(SDNode *N, unsigned OpNo);
321
322   void SplitRes_UNDEF(SDNode *N, SDOperand &Lo, SDOperand &Hi);
323   void SplitRes_LOAD(LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
324   void SplitRes_BUILD_PAIR(SDNode *N, SDOperand &Lo, SDOperand &Hi);
325   void SplitRes_INSERT_VECTOR_ELT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
326   void SplitRes_VECTOR_SHUFFLE(SDNode *N, SDOperand &Lo, SDOperand &Hi);
327
328   void SplitRes_BUILD_VECTOR(SDNode *N, SDOperand &Lo, SDOperand &Hi);
329   void SplitRes_CONCAT_VECTORS(SDNode *N, SDOperand &Lo, SDOperand &Hi);
330   void SplitRes_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
331   void SplitRes_UnOp(SDNode *N, SDOperand &Lo, SDOperand &Hi);
332   void SplitRes_BinOp(SDNode *N, SDOperand &Lo, SDOperand &Hi);
333   void SplitRes_FPOWI(SDNode *N, SDOperand &Lo, SDOperand &Hi);
334   void SplitRes_SELECT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
335   
336   // Operand Vector Scalarization: <128 x ty> -> 2 x <64 x ty>.
337   bool SplitOperand(SDNode *N, unsigned OpNo);
338
339   SDOperand SplitOp_BIT_CONVERT(SDNode *N);
340   SDOperand SplitOp_EXTRACT_SUBVECTOR(SDNode *N);
341   SDOperand SplitOp_EXTRACT_VECTOR_ELT(SDNode *N);
342   SDOperand SplitOp_RET(SDNode *N, unsigned OpNo);
343   SDOperand SplitOp_STORE(StoreSDNode *N, unsigned OpNo);
344   SDOperand SplitOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo);
345 };
346
347 } // end namespace llvm.
348
349 #endif