1 //===-- LegalizeTypesExpand.cpp - Expansion for LegalizeTypes -------------===//
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 implements expansion support for LegalizeTypes. Expansion is the
11 // act of changing a computation in an invalid type to be a computation in
12 // multiple registers of a smaller type. For example, implementing i64
13 // arithmetic in two i32 registers (as is often needed on 32-bit targets, for
16 //===----------------------------------------------------------------------===//
18 #include "LegalizeTypes.h"
19 #include "llvm/Constants.h"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 /// ExpandResult - This method is called when the specified result of the
27 /// specified node is found to need expansion. At this point, the node may also
28 /// have invalid operands or may have other results that need promotion, we just
29 /// know that (at least) one result needs expansion.
30 void DAGTypeLegalizer::ExpandResult(SDNode *N, unsigned ResNo) {
31 DEBUG(cerr << "Expand node result: "; N->dump(&DAG); cerr << "\n");
33 Lo = Hi = SDOperand();
35 // See if the target wants to custom expand this node.
36 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
37 TargetLowering::Custom) {
38 // If the target wants to, allow it to lower this itself.
39 if (SDNode *P = TLI.ExpandOperationResult(N, DAG)) {
40 // Everything that once used N now uses P. We are guaranteed that the
41 // result value types of N and the result value types of P match.
42 ReplaceNodeWith(N, P);
47 switch (N->getOpcode()) {
50 cerr << "ExpandResult #" << ResNo << ": ";
51 N->dump(&DAG); cerr << "\n";
53 assert(0 && "Do not know how to expand the result of this operator!");
56 case ISD::UNDEF: ExpandResult_UNDEF(N, Lo, Hi); break;
57 case ISD::Constant: ExpandResult_Constant(N, Lo, Hi); break;
58 case ISD::BUILD_PAIR: ExpandResult_BUILD_PAIR(N, Lo, Hi); break;
59 case ISD::MERGE_VALUES: ExpandResult_MERGE_VALUES(N, Lo, Hi); break;
60 case ISD::ANY_EXTEND: ExpandResult_ANY_EXTEND(N, Lo, Hi); break;
61 case ISD::ZERO_EXTEND: ExpandResult_ZERO_EXTEND(N, Lo, Hi); break;
62 case ISD::SIGN_EXTEND: ExpandResult_SIGN_EXTEND(N, Lo, Hi); break;
63 case ISD::AssertZext: ExpandResult_AssertZext(N, Lo, Hi); break;
64 case ISD::TRUNCATE: ExpandResult_TRUNCATE(N, Lo, Hi); break;
65 case ISD::BIT_CONVERT: ExpandResult_BIT_CONVERT(N, Lo, Hi); break;
66 case ISD::SIGN_EXTEND_INREG: ExpandResult_SIGN_EXTEND_INREG(N, Lo, Hi); break;
67 case ISD::LOAD: ExpandResult_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
71 case ISD::XOR: ExpandResult_Logical(N, Lo, Hi); break;
72 case ISD::BSWAP: ExpandResult_BSWAP(N, Lo, Hi); break;
74 case ISD::SUB: ExpandResult_ADDSUB(N, Lo, Hi); break;
76 case ISD::SUBC: ExpandResult_ADDSUBC(N, Lo, Hi); break;
78 case ISD::SUBE: ExpandResult_ADDSUBE(N, Lo, Hi); break;
79 case ISD::SELECT: ExpandResult_SELECT(N, Lo, Hi); break;
80 case ISD::SELECT_CC: ExpandResult_SELECT_CC(N, Lo, Hi); break;
81 case ISD::MUL: ExpandResult_MUL(N, Lo, Hi); break;
84 case ISD::SRL: ExpandResult_Shift(N, Lo, Hi); break;
86 case ISD::CTLZ: ExpandResult_CTLZ(N, Lo, Hi); break;
87 case ISD::CTPOP: ExpandResult_CTPOP(N, Lo, Hi); break;
88 case ISD::CTTZ: ExpandResult_CTTZ(N, Lo, Hi); break;
91 // If Lo/Hi is null, the sub-method took care of registering results etc.
93 SetExpandedOp(SDOperand(N, ResNo), Lo, Hi);
96 void DAGTypeLegalizer::ExpandResult_UNDEF(SDNode *N,
97 SDOperand &Lo, SDOperand &Hi) {
98 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
99 Lo = Hi = DAG.getNode(ISD::UNDEF, NVT);
102 void DAGTypeLegalizer::ExpandResult_Constant(SDNode *N,
103 SDOperand &Lo, SDOperand &Hi) {
104 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
105 uint64_t Cst = cast<ConstantSDNode>(N)->getValue();
106 Lo = DAG.getConstant(Cst, NVT);
107 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
110 void DAGTypeLegalizer::ExpandResult_BUILD_PAIR(SDNode *N,
111 SDOperand &Lo, SDOperand &Hi) {
112 // Return the operands.
113 Lo = N->getOperand(0);
114 Hi = N->getOperand(1);
117 void DAGTypeLegalizer::ExpandResult_MERGE_VALUES(SDNode *N,
118 SDOperand &Lo, SDOperand &Hi) {
119 // A MERGE_VALUES node can produce any number of values. We know that the
120 // first illegal one needs to be expanded into Lo/Hi.
123 // The string of legal results gets turns into the input operands, which have
125 for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
126 ReplaceValueWith(SDOperand(N, i), SDOperand(N->getOperand(i)));
128 // The first illegal result must be the one that needs to be expanded.
129 GetExpandedOp(N->getOperand(i), Lo, Hi);
131 // Legalize the rest of the results into the input operands whether they are
133 unsigned e = N->getNumValues();
134 for (++i; i != e; ++i)
135 ReplaceValueWith(SDOperand(N, i), SDOperand(N->getOperand(i)));
138 void DAGTypeLegalizer::ExpandResult_ANY_EXTEND(SDNode *N,
139 SDOperand &Lo, SDOperand &Hi) {
140 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
141 SDOperand Op = N->getOperand(0);
142 if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
143 // The low part is any extension of the input (which degenerates to a copy).
144 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
145 Hi = DAG.getNode(ISD::UNDEF, NVT); // The high part is undefined.
147 // For example, extension of an i48 to an i64. The operand type necessarily
148 // promotes to the result type, so will end up being expanded too.
149 assert(getTypeAction(Op.getValueType()) == Promote &&
150 "Don't know how to expand this result!");
151 SDOperand Res = GetPromotedOp(Op);
152 assert(Res.getValueType() == N->getValueType(0) &&
153 "Operand over promoted?");
154 // Split the promoted operand. This will simplify when it is expanded.
155 SplitOp(Res, Lo, Hi);
159 void DAGTypeLegalizer::ExpandResult_ZERO_EXTEND(SDNode *N,
160 SDOperand &Lo, SDOperand &Hi) {
161 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
162 SDOperand Op = N->getOperand(0);
163 if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
164 // The low part is zero extension of the input (which degenerates to a copy).
165 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
166 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
168 // For example, extension of an i48 to an i64. The operand type necessarily
169 // promotes to the result type, so will end up being expanded too.
170 assert(getTypeAction(Op.getValueType()) == Promote &&
171 "Don't know how to expand this result!");
172 SDOperand Res = GetPromotedOp(Op);
173 assert(Res.getValueType() == N->getValueType(0) &&
174 "Operand over promoted?");
175 // Split the promoted operand. This will simplify when it is expanded.
176 SplitOp(Res, Lo, Hi);
177 unsigned ExcessBits =
178 MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
179 Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerType(ExcessBits));
183 void DAGTypeLegalizer::ExpandResult_SIGN_EXTEND(SDNode *N,
184 SDOperand &Lo, SDOperand &Hi) {
185 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
186 SDOperand Op = N->getOperand(0);
187 if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
188 // The low part is sign extension of the input (which degenerates to a copy).
189 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
190 // The high part is obtained by SRA'ing all but one of the bits of low part.
191 unsigned LoSize = MVT::getSizeInBits(NVT);
192 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
193 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
195 // For example, extension of an i48 to an i64. The operand type necessarily
196 // promotes to the result type, so will end up being expanded too.
197 assert(getTypeAction(Op.getValueType()) == Promote &&
198 "Don't know how to expand this result!");
199 SDOperand Res = GetPromotedOp(Op);
200 assert(Res.getValueType() == N->getValueType(0) &&
201 "Operand over promoted?");
202 // Split the promoted operand. This will simplify when it is expanded.
203 SplitOp(Res, Lo, Hi);
204 unsigned ExcessBits =
205 MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
206 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
207 DAG.getValueType(MVT::getIntegerType(ExcessBits)));
211 void DAGTypeLegalizer::ExpandResult_AssertZext(SDNode *N,
212 SDOperand &Lo, SDOperand &Hi) {
213 GetExpandedOp(N->getOperand(0), Lo, Hi);
214 MVT::ValueType NVT = Lo.getValueType();
215 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
216 unsigned NVTBits = MVT::getSizeInBits(NVT);
217 unsigned EVTBits = MVT::getSizeInBits(EVT);
219 if (NVTBits < EVTBits) {
220 Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
221 DAG.getValueType(MVT::getIntegerType(EVTBits - NVTBits)));
223 Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
224 // The high part must be zero, make it explicit.
225 Hi = DAG.getConstant(0, NVT);
229 void DAGTypeLegalizer::ExpandResult_TRUNCATE(SDNode *N,
230 SDOperand &Lo, SDOperand &Hi) {
231 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
232 Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
233 Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
234 DAG.getConstant(MVT::getSizeInBits(NVT),
235 TLI.getShiftAmountTy()));
236 Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
239 void DAGTypeLegalizer::ExpandResult_BIT_CONVERT(SDNode *N,
240 SDOperand &Lo, SDOperand &Hi) {
241 // Lower the bit-convert to a store/load from the stack, then expand the load.
242 SDOperand Op = CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
243 ExpandResult_LOAD(cast<LoadSDNode>(Op.Val), Lo, Hi);
246 void DAGTypeLegalizer::
247 ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
248 GetExpandedOp(N->getOperand(0), Lo, Hi);
249 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
251 if (MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(Lo.getValueType())) {
252 // sext_inreg the low part if needed.
253 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
256 // The high part gets the sign extension from the lo-part. This handles
257 // things like sextinreg V:i64 from i8.
258 Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
259 DAG.getConstant(MVT::getSizeInBits(Hi.getValueType())-1,
260 TLI.getShiftAmountTy()));
262 // For example, extension of an i48 to an i64. Leave the low part alone,
263 // sext_inreg the high part.
264 unsigned ExcessBits =
265 MVT::getSizeInBits(EVT) - MVT::getSizeInBits(Lo.getValueType());
266 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
267 DAG.getValueType(MVT::getIntegerType(ExcessBits)));
271 void DAGTypeLegalizer::ExpandResult_LOAD(LoadSDNode *N,
272 SDOperand &Lo, SDOperand &Hi) {
273 MVT::ValueType VT = N->getValueType(0);
274 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
275 SDOperand Ch = N->getChain(); // Legalize the chain.
276 SDOperand Ptr = N->getBasePtr(); // Legalize the pointer.
277 ISD::LoadExtType ExtType = N->getExtensionType();
278 int SVOffset = N->getSrcValueOffset();
279 unsigned Alignment = N->getAlignment();
280 bool isVolatile = N->isVolatile();
282 assert(!(MVT::getSizeInBits(NVT) & 7) && "Expanded type not byte sized!");
284 if (ExtType == ISD::NON_EXTLOAD) {
285 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
286 isVolatile, Alignment);
287 // Increment the pointer to the other half.
288 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
289 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
290 DAG.getIntPtrConstant(IncrementSize));
291 Hi = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
292 isVolatile, MinAlign(Alignment, IncrementSize));
294 // Build a factor node to remember that this load is independent of the
296 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
299 // Handle endianness of the load.
300 if (TLI.isBigEndian())
302 } else if (MVT::getSizeInBits(N->getMemoryVT()) <= MVT::getSizeInBits(NVT)) {
303 MVT::ValueType EVT = N->getMemoryVT();
305 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
306 isVolatile, Alignment);
308 // Remember the chain.
311 if (ExtType == ISD::SEXTLOAD) {
312 // The high part is obtained by SRA'ing all but one of the bits of the
314 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
315 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
316 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
317 } else if (ExtType == ISD::ZEXTLOAD) {
318 // The high part is just a zero.
319 Hi = DAG.getConstant(0, NVT);
321 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
322 // The high part is undefined.
323 Hi = DAG.getNode(ISD::UNDEF, NVT);
325 } else if (TLI.isLittleEndian()) {
326 // Little-endian - low bits are at low addresses.
327 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
328 isVolatile, Alignment);
330 unsigned ExcessBits =
331 MVT::getSizeInBits(N->getMemoryVT()) - MVT::getSizeInBits(NVT);
332 MVT::ValueType NEVT = MVT::getIntegerType(ExcessBits);
334 // Increment the pointer to the other half.
335 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
336 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
337 DAG.getIntPtrConstant(IncrementSize));
338 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
339 SVOffset+IncrementSize, NEVT,
340 isVolatile, MinAlign(Alignment, IncrementSize));
342 // Build a factor node to remember that this load is independent of the
344 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
347 // Big-endian - high bits are at low addresses. Favor aligned loads at
348 // the cost of some bit-fiddling.
349 MVT::ValueType EVT = N->getMemoryVT();
350 unsigned EBytes = MVT::getStoreSizeInBits(EVT)/8;
351 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
352 unsigned ExcessBits = (EBytes - IncrementSize)*8;
354 // Load both the high bits and maybe some of the low bits.
355 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
356 MVT::getIntegerType(MVT::getSizeInBits(EVT)-ExcessBits),
357 isVolatile, Alignment);
359 // Increment the pointer to the other half.
360 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
361 DAG.getIntPtrConstant(IncrementSize));
362 // Load the rest of the low bits.
363 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
364 SVOffset+IncrementSize, MVT::getIntegerType(ExcessBits),
365 isVolatile, MinAlign(Alignment, IncrementSize));
367 // Build a factor node to remember that this load is independent of the
369 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
372 if (ExcessBits < MVT::getSizeInBits(NVT)) {
373 // Transfer low bits from the bottom of Hi to the top of Lo.
374 Lo = DAG.getNode(ISD::OR, NVT, Lo,
375 DAG.getNode(ISD::SHL, NVT, Hi,
376 DAG.getConstant(ExcessBits,
377 TLI.getShiftAmountTy())));
378 // Move high bits to the right position in Hi.
379 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
380 DAG.getConstant(MVT::getSizeInBits(NVT) - ExcessBits,
381 TLI.getShiftAmountTy()));
385 // Legalized the chain result - switch anything that used the old chain to
387 ReplaceValueWith(SDOperand(N, 1), Ch);
390 void DAGTypeLegalizer::ExpandResult_Logical(SDNode *N,
391 SDOperand &Lo, SDOperand &Hi) {
392 SDOperand LL, LH, RL, RH;
393 GetExpandedOp(N->getOperand(0), LL, LH);
394 GetExpandedOp(N->getOperand(1), RL, RH);
395 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
396 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
399 void DAGTypeLegalizer::ExpandResult_BSWAP(SDNode *N,
400 SDOperand &Lo, SDOperand &Hi) {
401 GetExpandedOp(N->getOperand(0), Hi, Lo); // Note swapped operands.
402 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
403 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
406 void DAGTypeLegalizer::ExpandResult_SELECT(SDNode *N,
407 SDOperand &Lo, SDOperand &Hi) {
408 SDOperand LL, LH, RL, RH;
409 GetExpandedOp(N->getOperand(1), LL, LH);
410 GetExpandedOp(N->getOperand(2), RL, RH);
411 Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LL, RL);
413 assert(N->getOperand(0).getValueType() != MVT::f32 &&
414 "FIXME: softfp shouldn't use expand!");
415 Hi = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LH, RH);
418 void DAGTypeLegalizer::ExpandResult_SELECT_CC(SDNode *N,
419 SDOperand &Lo, SDOperand &Hi) {
420 SDOperand LL, LH, RL, RH;
421 GetExpandedOp(N->getOperand(2), LL, LH);
422 GetExpandedOp(N->getOperand(3), RL, RH);
423 Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
424 N->getOperand(1), LL, RL, N->getOperand(4));
426 assert(N->getOperand(0).getValueType() != MVT::f32 &&
427 "FIXME: softfp shouldn't use expand!");
428 Hi = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
429 N->getOperand(1), LH, RH, N->getOperand(4));
432 void DAGTypeLegalizer::ExpandResult_ADDSUB(SDNode *N,
433 SDOperand &Lo, SDOperand &Hi) {
434 // Expand the subcomponents.
435 SDOperand LHSL, LHSH, RHSL, RHSH;
436 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
437 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
438 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
439 SDOperand LoOps[2] = { LHSL, RHSL };
440 SDOperand HiOps[3] = { LHSH, RHSH };
442 if (N->getOpcode() == ISD::ADD) {
443 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
444 HiOps[2] = Lo.getValue(1);
445 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
447 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
448 HiOps[2] = Lo.getValue(1);
449 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
453 void DAGTypeLegalizer::ExpandResult_ADDSUBC(SDNode *N,
454 SDOperand &Lo, SDOperand &Hi) {
455 // Expand the subcomponents.
456 SDOperand LHSL, LHSH, RHSL, RHSH;
457 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
458 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
459 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
460 SDOperand LoOps[2] = { LHSL, RHSL };
461 SDOperand HiOps[3] = { LHSH, RHSH };
463 if (N->getOpcode() == ISD::ADDC) {
464 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
465 HiOps[2] = Lo.getValue(1);
466 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
468 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
469 HiOps[2] = Lo.getValue(1);
470 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
473 // Legalized the flag result - switch anything that used the old flag to
475 ReplaceValueWith(SDOperand(N, 1), Hi.getValue(1));
478 void DAGTypeLegalizer::ExpandResult_ADDSUBE(SDNode *N,
479 SDOperand &Lo, SDOperand &Hi) {
480 // Expand the subcomponents.
481 SDOperand LHSL, LHSH, RHSL, RHSH;
482 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
483 GetExpandedOp(N->getOperand(1), RHSL, RHSH);
484 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
485 SDOperand LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
486 SDOperand HiOps[3] = { LHSH, RHSH };
488 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
489 HiOps[2] = Lo.getValue(1);
490 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
492 // Legalized the flag result - switch anything that used the old flag to
494 ReplaceValueWith(SDOperand(N, 1), Hi.getValue(1));
497 void DAGTypeLegalizer::ExpandResult_MUL(SDNode *N,
498 SDOperand &Lo, SDOperand &Hi) {
499 MVT::ValueType VT = N->getValueType(0);
500 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
502 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
503 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
504 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
505 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
506 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
507 SDOperand LL, LH, RL, RH;
508 GetExpandedOp(N->getOperand(0), LL, LH);
509 GetExpandedOp(N->getOperand(1), RL, RH);
510 unsigned BitSize = MVT::getSizeInBits(NVT);
511 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
512 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
514 // FIXME: generalize this to handle other bit sizes
515 if (LHSSB == 32 && RHSSB == 32 &&
516 DAG.MaskedValueIsZero(N->getOperand(0), 0xFFFFFFFF00000000ULL) &&
517 DAG.MaskedValueIsZero(N->getOperand(1), 0xFFFFFFFF00000000ULL)) {
518 // The inputs are both zero-extended.
520 // We can emit a umul_lohi.
521 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
522 Hi = SDOperand(Lo.Val, 1);
526 // We can emit a mulhu+mul.
527 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
528 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
532 if (LHSSB > BitSize && RHSSB > BitSize) {
533 // The input values are both sign-extended.
535 // We can emit a smul_lohi.
536 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
537 Hi = SDOperand(Lo.Val, 1);
541 // We can emit a mulhs+mul.
542 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
543 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
548 // Lo,Hi = umul LHS, RHS.
549 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
550 DAG.getVTList(NVT, NVT), LL, RL);
552 Hi = UMulLOHI.getValue(1);
553 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
554 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
555 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
556 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
563 // If nothing else, we can make a libcall.
564 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), N,
565 false/*sign irrelevant*/, Hi);
570 void DAGTypeLegalizer::ExpandResult_Shift(SDNode *N,
571 SDOperand &Lo, SDOperand &Hi) {
572 MVT::ValueType VT = N->getValueType(0);
574 // If we can emit an efficient shift operation, do so now. Check to see if
575 // the RHS is a constant.
576 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
577 return ExpandShiftByConstant(N, CN->getValue(), Lo, Hi);
579 // If we can determine that the high bit of the shift is zero or one, even if
580 // the low bits are variable, emit this shift in an optimized form.
581 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
584 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
586 if (N->getOpcode() == ISD::SHL)
587 PartsOpc = ISD::SHL_PARTS;
588 else if (N->getOpcode() == ISD::SRL)
589 PartsOpc = ISD::SRL_PARTS;
591 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
592 PartsOpc = ISD::SRA_PARTS;
595 // Next check to see if the target supports this SHL_PARTS operation or if it
596 // will custom expand it.
597 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
598 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
599 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
600 Action == TargetLowering::Custom) {
601 // Expand the subcomponents.
602 SDOperand LHSL, LHSH;
603 GetExpandedOp(N->getOperand(0), LHSL, LHSH);
605 SDOperand Ops[] = { LHSL, LHSH, N->getOperand(1) };
606 MVT::ValueType VT = LHSL.getValueType();
607 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
614 // Otherwise, emit a libcall.
615 unsigned RuntimeCode = ; // SRL -> SRL_I64 etc.
617 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), N,
618 false/*lshr is unsigned*/, Hi);
622 void DAGTypeLegalizer::ExpandResult_CTLZ(SDNode *N,
623 SDOperand &Lo, SDOperand &Hi) {
624 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
625 GetExpandedOp(N->getOperand(0), Lo, Hi);
626 MVT::ValueType NVT = Lo.getValueType();
628 SDOperand HiNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
629 DAG.getConstant(0, NVT), ISD::SETNE);
631 SDOperand LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
632 SDOperand HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
634 Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
635 DAG.getNode(ISD::ADD, NVT, LoLZ,
636 DAG.getConstant(MVT::getSizeInBits(NVT), NVT)));
637 Hi = DAG.getConstant(0, NVT);
640 void DAGTypeLegalizer::ExpandResult_CTPOP(SDNode *N,
641 SDOperand &Lo, SDOperand &Hi) {
642 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
643 GetExpandedOp(N->getOperand(0), Lo, Hi);
644 MVT::ValueType NVT = Lo.getValueType();
645 Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
646 DAG.getNode(ISD::CTPOP, NVT, Hi));
647 Hi = DAG.getConstant(0, NVT);
650 void DAGTypeLegalizer::ExpandResult_CTTZ(SDNode *N,
651 SDOperand &Lo, SDOperand &Hi) {
652 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
653 GetExpandedOp(N->getOperand(0), Lo, Hi);
654 MVT::ValueType NVT = Lo.getValueType();
656 SDOperand LoNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), Lo,
657 DAG.getConstant(0, NVT), ISD::SETNE);
659 SDOperand LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
660 SDOperand HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
662 Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
663 DAG.getNode(ISD::ADD, NVT, HiLZ,
664 DAG.getConstant(MVT::getSizeInBits(NVT), NVT)));
665 Hi = DAG.getConstant(0, NVT);
668 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
669 /// and the shift amount is a constant 'Amt'. Expand the operation.
670 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
671 SDOperand &Lo, SDOperand &Hi) {
672 // Expand the incoming operand to be shifted, so that we have its parts
674 GetExpandedOp(N->getOperand(0), InL, InH);
676 MVT::ValueType NVT = InL.getValueType();
677 unsigned VTBits = MVT::getSizeInBits(N->getValueType(0));
678 unsigned NVTBits = MVT::getSizeInBits(NVT);
679 MVT::ValueType ShTy = N->getOperand(1).getValueType();
681 if (N->getOpcode() == ISD::SHL) {
683 Lo = Hi = DAG.getConstant(0, NVT);
684 } else if (Amt > NVTBits) {
685 Lo = DAG.getConstant(0, NVT);
686 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
687 } else if (Amt == NVTBits) {
688 Lo = DAG.getConstant(0, NVT);
691 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
692 Hi = DAG.getNode(ISD::OR, NVT,
693 DAG.getNode(ISD::SHL, NVT, InH,
694 DAG.getConstant(Amt, ShTy)),
695 DAG.getNode(ISD::SRL, NVT, InL,
696 DAG.getConstant(NVTBits-Amt, ShTy)));
701 if (N->getOpcode() == ISD::SRL) {
703 Lo = DAG.getConstant(0, NVT);
704 Hi = DAG.getConstant(0, NVT);
705 } else if (Amt > NVTBits) {
706 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
707 Hi = DAG.getConstant(0, NVT);
708 } else if (Amt == NVTBits) {
710 Hi = DAG.getConstant(0, NVT);
712 Lo = DAG.getNode(ISD::OR, NVT,
713 DAG.getNode(ISD::SRL, NVT, InL,
714 DAG.getConstant(Amt, ShTy)),
715 DAG.getNode(ISD::SHL, NVT, InH,
716 DAG.getConstant(NVTBits-Amt, ShTy)));
717 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
722 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
724 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
725 DAG.getConstant(NVTBits-1, ShTy));
726 } else if (Amt > NVTBits) {
727 Lo = DAG.getNode(ISD::SRA, NVT, InH,
728 DAG.getConstant(Amt-NVTBits, ShTy));
729 Hi = DAG.getNode(ISD::SRA, NVT, InH,
730 DAG.getConstant(NVTBits-1, ShTy));
731 } else if (Amt == NVTBits) {
733 Hi = DAG.getNode(ISD::SRA, NVT, InH,
734 DAG.getConstant(NVTBits-1, ShTy));
736 Lo = DAG.getNode(ISD::OR, NVT,
737 DAG.getNode(ISD::SRL, NVT, InL,
738 DAG.getConstant(Amt, ShTy)),
739 DAG.getNode(ISD::SHL, NVT, InH,
740 DAG.getConstant(NVTBits-Amt, ShTy)));
741 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
745 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
746 /// this shift based on knowledge of the high bit of the shift amount. If we
747 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
749 bool DAGTypeLegalizer::
750 ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
751 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
752 unsigned NVTBits = MVT::getSizeInBits(NVT);
753 assert(!(NVTBits & (NVTBits - 1)) &&
754 "Expanded integer type size not a power of two!");
756 uint64_t HighBitMask = NVTBits, KnownZero, KnownOne;
757 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
759 // If we don't know anything about the high bit, exit.
760 if (((KnownZero|KnownOne) & HighBitMask) == 0)
763 // Get the incoming operand to be shifted.
765 GetExpandedOp(N->getOperand(0), InL, InH);
766 SDOperand Amt = N->getOperand(1);
768 // If we know that the high bit of the shift amount is one, then we can do
769 // this as a couple of simple shifts.
770 if (KnownOne & HighBitMask) {
771 // Mask out the high bit, which we know is set.
772 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
773 DAG.getConstant(NVTBits-1, Amt.getValueType()));
775 switch (N->getOpcode()) {
776 default: assert(0 && "Unknown shift");
778 Lo = DAG.getConstant(0, NVT); // Low part is zero.
779 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
782 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
783 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
786 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
787 DAG.getConstant(NVTBits-1, Amt.getValueType()));
788 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
793 // If we know that the high bit of the shift amount is zero, then we can do
794 // this as a couple of simple shifts.
795 assert((KnownZero & HighBitMask) && "Bad mask computation above");
798 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
799 DAG.getConstant(NVTBits, Amt.getValueType()),
802 switch (N->getOpcode()) {
803 default: assert(0 && "Unknown shift");
804 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
806 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
809 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
810 Hi = DAG.getNode(ISD::OR, NVT,
811 DAG.getNode(Op1, NVT, InH, Amt),
812 DAG.getNode(Op2, NVT, InL, Amt2));
817 //===----------------------------------------------------------------------===//
819 //===----------------------------------------------------------------------===//
821 /// ExpandOperand - This method is called when the specified operand of the
822 /// specified node is found to need expansion. At this point, all of the result
823 /// types of the node are known to be legal, but other operands of the node may
824 /// need promotion or expansion as well as the specified one.
825 bool DAGTypeLegalizer::ExpandOperand(SDNode *N, unsigned OpNo) {
826 DEBUG(cerr << "Expand node operand: "; N->dump(&DAG); cerr << "\n");
829 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
830 == TargetLowering::Custom)
831 Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
834 switch (N->getOpcode()) {
837 cerr << "ExpandOperand Op #" << OpNo << ": ";
838 N->dump(&DAG); cerr << "\n";
840 assert(0 && "Do not know how to expand this operator's operand!");
843 case ISD::TRUNCATE: Res = ExpandOperand_TRUNCATE(N); break;
844 case ISD::BIT_CONVERT: Res = ExpandOperand_BIT_CONVERT(N); break;
846 case ISD::SINT_TO_FP:
847 Res = ExpandOperand_SINT_TO_FP(N->getOperand(0), N->getValueType(0));
849 case ISD::UINT_TO_FP:
850 Res = ExpandOperand_UINT_TO_FP(N->getOperand(0), N->getValueType(0));
852 case ISD::EXTRACT_ELEMENT: Res = ExpandOperand_EXTRACT_ELEMENT(N); break;
854 case ISD::BR_CC: Res = ExpandOperand_BR_CC(N); break;
855 case ISD::SETCC: Res = ExpandOperand_SETCC(N); break;
858 Res = ExpandOperand_STORE(cast<StoreSDNode>(N), OpNo);
862 case ISD::MEMMOVE: Res = HandleMemIntrinsic(N); break;
866 // If the result is null, the sub-method took care of registering results etc.
867 if (!Res.Val) return false;
868 // If the result is N, the sub-method updated N in place. Check to see if any
869 // operands are new, and if so, mark them.
871 // Mark N as new and remark N and its operands. This allows us to correctly
872 // revisit N if it needs another step of promotion and allows us to visit
873 // any new operands to N.
874 N->setNodeId(NewNode);
879 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
880 "Invalid operand expansion");
882 ReplaceValueWith(SDOperand(N, 0), Res);
886 SDOperand DAGTypeLegalizer::ExpandOperand_TRUNCATE(SDNode *N) {
888 GetExpandedOp(N->getOperand(0), InL, InH);
889 // Just truncate the low part of the source.
890 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
893 SDOperand DAGTypeLegalizer::ExpandOperand_BIT_CONVERT(SDNode *N) {
894 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
897 SDOperand DAGTypeLegalizer::ExpandOperand_SINT_TO_FP(SDOperand Source,
898 MVT::ValueType DestTy) {
899 // We know the destination is legal, but that the input needs to be expanded.
900 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
902 // Check to see if the target has a custom way to lower this. If so, use it.
903 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
904 default: assert(0 && "This action not implemented for this operation!");
905 case TargetLowering::Legal:
906 case TargetLowering::Expand:
907 break; // This case is handled below.
908 case TargetLowering::Custom:
909 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
911 if (NV.Val) return NV;
912 break; // The target lowered this.
916 if (DestTy == MVT::f32)
917 LC = RTLIB::SINTTOFP_I64_F32;
919 assert(DestTy == MVT::f64 && "Unknown fp value type!");
920 LC = RTLIB::SINTTOFP_I64_F64;
923 assert(0 && "FIXME: no libcalls yet!");
926 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
927 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
928 SDOperand UnusedHiPart;
929 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, true, UnusedHiPart);
933 SDOperand DAGTypeLegalizer::ExpandOperand_UINT_TO_FP(SDOperand Source,
934 MVT::ValueType DestTy) {
935 // We know the destination is legal, but that the input needs to be expanded.
936 assert(getTypeAction(Source.getValueType()) == Expand &&
937 "This is not an expansion!");
938 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
940 // If this is unsigned, and not supported, first perform the conversion to
941 // signed, then adjust the result if the sign bit is set.
942 SDOperand SignedConv = ExpandOperand_SINT_TO_FP(Source, DestTy);
944 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
945 // incoming integer is set. To handle this, we dynamically test to see if
946 // it is set, and, if so, add a fudge factor.
948 GetExpandedOp(Source, Lo, Hi);
950 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
951 DAG.getConstant(0, Hi.getValueType()),
953 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
954 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
955 SignSet, Four, Zero);
956 uint64_t FF = 0x5f800000ULL;
957 if (TLI.isLittleEndian()) FF <<= 32;
958 Constant *FudgeFactor = ConstantInt::get((Type*)Type::Int64Ty, FF);
960 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
961 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
962 SDOperand FudgeInReg;
963 if (DestTy == MVT::f32)
964 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
965 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
966 // FIXME: Avoid the extend by construction the right constantpool?
967 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
968 CPIdx, NULL, 0, MVT::f32);
970 assert(0 && "Unexpected conversion");
972 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
975 SDOperand DAGTypeLegalizer::ExpandOperand_EXTRACT_ELEMENT(SDNode *N) {
977 GetExpandedOp(N->getOperand(0), Lo, Hi);
978 return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
981 SDOperand DAGTypeLegalizer::ExpandOperand_BR_CC(SDNode *N) {
982 SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
983 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
984 ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
986 // If ExpandSetCCOperands returned a scalar, we need to compare the result
987 // against zero to select between true and false values.
988 if (NewRHS.Val == 0) {
989 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
993 // Update N to have the operands specified.
994 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
995 DAG.getCondCode(CCCode), NewLHS, NewRHS,
999 SDOperand DAGTypeLegalizer::ExpandOperand_SETCC(SDNode *N) {
1000 SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1001 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1002 ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1004 // If ExpandSetCCOperands returned a scalar, use it.
1005 if (NewRHS.Val == 0) return NewLHS;
1007 // Otherwise, update N to have the operands specified.
1008 return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1009 DAG.getCondCode(CCCode));
1012 /// ExpandSetCCOperands - Expand the operands of a comparison. This code is
1013 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1014 void DAGTypeLegalizer::ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
1015 ISD::CondCode &CCCode) {
1016 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1017 GetExpandedOp(NewLHS, LHSLo, LHSHi);
1018 GetExpandedOp(NewRHS, RHSLo, RHSHi);
1020 MVT::ValueType VT = NewLHS.getValueType();
1021 if (VT == MVT::f32 || VT == MVT::f64) {
1022 assert(0 && "FIXME: softfp not implemented yet! should be promote not exp");
1025 if (VT == MVT::ppcf128) {
1026 // FIXME: This generated code sucks. We want to generate
1027 // FCMP crN, hi1, hi2
1029 // FCMP crN, lo1, lo2
1030 // The following can be improved, but not that much.
1031 SDOperand Tmp1, Tmp2, Tmp3;
1032 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1033 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
1034 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1035 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
1036 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
1037 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1038 NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1039 NewRHS = SDOperand(); // LHS is the result, not a compare.
1044 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1046 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1047 if (RHSCST->isAllOnesValue()) {
1048 // Equality comparison to -1.
1049 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1054 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1055 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1056 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1057 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1061 // If this is a comparison of the sign bit, just look at the top part.
1063 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1064 if ((CCCode == ISD::SETLT && CST->getValue() == 0) || // X < 0
1065 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
1071 // FIXME: This generated code sucks.
1072 ISD::CondCode LowCC;
1074 default: assert(0 && "Unknown integer setcc!");
1076 case ISD::SETULT: LowCC = ISD::SETULT; break;
1078 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1080 case ISD::SETULE: LowCC = ISD::SETULE; break;
1082 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1085 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1086 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1087 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1089 // NOTE: on targets without efficient SELECT of bools, we can always use
1090 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1091 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1092 SDOperand Tmp1, Tmp2;
1093 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
1094 false, DagCombineInfo);
1096 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
1097 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1098 CCCode, false, DagCombineInfo);
1100 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,
1101 DAG.getCondCode(CCCode));
1103 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
1104 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
1105 if ((Tmp1C && Tmp1C->getValue() == 0) ||
1106 (Tmp2C && Tmp2C->getValue() == 0 &&
1107 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1108 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1109 (Tmp2C && Tmp2C->getValue() == 1 &&
1110 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1111 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1112 // low part is known false, returns high part.
1113 // For LE / GE, if high part is known false, ignore the low part.
1114 // For LT / GT, if high part is known true, ignore the low part.
1116 NewRHS = SDOperand();
1120 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1121 ISD::SETEQ, false, DagCombineInfo);
1123 NewLHS = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1124 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1125 NewLHS, Tmp1, Tmp2);
1126 NewRHS = SDOperand();
1129 SDOperand DAGTypeLegalizer::ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo) {
1130 assert(OpNo == 1 && "Can only expand the stored value so far");
1132 MVT::ValueType VT = N->getOperand(1).getValueType();
1133 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1134 SDOperand Ch = N->getChain();
1135 SDOperand Ptr = N->getBasePtr();
1136 int SVOffset = N->getSrcValueOffset();
1137 unsigned Alignment = N->getAlignment();
1138 bool isVolatile = N->isVolatile();
1141 assert(!(MVT::getSizeInBits(NVT) & 7) && "Expanded type not byte sized!");
1143 if (!N->isTruncatingStore()) {
1144 unsigned IncrementSize = 0;
1145 GetExpandedOp(N->getValue(), Lo, Hi);
1146 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1148 if (TLI.isBigEndian())
1151 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(),
1152 SVOffset, isVolatile, Alignment);
1154 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1155 DAG.getIntPtrConstant(IncrementSize));
1156 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
1157 Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
1158 isVolatile, MinAlign(Alignment, IncrementSize));
1159 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1160 } else if (MVT::getSizeInBits(N->getMemoryVT()) <= MVT::getSizeInBits(NVT)) {
1161 GetExpandedOp(N->getValue(), Lo, Hi);
1162 return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
1163 N->getMemoryVT(), isVolatile, Alignment);
1164 } else if (TLI.isLittleEndian()) {
1165 // Little-endian - low bits are at low addresses.
1166 GetExpandedOp(N->getValue(), Lo, Hi);
1168 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
1169 isVolatile, Alignment);
1171 unsigned ExcessBits =
1172 MVT::getSizeInBits(N->getMemoryVT()) - MVT::getSizeInBits(NVT);
1173 MVT::ValueType NEVT = MVT::getIntegerType(ExcessBits);
1175 // Increment the pointer to the other half.
1176 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1177 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1178 DAG.getIntPtrConstant(IncrementSize));
1179 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
1180 SVOffset+IncrementSize, NEVT,
1181 isVolatile, MinAlign(Alignment, IncrementSize));
1182 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1184 // Big-endian - high bits are at low addresses. Favor aligned stores at
1185 // the cost of some bit-fiddling.
1186 GetExpandedOp(N->getValue(), Lo, Hi);
1188 MVT::ValueType EVT = N->getMemoryVT();
1189 unsigned EBytes = MVT::getStoreSizeInBits(EVT)/8;
1190 unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1191 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1192 MVT::ValueType HiVT =
1193 MVT::getIntegerType(MVT::getSizeInBits(EVT)-ExcessBits);
1195 if (ExcessBits < MVT::getSizeInBits(NVT)) {
1196 // Transfer high bits from the top of Lo to the bottom of Hi.
1197 Hi = DAG.getNode(ISD::SHL, NVT, Hi,
1198 DAG.getConstant(MVT::getSizeInBits(NVT) - ExcessBits,
1199 TLI.getShiftAmountTy()));
1200 Hi = DAG.getNode(ISD::OR, NVT, Hi,
1201 DAG.getNode(ISD::SRL, NVT, Lo,
1202 DAG.getConstant(ExcessBits,
1203 TLI.getShiftAmountTy())));
1206 // Store both the high bits and maybe some of the low bits.
1207 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
1208 SVOffset, HiVT, isVolatile, Alignment);
1210 // Increment the pointer to the other half.
1211 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1212 DAG.getIntPtrConstant(IncrementSize));
1213 // Store the lowest ExcessBits bits in the second half.
1214 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
1215 SVOffset+IncrementSize,
1216 MVT::getIntegerType(ExcessBits),
1217 isVolatile, MinAlign(Alignment, IncrementSize));
1218 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);