LegalizeTypes.h revision 278f83daebd1d2072a4ce6892fa3481db5cdefbe
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/ADT/DenseSet.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/Debug.h"
26
27namespace llvm {
28
29//===----------------------------------------------------------------------===//
30/// DAGTypeLegalizer - This takes an arbitrary SelectionDAG as input and hacks
31/// on it until only value types the target machine can handle are left.  This
32/// involves promoting small sizes to large sizes or splitting up large values
33/// into small values.
34///
35class VISIBILITY_HIDDEN DAGTypeLegalizer {
36  TargetLowering &TLI;
37  SelectionDAG &DAG;
38public:
39  // NodeIdFlags - This pass uses the NodeId on the SDNodes to hold information
40  // about the state of the node.  The enum has all the values.
41  enum NodeIdFlags {
42    /// ReadyToProcess - All operands have been processed, so this node is ready
43    /// to be handled.
44    ReadyToProcess = 0,
45
46    /// NewNode - This is a new node, not before seen, that was created in the
47    /// process of legalizing some other node.
48    NewNode = -1,
49
50    /// Unanalyzed - This node's ID needs to be set to the number of its
51    /// unprocessed operands.
52    Unanalyzed = -2,
53
54    /// Processed - This is a node that has already been processed.
55    Processed = -3
56
57    // 1+ - This is a node which has this many unprocessed operands.
58  };
59private:
60  enum LegalizeAction {
61    Legal,           // The target natively supports this type.
62    PromoteInteger,  // Replace this integer type with a larger one.
63    ExpandInteger,   // Split this integer type into two of half the size.
64    SoftenFloat,     // Convert this float type to a same size integer type.
65    ExpandFloat,     // Split this float type into two of half the size.
66    ScalarizeVector, // Replace this one-element vector with its element type.
67    SplitVector,     // This vector type should be split into smaller vectors.
68    WidenVector      // This vector type should be widened into a larger vector.
69  };
70
71  /// ValueTypeActions - This is a bitvector that contains two bits for each
72  /// simple value type, where the two bits correspond to the LegalizeAction
73  /// enum from TargetLowering.  This can be queried with "getTypeAction(VT)".
74  TargetLowering::ValueTypeActionImpl ValueTypeActions;
75
76  /// getTypeAction - Return how we should legalize values of this type.
77  LegalizeAction getTypeAction(MVT VT) const {
78    switch (ValueTypeActions.getTypeAction(VT)) {
79    default:
80      assert(false && "Unknown legalize action!");
81    case TargetLowering::Legal:
82      return Legal;
83    case TargetLowering::Promote:
84      // Promote can mean
85      //   1) For integers, use a larger integer type (e.g. i8 -> i32).
86      //   2) For vectors, use a wider vector type (e.g. v3i32 -> v4i32).
87      if (!VT.isVector())
88        return PromoteInteger;
89      else
90        return WidenVector;
91    case TargetLowering::Expand:
92      // Expand can mean
93      // 1) split scalar in half, 2) convert a float to an integer,
94      // 3) scalarize a single-element vector, 4) split a vector in two.
95      if (!VT.isVector()) {
96        if (VT.isInteger())
97          return ExpandInteger;
98        else if (VT.getSizeInBits() ==
99                 TLI.getTypeToTransformTo(VT).getSizeInBits())
100          return SoftenFloat;
101        else
102          return ExpandFloat;
103      } else if (VT.getVectorNumElements() == 1) {
104        return ScalarizeVector;
105      } else {
106        return SplitVector;
107      }
108    }
109  }
110
111  /// isTypeLegal - Return true if this type is legal on this target.
112  bool isTypeLegal(MVT VT) const {
113    return ValueTypeActions.getTypeAction(VT) == TargetLowering::Legal;
114  }
115
116  /// IgnoreNodeResults - Pretend all of this node's results are legal.
117  /// FIXME: Remove once PR2957 is done.
118  bool IgnoreNodeResults(SDNode *N) const {
119    return N->getOpcode() == ISD::TargetConstant ||
120           IgnoredNodesResultsSet.count(N);
121  }
122
123  /// IgnoredNode - Set of nodes whose result don't need to be legal.
124  /// FIXME: Remove once PR2957 is done.
125  DenseSet<SDNode*> IgnoredNodesResultsSet;
126
127  /// PromotedIntegers - For integer nodes that are below legal width, this map
128  /// indicates what promoted value to use.
129  DenseMap<SDValue, SDValue> PromotedIntegers;
130
131  /// ExpandedIntegers - For integer nodes that need to be expanded this map
132  /// indicates which operands are the expanded version of the input.
133  DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedIntegers;
134
135  /// SoftenedFloats - For floating point nodes converted to integers of
136  /// the same size, this map indicates the converted value to use.
137  DenseMap<SDValue, SDValue> SoftenedFloats;
138
139  /// ExpandedFloats - For float nodes that need to be expanded this map
140  /// indicates which operands are the expanded version of the input.
141  DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedFloats;
142
143  /// ScalarizedVectors - For nodes that are <1 x ty>, this map indicates the
144  /// scalar value of type 'ty' to use.
145  DenseMap<SDValue, SDValue> ScalarizedVectors;
146
147  /// SplitVectors - For nodes that need to be split this map indicates
148  /// which operands are the expanded version of the input.
149  DenseMap<SDValue, std::pair<SDValue, SDValue> > SplitVectors;
150
151  /// WidenedVectors - For vector nodes that need to be widened, indicates
152  /// the widened value to use.
153  DenseMap<SDValue, SDValue> WidenedVectors;
154
155  /// ReplacedValues - For values that have been replaced with another,
156  /// indicates the replacement value to use.
157  DenseMap<SDValue, SDValue> ReplacedValues;
158
159  /// Worklist - This defines a worklist of nodes to process.  In order to be
160  /// pushed onto this worklist, all operands of a node must have already been
161  /// processed.
162  SmallVector<SDNode*, 128> Worklist;
163
164public:
165  explicit DAGTypeLegalizer(SelectionDAG &dag)
166    : TLI(dag.getTargetLoweringInfo()), DAG(dag),
167    ValueTypeActions(TLI.getValueTypeActions()) {
168    assert(MVT::LAST_VALUETYPE <= 32 &&
169           "Too many value types for ValueTypeActions to hold!");
170  }
171
172  /// run - This is the main entry point for the type legalizer.  This does a
173  /// top-down traversal of the dag, legalizing types as it goes.  Returns
174  /// "true" if it made any changes.
175  bool run();
176
177  void NoteDeletion(SDNode *Old, SDNode *New) {
178    ExpungeNode(Old);
179    ExpungeNode(New);
180    for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i)
181      ReplacedValues[SDValue(Old, i)] = SDValue(New, i);
182  }
183
184private:
185  SDNode *AnalyzeNewNode(SDNode *N);
186  void AnalyzeNewValue(SDValue &Val);
187  void ExpungeNode(SDNode *N);
188  void PerformExpensiveChecks();
189  void RemapValue(SDValue &N);
190
191  // Common routines.
192  SDValue BitConvertToInteger(SDValue Op);
193  SDValue CreateStackStoreLoad(SDValue Op, MVT DestVT);
194  bool CustomLowerResults(SDNode *N, MVT VT, bool LegalizeResult);
195  SDValue GetVectorElementPointer(SDValue VecPtr, MVT EltVT, SDValue Index);
196  SDValue JoinIntegers(SDValue Lo, SDValue Hi);
197  SDValue LibCallify(RTLIB::Libcall LC, SDNode *N, bool isSigned);
198  SDValue MakeLibCall(RTLIB::Libcall LC, MVT RetVT,
199                      const SDValue *Ops, unsigned NumOps, bool isSigned,
200                      DebugLoc dl);
201  SDValue PromoteTargetBoolean(SDValue Bool, MVT VT);
202  void ReplaceValueWith(SDValue From, SDValue To);
203  void ReplaceValueWithHelper(SDValue From, SDValue To);
204  void SetIgnoredNodeResult(SDNode* N);
205  void SplitInteger(SDValue Op, SDValue &Lo, SDValue &Hi);
206  void SplitInteger(SDValue Op, MVT LoVT, MVT HiVT,
207                    SDValue &Lo, SDValue &Hi);
208
209  //===--------------------------------------------------------------------===//
210  // Integer Promotion Support: LegalizeIntegerTypes.cpp
211  //===--------------------------------------------------------------------===//
212
213  /// GetPromotedInteger - Given a processed operand Op which was promoted to a
214  /// larger integer type, this returns the promoted value.  The low bits of the
215  /// promoted value corresponding to the original type are exactly equal to Op.
216  /// The extra bits contain rubbish, so the promoted value may need to be zero-
217  /// or sign-extended from the original type before it is usable (the helpers
218  /// SExtPromotedInteger and ZExtPromotedInteger can do this for you).
219  /// For example, if Op is an i16 and was promoted to an i32, then this method
220  /// returns an i32, the lower 16 bits of which coincide with Op, and the upper
221  /// 16 bits of which contain rubbish.
222  SDValue GetPromotedInteger(SDValue Op) {
223    SDValue &PromotedOp = PromotedIntegers[Op];
224    RemapValue(PromotedOp);
225    assert(PromotedOp.getNode() && "Operand wasn't promoted?");
226    return PromotedOp;
227  }
228  void SetPromotedInteger(SDValue Op, SDValue Result);
229
230  /// SExtPromotedInteger - Get a promoted operand and sign extend it to the
231  /// final size.
232  SDValue SExtPromotedInteger(SDValue Op) {
233    MVT OldVT = Op.getValueType();
234    DebugLoc dl = Op.getDebugLoc();
235    Op = GetPromotedInteger(Op);
236    return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(), Op,
237                       DAG.getValueType(OldVT));
238  }
239
240  /// ZExtPromotedInteger - Get a promoted operand and zero extend it to the
241  /// final size.
242  SDValue ZExtPromotedInteger(SDValue Op) {
243    MVT OldVT = Op.getValueType();
244    DebugLoc dl = Op.getDebugLoc();
245    Op = GetPromotedInteger(Op);
246    return DAG.getZeroExtendInReg(Op, dl, OldVT);
247  }
248
249  // Integer Result Promotion.
250  void PromoteIntegerResult(SDNode *N, unsigned ResNo);
251  SDValue PromoteIntRes_AssertSext(SDNode *N);
252  SDValue PromoteIntRes_AssertZext(SDNode *N);
253  SDValue PromoteIntRes_Atomic1(AtomicSDNode *N);
254  SDValue PromoteIntRes_Atomic2(AtomicSDNode *N);
255  SDValue PromoteIntRes_BIT_CONVERT(SDNode *N);
256  SDValue PromoteIntRes_BSWAP(SDNode *N);
257  SDValue PromoteIntRes_BUILD_PAIR(SDNode *N);
258  SDValue PromoteIntRes_Constant(SDNode *N);
259  SDValue PromoteIntRes_CONVERT_RNDSAT(SDNode *N);
260  SDValue PromoteIntRes_CTLZ(SDNode *N);
261  SDValue PromoteIntRes_CTPOP(SDNode *N);
262  SDValue PromoteIntRes_CTTZ(SDNode *N);
263  SDValue PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N);
264  SDValue PromoteIntRes_FP_TO_XINT(SDNode *N);
265  SDValue PromoteIntRes_INT_EXTEND(SDNode *N);
266  SDValue PromoteIntRes_LOAD(LoadSDNode *N);
267  SDValue PromoteIntRes_Overflow(SDNode *N);
268  SDValue PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo);
269  SDValue PromoteIntRes_SDIV(SDNode *N);
270  SDValue PromoteIntRes_SELECT(SDNode *N);
271  SDValue PromoteIntRes_SELECT_CC(SDNode *N);
272  SDValue PromoteIntRes_SETCC(SDNode *N);
273  SDValue PromoteIntRes_SHL(SDNode *N);
274  SDValue PromoteIntRes_SimpleIntBinOp(SDNode *N);
275  SDValue PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N);
276  SDValue PromoteIntRes_SRA(SDNode *N);
277  SDValue PromoteIntRes_SRL(SDNode *N);
278  SDValue PromoteIntRes_TRUNCATE(SDNode *N);
279  SDValue PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo);
280  SDValue PromoteIntRes_UDIV(SDNode *N);
281  SDValue PromoteIntRes_UNDEF(SDNode *N);
282  SDValue PromoteIntRes_VAARG(SDNode *N);
283  SDValue PromoteIntRes_XMULO(SDNode *N, unsigned ResNo);
284
285  // Integer Operand Promotion.
286  bool PromoteIntegerOperand(SDNode *N, unsigned OperandNo);
287  SDValue PromoteIntOp_ANY_EXTEND(SDNode *N);
288  SDValue PromoteIntOp_BUILD_PAIR(SDNode *N);
289  SDValue PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo);
290  SDValue PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo);
291  SDValue PromoteIntOp_BUILD_VECTOR(SDNode *N);
292  SDValue PromoteIntOp_CONVERT_RNDSAT(SDNode *N);
293  SDValue PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N, unsigned OpNo);
294  SDValue PromoteIntOp_MEMBARRIER(SDNode *N);
295  SDValue PromoteIntOp_SELECT(SDNode *N, unsigned OpNo);
296  SDValue PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo);
297  SDValue PromoteIntOp_SETCC(SDNode *N, unsigned OpNo);
298  SDValue PromoteIntOp_Shift(SDNode *N);
299  SDValue PromoteIntOp_SIGN_EXTEND(SDNode *N);
300  SDValue PromoteIntOp_SINT_TO_FP(SDNode *N);
301  SDValue PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo);
302  SDValue PromoteIntOp_TRUNCATE(SDNode *N);
303  SDValue PromoteIntOp_UINT_TO_FP(SDNode *N);
304  SDValue PromoteIntOp_ZERO_EXTEND(SDNode *N);
305
306  void PromoteSetCCOperands(SDValue &LHS,SDValue &RHS, ISD::CondCode Code);
307
308  //===--------------------------------------------------------------------===//
309  // Integer Expansion Support: LegalizeIntegerTypes.cpp
310  //===--------------------------------------------------------------------===//
311
312  /// GetExpandedInteger - Given a processed operand Op which was expanded into
313  /// two integers of half the size, this returns the two halves.  The low bits
314  /// of Op are exactly equal to the bits of Lo; the high bits exactly equal Hi.
315  /// For example, if Op is an i64 which was expanded into two i32's, then this
316  /// method returns the two i32's, with Lo being equal to the lower 32 bits of
317  /// Op, and Hi being equal to the upper 32 bits.
318  void GetExpandedInteger(SDValue Op, SDValue &Lo, SDValue &Hi);
319  void SetExpandedInteger(SDValue Op, SDValue Lo, SDValue Hi);
320
321  // Integer Result Expansion.
322  void ExpandIntegerResult(SDNode *N, unsigned ResNo);
323  void ExpandIntRes_ANY_EXTEND        (SDNode *N, SDValue &Lo, SDValue &Hi);
324  void ExpandIntRes_AssertSext        (SDNode *N, SDValue &Lo, SDValue &Hi);
325  void ExpandIntRes_AssertZext        (SDNode *N, SDValue &Lo, SDValue &Hi);
326  void ExpandIntRes_Constant          (SDNode *N, SDValue &Lo, SDValue &Hi);
327  void ExpandIntRes_CTLZ              (SDNode *N, SDValue &Lo, SDValue &Hi);
328  void ExpandIntRes_CTPOP             (SDNode *N, SDValue &Lo, SDValue &Hi);
329  void ExpandIntRes_CTTZ              (SDNode *N, SDValue &Lo, SDValue &Hi);
330  void ExpandIntRes_LOAD          (LoadSDNode *N, SDValue &Lo, SDValue &Hi);
331  void ExpandIntRes_SIGN_EXTEND       (SDNode *N, SDValue &Lo, SDValue &Hi);
332  void ExpandIntRes_SIGN_EXTEND_INREG (SDNode *N, SDValue &Lo, SDValue &Hi);
333  void ExpandIntRes_TRUNCATE          (SDNode *N, SDValue &Lo, SDValue &Hi);
334  void ExpandIntRes_ZERO_EXTEND       (SDNode *N, SDValue &Lo, SDValue &Hi);
335  void ExpandIntRes_FP_TO_SINT        (SDNode *N, SDValue &Lo, SDValue &Hi);
336  void ExpandIntRes_FP_TO_UINT        (SDNode *N, SDValue &Lo, SDValue &Hi);
337
338  void ExpandIntRes_Logical           (SDNode *N, SDValue &Lo, SDValue &Hi);
339  void ExpandIntRes_ADDSUB            (SDNode *N, SDValue &Lo, SDValue &Hi);
340  void ExpandIntRes_ADDSUBC           (SDNode *N, SDValue &Lo, SDValue &Hi);
341  void ExpandIntRes_ADDSUBE           (SDNode *N, SDValue &Lo, SDValue &Hi);
342  void ExpandIntRes_BSWAP             (SDNode *N, SDValue &Lo, SDValue &Hi);
343  void ExpandIntRes_MUL               (SDNode *N, SDValue &Lo, SDValue &Hi);
344  void ExpandIntRes_SDIV              (SDNode *N, SDValue &Lo, SDValue &Hi);
345  void ExpandIntRes_SREM              (SDNode *N, SDValue &Lo, SDValue &Hi);
346  void ExpandIntRes_UDIV              (SDNode *N, SDValue &Lo, SDValue &Hi);
347  void ExpandIntRes_UREM              (SDNode *N, SDValue &Lo, SDValue &Hi);
348  void ExpandIntRes_Shift             (SDNode *N, SDValue &Lo, SDValue &Hi);
349
350  void ExpandShiftByConstant(SDNode *N, unsigned Amt,
351                             SDValue &Lo, SDValue &Hi);
352  bool ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi);
353
354  // Integer Operand Expansion.
355  bool ExpandIntegerOperand(SDNode *N, unsigned OperandNo);
356  SDValue ExpandIntOp_BIT_CONVERT(SDNode *N);
357  SDValue ExpandIntOp_BR_CC(SDNode *N);
358  SDValue ExpandIntOp_BUILD_VECTOR(SDNode *N);
359  SDValue ExpandIntOp_EXTRACT_ELEMENT(SDNode *N);
360  SDValue ExpandIntOp_SELECT_CC(SDNode *N);
361  SDValue ExpandIntOp_SETCC(SDNode *N);
362  SDValue ExpandIntOp_Shift(SDNode *N);
363  SDValue ExpandIntOp_SINT_TO_FP(SDNode *N);
364  SDValue ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo);
365  SDValue ExpandIntOp_TRUNCATE(SDNode *N);
366  SDValue ExpandIntOp_UINT_TO_FP(SDNode *N);
367
368  void IntegerExpandSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
369                                  ISD::CondCode &CCCode, DebugLoc dl);
370
371  //===--------------------------------------------------------------------===//
372  // Float to Integer Conversion Support: LegalizeFloatTypes.cpp
373  //===--------------------------------------------------------------------===//
374
375  /// GetSoftenedFloat - Given a processed operand Op which was converted to an
376  /// integer of the same size, this returns the integer.  The integer contains
377  /// exactly the same bits as Op - only the type changed.  For example, if Op
378  /// is an f32 which was softened to an i32, then this method returns an i32,
379  /// the bits of which coincide with those of Op.
380  SDValue GetSoftenedFloat(SDValue Op) {
381    SDValue &SoftenedOp = SoftenedFloats[Op];
382    RemapValue(SoftenedOp);
383    assert(SoftenedOp.getNode() && "Operand wasn't converted to integer?");
384    return SoftenedOp;
385  }
386  void SetSoftenedFloat(SDValue Op, SDValue Result);
387
388  // Result Float to Integer Conversion.
389  void SoftenFloatResult(SDNode *N, unsigned OpNo);
390  SDValue SoftenFloatRes_BIT_CONVERT(SDNode *N);
391  SDValue SoftenFloatRes_BUILD_PAIR(SDNode *N);
392  SDValue SoftenFloatRes_ConstantFP(ConstantFPSDNode *N);
393  SDValue SoftenFloatRes_FABS(SDNode *N);
394  SDValue SoftenFloatRes_FADD(SDNode *N);
395  SDValue SoftenFloatRes_FCEIL(SDNode *N);
396  SDValue SoftenFloatRes_FCOPYSIGN(SDNode *N);
397  SDValue SoftenFloatRes_FCOS(SDNode *N);
398  SDValue SoftenFloatRes_FDIV(SDNode *N);
399  SDValue SoftenFloatRes_FEXP(SDNode *N);
400  SDValue SoftenFloatRes_FEXP2(SDNode *N);
401  SDValue SoftenFloatRes_FFLOOR(SDNode *N);
402  SDValue SoftenFloatRes_FLOG(SDNode *N);
403  SDValue SoftenFloatRes_FLOG2(SDNode *N);
404  SDValue SoftenFloatRes_FLOG10(SDNode *N);
405  SDValue SoftenFloatRes_FMUL(SDNode *N);
406  SDValue SoftenFloatRes_FNEARBYINT(SDNode *N);
407  SDValue SoftenFloatRes_FNEG(SDNode *N);
408  SDValue SoftenFloatRes_FP_EXTEND(SDNode *N);
409  SDValue SoftenFloatRes_FP_ROUND(SDNode *N);
410  SDValue SoftenFloatRes_FPOW(SDNode *N);
411  SDValue SoftenFloatRes_FPOWI(SDNode *N);
412  SDValue SoftenFloatRes_FRINT(SDNode *N);
413  SDValue SoftenFloatRes_FSIN(SDNode *N);
414  SDValue SoftenFloatRes_FSQRT(SDNode *N);
415  SDValue SoftenFloatRes_FSUB(SDNode *N);
416  SDValue SoftenFloatRes_FTRUNC(SDNode *N);
417  SDValue SoftenFloatRes_LOAD(SDNode *N);
418  SDValue SoftenFloatRes_SELECT(SDNode *N);
419  SDValue SoftenFloatRes_SELECT_CC(SDNode *N);
420  SDValue SoftenFloatRes_VAARG(SDNode *N);
421  SDValue SoftenFloatRes_XINT_TO_FP(SDNode *N);
422
423  // Operand Float to Integer Conversion.
424  bool SoftenFloatOperand(SDNode *N, unsigned OpNo);
425  SDValue SoftenFloatOp_BIT_CONVERT(SDNode *N);
426  SDValue SoftenFloatOp_BR_CC(SDNode *N);
427  SDValue SoftenFloatOp_FP_ROUND(SDNode *N);
428  SDValue SoftenFloatOp_FP_TO_SINT(SDNode *N);
429  SDValue SoftenFloatOp_FP_TO_UINT(SDNode *N);
430  SDValue SoftenFloatOp_SELECT_CC(SDNode *N);
431  SDValue SoftenFloatOp_SETCC(SDNode *N);
432  SDValue SoftenFloatOp_STORE(SDNode *N, unsigned OpNo);
433
434  void SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
435                           ISD::CondCode &CCCode, DebugLoc dl);
436
437  //===--------------------------------------------------------------------===//
438  // Float Expansion Support: LegalizeFloatTypes.cpp
439  //===--------------------------------------------------------------------===//
440
441  /// GetExpandedFloat - Given a processed operand Op which was expanded into
442  /// two floating point values of half the size, this returns the two halves.
443  /// The low bits of Op are exactly equal to the bits of Lo; the high bits
444  /// exactly equal Hi.  For example, if Op is a ppcf128 which was expanded
445  /// into two f64's, then this method returns the two f64's, with Lo being
446  /// equal to the lower 64 bits of Op, and Hi to the upper 64 bits.
447  void GetExpandedFloat(SDValue Op, SDValue &Lo, SDValue &Hi);
448  void SetExpandedFloat(SDValue Op, SDValue Lo, SDValue Hi);
449
450  // Float Result Expansion.
451  void ExpandFloatResult(SDNode *N, unsigned ResNo);
452  void ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo, SDValue &Hi);
453  void ExpandFloatRes_FABS      (SDNode *N, SDValue &Lo, SDValue &Hi);
454  void ExpandFloatRes_FADD      (SDNode *N, SDValue &Lo, SDValue &Hi);
455  void ExpandFloatRes_FCEIL     (SDNode *N, SDValue &Lo, SDValue &Hi);
456  void ExpandFloatRes_FCOS      (SDNode *N, SDValue &Lo, SDValue &Hi);
457  void ExpandFloatRes_FDIV      (SDNode *N, SDValue &Lo, SDValue &Hi);
458  void ExpandFloatRes_FEXP      (SDNode *N, SDValue &Lo, SDValue &Hi);
459  void ExpandFloatRes_FEXP2     (SDNode *N, SDValue &Lo, SDValue &Hi);
460  void ExpandFloatRes_FFLOOR    (SDNode *N, SDValue &Lo, SDValue &Hi);
461  void ExpandFloatRes_FLOG      (SDNode *N, SDValue &Lo, SDValue &Hi);
462  void ExpandFloatRes_FLOG2     (SDNode *N, SDValue &Lo, SDValue &Hi);
463  void ExpandFloatRes_FLOG10    (SDNode *N, SDValue &Lo, SDValue &Hi);
464  void ExpandFloatRes_FMUL      (SDNode *N, SDValue &Lo, SDValue &Hi);
465  void ExpandFloatRes_FNEARBYINT(SDNode *N, SDValue &Lo, SDValue &Hi);
466  void ExpandFloatRes_FNEG      (SDNode *N, SDValue &Lo, SDValue &Hi);
467  void ExpandFloatRes_FP_EXTEND (SDNode *N, SDValue &Lo, SDValue &Hi);
468  void ExpandFloatRes_FPOW      (SDNode *N, SDValue &Lo, SDValue &Hi);
469  void ExpandFloatRes_FPOWI     (SDNode *N, SDValue &Lo, SDValue &Hi);
470  void ExpandFloatRes_FRINT     (SDNode *N, SDValue &Lo, SDValue &Hi);
471  void ExpandFloatRes_FSIN      (SDNode *N, SDValue &Lo, SDValue &Hi);
472  void ExpandFloatRes_FSQRT     (SDNode *N, SDValue &Lo, SDValue &Hi);
473  void ExpandFloatRes_FSUB      (SDNode *N, SDValue &Lo, SDValue &Hi);
474  void ExpandFloatRes_FTRUNC    (SDNode *N, SDValue &Lo, SDValue &Hi);
475  void ExpandFloatRes_LOAD      (SDNode *N, SDValue &Lo, SDValue &Hi);
476  void ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo, SDValue &Hi);
477
478  // Float Operand Expansion.
479  bool ExpandFloatOperand(SDNode *N, unsigned OperandNo);
480  SDValue ExpandFloatOp_BR_CC(SDNode *N);
481  SDValue ExpandFloatOp_FP_ROUND(SDNode *N);
482  SDValue ExpandFloatOp_FP_TO_SINT(SDNode *N);
483  SDValue ExpandFloatOp_FP_TO_UINT(SDNode *N);
484  SDValue ExpandFloatOp_SELECT_CC(SDNode *N);
485  SDValue ExpandFloatOp_SETCC(SDNode *N);
486  SDValue ExpandFloatOp_STORE(SDNode *N, unsigned OpNo);
487
488  void FloatExpandSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
489                                ISD::CondCode &CCCode, DebugLoc dl);
490
491  //===--------------------------------------------------------------------===//
492  // Scalarization Support: LegalizeVectorTypes.cpp
493  //===--------------------------------------------------------------------===//
494
495  /// GetScalarizedVector - Given a processed one-element vector Op which was
496  /// scalarized to its element type, this returns the element.  For example,
497  /// if Op is a v1i32, Op = < i32 val >, this method returns val, an i32.
498  SDValue GetScalarizedVector(SDValue Op) {
499    SDValue &ScalarizedOp = ScalarizedVectors[Op];
500    RemapValue(ScalarizedOp);
501    assert(ScalarizedOp.getNode() && "Operand wasn't scalarized?");
502    return ScalarizedOp;
503  }
504  void SetScalarizedVector(SDValue Op, SDValue Result);
505
506  // Vector Result Scalarization: <1 x ty> -> ty.
507  void ScalarizeVectorResult(SDNode *N, unsigned OpNo);
508  SDValue ScalarizeVecRes_BinOp(SDNode *N);
509  SDValue ScalarizeVecRes_ShiftOp(SDNode *N);
510  SDValue ScalarizeVecRes_UnaryOp(SDNode *N);
511
512  SDValue ScalarizeVecRes_BIT_CONVERT(SDNode *N);
513  SDValue ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N);
514  SDValue ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N);
515  SDValue ScalarizeVecRes_FPOWI(SDNode *N);
516  SDValue ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N);
517  SDValue ScalarizeVecRes_LOAD(LoadSDNode *N);
518  SDValue ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N);
519  SDValue ScalarizeVecRes_SELECT(SDNode *N);
520  SDValue ScalarizeVecRes_SELECT_CC(SDNode *N);
521  SDValue ScalarizeVecRes_UNDEF(SDNode *N);
522  SDValue ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N);
523  SDValue ScalarizeVecRes_VSETCC(SDNode *N);
524
525  // Vector Operand Scalarization: <1 x ty> -> ty.
526  bool ScalarizeVectorOperand(SDNode *N, unsigned OpNo);
527  SDValue ScalarizeVecOp_BIT_CONVERT(SDNode *N);
528  SDValue ScalarizeVecOp_CONCAT_VECTORS(SDNode *N);
529  SDValue ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N);
530  SDValue ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo);
531
532  //===--------------------------------------------------------------------===//
533  // Vector Splitting Support: LegalizeVectorTypes.cpp
534  //===--------------------------------------------------------------------===//
535
536  /// GetSplitVector - Given a processed vector Op which was split into smaller
537  /// vectors, this method returns the smaller vectors.  The first elements of
538  /// Op coincide with the elements of Lo; the remaining elements of Op coincide
539  /// with the elements of Hi: Op is what you would get by concatenating Lo and
540  /// Hi.  For example, if Op is a v8i32 that was split into two v4i32's, then
541  /// this method returns the two v4i32's, with Lo corresponding to the first 4
542  /// elements of Op, and Hi to the last 4 elements.
543  void GetSplitVector(SDValue Op, SDValue &Lo, SDValue &Hi);
544  void SetSplitVector(SDValue Op, SDValue Lo, SDValue Hi);
545
546  // Vector Result Splitting: <128 x ty> -> 2 x <64 x ty>.
547  void SplitVectorResult(SDNode *N, unsigned OpNo);
548  void SplitVecRes_BinOp(SDNode *N, SDValue &Lo, SDValue &Hi);
549  void SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo, SDValue &Hi);
550
551  void SplitVecRes_BIT_CONVERT(SDNode *N, SDValue &Lo, SDValue &Hi);
552  void SplitVecRes_BUILD_PAIR(SDNode *N, SDValue &Lo, SDValue &Hi);
553  void SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo, SDValue &Hi);
554  void SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo, SDValue &Hi);
555  void SplitVecRes_CONVERT_RNDSAT(SDNode *N, SDValue &Lo, SDValue &Hi);
556  void SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo, SDValue &Hi);
557  void SplitVecRes_FPOWI(SDNode *N, SDValue &Lo, SDValue &Hi);
558  void SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo, SDValue &Hi);
559  void SplitVecRes_LOAD(LoadSDNode *N, SDValue &Lo, SDValue &Hi);
560  void SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo, SDValue &Hi);
561  void SplitVecRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi);
562  void SplitVecRes_VECTOR_SHUFFLE(SDNode *N, SDValue &Lo, SDValue &Hi);
563  void SplitVecRes_VSETCC(SDNode *N, SDValue &Lo, SDValue &Hi);
564
565  // Vector Operand Splitting: <128 x ty> -> 2 x <64 x ty>.
566  bool SplitVectorOperand(SDNode *N, unsigned OpNo);
567  SDValue SplitVecOp_UnaryOp(SDNode *N);
568
569  SDValue SplitVecOp_BIT_CONVERT(SDNode *N);
570  SDValue SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N);
571  SDValue SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N);
572  SDValue SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo);
573  SDValue SplitVecOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo);
574
575  //===--------------------------------------------------------------------===//
576  // Vector Widening Support: LegalizeVectorTypes.cpp
577  //===--------------------------------------------------------------------===//
578
579  /// GetWidenedVector - Given a processed vector Op which was widened into a
580  /// larger vector, this method returns the larger vector.  The elements of
581  /// the returned vector consist of the elements of Op followed by elements
582  /// containing rubbish.  For example, if Op is a v2i32 that was widened to a
583  /// v4i32, then this method returns a v4i32 for which the first two elements
584  /// are the same as those of Op, while the last two elements contain rubbish.
585  SDValue GetWidenedVector(SDValue Op) {
586    SDValue &WidenedOp = WidenedVectors[Op];
587    RemapValue(WidenedOp);
588    assert(WidenedOp.getNode() && "Operand wasn't widened?");
589    return WidenedOp;
590  }
591  void SetWidenedVector(SDValue Op, SDValue Result);
592
593  // Widen Vector Result Promotion.
594  void WidenVectorResult(SDNode *N, unsigned ResNo);
595  SDValue WidenVecRes_BIT_CONVERT(SDNode* N);
596  SDValue WidenVecRes_BUILD_VECTOR(SDNode* N);
597  SDValue WidenVecRes_CONCAT_VECTORS(SDNode* N);
598  SDValue WidenVecRes_CONVERT_RNDSAT(SDNode* N);
599  SDValue WidenVecRes_EXTRACT_SUBVECTOR(SDNode* N);
600  SDValue WidenVecRes_INSERT_VECTOR_ELT(SDNode* N);
601  SDValue WidenVecRes_LOAD(SDNode* N);
602  SDValue WidenVecRes_SCALAR_TO_VECTOR(SDNode* N);
603  SDValue WidenVecRes_SELECT(SDNode* N);
604  SDValue WidenVecRes_SELECT_CC(SDNode* N);
605  SDValue WidenVecRes_UNDEF(SDNode *N);
606  SDValue WidenVecRes_VECTOR_SHUFFLE(SDNode *N);
607  SDValue WidenVecRes_VSETCC(SDNode* N);
608
609  SDValue WidenVecRes_Binary(SDNode *N);
610  SDValue WidenVecRes_Convert(SDNode *N);
611  SDValue WidenVecRes_Shift(SDNode *N);
612  SDValue WidenVecRes_Unary(SDNode *N);
613
614  // Widen Vector Operand.
615  bool WidenVectorOperand(SDNode *N, unsigned ResNo);
616  SDValue WidenVecOp_BIT_CONVERT(SDNode *N);
617  SDValue WidenVecOp_CONCAT_VECTORS(SDNode *N);
618  SDValue WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N);
619  SDValue WidenVecOp_STORE(SDNode* N);
620
621  SDValue WidenVecOp_Convert(SDNode *N);
622
623  //===--------------------------------------------------------------------===//
624  // Vector Widening Utilities Support: LegalizeVectorTypes.cpp
625  //===--------------------------------------------------------------------===//
626
627  /// Helper genWidenVectorLoads - Helper function to generate a set of
628  /// loads to load a vector with a resulting wider type. It takes
629  ///   ExtType: Extension type
630  ///   LdChain: list of chains for the load we have generated.
631  ///   Chain:   incoming chain for the ld vector.
632  ///   BasePtr: base pointer to load from.
633  ///   SV:         memory disambiguation source value.
634  ///   SVOffset:   memory disambiugation offset.
635  ///   Alignment:  alignment of the memory.
636  ///   isVolatile: volatile load.
637  ///   LdWidth:    width of memory that we want to load.
638  ///   ResType:    the wider result result type for the resulting vector.
639  ///   dl:         DebugLoc to be applied to new nodes
640  SDValue GenWidenVectorLoads(SmallVector<SDValue, 16>& LdChain, SDValue Chain,
641                              SDValue BasePtr, const Value *SV,
642                              int SVOffset, unsigned Alignment,
643                              bool isVolatile, unsigned LdWidth,
644                              MVT ResType, DebugLoc dl);
645
646  /// Helper genWidenVectorStores - Helper function to generate a set of
647  /// stores to store a widen vector into non widen memory
648  /// It takes
649  ///   StChain: list of chains for the stores we have generated
650  ///   Chain:   incoming chain for the ld vector
651  ///   BasePtr: base pointer to load from
652  ///   SV:      memory disambiguation source value
653  ///   SVOffset:   memory disambiugation offset
654  ///   Alignment:  alignment of the memory
655  ///   isVolatile: volatile lod
656  ///   ValOp:   value to store
657  ///   StWidth: width of memory that we want to store
658  ///   dl:         DebugLoc to be applied to new nodes
659  void GenWidenVectorStores(SmallVector<SDValue, 16>& StChain, SDValue Chain,
660                            SDValue BasePtr, const Value *SV,
661                            int SVOffset, unsigned Alignment,
662                            bool isVolatile, SDValue ValOp,
663                            unsigned StWidth, DebugLoc dl);
664
665  /// Modifies a vector input (widen or narrows) to a vector of NVT.  The
666  /// input vector must have the same element type as NVT.
667  SDValue ModifyToType(SDValue InOp, MVT WidenVT);
668
669
670  //===--------------------------------------------------------------------===//
671  // Generic Splitting: LegalizeTypesGeneric.cpp
672  //===--------------------------------------------------------------------===//
673
674  // Legalization methods which only use that the illegal type is split into two
675  // not necessarily identical types.  As such they can be used for splitting
676  // vectors and expanding integers and floats.
677
678  void GetSplitOp(SDValue Op, SDValue &Lo, SDValue &Hi) {
679    if (Op.getValueType().isVector())
680      GetSplitVector(Op, Lo, Hi);
681    else if (Op.getValueType().isInteger())
682      GetExpandedInteger(Op, Lo, Hi);
683    else
684      GetExpandedFloat(Op, Lo, Hi);
685  }
686
687  /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
688  /// which is split (or expanded) into two not necessarily identical pieces.
689  void GetSplitDestVTs(MVT InVT, MVT &LoVT, MVT &HiVT);
690
691  // Generic Result Splitting.
692  void SplitRes_MERGE_VALUES(SDNode *N, SDValue &Lo, SDValue &Hi);
693  void SplitRes_SELECT      (SDNode *N, SDValue &Lo, SDValue &Hi);
694  void SplitRes_SELECT_CC   (SDNode *N, SDValue &Lo, SDValue &Hi);
695  void SplitRes_UNDEF       (SDNode *N, SDValue &Lo, SDValue &Hi);
696
697  //===--------------------------------------------------------------------===//
698  // Generic Expansion: LegalizeTypesGeneric.cpp
699  //===--------------------------------------------------------------------===//
700
701  // Legalization methods which only use that the illegal type is split into two
702  // identical types of half the size, and that the Lo/Hi part is stored first
703  // in memory on little/big-endian machines, followed by the Hi/Lo part.  As
704  // such they can be used for expanding integers and floats.
705
706  void GetExpandedOp(SDValue Op, SDValue &Lo, SDValue &Hi) {
707    if (Op.getValueType().isInteger())
708      GetExpandedInteger(Op, Lo, Hi);
709    else
710      GetExpandedFloat(Op, Lo, Hi);
711  }
712
713  // Generic Result Expansion.
714  void ExpandRes_BIT_CONVERT       (SDNode *N, SDValue &Lo, SDValue &Hi);
715  void ExpandRes_BUILD_PAIR        (SDNode *N, SDValue &Lo, SDValue &Hi);
716  void ExpandRes_EXTRACT_ELEMENT   (SDNode *N, SDValue &Lo, SDValue &Hi);
717  void ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo, SDValue &Hi);
718  void ExpandRes_NormalLoad        (SDNode *N, SDValue &Lo, SDValue &Hi);
719  void ExpandRes_VAARG             (SDNode *N, SDValue &Lo, SDValue &Hi);
720
721  // Generic Operand Expansion.
722  SDValue ExpandOp_BIT_CONVERT      (SDNode *N);
723  SDValue ExpandOp_BUILD_VECTOR     (SDNode *N);
724  SDValue ExpandOp_EXTRACT_ELEMENT  (SDNode *N);
725  SDValue ExpandOp_INSERT_VECTOR_ELT(SDNode *N);
726  SDValue ExpandOp_SCALAR_TO_VECTOR (SDNode *N);
727  SDValue ExpandOp_NormalStore      (SDNode *N, unsigned OpNo);
728};
729
730} // end namespace llvm.
731
732#endif
733