LegalizeTypes.h revision dff67f5770ada2942dd8c815323ad2480bfdde44
1//===-- LegalizeTypes.h - Definition of the DAG Type Legalizer class ------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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
26namespace 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///
40class VISIBILITY_HIDDEN DAGTypeLegalizer {
41  TargetLowering &TLI;
42  SelectionDAG &DAG;
43
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
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  };
66
67  /// ValueTypeActions - This is a bitvector that contains two bits for each
68  /// simple value type, where the two bits correspond to the LegalizeAction
69  /// enum.  This can be queried with "getTypeAction(VT)".
70  TargetLowering::ValueTypeActionImpl ValueTypeActions;
71
72  /// getTypeAction - Return how we should legalize values of this type, either
73  /// it is already legal or we need to expand it into multiple registers of
74  /// smaller integer type, or we need to promote it to a larger type.
75  LegalizeAction getTypeAction(MVT::ValueType VT) const {
76    return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
77  }
78
79  /// isTypeLegal - Return true if this type is legal on this target.
80  ///
81  bool isTypeLegal(MVT::ValueType VT) const {
82    return getTypeAction(VT) == Legal;
83  }
84
85  SDOperand getIntPtrConstant(uint64_t Val) {
86    return DAG.getConstant(Val, TLI.getPointerTy());
87  }
88
89  /// PromotedNodes - For nodes that are below legal width, this map indicates
90  /// what promoted value to use.
91  DenseMap<SDOperand, SDOperand> PromotedNodes;
92
93  /// ExpandedNodes - For nodes that need to be expanded this map indicates
94  /// which operands are the expanded version of the input.
95  DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
96
97  /// ScalarizedNodes - For nodes that are <1 x ty>, this map indicates the
98  /// scalar value of type 'ty' to use.
99  DenseMap<SDOperand, SDOperand> ScalarizedNodes;
100
101  /// ReplacedNodes - For nodes that have been replaced with another,
102  /// indicates the replacement node to use.
103  DenseMap<SDOperand, SDOperand> ReplacedNodes;
104
105  /// Worklist - This defines a worklist of nodes to process.  In order to be
106  /// pushed onto this worklist, all operands of a node must have already been
107  /// processed.
108  SmallVector<SDNode*, 128> Worklist;
109
110public:
111  explicit DAGTypeLegalizer(SelectionDAG &dag)
112    : TLI(dag.getTargetLoweringInfo()), DAG(dag),
113    ValueTypeActions(TLI.getValueTypeActions()) {
114    assert(MVT::LAST_VALUETYPE <= 32 &&
115           "Too many value types for ValueTypeActions to hold!");
116  }
117
118  void run();
119
120private:
121  void MarkNewNodes(SDNode *N);
122
123  void ReplaceValueWith(SDOperand From, SDOperand To);
124  void ReplaceNodeWith(SDNode *From, SDNode *To);
125
126  void RemapNode(SDOperand &N);
127
128  SDOperand GetPromotedOp(SDOperand Op) {
129    SDOperand &PromotedOp = PromotedNodes[Op];
130    RemapNode(PromotedOp);
131    assert(PromotedOp.Val && "Operand wasn't promoted?");
132    return PromotedOp;
133  }
134  void SetPromotedOp(SDOperand Op, SDOperand Result);
135
136  /// GetPromotedZExtOp - Get a promoted operand and zero extend it to the final
137  /// size.
138  SDOperand GetPromotedZExtOp(SDOperand Op) {
139    MVT::ValueType OldVT = Op.getValueType();
140    Op = GetPromotedOp(Op);
141    return DAG.getZeroExtendInReg(Op, OldVT);
142  }
143
144  void GetExpandedOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
145  void SetExpandedOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
146
147  SDOperand GetScalarizedOp(SDOperand Op) {
148    SDOperand &ScalarOp = ScalarizedNodes[Op];
149    RemapNode(ScalarOp);
150    assert(ScalarOp.Val && "Operand wasn't scalarized?");
151    return ScalarOp;
152  }
153  void SetScalarizedOp(SDOperand Op, SDOperand Result);
154
155  // Common routines.
156  SDOperand CreateStackStoreLoad(SDOperand Op, MVT::ValueType DestVT);
157  SDOperand HandleMemIntrinsic(SDNode *N);
158  void SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
159
160  // Result Promotion.
161  void PromoteResult(SDNode *N, unsigned ResNo);
162  SDOperand PromoteResult_UNDEF(SDNode *N);
163  SDOperand PromoteResult_Constant(SDNode *N);
164  SDOperand PromoteResult_TRUNCATE(SDNode *N);
165  SDOperand PromoteResult_INT_EXTEND(SDNode *N);
166  SDOperand PromoteResult_FP_ROUND(SDNode *N);
167  SDOperand PromoteResult_FP_TO_XINT(SDNode *N);
168  SDOperand PromoteResult_SETCC(SDNode *N);
169  SDOperand PromoteResult_LOAD(LoadSDNode *N);
170  SDOperand PromoteResult_SimpleIntBinOp(SDNode *N);
171  SDOperand PromoteResult_SDIV(SDNode *N);
172  SDOperand PromoteResult_UDIV(SDNode *N);
173  SDOperand PromoteResult_SHL(SDNode *N);
174  SDOperand PromoteResult_SRA(SDNode *N);
175  SDOperand PromoteResult_SRL(SDNode *N);
176  SDOperand PromoteResult_SELECT   (SDNode *N);
177  SDOperand PromoteResult_SELECT_CC(SDNode *N);
178
179  // Result Expansion.
180  void ExpandResult(SDNode *N, unsigned ResNo);
181  void ExpandResult_UNDEF      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
182  void ExpandResult_Constant   (SDNode *N, SDOperand &Lo, SDOperand &Hi);
183  void ExpandResult_BUILD_PAIR (SDNode *N, SDOperand &Lo, SDOperand &Hi);
184  void ExpandResult_MERGE_VALUES(SDNode *N, SDOperand &Lo, SDOperand &Hi);
185  void ExpandResult_ANY_EXTEND (SDNode *N, SDOperand &Lo, SDOperand &Hi);
186  void ExpandResult_ZERO_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
187  void ExpandResult_SIGN_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
188  void ExpandResult_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
189  void ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi);
190  void ExpandResult_LOAD       (LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
191
192  void ExpandResult_Logical    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
193  void ExpandResult_BSWAP      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
194  void ExpandResult_ADDSUB     (SDNode *N, SDOperand &Lo, SDOperand &Hi);
195  void ExpandResult_ADDSUBC    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
196  void ExpandResult_ADDSUBE    (SDNode *N, SDOperand &Lo, SDOperand &Hi);
197  void ExpandResult_SELECT     (SDNode *N, SDOperand &Lo, SDOperand &Hi);
198  void ExpandResult_SELECT_CC  (SDNode *N, SDOperand &Lo, SDOperand &Hi);
199  void ExpandResult_MUL        (SDNode *N, SDOperand &Lo, SDOperand &Hi);
200  void ExpandResult_Shift      (SDNode *N, SDOperand &Lo, SDOperand &Hi);
201
202  void ExpandShiftByConstant(SDNode *N, unsigned Amt,
203                             SDOperand &Lo, SDOperand &Hi);
204  bool ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi);
205
206  // Result Vector Scalarization: <1 x ty> -> ty.
207  void ScalarizeResult(SDNode *N, unsigned OpNo);
208  SDOperand ScalarizeRes_UNDEF(SDNode *N);
209  SDOperand ScalarizeRes_LOAD(LoadSDNode *N);
210  SDOperand ScalarizeRes_BinOp(SDNode *N);
211  SDOperand ScalarizeRes_UnaryOp(SDNode *N);
212  SDOperand ScalarizeRes_FPOWI(SDNode *N);
213  SDOperand ScalarizeRes_VECTOR_SHUFFLE(SDNode *N);
214  SDOperand ScalarizeRes_BIT_CONVERT(SDNode *N);
215  SDOperand ScalarizeRes_SELECT(SDNode *N);
216
217  // Operand Promotion.
218  bool PromoteOperand(SDNode *N, unsigned OperandNo);
219  SDOperand PromoteOperand_ANY_EXTEND(SDNode *N);
220  SDOperand PromoteOperand_ZERO_EXTEND(SDNode *N);
221  SDOperand PromoteOperand_SIGN_EXTEND(SDNode *N);
222  SDOperand PromoteOperand_TRUNCATE(SDNode *N);
223  SDOperand PromoteOperand_FP_EXTEND(SDNode *N);
224  SDOperand PromoteOperand_FP_ROUND(SDNode *N);
225  SDOperand PromoteOperand_INT_TO_FP(SDNode *N);
226  SDOperand PromoteOperand_SELECT(SDNode *N, unsigned OpNo);
227  SDOperand PromoteOperand_BRCOND(SDNode *N, unsigned OpNo);
228  SDOperand PromoteOperand_BR_CC(SDNode *N, unsigned OpNo);
229  SDOperand PromoteOperand_SETCC(SDNode *N, unsigned OpNo);
230  SDOperand PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo);
231
232  void PromoteSetCCOperands(SDOperand &LHS,SDOperand &RHS, ISD::CondCode Code);
233
234  // Operand Expansion.
235  bool ExpandOperand(SDNode *N, unsigned OperandNo);
236  SDOperand ExpandOperand_TRUNCATE(SDNode *N);
237  SDOperand ExpandOperand_BIT_CONVERT(SDNode *N);
238  SDOperand ExpandOperand_UINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
239  SDOperand ExpandOperand_SINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
240  SDOperand ExpandOperand_EXTRACT_ELEMENT(SDNode *N);
241  SDOperand ExpandOperand_SETCC(SDNode *N);
242  SDOperand ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo);
243
244  void ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
245                           ISD::CondCode &CCCode);
246
247  // Operand Vector Scalarization: <1 x ty> -> ty.
248  bool ScalarizeOperand(SDNode *N, unsigned OpNo);
249  SDOperand ScalarizeOp_EXTRACT_VECTOR_ELT(SDNode *N, unsigned OpNo);
250
251};
252
253} // end namespace llvm.
254
255#endif
256