1 //===-- LegalizeTypes.h - Definition of the DAG Type Legalizer class ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DAGTypeLegalizer class. This is a private interface
11 // shared between the code that implements the SelectionDAG::LegalizeTypes
14 //===----------------------------------------------------------------------===//
16 #ifndef SELECTIONDAG_LEGALIZETYPES_H
17 #define SELECTIONDAG_LEGALIZETYPES_H
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"
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.
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.
40 class VISIBILITY_HIDDEN DAGTypeLegalizer {
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.
47 /// ReadyToProcess - All operands have been processed, so this node is ready
51 /// NewNode - This is a new node that was created in the process of
52 /// legalizing some other node.
55 /// Processed - This is a node that has already been processed.
58 // 1+ - This is a node which has this many unlegalized operands.
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.
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;
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);
79 /// isTypeLegal - Return true if this type is legal on this target.
81 bool isTypeLegal(MVT::ValueType VT) const {
82 return getTypeAction(VT) == Legal;
85 /// PromotedNodes - For nodes that are below legal width, this map indicates
86 /// what promoted value to use.
87 DenseMap<SDOperand, SDOperand> PromotedNodes;
89 /// ExpandedNodes - For nodes that need to be expanded this map indicates
90 /// which operands are the expanded version of the input.
91 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
93 /// ScalarizedNodes - For nodes that are <1 x ty>, this map indicates the
94 /// scalar value of type 'ty' to use.
95 DenseMap<SDOperand, SDOperand> ScalarizedNodes;
97 /// SplitNodes - For nodes that need to be split this map indicates
98 /// which operands are the expanded version of the input.
99 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
101 /// ReplacedNodes - For nodes that have been replaced with another,
102 /// indicates the replacement node to use.
103 DenseMap<SDOperand, SDOperand> ReplacedNodes;
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
108 SmallVector<SDNode*, 128> Worklist;
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!");
120 /// ReanalyzeNode - Recompute the NodeID and correct processed operands
121 /// for the specified node, adding it to the worklist if ready.
122 void ReanalyzeNode(SDNode *N) {
123 N->setNodeId(NewNode);
128 void AnalyzeNewNode(SDNode *&N);
130 void ReplaceValueWith(SDOperand From, SDOperand To);
131 void ReplaceNodeWith(SDNode *From, SDNode *To);
133 void RemapNode(SDOperand &N);
136 SDOperand CreateStackStoreLoad(SDOperand Op, MVT::ValueType DestVT);
137 SDOperand HandleMemIntrinsic(SDNode *N);
138 void SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
140 //===--------------------------------------------------------------------===//
141 // Promotion Support: LegalizeTypesPromote.cpp
142 //===--------------------------------------------------------------------===//
144 SDOperand GetPromotedOp(SDOperand Op) {
145 SDOperand &PromotedOp = PromotedNodes[Op];
146 RemapNode(PromotedOp);
147 assert(PromotedOp.Val && "Operand wasn't promoted?");
150 void SetPromotedOp(SDOperand Op, SDOperand Result);
152 /// GetPromotedZExtOp - Get a promoted operand and zero extend it to the final
154 SDOperand GetPromotedZExtOp(SDOperand Op) {
155 MVT::ValueType OldVT = Op.getValueType();
156 Op = GetPromotedOp(Op);
157 return DAG.getZeroExtendInReg(Op, OldVT);
161 void PromoteResult(SDNode *N, unsigned ResNo);
162 SDOperand PromoteResult_BUILD_PAIR(SDNode *N);
163 SDOperand PromoteResult_Constant(SDNode *N);
164 SDOperand PromoteResult_CTLZ(SDNode *N);
165 SDOperand PromoteResult_CTPOP(SDNode *N);
166 SDOperand PromoteResult_CTTZ(SDNode *N);
167 SDOperand PromoteResult_FP_ROUND(SDNode *N);
168 SDOperand PromoteResult_FP_TO_XINT(SDNode *N);
169 SDOperand PromoteResult_INT_EXTEND(SDNode *N);
170 SDOperand PromoteResult_LOAD(LoadSDNode *N);
171 SDOperand PromoteResult_SDIV(SDNode *N);
172 SDOperand PromoteResult_SELECT (SDNode *N);
173 SDOperand PromoteResult_SELECT_CC(SDNode *N);
174 SDOperand PromoteResult_SETCC(SDNode *N);
175 SDOperand PromoteResult_SHL(SDNode *N);
176 SDOperand PromoteResult_SimpleIntBinOp(SDNode *N);
177 SDOperand PromoteResult_SRA(SDNode *N);
178 SDOperand PromoteResult_SRL(SDNode *N);
179 SDOperand PromoteResult_TRUNCATE(SDNode *N);
180 SDOperand PromoteResult_UDIV(SDNode *N);
181 SDOperand PromoteResult_UNDEF(SDNode *N);
183 // Operand Promotion.
184 bool PromoteOperand(SDNode *N, unsigned OperandNo);
185 SDOperand PromoteOperand_ANY_EXTEND(SDNode *N);
186 SDOperand PromoteOperand_BUILD_PAIR(SDNode *N);
187 SDOperand PromoteOperand_BR_CC(SDNode *N, unsigned OpNo);
188 SDOperand PromoteOperand_BRCOND(SDNode *N, unsigned OpNo);
189 SDOperand PromoteOperand_BUILD_VECTOR(SDNode *N);
190 SDOperand PromoteOperand_FP_EXTEND(SDNode *N);
191 SDOperand PromoteOperand_FP_ROUND(SDNode *N);
192 SDOperand PromoteOperand_INT_TO_FP(SDNode *N);
193 SDOperand PromoteOperand_INSERT_VECTOR_ELT(SDNode *N, unsigned OpNo);
194 SDOperand PromoteOperand_MEMBARRIER(SDNode *N);
195 SDOperand PromoteOperand_RET(SDNode *N, unsigned OpNo);
196 SDOperand PromoteOperand_SELECT(SDNode *N, unsigned OpNo);
197 SDOperand PromoteOperand_SETCC(SDNode *N, unsigned OpNo);
198 SDOperand PromoteOperand_SIGN_EXTEND(SDNode *N);
199 SDOperand PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo);
200 SDOperand PromoteOperand_TRUNCATE(SDNode *N);
201 SDOperand PromoteOperand_ZERO_EXTEND(SDNode *N);
203 void PromoteSetCCOperands(SDOperand &LHS,SDOperand &RHS, ISD::CondCode Code);
205 //===--------------------------------------------------------------------===//
206 // Expansion Support: LegalizeTypesExpand.cpp
207 //===--------------------------------------------------------------------===//
209 void GetExpandedOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
210 void SetExpandedOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
213 void ExpandResult(SDNode *N, unsigned ResNo);
214 void ExpandResult_ANY_EXTEND (SDNode *N, SDOperand &Lo, SDOperand &Hi);
215 void ExpandResult_AssertZext (SDNode *N, SDOperand &Lo, SDOperand &Hi);
216 void ExpandResult_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
217 void ExpandResult_BUILD_PAIR (SDNode *N, SDOperand &Lo, SDOperand &Hi);
218 void ExpandResult_Constant (SDNode *N, SDOperand &Lo, SDOperand &Hi);
219 void ExpandResult_CTLZ (SDNode *N, SDOperand &Lo, SDOperand &Hi);
220 void ExpandResult_CTPOP (SDNode *N, SDOperand &Lo, SDOperand &Hi);
221 void ExpandResult_CTTZ (SDNode *N, SDOperand &Lo, SDOperand &Hi);
222 void ExpandResult_LOAD (LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
223 void ExpandResult_MERGE_VALUES(SDNode *N, SDOperand &Lo, SDOperand &Hi);
224 void ExpandResult_SIGN_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
225 void ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi);
226 void ExpandResult_TRUNCATE (SDNode *N, SDOperand &Lo, SDOperand &Hi);
227 void ExpandResult_UNDEF (SDNode *N, SDOperand &Lo, SDOperand &Hi);
228 void ExpandResult_ZERO_EXTEND(SDNode *N, SDOperand &Lo, SDOperand &Hi);
230 void ExpandResult_Logical (SDNode *N, SDOperand &Lo, SDOperand &Hi);
231 void ExpandResult_BSWAP (SDNode *N, SDOperand &Lo, SDOperand &Hi);
232 void ExpandResult_ADDSUB (SDNode *N, SDOperand &Lo, SDOperand &Hi);
233 void ExpandResult_ADDSUBC (SDNode *N, SDOperand &Lo, SDOperand &Hi);
234 void ExpandResult_ADDSUBE (SDNode *N, SDOperand &Lo, SDOperand &Hi);
235 void ExpandResult_SELECT (SDNode *N, SDOperand &Lo, SDOperand &Hi);
236 void ExpandResult_SELECT_CC (SDNode *N, SDOperand &Lo, SDOperand &Hi);
237 void ExpandResult_MUL (SDNode *N, SDOperand &Lo, SDOperand &Hi);
238 void ExpandResult_Shift (SDNode *N, SDOperand &Lo, SDOperand &Hi);
240 void ExpandShiftByConstant(SDNode *N, unsigned Amt,
241 SDOperand &Lo, SDOperand &Hi);
242 bool ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi);
244 // Operand Expansion.
245 bool ExpandOperand(SDNode *N, unsigned OperandNo);
246 SDOperand ExpandOperand_BIT_CONVERT(SDNode *N);
247 SDOperand ExpandOperand_BR_CC(SDNode *N);
248 SDOperand ExpandOperand_BUILD_VECTOR(SDNode *N);
249 SDOperand ExpandOperand_EXTRACT_ELEMENT(SDNode *N);
250 SDOperand ExpandOperand_SETCC(SDNode *N);
251 SDOperand ExpandOperand_SINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
252 SDOperand ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo);
253 SDOperand ExpandOperand_TRUNCATE(SDNode *N);
254 SDOperand ExpandOperand_UINT_TO_FP(SDOperand Source, MVT::ValueType DestTy);
256 void ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
257 ISD::CondCode &CCCode);
259 //===--------------------------------------------------------------------===//
260 // Scalarization Support: LegalizeTypesScalarize.cpp
261 //===--------------------------------------------------------------------===//
263 SDOperand GetScalarizedOp(SDOperand Op) {
264 SDOperand &ScalarOp = ScalarizedNodes[Op];
266 assert(ScalarOp.Val && "Operand wasn't scalarized?");
269 void SetScalarizedOp(SDOperand Op, SDOperand Result);
271 // Result Vector Scalarization: <1 x ty> -> ty.
272 void ScalarizeResult(SDNode *N, unsigned OpNo);
273 SDOperand ScalarizeRes_BinOp(SDNode *N);
274 SDOperand ScalarizeRes_UnaryOp(SDNode *N);
276 SDOperand ScalarizeRes_BIT_CONVERT(SDNode *N);
277 SDOperand ScalarizeRes_FPOWI(SDNode *N);
278 SDOperand ScalarizeRes_INSERT_VECTOR_ELT(SDNode *N);
279 SDOperand ScalarizeRes_LOAD(LoadSDNode *N);
280 SDOperand ScalarizeRes_SELECT(SDNode *N);
281 SDOperand ScalarizeRes_UNDEF(SDNode *N);
282 SDOperand ScalarizeRes_VECTOR_SHUFFLE(SDNode *N);
284 // Operand Vector Scalarization: <1 x ty> -> ty.
285 bool ScalarizeOperand(SDNode *N, unsigned OpNo);
286 SDOperand ScalarizeOp_EXTRACT_VECTOR_ELT(SDNode *N);
287 SDOperand ScalarizeOp_STORE(StoreSDNode *N, unsigned OpNo);
289 //===--------------------------------------------------------------------===//
290 // Vector Splitting Support: LegalizeTypesSplit.cpp
291 //===--------------------------------------------------------------------===//
293 void GetSplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi);
294 void SetSplitOp(SDOperand Op, SDOperand Lo, SDOperand Hi);
296 // Result Vector Splitting: <128 x ty> -> 2 x <64 x ty>.
297 void SplitResult(SDNode *N, unsigned OpNo);
299 void SplitRes_UNDEF(SDNode *N, SDOperand &Lo, SDOperand &Hi);
300 void SplitRes_LOAD(LoadSDNode *N, SDOperand &Lo, SDOperand &Hi);
301 void SplitRes_BUILD_PAIR(SDNode *N, SDOperand &Lo, SDOperand &Hi);
302 void SplitRes_INSERT_VECTOR_ELT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
303 void SplitRes_VECTOR_SHUFFLE(SDNode *N, SDOperand &Lo, SDOperand &Hi);
305 void SplitRes_BUILD_VECTOR(SDNode *N, SDOperand &Lo, SDOperand &Hi);
306 void SplitRes_CONCAT_VECTORS(SDNode *N, SDOperand &Lo, SDOperand &Hi);
307 void SplitRes_BIT_CONVERT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
308 void SplitRes_UnOp(SDNode *N, SDOperand &Lo, SDOperand &Hi);
309 void SplitRes_BinOp(SDNode *N, SDOperand &Lo, SDOperand &Hi);
310 void SplitRes_FPOWI(SDNode *N, SDOperand &Lo, SDOperand &Hi);
311 void SplitRes_SELECT(SDNode *N, SDOperand &Lo, SDOperand &Hi);
313 // Operand Vector Scalarization: <128 x ty> -> 2 x <64 x ty>.
314 bool SplitOperand(SDNode *N, unsigned OpNo);
316 SDOperand SplitOp_EXTRACT_SUBVECTOR(SDNode *N);
317 SDOperand SplitOp_RET(SDNode *N, unsigned OpNo);
318 SDOperand SplitOp_STORE(StoreSDNode *N, unsigned OpNo);
321 } // end namespace llvm.