1 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
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 the SelectionDAG::Legalize method.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineJumpTableInfo.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/Analysis/DebugInfo.h"
20 #include "llvm/CodeGen/PseudoSourceValue.h"
21 #include "llvm/Target/TargetFrameInfo.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/CallingConv.h"
27 #include "llvm/Constants.h"
28 #include "llvm/DerivedTypes.h"
29 #include "llvm/Function.h"
30 #include "llvm/GlobalVariable.h"
31 #include "llvm/LLVMContext.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/ADT/DenseMap.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/SmallPtrSet.h"
41 //===----------------------------------------------------------------------===//
42 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
43 /// hacks on it until the target machine can handle it. This involves
44 /// eliminating value sizes the machine cannot handle (promoting small sizes to
45 /// large sizes or splitting up large values into small values) as well as
46 /// eliminating operations the machine cannot handle.
48 /// This code also does a small amount of optimization and recognition of idioms
49 /// as part of its processing. For example, if a target does not support a
50 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
51 /// will attempt merge setcc and brc instructions into brcc's.
54 class SelectionDAGLegalize {
55 const TargetMachine &TM;
56 const TargetLowering &TLI;
58 CodeGenOpt::Level OptLevel;
60 // Libcall insertion helpers.
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDValue LastCALLSEQ_END;
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
73 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
75 Expand // Try to expand this to other ops, otherwise use a libcall.
78 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
81 TargetLowering::ValueTypeActionImpl ValueTypeActions;
83 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
86 DenseMap<SDValue, SDValue> LegalizedNodes;
88 void AddLegalizedOperand(SDValue From, SDValue To) {
89 LegalizedNodes.insert(std::make_pair(From, To));
90 // If someone requests legalization of the new node, return itself.
92 LegalizedNodes.insert(std::make_pair(To, To));
96 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
98 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
101 LegalizeAction getTypeAction(EVT VT) const {
103 (LegalizeAction)ValueTypeActions.getTypeAction(*DAG.getContext(), VT);
106 /// isTypeLegal - Return true if this type is legal on this target.
108 bool isTypeLegal(EVT VT) const {
109 return getTypeAction(VT) == Legal;
115 /// LegalizeOp - We know that the specified value has a legal type.
116 /// Recursively ensure that the operands have legal types, then return the
118 SDValue LegalizeOp(SDValue O);
120 SDValue OptimizeFloatStore(StoreSDNode *ST);
122 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
123 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
124 /// is necessary to spill the vector being inserted into to memory, perform
125 /// the insert there, and then read the result back.
126 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
127 SDValue Idx, DebugLoc dl);
128 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
129 SDValue Idx, DebugLoc dl);
131 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
132 /// performs the same shuffe in terms of order or result bytes, but on a type
133 /// whose vector element type is narrower than the original shuffle type.
134 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
135 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
136 SDValue N1, SDValue N2,
137 SmallVectorImpl<int> &Mask) const;
139 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
140 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
142 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
145 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
146 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
147 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
148 RTLIB::Libcall Call_PPCF128);
149 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
150 RTLIB::Libcall Call_I8,
151 RTLIB::Libcall Call_I16,
152 RTLIB::Libcall Call_I32,
153 RTLIB::Libcall Call_I64,
154 RTLIB::Libcall Call_I128);
156 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
157 SDValue ExpandBUILD_VECTOR(SDNode *Node);
158 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
159 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
160 SmallVectorImpl<SDValue> &Results);
161 SDValue ExpandFCOPYSIGN(SDNode *Node);
162 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
164 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
166 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
169 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
170 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
172 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
173 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
175 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
176 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
180 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
181 /// performs the same shuffe in terms of order or result bytes, but on a type
182 /// whose vector element type is narrower than the original shuffle type.
183 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
185 SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
186 SDValue N1, SDValue N2,
187 SmallVectorImpl<int> &Mask) const {
188 unsigned NumMaskElts = VT.getVectorNumElements();
189 unsigned NumDestElts = NVT.getVectorNumElements();
190 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
192 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
194 if (NumEltsGrowth == 1)
195 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
197 SmallVector<int, 8> NewMask;
198 for (unsigned i = 0; i != NumMaskElts; ++i) {
200 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
202 NewMask.push_back(-1);
204 NewMask.push_back(Idx * NumEltsGrowth + j);
207 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
208 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
209 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
212 SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
213 CodeGenOpt::Level ol)
214 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
215 DAG(dag), OptLevel(ol),
216 ValueTypeActions(TLI.getValueTypeActions()) {
217 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
218 "Too many value types for ValueTypeActions to hold!");
221 void SelectionDAGLegalize::LegalizeDAG() {
222 LastCALLSEQ_END = DAG.getEntryNode();
223 IsLegalizingCall = false;
225 // The legalize process is inherently a bottom-up recursive process (users
226 // legalize their uses before themselves). Given infinite stack space, we
227 // could just start legalizing on the root and traverse the whole graph. In
228 // practice however, this causes us to run out of stack space on large basic
229 // blocks. To avoid this problem, compute an ordering of the nodes where each
230 // node is only legalized after all of its operands are legalized.
231 DAG.AssignTopologicalOrder();
232 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
233 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
234 LegalizeOp(SDValue(I, 0));
236 // Finally, it's possible the root changed. Get the new root.
237 SDValue OldRoot = DAG.getRoot();
238 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
239 DAG.setRoot(LegalizedNodes[OldRoot]);
241 LegalizedNodes.clear();
243 // Remove dead nodes now.
244 DAG.RemoveDeadNodes();
248 /// FindCallEndFromCallStart - Given a chained node that is part of a call
249 /// sequence, find the CALLSEQ_END node that terminates the call sequence.
250 static SDNode *FindCallEndFromCallStart(SDNode *Node) {
251 if (Node->getOpcode() == ISD::CALLSEQ_END)
253 if (Node->use_empty())
254 return 0; // No CallSeqEnd
256 // The chain is usually at the end.
257 SDValue TheChain(Node, Node->getNumValues()-1);
258 if (TheChain.getValueType() != MVT::Other) {
259 // Sometimes it's at the beginning.
260 TheChain = SDValue(Node, 0);
261 if (TheChain.getValueType() != MVT::Other) {
262 // Otherwise, hunt for it.
263 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
264 if (Node->getValueType(i) == MVT::Other) {
265 TheChain = SDValue(Node, i);
269 // Otherwise, we walked into a node without a chain.
270 if (TheChain.getValueType() != MVT::Other)
275 for (SDNode::use_iterator UI = Node->use_begin(),
276 E = Node->use_end(); UI != E; ++UI) {
278 // Make sure to only follow users of our token chain.
280 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
281 if (User->getOperand(i) == TheChain)
282 if (SDNode *Result = FindCallEndFromCallStart(User))
288 /// FindCallStartFromCallEnd - Given a chained node that is part of a call
289 /// sequence, find the CALLSEQ_START node that initiates the call sequence.
290 static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
291 assert(Node && "Didn't find callseq_start for a call??");
292 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
294 assert(Node->getOperand(0).getValueType() == MVT::Other &&
295 "Node doesn't have a token chain argument!");
296 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
299 /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
300 /// see if any uses can reach Dest. If no dest operands can get to dest,
301 /// legalize them, legalize ourself, and return false, otherwise, return true.
303 /// Keep track of the nodes we fine that actually do lead to Dest in
304 /// NodesLeadingTo. This avoids retraversing them exponential number of times.
306 bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
307 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
308 if (N == Dest) return true; // N certainly leads to Dest :)
310 // If we've already processed this node and it does lead to Dest, there is no
311 // need to reprocess it.
312 if (NodesLeadingTo.count(N)) return true;
314 // If the first result of this node has been already legalized, then it cannot
316 if (LegalizedNodes.count(SDValue(N, 0))) return false;
318 // Okay, this node has not already been legalized. Check and legalize all
319 // operands. If none lead to Dest, then we can legalize this node.
320 bool OperandsLeadToDest = false;
321 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
322 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
323 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
325 if (OperandsLeadToDest) {
326 NodesLeadingTo.insert(N);
330 // Okay, this node looks safe, legalize it and return false.
331 LegalizeOp(SDValue(N, 0));
335 /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
336 /// a load from the constant pool.
337 static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
338 SelectionDAG &DAG, const TargetLowering &TLI) {
340 DebugLoc dl = CFP->getDebugLoc();
342 // If a FP immediate is precise when represented as a float and if the
343 // target can do an extending load from float to double, we put it into
344 // the constant pool as a float, even if it's is statically typed as a
345 // double. This shrinks FP constants and canonicalizes them for targets where
346 // an FP extending load is the same cost as a normal load (such as on the x87
347 // fp stack or PPC FP unit).
348 EVT VT = CFP->getValueType(0);
349 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
351 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
352 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
353 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
358 while (SVT != MVT::f32) {
359 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
360 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
361 // Only do this if the target has a native EXTLOAD instruction from
363 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
364 TLI.ShouldShrinkFPConstant(OrigVT)) {
365 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
366 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
372 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
373 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
375 return DAG.getExtLoad(ISD::EXTLOAD, dl,
376 OrigVT, DAG.getEntryNode(),
377 CPIdx, PseudoSourceValue::getConstantPool(),
378 0, VT, false, false, Alignment);
379 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
380 PseudoSourceValue::getConstantPool(), 0, false, false,
384 /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
386 SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
387 const TargetLowering &TLI) {
388 SDValue Chain = ST->getChain();
389 SDValue Ptr = ST->getBasePtr();
390 SDValue Val = ST->getValue();
391 EVT VT = Val.getValueType();
392 int Alignment = ST->getAlignment();
393 int SVOffset = ST->getSrcValueOffset();
394 DebugLoc dl = ST->getDebugLoc();
395 if (ST->getMemoryVT().isFloatingPoint() ||
396 ST->getMemoryVT().isVector()) {
397 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
398 if (TLI.isTypeLegal(intVT)) {
399 // Expand to a bitconvert of the value to the integer type of the
400 // same size, then a (misaligned) int store.
401 // FIXME: Does not handle truncating floating point stores!
402 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
403 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
404 SVOffset, ST->isVolatile(), ST->isNonTemporal(),
407 // Do a (aligned) store to a stack slot, then copy from the stack slot
408 // to the final destination using (unaligned) integer loads and stores.
409 EVT StoredVT = ST->getMemoryVT();
411 TLI.getRegisterType(*DAG.getContext(),
412 EVT::getIntegerVT(*DAG.getContext(),
413 StoredVT.getSizeInBits()));
414 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
415 unsigned RegBytes = RegVT.getSizeInBits() / 8;
416 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
418 // Make sure the stack slot is also aligned for the register type.
419 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
421 // Perform the original store, only redirected to the stack slot.
422 SDValue Store = DAG.getTruncStore(Chain, dl,
423 Val, StackPtr, NULL, 0, StoredVT,
425 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
426 SmallVector<SDValue, 8> Stores;
429 // Do all but one copies using the full register width.
430 for (unsigned i = 1; i < NumRegs; i++) {
431 // Load one integer register's worth from the stack slot.
432 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0,
434 // Store it to the final location. Remember the store.
435 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
436 ST->getSrcValue(), SVOffset + Offset,
437 ST->isVolatile(), ST->isNonTemporal(),
438 MinAlign(ST->getAlignment(), Offset)));
439 // Increment the pointers.
441 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
443 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
446 // The last store may be partial. Do a truncating store. On big-endian
447 // machines this requires an extending load from the stack slot to ensure
448 // that the bits are in the right place.
449 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
450 8 * (StoredBytes - Offset));
452 // Load from the stack slot.
453 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
454 NULL, 0, MemVT, false, false, 0);
456 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
457 ST->getSrcValue(), SVOffset + Offset,
458 MemVT, ST->isVolatile(),
460 MinAlign(ST->getAlignment(), Offset)));
461 // The order of the stores doesn't matter - say it with a TokenFactor.
462 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
466 assert(ST->getMemoryVT().isInteger() &&
467 !ST->getMemoryVT().isVector() &&
468 "Unaligned store of unknown type.");
469 // Get the half-size VT
470 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
471 int NumBits = NewStoredVT.getSizeInBits();
472 int IncrementSize = NumBits / 8;
474 // Divide the stored value in two parts.
475 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
477 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
479 // Store the two parts
480 SDValue Store1, Store2;
481 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
482 ST->getSrcValue(), SVOffset, NewStoredVT,
483 ST->isVolatile(), ST->isNonTemporal(), Alignment);
484 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
485 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
486 Alignment = MinAlign(Alignment, IncrementSize);
487 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
488 ST->getSrcValue(), SVOffset + IncrementSize,
489 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
492 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
495 /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
497 SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
498 const TargetLowering &TLI) {
499 int SVOffset = LD->getSrcValueOffset();
500 SDValue Chain = LD->getChain();
501 SDValue Ptr = LD->getBasePtr();
502 EVT VT = LD->getValueType(0);
503 EVT LoadedVT = LD->getMemoryVT();
504 DebugLoc dl = LD->getDebugLoc();
505 if (VT.isFloatingPoint() || VT.isVector()) {
506 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
507 if (TLI.isTypeLegal(intVT)) {
508 // Expand to a (misaligned) integer load of the same size,
509 // then bitconvert to floating point or vector.
510 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
511 SVOffset, LD->isVolatile(),
512 LD->isNonTemporal(), LD->getAlignment());
513 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
514 if (VT.isFloatingPoint() && LoadedVT != VT)
515 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
517 SDValue Ops[] = { Result, Chain };
518 return DAG.getMergeValues(Ops, 2, dl);
520 // Copy the value to a (aligned) stack slot using (unaligned) integer
521 // loads and stores, then do a (aligned) load from the stack slot.
522 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
523 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
524 unsigned RegBytes = RegVT.getSizeInBits() / 8;
525 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
527 // Make sure the stack slot is also aligned for the register type.
528 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
530 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
531 SmallVector<SDValue, 8> Stores;
532 SDValue StackPtr = StackBase;
535 // Do all but one copies using the full register width.
536 for (unsigned i = 1; i < NumRegs; i++) {
537 // Load one integer register's worth from the original location.
538 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
539 SVOffset + Offset, LD->isVolatile(),
541 MinAlign(LD->getAlignment(), Offset));
542 // Follow the load with a store to the stack slot. Remember the store.
543 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
544 NULL, 0, false, false, 0));
545 // Increment the pointers.
547 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
548 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
552 // The last copy may be partial. Do an extending load.
553 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
554 8 * (LoadedBytes - Offset));
555 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
556 LD->getSrcValue(), SVOffset + Offset,
557 MemVT, LD->isVolatile(),
559 MinAlign(LD->getAlignment(), Offset));
560 // Follow the load with a store to the stack slot. Remember the store.
561 // On big-endian machines this requires a truncating store to ensure
562 // that the bits end up in the right place.
563 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
564 NULL, 0, MemVT, false, false, 0));
566 // The order of the stores doesn't matter - say it with a TokenFactor.
567 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
570 // Finally, perform the original load only redirected to the stack slot.
571 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
572 NULL, 0, LoadedVT, false, false, 0);
574 // Callers expect a MERGE_VALUES node.
575 SDValue Ops[] = { Load, TF };
576 return DAG.getMergeValues(Ops, 2, dl);
579 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
580 "Unaligned load of unsupported type.");
582 // Compute the new VT that is half the size of the old one. This is an
584 unsigned NumBits = LoadedVT.getSizeInBits();
586 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
589 unsigned Alignment = LD->getAlignment();
590 unsigned IncrementSize = NumBits / 8;
591 ISD::LoadExtType HiExtType = LD->getExtensionType();
593 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
594 if (HiExtType == ISD::NON_EXTLOAD)
595 HiExtType = ISD::ZEXTLOAD;
597 // Load the value in two parts
599 if (TLI.isLittleEndian()) {
600 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
601 SVOffset, NewLoadedVT, LD->isVolatile(),
602 LD->isNonTemporal(), Alignment);
603 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
604 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
605 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
606 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
607 LD->isNonTemporal(), MinAlign(Alignment, IncrementSize));
609 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
610 SVOffset, NewLoadedVT, LD->isVolatile(),
611 LD->isNonTemporal(), Alignment);
612 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
613 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
614 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
615 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
616 LD->isNonTemporal(), MinAlign(Alignment, IncrementSize));
619 // aggregate the two parts
620 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
621 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
622 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
624 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
627 SDValue Ops[] = { Result, TF };
628 return DAG.getMergeValues(Ops, 2, dl);
631 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
632 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
633 /// is necessary to spill the vector being inserted into to memory, perform
634 /// the insert there, and then read the result back.
635 SDValue SelectionDAGLegalize::
636 PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
642 // If the target doesn't support this, we have to spill the input vector
643 // to a temporary stack slot, update the element, then reload it. This is
644 // badness. We could also load the value into a vector register (either
645 // with a "move to register" or "extload into register" instruction, then
646 // permute it into place, if the idx is a constant and if the idx is
647 // supported by the target.
648 EVT VT = Tmp1.getValueType();
649 EVT EltVT = VT.getVectorElementType();
650 EVT IdxVT = Tmp3.getValueType();
651 EVT PtrVT = TLI.getPointerTy();
652 SDValue StackPtr = DAG.CreateStackTemporary(VT);
654 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
657 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
658 PseudoSourceValue::getFixedStack(SPFI), 0,
661 // Truncate or zero extend offset to target pointer type.
662 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
663 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
664 // Add the offset to the index.
665 unsigned EltSize = EltVT.getSizeInBits()/8;
666 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
667 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
668 // Store the scalar value.
669 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
670 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT,
672 // Load the updated vector.
673 return DAG.getLoad(VT, dl, Ch, StackPtr,
674 PseudoSourceValue::getFixedStack(SPFI), 0,
679 SDValue SelectionDAGLegalize::
680 ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
681 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
682 // SCALAR_TO_VECTOR requires that the type of the value being inserted
683 // match the element type of the vector being created, except for
684 // integers in which case the inserted value can be over width.
685 EVT EltVT = Vec.getValueType().getVectorElementType();
686 if (Val.getValueType() == EltVT ||
687 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
688 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
689 Vec.getValueType(), Val);
691 unsigned NumElts = Vec.getValueType().getVectorNumElements();
692 // We generate a shuffle of InVec and ScVec, so the shuffle mask
693 // should be 0,1,2,3,4,5... with the appropriate element replaced with
695 SmallVector<int, 8> ShufOps;
696 for (unsigned i = 0; i != NumElts; ++i)
697 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
699 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
703 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
706 SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
707 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
708 // FIXME: We shouldn't do this for TargetConstantFP's.
709 // FIXME: move this to the DAG Combiner! Note that we can't regress due
710 // to phase ordering between legalized code and the dag combiner. This
711 // probably means that we need to integrate dag combiner and legalizer
713 // We generally can't do this one for long doubles.
714 SDValue Tmp1 = ST->getChain();
715 SDValue Tmp2 = ST->getBasePtr();
717 int SVOffset = ST->getSrcValueOffset();
718 unsigned Alignment = ST->getAlignment();
719 bool isVolatile = ST->isVolatile();
720 bool isNonTemporal = ST->isNonTemporal();
721 DebugLoc dl = ST->getDebugLoc();
722 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
723 if (CFP->getValueType(0) == MVT::f32 &&
724 getTypeAction(MVT::i32) == Legal) {
725 Tmp3 = DAG.getConstant(CFP->getValueAPF().
726 bitcastToAPInt().zextOrTrunc(32),
728 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
729 SVOffset, isVolatile, isNonTemporal, Alignment);
730 } else if (CFP->getValueType(0) == MVT::f64) {
731 // If this target supports 64-bit registers, do a single 64-bit store.
732 if (getTypeAction(MVT::i64) == Legal) {
733 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
734 zextOrTrunc(64), MVT::i64);
735 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
736 SVOffset, isVolatile, isNonTemporal, Alignment);
737 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
738 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
739 // stores. If the target supports neither 32- nor 64-bits, this
740 // xform is certainly not worth it.
741 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
742 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
743 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
744 if (TLI.isBigEndian()) std::swap(Lo, Hi);
746 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
747 SVOffset, isVolatile, isNonTemporal, Alignment);
748 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
749 DAG.getIntPtrConstant(4));
750 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
751 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
753 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
760 /// LegalizeOp - We know that the specified value has a legal type, and
761 /// that its operands are legal. Now ensure that the operation itself
762 /// is legal, recursively ensuring that the operands' operations remain
764 SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
768 SDNode *Node = Op.getNode();
769 DebugLoc dl = Node->getDebugLoc();
771 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
772 assert(getTypeAction(Node->getValueType(i)) == Legal &&
773 "Unexpected illegal type!");
775 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
776 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
777 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
778 "Unexpected illegal type!");
780 // Note that LegalizeOp may be reentered even from single-use nodes, which
781 // means that we always must cache transformed nodes.
782 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
783 if (I != LegalizedNodes.end()) return I->second;
785 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
787 bool isCustom = false;
789 // Figure out the correct action; the way to query this varies by opcode
790 TargetLowering::LegalizeAction Action;
791 bool SimpleFinishLegalizing = true;
792 switch (Node->getOpcode()) {
793 case ISD::INTRINSIC_W_CHAIN:
794 case ISD::INTRINSIC_WO_CHAIN:
795 case ISD::INTRINSIC_VOID:
798 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
800 case ISD::SINT_TO_FP:
801 case ISD::UINT_TO_FP:
802 case ISD::EXTRACT_VECTOR_ELT:
803 Action = TLI.getOperationAction(Node->getOpcode(),
804 Node->getOperand(0).getValueType());
806 case ISD::FP_ROUND_INREG:
807 case ISD::SIGN_EXTEND_INREG: {
808 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
809 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
815 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
816 Node->getOpcode() == ISD::SETCC ? 2 : 1;
817 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
818 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
819 ISD::CondCode CCCode =
820 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
821 Action = TLI.getCondCodeAction(CCCode, OpVT);
822 if (Action == TargetLowering::Legal) {
823 if (Node->getOpcode() == ISD::SELECT_CC)
824 Action = TLI.getOperationAction(Node->getOpcode(),
825 Node->getValueType(0));
827 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
833 // FIXME: Model these properly. LOAD and STORE are complicated, and
834 // STORE expects the unlegalized operand in some cases.
835 SimpleFinishLegalizing = false;
837 case ISD::CALLSEQ_START:
838 case ISD::CALLSEQ_END:
839 // FIXME: This shouldn't be necessary. These nodes have special properties
840 // dealing with the recursive nature of legalization. Removing this
841 // special case should be done as part of making LegalizeDAG non-recursive.
842 SimpleFinishLegalizing = false;
844 case ISD::EXTRACT_ELEMENT:
845 case ISD::FLT_ROUNDS_:
853 case ISD::MERGE_VALUES:
855 case ISD::FRAME_TO_ARGS_OFFSET:
856 // These operations lie about being legal: when they claim to be legal,
857 // they should actually be expanded.
858 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
859 if (Action == TargetLowering::Legal)
860 Action = TargetLowering::Expand;
862 case ISD::TRAMPOLINE:
864 case ISD::RETURNADDR:
865 case ISD::EH_SJLJ_SETJMP:
866 case ISD::EH_SJLJ_LONGJMP:
867 // These operations lie about being legal: when they claim to be legal,
868 // they should actually be custom-lowered.
869 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
870 if (Action == TargetLowering::Legal)
871 Action = TargetLowering::Custom;
873 case ISD::BUILD_VECTOR:
874 // A weird case: legalization for BUILD_VECTOR never legalizes the
876 // FIXME: This really sucks... changing it isn't semantically incorrect,
877 // but it massively pessimizes the code for floating-point BUILD_VECTORs
878 // because ConstantFP operands get legalized into constant pool loads
879 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
880 // though, because BUILD_VECTORS usually get lowered into other nodes
881 // which get legalized properly.
882 SimpleFinishLegalizing = false;
885 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
886 Action = TargetLowering::Legal;
888 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
893 if (SimpleFinishLegalizing) {
894 SmallVector<SDValue, 8> Ops, ResultVals;
895 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
896 Ops.push_back(LegalizeOp(Node->getOperand(i)));
897 switch (Node->getOpcode()) {
904 // Branches tweak the chain to include LastCALLSEQ_END
905 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
907 Ops[0] = LegalizeOp(Ops[0]);
908 LastCALLSEQ_END = DAG.getEntryNode();
915 // Legalizing shifts/rotates requires adjusting the shift amount
916 // to the appropriate width.
917 if (!Ops[1].getValueType().isVector())
918 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
923 // Legalizing shifts/rotates requires adjusting the shift amount
924 // to the appropriate width.
925 if (!Ops[2].getValueType().isVector())
926 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[2]));
930 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
933 case TargetLowering::Legal:
934 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
935 ResultVals.push_back(Result.getValue(i));
937 case TargetLowering::Custom:
938 // FIXME: The handling for custom lowering with multiple results is
940 Tmp1 = TLI.LowerOperation(Result, DAG);
941 if (Tmp1.getNode()) {
942 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
944 ResultVals.push_back(Tmp1);
946 ResultVals.push_back(Tmp1.getValue(i));
952 case TargetLowering::Expand:
953 ExpandNode(Result.getNode(), ResultVals);
955 case TargetLowering::Promote:
956 PromoteNode(Result.getNode(), ResultVals);
959 if (!ResultVals.empty()) {
960 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
961 if (ResultVals[i] != SDValue(Node, i))
962 ResultVals[i] = LegalizeOp(ResultVals[i]);
963 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
965 return ResultVals[Op.getResNo()];
969 switch (Node->getOpcode()) {
976 assert(0 && "Do not know how to legalize this operator!");
978 case ISD::BUILD_VECTOR:
979 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
980 default: assert(0 && "This action is not supported yet!");
981 case TargetLowering::Custom:
982 Tmp3 = TLI.LowerOperation(Result, DAG);
983 if (Tmp3.getNode()) {
988 case TargetLowering::Expand:
989 Result = ExpandBUILD_VECTOR(Result.getNode());
993 case ISD::CALLSEQ_START: {
994 SDNode *CallEnd = FindCallEndFromCallStart(Node);
996 // Recursively Legalize all of the inputs of the call end that do not lead
997 // to this call start. This ensures that any libcalls that need be inserted
998 // are inserted *before* the CALLSEQ_START.
999 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1000 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1001 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
1005 // Now that we legalized all of the inputs (which may have inserted
1006 // libcalls) create the new CALLSEQ_START node.
1007 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1009 // Merge in the last call, to ensure that this call start after the last
1011 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1012 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1013 Tmp1, LastCALLSEQ_END);
1014 Tmp1 = LegalizeOp(Tmp1);
1017 // Do not try to legalize the target-specific arguments (#1+).
1018 if (Tmp1 != Node->getOperand(0)) {
1019 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1021 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1024 // Remember that the CALLSEQ_START is legalized.
1025 AddLegalizedOperand(Op.getValue(0), Result);
1026 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1027 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1029 // Now that the callseq_start and all of the non-call nodes above this call
1030 // sequence have been legalized, legalize the call itself. During this
1031 // process, no libcalls can/will be inserted, guaranteeing that no calls
1033 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1034 // Note that we are selecting this call!
1035 LastCALLSEQ_END = SDValue(CallEnd, 0);
1036 IsLegalizingCall = true;
1038 // Legalize the call, starting from the CALLSEQ_END.
1039 LegalizeOp(LastCALLSEQ_END);
1040 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1043 case ISD::CALLSEQ_END:
1044 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1045 // will cause this node to be legalized as well as handling libcalls right.
1046 if (LastCALLSEQ_END.getNode() != Node) {
1047 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1048 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
1049 assert(I != LegalizedNodes.end() &&
1050 "Legalizing the call start should have legalized this node!");
1054 // Otherwise, the call start has been legalized and everything is going
1055 // according to plan. Just legalize ourselves normally here.
1056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1057 // Do not try to legalize the target-specific arguments (#1+), except for
1058 // an optional flag input.
1059 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1060 if (Tmp1 != Node->getOperand(0)) {
1061 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1063 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1066 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1067 if (Tmp1 != Node->getOperand(0) ||
1068 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1069 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1072 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1075 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1076 // This finishes up call legalization.
1077 IsLegalizingCall = false;
1079 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1080 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1081 if (Node->getNumValues() == 2)
1082 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
1083 return Result.getValue(Op.getResNo());
1085 LoadSDNode *LD = cast<LoadSDNode>(Node);
1086 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1087 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1089 ISD::LoadExtType ExtType = LD->getExtensionType();
1090 if (ExtType == ISD::NON_EXTLOAD) {
1091 EVT VT = Node->getValueType(0);
1092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1093 Tmp3 = Result.getValue(0);
1094 Tmp4 = Result.getValue(1);
1096 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1097 default: assert(0 && "This action is not supported yet!");
1098 case TargetLowering::Legal:
1099 // If this is an unaligned load and the target doesn't support it,
1101 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1102 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1103 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
1104 if (LD->getAlignment() < ABIAlignment){
1105 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1107 Tmp3 = Result.getOperand(0);
1108 Tmp4 = Result.getOperand(1);
1109 Tmp3 = LegalizeOp(Tmp3);
1110 Tmp4 = LegalizeOp(Tmp4);
1114 case TargetLowering::Custom:
1115 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1116 if (Tmp1.getNode()) {
1117 Tmp3 = LegalizeOp(Tmp1);
1118 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1121 case TargetLowering::Promote: {
1122 // Only promote a load of vector type to another.
1123 assert(VT.isVector() && "Cannot promote this load!");
1124 // Change base type to a different vector type.
1125 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1127 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
1128 LD->getSrcValueOffset(),
1129 LD->isVolatile(), LD->isNonTemporal(),
1130 LD->getAlignment());
1131 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
1132 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1136 // Since loads produce two values, make sure to remember that we
1137 // legalized both of them.
1138 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1139 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
1140 return Op.getResNo() ? Tmp4 : Tmp3;
1142 EVT SrcVT = LD->getMemoryVT();
1143 unsigned SrcWidth = SrcVT.getSizeInBits();
1144 int SVOffset = LD->getSrcValueOffset();
1145 unsigned Alignment = LD->getAlignment();
1146 bool isVolatile = LD->isVolatile();
1147 bool isNonTemporal = LD->isNonTemporal();
1149 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1150 // Some targets pretend to have an i1 loading operation, and actually
1151 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1152 // bits are guaranteed to be zero; it helps the optimizers understand
1153 // that these bits are zero. It is also useful for EXTLOAD, since it
1154 // tells the optimizers that those bits are undefined. It would be
1155 // nice to have an effective generic way of getting these benefits...
1156 // Until such a way is found, don't insist on promoting i1 here.
1157 (SrcVT != MVT::i1 ||
1158 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1159 // Promote to a byte-sized load if not loading an integral number of
1160 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1161 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1162 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1165 // The extra bits are guaranteed to be zero, since we stored them that
1166 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1168 ISD::LoadExtType NewExtType =
1169 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1171 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
1172 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1173 NVT, isVolatile, isNonTemporal, Alignment);
1175 Ch = Result.getValue(1); // The chain.
1177 if (ExtType == ISD::SEXTLOAD)
1178 // Having the top bits zero doesn't help when sign extending.
1179 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1180 Result.getValueType(),
1181 Result, DAG.getValueType(SrcVT));
1182 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1183 // All the top bits are guaranteed to be zero - inform the optimizers.
1184 Result = DAG.getNode(ISD::AssertZext, dl,
1185 Result.getValueType(), Result,
1186 DAG.getValueType(SrcVT));
1188 Tmp1 = LegalizeOp(Result);
1189 Tmp2 = LegalizeOp(Ch);
1190 } else if (SrcWidth & (SrcWidth - 1)) {
1191 // If not loading a power-of-2 number of bits, expand as two loads.
1192 assert(!SrcVT.isVector() && "Unsupported extload!");
1193 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1194 assert(RoundWidth < SrcWidth);
1195 unsigned ExtraWidth = SrcWidth - RoundWidth;
1196 assert(ExtraWidth < RoundWidth);
1197 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1198 "Load size not an integral number of bytes!");
1199 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1200 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1202 unsigned IncrementSize;
1204 if (TLI.isLittleEndian()) {
1205 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1206 // Load the bottom RoundWidth bits.
1207 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1208 Node->getValueType(0), Tmp1, Tmp2,
1209 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1210 isNonTemporal, Alignment);
1212 // Load the remaining ExtraWidth bits.
1213 IncrementSize = RoundWidth / 8;
1214 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1215 DAG.getIntPtrConstant(IncrementSize));
1216 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
1217 LD->getSrcValue(), SVOffset + IncrementSize,
1218 ExtraVT, isVolatile, isNonTemporal,
1219 MinAlign(Alignment, IncrementSize));
1221 // Build a factor node to remember that this load is independent of the
1223 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1226 // Move the top bits to the right place.
1227 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1228 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1230 // Join the hi and lo parts.
1231 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
1233 // Big endian - avoid unaligned loads.
1234 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1235 // Load the top RoundWidth bits.
1236 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
1237 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1238 isNonTemporal, Alignment);
1240 // Load the remaining ExtraWidth bits.
1241 IncrementSize = RoundWidth / 8;
1242 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1243 DAG.getIntPtrConstant(IncrementSize));
1244 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1245 Node->getValueType(0), Tmp1, Tmp2,
1246 LD->getSrcValue(), SVOffset + IncrementSize,
1247 ExtraVT, isVolatile, isNonTemporal,
1248 MinAlign(Alignment, IncrementSize));
1250 // Build a factor node to remember that this load is independent of the
1252 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1255 // Move the top bits to the right place.
1256 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1257 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1259 // Join the hi and lo parts.
1260 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
1263 Tmp1 = LegalizeOp(Result);
1264 Tmp2 = LegalizeOp(Ch);
1266 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1267 default: assert(0 && "This action is not supported yet!");
1268 case TargetLowering::Custom:
1271 case TargetLowering::Legal:
1272 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1273 Tmp1 = Result.getValue(0);
1274 Tmp2 = Result.getValue(1);
1277 Tmp3 = TLI.LowerOperation(Result, DAG);
1278 if (Tmp3.getNode()) {
1279 Tmp1 = LegalizeOp(Tmp3);
1280 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1283 // If this is an unaligned load and the target doesn't support it,
1285 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1286 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1287 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
1288 if (LD->getAlignment() < ABIAlignment){
1289 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1291 Tmp1 = Result.getOperand(0);
1292 Tmp2 = Result.getOperand(1);
1293 Tmp1 = LegalizeOp(Tmp1);
1294 Tmp2 = LegalizeOp(Tmp2);
1299 case TargetLowering::Expand:
1300 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1301 // f128 = EXTLOAD {f32,f64} too
1302 if ((SrcVT == MVT::f32 && (Node->getValueType(0) == MVT::f64 ||
1303 Node->getValueType(0) == MVT::f128)) ||
1304 (SrcVT == MVT::f64 && Node->getValueType(0) == MVT::f128)) {
1305 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
1306 LD->getSrcValueOffset(),
1307 LD->isVolatile(), LD->isNonTemporal(),
1308 LD->getAlignment());
1309 Result = DAG.getNode(ISD::FP_EXTEND, dl,
1310 Node->getValueType(0), Load);
1311 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1312 Tmp2 = LegalizeOp(Load.getValue(1));
1315 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1316 // Turn the unsupported load into an EXTLOAD followed by an explicit
1317 // zero/sign extend inreg.
1318 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
1319 Tmp1, Tmp2, LD->getSrcValue(),
1320 LD->getSrcValueOffset(), SrcVT,
1321 LD->isVolatile(), LD->isNonTemporal(),
1322 LD->getAlignment());
1324 if (ExtType == ISD::SEXTLOAD)
1325 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1326 Result.getValueType(),
1327 Result, DAG.getValueType(SrcVT));
1329 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
1330 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1331 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1336 // Since loads produce two values, make sure to remember that we legalized
1338 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1339 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1340 return Op.getResNo() ? Tmp2 : Tmp1;
1344 StoreSDNode *ST = cast<StoreSDNode>(Node);
1345 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1346 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
1347 int SVOffset = ST->getSrcValueOffset();
1348 unsigned Alignment = ST->getAlignment();
1349 bool isVolatile = ST->isVolatile();
1350 bool isNonTemporal = ST->isNonTemporal();
1352 if (!ST->isTruncatingStore()) {
1353 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1354 Result = SDValue(OptStore, 0);
1359 Tmp3 = LegalizeOp(ST->getValue());
1360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1363 EVT VT = Tmp3.getValueType();
1364 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1365 default: assert(0 && "This action is not supported yet!");
1366 case TargetLowering::Legal:
1367 // If this is an unaligned store and the target doesn't support it,
1369 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
1370 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
1371 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
1372 if (ST->getAlignment() < ABIAlignment)
1373 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1377 case TargetLowering::Custom:
1378 Tmp1 = TLI.LowerOperation(Result, DAG);
1379 if (Tmp1.getNode()) Result = Tmp1;
1381 case TargetLowering::Promote:
1382 assert(VT.isVector() && "Unknown legal promote case!");
1383 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
1384 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1385 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
1386 ST->getSrcValue(), SVOffset, isVolatile,
1387 isNonTemporal, Alignment);
1393 Tmp3 = LegalizeOp(ST->getValue());
1395 EVT StVT = ST->getMemoryVT();
1396 unsigned StWidth = StVT.getSizeInBits();
1398 if (StWidth != StVT.getStoreSizeInBits()) {
1399 // Promote to a byte-sized store with upper bits zero if not
1400 // storing an integral number of bytes. For example, promote
1401 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
1402 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1403 StVT.getStoreSizeInBits());
1404 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1405 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1406 SVOffset, NVT, isVolatile, isNonTemporal,
1408 } else if (StWidth & (StWidth - 1)) {
1409 // If not storing a power-of-2 number of bits, expand as two stores.
1410 assert(!StVT.isVector() && "Unsupported truncstore!");
1411 unsigned RoundWidth = 1 << Log2_32(StWidth);
1412 assert(RoundWidth < StWidth);
1413 unsigned ExtraWidth = StWidth - RoundWidth;
1414 assert(ExtraWidth < RoundWidth);
1415 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1416 "Store size not an integral number of bytes!");
1417 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1418 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1420 unsigned IncrementSize;
1422 if (TLI.isLittleEndian()) {
1423 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1424 // Store the bottom RoundWidth bits.
1425 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1427 isVolatile, isNonTemporal, Alignment);
1429 // Store the remaining ExtraWidth bits.
1430 IncrementSize = RoundWidth / 8;
1431 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1432 DAG.getIntPtrConstant(IncrementSize));
1433 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
1434 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1435 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1436 SVOffset + IncrementSize, ExtraVT, isVolatile,
1438 MinAlign(Alignment, IncrementSize));
1440 // Big endian - avoid unaligned stores.
1441 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1442 // Store the top RoundWidth bits.
1443 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
1444 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1445 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1446 SVOffset, RoundVT, isVolatile, isNonTemporal,
1449 // Store the remaining ExtraWidth bits.
1450 IncrementSize = RoundWidth / 8;
1451 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1452 DAG.getIntPtrConstant(IncrementSize));
1453 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1454 SVOffset + IncrementSize, ExtraVT, isVolatile,
1456 MinAlign(Alignment, IncrementSize));
1459 // The order of the stores doesn't matter.
1460 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
1462 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1463 Tmp2 != ST->getBasePtr())
1464 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1467 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1468 default: assert(0 && "This action is not supported yet!");
1469 case TargetLowering::Legal:
1470 // If this is an unaligned store and the target doesn't support it,
1472 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
1473 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
1474 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
1475 if (ST->getAlignment() < ABIAlignment)
1476 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1480 case TargetLowering::Custom:
1481 Result = TLI.LowerOperation(Result, DAG);
1484 // TRUNCSTORE:i16 i32 -> STORE i16
1485 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
1486 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1487 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1488 SVOffset, isVolatile, isNonTemporal,
1497 assert(Result.getValueType() == Op.getValueType() &&
1498 "Bad legalization!");
1500 // Make sure that the generated code is itself legal.
1502 Result = LegalizeOp(Result);
1504 // Note that LegalizeOp may be reentered even from single-use nodes, which
1505 // means that we always must cache transformed nodes.
1506 AddLegalizedOperand(Op, Result);
1510 SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1511 SDValue Vec = Op.getOperand(0);
1512 SDValue Idx = Op.getOperand(1);
1513 DebugLoc dl = Op.getDebugLoc();
1514 // Store the value to a temporary stack slot, then LOAD the returned part.
1515 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1516 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0,
1519 // Add the offset to the index.
1521 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1522 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1523 DAG.getConstant(EltSize, Idx.getValueType()));
1525 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1526 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1528 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1530 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1532 if (Op.getValueType().isVector())
1533 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0,
1536 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1537 NULL, 0, Vec.getValueType().getVectorElementType(),
1541 SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1542 // We can't handle this case efficiently. Allocate a sufficiently
1543 // aligned object on the stack, store each element into it, then load
1544 // the result as a vector.
1545 // Create the stack frame object.
1546 EVT VT = Node->getValueType(0);
1547 EVT EltVT = VT.getVectorElementType();
1548 DebugLoc dl = Node->getDebugLoc();
1549 SDValue FIPtr = DAG.CreateStackTemporary(VT);
1550 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
1551 const Value *SV = PseudoSourceValue::getFixedStack(FI);
1553 // Emit a store of each element to the stack slot.
1554 SmallVector<SDValue, 8> Stores;
1555 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
1556 // Store (in the right endianness) the elements to memory.
1557 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1558 // Ignore undef elements.
1559 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1561 unsigned Offset = TypeByteSize*i;
1563 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1564 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1566 // If the destination vector element type is narrower than the source
1567 // element type, only store the bits necessary.
1568 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
1569 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
1570 Node->getOperand(i), Idx, SV, Offset,
1571 EltVT, false, false, 0));
1573 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
1574 Node->getOperand(i), Idx, SV, Offset,
1579 if (!Stores.empty()) // Not all undef elements?
1580 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1581 &Stores[0], Stores.size());
1583 StoreChain = DAG.getEntryNode();
1585 // Result is a load from the stack slot.
1586 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0, false, false, 0);
1589 SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1590 DebugLoc dl = Node->getDebugLoc();
1591 SDValue Tmp1 = Node->getOperand(0);
1592 SDValue Tmp2 = Node->getOperand(1);
1594 // Get the sign bit of the RHS. First obtain a value that has the same
1595 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
1597 EVT FloatVT = Tmp2.getValueType();
1598 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
1599 if (isTypeLegal(IVT)) {
1600 // Convert to an integer with the same sign bit.
1601 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
1603 // Store the float to memory, then load the sign part out as an integer.
1604 MVT LoadTy = TLI.getPointerTy();
1605 // First create a temporary that is aligned for both the load and store.
1606 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1607 // Then store the float to it.
1609 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, NULL, 0,
1611 if (TLI.isBigEndian()) {
1612 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1613 // Load out a legal integer with the same sign bit as the float.
1614 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, NULL, 0, false, false, 0);
1615 } else { // Little endian
1616 SDValue LoadPtr = StackPtr;
1617 // The float may be wider than the integer we are going to load. Advance
1618 // the pointer so that the loaded integer will contain the sign bit.
1619 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1620 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1621 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1622 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1623 // Load a legal integer containing the sign bit.
1624 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, NULL, 0, false, false, 0);
1625 // Move the sign bit to the top bit of the loaded integer.
1626 unsigned BitShift = LoadTy.getSizeInBits() -
1627 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1628 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1630 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
1631 DAG.getConstant(BitShift,TLI.getShiftAmountTy()));
1634 // Now get the sign bit proper, by seeing whether the value is negative.
1635 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1636 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1638 // Get the absolute value of the result.
1639 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1640 // Select between the nabs and abs value based on the sign bit of
1642 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1643 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1647 void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1648 SmallVectorImpl<SDValue> &Results) {
1649 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1650 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1651 " not tell us which reg is the stack pointer!");
1652 DebugLoc dl = Node->getDebugLoc();
1653 EVT VT = Node->getValueType(0);
1654 SDValue Tmp1 = SDValue(Node, 0);
1655 SDValue Tmp2 = SDValue(Node, 1);
1656 SDValue Tmp3 = Node->getOperand(2);
1657 SDValue Chain = Tmp1.getOperand(0);
1659 // Chain the dynamic stack allocation so that it doesn't modify the stack
1660 // pointer when other instructions are using the stack.
1661 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1663 SDValue Size = Tmp2.getOperand(1);
1664 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1665 Chain = SP.getValue(1);
1666 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
1667 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
1668 if (Align > StackAlign)
1669 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1670 DAG.getConstant(-(uint64_t)Align, VT));
1671 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1672 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1674 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1675 DAG.getIntPtrConstant(0, true), SDValue());
1677 Results.push_back(Tmp1);
1678 Results.push_back(Tmp2);
1681 /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
1682 /// condition code CC on the current target. This routine expands SETCC with
1683 /// illegal condition code into AND / OR of multiple SETCC values.
1684 void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
1685 SDValue &LHS, SDValue &RHS,
1688 EVT OpVT = LHS.getValueType();
1689 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1690 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
1691 default: assert(0 && "Unknown condition code action!");
1692 case TargetLowering::Legal:
1695 case TargetLowering::Expand: {
1696 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1699 default: assert(0 && "Don't know how to expand this condition!");
1700 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1701 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1702 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1703 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1704 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1705 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1706 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1707 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1708 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1709 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1710 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1711 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1712 // FIXME: Implement more expansions.
1715 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1716 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1717 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
1725 /// EmitStackConvert - Emit a store/load combination to the stack. This stores
1726 /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1727 /// a load from the stack slot to DestVT, extending it if needed.
1728 /// The resultant code need not be legal.
1729 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
1733 // Create the stack frame object.
1735 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
1736 getTypeForEVT(*DAG.getContext()));
1737 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
1739 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1740 int SPFI = StackPtrFI->getIndex();
1741 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
1743 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1744 unsigned SlotSize = SlotVT.getSizeInBits();
1745 unsigned DestSize = DestVT.getSizeInBits();
1746 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1747 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
1749 // Emit a store to the stack slot. Use a truncstore if the input value is
1750 // later than DestVT.
1753 if (SrcSize > SlotSize)
1754 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
1755 SV, 0, SlotVT, false, false, SrcAlign);
1757 assert(SrcSize == SlotSize && "Invalid store");
1758 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
1759 SV, 0, false, false, SrcAlign);
1762 // Result is a load from the stack slot.
1763 if (SlotSize == DestSize)
1764 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, false,
1767 assert(SlotSize < DestSize && "Unknown extension!");
1768 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
1769 false, false, DestAlign);
1772 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
1773 DebugLoc dl = Node->getDebugLoc();
1774 // Create a vector sized/aligned stack slot, store the value to element #0,
1775 // then load the whole vector back out.
1776 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
1778 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1779 int SPFI = StackPtrFI->getIndex();
1781 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1783 PseudoSourceValue::getFixedStack(SPFI), 0,
1784 Node->getValueType(0).getVectorElementType(),
1786 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
1787 PseudoSourceValue::getFixedStack(SPFI), 0,
1792 /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
1793 /// support the operation, but do support the resultant vector type.
1794 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
1795 unsigned NumElems = Node->getNumOperands();
1796 SDValue Value1, Value2;
1797 DebugLoc dl = Node->getDebugLoc();
1798 EVT VT = Node->getValueType(0);
1799 EVT OpVT = Node->getOperand(0).getValueType();
1800 EVT EltVT = VT.getVectorElementType();
1802 // If the only non-undef value is the low element, turn this into a
1803 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
1804 bool isOnlyLowElement = true;
1805 bool MoreThanTwoValues = false;
1806 bool isConstant = true;
1807 for (unsigned i = 0; i < NumElems; ++i) {
1808 SDValue V = Node->getOperand(i);
1809 if (V.getOpcode() == ISD::UNDEF)
1812 isOnlyLowElement = false;
1813 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
1816 if (!Value1.getNode()) {
1818 } else if (!Value2.getNode()) {
1821 } else if (V != Value1 && V != Value2) {
1822 MoreThanTwoValues = true;
1826 if (!Value1.getNode())
1827 return DAG.getUNDEF(VT);
1829 if (isOnlyLowElement)
1830 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
1832 // If all elements are constants, create a load from the constant pool.
1834 std::vector<Constant*> CV;
1835 for (unsigned i = 0, e = NumElems; i != e; ++i) {
1836 if (ConstantFPSDNode *V =
1837 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
1838 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
1839 } else if (ConstantSDNode *V =
1840 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
1842 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1844 // If OpVT and EltVT don't match, EltVT is not legal and the
1845 // element values have been promoted/truncated earlier. Undo this;
1846 // we don't want a v16i8 to become a v16i32 for example.
1847 const ConstantInt *CI = V->getConstantIntValue();
1848 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1849 CI->getZExtValue()));
1852 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
1853 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
1854 CV.push_back(UndefValue::get(OpNTy));
1857 Constant *CP = ConstantVector::get(CV);
1858 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
1859 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
1860 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
1861 PseudoSourceValue::getConstantPool(), 0,
1862 false, false, Alignment);
1865 if (!MoreThanTwoValues) {
1866 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1867 for (unsigned i = 0; i < NumElems; ++i) {
1868 SDValue V = Node->getOperand(i);
1869 if (V.getOpcode() == ISD::UNDEF)
1871 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1873 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
1874 // Get the splatted value into the low element of a vector register.
1875 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1877 if (Value2.getNode())
1878 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1880 Vec2 = DAG.getUNDEF(VT);
1882 // Return shuffle(LowValVec, undef, <0,0,0,0>)
1883 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
1887 // Otherwise, we can't handle this case efficiently.
1888 return ExpandVectorBuildThroughStack(Node);
1891 // ExpandLibCall - Expand a node into a call to a libcall. If the result value
1892 // does not fit into a register, return the lo part and set the hi part to the
1893 // by-reg argument. If it does fit into a single register, return the result
1894 // and leave the Hi part unset.
1895 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
1897 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
1898 // The input chain to this libcall is the entry node of the function.
1899 // Legalizing the call will automatically add the previous call to the
1901 SDValue InChain = DAG.getEntryNode();
1903 TargetLowering::ArgListTy Args;
1904 TargetLowering::ArgListEntry Entry;
1905 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1906 EVT ArgVT = Node->getOperand(i).getValueType();
1907 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1908 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
1909 Entry.isSExt = isSigned;
1910 Entry.isZExt = !isSigned;
1911 Args.push_back(Entry);
1913 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1914 TLI.getPointerTy());
1916 // Splice the libcall in wherever FindInputOutputChains tells us to.
1917 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
1918 std::pair<SDValue, SDValue> CallInfo =
1919 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
1920 0, TLI.getLibcallCallingConv(LC), false,
1921 /*isReturnValueUsed=*/true,
1922 Callee, Args, DAG, Node->getDebugLoc());
1924 // Legalize the call sequence, starting with the chain. This will advance
1925 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
1926 // was added by LowerCallTo (guaranteeing proper serialization of calls).
1927 LegalizeOp(CallInfo.second);
1928 return CallInfo.first;
1931 SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
1932 RTLIB::Libcall Call_F32,
1933 RTLIB::Libcall Call_F64,
1934 RTLIB::Libcall Call_F80,
1935 RTLIB::Libcall Call_PPCF128) {
1937 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1938 default: assert(0 && "Unexpected request for libcall!");
1939 case MVT::f32: LC = Call_F32; break;
1940 case MVT::f64: LC = Call_F64; break;
1941 case MVT::f80: LC = Call_F80; break;
1942 case MVT::ppcf128: LC = Call_PPCF128; break;
1944 return ExpandLibCall(LC, Node, false);
1947 SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
1948 RTLIB::Libcall Call_I8,
1949 RTLIB::Libcall Call_I16,
1950 RTLIB::Libcall Call_I32,
1951 RTLIB::Libcall Call_I64,
1952 RTLIB::Libcall Call_I128) {
1954 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1955 default: assert(0 && "Unexpected request for libcall!");
1956 case MVT::i8: LC = Call_I8; break;
1957 case MVT::i16: LC = Call_I16; break;
1958 case MVT::i32: LC = Call_I32; break;
1959 case MVT::i64: LC = Call_I64; break;
1960 case MVT::i128: LC = Call_I128; break;
1962 return ExpandLibCall(LC, Node, isSigned);
1965 /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
1966 /// INT_TO_FP operation of the specified operand when the target requests that
1967 /// we expand it. At this point, we know that the result and operand types are
1968 /// legal for the target.
1969 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
1973 if (Op0.getValueType() == MVT::i32) {
1974 // simple 32-bit [signed|unsigned] integer to float/double expansion
1976 // Get the stack frame index of a 8 byte buffer.
1977 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
1979 // word offset constant for Hi/Lo address computation
1980 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
1981 // set up Hi and Lo (into buffer) address based on endian
1982 SDValue Hi = StackSlot;
1983 SDValue Lo = DAG.getNode(ISD::ADD, dl,
1984 TLI.getPointerTy(), StackSlot, WordOff);
1985 if (TLI.isLittleEndian())
1988 // if signed map to unsigned space
1991 // constant used to invert sign bit (signed to unsigned mapping)
1992 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
1993 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
1997 // store the lo of the constructed double - based on integer input
1998 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
1999 Op0Mapped, Lo, NULL, 0,
2001 // initial hi portion of constructed double
2002 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
2003 // store the hi of the constructed double - biased exponent
2004 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0,
2006 // load the constructed double
2007 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0,
2009 // FP constant to bias correct the final result
2010 SDValue Bias = DAG.getConstantFP(isSigned ?
2011 BitsToDouble(0x4330000080000000ULL) :
2012 BitsToDouble(0x4330000000000000ULL),
2014 // subtract the bias
2015 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2018 // handle final rounding
2019 if (DestVT == MVT::f64) {
2022 } else if (DestVT.bitsLT(MVT::f64)) {
2023 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
2024 DAG.getIntPtrConstant(0));
2025 } else if (DestVT.bitsGT(MVT::f64)) {
2026 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
2030 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
2031 // Code below here assumes !isSigned without checking again.
2033 // Implementation of unsigned i64 to f64 following the algorithm in
2034 // __floatundidf in compiler_rt. This implementation has the advantage
2035 // of performing rounding correctly, both in the default rounding mode
2036 // and in all alternate rounding modes.
2037 // TODO: Generalize this for use with other types.
2038 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2040 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2041 SDValue TwoP84PlusTwoP52 =
2042 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2044 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2046 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2047 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2048 DAG.getConstant(32, MVT::i64));
2049 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2050 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
2051 SDValue LoFlt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, LoOr);
2052 SDValue HiFlt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, HiOr);
2053 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt, TwoP84PlusTwoP52);
2054 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2057 // Implementation of unsigned i64 to f32. This implementation has the
2058 // advantage of performing rounding correctly.
2059 // TODO: Generalize this for use with other types.
2060 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
2061 EVT SHVT = TLI.getShiftAmountTy();
2063 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
2064 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2065 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2066 DAG.getConstant(UINT64_C(0x800), MVT::i64));
2067 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
2068 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2069 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2070 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2071 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2072 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2073 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
2075 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
2077 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2078 DAG.getConstant(32, SHVT));
2079 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2080 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2082 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2083 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2084 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2085 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2086 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2087 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2088 DAG.getIntPtrConstant(0));
2092 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2094 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2095 Op0, DAG.getConstant(0, Op0.getValueType()),
2097 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2098 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2099 SignSet, Four, Zero);
2101 // If the sign bit of the integer is set, the large number will be treated
2102 // as a negative number. To counteract this, the dynamic code adds an
2103 // offset depending on the data type.
2105 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
2106 default: assert(0 && "Unsupported integer type!");
2107 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2108 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2109 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2110 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2112 if (TLI.isLittleEndian()) FF <<= 32;
2113 Constant *FudgeFactor = ConstantInt::get(
2114 Type::getInt64Ty(*DAG.getContext()), FF);
2116 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2117 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2118 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2119 Alignment = std::min(Alignment, 4u);
2121 if (DestVT == MVT::f32)
2122 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2123 PseudoSourceValue::getConstantPool(), 0,
2124 false, false, Alignment);
2127 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
2128 DAG.getEntryNode(), CPIdx,
2129 PseudoSourceValue::getConstantPool(), 0,
2130 MVT::f32, false, false, Alignment));
2133 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
2136 /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2137 /// *INT_TO_FP operation of the specified operand when the target requests that
2138 /// we promote it. At this point, we know that the result and operand types are
2139 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2140 /// operation that takes a larger input.
2141 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
2145 // First step, figure out the appropriate *INT_TO_FP operation to use.
2146 EVT NewInTy = LegalOp.getValueType();
2148 unsigned OpToUse = 0;
2150 // Scan for the appropriate larger type to use.
2152 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
2153 assert(NewInTy.isInteger() && "Ran out of possibilities!");
2155 // If the target supports SINT_TO_FP of this type, use it.
2156 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2157 OpToUse = ISD::SINT_TO_FP;
2160 if (isSigned) continue;
2162 // If the target supports UINT_TO_FP of this type, use it.
2163 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2164 OpToUse = ISD::UINT_TO_FP;
2168 // Otherwise, try a larger type.
2171 // Okay, we found the operation and type to use. Zero extend our input to the
2172 // desired type then run the operation on it.
2173 return DAG.getNode(OpToUse, dl, DestVT,
2174 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2175 dl, NewInTy, LegalOp));
2178 /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2179 /// FP_TO_*INT operation of the specified operand when the target requests that
2180 /// we promote it. At this point, we know that the result and operand types are
2181 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2182 /// operation that returns a larger result.
2183 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
2187 // First step, figure out the appropriate FP_TO*INT operation to use.
2188 EVT NewOutTy = DestVT;
2190 unsigned OpToUse = 0;
2192 // Scan for the appropriate larger type to use.
2194 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
2195 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2197 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
2198 OpToUse = ISD::FP_TO_SINT;
2202 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
2203 OpToUse = ISD::FP_TO_UINT;
2207 // Otherwise, try a larger type.
2211 // Okay, we found the operation and type to use.
2212 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
2214 // Truncate the result of the extended FP_TO_*INT operation to the desired
2216 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2219 /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2221 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
2222 EVT VT = Op.getValueType();
2223 EVT SHVT = TLI.getShiftAmountTy();
2224 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
2225 switch (VT.getSimpleVT().SimpleTy) {
2226 default: assert(0 && "Unhandled Expand type in BSWAP!");
2228 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2229 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2230 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2232 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2233 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2234 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2235 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2236 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2237 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2238 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2239 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2240 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2242 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2243 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2244 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2245 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2246 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2247 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2248 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2249 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2250 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2251 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2252 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2253 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2254 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2255 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2256 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2257 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2258 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2259 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2260 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2261 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2262 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
2266 /// ExpandBitCount - Expand the specified bitcount instruction into operations.
2268 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
2271 default: assert(0 && "Cannot expand this yet!");
2273 static const uint64_t mask[6] = {
2274 0x5555555555555555ULL, 0x3333333333333333ULL,
2275 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2276 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2278 EVT VT = Op.getValueType();
2279 EVT ShVT = TLI.getShiftAmountTy();
2280 unsigned len = VT.getSizeInBits();
2281 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2282 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
2283 unsigned EltSize = VT.isVector() ?
2284 VT.getVectorElementType().getSizeInBits() : len;
2285 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
2286 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
2287 Op = DAG.getNode(ISD::ADD, dl, VT,
2288 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
2289 DAG.getNode(ISD::AND, dl, VT,
2290 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
2296 // for now, we do this:
2297 // x = x | (x >> 1);
2298 // x = x | (x >> 2);
2300 // x = x | (x >>16);
2301 // x = x | (x >>32); // for 64-bit input
2302 // return popcount(~x);
2304 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
2305 EVT VT = Op.getValueType();
2306 EVT ShVT = TLI.getShiftAmountTy();
2307 unsigned len = VT.getSizeInBits();
2308 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2309 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
2310 Op = DAG.getNode(ISD::OR, dl, VT, Op,
2311 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
2313 Op = DAG.getNOT(dl, Op, VT);
2314 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
2317 // for now, we use: { return popcount(~x & (x - 1)); }
2318 // unless the target has ctlz but not ctpop, in which case we use:
2319 // { return 32 - nlz(~x & (x-1)); }
2320 // see also http://www.hackersdelight.org/HDcode/ntz.cc
2321 EVT VT = Op.getValueType();
2322 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2323 DAG.getNOT(dl, Op, VT),
2324 DAG.getNode(ISD::SUB, dl, VT, Op,
2325 DAG.getConstant(1, VT)));
2326 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
2327 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2328 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
2329 return DAG.getNode(ISD::SUB, dl, VT,
2330 DAG.getConstant(VT.getSizeInBits(), VT),
2331 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2332 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
2337 void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2338 SmallVectorImpl<SDValue> &Results) {
2339 DebugLoc dl = Node->getDebugLoc();
2340 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
2341 switch (Node->getOpcode()) {
2345 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2346 Results.push_back(Tmp1);
2349 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
2351 case ISD::FRAMEADDR:
2352 case ISD::RETURNADDR:
2353 case ISD::FRAME_TO_ARGS_OFFSET:
2354 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2356 case ISD::FLT_ROUNDS_:
2357 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2359 case ISD::EH_RETURN:
2363 Results.push_back(Node->getOperand(0));
2365 case ISD::MEMBARRIER: {
2366 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2367 TargetLowering::ArgListTy Args;
2368 std::pair<SDValue, SDValue> CallResult =
2369 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
2370 false, false, false, false, 0, CallingConv::C, false,
2371 /*isReturnValueUsed=*/true,
2372 DAG.getExternalSymbol("__sync_synchronize",
2373 TLI.getPointerTy()),
2375 Results.push_back(CallResult.second);
2378 // By default, atomic intrinsics are marked Legal and lowered. Targets
2379 // which don't support them directly, however, may want libcalls, in which
2380 // case they mark them Expand, and we get here.
2381 // FIXME: Unimplemented for now. Add libcalls.
2382 case ISD::ATOMIC_SWAP:
2383 case ISD::ATOMIC_LOAD_ADD:
2384 case ISD::ATOMIC_LOAD_SUB:
2385 case ISD::ATOMIC_LOAD_AND:
2386 case ISD::ATOMIC_LOAD_OR:
2387 case ISD::ATOMIC_LOAD_XOR:
2388 case ISD::ATOMIC_LOAD_NAND:
2389 case ISD::ATOMIC_LOAD_MIN:
2390 case ISD::ATOMIC_LOAD_MAX:
2391 case ISD::ATOMIC_LOAD_UMIN:
2392 case ISD::ATOMIC_LOAD_UMAX:
2393 case ISD::ATOMIC_CMP_SWAP: {
2394 assert (0 && "atomic intrinsic not lowered!");
2395 Results.push_back(Node->getOperand(0));
2397 case ISD::DYNAMIC_STACKALLOC:
2398 ExpandDYNAMIC_STACKALLOC(Node, Results);
2400 case ISD::MERGE_VALUES:
2401 for (unsigned i = 0; i < Node->getNumValues(); i++)
2402 Results.push_back(Node->getOperand(i));
2405 EVT VT = Node->getValueType(0);
2407 Results.push_back(DAG.getConstant(0, VT));
2409 assert(VT.isFloatingPoint() && "Unknown value type!");
2410 Results.push_back(DAG.getConstantFP(0, VT));
2415 // If this operation is not supported, lower it to 'abort()' call
2416 TargetLowering::ArgListTy Args;
2417 std::pair<SDValue, SDValue> CallResult =
2418 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
2419 false, false, false, false, 0, CallingConv::C, false,
2420 /*isReturnValueUsed=*/true,
2421 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
2423 Results.push_back(CallResult.second);
2427 case ISD::BIT_CONVERT:
2428 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2429 Node->getValueType(0), dl);
2430 Results.push_back(Tmp1);
2432 case ISD::FP_EXTEND:
2433 Tmp1 = EmitStackConvert(Node->getOperand(0),
2434 Node->getOperand(0).getValueType(),
2435 Node->getValueType(0), dl);
2436 Results.push_back(Tmp1);
2438 case ISD::SIGN_EXTEND_INREG: {
2439 // NOTE: we could fall back on load/store here too for targets without
2440 // SAR. However, it is doubtful that any exist.
2441 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2442 EVT VT = Node->getValueType(0);
2443 EVT ShiftAmountTy = TLI.getShiftAmountTy();
2446 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2447 ExtraVT.getScalarType().getSizeInBits();
2448 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
2449 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2450 Node->getOperand(0), ShiftCst);
2451 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2452 Results.push_back(Tmp1);
2455 case ISD::FP_ROUND_INREG: {
2456 // The only way we can lower this is to turn it into a TRUNCSTORE,
2457 // EXTLOAD pair, targetting a temporary location (a stack slot).
2459 // NOTE: there is a choice here between constantly creating new stack
2460 // slots and always reusing the same one. We currently always create
2461 // new ones, as reuse may inhibit scheduling.
2462 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2463 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2464 Node->getValueType(0), dl);
2465 Results.push_back(Tmp1);
2468 case ISD::SINT_TO_FP:
2469 case ISD::UINT_TO_FP:
2470 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2471 Node->getOperand(0), Node->getValueType(0), dl);
2472 Results.push_back(Tmp1);
2474 case ISD::FP_TO_UINT: {
2475 SDValue True, False;
2476 EVT VT = Node->getOperand(0).getValueType();
2477 EVT NVT = Node->getValueType(0);
2478 const uint64_t zero[] = {0, 0};
2479 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
2480 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2481 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2482 Tmp1 = DAG.getConstantFP(apf, VT);
2483 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2484 Node->getOperand(0),
2486 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
2487 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2488 DAG.getNode(ISD::FSUB, dl, VT,
2489 Node->getOperand(0), Tmp1));
2490 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2491 DAG.getConstant(x, NVT));
2492 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2493 Results.push_back(Tmp1);
2497 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2498 EVT VT = Node->getValueType(0);
2499 Tmp1 = Node->getOperand(0);
2500 Tmp2 = Node->getOperand(1);
2501 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0,
2503 // Increment the pointer, VAList, to the next vaarg
2504 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2505 DAG.getConstant(TLI.getTargetData()->
2506 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
2507 TLI.getPointerTy()));
2508 // Store the incremented VAList to the legalized pointer
2509 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0,
2511 // Load the actual argument out of the pointer VAList
2512 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0,
2514 Results.push_back(Results[0].getValue(1));
2518 // This defaults to loading a pointer from the input and storing it to the
2519 // output, returning the chain.
2520 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2521 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2522 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
2523 Node->getOperand(2), VS, 0, false, false, 0);
2524 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0,
2526 Results.push_back(Tmp1);
2529 case ISD::EXTRACT_VECTOR_ELT:
2530 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2531 // This must be an access of the only element. Return it.
2532 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
2533 Node->getOperand(0));
2535 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2536 Results.push_back(Tmp1);
2538 case ISD::EXTRACT_SUBVECTOR:
2539 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
2541 case ISD::CONCAT_VECTORS: {
2542 Results.push_back(ExpandVectorBuildThroughStack(Node));
2545 case ISD::SCALAR_TO_VECTOR:
2546 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
2548 case ISD::INSERT_VECTOR_ELT:
2549 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2550 Node->getOperand(1),
2551 Node->getOperand(2), dl));
2553 case ISD::VECTOR_SHUFFLE: {
2554 SmallVector<int, 8> Mask;
2555 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2557 EVT VT = Node->getValueType(0);
2558 EVT EltVT = VT.getVectorElementType();
2559 if (getTypeAction(EltVT) == Promote)
2560 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
2561 unsigned NumElems = VT.getVectorNumElements();
2562 SmallVector<SDValue, 8> Ops;
2563 for (unsigned i = 0; i != NumElems; ++i) {
2565 Ops.push_back(DAG.getUNDEF(EltVT));
2568 unsigned Idx = Mask[i];
2570 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2571 Node->getOperand(0),
2572 DAG.getIntPtrConstant(Idx)));
2574 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2575 Node->getOperand(1),
2576 DAG.getIntPtrConstant(Idx - NumElems)));
2578 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2579 Results.push_back(Tmp1);
2582 case ISD::EXTRACT_ELEMENT: {
2583 EVT OpTy = Node->getOperand(0).getValueType();
2584 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2586 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2587 DAG.getConstant(OpTy.getSizeInBits()/2,
2588 TLI.getShiftAmountTy()));
2589 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2592 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2593 Node->getOperand(0));
2595 Results.push_back(Tmp1);
2598 case ISD::STACKSAVE:
2599 // Expand to CopyFromReg if the target set
2600 // StackPointerRegisterToSaveRestore.
2601 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2602 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2603 Node->getValueType(0)));
2604 Results.push_back(Results[0].getValue(1));
2606 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
2607 Results.push_back(Node->getOperand(0));
2610 case ISD::STACKRESTORE:
2611 // Expand to CopyToReg if the target set
2612 // StackPointerRegisterToSaveRestore.
2613 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2614 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2615 Node->getOperand(1)));
2617 Results.push_back(Node->getOperand(0));
2620 case ISD::FCOPYSIGN:
2621 Results.push_back(ExpandFCOPYSIGN(Node));
2624 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2625 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2626 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2627 Node->getOperand(0));
2628 Results.push_back(Tmp1);
2631 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2632 EVT VT = Node->getValueType(0);
2633 Tmp1 = Node->getOperand(0);
2634 Tmp2 = DAG.getConstantFP(0.0, VT);
2635 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
2636 Tmp1, Tmp2, ISD::SETUGT);
2637 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2638 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
2639 Results.push_back(Tmp1);
2643 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2644 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
2647 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2648 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
2651 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2652 RTLIB::COS_F80, RTLIB::COS_PPCF128));
2655 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2656 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
2659 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2660 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
2663 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2664 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
2667 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2668 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
2671 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2672 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
2675 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2676 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
2679 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2680 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
2683 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2684 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
2687 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2688 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
2690 case ISD::FNEARBYINT:
2691 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2692 RTLIB::NEARBYINT_F64,
2693 RTLIB::NEARBYINT_F80,
2694 RTLIB::NEARBYINT_PPCF128));
2697 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2698 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
2701 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2702 RTLIB::POW_F80, RTLIB::POW_PPCF128));
2705 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2706 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
2709 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2710 RTLIB::REM_F80, RTLIB::REM_PPCF128));
2712 case ISD::FP16_TO_FP32:
2713 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2715 case ISD::FP32_TO_FP16:
2716 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2718 case ISD::ConstantFP: {
2719 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
2720 // Check to see if this FP immediate is already legal.
2721 // If this is a legal constant, turn it into a TargetConstantFP node.
2722 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
2723 Results.push_back(SDValue(Node, 0));
2725 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
2728 case ISD::EHSELECTION: {
2729 unsigned Reg = TLI.getExceptionSelectorRegister();
2730 assert(Reg && "Can't expand to unknown register!");
2731 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
2732 Node->getValueType(0)));
2733 Results.push_back(Results[0].getValue(1));
2736 case ISD::EXCEPTIONADDR: {
2737 unsigned Reg = TLI.getExceptionAddressRegister();
2738 assert(Reg && "Can't expand to unknown register!");
2739 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
2740 Node->getValueType(0)));
2741 Results.push_back(Results[0].getValue(1));
2745 EVT VT = Node->getValueType(0);
2746 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2747 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
2748 "Don't know how to expand this subtraction!");
2749 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
2750 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
2751 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2752 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
2757 EVT VT = Node->getValueType(0);
2758 SDVTList VTs = DAG.getVTList(VT, VT);
2759 bool isSigned = Node->getOpcode() == ISD::SREM;
2760 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
2761 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
2762 Tmp2 = Node->getOperand(0);
2763 Tmp3 = Node->getOperand(1);
2764 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
2765 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
2766 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
2768 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
2769 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
2770 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
2771 } else if (isSigned) {
2772 Tmp1 = ExpandIntLibCall(Node, true,
2774 RTLIB::SREM_I16, RTLIB::SREM_I32,
2775 RTLIB::SREM_I64, RTLIB::SREM_I128);
2777 Tmp1 = ExpandIntLibCall(Node, false,
2779 RTLIB::UREM_I16, RTLIB::UREM_I32,
2780 RTLIB::UREM_I64, RTLIB::UREM_I128);
2782 Results.push_back(Tmp1);
2787 bool isSigned = Node->getOpcode() == ISD::SDIV;
2788 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
2789 EVT VT = Node->getValueType(0);
2790 SDVTList VTs = DAG.getVTList(VT, VT);
2791 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
2792 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
2793 Node->getOperand(1));
2795 Tmp1 = ExpandIntLibCall(Node, true,
2797 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
2798 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
2800 Tmp1 = ExpandIntLibCall(Node, false,
2802 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
2803 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
2804 Results.push_back(Tmp1);
2809 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
2811 EVT VT = Node->getValueType(0);
2812 SDVTList VTs = DAG.getVTList(VT, VT);
2813 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
2814 "If this wasn't legal, it shouldn't have been created!");
2815 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
2816 Node->getOperand(1));
2817 Results.push_back(Tmp1.getValue(1));
2821 EVT VT = Node->getValueType(0);
2822 SDVTList VTs = DAG.getVTList(VT, VT);
2823 // See if multiply or divide can be lowered using two-result operations.
2824 // We just need the low half of the multiply; try both the signed
2825 // and unsigned forms. If the target supports both SMUL_LOHI and
2826 // UMUL_LOHI, form a preference by checking which forms of plain
2827 // MULH it supports.
2828 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2829 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2830 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2831 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
2832 unsigned OpToUse = 0;
2833 if (HasSMUL_LOHI && !HasMULHS) {
2834 OpToUse = ISD::SMUL_LOHI;
2835 } else if (HasUMUL_LOHI && !HasMULHU) {
2836 OpToUse = ISD::UMUL_LOHI;
2837 } else if (HasSMUL_LOHI) {
2838 OpToUse = ISD::SMUL_LOHI;
2839 } else if (HasUMUL_LOHI) {
2840 OpToUse = ISD::UMUL_LOHI;
2843 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
2844 Node->getOperand(1)));
2847 Tmp1 = ExpandIntLibCall(Node, false,
2849 RTLIB::MUL_I16, RTLIB::MUL_I32,
2850 RTLIB::MUL_I64, RTLIB::MUL_I128);
2851 Results.push_back(Tmp1);
2856 SDValue LHS = Node->getOperand(0);
2857 SDValue RHS = Node->getOperand(1);
2858 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
2859 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
2861 Results.push_back(Sum);
2862 EVT OType = Node->getValueType(1);
2864 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
2866 // LHSSign -> LHS >= 0
2867 // RHSSign -> RHS >= 0
2868 // SumSign -> Sum >= 0
2871 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
2873 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
2875 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2876 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
2877 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2878 Node->getOpcode() == ISD::SADDO ?
2879 ISD::SETEQ : ISD::SETNE);
2881 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2882 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
2884 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
2885 Results.push_back(Cmp);
2890 SDValue LHS = Node->getOperand(0);
2891 SDValue RHS = Node->getOperand(1);
2892 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
2893 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
2895 Results.push_back(Sum);
2896 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
2897 Node->getOpcode () == ISD::UADDO ?
2898 ISD::SETULT : ISD::SETUGT));
2903 EVT VT = Node->getValueType(0);
2904 SDValue LHS = Node->getOperand(0);
2905 SDValue RHS = Node->getOperand(1);
2908 static const unsigned Ops[2][3] =
2909 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
2910 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
2911 bool isSigned = Node->getOpcode() == ISD::SMULO;
2912 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
2913 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
2914 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
2915 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
2916 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
2918 TopHalf = BottomHalf.getValue(1);
2920 // FIXME: We should be able to fall back to a libcall with an illegal
2921 // type in some cases.
2922 // Also, we can fall back to a division in some cases, but that's a big
2923 // performance hit in the general case.
2924 assert(TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
2925 VT.getSizeInBits() * 2)) &&
2926 "Don't know how to expand this operation yet!");
2927 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
2928 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
2929 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
2930 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
2931 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
2932 DAG.getIntPtrConstant(0));
2933 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
2934 DAG.getIntPtrConstant(1));
2937 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, TLI.getShiftAmountTy());
2938 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
2939 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
2942 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
2943 DAG.getConstant(0, VT), ISD::SETNE);
2945 Results.push_back(BottomHalf);
2946 Results.push_back(TopHalf);
2949 case ISD::BUILD_PAIR: {
2950 EVT PairTy = Node->getValueType(0);
2951 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
2952 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
2953 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
2954 DAG.getConstant(PairTy.getSizeInBits()/2,
2955 TLI.getShiftAmountTy()));
2956 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
2960 Tmp1 = Node->getOperand(0);
2961 Tmp2 = Node->getOperand(1);
2962 Tmp3 = Node->getOperand(2);
2963 if (Tmp1.getOpcode() == ISD::SETCC) {
2964 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
2966 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2968 Tmp1 = DAG.getSelectCC(dl, Tmp1,
2969 DAG.getConstant(0, Tmp1.getValueType()),
2970 Tmp2, Tmp3, ISD::SETNE);
2972 Results.push_back(Tmp1);
2975 SDValue Chain = Node->getOperand(0);
2976 SDValue Table = Node->getOperand(1);
2977 SDValue Index = Node->getOperand(2);
2979 EVT PTy = TLI.getPointerTy();
2981 const TargetData &TD = *TLI.getTargetData();
2982 unsigned EntrySize =
2983 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
2985 Index = DAG.getNode(ISD::MUL, dl, PTy,
2986 Index, DAG.getConstant(EntrySize, PTy));
2987 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
2989 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
2990 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
2991 PseudoSourceValue::getJumpTable(), 0, MemVT,
2994 if (TM.getRelocationModel() == Reloc::PIC_) {
2995 // For PIC, the sequence is:
2996 // BRIND(load(Jumptable + index) + RelocBase)
2997 // RelocBase can be JumpTable, GOT or some sort of global base.
2998 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
2999 TLI.getPICJumpTableRelocBase(Table, DAG));
3001 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
3002 Results.push_back(Tmp1);
3006 // Expand brcond's setcc into its constituent parts and create a BR_CC
3008 Tmp1 = Node->getOperand(0);
3009 Tmp2 = Node->getOperand(1);
3010 if (Tmp2.getOpcode() == ISD::SETCC) {
3011 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
3012 Tmp1, Tmp2.getOperand(2),
3013 Tmp2.getOperand(0), Tmp2.getOperand(1),
3014 Node->getOperand(2));
3016 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
3017 DAG.getCondCode(ISD::SETNE), Tmp2,
3018 DAG.getConstant(0, Tmp2.getValueType()),
3019 Node->getOperand(2));
3021 Results.push_back(Tmp1);
3024 Tmp1 = Node->getOperand(0);
3025 Tmp2 = Node->getOperand(1);
3026 Tmp3 = Node->getOperand(2);
3027 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
3029 // If we expanded the SETCC into an AND/OR, return the new node
3030 if (Tmp2.getNode() == 0) {
3031 Results.push_back(Tmp1);
3035 // Otherwise, SETCC for the given comparison type must be completely
3036 // illegal; expand it into a SELECT_CC.
3037 EVT VT = Node->getValueType(0);
3038 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3039 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3040 Results.push_back(Tmp1);
3043 case ISD::SELECT_CC: {
3044 Tmp1 = Node->getOperand(0); // LHS
3045 Tmp2 = Node->getOperand(1); // RHS
3046 Tmp3 = Node->getOperand(2); // True
3047 Tmp4 = Node->getOperand(3); // False
3048 SDValue CC = Node->getOperand(4);
3050 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
3051 Tmp1, Tmp2, CC, dl);
3053 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3054 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3055 CC = DAG.getCondCode(ISD::SETNE);
3056 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3058 Results.push_back(Tmp1);
3062 Tmp1 = Node->getOperand(0); // Chain
3063 Tmp2 = Node->getOperand(2); // LHS
3064 Tmp3 = Node->getOperand(3); // RHS
3065 Tmp4 = Node->getOperand(1); // CC
3067 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
3068 Tmp2, Tmp3, Tmp4, dl);
3069 LastCALLSEQ_END = DAG.getEntryNode();
3071 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3072 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3073 Tmp4 = DAG.getCondCode(ISD::SETNE);
3074 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3075 Tmp3, Node->getOperand(4));
3076 Results.push_back(Tmp1);
3079 case ISD::GLOBAL_OFFSET_TABLE:
3080 case ISD::GlobalAddress:
3081 case ISD::GlobalTLSAddress:
3082 case ISD::ExternalSymbol:
3083 case ISD::ConstantPool:
3084 case ISD::JumpTable:
3085 case ISD::INTRINSIC_W_CHAIN:
3086 case ISD::INTRINSIC_WO_CHAIN:
3087 case ISD::INTRINSIC_VOID:
3088 // FIXME: Custom lowering for these operations shouldn't return null!
3089 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3090 Results.push_back(SDValue(Node, i));
3094 void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3095 SmallVectorImpl<SDValue> &Results) {
3096 EVT OVT = Node->getValueType(0);
3097 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3098 Node->getOpcode() == ISD::SINT_TO_FP ||
3099 Node->getOpcode() == ISD::SETCC) {
3100 OVT = Node->getOperand(0).getValueType();
3102 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3103 DebugLoc dl = Node->getDebugLoc();
3104 SDValue Tmp1, Tmp2, Tmp3;
3105 switch (Node->getOpcode()) {
3109 // Zero extend the argument.
3110 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3111 // Perform the larger operation.
3112 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
3113 if (Node->getOpcode() == ISD::CTTZ) {
3114 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3115 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
3116 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3118 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3119 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3120 } else if (Node->getOpcode() == ISD::CTLZ) {
3121 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3122 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3123 DAG.getConstant(NVT.getSizeInBits() -
3124 OVT.getSizeInBits(), NVT));
3126 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
3129 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3130 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3131 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3132 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3133 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3134 Results.push_back(Tmp1);
3137 case ISD::FP_TO_UINT:
3138 case ISD::FP_TO_SINT:
3139 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3140 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3141 Results.push_back(Tmp1);
3143 case ISD::UINT_TO_FP:
3144 case ISD::SINT_TO_FP:
3145 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3146 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3147 Results.push_back(Tmp1);
3152 unsigned ExtOp, TruncOp;
3153 if (OVT.isVector()) {
3154 ExtOp = ISD::BIT_CONVERT;
3155 TruncOp = ISD::BIT_CONVERT;
3157 assert(OVT.isInteger() && "Cannot promote logic operation");
3158 ExtOp = ISD::ANY_EXTEND;
3159 TruncOp = ISD::TRUNCATE;
3161 // Promote each of the values to the new type.
3162 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3163 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3164 // Perform the larger operation, then convert back
3165 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3166 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
3170 unsigned ExtOp, TruncOp;
3171 if (Node->getValueType(0).isVector()) {
3172 ExtOp = ISD::BIT_CONVERT;
3173 TruncOp = ISD::BIT_CONVERT;
3174 } else if (Node->getValueType(0).isInteger()) {
3175 ExtOp = ISD::ANY_EXTEND;
3176 TruncOp = ISD::TRUNCATE;
3178 ExtOp = ISD::FP_EXTEND;
3179 TruncOp = ISD::FP_ROUND;
3181 Tmp1 = Node->getOperand(0);
3182 // Promote each of the values to the new type.
3183 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3184 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3185 // Perform the larger operation, then round down.
3186 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3187 if (TruncOp != ISD::FP_ROUND)
3188 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
3190 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
3191 DAG.getIntPtrConstant(0));
3192 Results.push_back(Tmp1);
3195 case ISD::VECTOR_SHUFFLE: {
3196 SmallVector<int, 8> Mask;
3197 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3199 // Cast the two input vectors.
3200 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
3201 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1));
3203 // Convert the shuffle mask to the right # elements.
3204 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
3205 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1);
3206 Results.push_back(Tmp1);
3210 unsigned ExtOp = ISD::FP_EXTEND;
3211 if (NVT.isInteger()) {
3212 ISD::CondCode CCCode =
3213 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3214 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3216 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3217 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3218 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3219 Tmp1, Tmp2, Node->getOperand(2)));
3225 // SelectionDAG::Legalize - This is the entry point for the file.
3227 void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
3228 /// run - This is the main entry point to this class.
3230 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();