Handle load/store of misaligned vectors that are the
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypesPromote.cpp
1 //===-- LegalizeTypesPromote.cpp - Promotion for LegalizeTypes ------------===//
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 implements promotion support for LegalizeTypes.  Promotion is the
11 // act of changing a computation in an invalid type to be a computation in a 
12 // larger type.  For example, implementing i8 arithmetic in an i32 register (as
13 // is often needed on powerpc for example).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "LegalizeTypes.h"
18 using namespace llvm;
19
20 //===----------------------------------------------------------------------===//
21 //  Result Promotion
22 //===----------------------------------------------------------------------===//
23
24 /// PromoteResult - This method is called when a result of a node is found to be
25 /// in need of promotion to a larger type.  At this point, the node may also
26 /// have invalid operands or may have other results that need expansion, we just
27 /// know that (at least) one result needs promotion.
28 void DAGTypeLegalizer::PromoteResult(SDNode *N, unsigned ResNo) {
29   DEBUG(cerr << "Promote node result: "; N->dump(&DAG); cerr << "\n");
30   SDOperand Result = SDOperand();
31   
32   switch (N->getOpcode()) {
33   default:
34 #ifndef NDEBUG
35     cerr << "PromoteResult #" << ResNo << ": ";
36     N->dump(&DAG); cerr << "\n";
37 #endif
38     assert(0 && "Do not know how to promote this operator!");
39     abort();
40   case ISD::UNDEF:    Result = PromoteResult_UNDEF(N); break;
41   case ISD::Constant: Result = PromoteResult_Constant(N); break;
42
43   case ISD::TRUNCATE:    Result = PromoteResult_TRUNCATE(N); break;
44   case ISD::SIGN_EXTEND:
45   case ISD::ZERO_EXTEND:
46   case ISD::ANY_EXTEND:  Result = PromoteResult_INT_EXTEND(N); break;
47   case ISD::FP_ROUND:    Result = PromoteResult_FP_ROUND(N); break;
48   case ISD::FP_TO_SINT:
49   case ISD::FP_TO_UINT:  Result = PromoteResult_FP_TO_XINT(N); break;
50   case ISD::SETCC:    Result = PromoteResult_SETCC(N); break;
51   case ISD::LOAD:     Result = PromoteResult_LOAD(cast<LoadSDNode>(N)); break;
52   case ISD::BUILD_PAIR:  Result = PromoteResult_BUILD_PAIR(N); break;
53   case ISD::BIT_CONVERT: Result = PromoteResult_BIT_CONVERT(N); break;
54
55   case ISD::AND:
56   case ISD::OR:
57   case ISD::XOR:
58   case ISD::ADD:
59   case ISD::SUB:
60   case ISD::MUL:      Result = PromoteResult_SimpleIntBinOp(N); break;
61
62   case ISD::SDIV:
63   case ISD::SREM:     Result = PromoteResult_SDIV(N); break;
64
65   case ISD::UDIV:
66   case ISD::UREM:     Result = PromoteResult_UDIV(N); break;
67
68   case ISD::SHL:      Result = PromoteResult_SHL(N); break;
69   case ISD::SRA:      Result = PromoteResult_SRA(N); break;
70   case ISD::SRL:      Result = PromoteResult_SRL(N); break;
71
72   case ISD::SELECT:    Result = PromoteResult_SELECT(N); break;
73   case ISD::SELECT_CC: Result = PromoteResult_SELECT_CC(N); break;
74
75   case ISD::CTLZ:     Result = PromoteResult_CTLZ(N); break;
76   case ISD::CTPOP:    Result = PromoteResult_CTPOP(N); break;
77   case ISD::CTTZ:     Result = PromoteResult_CTTZ(N); break;
78
79   case ISD::EXTRACT_VECTOR_ELT:
80     Result = PromoteResult_EXTRACT_VECTOR_ELT(N);
81     break;
82   }      
83
84   // If Result is null, the sub-method took care of registering the result.
85   if (Result.Val)
86     SetPromotedOp(SDOperand(N, ResNo), Result);
87 }
88
89 SDOperand DAGTypeLegalizer::PromoteResult_UNDEF(SDNode *N) {
90   return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
91 }
92
93 SDOperand DAGTypeLegalizer::PromoteResult_Constant(SDNode *N) {
94   MVT::ValueType VT = N->getValueType(0);
95   // Zero extend things like i1, sign extend everything else.  It shouldn't
96   // matter in theory which one we pick, but this tends to give better code?
97   unsigned Opc = VT != MVT::i1 ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
98   SDOperand Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
99                                  SDOperand(N, 0));
100   assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
101   return Result;
102 }
103
104 SDOperand DAGTypeLegalizer::PromoteResult_TRUNCATE(SDNode *N) {
105   SDOperand Res;
106
107   switch (getTypeAction(N->getOperand(0).getValueType())) {
108   default: assert(0 && "Unknown type action!");
109   case Legal:
110   case Expand:
111     Res = N->getOperand(0);
112     break;
113   case Promote:
114     Res = GetPromotedOp(N->getOperand(0));
115     break;
116   }
117
118   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
119   assert(MVT::getSizeInBits(Res.getValueType()) >= MVT::getSizeInBits(NVT) &&
120          "Truncation doesn't make sense!");
121   if (Res.getValueType() == NVT)
122     return Res;
123
124   // Truncate to NVT instead of VT
125   return DAG.getNode(ISD::TRUNCATE, NVT, Res);
126 }
127
128 SDOperand DAGTypeLegalizer::PromoteResult_INT_EXTEND(SDNode *N) {
129   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
130
131   if (getTypeAction(N->getOperand(0).getValueType()) == Promote) {
132     SDOperand Res = GetPromotedOp(N->getOperand(0));
133     assert(MVT::getSizeInBits(Res.getValueType()) <= MVT::getSizeInBits(NVT) &&
134            "Extension doesn't make sense!");
135
136     // If the result and operand types are the same after promotion, simplify
137     // to an in-register extension.
138     if (NVT == Res.getValueType()) {
139       // The high bits are not guaranteed to be anything.  Insert an extend.
140       if (N->getOpcode() == ISD::SIGN_EXTEND)
141         return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
142                            DAG.getValueType(N->getOperand(0).getValueType()));
143       if (N->getOpcode() == ISD::ZERO_EXTEND)
144         return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
145       assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
146       return Res;
147     }
148   }
149
150   // Otherwise, just extend the original operand all the way to the larger type.
151   return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
152 }
153
154 SDOperand DAGTypeLegalizer::PromoteResult_FP_ROUND(SDNode *N) {
155   // NOTE: Assumes input is legal.
156   if (N->getConstantOperandVal(1) == 0) 
157     return DAG.getNode(ISD::FP_ROUND_INREG, N->getOperand(0).getValueType(),
158                        N->getOperand(0), DAG.getValueType(N->getValueType(0)));
159   // If the precision discard isn't needed, just return the operand unrounded.
160   return N->getOperand(0);
161 }
162
163 SDOperand DAGTypeLegalizer::PromoteResult_FP_TO_XINT(SDNode *N) {
164   SDOperand Op = N->getOperand(0);
165   // If the operand needed to be promoted, do so now.
166   if (getTypeAction(Op.getValueType()) == Promote)
167     // The input result is prerounded, so we don't have to do anything special.
168     Op = GetPromotedOp(Op);
169   
170   unsigned NewOpc = N->getOpcode();
171   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
172   
173   // If we're promoting a UINT to a larger size, check to see if the new node
174   // will be legal.  If it isn't, check to see if FP_TO_SINT is legal, since
175   // we can use that instead.  This allows us to generate better code for
176   // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
177   // legal, such as PowerPC.
178   if (N->getOpcode() == ISD::FP_TO_UINT) {
179     if (!TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
180         (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
181          TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom))
182       NewOpc = ISD::FP_TO_SINT;
183   }
184
185   return DAG.getNode(NewOpc, NVT, Op);
186 }
187
188 SDOperand DAGTypeLegalizer::PromoteResult_SETCC(SDNode *N) {
189   assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
190   return DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), N->getOperand(0),
191                      N->getOperand(1), N->getOperand(2));
192 }
193
194 SDOperand DAGTypeLegalizer::PromoteResult_LOAD(LoadSDNode *N) {
195   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
196   ISD::LoadExtType ExtType =
197     ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
198   SDOperand Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
199                                  N->getSrcValue(), N->getSrcValueOffset(),
200                                  N->getMemoryVT(), N->isVolatile(),
201                                  N->getAlignment());
202
203   // Legalized the chain result - switch anything that used the old chain to
204   // use the new one.
205   ReplaceValueWith(SDOperand(N, 1), Res.getValue(1));
206   return Res;
207 }
208
209 SDOperand DAGTypeLegalizer::PromoteResult_BUILD_PAIR(SDNode *N) {
210   // The pair element type may be legal, or may not promote to the same type as
211   // the result, for example i16 = BUILD_PAIR (i8, i8) when i8 is legal but i16
212   // is not.  Handle all cases.
213   MVT::ValueType LVT = N->getOperand(0).getValueType();
214   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
215   SDOperand Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
216   SDOperand Hi = DAG.getNode(ISD::ANY_EXTEND, NVT, N->getOperand(1));
217   Hi = DAG.getNode(ISD::SHL, NVT, Hi, DAG.getConstant(MVT::getSizeInBits(LVT),
218                                                       TLI.getShiftAmountTy()));
219   return DAG.getNode(ISD::OR, NVT, Lo, Hi);
220 }
221
222 SDOperand DAGTypeLegalizer::PromoteResult_BIT_CONVERT(SDNode *N) {
223   SDOperand InOp = N->getOperand(0);
224   MVT::ValueType InVT = InOp.getValueType();
225   MVT::ValueType NInVT = TLI.getTypeToTransformTo(InVT);
226   MVT::ValueType OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
227
228   switch (getTypeAction(InVT)) {
229   default:
230     assert(false && "Unknown type action!");
231     break;
232   case Legal:
233     break;
234   case Promote:
235     if (MVT::getSizeInBits(OutVT) == MVT::getSizeInBits(NInVT))
236       // The input promotes to the same size.  Convert the promoted value.
237       return DAG.getNode(ISD::BIT_CONVERT, OutVT, GetPromotedOp(InOp));
238     break;
239   case Expand:
240     break;
241   case Scalarize:
242     // Convert the element to an integer and promote it by hand.
243     InOp = DAG.getNode(ISD::BIT_CONVERT,
244                        MVT::getIntegerType(MVT::getSizeInBits(InVT)),
245                        GetScalarizedOp(InOp));
246     InOp = DAG.getNode(ISD::ANY_EXTEND,
247                        MVT::getIntegerType(MVT::getSizeInBits(OutVT)), InOp);
248     return DAG.getNode(ISD::BIT_CONVERT, OutVT, InOp);
249   case Split:
250     // For example, i32 = BIT_CONVERT v2i16 on alpha.  Convert the split
251     // pieces of the input into integers and reassemble in the final type.
252     SDOperand Lo, Hi;
253     GetSplitOp(N->getOperand(0), Lo, Hi);
254
255     unsigned LoBits = MVT::getSizeInBits(Lo.getValueType());
256     Lo = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(LoBits), Lo);
257
258     unsigned HiBits = MVT::getSizeInBits(Hi.getValueType());
259     Hi = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(HiBits), Hi);
260
261     if (TLI.isBigEndian())
262       std::swap(Lo, Hi);
263
264     MVT::ValueType TargetTy = MVT::getIntegerType(MVT::getSizeInBits(OutVT));
265     Hi = DAG.getNode(ISD::ANY_EXTEND, TargetTy, Hi);
266     Hi = DAG.getNode(ISD::SHL, TargetTy, Hi,
267                      DAG.getConstant(MVT::getSizeInBits(Lo.getValueType()),
268                                      TLI.getShiftAmountTy()));
269     Lo = DAG.getNode(ISD::ZERO_EXTEND, TargetTy, Lo);
270
271     return DAG.getNode(ISD::BIT_CONVERT, OutVT,
272                        DAG.getNode(ISD::OR, TargetTy, Lo, Hi));
273   }
274
275   // Otherwise, lower the bit-convert to a store/load from the stack, then
276   // promote the load.
277   SDOperand Op = CreateStackStoreLoad(InOp, N->getValueType(0));
278   return PromoteResult_LOAD(cast<LoadSDNode>(Op.Val));
279 }
280
281 SDOperand DAGTypeLegalizer::PromoteResult_SimpleIntBinOp(SDNode *N) {
282   // The input may have strange things in the top bits of the registers, but
283   // these operations don't care.  They may have weird bits going out, but
284   // that too is okay if they are integer operations.
285   SDOperand LHS = GetPromotedOp(N->getOperand(0));
286   SDOperand RHS = GetPromotedOp(N->getOperand(1));
287   return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
288 }
289
290 SDOperand DAGTypeLegalizer::PromoteResult_SDIV(SDNode *N) {
291   // Sign extend the input.
292   SDOperand LHS = GetPromotedOp(N->getOperand(0));
293   SDOperand RHS = GetPromotedOp(N->getOperand(1));
294   MVT::ValueType VT = N->getValueType(0);
295   LHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, LHS.getValueType(), LHS,
296                     DAG.getValueType(VT));
297   RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, RHS.getValueType(), RHS,
298                     DAG.getValueType(VT));
299
300   return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
301 }
302
303 SDOperand DAGTypeLegalizer::PromoteResult_UDIV(SDNode *N) {
304   // Zero extend the input.
305   SDOperand LHS = GetPromotedOp(N->getOperand(0));
306   SDOperand RHS = GetPromotedOp(N->getOperand(1));
307   MVT::ValueType VT = N->getValueType(0);
308   LHS = DAG.getZeroExtendInReg(LHS, VT);
309   RHS = DAG.getZeroExtendInReg(RHS, VT);
310
311   return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
312 }
313
314 SDOperand DAGTypeLegalizer::PromoteResult_SHL(SDNode *N) {
315   return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
316                      GetPromotedOp(N->getOperand(0)), N->getOperand(1));
317 }
318
319 SDOperand DAGTypeLegalizer::PromoteResult_SRA(SDNode *N) {
320   // The input value must be properly sign extended.
321   MVT::ValueType VT = N->getValueType(0);
322   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
323   SDOperand Res = GetPromotedOp(N->getOperand(0));
324   Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
325   return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
326 }
327
328 SDOperand DAGTypeLegalizer::PromoteResult_SRL(SDNode *N) {
329   // The input value must be properly zero extended.
330   MVT::ValueType VT = N->getValueType(0);
331   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
332   SDOperand Res = GetPromotedZExtOp(N->getOperand(0));
333   return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
334 }
335
336 SDOperand DAGTypeLegalizer::PromoteResult_SELECT(SDNode *N) {
337   SDOperand LHS = GetPromotedOp(N->getOperand(1));
338   SDOperand RHS = GetPromotedOp(N->getOperand(2));
339   return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
340 }
341
342 SDOperand DAGTypeLegalizer::PromoteResult_SELECT_CC(SDNode *N) {
343   SDOperand LHS = GetPromotedOp(N->getOperand(2));
344   SDOperand RHS = GetPromotedOp(N->getOperand(3));
345   return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
346                      N->getOperand(1), LHS, RHS, N->getOperand(4));
347 }
348
349 SDOperand DAGTypeLegalizer::PromoteResult_CTLZ(SDNode *N) {
350   SDOperand Op = GetPromotedOp(N->getOperand(0));
351   MVT::ValueType OVT = N->getValueType(0);
352   MVT::ValueType NVT = Op.getValueType();
353   // Zero extend to the promoted type and do the count there.
354   Op = DAG.getNode(ISD::CTLZ, NVT, DAG.getZeroExtendInReg(Op, OVT));
355   // Subtract off the extra leading bits in the bigger type.
356   return DAG.getNode(ISD::SUB, NVT, Op,
357                      DAG.getConstant(MVT::getSizeInBits(NVT) -
358                                      MVT::getSizeInBits(OVT), NVT));
359 }
360
361 SDOperand DAGTypeLegalizer::PromoteResult_CTPOP(SDNode *N) {
362   SDOperand Op = GetPromotedOp(N->getOperand(0));
363   MVT::ValueType OVT = N->getValueType(0);
364   MVT::ValueType NVT = Op.getValueType();
365   // Zero extend to the promoted type and do the count there.
366   return DAG.getNode(ISD::CTPOP, NVT, DAG.getZeroExtendInReg(Op, OVT));
367 }
368
369 SDOperand DAGTypeLegalizer::PromoteResult_CTTZ(SDNode *N) {
370   SDOperand Op = GetPromotedOp(N->getOperand(0));
371   MVT::ValueType OVT = N->getValueType(0);
372   MVT::ValueType NVT = Op.getValueType();
373   // The count is the same in the promoted type except if the original
374   // value was zero.  This can be handled by setting the bit just off
375   // the top of the original type.
376   Op = DAG.getNode(ISD::OR, NVT, Op,
377                    // FIXME: Do this using an APINT constant.
378                    DAG.getConstant(1UL << MVT::getSizeInBits(OVT), NVT));
379   return DAG.getNode(ISD::CTTZ, NVT, Op);
380 }
381
382 SDOperand DAGTypeLegalizer::PromoteResult_EXTRACT_VECTOR_ELT(SDNode *N) {
383   MVT::ValueType OldVT = N->getValueType(0);
384   SDOperand OldVec = N->getOperand(0);
385   unsigned OldElts = MVT::getVectorNumElements(OldVec.getValueType());
386
387   if (OldElts == 1) {
388     assert(!isTypeLegal(OldVec.getValueType()) &&
389            "Legal one-element vector of a type needing promotion!");
390     // It is tempting to follow GetScalarizedOp by a call to GetPromotedOp,
391     // but this would be wrong because the scalarized value may not yet have
392     // been processed.
393     return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT),
394                        GetScalarizedOp(OldVec));
395   }
396
397   // Convert to a vector half as long with an element type of twice the width,
398   // for example <4 x i16> -> <2 x i32>.
399   assert(!(OldElts & 1) && "Odd length vectors not supported!");
400   MVT::ValueType NewVT = MVT::getIntegerType(2 * MVT::getSizeInBits(OldVT));
401   assert(!MVT::isExtendedVT(OldVT) && !MVT::isExtendedVT(NewVT));
402
403   SDOperand NewVec = DAG.getNode(ISD::BIT_CONVERT,
404                                  MVT::getVectorType(NewVT, OldElts / 2),
405                                  OldVec);
406
407   // Extract the element at OldIdx / 2 from the new vector.
408   SDOperand OldIdx = N->getOperand(1);
409   SDOperand NewIdx = DAG.getNode(ISD::SRL, OldIdx.getValueType(), OldIdx,
410                                  DAG.getConstant(1, TLI.getShiftAmountTy()));
411   SDOperand Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, NewIdx);
412
413   // Select the appropriate half of the element: Lo if OldIdx was even,
414   // Hi if it was odd.
415   SDOperand Lo = Elt;
416   SDOperand Hi = DAG.getNode(ISD::SRL, NewVT, Elt,
417                              DAG.getConstant(MVT::getSizeInBits(OldVT),
418                                              TLI.getShiftAmountTy()));
419   if (TLI.isBigEndian())
420     std::swap(Lo, Hi);
421
422   SDOperand Odd = DAG.getNode(ISD::AND, OldIdx.getValueType(), OldIdx,
423                               DAG.getConstant(1, TLI.getShiftAmountTy()));
424   return DAG.getNode(ISD::SELECT, NewVT, Odd, Hi, Lo);
425 }
426
427 //===----------------------------------------------------------------------===//
428 //  Operand Promotion
429 //===----------------------------------------------------------------------===//
430
431 /// PromoteOperand - This method is called when the specified operand of the
432 /// specified node is found to need promotion.  At this point, all of the result
433 /// types of the node are known to be legal, but other operands of the node may
434 /// need promotion or expansion as well as the specified one.
435 bool DAGTypeLegalizer::PromoteOperand(SDNode *N, unsigned OpNo) {
436   DEBUG(cerr << "Promote node operand: "; N->dump(&DAG); cerr << "\n");
437   SDOperand Res;
438   switch (N->getOpcode()) {
439     default:
440 #ifndef NDEBUG
441     cerr << "PromoteOperand Op #" << OpNo << ": ";
442     N->dump(&DAG); cerr << "\n";
443 #endif
444     assert(0 && "Do not know how to promote this operator's operand!");
445     abort();
446     
447   case ISD::ANY_EXTEND:  Res = PromoteOperand_ANY_EXTEND(N); break;
448   case ISD::ZERO_EXTEND: Res = PromoteOperand_ZERO_EXTEND(N); break;
449   case ISD::SIGN_EXTEND: Res = PromoteOperand_SIGN_EXTEND(N); break;
450   case ISD::TRUNCATE:    Res = PromoteOperand_TRUNCATE(N); break;
451   case ISD::FP_EXTEND:   Res = PromoteOperand_FP_EXTEND(N); break;
452   case ISD::FP_ROUND:    Res = PromoteOperand_FP_ROUND(N); break;
453   case ISD::SINT_TO_FP:
454   case ISD::UINT_TO_FP:  Res = PromoteOperand_INT_TO_FP(N); break;
455   case ISD::BUILD_PAIR:  Res = PromoteOperand_BUILD_PAIR(N); break;
456
457   case ISD::SELECT:      Res = PromoteOperand_SELECT(N, OpNo); break;
458   case ISD::BRCOND:      Res = PromoteOperand_BRCOND(N, OpNo); break;
459   case ISD::BR_CC:       Res = PromoteOperand_BR_CC(N, OpNo); break;
460   case ISD::SETCC:       Res = PromoteOperand_SETCC(N, OpNo); break;
461
462   case ISD::STORE:       Res = PromoteOperand_STORE(cast<StoreSDNode>(N),
463                                                     OpNo); break;
464   case ISD::MEMSET:
465   case ISD::MEMCPY:
466   case ISD::MEMMOVE:     Res = HandleMemIntrinsic(N); break;
467
468   case ISD::BUILD_VECTOR: Res = PromoteOperand_BUILD_VECTOR(N); break;
469   case ISD::INSERT_VECTOR_ELT:
470     Res = PromoteOperand_INSERT_VECTOR_ELT(N, OpNo);
471     break;
472
473   case ISD::RET:         Res = PromoteOperand_RET(N, OpNo); break;
474
475   case ISD::MEMBARRIER:  Res = PromoteOperand_MEMBARRIER(N); break;
476   }
477
478   // If the result is null, the sub-method took care of registering results etc.
479   if (!Res.Val) return false;
480   // If the result is N, the sub-method updated N in place.
481   if (Res.Val == N) {
482     // Mark N as new and remark N and its operands.  This allows us to correctly
483     // revisit N if it needs another step of promotion and allows us to visit
484     // any new operands to N.
485     ReanalyzeNode(N);
486     return true;
487   }
488
489   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
490          "Invalid operand expansion");
491   
492   ReplaceValueWith(SDOperand(N, 0), Res);
493   return false;
494 }
495
496 SDOperand DAGTypeLegalizer::PromoteOperand_ANY_EXTEND(SDNode *N) {
497   SDOperand Op = GetPromotedOp(N->getOperand(0));
498   return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
499 }
500
501 SDOperand DAGTypeLegalizer::PromoteOperand_ZERO_EXTEND(SDNode *N) {
502   SDOperand Op = GetPromotedOp(N->getOperand(0));
503   Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
504   return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
505 }
506
507 SDOperand DAGTypeLegalizer::PromoteOperand_SIGN_EXTEND(SDNode *N) {
508   SDOperand Op = GetPromotedOp(N->getOperand(0));
509   Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
510   return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
511                      Op, DAG.getValueType(N->getOperand(0).getValueType()));
512 }
513
514 SDOperand DAGTypeLegalizer::PromoteOperand_TRUNCATE(SDNode *N) {
515   SDOperand Op = GetPromotedOp(N->getOperand(0));
516   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
517 }
518
519 SDOperand DAGTypeLegalizer::PromoteOperand_FP_EXTEND(SDNode *N) {
520   SDOperand Op = GetPromotedOp(N->getOperand(0));
521   return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
522 }
523
524 SDOperand DAGTypeLegalizer::PromoteOperand_FP_ROUND(SDNode *N) {
525   SDOperand Op = GetPromotedOp(N->getOperand(0));
526   return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op,
527                      DAG.getIntPtrConstant(0));
528 }
529
530 SDOperand DAGTypeLegalizer::PromoteOperand_INT_TO_FP(SDNode *N) {
531   SDOperand In = GetPromotedOp(N->getOperand(0));
532   MVT::ValueType OpVT = N->getOperand(0).getValueType();
533   if (N->getOpcode() == ISD::UINT_TO_FP)
534     In = DAG.getZeroExtendInReg(In, OpVT);
535   else
536     In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
537                      In, DAG.getValueType(OpVT));
538   
539   return DAG.UpdateNodeOperands(SDOperand(N, 0), In);
540 }
541
542 SDOperand DAGTypeLegalizer::PromoteOperand_BUILD_PAIR(SDNode *N) {
543   // Since the result type is legal, the operands must promote to it.
544   MVT::ValueType OVT = N->getOperand(0).getValueType();
545   SDOperand Lo = GetPromotedOp(N->getOperand(0));
546   SDOperand Hi = GetPromotedOp(N->getOperand(1));
547   assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
548
549   Lo = DAG.getZeroExtendInReg(Lo, OVT);
550   Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
551                    DAG.getConstant(MVT::getSizeInBits(OVT),
552                                    TLI.getShiftAmountTy()));
553   return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
554 }
555
556 SDOperand DAGTypeLegalizer::PromoteOperand_SELECT(SDNode *N, unsigned OpNo) {
557   assert(OpNo == 0 && "Only know how to promote condition");
558   SDOperand Cond = GetPromotedOp(N->getOperand(0));  // Promote the condition.
559
560   // The top bits of the promoted condition are not necessarily zero, ensure
561   // that the value is properly zero extended.
562   unsigned BitWidth = Cond.getValueSizeInBits();
563   if (!DAG.MaskedValueIsZero(Cond, 
564                              APInt::getHighBitsSet(BitWidth, BitWidth-1)))
565     Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
566
567   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
568   return DAG.UpdateNodeOperands(SDOperand(N, 0), Cond, N->getOperand(1),
569                                 N->getOperand(2));
570 }
571
572 SDOperand DAGTypeLegalizer::PromoteOperand_BRCOND(SDNode *N, unsigned OpNo) {
573   assert(OpNo == 1 && "only know how to promote condition");
574   SDOperand Cond = GetPromotedOp(N->getOperand(1));  // Promote the condition.
575   
576   // The top bits of the promoted condition are not necessarily zero, ensure
577   // that the value is properly zero extended.
578   unsigned BitWidth = Cond.getValueSizeInBits();
579   if (!DAG.MaskedValueIsZero(Cond, 
580                              APInt::getHighBitsSet(BitWidth, BitWidth-1)))
581     Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
582
583   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
584   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0), Cond,
585                                 N->getOperand(2));
586 }
587
588 SDOperand DAGTypeLegalizer::PromoteOperand_BR_CC(SDNode *N, unsigned OpNo) {
589   assert(OpNo == 2 && "Don't know how to promote this operand");
590   
591   SDOperand LHS = N->getOperand(2);
592   SDOperand RHS = N->getOperand(3);
593   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
594   
595   // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
596   // legal types.
597   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
598                                 N->getOperand(1), LHS, RHS, N->getOperand(4));
599 }
600
601 SDOperand DAGTypeLegalizer::PromoteOperand_SETCC(SDNode *N, unsigned OpNo) {
602   assert(OpNo == 0 && "Don't know how to promote this operand");
603
604   SDOperand LHS = N->getOperand(0);
605   SDOperand RHS = N->getOperand(1);
606   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
607
608   // The CC (#2) is always legal.
609   return DAG.UpdateNodeOperands(SDOperand(N, 0), LHS, RHS, N->getOperand(2));
610 }
611
612 /// PromoteSetCCOperands - Promote the operands of a comparison.  This code is
613 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
614 void DAGTypeLegalizer::PromoteSetCCOperands(SDOperand &NewLHS,SDOperand &NewRHS,
615                                             ISD::CondCode CCCode) {
616   MVT::ValueType VT = NewLHS.getValueType();
617   
618   // Get the promoted values.
619   NewLHS = GetPromotedOp(NewLHS);
620   NewRHS = GetPromotedOp(NewRHS);
621   
622   // If this is an FP compare, the operands have already been extended.
623   if (!MVT::isInteger(NewLHS.getValueType()))
624     return;
625   
626   // Otherwise, we have to insert explicit sign or zero extends.  Note
627   // that we could insert sign extends for ALL conditions, but zero extend
628   // is cheaper on many machines (an AND instead of two shifts), so prefer
629   // it.
630   switch (CCCode) {
631   default: assert(0 && "Unknown integer comparison!");
632   case ISD::SETEQ:
633   case ISD::SETNE:
634   case ISD::SETUGE:
635   case ISD::SETUGT:
636   case ISD::SETULE:
637   case ISD::SETULT:
638     // ALL of these operations will work if we either sign or zero extend
639     // the operands (including the unsigned comparisons!).  Zero extend is
640     // usually a simpler/cheaper operation, so prefer it.
641     NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
642     NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
643     return;
644   case ISD::SETGE:
645   case ISD::SETGT:
646   case ISD::SETLT:
647   case ISD::SETLE:
648     NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
649                          DAG.getValueType(VT));
650     NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
651                          DAG.getValueType(VT));
652     return;
653   }
654 }
655
656 SDOperand DAGTypeLegalizer::PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo){
657   SDOperand Ch = N->getChain(), Ptr = N->getBasePtr();
658   int SVOffset = N->getSrcValueOffset();
659   unsigned Alignment = N->getAlignment();
660   bool isVolatile = N->isVolatile();
661   
662   SDOperand Val = GetPromotedOp(N->getValue());  // Get promoted value.
663
664   assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
665   
666   // Truncate the value and store the result.
667   return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
668                            SVOffset, N->getMemoryVT(),
669                            isVolatile, Alignment);
670 }
671
672 SDOperand DAGTypeLegalizer::PromoteOperand_BUILD_VECTOR(SDNode *N) {
673   // The vector type is legal but the element type is not.  This implies
674   // that the vector is a power-of-two in length and that the element
675   // type does not have a strange size (eg: it is not i1).
676   MVT::ValueType VecVT = N->getValueType(0);
677   unsigned NumElts = MVT::getVectorNumElements(VecVT);
678   assert(!(NumElts & 1) && "Legal vector of one illegal element?");
679
680   // Build a vector of half the length out of elements of twice the bitwidth.
681   // For example <4 x i16> -> <2 x i32>.
682   MVT::ValueType OldVT = N->getOperand(0).getValueType();
683   MVT::ValueType NewVT = MVT::getIntegerType(2 * MVT::getSizeInBits(OldVT));
684   assert(!MVT::isExtendedVT(OldVT) && !MVT::isExtendedVT(NewVT));
685
686   std::vector<SDOperand> NewElts;
687   NewElts.reserve(NumElts/2);
688
689   for (unsigned i = 0; i < NumElts; i += 2) {
690     // Combine two successive elements into one promoted element.
691     SDOperand Lo = N->getOperand(i);
692     SDOperand Hi = N->getOperand(i+1);
693     if (TLI.isBigEndian())
694       std::swap(Lo, Hi);
695     NewElts.push_back(DAG.getNode(ISD::BUILD_PAIR, NewVT, Lo, Hi));
696   }
697
698   SDOperand NewVec = DAG.getNode(ISD::BUILD_VECTOR,
699                                  MVT::getVectorType(NewVT, NewElts.size()),
700                                  &NewElts[0], NewElts.size());
701
702   // Convert the new vector to the old vector type.
703   return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
704 }
705
706 SDOperand DAGTypeLegalizer::PromoteOperand_INSERT_VECTOR_ELT(SDNode *N,
707                                                              unsigned OpNo) {
708   if (OpNo == 1) {
709     // Promote the inserted value.  This is valid because the type does not
710     // have to match the vector element type.
711
712     // Check that any extra bits introduced will be truncated away.
713     assert(MVT::getSizeInBits(N->getOperand(1).getValueType()) >=
714            MVT::getSizeInBits(MVT::getVectorElementType(N->getValueType(0))) &&
715            "Type of inserted value narrower than vector element type!");
716     return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
717                                   GetPromotedOp(N->getOperand(1)),
718                                   N->getOperand(2));
719   }
720
721   assert(OpNo == 2 && "Different operand and result vector types?");
722
723   // Promote the index.
724   SDOperand Idx = N->getOperand(2);
725   Idx = DAG.getZeroExtendInReg(GetPromotedOp(Idx), Idx.getValueType());
726   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
727                                 N->getOperand(1), Idx);
728 }
729
730 SDOperand DAGTypeLegalizer::PromoteOperand_RET(SDNode *N, unsigned OpNo) {
731   assert(!(OpNo & 1) && "Return values should be legally typed!");
732   assert((N->getNumOperands() & 1) && "Wrong number of operands!");
733
734   // It's a flag.  Promote all the flags in one hit, as an optimization.
735   SmallVector<SDOperand, 8> NewValues(N->getNumOperands());
736   NewValues[0] = N->getOperand(0); // The chain
737   for (unsigned i = 1, e = N->getNumOperands(); i < e; i += 2) {
738     // The return value.
739     NewValues[i] = N->getOperand(i);
740
741     // The flag.
742     SDOperand Flag = N->getOperand(i + 1);
743     if (getTypeAction(Flag.getValueType()) == Promote)
744       // The promoted value may have rubbish in the new bits, but that
745       // doesn't matter because those bits aren't queried anyway.
746       Flag = GetPromotedOp(Flag);
747     NewValues[i + 1] = Flag;
748   }
749
750   return DAG.UpdateNodeOperands(SDOperand (N, 0),
751                                 &NewValues[0], NewValues.size());
752 }
753
754 SDOperand DAGTypeLegalizer::PromoteOperand_MEMBARRIER(SDNode *N) {
755   SDOperand NewOps[6];
756   NewOps[0] = N->getOperand(0);
757   for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
758     SDOperand Flag = GetPromotedOp(N->getOperand(i));
759     NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
760   }
761   return DAG.UpdateNodeOperands(SDOperand (N, 0), NewOps,
762                                 array_lengthof(NewOps));
763 }