1 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Target/TargetLowering.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
36 ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
37 cl::desc("Pop up a window to show dags before legalize"));
39 static const bool ViewLegalizeDAGs = 0;
42 //===----------------------------------------------------------------------===//
43 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44 /// hacks on it until the target machine can handle it. This involves
45 /// eliminating value sizes the machine cannot handle (promoting small sizes to
46 /// large sizes or splitting up large values into small values) as well as
47 /// eliminating operations the machine cannot handle.
49 /// This code also does a small amount of optimization and recognition of idioms
50 /// as part of its processing. For example, if a target does not support a
51 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52 /// will attempt merge setcc and brc instructions into brcc's.
55 class VISIBILITY_HIDDEN SelectionDAGLegalize {
59 // Libcall insertion helpers.
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
64 SDOperand LastCALLSEQ_END;
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
72 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
74 Expand // Try to expand this to other ops, otherwise use a libcall.
77 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
80 TargetLowering::ValueTypeActionImpl ValueTypeActions;
82 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
85 DenseMap<SDOperand, SDOperand> LegalizedNodes;
87 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
90 DenseMap<SDOperand, SDOperand> PromotedNodes;
92 /// ExpandedNodes - For nodes that need to be expanded this map indicates
93 /// which which operands are the expanded version of the input. This allows
94 /// us to avoid expanding the same node more than once.
95 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
97 /// SplitNodes - For vector nodes that need to be split, this map indicates
98 /// which which operands are the split version of the input. This allows us
99 /// to avoid splitting the same node more than once.
100 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
104 /// processed to the result.
105 std::map<SDOperand, SDOperand> ScalarizedNodes;
107 void AddLegalizedOperand(SDOperand From, SDOperand To) {
108 LegalizedNodes.insert(std::make_pair(From, To));
109 // If someone requests legalization of the new node, return itself.
111 LegalizedNodes.insert(std::make_pair(To, To));
113 void AddPromotedOperand(SDOperand From, SDOperand To) {
114 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
115 assert(isNew && "Got into the map somehow?");
116 // If someone requests legalization of the new node, return itself.
117 LegalizedNodes.insert(std::make_pair(To, To));
122 SelectionDAGLegalize(SelectionDAG &DAG);
124 /// getTypeAction - Return how we should legalize values of this type, either
125 /// it is already legal or we need to expand it into multiple registers of
126 /// smaller integer type, or we need to promote it to a larger type.
127 LegalizeAction getTypeAction(MVT::ValueType VT) const {
128 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
131 /// isTypeLegal - Return true if this type is legal on this target.
133 bool isTypeLegal(MVT::ValueType VT) const {
134 return getTypeAction(VT) == Legal;
140 /// HandleOp - Legalize, Promote, or Expand the specified operand as
141 /// appropriate for its type.
142 void HandleOp(SDOperand Op);
144 /// LegalizeOp - We know that the specified value has a legal type.
145 /// Recursively ensure that the operands have legal types, then return the
147 SDOperand LegalizeOp(SDOperand O);
149 /// PromoteOp - Given an operation that produces a value in an invalid type,
150 /// promote it to compute the value into a larger type. The produced value
151 /// will have the correct bits for the low portion of the register, but no
152 /// guarantee is made about the top bits: it may be zero, sign-extended, or
154 SDOperand PromoteOp(SDOperand O);
156 /// ExpandOp - Expand the specified SDOperand into its two component pieces
157 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
158 /// the LegalizeNodes map is filled in for any results that are not expanded,
159 /// the ExpandedNodes map is filled in for any results that are expanded, and
160 /// the Lo/Hi values are returned. This applies to integer types and Vector
162 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164 /// SplitVectorOp - Given an operand of vector type, break it down into
165 /// two smaller values.
166 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
168 /// ScalarizeVectorOp - Given an operand of single-element vector type
169 /// (e.g. v1f32), convert it into the equivalent operation that returns a
170 /// scalar (e.g. f32) value.
171 SDOperand ScalarizeVectorOp(SDOperand O);
173 /// isShuffleLegal - Return true if a vector shuffle is legal with the
174 /// specified mask and type. Targets can specify exactly which masks they
175 /// support and the code generator is tasked with not creating illegal masks.
177 /// Note that this will also return true for shuffles that are promoted to a
180 /// If this is a legal shuffle, this method returns the (possibly promoted)
181 /// build_vector Mask. If it's not a legal shuffle, it returns null.
182 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
184 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
185 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
187 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189 SDOperand CreateStackTemporary(MVT::ValueType VT);
191 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
193 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
196 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
197 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
198 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
199 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 MVT::ValueType DestVT);
202 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
207 SDOperand ExpandBSWAP(SDOperand Op);
208 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
209 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
211 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
212 SDOperand &Lo, SDOperand &Hi);
214 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
215 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
217 SDOperand getIntPtrConstant(uint64_t Val) {
218 return DAG.getConstant(Val, TLI.getPointerTy());
223 /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
224 /// specified mask and type. Targets can specify exactly which masks they
225 /// support and the code generator is tasked with not creating illegal masks.
227 /// Note that this will also return true for shuffles that are promoted to a
229 SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
230 SDOperand Mask) const {
231 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
233 case TargetLowering::Legal:
234 case TargetLowering::Custom:
236 case TargetLowering::Promote: {
237 // If this is promoted to a different type, convert the shuffle mask and
238 // ask if it is legal in the promoted type!
239 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
241 // If we changed # elements, change the shuffle mask.
242 unsigned NumEltsGrowth =
243 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
244 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
245 if (NumEltsGrowth > 1) {
246 // Renumber the elements.
247 SmallVector<SDOperand, 8> Ops;
248 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
249 SDOperand InOp = Mask.getOperand(i);
250 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
251 if (InOp.getOpcode() == ISD::UNDEF)
252 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
254 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
255 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
259 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
265 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
268 SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
269 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
270 ValueTypeActions(TLI.getValueTypeActions()) {
271 assert(MVT::LAST_VALUETYPE <= 32 &&
272 "Too many value types for ValueTypeActions to hold!");
275 /// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
276 /// contains all of a nodes operands before it contains the node.
277 static void ComputeTopDownOrdering(SelectionDAG &DAG,
278 SmallVector<SDNode*, 64> &Order) {
280 DenseMap<SDNode*, unsigned> Visited;
281 std::vector<SDNode*> Worklist;
282 Worklist.reserve(128);
284 // Compute ordering from all of the leaves in the graphs, those (like the
285 // entry node) that have no operands.
286 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
287 E = DAG.allnodes_end(); I != E; ++I) {
288 if (I->getNumOperands() == 0) {
290 Worklist.push_back(I);
294 while (!Worklist.empty()) {
295 SDNode *N = Worklist.back();
298 if (++Visited[N] != N->getNumOperands())
299 continue; // Haven't visited all operands yet
303 // Now that we have N in, add anything that uses it if all of their operands
305 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
307 Worklist.push_back(*UI);
310 assert(Order.size() == Visited.size() &&
312 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
313 "Error: DAG is cyclic!");
317 void SelectionDAGLegalize::LegalizeDAG() {
318 LastCALLSEQ_END = DAG.getEntryNode();
319 IsLegalizingCall = false;
321 // The legalize process is inherently a bottom-up recursive process (users
322 // legalize their uses before themselves). Given infinite stack space, we
323 // could just start legalizing on the root and traverse the whole graph. In
324 // practice however, this causes us to run out of stack space on large basic
325 // blocks. To avoid this problem, compute an ordering of the nodes where each
326 // node is only legalized after all of its operands are legalized.
327 SmallVector<SDNode*, 64> Order;
328 ComputeTopDownOrdering(DAG, Order);
330 for (unsigned i = 0, e = Order.size(); i != e; ++i)
331 HandleOp(SDOperand(Order[i], 0));
333 // Finally, it's possible the root changed. Get the new root.
334 SDOperand OldRoot = DAG.getRoot();
335 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
336 DAG.setRoot(LegalizedNodes[OldRoot]);
338 ExpandedNodes.clear();
339 LegalizedNodes.clear();
340 PromotedNodes.clear();
342 ScalarizedNodes.clear();
344 // Remove dead nodes now.
345 DAG.RemoveDeadNodes();
349 /// FindCallEndFromCallStart - Given a chained node that is part of a call
350 /// sequence, find the CALLSEQ_END node that terminates the call sequence.
351 static SDNode *FindCallEndFromCallStart(SDNode *Node) {
352 if (Node->getOpcode() == ISD::CALLSEQ_END)
354 if (Node->use_empty())
355 return 0; // No CallSeqEnd
357 // The chain is usually at the end.
358 SDOperand TheChain(Node, Node->getNumValues()-1);
359 if (TheChain.getValueType() != MVT::Other) {
360 // Sometimes it's at the beginning.
361 TheChain = SDOperand(Node, 0);
362 if (TheChain.getValueType() != MVT::Other) {
363 // Otherwise, hunt for it.
364 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
365 if (Node->getValueType(i) == MVT::Other) {
366 TheChain = SDOperand(Node, i);
370 // Otherwise, we walked into a node without a chain.
371 if (TheChain.getValueType() != MVT::Other)
376 for (SDNode::use_iterator UI = Node->use_begin(),
377 E = Node->use_end(); UI != E; ++UI) {
379 // Make sure to only follow users of our token chain.
381 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
382 if (User->getOperand(i) == TheChain)
383 if (SDNode *Result = FindCallEndFromCallStart(User))
389 /// FindCallStartFromCallEnd - Given a chained node that is part of a call
390 /// sequence, find the CALLSEQ_START node that initiates the call sequence.
391 static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
392 assert(Node && "Didn't find callseq_start for a call??");
393 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
395 assert(Node->getOperand(0).getValueType() == MVT::Other &&
396 "Node doesn't have a token chain argument!");
397 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
400 /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
401 /// see if any uses can reach Dest. If no dest operands can get to dest,
402 /// legalize them, legalize ourself, and return false, otherwise, return true.
404 /// Keep track of the nodes we fine that actually do lead to Dest in
405 /// NodesLeadingTo. This avoids retraversing them exponential number of times.
407 bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
408 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
409 if (N == Dest) return true; // N certainly leads to Dest :)
411 // If we've already processed this node and it does lead to Dest, there is no
412 // need to reprocess it.
413 if (NodesLeadingTo.count(N)) return true;
415 // If the first result of this node has been already legalized, then it cannot
417 switch (getTypeAction(N->getValueType(0))) {
419 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
422 if (PromotedNodes.count(SDOperand(N, 0))) return false;
425 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
429 // Okay, this node has not already been legalized. Check and legalize all
430 // operands. If none lead to Dest, then we can legalize this node.
431 bool OperandsLeadToDest = false;
432 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
433 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
434 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
436 if (OperandsLeadToDest) {
437 NodesLeadingTo.insert(N);
441 // Okay, this node looks safe, legalize it and return false.
442 HandleOp(SDOperand(N, 0));
446 /// HandleOp - Legalize, Promote, or Expand the specified operand as
447 /// appropriate for its type.
448 void SelectionDAGLegalize::HandleOp(SDOperand Op) {
449 MVT::ValueType VT = Op.getValueType();
450 switch (getTypeAction(VT)) {
451 default: assert(0 && "Bad type action!");
452 case Legal: (void)LegalizeOp(Op); break;
453 case Promote: (void)PromoteOp(Op); break;
455 if (!MVT::isVector(VT)) {
456 // If this is an illegal scalar, expand it into its two component
460 } else if (MVT::getVectorNumElements(VT) == 1) {
461 // If this is an illegal single element vector, convert it to a
463 (void)ScalarizeVectorOp(Op);
465 // Otherwise, this is an illegal multiple element vector.
466 // Split it in half and legalize both parts.
468 SplitVectorOp(Op, X, Y);
474 /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
475 /// a load from the constant pool.
476 static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
477 SelectionDAG &DAG, TargetLowering &TLI) {
480 // If a FP immediate is precise when represented as a float and if the
481 // target can do an extending load from float to double, we put it into
482 // the constant pool as a float, even if it's is statically typed as a
484 MVT::ValueType VT = CFP->getValueType(0);
485 bool isDouble = VT == MVT::f64;
486 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
487 Type::FloatTy, CFP->getValue());
489 double Val = LLVMC->getValue();
491 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
492 : DAG.getConstant(FloatToBits(Val), MVT::i32);
495 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
496 // Only do this if the target has a native EXTLOAD instruction from f32.
497 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
498 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
503 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
505 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
506 CPIdx, NULL, 0, MVT::f32);
508 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
513 /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
516 SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
517 SelectionDAG &DAG, TargetLowering &TLI) {
518 MVT::ValueType VT = Node->getValueType(0);
519 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
520 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
521 "fcopysign expansion only supported for f32 and f64");
522 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
524 // First get the sign bit of second operand.
525 SDOperand Mask1 = (SrcVT == MVT::f64)
526 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
527 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
528 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
529 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
530 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
531 // Shift right or sign-extend it if the two operands have different types.
532 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
534 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
535 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
536 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
537 } else if (SizeDiff < 0)
538 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
540 // Clear the sign bit of first operand.
541 SDOperand Mask2 = (VT == MVT::f64)
542 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
543 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
544 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
545 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
546 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
548 // Or the value with the sign bit.
549 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
553 /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
555 SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
556 TargetLowering &TLI) {
557 assert(MVT::isInteger(ST->getStoredVT()) &&
558 "Non integer unaligned stores not implemented.");
559 int SVOffset = ST->getSrcValueOffset();
560 SDOperand Chain = ST->getChain();
561 SDOperand Ptr = ST->getBasePtr();
562 SDOperand Val = ST->getValue();
563 MVT::ValueType VT = Val.getValueType();
564 // Get the half-size VT
565 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
566 int NumBits = MVT::getSizeInBits(NewStoredVT);
567 int Alignment = ST->getAlignment();
568 int IncrementSize = NumBits / 8;
570 // Divide the stored value in two parts.
571 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
573 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
575 // Store the two parts
576 SDOperand Store1, Store2;
577 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
578 ST->getSrcValue(), SVOffset, NewStoredVT,
579 ST->isVolatile(), Alignment);
580 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
581 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
582 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
583 ST->getSrcValue(), SVOffset + IncrementSize,
584 NewStoredVT, ST->isVolatile(), Alignment);
586 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
589 /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
591 SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
592 TargetLowering &TLI) {
593 assert(MVT::isInteger(LD->getLoadedVT()) &&
594 "Non integer unaligned loads not implemented.");
595 int SVOffset = LD->getSrcValueOffset();
596 SDOperand Chain = LD->getChain();
597 SDOperand Ptr = LD->getBasePtr();
598 MVT::ValueType VT = LD->getValueType(0);
599 MVT::ValueType NewLoadedVT = LD->getLoadedVT() - 1;
600 int NumBits = MVT::getSizeInBits(NewLoadedVT);
601 int Alignment = LD->getAlignment();
602 int IncrementSize = NumBits / 8;
603 ISD::LoadExtType HiExtType = LD->getExtensionType();
605 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
606 if (HiExtType == ISD::NON_EXTLOAD)
607 HiExtType = ISD::ZEXTLOAD;
609 // Load the value in two parts
611 if (TLI.isLittleEndian()) {
612 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
613 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
616 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
617 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
620 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
621 NewLoadedVT,LD->isVolatile(), Alignment);
622 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
623 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
624 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
625 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
629 // aggregate the two parts
630 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
631 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
632 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
634 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
637 SDOperand Ops[] = { Result, TF };
638 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
641 /// LegalizeOp - We know that the specified value has a legal type, and
642 /// that its operands are legal. Now ensure that the operation itself
643 /// is legal, recursively ensuring that the operands' operations remain
645 SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
646 assert(isTypeLegal(Op.getValueType()) &&
647 "Caller should expand or promote operands that are not legal!");
648 SDNode *Node = Op.Val;
650 // If this operation defines any values that cannot be represented in a
651 // register on this target, make sure to expand or promote them.
652 if (Node->getNumValues() > 1) {
653 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
654 if (getTypeAction(Node->getValueType(i)) != Legal) {
655 HandleOp(Op.getValue(i));
656 assert(LegalizedNodes.count(Op) &&
657 "Handling didn't add legal operands!");
658 return LegalizedNodes[Op];
662 // Note that LegalizeOp may be reentered even from single-use nodes, which
663 // means that we always must cache transformed nodes.
664 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
665 if (I != LegalizedNodes.end()) return I->second;
667 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
668 SDOperand Result = Op;
669 bool isCustom = false;
671 switch (Node->getOpcode()) {
672 case ISD::FrameIndex:
673 case ISD::EntryToken:
675 case ISD::BasicBlock:
676 case ISD::TargetFrameIndex:
677 case ISD::TargetJumpTable:
678 case ISD::TargetConstant:
679 case ISD::TargetConstantFP:
680 case ISD::TargetConstantPool:
681 case ISD::TargetGlobalAddress:
682 case ISD::TargetGlobalTLSAddress:
683 case ISD::TargetExternalSymbol:
688 // Primitives must all be legal.
689 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
690 "This must be legal!");
693 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
694 // If this is a target node, legalize it by legalizing the operands then
695 // passing it through.
696 SmallVector<SDOperand, 8> Ops;
697 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
698 Ops.push_back(LegalizeOp(Node->getOperand(i)));
700 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
702 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
703 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
704 return Result.getValue(Op.ResNo);
706 // Otherwise this is an unhandled builtin node. splat.
708 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
710 assert(0 && "Do not know how to legalize this operator!");
712 case ISD::GLOBAL_OFFSET_TABLE:
713 case ISD::GlobalAddress:
714 case ISD::GlobalTLSAddress:
715 case ISD::ExternalSymbol:
716 case ISD::ConstantPool:
717 case ISD::JumpTable: // Nothing to do.
718 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
719 default: assert(0 && "This action is not supported yet!");
720 case TargetLowering::Custom:
721 Tmp1 = TLI.LowerOperation(Op, DAG);
722 if (Tmp1.Val) Result = Tmp1;
723 // FALLTHROUGH if the target doesn't want to lower this op after all.
724 case TargetLowering::Legal:
729 case ISD::RETURNADDR:
730 case ISD::FRAME_TO_ARGS_OFFSET:
731 // The only option for these nodes is to custom lower them. If the target
732 // does not custom lower them, then return zero.
733 Tmp1 = TLI.LowerOperation(Op, DAG);
737 Result = DAG.getConstant(0, TLI.getPointerTy());
739 case ISD::EXCEPTIONADDR: {
740 Tmp1 = LegalizeOp(Node->getOperand(0));
741 MVT::ValueType VT = Node->getValueType(0);
742 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Expand: {
745 unsigned Reg = TLI.getExceptionAddressRegister();
746 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
749 case TargetLowering::Custom:
750 Result = TLI.LowerOperation(Op, DAG);
751 if (Result.Val) break;
753 case TargetLowering::Legal: {
754 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
755 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
756 Ops, 2).getValue(Op.ResNo);
762 case ISD::EHSELECTION: {
763 Tmp1 = LegalizeOp(Node->getOperand(0));
764 Tmp2 = LegalizeOp(Node->getOperand(1));
765 MVT::ValueType VT = Node->getValueType(0);
766 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
767 default: assert(0 && "This action is not supported yet!");
768 case TargetLowering::Expand: {
769 unsigned Reg = TLI.getExceptionSelectorRegister();
770 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
773 case TargetLowering::Custom:
774 Result = TLI.LowerOperation(Op, DAG);
775 if (Result.Val) break;
777 case TargetLowering::Legal: {
778 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
779 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
780 Ops, 2).getValue(Op.ResNo);
786 case ISD::EH_RETURN: {
787 MVT::ValueType VT = Node->getValueType(0);
788 // The only "good" option for this node is to custom lower it.
789 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
790 default: assert(0 && "This action is not supported at all!");
791 case TargetLowering::Custom:
792 Result = TLI.LowerOperation(Op, DAG);
793 if (Result.Val) break;
795 case TargetLowering::Legal:
796 // Target does not know, how to lower this, lower to noop
797 Result = LegalizeOp(Node->getOperand(0));
802 case ISD::AssertSext:
803 case ISD::AssertZext:
804 Tmp1 = LegalizeOp(Node->getOperand(0));
805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
807 case ISD::MERGE_VALUES:
808 // Legalize eliminates MERGE_VALUES nodes.
809 Result = Node->getOperand(Op.ResNo);
811 case ISD::CopyFromReg:
812 Tmp1 = LegalizeOp(Node->getOperand(0));
813 Result = Op.getValue(0);
814 if (Node->getNumValues() == 2) {
815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
817 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
818 if (Node->getNumOperands() == 3) {
819 Tmp2 = LegalizeOp(Node->getOperand(2));
820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
822 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
824 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
826 // Since CopyFromReg produces two values, make sure to remember that we
827 // legalized both of them.
828 AddLegalizedOperand(Op.getValue(0), Result);
829 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
830 return Result.getValue(Op.ResNo);
832 MVT::ValueType VT = Op.getValueType();
833 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
834 default: assert(0 && "This action is not supported yet!");
835 case TargetLowering::Expand:
836 if (MVT::isInteger(VT))
837 Result = DAG.getConstant(0, VT);
838 else if (MVT::isFloatingPoint(VT))
839 Result = DAG.getConstantFP(0, VT);
841 assert(0 && "Unknown value type!");
843 case TargetLowering::Legal:
849 case ISD::INTRINSIC_W_CHAIN:
850 case ISD::INTRINSIC_WO_CHAIN:
851 case ISD::INTRINSIC_VOID: {
852 SmallVector<SDOperand, 8> Ops;
853 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
854 Ops.push_back(LegalizeOp(Node->getOperand(i)));
855 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
857 // Allow the target to custom lower its intrinsics if it wants to.
858 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
859 TargetLowering::Custom) {
860 Tmp3 = TLI.LowerOperation(Result, DAG);
861 if (Tmp3.Val) Result = Tmp3;
864 if (Result.Val->getNumValues() == 1) break;
866 // Must have return value and chain result.
867 assert(Result.Val->getNumValues() == 2 &&
868 "Cannot return more than two values!");
870 // Since loads produce two values, make sure to remember that we
871 // legalized both of them.
872 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
873 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
874 return Result.getValue(Op.ResNo);
878 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
881 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
882 case TargetLowering::Promote:
883 default: assert(0 && "This action is not supported yet!");
884 case TargetLowering::Expand: {
885 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
886 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
887 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
889 if (MMI && (useDEBUG_LOC || useLABEL)) {
890 const std::string &FName =
891 cast<StringSDNode>(Node->getOperand(3))->getValue();
892 const std::string &DirName =
893 cast<StringSDNode>(Node->getOperand(4))->getValue();
894 unsigned SrcFile = MMI->RecordSource(DirName, FName);
896 SmallVector<SDOperand, 8> Ops;
897 Ops.push_back(Tmp1); // chain
898 SDOperand LineOp = Node->getOperand(1);
899 SDOperand ColOp = Node->getOperand(2);
902 Ops.push_back(LineOp); // line #
903 Ops.push_back(ColOp); // col #
904 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
905 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
907 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
908 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
909 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
910 Ops.push_back(DAG.getConstant(ID, MVT::i32));
911 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
914 Result = Tmp1; // chain
918 case TargetLowering::Legal:
919 if (Tmp1 != Node->getOperand(0) ||
920 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
921 SmallVector<SDOperand, 8> Ops;
923 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
924 Ops.push_back(Node->getOperand(1)); // line # must be legal.
925 Ops.push_back(Node->getOperand(2)); // col # must be legal.
927 // Otherwise promote them.
928 Ops.push_back(PromoteOp(Node->getOperand(1)));
929 Ops.push_back(PromoteOp(Node->getOperand(2)));
931 Ops.push_back(Node->getOperand(3)); // filename must be legal.
932 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
933 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
940 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
941 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
942 default: assert(0 && "This action is not supported yet!");
943 case TargetLowering::Legal:
944 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
945 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
946 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
947 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
954 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
955 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
956 default: assert(0 && "This action is not supported yet!");
957 case TargetLowering::Legal:
958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
959 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
960 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
962 case TargetLowering::Expand:
963 Result = LegalizeOp(Node->getOperand(0));
968 case ISD::Constant: {
969 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
971 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
973 // We know we don't need to expand constants here, constants only have one
974 // value and we check that it is fine above.
976 if (opAction == TargetLowering::Custom) {
977 Tmp1 = TLI.LowerOperation(Result, DAG);
983 case ISD::ConstantFP: {
984 // Spill FP immediates to the constant pool if the target cannot directly
985 // codegen them. Targets often have some immediate values that can be
986 // efficiently generated into an FP register without a load. We explicitly
987 // leave these constants as ConstantFP nodes for the target to deal with.
988 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
990 // Check to see if this FP immediate is already legal.
991 bool isLegal = false;
992 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
993 E = TLI.legal_fpimm_end(); I != E; ++I)
994 if (CFP->isExactlyValue(*I)) {
999 // If this is a legal constant, turn it into a TargetConstantFP node.
1001 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
1005 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1006 default: assert(0 && "This action is not supported yet!");
1007 case TargetLowering::Custom:
1008 Tmp3 = TLI.LowerOperation(Result, DAG);
1014 case TargetLowering::Expand:
1015 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1019 case ISD::TokenFactor:
1020 if (Node->getNumOperands() == 2) {
1021 Tmp1 = LegalizeOp(Node->getOperand(0));
1022 Tmp2 = LegalizeOp(Node->getOperand(1));
1023 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1024 } else if (Node->getNumOperands() == 3) {
1025 Tmp1 = LegalizeOp(Node->getOperand(0));
1026 Tmp2 = LegalizeOp(Node->getOperand(1));
1027 Tmp3 = LegalizeOp(Node->getOperand(2));
1028 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1030 SmallVector<SDOperand, 8> Ops;
1031 // Legalize the operands.
1032 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1033 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1034 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1038 case ISD::FORMAL_ARGUMENTS:
1040 // The only option for this is to custom lower it.
1041 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1042 assert(Tmp3.Val && "Target didn't custom lower this node!");
1043 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1044 "Lowering call/formal_arguments produced unexpected # results!");
1046 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1047 // remember that we legalized all of them, so it doesn't get relegalized.
1048 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1049 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1052 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1055 case ISD::EXTRACT_SUBREG: {
1056 Tmp1 = LegalizeOp(Node->getOperand(0));
1057 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1058 assert(idx && "Operand must be a constant");
1059 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1063 case ISD::INSERT_SUBREG: {
1064 Tmp1 = LegalizeOp(Node->getOperand(0));
1065 Tmp2 = LegalizeOp(Node->getOperand(1));
1066 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1067 assert(idx && "Operand must be a constant");
1068 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1072 case ISD::BUILD_VECTOR:
1073 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1074 default: assert(0 && "This action is not supported yet!");
1075 case TargetLowering::Custom:
1076 Tmp3 = TLI.LowerOperation(Result, DAG);
1082 case TargetLowering::Expand:
1083 Result = ExpandBUILD_VECTOR(Result.Val);
1087 case ISD::INSERT_VECTOR_ELT:
1088 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1089 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1090 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1091 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1093 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1094 Node->getValueType(0))) {
1095 default: assert(0 && "This action is not supported yet!");
1096 case TargetLowering::Legal:
1098 case TargetLowering::Custom:
1099 Tmp3 = TLI.LowerOperation(Result, DAG);
1105 case TargetLowering::Expand: {
1106 // If the insert index is a constant, codegen this as a scalar_to_vector,
1107 // then a shuffle that inserts it into the right position in the vector.
1108 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1109 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1110 Tmp1.getValueType(), Tmp2);
1112 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1113 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1114 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1116 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1117 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1119 SmallVector<SDOperand, 8> ShufOps;
1120 for (unsigned i = 0; i != NumElts; ++i) {
1121 if (i != InsertPos->getValue())
1122 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1124 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1126 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1127 &ShufOps[0], ShufOps.size());
1129 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1130 Tmp1, ScVec, ShufMask);
1131 Result = LegalizeOp(Result);
1135 // If the target doesn't support this, we have to spill the input vector
1136 // to a temporary stack slot, update the element, then reload it. This is
1137 // badness. We could also load the value into a vector register (either
1138 // with a "move to register" or "extload into register" instruction, then
1139 // permute it into place, if the idx is a constant and if the idx is
1140 // supported by the target.
1141 MVT::ValueType VT = Tmp1.getValueType();
1142 MVT::ValueType EltVT = Tmp2.getValueType();
1143 MVT::ValueType IdxVT = Tmp3.getValueType();
1144 MVT::ValueType PtrVT = TLI.getPointerTy();
1145 SDOperand StackPtr = CreateStackTemporary(VT);
1146 // Store the vector.
1147 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
1149 // Truncate or zero extend offset to target pointer type.
1150 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1151 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1152 // Add the offset to the index.
1153 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1154 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1155 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1156 // Store the scalar value.
1157 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
1158 // Load the updated vector.
1159 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
1164 case ISD::SCALAR_TO_VECTOR:
1165 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1166 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1170 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1171 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1172 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1173 Node->getValueType(0))) {
1174 default: assert(0 && "This action is not supported yet!");
1175 case TargetLowering::Legal:
1177 case TargetLowering::Custom:
1178 Tmp3 = TLI.LowerOperation(Result, DAG);
1184 case TargetLowering::Expand:
1185 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1189 case ISD::VECTOR_SHUFFLE:
1190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1191 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1194 // Allow targets to custom lower the SHUFFLEs they support.
1195 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1196 default: assert(0 && "Unknown operation action!");
1197 case TargetLowering::Legal:
1198 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1199 "vector shuffle should not be created if not legal!");
1201 case TargetLowering::Custom:
1202 Tmp3 = TLI.LowerOperation(Result, DAG);
1208 case TargetLowering::Expand: {
1209 MVT::ValueType VT = Node->getValueType(0);
1210 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1211 MVT::ValueType PtrVT = TLI.getPointerTy();
1212 SDOperand Mask = Node->getOperand(2);
1213 unsigned NumElems = Mask.getNumOperands();
1214 SmallVector<SDOperand,8> Ops;
1215 for (unsigned i = 0; i != NumElems; ++i) {
1216 SDOperand Arg = Mask.getOperand(i);
1217 if (Arg.getOpcode() == ISD::UNDEF) {
1218 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1220 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1221 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1223 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1224 DAG.getConstant(Idx, PtrVT)));
1226 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1227 DAG.getConstant(Idx - NumElems, PtrVT)));
1230 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1233 case TargetLowering::Promote: {
1234 // Change base type to a different vector type.
1235 MVT::ValueType OVT = Node->getValueType(0);
1236 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1238 // Cast the two input vectors.
1239 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1240 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1242 // Convert the shuffle mask to the right # elements.
1243 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1244 assert(Tmp3.Val && "Shuffle not legal?");
1245 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1246 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1252 case ISD::EXTRACT_VECTOR_ELT:
1253 Tmp1 = Node->getOperand(0);
1254 Tmp2 = LegalizeOp(Node->getOperand(1));
1255 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1256 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1259 case ISD::EXTRACT_SUBVECTOR:
1260 Tmp1 = Node->getOperand(0);
1261 Tmp2 = LegalizeOp(Node->getOperand(1));
1262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1263 Result = ExpandEXTRACT_SUBVECTOR(Result);
1266 case ISD::CALLSEQ_START: {
1267 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1269 // Recursively Legalize all of the inputs of the call end that do not lead
1270 // to this call start. This ensures that any libcalls that need be inserted
1271 // are inserted *before* the CALLSEQ_START.
1272 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1273 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1274 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1278 // Now that we legalized all of the inputs (which may have inserted
1279 // libcalls) create the new CALLSEQ_START node.
1280 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1282 // Merge in the last call, to ensure that this call start after the last
1284 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1285 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1286 Tmp1 = LegalizeOp(Tmp1);
1289 // Do not try to legalize the target-specific arguments (#1+).
1290 if (Tmp1 != Node->getOperand(0)) {
1291 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1293 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1296 // Remember that the CALLSEQ_START is legalized.
1297 AddLegalizedOperand(Op.getValue(0), Result);
1298 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1299 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1301 // Now that the callseq_start and all of the non-call nodes above this call
1302 // sequence have been legalized, legalize the call itself. During this
1303 // process, no libcalls can/will be inserted, guaranteeing that no calls
1305 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1306 SDOperand InCallSEQ = LastCALLSEQ_END;
1307 // Note that we are selecting this call!
1308 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1309 IsLegalizingCall = true;
1311 // Legalize the call, starting from the CALLSEQ_END.
1312 LegalizeOp(LastCALLSEQ_END);
1313 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1316 case ISD::CALLSEQ_END:
1317 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1318 // will cause this node to be legalized as well as handling libcalls right.
1319 if (LastCALLSEQ_END.Val != Node) {
1320 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1321 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1322 assert(I != LegalizedNodes.end() &&
1323 "Legalizing the call start should have legalized this node!");
1327 // Otherwise, the call start has been legalized and everything is going
1328 // according to plan. Just legalize ourselves normally here.
1329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1330 // Do not try to legalize the target-specific arguments (#1+), except for
1331 // an optional flag input.
1332 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1333 if (Tmp1 != Node->getOperand(0)) {
1334 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1336 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1339 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1340 if (Tmp1 != Node->getOperand(0) ||
1341 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1342 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1345 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1348 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1349 // This finishes up call legalization.
1350 IsLegalizingCall = false;
1352 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1353 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1354 if (Node->getNumValues() == 2)
1355 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1356 return Result.getValue(Op.ResNo);
1357 case ISD::DYNAMIC_STACKALLOC: {
1358 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1359 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1360 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1363 Tmp1 = Result.getValue(0);
1364 Tmp2 = Result.getValue(1);
1365 switch (TLI.getOperationAction(Node->getOpcode(),
1366 Node->getValueType(0))) {
1367 default: assert(0 && "This action is not supported yet!");
1368 case TargetLowering::Expand: {
1369 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1370 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1371 " not tell us which reg is the stack pointer!");
1372 SDOperand Chain = Tmp1.getOperand(0);
1373 SDOperand Size = Tmp2.getOperand(1);
1374 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1375 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1376 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
1377 Tmp1 = LegalizeOp(Tmp1);
1378 Tmp2 = LegalizeOp(Tmp2);
1381 case TargetLowering::Custom:
1382 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1384 Tmp1 = LegalizeOp(Tmp3);
1385 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1388 case TargetLowering::Legal:
1391 // Since this op produce two values, make sure to remember that we
1392 // legalized both of them.
1393 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1394 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1395 return Op.ResNo ? Tmp2 : Tmp1;
1397 case ISD::INLINEASM: {
1398 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1399 bool Changed = false;
1400 // Legalize all of the operands of the inline asm, in case they are nodes
1401 // that need to be expanded or something. Note we skip the asm string and
1402 // all of the TargetConstant flags.
1403 SDOperand Op = LegalizeOp(Ops[0]);
1404 Changed = Op != Ops[0];
1407 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1408 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1409 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1410 for (++i; NumVals; ++i, --NumVals) {
1411 SDOperand Op = LegalizeOp(Ops[i]);
1420 Op = LegalizeOp(Ops.back());
1421 Changed |= Op != Ops.back();
1426 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1428 // INLINE asm returns a chain and flag, make sure to add both to the map.
1429 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1430 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1431 return Result.getValue(Op.ResNo);
1434 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1435 // Ensure that libcalls are emitted before a branch.
1436 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1437 Tmp1 = LegalizeOp(Tmp1);
1438 LastCALLSEQ_END = DAG.getEntryNode();
1440 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1443 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1444 // Ensure that libcalls are emitted before a branch.
1445 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1446 Tmp1 = LegalizeOp(Tmp1);
1447 LastCALLSEQ_END = DAG.getEntryNode();
1449 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1450 default: assert(0 && "Indirect target must be legal type (pointer)!");
1452 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1455 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1458 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1459 // Ensure that libcalls are emitted before a branch.
1460 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1461 Tmp1 = LegalizeOp(Tmp1);
1462 LastCALLSEQ_END = DAG.getEntryNode();
1464 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1467 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1468 default: assert(0 && "This action is not supported yet!");
1469 case TargetLowering::Legal: break;
1470 case TargetLowering::Custom:
1471 Tmp1 = TLI.LowerOperation(Result, DAG);
1472 if (Tmp1.Val) Result = Tmp1;
1474 case TargetLowering::Expand: {
1475 SDOperand Chain = Result.getOperand(0);
1476 SDOperand Table = Result.getOperand(1);
1477 SDOperand Index = Result.getOperand(2);
1479 MVT::ValueType PTy = TLI.getPointerTy();
1480 MachineFunction &MF = DAG.getMachineFunction();
1481 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1482 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1483 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1486 switch (EntrySize) {
1487 default: assert(0 && "Size of jump table not supported yet."); break;
1488 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1489 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1492 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1493 // For PIC, the sequence is:
1494 // BRIND(load(Jumptable + index) + RelocBase)
1495 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1497 if (TLI.usesGlobalOffsetTable())
1498 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1501 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
1502 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1503 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1505 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1511 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1512 // Ensure that libcalls are emitted before a return.
1513 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1514 Tmp1 = LegalizeOp(Tmp1);
1515 LastCALLSEQ_END = DAG.getEntryNode();
1517 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1518 case Expand: assert(0 && "It's impossible to expand bools");
1520 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1523 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1525 // The top bits of the promoted condition are not necessarily zero, ensure
1526 // that the value is properly zero extended.
1527 if (!DAG.MaskedValueIsZero(Tmp2,
1528 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1529 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1533 // Basic block destination (Op#2) is always legal.
1534 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1536 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1537 default: assert(0 && "This action is not supported yet!");
1538 case TargetLowering::Legal: break;
1539 case TargetLowering::Custom:
1540 Tmp1 = TLI.LowerOperation(Result, DAG);
1541 if (Tmp1.Val) Result = Tmp1;
1543 case TargetLowering::Expand:
1544 // Expand brcond's setcc into its constituent parts and create a BR_CC
1546 if (Tmp2.getOpcode() == ISD::SETCC) {
1547 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1548 Tmp2.getOperand(0), Tmp2.getOperand(1),
1549 Node->getOperand(2));
1551 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1552 DAG.getCondCode(ISD::SETNE), Tmp2,
1553 DAG.getConstant(0, Tmp2.getValueType()),
1554 Node->getOperand(2));
1560 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1561 // Ensure that libcalls are emitted before a branch.
1562 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1563 Tmp1 = LegalizeOp(Tmp1);
1564 Tmp2 = Node->getOperand(2); // LHS
1565 Tmp3 = Node->getOperand(3); // RHS
1566 Tmp4 = Node->getOperand(1); // CC
1568 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1569 LastCALLSEQ_END = DAG.getEntryNode();
1571 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1572 // the LHS is a legal SETCC itself. In this case, we need to compare
1573 // the result against zero to select between true and false values.
1574 if (Tmp3.Val == 0) {
1575 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1576 Tmp4 = DAG.getCondCode(ISD::SETNE);
1579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1580 Node->getOperand(4));
1582 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1583 default: assert(0 && "Unexpected action for BR_CC!");
1584 case TargetLowering::Legal: break;
1585 case TargetLowering::Custom:
1586 Tmp4 = TLI.LowerOperation(Result, DAG);
1587 if (Tmp4.Val) Result = Tmp4;
1592 LoadSDNode *LD = cast<LoadSDNode>(Node);
1593 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1594 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1596 ISD::LoadExtType ExtType = LD->getExtensionType();
1597 if (ExtType == ISD::NON_EXTLOAD) {
1598 MVT::ValueType VT = Node->getValueType(0);
1599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1600 Tmp3 = Result.getValue(0);
1601 Tmp4 = Result.getValue(1);
1603 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1604 default: assert(0 && "This action is not supported yet!");
1605 case TargetLowering::Legal:
1606 // If this is an unaligned load and the target doesn't support it,
1608 if (!TLI.allowsUnalignedMemoryAccesses()) {
1609 unsigned ABIAlignment = TLI.getTargetData()->
1610 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1611 if (LD->getAlignment() < ABIAlignment){
1612 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1614 Tmp3 = Result.getOperand(0);
1615 Tmp4 = Result.getOperand(1);
1621 case TargetLowering::Custom:
1622 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1624 Tmp3 = LegalizeOp(Tmp1);
1625 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1628 case TargetLowering::Promote: {
1629 // Only promote a load of vector type to another.
1630 assert(MVT::isVector(VT) && "Cannot promote this load!");
1631 // Change base type to a different vector type.
1632 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1634 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1635 LD->getSrcValueOffset(),
1636 LD->isVolatile(), LD->getAlignment());
1637 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1638 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1642 // Since loads produce two values, make sure to remember that we
1643 // legalized both of them.
1644 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1645 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1646 return Op.ResNo ? Tmp4 : Tmp3;
1648 MVT::ValueType SrcVT = LD->getLoadedVT();
1649 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1650 default: assert(0 && "This action is not supported yet!");
1651 case TargetLowering::Promote:
1652 assert(SrcVT == MVT::i1 &&
1653 "Can only promote extending LOAD from i1 -> i8!");
1654 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1655 LD->getSrcValue(), LD->getSrcValueOffset(),
1656 MVT::i8, LD->isVolatile(), LD->getAlignment());
1657 Tmp1 = Result.getValue(0);
1658 Tmp2 = Result.getValue(1);
1660 case TargetLowering::Custom:
1663 case TargetLowering::Legal:
1664 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1665 Tmp1 = Result.getValue(0);
1666 Tmp2 = Result.getValue(1);
1669 Tmp3 = TLI.LowerOperation(Result, DAG);
1671 Tmp1 = LegalizeOp(Tmp3);
1672 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1675 // If this is an unaligned load and the target doesn't support it,
1677 if (!TLI.allowsUnalignedMemoryAccesses()) {
1678 unsigned ABIAlignment = TLI.getTargetData()->
1679 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1680 if (LD->getAlignment() < ABIAlignment){
1681 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1683 Tmp1 = Result.getOperand(0);
1684 Tmp2 = Result.getOperand(1);
1691 case TargetLowering::Expand:
1692 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1693 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1694 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1695 LD->getSrcValueOffset(),
1696 LD->isVolatile(), LD->getAlignment());
1697 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1698 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1699 Tmp2 = LegalizeOp(Load.getValue(1));
1702 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1703 // Turn the unsupported load into an EXTLOAD followed by an explicit
1704 // zero/sign extend inreg.
1705 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1706 Tmp1, Tmp2, LD->getSrcValue(),
1707 LD->getSrcValueOffset(), SrcVT,
1708 LD->isVolatile(), LD->getAlignment());
1710 if (ExtType == ISD::SEXTLOAD)
1711 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1712 Result, DAG.getValueType(SrcVT));
1714 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1715 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1716 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1719 // Since loads produce two values, make sure to remember that we legalized
1721 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1722 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1723 return Op.ResNo ? Tmp2 : Tmp1;
1726 case ISD::EXTRACT_ELEMENT: {
1727 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1728 switch (getTypeAction(OpTy)) {
1729 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1731 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1733 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1734 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1735 TLI.getShiftAmountTy()));
1736 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1739 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1740 Node->getOperand(0));
1744 // Get both the low and high parts.
1745 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1746 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1747 Result = Tmp2; // 1 -> Hi
1749 Result = Tmp1; // 0 -> Lo
1755 case ISD::CopyToReg:
1756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1758 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
1759 "Register type must be legal!");
1760 // Legalize the incoming value (must be a legal type).
1761 Tmp2 = LegalizeOp(Node->getOperand(2));
1762 if (Node->getNumValues() == 1) {
1763 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
1765 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1766 if (Node->getNumOperands() == 4) {
1767 Tmp3 = LegalizeOp(Node->getOperand(3));
1768 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1774 // Since this produces two values, make sure to remember that we legalized
1776 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1777 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1785 // Ensure that libcalls are emitted before a return.
1786 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1787 Tmp1 = LegalizeOp(Tmp1);
1788 LastCALLSEQ_END = DAG.getEntryNode();
1790 switch (Node->getNumOperands()) {
1792 Tmp2 = Node->getOperand(1);
1793 Tmp3 = Node->getOperand(2); // Signness
1794 switch (getTypeAction(Tmp2.getValueType())) {
1796 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
1799 if (!MVT::isVector(Tmp2.getValueType())) {
1801 ExpandOp(Tmp2, Lo, Hi);
1803 // Big endian systems want the hi reg first.
1804 if (!TLI.isLittleEndian())
1808 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1810 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
1811 Result = LegalizeOp(Result);
1813 SDNode *InVal = Tmp2.Val;
1814 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1815 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
1817 // Figure out if there is a simple type corresponding to this Vector
1818 // type. If so, convert to the vector type.
1819 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1820 if (TLI.isTypeLegal(TVT)) {
1821 // Turn this into a return of the vector type.
1822 Tmp2 = LegalizeOp(Tmp2);
1823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1824 } else if (NumElems == 1) {
1825 // Turn this into a return of the scalar type.
1826 Tmp2 = ScalarizeVectorOp(Tmp2);
1827 Tmp2 = LegalizeOp(Tmp2);
1828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1830 // FIXME: Returns of gcc generic vectors smaller than a legal type
1831 // should be returned in integer registers!
1833 // The scalarized value type may not be legal, e.g. it might require
1834 // promotion or expansion. Relegalize the return.
1835 Result = LegalizeOp(Result);
1837 // FIXME: Returns of gcc generic vectors larger than a legal vector
1838 // type should be returned by reference!
1840 SplitVectorOp(Tmp2, Lo, Hi);
1841 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1842 Result = LegalizeOp(Result);
1847 Tmp2 = PromoteOp(Node->getOperand(1));
1848 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1849 Result = LegalizeOp(Result);
1854 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1856 default: { // ret <values>
1857 SmallVector<SDOperand, 8> NewValues;
1858 NewValues.push_back(Tmp1);
1859 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
1860 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1862 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
1863 NewValues.push_back(Node->getOperand(i+1));
1867 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
1868 "FIXME: TODO: implement returning non-legal vector types!");
1869 ExpandOp(Node->getOperand(i), Lo, Hi);
1870 NewValues.push_back(Lo);
1871 NewValues.push_back(Node->getOperand(i+1));
1873 NewValues.push_back(Hi);
1874 NewValues.push_back(Node->getOperand(i+1));
1879 assert(0 && "Can't promote multiple return value yet!");
1882 if (NewValues.size() == Node->getNumOperands())
1883 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
1885 Result = DAG.getNode(ISD::RET, MVT::Other,
1886 &NewValues[0], NewValues.size());
1891 if (Result.getOpcode() == ISD::RET) {
1892 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1893 default: assert(0 && "This action is not supported yet!");
1894 case TargetLowering::Legal: break;
1895 case TargetLowering::Custom:
1896 Tmp1 = TLI.LowerOperation(Result, DAG);
1897 if (Tmp1.Val) Result = Tmp1;
1903 StoreSDNode *ST = cast<StoreSDNode>(Node);
1904 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1905 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
1906 int SVOffset = ST->getSrcValueOffset();
1907 unsigned Alignment = ST->getAlignment();
1908 bool isVolatile = ST->isVolatile();
1910 if (!ST->isTruncatingStore()) {
1911 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1912 // FIXME: We shouldn't do this for TargetConstantFP's.
1913 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1914 // to phase ordering between legalized code and the dag combiner. This
1915 // probably means that we need to integrate dag combiner and legalizer
1917 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1918 if (CFP->getValueType(0) == MVT::f32) {
1919 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1921 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1922 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1924 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1925 SVOffset, isVolatile, Alignment);
1929 switch (getTypeAction(ST->getStoredVT())) {
1931 Tmp3 = LegalizeOp(ST->getValue());
1932 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1935 MVT::ValueType VT = Tmp3.getValueType();
1936 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1937 default: assert(0 && "This action is not supported yet!");
1938 case TargetLowering::Legal:
1939 // If this is an unaligned store and the target doesn't support it,
1941 if (!TLI.allowsUnalignedMemoryAccesses()) {
1942 unsigned ABIAlignment = TLI.getTargetData()->
1943 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
1944 if (ST->getAlignment() < ABIAlignment)
1945 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
1949 case TargetLowering::Custom:
1950 Tmp1 = TLI.LowerOperation(Result, DAG);
1951 if (Tmp1.Val) Result = Tmp1;
1953 case TargetLowering::Promote:
1954 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1955 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1956 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1957 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1958 ST->getSrcValue(), SVOffset, isVolatile,
1965 // Truncate the value and store the result.
1966 Tmp3 = PromoteOp(ST->getValue());
1967 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1968 SVOffset, ST->getStoredVT(),
1969 isVolatile, Alignment);
1973 unsigned IncrementSize = 0;
1976 // If this is a vector type, then we have to calculate the increment as
1977 // the product of the element size in bytes, and the number of elements
1978 // in the high half of the vector.
1979 if (MVT::isVector(ST->getValue().getValueType())) {
1980 SDNode *InVal = ST->getValue().Val;
1981 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1982 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
1984 // Figure out if there is a simple type corresponding to this Vector
1985 // type. If so, convert to the vector type.
1986 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1987 if (TLI.isTypeLegal(TVT)) {
1988 // Turn this into a normal store of the vector type.
1989 Tmp3 = LegalizeOp(Node->getOperand(1));
1990 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1991 SVOffset, isVolatile, Alignment);
1992 Result = LegalizeOp(Result);
1994 } else if (NumElems == 1) {
1995 // Turn this into a normal store of the scalar type.
1996 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
1997 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1998 SVOffset, isVolatile, Alignment);
1999 // The scalarized value type may not be legal, e.g. it might require
2000 // promotion or expansion. Relegalize the scalar store.
2001 Result = LegalizeOp(Result);
2004 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2005 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2008 ExpandOp(Node->getOperand(1), Lo, Hi);
2009 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2011 if (!TLI.isLittleEndian())
2015 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2016 SVOffset, isVolatile, Alignment);
2018 if (Hi.Val == NULL) {
2019 // Must be int <-> float one-to-one expansion.
2024 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2025 getIntPtrConstant(IncrementSize));
2026 assert(isTypeLegal(Tmp2.getValueType()) &&
2027 "Pointers must be legal!");
2028 SVOffset += IncrementSize;
2029 if (Alignment > IncrementSize)
2030 Alignment = IncrementSize;
2031 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2032 SVOffset, isVolatile, Alignment);
2033 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2038 assert(isTypeLegal(ST->getValue().getValueType()) &&
2039 "Cannot handle illegal TRUNCSTORE yet!");
2040 Tmp3 = LegalizeOp(ST->getValue());
2042 // The only promote case we handle is TRUNCSTORE:i1 X into
2043 // -> TRUNCSTORE:i8 (and X, 1)
2044 if (ST->getStoredVT() == MVT::i1 &&
2045 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2046 // Promote the bool to a mask then store.
2047 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2048 DAG.getConstant(1, Tmp3.getValueType()));
2049 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2051 isVolatile, Alignment);
2052 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2053 Tmp2 != ST->getBasePtr()) {
2054 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2058 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2059 switch (TLI.getStoreXAction(StVT)) {
2060 default: assert(0 && "This action is not supported yet!");
2061 case TargetLowering::Legal:
2062 // If this is an unaligned store and the target doesn't support it,
2064 if (!TLI.allowsUnalignedMemoryAccesses()) {
2065 unsigned ABIAlignment = TLI.getTargetData()->
2066 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2067 if (ST->getAlignment() < ABIAlignment)
2068 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2072 case TargetLowering::Custom:
2073 Tmp1 = TLI.LowerOperation(Result, DAG);
2074 if (Tmp1.Val) Result = Tmp1;
2081 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2084 case ISD::STACKSAVE:
2085 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2086 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2087 Tmp1 = Result.getValue(0);
2088 Tmp2 = Result.getValue(1);
2090 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2091 default: assert(0 && "This action is not supported yet!");
2092 case TargetLowering::Legal: break;
2093 case TargetLowering::Custom:
2094 Tmp3 = TLI.LowerOperation(Result, DAG);
2096 Tmp1 = LegalizeOp(Tmp3);
2097 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2100 case TargetLowering::Expand:
2101 // Expand to CopyFromReg if the target set
2102 // StackPointerRegisterToSaveRestore.
2103 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2104 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2105 Node->getValueType(0));
2106 Tmp2 = Tmp1.getValue(1);
2108 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2109 Tmp2 = Node->getOperand(0);
2114 // Since stacksave produce two values, make sure to remember that we
2115 // legalized both of them.
2116 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2117 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2118 return Op.ResNo ? Tmp2 : Tmp1;
2120 case ISD::STACKRESTORE:
2121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2123 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2125 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2126 default: assert(0 && "This action is not supported yet!");
2127 case TargetLowering::Legal: break;
2128 case TargetLowering::Custom:
2129 Tmp1 = TLI.LowerOperation(Result, DAG);
2130 if (Tmp1.Val) Result = Tmp1;
2132 case TargetLowering::Expand:
2133 // Expand to CopyToReg if the target set
2134 // StackPointerRegisterToSaveRestore.
2135 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2136 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2144 case ISD::READCYCLECOUNTER:
2145 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2146 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2147 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2148 Node->getValueType(0))) {
2149 default: assert(0 && "This action is not supported yet!");
2150 case TargetLowering::Legal:
2151 Tmp1 = Result.getValue(0);
2152 Tmp2 = Result.getValue(1);
2154 case TargetLowering::Custom:
2155 Result = TLI.LowerOperation(Result, DAG);
2156 Tmp1 = LegalizeOp(Result.getValue(0));
2157 Tmp2 = LegalizeOp(Result.getValue(1));
2161 // Since rdcc produce two values, make sure to remember that we legalized
2163 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2164 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2168 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2169 case Expand: assert(0 && "It's impossible to expand bools");
2171 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2174 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2175 // Make sure the condition is either zero or one.
2176 if (!DAG.MaskedValueIsZero(Tmp1,
2177 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2178 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2181 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2182 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2186 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2187 default: assert(0 && "This action is not supported yet!");
2188 case TargetLowering::Legal: break;
2189 case TargetLowering::Custom: {
2190 Tmp1 = TLI.LowerOperation(Result, DAG);
2191 if (Tmp1.Val) Result = Tmp1;
2194 case TargetLowering::Expand:
2195 if (Tmp1.getOpcode() == ISD::SETCC) {
2196 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2198 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2200 Result = DAG.getSelectCC(Tmp1,
2201 DAG.getConstant(0, Tmp1.getValueType()),
2202 Tmp2, Tmp3, ISD::SETNE);
2205 case TargetLowering::Promote: {
2206 MVT::ValueType NVT =
2207 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2208 unsigned ExtOp, TruncOp;
2209 if (MVT::isVector(Tmp2.getValueType())) {
2210 ExtOp = ISD::BIT_CONVERT;
2211 TruncOp = ISD::BIT_CONVERT;
2212 } else if (MVT::isInteger(Tmp2.getValueType())) {
2213 ExtOp = ISD::ANY_EXTEND;
2214 TruncOp = ISD::TRUNCATE;
2216 ExtOp = ISD::FP_EXTEND;
2217 TruncOp = ISD::FP_ROUND;
2219 // Promote each of the values to the new type.
2220 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2221 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2222 // Perform the larger operation, then round down.
2223 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2224 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2229 case ISD::SELECT_CC: {
2230 Tmp1 = Node->getOperand(0); // LHS
2231 Tmp2 = Node->getOperand(1); // RHS
2232 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2233 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2234 SDOperand CC = Node->getOperand(4);
2236 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2238 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2239 // the LHS is a legal SETCC itself. In this case, we need to compare
2240 // the result against zero to select between true and false values.
2241 if (Tmp2.Val == 0) {
2242 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2243 CC = DAG.getCondCode(ISD::SETNE);
2245 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2247 // Everything is legal, see if we should expand this op or something.
2248 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2249 default: assert(0 && "This action is not supported yet!");
2250 case TargetLowering::Legal: break;
2251 case TargetLowering::Custom:
2252 Tmp1 = TLI.LowerOperation(Result, DAG);
2253 if (Tmp1.Val) Result = Tmp1;
2259 Tmp1 = Node->getOperand(0);
2260 Tmp2 = Node->getOperand(1);
2261 Tmp3 = Node->getOperand(2);
2262 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2264 // If we had to Expand the SetCC operands into a SELECT node, then it may
2265 // not always be possible to return a true LHS & RHS. In this case, just
2266 // return the value we legalized, returned in the LHS
2267 if (Tmp2.Val == 0) {
2272 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2273 default: assert(0 && "Cannot handle this action for SETCC yet!");
2274 case TargetLowering::Custom:
2277 case TargetLowering::Legal:
2278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2280 Tmp4 = TLI.LowerOperation(Result, DAG);
2281 if (Tmp4.Val) Result = Tmp4;
2284 case TargetLowering::Promote: {
2285 // First step, figure out the appropriate operation to use.
2286 // Allow SETCC to not be supported for all legal data types
2287 // Mostly this targets FP
2288 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2289 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2291 // Scan for the appropriate larger type to use.
2293 NewInTy = (MVT::ValueType)(NewInTy+1);
2295 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2296 "Fell off of the edge of the integer world");
2297 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2298 "Fell off of the edge of the floating point world");
2300 // If the target supports SETCC of this type, use it.
2301 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2304 if (MVT::isInteger(NewInTy))
2305 assert(0 && "Cannot promote Legal Integer SETCC yet");
2307 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2308 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2310 Tmp1 = LegalizeOp(Tmp1);
2311 Tmp2 = LegalizeOp(Tmp2);
2312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2313 Result = LegalizeOp(Result);
2316 case TargetLowering::Expand:
2317 // Expand a setcc node into a select_cc of the same condition, lhs, and
2318 // rhs that selects between const 1 (true) and const 0 (false).
2319 MVT::ValueType VT = Node->getValueType(0);
2320 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2321 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2328 case ISD::MEMMOVE: {
2329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2332 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2333 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2334 case Expand: assert(0 && "Cannot expand a byte!");
2336 Tmp3 = LegalizeOp(Node->getOperand(2));
2339 Tmp3 = PromoteOp(Node->getOperand(2));
2343 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2347 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2349 // Length is too big, just take the lo-part of the length.
2351 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2355 Tmp4 = LegalizeOp(Node->getOperand(3));
2358 Tmp4 = PromoteOp(Node->getOperand(3));
2363 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2364 case Expand: assert(0 && "Cannot expand this yet!");
2366 Tmp5 = LegalizeOp(Node->getOperand(4));
2369 Tmp5 = PromoteOp(Node->getOperand(4));
2373 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2374 default: assert(0 && "This action not implemented for this operation!");
2375 case TargetLowering::Custom:
2378 case TargetLowering::Legal:
2379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
2381 Tmp1 = TLI.LowerOperation(Result, DAG);
2382 if (Tmp1.Val) Result = Tmp1;
2385 case TargetLowering::Expand: {
2386 // Otherwise, the target does not support this operation. Lower the
2387 // operation to an explicit libcall as appropriate.
2388 MVT::ValueType IntPtr = TLI.getPointerTy();
2389 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2390 TargetLowering::ArgListTy Args;
2391 TargetLowering::ArgListEntry Entry;
2393 const char *FnName = 0;
2394 if (Node->getOpcode() == ISD::MEMSET) {
2395 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2396 Args.push_back(Entry);
2397 // Extend the (previously legalized) ubyte argument to be an int value
2399 if (Tmp3.getValueType() > MVT::i32)
2400 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2402 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2403 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2404 Args.push_back(Entry);
2405 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2406 Args.push_back(Entry);
2409 } else if (Node->getOpcode() == ISD::MEMCPY ||
2410 Node->getOpcode() == ISD::MEMMOVE) {
2411 Entry.Ty = IntPtrTy;
2412 Entry.Node = Tmp2; Args.push_back(Entry);
2413 Entry.Node = Tmp3; Args.push_back(Entry);
2414 Entry.Node = Tmp4; Args.push_back(Entry);
2415 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2417 assert(0 && "Unknown op!");
2420 std::pair<SDOperand,SDOperand> CallResult =
2421 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
2422 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2423 Result = CallResult.second;
2430 case ISD::SHL_PARTS:
2431 case ISD::SRA_PARTS:
2432 case ISD::SRL_PARTS: {
2433 SmallVector<SDOperand, 8> Ops;
2434 bool Changed = false;
2435 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2436 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2437 Changed |= Ops.back() != Node->getOperand(i);
2440 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2442 switch (TLI.getOperationAction(Node->getOpcode(),
2443 Node->getValueType(0))) {
2444 default: assert(0 && "This action is not supported yet!");
2445 case TargetLowering::Legal: break;
2446 case TargetLowering::Custom:
2447 Tmp1 = TLI.LowerOperation(Result, DAG);
2449 SDOperand Tmp2, RetVal(0, 0);
2450 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2451 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2452 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2456 assert(RetVal.Val && "Illegal result number");
2462 // Since these produce multiple values, make sure to remember that we
2463 // legalized all of them.
2464 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2465 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2466 return Result.getValue(Op.ResNo);
2487 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2488 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2489 case Expand: assert(0 && "Not possible");
2491 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2494 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2500 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2501 default: assert(0 && "BinOp legalize operation not supported");
2502 case TargetLowering::Legal: break;
2503 case TargetLowering::Custom:
2504 Tmp1 = TLI.LowerOperation(Result, DAG);
2505 if (Tmp1.Val) Result = Tmp1;
2507 case TargetLowering::Expand: {
2508 if (Node->getValueType(0) == MVT::i32) {
2509 switch (Node->getOpcode()) {
2510 default: assert(0 && "Do not know how to expand this integer BinOp!");
2513 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2514 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
2516 bool isSigned = Node->getOpcode() == ISD::SDIV;
2517 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
2522 assert(MVT::isVector(Node->getValueType(0)) &&
2523 "Cannot expand this binary operator!");
2524 // Expand the operation into a bunch of nasty scalar code.
2525 SmallVector<SDOperand, 8> Ops;
2526 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
2527 MVT::ValueType PtrVT = TLI.getPointerTy();
2528 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2530 SDOperand Idx = DAG.getConstant(i, PtrVT);
2531 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2532 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2533 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2535 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2536 &Ops[0], Ops.size());
2539 case TargetLowering::Promote: {
2540 switch (Node->getOpcode()) {
2541 default: assert(0 && "Do not know how to promote this BinOp!");
2545 MVT::ValueType OVT = Node->getValueType(0);
2546 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2547 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2548 // Bit convert each of the values to the new type.
2549 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2550 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2551 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2552 // Bit convert the result back the original type.
2553 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2561 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2562 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2563 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2564 case Expand: assert(0 && "Not possible");
2566 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2569 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2575 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2576 default: assert(0 && "Operation not supported");
2577 case TargetLowering::Custom:
2578 Tmp1 = TLI.LowerOperation(Result, DAG);
2579 if (Tmp1.Val) Result = Tmp1;
2581 case TargetLowering::Legal: break;
2582 case TargetLowering::Expand: {
2583 // If this target supports fabs/fneg natively and select is cheap,
2584 // do this efficiently.
2585 if (!TLI.isSelectExpensive() &&
2586 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2587 TargetLowering::Legal &&
2588 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
2589 TargetLowering::Legal) {
2590 // Get the sign bit of the RHS.
2591 MVT::ValueType IVT =
2592 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2593 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2594 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2595 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2596 // Get the absolute value of the result.
2597 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2598 // Select between the nabs and abs value based on the sign bit of
2600 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2601 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2604 Result = LegalizeOp(Result);
2608 // Otherwise, do bitwise ops!
2609 MVT::ValueType NVT =
2610 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2611 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2612 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2613 Result = LegalizeOp(Result);
2621 Tmp1 = LegalizeOp(Node->getOperand(0));
2622 Tmp2 = LegalizeOp(Node->getOperand(1));
2623 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2624 // Since this produces two values, make sure to remember that we legalized
2626 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2627 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2632 Tmp1 = LegalizeOp(Node->getOperand(0));
2633 Tmp2 = LegalizeOp(Node->getOperand(1));
2634 Tmp3 = LegalizeOp(Node->getOperand(2));
2635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2636 // Since this produces two values, make sure to remember that we legalized
2638 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2639 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2642 case ISD::BUILD_PAIR: {
2643 MVT::ValueType PairTy = Node->getValueType(0);
2644 // TODO: handle the case where the Lo and Hi operands are not of legal type
2645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2647 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2648 case TargetLowering::Promote:
2649 case TargetLowering::Custom:
2650 assert(0 && "Cannot promote/custom this yet!");
2651 case TargetLowering::Legal:
2652 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2653 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2655 case TargetLowering::Expand:
2656 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2657 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2658 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2659 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2660 TLI.getShiftAmountTy()));
2661 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
2670 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2671 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2673 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2674 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2675 case TargetLowering::Custom:
2678 case TargetLowering::Legal:
2679 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2681 Tmp1 = TLI.LowerOperation(Result, DAG);
2682 if (Tmp1.Val) Result = Tmp1;
2685 case TargetLowering::Expand:
2686 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2687 bool isSigned = DivOpc == ISD::SDIV;
2688 if (MVT::isInteger(Node->getValueType(0))) {
2689 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2690 TargetLowering::Legal) {
2692 MVT::ValueType VT = Node->getValueType(0);
2693 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
2694 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2695 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2697 assert(Node->getValueType(0) == MVT::i32 &&
2698 "Cannot expand this binary operator!");
2699 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2700 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
2702 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
2705 // Floating point mod -> fmod libcall.
2706 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2707 ? RTLIB::REM_F32 : RTLIB::REM_F64;
2709 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2710 false/*sign irrelevant*/, Dummy);
2716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2717 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2719 MVT::ValueType VT = Node->getValueType(0);
2720 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2721 default: assert(0 && "This action is not supported yet!");
2722 case TargetLowering::Custom:
2725 case TargetLowering::Legal:
2726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2727 Result = Result.getValue(0);
2728 Tmp1 = Result.getValue(1);
2731 Tmp2 = TLI.LowerOperation(Result, DAG);
2733 Result = LegalizeOp(Tmp2);
2734 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2738 case TargetLowering::Expand: {
2739 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
2740 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2741 SV->getValue(), SV->getOffset());
2742 // Increment the pointer, VAList, to the next vaarg
2743 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2744 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2745 TLI.getPointerTy()));
2746 // Store the incremented VAList to the legalized pointer
2747 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2749 // Load the actual argument out of the pointer VAList
2750 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
2751 Tmp1 = LegalizeOp(Result.getValue(1));
2752 Result = LegalizeOp(Result);
2756 // Since VAARG produces two values, make sure to remember that we
2757 // legalized both of them.
2758 AddLegalizedOperand(SDOperand(Node, 0), Result);
2759 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2760 return Op.ResNo ? Tmp1 : Result;
2764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2765 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2766 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2768 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2769 default: assert(0 && "This action is not supported yet!");
2770 case TargetLowering::Custom:
2773 case TargetLowering::Legal:
2774 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2775 Node->getOperand(3), Node->getOperand(4));
2777 Tmp1 = TLI.LowerOperation(Result, DAG);
2778 if (Tmp1.Val) Result = Tmp1;
2781 case TargetLowering::Expand:
2782 // This defaults to loading a pointer from the input and storing it to the
2783 // output, returning the chain.
2784 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
2785 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
2786 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2788 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2798 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2799 default: assert(0 && "This action is not supported yet!");
2800 case TargetLowering::Custom:
2803 case TargetLowering::Legal:
2804 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2806 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2807 if (Tmp1.Val) Result = Tmp1;
2810 case TargetLowering::Expand:
2811 Result = Tmp1; // Default to a no-op, return the chain
2817 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2818 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2822 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2823 default: assert(0 && "This action is not supported yet!");
2824 case TargetLowering::Legal: break;
2825 case TargetLowering::Custom:
2826 Tmp1 = TLI.LowerOperation(Result, DAG);
2827 if (Tmp1.Val) Result = Tmp1;
2834 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2835 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2837 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2839 assert(0 && "ROTL/ROTR legalize operation not supported");
2841 case TargetLowering::Legal:
2843 case TargetLowering::Custom:
2844 Tmp1 = TLI.LowerOperation(Result, DAG);
2845 if (Tmp1.Val) Result = Tmp1;
2847 case TargetLowering::Promote:
2848 assert(0 && "Do not know how to promote ROTL/ROTR");
2850 case TargetLowering::Expand:
2851 assert(0 && "Do not know how to expand ROTL/ROTR");
2857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2858 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2859 case TargetLowering::Custom:
2860 assert(0 && "Cannot custom legalize this yet!");
2861 case TargetLowering::Legal:
2862 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2864 case TargetLowering::Promote: {
2865 MVT::ValueType OVT = Tmp1.getValueType();
2866 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2867 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
2869 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2870 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2871 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2872 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2875 case TargetLowering::Expand:
2876 Result = ExpandBSWAP(Tmp1);
2884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2885 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2886 case TargetLowering::Custom:
2887 case TargetLowering::Legal:
2888 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2889 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
2890 TargetLowering::Custom) {
2891 Tmp1 = TLI.LowerOperation(Result, DAG);
2897 case TargetLowering::Promote: {
2898 MVT::ValueType OVT = Tmp1.getValueType();
2899 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2901 // Zero extend the argument.
2902 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2903 // Perform the larger operation, then subtract if needed.
2904 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2905 switch (Node->getOpcode()) {
2910 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
2911 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2912 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
2914 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
2915 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
2918 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
2919 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2920 DAG.getConstant(MVT::getSizeInBits(NVT) -
2921 MVT::getSizeInBits(OVT), NVT));
2926 case TargetLowering::Expand:
2927 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
2938 Tmp1 = LegalizeOp(Node->getOperand(0));
2939 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2940 case TargetLowering::Promote:
2941 case TargetLowering::Custom:
2944 case TargetLowering::Legal:
2945 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2947 Tmp1 = TLI.LowerOperation(Result, DAG);
2948 if (Tmp1.Val) Result = Tmp1;
2951 case TargetLowering::Expand:
2952 switch (Node->getOpcode()) {
2953 default: assert(0 && "Unreachable!");
2955 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2956 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2957 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
2960 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2961 MVT::ValueType VT = Node->getValueType(0);
2962 Tmp2 = DAG.getConstantFP(0.0, VT);
2963 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
2964 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2965 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2971 MVT::ValueType VT = Node->getValueType(0);
2972 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2973 switch(Node->getOpcode()) {
2975 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2978 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2981 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2983 default: assert(0 && "Unreachable!");
2986 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2987 false/*sign irrelevant*/, Dummy);
2995 // We always lower FPOWI into a libcall. No target support it yet.
2996 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2997 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
2999 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3000 false/*sign irrelevant*/, Dummy);
3003 case ISD::BIT_CONVERT:
3004 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
3005 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3006 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3007 // The input has to be a vector type, we have to either scalarize it, pack
3008 // it, or convert it based on whether the input vector type is legal.
3009 SDNode *InVal = Node->getOperand(0).Val;
3010 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3011 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3013 // Figure out if there is a simple type corresponding to this Vector
3014 // type. If so, convert to the vector type.
3015 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3016 if (TLI.isTypeLegal(TVT)) {
3017 // Turn this into a bit convert of the vector input.
3018 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3019 LegalizeOp(Node->getOperand(0)));
3021 } else if (NumElems == 1) {
3022 // Turn this into a bit convert of the scalar input.
3023 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3024 ScalarizeVectorOp(Node->getOperand(0)));
3027 // FIXME: UNIMP! Store then reload
3028 assert(0 && "Cast from unsupported vector type not implemented yet!");
3031 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3032 Node->getOperand(0).getValueType())) {
3033 default: assert(0 && "Unknown operation action!");
3034 case TargetLowering::Expand:
3035 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3037 case TargetLowering::Legal:
3038 Tmp1 = LegalizeOp(Node->getOperand(0));
3039 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3045 // Conversion operators. The source and destination have different types.
3046 case ISD::SINT_TO_FP:
3047 case ISD::UINT_TO_FP: {
3048 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3049 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3051 switch (TLI.getOperationAction(Node->getOpcode(),
3052 Node->getOperand(0).getValueType())) {
3053 default: assert(0 && "Unknown operation action!");
3054 case TargetLowering::Custom:
3057 case TargetLowering::Legal:
3058 Tmp1 = LegalizeOp(Node->getOperand(0));
3059 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3061 Tmp1 = TLI.LowerOperation(Result, DAG);
3062 if (Tmp1.Val) Result = Tmp1;
3065 case TargetLowering::Expand:
3066 Result = ExpandLegalINT_TO_FP(isSigned,
3067 LegalizeOp(Node->getOperand(0)),
3068 Node->getValueType(0));
3070 case TargetLowering::Promote:
3071 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3072 Node->getValueType(0),
3078 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3079 Node->getValueType(0), Node->getOperand(0));
3082 Tmp1 = PromoteOp(Node->getOperand(0));
3084 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3085 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3087 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3088 Node->getOperand(0).getValueType());
3090 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3091 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3099 Tmp1 = LegalizeOp(Node->getOperand(0));
3100 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3103 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3105 // Since the result is legal, we should just be able to truncate the low
3106 // part of the source.
3107 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3110 Result = PromoteOp(Node->getOperand(0));
3111 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3116 case ISD::FP_TO_SINT:
3117 case ISD::FP_TO_UINT:
3118 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3120 Tmp1 = LegalizeOp(Node->getOperand(0));
3122 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3123 default: assert(0 && "Unknown operation action!");
3124 case TargetLowering::Custom:
3127 case TargetLowering::Legal:
3128 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3130 Tmp1 = TLI.LowerOperation(Result, DAG);
3131 if (Tmp1.Val) Result = Tmp1;
3134 case TargetLowering::Promote:
3135 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3136 Node->getOpcode() == ISD::FP_TO_SINT);
3138 case TargetLowering::Expand:
3139 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3140 SDOperand True, False;
3141 MVT::ValueType VT = Node->getOperand(0).getValueType();
3142 MVT::ValueType NVT = Node->getValueType(0);
3143 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3144 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3145 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3146 Node->getOperand(0), Tmp2, ISD::SETLT);
3147 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3148 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3149 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3151 False = DAG.getNode(ISD::XOR, NVT, False,
3152 DAG.getConstant(1ULL << ShiftAmt, NVT));
3153 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3156 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3162 // Convert f32 / f64 to i32 / i64.
3163 MVT::ValueType VT = Op.getValueType();
3164 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3165 switch (Node->getOpcode()) {
3166 case ISD::FP_TO_SINT:
3167 if (Node->getOperand(0).getValueType() == MVT::f32)
3168 LC = (VT == MVT::i32)
3169 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
3171 LC = (VT == MVT::i32)
3172 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
3174 case ISD::FP_TO_UINT:
3175 if (Node->getOperand(0).getValueType() == MVT::f32)
3176 LC = (VT == MVT::i32)
3177 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
3179 LC = (VT == MVT::i32)
3180 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
3182 default: assert(0 && "Unreachable!");
3185 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3186 false/*sign irrelevant*/, Dummy);
3190 Tmp1 = PromoteOp(Node->getOperand(0));
3191 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3192 Result = LegalizeOp(Result);
3198 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3199 TargetLowering::Expand) {
3200 // The only way we can lower this is to turn it into a TRUNCSTORE,
3201 // EXTLOAD pair, targetting a temporary location (a stack slot).
3203 // NOTE: there is a choice here between constantly creating new stack
3204 // slots and always reusing the same one. We currently always create
3205 // new ones, as reuse may inhibit scheduling.
3206 MVT::ValueType VT = Op.getValueType(); // 32
3207 const Type *Ty = MVT::getTypeForValueType(VT);
3208 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3209 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3210 MachineFunction &MF = DAG.getMachineFunction();
3212 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3213 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3214 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3215 StackSlot, NULL, 0, VT);
3216 Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT);
3220 case ISD::ANY_EXTEND:
3221 case ISD::ZERO_EXTEND:
3222 case ISD::SIGN_EXTEND:
3223 case ISD::FP_EXTEND:
3224 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3225 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3227 Tmp1 = LegalizeOp(Node->getOperand(0));
3228 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3231 switch (Node->getOpcode()) {
3232 case ISD::ANY_EXTEND:
3233 Tmp1 = PromoteOp(Node->getOperand(0));
3234 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3236 case ISD::ZERO_EXTEND:
3237 Result = PromoteOp(Node->getOperand(0));
3238 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3239 Result = DAG.getZeroExtendInReg(Result,
3240 Node->getOperand(0).getValueType());
3242 case ISD::SIGN_EXTEND:
3243 Result = PromoteOp(Node->getOperand(0));
3244 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3245 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3247 DAG.getValueType(Node->getOperand(0).getValueType()));
3249 case ISD::FP_EXTEND:
3250 Result = PromoteOp(Node->getOperand(0));
3251 if (Result.getValueType() != Op.getValueType())
3252 // Dynamically dead while we have only 2 FP types.
3253 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3256 Result = PromoteOp(Node->getOperand(0));
3257 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3262 case ISD::FP_ROUND_INREG:
3263 case ISD::SIGN_EXTEND_INREG: {
3264 Tmp1 = LegalizeOp(Node->getOperand(0));
3265 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3267 // If this operation is not supported, convert it to a shl/shr or load/store
3269 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3270 default: assert(0 && "This action not supported for this op yet!");
3271 case TargetLowering::Legal:
3272 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3274 case TargetLowering::Expand:
3275 // If this is an integer extend and shifts are supported, do that.
3276 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3277 // NOTE: we could fall back on load/store here too for targets without
3278 // SAR. However, it is doubtful that any exist.
3279 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3280 MVT::getSizeInBits(ExtraVT);
3281 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3282 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3283 Node->getOperand(0), ShiftCst);
3284 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3286 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3287 // The only way we can lower this is to turn it into a TRUNCSTORE,
3288 // EXTLOAD pair, targetting a temporary location (a stack slot).
3290 // NOTE: there is a choice here between constantly creating new stack
3291 // slots and always reusing the same one. We currently always create
3292 // new ones, as reuse may inhibit scheduling.
3293 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
3294 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3295 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3296 MachineFunction &MF = DAG.getMachineFunction();
3298 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3299 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3300 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3301 StackSlot, NULL, 0, ExtraVT);
3302 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
3303 Result, StackSlot, NULL, 0, ExtraVT);
3305 assert(0 && "Unknown op");
3311 case ISD::ADJUST_TRAMP: {
3312 Tmp1 = LegalizeOp(Node->getOperand(0));
3313 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3314 default: assert(0 && "This action is not supported yet!");
3315 case TargetLowering::Custom:
3316 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3317 Result = TLI.LowerOperation(Result, DAG);
3318 if (Result.Val) break;
3320 case TargetLowering::Expand:
3326 case ISD::TRAMPOLINE: {
3328 for (unsigned i = 0; i != 6; ++i)
3329 Ops[i] = LegalizeOp(Node->getOperand(i));
3330 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3331 // The only option for this node is to custom lower it.
3332 Result = TLI.LowerOperation(Result, DAG);
3333 assert(Result.Val && "Should always custom lower!");
3338 assert(Result.getValueType() == Op.getValueType() &&
3339 "Bad legalization!");
3341 // Make sure that the generated code is itself legal.
3343 Result = LegalizeOp(Result);
3345 // Note that LegalizeOp may be reentered even from single-use nodes, which
3346 // means that we always must cache transformed nodes.
3347 AddLegalizedOperand(Op, Result);
3351 /// PromoteOp - Given an operation that produces a value in an invalid type,
3352 /// promote it to compute the value into a larger type. The produced value will
3353 /// have the correct bits for the low portion of the register, but no guarantee
3354 /// is made about the top bits: it may be zero, sign-extended, or garbage.
3355 SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3356 MVT::ValueType VT = Op.getValueType();
3357 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3358 assert(getTypeAction(VT) == Promote &&
3359 "Caller should expand or legalize operands that are not promotable!");
3360 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3361 "Cannot promote to smaller type!");
3363 SDOperand Tmp1, Tmp2, Tmp3;
3365 SDNode *Node = Op.Val;
3367 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3368 if (I != PromotedNodes.end()) return I->second;
3370 switch (Node->getOpcode()) {
3371 case ISD::CopyFromReg:
3372 assert(0 && "CopyFromReg must be legal!");
3375 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
3377 assert(0 && "Do not know how to promote this operator!");
3380 Result = DAG.getNode(ISD::UNDEF, NVT);
3384 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3386 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
3387 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3389 case ISD::ConstantFP:
3390 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3391 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3395 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
3396 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3397 Node->getOperand(1), Node->getOperand(2));
3401 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3403 Result = LegalizeOp(Node->getOperand(0));
3404 assert(Result.getValueType() >= NVT &&
3405 "This truncation doesn't make sense!");
3406 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3407 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3410 // The truncation is not required, because we don't guarantee anything
3411 // about high bits anyway.
3412 Result = PromoteOp(Node->getOperand(0));
3415 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3416 // Truncate the low part of the expanded value to the result type
3417 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
3420 case ISD::SIGN_EXTEND:
3421 case ISD::ZERO_EXTEND:
3422 case ISD::ANY_EXTEND:
3423 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3424 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3426 // Input is legal? Just do extend all the way to the larger type.
3427 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
3430 // Promote the reg if it's smaller.
3431 Result = PromoteOp(Node->getOperand(0));
3432 // The high bits are not guaranteed to be anything. Insert an extend.
3433 if (Node->getOpcode() == ISD::SIGN_EXTEND)
3434 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3435 DAG.getValueType(Node->getOperand(0).getValueType()));
3436 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
3437 Result = DAG.getZeroExtendInReg(Result,
3438 Node->getOperand(0).getValueType());
3442 case ISD::BIT_CONVERT:
3443 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3444 Result = PromoteOp(Result);
3447 case ISD::FP_EXTEND:
3448 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3450 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3451 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3452 case Promote: assert(0 && "Unreachable with 2 FP types!");
3454 // Input is legal? Do an FP_ROUND_INREG.
3455 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
3456 DAG.getValueType(VT));
3461 case ISD::SINT_TO_FP:
3462 case ISD::UINT_TO_FP:
3463 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3465 // No extra round required here.
3466 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
3470 Result = PromoteOp(Node->getOperand(0));
3471 if (Node->getOpcode() == ISD::SINT_TO_FP)
3472 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3474 DAG.getValueType(Node->getOperand(0).getValueType()));
3476 Result = DAG.getZeroExtendInReg(Result,
3477 Node->getOperand(0).getValueType());
3478 // No extra round required here.
3479 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
3482 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3483 Node->getOperand(0));
3484 // Round if we cannot tolerate excess precision.
3485 if (NoExcessFPPrecision)
3486 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3487 DAG.getValueType(VT));
3492 case ISD::SIGN_EXTEND_INREG:
3493 Result = PromoteOp(Node->getOperand(0));
3494 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3495 Node->getOperand(1));
3497 case ISD::FP_TO_SINT:
3498 case ISD::FP_TO_UINT:
3499 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3502 Tmp1 = Node->getOperand(0);
3505 // The input result is prerounded, so we don't have to do anything
3507 Tmp1 = PromoteOp(Node->getOperand(0));
3510 // If we're promoting a UINT to a larger size, check to see if the new node
3511 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3512 // we can use that instead. This allows us to generate better code for
3513 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3514 // legal, such as PowerPC.
3515 if (Node->getOpcode() == ISD::FP_TO_UINT &&
3516 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
3517 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3518 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
3519 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3521 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3527 Tmp1 = PromoteOp(Node->getOperand(0));
3528 assert(Tmp1.getValueType() == NVT);
3529 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3530 // NOTE: we do not have to do any extra rounding here for
3531 // NoExcessFPPrecision, because we know the input will have the appropriate
3532 // precision, and these operations don't modify precision at all.
3538 Tmp1 = PromoteOp(Node->getOperand(0));
3539 assert(Tmp1.getValueType() == NVT);
3540 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3541 if (NoExcessFPPrecision)
3542 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3543 DAG.getValueType(VT));
3547 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3548 // directly as well, which may be better.
3549 Tmp1 = PromoteOp(Node->getOperand(0));
3550 assert(Tmp1.getValueType() == NVT);
3551 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3552 if (NoExcessFPPrecision)
3553 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3554 DAG.getValueType(VT));
3564 // The input may have strange things in the top bits of the registers, but
3565 // these operations don't care. They may have weird bits going out, but
3566 // that too is okay if they are integer operations.
3567 Tmp1 = PromoteOp(Node->getOperand(0));
3568 Tmp2 = PromoteOp(Node->getOperand(1));
3569 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3570 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3575 Tmp1 = PromoteOp(Node->getOperand(0));
3576 Tmp2 = PromoteOp(Node->getOperand(1));
3577 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3578 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3580 // Floating point operations will give excess precision that we may not be
3581 // able to tolerate. If we DO allow excess precision, just leave it,
3582 // otherwise excise it.
3583 // FIXME: Why would we need to round FP ops more than integer ones?
3584 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
3585 if (NoExcessFPPrecision)
3586 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3587 DAG.getValueType(VT));
3592 // These operators require that their input be sign extended.
3593 Tmp1 = PromoteOp(Node->getOperand(0));
3594 Tmp2 = PromoteOp(Node->getOperand(1));
3595 if (MVT::isInteger(NVT)) {
3596 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3597 DAG.getValueType(VT));
3598 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3599 DAG.getValueType(VT));
3601 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3603 // Perform FP_ROUND: this is probably overly pessimistic.
3604 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
3605 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3606 DAG.getValueType(VT));
3610 case ISD::FCOPYSIGN:
3611 // These operators require that their input be fp extended.
3612 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3614 Tmp1 = LegalizeOp(Node->getOperand(0));
3617 Tmp1 = PromoteOp(Node->getOperand(0));
3620 assert(0 && "not implemented");
3622 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3624 Tmp2 = LegalizeOp(Node->getOperand(1));
3627 Tmp2 = PromoteOp(Node->getOperand(1));
3630 assert(0 && "not implemented");
3632 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3634 // Perform FP_ROUND: this is probably overly pessimistic.
3635 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
3636 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3637 DAG.getValueType(VT));
3642 // These operators require that their input be zero extended.
3643 Tmp1 = PromoteOp(Node->getOperand(0));
3644 Tmp2 = PromoteOp(Node->getOperand(1));
3645 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
3646 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3647 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3648 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3652 Tmp1 = PromoteOp(Node->getOperand(0));
3653 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
3656 // The input value must be properly sign extended.
3657 Tmp1 = PromoteOp(Node->getOperand(0));
3658 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3659 DAG.getValueType(VT));
3660 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
3663 // The input value must be properly zero extended.
3664 Tmp1 = PromoteOp(Node->getOperand(0));
3665 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3666 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
3670 Tmp1 = Node->getOperand(0); // Get the chain.
3671 Tmp2 = Node->getOperand(1); // Get the pointer.
3672 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3673 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3674 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3676 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
3677 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3678 SV->getValue(), SV->getOffset());
3679 // Increment the pointer, VAList, to the next vaarg
3680 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3681 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3682 TLI.getPointerTy()));
3683 // Store the incremented VAList to the legalized pointer
3684 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3686 // Load the actual argument out of the pointer VAList
3687 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
3689 // Remember that we legalized the chain.
3690 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
3694 LoadSDNode *LD = cast<LoadSDNode>(Node);
3695 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3696 ? ISD::EXTLOAD : LD->getExtensionType();
3697 Result = DAG.getExtLoad(ExtType, NVT,
3698 LD->getChain(), LD->getBasePtr(),
3699 LD->getSrcValue(), LD->getSrcValueOffset(),
3702 LD->getAlignment());
3703 // Remember that we legalized the chain.
3704 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
3708 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3709 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
3710 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
3712 case ISD::SELECT_CC:
3713 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3714 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3715 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3716 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
3719 Tmp1 = Node->getOperand(0);
3720 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3721 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3722 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3723 DAG.getConstant(MVT::getSizeInBits(NVT) -
3724 MVT::getSizeInBits(VT),
3725 TLI.getShiftAmountTy()));
3730 // Zero extend the argument
3731 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
3732 // Perform the larger operation, then subtract if needed.
3733 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3734 switch(Node->getOpcode()) {
3739 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3740 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3741 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3743 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
3744 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
3747 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3748 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3749 DAG.getConstant(MVT::getSizeInBits(NVT) -
3750 MVT::getSizeInBits(VT), NVT));
3754 case ISD::EXTRACT_SUBVECTOR:
3755 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
3757 case ISD::EXTRACT_VECTOR_ELT:
3758 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3762 assert(Result.Val && "Didn't set a result!");
3764 // Make sure the result is itself legal.
3765 Result = LegalizeOp(Result);
3767 // Remember that we promoted this!
3768 AddPromotedOperand(Op, Result);
3772 /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3773 /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3774 /// based on the vector type. The return type of this matches the element type
3775 /// of the vector, which may not be legal for the target.
3776 SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3777 // We know that operand #0 is the Vec vector. If the index is a constant
3778 // or if the invec is a supported hardware type, we can use it. Otherwise,
3779 // lower to a store then an indexed load.
3780 SDOperand Vec = Op.getOperand(0);
3781 SDOperand Idx = Op.getOperand(1);
3783 SDNode *InVal = Vec.Val;
3784 MVT::ValueType TVT = InVal->getValueType(0);
3785 unsigned NumElems = MVT::getVectorNumElements(TVT);
3787 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3788 default: assert(0 && "This action is not supported yet!");
3789 case TargetLowering::Custom: {
3790 Vec = LegalizeOp(Vec);
3791 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3792 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3797 case TargetLowering::Legal:
3798 if (isTypeLegal(TVT)) {
3799 Vec = LegalizeOp(Vec);
3800 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3804 case TargetLowering::Expand:
3808 if (NumElems == 1) {
3809 // This must be an access of the only element. Return it.
3810 Op = ScalarizeVectorOp(Vec);
3811 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3812 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3814 SplitVectorOp(Vec, Lo, Hi);
3815 if (CIdx->getValue() < NumElems/2) {
3819 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3820 Idx.getValueType());
3823 // It's now an extract from the appropriate high or low part. Recurse.
3824 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3825 Op = ExpandEXTRACT_VECTOR_ELT(Op);
3827 // Store the value to a temporary stack slot, then LOAD the scalar
3828 // element back out.
3829 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3830 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3832 // Add the offset to the index.
3833 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3834 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3835 DAG.getConstant(EltSize, Idx.getValueType()));
3836 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3838 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
3843 /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
3844 /// we assume the operation can be split if it is not already legal.
3845 SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
3846 // We know that operand #0 is the Vec vector. For now we assume the index
3847 // is a constant and that the extracted result is a supported hardware type.
3848 SDOperand Vec = Op.getOperand(0);
3849 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3851 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
3853 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3854 // This must be an access of the desired vector length. Return it.
3858 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3860 SplitVectorOp(Vec, Lo, Hi);
3861 if (CIdx->getValue() < NumElems/2) {
3865 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3868 // It's now an extract from the appropriate high or low part. Recurse.
3869 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3870 return ExpandEXTRACT_SUBVECTOR(Op);
3873 /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3874 /// with condition CC on the current target. This usually involves legalizing
3875 /// or promoting the arguments. In the case where LHS and RHS must be expanded,
3876 /// there may be no choice but to create a new SetCC node to represent the
3877 /// legalized value of setcc lhs, rhs. In this case, the value is returned in
3878 /// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3879 void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3882 SDOperand Tmp1, Tmp2, Result;
3884 switch (getTypeAction(LHS.getValueType())) {
3886 Tmp1 = LegalizeOp(LHS); // LHS
3887 Tmp2 = LegalizeOp(RHS); // RHS
3890 Tmp1 = PromoteOp(LHS); // LHS
3891 Tmp2 = PromoteOp(RHS); // RHS
3893 // If this is an FP compare, the operands have already been extended.
3894 if (MVT::isInteger(LHS.getValueType())) {
3895 MVT::ValueType VT = LHS.getValueType();
3896 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3898 // Otherwise, we have to insert explicit sign or zero extends. Note
3899 // that we could insert sign extends for ALL conditions, but zero extend
3900 // is cheaper on many machines (an AND instead of two shifts), so prefer
3902 switch (cast<CondCodeSDNode>(CC)->get()) {
3903 default: assert(0 && "Unknown integer comparison!");
3910 // ALL of these operations will work if we either sign or zero extend
3911 // the operands (including the unsigned comparisons!). Zero extend is
3912 // usually a simpler/cheaper operation, so prefer it.
3913 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3914 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3920 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3921 DAG.getValueType(VT));
3922 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3923 DAG.getValueType(VT));
3929 MVT::ValueType VT = LHS.getValueType();
3930 if (VT == MVT::f32 || VT == MVT::f64) {
3931 // Expand into one or more soft-fp libcall(s).
3932 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
3933 switch (cast<CondCodeSDNode>(CC)->get()) {
3936 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
3940 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
3944 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
3948 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
3952 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
3956 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
3959 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3962 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
3965 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3966 switch (cast<CondCodeSDNode>(CC)->get()) {
3968 // SETONE = SETOLT | SETOGT
3969 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
3972 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
3975 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
3978 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
3981 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
3984 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
3986 default: assert(0 && "Unsupported FP setcc!");
3991 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
3992 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
3993 false /*sign irrelevant*/, Dummy);
3994 Tmp2 = DAG.getConstant(0, MVT::i32);
3995 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
3996 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
3997 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
3998 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
3999 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4000 false /*sign irrelevant*/, Dummy);
4001 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4002 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4003 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4011 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4012 ExpandOp(LHS, LHSLo, LHSHi);
4013 ExpandOp(RHS, RHSLo, RHSHi);
4014 switch (cast<CondCodeSDNode>(CC)->get()) {
4018 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4019 if (RHSCST->isAllOnesValue()) {
4020 // Comparison to -1.
4021 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4026 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4027 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4028 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4029 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4032 // If this is a comparison of the sign bit, just look at the top part.
4034 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4035 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4036 CST->getValue() == 0) || // X < 0
4037 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4038 CST->isAllOnesValue())) { // X > -1
4044 // FIXME: This generated code sucks.
4045 ISD::CondCode LowCC;
4046 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4048 default: assert(0 && "Unknown integer setcc!");
4050 case ISD::SETULT: LowCC = ISD::SETULT; break;
4052 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4054 case ISD::SETULE: LowCC = ISD::SETULE; break;
4056 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4059 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4060 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4061 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4063 // NOTE: on targets without efficient SELECT of bools, we can always use
4064 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4065 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4066 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4067 false, DagCombineInfo);
4069 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4070 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4071 CCCode, false, DagCombineInfo);
4073 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4075 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4076 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4077 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4078 (Tmp2C && Tmp2C->getValue() == 0 &&
4079 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4080 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4081 (Tmp2C && Tmp2C->getValue() == 1 &&
4082 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4083 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4084 // low part is known false, returns high part.
4085 // For LE / GE, if high part is known false, ignore the low part.
4086 // For LT / GT, if high part is known true, ignore the low part.
4090 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4091 ISD::SETEQ, false, DagCombineInfo);
4093 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4094 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4095 Result, Tmp1, Tmp2));
4106 /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
4107 /// The resultant code need not be legal. Note that SrcOp is the input operand
4108 /// to the BIT_CONVERT, not the BIT_CONVERT node itself.
4109 SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4111 // Create the stack frame object.
4112 SDOperand FIPtr = CreateStackTemporary(DestVT);
4114 // Emit a store to the stack slot.
4115 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
4116 // Result is a load from the stack slot.
4117 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4120 SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4121 // Create a vector sized/aligned stack slot, store the value to element #0,
4122 // then load the whole vector back out.
4123 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
4124 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
4126 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
4130 /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4131 /// support the operation, but do support the resultant vector type.
4132 SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4134 // If the only non-undef value is the low element, turn this into a
4135 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4136 unsigned NumElems = Node->getNumOperands();
4137 bool isOnlyLowElement = true;
4138 SDOperand SplatValue = Node->getOperand(0);
4139 std::map<SDOperand, std::vector<unsigned> > Values;
4140 Values[SplatValue].push_back(0);
4141 bool isConstant = true;
4142 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4143 SplatValue.getOpcode() != ISD::UNDEF)
4146 for (unsigned i = 1; i < NumElems; ++i) {
4147 SDOperand V = Node->getOperand(i);
4148 Values[V].push_back(i);
4149 if (V.getOpcode() != ISD::UNDEF)
4150 isOnlyLowElement = false;
4151 if (SplatValue != V)
4152 SplatValue = SDOperand(0,0);
4154 // If this isn't a constant element or an undef, we can't use a constant
4156 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4157 V.getOpcode() != ISD::UNDEF)
4161 if (isOnlyLowElement) {
4162 // If the low element is an undef too, then this whole things is an undef.
4163 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4164 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4165 // Otherwise, turn this into a scalar_to_vector node.
4166 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4167 Node->getOperand(0));
4170 // If all elements are constants, create a load from the constant pool.
4172 MVT::ValueType VT = Node->getValueType(0);
4174 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4175 std::vector<Constant*> CV;
4176 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4177 if (ConstantFPSDNode *V =
4178 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
4179 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
4180 } else if (ConstantSDNode *V =
4181 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4182 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4184 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4185 CV.push_back(UndefValue::get(OpNTy));
4188 Constant *CP = ConstantVector::get(CV);
4189 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
4190 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
4193 if (SplatValue.Val) { // Splat of one value?
4194 // Build the shuffle constant vector: <0, 0, 0, 0>
4195 MVT::ValueType MaskVT =
4196 MVT::getIntVectorWithNumElements(NumElems);
4197 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4198 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4199 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4200 &ZeroVec[0], ZeroVec.size());
4202 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4203 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4204 // Get the splatted value into the low element of a vector register.
4205 SDOperand LowValVec =
4206 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4208 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4209 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4210 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4215 // If there are only two unique elements, we may be able to turn this into a
4217 if (Values.size() == 2) {
4218 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4219 MVT::ValueType MaskVT =
4220 MVT::getIntVectorWithNumElements(NumElems);
4221 std::vector<SDOperand> MaskVec(NumElems);
4223 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4224 E = Values.end(); I != E; ++I) {
4225 for (std::vector<unsigned>::iterator II = I->second.begin(),
4226 EE = I->second.end(); II != EE; ++II)
4227 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
4230 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4231 &MaskVec[0], MaskVec.size());
4233 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4234 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4235 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
4236 SmallVector<SDOperand, 8> Ops;
4237 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4238 E = Values.end(); I != E; ++I) {
4239 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4243 Ops.push_back(ShuffleMask);
4245 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
4246 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4247 &Ops[0], Ops.size());
4251 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4252 // aligned object on the stack, store each element into it, then load
4253 // the result as a vector.
4254 MVT::ValueType VT = Node->getValueType(0);
4255 // Create the stack frame object.
4256 SDOperand FIPtr = CreateStackTemporary(VT);
4258 // Emit a store of each element to the stack slot.
4259 SmallVector<SDOperand, 8> Stores;
4260 unsigned TypeByteSize =
4261 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
4262 // Store (in the right endianness) the elements to memory.
4263 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4264 // Ignore undef elements.
4265 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4267 unsigned Offset = TypeByteSize*i;
4269 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4270 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4272 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
4276 SDOperand StoreChain;
4277 if (!Stores.empty()) // Not all undef elements?
4278 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4279 &Stores[0], Stores.size());
4281 StoreChain = DAG.getEntryNode();
4283 // Result is a load from the stack slot.
4284 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
4287 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
4288 /// specified value type.
4289 SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4290 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4291 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
4292 const Type *Ty = MVT::getTypeForValueType(VT);
4293 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
4294 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
4295 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4298 void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4299 SDOperand Op, SDOperand Amt,
4300 SDOperand &Lo, SDOperand &Hi) {
4301 // Expand the subcomponents.
4302 SDOperand LHSL, LHSH;
4303 ExpandOp(Op, LHSL, LHSH);
4305 SDOperand Ops[] = { LHSL, LHSH, Amt };
4306 MVT::ValueType VT = LHSL.getValueType();
4307 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
4308 Hi = Lo.getValue(1);
4312 /// ExpandShift - Try to find a clever way to expand this shift operation out to
4313 /// smaller elements. If we can't find a way that is more efficient than a
4314 /// libcall on this target, return false. Otherwise, return true with the
4315 /// low-parts expanded into Lo and Hi.
4316 bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4317 SDOperand &Lo, SDOperand &Hi) {
4318 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4319 "This is not a shift!");
4321 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
4322 SDOperand ShAmt = LegalizeOp(Amt);
4323 MVT::ValueType ShTy = ShAmt.getValueType();
4324 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4325 unsigned NVTBits = MVT::getSizeInBits(NVT);
4327 // Handle the case when Amt is an immediate. Other cases are currently broken
4328 // and are disabled.
4329 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4330 unsigned Cst = CN->getValue();
4331 // Expand the incoming operand to be shifted, so that we have its parts
4333 ExpandOp(Op, InL, InH);
4337 Lo = DAG.getConstant(0, NVT);
4338 Hi = DAG.getConstant(0, NVT);
4339 } else if (Cst > NVTBits) {
4340 Lo = DAG.getConstant(0, NVT);
4341 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
4342 } else if (Cst == NVTBits) {
4343 Lo = DAG.getConstant(0, NVT);
4346 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4347 Hi = DAG.getNode(ISD::OR, NVT,
4348 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4349 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4354 Lo = DAG.getConstant(0, NVT);
4355 Hi = DAG.getConstant(0, NVT);
4356 } else if (Cst > NVTBits) {
4357 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4358 Hi = DAG.getConstant(0, NVT);
4359 } else if (Cst == NVTBits) {
4361 Hi = DAG.getConstant(0, NVT);
4363 Lo = DAG.getNode(ISD::OR, NVT,
4364 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4365 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4366 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4371 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
4372 DAG.getConstant(NVTBits-1, ShTy));
4373 } else if (Cst > NVTBits) {
4374 Lo = DAG.getNode(ISD::SRA, NVT, InH,
4375 DAG.getConstant(Cst-NVTBits, ShTy));
4376 Hi = DAG.getNode(ISD::SRA, NVT, InH,
4377 DAG.getConstant(NVTBits-1, ShTy));
4378 } else if (Cst == NVTBits) {
4380 Hi = DAG.getNode(ISD::SRA, NVT, InH,
4381 DAG.getConstant(NVTBits-1, ShTy));
4383 Lo = DAG.getNode(ISD::OR, NVT,
4384 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4385 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4386 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4392 // Okay, the shift amount isn't constant. However, if we can tell that it is
4393 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4394 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4395 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4397 // If we know that the high bit of the shift amount is one, then we can do
4398 // this as a couple of simple shifts.
4399 if (KnownOne & Mask) {
4400 // Mask out the high bit, which we know is set.
4401 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4402 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4404 // Expand the incoming operand to be shifted, so that we have its parts
4406 ExpandOp(Op, InL, InH);
4409 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4410 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4413 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4414 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4417 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4418 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4419 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4424 // If we know that the high bit of the shift amount is zero, then we can do
4425 // this as a couple of simple shifts.
4426 if (KnownZero & Mask) {
4428 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4429 DAG.getConstant(NVTBits, Amt.getValueType()),
4432 // Expand the incoming operand to be shifted, so that we have its parts
4434 ExpandOp(Op, InL, InH);
4437 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4438 Hi = DAG.getNode(ISD::OR, NVT,
4439 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4440 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4443 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4444 Lo = DAG.getNode(ISD::OR, NVT,
4445 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4446 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4449 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4450 Lo = DAG.getNode(ISD::OR, NVT,
4451 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4452 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4461 // ExpandLibCall - Expand a node into a call to a libcall. If the result value
4462 // does not fit into a register, return the lo part and set the hi part to the
4463 // by-reg argument. If it does fit into a single register, return the result
4464 // and leave the Hi part unset.
4465 SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
4466 bool isSigned, SDOperand &Hi) {
4467 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4468 // The input chain to this libcall is the entry node of the function.
4469 // Legalizing the call will automatically add the previous call to the
4471 SDOperand InChain = DAG.getEntryNode();
4473 TargetLowering::ArgListTy Args;
4474 TargetLowering::ArgListEntry Entry;
4475 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4476 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4477 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
4478 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4479 Entry.isSExt = isSigned;
4480 Args.push_back(Entry);
4482 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
4484 // Splice the libcall in wherever FindInputOutputChains tells us to.
4485 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
4486 std::pair<SDOperand,SDOperand> CallInfo =
4487 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
4490 // Legalize the call sequence, starting with the chain. This will advance
4491 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4492 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4493 LegalizeOp(CallInfo.second);
4495 switch (getTypeAction(CallInfo.first.getValueType())) {
4496 default: assert(0 && "Unknown thing");
4498 Result = CallInfo.first;
4501 ExpandOp(CallInfo.first, Result, Hi);
4508 /// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4510 SDOperand SelectionDAGLegalize::
4511 ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
4512 assert(getTypeAction(Source.getValueType()) == Expand &&
4513 "This is not an expansion!");
4514 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4517 assert(Source.getValueType() == MVT::i64 &&
4518 "This only works for 64-bit -> FP");
4519 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4520 // incoming integer is set. To handle this, we dynamically test to see if
4521 // it is set, and, if so, add a fudge factor.
4523 ExpandOp(Source, Lo, Hi);
4525 // If this is unsigned, and not supported, first perform the conversion to
4526 // signed, then adjust the result if the sign bit is set.
4527 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4528 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4530 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4531 DAG.getConstant(0, Hi.getValueType()),
4533 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4534 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4535 SignSet, Four, Zero);
4536 uint64_t FF = 0x5f800000ULL;
4537 if (TLI.isLittleEndian()) FF <<= 32;
4538 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
4540 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4541 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4542 SDOperand FudgeInReg;
4543 if (DestTy == MVT::f32)
4544 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
4546 assert(DestTy == MVT::f64 && "Unexpected conversion");
4547 // FIXME: Avoid the extend by construction the right constantpool?
4548 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
4549 CPIdx, NULL, 0, MVT::f32);
4551 MVT::ValueType SCVT = SignedConv.getValueType();
4552 if (SCVT != DestTy) {
4553 // Destination type needs to be expanded as well. The FADD now we are
4554 // constructing will be expanded into a libcall.
4555 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4556 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4557 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4558 SignedConv, SignedConv.getValue(1));
4560 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4562 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
4565 // Check to see if the target has a custom way to lower this. If so, use it.
4566 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4567 default: assert(0 && "This action not implemented for this operation!");
4568 case TargetLowering::Legal:
4569 case TargetLowering::Expand:
4570 break; // This case is handled below.
4571 case TargetLowering::Custom: {
4572 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4575 return LegalizeOp(NV);
4576 break; // The target decided this was legal after all
4580 // Expand the source, then glue it back together for the call. We must expand
4581 // the source in case it is shared (this pass of legalize must traverse it).
4582 SDOperand SrcLo, SrcHi;
4583 ExpandOp(Source, SrcLo, SrcHi);
4584 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4587 if (DestTy == MVT::f32)
4588 LC = RTLIB::SINTTOFP_I64_F32;
4590 assert(DestTy == MVT::f64 && "Unknown fp value type!");
4591 LC = RTLIB::SINTTOFP_I64_F64;
4594 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
4595 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4596 SDOperand UnusedHiPart;
4597 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4601 /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4602 /// INT_TO_FP operation of the specified operand when the target requests that
4603 /// we expand it. At this point, we know that the result and operand types are
4604 /// legal for the target.
4605 SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4607 MVT::ValueType DestVT) {
4608 if (Op0.getValueType() == MVT::i32) {
4609 // simple 32-bit [signed|unsigned] integer to float/double expansion
4611 // get the stack frame index of a 8 byte buffer, pessimistically aligned
4612 MachineFunction &MF = DAG.getMachineFunction();
4613 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4614 unsigned StackAlign =
4615 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
4616 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
4617 // get address of 8 byte buffer
4618 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4619 // word offset constant for Hi/Lo address computation
4620 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4621 // set up Hi and Lo (into buffer) address based on endian
4622 SDOperand Hi = StackSlot;
4623 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4624 if (TLI.isLittleEndian())
4627 // if signed map to unsigned space
4628 SDOperand Op0Mapped;
4630 // constant used to invert sign bit (signed to unsigned mapping)
4631 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4632 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4636 // store the lo of the constructed double - based on integer input
4637 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
4638 Op0Mapped, Lo, NULL, 0);
4639 // initial hi portion of constructed double
4640 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4641 // store the hi of the constructed double - biased exponent
4642 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
4643 // load the constructed double
4644 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
4645 // FP constant to bias correct the final result
4646 SDOperand Bias = DAG.getConstantFP(isSigned ?
4647 BitsToDouble(0x4330000080000000ULL)
4648 : BitsToDouble(0x4330000000000000ULL),
4650 // subtract the bias
4651 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4654 // handle final rounding
4655 if (DestVT == MVT::f64) {
4659 // if f32 then cast to f32
4660 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4664 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4665 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4667 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4668 DAG.getConstant(0, Op0.getValueType()),
4670 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4671 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4672 SignSet, Four, Zero);
4674 // If the sign bit of the integer is set, the large number will be treated
4675 // as a negative number. To counteract this, the dynamic code adds an
4676 // offset depending on the data type.
4678 switch (Op0.getValueType()) {
4679 default: assert(0 && "Unsupported integer type!");
4680 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4681 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4682 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4683 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4685 if (TLI.isLittleEndian()) FF <<= 32;
4686 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
4688 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4689 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4690 SDOperand FudgeInReg;
4691 if (DestVT == MVT::f32)
4692 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
4694 assert(DestVT == MVT::f64 && "Unexpected conversion");
4695 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4696 DAG.getEntryNode(), CPIdx,
4697 NULL, 0, MVT::f32));
4700 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4703 /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4704 /// *INT_TO_FP operation of the specified operand when the target requests that
4705 /// we promote it. At this point, we know that the result and operand types are
4706 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4707 /// operation that takes a larger input.
4708 SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4709 MVT::ValueType DestVT,
4711 // First step, figure out the appropriate *INT_TO_FP operation to use.
4712 MVT::ValueType NewInTy = LegalOp.getValueType();
4714 unsigned OpToUse = 0;
4716 // Scan for the appropriate larger type to use.
4718 NewInTy = (MVT::ValueType)(NewInTy+1);
4719 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4721 // If the target supports SINT_TO_FP of this type, use it.
4722 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4724 case TargetLowering::Legal:
4725 if (!TLI.isTypeLegal(NewInTy))
4726 break; // Can't use this datatype.
4728 case TargetLowering::Custom:
4729 OpToUse = ISD::SINT_TO_FP;
4733 if (isSigned) continue;
4735 // If the target supports UINT_TO_FP of this type, use it.
4736 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4738 case TargetLowering::Legal:
4739 if (!TLI.isTypeLegal(NewInTy))
4740 break; // Can't use this datatype.
4742 case TargetLowering::Custom:
4743 OpToUse = ISD::UINT_TO_FP;
4748 // Otherwise, try a larger type.
4751 // Okay, we found the operation and type to use. Zero extend our input to the
4752 // desired type then run the operation on it.
4753 return DAG.getNode(OpToUse, DestVT,
4754 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4758 /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4759 /// FP_TO_*INT operation of the specified operand when the target requests that
4760 /// we promote it. At this point, we know that the result and operand types are
4761 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4762 /// operation that returns a larger result.
4763 SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4764 MVT::ValueType DestVT,
4766 // First step, figure out the appropriate FP_TO*INT operation to use.
4767 MVT::ValueType NewOutTy = DestVT;
4769 unsigned OpToUse = 0;
4771 // Scan for the appropriate larger type to use.
4773 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4774 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4776 // If the target supports FP_TO_SINT returning this type, use it.
4777 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4779 case TargetLowering::Legal:
4780 if (!TLI.isTypeLegal(NewOutTy))
4781 break; // Can't use this datatype.
4783 case TargetLowering::Custom:
4784 OpToUse = ISD::FP_TO_SINT;
4789 // If the target supports FP_TO_UINT of this type, use it.
4790 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4792 case TargetLowering::Legal:
4793 if (!TLI.isTypeLegal(NewOutTy))
4794 break; // Can't use this datatype.
4796 case TargetLowering::Custom:
4797 OpToUse = ISD::FP_TO_UINT;
4802 // Otherwise, try a larger type.
4805 // Okay, we found the operation and type to use. Truncate the result of the
4806 // extended FP_TO_*INT operation to the desired size.
4807 return DAG.getNode(ISD::TRUNCATE, DestVT,
4808 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4811 /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4813 SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4814 MVT::ValueType VT = Op.getValueType();
4815 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4816 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4818 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4820 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4821 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4822 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4824 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4825 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4826 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4827 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4828 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4829 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4830 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4831 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4832 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4834 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4835 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4836 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4837 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4838 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4839 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4840 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4841 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4842 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4843 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4844 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4845 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4846 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4847 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4848 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4849 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4850 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4851 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4852 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4853 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4854 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4858 /// ExpandBitCount - Expand the specified bitcount instruction into operations.
4860 SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4862 default: assert(0 && "Cannot expand this yet!");
4864 static const uint64_t mask[6] = {
4865 0x5555555555555555ULL, 0x3333333333333333ULL,
4866 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4867 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4869 MVT::ValueType VT = Op.getValueType();
4870 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4871 unsigned len = MVT::getSizeInBits(VT);
4872 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4873 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4874 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4875 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4876 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4877 DAG.getNode(ISD::AND, VT,
4878 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4883 // for now, we do this:
4884 // x = x | (x >> 1);
4885 // x = x | (x >> 2);
4887 // x = x | (x >>16);
4888 // x = x | (x >>32); // for 64-bit input
4889 // return popcount(~x);
4891 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4892 MVT::ValueType VT = Op.getValueType();
4893 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4894 unsigned len = MVT::getSizeInBits(VT);
4895 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4896 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4897 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4899 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4900 return DAG.getNode(ISD::CTPOP, VT, Op);
4903 // for now, we use: { return popcount(~x & (x - 1)); }
4904 // unless the target has ctlz but not ctpop, in which case we use:
4905 // { return 32 - nlz(~x & (x-1)); }
4906 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4907 MVT::ValueType VT = Op.getValueType();
4908 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4909 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4910 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4911 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4912 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4913 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4914 TLI.isOperationLegal(ISD::CTLZ, VT))
4915 return DAG.getNode(ISD::SUB, VT,
4916 DAG.getConstant(MVT::getSizeInBits(VT), VT),
4917 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4918 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4923 /// ExpandOp - Expand the specified SDOperand into its two component pieces
4924 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4925 /// LegalizeNodes map is filled in for any results that are not expanded, the
4926 /// ExpandedNodes map is filled in for any results that are expanded, and the
4927 /// Lo/Hi values are returned.
4928 void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4929 MVT::ValueType VT = Op.getValueType();
4930 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4931 SDNode *Node = Op.Val;
4932 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
4933 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4934 MVT::isVector(VT)) &&
4935 "Cannot expand to FP value or to larger int value!");
4937 // See if we already expanded it.
4938 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4939 = ExpandedNodes.find(Op);
4940 if (I != ExpandedNodes.end()) {
4941 Lo = I->second.first;
4942 Hi = I->second.second;
4946 switch (Node->getOpcode()) {
4947 case ISD::CopyFromReg:
4948 assert(0 && "CopyFromReg must be legal!");
4951 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4953 assert(0 && "Do not know how to expand this operator!");
4956 NVT = TLI.getTypeToExpandTo(VT);
4957 Lo = DAG.getNode(ISD::UNDEF, NVT);
4958 Hi = DAG.getNode(ISD::UNDEF, NVT);
4960 case ISD::Constant: {
4961 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4962 Lo = DAG.getConstant(Cst, NVT);
4963 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4966 case ISD::ConstantFP: {
4967 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
4968 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
4969 if (getTypeAction(Lo.getValueType()) == Expand)
4970 ExpandOp(Lo, Lo, Hi);
4973 case ISD::BUILD_PAIR:
4974 // Return the operands.
4975 Lo = Node->getOperand(0);
4976 Hi = Node->getOperand(1);
4979 case ISD::SIGN_EXTEND_INREG:
4980 ExpandOp(Node->getOperand(0), Lo, Hi);
4981 // sext_inreg the low part if needed.
4982 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4984 // The high part gets the sign extension from the lo-part. This handles
4985 // things like sextinreg V:i64 from i8.
4986 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4987 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4988 TLI.getShiftAmountTy()));
4992 ExpandOp(Node->getOperand(0), Lo, Hi);
4993 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4994 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5000 ExpandOp(Node->getOperand(0), Lo, Hi);
5001 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5002 DAG.getNode(ISD::CTPOP, NVT, Lo),
5003 DAG.getNode(ISD::CTPOP, NVT, Hi));
5004 Hi = DAG.getConstant(0, NVT);
5008 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5009 ExpandOp(Node->getOperand(0), Lo, Hi);
5010 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5011 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5012 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5014 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5015 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5017 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5018 Hi = DAG.getConstant(0, NVT);
5023 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5024 ExpandOp(Node->getOperand(0), Lo, Hi);
5025 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5026 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5027 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5029 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5030 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5032 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5033 Hi = DAG.getConstant(0, NVT);
5038 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5039 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5040 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5041 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5043 // Remember that we legalized the chain.
5044 Hi = LegalizeOp(Hi);
5045 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5046 if (!TLI.isLittleEndian())
5052 LoadSDNode *LD = cast<LoadSDNode>(Node);
5053 SDOperand Ch = LD->getChain(); // Legalize the chain.
5054 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5055 ISD::LoadExtType ExtType = LD->getExtensionType();
5056 int SVOffset = LD->getSrcValueOffset();
5057 unsigned Alignment = LD->getAlignment();
5058 bool isVolatile = LD->isVolatile();
5060 if (ExtType == ISD::NON_EXTLOAD) {
5061 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5062 isVolatile, Alignment);
5063 if (VT == MVT::f32 || VT == MVT::f64) {
5064 // f32->i32 or f64->i64 one to one expansion.
5065 // Remember that we legalized the chain.
5066 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5067 // Recursively expand the new load.
5068 if (getTypeAction(NVT) == Expand)
5069 ExpandOp(Lo, Lo, Hi);
5073 // Increment the pointer to the other half.
5074 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5075 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5076 getIntPtrConstant(IncrementSize));
5077 SVOffset += IncrementSize;
5078 if (Alignment > IncrementSize)
5079 Alignment = IncrementSize;
5080 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5081 isVolatile, Alignment);
5083 // Build a factor node to remember that this load is independent of the
5085 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5088 // Remember that we legalized the chain.
5089 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5090 if (!TLI.isLittleEndian())
5093 MVT::ValueType EVT = LD->getLoadedVT();
5095 if (VT == MVT::f64 && EVT == MVT::f32) {
5096 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5097 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5098 SVOffset, isVolatile, Alignment);
5099 // Remember that we legalized the chain.
5100 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5101 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5106 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5107 SVOffset, isVolatile, Alignment);
5109 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5110 SVOffset, EVT, isVolatile,
5113 // Remember that we legalized the chain.
5114 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5116 if (ExtType == ISD::SEXTLOAD) {
5117 // The high part is obtained by SRA'ing all but one of the bits of the
5119 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5120 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5121 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5122 } else if (ExtType == ISD::ZEXTLOAD) {
5123 // The high part is just a zero.
5124 Hi = DAG.getConstant(0, NVT);
5125 } else /* if (ExtType == ISD::EXTLOAD) */ {
5126 // The high part is undefined.
5127 Hi = DAG.getNode(ISD::UNDEF, NVT);
5134 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5135 SDOperand LL, LH, RL, RH;
5136 ExpandOp(Node->getOperand(0), LL, LH);
5137 ExpandOp(Node->getOperand(1), RL, RH);
5138 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5139 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5143 SDOperand LL, LH, RL, RH;
5144 ExpandOp(Node->getOperand(1), LL, LH);
5145 ExpandOp(Node->getOperand(2), RL, RH);
5146 if (getTypeAction(NVT) == Expand)
5147 NVT = TLI.getTypeToExpandTo(NVT);
5148 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5150 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5153 case ISD::SELECT_CC: {
5154 SDOperand TL, TH, FL, FH;
5155 ExpandOp(Node->getOperand(2), TL, TH);
5156 ExpandOp(Node->getOperand(3), FL, FH);
5157 if (getTypeAction(NVT) == Expand)
5158 NVT = TLI.getTypeToExpandTo(NVT);
5159 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5160 Node->getOperand(1), TL, FL, Node->getOperand(4));
5162 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5163 Node->getOperand(1), TH, FH, Node->getOperand(4));
5166 case ISD::ANY_EXTEND:
5167 // The low part is any extension of the input (which degenerates to a copy).
5168 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5169 // The high part is undefined.
5170 Hi = DAG.getNode(ISD::UNDEF, NVT);
5172 case ISD::SIGN_EXTEND: {
5173 // The low part is just a sign extension of the input (which degenerates to
5175 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
5177 // The high part is obtained by SRA'ing all but one of the bits of the lo
5179 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5180 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5181 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5184 case ISD::ZERO_EXTEND:
5185 // The low part is just a zero extension of the input (which degenerates to
5187 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
5189 // The high part is just a zero.
5190 Hi = DAG.getConstant(0, NVT);
5193 case ISD::TRUNCATE: {
5194 // The input value must be larger than this value. Expand *it*.
5196 ExpandOp(Node->getOperand(0), NewLo, Hi);
5198 // The low part is now either the right size, or it is closer. If not the
5199 // right size, make an illegal truncate so we recursively expand it.
5200 if (NewLo.getValueType() != Node->getValueType(0))
5201 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5202 ExpandOp(NewLo, Lo, Hi);
5206 case ISD::BIT_CONVERT: {
5208 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5209 // If the target wants to, allow it to lower this itself.
5210 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5211 case Expand: assert(0 && "cannot expand FP!");
5212 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5213 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5215 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5218 // f32 / f64 must be expanded to i32 / i64.
5219 if (VT == MVT::f32 || VT == MVT::f64) {
5220 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5221 if (getTypeAction(NVT) == Expand)
5222 ExpandOp(Lo, Lo, Hi);
5226 // If source operand will be expanded to the same type as VT, i.e.
5227 // i64 <- f64, i32 <- f32, expand the source operand instead.
5228 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5229 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5230 ExpandOp(Node->getOperand(0), Lo, Hi);
5234 // Turn this into a load/store pair by default.
5236 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
5238 ExpandOp(Tmp, Lo, Hi);
5242 case ISD::READCYCLECOUNTER:
5243 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5244 TargetLowering::Custom &&
5245 "Must custom expand ReadCycleCounter");
5246 Lo = TLI.LowerOperation(Op, DAG);
5247 assert(Lo.Val && "Node must be custom expanded!");
5248 Hi = Lo.getValue(1);
5249 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
5250 LegalizeOp(Lo.getValue(2)));
5253 // These operators cannot be expanded directly, emit them as calls to
5254 // library functions.
5255 case ISD::FP_TO_SINT: {
5256 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
5258 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5259 case Expand: assert(0 && "cannot expand FP!");
5260 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5261 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5264 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5266 // Now that the custom expander is done, expand the result, which is still
5269 ExpandOp(Op, Lo, Hi);
5275 if (Node->getOperand(0).getValueType() == MVT::f32)
5276 LC = RTLIB::FPTOSINT_F32_I64;
5278 LC = RTLIB::FPTOSINT_F64_I64;
5279 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5280 false/*sign irrelevant*/, Hi);
5284 case ISD::FP_TO_UINT: {
5285 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
5287 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5288 case Expand: assert(0 && "cannot expand FP!");
5289 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5290 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5293 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5295 // Now that the custom expander is done, expand the result.
5297 ExpandOp(Op, Lo, Hi);
5303 if (Node->getOperand(0).getValueType() == MVT::f32)
5304 LC = RTLIB::FPTOUINT_F32_I64;
5306 LC = RTLIB::FPTOUINT_F64_I64;
5307 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5308 false/*sign irrelevant*/, Hi);
5313 // If the target wants custom lowering, do so.
5314 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5315 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
5316 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
5317 Op = TLI.LowerOperation(Op, DAG);
5319 // Now that the custom expander is done, expand the result, which is
5321 ExpandOp(Op, Lo, Hi);
5326 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5327 // this X << 1 as X+X.
5328 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5329 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5330 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5331 SDOperand LoOps[2], HiOps[3];
5332 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5333 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5334 LoOps[1] = LoOps[0];
5335 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5337 HiOps[1] = HiOps[0];
5338 HiOps[2] = Lo.getValue(1);
5339 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5344 // If we can emit an efficient shift operation, do so now.
5345 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
5348 // If this target supports SHL_PARTS, use it.
5349 TargetLowering::LegalizeAction Action =
5350 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5351 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5352 Action == TargetLowering::Custom) {
5353 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5357 // Otherwise, emit a libcall.
5358 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5359 false/*left shift=unsigned*/, Hi);
5364 // If the target wants custom lowering, do so.
5365 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5366 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
5367 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
5368 Op = TLI.LowerOperation(Op, DAG);
5370 // Now that the custom expander is done, expand the result, which is
5372 ExpandOp(Op, Lo, Hi);
5377 // If we can emit an efficient shift operation, do so now.
5378 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
5381 // If this target supports SRA_PARTS, use it.
5382 TargetLowering::LegalizeAction Action =
5383 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5384 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5385 Action == TargetLowering::Custom) {
5386 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5390 // Otherwise, emit a libcall.
5391 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5392 true/*ashr is signed*/, Hi);
5397 // If the target wants custom lowering, do so.
5398 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5399 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
5400 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
5401 Op = TLI.LowerOperation(Op, DAG);
5403 // Now that the custom expander is done, expand the result, which is
5405 ExpandOp(Op, Lo, Hi);
5410 // If we can emit an efficient shift operation, do so now.
5411 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
5414 // If this target supports SRL_PARTS, use it.
5415 TargetLowering::LegalizeAction Action =
5416 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5417 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5418 Action == TargetLowering::Custom) {
5419 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5423 // Otherwise, emit a libcall.
5424 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5425 false/*lshr is unsigned*/, Hi);
5431 // If the target wants to custom expand this, let them.
5432 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5433 TargetLowering::Custom) {
5434 Op = TLI.LowerOperation(Op, DAG);
5436 ExpandOp(Op, Lo, Hi);
5441 // Expand the subcomponents.
5442 SDOperand LHSL, LHSH, RHSL, RHSH;
5443 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5444 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5445 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5446 SDOperand LoOps[2], HiOps[3];
5451 if (Node->getOpcode() == ISD::ADD) {
5452 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5453 HiOps[2] = Lo.getValue(1);
5454 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5456 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5457 HiOps[2] = Lo.getValue(1);
5458 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5465 // Expand the subcomponents.
5466 SDOperand LHSL, LHSH, RHSL, RHSH;
5467 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5468 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5469 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5470 SDOperand LoOps[2] = { LHSL, RHSL };
5471 SDOperand HiOps[3] = { LHSH, RHSH };
5473 if (Node->getOpcode() == ISD::ADDC) {
5474 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5475 HiOps[2] = Lo.getValue(1);
5476 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5478 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5479 HiOps[2] = Lo.getValue(1);
5480 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5482 // Remember that we legalized the flag.
5483 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5488 // Expand the subcomponents.
5489 SDOperand LHSL, LHSH, RHSL, RHSH;
5490 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5491 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5492 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5493 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5494 SDOperand HiOps[3] = { LHSH, RHSH };
5496 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5497 HiOps[2] = Lo.getValue(1);
5498 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5500 // Remember that we legalized the flag.
5501 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5505 // If the target wants to custom expand this, let them.
5506 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
5507 SDOperand New = TLI.LowerOperation(Op, DAG);
5509 ExpandOp(New, Lo, Hi);
5514 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5515 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
5516 if (HasMULHS || HasMULHU) {
5517 SDOperand LL, LH, RL, RH;
5518 ExpandOp(Node->getOperand(0), LL, LH);
5519 ExpandOp(Node->getOperand(1), RL, RH);
5520 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
5521 // FIXME: Move this to the dag combiner.
5522 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5523 // extended the sign bit of the low half through the upper half, and if so
5524 // emit a MULHS instead of the alternate sequence that is valid for any
5525 // i64 x i64 multiply.
5527 // is RH an extension of the sign bit of RL?
5528 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5529 RH.getOperand(1).getOpcode() == ISD::Constant &&
5530 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5531 // is LH an extension of the sign bit of LL?
5532 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5533 LH.getOperand(1).getOpcode() == ISD::Constant &&
5534 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
5536 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5538 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5540 } else if (HasMULHU) {
5542 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5545 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5546 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5547 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5548 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5549 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
5554 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5555 false/*sign irrelevant*/, Hi);
5559 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5562 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5565 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5568 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5572 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5573 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5577 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5578 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5582 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5583 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5587 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5588 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5591 case ISD::FP_EXTEND:
5592 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
5595 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
5600 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5601 switch(Node->getOpcode()) {
5603 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5606 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5609 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5611 default: assert(0 && "Unreachable!");
5613 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
5617 SDOperand Mask = (VT == MVT::f64)
5618 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5619 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5620 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5621 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5622 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5623 if (getTypeAction(NVT) == Expand)
5624 ExpandOp(Lo, Lo, Hi);
5628 SDOperand Mask = (VT == MVT::f64)
5629 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5630 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5631 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5632 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5633 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5634 if (getTypeAction(NVT) == Expand)
5635 ExpandOp(Lo, Lo, Hi);
5638 case ISD::FCOPYSIGN: {
5639 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5640 if (getTypeAction(NVT) == Expand)
5641 ExpandOp(Lo, Lo, Hi);
5644 case ISD::SINT_TO_FP:
5645 case ISD::UINT_TO_FP: {
5646 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5647 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
5649 if (Node->getOperand(0).getValueType() == MVT::i64) {
5651 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
5653 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
5656 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
5658 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
5661 // Promote the operand if needed.
5662 if (getTypeAction(SrcVT) == Promote) {
5663 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5665 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5666 DAG.getValueType(SrcVT))
5667 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5668 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5671 const char *LibCall = TLI.getLibcallName(LC);
5673 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5675 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5676 Node->getOperand(0));
5677 if (getTypeAction(Lo.getValueType()) == Expand)
5678 ExpandOp(Lo, Lo, Hi);
5684 // Make sure the resultant values have been legalized themselves, unless this
5685 // is a type that requires multi-step expansion.
5686 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5687 Lo = LegalizeOp(Lo);
5689 // Don't legalize the high part if it is expanded to a single node.
5690 Hi = LegalizeOp(Hi);
5693 // Remember in a map if the values will be reused later.
5694 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
5695 assert(isNew && "Value already expanded?!?");
5698 /// SplitVectorOp - Given an operand of vector type, break it down into
5699 /// two smaller values, still of vector type.
5700 void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5702 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
5703 SDNode *Node = Op.Val;
5704 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
5705 assert(NumElements > 1 && "Cannot split a single element vector!");
5706 unsigned NewNumElts = NumElements/2;
5707 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5708 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
5710 // See if we already split it.
5711 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5712 = SplitNodes.find(Op);
5713 if (I != SplitNodes.end()) {
5714 Lo = I->second.first;
5715 Hi = I->second.second;
5719 switch (Node->getOpcode()) {
5724 assert(0 && "Unhandled operation in SplitVectorOp!");
5725 case ISD::BUILD_PAIR:
5726 Lo = Node->getOperand(0);
5727 Hi = Node->getOperand(1);
5729 case ISD::BUILD_VECTOR: {
5730 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5731 Node->op_begin()+NewNumElts);
5732 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
5734 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5736 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
5739 case ISD::CONCAT_VECTORS: {
5740 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5741 if (NewNumSubvectors == 1) {
5742 Lo = Node->getOperand(0);
5743 Hi = Node->getOperand(1);
5745 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5746 Node->op_begin()+NewNumSubvectors);
5747 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
5749 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5751 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5767 SDOperand LL, LH, RL, RH;
5768 SplitVectorOp(Node->getOperand(0), LL, LH);
5769 SplitVectorOp(Node->getOperand(1), RL, RH);
5771 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5772 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
5776 LoadSDNode *LD = cast<LoadSDNode>(Node);
5777 SDOperand Ch = LD->getChain();
5778 SDOperand Ptr = LD->getBasePtr();
5779 const Value *SV = LD->getSrcValue();
5780 int SVOffset = LD->getSrcValueOffset();
5781 unsigned Alignment = LD->getAlignment();
5782 bool isVolatile = LD->isVolatile();
5784 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5785 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
5786 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5787 getIntPtrConstant(IncrementSize));
5788 SVOffset += IncrementSize;
5789 if (Alignment > IncrementSize)
5790 Alignment = IncrementSize;
5791 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5793 // Build a factor node to remember that this load is independent of the
5795 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5798 // Remember that we legalized the chain.
5799 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5802 case ISD::BIT_CONVERT: {
5803 // We know the result is a vector. The input may be either a vector or a
5805 SDOperand InOp = Node->getOperand(0);
5806 if (!MVT::isVector(InOp.getValueType()) ||
5807 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5808 // The input is a scalar or single-element vector.
5809 // Lower to a store/load so that it can be split.
5810 // FIXME: this could be improved probably.
5811 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
5813 SDOperand St = DAG.getStore(DAG.getEntryNode(),
5814 InOp, Ptr, NULL, 0);
5815 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
5817 // Split the vector and convert each of the pieces now.
5818 SplitVectorOp(InOp, Lo, Hi);
5819 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5820 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5825 // Remember in a map if the values will be reused later.
5827 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5828 assert(isNew && "Value already split?!?");
5832 /// ScalarizeVectorOp - Given an operand of single-element vector type
5833 /// (e.g. v1f32), convert it into the equivalent operation that returns a
5834 /// scalar (e.g. f32) value.
5835 SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5836 assert(MVT::isVector(Op.getValueType()) &&
5837 "Bad ScalarizeVectorOp invocation!");
5838 SDNode *Node = Op.Val;
5839 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5840 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
5842 // See if we already scalarized it.
5843 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5844 if (I != ScalarizedNodes.end()) return I->second;
5847 switch (Node->getOpcode()) {
5850 Node->dump(&DAG); cerr << "\n";
5852 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5868 Result = DAG.getNode(Node->getOpcode(),
5870 ScalarizeVectorOp(Node->getOperand(0)),
5871 ScalarizeVectorOp(Node->getOperand(1)));
5878 Result = DAG.getNode(Node->getOpcode(),
5880 ScalarizeVectorOp(Node->getOperand(0)));
5883 LoadSDNode *LD = cast<LoadSDNode>(Node);
5884 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5885 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
5887 const Value *SV = LD->getSrcValue();
5888 int SVOffset = LD->getSrcValueOffset();
5889 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5890 LD->isVolatile(), LD->getAlignment());
5892 // Remember that we legalized the chain.
5893 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5896 case ISD::BUILD_VECTOR:
5897 Result = Node->getOperand(0);
5899 case ISD::INSERT_VECTOR_ELT:
5900 // Returning the inserted scalar element.
5901 Result = Node->getOperand(1);
5903 case ISD::CONCAT_VECTORS:
5904 assert(Node->getOperand(0).getValueType() == NewVT &&
5905 "Concat of non-legal vectors not yet supported!");
5906 Result = Node->getOperand(0);
5908 case ISD::VECTOR_SHUFFLE: {
5909 // Figure out if the scalar is the LHS or RHS and return it.
5910 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5911 if (cast<ConstantSDNode>(EltNum)->getValue())
5912 Result = ScalarizeVectorOp(Node->getOperand(1));
5914 Result = ScalarizeVectorOp(Node->getOperand(0));
5917 case ISD::EXTRACT_SUBVECTOR:
5918 Result = Node->getOperand(0);
5919 assert(Result.getValueType() == NewVT);
5921 case ISD::BIT_CONVERT:
5922 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5925 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5926 ScalarizeVectorOp(Op.getOperand(1)),
5927 ScalarizeVectorOp(Op.getOperand(2)));
5931 if (TLI.isTypeLegal(NewVT))
5932 Result = LegalizeOp(Result);
5933 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5934 assert(isNew && "Value already scalarized?");
5939 // SelectionDAG::Legalize - This is the entry point for the file.
5941 void SelectionDAG::Legalize() {
5942 if (ViewLegalizeDAGs) viewGraph();
5944 /// run - This is the main entry point to this class.
5946 SelectionDAGLegalize(*this).LegalizeDAG();